Formerly KrillinAI, VideoTranslation and Dubbing Tool for 120+ Languages. Open-source AI workspace for creators, powered by Codex. Video downloader, video translation, create videos, images, audios, and edits with Agents in one place. For your Social Media like YouTube, TikTok, Instagram, etc.
TypeScript agent-skillscodex-clideskop-appdubbingimage-generationlocalizationskillsttsvideo-editingvideo-generationvideo-transcriptionvideo-translation
import { mkdirSync, readFileSync, statSync } from 'node:fs';
import {
rename,
rm,
writeFile
} from 'node:fs/promises';
import { join, resolve } from 'node:path';
import { readOpenCreatorConfig } from '@opencreator/config';
import type { CodexAvailabilityProbe } from '@opencreator/protocol';
import type { FastifyInstance } from 'fastify';
import {
applyCapabilityMatrix,
collectCodexCapabilityMatrixAsync,
collectStartupCapabilityMatrixAsync,
probeCodexVersionAsync,
resolveBundledCodexStartupSnapshot,
withRuntimeSkillCapabilities,
type RuntimeCapabilityMatrix
} from './codex/capabilities.js';
import { resolveCodexHome } from './codex/home.js';
import { synchronizeLocalCodexConfiguration } from './codex/probe-home.js';
import { setTimeout as delay } from 'node:timers/promises';
import type { prepareOfficialRuntimeProbe } from './gateway/runtime-probe.js';
import {
CODEX_PROBE_TIMEOUT_MS,
probeCodex
} from './codex/probe.js';
import { createRuntimeToken } from './security/token.js';
import {
installGracefulShutdown,
installParentPortShutdown
} from './shutdown.js';
import {
createProductionServerInput,
parseRuntimeChannel,
requiresPaidStartupProbe,
resolveProductionRuntimePaths,
resolveProductionServerEnvironment
} from './startup.js';
import {
acquireRuntimeLock,
RuntimeDataInUseError
} from './runtime-lock.js';
import {
installManagedParentWatch,
parseManagedParentPid
} from './managed-parent.js';
type BootstrapPhase = 'starting_runtime';
let server: FastifyInstance | undefined;
let closeWork: Promise<void> | undefined;
let releaseRuntimeLock: (() => void) | undefined;
let stopManagedParentWatch: (() => void) | undefined;
let cancelCapabilityRefresh: (() => void) | undefined;
let cancelAvailabilityProbe: (() => void) | undefined;
let availabilityProbeWork: Promise<void> | undefined;
await main().catch(error => {
emitBootstrapError({
code: error instanceof RuntimeDataInUseError
? error.code
: 'DAEMON_START_FAILED',
message: error instanceof Error ? error.message : String(error),
...(error instanceof RuntimeDataInUseError
? { details: error.details }
: {})
});
process.exitCode = 1;
});
async function main(): Promise<void> {
const token = createRuntimeToken();
const environment = resolveProductionServerEnvironment();
const paths = resolveProductionRuntimePaths(environment, {
runtimeChannel: parseRuntimeChannel(
process.env.OPENCREATOR_RUNTIME_CHANNEL
)
});
const {
appHome,
dataDir,
configFile,
credentialsFile,
runtimeDir,
creatorDir
} = paths;
const codexBin = environment.codexBin ?? 'codex';
const codexHome = resolveCodexHome({ isolatedHome: paths.codexHome }).path;
const localCodexHome = environment.localCodexHome === undefined
? environment.codexHome === undefined ? resolveCodexHome().path : codexHome
: resolveCodexHome({ isolatedHome: environment.localCodexHome }).path;
readOpenCreatorConfig(configFile);
mkdirSync(dataDir, { recursive: true });
mkdirSync(codexHome, { recursive: true });
releaseRuntimeLock = acquireRuntimeLock(dataDir);
process.once('exit', () => releaseRuntimeLock?.());
try {
synchronizeLocalCodexConfiguration(localCodexHome, codexHome);
} catch {
// Keep the app available so the user can inspect and fix their local setup.
console.warn('Local Codex configuration could not be synchronized. The assistant will check it again before use.');
}
stopManagedParentWatch = installManagedParentWatch({
parentPid: parseManagedParentPid(
process.env.OPENCREATOR_MANAGED_PARENT_PID
),
close: closeServer,
exit: code => process.exit(code),
onError(error) {
console.error(
`Failed to close daemon after its parent exited: ${
error instanceof Error ? error.message : String(error)
}`
);
}
});
const bundledStartup = resolveBundledCodexStartupSnapshot({
mode: process.env.OPENCREATOR_CODEX_RUNTIME_MODE,
version: process.env.OPENCREATOR_CODEX_VERSION,
commit: process.env.OPENCREATOR_CODEX_COMMIT
});
const versionProbe = bundledStartup === undefined
? await probeCodexVersionAsync({
codexBin,
timeoutMs: 3_000
})
: {
ready: true,
version: bundledStartup.version
};
if (!versionProbe.ready) {
emitBootstrapError({
code: 'CODEX_VERSION_CHECK_FAILED',
message: 'Codex CLI could not start. Reinstall it or select another Codex executable.',
details: {
codexBin,
version: versionProbe.version,
warning: versionProbe.warning
}
});
process.exitCode = 1;
return;
}
const shouldRunAvailabilityProbe = requiresPaidStartupProbe();
let availabilityProbe: CodexAvailabilityProbe = shouldRunAvailabilityProbe
? { status: 'pending' }
: {
status: 'skipped',
checkedAt: new Date().toISOString()
};
emitBootstrap('starting_runtime');
const capabilityResolution = bundledStartup === undefined
? await resolveCapabilities(
codexBin,
dataDir,
versionProbe.version
)
: {
state: bundledStartup.capabilities,
startBackgroundRefresh() {}
};
const capabilities = capabilityResolution.state;
let prepareAvailabilityProbe: (() => ReturnType<typeof prepareOfficialRuntimeProbe>) | undefined;
const { buildServer } = await import('./api/server.js');
server = await buildServer(createProductionServerInput({
token,
capabilities,
getCodexAvailabilityProbe: () => availabilityProbe,
onPrepareAvailabilityProbe(prepare) { prepareAvailabilityProbe = prepare; },
persistentAppServerEnabled:
process.env.OPENCREATOR_PERSISTENT_APP_SERVER !== '0',
...(process.env.OPENCREATOR_E2E_FAKE_CODEX_CONFIG !== undefined
&& process.env.OPENCREATOR_E2E_CREATOR_RUNTIME_PLATFORM !== undefined
&& process.env.OPENCREATOR_E2E_CREATOR_RUNTIME_ARCH !== undefined
? {
creatorRuntimePlatform:
process.env.OPENCREATOR_E2E_CREATOR_RUNTIME_PLATFORM as NodeJS.Platform,
creatorRuntimeArch: process.env.OPENCREATOR_E2E_CREATOR_RUNTIME_ARCH
}
: {}),
...environment,
appHome,
dataDir,
configFile,
credentialsFile,
runtimeDir,
creatorDir,
codexHome,
localCodexHome
}));
const address = await server.listen({ host: '127.0.0.1', port: 0 });
capabilityResolution.startBackgroundRefresh();
installGracefulShutdown({
close: closeServer,
onError(error) {
console.error(`Failed to close daemon cleanly: ${error instanceof Error ? error.message : String(error)}`);
process.exitCode = 1;
}
});
installParentPortShutdown({
close: closeServer,
exit: code => process.exit(code),
onError(error) {
console.error(`Failed to close daemon after parent request: ${String(error)}`);
}
});
emitParentMessage({
type: 'opencreator_daemon_ready',
address,
token
});
console.log(JSON.stringify({ address, token }));
if (shouldRunAvailabilityProbe) {
const controller = new AbortController();
cancelAvailabilityProbe = () => controller.abort();
availabilityProbeWork = runAvailabilityProbe({
codexBin,
dataDir,
prepare: () => prepareAvailabilityProbe!(),
signal: controller.signal,
update(next) {
availabilityProbe = next;
}
}).catch(() => {
if (!controller.signal.aborted) availabilityProbe = {
status: 'failed', checkedAt: new Date().toISOString(),
errorCode: 'OFFICIAL_PROBE_UNAVAILABLE', message: 'Official model availability probe is unavailable'
};
});
}
}
async function closeServer(): Promise<void> {
stopManagedParentWatch?.();
stopManagedParentWatch = undefined;
cancelCapabilityRefresh?.();
cancelAvailabilityProbe?.();
await availabilityProbeWork;
closeWork ??= server?.close() ?? Promise.resolve();
await closeWork;
releaseRuntimeLock?.();
}
async function resolveCapabilities(
codexBin: string,
dataDir: string,
codexVersion: string
): Promise<{
state: RuntimeCapabilityMatrix;
startBackgroundRefresh(): void;
}> {
const cachePath = join(dataDir, 'codex-capabilities.json');
const fingerprint = codexFingerprint(codexBin);
const cached = readCapabilityCache(cachePath, fingerprint, codexVersion);
if (cached !== undefined) {
return {
state: cached,
startBackgroundRefresh() {}
};
}
const state = await collectStartupCapabilityMatrixAsync({
codexBin,
timeoutMs: 500
});
let started = false;
return {
state,
startBackgroundRefresh() {
if (started) return;
started = true;
const controller = new AbortController();
cancelCapabilityRefresh = () => controller.abort();
void collectCodexCapabilityMatrixAsync({
codexBin,
timeoutMs: 5_000,
signal: controller.signal
}).then(async matrix => {
if (controller.signal.aborted) return;
applyCapabilityMatrix(state, matrix);
withRuntimeSkillCapabilities(state);
await writeCapabilityCache(cachePath, fingerprint, matrix);
}).catch(error => {
if (!controller.signal.aborted) {
console.warn(`Codex capability refresh failed: ${
error instanceof Error ? error.message : String(error)
}`);
}
});
}
};
}
async function writeCapabilityCache(
path: string,
fingerprint: string,
matrix: RuntimeCapabilityMatrix
): Promise<void> {
const temporary = `${path}.${process.pid}.tmp`;
try {
await writeFile(
temporary,
`${JSON.stringify({ fingerprint, matrix }, null, 2)}\n`,
{ mode: 0o600 }
);
await rename(temporary, path);
} catch {
await rm(temporary, { force: true }).catch(() => undefined);
}
}
function codexFingerprint(codexBin: string): string {
try {
const stat = statSync(codexBin);
return `${resolve(codexBin)}:${stat.size}:${stat.mtimeMs}`;
} catch {
return codexBin;
}
}
function readCapabilityCache(
path: string,
fingerprint: string,
codexVersion: string
): RuntimeCapabilityMatrix | undefined {
try {
const parsed = JSON.parse(readFileSync(path, 'utf8')) as unknown;
if (
!isRecord(parsed)
|| parsed.fingerprint !== fingerprint
|| !isRecord(parsed.matrix)
|| parsed.matrix.codexVersion !== codexVersion
) {
return undefined;
}
return parsed.matrix as RuntimeCapabilityMatrix;
} catch {
return undefined;
}
}
function emitBootstrap(
phase: BootstrapPhase,
extra: Record<string, unknown> = {}
): void {
const event = {
type: 'opencreator_daemon_bootstrap',
phase,
at: new Date().toISOString(),
...extra
};
emitParentMessage(event);
console.log(JSON.stringify(event));
}
function emitBootstrapError(input: {
code: string;
message: string;
durationMs?: number;
details?: Record<string, unknown>;
}): void {
const event = {
type: 'opencreator_daemon_bootstrap_error',
at: new Date().toISOString(),
...input
};
emitParentMessage(event);
console.log(JSON.stringify(event));
}
function emitParentMessage(message: Record<string, unknown>): void {
const parentPort = (
process as NodeJS.Process & {
parentPort?: {
postMessage(message: unknown): void;
};
}
).parentPort;
try {
parentPort?.postMessage(message);
} catch {
// stdout remains the compatibility transport for standalone Daemon use.
}
}
function probeErrorCode(
reason: string,
exitCode: number | null,
explicitCode?: string
): string {
if (explicitCode !== undefined) return explicitCode;
if (reason === 'spawn_failed') return 'CODEX_PROBE_SPAWN_FAILED';
if (reason === 'spawn_timeout') return 'CODEX_PROBE_SPAWN_TIMEOUT';
if (reason === 'timeout' || reason === 'inactivity_timeout') return 'CODEX_PROBE_TIMEOUT';
if (exitCode !== 0) return 'CODEX_PROBE_EXIT_NON_ZERO';
return 'CODEX_PROBE_NO_RESPONSE';
}
function probeErrorMessage(input: {
terminationReason: string;
exitCode: number | null;
stderrSummary?: string;
}): string {
if (input.stderrSummary !== undefined) return input.stderrSummary;
if (input.exitCode !== null && input.exitCode !== 0) {
return `Codex CLI exited with code ${input.exitCode}`;
}
return `Codex CLI returned no valid response (${input.terminationReason})`;
}
async function runAvailabilityProbe(input: {
codexBin: string;
dataDir: string;
prepare(): ReturnType<typeof prepareOfficialRuntimeProbe>;
signal: AbortSignal;
update(probe: CodexAvailabilityProbe): void;
}): Promise<void> {
let prepared: Awaited<ReturnType<typeof input.prepare>>;
while (!input.signal.aborted) {
try { prepared = await input.prepare(); } catch { /* Retry when platform credentials become available. */ }
if (prepared) break;
await delay(30_000, undefined, { signal: input.signal });
}
if (!prepared) return;
if (input.signal.aborted) { await prepared.cleanup(); return; }
const result = await (async () => {
try {
return await probeCodex({
codexBin: input.codexBin, codexHome: prepared.codexHome,
baseEnvironment: prepared.env, cwd: join(input.dataDir, 'probe'),
timeoutMs: CODEX_PROBE_TIMEOUT_MS, signal: input.signal
});
} finally { await prepared.cleanup(); }
})();
if (input.signal.aborted) return;
const checkedAt = new Date().toISOString();
if (result.ready) {
input.update({
status: 'succeeded',
checkedAt,
durationMs: result.durationMs,
responseReceived: result.responseReceived,
markerMatched: result.markerMatched
});
return;
}
const errorCode = probeErrorCode(
result.terminationReason,
result.exitCode,
result.errorCode
);
const message = probeErrorMessage(result);
input.update({
status: 'failed',
checkedAt,
durationMs: result.durationMs,
responseReceived: result.responseReceived,
markerMatched: result.markerMatched,
errorCode,
message
});
console.warn(`Codex background availability check failed [${errorCode}]: ${message}`);
}
function isRecord(value: unknown): value is Record<string, unknown> {
return typeof value === 'object' && value !== null && !Array.isArray(value);
}