@johpaz/hive-agents 1.0.3 → 1.0.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.
Files changed (179) hide show
  1. package/README.md +4 -4
  2. package/dist/hive.js +43711 -43411
  3. package/dist/ui/assets/A2UIPage-7Ry5rHh9.js +8 -0
  4. package/dist/ui/assets/AgentCreateForm-Dh5W0q_t.js +1 -0
  5. package/dist/ui/assets/AgentDetailPage-CIoPjtsS.js +1 -0
  6. package/dist/ui/assets/AgentNewPage-BNsZcJQf.js +1 -0
  7. package/dist/ui/assets/AgentsPage-YZrDmgNI.js +10 -0
  8. package/dist/ui/assets/ApiClientPage-6a6KFwsQ.js +3 -0
  9. package/dist/ui/assets/ChannelsPage-Dice4dkJ.js +8 -0
  10. package/dist/ui/assets/DashboardPage-B9r0sc0F.js +6 -0
  11. package/dist/ui/assets/LoginPage-BkAAX_tu.js +1 -0
  12. package/dist/ui/assets/{LogsPage-DyJOGQNy.js → LogsPage-8-H1OJs3.js} +1 -1
  13. package/dist/ui/assets/MeetingPage-14JoQo71.js +1 -0
  14. package/dist/ui/assets/ModelSelector-CrgJQ7DG.js +1 -0
  15. package/dist/ui/assets/{NotFound-Bpr12V51.js → NotFound-B06kVW7N.js} +1 -1
  16. package/dist/ui/assets/{Office3DPage-BvO66B0h.js → Office3DPage-BMUZWkFS.js} +2 -2
  17. package/dist/ui/assets/ProvidersPage-BiL3QxCs.js +1 -0
  18. package/dist/ui/assets/RecoverPage-D1o8yxNz.js +1 -0
  19. package/dist/ui/assets/SettingsPage-GP8UwQIL.js +9 -0
  20. package/dist/ui/assets/SetupPage-B_gU1YQU.js +1 -0
  21. package/dist/ui/assets/WebChatPage-Yahufnth.js +29 -0
  22. package/dist/ui/assets/activity-C_GS5wrG.js +1 -0
  23. package/dist/ui/assets/agentCapabilities-Dg72UAFj.js +1 -0
  24. package/dist/ui/assets/{alert-CQlRq6sv.js → alert-DChoEVJw.js} +1 -1
  25. package/dist/ui/assets/arrow-left-WyDPYZXU.js +1 -0
  26. package/dist/ui/assets/arrow-right-BuNjN-bk.js +1 -0
  27. package/dist/ui/assets/arrow-up-right-DWzTsCXj.js +1 -0
  28. package/dist/ui/assets/{badge-DKGZbVZJ.js → badge-uRjRSt86.js} +1 -1
  29. package/dist/ui/assets/calendar-B6x9cw-q.js +1 -0
  30. package/dist/ui/assets/{card-D3Nw3kh3.js → card-DwQizslL.js} +1 -1
  31. package/dist/ui/assets/check-C8pw-7Fx.js +1 -0
  32. package/dist/ui/assets/chevron-down-8GjmTjLR.js +1 -0
  33. package/dist/ui/assets/chevron-left-GK4EQOyZ.js +1 -0
  34. package/dist/ui/assets/chevron-right-BupZqrxu.js +1 -0
  35. package/dist/ui/assets/chevron-up-DZrzJIW8.js +1 -0
  36. package/dist/ui/assets/circle-alert-CtDbK2nl.js +1 -0
  37. package/dist/ui/assets/circle-check-BKNT2_q6.js +1 -0
  38. package/dist/ui/assets/circle-x-DfBjbztU.js +1 -0
  39. package/dist/ui/assets/copy-wR7o7Ly8.js +1 -0
  40. package/dist/ui/assets/core-Sjunb8Z1.js +1 -0
  41. package/dist/ui/assets/createLucideIcon-BRaPSYgN.js +1 -0
  42. package/dist/ui/assets/{dialog-Dn9hGPYl.js → dialog--mljnfAi.js} +1 -1
  43. package/dist/ui/assets/dist-js-B0s_TDau.js +1 -0
  44. package/dist/ui/assets/dist-js-CLxM1g2B.js +1 -0
  45. package/dist/ui/assets/download-D0aDV8Y_.js +1 -0
  46. package/dist/ui/assets/earth-BJRGJbVn.js +1 -0
  47. package/dist/ui/assets/ellipsis-vertical-BOW_iEOO.js +1 -0
  48. package/dist/ui/assets/external-link-5cujQzfV.js +1 -0
  49. package/dist/ui/assets/eye-sRPY5qOa.js +1 -0
  50. package/dist/ui/assets/file-text-DNSyNLUh.js +1 -0
  51. package/dist/ui/assets/gauge-C__zxC3_.js +1 -0
  52. package/dist/ui/assets/history-BHQWfE-7.js +1 -0
  53. package/dist/ui/assets/index-BHHUQZJd.js +116 -0
  54. package/dist/ui/assets/index-CEFhf4dF.css +2 -0
  55. package/dist/ui/assets/info-CG1VohyH.js +1 -0
  56. package/dist/ui/assets/key-BC28VCjO.js +1 -0
  57. package/dist/ui/assets/{label-k0jYxoWW.js → label-BceoUB2w.js} +1 -1
  58. package/dist/ui/assets/lock-open-Bhp0UJlL.js +1 -0
  59. package/dist/ui/assets/mail-Biwn8WrV.js +1 -0
  60. package/dist/ui/assets/pause-C2l5G2Py.js +1 -0
  61. package/dist/ui/assets/pencil-CUbpCfeG.js +1 -0
  62. package/dist/ui/assets/play-CMAscR3a.js +1 -0
  63. package/dist/ui/assets/plus-DzvRxH4U.js +1 -0
  64. package/dist/ui/assets/radio-DfGzov4y.js +1 -0
  65. package/dist/ui/assets/refresh-cw-DZXu5FrG.js +1 -0
  66. package/dist/ui/assets/scroll-area-DMEaZZUp.js +1 -0
  67. package/dist/ui/assets/search-ykSx-zZU.js +1 -0
  68. package/dist/ui/assets/{select-ot0a6art.js → select-BBEBtcLm.js} +1 -1
  69. package/dist/ui/assets/send-DAhlz2i9.js +1 -0
  70. package/dist/ui/assets/settings-DL0REysC.js +1 -0
  71. package/dist/ui/assets/shield-BPZZhjCT.js +1 -0
  72. package/dist/ui/assets/slider-DcmBdvYD.js +1 -0
  73. package/dist/ui/assets/sparkles-BViXTAh7.js +1 -0
  74. package/dist/ui/assets/square-psq7k_CB.js +1 -0
  75. package/dist/ui/assets/{switch-DXcELPsM.js → switch-CgiDezVR.js} +1 -1
  76. package/dist/ui/assets/tabs-gUXIjjZv.js +1 -0
  77. package/dist/ui/assets/terminal-CzxPKG-h.js +1 -0
  78. package/dist/ui/assets/{textarea-rp6vsOZl.js → textarea-DedxQYBx.js} +1 -1
  79. package/dist/ui/assets/trash-2-SgYCWiVk.js +1 -0
  80. package/dist/ui/assets/{useHiveAgentsModelLoad-BjnIsQKR.js → useHiveAgentsModelLoad-C7bhWPDG.js} +1 -1
  81. package/dist/ui/assets/useProviders-CiLSc05q.js +1 -0
  82. package/dist/ui/assets/vendor-radix-DUug7irY.js +63 -0
  83. package/dist/ui/assets/{vendor-react-CPJEWbIG.js → vendor-react-DYyYpuMy.js} +1 -1
  84. package/dist/ui/assets/volume-x-BDlMCDYT.js +1 -0
  85. package/dist/ui/assets/wifi-CyPHteEm.js +1 -0
  86. package/dist/ui/assets/zap-OVVrfEHr.js +1 -0
  87. package/dist/ui/index.html +8 -7
  88. package/package.json +1 -1
  89. package/packages/core/src/agent/agent-loop.ts +48 -0
  90. package/packages/core/src/agent/llm-client.ts +41 -6
  91. package/packages/core/src/agent/llm-providers/nvidia.ts +39 -0
  92. package/packages/core/src/agent/llm-providers/openai-compat-base.ts +38 -2
  93. package/packages/core/src/agent/mcp-result-normalizer.ts +42 -0
  94. package/packages/core/src/agent/tool-selector.ts +1 -0
  95. package/packages/core/src/artifacts/store.ts +65 -0
  96. package/packages/core/src/events/tool-narration.ts +3 -0
  97. package/packages/core/src/gateway/routes/system.ts +9 -4
  98. package/packages/core/src/gateway/webchat-turn.ts +34 -1
  99. package/packages/core/src/storage/seed.ts +1 -0
  100. package/packages/core/src/tool-runtime/embedded-worker.generated.ts +20 -0
  101. package/packages/core/src/tool-runtime/index.ts +56 -14
  102. package/packages/core/src/tools/index.ts +3 -2
  103. package/packages/core/src/tools/web/artifact-read.ts +162 -0
  104. package/packages/core/src/tools/web/index.ts +3 -0
  105. package/packages/skills/src/bundled-data.generated.ts +752 -752
  106. package/dist/ui/assets/A2UIPage-DExwevBV.js +0 -8
  107. package/dist/ui/assets/AgentCreateForm-7PPShsUn.js +0 -1
  108. package/dist/ui/assets/AgentDetailPage-CiV4tz4P.js +0 -1
  109. package/dist/ui/assets/AgentNewPage-D3TtVwQf.js +0 -1
  110. package/dist/ui/assets/AgentsPage-C2DThet7.js +0 -10
  111. package/dist/ui/assets/ApiClientPage-Cowdq-0f.js +0 -3
  112. package/dist/ui/assets/ChannelsPage-D4IgglwJ.js +0 -8
  113. package/dist/ui/assets/DashboardPage-DAgVB7Iy.js +0 -6
  114. package/dist/ui/assets/LoginPage-CpU61b3u.js +0 -1
  115. package/dist/ui/assets/MeetingPage-DLmqHmm2.js +0 -1
  116. package/dist/ui/assets/ModelSelector-D4OaICwN.js +0 -1
  117. package/dist/ui/assets/ProvidersPage-Bhb6Qr87.js +0 -1
  118. package/dist/ui/assets/RecoverPage-DaZrCpdB.js +0 -1
  119. package/dist/ui/assets/SettingsPage-BRD2F7up.js +0 -9
  120. package/dist/ui/assets/SetupPage-DKppgTja.js +0 -1
  121. package/dist/ui/assets/WebChatPage-CPtETNL5.js +0 -29
  122. package/dist/ui/assets/activity-BSosMryb.js +0 -1
  123. package/dist/ui/assets/agentCapabilities-VqxLmrF6.js +0 -1
  124. package/dist/ui/assets/arrow-left-C9Y6p4Jt.js +0 -1
  125. package/dist/ui/assets/arrow-right-BYfOLnTB.js +0 -1
  126. package/dist/ui/assets/arrow-up-right-D2trA3Ob.js +0 -1
  127. package/dist/ui/assets/calendar-rdd4nvY1.js +0 -1
  128. package/dist/ui/assets/check-DUq9uoNQ.js +0 -1
  129. package/dist/ui/assets/chevron-down-rPF4Pr1n.js +0 -1
  130. package/dist/ui/assets/chevron-left-Crzzf2sY.js +0 -1
  131. package/dist/ui/assets/chevron-right-DtMIvG9A.js +0 -1
  132. package/dist/ui/assets/chevron-up-CG0_KGBy.js +0 -1
  133. package/dist/ui/assets/circle-alert-CYt6gsDA.js +0 -1
  134. package/dist/ui/assets/circle-check-Zl32-jCa.js +0 -1
  135. package/dist/ui/assets/circle-x-BJXzKGmx.js +0 -1
  136. package/dist/ui/assets/copy-BmtJXmDe.js +0 -1
  137. package/dist/ui/assets/download-D3mYhuuL.js +0 -1
  138. package/dist/ui/assets/earth-BZzdDXFC.js +0 -1
  139. package/dist/ui/assets/ellipsis-vertical-BF705XvB.js +0 -1
  140. package/dist/ui/assets/external-link-gdpMTHUV.js +0 -1
  141. package/dist/ui/assets/eye-lSG04hI7.js +0 -1
  142. package/dist/ui/assets/file-text-CjQxcJvN.js +0 -1
  143. package/dist/ui/assets/gauge-DBryKhDK.js +0 -1
  144. package/dist/ui/assets/history-BC6ir9qU.js +0 -1
  145. package/dist/ui/assets/index-DyxoAvTq.js +0 -116
  146. package/dist/ui/assets/index-wML8DaUo.css +0 -2
  147. package/dist/ui/assets/info-BGBaIyKc.js +0 -1
  148. package/dist/ui/assets/key-D_CNJTY2.js +0 -1
  149. package/dist/ui/assets/loader-circle-RXdrfPsH.js +0 -1
  150. package/dist/ui/assets/lock-open-UB61lorP.js +0 -1
  151. package/dist/ui/assets/mail-C5bJ_dBt.js +0 -1
  152. package/dist/ui/assets/pause-BSTJvxDu.js +0 -1
  153. package/dist/ui/assets/pencil-C2B1Q7y2.js +0 -1
  154. package/dist/ui/assets/play-tcMbj-0p.js +0 -1
  155. package/dist/ui/assets/plus-BSZvyMND.js +0 -1
  156. package/dist/ui/assets/progress-DrI2UDu-.js +0 -1
  157. package/dist/ui/assets/radio-D9CrjAar.js +0 -1
  158. package/dist/ui/assets/refresh-cw-D7EB_fVv.js +0 -1
  159. package/dist/ui/assets/scroll-area-CfHn94Vb.js +0 -1
  160. package/dist/ui/assets/search-5S6LY69I.js +0 -1
  161. package/dist/ui/assets/send-Dax1tJ5b.js +0 -1
  162. package/dist/ui/assets/settings-97CL32lC.js +0 -1
  163. package/dist/ui/assets/shield-DwbCEZbF.js +0 -1
  164. package/dist/ui/assets/slider-CmE_z1Ii.js +0 -1
  165. package/dist/ui/assets/sparkles-CWMAzkpD.js +0 -1
  166. package/dist/ui/assets/square-B4dgTXI3.js +0 -1
  167. package/dist/ui/assets/tabs-CxiybXcw.js +0 -1
  168. package/dist/ui/assets/terminal-DoLedTPM.js +0 -1
  169. package/dist/ui/assets/trash-2-DxnVYOz8.js +0 -1
  170. package/dist/ui/assets/triangle-alert-Cka-BeIE.js +0 -1
  171. package/dist/ui/assets/useProviders-CueMz_mL.js +0 -1
  172. package/dist/ui/assets/vendor-radix-D0s_k6Gp.js +0 -63
  173. package/dist/ui/assets/volume-x-DLsli9cf.js +0 -1
  174. package/dist/ui/assets/wifi-BQRJGfLZ.js +0 -1
  175. package/dist/ui/assets/zap-DSR3CEce.js +0 -1
  176. /package/dist/ui/assets/{gateway-url-BlnabM5e.js → gateway-url-BZj_2-jf.js} +0 -0
  177. /package/dist/ui/assets/{useOfficeModel-Cy7cAyEn.js → useOfficeModel-BAFasrg8.js} +0 -0
  178. /package/dist/ui/assets/{utils-Dyb7SC2_.js → utils-SGUggNJc.js} +0 -0
  179. /package/dist/ui/assets/{vendor-router-zpB-76nT.js → vendor-router-Dsg8fxNQ.js} +0 -0
