@rudderjs/ai 1.17.3 → 1.18.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 (377) hide show
  1. package/README.md +19 -1274
  2. package/dist/budget-orm/index.d.ts +1 -95
  3. package/dist/budget-orm/index.d.ts.map +1 -1
  4. package/dist/budget-orm/index.js +4 -176
  5. package/dist/budget-orm/index.js.map +1 -1
  6. package/dist/chat-mentions.d.ts +1 -58
  7. package/dist/chat-mentions.d.ts.map +1 -1
  8. package/dist/chat-mentions.js +4 -80
  9. package/dist/chat-mentions.js.map +1 -1
  10. package/dist/commands/ai-eval.d.ts +1 -92
  11. package/dist/commands/ai-eval.d.ts.map +1 -1
  12. package/dist/commands/ai-eval.js +4 -377
  13. package/dist/commands/ai-eval.js.map +1 -1
  14. package/dist/commands/make-agent.d.ts +1 -2
  15. package/dist/commands/make-agent.d.ts.map +1 -1
  16. package/dist/commands/make-agent.js +4 -22
  17. package/dist/commands/make-agent.js.map +1 -1
  18. package/dist/computer-use/index.d.ts +1 -52
  19. package/dist/computer-use/index.d.ts.map +1 -1
  20. package/dist/computer-use/index.js +4 -50
  21. package/dist/computer-use/index.js.map +1 -1
  22. package/dist/conversation-orm/index.d.ts +1 -108
  23. package/dist/conversation-orm/index.d.ts.map +1 -1
  24. package/dist/conversation-orm/index.js +4 -214
  25. package/dist/conversation-orm/index.js.map +1 -1
  26. package/dist/doctor.d.ts +1 -1
  27. package/dist/doctor.d.ts.map +1 -1
  28. package/dist/doctor.js +4 -65
  29. package/dist/doctor.js.map +1 -1
  30. package/dist/eval/index.d.ts +1 -270
  31. package/dist/eval/index.d.ts.map +1 -1
  32. package/dist/eval/index.js +4 -509
  33. package/dist/eval/index.js.map +1 -1
  34. package/dist/gateway/index.d.ts +1 -10
  35. package/dist/gateway/index.d.ts.map +1 -1
  36. package/dist/gateway/index.js +4 -10
  37. package/dist/gateway/index.js.map +1 -1
  38. package/dist/index.d.ts +1 -66
  39. package/dist/index.d.ts.map +1 -1
  40. package/dist/index.js +4 -78
  41. package/dist/index.js.map +1 -1
  42. package/dist/mcp/index.d.ts +1 -15
  43. package/dist/mcp/index.d.ts.map +1 -1
  44. package/dist/mcp/index.js +4 -14
  45. package/dist/mcp/index.js.map +1 -1
  46. package/dist/memory-embedding/index.d.ts +1 -120
  47. package/dist/memory-embedding/index.d.ts.map +1 -1
  48. package/dist/memory-embedding/index.js +4 -228
  49. package/dist/memory-embedding/index.js.map +1 -1
  50. package/dist/memory-orm/index.d.ts +1 -117
  51. package/dist/memory-orm/index.d.ts.map +1 -1
  52. package/dist/memory-orm/index.js +4 -186
  53. package/dist/memory-orm/index.js.map +1 -1
  54. package/dist/node/index.d.ts +1 -2
  55. package/dist/node/index.d.ts.map +1 -1
  56. package/dist/node/index.js +4 -2
  57. package/dist/node/index.js.map +1 -1
  58. package/dist/observers.d.ts +1 -129
  59. package/dist/observers.d.ts.map +1 -1
  60. package/dist/observers.js +4 -39
  61. package/dist/observers.js.map +1 -1
  62. package/dist/react/index.d.ts +1 -15
  63. package/dist/react/index.d.ts.map +1 -1
  64. package/dist/react/index.js +4 -15
  65. package/dist/react/index.js.map +1 -1
  66. package/dist/server/index.d.ts +1 -1
  67. package/dist/server/index.d.ts.map +1 -1
  68. package/dist/server/index.js +4 -1
  69. package/dist/server/index.js.map +1 -1
  70. package/package.json +9 -13
  71. package/boost/guidelines.md +0 -260
  72. package/boost/skills/ai-agents/SKILL.md +0 -240
  73. package/boost/skills/ai-tools/SKILL.md +0 -260
  74. package/dist/agent-run-store.d.ts +0 -161
  75. package/dist/agent-run-store.d.ts.map +0 -1
  76. package/dist/agent-run-store.js +0 -98
  77. package/dist/agent-run-store.js.map +0 -1
  78. package/dist/agent-sse.d.ts +0 -153
  79. package/dist/agent-sse.d.ts.map +0 -1
  80. package/dist/agent-sse.js +0 -282
  81. package/dist/agent-sse.js.map +0 -1
  82. package/dist/agent.d.ts +0 -508
  83. package/dist/agent.d.ts.map +0 -1
  84. package/dist/agent.js +0 -1538
  85. package/dist/agent.js.map +0 -1
  86. package/dist/attachment.d.ts +0 -31
  87. package/dist/attachment.d.ts.map +0 -1
  88. package/dist/attachment.js +0 -89
  89. package/dist/attachment.js.map +0 -1
  90. package/dist/audio.d.ts +0 -45
  91. package/dist/audio.d.ts.map +0 -1
  92. package/dist/audio.js +0 -93
  93. package/dist/audio.js.map +0 -1
  94. package/dist/base64.d.ts +0 -7
  95. package/dist/base64.d.ts.map +0 -1
  96. package/dist/base64.js +0 -39
  97. package/dist/base64.js.map +0 -1
  98. package/dist/budget/pricing.d.ts +0 -124
  99. package/dist/budget/pricing.d.ts.map +0 -1
  100. package/dist/budget/pricing.js +0 -175
  101. package/dist/budget/pricing.js.map +0 -1
  102. package/dist/budget/storage.d.ts +0 -104
  103. package/dist/budget/storage.d.ts.map +0 -1
  104. package/dist/budget/storage.js +0 -0
  105. package/dist/budget/storage.js.map +0 -1
  106. package/dist/budget/with-budget.d.ts +0 -119
  107. package/dist/budget/with-budget.d.ts.map +0 -1
  108. package/dist/budget/with-budget.js +0 -175
  109. package/dist/budget/with-budget.js.map +0 -1
  110. package/dist/cached-embedding.d.ts +0 -14
  111. package/dist/cached-embedding.d.ts.map +0 -1
  112. package/dist/cached-embedding.js +0 -44
  113. package/dist/cached-embedding.js.map +0 -1
  114. package/dist/computer-use/actions.d.ts +0 -214
  115. package/dist/computer-use/actions.d.ts.map +0 -1
  116. package/dist/computer-use/actions.js +0 -48
  117. package/dist/computer-use/actions.js.map +0 -1
  118. package/dist/computer-use/errors.d.ts +0 -57
  119. package/dist/computer-use/errors.d.ts.map +0 -1
  120. package/dist/computer-use/errors.js +0 -76
  121. package/dist/computer-use/errors.js.map +0 -1
  122. package/dist/computer-use/playwright.d.ts +0 -76
  123. package/dist/computer-use/playwright.d.ts.map +0 -1
  124. package/dist/computer-use/playwright.js +0 -270
  125. package/dist/computer-use/playwright.js.map +0 -1
  126. package/dist/computer-use/tool.d.ts +0 -154
  127. package/dist/computer-use/tool.d.ts.map +0 -1
  128. package/dist/computer-use/tool.js +0 -210
  129. package/dist/computer-use/tool.js.map +0 -1
  130. package/dist/continuation-validation.d.ts +0 -85
  131. package/dist/continuation-validation.d.ts.map +0 -1
  132. package/dist/continuation-validation.js +0 -166
  133. package/dist/continuation-validation.js.map +0 -1
  134. package/dist/conversation-persistence.d.ts +0 -46
  135. package/dist/conversation-persistence.d.ts.map +0 -1
  136. package/dist/conversation-persistence.js +0 -176
  137. package/dist/conversation-persistence.js.map +0 -1
  138. package/dist/conversation.d.ts +0 -11
  139. package/dist/conversation.d.ts.map +0 -1
  140. package/dist/conversation.js +0 -55
  141. package/dist/conversation.js.map +0 -1
  142. package/dist/eval/fixtures.d.ts +0 -65
  143. package/dist/eval/fixtures.d.ts.map +0 -1
  144. package/dist/eval/fixtures.js +0 -110
  145. package/dist/eval/fixtures.js.map +0 -1
  146. package/dist/eval/html-reporter.d.ts +0 -25
  147. package/dist/eval/html-reporter.d.ts.map +0 -1
  148. package/dist/eval/html-reporter.js +0 -209
  149. package/dist/eval/html-reporter.js.map +0 -1
  150. package/dist/eval/json-reporter.d.ts +0 -43
  151. package/dist/eval/json-reporter.d.ts.map +0 -1
  152. package/dist/eval/json-reporter.js +0 -40
  153. package/dist/eval/json-reporter.js.map +0 -1
  154. package/dist/facade.d.ts +0 -96
  155. package/dist/facade.d.ts.map +0 -1
  156. package/dist/facade.js +0 -146
  157. package/dist/facade.js.map +0 -1
  158. package/dist/fake.d.ts +0 -201
  159. package/dist/fake.d.ts.map +0 -1
  160. package/dist/fake.js +0 -428
  161. package/dist/fake.js.map +0 -1
  162. package/dist/file-search.d.ts +0 -168
  163. package/dist/file-search.d.ts.map +0 -1
  164. package/dist/file-search.js +0 -158
  165. package/dist/file-search.js.map +0 -1
  166. package/dist/files.d.ts +0 -27
  167. package/dist/files.d.ts.map +0 -1
  168. package/dist/files.js +0 -44
  169. package/dist/files.js.map +0 -1
  170. package/dist/gateway/http-gateway-adapter.d.ts +0 -94
  171. package/dist/gateway/http-gateway-adapter.d.ts.map +0 -1
  172. package/dist/gateway/http-gateway-adapter.js +0 -106
  173. package/dist/gateway/http-gateway-adapter.js.map +0 -1
  174. package/dist/gateway/sse.d.ts +0 -28
  175. package/dist/gateway/sse.d.ts.map +0 -1
  176. package/dist/gateway/sse.js +0 -78
  177. package/dist/gateway/sse.js.map +0 -1
  178. package/dist/handoff.d.ts +0 -95
  179. package/dist/handoff.d.ts.map +0 -1
  180. package/dist/handoff.js +0 -78
  181. package/dist/handoff.js.map +0 -1
  182. package/dist/handoffs-driver.d.ts +0 -58
  183. package/dist/handoffs-driver.d.ts.map +0 -1
  184. package/dist/handoffs-driver.js +0 -103
  185. package/dist/handoffs-driver.js.map +0 -1
  186. package/dist/image.d.ts +0 -40
  187. package/dist/image.d.ts.map +0 -1
  188. package/dist/image.js +0 -109
  189. package/dist/image.js.map +0 -1
  190. package/dist/mcp/client-tools.d.ts +0 -39
  191. package/dist/mcp/client-tools.d.ts.map +0 -1
  192. package/dist/mcp/client-tools.js +0 -147
  193. package/dist/mcp/client-tools.js.map +0 -1
  194. package/dist/mcp/server-from-agent.d.ts +0 -24
  195. package/dist/mcp/server-from-agent.d.ts.map +0 -1
  196. package/dist/mcp/server-from-agent.js +0 -113
  197. package/dist/mcp/server-from-agent.js.map +0 -1
  198. package/dist/mcp/types.d.ts +0 -64
  199. package/dist/mcp/types.d.ts.map +0 -1
  200. package/dist/mcp/types.js +0 -6
  201. package/dist/mcp/types.js.map +0 -1
  202. package/dist/memory-extract.d.ts +0 -60
  203. package/dist/memory-extract.d.ts.map +0 -1
  204. package/dist/memory-extract.js +0 -163
  205. package/dist/memory-extract.js.map +0 -1
  206. package/dist/memory-inject.d.ts +0 -39
  207. package/dist/memory-inject.d.ts.map +0 -1
  208. package/dist/memory-inject.js +0 -135
  209. package/dist/memory-inject.js.map +0 -1
  210. package/dist/memory.d.ts +0 -55
  211. package/dist/memory.d.ts.map +0 -1
  212. package/dist/memory.js +0 -132
  213. package/dist/memory.js.map +0 -1
  214. package/dist/middleware.d.ts +0 -18
  215. package/dist/middleware.d.ts.map +0 -1
  216. package/dist/middleware.js +0 -72
  217. package/dist/middleware.js.map +0 -1
  218. package/dist/node/attachment.d.ts +0 -6
  219. package/dist/node/attachment.d.ts.map +0 -1
  220. package/dist/node/attachment.js +0 -35
  221. package/dist/node/attachment.js.map +0 -1
  222. package/dist/node/transcription.d.ts +0 -4
  223. package/dist/node/transcription.d.ts.map +0 -1
  224. package/dist/node/transcription.js +0 -8
  225. package/dist/node/transcription.js.map +0 -1
  226. package/dist/output.d.ts +0 -22
  227. package/dist/output.d.ts.map +0 -1
  228. package/dist/output.js +0 -60
  229. package/dist/output.js.map +0 -1
  230. package/dist/provider-tools.d.ts +0 -87
  231. package/dist/provider-tools.d.ts.map +0 -1
  232. package/dist/provider-tools.js +0 -189
  233. package/dist/provider-tools.js.map +0 -1
  234. package/dist/providers/anthropic.d.ts +0 -24
  235. package/dist/providers/anthropic.d.ts.map +0 -1
  236. package/dist/providers/anthropic.js +0 -405
  237. package/dist/providers/anthropic.js.map +0 -1
  238. package/dist/providers/azure.d.ts +0 -13
  239. package/dist/providers/azure.d.ts.map +0 -1
  240. package/dist/providers/azure.js +0 -15
  241. package/dist/providers/azure.js.map +0 -1
  242. package/dist/providers/bedrock.d.ts +0 -75
  243. package/dist/providers/bedrock.d.ts.map +0 -1
  244. package/dist/providers/bedrock.js +0 -181
  245. package/dist/providers/bedrock.js.map +0 -1
  246. package/dist/providers/cohere.d.ts +0 -13
  247. package/dist/providers/cohere.d.ts.map +0 -1
  248. package/dist/providers/cohere.js +0 -87
  249. package/dist/providers/cohere.js.map +0 -1
  250. package/dist/providers/deepseek.d.ts +0 -12
  251. package/dist/providers/deepseek.d.ts.map +0 -1
  252. package/dist/providers/deepseek.js +0 -15
  253. package/dist/providers/deepseek.js.map +0 -1
  254. package/dist/providers/elevenlabs.d.ts +0 -98
  255. package/dist/providers/elevenlabs.d.ts.map +0 -1
  256. package/dist/providers/elevenlabs.js +0 -229
  257. package/dist/providers/elevenlabs.js.map +0 -1
  258. package/dist/providers/google-cache-registry.d.ts +0 -132
  259. package/dist/providers/google-cache-registry.d.ts.map +0 -1
  260. package/dist/providers/google-cache-registry.js +0 -209
  261. package/dist/providers/google-cache-registry.js.map +0 -1
  262. package/dist/providers/google.d.ts +0 -38
  263. package/dist/providers/google.d.ts.map +0 -1
  264. package/dist/providers/google.js +0 -903
  265. package/dist/providers/google.js.map +0 -1
  266. package/dist/providers/groq.d.ts +0 -12
  267. package/dist/providers/groq.d.ts.map +0 -1
  268. package/dist/providers/groq.js +0 -15
  269. package/dist/providers/groq.js.map +0 -1
  270. package/dist/providers/jina.d.ts +0 -13
  271. package/dist/providers/jina.d.ts.map +0 -1
  272. package/dist/providers/jina.js +0 -90
  273. package/dist/providers/jina.js.map +0 -1
  274. package/dist/providers/mistral.d.ts +0 -13
  275. package/dist/providers/mistral.d.ts.map +0 -1
  276. package/dist/providers/mistral.js +0 -46
  277. package/dist/providers/mistral.js.map +0 -1
  278. package/dist/providers/ollama.d.ts +0 -11
  279. package/dist/providers/ollama.d.ts.map +0 -1
  280. package/dist/providers/ollama.js +0 -15
  281. package/dist/providers/ollama.js.map +0 -1
  282. package/dist/providers/openai.d.ts +0 -79
  283. package/dist/providers/openai.d.ts.map +0 -1
  284. package/dist/providers/openai.js +0 -792
  285. package/dist/providers/openai.js.map +0 -1
  286. package/dist/providers/openrouter.d.ts +0 -43
  287. package/dist/providers/openrouter.d.ts.map +0 -1
  288. package/dist/providers/openrouter.js +0 -21
  289. package/dist/providers/openrouter.js.map +0 -1
  290. package/dist/providers/voyage.d.ts +0 -91
  291. package/dist/providers/voyage.d.ts.map +0 -1
  292. package/dist/providers/voyage.js +0 -166
  293. package/dist/providers/voyage.js.map +0 -1
  294. package/dist/providers/xai.d.ts +0 -12
  295. package/dist/providers/xai.d.ts.map +0 -1
  296. package/dist/providers/xai.js +0 -15
  297. package/dist/providers/xai.js.map +0 -1
  298. package/dist/queue-job.d.ts +0 -100
  299. package/dist/queue-job.d.ts.map +0 -1
  300. package/dist/queue-job.js +0 -185
  301. package/dist/queue-job.js.map +0 -1
  302. package/dist/react/agent-run.d.ts +0 -111
  303. package/dist/react/agent-run.d.ts.map +0 -1
  304. package/dist/react/agent-run.js +0 -107
  305. package/dist/react/agent-run.js.map +0 -1
  306. package/dist/react/useAgentRun.d.ts +0 -68
  307. package/dist/react/useAgentRun.d.ts.map +0 -1
  308. package/dist/react/useAgentRun.js +0 -125
  309. package/dist/react/useAgentRun.js.map +0 -1
  310. package/dist/registry.d.ts +0 -45
  311. package/dist/registry.d.ts.map +0 -1
  312. package/dist/registry.js +0 -131
  313. package/dist/registry.js.map +0 -1
  314. package/dist/rerank.d.ts +0 -20
  315. package/dist/rerank.d.ts.map +0 -1
  316. package/dist/rerank.js +0 -40
  317. package/dist/rerank.js.map +0 -1
  318. package/dist/resume-approval.d.ts +0 -30
  319. package/dist/resume-approval.d.ts.map +0 -1
  320. package/dist/resume-approval.js +0 -147
  321. package/dist/resume-approval.js.map +0 -1
  322. package/dist/sanitize-conversation.d.ts +0 -43
  323. package/dist/sanitize-conversation.d.ts.map +0 -1
  324. package/dist/sanitize-conversation.js +0 -85
  325. package/dist/sanitize-conversation.js.map +0 -1
  326. package/dist/scoped-tool.d.ts +0 -98
  327. package/dist/scoped-tool.d.ts.map +0 -1
  328. package/dist/scoped-tool.js +0 -174
  329. package/dist/scoped-tool.js.map +0 -1
  330. package/dist/server/provider.d.ts +0 -22
  331. package/dist/server/provider.d.ts.map +0 -1
  332. package/dist/server/provider.js +0 -194
  333. package/dist/server/provider.js.map +0 -1
  334. package/dist/similarity-search.d.ts +0 -163
  335. package/dist/similarity-search.d.ts.map +0 -1
  336. package/dist/similarity-search.js +0 -147
  337. package/dist/similarity-search.js.map +0 -1
  338. package/dist/sub-agent-run-store.d.ts +0 -157
  339. package/dist/sub-agent-run-store.d.ts.map +0 -1
  340. package/dist/sub-agent-run-store.js +0 -87
  341. package/dist/sub-agent-run-store.js.map +0 -1
  342. package/dist/tool-execution.d.ts +0 -16
  343. package/dist/tool-execution.d.ts.map +0 -1
  344. package/dist/tool-execution.js +0 -498
  345. package/dist/tool-execution.js.map +0 -1
  346. package/dist/tool-helpers.d.ts +0 -77
  347. package/dist/tool-helpers.d.ts.map +0 -1
  348. package/dist/tool-helpers.js +0 -117
  349. package/dist/tool-helpers.js.map +0 -1
  350. package/dist/tool.d.ts +0 -216
  351. package/dist/tool.d.ts.map +0 -1
  352. package/dist/tool.js +0 -175
  353. package/dist/tool.js.map +0 -1
  354. package/dist/transcription.d.ts +0 -42
  355. package/dist/transcription.d.ts.map +0 -1
  356. package/dist/transcription.js +0 -77
  357. package/dist/transcription.js.map +0 -1
  358. package/dist/types.d.ts +0 -1020
  359. package/dist/types.d.ts.map +0 -1
  360. package/dist/types.js +0 -2
  361. package/dist/types.js.map +0 -1
  362. package/dist/util/hash.d.ts +0 -11
  363. package/dist/util/hash.d.ts.map +0 -1
  364. package/dist/util/hash.js +0 -23
  365. package/dist/util/hash.js.map +0 -1
  366. package/dist/vector-stores/index.d.ts +0 -96
  367. package/dist/vector-stores/index.d.ts.map +0 -1
  368. package/dist/vector-stores/index.js +0 -153
  369. package/dist/vector-stores/index.js.map +0 -1
  370. package/dist/vercel-protocol.d.ts +0 -18
  371. package/dist/vercel-protocol.d.ts.map +0 -1
  372. package/dist/vercel-protocol.js +0 -75
  373. package/dist/vercel-protocol.js.map +0 -1
  374. package/dist/zod-to-json-schema.d.ts +0 -16
  375. package/dist/zod-to-json-schema.d.ts.map +0 -1
  376. package/dist/zod-to-json-schema.js +0 -17
  377. package/dist/zod-to-json-schema.js.map +0 -1
