@agntn/tools 0.1.1 → 0.2.1
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 +4 -2
- package/dist/_chunks/index.d.mts +205 -0
- package/dist/_chunks/index.d.mts.map +1 -0
- package/dist/_chunks/libs/typebox.mjs +3 -234
- package/dist/_chunks/libs/typebox.mjs.map +1 -1
- package/dist/_chunks/mcp-answers.mjs +67 -0
- package/dist/_chunks/mcp-answers.mjs.map +1 -0
- package/dist/_chunks/src.mjs +21 -2
- package/dist/_chunks/src.mjs.map +1 -1
- package/dist/_chunks/updates.mjs +16 -0
- package/dist/_chunks/updates.mjs.map +1 -0
- package/dist/ai.d.mts +2 -1
- package/dist/ai.d.mts.map +1 -1
- package/dist/ai.mjs +2 -1
- package/dist/ai.mjs.map +1 -1
- package/dist/cli.d.mts +4 -3
- package/dist/cli.d.mts.map +1 -1
- package/dist/cli.mjs +184 -37
- package/dist/cli.mjs.map +1 -1
- package/dist/h3.d.mts +25 -0
- package/dist/h3.d.mts.map +1 -0
- package/dist/h3.mjs +23 -0
- package/dist/h3.mjs.map +1 -0
- package/dist/index.d.mts +3 -165
- package/dist/index.mjs +2 -2
- package/dist/mcp.d.mts +30 -4
- package/dist/mcp.d.mts.map +1 -1
- package/dist/mcp.mjs +25 -42
- package/dist/mcp.mjs.map +1 -1
- package/dist/omp.d.mts +7 -2
- package/dist/omp.d.mts.map +1 -1
- package/dist/omp.mjs +19 -6
- package/dist/omp.mjs.map +1 -1
- package/dist/pi.d.mts +24 -5
- package/dist/pi.d.mts.map +1 -1
- package/dist/pi.mjs +39 -5
- package/dist/pi.mjs.map +1 -1
- package/package.json +13 -3
- package/dist/index.d.mts.map +0 -1
package/dist/omp.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"omp.mjs","names":[],"sources":["../src/omp.ts"],"sourcesContent":["/**\n * OMP adapter: registers {@link ToolDefinition}s on an OMP `ExtensionAPI`.\n *\n * Only types come from `@oh-my-pi/pi-coding-agent`. Compiled OMP injects the\n * package root into the extension file itself, not into its dependencies, so\n * the extension passes the host `Text` component in.\n */\n\nimport type {\n AgentToolResult,\n ExtensionAPI,\n Text as HostText,\n Theme,\n ToolDefinition as OmpToolDefinition,\n ToolRenderResultOptions,\n} from \"@oh-my-pi/pi-coding-agent\";\n\nimport {\n invokeTool,\n sanitizeLine,\n wireSchema,\n type ToolDefinition,\n type ToolEffect,\n} from \"./index.ts\";\n\ntype TextComponent = typeof HostText;\ntype Component = ReturnType<NonNullable<OmpToolDefinition[\"renderCall\"]>>;\n\n/** The part of a host result a renderer reads. */\nexport interface OmpResultView {\n readonly details?: unknown;\n readonly isError?: boolean;\n}\n\n/** Per-tool renderer overrides, drawn with the host theme. */\nexport interface OmpRenderers {\n /** One-line summary of the arguments after the title. Sanitized by the adapter. */\n describeCall?(args: Readonly<Record<string, unknown>>): unknown;\n /** Short metadata after the badge, one entry per fact. Each entry is sanitized. */\n describeResult?(result: OmpResultView): readonly unknown[];\n /**\n * Replaces the result status line with the package's own component, such as\n * a bounded preview of long output. It owns sanitizing what it draws.\n */\n renderResult?: OmpToolDefinition[\"renderResult\"];\n}\n\nexport interface OmpToolOptions {\n /** The host `Text` component, imported by the extension from the package root. */\n readonly Text: TextComponent;\n /** Status-line renderers by tool name. */\n readonly renderers?: Readonly<Record<string, OmpRenderers>>;\n}\n\nconst APPROVAL: Record<ToolEffect, \"read\" | \"write\"> = {\n read: \"read\",\n write: \"write\",\n // OMP has no destructive tier; `exec` means running commands.\n destructive: \"write\",\n};\n\n/**\n * Registers every tool on the OMP extension API.\n *\n * The schema reaches OMP through `pi.typebox.Type.Unsafe`: the host-injected\n * build validates the raw JSON Schema with its full validator and emits the\n * document verbatim, so `pattern` and `maxProperties` survive. A standalone\n * omptype schema would drop both.\n *\n * @param pi - OMP extension API.\n * @param tools - Tools to register.\n * @param options - Host `Text
|
|
1
|
+
{"version":3,"file":"omp.mjs","names":[],"sources":["../src/omp.ts"],"sourcesContent":["/**\n * OMP adapter: registers {@link ToolDefinition}s on an OMP `ExtensionAPI`.\n *\n * Only types come from `@oh-my-pi/pi-coding-agent`. Compiled OMP injects the\n * package root into the extension file itself, not into its dependencies, so\n * the extension passes the host `Text` component in.\n */\n\nimport type {\n AgentToolResult,\n ExtensionAPI,\n Text as HostText,\n Theme,\n ToolDefinition as OmpToolDefinition,\n ToolRenderResultOptions,\n} from \"@oh-my-pi/pi-coding-agent\";\n\nimport {\n indexTools,\n invokeTool,\n sanitizeLine,\n wireSchema,\n type ToolDefinition,\n type ToolEffect,\n} from \"./index.ts\";\nimport { hostContext } from \"./updates.ts\";\n\ntype TextComponent = typeof HostText;\ntype Component = ReturnType<NonNullable<OmpToolDefinition[\"renderCall\"]>>;\n\n/** The part of a host result a renderer reads. */\nexport interface OmpResultView {\n readonly details?: unknown;\n readonly isError?: boolean;\n}\n\n/** Per-tool renderer overrides, drawn with the host theme. */\nexport interface OmpRenderers {\n /** One-line summary of the arguments after the title. Sanitized by the adapter. */\n describeCall?(args: Readonly<Record<string, unknown>>): unknown;\n /** Replaces the whole call line, `describeCall` included. It owns sanitizing what it draws. */\n renderCall?: OmpToolDefinition[\"renderCall\"];\n /** Short metadata after the badge, one entry per fact. Each entry is sanitized. */\n describeResult?(result: OmpResultView): readonly unknown[];\n /**\n * Replaces the result status line with the package's own component, such as\n * a bounded preview of long output. It owns sanitizing what it draws.\n */\n renderResult?: OmpToolDefinition[\"renderResult\"];\n}\n\nexport interface OmpToolOptions {\n /** The host `Text` component, imported by the extension from the package root. */\n readonly Text: TextComponent;\n /** `\"essential\"` keeps every tool in OMP's top-level list. Left out, OMP hides them behind discovery. */\n readonly loadMode?: OmpToolDefinition[\"loadMode\"];\n /** Status-line renderers by tool name. */\n readonly renderers?: Readonly<Record<string, OmpRenderers>>;\n}\n\nconst APPROVAL: Record<ToolEffect, \"read\" | \"write\"> = {\n read: \"read\",\n write: \"write\",\n // OMP has no destructive tier; `exec` means running commands.\n destructive: \"write\",\n};\n\n/**\n * Registers every tool on the OMP extension API.\n *\n * The schema reaches OMP through `pi.typebox.Type.Unsafe`: the host-injected\n * build validates the raw JSON Schema with its full validator and emits the\n * document verbatim, so `pattern` and `maxProperties` survive. A standalone\n * omptype schema would drop both.\n *\n * @param pi - OMP extension API.\n * @param tools - Tools to register.\n * @param options - Host `Text`, optional load mode and renderers.\n * @throws {ToolDefinitionError} When two tools share a name.\n */\nexport function registerOmpTools(\n pi: ExtensionAPI,\n tools: readonly ToolDefinition[],\n options: OmpToolOptions,\n): void {\n indexTools(tools);\n const { Type } = pi.typebox;\n const { Text } = options;\n\n for (const tool of tools) {\n const renderers = Object.hasOwn(options.renderers ?? {}, tool.name)\n ? options.renderers?.[tool.name]\n : undefined;\n\n pi.registerTool({\n name: tool.name,\n label: tool.title,\n description: tool.description,\n parameters: Type.Unsafe<Record<string, unknown>>(wireSchema(tool)),\n approval: APPROVAL[tool.effect],\n loadMode: options.loadMode,\n async execute(_toolCallId, params, signal, onUpdate, ctx): Promise<AgentToolResult<unknown>> {\n // Returned as is: OMP reads `isError` from the object (`explicitError`).\n return await invokeTool(tool, params, hostContext(signal, onUpdate, ctx));\n },\n renderCall(args, renderOptions, theme): Component {\n if (renderers?.renderCall) return renderers.renderCall(args, renderOptions, theme);\n const summary = renderers?.describeCall ? sanitizeLine(renderers.describeCall(args)) : \"\";\n const title = theme.fg(\"accent\", sanitizeLine(tool.title));\n return new Text(\n `${callIcon(renderOptions, theme)} ${title}${summary ? `: ${theme.fg(\"muted\", summary)}` : \"\"}`,\n 0,\n 0,\n );\n },\n renderResult(result, renderOptions, theme, args): Component {\n if (renderers?.renderResult)\n return renderers.renderResult(result, renderOptions, theme, args);\n if (renderOptions.isPartial)\n return new Text(progressLine(tool, result, renderOptions, theme), 0, 0);\n const icon = result.isError\n ? theme.styledSymbol(\"status.error\", \"error\")\n : theme.styledSymbol(\"status.done\", \"success\");\n const { bracketLeft, bracketRight } = theme.format;\n const badge = theme.fg(\"accent\", `${bracketLeft}${APPROVAL[tool.effect]}${bracketRight}`);\n const facts = result.isError ? [] : (renderers?.describeResult?.(result) ?? []);\n return new Text(\n `${icon} ${theme.fg(\"accent\", sanitizeLine(tool.title))} ${badge}${dimFacts(facts, theme)}`,\n 0,\n 0,\n );\n },\n });\n }\n}\n\n/**\n * Status line while the call runs: the spinner, the title and its last progress line.\n *\n * @param tool - Tool being called.\n * @param result - Partial result from `onUpdate`.\n * @param options - Render options from the host.\n * @param theme - Host theme.\n * @returns {string} The styled line.\n */\nfunction progressLine(\n tool: ToolDefinition,\n result: Readonly<{ content: readonly Readonly<{ type: string; text?: string }>[] }>,\n options: ToolRenderResultOptions,\n theme: Theme,\n): string {\n const message = sanitizeLine(\n result.content.findLast((block) => block.type === \"text\")?.text ?? \"\",\n );\n const title = theme.fg(\"accent\", sanitizeLine(tool.title));\n return `${callIcon(options, theme)} ${title}${message ? ` ${theme.fg(\"dim\", message)}` : \"\"}`;\n}\n\n/**\n * The `describeResult` facts after the badge, sanitized and dimmed; empty ones drop out.\n *\n * @param facts - Facts as the renderer gave them.\n * @param theme - Host theme.\n * @returns {string} The facts with a leading space, or nothing.\n */\nfunction dimFacts(facts: readonly unknown[], theme: Theme): string {\n const shown = facts.map(sanitizeLine).filter(Boolean);\n return shown.length > 0 ? ` ${theme.fg(\"dim\", shown.join(theme.sep.dot))}` : \"\";\n}\n\n/**\n * Status icon for a call: pending, spinning or done.\n *\n * Drawn with the host theme on purpose: compiled OMP injects only the package\n * root, so importing `@oh-my-pi/pi-coding-agent/tui` would stop the extension.\n *\n * @param options - Render options from the host.\n * @param theme - Host theme.\n * @returns {string} The styled icon.\n */\nfunction callIcon(options: ToolRenderResultOptions, theme: Theme): string {\n if (!options.isPartial) return theme.styledSymbol(\"status.done\", \"success\");\n if (options.spinnerFrame === undefined) return theme.styledSymbol(\"status.pending\", \"muted\");\n return (\n theme.spinnerFrames[options.spinnerFrame % theme.spinnerFrames.length] ??\n theme.styledSymbol(\"status.running\", \"accent\")\n );\n}\n"],"mappings":";;AA4DA,MAAM,WAAiD;CACrD,MAAM;CACN,OAAO;CAEP,aAAa;AACf;;;;;;;;;;;;;GAeA,UAAgB,QAAA;GAKd,MAAA,QAAgB,aAAA,QAAA,QAAA,UAAA,KAAA;IAChB,OAAQ,MAAA,WAAY,MAAA,QAAA,YAAA,QAAA,UAAA,GAAA,CAAA;GACpB;GAEA,WAAW,MAAQ,eAAO,OAAA;IACxB,IAAM,WAAA,YAAmB,OAAO,UAAQ,WAAiB,MAAK,eAClD,KAAA;IAGZ,MAAG,UAAa,WAAA,eAAA,aAAA,UAAA,aAAA,IAAA,CAAA,IAAA;IACd,MAAM,QAAK,MAAA,GAAA,UAAA,aAAA,KAAA,KAAA,CAAA;IACX,OAAO,IAAK,KAAA,GAAA,SAAA,eAAA,KAAA,EAAA,GAAA,QAAA,UAAA,KAAA,MAAA,GAAA,SAAA,OAAA,MAAA,MAAA,GAAA,CAAA;GACZ;GACA,aAAY,QAAK,eAA2C,OAAK,MAAA;IACjE,IAAA,WAAU,cAAc,OAAA,UAAA,aAAA,QAAA,eAAA,OAAA,IAAA;IACxB,IAAA,cAAkB,WAAA,OAAA,IAAA,KAAA,aAAA,MAAA,QAAA,eAAA,KAAA,GAAA,GAAA,CAAA;IAClB,MAAM,OAAQ,OAAA,UAAa,MAAQ,aAAQ,gBAAkD,OAAA,IAAA,MAAA,aAAA,eAAA,SAAA;IAE3F,MAAA,EAAO,aAAM,iBAAyB,MAAA;IACxC,MAAA,QAAA,MAAA,GAAA,UAAA,GAAA,cAAA,SAAA,KAAA,UAAA,cAAA;IACA,MAAA,QAAW,OAAM,UAAe,CAAA,IAAA,WAAkB,iBAAA,MAAA,KAAA,CAAA;IAChD,OAAI,IAAA,KAAW,GAAA,KAAA,GAAY,MAAA,GAAO,UAAU,aAAW,KAAM,KAAA,CAAA,EAAA,GAAA,QAAoB,SAAA,OAAA,KAAA,KAAA,GAAA,CAAA;GACjF;EACA,CAAA;CACA;AAKF;AAEE,SAAI,aAAW,MAAA,QACb,SAAO,OAAU;CACnB,MAAA,UAAI,aAAc,OAChB,QAAW,UAAK,UAAa,MAAM,SAAQ,MAAA,CAAA,EAAA,QAAoB,EAAG;CACpE,MAAA,QAAM,MAAO,GAAO,UAChB,aAAM,KAAA,KAAa,CAAA;CAEvB,OAAA,GAAM,SAAE,SAAa,KAAA,EAAA,GAAA,QAAiB,UAAM,IAAA,MAAA,GAAA,OAAA,OAAA,MAAA;AAC5C;AAEA,SAAA,SAAW,OACN,OAAQ;CAIf,MAAA,QAAA,MAAA,IAAA,YAAA,CAAA,CAAA,OAAA,OAAA;CACF,OAAC,MAAA,SAAA,IAAA,IAAA,MAAA,GAAA,OAAA,MAAA,KAAA,MAAA,IAAA,GAAA,CAAA,MAAA;AACH"}
|
package/dist/pi.d.mts
CHANGED
|
@@ -1,5 +1,8 @@
|
|
|
1
|
-
import { ToolDefinition as ToolDefinition$1 } from "./index.mjs";
|
|
1
|
+
import { ToolDefinition as ToolDefinition$1 } from "./_chunks/index.mjs";
|
|
2
2
|
import { ExtensionAPI, ToolDefinition } from "@earendil-works/pi-coding-agent";
|
|
3
|
+
type Component = ReturnType<NonNullable<ToolDefinition["renderCall"]>>;
|
|
4
|
+
/** The `Text` component of `@earendil-works/pi-tui`, which Pi resolves for every extension. */
|
|
5
|
+
export type PiTextComponent = new (text: string, paddingX: number, paddingY: number) => Component;
|
|
3
6
|
export interface PiToolOptions {
|
|
4
7
|
/**
|
|
5
8
|
* How a returned `isError` result reaches Pi.
|
|
@@ -10,17 +13,33 @@ export interface PiToolOptions {
|
|
|
10
13
|
* only with a peer range of `>=0.99.0`, since Pi does not check peer ranges.
|
|
11
14
|
*/
|
|
12
15
|
readonly failures?: "throw" | "return";
|
|
13
|
-
/** Host
|
|
16
|
+
/** Host `Text` from `@earendil-works/pi-tui`, which `describeCall` draws with. */
|
|
17
|
+
readonly Text?: PiTextComponent;
|
|
18
|
+
/** Renderers by tool name. */
|
|
14
19
|
readonly renderers?: Readonly<Record<string, PiRenderers>>;
|
|
20
|
+
/** Questions to ask before a call runs, by tool name. A name off the tool list throws. */
|
|
21
|
+
readonly confirm?: Readonly<Record<string, PiConfirm>>;
|
|
22
|
+
}
|
|
23
|
+
/** Builds the question for one call from its validated input; `undefined` skips it. */
|
|
24
|
+
export type PiConfirm = (input: Readonly<Record<string, unknown>>) => PiQuestion | undefined | Promise<PiQuestion | undefined>;
|
|
25
|
+
/** What `ctx.ui.confirm` shows. The adapter sanitizes both and keeps the message's lines. */
|
|
26
|
+
export interface PiQuestion {
|
|
27
|
+
readonly title: string;
|
|
28
|
+
readonly message: string;
|
|
29
|
+
}
|
|
30
|
+
/** Renderers for one tool: Pi's own, passed unchanged, or a one-line call summary. */
|
|
31
|
+
export interface PiRenderers extends Readonly<Pick<ToolDefinition, "renderCall" | "renderResult">> {
|
|
32
|
+
/** One-line summary of the arguments after the title, sanitized here. A `renderCall` wins. */
|
|
33
|
+
readonly describeCall?: (args: Readonly<Record<string, unknown>>) => unknown;
|
|
15
34
|
}
|
|
16
|
-
/** The host's own call and result renderers for one tool. */
|
|
17
|
-
export type PiRenderers = Pick<ToolDefinition, "renderCall" | "renderResult">;
|
|
18
35
|
/**
|
|
19
36
|
* Registers every tool on the Pi extension API.
|
|
20
37
|
*
|
|
21
38
|
* @param pi - Pi extension API.
|
|
22
39
|
* @param tools - Tools to register.
|
|
23
|
-
* @param options - Failure policy and
|
|
40
|
+
* @param options - Failure policy, host `Text`, renderers and questions.
|
|
41
|
+
* @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.
|
|
24
43
|
*/
|
|
25
44
|
export declare function registerPiTools(pi: ExtensionAPI, tools: readonly ToolDefinition$1[], options?: PiToolOptions): void;
|
|
26
45
|
//# sourceMappingURL=pi.d.mts.map
|
package/dist/pi.d.mts.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pi.d.mts","names":["PiToolDefinition","ToolDefinition"],"sources":["../src/pi.ts"],"mappings":";;
|
|
1
|
+
{"version":3,"file":"pi.d.mts","names":["PiToolDefinition","ToolDefinition"],"sources":["../src/pi.ts"],"mappings":";;KA4BK,YAAY,WAAW,YAAYA;;YAG5B,uBAAuB,cAAc,kBAAkB,qBAAqB;iBAEvE;;;;;;;;;WASN;;WAEA,OAAO;;WAEP,YAAY,SAAS,eAAe;;WAEpC,UAAU,SAAS,eAAe;;;YAIjC,aACV,OAAO,SAAS,6BACb,yBAAyB,QAAQ;;iBAGrB;WACN;WACA;;;iBAIM,oBAAoB,SACnC,KAAKA;;WAGI,gBAAgB,MAAM,SAAS;;;;;;;;;;;wBAY1B,gBACd,IAAI,cACJ,gBAAgBC,oBAChB,UAAS"}
|
package/dist/pi.mjs
CHANGED
|
@@ -1,24 +1,58 @@
|
|
|
1
|
-
import { invokeTool, resultText } from "./_chunks/src.mjs";
|
|
1
|
+
import { ToolDefinitionError, ToolInputError, indexTools, invokeTool, resultText, sanitizeLine, sanitizeText, validateInput } from "./_chunks/src.mjs";
|
|
2
|
+
import { hostContext } from "./_chunks/updates.mjs";
|
|
2
3
|
function registerPiTools(pi, tools, options = {}) {
|
|
4
|
+
const listed = indexTools(tools);
|
|
3
5
|
const failures = options.failures ?? "throw";
|
|
6
|
+
const stray = Object.keys(options.confirm ?? {}).find((name) => !listed.has(name));
|
|
7
|
+
if (stray !== void 0) throw new ToolDefinitionError(`confirm names ${JSON.stringify(stray)}, which isn't in the tool list`);
|
|
4
8
|
for (const tool of tools) {
|
|
5
|
-
const renderers =
|
|
9
|
+
const renderers = byName(options.renderers, tool.name);
|
|
10
|
+
const ask = byName(options.confirm, tool.name);
|
|
6
11
|
pi.registerTool({
|
|
7
|
-
...renderers,
|
|
12
|
+
...hostRenderers(tool, renderers, options.Text),
|
|
8
13
|
name: tool.name,
|
|
9
14
|
label: tool.title,
|
|
10
15
|
description: tool.description,
|
|
11
16
|
...tool.snippet === void 0 ? {} : { promptSnippet: tool.snippet },
|
|
12
17
|
...tool.guidelines === void 0 ? {} : { promptGuidelines: [...tool.guidelines] },
|
|
13
18
|
parameters: tool.input,
|
|
14
|
-
async execute(_toolCallId, params, signal) {
|
|
15
|
-
|
|
19
|
+
async execute(_toolCallId, params, signal, onUpdate, ctx) {
|
|
20
|
+
if (ask) await approve(tool, ask, params, signal, ctx);
|
|
21
|
+
const result = await invokeTool(tool, params, hostContext(signal, onUpdate, ctx));
|
|
16
22
|
if (result.isError && failures === "throw") throw new Error(resultText(result));
|
|
17
23
|
return result;
|
|
18
24
|
}
|
|
19
25
|
});
|
|
20
26
|
}
|
|
21
27
|
}
|
|
28
|
+
function byName(entries, name) {
|
|
29
|
+
return entries && Object.hasOwn(entries, name) ? entries[name] : void 0;
|
|
30
|
+
}
|
|
31
|
+
async function approve(tool, ask, params, signal, ctx) {
|
|
32
|
+
const checked = validateInput(tool, params);
|
|
33
|
+
if (!checked.ok) throw new ToolInputError(checked.lines);
|
|
34
|
+
const question = await ask(checked.value);
|
|
35
|
+
if (question === void 0) return;
|
|
36
|
+
if (!ctx.hasUI) throw new Error(`${tool.name} needs interactive approval in Pi TUI or RPC mode`);
|
|
37
|
+
const approved = await ctx.ui.confirm(sanitizeLine(question.title), sanitizeText(question.message), signal ? { signal } : {});
|
|
38
|
+
signal?.throwIfAborted();
|
|
39
|
+
if (!approved) throw new Error(`${tool.name} was cancelled by the user. Do not retry unless the user asks again.`);
|
|
40
|
+
}
|
|
41
|
+
function hostRenderers(tool, renderers, Text) {
|
|
42
|
+
if (!renderers) return {};
|
|
43
|
+
const { describeCall, ...own } = renderers;
|
|
44
|
+
if (own.renderCall || !describeCall) return own;
|
|
45
|
+
if (!Text) throw new ToolDefinitionError(`${tool.name}: describeCall needs the host Text option`);
|
|
46
|
+
const title = sanitizeLine(tool.title);
|
|
47
|
+
return {
|
|
48
|
+
...own,
|
|
49
|
+
renderCall(args, theme) {
|
|
50
|
+
const summary = sanitizeLine(describeCall(args));
|
|
51
|
+
const header = theme.fg("toolTitle", theme.bold(title));
|
|
52
|
+
return new Text(summary ? `${header} ${theme.fg("muted", summary)}` : header, 0, 0);
|
|
53
|
+
}
|
|
54
|
+
};
|
|
55
|
+
}
|
|
22
56
|
export { registerPiTools };
|
|
23
57
|
|
|
24
58
|
//# sourceMappingURL=pi.mjs.map
|
package/dist/pi.mjs.map
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"pi.mjs","names":[],"sources":["../src/pi.ts"],"sourcesContent":["/**\n * Pi adapter: registers {@link ToolDefinition}s on a Pi `ExtensionAPI`.\n *\n * Only types come from `@earendil-works/pi-coding-agent`; nothing is imported\n * from the host at runtime.\n */\n\nimport type {\n AgentToolResult,\n ExtensionAPI,\n ToolDefinition as PiToolDefinition,\n} from \"@earendil-works/pi-coding-agent\";\n\nimport { invokeTool
|
|
1
|
+
{"version":3,"file":"pi.mjs","names":[],"sources":["../src/pi.ts"],"sourcesContent":["/**\n * Pi adapter: registers {@link ToolDefinition}s on a Pi `ExtensionAPI`.\n *\n * Only types come from `@earendil-works/pi-coding-agent`; nothing is imported\n * from the host at runtime. A call line from `describeCall` needs the host\n * `Text`, which the extension passes in.\n */\n\nimport type {\n AgentToolResult,\n ExtensionAPI,\n ExtensionContext,\n ToolDefinition as PiToolDefinition,\n} from \"@earendil-works/pi-coding-agent\";\n\nimport { sanitizeText } from \"./escapes.ts\";\nimport {\n indexTools,\n invokeTool,\n resultText,\n sanitizeLine,\n ToolDefinitionError,\n ToolInputError,\n validateInput,\n type ToolDefinition,\n} from \"./index.ts\";\nimport { hostContext } from \"./updates.ts\";\n\ntype Component = ReturnType<NonNullable<PiToolDefinition[\"renderCall\"]>>;\n\n/** The `Text` component of `@earendil-works/pi-tui`, which Pi resolves for every extension. */\nexport type PiTextComponent = new (text: string, paddingX: number, paddingY: number) => Component;\n\nexport interface PiToolOptions {\n /**\n * How a returned `isError` result reaches Pi.\n *\n * `throw` (default) raises the result text: `pi-agent-core` up to 0.98 marks\n * every returned object as a success and records an error only after a throw.\n * `return` hands the result over unchanged and keeps its `details`; use it\n * only with a peer range of `>=0.99.0`, since Pi does not check peer ranges.\n */\n readonly failures?: \"throw\" | \"return\";\n /** Host `Text` from `@earendil-works/pi-tui`, which `describeCall` draws with. */\n readonly Text?: PiTextComponent;\n /** Renderers by tool name. */\n readonly renderers?: Readonly<Record<string, PiRenderers>>;\n /** Questions to ask before a call runs, by tool name. A name off the tool list throws. */\n readonly confirm?: Readonly<Record<string, PiConfirm>>;\n}\n\n/** Builds the question for one call from its validated input; `undefined` skips it. */\nexport type PiConfirm = (\n input: Readonly<Record<string, unknown>>,\n) => PiQuestion | undefined | Promise<PiQuestion | undefined>;\n\n/** What `ctx.ui.confirm` shows. The adapter sanitizes both and keeps the message's lines. */\nexport interface PiQuestion {\n readonly title: string;\n readonly message: string;\n}\n\n/** Renderers for one tool: Pi's own, passed unchanged, or a one-line call summary. */\nexport interface PiRenderers extends Readonly<\n Pick<PiToolDefinition, \"renderCall\" | \"renderResult\">\n> {\n /** One-line summary of the arguments after the title, sanitized here. A `renderCall` wins. */\n readonly describeCall?: (args: Readonly<Record<string, unknown>>) => unknown;\n}\n\n/**\n * Registers every tool on the Pi extension API.\n *\n * @param pi - Pi extension API.\n * @param tools - Tools to register.\n * @param options - Failure policy, host `Text`, renderers and questions.\n * @throws {ToolDefinitionError} When two tools share a name, a tool has `describeCall` but no\n * `Text` came with it, or `confirm` names a tool that isn't in the list.\n */\nexport function registerPiTools(\n pi: ExtensionAPI,\n tools: readonly ToolDefinition[],\n options: PiToolOptions = {},\n): void {\n const listed = indexTools(tools);\n const failures = options.failures ?? \"throw\";\n const stray = Object.keys(options.confirm ?? {}).find((name) => !listed.has(name));\n if (stray !== undefined) {\n throw new ToolDefinitionError(\n `confirm names ${JSON.stringify(stray)}, which isn't in the tool list`,\n );\n }\n\n for (const tool of tools) {\n const renderers = byName(options.renderers, tool.name);\n const ask = byName(options.confirm, tool.name);\n pi.registerTool({\n ...hostRenderers(tool, renderers, options.Text),\n name: tool.name,\n label: tool.title,\n description: tool.description,\n ...(tool.snippet === undefined ? {} : { promptSnippet: tool.snippet }),\n ...(tool.guidelines === undefined ? {} : { promptGuidelines: [...tool.guidelines] }),\n parameters: tool.input,\n async execute(_toolCallId, params, signal, onUpdate, ctx): Promise<AgentToolResult<unknown>> {\n if (ask) await approve(tool, ask, params, signal, ctx);\n const result = await invokeTool(tool, params, hostContext(signal, onUpdate, ctx));\n if (result.isError && failures === \"throw\") throw new Error(resultText(result));\n return result;\n },\n });\n }\n}\n\n/**\n * Own entry of a by-name option, so a tool named `constructor` doesn't reach the prototype.\n *\n * @param entries - Option keyed by tool name.\n * @param name - Tool name.\n * @returns {T | undefined} The entry, if the option has one for this tool.\n */\nfunction byName<T>(entries: Readonly<Record<string, T>> | undefined, name: string): T | undefined {\n return entries && Object.hasOwn(entries, name) ? entries[name] : undefined;\n}\n\n/**\n * Asks the user about one call and throws unless they say yes.\n *\n * @param tool - Tool being called.\n * @param ask - Its question builder.\n * @param params - Arguments as Pi passes them.\n * @param signal - The call's abort signal, which also dismisses the dialog.\n * @param ctx - Pi context of the call.\n * @returns {Promise<void>} Once the call may run.\n * @throws {ToolInputError} When the arguments fail the schema, before anyone is asked.\n * @throws {Error} Without a UI, on a \"no\", or with the reason of an abort.\n */\nasync function approve(\n tool: ToolDefinition,\n ask: PiConfirm,\n params: unknown,\n signal: AbortSignal | undefined,\n ctx: ExtensionContext,\n): Promise<void> {\n const checked = validateInput(tool, params);\n if (!checked.ok) throw new ToolInputError(checked.lines);\n const question = await ask(checked.value);\n if (question === undefined) return;\n if (!ctx.hasUI) throw new Error(`${tool.name} needs interactive approval in Pi TUI or RPC mode`);\n const approved = await ctx.ui.confirm(\n sanitizeLine(question.title),\n sanitizeText(question.message),\n signal ? { signal } : {},\n );\n signal?.throwIfAborted();\n if (!approved) {\n throw new Error(\n `${tool.name} was cancelled by the user. Do not retry unless the user asks again.`,\n );\n }\n}\n\n/**\n * The renderers Pi gets for one tool, with `describeCall` turned into a `renderCall`.\n *\n * @param tool - Tool being registered.\n * @param renderers - Its renderers, if any.\n * @param Text - Host `Text` component.\n * @returns {Pick<PiToolDefinition, \"renderCall\" | \"renderResult\">} What goes to `pi.registerTool`.\n * @throws {ToolDefinitionError} When `describeCall` would draw but `Text` is missing.\n */\nfunction hostRenderers(\n tool: ToolDefinition,\n renderers: PiRenderers | undefined,\n Text: PiTextComponent | undefined,\n): Pick<PiToolDefinition, \"renderCall\" | \"renderResult\"> {\n if (!renderers) return {};\n const { describeCall, ...own } = renderers;\n if (own.renderCall || !describeCall) return own;\n if (!Text) throw new ToolDefinitionError(`${tool.name}: describeCall needs the host Text option`);\n const title = sanitizeLine(tool.title);\n return {\n ...own,\n renderCall(args, theme): Component {\n const summary = sanitizeLine(describeCall(args as Readonly<Record<string, unknown>>));\n const header = theme.fg(\"toolTitle\", theme.bold(title));\n return new Text(summary ? `${header} ${theme.fg(\"muted\", summary)}` : header, 0, 0);\n },\n };\n}\n"],"mappings":";;;;;;;;;;EA+EA,GAAA,aAAgB;GAKd,GAAA,cAAe,MAAA,WAAgB,QAAA,IAAA;GAC/B,MAAM,KAAA;GACN,OAAM,KAAQ;GACd,aAAc,KAAA;GAMd,GAAK,KAAA,YAAc,KAAO,IAAA,CAAA,IAAA,EAAA,eAAA,KAAA,QAAA;GACxB,GAAA,KAAM,eAAY,KAAO,IAAQ,CAAA,IAAA,EAAA,kBAAoB,CAAA,GAAA,KAAA,UAAA,EAAA;GACrD,YAAY,KAAA;GACZ,MAAG,QAAA,aAAa,QAAA,QAAA,UAAA,KAAA;IACd,IAAG,KAAA,MAAA,QAAoB,MAAA,KAAW,QAAQ,QAAI,GAAA;IAC9C,MAAM,SAAK,MAAA,WAAA,MAAA,QAAA,YAAA,QAAA,UAAA,GAAA,CAAA;IACX,IAAA,OAAY,WAAA,aAAA,SAAA,MAAA,IAAA,MAAA,WAAA,MAAA,CAAA;IACZ,OAAA;GACA;EACA,CAAA;CACA;AACA;AAEE,SAAA,OAAM,SAAe,MAAA;CACrB,OAAI,WAAO,OAAW,OAAA,SAAa,IAAA,IAAS,QAAU,QAAM,KAAA;AAC5D;AAEJ,eAAC,QAAA,MAAA,KAAA,QAAA,QAAA,KAAA;CACH,MAAA,UAAA,cAAA,MAAA,MAAA;CACF,IAAA,CAAA,QAAA,IAAA,MAAA,IAAA,eAAA,QAAA,KAAA;;;;;;;;AAUE,SAAO,cAAW,MAAO,WAAO,MAAa;CAC/C,IAAA,CAAA,WAAA,OAAA,CAAA;;;;;;;;;;;;;AAcA;AAQE,SAAK"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@agntn/tools",
|
|
3
|
-
"version": "0.
|
|
3
|
+
"version": "0.2.1",
|
|
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",
|
|
@@ -24,6 +24,10 @@
|
|
|
24
24
|
"types": "./dist/mcp.d.mts",
|
|
25
25
|
"import": "./dist/mcp.mjs"
|
|
26
26
|
},
|
|
27
|
+
"./h3": {
|
|
28
|
+
"types": "./dist/h3.d.mts",
|
|
29
|
+
"import": "./dist/h3.mjs"
|
|
30
|
+
},
|
|
27
31
|
"./pi": {
|
|
28
32
|
"types": "./dist/pi.d.mts",
|
|
29
33
|
"import": "./dist/pi.mjs"
|
|
@@ -53,6 +57,8 @@
|
|
|
53
57
|
"@types/node": "26.2.0",
|
|
54
58
|
"ai": "7.0.127",
|
|
55
59
|
"changelogen": "0.6.2",
|
|
60
|
+
"h3": "2.0.1",
|
|
61
|
+
"h3-mcp": "0.2.0",
|
|
56
62
|
"obuild": "0.4.43",
|
|
57
63
|
"typebox": "1.3.34",
|
|
58
64
|
"typescript": "7.0.2",
|
|
@@ -63,7 +69,8 @@
|
|
|
63
69
|
"@earendil-works/pi-coding-agent": ">=0.84.2 <2",
|
|
64
70
|
"@modelcontextprotocol/server": ">=2.2.0 <3",
|
|
65
71
|
"@oh-my-pi/pi-coding-agent": ">=18.0.0 <19",
|
|
66
|
-
"ai": ">=7.0.0 <8"
|
|
72
|
+
"ai": ">=7.0.0 <8",
|
|
73
|
+
"h3-mcp": ">=0.2.0 <0.3"
|
|
67
74
|
},
|
|
68
75
|
"peerDependenciesMeta": {
|
|
69
76
|
"@earendil-works/pi-coding-agent": {
|
|
@@ -77,10 +84,13 @@
|
|
|
77
84
|
},
|
|
78
85
|
"ai": {
|
|
79
86
|
"optional": true
|
|
87
|
+
},
|
|
88
|
+
"h3-mcp": {
|
|
89
|
+
"optional": true
|
|
80
90
|
}
|
|
81
91
|
},
|
|
82
92
|
"engines": {
|
|
83
|
-
"node": ">=
|
|
93
|
+
"node": ">=26"
|
|
84
94
|
},
|
|
85
95
|
"scripts": {
|
|
86
96
|
"build": "obuild",
|
package/dist/index.d.mts.map
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
{"version":3,"file":"index.d.mts","names":[],"sources":["../src/index.ts"],"mappings":";;YAgCY;EACN;EAAc;;EACd;EAAe;EAAc;;;iBAGlB,WAAW;EAC1B,SAAS;EACT,SAAS;;EAET;;;;;;;;;YAUU;;;;;iBAMK;;WAEN;WACA;;WAEA;;WAEA;;WAEA;;;iBAIM;EACf,SAAS;;iBAGM,eAAe,cAAc,UAAU,SAAS;;WAEtD;;WAEA;;WAEA;;WAEA;;WAEA;WACA,QAAQ;;WAER;;WAEA;WACA,OAAO;;WAEP,MAAM;;;;;;;EAOf,QACE,OAAO,OAAO,QACd,SAAS,kBACR,WAAW,WAAW,QAAQ,WAAW;;;qBAIjC,4BAA4B;EAC9B;;;qBAIE,uBAAuB;EACzB;;WAEA;;;;EAKT,YAAY;;;;;;;;wBAcE,iBAAiB,cAAc,SAAS,SACtD,MAAM,eAAe,OAAO,WAC3B,eAAe,OAAO;;;;;;;;;;wBA+GT,WAAW,MAAM,iBAAiB;;;;;;;;;;wBAalC,WAAW,gBAAgB,mBAAmB,oBAAoB;YAStE,WAAW,cAAc;EAC/B;EAAU,OAAO,OAAO;;EACxB;EAAW;;;;;;;;;;;;;;;;wBAgBD,cAAc,cAAc,SAC1C,MAAM,eAAe,iBACrB,gBACC,WAAW;;;;;;;;;;wBAyJQ,WACpB,MAAM,gBACN,eACA,UAAS,kBACR,QAAQ;;;;;;;;;;;;wBAmBK,aAAa;;;;;;;wBAab,WAAW,QAAQ"}
|