generate-child.ts
3.79 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
/**
* Child process entry for a single generation job. Runs in a detached process
* so a failing/hanging run cannot crash the server. It receives the server's
* model + figma credentials via `COD_RUNTIME_CONFIG` (never via argv, never
* via the frontend) and injects them into the coderio engine at runtime.
*/
import { design2code, setRuntimeConfig, patchEvoltagentStreamingForceOff, ValidationMode, logger } from 'coderio';
import type { ProjectProfile } from 'coderio';
function mapMode(mode: string): ValidationMode {
if (mode === 'code') return ValidationMode.CodeOnly;
if (mode === 'with-report') return ValidationMode.ReportOnly;
return ValidationMode.Full;
}
async function main(): Promise<void> {
const argv = process.argv.slice(2);
let projectProfileArg: ProjectProfile | null | undefined = undefined;
const get = (name: string): string | undefined => {
const i = argv.indexOf(`--${name}`);
return i >= 0 ? argv[i + 1] : undefined;
};
const figmaUrl = get('figmaUrl');
const mode = mapMode(get('mode') ?? 'full');
const outputDir = get('outputDir');
const appName = get('appName');
if (!figmaUrl) {
process.stdout.write('__D2C_ERROR__:missing figmaUrl\n');
process.exit(1);
}
const raw = process.env.COD_RUNTIME_CONFIG;
let targetArg: unknown = undefined;
let fillContainerArg: boolean | undefined = undefined;
if (raw) {
try {
const cfg = JSON.parse(raw) as {
model?: Record<string, unknown>;
figma?: Record<string, unknown>;
projectProfile?: unknown;
target?: unknown;
fillContainer?: unknown;
};
setRuntimeConfig({ model: cfg.model, figma: cfg.figma });
// projectProfile is passed to design2code below (typed via the
// engine's ProjectProfile; we forward it as-is and let the engine
// validate/normalize).
if (cfg.projectProfile && typeof cfg.projectProfile === 'object') {
projectProfileArg = cfg.projectProfile as ProjectProfile;
}
if (cfg.target && typeof cfg.target === 'object') {
targetArg = cfg.target;
}
if (typeof cfg.fillContainer === 'boolean') {
fillContainerArg = cfg.fillContainer;
}
// 打印本次生成所采用的模型(需求:运行日志更详细,明确使用什么模型)
const _m = cfg.model && (cfg.model as Record<string, unknown>).model;
const _b = ((cfg.model && (cfg.model as Record<string, unknown>).baseUrl) || '') as string;
const _local = /11434|localhost|127\.0\.0\.1/.test(_b);
logger.printInfoLog(`本次生成采用模型:${_m ?? '默认模型'}${_local ? '(本地 Ollama)' : '(外部 API)'}`);
} catch (e) {
process.stdout.write(`__D2C_ERROR__:invalid runtime config: ${e instanceof Error ? e.message : String(e)}\n`);
process.exit(1);
}
}
// Force non-streaming agent calls (see patch module) — avoids the hang
// against OpenAI-compatible endpoints that stream reasoning_content.
patchEvoltagentStreamingForceOff();
try {
await design2code(figmaUrl, mode, {
outputDir: outputDir || undefined,
appName: appName || undefined,
resume: false,
projectProfile: projectProfileArg,
target: targetArg as never,
fillContainer: fillContainerArg,
});
process.stdout.write('__D2C_DONE__\n');
process.exit(0);
} catch (err) {
const message = err instanceof Error ? err.message : String(err);
process.stdout.write(`__D2C_ERROR__:${message}\n`);
process.exit(1);
}
}
void main();