@nexalab/agent-sdk 0.1.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/LICENSE +15 -0
- package/README.md +518 -0
- package/bin/nexa.mjs +33 -0
- package/dist/advanced-memory.d.ts +32 -0
- package/dist/advanced-memory.d.ts.map +1 -0
- package/dist/advanced-memory.js +54 -0
- package/dist/advanced-memory.js.map +1 -0
- package/dist/agent-builder.d.ts +60 -0
- package/dist/agent-builder.d.ts.map +1 -0
- package/dist/agent-builder.js +141 -0
- package/dist/agent-builder.js.map +1 -0
- package/dist/approvals.d.ts +34 -0
- package/dist/approvals.d.ts.map +1 -0
- package/dist/approvals.js +54 -0
- package/dist/approvals.js.map +1 -0
- package/dist/artifacts.d.ts +13 -0
- package/dist/artifacts.d.ts.map +1 -0
- package/dist/artifacts.js +13 -0
- package/dist/artifacts.js.map +1 -0
- package/dist/cache.d.ts +17 -0
- package/dist/cache.d.ts.map +1 -0
- package/dist/cache.js +29 -0
- package/dist/cache.js.map +1 -0
- package/dist/completion-gate.d.ts +5 -0
- package/dist/completion-gate.d.ts.map +1 -0
- package/dist/completion-gate.js +20 -0
- package/dist/completion-gate.js.map +1 -0
- package/dist/console-chat.d.ts +110 -0
- package/dist/console-chat.d.ts.map +1 -0
- package/dist/console-chat.js +284 -0
- package/dist/console-chat.js.map +1 -0
- package/dist/context.d.ts +45 -0
- package/dist/context.d.ts.map +1 -0
- package/dist/context.js +92 -0
- package/dist/context.js.map +1 -0
- package/dist/evals.d.ts +38 -0
- package/dist/evals.d.ts.map +1 -0
- package/dist/evals.js +37 -0
- package/dist/evals.js.map +1 -0
- package/dist/event-bus.d.ts +28 -0
- package/dist/event-bus.d.ts.map +1 -0
- package/dist/event-bus.js +28 -0
- package/dist/event-bus.js.map +1 -0
- package/dist/file-storage.d.ts +25 -0
- package/dist/file-storage.d.ts.map +1 -0
- package/dist/file-storage.js +100 -0
- package/dist/file-storage.js.map +1 -0
- package/dist/guardrails.d.ts +44 -0
- package/dist/guardrails.d.ts.map +1 -0
- package/dist/guardrails.js +37 -0
- package/dist/guardrails.js.map +1 -0
- package/dist/harness.d.ts +26 -0
- package/dist/harness.d.ts.map +1 -0
- package/dist/harness.js +49 -0
- package/dist/harness.js.map +1 -0
- package/dist/hooks.d.ts +58 -0
- package/dist/hooks.d.ts.map +1 -0
- package/dist/hooks.js +16 -0
- package/dist/hooks.js.map +1 -0
- package/dist/http-adapter.d.ts +13 -0
- package/dist/http-adapter.d.ts.map +1 -0
- package/dist/http-adapter.js +33 -0
- package/dist/http-adapter.js.map +1 -0
- package/dist/index.d.ts +58 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +58 -0
- package/dist/index.js.map +1 -0
- package/dist/kernel.d.ts +90 -0
- package/dist/kernel.d.ts.map +1 -0
- package/dist/kernel.js +251 -0
- package/dist/kernel.js.map +1 -0
- package/dist/knowledge.d.ts +35 -0
- package/dist/knowledge.d.ts.map +1 -0
- package/dist/knowledge.js +89 -0
- package/dist/knowledge.js.map +1 -0
- package/dist/local-tool-executor.d.ts +26 -0
- package/dist/local-tool-executor.d.ts.map +1 -0
- package/dist/local-tool-executor.js +30 -0
- package/dist/local-tool-executor.js.map +1 -0
- package/dist/loop-guard.d.ts +15 -0
- package/dist/loop-guard.d.ts.map +1 -0
- package/dist/loop-guard.js +12 -0
- package/dist/loop-guard.js.map +1 -0
- package/dist/manifest.d.ts +43 -0
- package/dist/manifest.d.ts.map +1 -0
- package/dist/manifest.js +31 -0
- package/dist/manifest.js.map +1 -0
- package/dist/mcp.d.ts +55 -0
- package/dist/mcp.d.ts.map +1 -0
- package/dist/mcp.js +175 -0
- package/dist/mcp.js.map +1 -0
- package/dist/memory.d.ts +32 -0
- package/dist/memory.d.ts.map +1 -0
- package/dist/memory.js +40 -0
- package/dist/memory.js.map +1 -0
- package/dist/mission-runtime.d.ts +106 -0
- package/dist/mission-runtime.d.ts.map +1 -0
- package/dist/mission-runtime.js +196 -0
- package/dist/mission-runtime.js.map +1 -0
- package/dist/model-harness.d.ts +23 -0
- package/dist/model-harness.d.ts.map +1 -0
- package/dist/model-harness.js +7 -0
- package/dist/model-harness.js.map +1 -0
- package/dist/model-router.d.ts +60 -0
- package/dist/model-router.d.ts.map +1 -0
- package/dist/model-router.js +90 -0
- package/dist/model-router.js.map +1 -0
- package/dist/multi-agent.d.ts +56 -0
- package/dist/multi-agent.d.ts.map +1 -0
- package/dist/multi-agent.js +69 -0
- package/dist/multi-agent.js.map +1 -0
- package/dist/native-tool-loop.d.ts +70 -0
- package/dist/native-tool-loop.d.ts.map +1 -0
- package/dist/native-tool-loop.js +92 -0
- package/dist/native-tool-loop.js.map +1 -0
- package/dist/openai-agent.d.ts +75 -0
- package/dist/openai-agent.d.ts.map +1 -0
- package/dist/openai-agent.js +190 -0
- package/dist/openai-agent.js.map +1 -0
- package/dist/openai-compatible.d.ts +117 -0
- package/dist/openai-compatible.d.ts.map +1 -0
- package/dist/openai-compatible.js +486 -0
- package/dist/openai-compatible.js.map +1 -0
- package/dist/otel.d.ts +12 -0
- package/dist/otel.d.ts.map +1 -0
- package/dist/otel.js +35 -0
- package/dist/otel.js.map +1 -0
- package/dist/permissions.d.ts +6 -0
- package/dist/permissions.d.ts.map +1 -0
- package/dist/permissions.js +33 -0
- package/dist/permissions.js.map +1 -0
- package/dist/plan-validator.d.ts +9 -0
- package/dist/plan-validator.d.ts.map +1 -0
- package/dist/plan-validator.js +27 -0
- package/dist/plan-validator.js.map +1 -0
- package/dist/policy.d.ts +27 -0
- package/dist/policy.d.ts.map +1 -0
- package/dist/policy.js +37 -0
- package/dist/policy.js.map +1 -0
- package/dist/postgres-storage.d.ts +33 -0
- package/dist/postgres-storage.d.ts.map +1 -0
- package/dist/postgres-storage.js +80 -0
- package/dist/postgres-storage.js.map +1 -0
- package/dist/provider-capabilities.d.ts +13 -0
- package/dist/provider-capabilities.d.ts.map +1 -0
- package/dist/provider-capabilities.js +19 -0
- package/dist/provider-capabilities.js.map +1 -0
- package/dist/providers.d.ts +14 -0
- package/dist/providers.d.ts.map +1 -0
- package/dist/providers.js +69 -0
- package/dist/providers.js.map +1 -0
- package/dist/queue.d.ts +29 -0
- package/dist/queue.d.ts.map +1 -0
- package/dist/queue.js +36 -0
- package/dist/queue.js.map +1 -0
- package/dist/redis-storage.d.ts +36 -0
- package/dist/redis-storage.d.ts.map +1 -0
- package/dist/redis-storage.js +68 -0
- package/dist/redis-storage.js.map +1 -0
- package/dist/replay.d.ts +13 -0
- package/dist/replay.d.ts.map +1 -0
- package/dist/replay.js +32 -0
- package/dist/replay.js.map +1 -0
- package/dist/runtime.d.ts +54 -0
- package/dist/runtime.d.ts.map +1 -0
- package/dist/runtime.js +218 -0
- package/dist/runtime.js.map +1 -0
- package/dist/sdk.d.ts +43 -0
- package/dist/sdk.d.ts.map +1 -0
- package/dist/sdk.js +82 -0
- package/dist/sdk.js.map +1 -0
- package/dist/secrets.d.ts +9 -0
- package/dist/secrets.d.ts.map +1 -0
- package/dist/secrets.js +23 -0
- package/dist/secrets.js.map +1 -0
- package/dist/sessions.d.ts +44 -0
- package/dist/sessions.d.ts.map +1 -0
- package/dist/sessions.js +76 -0
- package/dist/sessions.js.map +1 -0
- package/dist/skills.d.ts +12 -0
- package/dist/skills.d.ts.map +1 -0
- package/dist/skills.js +13 -0
- package/dist/skills.js.map +1 -0
- package/dist/smoke.d.ts +8 -0
- package/dist/smoke.d.ts.map +1 -0
- package/dist/smoke.js +5 -0
- package/dist/smoke.js.map +1 -0
- package/dist/sqlite-storage.d.ts +30 -0
- package/dist/sqlite-storage.d.ts.map +1 -0
- package/dist/sqlite-storage.js +52 -0
- package/dist/sqlite-storage.js.map +1 -0
- package/dist/storage.d.ts +74 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +83 -0
- package/dist/storage.js.map +1 -0
- package/dist/structured-output.d.ts +14 -0
- package/dist/structured-output.d.ts.map +1 -0
- package/dist/structured-output.js +24 -0
- package/dist/structured-output.js.map +1 -0
- package/dist/telemetry.d.ts +27 -0
- package/dist/telemetry.d.ts.map +1 -0
- package/dist/telemetry.js +40 -0
- package/dist/telemetry.js.map +1 -0
- package/dist/tenancy.d.ts +9 -0
- package/dist/tenancy.d.ts.map +1 -0
- package/dist/tenancy.js +8 -0
- package/dist/tenancy.js.map +1 -0
- package/dist/tool-factory.d.ts +25 -0
- package/dist/tool-factory.d.ts.map +1 -0
- package/dist/tool-factory.js +38 -0
- package/dist/tool-factory.js.map +1 -0
- package/dist/tool-orchestrator.d.ts +89 -0
- package/dist/tool-orchestrator.d.ts.map +1 -0
- package/dist/tool-orchestrator.js +102 -0
- package/dist/tool-orchestrator.js.map +1 -0
- package/dist/tool-registry.d.ts +52 -0
- package/dist/tool-registry.d.ts.map +1 -0
- package/dist/tool-registry.js +19 -0
- package/dist/tool-registry.js.map +1 -0
- package/dist/types.d.ts +1020 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +115 -0
- package/dist/types.js.map +1 -0
- package/dist/vector-memory.d.ts +19 -0
- package/dist/vector-memory.d.ts.map +1 -0
- package/dist/vector-memory.js +58 -0
- package/dist/vector-memory.js.map +1 -0
- package/dist/workflow.d.ts +65 -0
- package/dist/workflow.d.ts.map +1 -0
- package/dist/workflow.js +102 -0
- package/dist/workflow.js.map +1 -0
- package/dist/workspace.d.ts +35 -0
- package/dist/workspace.d.ts.map +1 -0
- package/dist/workspace.js +131 -0
- package/dist/workspace.js.map +1 -0
- package/package.json +223 -0
package/dist/mcp.d.ts
ADDED
|
@@ -0,0 +1,55 @@
|
|
|
1
|
+
import type { LocalToolHandler } from "./local-tool-executor.js";
|
|
2
|
+
import type { ToolDefinition } from "./tool-registry.js";
|
|
3
|
+
import type { JsonObject, JsonValue } from "./types.js";
|
|
4
|
+
export type McpTransport = {
|
|
5
|
+
send(message: JsonObject): Promise<void>;
|
|
6
|
+
onMessage(handler: (message: JsonObject) => void): void;
|
|
7
|
+
close(): Promise<void>;
|
|
8
|
+
};
|
|
9
|
+
export type StdioTransportConfig = {
|
|
10
|
+
command: string;
|
|
11
|
+
args?: string[];
|
|
12
|
+
env?: Record<string, string>;
|
|
13
|
+
cwd?: string;
|
|
14
|
+
};
|
|
15
|
+
export type HttpTransportConfig = {
|
|
16
|
+
url: string;
|
|
17
|
+
headers?: Record<string, string>;
|
|
18
|
+
};
|
|
19
|
+
export type McpConnectorConfig = StdioTransportConfig | HttpTransportConfig;
|
|
20
|
+
export declare function createStdioTransport(config: StdioTransportConfig): McpTransport;
|
|
21
|
+
export declare function createHttpTransport(config: HttpTransportConfig): McpTransport;
|
|
22
|
+
export type McpToolInfo = {
|
|
23
|
+
name: string;
|
|
24
|
+
description?: string;
|
|
25
|
+
inputSchema?: JsonObject;
|
|
26
|
+
};
|
|
27
|
+
export declare class McpClient {
|
|
28
|
+
private readonly transport;
|
|
29
|
+
private readonly pending;
|
|
30
|
+
constructor(transport: McpTransport);
|
|
31
|
+
initialize(clientInfo?: {
|
|
32
|
+
name: string;
|
|
33
|
+
version: string;
|
|
34
|
+
}): Promise<JsonValue>;
|
|
35
|
+
listTools(): Promise<McpToolInfo[]>;
|
|
36
|
+
callTool(name: string, args: JsonObject): Promise<{
|
|
37
|
+
content: JsonValue;
|
|
38
|
+
isError: boolean;
|
|
39
|
+
}>;
|
|
40
|
+
close(): Promise<void>;
|
|
41
|
+
private handleMessage;
|
|
42
|
+
private request;
|
|
43
|
+
private notify;
|
|
44
|
+
}
|
|
45
|
+
export type McpToolRegistration = {
|
|
46
|
+
tool: ToolDefinition;
|
|
47
|
+
handler: LocalToolHandler;
|
|
48
|
+
};
|
|
49
|
+
export type McpConnection = {
|
|
50
|
+
client: McpClient;
|
|
51
|
+
discoverTools(): Promise<McpToolRegistration[]>;
|
|
52
|
+
close(): Promise<void>;
|
|
53
|
+
};
|
|
54
|
+
export declare function mcp(config: McpConnectorConfig): Promise<McpConnection>;
|
|
55
|
+
//# sourceMappingURL=mcp.d.ts.map
|
|
@@ -0,0 +1 @@
|
|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AGR,OAAO,CAAC,QAAQ,CAAC,SAAS;IAFtC,OAAO,CAAC,QAAQ,CAAC,OAAO,CAA8F;gBAEzF,SAAS,EAAE,YAAY;IAI9C,UAAU,CAAC,UAAU,GAAE;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,OAAO,EAAE,MAAM,CAAA;KAAiD,GAAG,OAAO,CAAC,SAAS,CAAC;IAM5H,SAAS,IAAI,OAAO,CAAC,WAAW,EAAE,CAAC;IAKnC,QAAQ,CAAC,IAAI,EAAE,MAAM,EAAE,IAAI,EAAE,UAAU,GAAG,OAAO,CAAC;QAAE,OAAO,EAAE,SAAS,CAAC;QAAC,OAAO,EAAE,OAAO,CAAA;KAAE,CAAC;IAK3F,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC;IAI5B,OAAO,CAAC,aAAa;YAcP,OAAO;YAOP,MAAM;CAGrB;AAED,MAAM,MAAM,mBAAmB,GAAG;IAAE,IAAI,EAAE,cAAc,CAAC;IAAC,OAAO,EAAE,gBAAgB,CAAA;CAAE,CAAC;AAEtF,MAAM,MAAM,aAAa,GAAG;IAC1B,MAAM,EAAE,SAAS,CAAC;IAClB,aAAa,IAAI,OAAO,CAAC,mBAAmB,EAAE,CAAC,CAAC;IAChD,KAAK,IAAI,OAAO,CAAC,IAAI,CAAC,CAAC;CACxB,CAAC;AAEF,wBAAsB,GAAG,CAAC,MAAM,EAAE,kBAAkB,GAAG,OAAO,CAAC,aAAa,CAAC,CAa5E"}
|
package/dist/mcp.js
ADDED
|
@@ -0,0 +1,175 @@
|
|
|
1
|
+
import { spawn } from "node:child_process";
|
|
2
|
+
import { randomUUID } from "node:crypto";
|
|
3
|
+
export function createStdioTransport(config) {
|
|
4
|
+
const child = spawn(config.command, config.args ?? [], {
|
|
5
|
+
env: { ...process.env, ...(config.env ?? {}) },
|
|
6
|
+
...(config.cwd ? { cwd: config.cwd } : {})
|
|
7
|
+
});
|
|
8
|
+
const handlers = [];
|
|
9
|
+
let buffer = "";
|
|
10
|
+
child.stdout.on("data", (chunk) => {
|
|
11
|
+
buffer += chunk.toString("utf8");
|
|
12
|
+
const lines = buffer.split("\n");
|
|
13
|
+
buffer = lines.pop() ?? "";
|
|
14
|
+
for (const line of lines) {
|
|
15
|
+
const trimmed = line.trim();
|
|
16
|
+
if (!trimmed)
|
|
17
|
+
continue;
|
|
18
|
+
try {
|
|
19
|
+
const message = JSON.parse(trimmed);
|
|
20
|
+
for (const handler of handlers)
|
|
21
|
+
handler(message);
|
|
22
|
+
}
|
|
23
|
+
catch {
|
|
24
|
+
continue;
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
});
|
|
28
|
+
return {
|
|
29
|
+
async send(message) {
|
|
30
|
+
await new Promise((resolvePromise, rejectPromise) => {
|
|
31
|
+
child.stdin.write(`${JSON.stringify(message)}\n`, (error) => (error ? rejectPromise(error) : resolvePromise()));
|
|
32
|
+
});
|
|
33
|
+
},
|
|
34
|
+
onMessage(handler) {
|
|
35
|
+
handlers.push(handler);
|
|
36
|
+
},
|
|
37
|
+
async close() {
|
|
38
|
+
child.kill();
|
|
39
|
+
}
|
|
40
|
+
};
|
|
41
|
+
}
|
|
42
|
+
export function createHttpTransport(config) {
|
|
43
|
+
const handlers = [];
|
|
44
|
+
return {
|
|
45
|
+
async send(message) {
|
|
46
|
+
const response = await fetch(config.url, {
|
|
47
|
+
method: "POST",
|
|
48
|
+
headers: { "content-type": "application/json", accept: "application/json, text/event-stream", ...(config.headers ?? {}) },
|
|
49
|
+
body: JSON.stringify(message)
|
|
50
|
+
});
|
|
51
|
+
if (!response.ok)
|
|
52
|
+
throw new Error(`MCP HTTP transport failed with status ${response.status}`);
|
|
53
|
+
if (response.status === 202)
|
|
54
|
+
return;
|
|
55
|
+
const contentType = response.headers.get("content-type") ?? "";
|
|
56
|
+
if (contentType.includes("text/event-stream")) {
|
|
57
|
+
const text = await response.text();
|
|
58
|
+
for (const line of text.split("\n")) {
|
|
59
|
+
if (!line.startsWith("data:"))
|
|
60
|
+
continue;
|
|
61
|
+
const data = line.slice(5).trim();
|
|
62
|
+
if (!data)
|
|
63
|
+
continue;
|
|
64
|
+
handlers.forEach((handler) => handler(JSON.parse(data)));
|
|
65
|
+
}
|
|
66
|
+
return;
|
|
67
|
+
}
|
|
68
|
+
const body = (await response.json());
|
|
69
|
+
handlers.forEach((handler) => handler(body));
|
|
70
|
+
},
|
|
71
|
+
onMessage(handler) {
|
|
72
|
+
handlers.push(handler);
|
|
73
|
+
},
|
|
74
|
+
async close() { }
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
export class McpClient {
|
|
78
|
+
transport;
|
|
79
|
+
pending = new Map();
|
|
80
|
+
constructor(transport) {
|
|
81
|
+
this.transport = transport;
|
|
82
|
+
transport.onMessage((message) => this.handleMessage(message));
|
|
83
|
+
}
|
|
84
|
+
async initialize(clientInfo = { name: "nexa-agent-sdk", version: "1.0.0" }) {
|
|
85
|
+
const result = await this.request("initialize", { protocolVersion: "2024-11-05", capabilities: {}, clientInfo });
|
|
86
|
+
await this.notify("notifications/initialized");
|
|
87
|
+
return result;
|
|
88
|
+
}
|
|
89
|
+
async listTools() {
|
|
90
|
+
const result = (await this.request("tools/list"));
|
|
91
|
+
return result.tools ?? [];
|
|
92
|
+
}
|
|
93
|
+
async callTool(name, args) {
|
|
94
|
+
const result = (await this.request("tools/call", { name, arguments: args }));
|
|
95
|
+
return { content: result.content ?? [], isError: Boolean(result.isError) };
|
|
96
|
+
}
|
|
97
|
+
async close() {
|
|
98
|
+
await this.transport.close();
|
|
99
|
+
}
|
|
100
|
+
handleMessage(message) {
|
|
101
|
+
if (message.id === undefined || message.id === null)
|
|
102
|
+
return;
|
|
103
|
+
const key = String(message.id);
|
|
104
|
+
const waiter = this.pending.get(key);
|
|
105
|
+
if (!waiter)
|
|
106
|
+
return;
|
|
107
|
+
this.pending.delete(key);
|
|
108
|
+
if (message.error) {
|
|
109
|
+
const error = message.error;
|
|
110
|
+
waiter.reject(new Error(`MCP error ${String(error.code ?? "?")}: ${String(error.message ?? "unknown error")}`));
|
|
111
|
+
return;
|
|
112
|
+
}
|
|
113
|
+
waiter.resolve((message.result ?? null));
|
|
114
|
+
}
|
|
115
|
+
async request(method, params) {
|
|
116
|
+
const id = randomUUID();
|
|
117
|
+
const promise = new Promise((resolvePromise, rejectPromise) => this.pending.set(id, { resolve: resolvePromise, reject: rejectPromise }));
|
|
118
|
+
await this.transport.send({ jsonrpc: "2.0", id, method, ...(params ? { params } : {}) });
|
|
119
|
+
return promise;
|
|
120
|
+
}
|
|
121
|
+
async notify(method, params) {
|
|
122
|
+
await this.transport.send({ jsonrpc: "2.0", method, ...(params ? { params } : {}) });
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
export async function mcp(config) {
|
|
126
|
+
const transport = isHttpConfig(config) ? createHttpTransport(config) : createStdioTransport(config);
|
|
127
|
+
const client = new McpClient(transport);
|
|
128
|
+
await client.initialize();
|
|
129
|
+
return {
|
|
130
|
+
client,
|
|
131
|
+
async discoverTools() {
|
|
132
|
+
const tools = await client.listTools();
|
|
133
|
+
return tools.map((toolInfo) => toRegistration(client, toolInfo));
|
|
134
|
+
},
|
|
135
|
+
close: () => client.close()
|
|
136
|
+
};
|
|
137
|
+
}
|
|
138
|
+
function isHttpConfig(config) {
|
|
139
|
+
return "url" in config;
|
|
140
|
+
}
|
|
141
|
+
function toRegistration(client, info) {
|
|
142
|
+
const id = `mcp.${info.name}`;
|
|
143
|
+
return {
|
|
144
|
+
tool: {
|
|
145
|
+
id,
|
|
146
|
+
name: info.name,
|
|
147
|
+
capability: id,
|
|
148
|
+
version: "1.0.0",
|
|
149
|
+
provider: "mcp",
|
|
150
|
+
enabled: true,
|
|
151
|
+
...(info.description ? { description: info.description } : {}),
|
|
152
|
+
...(info.inputSchema ? { inputSchema: info.inputSchema } : {})
|
|
153
|
+
},
|
|
154
|
+
handler: async (input) => {
|
|
155
|
+
try {
|
|
156
|
+
const result = await client.callTool(info.name, input);
|
|
157
|
+
if (result.isError)
|
|
158
|
+
return { status: "error", error: summarizeMcpContent(result.content), retryable: false };
|
|
159
|
+
return { status: "ok", output: { content: result.content } };
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
return { status: "error", error: error instanceof Error ? error.message : String(error), retryable: true };
|
|
163
|
+
}
|
|
164
|
+
}
|
|
165
|
+
};
|
|
166
|
+
}
|
|
167
|
+
function summarizeMcpContent(content) {
|
|
168
|
+
if (Array.isArray(content)) {
|
|
169
|
+
return content
|
|
170
|
+
.map((item) => (item && typeof item === "object" && "text" in item ? String(item.text) : JSON.stringify(item)))
|
|
171
|
+
.join("\n");
|
|
172
|
+
}
|
|
173
|
+
return JSON.stringify(content);
|
|
174
|
+
}
|
|
175
|
+
//# sourceMappingURL=mcp.js.map
|
package/dist/mcp.js.map
ADDED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"mcp.js","sourceRoot":"","sources":["../src/mcp.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,KAAK,EAAE,MAAM,oBAAoB,CAAC;AAC3C,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAezC,MAAM,UAAU,oBAAoB,CAAC,MAA4B;IAC/D,MAAM,KAAK,GAAG,KAAK,CAAC,MAAM,CAAC,OAAO,EAAE,MAAM,CAAC,IAAI,IAAI,EAAE,EAAE;QACrD,GAAG,EAAE,EAAE,GAAG,OAAO,CAAC,GAAG,EAAE,GAAG,CAAC,MAAM,CAAC,GAAG,IAAI,EAAE,CAAC,EAAE;QAC9C,GAAG,CAAC,MAAM,CAAC,GAAG,CAAC,CAAC,CAAC,EAAE,GAAG,EAAE,MAAM,CAAC,GAAG,EAAE,CAAC,CAAC,CAAC,EAAE,CAAC;KAC3C,CAAC,CAAC;IACH,MAAM,QAAQ,GAAyC,EAAE,CAAC;IAC1D,IAAI,MAAM,GAAG,EAAE,CAAC;IAChB,KAAK,CAAC,MAAM,CAAC,EAAE,CAAC,MAAM,EAAE,CAAC,KAAa,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C,OAAkB;IAC7C,IAAI,KAAK,CAAC,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC;QAC3B,OAAO,OAAO;aACX,GAAG,CAAC,CAAC,IAAI,EAAE,EAAE,CAAC,CAAC,IAAI,IAAI,OAAO,IAAI,KAAK,QAAQ,IAAI,MAAM,IAAI,IAAI,CAAC,CAAC,CAAC,MAAM,CAAE,IAAmB,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,IAAI,CAAC,SAAS,CAAC,IAAI,CAAC,CAAC,CAAC;aAC9H,IAAI,CAAC,IAAI,CAAC,CAAC;IAChB,CAAC;IACD,OAAO,IAAI,CAAC,SAAS,CAAC,OAAO,CAAC,CAAC;AACjC,CAAC"}
|
package/dist/memory.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
import type { JsonObject } from "./types.js";
|
|
2
|
+
export type MemoryKind = "fact" | "episode" | "procedure" | "preference" | "artifact";
|
|
3
|
+
export type MemoryRecord = {
|
|
4
|
+
id: string;
|
|
5
|
+
kind: MemoryKind;
|
|
6
|
+
content: string;
|
|
7
|
+
source?: string;
|
|
8
|
+
tags: string[];
|
|
9
|
+
confidence: number;
|
|
10
|
+
importance: number;
|
|
11
|
+
metadata: JsonObject;
|
|
12
|
+
createdAt: string;
|
|
13
|
+
};
|
|
14
|
+
export type MemorySearchResult = MemoryRecord & {
|
|
15
|
+
score: number;
|
|
16
|
+
};
|
|
17
|
+
export declare class MemoryStore {
|
|
18
|
+
private readonly records;
|
|
19
|
+
write(input: {
|
|
20
|
+
kind: MemoryKind;
|
|
21
|
+
content: string;
|
|
22
|
+
source?: string;
|
|
23
|
+
tags?: string[];
|
|
24
|
+
confidence?: number;
|
|
25
|
+
importance?: number;
|
|
26
|
+
metadata?: JsonObject;
|
|
27
|
+
}): MemoryRecord;
|
|
28
|
+
get(id: string): MemoryRecord | undefined;
|
|
29
|
+
list(): MemoryRecord[];
|
|
30
|
+
search(query: string, limit?: number): MemorySearchResult[];
|
|
31
|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D"}
|
package/dist/memory.js
ADDED
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
export class MemoryStore {
|
|
2
|
+
records = new Map();
|
|
3
|
+
write(input) {
|
|
4
|
+
const record = {
|
|
5
|
+
id: crypto.randomUUID(),
|
|
6
|
+
kind: input.kind,
|
|
7
|
+
content: input.content,
|
|
8
|
+
...(input.source ? { source: input.source } : {}),
|
|
9
|
+
tags: input.tags ?? [],
|
|
10
|
+
confidence: input.confidence ?? 0.8,
|
|
11
|
+
importance: input.importance ?? 0.5,
|
|
12
|
+
metadata: input.metadata ?? {},
|
|
13
|
+
createdAt: new Date().toISOString()
|
|
14
|
+
};
|
|
15
|
+
this.records.set(record.id, record);
|
|
16
|
+
return record;
|
|
17
|
+
}
|
|
18
|
+
get(id) {
|
|
19
|
+
return this.records.get(id);
|
|
20
|
+
}
|
|
21
|
+
list() {
|
|
22
|
+
return [...this.records.values()];
|
|
23
|
+
}
|
|
24
|
+
search(query, limit = 10) {
|
|
25
|
+
const terms = tokenize(query);
|
|
26
|
+
return this.list()
|
|
27
|
+
.map((record) => ({ ...record, score: scoreMemory(record, terms) }))
|
|
28
|
+
.filter((record) => record.score > 0)
|
|
29
|
+
.sort((a, b) => b.score - a.score || b.importance - a.importance)
|
|
30
|
+
.slice(0, limit);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
function tokenize(value) {
|
|
34
|
+
return value.toLowerCase().split(/[^a-z0-9_áéíóúñ]+/i).filter(Boolean);
|
|
35
|
+
}
|
|
36
|
+
function scoreMemory(record, terms) {
|
|
37
|
+
const haystack = `${record.content} ${record.tags.join(" ")} ${record.source ?? ""}`.toLowerCase();
|
|
38
|
+
return terms.reduce((score, term) => score + (haystack.includes(term) ? 1 : 0), 0);
|
|
39
|
+
}
|
|
40
|
+
//# sourceMappingURL=memory.js.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.js","sourceRoot":"","sources":["../src/memory.ts"],"names":[],"mappings":"AAoB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,CAAC,MAAM,EAAE,EAAE,CAAC,MAAM,CAAC,KAAK,GAAG,CAAC,CAAC;aACpC,IAAI,CAAC,CAAC,CAAC,EAAE,CAAC,EAAE,EAAE,CAAC,CAAC,CAAC,KAAK,GAAG,CAAC,CAAC,KAAK,IAAI,CAAC,CAAC,UAAU,GAAG,CAAC,CAAC,UAAU,CAAC;aAChE,KAAK,CAAC,CAAC,EAAE,KAAK,CAAC,CAAC;IACrB,CAAC;CACF;AAED,SAAS,QAAQ,CAAC,KAAa;IAC7B,OAAO,KAAK,CAAC,WAAW,EAAE,CAAC,KAAK,CAAC,oBAAoB,CAAC,CAAC,MAAM,CAAC,OAAO,CAAC,CAAC;AACzE,CAAC;AAED,SAAS,WAAW,CAAC,MAAoB,EAAE,KAAe;IACxD,MAAM,QAAQ,GAAG,GAAG,MAAM,CAAC,OAAO,IAAI,MAAM,CAAC,IAAI,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,MAAM,CAAC,MAAM,IAAI,EAAE,EAAE,CAAC,WAAW,EAAE,CAAC;IACnG,OAAO,KAAK,CAAC,MAAM,CAAC,CAAC,KAAK,EAAE,IAAI,EAAE,EAAE,CAAC,KAAK,GAAG,CAAC,QAAQ,CAAC,QAAQ,CAAC,IAAI,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,EAAE,CAAC,CAAC,CAAC;AACrF,CAAC"}
|
|
@@ -0,0 +1,106 @@
|
|
|
1
|
+
import { EventBus } from "./event-bus.js";
|
|
2
|
+
import { MemoryStore } from "./memory.js";
|
|
3
|
+
import { ToolRegistry, type ToolExecutor } from "./tool-registry.js";
|
|
4
|
+
import type { JsonObject, JsonValue } from "./types.js";
|
|
5
|
+
export type MissionStatus = "accepted" | "planning" | "executing" | "verifying" | "completed" | "blocked" | "failed" | "cancelled";
|
|
6
|
+
export type MissionStepType = "tool" | "llm" | "verify";
|
|
7
|
+
export type ErrorClass = "transient" | "permanent" | "rate_limited" | "approval_required";
|
|
8
|
+
export type MissionPlanStep = {
|
|
9
|
+
id?: string;
|
|
10
|
+
title: string;
|
|
11
|
+
description: string;
|
|
12
|
+
toolHint: string | null;
|
|
13
|
+
stepType: MissionStepType;
|
|
14
|
+
dependsOn: number[];
|
|
15
|
+
input?: JsonObject;
|
|
16
|
+
maxRetries?: number;
|
|
17
|
+
};
|
|
18
|
+
export type MissionStep = Required<Omit<MissionPlanStep, "id" | "input" | "maxRetries">> & {
|
|
19
|
+
id: string;
|
|
20
|
+
input: JsonObject;
|
|
21
|
+
status: "pending" | "running" | "completed" | "failed" | "skipped";
|
|
22
|
+
output: JsonValue | null;
|
|
23
|
+
evidence: JsonValue[];
|
|
24
|
+
error: string | null;
|
|
25
|
+
retryCount: number;
|
|
26
|
+
maxRetries: number;
|
|
27
|
+
};
|
|
28
|
+
export type MissionRecord = {
|
|
29
|
+
id: string;
|
|
30
|
+
title: string;
|
|
31
|
+
goal: string;
|
|
32
|
+
status: MissionStatus;
|
|
33
|
+
steps: MissionStep[];
|
|
34
|
+
evidence: JsonValue[];
|
|
35
|
+
result: string | null;
|
|
36
|
+
error: string | null;
|
|
37
|
+
replannedCount: number;
|
|
38
|
+
createdAt: string;
|
|
39
|
+
updatedAt: string;
|
|
40
|
+
};
|
|
41
|
+
export type MissionPlanner = {
|
|
42
|
+
createPlan(input: {
|
|
43
|
+
mission: MissionRecord;
|
|
44
|
+
memories: JsonValue[];
|
|
45
|
+
}): Promise<MissionPlanStep[]>;
|
|
46
|
+
replan?(input: {
|
|
47
|
+
mission: MissionRecord;
|
|
48
|
+
failedStep: MissionStep;
|
|
49
|
+
error: string;
|
|
50
|
+
}): Promise<MissionPlanStep[]>;
|
|
51
|
+
};
|
|
52
|
+
export type MissionVerifier = {
|
|
53
|
+
verify(input: {
|
|
54
|
+
mission: MissionRecord;
|
|
55
|
+
steps: MissionStep[];
|
|
56
|
+
}): Promise<{
|
|
57
|
+
ok: boolean;
|
|
58
|
+
reason: string;
|
|
59
|
+
}>;
|
|
60
|
+
};
|
|
61
|
+
export declare class RetryEngine {
|
|
62
|
+
classifyError(error: unknown): ErrorClass;
|
|
63
|
+
shouldRetry(error: unknown, attempt: number, maxRetries: number): boolean;
|
|
64
|
+
}
|
|
65
|
+
export declare class DefaultMissionPlanner implements MissionPlanner {
|
|
66
|
+
createPlan(input: {
|
|
67
|
+
mission: MissionRecord;
|
|
68
|
+
memories: JsonValue[];
|
|
69
|
+
}): Promise<MissionPlanStep[]>;
|
|
70
|
+
}
|
|
71
|
+
export declare class DefaultMissionVerifier implements MissionVerifier {
|
|
72
|
+
verify(input: {
|
|
73
|
+
mission: MissionRecord;
|
|
74
|
+
steps: MissionStep[];
|
|
75
|
+
}): Promise<{
|
|
76
|
+
ok: boolean;
|
|
77
|
+
reason: string;
|
|
78
|
+
}>;
|
|
79
|
+
}
|
|
80
|
+
export type MissionRuntimeOptions = {
|
|
81
|
+
planner?: MissionPlanner;
|
|
82
|
+
verifier?: MissionVerifier;
|
|
83
|
+
tools?: ToolRegistry;
|
|
84
|
+
toolExecutor?: ToolExecutor;
|
|
85
|
+
memory?: MemoryStore;
|
|
86
|
+
events?: EventBus;
|
|
87
|
+
};
|
|
88
|
+
export declare class MissionRuntime {
|
|
89
|
+
readonly retry: RetryEngine;
|
|
90
|
+
readonly planner: MissionPlanner;
|
|
91
|
+
readonly verifier: MissionVerifier;
|
|
92
|
+
readonly tools: ToolRegistry;
|
|
93
|
+
readonly memory: MemoryStore;
|
|
94
|
+
readonly events: EventBus;
|
|
95
|
+
private readonly missions;
|
|
96
|
+
constructor(options?: MissionRuntimeOptions);
|
|
97
|
+
private readonly toolExecutor;
|
|
98
|
+
createMission(goal: string): MissionRecord;
|
|
99
|
+
getMission(id: string): MissionRecord;
|
|
100
|
+
runMission(id: string): Promise<MissionRecord>;
|
|
101
|
+
private planMission;
|
|
102
|
+
private executeStep;
|
|
103
|
+
private runStepAction;
|
|
104
|
+
private patch;
|
|
105
|
+
}
|
|
106
|
+
//# sourceMappingURL=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,MAAM,GAAG,aAAa;IAoB1C,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,aAAa;IAM/B,UAAU,CAAC,EAAE,EAAE,MAAM,GAAG,OAAO,CAAC,aAAa,CAAC;YA4BtC,WAAW;YAqBX,WAAW;YAmBX,aAAa;IAiB3B,OAAO,CAAC,KAAK;CAGd"}
|
|
@@ -0,0 +1,196 @@
|
|
|
1
|
+
import { EventBus } from "./event-bus.js";
|
|
2
|
+
import { MemoryStore } from "./memory.js";
|
|
3
|
+
import { ToolRegistry } from "./tool-registry.js";
|
|
4
|
+
export class RetryEngine {
|
|
5
|
+
classifyError(error) {
|
|
6
|
+
const message = String(error instanceof Error ? error.message : error).toLowerCase();
|
|
7
|
+
if (message.includes("approval") || message.includes("denied") || message.includes("permission"))
|
|
8
|
+
return "approval_required";
|
|
9
|
+
if (message.includes("429") || message.includes("rate limit"))
|
|
10
|
+
return "rate_limited";
|
|
11
|
+
if (["timeout", "econnreset", "enotfound", "econnrefused", "network", "503", "502"].some((token) => message.includes(token)))
|
|
12
|
+
return "transient";
|
|
13
|
+
return "permanent";
|
|
14
|
+
}
|
|
15
|
+
shouldRetry(error, attempt, maxRetries) {
|
|
16
|
+
const kind = this.classifyError(error);
|
|
17
|
+
return attempt < maxRetries && (kind === "transient" || kind === "rate_limited");
|
|
18
|
+
}
|
|
19
|
+
}
|
|
20
|
+
export class DefaultMissionPlanner {
|
|
21
|
+
async createPlan(input) {
|
|
22
|
+
const hasTool = input.memories.length > 0;
|
|
23
|
+
return [
|
|
24
|
+
{
|
|
25
|
+
title: "Gather context",
|
|
26
|
+
description: "Collect relevant context before acting.",
|
|
27
|
+
toolHint: hasTool ? "memory.search" : null,
|
|
28
|
+
stepType: hasTool ? "tool" : "llm",
|
|
29
|
+
dependsOn: []
|
|
30
|
+
},
|
|
31
|
+
{
|
|
32
|
+
title: "Execute goal",
|
|
33
|
+
description: input.mission.goal,
|
|
34
|
+
toolHint: null,
|
|
35
|
+
stepType: "llm",
|
|
36
|
+
dependsOn: [0]
|
|
37
|
+
},
|
|
38
|
+
{
|
|
39
|
+
title: "Verify result",
|
|
40
|
+
description: "Confirm the mission produced evidence.",
|
|
41
|
+
toolHint: null,
|
|
42
|
+
stepType: "verify",
|
|
43
|
+
dependsOn: [1]
|
|
44
|
+
}
|
|
45
|
+
];
|
|
46
|
+
}
|
|
47
|
+
}
|
|
48
|
+
export class DefaultMissionVerifier {
|
|
49
|
+
async verify(input) {
|
|
50
|
+
const actionable = input.steps.filter((step) => step.status !== "skipped");
|
|
51
|
+
const completed = actionable.filter((step) => step.status === "completed");
|
|
52
|
+
if (completed.length < actionable.length)
|
|
53
|
+
return { ok: false, reason: `Only ${completed.length}/${actionable.length} steps completed.` };
|
|
54
|
+
if (!input.mission.evidence.length)
|
|
55
|
+
return { ok: false, reason: "Mission has no evidence recorded." };
|
|
56
|
+
return { ok: true, reason: `${completed.length} steps completed with evidence.` };
|
|
57
|
+
}
|
|
58
|
+
}
|
|
59
|
+
export class MissionRuntime {
|
|
60
|
+
retry = new RetryEngine();
|
|
61
|
+
planner;
|
|
62
|
+
verifier;
|
|
63
|
+
tools;
|
|
64
|
+
memory;
|
|
65
|
+
events;
|
|
66
|
+
missions = new Map();
|
|
67
|
+
constructor(options = {}) {
|
|
68
|
+
this.planner = options.planner ?? new DefaultMissionPlanner();
|
|
69
|
+
this.verifier = options.verifier ?? new DefaultMissionVerifier();
|
|
70
|
+
this.tools = options.tools ?? new ToolRegistry();
|
|
71
|
+
this.memory = options.memory ?? new MemoryStore();
|
|
72
|
+
this.events = options.events ?? new EventBus();
|
|
73
|
+
this.toolExecutor = options.toolExecutor;
|
|
74
|
+
}
|
|
75
|
+
toolExecutor;
|
|
76
|
+
createMission(goal) {
|
|
77
|
+
const now = new Date().toISOString();
|
|
78
|
+
const mission = {
|
|
79
|
+
id: crypto.randomUUID(),
|
|
80
|
+
title: goal.length > 72 ? `${goal.slice(0, 69)}...` : goal,
|
|
81
|
+
goal,
|
|
82
|
+
status: "accepted",
|
|
83
|
+
steps: [],
|
|
84
|
+
evidence: [],
|
|
85
|
+
result: null,
|
|
86
|
+
error: null,
|
|
87
|
+
replannedCount: 0,
|
|
88
|
+
createdAt: now,
|
|
89
|
+
updatedAt: now
|
|
90
|
+
};
|
|
91
|
+
this.missions.set(mission.id, mission);
|
|
92
|
+
void this.events.publish({ type: "mission.created", source: "mission-runtime", missionId: mission.id, payload: { goal } });
|
|
93
|
+
return mission;
|
|
94
|
+
}
|
|
95
|
+
getMission(id) {
|
|
96
|
+
const mission = this.missions.get(id);
|
|
97
|
+
if (!mission)
|
|
98
|
+
throw new Error(`Mission not found: ${id}`);
|
|
99
|
+
return mission;
|
|
100
|
+
}
|
|
101
|
+
async runMission(id) {
|
|
102
|
+
const mission = this.getMission(id);
|
|
103
|
+
if (mission.status === "completed" || mission.status === "cancelled")
|
|
104
|
+
return mission;
|
|
105
|
+
if (!mission.steps.length)
|
|
106
|
+
await this.planMission(mission);
|
|
107
|
+
this.patch(mission, { status: "executing" });
|
|
108
|
+
for (const step of mission.steps) {
|
|
109
|
+
if (step.status === "completed" || step.status === "skipped")
|
|
110
|
+
continue;
|
|
111
|
+
const ok = await this.executeStep(mission, step);
|
|
112
|
+
if (!ok) {
|
|
113
|
+
this.patch(mission, { status: "blocked", error: step.error ?? "Step failed." });
|
|
114
|
+
return mission;
|
|
115
|
+
}
|
|
116
|
+
}
|
|
117
|
+
this.patch(mission, { status: "verifying" });
|
|
118
|
+
const verified = await this.verifier.verify({ mission, steps: mission.steps });
|
|
119
|
+
if (!verified.ok) {
|
|
120
|
+
this.patch(mission, { status: "blocked", error: verified.reason });
|
|
121
|
+
return mission;
|
|
122
|
+
}
|
|
123
|
+
const result = `Mission completed: ${mission.title}. ${mission.evidence.length} evidence item(s).`;
|
|
124
|
+
this.memory.write({ kind: "episode", content: result, source: `mission:${mission.id}`, tags: ["mission", "completed"] });
|
|
125
|
+
this.patch(mission, { status: "completed", result, error: null });
|
|
126
|
+
await this.events.publish({ type: "mission.completed", source: "mission-runtime", missionId: mission.id, payload: { result } });
|
|
127
|
+
return mission;
|
|
128
|
+
}
|
|
129
|
+
async planMission(mission) {
|
|
130
|
+
this.patch(mission, { status: "planning" });
|
|
131
|
+
const memories = this.memory.search(mission.goal, 5);
|
|
132
|
+
mission.steps = (await this.planner.createPlan({ mission, memories })).map((step, index) => ({
|
|
133
|
+
id: step.id ?? crypto.randomUUID(),
|
|
134
|
+
title: step.title,
|
|
135
|
+
description: step.description,
|
|
136
|
+
toolHint: step.toolHint,
|
|
137
|
+
stepType: step.stepType,
|
|
138
|
+
dependsOn: step.dependsOn,
|
|
139
|
+
input: step.input ?? {},
|
|
140
|
+
status: "pending",
|
|
141
|
+
output: null,
|
|
142
|
+
evidence: [],
|
|
143
|
+
error: null,
|
|
144
|
+
retryCount: 0,
|
|
145
|
+
maxRetries: step.maxRetries ?? 3
|
|
146
|
+
}));
|
|
147
|
+
await this.events.publish({ type: "mission.planned", source: "mission-runtime", missionId: mission.id, payload: { steps: mission.steps.map((step) => step.title) } });
|
|
148
|
+
}
|
|
149
|
+
async executeStep(mission, step) {
|
|
150
|
+
step.status = "running";
|
|
151
|
+
await this.events.publish({ type: "mission.step.started", source: "mission-runtime", missionId: mission.id, stepId: step.id, payload: { title: step.title } });
|
|
152
|
+
try {
|
|
153
|
+
const output = await this.runStepAction(mission, step);
|
|
154
|
+
step.output = output;
|
|
155
|
+
step.evidence = [{ step: step.title, output }];
|
|
156
|
+
step.status = "completed";
|
|
157
|
+
mission.evidence.push(...step.evidence);
|
|
158
|
+
await this.events.publish({ type: "mission.step.completed", source: "mission-runtime", missionId: mission.id, stepId: step.id, payload: { output } });
|
|
159
|
+
return true;
|
|
160
|
+
}
|
|
161
|
+
catch (error) {
|
|
162
|
+
step.error = error instanceof Error ? error.message : String(error);
|
|
163
|
+
step.status = "failed";
|
|
164
|
+
await this.events.publish({ type: "mission.step.failed", source: "mission-runtime", missionId: mission.id, stepId: step.id, payload: { error: step.error } });
|
|
165
|
+
return false;
|
|
166
|
+
}
|
|
167
|
+
}
|
|
168
|
+
async runStepAction(mission, step) {
|
|
169
|
+
if (step.stepType === "verify") {
|
|
170
|
+
if (!mission.evidence.length)
|
|
171
|
+
throw new Error("Verification failed: no evidence recorded.");
|
|
172
|
+
return { verified: true, evidenceCount: mission.evidence.length };
|
|
173
|
+
}
|
|
174
|
+
if (step.stepType === "llm" || !step.toolHint)
|
|
175
|
+
return { note: step.description, title: step.title };
|
|
176
|
+
if (step.toolHint === "memory.search")
|
|
177
|
+
return { results: this.memory.search(mission.goal, 5) };
|
|
178
|
+
if (step.toolHint === "memory.write")
|
|
179
|
+
return this.memory.write({ kind: "episode", content: mission.goal, source: `mission:${mission.id}` });
|
|
180
|
+
if (!this.toolExecutor)
|
|
181
|
+
throw new Error(`No tool executor configured for ${step.toolHint}.`);
|
|
182
|
+
const tool = this.tools.list().find((candidate) => candidate.id === step.toolHint || candidate.name === step.toolHint);
|
|
183
|
+
if (!tool)
|
|
184
|
+
throw new Error(`Tool not registered: ${step.toolHint}`);
|
|
185
|
+
const result = await this.toolExecutor.execute(tool.id, step.input, { taskId: mission.id, runId: mission.id, stepId: step.id });
|
|
186
|
+
if (result.status === "ok")
|
|
187
|
+
return result.output;
|
|
188
|
+
if (result.status === "pending_approval")
|
|
189
|
+
throw new Error(`Approval required: ${result.reason}`);
|
|
190
|
+
throw new Error(result.error);
|
|
191
|
+
}
|
|
192
|
+
patch(mission, patch) {
|
|
193
|
+
Object.assign(mission, patch, { updatedAt: new Date().toISOString() });
|
|
194
|
+
}
|
|
195
|
+
}
|
|
196
|
+
//# sourceMappingURL=mission-runtime.js.map
|