@ouro.bot/cli 0.1.0-alpha.7 → 0.1.0-alpha.71
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/AdoptionSpecialist.ouro/agent.json +70 -9
- package/AdoptionSpecialist.ouro/psyche/SOUL.md +5 -2
- package/AdoptionSpecialist.ouro/psyche/identities/monty.md +2 -2
- package/README.md +147 -205
- package/assets/ouroboros.png +0 -0
- package/changelog.json +395 -0
- package/dist/heart/active-work.js +178 -0
- package/dist/heart/bridges/manager.js +358 -0
- package/dist/heart/bridges/state-machine.js +135 -0
- package/dist/heart/bridges/store.js +123 -0
- package/dist/heart/config.js +68 -23
- package/dist/heart/core.js +282 -92
- package/dist/heart/cross-chat-delivery.js +146 -0
- package/dist/heart/daemon/agent-discovery.js +81 -0
- package/dist/heart/daemon/auth-flow.js +409 -0
- package/dist/heart/daemon/daemon-cli.js +1408 -248
- package/dist/heart/daemon/daemon-entry.js +55 -6
- package/dist/heart/daemon/daemon-runtime-sync.js +212 -0
- package/dist/heart/daemon/daemon.js +216 -10
- package/dist/heart/daemon/hatch-animation.js +10 -3
- package/dist/heart/daemon/hatch-flow.js +7 -82
- package/dist/heart/daemon/hooks/bundle-meta.js +92 -0
- package/dist/heart/daemon/launchd.js +159 -0
- package/dist/heart/daemon/log-tailer.js +4 -3
- package/dist/heart/daemon/message-router.js +17 -8
- package/dist/heart/daemon/ouro-bot-entry.js +0 -0
- package/dist/heart/daemon/ouro-bot-global-installer.js +128 -0
- package/dist/heart/daemon/ouro-entry.js +0 -0
- package/dist/heart/daemon/ouro-path-installer.js +178 -0
- package/dist/heart/daemon/ouro-uti.js +11 -2
- package/dist/heart/daemon/process-manager.js +14 -1
- package/dist/heart/daemon/run-hooks.js +37 -0
- package/dist/heart/daemon/runtime-logging.js +58 -15
- package/dist/heart/daemon/runtime-metadata.js +219 -0
- package/dist/heart/daemon/runtime-mode.js +67 -0
- package/dist/heart/daemon/sense-manager.js +307 -0
- package/dist/heart/daemon/skill-management-installer.js +94 -0
- package/dist/heart/daemon/socket-client.js +202 -0
- package/dist/heart/daemon/specialist-orchestrator.js +53 -84
- package/dist/heart/daemon/specialist-prompt.js +64 -5
- package/dist/heart/daemon/specialist-tools.js +213 -58
- package/dist/heart/daemon/staged-restart.js +114 -0
- package/dist/heart/daemon/thoughts.js +379 -0
- package/dist/heart/daemon/update-checker.js +111 -0
- package/dist/heart/daemon/update-hooks.js +138 -0
- package/dist/heart/daemon/wrapper-publish-guard.js +86 -0
- package/dist/heart/delegation.js +62 -0
- package/dist/heart/identity.js +126 -21
- package/dist/heart/kicks.js +1 -19
- package/dist/heart/model-capabilities.js +48 -0
- package/dist/heart/progress-story.js +42 -0
- package/dist/heart/providers/anthropic.js +74 -9
- package/dist/heart/providers/azure.js +86 -7
- package/dist/heart/providers/github-copilot.js +149 -0
- package/dist/heart/providers/minimax.js +4 -0
- package/dist/heart/providers/openai-codex.js +12 -3
- package/dist/heart/safe-workspace.js +228 -0
- package/dist/heart/sense-truth.js +61 -0
- package/dist/heart/session-activity.js +169 -0
- package/dist/heart/session-recall.js +116 -0
- package/dist/heart/streaming.js +100 -22
- package/dist/heart/target-resolution.js +123 -0
- package/dist/heart/turn-coordinator.js +28 -0
- package/dist/mind/associative-recall.js +14 -2
- package/dist/mind/bundle-manifest.js +70 -0
- package/dist/mind/context.js +27 -11
- package/dist/mind/first-impressions.js +16 -2
- package/dist/mind/friends/channel.js +35 -0
- package/dist/mind/friends/group-context.js +144 -0
- package/dist/mind/friends/store-file.js +19 -0
- package/dist/mind/friends/trust-explanation.js +74 -0
- package/dist/mind/friends/types.js +8 -0
- package/dist/mind/memory.js +27 -26
- package/dist/mind/pending.js +72 -9
- package/dist/mind/phrases.js +1 -0
- package/dist/mind/prompt.js +358 -77
- package/dist/mind/token-estimate.js +8 -12
- package/dist/nerves/cli-logging.js +15 -2
- package/dist/nerves/coverage/run-artifacts.js +1 -1
- package/dist/repertoire/ado-client.js +4 -2
- package/dist/repertoire/coding/feedback.js +134 -0
- package/dist/repertoire/coding/index.js +4 -1
- package/dist/repertoire/coding/manager.js +62 -4
- package/dist/repertoire/coding/spawner.js +3 -3
- package/dist/repertoire/coding/tools.js +41 -2
- package/dist/repertoire/data/ado-endpoints.json +188 -0
- package/dist/repertoire/guardrails.js +279 -0
- package/dist/repertoire/mcp-client.js +254 -0
- package/dist/repertoire/mcp-manager.js +195 -0
- package/dist/repertoire/skills.js +3 -26
- package/dist/repertoire/tasks/board.js +12 -0
- package/dist/repertoire/tasks/index.js +23 -9
- package/dist/repertoire/tasks/transitions.js +1 -2
- package/dist/repertoire/tools-base.js +642 -251
- package/dist/repertoire/tools-bluebubbles.js +93 -0
- package/dist/repertoire/tools-teams.js +58 -25
- package/dist/repertoire/tools.js +93 -52
- package/dist/senses/bluebubbles-client.js +210 -5
- package/dist/senses/bluebubbles-entry.js +2 -0
- package/dist/senses/bluebubbles-inbound-log.js +109 -0
- package/dist/senses/bluebubbles-media.js +339 -0
- package/dist/senses/bluebubbles-model.js +12 -4
- package/dist/senses/bluebubbles-mutation-log.js +45 -5
- package/dist/senses/bluebubbles-runtime-state.js +109 -0
- package/dist/senses/bluebubbles-session-cleanup.js +72 -0
- package/dist/senses/bluebubbles.js +893 -45
- package/dist/senses/cli-layout.js +87 -0
- package/dist/senses/cli.js +348 -144
- package/dist/senses/continuity.js +94 -0
- package/dist/senses/debug-activity.js +148 -0
- package/dist/senses/inner-dialog-worker.js +47 -18
- package/dist/senses/inner-dialog.js +333 -84
- package/dist/senses/pipeline.js +278 -0
- package/dist/senses/teams.js +573 -129
- package/dist/senses/trust-gate.js +112 -2
- package/package.json +14 -3
- package/subagents/README.md +4 -70
- package/dist/heart/daemon/specialist-session.js +0 -142
- package/dist/heart/daemon/subagent-installer.js +0 -125
- package/dist/inner-worker-entry.js +0 -4
- package/subagents/work-doer.md +0 -233
- package/subagents/work-merger.md +0 -624
- package/subagents/work-planner.md +0 -373
|
@@ -0,0 +1,279 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
|
|
3
|
+
if (k2 === undefined) k2 = k;
|
|
4
|
+
var desc = Object.getOwnPropertyDescriptor(m, k);
|
|
5
|
+
if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
|
|
6
|
+
desc = { enumerable: true, get: function() { return m[k]; } };
|
|
7
|
+
}
|
|
8
|
+
Object.defineProperty(o, k2, desc);
|
|
9
|
+
}) : (function(o, m, k, k2) {
|
|
10
|
+
if (k2 === undefined) k2 = k;
|
|
11
|
+
o[k2] = m[k];
|
|
12
|
+
}));
|
|
13
|
+
var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
|
|
14
|
+
Object.defineProperty(o, "default", { enumerable: true, value: v });
|
|
15
|
+
}) : function(o, v) {
|
|
16
|
+
o["default"] = v;
|
|
17
|
+
});
|
|
18
|
+
var __importStar = (this && this.__importStar) || (function () {
|
|
19
|
+
var ownKeys = function(o) {
|
|
20
|
+
ownKeys = Object.getOwnPropertyNames || function (o) {
|
|
21
|
+
var ar = [];
|
|
22
|
+
for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
|
|
23
|
+
return ar;
|
|
24
|
+
};
|
|
25
|
+
return ownKeys(o);
|
|
26
|
+
};
|
|
27
|
+
return function (mod) {
|
|
28
|
+
if (mod && mod.__esModule) return mod;
|
|
29
|
+
var result = {};
|
|
30
|
+
if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
|
|
31
|
+
__setModuleDefault(result, mod);
|
|
32
|
+
return result;
|
|
33
|
+
};
|
|
34
|
+
})();
|
|
35
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
36
|
+
exports.OURO_CLI_TRUST_MANIFEST = void 0;
|
|
37
|
+
exports.guardInvocation = guardInvocation;
|
|
38
|
+
const fs = __importStar(require("node:fs"));
|
|
39
|
+
const os = __importStar(require("node:os"));
|
|
40
|
+
const types_1 = require("../mind/friends/types");
|
|
41
|
+
const runtime_1 = require("../nerves/runtime");
|
|
42
|
+
const deny = (reason) => ({ allowed: false, reason });
|
|
43
|
+
const allow = { allowed: true };
|
|
44
|
+
// --- reason templates ---
|
|
45
|
+
// Structural reasons (always-on, apply to everyone)
|
|
46
|
+
const REASONS = {
|
|
47
|
+
readBeforeEdit: "i need to read that file first before i can edit it.",
|
|
48
|
+
readBeforeOverwrite: "i need to read that file first before i can overwrite it.",
|
|
49
|
+
protectedPath: "that path is protected — i can read it but not modify it.",
|
|
50
|
+
destructiveCommand: "that command is too dangerous to run — it could cause irreversible damage.",
|
|
51
|
+
compoundCommand: "i can only run simple commands for you — no chaining with && or ;",
|
|
52
|
+
// Trust reasons (vary by relationship)
|
|
53
|
+
needsTrust: "i'd need a closer friend to vouch for you before i can do that.",
|
|
54
|
+
needsTrustForWrite: "i'd need a closer friend to vouch for you before i can write files outside my home.",
|
|
55
|
+
};
|
|
56
|
+
// --- read-only tools that never need guardrails ---
|
|
57
|
+
const READ_ONLY_TOOLS = new Set(["read_file", "glob", "grep"]);
|
|
58
|
+
// --- protected path detection ---
|
|
59
|
+
const PROTECTED_PATH_SEGMENTS = [".git/"];
|
|
60
|
+
function getProtectedAbsolutePrefixes() {
|
|
61
|
+
return [`${os.homedir()}/.agentsecrets/`];
|
|
62
|
+
}
|
|
63
|
+
function isProtectedPath(filePath) {
|
|
64
|
+
for (const segment of PROTECTED_PATH_SEGMENTS) {
|
|
65
|
+
if (filePath.includes(`/${segment}`) || filePath.startsWith(segment))
|
|
66
|
+
return true;
|
|
67
|
+
}
|
|
68
|
+
for (const prefix of getProtectedAbsolutePrefixes()) {
|
|
69
|
+
if (filePath.startsWith(prefix))
|
|
70
|
+
return true;
|
|
71
|
+
}
|
|
72
|
+
return false;
|
|
73
|
+
}
|
|
74
|
+
// --- destructive shell patterns ---
|
|
75
|
+
const DESTRUCTIVE_PATTERNS = [
|
|
76
|
+
/\brm\s+(-\w*\s+)*-\w*r\w*\s+(-\w+\s+)*[/~]/, // rm -rf / or rm -rf ~
|
|
77
|
+
/\bchmod\s+(-\w*\s+)*-\w*R\w*\s+\d+\s+\//, // chmod -R 777 /
|
|
78
|
+
/\bmkfs\b/, // mkfs.*
|
|
79
|
+
/\bdd\s+if=/, // dd if=
|
|
80
|
+
];
|
|
81
|
+
function isDestructiveShellCommand(command) {
|
|
82
|
+
return DESTRUCTIVE_PATTERNS.some((p) => p.test(command));
|
|
83
|
+
}
|
|
84
|
+
// --- compound command splitting ---
|
|
85
|
+
// Shell operators that chain commands: &&, ||, ;, |, $(), backticks
|
|
86
|
+
const COMPOUND_SEPARATORS = /\s*(?:&&|\|\||;|\|)\s*/;
|
|
87
|
+
const SUBSHELL_PATTERN = /\$\(|`/;
|
|
88
|
+
function splitShellCommands(command) {
|
|
89
|
+
if (SUBSHELL_PATTERN.test(command))
|
|
90
|
+
return [command];
|
|
91
|
+
return command.split(COMPOUND_SEPARATORS).filter(Boolean);
|
|
92
|
+
}
|
|
93
|
+
function isCompoundCommand(command) {
|
|
94
|
+
return SUBSHELL_PATTERN.test(command) || splitShellCommands(command).length > 1;
|
|
95
|
+
}
|
|
96
|
+
// --- shell commands that write to protected paths ---
|
|
97
|
+
function shellWritesToProtectedPath(command) {
|
|
98
|
+
const redirectMatch = command.match(/>\s*(\S+)/);
|
|
99
|
+
if (redirectMatch && isProtectedPath(redirectMatch[1]))
|
|
100
|
+
return true;
|
|
101
|
+
const teeMatch = command.match(/tee\s+(?:-\w+\s+)*(\S+)/);
|
|
102
|
+
if (teeMatch && isProtectedPath(teeMatch[1]))
|
|
103
|
+
return true;
|
|
104
|
+
return false;
|
|
105
|
+
}
|
|
106
|
+
// --- structural guardrail checks (always on, all trust levels) ---
|
|
107
|
+
function checkReadBeforeWrite(toolName, args, context) {
|
|
108
|
+
if (toolName === "edit_file") {
|
|
109
|
+
const filePath = args.path || "";
|
|
110
|
+
if (!context.readPaths.has(filePath))
|
|
111
|
+
return deny(REASONS.readBeforeEdit);
|
|
112
|
+
}
|
|
113
|
+
if (toolName === "write_file") {
|
|
114
|
+
const filePath = args.path || "";
|
|
115
|
+
if (context.readPaths.has(filePath))
|
|
116
|
+
return allow;
|
|
117
|
+
if (!fs.existsSync(filePath))
|
|
118
|
+
return allow;
|
|
119
|
+
return deny(REASONS.readBeforeOverwrite);
|
|
120
|
+
}
|
|
121
|
+
return allow;
|
|
122
|
+
}
|
|
123
|
+
function checkDestructiveShellPatterns(toolName, args) {
|
|
124
|
+
if (toolName !== "shell")
|
|
125
|
+
return allow;
|
|
126
|
+
const command = args.command || "";
|
|
127
|
+
// Check each subcommand in compound commands for destructive patterns
|
|
128
|
+
for (const sub of splitShellCommands(command)) {
|
|
129
|
+
if (isDestructiveShellCommand(sub))
|
|
130
|
+
return deny(REASONS.destructiveCommand);
|
|
131
|
+
}
|
|
132
|
+
return allow;
|
|
133
|
+
}
|
|
134
|
+
function checkProtectedPaths(toolName, args) {
|
|
135
|
+
if (toolName === "write_file" || toolName === "edit_file") {
|
|
136
|
+
const filePath = args.path || "";
|
|
137
|
+
if (isProtectedPath(filePath))
|
|
138
|
+
return deny(REASONS.protectedPath);
|
|
139
|
+
}
|
|
140
|
+
if (toolName === "shell") {
|
|
141
|
+
const command = args.command || "";
|
|
142
|
+
if (shellWritesToProtectedPath(command))
|
|
143
|
+
return deny(REASONS.protectedPath);
|
|
144
|
+
}
|
|
145
|
+
return allow;
|
|
146
|
+
}
|
|
147
|
+
function checkStructuralGuardrails(toolName, args, context) {
|
|
148
|
+
const protectedResult = checkProtectedPaths(toolName, args);
|
|
149
|
+
if (!protectedResult.allowed)
|
|
150
|
+
return protectedResult;
|
|
151
|
+
const destructiveResult = checkDestructiveShellPatterns(toolName, args);
|
|
152
|
+
if (!destructiveResult.allowed)
|
|
153
|
+
return destructiveResult;
|
|
154
|
+
return checkReadBeforeWrite(toolName, args, context);
|
|
155
|
+
}
|
|
156
|
+
// --- ouro CLI trust manifest ---
|
|
157
|
+
/** Minimum trust level required for each ouro CLI subcommand. */
|
|
158
|
+
exports.OURO_CLI_TRUST_MANIFEST = {
|
|
159
|
+
whoami: "acquaintance",
|
|
160
|
+
changelog: "acquaintance",
|
|
161
|
+
"session list": "acquaintance",
|
|
162
|
+
"task board": "friend",
|
|
163
|
+
"task create": "friend",
|
|
164
|
+
"task update": "friend",
|
|
165
|
+
"task show": "friend",
|
|
166
|
+
"task actionable": "friend",
|
|
167
|
+
"task deps": "friend",
|
|
168
|
+
"task sessions": "friend",
|
|
169
|
+
"friend list": "friend",
|
|
170
|
+
"friend show": "friend",
|
|
171
|
+
"friend create": "friend",
|
|
172
|
+
"reminder create": "friend",
|
|
173
|
+
"mcp list": "acquaintance",
|
|
174
|
+
"mcp call": "friend",
|
|
175
|
+
auth: "family",
|
|
176
|
+
"auth verify": "family",
|
|
177
|
+
"auth switch": "family",
|
|
178
|
+
};
|
|
179
|
+
// --- trust level comparison ---
|
|
180
|
+
const LEVEL_ORDER = {
|
|
181
|
+
stranger: 0,
|
|
182
|
+
acquaintance: 1,
|
|
183
|
+
friend: 2,
|
|
184
|
+
family: 3,
|
|
185
|
+
};
|
|
186
|
+
function trustLevelSatisfied(required, actual) {
|
|
187
|
+
return LEVEL_ORDER[actual] >= LEVEL_ORDER[required];
|
|
188
|
+
}
|
|
189
|
+
// --- general CLI allowlists for acquaintance ---
|
|
190
|
+
const ACQUAINTANCE_SHELL_ALLOWLIST = new Set([
|
|
191
|
+
"cat", "ls", "head", "tail", "wc", "file", "stat", "which", "echo",
|
|
192
|
+
"pwd", "env", "printenv", "whoami", "date", "uname",
|
|
193
|
+
]);
|
|
194
|
+
const ACQUAINTANCE_GIT_ALLOWLIST = new Set([
|
|
195
|
+
"status", "log", "show", "diff", "branch",
|
|
196
|
+
]);
|
|
197
|
+
// --- trust-level shell guardrails ---
|
|
198
|
+
function resolveOuroSubcommand(command) {
|
|
199
|
+
const afterOuro = command.replace(/^ouro\s+/, "").trim();
|
|
200
|
+
/* v8 ignore next -- bare "ouro" is caught upstream by checkShellTrustGuardrails @preserve */
|
|
201
|
+
if (!afterOuro)
|
|
202
|
+
return null;
|
|
203
|
+
const tokens = afterOuro.split(/\s+/);
|
|
204
|
+
const twoWord = tokens.length >= 2 ? `${tokens[0]} ${tokens[1]}` : null;
|
|
205
|
+
// Two-word match first (e.g. "task board"), then one-word (e.g. "whoami")
|
|
206
|
+
if (twoWord && exports.OURO_CLI_TRUST_MANIFEST[twoWord])
|
|
207
|
+
return twoWord;
|
|
208
|
+
if (exports.OURO_CLI_TRUST_MANIFEST[tokens[0]])
|
|
209
|
+
return tokens[0];
|
|
210
|
+
return null;
|
|
211
|
+
}
|
|
212
|
+
function checkSingleShellCommandTrust(command, trustLevel) {
|
|
213
|
+
const trimmed = command.trim();
|
|
214
|
+
const tokens = trimmed.split(/\s+/);
|
|
215
|
+
const firstToken = tokens[0] || "";
|
|
216
|
+
// ouro CLI — check per-subcommand trust manifest
|
|
217
|
+
if (firstToken === "ouro") {
|
|
218
|
+
const subcommand = resolveOuroSubcommand(trimmed);
|
|
219
|
+
const requiredLevel = subcommand ? exports.OURO_CLI_TRUST_MANIFEST[subcommand] : "friend";
|
|
220
|
+
if (trustLevelSatisfied(requiredLevel, trustLevel))
|
|
221
|
+
return allow;
|
|
222
|
+
return deny(REASONS.needsTrust);
|
|
223
|
+
}
|
|
224
|
+
// git — check subcommand allowlist
|
|
225
|
+
if (firstToken === "git") {
|
|
226
|
+
const gitSub = tokens[1] || "";
|
|
227
|
+
if (ACQUAINTANCE_GIT_ALLOWLIST.has(gitSub))
|
|
228
|
+
return allow;
|
|
229
|
+
return deny(REASONS.needsTrust);
|
|
230
|
+
}
|
|
231
|
+
// General CLI — check allowlist
|
|
232
|
+
if (ACQUAINTANCE_SHELL_ALLOWLIST.has(firstToken))
|
|
233
|
+
return allow;
|
|
234
|
+
return deny(REASONS.needsTrust);
|
|
235
|
+
}
|
|
236
|
+
function checkShellTrustGuardrails(command, trustLevel) {
|
|
237
|
+
// Compound commands: for untrusted users, reject entirely.
|
|
238
|
+
// This prevents "ouro whoami && rm -rf /" from smuggling dangerous commands.
|
|
239
|
+
if (isCompoundCommand(command))
|
|
240
|
+
return deny(REASONS.compoundCommand);
|
|
241
|
+
return checkSingleShellCommandTrust(command, trustLevel);
|
|
242
|
+
}
|
|
243
|
+
function checkWriteTrustGuardrails(toolName, args, context) {
|
|
244
|
+
if (toolName !== "write_file" && toolName !== "edit_file")
|
|
245
|
+
return allow;
|
|
246
|
+
const filePath = args.path || "";
|
|
247
|
+
if (context.agentRoot && filePath.startsWith(context.agentRoot))
|
|
248
|
+
return allow;
|
|
249
|
+
if (!context.agentRoot)
|
|
250
|
+
return allow;
|
|
251
|
+
return deny(REASONS.needsTrustForWrite);
|
|
252
|
+
}
|
|
253
|
+
function checkTrustLevelGuardrails(toolName, args, context) {
|
|
254
|
+
// Trusted levels (family/friend) — no trust guardrails. Undefined defaults to friend.
|
|
255
|
+
if ((0, types_1.isTrustedLevel)(context.trustLevel))
|
|
256
|
+
return allow;
|
|
257
|
+
if (toolName === "shell") {
|
|
258
|
+
return checkShellTrustGuardrails(args.command || "", context.trustLevel);
|
|
259
|
+
}
|
|
260
|
+
return checkWriteTrustGuardrails(toolName, args, context);
|
|
261
|
+
}
|
|
262
|
+
// --- main entry point ---
|
|
263
|
+
function guardInvocation(toolName, args, context) {
|
|
264
|
+
(0, runtime_1.emitNervesEvent)({
|
|
265
|
+
component: "tools",
|
|
266
|
+
event: "tools.guard_check",
|
|
267
|
+
message: "guardrail check",
|
|
268
|
+
meta: { toolName },
|
|
269
|
+
});
|
|
270
|
+
// Read-only tools are always allowed (no structural or trust guardrails)
|
|
271
|
+
if (READ_ONLY_TOOLS.has(toolName))
|
|
272
|
+
return allow;
|
|
273
|
+
// Layer 1: structural guardrails (always on)
|
|
274
|
+
const structuralResult = checkStructuralGuardrails(toolName, args, context);
|
|
275
|
+
if (!structuralResult.allowed)
|
|
276
|
+
return structuralResult;
|
|
277
|
+
// Layer 2: trust-level guardrails (varies by friend's trust)
|
|
278
|
+
return checkTrustLevelGuardrails(toolName, args, context);
|
|
279
|
+
}
|
|
@@ -0,0 +1,254 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.McpClient = void 0;
|
|
4
|
+
const child_process_1 = require("child_process");
|
|
5
|
+
const readline_1 = require("readline");
|
|
6
|
+
const runtime_1 = require("../nerves/runtime");
|
|
7
|
+
const MCP_PROTOCOL_VERSION = "2024-11-05";
|
|
8
|
+
const DEFAULT_TOOL_CALL_TIMEOUT = 30_000;
|
|
9
|
+
class McpClient {
|
|
10
|
+
config;
|
|
11
|
+
process = null;
|
|
12
|
+
nextId = 1;
|
|
13
|
+
pending = new Map();
|
|
14
|
+
connected = false;
|
|
15
|
+
cachedTools = null;
|
|
16
|
+
onCloseCallback = null;
|
|
17
|
+
constructor(config) {
|
|
18
|
+
this.config = config;
|
|
19
|
+
}
|
|
20
|
+
async connect() {
|
|
21
|
+
(0, runtime_1.emitNervesEvent)({
|
|
22
|
+
event: "mcp.connect_start",
|
|
23
|
+
component: "repertoire",
|
|
24
|
+
message: "starting MCP server connection",
|
|
25
|
+
meta: { command: this.config.command },
|
|
26
|
+
});
|
|
27
|
+
const env = { ...process.env, ...this.config.env };
|
|
28
|
+
this.process = (0, child_process_1.spawn)(this.config.command, this.config.args ?? [], {
|
|
29
|
+
env,
|
|
30
|
+
stdio: ["pipe", "pipe", "pipe"],
|
|
31
|
+
});
|
|
32
|
+
this.setupLineReader();
|
|
33
|
+
this.setupProcessHandlers();
|
|
34
|
+
try {
|
|
35
|
+
await this.initialize();
|
|
36
|
+
this.connected = true;
|
|
37
|
+
(0, runtime_1.emitNervesEvent)({
|
|
38
|
+
event: "mcp.connect_end",
|
|
39
|
+
component: "repertoire",
|
|
40
|
+
message: "MCP server connected",
|
|
41
|
+
meta: { command: this.config.command },
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
catch (error) {
|
|
45
|
+
this.connected = false;
|
|
46
|
+
(0, runtime_1.emitNervesEvent)({
|
|
47
|
+
level: "error",
|
|
48
|
+
event: "mcp.connect_error",
|
|
49
|
+
component: "repertoire",
|
|
50
|
+
message: "MCP server connection failed",
|
|
51
|
+
meta: {
|
|
52
|
+
command: this.config.command,
|
|
53
|
+
/* v8 ignore next -- defensive: spawn errors are always Error instances @preserve */
|
|
54
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
55
|
+
},
|
|
56
|
+
});
|
|
57
|
+
throw error;
|
|
58
|
+
}
|
|
59
|
+
}
|
|
60
|
+
async listTools() {
|
|
61
|
+
if (this.cachedTools) {
|
|
62
|
+
return this.cachedTools;
|
|
63
|
+
}
|
|
64
|
+
const allTools = [];
|
|
65
|
+
let cursor;
|
|
66
|
+
do {
|
|
67
|
+
const params = {};
|
|
68
|
+
if (cursor) {
|
|
69
|
+
params.cursor = cursor;
|
|
70
|
+
}
|
|
71
|
+
const result = await this.sendRequest("tools/list", params);
|
|
72
|
+
allTools.push(...result.tools);
|
|
73
|
+
cursor = result.nextCursor;
|
|
74
|
+
} while (cursor);
|
|
75
|
+
this.cachedTools = allTools;
|
|
76
|
+
return allTools;
|
|
77
|
+
}
|
|
78
|
+
async callTool(name, args, timeout = DEFAULT_TOOL_CALL_TIMEOUT) {
|
|
79
|
+
(0, runtime_1.emitNervesEvent)({
|
|
80
|
+
event: "mcp.tool_call_start",
|
|
81
|
+
component: "repertoire",
|
|
82
|
+
message: `calling MCP tool: ${name}`,
|
|
83
|
+
meta: { tool: name },
|
|
84
|
+
});
|
|
85
|
+
try {
|
|
86
|
+
const result = await this.sendRequest("tools/call", {
|
|
87
|
+
name,
|
|
88
|
+
arguments: args,
|
|
89
|
+
}, timeout);
|
|
90
|
+
(0, runtime_1.emitNervesEvent)({
|
|
91
|
+
event: "mcp.tool_call_end",
|
|
92
|
+
component: "repertoire",
|
|
93
|
+
message: `MCP tool call completed: ${name}`,
|
|
94
|
+
meta: { tool: name },
|
|
95
|
+
});
|
|
96
|
+
return result;
|
|
97
|
+
}
|
|
98
|
+
catch (error) {
|
|
99
|
+
(0, runtime_1.emitNervesEvent)({
|
|
100
|
+
level: "error",
|
|
101
|
+
event: "mcp.tool_call_error",
|
|
102
|
+
component: "repertoire",
|
|
103
|
+
message: `MCP tool call failed: ${name}`,
|
|
104
|
+
meta: {
|
|
105
|
+
tool: name,
|
|
106
|
+
/* v8 ignore next -- defensive: callTool errors are always Error instances @preserve */
|
|
107
|
+
reason: error instanceof Error ? error.message : String(error),
|
|
108
|
+
},
|
|
109
|
+
});
|
|
110
|
+
throw error;
|
|
111
|
+
}
|
|
112
|
+
}
|
|
113
|
+
shutdown() {
|
|
114
|
+
this.connected = false;
|
|
115
|
+
this.rejectAllPending(new Error("Client shutdown"));
|
|
116
|
+
/* v8 ignore next -- defensive: process always exists during normal shutdown @preserve */
|
|
117
|
+
if (this.process && !this.process.killed) {
|
|
118
|
+
this.process.kill();
|
|
119
|
+
}
|
|
120
|
+
this.process = null;
|
|
121
|
+
}
|
|
122
|
+
isConnected() {
|
|
123
|
+
return this.connected;
|
|
124
|
+
}
|
|
125
|
+
onClose(callback) {
|
|
126
|
+
this.onCloseCallback = callback;
|
|
127
|
+
}
|
|
128
|
+
async initialize() {
|
|
129
|
+
const result = await this.sendRequest("initialize", {
|
|
130
|
+
protocolVersion: MCP_PROTOCOL_VERSION,
|
|
131
|
+
clientInfo: { name: "ouroboros", version: "1.0" },
|
|
132
|
+
capabilities: {},
|
|
133
|
+
});
|
|
134
|
+
// Send initialized notification (no id, no response expected)
|
|
135
|
+
this.writeMessage({
|
|
136
|
+
jsonrpc: "2.0",
|
|
137
|
+
method: "initialized",
|
|
138
|
+
});
|
|
139
|
+
return result;
|
|
140
|
+
}
|
|
141
|
+
sendRequest(method, params, timeout) {
|
|
142
|
+
return new Promise((resolve, reject) => {
|
|
143
|
+
if (!this.process || !this.connected && method !== "initialize") {
|
|
144
|
+
reject(new Error("MCP client is disconnected"));
|
|
145
|
+
return;
|
|
146
|
+
}
|
|
147
|
+
const id = this.nextId++;
|
|
148
|
+
const pending = { resolve, reject };
|
|
149
|
+
if (timeout) {
|
|
150
|
+
pending.timer = setTimeout(() => {
|
|
151
|
+
this.pending.delete(id);
|
|
152
|
+
reject(new Error(`MCP request timeout after ${timeout}ms: ${method}`));
|
|
153
|
+
}, timeout);
|
|
154
|
+
}
|
|
155
|
+
this.pending.set(id, pending);
|
|
156
|
+
const request = {
|
|
157
|
+
jsonrpc: "2.0",
|
|
158
|
+
id,
|
|
159
|
+
method,
|
|
160
|
+
params,
|
|
161
|
+
};
|
|
162
|
+
this.writeMessage(request);
|
|
163
|
+
});
|
|
164
|
+
}
|
|
165
|
+
writeMessage(message) {
|
|
166
|
+
/* v8 ignore next -- defensive: stdin always writable during active connection @preserve */
|
|
167
|
+
if (this.process?.stdin?.writable) {
|
|
168
|
+
this.process.stdin.write(JSON.stringify(message) + "\n");
|
|
169
|
+
}
|
|
170
|
+
}
|
|
171
|
+
setupLineReader() {
|
|
172
|
+
/* v8 ignore next -- defensive: stdout always exists after spawn @preserve */
|
|
173
|
+
if (!this.process?.stdout)
|
|
174
|
+
return;
|
|
175
|
+
const rl = (0, readline_1.createInterface)({ input: this.process.stdout });
|
|
176
|
+
rl.on("line", (line) => {
|
|
177
|
+
this.handleLine(line);
|
|
178
|
+
});
|
|
179
|
+
}
|
|
180
|
+
handleLine(line) {
|
|
181
|
+
let response;
|
|
182
|
+
try {
|
|
183
|
+
response = JSON.parse(line);
|
|
184
|
+
}
|
|
185
|
+
catch {
|
|
186
|
+
(0, runtime_1.emitNervesEvent)({
|
|
187
|
+
level: "warn",
|
|
188
|
+
event: "mcp.connect_error",
|
|
189
|
+
component: "repertoire",
|
|
190
|
+
message: "received malformed JSON from MCP server",
|
|
191
|
+
meta: { line },
|
|
192
|
+
});
|
|
193
|
+
return;
|
|
194
|
+
}
|
|
195
|
+
if (response.id === undefined || response.id === null) {
|
|
196
|
+
// Notification or invalid — ignore
|
|
197
|
+
return;
|
|
198
|
+
}
|
|
199
|
+
const pending = this.pending.get(response.id);
|
|
200
|
+
if (!pending)
|
|
201
|
+
return;
|
|
202
|
+
this.pending.delete(response.id);
|
|
203
|
+
if (pending.timer) {
|
|
204
|
+
clearTimeout(pending.timer);
|
|
205
|
+
}
|
|
206
|
+
if (response.error) {
|
|
207
|
+
pending.reject(new Error(response.error.message));
|
|
208
|
+
}
|
|
209
|
+
else {
|
|
210
|
+
pending.resolve(response.result);
|
|
211
|
+
}
|
|
212
|
+
}
|
|
213
|
+
setupProcessHandlers() {
|
|
214
|
+
/* v8 ignore next -- defensive: process always exists after spawn @preserve */
|
|
215
|
+
if (!this.process)
|
|
216
|
+
return;
|
|
217
|
+
this.process.on("error", (error) => {
|
|
218
|
+
(0, runtime_1.emitNervesEvent)({
|
|
219
|
+
level: "error",
|
|
220
|
+
event: "mcp.connect_error",
|
|
221
|
+
component: "repertoire",
|
|
222
|
+
message: "MCP server process error",
|
|
223
|
+
meta: { reason: error.message },
|
|
224
|
+
});
|
|
225
|
+
});
|
|
226
|
+
this.process.on("close", (code) => {
|
|
227
|
+
const wasConnected = this.connected;
|
|
228
|
+
this.connected = false;
|
|
229
|
+
this.rejectAllPending(new Error(`MCP server process closed with code ${code}`));
|
|
230
|
+
if (wasConnected) {
|
|
231
|
+
(0, runtime_1.emitNervesEvent)({
|
|
232
|
+
level: "error",
|
|
233
|
+
event: "mcp.connect_error",
|
|
234
|
+
component: "repertoire",
|
|
235
|
+
message: "MCP server process exited unexpectedly",
|
|
236
|
+
meta: { exitCode: code },
|
|
237
|
+
});
|
|
238
|
+
}
|
|
239
|
+
if (this.onCloseCallback) {
|
|
240
|
+
this.onCloseCallback();
|
|
241
|
+
}
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
rejectAllPending(error) {
|
|
245
|
+
for (const [id, pending] of this.pending) {
|
|
246
|
+
if (pending.timer) {
|
|
247
|
+
clearTimeout(pending.timer);
|
|
248
|
+
}
|
|
249
|
+
pending.reject(error);
|
|
250
|
+
this.pending.delete(id);
|
|
251
|
+
}
|
|
252
|
+
}
|
|
253
|
+
}
|
|
254
|
+
exports.McpClient = McpClient;
|