|
| 1 | +import { AbstractAgent } from "@/agent"; |
| 2 | +import { BaseEvent, EventType, Message, RunAgentInput } from "@ag-ui/core"; |
| 3 | +import { Observable, of } from "rxjs"; |
| 4 | + |
| 5 | +class LegacyAgent extends AbstractAgent { |
| 6 | + public receivedInput?: RunAgentInput; |
| 7 | + |
| 8 | + constructor(initialMessages: Message[]) { |
| 9 | + super({ initialMessages }); |
| 10 | + } |
| 11 | + |
| 12 | + override get maxVersion(): string { |
| 13 | + return "0.0.39"; |
| 14 | + } |
| 15 | + |
| 16 | + override run(input: RunAgentInput): Observable<BaseEvent> { |
| 17 | + this.receivedInput = input; |
| 18 | + return of({ |
| 19 | + type: EventType.RUN_STARTED, |
| 20 | + threadId: input.threadId, |
| 21 | + runId: input.runId, |
| 22 | + } as BaseEvent); |
| 23 | + } |
| 24 | + |
| 25 | + protected override prepareRunAgentInput( |
| 26 | + parameters?: Parameters<AbstractAgent["prepareRunAgentInput"]>[0], |
| 27 | + ): RunAgentInput { |
| 28 | + const prepared = super.prepareRunAgentInput(parameters); |
| 29 | + return { ...prepared, parentRunId: "legacy-parent" }; |
| 30 | + } |
| 31 | +} |
| 32 | + |
| 33 | +describe("BackwardCompatibility_0_0_39 middleware (auto insertion)", () => { |
| 34 | + it("automatically strips parentRunId and flattens array message content when maxVersion <= 0.0.39", async () => { |
| 35 | + const initialMessages: Message[] = [ |
| 36 | + { |
| 37 | + id: "msg-1", |
| 38 | + role: "user", |
| 39 | + content: [ |
| 40 | + { type: "text", text: "Hello " }, |
| 41 | + { type: "text", text: "world!" }, |
| 42 | + { type: "binary", mimeType: "text/plain", data: "ignored" }, |
| 43 | + ] as unknown as Message["content"], |
| 44 | + } as Message, |
| 45 | + { |
| 46 | + id: "msg-2", |
| 47 | + role: "assistant", |
| 48 | + content: undefined, |
| 49 | + } as Message, |
| 50 | + ]; |
| 51 | + |
| 52 | + const agent = new LegacyAgent(initialMessages); |
| 53 | + |
| 54 | + await agent.runAgent({ |
| 55 | + runId: "run-1", |
| 56 | + tools: [], |
| 57 | + context: [], |
| 58 | + forwardedProps: {}, |
| 59 | + }); |
| 60 | + |
| 61 | + expect(agent.receivedInput).toBeDefined(); |
| 62 | + expect(agent.receivedInput?.parentRunId).toBeUndefined(); |
| 63 | + expect(agent.receivedInput?.messages[0].content).toBe("Hello world!"); |
| 64 | + expect(agent.receivedInput?.messages[1].content).toBe(""); |
| 65 | + }); |
| 66 | +}); |
0 commit comments