pi-vault-mind 0.14.2 → 0.16.3
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/CHANGELOG.md +293 -0
- package/README.md +5 -5
- package/agents/broadcaster.agent.md +2 -2
- package/agents/heavy-lifter.agent.md +1 -1
- package/agents/main.agent.md +29 -0
- package/agents/manager.agent.md +1 -1
- package/agents/miner.agent.md +1 -1
- package/dist/packages/obsidian/src/client.js +331 -0
- package/dist/packages/obsidian/src/types.js +1 -0
- package/dist/src/activity.d.ts +49 -0
- package/dist/src/activity.js +175 -0
- package/dist/src/agent-bus/Agent.d.ts +9 -1
- package/dist/src/agent-bus/Agent.js +18 -2
- package/dist/src/agent-bus/index.d.ts +5 -5
- package/dist/src/agent-bus/index.js +1 -1
- package/dist/src/agent-queue.js +12 -3
- package/dist/src/agents/BroadcasterAgent.d.ts +1 -1
- package/dist/src/agents/BroadcasterAgent.js +2 -2
- package/dist/src/agents/HeavyLifterAgent.d.ts +1 -1
- package/dist/src/agents/HeavyLifterAgent.js +2 -2
- package/dist/src/agents/ManagerAgent.js +1 -1
- package/dist/src/agents/MinerAgent.d.ts +1 -1
- package/dist/src/agents/MinerAgent.js +2 -2
- package/dist/src/agents/index.d.ts +2 -2
- package/dist/src/agents/index.js +2 -2
- package/dist/src/auth.d.ts +17 -15
- package/dist/src/auth.js +26 -37
- package/dist/src/autosync.js +2 -2
- package/dist/src/commands.d.ts +2 -19
- package/dist/src/commands.js +301 -231
- package/dist/src/config-keys.d.ts +16 -0
- package/dist/src/config-keys.js +45 -0
- package/dist/src/discover-schema.js +4 -4
- package/dist/src/edits.d.ts +21 -0
- package/dist/src/edits.js +75 -0
- package/dist/src/embedding-probe.d.ts +34 -0
- package/dist/src/embedding-probe.js +142 -0
- package/dist/src/embedding-providers.d.ts +133 -0
- package/dist/src/embedding-providers.js +173 -0
- package/dist/src/embedding-secrets.d.ts +38 -0
- package/dist/src/embedding-secrets.js +291 -0
- package/dist/src/engine.d.ts +6 -0
- package/dist/src/engine.js +78 -6
- package/dist/src/events.js +7 -10
- package/dist/src/extension-packages.d.ts +9 -0
- package/dist/src/extension-packages.js +14 -0
- package/dist/src/git.d.ts +19 -0
- package/dist/src/git.js +78 -0
- package/dist/src/graph.d.ts +2 -1
- package/dist/src/graph.js +47 -13
- package/dist/src/identities-config.d.ts +6 -0
- package/dist/src/identities-config.js +39 -0
- package/dist/src/identity-injector.js +1 -1
- package/dist/src/index.js +9 -26
- package/dist/src/lance.d.ts +9 -0
- package/dist/src/lance.js +143 -203
- package/dist/src/modal-client.d.ts +16 -0
- package/dist/src/modal-client.js +16 -0
- package/dist/src/modal-config.d.ts +37 -14
- package/dist/src/modal-config.js +59 -32
- package/dist/src/model-router.d.ts +27 -0
- package/dist/src/model-router.js +67 -0
- package/dist/src/models.d.ts +42 -0
- package/dist/src/models.js +78 -0
- package/dist/src/personalize.js +17 -3
- package/dist/src/scaffold.d.ts +48 -0
- package/dist/src/scaffold.js +246 -0
- package/dist/src/server.d.ts +47 -4
- package/dist/src/server.js +988 -221
- package/dist/src/session-search.d.ts +63 -0
- package/dist/src/session-search.js +379 -0
- package/dist/src/settings-ui.d.ts +2 -2
- package/dist/src/settings-ui.js +76 -135
- package/dist/src/sync.d.ts +3 -3
- package/dist/src/sync.js +19 -15
- package/dist/src/tool-catalog.d.ts +18 -0
- package/dist/src/tool-catalog.js +232 -0
- package/dist/src/tools/marksman.js +1 -1
- package/dist/src/tools.js +111 -65
- package/dist/src/types.d.ts +127 -71
- package/dist/src/utils.d.ts +57 -6
- package/dist/src/utils.js +114 -28
- package/dist/src/vault-tools.d.ts +34 -0
- package/dist/src/vault-tools.js +99 -0
- package/dist/src/vault-writer.js +4 -1
- package/dist/src/vm-handlers.js +140 -43
- package/dist/src/watcher.js +9 -9
- package/dist/test/activity-tool.test.js +54 -0
- package/dist/test/agent-bus/Agent.test.js +174 -0
- package/dist/test/agent-bus/AgentLoader.test.js +180 -0
- package/dist/test/agent-bus/AgentRegistry.test.js +86 -0
- package/dist/test/agent-bus/LLMProvider.test.js +162 -0
- package/dist/test/agent-bus/TaskQueue.test.js +256 -0
- package/dist/test/agent-dispatch-context.test.js +73 -0
- package/dist/test/agent-queue.test.js +1 -1
- package/dist/test/agents/Agents.test.js +282 -0
- package/dist/test/auth.test.js +57 -56
- package/dist/test/bridge-identity.test.js +34 -3
- package/dist/test/classify-search-mode.test.js +51 -0
- package/dist/test/commands.test.js +410 -0
- package/dist/test/config-merge.test.js +98 -0
- package/dist/test/context-capture.test.js +56 -0
- package/dist/test/discover-schema.test.js +8 -5
- package/dist/test/dispatch.test.js +45 -91
- package/dist/test/edits.test.js +144 -0
- package/dist/test/embedding-probe.test.js +424 -0
- package/dist/test/embedding-providers.test.js +157 -0
- package/dist/test/embedding-secrets.test.js +210 -0
- package/dist/test/engine-identity.test.js +1 -1
- package/dist/test/events.test.js +675 -0
- package/dist/test/git-bridge.test.js +160 -0
- package/dist/test/graph.test.js +464 -0
- package/dist/test/helpers/server.js +81 -0
- package/dist/test/identity-injector.test.js +197 -0
- package/dist/test/index.test.js +5 -7
- package/dist/test/lance-modal.test.js +67 -2
- package/dist/test/lance.test.js +429 -0
- package/dist/test/modal-client.test.js +1 -1
- package/dist/test/modal-config.test.js +92 -79
- package/dist/test/model-readiness.test.js +122 -0
- package/dist/test/model-router.test.js +99 -0
- package/dist/test/obsidian-client.test.js +42 -0
- package/dist/test/personalize.test.js +1 -1
- package/dist/test/rest-models.test.js +153 -0
- package/dist/test/rest-queue.test.js +135 -87
- package/dist/test/rest-setup.test.js +625 -115
- package/dist/test/rest-vm.test.js +355 -94
- package/dist/test/server-cors.test.js +48 -0
- package/dist/test/server-ws.test.js +61 -34
- package/dist/test/session-crud.test.js +323 -0
- package/dist/test/session-search.test.js +139 -0
- package/dist/test/settings-ui.test.js +113 -4
- package/dist/test/sync.test.js +12 -12
- package/dist/test/tombstone.test.js +8 -10
- package/dist/test/tool-catalog.test.js +86 -0
- package/dist/test/utils.test.js +588 -0
- package/dist/test/vault-tools.test.js +103 -12
- package/dist/test/vault-writer.test.js +371 -0
- package/dist/test/vm-handlers.test.js +620 -0
- package/extension-packages.json +9 -0
- package/package.json +107 -90
- package/pi-vault-mind.config.example.json +24 -2
- package/scripts/check-deps.sh +35 -32
- package/scripts/cli-e2e.mjs +215 -0
- package/scripts/configuration-e2e.mjs +783 -0
- package/scripts/e2e-commands.mjs +391 -0
- package/scripts/e2e-ws.mjs +3 -2
- package/scripts/fetch-modal-token.sh +4 -3
- package/scripts/generate-config-keys-json.mjs +45 -0
- package/scripts/generate-extension-packages-json.mjs +55 -0
- package/scripts/pi-session-query +0 -0
- package/scripts/publish-obsidian-plugin.sh +0 -0
- package/scripts/reset-test-vault.sh +2 -2
- package/scripts/setup-test-config.sh +0 -0
- package/scripts/setup-vault-pi.sh +48 -9
- package/scripts/test-slash-commands.mjs +2 -2
- package/scripts/vault-pi.sh +12 -3
- package/skills/github-release-notes-file/SKILL.md +42 -0
- package/skills/obsidian-guided-test-session/SKILL.md +110 -0
- package/skills/obsidian-interactive-test/SKILL.md +88 -0
- package/skills/obsidian-plugin-publish/SKILL.md +58 -0
- package/skills/pi-vault-mind-release/SKILL.md +141 -0
- package/skills/pre-test-release/SKILL.md +116 -0
- package/dist/test/auto-indexer.test.js +0 -2
|
@@ -0,0 +1,174 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { describe, it, mock } from "node:test";
|
|
3
|
+
import { Agent } from "../../src/agent-bus/Agent.js";
|
|
4
|
+
import { LLMProvider } from "../../src/agent-bus/LLMProvider.js";
|
|
5
|
+
import { MessageBus } from "../../src/agent-bus/MessageBus.js";
|
|
6
|
+
class MockMessageBus extends MessageBus {
|
|
7
|
+
publishCalls = [];
|
|
8
|
+
subscriptions = new Map();
|
|
9
|
+
subscribe(topic, handler) {
|
|
10
|
+
if (!this.subscriptions.has(topic)) {
|
|
11
|
+
this.subscriptions.set(topic, new Set());
|
|
12
|
+
}
|
|
13
|
+
this.subscriptions.get(topic).add(handler);
|
|
14
|
+
}
|
|
15
|
+
unsubscribe(topic, handler) {
|
|
16
|
+
this.subscriptions.get(topic)?.delete(handler);
|
|
17
|
+
}
|
|
18
|
+
async publish(topic, senderId, payload) {
|
|
19
|
+
this.publishCalls.push({ topic, senderId, payload });
|
|
20
|
+
const handlers = this.subscriptions.get(topic);
|
|
21
|
+
if (handlers) {
|
|
22
|
+
const message = {
|
|
23
|
+
id: `msg-${this.publishCalls.length}`,
|
|
24
|
+
topic,
|
|
25
|
+
payload,
|
|
26
|
+
senderId,
|
|
27
|
+
timestamp: Date.now(),
|
|
28
|
+
};
|
|
29
|
+
const promises = Array.from(handlers).map((h) => h(message));
|
|
30
|
+
await Promise.allSettled(promises);
|
|
31
|
+
}
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
class MockLLMProvider extends LLMProvider {
|
|
35
|
+
response;
|
|
36
|
+
constructor(response) {
|
|
37
|
+
super({ model: "mock" });
|
|
38
|
+
this.response = response;
|
|
39
|
+
}
|
|
40
|
+
async prompt(_text, _options) {
|
|
41
|
+
return this.response;
|
|
42
|
+
}
|
|
43
|
+
}
|
|
44
|
+
// ---------------------------------------------------------------------------
|
|
45
|
+
// TestAgent — concrete subclass exposing protected members for testing
|
|
46
|
+
// ---------------------------------------------------------------------------
|
|
47
|
+
class TestAgent extends Agent {
|
|
48
|
+
lastMessage = null;
|
|
49
|
+
async tick() {
|
|
50
|
+
/* no-op */
|
|
51
|
+
}
|
|
52
|
+
async testHandleMessage(msg) {
|
|
53
|
+
await this.handleMessage(msg);
|
|
54
|
+
}
|
|
55
|
+
async testBroadcast(topic, payload) {
|
|
56
|
+
await this.broadcast(topic, payload);
|
|
57
|
+
}
|
|
58
|
+
async testSendDirect(target, payload) {
|
|
59
|
+
await this.sendDirect(target, payload);
|
|
60
|
+
}
|
|
61
|
+
async testAskLLM(promptText, jsonMode = true) {
|
|
62
|
+
return this.askLLM(promptText, jsonMode);
|
|
63
|
+
}
|
|
64
|
+
async testInvokePiTool(toolName, args, timeoutMs) {
|
|
65
|
+
return this.invokePiTool(toolName, args, timeoutMs);
|
|
66
|
+
}
|
|
67
|
+
}
|
|
68
|
+
// ---------------------------------------------------------------------------
|
|
69
|
+
// Helpers
|
|
70
|
+
// ---------------------------------------------------------------------------
|
|
71
|
+
function makeConfig(overrides) {
|
|
72
|
+
return { id: "test-agent-1", role: "tester", capabilities: ["test"], ...overrides };
|
|
73
|
+
}
|
|
74
|
+
// ---------------------------------------------------------------------------
|
|
75
|
+
// Tests
|
|
76
|
+
// ---------------------------------------------------------------------------
|
|
77
|
+
describe("Agent", () => {
|
|
78
|
+
// 1. Constructor stores config, bus, and optional llm
|
|
79
|
+
it("stores config, bus, and optional llm from constructor", () => {
|
|
80
|
+
const bus = new MockMessageBus();
|
|
81
|
+
const config = makeConfig();
|
|
82
|
+
const agent = new TestAgent(config, bus);
|
|
83
|
+
assert.equal(agent.config, config);
|
|
84
|
+
assert.equal(agent.config.id, "test-agent-1");
|
|
85
|
+
assert.equal(agent.config.role, "tester");
|
|
86
|
+
assert.deepEqual(agent.config.capabilities, ["test"]);
|
|
87
|
+
});
|
|
88
|
+
it("stores llm when provided", () => {
|
|
89
|
+
const bus = new MockMessageBus();
|
|
90
|
+
const llm = new MockLLMProvider("hello");
|
|
91
|
+
const agent = new TestAgent(makeConfig(), bus, llm);
|
|
92
|
+
assert.ok(agent.config);
|
|
93
|
+
});
|
|
94
|
+
it("subscribes to direct and broadcast topics on construction", () => {
|
|
95
|
+
const bus = new MockMessageBus();
|
|
96
|
+
new TestAgent(makeConfig({ id: "agent-42" }), bus);
|
|
97
|
+
assert.ok(bus.subscriptions.has("direct:agent-42"));
|
|
98
|
+
assert.ok(bus.subscriptions.has("broadcast"));
|
|
99
|
+
});
|
|
100
|
+
// 2. broadcast() calls bus.publish() with correct topic, agent ID, and payload
|
|
101
|
+
it("broadcast calls bus.publish with correct topic, sender, and payload", async () => {
|
|
102
|
+
const bus = new MockMessageBus();
|
|
103
|
+
const agent = new TestAgent(makeConfig({ id: "bcast-agent" }), bus);
|
|
104
|
+
await agent.testBroadcast("status", { ok: true });
|
|
105
|
+
assert.equal(bus.publishCalls.length, 1);
|
|
106
|
+
assert.equal(bus.publishCalls[0].topic, "status");
|
|
107
|
+
assert.equal(bus.publishCalls[0].senderId, "bcast-agent");
|
|
108
|
+
assert.deepEqual(bus.publishCalls[0].payload, { ok: true });
|
|
109
|
+
});
|
|
110
|
+
// 3. sendDirect() calls bus.publish() with direct:<target> topic
|
|
111
|
+
it("sendDirect calls bus.publish with direct:<target> topic", async () => {
|
|
112
|
+
const bus = new MockMessageBus();
|
|
113
|
+
const agent = new TestAgent(makeConfig({ id: "sender" }), bus);
|
|
114
|
+
await agent.testSendDirect("receiver", { msg: "hi" });
|
|
115
|
+
assert.equal(bus.publishCalls.length, 1);
|
|
116
|
+
assert.equal(bus.publishCalls[0].topic, "direct:receiver");
|
|
117
|
+
assert.equal(bus.publishCalls[0].senderId, "sender");
|
|
118
|
+
assert.deepEqual(bus.publishCalls[0].payload, { msg: "hi" });
|
|
119
|
+
});
|
|
120
|
+
// 4. handleMessage() default implementation is a no-op (doesn't throw)
|
|
121
|
+
it("handleMessage default implementation does not throw", async () => {
|
|
122
|
+
const bus = new MockMessageBus();
|
|
123
|
+
const agent = new TestAgent(makeConfig(), bus);
|
|
124
|
+
const msg = {
|
|
125
|
+
id: "msg-1",
|
|
126
|
+
topic: "test",
|
|
127
|
+
payload: {},
|
|
128
|
+
senderId: "other",
|
|
129
|
+
timestamp: Date.now(),
|
|
130
|
+
};
|
|
131
|
+
await assert.doesNotReject(() => agent.testHandleMessage(msg));
|
|
132
|
+
});
|
|
133
|
+
// 5. invokePiTool() — timeout path (mock bus never responds)
|
|
134
|
+
it("invokePiTool rejects after timeoutMs when no response arrives", async () => {
|
|
135
|
+
mock.timers.enable();
|
|
136
|
+
const bus = new MockMessageBus();
|
|
137
|
+
const agent = new TestAgent(makeConfig({ id: "pi-agent" }), bus);
|
|
138
|
+
const pending = agent.testInvokePiTool("test_tool", { input: 1 }, 100);
|
|
139
|
+
// Advance past the timeout
|
|
140
|
+
mock.timers.tick(150);
|
|
141
|
+
await assert.rejects(() => pending, /Pi tool invocation timed out for tool: 'test_tool'/);
|
|
142
|
+
mock.timers.reset();
|
|
143
|
+
});
|
|
144
|
+
// 6. askLLM() — jsonMode true returns parsed object
|
|
145
|
+
it("askLLM with jsonMode true returns parsed JSON object", async () => {
|
|
146
|
+
const bus = new MockMessageBus();
|
|
147
|
+
const llm = new MockLLMProvider('{"answer":42,"name":"test"}');
|
|
148
|
+
const agent = new TestAgent(makeConfig(), bus, llm);
|
|
149
|
+
const result = await agent.testAskLLM("prompt");
|
|
150
|
+
assert.deepEqual(result, { answer: 42, name: "test" });
|
|
151
|
+
});
|
|
152
|
+
// 6b. askLLM() — jsonMode false returns raw string
|
|
153
|
+
it("askLLM with jsonMode false returns raw string", async () => {
|
|
154
|
+
const bus = new MockMessageBus();
|
|
155
|
+
const llm = new MockLLMProvider("plain text response");
|
|
156
|
+
const agent = new TestAgent(makeConfig(), bus, llm);
|
|
157
|
+
const result = await agent.testAskLLM("prompt", false);
|
|
158
|
+
assert.equal(result, "plain text response");
|
|
159
|
+
});
|
|
160
|
+
// 6c. askLLM() — throws when no LLM provider attached
|
|
161
|
+
it("askLLM throws when no LLM provider is attached", async () => {
|
|
162
|
+
const bus = new MockMessageBus();
|
|
163
|
+
const agent = new TestAgent(makeConfig(), bus);
|
|
164
|
+
await assert.rejects(() => agent.testAskLLM("prompt"), /Agent test-agent-1 has no LLM provider attached/);
|
|
165
|
+
});
|
|
166
|
+
// 6d. askLLM() — jsonMode true returns null on invalid JSON
|
|
167
|
+
it("askLLM with jsonMode true returns null when LLM returns invalid JSON", async () => {
|
|
168
|
+
const bus = new MockMessageBus();
|
|
169
|
+
const llm = new MockLLMProvider("not valid json at all");
|
|
170
|
+
const agent = new TestAgent(makeConfig(), bus, llm);
|
|
171
|
+
const result = await agent.testAskLLM("prompt");
|
|
172
|
+
assert.equal(result, null);
|
|
173
|
+
});
|
|
174
|
+
});
|
|
@@ -0,0 +1,180 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import * as fs from "node:fs";
|
|
3
|
+
import { tmpdir } from "node:os";
|
|
4
|
+
import * as path from "node:path";
|
|
5
|
+
import { afterEach, beforeEach, describe, it } from "node:test";
|
|
6
|
+
import { Agent } from "../../src/agent-bus/Agent.js";
|
|
7
|
+
import { AgentLoader } from "../../src/agent-bus/AgentLoader.js";
|
|
8
|
+
import { AgentRegistry } from "../../src/agent-bus/AgentRegistry.js";
|
|
9
|
+
// ---------------------------------------------------------------------------
|
|
10
|
+
// Mocks
|
|
11
|
+
// ---------------------------------------------------------------------------
|
|
12
|
+
class MockMessageBus {
|
|
13
|
+
subscriptions = new Map();
|
|
14
|
+
subscribe(topic, handler) {
|
|
15
|
+
if (!this.subscriptions.has(topic)) {
|
|
16
|
+
this.subscriptions.set(topic, new Set());
|
|
17
|
+
}
|
|
18
|
+
this.subscriptions.get(topic).add(handler);
|
|
19
|
+
}
|
|
20
|
+
unsubscribe(topic, handler) {
|
|
21
|
+
this.subscriptions.get(topic)?.delete(handler);
|
|
22
|
+
}
|
|
23
|
+
async publish(_topic, _senderId, _payload) {
|
|
24
|
+
// no-op
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
class TestAgent extends Agent {
|
|
28
|
+
async tick() { }
|
|
29
|
+
}
|
|
30
|
+
// ---------------------------------------------------------------------------
|
|
31
|
+
// Helpers
|
|
32
|
+
// ---------------------------------------------------------------------------
|
|
33
|
+
function makeLoaderConfig(agentsDir, overrides) {
|
|
34
|
+
return {
|
|
35
|
+
agentsDirectory: agentsDir,
|
|
36
|
+
bus: new MockMessageBus(),
|
|
37
|
+
llmProviderFactory: async () => null,
|
|
38
|
+
...overrides,
|
|
39
|
+
};
|
|
40
|
+
}
|
|
41
|
+
function writeAgentFile(dir, name, frontmatter, body = "") {
|
|
42
|
+
const filePath = path.join(dir, `${name}.agent.md`);
|
|
43
|
+
const content = `---\n${frontmatter}\n---\n${body}`;
|
|
44
|
+
fs.writeFileSync(filePath, content, "utf8");
|
|
45
|
+
return filePath;
|
|
46
|
+
}
|
|
47
|
+
// ---------------------------------------------------------------------------
|
|
48
|
+
// Tests
|
|
49
|
+
// ---------------------------------------------------------------------------
|
|
50
|
+
describe("AgentLoader", () => {
|
|
51
|
+
let tempDir;
|
|
52
|
+
const registeredTypes = [];
|
|
53
|
+
beforeEach(() => {
|
|
54
|
+
tempDir = fs.mkdtempSync(path.join(tmpdir(), "agent-loader-test-"));
|
|
55
|
+
});
|
|
56
|
+
afterEach(() => {
|
|
57
|
+
// Clean up registry
|
|
58
|
+
for (const type of registeredTypes) {
|
|
59
|
+
AgentRegistry.register(type, TestAgent);
|
|
60
|
+
}
|
|
61
|
+
registeredTypes.length = 0;
|
|
62
|
+
// Clean up temp dir
|
|
63
|
+
fs.rmSync(tempDir, { recursive: true, force: true });
|
|
64
|
+
});
|
|
65
|
+
function registerType(type) {
|
|
66
|
+
AgentRegistry.register(type, TestAgent);
|
|
67
|
+
registeredTypes.push(type);
|
|
68
|
+
}
|
|
69
|
+
describe("constructor", () => {
|
|
70
|
+
it("stores config", () => {
|
|
71
|
+
const config = makeLoaderConfig("/some/path");
|
|
72
|
+
const loader = new AgentLoader(config);
|
|
73
|
+
const stored = loader.config;
|
|
74
|
+
assert.equal(stored.agentsDirectory, "/some/path");
|
|
75
|
+
});
|
|
76
|
+
});
|
|
77
|
+
describe("loadAgents()", () => {
|
|
78
|
+
it("parses a markdown file with YAML frontmatter and creates agent via registry", async () => {
|
|
79
|
+
registerType("test-agent");
|
|
80
|
+
writeAgentFile(tempDir, "my-agent", ["type: test-agent", "role: Tester", "capabilities: [test, debug]"].join("\n"));
|
|
81
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir));
|
|
82
|
+
const agents = await loader.loadAgents();
|
|
83
|
+
assert.equal(agents.length, 1);
|
|
84
|
+
assert.equal(agents[0].config.id, "my-agent");
|
|
85
|
+
assert.equal(agents[0].config.role, "Tester");
|
|
86
|
+
assert.deepEqual(agents[0].config.capabilities, ["test", "debug"]);
|
|
87
|
+
});
|
|
88
|
+
it("skips files without valid frontmatter (missing type)", async () => {
|
|
89
|
+
registerType("test-agent");
|
|
90
|
+
writeAgentFile(tempDir, "bad-agent", ["role: NoType", "capabilities: [x]"].join("\n"));
|
|
91
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir));
|
|
92
|
+
const agents = await loader.loadAgents();
|
|
93
|
+
assert.equal(agents.length, 0);
|
|
94
|
+
});
|
|
95
|
+
it("skips files where type is not registered", async () => {
|
|
96
|
+
writeAgentFile(tempDir, "unknown-agent", ["type: unregistered-type", "role: Ghost", "capabilities: []"].join("\n"));
|
|
97
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir));
|
|
98
|
+
const agents = await loader.loadAgents();
|
|
99
|
+
assert.equal(agents.length, 0);
|
|
100
|
+
});
|
|
101
|
+
it("returns empty array when directory does not exist", async () => {
|
|
102
|
+
const nonExistent = path.join(tempDir, "does-not-exist");
|
|
103
|
+
const loader = new AgentLoader(makeLoaderConfig(nonExistent));
|
|
104
|
+
const agents = await loader.loadAgents();
|
|
105
|
+
assert.deepEqual(agents, []);
|
|
106
|
+
});
|
|
107
|
+
it("skips non-.agent.md files", async () => {
|
|
108
|
+
registerType("test-agent");
|
|
109
|
+
// Write a regular .md file — should be ignored
|
|
110
|
+
fs.writeFileSync(path.join(tempDir, "not-an-agent.md"), "---\ntype: test-agent\nrole: Nope\ncapabilities: []\n---\n", "utf8");
|
|
111
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir));
|
|
112
|
+
const agents = await loader.loadAgents();
|
|
113
|
+
assert.equal(agents.length, 0);
|
|
114
|
+
});
|
|
115
|
+
it("creates LLM provider when llm_provider and llm_model are specified", async () => {
|
|
116
|
+
registerType("test-agent");
|
|
117
|
+
writeAgentFile(tempDir, "llm-agent", [
|
|
118
|
+
"type: test-agent",
|
|
119
|
+
"role: LLM User",
|
|
120
|
+
"capabilities: [chat]",
|
|
121
|
+
"llm_provider: ollama",
|
|
122
|
+
"llm_model: llama3",
|
|
123
|
+
].join("\n"));
|
|
124
|
+
let factoryCalled = false;
|
|
125
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir, {
|
|
126
|
+
llmProviderFactory: async (providerName, model) => {
|
|
127
|
+
factoryCalled = true;
|
|
128
|
+
assert.equal(providerName, "ollama");
|
|
129
|
+
assert.equal(model, "llama3");
|
|
130
|
+
return null;
|
|
131
|
+
},
|
|
132
|
+
}));
|
|
133
|
+
const agents = await loader.loadAgents();
|
|
134
|
+
assert.equal(agents.length, 1);
|
|
135
|
+
assert.equal(factoryCalled, true);
|
|
136
|
+
});
|
|
137
|
+
it("loads multiple agents from directory", async () => {
|
|
138
|
+
registerType("test-agent");
|
|
139
|
+
writeAgentFile(tempDir, "agent-a", "type: test-agent\nrole: Alpha\ncapabilities: [a]");
|
|
140
|
+
writeAgentFile(tempDir, "agent-b", "type: test-agent\nrole: Beta\ncapabilities: [b]");
|
|
141
|
+
const loader = new AgentLoader(makeLoaderConfig(tempDir));
|
|
142
|
+
const agents = await loader.loadAgents();
|
|
143
|
+
assert.equal(agents.length, 2);
|
|
144
|
+
const ids = agents.map((a) => a.config.id).sort();
|
|
145
|
+
assert.deepEqual(ids, ["agent-a", "agent-b"]);
|
|
146
|
+
});
|
|
147
|
+
});
|
|
148
|
+
describe("parseMarkdownWithFrontmatter()", () => {
|
|
149
|
+
it("extracts frontmatter and body", () => {
|
|
150
|
+
const content = [
|
|
151
|
+
"---",
|
|
152
|
+
"type: test-agent",
|
|
153
|
+
"role: Parser",
|
|
154
|
+
"capabilities: [parse, extract]",
|
|
155
|
+
"---",
|
|
156
|
+
"",
|
|
157
|
+
"# Agent Body",
|
|
158
|
+
"",
|
|
159
|
+
"This is the markdown body.",
|
|
160
|
+
].join("\n");
|
|
161
|
+
const loader = new AgentLoader(makeLoaderConfig("/fake"));
|
|
162
|
+
const result = loader.parseMarkdownWithFrontmatter(content);
|
|
163
|
+
assert.notEqual(result.frontmatter, null);
|
|
164
|
+
if (result.frontmatter) {
|
|
165
|
+
assert.equal(result.frontmatter.type, "test-agent");
|
|
166
|
+
assert.equal(result.frontmatter.role, "Parser");
|
|
167
|
+
assert.deepEqual(result.frontmatter.capabilities, ["parse", "extract"]);
|
|
168
|
+
}
|
|
169
|
+
assert.ok(result.body.includes("# Agent Body"));
|
|
170
|
+
assert.ok(result.body.includes("This is the markdown body."));
|
|
171
|
+
});
|
|
172
|
+
it("returns null frontmatter for files without YAML frontmatter", () => {
|
|
173
|
+
const content = "# Just a markdown file\n\nNo frontmatter here.";
|
|
174
|
+
const loader = new AgentLoader(makeLoaderConfig("/fake"));
|
|
175
|
+
const result = loader.parseMarkdownWithFrontmatter(content);
|
|
176
|
+
assert.equal(result.frontmatter, null);
|
|
177
|
+
assert.equal(result.body, content);
|
|
178
|
+
});
|
|
179
|
+
});
|
|
180
|
+
});
|
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { afterEach, describe, it } from "node:test";
|
|
3
|
+
import { Agent } from "../../src/agent-bus/Agent.js";
|
|
4
|
+
import { AgentRegistry } from "../../src/agent-bus/AgentRegistry.js";
|
|
5
|
+
// Minimal concrete Agent subclass for testing registration.
|
|
6
|
+
class TestAgent extends Agent {
|
|
7
|
+
async tick() { }
|
|
8
|
+
}
|
|
9
|
+
class OtherAgent extends Agent {
|
|
10
|
+
async tick() { }
|
|
11
|
+
}
|
|
12
|
+
// Noop constructor used for cleanup — overwrites previously registered types
|
|
13
|
+
// so they don't leak into other tests.
|
|
14
|
+
const noopCtor = TestAgent;
|
|
15
|
+
// Track types registered during each test so afterEach can overwrite them.
|
|
16
|
+
const registeredTypes = [];
|
|
17
|
+
afterEach(() => {
|
|
18
|
+
for (const type of registeredTypes) {
|
|
19
|
+
AgentRegistry.register(type, noopCtor);
|
|
20
|
+
}
|
|
21
|
+
registeredTypes.length = 0;
|
|
22
|
+
});
|
|
23
|
+
function register(type, ctor) {
|
|
24
|
+
const c = ctor ?? TestAgent;
|
|
25
|
+
AgentRegistry.register(type, c);
|
|
26
|
+
registeredTypes.push(type);
|
|
27
|
+
}
|
|
28
|
+
describe("AgentRegistry", () => {
|
|
29
|
+
describe("register()", () => {
|
|
30
|
+
it("adds a constructor to the registry", () => {
|
|
31
|
+
register("test-agent", TestAgent);
|
|
32
|
+
const result = AgentRegistry.get("test-agent");
|
|
33
|
+
assert.ok(result);
|
|
34
|
+
assert.strictEqual(result, TestAgent);
|
|
35
|
+
});
|
|
36
|
+
it("overwrites when the same type is registered twice", () => {
|
|
37
|
+
register("dupe-agent", TestAgent);
|
|
38
|
+
register("dupe-agent", OtherAgent);
|
|
39
|
+
const result = AgentRegistry.get("dupe-agent");
|
|
40
|
+
assert.strictEqual(result, OtherAgent);
|
|
41
|
+
});
|
|
42
|
+
});
|
|
43
|
+
describe("get()", () => {
|
|
44
|
+
it("returns the constructor for a registered type", () => {
|
|
45
|
+
register("my-agent", TestAgent);
|
|
46
|
+
const result = AgentRegistry.get("my-agent");
|
|
47
|
+
assert.ok(result);
|
|
48
|
+
assert.strictEqual(result, TestAgent);
|
|
49
|
+
});
|
|
50
|
+
it("returns undefined for an unknown type", () => {
|
|
51
|
+
const result = AgentRegistry.get("nonexistent");
|
|
52
|
+
assert.strictEqual(result, undefined);
|
|
53
|
+
});
|
|
54
|
+
});
|
|
55
|
+
describe("list()", () => {
|
|
56
|
+
it("returns all registered type names", () => {
|
|
57
|
+
register("agent-a", TestAgent);
|
|
58
|
+
register("agent-b", OtherAgent);
|
|
59
|
+
const types = AgentRegistry.list();
|
|
60
|
+
assert.ok(types.includes("agent-a"));
|
|
61
|
+
assert.ok(types.includes("agent-b"));
|
|
62
|
+
});
|
|
63
|
+
it("returns an array (may include cleanup entries from prior tests)", () => {
|
|
64
|
+
const types = AgentRegistry.list();
|
|
65
|
+
assert.ok(Array.isArray(types));
|
|
66
|
+
});
|
|
67
|
+
});
|
|
68
|
+
describe("isolation", () => {
|
|
69
|
+
it("does not leak registrations between tests", () => {
|
|
70
|
+
register("isolated-agent", TestAgent);
|
|
71
|
+
const before = AgentRegistry.get("isolated-agent");
|
|
72
|
+
assert.ok(before);
|
|
73
|
+
});
|
|
74
|
+
it("confirm prior test's registration was cleaned up", () => {
|
|
75
|
+
// The "isolated-agent" from the previous test should have been
|
|
76
|
+
// overwritten by afterEach with noopCtor (TestAgent), so get()
|
|
77
|
+
// returns TestAgent, not some other class.
|
|
78
|
+
const result = AgentRegistry.get("isolated-agent");
|
|
79
|
+
// It may still exist (as noopCtor/TestAgent) or be absent.
|
|
80
|
+
// The key point: it should NOT be a different class.
|
|
81
|
+
if (result) {
|
|
82
|
+
assert.notStrictEqual(result, OtherAgent);
|
|
83
|
+
}
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
});
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
import { strict as assert } from "node:assert";
|
|
2
|
+
import { afterEach, describe, it, mock } from "node:test";
|
|
3
|
+
import { AgentModelProvider } from "../../src/agent-bus/LLMProvider.js";
|
|
4
|
+
// ---------------------------------------------------------------------------
|
|
5
|
+
// Helpers
|
|
6
|
+
// ---------------------------------------------------------------------------
|
|
7
|
+
function makeConfig(overrides) {
|
|
8
|
+
return { model: "test-model", ...overrides };
|
|
9
|
+
}
|
|
10
|
+
function makeChatResponse(content) {
|
|
11
|
+
return {
|
|
12
|
+
choices: [{ message: { content } }],
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
function makeFetchMock(response, status = 200) {
|
|
16
|
+
return mock.fn(async (_url, _init) => {
|
|
17
|
+
return {
|
|
18
|
+
ok: status >= 200 && status < 300,
|
|
19
|
+
status,
|
|
20
|
+
json: async () => response,
|
|
21
|
+
text: async () => JSON.stringify(response),
|
|
22
|
+
};
|
|
23
|
+
});
|
|
24
|
+
}
|
|
25
|
+
// ---------------------------------------------------------------------------
|
|
26
|
+
// Tests
|
|
27
|
+
// ---------------------------------------------------------------------------
|
|
28
|
+
describe("LLMProvider", () => {
|
|
29
|
+
describe("constructor", () => {
|
|
30
|
+
it("stores config", () => {
|
|
31
|
+
const config = makeConfig({ model: "llama3", temperature: 0.5 });
|
|
32
|
+
// LLMProvider is abstract — test via concrete subclass
|
|
33
|
+
const provider = new AgentModelProvider(config);
|
|
34
|
+
// Access protected config via type assertion on the concrete instance
|
|
35
|
+
const stored = provider.config;
|
|
36
|
+
assert.equal(stored.model, "llama3");
|
|
37
|
+
assert.equal(stored.temperature, 0.5);
|
|
38
|
+
});
|
|
39
|
+
});
|
|
40
|
+
});
|
|
41
|
+
describe("AgentModelProvider", () => {
|
|
42
|
+
afterEach(() => {
|
|
43
|
+
mock.restoreAll();
|
|
44
|
+
});
|
|
45
|
+
describe("constructor", () => {
|
|
46
|
+
it("stores config", () => {
|
|
47
|
+
const config = makeConfig({ model: "gemma3", remoteUrl: "https://api.example.com" });
|
|
48
|
+
const provider = new AgentModelProvider(config);
|
|
49
|
+
const stored = provider.config;
|
|
50
|
+
assert.equal(stored.model, "gemma3");
|
|
51
|
+
assert.equal(stored.remoteUrl, "https://api.example.com");
|
|
52
|
+
});
|
|
53
|
+
});
|
|
54
|
+
describe("prompt()", () => {
|
|
55
|
+
it("extracts choices[0].message.content from chat completion response", async () => {
|
|
56
|
+
const fetchMock = makeFetchMock(makeChatResponse("Hello from the model"));
|
|
57
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
58
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
59
|
+
const result = await provider.prompt("Say hello");
|
|
60
|
+
assert.equal(result, "Hello from the model");
|
|
61
|
+
assert.equal(fetchMock.mock.callCount(), 1);
|
|
62
|
+
});
|
|
63
|
+
it("remote-first: tries remoteUrl, falls back to localUrl on failure", async () => {
|
|
64
|
+
const localResponse = makeChatResponse("from local");
|
|
65
|
+
const fetchMock = mock.fn(async (url, _init) => {
|
|
66
|
+
const urlStr = typeof url === "string" ? url : url.toString();
|
|
67
|
+
if (urlStr.includes("remote.example.com")) {
|
|
68
|
+
return {
|
|
69
|
+
ok: false,
|
|
70
|
+
status: 500,
|
|
71
|
+
json: async () => ({}),
|
|
72
|
+
text: async () => "Internal Server Error",
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
if (urlStr.includes("localhost")) {
|
|
76
|
+
return {
|
|
77
|
+
ok: true,
|
|
78
|
+
status: 200,
|
|
79
|
+
json: async () => localResponse,
|
|
80
|
+
text: async () => JSON.stringify(localResponse),
|
|
81
|
+
};
|
|
82
|
+
}
|
|
83
|
+
throw new Error(`Unexpected URL: ${urlStr}`);
|
|
84
|
+
});
|
|
85
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
86
|
+
const provider = new AgentModelProvider(makeConfig({
|
|
87
|
+
remoteUrl: "https://remote.example.com/v1",
|
|
88
|
+
localUrl: "http://localhost:11434/v1",
|
|
89
|
+
}));
|
|
90
|
+
const result = await provider.prompt("test");
|
|
91
|
+
assert.equal(result, "from local");
|
|
92
|
+
assert.equal(fetchMock.mock.callCount(), 2);
|
|
93
|
+
});
|
|
94
|
+
it("jsonMode: response is passed through extractJSON (strips fences)", async () => {
|
|
95
|
+
const rawResponse = '```json\n{"key": "value"}\n```';
|
|
96
|
+
const fetchMock = makeFetchMock(makeChatResponse(rawResponse));
|
|
97
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
98
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
99
|
+
const result = await provider.prompt("Give me JSON", { jsonMode: true });
|
|
100
|
+
assert.equal(result, '{"key": "value"}');
|
|
101
|
+
});
|
|
102
|
+
it("jsonMode: extracts first JSON object from mixed text", async () => {
|
|
103
|
+
const rawResponse = 'Here is the result:\n```json\n{"answer": 42}\n```\nHope that helps!';
|
|
104
|
+
const fetchMock = makeFetchMock(makeChatResponse(rawResponse));
|
|
105
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
106
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
107
|
+
const result = await provider.prompt("Give me JSON", { jsonMode: true });
|
|
108
|
+
assert.equal(result, '{"answer": 42}');
|
|
109
|
+
});
|
|
110
|
+
it("jsonMode: returns raw string if no JSON found", async () => {
|
|
111
|
+
const rawResponse = "Just some plain text, no JSON here.";
|
|
112
|
+
const fetchMock = makeFetchMock(makeChatResponse(rawResponse));
|
|
113
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
114
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
115
|
+
const result = await provider.prompt("Give me text", { jsonMode: true });
|
|
116
|
+
assert.equal(result, rawResponse);
|
|
117
|
+
});
|
|
118
|
+
it("jsonMode: already-valid JSON is returned as-is", async () => {
|
|
119
|
+
const rawResponse = '{"valid": true}';
|
|
120
|
+
const fetchMock = makeFetchMock(makeChatResponse(rawResponse));
|
|
121
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
122
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
123
|
+
const result = await provider.prompt("Give me JSON", { jsonMode: true });
|
|
124
|
+
assert.equal(result, '{"valid": true}');
|
|
125
|
+
});
|
|
126
|
+
it("includes system prompt in messages when provided", async () => {
|
|
127
|
+
const fetchMock = mock.fn(async (_url, init) => {
|
|
128
|
+
const body = JSON.parse(init.body);
|
|
129
|
+
assert.equal(body.messages.length, 2);
|
|
130
|
+
assert.equal(body.messages[0].role, "system");
|
|
131
|
+
assert.equal(body.messages[0].content, "You are helpful.");
|
|
132
|
+
assert.equal(body.messages[1].role, "user");
|
|
133
|
+
assert.equal(body.messages[1].content, "Hello");
|
|
134
|
+
return {
|
|
135
|
+
ok: true,
|
|
136
|
+
status: 200,
|
|
137
|
+
json: async () => makeChatResponse("Hi there"),
|
|
138
|
+
text: async () => JSON.stringify(makeChatResponse("Hi there")),
|
|
139
|
+
};
|
|
140
|
+
});
|
|
141
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
142
|
+
const provider = new AgentModelProvider(makeConfig({ remoteUrl: "http://localhost:11434/v1" }));
|
|
143
|
+
await provider.prompt("Hello", { systemPrompt: "You are helpful." });
|
|
144
|
+
});
|
|
145
|
+
it("throws when all endpoints fail", async () => {
|
|
146
|
+
const fetchMock = mock.fn(async (_url, _init) => {
|
|
147
|
+
return {
|
|
148
|
+
ok: false,
|
|
149
|
+
status: 503,
|
|
150
|
+
json: async () => ({}),
|
|
151
|
+
text: async () => "Service Unavailable",
|
|
152
|
+
};
|
|
153
|
+
});
|
|
154
|
+
mock.method(globalThis, "fetch", fetchMock);
|
|
155
|
+
const provider = new AgentModelProvider(makeConfig({
|
|
156
|
+
remoteUrl: "https://bad.example.com/v1",
|
|
157
|
+
localUrl: "http://localhost:11434/v1",
|
|
158
|
+
}));
|
|
159
|
+
await assert.rejects(() => provider.prompt("test"), /HTTP 503/);
|
|
160
|
+
});
|
|
161
|
+
});
|
|
162
|
+
});
|