Skip to content

Commit 46b3020

Browse files
committed
Fixed metadata system
1 parent 643d0b8 commit 46b3020

File tree

6 files changed

+80
-15
lines changed

6 files changed

+80
-15
lines changed

packages/cli-v3/src/entryPoints/dev-run-worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -378,6 +378,8 @@ const zodIpc = new ZodIpcConnection({
378378
return;
379379
}
380380

381+
runMetadataManager.runId = execution.run.id;
382+
381383
const executor = new TaskExecutor(task, {
382384
tracer,
383385
tracingSDK,

packages/cli-v3/src/entryPoints/managed-run-worker.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -376,6 +376,8 @@ const zodIpc = new ZodIpcConnection({
376376
return;
377377
}
378378

379+
runMetadataManager.runId = execution.run.id;
380+
379381
const executor = new TaskExecutor(task, {
380382
tracer,
381383
tracingSDK,

packages/react-hooks/src/hooks/useWaitToken.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ export interface WaitTokenInstance<TOutput> {
1717
isCompleted: boolean;
1818
/** Any error that occurred during completion */
1919
error?: Error;
20+
/** Whether the waitpoint is ready to be completed */
21+
isReady: boolean;
2022
}
2123

2224
/**
@@ -36,7 +38,7 @@ export interface WaitTokenInstance<TOutput> {
3638
* ```
3739
*/
3840
export function useWaitToken<TOutput>(
39-
waitpointId: string,
41+
waitpointId?: string,
4042
options?: UseApiClientOptions
4143
): WaitTokenInstance<TOutput> {
4244
const apiClient = useApiClient(options);
@@ -46,6 +48,10 @@ export function useWaitToken<TOutput>(
4648
throw new Error("Could not complete waitpoint in useWaitToken: Missing access token");
4749
}
4850

51+
if (!waitpointId) {
52+
throw new Error("Could not complete waitpoint in useWaitToken: Missing waitpoint ID");
53+
}
54+
4955
const result = await apiClient.completeWaitpointToken(waitpointId, {
5056
data: output,
5157
});
@@ -62,6 +68,7 @@ export function useWaitToken<TOutput>(
6268
},
6369
isLoading: mutation.isMutating,
6470
isCompleted: !!mutation.data?.success,
71+
isReady: !!waitpointId,
6572
error: mutation.error,
6673
};
6774
}

references/agent-loop/src/components/main-app.tsx

Lines changed: 57 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -6,36 +6,79 @@ import { useState } from "react";
66
import { Send } from "lucide-react";
77
import ChatInterface from "@/components/chat-interface";
88
import InitialPrompt from "@/components/initial-prompt";
9-
import { useRealtimeTaskTriggerWithStreams } from "@trigger.dev/react-hooks";
9+
import { useRealtimeTaskTriggerWithStreams, useWaitToken } from "@trigger.dev/react-hooks";
1010
import type { agentLoopExample } from "@/trigger/agent";
11+
import { AgentLoopMetadata } from "@/trigger/schemas";
12+
import type { TextStreamPart } from "ai";
1113

1214
type ChatConversation = Array<{ role: "user" | "assistant"; content: string }>;
1315

16+
type ResponseStreams = {
17+
[K in `responses.${number | string}`]: TextStreamPart<{}>;
18+
};
19+
1420
export 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,

references/agent-loop/src/trigger/agent.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ export const agentLoopExample = schemaTask({
3939
const token = await wait.createToken({ timeout: "1d" });
4040

4141
metadata.set("waitToken", token);
42+
await metadata.flush();
4243

4344
const result = streamText({
4445
model: aiModel,
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
import { z } from "zod";
2+
3+
export const AgentLoopMetadata = z.object({
4+
waitToken: z.object({
5+
id: z.string(),
6+
publicAccessToken: z.string(),
7+
}),
8+
});
9+
10+
export type AgentLoopMetadata = z.infer<typeof AgentLoopMetadata>;

0 commit comments

Comments
 (0)