|
1 |
| -import { withSnapshot, request } from "@llama-flow/core/middleware/snapshot"; |
2 |
| -import { createWorkflow, workflowEvent, getContext } from "@llama-flow/core"; |
| 1 | +import { |
| 2 | + withSnapshot, |
| 3 | + SnapshotData, |
| 4 | +} from "@llama-flow/core/middleware/snapshot"; |
| 5 | +import { createWorkflow, getContext, workflowEvent } from "@llama-flow/core"; |
3 | 6 | import { OpenAI } from "openai";
|
4 | 7 |
|
5 |
| -export const serializableMemoryMap = new Map<string, any>(); |
6 |
| - |
7 | 8 | const openai = new OpenAI();
|
8 | 9 |
|
9 | 10 | const workflow = withSnapshot(createWorkflow());
|
10 | 11 |
|
11 | 12 | const startEvent = workflowEvent<string>({
|
12 | 13 | debugLabel: "start",
|
13 | 14 | });
|
14 |
| -const humanInteractionRequestEvent = workflowEvent<string>({ |
15 |
| - debugLabel: "humanInteractionRequest", |
16 |
| -}); |
| 15 | +const humanInteractionRequestEvent = workflowEvent<{ |
| 16 | + snapshot: SnapshotData; |
| 17 | + reason: string; |
| 18 | +}>(); |
17 | 19 | const humanInteractionResponseEvent = workflowEvent<string>({
|
18 | 20 | debugLabel: "humanInteractionResponse",
|
19 | 21 | });
|
20 |
| -type ResponseData = |
21 |
| - | { |
22 |
| - requestId: string; |
23 |
| - reason: string; |
24 |
| - data: string; |
25 |
| - } |
26 |
| - | { |
27 |
| - content: string; |
28 |
| - }; |
29 |
| -const stopEvent = workflowEvent<ResponseData>({ |
| 22 | +const stopEvent = workflowEvent<string>({ |
30 | 23 | debugLabel: "stop",
|
31 | 24 | });
|
32 | 25 |
|
@@ -72,21 +65,14 @@ For example, alex is from "Alexander the Great", who was a king of the ancient G
|
72 | 65 | if (tools && tools.length > 0) {
|
73 | 66 | const askName = tools.find((tool) => tool.function.name === "ask_name");
|
74 | 67 | if (askName) {
|
75 |
| - return humanInteractionRequestEvent.with(askName.function.arguments); |
| 68 | + const snapshot = await getContext().snapshot(); |
| 69 | + return humanInteractionRequestEvent.with({ |
| 70 | + reason: askName.function.arguments, |
| 71 | + snapshot, |
| 72 | + }); |
76 | 73 | }
|
77 | 74 | }
|
78 |
| - return stopEvent.with({ content: response.choices[0].message.content! }); |
79 |
| -}); |
80 |
| - |
81 |
| -workflow.handle([humanInteractionRequestEvent], async (reason) => { |
82 |
| - const snapshot = await getContext().snapshot(); |
83 |
| - const requestId = crypto.randomUUID(); |
84 |
| - serializableMemoryMap.set(requestId, snapshot); |
85 |
| - return stopEvent.with({ |
86 |
| - requestId: requestId, |
87 |
| - reason: reason, |
88 |
| - data: "request human in the loop", |
89 |
| - }); |
| 75 | + return stopEvent.with(response.choices[0].message.content!); |
90 | 76 | });
|
91 | 77 |
|
92 | 78 | workflow.handle([humanInteractionResponseEvent], async ({ data }) => {
|
|
0 commit comments