@just-every/ensemble 0.2.244 → 0.2.246

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (32) hide show
  1. package/README.md +1 -0
  2. package/dist/cjs/data/model_data.cjs +124 -6
  3. package/dist/cjs/data/model_data.d.ts.map +1 -1
  4. package/dist/cjs/data/model_data.js.map +1 -1
  5. package/dist/cjs/model_providers/claude.cjs +8 -7
  6. package/dist/cjs/model_providers/claude.d.ts.map +1 -1
  7. package/dist/cjs/model_providers/claude.js.map +1 -1
  8. package/dist/cjs/model_providers/codex.cjs +566 -20
  9. package/dist/cjs/model_providers/codex.d.ts.map +1 -1
  10. package/dist/cjs/model_providers/codex.js.map +1 -1
  11. package/dist/cjs/model_providers/gemini.cjs +43 -1
  12. package/dist/cjs/model_providers/gemini.d.ts.map +1 -1
  13. package/dist/cjs/model_providers/gemini.js.map +1 -1
  14. package/dist/cjs/tsconfig.cjs.tsbuildinfo +1 -1
  15. package/dist/cjs/types/types.d.ts +3 -0
  16. package/dist/cjs/types/types.d.ts.map +1 -1
  17. package/dist/data/model_data.d.ts.map +1 -1
  18. package/dist/data/model_data.js +124 -6
  19. package/dist/data/model_data.js.map +1 -1
  20. package/dist/model_providers/claude.d.ts.map +1 -1
  21. package/dist/model_providers/claude.js +8 -7
  22. package/dist/model_providers/claude.js.map +1 -1
  23. package/dist/model_providers/codex.d.ts.map +1 -1
  24. package/dist/model_providers/codex.js +534 -21
  25. package/dist/model_providers/codex.js.map +1 -1
  26. package/dist/model_providers/gemini.d.ts.map +1 -1
  27. package/dist/model_providers/gemini.js +43 -1
  28. package/dist/model_providers/gemini.js.map +1 -1
  29. package/dist/tsconfig.tsbuildinfo +1 -1
  30. package/dist/types/types.d.ts +3 -0
  31. package/dist/types/types.d.ts.map +1 -1
  32. package/package.json +1 -1
@@ -1,4 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ var desc = Object.getOwnPropertyDescriptor(m, k);
5
+ if (!desc || ("get" in desc ? !m.__esModule : desc.writable || desc.configurable)) {
6
+ desc = { enumerable: true, get: function() { return m[k]; } };
7
+ }
8
+ Object.defineProperty(o, k2, desc);
9
+ }) : (function(o, m, k, k2) {
10
+ if (k2 === undefined) k2 = k;
11
+ o[k2] = m[k];
12
+ }));
13
+ var __setModuleDefault = (this && this.__setModuleDefault) || (Object.create ? (function(o, v) {
14
+ Object.defineProperty(o, "default", { enumerable: true, value: v });
15
+ }) : function(o, v) {
16
+ o["default"] = v;
17
+ });
18
+ var __importStar = (this && this.__importStar) || (function () {
19
+ var ownKeys = function(o) {
20
+ ownKeys = Object.getOwnPropertyNames || function (o) {
21
+ var ar = [];
22
+ for (var k in o) if (Object.prototype.hasOwnProperty.call(o, k)) ar[ar.length] = k;
23
+ return ar;
24
+ };
25
+ return ownKeys(o);
26
+ };
27
+ return function (mod) {
28
+ if (mod && mod.__esModule) return mod;
29
+ var result = {};
30
+ if (mod != null) for (var k = ownKeys(mod), i = 0; i < k.length; i++) if (k[i] !== "default") __createBinding(result, mod, k[i]);
31
+ __setModuleDefault(result, mod);
32
+ return result;
33
+ };
34
+ })();
2
35
  var __importDefault = (this && this.__importDefault) || function (mod) {
3
36
  return (mod && mod.__esModule) ? mod : { "default": mod };
4
37
  };
@@ -6,9 +39,11 @@ Object.defineProperty(exports, "__esModule", { value: true });
6
39
  exports.codexProvider = exports.CodexProvider = void 0;
7
40
  exports.resolveCodexModel = resolveCodexModel;
8
41
  const child_process_1 = require("child_process");
42
+ const crypto_1 = require("crypto");
9
43
  const promises_1 = require("fs/promises");
10
44
  const os_1 = require("os");
11
45
  const path_1 = __importDefault(require("path"));
46
+ const promises_2 = require("timers/promises");
12
47
  const base_provider_js_1 = require("./base_provider.cjs");
13
48
  const cost_tracker_js_1 = require("../utils/cost_tracker.cjs");
14
49
  const llm_logger_js_1 = require("../utils/llm_logger.cjs");
@@ -190,7 +225,7 @@ function summarizeCodexExecStream(value) {
190
225
  truncated: true,
191
226
  };
192
227
  }
