@@ -6,36 +6,79 @@ import { useState } from "react";
66import { Send } from "lucide-react" ;
77import ChatInterface from "@/components/chat-interface" ;
88import InitialPrompt from "@/components/initial-prompt" ;
9- import { useRealtimeTaskTriggerWithStreams } from "@trigger.dev/react-hooks" ;
9+ import { useRealtimeTaskTriggerWithStreams , useWaitToken } from "@trigger.dev/react-hooks" ;
1010import type { agentLoopExample } from "@/trigger/agent" ;
11+ import { AgentLoopMetadata } from "@/trigger/schemas" ;
12+ import type { TextStreamPart } from "ai" ;
1113
1214type ChatConversation = Array < { role : "user" | "assistant" ; content : string } > ;
1315
16+ type ResponseStreams = {
17+ [ K in `responses.${number | string } `] : TextStreamPart < { } > ;
18+ } ;
19+
1420export function useAgentLoop ( { publicAccessToken } : { publicAccessToken : string } ) {
15- const triggerInstance = useRealtimeTaskTriggerWithStreams < typeof agentLoopExample > (
16- "agent-loop-example" ,
17- {
18- accessToken : publicAccessToken ,
19- baseURL : process . env . NEXT_PUBLIC_TRIGGER_API_URL ,
20- }
21- ) ;
21+ const triggerInstance = useRealtimeTaskTriggerWithStreams <
22+ typeof agentLoopExample ,
23+ ResponseStreams
24+ > ( "agent-loop-example" , {
25+ accessToken : publicAccessToken ,
26+ baseURL : process . env . NEXT_PUBLIC_TRIGGER_API_URL ,
27+ } ) ;
2228 const [ conversation , setConversation ] = useState < ChatConversation > ( [ ] ) ;
2329 const [ isLoading , setIsLoading ] = useState ( false ) ;
30+ const [ waitToken , setWaitToken ] = useState < AgentLoopMetadata | null > ( null ) ;
31+
32+ const waitTokenInstance = useWaitToken ( waitToken ?. waitToken . id , {
33+ enabled : ! ! waitToken ?. waitToken . id ,
34+ accessToken : waitToken ?. waitToken . publicAccessToken ,
35+ baseURL : process . env . NEXT_PUBLIC_TRIGGER_API_URL ,
36+ } ) ;
37+
38+ const waitTokenStream = waitToken ?. waitToken . id
39+ ? triggerInstance . streams [ `responses.${ waitToken ?. waitToken . id } ` ]
40+ : undefined ;
41+
42+ const textStream = waitTokenStream
43+ ?. map ( ( part ) => {
44+ if ( part . type === "text-delta" ) {
45+ return part . textDelta ;
46+ }
47+ } )
48+ . join ( "" ) ;
49+
50+ console . log ( "textStream" , textStream ) ;
2451
2552 if ( triggerInstance . run ) {
2653 console . log ( "run" , triggerInstance . run ) ;
54+
55+ const metadata = AgentLoopMetadata . safeParse ( triggerInstance . run . metadata ) ;
56+
57+ if ( ! metadata . success ) {
58+ console . error ( "Failed to parse metadata" , metadata . error ) ;
59+ } else {
60+ console . log ( "metadata" , metadata . data ) ;
61+
62+ setWaitToken ( metadata . data ) ;
63+ }
2764 }
2865
2966 return {
3067 continueConversation : ( prompt : string ) => {
31- const result = triggerInstance . submit ( {
32- model : "gpt-4o-mini" ,
33- prompt,
34- } ) ;
68+ if ( waitTokenInstance . isReady ) {
69+ waitTokenInstance . complete ( {
70+ message : prompt ,
71+ } ) ;
72+ } else {
73+ const result = triggerInstance . submit ( {
74+ model : "gpt-4o-mini" ,
75+ prompt,
76+ } ) ;
3577
36- setConversation ( ( prev ) => [ ...prev , { role : "user" , content : prompt } ] ) ;
78+ setConversation ( ( prev ) => [ ...prev , { role : "user" , content : prompt } ] ) ;
3779
38- return result ;
80+ return result ;
81+ }
3982 } ,
4083 conversation,
4184 isLoading,
0 commit comments