|
| 1 | +import { z } from 'zod/v4' |
| 2 | +import { buildTool, type ToolDef } from 'src/Tool.js' |
| 3 | +import { lazySchema } from 'src/utils/lazySchema.js' |
| 4 | +import { |
| 5 | + completeGoal, |
| 6 | + formatGoalStatus, |
| 7 | + getActiveElapsedMs, |
| 8 | + getGoal, |
| 9 | + setGoal, |
| 10 | +} from 'src/services/goal/goalState.js' |
| 11 | +import { DESCRIPTION, generatePrompt } from './prompt.js' |
| 12 | +import type { ToolResultBlockParam } from '@anthropic-ai/sdk/resources/index.mjs' |
| 13 | + |
| 14 | +const inputSchema = lazySchema(() => |
| 15 | + z.strictObject({ |
| 16 | + action: z |
| 17 | + .enum(['get', 'set', 'complete']) |
| 18 | + .describe('The action to perform on the goal.'), |
| 19 | + objective: z |
| 20 | + .string() |
| 21 | + .optional() |
| 22 | + .describe('The goal objective. Required for "set" action.'), |
| 23 | + message: z |
| 24 | + .string() |
| 25 | + .optional() |
| 26 | + .describe('Completion message for "complete" action.'), |
| 27 | + }), |
| 28 | +) |
| 29 | +type InputSchema = ReturnType<typeof inputSchema> |
| 30 | + |
| 31 | +const outputSchema = lazySchema(() => |
| 32 | + z.object({ |
| 33 | + success: z.boolean(), |
| 34 | + action: z.string(), |
| 35 | + goal: z |
| 36 | + .object({ |
| 37 | + objective: z.string(), |
| 38 | + status: z.string(), |
| 39 | + tokensUsed: z.number(), |
| 40 | + tokenBudget: z.number().nullable(), |
| 41 | + elapsedSeconds: z.number(), |
| 42 | + }) |
| 43 | + .optional(), |
| 44 | + message: z.string().optional(), |
| 45 | + error: z.string().optional(), |
| 46 | + }), |
| 47 | +) |
| 48 | +type OutputSchema = ReturnType<typeof outputSchema> |
| 49 | + |
| 50 | +export type Input = z.infer<InputSchema> |
| 51 | +export type Output = z.infer<OutputSchema> |
| 52 | + |
| 53 | +export const GoalTool = buildTool({ |
| 54 | + name: 'goal', |
| 55 | + searchHint: 'manage long-running task goals', |
| 56 | + maxResultSizeChars: 10_000, |
| 57 | + async description() { |
| 58 | + return DESCRIPTION |
| 59 | + }, |
| 60 | + async prompt() { |
| 61 | + return generatePrompt() |
| 62 | + }, |
| 63 | + get inputSchema(): InputSchema { |
| 64 | + return inputSchema() |
| 65 | + }, |
| 66 | + get outputSchema(): OutputSchema { |
| 67 | + return outputSchema() |
| 68 | + }, |
| 69 | + userFacingName() { |
| 70 | + return 'Goal' |
| 71 | + }, |
| 72 | + shouldDefer: true, |
| 73 | + isConcurrencySafe() { |
| 74 | + return true |
| 75 | + }, |
| 76 | + isReadOnly(input: Input) { |
| 77 | + return input.action === 'get' |
| 78 | + }, |
| 79 | + toAutoClassifierInput(input) { |
| 80 | + if (input.action === 'get') return 'get goal status' |
| 81 | + if (input.action === 'set') return `set goal: ${input.objective}` |
| 82 | + return `complete goal: ${input.message ?? ''}` |
| 83 | + }, |
| 84 | + async checkPermissions(input: Input) { |
| 85 | + if (input.action === 'get') { |
| 86 | + return { behavior: 'allow' as const, updatedInput: input } |
| 87 | + } |
| 88 | + return { |
| 89 | + behavior: 'ask' as const, |
| 90 | + message: |
| 91 | + input.action === 'set' |
| 92 | + ? `Set goal: ${input.objective}` |
| 93 | + : `Complete goal${input.message ? `: ${input.message}` : ''}`, |
| 94 | + } |
| 95 | + }, |
| 96 | + async call({ action, objective, message }: Input): Promise<{ data: Output }> { |
| 97 | + if (action === 'get') { |
| 98 | + const goal = getGoal() |
| 99 | + if (!goal) { |
| 100 | + return { data: { success: true, action, message: 'No active goal.' } } |
| 101 | + } |
| 102 | + const elapsedSeconds = Math.floor(getActiveElapsedMs(goal) / 1000) |
| 103 | + return { |
| 104 | + data: { |
| 105 | + success: true, |
| 106 | + action, |
| 107 | + goal: { |
| 108 | + objective: goal.objective, |
| 109 | + status: goal.status, |
| 110 | + tokensUsed: goal.tokensUsed, |
| 111 | + tokenBudget: goal.tokenBudget, |
| 112 | + elapsedSeconds, |
| 113 | + }, |
| 114 | + }, |
| 115 | + } |
| 116 | + } |
| 117 | + |
| 118 | + if (action === 'set') { |
| 119 | + if (!objective) { |
| 120 | + return { |
| 121 | + data: { |
| 122 | + success: false, |
| 123 | + action, |
| 124 | + error: 'objective is required for set action.', |
| 125 | + }, |
| 126 | + } |
| 127 | + } |
| 128 | + setGoal(objective) |
| 129 | + return { |
| 130 | + data: { |
| 131 | + success: true, |
| 132 | + action, |
| 133 | + message: `Goal set: ${objective}`, |
| 134 | + goal: { |
| 135 | + objective, |
| 136 | + status: 'active', |
| 137 | + tokensUsed: 0, |
| 138 | + tokenBudget: null, |
| 139 | + elapsedSeconds: 0, |
| 140 | + }, |
| 141 | + }, |
| 142 | + } |
| 143 | + } |
| 144 | + |
| 145 | + if (action === 'complete') { |
| 146 | + if (!completeGoal()) { |
| 147 | + return { |
| 148 | + data: { |
| 149 | + success: false, |
| 150 | + action, |
| 151 | + error: 'No active goal to complete.', |
| 152 | + }, |
| 153 | + } |
| 154 | + } |
| 155 | + return { |
| 156 | + data: { |
| 157 | + success: true, |
| 158 | + action, |
| 159 | + message: message |
| 160 | + ? `Goal completed: ${message}` |
| 161 | + : 'Goal marked as complete.', |
| 162 | + }, |
| 163 | + } |
| 164 | + } |
| 165 | + |
| 166 | + return { |
| 167 | + data: { success: false, action, error: `Unknown action: ${action}` }, |
| 168 | + } |
| 169 | + }, |
| 170 | + renderToolUseMessage(input: Partial<Input>) { |
| 171 | + if (input.action === 'get') return 'Getting goal status' |
| 172 | + if (input.action === 'set') return `Setting goal: ${input.objective ?? ''}` |
| 173 | + if (input.action === 'complete') return 'Completing goal' |
| 174 | + return 'Managing goal' |
| 175 | + }, |
| 176 | + renderToolResultMessage(content: Output) { |
| 177 | + if (!content.success) return `Error: ${content.error}` |
| 178 | + if (content.action === 'get' && content.goal) { |
| 179 | + const g = content.goal |
| 180 | + return `Goal: ${g.objective} [${g.status}]` |
| 181 | + } |
| 182 | + return content.message ?? 'Done.' |
| 183 | + }, |
| 184 | + mapToolResultToToolResultBlockParam( |
| 185 | + content: Output, |
| 186 | + toolUseID: string, |
| 187 | + ): ToolResultBlockParam { |
| 188 | + if (!content.success) { |
| 189 | + return { |
| 190 | + tool_use_id: toolUseID, |
| 191 | + type: 'tool_result' as const, |
| 192 | + content: `Error: ${content.error}`, |
| 193 | + is_error: true, |
| 194 | + } |
| 195 | + } |
| 196 | + |
| 197 | + if (content.action === 'get' && content.goal) { |
| 198 | + const g = content.goal |
| 199 | + return { |
| 200 | + tool_use_id: toolUseID, |
| 201 | + type: 'tool_result' as const, |
| 202 | + content: `Goal: ${g.objective}\nStatus: ${g.status}\nTokens: ${g.tokensUsed}${g.tokenBudget !== null ? ` / ${g.tokenBudget}` : ''}\nElapsed: ${g.elapsedSeconds}s`, |
| 203 | + } |
| 204 | + } |
| 205 | + |
| 206 | + return { |
| 207 | + tool_use_id: toolUseID, |
| 208 | + type: 'tool_result' as const, |
| 209 | + content: content.message ?? 'Done.', |
| 210 | + } |
| 211 | + }, |
| 212 | +} satisfies ToolDef<InputSchema, Output>) |
0 commit comments