@kenkaiiii/gg-ai 4.3.166 → 4.3.168

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/dist/index.cjs CHANGED
@@ -438,9 +438,18 @@ function toAnthropicToolResultContent(content) {
438
438
  };
439
439
  });
440
440
  }
441
+ function remapAnthropicToolCallId(id, idMap) {
442
+ if (/^[a-zA-Z0-9_-]+$/.test(id)) return id;
443
+ const existing = idMap.get(id);
444
+ if (existing) return existing;
445
+ const mapped = id.replace(/[^a-zA-Z0-9_-]/g, "_");
446
+ idMap.set(id, mapped);
447
+ return mapped;
448
+ }
441
449
  function toAnthropicMessages(messages, cacheControl) {
442
450
  let systemText;
443
451
  const out = [];
452
+ const idMap = /* @__PURE__ */ new Map();
444
453
  for (const msg of messages) {
445
454
  if (msg.role === "system") {
446
455
  systemText = msg.content;
@@ -475,7 +484,7 @@ function toAnthropicMessages(messages, cacheControl) {
475
484
  if (part.type === "tool_call")
476
485
  return {
477
486
  type: "tool_use",
478
- id: part.id,
487
+ id: remapAnthropicToolCallId(part.id, idMap),
479
488
  name: part.name,
480
489
  input: part.args
481
490
  };
@@ -500,7 +509,7 @@ function toAnthropicMessages(messages, cacheControl) {
500
509
  role: "user",
501
510
  content: msg.content.map((result) => ({
502
511
  type: "tool_result",
503
- tool_use_id: result.toolCallId,
512
+ tool_use_id: remapAnthropicToolCallId(result.toolCallId, idMap),
504
513
  content: toAnthropicToolResultContent(result.content),
505
514
  is_error: result.isError
506
515
  }))
@@ -556,12 +565,19 @@ function toAnthropicMessages(messages, cacheControl) {
556
565
  }
557
566
  return { system, messages: out };
558
567
  }
559
- function toAnthropicTools(tools) {
560
- return tools.map((tool) => ({
561
- name: tool.name,
562
- description: tool.description,
563
- input_schema: tool.rawInputSchema ?? zodToJsonSchema(tool.parameters)
564
- }));
568
+ function toAnthropicTools(tools, options) {
569
+ return tools.map((tool, index) => {
570
+ const anthropicTool = {
571
+ name: tool.name,
572
+ description: tool.description,
573
+ input_schema: tool.rawInputSchema ?? zodToJsonSchema(tool.parameters),
574
+ ...options?.enableFineGrainedToolStreaming ? { eager_input_streaming: true } : {}
575
+ };
576
+ if (options?.cacheControl && index === tools.length - 1) {
577
+ anthropicTool.cache_control = options.cacheControl;
578
+ }
579
+ return anthropicTool;
580
+ });
565
581
  }
566
582
  function toAnthropicToolChoice(choice) {
567
583
  if (choice === "auto") return { type: "auto" };
@@ -780,6 +796,7 @@ async function* runStream(options) {
780
796
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
781
797
  const useStreaming = options.streaming !== false;
782
798
  const cacheControl = toAnthropicCacheControl(options.cacheRetention, options.baseUrl);
799
+ const supportsFirstPartyToolExtras = !options.baseUrl || options.baseUrl.includes("api.anthropic.com");
783
800
  const downgradedMessages = downgradeUnsupportedImages(options.messages, options.supportsImages);
784
801
  const { system: rawSystem, messages } = toAnthropicMessages(downgradedMessages, cacheControl);
785
802
  const system = isOAuth ? [
@@ -812,7 +829,10 @@ async function* runStream(options) {
812
829
  ...options.stop ? { stop_sequences: options.stop } : {},
813
830
  ...options.tools?.length || options.serverTools?.length || options.webSearch ? {
814
831
  tools: [
815
- ...options.tools?.length ? toAnthropicTools(options.tools) : [],
832
+ ...options.tools?.length ? toAnthropicTools(options.tools, {
833
+ ...supportsFirstPartyToolExtras && cacheControl ? { cacheControl } : {},
834
+ ...supportsFirstPartyToolExtras ? { enableFineGrainedToolStreaming: true } : {}
835
+ }) : [],
816
836
  ...options.serverTools ?? [],
817
837
  ...options.webSearch ? [{ type: "web_search_20250305", name: "web_search" }] : []
818
838
  ]
@@ -1198,12 +1218,15 @@ async function* runStream2(options) {
1198
1218
  };
1199
1219
  if (options.provider === "openai" || options.provider === "moonshot") {
1200
1220
  const paramsAny = params;
1201
- paramsAny.prompt_cache_key = "ggcoder";
1221
+ paramsAny.prompt_cache_key = options.promptCacheKey ?? "ggcoder";
1202
1222
  const retention = options.cacheRetention ?? "short";
1203
1223
  if (retention === "long") {
1204
1224
  paramsAny.prompt_cache_retention = "24h";
1205
1225
  }
1206
1226
  }
1227
+ if (options.provider === "openai" && options.serviceTier) {
1228
+ params.service_tier = options.serviceTier;
1229
+ }
1207
1230
  if (usesThinkingParam) {
1208
1231
  if (options.thinking) {
1209
1232
  params.thinking = { type: "enabled" };
@@ -1486,6 +1509,9 @@ async function* runStream3(options) {
1486
1509
  if (options.tools?.length) {
1487
1510
  body.tools = toCodexTools(options.tools);
1488
1511
  }
1512
+ if (options.promptCacheKey) {
1513
+ body.prompt_cache_key = options.promptCacheKey;
1514
+ }
1489
1515
  if (options.temperature != null && !options.thinking) {
1490
1516
  body.temperature = options.temperature;
1491
1517
  }
@@ -1537,6 +1563,7 @@ async function* runStream3(options) {
1537
1563
  const toolCalls = /* @__PURE__ */ new Map();
1538
1564
  let inputTokens = 0;
1539
1565
  let outputTokens = 0;
1566
+ let cacheRead = 0;
1540
1567
  for await (const event of parseSSE(response.body)) {
1541
1568
  const type = event.type;
1542
1569
  if (!type) continue;
@@ -1629,7 +1656,8 @@ async function* runStream3(options) {
1629
1656
  const resp = event.response;
1630
1657
  const usage = resp?.usage;
1631
1658
  if (usage) {
1632
- inputTokens = usage.input_tokens ?? 0;
1659
+ cacheRead = usage.input_tokens_details?.cached_tokens ?? 0;
1660
+ inputTokens = (usage.input_tokens ?? 0) - cacheRead;
1633
1661
  outputTokens = usage.output_tokens ?? 0;
1634
1662
  }
1635
1663
  }
@@ -1659,7 +1687,7 @@ async function* runStream3(options) {
1659
1687
  content: contentParts.length > 0 ? contentParts : textAccum || ""
1660
1688
  },
1661
1689
  stopReason,
1662
- usage: { inputTokens, outputTokens }
1690
+ usage: { inputTokens, outputTokens, ...cacheRead > 0 && { cacheRead } }
1663
1691
  };
1664
1692
  yield { type: "done", stopReason };
1665
1693
  return streamResponse;