@agntn/tools 0.2.3 → 0.3.0
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/index.d.mts +19 -10
- package/dist/_chunks/mcp-answers.d.mts +1 -0
- package/dist/_chunks/mcp-answers.mjs +1 -1
- package/dist/_chunks/src.mjs +27 -9
- package/dist/ai.d.mts +15 -5
- package/dist/ai.mjs +61 -15
- package/dist/cli.d.mts +2 -2
- package/dist/cli.mjs +47 -20
- package/dist/omp.d.mts +3 -0
- package/dist/omp.mjs +3 -1
- package/dist/pi.d.mts +6 -2
- package/dist/pi.mjs +11 -4
- package/package.json +5 -7
package/dist/_chunks/index.d.mts
CHANGED
|
@@ -31,8 +31,9 @@ export interface ToolResult<Details = unknown> {
|
|
|
31
31
|
* - `read` changes nothing.
|
|
32
32
|
* - `write` changes state that the next call can undo or overwrite.
|
|
33
33
|
* - `destructive` deletes or overwrites what cannot be recovered.
|
|
34
|
+
* - `exec` starts processes or runs code, which can then do anything at all.
|
|
34
35
|
*/
|
|
35
|
-
export type ToolEffect = "read" | "write" | "destructive";
|
|
36
|
+
export type ToolEffect = "read" | "write" | "destructive" | "exec";
|
|
36
37
|
/**
|
|
37
38
|
* How a tool reads on the command line. Without hints the command is the tool
|
|
38
39
|
* name without its package prefix, in kebab case, and every property is a flag.
|
|
@@ -77,8 +78,11 @@ export type ToolAnswer<Content> = {
|
|
|
77
78
|
} | {
|
|
78
79
|
readonly action: "decline" | "cancel";
|
|
79
80
|
};
|
|
80
|
-
/**
|
|
81
|
-
|
|
81
|
+
/**
|
|
82
|
+
* Puts a {@link ToolQuestion} in front of the user and waits for the {@link ToolAnswer}.
|
|
83
|
+
* `Static<Schema>` is only `Content`'s default: in the return type, two installs clashed.
|
|
84
|
+
*/
|
|
85
|
+
export type ToolAsk = <Schema extends TObject, Content extends Static<Schema> = Static<Schema>>(question: ToolQuestion<Schema>) => Promise<ToolAnswer<Content>>;
|
|
82
86
|
/** An MCP icon, the SDK's own shape: a URL or `data:` URI a client can draw on its card. */
|
|
83
87
|
export interface Icon {
|
|
84
88
|
readonly src: string;
|
|
@@ -90,6 +94,7 @@ export interface Icon {
|
|
|
90
94
|
}
|
|
91
95
|
/** Per-call context the adapters hand to `execute`. */
|
|
92
96
|
export interface ToolCallContext {
|
|
97
|
+
/** On cancel a read answers at once and a write still waits. Pass it on so the work stops too. */
|
|
93
98
|
signal?: AbortSignal;
|
|
94
99
|
/** Says the call is still at it. Absent where the host can't show it, so call `progress?.()`. */
|
|
95
100
|
progress?: (message: string, amount?: ToolProgress) => void;
|
|
@@ -103,9 +108,9 @@ export interface ToolCallContext {
|
|
|
103
108
|
/** Pi's or OMP's own `ctx`, the CLI's `CliHost`, nothing elsewhere. Narrow it yourself. */
|
|
104
109
|
host?: unknown;
|
|
105
110
|
}
|
|
106
|
-
export interface ToolDefinition<Input extends TObject = TObject, Details = unknown> {
|
|
107
|
-
/** Wire name, `<package>_<operation>`. */
|
|
108
|
-
readonly name:
|
|
111
|
+
export interface ToolDefinition<Input extends TObject = TObject, Details = unknown, Name extends string = string> {
|
|
112
|
+
/** Wire name, `<package>_<operation>`. Kept literal, so `toAiTools` keys its set by it. */
|
|
113
|
+
readonly name: Name;
|
|
109
114
|
/** Human label: MCP `title` and `annotations.title`, Pi and OMP `label`. */
|
|
110
115
|
readonly title: string;
|
|
111
116
|
/** Complete instruction for the model. MCP clients see nothing else. */
|
|
@@ -158,19 +163,22 @@ export declare class ToolInputError extends Error {
|
|
|
158
163
|
readonly lines: readonly string[];
|
|
159
164
|
/** The schema failures behind the lines, empty when the lines come from elsewhere. */
|
|
160
165
|
readonly issues: readonly InputIssue[];
|
|
166
|
+
/** The tool whose schema turned the input down, unset on a refusal an executor wrote itself. */
|
|
167
|
+
readonly tool: string | undefined;
|
|
161
168
|
/**
|
|
162
169
|
* @param lines - One validation failure per line.
|
|
163
170
|
* @param issues - The schema failures behind them, if any.
|
|
171
|
+
* @param tool - Name of the tool whose schema failed, when the core validated.
|
|
164
172
|
*/
|
|
165
|
-
constructor(lines: readonly string[], issues?: readonly InputIssue[]);
|
|
173
|
+
constructor(lines: readonly string[], issues?: readonly InputIssue[], tool?: string);
|
|
166
174
|
}
|
|
167
175
|
/**
|
|
168
176
|
* Declares a tool and checks the schema rules every surface depends on.
|
|
169
177
|
*
|
|
170
178
|
* @param tool - Tool definition.
|
|
171
|
-
* @returns {ToolDefinition<Input, Details>} The same definition, typed.
|
|
179
|
+
* @returns {ToolDefinition<Input, Details, Name>} The same definition, typed.
|
|
172
180
|
*/
|
|
173
|
-
export declare function defineTool<const Input extends TObject, Details>(tool: ToolDefinition<Input, Details>): ToolDefinition<Input, Details>;
|
|
181
|
+
export declare function defineTool<const Input extends TObject, Details, const Name extends string = string>(tool: ToolDefinition<Input, Details, Name>): ToolDefinition<Input, Details, Name>;
|
|
174
182
|
/**
|
|
175
183
|
* The input schema as the plain JSON Schema object every host receives.
|
|
176
184
|
*
|
|
@@ -219,7 +227,8 @@ export declare function validateInput<Input extends TObject>(tool: ToolDefinitio
|
|
|
219
227
|
*
|
|
220
228
|
* Progress is best effort: a line after the call settles goes nowhere, and a host that throws on
|
|
221
229
|
* one doesn't fail the call. The call answers once every question it started has settled, and a
|
|
222
|
-
* question after that point rejects: nobody waits for the answer.
|
|
230
|
+
* question after that point rejects: nobody waits for the answer. A cancelled read doesn't wait
|
|
231
|
+
* for anything and rejects with the abort reason. A write waits, since the host may have it.
|
|
223
232
|
*
|
|
224
233
|
* @param tool - Tool to run.
|
|
225
234
|
* @param args - Arguments as received from the host.
|
|
@@ -14,6 +14,7 @@ export interface McpServerInfo {
|
|
|
14
14
|
}
|
|
15
15
|
/**
|
|
16
16
|
* Maps a tool's title and declared effect to MCP annotations. Claude Code reads its label here.
|
|
17
|
+
* A process the tool starts can delete anything, so `exec` counts as destructive.
|
|
17
18
|
*
|
|
18
19
|
* @param tool - Tool to describe.
|
|
19
20
|
* @returns {ToolAnnotations} Title and hints for the MCP client.
|
|
@@ -5,7 +5,7 @@ function toolAnnotations(tool) {
|
|
|
5
5
|
return {
|
|
6
6
|
title: tool.title,
|
|
7
7
|
readOnlyHint: tool.effect === "read",
|
|
8
|
-
destructiveHint: tool.effect === "destructive",
|
|
8
|
+
destructiveHint: tool.effect === "destructive" || tool.effect === "exec",
|
|
9
9
|
idempotentHint: tool.idempotent ?? tool.effect === "read",
|
|
10
10
|
openWorldHint: tool.openWorld ?? false
|
|
11
11
|
};
|
package/dist/_chunks/src.mjs
CHANGED
|
@@ -24,10 +24,12 @@ var ToolInputError = class extends Error {
|
|
|
24
24
|
name = "ToolInputError";
|
|
25
25
|
lines;
|
|
26
26
|
issues;
|
|
27
|
-
|
|
27
|
+
tool;
|
|
28
|
+
constructor(lines, issues = [], tool) {
|
|
28
29
|
super(lines.join("\n"));
|
|
29
30
|
this.lines = lines;
|
|
30
31
|
this.issues = issues;
|
|
32
|
+
this.tool = tool;
|
|
31
33
|
}
|
|
32
34
|
};
|
|
33
35
|
const TOOL_NAME = /^[a-z][a-z0-9_]{0,63}$/;
|
|
@@ -175,22 +177,38 @@ function schemaAt(root, pointer) {
|
|
|
175
177
|
}
|
|
176
178
|
async function invokeTool(tool, args, context = {}) {
|
|
177
179
|
const checked = validateInput(tool, args);
|
|
178
|
-
if (!checked.ok) throw new ToolInputError(checked.lines, checked.issues);
|
|
180
|
+
if (!checked.ok) throw new ToolInputError(checked.lines, checked.issues, tool.name);
|
|
181
|
+
const signal = tool.effect === "read" ? context.signal : void 0;
|
|
182
|
+
signal?.throwIfAborted();
|
|
179
183
|
const { progress, ask } = context;
|
|
180
|
-
if (!progress && !ask) return await tool.execute(checked.value, context);
|
|
184
|
+
if (!progress && !ask) return await untilAborted(() => tool.execute(checked.value, context), signal);
|
|
181
185
|
let settled = false;
|
|
182
186
|
const questions = ask ? closedAfter(tool, ask, () => settled) : void 0;
|
|
187
|
+
const call = async () => {
|
|
188
|
+
try {
|
|
189
|
+
return await tool.execute(checked.value, {
|
|
190
|
+
...context,
|
|
191
|
+
...progress ? { progress: quietAfter(progress, () => settled) } : {},
|
|
192
|
+
...questions ? { ask: questions.ask } : {}
|
|
193
|
+
});
|
|
194
|
+
} finally {
|
|
195
|
+
await questions?.drained();
|
|
196
|
+
}
|
|
197
|
+
};
|
|
183
198
|
try {
|
|
184
|
-
return await
|
|
185
|
-
...context,
|
|
186
|
-
...progress ? { progress: quietAfter(progress, () => settled) } : {},
|
|
187
|
-
...questions ? { ask: questions.ask } : {}
|
|
188
|
-
});
|
|
199
|
+
return await untilAborted(call, signal);
|
|
189
200
|
} finally {
|
|
190
|
-
await questions?.drained();
|
|
191
201
|
settled = true;
|
|
192
202
|
}
|
|
193
203
|
}
|
|
204
|
+
async function untilAborted(start, signal) {
|
|
205
|
+
if (!signal) return await start();
|
|
206
|
+
return await new Promise((resolve, reject) => {
|
|
207
|
+
const abort = () => reject(signal.reason);
|
|
208
|
+
signal.addEventListener("abort", abort, { once: true });
|
|
209
|
+
Promise.try(start).then(resolve, reject).finally(() => signal.removeEventListener("abort", abort));
|
|
210
|
+
});
|
|
211
|
+
}
|
|
194
212
|
function quietAfter(progress, settled) {
|
|
195
213
|
return (message, amount) => {
|
|
196
214
|
if (settled()) return;
|
package/dist/ai.d.mts
CHANGED
|
@@ -1,31 +1,41 @@
|
|
|
1
1
|
import { Static, TObject } from "./_chunks/libs/typebox.mjs";
|
|
2
2
|
import { ToolDefinition } from "./_chunks/index.mjs";
|
|
3
3
|
import { Tool } from "ai";
|
|
4
|
+
/** An image block of the result, base64 with its MIME type, as MCP sends it. */
|
|
5
|
+
export interface AiToolImage {
|
|
6
|
+
data: string;
|
|
7
|
+
mimeType: string;
|
|
8
|
+
}
|
|
4
9
|
/**
|
|
5
10
|
* What an AI SDK tool returns: the executor's details with the text MCP
|
|
6
|
-
* clients read. Your code gets the whole object, the model reads
|
|
11
|
+
* clients read. Your code gets the whole object, the model reads `text` and `images`.
|
|
7
12
|
*/
|
|
8
13
|
export type AiToolOutput<Details> = (Details extends object ? Details : {
|
|
9
14
|
details: Details;
|
|
10
15
|
}) & {
|
|
11
16
|
text: string;
|
|
17
|
+
/** The result's image blocks, present only when it has some. */
|
|
18
|
+
images?: AiToolImage[];
|
|
12
19
|
};
|
|
13
20
|
/**
|
|
14
21
|
* Converts one tool, keeping its input and details types.
|
|
15
22
|
*
|
|
16
23
|
* A result with `isError` throws its text, which the AI SDK reports to the
|
|
17
24
|
* model as a tool error, the same as an executor that throws. The model reads
|
|
18
|
-
* `text`
|
|
25
|
+
* `text` and the images, as on every other host, not the details spelled out a second time.
|
|
19
26
|
*
|
|
20
27
|
* @param definition - Tool to convert.
|
|
21
28
|
* @returns {Tool} AI SDK tool.
|
|
22
29
|
*/
|
|
23
30
|
export declare function toAiTool<Input extends TObject, Details>(definition: ToolDefinition<Input, Details>): Tool<Static<Input>, AiToolOutput<Details>>;
|
|
31
|
+
/** What {@link toAiTools} returns: each tool under its own name, typed like {@link toAiTool}. */
|
|
32
|
+
export type AiTools<Tools extends readonly ToolDefinition[]> = { [Definition in Tools[number] as Definition["name"]]: Definition extends ToolDefinition<infer Input, infer Details> ? Tool<Static<Input>, AiToolOutput<Details>> : never; };
|
|
24
33
|
/**
|
|
25
|
-
* Builds an AI SDK tool set keyed by tool name.
|
|
34
|
+
* Builds an AI SDK tool set keyed by tool name, typed per tool.
|
|
35
|
+
* `Object.fromEntries` only knows a record, so the result is cast to the `AiTools` it built.
|
|
26
36
|
*
|
|
27
37
|
* @param tools - Tools to convert.
|
|
28
|
-
* @returns {
|
|
38
|
+
* @returns {AiTools<Tools>} AI SDK tools.
|
|
29
39
|
* @throws {ToolDefinitionError} When two tools share a name.
|
|
30
40
|
*/
|
|
31
|
-
export declare function toAiTools
|
|
41
|
+
export declare function toAiTools<const Tools extends readonly ToolDefinition[]>(tools: Tools): AiTools<Tools>;
|
package/dist/ai.mjs
CHANGED
|
@@ -18,30 +18,76 @@ function toAiTool(definition) {
|
|
|
18
18
|
const result = await invokeTool(definition, input, abortSignal ? { signal: abortSignal } : {});
|
|
19
19
|
const text = resultText(result);
|
|
20
20
|
if (result.isError) throw new Error(text);
|
|
21
|
-
|
|
22
|
-
if (typeof details !== "object" || details === null) return {
|
|
23
|
-
details,
|
|
24
|
-
text
|
|
25
|
-
};
|
|
26
|
-
if (Object.hasOwn(details, "text")) throw new ToolDefinitionError(`${definition.name}: details must not have a text field, the AI SDK output adds one`);
|
|
27
|
-
return {
|
|
28
|
-
...details,
|
|
29
|
-
text
|
|
30
|
-
};
|
|
21
|
+
return aiOutput(definition.name, result, text);
|
|
31
22
|
},
|
|
32
23
|
toModelOutput: ({ output }) => modelOutput(output)
|
|
33
24
|
});
|
|
34
25
|
}
|
|
26
|
+
const ADDED_FIELDS = ["text", "images"];
|
|
27
|
+
function aiOutput(name, result, text) {
|
|
28
|
+
const images = result.content.flatMap((block) => block.type === "image" ? [{
|
|
29
|
+
data: block.data,
|
|
30
|
+
mimeType: block.mimeType
|
|
31
|
+
}] : []);
|
|
32
|
+
const added = images.length > 0 ? {
|
|
33
|
+
text,
|
|
34
|
+
images
|
|
35
|
+
} : { text };
|
|
36
|
+
const { details } = result;
|
|
37
|
+
if (typeof details !== "object" || details === null) return {
|
|
38
|
+
details,
|
|
39
|
+
...added
|
|
40
|
+
};
|
|
41
|
+
const taken = ADDED_FIELDS.find((field) => Object.hasOwn(details, field));
|
|
42
|
+
if (taken !== void 0) throw new ToolDefinitionError(`${name}: details must not have their own ${taken} field, the AI SDK output adds one`);
|
|
43
|
+
return {
|
|
44
|
+
...details,
|
|
45
|
+
...added
|
|
46
|
+
};
|
|
47
|
+
}
|
|
35
48
|
function modelOutput(output) {
|
|
36
|
-
const text
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
49
|
+
const { text, images } = outputFields(output);
|
|
50
|
+
if (images.length > 0) {
|
|
51
|
+
const files = images.map((image) => ({
|
|
52
|
+
type: "file",
|
|
53
|
+
data: {
|
|
54
|
+
type: "data",
|
|
55
|
+
data: image.data
|
|
56
|
+
},
|
|
57
|
+
mediaType: image.mimeType
|
|
58
|
+
}));
|
|
59
|
+
return {
|
|
60
|
+
type: "content",
|
|
61
|
+
value: text === "" ? files : [{
|
|
62
|
+
type: "text",
|
|
63
|
+
text
|
|
64
|
+
}, ...files]
|
|
65
|
+
};
|
|
66
|
+
}
|
|
67
|
+
return text === "" ? {
|
|
41
68
|
type: "json",
|
|
42
69
|
value: output
|
|
70
|
+
} : {
|
|
71
|
+
type: "text",
|
|
72
|
+
value: text
|
|
43
73
|
};
|
|
44
74
|
}
|
|
75
|
+
function outputFields(output) {
|
|
76
|
+
if (typeof output !== "object" || output === null) return {
|
|
77
|
+
text: "",
|
|
78
|
+
images: []
|
|
79
|
+
};
|
|
80
|
+
const { text, images } = output;
|
|
81
|
+
return {
|
|
82
|
+
text: typeof text === "string" ? text : "",
|
|
83
|
+
images: Array.isArray(images) ? images.filter(isImage) : []
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
function isImage(value) {
|
|
87
|
+
if (typeof value !== "object" || value === null) return false;
|
|
88
|
+
const { data, mimeType } = value;
|
|
89
|
+
return typeof data === "string" && typeof mimeType === "string";
|
|
90
|
+
}
|
|
45
91
|
function toAiTools(tools) {
|
|
46
92
|
indexTools(tools);
|
|
47
93
|
return Object.fromEntries(tools.map((definition) => [definition.name, toAiTool(definition)]));
|
package/dist/cli.d.mts
CHANGED
|
@@ -56,8 +56,8 @@ export declare function createCli(options: CliOptions): Cli;
|
|
|
56
56
|
/**
|
|
57
57
|
* Applies `default` and `fallback`: an empty command line runs `default`, and
|
|
58
58
|
* a first word that names no command goes to `fallback` as its first
|
|
59
|
-
* argument
|
|
60
|
-
*
|
|
59
|
+
* argument, a flag included, so `web --provider brave query` searches. Only
|
|
60
|
+
* the root's own `--help` and `--version` stay at the root.
|
|
61
61
|
*
|
|
62
62
|
* @param options - CLI options.
|
|
63
63
|
* @param argv - Words after the executable and script.
|
package/dist/cli.mjs
CHANGED
|
@@ -10,6 +10,7 @@ const RESERVED_FLAGS = /* @__PURE__ */ new Set([
|
|
|
10
10
|
]);
|
|
11
11
|
const TEXT_KINDS = /* @__PURE__ */ new Set(["string", "enum"]);
|
|
12
12
|
const SHORT_FLAG = /^[a-z]$/i;
|
|
13
|
+
const INLINE_ENUM_WIDTH = 40;
|
|
13
14
|
const TYPE_KINDS = /* @__PURE__ */ new Map([
|
|
14
15
|
["boolean", "boolean"],
|
|
15
16
|
["integer", "number"],
|
|
@@ -146,10 +147,11 @@ function assertOneOptionPerSpelling(tool, fields) {
|
|
|
146
147
|
}
|
|
147
148
|
}
|
|
148
149
|
}
|
|
149
|
-
function fieldDescription(field) {
|
|
150
|
+
function fieldDescription(field, values = "") {
|
|
150
151
|
const { schema } = field;
|
|
151
152
|
return [
|
|
152
153
|
typeof schema.description === "string" ? schema.description : "",
|
|
154
|
+
values === "" ? "" : `(one of ${values})`,
|
|
153
155
|
field.stdin ? "(- reads stdin)" : "",
|
|
154
156
|
field.required ? "(required)" : ""
|
|
155
157
|
].filter((part) => part !== "").join(" ");
|
|
@@ -158,8 +160,15 @@ function valueHint(field) {
|
|
|
158
160
|
if (field.kind === "enum") return field.schema.enum.join("|");
|
|
159
161
|
return field.kind === "string" ? field.flag : field.kind;
|
|
160
162
|
}
|
|
161
|
-
function optionSpelling(field) {
|
|
162
|
-
return field.kind === "boolean" ? `--[no-]${field.flag}` : `--${field.flag}=<${
|
|
163
|
+
function optionSpelling(field, hint = valueHint(field)) {
|
|
164
|
+
return field.kind === "boolean" ? `--[no-]${field.flag}` : `--${field.flag}=<${hint}>`;
|
|
165
|
+
}
|
|
166
|
+
function optionRow(field) {
|
|
167
|
+
const short = field.short === void 0 ? "" : `-${field.short}, `;
|
|
168
|
+
const spelling = `${short}${optionSpelling(field)}`;
|
|
169
|
+
const named = `${short}${optionSpelling(field, field.flag)}`;
|
|
170
|
+
if (field.kind !== "enum" || spelling.length <= INLINE_ENUM_WIDTH || named.length >= spelling.length) return [spelling, fieldDescription(field)];
|
|
171
|
+
return [named, fieldDescription(field, field.schema.enum.join(", "))];
|
|
163
172
|
}
|
|
164
173
|
function emptyRecord() {
|
|
165
174
|
return Object.create(null);
|
|
@@ -256,12 +265,12 @@ function longField(spelled, options) {
|
|
|
256
265
|
if (name === "help") return "option";
|
|
257
266
|
const field = Object.hasOwn(options, name) ? options[name] : void 0;
|
|
258
267
|
if (field !== void 0) return field === "json" ? "option" : field;
|
|
259
|
-
return
|
|
268
|
+
return negatedField(name, options) === void 0 ? void 0 : "option";
|
|
260
269
|
}
|
|
261
|
-
function
|
|
270
|
+
function negatedField(name, options) {
|
|
262
271
|
const flag = name.slice(3);
|
|
263
272
|
const field = name.startsWith("no-") && Object.hasOwn(options, flag) ? options[flag] : void 0;
|
|
264
|
-
return field !==
|
|
273
|
+
return field !== "json" && field?.kind === "boolean" ? field : void 0;
|
|
265
274
|
}
|
|
266
275
|
function shortField(word, options) {
|
|
267
276
|
return Object.values(options).find((field) => field !== "json" && field.short !== void 0 && `-${field.short}` === word);
|
|
@@ -285,7 +294,7 @@ function asksHelp(token, word) {
|
|
|
285
294
|
}
|
|
286
295
|
function optionField(options, token, word) {
|
|
287
296
|
if (asksHelp(token, word)) return "help";
|
|
288
|
-
const field = Object.hasOwn(options, token.name) ? options[token.name] :
|
|
297
|
+
const field = Object.hasOwn(options, token.name) ? options[token.name] : negatedField(token.name, options);
|
|
289
298
|
if (field === "json") return token.rawName === "--json" ? field : void 0;
|
|
290
299
|
return field !== void 0 && spells(field, token, word) ? field : void 0;
|
|
291
300
|
}
|
|
@@ -313,7 +322,7 @@ function readOption(options, token, word, seen) {
|
|
|
313
322
|
}
|
|
314
323
|
function optionProblem(field, token, seen) {
|
|
315
324
|
if (seen) return "given more than once";
|
|
316
|
-
if (field.kind === "boolean") return token.inlineValue === true ?
|
|
325
|
+
if (field.kind === "boolean") return token.inlineValue === true ? `takes no value; use --${field.flag} or --no-${field.flag}` : void 0;
|
|
317
326
|
return token.value === void 0 ? "needs a value" : void 0;
|
|
318
327
|
}
|
|
319
328
|
function readStdin() {
|
|
@@ -363,12 +372,14 @@ function unionBranches(schema) {
|
|
|
363
372
|
}));
|
|
364
373
|
}
|
|
365
374
|
function wordValue(schema, word) {
|
|
366
|
-
const
|
|
375
|
+
const spelled = unionBranches(schema).flatMap((branch) => {
|
|
367
376
|
const read = branchValue(branch, word);
|
|
368
377
|
return read === void 0 ? [] : [read.value];
|
|
369
|
-
})
|
|
378
|
+
});
|
|
379
|
+
const readings = [word, ...spelled];
|
|
370
380
|
const index = readings.findIndex((value) => Check(schema, value));
|
|
371
|
-
|
|
381
|
+
if (index !== -1) return readings[index];
|
|
382
|
+
return spelled.find((value) => typeof value === "number") ?? word;
|
|
372
383
|
}
|
|
373
384
|
function fieldValue(field, raw) {
|
|
374
385
|
if (field.stdin && raw === "-") {
|
|
@@ -422,11 +433,21 @@ function cliIssueLines(fields, issue) {
|
|
|
422
433
|
if (field === void 0) return [issue.line];
|
|
423
434
|
return [`Invalid arguments at ${placeIn(field, inner)}: ${issue.problem}`];
|
|
424
435
|
}
|
|
436
|
+
function cliLines(fields, refusal) {
|
|
437
|
+
const lines = refusal.issues.flatMap((issue) => cliIssueLines(fields, issue));
|
|
438
|
+
return lines.length > 0 ? [...new Set(lines)] : refusal.lines;
|
|
439
|
+
}
|
|
425
440
|
function checkInput(tool, fields, input) {
|
|
426
441
|
const checked = validateInput(tool, input);
|
|
427
|
-
if (checked.ok)
|
|
428
|
-
|
|
429
|
-
|
|
442
|
+
if (!checked.ok) throw new ToolInputError(cliLines(fields, checked), checked.issues, tool.name);
|
|
443
|
+
}
|
|
444
|
+
async function invokeInCliWords(tool, fields, input, context) {
|
|
445
|
+
try {
|
|
446
|
+
return await invokeTool(tool, input, context);
|
|
447
|
+
} catch (error) {
|
|
448
|
+
if (!(error instanceof ToolInputError) || error.tool !== void 0) throw error;
|
|
449
|
+
throw new ToolInputError(cliLines(fields, error), error.issues);
|
|
450
|
+
}
|
|
430
451
|
}
|
|
431
452
|
function writeLine(stream, text) {
|
|
432
453
|
process[stream].write(text.endsWith("\n") ? text : `${text}\n`);
|
|
@@ -516,7 +537,7 @@ function toolCommand(tool) {
|
|
|
516
537
|
try {
|
|
517
538
|
const input = toolInput(fields, words.values);
|
|
518
539
|
checkInput(tool, fields, input);
|
|
519
|
-
result = await
|
|
540
|
+
result = await invokeInCliWords(tool, fields, input, {
|
|
520
541
|
...line.context,
|
|
521
542
|
host
|
|
522
543
|
});
|
|
@@ -543,8 +564,9 @@ function mcpCommand(options) {
|
|
|
543
564
|
fields: [],
|
|
544
565
|
json: false,
|
|
545
566
|
async run() {
|
|
546
|
-
const [{ createMcpServer }, {
|
|
547
|
-
|
|
567
|
+
const [{ createMcpServer }, { serveStdio }] = await Promise.all([import("./mcp.mjs"), import("@modelcontextprotocol/server/stdio")]);
|
|
568
|
+
const info = typeof options.mcp === "object" ? options.mcp : options;
|
|
569
|
+
serveStdio(() => createMcpServer(info, options.tools));
|
|
548
570
|
}
|
|
549
571
|
};
|
|
550
572
|
}
|
|
@@ -571,11 +593,16 @@ function createCli(options) {
|
|
|
571
593
|
if (missing !== void 0) throw new ToolDefinitionError(`${options.name}: no command ${missing}`);
|
|
572
594
|
return { run: (argv) => dispatch(options, commands, normalizeArgv(options, argv)) };
|
|
573
595
|
}
|
|
596
|
+
const ROOT_FLAGS = /* @__PURE__ */ new Set([
|
|
597
|
+
"--help",
|
|
598
|
+
"-h",
|
|
599
|
+
"--version",
|
|
600
|
+
"-v"
|
|
601
|
+
]);
|
|
574
602
|
function normalizeArgv(options, argv) {
|
|
575
603
|
const [first] = argv;
|
|
576
604
|
if (first === void 0) return options.default === void 0 ? [] : [options.default];
|
|
577
|
-
|
|
578
|
-
if (options.fallback === void 0 || option || commandWords(options).includes(first)) return [...argv];
|
|
605
|
+
if (options.fallback === void 0 || ROOT_FLAGS.has(first) || commandWords(options).includes(first)) return [...argv];
|
|
579
606
|
return [options.fallback, ...argv];
|
|
580
607
|
}
|
|
581
608
|
async function dispatch(options, commands, argv) {
|
|
@@ -627,7 +654,7 @@ function commandUsage(options, command) {
|
|
|
627
654
|
const takesOptions = flags.length > 0 || command.json;
|
|
628
655
|
const words = positional.map(fieldWord);
|
|
629
656
|
const rows = [
|
|
630
|
-
...flags.map(
|
|
657
|
+
...flags.map(optionRow),
|
|
631
658
|
...command.json ? [["--json", "Print the details as JSON instead of the text"]] : [],
|
|
632
659
|
["-h, --help", "Show this help"]
|
|
633
660
|
];
|
package/dist/omp.d.mts
CHANGED
|
@@ -36,6 +36,9 @@ export interface OmpToolOptions {
|
|
|
36
36
|
* document verbatim, so `pattern` and `maxProperties` survive. A standalone
|
|
37
37
|
* omptype schema would drop both.
|
|
38
38
|
*
|
|
39
|
+
* Every tool says `strict: false`. OMP's OpenAI strict mode makes each optional field required
|
|
40
|
+
* and nullable, so the model fills them all with junk, and the core refuses even the `null`.
|
|
41
|
+
*
|
|
39
42
|
* @param pi - OMP extension API.
|
|
40
43
|
* @param tools - Tools to register.
|
|
41
44
|
* @param options - Host `Text`, optional load mode and renderers.
|
package/dist/omp.mjs
CHANGED
|
@@ -3,7 +3,8 @@ import { hostContext } from "./_chunks/updates.mjs";
|
|
|
3
3
|
const APPROVAL = {
|
|
4
4
|
read: "read",
|
|
5
5
|
write: "write",
|
|
6
|
-
destructive: "write"
|
|
6
|
+
destructive: "write",
|
|
7
|
+
exec: "exec"
|
|
7
8
|
};
|
|
8
9
|
function registerOmpTools(pi, tools, options) {
|
|
9
10
|
indexTools(tools);
|
|
@@ -18,6 +19,7 @@ function registerOmpTools(pi, tools, options) {
|
|
|
18
19
|
parameters: Type.Unsafe(wireSchema(tool)),
|
|
19
20
|
approval: APPROVAL[tool.effect],
|
|
20
21
|
loadMode: options.loadMode,
|
|
22
|
+
strict: false,
|
|
21
23
|
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
22
24
|
return await invokeTool(tool, params, hostContext(signal, onUpdate, ctx));
|
|
23
25
|
},
|
package/dist/pi.d.mts
CHANGED
|
@@ -19,7 +19,11 @@ export interface PiToolOptions {
|
|
|
19
19
|
readonly renderers?: Readonly<Record<string, PiRenderers>>;
|
|
20
20
|
/** Questions to ask before a call runs, by tool name. A name off the tool list throws. */
|
|
21
21
|
readonly confirm?: Readonly<Record<string, PiConfirm>>;
|
|
22
|
+
/** Pi's argument repair by tool name, run before validation. A name off the list throws. */
|
|
23
|
+
readonly prepareArguments?: Readonly<Record<string, PiPrepareArguments>>;
|
|
22
24
|
}
|
|
25
|
+
/** Repairs raw model input before validation. Hand back what it doesn't recognise untouched. */
|
|
26
|
+
export type PiPrepareArguments = (args: unknown) => unknown;
|
|
23
27
|
/** Builds the question for one call from its validated input; `undefined` skips it. */
|
|
24
28
|
export type PiConfirm = (input: Readonly<Record<string, unknown>>) => PiQuestion | undefined | Promise<PiQuestion | undefined>;
|
|
25
29
|
/** What `ctx.ui.confirm` shows. The adapter sanitizes both and keeps the message's lines. */
|
|
@@ -37,8 +41,8 @@ export interface PiRenderers extends Readonly<Pick<ToolDefinition, "renderCall"
|
|
|
37
41
|
*
|
|
38
42
|
* @param pi - Pi extension API.
|
|
39
43
|
* @param tools - Tools to register.
|
|
40
|
-
* @param options - Failure policy, host `Text`, renderers and
|
|
44
|
+
* @param options - Failure policy, host `Text`, renderers, questions and argument repairs.
|
|
41
45
|
* @throws {ToolDefinitionError} When two tools share a name, a tool has `describeCall` but no
|
|
42
|
-
* `Text` came with it, or `confirm` names a tool that isn't in the list.
|
|
46
|
+
* `Text` came with it, or `confirm` or `prepareArguments` names a tool that isn't in the list.
|
|
43
47
|
*/
|
|
44
48
|
export declare function registerPiTools(pi: ExtensionAPI, tools: readonly ToolDefinition$1[], options?: PiToolOptions): void;
|
package/dist/pi.mjs
CHANGED
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
import { ToolDefinitionError, ToolInputError, indexTools, invokeTool, resultText, sanitizeLine, sanitizeText, validateInput } from "./_chunks/src.mjs";
|
|
2
2
|
import { hostContext } from "./_chunks/updates.mjs";
|
|
3
3
|
function registerPiTools(pi, tools, options = {}) {
|
|
4
|
-
const
|
|
4
|
+
const names = [...indexTools(tools).keys()];
|
|
5
5
|
const failures = options.failures ?? "throw";
|
|
6
|
-
|
|
7
|
-
|
|
6
|
+
refuseStray(names, "confirm", options.confirm);
|
|
7
|
+
refuseStray(names, "prepareArguments", options.prepareArguments);
|
|
8
8
|
for (const tool of tools) {
|
|
9
9
|
const renderers = byName(options.renderers, tool.name);
|
|
10
10
|
const ask = byName(options.confirm, tool.name);
|
|
11
|
+
const prepare = byName(options.prepareArguments, tool.name);
|
|
11
12
|
pi.registerTool({
|
|
12
13
|
...hostRenderers(tool, renderers, options.Text),
|
|
14
|
+
...prepare === void 0 ? {} : { prepareArguments: prepare },
|
|
13
15
|
name: tool.name,
|
|
14
16
|
label: tool.title,
|
|
15
17
|
description: tool.description,
|
|
@@ -25,12 +27,17 @@ function registerPiTools(pi, tools, options = {}) {
|
|
|
25
27
|
});
|
|
26
28
|
}
|
|
27
29
|
}
|
|
30
|
+
function refuseStray(names, option, entries) {
|
|
31
|
+
const stray = Object.keys(entries ?? {}).find((name) => !names.includes(name));
|
|
32
|
+
if (stray === void 0) return;
|
|
33
|
+
throw new ToolDefinitionError(`${option} names ${JSON.stringify(stray)}, which isn't in the tool list`);
|
|
34
|
+
}
|
|
28
35
|
function byName(entries, name) {
|
|
29
36
|
return entries && Object.hasOwn(entries, name) ? entries[name] : void 0;
|
|
30
37
|
}
|
|
31
38
|
async function approve(tool, ask, params, signal, ctx) {
|
|
32
39
|
const checked = validateInput(tool, params);
|
|
33
|
-
if (!checked.ok) throw new ToolInputError(checked.lines, checked.issues);
|
|
40
|
+
if (!checked.ok) throw new ToolInputError(checked.lines, checked.issues, tool.name);
|
|
34
41
|
const question = await ask(checked.value);
|
|
35
42
|
if (question === void 0) return;
|
|
36
43
|
if (!ctx.hasUI) throw new Error(`${tool.name} needs interactive approval in Pi TUI or RPC mode`);
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agntn/tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.3.0",
|
|
4
4
|
"description": "One tool definition for MCP, Pi, OMP and the AI SDK",
|
|
5
5
|
"homepage": "https://github.com/agntn/tools#readme",
|
|
6
6
|
"bugs": "https://github.com/agntn/tools/issues",
|
|
@@ -54,14 +54,13 @@
|
|
|
54
54
|
},
|
|
55
55
|
"devDependencies": {
|
|
56
56
|
"@agntn/ox": "0.1.0",
|
|
57
|
-
"@earendil-works/pi-coding-agent": "1.0.
|
|
57
|
+
"@earendil-works/pi-coding-agent": "1.0.4",
|
|
58
58
|
"@modelcontextprotocol/client": "2.2.0",
|
|
59
59
|
"@modelcontextprotocol/sdk": "1.32.1",
|
|
60
60
|
"@modelcontextprotocol/server": "2.2.0",
|
|
61
|
-
"@oh-my-pi/pi-coding-agent": "18.0.
|
|
61
|
+
"@oh-my-pi/pi-coding-agent": "18.0.11",
|
|
62
62
|
"@types/node": "26.2.0",
|
|
63
63
|
"ai": "7.0.127",
|
|
64
|
-
"changelogen": "0.6.2",
|
|
65
64
|
"h3": "2.0.1",
|
|
66
65
|
"h3-mcp": "0.2.0",
|
|
67
66
|
"obuild": "0.4.43",
|
|
@@ -107,9 +106,8 @@
|
|
|
107
106
|
"lint": "vp lint && vp fmt --check",
|
|
108
107
|
"lint:fix": "vp lint --fix && vp fmt",
|
|
109
108
|
"typecheck": "tsc --noEmit --skipLibCheck",
|
|
110
|
-
"typecheck:dist": "tsc -p tsconfig.dist.json",
|
|
109
|
+
"typecheck:dist": "tsc -p tsconfig.dist.json && tsc -p test/copies",
|
|
111
110
|
"test": "vp test run",
|
|
112
|
-
"test:watch": "vp test watch"
|
|
113
|
-
"release": "pnpm test && pnpm build && pnpm typecheck:dist && changelogen --release --push"
|
|
111
|
+
"test:watch": "vp test watch"
|
|
114
112
|
}
|
|
115
113
|
}
|