@omnicross/core 0.2.0 → 0.3.0

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.
Files changed (119) hide show
  1. package/dist/{BuiltinToolExecutor-CS2WpXhM.d.cts → BuiltinToolExecutor-BHgH-EaZ.d.cts} +25 -2
  2. package/dist/{BuiltinToolExecutor-BluWyeob.d.ts → BuiltinToolExecutor-Bi1LmiTP.d.ts} +25 -2
  3. package/dist/{CompletionService-BiJqftc-.d.cts → CompletionService-BniP7j88.d.cts} +2 -2
  4. package/dist/{CompletionService-NXN_Av5W.d.ts → CompletionService-BqrKPwfA.d.ts} +2 -2
  5. package/dist/{ProviderProxy-CgIcCVwk.d.cts → ProviderProxy-Bz_nkOwq.d.cts} +1 -1
  6. package/dist/{ProviderProxy-DYoY5XoT.d.ts → ProviderProxy-Ckr6oTrE.d.ts} +1 -1
  7. package/dist/auth/GeminiCodeAssistProjectResolver.cjs +2 -2
  8. package/dist/auth/GeminiCodeAssistProjectResolver.js +1 -1
  9. package/dist/{chunk-FJNKXOWE.js → chunk-2C3TTBS7.js} +4 -4
  10. package/dist/{chunk-NZYN7C3P.cjs → chunk-2RCCLQYV.cjs} +53 -53
  11. package/dist/{chunk-CDHS2QDC.js → chunk-4PFWFODB.js} +53 -53
  12. package/dist/{chunk-OST5RJOJ.cjs → chunk-4Z3ZLLC2.cjs} +13 -7
  13. package/dist/chunk-7RJI3NDI.cjs +236 -0
  14. package/dist/chunk-BTBEJRGL.cjs +781 -0
  15. package/dist/chunk-DBBR6RHF.js +236 -0
  16. package/dist/chunk-DXWFBBSK.js +781 -0
  17. package/dist/chunk-E4QHVXV5.cjs +118 -0
  18. package/dist/chunk-L7O2YQ5O.js +386 -0
  19. package/dist/{chunk-WSJE42SG.js → chunk-LKDAN4D7.js} +3684 -2462
  20. package/dist/{chunk-DEIUR2TH.js → chunk-LQYS2N47.js} +12 -8
  21. package/dist/chunk-MZEGQSKQ.js +118 -0
  22. package/dist/chunk-PTU7SXM3.cjs +386 -0
  23. package/dist/{chunk-CTKM773J.cjs → chunk-UPQIWJIE.cjs} +12 -8
  24. package/dist/{chunk-CFSGYFOZ.js → chunk-VL5TUDTM.js} +7 -1
  25. package/dist/{chunk-7B56AJBB.cjs → chunk-VSYQ6XYN.cjs} +2233 -1011
  26. package/dist/{chunk-TVYA5JSQ.cjs → chunk-ZAPN7XA6.cjs} +5 -5
  27. package/dist/completion/BuiltinToolExecutor.cjs +2 -2
  28. package/dist/completion/BuiltinToolExecutor.d.cts +3 -1
  29. package/dist/completion/BuiltinToolExecutor.d.ts +3 -1
  30. package/dist/completion/BuiltinToolExecutor.js +1 -1
  31. package/dist/completion/CompletionService.cjs +11 -10
  32. package/dist/completion/CompletionService.d.cts +6 -3
  33. package/dist/completion/CompletionService.d.ts +6 -3
  34. package/dist/completion/CompletionService.js +10 -9
  35. package/dist/completion.cjs +11 -10
  36. package/dist/completion.d.cts +6 -3
  37. package/dist/completion.d.ts +6 -3
  38. package/dist/completion.js +10 -9
  39. package/dist/egress-I46O_p5L.d.cts +126 -0
  40. package/dist/egress-I46O_p5L.d.ts +126 -0
  41. package/dist/frontends-Doz0RaWu.d.cts +97 -0
  42. package/dist/frontends-Doz0RaWu.d.ts +97 -0
  43. package/dist/image-generation.cjs +2 -2
  44. package/dist/image-generation.d.cts +7 -4
  45. package/dist/image-generation.d.ts +7 -4
  46. package/dist/image-generation.js +1 -1
  47. package/dist/index.cjs +25 -10
  48. package/dist/index.d.cts +12 -8
  49. package/dist/index.d.ts +12 -8
  50. package/dist/index.js +24 -9
  51. package/dist/openai-operation.d.cts +4 -1
  52. package/dist/openai-operation.d.ts +4 -1
  53. package/dist/outbound-api/routeResolver.d.cts +9 -5
  54. package/dist/outbound-api/routeResolver.d.ts +9 -5
  55. package/dist/outbound-api/subscriptionRegistryPort.d.cts +4 -1
  56. package/dist/outbound-api/subscriptionRegistryPort.d.ts +4 -1
  57. package/dist/outbound-api/types.d.cts +8 -4
  58. package/dist/outbound-api/types.d.ts +8 -4
  59. package/dist/outbound-api.cjs +25 -10
  60. package/dist/outbound-api.d.cts +117 -8
  61. package/dist/outbound-api.d.ts +117 -8
  62. package/dist/outbound-api.js +24 -9
  63. package/dist/pipeline/AccountAllowanceScheduling.d.cts +8 -4
  64. package/dist/pipeline/AccountAllowanceScheduling.d.ts +8 -4
  65. package/dist/pipeline/upstreamFetch.cjs +2 -2
  66. package/dist/pipeline/upstreamFetch.js +1 -1
  67. package/dist/ports.d.cts +8 -4
  68. package/dist/ports.d.ts +8 -4
  69. package/dist/provider-proxy/ProviderProxy.cjs +11 -10
  70. package/dist/provider-proxy/ProviderProxy.d.cts +5 -2
  71. package/dist/provider-proxy/ProviderProxy.d.ts +5 -2
  72. package/dist/provider-proxy/ProviderProxy.js +10 -9
  73. package/dist/provider-proxy/ingress/providerProxyShared.cjs +13 -10
  74. package/dist/provider-proxy/ingress/providerProxyShared.d.cts +22 -3
  75. package/dist/provider-proxy/ingress/providerProxyShared.d.ts +22 -3
  76. package/dist/provider-proxy/ingress/providerProxyShared.js +12 -9
  77. package/dist/provider-proxy/types.d.cts +4 -1
  78. package/dist/provider-proxy/types.d.ts +4 -1
  79. package/dist/provider-proxy.cjs +11 -10
  80. package/dist/provider-proxy.d.cts +10 -6
  81. package/dist/provider-proxy.d.ts +10 -6
  82. package/dist/provider-proxy.js +10 -9
  83. package/dist/proxy-UZTtjPZx.d.cts +18 -0
  84. package/dist/proxy-UZTtjPZx.d.ts +18 -0
  85. package/dist/{routeResolver-rpKQLyDO.d.cts → routeResolver-Da0yvkL1.d.cts} +2 -2
  86. package/dist/{routeResolver-CTCcSHWu.d.ts → routeResolver-STwMz_G4.d.ts} +2 -2
  87. package/dist/runtime-5NCT3J3T.cjs +8 -0
  88. package/dist/runtime-JTHUWCBM.js +8 -0
  89. package/dist/runtime-t8AZacFF.d.cts +233 -0
  90. package/dist/runtime-t8AZacFF.d.ts +233 -0
  91. package/dist/search/api.cjs +766 -0
  92. package/dist/search/api.d.cts +385 -0
  93. package/dist/search/api.d.ts +385 -0
  94. package/dist/search/api.js +766 -0
  95. package/dist/search/http.cjs +37 -0
  96. package/dist/search/http.d.cts +323 -0
  97. package/dist/search/http.d.ts +323 -0
  98. package/dist/search/http.js +37 -0
  99. package/dist/search.cjs +57 -0
  100. package/dist/search.d.cts +113 -0
  101. package/dist/search.d.ts +113 -0
  102. package/dist/search.js +57 -0
  103. package/dist/transformer/transformers/AnthropicTransformer.cjs +3 -3
  104. package/dist/transformer/transformers/AnthropicTransformer.js +2 -2
  105. package/dist/transformer/transformers.cjs +7 -7
  106. package/dist/transformer/transformers.js +6 -6
  107. package/dist/transformer.cjs +6 -6
  108. package/dist/transformer.js +5 -5
  109. package/dist/{types-DsHBBk3h.d.cts → types-BSIJdsyl.d.cts} +15 -0
  110. package/dist/{types-CbglB73l.d.cts → types-Be-yt_6T.d.cts} +1 -1
  111. package/dist/{types-CVRge76p.d.ts → types-By9Tg6-I.d.ts} +15 -0
  112. package/dist/types-CbCZqLGg.d.cts +127 -0
  113. package/dist/types-CbCZqLGg.d.ts +127 -0
  114. package/dist/{types-jjU24eYG.d.cts → types-D-oHYusb.d.cts} +73 -4
  115. package/dist/{types-Bd72H4SB.d.ts → types-Dm4_36V4.d.ts} +1 -1
  116. package/dist/{types-5T223WYQ.d.ts → types-Dvlpjn0d.d.ts} +73 -4
  117. package/package.json +6 -3
  118. package/dist/{chunk-6OC3ORW6.js → chunk-EUZRH544.js} +3 -3
  119. package/dist/{chunk-SQMAEDNE.cjs → chunk-POUTD5AM.cjs} +2 -2
