@agntn/tools 0.3.2 → 0.3.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/_chunks/causes.mjs +5 -2
- package/dist/cli.mjs +26 -6
- package/dist/omp.d.mts +1 -4
- package/dist/omp.mjs +21 -5
- package/package.json +1 -1
package/dist/_chunks/causes.mjs
CHANGED
|
@@ -14,12 +14,15 @@ function causeLines(error) {
|
|
|
14
14
|
for (let depth = 0; depth < CAUSE_DEPTH && level !== void 0 && !seen.has(level); depth += 1) {
|
|
15
15
|
seen.add(level);
|
|
16
16
|
const text = causeText(level);
|
|
17
|
-
if (text !== "" && !above
|
|
18
|
-
if (text !== "") above
|
|
17
|
+
if (text !== "" && !alreadySaid(above, text)) lines.push(`caused by: ${text}`);
|
|
18
|
+
if (text !== "") above += `\n${text}`;
|
|
19
19
|
level = nextCause(level);
|
|
20
20
|
}
|
|
21
21
|
return lines;
|
|
22
22
|
}
|
|
23
|
+
function alreadySaid(above, text) {
|
|
24
|
+
return above.includes(text.length > CAUSE_LENGTH ? text.slice(0, CAUSE_LENGTH) : text);
|
|
25
|
+
}
|
|
23
26
|
function nextCause(error) {
|
|
24
27
|
if (!(error instanceof Error)) return void 0;
|
|
25
28
|
if (error.cause !== void 0) return error.cause;
|
package/dist/cli.mjs
CHANGED
|
@@ -164,7 +164,14 @@ function fieldDescription(field, values = "") {
|
|
|
164
164
|
}
|
|
165
165
|
function valueHint(field) {
|
|
166
166
|
if (field.kind === "enum") return field.schema.enum.join("|");
|
|
167
|
-
return field.kind === "string" ? field.flag : field.kind;
|
|
167
|
+
if (field.kind !== "json") return field.kind === "string" ? field.flag : field.kind;
|
|
168
|
+
const word = wordBranch(field.schema);
|
|
169
|
+
if (word === void 0) return "json";
|
|
170
|
+
return `${valueHint({
|
|
171
|
+
...field,
|
|
172
|
+
schema: word,
|
|
173
|
+
kind: fieldKind(word)
|
|
174
|
+
})}|json`;
|
|
168
175
|
}
|
|
169
176
|
function optionSpelling(field, hint = valueHint(field)) {
|
|
170
177
|
return field.kind === "boolean" ? `--[no-]${field.flag}` : `--${field.flag}=<${hint}>`;
|
|
@@ -387,6 +394,23 @@ function wordValue(schema, word) {
|
|
|
387
394
|
if (index !== -1) return readings[index];
|
|
388
395
|
return spelled.find((value) => typeof value === "number") ?? word;
|
|
389
396
|
}
|
|
397
|
+
function wordBranch(schema) {
|
|
398
|
+
return unionBranches(schema).find((branch) => fieldKind(branch) !== "json" && branch.type !== "null");
|
|
399
|
+
}
|
|
400
|
+
function jsonValue(schema, word) {
|
|
401
|
+
let parsed;
|
|
402
|
+
try {
|
|
403
|
+
parsed = { value: JSON.parse(word) };
|
|
404
|
+
} catch {
|
|
405
|
+
parsed = void 0;
|
|
406
|
+
}
|
|
407
|
+
if (wordBranch(schema) === void 0) return parsed ?? { error: "must be JSON" };
|
|
408
|
+
if (parsed !== void 0 && (quotedJson(parsed.value) || Check(schema, parsed.value))) return parsed;
|
|
409
|
+
return { value: wordValue(schema, word) };
|
|
410
|
+
}
|
|
411
|
+
function quotedJson(value) {
|
|
412
|
+
return typeof value === "string" || typeof value === "object" && value !== null;
|
|
413
|
+
}
|
|
390
414
|
function fieldValue(field, raw) {
|
|
391
415
|
if (field.stdin && raw === "-") {
|
|
392
416
|
const text = readStdin();
|
|
@@ -396,11 +420,7 @@ function fieldValue(field, raw) {
|
|
|
396
420
|
if (field.kind === "number") return numberValue(field.schema.type === "integer", raw);
|
|
397
421
|
if (field.kind === "string") return { value: wordValue(field.schema, raw) };
|
|
398
422
|
if (field.kind !== "json") return { value: raw };
|
|
399
|
-
|
|
400
|
-
return { value: JSON.parse(raw) };
|
|
401
|
-
} catch {
|
|
402
|
-
return { error: "must be JSON" };
|
|
403
|
-
}
|
|
423
|
+
return jsonValue(field.schema, raw);
|
|
404
424
|
}
|
|
405
425
|
function toolInput(fields, values) {
|
|
406
426
|
const input = emptyRecord();
|
package/dist/omp.d.mts
CHANGED
|
@@ -14,10 +14,7 @@ export interface OmpRenderers {
|
|
|
14
14
|
renderCall?: ToolDefinition["renderCall"];
|
|
15
15
|
/** Short metadata after the badge, one entry per fact. Each entry is sanitized. */
|
|
16
16
|
describeResult?(result: OmpResultView): readonly unknown[];
|
|
17
|
-
/**
|
|
18
|
-
* Replaces the result status line with the package's own component, such as
|
|
19
|
-
* a bounded preview of long output. It owns sanitizing what it draws.
|
|
20
|
-
*/
|
|
17
|
+
/** Replaces the status line and the answer under it. It owns sanitizing what it draws. */
|
|
21
18
|
renderResult?: ToolDefinition["renderResult"];
|
|
22
19
|
}
|
|
23
20
|
export interface OmpToolOptions {
|
package/dist/omp.mjs
CHANGED
|
@@ -1,6 +1,7 @@
|
|
|
1
|
-
import { indexTools, invokeTool, sanitizeLine, wireSchema } from "./_chunks/src.mjs";
|
|
1
|
+
import { indexTools, invokeTool, sanitizeLine, sanitizeText, wireSchema } from "./_chunks/src.mjs";
|
|
2
2
|
import { throwWithCauses } from "./_chunks/causes.mjs";
|
|
3
3
|
import { hostContext } from "./_chunks/updates.mjs";
|
|
4
|
+
const COLLAPSED_LINES = 4;
|
|
4
5
|
const APPROVAL = {
|
|
5
6
|
read: "read",
|
|
6
7
|
write: "write",
|
|
@@ -34,11 +35,10 @@ function registerOmpTools(pi, tools, options) {
|
|
|
34
35
|
renderResult(result, renderOptions, theme, args) {
|
|
35
36
|
if (renderers?.renderResult) return renderers.renderResult(result, renderOptions, theme, args);
|
|
36
37
|
if (renderOptions.isPartial) return new Text(progressLine(tool, result, renderOptions, theme), 0, 0);
|
|
37
|
-
const icon = result.isError ? theme.styledSymbol("status.error", "error") : theme.styledSymbol("status.done", "success");
|
|
38
|
-
const { bracketLeft, bracketRight } = theme.format;
|
|
39
|
-
const badge = theme.fg("accent", `${bracketLeft}${APPROVAL[tool.effect]}${bracketRight}`);
|
|
40
38
|
const facts = result.isError ? [] : renderers?.describeResult?.(result) ?? [];
|
|
41
|
-
|
|
39
|
+
const status = `${statusLine(tool, result, theme)}${dimFacts(facts, theme)}`;
|
|
40
|
+
const answer = answerLines(result, renderOptions.expanded, theme);
|
|
41
|
+
return new Text(answer ? `${status}\n${answer}` : status, 0, 0);
|
|
42
42
|
}
|
|
43
43
|
});
|
|
44
44
|
}
|
|
@@ -48,6 +48,22 @@ function progressLine(tool, result, options, theme) {
|
|
|
48
48
|
const title = theme.fg("accent", sanitizeLine(tool.title));
|
|
49
49
|
return `${callIcon(options, theme)} ${title}${message ? ` ${theme.fg("dim", message)}` : ""}`;
|
|
50
50
|
}
|
|
51
|
+
function statusLine(tool, result, theme) {
|
|
52
|
+
const icon = result.isError ? theme.styledSymbol("status.error", "error") : theme.styledSymbol("status.done", "success");
|
|
53
|
+
const { bracketLeft, bracketRight } = theme.format;
|
|
54
|
+
const badge = theme.fg("accent", `${bracketLeft}${APPROVAL[tool.effect]}${bracketRight}`);
|
|
55
|
+
return `${icon} ${theme.fg("accent", sanitizeLine(tool.title))} ${badge}`;
|
|
56
|
+
}
|
|
57
|
+
function answerLines(result, expanded, theme) {
|
|
58
|
+
const text = result.content.flatMap((block) => block.type === "text" && block.text ? [block.text] : []).join("\n");
|
|
59
|
+
const lines = sanitizeText(text).trimEnd().split("\n");
|
|
60
|
+
if (lines.join("") === "") return "";
|
|
61
|
+
const shown = expanded ? lines : lines.slice(0, COLLAPSED_LINES);
|
|
62
|
+
const body = shown.map((line) => theme.fg("toolOutput", line));
|
|
63
|
+
const hidden = lines.length - shown.length;
|
|
64
|
+
if (hidden > 0) body.push(theme.fg("dim", `… ${hidden} more lines`));
|
|
65
|
+
return body.join("\n");
|
|
66
|
+
}
|
|
51
67
|
function dimFacts(facts, theme) {
|
|
52
68
|
const shown = facts.map(sanitizeLine).filter(Boolean);
|
|
53
69
|
return shown.length > 0 ? ` ${theme.fg("dim", shown.join(theme.sep.dot))}` : "";
|
package/package.json
CHANGED