|
| 1 | +import { EventType } from "@openuidev/react-headless"; |
| 2 | +import { describe, expect, it } from "vitest"; |
| 3 | +import { agnoAGUIAdapter } from "../adapter"; |
| 4 | + |
| 5 | +function aguiResponse(events: object[]) { |
| 6 | + return new Response(events.map((event) => `data: ${JSON.stringify(event)}\n\n`).join(""), { |
| 7 | + headers: { "Content-Type": "text/event-stream" }, |
| 8 | + }); |
| 9 | +} |
| 10 | + |
| 11 | +async function parse(events: object[]) { |
| 12 | + const parsed = []; |
| 13 | + for await (const event of agnoAGUIAdapter().parse(aguiResponse(events))) parsed.push(event); |
| 14 | + return parsed; |
| 15 | +} |
| 16 | + |
| 17 | +describe("agnoAGUIAdapter", () => { |
| 18 | + it("removes AgentOS lifecycle, state, raw events, and an empty tool parent", async () => { |
| 19 | + const parsed = await parse([ |
| 20 | + { type: EventType.RUN_STARTED, threadId: "thread-1", runId: "run-1" }, |
| 21 | + { type: EventType.STATE_SNAPSHOT, snapshot: {} }, |
| 22 | + { type: EventType.TEXT_MESSAGE_START, messageId: "empty", role: "assistant" }, |
| 23 | + { type: EventType.TEXT_MESSAGE_END, messageId: "empty" }, |
| 24 | + { |
| 25 | + type: EventType.TOOL_CALL_START, |
| 26 | + toolCallId: "call-1", |
| 27 | + toolCallName: "get_quarterly_revenue", |
| 28 | + parentMessageId: "empty", |
| 29 | + }, |
| 30 | + { type: EventType.TOOL_CALL_ARGS, toolCallId: "call-1", delta: "{}" }, |
| 31 | + { type: EventType.TOOL_CALL_END, toolCallId: "call-1" }, |
| 32 | + { |
| 33 | + type: EventType.TOOL_CALL_RESULT, |
| 34 | + toolCallId: "call-1", |
| 35 | + messageId: "call-1", |
| 36 | + role: "tool", |
| 37 | + content: '{"quarters":[]}', |
| 38 | + }, |
| 39 | + { type: EventType.TEXT_MESSAGE_START, messageId: "answer", role: "assistant" }, |
| 40 | + { type: EventType.TEXT_MESSAGE_CONTENT, messageId: "answer", delta: "root = Card([])" }, |
| 41 | + { type: EventType.TEXT_MESSAGE_END, messageId: "answer" }, |
| 42 | + { type: EventType.RUN_FINISHED, threadId: "thread-1", runId: "run-1" }, |
| 43 | + ]); |
| 44 | + |
| 45 | + expect(parsed.map((event) => event.type)).toEqual([ |
| 46 | + EventType.TOOL_CALL_START, |
| 47 | + EventType.TOOL_CALL_ARGS, |
| 48 | + EventType.TOOL_CALL_END, |
| 49 | + EventType.TOOL_CALL_RESULT, |
| 50 | + EventType.TEXT_MESSAGE_START, |
| 51 | + EventType.TEXT_MESSAGE_CONTENT, |
| 52 | + EventType.TEXT_MESSAGE_END, |
| 53 | + ]); |
| 54 | + }); |
| 55 | + |
| 56 | + it("keeps non-text events buffered inside an empty envelope", async () => { |
| 57 | + const parsed = await parse([ |
| 58 | + { type: EventType.TEXT_MESSAGE_START, messageId: "empty", role: "assistant" }, |
| 59 | + { |
| 60 | + type: EventType.TOOL_CALL_START, |
| 61 | + toolCallId: "call-1", |
| 62 | + toolCallName: "lookup", |
| 63 | + }, |
| 64 | + { type: EventType.TEXT_MESSAGE_END, messageId: "empty" }, |
| 65 | + ]); |
| 66 | + |
| 67 | + expect(parsed).toEqual([ |
| 68 | + expect.objectContaining({ type: EventType.TOOL_CALL_START, toolCallId: "call-1" }), |
| 69 | + ]); |
| 70 | + }); |
| 71 | + |
| 72 | + it("keeps a text envelope once meaningful content arrives", async () => { |
| 73 | + const parsed = await parse([ |
| 74 | + { type: EventType.TEXT_MESSAGE_START, messageId: "answer", role: "assistant" }, |
| 75 | + { type: EventType.TEXT_MESSAGE_CONTENT, messageId: "answer", delta: "hello" }, |
| 76 | + { type: EventType.TEXT_MESSAGE_END, messageId: "answer" }, |
| 77 | + ]); |
| 78 | + |
| 79 | + expect(parsed.map((event) => event.type)).toEqual([ |
| 80 | + EventType.TEXT_MESSAGE_START, |
| 81 | + EventType.TEXT_MESSAGE_CONTENT, |
| 82 | + EventType.TEXT_MESSAGE_END, |
| 83 | + ]); |
| 84 | + }); |
| 85 | +}); |
0 commit comments