@@ -1,5 +1,6 @@
1
1
  import { MCPCallToolResponse, MCPTool } from '@omnicross/contracts/mcp-types';
2
2
  import { WebSearchBackend } from './ports/web-search-backend.cjs';
3
+ import { S as SearchRuntime } from './runtime-t8AZacFF.cjs';
3
4
 
4
5
  /**
5
6
  * Core-owned tool-shape types.
@@ -65,10 +66,32 @@ interface McpToolProvider {
65
66
 
66
67
  declare class BuiltinToolExecutor {
67
68
  private webSearch;
68
- constructor(webSearch: WebSearchBackend);
69
+ private runtime?;
70
+ /**
71
+ * @param webSearch - the legacy port, still the `web_fetch` URL reader.
72
+ * @param runtime - the search runtime `web_search` delegates to. Optional so
73
+ * the single-argument construction hosts already use keeps compiling; when
74
+ * omitted, a default runtime over the builtin HTTP providers is built on
75
+ * first use.
76
+ */
77
+ constructor(webSearch: WebSearchBackend, runtime?: SearchRuntime | undefined);
69
78
  execute(toolName: string, args: Record<string, unknown>): Promise<MCPCallToolResponse>;
70
79
  private executeWebSearch;
71
- private resolveProviderChain;
80
+ /** Memoized lazy construction, so concurrent first calls share one runtime. */
81
+ private runtimeInit?;
82
+ /**
83
+ * The runtime `web_search` delegates to.
84
+ *
85
+ * Imported lazily so the search stack's transport and HTML parsing stay out
86
+ * of the module graph of every consumer that only wants the tool definitions
87
+ * — the same reason `builtin-web-fetch` is loaded on demand below.
88
+ *
89
+ * The PROMISE is memoized, not just its result: two concurrent first searches
90
+ * would otherwise each build a runtime and the later one would win. Harmless
91
+ * while the default runtime is stateless, but it stops being harmless the
92
+ * moment one holds a connection pool or a cache.
93
+ */
94
+ private getRuntime;
72
95
  private executeWebFetch;
