@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.d.cts CHANGED
@@ -157,6 +157,10 @@ interface StreamOptions {
157
157
  signal?: AbortSignal;
158
158
  /** Prompt cache retention preference. Providers map this to their supported values. Default: "short". */
159
159
  cacheRetention?: CacheRetention;
160
+ /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot). */
161
+ promptCacheKey?: string;
162
+ /** OpenAI service tier for latency-sensitive requests. Only sent to first-party OpenAI API calls. */
163
+ serviceTier?: "auto" | "default" | "flex" | "priority";
160
164
  /** OpenAI ChatGPT account ID (from OAuth JWT) for codex endpoint */
161
165
  accountId?: string;
162
166
  /** Enable provider-native web search. Each provider uses its own format:
package/dist/index.d.ts CHANGED
@@ -157,6 +157,10 @@ interface StreamOptions {
157
157
  signal?: AbortSignal;
158
158
  /** Prompt cache retention preference. Providers map this to their supported values. Default: "short". */
159
159
  cacheRetention?: CacheRetention;
160
+ /** Stable per-session cache routing key for providers that support it (OpenAI, Moonshot). */
161
+ promptCacheKey?: string;
162
+ /** OpenAI service tier for latency-sensitive requests. Only sent to first-party OpenAI API calls. */
163
+ serviceTier?: "auto" | "default" | "flex" | "priority";
160
164
  /** OpenAI ChatGPT account ID (from OAuth JWT) for codex endpoint */
161
165
  accountId?: string;
