@astrofoundry/pi-astro 0.7.0 → 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.
|
@@ -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
|
}
|