@astrofoundry/pi-astro 0.6.7 → 0.8.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/README.md +3 -1
- package/extensions/gemini-image/index.test.ts +81 -6
- package/extensions/gemini-image/index.ts +31 -9
- package/extensions/notify-on-stop/index.test.ts +67 -2
- package/extensions/notify-on-stop/index.ts +12 -2
- package/extensions/vscode-image/index.test.ts +114 -0
- package/extensions/vscode-image/index.ts +42 -0
- package/package.json +1 -1
package/README.md
CHANGED
|
@@ -62,7 +62,9 @@ pi # launch; confirm [Extensions] lists astro-agents, claude-glob
|
|
|
62
62
|
- `multi-edit` — registers the enhanced `edit` tool
|
|
63
63
|
- `gemini-image` — registers `gemini_image` tool (requires a Gemini API key; prompts and saves on first use)
|
|
64
64
|
- `security-guard` — blocks/prompts destructive bash commands and sensitive file access; configure at `~/.pi/agent/security-guard.json` (example written on first run); `/security-guard status|reload|test`
|
|
65
|
-
- `notify-on-stop` — runs a shell command when the agent finishes a turn (sound, voice, desktop notification).
|
|
65
|
+
- `notify-on-stop` — runs a shell command when the agent finishes a turn (sound, voice, desktop notification). macOS default: plays the Glass system sound **and** speaks "Agent done" via `say` using the **`Samantha (Enhanced)`** voice. Linux default: `notify-send "pi" "Agent done"`. Override the voice with `PI_STOP_NOTIFY_VOICE=<voice-name>` (macOS only; e.g. `Alex`, `Karen`, `Daniel (Enhanced)`). Replace the full command with `PI_STOP_NOTIFY='afplay /System/Library/Sounds/Glass.aiff && say "Done"'`. Disable everything with `PI_STOP_NOTIFY_OFF=1`. `/notify status|test|off`.
|
|
66
|
+
- **macOS voice install (required once for the default):** open **System Settings → Accessibility → Spoken Content → System Voice → Manage Voices…**, expand **English**, check **Samantha (Enhanced)**, click **Done** to download (~500 MB – 1 GB). Verify with `say -v "Samantha (Enhanced)" hi`. If the voice is missing, `say` errors silently and you'll only hear the Glass sound.
|
|
67
|
+
- `vscode-image` — only active inside VS Code's integrated terminal (`TERM_PROGRAM=vscode`). Switches pi-tui's image output to the **Kitty graphics protocol** so images returned by tools like `gemini_image` render as real pixels instead of the `[Image: …]` text fallback. **Requires enabling `Terminal › Integrated: Enable Images` in VS Code settings** (off by default); restart the integrated terminal after flipping it. Disable with `PI_VSCODE_IMAGE_OFF=1`. `/vscode-image` prints current status.
|
|
66
68
|
- `claude-globals` — auto-injects `~/.claude/CLAUDE.md` into every pi session's system prompt
|
|
67
69
|
|
|
68
70
|
**Bundled subagents** (callable via `astro_agent`):
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { mkdirSync, mkdtempSync, readdirSync, rmSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { tmpdir } from "node:os";
|
|
3
3
|
import { join } from "node:path";
|
|
4
4
|
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
@@ -131,7 +131,7 @@ describe("gemini-image extension", () => {
|
|
|
131
131
|
expect((res.content as Array<{ type: string }>)[1].type).toBe("image");
|
|
132
132
|
});
|
|
133
133
|
|
|
134
|
-
it("
|
|
134
|
+
it("saves under <cwd>/.gemini-images/<YYYY-MM-DD>/ with a zero-padded counter prefix by default", async () => {
|
|
135
135
|
pngImageResponse();
|
|
136
136
|
const { tool } = await load();
|
|
137
137
|
const res = await tool.execute(
|
|
@@ -143,11 +143,14 @@ describe("gemini-image extension", () => {
|
|
|
143
143
|
);
|
|
144
144
|
const paths = res.details.savedPaths as string[];
|
|
145
145
|
expect(paths).toHaveLength(1);
|
|
146
|
-
|
|
147
|
-
|
|
146
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
147
|
+
const dayDir = join(fakeCwd, ".gemini-images", today);
|
|
148
|
+
expect(paths[0]).toBe(join(dayDir, "000-a-thing.png"));
|
|
149
|
+
expect(readdirSync(join(fakeCwd, ".gemini-images"))).toEqual([today]);
|
|
150
|
+
expect(readdirSync(dayDir)).toEqual(["000-a-thing.png"]);
|
|
148
151
|
});
|
|
149
152
|
|
|
150
|
-
it("save_to override writes into
|
|
153
|
+
it("save_to override writes into <save_to>/<YYYY-MM-DD>/NNN-*.png", async () => {
|
|
151
154
|
pngImageResponse();
|
|
152
155
|
const { tool } = await load();
|
|
153
156
|
const res = await tool.execute(
|
|
@@ -158,7 +161,79 @@ describe("gemini-image extension", () => {
|
|
|
158
161
|
makeCtx({ cwd: fakeCwd }),
|
|
159
162
|
);
|
|
160
163
|
const paths = res.details.savedPaths as string[];
|
|
161
|
-
|
|
164
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
165
|
+
expect(paths[0]).toBe(join(fakeCwd, "out", today, "000-x.png"));
|
|
166
|
+
});
|
|
167
|
+
|
|
168
|
+
it("second call on the same day increments the counter (000 -> 001)", async () => {
|
|
169
|
+
pngImageResponse();
|
|
170
|
+
const { tool } = await load();
|
|
171
|
+
const first = await tool.execute(
|
|
172
|
+
"t",
|
|
173
|
+
{ prompt: "alpha", skip_confirm: true },
|
|
174
|
+
undefined,
|
|
175
|
+
undefined,
|
|
176
|
+
makeCtx({ cwd: fakeCwd }),
|
|
177
|
+
);
|
|
178
|
+
pngImageResponse();
|
|
179
|
+
const second = await tool.execute(
|
|
180
|
+
"t",
|
|
181
|
+
{ prompt: "beta", skip_confirm: true },
|
|
182
|
+
undefined,
|
|
183
|
+
undefined,
|
|
184
|
+
makeCtx({ cwd: fakeCwd }),
|
|
185
|
+
);
|
|
186
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
187
|
+
const dayDir = join(fakeCwd, ".gemini-images", today);
|
|
188
|
+
expect((first.details.savedPaths as string[])[0]).toBe(join(dayDir, "000-alpha.png"));
|
|
189
|
+
expect((second.details.savedPaths as string[])[0]).toBe(join(dayDir, "001-beta.png"));
|
|
190
|
+
});
|
|
191
|
+
|
|
192
|
+
it("counter resumes from max existing NNN prefix even if earlier entries were deleted", async () => {
|
|
193
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
194
|
+
const dayDir = join(fakeCwd, ".gemini-images", today);
|
|
195
|
+
mkdirSync(dayDir, { recursive: true });
|
|
196
|
+
writeFileSync(join(dayDir, "042-earlier.png"), "existing");
|
|
197
|
+
pngImageResponse();
|
|
198
|
+
const { tool } = await load();
|
|
199
|
+
const res = await tool.execute(
|
|
200
|
+
"t",
|
|
201
|
+
{ prompt: "gamma", skip_confirm: true },
|
|
202
|
+
undefined,
|
|
203
|
+
undefined,
|
|
204
|
+
makeCtx({ cwd: fakeCwd }),
|
|
205
|
+
);
|
|
206
|
+
expect((res.details.savedPaths as string[])[0]).toBe(join(dayDir, "043-gamma.png"));
|
|
207
|
+
});
|
|
208
|
+
|
|
209
|
+
it("multi-image call (Imagen) gets consecutive counters within the same call", async () => {
|
|
210
|
+
generateImagesMock.mockResolvedValue({
|
|
211
|
+
generatedImages: [
|
|
212
|
+
{ image: { imageBytes: "A", mimeType: "image/png" } },
|
|
213
|
+
{ image: { imageBytes: "B", mimeType: "image/png" } },
|
|
214
|
+
{ image: { imageBytes: "C", mimeType: "image/png" } },
|
|
215
|
+
],
|
|
216
|
+
});
|
|
217
|
+
const { tool } = await load();
|
|
218
|
+
const res = await tool.execute(
|
|
219
|
+
"t",
|
|
220
|
+
{
|
|
221
|
+
prompt: "batch",
|
|
222
|
+
model: "imagen-4.0-fast-generate-001",
|
|
223
|
+
number_of_images: 3,
|
|
224
|
+
skip_confirm: true,
|
|
225
|
+
},
|
|
226
|
+
undefined,
|
|
227
|
+
undefined,
|
|
228
|
+
makeCtx({ cwd: fakeCwd }),
|
|
229
|
+
);
|
|
230
|
+
const today = new Date().toISOString().slice(0, 10);
|
|
231
|
+
const dayDir = join(fakeCwd, ".gemini-images", today);
|
|
232
|
+
expect(res.details.savedPaths).toEqual([
|
|
233
|
+
join(dayDir, "000-batch.png"),
|
|
234
|
+
join(dayDir, "001-batch.png"),
|
|
235
|
+
join(dayDir, "002-batch.png"),
|
|
236
|
+
]);
|
|
162
237
|
});
|
|
163
238
|
|
|
164
239
|
it("silently saves API key on first paste (no confirm prompt)", async () => {
|
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
1
|
+
import { mkdirSync, readFileSync, readdirSync, writeFileSync } from "node:fs";
|
|
2
2
|
import { join, resolve } from "node:path";
|
|
3
3
|
import type { ExtensionAPI, ExtensionContext } from "@mariozechner/pi-coding-agent";
|
|
4
4
|
import type { AgentToolResult } from "@mariozechner/pi-agent-core";
|
|
@@ -51,7 +51,9 @@ const params = Type.Object({
|
|
|
51
51
|
}),
|
|
52
52
|
),
|
|
53
53
|
save_to: Type.Optional(
|
|
54
|
-
Type.String({
|
|
54
|
+
Type.String({
|
|
55
|
+
description: `Base directory to save PNG(s). Images are written to <save_to>/YYYY-MM-DD/NNN-<slug>.png where NNN is a zero-padded counter that resumes from the highest existing prefix in that day's folder. Default base: <cwd>/${DEFAULT_SAVE_SUBDIR}/.`,
|
|
56
|
+
}),
|
|
55
57
|
),
|
|
56
58
|
skip_confirm: Type.Optional(Type.Boolean({ description: "Skip the review/confirm prompt and call the API directly." })),
|
|
57
59
|
});
|
|
@@ -219,22 +221,42 @@ async function callImagen(
|
|
|
219
221
|
return { images };
|
|
220
222
|
}
|
|
221
223
|
|
|
222
|
-
function
|
|
224
|
+
function todayFolderName(): string {
|
|
225
|
+
return new Date().toISOString().slice(0, 10);
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
function nextCounter(dir: string): number {
|
|
229
|
+
const existing = readdirSync(dir);
|
|
230
|
+
let max = -1;
|
|
231
|
+
for (const name of existing) {
|
|
232
|
+
const match = /^(\d{3,})-/.exec(name);
|
|
233
|
+
if (!match) continue;
|
|
234
|
+
const n = parseInt(match[1], 10);
|
|
235
|
+
if (n > max) max = n;
|
|
236
|
+
}
|
|
237
|
+
return max + 1;
|
|
238
|
+
}
|
|
239
|
+
|
|
240
|
+
function saveImages(images: DecodedImage[], baseDir: string, prompt: string): string[] {
|
|
241
|
+
const dir = join(baseDir, todayFolderName());
|
|
223
242
|
mkdirSync(dir, { recursive: true });
|
|
243
|
+
|
|
224
244
|
const slug =
|
|
225
245
|
prompt
|
|
226
246
|
.toLowerCase()
|
|
227
247
|
.replace(/[^a-z0-9]+/g, "-")
|
|
228
248
|
.replace(/^-+|-+$/g, "")
|
|
229
249
|
.slice(0, 40) || "image";
|
|
230
|
-
|
|
250
|
+
|
|
251
|
+
let counter = nextCounter(dir);
|
|
231
252
|
const paths: string[] = [];
|
|
232
|
-
for (
|
|
233
|
-
const ext =
|
|
234
|
-
const
|
|
235
|
-
const full = join(dir,
|
|
236
|
-
writeFileSync(full, Buffer.from(
|
|
253
|
+
for (const img of images) {
|
|
254
|
+
const ext = img.mimeType === "image/jpeg" ? "jpg" : "png";
|
|
255
|
+
const prefix = String(counter).padStart(3, "0");
|
|
256
|
+
const full = join(dir, `${prefix}-${slug}.${ext}`);
|
|
257
|
+
writeFileSync(full, Buffer.from(img.data, "base64"));
|
|
237
258
|
paths.push(full);
|
|
259
|
+
counter++;
|
|
238
260
|
}
|
|
239
261
|
return paths;
|
|
240
262
|
}
|
|
@@ -51,12 +51,14 @@ function setPlatform(p: NodeJS.Platform): void {
|
|
|
51
51
|
describe("notify-on-stop", () => {
|
|
52
52
|
const origCmd = process.env.PI_STOP_NOTIFY;
|
|
53
53
|
const origOff = process.env.PI_STOP_NOTIFY_OFF;
|
|
54
|
+
const origVoice = process.env.PI_STOP_NOTIFY_VOICE;
|
|
54
55
|
|
|
55
56
|
beforeEach(() => {
|
|
56
57
|
vi.resetModules();
|
|
57
58
|
execMock.mockReset();
|
|
58
59
|
delete process.env.PI_STOP_NOTIFY;
|
|
59
60
|
delete process.env.PI_STOP_NOTIFY_OFF;
|
|
61
|
+
delete process.env.PI_STOP_NOTIFY_VOICE;
|
|
60
62
|
});
|
|
61
63
|
|
|
62
64
|
afterEach(() => {
|
|
@@ -65,16 +67,79 @@ describe("notify-on-stop", () => {
|
|
|
65
67
|
else process.env.PI_STOP_NOTIFY = origCmd;
|
|
66
68
|
if (origOff === undefined) delete process.env.PI_STOP_NOTIFY_OFF;
|
|
67
69
|
else process.env.PI_STOP_NOTIFY_OFF = origOff;
|
|
70
|
+
if (origVoice === undefined) delete process.env.PI_STOP_NOTIFY_VOICE;
|
|
71
|
+
else process.env.PI_STOP_NOTIFY_VOICE = origVoice;
|
|
68
72
|
});
|
|
69
73
|
|
|
70
|
-
it("runs default command on agent_end (macOS)", async () => {
|
|
74
|
+
it("runs default command on agent_end (macOS): afplay + say with Samantha (Enhanced)", async () => {
|
|
71
75
|
setPlatform("darwin");
|
|
72
76
|
const mod = await import("./index.ts");
|
|
73
77
|
const pi = makePi();
|
|
74
78
|
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
75
79
|
pi.handlers.agent_end({}, makeCtx());
|
|
76
80
|
expect(execMock).toHaveBeenCalled();
|
|
77
|
-
|
|
81
|
+
const cmd = execMock.mock.calls[0][0] as string;
|
|
82
|
+
expect(cmd).toMatch(/^afplay \/System\/Library\/Sounds\/Glass\.aiff && say -v /);
|
|
83
|
+
expect(cmd).toContain("'Samantha (Enhanced)'");
|
|
84
|
+
expect(cmd).toContain("'Agent done'");
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
it("PI_STOP_NOTIFY_VOICE overrides the default voice (macOS)", async () => {
|
|
88
|
+
setPlatform("darwin");
|
|
89
|
+
process.env.PI_STOP_NOTIFY_VOICE = "Alex";
|
|
90
|
+
const mod = await import("./index.ts");
|
|
91
|
+
const pi = makePi();
|
|
92
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
93
|
+
pi.handlers.agent_end({}, makeCtx());
|
|
94
|
+
const cmd = execMock.mock.calls[0][0] as string;
|
|
95
|
+
expect(cmd).toContain("say -v 'Alex'");
|
|
96
|
+
expect(cmd).not.toContain("Samantha");
|
|
97
|
+
});
|
|
98
|
+
|
|
99
|
+
it("PI_STOP_NOTIFY_VOICE with apostrophes is POSIX-safely quoted", async () => {
|
|
100
|
+
setPlatform("darwin");
|
|
101
|
+
process.env.PI_STOP_NOTIFY_VOICE = "O'Neill";
|
|
102
|
+
const mod = await import("./index.ts");
|
|
103
|
+
const pi = makePi();
|
|
104
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
105
|
+
pi.handlers.agent_end({}, makeCtx());
|
|
106
|
+
const cmd = execMock.mock.calls[0][0] as string;
|
|
107
|
+
expect(cmd).toContain("say -v 'O'\\''Neill'");
|
|
108
|
+
});
|
|
109
|
+
|
|
110
|
+
it("PI_STOP_NOTIFY_VOICE with only whitespace falls back to default voice (macOS)", async () => {
|
|
111
|
+
setPlatform("darwin");
|
|
112
|
+
process.env.PI_STOP_NOTIFY_VOICE = " ";
|
|
113
|
+
const mod = await import("./index.ts");
|
|
114
|
+
const pi = makePi();
|
|
115
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
116
|
+
pi.handlers.agent_end({}, makeCtx());
|
|
117
|
+
const cmd = execMock.mock.calls[0][0] as string;
|
|
118
|
+
expect(cmd).toContain("'Samantha (Enhanced)'");
|
|
119
|
+
});
|
|
120
|
+
|
|
121
|
+
it("PI_STOP_NOTIFY_VOICE is ignored when PI_STOP_NOTIFY overrides the full command", async () => {
|
|
122
|
+
setPlatform("darwin");
|
|
123
|
+
process.env.PI_STOP_NOTIFY = "my custom --cmd";
|
|
124
|
+
process.env.PI_STOP_NOTIFY_VOICE = "Alex";
|
|
125
|
+
const mod = await import("./index.ts");
|
|
126
|
+
const pi = makePi();
|
|
127
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
128
|
+
pi.handlers.agent_end({}, makeCtx());
|
|
129
|
+
expect(execMock.mock.calls[0][0]).toBe("my custom --cmd");
|
|
130
|
+
});
|
|
131
|
+
|
|
132
|
+
it("PI_STOP_NOTIFY_VOICE has no effect on Linux default", async () => {
|
|
133
|
+
setPlatform("linux");
|
|
134
|
+
process.env.PI_STOP_NOTIFY_VOICE = "Alex";
|
|
135
|
+
const mod = await import("./index.ts");
|
|
136
|
+
const pi = makePi();
|
|
137
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
138
|
+
pi.handlers.agent_end({}, makeCtx());
|
|
139
|
+
const cmd = execMock.mock.calls[0][0] as string;
|
|
140
|
+
expect(cmd).toMatch(/notify-send/);
|
|
141
|
+
expect(cmd).not.toContain("say");
|
|
142
|
+
expect(cmd).not.toContain("Alex");
|
|
78
143
|
});
|
|
79
144
|
|
|
80
145
|
it("runs custom PI_STOP_NOTIFY over default", async () => {
|
|
@@ -3,11 +3,21 @@ import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
|
3
3
|
|
|
4
4
|
const ENV_CMD = "PI_STOP_NOTIFY";
|
|
5
5
|
const ENV_OFF = "PI_STOP_NOTIFY_OFF";
|
|
6
|
+
const ENV_VOICE = "PI_STOP_NOTIFY_VOICE";
|
|
6
7
|
const TIMEOUT_MS = 10_000;
|
|
8
|
+
const DEFAULT_VOICE = "Samantha (Enhanced)";
|
|
9
|
+
const MACOS_SPOKEN_MESSAGE = "Agent done";
|
|
10
|
+
|
|
11
|
+
function shellQuote(value: string): string {
|
|
12
|
+
return `'${value.replace(/'/g, "'\\''")}'`;
|
|
13
|
+
}
|
|
7
14
|
|
|
8
15
|
function defaultCommand(): string | undefined {
|
|
9
16
|
if (process.platform === "darwin") {
|
|
10
|
-
|
|
17
|
+
const voice = process.env[ENV_VOICE]?.trim() || DEFAULT_VOICE;
|
|
18
|
+
const sound = "afplay /System/Library/Sounds/Glass.aiff";
|
|
19
|
+
const speak = `say -v ${shellQuote(voice)} ${shellQuote(MACOS_SPOKEN_MESSAGE)}`;
|
|
20
|
+
return `${sound} && ${speak}`;
|
|
11
21
|
}
|
|
12
22
|
if (process.platform === "linux") {
|
|
13
23
|
return 'command -v notify-send >/dev/null 2>&1 && notify-send "pi" "Agent done"';
|
|
@@ -39,7 +49,7 @@ export default function notifyOnStopExtension(pi: ExtensionAPI): void {
|
|
|
39
49
|
|
|
40
50
|
pi.registerCommand("notify", {
|
|
41
51
|
description:
|
|
42
|
-
"Stop-notify status/test. Usage: /notify [status|test|off]. Configure via env PI_STOP_NOTIFY
|
|
52
|
+
"Stop-notify status/test. Usage: /notify [status|test|off]. Configure via env PI_STOP_NOTIFY (full override) or PI_STOP_NOTIFY_VOICE (macOS default voice); disable with PI_STOP_NOTIFY_OFF=1.",
|
|
43
53
|
handler: async (args, ctx) => {
|
|
44
54
|
const sub = args.trim().toLowerCase() || "status";
|
|
45
55
|
if (sub === "test") {
|
|
@@ -0,0 +1,114 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
|
|
3
|
+
const setCapabilitiesMock = vi.fn();
|
|
4
|
+
|
|
5
|
+
vi.mock("@mariozechner/pi-tui", () => ({
|
|
6
|
+
setCapabilities: setCapabilitiesMock,
|
|
7
|
+
}));
|
|
8
|
+
|
|
9
|
+
type CommandHandler = (args: string, ctx: unknown) => Promise<void> | void;
|
|
10
|
+
|
|
11
|
+
function makePi(): {
|
|
12
|
+
commands: Map<string, { handler: CommandHandler }>;
|
|
13
|
+
registerCommand: (name: string, opts: { description?: string; handler: CommandHandler }) => void;
|
|
14
|
+
} {
|
|
15
|
+
const commands = new Map<string, { handler: CommandHandler }>();
|
|
16
|
+
return {
|
|
17
|
+
commands,
|
|
18
|
+
registerCommand: (name, opts) => {
|
|
19
|
+
commands.set(name, { handler: opts.handler });
|
|
20
|
+
},
|
|
21
|
+
};
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
function makeCtx(): { ui: { notify: ReturnType<typeof vi.fn> } } {
|
|
25
|
+
return { ui: { notify: vi.fn() } };
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
describe("vscode-image", () => {
|
|
29
|
+
const origTerm = process.env.TERM_PROGRAM;
|
|
30
|
+
const origOff = process.env.PI_VSCODE_IMAGE_OFF;
|
|
31
|
+
|
|
32
|
+
beforeEach(() => {
|
|
33
|
+
vi.resetModules();
|
|
34
|
+
setCapabilitiesMock.mockReset();
|
|
35
|
+
delete process.env.TERM_PROGRAM;
|
|
36
|
+
delete process.env.PI_VSCODE_IMAGE_OFF;
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
afterEach(() => {
|
|
40
|
+
if (origTerm === undefined) delete process.env.TERM_PROGRAM;
|
|
41
|
+
else process.env.TERM_PROGRAM = origTerm;
|
|
42
|
+
if (origOff === undefined) delete process.env.PI_VSCODE_IMAGE_OFF;
|
|
43
|
+
else process.env.PI_VSCODE_IMAGE_OFF = origOff;
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("does nothing when TERM_PROGRAM is unset", async () => {
|
|
47
|
+
const mod = await import("./index.ts");
|
|
48
|
+
const pi = makePi();
|
|
49
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
50
|
+
expect(setCapabilitiesMock).not.toHaveBeenCalled();
|
|
51
|
+
expect(pi.commands.has("vscode-image")).toBe(false);
|
|
52
|
+
});
|
|
53
|
+
|
|
54
|
+
it("does nothing when TERM_PROGRAM is not vscode", async () => {
|
|
55
|
+
process.env.TERM_PROGRAM = "iterm.app";
|
|
56
|
+
const mod = await import("./index.ts");
|
|
57
|
+
const pi = makePi();
|
|
58
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
59
|
+
expect(setCapabilitiesMock).not.toHaveBeenCalled();
|
|
60
|
+
expect(pi.commands.has("vscode-image")).toBe(false);
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
it("enables Kitty protocol when TERM_PROGRAM=vscode", async () => {
|
|
64
|
+
process.env.TERM_PROGRAM = "vscode";
|
|
65
|
+
const mod = await import("./index.ts");
|
|
66
|
+
const pi = makePi();
|
|
67
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
68
|
+
expect(setCapabilitiesMock).toHaveBeenCalledWith({
|
|
69
|
+
images: "kitty",
|
|
70
|
+
trueColor: true,
|
|
71
|
+
hyperlinks: true,
|
|
72
|
+
});
|
|
73
|
+
expect(pi.commands.has("vscode-image")).toBe(true);
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
it("matches TERM_PROGRAM case-insensitively", async () => {
|
|
77
|
+
process.env.TERM_PROGRAM = "VSCode";
|
|
78
|
+
const mod = await import("./index.ts");
|
|
79
|
+
const pi = makePi();
|
|
80
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
81
|
+
expect(setCapabilitiesMock).toHaveBeenCalled();
|
|
82
|
+
});
|
|
83
|
+
|
|
84
|
+
it("skips setCapabilities when PI_VSCODE_IMAGE_OFF=1 but still registers command", async () => {
|
|
85
|
+
process.env.TERM_PROGRAM = "vscode";
|
|
86
|
+
process.env.PI_VSCODE_IMAGE_OFF = "1";
|
|
87
|
+
const mod = await import("./index.ts");
|
|
88
|
+
const pi = makePi();
|
|
89
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
90
|
+
expect(setCapabilitiesMock).not.toHaveBeenCalled();
|
|
91
|
+
expect(pi.commands.has("vscode-image")).toBe(true);
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
it("/vscode-image reports enabled state", async () => {
|
|
95
|
+
process.env.TERM_PROGRAM = "vscode";
|
|
96
|
+
const mod = await import("./index.ts");
|
|
97
|
+
const pi = makePi();
|
|
98
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
99
|
+
const ctx = makeCtx();
|
|
100
|
+
await pi.commands.get("vscode-image")!.handler("", ctx);
|
|
101
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringMatching(/Kitty/), "info");
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
it("/vscode-image reports disabled when PI_VSCODE_IMAGE_OFF=1", async () => {
|
|
105
|
+
process.env.TERM_PROGRAM = "vscode";
|
|
106
|
+
process.env.PI_VSCODE_IMAGE_OFF = "1";
|
|
107
|
+
const mod = await import("./index.ts");
|
|
108
|
+
const pi = makePi();
|
|
109
|
+
mod.default(pi as unknown as Parameters<typeof mod.default>[0]);
|
|
110
|
+
const ctx = makeCtx();
|
|
111
|
+
await pi.commands.get("vscode-image")!.handler("", ctx);
|
|
112
|
+
expect(ctx.ui.notify).toHaveBeenCalledWith(expect.stringMatching(/disabled/), "info");
|
|
113
|
+
});
|
|
114
|
+
});
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { ExtensionAPI } from "@mariozechner/pi-coding-agent";
|
|
2
|
+
import { setCapabilities } from "@mariozechner/pi-tui";
|
|
3
|
+
|
|
4
|
+
const ENV_OFF = "PI_VSCODE_IMAGE_OFF";
|
|
5
|
+
|
|
6
|
+
function inVSCode(): boolean {
|
|
7
|
+
return process.env.TERM_PROGRAM?.toLowerCase() === "vscode";
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
function isDisabled(): boolean {
|
|
11
|
+
return process.env[ENV_OFF] === "1";
|
|
12
|
+
}
|
|
13
|
+
|
|
14
|
+
export default function vscodeImageExtension(pi: ExtensionAPI): void {
|
|
15
|
+
if (!inVSCode()) return;
|
|
16
|
+
|
|
17
|
+
if (!isDisabled()) {
|
|
18
|
+
setCapabilities({
|
|
19
|
+
images: "kitty",
|
|
20
|
+
trueColor: true,
|
|
21
|
+
hyperlinks: true,
|
|
22
|
+
});
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
pi.registerCommand("vscode-image", {
|
|
26
|
+
description:
|
|
27
|
+
"VSCode image-protocol status. Usage: /vscode-image. Disable with PI_VSCODE_IMAGE_OFF=1 (requires pi restart).",
|
|
28
|
+
handler: async (_args, ctx) => {
|
|
29
|
+
if (isDisabled()) {
|
|
30
|
+
ctx.ui.notify(
|
|
31
|
+
`vscode-image: disabled (${ENV_OFF}=1). Unset the env var and restart pi to re-enable.`,
|
|
32
|
+
"info",
|
|
33
|
+
);
|
|
34
|
+
return;
|
|
35
|
+
}
|
|
36
|
+
ctx.ui.notify(
|
|
37
|
+
"vscode-image: Kitty graphics protocol enabled. Ensure \"Terminal > Integrated: Enable Images\" is ON in VS Code settings.",
|
|
38
|
+
"info",
|
|
39
|
+
);
|
|
40
|
+
},
|
|
41
|
+
});
|
|
42
|
+
}
|