@@ -1,260 +0,0 @@
1
- ---
2
- name: ai-tools
3
- description: Defining server and client tools with Zod schemas, approval gates, streaming yields, and modelOutput for Rudder AI agents
4
- license: MIT
5
- appliesTo:
6
- - '@rudderjs/ai'
7
- trigger: writing a `toolDefinition()`, defining server or client tools, adding streaming yields, or wiring approval gates
8
- skip: configuring an `Agent` class itself — load `ai-agents` instead
9
- metadata:
10
- author: rudderjs
11
- ---
12
-
13
- # AI Tools
14
-
15
- ## When to use this skill
16
-
17
- Load this skill when you need to define tools for AI agents -- server-side executors, client-side browser tools, streaming generator tools, approval gates, or tools with custom model output formatting.
18
-
19
- ## Key concepts
20
-
21
- - **toolDefinition()**: Builder function that creates a typed tool from a Zod input schema. Call `.server()` to attach a handler, or leave as-is for a client tool.
22
- - **Server tools**: Have an `execute` function that runs on the server. Can be a regular async function or an `async function*` generator.
23
- - **Client tools**: No `execute` -- the agent loop pauses and returns pending tool calls for browser-side execution.
24
- - **Approval gates**: `needsApproval: true` (or a predicate function) pauses the loop with `tool_approval_required` finish reason.
25
- - **modelOutput()**: Transform the tool's structured result into a shorter string for the model's context, while the UI still gets the full result.
26
- - **Tool updates (streaming)**: Generator tools can `yield` progress payloads that surface as `tool-update` stream chunks.
27
-
28
- ## Step-by-step
29
-
30
- ### 1. Basic server tool
31
-
32
- ```ts
33
- import { toolDefinition } from '@rudderjs/ai'
34
- import { z } from 'zod'
35
-
36
- const weatherTool = toolDefinition({
37
- name: 'get_weather',
38
- description: 'Get current weather for a location',
39
- inputSchema: z.object({
40
- location: z.string().describe('City name'),
41
- units: z.enum(['celsius', 'fahrenheit']).default('celsius'),
42
- }),
43
- }).server(async ({ location, units }) => {
44
- const data = await fetchWeather(location, units)
45
- return { temp: data.temperature, conditions: data.conditions, unit: units }
46
- })
47
- ```
48
-
49
- ### 2. Client tool (browser-side execution)
50
-
51
- ```ts
52
- // No .server() call -- this is a client tool
53
- const readClipboardTool = toolDefinition({
54
- name: 'read_clipboard',
55
- description: 'Read the contents of the user clipboard',
56
- inputSchema: z.object({}),
57
- })
58
-
59
- // When the agent calls this tool, the loop pauses with:
60
- // finishReason: 'client_tool_calls'
61
- // pendingClientToolCalls: [{ id, name: 'read_clipboard', arguments: {} }]
62
- // The caller executes it browser-side and resumes with tool results.
63
- ```
64
-
65
- ### 3. Tool with approval gate
66
-
67
- ```ts
68
- const deleteUserTool = toolDefinition({
69
- name: 'delete_user',
70
- description: 'Permanently delete a user account',
71
- inputSchema: z.object({ userId: z.string() }),
72
- needsApproval: true, // always requires approval
73
- }).server(async ({ userId }) => {
74
- await User.forceDelete(userId)
75
- return { deleted: true }
76
- })
77
-
78
- // Conditional approval
79
- const sendEmailTool = toolDefinition({
80
- name: 'send_email',
81
- description: 'Send an email to a user',
82
- inputSchema: z.object({
83
- to: z.string(),
84
- subject: z.string(),
85
- body: z.string(),
86
- }),
87
- needsApproval: (input) => input.to.endsWith('@external.com'),
88
- }).server(async (input) => {
89
- await sendEmail(input)
90
- return { sent: true }
91
- })
92
- ```
93
-
94
- When approval is required, the loop stops with:
95
- - `finishReason: 'tool_approval_required'`
96
- - `pendingApprovalToolCall: { toolCall, isClientTool: false }`
97
-
98
- Resume by passing `approvedToolCallIds` or `rejectedToolCallIds` in the next prompt options.
99
-
100
- ### 4. Streaming tool with progress yields
101
-
102
- ```ts
103
- const analyzeDataTool = toolDefinition({
104
- name: 'analyze_data',
105
- description: 'Analyze a dataset and return insights',
106
- inputSchema: z.object({ datasetId: z.string() }),
107
- }).server(async function* ({ datasetId }) {
108
- const dataset = await loadDataset(datasetId)
109
-
110
- yield { progress: 25, message: 'Loading data...' }
111
-
112
- const cleaned = cleanData(dataset)
113
- yield { progress: 50, message: 'Cleaning data...' }
114
-
115
- const analysis = runAnalysis(cleaned)
116
- yield { progress: 75, message: 'Running analysis...' }
117
-
118
- const insights = summarize(analysis)
119
- yield { progress: 100, message: 'Complete' }
120
-
121
- return { insights, recordCount: dataset.length }
122
- // Each yield surfaces as a 'tool-update' StreamChunk
123
- // The return value is the final 'tool-result'
124
- })
125
- ```
126
-
127
- ### 5. modelOutput() -- control what the model sees
128
-
129
- ```ts
130
- const searchTool = toolDefinition({
131
- name: 'search_documents',
132
- description: 'Search the document database',
133
- inputSchema: z.object({ query: z.string() }),
134
- }).server(async ({ query }) => {
135
- const results = await searchDb(query)
136
- return {
137
- results, // full structured data for the UI
138
- totalCount: results.length,
139
- metadata: { /* ... */ },
140
- }
141
- }).modelOutput((result) => {
142
- // The MODEL only sees this condensed string on its next step
143
- // The UI still receives the full structured result above
144
- return `Found ${result.totalCount} results: ${result.results.map(r => r.title).join(', ')}`
145
- })
146
- ```
147
-
148
- ### 6. Dynamic tools (runtime-defined schemas)
149
-
150
- ```ts
151
- import { dynamicTool } from '@rudderjs/ai'
152
-
153
- // When the schema isn't known at compile time
154
- const tool = dynamicTool({
155
- name: agentDef.slug,
156
- description: agentDef.description,
157
- inputSchema: z.object({}),
158
- }).server(async () => {
159
- return await agentDef.run()
160
- })
161
- ```
162
-
163
- ### 7. Tool with ToolCallContext
164
-
165
- ```ts
166
- const myTool = toolDefinition({
167
- name: 'my_tool',
168
- description: 'A tool that needs its call ID',
169
- inputSchema: z.object({ data: z.string() }),
170
- }).server(async (input, ctx) => {
171
- // ctx.toolCallId is the unique ID the model assigned to this call
172
- console.log(`Tool call ID: ${ctx?.toolCallId}`)
173
- return { processed: true }
174
- })
175
- ```
176
-
177
- ### 8. Lazy tools (not advertised until needed)
178
-
179
- ```ts
180
- const secretTool = toolDefinition({
181
- name: 'admin_panel',
182
- description: 'Access admin functions',
183
- inputSchema: z.object({ action: z.string() }),
184
- lazy: true, // not included in the tool list sent to the model
185
- }).server(async ({ action }) => {
186
- // Only callable if the model explicitly names it
187
- return { result: await adminAction(action) }
188
- })
189
- ```
190
-
191
- ### 9. Pause the parent loop from inside a server tool
192
-
193
- ```ts
194
- import { pauseForClientTools, pauseForApproval } from '@rudderjs/ai'
195
-
196
- const runSubAgentTool = toolDefinition({
197
- name: 'run_sub_agent',
198
- description: 'Run a sub-agent that may need browser tools or approval',
199
- inputSchema: z.object({ task: z.string() }),
200
- }).server(async function* ({ task }, ctx) {
201
- const subResponse = await runSubAgent(task)
202
-
203
- if (subResponse.pendingClientToolCalls?.length) {
204
- // Client-tool pause -- surface inner client tool calls to the browser
205
- yield pauseForClientTools(subResponse.pendingClientToolCalls, subResponse.resumeId)
206
- return undefined as never // unreachable after pause
207
- }
208
-
209
- if (subResponse.pendingApprovalToolCall) {
210
- // Approval pause -- surface the gated tool call for the user to approve/reject
211
- const { toolCall, isClientTool } = subResponse.pendingApprovalToolCall
212
- yield pauseForApproval(toolCall, isClientTool, subResponse.resumeId)
213
- return undefined as never
214
- }
215
-
216
- return subResponse.text
217
- })
218
- ```
219
-
220
- `Agent.asTool({ suspendable })` does this automatically — yield manually only for hand-rolled sub-agent runners or non-agent tools that need a browser/approval round-trip.
221
-
222
- ### 10. Using tools with an agent
223
-
224
- ```ts
225
- import { Agent } from '@rudderjs/ai'
226
- import type { HasTools, AnyTool } from '@rudderjs/ai'
227
-
228
- class MyAgent extends Agent implements HasTools {
229
- instructions() { return 'You are a helpful assistant with access to tools.' }
230
-
231
- tools(): AnyTool[] {
232
- return [
233
- weatherTool,
234
- searchTool,
235
- analyzeDataTool,
236
- deleteUserTool,
237
- ]
238
- }
239
- }
240
-
241
- // Or with the anonymous agent
242
- const response = await agent({
243
- instructions: 'You are helpful.',
244
- tools: [weatherTool, searchTool],
245
- }).prompt('What is the weather in Paris?')
246
- ```
247
-
248
- ## Examples
249
-
250
- Tools are typically defined in `app/Tools/` or co-located with the agent that uses them. See `packages/ai/src/tool.ts` for the full builder API.
251
-
252
- ## Common pitfalls
253
-
254
- - **Zod schemas required**: Tool input schemas must be Zod objects. They are converted to JSON Schema for each provider automatically.
255
- - **Generator vs async function**: Use `async function*` only when you need streaming progress yields. For simple tools, use a regular `async` function.
256
- - **modelOutput is optional**: Only use `.modelOutput()` when the tool returns large structured data that would waste model context. The default behavior is `JSON.stringify` of the result.
257
- - **Approval flow is two-step**: When a tool needs approval, the loop stops. You must resume with `approvedToolCallIds` or `rejectedToolCallIds` in the next `prompt()` call's options. For approval-gated tools inside a sub-agent wrapped via `asTool({ suspendable })`, resume goes through `Agent.resumeAsTool(subRunId, [], { runStore, agent, approvedToolCallIds })` — the snapshot's `pauseKind: 'approval'` discriminator routes the resume contract.
258
- - **Client tool placeholder mode**: By default, client tools without `execute` get a placeholder result and the loop continues. Pass `toolCallStreamingMode: 'stop-on-client-tool'` to pause instead.
259
- - **exactOptionalPropertyTypes**: If your tsconfig has this enabled, do not pass `undefined` for optional tool parameters -- omit the key entirely.
260
- - **Tool name conventions**: Use `snake_case` for tool names (e.g. `get_weather`, `search_documents`). This matches what AI models expect.
@@ -1,161 +0,0 @@
1
- import type { AiMessage, ToolCall } from './types.js';
2
- /**
3
- * Discriminator for the kind of pause a standalone run is parked on.
4
- * Mirrors {@link SubAgentPauseKind} — the two run-store families share a
5
- * vocabulary so a host can persist sub-agent and top-level pauses the same way.
6
- *
7
- * - `'client_tool'` — the run surfaced one or more client tools; resume must
8
- * carry one tool-result per id in `pendingToolCallIds`. The default when the
9
- * field is absent (older snapshots stay readable after an upgrade).
10
- * - `'approval'` — the run stopped on an approval gate; resume must carry an
11
- * approve/reject decision covering the single id in `pendingToolCallIds`.
12
- */
13
- export type AgentPauseKind = 'client_tool' | 'approval';
14
- /**
15
- * Snapshot of a paused **standalone** (top-level) agent run — the state a host
16
- * persists between an `agent.stream()` that parks on a client tool or approval
17
- * gate and the follow-up request that resumes it.
18
- *
19
- * This is the standalone sibling of {@link SubAgentRunSnapshot}: same idea, but
20
- * for a top-level `stream()` rather than an `Agent.asTool` sub-run. The shape is
21
- * intentionally replay-ready — `messages` is the full conversation up to the
22
- * pause point, so resume only appends the incoming client-tool results (or
23
- * injects the approval decision) and re-enters `stream()` in `messages` mode.
24
- */
25
- export interface AgentRunState {
26
- /** Full conversation history at suspend time (system + user + every interleaved assistant/tool message). */
27
- messages: AiMessage[];
28
- /**
29
- * Tool-call ids the run is waiting on.
30
- *
31
- * - `pauseKind === 'client_tool'` (default): one entry per client tool the
32
- * loop surfaced; resume appends one result per id.
33
- * - `pauseKind === 'approval'`: a single entry for the approval-gated tool
34
- * call; resume injects the id into the approve or reject set.
35
- */
36
- pendingToolCallIds: string[];
37
- /** Total steps the run has executed across all suspends so far. */
38
- stepsSoFar: number;
39
- /** Total prompt+completion tokens accumulated across all suspends. */
40
- tokensSoFar: number;
41
- /**
42
- * Discriminator for the resume contract. Defaults to `'client_tool'` when
43
- * absent so snapshots written before approval-pause support stay readable.
44
- */
45
- pauseKind?: AgentPauseKind;
46
- /**
47
- * Approval pauses only. The full pending tool-call payload (name + args + id)
48
- * so a renderer can show "approve `delete_user(id=42)`?" without re-running
49
- * the agent. Mirrors `AgentResponse.pendingApprovalToolCall`.
50
- */
51
- pendingApprovalToolCall?: {
52
- toolCall: ToolCall;
53
- isClientTool: boolean;
54
- };
55
- /**
56
- * Opaque metadata the host can pass through. The framework treats this as
57
- * JSON and never reads it — useful for rehydrating request context
58
- * (e.g. `{ userId, threadId, agentSlug }`) around the resume call.
59
- */
60
- meta?: unknown;
61
- }
62
- /**
63
- * Pluggable persistence backend for paused standalone agent runs. The framework
64
- * ships two reference implementations:
65
- *
66
- * - {@link InMemoryAgentRunStore} — a `Map`-backed store. Single-process only;
67
- * fine for unit tests and small dev setups, lossy across worker processes and
68
- * restarts.
69
- * - {@link CachedAgentRunStore} — lazy adapter on top of `@rudderjs/cache`.
70
- * Cross-process / cross-restart when the cache is configured with redis or any
71
- * non-memory driver.
72
- *
73
- * Hosts may implement their own (Redis directly, Prisma, etc.) by satisfying
74
- * this interface.
75
- *
76
- * The split between {@link load} (non-destructive peek) and {@link consume}
77
- * (atomic single-use read+delete) matters: a host can `load()` to render a
78
- * "waiting for approval" view on a GET without burning the run, then `consume()`
79
- * on the resume POST so a forged or replayed `runId` cannot read data twice.
80
- */
81
- export interface AgentRunStore {
82
- /** Persist a snapshot under `runId`. Implementations MAY apply a TTL. */
83
- store(runId: string, state: AgentRunState): Promise<void>;
84
- /**
85
- * Non-destructive read. Returns `null` if the id is unknown or the snapshot
86
- * has expired. Leaves the snapshot in place — use for read-only peeks
87
- * (rendering a pending-run view); use {@link consume} when resuming.
88
- */
89
- load(runId: string): Promise<AgentRunState | null>;
90
- /**
91
- * Atomic read + delete. Returns `null` if the id is unknown or the snapshot
92
- * has expired. Single-use semantics matter: a forged or replayed `runId` must
93
- * not return data twice.
94
- */
95
- consume(runId: string): Promise<AgentRunState | null>;
96
- }
97
- /**
98
- * Generate a fresh, hard-to-guess run id. Uses `crypto.randomUUID()` where the
99
- * runtime exposes it (Node ≥ 16.7, every modern browser, Deno, Bun), falling
100
- * back to a timestamp + random suffix. Run ids are unguessable on purpose — a
101
- * `runId` is a capability handle to a parked conversation, so a predictable id
102
- * would let a third party `consume()` someone else's run.
103
- */
104
- export declare function newAgentRunId(): string;
105
- /**
106
- * `Map`-backed implementation suitable for tests and single-process dev.
107
- * Loses state across restarts and worker processes — for any multi-worker
108
- * deployment, use {@link CachedAgentRunStore} or a custom backend.
109
- */
110
- export declare class InMemoryAgentRunStore implements AgentRunStore {
111
- private readonly states;
112
- store(runId: string, state: AgentRunState): Promise<void>;
113
- load(runId: string): Promise<AgentRunState | null>;
114
- consume(runId: string): Promise<AgentRunState | null>;
115
- /** Test helper — clears all snapshots without consuming. */
116
- clear(): void;
117
- }
118
- /**
119
- * Minimal structural shape of a cache adapter (the methods this store touches).
120
- * Mirrors `@rudderjs/cache`'s `CacheAdapter` so the dep stays structural — the
121
- * framework's main entry stays runtime-agnostic.
122
- */
123
- interface CacheStoreLike {
124
- get<T = unknown>(key: string): Promise<T | null>;
125
- set(key: string, value: unknown, ttlSeconds?: number): Promise<void>;
126
- forget(key: string): Promise<void>;
127
- }
128
- export interface CachedAgentRunStoreOptions {
129
- /**
130
- * Cache adapter to use. When omitted, the store loads `@rudderjs/cache`
131
- * lazily and falls back to the registered global adapter
132
- * (`CacheRegistry.get()`); throws if neither resolves.
133
- */
134
- cache?: CacheStoreLike;
135
- /** Key namespace prefix. Default `'rudderjs:ai:agent-run:'`. */
136
- keyPrefix?: string;
137
- /** Time-to-live in seconds. Default 5 minutes. */
138
- ttlSeconds?: number;
139
- }
140
- /**
141
- * Standalone agent run store backed by `@rudderjs/cache`. Loads the cache
142
- * adapter lazily so `@rudderjs/ai`'s main entry stays runtime-agnostic (no
143
- * static import on the cache package).
144
- *
145
- * Default TTL is 5 minutes — long enough for a browser to round-trip a few
146
- * client tool calls or an approval decision, short enough that abandoned runs
147
- * garbage-collect promptly and the storage bill stays bounded.
148
- */
149
- export declare class CachedAgentRunStore implements AgentRunStore {
150
- private readonly explicitCache?;
151
- private readonly keyPrefix;
152
- private readonly ttlSeconds;
153
- private resolvedCache?;
154
- constructor(opts?: CachedAgentRunStoreOptions);
155
- private getCache;
156
- store(runId: string, state: AgentRunState): Promise<void>;
157
- load(runId: string): Promise<AgentRunState | null>;
158
- consume(runId: string): Promise<AgentRunState | null>;
159
- }
160
- export {};
161
- //# sourceMappingURL=agent-run-store.d.ts.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"agent-run-store.d.ts","sourceRoot":"","sources":["../src/agent-run-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,SAAS,EAAE,QAAQ,EAAE,MAAM,YAAY,CAAA;AAErD;;;;;;;;;;GAUG;AACH,MAAM,MAAM,cAAc,GAAG,aAAa,GAAG,UAAU,CAAA;AAEvD;;;;;;;;;;GAUG;AACH,MAAM,WAAW,aAAa;IAC5B,4GAA4G;IAC5G,QAAQ,EAAY,SAAS,EAAE,CAAA;IAC/B;;;;;;;OAOG;IACH,kBAAkB,EAAE,MAAM,EAAE,CAAA;IAC5B,mEAAmE;IACnE,UAAU,EAAU,MAAM,CAAA;IAC1B,sEAAsE;IACtE,WAAW,EAAS,MAAM,CAAA;IAC1B;;;OAGG;IACH,SAAS,CAAC,EAAU,cAAc,CAAA;IAClC;;;;OAIG;IACH,uBAAuB,CAAC,EAAE;QAAE,QAAQ,EAAE,QAAQ,CAAC;QAAC,YAAY,EAAE,OAAO,CAAA;KAAE,CAAA;IACvE;;;;OAIG;IACH,IAAI,CAAC,EAAE,OAAO,CAAA;CACf;AAED;;;;;;;;;;;;;;;;;;GAkBG;AACH,MAAM,WAAW,aAAa;IAC5B,yEAAyE;IACzE,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACzD;;;;OAIG;IACH,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;IAClD;;;;OAIG;IACH,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC,CAAA;CACtD;AAED;;;;;;GAMG;AACH,wBAAgB,aAAa,IAAI,MAAM,CAGtC;AAID;;;;GAIG;AACH,qBAAa,qBAAsB,YAAW,aAAa;IACzD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAmC;IAEpD,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAIzD,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAIlD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAO3D,4DAA4D;IAC5D,KAAK,IAAI,IAAI;CAGd;AAID;;;;GAIG;AACH,UAAU,cAAc;IACtB,GAAG,CAAC,CAAC,GAAG,OAAO,EAAE,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,CAAC,GAAG,IAAI,CAAC,CAAA;IAChD,GAAG,CAAC,GAAG,EAAE,MAAM,EAAE,KAAK,EAAE,OAAO,EAAE,UAAU,CAAC,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;IACpE,MAAM,CAAC,GAAG,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAAA;CACnC;AAED,MAAM,WAAW,0BAA0B;IACzC;;;;OAIG;IACH,KAAK,CAAC,EAAO,cAAc,CAAA;IAC3B,gEAAgE;IAChE,SAAS,CAAC,EAAG,MAAM,CAAA;IACnB,kDAAkD;IAClD,UAAU,CAAC,EAAE,MAAM,CAAA;CACpB;AAED;;;;;;;;GAQG;AACH,qBAAa,mBAAoB,YAAW,aAAa;IACvD,OAAO,CAAC,QAAQ,CAAC,aAAa,CAAC,CAAgB;IAC/C,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAa;IACvC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAY;IACvC,OAAO,CAAC,aAAa,CAAC,CAAyB;gBAEnC,IAAI,GAAE,0BAA+B;YAMnC,QAAQ;IAuBhB,KAAK,CAAC,KAAK,EAAE,MAAM,EAAE,KAAK,EAAE,aAAa,GAAG,OAAO,CAAC,IAAI,CAAC;IAKzD,IAAI,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;IAKlD,OAAO,CAAC,KAAK,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,GAAG,IAAI,CAAC;CAQ5D"}
@@ -1,98 +0,0 @@
1
- /**
2
- * Generate a fresh, hard-to-guess run id. Uses `crypto.randomUUID()` where the
3
- * runtime exposes it (Node ≥ 16.7, every modern browser, Deno, Bun), falling
4
- * back to a timestamp + random suffix. Run ids are unguessable on purpose — a
5
- * `runId` is a capability handle to a parked conversation, so a predictable id
6
- * would let a third party `consume()` someone else's run.
7
- */
8
- export function newAgentRunId() {
9
- if (typeof globalThis.crypto?.randomUUID === 'function')
10
- return globalThis.crypto.randomUUID();
11
- return `run-${Date.now().toString(36)}-${Math.random().toString(36).slice(2, 12)}`;
12
- }
13
- // ─── In-memory ─────────────────────────────────────────────
14
- /**
15
- * `Map`-backed implementation suitable for tests and single-process dev.
16
- * Loses state across restarts and worker processes — for any multi-worker
17
- * deployment, use {@link CachedAgentRunStore} or a custom backend.
18
- */
19
- export class InMemoryAgentRunStore {
20
- states = new Map();
21
- async store(runId, state) {
22
- this.states.set(runId, state);
23
- }
24
- async load(runId) {
25
- return this.states.get(runId) ?? null;
26
- }
27
- async consume(runId) {
28
- const state = this.states.get(runId);
29
- if (!state)
30
- return null;
31
- this.states.delete(runId);
32
- return state;
33
- }
34
- /** Test helper — clears all snapshots without consuming. */
35
- clear() {
36
- this.states.clear();
37
- }
38
- }
39
- /**
40
- * Standalone agent run store backed by `@rudderjs/cache`. Loads the cache
41
- * adapter lazily so `@rudderjs/ai`'s main entry stays runtime-agnostic (no
42
- * static import on the cache package).
43
- *
44
- * Default TTL is 5 minutes — long enough for a browser to round-trip a few
45
- * client tool calls or an approval decision, short enough that abandoned runs
46
- * garbage-collect promptly and the storage bill stays bounded.
47
- */
48
- export class CachedAgentRunStore {
49
- explicitCache;
50
- keyPrefix;
51
- ttlSeconds;
52
- resolvedCache;
53
- constructor(opts = {}) {
54
- if (opts.cache)
55
- this.explicitCache = opts.cache;
56
- this.keyPrefix = opts.keyPrefix ?? 'rudderjs:ai:agent-run:';
57
- this.ttlSeconds = opts.ttlSeconds ?? 5 * 60;
58
- }
59
- async getCache() {
60
- if (this.resolvedCache)
61
- return this.resolvedCache;
62
- if (this.explicitCache) {
63
- this.resolvedCache = this.explicitCache;
64
- return this.resolvedCache;
65
- }
66
- // Lazy-import @rudderjs/cache and ask the registry for the active adapter.
67
- // This keeps the static import surface zero — the import only fires when the
68
- // host actually opts into suspendable standalone runs. We dodge static
69
- // module-resolution by using an indirected specifier so `@rudderjs/cache`
70
- // doesn't need to be a declared dep of `@rudderjs/ai` (optional runtime peer).
71
- const cacheSpecifier = '@rudderjs/cache';
72
- const mod = await import(/* @vite-ignore */ cacheSpecifier);
73
- const adapter = mod.CacheRegistry?.get?.();
74
- if (!adapter) {
75
- throw new Error('[Rudder AI] CachedAgentRunStore needs a cache adapter. Install `@rudderjs/cache`, register a driver, or pass `{ cache }` explicitly.');
76
- }
77
- this.resolvedCache = adapter;
78
- return adapter;
79
- }
80
- async store(runId, state) {
81
- const cache = await this.getCache();
82
- await cache.set(this.keyPrefix + runId, state, this.ttlSeconds);
83
- }
84
- async load(runId) {
85
- const cache = await this.getCache();
86
- return (await cache.get(this.keyPrefix + runId)) ?? null;
87
- }
88
- async consume(runId) {
89
- const cache = await this.getCache();
90
- const key = this.keyPrefix + runId;
91
- const state = await cache.get(key);
92
- if (!state)
93
- return null;
94
- await cache.forget(key);
95
- return state;
96
- }
97
- }
98
- //# sourceMappingURL=agent-run-store.js.map
@@ -1 +0,0 @@
1
- {"version":3,"file":"agent-run-store.js","sourceRoot":"","sources":["../src/agent-run-store.ts"],"names":[],"mappings":"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"}
@@ -1,153 +0,0 @@
1
- /**
2
- * Named-event SSE protocol for streaming an agent loop to a browser.
3
- *
4
- * `@rudderjs/ai` already ships the Vercel AI SDK data-stream protocol
5
- * ({@link toVercelDataStream}) - the numeric-prefix wire (`0:` / `9:` / `a:`
6
- * ...). This is the alternative for apps that want a plain
7
- * `text/event-stream` with self-describing event names that mirror the agent
8
- * loop's own lifecycle (`text`, `tool_call`, `tool_update`, `tool_result`,
9
- * `pending_client_tools`, `tool_approval_required`, `handoff`, `complete`,
10
- * `error`).
11
- *
12
- * Both ends live here so the wire vocabulary can never drift:
13
- *
14
- * - Server: {@link toAgentSseStream} / {@link toAgentSseResponse} project an
15
- * `agent.stream()` result onto the named events and frame them as SSE.
16
- * - Browser: {@link readAgentStream} decodes the same events back into an
17
- * {@link AgentStreamTurn} and fires per-event callbacks for UI side effects.
18
- * {@link applyAgentSseEvent} is exposed so the per-event reducer can be
19
- * unit-tested against a synthetic turn.
20
- *
21
- * Runtime-agnostic: uses only web globals (`ReadableStream`, `Response`,
22
- * `TextEncoder` / `TextDecoder`, `crypto.randomUUID`), no `node:` imports, so
23
- * the module is safe in the main entry and runs server-side (Node / edge) and
24
- * client-side alike.
25
- *
26
- * This ships the framework-generic core. App-specific events (conversation
27
- * ids, billing, sub-run fan-out bookkeeping, server-authoritative history
28
- * sync) are not part of it - emit and decode those alongside this protocol on
29
- * your own channel.
30
- */
31
- import type { AgentStreamResponse, AiMessage, FinishReason, TokenUsage, ToolCall } from './types.js';
32
- /** The named SSE events this protocol emits, in agent-loop order. */
33
- export type AgentSseEventName = 'text' | 'tool_call' | 'tool_update' | 'tool_result' | 'pending_client_tools' | 'tool_approval_required' | 'handoff' | 'complete' | 'error';
34
- /** What the loop parked on when it paused, surfaced on the `complete` event. */
35
- export type AgentAwaiting = 'client_tools' | 'approval';
36
- export interface AgentSseTextPayload {
37
- text: string;
38
- }
39
- export interface AgentSseToolCallPayload {
40
- id?: string;
41
- tool: string;
42
- input?: Record<string, unknown>;
43
- }
44
- export interface AgentSseToolUpdatePayload {
45
- id?: string;
46
- tool?: string;
47
- update: unknown;
48
- }
49
- export interface AgentSseToolResultPayload {
50
- id?: string;
51
- toolCallId?: string;
52
- tool?: string;
53
- /** String passthrough when the tool returned a string, else JSON-encoded. */
54
- content: string;
55
- }
56
- export interface AgentSsePendingClientToolsPayload {
57
- toolCalls: ToolCall[];
58
- }
59
- export interface AgentSseApprovalPayload {
60
- toolCall: ToolCall;
61
- isClientTool: boolean;
62
- }
63
- export interface AgentSseHandoffPayload {
64
- from: string;
65
- to: string;
66
- message?: string;
67
- }
68
- export interface AgentSseCompletePayload {
69
- done: true;
70
- finishReason?: FinishReason;
71
- awaiting?: AgentAwaiting;
72
- /** Number of model steps the run took (`response.steps.length`). */
73
- steps?: number;
74
- usage?: TokenUsage;
75
- }
76
- export interface AgentSseErrorPayload {
77
- message: string;
78
- }
79
- /**
80
- * Project an `agent.stream()` result onto the named-event SSE wire as a
81
- * `ReadableStream<Uint8Array>`.
82
- *
83
- * Iterates the chunk stream, emits one named event per loop chunk, then
84
- * awaits the `response` promise and emits a terminal `complete` event
85
- * carrying `done`, `finishReason`, the `awaiting` pause (if any), step count,
86
- * and usage. If iteration or the response throws, an `error` event is emitted
87
- * and the stream closes cleanly so the browser reader's `onError` fires.
88
- */
89
- export declare function toAgentSseStream(streaming: AgentStreamResponse): ReadableStream<Uint8Array>;
90
- /**
91
- * Wrap {@link toAgentSseStream} in a `Response` with the standard
92
- * `text/event-stream` headers (no caching, no proxy buffering). Return it
93
- * directly from a route handler.
94
- */
95
- export declare function toAgentSseResponse(streaming: AgentStreamResponse, init?: ResponseInit): Response;
96
- /**
97
- * The accumulated state of one streamed agent turn, built up event by event
98
- * by {@link readAgentStream}. Mirrors {@link AgentResponse} fields the browser
99
- * needs to render the turn and build the next continuation request.
100
- */
101
- export interface AgentStreamTurn {
102
- /** Concatenated `text` event deltas. */
103
- assistantText: string;
104
- /** Tool calls stamped by `tool_call` events (for the next continuation). */
105
- assistantToolCalls: ToolCall[];
106
- /** Server-side `role:'tool'` result messages from `tool_result` events. */
107
- serverToolResults: AiMessage[];
108
- /** Client tool calls from a `pending_client_tools` event to run locally. */
109
- pendingClientTools: ToolCall[];
110
- /** Approval pause from a `tool_approval_required` event. */
111
- pendingApproval: AgentSseApprovalPayload | null;
112
- /** Chain of agent class names traversed via `handoff` events. */
113
- handoffPath: string[];
114
- /** `true` once a `complete` event with `done: true` arrived. */
115
- done: boolean;
116
- /** What the run paused on, from the `complete` event. */
117
- awaiting: AgentAwaiting | undefined;
118
- }
119
- /** A fresh, empty {@link AgentStreamTurn}. */
120
- export declare function newAgentStreamTurn(): AgentStreamTurn;
121
- /** Per-event callbacks fired by {@link readAgentStream} for UI side effects. */
122
- export interface AgentStreamCallbacks {
123
- onText?: (text: string) => void;
124
- onToolCall?: (call: AgentSseToolCallPayload) => void;
125
- onToolUpdate?: (update: AgentSseToolUpdatePayload) => void;
126
- onToolResult?: (result: AgentSseToolResultPayload) => void;
127
- onPendingClientTools?: (toolCalls: ToolCall[]) => void;
128
- onToolApprovalRequired?: (approval: AgentSseApprovalPayload) => void;
129
- onHandoff?: (handoff: AgentSseHandoffPayload) => void;
130
- onComplete?: (data: AgentSseCompletePayload) => void;
131
- onError?: (error: AgentSseErrorPayload) => void;
132
- /** Fired for any SSE event outside the standard protocol vocabulary. Use this
133
- * to observe app-specific events (e.g. a server-issued `run_started` carrying
134
- * a `runId`) without consuming the response body twice. */
135
- onAppEvent?: (event: string, data: unknown) => void;
136
- }
137
- /**
138
- * Read a named-event agent-SSE response body, applying each event to a fresh
139
- * {@link AgentStreamTurn} and firing the matching callback. Resolves with the
140
- * accumulated turn once the stream closes.
141
- *
142
- * The caller owns the `fetch` and the `!resp.ok` branch (so a rich error body
143
- * can be read for non-2xx responses); pass an already-OK response. A missing
144
- * body resolves to an empty turn. Malformed event JSON is skipped.
145
- */
146
- export declare function readAgentStream(resp: Response, callbacks?: AgentStreamCallbacks): Promise<AgentStreamTurn>;
147
- /**
148
- * Apply a single parsed SSE event to the turn state and fire the matching
149
- * callback. Mutates `turn`; otherwise pure. Exposed for unit-testing the
150
- * reducer against a synthetic turn without a live stream.
151
- */
152
- export declare function applyAgentSseEvent(event: string, data: unknown, turn: AgentStreamTurn, callbacks?: AgentStreamCallbacks): void;
153
- //# sourceMappingURL=agent-sse.d.ts.map