llm-messages 0.3.0 → 0.4.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/CHANGELOG.md CHANGED
@@ -4,6 +4,17 @@ All notable changes to this project are documented here. The format is based on
4
4
  [Keep a Changelog](https://keepachangelog.com/en/1.1.0/) and this project adheres
5
5
  to [Semantic Versioning](https://semver.org/spec/v2.0.0.html).
6
6
 
7
+ ## [0.4.0] - 2026-06-01
8
+
9
+ ### Added
10
+
11
+ - Audio and document content parts. Audio (OpenAI `input_audio`) converts between
12
+ OpenAI and Gemini; Anthropic has no audio input, so audio is dropped with an
13
+ `unsupported-modality` warning. Documents (OpenAI `file`, Anthropic `document`,
14
+ Gemini `inlineData` / `fileData`) convert across all three, base64 losslessly.
15
+ Adds the `MediaPart` type and `unsupported-modality` / `gemini-url-media`
16
+ warning codes. (#5)
17
+
7
18
  ## [0.3.0] - 2026-06-01
8
19
 
9
20
  ### Added
package/README.md CHANGED
@@ -135,7 +135,7 @@ deterministic one.
135
135
  | Match key | `tool_call_id` | `tool_use_id` | function `name` (id optional) |
136
136
  | Role alternation | not required | strict | strict |
137
137
 
138
- ## Images
138
+ ## Images, audio and documents
139
139
 
140
140
  Image parts convert across all three providers:
141
141
 
@@ -159,11 +159,16 @@ Base64 data URLs round trip losslessly. A remote `https` URL maps to an Anthropi
159
159
  `gemini-url-image` warning, since Gemini may require the Files API for non-Google
160
160
  URIs.
161
161
 
162
+ **Audio** (`input_audio`) and **documents** (`file`, e.g. PDF) convert too. Audio
163
+ moves between OpenAI and Gemini; Anthropic has no audio input, so an audio part is
164
+ dropped with an `unsupported-modality` warning. Documents convert across all three
165
+ (OpenAI `file`, Anthropic `document`, Gemini `inlineData`).
166
+
162
167
  ## Scope
163
168
 
164
- Version 0.x covers text, system prompts, tool calls/results and images, which is
165
- the core of every agent loop. Other modalities (audio, files) are passed through
166
- where possible and reported as `dropped-content` otherwise.
169
+ Version 0.x covers text, system prompts, tool calls/results, images, audio and
170
+ documents, which is the core of every agent loop. Unsupported parts are reported
171
+ via `dropped-content` rather than failing.
167
172
 
168
173
  ## Part of a set
169
174
 
package/dist/index.cjs CHANGED
@@ -128,13 +128,16 @@ function imageToAnthropic(image) {
128
128
  function imageFromGemini(part) {
129
129
  if (isRecord(part) && isRecord(part.inlineData)) {
130
130
  const data = part.inlineData;
131
- if (typeof data.mimeType === "string" && typeof data.data === "string") {
131
+ if (typeof data.mimeType === "string" && typeof data.data === "string" && data.mimeType.startsWith("image/")) {
132
132
  return { kind: "base64", mediaType: data.mimeType, data: data.data };
133
133
  }
134
134
  }
135
135
  if (isRecord(part) && isRecord(part.fileData)) {
136
136
  const data = part.fileData;
137
- if (typeof data.fileUri === "string") return { kind: "url", url: data.fileUri };
137
+ const mime = typeof data.mimeType === "string" ? data.mimeType : "";
138
+ if (typeof data.fileUri === "string" && (mime === "" || mime.startsWith("image/"))) {
139
+ return { kind: "url", url: data.fileUri };
140
+ }
138
141
  }
139
142
  return null;
140
143
  }
@@ -149,6 +152,124 @@ function imageToGemini(image, reporter) {
149
152
  return { fileData: { fileUri: image.url } };
150
153
  }
151
154
 
155
+ // src/media.ts
156
+ function modalityFromMime(mediaType) {
157
+ return mediaType.startsWith("audio/") ? "audio" : "document";
158
+ }
159
+ function mediaFromOpenAI(part) {
160
+ if (!isRecord(part)) return null;
161
+ if (part.type === "input_audio" && isRecord(part.input_audio)) {
162
+ const audio = part.input_audio;
163
+ if (typeof audio.data === "string") {
164
+ const format = typeof audio.format === "string" ? audio.format : "wav";
165
+ return { modality: "audio", source: { kind: "base64", mediaType: `audio/${format}`, data: audio.data } };
166
+ }
167
+ }
168
+ if (part.type === "file" && isRecord(part.file)) {
169
+ const file = part.file;
170
+ const filename = typeof file.filename === "string" ? file.filename : void 0;
171
+ if (typeof file.file_data === "string") {
172
+ const parsed = parseDataUrl(file.file_data);
173
+ if (parsed) return { modality: "document", source: { kind: "base64", ...parsed }, filename };
174
+ }
175
+ if (typeof file.file_id === "string") {
176
+ return { modality: "document", source: { kind: "file_id", id: file.file_id }, filename };
177
+ }
178
+ }
179
+ return null;
180
+ }
181
+ function mediaToOpenAI(media) {
182
+ const { modality, source } = media;
183
+ if (modality === "audio") {
184
+ if (source.kind !== "base64") return null;
185
+ const audio = {
186
+ type: "input_audio",
187
+ input_audio: { data: source.data, format: source.mediaType.replace(/^audio\//, "") }
188
+ };
189
+ return audio;
190
+ }
191
+ if (source.kind === "base64") {
192
+ const file = {
193
+ type: "file",
194
+ file: {
195
+ file_data: toDataUrl(source.mediaType, source.data),
196
+ ...media.filename ? { filename: media.filename } : {}
197
+ }
198
+ };
199
+ return file;
200
+ }
201
+ if (source.kind === "file_id") {
202
+ const file = {
203
+ type: "file",
204
+ file: { file_id: source.id, ...media.filename ? { filename: media.filename } : {} }
205
+ };
206
+ return file;
207
+ }
208
+ return null;
209
+ }
210
+ function mediaFromAnthropic(block) {
211
+ if (!isRecord(block) || block.type !== "document" || !isRecord(block.source)) return null;
212
+ const source = block.source;
213
+ if (source.type === "base64" && typeof source.media_type === "string" && typeof source.data === "string") {
214
+ return { modality: "document", source: { kind: "base64", mediaType: source.media_type, data: source.data } };
215
+ }
216
+ if (source.type === "url" && typeof source.url === "string") {
217
+ return { modality: "document", source: { kind: "url", url: source.url } };
218
+ }
219
+ if (source.type === "file" && typeof source.file_id === "string") {
220
+ return { modality: "document", source: { kind: "file_id", id: source.file_id } };
221
+ }
222
+ return null;
223
+ }
224
+ function mediaToAnthropic(media, reporter) {
225
+ if (media.modality === "audio") {
226
+ reporter.warn("unsupported-modality", "Anthropic has no audio input; dropped an audio part.");
227
+ return null;
228
+ }
229
+ const { source } = media;
230
+ if (source.kind === "base64") {
231
+ return { type: "document", source: { type: "base64", media_type: source.mediaType, data: source.data } };
232
+ }
233
+ if (source.kind === "url") {
234
+ return { type: "document", source: { type: "url", url: source.url } };
235
+ }
236
+ return { type: "document", source: { type: "file", file_id: source.id } };
237
+ }
238
+ function mediaFromGemini(part) {
239
+ if (isRecord(part) && isRecord(part.inlineData)) {
240
+ const data = part.inlineData;
241
+ if (typeof data.mimeType === "string" && typeof data.data === "string" && !data.mimeType.startsWith("image/")) {
242
+ return {
243
+ modality: modalityFromMime(data.mimeType),
244
+ source: { kind: "base64", mediaType: data.mimeType, data: data.data }
245
+ };
246
+ }
247
+ }
248
+ if (isRecord(part) && isRecord(part.fileData)) {
249
+ const data = part.fileData;
250
+ const mime = typeof data.mimeType === "string" ? data.mimeType : "";
251
+ if (typeof data.fileUri === "string" && mime !== "" && !mime.startsWith("image/")) {
252
+ return { modality: modalityFromMime(mime), source: { kind: "url", url: data.fileUri } };
253
+ }
254
+ }
255
+ return null;
256
+ }
257
+ function mediaToGemini(media, reporter) {
258
+ const { source } = media;
259
+ if (source.kind === "base64") {
260
+ return { inlineData: { mimeType: source.mediaType, data: source.data } };
261
+ }
262
+ if (source.kind === "url") {
263
+ reporter.warn(
264
+ "gemini-url-media",
265
+ "A media URL was emitted as Gemini fileData.fileUri; Gemini may require the Files API for non-Google URIs."
266
+ );
267
+ return { fileData: { fileUri: source.url } };
268
+ }
269
+ reporter.warn("unsupported-modality", "Gemini has no file-id media reference; dropped a file_id part.");
270
+ return null;
271
+ }
272
+
152
273
  // src/providers/openai.ts
153
274
  function isSystem(message) {
154
275
  return message.role === "system" || message.role === "developer";
@@ -217,6 +338,12 @@ function userContent(content, reporter) {
217
338
  blocks.push(imageToAnthropic(image));
218
339
  continue;
219
340
  }
341
+ const media = mediaFromOpenAI(part);
342
+ if (media) {
343
+ const block = mediaToAnthropic(media, reporter);
344
+ if (block) blocks.push(block);
345
+ continue;
346
+ }
220
347
  reporter.warn("dropped-content", "Dropped an unsupported user content part.");
221
348
  }
222
349
  return blocks;
@@ -258,7 +385,7 @@ function asBlocks(content) {
258
385
  return content ? [{ type: "text", text: content }] : [];
259
386
  }
260
387
  function fromAnthropic(conversation, options = {}) {
261
- void options;
388
+ const reporter = new Reporter(options);
262
389
  const out = [];
263
390
  if (conversation.system) {
264
391
  out.push({ role: "system", content: textOf(conversation.system) });
@@ -276,7 +403,7 @@ function fromAnthropic(conversation, options = {}) {
276
403
  });
277
404
  }
278
405
  if (contentBlocks.length > 0) {
279
- out.push({ role: "user", content: userContentToOpenAI(contentBlocks) });
406
+ out.push({ role: "user", content: userContentToOpenAI(contentBlocks, reporter) });
280
407
  }
281
408
  continue;
282
409
  }
@@ -293,15 +420,24 @@ function fromAnthropic(conversation, options = {}) {
293
420
  }
294
421
  return out;
295
422
  }
296
- function userContentToOpenAI(blocks) {
297
- const hasImage = blocks.some((block) => imageFromAnthropic(block) !== null);
298
- if (!hasImage) return textOf(blocks);
423
+ function userContentToOpenAI(blocks, reporter) {
424
+ const hasMedia = blocks.some((block) => imageFromAnthropic(block) !== null || mediaFromAnthropic(block) !== null);
425
+ if (!hasMedia) return textOf(blocks);
299
426
  const parts = [];
300
427
  for (const block of blocks) {
301
428
  const image = imageFromAnthropic(block);
302
429
  if (image) {
303
430
  parts.push(imageToOpenAI(image));
304
- } else if (isRecord(block) && block.type === "text" && typeof block.text === "string") {
431
+ continue;
432
+ }
433
+ const media = mediaFromAnthropic(block);
434
+ if (media) {
435
+ const part = mediaToOpenAI(media);
436
+ if (part) parts.push(part);
437
+ else reporter.warn("dropped-content", "A document URL has no OpenAI Chat Completions equivalent; dropped.");
438
+ continue;
439
+ }
440
+ if (isRecord(block) && block.type === "text" && typeof block.text === "string") {
305
441
  parts.push({ type: "text", text: block.text });
306
442
  }
307
443
  }
@@ -368,6 +504,12 @@ function userParts(content, reporter) {
368
504
  parts.push(imageToGemini(image, reporter));
369
505
  continue;
370
506
  }
507
+ const media = mediaFromOpenAI(part);
508
+ if (media) {
509
+ const geminiPart = mediaToGemini(media, reporter);
510
+ if (geminiPart) parts.push(geminiPart);
511
+ continue;
512
+ }
371
513
  reporter.warn("dropped-content", "Dropped an unsupported user content part.");
372
514
  }
373
515
  return parts.length > 0 ? parts : [{ text: "" }];
@@ -437,7 +579,7 @@ function fromGemini(conversation, options = {}) {
437
579
  continue;
438
580
  }
439
581
  const contentParts = [];
440
- let hasImage = false;
582
+ let hasMedia = false;
441
583
  for (const part of parts) {
442
584
  if (isRecord(part) && isRecord(part.functionResponse)) {
443
585
  const fr = part.functionResponse;
@@ -448,7 +590,16 @@ function fromGemini(conversation, options = {}) {
448
590
  const image = imageFromGemini(part);
449
591
  if (image) {
450
592
  contentParts.push(imageToOpenAI(image));
451
- hasImage = true;
593
+ hasMedia = true;
594
+ continue;
595
+ }
596
+ const media = mediaFromGemini(part);
597
+ if (media) {
598
+ const openaiPart = mediaToOpenAI(media);
599
+ if (openaiPart) {
600
+ contentParts.push(openaiPart);
601
+ hasMedia = true;
602
+ }
452
603
  continue;
453
604
  }
454
605
  if (isRecord(part) && typeof part.text === "string") {
@@ -456,7 +607,7 @@ function fromGemini(conversation, options = {}) {
456
607
  }
457
608
  }
458
609
  if (contentParts.length > 0) {
459
- if (hasImage) {
610
+ if (hasMedia) {
460
611
  out.push({ role: "user", content: contentParts });
461
612
  } else {
462
613
  const text = textOf(contentParts);
@@ -1 +1 @@
1
- {"version":3,"sources":["../src/index.ts","../src/util.ts","../src/image.ts","../src/providers/openai.ts","../src/providers/anthropic.ts","../src/providers/gemini.ts","../src/convert.ts","../src/response.ts"],"sourcesContent":["export { toAnthropic, fromAnthropic } from './providers/anthropic.js';\nexport { toGemini, fromGemini } from './providers/gemini.js';\nexport { convert } from './convert.js';\nexport type { ConversationOf } from './convert.js';\nexport { parseDataUrl, toDataUrl } from './image.js';\nexport type { NormalizedImage } from './image.js';\nexport { responseFromOpenAI, responseFromAnthropic, responseFromGemini, normalizeResponse } from './response.js';\nexport type { NormalizedResponse, FinishReason, Usage } from './response.js';\n\nexport type {\n Provider,\n Warning,\n WarningCode,\n ConvertOptions,\n OpenAIMessage,\n OpenAISystemMessage,\n OpenAIUserMessage,\n OpenAIAssistantMessage,\n OpenAIToolMessage,\n OpenAIToolCall,\n OpenAIContentPart,\n OpenAITextPart,\n OpenAIImagePart,\n AnthropicConversation,\n AnthropicMessage,\n AnthropicContentBlock,\n AnthropicTextBlock,\n AnthropicToolUseBlock,\n AnthropicToolResultBlock,\n AnthropicImageBlock,\n GeminiConversation,\n GeminiContent,\n GeminiPart,\n GeminiTextPart,\n GeminiFunctionCallPart,\n GeminiFunctionResponsePart,\n GeminiInlineDataPart,\n GeminiFileDataPart,\n} from './types.js';\n","import type { ConvertOptions, OpenAITextPart, Warning, WarningCode } from './types.js';\n\n/** Thin wrapper around the optional `onWarning` callback. */\nexport class Reporter {\n constructor(private readonly options: ConvertOptions = {}) {}\n\n warn(code: WarningCode, message: string): void {\n this.options.onWarning?.({ code, message } satisfies Warning);\n }\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Flattens any supported text content (a plain string or an array of text\n * parts/blocks) into a single string. Non text parts are ignored.\n */\nexport function textOf(content: unknown): string {\n if (typeof content === 'string') return content;\n if (Array.isArray(content)) {\n return content\n .map((part) => {\n if (typeof part === 'string') return part;\n if (isRecord(part) && typeof part.text === 'string') return part.text;\n return '';\n })\n .join('');\n }\n return '';\n}\n\n/** Wraps a string as a single OpenAI/Anthropic text part array. */\nexport function textPart(text: string): OpenAITextPart[] {\n return [{ type: 'text', text }];\n}\n\n/** Safely parses a JSON string, returning a discriminated result. */\nexport function tryParseJson(input: string): { ok: true; value: unknown } | { ok: false } {\n try {\n return { ok: true, value: JSON.parse(input) };\n } catch {\n return { ok: false };\n }\n}\n\n/**\n * Parses an OpenAI tool-call `arguments` JSON string into an object. Reports and\n * returns `{}` when the string is not valid JSON object syntax.\n */\nexport function parseArguments(args: string, reporter: Reporter, fnName: string): Record<string, unknown> {\n const parsed = tryParseJson(args);\n if (parsed.ok && isRecord(parsed.value)) return parsed.value;\n reporter.warn(\n 'invalid-json-arguments',\n `Tool call '${fnName}' had arguments that were not a JSON object; used an empty object instead.`,\n );\n return {};\n}\n\n/**\n * Converts an OpenAI tool result string into a Gemini `functionResponse.response`\n * object. A JSON object string is used directly; anything else is wrapped as\n * `{ result: <text> }`, which {@link unwrapResponse} reverses.\n */\nexport function wrapResponse(content: string): Record<string, unknown> {\n const parsed = tryParseJson(content);\n if (parsed.ok && isRecord(parsed.value)) return parsed.value;\n return { result: content };\n}\n\n/** Reverses {@link wrapResponse}: a lone `{ result: string }` becomes that string. */\nexport function unwrapResponse(response: Record<string, unknown>): string {\n const keys = Object.keys(response);\n if (keys.length === 1 && keys[0] === 'result' && typeof response.result === 'string') {\n return response.result;\n }\n return JSON.stringify(response);\n}\n","import type { AnthropicImageBlock, GeminiPart, OpenAIImagePart } from './types.js';\nimport { Reporter, isRecord } from './util.js';\n\n/**\n * A provider-neutral image. `base64` carries the raw bytes plus media type\n * (mapping to a data URL, an Anthropic base64 source or a Gemini `inlineData`\n * part); `url` carries a remote reference.\n */\nexport type NormalizedImage = { kind: 'base64'; mediaType: string; data: string } | { kind: 'url'; url: string };\n\nconst DATA_URL = /^data:([^;,]+);base64,(.*)$/s;\n\n/** Decomposes a `data:<mediaType>;base64,<data>` URL. Returns null otherwise. */\nexport function parseDataUrl(url: string): { mediaType: string; data: string } | null {\n const match = DATA_URL.exec(url);\n if (!match) return null;\n return { mediaType: match[1], data: match[2] };\n}\n\n/** Reassembles a base64 data URL. The inverse of {@link parseDataUrl}. */\nexport function toDataUrl(mediaType: string, data: string): string {\n return `data:${mediaType};base64,${data}`;\n}\n\n/* ------------------------------- OpenAI -------------------------------- */\n\nexport function imageFromOpenAI(part: unknown): NormalizedImage | null {\n if (!isRecord(part) || part.type !== 'image_url' || !isRecord(part.image_url)) return null;\n const url = part.image_url.url;\n if (typeof url !== 'string') return null;\n const parsed = parseDataUrl(url);\n return parsed ? { kind: 'base64', ...parsed } : { kind: 'url', url };\n}\n\nexport function imageToOpenAI(image: NormalizedImage): OpenAIImagePart {\n const url = image.kind === 'base64' ? toDataUrl(image.mediaType, image.data) : image.url;\n return { type: 'image_url', image_url: { url } };\n}\n\n/* ------------------------------ Anthropic ------------------------------ */\n\nexport function imageFromAnthropic(block: unknown): NormalizedImage | null {\n if (!isRecord(block) || block.type !== 'image' || !isRecord(block.source)) return null;\n const source = block.source;\n if (source.type === 'base64' && typeof source.media_type === 'string' && typeof source.data === 'string') {\n return { kind: 'base64', mediaType: source.media_type, data: source.data };\n }\n if (source.type === 'url' && typeof source.url === 'string') {\n return { kind: 'url', url: source.url };\n }\n return null;\n}\n\nexport function imageToAnthropic(image: NormalizedImage): AnthropicImageBlock {\n if (image.kind === 'base64') {\n return { type: 'image', source: { type: 'base64', media_type: image.mediaType, data: image.data } };\n }\n return { type: 'image', source: { type: 'url', url: image.url } };\n}\n\n/* -------------------------------- Gemini ------------------------------- */\n\nexport function imageFromGemini(part: unknown): NormalizedImage | null {\n if (isRecord(part) && isRecord(part.inlineData)) {\n const data = part.inlineData;\n if (typeof data.mimeType === 'string' && typeof data.data === 'string') {\n return { kind: 'base64', mediaType: data.mimeType, data: data.data };\n }\n }\n if (isRecord(part) && isRecord(part.fileData)) {\n const data = part.fileData;\n if (typeof data.fileUri === 'string') return { kind: 'url', url: data.fileUri };\n }\n return null;\n}\n\nexport function imageToGemini(image: NormalizedImage, reporter: Reporter): GeminiPart {\n if (image.kind === 'base64') {\n return { inlineData: { mimeType: image.mediaType, data: image.data } };\n }\n // Gemini's fileData.fileUri is intended for Files API URIs, not arbitrary\n // public URLs. We emit it as a best effort and flag it so the caller can\n // upload via the Files API or inline the bytes if Gemini rejects it.\n reporter.warn(\n 'gemini-url-image',\n 'A remote image URL was emitted as Gemini fileData.fileUri; Gemini may require the Files API for non-Google URIs.',\n );\n return { fileData: { fileUri: image.url } };\n}\n","import type {\n OpenAIAssistantMessage,\n OpenAIMessage,\n OpenAISystemMessage,\n OpenAIToolMessage,\n OpenAIUserMessage,\n} from '../types.js';\nimport { Reporter, textOf } from '../util.js';\n\n/** Any canonical message other than a system/developer prompt. */\nexport type NonSystemMessage = OpenAIUserMessage | OpenAIAssistantMessage | OpenAIToolMessage;\n\nfunction isSystem(message: OpenAIMessage): message is OpenAISystemMessage {\n return message.role === 'system' || message.role === 'developer';\n}\n\n/**\n * Splits a canonical OpenAI conversation into its system prompt and the\n * remaining messages. All `system` / `developer` messages are folded into a\n * single string (joined by blank lines) because Anthropic and Gemini carry the\n * system prompt as one top-level field. A system message that appears after the\n * conversation has started is still folded in, but reported as `system-midstream`.\n */\nexport function splitSystem(\n messages: OpenAIMessage[],\n reporter: Reporter,\n): { system?: string; rest: NonSystemMessage[] } {\n const systemParts: string[] = [];\n const rest: NonSystemMessage[] = [];\n let started = false;\n\n for (const message of messages) {\n if (isSystem(message)) {\n if (started) {\n reporter.warn(\n 'system-midstream',\n 'A system message appeared mid conversation; it was merged into the top-level system prompt.',\n );\n }\n systemParts.push(textOf(message.content));\n continue;\n }\n started = true;\n rest.push(message);\n }\n\n const system = systemParts.length > 0 ? systemParts.join('\\n\\n') : undefined;\n return system === undefined ? { rest } : { system, rest };\n}\n","import type {\n AnthropicContentBlock,\n AnthropicConversation,\n AnthropicMessage,\n ConvertOptions,\n OpenAIAssistantMessage,\n OpenAIContentPart,\n OpenAIMessage,\n OpenAIToolMessage,\n} from '../types.js';\nimport { Reporter, isRecord, parseArguments, textOf } from '../util.js';\nimport { imageFromAnthropic, imageFromOpenAI, imageToAnthropic, imageToOpenAI } from '../image.js';\nimport { splitSystem } from './openai.js';\n\n/* ------------------------------------------------------------------ */\n/* OpenAI (canonical) -> Anthropic */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a canonical OpenAI conversation into an Anthropic request fragment\n * (`{ system, messages }`). System messages move to the top-level `system`\n * field, tool-call arguments are JSON parsed into `input` objects, tool results\n * are folded into `tool_result` blocks inside a user turn, and consecutive\n * same-role turns are merged to satisfy Anthropic's strict alternation.\n */\nexport function toAnthropic(messages: OpenAIMessage[], options: ConvertOptions = {}): AnthropicConversation {\n const reporter = new Reporter(options);\n const { system, rest } = splitSystem(messages, reporter);\n const out: AnthropicMessage[] = [];\n\n for (let i = 0; i < rest.length; i++) {\n const message = rest[i];\n\n if (message.role === 'tool') {\n const blocks: AnthropicContentBlock[] = [];\n let j = i;\n while (j < rest.length && rest[j].role === 'tool') {\n const tool = rest[j] as OpenAIToolMessage;\n blocks.push({ type: 'tool_result', tool_use_id: tool.tool_call_id, content: textOf(tool.content) });\n j++;\n }\n out.push({ role: 'user', content: blocks });\n i = j - 1;\n continue;\n }\n\n if (message.role === 'user') {\n out.push({ role: 'user', content: userContent(message.content, reporter) });\n continue;\n }\n\n out.push({ role: 'assistant', content: assistantContent(message, reporter) });\n }\n\n const merged = mergeConsecutive(out, reporter);\n return system === undefined ? { messages: merged } : { system, messages: merged };\n}\n\nfunction userContent(content: string | OpenAIContentPart[], reporter: Reporter): string | AnthropicContentBlock[] {\n if (typeof content === 'string') return content;\n if (!Array.isArray(content)) return textOf(content);\n const blocks: AnthropicContentBlock[] = [];\n for (const part of content) {\n if (isRecord(part) && part.type === 'text' && typeof part.text === 'string') {\n blocks.push({ type: 'text', text: part.text });\n continue;\n }\n const image = imageFromOpenAI(part);\n if (image) {\n blocks.push(imageToAnthropic(image));\n continue;\n }\n reporter.warn('dropped-content', 'Dropped an unsupported user content part.');\n }\n return blocks;\n}\n\nfunction assistantContent(message: OpenAIAssistantMessage, reporter: Reporter): string | AnthropicContentBlock[] {\n const text = textOf(message.content ?? '');\n const toolCalls = message.tool_calls ?? [];\n if (toolCalls.length === 0) return text;\n\n const blocks: AnthropicContentBlock[] = [];\n if (text) blocks.push({ type: 'text', text });\n for (const call of toolCalls) {\n blocks.push({\n type: 'tool_use',\n id: call.id,\n name: call.function.name,\n input: parseArguments(call.function.arguments, reporter, call.function.name),\n });\n }\n return blocks;\n}\n\n/** Merges adjacent same-role messages by concatenating their content blocks. */\nfunction mergeConsecutive(messages: AnthropicMessage[], reporter: Reporter): AnthropicMessage[] {\n const result: AnthropicMessage[] = [];\n for (const message of messages) {\n const previous = result[result.length - 1];\n if (previous && previous.role === message.role) {\n previous.content = [...asBlocks(previous.content), ...asBlocks(message.content)];\n reporter.warn(\n 'merged-role',\n `Merged consecutive '${message.role}' turns (Anthropic requires alternating roles).`,\n );\n } else {\n result.push({ role: message.role, content: message.content });\n }\n }\n return result;\n}\n\nfunction asBlocks(content: string | AnthropicContentBlock[]): AnthropicContentBlock[] {\n if (typeof content !== 'string') return content;\n return content ? [{ type: 'text', text: content }] : [];\n}\n\n/* ------------------------------------------------------------------ */\n/* Anthropic -> OpenAI (canonical) */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts an Anthropic request fragment back into a canonical OpenAI message\n * array. The top-level `system` becomes a leading system message, `tool_use`\n * blocks become `tool_calls` (with `input` re-serialized to a JSON string), and\n * `tool_result` blocks become standalone `role: 'tool'` messages.\n */\nexport function fromAnthropic(conversation: AnthropicConversation, options: ConvertOptions = {}): OpenAIMessage[] {\n void options;\n const out: OpenAIMessage[] = [];\n if (conversation.system) {\n out.push({ role: 'system', content: textOf(conversation.system) });\n }\n\n for (const message of conversation.messages) {\n const blocks = asBlocks(message.content);\n\n if (message.role === 'user') {\n const toolResults = blocks.filter((b) => b.type === 'tool_result');\n const contentBlocks = blocks.filter((b) => b.type !== 'tool_result');\n for (const block of toolResults) {\n out.push({\n role: 'tool',\n tool_call_id: String((block as { tool_use_id?: string }).tool_use_id ?? ''),\n content: textOf((block as { content?: unknown }).content),\n });\n }\n if (contentBlocks.length > 0) {\n out.push({ role: 'user', content: userContentToOpenAI(contentBlocks) });\n }\n continue;\n }\n\n // assistant\n const text = textOf(blocks.filter((b) => b.type === 'text'));\n const toolUses = blocks.filter((b) => b.type === 'tool_use');\n const assistant: OpenAIAssistantMessage = { role: 'assistant', content: text || null };\n if (toolUses.length > 0) {\n assistant.tool_calls = toolUses.map((block) => {\n const b = block as { id: string; name: string; input: Record<string, unknown> };\n return { id: b.id, type: 'function', function: { name: b.name, arguments: JSON.stringify(b.input ?? {}) } };\n });\n }\n out.push(assistant);\n }\n\n return out;\n}\n\n/** Rebuilds OpenAI user content from Anthropic blocks, as a string unless an image is present. */\nfunction userContentToOpenAI(blocks: AnthropicContentBlock[]): string | OpenAIContentPart[] {\n const hasImage = blocks.some((block) => imageFromAnthropic(block) !== null);\n if (!hasImage) return textOf(blocks);\n const parts: OpenAIContentPart[] = [];\n for (const block of blocks) {\n const image = imageFromAnthropic(block);\n if (image) {\n parts.push(imageToOpenAI(image));\n } else if (isRecord(block) && block.type === 'text' && typeof block.text === 'string') {\n parts.push({ type: 'text', text: block.text });\n }\n }\n return parts;\n}\n","import type {\n ConvertOptions,\n GeminiContent,\n GeminiConversation,\n GeminiPart,\n OpenAIAssistantMessage,\n OpenAIContentPart,\n OpenAIMessage,\n OpenAIToolCall,\n OpenAIToolMessage,\n} from '../types.js';\nimport { Reporter, isRecord, parseArguments, textOf, unwrapResponse, wrapResponse } from '../util.js';\nimport { imageFromGemini, imageFromOpenAI, imageToGemini, imageToOpenAI } from '../image.js';\nimport { splitSystem } from './openai.js';\n\n/* ------------------------------------------------------------------ */\n/* OpenAI (canonical) -> Gemini */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a canonical OpenAI conversation into a Gemini request fragment\n * (`{ systemInstruction, contents }`). The assistant role becomes `model`,\n * tool-call arguments are JSON parsed into `args` objects, tool results become\n * `functionResponse` parts whose `name` is recovered from the matching call, and\n * consecutive same-role turns are merged for Gemini's strict alternation.\n *\n * The OpenAI tool-call `id` is carried through as `functionCall.id` so that a\n * round trip (OpenAI -> Gemini -> OpenAI) preserves ids exactly.\n */\nexport function toGemini(messages: OpenAIMessage[], options: ConvertOptions = {}): GeminiConversation {\n const reporter = new Reporter(options);\n const { system, rest } = splitSystem(messages, reporter);\n\n const idToName = new Map<string, string>();\n for (const message of rest) {\n if (message.role === 'assistant' && message.tool_calls) {\n for (const call of message.tool_calls) idToName.set(call.id, call.function.name);\n }\n }\n\n const contents: GeminiContent[] = [];\n for (let i = 0; i < rest.length; i++) {\n const message = rest[i];\n\n if (message.role === 'tool') {\n const parts: GeminiPart[] = [];\n let j = i;\n while (j < rest.length && rest[j].role === 'tool') {\n const tool = rest[j] as OpenAIToolMessage;\n const name = idToName.get(tool.tool_call_id);\n if (!name) {\n reporter.warn(\n 'unmapped-tool-result',\n `Tool result '${tool.tool_call_id}' has no matching call; used the id as the function name.`,\n );\n }\n parts.push({\n functionResponse: {\n id: tool.tool_call_id,\n name: name ?? tool.tool_call_id,\n response: wrapResponse(textOf(tool.content)),\n },\n });\n j++;\n }\n contents.push({ role: 'user', parts });\n i = j - 1;\n continue;\n }\n\n if (message.role === 'user') {\n contents.push({ role: 'user', parts: userParts(message.content, reporter) });\n continue;\n }\n\n contents.push({ role: 'model', parts: assistantParts(message, reporter) });\n }\n\n const merged = mergeConsecutive(contents, reporter);\n return system === undefined\n ? { contents: merged }\n : { systemInstruction: { parts: [{ text: system }] }, contents: merged };\n}\n\nfunction userParts(content: string | OpenAIContentPart[], reporter: Reporter): GeminiPart[] {\n if (typeof content === 'string') return [{ text: content }];\n const parts: GeminiPart[] = [];\n for (const part of content) {\n if (isRecord(part) && part.type === 'text' && typeof part.text === 'string') {\n parts.push({ text: part.text });\n continue;\n }\n const image = imageFromOpenAI(part);\n if (image) {\n parts.push(imageToGemini(image, reporter));\n continue;\n }\n reporter.warn('dropped-content', 'Dropped an unsupported user content part.');\n }\n return parts.length > 0 ? parts : [{ text: '' }];\n}\n\nfunction assistantParts(message: OpenAIAssistantMessage, reporter: Reporter): GeminiPart[] {\n const parts: GeminiPart[] = [];\n const text = textOf(message.content ?? '');\n if (text) parts.push({ text });\n for (const call of message.tool_calls ?? []) {\n parts.push({\n functionCall: {\n id: call.id,\n name: call.function.name,\n args: parseArguments(call.function.arguments, reporter, call.function.name),\n },\n });\n }\n return parts.length > 0 ? parts : [{ text: '' }];\n}\n\n/** Merges adjacent same-role contents by concatenating their `parts` arrays. */\nfunction mergeConsecutive(contents: GeminiContent[], reporter: Reporter): GeminiContent[] {\n const result: GeminiContent[] = [];\n for (const content of contents) {\n const previous = result[result.length - 1];\n if (previous && previous.role === content.role) {\n previous.parts = [...previous.parts, ...content.parts];\n reporter.warn('merged-role', `Merged consecutive '${content.role}' turns (Gemini requires alternating roles).`);\n } else {\n result.push({ role: content.role, parts: [...content.parts] });\n }\n }\n return result;\n}\n\n/* ------------------------------------------------------------------ */\n/* Gemini -> OpenAI (canonical) */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a Gemini request fragment back into a canonical OpenAI message array.\n * Because Gemini matches tool calls and responses by function name (the `id`\n * field is optional), this maintains a queue of pending calls and resolves each\n * `functionResponse` by id when present, otherwise by name in call order,\n * generating a deterministic id as a last resort.\n */\nexport function fromGemini(conversation: GeminiConversation, options: ConvertOptions = {}): OpenAIMessage[] {\n const reporter = new Reporter(options);\n const out: OpenAIMessage[] = [];\n\n if (conversation.systemInstruction) {\n const text = textOf(conversation.systemInstruction.parts);\n if (text) out.push({ role: 'system', content: text });\n }\n\n const pending: { id: string; name: string }[] = [];\n let counter = 0;\n const generateId = (name: string): string => `call_${name.replace(/[^a-zA-Z0-9_-]/g, '_')}_${counter++}`;\n\n for (const content of conversation.contents) {\n const parts = Array.isArray(content.parts) ? content.parts : [];\n\n if (content.role === 'model') {\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n for (const part of parts) {\n if (isRecord(part) && isRecord(part.functionCall)) {\n const fc = part.functionCall as { id?: string; name: string; args?: Record<string, unknown> };\n const id = fc.id ?? generateId(fc.name);\n if (!fc.id) reporter.warn('generated-id', `Gemini functionCall '${fc.name}' had no id; generated '${id}'.`);\n toolCalls.push({\n id,\n type: 'function',\n function: { name: fc.name, arguments: JSON.stringify(fc.args ?? {}) },\n });\n pending.push({ id, name: fc.name });\n } else if (isRecord(part) && typeof part.text === 'string') {\n textPieces.push(part.text);\n }\n }\n const text = textPieces.join('');\n const assistant: OpenAIAssistantMessage = { role: 'assistant', content: text || null };\n if (toolCalls.length > 0) assistant.tool_calls = toolCalls;\n out.push(assistant);\n continue;\n }\n\n // role 'user' or unspecified\n const contentParts: OpenAIContentPart[] = [];\n let hasImage = false;\n for (const part of parts) {\n if (isRecord(part) && isRecord(part.functionResponse)) {\n const fr = part.functionResponse as { id?: string; name: string; response?: Record<string, unknown> };\n const id = resolveResponseId(fr, pending, reporter, generateId);\n out.push({ role: 'tool', tool_call_id: id, content: unwrapResponse(fr.response ?? {}) });\n continue;\n }\n const image = imageFromGemini(part);\n if (image) {\n contentParts.push(imageToOpenAI(image));\n hasImage = true;\n continue;\n }\n if (isRecord(part) && typeof part.text === 'string') {\n contentParts.push({ type: 'text', text: part.text });\n }\n }\n if (contentParts.length > 0) {\n if (hasImage) {\n out.push({ role: 'user', content: contentParts });\n } else {\n const text = textOf(contentParts);\n if (text) out.push({ role: 'user', content: text });\n }\n }\n }\n\n return out;\n}\n\nfunction resolveResponseId(\n response: { id?: string; name: string },\n pending: { id: string; name: string }[],\n reporter: Reporter,\n generateId: (name: string) => string,\n): string {\n if (response.id) {\n const index = pending.findIndex((p) => p.id === response.id);\n if (index >= 0) pending.splice(index, 1);\n return response.id;\n }\n const index = pending.findIndex((p) => p.name === response.name);\n if (index >= 0) {\n const { id } = pending[index];\n pending.splice(index, 1);\n return id;\n }\n const id = generateId(response.name);\n reporter.warn(\n 'unmapped-tool-result',\n `Gemini functionResponse for '${response.name}' had no matching call; generated '${id}'.`,\n );\n return id;\n}\n","import type { AnthropicConversation, ConvertOptions, GeminiConversation, OpenAIMessage, Provider } from './types.js';\nimport { fromAnthropic, toAnthropic } from './providers/anthropic.js';\nimport { fromGemini, toGemini } from './providers/gemini.js';\n\n/** Maps a provider to the conversation shape it accepts and returns. */\nexport type ConversationOf<P extends Provider> = P extends 'openai'\n ? OpenAIMessage[]\n : P extends 'anthropic'\n ? AnthropicConversation\n : P extends 'gemini'\n ? GeminiConversation\n : never;\n\n/**\n * Converts a conversation from one provider format to another. Every conversion\n * routes through the canonical OpenAI representation, so any source/target pair\n * is supported, including same-provider normalization.\n *\n * @example\n * const gemini = convert(openaiMessages, { from: 'openai', to: 'gemini' });\n * const openai = convert(anthropicBody, { from: 'anthropic', to: 'openai' });\n */\nexport function convert<From extends Provider, To extends Provider>(\n conversation: ConversationOf<From>,\n route: { from: From; to: To },\n options: ConvertOptions = {},\n): ConversationOf<To> {\n const canonical = toCanonical(conversation, route.from, options);\n return fromCanonical(canonical, route.to, options) as ConversationOf<To>;\n}\n\nfunction toCanonical(conversation: unknown, from: Provider, options: ConvertOptions): OpenAIMessage[] {\n switch (from) {\n case 'openai':\n return conversation as OpenAIMessage[];\n case 'anthropic':\n return fromAnthropic(conversation as AnthropicConversation, options);\n case 'gemini':\n return fromGemini(conversation as GeminiConversation, options);\n default:\n throw new Error(`Unknown source provider: ${String(from)}`);\n }\n}\n\nfunction fromCanonical(canonical: OpenAIMessage[], to: Provider, options: ConvertOptions): ConversationOf<Provider> {\n switch (to) {\n case 'openai':\n return canonical;\n case 'anthropic':\n return toAnthropic(canonical, options);\n case 'gemini':\n return toGemini(canonical, options);\n default:\n throw new Error(`Unknown target provider: ${String(to)}`);\n }\n}\n","import type { ConvertOptions, OpenAIAssistantMessage, OpenAIToolCall, Provider } from './types.js';\nimport { Reporter, isRecord, textOf } from './util.js';\n\n/** A provider-neutral finish reason. */\nexport type FinishReason = 'stop' | 'tool_calls' | 'length' | 'content_filter' | 'unknown';\n\n/** Provider-neutral token usage. */\nexport interface Usage {\n inputTokens: number;\n outputTokens: number;\n}\n\n/** A provider response normalized to the canonical OpenAI assistant shape. */\nexport interface NormalizedResponse {\n message: OpenAIAssistantMessage;\n finishReason: FinishReason;\n usage: Usage;\n}\n\nconst num = (value: unknown): number => (typeof value === 'number' ? value : 0);\n\n/** Builds the canonical message, setting `content` to null when only tool calls are present. */\nfunction buildMessage(text: string, toolCalls: OpenAIToolCall[]): OpenAIAssistantMessage {\n const message: OpenAIAssistantMessage = { role: 'assistant', content: text ? text : null };\n if (toolCalls.length > 0) message.tool_calls = toolCalls;\n return message;\n}\n\n/** When tool calls are present the canonical finish reason is always `tool_calls`. */\nfunction finalReason(mapped: FinishReason, toolCalls: OpenAIToolCall[]): FinishReason {\n return toolCalls.length > 0 ? 'tool_calls' : mapped;\n}\n\n/* -------------------------------- OpenAI ------------------------------- */\n\nconst OPENAI_FINISH: Record<string, FinishReason> = {\n stop: 'stop',\n length: 'length',\n tool_calls: 'tool_calls',\n content_filter: 'content_filter',\n function_call: 'tool_calls',\n};\n\n/** Normalizes an OpenAI Chat Completions response body. */\nexport function responseFromOpenAI(body: unknown): NormalizedResponse {\n const root = isRecord(body) ? body : {};\n const choice = Array.isArray(root.choices) && isRecord(root.choices[0]) ? root.choices[0] : {};\n const message = isRecord(choice.message) ? choice.message : {};\n\n const text = typeof message.content === 'string' ? message.content : textOf(message.content);\n const toolCalls = Array.isArray(message.tool_calls) ? (message.tool_calls as OpenAIToolCall[]) : [];\n\n const usage = isRecord(root.usage) ? root.usage : {};\n return {\n message: buildMessage(text, toolCalls),\n finishReason: finalReason(OPENAI_FINISH[String(choice.finish_reason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.prompt_tokens), outputTokens: num(usage.completion_tokens) },\n };\n}\n\n/* ------------------------------- Anthropic ----------------------------- */\n\nconst ANTHROPIC_FINISH: Record<string, FinishReason> = {\n end_turn: 'stop',\n stop_sequence: 'stop',\n tool_use: 'tool_calls',\n max_tokens: 'length',\n refusal: 'content_filter',\n pause_turn: 'unknown',\n};\n\n/** Normalizes an Anthropic Messages response body. */\nexport function responseFromAnthropic(body: unknown): NormalizedResponse {\n const root = isRecord(body) ? body : {};\n const blocks = Array.isArray(root.content) ? root.content : [];\n\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n for (const block of blocks) {\n if (!isRecord(block)) continue;\n if (block.type === 'text' && typeof block.text === 'string') {\n textPieces.push(block.text);\n } else if (block.type === 'tool_use' && typeof block.name === 'string') {\n toolCalls.push({\n id: typeof block.id === 'string' ? block.id : '',\n type: 'function',\n function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },\n });\n }\n }\n\n const usage = isRecord(root.usage) ? root.usage : {};\n return {\n message: buildMessage(textPieces.join(''), toolCalls),\n finishReason: finalReason(ANTHROPIC_FINISH[String(root.stop_reason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.input_tokens), outputTokens: num(usage.output_tokens) },\n };\n}\n\n/* -------------------------------- Gemini ------------------------------- */\n\nconst GEMINI_FINISH: Record<string, FinishReason> = {\n STOP: 'stop',\n MAX_TOKENS: 'length',\n SAFETY: 'content_filter',\n RECITATION: 'content_filter',\n MALFORMED_FUNCTION_CALL: 'content_filter',\n};\n\n/** Normalizes a Gemini generateContent response body. */\nexport function responseFromGemini(body: unknown, options: ConvertOptions = {}): NormalizedResponse {\n const reporter = new Reporter(options);\n const root = isRecord(body) ? body : {};\n const candidate = Array.isArray(root.candidates) && isRecord(root.candidates[0]) ? root.candidates[0] : {};\n const content = isRecord(candidate.content) ? candidate.content : {};\n const parts = Array.isArray(content.parts) ? content.parts : [];\n\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n let counter = 0;\n for (const part of parts) {\n if (!isRecord(part)) continue;\n if (isRecord(part.functionCall)) {\n const call = part.functionCall as { id?: string; name?: string; args?: Record<string, unknown> };\n const name = typeof call.name === 'string' ? call.name : 'function';\n let id = call.id;\n if (!id) {\n id = `call_${name.replace(/[^a-zA-Z0-9_-]/g, '_')}_${counter++}`;\n reporter.warn('generated-id', `Gemini functionCall '${name}' had no id; generated '${id}'.`);\n }\n toolCalls.push({ id, type: 'function', function: { name, arguments: JSON.stringify(call.args ?? {}) } });\n } else if (typeof part.text === 'string') {\n textPieces.push(part.text);\n }\n }\n\n const usage = isRecord(root.usageMetadata) ? root.usageMetadata : {};\n return {\n message: buildMessage(textPieces.join(''), toolCalls),\n finishReason: finalReason(GEMINI_FINISH[String(candidate.finishReason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.promptTokenCount), outputTokens: num(usage.candidatesTokenCount) },\n };\n}\n\n/* ------------------------------ Dispatcher ----------------------------- */\n\n/** Normalizes a provider response body into the canonical shape. */\nexport function normalizeResponse(\n body: unknown,\n route: { from: Provider },\n options: ConvertOptions = {},\n): NormalizedResponse {\n switch (route.from) {\n case 'openai':\n return responseFromOpenAI(body);\n case 'anthropic':\n return responseFromAnthropic(body);\n case 'gemini':\n return responseFromGemini(body, options);\n default:\n throw new Error(`Unknown source provider: ${String(route.from)}`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;;;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;AAAA;;;ACGO,IAAM,WAAN,MAAe;AAAA,EACpB,YAA6B,UAA0B,CAAC,GAAG;AAA9B;AAAA,EAA+B;AAAA,EAA/B;AAAA,EAE7B,KAAK,MAAmB,SAAuB;AAC7C,SAAK,QAAQ,YAAY,EAAE,MAAM,QAAQ,CAAmB;AAAA,EAC9D;AACF;AAEO,SAAS,SAAS,OAAkD;AACzE,SAAO,OAAO,UAAU,YAAY,UAAU,QAAQ,CAAC,MAAM,QAAQ,KAAK;AAC5E;AAMO,SAAS,OAAO,SAA0B;AAC/C,MAAI,OAAO,YAAY,SAAU,QAAO;AACxC,MAAI,MAAM,QAAQ,OAAO,GAAG;AAC1B,WAAO,QACJ,IAAI,CAAC,SAAS;AACb,UAAI,OAAO,SAAS,SAAU,QAAO;AACrC,UAAI,SAAS,IAAI,KAAK,OAAO,KAAK,SAAS,SAAU,QAAO,KAAK;AACjE,aAAO;AAAA,IACT,CAAC,EACA,KAAK,EAAE;AAAA,EACZ;AACA,SAAO;AACT;AAQO,SAAS,aAAa,OAA6D;AACxF,MAAI;AACF,WAAO,EAAE,IAAI,MAAM,OAAO,KAAK,MAAM,KAAK,EAAE;AAAA,EAC9C,QAAQ;AACN,WAAO,EAAE,IAAI,MAAM;AAAA,EACrB;AACF;AAMO,SAAS,eAAe,MAAc,UAAoB,QAAyC;AACx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ACA,UAA0B,CAAC,GACP;AACpB,UAAQ,MAAM,MAAM;AAAA,IAClB,KAAK;AACH,aAAO,mBAAmB,IAAI;AAAA,IAChC,KAAK;AACH,aAAO,sBAAsB,IAAI;AAAA,IACnC,KAAK;AACH,aAAO,mBAAmB,MAAM,OAAO;AAAA,IACzC;AACE,YAAM,IAAI,MAAM,4BAA4B,OAAO,MAAM,IAAI,CAAC,EAAE;AAAA,EACpE;AACF;","names":["mergeConsecutive","index","id"]}
1
+ {"version":3,"sources":["../src/index.ts","../src/util.ts","../src/image.ts","../src/media.ts","../src/providers/openai.ts","../src/providers/anthropic.ts","../src/providers/gemini.ts","../src/convert.ts","../src/response.ts"],"sourcesContent":["export { toAnthropic, fromAnthropic } from './providers/anthropic.js';\nexport { toGemini, fromGemini } from './providers/gemini.js';\nexport { convert } from './convert.js';\nexport type { ConversationOf } from './convert.js';\nexport { parseDataUrl, toDataUrl } from './image.js';\nexport type { NormalizedImage } from './image.js';\nexport type { MediaPart, MediaModality, MediaSource } from './media.js';\nexport { responseFromOpenAI, responseFromAnthropic, responseFromGemini, normalizeResponse } from './response.js';\nexport type { NormalizedResponse, FinishReason, Usage } from './response.js';\n\nexport type {\n Provider,\n Warning,\n WarningCode,\n ConvertOptions,\n OpenAIMessage,\n OpenAISystemMessage,\n OpenAIUserMessage,\n OpenAIAssistantMessage,\n OpenAIToolMessage,\n OpenAIToolCall,\n OpenAIContentPart,\n OpenAITextPart,\n OpenAIImagePart,\n OpenAIAudioPart,\n OpenAIFilePart,\n AnthropicConversation,\n AnthropicMessage,\n AnthropicContentBlock,\n AnthropicTextBlock,\n AnthropicToolUseBlock,\n AnthropicToolResultBlock,\n AnthropicImageBlock,\n AnthropicDocumentBlock,\n GeminiConversation,\n GeminiContent,\n GeminiPart,\n GeminiTextPart,\n GeminiFunctionCallPart,\n GeminiFunctionResponsePart,\n GeminiInlineDataPart,\n GeminiFileDataPart,\n} from './types.js';\n","import type { ConvertOptions, OpenAITextPart, Warning, WarningCode } from './types.js';\n\n/** Thin wrapper around the optional `onWarning` callback. */\nexport class Reporter {\n constructor(private readonly options: ConvertOptions = {}) {}\n\n warn(code: WarningCode, message: string): void {\n this.options.onWarning?.({ code, message } satisfies Warning);\n }\n}\n\nexport function isRecord(value: unknown): value is Record<string, unknown> {\n return typeof value === 'object' && value !== null && !Array.isArray(value);\n}\n\n/**\n * Flattens any supported text content (a plain string or an array of text\n * parts/blocks) into a single string. Non text parts are ignored.\n */\nexport function textOf(content: unknown): string {\n if (typeof content === 'string') return content;\n if (Array.isArray(content)) {\n return content\n .map((part) => {\n if (typeof part === 'string') return part;\n if (isRecord(part) && typeof part.text === 'string') return part.text;\n return '';\n })\n .join('');\n }\n return '';\n}\n\n/** Wraps a string as a single OpenAI/Anthropic text part array. */\nexport function textPart(text: string): OpenAITextPart[] {\n return [{ type: 'text', text }];\n}\n\n/** Safely parses a JSON string, returning a discriminated result. */\nexport function tryParseJson(input: string): { ok: true; value: unknown } | { ok: false } {\n try {\n return { ok: true, value: JSON.parse(input) };\n } catch {\n return { ok: false };\n }\n}\n\n/**\n * Parses an OpenAI tool-call `arguments` JSON string into an object. Reports and\n * returns `{}` when the string is not valid JSON object syntax.\n */\nexport function parseArguments(args: string, reporter: Reporter, fnName: string): Record<string, unknown> {\n const parsed = tryParseJson(args);\n if (parsed.ok && isRecord(parsed.value)) return parsed.value;\n reporter.warn(\n 'invalid-json-arguments',\n `Tool call '${fnName}' had arguments that were not a JSON object; used an empty object instead.`,\n );\n return {};\n}\n\n/**\n * Converts an OpenAI tool result string into a Gemini `functionResponse.response`\n * object. A JSON object string is used directly; anything else is wrapped as\n * `{ result: <text> }`, which {@link unwrapResponse} reverses.\n */\nexport function wrapResponse(content: string): Record<string, unknown> {\n const parsed = tryParseJson(content);\n if (parsed.ok && isRecord(parsed.value)) return parsed.value;\n return { result: content };\n}\n\n/** Reverses {@link wrapResponse}: a lone `{ result: string }` becomes that string. */\nexport function unwrapResponse(response: Record<string, unknown>): string {\n const keys = Object.keys(response);\n if (keys.length === 1 && keys[0] === 'result' && typeof response.result === 'string') {\n return response.result;\n }\n return JSON.stringify(response);\n}\n","import type { AnthropicImageBlock, GeminiPart, OpenAIImagePart } from './types.js';\nimport { Reporter, isRecord } from './util.js';\n\n/**\n * A provider-neutral image. `base64` carries the raw bytes plus media type\n * (mapping to a data URL, an Anthropic base64 source or a Gemini `inlineData`\n * part); `url` carries a remote reference.\n */\nexport type NormalizedImage = { kind: 'base64'; mediaType: string; data: string } | { kind: 'url'; url: string };\n\nconst DATA_URL = /^data:([^;,]+);base64,(.*)$/s;\n\n/** Decomposes a `data:<mediaType>;base64,<data>` URL. Returns null otherwise. */\nexport function parseDataUrl(url: string): { mediaType: string; data: string } | null {\n const match = DATA_URL.exec(url);\n if (!match) return null;\n return { mediaType: match[1], data: match[2] };\n}\n\n/** Reassembles a base64 data URL. The inverse of {@link parseDataUrl}. */\nexport function toDataUrl(mediaType: string, data: string): string {\n return `data:${mediaType};base64,${data}`;\n}\n\n/* ------------------------------- OpenAI -------------------------------- */\n\nexport function imageFromOpenAI(part: unknown): NormalizedImage | null {\n if (!isRecord(part) || part.type !== 'image_url' || !isRecord(part.image_url)) return null;\n const url = part.image_url.url;\n if (typeof url !== 'string') return null;\n const parsed = parseDataUrl(url);\n return parsed ? { kind: 'base64', ...parsed } : { kind: 'url', url };\n}\n\nexport function imageToOpenAI(image: NormalizedImage): OpenAIImagePart {\n const url = image.kind === 'base64' ? toDataUrl(image.mediaType, image.data) : image.url;\n return { type: 'image_url', image_url: { url } };\n}\n\n/* ------------------------------ Anthropic ------------------------------ */\n\nexport function imageFromAnthropic(block: unknown): NormalizedImage | null {\n if (!isRecord(block) || block.type !== 'image' || !isRecord(block.source)) return null;\n const source = block.source;\n if (source.type === 'base64' && typeof source.media_type === 'string' && typeof source.data === 'string') {\n return { kind: 'base64', mediaType: source.media_type, data: source.data };\n }\n if (source.type === 'url' && typeof source.url === 'string') {\n return { kind: 'url', url: source.url };\n }\n return null;\n}\n\nexport function imageToAnthropic(image: NormalizedImage): AnthropicImageBlock {\n if (image.kind === 'base64') {\n return { type: 'image', source: { type: 'base64', media_type: image.mediaType, data: image.data } };\n }\n return { type: 'image', source: { type: 'url', url: image.url } };\n}\n\n/* -------------------------------- Gemini ------------------------------- */\n\nexport function imageFromGemini(part: unknown): NormalizedImage | null {\n if (isRecord(part) && isRecord(part.inlineData)) {\n const data = part.inlineData;\n if (typeof data.mimeType === 'string' && typeof data.data === 'string' && data.mimeType.startsWith('image/')) {\n return { kind: 'base64', mediaType: data.mimeType, data: data.data };\n }\n }\n if (isRecord(part) && isRecord(part.fileData)) {\n const data = part.fileData;\n // A fileData with no mimeType is assumed to be an image (the only media we\n // emit as a bare fileUri); a typed non-image fileData belongs to media.ts.\n const mime = typeof data.mimeType === 'string' ? data.mimeType : '';\n if (typeof data.fileUri === 'string' && (mime === '' || mime.startsWith('image/'))) {\n return { kind: 'url', url: data.fileUri };\n }\n }\n return null;\n}\n\nexport function imageToGemini(image: NormalizedImage, reporter: Reporter): GeminiPart {\n if (image.kind === 'base64') {\n return { inlineData: { mimeType: image.mediaType, data: image.data } };\n }\n // Gemini's fileData.fileUri is intended for Files API URIs, not arbitrary\n // public URLs. We emit it as a best effort and flag it so the caller can\n // upload via the Files API or inline the bytes if Gemini rejects it.\n reporter.warn(\n 'gemini-url-image',\n 'A remote image URL was emitted as Gemini fileData.fileUri; Gemini may require the Files API for non-Google URIs.',\n );\n return { fileData: { fileUri: image.url } };\n}\n","import type {\n AnthropicDocumentBlock,\n GeminiPart,\n OpenAIAudioPart,\n OpenAIContentPart,\n OpenAIFilePart,\n} from './types.js';\nimport { Reporter, isRecord } from './util.js';\nimport { parseDataUrl, toDataUrl } from './image.js';\n\n/**\n * A provider-neutral non-image media part (audio or document). Images keep their\n * own dedicated path in `image.ts`; everything else flows through here.\n */\nexport type MediaModality = 'audio' | 'document';\n\nexport type MediaSource =\n | { kind: 'base64'; mediaType: string; data: string }\n | { kind: 'url'; url: string }\n | { kind: 'file_id'; id: string };\n\nexport interface MediaPart {\n modality: MediaModality;\n source: MediaSource;\n filename?: string;\n}\n\nfunction modalityFromMime(mediaType: string): MediaModality {\n return mediaType.startsWith('audio/') ? 'audio' : 'document';\n}\n\n/* -------------------------------- OpenAI ------------------------------- */\n\nexport function mediaFromOpenAI(part: unknown): MediaPart | null {\n if (!isRecord(part)) return null;\n\n if (part.type === 'input_audio' && isRecord(part.input_audio)) {\n const audio = part.input_audio;\n if (typeof audio.data === 'string') {\n const format = typeof audio.format === 'string' ? audio.format : 'wav';\n return { modality: 'audio', source: { kind: 'base64', mediaType: `audio/${format}`, data: audio.data } };\n }\n }\n\n if (part.type === 'file' && isRecord(part.file)) {\n const file = part.file;\n const filename = typeof file.filename === 'string' ? file.filename : undefined;\n if (typeof file.file_data === 'string') {\n const parsed = parseDataUrl(file.file_data);\n if (parsed) return { modality: 'document', source: { kind: 'base64', ...parsed }, filename };\n }\n if (typeof file.file_id === 'string') {\n return { modality: 'document', source: { kind: 'file_id', id: file.file_id }, filename };\n }\n }\n\n return null;\n}\n\nexport function mediaToOpenAI(media: MediaPart): OpenAIContentPart | null {\n const { modality, source } = media;\n if (modality === 'audio') {\n if (source.kind !== 'base64') return null; // Chat Completions has no URL/id audio input\n const audio: OpenAIAudioPart = {\n type: 'input_audio',\n input_audio: { data: source.data, format: source.mediaType.replace(/^audio\\//, '') },\n };\n return audio;\n }\n // document\n if (source.kind === 'base64') {\n const file: OpenAIFilePart = {\n type: 'file',\n file: {\n file_data: toDataUrl(source.mediaType, source.data),\n ...(media.filename ? { filename: media.filename } : {}),\n },\n };\n return file;\n }\n if (source.kind === 'file_id') {\n const file: OpenAIFilePart = {\n type: 'file',\n file: { file_id: source.id, ...(media.filename ? { filename: media.filename } : {}) },\n };\n return file;\n }\n return null; // Chat Completions has no URL document input\n}\n\n/* ------------------------------- Anthropic ----------------------------- */\n\nexport function mediaFromAnthropic(block: unknown): MediaPart | null {\n if (!isRecord(block) || block.type !== 'document' || !isRecord(block.source)) return null;\n const source = block.source;\n if (source.type === 'base64' && typeof source.media_type === 'string' && typeof source.data === 'string') {\n return { modality: 'document', source: { kind: 'base64', mediaType: source.media_type, data: source.data } };\n }\n if (source.type === 'url' && typeof source.url === 'string') {\n return { modality: 'document', source: { kind: 'url', url: source.url } };\n }\n if (source.type === 'file' && typeof source.file_id === 'string') {\n return { modality: 'document', source: { kind: 'file_id', id: source.file_id } };\n }\n return null;\n}\n\nexport function mediaToAnthropic(media: MediaPart, reporter: Reporter): AnthropicDocumentBlock | null {\n if (media.modality === 'audio') {\n reporter.warn('unsupported-modality', 'Anthropic has no audio input; dropped an audio part.');\n return null;\n }\n const { source } = media;\n if (source.kind === 'base64') {\n return { type: 'document', source: { type: 'base64', media_type: source.mediaType, data: source.data } };\n }\n if (source.kind === 'url') {\n return { type: 'document', source: { type: 'url', url: source.url } };\n }\n return { type: 'document', source: { type: 'file', file_id: source.id } };\n}\n\n/* -------------------------------- Gemini ------------------------------- */\n\nexport function mediaFromGemini(part: unknown): MediaPart | null {\n if (isRecord(part) && isRecord(part.inlineData)) {\n const data = part.inlineData;\n if (typeof data.mimeType === 'string' && typeof data.data === 'string' && !data.mimeType.startsWith('image/')) {\n return {\n modality: modalityFromMime(data.mimeType),\n source: { kind: 'base64', mediaType: data.mimeType, data: data.data },\n };\n }\n }\n if (isRecord(part) && isRecord(part.fileData)) {\n const data = part.fileData;\n const mime = typeof data.mimeType === 'string' ? data.mimeType : '';\n // A bare fileUri (no mimeType) is treated as an image in image.ts.\n if (typeof data.fileUri === 'string' && mime !== '' && !mime.startsWith('image/')) {\n return { modality: modalityFromMime(mime), source: { kind: 'url', url: data.fileUri } };\n }\n }\n return null;\n}\n\nexport function mediaToGemini(media: MediaPart, reporter: Reporter): GeminiPart | null {\n const { source } = media;\n if (source.kind === 'base64') {\n return { inlineData: { mimeType: source.mediaType, data: source.data } };\n }\n if (source.kind === 'url') {\n reporter.warn(\n 'gemini-url-media',\n 'A media URL was emitted as Gemini fileData.fileUri; Gemini may require the Files API for non-Google URIs.',\n );\n return { fileData: { fileUri: source.url } };\n }\n reporter.warn('unsupported-modality', 'Gemini has no file-id media reference; dropped a file_id part.');\n return null;\n}\n","import type {\n OpenAIAssistantMessage,\n OpenAIMessage,\n OpenAISystemMessage,\n OpenAIToolMessage,\n OpenAIUserMessage,\n} from '../types.js';\nimport { Reporter, textOf } from '../util.js';\n\n/** Any canonical message other than a system/developer prompt. */\nexport type NonSystemMessage = OpenAIUserMessage | OpenAIAssistantMessage | OpenAIToolMessage;\n\nfunction isSystem(message: OpenAIMessage): message is OpenAISystemMessage {\n return message.role === 'system' || message.role === 'developer';\n}\n\n/**\n * Splits a canonical OpenAI conversation into its system prompt and the\n * remaining messages. All `system` / `developer` messages are folded into a\n * single string (joined by blank lines) because Anthropic and Gemini carry the\n * system prompt as one top-level field. A system message that appears after the\n * conversation has started is still folded in, but reported as `system-midstream`.\n */\nexport function splitSystem(\n messages: OpenAIMessage[],\n reporter: Reporter,\n): { system?: string; rest: NonSystemMessage[] } {\n const systemParts: string[] = [];\n const rest: NonSystemMessage[] = [];\n let started = false;\n\n for (const message of messages) {\n if (isSystem(message)) {\n if (started) {\n reporter.warn(\n 'system-midstream',\n 'A system message appeared mid conversation; it was merged into the top-level system prompt.',\n );\n }\n systemParts.push(textOf(message.content));\n continue;\n }\n started = true;\n rest.push(message);\n }\n\n const system = systemParts.length > 0 ? systemParts.join('\\n\\n') : undefined;\n return system === undefined ? { rest } : { system, rest };\n}\n","import type {\n AnthropicContentBlock,\n AnthropicConversation,\n AnthropicMessage,\n ConvertOptions,\n OpenAIAssistantMessage,\n OpenAIContentPart,\n OpenAIMessage,\n OpenAIToolMessage,\n} from '../types.js';\nimport { Reporter, isRecord, parseArguments, textOf } from '../util.js';\nimport { imageFromAnthropic, imageFromOpenAI, imageToAnthropic, imageToOpenAI } from '../image.js';\nimport { mediaFromAnthropic, mediaFromOpenAI, mediaToAnthropic, mediaToOpenAI } from '../media.js';\nimport { splitSystem } from './openai.js';\n\n/* ------------------------------------------------------------------ */\n/* OpenAI (canonical) -> Anthropic */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a canonical OpenAI conversation into an Anthropic request fragment\n * (`{ system, messages }`). System messages move to the top-level `system`\n * field, tool-call arguments are JSON parsed into `input` objects, tool results\n * are folded into `tool_result` blocks inside a user turn, and consecutive\n * same-role turns are merged to satisfy Anthropic's strict alternation.\n */\nexport function toAnthropic(messages: OpenAIMessage[], options: ConvertOptions = {}): AnthropicConversation {\n const reporter = new Reporter(options);\n const { system, rest } = splitSystem(messages, reporter);\n const out: AnthropicMessage[] = [];\n\n for (let i = 0; i < rest.length; i++) {\n const message = rest[i];\n\n if (message.role === 'tool') {\n const blocks: AnthropicContentBlock[] = [];\n let j = i;\n while (j < rest.length && rest[j].role === 'tool') {\n const tool = rest[j] as OpenAIToolMessage;\n blocks.push({ type: 'tool_result', tool_use_id: tool.tool_call_id, content: textOf(tool.content) });\n j++;\n }\n out.push({ role: 'user', content: blocks });\n i = j - 1;\n continue;\n }\n\n if (message.role === 'user') {\n out.push({ role: 'user', content: userContent(message.content, reporter) });\n continue;\n }\n\n out.push({ role: 'assistant', content: assistantContent(message, reporter) });\n }\n\n const merged = mergeConsecutive(out, reporter);\n return system === undefined ? { messages: merged } : { system, messages: merged };\n}\n\nfunction userContent(content: string | OpenAIContentPart[], reporter: Reporter): string | AnthropicContentBlock[] {\n if (typeof content === 'string') return content;\n if (!Array.isArray(content)) return textOf(content);\n const blocks: AnthropicContentBlock[] = [];\n for (const part of content) {\n if (isRecord(part) && part.type === 'text' && typeof part.text === 'string') {\n blocks.push({ type: 'text', text: part.text });\n continue;\n }\n const image = imageFromOpenAI(part);\n if (image) {\n blocks.push(imageToAnthropic(image));\n continue;\n }\n const media = mediaFromOpenAI(part);\n if (media) {\n const block = mediaToAnthropic(media, reporter);\n if (block) blocks.push(block);\n continue;\n }\n reporter.warn('dropped-content', 'Dropped an unsupported user content part.');\n }\n return blocks;\n}\n\nfunction assistantContent(message: OpenAIAssistantMessage, reporter: Reporter): string | AnthropicContentBlock[] {\n const text = textOf(message.content ?? '');\n const toolCalls = message.tool_calls ?? [];\n if (toolCalls.length === 0) return text;\n\n const blocks: AnthropicContentBlock[] = [];\n if (text) blocks.push({ type: 'text', text });\n for (const call of toolCalls) {\n blocks.push({\n type: 'tool_use',\n id: call.id,\n name: call.function.name,\n input: parseArguments(call.function.arguments, reporter, call.function.name),\n });\n }\n return blocks;\n}\n\n/** Merges adjacent same-role messages by concatenating their content blocks. */\nfunction mergeConsecutive(messages: AnthropicMessage[], reporter: Reporter): AnthropicMessage[] {\n const result: AnthropicMessage[] = [];\n for (const message of messages) {\n const previous = result[result.length - 1];\n if (previous && previous.role === message.role) {\n previous.content = [...asBlocks(previous.content), ...asBlocks(message.content)];\n reporter.warn(\n 'merged-role',\n `Merged consecutive '${message.role}' turns (Anthropic requires alternating roles).`,\n );\n } else {\n result.push({ role: message.role, content: message.content });\n }\n }\n return result;\n}\n\nfunction asBlocks(content: string | AnthropicContentBlock[]): AnthropicContentBlock[] {\n if (typeof content !== 'string') return content;\n return content ? [{ type: 'text', text: content }] : [];\n}\n\n/* ------------------------------------------------------------------ */\n/* Anthropic -> OpenAI (canonical) */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts an Anthropic request fragment back into a canonical OpenAI message\n * array. The top-level `system` becomes a leading system message, `tool_use`\n * blocks become `tool_calls` (with `input` re-serialized to a JSON string), and\n * `tool_result` blocks become standalone `role: 'tool'` messages.\n */\nexport function fromAnthropic(conversation: AnthropicConversation, options: ConvertOptions = {}): OpenAIMessage[] {\n const reporter = new Reporter(options);\n const out: OpenAIMessage[] = [];\n if (conversation.system) {\n out.push({ role: 'system', content: textOf(conversation.system) });\n }\n\n for (const message of conversation.messages) {\n const blocks = asBlocks(message.content);\n\n if (message.role === 'user') {\n const toolResults = blocks.filter((b) => b.type === 'tool_result');\n const contentBlocks = blocks.filter((b) => b.type !== 'tool_result');\n for (const block of toolResults) {\n out.push({\n role: 'tool',\n tool_call_id: String((block as { tool_use_id?: string }).tool_use_id ?? ''),\n content: textOf((block as { content?: unknown }).content),\n });\n }\n if (contentBlocks.length > 0) {\n out.push({ role: 'user', content: userContentToOpenAI(contentBlocks, reporter) });\n }\n continue;\n }\n\n // assistant\n const text = textOf(blocks.filter((b) => b.type === 'text'));\n const toolUses = blocks.filter((b) => b.type === 'tool_use');\n const assistant: OpenAIAssistantMessage = { role: 'assistant', content: text || null };\n if (toolUses.length > 0) {\n assistant.tool_calls = toolUses.map((block) => {\n const b = block as { id: string; name: string; input: Record<string, unknown> };\n return { id: b.id, type: 'function', function: { name: b.name, arguments: JSON.stringify(b.input ?? {}) } };\n });\n }\n out.push(assistant);\n }\n\n return out;\n}\n\n/** Rebuilds OpenAI user content from Anthropic blocks, as a string unless media is present. */\nfunction userContentToOpenAI(blocks: AnthropicContentBlock[], reporter: Reporter): string | OpenAIContentPart[] {\n const hasMedia = blocks.some((block) => imageFromAnthropic(block) !== null || mediaFromAnthropic(block) !== null);\n if (!hasMedia) return textOf(blocks);\n const parts: OpenAIContentPart[] = [];\n for (const block of blocks) {\n const image = imageFromAnthropic(block);\n if (image) {\n parts.push(imageToOpenAI(image));\n continue;\n }\n const media = mediaFromAnthropic(block);\n if (media) {\n const part = mediaToOpenAI(media);\n if (part) parts.push(part);\n else reporter.warn('dropped-content', 'A document URL has no OpenAI Chat Completions equivalent; dropped.');\n continue;\n }\n if (isRecord(block) && block.type === 'text' && typeof block.text === 'string') {\n parts.push({ type: 'text', text: block.text });\n }\n }\n return parts;\n}\n","import type {\n ConvertOptions,\n GeminiContent,\n GeminiConversation,\n GeminiPart,\n OpenAIAssistantMessage,\n OpenAIContentPart,\n OpenAIMessage,\n OpenAIToolCall,\n OpenAIToolMessage,\n} from '../types.js';\nimport { Reporter, isRecord, parseArguments, textOf, unwrapResponse, wrapResponse } from '../util.js';\nimport { imageFromGemini, imageFromOpenAI, imageToGemini, imageToOpenAI } from '../image.js';\nimport { mediaFromGemini, mediaFromOpenAI, mediaToGemini, mediaToOpenAI } from '../media.js';\nimport { splitSystem } from './openai.js';\n\n/* ------------------------------------------------------------------ */\n/* OpenAI (canonical) -> Gemini */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a canonical OpenAI conversation into a Gemini request fragment\n * (`{ systemInstruction, contents }`). The assistant role becomes `model`,\n * tool-call arguments are JSON parsed into `args` objects, tool results become\n * `functionResponse` parts whose `name` is recovered from the matching call, and\n * consecutive same-role turns are merged for Gemini's strict alternation.\n *\n * The OpenAI tool-call `id` is carried through as `functionCall.id` so that a\n * round trip (OpenAI -> Gemini -> OpenAI) preserves ids exactly.\n */\nexport function toGemini(messages: OpenAIMessage[], options: ConvertOptions = {}): GeminiConversation {\n const reporter = new Reporter(options);\n const { system, rest } = splitSystem(messages, reporter);\n\n const idToName = new Map<string, string>();\n for (const message of rest) {\n if (message.role === 'assistant' && message.tool_calls) {\n for (const call of message.tool_calls) idToName.set(call.id, call.function.name);\n }\n }\n\n const contents: GeminiContent[] = [];\n for (let i = 0; i < rest.length; i++) {\n const message = rest[i];\n\n if (message.role === 'tool') {\n const parts: GeminiPart[] = [];\n let j = i;\n while (j < rest.length && rest[j].role === 'tool') {\n const tool = rest[j] as OpenAIToolMessage;\n const name = idToName.get(tool.tool_call_id);\n if (!name) {\n reporter.warn(\n 'unmapped-tool-result',\n `Tool result '${tool.tool_call_id}' has no matching call; used the id as the function name.`,\n );\n }\n parts.push({\n functionResponse: {\n id: tool.tool_call_id,\n name: name ?? tool.tool_call_id,\n response: wrapResponse(textOf(tool.content)),\n },\n });\n j++;\n }\n contents.push({ role: 'user', parts });\n i = j - 1;\n continue;\n }\n\n if (message.role === 'user') {\n contents.push({ role: 'user', parts: userParts(message.content, reporter) });\n continue;\n }\n\n contents.push({ role: 'model', parts: assistantParts(message, reporter) });\n }\n\n const merged = mergeConsecutive(contents, reporter);\n return system === undefined\n ? { contents: merged }\n : { systemInstruction: { parts: [{ text: system }] }, contents: merged };\n}\n\nfunction userParts(content: string | OpenAIContentPart[], reporter: Reporter): GeminiPart[] {\n if (typeof content === 'string') return [{ text: content }];\n const parts: GeminiPart[] = [];\n for (const part of content) {\n if (isRecord(part) && part.type === 'text' && typeof part.text === 'string') {\n parts.push({ text: part.text });\n continue;\n }\n const image = imageFromOpenAI(part);\n if (image) {\n parts.push(imageToGemini(image, reporter));\n continue;\n }\n const media = mediaFromOpenAI(part);\n if (media) {\n const geminiPart = mediaToGemini(media, reporter);\n if (geminiPart) parts.push(geminiPart);\n continue;\n }\n reporter.warn('dropped-content', 'Dropped an unsupported user content part.');\n }\n return parts.length > 0 ? parts : [{ text: '' }];\n}\n\nfunction assistantParts(message: OpenAIAssistantMessage, reporter: Reporter): GeminiPart[] {\n const parts: GeminiPart[] = [];\n const text = textOf(message.content ?? '');\n if (text) parts.push({ text });\n for (const call of message.tool_calls ?? []) {\n parts.push({\n functionCall: {\n id: call.id,\n name: call.function.name,\n args: parseArguments(call.function.arguments, reporter, call.function.name),\n },\n });\n }\n return parts.length > 0 ? parts : [{ text: '' }];\n}\n\n/** Merges adjacent same-role contents by concatenating their `parts` arrays. */\nfunction mergeConsecutive(contents: GeminiContent[], reporter: Reporter): GeminiContent[] {\n const result: GeminiContent[] = [];\n for (const content of contents) {\n const previous = result[result.length - 1];\n if (previous && previous.role === content.role) {\n previous.parts = [...previous.parts, ...content.parts];\n reporter.warn('merged-role', `Merged consecutive '${content.role}' turns (Gemini requires alternating roles).`);\n } else {\n result.push({ role: content.role, parts: [...content.parts] });\n }\n }\n return result;\n}\n\n/* ------------------------------------------------------------------ */\n/* Gemini -> OpenAI (canonical) */\n/* ------------------------------------------------------------------ */\n\n/**\n * Converts a Gemini request fragment back into a canonical OpenAI message array.\n * Because Gemini matches tool calls and responses by function name (the `id`\n * field is optional), this maintains a queue of pending calls and resolves each\n * `functionResponse` by id when present, otherwise by name in call order,\n * generating a deterministic id as a last resort.\n */\nexport function fromGemini(conversation: GeminiConversation, options: ConvertOptions = {}): OpenAIMessage[] {\n const reporter = new Reporter(options);\n const out: OpenAIMessage[] = [];\n\n if (conversation.systemInstruction) {\n const text = textOf(conversation.systemInstruction.parts);\n if (text) out.push({ role: 'system', content: text });\n }\n\n const pending: { id: string; name: string }[] = [];\n let counter = 0;\n const generateId = (name: string): string => `call_${name.replace(/[^a-zA-Z0-9_-]/g, '_')}_${counter++}`;\n\n for (const content of conversation.contents) {\n const parts = Array.isArray(content.parts) ? content.parts : [];\n\n if (content.role === 'model') {\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n for (const part of parts) {\n if (isRecord(part) && isRecord(part.functionCall)) {\n const fc = part.functionCall as { id?: string; name: string; args?: Record<string, unknown> };\n const id = fc.id ?? generateId(fc.name);\n if (!fc.id) reporter.warn('generated-id', `Gemini functionCall '${fc.name}' had no id; generated '${id}'.`);\n toolCalls.push({\n id,\n type: 'function',\n function: { name: fc.name, arguments: JSON.stringify(fc.args ?? {}) },\n });\n pending.push({ id, name: fc.name });\n } else if (isRecord(part) && typeof part.text === 'string') {\n textPieces.push(part.text);\n }\n }\n const text = textPieces.join('');\n const assistant: OpenAIAssistantMessage = { role: 'assistant', content: text || null };\n if (toolCalls.length > 0) assistant.tool_calls = toolCalls;\n out.push(assistant);\n continue;\n }\n\n // role 'user' or unspecified\n const contentParts: OpenAIContentPart[] = [];\n let hasMedia = false;\n for (const part of parts) {\n if (isRecord(part) && isRecord(part.functionResponse)) {\n const fr = part.functionResponse as { id?: string; name: string; response?: Record<string, unknown> };\n const id = resolveResponseId(fr, pending, reporter, generateId);\n out.push({ role: 'tool', tool_call_id: id, content: unwrapResponse(fr.response ?? {}) });\n continue;\n }\n const image = imageFromGemini(part);\n if (image) {\n contentParts.push(imageToOpenAI(image));\n hasMedia = true;\n continue;\n }\n const media = mediaFromGemini(part);\n if (media) {\n const openaiPart = mediaToOpenAI(media);\n if (openaiPart) {\n contentParts.push(openaiPart);\n hasMedia = true;\n }\n continue;\n }\n if (isRecord(part) && typeof part.text === 'string') {\n contentParts.push({ type: 'text', text: part.text });\n }\n }\n if (contentParts.length > 0) {\n if (hasMedia) {\n out.push({ role: 'user', content: contentParts });\n } else {\n const text = textOf(contentParts);\n if (text) out.push({ role: 'user', content: text });\n }\n }\n }\n\n return out;\n}\n\nfunction resolveResponseId(\n response: { id?: string; name: string },\n pending: { id: string; name: string }[],\n reporter: Reporter,\n generateId: (name: string) => string,\n): string {\n if (response.id) {\n const index = pending.findIndex((p) => p.id === response.id);\n if (index >= 0) pending.splice(index, 1);\n return response.id;\n }\n const index = pending.findIndex((p) => p.name === response.name);\n if (index >= 0) {\n const { id } = pending[index];\n pending.splice(index, 1);\n return id;\n }\n const id = generateId(response.name);\n reporter.warn(\n 'unmapped-tool-result',\n `Gemini functionResponse for '${response.name}' had no matching call; generated '${id}'.`,\n );\n return id;\n}\n","import type { AnthropicConversation, ConvertOptions, GeminiConversation, OpenAIMessage, Provider } from './types.js';\nimport { fromAnthropic, toAnthropic } from './providers/anthropic.js';\nimport { fromGemini, toGemini } from './providers/gemini.js';\n\n/** Maps a provider to the conversation shape it accepts and returns. */\nexport type ConversationOf<P extends Provider> = P extends 'openai'\n ? OpenAIMessage[]\n : P extends 'anthropic'\n ? AnthropicConversation\n : P extends 'gemini'\n ? GeminiConversation\n : never;\n\n/**\n * Converts a conversation from one provider format to another. Every conversion\n * routes through the canonical OpenAI representation, so any source/target pair\n * is supported, including same-provider normalization.\n *\n * @example\n * const gemini = convert(openaiMessages, { from: 'openai', to: 'gemini' });\n * const openai = convert(anthropicBody, { from: 'anthropic', to: 'openai' });\n */\nexport function convert<From extends Provider, To extends Provider>(\n conversation: ConversationOf<From>,\n route: { from: From; to: To },\n options: ConvertOptions = {},\n): ConversationOf<To> {\n const canonical = toCanonical(conversation, route.from, options);\n return fromCanonical(canonical, route.to, options) as ConversationOf<To>;\n}\n\nfunction toCanonical(conversation: unknown, from: Provider, options: ConvertOptions): OpenAIMessage[] {\n switch (from) {\n case 'openai':\n return conversation as OpenAIMessage[];\n case 'anthropic':\n return fromAnthropic(conversation as AnthropicConversation, options);\n case 'gemini':\n return fromGemini(conversation as GeminiConversation, options);\n default:\n throw new Error(`Unknown source provider: ${String(from)}`);\n }\n}\n\nfunction fromCanonical(canonical: OpenAIMessage[], to: Provider, options: ConvertOptions): ConversationOf<Provider> {\n switch (to) {\n case 'openai':\n return canonical;\n case 'anthropic':\n return toAnthropic(canonical, options);\n case 'gemini':\n return toGemini(canonical, options);\n default:\n throw new Error(`Unknown target provider: ${String(to)}`);\n }\n}\n","import type { ConvertOptions, OpenAIAssistantMessage, OpenAIToolCall, Provider } from './types.js';\nimport { Reporter, isRecord, textOf } from './util.js';\n\n/** A provider-neutral finish reason. */\nexport type FinishReason = 'stop' | 'tool_calls' | 'length' | 'content_filter' | 'unknown';\n\n/** Provider-neutral token usage. */\nexport interface Usage {\n inputTokens: number;\n outputTokens: number;\n}\n\n/** A provider response normalized to the canonical OpenAI assistant shape. */\nexport interface NormalizedResponse {\n message: OpenAIAssistantMessage;\n finishReason: FinishReason;\n usage: Usage;\n}\n\nconst num = (value: unknown): number => (typeof value === 'number' ? value : 0);\n\n/** Builds the canonical message, setting `content` to null when only tool calls are present. */\nfunction buildMessage(text: string, toolCalls: OpenAIToolCall[]): OpenAIAssistantMessage {\n const message: OpenAIAssistantMessage = { role: 'assistant', content: text ? text : null };\n if (toolCalls.length > 0) message.tool_calls = toolCalls;\n return message;\n}\n\n/** When tool calls are present the canonical finish reason is always `tool_calls`. */\nfunction finalReason(mapped: FinishReason, toolCalls: OpenAIToolCall[]): FinishReason {\n return toolCalls.length > 0 ? 'tool_calls' : mapped;\n}\n\n/* -------------------------------- OpenAI ------------------------------- */\n\nconst OPENAI_FINISH: Record<string, FinishReason> = {\n stop: 'stop',\n length: 'length',\n tool_calls: 'tool_calls',\n content_filter: 'content_filter',\n function_call: 'tool_calls',\n};\n\n/** Normalizes an OpenAI Chat Completions response body. */\nexport function responseFromOpenAI(body: unknown): NormalizedResponse {\n const root = isRecord(body) ? body : {};\n const choice = Array.isArray(root.choices) && isRecord(root.choices[0]) ? root.choices[0] : {};\n const message = isRecord(choice.message) ? choice.message : {};\n\n const text = typeof message.content === 'string' ? message.content : textOf(message.content);\n const toolCalls = Array.isArray(message.tool_calls) ? (message.tool_calls as OpenAIToolCall[]) : [];\n\n const usage = isRecord(root.usage) ? root.usage : {};\n return {\n message: buildMessage(text, toolCalls),\n finishReason: finalReason(OPENAI_FINISH[String(choice.finish_reason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.prompt_tokens), outputTokens: num(usage.completion_tokens) },\n };\n}\n\n/* ------------------------------- Anthropic ----------------------------- */\n\nconst ANTHROPIC_FINISH: Record<string, FinishReason> = {\n end_turn: 'stop',\n stop_sequence: 'stop',\n tool_use: 'tool_calls',\n max_tokens: 'length',\n refusal: 'content_filter',\n pause_turn: 'unknown',\n};\n\n/** Normalizes an Anthropic Messages response body. */\nexport function responseFromAnthropic(body: unknown): NormalizedResponse {\n const root = isRecord(body) ? body : {};\n const blocks = Array.isArray(root.content) ? root.content : [];\n\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n for (const block of blocks) {\n if (!isRecord(block)) continue;\n if (block.type === 'text' && typeof block.text === 'string') {\n textPieces.push(block.text);\n } else if (block.type === 'tool_use' && typeof block.name === 'string') {\n toolCalls.push({\n id: typeof block.id === 'string' ? block.id : '',\n type: 'function',\n function: { name: block.name, arguments: JSON.stringify(block.input ?? {}) },\n });\n }\n }\n\n const usage = isRecord(root.usage) ? root.usage : {};\n return {\n message: buildMessage(textPieces.join(''), toolCalls),\n finishReason: finalReason(ANTHROPIC_FINISH[String(root.stop_reason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.input_tokens), outputTokens: num(usage.output_tokens) },\n };\n}\n\n/* -------------------------------- Gemini ------------------------------- */\n\nconst GEMINI_FINISH: Record<string, FinishReason> = {\n STOP: 'stop',\n MAX_TOKENS: 'length',\n SAFETY: 'content_filter',\n RECITATION: 'content_filter',\n MALFORMED_FUNCTION_CALL: 'content_filter',\n};\n\n/** Normalizes a Gemini generateContent response body. */\nexport function responseFromGemini(body: unknown, options: ConvertOptions = {}): NormalizedResponse {\n const reporter = new Reporter(options);\n const root = isRecord(body) ? body : {};\n const candidate = Array.isArray(root.candidates) && isRecord(root.candidates[0]) ? root.candidates[0] : {};\n const content = isRecord(candidate.content) ? candidate.content : {};\n const parts = Array.isArray(content.parts) ? content.parts : [];\n\n const textPieces: string[] = [];\n const toolCalls: OpenAIToolCall[] = [];\n let counter = 0;\n for (const part of parts) {\n if (!isRecord(part)) continue;\n if (isRecord(part.functionCall)) {\n const call = part.functionCall as { id?: string; name?: string; args?: Record<string, unknown> };\n const name = typeof call.name === 'string' ? call.name : 'function';\n let id = call.id;\n if (!id) {\n id = `call_${name.replace(/[^a-zA-Z0-9_-]/g, '_')}_${counter++}`;\n reporter.warn('generated-id', `Gemini functionCall '${name}' had no id; generated '${id}'.`);\n }\n toolCalls.push({ id, type: 'function', function: { name, arguments: JSON.stringify(call.args ?? {}) } });\n } else if (typeof part.text === 'string') {\n textPieces.push(part.text);\n }\n }\n\n const usage = isRecord(root.usageMetadata) ? root.usageMetadata : {};\n return {\n message: buildMessage(textPieces.join(''), toolCalls),\n finishReason: finalReason(GEMINI_FINISH[String(candidate.finishReason)] ?? 'unknown', toolCalls),\n usage: { inputTokens: num(usage.promptTokenCount), outputTokens: num(usage.candidatesTokenCount) },\n };\n}\n\n/* ------------------------------ Dispatcher ----------------------------- */\n\n/** Normalizes a provider response body into the canonical shape. */\nexport function normalizeResponse(\n body: unknown,\n route: { from: Provider },\n options: ConvertOptions = {},\n): NormalizedResponse {\n switch (route.from) {\n case 'openai':\n return responseFromOpenAI(body);\n case 'anthropic':\n return responseFromAnthropic(body);\n case 'gemini':\n return responseFromGemini(body, options);\n default:\n throw new Error(`Unknown source provider: ${String(route.from)}`);\n }\n}\n"],"mappings":";;;;;;;;;;;;;;;;;;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