vibedgames 0.0.3 → 0.0.4
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 +7 -2
- package/dist/.tsbuildinfo +1 -0
- package/dist/commands/completions.d.ts +7 -0
- package/dist/commands/completions.js +138 -0
- package/dist/commands/deploy.d.ts +9 -9
- package/dist/commands/deploy.js +6 -0
- package/dist/commands/init.d.ts +15 -15
- package/dist/commands/login.js +2 -2
- package/dist/commands/media.d.ts +1 -0
- package/dist/commands/media.js +401 -0
- package/dist/commands/new.d.ts +26 -0
- package/dist/commands/new.js +229 -0
- package/dist/index.js +6 -0
- package/dist/lib/api.js +14 -7
- package/dist/lib/config-file.js +18 -3
- package/dist/lib/config.js +5 -4
- package/dist/lib/media-args.d.ts +28 -0
- package/dist/lib/media-args.js +199 -0
- package/dist/lib/media-download.d.ts +31 -0
- package/dist/lib/media-download.js +190 -0
- package/dist/lib/media-poll.d.ts +17 -0
- package/dist/lib/media-poll.js +80 -0
- package/dist/lib/media-upload.d.ts +12 -0
- package/dist/lib/media-upload.js +51 -0
- package/dist/lib/types.d.ts +2 -0
- package/dist/lib/types.js +4 -0
- package/package.json +11 -9
- package/dist/commands/list.d.ts +0 -1
- package/dist/commands/list.js +0 -23
- package/dist/commands/rm.d.ts +0 -12
- package/dist/commands/rm.js +0 -35
- package/dist/commands/skills.d.ts +0 -8
- package/dist/commands/skills.js +0 -53
- package/dist/prompt-E6QYABXJ.js +0 -754
|
@@ -0,0 +1,229 @@
|
|
|
1
|
+
import { existsSync, mkdirSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
+
import { resolve } from "node:path";
|
|
3
|
+
import { defineCommand } from "citty";
|
|
4
|
+
import consola from "consola";
|
|
5
|
+
import tiged from "tiged";
|
|
6
|
+
const SLUG_RE = /^[a-z0-9][a-z0-9-]*[a-z0-9]$/;
|
|
7
|
+
const ENGINES = {
|
|
8
|
+
// phaserjs/template-vite-ts is the official Phaser 4 + Vite + TypeScript
|
|
9
|
+
// starter (despite the description still saying "Phaser 3" — package.json
|
|
10
|
+
// pins phaser@^4). Pulled fresh on each `vg new`.
|
|
11
|
+
phaser: {
|
|
12
|
+
repo: "phaserjs/template-vite-ts",
|
|
13
|
+
label: "Phaser 4 + Vite + TypeScript (official)",
|
|
14
|
+
skill: "phaser",
|
|
15
|
+
postNotes: [
|
|
16
|
+
"The phaser template ships a `log.js` telemetry shim and uses `vite/config.*.mjs` for build configs — both are upstream, not vibedgames.",
|
|
17
|
+
],
|
|
18
|
+
},
|
|
19
|
+
// No officially-blessed Three.js starter exists. Using the most-starred
|
|
20
|
+
// community Vite+TS starter that's been kept current.
|
|
21
|
+
threejs: {
|
|
22
|
+
repo: "pachoclo/vite-threejs-ts-template",
|
|
23
|
+
label: "Three.js + Vite + TypeScript (community)",
|
|
24
|
+
skill: "threejs",
|
|
25
|
+
},
|
|
26
|
+
// React Three Fiber. Same author as the threejs preset — Vite + TS +
|
|
27
|
+
// React 18 + R3F 8 + drei, with leva and r3f-perf wired up for debug.
|
|
28
|
+
// The pmndrs org doesn't ship an official R3F starter (its templates
|
|
29
|
+
// are all Next.js-based), and the highest-starred R3F template on
|
|
30
|
+
// GitHub is still on CRA + React 17, so this is the cleanest current
|
|
31
|
+
// option for an agent.
|
|
32
|
+
"react-r3f": {
|
|
33
|
+
repo: "pachoclo/vite-r3f-ts-template",
|
|
34
|
+
label: "React + React Three Fiber + Vite + TypeScript (community)",
|
|
35
|
+
skill: "threejs",
|
|
36
|
+
},
|
|
37
|
+
// Engine-agnostic fallback for "I'll wire it up myself" / non-canvas
|
|
38
|
+
// games. Stays inline so we don't get blocked on a network fetch when
|
|
39
|
+
// the user explicitly asked for a minimal start.
|
|
40
|
+
none: {
|
|
41
|
+
inline: true,
|
|
42
|
+
label: "minimal Vite + TypeScript canvas",
|
|
43
|
+
skill: "deploy",
|
|
44
|
+
},
|
|
45
|
+
};
|
|
46
|
+
const NONE_FILES = [
|
|
47
|
+
{
|
|
48
|
+
path: "package.json",
|
|
49
|
+
content: (slug) => `${JSON.stringify({
|
|
50
|
+
name: slug,
|
|
51
|
+
version: "0.0.0",
|
|
52
|
+
private: true,
|
|
53
|
+
type: "module",
|
|
54
|
+
scripts: {
|
|
55
|
+
dev: "vite",
|
|
56
|
+
build: "vite build",
|
|
57
|
+
preview: "vite preview",
|
|
58
|
+
},
|
|
59
|
+
devDependencies: { typescript: "^5.6.0", vite: "^7.0.0" },
|
|
60
|
+
}, null, 2)}\n`,
|
|
61
|
+
},
|
|
62
|
+
{
|
|
63
|
+
path: "tsconfig.json",
|
|
64
|
+
content: () => `${JSON.stringify({
|
|
65
|
+
compilerOptions: {
|
|
66
|
+
target: "ES2022",
|
|
67
|
+
lib: ["ES2022", "DOM", "DOM.Iterable"],
|
|
68
|
+
module: "Preserve",
|
|
69
|
+
moduleResolution: "Bundler",
|
|
70
|
+
strict: true,
|
|
71
|
+
noUncheckedIndexedAccess: true,
|
|
72
|
+
isolatedModules: true,
|
|
73
|
+
esModuleInterop: true,
|
|
74
|
+
skipLibCheck: true,
|
|
75
|
+
noEmit: true,
|
|
76
|
+
types: ["vite/client"],
|
|
77
|
+
},
|
|
78
|
+
include: ["src", "vite.config.ts"],
|
|
79
|
+
}, null, 2)}\n`,
|
|
80
|
+
},
|
|
81
|
+
{
|
|
82
|
+
path: "vite.config.ts",
|
|
83
|
+
content: () => `import { defineConfig } from "vite";\n\nexport default defineConfig({\n base: "./",\n server: { port: 5173 },\n});\n`,
|
|
84
|
+
},
|
|
85
|
+
{
|
|
86
|
+
path: "index.html",
|
|
87
|
+
content: (slug) => `<!doctype html>\n<html lang="en">\n <head>\n <meta charset="UTF-8" />\n <title>${slug}</title>\n <style>html,body{margin:0;background:#0f1020;color:#fff;font-family:system-ui,sans-serif;}#game{display:flex;align-items:center;justify-content:center;height:100vh;}</style>\n </head>\n <body>\n <div id="game"></div>\n <script type="module" src="/src/main.ts"></script>\n </body>\n</html>\n`,
|
|
88
|
+
},
|
|
89
|
+
{
|
|
90
|
+
path: "src/main.ts",
|
|
91
|
+
content: (slug) => `const canvas = document.createElement("canvas");\ncanvas.width = 800;\ncanvas.height = 600;\ndocument.getElementById("game")!.appendChild(canvas);\nconst ctx = canvas.getContext("2d")!;\n\nctx.fillStyle = "#fff";\nctx.textAlign = "center";\nctx.font = "28px system-ui";\nctx.fillText("${slug}", canvas.width / 2, canvas.height / 2 - 8);\nctx.font = "14px system-ui";\nctx.fillStyle = "#aaa";\nctx.fillText("Edit src/main.ts to start building.", canvas.width / 2, canvas.height / 2 + 22);\n`,
|
|
92
|
+
},
|
|
93
|
+
{ path: ".gitignore", content: () => `node_modules\ndist\n.DS_Store\n` },
|
|
94
|
+
];
|
|
95
|
+
export const newCommand = defineCommand({
|
|
96
|
+
meta: {
|
|
97
|
+
name: "new",
|
|
98
|
+
description: "Scaffold a new browser game. Pulls an official engine template (phaser, threejs) or generates a minimal canvas starter.",
|
|
99
|
+
},
|
|
100
|
+
args: {
|
|
101
|
+
slug: {
|
|
102
|
+
type: "positional",
|
|
103
|
+
description: "Lowercase, hyphenated slug — used as the directory name and deploy subdomain.",
|
|
104
|
+
required: true,
|
|
105
|
+
},
|
|
106
|
+
engine: {
|
|
107
|
+
type: "string",
|
|
108
|
+
description: "Engine preset: phaser (default), threejs, or none.",
|
|
109
|
+
default: "phaser",
|
|
110
|
+
},
|
|
111
|
+
template: {
|
|
112
|
+
type: "string",
|
|
113
|
+
description: "Override the engine preset and fetch from an arbitrary degit spec (e.g. owner/repo, owner/repo#branch). Skips the engine preset entirely.",
|
|
114
|
+
},
|
|
115
|
+
here: {
|
|
116
|
+
type: "boolean",
|
|
117
|
+
description: "Write into the current directory instead of creating <slug>/.",
|
|
118
|
+
default: false,
|
|
119
|
+
},
|
|
120
|
+
force: {
|
|
121
|
+
type: "boolean",
|
|
122
|
+
description: "Overwrite an existing target directory.",
|
|
123
|
+
default: false,
|
|
124
|
+
},
|
|
125
|
+
},
|
|
126
|
+
run: async ({ args }) => {
|
|
127
|
+
const slug = args.slug.trim().toLowerCase();
|
|
128
|
+
if (!SLUG_RE.test(slug)) {
|
|
129
|
+
consola.error(`Invalid slug: ${args.slug}\n Use lowercase letters, digits, and hyphens (e.g. "asteroid-belt").`);
|
|
130
|
+
process.exit(1);
|
|
131
|
+
}
|
|
132
|
+
const preset = args.template
|
|
133
|
+
? { repo: args.template, label: `custom: ${args.template}`, skill: "deploy" }
|
|
134
|
+
: ENGINES[args.engine];
|
|
135
|
+
if (!preset) {
|
|
136
|
+
consola.error(`Unknown engine: ${args.engine}. Use one of: ${Object.keys(ENGINES).join(", ")}.`);
|
|
137
|
+
process.exit(1);
|
|
138
|
+
}
|
|
139
|
+
const target = args.here ? process.cwd() : resolve(process.cwd(), slug);
|
|
140
|
+
if (!args.here) {
|
|
141
|
+
if (existsSync(target) && !args.force) {
|
|
142
|
+
consola.error(`Directory ${target} already exists. Pass --force to overwrite, or pick a different slug.`);
|
|
143
|
+
process.exit(1);
|
|
144
|
+
}
|
|
145
|
+
mkdirSync(target, { recursive: true });
|
|
146
|
+
}
|
|
147
|
+
consola.start(`Scaffolding ${slug} with ${preset.label}`);
|
|
148
|
+
if ("inline" in preset) {
|
|
149
|
+
writeInlineTemplate(target, slug, args.force);
|
|
150
|
+
}
|
|
151
|
+
else {
|
|
152
|
+
try {
|
|
153
|
+
await fetchTemplate(preset.repo, target, args.force);
|
|
154
|
+
}
|
|
155
|
+
catch (err) {
|
|
156
|
+
consola.error(`Failed to fetch template ${preset.repo}: ${err instanceof Error ? err.message : String(err)}\n ` +
|
|
157
|
+
`Check your network connection, or pass --engine none to scaffold a minimal starter offline.`);
|
|
158
|
+
process.exit(1);
|
|
159
|
+
}
|
|
160
|
+
}
|
|
161
|
+
// Post-process: name + vibedgames.json + README link to skills.
|
|
162
|
+
rewritePackageName(target, slug);
|
|
163
|
+
writeVibedgamesJson(target, slug);
|
|
164
|
+
writeReadme(target, slug, preset);
|
|
165
|
+
consola.success(`Scaffolded ${slug} in ${target}`);
|
|
166
|
+
if ("postNotes" in preset && preset.postNotes) {
|
|
167
|
+
for (const n of preset.postNotes)
|
|
168
|
+
consola.info(n);
|
|
169
|
+
}
|
|
170
|
+
consola.log("");
|
|
171
|
+
consola.info("Next steps:");
|
|
172
|
+
consola.log(` cd ${args.here ? "." : slug}`);
|
|
173
|
+
consola.log(" npm install");
|
|
174
|
+
consola.log(" npm run dev");
|
|
175
|
+
consola.log(" # then: npm run build && vg deploy ./dist");
|
|
176
|
+
consola.log("");
|
|
177
|
+
consola.log(`Skill to load for engine work: ${preset.skill} (Claude Code: triggered automatically by package.json deps)`);
|
|
178
|
+
},
|
|
179
|
+
});
|
|
180
|
+
async function fetchTemplate(repo, target, force) {
|
|
181
|
+
const emitter = tiged(repo, { force, verbose: false, mode: "tar" });
|
|
182
|
+
await emitter.clone(target);
|
|
183
|
+
}
|
|
184
|
+
function writeInlineTemplate(target, slug, force) {
|
|
185
|
+
for (const file of NONE_FILES) {
|
|
186
|
+
const path = resolve(target, file.path);
|
|
187
|
+
if (existsSync(path) && !force)
|
|
188
|
+
continue;
|
|
189
|
+
mkdirSync(path.slice(0, path.lastIndexOf("/")), { recursive: true });
|
|
190
|
+
writeFileSync(path, file.content(slug));
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
function rewritePackageName(target, slug) {
|
|
194
|
+
const pkgPath = resolve(target, "package.json");
|
|
195
|
+
if (!existsSync(pkgPath))
|
|
196
|
+
return;
|
|
197
|
+
try {
|
|
198
|
+
const raw = readFileSync(pkgPath, "utf8");
|
|
199
|
+
const pkg = JSON.parse(raw);
|
|
200
|
+
pkg.name = slug;
|
|
201
|
+
delete pkg.repository;
|
|
202
|
+
delete pkg.bugs;
|
|
203
|
+
delete pkg.homepage;
|
|
204
|
+
writeFileSync(pkgPath, `${JSON.stringify(pkg, null, 2)}\n`);
|
|
205
|
+
}
|
|
206
|
+
catch (err) {
|
|
207
|
+
consola.warn(`Could not rewrite package.json name to ${slug}: ${err instanceof Error ? err.message : String(err)}`);
|
|
208
|
+
}
|
|
209
|
+
}
|
|
210
|
+
function writeVibedgamesJson(target, slug) {
|
|
211
|
+
writeFileSync(resolve(target, "vibedgames.json"), `${JSON.stringify({ slug, name: slug.replace(/-/g, " ") }, null, 2)}\n`);
|
|
212
|
+
}
|
|
213
|
+
function writeReadme(target, slug, preset) {
|
|
214
|
+
const readmePath = resolve(target, "README.md");
|
|
215
|
+
// Keep the template's README if present (it usually has engine docs).
|
|
216
|
+
// Append a vibedgames-specific footer so devs see the deploy flow.
|
|
217
|
+
const footer = `\n\n---\n\n## Vibedgames\n\n` +
|
|
218
|
+
`This project was scaffolded with \`vg new ${slug}\` (${preset.label}).\n\n` +
|
|
219
|
+
`\`\`\`sh\nnpm install\nnpm run dev # local preview\nnpm run build\nvg deploy ./dist # ships to ${slug}.vibedgames.com\n\`\`\`\n\n` +
|
|
220
|
+
`For engine-specific work, the \`${preset.skill}\` skill loads automatically in Claude Code.\n`;
|
|
221
|
+
if (existsSync(readmePath)) {
|
|
222
|
+
const existing = readFileSync(readmePath, "utf8");
|
|
223
|
+
if (existing.includes("## Vibedgames"))
|
|
224
|
+
return;
|
|
225
|
+
writeFileSync(readmePath, existing.replace(/\s*$/, "") + footer);
|
|
226
|
+
return;
|
|
227
|
+
}
|
|
228
|
+
writeFileSync(readmePath, `# ${slug}${footer}`);
|
|
229
|
+
}
|
package/dist/index.js
CHANGED
|
@@ -1,10 +1,13 @@
|
|
|
1
1
|
#!/usr/bin/env node
|
|
2
2
|
import { readFileSync } from "node:fs";
|
|
3
3
|
import { defineCommand, runMain } from "citty";
|
|
4
|
+
import { completionsCommand } from "./commands/completions.js";
|
|
4
5
|
import { deployCommand } from "./commands/deploy.js";
|
|
5
6
|
import { initCommand } from "./commands/init.js";
|
|
6
7
|
import { loginCommand } from "./commands/login.js";
|
|
7
8
|
import { logoutCommand } from "./commands/logout.js";
|
|
9
|
+
import { mediaCommand } from "./commands/media.js";
|
|
10
|
+
import { newCommand } from "./commands/new.js";
|
|
8
11
|
import { whoamiCommand } from "./commands/whoami.js";
|
|
9
12
|
const pkg = JSON.parse(readFileSync(new URL("../package.json", import.meta.url), "utf8"));
|
|
10
13
|
const main = defineCommand({
|
|
@@ -14,10 +17,13 @@ const main = defineCommand({
|
|
|
14
17
|
description: "vibedgames CLI",
|
|
15
18
|
},
|
|
16
19
|
subCommands: {
|
|
20
|
+
new: newCommand,
|
|
17
21
|
init: initCommand,
|
|
18
22
|
login: loginCommand,
|
|
19
23
|
logout: logoutCommand,
|
|
20
24
|
deploy: deployCommand,
|
|
25
|
+
media: mediaCommand,
|
|
26
|
+
completions: completionsCommand,
|
|
21
27
|
whoami: whoamiCommand,
|
|
22
28
|
},
|
|
23
29
|
});
|
package/dist/lib/api.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { createTRPCClient, httpBatchLink } from "@trpc/client";
|
|
1
|
+
import { createTRPCClient, httpBatchLink, httpLink, splitLink } from "@trpc/client";
|
|
2
2
|
import superjson from "superjson";
|
|
3
3
|
import { getBaseUrl, getToken } from "./config.js";
|
|
4
4
|
/** Authenticated client — requires a saved session token. */
|
|
@@ -7,20 +7,27 @@ export function createClient() {
|
|
|
7
7
|
if (!token) {
|
|
8
8
|
throw new Error("Not logged in. Run `vg login` to authenticate.");
|
|
9
9
|
}
|
|
10
|
+
const url = `${getBaseUrl()}/api/trpc`;
|
|
11
|
+
const headers = () => ({ Authorization: `Bearer ${token}` });
|
|
10
12
|
return createTRPCClient({
|
|
11
13
|
links: [
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
}),
|
|
14
|
+
// Media input bytes use presigned uploads, but mutations can still
|
|
15
|
+
// carry model params. Keep writes off httpBatchLink so one batch
|
|
16
|
+
// body cannot grow with concurrent calls.
|
|
17
|
+
splitLink({
|
|
18
|
+
condition: (op) => op.type === "mutation",
|
|
19
|
+
true: httpLink({ url, transformer: superjson, headers }),
|
|
20
|
+
false: httpBatchLink({ url, transformer: superjson, headers }),
|
|
18
21
|
}),
|
|
19
22
|
],
|
|
20
23
|
});
|
|
21
24
|
}
|
|
22
25
|
/** Unauthenticated client — for login flow. */
|
|
23
26
|
export function createPublicClient(baseUrl) {
|
|
27
|
+
// The login-flow procedures carry no large payloads, so the plain
|
|
28
|
+
// batch link is fine here — no need for the splitLink dance the
|
|
29
|
+
// authenticated client uses to keep `media.run` mutations off the
|
|
30
|
+
// batched path.
|
|
24
31
|
return createTRPCClient({
|
|
25
32
|
links: [
|
|
26
33
|
httpBatchLink({
|
package/dist/lib/config-file.js
CHANGED
|
@@ -1,9 +1,24 @@
|
|
|
1
1
|
import { existsSync, readFileSync, writeFileSync } from "node:fs";
|
|
2
|
-
import { join } from "node:path";
|
|
2
|
+
import { dirname, join, resolve } from "node:path";
|
|
3
3
|
const FILENAME = "vibedgames.json";
|
|
4
|
+
// Walk from `dir` up toward the filesystem root, stopping at the first
|
|
5
|
+
// vibedgames.json. Lets `vg deploy ./dist` pick up the config from the
|
|
6
|
+
// project root even when the build step doesn't copy it into the output.
|
|
7
|
+
function findConfigPath(dir) {
|
|
8
|
+
let current = resolve(dir);
|
|
9
|
+
while (true) {
|
|
10
|
+
const candidate = join(current, FILENAME);
|
|
11
|
+
if (existsSync(candidate))
|
|
12
|
+
return candidate;
|
|
13
|
+
const parent = dirname(current);
|
|
14
|
+
if (parent === current)
|
|
15
|
+
return null;
|
|
16
|
+
current = parent;
|
|
17
|
+
}
|
|
18
|
+
}
|
|
4
19
|
export function readProjectConfig(dir) {
|
|
5
|
-
const path =
|
|
6
|
-
if (!
|
|
20
|
+
const path = findConfigPath(dir);
|
|
21
|
+
if (!path)
|
|
7
22
|
return null;
|
|
8
23
|
const raw = readFileSync(path, "utf-8");
|
|
9
24
|
const parsed = JSON.parse(raw);
|
package/dist/lib/config.js
CHANGED
|
@@ -36,10 +36,11 @@ export function clearConfig() {
|
|
|
36
36
|
}
|
|
37
37
|
}
|
|
38
38
|
export function getBaseUrl() {
|
|
39
|
-
return
|
|
40
|
-
getConfig()?.baseUrl ??
|
|
41
|
-
"https://vibedgames.com");
|
|
39
|
+
return process.env.VG_API_URL ?? getConfig()?.baseUrl ?? "https://vibedgames.com";
|
|
42
40
|
}
|
|
43
41
|
export function getToken() {
|
|
44
|
-
|
|
42
|
+
// VG_TOKEN lets local/CI runs authenticate without touching the saved
|
|
43
|
+
// login (e.g. a seeded dev session), so headless testing never clobbers
|
|
44
|
+
// the user's real `vg login` credentials.
|
|
45
|
+
return process.env.VG_TOKEN ?? getConfig()?.token ?? null;
|
|
45
46
|
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Parse `--<key> value` pairs from argv into a JS object, JSON-decoding
|
|
3
|
+
* values that look like JSON (true/false/null/numbers/objects/arrays) and
|
|
4
|
+
* leaving everything else as a string. Mirrors genmedia's parse-value
|
|
5
|
+
* behavior so skills targeting that surface produce the same input shapes.
|
|
6
|
+
*
|
|
7
|
+
* Handles both `--key value` and the GNU-style `--key=value`. Without
|
|
8
|
+
* `=` support, `--prompt=hello` would silently send the malformed key
|
|
9
|
+
* `"prompt=hello"` to fal, and `--async=true` would slip past the
|
|
10
|
+
* RUN_RESERVED_FLAGS guard as a bogus model param.
|
|
11
|
+
*/
|
|
12
|
+
export declare function parseRunInput(argv: string[]): Record<string, unknown>;
|
|
13
|
+
export type LocalFile = {
|
|
14
|
+
path: string;
|
|
15
|
+
filename: string;
|
|
16
|
+
contentType: string;
|
|
17
|
+
};
|
|
18
|
+
/**
|
|
19
|
+
* Probe a path the user explicitly asked us to read (e.g.
|
|
20
|
+
* `vg media upload <path>`). Skips the path-shape heuristic so a bare
|
|
21
|
+
* filename with a non-media extension — `model.glb`, `scene.fbx`,
|
|
22
|
+
* `data.ply`, even `LICENSE` — still works.
|
|
23
|
+
*/
|
|
24
|
+
export declare function readExplicitLocalFile(value: string): LocalFile | null;
|
|
25
|
+
export declare function parseDownloadFlag(argv: string[]): {
|
|
26
|
+
mode: "off" | "on";
|
|
27
|
+
template?: string;
|
|
28
|
+
};
|
|
@@ -0,0 +1,199 @@
|
|
|
1
|
+
import { statSync } from "node:fs";
|
|
2
|
+
import { homedir } from "node:os";
|
|
3
|
+
import { basename, extname, isAbsolute, join, resolve } from "node:path";
|
|
4
|
+
// `--json`/`--help`/`--quiet` are global; `--async` switches the run path
|
|
5
|
+
// in citty; `--download` takes an optional template. None of them should
|
|
6
|
+
// leak through to fal as model parameters. `--logs` is intentionally NOT
|
|
7
|
+
// here: it's a `vg media status` flag, not a `run` flag, so swallowing it
|
|
8
|
+
// would block users from passing a legitimate `logs` parameter to a
|
|
9
|
+
// model endpoint.
|
|
10
|
+
const RUN_RESERVED_FLAGS = new Set([
|
|
11
|
+
"--json",
|
|
12
|
+
"--help",
|
|
13
|
+
"--quiet",
|
|
14
|
+
"--async",
|
|
15
|
+
"--download",
|
|
16
|
+
]);
|
|
17
|
+
/**
|
|
18
|
+
* Parse `--<key> value` pairs from argv into a JS object, JSON-decoding
|
|
19
|
+
* values that look like JSON (true/false/null/numbers/objects/arrays) and
|
|
20
|
+
* leaving everything else as a string. Mirrors genmedia's parse-value
|
|
21
|
+
* behavior so skills targeting that surface produce the same input shapes.
|
|
22
|
+
*
|
|
23
|
+
* Handles both `--key value` and the GNU-style `--key=value`. Without
|
|
24
|
+
* `=` support, `--prompt=hello` would silently send the malformed key
|
|
25
|
+
* `"prompt=hello"` to fal, and `--async=true` would slip past the
|
|
26
|
+
* RUN_RESERVED_FLAGS guard as a bogus model param.
|
|
27
|
+
*/
|
|
28
|
+
export function parseRunInput(argv) {
|
|
29
|
+
const out = {};
|
|
30
|
+
for (let i = 0; i < argv.length; i++) {
|
|
31
|
+
const arg = argv[i];
|
|
32
|
+
if (!arg || !arg.startsWith("--"))
|
|
33
|
+
continue;
|
|
34
|
+
// POSIX `--` argument terminator: stop interpreting subsequent
|
|
35
|
+
// tokens as flags. Without this guard, `--` itself parses as
|
|
36
|
+
// `{ "": <next token> }` and pollutes the request body.
|
|
37
|
+
if (arg === "--")
|
|
38
|
+
break;
|
|
39
|
+
const eqIdx = arg.indexOf("=");
|
|
40
|
+
const name = eqIdx === -1 ? arg : arg.slice(0, eqIdx);
|
|
41
|
+
const inlineValue = eqIdx === -1 ? undefined : arg.slice(eqIdx + 1);
|
|
42
|
+
if (RUN_RESERVED_FLAGS.has(name)) {
|
|
43
|
+
// --download (without inline value) optionally consumes the next
|
|
44
|
+
// token as its template. With `--download=foo` the value is
|
|
45
|
+
// already attached and we move on without skipping anything.
|
|
46
|
+
if (name === "--download" && inlineValue === undefined) {
|
|
47
|
+
const next = argv[i + 1];
|
|
48
|
+
if (next && !next.startsWith("--"))
|
|
49
|
+
i++;
|
|
50
|
+
}
|
|
51
|
+
continue;
|
|
52
|
+
}
|
|
53
|
+
const key = name.slice(2);
|
|
54
|
+
if (inlineValue !== undefined) {
|
|
55
|
+
assign(out, key, parseValue(inlineValue));
|
|
56
|
+
continue;
|
|
57
|
+
}
|
|
58
|
+
const next = argv[i + 1];
|
|
59
|
+
if (next === undefined || next.startsWith("--")) {
|
|
60
|
+
assign(out, key, true);
|
|
61
|
+
continue;
|
|
62
|
+
}
|
|
63
|
+
assign(out, key, parseValue(next));
|
|
64
|
+
i++;
|
|
65
|
+
}
|
|
66
|
+
return out;
|
|
67
|
+
}
|
|
68
|
+
function assign(out, key, value) {
|
|
69
|
+
if (key in out) {
|
|
70
|
+
const existing = out[key];
|
|
71
|
+
if (Array.isArray(existing))
|
|
72
|
+
existing.push(value);
|
|
73
|
+
else
|
|
74
|
+
out[key] = [existing, value];
|
|
75
|
+
return;
|
|
76
|
+
}
|
|
77
|
+
out[key] = value;
|
|
78
|
+
}
|
|
79
|
+
function parseValue(raw) {
|
|
80
|
+
if (raw === "true")
|
|
81
|
+
return true;
|
|
82
|
+
if (raw === "false")
|
|
83
|
+
return false;
|
|
84
|
+
if (raw === "null")
|
|
85
|
+
return null;
|
|
86
|
+
if (raw.length > 0 && (raw[0] === "{" || raw[0] === "[")) {
|
|
87
|
+
try {
|
|
88
|
+
return JSON.parse(raw);
|
|
89
|
+
}
|
|
90
|
+
catch {
|
|
91
|
+
return raw;
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
if (/^-?\d+(\.\d+)?$/.test(raw)) {
|
|
95
|
+
const n = Number(raw);
|
|
96
|
+
if (Number.isFinite(n))
|
|
97
|
+
return n;
|
|
98
|
+
}
|
|
99
|
+
return raw;
|
|
100
|
+
}
|
|
101
|
+
/**
|
|
102
|
+
* Probe a path the user explicitly asked us to read (e.g.
|
|
103
|
+
* `vg media upload <path>`). Skips the path-shape heuristic so a bare
|
|
104
|
+
* filename with a non-media extension — `model.glb`, `scene.fbx`,
|
|
105
|
+
* `data.ply`, even `LICENSE` — still works.
|
|
106
|
+
*/
|
|
107
|
+
export function readExplicitLocalFile(value) {
|
|
108
|
+
if (value.startsWith("http://") || value.startsWith("https://"))
|
|
109
|
+
return null;
|
|
110
|
+
if (value.startsWith("data:"))
|
|
111
|
+
return null;
|
|
112
|
+
if (value.length === 0)
|
|
113
|
+
return null;
|
|
114
|
+
const expanded = expandHome(value);
|
|
115
|
+
const abs = isAbsolute(expanded) ? expanded : resolve(expanded);
|
|
116
|
+
let stat;
|
|
117
|
+
try {
|
|
118
|
+
stat = statSync(abs);
|
|
119
|
+
}
|
|
120
|
+
catch {
|
|
121
|
+
return null;
|
|
122
|
+
}
|
|
123
|
+
if (!stat.isFile())
|
|
124
|
+
return null;
|
|
125
|
+
return {
|
|
126
|
+
path: abs,
|
|
127
|
+
filename: basename(abs),
|
|
128
|
+
contentType: contentTypeForPath(abs),
|
|
129
|
+
};
|
|
130
|
+
}
|
|
131
|
+
function expandHome(value) {
|
|
132
|
+
// node:path's resolve() doesn't expand `~` — that's a shell feature.
|
|
133
|
+
// We expand it ourselves so quoted paths like `"~/photo.png"` work.
|
|
134
|
+
if (value === "~")
|
|
135
|
+
return homedir();
|
|
136
|
+
if (value.startsWith("~/") || value.startsWith("~\\")) {
|
|
137
|
+
return join(homedir(), value.slice(2));
|
|
138
|
+
}
|
|
139
|
+
return value;
|
|
140
|
+
}
|
|
141
|
+
function contentTypeForPath(path) {
|
|
142
|
+
// extname splits on basename, so a dotted directory like
|
|
143
|
+
// "/home/user/my.project/texture" correctly yields "" instead of
|
|
144
|
+
// mistaking "project/texture" for the extension.
|
|
145
|
+
const ext = extname(path).slice(1).toLowerCase();
|
|
146
|
+
const map = {
|
|
147
|
+
png: "image/png",
|
|
148
|
+
jpg: "image/jpeg",
|
|
149
|
+
jpeg: "image/jpeg",
|
|
150
|
+
webp: "image/webp",
|
|
151
|
+
gif: "image/gif",
|
|
152
|
+
bmp: "image/bmp",
|
|
153
|
+
tif: "image/tiff",
|
|
154
|
+
tiff: "image/tiff",
|
|
155
|
+
avif: "image/avif",
|
|
156
|
+
mp4: "video/mp4",
|
|
157
|
+
mov: "video/quicktime",
|
|
158
|
+
webm: "video/webm",
|
|
159
|
+
mp3: "audio/mpeg",
|
|
160
|
+
wav: "audio/wav",
|
|
161
|
+
ogg: "audio/ogg",
|
|
162
|
+
flac: "audio/flac",
|
|
163
|
+
m4a: "audio/mp4",
|
|
164
|
+
txt: "text/plain",
|
|
165
|
+
json: "application/json",
|
|
166
|
+
};
|
|
167
|
+
return map[ext] ?? "application/octet-stream";
|
|
168
|
+
}
|
|
169
|
+
export function parseDownloadFlag(argv) {
|
|
170
|
+
let lastIdx = -1;
|
|
171
|
+
let inlineValue;
|
|
172
|
+
for (let i = 0; i < argv.length; i++) {
|
|
173
|
+
const arg = argv[i];
|
|
174
|
+
if (!arg)
|
|
175
|
+
continue;
|
|
176
|
+
if (arg === "--")
|
|
177
|
+
break;
|
|
178
|
+
if (arg === "--download") {
|
|
179
|
+
lastIdx = i;
|
|
180
|
+
inlineValue = undefined;
|
|
181
|
+
}
|
|
182
|
+
else if (arg.startsWith("--download=")) {
|
|
183
|
+
lastIdx = i;
|
|
184
|
+
inlineValue = arg.slice("--download=".length);
|
|
185
|
+
}
|
|
186
|
+
}
|
|
187
|
+
if (lastIdx === -1)
|
|
188
|
+
return { mode: "off" };
|
|
189
|
+
const candidate = inlineValue ?? argv[lastIdx + 1];
|
|
190
|
+
if (candidate === "false")
|
|
191
|
+
return { mode: "off" };
|
|
192
|
+
if (candidate === undefined ||
|
|
193
|
+
candidate === "" ||
|
|
194
|
+
candidate.startsWith("--") ||
|
|
195
|
+
candidate === "true") {
|
|
196
|
+
return { mode: "on" };
|
|
197
|
+
}
|
|
198
|
+
return { mode: "on", template: candidate };
|
|
199
|
+
}
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
type MediaRef = {
|
|
2
|
+
url: string;
|
|
3
|
+
filename: string;
|
|
4
|
+
contentType: string | null;
|
|
5
|
+
};
|
|
6
|
+
/**
|
|
7
|
+
* Walk a fal result payload and pull out anything that looks like a media
|
|
8
|
+
* output. fal endpoints embed outputs in objects shaped like
|
|
9
|
+
* `{ url, content_type, file_name, ... }`; we accept image/video/audio
|
|
10
|
+
* content types and also fall back to URL extension when content_type is
|
|
11
|
+
* absent (some endpoints omit it).
|
|
12
|
+
*/
|
|
13
|
+
export declare function extractMediaRefs(result: unknown): MediaRef[];
|
|
14
|
+
type DownloadResult = {
|
|
15
|
+
downloaded: string[];
|
|
16
|
+
failed: {
|
|
17
|
+
url: string;
|
|
18
|
+
error: string;
|
|
19
|
+
}[];
|
|
20
|
+
};
|
|
21
|
+
/**
|
|
22
|
+
* Download all extracted media refs to disk, materializing the path from
|
|
23
|
+
* a template. Supported placeholders: {index}, {name}, {ext}, {request_id}.
|
|
24
|
+
* Falls back to the source filename in the cwd when no template is given.
|
|
25
|
+
*/
|
|
26
|
+
export declare function downloadMedia(opts: {
|
|
27
|
+
refs: MediaRef[];
|
|
28
|
+
template?: string;
|
|
29
|
+
requestId: string;
|
|
30
|
+
}): Promise<DownloadResult>;
|
|
31
|
+
export {};
|