@introspection-ai/recipes 0.46.1 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/artifact-review-tui.d.ts.map +1 -1
- package/dist/artifact-review-tui.js +22 -15
- package/dist/artifact-review-tui.js.map +1 -1
- package/dist/artifact-viewer-tui.d.ts +28 -4
- package/dist/artifact-viewer-tui.d.ts.map +1 -1
- package/dist/artifact-viewer-tui.js +58 -10
- package/dist/artifact-viewer-tui.js.map +1 -1
- package/dist/artifact-viewer.d.ts +35 -13
- package/dist/artifact-viewer.d.ts.map +1 -1
- package/dist/artifact-viewer.js +603 -76
- package/dist/artifact-viewer.js.map +1 -1
- package/dist/artifact.d.ts +110 -0
- package/dist/artifact.d.ts.map +1 -0
- package/dist/artifact.js +119 -0
- package/dist/artifact.js.map +1 -0
- package/dist/pi-extension.d.ts.map +1 -1
- package/dist/pi-extension.js +2 -1
- package/dist/pi-extension.js.map +1 -1
- package/dist/recipe/manifest.generated.d.ts +1 -1
- package/dist/recipe/manifest.generated.js +1 -1
- package/dist/review.d.ts +11 -1
- package/dist/review.d.ts.map +1 -1
- package/dist/review.js +29 -1
- package/dist/review.js.map +1 -1
- package/dist/session.d.ts +11 -0
- package/dist/session.d.ts.map +1 -1
- package/dist/session.js +4 -1
- package/dist/session.js.map +1 -1
- package/package.json +5 -5
package/dist/artifact-viewer.js
CHANGED
|
@@ -4,19 +4,23 @@
|
|
|
4
4
|
* the right of Pi's terminal, so the user can read it before approving it.
|
|
5
5
|
*
|
|
6
6
|
* An agent gets it by listing `show_artifact` in its `tools`, like
|
|
7
|
-
* `ask_user_question`.
|
|
8
|
-
*
|
|
9
|
-
*
|
|
10
|
-
*
|
|
7
|
+
* `ask_user_question`. Any text file can be shown: markdown renders, code and
|
|
8
|
+
* other text are highlighted, and a file can be shown as of a git commit or
|
|
9
|
+
* as a diff against one (or against the version this session last showed).
|
|
10
|
+
* The panel exists only in Pi's interactive terminal; elsewhere the result's
|
|
11
|
+
* `details` carry the file and the diff, so a host such as a browser can
|
|
12
|
+
* render them itself.
|
|
11
13
|
*/
|
|
12
|
-
import { spawn } from "node:child_process";
|
|
13
|
-
import { readdirSync, readFileSync, realpathSync, statSync, watch, } from "node:fs";
|
|
14
|
-
import { basename, dirname, extname, isAbsolute, relative, resolve, sep, } from "node:path";
|
|
14
|
+
import { execFileSync, spawn } from "node:child_process";
|
|
15
|
+
import { closeSync, existsSync, lstatSync, openSync, readdirSync, readFileSync, readSync, realpathSync, statSync, watch, } from "node:fs";
|
|
16
|
+
import { basename, dirname, extname, isAbsolute, join, relative, resolve, sep, } from "node:path";
|
|
17
|
+
import { generateDiffString, getLanguageFromPath, } from "@earendil-works/pi-coding-agent";
|
|
15
18
|
import { Type } from "typebox";
|
|
16
19
|
import { ARTIFACT_PANEL_MIN_TERMINAL_COLUMNS, ARTIFACT_PANEL_OVERLAY_OPTIONS, ArtifactPanel, } from "./artifact-viewer-tui.js";
|
|
20
|
+
import { artifactDiffCounts, artifactKind, SHOW_ARTIFACT_TOOL_NAME, } from "./artifact.js";
|
|
17
21
|
import { createReviewArtifactsTool, REVIEW_ARTIFACTS_TOOL_NAME, } from "./artifact-review.js";
|
|
18
22
|
import { artifactPanelControl, setArtifactPanelControl, } from "./interaction-tui.js";
|
|
19
|
-
export
|
|
23
|
+
export { SHOW_ARTIFACT_TOOL_NAME } from "./artifact.js";
|
|
20
24
|
/** The viewer's commands, as subcommands of Recipes' own `/recipe` so they
|
|
21
25
|
* never clash with another extension's or the user's commands. */
|
|
22
26
|
const VIEW_ARTIFACT_COMMAND = "recipe view";
|
|
@@ -86,12 +90,53 @@ const SKIPPED_FOLDERS = new Set([
|
|
|
86
90
|
/** Where an owner's reviewable files usually live, listed first. */
|
|
87
91
|
const PREFERRED_PARTS = ["docs/", "evals/cases/", "prompts/", "outputs/"];
|
|
88
92
|
const MAX_LISTED_FILES = 50;
|
|
93
|
+
/** Extensions of files that are never text, left out of the file list without reading them. */
|
|
94
|
+
const BINARY_EXTENSIONS = new Set([
|
|
95
|
+
"png",
|
|
96
|
+
"jpg",
|
|
97
|
+
"jpeg",
|
|
98
|
+
"gif",
|
|
99
|
+
"webp",
|
|
100
|
+
"ico",
|
|
101
|
+
"bmp",
|
|
102
|
+
"tiff",
|
|
103
|
+
"pdf",
|
|
104
|
+
"zip",
|
|
105
|
+
"gz",
|
|
106
|
+
"tgz",
|
|
107
|
+
"bz2",
|
|
108
|
+
"xz",
|
|
109
|
+
"7z",
|
|
110
|
+
"tar",
|
|
111
|
+
"jar",
|
|
112
|
+
"wasm",
|
|
113
|
+
"so",
|
|
114
|
+
"dylib",
|
|
115
|
+
"dll",
|
|
116
|
+
"exe",
|
|
117
|
+
"bin",
|
|
118
|
+
"woff",
|
|
119
|
+
"woff2",
|
|
120
|
+
"ttf",
|
|
121
|
+
"otf",
|
|
122
|
+
"mp3",
|
|
123
|
+
"mp4",
|
|
124
|
+
"mov",
|
|
125
|
+
"wav",
|
|
126
|
+
"parquet",
|
|
127
|
+
"sqlite",
|
|
128
|
+
"db",
|
|
129
|
+
"docx",
|
|
130
|
+
"xlsx",
|
|
131
|
+
"pptx",
|
|
132
|
+
].map((extension) => `.${extension}`));
|
|
89
133
|
/**
|
|
90
|
-
* The
|
|
91
|
-
* case-insensitive)
|
|
92
|
-
* so a large workspace stays quick; hidden
|
|
134
|
+
* The text files under `cwd` matching `query` (a substring of the path,
|
|
135
|
+
* case-insensitive): the usual review folders first, then markdown, then the
|
|
136
|
+
* rest. Bounded in depth and count so a large workspace stays quick; hidden
|
|
137
|
+
* folders, build output and binary file types are skipped.
|
|
93
138
|
*/
|
|
94
|
-
export function
|
|
139
|
+
export function artifactFilesFor(cwd, query) {
|
|
95
140
|
const found = [];
|
|
96
141
|
const walk = (dir, depth) => {
|
|
97
142
|
if (depth > 6 || found.length >= 1000)
|
|
@@ -111,7 +156,8 @@ export function markdownFilesFor(cwd, query) {
|
|
|
111
156
|
if (!SKIPPED_FOLDERS.has(entry.name))
|
|
112
157
|
walk(path, depth + 1);
|
|
113
158
|
}
|
|
114
|
-
else if (entry.isFile() &&
|
|
159
|
+
else if (entry.isFile() &&
|
|
160
|
+
!BINARY_EXTENSIONS.has(extname(entry.name).toLowerCase())) {
|
|
115
161
|
found.push(relative(cwd, path).split(sep).join("/"));
|
|
116
162
|
}
|
|
117
163
|
}
|
|
@@ -124,26 +170,58 @@ export function markdownFilesFor(cwd, query) {
|
|
|
124
170
|
};
|
|
125
171
|
return found
|
|
126
172
|
.filter((path) => path.toLowerCase().includes(needle))
|
|
127
|
-
.sort((a, b) => rank(a) - rank(b) ||
|
|
173
|
+
.sort((a, b) => rank(a) - rank(b) ||
|
|
174
|
+
Number(!isMarkdownPath(a)) - Number(!isMarkdownPath(b)) ||
|
|
175
|
+
a.localeCompare(b))
|
|
128
176
|
.slice(0, MAX_LISTED_FILES)
|
|
129
177
|
.map((path) => ({ value: path, label: path }));
|
|
130
178
|
}
|
|
131
179
|
/** File extensions the panel renders. */
|
|
132
180
|
const ARTIFACT_MARKDOWN_EXTENSIONS = [".md", ".markdown"];
|
|
181
|
+
/** The largest file the viewer reads. */
|
|
182
|
+
const ARTIFACT_MAX_BYTES = 1024 * 1024;
|
|
183
|
+
/** The largest file the viewer reads for a host that keeps large files in its store. */
|
|
184
|
+
const STORED_ARTIFACT_MAX_BYTES = 25 * 1024 * 1024;
|
|
185
|
+
/** The most of a file (and of its diff) a result's `details` carry, as a review's interrupt does. */
|
|
186
|
+
const ARTIFACT_DETAILS_MAX_BYTES = 1024 * 1024;
|
|
187
|
+
/** The bytes read to tell a text file from a binary one. */
|
|
188
|
+
const BINARY_SNIFF_BYTES = 8192;
|
|
189
|
+
/** `diff` against the version this session last showed. */
|
|
190
|
+
const DIFF_PREVIOUS = "previous";
|
|
191
|
+
/** A git revision: a name, a hash, or one with `~`/`^`/`@{…}`; never an option. */
|
|
192
|
+
const GIT_REF = /^[A-Za-z0-9_][\w./~^@{}-]*$/;
|
|
133
193
|
const BUILTIN_OWNER = "<builtin:show_artifact>";
|
|
134
194
|
const DEFAULT_DEBOUNCE_MS = 100;
|
|
135
195
|
const description = [
|
|
136
|
-
"Show a
|
|
137
|
-
"Call this when you want the user to read a
|
|
138
|
-
"`path` is relative to the working directory.
|
|
139
|
-
"
|
|
140
|
-
|
|
196
|
+
"Show a file to the user beside the conversation: markdown rendered, code and other text highlighted, HTML as source (a browser host renders it).",
|
|
197
|
+
"Call this when you want the user to read a file you wrote (a brief, test cases, a report, code), especially before asking them to approve it, instead of only printing its path.",
|
|
198
|
+
"`path` is relative to the working directory and must be a text file. `ref` and `diff` work like git, in the working directory's repository:",
|
|
199
|
+
"- `{ path }`: the file as it is now.",
|
|
200
|
+
'- `{ path, ref: "HEAD~1" }`: the file at a commit, like `git show HEAD~1:path`.',
|
|
201
|
+
'- `{ path, diff: "HEAD" }`: what changed since a commit, like `git diff HEAD -- path`.',
|
|
202
|
+
'- `{ path, ref: "HEAD", diff: "main" }`: what changed between two commits, like `git diff main HEAD -- path`.',
|
|
203
|
+
'- `{ path, diff: "previous" }`: what changed since you last showed it in this session; use it after revising a file the user is reading.',
|
|
204
|
+
"The panel re-renders when the file changes, so you don't need to call this again after editing a file it already shows.",
|
|
141
205
|
].join("\n");
|
|
142
206
|
const parameters = Type.Object({
|
|
143
207
|
path: Type.String({
|
|
144
|
-
description: 'The
|
|
208
|
+
description: 'The file to show, relative to the working directory (e.g. "evals/cases/refund.md").',
|
|
145
209
|
}),
|
|
210
|
+
ref: Type.Optional(Type.String({
|
|
211
|
+
description: "Show the file as of this git commit (a hash, a branch, HEAD~1) instead of as it is now.",
|
|
212
|
+
})),
|
|
213
|
+
diff: Type.Optional(Type.String({
|
|
214
|
+
description: `Show a diff instead of the whole file: "${DIFF_PREVIOUS}" for what changed since you last showed it, or a git ref to compare against.`,
|
|
215
|
+
})),
|
|
146
216
|
}, { additionalProperties: false });
|
|
217
|
+
/** The folder holding `path`, or its nearest existing parent, never above `root`. */
|
|
218
|
+
function nearestExistingFolder(path, root) {
|
|
219
|
+
let dir = dirname(path);
|
|
220
|
+
while (!existsSync(dir) && dir !== root && isInside(root, dirname(dir))) {
|
|
221
|
+
dir = dirname(dir);
|
|
222
|
+
}
|
|
223
|
+
return dir;
|
|
224
|
+
}
|
|
147
225
|
function isInside(root, target) {
|
|
148
226
|
const rel = relative(root, target);
|
|
149
227
|
return rel === "" || (!rel.startsWith("..") && !isAbsolute(rel));
|
|
@@ -152,23 +230,49 @@ function isInside(root, target) {
|
|
|
152
230
|
function isMarkdownPath(path) {
|
|
153
231
|
return ARTIFACT_MARKDOWN_EXTENSIONS.includes(extname(path).toLowerCase());
|
|
154
232
|
}
|
|
233
|
+
/** Whether a file's first bytes hold a NUL, as a binary file's do. */
|
|
234
|
+
function looksBinary(absolutePath) {
|
|
235
|
+
const fd = openSync(absolutePath, "r");
|
|
236
|
+
try {
|
|
237
|
+
const buffer = Buffer.alloc(BINARY_SNIFF_BYTES);
|
|
238
|
+
const read = readSync(fd, buffer, 0, buffer.length, 0);
|
|
239
|
+
return buffer.subarray(0, read).includes(0);
|
|
240
|
+
}
|
|
241
|
+
finally {
|
|
242
|
+
closeSync(fd);
|
|
243
|
+
}
|
|
244
|
+
}
|
|
155
245
|
/**
|
|
156
246
|
* Resolve `path` against `cwd` and check that it names an existing markdown
|
|
157
|
-
* file inside `cwd`, following symlinks
|
|
158
|
-
* act on.
|
|
247
|
+
* file inside `cwd`, following symlinks; with `anyText`, any text file up to
|
|
248
|
+
* `ARTIFACT_MAX_BYTES`. Throws an error the model or user can act on.
|
|
159
249
|
*/
|
|
160
|
-
export function resolveArtifactPath(cwd, path) {
|
|
250
|
+
export function resolveArtifactPath(cwd, path, { anyText = false, inWorkingTree = true, maxBytes = ARTIFACT_MAX_BYTES, } = {}) {
|
|
161
251
|
const input = path.trim();
|
|
162
252
|
if (!input)
|
|
163
|
-
throw new Error(
|
|
253
|
+
throw new Error(`Give the path of a ${anyText ? "" : "markdown "}file to show.`);
|
|
164
254
|
const root = resolve(cwd);
|
|
165
255
|
const absolutePath = resolve(root, input);
|
|
166
256
|
if (!isInside(root, absolutePath)) {
|
|
167
257
|
throw new Error(`${input} is outside the working directory; only files under ${root} can be shown.`);
|
|
168
258
|
}
|
|
169
|
-
if (!isMarkdownPath(absolutePath)) {
|
|
259
|
+
if (!anyText && !isMarkdownPath(absolutePath)) {
|
|
170
260
|
throw new Error(`${input} is not a markdown file; only ${ARTIFACT_MARKDOWN_EXTENSIONS.join(" and ")} files can be shown for now.`);
|
|
171
261
|
}
|
|
262
|
+
const resolved = {
|
|
263
|
+
absolutePath,
|
|
264
|
+
relativePath: relative(root, absolutePath).split(sep).join("/"),
|
|
265
|
+
};
|
|
266
|
+
if (!inWorkingTree) {
|
|
267
|
+
// The file need not exist, but git will run from its nearest existing
|
|
268
|
+
// folder, so that folder must not lead out of the working directory.
|
|
269
|
+
const existing = nearestExistingFolder(absolutePath, root);
|
|
270
|
+
if (existsSync(existing) &&
|
|
271
|
+
!isInside(realpathSync(root), realpathSync(existing))) {
|
|
272
|
+
throw new Error(`${input} links outside the working directory; only files under ${root} can be shown.`);
|
|
273
|
+
}
|
|
274
|
+
return resolved;
|
|
275
|
+
}
|
|
172
276
|
let stats;
|
|
173
277
|
try {
|
|
174
278
|
stats = statSync(absolutePath);
|
|
@@ -178,14 +282,17 @@ export function resolveArtifactPath(cwd, path) {
|
|
|
178
282
|
}
|
|
179
283
|
if (!stats.isFile())
|
|
180
284
|
throw new Error(`${input} is not a file.`);
|
|
285
|
+
if (anyText && stats.size > maxBytes) {
|
|
286
|
+
throw new Error(`${input} is ${Math.ceil(stats.size / 1024)} KB, over the ${maxBytes / 1024} KB the viewer reads; show a smaller file or a part of it.`);
|
|
287
|
+
}
|
|
288
|
+
if (anyText && looksBinary(absolutePath)) {
|
|
289
|
+
throw new Error(`${input} is not a text file, so it can't be shown.`);
|
|
290
|
+
}
|
|
181
291
|
// A symlink inside the working directory may still point outside it.
|
|
182
292
|
if (!isInside(realpathSync(root), realpathSync(absolutePath))) {
|
|
183
293
|
throw new Error(`${input} links outside the working directory; only files under ${root} can be shown.`);
|
|
184
294
|
}
|
|
185
|
-
return
|
|
186
|
-
absolutePath,
|
|
187
|
-
relativePath: relative(root, absolutePath).split(sep).join("/"),
|
|
188
|
-
};
|
|
295
|
+
return resolved;
|
|
189
296
|
}
|
|
190
297
|
/** The default `o` action: the platform's "open with default app" command. */
|
|
191
298
|
function openWithSystemApp(absolutePath) {
|
|
@@ -258,20 +365,246 @@ function workspaceRoot(target) {
|
|
|
258
365
|
return resolve(target.absolutePath, ...target.relativePath.split("/").map(() => ".."));
|
|
259
366
|
}
|
|
260
367
|
/**
|
|
261
|
-
*
|
|
262
|
-
*
|
|
263
|
-
*
|
|
368
|
+
* Where git runs for `target`, and how it names the file from there: the
|
|
369
|
+
* file's own folder (or its nearest existing parent, for a deleted file), so
|
|
370
|
+
* a repository nested below the working directory, such as a hosted
|
|
371
|
+
* workspace's `repos/<name>`, is the one asked.
|
|
264
372
|
*/
|
|
265
|
-
function
|
|
266
|
-
|
|
373
|
+
function gitLocation(target) {
|
|
374
|
+
const dir = nearestExistingFolder(target.absolutePath, workspaceRoot(target));
|
|
375
|
+
return {
|
|
376
|
+
dir,
|
|
377
|
+
path: `./${relative(dir, target.absolutePath).split(sep).join("/")}`,
|
|
378
|
+
};
|
|
379
|
+
}
|
|
380
|
+
/** A file at `ref` in its git repository. */
|
|
381
|
+
function gitVersion(target, ref, maxBytes = ARTIFACT_MAX_BYTES) {
|
|
382
|
+
if (!GIT_REF.test(ref)) {
|
|
383
|
+
throw new Error(`${ref} is not a git ref; give a commit hash, a branch or tag, or one like HEAD~1.`);
|
|
384
|
+
}
|
|
385
|
+
let text;
|
|
386
|
+
try {
|
|
387
|
+
// `./` resolves the path against git's folder, wherever the repository's root is.
|
|
388
|
+
const at = gitLocation(target);
|
|
389
|
+
text = execFileSync("git", ["show", `${ref}:${at.path}`], {
|
|
390
|
+
cwd: at.dir,
|
|
391
|
+
encoding: "utf8",
|
|
392
|
+
maxBuffer: maxBytes,
|
|
393
|
+
stdio: ["ignore", "pipe", "pipe"],
|
|
394
|
+
});
|
|
395
|
+
}
|
|
396
|
+
catch (error) {
|
|
397
|
+
if (error.code === "ENOBUFS") {
|
|
398
|
+
throw new Error(`${target.relativePath} at ${ref} is over the ${maxBytes / 1024} KB the viewer reads.`);
|
|
399
|
+
}
|
|
400
|
+
if (git(target, ["rev-parse", "--git-dir"]) === undefined) {
|
|
401
|
+
throw new Error(`${target.relativePath} isn't in a git repository, so it has no version at ${ref}.`);
|
|
402
|
+
}
|
|
403
|
+
throw new Error(`${target.relativePath} has no version at ${ref} in the working directory's git repository.`);
|
|
404
|
+
}
|
|
405
|
+
// The version at the ref gets the checks a working-tree file gets on disk.
|
|
406
|
+
if (text.includes("\0")) {
|
|
407
|
+
throw new Error(`${target.relativePath} at ${ref} is not a text file, so it can't be shown.`);
|
|
408
|
+
}
|
|
409
|
+
return text;
|
|
410
|
+
}
|
|
411
|
+
/**
|
|
412
|
+
* The file at `ref` as a diff's base: empty when the commit exists but the
|
|
413
|
+
* file was added since, as `git diff <ref> -- path` shows it all as added.
|
|
414
|
+
* A ref naming no commit still fails.
|
|
415
|
+
*/
|
|
416
|
+
function gitBaseVersion(target, ref) {
|
|
417
|
+
try {
|
|
418
|
+
return gitVersion(target, ref);
|
|
419
|
+
}
|
|
420
|
+
catch (error) {
|
|
421
|
+
const commit = GIT_REF.test(ref)
|
|
422
|
+
? git(target, ["rev-parse", "--verify", "--quiet", `${ref}^{commit}`])
|
|
423
|
+
: undefined;
|
|
424
|
+
const inCommit = commit === undefined
|
|
425
|
+
? undefined
|
|
426
|
+
: git(target, ["cat-file", "-e", `${ref}:${gitLocation(target).path}`]);
|
|
427
|
+
if (commit !== undefined && inCommit === undefined)
|
|
428
|
+
return "";
|
|
429
|
+
throw error;
|
|
430
|
+
}
|
|
431
|
+
}
|
|
432
|
+
/** Run git in the file's repository; undefined outside one or on failure. */
|
|
433
|
+
function git(target, args) {
|
|
434
|
+
try {
|
|
435
|
+
return execFileSync("git", args, {
|
|
436
|
+
cwd: gitLocation(target).dir,
|
|
437
|
+
encoding: "utf8",
|
|
438
|
+
maxBuffer: ARTIFACT_MAX_BYTES,
|
|
439
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
440
|
+
}).trim();
|
|
441
|
+
}
|
|
442
|
+
catch {
|
|
443
|
+
return undefined;
|
|
444
|
+
}
|
|
445
|
+
}
|
|
446
|
+
/** Run git in `dir`; undefined outside a repository or on failure (a non-zero exit). */
|
|
447
|
+
function gitIn(dir, args) {
|
|
448
|
+
try {
|
|
449
|
+
return execFileSync("git", args, {
|
|
450
|
+
cwd: dir,
|
|
451
|
+
encoding: "utf8",
|
|
452
|
+
maxBuffer: ARTIFACT_MAX_BYTES,
|
|
453
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
454
|
+
}).trim();
|
|
455
|
+
}
|
|
456
|
+
catch {
|
|
457
|
+
return undefined;
|
|
458
|
+
}
|
|
459
|
+
}
|
|
460
|
+
/**
|
|
461
|
+
* Where a host can read the shown file itself: its repository, commit and
|
|
462
|
+
* path, when the repository is one the host serves (`resolve`) and reads a file
|
|
463
|
+
* this size from (its `maxBytes`), the file is
|
|
464
|
+
* unchanged against that commit (`ref`, or HEAD for the working tree), and the
|
|
465
|
+
* commit is on a remote, so the host has it. Undefined otherwise; the result
|
|
466
|
+
* then carries the file.
|
|
467
|
+
*/
|
|
468
|
+
function repositorySource(target, ref, resolve, current) {
|
|
469
|
+
if (!resolve)
|
|
470
|
+
return undefined;
|
|
471
|
+
const bytes = new TextEncoder().encode(current).length;
|
|
472
|
+
const dir = dirname(target.absolutePath);
|
|
473
|
+
if (!existsSync(dir))
|
|
474
|
+
return undefined;
|
|
475
|
+
// A committed link holds its target's path, not the document shown: carry the content instead.
|
|
476
|
+
if (!ref && lstatSync(target.absolutePath).isSymbolicLink())
|
|
477
|
+
return undefined;
|
|
478
|
+
const root = gitIn(dir, ["rev-parse", "--show-toplevel"]);
|
|
479
|
+
if (!root)
|
|
480
|
+
return undefined;
|
|
481
|
+
const repository = resolve(root);
|
|
482
|
+
// Past what the host reads from a repository, the file is stored instead.
|
|
483
|
+
if (!repository || bytes > (repository.maxBytes ?? Infinity))
|
|
484
|
+
return undefined;
|
|
485
|
+
const path = relative(realpathSync(root), join(realpathSync(dir), basename(target.absolutePath)))
|
|
486
|
+
.split(sep)
|
|
487
|
+
.join("/");
|
|
488
|
+
if (!path || path.startsWith(".."))
|
|
489
|
+
return undefined;
|
|
490
|
+
const commit = gitIn(root, [
|
|
491
|
+
"rev-parse",
|
|
492
|
+
"--verify",
|
|
493
|
+
"--quiet",
|
|
494
|
+
`${ref ?? "HEAD"}^{commit}`,
|
|
495
|
+
]);
|
|
496
|
+
if (!commit)
|
|
497
|
+
return undefined;
|
|
498
|
+
// The content read must be the committed blob itself, or the host would show
|
|
499
|
+
// another version; git's own diff trusts the index (`assume-unchanged`).
|
|
500
|
+
let blob;
|
|
501
|
+
try {
|
|
502
|
+
blob = execFileSync("git", ["cat-file", "blob", `${commit}:${path}`], {
|
|
503
|
+
cwd: root,
|
|
504
|
+
encoding: "utf8",
|
|
505
|
+
maxBuffer: STORED_ARTIFACT_MAX_BYTES,
|
|
506
|
+
stdio: ["ignore", "pipe", "ignore"],
|
|
507
|
+
});
|
|
508
|
+
}
|
|
509
|
+
catch {
|
|
510
|
+
return undefined;
|
|
511
|
+
}
|
|
512
|
+
if (blob !== current)
|
|
513
|
+
return undefined;
|
|
514
|
+
if (!gitIn(root, ["branch", "-r", "--contains", commit]))
|
|
515
|
+
return undefined;
|
|
516
|
+
return { repository_id: repository.id, commit, path };
|
|
517
|
+
}
|
|
518
|
+
/** The branch (or short commit when detached) and whether the tree has uncommitted changes, as a zsh git prompt shows them. */
|
|
519
|
+
function gitState(target) {
|
|
520
|
+
const branch = git(target, ["rev-parse", "--abbrev-ref", "HEAD"]);
|
|
521
|
+
if (branch === undefined)
|
|
522
|
+
return undefined;
|
|
523
|
+
const name = branch === "HEAD"
|
|
524
|
+
? (git(target, ["rev-parse", "--short", "HEAD"]) ?? branch)
|
|
525
|
+
: branch;
|
|
526
|
+
const status = git(target, ["status", "--porcelain", "--untracked-files=no"]);
|
|
527
|
+
return { branch: name, dirty: Boolean(status) };
|
|
528
|
+
}
|
|
529
|
+
/** The file as `source` names it: at its ref, or as it is now. */
|
|
530
|
+
function readSource(source) {
|
|
531
|
+
if (source.ref)
|
|
532
|
+
return gitVersion(source.target, source.ref, source.maxBytes);
|
|
533
|
+
if (source.deleted && !existsSync(source.target.absolutePath))
|
|
534
|
+
return "";
|
|
535
|
+
// A re-read re-checks the path: the file may since have become a link out of the working directory.
|
|
536
|
+
resolveArtifactPath(workspaceRoot(source.target), source.target.relativePath, {
|
|
537
|
+
anyText: true,
|
|
538
|
+
...(source.maxBytes ? { maxBytes: source.maxBytes } : {}),
|
|
539
|
+
});
|
|
540
|
+
return readFileSync(source.target.absolutePath, "utf8");
|
|
541
|
+
}
|
|
542
|
+
/** Read an open file for the panel; a file that no longer passes reads as unreadable. */
|
|
543
|
+
function readDocument(source) {
|
|
544
|
+
const path = source.target.relativePath;
|
|
545
|
+
const language = getLanguageFromPath(path);
|
|
546
|
+
const title = source.ref ? `${path} @ ${source.ref}` : path;
|
|
547
|
+
let text;
|
|
267
548
|
try {
|
|
268
|
-
|
|
269
|
-
markdown = readFileSync(target.absolutePath, "utf8");
|
|
549
|
+
text = readSource(source);
|
|
270
550
|
}
|
|
271
551
|
catch {
|
|
272
|
-
|
|
552
|
+
text = undefined;
|
|
273
553
|
}
|
|
274
|
-
|
|
554
|
+
const state = gitState(source.target);
|
|
555
|
+
let diff;
|
|
556
|
+
// A diff of a file past the inline cap is too large to draw or carry.
|
|
557
|
+
const diffable = text !== undefined &&
|
|
558
|
+
new TextEncoder().encode(text).length <= ARTIFACT_DETAILS_MAX_BYTES;
|
|
559
|
+
if (source.base && text !== undefined && diffable) {
|
|
560
|
+
let baseText = source.base.text;
|
|
561
|
+
if (baseText === undefined && source.base.ref) {
|
|
562
|
+
try {
|
|
563
|
+
baseText = gitBaseVersion(source.target, source.base.ref);
|
|
564
|
+
}
|
|
565
|
+
catch {
|
|
566
|
+
baseText = undefined;
|
|
567
|
+
}
|
|
568
|
+
}
|
|
569
|
+
if (baseText !== undefined) {
|
|
570
|
+
const diffText = generateDiffString(baseText, text).diff;
|
|
571
|
+
diff = {
|
|
572
|
+
against: source.base.label,
|
|
573
|
+
text: diffText,
|
|
574
|
+
...artifactDiffCounts(diffText),
|
|
575
|
+
};
|
|
576
|
+
}
|
|
577
|
+
}
|
|
578
|
+
return {
|
|
579
|
+
title,
|
|
580
|
+
kind: artifactKind(path),
|
|
581
|
+
text,
|
|
582
|
+
language,
|
|
583
|
+
...(state ? { git: state } : {}),
|
|
584
|
+
...(diff ? { diff } : {}),
|
|
585
|
+
...(diff && source.diffFirst ? { diffFirst: true } : {}),
|
|
586
|
+
};
|
|
587
|
+
}
|
|
588
|
+
/** A document's diff for `details`, its text capped. */
|
|
589
|
+
function cappedDiff(diff) {
|
|
590
|
+
const text = capped(diff.text);
|
|
591
|
+
return {
|
|
592
|
+
against: diff.against,
|
|
593
|
+
text: text.text,
|
|
594
|
+
added: diff.added,
|
|
595
|
+
removed: diff.removed,
|
|
596
|
+
...(text.truncated ? { truncated: true } : {}),
|
|
597
|
+
};
|
|
598
|
+
}
|
|
599
|
+
/** `text` cut to `ARTIFACT_DETAILS_MAX_BYTES`, on a character boundary. */
|
|
600
|
+
function capped(text) {
|
|
601
|
+
const bytes = new TextEncoder().encode(text);
|
|
602
|
+
if (bytes.length <= ARTIFACT_DETAILS_MAX_BYTES)
|
|
603
|
+
return { text };
|
|
604
|
+
const kept = new TextDecoder()
|
|
605
|
+
.decode(bytes.subarray(0, ARTIFACT_DETAILS_MAX_BYTES))
|
|
606
|
+
.replace(/\uFFFD$/, "");
|
|
607
|
+
return { text: kept, truncated: true };
|
|
275
608
|
}
|
|
276
609
|
/**
|
|
277
610
|
* One session's viewer: at most one panel, the file it shows, and the last
|
|
@@ -280,6 +613,8 @@ function readDocument(target) {
|
|
|
280
613
|
class ArtifactViewer {
|
|
281
614
|
open;
|
|
282
615
|
last;
|
|
616
|
+
/** What the tool last showed of each path, for `diff: "previous"`. */
|
|
617
|
+
shownVersions = new Map();
|
|
283
618
|
openExternal;
|
|
284
619
|
watchFile;
|
|
285
620
|
debounceMs;
|
|
@@ -288,7 +623,7 @@ class ArtifactViewer {
|
|
|
288
623
|
this.watchFile = options.watchFile ?? watchFileWithFs;
|
|
289
624
|
this.debounceMs = options.debounceMs ?? DEFAULT_DEBOUNCE_MS;
|
|
290
625
|
}
|
|
291
|
-
/** The last
|
|
626
|
+
/** The last file shown or written, relative to the working directory. */
|
|
292
627
|
get lastPath() {
|
|
293
628
|
return this.last;
|
|
294
629
|
}
|
|
@@ -298,31 +633,39 @@ class ArtifactViewer {
|
|
|
298
633
|
get isHidden() {
|
|
299
634
|
return this.open?.handle?.isHidden() ?? false;
|
|
300
635
|
}
|
|
301
|
-
/**
|
|
636
|
+
/** What the tool last showed of `path`, if anything. */
|
|
637
|
+
shownVersion(path) {
|
|
638
|
+
return this.shownVersions.get(path);
|
|
639
|
+
}
|
|
640
|
+
/** Record what the tool showed of `path`. */
|
|
641
|
+
recordShown(path, text) {
|
|
642
|
+
this.shownVersions.set(path, text);
|
|
643
|
+
}
|
|
644
|
+
/** Remember a file the agent wrote, for `/recipe view` with no argument. */
|
|
302
645
|
remember(cwd, path) {
|
|
303
|
-
if (!isMarkdownPath(path))
|
|
304
|
-
return;
|
|
305
646
|
try {
|
|
306
|
-
this.last = resolveArtifactPath(cwd, path
|
|
647
|
+
this.last = resolveArtifactPath(cwd, path, {
|
|
648
|
+
anyText: true,
|
|
649
|
+
}).relativePath;
|
|
307
650
|
}
|
|
308
651
|
catch {
|
|
309
652
|
// Not a file we could show; keep the previous one.
|
|
310
653
|
}
|
|
311
654
|
}
|
|
312
655
|
/**
|
|
313
|
-
* Show `
|
|
656
|
+
* Show `source` in the panel, opening it if needed. The panel never takes
|
|
314
657
|
* focus unless `focus` is set.
|
|
315
658
|
*/
|
|
316
|
-
show(ctx,
|
|
317
|
-
this.last = target.relativePath;
|
|
659
|
+
show(ctx, source, focus = false) {
|
|
660
|
+
this.last = source.target.relativePath;
|
|
318
661
|
if (this.open) {
|
|
319
|
-
this.open.
|
|
320
|
-
// Before Pi builds the panel, the factory picks up the new
|
|
662
|
+
this.open.source = source;
|
|
663
|
+
// Before Pi builds the panel, the factory picks up the new source.
|
|
321
664
|
if (!this.open.panel)
|
|
322
665
|
return;
|
|
323
666
|
this.open.stopWatching();
|
|
324
|
-
this.open.panel.setDocument(readDocument(
|
|
325
|
-
this.open.stopWatching = this.watch(
|
|
667
|
+
this.open.panel.setDocument(readDocument(source));
|
|
668
|
+
this.open.stopWatching = this.watch(source);
|
|
326
669
|
if (focus)
|
|
327
670
|
this.focus();
|
|
328
671
|
return;
|
|
@@ -331,7 +674,7 @@ class ArtifactViewer {
|
|
|
331
674
|
const state = {
|
|
332
675
|
panel: undefined,
|
|
333
676
|
handle: undefined,
|
|
334
|
-
|
|
677
|
+
source,
|
|
335
678
|
close: () => done?.(undefined),
|
|
336
679
|
stopWatching: () => { },
|
|
337
680
|
};
|
|
@@ -340,13 +683,13 @@ class ArtifactViewer {
|
|
|
340
683
|
void ctx.ui
|
|
341
684
|
.custom((tui, theme, _keybindings, finish) => {
|
|
342
685
|
done = finish;
|
|
343
|
-
state.panel = new ArtifactPanel(tui, theme, readDocument(state.
|
|
686
|
+
state.panel = new ArtifactPanel(tui, theme, readDocument(state.source), {
|
|
344
687
|
openExternal: () => this.openCurrent(ctx),
|
|
345
688
|
unfocus: () => this.unfocus(),
|
|
346
689
|
close: () => state.close(),
|
|
347
690
|
disposed: () => this.release(state),
|
|
348
691
|
});
|
|
349
|
-
state.stopWatching = this.watch(state.
|
|
692
|
+
state.stopWatching = this.watch(state.source);
|
|
350
693
|
return state.panel;
|
|
351
694
|
}, {
|
|
352
695
|
overlay: true,
|
|
@@ -408,7 +751,7 @@ class ArtifactViewer {
|
|
|
408
751
|
this.open.panel?.requestRender();
|
|
409
752
|
}
|
|
410
753
|
openCurrent(ctx) {
|
|
411
|
-
const target = this.open?.target;
|
|
754
|
+
const target = this.open?.source.target;
|
|
412
755
|
if (!target)
|
|
413
756
|
return;
|
|
414
757
|
Promise.resolve()
|
|
@@ -417,16 +760,19 @@ class ArtifactViewer {
|
|
|
417
760
|
ctx.ui.notify(`Could not open ${target.relativePath}: ${error instanceof Error ? error.message : String(error)}`, "error");
|
|
418
761
|
});
|
|
419
762
|
}
|
|
420
|
-
watch(
|
|
763
|
+
watch(source) {
|
|
764
|
+
// A commit's version never changes, and its file may be gone from the working tree.
|
|
765
|
+
if (source.ref)
|
|
766
|
+
return () => { };
|
|
421
767
|
let timer;
|
|
422
|
-
const stop = this.watchFile(target.absolutePath, () => {
|
|
768
|
+
const stop = this.watchFile(source.target.absolutePath, () => {
|
|
423
769
|
if (timer)
|
|
424
770
|
clearTimeout(timer);
|
|
425
771
|
timer = setTimeout(() => {
|
|
426
772
|
timer = undefined;
|
|
427
|
-
if (this.open?.
|
|
773
|
+
if (this.open?.source !== source)
|
|
428
774
|
return;
|
|
429
|
-
this.open.panel?.setDocument(readDocument(
|
|
775
|
+
this.open.panel?.setDocument(readDocument(source), true);
|
|
430
776
|
}, this.debounceMs);
|
|
431
777
|
});
|
|
432
778
|
return () => {
|
|
@@ -452,32 +798,208 @@ class ArtifactViewer {
|
|
|
452
798
|
function errorText(error) {
|
|
453
799
|
return error instanceof Error ? error.message : String(error);
|
|
454
800
|
}
|
|
455
|
-
/**
|
|
456
|
-
|
|
801
|
+
/**
|
|
802
|
+
* What to show of `target`: at `ref` or as it is now, diffed against `diff`
|
|
803
|
+
* (`previous` or a git ref) when given. Records what is shown, for the next
|
|
804
|
+
* `previous`; `note` says when there was nothing earlier to compare against.
|
|
805
|
+
*/
|
|
806
|
+
function artifactSourceFor(viewer, target, ref, diff, deleted = false, maxBytes) {
|
|
807
|
+
const source = {
|
|
808
|
+
target,
|
|
809
|
+
...(ref ? { ref } : {}),
|
|
810
|
+
...(deleted ? { deleted } : {}),
|
|
811
|
+
...(maxBytes ? { maxBytes } : {}),
|
|
812
|
+
};
|
|
813
|
+
const current = readSource(source);
|
|
814
|
+
const against = diff?.trim();
|
|
815
|
+
const previous = viewer.shownVersion(target.relativePath);
|
|
816
|
+
const previousBase = previous === undefined
|
|
817
|
+
? undefined
|
|
818
|
+
: { label: "the version shown before", text: previous };
|
|
819
|
+
let note = "";
|
|
820
|
+
if (against === DIFF_PREVIOUS) {
|
|
821
|
+
if (previousBase)
|
|
822
|
+
source.base = previousBase;
|
|
823
|
+
else
|
|
824
|
+
note = ` Nothing of ${target.relativePath} was shown earlier in this session, so the whole file is shown.`;
|
|
825
|
+
}
|
|
826
|
+
else if (against) {
|
|
827
|
+
// Fails here, with the reason, when the ref names no commit.
|
|
828
|
+
const base = gitBaseVersion(target, against);
|
|
829
|
+
if (deleted && base === "") {
|
|
830
|
+
throw new Error(`${target.relativePath} does not exist, and isn't in ${against} either.`);
|
|
831
|
+
}
|
|
832
|
+
source.base = { label: against, ref: against };
|
|
833
|
+
}
|
|
834
|
+
else if (!ref || diff !== undefined) {
|
|
835
|
+
// With no diff named, `d` still offers one: against HEAD for a tracked file, else the version shown before.
|
|
836
|
+
const tracked = git(target, ["cat-file", "-e", `HEAD:${gitLocation(target).path}`]) !==
|
|
837
|
+
undefined;
|
|
838
|
+
if (tracked)
|
|
839
|
+
source.base = { label: "HEAD", ref: "HEAD" };
|
|
840
|
+
else if (previousBase)
|
|
841
|
+
source.base = previousBase;
|
|
842
|
+
if (deleted && !tracked) {
|
|
843
|
+
throw new Error(`${target.relativePath} does not exist, and isn't in HEAD either.`);
|
|
844
|
+
}
|
|
845
|
+
}
|
|
846
|
+
// A bare `diff` (`""`) opens on the diff `d` would show.
|
|
847
|
+
if (diff !== undefined && source.base)
|
|
848
|
+
source.diffFirst = true;
|
|
849
|
+
else if (diff !== undefined && !against && !source.base)
|
|
850
|
+
note = ` ${target.relativePath} isn't tracked by git and wasn't shown earlier in this session, so there is nothing to diff against.`;
|
|
851
|
+
viewer.recordShown(target.relativePath, current);
|
|
852
|
+
return { source, current, note };
|
|
853
|
+
}
|
|
854
|
+
/** `--name value`, `--name=value`, or a bare `--name` (value undefined). */
|
|
855
|
+
const COMMAND_OPTION = (name) => new RegExp(`(?:^|\\s)--${name}(?:(?:=|\\s+)(?!--)(\\S+))?(?=\\s|$)`);
|
|
856
|
+
/**
|
|
857
|
+
* Resolve a path to show. A file gone from the working tree passes only when
|
|
858
|
+
* a git base is asked for (`diff` other than `previous`, or a bare one, which
|
|
859
|
+
* diffs against HEAD): it then shows as removed, as `git diff <ref> -- path` does.
|
|
860
|
+
*/
|
|
861
|
+
function resolveShownPath(cwd, path, ref, diff, maxBytes = ARTIFACT_MAX_BYTES) {
|
|
862
|
+
if (ref) {
|
|
863
|
+
return {
|
|
864
|
+
target: resolveArtifactPath(cwd, path, {
|
|
865
|
+
anyText: true,
|
|
866
|
+
inWorkingTree: false,
|
|
867
|
+
maxBytes,
|
|
868
|
+
}),
|
|
869
|
+
deleted: false,
|
|
870
|
+
};
|
|
871
|
+
}
|
|
872
|
+
try {
|
|
873
|
+
return {
|
|
874
|
+
target: resolveArtifactPath(cwd, path, { anyText: true, maxBytes }),
|
|
875
|
+
deleted: false,
|
|
876
|
+
};
|
|
877
|
+
}
|
|
878
|
+
catch (error) {
|
|
879
|
+
const gitBase = diff !== undefined && diff.trim() !== DIFF_PREVIOUS;
|
|
880
|
+
if (!gitBase)
|
|
881
|
+
throw error;
|
|
882
|
+
const target = resolveArtifactPath(cwd, path, {
|
|
883
|
+
anyText: true,
|
|
884
|
+
inWorkingTree: false,
|
|
885
|
+
maxBytes,
|
|
886
|
+
});
|
|
887
|
+
// A file that exists but fails a check (binary, too large) still fails.
|
|
888
|
+
if (existsSync(target.absolutePath))
|
|
889
|
+
throw error;
|
|
890
|
+
return { target, deleted: true };
|
|
891
|
+
}
|
|
892
|
+
}
|
|
893
|
+
/**
|
|
894
|
+
* `/recipe view`'s arguments: a path, `--ref <ref>` and `--diff [<ref|previous>]`.
|
|
895
|
+
* A bare `--diff` (`diff: ""`) opens on the diff `d` would show.
|
|
896
|
+
*/
|
|
897
|
+
function commandArgs(args, cwd) {
|
|
898
|
+
let rest = args;
|
|
899
|
+
let spaced = false;
|
|
900
|
+
const option = (name) => {
|
|
901
|
+
const match = COMMAND_OPTION(name).exec(rest);
|
|
902
|
+
if (!match)
|
|
903
|
+
return undefined;
|
|
904
|
+
rest = rest.replace(match[0], " ");
|
|
905
|
+
if (name === "diff")
|
|
906
|
+
spaced = match[1] !== undefined && !match[0].includes("=");
|
|
907
|
+
return match[1] ?? "";
|
|
908
|
+
};
|
|
909
|
+
let diff = option("diff");
|
|
910
|
+
const ref = option("ref");
|
|
911
|
+
let path = commandPath(rest);
|
|
912
|
+
// `--diff <file>` with no other path, when that names a file: a bare `--diff` on it.
|
|
913
|
+
// `--diff=<ref>`, and a value naming no file, stay a base for the last file shown.
|
|
914
|
+
if (!path &&
|
|
915
|
+
diff &&
|
|
916
|
+
diff !== DIFF_PREVIOUS &&
|
|
917
|
+
spaced &&
|
|
918
|
+
existsSync(resolve(cwd, commandPath(diff)))) {
|
|
919
|
+
path = commandPath(diff);
|
|
920
|
+
diff = "";
|
|
921
|
+
}
|
|
922
|
+
return {
|
|
923
|
+
path,
|
|
924
|
+
...(ref ? { ref } : {}),
|
|
925
|
+
...(diff !== undefined ? { diff } : {}),
|
|
926
|
+
};
|
|
927
|
+
}
|
|
928
|
+
const ARTIFACT_MIME_TYPES = {
|
|
929
|
+
markdown: "text/markdown",
|
|
930
|
+
html: "text/html",
|
|
931
|
+
text: "text/plain",
|
|
932
|
+
};
|
|
933
|
+
/**
|
|
934
|
+
* Build the tool over a session's viewer. With `store`, a file past the
|
|
935
|
+
* inline cap is kept in the host's store during the call, so the result the
|
|
936
|
+
* conversation records names it (`file`) instead of carrying it.
|
|
937
|
+
*/
|
|
938
|
+
function createShowArtifactTool(viewer, store, resolveRepository) {
|
|
457
939
|
const tool = {
|
|
458
940
|
name: SHOW_ARTIFACT_TOOL_NAME,
|
|
459
941
|
label: "Show artifact",
|
|
460
942
|
description,
|
|
461
|
-
promptSnippet: "Show the user a
|
|
943
|
+
promptSnippet: "Show the user a file you wrote, or what changed in it, beside the conversation",
|
|
462
944
|
promptGuidelines: [
|
|
463
|
-
"When you write a
|
|
945
|
+
"When you write a file the user should read or approve (a brief, test cases, a report, code), show it with show_artifact instead of only printing its path; after revising one, show it with diff to point at what changed.",
|
|
464
946
|
],
|
|
465
947
|
parameters,
|
|
466
948
|
async execute(_toolCallId, params, _signal, _onUpdate, ctx) {
|
|
467
|
-
const
|
|
949
|
+
const ref = params.ref?.trim() || undefined;
|
|
950
|
+
const maxBytes = store || resolveRepository
|
|
951
|
+
? STORED_ARTIFACT_MAX_BYTES
|
|
952
|
+
: ARTIFACT_MAX_BYTES;
|
|
953
|
+
const { target, deleted } = resolveShownPath(ctx.cwd, params.path, ref, params.diff, maxBytes);
|
|
954
|
+
const { source, current, note } = artifactSourceFor(viewer, target, ref, params.diff, deleted, maxBytes);
|
|
955
|
+
const document = readDocument(source);
|
|
956
|
+
const kind = artifactKind(target.relativePath);
|
|
957
|
+
const bytes = new TextEncoder().encode(current).length;
|
|
958
|
+
// A committed file the host serves is read from its repository; otherwise it rides along, or is stored.
|
|
959
|
+
const committed = deleted
|
|
960
|
+
? undefined
|
|
961
|
+
: repositorySource(target, ref, resolveRepository, current);
|
|
962
|
+
// Stored now, so the recorded result names the file; a host's own sync recognises the same upload.
|
|
963
|
+
const stored = !committed && store && bytes > ARTIFACT_DETAILS_MAX_BYTES
|
|
964
|
+
? await store({
|
|
965
|
+
path: target.relativePath,
|
|
966
|
+
content: current,
|
|
967
|
+
mimeType: ARTIFACT_MIME_TYPES[kind],
|
|
968
|
+
})
|
|
969
|
+
: undefined;
|
|
970
|
+
const content = stored || committed ? { text: "" } : capped(current);
|
|
971
|
+
const details = {
|
|
972
|
+
path: target.relativePath,
|
|
973
|
+
shown: false,
|
|
974
|
+
kind,
|
|
975
|
+
...(document.language ? { language: document.language } : {}),
|
|
976
|
+
...(ref ? { ref } : {}),
|
|
977
|
+
content: content.text,
|
|
978
|
+
...("truncated" in content && content.truncated
|
|
979
|
+
? { truncated: true }
|
|
980
|
+
: {}),
|
|
981
|
+
...(stored ? { file: { id: stored.id, bytes } } : {}),
|
|
982
|
+
...(committed ? { source: committed } : {}),
|
|
983
|
+
...(document.git ? { git: document.git } : {}),
|
|
984
|
+
...(document.diff ? { diff: cappedDiff(document.diff) } : {}),
|
|
985
|
+
...(document.diffFirst ? { opens: "diff" } : {}),
|
|
986
|
+
};
|
|
987
|
+
const what = document.diffFirst
|
|
988
|
+
? `the diff of ${document.title} against ${document.diff.against}`
|
|
989
|
+
: document.title;
|
|
468
990
|
if (ctx.mode !== "tui") {
|
|
469
991
|
return {
|
|
470
992
|
content: [
|
|
471
993
|
{
|
|
472
994
|
type: "text",
|
|
473
|
-
text: `
|
|
995
|
+
text: `There is no artifact panel here; the result carries ${what} for a host that displays it (a browser does). If the user may not see it, tell them where the file is.${note}`,
|
|
474
996
|
},
|
|
475
997
|
],
|
|
476
|
-
details
|
|
998
|
+
details,
|
|
477
999
|
};
|
|
478
1000
|
}
|
|
479
1001
|
// It takes the keyboard as it opens, so Esc or q closes it without a shortcut first.
|
|
480
|
-
viewer.show(ctx,
|
|
1002
|
+
viewer.show(ctx, source, true);
|
|
481
1003
|
const columns = process.stdout.columns;
|
|
482
1004
|
if (columns !== undefined &&
|
|
483
1005
|
columns < ARTIFACT_PANEL_MIN_TERMINAL_COLUMNS) {
|
|
@@ -485,20 +1007,20 @@ function createShowArtifactTool(viewer) {
|
|
|
485
1007
|
content: [
|
|
486
1008
|
{
|
|
487
1009
|
type: "text",
|
|
488
|
-
text: `The terminal is ${columns} columns wide, too narrow for the panel (it needs ${ARTIFACT_PANEL_MIN_TERMINAL_COLUMNS}), so the user can't see ${target.relativePath}: tell them where the file is, or put what they need in your message. The panel appears if they widen the terminal
|
|
1010
|
+
text: `The terminal is ${columns} columns wide, too narrow for the panel (it needs ${ARTIFACT_PANEL_MIN_TERMINAL_COLUMNS}), so the user can't see ${target.relativePath}: tell them where the file is, or put what they need in your message. The panel appears if they widen the terminal.${note}`,
|
|
489
1011
|
},
|
|
490
1012
|
],
|
|
491
|
-
details
|
|
1013
|
+
details,
|
|
492
1014
|
};
|
|
493
1015
|
}
|
|
494
1016
|
return {
|
|
495
1017
|
content: [
|
|
496
1018
|
{
|
|
497
1019
|
type: "text",
|
|
498
|
-
text: `Showing ${
|
|
1020
|
+
text: `Showing ${what} in the panel on the right, which has the keyboard: the user scrolls it and closes it with Esc or q, which returns them to the editor; /${VIEW_ARTIFACT_COMMAND} reopens it.${note}`,
|
|
499
1021
|
},
|
|
500
1022
|
],
|
|
501
|
-
details: {
|
|
1023
|
+
details: { ...details, shown: true },
|
|
502
1024
|
};
|
|
503
1025
|
},
|
|
504
1026
|
};
|
|
@@ -530,26 +1052,31 @@ export function artifactViewerExtension(registrations, options = {}, agentTools
|
|
|
530
1052
|
if (registrations?.owner("tool", SHOW_ARTIFACT_TOOL_NAME))
|
|
531
1053
|
return;
|
|
532
1054
|
registrations?.claim("tool", SHOW_ARTIFACT_TOOL_NAME, BUILTIN_OWNER);
|
|
533
|
-
pi.registerTool(createShowArtifactTool(viewer));
|
|
1055
|
+
pi.registerTool(createShowArtifactTool(viewer, options.storeArtifact, options.artifactRepository));
|
|
534
1056
|
addRecipeSubcommand(pi, "view", async (args, ctx) => {
|
|
535
1057
|
if (ctx.mode !== "tui") {
|
|
536
1058
|
ctx.ui.notify(`/${VIEW_ARTIFACT_COMMAND} needs Pi's interactive terminal.`, "warning");
|
|
537
1059
|
return;
|
|
538
1060
|
}
|
|
539
|
-
const
|
|
1061
|
+
const parsed = commandArgs(args, ctx.cwd);
|
|
1062
|
+
const path = parsed.path || viewer.lastPath;
|
|
540
1063
|
if (!path) {
|
|
541
|
-
ctx.ui.notify(`Usage: /${VIEW_ARTIFACT_COMMAND} <file.
|
|
1064
|
+
ctx.ui.notify(`Usage: /${VIEW_ARTIFACT_COMMAND} <file> [--diff [<ref|previous>]] [--ref <ref>]. With no file, it shows the last file the agent showed or wrote.`, "info");
|
|
542
1065
|
return;
|
|
543
1066
|
}
|
|
544
1067
|
try {
|
|
545
|
-
|
|
1068
|
+
const { target, deleted } = resolveShownPath(ctx.cwd, path, parsed.ref, parsed.diff);
|
|
1069
|
+
const { source, note } = artifactSourceFor(viewer, target, parsed.ref, parsed.diff, deleted);
|
|
1070
|
+
viewer.show(ctx, source, true);
|
|
1071
|
+
if (note)
|
|
1072
|
+
ctx.ui.notify(note.trim(), "info");
|
|
546
1073
|
}
|
|
547
1074
|
catch (error) {
|
|
548
1075
|
ctx.ui.notify(errorText(error), "warning");
|
|
549
1076
|
}
|
|
550
1077
|
}, {
|
|
551
|
-
description: "Show a
|
|
552
|
-
complete: (argument, cwd) =>
|
|
1078
|
+
description: "Show a file in the panel on the right (--diff for what changed since HEAD, or --diff <ref|previous>; --ref <ref> for a commit's version)",
|
|
1079
|
+
complete: (argument, cwd) => artifactFilesFor(cwd, argument),
|
|
553
1080
|
});
|
|
554
1081
|
pi.on("tool_result", (event, ctx) => {
|
|
555
1082
|
if (event.isError)
|