|
3 | 3 | agentToolCallResultEvent, |
4 | 4 | run, |
5 | 5 | startAgentEvent, |
| 6 | + stopAgentEvent, |
| 7 | + WorkflowStream, |
6 | 8 | type AgentInputData, |
7 | 9 | type WorkflowEventData, |
8 | 10 | } from "@llamaindex/workflow"; |
@@ -49,51 +51,49 @@ export async function runWorkflow( |
49 | 51 | }); |
50 | 52 | } |
51 | 53 |
|
52 | | -// Process the workflow stream to handle non-stream events as annotations |
53 | | -async function* processWorkflowStream( |
54 | | - stream: AsyncIterable<WorkflowEventData<unknown>>, |
55 | | -): AsyncIterable<WorkflowEventData<unknown>> { |
56 | | - for await (const event of stream) { |
57 | | - const transformedEvent = transformWorkflowEvent(event); |
| 54 | +function processWorkflowStream( |
| 55 | + stream: WorkflowStream<WorkflowEventData<unknown>>, |
| 56 | +) { |
| 57 | + return stream.until(stopAgentEvent).pipeThrough( |
| 58 | + new TransformStream<WorkflowEventData<unknown>, WorkflowEventData<unknown>>( |
| 59 | + { |
| 60 | + async transform(event, controller) { |
| 61 | + let transformedEvent = event; |
58 | 62 |
|
59 | | - if (sourceEvent.include(transformedEvent)) { |
60 | | - const sourceNodes = transformedEvent.data.data.nodes; |
61 | | - downloadLlamaCloudFilesFromNodes(sourceNodes); // download files in background |
62 | | - } |
63 | | - yield transformedEvent; |
64 | | - } |
65 | | -} |
| 63 | + // Handle agent events from AgentToolCall |
| 64 | + if (agentToolCallEvent.include(event)) { |
| 65 | + const inputString = JSON.stringify(event.data.toolKwargs); |
| 66 | + transformedEvent = toAgentRunEvent({ |
| 67 | + agent: event.data.agentName, |
| 68 | + text: `Using tool: '${event.data.toolName}' with inputs: '${inputString}'`, |
| 69 | + type: "text", |
| 70 | + }); |
| 71 | + } |
| 72 | + // Handle source nodes from AgentToolCallResult |
| 73 | + else if (agentToolCallResultEvent.include(event)) { |
| 74 | + const rawOutput = event.data.raw; |
| 75 | + if ( |
| 76 | + rawOutput && |
| 77 | + typeof rawOutput === "object" && |
| 78 | + "sourceNodes" in rawOutput // TODO: better use Zod to validate and extract sourceNodes from toolCallResult |
| 79 | + ) { |
| 80 | + const sourceNodes = |
| 81 | + rawOutput.sourceNodes as unknown as NodeWithScore<Metadata>[]; |
| 82 | + transformedEvent = toSourceEvent(sourceNodes); |
| 83 | + } |
| 84 | + } |
66 | 85 |
|
67 | | -// transform WorkflowEvent to another WorkflowEvent for annotations display purpose |
68 | | -// this useful for handling AgentWorkflow events, because we cannot easily append custom events like custom workflows |
69 | | -function transformWorkflowEvent( |
70 | | - event: WorkflowEventData<unknown>, |
71 | | -): WorkflowEventData<unknown> { |
72 | | - // convert AgentToolCall event to AgentRunEvent |
73 | | - if (agentToolCallEvent.include(event)) { |
74 | | - const inputString = JSON.stringify(event.data.toolKwargs); |
75 | | - return toAgentRunEvent({ |
76 | | - agent: event.data.agentName, |
77 | | - text: `Using tool: '${event.data.toolName}' with inputs: '${inputString}'`, |
78 | | - type: "text", |
79 | | - }); |
80 | | - } |
| 86 | + // Post-process for llama-cloud files |
| 87 | + if (sourceEvent.include(transformedEvent)) { |
| 88 | + const sourceNodesForDownload = transformedEvent.data.data.nodes; // These are SourceEventNode[] |
| 89 | + downloadLlamaCloudFilesFromNodes(sourceNodesForDownload); // download files in background |
| 90 | + } |
81 | 91 |
|
82 | | - // if AgentToolCallResult contains sourceNodes, convert it to SourceEvent |
83 | | - if (agentToolCallResultEvent.include(event)) { |
84 | | - const rawOutput = event.data.raw; |
85 | | - if ( |
86 | | - rawOutput && |
87 | | - typeof rawOutput === "object" && |
88 | | - "sourceNodes" in rawOutput // TODO: better use Zod to validate and extract sourceNodes from toolCallResult |
89 | | - ) { |
90 | | - return toSourceEvent( |
91 | | - rawOutput.sourceNodes as unknown as NodeWithScore<Metadata>[], |
92 | | - ); |
93 | | - } |
94 | | - } |
95 | | - |
96 | | - return event; |
| 92 | + controller.enqueue(transformedEvent); |
| 93 | + }, |
| 94 | + }, |
| 95 | + ), |
| 96 | + ); |
97 | 97 | } |
98 | 98 |
|
99 | 99 | async function downloadLlamaCloudFilesFromNodes(nodes: SourceEventNode[]) { |
|
0 commit comments