@agntn/tools 0.2.4 → 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 +14 -8
- package/dist/_chunks/mcp-answers.d.mts +1 -0
- package/dist/_chunks/mcp-answers.mjs +1 -1
- package/dist/_chunks/src.mjs +23 -7
- package/dist/ai.d.mts +15 -5
- package/dist/ai.mjs +61 -15
- package/dist/cli.mjs +26 -14
- package/dist/omp.d.mts +3 -0
- package/dist/omp.mjs +3 -1
- package/dist/pi.d.mts +6 -2
- package/dist/pi.mjs +10 -3
- package/package.json +4 -6
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,7 +78,10 @@ export type ToolAnswer<Content> = {
|
|
|
77
78
|
} | {
|
|
78
79
|
readonly action: "decline" | "cancel";
|
|
79
80
|
};
|
|
80
|
-
/**
|
|
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
|
+
*/
|
|
81
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 {
|
|
@@ -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. */
|
|
@@ -171,9 +176,9 @@ export declare class ToolInputError extends Error {
|
|
|
171
176
|
* Declares a tool and checks the schema rules every surface depends on.
|
|
172
177
|
*
|
|
173
178
|
* @param tool - Tool definition.
|
|
174
|
-
* @returns {ToolDefinition<Input, Details>} The same definition, typed.
|
|
179
|
+
* @returns {ToolDefinition<Input, Details, Name>} The same definition, typed.
|
|
175
180
|
*/
|
|
176
|
-
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>;
|
|
177
182
|
/**
|
|
178
183
|
* The input schema as the plain JSON Schema object every host receives.
|
|
179
184
|
*
|
|
@@ -222,7 +227,8 @@ export declare function validateInput<Input extends TObject>(tool: ToolDefinitio
|
|
|
222
227
|
*
|
|
223
228
|
* Progress is best effort: a line after the call settles goes nowhere, and a host that throws on
|
|
224
229
|
* one doesn't fail the call. The call answers once every question it started has settled, and a
|
|
225
|
-
* 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.
|
|
226
232
|
*
|
|
227
233
|
* @param tool - Tool to run.
|
|
228
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
|
@@ -178,21 +178,37 @@ function schemaAt(root, pointer) {
|
|
|
178
178
|
async function invokeTool(tool, args, context = {}) {
|
|
179
179
|
const checked = validateInput(tool, args);
|
|
180
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();
|
|
181
183
|
const { progress, ask } = context;
|
|
182
|
-
if (!progress && !ask) return await tool.execute(checked.value, context);
|
|
184
|
+
if (!progress && !ask) return await untilAborted(() => tool.execute(checked.value, context), signal);
|
|
183
185
|
let settled = false;
|
|
184
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
|
+
};
|
|
185
198
|
try {
|
|
186
|
-
return await
|
|
187
|
-
...context,
|
|
188
|
-
...progress ? { progress: quietAfter(progress, () => settled) } : {},
|
|
189
|
-
...questions ? { ask: questions.ask } : {}
|
|
190
|
-
});
|
|
199
|
+
return await untilAborted(call, signal);
|
|
191
200
|
} finally {
|
|
192
|
-
await questions?.drained();
|
|
193
201
|
settled = true;
|
|
194
202
|
}
|
|
195
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
|
+
}
|
|
196
212
|
function quietAfter(progress, settled) {
|
|
197
213
|
return (message, amount) => {
|
|
198
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.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 === "-") {
|
|
@@ -553,8 +564,9 @@ function mcpCommand(options) {
|
|
|
553
564
|
fields: [],
|
|
554
565
|
json: false,
|
|
555
566
|
async run() {
|
|
556
|
-
const [{ createMcpServer }, {
|
|
557
|
-
|
|
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));
|
|
558
570
|
}
|
|
559
571
|
};
|
|
560
572
|
}
|
|
@@ -642,7 +654,7 @@ function commandUsage(options, command) {
|
|
|
642
654
|
const takesOptions = flags.length > 0 || command.json;
|
|
643
655
|
const words = positional.map(fieldWord);
|
|
644
656
|
const rows = [
|
|
645
|
-
...flags.map(
|
|
657
|
+
...flags.map(optionRow),
|
|
646
658
|
...command.json ? [["--json", "Print the details as JSON instead of the text"]] : [],
|
|
647
659
|
["-h, --help", "Show this help"]
|
|
648
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,6 +27,11 @@ 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
|
}
|
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",
|
|
@@ -109,7 +108,6 @@
|
|
|
109
108
|
"typecheck": "tsc --noEmit --skipLibCheck",
|
|
110
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
|
}
|