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.
Files changed (60) hide show
  1. package/README.md +1 -1
  2. package/dist/commands/completions.d.ts +1 -1
  3. package/dist/commands/completions.js +21 -20
  4. package/dist/commands/credits.d.ts +5 -5
  5. package/dist/commands/credits.js +45 -47
  6. package/dist/commands/deploy.d.ts +10 -10
  7. package/dist/commands/deploy.js +157 -137
  8. package/dist/commands/factory.d.ts +1 -1
  9. package/dist/commands/factory.js +18 -15
  10. package/dist/commands/fork.d.ts +14 -14
  11. package/dist/commands/fork.js +53 -45
  12. package/dist/commands/generate.js +343 -286
  13. package/dist/commands/init.d.ts +8 -8
  14. package/dist/commands/init.js +31 -24
  15. package/dist/commands/login.js +14 -14
  16. package/dist/commands/logout.js +2 -2
  17. package/dist/commands/new.d.ts +16 -16
  18. package/dist/commands/new.js +191 -165
  19. package/dist/commands/playtest.d.ts +5 -5
  20. package/dist/commands/playtest.js +103 -97
  21. package/dist/commands/update.d.ts +7 -7
  22. package/dist/commands/update.js +22 -17
  23. package/dist/commands/whoami.d.ts +5 -5
  24. package/dist/commands/whoami.js +11 -10
  25. package/dist/index.js +12 -10
  26. package/dist/lib/api.d.ts +16 -4
  27. package/dist/lib/api.js +20 -32
  28. package/dist/lib/archive.d.ts +4 -4
  29. package/dist/lib/archive.js +123 -102
  30. package/dist/lib/codex.d.ts +46 -15
  31. package/dist/lib/codex.js +178 -108
  32. package/dist/lib/config-file.d.ts +6 -6
  33. package/dist/lib/config-file.js +33 -30
  34. package/dist/lib/config.d.ts +8 -8
  35. package/dist/lib/config.js +38 -30
  36. package/dist/lib/manifest.d.ts +3 -3
  37. package/dist/lib/manifest.js +44 -40
  38. package/dist/lib/media-args.d.ts +8 -6
  39. package/dist/lib/media-args.js +113 -94
  40. package/dist/lib/media-download.d.ts +9 -8
  41. package/dist/lib/media-download.js +158 -144
  42. package/dist/lib/media-poll.d.ts +8 -7
  43. package/dist/lib/media-poll.js +38 -32
  44. package/dist/lib/media-upload.d.ts +1 -1
  45. package/dist/lib/media-upload.js +30 -29
  46. package/dist/lib/output.d.ts +34 -11
  47. package/dist/lib/output.js +28 -20
  48. package/dist/lib/package-manager.d.ts +3 -3
  49. package/dist/lib/package-manager.js +27 -22
  50. package/dist/lib/run.d.ts +4 -4
  51. package/dist/lib/run.js +7 -4
  52. package/dist/lib/strict-args.d.ts +4 -4
  53. package/dist/lib/strict-args.js +29 -19
  54. package/dist/lib/types.d.ts +16 -2
  55. package/dist/lib/types.js +10 -4
  56. package/dist/lib/update.d.ts +4 -4
  57. package/dist/lib/update.js +47 -35
  58. package/dist/lib/upload.d.ts +4 -4
  59. package/dist/lib/upload.js +14 -9
  60. package/package.json +7 -8
@@ -1,17 +1,17 @@
1
- export type SourceArchive = {
1
+ export interface SourceArchive {
2
2
  /** absolute path to the temp .tgz */
3
3
  path: string;
4
4
  sha256: string;
5
5
  bytes: number;
6
6
  /** posix relative paths included, for display */
7
7
  files: string[];
8
- };
8
+ }
9
9
  /**
10
10
  * Create a gzipped tar of `root`'s source (respecting .gitignore +
11
11
  * HARD_EXCLUDES) at a temp path. Returns the archive path, its sha256, byte
12
12
  * size, and the list of included files.
13
13
  */
