Repository navigation
Expand file tree
/
Copy pathcli.mjs
More file actions
349 lines (331 loc) · 14.6 KB
/
Copy pathcli.mjs
File metadata and controls
349 lines (331 loc) · 14.6 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
#!/usr/bin/env node
import fs from 'node:fs';
import path from 'node:path';
import process from 'node:process';
import { spawn } from 'node:child_process';
import { agentTools, runStudioCommand, studioHelp } from './src/agent-tools.mjs';
import { isMainModule } from './src/main-module.mjs';
import { inspectFfmpeg } from './src/ffmpeg.mjs';
import { DATA_ROOT, PROJECT_ROOT, StudioError, ensureDataRoot, safeResolveWithin } from './src/paths.mjs';
const DEFAULT_PORT = 32146;
export function validatePort(value) {
const port = Number(value);
if (!Number.isInteger(port) || port < 1 || port > 65_535) {
throw new StudioError('INVALID_PORT', `Invalid local server port: ${value}. Set GAME_TRAILER_STUDIO_PORT to a number from 1 to 65535.`);
}
return port;
}
const API_TIMEOUT_MS = 35_000;
const TERMINAL_JOB_STATUSES = new Set(['complete', 'failed', 'cancelled', 'canceled']);
function serverPort() {
return validatePort(process.env.GAME_TRAILER_STUDIO_PORT || DEFAULT_PORT);
}
function apiRoot() {
return `http://127.0.0.1:${serverPort()}/api`;
}
export function parseArguments(values) {
const positional = [];
const flags = {};
for (let index = 0; index < values.length; index += 1) {
const value = values[index];
if (!value.startsWith('--')) {
positional.push(value);
continue;
}
const body = value.slice(2);
const equals = body.indexOf('=');
const rawKey = equals === -1 ? body : body.slice(0, equals);
const inline = equals === -1 ? undefined : body.slice(equals + 1);
if (rawKey.startsWith('no-')) {
flags[rawKey.slice(3)] = false;
} else if (inline !== undefined) {
flags[rawKey] = inline;
} else if (values[index + 1] && !values[index + 1].startsWith('--')) {
flags[rawKey] = values[++index];
} else {
flags[rawKey] = true;
}
}
return { positional, flags };
}
function boolFlag(value, fallback = false) {
if (value === undefined) return fallback;
if (typeof value === 'boolean') return value;
return !['0', 'false', 'nee', 'no', 'off'].includes(String(value).toLowerCase());
}
export async function api(pathname, { method = 'GET', body, start = true } = {}) {
if (start) await ensureDaemon();
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), API_TIMEOUT_MS);
try {
const response = await fetch(`${apiRoot()}${pathname}`, {
method,
signal: controller.signal,
headers: body === undefined ? { Accept: 'application/json' } : {
Accept: 'application/json',
'Content-Type': 'application/json',
'X-Studio-Client': 'cli',
},
body: body === undefined ? undefined : JSON.stringify(body),
});
const payload = await response.json().catch(() => ({}));
if (!response.ok) {
throw new StudioError(
payload?.error?.code || 'API_ERROR',
payload?.error?.message || `Local API error ${response.status}.`,
{ details: payload?.error?.details ?? {} },
);
}
return payload;
} catch (error) {
if (error?.name === 'AbortError') {
throw new StudioError('API_TIMEOUT', `The local Studio API did not respond within ${API_TIMEOUT_MS / 1000} seconds.`, { cause: error });
}
throw error;
} finally {
clearTimeout(timeout);
}
}
async function health() {
// Build the URL outside the try: the catch below means "no server is running",
// and swallowing INVALID_PORT there made the CLI start a doomed daemon and
// point at server.log instead of telling the user about the bad variable (#133).
const url = `${apiRoot()}/health`;
const controller = new AbortController();
const timeout = setTimeout(() => controller.abort(), 800);
try {
const response = await fetch(url, {
signal: controller.signal,
headers: { Accept: 'application/json' },
});
if (!response.ok) return false;
const payload = await response.json();
return payload?.ok === true
&& ['AI Edit Studio', 'Game Trailer Studio'].includes(payload.service)
&& payload.schemaVersion >= 2;
} catch {
return false;
} finally {
clearTimeout(timeout);
}
}
let daemonStart = null;
async function ensureDaemon() {
if (!daemonStart) daemonStart = startDaemon().finally(() => { daemonStart = null; });
return daemonStart;
}
async function startDaemon() {
// Validate before any file is created or a server is spawned: a bad port is a
// CLI argument error, not a failed startup (#133).
serverPort();
if (await health()) return;
await ensureDataRoot(0);
const logRoot = safeResolveWithin(DATA_ROOT, 'logs');
fs.mkdirSync(logRoot, { recursive: true });
const logPath = safeResolveWithin(logRoot, 'server.log');
const logFd = fs.openSync(logPath, 'a');
const serverPath = path.join(PROJECT_ROOT, 'server.mjs');
let spawnError = null;
let earlyExit = null;
try {
const child = spawn(process.execPath, [serverPath], {
cwd: PROJECT_ROOT,
detached: true,
windowsHide: true,
shell: false,
stdio: ['ignore', logFd, logFd],
});
child.once('error', (error) => {
spawnError = error;
});
child.once('exit', (code, signal) => {
earlyExit = { code, signal };
});
child.unref();
} catch (error) {
throw new StudioError('SERVER_START_FAILED', `The local Studio server could not be started. Check ${logPath}.`, {
cause: error,
details: { logPath },
});
} finally {
fs.closeSync(logFd);
}
for (let attempt = 0; attempt < 40; attempt += 1) {
await new Promise((resolve) => setTimeout(resolve, 200));
if (spawnError) {
throw new StudioError('SERVER_START_FAILED', `The local Studio server could not be started. Check ${logPath}.`, {
cause: spawnError,
details: { logPath },
});
}
if (await health()) return;
if (earlyExit) {
throw new StudioError('SERVER_START_FAILED', `The local Studio server exited during startup. Check ${logPath}.`, {
details: { logPath, ...earlyExit },
});
}
}
throw new StudioError('SERVER_START_FAILED', `The local Studio server did not start. Check ${logPath}.`);
}
function absolutePathFlag(value) {
const text = String(value ?? '').trim();
return text ? path.resolve(text) : '';
}
// Pass the raw string on: Number('abc') here would turn the bad value into NaN
// and the error would name the wrong thing. The server validates every request (#134).
const numericFlag = value => (value === undefined || value === '' || value === null ? undefined : String(value));
export function requestFromFlags(flags, inputOverride = '') {
return {
project: flags.project || 'Game trailer',
input: absolutePathFlag(inputOverride || flags.input),
duration: numericFlag(flags.duration),
style: flags.style || 'action',
title: flags.title || '',
music: absolutePathFlag(flags.music),
output: absolutePathFlag(flags.output),
resolution: flags.resolution || '2560x1440',
fps: numericFlag(flags.fps),
preview: boolFlag(flags.preview, true),
previewOnly: boolFlag(flags['preview-only'], false),
seed: numericFlag(flags.seed),
};
}
async function waitForJob(jobId) {
while (true) {
const payload = await api(`/jobs/${encodeURIComponent(jobId)}`);
if (TERMINAL_JOB_STATUSES.has(payload.job?.status)) return payload.job;
await new Promise((resolve) => setTimeout(resolve, 750));
}
}
async function createAndWaitForJob(request, jsonMode, extra = {}) {
const payload = await api('/jobs', { method: 'POST', body: request });
const created = payload.job;
print({ id: created.id, status: created.status, ...extra }, jsonMode);
const finished = await waitForJob(created.id);
print(finished, jsonMode);
if (finished.status !== 'complete') process.exitCode = 1;
}
function print(value, jsonMode) {
if (jsonMode) console.log(JSON.stringify(value, null, 2));
else if (typeof value === 'string') console.log(value);
else console.log(JSON.stringify(value, null, 2));
}
function help() {
const studioCommands = agentTools.map(tool => tool.name.replace(/^studio_/, '')).join('|');
console.log(`Agent/editor interface: node cli.mjs studio ${studioCommands} [--project ID] [--revision N] [--request file.json|-] [--wait]\nMCP stdio: node mcp.mjs\nSpeech engine (optional, local): node cli.mjs speech-setup [--model base|small|large-v3-turbo-q5_0]\nInstall for every agent on this computer: node cli.mjs install [--data-root <folder>] [--no-claude] [--no-codex] [--no-desktop] [--no-cursor] [--dry-run] [--json] | node cli.mjs uninstall\nOpenAI Codex only: node cli.mjs codex-setup [--data-root <folder>] [--plugin] [--uninstall] [--dry-run]\nSee AGENTS.md for workflows and examples.`);
console.log(`AI Edit Studio\n\nUsage:\n node cli.mjs doctor [--json]\n node cli.mjs serve\n node cli.mjs mcp (MCP server over stdio, for agents)\n node cli.mjs processes [query] [--json]\n node cli.mjs capture start --process bf6.exe --project BF6 --fps 120\n node cli.mjs capture stop\n node cli.mjs capture status\n node cli.mjs run --input <file|folder> [--output D:\\...\\trailer.mp4]\n node cli.mjs from-last-capture [same trailer options]\n node cli.mjs jobs [--json]\n node cli.mjs job <id> [--json]\n\nTrailer options:\n --duration 60 --style action|cinematic|gameplay|showcase|air-combat\n --title "My trailer" --music D:\\Music\\track.wav\n --resolution 2560x1440 --fps 60 --no-preview --preview-only\n`);
}
async function main() {
const args = process.argv.slice(2);
if (args.length === 1) {
const [arg] = args;
if (arg === '-h') return help();
if (arg === '--version' || arg === '-v' || arg === 'version') {
const { name, version } = JSON.parse(fs.readFileSync(new URL('./package.json', import.meta.url), 'utf8'));
return console.log(name + ' ' + version);
}
}
const { positional, flags } = parseArguments(args);
const [command, subcommand, third] = positional;
const jsonMode = boolFlag(flags.json, false);
if (command === 'studio' && subcommand && flags.help) return console.log(studioHelp(subcommand));
if (!command || command === 'help' || flags.help) return help();
// MCP over stdio for `npx -y ai-edit-studio mcp` (the registry entry uses this). Print nothing before it starts.
if (command === 'mcp') { await import('./mcp.mjs'); return; }
if (command === 'studio') return print(await runStudioCommand(subcommand, flags, api), true);
if (command === 'doctor') {
const info = await inspectFfmpeg({ smokeTest: true });
const storage = await ensureDataRoot(2 * 1024 ** 3);
const { speechStatus } = await import('./src/transcribe.mjs');
return print({ ok: info.capabilities.nvencReady || info.capabilities.libx264, ...info, storage, speech: await speechStatus().catch(e => ({ ready: false, error: e.message })) }, jsonMode);
}
if (command === 'install' || command === 'uninstall') {
const { install, formatReport } = await import('./src/install.mjs');
const clients = Object.fromEntries(['claude', 'codex', 'desktop', 'cursor'].map(id => [id, flags[id] !== false]));
const report = await install({ uninstall: command === 'uninstall', clients, dryRun: Boolean(flags['dry-run']), dataRoot: typeof flags['data-root'] === 'string' ? flags['data-root'] : undefined });
if (!report.ok) process.exitCode = 1;
return jsonMode ? print(report, true) : process.stdout.write(formatReport(report));
}
if (command === 'codex-setup') {
const { codexSetup } = await import('./src/integrations.mjs');
const mode = flags.uninstall ? 'uninstall' : flags.plugin ? 'plugin' : 'full';
return print(await codexSetup({ mode, dataRoot: typeof flags['data-root'] === 'string' ? flags['data-root'] : undefined, dryRun: Boolean(flags['dry-run']) }), true);
}
if (command === 'speech-setup') {
const { setupSpeech, MODEL_SIZES } = await import('./src/transcribe.mjs');
const model = String(flags.model ?? 'base');
console.error(`Installing the local speech engine (whisper.cpp) and model "${model}" (${MODEL_SIZES[model] ?? 'size unknown'}) into the data folder…`);
return print(await setupSpeech({ model, onLog: line => console.error(line) }), true);
}
if (command === 'serve') {
const { installShutdownHandlers, startServer } = await import('./server.mjs');
const server = await startServer({ port: serverPort() });
installShutdownHandlers(server);
return;
}
if (command === 'processes') {
const query = subcommand || flags.query || '';
const payload = await api(`/processes?query=${encodeURIComponent(query)}`);
return print(payload.processes, jsonMode);
}
if (command === 'capture') {
if (subcommand === 'start') {
if (!flags.process) throw new StudioError('PROCESS_REQUIRED', 'Use --process <name.exe|PID>.');
const payload = await api('/capture/start', {
method: 'POST',
body: {
process: flags.process,
project: flags.project || 'Game capture',
fps: Number(flags.fps || 60),
audioDevice: flags['audio-device'] || '',
},
});
return print(payload.capture, jsonMode);
}
if (subcommand === 'stop') {
const payload = await api('/capture/stop', { method: 'POST', body: {} });
return print(payload.capture, jsonMode);
}
if (subcommand === 'status') {
const payload = await api('/capture/status');
return print(payload.capture, jsonMode);
}
throw new StudioError('INVALID_COMMAND', 'Use capture start, capture stop or capture status.');
}
if (command === 'run') {
if (!flags.input) throw new StudioError('INPUT_REQUIRED', 'Use --input <video file|folder>.');
return createAndWaitForJob(requestFromFlags(flags), jsonMode);
}
if (command === 'from-last-capture') {
const payload = await api('/capture/status');
const capture = payload.capture;
if (!capture?.output || capture.active) {
throw new StudioError('CAPTURE_NOT_READY', 'There is no finished recording yet to use as a trailer source.');
}
return createAndWaitForJob(
requestFromFlags(flags, capture.output),
jsonMode,
{ input: path.resolve(capture.output) },
);
}
if (command === 'jobs') {
return print((await api('/jobs')).jobs, jsonMode);
}
if (command === 'job') {
if (!subcommand) throw new StudioError('JOB_REQUIRED', 'Provide the production ID.');
return print((await api(`/jobs/${encodeURIComponent(subcommand)}`)).job, jsonMode);
}
throw new StudioError('INVALID_COMMAND', `Unknown command: ${command}.`);
}
if (isMainModule(import.meta.url)) main().catch((error) => {
const record = {
ok: false,
error: {
code: error?.code || 'CLI_FAILED',
message: error?.message || String(error),
details: error?.details ?? {},
},
};
console.error(JSON.stringify(record, null, 2));
process.exitCode = 1;
});