@n8n/agents 0.3.0 → 0.5.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/dist/build.tsbuildinfo +1 -1
- package/dist/codegen/generate-agent-code.d.ts +2 -0
- package/dist/codegen/generate-agent-code.js +197 -0
- package/dist/codegen/generate-agent-code.js.map +1 -0
- package/dist/index.d.ts +10 -1
- package/dist/index.js +12 -2
- package/dist/index.js.map +1 -1
- package/dist/runtime/agent-runtime.d.ts +4 -3
- package/dist/runtime/agent-runtime.js +28 -87
- package/dist/runtime/agent-runtime.js.map +1 -1
- package/dist/runtime/memory-store.d.ts +3 -3
- package/dist/runtime/memory-store.js +2 -4
- package/dist/runtime/memory-store.js.map +1 -1
- package/dist/runtime/message-list.d.ts +9 -4
- package/dist/runtime/message-list.js +51 -8
- package/dist/runtime/message-list.js.map +1 -1
- package/dist/runtime/messages.d.ts +3 -3
- package/dist/runtime/messages.js +1 -2
- package/dist/runtime/messages.js.map +1 -1
- package/dist/runtime/model-factory.js +17 -4
- package/dist/runtime/model-factory.js.map +1 -1
- package/dist/runtime/runtime-helpers.d.ts +5 -5
- package/dist/runtime/runtime-helpers.js +6 -7
- package/dist/runtime/runtime-helpers.js.map +1 -1
- package/dist/runtime/strip-orphaned-tool-messages.d.ts +2 -2
- package/dist/runtime/strip-orphaned-tool-messages.js.map +1 -1
- package/dist/runtime/title-generation.d.ts +4 -0
- package/dist/runtime/title-generation.js +52 -15
- package/dist/runtime/title-generation.js.map +1 -1
- package/dist/runtime/working-memory.d.ts +10 -17
- package/dist/runtime/working-memory.js +41 -110
- package/dist/runtime/working-memory.js.map +1 -1
- package/dist/sdk/agent.d.ts +12 -1
- package/dist/sdk/agent.js +201 -1
- package/dist/sdk/agent.js.map +1 -1
- package/dist/sdk/eval.js +6 -0
- package/dist/sdk/eval.js.map +1 -1
- package/dist/sdk/from-schema.d.ts +15 -0
- package/dist/sdk/from-schema.js +218 -0
- package/dist/sdk/from-schema.js.map +1 -0
- package/dist/sdk/mcp-client.d.ts +1 -0
- package/dist/sdk/mcp-client.js +3 -0
- package/dist/sdk/mcp-client.js.map +1 -1
- package/dist/sdk/memory.d.ts +2 -0
- package/dist/sdk/memory.js +10 -0
- package/dist/sdk/memory.js.map +1 -1
- package/dist/sdk/message.d.ts +2 -2
- package/dist/sdk/message.js +14 -5
- package/dist/sdk/message.js.map +1 -1
- package/dist/sdk/provider-capabilities.d.ts +10 -0
- package/dist/sdk/provider-capabilities.js +26 -0
- package/dist/sdk/provider-capabilities.js.map +1 -0
- package/dist/sdk/tool.js +2 -1
- package/dist/sdk/tool.js.map +1 -1
- package/dist/sdk/verify.js +0 -11
- package/dist/sdk/verify.js.map +1 -1
- package/dist/storage/postgres-memory.d.ts +2 -2
- package/dist/storage/postgres-memory.js +4 -5
- package/dist/storage/postgres-memory.js.map +1 -1
- package/dist/storage/sqlite-memory.d.ts +2 -2
- package/dist/storage/sqlite-memory.js +4 -5
- package/dist/storage/sqlite-memory.js.map +1 -1
- package/dist/types/index.d.ts +2 -0
- package/dist/types/sdk/agent-builder.d.ts +25 -0
- package/dist/types/sdk/agent-builder.js +3 -0
- package/dist/types/sdk/agent-builder.js.map +1 -0
- package/dist/types/sdk/agent.d.ts +1 -0
- package/dist/types/sdk/credential-provider.d.ts +13 -0
- package/dist/types/sdk/credential-provider.js +3 -0
- package/dist/types/sdk/credential-provider.js.map +1 -0
- package/dist/types/sdk/eval.d.ts +3 -0
- package/dist/types/sdk/handler-executor.d.ts +12 -0
- package/dist/types/sdk/handler-executor.js +3 -0
- package/dist/types/sdk/handler-executor.js.map +1 -0
- package/dist/types/sdk/memory.d.ts +4 -3
- package/dist/types/sdk/message.d.ts +1 -0
- package/dist/types/sdk/schema.d.ts +99 -0
- package/dist/types/sdk/schema.js +3 -0
- package/dist/types/sdk/schema.js.map +1 -0
- package/dist/types/sdk/tool.d.ts +1 -0
- package/dist/utils/zod.d.ts +1 -0
- package/dist/utils/zod.js +24 -1
- package/dist/utils/zod.js.map +1 -1
- package/package.json +1 -1
|
@@ -0,0 +1,197 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.generateAgentCode = generateAgentCode;
|
|
37
|
+
function escapeTemplateLiteral(str) {
|
|
38
|
+
return str.replace(/\\/g, '\\\\').replace(/`/g, '\\`').replace(/\$/g, '\\$');
|
|
39
|
+
}
|
|
40
|
+
function escapeSingleQuote(str) {
|
|
41
|
+
return JSON.stringify(str).slice(1, -1).replace(/'/g, "\\'");
|
|
42
|
+
}
|
|
43
|
+
let prettierInstance;
|
|
44
|
+
async function formatCode(code) {
|
|
45
|
+
prettierInstance ??= await Promise.resolve().then(() => __importStar(require('prettier')));
|
|
46
|
+
return await prettierInstance.format(code, {
|
|
47
|
+
parser: 'typescript',
|
|
48
|
+
singleQuote: true,
|
|
49
|
+
useTabs: true,
|
|
50
|
+
trailingComma: 'all',
|
|
51
|
+
printWidth: 100,
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
function assertAllHandled(_) {
|
|
55
|
+
}
|
|
56
|
+
function modelParts(model) {
|
|
57
|
+
if (model.provider && model.name) {
|
|
58
|
+
return [`.model('${escapeSingleQuote(model.provider)}', '${escapeSingleQuote(model.name)}')`];
|
|
59
|
+
}
|
|
60
|
+
if (model.name) {
|
|
61
|
+
return [`.model('${escapeSingleQuote(model.name)}')`];
|
|
62
|
+
}
|
|
63
|
+
return [];
|
|
64
|
+
}
|
|
65
|
+
function toolPart(tool) {
|
|
66
|
+
if (!tool.editable) {
|
|
67
|
+
return {
|
|
68
|
+
part: `.tool(new WorkflowTool('${escapeSingleQuote(tool.name)}'))`,
|
|
69
|
+
usesWorkflowTool: true,
|
|
70
|
+
};
|
|
71
|
+
}
|
|
72
|
+
const parts = [`new Tool('${escapeSingleQuote(tool.name)}')`];
|
|
73
|
+
parts.push(`.description('${escapeSingleQuote(tool.description)}')`);
|
|
74
|
+
if (tool.inputSchemaSource)
|
|
75
|
+
parts.push(`.input(${tool.inputSchemaSource})`);
|
|
76
|
+
if (tool.outputSchemaSource)
|
|
77
|
+
parts.push(`.output(${tool.outputSchemaSource})`);
|
|
78
|
+
if (tool.suspendSchemaSource)
|
|
79
|
+
parts.push(`.suspend(${tool.suspendSchemaSource})`);
|
|
80
|
+
if (tool.resumeSchemaSource)
|
|
81
|
+
parts.push(`.resume(${tool.resumeSchemaSource})`);
|
|
82
|
+
if (tool.handlerSource)
|
|
83
|
+
parts.push(`.handler(${tool.handlerSource})`);
|
|
84
|
+
if (tool.toMessageSource)
|
|
85
|
+
parts.push(`.toMessage(${tool.toMessageSource})`);
|
|
86
|
+
if (tool.requireApproval)
|
|
87
|
+
parts.push('.requireApproval()');
|
|
88
|
+
if (tool.needsApprovalFnSource)
|
|
89
|
+
parts.push(`.needsApprovalFn(${tool.needsApprovalFnSource})`);
|
|
90
|
+
return { part: `.tool(${parts.join('')})`, usesWorkflowTool: false };
|
|
91
|
+
}
|
|
92
|
+
function evalPart(ev) {
|
|
93
|
+
const parts = [`new Eval('${escapeSingleQuote(ev.name)}')`];
|
|
94
|
+
if (ev.description)
|
|
95
|
+
parts.push(`.description('${escapeSingleQuote(ev.description)}')`);
|
|
96
|
+
if (ev.modelId)
|
|
97
|
+
parts.push(`.model('${escapeSingleQuote(ev.modelId)}')`);
|
|
98
|
+
if (ev.credentialName)
|
|
99
|
+
parts.push(`.credential('${escapeSingleQuote(ev.credentialName)}')`);
|
|
100
|
+
if (ev.handlerSource) {
|
|
101
|
+
parts.push(ev.type === 'check' ? `.check(${ev.handlerSource})` : `.judge(${ev.handlerSource})`);
|
|
102
|
+
}
|
|
103
|
+
return `.eval(${parts.join('')})`;
|
|
104
|
+
}
|
|
105
|
+
function guardrailPart(g) {
|
|
106
|
+
const method = g.position === 'input' ? 'inputGuardrail' : 'outputGuardrail';
|
|
107
|
+
return `.${method}(${g.source})`;
|
|
108
|
+
}
|
|
109
|
+
function memoryPart(memory) {
|
|
110
|
+
if (memory.source) {
|
|
111
|
+
return `.memory(${memory.source})`;
|
|
112
|
+
}
|
|
113
|
+
return `.memory(new Memory().lastMessages(${memory.lastMessages ?? 10}))`;
|
|
114
|
+
}
|
|
115
|
+
function thinkingPart(thinking) {
|
|
116
|
+
const props = [];
|
|
117
|
+
if (thinking.budgetTokens !== undefined)
|
|
118
|
+
props.push(`budgetTokens: ${thinking.budgetTokens}`);
|
|
119
|
+
if (thinking.reasoningEffort)
|
|
120
|
+
props.push(`reasoningEffort: '${thinking.reasoningEffort}'`);
|
|
121
|
+
if (props.length > 0) {
|
|
122
|
+
return `.thinking('${thinking.provider}', { ${props.join(', ')} })`;
|
|
123
|
+
}
|
|
124
|
+
return `.thinking('${thinking.provider}')`;
|
|
125
|
+
}
|
|
126
|
+
function buildImports(schema, needsWorkflowTool) {
|
|
127
|
+
const agentImports = new Set(['Agent']);
|
|
128
|
+
if (schema.tools.some((t) => t.editable))
|
|
129
|
+
agentImports.add('Tool');
|
|
130
|
+
if (needsWorkflowTool)
|
|
131
|
+
agentImports.add('WorkflowTool');
|
|
132
|
+
if (schema.memory)
|
|
133
|
+
agentImports.add('Memory');
|
|
134
|
+
if (schema.mcp && schema.mcp.length > 0)
|
|
135
|
+
agentImports.add('McpClient');
|
|
136
|
+
if (schema.evaluations.length > 0)
|
|
137
|
+
agentImports.add('Eval');
|
|
138
|
+
const toolsNeedZod = schema.tools.some((t) => (t.inputSchemaSource?.includes('z.') ?? false) ||
|
|
139
|
+
(t.outputSchemaSource?.includes('z.') ?? false));
|
|
140
|
+
const structuredOutputNeedsZod = schema.config.structuredOutput.schemaSource?.includes('z.') ?? false;
|
|
141
|
+
let imports = `import { ${Array.from(agentImports).sort().join(', ')} } from '@n8n/agents';`;
|
|
142
|
+
if (toolsNeedZod || structuredOutputNeedsZod)
|
|
143
|
+
imports += "\nimport { z } from 'zod';";
|
|
144
|
+
return imports;
|
|
145
|
+
}
|
|
146
|
+
async function generateAgentCode(schema, agentName) {
|
|
147
|
+
const { model, credential, instructions, description: _description, tools, providerTools, memory, evaluations, guardrails, mcp, telemetry, checkpoint, config, ...rest } = schema;
|
|
148
|
+
assertAllHandled(rest);
|
|
149
|
+
const { thinking, toolCallConcurrency, requireToolApproval, structuredOutput, ...configRest } = config;
|
|
150
|
+
assertAllHandled(configRest);
|
|
151
|
+
const parts = [];
|
|
152
|
+
let needsWorkflowTool = false;
|
|
153
|
+
parts.push(`export default new Agent('${escapeSingleQuote(agentName)}')`);
|
|
154
|
+
parts.push(...modelParts(model));
|
|
155
|
+
if (credential)
|
|
156
|
+
parts.push(`.credential('${escapeSingleQuote(credential)}')`);
|
|
157
|
+
if (instructions)
|
|
158
|
+
parts.push(`.instructions(\`${escapeTemplateLiteral(instructions)}\`)`);
|
|
159
|
+
for (const tool of tools) {
|
|
160
|
+
const { part, usesWorkflowTool } = toolPart(tool);
|
|
161
|
+
if (usesWorkflowTool)
|
|
162
|
+
needsWorkflowTool = true;
|
|
163
|
+
parts.push(part);
|
|
164
|
+
}
|
|
165
|
+
for (const pt of providerTools) {
|
|
166
|
+
parts.push(`.providerTool(${pt.source})`);
|
|
167
|
+
}
|
|
168
|
+
if (memory)
|
|
169
|
+
parts.push(memoryPart(memory));
|
|
170
|
+
for (const ev of evaluations) {
|
|
171
|
+
parts.push(evalPart(ev));
|
|
172
|
+
}
|
|
173
|
+
for (const g of guardrails) {
|
|
174
|
+
parts.push(guardrailPart(g));
|
|
175
|
+
}
|
|
176
|
+
if (mcp && mcp.length > 0) {
|
|
177
|
+
const configs = mcp.map((s) => s.configSource).join(', ');
|
|
178
|
+
parts.push(`.mcp(new McpClient([${configs}]))`);
|
|
179
|
+
}
|
|
180
|
+
if (telemetry)
|
|
181
|
+
parts.push(`.telemetry(${telemetry.source})`);
|
|
182
|
+
if (checkpoint)
|
|
183
|
+
parts.push(`.checkpoint('${escapeSingleQuote(checkpoint)}')`);
|
|
184
|
+
if (thinking)
|
|
185
|
+
parts.push(thinkingPart(thinking));
|
|
186
|
+
if (toolCallConcurrency)
|
|
187
|
+
parts.push(`.toolCallConcurrency(${toolCallConcurrency})`);
|
|
188
|
+
if (requireToolApproval)
|
|
189
|
+
parts.push('.requireToolApproval()');
|
|
190
|
+
if (structuredOutput.enabled && structuredOutput.schemaSource) {
|
|
191
|
+
parts.push(`.structuredOutput(${structuredOutput.schemaSource})`);
|
|
192
|
+
}
|
|
193
|
+
const imports = buildImports(schema, needsWorkflowTool);
|
|
194
|
+
const raw = `${imports}\n\n${parts.join('')};\n`;
|
|
195
|
+
return await formatCode(raw);
|
|
196
|
+
}
|
|
197
|
+
//# sourceMappingURL=generate-agent-code.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"generate-agent-code.js","sourceRoot":"","sources":["../../src/codegen/generate-agent-code.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA4IA,8CA4EC;AA1MD,SAAS,qBAAqB,CAAC,GAAW;IACzC,OAAO,GAAG,CAAC,OAAO,CAAC,KAAK,EAAE,MAAM,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC,OAAO,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;AAC9E,CAAC;AAED,SAAS,iBAAiB,CAAC,GAAW;IACrC,OAAO,IAAI,CAAC,SAAS,CAAC,GAAG,CAAC,CAAC,KAAK,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC,CAAC,OAAO,CAAC,IAAI,EAAE,KAAK,CAAC,CAAC;AAC9D,CAAC;AAED,IAAI,gBAA6C,CAAC;AAMlD,KAAK,UAAU,UAAU,CAAC,IAAY;IACrC,gBAAgB,KAAK,wDAAa,UAAU,GAAC,CAAC;IAC9C,OAAO,MAAM,gBAAgB,CAAC,MAAM,CAAC,IAAI,EAAE;QAC1C,MAAM,EAAE,YAAY;QACpB,WAAW,EAAE,IAAI;QACjB,OAAO,EAAE,IAAI;QACb,aAAa,EAAE,KAAK;QACpB,UAAU,EAAE,GAAG;KACf,CAAC,CAAC;AACJ,CAAC;AAOD,SAAS,gBAAgB,CAAC,CAAwB;AAElD,CAAC;AAMD,SAAS,UAAU,CAAC,KAA2B;IAC9C,IAAI,KAAK,CAAC,QAAQ,IAAI,KAAK,CA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,CAAC"}
|
package/dist/index.d.ts
CHANGED
|
@@ -13,19 +13,28 @@ export { Telemetry } from './sdk/telemetry';
|
|
|
13
13
|
export { LangSmithTelemetry } from './integrations/langsmith';
|
|
14
14
|
export type { LangSmithTelemetryConfig } from './integrations/langsmith';
|
|
15
15
|
export { Agent } from './sdk/agent';
|
|
16
|
+
export type { AgentBuilder, CredentialProvider, ResolvedCredential, CredentialListItem, } from './types';
|
|
16
17
|
export { McpClient } from './sdk/mcp-client';
|
|
17
18
|
export { Network } from './sdk/network';
|
|
18
19
|
export { providerTools } from './sdk/provider-tools';
|
|
19
20
|
export { verify } from './sdk/verify';
|
|
20
21
|
export type { VerifyResult } from './sdk/verify';
|
|
21
22
|
export type { ContentCitation, ContentFile, ContentMetadata, ContentReasoning, ContentText, ContentToolCall, ContentToolResult, Message, MessageContent, MessageRole, AgentMessage, CustomAgentMessages, AgentDbMessage, } from './types/sdk/message';
|
|
22
|
-
export {
|
|
23
|
+
export type { HandlerExecutor } from './types/sdk/handler-executor';
|
|
24
|
+
export type { AgentSchema, ToolSchema, MemorySchema, EvalSchema, ThinkingSchema, ProviderToolSchema, GuardrailSchema, McpServerSchema, TelemetrySchema, } from './types/sdk/schema';
|
|
25
|
+
export { generateAgentCode } from './codegen/generate-agent-code';
|
|
26
|
+
export { filterLlmMessages, isLlmMessage } from './sdk/message';
|
|
23
27
|
export { fetchProviderCatalog } from './sdk/catalog';
|
|
28
|
+
export { providerCapabilities } from './sdk/provider-capabilities';
|
|
29
|
+
export type { ProviderCapability } from './sdk/provider-capabilities';
|
|
24
30
|
export type { ProviderCatalog, ProviderInfo, ModelInfo, ModelCost, ModelLimits, } from './sdk/catalog';
|
|
25
31
|
export { SqliteMemory } from './storage/sqlite-memory';
|
|
32
|
+
export { UPDATE_WORKING_MEMORY_TOOL_NAME, WORKING_MEMORY_DEFAULT_INSTRUCTION, } from './runtime/working-memory';
|
|
26
33
|
export type { SqliteMemoryConfig } from './storage/sqlite-memory';
|
|
27
34
|
export { PostgresMemory } from './storage/postgres-memory';
|
|
28
35
|
export type { PostgresMemoryConfig } from './storage/postgres-memory';
|
|
36
|
+
export { createModel } from './runtime/model-factory';
|
|
37
|
+
export { generateTitleFromMessage } from './runtime/title-generation';
|
|
29
38
|
export { Workspace } from './workspace';
|
|
30
39
|
export { BaseFilesystem } from './workspace';
|
|
31
40
|
export { BaseSandbox } from './workspace';
|
package/dist/index.js
CHANGED
|
@@ -33,7 +33,7 @@ var __importStar = (this && this.__importStar) || (function () {
|
|
|
33
33
|
};
|
|
34
34
|
})();
|
|
35
35
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
-
exports.ProcessHandle = exports.SandboxProcessManager = exports.createWorkspaceTools = exports.BaseSandbox = exports.BaseFilesystem = exports.Workspace = exports.PostgresMemory = exports.SqliteMemory = exports.fetchProviderCatalog = exports.isLlmMessage = exports.filterLlmMessages = exports.
|
|
36
|
+
exports.ProcessHandle = exports.SandboxProcessManager = exports.createWorkspaceTools = exports.BaseSandbox = exports.BaseFilesystem = exports.Workspace = exports.generateTitleFromMessage = exports.createModel = exports.PostgresMemory = exports.WORKING_MEMORY_DEFAULT_INSTRUCTION = exports.UPDATE_WORKING_MEMORY_TOOL_NAME = exports.SqliteMemory = exports.providerCapabilities = exports.fetchProviderCatalog = exports.isLlmMessage = exports.filterLlmMessages = exports.generateAgentCode = exports.verify = exports.providerTools = exports.Network = exports.McpClient = exports.Agent = exports.LangSmithTelemetry = exports.Telemetry = exports.evals = exports.evaluate = exports.Eval = exports.Guardrail = exports.Memory = exports.Tool = exports.AgentEvent = void 0;
|
|
37
37
|
var types_1 = require("./types");
|
|
38
38
|
Object.defineProperty(exports, "AgentEvent", { enumerable: true, get: function () { return types_1.AgentEvent; } });
|
|
39
39
|
var tool_1 = require("./sdk/tool");
|
|
@@ -61,16 +61,26 @@ var provider_tools_1 = require("./sdk/provider-tools");
|
|
|
61
61
|
Object.defineProperty(exports, "providerTools", { enumerable: true, get: function () { return provider_tools_1.providerTools; } });
|
|
62
62
|
var verify_1 = require("./sdk/verify");
|
|
63
63
|
Object.defineProperty(exports, "verify", { enumerable: true, get: function () { return verify_1.verify; } });
|
|
64
|
+
var generate_agent_code_1 = require("./codegen/generate-agent-code");
|
|
65
|
+
Object.defineProperty(exports, "generateAgentCode", { enumerable: true, get: function () { return generate_agent_code_1.generateAgentCode; } });
|
|
64
66
|
var message_1 = require("./sdk/message");
|
|
65
|
-
Object.defineProperty(exports, "toDbMessage", { enumerable: true, get: function () { return message_1.toDbMessage; } });
|
|
66
67
|
Object.defineProperty(exports, "filterLlmMessages", { enumerable: true, get: function () { return message_1.filterLlmMessages; } });
|
|
67
68
|
Object.defineProperty(exports, "isLlmMessage", { enumerable: true, get: function () { return message_1.isLlmMessage; } });
|
|
68
69
|
var catalog_1 = require("./sdk/catalog");
|
|
69
70
|
Object.defineProperty(exports, "fetchProviderCatalog", { enumerable: true, get: function () { return catalog_1.fetchProviderCatalog; } });
|
|
71
|
+
var provider_capabilities_1 = require("./sdk/provider-capabilities");
|
|
72
|
+
Object.defineProperty(exports, "providerCapabilities", { enumerable: true, get: function () { return provider_capabilities_1.providerCapabilities; } });
|
|
70
73
|
var sqlite_memory_1 = require("./storage/sqlite-memory");
|
|
71
74
|
Object.defineProperty(exports, "SqliteMemory", { enumerable: true, get: function () { return sqlite_memory_1.SqliteMemory; } });
|
|
75
|
+
var working_memory_1 = require("./runtime/working-memory");
|
|
76
|
+
Object.defineProperty(exports, "UPDATE_WORKING_MEMORY_TOOL_NAME", { enumerable: true, get: function () { return working_memory_1.UPDATE_WORKING_MEMORY_TOOL_NAME; } });
|
|
77
|
+
Object.defineProperty(exports, "WORKING_MEMORY_DEFAULT_INSTRUCTION", { enumerable: true, get: function () { return working_memory_1.WORKING_MEMORY_DEFAULT_INSTRUCTION; } });
|
|
72
78
|
var postgres_memory_1 = require("./storage/postgres-memory");
|
|
73
79
|
Object.defineProperty(exports, "PostgresMemory", { enumerable: true, get: function () { return postgres_memory_1.PostgresMemory; } });
|
|
80
|
+
var model_factory_1 = require("./runtime/model-factory");
|
|
81
|
+
Object.defineProperty(exports, "createModel", { enumerable: true, get: function () { return model_factory_1.createModel; } });
|
|
82
|
+
var title_generation_1 = require("./runtime/title-generation");
|
|
83
|
+
Object.defineProperty(exports, "generateTitleFromMessage", { enumerable: true, get: function () { return title_generation_1.generateTitleFromMessage; } });
|
|
74
84
|
var workspace_1 = require("./workspace");
|
|
75
85
|
Object.defineProperty(exports, "Workspace", { enumerable: true, get: function () { return workspace_1.Workspace; } });
|
|
76
86
|
var workspace_2 = require("./workspace");
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,iCAAqC;AAA5B,mGAAA,UAAU,OAAA;AAGnB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,6CAA4C;AAAnC,sGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,2CAA0C;AAAjC,oGAAA,QAAQ,OAAA;AAEjB,uDAAuC;AACvC,6CAA4C;AAAnC,sGAAA,SAAS,OAAA;AAClB,sDAA8D;AAArD,+GAAA,kBAAkB,OAAA;AAE3B,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;
|
|
1
|
+
{"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":";;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;AA2CA,iCAAqC;AAA5B,mGAAA,UAAU,OAAA;AAGnB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AACf,6CAA4C;AAAnC,sGAAA,SAAS,OAAA;AAClB,mCAAkC;AAAzB,4FAAA,IAAI,OAAA;AACb,2CAA0C;AAAjC,oGAAA,QAAQ,OAAA;AAEjB,uDAAuC;AACvC,6CAA4C;AAAnC,sGAAA,SAAS,OAAA;AAClB,sDAA8D;AAArD,+GAAA,kBAAkB,OAAA;AAE3B,qCAAoC;AAA3B,8FAAA,KAAK,OAAA;AAOd,+CAA6C;AAApC,uGAAA,SAAS,OAAA;AAClB,yCAAwC;AAA/B,kGAAA,OAAO,OAAA;AAChB,uDAAqD;AAA5C,+GAAA,aAAa,OAAA;AACtB,uCAAsC;AAA7B,gGAAA,MAAM,OAAA;AA6Bf,qEAAkE;AAAzD,wHAAA,iBAAiB,OAAA;AAC1B,yCAAgE;AAAvD,4GAAA,iBAAiB,OAAA;AAAE,uGAAA,YAAY,OAAA;AACxC,yCAAqD;AAA5C,+GAAA,oBAAoB,OAAA;AAC7B,qEAAmE;AAA1D,6HAAA,oBAAoB,OAAA;AAS7B,yDAAuD;AAA9C,6GAAA,YAAY,OAAA;AACrB,2DAGkC;AAFjC,iIAAA,+BAA+B,OAAA;AAC/B,oIAAA,kCAAkC,OAAA;AAGnC,6DAA2D;AAAlD,iHAAA,cAAc,OAAA;AAGvB,yDAAsD;AAA7C,4GAAA,WAAW,OAAA;AACpB,+DAAsE;AAA7D,4HAAA,wBAAwB,OAAA;AAEjC,yCAAwC;AAA/B,sGAAA,SAAS,OAAA;AAClB,yCAA6C;AAApC,2GAAA,cAAc,OAAA;AACvB,yCAA0C;AAAjC,wGAAA,WAAW,OAAA;AACpB,yCAAmD;AAA1C,iHAAA,oBAAoB,OAAA;AAC7B,yCAAmE;AAA1D,kHAAA,qBAAqB,OAAA;AAAE,0GAAA,aAAa,OAAA"}
|
|
@@ -1,8 +1,8 @@
|
|
|
1
1
|
import type { ProviderOptions } from '@ai-sdk/provider-utils';
|
|
2
2
|
import type { z } from 'zod';
|
|
3
|
-
import type { BuiltMemory, BuiltProviderTool, BuiltTool, CheckpointStore, GenerateResult, RunOptions, SemanticRecallConfig, SerializableAgentState, StreamResult, ThinkingConfig, TitleGenerationConfig
|
|
3
|
+
import type { BuiltMemory, BuiltProviderTool, BuiltTelemetry, BuiltTool, CheckpointStore, GenerateResult, RunOptions, SemanticRecallConfig, SerializableAgentState, StreamResult, ThinkingConfig, TitleGenerationConfig } from '../types';
|
|
4
4
|
import { AgentEventBus } from './event-bus';
|
|
5
|
-
import type {
|
|
5
|
+
import type { ExecutionOptions, ModelConfig } from '../types/sdk/agent';
|
|
6
6
|
import type { AgentMessage } from '../types/sdk/message';
|
|
7
7
|
export interface AgentRuntimeConfig {
|
|
8
8
|
name: string;
|
|
@@ -18,6 +18,7 @@ export interface AgentRuntimeConfig {
|
|
|
18
18
|
structured: boolean;
|
|
19
19
|
schema?: z.ZodObject<z.ZodRawShape>;
|
|
20
20
|
scope?: 'resource' | 'thread';
|
|
21
|
+
instruction?: string;
|
|
21
22
|
};
|
|
22
23
|
semanticRecall?: SemanticRecallConfig;
|
|
23
24
|
structuredOutput?: z.ZodType;
|
|
@@ -60,13 +61,13 @@ export declare class AgentRuntime {
|
|
|
60
61
|
private runStreamLoop;
|
|
61
62
|
private saveToMemory;
|
|
62
63
|
private saveEmbeddingsForMessages;
|
|
63
|
-
private extractAndPersistWorkingMemory;
|
|
64
64
|
private buildThinkingProviderOptions;
|
|
65
65
|
private buildCallProviderOptions;
|
|
66
66
|
private iterateToolCallsConcurrent;
|
|
67
67
|
private iteratePendingToolCallsConcurrent;
|
|
68
68
|
private processToolCall;
|
|
69
69
|
private buildLoopContext;
|
|
70
|
+
private buildWorkingMemoryToolForRun;
|
|
70
71
|
private persistSuspension;
|
|
71
72
|
private cleanupRun;
|
|
72
73
|
private emitTurnEnd;
|
|
@@ -43,7 +43,6 @@ const zod_to_json_schema_1 = require("zod-to-json-schema");
|
|
|
43
43
|
const catalog_1 = require("../sdk/catalog");
|
|
44
44
|
const message_1 = require("../sdk/message");
|
|
45
45
|
const event_bus_1 = require("./event-bus");
|
|
46
|
-
const logger_1 = require("./logger");
|
|
47
46
|
const memory_store_1 = require("./memory-store");
|
|
48
47
|
const message_list_1 = require("./message-list");
|
|
49
48
|
const messages_1 = require("./messages");
|
|
@@ -56,14 +55,6 @@ const title_generation_1 = require("./title-generation");
|
|
|
56
55
|
const tool_adapter_1 = require("./tool-adapter");
|
|
57
56
|
const working_memory_1 = require("./working-memory");
|
|
58
57
|
const zod_1 = require("../utils/zod");
|
|
59
|
-
const logger = (0, logger_1.createFilteredLogger)();
|
|
60
|
-
function isTextPart(part) {
|
|
61
|
-
return (typeof part === 'object' &&
|
|
62
|
-
part !== null &&
|
|
63
|
-
'type' in part &&
|
|
64
|
-
part.type === 'text' &&
|
|
65
|
-
'text' in part);
|
|
66
|
-
}
|
|
67
58
|
const MAX_LOOP_ITERATIONS = 20;
|
|
68
59
|
const ajv = new ajv_1.default({ strict: false });
|
|
69
60
|
const EMPTY_MESSAGE_LIST = {
|
|
@@ -199,7 +190,7 @@ class AgentRuntime {
|
|
|
199
190
|
limit: this.config.lastMessages ?? 10,
|
|
200
191
|
});
|
|
201
192
|
if (memMessages.length > 0) {
|
|
202
|
-
list.addHistory((0, strip_orphaned_tool_messages_1.stripOrphanedToolMessages)(memMessages
|
|
193
|
+
list.addHistory((0, strip_orphaned_tool_messages_1.stripOrphanedToolMessages)(memMessages));
|
|
203
194
|
}
|
|
204
195
|
}
|
|
205
196
|
if (this.config.semanticRecall && options?.persistence?.threadId) {
|
|
@@ -240,7 +231,7 @@ class AgentRuntime {
|
|
|
240
231
|
}
|
|
241
232
|
else {
|
|
242
233
|
recalled = allMsgs.filter((m) => {
|
|
243
|
-
const id =
|
|
234
|
+
const id = m.id;
|
|
244
235
|
return id !== undefined && hitIds.has(id);
|
|
245
236
|
});
|
|
246
237
|
}
|
|
@@ -258,12 +249,10 @@ class AgentRuntime {
|
|
|
258
249
|
return;
|
|
259
250
|
const { historyIds } = list.serialize();
|
|
260
251
|
const historyIdSet = new Set(historyIds);
|
|
261
|
-
const newRecalled = recalled
|
|
262
|
-
|
|
263
|
-
const id = 'id' in m && typeof m.id === 'string' ? m.id : undefined;
|
|
252
|
+
const newRecalled = recalled.filter((m) => {
|
|
253
|
+
const id = m.id;
|
|
264
254
|
return !id || !historyIdSet.has(id);
|
|
265
|
-
})
|
|
266
|
-
.map(message_1.toDbMessage);
|
|
255
|
+
});
|
|
267
256
|
if (newRecalled.length > 0) {
|
|
268
257
|
list.addHistory(newRecalled);
|
|
269
258
|
}
|
|
@@ -344,7 +333,7 @@ class AgentRuntime {
|
|
|
344
333
|
};
|
|
345
334
|
}
|
|
346
335
|
async runGenerateLoop(list, options, pendingResume, runId) {
|
|
347
|
-
const { model, toolMap, aiTools, providerOptions, hasTools, outputSpec } = this.buildLoopContext(options);
|
|
336
|
+
const { model, toolMap, aiTools, providerOptions, hasTools, outputSpec } = this.buildLoopContext({ ...options, persistence: options?.persistence });
|
|
348
337
|
let totalUsage;
|
|
349
338
|
let lastFinishReason = 'stop';
|
|
350
339
|
let structuredOutput;
|
|
@@ -435,15 +424,6 @@ class AgentRuntime {
|
|
|
435
424
|
if (lastFinishReason === 'tool-calls') {
|
|
436
425
|
throw new Error(`Agent loop exceeded ${maxIterations} iterations without reaching a stop condition`);
|
|
437
426
|
}
|
|
438
|
-
if (this.config.workingMemory &&
|
|
439
|
-
this.config.memory?.saveWorkingMemory &&
|
|
440
|
-
options?.persistence) {
|
|
441
|
-
this.extractAndPersistWorkingMemory(list, {
|
|
442
|
-
threadId: options.persistence.threadId,
|
|
443
|
-
resourceId: options.persistence.resourceId,
|
|
444
|
-
scope: this.config.workingMemory?.scope ?? 'resource',
|
|
445
|
-
});
|
|
446
|
-
}
|
|
447
427
|
await this.saveToMemory(list, options);
|
|
448
428
|
await this.flushTelemetry(options);
|
|
449
429
|
if (this.config.titleGeneration && options?.persistence?.threadId && this.config.memory) {
|
|
@@ -484,20 +464,9 @@ class AgentRuntime {
|
|
|
484
464
|
return readable;
|
|
485
465
|
}
|
|
486
466
|
async runStreamLoop(list, options, writer, pendingResume, runId) {
|
|
487
|
-
const { model, toolMap, aiTools, providerOptions, hasTools, outputSpec } = this.buildLoopContext(options);
|
|
488
|
-
const wmParamsStream = this.resolveWorkingMemoryParams(options?.persistence);
|
|
489
|
-
const wmFilter = wmParamsStream?.persistFn
|
|
490
|
-
? new working_memory_1.WorkingMemoryStreamFilter(writer, async (content) => {
|
|
491
|
-
await wmParamsStream.persistFn(content);
|
|
492
|
-
})
|
|
493
|
-
: undefined;
|
|
467
|
+
const { model, toolMap, aiTools, providerOptions, hasTools, outputSpec } = this.buildLoopContext({ ...options, persistence: options?.persistence });
|
|
494
468
|
const writeChunk = async (chunk) => {
|
|
495
|
-
|
|
496
|
-
await wmFilter.write(chunk);
|
|
497
|
-
}
|
|
498
|
-
else {
|
|
499
|
-
await writer.write(chunk);
|
|
500
|
-
}
|
|
469
|
+
await writer.write(chunk);
|
|
501
470
|
};
|
|
502
471
|
let totalUsage;
|
|
503
472
|
let lastFinishReason = 'stop';
|
|
@@ -507,8 +476,6 @@ class AgentRuntime {
|
|
|
507
476
|
const closeStreamWithError = async (error, status) => {
|
|
508
477
|
await this.cleanupRun(runId);
|
|
509
478
|
this.updateState({ status });
|
|
510
|
-
if (wmFilter)
|
|
511
|
-
await wmFilter.flush();
|
|
512
479
|
await writer.write({ type: 'error', error });
|
|
513
480
|
await writer.write({ type: 'finish', finishReason: 'error' });
|
|
514
481
|
await writer.close();
|
|
@@ -663,8 +630,6 @@ class AgentRuntime {
|
|
|
663
630
|
}
|
|
664
631
|
this.emitTurnEnd(newMessages, (0, runtime_helpers_1.extractToolResults)(list.responseDelta()));
|
|
665
632
|
}
|
|
666
|
-
if (wmFilter)
|
|
667
|
-
await wmFilter.flush();
|
|
668
633
|
const costUsage = this.applyCost(totalUsage);
|
|
669
634
|
const parentCost = costUsage?.cost ?? 0;
|
|
670
635
|
const subCost = collectedSubAgentUsage.reduce((sum, s) => sum + (s.usage.cost ?? 0), 0);
|
|
@@ -680,15 +645,6 @@ class AgentRuntime {
|
|
|
680
645
|
}),
|
|
681
646
|
});
|
|
682
647
|
try {
|
|
683
|
-
if (this.config.workingMemory &&
|
|
684
|
-
this.config.memory?.saveWorkingMemory &&
|
|
685
|
-
options?.persistence) {
|
|
686
|
-
this.extractAndPersistWorkingMemory(list, {
|
|
687
|
-
threadId: options.persistence.threadId,
|
|
688
|
-
resourceId: options.persistence.resourceId,
|
|
689
|
-
scope: this.config.workingMemory?.scope ?? 'resource',
|
|
690
|
-
});
|
|
691
|
-
}
|
|
692
648
|
await this.saveToMemory(list, options);
|
|
693
649
|
if (this.config.titleGeneration && options?.persistence && this.config.memory) {
|
|
694
650
|
void (0, title_generation_1.generateThreadTitle)({
|
|
@@ -756,35 +712,6 @@ class AgentRuntime {
|
|
|
756
712
|
})),
|
|
757
713
|
});
|
|
758
714
|
}
|
|
759
|
-
extractAndPersistWorkingMemory(list, params) {
|
|
760
|
-
const delta = list.responseDelta();
|
|
761
|
-
for (let i = delta.length - 1; i >= 0; i--) {
|
|
762
|
-
const msg = delta[i];
|
|
763
|
-
if (!(0, message_1.isLlmMessage)(msg) || msg.role !== 'assistant')
|
|
764
|
-
continue;
|
|
765
|
-
for (const part of msg.content) {
|
|
766
|
-
if (!isTextPart(part))
|
|
767
|
-
continue;
|
|
768
|
-
const { cleanText, workingMemory } = (0, working_memory_1.parseWorkingMemory)(part.text);
|
|
769
|
-
if (workingMemory !== null) {
|
|
770
|
-
if (this.config.workingMemory?.structured && this.config.workingMemory.schema) {
|
|
771
|
-
try {
|
|
772
|
-
this.config.workingMemory.schema.parse(JSON.parse(workingMemory));
|
|
773
|
-
}
|
|
774
|
-
catch {
|
|
775
|
-
part.text = cleanText;
|
|
776
|
-
return;
|
|
777
|
-
}
|
|
778
|
-
}
|
|
779
|
-
part.text = cleanText;
|
|
780
|
-
this.config.memory.saveWorkingMemory(params, workingMemory).catch((error) => {
|
|
781
|
-
logger.warn('Failed to persist working memory', { error });
|
|
782
|
-
});
|
|
783
|
-
}
|
|
784
|
-
return;
|
|
785
|
-
}
|
|
786
|
-
}
|
|
787
|
-
}
|
|
788
715
|
buildThinkingProviderOptions() {
|
|
789
716
|
if (!this.config.thinking)
|
|
790
717
|
return undefined;
|
|
@@ -1109,10 +1036,8 @@ class AgentRuntime {
|
|
|
1109
1036
|
: actualResult;
|
|
1110
1037
|
const toolResultMsg = (0, runtime_helpers_1.makeToolResultMessage)(toolCallId, toolName, modelResult);
|
|
1111
1038
|
list.addResponse([toolResultMsg]);
|
|
1112
|
-
const
|
|
1113
|
-
|
|
1114
|
-
if (customToolMessage) {
|
|
1115
|
-
customMessage = (0, message_1.toDbMessage)(customToolMessage);
|
|
1039
|
+
const customMessage = builtTool?.toMessage?.(actualResult);
|
|
1040
|
+
if (customMessage) {
|
|
1116
1041
|
list.addResponse([customMessage]);
|
|
1117
1042
|
}
|
|
1118
1043
|
return {
|
|
@@ -1129,12 +1054,16 @@ class AgentRuntime {
|
|
|
1129
1054
|
};
|
|
1130
1055
|
}
|
|
1131
1056
|
buildLoopContext(execOptions) {
|
|
1132
|
-
const
|
|
1057
|
+
const wmTool = this.buildWorkingMemoryToolForRun(execOptions?.persistence);
|
|
1058
|
+
const allUserTools = wmTool
|
|
1059
|
+
? [...(this.config.tools ?? []), wmTool]
|
|
1060
|
+
: (this.config.tools ?? []);
|
|
1061
|
+
const aiTools = (0, tool_adapter_1.toAiSdkTools)(allUserTools);
|
|
1133
1062
|
const aiProviderTools = (0, tool_adapter_1.toAiSdkProviderTools)(this.config.providerTools);
|
|
1134
1063
|
const allTools = { ...aiTools, ...aiProviderTools };
|
|
1135
1064
|
return {
|
|
1136
1065
|
model: (0, model_factory_1.createModel)(this.config.model),
|
|
1137
|
-
toolMap: (0, tool_adapter_1.buildToolMap)(
|
|
1066
|
+
toolMap: (0, tool_adapter_1.buildToolMap)(allUserTools),
|
|
1138
1067
|
aiTools: allTools,
|
|
1139
1068
|
providerOptions: this.buildCallProviderOptions(execOptions?.providerOptions),
|
|
1140
1069
|
hasTools: Object.keys(allTools).length > 0,
|
|
@@ -1143,6 +1072,16 @@ class AgentRuntime {
|
|
|
1143
1072
|
: undefined,
|
|
1144
1073
|
};
|
|
1145
1074
|
}
|
|
1075
|
+
buildWorkingMemoryToolForRun(persistence) {
|
|
1076
|
+
const wmParams = this.resolveWorkingMemoryParams(persistence);
|
|
1077
|
+
if (!wmParams)
|
|
1078
|
+
return undefined;
|
|
1079
|
+
return (0, working_memory_1.buildWorkingMemoryTool)({
|
|
1080
|
+
structured: wmParams.structured,
|
|
1081
|
+
schema: wmParams.schema,
|
|
1082
|
+
persist: wmParams.persistFn,
|
|
1083
|
+
});
|
|
1084
|
+
}
|
|
1146
1085
|
async persistSuspension(pendingToolCalls, options, list, totalUsage, existingRunId) {
|
|
1147
1086
|
const runId = existingRunId ?? (0, run_state_1.generateRunId)();
|
|
1148
1087
|
const executionOptions = options?.maxIterations !== undefined ? { maxIterations: options.maxIterations } : undefined;
|
|
@@ -1211,6 +1150,7 @@ class AgentRuntime {
|
|
|
1211
1150
|
template: wmParams.template,
|
|
1212
1151
|
structured: wmParams.structured,
|
|
1213
1152
|
state: wmState,
|
|
1153
|
+
...(wmParams.instruction !== undefined && { instruction: wmParams.instruction }),
|
|
1214
1154
|
};
|
|
1215
1155
|
}
|
|
1216
1156
|
resolveWorkingMemoryParams(options) {
|
|
@@ -1239,6 +1179,7 @@ class AgentRuntime {
|
|
|
1239
1179
|
template: this.config.workingMemory.template,
|
|
1240
1180
|
structured: this.config.workingMemory.structured,
|
|
1241
1181
|
schema: this.config.workingMemory.schema,
|
|
1182
|
+
instruction: this.config.workingMemory.instruction,
|
|
1242
1183
|
};
|
|
1243
1184
|
}
|
|
1244
1185
|
}
|