73
96
  }
74
97
  /** Get the built-in MCPTool definitions for web search tools. */
@@ -1,5 +1,6 @@
1
1
  import { MCPCallToolResponse, MCPTool } from '@omnicross/contracts/mcp-types';
2
2
  import { WebSearchBackend } from './ports/web-search-backend.js';
3
+ import { S as SearchRuntime } from './runtime-t8AZacFF.js';
3
4
 
4
5
  /**
5
6
  * Core-owned tool-shape types.
@@ -65,10 +66,32 @@ interface McpToolProvider {
65
66
 
66
67
  declare class BuiltinToolExecutor {
67
68
  private webSearch;
68
- constructor(webSearch: WebSearchBackend);
69
+ private runtime?;
70
+ /**
71
+ * @param webSearch - the legacy port, still the `web_fetch` URL reader.
72
+ * @param runtime - the search runtime `web_search` delegates to. Optional so
73
+ * the single-argument construction hosts already use keeps compiling; when
74
+ * omitted, a default runtime over the builtin HTTP providers is built on
75
+ * first use.
76
+ */
77
+ constructor(webSearch: WebSearchBackend, runtime?: SearchRuntime | undefined);
69
78
  execute(toolName: string, args: Record<string, unknown>): Promise<MCPCallToolResponse>;
70
79
  private executeWebSearch;
71
- private resolveProviderChain;
80
+ /** Memoized lazy construction, so concurrent first calls share one runtime. */
81
+ private runtimeInit?;
82
+ /**
83
+ * The runtime `web_search` delegates to.
84
+ *
85
+ * Imported lazily so the search stack's transport and HTML parsing stay out
86
+ * of the module graph of every consumer that only wants the tool definitions
87
+ * — the same reason `builtin-web-fetch` is loaded on demand below.
88
+ *
89
+ * The PROMISE is memoized, not just its result: two concurrent first searches
90
+ * would otherwise each build a runtime and the later one would win. Harmless
91
+ * while the default runtime is stateless, but it stops being harmless the
92
+ * moment one holds a connection pool or a cache.
93
+ */
94
+ private getRuntime;
72
95
  private executeWebFetch;