193
- function codexUsageFromJsonl(stdout, model, requestId) {
228
+ function codexUsageFromJsonl(stdout, model, requestId, metadata) {
194
229
  let latestUsage;
195
230
  for (const line of stdout.split(/\r?\n/)) {
196
231
  const trimmed = line.trim();
@@ -228,6 +263,7 @@ function codexUsageFromJsonl(stdout, model, requestId) {
228
263
  cached_tokens: latestUsage.cached_input_tokens,
229
264
  request_id: requestId,
230
265
  metadata: {
266
+ ...metadata,
231
267
  source: 'codex_cli_json',
232
268
  reasoning_output_tokens: latestUsage.reasoning_output_tokens ?? 0,
233
269
  },
@@ -239,6 +275,418 @@ function finiteNumber(value) {
239
275
  function isRecord(value) {
240
276
  return Boolean(value) && typeof value === 'object' && !Array.isArray(value);
241
277
  }
278
+ function getCodexProviderSettings(settings) {
279
+ return settings;
280
+ }
281
+ function compactJson(value) {
282
+ return JSON.stringify(value);
283
+ }
284
+ function getToolNames(tools) {
285
+ return tools.map(tool => tool.definition.function.name);
286
+ }
287
+ function getTerminalToolNameSet(agent) {
288
+ return new Set((agent.terminalToolNames ?? []).filter((name) => typeof name === 'string' && name.trim().length > 0));
289
+ }
290
+ function getAvailableTerminalToolNames(tools, agent) {
291
+ const terminalNames = getTerminalToolNameSet(agent);
292
+ if (terminalNames.size === 0)
293
+ return [];
294
+ return getToolNames(tools).filter(name => terminalNames.has(name));
295
+ }
296
+ function createCodexToolOutputSchema(tools, agent) {
297
+ const requiresTerminalTool = getAvailableTerminalToolNames(tools, agent).length > 0;
298
+ return {
299
+ name: 'codex_tool_action',
300
+ type: 'json_schema',
301
+ description: 'A simulated Ensemble tool action for Codex CLI provider requests.',
302
+ schema: {
303
+ type: 'object',
304
+ additionalProperties: false,
305
+ properties: {
306
+ action: {
307
+ type: 'string',
308
+ enum: requiresTerminalTool ? ['tool_calls'] : ['tool_calls', 'final_response'],
309
+ description: requiresTerminalTool
310
+ ? 'Use tool_calls. This request is only complete after calling a terminal tool.'
311
+ : 'Use tool_calls when requesting Ensemble tool execution; use final_response only when no tool is needed.',
312
+ },
313
+ toolCalls: {
314
+ type: 'array',
315
+ description: 'Tool calls to execute in parallel for this turn. Empty when action is final_response.',
316
+ items: {
317
+ type: 'object',
318
+ additionalProperties: false,
319
+ properties: {
320
+ name: {
321
+ type: 'string',
322
+ enum: getToolNames(tools),
323
+ },
324
+ argumentsJson: {
325
+ type: 'string',
326
+ description: 'A compact JSON string containing the complete arguments object for the named tool.',
327
+ },
328
+ },
329
+ required: ['name', 'argumentsJson'],
330
+ },
331
+ },
332
+ finalResponse: {
333
+ type: 'string',
334
+ description: 'Final assistant response. Use an empty string when action is tool_calls.',
335
+ },
336
+ },
337
+ required: ['action', 'toolCalls', 'finalResponse'],
338
+ },
339
+ };
340
+ }
341
+ function describeCodexToolChoice(settings) {
342
+ const toolChoice = settings?.tool_choice;
343
+ if (toolChoice === 'required') {
344
+ return 'This turn requires at least one tool call.';
345
+ }
346
+ if (toolChoice === 'none') {
347
+ return 'This turn forbids tool calls; return final_response.';
348
+ }
349
+ if (typeof toolChoice === 'object' && toolChoice?.type === 'function' && toolChoice.function?.name) {
350
+ return `This turn requires the ${toolChoice.function.name} tool.`;
351
+ }
352
+ return 'Use tool_calls when a tool result is needed; otherwise use final_response.';
353
+ }
354
+ function buildCodexToolInstructions(tools, agent) {
355
+ const settings = agent.modelSettings;
356
+ const terminalToolNames = getAvailableTerminalToolNames(tools, agent);
357
+ const toolDescriptions = tools
358
+ .map(tool => {
359
+ const fn = tool.definition.function;
360
+ return [
361
+ `Tool: ${fn.name}`,
362
+ `Description: ${fn.description}`,
363
+ `Parameters JSON schema: ${compactJson(fn.parameters)}`,
364
+ ].join('\n');
365
+ })
366
+ .join('\n\n');
367
+ return [
368
+ 'You are using Ensemble simulated tool mode.',
369
+ 'You cannot execute these tools yourself. Instead, respond only with JSON matching the provided output schema.',
370
+ describeCodexToolChoice(settings),
371
+ 'For action "tool_calls", include one or more entries in toolCalls and set finalResponse to an empty string.',
372
+ terminalToolNames.length > 0
373
+ ? `This request is not complete until you call one of these terminal tools: ${terminalToolNames.join(', ')}. Do not use final_response.`
374
+ : 'For action "final_response", set toolCalls to an empty array and put the final answer in finalResponse.',
375
+ 'Use exact tool names.',
376
+ 'Set argumentsJson to a compact JSON string containing the complete arguments object for the named tool.',
377
+ 'For example, the arguments object {"label":"ok"} must be encoded as "{\\"label\\":\\"ok\\"}".',
378
+ '',
379
+ 'Available tools:',
380
+ toolDescriptions,
381
+ ].join('\n');
382
+ }
383
+ function validateCodexWorkspaceRelativePath(filePath) {
384
+ const normalized = filePath.replace(/\\/g, '/').replace(/^\.\/+/, '');
385
+ if (!normalized ||
386
+ path_1.default.isAbsolute(normalized) ||
387
+ normalized.split('/').some(part => part === '..' || part === '')) {
388
+ throw new Error(`Invalid Codex workspace file path: ${filePath}`);
389
+ }
390
+ return normalized;
391
+ }
392
+ async function writeCodexWorkspaceFiles(tempDir, files) {
393
+ const written = [];
394
+ for (const [rawPath, content] of Object.entries(files ?? {})) {
395
+ const relativePath = validateCodexWorkspaceRelativePath(rawPath);
396
+ const targetPath = path_1.default.join(tempDir, relativePath);
397
+ await (0, promises_1.mkdir)(path_1.default.dirname(targetPath), { recursive: true });
398
+ await (0, promises_1.writeFile)(targetPath, content, 'utf8');
399
+ written.push(relativePath);
400
+ }
401
+ return written.sort();
402
+ }
403
+ function buildCodexWorkspaceFileInstructions(files, extra) {
404
+ if (files.length === 0 && !extra?.trim())
405
+ return '';
406
+ return [
407
+ 'A disposable Codex workspace has been prepared for this request.',
408
+ files.length > 0
409
+ ? `Workspace files available in the current directory:\n${files.map(file => `- ${file}`).join('\n')}`
410
+ : undefined,
411
+ extra?.trim() || undefined,
412
+ ]
413
+ .filter(Boolean)
414
+ .join('\n');
415
+ }
416
+ function shellIdentifier(value) {
417
+ if (!/^[A-Za-z0-9_.:-]+$/.test(value)) {
418
+ throw new Error(`Codex filesystem tool name is not shell-safe: ${value}`);
419
+ }
420
+ return value;
421
+ }
422
+ function buildCodexFilesystemToolInstructions(tools, agent) {
423
+ const terminalToolNames = getAvailableTerminalToolNames(tools, agent);
424
+ const toolDescriptions = tools
425
+ .map(tool => {
426
+ const fn = tool.definition.function;
427
+ return [
428
+ `Command: ./tools/${fn.name} <arguments-json-file>`,
429
+ `Description: ${fn.description}`,
430
+ `Arguments JSON schema: ${compactJson(fn.parameters)}`,
431
+ ].join('\n');
432
+ })
433
+ .join('\n\n');
434
+ return [
435
+ 'You are using Codex filesystem tool mode.',
436
+ 'Work in the current directory. Read the provided JSON files, write candidate JSON files, and run the executable commands in ./tools.',
437
+ 'Each ./tools command takes one path to a JSON arguments file. The command prints the real tool result to stdout.',
438
+ 'When a layered document argument would otherwise be a large JSON string, you may put candidateLayeredDocumentPath or repairedLayeredDocumentPath in the arguments file; the wrapper will read that file and pass the required JSON string to the real tool.',
439
+ terminalToolNames.length > 0
440
+ ? `This request is not complete until you run one of these terminal tool commands: ${terminalToolNames.map(name => `./tools/${name}`).join(', ')}.`
441
+ : undefined,
442
+ 'After the terminal tool command succeeds, respond with a brief completion note. Do not invent tool outputs; use the command output.',
443
+ '',
444
+ 'Available filesystem tools:',
445
+ toolDescriptions,
446
+ ]
447
+ .filter(Boolean)
448
+ .join('\n');
449
+ }
450
+ async function startCodexFilesystemToolBroker(tempDir, tools, agent) {
451
+ const toolsDir = path_1.default.join(tempDir, 'tools');
452
+ const requestDir = path_1.default.join(tempDir, '.codex-tool-requests');
453
+ const responseDir = path_1.default.join(tempDir, '.codex-tool-responses');
454
+ await Promise.all([
455
+ (0, promises_1.mkdir)(toolsDir, { recursive: true }),
456
+ (0, promises_1.mkdir)(requestDir, { recursive: true }),
457
+ (0, promises_1.mkdir)(responseDir, { recursive: true }),
458
+ ]);
459
+ for (const tool of tools) {
460
+ const toolName = shellIdentifier(tool.definition.function.name);
461
+ const toolPath = path_1.default.join(toolsDir, toolName);
462
+ await (0, promises_1.writeFile)(toolPath, buildCodexFilesystemToolWrapperScript({
463
+ toolName,
464
+ workspaceDir: tempDir,
465
+ requestDir,
466
+ responseDir,
467
+ }), 'utf8');
468
+ await (0, promises_1.chmod)(toolPath, 0o755);
469
+ }
470
+ const toolByName = new Map(tools.map(tool => [tool.definition.function.name, tool]));
471
+ const seen = new Set();
472
+ const inFlight = new Set();
473
+ let stopped = false;
474
+ const serviceRequest = async (fileName) => {
475
+ const requestPath = path_1.default.join(requestDir, fileName);
476
+ let request;
477
+ try {
478
+ request = JSON.parse(await (0, promises_1.readFile)(requestPath, 'utf8'));
479
+ }
480
+ catch (error) {
481
+ await (0, promises_1.writeFile)(path_1.default.join(responseDir, fileName), JSON.stringify({
482
+ ok: false,
483
+ error: `Could not read Codex filesystem tool request: ${error instanceof Error ? error.message : String(error)}`,
484
+ }), 'utf8');
485
+ return;
486
+ }
487
+ const toolName = typeof request.toolName === 'string' ? request.toolName : '';
488
+ const tool = toolByName.get(toolName);
489
+ if (!tool) {
490
+ await (0, promises_1.writeFile)(path_1.default.join(responseDir, fileName), JSON.stringify({ ok: false, error: `Unknown Codex filesystem tool: ${toolName || '<missing>'}` }), 'utf8');
491
+ return;
492
+ }
493
+ try {
494
+ const argumentsJson = typeof request.argumentsJson === 'string' ? request.argumentsJson : '{}';
495
+ const parsedArguments = JSON.parse(argumentsJson);
496
+ const result = await tool.function(parsedArguments);
497
+ const output = typeof result === 'string' ? result : JSON.stringify(result);
498
+ await (0, promises_1.writeFile)(path_1.default.join(responseDir, fileName), JSON.stringify({ ok: true, output }), 'utf8');
499
+ }
500
+ catch (error) {
501
+ await (0, promises_1.writeFile)(path_1.default.join(responseDir, fileName), JSON.stringify({
502
+ ok: false,
503
+ error: error instanceof Error ? error.message : String(error),
504
+ }), 'utf8');
505
+ }
506
+ };
507
+ const loop = (async () => {
508
+ while (!stopped) {
509
+ const entries = await (0, promises_1.readdir)(requestDir).catch(() => []);
510
+ for (const fileName of entries) {
511
+ if (!fileName.endsWith('.json') || seen.has(fileName))
512
+ continue;
513
+ seen.add(fileName);
514
+ const task = serviceRequest(fileName).finally(() => {
515
+ inFlight.delete(task);
516
+ });
517
+ inFlight.add(task);
518
+ }
519
+ await (0, promises_2.setTimeout)(50);
520
+ }
521
+ await Promise.allSettled([...inFlight]);
522
+ })();
523
+ return {
524
+ instructions: buildCodexFilesystemToolInstructions(tools, agent),
525
+ stop: async () => {
526
+ stopped = true;
527
+ await loop;
528
+ },
529
+ };
530
+ }
531
+ function buildCodexFilesystemToolWrapperScript(args) {
532
+ return `#!/usr/bin/env node
533
+ const fs = require('fs');
534
+ const path = require('path');
535
+
536
+ const toolName = ${JSON.stringify(args.toolName)};
537
+ const workspaceDir = ${JSON.stringify(args.workspaceDir)};
538
+ const requestDir = ${JSON.stringify(args.requestDir)};
539
+ const responseDir = ${JSON.stringify(args.responseDir)};
540
+
541
+ function sleep(ms) {
542
+ Atomics.wait(new Int32Array(new SharedArrayBuffer(4)), 0, 0, ms);
543
+ }
544
+
545
+ function readText(filePath) {
546
+ return fs.readFileSync(path.resolve(process.cwd(), filePath), 'utf8');
547
+ }
548
+
549
+ function readArgumentsJson() {
550
+ const argsPath = process.argv[2];
551
+ const raw = argsPath ? readText(argsPath) : fs.readFileSync(0, 'utf8');
552
+ if (!raw.trim()) {
553
+ throw new Error('Expected a JSON arguments file path or JSON on stdin.');
554
+ }
555
+ const parsed = JSON.parse(raw);
556
+ if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) {
557
+ throw new Error('Tool arguments must be a JSON object.');
558
+ }
559
+ if (parsed.candidateLayeredDocumentPath && !parsed.candidateLayeredDocumentJson) {
560
+ parsed.candidateLayeredDocumentJson = readText(parsed.candidateLayeredDocumentPath);
561
+ delete parsed.candidateLayeredDocumentPath;
562
+ }
563
+ if (parsed.repairedLayeredDocumentPath && !parsed.repairedLayeredDocumentJson) {
564
+ parsed.repairedLayeredDocumentJson = readText(parsed.repairedLayeredDocumentPath);
565
+ delete parsed.repairedLayeredDocumentPath;
566
+ }
567
+ return JSON.stringify(parsed);
568
+ }
569
+
570
+ async function main() {
571
+ process.chdir(workspaceDir);
572
+ fs.mkdirSync(requestDir, { recursive: true });
573
+ fs.mkdirSync(responseDir, { recursive: true });
574
+ const id = String(Date.now()) + '-' + String(process.pid) + '-' + Math.random().toString(16).slice(2);
575
+ const fileName = id + '.json';
576
+ fs.writeFileSync(
577
+ path.join(requestDir, fileName),
578
+ JSON.stringify({ id, toolName, argumentsJson: readArgumentsJson() }),
579
+ 'utf8'
580
+ );
581
+
582
+ const responsePath = path.join(responseDir, fileName);
583
+ const deadline = Date.now() + 300000;
584
+ while (Date.now() < deadline) {
585
+ if (fs.existsSync(responsePath)) {
586
+ const response = JSON.parse(fs.readFileSync(responsePath, 'utf8'));
587
+ if (!response.ok) {
588
+ console.error(response.error || 'Tool failed.');
589
+ process.exit(1);
590
+ }
591
+ process.stdout.write(String(response.output || ''));
592
+ if (!String(response.output || '').endsWith('\\n')) process.stdout.write('\\n');
593
+ return;
594
+ }
595
+ sleep(50);
596
+ }
597
+ console.error('Timed out waiting for tool result.');
598
+ process.exit(1);
599
+ }
600
+
601
+ main().catch(error => {
602
+ console.error(error && error.message ? error.message : String(error));
603
+ process.exit(1);
604
+ });
605
+ `;
606
+ }
607
+ function normalizeCodexToolArguments(value, toolName) {
608
+ if (typeof value !== 'string') {
609
+ throw new Error(`Codex tool call ${toolName} must provide argumentsJson as a JSON string.`);
610
+ }
611
+ const parsed = JSON.parse(value);
612
+ if (!isRecord(parsed)) {
613
+ throw new Error(`Codex tool call ${toolName} argumentsJson must decode to a JSON object.`);
614
+ }
615
+ return JSON.stringify(parsed);
616
+ }
617
+ function parseCodexToolAction(content, tools, settings, terminalToolNames = []) {
618
+ const parsed = JSON.parse(content);
619
+ if (!isRecord(parsed)) {
620
+ throw new Error('Codex tool response must be a JSON object.');
621
+ }
622
+ const toolChoice = settings?.tool_choice;
623
+ const toolNames = new Set(getToolNames(tools));
624
+ const action = parsed.action;
625
+ if (action === 'final_response') {
626
+ if (terminalToolNames.length > 0) {
627
+ throw new Error(`Codex returned final_response when terminal tool ${terminalToolNames.join(' or ')} was required.`);
628
+ }
629
+ if (toolChoice === 'required') {
630
+ throw new Error('Codex returned final_response when tool_choice required a tool call.');
631
+ }
632
+ if (typeof toolChoice === 'object' && toolChoice?.type === 'function' && toolChoice.function?.name) {
633
+ throw new Error(`Codex returned final_response when ${toolChoice.function.name} was required.`);
634
+ }
635
+ return {
636
+ action: 'final_response',
637
+ content: typeof parsed.finalResponse === 'string' ? parsed.finalResponse : '',
638
+ };
639
+ }
640
+ if (action !== 'tool_calls') {
641
+ throw new Error(`Codex tool response action must be tool_calls or final_response; received ${String(action)}.`);
642
+ }
643
+ if (toolChoice === 'none') {
644
+ throw new Error('Codex returned tool_calls when tool_choice forbids tool use.');
645
+ }
646
+ if (!Array.isArray(parsed.toolCalls) || parsed.toolCalls.length === 0) {
647
+ throw new Error('Codex tool response must include at least one tool call.');
648
+ }
649
+ const requiredToolName = typeof toolChoice === 'object' && toolChoice?.type === 'function' && toolChoice.function?.name
650
+ ? toolChoice.function.name
651
+ : undefined;
652
+ const toolCalls = parsed.toolCalls.map((rawCall, index) => {
653
+ if (!isRecord(rawCall)) {
654
+ throw new Error(`Codex tool call at index ${index} must be an object.`);
655
+ }
656
+ const name = rawCall.name;
657
+ if (typeof name !== 'string' || !toolNames.has(name)) {
658
+ throw new Error(`Codex requested unknown tool ${String(name)}.`);
659
+ }
660
+ if (requiredToolName && name !== requiredToolName) {
661
+ throw new Error(`Codex requested ${name}, but ${requiredToolName} was required.`);
662
+ }
663
+ const id = `codex_tool_${(0, crypto_1.randomUUID)()}`;
664
+ return {
665
+ id,
666
+ call_id: id,
667
+ type: 'function',
668
+ function: {
669
+ name,
670
+ arguments: normalizeCodexToolArguments(rawCall.argumentsJson, name),
671
+ },
672
+ };
673
+ });
674
+ return {
675
+ action: 'tool_calls',
676
+ toolCalls,
677
+ };
678
+ }
679
+ async function readOptionalUtf8File(filePath) {
680
+ try {
681
+ return await (0, promises_1.readFile)(filePath, 'utf8');
682
+ }
683
+ catch (error) {
684
+ if (error.code === 'ENOENT') {
685
+ return null;
686
+ }
687
+ throw error;
688
+ }
689
+ }
242
690
  function serializeCodexExecError(error) {
243
691
  if (!(error instanceof Error))
244
692
  return error;
@@ -362,30 +810,60 @@ async function runCodexExec(options) {
362
810
  });
363
811
  return { stdout, stderr, diagnostics };
364
812
  }
365
- function disabledFeatureArgs(allowImageGeneration = false) {
366
- return CODEX_DISABLED_FEATURES.filter(feature => !(allowImageGeneration && feature === 'image_generation')).flatMap(feature => ['--disable', feature]);
813
+ function disabledFeatureArgs(options = {}) {
814
+ return CODEX_DISABLED_FEATURES.filter(feature => {
815
+ if (options.allowImageGeneration && feature === 'image_generation')
816
+ return false;
817
+ if (options.allowShellTool && feature === 'shell_tool')
818
+ return false;
819
+ return true;
820
+ }).flatMap(feature => ['--disable', feature]);
367
821
  }
368
- async function executeCodexRequest(messages, model, agent, requestId) {
369
- if (agent.tools?.length || agent.getTools || agent.processToolCall || agent.params || agent.processParams) {
370
- throw new Error('Codex provider v1 does not support tool requests.');
822
+ async function executeCodexRequest(messages, model, agent, requestId, options = {}) {
823
+ if (agent.params || agent.processParams) {
824
+ throw new Error('Codex provider v1 does not support params requests.');
371
825
  }
372
826
  const settings = agent.modelSettings;
827
+ const codexSettings = getCodexProviderSettings(settings);
828
+ const requestJsonSchema = options.jsonSchema ?? settings?.json_schema;
373
829
  const { model: codexModel, effort } = resolveCodexModel(model, settings);
374
830
  const codexHome = resolveCodexHome(settings);
375
- const cwd = agent.cwd || process.cwd();
376
831
  const tempDir = await (0, promises_1.mkdtemp)(path_1.default.join((0, os_1.tmpdir)(), 'ensemble-codex-'));
832
+ const cwd = options.useTempCwd ? tempDir : agent.cwd || process.cwd();
377
833
  const imageWriter = new codex_assets_js_1.CodexImageAttachmentWriter(tempDir, cwd);
378
- const { instructions, prompt, images } = await buildCodexInput(messages, agent, imageWriter);
834
+ let filesystemToolBroker = null;
835
+ const workspaceFileNames = await writeCodexWorkspaceFiles(tempDir, codexSettings?.codex_workspace_files);
836
+ if (options.filesystemTools?.length) {
837
+ filesystemToolBroker = await startCodexFilesystemToolBroker(tempDir, options.filesystemTools, agent);
838
+ }
839
+ const extraInstructions = [
840
+ buildCodexWorkspaceFileInstructions(workspaceFileNames, codexSettings?.codex_workspace_instructions),
841
+ filesystemToolBroker?.instructions,
842
+ options.extraInstructions,
843
+ ]
844
+ .map(value => value?.trim() ?? '')
845
+ .filter(Boolean)
846
+ .join('\n\n');
847
+ const requestAgent = extraInstructions
848
+ ? {
849
+ ...agent,
850
+ instructions: [agent.instructions, extraInstructions]
851
+ .map(value => value?.trim() ?? '')
852
+ .filter(Boolean)
853
+ .join('\n\n'),
854
+ }
855
+ : agent;
856
+ const { instructions, prompt, images } = await buildCodexInput(messages, requestAgent, imageWriter);
379
857
  const hasInstructions = instructions.trim().length > 0;
380
858
  const instructionsPath = path_1.default.join(tempDir, 'instructions.md');
381
859
  const lastMessagePath = path_1.default.join(tempDir, 'last-message.json');
382
- const schemaPath = settings?.json_schema?.schema ? path_1.default.join(tempDir, 'schema.json') : undefined;
860
+ const schemaPath = requestJsonSchema?.schema ? path_1.default.join(tempDir, 'schema.json') : undefined;
383
861
  try {
384
862
  if (hasInstructions) {
385
863
  await (0, promises_1.writeFile)(instructionsPath, instructions, 'utf8');
386
864
  }
387
865
  if (schemaPath) {
388
- await (0, promises_1.writeFile)(schemaPath, JSON.stringify(settings.json_schema.schema, null, 2), 'utf8');
866
+ await (0, promises_1.writeFile)(schemaPath, JSON.stringify(requestJsonSchema.schema, null, 2), 'utf8');
389
867
  }
390
868
  const commandArgs = [
391
869
  'exec',
@@ -394,7 +872,7 @@ async function executeCodexRequest(messages, model, agent, requestId) {
394
872
  '--ignore-user-config',
395
873
  '--ignore-rules',
396
874
  '--skip-git-repo-check',
397
- ...disabledFeatureArgs(),
875
+ ...disabledFeatureArgs({ allowShellTool: options.allowShellTool }),
398
876
  '-m',
399
877
  codexModel,
400
878
  '-c',
@@ -402,19 +880,21 @@ async function executeCodexRequest(messages, model, agent, requestId) {
402
880
  ...(hasInstructions ? ['-c', `model_instructions_file=${JSON.stringify(instructionsPath)}`] : []),
403
881
  ...(schemaPath ? ['--output-schema', schemaPath] : []),
404
882
  ...(images.length > 0 ? ['--image', images.join(',')] : []),
883
+ ...(options.allowShellTool ? ['--sandbox', 'workspace-write', '--add-dir', tempDir] : []),
405
884
  '--output-last-message',
406
885
  lastMessagePath,
407
886
  '--cd',
408
887
  cwd,
409
888
  '-',
410
889
  ];
411
- const loggedRequestId = (0, llm_logger_js_1.log_llm_request)(agent.agent_id || 'default', 'codex', codexModel, {
890
+ const loggedRequestId = (0, llm_logger_js_1.log_llm_request)(agent.agent_id || 'default', 'codex', model, {
412
891
  command: 'codex',
413
892
  args: commandArgs,
414
893
  cwd,
415
894
  prompt,
416
895
  images,
417
- schema: settings?.json_schema?.schema,
896
+ codex_model: codexModel,
897
+ schema: requestJsonSchema?.schema,
418
898
  }, new Date(), requestId, agent.tags);
419
899
  let codexExecDiagnostics;
420
900
  try {
@@ -429,7 +909,9 @@ async function executeCodexRequest(messages, model, agent, requestId) {
429
909
  abortSignal: agent.abortSignal,
430
910
  });
431
911
  codexExecDiagnostics = codexExecResult.diagnostics;
432
- const usage = codexUsageFromJsonl(codexExecResult.stdout, codexModel, loggedRequestId);
912
+ const usage = codexUsageFromJsonl(codexExecResult.stdout, model, loggedRequestId, {
913
+ codex_cli_model: codexModel,
914
+ });
433
915
  if (usage)
434
916
  cost_tracker_js_1.costTracker.addUsage(usage);
435
917
  }
@@ -456,9 +938,58 @@ async function executeCodexRequest(messages, model, agent, requestId) {
456
938
  throw error;
457
939
  }
458
940
  finally {
941
+ await filesystemToolBroker?.stop();
459
942
  await (0, promises_1.rm)(tempDir, { recursive: true, force: true });
460
943
  }
461
944
  }
945
+ async function* executeCodexToolRequest(messages, model, agent, tools, requestId) {
946
+ if (agent.modelSettings?.json_schema?.schema) {
947
+ throw new Error('Codex provider cannot combine simulated tool calls with a caller-supplied json_schema.');
948
+ }
949
+ const content = await executeCodexRequest(messages, model, agent, requestId, {
950
+ extraInstructions: buildCodexToolInstructions(tools, agent),
951
+ jsonSchema: createCodexToolOutputSchema(tools, agent),
952
+ });
953
+ const action = parseCodexToolAction(content, tools, agent.modelSettings, getAvailableTerminalToolNames(tools, agent));
954
+ if (action.action === 'final_response') {
955
+ const messageId = `codex-${Date.now()}`;
956
+ yield {
957
+ type: 'message_complete',
958
+ content: action.content,
959
+ message_id: messageId,
960
+ };
961
+ return;
962
+ }
963
+ for (const toolCall of action.toolCalls) {
964
+ yield {
965
+ type: 'tool_start',
966
+ tool_call: toolCall,
967
+ };
968
+ }
969
+ }
970
+ async function* executeCodexFilesystemToolRequest(messages, model, agent, tools, requestId) {
971
+ if (agent.modelSettings?.json_schema?.schema) {
972
+ throw new Error('Codex provider cannot combine filesystem tool mode with a caller-supplied json_schema.');
973
+ }
974
+ const content = await executeCodexRequest(messages, model, agent, requestId, {
975
+ allowShellTool: true,
976
+ filesystemTools: tools,
977
+ useTempCwd: true,
978
+ });
979
+ const messageId = `codex-${Date.now()}`;
980
+ if (content) {
981
+ yield {
982
+ type: 'message_delta',
983
+ content,
984
+ message_id: messageId,
985
+ };
986
+ }
987
+ yield {
988
+ type: 'message_complete',
989
+ content,
990
+ message_id: messageId,
991
+ };
992
+ }
462
993
  function buildCodexImagePrompt(prompt, opts = {}) {
463
994
  const count = opts.n && opts.n > 0 ? Math.floor(opts.n) : 1;
464
995
  const details = [
@@ -541,7 +1072,7 @@ async function executeCodexImageGeneration(prompt, model, agent, opts = {}) {
541
1072
  '--skip-git-repo-check',
542
1073
  '--enable',
543
1074
  'image_generation',
544
- ...disabledFeatureArgs(true),
1075
+ ...disabledFeatureArgs({ allowImageGeneration: true }),
545
1076
  ...(promptModelAttempt
546
1077
  ? [
547
1078
  '-m',
@@ -590,8 +1121,11 @@ async function executeCodexImageGeneration(prompt, model, agent, opts = {}) {
590
1121
  });
591
1122
  throw error;
592
1123
  }
593
- const rawLastMessage = await (0, promises_1.readFile)(lastMessagePath, 'utf8');
594
- const responseImagePaths = await (0, codex_assets_js_1.extractExistingCodexImagePaths)(rawLastMessage, outputDir);
1124
+ const rawLastMessage = await readOptionalUtf8File(lastMessagePath);
1125
+ const lastMessageContent = rawLastMessage ?? '';
1126
+ const responseImagePaths = rawLastMessage
1127
+ ? await (0, codex_assets_js_1.extractExistingCodexImagePaths)(rawLastMessage, outputDir)
1128
+ : [];
595
1129
  const outputImagePaths = await (0, codex_assets_js_1.newestFirst)(await (0, codex_assets_js_1.listCodexOutputImages)(outputDir));
596
1130
  const generatedImagePaths = await (0, codex_assets_js_1.newestFirst)(await (0, codex_assets_js_1.listCodexGeneratedImages)(isolatedCodexHome));
597
1131
  const selectedImagePaths = [];
@@ -603,8 +1137,9 @@ async function executeCodexImageGeneration(prompt, model, agent, opts = {}) {
603
1137
  break;
604
1138
  }
605
1139
  if (selectedImagePaths.length < expectedImageCount) {
606
- const lastMessage = rawLastMessage.trim();
607
- throw new Error(`Codex image generation resolved ${selectedImagePaths.length} image artifact${selectedImagePaths.length === 1 ? '' : 's'}, expected ${expectedImageCount}.${lastMessage ? ` Last message: ${lastMessage}` : ''}`);
1140
+ const lastMessage = lastMessageContent.trim();
1141
+ const lastMessageNote = rawLastMessage === null ? ' Codex CLI did not write --output-last-message.' : '';
1142
+ throw new Error(`Codex image generation resolved ${selectedImagePaths.length} image artifact${selectedImagePaths.length === 1 ? '' : 's'}, expected ${expectedImageCount}.${lastMessageNote}${lastMessage ? ` Last message: ${lastMessage}` : ''}`);
608
1143
  }
609
1144
  const generatedImages = await (0, codex_assets_js_1.readCodexImageFiles)(selectedImagePaths);
610
1145
  (0, llm_logger_js_1.log_llm_response)(loggedRequestId, {
@@ -613,7 +1148,8 @@ async function executeCodexImageGeneration(prompt, model, agent, opts = {}) {
613
1148
  response_image_paths: responseImagePaths,
614
1149
  output_image_paths: outputImagePaths,
615
1150
  codex_home_image_paths: generatedImagePaths,
616
- last_message: rawLastMessage.trim(),
1151
+ last_message: lastMessageContent.trim(),
1152
+ last_message_missing: rawLastMessage === null,
617
1153
  codex_exec: codexExecDiagnostics,
618
1154
  });
619
1155
  cost_tracker_js_1.costTracker.addUsage({
@@ -653,6 +1189,16 @@ class CodexProvider extends base_provider_js_1.BaseModelProvider {
653
1189
  }
654
1190
  async *createResponseStream(messages, model, agent, requestId) {
655
1191
  try {
1192
+ const { getToolsFromAgent } = await Promise.resolve().then(() => __importStar(require("../utils/agent.cjs")));
1193
+ const tools = agent ? await getToolsFromAgent(agent) : [];
1194
+ if (tools.length > 0) {
1195
+ if (getCodexProviderSettings(agent.modelSettings)?.codex_tool_transport === 'filesystem') {
1196
+ yield* executeCodexFilesystemToolRequest(messages, model, agent, tools, requestId);
1197
+ return;
1198
+ }
1199
+ yield* executeCodexToolRequest(messages, model, agent, tools, requestId);
1200
+ return;
1201
+ }
656
1202
  const content = await executeCodexRequest(messages, model, agent, requestId);
657
1203
  const messageId = `codex-${Date.now()}`;
658
1204
  if (content) {