@agntn/tools 0.3.1 โ 0.3.2
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/README.md +1 -0
- package/dist/_chunks/causes.mjs +6 -1
- package/dist/cli.d.mts +2 -2
- package/dist/cli.mjs +6 -4
- package/dist/pi.d.mts +20 -2
- package/dist/pi.mjs +70 -2
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -24,6 +24,7 @@ MCP wants an `inputSchema`. Pi wants `parameters` and a prompt snippet. OMP swap
|
|
|
24
24
|
- ๐งช **Schema rules at definition time.** An open object or a union of literals fails in `defineTool`. Not three weeks later in a model's hands.
|
|
25
25
|
- โณ **Progress that gets somewhere.** `progress?.("still at it")` turns into `onUpdate` in Pi and OMP, `notifications/progress` in MCP and one live line in your terminal. A slow call stops looking like a dead one.
|
|
26
26
|
- ๐ **Ask the person, not the model.** `ask?.({ message, schema })` becomes MCP elicitation, or one dialog per field in Pi and OMP. Three puzzles match? Let them pick instead of letting the model guess.
|
|
27
|
+
- ๐ **Tools when you need them.** `AGNTN_PI_EXPOSURE=deferred` keeps Pi tools out of the prompt. The ones nobody called today stop riding along. Turn on `tool_search` and the model still finds them.
|
|
27
28
|
- โจ๏ธ **A CLI on the side.** `runCli` turns the same tools into commands, no CLI library needed. Flags come from the schema. Hints add positionals, short flags and stdin.
|
|
28
29
|
- ๐ **Runs anywhere.** No `node:*` import in the core or the host adapters. The docs site runs them in a browser tab. The CLI is the one Node part, as a CLI should be.
|
|
29
30
|
|
package/dist/_chunks/causes.mjs
CHANGED
|
@@ -1,4 +1,9 @@
|
|
|
1
|
+
import { __exportAll } from "./rolldown-runtime.mjs";
|
|
1
2
|
import { ToolInputError, sanitizeLine } from "./src.mjs";
|
|
3
|
+
var causes_exports = /* @__PURE__ */ __exportAll({
|
|
4
|
+
causeLines: () => causeLines,
|
|
5
|
+
throwWithCauses: () => throwWithCauses
|
|
6
|
+
});
|
|
2
7
|
const CAUSE_DEPTH = 5;
|
|
3
8
|
const CAUSE_LENGTH = 300;
|
|
4
9
|
function causeLines(error) {
|
|
@@ -68,4 +73,4 @@ function throwWithCauses(error) {
|
|
|
68
73
|
shown.name = error.name;
|
|
69
74
|
throw shown;
|
|
70
75
|
}
|
|
71
|
-
export { causeLines, throwWithCauses };
|
|
76
|
+
export { causeLines, causes_exports, throwWithCauses };
|
package/dist/cli.d.mts
CHANGED
|
@@ -28,8 +28,8 @@ export interface CliOptions {
|
|
|
28
28
|
/** Adds `mcp` over stdio, named by the CLI on `true` or by the server info it's handed. */
|
|
29
29
|
readonly mcp?: boolean | McpServerInfo;
|
|
30
30
|
/**
|
|
31
|
-
* Errors the package throws on purpose. They print as one line with
|
|
32
|
-
* code 1; any other error keeps its stack trace.
|
|
31
|
+
* Errors the package throws on purpose. They print as one line with a `caused by:`
|
|
32
|
+
* line per cause and exit code 1; any other error keeps its stack trace.
|
|
33
33
|
*/
|
|
34
34
|
readonly expected?: (error: unknown) => boolean;
|
|
35
35
|
}
|
package/dist/cli.mjs
CHANGED
|
@@ -462,10 +462,12 @@ function fail(lines) {
|
|
|
462
462
|
writeLine("stderr", lines.map(sanitizeLine).join("\n"));
|
|
463
463
|
process.exitCode = 1;
|
|
464
464
|
}
|
|
465
|
-
function handleError(options, error) {
|
|
465
|
+
async function handleError(options, error) {
|
|
466
466
|
if (error instanceof ToolInputError) fail(error.lines);
|
|
467
|
-
else if (options.expected?.(error) === true)
|
|
468
|
-
|
|
467
|
+
else if (options.expected?.(error) === true) {
|
|
468
|
+
const { causeLines } = await import("./_chunks/causes.mjs").then((n) => n.causes_exports);
|
|
469
|
+
fail([error instanceof Error ? error.message : String(error), ...causeLines(error)]);
|
|
470
|
+
} else throw error;
|
|
469
471
|
}
|
|
470
472
|
function detailsJson(details) {
|
|
471
473
|
return (JSON.stringify(details, (_key, value) => typeof value === "bigint" ? value.toString() : value, 2) ?? "null").replaceAll(JSON_FORGING, (character) => `\\u${(character.codePointAt(0) ?? 0).toString(16).padStart(4, "0")}`);
|
|
@@ -695,7 +697,7 @@ async function runCommand(options, command, rawArgs) {
|
|
|
695
697
|
if (words.errors.length > 0) throw new ToolInputError(words.errors);
|
|
696
698
|
await command.run(words);
|
|
697
699
|
} catch (error) {
|
|
698
|
-
handleError(options, error);
|
|
700
|
+
await handleError(options, error);
|
|
699
701
|
}
|
|
700
702
|
}
|
|
701
703
|
function table(rows) {
|
package/dist/pi.d.mts
CHANGED
|
@@ -21,6 +21,23 @@ export interface PiToolOptions {
|
|
|
21
21
|
readonly confirm?: Readonly<Record<string, PiConfirm>>;
|
|
22
22
|
/** Pi's argument repair by tool name, run before validation. A name off the list throws. */
|
|
23
23
|
readonly prepareArguments?: Readonly<Record<string, PiPrepareArguments>>;
|
|
24
|
+
/** How the model reaches the tools, for all or by name. Left out, Pi's `direct`. */
|
|
25
|
+
readonly exposure?: PiToolExposure | PiExposures;
|
|
26
|
+
/** Group the tools share, as Pi groups the tools of one MCP server. */
|
|
27
|
+
readonly namespace?: PiToolNamespace;
|
|
28
|
+
}
|
|
29
|
+
/** Pi's `ToolExposure`, spelled out so older Pi types still compile. */
|
|
30
|
+
export type PiToolExposure = "direct" | "model-only" | "codemode" | "deferred" | "hidden";
|
|
31
|
+
/** Exposure by tool name, with a fallback for the rest. A name off the list throws. */
|
|
32
|
+
export interface PiExposures {
|
|
33
|
+
readonly default?: PiToolExposure;
|
|
34
|
+
readonly tools?: Readonly<Record<string, PiToolExposure>>;
|
|
35
|
+
}
|
|
36
|
+
/** Pi's `ToolNamespace`: what `describeNamespace()` and `tool_search` read. */
|
|
37
|
+
export interface PiToolNamespace {
|
|
38
|
+
readonly name: string;
|
|
39
|
+
readonly description?: string;
|
|
40
|
+
readonly instructions?: string;
|
|
24
41
|
}
|
|
25
42
|
/** Repairs raw model input before validation. Hand back what it doesn't recognise untouched. */
|
|
26
43
|
export type PiPrepareArguments = (args: unknown) => unknown;
|
|
@@ -41,8 +58,9 @@ export interface PiRenderers extends Readonly<Pick<ToolDefinition, "renderCall"
|
|
|
41
58
|
*
|
|
42
59
|
* @param pi - Pi extension API.
|
|
43
60
|
* @param tools - Tools to register.
|
|
44
|
-
* @param options - Failure policy, host `Text`, renderers, questions and
|
|
61
|
+
* @param options - Failure policy, host `Text`, renderers, questions, repairs and exposure.
|
|
45
62
|
* @throws {ToolDefinitionError} When two tools share a name, a tool has `describeCall` but no
|
|
46
|
-
* `Text` came with it,
|
|
63
|
+
* `Text` came with it, `confirm`, `prepareArguments` or `exposure` names a tool that isn't in
|
|
64
|
+
* the list, or an exposure isn't one Pi knows.
|
|
47
65
|
*/
|
|
48
66
|
export declare function registerPiTools(pi: ExtensionAPI, tools: readonly ToolDefinition$1[], options?: PiToolOptions): void;
|
package/dist/pi.mjs
CHANGED
|
@@ -1,24 +1,37 @@
|
|
|
1
|
+
import { Check } from "./_chunks/libs/typebox.mjs";
|
|
1
2
|
import { ToolDefinitionError, ToolInputError, indexTools, invokeTool, resultText, sanitizeLine, sanitizeText, validateInput } from "./_chunks/src.mjs";
|
|
2
3
|
import { throwWithCauses } from "./_chunks/causes.mjs";
|
|
3
4
|
import { hostContext } from "./_chunks/updates.mjs";
|
|
5
|
+
const EXPOSURES = [
|
|
6
|
+
"direct",
|
|
7
|
+
"model-only",
|
|
8
|
+
"codemode",
|
|
9
|
+
"deferred",
|
|
10
|
+
"hidden"
|
|
11
|
+
];
|
|
12
|
+
const EXPOSURE_VARIABLE = "AGNTN_PI_EXPOSURE";
|
|
4
13
|
function registerPiTools(pi, tools, options = {}) {
|
|
5
14
|
const names = [...indexTools(tools).keys()];
|
|
6
15
|
const failures = options.failures ?? "throw";
|
|
7
16
|
refuseStray(names, "confirm", options.confirm);
|
|
8
17
|
refuseStray(names, "prepareArguments", options.prepareArguments);
|
|
18
|
+
checkOption(names, options.exposure);
|
|
19
|
+
fromEnvironment(EXPOSURE_VARIABLE);
|
|
20
|
+
const exposures = new Map(names.map((name) => [name, exposureOf(name, options.exposure)]));
|
|
9
21
|
for (const tool of tools) {
|
|
10
22
|
const renderers = byName(options.renderers, tool.name);
|
|
11
23
|
const ask = byName(options.confirm, tool.name);
|
|
12
|
-
const prepare = byName(options.prepareArguments, tool.name);
|
|
24
|
+
const prepare = parseJsonText(tool, byName(options.prepareArguments, tool.name));
|
|
13
25
|
pi.registerTool({
|
|
14
26
|
...hostRenderers(tool, renderers, options.Text),
|
|
15
|
-
|
|
27
|
+
prepareArguments: prepare,
|
|
16
28
|
name: tool.name,
|
|
17
29
|
label: tool.title,
|
|
18
30
|
description: tool.description,
|
|
19
31
|
...tool.snippet === void 0 ? {} : { promptSnippet: tool.snippet },
|
|
20
32
|
...tool.guidelines === void 0 ? {} : { promptGuidelines: [...tool.guidelines] },
|
|
21
33
|
parameters: tool.input,
|
|
34
|
+
...reach(exposures.get(tool.name), options.namespace),
|
|
22
35
|
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
23
36
|
if (ask) await approve(tool, ask, params, signal, ctx);
|
|
24
37
|
const context = hostContext(signal, onUpdate, ctx);
|
|
@@ -29,6 +42,61 @@ function registerPiTools(pi, tools, options = {}) {
|
|
|
29
42
|
});
|
|
30
43
|
}
|
|
31
44
|
}
|
|
45
|
+
function parseJsonText(tool, own) {
|
|
46
|
+
if (own === void 0) return (args) => repairJsonText(tool, args);
|
|
47
|
+
return (args) => own(repairJsonText(tool, args));
|
|
48
|
+
}
|
|
49
|
+
function repairJsonText(tool, args) {
|
|
50
|
+
if (typeof args !== "object" || args === null || Array.isArray(args)) return args;
|
|
51
|
+
const raw = args;
|
|
52
|
+
let repaired;
|
|
53
|
+
const properties = Object.entries(tool.input.properties);
|
|
54
|
+
for (const [key, schema] of properties) {
|
|
55
|
+
const parsed = Object.hasOwn(raw, key) ? jsonValue(raw[key], schema) : void 0;
|
|
56
|
+
if (parsed === void 0) continue;
|
|
57
|
+
repaired ??= { ...raw };
|
|
58
|
+
repaired[key] = parsed;
|
|
59
|
+
}
|
|
60
|
+
return repaired ?? args;
|
|
61
|
+
}
|
|
62
|
+
function jsonValue(value, schema) {
|
|
63
|
+
if (typeof value !== "string" || !/^\s*[[{]/.test(value)) return void 0;
|
|
64
|
+
try {
|
|
65
|
+
const parsed = JSON.parse(value);
|
|
66
|
+
return typeof parsed === "object" && parsed !== null && Check(schema, parsed) ? parsed : void 0;
|
|
67
|
+
} catch {
|
|
68
|
+
return;
|
|
69
|
+
}
|
|
70
|
+
}
|
|
71
|
+
function reach(exposure, namespace) {
|
|
72
|
+
return {
|
|
73
|
+
...exposure === void 0 ? {} : { exposure },
|
|
74
|
+
...namespace === void 0 ? {} : { namespace: { ...namespace } }
|
|
75
|
+
};
|
|
76
|
+
}
|
|
77
|
+
function exposureOf(name, option) {
|
|
78
|
+
const own = `${EXPOSURE_VARIABLE}_${name.split("_", 1)[0]?.toUpperCase() ?? ""}`;
|
|
79
|
+
const global = fromEnvironment(EXPOSURE_VARIABLE);
|
|
80
|
+
return fromEnvironment(own) ?? global ?? (typeof option === "object" ? byName(option.tools, name) ?? option.default : option);
|
|
81
|
+
}
|
|
82
|
+
function checkOption(names, option) {
|
|
83
|
+
if (typeof option !== "object") {
|
|
84
|
+
if (option !== void 0) checkExposure(option, "exposure");
|
|
85
|
+
return;
|
|
86
|
+
}
|
|
87
|
+
refuseStray(names, "exposure.tools", option.tools);
|
|
88
|
+
if (option.default !== void 0) checkExposure(option.default, "exposure.default");
|
|
89
|
+
for (const [name, value] of Object.entries(option.tools ?? {})) checkExposure(value, `exposure of ${name}`);
|
|
90
|
+
}
|
|
91
|
+
function fromEnvironment(variable) {
|
|
92
|
+
const value = typeof process === "undefined" ? void 0 : process.env[variable];
|
|
93
|
+
return value ? checkExposure(value, variable) : void 0;
|
|
94
|
+
}
|
|
95
|
+
function checkExposure(value, where) {
|
|
96
|
+
if (typeof value === "string" && EXPOSURES.includes(value)) return value;
|
|
97
|
+
const shown = typeof value === "string" ? `is ${JSON.stringify(value)}` : "isn't a string";
|
|
98
|
+
throw new ToolDefinitionError(`${where} ${shown}; takes ${EXPOSURES.join(", ")}`);
|
|
99
|
+
}
|
|
32
100
|
function refuseStray(names, option, entries) {
|
|
33
101
|
const stray = Object.keys(entries ?? {}).find((name) => !names.includes(name));
|
|
34
102
|
if (stray === void 0) return;
|
package/package.json
CHANGED