14
- export declare function packSource(root: string, tmpDir: string): Promise<SourceArchive>;
14
+ export declare const packSource: (root: string, tmpDir: string) => Promise<SourceArchive>;
15
15
  /** Extract a gzipped tar into `destDir` (created if needed). tar strips
16
16
  * leading slashes and rejects `..` paths, so extraction stays inside dest. */
17
- export declare function extractSource(archivePath: string, destDir: string): Promise<void>;
17
+ export declare const extractSource: (archivePath: string, destDir: string) => Promise<void>;
@@ -1,8 +1,9 @@
1
1
  import { createHash } from "node:crypto";
2
2
  import { copyFileSync, existsSync, mkdirSync, mkdtempSync, readdirSync, readFileSync, rmSync, statSync, writeFileSync, } from "node:fs";
3
- import { dirname, join, relative, sep } from "node:path";
3
+ import path from "node:path";
4
4
  import ignore from "ignore";
5
5
  import { create as tarCreate, extract as tarExtract } from "tar";
6
+ import { isJsonObject, isJsonString } from "./types.js";
6
7
  /**
7
8
  * Patterns ALWAYS excluded from a source archive, regardless of .gitignore —
8
9
  * build output, VCS internals, and (critically) secrets. Source upload is a
@@ -44,87 +45,95 @@ const HARD_EXCLUDES = [
44
45
  * negation like `!.env` in a user ignore file can NOT un-exclude a secret.
45
46
  * A path is excluded if EITHER matcher says so.
46
47
  */
47
- function buildIgnorer(root) {
48
+ const buildIgnorer = (root) => {
48
49
  const hard = ignore().add(HARD_EXCLUDES);
49
50
  const user = ignore();
50
51
  for (const name of [".gitignore", ".vibedgamesignore"]) {
51
- const p = join(root, name);
52
- if (existsSync(p))
53
- user.add(readFileSync(p, "utf8"));
52
+ const p = path.join(root, name);
53
+ if (existsSync(p)) {
54
+ user.add(readFileSync(p, "utf-8"));
55
+ }
54
56
  }
55
57
  return (rel) => hard.ignores(rel) || user.ignores(rel);
56
- }
58
+ };
57
59
  /** Walk `root`, returning posix relative paths of files not ignored. Ignored
58
60
  * directories are pruned (not descended into) for speed. */
