acp-kernel 0.0.23 → 0.0.25

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.
@@ -0,0 +1,676 @@
1
+ // src/wire/util.ts
2
+ import { createHash } from "crypto";
3
+ function hashId(s) {
4
+ return createHash("sha256").update(s, "utf8").digest("hex").slice(0, 16);
5
+ }
6
+
7
+ // src/wire/message-id.ts
8
+ function deriveMessageId(role, contentType, text, options = {}) {
9
+ const seed = `${role}|${contentType}|${options.toolCallId ?? ""}|${options.toolName ?? ""}|${text}`;
10
+ return "h_" + hashId(seed);
11
+ }
12
+ var ClusterCounter = class {
13
+ counts = /* @__PURE__ */ new Map();
14
+ next(baseId) {
15
+ const n = this.counts.get(baseId) ?? 0;
16
+ this.counts.set(baseId, n + 1);
17
+ return n === 0 ? baseId : `${baseId}_${n}`;
18
+ }
19
+ };
20
+
21
+ // src/wire/anthropic.ts
22
+ function extractSystem(system) {
23
+ if (!system) return "";
24
+ if (typeof system === "string") return system;
25
+ return system.map((b) => b.text).join("\n\n");
26
+ }
27
+ function buildSystem(text, original) {
28
+ if (Array.isArray(original) && original.length > 0) {
29
+ const ccBlock = original.find((b) => b.cache_control);
30
+ return [{ type: "text", text, ...ccBlock ? { cache_control: ccBlock.cache_control } : {} }];
31
+ }
32
+ return text;
33
+ }
34
+ function anthropicToCore(body) {
35
+ const msgs = [];
36
+ const cacheControls = /* @__PURE__ */ new Map();
37
+ const clusters = new ClusterCounter();
38
+ for (const m of body.messages) {
39
+ const blocks = typeof m.content === "string" ? [{ type: "text", text: m.content }] : m.content;
40
+ for (const b of blocks) {
41
+ switch (b.type) {
42
+ case "text": {
43
+ const base = deriveMessageId(m.role, "text", b.text);
44
+ const id = clusters.next(base);
45
+ msgs.push({ id, role: m.role, contentType: "text", text: b.text });
46
+ if (b.cache_control) cacheControls.set(id, b.cache_control);
47
+ break;
48
+ }
49
+ case "tool_use": {
50
+ const base = deriveMessageId("assistant", "tool-call", safeStringify(b.input), {
51
+ toolCallId: b.id,
52
+ toolName: b.name
53
+ });
54
+ const id = clusters.next(base);
55
+ msgs.push({
56
+ id,
57
+ role: "assistant",
58
+ contentType: "tool-call",
59
+ toolName: b.name,
60
+ toolCallId: b.id,
61
+ text: safeStringify(b.input)
62
+ });
63
+ if (b.cache_control) cacheControls.set(id, b.cache_control);
64
+ break;
65
+ }
66
+ case "tool_result": {
67
+ const text = typeof b.content === "string" ? b.content : b.content.map((c) => c.text).join("\n");
68
+ const base = deriveMessageId("tool", "tool-result", text, { toolCallId: b.tool_use_id });
69
+ const id = clusters.next(base);
70
+ msgs.push({
71
+ id,
72
+ role: "tool",
73
+ contentType: "tool-result",
74
+ toolCallId: b.tool_use_id,
75
+ text,
76
+ ...b.is_error === true ? { toolIsError: true } : {}
77
+ });
78
+ if (b.cache_control) cacheControls.set(id, b.cache_control);
79
+ break;
80
+ }
81
+ case "thinking": {
82
+ const base = deriveMessageId("assistant", "reasoning", b.thinking);
83
+ msgs.push({
84
+ id: clusters.next(base),
85
+ role: "assistant",
86
+ contentType: "reasoning",
87
+ text: b.thinking,
88
+ ...b.signature ? { thinkingSignature: b.signature } : {}
89
+ });
90
+ break;
91
+ }
92
+ case "image": {
93
+ const base = deriveMessageId(m.role, "text", "[image]");
94
+ msgs.push({
95
+ id: clusters.next(base),
96
+ role: m.role,
97
+ contentType: "text",
98
+ text: "[image]",
99
+ rawAnthropicBlock: b
100
+ });
101
+ break;
102
+ }
103
+ }
104
+ }
105
+ }
106
+ return { msgs, cacheControls };
107
+ }
108
+ function coreToAnthropic(messages, cacheControls) {
109
+ const out = [];
110
+ let current = null;
111
+ const flush = () => {
112
+ if (current && current.blocks.length > 0) {
113
+ out.push({ role: current.role, content: current.blocks });
114
+ }
115
+ current = null;
116
+ };
117
+ const cc = (id) => {
118
+ const v = cacheControls?.get(id);
119
+ return v ? { cache_control: v } : {};
120
+ };
121
+ for (const m of messages) {
122
+ const target = m.role === "assistant" ? "assistant" : "user";
123
+ if (!current || current.role !== target) {
124
+ flush();
125
+ current = { role: target, blocks: [] };
126
+ }
127
+ switch (m.contentType) {
128
+ case "text": {
129
+ if (m.rawAnthropicBlock) {
130
+ current.blocks.push(m.rawAnthropicBlock);
131
+ break;
132
+ }
133
+ current.blocks.push({ type: "text", text: m.text ?? "", ...cc(m.id) });
134
+ break;
135
+ }
136
+ case "tool-call":
137
+ current.blocks.push({
138
+ type: "tool_use",
139
+ id: m.toolCallId ?? `call_${m.id}`,
140
+ name: m.toolName ?? "unknown",
141
+ input: safeParse(m.text),
142
+ ...cc(m.id)
143
+ });
144
+ break;
145
+ case "tool-result":
146
+ current.blocks.push({
147
+ type: "tool_result",
148
+ tool_use_id: m.toolCallId ?? "",
149
+ content: m.text ?? "",
150
+ ...m.toolIsError ? { is_error: true } : {},
151
+ ...cc(m.id)
152
+ });
153
+ break;
154
+ case "reasoning":
155
+ current.blocks.push({
156
+ type: "thinking",
157
+ thinking: m.text ?? "",
158
+ ...m.thinkingSignature ? { signature: m.thinkingSignature } : {}
159
+ });
160
+ break;
161
+ }
162
+ }
163
+ flush();
164
+ return out;
165
+ }
166
+ function conversationSignalAnthropic(body, headerValue) {
167
+ if (headerValue && headerValue.trim()) return headerValue.trim();
168
+ const firstUser = body.messages.find((m) => m.role === "user");
169
+ const seed = firstUser ? JSON.stringify(firstUser.content) : "default";
170
+ return hashId(seed);
171
+ }
172
+ function safeStringify(v) {
173
+ try {
174
+ return JSON.stringify(v ?? {});
175
+ } catch {
176
+ return "{}";
177
+ }
178
+ }
179
+ function safeParse(s) {
180
+ if (!s) return {};
181
+ try {
182
+ return JSON.parse(s);
183
+ } catch {
184
+ return {};
185
+ }
186
+ }
187
+
188
+ // src/wire/bili-message.ts
189
+ function toCoreMessages(msgs) {
190
+ return msgs;
191
+ }
192
+ function parseDataUrl(url) {
193
+ const m = /^data:([^;,]+)(?:;base64)?,(.+)$/i.exec(url);
194
+ if (!m) return void 0;
195
+ return { mediaType: m[1], base64: m[2] };
196
+ }
197
+
198
+ // src/wire/openai.ts
199
+ function openaiToCore(body) {
200
+ const msgs = [];
201
+ const clusters = new ClusterCounter();
202
+ for (const m of body.messages) {
203
+ switch (m.role) {
204
+ case "system":
205
+ case "developer": {
206
+ const base = deriveMessageId(m.role, "text", stringContent(m.content));
207
+ msgs.push({ id: clusters.next(base), role: "system", contentType: "text", text: stringContent(m.content), originalRole: m.role });
208
+ break;
209
+ }
210
+ case "user": {
211
+ const text = stringContent(m.content);
212
+ const img = firstImagePart(m.content);
213
+ const base = deriveMessageId("user", "text", text);
214
+ msgs.push({
215
+ id: clusters.next(base),
216
+ role: "user",
217
+ contentType: "text",
218
+ text,
219
+ ...img ? { rawOpenaiContent: img.part, imageMediaType: img.mediaType, imageBase64: img.base64 } : {}
220
+ });
221
+ break;
222
+ }
223
+ case "assistant": {
224
+ const reasoning = typeof m.reasoning_content === "string" ? m.reasoning_content : "";
225
+ if (reasoning) {
226
+ const base = deriveMessageId("assistant", "reasoning", reasoning);
227
+ msgs.push({
228
+ id: clusters.next(base),
229
+ role: "assistant",
230
+ contentType: "reasoning",
231
+ text: reasoning,
232
+ reasoningContent: reasoning
233
+ });
234
+ }
235
+ const text = stringContent(m.content);
236
+ if (text) {
237
+ const base = deriveMessageId("assistant", "text", text);
238
+ msgs.push({ id: clusters.next(base), role: "assistant", contentType: "text", text });
239
+ }
240
+ if (Array.isArray(m.tool_calls)) {
241
+ for (const tc of m.tool_calls) {
242
+ const base = deriveMessageId("assistant", "tool-call", tc.function.arguments ?? "", {
243
+ toolCallId: tc.id,
244
+ toolName: tc.function.name
245
+ });
246
+ msgs.push({
247
+ id: clusters.next(base),
248
+ role: "assistant",
249
+ contentType: "tool-call",
250
+ toolName: tc.function.name,
251
+ toolCallId: tc.id,
252
+ text: tc.function.arguments ?? ""
253
+ });
254
+ }
255
+ }
256
+ break;
257
+ }
258
+ case "tool": {
259
+ const base = deriveMessageId("tool", "tool-result", stringContent(m.content), {
260
+ toolCallId: m.tool_call_id ?? ""
261
+ });
262
+ msgs.push({
263
+ id: clusters.next(base),
264
+ role: "tool",
265
+ contentType: "tool-result",
266
+ toolCallId: m.tool_call_id ?? "",
267
+ text: stringContent(m.content)
268
+ });
269
+ break;
270
+ }
271
+ }
272
+ }
273
+ return { msgs };
274
+ }
275
+ function coreToOpenai(messages) {
276
+ const out = [];
277
+ let pending = null;
278
+ const flush = () => {
279
+ if (!pending) return;
280
+ const reasoning = pending.reasoning !== null && pending.reasoning.length > 0 ? pending.reasoning : void 0;
281
+ if (pending.toolCalls.length > 0) {
282
+ out.push({
283
+ role: "assistant",
284
+ content: pending.text ?? null,
285
+ tool_calls: pending.toolCalls,
286
+ ...reasoning ? { reasoning_content: reasoning } : {}
287
+ });
288
+ } else if (pending.text !== null) {
289
+ out.push({ role: "assistant", content: pending.text, ...reasoning ? { reasoning_content: reasoning } : {} });
290
+ } else if (reasoning) {
291
+ out.push({ role: "assistant", content: null, reasoning_content: reasoning });
292
+ }
293
+ pending = null;
294
+ };
295
+ for (const m of messages) {
296
+ if (m.role === "assistant") {
297
+ if (!pending) pending = { text: null, toolCalls: [], reasoning: null };
298
+ if (m.contentType === "reasoning") {
299
+ pending.reasoning = (pending.reasoning ?? "") + (m.reasoningContent ?? m.text ?? "");
300
+ } else if (m.contentType === "text") {
301
+ pending.text = (pending.text ?? "") + (m.text ?? "");
302
+ } else if (m.contentType === "tool-call") {
303
+ pending.toolCalls.push({
304
+ id: m.toolCallId ?? `call_${m.id}`,
305
+ type: "function",
306
+ function: { name: m.toolName ?? "unknown", arguments: m.text ?? "" }
307
+ });
308
+ }
309
+ } else {
310
+ flush();
311
+ if (m.role === "system") {
312
+ out.push({ role: m.originalRole === "developer" ? "developer" : "system", content: m.text ?? "" });
313
+ } else if (m.role === "user") {
314
+ if (m.rawOpenaiContent || m.imageBase64) {
315
+ const parts = [];
316
+ if (m.text) parts.push({ type: "text", text: m.text });
317
+ if (m.rawOpenaiContent) {
318
+ parts.push(m.rawOpenaiContent);
319
+ } else if (m.imageBase64 && m.imageMediaType) {
320
+ parts.push({ type: "image_url", image_url: { url: `data:${m.imageMediaType};base64,${m.imageBase64}` } });
321
+ }
322
+ out.push({ role: "user", content: parts });
323
+ } else {
324
+ out.push({ role: "user", content: m.text ?? "" });
325
+ }
326
+ } else if (m.role === "tool") {
327
+ out.push({ role: "tool", tool_call_id: m.toolCallId ?? "", content: m.text ?? "" });
328
+ }
329
+ }
330
+ }
331
+ flush();
332
+ return out;
333
+ }
334
+ function injectOpenaiSystem(messages, parts) {
335
+ if (parts.length === 0) return messages;
336
+ const extra = parts.join("\n\n");
337
+ if (messages.length > 0 && (messages[0]?.role === "system" || messages[0]?.role === "developer")) {
338
+ const head = messages[0];
339
+ const base = stringContent(head.content);
340
+ const merged = base ? `${base}
341
+
342
+ ---
343
+
344
+ ${extra}` : extra;
345
+ return [{ ...head, content: merged }, ...messages.slice(1)];
346
+ }
347
+ return [{ role: "system", content: extra }, ...messages];
348
+ }
349
+ function conversationSignalOpenai(body, headerValue) {
350
+ if (headerValue && headerValue.trim()) return headerValue.trim();
351
+ const firstUser = body.messages.find((m) => m.role === "user");
352
+ const seed = firstUser ? stringContent(firstUser.content) : "default";
353
+ return hashId(seed);
354
+ }
355
+ function stringContent(content) {
356
+ if (content == null) return "";
357
+ if (typeof content === "string") return content;
358
+ if (Array.isArray(content)) {
359
+ return content.map((p) => typeof p === "string" ? p : p.type === "text" ? p.text ?? "" : "").join("\n");
360
+ }
361
+ return "";
362
+ }
363
+ function firstImagePart(content) {
364
+ if (!Array.isArray(content)) return void 0;
365
+ for (const p of content) {
366
+ if (p && typeof p === "object" && p.type === "image_url") {
367
+ const iu = p.image_url;
368
+ const url = iu?.url;
369
+ if (typeof url === "string") {
370
+ const parsed = parseDataUrl(url);
371
+ if (parsed) return { part: p, mediaType: parsed.mediaType, base64: parsed.base64 };
372
+ }
373
+ }
374
+ }
375
+ return void 0;
376
+ }
377
+
378
+ // src/wire/responses.ts
379
+ var OPAQUE_ITEM_TYPES = /* @__PURE__ */ new Set([
380
+ "additional_tools",
381
+ "mcp_list_tools"
382
+ ]);
383
+ function isOpaqueItem(item) {
384
+ return OPAQUE_ITEM_TYPES.has(item.type);
385
+ }
386
+ function shouldDropAllReasoning() {
387
+ return (process.env.ACP_REASONING_KEEP ?? "").trim().toLowerCase() === "none";
388
+ }
389
+ function partText(part) {
390
+ if (part.type === "input_text" || part.type === "output_text") {
391
+ return typeof part.text === "string" ? part.text : "";
392
+ }
393
+ return "";
394
+ }
395
+ function messageContent(content) {
396
+ return typeof content === "string" ? content : content.map(partText).join("\n");
397
+ }
398
+ function responsesToCore(body) {
399
+ const msgs = [];
400
+ const systemParts = [];
401
+ const preamble = [];
402
+ const customToolCallIds = /* @__PURE__ */ new Set();
403
+ const layout = [];
404
+ let droppedReasoning = 0;
405
+ const clusters = new ClusterCounter();
406
+ let idx = 0;
407
+ if (typeof body.instructions === "string" && body.instructions.trim()) systemParts.push(body.instructions);
408
+ if (typeof body.input === "string") {
409
+ const id = clusters.next(deriveMessageId("user", "text", body.input));
410
+ msgs.push({ id, role: "user", contentType: "text", text: body.input });
411
+ return { msgs, systemParts, preamble, customToolCallIds, layout, droppedReasoning, stringInput: { original: body.input, coreId: id } };
412
+ }
413
+ for (const item of body.input) {
414
+ let coreId;
415
+ if (isOpaqueItem(item)) preamble.push(item);
416
+ switch (item.type) {
417
+ case "reasoning": {
418
+ if (shouldDropAllReasoning()) {
419
+ droppedReasoning++;
420
+ continue;
421
+ }
422
+ const rid = typeof item.id === "string" ? String(item.id) : hashId(JSON.stringify(item));
423
+ coreId = clusters.next(deriveMessageId("assistant", "reasoning", rid));
424
+ msgs.push({
425
+ id: coreId,
426
+ role: "assistant",
427
+ contentType: "reasoning",
428
+ text: rid,
429
+ rawResponsesItem: item
430
+ });
431
+ break;
432
+ }
433
+ case "message": {
434
+ const message = item;
435
+ const text = messageContent(message.content);
436
+ if (message.role === "system" || message.role === "developer") {
437
+ systemParts.push(text);
438
+ idx++;
439
+ continue;
440
+ } else if (message.role === "user" || message.role === "assistant" && text) {
441
+ const role = message.role;
442
+ coreId = clusters.next(deriveMessageId(role, "text", text));
443
+ const imageUrl = Array.isArray(message.content) ? message.content.find((part) => part.type === "input_image" && typeof part.image_url === "string")?.image_url : void 0;
444
+ const image = typeof imageUrl === "string" ? parseDataUrl(imageUrl) : void 0;
445
+ msgs.push({
446
+ id: coreId,
447
+ role,
448
+ contentType: "text",
449
+ text,
450
+ rawResponsesItem: item,
451
+ ...image ? { imageMediaType: image.mediaType, imageBase64: image.base64 } : {}
452
+ });
453
+ }
454
+ break;
455
+ }
456
+ case "function_call": {
457
+ const call = item;
458
+ coreId = clusters.next(deriveMessageId("assistant", "tool-call", call.arguments ?? "", {
459
+ toolCallId: call.call_id,
460
+ toolName: call.name
461
+ }));
462
+ msgs.push({
463
+ id: coreId,
464
+ role: "assistant",
465
+ contentType: "tool-call",
466
+ toolName: call.name,
467
+ toolCallId: call.call_id,
468
+ text: call.arguments ?? "",
469
+ rawResponsesItem: item
470
+ });
471
+ break;
472
+ }
473
+ case "function_call_output": {
474
+ const output = item;
475
+ const text = typeof output.output === "string" ? output.output : JSON.stringify(output.output);
476
+ coreId = clusters.next(deriveMessageId("tool", "tool-result", text, { toolCallId: output.call_id }));
477
+ msgs.push({ id: coreId, role: "tool", contentType: "tool-result", toolCallId: output.call_id, text, rawResponsesItem: item });
478
+ break;
479
+ }
480
+ case "computer_call":
481
+ case "computer_call_output":
482
+ case "file_search_call":
483
+ case "web_search_call":
484
+ case "image_generation_call":
485
+ case "code_interpreter_call":
486
+ case "mcp_call": {
487
+ const rid = typeof item.id === "string" ? String(item.id) : hashId(JSON.stringify(item));
488
+ coreId = clusters.next(deriveMessageId("assistant", "responses-call", rid));
489
+ msgs.push({ id: coreId, role: "assistant", contentType: "reasoning", text: rid, rawResponsesItem: item });
490
+ break;
491
+ }
492
+ case "custom_tool_call": {
493
+ const ctc = item;
494
+ const callId = ctc.call_id ?? `call_${idx}`;
495
+ customToolCallIds.add(callId);
496
+ const argText = ctc.input ?? ctc.arguments ?? "";
497
+ coreId = clusters.next(deriveMessageId("assistant", "tool-call", argText, { toolCallId: callId, toolName: ctc.name ?? "custom" }));
498
+ msgs.push({ id: coreId, role: "assistant", contentType: "tool-call", toolName: ctc.name ?? "custom", toolCallId: callId, text: argText, rawResponsesItem: item });
499
+ break;
500
+ }
501
+ case "custom_tool_call_output": {
502
+ const ctco = item;
503
+ const callId = ctco.call_id ?? `call_${idx}`;
504
+ customToolCallIds.add(callId);
505
+ const outText = typeof ctco.output === "string" ? ctco.output : JSON.stringify(ctco.output ?? "");
506
+ coreId = clusters.next(deriveMessageId("tool", "tool-result", outText, { toolCallId: callId }));
507
+ msgs.push({ id: coreId, role: "tool", contentType: "tool-result", toolCallId: callId, text: outText, rawResponsesItem: item });
508
+ break;
509
+ }
510
+ default:
511
+ if (!isOpaqueItem(item)) preamble.push(item);
512
+ break;
513
+ }
514
+ layout.push({ original: item, coreId });
515
+ idx++;
516
+ }
517
+ return { msgs, systemParts, preamble, customToolCallIds, layout, droppedReasoning };
518
+ }
519
+ function patchTextParts(parts, text) {
520
+ const textIndexes = parts.flatMap(
521
+ (part, index) => part.type === "input_text" || part.type === "output_text" ? [index] : []
522
+ );
523
+ if (textIndexes.length === 0) return [{ type: "input_text", text }, ...parts];
524
+ const first = textIndexes[0];
525
+ const remaining = new Set(textIndexes.slice(1));
526
+ return parts.map((part, index) => {
527
+ if (index === first) return { ...part, text };
528
+ if (remaining.has(index)) return { ...part, text: "" };
529
+ return part;
530
+ });
531
+ }
532
+ function patchOriginalItem(original, source, next) {
533
+ if (source.text === next.text && source.toolName === next.toolName && source.toolCallId === next.toolCallId && source.role === next.role && source.contentType === next.contentType) return original;
534
+ if (original.type === "message") {
535
+ const message = original;
536
+ const content = typeof message.content === "string" ? next.text ?? "" : patchTextParts(message.content, next.text ?? "");
537
+ return { ...message, content };
538
+ }
539
+ if (original.type === "function_call") {
540
+ return {
541
+ ...original,
542
+ name: next.toolName ?? String(original.name ?? "unknown"),
543
+ call_id: next.toolCallId ?? String(original.call_id ?? ""),
544
+ arguments: next.text ?? ""
545
+ };
546
+ }
547
+ if (original.type === "function_call_output") {
548
+ return { ...original, call_id: next.toolCallId ?? String(original.call_id ?? ""), output: next.text ?? "" };
549
+ }
550
+ return original;
551
+ }
552
+ function patchResponsesInput(projection, messages) {
553
+ if (projection.stringInput) {
554
+ const original = projection.msgs.find((message) => message.id === projection.stringInput?.coreId);
555
+ const next = messages.find((message) => message.id === projection.stringInput?.coreId);
556
+ if (original && next && messages.length === 1 && next.role === "user" && next.contentType === "text") {
557
+ return next.text === original.text ? projection.stringInput.original : next.text ?? "";
558
+ }
559
+ return coreToResponses(messages, projection.customToolCallIds);
560
+ }
561
+ const sourceById = new Map(projection.msgs.map((message) => [message.id, message]));
562
+ const nextById = new Map(messages.map((message) => [message.id, message]));
563
+ const slotById = /* @__PURE__ */ new Map();
564
+ projection.layout.forEach((slot, index) => {
565
+ if (slot.coreId) slotById.set(slot.coreId, index);
566
+ });
567
+ const insertions = /* @__PURE__ */ new Map();
568
+ for (let index = 0; index < messages.length; index++) {
569
+ const message = messages[index];
570
+ if (sourceById.has(message.id)) continue;
571
+ let target = projection.layout.length;
572
+ for (let nextIndex = index + 1; nextIndex < messages.length; nextIndex++) {
573
+ const slot = slotById.get(messages[nextIndex].id);
574
+ if (slot !== void 0) {
575
+ target = slot;
576
+ break;
577
+ }
578
+ }
579
+ const generated = coreToResponses([message], projection.customToolCallIds);
580
+ if (generated.length > 0) insertions.set(target, [...insertions.get(target) ?? [], ...generated]);
581
+ }
582
+ const out = [];
583
+ projection.layout.forEach((slot, index) => {
584
+ out.push(...insertions.get(index) ?? []);
585
+ if (!slot.coreId) {
586
+ out.push(slot.original);
587
+ return;
588
+ }
589
+ const source = sourceById.get(slot.coreId);
590
+ const next = nextById.get(slot.coreId);
591
+ if (source && next) out.push(patchOriginalItem(slot.original, source, next));
592
+ });
593
+ out.push(...insertions.get(projection.layout.length) ?? []);
594
+ return out;
595
+ }
596
+ function coreToResponses(messages, customToolCallIds = /* @__PURE__ */ new Set()) {
597
+ const out = [];
598
+ for (const message of messages) {
599
+ const biliMessage = message;
600
+ const raw = biliMessage.rawResponsesItem;
601
+ if (message.role === "system") {
602
+ out.push({ type: "message", role: "developer", content: message.text ?? "" });
603
+ } else if (message.role === "user") {
604
+ if (raw?.type === "message" && messageContent(raw.content) === (message.text ?? "")) out.push(raw);
605
+ else out.push({ type: "message", role: "user", content: message.text ?? "" });
606
+ } else if (message.role === "assistant") {
607
+ if (message.contentType === "text") {
608
+ out.push({ type: "message", role: "assistant", content: message.text ?? "" });
609
+ } else if (message.contentType === "tool-call") {
610
+ const callId = message.toolCallId ?? `call_${message.id}`;
611
+ if (customToolCallIds.has(callId)) {
612
+ out.push({ type: "custom_tool_call", call_id: callId, name: message.toolName ?? "unknown", input: message.text ?? "", status: "completed" });
613
+ } else {
614
+ out.push({ type: "function_call", call_id: callId, name: message.toolName ?? "unknown", arguments: message.text ?? "" });
615
+ }
616
+ } else if (message.contentType === "reasoning") {
617
+ if (raw) out.push(raw);
618
+ }
619
+ } else if (message.role === "tool") {
620
+ const callId = message.toolCallId ?? "";
621
+ if (customToolCallIds.has(callId)) {
622
+ out.push({ type: "custom_tool_call_output", call_id: callId, output: message.text ?? "" });
623
+ } else {
624
+ out.push({ type: "function_call_output", call_id: callId, output: message.text ?? "" });
625
+ }
626
+ }
627
+ }
628
+ return out;
629
+ }
630
+ function injectResponsesDeveloperMessage(input, content) {
631
+ const items = typeof input === "string" ? [{ type: "message", role: "user", content: input }] : [...input];
632
+ let index = 0;
633
+ while (items[index]?.type === "additional_tools") index++;
634
+ items.splice(index, 0, { type: "message", role: "developer", content });
635
+ return items;
636
+ }
637
+ function conversationIdentityResponses(body, headerValue) {
638
+ if (headerValue?.trim()) return { value: headerValue.trim(), source: "header", clientProvided: true };
639
+ if (typeof body.session_id === "string" && body.session_id.trim()) {
640
+ return { value: body.session_id.trim(), source: "body-session", clientProvided: true };
641
+ }
642
+ const metadataSession = body.metadata?.session_id;
643
+ if (typeof metadataSession === "string" && metadataSession.trim()) {
644
+ return { value: metadataSession.trim(), source: "metadata-session", clientProvided: true };
645
+ }
646
+ if (typeof body.previous_response_id === "string" && body.previous_response_id.trim()) {
647
+ return { value: body.previous_response_id.trim(), source: "previous-response", clientProvided: false };
648
+ }
649
+ return { value: hashId(JSON.stringify(body.input ?? [])), source: "content-fingerprint", clientProvided: false };
650
+ }
651
+ function conversationSignalResponses(body, headerValue) {
652
+ return conversationIdentityResponses(body, headerValue).value;
653
+ }
654
+ export {
655
+ ClusterCounter,
656
+ anthropicToCore,
657
+ buildSystem,
658
+ conversationIdentityResponses,
659
+ conversationSignalAnthropic,
660
+ conversationSignalOpenai,
661
+ conversationSignalResponses,
662
+ coreToAnthropic,
663
+ coreToOpenai,
664
+ coreToResponses,
665
+ deriveMessageId,
666
+ extractSystem,
667
+ hashId,
668
+ injectOpenaiSystem,
669
+ injectResponsesDeveloperMessage,
670
+ openaiToCore,
671
+ parseDataUrl,
672
+ patchResponsesInput,
673
+ responsesToCore,
674
+ toCoreMessages
675
+ };
676
+ //# sourceMappingURL=index.js.map