File size: 41,560 Bytes
1dbc34b | 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 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047 1048 1049 1050 1051 1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 | /**
* Codex Provider - Executes queries using Codex CLI
*
* Spawns the Codex CLI and converts JSONL output into ProviderMessage format.
*/
import path from 'path';
import { BaseProvider } from './base-provider.js';
import {
spawnJSONLProcess,
spawnProcess,
findCodexCliPath,
getCodexAuthIndicators,
secureFs,
getDataDirectory,
getCodexConfigDir,
} from '@automaker/platform';
import { checkCodexAuthentication } from '../lib/codex-auth.js';
import {
formatHistoryAsText,
extractTextFromContent,
classifyError,
getUserFriendlyErrorMessage,
createLogger,
} from '@automaker/utils';
import type {
ExecuteOptions,
ProviderMessage,
InstallationStatus,
ModelDefinition,
} from './types.js';
import {
supportsReasoningEffort,
validateBareModelId,
calculateReasoningTimeout,
type CodexApprovalPolicy,
type CodexSandboxMode,
type CodexAuthStatus,
} from '@automaker/types';
import { CodexConfigManager } from './codex-config-manager.js';
import { executeCodexSdkQuery } from './codex-sdk-client.js';
import {
resolveCodexToolCall,
extractCodexTodoItems,
getCodexTodoToolName,
} from './codex-tool-mapping.js';
import { SettingsService } from '../services/settings-service.js';
import { createTempEnvOverride } from '../lib/auth-utils.js';
import { checkSandboxCompatibility } from '../lib/sdk-options.js';
import { CODEX_MODELS } from './codex-models.js';
const CODEX_COMMAND = 'codex';
const CODEX_EXEC_SUBCOMMAND = 'exec';
const CODEX_RESUME_SUBCOMMAND = 'resume';
const CODEX_JSON_FLAG = '--json';
const CODEX_MODEL_FLAG = '--model';
const CODEX_VERSION_FLAG = '--version';
const CODEX_CONFIG_FLAG = '--config';
const CODEX_ADD_DIR_FLAG = '--add-dir';
const CODEX_OUTPUT_SCHEMA_FLAG = '--output-schema';
const CODEX_SKIP_GIT_REPO_CHECK_FLAG = '--skip-git-repo-check';
const CODEX_REASONING_EFFORT_KEY = 'reasoning_effort';
const CODEX_YOLO_FLAG = '--dangerously-bypass-approvals-and-sandbox';
const OPENAI_API_KEY_ENV = 'OPENAI_API_KEY';
const CODEX_EXECUTION_MODE_CLI = 'cli';
const CODEX_EXECUTION_MODE_SDK = 'sdk';
const ERROR_CODEX_CLI_REQUIRED =
'Codex CLI is required for tool-enabled requests. Please install Codex CLI and run `codex login`.';
const ERROR_CODEX_AUTH_REQUIRED = "Codex authentication is required. Please run 'codex login'.";
const ERROR_CODEX_SDK_AUTH_REQUIRED = 'OpenAI API key required for Codex SDK execution.';
const CODEX_EVENT_TYPES = {
itemCompleted: 'item.completed',
itemStarted: 'item.started',
itemUpdated: 'item.updated',
turnCompleted: 'turn.completed',
error: 'error',
} as const;
const CODEX_ITEM_TYPES = {
reasoning: 'reasoning',
agentMessage: 'agent_message',
commandExecution: 'command_execution',
todoList: 'todo_list',
} as const;
const SYSTEM_PROMPT_LABEL = 'System instructions';
const HISTORY_HEADER = 'Current request:\n';
const TEXT_ENCODING = 'utf-8';
/**
* Default timeout for Codex CLI operations in milliseconds.
* This is the "no output" timeout - if the CLI doesn't produce any JSONL output
* for this duration, the process is killed. For reasoning models with high
* reasoning effort, this timeout is dynamically extended via calculateReasoningTimeout().
*
* For feature generation (which can generate 50+ features), we use a much longer
* base timeout (5 minutes) since Codex models are slower at generating large JSON responses.
*
* @see calculateReasoningTimeout from @automaker/types
*/
const CODEX_CLI_TIMEOUT_MS = 120000; // 2 minutes — matches CLI provider base timeout
const CODEX_FEATURE_GENERATION_BASE_TIMEOUT_MS = 300000; // 5 minutes for feature generation
const SYSTEM_PROMPT_SEPARATOR = '\n\n';
const CODEX_INSTRUCTIONS_DIR = '.codex';
const CODEX_INSTRUCTIONS_SECTION = 'Codex Project Instructions';
const CODEX_INSTRUCTIONS_PATH_LABEL = 'Path';
const CODEX_INSTRUCTIONS_SOURCE_LABEL = 'Source';
const CODEX_INSTRUCTIONS_USER_SOURCE = 'User instructions';
const CODEX_INSTRUCTIONS_PROJECT_SOURCE = 'Project instructions';
const CODEX_USER_INSTRUCTIONS_FILE = 'AGENTS.md';
const CODEX_PROJECT_INSTRUCTIONS_FILES = ['AGENTS.md'] as const;
const CODEX_SETTINGS_DIR_FALLBACK = './data';
const DEFAULT_CODEX_AUTO_LOAD_AGENTS = false;
const DEFAULT_CODEX_SANDBOX_MODE: CodexSandboxMode = 'workspace-write';
const DEFAULT_CODEX_APPROVAL_POLICY: CodexApprovalPolicy = 'on-request';
const TOOL_USE_ID_PREFIX = 'codex-tool-';
const ITEM_ID_KEYS = ['id', 'item_id', 'call_id', 'tool_use_id', 'command_id'] as const;
const EVENT_ID_KEYS = ['id', 'event_id', 'request_id'] as const;
const COMMAND_OUTPUT_FIELDS = ['output', 'stdout', 'stderr', 'result'] as const;
const COMMAND_OUTPUT_SEPARATOR = '\n';
const OUTPUT_SCHEMA_FILENAME = 'output-schema.json';
const OUTPUT_SCHEMA_INDENT_SPACES = 2;
const IMAGE_TEMP_DIR = '.codex-images';
const IMAGE_FILE_PREFIX = 'image-';
const IMAGE_FILE_EXT = '.png';
const DEFAULT_ALLOWED_TOOLS = [
'Read',
'Write',
'Edit',
'MultiEdit',
'Glob',
'Grep',
'LS',
'Bash',
'WebSearch',
'WebFetch',
'TodoWrite',
'Task',
'Skill',
] as const;
const SEARCH_TOOL_NAMES = new Set(['WebSearch', 'WebFetch']);
const MIN_MAX_TURNS = 1;
const CONFIG_KEY_MAX_TURNS = 'max_turns';
const CONSTRAINTS_SECTION_TITLE = 'Codex Execution Constraints';
const CONSTRAINTS_MAX_TURNS_LABEL = 'Max turns';
const CONSTRAINTS_ALLOWED_TOOLS_LABEL = 'Allowed tools';
const CONSTRAINTS_OUTPUT_SCHEMA_LABEL = 'Output format';
const CONSTRAINTS_SESSION_ID_LABEL = 'Session ID';
const CONSTRAINTS_NO_TOOLS_VALUE = 'none';
const CONSTRAINTS_OUTPUT_SCHEMA_VALUE = 'Respond with JSON that matches the provided schema.';
type CodexExecutionMode = typeof CODEX_EXECUTION_MODE_CLI | typeof CODEX_EXECUTION_MODE_SDK;
type CodexExecutionPlan = {
mode: CodexExecutionMode;
cliPath: string | null;
openAiApiKey?: string | null;
};
const ALLOWED_ENV_VARS = [
OPENAI_API_KEY_ENV,
'PATH',
'HOME',
'SHELL',
'TERM',
'USER',
'LANG',
'LC_ALL',
];
function buildEnv(): Record<string, string> {
const env: Record<string, string> = {};
for (const key of ALLOWED_ENV_VARS) {
const value = process.env[key];
if (value) {
env[key] = value;
}
}
return env;
}
async function resolveOpenAiApiKey(): Promise<string | null> {
const envKey = process.env[OPENAI_API_KEY_ENV];
if (envKey) {
return envKey;
}
try {
const settingsService = new SettingsService(getCodexSettingsDir());
const credentials = await settingsService.getCredentials();
const storedKey = credentials.apiKeys.openai?.trim();
return storedKey ? storedKey : null;
} catch {
return null;
}
}
function hasMcpServersConfigured(options: ExecuteOptions): boolean {
return Boolean(options.mcpServers && Object.keys(options.mcpServers).length > 0);
}
function isNoToolsRequested(options: ExecuteOptions): boolean {
return Array.isArray(options.allowedTools) && options.allowedTools.length === 0;
}
function isSdkEligible(options: ExecuteOptions): boolean {
return isNoToolsRequested(options) && !hasMcpServersConfigured(options);
}
function isSdkEligibleWithApiKey(options: ExecuteOptions): boolean {
// When using an API key (not CLI OAuth), prefer SDK over CLI to avoid OAuth issues.
// SDK mode is used when MCP servers are not configured (MCP requires CLI).
// Tool requests are handled by the SDK, so we allow SDK mode even with tools.
return !hasMcpServersConfigured(options);
}
async function resolveCodexExecutionPlan(options: ExecuteOptions): Promise<CodexExecutionPlan> {
const cliPath = await findCodexCliPath();
const authIndicators = await getCodexAuthIndicators();
const openAiApiKey = await resolveOpenAiApiKey();
const hasApiKey = Boolean(openAiApiKey);
const cliAvailable = Boolean(cliPath);
// CLI OAuth login takes priority: if the user has logged in via `codex login`,
// use the CLI regardless of whether an API key is also stored.
// hasOAuthToken = OAuth session from `codex login`
// authIndicators.hasApiKey = API key stored in Codex's own auth file (via `codex login --api-key`)
// Both are "CLI-native" auth — distinct from an API key stored in Automaker's credentials.
const hasCliNativeAuth = authIndicators.hasOAuthToken || authIndicators.hasApiKey;
const sdkEligible = isSdkEligible(options);
// If CLI is available and the user authenticated via the CLI (`codex login`),
// prefer CLI mode over SDK. This ensures `codex login` sessions take priority
// over API keys stored in Automaker's credentials.
if (cliAvailable && hasCliNativeAuth) {
return {
mode: CODEX_EXECUTION_MODE_CLI,
cliPath,
openAiApiKey,
};
}
// No CLI-native auth — prefer SDK when an API key is available.
// Using SDK with an API key avoids OAuth issues that can arise with the CLI.
// MCP servers still require CLI mode since the SDK doesn't support MCP.
if (hasApiKey && isSdkEligibleWithApiKey(options)) {
return {
mode: CODEX_EXECUTION_MODE_SDK,
cliPath,
openAiApiKey,
};
}
// MCP servers are requested with an API key but no CLI-native auth — use CLI mode
// with the API key passed as an environment variable.
if (hasApiKey && cliAvailable) {
return {
mode: CODEX_EXECUTION_MODE_CLI,
cliPath,
openAiApiKey,
};
}
if (sdkEligible) {
if (!cliAvailable) {
throw new Error(ERROR_CODEX_SDK_AUTH_REQUIRED);
}
}
if (!cliAvailable) {
throw new Error(ERROR_CODEX_CLI_REQUIRED);
}
// At this point, neither hasCliNativeAuth nor hasApiKey is true,
// so authentication is required regardless.
throw new Error(ERROR_CODEX_AUTH_REQUIRED);
}
function getEventType(event: Record<string, unknown>): string | null {
if (typeof event.type === 'string') {
return event.type;
}
if (typeof event.event === 'string') {
return event.event;
}
return null;
}
function extractText(value: unknown): string | null {
if (typeof value === 'string') {
return value;
}
if (Array.isArray(value)) {
return value
.map((item) => extractText(item))
.filter(Boolean)
.join('\n');
}
if (value && typeof value === 'object') {
const record = value as Record<string, unknown>;
if (typeof record.text === 'string') {
return record.text;
}
if (typeof record.content === 'string') {
return record.content;
}
if (typeof record.message === 'string') {
return record.message;
}
}
return null;
}
function extractCommandText(item: Record<string, unknown>): string | null {
const direct = extractText(item.command ?? item.input ?? item.content);
if (direct) {
return direct;
}
return null;
}
function extractCommandOutput(item: Record<string, unknown>): string | null {
const outputs: string[] = [];
for (const field of COMMAND_OUTPUT_FIELDS) {
const value = item[field];
const text = extractText(value);
if (text) {
outputs.push(text);
}
}
if (outputs.length === 0) {
return null;
}
const uniqueOutputs = outputs.filter((output, index) => outputs.indexOf(output) === index);
return uniqueOutputs.join(COMMAND_OUTPUT_SEPARATOR);
}
function extractItemType(item: Record<string, unknown>): string | null {
if (typeof item.type === 'string') {
return item.type;
}
if (typeof item.kind === 'string') {
return item.kind;
}
return null;
}
function resolveSystemPrompt(systemPrompt?: unknown): string | null {
if (!systemPrompt) {
return null;
}
if (typeof systemPrompt === 'string') {
return systemPrompt;
}
if (typeof systemPrompt === 'object' && systemPrompt !== null) {
const record = systemPrompt as Record<string, unknown>;
if (typeof record.append === 'string') {
return record.append;
}
}
return null;
}
function buildPromptText(options: ExecuteOptions): string {
return typeof options.prompt === 'string'
? options.prompt
: extractTextFromContent(options.prompt);
}
function buildCombinedPrompt(options: ExecuteOptions, systemPromptText?: string | null): string {
const promptText = buildPromptText(options);
const historyText = options.conversationHistory
? formatHistoryAsText(options.conversationHistory)
: '';
const resolvedSystemPrompt = systemPromptText ?? resolveSystemPrompt(options.systemPrompt);
const systemSection = resolvedSystemPrompt
? `${SYSTEM_PROMPT_LABEL}:\n${resolvedSystemPrompt}\n\n`
: '';
return `${historyText}${systemSection}${HISTORY_HEADER}${promptText}`;
}
function buildResumePrompt(options: ExecuteOptions): string {
const promptText = buildPromptText(options);
return `${HISTORY_HEADER}${promptText}`;
}
function formatConfigValue(value: string | number | boolean): string {
return String(value);
}
function buildConfigOverrides(
overrides: Array<{ key: string; value: string | number | boolean }>
): string[] {
const args: string[] = [];
for (const override of overrides) {
args.push(CODEX_CONFIG_FLAG, `${override.key}=${formatConfigValue(override.value)}`);
}
return args;
}
function resolveMaxTurns(maxTurns?: number): number | null {
if (typeof maxTurns !== 'number' || Number.isNaN(maxTurns) || !Number.isFinite(maxTurns)) {
return null;
}
const normalized = Math.floor(maxTurns);
return normalized >= MIN_MAX_TURNS ? normalized : null;
}
function resolveSearchEnabled(allowedTools: string[], restrictTools: boolean): boolean {
const toolsToCheck = restrictTools ? allowedTools : Array.from(DEFAULT_ALLOWED_TOOLS);
return toolsToCheck.some((tool) => SEARCH_TOOL_NAMES.has(tool));
}
function buildCodexConstraintsPrompt(
options: ExecuteOptions,
config: {
allowedTools: string[];
restrictTools: boolean;
maxTurns: number | null;
hasOutputSchema: boolean;
}
): string | null {
const lines: string[] = [];
if (config.maxTurns !== null) {
lines.push(`${CONSTRAINTS_MAX_TURNS_LABEL}: ${config.maxTurns}`);
}
if (config.restrictTools) {
const allowed =
config.allowedTools.length > 0 ? config.allowedTools.join(', ') : CONSTRAINTS_NO_TOOLS_VALUE;
lines.push(`${CONSTRAINTS_ALLOWED_TOOLS_LABEL}: ${allowed}`);
}
if (config.hasOutputSchema) {
lines.push(`${CONSTRAINTS_OUTPUT_SCHEMA_LABEL}: ${CONSTRAINTS_OUTPUT_SCHEMA_VALUE}`);
}
if (options.sdkSessionId) {
lines.push(`${CONSTRAINTS_SESSION_ID_LABEL}: ${options.sdkSessionId}`);
}
if (lines.length === 0) {
return null;
}
return `## ${CONSTRAINTS_SECTION_TITLE}\n${lines.map((line) => `- ${line}`).join('\n')}`;
}
async function writeOutputSchemaFile(
cwd: string,
outputFormat?: ExecuteOptions['outputFormat']
): Promise<string | null> {
if (!outputFormat || outputFormat.type !== 'json_schema') {
return null;
}
if (!outputFormat.schema || typeof outputFormat.schema !== 'object') {
throw new Error('Codex output schema must be a JSON object.');
}
const schemaDir = path.join(cwd, CODEX_INSTRUCTIONS_DIR);
await secureFs.mkdir(schemaDir, { recursive: true });
const schemaPath = path.join(schemaDir, OUTPUT_SCHEMA_FILENAME);
const schemaContent = JSON.stringify(outputFormat.schema, null, OUTPUT_SCHEMA_INDENT_SPACES);
await secureFs.writeFile(schemaPath, schemaContent, TEXT_ENCODING);
return schemaPath;
}
type ImageBlock = {
type: 'image';
source: {
type: string;
media_type: string;
data: string;
};
};
function extractImageBlocks(prompt: ExecuteOptions['prompt']): ImageBlock[] {
if (typeof prompt === 'string') {
return [];
}
if (!Array.isArray(prompt)) {
return [];
}
const images: ImageBlock[] = [];
for (const block of prompt) {
if (
block &&
typeof block === 'object' &&
'type' in block &&
block.type === 'image' &&
'source' in block &&
block.source &&
typeof block.source === 'object' &&
'data' in block.source &&
'media_type' in block.source
) {
images.push(block as ImageBlock);
}
}
return images;
}
async function writeImageFiles(cwd: string, imageBlocks: ImageBlock[]): Promise<string[]> {
if (imageBlocks.length === 0) {
return [];
}
const imageDir = path.join(cwd, CODEX_INSTRUCTIONS_DIR, IMAGE_TEMP_DIR);
await secureFs.mkdir(imageDir, { recursive: true });
const imagePaths: string[] = [];
for (let i = 0; i < imageBlocks.length; i++) {
const imageBlock = imageBlocks[i];
const imageName = `${IMAGE_FILE_PREFIX}${Date.now()}-${i}${IMAGE_FILE_EXT}`;
const imagePath = path.join(imageDir, imageName);
// Convert base64 to buffer
const imageData = Buffer.from(imageBlock.source.data, 'base64');
await secureFs.writeFile(imagePath, imageData);
imagePaths.push(imagePath);
}
return imagePaths;
}
function normalizeIdentifier(value: unknown): string | null {
if (typeof value === 'string') {
const trimmed = value.trim();
return trimmed ? trimmed : null;
}
if (typeof value === 'number' && Number.isFinite(value)) {
return String(value);
}
return null;
}
function getIdentifierFromRecord(
record: Record<string, unknown>,
keys: readonly string[]
): string | null {
for (const key of keys) {
const id = normalizeIdentifier(record[key]);
if (id) {
return id;
}
}
return null;
}
function getItemIdentifier(
event: Record<string, unknown>,
item: Record<string, unknown>
): string | null {
return (
getIdentifierFromRecord(item, ITEM_ID_KEYS) ?? getIdentifierFromRecord(event, EVENT_ID_KEYS)
);
}
class CodexToolUseTracker {
private readonly toolUseIdsByItem = new Map<string, string>();
private readonly anonymousToolUses: string[] = [];
private sequence = 0;
register(event: Record<string, unknown>, item: Record<string, unknown>): string {
const itemId = getItemIdentifier(event, item);
const toolUseId = this.nextToolUseId();
if (itemId) {
this.toolUseIdsByItem.set(itemId, toolUseId);
} else {
this.anonymousToolUses.push(toolUseId);
}
return toolUseId;
}
resolve(event: Record<string, unknown>, item: Record<string, unknown>): string | null {
const itemId = getItemIdentifier(event, item);
if (itemId) {
const toolUseId = this.toolUseIdsByItem.get(itemId);
if (toolUseId) {
this.toolUseIdsByItem.delete(itemId);
return toolUseId;
}
}
if (this.anonymousToolUses.length > 0) {
return this.anonymousToolUses.shift() || null;
}
return null;
}
private nextToolUseId(): string {
this.sequence += 1;
return `${TOOL_USE_ID_PREFIX}${this.sequence}`;
}
}
type CodexCliSettings = {
autoLoadAgents: boolean;
sandboxMode: CodexSandboxMode;
approvalPolicy: CodexApprovalPolicy;
enableWebSearch: boolean;
enableImages: boolean;
additionalDirs: string[];
threadId?: string;
};
function getCodexSettingsDir(): string {
const configured = getDataDirectory() ?? process.env.DATA_DIR;
return configured ? path.resolve(configured) : path.resolve(CODEX_SETTINGS_DIR_FALLBACK);
}
async function loadCodexCliSettings(
overrides?: ExecuteOptions['codexSettings']
): Promise<CodexCliSettings> {
const defaults: CodexCliSettings = {
autoLoadAgents: DEFAULT_CODEX_AUTO_LOAD_AGENTS,
sandboxMode: DEFAULT_CODEX_SANDBOX_MODE,
approvalPolicy: DEFAULT_CODEX_APPROVAL_POLICY,
enableWebSearch: false,
enableImages: true,
additionalDirs: [],
threadId: undefined,
};
try {
const settingsService = new SettingsService(getCodexSettingsDir());
const settings = await settingsService.getGlobalSettings();
const resolved: CodexCliSettings = {
autoLoadAgents: settings.codexAutoLoadAgents ?? defaults.autoLoadAgents,
sandboxMode: settings.codexSandboxMode ?? defaults.sandboxMode,
approvalPolicy: settings.codexApprovalPolicy ?? defaults.approvalPolicy,
enableWebSearch: settings.codexEnableWebSearch ?? defaults.enableWebSearch,
enableImages: settings.codexEnableImages ?? defaults.enableImages,
additionalDirs: settings.codexAdditionalDirs ?? defaults.additionalDirs,
threadId: settings.codexThreadId,
};
if (!overrides) {
return resolved;
}
return {
autoLoadAgents: overrides.autoLoadAgents ?? resolved.autoLoadAgents,
sandboxMode: overrides.sandboxMode ?? resolved.sandboxMode,
approvalPolicy: overrides.approvalPolicy ?? resolved.approvalPolicy,
enableWebSearch: overrides.enableWebSearch ?? resolved.enableWebSearch,
enableImages: overrides.enableImages ?? resolved.enableImages,
additionalDirs: overrides.additionalDirs ?? resolved.additionalDirs,
threadId: overrides.threadId ?? resolved.threadId,
};
} catch {
return {
autoLoadAgents: overrides?.autoLoadAgents ?? defaults.autoLoadAgents,
sandboxMode: overrides?.sandboxMode ?? defaults.sandboxMode,
approvalPolicy: overrides?.approvalPolicy ?? defaults.approvalPolicy,
enableWebSearch: overrides?.enableWebSearch ?? defaults.enableWebSearch,
enableImages: overrides?.enableImages ?? defaults.enableImages,
additionalDirs: overrides?.additionalDirs ?? defaults.additionalDirs,
threadId: overrides?.threadId ?? defaults.threadId,
};
}
}
function buildCodexInstructionsPrompt(
filePath: string,
content: string,
sourceLabel: string
): string {
return `## ${CODEX_INSTRUCTIONS_SECTION}\n**${CODEX_INSTRUCTIONS_SOURCE_LABEL}:** ${sourceLabel}\n**${CODEX_INSTRUCTIONS_PATH_LABEL}:** \`${filePath}\`\n\n${content}`;
}
async function readCodexInstructionFile(filePath: string): Promise<string | null> {
try {
const raw = await secureFs.readFile(filePath, TEXT_ENCODING);
const content = String(raw).trim();
return content ? content : null;
} catch {
return null;
}
}
async function loadCodexInstructions(cwd: string, enabled: boolean): Promise<string | null> {
if (!enabled) {
return null;
}
const sources: Array<{ path: string; content: string; sourceLabel: string }> = [];
const userInstructionsPath = path.join(getCodexConfigDir(), CODEX_USER_INSTRUCTIONS_FILE);
const userContent = await readCodexInstructionFile(userInstructionsPath);
if (userContent) {
sources.push({
path: userInstructionsPath,
content: userContent,
sourceLabel: CODEX_INSTRUCTIONS_USER_SOURCE,
});
}
for (const fileName of CODEX_PROJECT_INSTRUCTIONS_FILES) {
const projectPath = path.join(cwd, CODEX_INSTRUCTIONS_DIR, fileName);
const projectContent = await readCodexInstructionFile(projectPath);
if (projectContent) {
sources.push({
path: projectPath,
content: projectContent,
sourceLabel: CODEX_INSTRUCTIONS_PROJECT_SOURCE,
});
}
}
if (sources.length === 0) {
return null;
}
const seen = new Set<string>();
const uniqueSources = sources.filter((source) => {
const normalized = source.content.trim();
if (seen.has(normalized)) {
return false;
}
seen.add(normalized);
return true;
});
return uniqueSources
.map((source) => buildCodexInstructionsPrompt(source.path, source.content, source.sourceLabel))
.join('\n\n');
}
const logger = createLogger('CodexProvider');
export class CodexProvider extends BaseProvider {
getName(): string {
return 'codex';
}
async *executeQuery(options: ExecuteOptions): AsyncGenerator<ProviderMessage> {
// Validate that model doesn't have a provider prefix (except codex- which should already be stripped)
// AgentService should strip prefixes before passing to providers
validateBareModelId(options.model, 'CodexProvider', 'codex');
try {
const mcpServers = options.mcpServers ?? {};
const hasMcpServers = Object.keys(mcpServers).length > 0;
const codexSettings = await loadCodexCliSettings(options.codexSettings);
const codexInstructions = await loadCodexInstructions(
options.cwd,
codexSettings.autoLoadAgents
);
const baseSystemPrompt = resolveSystemPrompt(options.systemPrompt);
const resolvedMaxTurns = resolveMaxTurns(options.maxTurns);
if (resolvedMaxTurns === null && options.maxTurns === undefined) {
logger.warn(
`[executeQuery] maxTurns not provided — Codex CLI will use its internal default. ` +
`This may cause premature completion. Model: ${options.model}`
);
} else {
logger.info(
`[executeQuery] maxTurns: requested=${options.maxTurns}, resolved=${resolvedMaxTurns}, model=${options.model}`
);
}
const resolvedAllowedTools = options.allowedTools ?? Array.from(DEFAULT_ALLOWED_TOOLS);
const restrictTools = !hasMcpServers || options.mcpUnrestrictedTools === false;
const wantsOutputSchema = Boolean(
options.outputFormat && options.outputFormat.type === 'json_schema'
);
const constraintsPrompt = buildCodexConstraintsPrompt(options, {
allowedTools: resolvedAllowedTools,
restrictTools,
maxTurns: resolvedMaxTurns,
hasOutputSchema: wantsOutputSchema,
});
const systemPromptParts = [codexInstructions, baseSystemPrompt, constraintsPrompt].filter(
(part): part is string => Boolean(part)
);
const combinedSystemPrompt = systemPromptParts.length
? systemPromptParts.join(SYSTEM_PROMPT_SEPARATOR)
: null;
const executionPlan = await resolveCodexExecutionPlan(options);
if (executionPlan.mode === CODEX_EXECUTION_MODE_SDK) {
const cleanupEnv = executionPlan.openAiApiKey
? createTempEnvOverride({ [OPENAI_API_KEY_ENV]: executionPlan.openAiApiKey })
: null;
try {
yield* executeCodexSdkQuery(options, combinedSystemPrompt);
} finally {
cleanupEnv?.();
}
return;
}
if (hasMcpServers) {
const configManager = new CodexConfigManager();
await configManager.configureMcpServers(options.cwd, options.mcpServers!);
}
const toolUseTracker = new CodexToolUseTracker();
const sandboxCheck = checkSandboxCompatibility(
options.cwd,
codexSettings.sandboxMode !== 'danger-full-access'
);
if (!sandboxCheck.enabled && sandboxCheck.message) {
console.warn(`[CodexProvider] ${sandboxCheck.message}`);
}
const searchEnabled =
codexSettings.enableWebSearch || resolveSearchEnabled(resolvedAllowedTools, restrictTools);
const isResumeQuery = Boolean(options.sdkSessionId);
const schemaPath = isResumeQuery
? null
: await writeOutputSchemaFile(options.cwd, options.outputFormat);
const imageBlocks =
!isResumeQuery && codexSettings.enableImages ? extractImageBlocks(options.prompt) : [];
const imagePaths = isResumeQuery ? [] : await writeImageFiles(options.cwd, imageBlocks);
const approvalPolicy =
hasMcpServers && options.mcpAutoApproveTools !== undefined
? options.mcpAutoApproveTools
? 'never'
: 'on-request'
: codexSettings.approvalPolicy;
const promptText = isResumeQuery
? buildResumePrompt(options)
: buildCombinedPrompt(options, combinedSystemPrompt);
const commandPath = executionPlan.cliPath || CODEX_COMMAND;
// Build config overrides for max turns and reasoning effort
const overrides: Array<{ key: string; value: string | number | boolean }> = [];
if (resolvedMaxTurns !== null) {
overrides.push({ key: CONFIG_KEY_MAX_TURNS, value: resolvedMaxTurns });
}
// Add reasoning effort if model supports it and reasoningEffort is specified
if (
options.reasoningEffort &&
supportsReasoningEffort(options.model) &&
options.reasoningEffort !== 'none'
) {
overrides.push({ key: CODEX_REASONING_EFFORT_KEY, value: options.reasoningEffort });
}
// Add approval policy
overrides.push({ key: 'approval_policy', value: approvalPolicy });
// Add web search if enabled
if (searchEnabled) {
overrides.push({ key: 'features.web_search_request', value: true });
}
const configOverrideArgs = buildConfigOverrides(overrides);
const preExecArgs: string[] = [];
// Add additional directories with write access
if (
!isResumeQuery &&
codexSettings.additionalDirs &&
codexSettings.additionalDirs.length > 0
) {
for (const dir of codexSettings.additionalDirs) {
preExecArgs.push(CODEX_ADD_DIR_FLAG, dir);
}
}
// If images were written to disk, add the image directory so the CLI can access them.
// Note: imagePaths is set to [] when isResumeQuery is true, so this check is sufficient.
if (imagePaths.length > 0) {
const imageDir = path.join(options.cwd, CODEX_INSTRUCTIONS_DIR, IMAGE_TEMP_DIR);
preExecArgs.push(CODEX_ADD_DIR_FLAG, imageDir);
}
// Model is already bare (no prefix) - validated by executeQuery
const codexCommand = isResumeQuery
? [CODEX_EXEC_SUBCOMMAND, CODEX_RESUME_SUBCOMMAND]
: [CODEX_EXEC_SUBCOMMAND];
const args = [
...codexCommand,
CODEX_YOLO_FLAG,
CODEX_SKIP_GIT_REPO_CHECK_FLAG,
...preExecArgs,
CODEX_MODEL_FLAG,
options.model,
CODEX_JSON_FLAG,
...configOverrideArgs,
...(schemaPath ? [CODEX_OUTPUT_SCHEMA_FLAG, schemaPath] : []),
...(options.sdkSessionId ? [options.sdkSessionId] : []),
'-', // Read prompt from stdin to avoid shell escaping issues
];
const envOverrides = buildEnv();
if (executionPlan.openAiApiKey && !envOverrides[OPENAI_API_KEY_ENV]) {
envOverrides[OPENAI_API_KEY_ENV] = executionPlan.openAiApiKey;
}
// Calculate dynamic timeout based on reasoning effort.
// Higher reasoning effort (e.g., 'xhigh' for "xtra thinking" mode) requires more time
// for the model to generate reasoning tokens before producing output.
// This fixes GitHub issue #530 where features would get stuck with reasoning models.
//
// For feature generation with 'xhigh', use the extended 5-minute base timeout
// since generating 50+ features takes significantly longer than normal operations.
const baseTimeout =
options.reasoningEffort === 'xhigh'
? CODEX_FEATURE_GENERATION_BASE_TIMEOUT_MS
: CODEX_CLI_TIMEOUT_MS;
const timeout = calculateReasoningTimeout(options.reasoningEffort, baseTimeout);
const stream = spawnJSONLProcess({
command: commandPath,
args,
cwd: options.cwd,
env: envOverrides,
abortController: options.abortController,
timeout,
stdinData: promptText, // Pass prompt via stdin
});
for await (const rawEvent of stream) {
const event = rawEvent as Record<string, unknown>;
const eventType = getEventType(event);
// Track thread/session ID from events
const threadId = event.thread_id;
if (threadId && typeof threadId === 'string') {
this._lastSessionId = threadId;
}
if (eventType === CODEX_EVENT_TYPES.error) {
const errorText = extractText(event.error ?? event.message) || 'Codex CLI error';
// Enhance error message with helpful context
let enhancedError = errorText;
const errorLower = errorText.toLowerCase();
if (errorLower.includes('rate limit')) {
enhancedError = `${errorText}\n\nTip: You're being rate limited. Try reducing concurrent tasks or waiting a few minutes before retrying.`;
} else if (errorLower.includes('authentication') || errorLower.includes('unauthorized')) {
enhancedError = `${errorText}\n\nTip: Check that your OPENAI_API_KEY is set correctly or run 'codex login' to authenticate.`;
} else if (
errorLower.includes('model does not exist') ||
errorLower.includes('requested model does not exist') ||
errorLower.includes('do not have access') ||
errorLower.includes('model_not_found') ||
errorLower.includes('invalid_model')
) {
enhancedError =
`${errorText}\n\nTip: The model '${options.model}' may not be available on your OpenAI plan. ` +
`See https://platform.openai.com/docs/models for available models. ` +
`Some models require a ChatGPT Pro/Plus subscription—authenticate with 'codex login' instead of an API key.`;
} else if (
errorLower.includes('stream disconnected') ||
errorLower.includes('stream ended') ||
errorLower.includes('connection reset')
) {
enhancedError =
`${errorText}\n\nTip: The connection to OpenAI was interrupted. This can happen due to:\n` +
`- Network instability\n` +
`- The model not being available on your plan\n` +
`- Server-side timeouts for long-running requests\n` +
`Try again, or switch to a different model.`;
} else if (
errorLower.includes('command not found') ||
errorLower.includes('is not recognized as an internal or external command')
) {
enhancedError = `${errorText}\n\nTip: Make sure the Codex CLI is installed. Run 'npm install -g @openai/codex-cli' to install.`;
}
console.error('[CodexProvider] CLI error event:', { errorText, event });
yield { type: 'error', error: enhancedError };
continue;
}
if (eventType === CODEX_EVENT_TYPES.turnCompleted) {
const resultText = extractText(event.result) || undefined;
yield { type: 'result', subtype: 'success', result: resultText };
continue;
}
if (!eventType) {
const fallbackText = extractText(event);
if (fallbackText) {
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'text', text: fallbackText }],
},
};
}
continue;
}
const item = (event.item ?? {}) as Record<string, unknown>;
const itemType = extractItemType(item);
if (
eventType === CODEX_EVENT_TYPES.itemStarted &&
itemType === CODEX_ITEM_TYPES.commandExecution
) {
const commandText = extractCommandText(item) || '';
const tool = resolveCodexToolCall(commandText);
const toolUseId = toolUseTracker.register(event, item);
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [
{
type: 'tool_use',
name: tool.name,
input: tool.input,
tool_use_id: toolUseId,
},
],
},
};
continue;
}
if (eventType === CODEX_EVENT_TYPES.itemUpdated && itemType === CODEX_ITEM_TYPES.todoList) {
const todos = extractCodexTodoItems(item);
if (todos) {
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [
{
type: 'tool_use',
name: getCodexTodoToolName(),
input: { todos },
},
],
},
};
} else {
const todoText = extractText(item) || '';
const formatted = todoText ? `Updated TODO list:\n${todoText}` : 'Updated TODO list';
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'text', text: formatted }],
},
};
}
continue;
}
if (eventType === CODEX_EVENT_TYPES.itemCompleted) {
if (itemType === CODEX_ITEM_TYPES.reasoning) {
const thinkingText = extractText(item) || '';
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'thinking', thinking: thinkingText }],
},
};
continue;
}
if (itemType === CODEX_ITEM_TYPES.commandExecution) {
const commandOutput =
extractCommandOutput(item) ?? extractCommandText(item) ?? extractText(item) ?? '';
if (commandOutput) {
const toolUseId = toolUseTracker.resolve(event, item);
const toolResultBlock: {
type: 'tool_result';
content: string;
tool_use_id?: string;
} = { type: 'tool_result', content: commandOutput };
if (toolUseId) {
toolResultBlock.tool_use_id = toolUseId;
}
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [toolResultBlock],
},
};
}
continue;
}
const text = extractText(item) || extractText(event);
if (text) {
yield {
type: 'assistant',
message: {
role: 'assistant',
content: [{ type: 'text', text }],
},
};
}
}
}
} catch (error) {
const errorInfo = classifyError(error);
const userMessage = getUserFriendlyErrorMessage(error);
const enhancedMessage = errorInfo.isRateLimit
? `${userMessage}\n\nTip: If you're rate limited, try reducing concurrent tasks or waiting a few minutes.`
: userMessage;
console.error('[CodexProvider] executeQuery() error:', {
type: errorInfo.type,
message: errorInfo.message,
isRateLimit: errorInfo.isRateLimit,
retryAfter: errorInfo.retryAfter,
stack: error instanceof Error ? error.stack : undefined,
});
yield { type: 'error', error: enhancedMessage };
}
}
async detectInstallation(): Promise<InstallationStatus> {
const cliPath = await findCodexCliPath();
const hasApiKey = Boolean(await resolveOpenAiApiKey());
const installed = !!cliPath;
let version = '';
if (installed) {
try {
const result = await spawnProcess({
command: cliPath || CODEX_COMMAND,
args: [CODEX_VERSION_FLAG],
cwd: process.cwd(),
});
version = result.stdout.trim();
} catch {
version = '';
}
}
// Determine auth status - always verify with CLI, never assume authenticated
const authCheck = await checkCodexAuthentication(cliPath);
const authenticated = authCheck.authenticated;
return {
installed,
path: cliPath || undefined,
version: version || undefined,
method: 'cli' as const, // Installation method
hasApiKey,
authenticated,
};
}
getAvailableModels(): ModelDefinition[] {
// Return all available Codex/OpenAI models
return CODEX_MODELS;
}
/**
* Check authentication status for Codex CLI
*/
async checkAuth(): Promise<CodexAuthStatus> {
const cliPath = await findCodexCliPath();
const hasApiKey = Boolean(await resolveOpenAiApiKey());
const authIndicators = await getCodexAuthIndicators();
// Check for API key in environment
if (hasApiKey) {
return { authenticated: true, method: 'api_key' };
}
// Check for OAuth/token from Codex CLI
if (authIndicators.hasOAuthToken || authIndicators.hasApiKey) {
return { authenticated: true, method: 'oauth' };
}
// CLI is installed but not authenticated via indicators - try CLI command
if (cliPath) {
try {
// Try 'codex login status' first (same as checkCodexAuthentication)
const result = await spawnProcess({
command: cliPath || CODEX_COMMAND,
args: ['login', 'status'],
cwd: process.cwd(),
env: {
...process.env,
TERM: 'dumb',
},
});
// Check both stdout and stderr - Codex CLI outputs to stderr
const combinedOutput = (result.stdout + result.stderr).toLowerCase();
const isLoggedIn = combinedOutput.includes('logged in');
if (result.exitCode === 0 && isLoggedIn) {
return { authenticated: true, method: 'oauth' };
}
} catch (error) {
logger.warn('Error running login status command during auth check:', error);
}
}
return { authenticated: false, method: 'none' };
}
/**
* Get the detected CLI path (public accessor for status endpoints)
*/
async getCliPath(): Promise<string | null> {
const path = await findCodexCliPath();
return path || null;
}
/**
* Get the last CLI session ID (for tracking across queries)
* This can be used to resume sessions in subsequent requests
*/
getLastSessionId(): string | null {
return this._lastSessionId ?? null;
}
/**
* Set a session ID to use for CLI session resumption
*/
setSessionId(sessionId: string | null): void {
this._lastSessionId = sessionId;
}
private _lastSessionId: string | null = null;
}
|