73
96
  }
74
97
  /** Get the built-in MCPTool definitions for web search tools. */
@@ -3,8 +3,8 @@ import { LLMProvider } from '@omnicross/contracts/llm-config';
3
3
  import { C as CorePaths, U as UsageEventSink } from './usage-event-sink-BX7FE1NL.cjs';
4
4
  import { L as Logger } from './logger-4GvQNzhE.cjs';
5
5
  import { ProviderConfigSource } from './ports/provider-config-source.cjs';
6
- import { U as UsageRecorderImport } from './types-DsHBBk3h.cjs';
7
- import { O as OpenAITool, A as AnthropicTool, G as GeminiTools, M as McpToolProvider, B as BuiltinToolExecutor } from './BuiltinToolExecutor-CS2WpXhM.cjs';
6
+ import { U as UsageRecorderImport } from './types-BSIJdsyl.cjs';
7
+ import { O as OpenAITool, A as AnthropicTool, G as GeminiTools, M as McpToolProvider, B as BuiltinToolExecutor } from './BuiltinToolExecutor-BHgH-EaZ.cjs';
8
8
  import { C as CompletionOptions, S as StreamCallbacks, a as CompletionResult } from './types-BScIHmPr.cjs';
9
9
  import { ApiKeyPoolService } from './completion/ApiKeyPoolService.cjs';
10
10
  import { MCPTool } from '@omnicross/contracts/mcp-types';
@@ -3,8 +3,8 @@ import { LLMProvider } from '@omnicross/contracts/llm-config';
3
3
  import { C as CorePaths, U as UsageEventSink } from './usage-event-sink-BX7FE1NL.js';
4
4
  import { L as Logger } from './logger-4GvQNzhE.js';
5
5
  import { ProviderConfigSource } from './ports/provider-config-source.js';
6
- import { U as UsageRecorderImport } from './types-CVRge76p.js';
7
- import { O as OpenAITool, A as AnthropicTool, G as GeminiTools, M as McpToolProvider, B as BuiltinToolExecutor } from './BuiltinToolExecutor-BluWyeob.js';
6
+ import { U as UsageRecorderImport } from './types-By9Tg6-I.js';
7
+ import { O as OpenAITool, A as AnthropicTool, G as GeminiTools, M as McpToolProvider, B as BuiltinToolExecutor } from './BuiltinToolExecutor-Bi1LmiTP.js';
8
8
  import { C as CompletionOptions, S as StreamCallbacks, a as CompletionResult } from './types-BScIHmPr.js';
9
9
  import { ApiKeyPoolService } from './completion/ApiKeyPoolService.js';
10
10
  import { MCPTool } from '@omnicross/contracts/mcp-types';
@@ -1,4 +1,4 @@
1
- import { aD as RouteContext, W as ProviderProxyDeps } from './types-DsHBBk3h.cjs';
1
+ import { aD as RouteContext, W as ProviderProxyDeps } from './types-BSIJdsyl.cjs';
2
2
 
