@ouro.bot/cli 0.1.0-alpha.1517 → 0.1.0-alpha.1518
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.json +6 -0
- package/deploy/unraid/sanctuary.ouro/bundle-meta.json +1 -1
- package/deploy/unraid/sanctuary.xml +1 -1
- package/dist/actions/generated/mailbox/actions.json +558 -7
- package/dist/actions/generated/mailbox/commands.json +168 -2
- package/dist/actions/generated/mailbox/mcp.json +413 -5
- package/dist/actions/generated/mailbox-tools/tools.json +487 -6
- package/dist/heart/daemon/cli-exec.js +19 -3
- package/dist/heart/daemon/cli-parse.js +21 -9
- package/dist/heart/daemon/daemon.js +9 -0
- package/dist/heart/letters/doors-tools.js +33 -1
- package/dist/heart/letters/doors.js +229 -19
- package/dist/heart/letters/letter-index.js +89 -0
- package/dist/heart/letters/letter.js +247 -0
- package/dist/heart/letters/store.js +210 -0
- package/dist/heart/letters/writer.js +298 -0
- package/dist/heart/mcp/mcp-server.js +37 -8
- package/dist/repertoire/in-turn-tools-door.js +1 -0
- package/dist/repertoire/tools.js +9 -3
- package/npm-shrinkwrap.json +2 -2
- package/package.json +1 -1
|
@@ -1,6 +1,8 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.IN_TURN_WAIT_MAX_S = void 0;
|
|
3
4
|
exports.declaredToolSchema = declaredToolSchema;
|
|
5
|
+
exports.mailboxToolRefusal = mailboxToolRefusal;
|
|
4
6
|
exports.mailboxToolDefinitions = mailboxToolDefinitions;
|
|
5
7
|
// The mailbox's in-turn tools, from the generated actions/generated/mailbox-tools/tools.json. They live apart from
|
|
6
8
|
// doors.ts because they need the MCP tool code, and the CLI loads doors.ts on every start: `ouro --version` must not
|
|
@@ -8,6 +10,8 @@ exports.mailboxToolDefinitions = mailboxToolDefinitions;
|
|
|
8
10
|
const runtime_1 = require("../../nerves/runtime");
|
|
9
11
|
const mcp_declared_tools_1 = require("../../repertoire/mcp-declared-tools");
|
|
10
12
|
const mcp_tools_1 = require("../../repertoire/mcp-tools");
|
|
13
|
+
const identity_1 = require("../identity");
|
|
14
|
+
const tool_error_code_1 = require("../../repertoire/tool-error-code");
|
|
11
15
|
const doors_1 = require("./doors");
|
|
12
16
|
/** The server name the census's declared-tools source gives these tools (ax/census.config.json). */
|
|
13
17
|
const MAILBOX_SERVER = "mailbox";
|
|
@@ -18,13 +22,41 @@ const MAILBOX_SERVER = "mailbox";
|
|
|
18
22
|
function declaredToolSchema(server, name, declared) {
|
|
19
23
|
return (0, mcp_tools_1.mcpToolSchema)({ server }, { name, description: declared.description, inputSchema: declared.parameters });
|
|
20
24
|
}
|
|
25
|
+
/**
|
|
26
|
+
* The longest an in-turn mailbox_wait blocks, in seconds. Turns run one at a time across the process, so a long wait
|
|
27
|
+
* would hold up every agent's turns; a longer wait belongs to the CLI or a coding tool.
|
|
28
|
+
*/
|
|
29
|
+
exports.IN_TURN_WAIT_MAX_S = 5;
|
|
30
|
+
/** One in-turn call, for the turn's own agent over its daemon socket: the result as JSON, or the refusal's text with its card's code. */
|
|
31
|
+
async function runMailboxTool(name, args, ctx) {
|
|
32
|
+
const input = name === "mailbox_wait" && !(typeof args.timeout === "number" && args.timeout <= exports.IN_TURN_WAIT_MAX_S)
|
|
33
|
+
? { ...args, timeout: exports.IN_TURN_WAIT_MAX_S }
|
|
34
|
+
: args;
|
|
35
|
+
try {
|
|
36
|
+
return JSON.stringify(await (0, doors_1.runMailboxAction)(name, input, {
|
|
37
|
+
agent: ctx?.agentName ?? (0, identity_1.getAgentName)(),
|
|
38
|
+
...(ctx?.daemonSocketPath ? { socketPath: ctx.daemonSocketPath } : {}),
|
|
39
|
+
}));
|
|
40
|
+
}
|
|
41
|
+
catch (error) {
|
|
42
|
+
if (error instanceof doors_1.MailboxDoorError)
|
|
43
|
+
return (0, tool_error_code_1.withCode)(error.message, error.code);
|
|
44
|
+
throw error;
|
|
45
|
+
}
|
|
46
|
+
}
|
|
47
|
+
/** A person-only action's refusal, which the dispatcher answers when a model calls it by name; null for any other name. */
|
|
48
|
+
function mailboxToolRefusal(name) {
|
|
49
|
+
const { refusals } = (0, doors_1.readMailboxDoors)();
|
|
50
|
+
const refusal = Object.hasOwn(refusals, name) ? refusals[name] : undefined;
|
|
51
|
+
return refusal ? (0, tool_error_code_1.withCode)(refusal.summary, "person-only") : null;
|
|
52
|
+
}
|
|
21
53
|
/** The in-turn registry's definitions: the declared tools, each running its action and answering its result as JSON. */
|
|
22
54
|
function mailboxToolDefinitions() {
|
|
23
55
|
const definitions = [...(0, doors_1.readMailboxDoors)().tools].map(([name, declared]) => ({
|
|
24
56
|
tool: declaredToolSchema(MAILBOX_SERVER, name, { description: declared.description, parameters: declared.parameters }),
|
|
25
57
|
ax: declared.ax,
|
|
26
58
|
riskProfile: (0, mcp_declared_tools_1.declaredRiskProfile)(declared.ax.effect),
|
|
27
|
-
handler: (args) =>
|
|
59
|
+
handler: (args, ctx) => runMailboxTool(name, args, ctx),
|
|
28
60
|
}));
|
|
29
61
|
(0, runtime_1.emitNervesEvent)({ component: "repertoire", event: "mailbox.tools_registered", message: "mailbox in-turn tools registered", meta: { tools: definitions.length } });
|
|
30
62
|
return definitions;
|
|
@@ -38,6 +38,9 @@ exports.resetMailboxDoorsForTest = resetMailboxDoorsForTest;
|
|
|
38
38
|
exports.doorsRoot = doorsRoot;
|
|
39
39
|
exports.readMailboxDoors = readMailboxDoors;
|
|
40
40
|
exports.runMailboxAction = runMailboxAction;
|
|
41
|
+
exports.renderMailboxResult = renderMailboxResult;
|
|
42
|
+
exports.parseMailboxCli = parseMailboxCli;
|
|
43
|
+
exports.mailboxUsageLines = mailboxUsageLines;
|
|
41
44
|
exports.mailboxCliDeclarations = mailboxCliDeclarations;
|
|
42
45
|
exports.mailboxHelp = mailboxHelp;
|
|
43
46
|
exports.mailboxMcpSchemas = mailboxMcpSchemas;
|
|
@@ -120,7 +123,10 @@ function readMailboxDoors(root = PACKAGE_ROOT) {
|
|
|
120
123
|
const actions = readGenerated(root, "mailbox/actions.json");
|
|
121
124
|
if (!Array.isArray(actions.actions))
|
|
122
125
|
throw new Error("actions/generated/mailbox/actions.json has no actions list. Fix: run ouro-actions generate.");
|
|
123
|
-
const doors = {
|
|
126
|
+
const doors = {
|
|
127
|
+
actions: actions.actions, commands: commands.commands, mcpTools: mcp.tools,
|
|
128
|
+
refusals: (mcp.refusals ?? {}), tools,
|
|
129
|
+
};
|
|
124
130
|
loaded = doors;
|
|
125
131
|
(0, runtime_1.emitNervesEvent)({
|
|
126
132
|
component: "daemon",
|
|
@@ -130,26 +136,210 @@ function readMailboxDoors(root = PACKAGE_ROOT) {
|
|
|
130
136
|
});
|
|
131
137
|
return doors;
|
|
132
138
|
}
|
|
133
|
-
|
|
134
|
-
|
|
135
|
-
|
|
136
|
-
}
|
|
137
|
-
|
|
138
|
-
|
|
139
|
-
|
|
140
|
-
|
|
141
|
-
|
|
142
|
-
|
|
143
|
-
|
|
144
|
-
|
|
139
|
+
const TYPE_WORDS = { string: "text", integer: "a whole number", boolean: "true or false", array: "a list" };
|
|
140
|
+
/** What is wrong with one value against its generated schema, naming it; null when nothing is. */
|
|
141
|
+
function problemWith(name, schema, value) {
|
|
142
|
+
const takes = `${name} takes ${TYPE_WORDS[schema.type] ?? "an object"}`;
|
|
143
|
+
switch (schema.type) {
|
|
144
|
+
case "string":
|
|
145
|
+
if (typeof value !== "string")
|
|
146
|
+
return `${takes}: ${schema.description}`;
|
|
147
|
+
if (schema.enum && !schema.enum.includes(value))
|
|
148
|
+
return `${name} is one of ${schema.enum.join(", ")}, not ${value}.`;
|
|
149
|
+
if (schema.maxLength !== undefined && [...value].length > schema.maxLength)
|
|
150
|
+
return `${name} is longer than ${schema.maxLength} characters.`;
|
|
151
|
+
return null;
|
|
152
|
+
case "integer":
|
|
153
|
+
if (!Number.isInteger(value))
|
|
154
|
+
return `${takes}: ${schema.description}`;
|
|
155
|
+
if ((schema.minimum !== undefined && value < schema.minimum) || (schema.maximum !== undefined && value > schema.maximum)) {
|
|
156
|
+
return `${name} is out of range: ${schema.description}`;
|
|
157
|
+
}
|
|
158
|
+
return null;
|
|
159
|
+
case "boolean":
|
|
160
|
+
return typeof value === "boolean" ? null : `${takes}: ${schema.description}`;
|
|
161
|
+
case "array": {
|
|
162
|
+
if (!Array.isArray(value))
|
|
163
|
+
return `${takes}: ${schema.description}`;
|
|
164
|
+
if (value.length < (schema.minItems ?? 0) || value.length > (schema.maxItems ?? Infinity))
|
|
165
|
+
return `${name} has ${value.length} items: ${schema.description}`;
|
|
166
|
+
for (const [index, item] of value.entries()) {
|
|
167
|
+
const problem = problemWith(`${name}[${index}]`, schema.items, item);
|
|
168
|
+
if (problem)
|
|
169
|
+
return problem;
|
|
170
|
+
}
|
|
171
|
+
return null;
|
|
172
|
+
}
|
|
173
|
+
default: {
|
|
174
|
+
if (typeof value !== "object" || value === null || Array.isArray(value))
|
|
175
|
+
return `${takes}: ${schema.description}`;
|
|
176
|
+
const record = value;
|
|
177
|
+
const extra = Object.keys(record).find((key) => !Object.hasOwn(schema.properties, key));
|
|
178
|
+
if (extra)
|
|
179
|
+
return `${name} has ${extra}, which it doesn't take.`;
|
|
180
|
+
const missing = schema.required.find((key) => record[key] === undefined);
|
|
181
|
+
if (missing)
|
|
182
|
+
return `${name}.${missing} is missing: ${schema.properties[missing].description}`;
|
|
183
|
+
for (const [key, item] of Object.entries(record)) {
|
|
184
|
+
const problem = problemWith(`${name}.${key}`, schema.properties[key], item);
|
|
185
|
+
if (problem)
|
|
186
|
+
return problem;
|
|
187
|
+
}
|
|
188
|
+
return null;
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
}
|
|
192
|
+
/** Errors a socket call throws when nothing listens there: the daemon is down. */
|
|
193
|
+
const DOWN = new Set(["ENOENT", "ECONNREFUSED", "ENOTSOCK"]);
|
|
194
|
+
/**
|
|
195
|
+
* Runs one mailbox action: its input is checked against the generated schema here, then the daemon's single writer
|
|
196
|
+
* runs it (src/heart/letters/writer.ts) over the socket. Refusals carry the card's code.
|
|
197
|
+
*/
|
|
198
|
+
async function runMailboxAction(action, input, caller) {
|
|
199
|
+
const doors = readMailboxDoors();
|
|
200
|
+
const refusal = Object.hasOwn(doors.refusals, action) ? doors.refusals[action] : undefined;
|
|
201
|
+
if (refusal)
|
|
202
|
+
throw new MailboxDoorError("person-only", refusal.summary);
|
|
203
|
+
const tool = doors.mcpTools.find((t) => t.name === action);
|
|
204
|
+
if (!tool)
|
|
145
205
|
throw new MailboxDoorError("unknown-door", `The mailbox has no action ${action}.`);
|
|
146
|
-
const
|
|
206
|
+
const schema = tool.inputSchema;
|
|
207
|
+
const extra = Object.keys(input).filter((key) => !Object.hasOwn(schema.properties, key));
|
|
147
208
|
if (extra.length > 0)
|
|
148
209
|
throw new MailboxDoorError("unknown-input", `${action} doesn't take ${extra.join(", ")}.`);
|
|
149
|
-
|
|
210
|
+
// ouro-actions writes every input schema with its required list, empty when nothing is.
|
|
211
|
+
const missing = schema.required.find((key) => input[key] === undefined);
|
|
212
|
+
if (missing)
|
|
213
|
+
throw new MailboxDoorError("bad-input", `${missing} is missing: ${schema.properties[missing].description}`);
|
|
214
|
+
for (const [key, value] of Object.entries(input)) {
|
|
215
|
+
const problem = problemWith(key, schema.properties[key], value);
|
|
216
|
+
if (problem)
|
|
217
|
+
throw new MailboxDoorError("bad-input", problem);
|
|
218
|
+
}
|
|
219
|
+
const send = caller.send ?? (await Promise.resolve().then(() => __importStar(require("../daemon/socket-client")))).sendDaemonCommand;
|
|
220
|
+
const socketPath = caller.socketPath ?? (await Promise.resolve().then(() => __importStar(require("../daemon/socket-client")))).DEFAULT_DAEMON_SOCKET_PATH;
|
|
221
|
+
const command = { kind: "letters.action", agent: caller.agent, action, input, ...(caller.dryRun ? { dryRun: true } : {}) };
|
|
222
|
+
// A wait holds the socket for its own timeout; anything else answers at once.
|
|
223
|
+
const waitSeconds = action === "mailbox_wait" ? Number(input.timeout ?? 60) : 0;
|
|
224
|
+
let response;
|
|
225
|
+
try {
|
|
226
|
+
response = await send(socketPath, command, { timeoutMs: (waitSeconds + 30) * 1000 });
|
|
227
|
+
}
|
|
228
|
+
catch (error) {
|
|
229
|
+
const code = error.code;
|
|
230
|
+
if (code && DOWN.has(code))
|
|
231
|
+
throw new MailboxDoorError("daemon-down", `The Ouro daemon isn't running, so ${action} did nothing.`);
|
|
232
|
+
throw error;
|
|
233
|
+
}
|
|
234
|
+
const refused = response.data;
|
|
235
|
+
if (!response.ok && typeof refused?.code === "string")
|
|
236
|
+
throw new MailboxDoorError(refused.code, String(refused.message));
|
|
237
|
+
if (!response.ok || typeof response.data !== "object" || response.data === null) {
|
|
238
|
+
throw new Error(`${action} failed in the daemon: ${response.error ?? response.message ?? "it answered nothing"}`);
|
|
239
|
+
}
|
|
240
|
+
const result = response.data;
|
|
150
241
|
(0, runtime_1.emitNervesEvent)({ component: "daemon", event: "mailbox.action_run", message: "mailbox action ran", meta: { action, outcome: result.outcome } });
|
|
151
242
|
return result;
|
|
152
243
|
}
|
|
244
|
+
/** A result as a person reads it in a shell; --json prints the result itself. */
|
|
245
|
+
function renderMailboxResult(action, result) {
|
|
246
|
+
if (action === "mailbox_list")
|
|
247
|
+
return result.count === 0 ? "No letters are waiting." : String(result.listing);
|
|
248
|
+
if (action === "mailbox_read") {
|
|
249
|
+
const letter = JSON.parse(String(result.letter));
|
|
250
|
+
return [`${letter.code} ${letter.subject} (${String(result.state)})`, ...(result.events ? [String(result.events)] : [])].join("\n");
|
|
251
|
+
}
|
|
252
|
+
if (typeof result.message === "string")
|
|
253
|
+
return result.message;
|
|
254
|
+
return `${result.outcome}: ${String(result.state)}`;
|
|
255
|
+
}
|
|
256
|
+
/** The camelCase input name of a kebab-case flag (`why-only-you` is `whyOnlyYou`). */
|
|
257
|
+
function inputName(flag) {
|
|
258
|
+
return flag.replace(/-([a-z])/g, (_, letter) => letter.toUpperCase());
|
|
259
|
+
}
|
|
260
|
+
/** `ouro mailbox <word> …` as the action's input, read the way commands.json declares its words and flags. */
|
|
261
|
+
function parseMailboxCli(word, tokens) {
|
|
262
|
+
const doors = readMailboxDoors();
|
|
263
|
+
const command = doors.commands.find((c) => c.name === `mailbox ${word}`);
|
|
264
|
+
const action = doors.actions.find((a) => a.cli === command.name).id;
|
|
265
|
+
const properties = doors.mcpTools.find((t) => t.name === action).inputSchema.properties;
|
|
266
|
+
const flags = command.flags ?? {};
|
|
267
|
+
const args = command.args ?? [];
|
|
268
|
+
const usageText = `Usage: ${mailboxUsageLines().find((line) => line.startsWith(`ouro ${command.name} `))}`;
|
|
269
|
+
const input = {};
|
|
270
|
+
const words = [];
|
|
271
|
+
let json = false;
|
|
272
|
+
let dryRun = false;
|
|
273
|
+
for (let index = 0; index < tokens.length; index++) {
|
|
274
|
+
const token = tokens[index];
|
|
275
|
+
if (token === "--json")
|
|
276
|
+
json = true;
|
|
277
|
+
else if (token === "--dry-run" && !command.readOnly)
|
|
278
|
+
dryRun = true;
|
|
279
|
+
else if (token.startsWith("--") && Object.hasOwn(flags, token.slice(2))) {
|
|
280
|
+
const name = token.slice(2);
|
|
281
|
+
const key = inputName(name);
|
|
282
|
+
if (flags[name].type === "boolean") {
|
|
283
|
+
input[key] = true;
|
|
284
|
+
continue;
|
|
285
|
+
}
|
|
286
|
+
const value = tokens[++index];
|
|
287
|
+
if (value === undefined)
|
|
288
|
+
throw new Error(`${token} needs a value.\n${usageText}`);
|
|
289
|
+
const type = properties[key].type;
|
|
290
|
+
// The generator gives a whole number flag type "number" and a list one JSON string (ouro-actions.mts).
|
|
291
|
+
if (type === "integer") {
|
|
292
|
+
input[key] = Number(value);
|
|
293
|
+
if (!Number.isFinite(input[key]))
|
|
294
|
+
throw new Error(`${token} takes a number, not ${value}.\n${usageText}`);
|
|
295
|
+
}
|
|
296
|
+
else if (type === "array") {
|
|
297
|
+
try {
|
|
298
|
+
input[key] = JSON.parse(value);
|
|
299
|
+
}
|
|
300
|
+
catch {
|
|
301
|
+
throw new Error(`${token} takes a JSON list, such as '[{"label":"Yes","effect":"…"}]'.\n${usageText}`);
|
|
302
|
+
}
|
|
303
|
+
}
|
|
304
|
+
else
|
|
305
|
+
input[key] = value;
|
|
306
|
+
}
|
|
307
|
+
else if (token.startsWith("-"))
|
|
308
|
+
throw new Error(`ouro ${command.name} doesn't take ${token}.\n${usageText}`);
|
|
309
|
+
else
|
|
310
|
+
words.push(token);
|
|
311
|
+
}
|
|
312
|
+
if (words.length > args.length)
|
|
313
|
+
throw new Error(`ouro ${command.name} takes ${args.length === 0 ? "no words" : `only <${args.map((a) => a.name).join("> <")}>`} after it: ${words.join(" ")}\n${usageText}`);
|
|
314
|
+
args.forEach((arg, index) => {
|
|
315
|
+
if (words[index] !== undefined)
|
|
316
|
+
input[arg.name] = words[index];
|
|
317
|
+
});
|
|
318
|
+
const missing = [
|
|
319
|
+
...args.filter((arg) => arg.required && input[arg.name] === undefined).map((arg) => `<${arg.name}>`),
|
|
320
|
+
...Object.entries(flags).filter(([name, flag]) => flag.required && input[inputName(name)] === undefined).map(([name]) => `--${name}`),
|
|
321
|
+
];
|
|
322
|
+
if (missing.length > 0)
|
|
323
|
+
throw new Error(`ouro ${command.name} needs ${missing.join(" and ")}.\n${usageText}`);
|
|
324
|
+
return { action, input, json, dryRun };
|
|
325
|
+
}
|
|
326
|
+
/** A command line the parser accepts, after `ouro`: each required word and flag with a sample value, a write as a dry run. */
|
|
327
|
+
function exampleFor(command) {
|
|
328
|
+
const args = (command.args ?? []).filter((arg) => arg.required).map(() => "L1");
|
|
329
|
+
const flags = Object.entries(command.flags ?? {}).filter(([, flag]) => flag.required).map(([name, flag]) => `--${name} ${flag.type === "enum" ? flag.values[0] : "example"}`);
|
|
330
|
+
return [command.name, ...args, ...flags, ...(command.readOnly ? [] : ["--dry-run"]), "--json"].join(" ");
|
|
331
|
+
}
|
|
332
|
+
/** Each generated command's usage line: its words, its flags (optional ones in brackets), --dry-run on a write, --json. */
|
|
333
|
+
function mailboxUsageLines() {
|
|
334
|
+
return readMailboxDoors().commands.map((command) => {
|
|
335
|
+
const args = (command.args ?? []).map((arg) => (arg.required ? `<${arg.name}>` : `[<${arg.name}>]`));
|
|
336
|
+
const flags = Object.entries(command.flags ?? {}).map(([name, flag]) => {
|
|
337
|
+
const value = flag.type === "boolean" ? "" : flag.type === "enum" ? ` <${flag.values.join("|")}>` : flag.type === "number" ? " <n>" : ` <${name}>`;
|
|
338
|
+
return flag.required ? `--${name}${value}` : `[--${name}${value}]`;
|
|
339
|
+
});
|
|
340
|
+
return ["ouro", command.name, ...args, ...flags, ...(command.readOnly ? [] : ["[--dry-run]"]), "[--json]"].join(" ");
|
|
341
|
+
});
|
|
342
|
+
}
|
|
153
343
|
function cardFor(name) {
|
|
154
344
|
return readMailboxDoors().tools.get(name).ax;
|
|
155
345
|
}
|
|
@@ -160,6 +350,16 @@ function cardFor(name) {
|
|
|
160
350
|
function whenFrom(where, when) {
|
|
161
351
|
return `${where}, ${when.charAt(0).toLowerCase()}${when.slice(1)}`;
|
|
162
352
|
}
|
|
353
|
+
/** The group's card for its person-only words, which refuse every agent; none when it has none. */
|
|
354
|
+
function personOnlyCard(words) {
|
|
355
|
+
if (words.length === 0)
|
|
356
|
+
return [];
|
|
357
|
+
return [{
|
|
358
|
+
code: "person-only",
|
|
359
|
+
means: `Only the person does what these words do, so they refuse every agent: ${words.join(", ")}. The refusal says how the person does it.`,
|
|
360
|
+
fix: "Tell the person what the refusal says, and leave the step to them.",
|
|
361
|
+
}];
|
|
362
|
+
}
|
|
163
363
|
/** The CLI's declarations: one per generated command, each carded from its action, then the group's own words. */
|
|
164
364
|
function mailboxCliDeclarations() {
|
|
165
365
|
const doors = readMailboxDoors();
|
|
@@ -168,18 +368,28 @@ function mailboxCliDeclarations() {
|
|
|
168
368
|
if (!action)
|
|
169
369
|
throw new Error(`actions/generated/mailbox/commands.json lists ${command.name}, but no action in actions.json names it as its cli. Fix: run ouro-actions generate.`);
|
|
170
370
|
const card = cardFor(action.id);
|
|
371
|
+
// The action's own codes, except no-agent, which the CLI's card says its own way, and unknown-input, which the
|
|
372
|
+
// flag gate answers first as unknown-flag.
|
|
373
|
+
const own = card.errors.filter((error) => error.code !== "no-agent" && error.code !== "unknown-input");
|
|
374
|
+
const errors = [
|
|
375
|
+
...own, ...(0, cards_1.agentErrors)(command.name), (0, cards_1.unknownFlag)(command.name),
|
|
376
|
+
(0, cards_1.usageError)(command.name, "is missing a word or flag it needs, or got a value it can't read; the error names it.", `Read ouro ${command.name} --help, then run it again.`),
|
|
377
|
+
];
|
|
171
378
|
const ax = {
|
|
172
|
-
id: `cli:${command.name}`, group: card.group, when: whenFrom("From a shell", card.when), effect: card.effect, errors
|
|
379
|
+
id: `cli:${command.name}`, group: card.group, when: whenFrom("From a shell", card.when), effect: card.effect, errors,
|
|
173
380
|
...(card.idempotent ? { idempotent: true } : {}),
|
|
174
381
|
};
|
|
175
|
-
return { name: command.name, effect: command.effect, ...(command.readOnly ? { readOnly: true } : {}), summary: command.summary, example:
|
|
382
|
+
return { name: command.name, effect: command.effect, ...(command.readOnly ? { readOnly: true } : {}), summary: command.summary, example: exampleFor(command), ax };
|
|
176
383
|
});
|
|
177
384
|
const words = doors.commands.map((command) => command.name.replace(/^mailbox /, "")).join(", ");
|
|
178
385
|
return [
|
|
179
386
|
...commands,
|
|
180
387
|
{
|
|
181
388
|
name: "mailbox", effect: "private", readOnly: true, summary: "The commands for the letters an agent files for its person.",
|
|
182
|
-
ax: { id: "cli:mailbox", group: "mailbox", effect: "read", when: "Names the ways to see the letters an agent has filed for its person and what they wait on.", errors: [
|
|
389
|
+
ax: { id: "cli:mailbox", group: "mailbox", effect: "read", when: "Names the ways to see the letters an agent has filed for its person and what they wait on.", errors: [
|
|
390
|
+
(0, cards_1.needsSubcommand)("mailbox", words), (0, cards_1.unknownFlag)("mailbox"),
|
|
391
|
+
...personOnlyCard(Object.values(doors.refusals).map((refusal) => `ouro ${String(refusal.action)}`)),
|
|
392
|
+
] },
|
|
183
393
|
},
|
|
184
394
|
];
|
|
185
395
|
}
|
|
@@ -194,7 +404,7 @@ function mailboxHelp() {
|
|
|
194
404
|
example: `ouro ${commands[0].name}`,
|
|
195
405
|
subcommands,
|
|
196
406
|
},
|
|
197
|
-
commands: Object.fromEntries(commands.map((command) => [command.name, { description: command.summary, usage:
|
|
407
|
+
commands: Object.fromEntries(commands.map((command, index) => [command.name, { description: command.summary, usage: mailboxUsageLines()[index], example: `ouro ${exampleFor(command)}` }])),
|
|
198
408
|
};
|
|
199
409
|
}
|
|
200
410
|
/** The MCP server's schemas: the generated tools, each carded from its action as an `mcp:` door. */
|
|
@@ -0,0 +1,89 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.LetterIndex = exports.ANSWERED_DEDUPE_MS = void 0;
|
|
4
|
+
exports.dedupeScope = dedupeScope;
|
|
5
|
+
// The daemon's in-memory index of letters (design §3.2): every letter's id, code, state and dedupe scope, rebuilt from
|
|
6
|
+
// the files when the daemon starts. There is no database; an agent has tens of open letters, not thousands.
|
|
7
|
+
const runtime_1 = require("../../nerves/runtime");
|
|
8
|
+
const letter_1 = require("./letter");
|
|
9
|
+
/** A send matching a letter answered this recently gets that answer, not a second letter. */
|
|
10
|
+
exports.ANSWERED_DEDUPE_MS = 24 * 60 * 60 * 1000;
|
|
11
|
+
/** `(from.agent, from.peer?.did ?? "local", dedupeKey)` (review M4); null when the letter has no dedupe key. */
|
|
12
|
+
function dedupeScope(letter) {
|
|
13
|
+
if (!letter.dedupeKey)
|
|
14
|
+
return null;
|
|
15
|
+
return JSON.stringify([letter.from.agent, letter.from.peer?.did ?? "local", letter.dedupeKey]);
|
|
16
|
+
}
|
|
17
|
+
function codeNumber(code) {
|
|
18
|
+
const match = /^L(\d+)$/.exec(code);
|
|
19
|
+
return match ? Number(match[1]) : 0;
|
|
20
|
+
}
|
|
21
|
+
class LetterIndex {
|
|
22
|
+
recordedNextCode;
|
|
23
|
+
byId = new Map();
|
|
24
|
+
highestCode = 0;
|
|
25
|
+
constructor(letters, recordedNextCode) {
|
|
26
|
+
this.recordedNextCode = recordedNextCode;
|
|
27
|
+
for (const stored of letters)
|
|
28
|
+
this.add(stored);
|
|
29
|
+
(0, runtime_1.emitNervesEvent)({ component: "daemon", event: "letters.index_built", message: "letters index built", meta: { letters: this.byId.size } });
|
|
30
|
+
}
|
|
31
|
+
add(stored) {
|
|
32
|
+
const entry = { ...stored, events: [...stored.events], state: (0, letter_1.letterState)(stored.events) };
|
|
33
|
+
this.byId.set(stored.letter.id, entry);
|
|
34
|
+
this.highestCode = Math.max(this.highestCode, codeNumber(stored.letter.code));
|
|
35
|
+
return entry;
|
|
36
|
+
}
|
|
37
|
+
addEvent(id, event) {
|
|
38
|
+
const entry = this.byId.get(id);
|
|
39
|
+
if (!entry)
|
|
40
|
+
throw new Error(`The index has no letter ${id}.`);
|
|
41
|
+
entry.events.push(event);
|
|
42
|
+
entry.state = (0, letter_1.letterState)(entry.events);
|
|
43
|
+
return entry;
|
|
44
|
+
}
|
|
45
|
+
markClosed(id) {
|
|
46
|
+
this.byId.get(id).closed = true;
|
|
47
|
+
}
|
|
48
|
+
/** A letter by its id, or by its code in any case ("l7" finds L7). */
|
|
49
|
+
get(idOrCode) {
|
|
50
|
+
const byId = this.byId.get(idOrCode);
|
|
51
|
+
if (byId)
|
|
52
|
+
return byId;
|
|
53
|
+
const code = idOrCode.toUpperCase();
|
|
54
|
+
return [...this.byId.values()].find((entry) => entry.letter.code === code);
|
|
55
|
+
}
|
|
56
|
+
/** Waiting letters, or every letter with `all`, oldest first; narrowed by kind or by the agent that filed them. */
|
|
57
|
+
list(filter) {
|
|
58
|
+
return [...this.byId.values()]
|
|
59
|
+
.filter((entry) => (filter.all || entry.state === "waiting")
|
|
60
|
+
&& (!filter.kind || entry.letter.kind === filter.kind)
|
|
61
|
+
&& (!filter.from || entry.letter.from.agent === filter.from))
|
|
62
|
+
.sort((a, b) => a.letter.createdAt.localeCompare(b.letter.createdAt));
|
|
63
|
+
}
|
|
64
|
+
/** The letter a new send duplicates: one waiting in the same scope, or one answered there in the last 24 hours. */
|
|
65
|
+
findDuplicate(candidate, now) {
|
|
66
|
+
const scope = dedupeScope(candidate);
|
|
67
|
+
if (!scope)
|
|
68
|
+
return null;
|
|
69
|
+
for (const entry of this.byId.values()) {
|
|
70
|
+
if (dedupeScope(entry.letter) !== scope)
|
|
71
|
+
continue;
|
|
72
|
+
if (entry.state === "waiting")
|
|
73
|
+
return { entry, why: "open" };
|
|
74
|
+
const answer = entry.events.find((event) => event.t === "answered");
|
|
75
|
+
if (answer && now - Date.parse(answer.at) <= exports.ANSWERED_DEDUPE_MS)
|
|
76
|
+
return { entry, why: "answered" };
|
|
77
|
+
}
|
|
78
|
+
return null;
|
|
79
|
+
}
|
|
80
|
+
/** Records that the counter moved past a code, so a create that fails after it can't hand the code out again. */
|
|
81
|
+
reserve(next) {
|
|
82
|
+
this.recordedNextCode = Math.max(this.recordedNextCode, next);
|
|
83
|
+
}
|
|
84
|
+
/** The next code number: past the recorded counter and past every code on disk, so none is ever reused. */
|
|
85
|
+
nextCode() {
|
|
86
|
+
return Math.max(this.recordedNextCode, this.highestCode + 1);
|
|
87
|
+
}
|
|
88
|
+
}
|
|
89
|
+
exports.LetterIndex = LetterIndex;
|