vibedgames 0.4.1 → 0.5.1
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 +1 -1
- package/dist/commands/completions.d.ts +1 -1
- package/dist/commands/completions.js +21 -20
- package/dist/commands/credits.d.ts +5 -5
- package/dist/commands/credits.js +45 -47
- package/dist/commands/deploy.d.ts +10 -10
- package/dist/commands/deploy.js +157 -137
- package/dist/commands/factory.d.ts +1 -1
- package/dist/commands/factory.js +18 -15
- package/dist/commands/fork.d.ts +14 -14
- package/dist/commands/fork.js +53 -45
- package/dist/commands/generate.js +343 -286
- package/dist/commands/init.d.ts +8 -8
- package/dist/commands/init.js +31 -24
- package/dist/commands/login.js +14 -14
- package/dist/commands/logout.js +2 -2
- package/dist/commands/new.d.ts +16 -16
- package/dist/commands/new.js +191 -165
- package/dist/commands/playtest.d.ts +5 -5
- package/dist/commands/playtest.js +103 -97
- package/dist/commands/update.d.ts +7 -7
- package/dist/commands/update.js +22 -17
- package/dist/commands/whoami.d.ts +5 -5
- package/dist/commands/whoami.js +11 -10
- package/dist/index.js +12 -10
- package/dist/lib/api.d.ts +16 -4
- package/dist/lib/api.js +20 -32
- package/dist/lib/archive.d.ts +4 -4
- package/dist/lib/archive.js +123 -102
- package/dist/lib/codex.d.ts +46 -15
- package/dist/lib/codex.js +178 -108
- package/dist/lib/config-file.d.ts +6 -6
- package/dist/lib/config-file.js +33 -30
- package/dist/lib/config.d.ts +8 -8
- package/dist/lib/config.js +38 -30
- package/dist/lib/manifest.d.ts +3 -3
- package/dist/lib/manifest.js +44 -40
- package/dist/lib/media-args.d.ts +8 -6
- package/dist/lib/media-args.js +113 -94
- package/dist/lib/media-download.d.ts +9 -8
- package/dist/lib/media-download.js +158 -144
- package/dist/lib/media-poll.d.ts +8 -7
- package/dist/lib/media-poll.js +38 -32
- package/dist/lib/media-upload.d.ts +1 -1
- package/dist/lib/media-upload.js +30 -29
- package/dist/lib/output.d.ts +34 -11
- package/dist/lib/output.js +28 -20
- package/dist/lib/package-manager.d.ts +3 -3
- package/dist/lib/package-manager.js +27 -22
- package/dist/lib/run.d.ts +4 -4
- package/dist/lib/run.js +7 -4
- package/dist/lib/strict-args.d.ts +4 -4
- package/dist/lib/strict-args.js +29 -19
- package/dist/lib/types.d.ts +16 -2
- package/dist/lib/types.js +10 -4
- package/dist/lib/update.d.ts +4 -4
- package/dist/lib/update.js +47 -35
- package/dist/lib/upload.d.ts +4 -4
- package/dist/lib/upload.js +14 -9
- package/package.json +7 -8
package/dist/lib/codex.js
CHANGED
|
@@ -1,10 +1,11 @@
|
|
|
1
1
|
import { randomBytes } from "node:crypto";
|
|
2
|
-
import { copyFileSync, mkdirSync, mkdtempSync, readdirSync, statSync } from "node:fs";
|
|
2
|
+
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, statSync } from "node:fs";
|
|
3
3
|
import { homedir, tmpdir } from "node:os";
|
|
4
|
-
import
|
|
4
|
+
import path from "node:path";
|
|
5
5
|
import spawn from "cross-spawn";
|
|
6
6
|
import { readExplicitLocalFile } from "./media-args.js";
|
|
7
7
|
import { disambiguateTargets } from "./media-download.js";
|
|
8
|
+
import { isJsonNumber, isJsonString } from "./types.js";
|
|
8
9
|
/**
|
|
9
10
|
* Expected, user-facing failure of the codex provider. Carries a clean
|
|
10
11
|
* message plus any output Codex itself emitted, so the caller can present
|
|
@@ -22,20 +23,52 @@ export class CodexError extends Error {
|
|
|
22
23
|
}
|
|
23
24
|
}
|
|
24
25
|
/**
|
|
25
|
-
* Resolve
|
|
26
|
-
* to the `VG_GENERATE_PROVIDER` env var and finally the vibedgames
|
|
26
|
+
* Resolve an explicitly named provider from the `--provider` flag, falling
|
|
27
|
+
* back to the `VG_GENERATE_PROVIDER` env var and finally the vibedgames
|
|
27
28
|
* default. Unknown values throw so a typo (`--provider coddex`) fails
|
|
28
|
-
* loudly instead of silently hitting the paid backend.
|
|
29
|
+
* loudly instead of silently hitting the paid backend. `chooseProvider`
|
|
30
|
+
* layers the automatic Codex routing on top of this.
|
|
29
31
|
*/
|
|
30
|
-
export
|
|
32
|
+
export const resolveProvider = (flag) => {
|
|
31
33
|
const raw = (flag ?? process.env.VG_GENERATE_PROVIDER ?? "").trim().toLowerCase();
|
|
32
|
-
if (raw === "codex")
|
|
34
|
+
if (raw === "codex") {
|
|
33
35
|
return "codex";
|
|
36
|
+
}
|
|
34
37
|
if (raw === "" || raw === "vibedgames" || raw === "fal" || raw === "default") {
|
|
35
38
|
return "vibedgames";
|
|
36
39
|
}
|
|
37
40
|
throw new Error(`Unknown --provider "${raw}". Supported: vibedgames (default), codex.`);
|
|
38
|
-
}
|
|
41
|
+
};
|
|
42
|
+
/** The OpenAI image family — the models a signed-in Codex plan serves itself. */
|
|
43
|
+
export const isOpenAiImageEndpoint = (endpointId) => endpointId === "codex" || /^openai\/gpt-image/iu.test(endpointId);
|
|
44
|
+
/**
|
|
45
|
+
* The `codex` binary this machine would run: `VG_CODEX_BIN` when set (and
|
|
46
|
+
* present), else the first `codex` on PATH. Null when there is none — the
|
|
47
|
+
* signal that keeps automatic routing on the vibedgames runner.
|
|
48
|
+
*/
|
|
49
|
+
export const findCodexBinary = (env = process.env) => {
|
|
50
|
+
const pinned = env.VG_CODEX_BIN;
|
|
51
|
+
if (pinned) {
|
|
52
|
+
return existsSync(pinned) ? pinned : null;
|
|
53
|
+
}
|
|
54
|
+
for (const dir of (env.PATH ?? "").split(path.delimiter)) {
|
|
55
|
+
if (!dir) {
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
for (const name of ["codex", "codex.cmd"]) {
|
|
59
|
+
const candidate = path.join(dir, name);
|
|
60
|
+
try {
|
|
61
|
+
if (statSync(candidate).isFile()) {
|
|
62
|
+
return candidate;
|
|
63
|
+
}
|
|
64
|
+
}
|
|
65
|
+
catch {
|
|
66
|
+
// not here; keep walking PATH
|
|
67
|
+
}
|
|
68
|
+
}
|
|
69
|
+
}
|
|
70
|
+
return null;
|
|
71
|
+
};
|
|
39
72
|
// Input keys we map onto Codex's natural-language image request. Codex
|
|
40
73
|
// only exposes a single built-in image model, so most fal-style params
|
|
41
74
|
// don't apply — we fold the few that have a meaning (prompt, count,
|
|
@@ -54,16 +87,33 @@ const REF_KEYS = [
|
|
|
54
87
|
];
|
|
55
88
|
const MAX_IMAGES = 8;
|
|
56
89
|
const IMAGE_EXT = new Set(["png", "jpg", "jpeg", "webp", "gif"]);
|
|
90
|
+
const buildSizeHint = (input) => {
|
|
91
|
+
const parts = [];
|
|
92
|
+
const size = input.image_size ?? input.size;
|
|
93
|
+
if (isJsonString(size) && size.trim().length > 0) {
|
|
94
|
+
parts.push(size.trim());
|
|
95
|
+
}
|
|
96
|
+
const aspect = input.aspect_ratio;
|
|
97
|
+
if (isJsonString(aspect) && aspect.trim().length > 0) {
|
|
98
|
+
parts.push(`aspect ratio ${aspect.trim()}`);
|
|
99
|
+
}
|
|
100
|
+
const w = input.width;
|
|
101
|
+
const h = input.height;
|
|
102
|
+
if (isJsonNumber(w) && isJsonNumber(h)) {
|
|
103
|
+
parts.push(`${w}x${h}px`);
|
|
104
|
+
}
|
|
105
|
+
return parts.length > 0 ? parts.join(", ") : undefined;
|
|
106
|
+
};
|
|
57
107
|
/**
|
|
58
108
|
* Pull the fields Codex can act on out of the parsed `run` input. Pure:
|
|
59
109
|
* no filesystem access, so the reference values are returned verbatim and
|
|
60
110
|
* resolved to local files later.
|
|
61
111
|
*/
|
|
62
|
-
export
|
|
112
|
+
export const parseCodexInput = (input) => {
|
|
63
113
|
let prompt = "";
|
|
64
114
|
for (const key of PROMPT_KEYS) {
|
|
65
115
|
const v = input[key];
|
|
66
|
-
if (
|
|
116
|
+
if (isJsonString(v) && v.trim().length > 0) {
|
|
67
117
|
prompt = v.trim();
|
|
68
118
|
break;
|
|
69
119
|
}
|
|
@@ -71,7 +121,7 @@ export function parseCodexInput(input) {
|
|
|
71
121
|
let count = 1;
|
|
72
122
|
for (const key of COUNT_KEYS) {
|
|
73
123
|
const v = input[key];
|
|
74
|
-
if (
|
|
124
|
+
if (isJsonNumber(v)) {
|
|
75
125
|
count = Math.max(1, Math.min(MAX_IMAGES, Math.floor(v)));
|
|
76
126
|
break;
|
|
77
127
|
}
|
|
@@ -79,37 +129,47 @@ export function parseCodexInput(input) {
|
|
|
79
129
|
const referenceCandidates = [];
|
|
80
130
|
for (const key of REF_KEYS) {
|
|
81
131
|
const v = input[key];
|
|
82
|
-
if (
|
|
132
|
+
if (isJsonString(v)) {
|
|
83
133
|
referenceCandidates.push(v);
|
|
134
|
+
}
|
|
84
135
|
else if (Array.isArray(v)) {
|
|
85
|
-
for (const item of v)
|
|
86
|
-
if (
|
|
136
|
+
for (const item of v) {
|
|
137
|
+
if (isJsonString(item)) {
|
|
87
138
|
referenceCandidates.push(item);
|
|
139
|
+
}
|
|
140
|
+
}
|
|
88
141
|
}
|
|
89
142
|
}
|
|
90
|
-
return { prompt,
|
|
91
|
-
}
|
|
92
|
-
|
|
93
|
-
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
97
|
-
|
|
98
|
-
|
|
99
|
-
|
|
143
|
+
return { count, prompt, referenceCandidates, sizeHint: buildSizeHint(input) };
|
|
144
|
+
};
|
|
145
|
+
const isLocalReference = (ref) => !/^(?:https?:|data:)/iu.test(ref);
|
|
146
|
+
/**
|
|
147
|
+
* Pick the backend for one `run`. An explicit `--provider` or
|
|
148
|
+
* `VG_GENERATE_PROVIDER` always wins. Left unset, an OpenAI image endpoint
|
|
149
|
+
* goes to a locally installed Codex CLI — the same model family on the
|
|
150
|
+
* user's own plan, nothing billed to vibedgames — whenever Codex can honour
|
|
151
|
+
* the request (synchronous, local references only). Everything else stays
|
|
152
|
+
* on the vibedgames runner, so video/audio and non-OpenAI image models are
|
|
153
|
+
* unaffected by having Codex installed.
|
|
154
|
+
*/
|
|
155
|
+
export const chooseProvider = (flag, ctx) => {
|
|
156
|
+
const named = (flag ?? process.env.VG_GENERATE_PROVIDER ?? "").trim();
|
|
157
|
+
if (named !== "") {
|
|
158
|
+
return { auto: false, provider: resolveProvider(named) };
|
|
100
159
|
}
|
|
101
|
-
const
|
|
102
|
-
const
|
|
103
|
-
|
|
104
|
-
|
|
105
|
-
|
|
106
|
-
}
|
|
160
|
+
const refs = parseCodexInput(ctx.input).referenceCandidates;
|
|
161
|
+
const eligible = ctx.codexInstalled &&
|
|
162
|
+
!ctx.async &&
|
|
163
|
+
isOpenAiImageEndpoint(ctx.endpointId) &&
|
|
164
|
+
refs.every((ref) => isLocalReference(ref));
|
|
165
|
+
return { auto: eligible, provider: eligible ? "codex" : "vibedgames" };
|
|
166
|
+
};
|
|
107
167
|
/**
|
|
108
168
|
* Build the natural-language instruction handed to `codex exec`. We pin
|
|
109
169
|
* exact output filenames and the working directory so the generated
|
|
110
170
|
* files land somewhere we can deterministically collect them.
|
|
111
171
|
*/
|
|
112
|
-
export
|
|
172
|
+
export const buildCodexPrompt = (input, filenames, hasReferences) => {
|
|
113
173
|
const n = filenames.length;
|
|
114
174
|
const noun = n === 1 ? "image" : `${n} images`;
|
|
115
175
|
const lines = [
|
|
@@ -118,21 +178,22 @@ export function buildCodexPrompt(input, filenames, hasReferences) {
|
|
|
118
178
|
: `Generate ${noun} using your built-in image generation.`,
|
|
119
179
|
`Prompt: ${input.prompt}`,
|
|
120
180
|
];
|
|
121
|
-
if (input.sizeHint)
|
|
181
|
+
if (input.sizeHint) {
|
|
122
182
|
lines.push(`Size: ${input.sizeHint}.`);
|
|
183
|
+
}
|
|
123
184
|
lines.push(`Save the output as PNG into the current working directory using exactly ` +
|
|
124
185
|
`these filenames: ${filenames.join(", ")}.`, `Do not read, create, or modify any other files, and do not write or run code. $imagegen`);
|
|
125
186
|
return lines.join("\n");
|
|
126
|
-
}
|
|
187
|
+
};
|
|
127
188
|
/**
|
|
128
189
|
* Choose the on-disk destination for a collected Codex output, mirroring
|
|
129
190
|
* the `--download` template semantics used for the vibedgames path
|
|
130
191
|
* ({index}, {ext}, {request_id} placeholders; bare path treated as a
|
|
131
192
|
* directory or a literal file by extension).
|
|
132
193
|
*/
|
|
133
|
-
export
|
|
194
|
+
export const renderLocalTarget = (template, index, ext, requestId, count) => {
|
|
134
195
|
if (!template) {
|
|
135
|
-
return resolve(process.cwd(), `codex-image-${requestId}-${index}.${ext}`);
|
|
196
|
+
return path.resolve(process.cwd(), `codex-image-${requestId}-${index}.${ext}`);
|
|
136
197
|
}
|
|
137
198
|
if (template.includes("{")) {
|
|
138
199
|
const rendered = template
|
|
@@ -140,21 +201,20 @@ export function renderLocalTarget(template, index, ext, requestId, count) {
|
|
|
140
201
|
.replaceAll("{ext}", ext)
|
|
141
202
|
.replaceAll("{request_id}", requestId)
|
|
142
203
|
.replaceAll("{name}", "output");
|
|
143
|
-
return resolve(rendered);
|
|
204
|
+
return path.resolve(rendered);
|
|
144
205
|
}
|
|
145
|
-
if (extname(template)) {
|
|
146
|
-
if (count <= 1)
|
|
147
|
-
return resolve(template);
|
|
148
|
-
|
|
206
|
+
if (path.extname(template)) {
|
|
207
|
+
if (count <= 1) {
|
|
208
|
+
return path.resolve(template);
|
|
209
|
+
}
|
|
210
|
+
const e = path.extname(template);
|
|
149
211
|
const stem = template.slice(0, template.length - e.length);
|
|
150
|
-
return resolve(index === 0 ? template : `${stem}_${index}${e}`);
|
|
212
|
+
return path.resolve(index === 0 ? template : `${stem}_${index}${e}`);
|
|
151
213
|
}
|
|
152
|
-
return resolve(template, `codex-image-${index}.${ext}`);
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
|
|
156
|
-
}
|
|
157
|
-
function listImages(dir) {
|
|
214
|
+
return path.resolve(template, `codex-image-${index}.${ext}`);
|
|
215
|
+
};
|
|
216
|
+
const codexHome = () => process.env.CODEX_HOME ?? path.join(homedir(), ".codex");
|
|
217
|
+
const listImages = (dir) => {
|
|
158
218
|
let names;
|
|
159
219
|
try {
|
|
160
220
|
names = readdirSync(dir);
|
|
@@ -164,41 +224,73 @@ function listImages(dir) {
|
|
|
164
224
|
}
|
|
165
225
|
const out = [];
|
|
166
226
|
for (const name of names) {
|
|
167
|
-
const ext = extname(name).slice(1).toLowerCase();
|
|
168
|
-
if (!IMAGE_EXT.has(ext))
|
|
227
|
+
const ext = path.extname(name).slice(1).toLowerCase();
|
|
228
|
+
if (!IMAGE_EXT.has(ext)) {
|
|
169
229
|
continue;
|
|
170
|
-
|
|
230
|
+
}
|
|
231
|
+
const full = path.join(dir, name);
|
|
171
232
|
try {
|
|
172
|
-
if (statSync(full).isFile())
|
|
233
|
+
if (statSync(full).isFile()) {
|
|
173
234
|
out.push(full);
|
|
235
|
+
}
|
|
174
236
|
}
|
|
175
237
|
catch {
|
|
176
238
|
// Raced away between readdir and stat; skip.
|
|
177
239
|
}
|
|
178
240
|
}
|
|
179
241
|
return out.toSorted();
|
|
180
|
-
}
|
|
242
|
+
};
|
|
181
243
|
// Keep the `limit` most-recently-modified paths (by mtime), returned in
|
|
182
244
|
// stable name order. Used to bound the shared-store fallback.
|
|
183
|
-
|
|
184
|
-
if (paths.length <= limit)
|
|
245
|
+
const pickNewest = (paths, limit) => {
|
|
246
|
+
if (paths.length <= limit) {
|
|
185
247
|
return paths;
|
|
248
|
+
}
|
|
186
249
|
const withTime = paths.map((p) => {
|
|
187
250
|
let mtimeMs = 0;
|
|
188
251
|
try {
|
|
189
|
-
mtimeMs = statSync(p)
|
|
252
|
+
({ mtimeMs } = statSync(p));
|
|
190
253
|
}
|
|
191
254
|
catch {
|
|
192
255
|
// Gone between listing and stat; sort it oldest.
|
|
193
256
|
}
|
|
194
|
-
return {
|
|
257
|
+
return { mtimeMs, p };
|
|
195
258
|
});
|
|
196
259
|
return withTime
|
|
197
260
|
.toSorted((a, b) => b.mtimeMs - a.mtimeMs)
|
|
198
261
|
.slice(0, limit)
|
|
199
262
|
.map((x) => x.p)
|
|
200
263
|
.toSorted();
|
|
201
|
-
}
|
|
264
|
+
};
|
|
265
|
+
const spawnCodex = (bin, args, cwd) =>
|
|
266
|
+
// oxlint-disable-next-line promise/avoid-new -- child_process.spawn is event-based
|
|
267
|
+
new Promise((resolve) => {
|
|
268
|
+
const child = spawn(bin, args, { cwd, stdio: ["ignore", "pipe", "pipe"] });
|
|
269
|
+
let output = "";
|
|
270
|
+
// Forward Codex's own stdout/stderr straight to our stderr so the user
|
|
271
|
+
// sees its progress and messages directly. It goes to stderr (never
|
|
272
|
+
// stdout) so it can't corrupt our `--json` payload. We also buffer it
|
|
273
|
+
// so a failure can surface the tail even for a caller that isn't
|
|
274
|
+
// watching the live stream.
|
|
275
|
+
child.stdout?.on("data", (c) => {
|
|
276
|
+
output += c.toString("utf-8");
|
|
277
|
+
process.stderr.write(c);
|
|
278
|
+
});
|
|
279
|
+
child.stderr?.on("data", (c) => {
|
|
280
|
+
output += c.toString("utf-8");
|
|
281
|
+
process.stderr.write(c);
|
|
282
|
+
});
|
|
283
|
+
child.on("error", (err) => {
|
|
284
|
+
resolve({
|
|
285
|
+
code: 1,
|
|
286
|
+
notFound: err.code === "ENOENT",
|
|
287
|
+
output: output + err.message,
|
|
288
|
+
});
|
|
289
|
+
});
|
|
290
|
+
child.on("close", (code) => {
|
|
291
|
+
resolve({ code: code ?? 1, notFound: false, output });
|
|
292
|
+
});
|
|
293
|
+
});
|
|
202
294
|
/**
|
|
203
295
|
* Delegate image generation to the local `codex` CLI. Runs
|
|
204
296
|
* `codex exec` in an isolated temp workspace, then collects the produced
|
|
@@ -206,7 +298,7 @@ function pickNewest(paths, limit) {
|
|
|
206
298
|
* `generated_images` store). Throws with actionable guidance when Codex
|
|
207
299
|
* is missing, fails, or produces nothing.
|
|
208
300
|
*/
|
|
209
|
-
export async
|
|
301
|
+
export const generateImagesWithCodex = async (opts) => {
|
|
210
302
|
const parsed = parseCodexInput(opts.input);
|
|
211
303
|
if (!parsed.prompt) {
|
|
212
304
|
throw new CodexError("codex image generation requires a prompt (--prompt).");
|
|
@@ -218,13 +310,15 @@ export async function generateImagesWithCodex(opts) {
|
|
|
218
310
|
const ignoredReferences = [];
|
|
219
311
|
for (const candidate of parsed.referenceCandidates) {
|
|
220
312
|
const local = readExplicitLocalFile(candidate);
|
|
221
|
-
if (local)
|
|
313
|
+
if (local) {
|
|
222
314
|
references.push(local.path);
|
|
223
|
-
|
|
315
|
+
}
|
|
316
|
+
else {
|
|
224
317
|
ignoredReferences.push(candidate);
|
|
318
|
+
}
|
|
225
319
|
}
|
|
226
320
|
const requestId = randomBytes(4).toString("hex");
|
|
227
|
-
const workDir = mkdtempSync(join(tmpdir(), "vg-codex-"));
|
|
321
|
+
const workDir = mkdtempSync(path.join(tmpdir(), "vg-codex-"));
|
|
228
322
|
const filenames = Array.from({ length: parsed.count }, (_, i) => `output-${i}.png`);
|
|
229
323
|
const prompt = buildCodexPrompt(parsed, filenames, references.length > 0);
|
|
230
324
|
const bin = process.env.VG_CODEX_BIN ?? "codex";
|
|
@@ -240,7 +334,7 @@ export async function generateImagesWithCodex(opts) {
|
|
|
240
334
|
];
|
|
241
335
|
// Snapshot Codex's default image store so we can tell which files this
|
|
242
336
|
// run produced if the model saves there instead of the workspace.
|
|
243
|
-
const storeDir = join(codexHome(), "generated_images");
|
|
337
|
+
const storeDir = path.join(codexHome(), "generated_images");
|
|
244
338
|
const before = new Set(listImages(storeDir));
|
|
245
339
|
const outcome = await spawnCodex(bin, args, workDir);
|
|
246
340
|
if (outcome.notFound) {
|
|
@@ -258,7 +352,7 @@ export async function generateImagesWithCodex(opts) {
|
|
|
258
352
|
// workspace must NOT count as output nor suppress the store fallback.
|
|
259
353
|
const pinned = new Set(filenames);
|
|
260
354
|
const fromWork = listImages(workDir);
|
|
261
|
-
const pinnedWork = fromWork.filter((p) => pinned.has(basename(p)));
|
|
355
|
+
const pinnedWork = fromWork.filter((p) => pinned.has(path.basename(p)));
|
|
262
356
|
// Fallback for when Codex ignored the workspace and wrote to its shared
|
|
263
357
|
// store instead. That store is not scoped to this invocation, so cap to
|
|
264
358
|
// the newest `count` new files to limit picking up a concurrent run's
|
|
@@ -269,23 +363,25 @@ export async function generateImagesWithCodex(opts) {
|
|
|
269
363
|
// any workspace PNG (Codex saved images but chose different names) —
|
|
270
364
|
// capped to `count` like the store branch so an unexpected extra file
|
|
271
365
|
// can't inflate the output set.
|
|
272
|
-
|
|
273
|
-
|
|
274
|
-
|
|
275
|
-
|
|
276
|
-
|
|
366
|
+
let rawFiles = pickNewest(fromWork, parsed.count);
|
|
367
|
+
if (pinnedWork.length > 0) {
|
|
368
|
+
rawFiles = pinnedWork;
|
|
369
|
+
}
|
|
370
|
+
else if (fromStore.length > 0) {
|
|
371
|
+
rawFiles = fromStore;
|
|
372
|
+
}
|
|
277
373
|
if (rawFiles.length === 0) {
|
|
278
374
|
throw new CodexError(`codex produced no image files. It may have declined the request or lack ` +
|
|
279
375
|
`image-generation access on the signed-in plan.`, { output: outcome.output });
|
|
280
376
|
}
|
|
281
|
-
return {
|
|
282
|
-
}
|
|
377
|
+
return { ignoredReferences, prompt, rawFiles, requestId };
|
|
378
|
+
};
|
|
283
379
|
/**
|
|
284
380
|
* Copy Codex's raw outputs to their final destinations per the
|
|
285
381
|
* `--download` template (or default cwd naming). Returns the written
|
|
286
382
|
* paths and any per-file failures.
|
|
287
383
|
*/
|
|
288
|
-
export
|
|
384
|
+
export const placeCodexOutputs = (rawFiles, template, requestId) => {
|
|
289
385
|
const downloaded = [];
|
|
290
386
|
// Keyed `url` (holding the source path) to match the shape the
|
|
291
387
|
// vibedgames run/status paths use for `download_failures`, so agents
|
|
@@ -297,51 +393,25 @@ export function placeCodexOutputs(rawFiles, template, requestId) {
|
|
|
297
393
|
// {index} (e.g. `{request_id}.{ext}` or a fixed filename) would
|
|
298
394
|
// otherwise overwrite earlier outputs while still reporting each path.
|
|
299
395
|
const rendered = rawFiles.map((source, index) => {
|
|
300
|
-
const ext = extname(source).slice(1).toLowerCase() || "png";
|
|
396
|
+
const ext = path.extname(source).slice(1).toLowerCase() || "png";
|
|
301
397
|
return renderLocalTarget(template, index, ext, requestId, rawFiles.length);
|
|
302
398
|
});
|
|
303
399
|
const targets = disambiguateTargets(rendered);
|
|
304
|
-
rawFiles.
|
|
400
|
+
for (const [index, source] of rawFiles.entries()) {
|
|
305
401
|
const target = targets[index];
|
|
402
|
+
if (target === undefined) {
|
|
403
|
+
continue;
|
|
404
|
+
}
|
|
306
405
|
try {
|
|
307
|
-
if (resolve(source) !== target) {
|
|
308
|
-
mkdirSync(dirname(target), { recursive: true });
|
|
406
|
+
if (path.resolve(source) !== target) {
|
|
407
|
+
mkdirSync(path.dirname(target), { recursive: true });
|
|
309
408
|
copyFileSync(source, target);
|
|
310
409
|
}
|
|
311
410
|
downloaded.push(target);
|
|
312
411
|
}
|
|
313
|
-
catch (
|
|
314
|
-
failed.push({
|
|
412
|
+
catch (error) {
|
|
413
|
+
failed.push({ error: error instanceof Error ? error.message : String(error), url: source });
|
|
315
414
|
}
|
|
316
|
-
}
|
|
415
|
+
}
|
|
317
416
|
return { downloaded, failed };
|
|
318
|
-
}
|
|
319
|
-
function spawnCodex(bin, args, cwd) {
|
|
320
|
-
return new Promise((resolvePromise) => {
|
|
321
|
-
const child = spawn(bin, args, { cwd, stdio: ["ignore", "pipe", "pipe"] });
|
|
322
|
-
let output = "";
|
|
323
|
-
// Forward Codex's own stdout/stderr straight to our stderr so the user
|
|
324
|
-
// sees its progress and messages directly. It goes to stderr (never
|
|
325
|
-
// stdout) so it can't corrupt our `--json` payload. We also buffer it
|
|
326
|
-
// so a failure can surface the tail even for a caller that isn't
|
|
327
|
-
// watching the live stream.
|
|
328
|
-
child.stdout?.on("data", (c) => {
|
|
329
|
-
output += c.toString("utf8");
|
|
330
|
-
process.stderr.write(c);
|
|
331
|
-
});
|
|
332
|
-
child.stderr?.on("data", (c) => {
|
|
333
|
-
output += c.toString("utf8");
|
|
334
|
-
process.stderr.write(c);
|
|
335
|
-
});
|
|
336
|
-
child.on("error", (err) => {
|
|
337
|
-
resolvePromise({
|
|
338
|
-
code: 1,
|
|
339
|
-
output: output + err.message,
|
|
340
|
-
notFound: err.code === "ENOENT",
|
|
341
|
-
});
|
|
342
|
-
});
|
|
343
|
-
child.on("close", (code) => {
|
|
344
|
-
resolvePromise({ code: code ?? 1, output, notFound: false });
|
|
345
|
-
});
|
|
346
|
-
});
|
|
347
|
-
}
|
|
417
|
+
};
|
|
@@ -1,7 +1,7 @@
|
|
|
1
|
-
export
|
|
1
|
+
export interface ProjectConfig {
|
|
2
2
|
slug: string;
|
|
3
3
|
name?: string;
|
|
4
|
-
}
|
|
4
|
+
}
|
|
5
5
|
/**
|
|
6
6
|
* The slug grammar every surface must agree on — it is the deploy target, the
|
|
7
7
|
* fork target, and the label in `{slug}.vibedgames.com`. Kept here rather than
|
|
@@ -10,7 +10,7 @@ export type ProjectConfig = {
|
|
|
10
10
|
export declare const SLUG_RE: RegExp;
|
|
11
11
|
/** The directory containing the nearest vibedgames.json at or above `dir`,
|
|
12
12
|
* or null if none — i.e. the project root to archive as forkable source. */
|
|
13
|
-
export declare
|
|
14
|
-
export declare
|
|
15
|
-
export declare
|
|
16
|
-
export declare
|
|
13
|
+
export declare const findProjectRoot: (dir: string) => string | null;
|
|
14
|
+
export declare const readProjectConfig: (dir: string) => ProjectConfig | null;
|
|
15
|
+
export declare const writeProjectConfig: (dir: string, config: ProjectConfig) => void;
|
|
16
|
+
export declare const projectConfigPath: (dir: string) => string;
|
package/dist/lib/config-file.js
CHANGED
|
@@ -1,51 +1,54 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import
|
|
2
|
+
import path from "node:path";
|
|
3
|
+
import { isJsonObject, isJsonString } from "./types.js";
|
|
3
4
|
const FILENAME = "vibedgames.json";
|
|
4
5
|
/**
|
|
5
6
|
* The slug grammar every surface must agree on — it is the deploy target, the
|
|
6
7
|
* fork target, and the label in `{slug}.vibedgames.com`. Kept here rather than
|
|
7
8
|
* per-command so a validator can't drift and reject a slug that deploy accepts.
|
|
8
9
|
*/
|
|
9
|
-
export const SLUG_RE = /^[a-z0-9][a-z0-9-]*[a-z0-9]
|
|
10
|
+
export const SLUG_RE = /^[a-z0-9][a-z0-9-]*[a-z0-9]$/u;
|
|
10
11
|
// Walk from `dir` up toward the filesystem root, stopping at the first
|
|
11
12
|
// vibedgames.json. Lets `vg deploy ./dist` pick up the config from the
|
|
12
13
|
// project root even when the build step doesn't copy it into the output.
|
|
13
|
-
|
|
14
|
-
let current = resolve(dir);
|
|
14
|
+
const findConfigPath = (dir) => {
|
|
15
|
+
let current = path.resolve(dir);
|
|
15
16
|
while (true) {
|
|
16
|
-
const candidate = join(current, FILENAME);
|
|
17
|
-
if (existsSync(candidate))
|
|
17
|
+
const candidate = path.join(current, FILENAME);
|
|
18
|
+
if (existsSync(candidate)) {
|
|
18
19
|
return candidate;
|
|
19
|
-
|
|
20
|
-
|
|
20
|
+
}
|
|
21
|
+
const parent = path.dirname(current);
|
|
22
|
+
if (parent === current) {
|
|
21
23
|
return null;
|
|
24
|
+
}
|
|
22
25
|
current = parent;
|
|
23
26
|
}
|
|
24
|
-
}
|
|
27
|
+
};
|
|
25
28
|
/** The directory containing the nearest vibedgames.json at or above `dir`,
|
|
26
29
|
* or null if none — i.e. the project root to archive as forkable source. */
|
|
27
|
-
export
|
|
28
|
-
const
|
|
29
|
-
return
|
|
30
|
-
}
|
|
31
|
-
export
|
|
32
|
-
const
|
|
33
|
-
if (!
|
|
30
|
+
export const findProjectRoot = (dir) => {
|
|
31
|
+
const found = findConfigPath(dir);
|
|
32
|
+
return found ? path.dirname(found) : null;
|
|
33
|
+
};
|
|
34
|
+
export const readProjectConfig = (dir) => {
|
|
35
|
+
const file = findConfigPath(dir);
|
|
36
|
+
if (!file) {
|
|
34
37
|
return null;
|
|
35
|
-
|
|
38
|
+
}
|
|
39
|
+
const raw = readFileSync(file, "utf-8");
|
|
36
40
|
const parsed = JSON.parse(raw);
|
|
37
|
-
if (!parsed ||
|
|
38
|
-
typeof parsed !== "object" ||
|
|
39
|
-
!("slug" in parsed) ||
|
|
40
|
-
typeof parsed.slug !== "string") {
|
|
41
|
+
if (!isJsonObject(parsed) || !isJsonString(parsed.slug)) {
|
|
41
42
|
throw new Error(`${FILENAME} is malformed — missing "slug".`);
|
|
42
43
|
}
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
}
|
|
49
|
-
export
|
|
50
|
-
|
|
51
|
-
}
|
|
44
|
+
const config = { slug: parsed.slug };
|
|
45
|
+
if (isJsonString(parsed.name)) {
|
|
46
|
+
config.name = parsed.name;
|
|
47
|
+
}
|
|
48
|
+
return config;
|
|
49
|
+
};
|
|
50
|
+
export const writeProjectConfig = (dir, config) => {
|
|
51
|
+
const file = path.join(dir, FILENAME);
|
|
52
|
+
writeFileSync(file, `${JSON.stringify(config, null, 2)}\n`);
|
|
53
|
+
};
|
|
54
|
+
export const projectConfigPath = (dir) => path.join(dir, FILENAME);
|
package/dist/lib/config.d.ts
CHANGED
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
|
|
1
|
+
interface Config {
|
|
2
2
|
token: string;
|
|
3
3
|
baseUrl: string;
|
|
4
|
-
}
|
|
5
|
-
export declare
|
|
6
|
-
export declare
|
|
7
|
-
export declare
|
|
8
|
-
export declare
|
|
9
|
-
export declare
|
|
10
|
-
export declare
|
|
4
|
+
}
|
|
5
|
+
export declare const getConfigDir: () => string;
|
|
6
|
+
export declare const getConfig: () => Config | null;
|
|
7
|
+
export declare const saveConfig: (config: Config) => void;
|
|
8
|
+
export declare const clearConfig: () => void;
|
|
9
|
+
export declare const getBaseUrl: () => string;
|
|
10
|
+
export declare const getToken: () => string | null;
|
|
11
11
|
export {};
|