Skip to content

Commit 134b294

Browse files
send snapshot as payload
1 parent 393231c commit 134b294

File tree

2 files changed

+38
-39
lines changed

2 files changed

+38
-39
lines changed

demo/hono/app.ts

Lines changed: 22 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,12 +18,14 @@ app.post(
1818
)
1919
);
2020

21+
export const serializableMemoryMap = new Map<string, any>();
22+
2123
app.post("/human-in-the-loop", async (ctx) => {
2224
const {
2325
workflow,
2426
stopEvent,
2527
startEvent,
26-
serializableMemoryMap,
28+
humanInteractionRequestEvent,
2729
humanInteractionResponseEvent,
2830
} = await import("../workflows/human-in-the-loop");
2931

@@ -45,15 +47,26 @@ app.post("/human-in-the-loop", async (ctx) => {
4547
}
4648

4749
const { stream } = context;
48-
return new Promise<Response>((resolve) => {
50+
return new Promise<Response>(async (resolve) => {
4951
// consume stream
50-
stream
51-
.until(stopEvent)
52-
.toArray()
53-
.then((events) => {
54-
const stopEvent = events.at(-1)!;
55-
resolve(Response.json(stopEvent.data));
56-
});
52+
for await (const event of stream) {
53+
if (humanInteractionRequestEvent.include(event)) {
54+
// request for a human, serialize the workflow for later resume
55+
const requestId = crypto.randomUUID();
56+
serializableMemoryMap.set(requestId, event.data.snapshot);
57+
// send request id to user
58+
resolve(
59+
Response.json({
60+
requestId: requestId,
61+
reason: event.data.reason,
62+
data: "request human in the loop",
63+
})
64+
);
65+
}
66+
if (stopEvent.include(event)) {
67+
resolve(Response.json(event.data));
68+
}
69+
}
5770
});
5871
});
5972

demo/workflows/human-in-the-loop.ts

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -1,32 +1,25 @@
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";
36
import { OpenAI } from "openai";
47

5-
export const serializableMemoryMap = new Map<string, any>();
6-
78
const openai = new OpenAI();
89

910
const workflow = withSnapshot(createWorkflow());
1011

1112
const startEvent = workflowEvent<string>({
1213
debugLabel: "start",
1314
});
14-
const humanInteractionRequestEvent = workflowEvent<string>({
15-
debugLabel: "humanInteractionRequest",
16-
});
15+
const humanInteractionRequestEvent = workflowEvent<{
16+
snapshot: SnapshotData;
17+
reason: string;
18+
}>();
1719
const humanInteractionResponseEvent = workflowEvent<string>({
1820
debugLabel: "humanInteractionResponse",
1921
});
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>({
3023
debugLabel: "stop",
3124
});
3225

@@ -72,21 +65,14 @@ For example, alex is from "Alexander the Great", who was a king of the ancient G
7265
if (tools && tools.length > 0) {
7366
const askName = tools.find((tool) => tool.function.name === "ask_name");
7467
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+
});
7673
}
7774
}
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!);
9076
});
9177

9278
workflow.handle([humanInteractionResponseEvent], async ({ data }) => {

0 commit comments

Comments
 (0)