@theopenbee/engine-codex 0.1.1 → 0.2.0

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.
package/README.md CHANGED
@@ -46,6 +46,10 @@ Then enable the plugin, and pick `codex` as the default engine or per worker.
46
46
 
47
47
  The plugin checks the setting when it is enabled: it runs `codex --version` and shows the version, or the reason it failed (for example `codex not found on PATH`).
48
48
 
49
+ ## Model
50
+
51
+ The worker's **Model** field is passed to Codex as `--model <model>`, for new and resumed conversations, before the extra args. The suggested models come from `codex debug models`: the models Codex shows in its own model picker, in Codex's order and with their display names. If the engine args already set a model with `--model` or `-m`, they win and the Model field is ignored. Without a model, Codex uses its own default.
52
+
49
53
  ## Sessions
50
54
 
51
55
  Codex names its conversations (threads) itself, so the plugin keeps a small mapping from each openbee session id to the Codex thread id. The mapping files live in `~/.openbee/.codex/sessions/`, one file per session, holding only the thread id. openbee never deletes this directory, so conversations survive uninstalling the plugin and conversations that were started before the plugin was installed keep working.
@@ -59,13 +63,13 @@ Token usage is read from Codex's own session files, `~/.codex/sessions/` (or `$C
59
63
  New conversation, with the prompt on standard input:
60
64
 
61
65
  ```
62
- codex exec - --json --dangerously-bypass-approvals-and-sandbox [mcp -c options] [extra args]
66
+ codex exec - --json --dangerously-bypass-approvals-and-sandbox [mcp -c options] [--model <model>] [extra args]
63
67
  ```
64
68
 
65
69
  Later turns, with the prompt as the last argument:
66
70
 
67
71
  ```
68
- codex exec resume <thread id> --json --dangerously-bypass-approvals-and-sandbox <prompt> [mcp -c options] [extra args]
72
+ codex exec resume <thread id> --json --dangerously-bypass-approvals-and-sandbox <prompt> [mcp -c options] [--model <model>] [extra args]
69
73
  ```
70
74
 
71
75
  - When openbee provides its MCP server, the plugin adds `-c mcp_servers.<name>.url="<url>"` and `-c mcp_servers.<name>.bearer_token_env_var="<key variable>"`. Codex reads the API key from its environment, so the key never appears on the command line.
package/dist/codex.d.ts CHANGED
@@ -1,7 +1,8 @@
1
- import type { Engine, RunParams, TokenUsage } from "@theopenbee/engine-sdk";
1
+ import type { Engine, ModelInfo, RunParams, TokenUsage } from "@theopenbee/engine-sdk";
2
2
  type McpParams = NonNullable<RunParams["mcp"]>;
3
3
  export declare function goQuote(value: string): string;
4
4
  export declare function mcpArgs(mcp: McpParams | undefined): string[];
5
+ export declare function modelArgs(model: string | undefined, extraArgs: string[]): string[];
5
6
  export declare function buildArgs(threadId: string, resume: boolean, prompt: string, extraArgs: string[]): string[];
6
7
  export declare function threadIdFromLine(line: string): string;
7
8
  export declare function extractResultFromLog(logPath: string): Promise<string>;
@@ -16,5 +17,6 @@ export declare function codexBaseDir(env?: NodeJS.ProcessEnv, home?: string): st
16
17
  export declare function findCodexSessionFile(codexBase: string, threadId: string): string | undefined;
17
18
  export declare function collectUsage(store: SessionStore, codexBase: string, sessionIds: string[]): Promise<Record<string, TokenUsage[]>>;
18
19
  export declare function failureText(code: number | null, signal: NodeJS.Signals | null, stderrTail: string): string;
20
+ export declare function parseModels(output: string): ModelInfo[];
19
21
  export declare const engine: Engine;
20
22
  export {};
package/dist/codex.js CHANGED
@@ -1,8 +1,11 @@
1
+ import { execFile } from "node:child_process";
1
2
  import { createReadStream, mkdirSync, readFileSync, readdirSync, renameSync, rmSync, statSync, writeFileSync } from "node:fs";
2
3
  import { homedir } from "node:os";
3
4
  import { join } from "node:path";
4
5
  import { createInterface } from "node:readline";
5
6
  import { cliVersion, findCli, log, spawnCli } from "@theopenbee/engine-sdk";
7
+ const MODELS_TIMEOUT_MS = 20_000;
8
+ const MODELS_MAX_BUFFER = 64 * 1024 * 1024;
6
9
  function isObject(value) {
7
10
  return typeof value === "object" && value !== null && !Array.isArray(value);
8
11
  }
@@ -77,6 +80,15 @@ export function mcpArgs(mcp) {
77
80
  const prefix = `mcp_servers.${mcp.name}.`;
78
81
  return ["-c", `${prefix}url=${goQuote(mcp.url)}`, "-c", `${prefix}bearer_token_env_var=${goQuote(mcp.api_key_env)}`];
79
82
  }
