pi-incise 0.1.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/LICENSE +21 -0
- package/README.md +27 -0
- package/extension/binary.ts +124 -0
- package/extension/index.ts +138 -0
- package/extension/normalize.ts +81 -0
- package/extension/runner.ts +68 -0
- package/extension/schemas.ts +97 -0
- package/package.json +57 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 pdalinis
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
package/README.md
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
# pi-incise
|
|
2
|
+
|
|
3
|
+
Structured, byte-preserving Markdown tools for [Pi](https://pi.dev). The package supplies eight tools backed by the native Incise CLI and does not download or compile code during installation.
|
|
4
|
+
|
|
5
|
+
## Install
|
|
6
|
+
|
|
7
|
+
Before promotion to `latest`, install the release candidate with:
|
|
8
|
+
|
|
9
|
+
```bash
|
|
10
|
+
pi install npm:pi-incise@next
|
|
11
|
+
```
|
|
12
|
+
|
|
13
|
+
After the live composition benchmark passes and that exact version is promoted, `pi install npm:pi-incise` installs the stable release.
|
|
14
|
+
|
|
15
|
+
Prebuilt binaries are provided for macOS arm64/x64 and glibc Linux arm64/x64. Windows and musl Linux are not supported in v1.
|
|
16
|
+
|
|
17
|
+
## Tools
|
|
18
|
+
|
|
19
|
+
The package registers `table_edit`, `list_edit`, `section_edit`, `frontmatter_edit`, `table_get`, `md_tables`, `md_lists`, and `md_outline`. Inspect document structure with the relevant read tool before editing, and follow the remedy in any Incise refusal.
|
|
20
|
+
|
|
21
|
+
## Binary resolution
|
|
22
|
+
|
|
23
|
+
The extension uses `INCISE_BIN` first, then the matching optional native package, then `incise` on `PATH`, and finally a release or debug binary from a development checkout. `/incise-doctor` reports the selected path, versions, schema status, and registered-tool count. Version mismatches from explicit overrides are warnings; a mismatched packaged binary disables the tools as an installation-integrity failure.
|
|
24
|
+
|
|
25
|
+
## Security
|
|
26
|
+
|
|
27
|
+
Pi extensions and their subprocesses run with the user’s full permissions. Review this package before installation and use OS- or container-level isolation for unattended execution. Incise performs atomic, byte-preserving edits, but it is not a filesystem sandbox.
|
|
@@ -0,0 +1,124 @@
|
|
|
1
|
+
import { accessSync, constants, existsSync, readFileSync } from "node:fs";
|
|
2
|
+
import { createRequire } from "node:module";
|
|
3
|
+
import { delimiter, dirname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
import { fileURLToPath } from "node:url";
|
|
5
|
+
|
|
6
|
+
export type BinarySource = "environment" | "package" | "path" | "development";
|
|
7
|
+
|
|
8
|
+
export interface ResolvedBinary {
|
|
9
|
+
path: string;
|
|
10
|
+
source: BinarySource;
|
|
11
|
+
packageName?: string;
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export interface RuntimePlatform {
|
|
15
|
+
platform: NodeJS.Platform;
|
|
16
|
+
arch: string;
|
|
17
|
+
glibc: boolean;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
const extensionDirectory = dirname(fileURLToPath(import.meta.url));
|
|
21
|
+
export const packageRoot = resolve(extensionDirectory, "..");
|
|
22
|
+
|
|
23
|
+
export function packageVersion(root = packageRoot): string {
|
|
24
|
+
const manifest = JSON.parse(readFileSync(join(root, "package.json"), "utf8")) as { version?: unknown };
|
|
25
|
+
if (typeof manifest.version !== "string" || !manifest.version) {
|
|
26
|
+
throw new Error("pi-incise package.json has no version");
|
|
27
|
+
}
|
|
28
|
+
return manifest.version;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function currentRuntime(): RuntimePlatform {
|
|
32
|
+
let glibc = false;
|
|
33
|
+
if (process.platform === "linux") {
|
|
34
|
+
const report = process.report?.getReport() as { header?: { glibcVersionRuntime?: unknown } } | undefined;
|
|
35
|
+
glibc = typeof report?.header?.glibcVersionRuntime === "string";
|
|
36
|
+
}
|
|
37
|
+
return { platform: process.platform, arch: process.arch, glibc };
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export function nativePackageFor(runtime: RuntimePlatform): string | undefined {
|
|
41
|
+
if (runtime.platform === "darwin" && runtime.arch === "arm64") {
|
|
42
|
+
return "@pdalinis/pi-incise-darwin-arm64";
|
|
43
|
+
}
|
|
44
|
+
if (runtime.platform === "darwin" && runtime.arch === "x64") {
|
|
45
|
+
return "@pdalinis/pi-incise-darwin-x64";
|
|
46
|
+
}
|
|
47
|
+
if (runtime.platform === "linux" && runtime.glibc && runtime.arch === "arm64") {
|
|
48
|
+
return "@pdalinis/pi-incise-linux-arm64-gnu";
|
|
49
|
+
}
|
|
50
|
+
if (runtime.platform === "linux" && runtime.glibc && runtime.arch === "x64") {
|
|
51
|
+
return "@pdalinis/pi-incise-linux-x64-gnu";
|
|
52
|
+
}
|
|
53
|
+
return undefined;
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
function executable(path: string): boolean {
|
|
57
|
+
try {
|
|
58
|
+
accessSync(path, constants.X_OK);
|
|
59
|
+
return true;
|
|
60
|
+
} catch {
|
|
61
|
+
return false;
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
function pathExecutable(name: string, pathValue: string | undefined): string | undefined {
|
|
66
|
+
for (const directory of (pathValue ?? "").split(delimiter)) {
|
|
67
|
+
if (!directory) continue;
|
|
68
|
+
const candidate = join(directory, name);
|
|
69
|
+
if (executable(candidate)) return candidate;
|
|
70
|
+
}
|
|
71
|
+
return undefined;
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
export function developmentCandidates(start = extensionDirectory): string[] {
|
|
75
|
+
let directory = resolve(start);
|
|
76
|
+
for (;;) {
|
|
77
|
+
if (existsSync(join(directory, "Cargo.toml")) && existsSync(join(directory, "crates"))) {
|
|
78
|
+
return [join(directory, "target", "release", "incise"), join(directory, "target", "debug", "incise")];
|
|
79
|
+
}
|
|
80
|
+
const parent = dirname(directory);
|
|
81
|
+
if (parent === directory) return [];
|
|
82
|
+
directory = parent;
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
export interface ResolveBinaryOptions {
|
|
87
|
+
env?: NodeJS.ProcessEnv;
|
|
88
|
+
runtime?: RuntimePlatform;
|
|
89
|
+
startDirectory?: string;
|
|
90
|
+
requireFrom?: string;
|
|
91
|
+
}
|
|
92
|
+
|
|
93
|
+
export function resolveBinary(options: ResolveBinaryOptions = {}): ResolvedBinary | undefined {
|
|
94
|
+
const env = options.env ?? process.env;
|
|
95
|
+
const explicit = env.INCISE_BIN;
|
|
96
|
+
if (explicit) {
|
|
97
|
+
const candidate = isAbsolute(explicit) ? explicit : resolve(explicit);
|
|
98
|
+
if (executable(candidate)) return { path: candidate, source: "environment" };
|
|
99
|
+
}
|
|
100
|
+
|
|
101
|
+
const packageName = nativePackageFor(options.runtime ?? currentRuntime());
|
|
102
|
+
if (packageName) {
|
|
103
|
+
try {
|
|
104
|
+
const require = createRequire(options.requireFrom ?? import.meta.url);
|
|
105
|
+
const manifest = require.resolve(`${packageName}/package.json`);
|
|
106
|
+
const candidate = join(dirname(manifest), "bin", "incise");
|
|
107
|
+
if (executable(candidate)) return { path: candidate, source: "package", packageName };
|
|
108
|
+
} catch {
|
|
109
|
+
// An optional dependency is absent on unsupported systems and in development checkouts.
|
|
110
|
+
}
|
|
111
|
+
}
|
|
112
|
+
|
|
113
|
+
const onPath = pathExecutable("incise", env.PATH);
|
|
114
|
+
if (onPath) return { path: onPath, source: "path" };
|
|
115
|
+
|
|
116
|
+
for (const candidate of developmentCandidates(options.startDirectory)) {
|
|
117
|
+
if (executable(candidate)) return { path: candidate, source: "development" };
|
|
118
|
+
}
|
|
119
|
+
return undefined;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export function parseBinaryVersion(stdout: string): string | undefined {
|
|
123
|
+
return stdout.trim().match(/^incise\s+(\S+)$/)?.[1];
|
|
124
|
+
}
|
|
@@ -0,0 +1,138 @@
|
|
|
1
|
+
import { withFileMutationQueue, type ExtensionAPI } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
import type { TSchema } from "typebox";
|
|
3
|
+
|
|
4
|
+
import { packageVersion, resolveBinary, type ResolvedBinary } from "./binary.ts";
|
|
5
|
+
import { prepareInvocation, READ_SUBCOMMAND, type ToolArguments } from "./normalize.ts";
|
|
6
|
+
import { binaryVersion, processError, runIncise } from "./runner.ts";
|
|
7
|
+
import { loadMeasuredSchemas, PROMPT_METADATA, STRUCTURAL_READ_SCHEMAS, type ToolSchema } from "./schemas.ts";
|
|
8
|
+
|
|
9
|
+
interface Diagnostics {
|
|
10
|
+
packageVersion: string;
|
|
11
|
+
binary?: ResolvedBinary;
|
|
12
|
+
binaryVersion?: string;
|
|
13
|
+
schemas: ToolSchema[];
|
|
14
|
+
error?: string;
|
|
15
|
+
fatalMismatch: boolean;
|
|
16
|
+
}
|
|
17
|
+
|
|
18
|
+
function diagnosticText(diagnostics: Diagnostics): string {
|
|
19
|
+
const registered = diagnostics.schemas.length === 5 && !diagnostics.fatalMismatch
|
|
20
|
+
? [...diagnostics.schemas, ...STRUCTURAL_READ_SCHEMAS].map((schema) => schema.name).join(", ")
|
|
21
|
+
: "none";
|
|
22
|
+
const lines = [
|
|
23
|
+
`pi-incise ${diagnostics.packageVersion}`,
|
|
24
|
+
`binary: ${diagnostics.binary?.path ?? "not found"}`,
|
|
25
|
+
`binary source: ${diagnostics.binary?.source ?? "none"}`,
|
|
26
|
+
`binary version: ${diagnostics.binaryVersion ?? "unknown"}`,
|
|
27
|
+
`schemas: ${diagnostics.schemas.length === 5 ? "available" : "unavailable"}`,
|
|
28
|
+
`registered tools: ${registered}`,
|
|
29
|
+
];
|
|
30
|
+
if (diagnostics.binaryVersion && diagnostics.binaryVersion !== diagnostics.packageVersion) {
|
|
31
|
+
lines.push(`warning: package ${diagnostics.packageVersion} is using incise ${diagnostics.binaryVersion}`);
|
|
32
|
+
}
|
|
33
|
+
if (diagnostics.error) lines.push(`error: ${diagnostics.error}`);
|
|
34
|
+
return lines.join("\n");
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
function registerDoctor(pi: ExtensionAPI, diagnostics: Diagnostics): void {
|
|
38
|
+
pi.registerCommand("incise-doctor", {
|
|
39
|
+
description: "Report the pi-incise package, binary, schema, and tool status",
|
|
40
|
+
handler: async (_args, ctx) => {
|
|
41
|
+
const text = diagnosticText(diagnostics);
|
|
42
|
+
const severity = diagnostics.error || diagnostics.fatalMismatch ? "error" :
|
|
43
|
+
diagnostics.binaryVersion && diagnostics.binaryVersion !== diagnostics.packageVersion ? "warning" : "info";
|
|
44
|
+
ctx.ui.notify(text, severity);
|
|
45
|
+
},
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
function argvFor(name: string, operation: string, path: string, args: ToolArguments): string[] {
|
|
50
|
+
const argv = [operation, path];
|
|
51
|
+
if (!(name in READ_SUBCOMMAND) || name === "table_get") {
|
|
52
|
+
argv.push("--args", JSON.stringify(args));
|
|
53
|
+
}
|
|
54
|
+
return argv;
|
|
55
|
+
}
|
|
56
|
+
|
|
57
|
+
function registerTool(pi: ExtensionAPI, binary: ResolvedBinary, schema: ToolSchema): void {
|
|
58
|
+
const prompt = PROMPT_METADATA[schema.name];
|
|
59
|
+
pi.registerTool({
|
|
60
|
+
name: schema.name,
|
|
61
|
+
label: schema.name,
|
|
62
|
+
description: schema.description,
|
|
63
|
+
promptSnippet: prompt?.snippet,
|
|
64
|
+
promptGuidelines: prompt?.guidelines,
|
|
65
|
+
parameters: schema.parameters as TSchema,
|
|
66
|
+
async execute(_toolCallId, params, signal, _onUpdate, ctx) {
|
|
67
|
+
const invocation = prepareInvocation(schema.name, params as ToolArguments, ctx.cwd);
|
|
68
|
+
const call = async () => runIncise(
|
|
69
|
+
pi.exec.bind(pi),
|
|
70
|
+
binary.path,
|
|
71
|
+
argvFor(schema.name, invocation.operation, invocation.path, invocation.args),
|
|
72
|
+
signal,
|
|
73
|
+
);
|
|
74
|
+
const result = invocation.write
|
|
75
|
+
? await withFileMutationQueue(invocation.path, call)
|
|
76
|
+
: await call();
|
|
77
|
+
if (result.code !== 0 || result.payload.ok === false) throw processError(result);
|
|
78
|
+
|
|
79
|
+
if (invocation.write) {
|
|
80
|
+
return {
|
|
81
|
+
content: [{ type: "text", text: String(result.payload.description ?? "") }],
|
|
82
|
+
details: {
|
|
83
|
+
exitCode: result.code,
|
|
84
|
+
hash: result.payload.hash,
|
|
85
|
+
path: result.payload.path ?? invocation.path,
|
|
86
|
+
changed: Boolean(result.payload.changed),
|
|
87
|
+
},
|
|
88
|
+
};
|
|
89
|
+
}
|
|
90
|
+
return {
|
|
91
|
+
content: [{ type: "text", text: String(result.payload.text ?? "") }],
|
|
92
|
+
details: {
|
|
93
|
+
exitCode: result.code,
|
|
94
|
+
hash: result.payload.hash,
|
|
95
|
+
path: result.payload.path ?? invocation.path,
|
|
96
|
+
},
|
|
97
|
+
};
|
|
98
|
+
},
|
|
99
|
+
});
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
export default async function inciseExtension(pi: ExtensionAPI): Promise<void> {
|
|
103
|
+
const expectedVersion = packageVersion();
|
|
104
|
+
const binary = resolveBinary();
|
|
105
|
+
const diagnostics: Diagnostics = {
|
|
106
|
+
packageVersion: expectedVersion,
|
|
107
|
+
binary,
|
|
108
|
+
schemas: [],
|
|
109
|
+
fatalMismatch: false,
|
|
110
|
+
};
|
|
111
|
+
|
|
112
|
+
if (!binary) {
|
|
113
|
+
diagnostics.error = "No Incise binary is available for this platform. Set INCISE_BIN or install incise on PATH.";
|
|
114
|
+
registerDoctor(pi, diagnostics);
|
|
115
|
+
return;
|
|
116
|
+
}
|
|
117
|
+
|
|
118
|
+
diagnostics.binaryVersion = await binaryVersion(pi.exec.bind(pi), binary.path);
|
|
119
|
+
if (binary.source === "package" && diagnostics.binaryVersion !== expectedVersion) {
|
|
120
|
+
diagnostics.fatalMismatch = true;
|
|
121
|
+
diagnostics.error = "The packaged native binary version does not match pi-incise; reinstall the package.";
|
|
122
|
+
registerDoctor(pi, diagnostics);
|
|
123
|
+
return;
|
|
124
|
+
}
|
|
125
|
+
|
|
126
|
+
try {
|
|
127
|
+
diagnostics.schemas = await loadMeasuredSchemas(pi.exec.bind(pi), binary.path);
|
|
128
|
+
} catch (error) {
|
|
129
|
+
diagnostics.error = error instanceof Error ? error.message : String(error);
|
|
130
|
+
registerDoctor(pi, diagnostics);
|
|
131
|
+
return;
|
|
132
|
+
}
|
|
133
|
+
|
|
134
|
+
registerDoctor(pi, diagnostics);
|
|
135
|
+
for (const schema of [...diagnostics.schemas, ...STRUCTURAL_READ_SCHEMAS]) {
|
|
136
|
+
registerTool(pi, binary, schema);
|
|
137
|
+
}
|
|
138
|
+
}
|
|
@@ -0,0 +1,81 @@
|
|
|
1
|
+
import { resolve } from "node:path";
|
|
2
|
+
|
|
3
|
+
export type ToolArguments = Record<string, unknown>;
|
|
4
|
+
|
|
5
|
+
export const WRITE_TOOLS = new Set([
|
|
6
|
+
"table_edit",
|
|
7
|
+
"list_edit",
|
|
8
|
+
"section_edit",
|
|
9
|
+
"frontmatter_edit",
|
|
10
|
+
]);
|
|
11
|
+
|
|
12
|
+
export const READ_SUBCOMMAND: Readonly<Record<string, string>> = {
|
|
13
|
+
md_outline: "outline",
|
|
14
|
+
md_tables: "tables",
|
|
15
|
+
md_lists: "lists",
|
|
16
|
+
table_get: "rows",
|
|
17
|
+
};
|
|
18
|
+
|
|
19
|
+
function actionName(value: unknown): string {
|
|
20
|
+
return value === undefined || value === null ? "None" : String(value);
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export function normalizeEdit(name: string, input: ToolArguments): { operation: string; args: ToolArguments } {
|
|
24
|
+
if (name === "table_edit") return { operation: `table-${actionName(input.action)}`, args: input };
|
|
25
|
+
if (name === "list_edit") return { operation: `list-${actionName(input.action)}`, args: input };
|
|
26
|
+
if (name === "frontmatter_edit") {
|
|
27
|
+
return { operation: `frontmatter-${actionName(input.action)}`, args: input };
|
|
28
|
+
}
|
|
29
|
+
if (name !== "section_edit") throw new Error(`not an edit tool: ${name}`);
|
|
30
|
+
|
|
31
|
+
const args: ToolArguments = { ...input };
|
|
32
|
+
if ("new_heading" in args) {
|
|
33
|
+
args.heading = args.new_heading;
|
|
34
|
+
delete args.new_heading;
|
|
35
|
+
}
|
|
36
|
+
if ("file" in args && !("path" in args)) {
|
|
37
|
+
args.path = args.file;
|
|
38
|
+
delete args.file;
|
|
39
|
+
}
|
|
40
|
+
if (args.section && typeof args.section === "object" && !Array.isArray(args.section)) {
|
|
41
|
+
const section = { ...(args.section as ToolArguments) };
|
|
42
|
+
if ("heading" in section && !("path" in section)) {
|
|
43
|
+
section.path = section.heading;
|
|
44
|
+
delete section.heading;
|
|
45
|
+
}
|
|
46
|
+
args.section = section;
|
|
47
|
+
}
|
|
48
|
+
return { operation: `section-${actionName(args.action)}`, args };
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
export function stripPiPathPrefix(path: string): string {
|
|
52
|
+
return path.startsWith("@") ? path.slice(1) : path;
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function resolveToolPath(args: ToolArguments, cwd: string): { path: string; args: ToolArguments } {
|
|
56
|
+
const raw = args.path ?? args.file;
|
|
57
|
+
if (typeof raw !== "string" || !raw) {
|
|
58
|
+
throw new Error("incise needs `path`: the markdown file to read or edit.");
|
|
59
|
+
}
|
|
60
|
+
const path = resolve(cwd, stripPiPathPrefix(raw));
|
|
61
|
+
return { path, args: { ...args, path } };
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export interface Invocation {
|
|
65
|
+
operation: string;
|
|
66
|
+
path: string;
|
|
67
|
+
args: ToolArguments;
|
|
68
|
+
write: boolean;
|
|
69
|
+
}
|
|
70
|
+
|
|
71
|
+
export function prepareInvocation(name: string, input: ToolArguments, cwd: string): Invocation {
|
|
72
|
+
if (WRITE_TOOLS.has(name)) {
|
|
73
|
+
const normalized = normalizeEdit(name, input);
|
|
74
|
+
const located = resolveToolPath(normalized.args, cwd);
|
|
75
|
+
return { operation: normalized.operation, path: located.path, args: located.args, write: true };
|
|
76
|
+
}
|
|
77
|
+
const operation = READ_SUBCOMMAND[name];
|
|
78
|
+
if (!operation) throw new Error(`unknown incise tool: ${name}`);
|
|
79
|
+
const located = resolveToolPath(input, cwd);
|
|
80
|
+
return { operation, path: located.path, args: located.args, write: false };
|
|
81
|
+
}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import type { ExecOptions, ExecResult } from "@earendil-works/pi-coding-agent";
|
|
2
|
+
|
|
3
|
+
export type Exec = (command: string, args: string[], options?: ExecOptions) => Promise<ExecResult>;
|
|
4
|
+
|
|
5
|
+
export interface IncisePayload {
|
|
6
|
+
ok?: boolean;
|
|
7
|
+
error?: string;
|
|
8
|
+
description?: string;
|
|
9
|
+
text?: string;
|
|
10
|
+
hash?: string;
|
|
11
|
+
path?: string;
|
|
12
|
+
changed?: boolean;
|
|
13
|
+
written?: boolean;
|
|
14
|
+
stale?: boolean;
|
|
15
|
+
usage?: boolean;
|
|
16
|
+
[key: string]: unknown;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
export interface InciseResult {
|
|
20
|
+
code: number;
|
|
21
|
+
killed: boolean;
|
|
22
|
+
stderr: string;
|
|
23
|
+
payload: IncisePayload;
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
function fallbackMessage(result: ExecResult): string {
|
|
27
|
+
return (result.stderr || result.stdout).trim() || `incise exited ${result.code}`;
|
|
28
|
+
}
|
|
29
|
+
|
|
30
|
+
export async function runIncise(
|
|
31
|
+
exec: Exec,
|
|
32
|
+
binary: string,
|
|
33
|
+
args: string[],
|
|
34
|
+
signal?: AbortSignal,
|
|
35
|
+
timeout = 30_000,
|
|
36
|
+
): Promise<InciseResult> {
|
|
37
|
+
const result = await exec(binary, [...args, "--json"], { signal, timeout });
|
|
38
|
+
let payload: IncisePayload = {};
|
|
39
|
+
try {
|
|
40
|
+
payload = result.stdout.trim() ? JSON.parse(result.stdout) as IncisePayload : {};
|
|
41
|
+
} catch {
|
|
42
|
+
// The synthesized error below retains the process output for non-JSON failures.
|
|
43
|
+
}
|
|
44
|
+
if (!payload || typeof payload !== "object" || Array.isArray(payload)) payload = {};
|
|
45
|
+
if (Object.keys(payload).length === 0) payload = { ok: false, error: fallbackMessage(result) };
|
|
46
|
+
return { code: result.code, killed: result.killed, stderr: result.stderr, payload };
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
export async function binaryVersion(exec: Exec, binary: string): Promise<string | undefined> {
|
|
50
|
+
const result = await exec(binary, ["--version"], { timeout: 10_000 });
|
|
51
|
+
if (result.code !== 0) return undefined;
|
|
52
|
+
return result.stdout.trim().match(/^incise\s+(\S+)$/)?.[1];
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function processError(result: InciseResult): Error {
|
|
56
|
+
const message = typeof result.payload.error === "string"
|
|
57
|
+
? result.payload.error
|
|
58
|
+
: `incise exited ${result.code} with nothing to say.`;
|
|
59
|
+
const error = new Error(message) as Error & {
|
|
60
|
+
exitCode?: number;
|
|
61
|
+
kind?: "refusal" | "usage" | "stale" | "process";
|
|
62
|
+
stderr?: string;
|
|
63
|
+
};
|
|
64
|
+
error.exitCode = result.code;
|
|
65
|
+
error.kind = result.code === 1 ? "refusal" : result.code === 2 ? "usage" : result.code === 3 ? "stale" : "process";
|
|
66
|
+
error.stderr = result.stderr;
|
|
67
|
+
return error;
|
|
68
|
+
}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import type { Exec } from "./runner.ts";
|
|
2
|
+
|
|
3
|
+
export interface ToolSchema {
|
|
4
|
+
name: string;
|
|
5
|
+
description: string;
|
|
6
|
+
parameters: Record<string, unknown>;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const PATH = {
|
|
10
|
+
type: "string",
|
|
11
|
+
description: "Path to the markdown file.",
|
|
12
|
+
};
|
|
13
|
+
|
|
14
|
+
export const STRUCTURAL_READ_SCHEMAS: ToolSchema[] = [
|
|
15
|
+
{
|
|
16
|
+
name: "md_tables",
|
|
17
|
+
description: "List every markdown table in a document: the heading each one sits under, its column names, and how many rows it has. Returns the structure, not the document text. Call this before a `table_edit` to get the exact `table` heading and `column` spellings it needs, and again if an edit is refused for a table or column that is not there.",
|
|
18
|
+
parameters: { type: "object", properties: { path: PATH }, required: ["path"] },
|
|
19
|
+
},
|
|
20
|
+
{
|
|
21
|
+
name: "md_lists",
|
|
22
|
+
description: "List every bullet or task list in a markdown document: the heading each one sits under, how many items it has, and whether they are checkboxes. Returns the structure, not the document text. Call this before a `list_edit` to get the exact `list` heading and the item text its `match` needs.",
|
|
23
|
+
parameters: { type: "object", properties: { path: PATH }, required: ["path"] },
|
|
24
|
+
},
|
|
25
|
+
{
|
|
26
|
+
name: "md_outline",
|
|
27
|
+
description: "List the headings of a markdown document as a tree, with the level and ordinal of each. Returns the structure, not the document text. Call this to find the heading to address a `section_edit` to, or to see what a file contains before editing it.",
|
|
28
|
+
parameters: { type: "object", properties: { path: PATH }, required: ["path"] },
|
|
29
|
+
},
|
|
30
|
+
];
|
|
31
|
+
|
|
32
|
+
const MEASURED_NAMES = new Set([
|
|
33
|
+
"table_edit",
|
|
34
|
+
"list_edit",
|
|
35
|
+
"section_edit",
|
|
36
|
+
"frontmatter_edit",
|
|
37
|
+
"table_get",
|
|
38
|
+
]);
|
|
39
|
+
|
|
40
|
+
export async function loadMeasuredSchemas(exec: Exec, binary: string): Promise<ToolSchema[]> {
|
|
41
|
+
const result = await exec(binary, ["schema"], { timeout: 10_000 });
|
|
42
|
+
if (result.code !== 0) throw new Error((result.stderr || result.stdout).trim() || "incise schema failed");
|
|
43
|
+
let value: unknown;
|
|
44
|
+
try {
|
|
45
|
+
value = JSON.parse(result.stdout);
|
|
46
|
+
} catch {
|
|
47
|
+
throw new Error("incise schema did not return valid JSON");
|
|
48
|
+
}
|
|
49
|
+
if (!Array.isArray(value) || value.length !== MEASURED_NAMES.size) {
|
|
50
|
+
throw new Error("incise schema did not return the five measured tool schemas");
|
|
51
|
+
}
|
|
52
|
+
const schemas = value as ToolSchema[];
|
|
53
|
+
if (!schemas.every((schema) => schema && typeof schema.name === "string" &&
|
|
54
|
+
MEASURED_NAMES.has(schema.name) && typeof schema.description === "string" &&
|
|
55
|
+
schema.parameters && typeof schema.parameters === "object")) {
|
|
56
|
+
throw new Error("incise schema returned an unexpected tool schema");
|
|
57
|
+
}
|
|
58
|
+
if (new Set(schemas.map((schema) => schema.name)).size !== MEASURED_NAMES.size) {
|
|
59
|
+
throw new Error("incise schema returned duplicate tool names");
|
|
60
|
+
}
|
|
61
|
+
return schemas;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
export const PROMPT_METADATA: Readonly<Record<string, { snippet: string; guidelines: string[] }>> = {
|
|
65
|
+
md_tables: {
|
|
66
|
+
snippet: "Inspect Markdown tables before structured table edits",
|
|
67
|
+
guidelines: ["Use md_tables before table_edit to copy exact table addresses and column names."],
|
|
68
|
+
},
|
|
69
|
+
md_lists: {
|
|
70
|
+
snippet: "Inspect Markdown lists before structured list edits",
|
|
71
|
+
guidelines: ["Use md_lists before list_edit to copy exact list addresses and item text."],
|
|
72
|
+
},
|
|
73
|
+
md_outline: {
|
|
74
|
+
snippet: "Inspect Markdown section structure before section edits",
|
|
75
|
+
guidelines: ["Use md_outline before section_edit to copy exact heading paths."],
|
|
76
|
+
},
|
|
77
|
+
table_get: {
|
|
78
|
+
snippet: "Read selected rows from a Markdown table",
|
|
79
|
+
guidelines: ["Use table_get instead of reconstructing a Markdown table when only selected rows are needed."],
|
|
80
|
+
},
|
|
81
|
+
table_edit: {
|
|
82
|
+
snippet: "Edit Markdown tables without reconstructing them",
|
|
83
|
+
guidelines: ["Use table_edit for structured Markdown table changes and follow any Incise refusal remedy."],
|
|
84
|
+
},
|
|
85
|
+
list_edit: {
|
|
86
|
+
snippet: "Edit Markdown lists without reconstructing them",
|
|
87
|
+
guidelines: ["Use list_edit for structured Markdown list changes and follow any Incise refusal remedy."],
|
|
88
|
+
},
|
|
89
|
+
section_edit: {
|
|
90
|
+
snippet: "Edit Markdown sections without reconstructing them",
|
|
91
|
+
guidelines: ["Use section_edit for Markdown section changes and follow any Incise refusal remedy."],
|
|
92
|
+
},
|
|
93
|
+
frontmatter_edit: {
|
|
94
|
+
snippet: "Edit Markdown frontmatter without rewriting it",
|
|
95
|
+
guidelines: ["Use frontmatter_edit for Markdown frontmatter changes and follow any Incise refusal remedy."],
|
|
96
|
+
},
|
|
97
|
+
};
|
package/package.json
ADDED
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "pi-incise",
|
|
3
|
+
"version": "0.1.1",
|
|
4
|
+
"description": "Byte-preserving structured Markdown tools for Pi",
|
|
5
|
+
"type": "module",
|
|
6
|
+
"license": "MIT",
|
|
7
|
+
"author": "Peter Dalinis",
|
|
8
|
+
"repository": {
|
|
9
|
+
"type": "git",
|
|
10
|
+
"url": "git+https://github.com/pdalinis/incise.git",
|
|
11
|
+
"directory": "plugins/pi"
|
|
12
|
+
},
|
|
13
|
+
"homepage": "https://github.com/pdalinis/incise#readme",
|
|
14
|
+
"bugs": "https://github.com/pdalinis/incise/issues",
|
|
15
|
+
"keywords": [
|
|
16
|
+
"pi-package",
|
|
17
|
+
"markdown",
|
|
18
|
+
"structured-editing",
|
|
19
|
+
"ai-agent"
|
|
20
|
+
],
|
|
21
|
+
"engines": {
|
|
22
|
+
"node": ">=22.19.0"
|
|
23
|
+
},
|
|
24
|
+
"files": [
|
|
25
|
+
"extension",
|
|
26
|
+
"LICENSE",
|
|
27
|
+
"README.md"
|
|
28
|
+
],
|
|
29
|
+
"pi": {
|
|
30
|
+
"extensions": [
|
|
31
|
+
"./extension/index.ts"
|
|
32
|
+
]
|
|
33
|
+
},
|
|
34
|
+
"peerDependencies": {
|
|
35
|
+
"@earendil-works/pi-coding-agent": "*",
|
|
36
|
+
"typebox": "*"
|
|
37
|
+
},
|
|
38
|
+
"devDependencies": {
|
|
39
|
+
"@earendil-works/pi-coding-agent": "^0.85.1",
|
|
40
|
+
"@types/node": "^22.19.19",
|
|
41
|
+
"typescript": "^5.9.3"
|
|
42
|
+
},
|
|
43
|
+
"optionalDependencies": {
|
|
44
|
+
"@pdalinis/pi-incise-darwin-arm64": "0.1.1",
|
|
45
|
+
"@pdalinis/pi-incise-darwin-x64": "0.1.1",
|
|
46
|
+
"@pdalinis/pi-incise-linux-arm64-gnu": "0.1.1",
|
|
47
|
+
"@pdalinis/pi-incise-linux-x64-gnu": "0.1.1"
|
|
48
|
+
},
|
|
49
|
+
"scripts": {
|
|
50
|
+
"test": "node --test --experimental-strip-types test/*.test.ts",
|
|
51
|
+
"typecheck": "tsc --noEmit",
|
|
52
|
+
"pack:check": "node --experimental-strip-types test/pack-check.ts"
|
|
53
|
+
},
|
|
54
|
+
"publishConfig": {
|
|
55
|
+
"access": "public"
|
|
56
|
+
}
|
|
57
|
+
}
|