3
3
  /**
4
4
  * ProviderProxyRouteMap — the per-run `Map<token, RouteContext>` with crypto
@@ -1,4 +1,4 @@
1
- import { aD as RouteContext, W as ProviderProxyDeps } from './types-CVRge76p.js';
1
+ import { aD as RouteContext, W as ProviderProxyDeps } from './types-By9Tg6-I.js';
2
2
 
3
3
  /**
4
4
  * ProviderProxyRouteMap — the per-run `Map<token, RouteContext>` with crypto
@@ -7,7 +7,7 @@ require('../chunk-77USGNRW.cjs');
7
7
  require('../chunk-IUL4VWNU.cjs');
8
8
 
9
9
 
10
- var _chunkOST5RJOJcjs = require('../chunk-OST5RJOJ.cjs');
10
+ var _chunk4Z3ZLLC2cjs = require('../chunk-4Z3ZLLC2.cjs');
11
11
  require('../chunk-235MXAGX.cjs');
12
12
  require('../chunk-HVBBDBS3.cjs');
13
13
  require('../chunk-NKVZ6Z3Y.cjs');
@@ -30,7 +30,7 @@ var GeminiCodeAssistHandshakeError = class extends Error {
30
30
  var GeminiCodeAssistProjectResolver = (_class = class {
31
31
  // upstream-proxy: the Code Assist handshake is upstream egress → honor the
32
32
  // gemini proxy by default (still injectable for tests).
33
- constructor(fetchImpl = (url, init) => _chunkOST5RJOJcjs.fetchUpstream.call(void 0, url, init, { providerId: "gemini" })) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
33
+ constructor(fetchImpl = (url, init) => _chunk4Z3ZLLC2cjs.fetchUpstream.call(void 0, url, init, { providerId: "gemini" })) {;_class.prototype.__init.call(this);_class.prototype.__init2.call(this);
34
34
  this.fetchImpl = fetchImpl;
35
35
  }
36
36
 
@@ -7,7 +7,7 @@ import "../chunk-WYVGULTQ.js";
7
7
  import "../chunk-ZLNHPUFA.js";
8
8
  import {
9
9
  fetchUpstream
10
- } from "../chunk-CFSGYFOZ.js";
10
+ } from "../chunk-VL5TUDTM.js";
11
11
  import "../chunk-3WD4HTR2.js";
12
12
  import "../chunk-MKF46GNZ.js";
13
13
  import "../chunk-GL6XM527.js";
@@ -1,3 +1,6 @@
1
+ import {
2
+ GeminiTransformer
3
+ } from "./chunk-HN74IGEO.js";
1
4
  import {
2
5
  OpenAIResponseTransformer
3
6
  } from "./chunk-V53ZEFXC.js";
@@ -6,10 +9,7 @@ import {
6
9
  } from "./chunk-7PIUVCKK.js";
7
10
  import {
8
11
  AnthropicTransformer
9
- } from "./chunk-6OC3ORW6.js";
10
- import {
11
- GeminiTransformer
12
- } from "./chunk-HN74IGEO.js";
12
+ } from "./chunk-EUZRH544.js";
13
13
  import {
14
14
  GeminiCodeAssistTransformer
15
15
  } from "./chunk-4VVCXVB7.js";
@@ -1,20 +1,7 @@
1
1
  "use strict";Object.defineProperty(exports, "__esModule", {value: true}); function _interopRequireWildcard(obj) { if (obj && obj.__esModule) { return obj; } else { var newObj = {}; if (obj != null) { for (var key in obj) { if (Object.prototype.hasOwnProperty.call(obj, key)) { newObj[key] = obj[key]; } } } newObj.default = obj; return newObj; } } function _nullishCoalesce(lhs, rhsFn) { if (lhs != null) { return lhs; } else { return rhsFn(); } }// src/completion/BuiltinToolExecutor.ts
2
+ var _searchtypes = require('@omnicross/contracts/search-types');
2
3
  var DEFAULT_SEARCH_COUNT = 5;
3
4
  var DEFAULT_FETCH_MAX_CHARS = 2e4;
4
- var FALLBACK_ORDER = [
5
- "tavily",
6
- "jina",
7
- "searxng",
8
- "zhipu",
9
- "z.ai",
10
- "bocha",
11
- "grok",
12
- "local-google",
13
- "local-bing",
14
- "local-baidu",
15
- "local-duckduckgo"
16
- ];
17
- var IMPLICIT_LOCAL_FALLBACK = "local-google";
18
5
  var WEB_SEARCH_TOOL = {
19
6
  id: "builtin__web_search",
20
7
  serverId: "builtin",
@@ -50,10 +37,19 @@ var WEB_FETCH_TOOL = {
50
37
  isBuiltIn: true
51
38
  };
52
39
  var BuiltinToolExecutor = class {
53
- constructor(webSearch) {
40
+ /**
41
+ * @param webSearch - the legacy port, still the `web_fetch` URL reader.
42
+ * @param runtime - the search runtime `web_search` delegates to. Optional so
43
+ * the single-argument construction hosts already use keeps compiling; when
44
+ * omitted, a default runtime over the builtin HTTP providers is built on
45
+ * first use.
46
+ */
47
+ constructor(webSearch, runtime) {
54
48
  this.webSearch = webSearch;
49
+ this.runtime = runtime;
55
50
  }
56
51
 
