@kenkaiiii/ggcoder 5.83.4 → 5.83.5

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.
@@ -118,6 +118,8 @@ export class PaidModelConfirmationError extends Error {
118
118
  }
119
119
  export { resolveSessionToolResultCharLimit, resolveSessionTurnToolResultCharLimit, } from "./agent-session/helpers.js";
120
120
  // ── Agent Session ──────────────────────────────────────────
121
+ /** A prompt that asks for the live web (see AgentSession.attachWebSearchIfAsked). */
122
+ const WEB_INTENT = /\b(?:web|internet|online|google|googling|website|look (?:it |this |that )?up|(?:latest|newest|current) (?:version|release|news|docs?|documentation)|release notes|news)\b/i;
121
123
  export class AgentSession {
122
124
  eventBus = new EventBus();
123
125
  slashCommands = new SlashCommandRegistry();
@@ -361,6 +363,12 @@ export class AgentSession {
361
363
  async initialize() {
362
364
  // Set model for accurate token estimation
363
365
  setEstimatorModel(this.model);
366
+ // The Anthropic transport needs the current Claude Code version for its
367
+ // User-Agent before the first request. Without a fresh on-disk cache that
368
+ // is an npm round-trip (~0.3–3s) on the user's first prompt; start it now
369
+ // so it overlaps session setup and the user's typing instead.
370
+ if (this.provider === "anthropic")
371
+ void getClaudeCliUserAgent().catch(() => { });
364
372
  const paths = await ensureAppDirs();
365
373
  // Load settings & auth
366
374
  this.settingsManager = new SettingsManager(paths.settingsFile);
@@ -780,6 +788,45 @@ export class AgentSession {
780
788
  this.wrapToPromote("spawn_agent", ["wait_agent"]);
781
789
  this.wrapToPromote("bash", ["task_output", "task_send", "task_stop"], (args) => args.run_in_background === true);
782
790
  }
791
+ /**
792
+ * Whether provider-native web search rides along with this request.
793
+ *
794
+ * Anthropic's `web_search_20250305` server tool costs ~2.5k prefix tokens on
795
+ * every request (count_tokens: 6,628 vs 4,098 for the GG prefix without it),
796
+ * yet web search is reached in ~6% of sessions — the reason the client tool
797
+ * is deferred everywhere else. So on a tiered Anthropic session it is only
798
+ * attached once the model has promoted `web_search` (tool_search or a direct
799
+ * call). The server tool is appended last, so the tool block stays
800
+ * append-only. Untiered sessions and other providers keep it always on.
801
+ */
802
+ nativeWebSearchOn() {
803
+ if (this.provider !== "anthropic")
804
+ return true;
805
+ if (!this.deferredBuiltinToolNames.includes("web_search"))
806
+ return true;
807
+ return this.tools.some((t) => t.name === "web_search");
808
+ }
809
+ /**
810
+ * A human prompt that plainly asks for the web gets native web search from
811
+ * its first request. Without this, an on-demand web_search costs an extra
812
+ * round trip (tool_search or a client search) before the native tool is
813
+ * attached — slower than the always-on tool it replaced for exactly the
814
+ * sessions that need it. Conservative on purpose: a miss only costs that
815
+ * one discovery turn, a false hit only the ~2.5k-token schema.
816
+ */
817
+ attachWebSearchIfAsked(content) {
818
+ if (this.provider !== "anthropic" || this.nativeWebSearchOn())
819
+ return;
820
+ const text = typeof content === "string"
821
+ ? content
822
+ : Array.isArray(content)
823
+ ? content
824
+ .map((part) => part && typeof part === "object" && "text" in part ? String(part.text) : "")
825
+ .join(" ")
826
+ : "";
827
+ if (WEB_INTENT.test(text))
828
+ this.promoteDeferredBuiltin("web_search");
829
+ }
783
830
  /** Append a deferred built-in to the live tool list (append-only). */
784
831
  promoteDeferredBuiltin(name) {
785
832
  if (!this.deferredBuiltinToolNames.includes(name))
@@ -903,6 +950,8 @@ export class AgentSession {
903
950
  this.messages.push(userMessage);
904
951
  await this.persistMessage(userMessage);
905
952
  this.lastPersistedIndex = this.messages.length;
953
+ if (provenance.source === "human")
954
+ this.attachWebSearchIfAsked(content);
906
955
  await this.runLoop(options);
907
956
  }
908
957
  /**
@@ -920,6 +969,7 @@ export class AgentSession {
920
969
  const parts = this.buildAttachmentParts(text, attachments);
921
970
  if (parts.length === 0)
922
971
  return;
972
+ this.attachWebSearchIfAsked(text);
923
973
  const userMessage = {
924
974
  role: "user",
925
975
  content: parts,
@@ -1703,7 +1753,7 @@ export class AgentSession {
1703
1753
  provider: this.provider,
1704
1754
  model: this.model,
1705
1755
  tools: options.disableTools ? [] : this.tools,
1706
- webSearch: !options.disableTools,
1756
+ webSearch: options.disableTools ? false : () => this.nativeWebSearchOn(),
1707
1757
  maxTokens: this.requestMaxTokens(accountId),
1708
1758
  maxTurns: this.opts.maxTurns,
1709
1759
  maxTurnExtensions: this.opts.maxTurnExtensions,
@@ -1760,7 +1810,7 @@ export class AgentSession {
1760
1810
  thinking: this.planModeRef.current || options.capThinking
1761
1811
  ? clampThinkingForPlanMode(this.thinkingLevel)
1762
1812
  : this.thinkingLevel,
1763
- webSearch: !options.disableTools,
1813
+ webSearch: !options.disableTools && this.nativeWebSearchOn(),
1764
1814
  supportsImages: modelInfo?.supportsImages,
1765
1815
  promptCacheKey: this.getPromptCacheKey(),
1766
1816
  },
@@ -2118,7 +2168,13 @@ export class AgentSession {
2118
2168
  // Add/remove client-side web_search tool based on provider.
2119
2169
  // Anthropic has native server-side web search; all other providers need the client tool.
2120
2170
  const hasWebSearch = this.tools.some((t) => t.name === "web_search");
2121
- if (this.provider === "anthropic" && hasWebSearch) {
2171
+ if (this.deferredBuiltinToolNames.includes("web_search")) {
2172
+ // Tiered: the client tool lives in the catalog for every provider and,
2173
+ // once promoted, is swapped for the native server tool by the Anthropic
2174
+ // transport. Nothing to add or remove — doing so would either load the
2175
+ // schema eagerly or take away a capability the model already has.
2176
+ }
2177
+ else if (this.provider === "anthropic" && hasWebSearch) {
2122
2178
  // Switching TO anthropic — remove client-side web_search (server-side handles it)
2123
2179
  this.tools = this.tools.filter((t) => t.name !== "web_search");
2124
2180
  }
@@ -3566,7 +3622,7 @@ export class AgentSession {
3566
3622
  model: this.model,
3567
3623
  messages,
3568
3624
  tools: this.tools,
3569
- webSearch: true,
3625
+ webSearch: this.nativeWebSearchOn(),
3570
3626
  maxTokens: this.requestMaxTokens(creds.accountId),
3571
3627
  thinking: this.planModeRef.current
3572
3628
  ? clampThinkingForPlanMode(this.thinkingLevel)