|
| 1 | +import type { AssistantMessage, Message, ToolMessage, UserMessage } from "../../types"; |
| 2 | +import type { MessageFormat } from "../../types/messageFormat"; |
| 3 | + |
| 4 | +// ── LangGraph / LangChain message types ────────────────────────── |
| 5 | + |
| 6 | +/** |
| 7 | + * LangChain-style message as returned by the LangGraph thread state API. |
| 8 | + * Each message carries a `type` discriminator (`"human"`, `"ai"`, `"tool"`, |
| 9 | + * `"system"`) and uses snake_case field names. |
| 10 | + */ |
| 11 | +interface LangChainMessage { |
| 12 | + id?: string; |
| 13 | + type: "human" | "ai" | "tool" | "system" | "developer" | (string & {}); |
| 14 | + content: string | Array<{ type: string; text?: string }>; |
| 15 | + name?: string; |
| 16 | + tool_calls?: Array<{ |
| 17 | + id: string; |
| 18 | + name: string; |
| 19 | + args: Record<string, unknown> | string; |
| 20 | + }>; |
| 21 | + tool_call_id?: string; |
| 22 | +} |
| 23 | + |
| 24 | +// ── Outbound (AG-UI → LangGraph) ──────────────────────────────── |
| 25 | + |
| 26 | +function toLangChainMessage(message: Message): LangChainMessage { |
| 27 | + switch (message.role) { |
| 28 | + case "user": |
| 29 | + return { type: "human", content: message.content ?? "" }; |
| 30 | + |
| 31 | + case "assistant": { |
| 32 | + const result: LangChainMessage = { type: "ai", content: message.content ?? "" }; |
| 33 | + if (message.toolCalls?.length) { |
| 34 | + result.tool_calls = message.toolCalls.map((tc) => ({ |
| 35 | + id: tc.id, |
| 36 | + name: tc.function.name, |
| 37 | + args: safeParseArgs(tc.function.arguments), |
| 38 | + })); |
| 39 | + } |
| 40 | + return result; |
| 41 | + } |
| 42 | + |
| 43 | + case "tool": |
| 44 | + return { |
| 45 | + type: "tool", |
| 46 | + content: message.content, |
| 47 | + tool_call_id: message.toolCallId, |
| 48 | + }; |
| 49 | + |
| 50 | + case "system": |
| 51 | + return { type: "system", content: message.content }; |
| 52 | + |
| 53 | + case "developer": |
| 54 | + return { type: "system", content: message.content }; |
| 55 | + |
| 56 | + default: |
| 57 | + return { type: "system", content: "" }; |
| 58 | + } |
| 59 | +} |
| 60 | + |
| 61 | +// ── Inbound (LangGraph → AG-UI) ──────────────────────────────── |
| 62 | + |
| 63 | +function fromLangChainMessage(msg: LangChainMessage): Message { |
| 64 | + const id = msg.id ?? crypto.randomUUID(); |
| 65 | + |
| 66 | + switch (msg.type) { |
| 67 | + case "human": |
| 68 | + return { id, role: "user", content: extractContent(msg.content) } satisfies UserMessage; |
| 69 | + |
| 70 | + case "ai": { |
| 71 | + const result: AssistantMessage = { |
| 72 | + id, |
| 73 | + role: "assistant", |
| 74 | + content: extractContent(msg.content), |
| 75 | + }; |
| 76 | + if (msg.tool_calls?.length) { |
| 77 | + result.toolCalls = msg.tool_calls.map((tc) => ({ |
| 78 | + id: tc.id, |
| 79 | + type: "function" as const, |
| 80 | + function: { |
| 81 | + name: tc.name, |
| 82 | + arguments: typeof tc.args === "string" ? tc.args : JSON.stringify(tc.args), |
| 83 | + }, |
| 84 | + })); |
| 85 | + } |
| 86 | + return result; |
| 87 | + } |
| 88 | + |
| 89 | + case "tool": |
| 90 | + return { |
| 91 | + id, |
| 92 | + role: "tool", |
| 93 | + content: extractContent(msg.content), |
| 94 | + toolCallId: msg.tool_call_id ?? "", |
| 95 | + } satisfies ToolMessage; |
| 96 | + |
| 97 | + case "system": |
| 98 | + case "developer": |
| 99 | + return { id, role: "system", content: extractContent(msg.content) }; |
| 100 | + |
| 101 | + default: |
| 102 | + return { id, role: "system", content: extractContent(msg.content) }; |
| 103 | + } |
| 104 | +} |
| 105 | + |
| 106 | +// ── Helpers ────────────────────────────────────────────────────── |
| 107 | + |
| 108 | +function extractContent(content: string | Array<{ type: string; text?: string }>): string { |
| 109 | + if (typeof content === "string") return content; |
| 110 | + return content |
| 111 | + .filter((block) => block.type === "text" && block.text) |
| 112 | + .map((block) => block.text!) |
| 113 | + .join(""); |
| 114 | +} |
| 115 | + |
| 116 | +function safeParseArgs(args: string): Record<string, unknown> | string { |
| 117 | + try { |
| 118 | + return JSON.parse(args) as Record<string, unknown>; |
| 119 | + } catch { |
| 120 | + return args; |
| 121 | + } |
| 122 | +} |
| 123 | + |
| 124 | +// ── MessageFormat implementation ───────────────────────────────── |
| 125 | + |
| 126 | +/** |
| 127 | + * Converts between AG-UI message format and LangGraph's LangChain-style |
| 128 | + * message format. |
| 129 | + * |
| 130 | + * LangGraph uses `type` discriminators (`"human"`, `"ai"`, `"tool"`, |
| 131 | + * `"system"`) instead of `role`, and tool call arguments are objects |
| 132 | + * rather than JSON strings. |
| 133 | + * |
| 134 | + * AG-UI → LangGraph (toApi): |
| 135 | + * - Maps `role` to `type` (`"user"` → `"human"`, `"assistant"` → `"ai"`) |
| 136 | + * - Converts `toolCalls[].function.arguments` from JSON string to object |
| 137 | + * - Converts `toolCallId` → `tool_call_id` |
| 138 | + * |
| 139 | + * LangGraph → AG-UI (fromApi): |
| 140 | + * - Maps `type` to `role` (`"human"` → `"user"`, `"ai"` → `"assistant"`) |
| 141 | + * - Converts tool call `args` object to JSON string |
| 142 | + * - Generates `id` via `crypto.randomUUID()` if not present |
| 143 | + */ |
| 144 | +export const langGraphMessageFormat: MessageFormat = { |
| 145 | + toApi(messages: Message[]): LangChainMessage[] { |
| 146 | + return messages.map(toLangChainMessage); |
| 147 | + }, |
| 148 | + |
| 149 | + fromApi(data: unknown): Message[] { |
| 150 | + return (data as LangChainMessage[]).map(fromLangChainMessage); |
| 151 | + }, |
| 152 | +}; |
| 153 | + |
| 154 | +export type { LangChainMessage as LangGraphMessageFormat }; |
0 commit comments