52
+
57
53
  async execute(toolName, args) {
58
54
  switch (toolName) {
59
55
  case "web_search":
@@ -73,47 +69,51 @@ var BuiltinToolExecutor = class {
73
69
  return { isError: true, content: [{ type: "text", text: "Error: query parameter is required." }] };
74
70
  }
75
71
  const count = Math.min(Math.max(Number(_nullishCoalesce(args.count, () => ( DEFAULT_SEARCH_COUNT))), 1), 10);
76
- const chain = this.resolveProviderChain();
77
- let lastError = "";
78
- for (const providerId of chain) {
79
- try {
80
- const result = await this.webSearch.search(query, providerId, { maxResults: count });
81
- if (!result.success) {
82
- lastError = result.error || "Unknown error";
83
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} failed: ${lastError}, trying next...`);
84
- continue;
85
- }
86
- const items = result.results.slice(0, count);
87
- if (items.length === 0) {
88
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} returned 0 results, trying next...`);
89
- continue;
90
- }
91
- const text = items.map((r, i) => `${i + 1}. ${r.title}
72
+ let response;
73
+ try {
74
+ response = await (await this.getRuntime()).search({
75
+ query,
76
+ options: { maxResults: count }
77
+ });
78
+ } catch (err) {
79
+ const shape = _searchtypes.toSearchErrorShape.call(void 0, err);
80
+ return {
81
+ isError: false,
82
+ content: [{ type: "text", text: `Search failed (${shape.code}): ${shape.message}` }]
83
+ };
84
+ }
85
+ if (response.results.length === 0) {
86
+ return {
87
+ isError: false,
88
+ content: [{ type: "text", text: `No results found for "${query}" after trying ${response.attempts.length} provider(s). Try rephrasing your search.` }]
89
+ };
90
+ }
91
+ const text = response.results.map((r, i) => `${i + 1}. ${r.title}
92
92
  URL: ${r.url}
93
93
  ${r.content || ""}`).join("\n\n");
94
- return { isError: false, content: [{ type: "text", text }] };
95
- } catch (err) {
96
- lastError = err instanceof Error ? err.message : String(err);
97
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} threw: ${lastError}, trying next...`);
98
- continue;
99
- }
100
- }
101
- return {
102
- isError: false,
103
- content: [{ type: "text", text: lastError ? `Search failed after trying ${chain.length} provider(s). Last error: ${lastError}` : `No results found for "${query}" after trying ${chain.length} provider(s). Try rephrasing your search.` }]
104
- };
94
+ return { isError: false, content: [{ type: "text", text }] };
105
95
  }