@@ -4,6 +4,8 @@ import { dirname, join } from "node:path"
4
4
  import { availableParallelism } from "node:os"
5
5
  import type { Config } from "../config/loader.ts"
6
6
  import { loadConfig } from "../config/loader.ts"
7
+ import { logger } from "../utils/logger.ts"
8
+ import { embeddedToolWorkerPath } from "./embedded-worker.generated.ts"
7
9
 
8
10
  export type ToolCallLike = {
9
11
  id: string
@@ -105,7 +107,19 @@ type WorkerSlot = {
105
107
  job?: QueuedJob
106
108
  }
107
109
 
108
- function resolveWorkerEntry(): string {
110
+ let cachedWorkerEntry: string | null | undefined
111
+
112
+ /**
113
+ * Locate the tool worker entry, or null when this build ships without one.
114
+ *
115
+ * Returning null instead of throwing is deliberate: workers are an
116
+ * optimization, and a packaging gap must never take a whole turn down with it
117
+ * (it did until v1.0.3 — every desktop install failed each multi-tool turn
118
+ * with "Tool worker entry not found"). The caller degrades to the main thread.
119
+ */
120
+ function resolveWorkerEntry(): string | null {
121
+ if (cachedWorkerEntry !== undefined) return cachedWorkerEntry
122
+
109
123
  const candidates = [
110
124
  new URL("./tool-worker.js", import.meta.url),
111
125
  new URL("./tool-worker.ts", import.meta.url),
@@ -115,7 +129,8 @@ function resolveWorkerEntry(): string {
115
129
 
116
130
  for (const candidate of candidates) {
117
131
  if (existsSync(fileURLToPath(candidate))) {
118
- return candidate.href
132
+ cachedWorkerEntry = candidate.href
133
+ return cachedWorkerEntry
119
134
  }
120
135
  }
121
136
 
@@ -135,13 +150,25 @@ function resolveWorkerEntry(): string {
135
150
 
136
151
  for (const filePath of fallbacks) {
137
152
  if (existsSync(filePath)) {
138
- return filePath
153
+ cachedWorkerEntry = filePath
154
+ return cachedWorkerEntry
139
155
  }
140
156
  }
141
157
 
142
- throw new Error(
143
- `Tool worker entry not found. Tried: ${[...candidates.map((candidate) => fileURLToPath(candidate)), ...fallbacks].join(", ")}`
158
+ // Standalone executable: the worker was embedded at compile time and lives in
159
+ // the virtual bunfs, where existsSync() reports false — so it is taken on
160
+ // trust. It is only ever set by scripts/build-gateway.ts.
161
+ if (embeddedToolWorkerPath) {
162
+ cachedWorkerEntry = embeddedToolWorkerPath
163
+ return cachedWorkerEntry
164
+ }
165
+
166
+ logger.warn(
167
+ "[tool-runtime] No tool worker entry found — running tool batches on the main thread (sequentially)",
168
+ { tried: [...candidates.map((candidate) => fileURLToPath(candidate)), ...fallbacks] }
144
169
  )
170
+ cachedWorkerEntry = null
171
+ return null
145
172
  }
146
173
 
147
174
  function serializeError(error: unknown): SerializedError {
@@ -187,6 +214,7 @@ const DEFAULT_MAIN_THREAD_TOOL_NAMES = new Set([
187
214
  "browser_navigate",
188
215
  "browser_screenshot",
189
216
  "artifact_inspect",
217
+ "artifact_read",
190
218
  "browser_click",
191
219
  "browser_type",
192
220
  "browser_extract",
@@ -255,10 +283,12 @@ class ToolWorkerPool {
255
283
  private workers: WorkerSlot[] = []
256
284
  private queue: QueuedJob[] = []
257
285
  private readonly maxWorkers: number
286
+ private readonly workerEntry: string
258
287
  private disposed = false
259
288
 
260
- constructor(maxWorkers: number) {
289
+ constructor(maxWorkers: number, workerEntry: string) {
261
290
  this.maxWorkers = Math.max(1, maxWorkers)
291
+ this.workerEntry = workerEntry
262
292
  }
263
293
 
264
294
  execute(job: Omit<QueuedJob, "resolve" | "settled" | "startedAt">): Promise<ToolBatchResult> {
@@ -385,7 +415,7 @@ class ToolWorkerPool {
385
415
  }
386
416
 
387
417
  private createSlot(): WorkerSlot {
388
- const worker = new Worker(resolveWorkerEntry(), { type: "module" })
418
+ const worker = new Worker(this.workerEntry, { type: "module" })
389
419
  const slot: WorkerSlot = { worker, busy: false }
390
420
 
391
421
  worker.onmessage = (event: MessageEvent<WorkerMessage>) => {
@@ -500,11 +530,15 @@ class ToolWorkerPool {
500
530
  job.resolve(result)
501
531
 
502
532
  if (restart) {
533
+ // Un worker que abortó o murió se descarta, y el reemplazo lo crea
534
+ // `drain()` solo si queda trabajo. Antes se levantaba uno nuevo en el
535
+ // acto: sobre un aborto —el caso típico al terminar un turno o una
536
+ // suite— eso deja un worker recién arrancado que nadie va a usar y que
537
+ // hay que terminar a mitad del arranque. Bun se cae con SIGSEGV al
538
+ // cerrar el proceso en ese estado (CI: workers_spawned 13, terminated 11).
503
539
  slot.worker.terminate()
504
540
  const index = this.workers.indexOf(slot)
505
- if (index >= 0) {
506
- this.workers[index] = this.createSlot()
507
- }
541
+ if (index >= 0) this.workers.splice(index, 1)
508
542
  } else {
509
543
  slot.busy = false
510
544
  slot.job = undefined
@@ -550,9 +584,12 @@ function resolveToolTimeout(
550
584
  return baseTimeoutMs
551
585
  }
552
586
 
553
- function getPool(maxWorkers: number): ToolWorkerPool {
587
+ function getPool(maxWorkers: number): ToolWorkerPool | null {
588
+ const workerEntry = resolveWorkerEntry()
589
+ if (!workerEntry) return null
590
+
554
591
  if (!sharedPool || sharedPoolSize !== maxWorkers) {
555
- sharedPool = new ToolWorkerPool(maxWorkers)
592
+ sharedPool = new ToolWorkerPool(maxWorkers, workerEntry)
556
593
  sharedPoolSize = maxWorkers
557
594
  }
558
595
  return sharedPool
@@ -578,7 +615,13 @@ export async function executeToolBatch(options: ExecuteToolBatchOptions): Promis
578
615
  }))
579
616
  }
580
617
 
581
- if (!runtimeConfig.enabled || !runtimeConfig.parallelToolCalls || options.toolCalls.length <= 1) {
618
+ // A null pool means workers are disabled, unnecessary (single call), or
619
+ // unavailable in this build — all three degrade to the main thread.
620
+ const pool = runtimeConfig.enabled && runtimeConfig.parallelToolCalls && options.toolCalls.length > 1
621
+ ? getPool(runtimeConfig.maxWorkers)
622
+ : null
623
+
624
+ if (!pool) {
582
625
  const results: ToolBatchResult[] = []
583
626
  for (const toolCall of options.toolCalls) {
584
627
  const startedAt = performance.now()
@@ -605,7 +648,6 @@ export async function executeToolBatch(options: ExecuteToolBatchOptions): Promis
605
648
  return results
606
649
  }
607
650
 
608
- const pool = getPool(runtimeConfig.maxWorkers)
609
651
  const batchId = crypto.randomUUID()
610
652
  const abortHandler = () => pool.abortBatch(batchId, "Tool execution aborted")
611
653
  options.signal?.addEventListener("abort", abortHandler, { once: true })
@@ -11,7 +11,7 @@ import type { Config } from "../config/loader.ts";
11
11
  // Filesystem (7)
12
12
  import * as filesystem from "./filesystem/index.ts";
13
13
 
14
- // Web (10)
14
+ // Web (11)
15
15
  import * as web from "./web/index.ts";
16
16
 
17
17
  // Cron (8) - Croner-based scheduler tools
@@ -43,7 +43,7 @@ export function createAllTools(config: Config): Tool[] {
43
43
  // FILESYSTEM (7)
44
44
  ...filesystem.createTools(),
45
45
 
46
- // WEB (10)
46
+ // WEB (11)
47
47
  ...web.createTools(),
48
48
 
49
49
  // CRON (8)
@@ -124,6 +124,7 @@ export {
124
124
  browserScriptTool,
125
125
  browserWaitTool,
126
126
  artifactInspectTool,
127
+ artifactReadTool,
127
128
  } from "./web/index.ts";
128
129
 
129
130
  export {
@@ -0,0 +1,162 @@
1
+ /**
2
+ * artifact_read — reads back the content an artifact_ref points at.
3
+ *
4
+ * mcp-result-normalizer.ts keeps oversized MCP text results out of the context
5
+ * window: the model gets `{ type: "artifact_ref", artifact_id, size, preview }`
6
+ * with a blind 500-char preview. That is the right call for the context, but
7
+ * without a way to read the artifact back it left the agent stuck — it would
8
+ * call artifact_inspect (metadata only), find no content, and burn its
9
+ * iterations until the turn died on an empty synthesis. This is the way back in.
10
+ *
11
+ * Slicing is by characters, not lines: the case this was built for is a single
12
+ * 245 KB JSON line from a Gmail MCP server, where line-based paging returns
13
+ * everything or nothing. `search` exists for the same reason — jumping straight
14
+ * to the three relevant messages beats paging through 245 KB in 20 KB chunks.
15
+ */
16
+
17
+ import type { Tool } from "../types";
18
+ import { readArtifactText } from "../../artifacts/store";
19
+
20
+ const DEFAULT_LIMIT = 20_000;
21
+ const MAX_LIMIT = 50_000;
22
+ const MAX_MATCHES = 10;
23
+ const DEFAULT_MATCH_CONTEXT = 600;
24
+ const MAX_MATCH_CONTEXT = 8_000;
25
+ /**
26
+ * What a search may return in total. Capping the per-match window alone would
27
+ * be the wrong knob: a record whose fields are 2 KB of mail headers needs a
28
+ * wide window to show its id, while ten such windows would put back into the
29
+ * context exactly what the artifact was created to keep out.
30
+ */
31
+ const MAX_SEARCH_CHARS = 40_000;
32
+
33
+ function clampNumber(value: unknown, fallback: number, min: number, max: number): number {
34
+ const parsed = typeof value === "number" ? value : Number(value);
35
+ if (!Number.isFinite(parsed)) return fallback;
36
+ return Math.min(max, Math.max(min, Math.floor(parsed)));
37
+ }
38
+
39
+ function findMatches(text: string, query: string, context: number): Array<{ offset: number; excerpt: string }> {
40
+ const haystack = text.toLowerCase();
41
+ const needle = query.toLowerCase();
42
+ const matches: Array<{ offset: number; excerpt: string }> = [];
43
+ const half = Math.floor(context / 2);
44
+ let from = 0;
45
+
46
+ let used = 0;
47
+
48
+ while (matches.length < MAX_MATCHES) {
49
+ const at = haystack.indexOf(needle, from);
50
+ if (at === -1) break;
51
+ const start = Math.max(0, at - half);
52
+ const end = Math.min(text.length, at + needle.length + half);
53
+ const excerpt = text.slice(start, end);
54
+ // The first match always comes back, however wide it is — returning zero
55
+ // results for a query that did match would be the worst possible answer.
56
+ if (matches.length > 0 && used + excerpt.length > MAX_SEARCH_CHARS) break;
57
+ matches.push({ offset: at, excerpt });
58
+ used += excerpt.length;
59
+ from = at + needle.length;
60
+ }
61
+
62
+ return matches;
63
+ }
64
+
65
+ function countMatches(text: string, query: string): number {
66
+ const haystack = text.toLowerCase();
67
+ const needle = query.toLowerCase();
68
+ let total = 0;
69
+ let from = 0;
70
+ for (;;) {
71
+ const at = haystack.indexOf(needle, from);
72
+ if (at === -1) return total;
73
+ total++;
74
+ from = at + needle.length;
75
+ }
76
+ }
77
+
78
+ export const artifactReadTool: Tool = {
79
+ name: "artifact_read",
80
+ description:
81
+ "Read the text content of a managed artifact in slices, or search inside it. Use this on any artifact_ref a tool returned instead of guessing from its preview. Spanish: leer artefacto, ver contenido del artefacto, abrir resultado grande, buscar dentro del artefacto",
82
+ parameters: {
83
+ type: "object",
84
+ properties: {
85
+ artifactId: {
86
+ type: "string",
87
+ description: "Managed artifact identifier (the artifact_id of an artifact_ref).",
88
+ },
89
+ offset: {
90
+ type: "number",
91
+ description: "Character position to start reading from (default: 0). Use next_offset from the previous call to continue.",
92
+ },
93
+ limit: {
94
+ type: "number",
95
+ description: `Characters to return (default: ${DEFAULT_LIMIT}, max: ${MAX_LIMIT}).`,
96
+ },
97
+ search: {
98
+ type: "string",
99
+ description: "Return excerpts around each case-insensitive match instead of a contiguous slice. Far cheaper than paging a large artifact.",
100
+ },
101
+ context: {
102
+ type: "number",
103
+ description: `Characters of surrounding text per match (default: ${DEFAULT_MATCH_CONTEXT}, max: ${MAX_MATCH_CONTEXT}). Widen it when records carry long fields and the excerpt cuts off the data you need.`,
104
+ },
105
+ },
106
+ required: ["artifactId"],
107
+ },
108
+ execute: async (params, config) => {
109
+ const artifactId = String(params.artifactId ?? "").trim();
110
+ if (!artifactId) return { ok: false, error: "artifactId is required" };
111
+
112
+ const userId = config?.configurable?.user_id as string | undefined;
113
+ const result = await readArtifactText(artifactId, { userId });
114
+ if (!result.ok || !result.artifact) {
115
+ return { ok: false, artifact_id: artifactId, error: result.error, status: result.status };
116
+ }
117
+
118
+ const artifact = result.artifact;
119
+ const text = result.text ?? "";
120
+ const search = typeof params.search === "string" ? params.search.trim() : "";
121
+
122
+ if (search) {
123
+ const context = clampNumber(params.context, DEFAULT_MATCH_CONTEXT, 40, MAX_MATCH_CONTEXT);
124
+ const matches = findMatches(text, search, context);
125
+ // Always the real count, never the number returned: collection stops at
126
+ // MAX_MATCHES or at the character budget, and an agent told "4 matches"
127
+ // when there are 8 stops looking.
128
+ const total = countMatches(text, search);
129
+ return {
130
+ ok: true,
131
+ artifact_id: artifact.id,
132
+ mime_type: artifact.mime_type,
133
+ total_chars: text.length,
134
+ query: search,
135
+ total_matches: total,
136
+ returned_matches: matches.length,
137
+ truncated: total > matches.length,
138
+ context,
139
+ matches,
140
+ };
141
+ }
142
+
143
+ const offset = clampNumber(params.offset, 0, 0, Math.max(0, text.length));
144
+ const limit = clampNumber(params.limit, DEFAULT_LIMIT, 1, MAX_LIMIT);
145
+ const content = text.slice(offset, offset + limit);
146
+ const nextOffset = offset + content.length;
147
+ const eof = nextOffset >= text.length;
148
+
149
+ return {
150
+ ok: true,
151
+ artifact_id: artifact.id,
152
+ mime_type: artifact.mime_type,
153
+ kind: artifact.kind,
154
+ total_chars: text.length,
155
+ offset,
156
+ returned_chars: content.length,
157
+ next_offset: eof ? null : nextOffset,
158
+ eof,
159
+ content,
160
+ };
161
+ },
162
+ };
@@ -15,6 +15,7 @@ import { browserExtractTool } from "./browser-extract.ts";
15
15
  import { browserScriptTool } from "./browser-script.ts";
16
16
  import { browserWaitTool } from "./browser-wait.ts";
17
17
  import { artifactInspectTool } from "./artifact-inspect.ts";
18
+ import { artifactReadTool } from "./artifact-read.ts";
18
19
 
19
20
  export function createTools(): Tool[] {
20
21
  return [
@@ -28,6 +29,7 @@ export function createTools(): Tool[] {
28
29
  browserScriptTool,
29
30
  browserWaitTool,
30
31
  artifactInspectTool,
32
+ artifactReadTool,
31
33
  ];
32
34
  }
33
35
 
@@ -42,3 +44,4 @@ export * from "./browser-script.ts";
42
44
  export * from "./browser-wait.ts";
43
45
  export * from "./browser-service.ts";
44
46
  export * from "./artifact-inspect.ts";
47
+ export * from "./artifact-read.ts";