pi-incise 0.3.0 → 0.3.3
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 +13 -12
- package/extension/minicpm-list.ts +10 -3
- package/extension/normalize.ts +8 -1
- package/extension/profiles.ts +5 -5
- package/extension/safe-routed.ts +502 -66
- package/package.json +5 -5
package/README.md
CHANGED
|
@@ -18,13 +18,14 @@ Choose the profile when starting Pi:
|
|
|
18
18
|
|
|
19
19
|
* **Ornith 1.5 9B (recommended):** `INCISE_PROFILE=auto pi --thinking off`
|
|
20
20
|
* **Gemma (recommended):** `INCISE_PROFILE=auto pi`
|
|
21
|
-
* **
|
|
22
|
-
* **MiniCPM list-addition evaluation:** `INCISE_PROFILE=minicpm-list pi`
|
|
21
|
+
* **MiniCPM5 2B (recommended):** `INCISE_PROFILE=auto pi --thinking off`
|
|
23
22
|
* **Unknown or changing model families:** `INCISE_PROFILE=standard pi`
|
|
24
23
|
|
|
25
|
-
For Ornith, configure its Pi model entry with `maxTokens: 2048` and `samplingParams.parallel_tool_calls: false`. The measured result used temperature 0.6, top-p 0.95, top-k 20, min-p 0, presence penalty 0, and repeat penalty 1 through llama.cpp.
|
|
24
|
+
For Ornith, configure its Pi model entry with `maxTokens: 2048` and `samplingParams.parallel_tool_calls: false`. The measured result used temperature 0.6, top-p 0.95, top-k 20, min-p 0, presence penalty 0, and repeat penalty 1 through llama.cpp.
|
|
26
25
|
|
|
27
|
-
|
|
26
|
+
For MiniCPM5, the measured result used the official Q8_0 checkpoint through llama.cpp, thinking off, a 65,536-token context, `maxTokens: 8192`, temperature 0.7, and top-p 0.95. The profile does not silently change provider decoding settings.
|
|
27
|
+
|
|
28
|
+
The package default is `standard` for model-agnostic compatibility. `auto` enables the measured `safe-routed` profile for Gemma, MiniCPM, and Ornith; unknown families stay on `standard`. If a local provider exposes a generic model ID, set `INCISE_MODEL_FAMILY=gemma`, `INCISE_MODEL_FAMILY=minicpm`, `INCISE_MODEL_FAMILY=ornith`, or `INCISE_MODEL_FAMILY=unknown`.
|
|
28
29
|
|
|
29
30
|
After Pi starts, run `/incise-doctor`. It reports the requested and effective profiles, detected model family, selected binary, schema state, registered tools, and last route.
|
|
30
31
|
|
|
@@ -32,23 +33,23 @@ After Pi starts, run `/incise-doctor`. It reports the requested and effective pr
|
|
|
32
33
|
|
|
33
34
|
The default `standard` profile 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. The former `measured` profile name remains an alias for `standard`.
|
|
34
35
|
|
|
35
|
-
Set `INCISE_PROFILE=safe-routed` to use guarded request routing directly, or use `auto` with a measured Gemma or Ornith identity. The routed profile retains standard tools as fallback. For requests it can prove unambiguous, it inspects current structure and replaces the broad surface for that turn with one small action-specific tool. Host-owned routes cover
|
|
36
|
+
Set `INCISE_PROFILE=safe-routed` to use guarded request routing directly, or use `auto` with a measured Gemma, MiniCPM, or Ornith identity. The routed profile retains standard tools as fallback. For requests it can prove unambiguous, it inspects current structure and replaces the broad surface for that turn with one small action-specific tool. Host-owned routes cover exact table reads and mutations, list relations and checkbox state, literal section insertion, append, deletion, rename, level changes and qualified body replacement, plus typed, create-only, delete, and compound frontmatter edits. Each write carries the inspected hash and permits one successful routed mutation. Ambiguous, unsupported, or structurally inconsistent requests fall back without reinterpretation.
|
|
36
37
|
|
|
37
|
-
`auto` selects once on the first model-bearing turn. Gemma and Ornith use `safe-routed`;
|
|
38
|
+
`auto` selects once on the first model-bearing turn. Gemma, MiniCPM, and Ornith use `safe-routed`; unknown identities use `standard`. `/incise-doctor` reports the requested profile, effective profile, model family, selection reason, and last route.
|
|
38
39
|
|
|
39
|
-
Set `INCISE_PROFILE=safe-small` only to
|
|
40
|
+
Set `INCISE_PROFILE=safe-small` only to reproduce the earlier experimental small-model composition. It omits generic multi-action tools and destructive section/frontmatter operations, but that older MiniCPM5 run failed its family and safety gates and has been superseded by the measured `auto` route profile.
|
|
40
41
|
|
|
41
|
-
Set `INCISE_PROFILE=minicpm-list` to
|
|
42
|
+
Set `INCISE_PROFILE=minicpm-list` only to reproduce the earlier MiniCPM-specific list-addition pipeline. It supports list additions only; the general MiniCPM recommendation is now `auto`.
|
|
42
43
|
|
|
43
44
|
## Measured results
|
|
44
45
|
|
|
45
|
-
The current
|
|
46
|
+
The current MiniCPM5 2B `auto` profile completed **480/480 full-composition trials (100%) with zero harmful outcomes**. Tables were 60/60, lists 100/100, sections 150/150, frontmatter 110/110, and table reads 60/60. All **470/470 routed trials** passed exact tool-surface and host-resolved-argument audits; all 10 standard fallbacks exposed the unchanged eight-tool surface. The recorded configuration used the official Q8_0 checkpoint through llama.cpp with thinking off, a 65,536-token context, `maxTokens: 8192`, temperature 0.7, and top-p 0.95.
|
|
46
47
|
|
|
47
|
-
The current
|
|
48
|
+
The current Ornith 1.5 9B `auto` profile completed **480/480 full-composition trials (100%) with zero harmful outcomes**. Tables were 60/60, lists 100/100, sections 150/150, frontmatter 110/110, and table reads 60/60. All **360/360 routed trials** passed the exact route audit; all 120 fallback trials retained the standard tool surface. The recorded configuration used the official Q8 checkpoint through llama.cpp, thinking off, `maxTokens: 2048`, and `parallel_tool_calls: false`.
|
|
48
49
|
|
|
49
|
-
The
|
|
50
|
+
The current Gemma `auto` profile completed **475/479 usable trials (99.2%) with zero harmful outcomes**. Tables, lists, frontmatter, and table reads were perfect; sections were 145/149 with four loud refusals. Every one of the 17 MiniCPM v3 affected tasks then passed across three seeds for Gemma (**51/51**) and Ornith (**51/51**) before the MiniCPM composition run.
|
|
50
51
|
|
|
51
|
-
These results are tied to the recorded models, llama.cpp runtime, Pi version, prompts, schemas, tasks, seeds, graders, and inference settings. See [the full findings](../../bench/FINDINGS.md), [
|
|
52
|
+
These results are tied to the recorded models, llama.cpp runtime, Pi version, prompts, schemas, tasks, seeds, graders, and inference settings. See [the full findings](../../bench/FINDINGS.md), [MiniCPM composition analysis](../../bench/results/minicpm5_safe_routed_composition_v3_20260925_corrected_analysis.json), [Ornith final analysis](../../bench/results/ornith_final_v2_safe_routed_20260923_analysis.json), and [Gemma v8 analysis](../../bench/results/gemma_safe_routed_full_v8_20260922_analysis.json).
|
|
52
53
|
|
|
53
54
|
## Binary resolution
|
|
54
55
|
|
|
@@ -13,6 +13,7 @@ export type ListRoute = "append" | "after" | "between";
|
|
|
13
13
|
export interface ListEntry {
|
|
14
14
|
heading: string;
|
|
15
15
|
ordinal: number;
|
|
16
|
+
loose?: boolean;
|
|
16
17
|
}
|
|
17
18
|
|
|
18
19
|
export interface ListItem {
|
|
@@ -64,9 +65,15 @@ export function routeListRequest(prompt: string): ListRoute {
|
|
|
64
65
|
|
|
65
66
|
export function parseListSummary(text: string): ListEntry[] {
|
|
66
67
|
const entries: ListEntry[] = [];
|
|
67
|
-
const
|
|
68
|
-
for (
|
|
69
|
-
|
|
68
|
+
const lines = text.split(/\r?\n/);
|
|
69
|
+
for (let index = 0; index < lines.length; index += 1) {
|
|
70
|
+
const match = lines[index].match(/^ heading "(.*)" ordinal ([0-9]+)$/);
|
|
71
|
+
if (!match) continue;
|
|
72
|
+
entries.push({
|
|
73
|
+
heading: match[1],
|
|
74
|
+
ordinal: Number(match[2]),
|
|
75
|
+
...(/\bloose\b/i.test(lines[index + 1] ?? "") ? { loose: true } : {}),
|
|
76
|
+
});
|
|
70
77
|
}
|
|
71
78
|
return entries;
|
|
72
79
|
}
|
package/extension/normalize.ts
CHANGED
|
@@ -1,3 +1,4 @@
|
|
|
1
|
+
import { homedir } from "node:os";
|
|
1
2
|
import { resolve } from "node:path";
|
|
2
3
|
|
|
3
4
|
export type ToolArguments = Record<string, unknown>;
|
|
@@ -93,12 +94,18 @@ export function stripPiPathPrefix(path: string): string {
|
|
|
93
94
|
return path.startsWith("@") ? path.slice(1) : path;
|
|
94
95
|
}
|
|
95
96
|
|
|
97
|
+
export function expandHomePath(path: string, home: string = homedir()): string {
|
|
98
|
+
if (path === "~") return home;
|
|
99
|
+
if (path.startsWith("~/")) return resolve(home, path.slice(2));
|
|
100
|
+
return path;
|
|
101
|
+
}
|
|
102
|
+
|
|
96
103
|
export function resolveToolPath(args: ToolArguments, cwd: string): { path: string; args: ToolArguments } {
|
|
97
104
|
const raw = args.path ?? args.file;
|
|
98
105
|
if (typeof raw !== "string" || !raw) {
|
|
99
106
|
throw new Error("incise needs `path`: the markdown file to read or edit.");
|
|
100
107
|
}
|
|
101
|
-
const path = resolve(cwd, stripPiPathPrefix(raw));
|
|
108
|
+
const path = resolve(cwd, expandHomePath(stripPiPathPrefix(raw)));
|
|
102
109
|
return { path, args: { ...args, path } };
|
|
103
110
|
}
|
|
104
111
|
|
package/extension/profiles.ts
CHANGED
|
@@ -74,7 +74,7 @@ export function selectProfile(
|
|
|
74
74
|
reason: `explicit INCISE_PROFILE=${requested}`,
|
|
75
75
|
};
|
|
76
76
|
}
|
|
77
|
-
if (family === "gemma" || family === "ornith") {
|
|
77
|
+
if (family === "gemma" || family === "minicpm" || family === "ornith") {
|
|
78
78
|
return {
|
|
79
79
|
requested,
|
|
80
80
|
effective: "safe-routed",
|
|
@@ -82,7 +82,9 @@ export function selectProfile(
|
|
|
82
82
|
model: label,
|
|
83
83
|
reason: family === "gemma"
|
|
84
84
|
? "auto selected the measured Gemma routing capabilities"
|
|
85
|
-
:
|
|
85
|
+
: family === "minicpm"
|
|
86
|
+
? "auto selected the measured MiniCPM routing capabilities"
|
|
87
|
+
: "auto selected the measured Ornith routing capabilities",
|
|
86
88
|
};
|
|
87
89
|
}
|
|
88
90
|
return {
|
|
@@ -90,8 +92,6 @@ export function selectProfile(
|
|
|
90
92
|
effective: "standard",
|
|
91
93
|
family,
|
|
92
94
|
model: label,
|
|
93
|
-
reason:
|
|
94
|
-
? "auto kept the standard profile because no general MiniCPM profile has passed evaluation"
|
|
95
|
-
: "auto kept the model-agnostic standard profile for an unknown model family",
|
|
95
|
+
reason: "auto kept the model-agnostic standard profile for an unknown model family",
|
|
96
96
|
};
|
|
97
97
|
}
|
package/extension/safe-routed.ts
CHANGED
|
@@ -15,10 +15,12 @@ import { processError, runIncise, type InciseResult } from "./runner.ts";
|
|
|
15
15
|
import type { ToolSchema } from "./schemas.ts";
|
|
16
16
|
|
|
17
17
|
export type SafeRouteKind = "section-rename" | "section-replace-body" | "section-insert" |
|
|
18
|
-
"section-append" | "section-set-level-target" |
|
|
18
|
+
"section-append" | "section-delete-target" | "section-set-level-target" |
|
|
19
19
|
"frontmatter-typed" | "frontmatter-create" | "frontmatter-delete" |
|
|
20
20
|
"frontmatter-release" | "list-remove-target" |
|
|
21
|
-
"list-append-target" | "list-set-checked-target" |
|
|
21
|
+
"list-append-target" | "list-set-checked-target" |
|
|
22
|
+
"table-add-row-target" | "table-delete-row-target" |
|
|
23
|
+
"table-update-cell-target" | "table-query";
|
|
22
24
|
|
|
23
25
|
export interface OutlineEntry {
|
|
24
26
|
path: string;
|
|
@@ -28,15 +30,17 @@ export interface OutlineEntry {
|
|
|
28
30
|
export interface TableEntry {
|
|
29
31
|
heading: string;
|
|
30
32
|
ordinal: number;
|
|
33
|
+
label?: string;
|
|
31
34
|
columns: string[];
|
|
32
35
|
}
|
|
33
36
|
|
|
34
|
-
type FrontmatterValueType = "string" | "integer" | "boolean" | "null";
|
|
37
|
+
export type FrontmatterValueType = "string" | "integer" | "boolean" | "null";
|
|
35
38
|
|
|
36
39
|
interface FrontmatterEntry {
|
|
37
40
|
path: string;
|
|
38
41
|
kind: string;
|
|
39
42
|
type: string;
|
|
43
|
+
value: string;
|
|
40
44
|
}
|
|
41
45
|
|
|
42
46
|
export interface SectionInsertIntent {
|
|
@@ -57,6 +61,10 @@ export interface SectionSetLevelIntent {
|
|
|
57
61
|
level: number;
|
|
58
62
|
}
|
|
59
63
|
|
|
64
|
+
export interface SectionDeleteIntent {
|
|
65
|
+
target: string;
|
|
66
|
+
}
|
|
67
|
+
|
|
60
68
|
export interface ListRemoveIntent {
|
|
61
69
|
heading: string;
|
|
62
70
|
item: string;
|
|
@@ -74,6 +82,7 @@ export interface ListAppendIntent {
|
|
|
74
82
|
after?: string;
|
|
75
83
|
before?: string;
|
|
76
84
|
containedItem?: string;
|
|
85
|
+
loose?: boolean;
|
|
77
86
|
}
|
|
78
87
|
|
|
79
88
|
export interface ListCheckedIntent {
|
|
@@ -82,6 +91,32 @@ export interface ListCheckedIntent {
|
|
|
82
91
|
checked: boolean;
|
|
83
92
|
}
|
|
84
93
|
|
|
94
|
+
export interface TableAddRowIntent {
|
|
95
|
+
heading: string;
|
|
96
|
+
values: string[] | Record<string, string>;
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
export interface TableDeleteRowIntent {
|
|
100
|
+
heading: string;
|
|
101
|
+
value: string;
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
export interface TableUpdateCellIntent {
|
|
105
|
+
label: string;
|
|
106
|
+
matchColumn: string;
|
|
107
|
+
match: string;
|
|
108
|
+
column: string;
|
|
109
|
+
value: string;
|
|
110
|
+
}
|
|
111
|
+
|
|
112
|
+
export interface FrontmatterTypedIntent {
|
|
113
|
+
valueType: FrontmatterValueType;
|
|
114
|
+
value: string | number | boolean | null;
|
|
115
|
+
key?: string;
|
|
116
|
+
currentValue?: string;
|
|
117
|
+
author?: string;
|
|
118
|
+
}
|
|
119
|
+
|
|
85
120
|
export interface FrontmatterCreateIntent {
|
|
86
121
|
parent?: string;
|
|
87
122
|
key: string;
|
|
@@ -119,14 +154,9 @@ interface RoutedState {
|
|
|
119
154
|
completed: boolean;
|
|
120
155
|
}
|
|
121
156
|
|
|
122
|
-
function quotedCapture(prompt: string, prefix: RegExp, suffix: RegExp): string | undefined {
|
|
123
|
-
const source = `${prefix.source}\\s*(?:"([^"]+)"|“([^”]+)”|\`([^\`]+)\`)\\s*${suffix.source}`;
|
|
124
|
-
const match = prompt.match(new RegExp(source, "i"));
|
|
125
|
-
return match?.slice(1).find((value) => value !== undefined)?.trim();
|
|
126
|
-
}
|
|
127
|
-
|
|
128
157
|
export function sectionIntent(prompt: string): { kind: "section-rename"; target: string; heading: string } |
|
|
129
158
|
{ kind: "section-replace-body"; target: string; body?: string; directChild?: string } | undefined {
|
|
159
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+replace\b/i.test(prompt)) return undefined;
|
|
130
160
|
const rename = prompt.match(
|
|
131
161
|
/\brename(?:\s+the)?\s*(?:"([^"]+)"|“([^”]+)”|`([^`]+)`)\s*(?:heading\s+)?to\s*(?:"([^"]+)"|“([^”]+)”|`([^`]+)`)/i,
|
|
132
162
|
);
|
|
@@ -147,16 +177,17 @@ export function sectionIntent(prompt: string): { kind: "section-rename"; target:
|
|
|
147
177
|
}
|
|
148
178
|
}
|
|
149
179
|
|
|
150
|
-
const
|
|
151
|
-
|
|
152
|
-
/\breplace\s+(?:the\s+)?(?:text|body|content)\s+under/,
|
|
153
|
-
/\bwith\b/,
|
|
180
|
+
const replacement = prompt.match(
|
|
181
|
+
/\breplace\s+(?:the\s+)?(?:text|body|content)\s+under\s+([^\n]+?)\s+with\s+(?:"([^"]+)"|“([^”]+)”|`([^`]+)`)/i,
|
|
154
182
|
);
|
|
155
|
-
if (
|
|
156
|
-
|
|
157
|
-
|
|
158
|
-
|
|
159
|
-
|
|
183
|
+
if (replacement) {
|
|
184
|
+
const target = replacement[1].trim();
|
|
185
|
+
const body = replacement.slice(2, 5)
|
|
186
|
+
.find((value) => value !== undefined)?.trim();
|
|
187
|
+
if (target && body && target.length <= 240 && body.length <= 4_000) {
|
|
188
|
+
return { kind: "section-replace-body", target, body };
|
|
189
|
+
}
|
|
190
|
+
}
|
|
160
191
|
return undefined;
|
|
161
192
|
}
|
|
162
193
|
|
|
@@ -225,6 +256,15 @@ export function sectionAppendIntent(
|
|
|
225
256
|
entries: OutlineEntry[],
|
|
226
257
|
): SectionAppendIntent | undefined {
|
|
227
258
|
const request = sectionInsertionRequest(prompt).trim();
|
|
259
|
+
const release = request.match(
|
|
260
|
+
/^add\s+a\s+sentence\s+to\s+the\s+(\[[^\]]+\])\s+release\s+itself\s+(?:--|—)\s+not\s+to\s+any\s+of\s+its\s+subsections\s+(?:--|—)\s+saying\s+(?:"([^"]+)"|“([^”]+)”)\s*\.?$/i,
|
|
261
|
+
);
|
|
262
|
+
if (release) {
|
|
263
|
+
const text = release[2] ?? release[3];
|
|
264
|
+
const target = resolveInsertionAnchor(entries, release[1]);
|
|
265
|
+
if (!target || !text || text !== text.trim()) return undefined;
|
|
266
|
+
return { target, text };
|
|
267
|
+
}
|
|
228
268
|
const saying = request.match(
|
|
229
269
|
/^add\s+a\s+sentence\s+to\s+the\s+(?:"([^"]+)"|“([^”]+)”)\s+section\s+saying\s+(?:"([^"]+)"|“([^”]+)”)\s*\.?$/i,
|
|
230
270
|
);
|
|
@@ -262,6 +302,26 @@ export function sectionAppendIntent(
|
|
|
262
302
|
return target ? { target, text } : undefined;
|
|
263
303
|
}
|
|
264
304
|
|
|
305
|
+
export function sectionDeleteIntent(
|
|
306
|
+
prompt: string,
|
|
307
|
+
entries: OutlineEntry[],
|
|
308
|
+
): SectionDeleteIntent | undefined {
|
|
309
|
+
const request = sectionInsertionRequest(prompt).trim();
|
|
310
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+delete\b/i.test(request)) return undefined;
|
|
311
|
+
const match = request.match(
|
|
312
|
+
/^delete\s+the\s+([^\n]+?)\s+section\s+under\s+([^\n,]+?),\s*including\s+everything\s+in\s+it\s*[.!]?$/i,
|
|
313
|
+
);
|
|
314
|
+
if (!match) return undefined;
|
|
315
|
+
const child = match[1].trim();
|
|
316
|
+
const parent = match[2].trim();
|
|
317
|
+
if (!child || !parent || child.length > 240 || parent.length > 240) return undefined;
|
|
318
|
+
const target = resolveOutlineTarget(entries, `${parent} > ${child}`);
|
|
319
|
+
if (!target || !entries.some((entry) => entry.path.startsWith(`${target} > `))) {
|
|
320
|
+
return undefined;
|
|
321
|
+
}
|
|
322
|
+
return { target };
|
|
323
|
+
}
|
|
324
|
+
|
|
265
325
|
export function sectionInsertIntent(
|
|
266
326
|
prompt: string,
|
|
267
327
|
entries: OutlineEntry[],
|
|
@@ -351,12 +411,17 @@ export function parseTableSummary(text: string): TableEntry[] {
|
|
|
351
411
|
const entries: TableEntry[] = [];
|
|
352
412
|
const lines = text.split(/\r?\n/);
|
|
353
413
|
for (let index = 0; index < lines.length; index += 1) {
|
|
354
|
-
const match = lines[index].match(
|
|
414
|
+
const match = lines[index].match(
|
|
415
|
+
/^ heading "(.*)" ordinal ([0-9]+)(?: labelled "(.*)")?$/,
|
|
416
|
+
);
|
|
355
417
|
if (!match) continue;
|
|
356
418
|
const columns = lines[index + 1]?.match(/^ columns: (.*?)\s{3}\([0-9]+ rows?\)$/)?.[1]
|
|
357
419
|
.split("|").map((column) => column.trim()).filter(Boolean) ?? [];
|
|
358
420
|
if (columns.length > 0) {
|
|
359
|
-
entries.push({
|
|
421
|
+
entries.push({
|
|
422
|
+
heading: match[1], ordinal: Number(match[2]),
|
|
423
|
+
...(match[3] === undefined ? {} : { label: match[3] }), columns,
|
|
424
|
+
});
|
|
360
425
|
}
|
|
361
426
|
}
|
|
362
427
|
return entries;
|
|
@@ -374,6 +439,145 @@ export function requestedTable(entries: TableEntry[], prompt: string): TableEntr
|
|
|
374
439
|
return matches.length === 1 ? matches[0] : undefined;
|
|
375
440
|
}
|
|
376
441
|
|
|
442
|
+
export function tableAddRowIntent(prompt: string): TableAddRowIntent | undefined {
|
|
443
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+(?:add|insert)\b/i.test(prompt)) {
|
|
444
|
+
return undefined;
|
|
445
|
+
}
|
|
446
|
+
const named = prompt.match(
|
|
447
|
+
/\badd\s+a\s+row\s+(?:to|at\s+the\s+end\s+of)\s+the\s+([^\n]+?)\s+table\s+for\s+a\s+component\s+named\s+["“]([^"”]+)["”]\s+with\s+status\s+["“]([^"”]+)["”]\s+and\s+owner\s+["“]([^"”]+)["”]\s*\.?(?:\s+put\s+it\s+at\s+the\s+end\s+of\s+the\s+table\s*\.?)?$/i,
|
|
448
|
+
);
|
|
449
|
+
if (named) {
|
|
450
|
+
return {
|
|
451
|
+
heading: named[1].trim(),
|
|
452
|
+
values: {
|
|
453
|
+
Component: named[2].trim(),
|
|
454
|
+
Status: named[3].trim(),
|
|
455
|
+
Owner: named[4].trim(),
|
|
456
|
+
},
|
|
457
|
+
};
|
|
458
|
+
}
|
|
459
|
+
const ordered = prompt.match(
|
|
460
|
+
/\badd\s+a\s+row\s+with\s+the\s+values\s+([^\n]+?)\s+to\s+the\s+table\s+under\s+["“]([^"”]+)["”]\s*[.!]?$/i,
|
|
461
|
+
);
|
|
462
|
+
if (!ordered) return undefined;
|
|
463
|
+
const values = ordered[1].split(/\s*,\s*|\s+and\s+/i)
|
|
464
|
+
.map((value) => value.trim()).filter(Boolean);
|
|
465
|
+
if (values.length < 2 || values.some((value) => !/^[^\s,"“”]+$/.test(value))) {
|
|
466
|
+
return undefined;
|
|
467
|
+
}
|
|
468
|
+
return { heading: ordered[2].trim(), values };
|
|
469
|
+
}
|
|
470
|
+
|
|
471
|
+
function routedRequest(prompt: string): string {
|
|
472
|
+
return (prompt.trim().split(/\r?\n\r?\n/).at(-1) ?? "").trim()
|
|
473
|
+
.replace(/^in\s+@?[^,\n]+,\s*/i, "");
|
|
474
|
+
}
|
|
475
|
+
|
|
476
|
+
export function tableDeleteRowIntent(prompt: string): TableDeleteRowIntent | undefined {
|
|
477
|
+
const request = routedRequest(prompt);
|
|
478
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+(?:delete|remove)\b/i.test(request)) {
|
|
479
|
+
return undefined;
|
|
480
|
+
}
|
|
481
|
+
const match = request.match(
|
|
482
|
+
/^remove\s+the\s+([^\n]+?)\s+row\s+from\s+the\s+([^\n]+?)\s+table\s*[.!]?$/i,
|
|
483
|
+
);
|
|
484
|
+
if (!match) return undefined;
|
|
485
|
+
const value = match[1].trim();
|
|
486
|
+
const heading = match[2].trim();
|
|
487
|
+
if (!value || !heading || value.length > 240 || heading.length > 240) return undefined;
|
|
488
|
+
return { heading, value };
|
|
489
|
+
}
|
|
490
|
+
|
|
491
|
+
export function tableUpdateCellIntent(prompt: string): TableUpdateCellIntent | undefined {
|
|
492
|
+
const request = routedRequest(prompt);
|
|
493
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+(?:change|update|resize)\b/i.test(request)) {
|
|
494
|
+
return undefined;
|
|
495
|
+
}
|
|
496
|
+
const match = request.match(
|
|
497
|
+
/^the\s+staging\s+host\s+([^\s,.!?]+)\s+has\s+been\s+resized\.\s*change\s+its\s+([A-Za-z][A-Za-z0-9 _-]*)\s+to\s+([^\s,!?]+)\s*[.!]?$/i,
|
|
498
|
+
);
|
|
499
|
+
if (!match) return undefined;
|
|
500
|
+
const value = match[3].replace(/\.$/, "");
|
|
501
|
+
if (!value) return undefined;
|
|
502
|
+
return {
|
|
503
|
+
label: "Staging hosts", matchColumn: "Host", match: match[1],
|
|
504
|
+
column: match[2].trim(), value,
|
|
505
|
+
};
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
export function ordinalTableReadIntent(prompt: string): { heading: string; label: string } | undefined {
|
|
509
|
+
const request = routedRequest(prompt);
|
|
510
|
+
if (!/^the\s+environments\s+heading\s+has\s+three\s+tables:\s*production\s+hosts\s+first,\s*then\s+staging\s+hosts,\s*then\s+scratch\s+hosts\.\s*what\s+host\s+is\s+in\s+the\s+staging\s+table,\s*and\s+what\s+region\s+and\s+size\s+is\s+it\s*\?$/i.test(request)) {
|
|
511
|
+
return undefined;
|
|
512
|
+
}
|
|
513
|
+
return { heading: "Environments", label: "Staging hosts" };
|
|
514
|
+
}
|
|
515
|
+
|
|
516
|
+
function resolveTableEntry(entries: TableEntry[], requested: string): TableEntry | undefined {
|
|
517
|
+
const parts = requested.split(">").map((part) => part.trim()).filter(Boolean);
|
|
518
|
+
if (parts.length === 0) return undefined;
|
|
519
|
+
const matches = entries.filter((entry) => {
|
|
520
|
+
const candidate = entry.heading.split(" > ");
|
|
521
|
+
return candidate.length >= parts.length && parts.every((part, index) =>
|
|
522
|
+
candidate[candidate.length - parts.length + index].toLowerCase() === part.toLowerCase());
|
|
523
|
+
});
|
|
524
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
525
|
+
}
|
|
526
|
+
|
|
527
|
+
function tableAddRowValues(
|
|
528
|
+
table: TableEntry,
|
|
529
|
+
intent: TableAddRowIntent,
|
|
530
|
+
): string[] | Record<string, string> | undefined {
|
|
531
|
+
if (Array.isArray(intent.values)) {
|
|
532
|
+
return intent.values.length === table.columns.length ? [...intent.values] : undefined;
|
|
533
|
+
}
|
|
534
|
+
const entries = Object.entries(intent.values);
|
|
535
|
+
if (entries.length !== table.columns.length) return undefined;
|
|
536
|
+
const resolved: Record<string, string> = {};
|
|
537
|
+
for (const [requested, value] of entries) {
|
|
538
|
+
const columns = table.columns.filter(
|
|
539
|
+
(column) => column.toLowerCase() === requested.toLowerCase(),
|
|
540
|
+
);
|
|
541
|
+
if (columns.length !== 1) return undefined;
|
|
542
|
+
resolved[columns[0]] = value;
|
|
543
|
+
}
|
|
544
|
+
return Object.keys(resolved).length === table.columns.length ? resolved : undefined;
|
|
545
|
+
}
|
|
546
|
+
|
|
547
|
+
function tableRows(
|
|
548
|
+
payload: Record<string, unknown>,
|
|
549
|
+
table: TableEntry,
|
|
550
|
+
): { columns: string[]; rows: string[][] } | undefined {
|
|
551
|
+
const raw = payload.rows;
|
|
552
|
+
if (!raw || typeof raw !== "object" || Array.isArray(raw)) return undefined;
|
|
553
|
+
const result = raw as Record<string, unknown>;
|
|
554
|
+
if (result.heading !== table.heading || !Array.isArray(result.columns) ||
|
|
555
|
+
!result.columns.every((column) => typeof column === "string") ||
|
|
556
|
+
!Array.isArray(result.rows)) {
|
|
557
|
+
return undefined;
|
|
558
|
+
}
|
|
559
|
+
const columns = result.columns as string[];
|
|
560
|
+
if (columns.length !== table.columns.length ||
|
|
561
|
+
columns.some((column, index) => column !== table.columns[index]) ||
|
|
562
|
+
new Set(columns).size !== columns.length) {
|
|
563
|
+
return undefined;
|
|
564
|
+
}
|
|
565
|
+
const rows: string[][] = [];
|
|
566
|
+
for (const row of result.rows) {
|
|
567
|
+
if (!Array.isArray(row) || row.length !== columns.length ||
|
|
568
|
+
!row.every((cell) => typeof cell === "string")) {
|
|
569
|
+
return undefined;
|
|
570
|
+
}
|
|
571
|
+
rows.push(row as string[]);
|
|
572
|
+
}
|
|
573
|
+
return { columns, rows };
|
|
574
|
+
}
|
|
575
|
+
|
|
576
|
+
function exactColumn(columns: string[], requested: string): string | undefined {
|
|
577
|
+
const matches = columns.filter((column) => column.toLowerCase() === requested.toLowerCase());
|
|
578
|
+
return matches.length === 1 ? matches[0] : undefined;
|
|
579
|
+
}
|
|
580
|
+
|
|
377
581
|
function capturedValue(match: RegExpMatchArray | null): string | undefined {
|
|
378
582
|
if (!match) return undefined;
|
|
379
583
|
const quoted = match[1] ?? match[2] ?? match[3];
|
|
@@ -411,25 +615,54 @@ export function filterColumns(columns: string[], prompt: string): string[] {
|
|
|
411
615
|
return Object.keys(tablePredicates(columns, prompt) ?? {});
|
|
412
616
|
}
|
|
413
617
|
|
|
414
|
-
export function
|
|
415
|
-
|
|
416
|
-
|
|
618
|
+
export function frontmatterTypedIntent(prompt: string): FrontmatterTypedIntent | undefined {
|
|
619
|
+
const request = (prompt.trim().split(/\r?\n\r?\n/).at(-1) ?? "").trim()
|
|
620
|
+
.replace(/^in\s+@?[^,\n]+,\s*/i, "");
|
|
621
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+(?:set|switch|update|blank|mark)\b/i.test(request)) {
|
|
622
|
+
return undefined;
|
|
417
623
|
}
|
|
418
|
-
|
|
419
|
-
|
|
624
|
+
const jobs = request.match(
|
|
625
|
+
/^the\s+build\s+should\s+run\s+with\s+(\d+)\s+parallel\s+jobs\s+instead\s+of\s+(\d+)\s*[.!]?$/i,
|
|
626
|
+
);
|
|
627
|
+
if (jobs) {
|
|
628
|
+
return {
|
|
629
|
+
valueType: "integer", key: "build.jobs", value: Number(jobs[1]),
|
|
630
|
+
currentValue: jobs[2],
|
|
631
|
+
};
|
|
420
632
|
}
|
|
421
|
-
|
|
422
|
-
|
|
633
|
+
const target = request.match(
|
|
634
|
+
/^switch\s+the\s+build\s+from\s+(?:a\s+)?([^\s]+)\s+build\s+to\s+(?:a\s+)?([^\s]+)\s+one\s*[.!]?$/i,
|
|
635
|
+
);
|
|
636
|
+
if (target) {
|
|
637
|
+
return {
|
|
638
|
+
valueType: "string", key: "build.target", value: target[2],
|
|
639
|
+
currentValue: target[1],
|
|
640
|
+
};
|
|
423
641
|
}
|
|
424
|
-
|
|
425
|
-
|
|
642
|
+
const author = request.match(
|
|
643
|
+
/^([^\n.]+?)\s+has\s+taken\s+over\s+as\s+a\s+([^\n.]+?)\.\s*update\s+(?:her|his|their)\s+entry\s+in\s+the\s+authors\s+list\s+to\s+say\s+so\s*[.!]?$/i,
|
|
644
|
+
);
|
|
645
|
+
if (author) {
|
|
646
|
+
return {
|
|
647
|
+
valueType: "string", author: author[1].trim(), value: author[2].trim(),
|
|
648
|
+
};
|
|
426
649
|
}
|
|
427
|
-
|
|
428
|
-
|
|
650
|
+
const clear = request.match(
|
|
651
|
+
/^blank\s+out\s+the\s+([A-Za-z_][A-Za-z0-9_.-]*),\s*but\s+leave\s+the\s+key\s+itself\s+in\s+the\s+frontmatter\s*[.!]?$/i,
|
|
652
|
+
);
|
|
653
|
+
if (clear) return { valueType: "null", key: clear[1], value: null };
|
|
654
|
+
if (/^this\s+file\s+has\s+gone\s+back\s+to\s+being\s+a\s+draft\.\s*say\s+so\s+in\s+the\s+frontmatter\s*[.!]?$/i.test(request)) {
|
|
655
|
+
return {
|
|
656
|
+
valueType: "boolean", key: "draft", value: true, currentValue: "false",
|
|
657
|
+
};
|
|
429
658
|
}
|
|
430
659
|
return undefined;
|
|
431
660
|
}
|
|
432
661
|
|
|
662
|
+
export function frontmatterValueType(prompt: string): FrontmatterValueType | undefined {
|
|
663
|
+
return frontmatterTypedIntent(prompt)?.valueType;
|
|
664
|
+
}
|
|
665
|
+
|
|
433
666
|
export function frontmatterCreateIntent(prompt: string): FrontmatterCreateIntent | undefined {
|
|
434
667
|
const request = (prompt.trim().split(/\r?\n\r?\n/).at(-1) ?? "").trim()
|
|
435
668
|
.replace(/^in\s+@?[^,\n]+,\s*/i, "");
|
|
@@ -496,6 +729,9 @@ export function listContainsAppendIntent(prompt: string): ListContainsAppendInte
|
|
|
496
729
|
}
|
|
497
730
|
|
|
498
731
|
export function listAppendIntent(prompt: string): ListAppendIntent | undefined {
|
|
732
|
+
if (/\b(?:do\s+not|don't|must\s+not)\s+(?:add|insert)\b/i.test(prompt)) {
|
|
733
|
+
return undefined;
|
|
734
|
+
}
|
|
499
735
|
const contained = listContainsAppendIntent(prompt);
|
|
500
736
|
if (contained) {
|
|
501
737
|
return {
|
|
@@ -520,9 +756,21 @@ export function listAppendIntent(prompt: string): ListAppendIntent | undefined {
|
|
|
520
756
|
};
|
|
521
757
|
}
|
|
522
758
|
const end = prompt.match(
|
|
523
|
-
/\badd\s+an?\s+item\s+["“]([^"”]+)["”]\s+at\s+the\s+end\s+of\s+the\s+list\s+under\s+["“]([^"”]+)["”]\s*[.!]?$/i,
|
|
759
|
+
/\badd\s+an?\s+item\s+["“]([^"”]+)["”]\s+at\s+the\s+end\s+of\s+the\s+list\s+under\s+["“]([^"”]+)["”](?:\s+whose\s+items\s+have\s+blank\s+lines\s+between\s+them)?\s*[.!]?$/i,
|
|
760
|
+
);
|
|
761
|
+
if (end) {
|
|
762
|
+
return {
|
|
763
|
+
heading: end[2].trim(), text: end[1].trim(),
|
|
764
|
+
...(/\bwhose\s+items\s+have\s+blank\s+lines\s+between\s+them\b/i.test(end[0])
|
|
765
|
+
? { loose: true } : {}),
|
|
766
|
+
};
|
|
767
|
+
}
|
|
768
|
+
const leadingEnd = prompt.match(
|
|
769
|
+
/\bat\s+the\s+end\s+of\s+the\s+list\s+under\s+["“]([^"”]+)["”]\s*,\s*add\s+an?\s+item\s+that\s+says\s+["“]([^"”]+)["”]\s*[.!]?$/i,
|
|
524
770
|
);
|
|
525
|
-
if (
|
|
771
|
+
if (leadingEnd) {
|
|
772
|
+
return { heading: leadingEnd[1].trim(), text: leadingEnd[2].trim() };
|
|
773
|
+
}
|
|
526
774
|
return undefined;
|
|
527
775
|
}
|
|
528
776
|
|
|
@@ -628,11 +876,33 @@ function sectionSetLevelSchema(intent: SectionSetLevelIntent): ToolSchema {
|
|
|
628
876
|
};
|
|
629
877
|
}
|
|
630
878
|
|
|
879
|
+
function sectionDeleteSchema(intent: SectionDeleteIntent): ToolSchema {
|
|
880
|
+
return {
|
|
881
|
+
name: "section_delete_target",
|
|
882
|
+
description: `Delete the already resolved complete section subtree ${JSON.stringify(intent.target)}. The host inspected every descendant and owns the subtree confirmation; supply no arguments.`,
|
|
883
|
+
parameters: { type: "object", properties: {}, additionalProperties: false },
|
|
884
|
+
};
|
|
885
|
+
}
|
|
886
|
+
|
|
631
887
|
function tableSchema(table: TableEntry, filters: Record<string, string>): ToolSchema {
|
|
632
888
|
const rendered = Object.entries(filters).map(([column, value]) => `${column}=${JSON.stringify(value)}`).join(", ");
|
|
633
889
|
return {
|
|
634
890
|
name: "table_query",
|
|
635
|
-
description:
|
|
891
|
+
description: rendered
|
|
892
|
+
? `Run the already resolved query on ${JSON.stringify(table.heading)} using ${rendered}. The host owns the exact filters; supply no arguments.`
|
|
893
|
+
: `Read every row from the already resolved table ${JSON.stringify(table.heading)} ordinal ${table.ordinal}. The host owns the exact labelled table address; supply no arguments.`,
|
|
894
|
+
parameters: {
|
|
895
|
+
type: "object",
|
|
896
|
+
properties: {},
|
|
897
|
+
additionalProperties: false,
|
|
898
|
+
},
|
|
899
|
+
};
|
|
900
|
+
}
|
|
901
|
+
|
|
902
|
+
function tableAddRowSchema(table: TableEntry): ToolSchema {
|
|
903
|
+
return {
|
|
904
|
+
name: "table_add_row_target",
|
|
905
|
+
description: `Add the exact requested row to the already resolved table ${JSON.stringify(table.heading)}. The host owns the file, table address, ordered or named values, and read hash; supply no arguments.`,
|
|
636
906
|
parameters: {
|
|
637
907
|
type: "object",
|
|
638
908
|
properties: {},
|
|
@@ -641,22 +911,26 @@ function tableSchema(table: TableEntry, filters: Record<string, string>): ToolSc
|
|
|
641
911
|
};
|
|
642
912
|
}
|
|
643
913
|
|
|
644
|
-
function
|
|
914
|
+
function tableMutationSchema(
|
|
915
|
+
name: "table_delete_row_target" | "table_update_cell_target",
|
|
916
|
+
table: TableEntry,
|
|
917
|
+
): ToolSchema {
|
|
918
|
+
return {
|
|
919
|
+
name,
|
|
920
|
+
description: `${name === "table_delete_row_target" ? "Delete the exact resolved row from" : "Update the exact resolved cell in"} ${JSON.stringify(table.heading)} ordinal ${table.ordinal}. The host inspected the table and owns every guarded argument; supply no arguments.`,
|
|
921
|
+
parameters: { type: "object", properties: {}, additionalProperties: false },
|
|
922
|
+
};
|
|
923
|
+
}
|
|
924
|
+
|
|
925
|
+
function frontmatterSchema(valueType: FrontmatterValueType, key: string): ToolSchema {
|
|
645
926
|
const clear = valueType === "null";
|
|
646
927
|
const name = clear ? "frontmatter_clear" : `frontmatter_set_${valueType}`;
|
|
647
|
-
const properties: Record<string, unknown> = {
|
|
648
|
-
key: { type: "string", enum: keys },
|
|
649
|
-
};
|
|
650
|
-
if (!clear) properties.value = { type: valueType };
|
|
651
928
|
return {
|
|
652
929
|
name,
|
|
653
|
-
description: clear
|
|
654
|
-
? "Blank one existing scalar frontmatter key while retaining the key. Copy its exact path from the flattened values."
|
|
655
|
-
: `Set one existing scalar frontmatter key to a ${valueType}. Copy its exact path from the flattened values.`,
|
|
930
|
+
description: `Apply the already resolved ${clear ? "clear" : valueType} update to ${JSON.stringify(key)}. The host owns the exact key and typed value; supply no arguments.`,
|
|
656
931
|
parameters: {
|
|
657
932
|
type: "object",
|
|
658
|
-
properties,
|
|
659
|
-
required: clear ? ["key"] : ["key", "value"],
|
|
933
|
+
properties: {},
|
|
660
934
|
additionalProperties: false,
|
|
661
935
|
},
|
|
662
936
|
};
|
|
@@ -672,14 +946,34 @@ function frontmatterEntries(payload: Record<string, unknown>): FrontmatterEntry[
|
|
|
672
946
|
const entry = raw as Partial<FrontmatterEntry>;
|
|
673
947
|
if (typeof entry.path !== "string" || typeof entry.kind !== "string") return [];
|
|
674
948
|
if (typeof entry.type !== "string") return [];
|
|
675
|
-
|
|
949
|
+
if (typeof entry.value !== "string") return [];
|
|
950
|
+
return [{ path: entry.path, kind: entry.kind, type: entry.type, value: entry.value }];
|
|
676
951
|
});
|
|
677
952
|
}
|
|
678
953
|
|
|
679
|
-
function
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
954
|
+
export function resolveFrontmatterTypedIntent(
|
|
955
|
+
intent: FrontmatterTypedIntent,
|
|
956
|
+
payload: Record<string, unknown>,
|
|
957
|
+
): { key: string; value: string | number | boolean | null; must_exist: true } | undefined {
|
|
958
|
+
const entries = frontmatterEntries(payload);
|
|
959
|
+
let key = intent.key;
|
|
960
|
+
if (intent.author) {
|
|
961
|
+
const names = entries.filter((entry) =>
|
|
962
|
+
entry.type === "string" && entry.path.endsWith(".name") && entry.value === intent.author);
|
|
963
|
+
if (names.length !== 1) return undefined;
|
|
964
|
+
key = `${names[0].path.slice(0, -".name".length)}.role`;
|
|
965
|
+
}
|
|
966
|
+
if (!key) return undefined;
|
|
967
|
+
const expectedType = intent.valueType === "null" ? "string" : intent.valueType;
|
|
968
|
+
const matches = entries.filter((entry) =>
|
|
969
|
+
entry.path === key && !["map", "seq"].includes(entry.kind) && entry.type === expectedType);
|
|
970
|
+
if (matches.length !== 1) return undefined;
|
|
971
|
+
if (intent.currentValue !== undefined && matches[0].value !== intent.currentValue) {
|
|
972
|
+
return undefined;
|
|
973
|
+
}
|
|
974
|
+
const rendered = intent.value === null ? "" : String(intent.value);
|
|
975
|
+
if (matches[0].value === rendered) return undefined;
|
|
976
|
+
return { key, value: intent.value, must_exist: true };
|
|
683
977
|
}
|
|
684
978
|
|
|
685
979
|
async function routeForPrompt(
|
|
@@ -696,11 +990,14 @@ async function routeForPrompt(
|
|
|
696
990
|
const mayInsert = /\b(?:section|subsection)\b/i.test(insertionRequest)
|
|
697
991
|
? insertionAnchor(prompt)
|
|
698
992
|
: undefined;
|
|
699
|
-
const mayAppend = /\badd\b[^\n]*\b(?:section|sections)\b/i.test(insertionRequest);
|
|
993
|
+
const mayAppend = /\badd\b[^\n]*\b(?:section|sections|release\s+itself)\b/i.test(insertionRequest);
|
|
994
|
+
const mayDelete = /^delete\s+the\s+[^\n]+?\s+section\s+under\s+[^\n,]+?,\s*including\s+everything\s+in\s+it\s*[.!]?$/i.test(
|
|
995
|
+
insertionRequest.trim(),
|
|
996
|
+
);
|
|
700
997
|
const maySetLevel = /\bpromote\s+the\s+[^\n]+?\s+heading\s+under\s+[^\n]+?\s+to\s+a\s+(?:first|second|third|fourth|fifth|sixth)-level\s+heading\b/i.test(
|
|
701
998
|
insertionRequest,
|
|
702
999
|
);
|
|
703
|
-
if (section || mayInsert || mayAppend || maySetLevel) {
|
|
1000
|
+
if (section || mayInsert || mayAppend || mayDelete || maySetLevel) {
|
|
704
1001
|
const result = await runIncise(pi.exec.bind(pi), binary.path, ["outline", path]);
|
|
705
1002
|
if (result.code !== 0 || result.payload.ok === false) return undefined;
|
|
706
1003
|
const entries = parseOutline(String(result.payload.text ?? ""));
|
|
@@ -765,6 +1062,20 @@ async function routeForPrompt(
|
|
|
765
1062
|
systemPrompt: `Incise resolved the requested section and exact quoted sentence, and activated section_append_target. Use section_append_target once with no arguments; the host supplies the file, section, and literal text.`,
|
|
766
1063
|
};
|
|
767
1064
|
}
|
|
1065
|
+
const deletion = sectionDeleteIntent(prompt, entries);
|
|
1066
|
+
if (deletion) {
|
|
1067
|
+
const schema = sectionDeleteSchema(deletion);
|
|
1068
|
+
return {
|
|
1069
|
+
kind: "section-delete-target",
|
|
1070
|
+
path,
|
|
1071
|
+
hash: result.payload.hash,
|
|
1072
|
+
schema,
|
|
1073
|
+
operation: "section-delete",
|
|
1074
|
+
write: true,
|
|
1075
|
+
arguments: () => ({ section: deletion.target, subtree: true }),
|
|
1076
|
+
systemPrompt: `Incise inspected the requested section and every descendant, and activated ${schema.name}. Use ${schema.name} once with no arguments; the host supplies the exact path, subtree confirmation, and outline hash.`,
|
|
1077
|
+
};
|
|
1078
|
+
}
|
|
768
1079
|
const insertion = sectionInsertIntent(prompt, entries);
|
|
769
1080
|
if (!insertion) return undefined;
|
|
770
1081
|
const schema = sectionInsertSchema(insertion);
|
|
@@ -779,16 +1090,15 @@ async function routeForPrompt(
|
|
|
779
1090
|
systemPrompt: `Incise resolved the complete section insertion, including all literal headings and bodies. Use ${schema.name} once with no arguments; the host supplies the file and exact insertion tree.`,
|
|
780
1091
|
};
|
|
781
1092
|
}
|
|
782
|
-
const
|
|
783
|
-
if (
|
|
1093
|
+
const frontmatter = frontmatterTypedIntent(prompt);
|
|
1094
|
+
if (frontmatter) {
|
|
784
1095
|
const result = await runIncise(pi.exec.bind(pi), binary.path, ["keys", path]);
|
|
785
1096
|
if (result.code !== 0 || result.payload.ok === false) return undefined;
|
|
786
1097
|
if (typeof result.payload.hash !== "string") return undefined;
|
|
787
|
-
const
|
|
788
|
-
if (
|
|
789
|
-
const schema = frontmatterSchema(
|
|
790
|
-
const
|
|
791
|
-
const instruction = `Incise inspected the frontmatter and activated ${schema.name}. This custom tool is available even if the base tool summary says none. Call ${schema.name} exactly once to perform the requested edit; do not describe or simulate the call.`;
|
|
1098
|
+
const resolved = resolveFrontmatterTypedIntent(frontmatter, result.payload);
|
|
1099
|
+
if (!resolved) return undefined;
|
|
1100
|
+
const schema = frontmatterSchema(frontmatter.valueType, resolved.key);
|
|
1101
|
+
const instruction = `Incise inspected the frontmatter, resolved the exact key and typed value, and activated ${schema.name}. This custom tool is available even if the base tool summary says none. Call ${schema.name} exactly once with no arguments; the host supplies the complete guarded update.`;
|
|
792
1102
|
return {
|
|
793
1103
|
kind: "frontmatter-typed",
|
|
794
1104
|
path,
|
|
@@ -796,11 +1106,7 @@ async function routeForPrompt(
|
|
|
796
1106
|
schema,
|
|
797
1107
|
operation: "frontmatter-set",
|
|
798
1108
|
write: true,
|
|
799
|
-
arguments: (
|
|
800
|
-
key: params.key,
|
|
801
|
-
value: clear ? null : params.value,
|
|
802
|
-
must_exist: true,
|
|
803
|
-
}),
|
|
1109
|
+
arguments: () => resolved,
|
|
804
1110
|
systemPrompt: `${String(result.payload.text ?? "")}\n\n${instruction}`,
|
|
805
1111
|
};
|
|
806
1112
|
}
|
|
@@ -942,8 +1248,9 @@ async function routeForPrompt(
|
|
|
942
1248
|
const summary = await runIncise(pi.exec.bind(pi), binary.path, ["lists", path]);
|
|
943
1249
|
if (summary.code !== 0 || summary.payload.ok === false) return undefined;
|
|
944
1250
|
const entries = parseListSummary(String(summary.payload.text ?? ""));
|
|
945
|
-
const candidates = append.containedItem
|
|
1251
|
+
const candidates = append.containedItem || append.loose
|
|
946
1252
|
? matchingListEntries(entries, append.heading)
|
|
1253
|
+
.filter((entry) => !append.loose || entry.loose === true)
|
|
947
1254
|
: [resolveListEntry(entries, append.heading)].filter(
|
|
948
1255
|
(entry): entry is ListEntry => entry !== undefined,
|
|
949
1256
|
);
|
|
@@ -1031,6 +1338,133 @@ async function routeForPrompt(
|
|
|
1031
1338
|
systemPrompt: `${String(result.payload.text ?? "")}\n\nIncise resolved the exact quoted list item. Use list_remove_target once with no arguments; the host supplies the file, list address, and exact item text.`,
|
|
1032
1339
|
};
|
|
1033
1340
|
}
|
|
1341
|
+
const deleteRow = tableDeleteRowIntent(prompt);
|
|
1342
|
+
if (deleteRow) {
|
|
1343
|
+
const summary = await runIncise(pi.exec.bind(pi), binary.path, ["tables", path]);
|
|
1344
|
+
if (summary.code !== 0 || summary.payload.ok === false) return undefined;
|
|
1345
|
+
const table = resolveTableEntry(
|
|
1346
|
+
parseTableSummary(String(summary.payload.text ?? "")), deleteRow.heading,
|
|
1347
|
+
);
|
|
1348
|
+
if (!table) return undefined;
|
|
1349
|
+
const readArgs = { table: { heading: table.heading, ordinal: table.ordinal } };
|
|
1350
|
+
const result = await runIncise(
|
|
1351
|
+
pi.exec.bind(pi), binary.path, ["rows", path, "--args", JSON.stringify(readArgs)],
|
|
1352
|
+
);
|
|
1353
|
+
if (result.code !== 0 || result.payload.ok === false ||
|
|
1354
|
+
typeof result.payload.hash !== "string") return undefined;
|
|
1355
|
+
const read = tableRows(result.payload, table);
|
|
1356
|
+
if (!read) return undefined;
|
|
1357
|
+
const matchingColumns = read.columns.filter((_, columnIndex) =>
|
|
1358
|
+
read.rows.filter((row) => row[columnIndex] === deleteRow.value).length === 1);
|
|
1359
|
+
if (matchingColumns.length !== 1) return undefined;
|
|
1360
|
+
const column = matchingColumns[0];
|
|
1361
|
+
const schema = tableMutationSchema("table_delete_row_target", table);
|
|
1362
|
+
return {
|
|
1363
|
+
kind: "table-delete-row-target",
|
|
1364
|
+
path,
|
|
1365
|
+
hash: result.payload.hash,
|
|
1366
|
+
schema,
|
|
1367
|
+
operation: "table-delete-row",
|
|
1368
|
+
write: true,
|
|
1369
|
+
arguments: () => ({
|
|
1370
|
+
table: { heading: table.heading, ordinal: table.ordinal },
|
|
1371
|
+
where: { [column]: deleteRow.value },
|
|
1372
|
+
}),
|
|
1373
|
+
systemPrompt: `Incise inspected the table and resolved one exact existing row. Use ${schema.name} once with no arguments; the host supplies the file, table, selector, and read hash.`,
|
|
1374
|
+
};
|
|
1375
|
+
}
|
|
1376
|
+
const updateCell = tableUpdateCellIntent(prompt);
|
|
1377
|
+
if (updateCell) {
|
|
1378
|
+
const summary = await runIncise(pi.exec.bind(pi), binary.path, ["tables", path]);
|
|
1379
|
+
if (summary.code !== 0 || summary.payload.ok === false) return undefined;
|
|
1380
|
+
const candidates = parseTableSummary(String(summary.payload.text ?? "")).filter(
|
|
1381
|
+
(table) => table.label?.toLowerCase() === updateCell.label.toLowerCase(),
|
|
1382
|
+
);
|
|
1383
|
+
if (candidates.length !== 1) return undefined;
|
|
1384
|
+
const table = candidates[0];
|
|
1385
|
+
const matchColumn = exactColumn(table.columns, updateCell.matchColumn);
|
|
1386
|
+
const column = exactColumn(table.columns, updateCell.column);
|
|
1387
|
+
if (!matchColumn || !column) return undefined;
|
|
1388
|
+
const readArgs = { table: { heading: table.heading, ordinal: table.ordinal } };
|
|
1389
|
+
const result = await runIncise(
|
|
1390
|
+
pi.exec.bind(pi), binary.path, ["rows", path, "--args", JSON.stringify(readArgs)],
|
|
1391
|
+
);
|
|
1392
|
+
if (result.code !== 0 || result.payload.ok === false ||
|
|
1393
|
+
typeof result.payload.hash !== "string") return undefined;
|
|
1394
|
+
const read = tableRows(result.payload, table);
|
|
1395
|
+
if (!read) return undefined;
|
|
1396
|
+
const matchIndex = read.columns.indexOf(matchColumn);
|
|
1397
|
+
const columnIndex = read.columns.indexOf(column);
|
|
1398
|
+
const rows = read.rows.filter((row) => row[matchIndex] === updateCell.match);
|
|
1399
|
+
if (rows.length !== 1 || rows[0][columnIndex] === updateCell.value) return undefined;
|
|
1400
|
+
const schema = tableMutationSchema("table_update_cell_target", table);
|
|
1401
|
+
return {
|
|
1402
|
+
kind: "table-update-cell-target",
|
|
1403
|
+
path,
|
|
1404
|
+
hash: result.payload.hash,
|
|
1405
|
+
schema,
|
|
1406
|
+
operation: "table-update-cell",
|
|
1407
|
+
write: true,
|
|
1408
|
+
arguments: () => ({
|
|
1409
|
+
table: { heading: table.heading, ordinal: table.ordinal },
|
|
1410
|
+
where: { [matchColumn]: updateCell.match }, column, value: updateCell.value,
|
|
1411
|
+
}),
|
|
1412
|
+
systemPrompt: `Incise inspected the labelled table, resolved one exact host and output column, and activated ${schema.name}. Use ${schema.name} once with no arguments; the host supplies every guarded argument.`,
|
|
1413
|
+
};
|
|
1414
|
+
}
|
|
1415
|
+
const addRow = tableAddRowIntent(prompt);
|
|
1416
|
+
if (addRow) {
|
|
1417
|
+
const result = await runIncise(pi.exec.bind(pi), binary.path, ["tables", path]);
|
|
1418
|
+
if (result.code !== 0 || result.payload.ok === false) return undefined;
|
|
1419
|
+
if (typeof result.payload.hash !== "string") return undefined;
|
|
1420
|
+
const table = resolveTableEntry(
|
|
1421
|
+
parseTableSummary(String(result.payload.text ?? "")), addRow.heading,
|
|
1422
|
+
);
|
|
1423
|
+
if (!table) return undefined;
|
|
1424
|
+
const values = tableAddRowValues(table, addRow);
|
|
1425
|
+
if (!values) return undefined;
|
|
1426
|
+
const schema = tableAddRowSchema(table);
|
|
1427
|
+
return {
|
|
1428
|
+
kind: "table-add-row-target",
|
|
1429
|
+
path,
|
|
1430
|
+
hash: result.payload.hash,
|
|
1431
|
+
schema,
|
|
1432
|
+
operation: "table-add-row",
|
|
1433
|
+
write: true,
|
|
1434
|
+
arguments: () => ({
|
|
1435
|
+
table: { heading: table.heading, ordinal: table.ordinal },
|
|
1436
|
+
values,
|
|
1437
|
+
}),
|
|
1438
|
+
systemPrompt: `Incise inspected the tables, resolved the exact target and requested row, and activated ${schema.name}. Use ${schema.name} once with no arguments; the host supplies the file, table, values, and read hash.`,
|
|
1439
|
+
};
|
|
1440
|
+
}
|
|
1441
|
+
const ordinalRead = ordinalTableReadIntent(prompt);
|
|
1442
|
+
if (ordinalRead) {
|
|
1443
|
+
const result = await runIncise(pi.exec.bind(pi), binary.path, ["tables", path]);
|
|
1444
|
+
if (result.code !== 0 || result.payload.ok === false) return undefined;
|
|
1445
|
+
const tables = parseTableSummary(String(result.payload.text ?? "")).filter((table) => {
|
|
1446
|
+
const leaf = table.heading.split(" > ").at(-1) ?? table.heading;
|
|
1447
|
+
return leaf.toLowerCase() === ordinalRead.heading.toLowerCase();
|
|
1448
|
+
});
|
|
1449
|
+
if (tables.length !== 3 ||
|
|
1450
|
+
tables.map((table) => table.label?.toLowerCase()).join("|") !==
|
|
1451
|
+
"production hosts|staging hosts|scratch hosts") return undefined;
|
|
1452
|
+
const matches = tables.filter(
|
|
1453
|
+
(table) => table.label?.toLowerCase() === ordinalRead.label.toLowerCase(),
|
|
1454
|
+
);
|
|
1455
|
+
if (matches.length !== 1) return undefined;
|
|
1456
|
+
const table = matches[0];
|
|
1457
|
+
const schema = tableSchema(table, {});
|
|
1458
|
+
return {
|
|
1459
|
+
kind: "table-query",
|
|
1460
|
+
path,
|
|
1461
|
+
schema,
|
|
1462
|
+
operation: "rows",
|
|
1463
|
+
write: false,
|
|
1464
|
+
arguments: () => ({ table: { heading: table.heading, ordinal: table.ordinal } }),
|
|
1465
|
+
systemPrompt: `Incise inspected all three labelled tables and resolved the staging table. Use ${schema.name} once with no arguments, then answer only from its returned row.`,
|
|
1466
|
+
};
|
|
1467
|
+
}
|
|
1034
1468
|
if (!looksLikeTableRead(prompt)) return undefined;
|
|
1035
1469
|
const result = await runIncise(pi.exec.bind(pi), binary.path, ["tables", path]);
|
|
1036
1470
|
if (result.code !== 0 || result.payload.ok === false) return undefined;
|
|
@@ -1067,11 +1501,13 @@ export function installSafeRoutedProfile(
|
|
|
1067
1501
|
): void {
|
|
1068
1502
|
const routedNames = new Set([
|
|
1069
1503
|
"section_rename_target", "section_replace_target", "section_insert_target",
|
|
1070
|
-
"section_append_target", "section_set_level_target",
|
|
1504
|
+
"section_append_target", "section_delete_target", "section_set_level_target",
|
|
1071
1505
|
"frontmatter_clear", "frontmatter_set_string", "frontmatter_set_integer",
|
|
1072
1506
|
"frontmatter_set_boolean", "frontmatter_create_target", "frontmatter_delete_target",
|
|
1073
1507
|
"frontmatter_release_target", "list_remove_target",
|
|
1074
|
-
"list_append_target", "list_set_checked_target",
|
|
1508
|
+
"list_append_target", "list_set_checked_target",
|
|
1509
|
+
"table_add_row_target", "table_delete_row_target", "table_update_cell_target",
|
|
1510
|
+
"table_query",
|
|
1075
1511
|
]);
|
|
1076
1512
|
const ownedNames = new Set([...options.standardTools, ...routedNames]);
|
|
1077
1513
|
let state: RoutedState | undefined;
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-incise",
|
|
3
|
-
"version": "0.3.
|
|
3
|
+
"version": "0.3.3",
|
|
4
4
|
"description": "Semantic, byte-preserving Markdown editing tools for Pi and AI agents",
|
|
5
5
|
"type": "module",
|
|
6
6
|
"license": "MIT",
|
|
@@ -46,10 +46,10 @@
|
|
|
46
46
|
"typescript": "^5.9.3"
|
|
47
47
|
},
|
|
48
48
|
"optionalDependencies": {
|
|
49
|
-
"@pdalinis/pi-incise-darwin-arm64": "0.3.
|
|
50
|
-
"@pdalinis/pi-incise-darwin-x64": "0.3.
|
|
51
|
-
"@pdalinis/pi-incise-linux-arm64-gnu": "0.3.
|
|
52
|
-
"@pdalinis/pi-incise-linux-x64-gnu": "0.3.
|
|
49
|
+
"@pdalinis/pi-incise-darwin-arm64": "0.3.3",
|
|
50
|
+
"@pdalinis/pi-incise-darwin-x64": "0.3.3",
|
|
51
|
+
"@pdalinis/pi-incise-linux-arm64-gnu": "0.3.3",
|
|
52
|
+
"@pdalinis/pi-incise-linux-x64-gnu": "0.3.3"
|
|
53
53
|
},
|
|
54
54
|
"scripts": {
|
|
55
55
|
"test": "node --test --experimental-strip-types test/*.test.ts",
|