@rudderjs/ai 1.17.2 → 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.
- package/README.md +19 -1274
- package/dist/budget-orm/index.d.ts +1 -95
- package/dist/budget-orm/index.d.ts.map +1 -1
- package/dist/budget-orm/index.js +4 -176
- package/dist/budget-orm/index.js.map +1 -1
- package/dist/chat-mentions.d.ts +1 -58
- package/dist/chat-mentions.d.ts.map +1 -1
- package/dist/chat-mentions.js +4 -80
- package/dist/chat-mentions.js.map +1 -1
- package/dist/commands/ai-eval.d.ts +1 -92
- package/dist/commands/ai-eval.d.ts.map +1 -1
- package/dist/commands/ai-eval.js +4 -377
- package/dist/commands/ai-eval.js.map +1 -1
- package/dist/commands/make-agent.d.ts +1 -2
- package/dist/commands/make-agent.d.ts.map +1 -1
- package/dist/commands/make-agent.js +4 -22
- package/dist/commands/make-agent.js.map +1 -1
- package/dist/computer-use/index.d.ts +1 -52
- package/dist/computer-use/index.d.ts.map +1 -1
- package/dist/computer-use/index.js +4 -50
- package/dist/computer-use/index.js.map +1 -1
- package/dist/conversation-orm/index.d.ts +1 -108
- package/dist/conversation-orm/index.d.ts.map +1 -1
- package/dist/conversation-orm/index.js +4 -214
- package/dist/conversation-orm/index.js.map +1 -1
- package/dist/doctor.d.ts +1 -1
- package/dist/doctor.d.ts.map +1 -1
- package/dist/doctor.js +4 -65
- package/dist/doctor.js.map +1 -1
- package/dist/eval/index.d.ts +1 -270
- package/dist/eval/index.d.ts.map +1 -1
- package/dist/eval/index.js +4 -509
- package/dist/eval/index.js.map +1 -1
- package/dist/gateway/index.d.ts +1 -10
- package/dist/gateway/index.d.ts.map +1 -1
- package/dist/gateway/index.js +4 -10
- package/dist/gateway/index.js.map +1 -1
- package/dist/index.d.ts +1 -66
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -78
- package/dist/index.js.map +1 -1
- package/dist/mcp/index.d.ts +1 -15
- package/dist/mcp/index.d.ts.map +1 -1
- package/dist/mcp/index.js +4 -14
- package/dist/mcp/index.js.map +1 -1
- package/dist/memory-embedding/index.d.ts +1 -120
- package/dist/memory-embedding/index.d.ts.map +1 -1
- package/dist/memory-embedding/index.js +4 -228
- package/dist/memory-embedding/index.js.map +1 -1
- package/dist/memory-orm/index.d.ts +1 -117
- package/dist/memory-orm/index.d.ts.map +1 -1
- package/dist/memory-orm/index.js +4 -186
- package/dist/memory-orm/index.js.map +1 -1
- package/dist/node/index.d.ts +1 -2
- package/dist/node/index.d.ts.map +1 -1
- package/dist/node/index.js +4 -2
- package/dist/node/index.js.map +1 -1
- package/dist/observers.d.ts +1 -129
- package/dist/observers.d.ts.map +1 -1
- package/dist/observers.js +4 -39
- package/dist/observers.js.map +1 -1
- package/dist/react/index.d.ts +1 -15
- package/dist/react/index.d.ts.map +1 -1
- package/dist/react/index.js +4 -15
- package/dist/react/index.js.map +1 -1
- package/dist/server/index.d.ts +1 -1
- package/dist/server/index.d.ts.map +1 -1
- package/dist/server/index.js +4 -1
- package/dist/server/index.js.map +1 -1
- package/package.json +9 -13
- package/boost/guidelines.md +0 -260
- package/boost/skills/ai-agents/SKILL.md +0 -240
- package/boost/skills/ai-tools/SKILL.md +0 -260
- package/dist/agent-run-store.d.ts +0 -161
- package/dist/agent-run-store.d.ts.map +0 -1
- package/dist/agent-run-store.js +0 -98
- package/dist/agent-run-store.js.map +0 -1
- package/dist/agent-sse.d.ts +0 -153
- package/dist/agent-sse.d.ts.map +0 -1
- package/dist/agent-sse.js +0 -282
- package/dist/agent-sse.js.map +0 -1
- package/dist/agent.d.ts +0 -508
- package/dist/agent.d.ts.map +0 -1
- package/dist/agent.js +0 -1538
- package/dist/agent.js.map +0 -1
- package/dist/attachment.d.ts +0 -31
- package/dist/attachment.d.ts.map +0 -1
- package/dist/attachment.js +0 -89
- package/dist/attachment.js.map +0 -1
- package/dist/audio.d.ts +0 -45
- package/dist/audio.d.ts.map +0 -1
- package/dist/audio.js +0 -93
- package/dist/audio.js.map +0 -1
- package/dist/base64.d.ts +0 -7
- package/dist/base64.d.ts.map +0 -1
- package/dist/base64.js +0 -39
- package/dist/base64.js.map +0 -1
- package/dist/budget/pricing.d.ts +0 -124
- package/dist/budget/pricing.d.ts.map +0 -1
- package/dist/budget/pricing.js +0 -175
- package/dist/budget/pricing.js.map +0 -1
- package/dist/budget/storage.d.ts +0 -104
- package/dist/budget/storage.d.ts.map +0 -1
- package/dist/budget/storage.js +0 -0
- package/dist/budget/storage.js.map +0 -1
- package/dist/budget/with-budget.d.ts +0 -119
- package/dist/budget/with-budget.d.ts.map +0 -1
- package/dist/budget/with-budget.js +0 -175
- package/dist/budget/with-budget.js.map +0 -1
- package/dist/cached-embedding.d.ts +0 -14
- package/dist/cached-embedding.d.ts.map +0 -1
- package/dist/cached-embedding.js +0 -44
- package/dist/cached-embedding.js.map +0 -1
- package/dist/computer-use/actions.d.ts +0 -214
- package/dist/computer-use/actions.d.ts.map +0 -1
- package/dist/computer-use/actions.js +0 -48
- package/dist/computer-use/actions.js.map +0 -1
- package/dist/computer-use/errors.d.ts +0 -57
- package/dist/computer-use/errors.d.ts.map +0 -1
- package/dist/computer-use/errors.js +0 -76
- package/dist/computer-use/errors.js.map +0 -1
- package/dist/computer-use/playwright.d.ts +0 -76
- package/dist/computer-use/playwright.d.ts.map +0 -1
- package/dist/computer-use/playwright.js +0 -270
- package/dist/computer-use/playwright.js.map +0 -1
- package/dist/computer-use/tool.d.ts +0 -154
- package/dist/computer-use/tool.d.ts.map +0 -1
- package/dist/computer-use/tool.js +0 -210
- package/dist/computer-use/tool.js.map +0 -1
- package/dist/continuation-validation.d.ts +0 -85
- package/dist/continuation-validation.d.ts.map +0 -1
- package/dist/continuation-validation.js +0 -166
- package/dist/continuation-validation.js.map +0 -1
- package/dist/conversation-persistence.d.ts +0 -46
- package/dist/conversation-persistence.d.ts.map +0 -1
- package/dist/conversation-persistence.js +0 -176
- package/dist/conversation-persistence.js.map +0 -1
- package/dist/conversation.d.ts +0 -11
- package/dist/conversation.d.ts.map +0 -1
- package/dist/conversation.js +0 -55
- package/dist/conversation.js.map +0 -1
- package/dist/eval/fixtures.d.ts +0 -65
- package/dist/eval/fixtures.d.ts.map +0 -1
- package/dist/eval/fixtures.js +0 -110
- package/dist/eval/fixtures.js.map +0 -1
- package/dist/eval/html-reporter.d.ts +0 -25
- package/dist/eval/html-reporter.d.ts.map +0 -1
- package/dist/eval/html-reporter.js +0 -209
- package/dist/eval/html-reporter.js.map +0 -1
- package/dist/eval/json-reporter.d.ts +0 -43
- package/dist/eval/json-reporter.d.ts.map +0 -1
- package/dist/eval/json-reporter.js +0 -40
- package/dist/eval/json-reporter.js.map +0 -1
- package/dist/facade.d.ts +0 -96
- package/dist/facade.d.ts.map +0 -1
- package/dist/facade.js +0 -146
- package/dist/facade.js.map +0 -1
- package/dist/fake.d.ts +0 -201
- package/dist/fake.d.ts.map +0 -1
- package/dist/fake.js +0 -428
- package/dist/fake.js.map +0 -1
- package/dist/file-search.d.ts +0 -168
- package/dist/file-search.d.ts.map +0 -1
- package/dist/file-search.js +0 -158
- package/dist/file-search.js.map +0 -1
- package/dist/files.d.ts +0 -27
- package/dist/files.d.ts.map +0 -1
- package/dist/files.js +0 -44
- package/dist/files.js.map +0 -1
- package/dist/gateway/http-gateway-adapter.d.ts +0 -94
- package/dist/gateway/http-gateway-adapter.d.ts.map +0 -1
- package/dist/gateway/http-gateway-adapter.js +0 -106
- package/dist/gateway/http-gateway-adapter.js.map +0 -1
- package/dist/gateway/sse.d.ts +0 -28
- package/dist/gateway/sse.d.ts.map +0 -1
- package/dist/gateway/sse.js +0 -78
- package/dist/gateway/sse.js.map +0 -1
- package/dist/handoff.d.ts +0 -95
- package/dist/handoff.d.ts.map +0 -1
- package/dist/handoff.js +0 -78
- package/dist/handoff.js.map +0 -1
- package/dist/handoffs-driver.d.ts +0 -58
- package/dist/handoffs-driver.d.ts.map +0 -1
- package/dist/handoffs-driver.js +0 -103
- package/dist/handoffs-driver.js.map +0 -1
- package/dist/image.d.ts +0 -40
- package/dist/image.d.ts.map +0 -1
- package/dist/image.js +0 -109
- package/dist/image.js.map +0 -1
- package/dist/mcp/client-tools.d.ts +0 -39
- package/dist/mcp/client-tools.d.ts.map +0 -1
- package/dist/mcp/client-tools.js +0 -147
- package/dist/mcp/client-tools.js.map +0 -1
- package/dist/mcp/server-from-agent.d.ts +0 -24
- package/dist/mcp/server-from-agent.d.ts.map +0 -1
- package/dist/mcp/server-from-agent.js +0 -113
- package/dist/mcp/server-from-agent.js.map +0 -1
- package/dist/mcp/types.d.ts +0 -64
- package/dist/mcp/types.d.ts.map +0 -1
- package/dist/mcp/types.js +0 -6
- package/dist/mcp/types.js.map +0 -1
- package/dist/memory-extract.d.ts +0 -60
- package/dist/memory-extract.d.ts.map +0 -1
- package/dist/memory-extract.js +0 -163
- package/dist/memory-extract.js.map +0 -1
- package/dist/memory-inject.d.ts +0 -39
- package/dist/memory-inject.d.ts.map +0 -1
- package/dist/memory-inject.js +0 -135
- package/dist/memory-inject.js.map +0 -1
- package/dist/memory.d.ts +0 -55
- package/dist/memory.d.ts.map +0 -1
- package/dist/memory.js +0 -132
- package/dist/memory.js.map +0 -1
- package/dist/middleware.d.ts +0 -18
- package/dist/middleware.d.ts.map +0 -1
- package/dist/middleware.js +0 -72
- package/dist/middleware.js.map +0 -1
- package/dist/node/attachment.d.ts +0 -6
- package/dist/node/attachment.d.ts.map +0 -1
- package/dist/node/attachment.js +0 -35
- package/dist/node/attachment.js.map +0 -1
- package/dist/node/transcription.d.ts +0 -4
- package/dist/node/transcription.d.ts.map +0 -1
- package/dist/node/transcription.js +0 -8
- package/dist/node/transcription.js.map +0 -1
- package/dist/output.d.ts +0 -22
- package/dist/output.d.ts.map +0 -1
- package/dist/output.js +0 -60
- package/dist/output.js.map +0 -1
- package/dist/provider-tools.d.ts +0 -87
- package/dist/provider-tools.d.ts.map +0 -1
- package/dist/provider-tools.js +0 -189
- package/dist/provider-tools.js.map +0 -1
- package/dist/providers/anthropic.d.ts +0 -24
- package/dist/providers/anthropic.d.ts.map +0 -1
- package/dist/providers/anthropic.js +0 -405
- package/dist/providers/anthropic.js.map +0 -1
- package/dist/providers/azure.d.ts +0 -13
- package/dist/providers/azure.d.ts.map +0 -1
- package/dist/providers/azure.js +0 -15
- package/dist/providers/azure.js.map +0 -1
- package/dist/providers/bedrock.d.ts +0 -75
- package/dist/providers/bedrock.d.ts.map +0 -1
- package/dist/providers/bedrock.js +0 -181
- package/dist/providers/bedrock.js.map +0 -1
- package/dist/providers/cohere.d.ts +0 -13
- package/dist/providers/cohere.d.ts.map +0 -1
- package/dist/providers/cohere.js +0 -87
- package/dist/providers/cohere.js.map +0 -1
- package/dist/providers/deepseek.d.ts +0 -12
- package/dist/providers/deepseek.d.ts.map +0 -1
- package/dist/providers/deepseek.js +0 -15
- package/dist/providers/deepseek.js.map +0 -1
- package/dist/providers/elevenlabs.d.ts +0 -98
- package/dist/providers/elevenlabs.d.ts.map +0 -1
- package/dist/providers/elevenlabs.js +0 -229
- package/dist/providers/elevenlabs.js.map +0 -1
- package/dist/providers/google-cache-registry.d.ts +0 -132
- package/dist/providers/google-cache-registry.d.ts.map +0 -1
- package/dist/providers/google-cache-registry.js +0 -209
- package/dist/providers/google-cache-registry.js.map +0 -1
- package/dist/providers/google.d.ts +0 -38
- package/dist/providers/google.d.ts.map +0 -1
- package/dist/providers/google.js +0 -903
- package/dist/providers/google.js.map +0 -1
- package/dist/providers/groq.d.ts +0 -12
- package/dist/providers/groq.d.ts.map +0 -1
- package/dist/providers/groq.js +0 -15
- package/dist/providers/groq.js.map +0 -1
- package/dist/providers/jina.d.ts +0 -13
- package/dist/providers/jina.d.ts.map +0 -1
- package/dist/providers/jina.js +0 -90
- package/dist/providers/jina.js.map +0 -1
- package/dist/providers/mistral.d.ts +0 -13
- package/dist/providers/mistral.d.ts.map +0 -1
- package/dist/providers/mistral.js +0 -46
- package/dist/providers/mistral.js.map +0 -1
- package/dist/providers/ollama.d.ts +0 -11
- package/dist/providers/ollama.d.ts.map +0 -1
- package/dist/providers/ollama.js +0 -15
- package/dist/providers/ollama.js.map +0 -1
- package/dist/providers/openai.d.ts +0 -79
- package/dist/providers/openai.d.ts.map +0 -1
- package/dist/providers/openai.js +0 -792
- package/dist/providers/openai.js.map +0 -1
- package/dist/providers/openrouter.d.ts +0 -43
- package/dist/providers/openrouter.d.ts.map +0 -1
- package/dist/providers/openrouter.js +0 -21
- package/dist/providers/openrouter.js.map +0 -1
- package/dist/providers/voyage.d.ts +0 -91
- package/dist/providers/voyage.d.ts.map +0 -1
- package/dist/providers/voyage.js +0 -166
- package/dist/providers/voyage.js.map +0 -1
- package/dist/providers/xai.d.ts +0 -12
- package/dist/providers/xai.d.ts.map +0 -1
- package/dist/providers/xai.js +0 -15
- package/dist/providers/xai.js.map +0 -1
- package/dist/queue-job.d.ts +0 -100
- package/dist/queue-job.d.ts.map +0 -1
- package/dist/queue-job.js +0 -185
- package/dist/queue-job.js.map +0 -1
- package/dist/react/agent-run.d.ts +0 -111
- package/dist/react/agent-run.d.ts.map +0 -1
- package/dist/react/agent-run.js +0 -107
- package/dist/react/agent-run.js.map +0 -1
- package/dist/react/useAgentRun.d.ts +0 -68
- package/dist/react/useAgentRun.d.ts.map +0 -1
- package/dist/react/useAgentRun.js +0 -125
- package/dist/react/useAgentRun.js.map +0 -1
- package/dist/registry.d.ts +0 -45
- package/dist/registry.d.ts.map +0 -1
- package/dist/registry.js +0 -131
- package/dist/registry.js.map +0 -1
- package/dist/rerank.d.ts +0 -20
- package/dist/rerank.d.ts.map +0 -1
- package/dist/rerank.js +0 -40
- package/dist/rerank.js.map +0 -1
- package/dist/resume-approval.d.ts +0 -30
- package/dist/resume-approval.d.ts.map +0 -1
- package/dist/resume-approval.js +0 -147
- package/dist/resume-approval.js.map +0 -1
- package/dist/sanitize-conversation.d.ts +0 -43
- package/dist/sanitize-conversation.d.ts.map +0 -1
- package/dist/sanitize-conversation.js +0 -85
- package/dist/sanitize-conversation.js.map +0 -1
- package/dist/scoped-tool.d.ts +0 -98
- package/dist/scoped-tool.d.ts.map +0 -1
- package/dist/scoped-tool.js +0 -174
- package/dist/scoped-tool.js.map +0 -1
- package/dist/server/provider.d.ts +0 -22
- package/dist/server/provider.d.ts.map +0 -1
- package/dist/server/provider.js +0 -194
- package/dist/server/provider.js.map +0 -1
- package/dist/similarity-search.d.ts +0 -163
- package/dist/similarity-search.d.ts.map +0 -1
- package/dist/similarity-search.js +0 -147
- package/dist/similarity-search.js.map +0 -1
- package/dist/sub-agent-run-store.d.ts +0 -157
- package/dist/sub-agent-run-store.d.ts.map +0 -1
- package/dist/sub-agent-run-store.js +0 -87
- package/dist/sub-agent-run-store.js.map +0 -1
- package/dist/tool-execution.d.ts +0 -16
- package/dist/tool-execution.d.ts.map +0 -1
- package/dist/tool-execution.js +0 -498
- package/dist/tool-execution.js.map +0 -1
- package/dist/tool-helpers.d.ts +0 -77
- package/dist/tool-helpers.d.ts.map +0 -1
- package/dist/tool-helpers.js +0 -117
- package/dist/tool-helpers.js.map +0 -1
- package/dist/tool.d.ts +0 -216
- package/dist/tool.d.ts.map +0 -1
- package/dist/tool.js +0 -175
- package/dist/tool.js.map +0 -1
- package/dist/transcription.d.ts +0 -42
- package/dist/transcription.d.ts.map +0 -1
- package/dist/transcription.js +0 -77
- package/dist/transcription.js.map +0 -1
- package/dist/types.d.ts +0 -1020
- package/dist/types.d.ts.map +0 -1
- package/dist/types.js +0 -2
- package/dist/types.js.map +0 -1
- package/dist/util/hash.d.ts +0 -11
- package/dist/util/hash.d.ts.map +0 -1
- package/dist/util/hash.js +0 -23
- package/dist/util/hash.js.map +0 -1
- package/dist/vector-stores/index.d.ts +0 -96
- package/dist/vector-stores/index.d.ts.map +0 -1
- package/dist/vector-stores/index.js +0 -153
- package/dist/vector-stores/index.js.map +0 -1
- package/dist/vercel-protocol.d.ts +0 -18
- package/dist/vercel-protocol.d.ts.map +0 -1
- package/dist/vercel-protocol.js +0 -75
- package/dist/vercel-protocol.js.map +0 -1
- package/dist/zod-to-json-schema.d.ts +0 -16
- package/dist/zod-to-json-schema.d.ts.map +0 -1
- package/dist/zod-to-json-schema.js +0 -17
- 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 RudderJS 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,EA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AA;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"}
|
package/dist/agent-run-store.js
DELETED
|
@@ -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
|
-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