@deepstrike/wasm 0.1.11 → 0.1.14

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/agent.d.ts DELETED
@@ -1,57 +0,0 @@
1
- import type { LLMProvider, StreamEvent } from "./types.js";
2
- import type { RegisteredTool } from "./tools/index.js";
3
- import type { KnowledgeSource } from "./knowledge/index.js";
4
- import type { SignalSource } from "./signals/index.js";
5
- import type { DreamStore } from "./memory/index.js";
6
- import { Governance } from "./governance.js";
7
- export interface SkillMetadata {
8
- name: string;
9
- description: string;
10
- whenToUse?: string;
11
- allowedTools?: string[];
12
- effort?: number;
13
- estimatedTokens?: number;
14
- }
15
- export interface AgentOptions {
16
- maxTokens: number;
17
- maxTurns?: number;
18
- timeoutMs?: number;
19
- extensions?: Record<string, unknown>;
20
- /**
21
- * System-level instructions prepended to every context render.
22
- * Passed to the kernel's `system` partition before the first LLM call.
23
- */
24
- systemPrompt?: string;
25
- /**
26
- * Long-term memory snippets pre-seeded into the context before the first LLM call.
27
- * Each string is pushed to the kernel's `memory` partition.
28
- */
29
- initialMemory?: string[];
30
- skillDir?: string;
31
- knowledgeSource?: KnowledgeSource;
32
- signalSource?: SignalSource;
33
- dreamStore?: DreamStore;
34
- agentId?: string;
35
- governance?: Governance;
36
- /** Host-provided skill content map (name → markdown body). WASM has no fs access. */
37
- skillContentMap?: Map<string, string>;
38
- }
39
- export declare class Agent {
40
- private readonly provider;
41
- private readonly options;
42
- private tools;
43
- private blockedTools;
44
- private extensions;
45
- private interrupted;
46
- private pendingInterrupt;
47
- private _pendingSkills;
48
- constructor(provider: LLMProvider, options: AgentOptions);
49
- register(...tools: RegisteredTool[]): this;
50
- unregister(name: string): this;
51
- blockTool(name: string): this;
52
- interrupt(): void;
53
- run(goal: string, criteria?: string[], extensions?: Record<string, unknown>): Promise<string>;
54
- runStreaming(goal: string, criteria?: string[], extensions?: Record<string, unknown>): AsyncIterable<StreamEvent>;
55
- /** Register available skills from host-provided metadata (WASM has no fs access). */
56
- setAvailableSkills(skills: SkillMetadata[]): void;
57
- }
package/dist/agent.js DELETED
@@ -1,244 +0,0 @@
1
- import { executeTools } from "./tools/index.js";
2
- async function loadKernel() {
3
- return import("@deepstrike/wasm-kernel");
4
- }
5
- export class Agent {
6
- provider;
7
- options;
8
- tools = new Map();
9
- blockedTools = new Set();
10
- extensions;
11
- interrupted = false;
12
- pendingInterrupt = false;
13
- _pendingSkills = [];
14
- constructor(provider, options) {
15
- this.provider = provider;
16
- this.options = options;
17
- this.extensions = options.extensions ?? {};
18
- }
19
- register(...tools) {
20
- for (const t of tools)
21
- this.tools.set(t.schema.name, t);
22
- return this;
23
- }
24
- unregister(name) { this.tools.delete(name); return this; }
25
- blockTool(name) { this.blockedTools.add(name); return this; }
26
- interrupt() { this.interrupted = true; }
27
- async run(goal, criteria, extensions) {
28
- let text = "";
29
- for await (const evt of this.runStreaming(goal, criteria, extensions)) {
30
- if (evt.type === "text_delta")
31
- text += evt.delta;
32
- }
33
- return text;
34
- }
35
- async *runStreaming(goal, criteria, extensions) {
36
- this.interrupted = false;
37
- this.pendingInterrupt = false;
38
- if (this.options.knowledgeSource) {
39
- await this.options.knowledgeSource.init();
40
- }
41
- const kernel = await loadKernel();
42
- if (this.options.governance)
43
- this.options.governance._attach(kernel);
44
- const ext = { ...this.extensions, ...(extensions ?? {}) };
45
- const sm = new kernel.LoopStateMachine({
46
- maxTokens: this.options.maxTokens,
47
- maxTurns: this.options.maxTurns ?? 25,
48
- timeoutMs: this.options.timeoutMs,
49
- });
50
- sm.setTools(Array.from(this.tools.values()).map(t => t.schema));
51
- if (this.options.systemPrompt) {
52
- const tokens = Math.max(1, Math.ceil(this.options.systemPrompt.length / 4));
53
- sm.addSystemMessage(this.options.systemPrompt, tokens);
54
- }
55
- for (const mem of this.options.initialMemory ?? []) {
56
- sm.addMemoryMessage(mem, Math.max(1, Math.ceil(mem.length / 4)));
57
- }
58
- if (this._pendingSkills.length > 0) {
59
- sm.setAvailableSkills(this._pendingSkills);
60
- }
61
- if (this.options.dreamStore && this.options.agentId)
62
- sm.setMemoryEnabled(true);
63
- if (this.options.knowledgeSource)
64
- sm.setKnowledgeEnabled(true);
65
- const router = new kernel.SignalRouter(256);
66
- let action = sm.start({ goal, criteria: criteria ?? [] });
67
- let finalText = "";
68
- const sessionStart = Date.now();
69
- const sessionMsgs = [{ role: "user", content: goal }];
70
- while (!sm.isTerminal()) {
71
- if (this.interrupted) {
72
- action = sm.feedTimeout();
73
- break;
74
- }
75
- if (this.pendingInterrupt) {
76
- this.pendingInterrupt = false;
77
- action = sm.feedTimeout();
78
- break;
79
- }
80
- if (this.options.signalSource) {
81
- const sig = await this.options.signalSource.nextSignal();
82
- if (sig) {
83
- const kernelSig = {
84
- id: crypto.randomUUID(),
85
- source: sig.source,
86
- signalType: sig.signalType,
87
- urgency: sig.urgency,
88
- summary: String(sig.payload?.goal ?? sig.signalType),
89
- payload: JSON.stringify(sig.payload ?? {}),
90
- dedupeKey: sig.dedupeKey,
91
- timestampMs: Date.now(),
92
- };
93
- const disposition = router.ingest(kernelSig, action.kind === "execute_tools");
94
- if (disposition === "interrupt_now") {
95
- action = sm.feedTimeout();
96
- break;
97
- }
98
- if (disposition === "interrupt")
99
- this.pendingInterrupt = true;
100
- }
101
- }
102
- sm.takeObservations();
103
- if (action.kind === "call_llm") {
104
- finalText = "";
105
- const finalToolCalls = [];
106
- const messages = (action.messages ?? []);
107
- const tools = (action.tools ?? []);
108
- try {
109
- for await (const evt of this.provider.stream(messages, tools, Object.keys(ext).length ? ext : undefined)) {
110
- yield evt;
111
- if (evt.type === "text_delta")
112
- finalText += evt.delta;
113
- else if (evt.type === "tool_call") {
114
- const tc = evt;
115
- finalToolCalls.push({ id: tc.id, name: tc.name, arguments: JSON.stringify(tc.arguments) });
116
- }
117
- }
118
- }
119
- catch (err) {
120
- yield { type: "error", message: String(err) };
121
- action = sm.feedTimeout();
122
- break;
123
- }
124
- action = sm.feedLlmResponse({ role: "assistant", content: finalText, toolCalls: finalToolCalls });
125
- sessionMsgs.push({ role: "assistant", content: finalText, toolCalls: finalToolCalls });
126
- }
127
- else if (action.kind === "execute_tools") {
128
- const allCalls = (action.calls ?? []);
129
- // Governance check
130
- const permittedCalls = [];
131
- for (const c of allCalls) {
132
- if (this.blockedTools.has(c.name)) {
133
- yield { type: "error", message: `tool blocked: ${c.name}` };
134
- continue;
135
- }
136
- if (this.options.governance) {
137
- this.options.governance.setTime(Date.now());
138
- const verdict = this.options.governance.evaluate(c.name, c.arguments);
139
- if (verdict.kind === "deny") {
140
- yield { type: "error", message: `permission denied: ${c.name} — ${verdict.reason}` };
141
- continue;
142
- }
143
- if (verdict.kind === "ask_user") {
144
- yield { type: "permission_request", callId: c.id, toolName: c.name, arguments: c.arguments, reason: verdict.reason };
145
- continue;
146
- }
147
- }
148
- permittedCalls.push(c);
149
- }
150
- const skillCalls = permittedCalls.filter(c => c.name === "skill");
151
- const memoryCalls = permittedCalls.filter(c => c.name === "memory");
152
- const knowledgeCalls = permittedCalls.filter(c => c.name === "knowledge");
153
- const regularCalls = permittedCalls.filter(c => !["skill", "memory", "knowledge"].includes(c.name));
154
- // skill: WASM host must provide skill content via a registered tool or extension
155
- const skillContentMap = this.options.skillContentMap ?? new Map();
156
- const skillResults = skillCalls.map(c => {
157
- const args = tryParseJson(c.arguments);
158
- const name = String(args?.name ?? "");
159
- const content = skillContentMap.get(name);
160
- const output = content ?? `Skill "${name}" not found.`;
161
- return { callId: c.id, output, isError: content === undefined };
162
- });
163
- const memoryResults = [];
164
- if (this.options.dreamStore && this.options.agentId) {
165
- for (const c of memoryCalls) {
166
- const args = tryParseJson(c.arguments);
167
- const query = String(args?.query ?? "");
168
- const topK = typeof args?.top_k === "number" ? args.top_k : 5;
169
- const entries = await this.options.dreamStore.search(this.options.agentId, query, topK);
170
- const output = entries.length ? entries.map(e => `[score=${e.score.toFixed(3)}] ${e.text}`).join("\n---\n") : "No relevant memories found.";
171
- yield { type: "tool_result", callId: c.id, name: c.name, content: output, isError: false };
172
- memoryResults.push({ callId: c.id, output, isError: false });
173
- }
174
- }
175
- else {
176
- for (const c of memoryCalls)
177
- memoryResults.push({ callId: c.id, output: "Memory retrieval not configured.", isError: true });
178
- }
179
- const knowledgeResults = [];
180
- if (this.options.knowledgeSource) {
181
- for (const c of knowledgeCalls) {
182
- const args = tryParseJson(c.arguments);
183
- const query = String(args?.query ?? "");
184
- const topK = typeof args?.top_k === "number" ? args.top_k : 5;
185
- const snippets = await this.options.knowledgeSource.retrieve(query, topK);
186
- const output = snippets.length ? snippets.join("\n---\n") : "No relevant knowledge found.";
187
- yield { type: "tool_result", callId: c.id, name: c.name, content: output, isError: false };
188
- knowledgeResults.push({ callId: c.id, output, isError: false });
189
- }
190
- }
191
- else {
192
- for (const c of knowledgeCalls)
193
- knowledgeResults.push({ callId: c.id, output: "Knowledge source not configured.", isError: true });
194
- }
195
- const results = await executeTools(regularCalls, this.tools);
196
- for (const r of results) {
197
- const name = regularCalls.find(c => c.id === r.callId)?.name ?? "";
198
- yield { type: "tool_result", callId: r.callId, name, content: r.output, isError: r.isError };
199
- }
200
- action = sm.feedToolResults([
201
- ...skillResults,
202
- ...memoryResults,
203
- ...knowledgeResults,
204
- ...results.map(r => ({ callId: r.callId, output: r.output, isError: r.isError })),
205
- ]);
206
- }
207
- else if (action.kind === "done") {
208
- break;
209
- }
210
- }
211
- const result = action.result;
212
- if (this.options.dreamStore && this.options.agentId && sessionMsgs.length > 1) {
213
- try {
214
- await this.options.dreamStore.saveSession({
215
- sessionId: crypto.randomUUID(),
216
- agentId: this.options.agentId,
217
- messages: sessionMsgs,
218
- metadata: null,
219
- createdAtMs: sessionStart,
220
- updatedAtMs: Date.now(),
221
- });
222
- }
223
- catch { /* session save failure must not surface to caller */ }
224
- }
225
- yield {
226
- type: "done",
227
- iterations: result?.turnsUsed ?? 0,
228
- totalTokens: Number(result?.totalTokensUsed ?? 0),
229
- status: result?.termination ?? "error",
230
- };
231
- }
232
- /** Register available skills from host-provided metadata (WASM has no fs access). */
233
- setAvailableSkills(skills) {
234
- this._pendingSkills = skills;
235
- }
236
- }
237
- function tryParseJson(s) {
238
- try {
239
- return JSON.parse(s);
240
- }
241
- catch {
242
- return null;
243
- }
244
- }