@@ -6,7 +6,7 @@ import { writeFile } from "fs/promises";
66import { join } from "path" ;
77import { assert , describe , expect , it } from "vitest" ;
88import { z } from "zod" ;
9- import { defineTool , approveAll } from "../../src/index.js" ;
9+ import { defineTool , approveAll , ToolSet } from "../../src/index.js" ;
1010import type { PermissionRequest } from "../../src/index.js" ;
1111import { createSdkTestContext } from "./harness/sdkTestContext" ;
1212
@@ -45,6 +45,52 @@ describe("Custom tools", async () => {
4545 expect ( assistantMessage ?. data . content ) . toContain ( "HELLO" ) ;
4646 } ) ;
4747
48+ it ( "low_level_tool_definition" , async ( ) => {
49+ let currentPhase = "" ;
50+ const session = await client . createSession ( {
51+ onPermissionRequest : approveAll ,
52+ availableTools : new ToolSet ( ) . addCustom ( "*" ) . addBuiltIn ( "web_fetch" ) ,
53+ tools : [
54+ defineTool ( "set_current_phase" , {
55+ description : "Sets the current phase of the agent" ,
56+ parameters : z . object ( {
57+ phase : z . enum ( [ "searching" , "analyzing" , "done" ] ) ,
58+ } ) ,
59+ handler : ( { phase } ) => {
60+ currentPhase = phase ;
61+ return `Phase set to ${ phase } ` ;
62+ } ,
63+ } ) ,
64+ defineTool ( "search_items" , {
65+ description : "Search for items by keyword" ,
66+ parameters : z . object ( {
67+ keyword : z . string ( ) ,
68+ } ) ,
69+ handler : ( _args , invocation ) => {
70+ const args = invocation . arguments as Record < string , unknown > ;
71+ if ( args . keyword !== "copilot" ) {
72+ throw new Error ( `Expected keyword to be 'copilot', got: ${ String ( args . keyword ) } ` ) ;
73+ }
74+ return "Found: item_alpha, item_beta" ;
75+ } ,
76+ } ) ,
77+ ] ,
78+ } ) ;
79+
80+ const assistantMessage = await session . sendAndWait ( {
81+ prompt : "First, set the current phase to 'analyzing'. Then search for items with keyword 'copilot'. Report the phase and search results." ,
82+ } ) ;
83+
84+ const content = assistantMessage ?. data . content ?? "" ;
85+ expect ( content . length ) . toBeGreaterThan ( 0 ) ;
86+ expect ( content . toLowerCase ( ) ) . toContain ( "analyzing" ) ;
87+ expect (
88+ content . toLowerCase ( ) . includes ( "item_alpha" ) ||
89+ content . toLowerCase ( ) . includes ( "item_beta" )
90+ ) . toBe ( true ) ;
91+ expect ( currentPhase ) . toBe ( "analyzing" ) ;
92+ } ) ;
93+
4894 it ( "handles tool calling errors" , async ( ) => {
4995 const session = await client . createSession ( {
5096 onPermissionRequest : approveAll ,
0 commit comments