@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.
- 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
package/dist/mcp/client-tools.js
DELETED
|
@@ -1,147 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { dynamicTool } from '../tool.js';
|
|
3
|
-
const CLIENT_INFO = { name: 'rudderjs-ai-mcp-bridge', version: '1.0.0' };
|
|
4
|
-
/**
|
|
5
|
-
* Connect to a remote MCP server and surface its tools as Rudder `Tool`s.
|
|
6
|
-
*
|
|
7
|
-
* Three transport shapes are accepted:
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* // (a) HTTP — string URL or URL instance
|
|
11
|
-
* const t = await mcpClientTools('https://api.example.com/mcp')
|
|
12
|
-
*
|
|
13
|
-
* // (b) Local stdio subprocess
|
|
14
|
-
* const t = await mcpClientTools({ command: 'npx', args: ['some-mcp-server'] })
|
|
15
|
-
*
|
|
16
|
-
* // (c) Already-connected SDK Client (caller owns lifecycle)
|
|
17
|
-
* const t = await mcpClientTools(myClient)
|
|
18
|
-
* ```
|
|
19
|
-
*
|
|
20
|
-
* The returned array exposes a `close()` method when this call owns the client
|
|
21
|
-
* (cases a + b). Pass it back so the subprocess / HTTP session can shut down
|
|
22
|
-
* cleanly when your agent is done.
|
|
23
|
-
*
|
|
24
|
-
* The remote server's `inputSchema` (JSON Schema) ships through to providers
|
|
25
|
-
* via `ToolDefinitionOptions.jsonSchema` — no zod conversion in either direction.
|
|
26
|
-
*/
|
|
27
|
-
export async function mcpClientTools(transport, opts = {}) {
|
|
28
|
-
const streaming = opts.streaming ?? true;
|
|
29
|
-
const namePrefix = opts.namePrefix ?? '';
|
|
30
|
-
const { client, ownsClient } = await resolveClient(transport);
|
|
31
|
-
let toolList;
|
|
32
|
-
try {
|
|
33
|
-
const listed = await client.listTools();
|
|
34
|
-
toolList = listed.tools.filter(t => opts.filter ? opts.filter(t.name) : true);
|
|
35
|
-
}
|
|
36
|
-
catch (err) {
|
|
37
|
-
if (ownsClient)
|
|
38
|
-
await safeClose(client);
|
|
39
|
-
throw err;
|
|
40
|
-
}
|
|
41
|
-
const tools = toolList.map(t => buildTool(client, t, namePrefix, streaming));
|
|
42
|
-
const handle = ownsClient
|
|
43
|
-
? Object.defineProperty([...tools], 'close', {
|
|
44
|
-
value: () => safeClose(client),
|
|
45
|
-
enumerable: false,
|
|
46
|
-
writable: false,
|
|
47
|
-
})
|
|
48
|
-
: tools;
|
|
49
|
-
return handle;
|
|
50
|
-
}
|
|
51
|
-
async function resolveClient(transport) {
|
|
52
|
-
// Already a Client instance — duck-type check for `callTool` + `listTools`.
|
|
53
|
-
if (typeof transport === 'object' && transport !== null && 'callTool' in transport && 'listTools' in transport) {
|
|
54
|
-
return { client: transport, ownsClient: false };
|
|
55
|
-
}
|
|
56
|
-
// Lazy-load the SDK so apps that don't import @rudderjs/ai/mcp don't pay for it.
|
|
57
|
-
const { Client } = await import('@modelcontextprotocol/sdk/client/index.js');
|
|
58
|
-
const sdkTransport = await buildTransport(transport);
|
|
59
|
-
const client = new Client(CLIENT_INFO);
|
|
60
|
-
await client.connect(sdkTransport);
|
|
61
|
-
return { client, ownsClient: true };
|
|
62
|
-
}
|
|
63
|
-
async function buildTransport(transport) {
|
|
64
|
-
if (typeof transport === 'string' || transport instanceof URL) {
|
|
65
|
-
const url = transport instanceof URL ? transport : new URL(transport);
|
|
66
|
-
const { StreamableHTTPClientTransport } = await import('@modelcontextprotocol/sdk/client/streamableHttp.js');
|
|
67
|
-
return new StreamableHTTPClientTransport(url);
|
|
68
|
-
}
|
|
69
|
-
if (typeof transport === 'object' && transport !== null && 'command' in transport) {
|
|
70
|
-
const spawn = transport;
|
|
71
|
-
const { StdioClientTransport } = await import('@modelcontextprotocol/sdk/client/stdio.js');
|
|
72
|
-
return new StdioClientTransport({
|
|
73
|
-
command: spawn.command,
|
|
74
|
-
...(spawn.args !== undefined ? { args: [...spawn.args] } : {}),
|
|
75
|
-
...(spawn.env !== undefined ? { env: spawn.env } : {}),
|
|
76
|
-
...(spawn.cwd !== undefined ? { cwd: spawn.cwd } : {}),
|
|
77
|
-
});
|
|
78
|
-
}
|
|
79
|
-
throw new Error(`mcpClientTools: unsupported transport shape: ${typeof transport}`);
|
|
80
|
-
}
|
|
81
|
-
async function safeClose(client) {
|
|
82
|
-
try {
|
|
83
|
-
await client.close();
|
|
84
|
-
}
|
|
85
|
-
catch { /* best-effort */ }
|
|
86
|
-
}
|
|
87
|
-
function buildTool(client, remote, namePrefix, streaming) {
|
|
88
|
-
const localName = namePrefix + remote.name;
|
|
89
|
-
const builder = dynamicTool({
|
|
90
|
-
name: localName,
|
|
91
|
-
description: remote.description ?? '',
|
|
92
|
-
inputSchema: z.unknown(), // placeholder — real shape lives in jsonSchema
|
|
93
|
-
jsonSchema: remote.inputSchema,
|
|
94
|
-
});
|
|
95
|
-
if (streaming) {
|
|
96
|
-
const built = builder.server(async function* (input, _ctx) {
|
|
97
|
-
const collected = [];
|
|
98
|
-
const result = await client.callTool({ name: remote.name, arguments: (input ?? {}) }, undefined, { onprogress: (p) => collected.push(p) });
|
|
99
|
-
// SDK delivers progress notifications synchronously into onprogress during
|
|
100
|
-
// the request lifetime, so by the time we're here all progress events have
|
|
101
|
-
// arrived. Yielding them before returning preserves the observable order
|
|
102
|
-
// (tool-update chunks land before tool-result).
|
|
103
|
-
for (const p of collected)
|
|
104
|
-
yield p;
|
|
105
|
-
return mcpContentToString(result);
|
|
106
|
-
});
|
|
107
|
-
return built;
|
|
108
|
-
}
|
|
109
|
-
const built = builder.server(async (input) => {
|
|
110
|
-
const result = await client.callTool({ name: remote.name, arguments: (input ?? {}) });
|
|
111
|
-
return mcpContentToString(result);
|
|
112
|
-
});
|
|
113
|
-
return built;
|
|
114
|
-
}
|
|
115
|
-
/**
|
|
116
|
-
* Flatten an MCP tool result into a string for the agent's `tool_result` slot.
|
|
117
|
-
* Text blocks concatenate; image / resource blocks become bracketed placeholders
|
|
118
|
-
* so the model knows something non-text was returned.
|
|
119
|
-
*/
|
|
120
|
-
function mcpContentToString(result) {
|
|
121
|
-
const parts = [];
|
|
122
|
-
for (const block of result.content) {
|
|
123
|
-
if (typeof block !== 'object' || block === null)
|
|
124
|
-
continue;
|
|
125
|
-
const b = block;
|
|
126
|
-
if (b['type'] === 'text' && typeof b['text'] === 'string') {
|
|
127
|
-
parts.push(b['text']);
|
|
128
|
-
}
|
|
129
|
-
else if (b['type'] === 'image') {
|
|
130
|
-
parts.push(`[image: ${b['mimeType'] ?? 'unknown mime'}]`);
|
|
131
|
-
}
|
|
132
|
-
else if (b['type'] === 'resource' || b['type'] === 'resource_link') {
|
|
133
|
-
const ref = b['resource'] && typeof b['resource'] === 'object'
|
|
134
|
-
? b['resource']['uri']
|
|
135
|
-
: b['uri'];
|
|
136
|
-
parts.push(`[resource: ${ref ?? 'unknown'}]`);
|
|
137
|
-
}
|
|
138
|
-
else if (b['type']) {
|
|
139
|
-
parts.push(`[${b['type']}]`);
|
|
140
|
-
}
|
|
141
|
-
}
|
|
142
|
-
const text = parts.join('\n').trim();
|
|
143
|
-
if (result.isError)
|
|
144
|
-
return `[error] ${text || 'Tool reported an error'}`;
|
|
145
|
-
return text || '(empty result)';
|
|
146
|
-
}
|
|
147
|
-
//# sourceMappingURL=client-tools.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"client-tools.js","sourceRoot":"","sources":["../../src/mcp/client-tools.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AACvB,OAAO,EAAE,WAAW,EAAE,MAAM,YAAY,CAAA;AAMxC,MAAM,WAAW,GAAG,EAAE,IAAI,EAAE,wBAAwB,EAAE,OAAO,EAAE,OAAO,EAAW,CAAA;AAejF;;;;;;;;;;;;;;;;;;;;;;GAsBG;AACH,MAAM,CAAC,KAAK,UAAU,cAAc,CAClC,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AO,EAAE,CAAC;YACjC,KAAK,CAAC,IAAI,CAAC,WAAW,CAAC,CAAC,UAAU,CAAC,IAAI,cAAc,GAAG,CAAC,CAAA;QAC3D,CAAC;aAAM,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,UAAU,IAAI,CAAC,CAAC,MAAM,CAAC,KAAK,eAAe,EAAE,CAAC;YACrE,MAAM,GAAG,GAAG,CAAC,CAAC,UAAU,CAAC,IAAI,OAAO,CAAC,CAAC,UAAU,CAAC,KAAK,QAAQ;gBAC5D,CAAC,CAAE,CAAC,CAAC,UAAU,CAA6B,CAAC,KAAK,CAAC;gBACnD,CAAC,CAAC,CAAC,CAAC,KAAK,CAAC,CAAA;YACZ,KAAK,CAAC,IAAI,CAAC,cAAc,GAAG,IAAI,SAAS,GAAG,CAAC,CAAA;QAC/C,CAAC;aAAM,IAAI,CAAC,CAAC,MAAM,CAAC,EAAE,CAAC;YACrB,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,MAAM,CAAC,GAAG,CAAC,CAAA;QAC9B,CAAC;IACH,CAAC;IACD,MAAM,IAAI,GAAG,KAAK,CAAC,IAAI,CAAC,IAAI,CAAC,CAAC,IAAI,EAAE,CAAA;IACpC,IAAI,MAAM,CAAC,OAAO;QAAE,OAAO,WAAW,IAAI,IAAI,wBAAwB,EAAE,CAAA;IACxE,OAAO,IAAI,IAAI,gBAAgB,CAAA;AACjC,CAAC"}
|
|
@@ -1,24 +0,0 @@
|
|
|
1
|
-
import type { Agent } from '../agent.js';
|
|
2
|
-
import type { McpServerFromAgentOptions } from './types.js';
|
|
3
|
-
/**
|
|
4
|
-
* Wrap an `Agent` class as an MCP server. External MCP clients (Claude Desktop,
|
|
5
|
-
* Cursor, etc.) can connect to it like any other MCP server.
|
|
6
|
-
*
|
|
7
|
-
* Returns an `McpServer` from `@modelcontextprotocol/sdk` — connect it with the
|
|
8
|
-
* SDK's stdio / HTTP transports:
|
|
9
|
-
*
|
|
10
|
-
* ```ts
|
|
11
|
-
* import { mcpServerFromAgent } from '@rudderjs/ai/mcp'
|
|
12
|
-
* import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
13
|
-
*
|
|
14
|
-
* const server = await mcpServerFromAgent(MyAgent)
|
|
15
|
-
* await server.connect(new StdioServerTransport())
|
|
16
|
-
* ```
|
|
17
|
-
*
|
|
18
|
-
* Three exposure modes via `opts.expose`:
|
|
19
|
-
* - `'tools'` (default) — one MCP tool per `agent.tools()` entry
|
|
20
|
-
* - `'agent'` — one MCP tool that runs the whole agent (`prompt(text) → text`)
|
|
21
|
-
* - `'both'` — expose individual tools and the agent prompt-tool side by side
|
|
22
|
-
*/
|
|
23
|
-
export declare function mcpServerFromAgent(AgentClass: new () => Agent, opts?: McpServerFromAgentOptions): Promise<unknown>;
|
|
24
|
-
//# sourceMappingURL=server-from-agent.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server-from-agent.d.ts","sourceRoot":"","sources":["../../src/mcp/server-from-agent.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EAAE,KAAK,EAAE,MAAM,aAAa,CAAA;AAExC,OAAO,KAAK,EAAE,yBAAyB,EAAE,MAAM,YAAY,CAAA;AAE3D;;;;;;;;;;;;;;;;;;;GAmBG;AACH,wBAAsB,kBAAkB,CACtC,UAAU,EAAE,UAAU,KAAK,EAC3B,IAAI,GAAE,yBAA8B,GACnC,OAAO,CAAC,OAAO,CAAC,CAiClB"}
|
|
@@ -1,113 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
/**
|
|
3
|
-
* Wrap an `Agent` class as an MCP server. External MCP clients (Claude Desktop,
|
|
4
|
-
* Cursor, etc.) can connect to it like any other MCP server.
|
|
5
|
-
*
|
|
6
|
-
* Returns an `McpServer` from `@modelcontextprotocol/sdk` — connect it with the
|
|
7
|
-
* SDK's stdio / HTTP transports:
|
|
8
|
-
*
|
|
9
|
-
* ```ts
|
|
10
|
-
* import { mcpServerFromAgent } from '@rudderjs/ai/mcp'
|
|
11
|
-
* import { StdioServerTransport } from '@modelcontextprotocol/sdk/server/stdio.js'
|
|
12
|
-
*
|
|
13
|
-
* const server = await mcpServerFromAgent(MyAgent)
|
|
14
|
-
* await server.connect(new StdioServerTransport())
|
|
15
|
-
* ```
|
|
16
|
-
*
|
|
17
|
-
* Three exposure modes via `opts.expose`:
|
|
18
|
-
* - `'tools'` (default) — one MCP tool per `agent.tools()` entry
|
|
19
|
-
* - `'agent'` — one MCP tool that runs the whole agent (`prompt(text) → text`)
|
|
20
|
-
* - `'both'` — expose individual tools and the agent prompt-tool side by side
|
|
21
|
-
*/
|
|
22
|
-
export async function mcpServerFromAgent(AgentClass, opts = {}) {
|
|
23
|
-
const expose = opts.expose ?? 'tools';
|
|
24
|
-
const { McpServer } = await import('@modelcontextprotocol/sdk/server/mcp.js');
|
|
25
|
-
const agent = new AgentClass();
|
|
26
|
-
const instructions = opts.instructions ?? safeInstructions(agent);
|
|
27
|
-
const name = opts.name ?? `${AgentClass.name}Server`;
|
|
28
|
-
const version = opts.version ?? '1.0.0';
|
|
29
|
-
const server = new McpServer({ name, version }, instructions !== undefined ? { instructions } : {});
|
|
30
|
-
if (expose === 'tools' || expose === 'both') {
|
|
31
|
-
// `tools()` is opt-in via the HasTools interface — abstract `Agent` doesn't
|
|
32
|
-
// declare it. Cast and tolerate absence gracefully (subclass without tools()
|
|
33
|
-
// returns no tools but the agent prompt-tool path still works).
|
|
34
|
-
const hasTools = agent;
|
|
35
|
-
const toolList = typeof hasTools.tools === 'function' ? hasTools.tools() : [];
|
|
36
|
-
for (const tool of toolList) {
|
|
37
|
-
registerAgentToolOnServer(server, tool);
|
|
38
|
-
}
|
|
39
|
-
}
|
|
40
|
-
if (expose === 'agent' || expose === 'both') {
|
|
41
|
-
registerAgentPromptToolOnServer(server, AgentClass, opts.agentToolName ?? AgentClass.name);
|
|
42
|
-
}
|
|
43
|
-
return server;
|
|
44
|
-
}
|
|
45
|
-
function registerAgentToolOnServer(server, tool) {
|
|
46
|
-
const name = tool.definition.name;
|
|
47
|
-
const description = tool.definition.description;
|
|
48
|
-
// Pass the zod schema as a single AnySchema (the SDK's `inputSchema` field
|
|
49
|
-
// accepts either a ZodRawShape record or a single zod schema). The callback
|
|
50
|
-
// then receives `input: unknown` which is what the agent tool expects too.
|
|
51
|
-
server.registerTool(name, { description, inputSchema: tool.definition.inputSchema }, async (input) => {
|
|
52
|
-
const result = await runAgentTool(tool, input);
|
|
53
|
-
return { content: [{ type: 'text', text: stringifyResult(result) }] };
|
|
54
|
-
});
|
|
55
|
-
}
|
|
56
|
-
function registerAgentPromptToolOnServer(server, AgentClass, toolName) {
|
|
57
|
-
const promptShape = { prompt: z.string().describe('User message for the agent') };
|
|
58
|
-
server.registerTool(toolName, {
|
|
59
|
-
description: `Run the ${AgentClass.name} agent with a prompt and return its response`,
|
|
60
|
-
inputSchema: promptShape,
|
|
61
|
-
}, async (input) => {
|
|
62
|
-
const args = (input ?? {});
|
|
63
|
-
if (typeof args.prompt !== 'string') {
|
|
64
|
-
return { content: [{ type: 'text', text: '[error] Agent prompt-tool requires { prompt: string }' }] };
|
|
65
|
-
}
|
|
66
|
-
const agent = new AgentClass();
|
|
67
|
-
const response = await agent.prompt(args.prompt);
|
|
68
|
-
return { content: [{ type: 'text', text: response.text ?? '' }] };
|
|
69
|
-
});
|
|
70
|
-
}
|
|
71
|
-
async function runAgentTool(tool, input) {
|
|
72
|
-
if (!tool.execute) {
|
|
73
|
-
throw new Error(`mcpServerFromAgent: tool "${tool.definition.name}" has no execute fn (client-only tool — cannot be exposed via MCP)`);
|
|
74
|
-
}
|
|
75
|
-
const out = tool.execute(input);
|
|
76
|
-
if (out instanceof Promise)
|
|
77
|
-
return await out;
|
|
78
|
-
// Generator path — drain progress yields silently and return the final value.
|
|
79
|
-
// (MCP forwards progress via `notifications/progress`; the agent loop's
|
|
80
|
-
// tool-update chunks don't map cleanly without a progressToken from the
|
|
81
|
-
// caller, so we drop them in v1. Future enhancement: forward progress when
|
|
82
|
-
// the calling MCP client supplied a progressToken.)
|
|
83
|
-
const iter = out;
|
|
84
|
-
let next = await iter.next();
|
|
85
|
-
while (!next.done)
|
|
86
|
-
next = await iter.next();
|
|
87
|
-
return next.value;
|
|
88
|
-
}
|
|
89
|
-
function stringifyResult(result) {
|
|
90
|
-
if (typeof result === 'string')
|
|
91
|
-
return result;
|
|
92
|
-
if (result === undefined || result === null)
|
|
93
|
-
return '';
|
|
94
|
-
if (typeof result === 'object') {
|
|
95
|
-
try {
|
|
96
|
-
return JSON.stringify(result, null, 2);
|
|
97
|
-
}
|
|
98
|
-
catch {
|
|
99
|
-
return String(result);
|
|
100
|
-
}
|
|
101
|
-
}
|
|
102
|
-
return String(result);
|
|
103
|
-
}
|
|
104
|
-
function safeInstructions(agent) {
|
|
105
|
-
try {
|
|
106
|
-
const out = agent.instructions();
|
|
107
|
-
return typeof out === 'string' && out.length > 0 ? out : undefined;
|
|
108
|
-
}
|
|
109
|
-
catch {
|
|
110
|
-
return undefined;
|
|
111
|
-
}
|
|
112
|
-
}
|
|
113
|
-
//# sourceMappingURL=server-from-agent.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"server-from-agent.js","sourceRoot":"","sources":["../../src/mcp/server-from-agent.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAKvB;;;;;;;;;;;;;;;;;;;GAmBG;AACH,MAAM,CAAC,KAAK,UAAU,kBAAkB,CACtC,UAA2B,EAC3B,OAAkC,EAAE;IAEpC,MAAM,MAAM,GAAG,IAAI,CAAC,MAAM,IAAI,OAAO,CAAA;IAErC,MAAM,EAAE,SAAS,EAAE,GAAG,MAAM,MAAM,CAAC,yCAAyC,CAE3E,CAAA;IAED,MAAM,KAAK,GAAG,IAAI,UAAU,EAAE,CAAA;IAC9B,MAAM,YAAY,GAAG,IAAI,CAAC,YAAY,IAAI,gBAAgB,CAAC,K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GAAG,CAAC,CAAC,CAAC,CAAC,GAAG,CAAC,CAAC,CAAC,SAAS,CAAA;IACpE,CAAC;IAAC,MAAM,CAAC;QAAC,OAAO,SAAS,CAAA;IAAC,CAAC;AAC9B,CAAC"}
|
package/dist/mcp/types.d.ts
DELETED
|
@@ -1,64 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Public types for `@rudderjs/ai/mcp`. Kept in a separate module so the
|
|
3
|
-
* client + server connectors can share them without circular imports.
|
|
4
|
-
*/
|
|
5
|
-
/**
|
|
6
|
-
* Configuration for spawning a local MCP server as a stdio subprocess.
|
|
7
|
-
* Mirrors `StdioServerParameters` from `@modelcontextprotocol/sdk` but is
|
|
8
|
-
* re-exported here so consumers don't need a direct SDK import for the
|
|
9
|
-
* common case.
|
|
10
|
-
*/
|
|
11
|
-
export interface StdioServerSpawn {
|
|
12
|
-
command: string;
|
|
13
|
-
args?: readonly string[];
|
|
14
|
-
/** Inherited from the parent process when omitted. */
|
|
15
|
-
env?: Readonly<Record<string, string>>;
|
|
16
|
-
/** Working directory for the spawned process. */
|
|
17
|
-
cwd?: string;
|
|
18
|
-
}
|
|
19
|
-
/**
|
|
20
|
-
* Anything `mcpClientTools()` accepts as the connection target.
|
|
21
|
-
*
|
|
22
|
-
* - `string` / `URL` — connects via the Streamable HTTP transport
|
|
23
|
-
* - `StdioServerSpawn` — spawns a subprocess and connects over stdio
|
|
24
|
-
* - existing `Client` instance — used as-is, lifecycle remains the caller's
|
|
25
|
-
*
|
|
26
|
-
* Implementation note: keeping `Client` as `unknown` here so the type union
|
|
27
|
-
* doesn't force a hard dep on `@modelcontextprotocol/sdk` at module load.
|
|
28
|
-
* The runtime code uses an instanceof check via dynamic import.
|
|
29
|
-
*/
|
|
30
|
-
export type McpClientTransport = string | URL | StdioServerSpawn | object;
|
|
31
|
-
export interface McpClientToolsOptions {
|
|
32
|
-
/** Filter exposed tools — return `false` to drop a remote tool from the result. Defaults to all. */
|
|
33
|
-
filter?: (toolName: string) => boolean;
|
|
34
|
-
/** Prefix tool names to avoid collisions when wiring multiple remote servers. */
|
|
35
|
-
namePrefix?: string;
|
|
36
|
-
/**
|
|
37
|
-
* Forward MCP `notifications/progress` from the remote server as `tool-update`
|
|
38
|
-
* chunks during agent execution. Defaults to `true`.
|
|
39
|
-
*/
|
|
40
|
-
streaming?: boolean;
|
|
41
|
-
}
|
|
42
|
-
export interface McpServerFromAgentOptions {
|
|
43
|
-
/** Server name advertised over MCP. Default: `${AgentClass.name}Server`. */
|
|
44
|
-
name?: string;
|
|
45
|
-
/** Server version. Default: `'1.0.0'`. */
|
|
46
|
-
version?: string;
|
|
47
|
-
/**
|
|
48
|
-
* Server instructions advertised over MCP — typically the agent's own
|
|
49
|
-
* system prompt. Default: tries `agent.instructions()`, falls back to undefined.
|
|
50
|
-
*/
|
|
51
|
-
instructions?: string;
|
|
52
|
-
/**
|
|
53
|
-
* What to expose:
|
|
54
|
-
* - `'tools'` (default): one MCP tool per `agent.tools()` entry — external
|
|
55
|
-
* MCP clients call them as if they were the server's own.
|
|
56
|
-
* - `'agent'`: one MCP tool `prompt(text: string)` that runs the whole agent
|
|
57
|
-
* and returns the response text. Ship one agent, callable from any MCP client.
|
|
58
|
-
* - `'both'`: both of the above.
|
|
59
|
-
*/
|
|
60
|
-
expose?: 'tools' | 'agent' | 'both';
|
|
61
|
-
/** Name of the synthetic prompt-tool when `expose: 'agent' | 'both'`. Default: agent class name. */
|
|
62
|
-
agentToolName?: string;
|
|
63
|
-
}
|
|
64
|
-
//# sourceMappingURL=types.d.ts.map
|
package/dist/mcp/types.d.ts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../../src/mcp/types.ts"],"names":[],"mappings":"AAAA;;;GAGG;AAEH;;;;;GAKG;AACH,MAAM,WAAW,gBAAgB;IAC/B,OAAO,EAAE,MAAM,CAAA;IACf,IAAI,CAAC,EAAI,SAAS,MAAM,EAAE,CAAA;IAC1B,sDAAsD;IACtD,GAAG,CAAC,EAAK,QAAQ,CAAC,MAAM,CAAC,MAAM,EAAE,MAAM,CAAC,CAAC,CAAA;IACzC,iDAAiD;IACjD,GAAG,CAAC,EAAK,MAAM,CAAA;CAChB;AAED;;;;;;;;;;GAUG;AACH,MAAM,MAAM,kBAAkB,GAC1B,MAAM,GACN,GAAG,GACH,gBAAgB,GAChB,MAAM,CAAA;AAEV,MAAM,WAAW,qBAAqB;IACpC,oGAAoG;IACpG,MAAM,CAAC,EAAE,CAAC,QAAQ,EAAE,MAAM,KAAK,OAAO,CAAA;IACtC,iFAAiF;IACjF,UAAU,CAAC,EAAE,MAAM,CAAA;IACnB;;;OAGG;IACH,SAAS,CAAC,EAAE,OAAO,CAAA;CACpB;AAED,MAAM,WAAW,yBAAyB;IACxC,4EAA4E;IAC5E,IAAI,CAAC,EAAE,MAAM,CAAA;IACb,0CAA0C;IAC1C,OAAO,CAAC,EAAE,MAAM,CAAA;IAChB;;;OAGG;IACH,YAAY,CAAC,EAAE,MAAM,CAAA;IACrB;;;;;;;OAOG;IACH,MAAM,CAAC,EAAE,OAAO,GAAG,OAAO,GAAG,MAAM,CAAA;IACnC,oGAAoG;IACpG,aAAa,CAAC,EAAE,MAAM,CAAA;CACvB"}
|
package/dist/mcp/types.js
DELETED
package/dist/mcp/types.js.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"types.js","sourceRoot":"","sources":["../../src/mcp/types.ts"],"names":[],"mappings":"AAAA;;;GAGG"}
|
package/dist/memory-extract.d.ts
DELETED
|
@@ -1,60 +0,0 @@
|
|
|
1
|
-
import type { AiMiddleware, MemoryEntry, RemembersSpec } from './types.js';
|
|
2
|
-
import type { UserMemoryLookup } from './memory.js';
|
|
3
|
-
export interface MemoryExtractOptions {
|
|
4
|
-
/**
|
|
5
|
-
* Override the {@link UserMemory} lookup. Defaults to the module-level
|
|
6
|
-
* `resolveUserMemory()` registry that `AiProvider` writes to from
|
|
7
|
-
* `AiConfig.memory`. Tests pass a closure to inject a fake.
|
|
8
|
-
*/
|
|
9
|
-
lookup?: UserMemoryLookup;
|
|
10
|
-
/**
|
|
11
|
-
* Confidence floor for the small model's self-rated `score`. Facts
|
|
12
|
-
* with a score < threshold are dropped before any `remember()` call.
|
|
13
|
-
* Default `0.7`.
|
|
14
|
-
*
|
|
15
|
-
* **Tuning note (poisoning mitigation):** the threshold is the v1
|
|
16
|
-
* defense against a malicious user planting adversarial "facts." A
|
|
17
|
-
* low threshold accepts more spam; a high threshold filters useful
|
|
18
|
-
* signal. Pair with `MemoryExtractOptions.onExtracted` for an audit
|
|
19
|
-
* log when you ship extract to production.
|
|
20
|
-
*/
|
|
21
|
-
threshold?: number;
|
|
22
|
-
/**
|
|
23
|
-
* Called after a successful extract with the entries that survived
|
|
24
|
-
* the threshold filter and were written to the store. Use this to
|
|
25
|
-
* stream entries into telescope, an audit log, or test assertions.
|
|
26
|
-
*/
|
|
27
|
-
onExtracted?: (entries: MemoryEntry[]) => void;
|
|
28
|
-
/**
|
|
29
|
-
* Called when extract fails — small-model network error, JSON parse
|
|
30
|
-
* failure, schema-validation rejection, or `mem.remember()` throw.
|
|
31
|
-
* Errors are otherwise swallowed; the parent run never breaks.
|
|
32
|
-
*/
|
|
33
|
-
onError?: (err: unknown) => void;
|
|
34
|
-
}
|
|
35
|
-
/**
|
|
36
|
-
* Post-conversation {@link AiMiddleware} that asks a small model to
|
|
37
|
-
* distill the latest `[user, assistant]` turn into durable facts and
|
|
38
|
-
* writes the survivors (above `threshold`) to the registered
|
|
39
|
-
* {@link UserMemory}. Auto-installed by `Agent.prompt` /
|
|
40
|
-
* `Agent.stream` when `Agent.remembers().extract === 'auto'` and
|
|
41
|
-
* `extractWith` is set; can also be dropped into `Agent.middleware()`
|
|
42
|
-
* manually.
|
|
43
|
-
*
|
|
44
|
-
* Runs in `onFinish` — only fires on a successful loop, so failed
|
|
45
|
-
* runs don't pollute memory. Failures inside the extract path
|
|
46
|
-
* (network, JSON parse, zod validation, `remember()` throw) are
|
|
47
|
-
* routed through `MemoryExtractOptions.onError` and otherwise
|
|
48
|
-
* swallowed; the parent prompt never breaks because of memory work.
|
|
49
|
-
*
|
|
50
|
-
* **Auto-installed extracts skip continuation calls** (`options.messages`
|
|
51
|
-
* set) at the host level — the same way auto-inject does. Manually
|
|
52
|
-
* installed extracts always run.
|
|
53
|
-
*
|
|
54
|
-
* **Pitfall — memory poisoning:** auto-extraction lets a malicious
|
|
55
|
-
* user plant adversarial "facts." The threshold (default 0.7) is the
|
|
56
|
-
* baseline defense; pair with `onExtracted` for an audit log when you
|
|
57
|
-
* ship to production. A content-filter middleware is a follow-up.
|
|
58
|
-
*/
|
|
59
|
-
export declare function withMemoryExtract(spec: RemembersSpec, opts?: MemoryExtractOptions): AiMiddleware;
|
|
60
|
-
//# sourceMappingURL=memory-extract.d.ts.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memory-extract.d.ts","sourceRoot":"","sources":["../src/memory-extract.ts"],"names":[],"mappings":"AAIA,OAAO,KAAK,EAEV,YAAY,EAEZ,WAAW,EACX,aAAa,EACd,MAAM,YAAY,CAAA;AACnB,OAAO,KAAK,EAAE,gBAAgB,EAAE,MAAM,aAAa,CAAA;AAEnD,MAAM,WAAW,oBAAoB;IACnC;;;;OAIG;IACH,MAAM,CAAC,EAAO,gBAAgB,CAAA;IAC9B;;;;;;;;;;OAUG;IACH,SAAS,CAAC,EAAI,MAAM,CAAA;IACpB;;;;OAIG;IACH,WAAW,CAAC,EAAE,CAAC,OAAO,EAAE,WAAW,EAAE,KAAK,IAAI,CAAA;IAC9C;;;;OAIG;IACH,OAAO,CAAC,EAAM,CAAC,GAAG,EAAE,OAAO,KAAK,IAAI,CAAA;CACrC;AAuBD;;;;;;;;;;;;;;;;;;;;;;;GAuBG;AACH,wBAAgB,iBAAiB,CAC/B,IAAI,EAAE,aAAa,EACnB,IAAI,GAAE,oBAAyB,GAC9B,YAAY,CAwDd"}
|
package/dist/memory-extract.js
DELETED
|
@@ -1,163 +0,0 @@
|
|
|
1
|
-
import { z } from 'zod';
|
|
2
|
-
import { agent, resolveUserMemory } from './agent.js';
|
|
3
|
-
import { Output } from './output.js';
|
|
4
|
-
/**
|
|
5
|
-
* The shape we ask the small model to fill in when distilling facts.
|
|
6
|
-
* `score` is the model's self-rated confidence in [0, 1]; tags are
|
|
7
|
-
* additive — the spec's `tags` are unioned in regardless.
|
|
8
|
-
*/
|
|
9
|
-
const ExtractedFactsSchema = z.object({
|
|
10
|
-
facts: z.array(z.object({
|
|
11
|
-
fact: z.string().min(1),
|
|
12
|
-
score: z.number().min(0).max(1),
|
|
13
|
-
tags: z.array(z.string()).optional(),
|
|
14
|
-
})),
|
|
15
|
-
});
|
|
16
|
-
const EXTRACT_INSTRUCTIONS = [
|
|
17
|
-
'You distill durable facts about a USER from a single conversation turn.',
|
|
18
|
-
'A "durable fact" is something true about the user that future conversations would benefit from knowing — preferences, identifying details, ongoing projects, persistent constraints.',
|
|
19
|
-
'Skip anything specific to this conversation, ephemeral state, or already-obvious context.',
|
|
20
|
-
'Self-rate each fact\'s confidence in [0, 1]; the host filters out anything below the threshold.',
|
|
21
|
-
'If nothing is worth remembering, return {"facts": []}.',
|
|
22
|
-
].join(' ');
|
|
23
|
-
/**
|
|
24
|
-
* Post-conversation {@link AiMiddleware} that asks a small model to
|
|
25
|
-
* distill the latest `[user, assistant]` turn into durable facts and
|
|
26
|
-
* writes the survivors (above `threshold`) to the registered
|
|
27
|
-
* {@link UserMemory}. Auto-installed by `Agent.prompt` /
|
|
28
|
-
* `Agent.stream` when `Agent.remembers().extract === 'auto'` and
|
|
29
|
-
* `extractWith` is set; can also be dropped into `Agent.middleware()`
|
|
30
|
-
* manually.
|
|
31
|
-
*
|
|
32
|
-
* Runs in `onFinish` — only fires on a successful loop, so failed
|
|
33
|
-
* runs don't pollute memory. Failures inside the extract path
|
|
34
|
-
* (network, JSON parse, zod validation, `remember()` throw) are
|
|
35
|
-
* routed through `MemoryExtractOptions.onError` and otherwise
|
|
36
|
-
* swallowed; the parent prompt never breaks because of memory work.
|
|
37
|
-
*
|
|
38
|
-
* **Auto-installed extracts skip continuation calls** (`options.messages`
|
|
39
|
-
* set) at the host level — the same way auto-inject does. Manually
|
|
40
|
-
* installed extracts always run.
|
|
41
|
-
*
|
|
42
|
-
* **Pitfall — memory poisoning:** auto-extraction lets a malicious
|
|
43
|
-
* user plant adversarial "facts." The threshold (default 0.7) is the
|
|
44
|
-
* baseline defense; pair with `onExtracted` for an audit log when you
|
|
45
|
-
* ship to production. A content-filter middleware is a follow-up.
|
|
46
|
-
*/
|
|
47
|
-
export function withMemoryExtract(spec, opts = {}) {
|
|
48
|
-
const lookup = opts.lookup ?? resolveUserMemory;
|
|
49
|
-
const threshold = opts.threshold ?? 0.7;
|
|
50
|
-
const wrapper = Output.object({ schema: ExtractedFactsSchema });
|
|
51
|
-
return {
|
|
52
|
-
name: 'memory-extract',
|
|
53
|
-
async onFinish(ctx) {
|
|
54
|
-
try {
|
|
55
|
-
if (spec.extract !== 'auto')
|
|
56
|
-
return;
|
|
57
|
-
if (!spec.extractWith)
|
|
58
|
-
return;
|
|
59
|
-
const mem = lookup();
|
|
60
|
-
if (!mem)
|
|
61
|
-
return;
|
|
62
|
-
const turn = extractLatestTurn(ctx.messages);
|
|
63
|
-
if (!turn)
|
|
64
|
-
return;
|
|
65
|
-
const extractor = agent({
|
|
66
|
-
instructions: `${EXTRACT_INSTRUCTIONS}\n\n${wrapper.toSystemPrompt()}`,
|
|
67
|
-
model: spec.extractWith,
|
|
68
|
-
});
|
|
69
|
-
const prompt = [
|
|
70
|
-
`User said: ${JSON.stringify(turn.user)}`,
|
|
71
|
-
`Assistant replied: ${JSON.stringify(turn.assistant)}`,
|
|
72
|
-
'',
|
|
73
|
-
'Extract durable facts about the USER from the above. Return strictly valid JSON.',
|
|
74
|
-
].join('\n');
|
|
75
|
-
const response = await extractor.prompt(prompt);
|
|
76
|
-
const parsed = wrapper.parse(response.text);
|
|
77
|
-
const surviving = parsed.facts.filter(f => f.score >= threshold);
|
|
78
|
-
if (surviving.length === 0) {
|
|
79
|
-
opts.onExtracted?.([]);
|
|
80
|
-
return;
|
|
81
|
-
}
|
|
82
|
-
const tagsFromSpec = spec.tags ?? [];
|
|
83
|
-
const written = [];
|
|
84
|
-
for (const f of surviving) {
|
|
85
|
-
const merged = mergeTags(f.tags, tagsFromSpec);
|
|
86
|
-
const entry = await mem.remember(spec.user, f.fact, {
|
|
87
|
-
score: f.score,
|
|
88
|
-
...(merged.length > 0 ? { tags: merged } : {}),
|
|
89
|
-
});
|
|
90
|
-
written.push(entry);
|
|
91
|
-
}
|
|
92
|
-
opts.onExtracted?.(written);
|
|
93
|
-
}
|
|
94
|
-
catch (err) {
|
|
95
|
-
opts.onError?.(err);
|
|
96
|
-
// Never break the parent run.
|
|
97
|
-
}
|
|
98
|
-
},
|
|
99
|
-
};
|
|
100
|
-
}
|
|
101
|
-
// ─── Helpers ──────────────────────────────────────────────
|
|
102
|
-
/**
|
|
103
|
-
* Walk `messages` from the end and return the most recent
|
|
104
|
-
* `(user, assistant)` pair where the assistant message follows the
|
|
105
|
-
* user message. Skips trailing `tool` messages so multi-step tool
|
|
106
|
-
* loops still surface the original user request and the model's
|
|
107
|
-
* final synthesis.
|
|
108
|
-
*
|
|
109
|
-
* Returns `null` when:
|
|
110
|
-
* - the loop didn't reach a final assistant message (stopped on a
|
|
111
|
-
* client-tool pause, approval gate, or handoff), or
|
|
112
|
-
* - the assistant message has no extractable text content.
|
|
113
|
-
*/
|
|
114
|
-
function extractLatestTurn(messages) {
|
|
115
|
-
let assistantText = null;
|
|
116
|
-
let lastAssistantIdx = -1;
|
|
117
|
-
// Walk backwards looking for the LAST assistant message that's a
|
|
118
|
-
// text reply (not a tool-calls message).
|
|
119
|
-
for (let i = messages.length - 1; i >= 0; i--) {
|
|
120
|
-
const m = messages[i];
|
|
121
|
-
if (!m || m.role !== 'assistant')
|
|
122
|
-
continue;
|
|
123
|
-
if (m.toolCalls && m.toolCalls.length > 0)
|
|
124
|
-
continue; // tool-call step, not a final reply
|
|
125
|
-
const text = contentToString(m.content);
|
|
126
|
-
if (text.length === 0)
|
|
127
|
-
continue;
|
|
128
|
-
assistantText = text;
|
|
129
|
-
lastAssistantIdx = i;
|
|
130
|
-
break;
|
|
131
|
-
}
|
|
132
|
-
if (assistantText === null || lastAssistantIdx === -1)
|
|
133
|
-
return null;
|
|
134
|
-
// Find the most recent user message before that assistant message.
|
|
135
|
-
for (let i = lastAssistantIdx - 1; i >= 0; i--) {
|
|
136
|
-
const m = messages[i];
|
|
137
|
-
if (!m || m.role !== 'user')
|
|
138
|
-
continue;
|
|
139
|
-
const text = contentToString(m.content);
|
|
140
|
-
if (text.length === 0)
|
|
141
|
-
continue;
|
|
142
|
-
return { user: text, assistant: assistantText };
|
|
143
|
-
}
|
|
144
|
-
return null;
|
|
145
|
-
}
|
|
146
|
-
function contentToString(content) {
|
|
147
|
-
if (typeof content === 'string')
|
|
148
|
-
return content;
|
|
149
|
-
const out = [];
|
|
150
|
-
for (const p of content) {
|
|
151
|
-
if (p.type === 'text' && typeof p.text === 'string')
|
|
152
|
-
out.push(p.text);
|
|
153
|
-
}
|
|
154
|
-
return out.join('\n');
|
|
155
|
-
}
|
|
156
|
-
function mergeTags(modelTags, specTags) {
|
|
157
|
-
if (!modelTags || modelTags.length === 0)
|
|
158
|
-
return [...specTags];
|
|
159
|
-
if (specTags.length === 0)
|
|
160
|
-
return [...modelTags];
|
|
161
|
-
return Array.from(new Set([...modelTags, ...specTags]));
|
|
162
|
-
}
|
|
163
|
-
//# sourceMappingURL=memory-extract.js.map
|
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"memory-extract.js","sourceRoot":"","sources":["../src/memory-extract.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,CAAC,EAAE,MAAM,KAAK,CAAA;AAEvB,OAAO,EAAE,KAAK,EAAE,iBAAiB,EAAE,M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