@kenkaiiii/gg-ai 4.3.2 → 4.3.4

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.js CHANGED
@@ -426,22 +426,37 @@ function normalizeOpenAIStopReason(reason) {
426
426
  }
427
427
 
428
428
  // src/providers/anthropic.ts
429
+ var clientCache = /* @__PURE__ */ new Map();
430
+ function getOrCreateClient(options) {
431
+ const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
432
+ const key = `${options.apiKey ?? ""}|${options.baseUrl ?? ""}|${isOAuth}`;
433
+ let client = clientCache.get(key);
434
+ if (!client) {
435
+ client = new Anthropic({
436
+ ...isOAuth ? { apiKey: null, authToken: options.apiKey } : { apiKey: options.apiKey },
437
+ ...options.baseUrl ? { baseURL: options.baseUrl } : {},
438
+ ...options.fetch ? { fetch: options.fetch } : {},
439
+ // Disable SDK-level retries — the agent loop handles retries itself with
440
+ // stall detection and context compaction. SDK retries on abort just cycle
441
+ // through the already-aborted signal, wasting time.
442
+ maxRetries: 0,
443
+ ...isOAuth ? {
444
+ defaultHeaders: {
445
+ "user-agent": "claude-cli/2.1.75",
446
+ "x-app": "cli"
447
+ }
448
+ } : {}
449
+ });
450
+ clientCache.set(key, client);
451
+ }
452
+ return client;
453
+ }
429
454
  function streamAnthropic(options) {
430
455
  return new StreamResult(runStream(options));
431
456
  }
432
457
  async function* runStream(options) {
458
+ const client = getOrCreateClient(options);
433
459
  const isOAuth = options.apiKey?.startsWith("sk-ant-oat");
434
- const client = new Anthropic({
435
- ...isOAuth ? { apiKey: null, authToken: options.apiKey } : { apiKey: options.apiKey },
436
- ...options.baseUrl ? { baseURL: options.baseUrl } : {},
437
- ...options.fetch ? { fetch: options.fetch } : {},
438
- ...isOAuth ? {
439
- defaultHeaders: {
440
- "user-agent": "claude-cli/2.1.75",
441
- "x-app": "cli"
442
- }
443
- } : {}
444
- });
445
460
  const cacheControl = toAnthropicCacheControl(options.cacheRetention, options.baseUrl);
446
461
  const { system: rawSystem, messages } = toAnthropicMessages(options.messages, cacheControl);
447
462
  const system = isOAuth ? [
@@ -705,16 +720,26 @@ function toError(err) {
705
720
 
706
721
  // src/providers/openai.ts
707
722
  import OpenAI from "openai";
723
+ var clientCache2 = /* @__PURE__ */ new Map();
724
+ function getOrCreateClient2(options) {
725
+ const key = `${options.apiKey ?? ""}|${options.baseUrl ?? ""}`;
726
+ let client = clientCache2.get(key);
727
+ if (!client) {
728
+ client = new OpenAI({
729
+ apiKey: options.apiKey,
730
+ ...options.baseUrl ? { baseURL: options.baseUrl } : {},
731
+ ...options.fetch ? { fetch: options.fetch } : {}
732
+ });
733
+ clientCache2.set(key, client);
734
+ }
735
+ return client;
736
+ }
708
737
  function streamOpenAI(options) {
709
738
  return new StreamResult(runStream2(options));
710
739
  }
711
740
  async function* runStream2(options) {
712
741
  const providerName = options.provider ?? "openai";
713
- const client = new OpenAI({
714
- apiKey: options.apiKey,
715
- ...options.baseUrl ? { baseURL: options.baseUrl } : {},
716
- ...options.fetch ? { fetch: options.fetch } : {}
717
- });
742
+ const client = getOrCreateClient2(options);
718
743
  const usesThinkingParam = options.provider === "glm" || options.provider === "moonshot" || options.provider === "xiaomi";
719
744
  const messages = toOpenAIMessages(options.messages, { provider: options.provider });
720
745
  const defaultTemp = options.provider === "glm" ? 0.6 : void 0;
@@ -723,7 +748,7 @@ async function* runStream2(options) {
723
748
  model: options.model,
724
749
  messages,
725
750
  stream: true,
726
- ...options.maxTokens ? options.provider === "xiaomi" ? { max_completion_tokens: options.maxTokens } : { max_tokens: options.maxTokens } : {},
751
+ ...options.maxTokens ? { max_tokens: options.maxTokens } : {},
727
752
  ...effectiveTemp != null && !options.thinking ? { temperature: effectiveTemp } : {},
728
753
  ...options.topP != null ? { top_p: options.topP } : {},
729
754
  ...options.stop ? { stop: options.stop } : {},
@@ -743,7 +768,7 @@ async function* runStream2(options) {
743
768
  if (usesThinkingParam) {
744
769
  if (options.thinking) {
745
770
  params.thinking = { type: "enabled" };
746
- } else if (options.provider !== "xiaomi") {
771
+ } else {
747
772
  params.thinking = { type: "disabled" };
748
773
  }
749
774
  }
@@ -792,7 +817,9 @@ async function* runStream2(options) {
792
817
  const reasoningContent = delta.reasoning_content;
793
818
  if (typeof reasoningContent === "string" && reasoningContent) {
794
819
  thinkingAccum += reasoningContent;
795
- yield { type: "thinking_delta", text: reasoningContent };
820
+ if (options.thinking) {
821
+ yield { type: "thinking_delta", text: reasoningContent };
822
+ }
796
823
  }
797
824
  if (delta.content) {
798
825
  textAccum += delta.content;