|
| 1 | +// filepath: e:\Project\Roo-Code\src\api\providers\human-relay.ts |
| 2 | +import { Anthropic } from "@anthropic-ai/sdk" |
| 3 | +import { ApiHandlerOptions, ModelInfo } from "../../shared/api" |
| 4 | +import { ApiHandler, SingleCompletionHandler } from "../index" |
| 5 | +import { ApiStream } from "../transform/stream" |
| 6 | +import * as vscode from "vscode" |
| 7 | +import { ExtensionMessage } from "../../shared/ExtensionMessage" |
| 8 | +import { getPanel } from "../../activate/registerCommands" // Import the getPanel function |
| 9 | + |
| 10 | +/** |
| 11 | + * Human Relay API processor |
| 12 | + * This processor does not directly call the API, but interacts with the model through human operations copy and paste. |
| 13 | + */ |
| 14 | +export class HumanRelayHandler implements ApiHandler, SingleCompletionHandler { |
| 15 | + private options: ApiHandlerOptions |
| 16 | + |
| 17 | + constructor(options: ApiHandlerOptions) { |
| 18 | + this.options = options |
| 19 | + } |
| 20 | + countTokens(content: Array<Anthropic.Messages.ContentBlockParam>): Promise<number> { |
| 21 | + return Promise.resolve(0) |
| 22 | + } |
| 23 | + |
| 24 | + /** |
| 25 | + * Create a message processing flow, display a dialog box to request human assistance |
| 26 | + * @param systemPrompt System prompt words |
| 27 | + * @param messages Message list |
| 28 | + */ |
| 29 | + async *createMessage(systemPrompt: string, messages: Anthropic.Messages.MessageParam[]): ApiStream { |
| 30 | + // Get the most recent user message |
| 31 | + const latestMessage = messages[messages.length - 1] |
| 32 | + |
| 33 | + if (!latestMessage) { |
| 34 | + throw new Error("No message to relay") |
| 35 | + } |
| 36 | + |
| 37 | + // If it is the first message, splice the system prompt word with the user message |
| 38 | + let promptText = "" |
| 39 | + if (messages.length === 1) { |
| 40 | + promptText = `${systemPrompt}\n\n${getMessageContent(latestMessage)}` |
| 41 | + } else { |
| 42 | + promptText = getMessageContent(latestMessage) |
| 43 | + } |
| 44 | + |
| 45 | + // Copy to clipboard |
| 46 | + await vscode.env.clipboard.writeText(promptText) |
| 47 | + |
| 48 | + // A dialog box pops up to request user action |
| 49 | + const response = await showHumanRelayDialog(promptText) |
| 50 | + |
| 51 | + if (!response) { |
| 52 | + // The user canceled the operation |
| 53 | + throw new Error("Human relay operation cancelled") |
| 54 | + } |
| 55 | + |
| 56 | + // Return to the user input reply |
| 57 | + yield { type: "text", text: response } |
| 58 | + } |
| 59 | + |
| 60 | + /** |
| 61 | + * Get model information |
| 62 | + */ |
| 63 | + getModel(): { id: string; info: ModelInfo } { |
| 64 | + // Human relay does not depend on a specific model, here is a default configuration |
| 65 | + return { |
| 66 | + id: "human-relay", |
| 67 | + info: { |
| 68 | + maxTokens: 16384, |
| 69 | + contextWindow: 100000, |
| 70 | + supportsImages: true, |
| 71 | + supportsPromptCache: false, |
| 72 | + supportsComputerUse: true, |
| 73 | + inputPrice: 0, |
| 74 | + outputPrice: 0, |
| 75 | + description: "Calling web-side AI model through human relay", |
| 76 | + }, |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + /** |
| 81 | + * Implementation of a single prompt |
| 82 | + * @param prompt Prompt content |
| 83 | + */ |
| 84 | + async completePrompt(prompt: string): Promise<string> { |
| 85 | + // Copy to clipboard |
| 86 | + await vscode.env.clipboard.writeText(prompt) |
| 87 | + |
| 88 | + // A dialog box pops up to request user action |
| 89 | + const response = await showHumanRelayDialog(prompt) |
| 90 | + |
| 91 | + if (!response) { |
| 92 | + throw new Error("Human relay operation cancelled") |
| 93 | + } |
| 94 | + |
| 95 | + return response |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +/** |
| 100 | + * Extract text content from message object |
| 101 | + * @param message |
| 102 | + */ |
| 103 | +function getMessageContent(message: Anthropic.Messages.MessageParam): string { |
| 104 | + if (typeof message.content === "string") { |
| 105 | + return message.content |
| 106 | + } else if (Array.isArray(message.content)) { |
| 107 | + return message.content |
| 108 | + .filter((item) => item.type === "text") |
| 109 | + .map((item) => (item.type === "text" ? item.text : "")) |
| 110 | + .join("\n") |
| 111 | + } |
| 112 | + return "" |
| 113 | +} |
| 114 | +/** |
| 115 | + * Displays the human relay dialog and waits for user response. |
| 116 | + * @param promptText The prompt text that needs to be copied. |
| 117 | + * @returns The user's input response or undefined (if canceled). |
| 118 | + */ |
| 119 | +async function showHumanRelayDialog(promptText: string): Promise<string | undefined> { |
| 120 | + return new Promise<string | undefined>((resolve) => { |
| 121 | + // Create a unique request ID |
| 122 | + const requestId = Date.now().toString() |
| 123 | + |
| 124 | + // Register a global callback function |
| 125 | + vscode.commands.executeCommand( |
| 126 | + "roo-cline.registerHumanRelayCallback", |
| 127 | + requestId, |
| 128 | + (response: string | undefined) => { |
| 129 | + resolve(response) |
| 130 | + }, |
| 131 | + ) |
| 132 | + |
| 133 | + // Open the dialog box directly using the current panel |
| 134 | + vscode.commands.executeCommand("roo-cline.showHumanRelayDialog", { |
| 135 | + requestId, |
| 136 | + promptText, |
| 137 | + }) |
| 138 | + }) |
| 139 | +} |
0 commit comments