codexRuntime.ts 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932
  1. import { EventEmitter } from 'node:events';
  2. import { spawn, type ChildProcessWithoutNullStreams } from 'node:child_process';
  3. import { locateCodexBinary, readCodexVersion } from './codexLocator';
  4. import { JsonRpcPeer, type JsonRpcServerRequest } from './jsonRpcPeer';
  5. /**
  6. * 移植自 Noobi.ai src/main/codexAppServer.ts,主要改动:
  7. * 1. 删掉全部账号相关能力(account/read、account/login/start、account/logout)——本模块不做任何登录;
  8. * 2. 不再传 --strict-config(用户与程序共写 config.toml,未知字段只应告警不该退出);
  9. * 3. 新增 provider 注入:用 -c 覆盖 model_provider / model / model_providers.*,api_key 只走子进程环境变量;
  10. * 4. clientInfo 改为本项目标识。
  11. */
  12. export type JsonValue = null | boolean | number | string | JsonValue[] | { [key: string]: JsonValue };
  13. export interface ModelOption {
  14. id: string;
  15. model: string;
  16. displayName: string;
  17. description: string;
  18. isDefault: boolean;
  19. defaultEffort: string;
  20. efforts: string[];
  21. }
  22. export interface RuntimeCapabilities {
  23. namespaceTools: boolean;
  24. imageGeneration: boolean;
  25. webSearch: boolean;
  26. }
  27. export interface RuntimeStatus {
  28. state: 'stopped' | 'starting' | 'ready' | 'error';
  29. binaryPath: string | null;
  30. version: string | null;
  31. codexHome: string | null;
  32. models: ModelOption[];
  33. capabilities: RuntimeCapabilities;
  34. providerId: string | null;
  35. defaultModel: string | null;
  36. /** model/list 拿不到可用模型时为 true */
  37. degraded: boolean;
  38. error: string | null;
  39. }
  40. export interface CodexMcpServerStatus {
  41. name: string;
  42. authStatus: string;
  43. connected: boolean;
  44. toolCount: number;
  45. tools: Array<{ name: string; description: string }>;
  46. }
  47. export interface CodexSkillSummary {
  48. name: string;
  49. description: string;
  50. path: string;
  51. scope: 'user' | 'repo' | 'system' | 'admin' | string;
  52. enabled: boolean;
  53. cwd: string;
  54. }
  55. /** 一次 spawn 用的模型来源:一律是自定义 provider,Codex 的内置 id 是保留字、不可覆盖 */
  56. export interface CodexProviderSpec {
  57. id: string;
  58. name?: string | null;
  59. baseUrl: string;
  60. model: string;
  61. /** apiKey 只注入子进程环境变量,不落任何盘 */
  62. apiKey?: string | null;
  63. envKey?: string;
  64. httpHeaders?: Record<string, string> | null;
  65. envHttpHeaders?: Record<string, string> | null;
  66. /** 仅放行 PROVIDER_EXTRA_ALLOWLIST 里的键,其余在 providerService 就被丢弃 */
  67. extra?: Record<string, JsonValue> | null;
  68. }
  69. export interface CodexRuntimeOptions {
  70. codexHome: string;
  71. provider?: CodexProviderSpec | null;
  72. clientInfo?: { name: string; title: string; version: string };
  73. }
  74. export interface StartThreadOptions {
  75. cwd: string;
  76. model?: string | null;
  77. sandbox?: 'read-only' | 'workspace-write' | 'danger-full-access';
  78. approvalPolicy?: 'untrusted' | 'on-request' | 'never';
  79. developerInstructions?: string;
  80. ephemeral?: boolean;
  81. }
  82. export interface StartTurnOptions {
  83. threadId: string;
  84. prompt: string;
  85. cwd?: string;
  86. model?: string | null;
  87. effort?: string | null;
  88. approvalPolicy?: 'untrusted' | 'on-request' | 'never';
  89. skills?: Array<{ name: string; path: string }>;
  90. /** 仅宿主侧的完成期限,不会发给 App Server */
  91. timeoutMs?: number;
  92. }
  93. export interface TurnResult {
  94. turnId: string;
  95. status: string;
  96. text: string;
  97. raw: unknown;
  98. }
  99. interface SkillsListResponse {
  100. data: Array<{
  101. cwd: string;
  102. skills: Array<{
  103. name: string;
  104. description: string;
  105. path: string;
  106. scope: string;
  107. enabled: boolean;
  108. }>;
  109. }>;
  110. }
  111. interface ListMcpServerStatusResponse {
  112. data: Array<{
  113. name: string;
  114. serverInfo: unknown | null;
  115. tools: Record<string, unknown>;
  116. authStatus: string;
  117. }>;
  118. nextCursor: string | null;
  119. }
  120. interface InitializeResponse {
  121. userAgent: string;
  122. codexHome: string;
  123. platformFamily: string;
  124. platformOs: string;
  125. }
  126. interface ModelListResponse {
  127. data: Array<{
  128. id: string;
  129. model: string;
  130. displayName: string;
  131. description: string;
  132. isDefault: boolean;
  133. defaultReasoningEffort: string;
  134. supportedReasoningEfforts: Array<{ reasoningEffort: string }>;
  135. }>;
  136. nextCursor: string | null;
  137. }
  138. interface ModelProviderCapabilitiesResponse {
  139. namespaceTools: boolean;
  140. imageGeneration: boolean;
  141. webSearch: boolean;
  142. }
  143. interface ThreadResponse {
  144. thread: { id: string };
  145. model: string;
  146. }
  147. interface TurnStartResponse {
  148. turn: { id: string; status: string };
  149. }
  150. interface EarlyTurnState {
  151. text: string;
  152. completed: TurnResult | null;
  153. }
  154. interface TurnWaiter {
  155. text: string;
  156. resolve(result: TurnResult): void;
  157. reject(error: Error): void;
  158. timer: NodeJS.Timeout;
  159. }
  160. const TURN_TIMEOUT_MS = 20 * 60 * 1_000;
  161. /** 单条 RPC 超过这个耗时才值得进诊断(jsonRpcPeer 的预算是 30 秒) */
  162. const SLOW_RPC_MS = 3_000;
  163. /** 退出事件可能比 stderr 落地慢半拍,等一下再拼错误信息 */
  164. const EXIT_GRACE_MS = 300;
  165. const STDERR_TAIL_LINES = 40;
  166. export const DEFAULT_API_KEY_ENV = 'ZSJZ_CODEX_API_KEY';
  167. /**
  168. * 模型管理 config 里允许透传给 Codex 的 provider 字段(对应 rust-v0.155.1
  169. * model-provider-info/src/lib.rs 的 ModelProviderInfo)。providerService 用它做白名单,
  170. * 这里做 -c 落参,两处必须同源,否则页面提示"已丢弃"而实际写进去、或反之。
  171. */
  172. export const PROVIDER_EXTRA_ALLOWLIST: readonly string[] = Object.freeze([
  173. 'request_max_retries',
  174. 'stream_max_retries',
  175. 'stream_idle_timeout_ms',
  176. 'websocket_connect_timeout_ms',
  177. 'supports_websockets',
  178. 'query_params',
  179. ]);
  180. const PROVIDER_EXTRA_ALLOWED = new Set<string>(PROVIDER_EXTRA_ALLOWLIST);
  181. export class CodexRuntime extends EventEmitter {
  182. readonly #codexHome: string;
  183. readonly #clientInfo: { name: string; title: string; version: string };
  184. #provider: CodexProviderSpec | null;
  185. #child: ChildProcessWithoutNullStreams | null = null;
  186. #peer: JsonRpcPeer | null = null;
  187. #startPromise: Promise<RuntimeStatus> | null = null;
  188. #runtime: RuntimeStatus = emptyRuntimeStatus();
  189. #turnWaiters = new Map<string, TurnWaiter>();
  190. #earlyTurnStates = new Map<string, EarlyTurnState>();
  191. #runTurnStartsInFlight = 0;
  192. #generation = 0;
  193. constructor(options: CodexRuntimeOptions) {
  194. super();
  195. this.#codexHome = options.codexHome;
  196. this.#provider = options.provider ?? null;
  197. this.#clientInfo = options.clientInfo ?? {
  198. name: 'zsjz_ai',
  199. title: '清鉴线索调查工具',
  200. version: '0.1.0',
  201. };
  202. }
  203. get status(): RuntimeStatus {
  204. return structuredClone(this.#runtime);
  205. }
  206. get provider(): CodexProviderSpec | null {
  207. return this.#provider ? { ...this.#provider, apiKey: null } : null;
  208. }
  209. /** 换 provider 必须重启子进程:api_key 走的是子进程环境变量,无法热更新 */
  210. async applyProvider(provider: CodexProviderSpec | null): Promise<RuntimeStatus> {
  211. const previous = this.#provider;
  212. this.#provider = provider;
  213. if (this.#child) await this.stop();
  214. try {
  215. return await this.start();
  216. } catch (error) {
  217. // 失败要回滚:坏 spec 留在内存里,之后每次 start 都会重放同一组参数,
  218. // 一条选错的模型能把运行时锁死到重启应用为止。
  219. this.#provider = previous;
  220. throw error;
  221. }
  222. }
  223. async start(): Promise<RuntimeStatus> {
  224. if (this.#runtime.state === 'ready' && this.#peer) return this.status;
  225. if (this.#startPromise) return this.#startPromise;
  226. const generation = ++this.#generation;
  227. this.#startPromise = this.#start(generation);
  228. try {
  229. return await this.#startPromise;
  230. } finally {
  231. this.#startPromise = null;
  232. }
  233. }
  234. async refresh(): Promise<RuntimeStatus> {
  235. await this.start();
  236. let models: ModelOption[] = [];
  237. let degraded = false;
  238. try {
  239. models = await this.listModels();
  240. } catch (error) {
  241. // 第三方/本地端点常常不实现 model/list,这里降级而不是失败
  242. degraded = true;
  243. this.emit('diagnostic', `model/list 不可用:${asError(error).message}`);
  244. }
  245. const capabilities = await this.readModelProviderCapabilities().catch(() => emptyCapabilities());
  246. this.#runtime = {
  247. ...this.#runtime,
  248. models,
  249. capabilities,
  250. degraded: degraded || models.length === 0,
  251. // model/list 返回的是 Codex 内置模型目录(与自定义 provider 无关),
  252. // 实际会用的是 provider 里配置的模型,所以它优先
  253. defaultModel: this.#provider?.model ?? models.find((item) => item.isDefault)?.model ?? null,
  254. state: 'ready',
  255. error: null,
  256. };
  257. this.emit('status', this.status);
  258. return this.status;
  259. }
  260. async readModelProviderCapabilities(): Promise<RuntimeCapabilities> {
  261. const result = await this.#request<ModelProviderCapabilitiesResponse>(
  262. 'modelProvider/capabilities/read',
  263. {},
  264. );
  265. return {
  266. namespaceTools: result.namespaceTools === true,
  267. imageGeneration: result.imageGeneration === true,
  268. webSearch: result.webSearch === true,
  269. };
  270. }
  271. async listModels(): Promise<ModelOption[]> {
  272. const models: ModelOption[] = [];
  273. let cursor: string | null = null;
  274. for (let page = 0; page < 10; page += 1) {
  275. const result: ModelListResponse = await this.#request<ModelListResponse>('model/list', {
  276. cursor,
  277. limit: 100,
  278. includeHidden: false,
  279. });
  280. models.push(
  281. ...result.data.map((model) => ({
  282. id: model.id,
  283. model: model.model,
  284. displayName: model.displayName,
  285. description: model.description,
  286. isDefault: model.isDefault,
  287. defaultEffort: model.defaultReasoningEffort,
  288. efforts: model.supportedReasoningEfforts.map((item) => item.reasoningEffort),
  289. })),
  290. );
  291. cursor = result.nextCursor;
  292. if (!cursor) break;
  293. }
  294. return models;
  295. }
  296. async listSkills(options: { cwds?: string[]; forceReload?: boolean } = {}): Promise<CodexSkillSummary[]> {
  297. await this.start();
  298. const result = await this.#request<SkillsListResponse>('skills/list', {
  299. ...(options.cwds?.length ? { cwds: options.cwds } : {}),
  300. forceReload: options.forceReload ?? false,
  301. });
  302. return result.data.flatMap((entry) =>
  303. entry.skills.map((skill) => ({
  304. name: skill.name,
  305. description: skill.description,
  306. path: skill.path,
  307. scope: skill.scope,
  308. enabled: skill.enabled,
  309. cwd: entry.cwd,
  310. })),
  311. );
  312. }
  313. async setSkillEnabled(selector: { path?: string; name?: string }, enabled: boolean): Promise<boolean> {
  314. await this.start();
  315. const result = await this.#request<{ effectiveEnabled: boolean }>('skills/config/write', {
  316. ...(selector.path ? { path: selector.path } : {}),
  317. ...(!selector.path && selector.name ? { name: selector.name } : {}),
  318. enabled,
  319. });
  320. return result.effectiveEnabled;
  321. }
  322. async listMcpServerStatuses(): Promise<CodexMcpServerStatus[]> {
  323. await this.start();
  324. const statuses: CodexMcpServerStatus[] = [];
  325. let cursor: string | null = null;
  326. for (let page = 0; page < 20; page += 1) {
  327. const result: ListMcpServerStatusResponse = await this.#request<ListMcpServerStatusResponse>(
  328. 'mcpServerStatus/list',
  329. {
  330. cursor,
  331. limit: 100,
  332. detail: 'toolsAndAuthOnly',
  333. },
  334. );
  335. statuses.push(
  336. ...result.data.map((server) => ({
  337. name: server.name,
  338. authStatus: server.authStatus,
  339. connected: server.serverInfo !== null,
  340. toolCount: Object.keys(server.tools ?? {}).length,
  341. tools: toToolSummaries(server.tools),
  342. })),
  343. );
  344. cursor = result.nextCursor;
  345. if (!cursor) break;
  346. }
  347. return statuses;
  348. }
  349. async readConfig(): Promise<Record<string, unknown>> {
  350. await this.start();
  351. const result = await this.#request<{ config: Record<string, unknown> }>('config/read', {
  352. includeLayers: false,
  353. });
  354. return structuredClone(result.config);
  355. }
  356. /** 局部写:Codex 自己也持有 config.toml 写权,禁止整文件覆盖 */
  357. async writeConfigValue(keyPath: string, value: JsonValue): Promise<void> {
  358. await this.start();
  359. await this.#request<unknown>('config/value/write', { keyPath, value, mergeStrategy: 'replace' });
  360. }
  361. async reloadMcpServers(): Promise<void> {
  362. await this.start();
  363. await this.#request<unknown>('config/mcpServer/reload');
  364. }
  365. async startThread(options: StartThreadOptions): Promise<string> {
  366. await this.start();
  367. const result = await this.#request<ThreadResponse>('thread/start', {
  368. cwd: options.cwd,
  369. ...(options.model ? { model: options.model } : {}),
  370. sandbox: options.sandbox ?? 'workspace-write',
  371. approvalPolicy: options.approvalPolicy ?? 'never',
  372. ...(options.developerInstructions ? { developerInstructions: options.developerInstructions } : {}),
  373. ...(options.ephemeral === undefined ? {} : { ephemeral: options.ephemeral }),
  374. experimentalRawEvents: false,
  375. });
  376. return result.thread.id;
  377. }
  378. async resumeThread(threadId: string, options: StartThreadOptions): Promise<string> {
  379. await this.start();
  380. const result = await this.#request<ThreadResponse>('thread/resume', {
  381. threadId,
  382. cwd: options.cwd,
  383. ...(options.model ? { model: options.model } : {}),
  384. sandbox: options.sandbox ?? 'workspace-write',
  385. approvalPolicy: options.approvalPolicy ?? 'never',
  386. ...(options.developerInstructions ? { developerInstructions: options.developerInstructions } : {}),
  387. // excludeTurns 不影响上游收到的历史(实测 true/false 两轮上下文都完整带回)
  388. excludeTurns: true,
  389. });
  390. return result.thread.id;
  391. }
  392. async startTurn(options: StartTurnOptions): Promise<string> {
  393. await this.start();
  394. const result = await this.#request<TurnStartResponse>('turn/start', {
  395. threadId: options.threadId,
  396. input: [
  397. { type: 'text', text: options.prompt, text_elements: [] },
  398. ...(options.skills ?? []).map((skill) => ({
  399. type: 'skill' as const,
  400. name: skill.name,
  401. path: skill.path,
  402. })),
  403. ],
  404. ...(options.cwd ? { cwd: options.cwd } : {}),
  405. ...(options.model ? { model: options.model } : {}),
  406. ...(options.effort ? { effort: options.effort } : {}),
  407. ...(options.approvalPolicy ? { approvalPolicy: options.approvalPolicy } : {}),
  408. });
  409. return result.turn.id;
  410. }
  411. async runTurn(options: StartTurnOptions): Promise<TurnResult> {
  412. this.#runTurnStartsInFlight += 1;
  413. let turnId: string;
  414. try {
  415. turnId = await this.startTurn(options);
  416. } catch (error) {
  417. this.#releaseRunTurnStart();
  418. throw error;
  419. }
  420. // turn/completed 可能早于 turn/start 的响应回来,先存起来
  421. const startedAt = Date.now();
  422. const noteTurn = (result: TurnResult): TurnResult => {
  423. this.emit(
  424. 'diagnostic',
  425. `layer=turn turn=${result.turnId} status=${result.status} 用时 ${((Date.now() - startedAt) / 1000).toFixed(1)}s`,
  426. );
  427. return result;
  428. };
  429. const early = this.#earlyTurnStates.get(turnId);
  430. this.#earlyTurnStates.delete(turnId);
  431. if (early?.completed) {
  432. this.#releaseRunTurnStart();
  433. return noteTurn(early.completed);
  434. }
  435. return new Promise<TurnResult>((resolve, reject) => {
  436. const timeoutMs =
  437. options.timeoutMs === undefined
  438. ? TURN_TIMEOUT_MS
  439. : Math.min(TURN_TIMEOUT_MS, Math.max(1_000, Math.trunc(options.timeoutMs)));
  440. const timer = setTimeout(() => {
  441. this.#turnWaiters.delete(turnId);
  442. void this.interruptTurn(options.threadId, turnId).catch(() => undefined);
  443. this.emit(
  444. 'diagnostic',
  445. `layer=turn turn=${turnId} 宿主侧期限已到(${Math.round(timeoutMs / 1000)} 秒),已发 turn/interrupt`,
  446. );
  447. reject(new Error(`Codex turn ${turnId} 超时`));
  448. }, timeoutMs);
  449. timer.unref();
  450. this.#turnWaiters.set(turnId, {
  451. text: early?.text ?? '',
  452. resolve: (result) => resolve(noteTurn(result)),
  453. reject,
  454. timer,
  455. });
  456. this.#releaseRunTurnStart();
  457. });
  458. }
  459. async interruptTurn(threadId: string, turnId: string): Promise<void> {
  460. await this.#request<Record<string, never>>('turn/interrupt', { threadId, turnId });
  461. }
  462. async unsubscribeThread(threadId: string): Promise<void> {
  463. await this.#request<{ status: string }>('thread/unsubscribe', { threadId });
  464. }
  465. respondToServerRequest(requestId: string | number, result: unknown): void {
  466. if (!this.#peer) throw new Error('Codex App Server 未运行');
  467. this.#peer.respond(requestId, result);
  468. }
  469. rejectServerRequest(requestId: string | number, code: number, message: string): void {
  470. if (!this.#peer) throw new Error('Codex App Server 未运行');
  471. this.#peer.respondError(requestId, { code, message });
  472. }
  473. async stop(): Promise<void> {
  474. this.#generation += 1;
  475. const child = this.#child;
  476. const peer = this.#peer;
  477. this.#child = null;
  478. this.#peer = null;
  479. for (const waiter of this.#turnWaiters.values()) {
  480. clearTimeout(waiter.timer);
  481. waiter.reject(new Error('Codex App Server 在本轮对话进行中被停止'));
  482. }
  483. this.#turnWaiters.clear();
  484. this.#earlyTurnStates.clear();
  485. this.#runTurnStartsInFlight = 0;
  486. if (child && child.exitCode === null) {
  487. peer?.endOutput();
  488. await Promise.race([
  489. new Promise<void>((resolve) => child.once('exit', () => resolve())),
  490. new Promise<void>((resolve) => setTimeout(resolve, 1_500)),
  491. ]);
  492. if (child.exitCode === null) {
  493. child.kill('SIGTERM');
  494. await Promise.race([
  495. new Promise<void>((resolve) => child.once('exit', () => resolve())),
  496. new Promise<void>((resolve) => setTimeout(resolve, 1_000)),
  497. ]);
  498. }
  499. if (child.exitCode === null) child.kill('SIGKILL');
  500. }
  501. peer?.close(new Error('Codex App Server 已停止'));
  502. this.#runtime = { ...emptyRuntimeStatus(), providerId: this.#provider?.id ?? null };
  503. this.emit('status', this.status);
  504. }
  505. async dispose(): Promise<void> {
  506. await this.stop();
  507. this.removeAllListeners();
  508. }
  509. async #start(generation: number): Promise<RuntimeStatus> {
  510. this.#runtime = { ...emptyRuntimeStatus(), state: 'starting', providerId: this.#provider?.id ?? null };
  511. this.emit('status', this.status);
  512. let child: ChildProcessWithoutNullStreams | null = null;
  513. let peer: JsonRpcPeer | null = null;
  514. let watcher: ExitWatcher | null = null;
  515. try {
  516. const binaryPath = await locateCodexBinary();
  517. this.#assertGeneration(generation);
  518. const version = readCodexVersion(binaryPath);
  519. this.#assertGeneration(generation);
  520. const provider = this.#provider;
  521. child = spawn(binaryPath, buildArgs(provider), {
  522. cwd: process.cwd(),
  523. env: buildEnv(this.#codexHome, provider),
  524. stdio: ['pipe', 'pipe', 'pipe'],
  525. windowsHide: true,
  526. });
  527. this.#child = child;
  528. peer = new JsonRpcPeer(child.stdout, child.stdin);
  529. this.#peer = peer;
  530. const spawnedChild = child;
  531. const spawnedPeer = peer;
  532. peer.on('notification', (notification) => this.#handleNotification(notification));
  533. peer.on('serverRequest', (request: JsonRpcServerRequest) => this.emit('serverRequest', request));
  534. peer.on('protocolError', (error) => this.emit('diagnostic', asError(error).message));
  535. watcher = createExitWatcher(
  536. spawnedChild,
  537. (line) => this.emit('diagnostic', sanitizeDiagnostic(line)),
  538. (error) => {
  539. if (this.#peer !== spawnedPeer || this.#child !== spawnedChild) return;
  540. this.#handleExit(spawnedChild, error);
  541. },
  542. );
  543. child.stderr.setEncoding('utf8');
  544. child.stderr.on('data', (chunk: string) => {
  545. for (const line of chunk.split(/\r?\n/u).filter(Boolean)) watcher?.noteStderr(line);
  546. });
  547. child.once('error', (error) => watcher?.shutdown(error.message));
  548. child.once('exit', () => watcher?.shutdown('Codex App Server 已退出'));
  549. peer.once('closed', (error) => watcher?.shutdown(asError(error).message));
  550. peer.start();
  551. const initialized = await peer.request<InitializeResponse>('initialize', {
  552. clientInfo: this.#clientInfo,
  553. capabilities: { experimentalApi: true },
  554. });
  555. this.#assertGeneration(generation);
  556. peer.notify('initialized');
  557. this.#runtime = {
  558. ...emptyRuntimeStatus(),
  559. state: 'ready',
  560. binaryPath,
  561. version,
  562. codexHome: initialized.codexHome,
  563. providerId: provider?.id ?? null,
  564. };
  565. return await this.refresh();
  566. } catch (error) {
  567. // 子进程启动即退出的话真话只在 stderr 里:等它落定,别把「协议流已关闭」丢给用户猜
  568. const fallback = asError(error).message;
  569. const message = watcher ? (await watcher.describe(fallback)).message : fallback;
  570. peer?.close(new Error(message));
  571. if (this.#peer === peer) this.#peer = null;
  572. if (this.#child === child) this.#child = null;
  573. if (child && child.exitCode === null) child.kill('SIGTERM');
  574. if (generation === this.#generation) {
  575. this.#runtime = { ...this.#runtime, state: 'error', error: sanitizeDiagnostic(message) };
  576. this.emit('status', this.status);
  577. }
  578. // 换掉原文而不是换掉错误对象:调用方拿到的类型与 stack 不变,只是话变清楚了
  579. if (error instanceof Error) error.message = message;
  580. throw error;
  581. }
  582. }
  583. #request<T>(method: string, params?: unknown): Promise<T> {
  584. if (!this.#peer) throw new Error('Codex App Server 未运行');
  585. const startedAt = Date.now();
  586. // 只报慢的和失败的:正常一轮有几十条 RPC,全打等于没打
  587. return this.#peer.request<T>(method, params).then(
  588. (value) => {
  589. const ms = Date.now() - startedAt;
  590. if (ms > SLOW_RPC_MS) {
  591. this.emit('diagnostic', `layer=rpc ${method} 用了 ${(ms / 1000).toFixed(1)} 秒`);
  592. }
  593. return value;
  594. },
  595. (error: unknown) => {
  596. const ms = Date.now() - startedAt;
  597. this.emit(
  598. 'diagnostic',
  599. `layer=rpc ${method} 失败(${(ms / 1000).toFixed(1)} 秒):${sanitizeDiagnostic(error instanceof Error ? error.message : String(error))}`,
  600. );
  601. throw error;
  602. },
  603. );
  604. }
  605. #handleNotification(notification: { method: string; params?: unknown }): void {
  606. const params = asRecord(notification.params);
  607. const turnId = readString(params?.turnId) ?? readString(asRecord(params?.turn)?.id);
  608. if (turnId) {
  609. const waiter = this.#turnWaiters.get(turnId);
  610. if (waiter && notification.method === 'item/agentMessage/delta') {
  611. waiter.text += readString(params?.delta) ?? '';
  612. }
  613. if (waiter && notification.method === 'item/completed') {
  614. const item = asRecord(params?.item);
  615. if (item?.type === 'agentMessage' && typeof item.text === 'string') waiter.text = item.text;
  616. }
  617. if (waiter && notification.method === 'turn/completed') {
  618. clearTimeout(waiter.timer);
  619. this.#turnWaiters.delete(turnId);
  620. const turn = asRecord(params?.turn);
  621. waiter.resolve({
  622. turnId,
  623. status: readString(turn?.status) ?? 'completed',
  624. text: waiter.text,
  625. raw: params,
  626. });
  627. }
  628. if (!waiter && this.#runTurnStartsInFlight > 0) {
  629. const early = this.#earlyTurnStates.get(turnId) ?? { text: '', completed: null };
  630. if (notification.method === 'item/agentMessage/delta') {
  631. early.text += readString(params?.delta) ?? '';
  632. }
  633. if (notification.method === 'item/completed') {
  634. const item = asRecord(params?.item);
  635. if (item?.type === 'agentMessage' && typeof item.text === 'string') early.text = item.text;
  636. }
  637. if (notification.method === 'turn/completed') {
  638. const turn = asRecord(params?.turn);
  639. early.completed = {
  640. turnId,
  641. status: readString(turn?.status) ?? 'completed',
  642. text: early.text,
  643. raw: params,
  644. };
  645. }
  646. this.#earlyTurnStates.set(turnId, early);
  647. }
  648. }
  649. this.emit('notification', notification);
  650. }
  651. #handleExit(child: ChildProcessWithoutNullStreams, error: Error): void {
  652. if (this.#child !== child) return;
  653. this.#peer?.close(error);
  654. this.#peer = null;
  655. this.#child = null;
  656. this.#runtime = {
  657. ...this.#runtime,
  658. state: 'error',
  659. degraded: true,
  660. error: sanitizeDiagnostic(error.message),
  661. };
  662. for (const waiter of this.#turnWaiters.values()) {
  663. clearTimeout(waiter.timer);
  664. waiter.reject(error);
  665. }
  666. this.#turnWaiters.clear();
  667. this.#earlyTurnStates.clear();
  668. this.#runTurnStartsInFlight = 0;
  669. this.emit('status', this.status);
  670. }
  671. #assertGeneration(generation: number): void {
  672. if (generation !== this.#generation) throw new Error('Codex App Server 的启动已被取消');
  673. }
  674. #releaseRunTurnStart(): void {
  675. this.#runTurnStartsInFlight = Math.max(0, this.#runTurnStartsInFlight - 1);
  676. if (this.#runTurnStartsInFlight === 0) this.#earlyTurnStates.clear();
  677. }
  678. }
  679. /** 每个配置项单独一个 -c,避免手写嵌套 inline table */
  680. export function buildArgs(provider: CodexProviderSpec | null): string[] {
  681. const args = ['app-server', '--listen', 'stdio://'];
  682. if (!provider) return args;
  683. args.push('-c', `model_provider=${tomlString(provider.id)}`);
  684. args.push('-c', `model=${tomlString(provider.model)}`);
  685. const prefix = `model_providers.${provider.id}`;
  686. if (provider.name) args.push('-c', `${prefix}.name=${tomlString(provider.name)}`);
  687. if (provider.baseUrl) args.push('-c', `${prefix}.base_url=${tomlString(provider.baseUrl)}`);
  688. // wire_api 只支持 responses:0.155.1 已下线 chat
  689. args.push('-c', `${prefix}.wire_api="responses"`);
  690. args.push('-c', `${prefix}.requires_openai_auth=false`);
  691. if (provider.apiKey) {
  692. args.push('-c', `${prefix}.env_key=${tomlString(provider.envKey ?? DEFAULT_API_KEY_ENV)}`);
  693. }
  694. if (provider.httpHeaders && Object.keys(provider.httpHeaders).length) {
  695. args.push('-c', `${prefix}.http_headers=${tomlInlineTable(provider.httpHeaders)}`);
  696. }
  697. if (provider.envHttpHeaders && Object.keys(provider.envHttpHeaders).length) {
  698. args.push('-c', `${prefix}.env_http_headers=${tomlInlineTable(provider.envHttpHeaders)}`);
  699. }
  700. for (const [key, value] of Object.entries(provider.extra ?? {})) {
  701. if (!PROVIDER_EXTRA_ALLOWED.has(key) || value === null || value === undefined) continue;
  702. args.push('-c', `${prefix}.${key}=${tomlValue(value)}`);
  703. }
  704. return args;
  705. }
  706. function buildEnv(codexHome: string, provider: CodexProviderSpec | null): NodeJS.ProcessEnv {
  707. const env: NodeJS.ProcessEnv = {
  708. ...process.env,
  709. CODEX_HOME: codexHome,
  710. RUST_LOG: process.env.RUST_LOG ?? 'warn',
  711. LOG_FORMAT: 'json',
  712. };
  713. // 密钥只存在于子进程环境变量,config.toml 里只写 env_key 名字
  714. if (provider?.apiKey) {
  715. env[provider.envKey ?? DEFAULT_API_KEY_ENV] = provider.apiKey;
  716. }
  717. return env;
  718. }
  719. function tomlString(value: string): string {
  720. return JSON.stringify(value);
  721. }
  722. function tomlInlineTable(value: Record<string, string>): string {
  723. const entries = Object.entries(value).map(([key, item]) => `${tomlKey(key)}=${tomlString(item)}`);
  724. return `{${entries.join(',')}}`;
  725. }
  726. function tomlKey(key: string): string {
  727. return /^[A-Za-z0-9_-]+$/.test(key) ? key : tomlString(key);
  728. }
  729. function tomlValue(value: JsonValue): string {
  730. if (typeof value === 'string') return tomlString(value);
  731. if (typeof value === 'number' || typeof value === 'boolean') return String(value);
  732. if (Array.isArray(value)) {
  733. return `[${value.filter((item) => item !== null).map(tomlValue).join(',')}]`;
  734. }
  735. if (value && typeof value === 'object') {
  736. // TOML inline table 用 `=` 而不是 JSON 的 `:`,不能直接 JSON.stringify
  737. return tomlInlineTable(
  738. Object.fromEntries(
  739. Object.entries(value as Record<string, JsonValue>)
  740. .filter(([, item]) => typeof item === 'string')
  741. .map(([key, item]) => [key, String(item)]),
  742. ),
  743. );
  744. }
  745. return '""';
  746. }
  747. function emptyRuntimeStatus(): RuntimeStatus {
  748. return {
  749. state: 'stopped',
  750. binaryPath: null,
  751. version: null,
  752. codexHome: null,
  753. models: [],
  754. capabilities: emptyCapabilities(),
  755. providerId: null,
  756. defaultModel: null,
  757. degraded: false,
  758. error: null,
  759. };
  760. }
  761. function emptyCapabilities(): RuntimeCapabilities {
  762. return { namespaceTools: false, imageGeneration: false, webSearch: false };
  763. }
  764. function asRecord(value: unknown): Record<string, unknown> | null {
  765. return value && typeof value === 'object' && !Array.isArray(value)
  766. ? (value as Record<string, unknown>)
  767. : null;
  768. }
  769. function readString(value: unknown): string | null {
  770. return typeof value === 'string' ? value : null;
  771. }
  772. /**
  773. * mcpServerStatus/list 的 tools 是「工具名 → 详情」映射,详情结构由 Codex 版本决定。
  774. * 这里只认 name / description,认不出的形态退化成只留 key 名,避免协议一变列表就空掉。
  775. */
  776. export function toToolSummaries(
  777. tools: Record<string, unknown> | null | undefined,
  778. ): Array<{ name: string; description: string }> {
  779. return Object.entries(tools ?? {}).map(([key, value]) => {
  780. const detail = asRecord(value);
  781. return {
  782. name: readString(detail?.name) || key,
  783. description: readString(detail?.description) || '',
  784. };
  785. });
  786. }
  787. function asError(value: unknown): Error {
  788. return value instanceof Error ? value : new Error(String(value));
  789. }
  790. export function sanitizeDiagnostic(value: string): string {
  791. return value
  792. .replace(/sk-[A-Za-z0-9_-]{12,}/gu, 'sk-[redacted]')
  793. .replace(/Bearer\s+[A-Za-z0-9._~-]+/giu, 'Bearer [redacted]')
  794. .slice(0, 4_000);
  795. }
  796. function delay(ms: number): Promise<void> {
  797. return new Promise((resolve) => {
  798. const timer = setTimeout(resolve, ms);
  799. timer.unref();
  800. });
  801. }
  802. /**
  803. * Codex 的致命错误由 anyhow 打成纯文本(`Error: ...`),RUST_LOG 的常规日志是 JSON 行;
  804. * 纯文本里还混着 `WARNING: ...` 这类噪音,所以优先取 Error 行,取不到才退化成第一条纯文本行。
  805. */
  806. function pickFatalLine(tail: string[]): string | null {
  807. const plain = tail
  808. .map((line) => line.trim())
  809. .filter((line) => line && !line.startsWith('{'));
  810. if (!plain.length) return null;
  811. const fatal = plain.find((line) => /^(error|panic)\b/iu.test(line) || line.includes('panicked at'));
  812. return sanitizeDiagnostic((fatal ?? plain[0]).slice(0, 500));
  813. }
  814. interface ExitWatcher {
  815. /** 收 stderr:既实时推诊断,也留尾部若干行用于拼退出原因 */
  816. noteStderr(line: string): void;
  817. /** 统一退出出口(流关闭 / 进程退出 / spawn 失败),只生效一次 */
  818. shutdown(fallback: string): void;
  819. /** 等 stderr 收完,把没有信息量的 fallback 换成带 exit code 与原因原文的错误 */
  820. describe(fallback: string): Promise<Error>;
  821. }
  822. function createExitWatcher(
  823. child: ChildProcessWithoutNullStreams,
  824. onDiagnostic: (line: string) => void,
  825. onExit: (error: Error) => void,
  826. ): ExitWatcher {
  827. const tail: string[] = [];
  828. const closed = new Promise<void>((resolve) => child.once('close', () => resolve()));
  829. let shuttingDown = false;
  830. const describe = async (fallback: string): Promise<Error> => {
  831. // 等不到 close 也得往下走:一次卡住的退出不能把启动的 Promise 挂在那里
  832. await Promise.race([closed, delay(EXIT_GRACE_MS)]);
  833. const code = child.exitCode ?? child.signalCode;
  834. const reason = code === null || code === undefined ? fallback : `Codex App Server 退出(${code})`;
  835. const detail = pickFatalLine(tail);
  836. return new Error(detail ? `${reason}:${detail}` : reason);
  837. };
  838. return {
  839. noteStderr(line: string): void {
  840. tail.push(line);
  841. if (tail.length > STDERR_TAIL_LINES) tail.shift();
  842. onDiagnostic(line);
  843. },
  844. describe,
  845. shutdown(fallback: string): void {
  846. if (shuttingDown) return;
  847. shuttingDown = true;
  848. void describe(fallback).then((error) => {
  849. // 流都关了进程还活着(卡死的子进程)只能自己收
  850. if (child.exitCode === null && child.signalCode === null) child.kill('SIGTERM');
  851. onExit(error);
  852. });
  853. },
  854. };
  855. }