162
166
  /** Enable provider-native web search. Each provider uses its own format:
package/dist/index.js CHANGED
@@ -390,9 +390,18 @@ function toAnthropicToolResultContent(content) {
390
390
  };
391
391
  });
392
392
  }
393
+ function remapAnthropicToolCallId(id, idMap) {
394
+ if (/^[a-zA-Z0-9_-]+$/.test(id)) return id;
395
+ const existing = idMap.get(id);
396
+ if (existing) return existing;
397
+ const mapped = id.replace(/[^a-zA-Z0-9_-]/g, "_");
398
+ idMap.set(id, mapped);
399
+ return mapped;
400
+ }
393
401
  function toAnthropicMessages(messages, cacheControl) {
394
402
  let systemText;
395
403
  const out = [];
404
+ const idMap = /* @__PURE__ */ new Map();
396
405
  for (const msg of messages) {
397
406
  if (msg.role === "system") {
398
407
  systemText = msg.content;
@@ -427,7 +436,7 @@ function toAnthropicMessages(messages, cacheControl) {
427
436
  if (part.type === "tool_call")
428
437
  return {
429
438
  type: "tool_use",
430
- id: part.id,
439
+ id: remapAnthropicToolCallId(part.id, idMap),
431
440
  name: part.name,
432
441
  input: part.args
433
442
  };
@@ -452,7 +461,7 @@ function toAnthropicMessages(messages, cacheControl) {
452
461
  role: "user",
453
462
  content: msg.content.map((result) => ({
454
463
  type: "tool_result",
455
- tool_use_id: result.toolCallId,
464
+ tool_use_id: remapAnthropicToolCallId(result.toolCallId, idMap),
456
465
  content: toAnthropicToolResultContent(result.content),
457
466
  is_error: result.isError
458
467
  }))
@@ -508,12 +517,19 @@ function toAnthropicMessages(messages, cacheControl) {
508
517
  }
509
518
  return { system, messages: out };
510
519
  }
511
- function toAnthropicTools(tools) {
512
- return tools.map((tool) => ({
513
- name: tool.name,
514
- description: tool.description,
515
- input_schema: tool.rawInputSchema ?? zodToJsonSchema(tool.parameters)
516
- }));
520
+ function toAnthropicTools(tools, options) {
521
+ return tools.map((tool, index) => {
522
+ const anthropicTool = {
523
+ name: tool.name,
524
+ description: tool.description,
525
+ input_schema: tool.rawInputSchema ?? zodToJsonSchema(tool.parameters),
526
+ ...options?.enableFineGrainedToolStreaming ? { eager_input_streaming: true } : {}
527
+ };
528
+ if (options?.cacheControl && index === tools.length - 1) {
529
+ anthropicTool.cache_control = options.cacheControl;
530
+ }
531
+ return anthropicTool;
532
+ });
517
533
  }
518
534
  function toAnthropicToolChoice(choice) {
519
535
  if (choice === "auto") return { type: "auto" };
@@ -732,6 +748,7 @@ async function* runStream(options) {
732
748
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
733
749
  const useStreaming = options.streaming !== false;
734
750
  const cacheControl = toAnthropicCacheControl(options.cacheRetention, options.baseUrl);
751
+ const supportsFirstPartyToolExtras = !options.baseUrl || options.baseUrl.includes("api.anthropic.com");
735
752
  const downgradedMessages = downgradeUnsupportedImages(options.messages, options.supportsImages);
736
753
  const { system: rawSystem, messages } = toAnthropicMessages(downgradedMessages, cacheControl);
737
754
  const system = isOAuth ? [
@@ -764,7 +781,10 @@ async function* runStream(options) {
764
781
  ...options.stop ? { stop_sequences: options.stop } : {},
765
782
  ...options.tools?.length || options.serverTools?.length || options.webSearch ? {
766
783
  tools: [
767
- ...options.tools?.length ? toAnthropicTools(options.tools) : [],
784
+ ...options.tools?.length ? toAnthropicTools(options.tools, {
785
+ ...supportsFirstPartyToolExtras && cacheControl ? { cacheControl } : {},
786
+ ...supportsFirstPartyToolExtras ? { enableFineGrainedToolStreaming: true } : {}
787
+ }) : [],
768
788
  ...options.serverTools ?? [],
769
789
  ...options.webSearch ? [{ type: "web_search_20250305", name: "web_search" }] : []
770
790
  ]
@@ -1150,12 +1170,15 @@ async function* runStream2(options) {
1150
1170
  };
1151
1171
  if (options.provider === "openai" || options.provider === "moonshot") {
1152
1172
  const paramsAny = params;
1153
- paramsAny.prompt_cache_key = "ggcoder";
1173
+ paramsAny.prompt_cache_key = options.promptCacheKey ?? "ggcoder";
1154
1174
  const retention = options.cacheRetention ?? "short";
1155
1175
  if (retention === "long") {
1156
1176
  paramsAny.prompt_cache_retention = "24h";
1157
1177
  }
1158
1178
  }
1179
+ if (options.provider === "openai" && options.serviceTier) {
1180
+ params.service_tier = options.serviceTier;
1181
+ }
1159
1182
  if (usesThinkingParam) {
1160
1183
  if (options.thinking) {
1161
1184
  params.thinking = { type: "enabled" };
@@ -1438,6 +1461,9 @@ async function* runStream3(options) {
1438
1461
  if (options.tools?.length) {
1439
1462
  body.tools = toCodexTools(options.tools);
1440
1463
  }
1464
+ if (options.promptCacheKey) {
1465
+ body.prompt_cache_key = options.promptCacheKey;
1466
+ }
1441
1467
  if (options.temperature != null && !options.thinking) {
1442
1468
  body.temperature = options.temperature;
1443
1469
  }
@@ -1489,6 +1515,7 @@ async function* runStream3(options) {
1489
1515
  const toolCalls = /* @__PURE__ */ new Map();
1490
1516
  let inputTokens = 0;
1491
1517
  let outputTokens = 0;
1518
+ let cacheRead = 0;
1492
1519
  for await (const event of parseSSE(response.body)) {
1493
1520
  const type = event.type;
1494
1521
  if (!type) continue;
@@ -1581,7 +1608,8 @@ async function* runStream3(options) {
1581
1608
  const resp = event.response;
1582
1609
  const usage = resp?.usage;
1583
1610
  if (usage) {
1584
- inputTokens = usage.input_tokens ?? 0;
1611
+ cacheRead = usage.input_tokens_details?.cached_tokens ?? 0;
1612
+ inputTokens = (usage.input_tokens ?? 0) - cacheRead;
1585
1613
  outputTokens = usage.output_tokens ?? 0;
1586
1614
  }
1587
1615
  }
@@ -1611,7 +1639,7 @@ async function* runStream3(options) {
1611
1639
  content: contentParts.length > 0 ? contentParts : textAccum || ""
1612
1640
  },
1613
1641
  stopReason,
1614
- usage: { inputTokens, outputTokens }
1642
+ usage: { inputTokens, outputTokens, ...cacheRead > 0 && { cacheRead } }
1615
1643
  };
1616
1644
  yield { type: "done", stopReason };
1617
1645
  return streamResponse;