@ouro.bot/cli 0.1.0-alpha.1679 → 0.1.0-alpha.1680
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/heart/daemon/cli-commands/entry-points.js +30 -0
- package/dist/heart/versioning/ouro-bot-wrapper.js +9 -1
- package/dist/nerves/coverage/file-completeness.js +2 -0
- package/dist/ouro-ax/ax-command.js +794 -0
- package/dist/ouro-ax/effects.js +51 -0
- package/dist/ouro-ax/exit-codes.js +51 -0
- package/dist/ouro-ax/help.js +181 -0
- package/dist/ouro-ax/outcome.js +221 -0
- package/dist/ouro-ax/precise-copy.js +200 -0
- package/dist/ouro-ax/problem.js +95 -0
- package/dist/ouro-ax/voice.js +83 -0
- package/dist/ouro-ax/wait.js +158 -0
- package/npm-shrinkwrap.json +2 -2
- package/package.json +4 -1
|
@@ -0,0 +1,200 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VAGUE_FAILURE_PHRASES = exports.VAGUE_CAUSE = exports.PRECISE_FAILURE_KEY = exports.NOT_BARE_LEADS = exports.NOT_A_CAUSE = exports.LEAD_FAILURE = exports.FAILURE_KEYS = exports.CAUSE_MARKER = exports.BARE_FAILURE = void 0;
|
|
4
|
+
exports.checkFailureCopyAllow = checkFailureCopyAllow;
|
|
5
|
+
exports.preciseFailureProblem = preciseFailureProblem;
|
|
6
|
+
exports.statesCause = statesCause;
|
|
7
|
+
exports.vagueFailureIn = vagueFailureIn;
|
|
8
|
+
exports.vagueFailuresIn = vagueFailuresIn;
|
|
9
|
+
/** A phrase's name, then its pattern. */
|
|
10
|
+
const VAGUE_FAILURE_PHRASES = Object.freeze([
|
|
11
|
+
["couldn't", /\bcouldn[’']?t\b/i],
|
|
12
|
+
["could not", /\bcould not\b/i],
|
|
13
|
+
["something went wrong", /\bsomething went wrong\b/i],
|
|
14
|
+
["unable to", /\bunable to\b/i],
|
|
15
|
+
["failed to load", /\bfailed to load\b/i],
|
|
16
|
+
["an error occurred", /\ban? (?:unknown |unexpected )?error (?:has )?occurred\b|\ban (?:unknown|unexpected) error\b/i],
|
|
17
|
+
]);
|
|
18
|
+
exports.VAGUE_FAILURE_PHRASES = VAGUE_FAILURE_PHRASES;
|
|
19
|
+
/**
|
|
20
|
+
* A bare failure: the whole text is "error" or "failed" (or "failure"), at most two words before it, or "Error:" with nothing after.
|
|
21
|
+
*
|
|
22
|
+
* @claim "at most two words before it" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
23
|
+
*/
|
|
24
|
+
const BARE_FAILURE = /^[^\p{L}\p{N}]*(?:(?:\p{L}[\p{L}'’-]*\s+){0,2}(?:error|errors|failed|failure)|error\s*:)[^\p{L}\p{N}]*$/iu;
|
|
25
|
+
exports.BARE_FAILURE = BARE_FAILURE;
|
|
26
|
+
/** What marks a failure's cause after the phrase, in the same sentence: a colon, a dash or "because". */
|
|
27
|
+
const CAUSE_MARKER = /:|\s[—–-]\s|\bbecause\b/iu;
|
|
28
|
+
exports.CAUSE_MARKER = CAUSE_MARKER;
|
|
29
|
+
/**
|
|
30
|
+
* First words that make the words after a marker a next step ("try again") or a vague word ("error") in place of a cause.
|
|
31
|
+
*
|
|
32
|
+
* @claim "First words that make the words after a marker a next step" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
33
|
+
*/
|
|
34
|
+
const NOT_A_CAUSE = Object.freeze(["try", "please", "retry", "contact", "see", "error", "errors", "unknown", "failed", "failure"]);
|
|
35
|
+
exports.NOT_A_CAUSE = NOT_A_CAUSE;
|
|
36
|
+
/**
|
|
37
|
+
* A cause that names nothing: the whole of it is "it broke", "it didn't work", "there was a problem", "an issue occurred"
|
|
38
|
+
* and the like, so it says neither what failed nor why. A cause that goes on ("it failed because the disk is full") names
|
|
39
|
+
* something and passes.
|
|
40
|
+
*
|
|
41
|
+
* @claim "A cause that names nothing" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
42
|
+
*/
|
|
43
|
+
const VAGUE_CAUSE = /^[^\p{L}\p{N}]*(?:(?:it|this|that|something)\s+(?:broke|failed|crashed|errored|stopped(?:\s+working)?|went\s+wrong|(?:didn[’']?t|did\s+not|doesn[’']?t|does\s+not)\s+work|(?:isn[’']?t|is\s+not)\s+working)|there\s+(?:was|is)\s+(?:an?\s+)?(?:problem|issue|error)|an?\s+(?:problem|issue|error)\s+(?:has\s+)?(?:occurred|happened))[^\p{L}\p{N}]*$/iu;
|
|
44
|
+
exports.VAGUE_CAUSE = VAGUE_CAUSE;
|
|
45
|
+
/** A failure that leads the text ("Error: …", "Upload failed - …"), held to the same test as a phrase; the group is the marker. */
|
|
46
|
+
const LEAD_FAILURE = /^[^\p{L}\p{N}]*(?:\p{L}[\p{L}'’-]*\s+){0,2}(?:error|errors|failed|failure)\s*(:|\s[—–-]\s)/iu;
|
|
47
|
+
exports.LEAD_FAILURE = LEAD_FAILURE;
|
|
48
|
+
/**
|
|
49
|
+
* Words before "error" or "failed" that make the text other than a failure: a negation ("No errors") or an action ("Retry failed").
|
|
50
|
+
*
|
|
51
|
+
* @claim "that make the text other than a failure" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
52
|
+
*/
|
|
53
|
+
const NOT_BARE_LEADS = Object.freeze(["no", "nothing", "zero", "none", "show", "hide", "retry", "clear"]);
|
|
54
|
+
exports.NOT_BARE_LEADS = NOT_BARE_LEADS;
|
|
55
|
+
/**
|
|
56
|
+
* The fields of an answer that speak to the agent or the person: a refusal's or an outcome's `error`, `human` (its
|
|
57
|
+
* `reason` and `resume`), next steps and fixes, and in an Ouro AX answer (one with an `ax` field: an outcome, a checks
|
|
58
|
+
* report) its `summary` and each finding's `detail`.
|
|
59
|
+
*
|
|
60
|
+
* @claim "each finding's `detail`" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
61
|
+
*/
|
|
62
|
+
const FAILURE_KEYS = new Set(["error", "human", "reason", "next", "fix", "resume"]);
|
|
63
|
+
exports.FAILURE_KEYS = FAILURE_KEYS;
|
|
64
|
+
const AX_KEYS = new Set(["summary", "detail"]);
|
|
65
|
+
/** The field on an outcome's `error` or `human`, or a checks row, that says why its short failure text is precise. */
|
|
66
|
+
const PRECISE_FAILURE_KEY = "preciseFailure";
|
|
67
|
+
exports.PRECISE_FAILURE_KEY = PRECISE_FAILURE_KEY;
|
|
68
|
+
/**
|
|
69
|
+
* What is wrong with a record's `preciseFailure`, or null when the field is absent or holds a reason. A blank or
|
|
70
|
+
* non-text value is skipped, so the detectors keep reading the record's text.
|
|
71
|
+
*
|
|
72
|
+
* @example
|
|
73
|
+
* preciseFailureProblem({ code: "x", preciseFailure: " " }); // "preciseFailure needs a reason in words"
|
|
74
|
+
*/
|
|
75
|
+
function preciseFailureProblem(record) {
|
|
76
|
+
const reason = record[PRECISE_FAILURE_KEY];
|
|
77
|
+
return reason === undefined || (typeof reason === "string" && reason.trim().length > 0) ? null : "preciseFailure needs a reason in words";
|
|
78
|
+
}
|
|
79
|
+
/** A code or a status ("failed", "not_found"), not copy. */
|
|
80
|
+
const TOKEN = /^[a-z][\w.:-]*$/;
|
|
81
|
+
/**
|
|
82
|
+
* The vague failure phrase in a message, by its name, or null when there is none or an allow-list entry lets it
|
|
83
|
+
* through. A text that is only "Error" or "Upload failed" is "bare error" or "bare failed".
|
|
84
|
+
*
|
|
85
|
+
* @claim "or null when there is none or an allow-list entry lets it through" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
86
|
+
*
|
|
87
|
+
* @example
|
|
88
|
+
* vagueFailureIn("Couldn't show this picture"); // "couldn't"
|
|
89
|
+
* vagueFailureIn("The photo service says this picture was deleted. Ask its owner to share it again."); // null
|
|
90
|
+
*/
|
|
91
|
+
function vagueFailureIn(text, allow = []) {
|
|
92
|
+
const phrase = phraseIn(text);
|
|
93
|
+
return phrase && !allowedBy(text, allow) ? phrase : null;
|
|
94
|
+
}
|
|
95
|
+
const SENTENCE_END = /[.!?](?=\s|$)/u;
|
|
96
|
+
const WORD = /\p{L}[\p{L}\p{N}'’-]*/gu;
|
|
97
|
+
const wordsOf = (text) => text.toLowerCase().match(WORD) ?? [];
|
|
98
|
+
/**
|
|
99
|
+
* Whether the text after a failure phrase states the cause in the same sentence: after a colon, a dash or "because", a
|
|
100
|
+
* substitution (`${…}`) or at least two words whose first is not in {@link NOT_A_CAUSE}, that are not a cause naming nothing
|
|
101
|
+
* ({@link VAGUE_CAUSE}), with no vague phrase in it.
|
|
102
|
+
*
|
|
103
|
+
* @claim "at least two words whose first is not in" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::finds each vague phrase and a bare failure, and lets precise copy and allowed text through)
|
|
104
|
+
*
|
|
105
|
+
* @example
|
|
106
|
+
* statesCause(" send: you're offline. Try again later."); // true
|
|
107
|
+
* statesCause(" send: try again."); // false
|
|
108
|
+
*/
|
|
109
|
+
function statesCause(rest) {
|
|
110
|
+
const sentence = rest.split(SENTENCE_END)[0];
|
|
111
|
+
const marker = CAUSE_MARKER.exec(sentence);
|
|
112
|
+
if (!marker)
|
|
113
|
+
return false;
|
|
114
|
+
const cause = sentence.slice(marker.index + marker[0].length);
|
|
115
|
+
if (cause.includes("${…}"))
|
|
116
|
+
return true;
|
|
117
|
+
const words = wordsOf(cause);
|
|
118
|
+
return words.length >= 2 && !NOT_A_CAUSE.includes(words[0]) && !VAGUE_CAUSE.test(cause) && !VAGUE_FAILURE_PHRASES.some(([, pattern]) => pattern.test(cause));
|
|
119
|
+
}
|
|
120
|
+
function phraseIn(text) {
|
|
121
|
+
const trimmed = text.trim();
|
|
122
|
+
if (TOKEN.test(trimmed))
|
|
123
|
+
return null;
|
|
124
|
+
const found = VAGUE_FAILURE_PHRASES.find(([, pattern]) => [...text.matchAll(new RegExp(pattern.source, `${pattern.flags}g`))].some((match) => !statesCause(text.slice(match.index + match[0].length))));
|
|
125
|
+
if (found)
|
|
126
|
+
return found[0];
|
|
127
|
+
const word = (failure) => (/\berrors?\b/i.test(failure) ? "bare error" : "bare failed");
|
|
128
|
+
const lead = LEAD_FAILURE.exec(trimmed);
|
|
129
|
+
if (lead && !wordsOf(lead[0]).some((w) => NOT_BARE_LEADS.includes(w)))
|
|
130
|
+
return statesCause(trimmed.slice(lead[0].length - lead[1].length)) ? null : word(lead[0]);
|
|
131
|
+
if (!BARE_FAILURE.test(trimmed))
|
|
132
|
+
return null;
|
|
133
|
+
if (wordsOf(trimmed).slice(0, -1).some((w) => NOT_BARE_LEADS.includes(w)))
|
|
134
|
+
return null;
|
|
135
|
+
return word(trimmed);
|
|
136
|
+
}
|
|
137
|
+
const allowedBy = (text, allow) => allow.find((entry) => text.includes(entry.match)) ?? null;
|
|
138
|
+
/**
|
|
139
|
+
* Every vague failure message in a value: a message, a `help --json`, an outcome, a refusal or a checks report. A
|
|
140
|
+
* finding an allow-list entry lets through carries the entry. With `messagesOnly`, only what is under
|
|
141
|
+
* {@link FAILURE_KEYS}, and an Ouro AX answer's `summary` and `detail`; a `code` is never read. A record with a
|
|
142
|
+
* `preciseFailure` reason (an outcome's `error` or `human`, a checks row) lets its own short texts, and a list of them, through, with
|
|
143
|
+
* the reason carried as the allow entry; the X-ray's `copy.precise-failure` reads the same list.
|
|
144
|
+
*
|
|
145
|
+
* @claim "a `code` is never read" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
146
|
+
* @claim "Every vague failure message in a value" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::walks a value, reading an outcome's error, human reason and summary and a checks report's findings, not the person's data)
|
|
147
|
+
*
|
|
148
|
+
* @example
|
|
149
|
+
* vagueFailuresIn({ ax: "outcome/1", state: "failed", summary: "Not sent", error: { code: "send_failed", message: "Something went wrong" } }, { messagesOnly: true });
|
|
150
|
+
* // [{ phrase: "something went wrong", where: "error.message", text: "Something went wrong", allowed: null }]
|
|
151
|
+
*/
|
|
152
|
+
function vagueFailuresIn(value, options = {}) {
|
|
153
|
+
const out = [];
|
|
154
|
+
const visit = (v, where, speaks, ax, why = null) => {
|
|
155
|
+
if (typeof v === "string") {
|
|
156
|
+
const phrase = speaks ? phraseIn(v) : null;
|
|
157
|
+
if (phrase)
|
|
158
|
+
out.push({ phrase, where, text: v, allowed: why ? { match: v, reason: why } : allowedBy(v, options.allow ?? []) });
|
|
159
|
+
return;
|
|
160
|
+
}
|
|
161
|
+
if (Array.isArray(v))
|
|
162
|
+
return v.forEach((x, i) => visit(x, `${where}[${i}]`, speaks, ax, why));
|
|
163
|
+
if (typeof v !== "object" || v === null)
|
|
164
|
+
return;
|
|
165
|
+
const record = v;
|
|
166
|
+
const inAx = ax || typeof record["ax"] === "string";
|
|
167
|
+
const reason = record[PRECISE_FAILURE_KEY];
|
|
168
|
+
const own = typeof reason === "string" && reason.trim() ? reason.trim() : null;
|
|
169
|
+
for (const [k, x] of Object.entries(record)) {
|
|
170
|
+
if (k === "code" || k === PRECISE_FAILURE_KEY)
|
|
171
|
+
continue;
|
|
172
|
+
visit(x, where ? `${where}.${k}` : k, speaks || FAILURE_KEYS.has(k) || (inAx && AX_KEYS.has(k)), inAx, own);
|
|
173
|
+
}
|
|
174
|
+
};
|
|
175
|
+
visit(value, "", !options.messagesOnly, false);
|
|
176
|
+
return out;
|
|
177
|
+
}
|
|
178
|
+
/**
|
|
179
|
+
* An allow-list read from JSON, checked: a list of `{ match, reason }`, each with a non-empty `match` and a `reason`.
|
|
180
|
+
* Throws naming each entry that is not one, with the fix.
|
|
181
|
+
*
|
|
182
|
+
* @claim "each with a non-empty `match` and a `reason`" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::an allow-list needs a match and a reason in every entry)
|
|
183
|
+
* @claim "Throws naming each entry that is not one" registry/ouro/ax/precise-copy.test.mts verify:testExists(registry/ouro/ax/precise-copy.test.mts::an allow-list needs a match and a reason in every entry)
|
|
184
|
+
*
|
|
185
|
+
* @example
|
|
186
|
+
* checkFailureCopyAllow([{ match: "could not be charged", reason: "names the card service and its answer" }]);
|
|
187
|
+
*/
|
|
188
|
+
function checkFailureCopyAllow(value) {
|
|
189
|
+
const shape = '{ "match": "<part of the text>", "reason": "<why this wording is precise>" }';
|
|
190
|
+
if (!Array.isArray(value))
|
|
191
|
+
throw new Error(`the failure copy allow-list is a list. Fix: [${shape}].`);
|
|
192
|
+
const problems = value.flatMap((entry, i) => {
|
|
193
|
+
const e = entry;
|
|
194
|
+
const ok = typeof e === "object" && e !== null && typeof e.match === "string" && e.match.length > 0 && typeof e.reason === "string" && e.reason.trim().length > 0;
|
|
195
|
+
return ok ? [] : [`entry ${i} ${JSON.stringify(entry)} needs a non-empty "match" and a "reason". Fix: ${shape}.`];
|
|
196
|
+
});
|
|
197
|
+
if (problems.length)
|
|
198
|
+
throw new Error(`the failure copy allow-list has ${problems.length} problem${problems.length === 1 ? "" : "s"}:\n${problems.join("\n")}`);
|
|
199
|
+
return value;
|
|
200
|
+
}
|
|
@@ -0,0 +1,95 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.AxError = void 0;
|
|
4
|
+
exports.commandLine = commandLine;
|
|
5
|
+
exports.didYouMean = didYouMean;
|
|
6
|
+
exports.errorJson = errorJson;
|
|
7
|
+
exports.errorLine = errorLine;
|
|
8
|
+
exports.errorMessage = errorMessage;
|
|
9
|
+
exports.isAxError = isAxError;
|
|
10
|
+
exports.shellWord = shellWord;
|
|
11
|
+
/** The error the kit's primitives throw: its message is the one line, its shape the data. */
|
|
12
|
+
class AxError extends Error {
|
|
13
|
+
shape;
|
|
14
|
+
constructor(shape) {
|
|
15
|
+
super(errorMessage(shape));
|
|
16
|
+
this.name = "AxError";
|
|
17
|
+
this.shape = shape;
|
|
18
|
+
}
|
|
19
|
+
get exit() {
|
|
20
|
+
return this.shape.exit;
|
|
21
|
+
}
|
|
22
|
+
}
|
|
23
|
+
exports.AxError = AxError;
|
|
24
|
+
function isAxError(value) {
|
|
25
|
+
return value instanceof AxError;
|
|
26
|
+
}
|
|
27
|
+
const sentence = (text) => text.trim().replace(/[.\s]+$/, "");
|
|
28
|
+
/** `<thing>: <problem>. Fix: <fix>.`, without the tool's prefix (a CLI adds its own). */
|
|
29
|
+
function errorMessage(shape) {
|
|
30
|
+
const head = `${shape.thing}: ${sentence(shape.problem)}.`;
|
|
31
|
+
return shape.fix === null ? head : `${head} Fix: ${sentence(shape.fix)}.`;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* The one stderr line: `<tool>: <thing>: <problem>. Fix: <fix>.` Line breaks in any part become spaces. A thing that is
|
|
35
|
+
* the tool itself (a one-command CLI names its only command after the tool) is not said twice.
|
|
36
|
+
*/
|
|
37
|
+
function errorLine(tool, shape) {
|
|
38
|
+
const message = shape.thing === tool ? errorMessage({ ...shape, thing: "" }).replace(/^: /, "") : errorMessage(shape);
|
|
39
|
+
return `${tool}: ${message}`.replace(/\s*[\r\n]+\s*/g, " ");
|
|
40
|
+
}
|
|
41
|
+
/** The `--json` and MCP form: `{ error: { code, exit, thing, problem, fix, message } }`. */
|
|
42
|
+
function errorJson(shape) {
|
|
43
|
+
return { error: { ...shape, message: errorMessage(shape) } };
|
|
44
|
+
}
|
|
45
|
+
// Edits between two words, a swap of two letters side by side counting as one: "romos" is one edit from "rooms".
|
|
46
|
+
function distance(a, b) {
|
|
47
|
+
const rows = Array.from({ length: a.length + 1 }, (_, i) => Array.from({ length: b.length + 1 }, (_, j) => (i === 0 ? j : j === 0 ? i : 0)));
|
|
48
|
+
for (let i = 1; i <= a.length; i++) {
|
|
49
|
+
for (let j = 1; j <= b.length; j++) {
|
|
50
|
+
const row = rows[i];
|
|
51
|
+
row[j] = Math.min(rows[i - 1][j] + 1, row[j - 1] + 1, rows[i - 1][j - 1] + (a[i - 1] === b[j - 1] ? 0 : 1));
|
|
52
|
+
if (i > 1 && j > 1 && a[i - 1] === b[j - 2] && a[i - 2] === b[j - 1])
|
|
53
|
+
row[j] = Math.min(row[j], rows[i - 2][j - 2] + 1);
|
|
54
|
+
}
|
|
55
|
+
}
|
|
56
|
+
return rows[a.length][b.length];
|
|
57
|
+
}
|
|
58
|
+
/**
|
|
59
|
+
* The candidate an agent most likely meant: the nearest within an edit for every three letters of the word (at least one,
|
|
60
|
+
* at most two), else a unique prefix; null when neither applies.
|
|
61
|
+
*
|
|
62
|
+
* @claim "the nearest within an edit for every three letters of the word" registry/ouro/ax/problem.test.mts verify:testExists(registry/ouro/ax/problem.test.mts::didYouMean: the nearest within a couple of edits, else a unique prefix, else null) verify:testExists(registry/ouro/ax/problem.test.mts::didYouMean: a short word suggests only a name an edit away, not one that changes half its letters)
|
|
63
|
+
*/
|
|
64
|
+
function didYouMean(word, candidates) {
|
|
65
|
+
if (word === "")
|
|
66
|
+
return null;
|
|
67
|
+
let best = null;
|
|
68
|
+
// Scaled to the word: two edits in a four-letter word change half of it, which is a different word, not a typo (#1670).
|
|
69
|
+
let bestDistance = Math.min(2, Math.max(1, Math.floor(word.length / 3))) + 1;
|
|
70
|
+
for (const c of candidates) {
|
|
71
|
+
const d = distance(word, c);
|
|
72
|
+
if (d < bestDistance) {
|
|
73
|
+
best = c;
|
|
74
|
+
bestDistance = d;
|
|
75
|
+
}
|
|
76
|
+
}
|
|
77
|
+
if (best)
|
|
78
|
+
return best;
|
|
79
|
+
const prefixed = candidates.filter((c) => c.startsWith(word) || word.startsWith(c));
|
|
80
|
+
return prefixed.length === 1 ? prefixed[0] : null;
|
|
81
|
+
}
|
|
82
|
+
/** Quotes a word for a POSIX shell only when it needs it, so a fix copies and runs as printed. */
|
|
83
|
+
function shellWord(word) {
|
|
84
|
+
if (/^<[^>\s]+>$/.test(word) || /^[A-Za-z0-9_./:@#,=+-]+$/.test(word))
|
|
85
|
+
return word;
|
|
86
|
+
return `"${word.replace(/(["\\$`])/g, "\\$1")}"`;
|
|
87
|
+
}
|
|
88
|
+
/**
|
|
89
|
+
* A command line from words, each quoted only when it needs it.
|
|
90
|
+
*
|
|
91
|
+
* @claim "each quoted only when" registry/ouro/ax/problem.test.mts verify:testExists(registry/ouro/ax/problem.test.mts::commandLine quotes only what a shell would split or expand)
|
|
92
|
+
*/
|
|
93
|
+
function commandLine(words) {
|
|
94
|
+
return words.map(shellWord).join(" ");
|
|
95
|
+
}
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.VOICE_MAX = void 0;
|
|
4
|
+
exports.checkVoice = checkVoice;
|
|
5
|
+
exports.checkVoiceLine = checkVoiceLine;
|
|
6
|
+
exports.createVoice = createVoice;
|
|
7
|
+
exports.isCi = isCi;
|
|
8
|
+
exports.voiceLines = voiceLines;
|
|
9
|
+
exports.voiceOn = voiceOn;
|
|
10
|
+
/**
|
|
11
|
+
* The longest voice line, in characters.
|
|
12
|
+
*
|
|
13
|
+
* @claim "The longest voice line" registry/ouro/ax/voice.test.mts verify:testExists(registry/ouro/ax/voice.test.mts::a line is one line of plain ASCII, at most 72 characters, checked when the tool is built)
|
|
14
|
+
*/
|
|
15
|
+
const VOICE_MAX = 72;
|
|
16
|
+
exports.VOICE_MAX = VOICE_MAX;
|
|
17
|
+
/** What is wrong with a voice line, or null when it is one line of printable ASCII, at most {@link VOICE_MAX} characters. */
|
|
18
|
+
function checkVoiceLine(line) {
|
|
19
|
+
if (line.trim() === "")
|
|
20
|
+
return "is empty";
|
|
21
|
+
if (/[\r\n]/.test(line))
|
|
22
|
+
return "is more than one line";
|
|
23
|
+
if (/[^\x20-\x7e]/.test(line))
|
|
24
|
+
return "is not plain ASCII (no emoji, curly quotes or tabs)";
|
|
25
|
+
if (line.length > VOICE_MAX)
|
|
26
|
+
return `is ${line.length} characters; the most is ${VOICE_MAX}`;
|
|
27
|
+
return null;
|
|
28
|
+
}
|
|
29
|
+
/** Throws at definition time when a fixed voice line breaks the rules; `done` lines are checked as they are made. */
|
|
30
|
+
function checkVoice(tool, voice) {
|
|
31
|
+
for (const slot of ["banner", "summary", "empty", "healthy", "waited"]) {
|
|
32
|
+
for (const line of voice[slot] ?? []) {
|
|
33
|
+
const problem = checkVoiceLine(line);
|
|
34
|
+
if (problem)
|
|
35
|
+
throw new Error(`${tool}: the voice's ${slot} line ${JSON.stringify(line)} ${problem}. Fix: one line of plain ASCII, at most ${VOICE_MAX} characters.`);
|
|
36
|
+
}
|
|
37
|
+
}
|
|
38
|
+
}
|
|
39
|
+
/** The fixed voice lines (banner, summary, empty, healthy and waited), as `help --json` lists them for a checker. */
|
|
40
|
+
function voiceLines(voice) {
|
|
41
|
+
return [...(voice.banner ?? []), ...(voice.summary ?? []), ...(voice.empty ?? []), ...(voice.healthy ?? []), ...(voice.waited ?? [])];
|
|
42
|
+
}
|
|
43
|
+
/**
|
|
44
|
+
* Whether the `CI` environment variable says this is a CI run: set, and not empty, "0" or "false".
|
|
45
|
+
*
|
|
46
|
+
* @claim "set, and not empty" registry/ouro/ax/voice.test.mts verify:testExists(registry/ouro/ax/voice.test.mts::voiceOn: only a terminal, without --json, --quiet or CI)
|
|
47
|
+
*/
|
|
48
|
+
function isCi(env) {
|
|
49
|
+
const ci = env["CI"];
|
|
50
|
+
return ci !== undefined && !["", "0", "false"].includes(ci.trim().toLowerCase());
|
|
51
|
+
}
|
|
52
|
+
/** Whether a voice line may print: stdout is a terminal, and not `--json`, `--quiet` or CI. */
|
|
53
|
+
function voiceOn(options) {
|
|
54
|
+
return options.tty && !options.json && !options.quiet && !isCi(options.env);
|
|
55
|
+
}
|
|
56
|
+
function processMemory() {
|
|
57
|
+
const seen = new Map();
|
|
58
|
+
return { last: (slot) => seen.get(slot) ?? null, remember: (slot, index) => void seen.set(slot, index) };
|
|
59
|
+
}
|
|
60
|
+
/** One process's voice: it rotates the variants of a slot and gives at most one `done` line. */
|
|
61
|
+
function createVoice(voice, options) {
|
|
62
|
+
const on = voiceOn(options);
|
|
63
|
+
const memory = options.memory ?? processMemory();
|
|
64
|
+
const start = options.start ?? Math.floor(Date.now() / 1000);
|
|
65
|
+
let celebrated = false;
|
|
66
|
+
return {
|
|
67
|
+
on,
|
|
68
|
+
line(slot, outcome) {
|
|
69
|
+
if (!on)
|
|
70
|
+
return null;
|
|
71
|
+
const all = slot === "done" ? (outcome && voice.done ? voice.done(outcome) : []) : (voice[slot] ?? []);
|
|
72
|
+
const lines = all.filter((l) => checkVoiceLine(l) === null);
|
|
73
|
+
if (!lines.length || (slot === "done" && celebrated))
|
|
74
|
+
return null;
|
|
75
|
+
if (slot === "done")
|
|
76
|
+
celebrated = true;
|
|
77
|
+
const last = memory.last(slot);
|
|
78
|
+
const index = last === null ? start % lines.length : (last + 1) % lines.length;
|
|
79
|
+
memory.remember(slot, index);
|
|
80
|
+
return lines[index];
|
|
81
|
+
},
|
|
82
|
+
};
|
|
83
|
+
}
|
|
@@ -0,0 +1,158 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.WAIT_VERSION = exports.WAIT_STATES = exports.WAIT_FLAGS = exports.DEFAULT_WAIT_EXITS = void 0;
|
|
4
|
+
exports.formatDuration = formatDuration;
|
|
5
|
+
exports.isWait = isWait;
|
|
6
|
+
exports.parseDuration = parseDuration;
|
|
7
|
+
exports.validateWait = validateWait;
|
|
8
|
+
exports.wait = wait;
|
|
9
|
+
exports.waitExit = waitExit;
|
|
10
|
+
exports.waitFor = waitFor;
|
|
11
|
+
exports.waitText = waitText;
|
|
12
|
+
/**
|
|
13
|
+
* A typed wait: the agent names a reference it already holds, the tool waits (polling, if it must, where the agent
|
|
14
|
+
* cannot see it) until something happens or the time limit passes, and returns what happened with the cursor that
|
|
15
|
+
* resumes the wait. The agent waits with one call instead of a polling loop of its own.
|
|
16
|
+
*/
|
|
17
|
+
const WAIT_VERSION = "wait/1";
|
|
18
|
+
exports.WAIT_VERSION = WAIT_VERSION;
|
|
19
|
+
/**
|
|
20
|
+
* How a wait ended.
|
|
21
|
+
* - `satisfied`: it happened; `events` holds what happened.
|
|
22
|
+
* - `waiting`: still waiting (a stream's first line); keep reading or resume.
|
|
23
|
+
* - `timed-out`: nothing happened in time; `resume` waits again from the same cursor.
|
|
24
|
+
* - `gone`: what was awaited cannot happen any more (deleted, left, closed).
|
|
25
|
+
*/
|
|
26
|
+
const WAIT_STATES = Object.freeze(["satisfied", "waiting", "timed-out", "gone"]);
|
|
27
|
+
exports.WAIT_STATES = WAIT_STATES;
|
|
28
|
+
/** Builds a wait; a `timed-out` or `waiting` wait needs `resume`, which {@link validateWait} checks. */
|
|
29
|
+
function wait(fields) {
|
|
30
|
+
const { for: what, state, since, target = null, until = null, cursor = null, events = [], fresh = null, resume = null, reason = null, callback = null, next = [] } = fields;
|
|
31
|
+
return { ax: WAIT_VERSION, for: what, target, state, since, until, cursor, events, fresh, resume, reason, callback, next };
|
|
32
|
+
}
|
|
33
|
+
const isRecord = (v) => typeof v === "object" && v !== null && !Array.isArray(v);
|
|
34
|
+
const isText = (v) => typeof v === "string" && v.trim().length > 0;
|
|
35
|
+
const isStep = (v) => isRecord(v) && isText(v["command"]) && isText(v["why"]);
|
|
36
|
+
/** Whether a value is a wait of this version (its shape only). */
|
|
37
|
+
function isWait(v) {
|
|
38
|
+
return isRecord(v) && v["ax"] === WAIT_VERSION && WAIT_STATES.includes(v["state"]);
|
|
39
|
+
}
|
|
40
|
+
/** Checks a wait: one that can go on names how to resume it, and a satisfied one carries what happened. */
|
|
41
|
+
function validateWait(v) {
|
|
42
|
+
const errors = [];
|
|
43
|
+
const warnings = [];
|
|
44
|
+
if (!isWait(v))
|
|
45
|
+
return { ok: false, errors: [`not a ${WAIT_VERSION} envelope: ax must be "${WAIT_VERSION}" and state one of ${WAIT_STATES.join(", ")}`], warnings };
|
|
46
|
+
if (!isText(v.for))
|
|
47
|
+
errors.push("for is empty: say what is awaited");
|
|
48
|
+
if (!isText(v.since))
|
|
49
|
+
errors.push("since is empty");
|
|
50
|
+
if (!Array.isArray(v.events))
|
|
51
|
+
errors.push("events is a list");
|
|
52
|
+
else if (v.state === "satisfied" && v.events.length === 0)
|
|
53
|
+
errors.push("satisfied, but no event says what happened");
|
|
54
|
+
if ((v.state === "waiting" || v.state === "timed-out") && !isStep(v.resume))
|
|
55
|
+
errors.push(`${v.state} needs resume: the exact command that waits again from the cursor`);
|
|
56
|
+
else if (v.resume !== null && !isStep(v.resume))
|
|
57
|
+
errors.push("resume needs a command and a why");
|
|
58
|
+
if (v.state !== "gone" && v.cursor === null)
|
|
59
|
+
warnings.push("no cursor: a resumed wait may miss or repeat events");
|
|
60
|
+
if (v.fresh === null)
|
|
61
|
+
warnings.push("no freshness: nothing seen is not the same as nothing happened");
|
|
62
|
+
if (!Array.isArray(v.next) || !v.next.every(isStep))
|
|
63
|
+
errors.push("every next step needs a command and a why");
|
|
64
|
+
return { ok: errors.length === 0, errors, warnings };
|
|
65
|
+
}
|
|
66
|
+
/**
|
|
67
|
+
* The default: a satisfied or open wait exits `ok`; a wait that timed out or can no longer happen exits `error`.
|
|
68
|
+
*
|
|
69
|
+
* @claim "a wait that timed out or can no longer happen exits" registry/ouro/ax/wait.test.mts verify:testExists(registry/ouro/ax/wait.test.mts::waitExit and waitText)
|
|
70
|
+
*/
|
|
71
|
+
const DEFAULT_WAIT_EXITS = Object.freeze({ satisfied: "ok", waiting: "ok", "timed-out": "error", gone: "error" });
|
|
72
|
+
exports.DEFAULT_WAIT_EXITS = DEFAULT_WAIT_EXITS;
|
|
73
|
+
/** The exit code a wait gives, by its state's name in the tool's table. */
|
|
74
|
+
function waitExit(w, exits, names = DEFAULT_WAIT_EXITS) {
|
|
75
|
+
return exits.code(names[w.state]);
|
|
76
|
+
}
|
|
77
|
+
/** The wait as text: what happened (one line per event), or that nothing did, then how to resume. */
|
|
78
|
+
function waitText(w) {
|
|
79
|
+
const head = { satisfied: `Happened: ${w.for}.`, waiting: `Waiting for ${w.for}.`, "timed-out": `Nothing yet: ${w.for}, waited until ${w.until ?? "the limit"}.`, gone: `It can't happen any more: ${w.for}${w.reason ? `: ${w.reason.replace(/[.\s]+$/, "")}` : ""}.` }[w.state];
|
|
80
|
+
const lines = [head, ...w.events.map((e) => `${e.at} ${e.by}: ${e.summary}`)];
|
|
81
|
+
if (w.fresh && w.fresh.state !== "live")
|
|
82
|
+
lines.push(`Freshness: ${w.fresh.state}${w.fresh.asOf ? ` as of ${w.fresh.asOf}` : ""}.`);
|
|
83
|
+
if (w.resume)
|
|
84
|
+
lines.push(`Resume: ${w.resume.command} (${w.resume.why})`);
|
|
85
|
+
for (const n of w.next)
|
|
86
|
+
lines.push(`Next: ${n.command} (${n.why})`);
|
|
87
|
+
return lines.join("\n");
|
|
88
|
+
}
|
|
89
|
+
const UNITS = Object.freeze({ ms: 1, s: 1000, m: 60_000, h: 3_600_000 });
|
|
90
|
+
/**
|
|
91
|
+
* "90s", "2m", "1h", "500ms" or a bare number of seconds, as milliseconds; null when it is none of these.
|
|
92
|
+
*
|
|
93
|
+
* @claim "or a bare number of seconds" registry/ouro/ax/wait.test.mts verify:testExists(registry/ouro/ax/wait.test.mts::parseDuration and formatDuration read and write the same durations)
|
|
94
|
+
*/
|
|
95
|
+
function parseDuration(text) {
|
|
96
|
+
const m = /^\s*(\d+(?:\.\d+)?)\s*(ms|s|m|h)?\s*$/.exec(text);
|
|
97
|
+
if (!m)
|
|
98
|
+
return null;
|
|
99
|
+
return Math.round(Number(m[1]) * (UNITS[m[2] ?? "s"]));
|
|
100
|
+
}
|
|
101
|
+
/** Milliseconds as the shortest exact duration {@link parseDuration} reads back: "2m", "90s", "500ms". */
|
|
102
|
+
function formatDuration(ms) {
|
|
103
|
+
for (const unit of ["h", "m", "s"]) {
|
|
104
|
+
const size = UNITS[unit];
|
|
105
|
+
if (ms >= size && ms % size === 0)
|
|
106
|
+
return `${ms / size}${unit}`;
|
|
107
|
+
}
|
|
108
|
+
return `${ms}ms`;
|
|
109
|
+
}
|
|
110
|
+
/**
|
|
111
|
+
* The flags a wait command takes, shaped as `ax-command` flags: how long to wait and where to resume from. Spread them
|
|
112
|
+
* into a command's `flags`.
|
|
113
|
+
*/
|
|
114
|
+
const WAIT_FLAGS = Object.freeze({
|
|
115
|
+
timeout: { type: "string", placeholder: "<duration>", description: 'How long to wait, such as "90s" or "5m"; then it exits with a resume command.' },
|
|
116
|
+
cursor: { type: "string", placeholder: "<cursor>", description: "Resume from this cursor (a wait's resume command gives it); without it, only what happens from now counts." },
|
|
117
|
+
});
|
|
118
|
+
exports.WAIT_FLAGS = WAIT_FLAGS;
|
|
119
|
+
const realSleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
|
|
120
|
+
/**
|
|
121
|
+
* Waits on a reference until a matching event, a time limit or the target is gone, reading the source as often as
|
|
122
|
+
* `intervalMs` allows, and returns the typed wait. Inside the tool, so the agent makes one call instead of a loop.
|
|
123
|
+
*/
|
|
124
|
+
async function waitFor(options) {
|
|
125
|
+
const now = options.now ?? Date.now;
|
|
126
|
+
const sleep = options.sleep ?? realSleep;
|
|
127
|
+
const interval = Math.max(1, options.intervalMs ?? 1000);
|
|
128
|
+
const match = options.match ?? (() => true);
|
|
129
|
+
const started = now();
|
|
130
|
+
const deadline = started + Math.max(0, options.timeoutMs);
|
|
131
|
+
const base = { for: options.for, target: options.target, since: new Date(started).toISOString(), until: new Date(deadline).toISOString(), callback: options.callback ?? null };
|
|
132
|
+
let cursor = options.cursor ?? null;
|
|
133
|
+
let fresh = null;
|
|
134
|
+
if (cursor === null) {
|
|
135
|
+
const first = await options.read(null);
|
|
136
|
+
cursor = first.cursor;
|
|
137
|
+
fresh = first.fresh ?? null;
|
|
138
|
+
}
|
|
139
|
+
const stopped = () => wait({ ...base, state: "gone", cursor, fresh, reason: "stopped", next: [] });
|
|
140
|
+
for (;;) {
|
|
141
|
+
if (options.signal?.aborted)
|
|
142
|
+
return stopped();
|
|
143
|
+
const read = await options.read(cursor);
|
|
144
|
+
cursor = read.cursor ?? cursor;
|
|
145
|
+
fresh = read.fresh ?? fresh;
|
|
146
|
+
if (read.gone)
|
|
147
|
+
return wait({ ...base, state: "gone", cursor, fresh, reason: read.gone, next: options.next?.([]) ?? [] });
|
|
148
|
+
const events = read.events.filter(match);
|
|
149
|
+
if (events.length > 0)
|
|
150
|
+
return wait({ ...base, state: "satisfied", cursor, fresh, events, next: options.next?.(events) ?? [] });
|
|
151
|
+
const left = deadline - now();
|
|
152
|
+
if (left <= 0)
|
|
153
|
+
return wait({ ...base, state: "timed-out", cursor, fresh, resume: options.resume(cursor) });
|
|
154
|
+
await sleep(Math.min(interval, left));
|
|
155
|
+
if (options.signal?.aborted)
|
|
156
|
+
return stopped();
|
|
157
|
+
}
|
|
158
|
+
}
|
package/npm-shrinkwrap.json
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ouro.bot/cli",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.1680",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "@ouro.bot/cli",
|
|
9
|
-
"version": "0.1.0-alpha.
|
|
9
|
+
"version": "0.1.0-alpha.1680",
|
|
10
10
|
"bundleDependencies": [
|
|
11
11
|
"@ouro.bot/friends"
|
|
12
12
|
],
|
package/package.json
CHANGED
|
@@ -1,12 +1,15 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@ouro.bot/cli",
|
|
3
|
-
"version": "0.1.0-alpha.
|
|
3
|
+
"version": "0.1.0-alpha.1680",
|
|
4
4
|
"description": "Ouroboros, the agent harness: agents with durable memory that live where you talk (terminal, Teams, iMessage, mail, voice) and use real tools. Start with npx ouro.bot@latest.",
|
|
5
5
|
"homepage": "https://ouroboros.bot/",
|
|
6
6
|
"engines": {
|
|
7
7
|
"node": ">=22"
|
|
8
8
|
},
|
|
9
9
|
"main": "dist/heart/daemon/ouro-entry.js",
|
|
10
|
+
"imports": {
|
|
11
|
+
"#ouro-ax/*": "./src/ouro-ax/*"
|
|
12
|
+
},
|
|
10
13
|
"bin": {
|
|
11
14
|
"cli": "dist/heart/daemon/ouro-bot-entry.js",
|
|
12
15
|
"ouro": "dist/heart/daemon/ouro-entry.js",
|