@@ -2,88 +2,98 @@ import * as t from "tap";
22import { startTestAgent } from "../helpers/startTestAgent" ;
33import { AiSDK } from "./AiSDK" ;
44import { getContext , runWithContext , type Context } from "../agent/Context" ;
5+ import { getMajorNodeVersion } from "../helpers/getNodeVersion" ;
56
6- t . test ( "It works with agentic" , async ( t ) => {
7- startTestAgent ( {
8- wrappers : [ new AiSDK ( ) ] ,
9- rewrite : { } ,
10- } ) ;
11-
12- const getTestContext = ( message : string ) : Context => {
13- return {
14- remoteAddress : "::1" ,
15- method : "POST" ,
16- url : "http://localhost:4000" ,
17- query : {
18- message,
19- } ,
20- body : undefined ,
21- headers : { } ,
22- cookies : { } ,
23- routeParams : { } ,
24- source : "express" ,
25- route : "/posts/:id" ,
26- } ;
27- } ;
28-
29- const { google } =
30- require ( "@ai-sdk/google" ) as typeof import ( "@ai-sdk/google" ) ;
31- const { generateText, tool } = require ( "ai" ) as typeof import ( "ai" ) ;
32- const { z } = require ( "zod" ) as typeof import ( "zod" ) ;
33-
34- const callWithPrompt = async ( prompt : string ) => {
35- return await generateText ( {
36- model : google ( "models/gemini-2.0-flash" ) ,
37- tools : {
38- weather : tool ( {
39- description : "Get the weather in a location" ,
40- parameters : z . object ( {
41- location : z
42- . string ( )
43- . describe ( "The location to get the weather for" ) ,
44- } ) ,
45- execute : async ( { location } ) => {
46- const temperature = location === "Norway" ? 5 : 24 ;
47- return {
48- temperature,
49- context : getContext ( ) ,
50- } ;
51- } ,
52- } ) ,
53- } ,
54- prompt : prompt ,
7+ t . test (
8+ "It works with AI tool execution" ,
9+ {
10+ skip :
11+ ! process . env . GOOGLE_GENERATIVE_AI_API_KEY || getMajorNodeVersion ( ) < 20
12+ ? "Google API key not set or Node version < 20"
13+ : undefined ,
14+ } ,
15+ async ( t ) => {
16+ startTestAgent ( {
17+ wrappers : [ new AiSDK ( ) ] ,
18+ rewrite : { } ,
5519 } ) ;
56- } ;
57-
58- await runWithContext (
59- getTestContext ( "What is the weather in San Francisco?" ) ,
60- async ( ) => {
61- const result = await callWithPrompt (
62- "What is the weather in San Francisco?"
63- ) ;
6420
65- t . same ( result . toolResults . length , 1 ) ;
66- t . same ( result . toolResults [ 0 ] . toolName , "weather" ) ;
67- t . same ( result . toolResults [ 0 ] . result . temperature , 24 ) ;
68- t . match ( result . toolResults [ 0 ] . result . context , {
21+ const getTestContext = ( message : string ) : Context => {
22+ return {
6923 remoteAddress : "::1" ,
7024 method : "POST" ,
7125 url : "http://localhost:4000" ,
7226 query : {
73- message : "What is the weather in San Francisco?" ,
27+ message,
7428 } ,
7529 body : undefined ,
7630 headers : { } ,
7731 cookies : { } ,
7832 routeParams : { } ,
7933 source : "express" ,
8034 route : "/posts/:id" ,
81- aiToolParams : [
82- {
83- location : "San Francisco" ,
84- } ,
85- ] ,
35+ } ;
36+ } ;
37+
38+ const { google } =
39+ require ( "@ai-sdk/google" ) as typeof import ( "@ai-sdk/google" ) ;
40+ const { generateText, tool } = require ( "ai" ) as typeof import ( "ai" ) ;
41+ const { z } = require ( "zod" ) as typeof import ( "zod" ) ;
42+
43+ const callWithPrompt = async ( prompt : string ) => {
44+ return await generateText ( {
45+ model : google ( "models/gemini-2.0-flash-lite" ) ,
46+ tools : {
47+ weather : tool ( {
48+ description : "Get the weather in a location" ,
49+ parameters : z . object ( {
50+ location : z
51+ . string ( )
52+ . describe ( "The location to get the weather for" ) ,
53+ } ) ,
54+ execute : async ( { location } ) => {
55+ const temperature = location === "Norway" ? 5 : 24 ;
56+ return {
57+ temperature,
58+ context : getContext ( ) ,
59+ } ;
60+ } ,
61+ } ) ,
62+ } ,
63+ prompt : prompt ,
8664 } ) ;
87- }
88- ) ;
89- } ) ;
65+ } ;
66+
67+ await runWithContext (
68+ getTestContext ( "What is the weather in San Francisco?" ) ,
69+ async ( ) => {
70+ const result = await callWithPrompt (
71+ "What is the weather in San Francisco?"
72+ ) ;
73+
74+ t . same ( result . toolResults . length , 1 ) ;
75+ t . same ( result . toolResults [ 0 ] . toolName , "weather" ) ;
76+ t . same ( result . toolResults [ 0 ] . result . temperature , 24 ) ;
77+ t . match ( result . toolResults [ 0 ] . result . context , {
78+ remoteAddress : "::1" ,
79+ method : "POST" ,
80+ url : "http://localhost:4000" ,
81+ query : {
82+ message : "What is the weather in San Francisco?" ,
83+ } ,
84+ body : undefined ,
85+ headers : { } ,
86+ cookies : { } ,
87+ routeParams : { } ,
88+ source : "express" ,
89+ route : "/posts/:id" ,
90+ aiToolParams : [
91+ {
92+ location : "San Francisco" ,
93+ } ,
94+ ] ,
95+ } ) ;
96+ }
97+ ) ;
98+ }
99+ ) ;
0 commit comments