|
1 | 1 | import { createHash } from "node:crypto"; |
2 | | -import { |
3 | | - EventSchemas, |
4 | | - HttpAgent, |
5 | | - type Message, |
6 | | - type RunAgentInput, |
7 | | -} from "@ag-ui/client"; |
| 2 | +import { HttpAgent, type Message, type RunAgentInput } from "@ag-ui/client"; |
| 3 | +import { EventSchemas } from "@ag-ui/core/schemas"; |
8 | 4 | import type { BuiltInAgentClassicConfig } from "@copilotkit/runtime/v2"; |
9 | 5 | import type { Subscription } from "rxjs"; |
10 | 6 | import { |
@@ -97,11 +93,20 @@ export function planMessages(prompt: Options["prompt"]): Message[] { |
97 | 93 | `Plan models cannot receive ${part.mediaType} attachments.`, |
98 | 94 | ); |
99 | 95 | const data = part.data; |
100 | | - const url = |
101 | | - data instanceof URL |
102 | | - ? data.href |
103 | | - : `data:${part.mediaType};base64,${typeof data === "string" ? data : Buffer.from(data).toString("base64")}`; |
104 | | - return { type: "binary", mimeType: part.mediaType, url }; |
| 96 | + return { |
| 97 | + type: "image", |
| 98 | + source: |
| 99 | + data instanceof URL |
| 100 | + ? { type: "url", value: data.href, mimeType: part.mediaType } |
| 101 | + : { |
| 102 | + type: "data", |
| 103 | + value: |
| 104 | + typeof data === "string" |
| 105 | + ? data |
| 106 | + : Buffer.from(data).toString("base64"), |
| 107 | + mimeType: part.mediaType, |
| 108 | + }, |
| 109 | + }; |
105 | 110 | }), |
106 | 111 | }); |
107 | 112 | } else if (message.role === "assistant") { |
@@ -152,37 +157,6 @@ export function planMessages(prompt: Options["prompt"]): Message[] { |
152 | 157 | return messages; |
153 | 158 | } |
154 | 159 |
|
155 | | -/** The pinned JS client uses binary parts; current Python AG-UI uses image/source. */ |
156 | | -function planWireBody( |
157 | | - body: BodyInit | null | undefined, |
158 | | -): BodyInit | null | undefined { |
159 | | - if (typeof body !== "string") return body; |
160 | | - const input = JSON.parse(body) as RunAgentInput; |
161 | | - return JSON.stringify({ |
162 | | - ...input, |
163 | | - messages: input.messages.map((message) => { |
164 | | - if (message.role !== "user" || typeof message.content === "string") |
165 | | - return message; |
166 | | - return { |
167 | | - ...message, |
168 | | - content: message.content.map((part) => { |
169 | | - if (part.type !== "binary") return part; |
170 | | - if (!part.mimeType.startsWith("image/")) |
171 | | - throw new Error("The plan model only supports image media parts."); |
172 | | - const encoded = part.data ?? part.url?.split(";base64,", 2)[1]; |
173 | | - return { |
174 | | - type: "image", |
175 | | - source: |
176 | | - encoded !== undefined |
177 | | - ? { type: "data", value: encoded, mimeType: part.mimeType } |
178 | | - : { type: "url", value: part.url, mimeType: part.mimeType }, |
179 | | - }; |
180 | | - }), |
181 | | - }; |
182 | | - }), |
183 | | - }); |
184 | | -} |
185 | | - |
186 | 160 | /** Model transport only. BuiltInAgent still validates and executes every server tool. */ |
187 | 161 | export class PlanModel implements PlanLanguageModel { |
188 | 162 | readonly specificationVersion = "v3"; |
@@ -296,7 +270,6 @@ export class PlanModel implements PlanLanguageModel { |
296 | 270 | fetch: (url, init) => |
297 | 271 | fetch(url, { |
298 | 272 | ...init, |
299 | | - body: planWireBody(init?.body), |
300 | 273 | redirect: "error", |
301 | 274 | signal: AbortSignal.any([ |
302 | 275 | ...(init?.signal ? [init.signal] : []), |
|
0 commit comments