83
+ export function modelArgs(model, extraArgs) {
84
+ if (typeof model !== "string" || model === "")
85
+ return [];
86
+ if (extraArgs.some((arg) => arg === "--model" || arg === "-m" || arg.startsWith("--model="))) {
87
+ log.warn(`engine args already set a model, ignoring model ${JSON.stringify(model)}`);
88
+ return [];
89
+ }
90
+ return ["--model", model];
91
+ }
80
92
  export function buildArgs(threadId, resume, prompt, extraArgs) {
81
93
  const base = resume && threadId !== ""
82
94
  ? ["exec", "resume", threadId, "--json", "--dangerously-bypass-approvals-and-sandbox"]
@@ -346,6 +358,47 @@ export function failureText(code, signal, stderrTail) {
346
358
  const detail = lastNonEmptyLine(stderrTail);
347
359
  return detail === "" ? base : `${base}: ${detail}`;
348
360
  }
361
+ export function parseModels(output) {
362
+ let catalog;
363
+ try {
364
+ catalog = JSON.parse(output);
365
+ }
366
+ catch (err) {
367
+ throw new Error(`codex debug models returned invalid JSON: ${errorMessage(err)}`);
368
+ }
369
+ if (!isObject(catalog) || !Array.isArray(catalog.models))
370
+ throw new Error("codex debug models returned no model list");
371
+ const models = [];
372
+ for (const entry of catalog.models) {
373
+ if (!isObject(entry) || entry.visibility !== "list")
374
+ continue;
375
+ const id = stringOrEmpty(entry.slug);
376
+ if (id === "")
377
+ continue;
378
+ const name = stringOrEmpty(entry.display_name);
379
+ models.push(name === "" ? { id } : { id, name });
380
+ }
381
+ return models;
382
+ }
383
+ function listModels(command) {
384
+ return new Promise((resolve, reject) => {
385
+ execFile(command, ["debug", "models"], { timeout: MODELS_TIMEOUT_MS, killSignal: "SIGKILL", maxBuffer: MODELS_MAX_BUFFER, windowsHide: true }, (err, stdout, stderr) => {
386
+ if (!err) {
387
+ resolve(String(stdout));
388
+ return;
389
+ }
390
+ const { code, killed, signal } = err;
391
+ if (typeof code === "number")
392
+ reject(new Error(failureText(code, null, String(stderr))));
393
+ else if (killed)
394
+ reject(new Error(`codex debug models timed out after ${MODELS_TIMEOUT_MS / 1000}s`));
395
+ else if (code === null)
396
+ reject(new Error(failureText(null, signal ?? null, String(stderr))));
397
+ else
398
+ reject(new Error(`codex debug models failed: ${errorMessage(err)}`));
399
+ });
400
+ });
401
+ }
349
402
  function resolveThread(store, sessionId, resume) {
350
403
  if (!resume)
351
404
  return { threadId: "", resume: false };
@@ -366,7 +419,12 @@ export const engine = {
366
419
  const command = findCli(req.config, "codex");
367
420
  const store = openSessionStore(join(req.state_dir, "sessions"));
368
421
  const thread = resolveThread(store, params.session_id, params.resume);
369
- const args = buildArgs(thread.threadId, thread.resume, params.prompt, [...mcpArgs(params.mcp), ...(params.extra_args ?? [])]);
422
+ const extraArgs = params.extra_args ?? [];
423
+ const args = buildArgs(thread.threadId, thread.resume, params.prompt, [
424
+ ...mcpArgs(params.mcp),
425
+ ...modelArgs(params.model, extraArgs),
426
+ ...extraArgs,
427
+ ]);
370
428
  let awaitingThread = !thread.resume;
371
429
  let outcome;
372
430
  try {
@@ -404,4 +462,7 @@ export const engine = {
404
462
  async usage(req) {
405
463
  return collectUsage(new SessionStore(join(req.state_dir, "sessions")), codexBaseDir(), req.params.session_ids ?? []);
406
464
  },
465
+ async models(req) {
466
+ return parseModels(await listModels(findCli(req.config, "codex")));
467
+ },
407
468
  };
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theopenbee/engine-codex",
3
- "version": "0.1.1",
3
+ "version": "0.2.0",
4
4
  "description": "Codex engine plugin for openbee",
5
5
  "keywords": [
6
6
  "openbee",
@@ -37,7 +37,7 @@
37
37
  "prepare": "npm run build"
38
38
  },
39
39
  "dependencies": {
40
- "@theopenbee/engine-sdk": "^0.1.0"
40
+ "@theopenbee/engine-sdk": "^0.2.0"
41
41
  },
42
42
  "devDependencies": {
43
43
  "@types/node": "^22.10.0",