106
- resolveProviderChain() {
107
- const chain = [];
108
- for (const id of FALLBACK_ORDER) {
109
- if (this.webSearch.isProviderEnabled(id)) {
110
- chain.push(id);
111
- }
112
- }
113
- if (!chain.includes(IMPLICIT_LOCAL_FALLBACK)) {
114
- chain.push(IMPLICIT_LOCAL_FALLBACK);
115
- }
116
- return chain;
96
+ /** Memoized lazy construction, so concurrent first calls share one runtime. */
97
+
98
+ /**
99
+ * The runtime `web_search` delegates to.
100
+ *
101
+ * Imported lazily so the search stack's transport and HTML parsing stay out
102
+ * of the module graph of every consumer that only wants the tool definitions
103
+ * — the same reason `builtin-web-fetch` is loaded on demand below.
104
+ *
105
+ * The PROMISE is memoized, not just its result: two concurrent first searches
106
+ * would otherwise each build a runtime and the later one would win. Harmless
107
+ * while the default runtime is stateless, but it stops being harmless the
108
+ * moment one holds a connection pool or a cache.
109
+ */
110
+ getRuntime() {
111
+ if (this.runtime !== void 0) return Promise.resolve(this.runtime);
112
+ this.runtimeInit ??= Promise.resolve().then(() => _interopRequireWildcard(require("./runtime-5NCT3J3T.cjs"))).then(({ createSearchRuntime }) => {
113
+ this.runtime = createSearchRuntime();
114
+ return this.runtime;
115
+ });
116
+ return this.runtimeInit;
117
117
  }
118
118
  // -----------------------------------------------------------------------
119
119
  // web_fetch
@@ -1,20 +1,7 @@
1
1
  // src/completion/BuiltinToolExecutor.ts
2
+ import { toSearchErrorShape } from "@omnicross/contracts/search-types";
2
3
  var DEFAULT_SEARCH_COUNT = 5;
3
4
  var DEFAULT_FETCH_MAX_CHARS = 2e4;
4
- var FALLBACK_ORDER = [
5
- "tavily",
6
- "jina",
7
- "searxng",
8
- "zhipu",
9
- "z.ai",
10
- "bocha",
11
- "grok",
12
- "local-google",
13
- "local-bing",
14
- "local-baidu",
15
- "local-duckduckgo"
16
- ];
17
- var IMPLICIT_LOCAL_FALLBACK = "local-google";
18
5
  var WEB_SEARCH_TOOL = {
19
6
  id: "builtin__web_search",
20
7
  serverId: "builtin",
@@ -50,10 +37,19 @@ var WEB_FETCH_TOOL = {
50
37
  isBuiltIn: true
51
38
  };
52
39
  var BuiltinToolExecutor = class {
53
- constructor(webSearch) {
40
+ /**
41
+ * @param webSearch - the legacy port, still the `web_fetch` URL reader.
42
+ * @param runtime - the search runtime `web_search` delegates to. Optional so
43
+ * the single-argument construction hosts already use keeps compiling; when
44
+ * omitted, a default runtime over the builtin HTTP providers is built on
45
+ * first use.
46
+ */
47
+ constructor(webSearch, runtime) {
54
48
  this.webSearch = webSearch;
49
+ this.runtime = runtime;
55
50
  }
56
51
  webSearch;
52
+ runtime;
57
53
  async execute(toolName, args) {
58
54
  switch (toolName) {
59
55
  case "web_search":
@@ -73,47 +69,51 @@ var BuiltinToolExecutor = class {
73
69
  return { isError: true, content: [{ type: "text", text: "Error: query parameter is required." }] };
74
70
  }
75
71
  const count = Math.min(Math.max(Number(args.count ?? DEFAULT_SEARCH_COUNT), 1), 10);
76
- const chain = this.resolveProviderChain();
77
- let lastError = "";
78
- for (const providerId of chain) {
79
- try {
80
- const result = await this.webSearch.search(query, providerId, { maxResults: count });
81
- if (!result.success) {
82
- lastError = result.error || "Unknown error";
83
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} failed: ${lastError}, trying next...`);
84
- continue;
85
- }
86
- const items = result.results.slice(0, count);
87
- if (items.length === 0) {
88
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} returned 0 results, trying next...`);
89
- continue;
90
- }
91
- const text = items.map((r, i) => `${i + 1}. ${r.title}
72
+ let response;
73
+ try {
74
+ response = await (await this.getRuntime()).search({
75
+ query,
76
+ options: { maxResults: count }
77
+ });
78
+ } catch (err) {
79
+ const shape = toSearchErrorShape(err);
80
+ return {
81
+ isError: false,
82
+ content: [{ type: "text", text: `Search failed (${shape.code}): ${shape.message}` }]
83
+ };
84
+ }
85
+ if (response.results.length === 0) {
86
+ return {
87
+ isError: false,
88
+ content: [{ type: "text", text: `No results found for "${query}" after trying ${response.attempts.length} provider(s). Try rephrasing your search.` }]
89
+ };
90
+ }
91
+ const text = response.results.map((r, i) => `${i + 1}. ${r.title}
92
92
  URL: ${r.url}
93
93
  ${r.content || ""}`).join("\n\n");
94
- return { isError: false, content: [{ type: "text", text }] };
95
- } catch (err) {
96
- lastError = err instanceof Error ? err.message : String(err);
97
- console.log(`[BuiltinToolExecutor] Search provider ${providerId} threw: ${lastError}, trying next...`);
98
- continue;
99
- }
100
- }
101
- return {
102
- isError: false,
103
- content: [{ type: "text", text: lastError ? `Search failed after trying ${chain.length} provider(s). Last error: ${lastError}` : `No results found for "${query}" after trying ${chain.length} provider(s). Try rephrasing your search.` }]
104
- };
94
+ return { isError: false, content: [{ type: "text", text }] };
105
95
  }
106
- resolveProviderChain() {
107
- const chain = [];
108
- for (const id of FALLBACK_ORDER) {
109
- if (this.webSearch.isProviderEnabled(id)) {
110
- chain.push(id);
111
- }
112
- }
113
- if (!chain.includes(IMPLICIT_LOCAL_FALLBACK)) {
114
- chain.push(IMPLICIT_LOCAL_FALLBACK);
115
- }
116
- return chain;
96
+ /** Memoized lazy construction, so concurrent first calls share one runtime. */
97
+ runtimeInit;
98
+ /**
99
+ * The runtime `web_search` delegates to.
100
+ *
101
+ * Imported lazily so the search stack's transport and HTML parsing stay out
102
+ * of the module graph of every consumer that only wants the tool definitions
103
+ * — the same reason `builtin-web-fetch` is loaded on demand below.
104
+ *
105
+ * The PROMISE is memoized, not just its result: two concurrent first searches
106
+ * would otherwise each build a runtime and the later one would win. Harmless
107
+ * while the default runtime is stateless, but it stops being harmless the
108
+ * moment one holds a connection pool or a cache.
109
+ */
110
+ getRuntime() {
111
+ if (this.runtime !== void 0) return Promise.resolve(this.runtime);
112
+ this.runtimeInit ??= import("./runtime-JTHUWCBM.js").then(({ createSearchRuntime }) => {
113
+ this.runtime = createSearchRuntime();
114
+ return this.runtime;
115
+ });
116
+ return this.runtimeInit;
117
117
  }
118
118
  // -----------------------------------------------------------------------
119
119
  // web_fetch
@@ -32,6 +32,12 @@ function bumpUpstreamProxyGeneration() {
32
32
  }
33
33
  }
34
34
  }
35
+ function isClientDisconnectAbort(signal) {
36
+ if (!_optionalChain([signal, 'optionalAccess', _3 => _3.aborted])) return false;
37
+ const reason = signal.reason;
38
+ if (!reason || typeof reason !== "object") return false;
39
+ return reason.code === "client_disconnect";
40
+ }
35
41
  function normalizeProxy(cfg) {
36
42
  if ("url" in cfg) {
37
43
  let u;
@@ -110,7 +116,7 @@ function fetchUpstream(url, init, ctx) {
110
116
  const dispatcher = resolveUpstreamDispatcher({ ..._nullishCoalesce(ctx, () => ( {})), url });
111
117
  const doFetch = dispatcher ? () => fetch(url, { ...init, dispatcher }) : () => fetch(url, init);
112
118
  const doFetchWithAllowanceTap = () => doFetch().then((response) => {
113
- if (_optionalChain([ctx, 'optionalAccess', _3 => _3.providerId]) === "codex" && ctx.accountId) {
119
+ if (_optionalChain([ctx, 'optionalAccess', _4 => _4.providerId]) === "codex" && ctx.accountId) {
114
120
  try {
115
121
  _chunkHVBBDBS3cjs.getSharedAccountAllowanceStore.call(void 0, ).recordCodexHeaders(ctx.accountId, response.headers);
116
122
  } catch (e4) {
@@ -118,10 +124,10 @@ function fetchUpstream(url, init, ctx) {
118
124
  }
119
125
  return response;
120
126
  });
121
- const activity = _optionalChain([ctx, 'optionalAccess', _4 => _4.routeActivity]);
122
- const activityStartedAt = activity && _optionalChain([ctx, 'optionalAccess', _5 => _5.providerId]) && ctx.accountId ? Date.now() : 0;
127
+ const activity = _optionalChain([ctx, 'optionalAccess', _5 => _5.routeActivity]);
128
+ const activityStartedAt = activity && _optionalChain([ctx, 'optionalAccess', _6 => _6.providerId]) && ctx.accountId ? Date.now() : 0;
123
129
  const recordActivity = (status) => {
124
- if (!activity || !_optionalChain([ctx, 'optionalAccess', _6 => _6.providerId]) || !ctx.accountId) return;
130
+ if (!activity || !_optionalChain([ctx, 'optionalAccess', _7 => _7.providerId]) || !ctx.accountId) return;
125
131
  const record = _chunkNKVZ6Z3Ycjs.getSharedAccountRouteActivity.call(void 0, ).record({
126
132
  providerId: ctx.providerId,
127
133
  accountId: ctx.accountId,
@@ -134,7 +140,7 @@ function fetchUpstream(url, init, ctx) {
134
140
  ts: activityStartedAt
135
141
  });
136
142
  try {
137
- _optionalChain([activity, 'access', _7 => _7.onRecorded, 'optionalCall', _8 => _8(record)]);
143
+ _optionalChain([activity, 'access', _8 => _8.onRecorded, 'optionalCall', _9 => _9(record)]);
138
144
  } catch (e5) {
139
145
  }
140
146
  };
@@ -144,12 +150,12 @@ function fetchUpstream(url, init, ctx) {
144
150
  return response;
145
151
  },
146
152
  (error) => {
147
- recordActivity(0);
153
+ if (!isClientDisconnectAbort(init.signal)) recordActivity(0);
148
154
  throw error;
149
155
  }
150
156
  );
151
157
  const tracePath = _chunk235MXAGXcjs.getUpstreamTracePath.call(void 0, );
152
- if (!tracePath || !_optionalChain([ctx, 'optionalAccess', _9 => _9.providerId])) return doFetchWithActivity();
158
+ if (!tracePath || !_optionalChain([ctx, 'optionalAccess', _10 => _10.providerId])) return doFetchWithActivity();
153
159
  const startedAt = Date.now();
154
160
  const method = typeof init.method === "string" ? init.method : "GET";
155
161
  const requestHeaders = _chunk235MXAGXcjs.redactHeaders.call(void 0,