@ethosagent/core 0.1.1 → 0.2.1
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/index.d.ts +77 -8
- package/dist/index.js +365 -75
- package/dist/index.js.map +1 -1
- package/package.json +6 -3
package/dist/index.d.ts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { LLMProvider, ToolRegistry, PersonalityRegistry, MemoryProvider, SessionStore,
|
|
1
|
+
import { HookRegistry, LLMProvider, ToolRegistry, PersonalityRegistry, MemoryProvider, SessionStore, ContextInjector, Storage, Session, SessionFilter, StoredMessage, SessionUsage, SearchResult, MemoryLoadContext, MemoryUpdate, PersonalityConfig, VoidHooks, ModifyingHooks, ClaimingHooks, Tool, ToolFilterOpts, ToolContext, ToolResult } from '@ethosagent/types';
|
|
2
2
|
|
|
3
3
|
type AgentEvent = {
|
|
4
4
|
type: 'text_delta';
|
|
@@ -16,12 +16,22 @@ type AgentEvent = {
|
|
|
16
16
|
toolName: string;
|
|
17
17
|
message: string;
|
|
18
18
|
percent?: number;
|
|
19
|
+
audience: 'internal' | 'user';
|
|
19
20
|
} | {
|
|
20
21
|
type: 'tool_end';
|
|
21
22
|
toolCallId: string;
|
|
22
23
|
toolName: string;
|
|
23
24
|
ok: boolean;
|
|
24
25
|
durationMs: number;
|
|
26
|
+
audience?: 'internal' | 'user';
|
|
27
|
+
/**
|
|
28
|
+
* Tool output body — the success value when `ok`, or the error
|
|
29
|
+
* message when `ok: false`. Optional so consumers that only care
|
|
30
|
+
* about the status (CLI ASCII chips, telemetry) can ignore it.
|
|
31
|
+
* The web chip surfaces this on expand-on-click without a
|
|
32
|
+
* follow-up history fetch.
|
|
33
|
+
*/
|
|
34
|
+
result?: string;
|
|
25
35
|
} | {
|
|
26
36
|
type: 'usage';
|
|
27
37
|
inputTokens: number;
|
|
@@ -47,6 +57,26 @@ interface AgentLoopConfig {
|
|
|
47
57
|
session?: SessionStore;
|
|
48
58
|
hooks?: HookRegistry;
|
|
49
59
|
injectors?: ContextInjector[];
|
|
60
|
+
/**
|
|
61
|
+
* Maps each plugin-registered injector to its plugin id so AgentLoop can
|
|
62
|
+
* gate injectors by personality. Built-in injectors are absent (always fire).
|
|
63
|
+
* Populated by PluginApiImpl.registerInjector(); passed through from wiring.
|
|
64
|
+
*/
|
|
65
|
+
injectorPluginIds?: Map<ContextInjector, string>;
|
|
66
|
+
/**
|
|
67
|
+
* Base Storage instance handed to tools via `ToolContext.storage` after
|
|
68
|
+
* being decorated with a ScopedStorage that enforces the active
|
|
69
|
+
* personality's `fs_reach` allowlist. When unset, ToolContext.storage is
|
|
70
|
+
* left undefined and tools fall back to unrestricted node:fs (legacy
|
|
71
|
+
* behavior — existing CLI/TUI tests don't need a storage instance).
|
|
72
|
+
*/
|
|
73
|
+
storage?: Storage;
|
|
74
|
+
/**
|
|
75
|
+
* Absolute path to ~/.ethos/ used for `${ETHOS_HOME}` substitution in
|
|
76
|
+
* `fs_reach` paths. Defaults to `${HOME}/.ethos`. Required only when
|
|
77
|
+
* `storage` is set.
|
|
78
|
+
*/
|
|
79
|
+
dataDir?: string;
|
|
50
80
|
modelRouting?: Record<string, string>;
|
|
51
81
|
options?: {
|
|
52
82
|
maxIterations?: number;
|
|
@@ -54,12 +84,37 @@ interface AgentLoopConfig {
|
|
|
54
84
|
platform?: string;
|
|
55
85
|
workingDir?: string;
|
|
56
86
|
resultBudgetChars?: number;
|
|
87
|
+
/**
|
|
88
|
+
* Hard cap on total tool calls per user turn (across all LLM iterations).
|
|
89
|
+
* Defaults to 20. Trips a `tool_progress` warning and exits cleanly.
|
|
90
|
+
* See plan/IMPROVEMENT.md P1-3.
|
|
91
|
+
*/
|
|
92
|
+
maxToolCallsPerTurn?: number;
|
|
93
|
+
/**
|
|
94
|
+
* Hard cap on the number of times the same tool name can be invoked in a
|
|
95
|
+
* single turn. Catches the "infinite loop on a single tool" failure mode
|
|
96
|
+
* (e.g. tts loop reported as OpenClaw #67744). Defaults to 5.
|
|
97
|
+
*/
|
|
98
|
+
maxIdenticalToolCalls?: number;
|
|
99
|
+
/**
|
|
100
|
+
* Default streaming watchdog in milliseconds. If no chunk arrives from the
|
|
101
|
+
* LLM within this window, the agent aborts the stream and emits an error.
|
|
102
|
+
* Reset on every chunk. Personalities can override via
|
|
103
|
+
* `personality.streamingTimeoutMs`. Defaults to 120000 (2 minutes).
|
|
104
|
+
*/
|
|
105
|
+
streamingTimeoutMs?: number;
|
|
57
106
|
};
|
|
58
107
|
}
|
|
59
108
|
interface RunOptions {
|
|
60
109
|
sessionKey?: string;
|
|
61
110
|
personalityId?: string;
|
|
62
111
|
abortSignal?: AbortSignal;
|
|
112
|
+
/**
|
|
113
|
+
* Identifier surfaced to tools as `ToolContext.agentId`. Delegation tools
|
|
114
|
+
* use this to thread spawn depth (`depth:N`) into child loops so
|
|
115
|
+
* `MAX_SPAWN_DEPTH` can be enforced across recursive sub-agent calls.
|
|
116
|
+
*/
|
|
117
|
+
agentId?: string;
|
|
63
118
|
}
|
|
64
119
|
declare class AgentLoop {
|
|
65
120
|
private readonly llm;
|
|
@@ -67,18 +122,29 @@ declare class AgentLoop {
|
|
|
67
122
|
private readonly personalities;
|
|
68
123
|
private readonly memory;
|
|
69
124
|
private readonly session;
|
|
70
|
-
|
|
125
|
+
/** Public so surfaces (web, ACP) can register late-binding hooks they own
|
|
126
|
+
* without re-running the whole wiring factory. The CLI/TUI register hooks
|
|
127
|
+
* before construction; web registers an approval hook after createAgentLoop
|
|
128
|
+
* returns. */
|
|
129
|
+
readonly hooks: HookRegistry;
|
|
71
130
|
private readonly injectors;
|
|
131
|
+
private readonly injectorPluginIds;
|
|
72
132
|
private readonly maxIterations;
|
|
73
133
|
private readonly historyLimit;
|
|
74
134
|
private readonly platform;
|
|
75
135
|
private readonly workingDir;
|
|
76
136
|
private readonly resultBudgetChars;
|
|
137
|
+
private readonly maxToolCallsPerTurn;
|
|
138
|
+
private readonly maxIdenticalToolCalls;
|
|
139
|
+
private readonly streamingTimeoutMs;
|
|
77
140
|
private readonly modelRouting;
|
|
141
|
+
private readonly storage?;
|
|
142
|
+
private readonly dataDir?;
|
|
78
143
|
constructor(config: AgentLoopConfig);
|
|
79
144
|
run(text: string, opts?: RunOptions): AsyncGenerator<AgentEvent>;
|
|
80
145
|
private handleChunk;
|
|
81
146
|
private toLLMMessages;
|
|
147
|
+
private buildScopedStorage;
|
|
82
148
|
}
|
|
83
149
|
|
|
84
150
|
declare class InMemorySessionStore implements SessionStore {
|
|
@@ -119,6 +185,7 @@ declare class DefaultPersonalityRegistry implements PersonalityRegistry {
|
|
|
119
185
|
getDefault(): PersonalityConfig;
|
|
120
186
|
setDefault(id: string): void;
|
|
121
187
|
loadFromDirectory(_dir: string): Promise<void>;
|
|
188
|
+
remove(id: string): void;
|
|
122
189
|
}
|
|
123
190
|
|
|
124
191
|
declare class DefaultHookRegistry implements HookRegistry {
|
|
@@ -135,9 +202,9 @@ declare class DefaultHookRegistry implements HookRegistry {
|
|
|
135
202
|
registerClaiming<K extends keyof ClaimingHooks>(name: K, handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>, opts?: {
|
|
136
203
|
pluginId?: string;
|
|
137
204
|
}): () => void;
|
|
138
|
-
fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K]): Promise<void>;
|
|
139
|
-
fireModifying<K extends keyof ModifyingHooks>(name: K, payload: ModifyingHooks[K][0]): Promise<ModifyingHooks[K][1]>;
|
|
140
|
-
fireClaiming<K extends keyof ClaimingHooks>(name: K, payload: ClaimingHooks[K][0]): Promise<ClaimingHooks[K][1]>;
|
|
205
|
+
fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K], allowedPlugins?: string[]): Promise<void>;
|
|
206
|
+
fireModifying<K extends keyof ModifyingHooks>(name: K, payload: ModifyingHooks[K][0], allowedPlugins?: string[]): Promise<ModifyingHooks[K][1]>;
|
|
207
|
+
fireClaiming<K extends keyof ClaimingHooks>(name: K, payload: ClaimingHooks[K][0], allowedPlugins?: string[]): Promise<ClaimingHooks[K][1]>;
|
|
141
208
|
unregisterPlugin(pluginId: string): void;
|
|
142
209
|
private remove;
|
|
143
210
|
}
|
|
@@ -153,13 +220,15 @@ declare class PluginRegistry<T, C = unknown> {
|
|
|
153
220
|
|
|
154
221
|
declare class DefaultToolRegistry implements ToolRegistry {
|
|
155
222
|
private readonly tools;
|
|
156
|
-
register(tool: Tool
|
|
223
|
+
register(tool: Tool, opts?: {
|
|
224
|
+
pluginId?: string;
|
|
225
|
+
}): void;
|
|
157
226
|
registerAll(tools: Tool[]): void;
|
|
158
227
|
unregister(name: string): void;
|
|
159
228
|
get(name: string): Tool | undefined;
|
|
160
229
|
getAvailable(): Tool[];
|
|
161
230
|
getForToolset(toolset: string): Tool[];
|
|
162
|
-
toDefinitions(allowedTools?: string[]): {
|
|
231
|
+
toDefinitions(allowedTools?: string[], filterOpts?: ToolFilterOpts): {
|
|
163
232
|
name: string;
|
|
164
233
|
description: string;
|
|
165
234
|
parameters: Record<string, unknown>;
|
|
@@ -168,7 +237,7 @@ declare class DefaultToolRegistry implements ToolRegistry {
|
|
|
168
237
|
toolCallId: string;
|
|
169
238
|
name: string;
|
|
170
239
|
args: unknown;
|
|
171
|
-
}>, ctx: ToolContext, allowedTools?: string[]): Promise<Array<{
|
|
240
|
+
}>, ctx: ToolContext, allowedTools?: string[], filterOpts?: ToolFilterOpts): Promise<Array<{
|
|
172
241
|
toolCallId: string;
|
|
173
242
|
name: string;
|
|
174
243
|
result: ToolResult;
|
package/dist/index.js
CHANGED
|
@@ -1,3 +1,101 @@
|
|
|
1
|
+
// src/agent-loop.ts
|
|
2
|
+
import { homedir } from "os";
|
|
3
|
+
import { join } from "path";
|
|
4
|
+
|
|
5
|
+
// ../storage-fs/src/fs-storage.ts
|
|
6
|
+
import {
|
|
7
|
+
appendFile,
|
|
8
|
+
mkdir,
|
|
9
|
+
readdir,
|
|
10
|
+
readFile,
|
|
11
|
+
rename,
|
|
12
|
+
rm,
|
|
13
|
+
stat,
|
|
14
|
+
writeFile
|
|
15
|
+
} from "fs/promises";
|
|
16
|
+
|
|
17
|
+
// ../storage-fs/src/in-memory-storage.ts
|
|
18
|
+
import { dirname } from "path";
|
|
19
|
+
|
|
20
|
+
// ../storage-fs/src/scoped-storage.ts
|
|
21
|
+
import {
|
|
22
|
+
BoundaryError
|
|
23
|
+
} from "@ethosagent/types";
|
|
24
|
+
var ScopedStorage = class {
|
|
25
|
+
constructor(inner, scope) {
|
|
26
|
+
this.inner = inner;
|
|
27
|
+
this.readPrefixes = scope.read.map(normalizePrefix);
|
|
28
|
+
this.writePrefixes = scope.write.map(normalizePrefix);
|
|
29
|
+
}
|
|
30
|
+
inner;
|
|
31
|
+
readPrefixes;
|
|
32
|
+
writePrefixes;
|
|
33
|
+
check(path, kind) {
|
|
34
|
+
const allowed = kind === "read" ? this.readPrefixes : this.writePrefixes;
|
|
35
|
+
if (!isPathAllowed(path, allowed)) {
|
|
36
|
+
throw new BoundaryError(kind, path, allowed);
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
async read(path) {
|
|
40
|
+
this.check(path, "read");
|
|
41
|
+
return this.inner.read(path);
|
|
42
|
+
}
|
|
43
|
+
async exists(path) {
|
|
44
|
+
this.check(path, "read");
|
|
45
|
+
return this.inner.exists(path);
|
|
46
|
+
}
|
|
47
|
+
async mtime(path) {
|
|
48
|
+
this.check(path, "read");
|
|
49
|
+
return this.inner.mtime(path);
|
|
50
|
+
}
|
|
51
|
+
async list(dir) {
|
|
52
|
+
this.check(dir, "read");
|
|
53
|
+
return this.inner.list(dir);
|
|
54
|
+
}
|
|
55
|
+
async listEntries(dir) {
|
|
56
|
+
this.check(dir, "read");
|
|
57
|
+
return this.inner.listEntries(dir);
|
|
58
|
+
}
|
|
59
|
+
async write(path, content, opts) {
|
|
60
|
+
this.check(path, "write");
|
|
61
|
+
return this.inner.write(path, content, opts);
|
|
62
|
+
}
|
|
63
|
+
async append(path, content) {
|
|
64
|
+
this.check(path, "write");
|
|
65
|
+
return this.inner.append(path, content);
|
|
66
|
+
}
|
|
67
|
+
async writeAtomic(path, content, opts) {
|
|
68
|
+
this.check(path, "write");
|
|
69
|
+
return this.inner.writeAtomic(path, content, opts);
|
|
70
|
+
}
|
|
71
|
+
async mkdir(dir) {
|
|
72
|
+
this.check(dir, "write");
|
|
73
|
+
return this.inner.mkdir(dir);
|
|
74
|
+
}
|
|
75
|
+
async remove(path, opts) {
|
|
76
|
+
this.check(path, "write");
|
|
77
|
+
return this.inner.remove(path, opts);
|
|
78
|
+
}
|
|
79
|
+
async rename(from, to) {
|
|
80
|
+
this.check(from, "write");
|
|
81
|
+
this.check(to, "write");
|
|
82
|
+
return this.inner.rename(from, to);
|
|
83
|
+
}
|
|
84
|
+
};
|
|
85
|
+
function normalizePrefix(prefix) {
|
|
86
|
+
return prefix;
|
|
87
|
+
}
|
|
88
|
+
function isPathAllowed(path, prefixes) {
|
|
89
|
+
for (const prefix of prefixes) {
|
|
90
|
+
if (path === prefix) return true;
|
|
91
|
+
const withoutSlash = prefix.endsWith("/") ? prefix.slice(0, -1) : prefix;
|
|
92
|
+
if (path === withoutSlash) return true;
|
|
93
|
+
const withSlash = `${withoutSlash}/`;
|
|
94
|
+
if (path.startsWith(withSlash)) return true;
|
|
95
|
+
}
|
|
96
|
+
return false;
|
|
97
|
+
}
|
|
98
|
+
|
|
1
99
|
// src/defaults/in-memory-session.ts
|
|
2
100
|
var InMemorySessionStore = class {
|
|
3
101
|
sessions = /* @__PURE__ */ new Map();
|
|
@@ -150,9 +248,17 @@ var DefaultPersonalityRegistry = class {
|
|
|
150
248
|
}
|
|
151
249
|
async loadFromDirectory(_dir) {
|
|
152
250
|
}
|
|
251
|
+
remove(id) {
|
|
252
|
+
this.personalities.delete(id);
|
|
253
|
+
}
|
|
153
254
|
};
|
|
154
255
|
|
|
155
256
|
// src/hook-registry.ts
|
|
257
|
+
function isAllowed(h, allowedPlugins) {
|
|
258
|
+
if (allowedPlugins === void 0) return true;
|
|
259
|
+
if (!h.pluginId) return true;
|
|
260
|
+
return allowedPlugins.includes(h.pluginId);
|
|
261
|
+
}
|
|
156
262
|
var DefaultHookRegistry = class {
|
|
157
263
|
voidHandlers = /* @__PURE__ */ new Map();
|
|
158
264
|
modifyingHandlers = /* @__PURE__ */ new Map();
|
|
@@ -192,13 +298,18 @@ var DefaultHookRegistry = class {
|
|
|
192
298
|
}
|
|
193
299
|
// Void hooks: all handlers run in parallel via Promise.allSettled.
|
|
194
300
|
// Failures are logged but never propagate (fail-open by default).
|
|
195
|
-
|
|
196
|
-
|
|
301
|
+
// allowedPlugins gates plugin-registered handlers; built-in handlers always fire.
|
|
302
|
+
async fireVoid(name, payload, allowedPlugins) {
|
|
303
|
+
const handlers = (this.voidHandlers.get(name) ?? []).filter(
|
|
304
|
+
(h) => isAllowed(h, allowedPlugins)
|
|
305
|
+
);
|
|
197
306
|
await Promise.allSettled(handlers.map((h) => h.handler(payload)));
|
|
198
307
|
}
|
|
199
308
|
// Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).
|
|
200
|
-
async fireModifying(name, payload) {
|
|
201
|
-
const handlers = this.modifyingHandlers.get(name) ?? []
|
|
309
|
+
async fireModifying(name, payload, allowedPlugins) {
|
|
310
|
+
const handlers = (this.modifyingHandlers.get(name) ?? []).filter(
|
|
311
|
+
(h) => isAllowed(h, allowedPlugins)
|
|
312
|
+
);
|
|
202
313
|
const merged = {};
|
|
203
314
|
for (const h of handlers) {
|
|
204
315
|
try {
|
|
@@ -216,8 +327,10 @@ var DefaultHookRegistry = class {
|
|
|
216
327
|
return merged;
|
|
217
328
|
}
|
|
218
329
|
// Claiming hooks: handlers run sequentially, stop after first { handled: true }.
|
|
219
|
-
async fireClaiming(name, payload) {
|
|
220
|
-
const handlers = this.claimingHandlers.get(name) ?? []
|
|
330
|
+
async fireClaiming(name, payload, allowedPlugins) {
|
|
331
|
+
const handlers = (this.claimingHandlers.get(name) ?? []).filter(
|
|
332
|
+
(h) => isAllowed(h, allowedPlugins)
|
|
333
|
+
);
|
|
221
334
|
for (const h of handlers) {
|
|
222
335
|
try {
|
|
223
336
|
const result = await h.handler(payload);
|
|
@@ -249,10 +362,26 @@ var DefaultHookRegistry = class {
|
|
|
249
362
|
};
|
|
250
363
|
|
|
251
364
|
// src/tool-registry.ts
|
|
365
|
+
function mcpServerName(toolName) {
|
|
366
|
+
if (!toolName.startsWith("mcp__")) return void 0;
|
|
367
|
+
return toolName.split("__")[1];
|
|
368
|
+
}
|
|
369
|
+
function passesFilter(entry, filterOpts) {
|
|
370
|
+
if (!filterOpts) return true;
|
|
371
|
+
const { allowedMcpServers, allowedPlugins } = filterOpts;
|
|
372
|
+
if (allowedMcpServers !== void 0) {
|
|
373
|
+
const server = mcpServerName(entry.tool.name);
|
|
374
|
+
if (server !== void 0 && !allowedMcpServers.includes(server)) return false;
|
|
375
|
+
}
|
|
376
|
+
if (allowedPlugins !== void 0 && entry.pluginId !== void 0) {
|
|
377
|
+
if (!allowedPlugins.includes(entry.pluginId)) return false;
|
|
378
|
+
}
|
|
379
|
+
return true;
|
|
380
|
+
}
|
|
252
381
|
var DefaultToolRegistry = class {
|
|
253
382
|
tools = /* @__PURE__ */ new Map();
|
|
254
|
-
register(tool) {
|
|
255
|
-
this.tools.set(tool.name, tool);
|
|
383
|
+
register(tool, opts) {
|
|
384
|
+
this.tools.set(tool.name, { tool, pluginId: opts?.pluginId });
|
|
256
385
|
}
|
|
257
386
|
registerAll(tools) {
|
|
258
387
|
for (const tool of tools) {
|
|
@@ -263,27 +392,33 @@ var DefaultToolRegistry = class {
|
|
|
263
392
|
this.tools.delete(name);
|
|
264
393
|
}
|
|
265
394
|
get(name) {
|
|
266
|
-
return this.tools.get(name);
|
|
395
|
+
return this.tools.get(name)?.tool;
|
|
267
396
|
}
|
|
268
397
|
getAvailable() {
|
|
269
|
-
return [...this.tools.values()].filter((
|
|
398
|
+
return [...this.tools.values()].filter((e) => !e.tool.isAvailable || e.tool.isAvailable()).map((e) => e.tool);
|
|
270
399
|
}
|
|
271
400
|
getForToolset(toolset) {
|
|
272
401
|
return this.getAvailable().filter((t) => t.toolset === toolset);
|
|
273
402
|
}
|
|
274
|
-
toDefinitions(allowedTools) {
|
|
275
|
-
const
|
|
276
|
-
|
|
277
|
-
|
|
278
|
-
|
|
279
|
-
|
|
280
|
-
|
|
403
|
+
toDefinitions(allowedTools, filterOpts) {
|
|
404
|
+
const entries = [...this.tools.values()].filter(
|
|
405
|
+
(e) => !e.tool.isAvailable || e.tool.isAvailable()
|
|
406
|
+
);
|
|
407
|
+
const filtered = entries.filter((e) => {
|
|
408
|
+
if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(e.tool.name))
|
|
409
|
+
return false;
|
|
410
|
+
return passesFilter(e, filterOpts);
|
|
411
|
+
});
|
|
412
|
+
return filtered.map((e) => ({
|
|
413
|
+
name: e.tool.name,
|
|
414
|
+
description: e.tool.description,
|
|
415
|
+
parameters: e.tool.schema
|
|
281
416
|
}));
|
|
282
417
|
}
|
|
283
418
|
// Runs all tool calls in parallel. Results are returned in input order.
|
|
284
419
|
// Budget is split evenly across parallel calls; each result is post-trimmed to budget.
|
|
285
|
-
// allowedTools
|
|
286
|
-
async executeParallel(calls, ctx, allowedTools) {
|
|
420
|
+
// allowedTools + filterOpts enforce tool access at execution time (belt-and-suspenders).
|
|
421
|
+
async executeParallel(calls, ctx, allowedTools, filterOpts) {
|
|
287
422
|
const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));
|
|
288
423
|
const results = await Promise.allSettled(
|
|
289
424
|
calls.map(async (call) => {
|
|
@@ -298,8 +433,8 @@ var DefaultToolRegistry = class {
|
|
|
298
433
|
}
|
|
299
434
|
};
|
|
300
435
|
}
|
|
301
|
-
const
|
|
302
|
-
if (!
|
|
436
|
+
const entry = this.tools.get(call.name);
|
|
437
|
+
if (!entry) {
|
|
303
438
|
return {
|
|
304
439
|
toolCallId: call.toolCallId,
|
|
305
440
|
name: call.name,
|
|
@@ -310,7 +445,18 @@ var DefaultToolRegistry = class {
|
|
|
310
445
|
}
|
|
311
446
|
};
|
|
312
447
|
}
|
|
313
|
-
if (
|
|
448
|
+
if (!passesFilter(entry, filterOpts)) {
|
|
449
|
+
return {
|
|
450
|
+
toolCallId: call.toolCallId,
|
|
451
|
+
name: call.name,
|
|
452
|
+
result: {
|
|
453
|
+
ok: false,
|
|
454
|
+
error: `Tool ${call.name} is not permitted for this personality`,
|
|
455
|
+
code: "not_available"
|
|
456
|
+
}
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
if (entry.tool.isAvailable && !entry.tool.isAvailable()) {
|
|
314
460
|
return {
|
|
315
461
|
toolCallId: call.toolCallId,
|
|
316
462
|
name: call.name,
|
|
@@ -321,10 +467,10 @@ var DefaultToolRegistry = class {
|
|
|
321
467
|
}
|
|
322
468
|
};
|
|
323
469
|
}
|
|
324
|
-
const budget = Math.min(perCallBudget, tool.maxResultChars ?? perCallBudget);
|
|
470
|
+
const budget = Math.min(perCallBudget, entry.tool.maxResultChars ?? perCallBudget);
|
|
325
471
|
const toolCtx = { ...ctx, resultBudgetChars: budget };
|
|
326
472
|
try {
|
|
327
|
-
const result = await tool.execute(call.args, toolCtx);
|
|
473
|
+
const result = await entry.tool.execute(call.args, toolCtx);
|
|
328
474
|
if (result.ok && result.value.length > budget) {
|
|
329
475
|
return {
|
|
330
476
|
toolCallId: call.toolCallId,
|
|
@@ -373,14 +519,24 @@ var AgentLoop = class {
|
|
|
373
519
|
personalities;
|
|
374
520
|
memory;
|
|
375
521
|
session;
|
|
522
|
+
/** Public so surfaces (web, ACP) can register late-binding hooks they own
|
|
523
|
+
* without re-running the whole wiring factory. The CLI/TUI register hooks
|
|
524
|
+
* before construction; web registers an approval hook after createAgentLoop
|
|
525
|
+
* returns. */
|
|
376
526
|
hooks;
|
|
377
527
|
injectors;
|
|
528
|
+
injectorPluginIds;
|
|
378
529
|
maxIterations;
|
|
379
530
|
historyLimit;
|
|
380
531
|
platform;
|
|
381
532
|
workingDir;
|
|
382
533
|
resultBudgetChars;
|
|
534
|
+
maxToolCallsPerTurn;
|
|
535
|
+
maxIdenticalToolCalls;
|
|
536
|
+
streamingTimeoutMs;
|
|
383
537
|
modelRouting;
|
|
538
|
+
storage;
|
|
539
|
+
dataDir;
|
|
384
540
|
constructor(config) {
|
|
385
541
|
this.llm = config.llm;
|
|
386
542
|
this.tools = config.tools ?? new DefaultToolRegistry();
|
|
@@ -389,12 +545,18 @@ var AgentLoop = class {
|
|
|
389
545
|
this.session = config.session ?? new InMemorySessionStore();
|
|
390
546
|
this.hooks = config.hooks ?? new DefaultHookRegistry();
|
|
391
547
|
this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);
|
|
548
|
+
this.injectorPluginIds = config.injectorPluginIds ?? /* @__PURE__ */ new Map();
|
|
392
549
|
this.maxIterations = config.options?.maxIterations ?? 50;
|
|
393
550
|
this.historyLimit = config.options?.historyLimit ?? 200;
|
|
394
551
|
this.platform = config.options?.platform ?? "cli";
|
|
395
552
|
this.workingDir = config.options?.workingDir ?? process.cwd();
|
|
396
553
|
this.resultBudgetChars = config.options?.resultBudgetChars ?? 8e4;
|
|
554
|
+
this.maxToolCallsPerTurn = config.options?.maxToolCallsPerTurn ?? 20;
|
|
555
|
+
this.maxIdenticalToolCalls = config.options?.maxIdenticalToolCalls ?? 5;
|
|
556
|
+
this.streamingTimeoutMs = config.options?.streamingTimeoutMs ?? 12e4;
|
|
397
557
|
this.modelRouting = config.modelRouting ?? {};
|
|
558
|
+
if (config.storage) this.storage = config.storage;
|
|
559
|
+
if (config.dataDir) this.dataDir = config.dataDir;
|
|
398
560
|
}
|
|
399
561
|
async *run(text, opts = {}) {
|
|
400
562
|
const abortSignal = opts.abortSignal ?? new AbortController().signal;
|
|
@@ -421,12 +583,21 @@ var AgentLoop = class {
|
|
|
421
583
|
const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;
|
|
422
584
|
const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : void 0;
|
|
423
585
|
const allowedTools = personality.toolset?.length ? personality.toolset : void 0;
|
|
424
|
-
|
|
425
|
-
|
|
426
|
-
|
|
427
|
-
|
|
428
|
-
|
|
429
|
-
|
|
586
|
+
const allowedPlugins = personality.plugins ?? [];
|
|
587
|
+
const filterOpts = {
|
|
588
|
+
allowedMcpServers: personality.mcp_servers,
|
|
589
|
+
allowedPlugins
|
|
590
|
+
};
|
|
591
|
+
await this.hooks.fireVoid(
|
|
592
|
+
"session_start",
|
|
593
|
+
{
|
|
594
|
+
sessionId,
|
|
595
|
+
sessionKey,
|
|
596
|
+
platform: this.platform,
|
|
597
|
+
personalityId: personality.id
|
|
598
|
+
},
|
|
599
|
+
allowedPlugins
|
|
600
|
+
);
|
|
430
601
|
await this.session.appendMessage({
|
|
431
602
|
sessionId,
|
|
432
603
|
role: "user",
|
|
@@ -440,6 +611,7 @@ var AgentLoop = class {
|
|
|
440
611
|
platform: this.platform,
|
|
441
612
|
workingDir: this.workingDir,
|
|
442
613
|
personalityId: personality.id,
|
|
614
|
+
memoryScope: personality.memoryScope,
|
|
443
615
|
query: text
|
|
444
616
|
});
|
|
445
617
|
const promptCtx = {
|
|
@@ -454,15 +626,13 @@ var AgentLoop = class {
|
|
|
454
626
|
personalityId: personality.id
|
|
455
627
|
};
|
|
456
628
|
const systemParts = [];
|
|
457
|
-
if (personality.ethosFile) {
|
|
458
|
-
|
|
459
|
-
|
|
460
|
-
const identity = await fs.readFile(personality.ethosFile, "utf-8");
|
|
461
|
-
systemParts.push(identity.trim());
|
|
462
|
-
} catch {
|
|
463
|
-
}
|
|
629
|
+
if (personality.ethosFile && this.storage) {
|
|
630
|
+
const identity = await this.storage.read(personality.ethosFile);
|
|
631
|
+
if (identity) systemParts.push(identity.trim());
|
|
464
632
|
}
|
|
465
633
|
for (const injector of this.injectors) {
|
|
634
|
+
const injPluginId = this.injectorPluginIds.get(injector);
|
|
635
|
+
if (injPluginId !== void 0 && !allowedPlugins.includes(injPluginId)) continue;
|
|
466
636
|
if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;
|
|
467
637
|
const result = await injector.inject(promptCtx);
|
|
468
638
|
if (result) {
|
|
@@ -481,11 +651,15 @@ var AgentLoop = class {
|
|
|
481
651
|
|
|
482
652
|
${memCtx.content}`);
|
|
483
653
|
}
|
|
484
|
-
const buildResult = await this.hooks.fireModifying(
|
|
485
|
-
|
|
486
|
-
|
|
487
|
-
|
|
488
|
-
|
|
654
|
+
const buildResult = await this.hooks.fireModifying(
|
|
655
|
+
"before_prompt_build",
|
|
656
|
+
{
|
|
657
|
+
sessionId,
|
|
658
|
+
personalityId: personality.id,
|
|
659
|
+
history
|
|
660
|
+
},
|
|
661
|
+
allowedPlugins
|
|
662
|
+
);
|
|
489
663
|
if (buildResult.overrideSystem) {
|
|
490
664
|
systemParts.length = 0;
|
|
491
665
|
systemParts.push(buildResult.overrideSystem);
|
|
@@ -497,39 +671,103 @@ ${memCtx.content}`);
|
|
|
497
671
|
const llmMessages = this.toLLMMessages(history);
|
|
498
672
|
let fullText = "";
|
|
499
673
|
let turnCount = 0;
|
|
674
|
+
let totalToolCalls = 0;
|
|
675
|
+
const toolNameCounts = /* @__PURE__ */ new Map();
|
|
500
676
|
for (let iteration = 0; iteration < this.maxIterations; iteration++) {
|
|
501
677
|
if (abortSignal.aborted) {
|
|
502
678
|
yield { type: "error", error: "Aborted", code: "aborted" };
|
|
503
679
|
return;
|
|
504
680
|
}
|
|
505
|
-
|
|
506
|
-
|
|
507
|
-
|
|
508
|
-
|
|
509
|
-
|
|
681
|
+
if (totalToolCalls >= this.maxToolCallsPerTurn) {
|
|
682
|
+
yield {
|
|
683
|
+
type: "tool_progress",
|
|
684
|
+
toolName: "_budget",
|
|
685
|
+
message: `Stopped: hit ${this.maxToolCallsPerTurn}-tool-call budget for this turn`,
|
|
686
|
+
audience: "user"
|
|
687
|
+
};
|
|
688
|
+
break;
|
|
689
|
+
}
|
|
690
|
+
const overusedTool = [...toolNameCounts.entries()].find(
|
|
691
|
+
([, count]) => count >= this.maxIdenticalToolCalls
|
|
692
|
+
);
|
|
693
|
+
if (overusedTool) {
|
|
694
|
+
yield {
|
|
695
|
+
type: "tool_progress",
|
|
696
|
+
toolName: overusedTool[0],
|
|
697
|
+
message: `Stopped: ${overusedTool[0]} called ${overusedTool[1]} times in one turn (likely loop)`,
|
|
698
|
+
audience: "user"
|
|
699
|
+
};
|
|
700
|
+
break;
|
|
701
|
+
}
|
|
702
|
+
await this.hooks.fireVoid(
|
|
703
|
+
"before_llm_call",
|
|
704
|
+
{
|
|
705
|
+
sessionId,
|
|
706
|
+
model: this.llm.model,
|
|
707
|
+
turnNumber: turnCount
|
|
708
|
+
},
|
|
709
|
+
allowedPlugins
|
|
710
|
+
);
|
|
510
711
|
const pendingToolCalls = [];
|
|
511
712
|
let chunkText = "";
|
|
713
|
+
const watchdogMs = personality.streamingTimeoutMs ?? this.streamingTimeoutMs;
|
|
714
|
+
const watchdogController = new AbortController();
|
|
715
|
+
const combinedSignal = AbortSignal.any([abortSignal, watchdogController.signal]);
|
|
716
|
+
let watchdogTimer;
|
|
717
|
+
const armWatchdog = () => {
|
|
718
|
+
if (watchdogTimer) clearTimeout(watchdogTimer);
|
|
719
|
+
watchdogTimer = setTimeout(() => watchdogController.abort(), watchdogMs);
|
|
720
|
+
};
|
|
721
|
+
const disarmWatchdog = () => {
|
|
722
|
+
if (watchdogTimer) clearTimeout(watchdogTimer);
|
|
723
|
+
watchdogTimer = void 0;
|
|
724
|
+
};
|
|
512
725
|
try {
|
|
513
|
-
|
|
726
|
+
armWatchdog();
|
|
727
|
+
const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools, filterOpts), {
|
|
514
728
|
system: systemPrompt,
|
|
515
729
|
cacheSystemPrompt: true,
|
|
516
|
-
abortSignal,
|
|
730
|
+
abortSignal: combinedSignal,
|
|
517
731
|
...modelOverride ? { modelOverride } : {}
|
|
518
732
|
});
|
|
519
733
|
for await (const chunk of stream) {
|
|
520
734
|
if (abortSignal.aborted) break;
|
|
735
|
+
if (watchdogController.signal.aborted) break;
|
|
736
|
+
armWatchdog();
|
|
521
737
|
yield* this.handleChunk(chunk, pendingToolCalls, (t) => {
|
|
522
738
|
chunkText += t;
|
|
523
739
|
fullText += t;
|
|
524
740
|
});
|
|
525
741
|
}
|
|
742
|
+
disarmWatchdog();
|
|
743
|
+
if (watchdogController.signal.aborted && !abortSignal.aborted) {
|
|
744
|
+
yield {
|
|
745
|
+
type: "error",
|
|
746
|
+
error: `LLM stream stalled \u2014 no chunk for ${watchdogMs}ms`,
|
|
747
|
+
code: "streaming_timeout"
|
|
748
|
+
};
|
|
749
|
+
return;
|
|
750
|
+
}
|
|
526
751
|
} catch (err) {
|
|
752
|
+
disarmWatchdog();
|
|
753
|
+
if (watchdogController.signal.aborted && !abortSignal.aborted) {
|
|
754
|
+
yield {
|
|
755
|
+
type: "error",
|
|
756
|
+
error: `LLM stream stalled \u2014 no chunk for ${watchdogMs}ms`,
|
|
757
|
+
code: "streaming_timeout"
|
|
758
|
+
};
|
|
759
|
+
return;
|
|
760
|
+
}
|
|
527
761
|
const msg = err instanceof Error ? err.message : String(err);
|
|
528
762
|
yield { type: "error", error: msg, code: "llm_error" };
|
|
529
763
|
return;
|
|
530
764
|
}
|
|
531
765
|
turnCount++;
|
|
532
766
|
const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== void 0);
|
|
767
|
+
totalToolCalls += completedToolCalls.length;
|
|
768
|
+
for (const tc of completedToolCalls) {
|
|
769
|
+
toolNameCounts.set(tc.toolName, (toolNameCounts.get(tc.toolName) ?? 0) + 1);
|
|
770
|
+
}
|
|
533
771
|
await this.session.appendMessage({
|
|
534
772
|
sessionId,
|
|
535
773
|
role: "assistant",
|
|
@@ -542,11 +780,15 @@ ${memCtx.content}`);
|
|
|
542
780
|
}))
|
|
543
781
|
}
|
|
544
782
|
});
|
|
545
|
-
await this.hooks.fireVoid(
|
|
546
|
-
|
|
547
|
-
|
|
548
|
-
|
|
549
|
-
|
|
783
|
+
await this.hooks.fireVoid(
|
|
784
|
+
"after_llm_call",
|
|
785
|
+
{
|
|
786
|
+
sessionId,
|
|
787
|
+
text: chunkText,
|
|
788
|
+
usage: { inputTokens: 0, outputTokens: 0 }
|
|
789
|
+
},
|
|
790
|
+
allowedPlugins
|
|
791
|
+
);
|
|
550
792
|
if (completedToolCalls.length > 0) {
|
|
551
793
|
const assistantContent = [];
|
|
552
794
|
if (chunkText) assistantContent.push({ type: "text", text: chunkText });
|
|
@@ -563,32 +805,50 @@ ${memCtx.content}`);
|
|
|
563
805
|
llmMessages.push({ role: "assistant", content: chunkText });
|
|
564
806
|
break;
|
|
565
807
|
}
|
|
808
|
+
const progressBuffer = [];
|
|
809
|
+
const scopedStorage = this.buildScopedStorage(personality);
|
|
566
810
|
const toolCtxBase = {
|
|
567
811
|
sessionId,
|
|
568
812
|
sessionKey,
|
|
569
813
|
platform: this.platform,
|
|
570
814
|
workingDir: this.workingDir,
|
|
815
|
+
agentId: opts.agentId,
|
|
816
|
+
personalityId: personality.id,
|
|
817
|
+
memoryScope: personality.memoryScope,
|
|
571
818
|
currentTurn: turnCount,
|
|
572
819
|
messageCount: allMessages.length + turnCount,
|
|
573
820
|
abortSignal,
|
|
574
|
-
emit: (
|
|
821
|
+
emit: (event) => {
|
|
822
|
+
progressBuffer.push({
|
|
823
|
+
toolName: event.toolName,
|
|
824
|
+
message: event.message,
|
|
825
|
+
...event.percent !== void 0 && { percent: event.percent },
|
|
826
|
+
audience: event.audience ?? "internal"
|
|
827
|
+
});
|
|
575
828
|
},
|
|
576
|
-
resultBudgetChars: this.resultBudgetChars
|
|
829
|
+
resultBudgetChars: this.resultBudgetChars,
|
|
830
|
+
...scopedStorage ? { storage: scopedStorage } : {}
|
|
577
831
|
};
|
|
578
832
|
const prepped = [];
|
|
579
833
|
for (const tc of completedToolCalls) {
|
|
580
|
-
const beforeResult = await this.hooks.fireModifying(
|
|
581
|
-
|
|
582
|
-
|
|
583
|
-
|
|
584
|
-
|
|
834
|
+
const beforeResult = await this.hooks.fireModifying(
|
|
835
|
+
"before_tool_call",
|
|
836
|
+
{
|
|
837
|
+
sessionId,
|
|
838
|
+
toolCallId: tc.toolCallId,
|
|
839
|
+
toolName: tc.toolName,
|
|
840
|
+
args: tc.args
|
|
841
|
+
},
|
|
842
|
+
allowedPlugins
|
|
843
|
+
);
|
|
585
844
|
if (beforeResult.error) {
|
|
586
845
|
yield {
|
|
587
846
|
type: "tool_end",
|
|
588
847
|
toolCallId: tc.toolCallId,
|
|
589
848
|
toolName: tc.toolName,
|
|
590
849
|
ok: false,
|
|
591
|
-
durationMs: 0
|
|
850
|
+
durationMs: 0,
|
|
851
|
+
result: beforeResult.error
|
|
592
852
|
};
|
|
593
853
|
prepped.push({
|
|
594
854
|
toolCallId: tc.toolCallId,
|
|
@@ -609,8 +869,12 @@ ${memCtx.content}`);
|
|
|
609
869
|
}
|
|
610
870
|
const execInputs = prepped.filter((p) => p.rejected === void 0).map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));
|
|
611
871
|
const startedAt = Date.now();
|
|
612
|
-
const execResults = execInputs.length > 0 ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools) : [];
|
|
872
|
+
const execResults = execInputs.length > 0 ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools, filterOpts) : [];
|
|
613
873
|
const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));
|
|
874
|
+
for (const ev of progressBuffer) {
|
|
875
|
+
yield { type: "tool_progress", ...ev };
|
|
876
|
+
}
|
|
877
|
+
progressBuffer.length = 0;
|
|
614
878
|
const toolResultContent = [];
|
|
615
879
|
for (const p of prepped) {
|
|
616
880
|
const durationMs = Date.now() - startedAt;
|
|
@@ -629,14 +893,19 @@ ${memCtx.content}`);
|
|
|
629
893
|
toolCallId: p.toolCallId,
|
|
630
894
|
toolName: p.name,
|
|
631
895
|
ok: result.ok,
|
|
632
|
-
durationMs
|
|
896
|
+
durationMs,
|
|
897
|
+
result: result.ok ? result.value : result.error
|
|
633
898
|
};
|
|
634
|
-
await this.hooks.fireVoid(
|
|
635
|
-
|
|
636
|
-
|
|
637
|
-
|
|
638
|
-
|
|
639
|
-
|
|
899
|
+
await this.hooks.fireVoid(
|
|
900
|
+
"after_tool_call",
|
|
901
|
+
{
|
|
902
|
+
sessionId,
|
|
903
|
+
toolName: p.name,
|
|
904
|
+
result,
|
|
905
|
+
durationMs
|
|
906
|
+
},
|
|
907
|
+
allowedPlugins
|
|
908
|
+
);
|
|
640
909
|
}
|
|
641
910
|
await this.session.appendMessage({
|
|
642
911
|
sessionId,
|
|
@@ -654,12 +923,8 @@ ${memCtx.content}`);
|
|
|
654
923
|
}
|
|
655
924
|
llmMessages.push({ role: "user", content: toolResultContent });
|
|
656
925
|
}
|
|
657
|
-
await this.memory.sync(
|
|
658
|
-
{ sessionId, sessionKey, platform: this.platform, workingDir: this.workingDir },
|
|
659
|
-
[]
|
|
660
|
-
);
|
|
661
926
|
await this.session.updateUsage(sessionId, { apiCallCount: turnCount });
|
|
662
|
-
await this.hooks.fireVoid("agent_done", { sessionId, text: fullText, turnCount });
|
|
927
|
+
await this.hooks.fireVoid("agent_done", { sessionId, text: fullText, turnCount }, allowedPlugins);
|
|
663
928
|
yield { type: "done", text: fullText, turnCount };
|
|
664
929
|
}
|
|
665
930
|
*handleChunk(chunk, pendingToolCalls, onText) {
|
|
@@ -743,7 +1008,32 @@ ${memCtx.content}`);
|
|
|
743
1008
|
}
|
|
744
1009
|
return messages;
|
|
745
1010
|
}
|
|
1011
|
+
// ---------------------------------------------------------------------------
|
|
1012
|
+
// Per-turn ScopedStorage construction (Phase 4 — fs_reach enforcement).
|
|
1013
|
+
//
|
|
1014
|
+
// When the AgentLoop was wired with `storage` + `dataDir`, build a
|
|
1015
|
+
// ScopedStorage decorated with the active personality's `fs_reach`
|
|
1016
|
+
// allowlist for this turn. Substitutions (${ETHOS_HOME} / ${self} /
|
|
1017
|
+
// ${CWD}) are resolved here so the underlying storage-fs class stays
|
|
1018
|
+
// pristine. When `fs_reach` is unset, fall back to a sensible default:
|
|
1019
|
+
// read: [<ethosHome>/personalities/<self>/, <ethosHome>/skills/, <cwd>]
|
|
1020
|
+
// write: [<ethosHome>/personalities/<self>/, <cwd>]
|
|
1021
|
+
// ---------------------------------------------------------------------------
|
|
1022
|
+
buildScopedStorage(personality) {
|
|
1023
|
+
if (!this.storage) return void 0;
|
|
1024
|
+
const ethosHome = this.dataDir ?? join(homedir(), ".ethos");
|
|
1025
|
+
const cwd = this.workingDir;
|
|
1026
|
+
const self = personality.id;
|
|
1027
|
+
const ownDir = `${join(ethosHome, "personalities", self)}/`;
|
|
1028
|
+
const fsReach = personality.fs_reach;
|
|
1029
|
+
const readPrefixes = fsReach?.read && fsReach.read.length > 0 ? fsReach.read.map((p) => substitute(p, { ethosHome, self, cwd })) : [ownDir, `${join(ethosHome, "skills")}/`, cwd];
|
|
1030
|
+
const writePrefixes = fsReach?.write && fsReach.write.length > 0 ? fsReach.write.map((p) => substitute(p, { ethosHome, self, cwd })) : [ownDir, cwd];
|
|
1031
|
+
return new ScopedStorage(this.storage, { read: readPrefixes, write: writePrefixes });
|
|
1032
|
+
}
|
|
746
1033
|
};
|
|
1034
|
+
function substitute(template, vars) {
|
|
1035
|
+
return template.replace(/\$\{ETHOS_HOME\}/g, vars.ethosHome).replace(/\$\{self\}/g, vars.self).replace(/\$\{CWD\}/g, vars.cwd);
|
|
1036
|
+
}
|
|
747
1037
|
|
|
748
1038
|
// src/plugin-registry.ts
|
|
749
1039
|
var PluginRegistry = class {
|
package/dist/index.js.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"sources":["../src/defaults/in-memory-session.ts","../src/defaults/noop-memory.ts","../src/defaults/noop-personality.ts","../src/hook-registry.ts","../src/tool-registry.ts","../src/agent-loop.ts","../src/plugin-registry.ts"],"sourcesContent":["import type {\n SearchResult,\n Session,\n SessionFilter,\n SessionStore,\n SessionUsage,\n StoredMessage,\n} from '@ethosagent/types';\n\nexport class InMemorySessionStore implements SessionStore {\n private sessions = new Map<string, Session>();\n private messages = new Map<string, StoredMessage[]>();\n private idCounter = 0;\n\n async createSession(data: Omit<Session, 'id' | 'createdAt' | 'updatedAt'>): Promise<Session> {\n const session: Session = {\n ...data,\n id: `session_${++this.idCounter}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n this.sessions.set(session.id, session);\n this.messages.set(session.id, []);\n return session;\n }\n\n async getSession(id: string): Promise<Session | null> {\n return this.sessions.get(id) ?? null;\n }\n\n async getSessionByKey(key: string): Promise<Session | null> {\n for (const s of this.sessions.values()) {\n if (s.key === key) return s;\n }\n return null;\n }\n\n async updateSession(id: string, patch: Partial<Session>): Promise<void> {\n const session = this.sessions.get(id);\n if (!session) throw new Error(`Session not found: ${id}`);\n this.sessions.set(id, { ...session, ...patch, updatedAt: new Date() });\n }\n\n async deleteSession(id: string): Promise<void> {\n this.sessions.delete(id);\n this.messages.delete(id);\n }\n\n async listSessions(filter?: SessionFilter): Promise<Session[]> {\n let results = [...this.sessions.values()];\n if (filter?.platform) results = results.filter((s) => s.platform === filter.platform);\n if (filter?.personalityId)\n results = results.filter((s) => s.personalityId === filter.personalityId);\n if (filter?.workingDir) results = results.filter((s) => s.workingDir === filter.workingDir);\n if (filter?.since) {\n const since = filter.since;\n results = results.filter((s) => s.createdAt >= since);\n }\n results.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n const offset = filter?.offset ?? 0;\n const limit = filter?.limit ?? results.length;\n return results.slice(offset, offset + limit);\n }\n\n async appendMessage(data: Omit<StoredMessage, 'id' | 'timestamp'>): Promise<StoredMessage> {\n const message: StoredMessage = {\n ...data,\n id: `msg_${++this.idCounter}`,\n timestamp: new Date(),\n };\n const list = this.messages.get(data.sessionId) ?? [];\n list.push(message);\n this.messages.set(data.sessionId, list);\n return message;\n }\n\n async getMessages(\n sessionId: string,\n options?: { limit?: number; offset?: number },\n ): Promise<StoredMessage[]> {\n const all = this.messages.get(sessionId) ?? [];\n const offset = options?.offset ?? 0;\n // Return most-recent messages: trim from the tail, then skip `offset` from the end\n const end = all.length - offset;\n const start = options?.limit ? Math.max(0, end - options.limit) : 0;\n return all.slice(start, end);\n }\n\n async updateUsage(sessionId: string, delta: Partial<SessionUsage>): Promise<void> {\n const session = this.sessions.get(sessionId);\n if (!session) return;\n const usage = { ...session.usage };\n for (const [k, v] of Object.entries(delta) as [keyof SessionUsage, number][]) {\n (usage[k] as number) += v;\n }\n this.sessions.set(sessionId, { ...session, usage, updatedAt: new Date() });\n }\n\n async search(\n query: string,\n options?: { limit?: number; sessionId?: string },\n ): Promise<SearchResult[]> {\n const results: SearchResult[] = [];\n const lower = query.toLowerCase();\n for (const [sessionId, msgs] of this.messages.entries()) {\n if (options?.sessionId && sessionId !== options.sessionId) continue;\n for (const msg of msgs) {\n const idx = msg.content.toLowerCase().indexOf(lower);\n if (idx >= 0) {\n results.push({\n sessionId,\n messageId: msg.id,\n snippet: msg.content.slice(Math.max(0, idx - 50), idx + 150),\n score: 1,\n timestamp: msg.timestamp,\n });\n }\n }\n }\n results.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());\n return results.slice(0, options?.limit ?? 20);\n }\n\n async pruneOldSessions(olderThan: Date): Promise<number> {\n let count = 0;\n for (const [id, session] of this.sessions.entries()) {\n if (session.updatedAt < olderThan) {\n this.sessions.delete(id);\n this.messages.delete(id);\n count++;\n }\n }\n return count;\n }\n\n async vacuum(): Promise<void> {\n // No-op for in-memory store\n }\n}\n","import type { MemoryLoadContext, MemoryProvider, MemoryUpdate } from '@ethosagent/types';\n\nexport class NoopMemoryProvider implements MemoryProvider {\n async prefetch(_ctx: MemoryLoadContext) {\n return null;\n }\n\n async sync(_ctx: MemoryLoadContext, _updates: MemoryUpdate[]): Promise<void> {\n // No-op\n }\n}\n","import type { PersonalityConfig, PersonalityRegistry } from '@ethosagent/types';\n\nconst DEFAULT_PERSONALITY: PersonalityConfig = {\n id: 'default',\n name: 'Default',\n description: 'Default Ethos personality',\n};\n\nexport class DefaultPersonalityRegistry implements PersonalityRegistry {\n private readonly personalities = new Map<string, PersonalityConfig>([\n ['default', DEFAULT_PERSONALITY],\n ]);\n private defaultId = 'default';\n\n define(config: PersonalityConfig): void {\n this.personalities.set(config.id, config);\n }\n\n get(id: string): PersonalityConfig | undefined {\n return this.personalities.get(id);\n }\n\n list(): PersonalityConfig[] {\n return [...this.personalities.values()];\n }\n\n getDefault(): PersonalityConfig {\n return this.personalities.get(this.defaultId) ?? DEFAULT_PERSONALITY;\n }\n\n setDefault(id: string): void {\n if (!this.personalities.has(id)) {\n throw new Error(`Unknown personality: ${id}`);\n }\n this.defaultId = id;\n }\n\n async loadFromDirectory(_dir: string): Promise<void> {\n // Implemented in extensions/personalities\n }\n}\n","import type { ClaimingHooks, HookRegistry, ModifyingHooks, VoidHooks } from '@ethosagent/types';\n\ntype AnyHandler = (...args: unknown[]) => Promise<unknown>;\n\ninterface RegisteredHandler {\n handler: AnyHandler;\n pluginId?: string;\n failurePolicy: 'fail-open' | 'fail-closed';\n}\n\nexport class DefaultHookRegistry implements HookRegistry {\n private readonly voidHandlers = new Map<string, RegisteredHandler[]>();\n private readonly modifyingHandlers = new Map<string, RegisteredHandler[]>();\n private readonly claimingHandlers = new Map<string, RegisteredHandler[]>();\n\n registerVoid<K extends keyof VoidHooks>(\n name: K,\n handler: (payload: VoidHooks[K]) => Promise<void>,\n opts?: { pluginId?: string; failurePolicy?: 'fail-open' | 'fail-closed' },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: opts?.failurePolicy ?? 'fail-open',\n };\n const list = this.voidHandlers.get(name) ?? [];\n list.push(entry);\n this.voidHandlers.set(name, list);\n return () => this.remove(this.voidHandlers, name, entry);\n }\n\n registerModifying<K extends keyof ModifyingHooks>(\n name: K,\n handler: (payload: ModifyingHooks[K][0]) => Promise<Partial<ModifyingHooks[K][1]> | null>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.modifyingHandlers.get(name) ?? [];\n list.push(entry);\n this.modifyingHandlers.set(name, list);\n return () => this.remove(this.modifyingHandlers, name, entry);\n }\n\n registerClaiming<K extends keyof ClaimingHooks>(\n name: K,\n handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.claimingHandlers.get(name) ?? [];\n list.push(entry);\n this.claimingHandlers.set(name, list);\n return () => this.remove(this.claimingHandlers, name, entry);\n }\n\n // Void hooks: all handlers run in parallel via Promise.allSettled.\n // Failures are logged but never propagate (fail-open by default).\n async fireVoid<K extends keyof VoidHooks>(name: K, payload: VoidHooks[K]): Promise<void> {\n const handlers = this.voidHandlers.get(name) ?? [];\n await Promise.allSettled(handlers.map((h) => h.handler(payload)));\n }\n\n // Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).\n async fireModifying<K extends keyof ModifyingHooks>(\n name: K,\n payload: ModifyingHooks[K][0],\n ): Promise<ModifyingHooks[K][1]> {\n const handlers = this.modifyingHandlers.get(name) ?? [];\n const merged: Record<string, unknown> = {};\n for (const h of handlers) {\n try {\n const result = await h.handler(payload);\n if (result && typeof result === 'object') {\n for (const [k, v] of Object.entries(result)) {\n if (!(k in merged) && v !== null && v !== undefined) {\n merged[k] = v;\n }\n }\n }\n } catch {\n // fail-open: continue with other handlers\n }\n }\n return merged as ModifyingHooks[K][1];\n }\n\n // Claiming hooks: handlers run sequentially, stop after first { handled: true }.\n async fireClaiming<K extends keyof ClaimingHooks>(\n name: K,\n payload: ClaimingHooks[K][0],\n ): Promise<ClaimingHooks[K][1]> {\n const handlers = this.claimingHandlers.get(name) ?? [];\n for (const h of handlers) {\n try {\n const result = (await h.handler(payload)) as ClaimingHooks[K][1];\n if (result && (result as { handled: boolean }).handled) {\n return result;\n }\n } catch {\n // fail-open: try next handler\n }\n }\n return { handled: false } as ClaimingHooks[K][1];\n }\n\n unregisterPlugin(pluginId: string): void {\n for (const map of [this.voidHandlers, this.modifyingHandlers, this.claimingHandlers]) {\n for (const [name, handlers] of map.entries()) {\n map.set(\n name,\n handlers.filter((h) => h.pluginId !== pluginId),\n );\n }\n }\n }\n\n private remove(\n map: Map<string, RegisteredHandler[]>,\n name: string,\n entry: RegisteredHandler,\n ): void {\n const list = map.get(name) ?? [];\n map.set(\n name,\n list.filter((h) => h !== entry),\n );\n }\n}\n","import type { Tool, ToolContext, ToolRegistry, ToolResult } from '@ethosagent/types';\n\nexport class DefaultToolRegistry implements ToolRegistry {\n private readonly tools = new Map<string, Tool>();\n\n register(tool: Tool): void {\n this.tools.set(tool.name, tool);\n }\n\n registerAll(tools: Tool[]): void {\n for (const tool of tools) {\n this.register(tool);\n }\n }\n\n unregister(name: string): void {\n this.tools.delete(name);\n }\n\n get(name: string): Tool | undefined {\n return this.tools.get(name);\n }\n\n getAvailable(): Tool[] {\n return [...this.tools.values()].filter((t) => !t.isAvailable || t.isAvailable());\n }\n\n getForToolset(toolset: string): Tool[] {\n return this.getAvailable().filter((t) => t.toolset === toolset);\n }\n\n toDefinitions(allowedTools?: string[]) {\n const available = this.getAvailable();\n const filtered =\n allowedTools && allowedTools.length > 0\n ? available.filter((t) => allowedTools.includes(t.name))\n : available;\n return filtered.map((t) => ({\n name: t.name,\n description: t.description,\n parameters: t.schema,\n }));\n }\n\n // Runs all tool calls in parallel. Results are returned in input order.\n // Budget is split evenly across parallel calls; each result is post-trimmed to budget.\n // allowedTools enforces toolset at execution time (belt-and-suspenders after toDefinitions filtering).\n async executeParallel(\n calls: Array<{ toolCallId: string; name: string; args: unknown }>,\n ctx: ToolContext,\n allowedTools?: string[],\n ): Promise<Array<{ toolCallId: string; name: string; result: ToolResult }>> {\n const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));\n\n const results = await Promise.allSettled(\n calls.map(async (call) => {\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(call.name)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const tool = this.tools.get(call.name);\n if (!tool) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Unknown tool: ${call.name}`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n if (tool.isAvailable && !tool.isAvailable()) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not currently available`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const budget = Math.min(perCallBudget, tool.maxResultChars ?? perCallBudget);\n const toolCtx: ToolContext = { ...ctx, resultBudgetChars: budget };\n\n try {\n const result = await tool.execute(call.args, toolCtx);\n // Post-trim result to budget\n if (result.ok && result.value.length > budget) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: true,\n value: `${result.value.slice(0, budget)}\\n[truncated — ${result.value.length} chars total]`,\n } as ToolResult,\n };\n }\n return { toolCallId: call.toolCallId, name: call.name, result };\n } catch (err) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n code: 'execution_failed',\n } as ToolResult,\n };\n }\n }),\n );\n\n // Unwrap settled results — always return, never throw\n return results.map((r, i) => {\n if (r.status === 'fulfilled') return r.value;\n const call = calls[i] ?? { toolCallId: 'unknown', name: 'unknown', args: {} };\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: String(r.reason),\n code: 'execution_failed',\n } as ToolResult,\n };\n });\n }\n}\n","import type {\n CompletionChunk,\n ContextInjector,\n HookRegistry,\n LLMProvider,\n MemoryProvider,\n Message,\n MessageContent,\n PersonalityRegistry,\n PromptContext,\n SessionStore,\n StoredMessage,\n ToolRegistry,\n ToolResult,\n} from '@ethosagent/types';\n\nimport { InMemorySessionStore } from './defaults/in-memory-session';\nimport { NoopMemoryProvider } from './defaults/noop-memory';\nimport { DefaultPersonalityRegistry } from './defaults/noop-personality';\nimport { DefaultHookRegistry } from './hook-registry';\nimport { DefaultToolRegistry } from './tool-registry';\n\n// ---------------------------------------------------------------------------\n// Agent events emitted by run()\n// ---------------------------------------------------------------------------\n\nexport type AgentEvent =\n | { type: 'text_delta'; text: string }\n | { type: 'thinking_delta'; thinking: string }\n | { type: 'tool_start'; toolCallId: string; toolName: string; args: unknown }\n | { type: 'tool_progress'; toolName: string; message: string; percent?: number }\n | { type: 'tool_end'; toolCallId: string; toolName: string; ok: boolean; durationMs: number }\n | { type: 'usage'; inputTokens: number; outputTokens: number; estimatedCostUsd: number }\n | { type: 'error'; error: string; code: string }\n | { type: 'done'; text: string; turnCount: number }\n // Emitted once after context injectors run; carries any metadata they wrote to PromptContext.meta.\n | { type: 'context_meta'; data: Record<string, unknown> };\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nexport interface AgentLoopConfig {\n llm: LLMProvider;\n tools?: ToolRegistry;\n personalities?: PersonalityRegistry;\n memory?: MemoryProvider;\n session?: SessionStore;\n hooks?: HookRegistry;\n injectors?: ContextInjector[];\n // Maps personality ID → model ID. Resolution: modelRouting[id] → personality.model → llm.model\n modelRouting?: Record<string, string>;\n options?: {\n maxIterations?: number;\n historyLimit?: number;\n platform?: string;\n workingDir?: string;\n resultBudgetChars?: number;\n };\n}\n\nexport interface RunOptions {\n sessionKey?: string;\n personalityId?: string;\n abortSignal?: AbortSignal;\n}\n\n// ---------------------------------------------------------------------------\n// AgentLoop\n// ---------------------------------------------------------------------------\n\nexport class AgentLoop {\n private readonly llm: LLMProvider;\n private readonly tools: ToolRegistry;\n private readonly personalities: PersonalityRegistry;\n private readonly memory: MemoryProvider;\n private readonly session: SessionStore;\n private readonly hooks: HookRegistry;\n private readonly injectors: ContextInjector[];\n private readonly maxIterations: number;\n private readonly historyLimit: number;\n private readonly platform: string;\n private readonly workingDir: string;\n private readonly resultBudgetChars: number;\n private readonly modelRouting: Record<string, string>;\n\n constructor(config: AgentLoopConfig) {\n this.llm = config.llm;\n this.tools = config.tools ?? new DefaultToolRegistry();\n this.personalities = config.personalities ?? new DefaultPersonalityRegistry();\n this.memory = config.memory ?? new NoopMemoryProvider();\n this.session = config.session ?? new InMemorySessionStore();\n this.hooks = config.hooks ?? new DefaultHookRegistry();\n this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);\n this.maxIterations = config.options?.maxIterations ?? 50;\n this.historyLimit = config.options?.historyLimit ?? 200;\n this.platform = config.options?.platform ?? 'cli';\n this.workingDir = config.options?.workingDir ?? process.cwd();\n this.resultBudgetChars = config.options?.resultBudgetChars ?? 80_000;\n this.modelRouting = config.modelRouting ?? {};\n }\n\n async *run(text: string, opts: RunOptions = {}): AsyncGenerator<AgentEvent> {\n const abortSignal = opts.abortSignal ?? new AbortController().signal;\n const sessionKey = opts.sessionKey ?? `${this.platform}:default`;\n\n // Step 1: Resolve or create session\n const ethosSession =\n (await this.session.getSessionByKey(sessionKey)) ??\n (await this.session.createSession({\n key: sessionKey,\n platform: this.platform,\n model: this.llm.model,\n provider: this.llm.name,\n personalityId: opts.personalityId,\n workingDir: this.workingDir,\n usage: {\n inputTokens: 0,\n outputTokens: 0,\n cacheReadTokens: 0,\n cacheCreationTokens: 0,\n estimatedCostUsd: 0,\n apiCallCount: 0,\n compactionCount: 0,\n },\n }));\n\n const sessionId = ethosSession.id;\n const personality =\n (opts.personalityId ? this.personalities.get(opts.personalityId) : null) ??\n this.personalities.getDefault();\n\n // Resolve effective model: explicit per-personality routing > LLM base model.\n // personality.model is intentionally skipped — those IDs are Anthropic-specific\n // and break non-Anthropic providers (OpenRouter, Gemini, Ollama, etc.).\n // Configure overrides via modelRouting in ~/.ethos/config.yaml instead.\n const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;\n const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : undefined;\n // Allowed tool names for this personality (undefined = no restriction)\n const allowedTools = personality.toolset?.length ? personality.toolset : undefined;\n\n // Step 2: Fire session_start hooks\n await this.hooks.fireVoid('session_start', {\n sessionId,\n sessionKey,\n platform: this.platform,\n personalityId: personality.id,\n });\n\n // Step 3: Persist the user message\n await this.session.appendMessage({\n sessionId,\n role: 'user',\n content: text,\n });\n\n // Step 4: Load history (trimmed to most-recent limit)\n const allMessages = await this.session.getMessages(sessionId, { limit: this.historyLimit });\n const history = allMessages.filter((m) => m.role !== 'system');\n\n // Step 5: Prefetch memory\n const memCtx = await this.memory.prefetch({\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n personalityId: personality.id,\n query: text,\n });\n\n // Step 6: Build system prompt from injectors\n const promptCtx: PromptContext = {\n sessionId,\n sessionKey,\n platform: this.platform,\n model: this.llm.model,\n history,\n workingDir: this.workingDir,\n isDm: true,\n turnNumber: allMessages.length,\n personalityId: personality.id,\n };\n\n const systemParts: string[] = [];\n\n // ETHOS.md / personality identity\n if (personality.ethosFile) {\n try {\n const fs = await import('node:fs/promises');\n const identity = await fs.readFile(personality.ethosFile, 'utf-8');\n systemParts.push(identity.trim());\n } catch {\n // ethosFile not readable — skip\n }\n }\n\n // Context injectors sorted by priority (already sorted in constructor)\n for (const injector of this.injectors) {\n if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;\n const result = await injector.inject(promptCtx);\n if (result) {\n if (result.position === 'prepend') {\n systemParts.unshift(result.content);\n } else {\n systemParts.push(result.content);\n }\n }\n }\n\n // Emit injector metadata (e.g. skill_files_used) so eval harness can capture it.\n if (promptCtx.meta && Object.keys(promptCtx.meta).length > 0) {\n yield { type: 'context_meta', data: promptCtx.meta };\n }\n\n // Memory injected last, as context about the user\n if (memCtx) {\n systemParts.push(`## Memory\\n\\n${memCtx.content}`);\n }\n\n // Step 7: Before-prompt-build modifying hooks (plugins can prepend/append/override)\n const buildResult = await this.hooks.fireModifying('before_prompt_build', {\n sessionId,\n personalityId: personality.id,\n history,\n });\n\n if (buildResult.overrideSystem) {\n systemParts.length = 0;\n systemParts.push(buildResult.overrideSystem);\n } else {\n if (buildResult.prependSystem) systemParts.unshift(buildResult.prependSystem);\n if (buildResult.appendSystem) systemParts.push(buildResult.appendSystem);\n }\n\n const systemPrompt = systemParts.join('\\n\\n').trim() || undefined;\n\n // Step 8: Agentic loop — LLM call → tool use → LLM call → ...\n const llmMessages = this.toLLMMessages(history);\n let fullText = '';\n let turnCount = 0;\n\n for (let iteration = 0; iteration < this.maxIterations; iteration++) {\n if (abortSignal.aborted) {\n yield { type: 'error', error: 'Aborted', code: 'aborted' };\n return;\n }\n\n // Fire before_llm_call\n await this.hooks.fireVoid('before_llm_call', {\n sessionId,\n model: this.llm.model,\n turnNumber: turnCount,\n });\n\n // Stream LLM response\n const pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }> = [];\n let chunkText = '';\n\n try {\n const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools), {\n system: systemPrompt,\n cacheSystemPrompt: true,\n abortSignal,\n ...(modelOverride ? { modelOverride } : {}),\n });\n\n for await (const chunk of stream) {\n if (abortSignal.aborted) break;\n yield* this.handleChunk(chunk, pendingToolCalls, (t) => {\n chunkText += t;\n fullText += t;\n });\n }\n } catch (err) {\n const msg = err instanceof Error ? err.message : String(err);\n yield { type: 'error', error: msg, code: 'llm_error' };\n return;\n }\n\n turnCount++;\n\n // Determine which tool calls completed parsing\n const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== undefined);\n\n // Persist assistant message — include tool_use references so history is LLM-replayable\n await this.session.appendMessage({\n sessionId,\n role: 'assistant',\n content: chunkText,\n ...(completedToolCalls.length > 0 && {\n toolCalls: completedToolCalls.map((tc) => ({\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n })),\n }),\n });\n\n // Fire after_llm_call\n await this.hooks.fireVoid('after_llm_call', {\n sessionId,\n text: chunkText,\n usage: { inputTokens: 0, outputTokens: 0 },\n });\n\n // Push assistant message with proper content blocks for next iteration\n if (completedToolCalls.length > 0) {\n const assistantContent: MessageContent[] = [];\n if (chunkText) assistantContent.push({ type: 'text', text: chunkText });\n for (const tc of completedToolCalls) {\n assistantContent.push({\n type: 'tool_use',\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n });\n }\n llmMessages.push({ role: 'assistant', content: assistantContent });\n } else {\n llmMessages.push({ role: 'assistant', content: chunkText });\n break;\n }\n\n // Step 9: Pre-flight hooks → execute non-rejected tools → collect all results\n const toolCtxBase = {\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n currentTurn: turnCount,\n messageCount: allMessages.length + turnCount,\n abortSignal,\n emit: (_event: {\n type: 'progress';\n toolName: string;\n message: string;\n percent?: number;\n }) => {\n // Progress emission wired in Phase 6 (terminal streaming)\n },\n resultBudgetChars: this.resultBudgetChars,\n };\n\n // Run before_tool_call hooks; build exec list with effective args\n // Rejected tools get tool_end ok:false + an error tool_result sent back to LLM\n type Prepped = { toolCallId: string; name: string; args: unknown; rejected?: string };\n const prepped: Prepped[] = [];\n\n for (const tc of completedToolCalls) {\n const beforeResult = await this.hooks.fireModifying('before_tool_call', {\n sessionId,\n toolName: tc.toolName,\n args: tc.args,\n });\n\n if (beforeResult.error) {\n yield {\n type: 'tool_end',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n ok: false,\n durationMs: 0,\n };\n prepped.push({\n toolCallId: tc.toolCallId,\n name: tc.toolName,\n args: tc.args,\n rejected: beforeResult.error,\n });\n continue;\n }\n\n const effectiveArgs = beforeResult.args ?? tc.args;\n yield {\n type: 'tool_start',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: effectiveArgs,\n };\n prepped.push({ toolCallId: tc.toolCallId, name: tc.toolName, args: effectiveArgs });\n }\n\n // Execute only non-rejected tools; results keyed by toolCallId\n const execInputs = prepped\n .filter((p) => p.rejected === undefined)\n .map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));\n\n const startedAt = Date.now();\n const execResults =\n execInputs.length > 0\n ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools)\n : [];\n const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));\n\n // Persist results + emit tool_end + build tool_result content blocks (original order)\n const toolResultContent: MessageContent[] = [];\n\n for (const p of prepped) {\n const durationMs = Date.now() - startedAt;\n let result: ToolResult;\n\n if (p.rejected !== undefined) {\n result = { ok: false, error: p.rejected, code: 'execution_failed' };\n // tool_end already emitted above; no after_tool_call hook for blocked tools\n } else {\n const execResult = execResultMap.get(p.toolCallId);\n result = execResult?.result ?? {\n ok: false,\n error: 'Tool result missing',\n code: 'execution_failed',\n };\n yield {\n type: 'tool_end',\n toolCallId: p.toolCallId,\n toolName: p.name,\n ok: result.ok,\n durationMs,\n };\n await this.hooks.fireVoid('after_tool_call', {\n sessionId,\n toolName: p.name,\n result,\n durationMs,\n });\n }\n\n // Persist every result (rejected or not) so history matches what LLM sees\n await this.session.appendMessage({\n sessionId,\n role: 'tool_result',\n content: result.ok ? result.value : result.error,\n toolCallId: p.toolCallId,\n toolName: p.name,\n });\n\n toolResultContent.push({\n type: 'tool_result',\n tool_use_id: p.toolCallId,\n content: result.ok ? result.value : result.error,\n is_error: !result.ok,\n });\n }\n\n // Feed all tool results back to LLM as a single user message with content blocks\n llmMessages.push({ role: 'user', content: toolResultContent });\n }\n\n // Step 10: Sync memory\n await this.memory.sync(\n { sessionId, sessionKey, platform: this.platform, workingDir: this.workingDir },\n [],\n );\n\n // Step 11: Update usage\n await this.session.updateUsage(sessionId, { apiCallCount: turnCount });\n\n // Step 12: Fire agent_done\n await this.hooks.fireVoid('agent_done', { sessionId, text: fullText, turnCount });\n\n yield { type: 'done', text: fullText, turnCount };\n }\n\n private *handleChunk(\n chunk: CompletionChunk,\n pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }>,\n onText: (t: string) => void,\n ): Generator<AgentEvent> {\n switch (chunk.type) {\n case 'text_delta':\n onText(chunk.text);\n yield { type: 'text_delta', text: chunk.text };\n break;\n\n case 'thinking_delta':\n yield { type: 'thinking_delta', thinking: chunk.thinking };\n break;\n\n case 'tool_use_start':\n pendingToolCalls.push({\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n partialJson: '',\n });\n break;\n\n case 'tool_use_delta': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) tc.partialJson += chunk.partialJson;\n break;\n }\n\n case 'tool_use_end': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) {\n try {\n tc.args = JSON.parse(chunk.inputJson || tc.partialJson);\n } catch {\n tc.args = {};\n }\n }\n break;\n }\n\n case 'usage':\n yield {\n type: 'usage',\n inputTokens: chunk.usage.inputTokens,\n outputTokens: chunk.usage.outputTokens,\n estimatedCostUsd: chunk.usage.estimatedCostUsd,\n };\n break;\n\n case 'done':\n // finishReason available here for future context-compaction (Phase 3)\n break;\n }\n }\n\n // Reconstruct LLM-ready messages from stored history.\n // Assistant messages with tool calls produce proper tool_use content blocks.\n // Consecutive tool_result rows are grouped into a single user message.\n private toLLMMessages(stored: StoredMessage[]): Message[] {\n const messages: Message[] = [];\n\n for (const msg of stored) {\n if (msg.role === 'system') continue;\n\n if (msg.role === 'user') {\n messages.push({ role: 'user', content: msg.content });\n } else if (msg.role === 'assistant') {\n if (msg.toolCalls && msg.toolCalls.length > 0) {\n const content: MessageContent[] = [];\n if (msg.content) content.push({ type: 'text', text: msg.content });\n for (const tc of msg.toolCalls) {\n content.push({ type: 'tool_use', id: tc.id, name: tc.name, input: tc.input });\n }\n messages.push({ role: 'assistant', content });\n } else {\n messages.push({ role: 'assistant', content: msg.content });\n }\n } else if (msg.role === 'tool_result') {\n const resultBlock: MessageContent = {\n type: 'tool_result',\n tool_use_id: msg.toolCallId ?? '',\n content: msg.content,\n is_error: false,\n };\n const last = messages[messages.length - 1];\n // Append to existing tool_result user message, or start a new one\n if (last?.role === 'user' && Array.isArray(last.content)) {\n (last.content as MessageContent[]).push(resultBlock);\n } else {\n messages.push({ role: 'user', content: [resultBlock] });\n }\n }\n }\n\n return messages;\n }\n}\n","// Generic plugin registry — adapted from praxis PluginRegistry pattern.\n// Each subsystem (tools, channels, memory backends) gets its own instance.\n\nexport type PluginFactory<T, C = unknown> = (config: C) => T | null;\n\nexport class PluginRegistry<T, C = unknown> {\n private readonly factories = new Map<string, PluginFactory<T, C>>();\n\n register(type: string, factory: PluginFactory<T, C>): void {\n this.factories.set(type, factory);\n }\n\n create(type: string, config: C): T {\n const factory = this.factories.get(type);\n if (!factory) {\n throw new Error(\n `Unknown plugin type: \"${type}\". Registered: ${[...this.factories.keys()].join(', ')}`,\n );\n }\n const instance = factory(config);\n if (!instance) {\n throw new Error(`Plugin factory for \"${type}\" returned null`);\n }\n return instance;\n }\n\n has(type: string): boolean {\n return this.factories.has(type);\n }\n\n types(): string[] {\n return [...this.factories.keys()];\n }\n}\n"],"mappings":";AASO,IAAM,uBAAN,MAAmD;AAAA,EAChD,WAAW,oBAAI,IAAqB;AAAA,EACpC,WAAW,oBAAI,IAA6B;AAAA,EAC5C,YAAY;AAAA,EAEpB,MAAM,cAAc,MAAyE;AAC3F,UAAM,UAAmB;AAAA,MACvB,GAAG;AAAA,MACH,IAAI,WAAW,EAAE,KAAK,SAAS;AAAA,MAC/B,WAAW,oBAAI,KAAK;AAAA,MACpB,WAAW,oBAAI,KAAK;AAAA,IACtB;AACA,SAAK,SAAS,IAAI,QAAQ,IAAI,OAAO;AACrC,SAAK,SAAS,IAAI,QAAQ,IAAI,CAAC,CAAC;AAChC,WAAO;AAAA,EACT;AAAA,EAEA,MAAM,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MAAc,SAAoC;AACzD,SAAK,UAAU,IAAI,MAAM,OAAO;AAAA,EAClC;AAAA,EAEA,OAAO,MAAc,QAAc;AACjC,UAAM,UAAU,KAAK,UAAU,IAAI,IAAI;AACvC,QAAI,CAAC,SAAS;AACZ,YAAM,IAAI;AAAA,QACR,yBAAyB,IAAI,kBAAkB,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC,EAAE,KAAK,IAAI,CAAC;AAAA,MACtF;AAAA,IACF;AACA,UAAM,WAAW,QAAQ,MAAM;AAC/B,QAAI,CAAC,UAAU;AACb,YAAM,IAAI,MAAM,uBAAuB,IAAI,iBAAiB;AAAA,IAC9D;AACA,WAAO;AAAA,EACT;AAAA,EAEA,IAAI,MAAuB;AACzB,WAAO,KAAK,UAAU,IAAI,IAAI;AAAA,EAChC;AAAA,EAEA,QAAkB;AAChB,WAAO,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,EAClC;AACF;","names":[]}
|
|
1
|
+
{"version":3,"sources":["../src/agent-loop.ts","../../storage-fs/src/fs-storage.ts","../../storage-fs/src/in-memory-storage.ts","../../storage-fs/src/scoped-storage.ts","../src/defaults/in-memory-session.ts","../src/defaults/noop-memory.ts","../src/defaults/noop-personality.ts","../src/hook-registry.ts","../src/tool-registry.ts","../src/plugin-registry.ts"],"sourcesContent":["import { homedir } from 'node:os';\nimport { join } from 'node:path';\nimport { ScopedStorage } from '@ethosagent/storage-fs';\nimport type {\n CompletionChunk,\n ContextInjector,\n HookRegistry,\n LLMProvider,\n MemoryProvider,\n Message,\n MessageContent,\n PersonalityConfig,\n PersonalityRegistry,\n PromptContext,\n SessionStore,\n Storage,\n StoredMessage,\n ToolFilterOpts,\n ToolRegistry,\n ToolResult,\n} from '@ethosagent/types';\n\nimport { InMemorySessionStore } from './defaults/in-memory-session';\nimport { NoopMemoryProvider } from './defaults/noop-memory';\nimport { DefaultPersonalityRegistry } from './defaults/noop-personality';\nimport { DefaultHookRegistry } from './hook-registry';\nimport { DefaultToolRegistry } from './tool-registry';\n\n// ---------------------------------------------------------------------------\n// Agent events emitted by run()\n// ---------------------------------------------------------------------------\n\nexport type AgentEvent =\n | { type: 'text_delta'; text: string }\n | { type: 'thinking_delta'; thinking: string }\n | { type: 'tool_start'; toolCallId: string; toolName: string; args: unknown }\n // Phase 30.2 — `audience` gates whether channel adapters / chat.ts surface\n // this event to the user. Default is `'internal'`; tools opt in to `'user'`\n // per event. Framework-emitted budget warnings are `'user'` (see step 7).\n | {\n type: 'tool_progress';\n toolName: string;\n message: string;\n percent?: number;\n audience: 'internal' | 'user';\n }\n | {\n type: 'tool_end';\n toolCallId: string;\n toolName: string;\n ok: boolean;\n durationMs: number;\n // Phase 30.2 — same boundary applies to tool_end success rendering.\n // Failures (`ok: false`) ignore the field and always render.\n audience?: 'internal' | 'user';\n /**\n * Tool output body — the success value when `ok`, or the error\n * message when `ok: false`. Optional so consumers that only care\n * about the status (CLI ASCII chips, telemetry) can ignore it.\n * The web chip surfaces this on expand-on-click without a\n * follow-up history fetch.\n */\n result?: string;\n }\n | { type: 'usage'; inputTokens: number; outputTokens: number; estimatedCostUsd: number }\n | { type: 'error'; error: string; code: string }\n | { type: 'done'; text: string; turnCount: number }\n // Emitted once after context injectors run; carries any metadata they wrote to PromptContext.meta.\n | { type: 'context_meta'; data: Record<string, unknown> };\n\n// ---------------------------------------------------------------------------\n// Config\n// ---------------------------------------------------------------------------\n\nexport interface AgentLoopConfig {\n llm: LLMProvider;\n tools?: ToolRegistry;\n personalities?: PersonalityRegistry;\n memory?: MemoryProvider;\n session?: SessionStore;\n hooks?: HookRegistry;\n injectors?: ContextInjector[];\n /**\n * Maps each plugin-registered injector to its plugin id so AgentLoop can\n * gate injectors by personality. Built-in injectors are absent (always fire).\n * Populated by PluginApiImpl.registerInjector(); passed through from wiring.\n */\n injectorPluginIds?: Map<ContextInjector, string>;\n /**\n * Base Storage instance handed to tools via `ToolContext.storage` after\n * being decorated with a ScopedStorage that enforces the active\n * personality's `fs_reach` allowlist. When unset, ToolContext.storage is\n * left undefined and tools fall back to unrestricted node:fs (legacy\n * behavior — existing CLI/TUI tests don't need a storage instance).\n */\n storage?: Storage;\n /**\n * Absolute path to ~/.ethos/ used for `${ETHOS_HOME}` substitution in\n * `fs_reach` paths. Defaults to `${HOME}/.ethos`. Required only when\n * `storage` is set.\n */\n dataDir?: string;\n // Maps personality ID → model ID. Resolution: modelRouting[id] → personality.model → llm.model\n modelRouting?: Record<string, string>;\n options?: {\n maxIterations?: number;\n historyLimit?: number;\n platform?: string;\n workingDir?: string;\n resultBudgetChars?: number;\n /**\n * Hard cap on total tool calls per user turn (across all LLM iterations).\n * Defaults to 20. Trips a `tool_progress` warning and exits cleanly.\n * See plan/IMPROVEMENT.md P1-3.\n */\n maxToolCallsPerTurn?: number;\n /**\n * Hard cap on the number of times the same tool name can be invoked in a\n * single turn. Catches the \"infinite loop on a single tool\" failure mode\n * (e.g. tts loop reported as OpenClaw #67744). Defaults to 5.\n */\n maxIdenticalToolCalls?: number;\n /**\n * Default streaming watchdog in milliseconds. If no chunk arrives from the\n * LLM within this window, the agent aborts the stream and emits an error.\n * Reset on every chunk. Personalities can override via\n * `personality.streamingTimeoutMs`. Defaults to 120000 (2 minutes).\n */\n streamingTimeoutMs?: number;\n };\n}\n\nexport interface RunOptions {\n sessionKey?: string;\n personalityId?: string;\n abortSignal?: AbortSignal;\n /**\n * Identifier surfaced to tools as `ToolContext.agentId`. Delegation tools\n * use this to thread spawn depth (`depth:N`) into child loops so\n * `MAX_SPAWN_DEPTH` can be enforced across recursive sub-agent calls.\n */\n agentId?: string;\n}\n\n// ---------------------------------------------------------------------------\n// AgentLoop\n// ---------------------------------------------------------------------------\n\nexport class AgentLoop {\n private readonly llm: LLMProvider;\n private readonly tools: ToolRegistry;\n private readonly personalities: PersonalityRegistry;\n private readonly memory: MemoryProvider;\n private readonly session: SessionStore;\n /** Public so surfaces (web, ACP) can register late-binding hooks they own\n * without re-running the whole wiring factory. The CLI/TUI register hooks\n * before construction; web registers an approval hook after createAgentLoop\n * returns. */\n readonly hooks: HookRegistry;\n private readonly injectors: ContextInjector[];\n private readonly injectorPluginIds: Map<ContextInjector, string>;\n private readonly maxIterations: number;\n private readonly historyLimit: number;\n private readonly platform: string;\n private readonly workingDir: string;\n private readonly resultBudgetChars: number;\n private readonly maxToolCallsPerTurn: number;\n private readonly maxIdenticalToolCalls: number;\n private readonly streamingTimeoutMs: number;\n private readonly modelRouting: Record<string, string>;\n private readonly storage?: Storage;\n private readonly dataDir?: string;\n\n constructor(config: AgentLoopConfig) {\n this.llm = config.llm;\n this.tools = config.tools ?? new DefaultToolRegistry();\n this.personalities = config.personalities ?? new DefaultPersonalityRegistry();\n this.memory = config.memory ?? new NoopMemoryProvider();\n this.session = config.session ?? new InMemorySessionStore();\n this.hooks = config.hooks ?? new DefaultHookRegistry();\n this.injectors = (config.injectors ?? []).sort((a, b) => b.priority - a.priority);\n this.injectorPluginIds = config.injectorPluginIds ?? new Map();\n this.maxIterations = config.options?.maxIterations ?? 50;\n this.historyLimit = config.options?.historyLimit ?? 200;\n this.platform = config.options?.platform ?? 'cli';\n this.workingDir = config.options?.workingDir ?? process.cwd();\n this.resultBudgetChars = config.options?.resultBudgetChars ?? 80_000;\n this.maxToolCallsPerTurn = config.options?.maxToolCallsPerTurn ?? 20;\n this.maxIdenticalToolCalls = config.options?.maxIdenticalToolCalls ?? 5;\n this.streamingTimeoutMs = config.options?.streamingTimeoutMs ?? 120_000;\n this.modelRouting = config.modelRouting ?? {};\n if (config.storage) this.storage = config.storage;\n if (config.dataDir) this.dataDir = config.dataDir;\n }\n\n async *run(text: string, opts: RunOptions = {}): AsyncGenerator<AgentEvent> {\n const abortSignal = opts.abortSignal ?? new AbortController().signal;\n const sessionKey = opts.sessionKey ?? `${this.platform}:default`;\n\n // Step 1: Resolve or create session\n const ethosSession =\n (await this.session.getSessionByKey(sessionKey)) ??\n (await this.session.createSession({\n key: sessionKey,\n platform: this.platform,\n model: this.llm.model,\n provider: this.llm.name,\n personalityId: opts.personalityId,\n workingDir: this.workingDir,\n usage: {\n inputTokens: 0,\n outputTokens: 0,\n cacheReadTokens: 0,\n cacheCreationTokens: 0,\n estimatedCostUsd: 0,\n apiCallCount: 0,\n compactionCount: 0,\n },\n }));\n\n const sessionId = ethosSession.id;\n const personality =\n (opts.personalityId ? this.personalities.get(opts.personalityId) : null) ??\n this.personalities.getDefault();\n\n // Resolve effective model: explicit per-personality routing > LLM base model.\n // personality.model is intentionally skipped — those IDs are Anthropic-specific\n // and break non-Anthropic providers (OpenRouter, Gemini, Ollama, etc.).\n // Configure overrides via modelRouting in ~/.ethos/config.yaml instead.\n const effectiveModel = this.modelRouting[personality.id] ?? this.llm.model;\n const modelOverride = effectiveModel !== this.llm.model ? effectiveModel : undefined;\n // Allowed tool names for this personality (undefined = no restriction)\n const allowedTools = personality.toolset?.length ? personality.toolset : undefined;\n // Per-personality plugin + MCP gate (default-deny: missing field = no access)\n const allowedPlugins = personality.plugins ?? [];\n const filterOpts: ToolFilterOpts = {\n allowedMcpServers: personality.mcp_servers,\n allowedPlugins,\n };\n\n // Step 2: Fire session_start hooks\n await this.hooks.fireVoid(\n 'session_start',\n {\n sessionId,\n sessionKey,\n platform: this.platform,\n personalityId: personality.id,\n },\n allowedPlugins,\n );\n\n // Step 3: Persist the user message.\n //\n // Subagent task contract: the delegated task always lives in the child's\n // first user message (this `text`). It is NEVER copied into the system\n // prompt, NEVER injected via memory, and NEVER duplicated across both.\n // The regression test in\n // `extensions/tools-delegation/src/__tests__/task-contract.test.ts`\n // captures every `LLMProvider.complete()` request and asserts the marker\n // never appears in `opts.system` and appears exactly once across all\n // user-role messages.\n await this.session.appendMessage({\n sessionId,\n role: 'user',\n content: text,\n });\n\n // Step 4: Load history (trimmed to most-recent limit)\n const allMessages = await this.session.getMessages(sessionId, { limit: this.historyLimit });\n const history = allMessages.filter((m) => m.role !== 'system');\n\n // Step 5: Prefetch memory\n const memCtx = await this.memory.prefetch({\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n personalityId: personality.id,\n memoryScope: personality.memoryScope,\n query: text,\n });\n\n // Step 6: Build system prompt from injectors\n const promptCtx: PromptContext = {\n sessionId,\n sessionKey,\n platform: this.platform,\n model: this.llm.model,\n history,\n workingDir: this.workingDir,\n isDm: true,\n turnNumber: allMessages.length,\n personalityId: personality.id,\n };\n\n const systemParts: string[] = [];\n\n // ETHOS.md / personality identity — routes through Storage so ScopedStorage\n // and InMemoryStorage fixtures work correctly. Only runs when storage is\n // wired (production always provides it; tests without a real ethosFile skip).\n if (personality.ethosFile && this.storage) {\n const identity = await this.storage.read(personality.ethosFile);\n if (identity) systemParts.push(identity.trim());\n }\n\n // Context injectors sorted by priority (already sorted in constructor)\n for (const injector of this.injectors) {\n // Plugin-registered injectors only fire when the plugin is permitted.\n const injPluginId = this.injectorPluginIds.get(injector);\n if (injPluginId !== undefined && !allowedPlugins.includes(injPluginId)) continue;\n if (injector.shouldInject && !injector.shouldInject(promptCtx)) continue;\n const result = await injector.inject(promptCtx);\n if (result) {\n if (result.position === 'prepend') {\n systemParts.unshift(result.content);\n } else {\n systemParts.push(result.content);\n }\n }\n }\n\n // Emit injector metadata (e.g. skill_files_used) so eval harness can capture it.\n if (promptCtx.meta && Object.keys(promptCtx.meta).length > 0) {\n yield { type: 'context_meta', data: promptCtx.meta };\n }\n\n // Memory injected last, as context about the user\n if (memCtx) {\n systemParts.push(`## Memory\\n\\n${memCtx.content}`);\n }\n\n // Step 7: Before-prompt-build modifying hooks (plugins can prepend/append/override)\n const buildResult = await this.hooks.fireModifying(\n 'before_prompt_build',\n {\n sessionId,\n personalityId: personality.id,\n history,\n },\n allowedPlugins,\n );\n\n if (buildResult.overrideSystem) {\n systemParts.length = 0;\n systemParts.push(buildResult.overrideSystem);\n } else {\n if (buildResult.prependSystem) systemParts.unshift(buildResult.prependSystem);\n if (buildResult.appendSystem) systemParts.push(buildResult.appendSystem);\n }\n\n const systemPrompt = systemParts.join('\\n\\n').trim() || undefined;\n\n // Step 8: Agentic loop — LLM call → tool use → LLM call → ...\n const llmMessages = this.toLLMMessages(history);\n let fullText = '';\n let turnCount = 0;\n\n // Tool-call budget tracking — prevents runaway loops (see IMPROVEMENT.md P1-3).\n // Counted across all iterations within a single user turn.\n let totalToolCalls = 0;\n const toolNameCounts = new Map<string, number>();\n\n for (let iteration = 0; iteration < this.maxIterations; iteration++) {\n if (abortSignal.aborted) {\n yield { type: 'error', error: 'Aborted', code: 'aborted' };\n return;\n }\n\n // Budget guard: bail before the next LLM call if we've already exceeded\n // either the total tool-call budget or the per-tool repeat budget. The\n // previous iteration's tool_result is in llmMessages, so the LLM history\n // stays valid; we just refuse to call again.\n if (totalToolCalls >= this.maxToolCallsPerTurn) {\n yield {\n type: 'tool_progress',\n toolName: '_budget',\n message: `Stopped: hit ${this.maxToolCallsPerTurn}-tool-call budget for this turn`,\n audience: 'user',\n };\n break;\n }\n const overusedTool = [...toolNameCounts.entries()].find(\n ([, count]) => count >= this.maxIdenticalToolCalls,\n );\n if (overusedTool) {\n yield {\n type: 'tool_progress',\n toolName: overusedTool[0],\n message: `Stopped: ${overusedTool[0]} called ${overusedTool[1]} times in one turn (likely loop)`,\n audience: 'user',\n };\n break;\n }\n\n // Fire before_llm_call\n await this.hooks.fireVoid(\n 'before_llm_call',\n {\n sessionId,\n model: this.llm.model,\n turnNumber: turnCount,\n },\n allowedPlugins,\n );\n\n // Stream LLM response\n const pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }> = [];\n let chunkText = '';\n\n // Streaming watchdog: cancel the stream if no chunk arrives within the\n // per-personality window. Reset every chunk so slow-but-progressing\n // reasoning is unaffected. See IMPROVEMENT.md P1-2 / OpenClaw #68596.\n const watchdogMs = personality.streamingTimeoutMs ?? this.streamingTimeoutMs;\n const watchdogController = new AbortController();\n const combinedSignal = AbortSignal.any([abortSignal, watchdogController.signal]);\n let watchdogTimer: ReturnType<typeof setTimeout> | undefined;\n const armWatchdog = () => {\n if (watchdogTimer) clearTimeout(watchdogTimer);\n watchdogTimer = setTimeout(() => watchdogController.abort(), watchdogMs);\n };\n const disarmWatchdog = () => {\n if (watchdogTimer) clearTimeout(watchdogTimer);\n watchdogTimer = undefined;\n };\n\n try {\n armWatchdog();\n const stream = this.llm.complete(llmMessages, this.tools.toDefinitions(allowedTools, filterOpts), {\n system: systemPrompt,\n cacheSystemPrompt: true,\n abortSignal: combinedSignal,\n ...(modelOverride ? { modelOverride } : {}),\n });\n\n for await (const chunk of stream) {\n if (abortSignal.aborted) break;\n if (watchdogController.signal.aborted) break;\n armWatchdog();\n yield* this.handleChunk(chunk, pendingToolCalls, (t) => {\n chunkText += t;\n fullText += t;\n });\n }\n disarmWatchdog();\n\n if (watchdogController.signal.aborted && !abortSignal.aborted) {\n yield {\n type: 'error',\n error: `LLM stream stalled — no chunk for ${watchdogMs}ms`,\n code: 'streaming_timeout',\n };\n return;\n }\n } catch (err) {\n disarmWatchdog();\n if (watchdogController.signal.aborted && !abortSignal.aborted) {\n yield {\n type: 'error',\n error: `LLM stream stalled — no chunk for ${watchdogMs}ms`,\n code: 'streaming_timeout',\n };\n return;\n }\n const msg = err instanceof Error ? err.message : String(err);\n yield { type: 'error', error: msg, code: 'llm_error' };\n return;\n }\n\n turnCount++;\n\n // Determine which tool calls completed parsing\n const completedToolCalls = pendingToolCalls.filter((tc) => tc.args !== undefined);\n\n // Update budget counters — these gate the NEXT iteration's LLM call.\n totalToolCalls += completedToolCalls.length;\n for (const tc of completedToolCalls) {\n toolNameCounts.set(tc.toolName, (toolNameCounts.get(tc.toolName) ?? 0) + 1);\n }\n\n // Persist assistant message — include tool_use references so history is LLM-replayable\n await this.session.appendMessage({\n sessionId,\n role: 'assistant',\n content: chunkText,\n ...(completedToolCalls.length > 0 && {\n toolCalls: completedToolCalls.map((tc) => ({\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n })),\n }),\n });\n\n // Fire after_llm_call\n await this.hooks.fireVoid(\n 'after_llm_call',\n {\n sessionId,\n text: chunkText,\n usage: { inputTokens: 0, outputTokens: 0 },\n },\n allowedPlugins,\n );\n\n // Push assistant message with proper content blocks for next iteration\n if (completedToolCalls.length > 0) {\n const assistantContent: MessageContent[] = [];\n if (chunkText) assistantContent.push({ type: 'text', text: chunkText });\n for (const tc of completedToolCalls) {\n assistantContent.push({\n type: 'tool_use',\n id: tc.toolCallId,\n name: tc.toolName,\n input: tc.args,\n });\n }\n llmMessages.push({ role: 'assistant', content: assistantContent });\n } else {\n llmMessages.push({ role: 'assistant', content: chunkText });\n break;\n }\n\n // Step 9: Pre-flight hooks → execute non-rejected tools → collect all results\n\n // Phase 30.2 — tools call ctx.emit() during execution; AsyncGenerator can't\n // yield from a sync callback, so we buffer per-batch then drain after\n // executeParallel resolves. Order is preserved (insertion = call order).\n const progressBuffer: Array<{\n toolName: string;\n message: string;\n percent?: number;\n audience: 'internal' | 'user';\n }> = [];\n\n const scopedStorage = this.buildScopedStorage(personality);\n\n const toolCtxBase = {\n sessionId,\n sessionKey,\n platform: this.platform,\n workingDir: this.workingDir,\n agentId: opts.agentId,\n personalityId: personality.id,\n memoryScope: personality.memoryScope,\n currentTurn: turnCount,\n messageCount: allMessages.length + turnCount,\n abortSignal,\n emit: (event: {\n type: 'progress';\n toolName: string;\n message: string;\n percent?: number;\n audience?: 'internal' | 'user';\n }) => {\n progressBuffer.push({\n toolName: event.toolName,\n message: event.message,\n ...(event.percent !== undefined && { percent: event.percent }),\n audience: event.audience ?? 'internal',\n });\n },\n resultBudgetChars: this.resultBudgetChars,\n ...(scopedStorage ? { storage: scopedStorage } : {}),\n };\n\n // Run before_tool_call hooks; build exec list with effective args\n // Rejected tools get tool_end ok:false + an error tool_result sent back to LLM\n type Prepped = { toolCallId: string; name: string; args: unknown; rejected?: string };\n const prepped: Prepped[] = [];\n\n for (const tc of completedToolCalls) {\n const beforeResult = await this.hooks.fireModifying(\n 'before_tool_call',\n {\n sessionId,\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: tc.args,\n },\n allowedPlugins,\n );\n\n if (beforeResult.error) {\n yield {\n type: 'tool_end',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n ok: false,\n durationMs: 0,\n result: beforeResult.error,\n };\n prepped.push({\n toolCallId: tc.toolCallId,\n name: tc.toolName,\n args: tc.args,\n rejected: beforeResult.error,\n });\n continue;\n }\n\n const effectiveArgs = beforeResult.args ?? tc.args;\n yield {\n type: 'tool_start',\n toolCallId: tc.toolCallId,\n toolName: tc.toolName,\n args: effectiveArgs,\n };\n prepped.push({ toolCallId: tc.toolCallId, name: tc.toolName, args: effectiveArgs });\n }\n\n // Execute only non-rejected tools; results keyed by toolCallId\n const execInputs = prepped\n .filter((p) => p.rejected === undefined)\n .map((p) => ({ toolCallId: p.toolCallId, name: p.name, args: p.args }));\n\n const startedAt = Date.now();\n const execResults =\n execInputs.length > 0\n ? await this.tools.executeParallel(execInputs, toolCtxBase, allowedTools, filterOpts)\n : [];\n const execResultMap = new Map(execResults.map((r) => [r.toolCallId, r]));\n\n // Drain any progress events tools emitted during execution. Order is\n // call-order (across the parallel batch) — close enough for users; the\n // exact interleaving doesn't matter when ctx.emit is best-effort.\n for (const ev of progressBuffer) {\n yield { type: 'tool_progress', ...ev };\n }\n progressBuffer.length = 0;\n\n // Persist results + emit tool_end + build tool_result content blocks (original order)\n const toolResultContent: MessageContent[] = [];\n\n for (const p of prepped) {\n const durationMs = Date.now() - startedAt;\n let result: ToolResult;\n\n if (p.rejected !== undefined) {\n result = { ok: false, error: p.rejected, code: 'execution_failed' };\n // tool_end already emitted above; no after_tool_call hook for blocked tools\n } else {\n const execResult = execResultMap.get(p.toolCallId);\n result = execResult?.result ?? {\n ok: false,\n error: 'Tool result missing',\n code: 'execution_failed',\n };\n yield {\n type: 'tool_end',\n toolCallId: p.toolCallId,\n toolName: p.name,\n ok: result.ok,\n durationMs,\n result: result.ok ? result.value : result.error,\n };\n await this.hooks.fireVoid(\n 'after_tool_call',\n {\n sessionId,\n toolName: p.name,\n result,\n durationMs,\n },\n allowedPlugins,\n );\n }\n\n // Persist every result (rejected or not) so history matches what LLM sees\n await this.session.appendMessage({\n sessionId,\n role: 'tool_result',\n content: result.ok ? result.value : result.error,\n toolCallId: p.toolCallId,\n toolName: p.name,\n });\n\n toolResultContent.push({\n type: 'tool_result',\n tool_use_id: p.toolCallId,\n content: result.ok ? result.value : result.error,\n is_error: !result.ok,\n });\n }\n\n // Feed all tool results back to LLM as a single user message with content blocks\n llmMessages.push({ role: 'user', content: toolResultContent });\n }\n\n // Step 10: Memory writes flow through the `memory_save` tool during the\n // turn (see extensions/tools-memory). The agent-loop itself produces no\n // updates, so there's nothing to sync here.\n\n // Step 11: Update usage\n await this.session.updateUsage(sessionId, { apiCallCount: turnCount });\n\n // Step 12: Fire agent_done\n await this.hooks.fireVoid('agent_done', { sessionId, text: fullText, turnCount }, allowedPlugins);\n\n yield { type: 'done', text: fullText, turnCount };\n }\n\n private *handleChunk(\n chunk: CompletionChunk,\n pendingToolCalls: Array<{\n toolCallId: string;\n toolName: string;\n partialJson: string;\n args?: unknown;\n }>,\n onText: (t: string) => void,\n ): Generator<AgentEvent> {\n switch (chunk.type) {\n case 'text_delta':\n onText(chunk.text);\n yield { type: 'text_delta', text: chunk.text };\n break;\n\n case 'thinking_delta':\n yield { type: 'thinking_delta', thinking: chunk.thinking };\n break;\n\n case 'tool_use_start':\n pendingToolCalls.push({\n toolCallId: chunk.toolCallId,\n toolName: chunk.toolName,\n partialJson: '',\n });\n break;\n\n case 'tool_use_delta': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) tc.partialJson += chunk.partialJson;\n break;\n }\n\n case 'tool_use_end': {\n const tc = pendingToolCalls.find((t) => t.toolCallId === chunk.toolCallId);\n if (tc) {\n try {\n tc.args = JSON.parse(chunk.inputJson || tc.partialJson);\n } catch {\n tc.args = {};\n }\n }\n break;\n }\n\n case 'usage':\n yield {\n type: 'usage',\n inputTokens: chunk.usage.inputTokens,\n outputTokens: chunk.usage.outputTokens,\n estimatedCostUsd: chunk.usage.estimatedCostUsd,\n };\n break;\n\n case 'done':\n // finishReason available here for future context-compaction (Phase 3)\n break;\n }\n }\n\n // Reconstruct LLM-ready messages from stored history.\n // Assistant messages with tool calls produce proper tool_use content blocks.\n // Consecutive tool_result rows are grouped into a single user message.\n private toLLMMessages(stored: StoredMessage[]): Message[] {\n const messages: Message[] = [];\n\n for (const msg of stored) {\n if (msg.role === 'system') continue;\n\n if (msg.role === 'user') {\n messages.push({ role: 'user', content: msg.content });\n } else if (msg.role === 'assistant') {\n if (msg.toolCalls && msg.toolCalls.length > 0) {\n const content: MessageContent[] = [];\n if (msg.content) content.push({ type: 'text', text: msg.content });\n for (const tc of msg.toolCalls) {\n content.push({ type: 'tool_use', id: tc.id, name: tc.name, input: tc.input });\n }\n messages.push({ role: 'assistant', content });\n } else {\n messages.push({ role: 'assistant', content: msg.content });\n }\n } else if (msg.role === 'tool_result') {\n const resultBlock: MessageContent = {\n type: 'tool_result',\n tool_use_id: msg.toolCallId ?? '',\n content: msg.content,\n is_error: false,\n };\n const last = messages[messages.length - 1];\n // Append to existing tool_result user message, or start a new one\n if (last?.role === 'user' && Array.isArray(last.content)) {\n (last.content as MessageContent[]).push(resultBlock);\n } else {\n messages.push({ role: 'user', content: [resultBlock] });\n }\n }\n }\n\n return messages;\n }\n\n // ---------------------------------------------------------------------------\n // Per-turn ScopedStorage construction (Phase 4 — fs_reach enforcement).\n //\n // When the AgentLoop was wired with `storage` + `dataDir`, build a\n // ScopedStorage decorated with the active personality's `fs_reach`\n // allowlist for this turn. Substitutions (${ETHOS_HOME} / ${self} /\n // ${CWD}) are resolved here so the underlying storage-fs class stays\n // pristine. When `fs_reach` is unset, fall back to a sensible default:\n // read: [<ethosHome>/personalities/<self>/, <ethosHome>/skills/, <cwd>]\n // write: [<ethosHome>/personalities/<self>/, <cwd>]\n // ---------------------------------------------------------------------------\n\n private buildScopedStorage(personality: PersonalityConfig): Storage | undefined {\n if (!this.storage) return undefined;\n\n const ethosHome = this.dataDir ?? join(homedir(), '.ethos');\n const cwd = this.workingDir;\n const self = personality.id;\n const ownDir = `${join(ethosHome, 'personalities', self)}/`;\n\n const fsReach = personality.fs_reach;\n const readPrefixes =\n fsReach?.read && fsReach.read.length > 0\n ? fsReach.read.map((p) => substitute(p, { ethosHome, self, cwd }))\n : [ownDir, `${join(ethosHome, 'skills')}/`, cwd];\n const writePrefixes =\n fsReach?.write && fsReach.write.length > 0\n ? fsReach.write.map((p) => substitute(p, { ethosHome, self, cwd }))\n : [ownDir, cwd];\n\n return new ScopedStorage(this.storage, { read: readPrefixes, write: writePrefixes });\n }\n}\n\nfunction substitute(\n template: string,\n vars: { ethosHome: string; self: string; cwd: string },\n): string {\n return template\n .replace(/\\$\\{ETHOS_HOME\\}/g, vars.ethosHome)\n .replace(/\\$\\{self\\}/g, vars.self)\n .replace(/\\$\\{CWD\\}/g, vars.cwd);\n}\n","import {\n appendFile,\n mkdir,\n readdir,\n readFile,\n rename,\n rm,\n stat,\n writeFile,\n} from 'node:fs/promises';\nimport type {\n Storage,\n StorageDirEntry,\n StorageRemoveOptions,\n StorageWriteOptions,\n} from '@ethosagent/types';\n\nexport class FsStorage implements Storage {\n async read(path: string): Promise<string | null> {\n try {\n return await readFile(path, 'utf-8');\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;\n throw err;\n }\n }\n\n async exists(path: string): Promise<boolean> {\n try {\n await stat(path);\n return true;\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return false;\n throw err;\n }\n }\n\n async mtime(path: string): Promise<number | null> {\n try {\n const s = await stat(path);\n return s.mtimeMs;\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return null;\n throw err;\n }\n }\n\n async list(dir: string): Promise<string[]> {\n try {\n return await readdir(dir);\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return [];\n throw err;\n }\n }\n\n async listEntries(dir: string): Promise<StorageDirEntry[]> {\n try {\n const entries = await readdir(dir, { withFileTypes: true });\n return entries.map((e) => ({ name: e.name, isDir: e.isDirectory() }));\n } catch (err) {\n if ((err as NodeJS.ErrnoException).code === 'ENOENT') return [];\n throw err;\n }\n }\n\n async write(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n if (opts?.mode !== undefined) {\n await writeFile(path, content, { encoding: 'utf-8', mode: opts.mode });\n return;\n }\n await writeFile(path, content, 'utf-8');\n }\n\n async append(path: string, content: string): Promise<void> {\n await appendFile(path, content, 'utf-8');\n }\n\n async writeAtomic(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n const tmp = `${path}.tmp.${process.pid}.${Date.now()}`;\n if (opts?.mode !== undefined) {\n await writeFile(tmp, content, { encoding: 'utf-8', mode: opts.mode });\n } else {\n await writeFile(tmp, content, 'utf-8');\n }\n try {\n await rename(tmp, path);\n } catch (err) {\n // Best-effort cleanup of temp file on rename failure.\n try {\n await rm(tmp, { force: true });\n } catch {\n // ignore\n }\n throw err;\n }\n }\n\n async mkdir(dir: string): Promise<void> {\n await mkdir(dir, { recursive: true });\n }\n\n async remove(path: string, opts?: StorageRemoveOptions): Promise<void> {\n await rm(path, { recursive: opts?.recursive === true });\n }\n\n async rename(from: string, to: string): Promise<void> {\n await rename(from, to);\n }\n}\n","import { dirname } from 'node:path';\nimport type {\n Storage,\n StorageDirEntry,\n StorageRemoveOptions,\n StorageWriteOptions,\n} from '@ethosagent/types';\n\ninterface FileNode {\n type: 'file';\n content: string;\n mode?: number;\n mtimeMs: number;\n}\n\ninterface DirNode {\n type: 'dir';\n mtimeMs: number;\n}\n\ntype Node = FileNode | DirNode;\n\n/**\n * In-memory Storage implementation for tests. Mirrors fs semantics closely:\n *\n * - read/exists/mtime return null for missing paths.\n * - write requires the parent directory to exist (throws ENOENT otherwise) —\n * matches fs.writeFile and forces tests to mkdir first, just like prod.\n * - mkdir is always recursive; no-op on existing dirs; throws on file conflict.\n * - remove without recursive throws on missing paths and on non-empty dirs.\n * - writeAtomic round-trips through a temp key, identical to fs flow.\n *\n * mtime ticks forward on every write so cache-by-mtime patterns work in tests\n * without sleeping.\n */\nexport class InMemoryStorage implements Storage {\n // Absolute path → node\n private readonly nodes = new Map<string, Node>();\n private clock = 0;\n\n // Treat the filesystem root as always existing so consumers don't need to\n // mkdir('/') before writing to a file at the root.\n private isRootLike(path: string): boolean {\n return path === '/' || /^[A-Za-z]:[\\\\/]?$/.test(path);\n }\n\n private nextMtime(): number {\n this.clock += 1;\n return this.clock;\n }\n\n private getNode(path: string): Node | undefined {\n return this.nodes.get(path);\n }\n\n private requireParentDir(path: string): void {\n const parent = dirname(path);\n if (parent === path) return;\n if (this.isRootLike(parent)) return;\n const node = this.nodes.get(parent);\n if (!node) {\n const err = new Error(`ENOENT: no such file or directory, open '${path}'`);\n (err as NodeJS.ErrnoException).code = 'ENOENT';\n throw err;\n }\n if (node.type !== 'dir') {\n const err = new Error(`ENOTDIR: not a directory, open '${path}'`);\n (err as NodeJS.ErrnoException).code = 'ENOTDIR';\n throw err;\n }\n }\n\n async read(path: string): Promise<string | null> {\n const node = this.getNode(path);\n if (!node) return null;\n if (node.type === 'dir') {\n const err = new Error(`EISDIR: illegal operation on a directory, read '${path}'`);\n (err as NodeJS.ErrnoException).code = 'EISDIR';\n throw err;\n }\n return node.content;\n }\n\n async exists(path: string): Promise<boolean> {\n return this.nodes.has(path);\n }\n\n async mtime(path: string): Promise<number | null> {\n const node = this.getNode(path);\n return node ? node.mtimeMs : null;\n }\n\n async list(dir: string): Promise<string[]> {\n const node = this.getNode(dir);\n if (!node) return [];\n if (node.type !== 'dir') {\n const err = new Error(`ENOTDIR: not a directory, scandir '${dir}'`);\n (err as NodeJS.ErrnoException).code = 'ENOTDIR';\n throw err;\n }\n const prefix = dir.endsWith('/') ? dir : `${dir}/`;\n const names: string[] = [];\n for (const key of this.nodes.keys()) {\n if (key === dir) continue;\n if (!key.startsWith(prefix)) continue;\n const rest = key.slice(prefix.length);\n if (rest.includes('/')) continue;\n names.push(rest);\n }\n return names.sort();\n }\n\n async listEntries(dir: string): Promise<StorageDirEntry[]> {\n const names = await this.list(dir);\n const prefix = dir.endsWith('/') ? dir : `${dir}/`;\n return names.map((name) => {\n const node = this.nodes.get(prefix + name);\n return { name, isDir: node?.type === 'dir' };\n });\n }\n\n async write(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n const existing = this.nodes.get(path);\n if (existing && existing.type === 'dir') {\n const err = new Error(`EISDIR: illegal operation on a directory, write '${path}'`);\n (err as NodeJS.ErrnoException).code = 'EISDIR';\n throw err;\n }\n this.requireParentDir(path);\n const node: FileNode = {\n type: 'file',\n content,\n mtimeMs: this.nextMtime(),\n };\n if (opts?.mode !== undefined) node.mode = opts.mode;\n this.nodes.set(path, node);\n }\n\n async writeAtomic(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n // Same observable end-state as write — atomicity is a property of the\n // backing store; the in-memory map is single-step by definition.\n await this.write(path, content, opts);\n }\n\n async append(path: string, content: string): Promise<void> {\n const existing = this.nodes.get(path);\n if (existing && existing.type === 'dir') {\n const err = new Error(`EISDIR: illegal operation on a directory, append '${path}'`);\n (err as NodeJS.ErrnoException).code = 'EISDIR';\n throw err;\n }\n if (!existing) {\n // appendFile creates the file if missing — match that semantics.\n this.requireParentDir(path);\n this.nodes.set(path, {\n type: 'file',\n content,\n mtimeMs: this.nextMtime(),\n });\n return;\n }\n this.nodes.set(path, {\n ...existing,\n content: existing.content + content,\n mtimeMs: this.nextMtime(),\n });\n }\n\n async mkdir(dir: string): Promise<void> {\n if (this.isRootLike(dir)) return;\n const existing = this.nodes.get(dir);\n if (existing) {\n if (existing.type === 'dir') return;\n const err = new Error(`EEXIST: file already exists, mkdir '${dir}'`);\n (err as NodeJS.ErrnoException).code = 'EEXIST';\n throw err;\n }\n // Recursively create parents.\n const parent = dirname(dir);\n if (parent !== dir && !this.isRootLike(parent) && !this.nodes.has(parent)) {\n await this.mkdir(parent);\n }\n this.nodes.set(dir, { type: 'dir', mtimeMs: this.nextMtime() });\n }\n\n async remove(path: string, opts?: StorageRemoveOptions): Promise<void> {\n const node = this.nodes.get(path);\n if (!node) {\n const err = new Error(`ENOENT: no such file or directory, remove '${path}'`);\n (err as NodeJS.ErrnoException).code = 'ENOENT';\n throw err;\n }\n if (node.type === 'file') {\n this.nodes.delete(path);\n return;\n }\n // Directory.\n const prefix = path.endsWith('/') ? path : `${path}/`;\n const children: string[] = [];\n for (const key of this.nodes.keys()) {\n if (key !== path && key.startsWith(prefix)) children.push(key);\n }\n if (children.length > 0 && opts?.recursive !== true) {\n const err = new Error(`ENOTEMPTY: directory not empty, remove '${path}'`);\n (err as NodeJS.ErrnoException).code = 'ENOTEMPTY';\n throw err;\n }\n for (const key of children) this.nodes.delete(key);\n this.nodes.delete(path);\n }\n\n async rename(from: string, to: string): Promise<void> {\n const node = this.nodes.get(from);\n if (!node) {\n const err = new Error(`ENOENT: no such file or directory, rename '${from}' -> '${to}'`);\n (err as NodeJS.ErrnoException).code = 'ENOENT';\n throw err;\n }\n this.requireParentDir(to);\n\n if (node.type === 'file') {\n const target = this.nodes.get(to);\n if (target?.type === 'dir') {\n const err = new Error(`EISDIR: cannot rename file onto directory, '${from}' -> '${to}'`);\n (err as NodeJS.ErrnoException).code = 'EISDIR';\n throw err;\n }\n this.nodes.delete(from);\n this.nodes.set(to, { ...node, mtimeMs: this.nextMtime() });\n return;\n }\n\n // Directory rename — move the directory itself plus every descendant.\n const targetExisting = this.nodes.get(to);\n if (targetExisting) {\n const err = new Error(`EEXIST: target exists, rename '${from}' -> '${to}'`);\n (err as NodeJS.ErrnoException).code = 'EEXIST';\n throw err;\n }\n const fromPrefix = from.endsWith('/') ? from : `${from}/`;\n const moves: Array<[string, string]> = [[from, to]];\n for (const key of this.nodes.keys()) {\n if (key !== from && key.startsWith(fromPrefix)) {\n moves.push([key, to + key.slice(from.length)]);\n }\n }\n for (const [src, dst] of moves) {\n const n = this.nodes.get(src);\n if (!n) continue;\n this.nodes.delete(src);\n this.nodes.set(dst, { ...n, mtimeMs: this.nextMtime() });\n }\n }\n\n // --- Test helpers -----------------------------------------------------\n\n /** Drop all state. Useful for `beforeEach` resets without re-instantiating. */\n reset(): void {\n this.nodes.clear();\n this.clock = 0;\n }\n\n /** Return the recorded mode for a file (undefined if no mode was set). */\n getMode(path: string): number | undefined {\n const node = this.nodes.get(path);\n if (!node || node.type !== 'file') return undefined;\n return node.mode;\n }\n}\n","import {\n BoundaryError,\n type Storage,\n type StorageDirEntry,\n type StorageRemoveOptions,\n type StorageWriteOptions,\n} from '@ethosagent/types';\n\nexport interface ScopedStorageScope {\n /** Absolute path prefixes that may be read. */\n read: readonly string[];\n /** Absolute path prefixes that may be written / mutated. */\n write: readonly string[];\n}\n\n/**\n * Decorator over Storage that enforces a per-scope read/write allowlist.\n * Used by tools-file to bound a personality's filesystem reach to its own\n * directory + cwd (Phase 4 of the storage abstraction plan).\n *\n * A path is permitted if any allowed prefix is a prefix of the absolute path.\n * Prefixes are matched literally — there is no glob expansion. Pass paths\n * that end in `/` for directory scopes; ScopedStorage normalizes them so\n * `/a/b` does not also match `/a/bc/`.\n */\nexport class ScopedStorage implements Storage {\n private readonly readPrefixes: string[];\n private readonly writePrefixes: string[];\n\n constructor(\n private readonly inner: Storage,\n scope: ScopedStorageScope,\n ) {\n this.readPrefixes = scope.read.map(normalizePrefix);\n this.writePrefixes = scope.write.map(normalizePrefix);\n }\n\n private check(path: string, kind: 'read' | 'write'): void {\n const allowed = kind === 'read' ? this.readPrefixes : this.writePrefixes;\n if (!isPathAllowed(path, allowed)) {\n throw new BoundaryError(kind, path, allowed);\n }\n }\n\n async read(path: string): Promise<string | null> {\n this.check(path, 'read');\n return this.inner.read(path);\n }\n\n async exists(path: string): Promise<boolean> {\n this.check(path, 'read');\n return this.inner.exists(path);\n }\n\n async mtime(path: string): Promise<number | null> {\n this.check(path, 'read');\n return this.inner.mtime(path);\n }\n\n async list(dir: string): Promise<string[]> {\n this.check(dir, 'read');\n return this.inner.list(dir);\n }\n\n async listEntries(dir: string): Promise<StorageDirEntry[]> {\n this.check(dir, 'read');\n return this.inner.listEntries(dir);\n }\n\n async write(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n this.check(path, 'write');\n return this.inner.write(path, content, opts);\n }\n\n async append(path: string, content: string): Promise<void> {\n this.check(path, 'write');\n return this.inner.append(path, content);\n }\n\n async writeAtomic(path: string, content: string, opts?: StorageWriteOptions): Promise<void> {\n this.check(path, 'write');\n return this.inner.writeAtomic(path, content, opts);\n }\n\n async mkdir(dir: string): Promise<void> {\n this.check(dir, 'write');\n return this.inner.mkdir(dir);\n }\n\n async remove(path: string, opts?: StorageRemoveOptions): Promise<void> {\n this.check(path, 'write');\n return this.inner.remove(path, opts);\n }\n\n async rename(from: string, to: string): Promise<void> {\n this.check(from, 'write');\n this.check(to, 'write');\n return this.inner.rename(from, to);\n }\n}\n\nfunction normalizePrefix(prefix: string): string {\n // A prefix matches any path where prefix is followed by '/' or end-of-string,\n // OR where the path equals the prefix exactly. We keep the prefix as-given\n // (with or without trailing slash) and handle the boundary in isPathAllowed.\n return prefix;\n}\n\nfunction isPathAllowed(path: string, prefixes: readonly string[]): boolean {\n for (const prefix of prefixes) {\n if (path === prefix) return true;\n const withoutSlash = prefix.endsWith('/') ? prefix.slice(0, -1) : prefix;\n // Allow the directory itself (without trailing slash) — needed so\n // `mkdir(<personality-dir>)` and `list(<personality-dir>)` succeed\n // when the configured prefix has a trailing slash.\n if (path === withoutSlash) return true;\n const withSlash = `${withoutSlash}/`;\n if (path.startsWith(withSlash)) return true;\n }\n return false;\n}\n","import type {\n SearchResult,\n Session,\n SessionFilter,\n SessionStore,\n SessionUsage,\n StoredMessage,\n} from '@ethosagent/types';\n\nexport class InMemorySessionStore implements SessionStore {\n private sessions = new Map<string, Session>();\n private messages = new Map<string, StoredMessage[]>();\n private idCounter = 0;\n\n async createSession(data: Omit<Session, 'id' | 'createdAt' | 'updatedAt'>): Promise<Session> {\n const session: Session = {\n ...data,\n id: `session_${++this.idCounter}`,\n createdAt: new Date(),\n updatedAt: new Date(),\n };\n this.sessions.set(session.id, session);\n this.messages.set(session.id, []);\n return session;\n }\n\n async getSession(id: string): Promise<Session | null> {\n return this.sessions.get(id) ?? null;\n }\n\n async getSessionByKey(key: string): Promise<Session | null> {\n for (const s of this.sessions.values()) {\n if (s.key === key) return s;\n }\n return null;\n }\n\n async updateSession(id: string, patch: Partial<Session>): Promise<void> {\n const session = this.sessions.get(id);\n if (!session) throw new Error(`Session not found: ${id}`);\n this.sessions.set(id, { ...session, ...patch, updatedAt: new Date() });\n }\n\n async deleteSession(id: string): Promise<void> {\n this.sessions.delete(id);\n this.messages.delete(id);\n }\n\n async listSessions(filter?: SessionFilter): Promise<Session[]> {\n let results = [...this.sessions.values()];\n if (filter?.platform) results = results.filter((s) => s.platform === filter.platform);\n if (filter?.personalityId)\n results = results.filter((s) => s.personalityId === filter.personalityId);\n if (filter?.workingDir) results = results.filter((s) => s.workingDir === filter.workingDir);\n if (filter?.since) {\n const since = filter.since;\n results = results.filter((s) => s.createdAt >= since);\n }\n results.sort((a, b) => b.createdAt.getTime() - a.createdAt.getTime());\n const offset = filter?.offset ?? 0;\n const limit = filter?.limit ?? results.length;\n return results.slice(offset, offset + limit);\n }\n\n async appendMessage(data: Omit<StoredMessage, 'id' | 'timestamp'>): Promise<StoredMessage> {\n const message: StoredMessage = {\n ...data,\n id: `msg_${++this.idCounter}`,\n timestamp: new Date(),\n };\n const list = this.messages.get(data.sessionId) ?? [];\n list.push(message);\n this.messages.set(data.sessionId, list);\n return message;\n }\n\n async getMessages(\n sessionId: string,\n options?: { limit?: number; offset?: number },\n ): Promise<StoredMessage[]> {\n const all = this.messages.get(sessionId) ?? [];\n const offset = options?.offset ?? 0;\n // Return most-recent messages: trim from the tail, then skip `offset` from the end\n const end = all.length - offset;\n const start = options?.limit ? Math.max(0, end - options.limit) : 0;\n return all.slice(start, end);\n }\n\n async updateUsage(sessionId: string, delta: Partial<SessionUsage>): Promise<void> {\n const session = this.sessions.get(sessionId);\n if (!session) return;\n const usage = { ...session.usage };\n for (const [k, v] of Object.entries(delta) as [keyof SessionUsage, number][]) {\n (usage[k] as number) += v;\n }\n this.sessions.set(sessionId, { ...session, usage, updatedAt: new Date() });\n }\n\n async search(\n query: string,\n options?: { limit?: number; sessionId?: string },\n ): Promise<SearchResult[]> {\n const results: SearchResult[] = [];\n const lower = query.toLowerCase();\n for (const [sessionId, msgs] of this.messages.entries()) {\n if (options?.sessionId && sessionId !== options.sessionId) continue;\n for (const msg of msgs) {\n const idx = msg.content.toLowerCase().indexOf(lower);\n if (idx >= 0) {\n results.push({\n sessionId,\n messageId: msg.id,\n snippet: msg.content.slice(Math.max(0, idx - 50), idx + 150),\n score: 1,\n timestamp: msg.timestamp,\n });\n }\n }\n }\n results.sort((a, b) => b.timestamp.getTime() - a.timestamp.getTime());\n return results.slice(0, options?.limit ?? 20);\n }\n\n async pruneOldSessions(olderThan: Date): Promise<number> {\n let count = 0;\n for (const [id, session] of this.sessions.entries()) {\n if (session.updatedAt < olderThan) {\n this.sessions.delete(id);\n this.messages.delete(id);\n count++;\n }\n }\n return count;\n }\n\n async vacuum(): Promise<void> {\n // No-op for in-memory store\n }\n}\n","import type { MemoryLoadContext, MemoryProvider, MemoryUpdate } from '@ethosagent/types';\n\nexport class NoopMemoryProvider implements MemoryProvider {\n async prefetch(_ctx: MemoryLoadContext) {\n return null;\n }\n\n async sync(_ctx: MemoryLoadContext, _updates: MemoryUpdate[]): Promise<void> {\n // No-op\n }\n}\n","import type { PersonalityConfig, PersonalityRegistry } from '@ethosagent/types';\n\nconst DEFAULT_PERSONALITY: PersonalityConfig = {\n id: 'default',\n name: 'Default',\n description: 'Default Ethos personality',\n};\n\nexport class DefaultPersonalityRegistry implements PersonalityRegistry {\n private readonly personalities = new Map<string, PersonalityConfig>([\n ['default', DEFAULT_PERSONALITY],\n ]);\n private defaultId = 'default';\n\n define(config: PersonalityConfig): void {\n this.personalities.set(config.id, config);\n }\n\n get(id: string): PersonalityConfig | undefined {\n return this.personalities.get(id);\n }\n\n list(): PersonalityConfig[] {\n return [...this.personalities.values()];\n }\n\n getDefault(): PersonalityConfig {\n return this.personalities.get(this.defaultId) ?? DEFAULT_PERSONALITY;\n }\n\n setDefault(id: string): void {\n if (!this.personalities.has(id)) {\n throw new Error(`Unknown personality: ${id}`);\n }\n this.defaultId = id;\n }\n\n async loadFromDirectory(_dir: string): Promise<void> {\n // Implemented in extensions/personalities\n }\n\n remove(id: string): void {\n this.personalities.delete(id);\n }\n}\n","import type { ClaimingHooks, HookRegistry, ModifyingHooks, VoidHooks } from '@ethosagent/types';\n\ntype AnyHandler = (...args: unknown[]) => Promise<unknown>;\n\ninterface RegisteredHandler {\n handler: AnyHandler;\n pluginId?: string;\n failurePolicy: 'fail-open' | 'fail-closed';\n}\n\n/** Returns true when a handler should fire given the allowedPlugins filter. */\nfunction isAllowed(h: RegisteredHandler, allowedPlugins: string[] | undefined): boolean {\n if (allowedPlugins === undefined) return true; // no filter — fire all\n if (!h.pluginId) return true; // built-in handler — always fires\n return allowedPlugins.includes(h.pluginId);\n}\n\nexport class DefaultHookRegistry implements HookRegistry {\n private readonly voidHandlers = new Map<string, RegisteredHandler[]>();\n private readonly modifyingHandlers = new Map<string, RegisteredHandler[]>();\n private readonly claimingHandlers = new Map<string, RegisteredHandler[]>();\n\n registerVoid<K extends keyof VoidHooks>(\n name: K,\n handler: (payload: VoidHooks[K]) => Promise<void>,\n opts?: { pluginId?: string; failurePolicy?: 'fail-open' | 'fail-closed' },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: opts?.failurePolicy ?? 'fail-open',\n };\n const list = this.voidHandlers.get(name) ?? [];\n list.push(entry);\n this.voidHandlers.set(name, list);\n return () => this.remove(this.voidHandlers, name, entry);\n }\n\n registerModifying<K extends keyof ModifyingHooks>(\n name: K,\n handler: (payload: ModifyingHooks[K][0]) => Promise<Partial<ModifyingHooks[K][1]> | null>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.modifyingHandlers.get(name) ?? [];\n list.push(entry);\n this.modifyingHandlers.set(name, list);\n return () => this.remove(this.modifyingHandlers, name, entry);\n }\n\n registerClaiming<K extends keyof ClaimingHooks>(\n name: K,\n handler: (payload: ClaimingHooks[K][0]) => Promise<ClaimingHooks[K][1]>,\n opts?: { pluginId?: string },\n ): () => void {\n const entry: RegisteredHandler = {\n handler: handler as AnyHandler,\n pluginId: opts?.pluginId,\n failurePolicy: 'fail-open',\n };\n const list = this.claimingHandlers.get(name) ?? [];\n list.push(entry);\n this.claimingHandlers.set(name, list);\n return () => this.remove(this.claimingHandlers, name, entry);\n }\n\n // Void hooks: all handlers run in parallel via Promise.allSettled.\n // Failures are logged but never propagate (fail-open by default).\n // allowedPlugins gates plugin-registered handlers; built-in handlers always fire.\n async fireVoid<K extends keyof VoidHooks>(\n name: K,\n payload: VoidHooks[K],\n allowedPlugins?: string[],\n ): Promise<void> {\n const handlers = (this.voidHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n await Promise.allSettled(handlers.map((h) => h.handler(payload)));\n }\n\n // Modifying hooks: handlers run sequentially; results are merged (first non-null value per key wins).\n async fireModifying<K extends keyof ModifyingHooks>(\n name: K,\n payload: ModifyingHooks[K][0],\n allowedPlugins?: string[],\n ): Promise<ModifyingHooks[K][1]> {\n const handlers = (this.modifyingHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n const merged: Record<string, unknown> = {};\n for (const h of handlers) {\n try {\n const result = await h.handler(payload);\n if (result && typeof result === 'object') {\n for (const [k, v] of Object.entries(result)) {\n if (!(k in merged) && v !== null && v !== undefined) {\n merged[k] = v;\n }\n }\n }\n } catch {\n // fail-open: continue with other handlers\n }\n }\n return merged as ModifyingHooks[K][1];\n }\n\n // Claiming hooks: handlers run sequentially, stop after first { handled: true }.\n async fireClaiming<K extends keyof ClaimingHooks>(\n name: K,\n payload: ClaimingHooks[K][0],\n allowedPlugins?: string[],\n ): Promise<ClaimingHooks[K][1]> {\n const handlers = (this.claimingHandlers.get(name) ?? []).filter((h) =>\n isAllowed(h, allowedPlugins),\n );\n for (const h of handlers) {\n try {\n const result = (await h.handler(payload)) as ClaimingHooks[K][1];\n if (result && (result as { handled: boolean }).handled) {\n return result;\n }\n } catch {\n // fail-open: try next handler\n }\n }\n return { handled: false } as ClaimingHooks[K][1];\n }\n\n unregisterPlugin(pluginId: string): void {\n for (const map of [this.voidHandlers, this.modifyingHandlers, this.claimingHandlers]) {\n for (const [name, handlers] of map.entries()) {\n map.set(\n name,\n handlers.filter((h) => h.pluginId !== pluginId),\n );\n }\n }\n }\n\n private remove(\n map: Map<string, RegisteredHandler[]>,\n name: string,\n entry: RegisteredHandler,\n ): void {\n const list = map.get(name) ?? [];\n map.set(\n name,\n list.filter((h) => h !== entry),\n );\n }\n}\n","import type { Tool, ToolContext, ToolFilterOpts, ToolRegistry, ToolResult } from '@ethosagent/types';\n\ninterface ToolEntry {\n tool: Tool;\n pluginId?: string;\n}\n\n/** Extract MCP server name from `mcp__<server>__<tool>` naming convention. */\nfunction mcpServerName(toolName: string): string | undefined {\n if (!toolName.startsWith('mcp__')) return undefined;\n return toolName.split('__')[1];\n}\n\n/** Returns true when a tool passes the MCP server + plugin filters. */\nfunction passesFilter(entry: ToolEntry, filterOpts: ToolFilterOpts | undefined): boolean {\n if (!filterOpts) return true;\n\n const { allowedMcpServers, allowedPlugins } = filterOpts;\n\n // MCP server gate: MCP tools only appear when their server is in the allowlist.\n if (allowedMcpServers !== undefined) {\n const server = mcpServerName(entry.tool.name);\n if (server !== undefined && !allowedMcpServers.includes(server)) return false;\n }\n\n // Plugin gate: plugin tools only appear when their plugin is in the allowlist.\n if (allowedPlugins !== undefined && entry.pluginId !== undefined) {\n if (!allowedPlugins.includes(entry.pluginId)) return false;\n }\n\n return true;\n}\n\nexport class DefaultToolRegistry implements ToolRegistry {\n private readonly tools = new Map<string, ToolEntry>();\n\n register(tool: Tool, opts?: { pluginId?: string }): void {\n this.tools.set(tool.name, { tool, pluginId: opts?.pluginId });\n }\n\n registerAll(tools: Tool[]): void {\n for (const tool of tools) {\n this.register(tool);\n }\n }\n\n unregister(name: string): void {\n this.tools.delete(name);\n }\n\n get(name: string): Tool | undefined {\n return this.tools.get(name)?.tool;\n }\n\n getAvailable(): Tool[] {\n return [...this.tools.values()]\n .filter((e) => !e.tool.isAvailable || e.tool.isAvailable())\n .map((e) => e.tool);\n }\n\n getForToolset(toolset: string): Tool[] {\n return this.getAvailable().filter((t) => t.toolset === toolset);\n }\n\n toDefinitions(allowedTools?: string[], filterOpts?: ToolFilterOpts) {\n const entries = [...this.tools.values()].filter(\n (e) => !e.tool.isAvailable || e.tool.isAvailable(),\n );\n\n const filtered = entries.filter((e) => {\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(e.tool.name))\n return false;\n return passesFilter(e, filterOpts);\n });\n\n return filtered.map((e) => ({\n name: e.tool.name,\n description: e.tool.description,\n parameters: e.tool.schema,\n }));\n }\n\n // Runs all tool calls in parallel. Results are returned in input order.\n // Budget is split evenly across parallel calls; each result is post-trimmed to budget.\n // allowedTools + filterOpts enforce tool access at execution time (belt-and-suspenders).\n async executeParallel(\n calls: Array<{ toolCallId: string; name: string; args: unknown }>,\n ctx: ToolContext,\n allowedTools?: string[],\n filterOpts?: ToolFilterOpts,\n ): Promise<Array<{ toolCallId: string; name: string; result: ToolResult }>> {\n const perCallBudget = Math.floor(ctx.resultBudgetChars / Math.max(calls.length, 1));\n\n const results = await Promise.allSettled(\n calls.map(async (call) => {\n // Name-based allowlist check\n if (allowedTools && allowedTools.length > 0 && !allowedTools.includes(call.name)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const entry = this.tools.get(call.name);\n if (!entry) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Unknown tool: ${call.name}`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n // MCP server + plugin filter check\n if (!passesFilter(entry, filterOpts)) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not permitted for this personality`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n if (entry.tool.isAvailable && !entry.tool.isAvailable()) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: `Tool ${call.name} is not currently available`,\n code: 'not_available',\n } as ToolResult,\n };\n }\n\n const budget = Math.min(perCallBudget, entry.tool.maxResultChars ?? perCallBudget);\n const toolCtx: ToolContext = { ...ctx, resultBudgetChars: budget };\n\n try {\n const result = await entry.tool.execute(call.args, toolCtx);\n // Post-trim result to budget\n if (result.ok && result.value.length > budget) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: true,\n value: `${result.value.slice(0, budget)}\\n[truncated — ${result.value.length} chars total]`,\n } as ToolResult,\n };\n }\n return { toolCallId: call.toolCallId, name: call.name, result };\n } catch (err) {\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: err instanceof Error ? err.message : String(err),\n code: 'execution_failed',\n } as ToolResult,\n };\n }\n }),\n );\n\n // Unwrap settled results — always return, never throw\n return results.map((r, i) => {\n if (r.status === 'fulfilled') return r.value;\n const call = calls[i] ?? { toolCallId: 'unknown', name: 'unknown', args: {} };\n return {\n toolCallId: call.toolCallId,\n name: call.name,\n result: {\n ok: false,\n error: String(r.reason),\n code: 'execution_failed',\n } as ToolResult,\n };\n });\n }\n}\n","// Generic plugin registry — adapted from praxis PluginRegistry pattern.\n// Each subsystem (tools, channels, memory backends) gets its own instance.\n\nexport type PluginFactory<T, C = unknown> = (config: C) => T | null;\n\nexport class PluginRegistry<T, C = unknown> {\n private readonly factories = new Map<string, PluginFactory<T, C>>();\n\n register(type: string, factory: PluginFactory<T, C>): void {\n this.factories.set(type, factory);\n }\n\n create(type: string, config: C): T {\n const factory = this.factories.get(type);\n if (!factory) {\n throw new Error(\n `Unknown plugin type: \"${type}\". Registered: ${[...this.factories.keys()].join(', ')}`,\n );\n }\n const instance = factory(config);\n if (!instance) {\n throw new Error(`Plugin factory for \"${type}\" returned null`);\n }\n return instance;\n }\n\n has(type: string): boolean {\n return this.factories.has(type);\n }\n\n types(): string[] {\n return [...this.factories.keys()];\n }\n}\n"],"mappings":";AAAA,SAAS,eAAe;AACxB,SAAS,YAAY;;;ACDrB;AAAA,EACE;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,EACA;AAAA,OACK;;;ACTP,SAAS,eAAe;;;ACAxB;AAAA,EACE;AAAA,OAKK;AAmBA,IAAM,gBAAN,MAAuC;AAAA,EAI5C,YACmB,OACjB,OACA;AAFiB;AAGjB,SAAK,eAAe,MAAM,KAAK,IAAI,eAAe;AAClD,SAAK,gBAAgB,MAAM,MAAM,IAAI,eAAe;AAAA,EACtD;AAAA,EALmB;AAAA,EAJF;AAAA,EACA;AAAA,EAUT,MAAM,MAAc,MAA8B;AACxD,UAAM,UAAU,SAAS,SAAS,KAAK,eAAe,KAAK;AAC3D,QAAI,CAAC,cAAc,MAAM,OAAO,GAAG;AACjC,YAAM,IAAI,cAAc,MAAM,MAAM,OAAO;AAAA,IAC7C;AAAA,EACF;AAAA,EAEA,MAAM,KAAK,MAAsC;AAC/C,SAAK,MAAM,MAAM,MAAM;AACvB,WAAO,KAAK,MAAM,KAAK,IAAI;AAAA,EAC7B;AAAA,EAEA,MAAM,OAAO,MAAgC;AAC3C,SAAK,MAAM,MAAM,MAAM;AACvB,WAAO,KAAK,MAAM,O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,WAAO,CAAC,GAAG,KAAK,UAAU,KAAK,CAAC;AAAA,EAClC;AACF;","names":[]}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ethosagent/core",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
4
4
|
"private": false,
|
|
5
5
|
"type": "module",
|
|
6
6
|
"description": "Core agent loop, tool registry, and hook registry for the Ethos agent framework",
|
|
@@ -19,9 +19,12 @@
|
|
|
19
19
|
}
|
|
20
20
|
},
|
|
21
21
|
"dependencies": {
|
|
22
|
-
"@ethosagent/types": "0.
|
|
22
|
+
"@ethosagent/types": "0.2.0"
|
|
23
|
+
},
|
|
24
|
+
"devDependencies": {
|
|
25
|
+
"@ethosagent/storage-fs": "0.0.0"
|
|
23
26
|
},
|
|
24
27
|
"scripts": {
|
|
25
|
-
"build": "tsup
|
|
28
|
+
"build": "tsup"
|
|
26
29
|
}
|
|
27
30
|
}
|