|
| 1 | +import { Middleware } from "./middleware"; |
| 2 | +import { AbstractAgent } from "@/agent"; |
| 3 | +import type { RunAgentInput, BaseEvent } from "@ag-ui/core"; |
| 4 | +import type { Observable } from "rxjs"; |
| 5 | + |
| 6 | +type InputMessage = RunAgentInput["messages"][number]; |
| 7 | + |
| 8 | +function sanitizeMessageContent(message: InputMessage): InputMessage { |
| 9 | + const rawContent = (message as { content?: unknown }).content; |
| 10 | + |
| 11 | + if (Array.isArray(rawContent)) { |
| 12 | + const concatenatedContent = rawContent |
| 13 | + .filter( |
| 14 | + (part): part is { type: "text"; text: string } => |
| 15 | + typeof part === "object" && |
| 16 | + part !== null && |
| 17 | + "type" in part && |
| 18 | + (part as { type: unknown }).type === "text" && |
| 19 | + typeof (part as { text?: unknown }).text === "string", |
| 20 | + ) |
| 21 | + .map((part) => part.text) |
| 22 | + .join(""); |
| 23 | + |
| 24 | + return { |
| 25 | + ...message, |
| 26 | + content: concatenatedContent, |
| 27 | + } as InputMessage; |
| 28 | + } |
| 29 | + |
| 30 | + if (typeof rawContent === "string") { |
| 31 | + return message; |
| 32 | + } |
| 33 | + |
| 34 | + return { |
| 35 | + ...message, |
| 36 | + content: "", |
| 37 | + } as InputMessage; |
| 38 | +} |
| 39 | + |
| 40 | +/** |
| 41 | + * Middleware placeholder that maintains compatibility with AG-UI 0.0.39 flows. |
| 42 | + * Currently it simply forwards all events to the next middleware/agent. |
| 43 | + */ |
| 44 | +export class BackwardCompatibility_0_0_39 extends Middleware { |
| 45 | + override run(input: RunAgentInput, next: AbstractAgent): Observable<BaseEvent> { |
| 46 | + const { parentRunId: _parentRunId, ...rest } = input; |
| 47 | + const sanitizedInput: RunAgentInput = { |
| 48 | + ...rest, |
| 49 | + messages: rest.messages.map(sanitizeMessageContent), |
| 50 | + } as RunAgentInput; |
| 51 | + |
| 52 | + return this.runNext(sanitizedInput, next); |
| 53 | + } |
| 54 | +} |
0 commit comments