|
| 1 | +import { ContinueError, ContinueErrorReason } from "core/util/errors.js"; |
| 2 | + |
| 3 | +import { |
| 4 | + ApiRequestError, |
| 5 | + AuthenticationRequiredError, |
| 6 | + post, |
| 7 | +} from "../util/apiClient.js"; |
| 8 | +import { logger } from "../util/logger.js"; |
| 9 | + |
| 10 | +import { Tool } from "./types.js"; |
| 11 | + |
| 12 | +/** |
| 13 | + * Extract the agent ID from the --id command line flag |
| 14 | + */ |
| 15 | +function getAgentIdFromArgs(): string | undefined { |
| 16 | + const args = process.argv; |
| 17 | + const idIndex = args.indexOf("--id"); |
| 18 | + if (idIndex !== -1 && idIndex + 1 < args.length) { |
| 19 | + return args[idIndex + 1]; |
| 20 | + } |
| 21 | + return undefined; |
| 22 | +} |
| 23 | + |
| 24 | +export const reportFailureTool: Tool = { |
| 25 | + name: "ReportFailure", |
| 26 | + displayName: "Report Failure", |
| 27 | + description: |
| 28 | + "Report that the task has failed due to an unrecoverable error. Include a succinct explanation for the user.", |
| 29 | + parameters: { |
| 30 | + type: "object", |
| 31 | + required: ["errorMessage"], |
| 32 | + properties: { |
| 33 | + errorMessage: { |
| 34 | + type: "string", |
| 35 | + description: "Explain what went wrong and why you cannot continue.", |
| 36 | + }, |
| 37 | + }, |
| 38 | + }, |
| 39 | + readonly: true, |
| 40 | + isBuiltIn: true, |
| 41 | + run: async (args: { errorMessage: string }): Promise<string> => { |
| 42 | + try { |
| 43 | + const trimmedMessage = args.errorMessage.trim(); |
| 44 | + if (!trimmedMessage) { |
| 45 | + throw new ContinueError( |
| 46 | + ContinueErrorReason.Unspecified, |
| 47 | + "errorMessage is required to report a failure.", |
| 48 | + ); |
| 49 | + } |
| 50 | + |
| 51 | + const agentId = getAgentIdFromArgs(); |
| 52 | + if (!agentId) { |
| 53 | + const errorMessage = |
| 54 | + "Agent ID is required. Please use the --id flag with cn serve."; |
| 55 | + logger.error(errorMessage); |
| 56 | + throw new ContinueError(ContinueErrorReason.Unspecified, errorMessage); |
| 57 | + } |
| 58 | + |
| 59 | + await post(`agents/${agentId}/status`, { |
| 60 | + status: "FAILED", |
| 61 | + errorMessage: trimmedMessage, |
| 62 | + }); |
| 63 | + |
| 64 | + logger.info(`Failure reported: ${trimmedMessage}`); |
| 65 | + return "Failure reported to user."; |
| 66 | + } catch (error) { |
| 67 | + if (error instanceof ContinueError) { |
| 68 | + throw error; |
| 69 | + } |
| 70 | + |
| 71 | + if (error instanceof AuthenticationRequiredError) { |
| 72 | + logger.error(error.message); |
| 73 | + throw new Error("Error: Authentication required"); |
| 74 | + } |
| 75 | + |
| 76 | + if (error instanceof ApiRequestError) { |
| 77 | + throw new Error( |
| 78 | + `Error reporting failure: ${error.status} ${error.response || error.statusText}`, |
| 79 | + ); |
| 80 | + } |
| 81 | + |
| 82 | + const errorMessage = |
| 83 | + error instanceof Error ? error.message : String(error); |
| 84 | + logger.error(`Error reporting failure: ${errorMessage}`); |
| 85 | + throw new Error(`Error reporting failure: ${errorMessage}`); |
| 86 | + } |
| 87 | + }, |
| 88 | +}; |
0 commit comments