11import { z } from "zod" ;
22import { helpers } from "./workflow" ;
33import { inferableInstance } from "../tests/utils" ;
4+ import assert from "assert" ;
45
56describe ( "workflow" , ( ) => {
67 jest . setTimeout ( 60_000 ) ;
@@ -12,18 +13,6 @@ describe("workflow", () => {
1213 const onSimpleResult = jest . fn ( ) ;
1314 const toolCall = jest . fn ( ) ;
1415
15- inferable . tools . register ( {
16- func : ( _i , _c ) => {
17- toolCall ( ) ;
18- return {
19- word : "needle" ,
20- } ;
21- } ,
22- name : "searchHaystack" ,
23- } ) ;
24-
25- inferable . tools . listen ( ) ;
26-
2716 // Generate a unique workflow name to prevent conflicts with other tests
2817 const workflowName = `haystack-search-${ Math . random ( ) . toString ( 36 ) . substring ( 2 , 15 ) } ` ;
2918
@@ -35,59 +24,88 @@ describe("workflow", () => {
3524 } ) ,
3625 } ) ;
3726
27+ workflow . tools . register ( {
28+ name : "searchHaystack2" ,
29+ inputSchema : z . object ( {
30+ searchQuery : z . string ( ) ,
31+ } ) ,
32+ func : async ( input ) => {
33+ toolCall ( input ) ;
34+ if ( input . searchQuery === "marco" ) {
35+ return { word : "not-needle" } ;
36+ } else if ( input . searchQuery === "marco 42" ) {
37+ return { word : "needle" } ;
38+ } else {
39+ return { word : `not-found-${ input . searchQuery } ` } ;
40+ }
41+ } ,
42+ } ) ;
43+
3844 workflow . version ( 1 ) . define ( async ( ctx , input ) => {
3945 onStart ( input ) ;
4046 ctx . log ( "info" , { message : "Starting workflow" } ) ;
41- const searchAgent = ctx . agent ( {
47+ const { word } = await ctx . agents . react ( {
4248 name : "search" ,
43- tools : [ "searchHaystack" ] ,
44- systemPrompt : helpers . structuredPrompt ( {
49+ instructions : helpers . structuredPrompt ( {
4550 facts : [ "You are haystack searcher" ] ,
46- goals : [ "Find the special word in the haystack" ] ,
51+ goals : [
52+ "Find the special word in the haystack. Only search for the words asked explictly by the user." ,
53+ ] ,
4754 } ) ,
48- resultSchema : z . object ( {
55+ schema : z . object ( {
4956 word : z . string ( ) ,
5057 } ) ,
58+ tools : [ "searchHaystack2" ] ,
59+ input : `Try the searchQuery 'marco'.` ,
60+ onBeforeReturn : async ( result , agent ) => {
61+ if ( result . word !== "needle" ) {
62+ await agent . sendMessage ( "Try the searchQuery 'marco 42'." ) ;
63+ }
64+ } ,
5165 } ) ;
5266
53- const result = await searchAgent . trigger ( {
54- data : { } ,
55- } ) ;
67+ assert ( word === "needle" , `Expected word to be "needle", got ${ word } ` ) ;
5668
57- ctx . result ( "testResultCall" , async ( ) => {
69+ const cachedResult = await ctx . result ( "testResultCall" , async ( ) => {
5870 return {
5971 word : "needle" ,
6072 } ;
6173 } ) ;
6274
63- if ( ! result || ! result . result || ! result . result . word ) {
64- throw new Error ( "No result" ) ;
65- }
75+ assert (
76+ cachedResult . word === "needle" ,
77+ `Expected cachedResult to be "needle", got ${ cachedResult . word } ` ,
78+ ) ;
6679
67- onAgentResult ( result . result . word ) ;
80+ onAgentResult ( cachedResult . word ) ;
6881
6982 ctx . log ( "info" , { message : "About to run simple LLM call" } ) ;
7083
7184 await ctx . llm . structured ( {
7285 input : "Return the word, needle." ,
7386 schema : z . object ( {
7487 word : z . string ( ) ,
75- } )
88+ } ) ,
7689 } ) ;
7790
7891 // Duplicate call
7992 const simpleResult = await ctx . llm . structured ( {
8093 input : "Return the word, needle." ,
8194 schema : z . object ( {
8295 word : z . string ( ) ,
83- } )
96+ } ) ,
8497 } ) ;
8598
86- if ( ! simpleResult || ! simpleResult . word ) {
87- throw new Error ( "No simpleResult" ) ;
88- }
99+ assert (
100+ simpleResult . word === "needle" ,
101+ `Expected simpleResult to be "needle", got ${ simpleResult . word } ` ,
102+ ) ;
103+
89104 onSimpleResult ( simpleResult . word ) ;
90105
106+ return {
107+ word : "needle" ,
108+ } ;
91109 } ) ;
92110
93111 await workflow . listen ( ) ;
@@ -114,7 +132,7 @@ describe("workflow", () => {
114132 expect ( onAgentResult ) . toHaveBeenCalledWith ( "needle" ) ;
115133 expect ( onAgentResult ) . toHaveBeenCalledTimes ( 1 ) ;
116134
117- expect ( toolCall ) . toHaveBeenCalledTimes ( 1 ) ;
135+ expect ( toolCall ) . toHaveBeenCalledTimes ( 2 ) ;
118136
119137 expect ( onSimpleResult ) . toHaveBeenCalledWith ( "needle" ) ;
120138 expect ( onSimpleResult ) . toHaveBeenCalledTimes ( 1 ) ;
0 commit comments