59
- function collectFiles(root, ignored) {
61
+ const collectFiles = (root, ignored) => {
60
62
  const out = [];
61
63
  const walk = (dir) => {
62
64
  for (const entry of readdirSync(dir, { withFileTypes: true })) {
63
- const abs = join(dir, entry.name);
64
- const rel = relative(root, abs).split(sep).join("/");
65
- if (!rel)
65
+ const abs = path.join(dir, entry.name);
66
+ const rel = path.relative(root, abs).split(path.sep).join("/");
67
+ if (!rel) {
66
68
  continue;
69
+ }
67
70
  // `ignore` matches dirs via a trailing slash; test both forms.
68
71
  if (entry.isDirectory()) {
69
- if (ignored(`${rel}/`) || ignored(rel))
72
+ if (ignored(`${rel}/`) || ignored(rel)) {
70
73
  continue;
74
+ }
71
75
  walk(abs);
72
76
  }
73
77
  else if (entry.isFile()) {
74
- if (ignored(rel))
78
+ if (ignored(rel)) {
75
79
  continue;
80
+ }
76
81
  out.push(rel);
77
82
  }
78
83
  }
79
84
  };
80
85
  walk(root);
81
- return out.sort();
82
- }
83
- /**
84
- * Create a gzipped tar of `root`'s source (respecting .gitignore +
85
- * HARD_EXCLUDES) at a temp path. Returns the archive path, its sha256, byte
86
- * size, and the list of included files.
87
- */
88
- export async function packSource(root, tmpDir) {
89
- const ignored = buildIgnorer(root);
90
- const files = collectFiles(root, ignored);
91
- if (files.length === 0) {
92
- throw new Error("No source files to archive (everything was ignored?).");
93
- }
94
- mkdirSync(tmpDir, { recursive: true });
95
- // If package.json uses pnpm-only protocols (workspace:/catalog:), a forked
96
- // copy wouldn't `npm install` standalone. Rewrite them to concrete installed
97
- // versions in the ARCHIVED package.json only (disk untouched) by staging the
98
- // file list into a temp dir. No protocols → no staging (fast path).
99
- const rewritten = files.includes("package.json") ? rewriteWorkspaceProtocols(root) : null;
100
- let cwd = root;
101
- let stage = null;
102
- if (rewritten) {
103
- stage = mkdtempSync(join(tmpDir, "stage-"));
104
- for (const f of files) {
105
- const dest = join(stage, f);
106
- mkdirSync(dirname(dest), { recursive: true });
107
- copyFileSync(join(root, f), dest);
86
+ return out.toSorted();
87
+ };
88
+ /** Version of an installed dependency, searching node_modules up the tree. */
89
+ const installedVersion = (root, name) => {
90
+ let dir = root;
91
+ while (true) {
92
+ const p = path.join(dir, "node_modules", name, "package.json");
93
+ if (existsSync(p)) {
94
+ try {
95
+ const pkg = JSON.parse(readFileSync(p, "utf-8"));
96
+ const v = isJsonObject(pkg) ? pkg.version : undefined;
97
+ if (isJsonString(v)) {
98
+ return v;
99
+ }
100
+ }
101
+ catch {
102
+ /* fall through to parent */
103
+ }
108
104
  }
109
- writeFileSync(join(stage, "package.json"), rewritten);
110
- cwd = stage;
111
- }
112
- const path = join(tmpDir, `vg-source-${process.pid}-${files.length}.tgz`);
113
- try {
114
- await tarCreate({ gzip: true, file: path, cwd, portable: true }, files);
105
+ const parent = path.dirname(dir);
106
+ if (parent === dir) {
107
+ return null;
108
+ }
109
+ dir = parent;
115
110
  }
116
- finally {
117
- if (stage)
118
- rmSync(stage, { recursive: true, force: true });
111
+ };
112
+ const resolveSpec = (root, name, spec) => {
113
+ if (spec.startsWith("workspace:")) {
114
+ const range = spec.slice("workspace:".length);
115
+ // Explicit range (e.g. workspace:^1.2.3) — keep the literal range.
116
+ if (/\d/u.test(range)) {
117
+ return range;
118
+ }
119
+ const v = installedVersion(root, name);
120
+ if (!v) {
121
+ return null;
122
+ }
123
+ if (range === "~") {
124
+ return `~${v}`;
125
+ }
126
+ if (range === "*") {
127
+ return v;
128
+ // exact pin
129
+ }
130
+ // "" or "^"
131
+ return `^${v}`;
119
132
  }
120
- const buf = readFileSync(path);
121
- return {
122
- path,
123
- sha256: createHash("sha256").update(buf).digest("hex"),
124
- bytes: statSync(path).size,
125
- files,
126
- };
127
- }
133
+ // catalog: / catalog:<name> — resolve to the installed version, caret-pinned.
134
+ const v = installedVersion(root, name);
135
+ return v ? `^${v}` : null;
136
+ };
128
137
  const DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "optionalDependencies"];
129
138
  /**
130
139
  * Rewrite `workspace:` / `catalog:` dep specs in package.json to the concrete
@@ -132,24 +141,30 @@ const DEP_FIELDS = ["dependencies", "devDependencies", "peerDependencies", "opti
132
141
  * does — so the forked project installs from npm. Returns the rewritten JSON
133
142
  * string, or null if there's nothing to change (the common standalone case).
134
143
  */
135
- function rewriteWorkspaceProtocols(root) {
144
+ const rewriteWorkspaceProtocols = (root) => {
136
145
  let pkg;
137
146
  try {
138
- pkg = JSON.parse(readFileSync(join(root, "package.json"), "utf8"));
147
+ pkg = JSON.parse(readFileSync(path.join(root, "package.json"), "utf-8"));
139
148
  }
140
149
  catch {
141
150
  return null;
142
151
  }
152
+ if (!isJsonObject(pkg)) {
153
+ return null;
154
+ }
143
155
  let changed = false;
144
156
  for (const field of DEP_FIELDS) {
145
157
  const deps = pkg[field];
146
- if (!deps || typeof deps !== "object")
158
+ if (!isJsonObject(deps)) {
147
159
  continue;
160
+ }
148
161
  for (const [name, spec] of Object.entries(deps)) {
149
- if (typeof spec !== "string")
162
+ if (!isJsonString(spec)) {
150
163
  continue;
151
- if (!spec.startsWith("workspace:") && !spec.startsWith("catalog:"))
164
+ }
165
+ if (!spec.startsWith("workspace:") && !spec.startsWith("catalog:")) {
152
166
  continue;
167
+ }
153
168
  const resolved = resolveSpec(root, name, spec);
154
169
  if (resolved && resolved !== spec) {
155
170
  deps[name] = resolved;
@@ -158,50 +173,56 @@ function rewriteWorkspaceProtocols(root) {
158
173
  }
159
174
  }
160
175
  return changed ? `${JSON.stringify(pkg, null, 2)}\n` : null;
161
- }
162
- function resolveSpec(root, name, spec) {
163
- if (spec.startsWith("workspace:")) {
164
- const range = spec.slice("workspace:".length);
165
- // Explicit range (e.g. workspace:^1.2.3) — keep the literal range.
166
- if (/\d/.test(range))
167
- return range;
168
- const v = installedVersion(root, name);
169
- if (!v)
170
- return null;
171
- if (range === "~")
172
- return `~${v}`;
173
- if (range === "*")
174
- return v; // exact pin
175
- return `^${v}`; // "" or "^"
176
+ };
177
+ /**
178
+ * Create a gzipped tar of `root`'s source (respecting .gitignore +
179
+ * HARD_EXCLUDES) at a temp path. Returns the archive path, its sha256, byte
180
+ * size, and the list of included files.
181
+ */
182
+ export const packSource = async (root, tmpDir) => {
183
+ const ignored = buildIgnorer(root);
184
+ const files = collectFiles(root, ignored);
185
+ if (files.length === 0) {
186
+ throw new Error("No source files to archive (everything was ignored?).");
176
187
  }
177
- // catalog: / catalog:<name> — resolve to the installed version, caret-pinned.
178
- const v = installedVersion(root, name);
179
- return v ? `^${v}` : null;
180
- }
181
- /** Version of an installed dependency, searching node_modules up the tree. */
182
- function installedVersion(root, name) {
183
- let dir = root;
184
- while (true) {
185
- const p = join(dir, "node_modules", name, "package.json");
186
- if (existsSync(p)) {
187
- try {
188
- const v = JSON.parse(readFileSync(p, "utf8")).version;
189
- if (typeof v === "string")
190
- return v;
191
- }
192
- catch {
193
- /* fall through to parent */
194
- }
188
+ mkdirSync(tmpDir, { recursive: true });
189
+ // If package.json uses pnpm-only protocols (workspace:/catalog:), a forked
190
+ // copy wouldn't `npm install` standalone. Rewrite them to concrete installed
191
+ // versions in the ARCHIVED package.json only (disk untouched) by staging the
192
+ // file list into a temp dir. No protocols → no staging (fast path).
193
+ const rewritten = files.includes("package.json") ? rewriteWorkspaceProtocols(root) : null;
194
+ let cwd = root;
195
+ let stage = null;
196
+ if (rewritten) {
197
+ stage = mkdtempSync(path.join(tmpDir, "stage-"));
198
+ for (const f of files) {
199
+ const dest = path.join(stage, f);
200
+ mkdirSync(path.dirname(dest), { recursive: true });
201
+ copyFileSync(path.join(root, f), dest);
195
202
  }
196
- const parent = dirname(dir);
197
- if (parent === dir)
198
- return null;
199
- dir = parent;
203
+ writeFileSync(path.join(stage, "package.json"), rewritten);
204
+ cwd = stage;
205
+ }
206
+ const tarball = path.join(tmpDir, `vg-source-${process.pid}-${files.length}.tgz`);
207
+ try {
208
+ await tarCreate({ cwd, file: tarball, gzip: true, portable: true }, files);
200
209
  }
201
- }
210
+ finally {
211
+ if (stage) {
212
+ rmSync(stage, { force: true, recursive: true });
213
+ }
214
+ }
215
+ const buf = readFileSync(tarball);
216
+ return {
217
+ bytes: statSync(tarball).size,
218
+ files,
219
+ path: tarball,
220
+ sha256: createHash("sha256").update(buf).digest("hex"),
221
+ };
222
+ };
202
223
  /** Extract a gzipped tar into `destDir` (created if needed). tar strips
203
224
  * leading slashes and rejects `..` paths, so extraction stays inside dest. */
204
- export async function extractSource(archivePath, destDir) {
225
+ export const extractSource = async (archivePath, destDir) => {
205
226
  mkdirSync(destDir, { recursive: true });
206
- await tarExtract({ file: archivePath, cwd: destDir });
207
- }
227
+ await tarExtract({ cwd: destDir, file: archivePath });
228
+ };
@@ -1,3 +1,4 @@
1
+ import type { JsonObject } from "./types.js";
1
2
  export type Provider = "vibedgames" | "codex";
2
3
  /**
3
4
  * Expected, user-facing failure of the codex provider. Carries a clean
@@ -14,39 +15,69 @@ export declare class CodexError extends Error {
14
15
  });
15
16
  }
16
17
  /**
17
- * Resolve the effective provider from the `--provider` flag, falling back
18
- * to the `VG_GENERATE_PROVIDER` env var and finally the vibedgames
18
+ * Resolve an explicitly named provider from the `--provider` flag, falling
19
+ * back to the `VG_GENERATE_PROVIDER` env var and finally the vibedgames
19
20
  * default. Unknown values throw so a typo (`--provider coddex`) fails
20
- * loudly instead of silently hitting the paid backend.
21
+ * loudly instead of silently hitting the paid backend. `chooseProvider`
22
+ * layers the automatic Codex routing on top of this.
21
23
  */
22
- export declare function resolveProvider(flag?: string): Provider;
23
- export type CodexInput = {
24
+ export declare const resolveProvider: (flag?: string) => Provider;
25
+ /** The OpenAI image family — the models a signed-in Codex plan serves itself. */
26
+ export declare const isOpenAiImageEndpoint: (endpointId: string) => boolean;
27
+ /**
28
+ * The `codex` binary this machine would run: `VG_CODEX_BIN` when set (and
29
+ * present), else the first `codex` on PATH. Null when there is none — the
30
+ * signal that keeps automatic routing on the vibedgames runner.
31
+ */
32
+ export declare const findCodexBinary: (env?: NodeJS.ProcessEnv) => string | null;
33
+ export interface CodexInput {
24
34
  prompt: string;
25
35
  count: number;
26
36
  sizeHint?: string;
27
37
  /** Raw reference values from the input; may be local paths or URLs. */
28
38
  referenceCandidates: string[];
29
- };
39
+ }
30
40
  /**
31
41
  * Pull the fields Codex can act on out of the parsed `run` input. Pure:
32
42
  * no filesystem access, so the reference values are returned verbatim and
33
43
  * resolved to local files later.
34
44
  */
35
- export declare function parseCodexInput(input: Record<string, unknown>): CodexInput;
45
+ export declare const parseCodexInput: (input: JsonObject) => CodexInput;
46
+ export interface AutoRouteContext {
47
+ endpointId: string;
48
+ async: boolean;
49
+ input: JsonObject;
50
+ codexInstalled: boolean;
51
+ }
52
+ export interface ProviderChoice {
53
+ provider: Provider;
54
+ /** True when nothing named the provider and Codex was picked for it. */
55
+ auto: boolean;
56
+ }
57
+ /**
58
+ * Pick the backend for one `run`. An explicit `--provider` or
59
+ * `VG_GENERATE_PROVIDER` always wins. Left unset, an OpenAI image endpoint
60
+ * goes to a locally installed Codex CLI — the same model family on the
61
+ * user's own plan, nothing billed to vibedgames — whenever Codex can honour
62
+ * the request (synchronous, local references only). Everything else stays
63
+ * on the vibedgames runner, so video/audio and non-OpenAI image models are
64
+ * unaffected by having Codex installed.
65
+ */
66
+ export declare const chooseProvider: (flag: string | undefined, ctx: AutoRouteContext) => ProviderChoice;
36
67
  /**
37
68
  * Build the natural-language instruction handed to `codex exec`. We pin
38
69
  * exact output filenames and the working directory so the generated
39
70
  * files land somewhere we can deterministically collect them.
40
71
  */
41
- export declare function buildCodexPrompt(input: CodexInput, filenames: string[], hasReferences: boolean): string;
72
+ export declare const buildCodexPrompt: (input: CodexInput, filenames: string[], hasReferences: boolean) => string;
42
73
  /**
43
74
  * Choose the on-disk destination for a collected Codex output, mirroring
44
75
  * the `--download` template semantics used for the vibedgames path
45
76
  * ({index}, {ext}, {request_id} placeholders; bare path treated as a
46
77
  * directory or a literal file by extension).
47
78
  */
48
- export declare function renderLocalTarget(template: string | undefined, index: number, ext: string, requestId: string, count: number): string;
49
- type CodexRun = {
79
+ export declare const renderLocalTarget: (template: string | undefined, index: number, ext: string, requestId: string, count: number) => string;
80
+ interface CodexRun {
50
81
  requestId: string;
51
82
  prompt: string;
52
83
  /** Absolute paths to the images Codex produced, in a stable order. */
@@ -58,7 +89,7 @@ type CodexRun = {
58
89
  * a suppressed logger would vanish.
59
90
  */
60
91
  ignoredReferences: string[];
61
- };
92
+ }
62
93
  /**
63
94
  * Delegate image generation to the local `codex` CLI. Runs
64
95
  * `codex exec` in an isolated temp workspace, then collects the produced
@@ -66,15 +97,15 @@ type CodexRun = {
66
97
  * `generated_images` store). Throws with actionable guidance when Codex
67
98
  * is missing, fails, or produces nothing.
68
99
  */
69
- export declare function generateImagesWithCodex(opts: {
70
- input: Record<string, unknown>;
71
- }): Promise<CodexRun>;
100
+ export declare const generateImagesWithCodex: (opts: {
101
+ input: JsonObject;
102
+ }) => Promise<CodexRun>;
72
103
  /**
73
104
  * Copy Codex's raw outputs to their final destinations per the
74
105
  * `--download` template (or default cwd naming). Returns the written
75
106
  * paths and any per-file failures.
76
107
  */
77
- export declare function placeCodexOutputs(rawFiles: string[], template: string | undefined, requestId: string): {
108
+ export declare const placeCodexOutputs: (rawFiles: string[], template: string | undefined, requestId: string) => {
78
109
  downloaded: string[];
79
110
  failed: {
80
111
  url: string;