@hyperspaceng/neural-coding-agent 0.61.6 → 0.63.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +54 -0
- package/README.md +2 -2
- package/dist/cli/file-processor.d.ts.map +1 -1
- package/dist/cli/file-processor.js +4 -0
- package/dist/cli/file-processor.js.map +1 -1
- package/dist/core/agent-session.d.ts +10 -3
- package/dist/core/agent-session.d.ts.map +1 -1
- package/dist/core/agent-session.js +60 -46
- package/dist/core/agent-session.js.map +1 -1
- package/dist/core/export-html/index.d.ts +2 -2
- package/dist/core/export-html/index.d.ts.map +1 -1
- package/dist/core/export-html/index.js +2 -2
- package/dist/core/export-html/index.js.map +1 -1
- package/dist/core/export-html/tool-renderer.d.ts +2 -2
- package/dist/core/export-html/tool-renderer.d.ts.map +1 -1
- package/dist/core/export-html/tool-renderer.js +41 -16
- package/dist/core/export-html/tool-renderer.js.map +1 -1
- package/dist/core/extensions/index.d.ts +3 -2
- package/dist/core/extensions/index.d.ts.map +1 -1
- package/dist/core/extensions/index.js.map +1 -1
- package/dist/core/extensions/loader.d.ts.map +1 -1
- package/dist/core/extensions/loader.js +12 -2
- package/dist/core/extensions/loader.js.map +1 -1
- package/dist/core/extensions/runner.d.ts +4 -7
- package/dist/core/extensions/runner.d.ts.map +1 -1
- package/dist/core/extensions/runner.js +27 -38
- package/dist/core/extensions/runner.js.map +1 -1
- package/dist/core/extensions/types.d.ts +44 -9
- package/dist/core/extensions/types.d.ts.map +1 -1
- package/dist/core/extensions/types.js.map +1 -1
- package/dist/core/extensions/wrapper.d.ts.map +1 -1
- package/dist/core/extensions/wrapper.js +2 -8
- package/dist/core/extensions/wrapper.js.map +1 -1
- package/dist/core/index.d.ts +1 -0
- package/dist/core/index.d.ts.map +1 -1
- package/dist/core/index.js +1 -0
- package/dist/core/index.js.map +1 -1
- package/dist/core/output-guard.d.ts +6 -0
- package/dist/core/output-guard.d.ts.map +1 -0
- package/dist/core/output-guard.js +59 -0
- package/dist/core/output-guard.js.map +1 -0
- package/dist/core/package-manager.d.ts +1 -0
- package/dist/core/package-manager.d.ts.map +1 -1
- package/dist/core/package-manager.js +27 -8
- package/dist/core/package-manager.js.map +1 -1
- package/dist/core/prompt-templates.d.ts +2 -1
- package/dist/core/prompt-templates.d.ts.map +1 -1
- package/dist/core/prompt-templates.js +30 -32
- package/dist/core/prompt-templates.js.map +1 -1
- package/dist/core/resource-loader.d.ts +6 -5
- package/dist/core/resource-loader.d.ts.map +1 -1
- package/dist/core/resource-loader.js +136 -108
- package/dist/core/resource-loader.js.map +1 -1
- package/dist/core/sdk.d.ts +1 -1
- package/dist/core/sdk.d.ts.map +1 -1
- package/dist/core/sdk.js.map +1 -1
- package/dist/core/skills.d.ts +2 -1
- package/dist/core/skills.d.ts.map +1 -1
- package/dist/core/skills.js +25 -1
- package/dist/core/skills.js.map +1 -1
- package/dist/core/slash-commands.d.ts +2 -3
- package/dist/core/slash-commands.d.ts.map +1 -1
- package/dist/core/slash-commands.js.map +1 -1
- package/dist/core/source-info.d.ts +18 -0
- package/dist/core/source-info.d.ts.map +1 -0
- package/dist/core/source-info.js +19 -0
- package/dist/core/source-info.js.map +1 -0
- package/dist/core/system-prompt.d.ts.map +1 -1
- package/dist/core/system-prompt.js +3 -38
- package/dist/core/system-prompt.js.map +1 -1
- package/dist/core/tools/bash.d.ts +19 -9
- package/dist/core/tools/bash.d.ts.map +1 -1
- package/dist/core/tools/bash.js +151 -59
- package/dist/core/tools/bash.js.map +1 -1
- package/dist/core/tools/edit.d.ts +14 -2
- package/dist/core/tools/edit.d.ts.map +1 -1
- package/dist/core/tools/edit.js +92 -21
- package/dist/core/tools/edit.js.map +1 -1
- package/dist/core/tools/find.d.ts +11 -4
- package/dist/core/tools/find.d.ts.map +1 -1
- package/dist/core/tools/find.js +76 -27
- package/dist/core/tools/find.js.map +1 -1
- package/dist/core/tools/grep.d.ts +15 -4
- package/dist/core/tools/grep.d.ts.map +1 -1
- package/dist/core/tools/grep.js +83 -29
- package/dist/core/tools/grep.js.map +1 -1
- package/dist/core/tools/index.d.ts +57 -19
- package/dist/core/tools/index.d.ts.map +1 -1
- package/dist/core/tools/index.js +50 -26
- package/dist/core/tools/index.js.map +1 -1
- package/dist/core/tools/ls.d.ts +9 -3
- package/dist/core/tools/ls.d.ts.map +1 -1
- package/dist/core/tools/ls.js +67 -13
- package/dist/core/tools/ls.js.map +1 -1
- package/dist/core/tools/read.d.ts +10 -3
- package/dist/core/tools/read.d.ts.map +1 -1
- package/dist/core/tools/read.js +110 -51
- package/dist/core/tools/read.js.map +1 -1
- package/dist/core/tools/render-utils.d.ts +21 -0
- package/dist/core/tools/render-utils.d.ts.map +1 -0
- package/dist/core/tools/render-utils.js +49 -0
- package/dist/core/tools/render-utils.js.map +1 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts +14 -0
- package/dist/core/tools/tool-definition-wrapper.d.ts.map +1 -0
- package/dist/core/tools/tool-definition-wrapper.js +30 -0
- package/dist/core/tools/tool-definition-wrapper.js.map +1 -0
- package/dist/core/tools/write.d.ts +9 -3
- package/dist/core/tools/write.d.ts.map +1 -1
- package/dist/core/tools/write.js +162 -27
- package/dist/core/tools/write.js.map +1 -1
- package/dist/index.d.ts +3 -2
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -1
- package/dist/index.js.map +1 -1
- package/dist/main.d.ts.map +1 -1
- package/dist/main.js +29 -9
- package/dist/main.js.map +1 -1
- package/dist/modes/interactive/components/tool-execution.d.ts +15 -40
- package/dist/modes/interactive/components/tool-execution.d.ts.map +1 -1
- package/dist/modes/interactive/components/tool-execution.js +126 -679
- package/dist/modes/interactive/components/tool-execution.js.map +1 -1
- package/dist/modes/interactive/interactive-mode.d.ts +4 -11
- package/dist/modes/interactive/interactive-mode.d.ts.map +1 -1
- package/dist/modes/interactive/interactive-mode.js +144 -92
- package/dist/modes/interactive/interactive-mode.js.map +1 -1
- package/dist/modes/interactive/theme/theme.d.ts +3 -0
- package/dist/modes/interactive/theme/theme.d.ts.map +1 -1
- package/dist/modes/interactive/theme/theme.js +14 -0
- package/dist/modes/interactive/theme/theme.js.map +1 -1
- package/dist/modes/print-mode.d.ts.map +1 -1
- package/dist/modes/print-mode.js +5 -11
- package/dist/modes/print-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-mode.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-mode.js +27 -20
- package/dist/modes/rpc/rpc-mode.js.map +1 -1
- package/dist/modes/rpc/rpc-types.d.ts +3 -4
- package/dist/modes/rpc/rpc-types.d.ts.map +1 -1
- package/dist/modes/rpc/rpc-types.js.map +1 -1
- package/dist/utils/image-resize.d.ts +5 -5
- package/dist/utils/image-resize.d.ts.map +1 -1
- package/dist/utils/image-resize.js +45 -94
- package/dist/utils/image-resize.js.map +1 -1
- package/docs/extensions.md +72 -32
- package/docs/tui.md +2 -2
- package/examples/extensions/built-in-tool-renderer.ts +8 -8
- package/examples/extensions/commands.ts +3 -3
- package/examples/extensions/custom-provider-anthropic/package-lock.json +2 -2
- package/examples/extensions/custom-provider-anthropic/package.json +1 -1
- package/examples/extensions/custom-provider-gitlab-duo/package.json +1 -1
- package/examples/extensions/custom-provider-qwen-cli/package.json +1 -1
- package/examples/extensions/minimal-mode.ts +14 -14
- package/examples/extensions/question.ts +2 -2
- package/examples/extensions/questionnaire.ts +2 -2
- package/examples/extensions/subagent/index.ts +2 -2
- package/examples/extensions/todo.ts +2 -2
- package/examples/extensions/truncated-tool.ts +2 -2
- package/examples/extensions/with-deps/package-lock.json +2 -2
- package/examples/extensions/with-deps/package.json +1 -1
- package/examples/sdk/04-skills.ts +8 -2
- package/examples/sdk/08-prompt-templates.ts +2 -1
- package/examples/sdk/12-full-control.ts +0 -1
- package/package.json +4 -4
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { applyExifOrientation } from "./exif-orientation.js";
|
|
2
2
|
import { loadPhoton } from "./photon.js";
|
|
3
|
-
// 4.5MB
|
|
3
|
+
// 4.5MB of base64 payload. Provides headroom below Anthropic's 5MB limit.
|
|
4
4
|
const DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;
|
|
5
5
|
const DEFAULT_OPTIONS = {
|
|
6
6
|
maxWidth: 2000,
|
|
@@ -8,38 +8,34 @@ const DEFAULT_OPTIONS = {
|
|
|
8
8
|
maxBytes: DEFAULT_MAX_BYTES,
|
|
9
9
|
jpegQuality: 80,
|
|
10
10
|
};
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
return
|
|
11
|
+
function encodeCandidate(buffer, mimeType) {
|
|
12
|
+
const data = Buffer.from(buffer).toString("base64");
|
|
13
|
+
return {
|
|
14
|
+
data,
|
|
15
|
+
encodedSize: Buffer.byteLength(data, "utf-8"),
|
|
16
|
+
mimeType,
|
|
17
|
+
};
|
|
14
18
|
}
|
|
15
19
|
/**
|
|
16
|
-
* Resize an image to fit within the specified max dimensions and file size.
|
|
17
|
-
* Returns the
|
|
20
|
+
* Resize an image to fit within the specified max dimensions and encoded file size.
|
|
21
|
+
* Returns null if the image cannot be resized below maxBytes.
|
|
18
22
|
*
|
|
19
23
|
* Uses Photon (Rust/WASM) for image processing. If Photon is not available,
|
|
20
|
-
* returns
|
|
24
|
+
* returns null.
|
|
21
25
|
*
|
|
22
26
|
* Strategy for staying under maxBytes:
|
|
23
27
|
* 1. First resize to maxWidth/maxHeight
|
|
24
28
|
* 2. Try both PNG and JPEG formats, pick the smaller one
|
|
25
29
|
* 3. If still too large, try JPEG with decreasing quality
|
|
26
|
-
* 4. If still too large, progressively reduce dimensions
|
|
30
|
+
* 4. If still too large, progressively reduce dimensions until 1x1
|
|
27
31
|
*/
|
|
28
32
|
export async function resizeImage(img, options) {
|
|
29
33
|
const opts = { ...DEFAULT_OPTIONS, ...options };
|
|
30
34
|
const inputBuffer = Buffer.from(img.data, "base64");
|
|
35
|
+
const inputBase64Size = Buffer.byteLength(img.data, "utf-8");
|
|
31
36
|
const photon = await loadPhoton();
|
|
32
37
|
if (!photon) {
|
|
33
|
-
|
|
34
|
-
return {
|
|
35
|
-
data: img.data,
|
|
36
|
-
mimeType: img.mimeType,
|
|
37
|
-
originalWidth: 0,
|
|
38
|
-
originalHeight: 0,
|
|
39
|
-
width: 0,
|
|
40
|
-
height: 0,
|
|
41
|
-
wasResized: false,
|
|
42
|
-
};
|
|
38
|
+
return null;
|
|
43
39
|
}
|
|
44
40
|
let image;
|
|
45
41
|
try {
|
|
@@ -51,9 +47,8 @@ export async function resizeImage(img, options) {
|
|
|
51
47
|
const originalWidth = image.get_width();
|
|
52
48
|
const originalHeight = image.get_height();
|
|
53
49
|
const format = img.mimeType?.split("/")[1] ?? "png";
|
|
54
|
-
// Check if already within all limits (dimensions AND size)
|
|
55
|
-
|
|
56
|
-
if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {
|
|
50
|
+
// Check if already within all limits (dimensions AND encoded size)
|
|
51
|
+
if (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && inputBase64Size < opts.maxBytes) {
|
|
57
52
|
return {
|
|
58
53
|
data: img.data,
|
|
59
54
|
mimeType: img.mimeType ?? `image/${format}`,
|
|
@@ -75,96 +70,52 @@ export async function resizeImage(img, options) {
|
|
|
75
70
|
targetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);
|
|
76
71
|
targetHeight = opts.maxHeight;
|
|
77
72
|
}
|
|
78
|
-
|
|
79
|
-
function tryBothFormats(width, height, jpegQuality) {
|
|
73
|
+
function tryEncodings(width, height, jpegQualities) {
|
|
80
74
|
const resized = photon.resize(image, width, height, photon.SamplingFilter.Lanczos3);
|
|
81
75
|
try {
|
|
82
|
-
const
|
|
83
|
-
const
|
|
84
|
-
|
|
76
|
+
const candidates = [encodeCandidate(resized.get_bytes(), "image/png")];
|
|
77
|
+
for (const quality of jpegQualities) {
|
|
78
|
+
candidates.push(encodeCandidate(resized.get_bytes_jpeg(quality), "image/jpeg"));
|
|
79
|
+
}
|
|
80
|
+
return candidates;
|
|
85
81
|
}
|
|
86
82
|
finally {
|
|
87
83
|
resized.free();
|
|
88
84
|
}
|
|
89
85
|
}
|
|
90
|
-
|
|
91
|
-
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
best = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);
|
|
98
|
-
if (best.buffer.length <= opts.maxBytes) {
|
|
99
|
-
return {
|
|
100
|
-
data: Buffer.from(best.buffer).toString("base64"),
|
|
101
|
-
mimeType: best.mimeType,
|
|
102
|
-
originalWidth,
|
|
103
|
-
originalHeight,
|
|
104
|
-
width: finalWidth,
|
|
105
|
-
height: finalHeight,
|
|
106
|
-
wasResized: true,
|
|
107
|
-
};
|
|
108
|
-
}
|
|
109
|
-
// Still too large - try JPEG with decreasing quality
|
|
110
|
-
for (const quality of qualitySteps) {
|
|
111
|
-
best = tryBothFormats(targetWidth, targetHeight, quality);
|
|
112
|
-
if (best.buffer.length <= opts.maxBytes) {
|
|
113
|
-
return {
|
|
114
|
-
data: Buffer.from(best.buffer).toString("base64"),
|
|
115
|
-
mimeType: best.mimeType,
|
|
116
|
-
originalWidth,
|
|
117
|
-
originalHeight,
|
|
118
|
-
width: finalWidth,
|
|
119
|
-
height: finalHeight,
|
|
120
|
-
wasResized: true,
|
|
121
|
-
};
|
|
122
|
-
}
|
|
123
|
-
}
|
|
124
|
-
// Still too large - reduce dimensions progressively
|
|
125
|
-
for (const scale of scaleSteps) {
|
|
126
|
-
finalWidth = Math.round(targetWidth * scale);
|
|
127
|
-
finalHeight = Math.round(targetHeight * scale);
|
|
128
|
-
if (finalWidth < 100 || finalHeight < 100) {
|
|
129
|
-
break;
|
|
130
|
-
}
|
|
131
|
-
for (const quality of qualitySteps) {
|
|
132
|
-
best = tryBothFormats(finalWidth, finalHeight, quality);
|
|
133
|
-
if (best.buffer.length <= opts.maxBytes) {
|
|
86
|
+
const qualitySteps = Array.from(new Set([opts.jpegQuality, 85, 70, 55, 40]));
|
|
87
|
+
let currentWidth = targetWidth;
|
|
88
|
+
let currentHeight = targetHeight;
|
|
89
|
+
while (true) {
|
|
90
|
+
const candidates = tryEncodings(currentWidth, currentHeight, qualitySteps);
|
|
91
|
+
for (const candidate of candidates) {
|
|
92
|
+
if (candidate.encodedSize < opts.maxBytes) {
|
|
134
93
|
return {
|
|
135
|
-
data:
|
|
136
|
-
mimeType:
|
|
94
|
+
data: candidate.data,
|
|
95
|
+
mimeType: candidate.mimeType,
|
|
137
96
|
originalWidth,
|
|
138
97
|
originalHeight,
|
|
139
|
-
width:
|
|
140
|
-
height:
|
|
98
|
+
width: currentWidth,
|
|
99
|
+
height: currentHeight,
|
|
141
100
|
wasResized: true,
|
|
142
101
|
};
|
|
143
102
|
}
|
|
144
103
|
}
|
|
104
|
+
if (currentWidth === 1 && currentHeight === 1) {
|
|
105
|
+
break;
|
|
106
|
+
}
|
|
107
|
+
const nextWidth = currentWidth === 1 ? 1 : Math.max(1, Math.floor(currentWidth * 0.75));
|
|
108
|
+
const nextHeight = currentHeight === 1 ? 1 : Math.max(1, Math.floor(currentHeight * 0.75));
|
|
109
|
+
if (nextWidth === currentWidth && nextHeight === currentHeight) {
|
|
110
|
+
break;
|
|
111
|
+
}
|
|
112
|
+
currentWidth = nextWidth;
|
|
113
|
+
currentHeight = nextHeight;
|
|
145
114
|
}
|
|
146
|
-
|
|
147
|
-
return {
|
|
148
|
-
data: Buffer.from(best.buffer).toString("base64"),
|
|
149
|
-
mimeType: best.mimeType,
|
|
150
|
-
originalWidth,
|
|
151
|
-
originalHeight,
|
|
152
|
-
width: finalWidth,
|
|
153
|
-
height: finalHeight,
|
|
154
|
-
wasResized: true,
|
|
155
|
-
};
|
|
115
|
+
return null;
|
|
156
116
|
}
|
|
157
117
|
catch {
|
|
158
|
-
|
|
159
|
-
return {
|
|
160
|
-
data: img.data,
|
|
161
|
-
mimeType: img.mimeType,
|
|
162
|
-
originalWidth: 0,
|
|
163
|
-
originalHeight: 0,
|
|
164
|
-
width: 0,
|
|
165
|
-
height: 0,
|
|
166
|
-
wasResized: false,
|
|
167
|
-
};
|
|
118
|
+
return null;
|
|
168
119
|
}
|
|
169
120
|
finally {
|
|
170
121
|
if (image) {
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"image-resize.js","sourceRoot":"","sources":["../../src/utils/image-resize.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAmBzC,wDAAwD;AACxD,MAAM,iBAAiB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE5C,MAAM,eAAe,GAAiC;IACrD,QAAQ,EAAE,IAAI;IACd,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,iBAAiB;IAC3B,WAAW,EAAE,EAAE;CACf,CAAC;AAEF,gDAAgD;AAChD,SAAS,WAAW,CACnB,CAA2C,EAC3C,CAA2C,EACA;IAC3C,OAAO,CAAC,CAAC,MAAM,CAAC,MAAM,IAAI,CAAC,CAAC,MAAM,CAAC,MAAM,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC,CAAC;AAAA,CAClD;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAiB,EAAE,OAA4B,EAAyB;IACzG,MAAM,IAAI,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAChD,MAAM,WAAW,GAAG,MAAM,CAAC,IAAI,CAAC,GAAG,CAAC,IAAI,EAAE,QAAQ,CAAC,CAAC;IAEpD,MAAM,MAAM,GAAG,MAAM,UAAU,EAAE,CAAC;IAClC,IAAI,CAAC,MAAM,EAAE,CAAC;QACb,8CAA8C;QAC9C,OAAO;YACN,IAAI,EAAE,GAAG,CAAC,IAAI;YACd,QAAQ,EAAE,GAAG,CAAC,QAAQ;YACtB,aAAa,E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AAU,mBAAmB,CAAC,MAAoB,EAAsB;IAC7E,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC;IAClD,OAAO,oBAAoB,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,cAAc,kBAAkB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,6BAA6B,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC;AAAA,CAClM","sourcesContent":["import type { ImageContent } from \"@hyperspaceng/neural-ai\";\nimport { applyExifOrientation } from \"./exif-orientation.js\";\nimport { loadPhoton } from \"./photon.js\";\n\nexport interface ImageResizeOptions {\n\tmaxWidth?: number; // Default: 2000\n\tmaxHeight?: number; // Default: 2000\n\tmaxBytes?: number; // Default: 4.5MB (below Anthropic's 5MB limit)\n\tjpegQuality?: number; // Default: 80\n}\n\nexport interface ResizedImage {\n\tdata: string; // base64\n\tmimeType: string;\n\toriginalWidth: number;\n\toriginalHeight: number;\n\twidth: number;\n\theight: number;\n\twasResized: boolean;\n}\n\n// 4.5MB - provides headroom below Anthropic's 5MB limit\nconst DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;\n\nconst DEFAULT_OPTIONS: Required<ImageResizeOptions> = {\n\tmaxWidth: 2000,\n\tmaxHeight: 2000,\n\tmaxBytes: DEFAULT_MAX_BYTES,\n\tjpegQuality: 80,\n};\n\n/** Helper to pick the smaller of two buffers */\nfunction pickSmaller(\n\ta: { buffer: Uint8Array; mimeType: string },\n\tb: { buffer: Uint8Array; mimeType: string },\n): { buffer: Uint8Array; mimeType: string } {\n\treturn a.buffer.length <= b.buffer.length ? a : b;\n}\n\n/**\n * Resize an image to fit within the specified max dimensions and file size.\n * Returns the original image if it already fits within the limits.\n *\n * Uses Photon (Rust/WASM) for image processing. If Photon is not available,\n * returns the original image unchanged.\n *\n * Strategy for staying under maxBytes:\n * 1. First resize to maxWidth/maxHeight\n * 2. Try both PNG and JPEG formats, pick the smaller one\n * 3. If still too large, try JPEG with decreasing quality\n * 4. If still too large, progressively reduce dimensions\n */\nexport async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage> {\n\tconst opts = { ...DEFAULT_OPTIONS, ...options };\n\tconst inputBuffer = Buffer.from(img.data, \"base64\");\n\n\tconst photon = await loadPhoton();\n\tif (!photon) {\n\t\t// Photon not available, return original image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t}\n\n\tlet image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;\n\ttry {\n\t\tconst inputBytes = new Uint8Array(inputBuffer);\n\t\tconst rawImage = photon.PhotonImage.new_from_byteslice(inputBytes);\n\t\timage = applyExifOrientation(photon, rawImage, inputBytes);\n\t\tif (image !== rawImage) rawImage.free();\n\n\t\tconst originalWidth = image.get_width();\n\t\tconst originalHeight = image.get_height();\n\t\tconst format = img.mimeType?.split(\"/\")[1] ?? \"png\";\n\n\t\t// Check if already within all limits (dimensions AND size)\n\t\tconst originalSize = inputBuffer.length;\n\t\tif (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && originalSize <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: img.data,\n\t\t\t\tmimeType: img.mimeType ?? `image/${format}`,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: originalWidth,\n\t\t\t\theight: originalHeight,\n\t\t\t\twasResized: false,\n\t\t\t};\n\t\t}\n\n\t\t// Calculate initial dimensions respecting max limits\n\t\tlet targetWidth = originalWidth;\n\t\tlet targetHeight = originalHeight;\n\n\t\tif (targetWidth > opts.maxWidth) {\n\t\t\ttargetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);\n\t\t\ttargetWidth = opts.maxWidth;\n\t\t}\n\t\tif (targetHeight > opts.maxHeight) {\n\t\t\ttargetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);\n\t\t\ttargetHeight = opts.maxHeight;\n\t\t}\n\n\t\t// Helper to resize and encode in both formats, returning the smaller one\n\t\tfunction tryBothFormats(\n\t\t\twidth: number,\n\t\t\theight: number,\n\t\t\tjpegQuality: number,\n\t\t): { buffer: Uint8Array; mimeType: string } {\n\t\t\tconst resized = photon!.resize(image!, width, height, photon!.SamplingFilter.Lanczos3);\n\n\t\t\ttry {\n\t\t\t\tconst pngBuffer = resized.get_bytes();\n\t\t\t\tconst jpegBuffer = resized.get_bytes_jpeg(jpegQuality);\n\n\t\t\t\treturn pickSmaller(\n\t\t\t\t\t{ buffer: pngBuffer, mimeType: \"image/png\" },\n\t\t\t\t\t{ buffer: jpegBuffer, mimeType: \"image/jpeg\" },\n\t\t\t\t);\n\t\t\t} finally {\n\t\t\t\tresized.free();\n\t\t\t}\n\t\t}\n\n\t\t// Try to produce an image under maxBytes\n\t\tconst qualitySteps = [85, 70, 55, 40];\n\t\tconst scaleSteps = [1.0, 0.75, 0.5, 0.35, 0.25];\n\n\t\tlet best: { buffer: Uint8Array; mimeType: string };\n\t\tlet finalWidth = targetWidth;\n\t\tlet finalHeight = targetHeight;\n\n\t\t// First attempt: resize to target dimensions, try both formats\n\t\tbest = tryBothFormats(targetWidth, targetHeight, opts.jpegQuality);\n\n\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\tmimeType: best.mimeType,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: finalWidth,\n\t\t\t\theight: finalHeight,\n\t\t\t\twasResized: true,\n\t\t\t};\n\t\t}\n\n\t\t// Still too large - try JPEG with decreasing quality\n\t\tfor (const quality of qualitySteps) {\n\t\t\tbest = tryBothFormats(targetWidth, targetHeight, quality);\n\n\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\treturn {\n\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\toriginalWidth,\n\t\t\t\t\toriginalHeight,\n\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\theight: finalHeight,\n\t\t\t\t\twasResized: true,\n\t\t\t\t};\n\t\t\t}\n\t\t}\n\n\t\t// Still too large - reduce dimensions progressively\n\t\tfor (const scale of scaleSteps) {\n\t\t\tfinalWidth = Math.round(targetWidth * scale);\n\t\t\tfinalHeight = Math.round(targetHeight * scale);\n\n\t\t\tif (finalWidth < 100 || finalHeight < 100) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tfor (const quality of qualitySteps) {\n\t\t\t\tbest = tryBothFormats(finalWidth, finalHeight, quality);\n\n\t\t\t\tif (best.buffer.length <= opts.maxBytes) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\t\t\t\tmimeType: best.mimeType,\n\t\t\t\t\t\toriginalWidth,\n\t\t\t\t\t\toriginalHeight,\n\t\t\t\t\t\twidth: finalWidth,\n\t\t\t\t\t\theight: finalHeight,\n\t\t\t\t\t\twasResized: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\t\t}\n\n\t\t// Last resort: return smallest version we produced\n\t\treturn {\n\t\t\tdata: Buffer.from(best.buffer).toString(\"base64\"),\n\t\t\tmimeType: best.mimeType,\n\t\t\toriginalWidth,\n\t\t\toriginalHeight,\n\t\t\twidth: finalWidth,\n\t\t\theight: finalHeight,\n\t\t\twasResized: true,\n\t\t};\n\t} catch {\n\t\t// Failed to load image\n\t\treturn {\n\t\t\tdata: img.data,\n\t\t\tmimeType: img.mimeType,\n\t\t\toriginalWidth: 0,\n\t\t\toriginalHeight: 0,\n\t\t\twidth: 0,\n\t\t\theight: 0,\n\t\t\twasResized: false,\n\t\t};\n\t} finally {\n\t\tif (image) {\n\t\t\timage.free();\n\t\t}\n\t}\n}\n\n/**\n * Format a dimension note for resized images.\n * This helps the model understand the coordinate mapping.\n */\nexport function formatDimensionNote(result: ResizedImage): string | undefined {\n\tif (!result.wasResized) {\n\t\treturn undefined;\n\t}\n\n\tconst scale = result.originalWidth / result.width;\n\treturn `[Image: original ${result.originalWidth}x${result.originalHeight}, displayed at ${result.width}x${result.height}. Multiply coordinates by ${scale.toFixed(2)} to map to original image.]`;\n}\n"]}
|
|
1
|
+
{"version":3,"file":"image-resize.js","sourceRoot":"","sources":["../../src/utils/image-resize.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,oBAAoB,EAAE,MAAM,uBAAuB,CAAC;AAC7D,OAAO,EAAE,UAAU,EAAE,MAAM,aAAa,CAAC;AAmBzC,0EAA0E;AAC1E,MAAM,iBAAiB,GAAG,GAAG,GAAG,IAAI,GAAG,IAAI,CAAC;AAE5C,MAAM,eAAe,GAAiC;IACrD,QAAQ,EAAE,IAAI;IACd,SAAS,EAAE,IAAI;IACf,QAAQ,EAAE,iBAAiB;IAC3B,WAAW,EAAE,EAAE;CACf,CAAC;AAQF,SAAS,eAAe,CAAC,MAAkB,EAAE,QAAgB,EAAoB;IAChF,MAAM,IAAI,GAAG,MAAM,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAC;IACpD,OAAO;QACN,IAAI;QACJ,WAAW,EAAE,MAAM,CAAC,UAAU,CAAC,IAAI,EAAE,OAAO,CAAC;QAC7C,QAAQ;KACR,CAAC;AAAA,CACF;AAED;;;;;;;;;;;;GAYG;AACH,MAAM,CAAC,KAAK,UAAU,WAAW,CAAC,GAAiB,EAAE,OAA4B,EAAgC;IAChH,MAAM,IAAI,GAAG,EAAE,GAAG,eAAe,EAAE,GAAG,OAAO,EAAE,CAAC;IAChD,MAAM,WAAW,GAAG,MAAM,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AAC;YAAS,CAAC;QACV,IAAI,KAAK,EAAE,CAAC;YACX,KAAK,CAAC,IAAI,EAAE,CAAC;QACd,CAAC;IACF,CAAC;AAAA,CACD;AAED;;;GAGG;AACH,MAAM,UAAU,mBAAmB,CAAC,MAAoB,EAAsB;IAC7E,IAAI,CAAC,MAAM,CAAC,UAAU,EAAE,CAAC;QACxB,OAAO,SAAS,CAAC;IAClB,CAAC;IAED,MAAM,KAAK,GAAG,MAAM,CAAC,aAAa,GAAG,MAAM,CAAC,KAAK,CAAC;IAClD,OAAO,oBAAoB,MAAM,CAAC,aAAa,IAAI,MAAM,CAAC,cAAc,kBAAkB,MAAM,CAAC,KAAK,IAAI,MAAM,CAAC,MAAM,6BAA6B,KAAK,CAAC,OAAO,CAAC,CAAC,CAAC,6BAA6B,CAAC;AAAA,CAClM","sourcesContent":["import type { ImageContent } from \"@hyperspaceng/neural-ai\";\nimport { applyExifOrientation } from \"./exif-orientation.js\";\nimport { loadPhoton } from \"./photon.js\";\n\nexport interface ImageResizeOptions {\n\tmaxWidth?: number; // Default: 2000\n\tmaxHeight?: number; // Default: 2000\n\tmaxBytes?: number; // Default: 4.5MB of base64 payload (below Anthropic's 5MB limit)\n\tjpegQuality?: number; // Default: 80\n}\n\nexport interface ResizedImage {\n\tdata: string; // base64\n\tmimeType: string;\n\toriginalWidth: number;\n\toriginalHeight: number;\n\twidth: number;\n\theight: number;\n\twasResized: boolean;\n}\n\n// 4.5MB of base64 payload. Provides headroom below Anthropic's 5MB limit.\nconst DEFAULT_MAX_BYTES = 4.5 * 1024 * 1024;\n\nconst DEFAULT_OPTIONS: Required<ImageResizeOptions> = {\n\tmaxWidth: 2000,\n\tmaxHeight: 2000,\n\tmaxBytes: DEFAULT_MAX_BYTES,\n\tjpegQuality: 80,\n};\n\ninterface EncodedCandidate {\n\tdata: string;\n\tencodedSize: number;\n\tmimeType: string;\n}\n\nfunction encodeCandidate(buffer: Uint8Array, mimeType: string): EncodedCandidate {\n\tconst data = Buffer.from(buffer).toString(\"base64\");\n\treturn {\n\t\tdata,\n\t\tencodedSize: Buffer.byteLength(data, \"utf-8\"),\n\t\tmimeType,\n\t};\n}\n\n/**\n * Resize an image to fit within the specified max dimensions and encoded file size.\n * Returns null if the image cannot be resized below maxBytes.\n *\n * Uses Photon (Rust/WASM) for image processing. If Photon is not available,\n * returns null.\n *\n * Strategy for staying under maxBytes:\n * 1. First resize to maxWidth/maxHeight\n * 2. Try both PNG and JPEG formats, pick the smaller one\n * 3. If still too large, try JPEG with decreasing quality\n * 4. If still too large, progressively reduce dimensions until 1x1\n */\nexport async function resizeImage(img: ImageContent, options?: ImageResizeOptions): Promise<ResizedImage | null> {\n\tconst opts = { ...DEFAULT_OPTIONS, ...options };\n\tconst inputBuffer = Buffer.from(img.data, \"base64\");\n\tconst inputBase64Size = Buffer.byteLength(img.data, \"utf-8\");\n\n\tconst photon = await loadPhoton();\n\tif (!photon) {\n\t\treturn null;\n\t}\n\n\tlet image: ReturnType<typeof photon.PhotonImage.new_from_byteslice> | undefined;\n\ttry {\n\t\tconst inputBytes = new Uint8Array(inputBuffer);\n\t\tconst rawImage = photon.PhotonImage.new_from_byteslice(inputBytes);\n\t\timage = applyExifOrientation(photon, rawImage, inputBytes);\n\t\tif (image !== rawImage) rawImage.free();\n\n\t\tconst originalWidth = image.get_width();\n\t\tconst originalHeight = image.get_height();\n\t\tconst format = img.mimeType?.split(\"/\")[1] ?? \"png\";\n\n\t\t// Check if already within all limits (dimensions AND encoded size)\n\t\tif (originalWidth <= opts.maxWidth && originalHeight <= opts.maxHeight && inputBase64Size < opts.maxBytes) {\n\t\t\treturn {\n\t\t\t\tdata: img.data,\n\t\t\t\tmimeType: img.mimeType ?? `image/${format}`,\n\t\t\t\toriginalWidth,\n\t\t\t\toriginalHeight,\n\t\t\t\twidth: originalWidth,\n\t\t\t\theight: originalHeight,\n\t\t\t\twasResized: false,\n\t\t\t};\n\t\t}\n\n\t\t// Calculate initial dimensions respecting max limits\n\t\tlet targetWidth = originalWidth;\n\t\tlet targetHeight = originalHeight;\n\n\t\tif (targetWidth > opts.maxWidth) {\n\t\t\ttargetHeight = Math.round((targetHeight * opts.maxWidth) / targetWidth);\n\t\t\ttargetWidth = opts.maxWidth;\n\t\t}\n\t\tif (targetHeight > opts.maxHeight) {\n\t\t\ttargetWidth = Math.round((targetWidth * opts.maxHeight) / targetHeight);\n\t\t\ttargetHeight = opts.maxHeight;\n\t\t}\n\n\t\tfunction tryEncodings(width: number, height: number, jpegQualities: number[]): EncodedCandidate[] {\n\t\t\tconst resized = photon!.resize(image!, width, height, photon!.SamplingFilter.Lanczos3);\n\n\t\t\ttry {\n\t\t\t\tconst candidates: EncodedCandidate[] = [encodeCandidate(resized.get_bytes(), \"image/png\")];\n\t\t\t\tfor (const quality of jpegQualities) {\n\t\t\t\t\tcandidates.push(encodeCandidate(resized.get_bytes_jpeg(quality), \"image/jpeg\"));\n\t\t\t\t}\n\t\t\t\treturn candidates;\n\t\t\t} finally {\n\t\t\t\tresized.free();\n\t\t\t}\n\t\t}\n\n\t\tconst qualitySteps = Array.from(new Set([opts.jpegQuality, 85, 70, 55, 40]));\n\t\tlet currentWidth = targetWidth;\n\t\tlet currentHeight = targetHeight;\n\n\t\twhile (true) {\n\t\t\tconst candidates = tryEncodings(currentWidth, currentHeight, qualitySteps);\n\t\t\tfor (const candidate of candidates) {\n\t\t\t\tif (candidate.encodedSize < opts.maxBytes) {\n\t\t\t\t\treturn {\n\t\t\t\t\t\tdata: candidate.data,\n\t\t\t\t\t\tmimeType: candidate.mimeType,\n\t\t\t\t\t\toriginalWidth,\n\t\t\t\t\t\toriginalHeight,\n\t\t\t\t\t\twidth: currentWidth,\n\t\t\t\t\t\theight: currentHeight,\n\t\t\t\t\t\twasResized: true,\n\t\t\t\t\t};\n\t\t\t\t}\n\t\t\t}\n\n\t\t\tif (currentWidth === 1 && currentHeight === 1) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tconst nextWidth = currentWidth === 1 ? 1 : Math.max(1, Math.floor(currentWidth * 0.75));\n\t\t\tconst nextHeight = currentHeight === 1 ? 1 : Math.max(1, Math.floor(currentHeight * 0.75));\n\t\t\tif (nextWidth === currentWidth && nextHeight === currentHeight) {\n\t\t\t\tbreak;\n\t\t\t}\n\n\t\t\tcurrentWidth = nextWidth;\n\t\t\tcurrentHeight = nextHeight;\n\t\t}\n\n\t\treturn null;\n\t} catch {\n\t\treturn null;\n\t} finally {\n\t\tif (image) {\n\t\t\timage.free();\n\t\t}\n\t}\n}\n\n/**\n * Format a dimension note for resized images.\n * This helps the model understand the coordinate mapping.\n */\nexport function formatDimensionNote(result: ResizedImage): string | undefined {\n\tif (!result.wasResized) {\n\t\treturn undefined;\n\t}\n\n\tconst scale = result.originalWidth / result.width;\n\treturn `[Image: original ${result.originalWidth}x${result.originalHeight}, displayed at ${result.width}x${result.height}. Multiply coordinates by ${scale.toFixed(2)} to map to original image.]`;\n}\n"]}
|
package/docs/extensions.md
CHANGED
|
@@ -976,8 +976,8 @@ pi.registerTool({
|
|
|
976
976
|
},
|
|
977
977
|
|
|
978
978
|
// Optional: Custom rendering
|
|
979
|
-
renderCall(args, theme) { ... },
|
|
980
|
-
renderResult(result, options, theme) { ... },
|
|
979
|
+
renderCall(args, theme, context) { ... },
|
|
980
|
+
renderResult(result, options, theme, context) { ... },
|
|
981
981
|
});
|
|
982
982
|
```
|
|
983
983
|
|
|
@@ -1089,6 +1089,8 @@ Labels persist in the session and survive restarts. Use them to mark important p
|
|
|
1089
1089
|
|
|
1090
1090
|
Register a command.
|
|
1091
1091
|
|
|
1092
|
+
If multiple extensions register the same command name, pi keeps them all and assigns numeric invocation suffixes in load order, for example `/review:1` and `/review:2`.
|
|
1093
|
+
|
|
1092
1094
|
```typescript
|
|
1093
1095
|
pi.registerCommand("stats", {
|
|
1094
1096
|
description: "Show session statistics",
|
|
@@ -1126,20 +1128,28 @@ The list matches the RPC `get_commands` ordering: extensions first, then templat
|
|
|
1126
1128
|
```typescript
|
|
1127
1129
|
const commands = pi.getCommands();
|
|
1128
1130
|
const bySource = commands.filter((command) => command.source === "extension");
|
|
1131
|
+
const userScoped = commands.filter((command) => command.sourceInfo.scope === "user");
|
|
1129
1132
|
```
|
|
1130
1133
|
|
|
1131
1134
|
Each entry has this shape:
|
|
1132
1135
|
|
|
1133
1136
|
```typescript
|
|
1134
1137
|
{
|
|
1135
|
-
name: string; //
|
|
1138
|
+
name: string; // Invokable command name without the leading slash. May be suffixed like "review:1"
|
|
1136
1139
|
description?: string;
|
|
1137
1140
|
source: "extension" | "prompt" | "skill";
|
|
1138
|
-
|
|
1139
|
-
|
|
1141
|
+
sourceInfo: {
|
|
1142
|
+
path: string;
|
|
1143
|
+
source: string;
|
|
1144
|
+
scope: "user" | "project" | "temporary";
|
|
1145
|
+
origin: "package" | "top-level";
|
|
1146
|
+
baseDir?: string;
|
|
1147
|
+
};
|
|
1140
1148
|
}
|
|
1141
1149
|
```
|
|
1142
1150
|
|
|
1151
|
+
Use `sourceInfo` as the canonical provenance field. Do not infer ownership from command names or from ad hoc path parsing.
|
|
1152
|
+
|
|
1143
1153
|
Built-in interactive commands (like `/model` and `/settings`) are not included here. They are handled only in interactive
|
|
1144
1154
|
mode and would not execute if sent via `prompt`.
|
|
1145
1155
|
|
|
@@ -1191,12 +1201,27 @@ const result = await pi.exec("git", ["status"], { signal, timeout: 5000 });
|
|
|
1191
1201
|
Manage active tools. This works for both built-in tools and dynamically registered tools.
|
|
1192
1202
|
|
|
1193
1203
|
```typescript
|
|
1194
|
-
const active = pi.getActiveTools();
|
|
1195
|
-
const all = pi.getAllTools();
|
|
1196
|
-
|
|
1204
|
+
const active = pi.getActiveTools();
|
|
1205
|
+
const all = pi.getAllTools();
|
|
1206
|
+
// [{
|
|
1207
|
+
// name: "read",
|
|
1208
|
+
// description: "Read file contents...",
|
|
1209
|
+
// parameters: ...,
|
|
1210
|
+
// sourceInfo: { path: "<builtin:read>", source: "builtin", scope: "temporary", origin: "top-level" }
|
|
1211
|
+
// }, ...]
|
|
1212
|
+
const names = all.map(t => t.name);
|
|
1213
|
+
const builtinTools = all.filter((t) => t.sourceInfo.source === "builtin");
|
|
1214
|
+
const extensionTools = all.filter((t) => t.sourceInfo.source !== "builtin" && t.sourceInfo.source !== "sdk");
|
|
1197
1215
|
pi.setActiveTools(["read", "bash"]); // Switch to read-only
|
|
1198
1216
|
```
|
|
1199
1217
|
|
|
1218
|
+
`pi.getAllTools()` returns `name`, `description`, `parameters`, and `sourceInfo`.
|
|
1219
|
+
|
|
1220
|
+
Typical `sourceInfo.source` values:
|
|
1221
|
+
- `builtin` for built-in tools
|
|
1222
|
+
- `sdk` for tools passed via `createAgentSession({ customTools })`
|
|
1223
|
+
- extension source metadata for tools registered by extensions
|
|
1224
|
+
|
|
1200
1225
|
### pi.setModel(model)
|
|
1201
1226
|
|
|
1202
1227
|
Set the current model. Returns `false` if no API key is available for the model. See [models.md](models.md) for configuring custom models.
|
|
@@ -1427,8 +1452,8 @@ pi.registerTool({
|
|
|
1427
1452
|
},
|
|
1428
1453
|
|
|
1429
1454
|
// Optional: Custom rendering
|
|
1430
|
-
renderCall(args, theme) { ... },
|
|
1431
|
-
renderResult(result, options, theme) { ... },
|
|
1455
|
+
renderCall(args, theme, context) { ... },
|
|
1456
|
+
renderResult(result, options, theme, context) { ... },
|
|
1432
1457
|
});
|
|
1433
1458
|
```
|
|
1434
1459
|
|
|
@@ -1463,7 +1488,9 @@ pi --no-tools -e ./my-extension.ts
|
|
|
1463
1488
|
|
|
1464
1489
|
See [examples/extensions/tool-override.ts](../examples/extensions/tool-override.ts) for a complete example that overrides `read` with logging and access control.
|
|
1465
1490
|
|
|
1466
|
-
**Rendering:** If your override
|
|
1491
|
+
**Rendering:** Built-in renderer inheritance is resolved per slot. Execution override and rendering override are independent. If your override omits `renderCall`, the built-in `renderCall` is used. If your override omits `renderResult`, the built-in `renderResult` is used. If your override omits both, the built-in renderer is used automatically (syntax highlighting, diffs, etc.). This lets you wrap built-in tools for logging or access control without reimplementing the UI.
|
|
1492
|
+
|
|
1493
|
+
**Prompt metadata:** `promptSnippet` and `promptGuidelines` are not inherited from the built-in tool. If your override should keep those prompt instructions, define them on the override explicitly.
|
|
1467
1494
|
|
|
1468
1495
|
**Your implementation must match the exact result shape**, including the `details` type. The UI and session logic depend on these shapes for rendering and state tracking.
|
|
1469
1496
|
|
|
@@ -1597,44 +1624,52 @@ export default function (pi: ExtensionAPI) {
|
|
|
1597
1624
|
|
|
1598
1625
|
### Custom Rendering
|
|
1599
1626
|
|
|
1600
|
-
Tools can provide `renderCall` and `renderResult` for custom TUI display. See [tui.md](tui.md) for the full component API and [tool-execution.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/modes/interactive/components/tool-execution.ts) for how
|
|
1627
|
+
Tools can provide `renderCall` and `renderResult` for custom TUI display. See [tui.md](tui.md) for the full component API and [tool-execution.ts](https://github.com/badlogic/pi-mono/blob/main/packages/coding-agent/src/modes/interactive/components/tool-execution.ts) for how tool rows are composed.
|
|
1628
|
+
|
|
1629
|
+
Tool output is wrapped in a `Box` that handles padding and background. A defined `renderCall` or `renderResult` must return a `Component`. If a slot renderer is not defined, `tool-execution.ts` uses fallback rendering for that slot.
|
|
1630
|
+
|
|
1631
|
+
`renderCall` and `renderResult` each receive a `context` object with:
|
|
1632
|
+
- `args` - the current tool call arguments
|
|
1633
|
+
- `state` - shared row-local state across `renderCall` and `renderResult`
|
|
1634
|
+
- `lastComponent` - the previously returned component for that slot, if any
|
|
1635
|
+
- `invalidate()` - request a rerender of this tool row
|
|
1636
|
+
- `toolCallId`, `cwd`, `executionStarted`, `argsComplete`, `isPartial`, `expanded`, `showImages`, `isError`
|
|
1601
1637
|
|
|
1602
|
-
|
|
1638
|
+
Use `context.state` for cross-slot shared state. Keep slot-local caches on the returned component instance when you want to reuse and mutate the same component across renders.
|
|
1603
1639
|
|
|
1604
1640
|
#### renderCall
|
|
1605
1641
|
|
|
1606
|
-
Renders the tool call
|
|
1642
|
+
Renders the tool call or header:
|
|
1607
1643
|
|
|
1608
1644
|
```typescript
|
|
1609
1645
|
import { Text } from "@mariozechner/pi-tui";
|
|
1610
1646
|
|
|
1611
|
-
renderCall(args, theme) {
|
|
1612
|
-
|
|
1613
|
-
|
|
1647
|
+
renderCall(args, theme, context) {
|
|
1648
|
+
const text = (context.lastComponent as Text | undefined) ?? new Text("", 0, 0);
|
|
1649
|
+
let content = theme.fg("toolTitle", theme.bold("my_tool "));
|
|
1650
|
+
content += theme.fg("muted", args.action);
|
|
1614
1651
|
if (args.text) {
|
|
1615
|
-
|
|
1652
|
+
content += " " + theme.fg("dim", `"${args.text}"`);
|
|
1616
1653
|
}
|
|
1617
|
-
|
|
1654
|
+
text.setText(content);
|
|
1655
|
+
return text;
|
|
1618
1656
|
}
|
|
1619
1657
|
```
|
|
1620
1658
|
|
|
1621
1659
|
#### renderResult
|
|
1622
1660
|
|
|
1623
|
-
Renders the tool result:
|
|
1661
|
+
Renders the tool result or output:
|
|
1624
1662
|
|
|
1625
1663
|
```typescript
|
|
1626
|
-
renderResult(result, { expanded, isPartial }, theme) {
|
|
1627
|
-
// Handle streaming
|
|
1664
|
+
renderResult(result, { expanded, isPartial }, theme, context) {
|
|
1628
1665
|
if (isPartial) {
|
|
1629
1666
|
return new Text(theme.fg("warning", "Processing..."), 0, 0);
|
|
1630
1667
|
}
|
|
1631
1668
|
|
|
1632
|
-
// Handle errors
|
|
1633
1669
|
if (result.details?.error) {
|
|
1634
1670
|
return new Text(theme.fg("error", `Error: ${result.details.error}`), 0, 0);
|
|
1635
1671
|
}
|
|
1636
1672
|
|
|
1637
|
-
// Normal result - support expanded view (Ctrl+O)
|
|
1638
1673
|
let text = theme.fg("success", "✓ Done");
|
|
1639
1674
|
if (expanded && result.details?.items) {
|
|
1640
1675
|
for (const item of result.details.items) {
|
|
@@ -1645,6 +1680,8 @@ renderResult(result, { expanded, isPartial }, theme) {
|
|
|
1645
1680
|
}
|
|
1646
1681
|
```
|
|
1647
1682
|
|
|
1683
|
+
If a slot intentionally has no visible content, return an empty `Component` such as an empty `Container`.
|
|
1684
|
+
|
|
1648
1685
|
#### Keybinding Hints
|
|
1649
1686
|
|
|
1650
1687
|
Use `keyHint()` to display keybinding hints that respect the active keybinding configuration:
|
|
@@ -1652,7 +1689,7 @@ Use `keyHint()` to display keybinding hints that respect the active keybinding c
|
|
|
1652
1689
|
```typescript
|
|
1653
1690
|
import { keyHint } from "@mariozechner/pi-coding-agent";
|
|
1654
1691
|
|
|
1655
|
-
renderResult(result, { expanded }, theme) {
|
|
1692
|
+
renderResult(result, { expanded }, theme, context) {
|
|
1656
1693
|
let text = theme.fg("success", "✓ Done");
|
|
1657
1694
|
if (!expanded) {
|
|
1658
1695
|
text += ` (${keyHint("app.tools.expand", "to expand")})`;
|
|
@@ -1676,16 +1713,19 @@ Custom editors and `ctx.ui.custom()` components receive `keybindings: Keybinding
|
|
|
1676
1713
|
|
|
1677
1714
|
#### Best Practices
|
|
1678
1715
|
|
|
1679
|
-
- Use `Text` with padding `(0, 0)
|
|
1680
|
-
- Use `\n` for multi-line content
|
|
1681
|
-
- Handle `isPartial` for streaming progress
|
|
1682
|
-
- Support `expanded` for detail on demand
|
|
1683
|
-
- Keep default view compact
|
|
1716
|
+
- Use `Text` with padding `(0, 0)`. The Box handles padding.
|
|
1717
|
+
- Use `\n` for multi-line content.
|
|
1718
|
+
- Handle `isPartial` for streaming progress.
|
|
1719
|
+
- Support `expanded` for detail on demand.
|
|
1720
|
+
- Keep default view compact.
|
|
1721
|
+
- Read `context.args` in `renderResult` instead of copying args into `context.state`.
|
|
1722
|
+
- Use `context.state` only for data that must be shared across call and result slots.
|
|
1723
|
+
- Reuse `context.lastComponent` when the same component instance can be updated in place.
|
|
1684
1724
|
|
|
1685
1725
|
#### Fallback
|
|
1686
1726
|
|
|
1687
|
-
If
|
|
1688
|
-
- `renderCall`: Shows tool name
|
|
1727
|
+
If a slot renderer is not defined or throws:
|
|
1728
|
+
- `renderCall`: Shows the tool name
|
|
1689
1729
|
- `renderResult`: Shows raw text from `content`
|
|
1690
1730
|
|
|
1691
1731
|
## Custom UI
|
package/docs/tui.md
CHANGED
|
@@ -394,7 +394,7 @@ Components accept theme objects for styling.
|
|
|
394
394
|
**In `renderCall`/`renderResult`**, use the `theme` parameter:
|
|
395
395
|
|
|
396
396
|
```typescript
|
|
397
|
-
renderResult(result, options, theme) {
|
|
397
|
+
renderResult(result, options, theme, context) {
|
|
398
398
|
// Use theme.fg() for foreground colors
|
|
399
399
|
return new Text(theme.fg("success", "Done!"), 0, 0);
|
|
400
400
|
|
|
@@ -428,7 +428,7 @@ renderResult(result, options, theme) {
|
|
|
428
428
|
import { getMarkdownTheme } from "@mariozechner/pi-coding-agent";
|
|
429
429
|
import { Markdown } from "@mariozechner/pi-tui";
|
|
430
430
|
|
|
431
|
-
renderResult(result, options, theme) {
|
|
431
|
+
renderResult(result, options, theme, context) {
|
|
432
432
|
const mdTheme = getMarkdownTheme();
|
|
433
433
|
return new Markdown(result.details.markdown, 0, 0, mdTheme);
|
|
434
434
|
}
|
|
@@ -42,7 +42,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
42
42
|
return originalRead.execute(toolCallId, params, signal, onUpdate);
|
|
43
43
|
},
|
|
44
44
|
|
|
45
|
-
renderCall(args, theme) {
|
|
45
|
+
renderCall(args, theme, _context) {
|
|
46
46
|
let text = theme.fg("toolTitle", theme.bold("read "));
|
|
47
47
|
text += theme.fg("accent", args.path);
|
|
48
48
|
if (args.offset || args.limit) {
|
|
@@ -54,7 +54,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
54
54
|
return new Text(text, 0, 0);
|
|
55
55
|
},
|
|
56
56
|
|
|
57
|
-
renderResult(result, { expanded, isPartial }, theme) {
|
|
57
|
+
renderResult(result, { expanded, isPartial }, theme, _context) {
|
|
58
58
|
if (isPartial) return new Text(theme.fg("warning", "Reading..."), 0, 0);
|
|
59
59
|
|
|
60
60
|
const details = result.details as ReadToolDetails | undefined;
|
|
@@ -101,7 +101,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
101
101
|
return originalBash.execute(toolCallId, params, signal, onUpdate);
|
|
102
102
|
},
|
|
103
103
|
|
|
104
|
-
renderCall(args, theme) {
|
|
104
|
+
renderCall(args, theme, _context) {
|
|
105
105
|
let text = theme.fg("toolTitle", theme.bold("$ "));
|
|
106
106
|
const cmd = args.command.length > 80 ? `${args.command.slice(0, 77)}...` : args.command;
|
|
107
107
|
text += theme.fg("accent", cmd);
|
|
@@ -111,7 +111,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
111
111
|
return new Text(text, 0, 0);
|
|
112
112
|
},
|
|
113
113
|
|
|
114
|
-
renderResult(result, { expanded, isPartial }, theme) {
|
|
114
|
+
renderResult(result, { expanded, isPartial }, theme, _context) {
|
|
115
115
|
if (isPartial) return new Text(theme.fg("warning", "Running..."), 0, 0);
|
|
116
116
|
|
|
117
117
|
const details = result.details as BashToolDetails | undefined;
|
|
@@ -160,13 +160,13 @@ export default function (pi: ExtensionAPI) {
|
|
|
160
160
|
return originalEdit.execute(toolCallId, params, signal, onUpdate);
|
|
161
161
|
},
|
|
162
162
|
|
|
163
|
-
renderCall(args, theme) {
|
|
163
|
+
renderCall(args, theme, _context) {
|
|
164
164
|
let text = theme.fg("toolTitle", theme.bold("edit "));
|
|
165
165
|
text += theme.fg("accent", args.path);
|
|
166
166
|
return new Text(text, 0, 0);
|
|
167
167
|
},
|
|
168
168
|
|
|
169
|
-
renderResult(result, { expanded, isPartial }, theme) {
|
|
169
|
+
renderResult(result, { expanded, isPartial }, theme, _context) {
|
|
170
170
|
if (isPartial) return new Text(theme.fg("warning", "Editing..."), 0, 0);
|
|
171
171
|
|
|
172
172
|
const details = result.details as EditToolDetails | undefined;
|
|
@@ -224,7 +224,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
224
224
|
return originalWrite.execute(toolCallId, params, signal, onUpdate);
|
|
225
225
|
},
|
|
226
226
|
|
|
227
|
-
renderCall(args, theme) {
|
|
227
|
+
renderCall(args, theme, _context) {
|
|
228
228
|
let text = theme.fg("toolTitle", theme.bold("write "));
|
|
229
229
|
text += theme.fg("accent", args.path);
|
|
230
230
|
const lineCount = args.content.split("\n").length;
|
|
@@ -232,7 +232,7 @@ export default function (pi: ExtensionAPI) {
|
|
|
232
232
|
return new Text(text, 0, 0);
|
|
233
233
|
},
|
|
234
234
|
|
|
235
|
-
renderResult(result, { isPartial }, theme) {
|
|
235
|
+
renderResult(result, { isPartial }, theme, _context) {
|
|
236
236
|
if (isPartial) return new Text(theme.fg("warning", "Writing..."), 0, 0);
|
|
237
237
|
|
|
238
238
|
const content = result.content[0];
|
|
@@ -60,10 +60,10 @@ export default function commandsExtension(pi: ExtensionAPI) {
|
|
|
60
60
|
if (selected && !selected.startsWith("---")) {
|
|
61
61
|
const cmdName = selected.split(" - ")[0].slice(1); // Remove leading /
|
|
62
62
|
const cmd = commands.find((c) => c.name === cmdName);
|
|
63
|
-
if (cmd?.path) {
|
|
64
|
-
const showPath = await ctx.ui.confirm(cmd.name, `View source path?\n${cmd.path}`);
|
|
63
|
+
if (cmd?.sourceInfo.path) {
|
|
64
|
+
const showPath = await ctx.ui.confirm(cmd.name, `View source path?\n${cmd.sourceInfo.path}`);
|
|
65
65
|
if (showPath) {
|
|
66
|
-
ctx.ui.notify(cmd.path, "info");
|
|
66
|
+
ctx.ui.notify(cmd.sourceInfo.path, "info");
|
|
67
67
|
}
|
|
68
68
|
}
|
|
69
69
|
}
|
|
@@ -1,12 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "pi-extension-custom-provider",
|
|
3
|
-
"version": "1.
|
|
3
|
+
"version": "1.14.0",
|
|
4
4
|
"lockfileVersion": 3,
|
|
5
5
|
"requires": true,
|
|
6
6
|
"packages": {
|
|
7
7
|
"": {
|
|
8
8
|
"name": "pi-extension-custom-provider",
|
|
9
|
-
"version": "1.
|
|
9
|
+
"version": "1.14.0",
|
|
10
10
|
"dependencies": {
|
|
11
11
|
"@anthropic-ai/sdk": "^0.52.0"
|
|
12
12
|
}
|