11import { ToolLoopAgent , stepCountIs , type TypedToolResult } from "ai" ;
22import { z } from "zod" ;
33import {
4- todoWriteTool ,
5- readFileTool ,
6- writeFileTool ,
7- editFileTool ,
8- grepTool ,
9- globTool ,
10- bashTool ,
11- taskTool ,
4+ todoWriteTool ,
5+ readFileTool ,
6+ writeFileTool ,
7+ editFileTool ,
8+ grepTool ,
9+ globTool ,
10+ bashTool ,
11+ taskTool ,
1212} from "./tools" ;
1313import { buildSystemPrompt } from "./system-prompt" ;
1414import type { TodoItem , AgentMode , ApprovalRule } from "./types" ;
@@ -21,88 +21,88 @@ const agentModeSchema = z.enum(["interactive", "background"]);
2121const autoApproveSchema = z . enum ( [ "off" , "edits" , "all" ] ) ;
2222
2323const callOptionsSchema = z . object ( {
24- workingDirectory : z . string ( ) ,
25- mode : agentModeSchema . optional ( ) ,
26- customInstructions : z . string ( ) . optional ( ) ,
27- sandbox : z . custom < Sandbox > ( ) . optional ( ) ,
28- autoApprove : autoApproveSchema . optional ( ) ,
29- approvalRules : z . array ( approvalRuleSchema ) . optional ( ) ,
24+ workingDirectory : z . string ( ) ,
25+ mode : agentModeSchema . optional ( ) ,
26+ customInstructions : z . string ( ) . optional ( ) ,
27+ sandbox : z . custom < Sandbox > ( ) . optional ( ) ,
28+ autoApprove : autoApproveSchema . optional ( ) ,
29+ approvalRules : z . array ( approvalRuleSchema ) . optional ( ) ,
3030} ) ;
3131
3232export type DeepAgentCallOptions = z . infer < typeof callOptionsSchema > ;
3333
3434const model = gateway ( "anthropic/claude-haiku-4.5" , {
35- devtools : true ,
35+ devtools : true ,
3636} ) ;
3737
3838export const deepAgentModelId = model . modelId ;
3939
4040export const deepAgent = new ToolLoopAgent ( {
41- model,
42- instructions : buildSystemPrompt ( { } ) ,
43- tools : addCacheControl ( {
44- tools : {
45- todo_write : todoWriteTool ,
46- read : readFileTool ( ) ,
47- write : writeFileTool ( { needsApproval : true } ) ,
48- edit : editFileTool ( { needsApproval : true } ) ,
49- grep : grepTool ( ) ,
50- glob : globTool ( ) ,
51- bash : bashTool ( { needsApproval : true } ) ,
52- task : taskTool ,
53- } ,
54- model,
55- } ) ,
56- stopWhen : stepCountIs ( 50 ) ,
57- callOptionsSchema,
58- prepareStep : ( { messages, model, steps } ) => ( {
59- messages : addCacheControl ( {
60- messages : compactContext ( { messages, steps } ) ,
61- model,
62- } ) ,
63- } ) ,
64- prepareCall : ( { options, model, ...settings } ) => {
65- const workingDirectory = options ?. workingDirectory ?? process . cwd ( ) ;
66- const mode : AgentMode = options ?. mode ?? "interactive" ;
67- const autoApprove = options ?. autoApprove ?? "off" ;
68- const approvalRules : ApprovalRule [ ] = options ?. approvalRules ?? [ ] ;
41+ model,
42+ instructions : buildSystemPrompt ( { } ) ,
43+ tools : addCacheControl ( {
44+ tools : {
45+ todo_write : todoWriteTool ,
46+ read : readFileTool ( ) ,
47+ write : writeFileTool ( { needsApproval : true } ) ,
48+ edit : editFileTool ( { needsApproval : true } ) ,
49+ grep : grepTool ( ) ,
50+ glob : globTool ( ) ,
51+ bash : bashTool ( { needsApproval : true } ) ,
52+ task : taskTool ,
53+ } ,
54+ model,
55+ } ) ,
56+ stopWhen : stepCountIs ( 50 ) ,
57+ callOptionsSchema,
58+ prepareStep : ( { messages, model, steps } ) => ( {
59+ messages : addCacheControl ( {
60+ messages : compactContext ( { messages, steps } ) ,
61+ model,
62+ } ) ,
63+ } ) ,
64+ prepareCall : ( { options, model, ...settings } ) => {
65+ const workingDirectory = options ?. workingDirectory ?? process . cwd ( ) ;
66+ const mode : AgentMode = options ?. mode ?? "interactive" ;
67+ const autoApprove = options ?. autoApprove ?? "off" ;
68+ const approvalRules : ApprovalRule [ ] = options ?. approvalRules ?? [ ] ;
6969
70- const customInstructions = options ?. customInstructions ;
70+ const customInstructions = options ?. customInstructions ;
7171
72- // Use provided sandbox, or create a local sandbox with the working directory
73- const sandbox = options ?. sandbox ?? createLocalSandbox ( workingDirectory ) ;
72+ // Use provided sandbox, or create a local sandbox with the working directory
73+ const sandbox = options ?. sandbox ?? createLocalSandbox ( workingDirectory ) ;
7474
75- return {
76- ...settings ,
77- model,
78- instructions : buildSystemPrompt ( {
79- cwd : sandbox . workingDirectory ,
80- mode,
81- currentBranch : sandbox . currentBranch ,
82- customInstructions,
83- } ) ,
84- experimental_context : { sandbox, mode, autoApprove, approvalRules } ,
85- } ;
86- } ,
87- onFinish : async ( { experimental_context } ) => {
88- try {
89- const sandbox = getSandbox ( experimental_context ) ;
90- await sandbox . stop ( ) ;
91- } catch {
92- // Sandbox not available, nothing to clean up
93- }
94- } ,
75+ return {
76+ ...settings ,
77+ model,
78+ instructions : buildSystemPrompt ( {
79+ cwd : sandbox . workingDirectory ,
80+ mode,
81+ currentBranch : sandbox . currentBranch ,
82+ customInstructions,
83+ } ) ,
84+ experimental_context : { sandbox, mode, autoApprove, approvalRules } ,
85+ } ;
86+ } ,
87+ onFinish : async ( { experimental_context } ) => {
88+ try {
89+ const sandbox = getSandbox ( experimental_context ) ;
90+ await sandbox . stop ( ) ;
91+ } catch {
92+ // Sandbox not available, nothing to clean up
93+ }
94+ } ,
9595} ) ;
9696
9797export function extractTodosFromStep (
98- toolResults : Array < TypedToolResult < typeof deepAgent . tools > > ,
98+ toolResults : Array < TypedToolResult < typeof deepAgent . tools > > ,
9999) : TodoItem [ ] | null {
100- for ( const result of toolResults ) {
101- if ( ! result . dynamic && result . toolName === "todo_write" && result . output ) {
102- return result . output . todos ;
103- }
104- }
105- return null ;
100+ for ( const result of toolResults ) {
101+ if ( ! result . dynamic && result . toolName === "todo_write" && result . output ) {
102+ return result . output . todos ;
103+ }
104+ }
105+ return null ;
106106}
107107
108108export type DeepAgent = typeof deepAgent ;
0 commit comments