@zvada/agent-server 0.3.14 → 0.3.16
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/CHANGELOG.md +54 -0
- package/README.md +1 -0
- package/docs/consuming.md +24 -0
- package/docs/harnesses.md +60 -3
- package/package.json +1 -1
- package/src/client/client.ts +23 -0
- package/src/core/agents/base.ts +37 -0
- package/src/core/agents/claude-code/claude-agent.ts +28 -3
- package/src/core/agents/claude-code/generator-session.ts +11 -1
- package/src/core/agents/claude-code/models.ts +76 -0
- package/src/core/agents/codex-app-server/codex-app-server-agent.ts +158 -17
- package/src/core/agents/codex-app-server/models.ts +177 -0
- package/src/core/agents/codex-sdk/codex-sdk-agent.ts +51 -7
- package/src/core/agents/thinking-levels.ts +9 -0
- package/src/core/index.ts +4 -1
- package/src/core/provision/pins.ts +3 -0
- package/src/core/runtime/agent-runtime.ts +47 -7
- package/src/core/runtime/event-processor.ts +34 -6
- package/src/core/runtime/model-lists.ts +204 -0
- package/src/protocol/execution.ts +8 -2
- package/src/protocol/model-catalog.ts +220 -0
- package/src/protocol/models.ts +105 -31
- package/src/protocol/thinking.ts +42 -19
- package/src/protocol/wire.ts +24 -1
- package/src/server/agent-server.ts +17 -0
|
@@ -4,14 +4,17 @@ import { isAbsolute, resolve } from "node:path";
|
|
|
4
4
|
import type {
|
|
5
5
|
AgentCapabilities,
|
|
6
6
|
AgentInput,
|
|
7
|
+
ModelDescriptor,
|
|
7
8
|
PermissionMode,
|
|
8
9
|
PermissionToolCall,
|
|
10
|
+
ThinkingLevel,
|
|
9
11
|
} from "../../../protocol/index.ts";
|
|
10
|
-
import { AsyncQueue, codexReasoningEffort } from "../../../protocol/index.ts";
|
|
12
|
+
import { AsyncQueue, codexReasoningEffort, modelThinkingLevels } from "../../../protocol/index.ts";
|
|
11
13
|
import { SessionResumeError } from "../../utils/errors.ts";
|
|
12
14
|
import type {
|
|
13
15
|
AgentExecuteOptions,
|
|
14
16
|
CancelResult,
|
|
17
|
+
ListModelsOptions,
|
|
15
18
|
PermissionRequestHandler,
|
|
16
19
|
RawAgentEvent,
|
|
17
20
|
} from "../base.ts";
|
|
@@ -30,8 +33,19 @@ import {
|
|
|
30
33
|
codexMcpServers,
|
|
31
34
|
writeCodexMcpServers,
|
|
32
35
|
} from "./mcp.ts";
|
|
36
|
+
import {
|
|
37
|
+
type CodexModelThinking,
|
|
38
|
+
codexModelDescriptors,
|
|
39
|
+
codexModelThinking,
|
|
40
|
+
codexThinkingLevel,
|
|
41
|
+
listCodexModels,
|
|
42
|
+
readCodexListing,
|
|
43
|
+
readCodexModels,
|
|
44
|
+
} from "./models.ts";
|
|
33
45
|
import { CodexTurnSubagents } from "./subagents.ts";
|
|
34
46
|
|
|
47
|
+
export type { CodexModelThinking } from "./models.ts";
|
|
48
|
+
|
|
35
49
|
const CAPABILITIES: AgentCapabilities = {
|
|
36
50
|
multiTurn: true,
|
|
37
51
|
sessionResume: true,
|
|
@@ -60,6 +74,14 @@ export interface AppServerSession {
|
|
|
60
74
|
/** Credentials and permission broker belong only to the current execute. */
|
|
61
75
|
turn?: AppServerTurn;
|
|
62
76
|
idleTimer?: ReturnType<typeof setTimeout>;
|
|
77
|
+
/** The thread's model, as Codex resolved it (config.toml included). */
|
|
78
|
+
model?: string;
|
|
79
|
+
/** The model Codex reported for the thread, until a turn selects another. */
|
|
80
|
+
reportedModel?: string;
|
|
81
|
+
/** The thread's reasoning effort: Codex's own, then the last one a turn sent (they persist). */
|
|
82
|
+
effort?: string;
|
|
83
|
+
/** What each model accepts, from this process's `model/list`; read once. */
|
|
84
|
+
modelEfforts?: Promise<ReadonlyMap<string, CodexModelThinking> | undefined>;
|
|
63
85
|
}
|
|
64
86
|
|
|
65
87
|
interface AppServerTurn {
|
|
@@ -187,6 +209,17 @@ export interface CodexChatGptAuth {
|
|
|
187
209
|
|
|
188
210
|
export type CodexAppServerAuth = { type: "apiKey"; apiKey: string } | CodexChatGptAuth;
|
|
189
211
|
|
|
212
|
+
function loginParams(auth: CodexAppServerAuth): Record<string, unknown> {
|
|
213
|
+
return auth.type === "apiKey"
|
|
214
|
+
? { type: "apiKey", apiKey: auth.apiKey }
|
|
215
|
+
: {
|
|
216
|
+
type: "chatgptAuthTokens",
|
|
217
|
+
accessToken: auth.tokens.accessToken,
|
|
218
|
+
chatgptAccountId: auth.tokens.chatgptAccountId,
|
|
219
|
+
chatgptPlanType: auth.tokens.chatgptPlanType ?? null,
|
|
220
|
+
};
|
|
221
|
+
}
|
|
222
|
+
|
|
190
223
|
function authFingerprint(auth: CodexAppServerAuth | undefined): string | undefined {
|
|
191
224
|
if (!auth) return undefined;
|
|
192
225
|
return configFingerprint(
|
|
@@ -225,6 +258,23 @@ export function appServerNotificationEndsTurn(notification: CodexNotification):
|
|
|
225
258
|
);
|
|
226
259
|
}
|
|
227
260
|
|
|
261
|
+
/**
|
|
262
|
+
* The thinking levels each model the logged-in account can run accepts, from
|
|
263
|
+
* `model/list`. `undefined` when the CLI cannot answer; callers fall back to
|
|
264
|
+
* `MODEL_CATALOG`.
|
|
265
|
+
*/
|
|
266
|
+
export async function listModelThinking(
|
|
267
|
+
client: Pick<CodexAppServerClient, "request">,
|
|
268
|
+
): Promise<ReadonlyMap<string, CodexModelThinking> | undefined> {
|
|
269
|
+
try {
|
|
270
|
+
const thinking = codexModelThinking(await readCodexModels(client));
|
|
271
|
+
return thinking.size ? thinking : undefined;
|
|
272
|
+
} catch {
|
|
273
|
+
// An older CLI, a slow answer or a closed process: the catalog decides.
|
|
274
|
+
return undefined;
|
|
275
|
+
}
|
|
276
|
+
}
|
|
277
|
+
|
|
228
278
|
/**
|
|
229
279
|
* Codex harness backed by a long-lived `codex app-server` subprocess speaking
|
|
230
280
|
* JSON-RPC. Unlike the SDK harness this streams token-level deltas. A client +
|
|
@@ -249,6 +299,11 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
249
299
|
super();
|
|
250
300
|
}
|
|
251
301
|
|
|
302
|
+
/** With host-managed auth, each session may list as a different account. */
|
|
303
|
+
get modelsPerSession(): boolean {
|
|
304
|
+
return Boolean(this.agentOptions.resolveAuth);
|
|
305
|
+
}
|
|
306
|
+
|
|
252
307
|
private async resolveSession(
|
|
253
308
|
options: AgentExecuteOptions,
|
|
254
309
|
turn: AppServerTurn,
|
|
@@ -359,7 +414,7 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
359
414
|
cause instanceof Error ? cause.message : String(cause),
|
|
360
415
|
{ cause },
|
|
361
416
|
);
|
|
362
|
-
})) as { thread?: { id?: string } };
|
|
417
|
+
})) as { thread?: { id?: string }; model?: unknown; reasoningEffort?: unknown };
|
|
363
418
|
const threadId = result?.thread?.id;
|
|
364
419
|
if (resumeThreadId && threadId !== resumeThreadId) {
|
|
365
420
|
throw new SessionResumeError(
|
|
@@ -370,6 +425,9 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
370
425
|
if (!threadId) throw new Error("codex app-server thread/start returned no thread id");
|
|
371
426
|
signal.throwIfAborted();
|
|
372
427
|
session.threadId = threadId;
|
|
428
|
+
session.model = typeof result.model === "string" ? result.model : options.model;
|
|
429
|
+
if (typeof result.model === "string") session.reportedModel = result.model;
|
|
430
|
+
if (typeof result.reasoningEffort === "string") session.effort = result.reasoningEffort;
|
|
373
431
|
this.sessions.set(options.sessionId, session);
|
|
374
432
|
return session;
|
|
375
433
|
} catch (error) {
|
|
@@ -422,22 +480,61 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
422
480
|
session.mcpFingerprint = fingerprint;
|
|
423
481
|
}
|
|
424
482
|
|
|
483
|
+
/** What the thread's model accepts: live from this process, else what the engine knows. */
|
|
484
|
+
private async modelThinking(
|
|
485
|
+
session: AppServerSession,
|
|
486
|
+
known?: readonly ThinkingLevel[],
|
|
487
|
+
): Promise<CodexModelThinking | undefined> {
|
|
488
|
+
if (!session.model) return undefined;
|
|
489
|
+
session.modelEfforts ??= listModelThinking(session.client);
|
|
490
|
+
const live = (await session.modelEfforts)?.get(session.model);
|
|
491
|
+
if (live) return live;
|
|
492
|
+
const levels = known ?? modelThinkingLevels("codex-app-server", session.model);
|
|
493
|
+
return levels && { levels };
|
|
494
|
+
}
|
|
495
|
+
|
|
496
|
+
/**
|
|
497
|
+
* The models Codex offers this login. Reuses the session's process when
|
|
498
|
+
* `sessionId` has one started with the same `env` (its `CODEX_HOME` decides
|
|
499
|
+
* the login and config); otherwise starts `codex app-server` just to ask,
|
|
500
|
+
* with the account `resolveAuth` selects for that session.
|
|
501
|
+
*/
|
|
502
|
+
async listModels(options: ListModelsOptions): Promise<ModelDescriptor[]> {
|
|
503
|
+
const live = options.sessionId ? this.sessions.get(options.sessionId) : undefined;
|
|
504
|
+
if (live && !live.client.closed && live.envFingerprint === configFingerprint(options.env)) {
|
|
505
|
+
const listing = await readCodexListing(live.client, options.cwd);
|
|
506
|
+
return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
|
|
507
|
+
}
|
|
508
|
+
const auth =
|
|
509
|
+
options.sessionId && this.agentOptions.resolveAuth
|
|
510
|
+
? await this.agentOptions.resolveAuth(options.sessionId)
|
|
511
|
+
: undefined;
|
|
512
|
+
options.signal?.throwIfAborted();
|
|
513
|
+
const listing = await listCodexModels(
|
|
514
|
+
{
|
|
515
|
+
codexPath:
|
|
516
|
+
(await this.agentOptions.resolveCliPath?.()) ?? process.env.CODEX_CLI_PATH ?? "codex",
|
|
517
|
+
cwd: options.cwd,
|
|
518
|
+
env: options.env ? ({ ...process.env, ...options.env } as NodeJS.ProcessEnv) : process.env,
|
|
519
|
+
...(auth?.type === "chatgptAuthTokens" && { experimentalApi: true }),
|
|
520
|
+
},
|
|
521
|
+
{
|
|
522
|
+
signal: options.signal,
|
|
523
|
+
...(this.agentOptions.createClient && { createClient: this.agentOptions.createClient }),
|
|
524
|
+
...(auth && {
|
|
525
|
+
login: async (client) => {
|
|
526
|
+
await client.request("account/login/start", loginParams(auth));
|
|
527
|
+
},
|
|
528
|
+
}),
|
|
529
|
+
},
|
|
530
|
+
);
|
|
531
|
+
return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
|
|
532
|
+
}
|
|
533
|
+
|
|
425
534
|
private async authenticate(session: AppServerSession): Promise<void> {
|
|
426
535
|
session.turn?.signal.throwIfAborted();
|
|
427
536
|
const auth = session.turn?.auth;
|
|
428
|
-
if (auth)
|
|
429
|
-
await session.client.request(
|
|
430
|
-
"account/login/start",
|
|
431
|
-
auth.type === "apiKey"
|
|
432
|
-
? { type: "apiKey", apiKey: auth.apiKey }
|
|
433
|
-
: {
|
|
434
|
-
type: "chatgptAuthTokens",
|
|
435
|
-
accessToken: auth.tokens.accessToken,
|
|
436
|
-
chatgptAccountId: auth.tokens.chatgptAccountId,
|
|
437
|
-
chatgptPlanType: auth.tokens.chatgptPlanType ?? null,
|
|
438
|
-
},
|
|
439
|
-
);
|
|
440
|
-
}
|
|
537
|
+
if (auth) await session.client.request("account/login/start", loginParams(auth));
|
|
441
538
|
session.turn?.signal.throwIfAborted();
|
|
442
539
|
}
|
|
443
540
|
|
|
@@ -714,8 +811,51 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
714
811
|
threadId,
|
|
715
812
|
input: toUserInput(input),
|
|
716
813
|
};
|
|
717
|
-
|
|
718
|
-
|
|
814
|
+
// A refused turn/start changes nothing on the thread.
|
|
815
|
+
const before = {
|
|
816
|
+
model: session.model,
|
|
817
|
+
reportedModel: session.reportedModel,
|
|
818
|
+
effort: session.effort,
|
|
819
|
+
};
|
|
820
|
+
const switched = Boolean(options.model) && options.model !== session.model;
|
|
821
|
+
if (options.model) {
|
|
822
|
+
turnParams.model = options.model;
|
|
823
|
+
session.model = options.model;
|
|
824
|
+
// A model this turn selects is requested, not reported.
|
|
825
|
+
if (options.model !== session.reportedModel) session.reportedModel = undefined;
|
|
826
|
+
}
|
|
827
|
+
if (options.thinkingLevel) {
|
|
828
|
+
const accepts = await this.modelThinking(
|
|
829
|
+
session,
|
|
830
|
+
options.model ? options.modelThinking : undefined,
|
|
831
|
+
);
|
|
832
|
+
turnParams.effort = codexReasoningEffort(
|
|
833
|
+
options.thinkingLevel,
|
|
834
|
+
accepts?.levels,
|
|
835
|
+
accepts?.off,
|
|
836
|
+
);
|
|
837
|
+
session.effort = turnParams.effort as string;
|
|
838
|
+
} else if (switched && session.effort) {
|
|
839
|
+
// The thread keeps its effort across a model switch. Fit it to the new
|
|
840
|
+
// model, so Codex is not asked for a level that model lacks.
|
|
841
|
+
const kept = codexThinkingLevel(session.effort);
|
|
842
|
+
const accepts = await this.modelThinking(session, options.modelThinking);
|
|
843
|
+
if (kept && accepts?.levels.length) {
|
|
844
|
+
const effort = codexReasoningEffort(kept, accepts.levels, accepts.off);
|
|
845
|
+
if (effort !== session.effort) {
|
|
846
|
+
turnParams.effort = effort;
|
|
847
|
+
session.effort = effort;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
}
|
|
851
|
+
// Codex runs the effort it is given, and overrides persist on the
|
|
852
|
+
// thread, so the thread's effort is the one this turn runs.
|
|
853
|
+
const applied = codexThinkingLevel(session.effort);
|
|
854
|
+
if (applied || session.reportedModel)
|
|
855
|
+
options.onExecution?.({
|
|
856
|
+
...(applied && { thinkingLevel: applied }),
|
|
857
|
+
...(session.reportedModel && { model: session.reportedModel }),
|
|
858
|
+
});
|
|
719
859
|
|
|
720
860
|
if (controller.signal.aborted) throw new Error("turn aborted");
|
|
721
861
|
if (this.invalidatedClients.has(client)) {
|
|
@@ -723,6 +863,7 @@ export class CodexAppServerAgent extends SessionAgent {
|
|
|
723
863
|
throw new Error("Codex process was invalidated before turn admission");
|
|
724
864
|
}
|
|
725
865
|
client.request("turn/start", turnParams).catch((err: unknown) => {
|
|
866
|
+
if (session) Object.assign(session, before);
|
|
726
867
|
if (controller.signal.aborted) {
|
|
727
868
|
if (turn.subagents) void finishManaged();
|
|
728
869
|
else forceCancel();
|
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
import type { AgentHarness, ModelDescriptor, ThinkingLevel } from "../../../protocol/index.ts";
|
|
2
|
+
import { isThinkingLevel } from "../../../protocol/index.ts";
|
|
3
|
+
import { knownThinkingLevels } from "../thinking-levels.ts";
|
|
4
|
+
import { CodexAppServerClient, type CodexAppServerClientOptions } from "./client.ts";
|
|
5
|
+
|
|
6
|
+
const MODEL_LIST_TIMEOUT_MS = 5_000;
|
|
7
|
+
|
|
8
|
+
/** One `model/list` entry, as far as agent-server reads it. */
|
|
9
|
+
export type CodexModel = Record<string, unknown>;
|
|
10
|
+
|
|
11
|
+
/** A model's thinking levels, and the native effort that `off` means for it. */
|
|
12
|
+
export interface CodexModelThinking {
|
|
13
|
+
levels: readonly ThinkingLevel[];
|
|
14
|
+
off?: "none" | "minimal";
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
/** The level a native Codex effort is: `none` and `minimal` are `off`. */
|
|
18
|
+
export function codexThinkingLevel(effort: unknown): ThinkingLevel | undefined {
|
|
19
|
+
if (effort === "none" || effort === "minimal") return "off";
|
|
20
|
+
return typeof effort === "string" && effort !== "off" && isThinkingLevel(effort)
|
|
21
|
+
? effort
|
|
22
|
+
: undefined;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function efforts(model: CodexModel): unknown[] {
|
|
26
|
+
return Array.isArray(model.supportedReasoningEfforts)
|
|
27
|
+
? (model.supportedReasoningEfforts as Array<{ reasoningEffort?: unknown }>).map(
|
|
28
|
+
(option) => option?.reasoningEffort,
|
|
29
|
+
)
|
|
30
|
+
: [];
|
|
31
|
+
}
|
|
32
|
+
|
|
33
|
+
/** Every page of `model/list`, hidden models included. Throws when the CLI cannot answer. */
|
|
34
|
+
export async function readCodexModels(
|
|
35
|
+
client: Pick<CodexAppServerClient, "request">,
|
|
36
|
+
): Promise<CodexModel[]> {
|
|
37
|
+
const models: CodexModel[] = [];
|
|
38
|
+
let cursor: string | undefined;
|
|
39
|
+
for (let page = 0; page < 10; page++) {
|
|
40
|
+
const result = (await client.request(
|
|
41
|
+
"model/list",
|
|
42
|
+
{ includeHidden: true, ...(cursor && { cursor }) },
|
|
43
|
+
MODEL_LIST_TIMEOUT_MS,
|
|
44
|
+
)) as { data?: unknown; nextCursor?: unknown } | undefined;
|
|
45
|
+
if (!Array.isArray(result?.data)) throw new Error("codex model/list returned no models");
|
|
46
|
+
models.push(...(result.data as CodexModel[]));
|
|
47
|
+
cursor = typeof result.nextCursor === "string" ? result.nextCursor : undefined;
|
|
48
|
+
if (!cursor) break;
|
|
49
|
+
}
|
|
50
|
+
return models;
|
|
51
|
+
}
|
|
52
|
+
|
|
53
|
+
/**
|
|
54
|
+
* What each model accepts, by its `model` and `id`. Codex's `none` or
|
|
55
|
+
* `minimal` is `off`, remembered so `off` sends the value the model
|
|
56
|
+
* advertised (`none` when it offers both).
|
|
57
|
+
*/
|
|
58
|
+
export function codexModelThinking(models: readonly CodexModel[]): Map<string, CodexModelThinking> {
|
|
59
|
+
const thinking = new Map<string, CodexModelThinking>();
|
|
60
|
+
for (const model of models) {
|
|
61
|
+
const native = efforts(model);
|
|
62
|
+
const off = native.includes("none")
|
|
63
|
+
? "none"
|
|
64
|
+
: native.includes("minimal")
|
|
65
|
+
? "minimal"
|
|
66
|
+
: undefined;
|
|
67
|
+
const levels = knownThinkingLevels(native.map(codexThinkingLevel));
|
|
68
|
+
if (!levels.length) continue;
|
|
69
|
+
for (const name of [model.model, model.id])
|
|
70
|
+
if (typeof name === "string") thinking.set(name, { levels, ...(off && { off }) });
|
|
71
|
+
}
|
|
72
|
+
return thinking;
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* The model a new thread in `cwd` runs when the turn names none, when Codex's
|
|
77
|
+
* configuration (`~/.codex/config.toml`, a project layer) selects one. Codex's
|
|
78
|
+
* own `isDefault` ignores it.
|
|
79
|
+
*/
|
|
80
|
+
export async function readCodexConfiguredModel(
|
|
81
|
+
client: Pick<CodexAppServerClient, "request">,
|
|
82
|
+
cwd: string,
|
|
83
|
+
): Promise<string | undefined> {
|
|
84
|
+
try {
|
|
85
|
+
const result = (await client.request("config/read", { cwd }, MODEL_LIST_TIMEOUT_MS)) as
|
|
86
|
+
| { config?: { model?: unknown } }
|
|
87
|
+
| undefined;
|
|
88
|
+
return typeof result?.config?.model === "string" ? result.config.model : undefined;
|
|
89
|
+
} catch {
|
|
90
|
+
return undefined;
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
/**
|
|
95
|
+
* The models Codex offers in a picker, in the shared shape. Hidden models are
|
|
96
|
+
* left out. `configuredModel` (see `readCodexConfiguredModel`) is the default
|
|
97
|
+
* when given; otherwise Codex's own default is.
|
|
98
|
+
*/
|
|
99
|
+
export function codexModelDescriptors(
|
|
100
|
+
models: readonly CodexModel[],
|
|
101
|
+
harness: Extract<AgentHarness, "codex-sdk" | "codex-app-server">,
|
|
102
|
+
configuredModel?: string,
|
|
103
|
+
): ModelDescriptor[] {
|
|
104
|
+
return models.flatMap((model) => {
|
|
105
|
+
const id = typeof model.model === "string" ? model.model : model.id;
|
|
106
|
+
if (typeof id !== "string" || model.hidden === true) return [];
|
|
107
|
+
const levels = knownThinkingLevels(efforts(model).map(codexThinkingLevel));
|
|
108
|
+
const fallback = codexThinkingLevel(model.defaultReasoningEffort);
|
|
109
|
+
return [
|
|
110
|
+
{
|
|
111
|
+
id,
|
|
112
|
+
harness,
|
|
113
|
+
label: typeof model.displayName === "string" && model.displayName ? model.displayName : id,
|
|
114
|
+
...(typeof model.description === "string" &&
|
|
115
|
+
model.description && { description: model.description }),
|
|
116
|
+
...((configuredModel ? id === configuredModel : model.isDefault === true) && {
|
|
117
|
+
isDefault: true,
|
|
118
|
+
}),
|
|
119
|
+
thinking: {
|
|
120
|
+
levels,
|
|
121
|
+
...(fallback && levels.includes(fallback) && { default: fallback }),
|
|
122
|
+
},
|
|
123
|
+
...(Array.isArray(model.inputModalities) && {
|
|
124
|
+
images: model.inputModalities.includes("image"),
|
|
125
|
+
}),
|
|
126
|
+
},
|
|
127
|
+
];
|
|
128
|
+
});
|
|
129
|
+
}
|
|
130
|
+
|
|
131
|
+
/** What one `codex app-server` says about its models. */
|
|
132
|
+
export interface CodexModelListing {
|
|
133
|
+
models: CodexModel[];
|
|
134
|
+
/** The model new threads in the listing's folder run by default (see `readCodexConfiguredModel`). */
|
|
135
|
+
configuredModel?: string;
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** Read the models and the configured default from a running app-server. */
|
|
139
|
+
export async function readCodexListing(
|
|
140
|
+
client: Pick<CodexAppServerClient, "request">,
|
|
141
|
+
cwd: string,
|
|
142
|
+
): Promise<CodexModelListing> {
|
|
143
|
+
const models = await readCodexModels(client);
|
|
144
|
+
const configuredModel = await readCodexConfiguredModel(client, cwd);
|
|
145
|
+
return { models, ...(configuredModel && { configuredModel }) };
|
|
146
|
+
}
|
|
147
|
+
|
|
148
|
+
/**
|
|
149
|
+
* Start `codex app-server` only to list its models, for the login its
|
|
150
|
+
* environment selects (or `login` performs). No thread is started.
|
|
151
|
+
*/
|
|
152
|
+
export async function listCodexModels(
|
|
153
|
+
options: CodexAppServerClientOptions & { cwd: string },
|
|
154
|
+
{
|
|
155
|
+
signal,
|
|
156
|
+
createClient = (clientOptions) => new CodexAppServerClient(clientOptions),
|
|
157
|
+
login,
|
|
158
|
+
}: {
|
|
159
|
+
signal?: AbortSignal;
|
|
160
|
+
createClient?: (options: CodexAppServerClientOptions) => CodexAppServerClient;
|
|
161
|
+
login?: (client: CodexAppServerClient) => Promise<void>;
|
|
162
|
+
} = {},
|
|
163
|
+
): Promise<CodexModelListing> {
|
|
164
|
+
signal?.throwIfAborted();
|
|
165
|
+
const client = createClient(options);
|
|
166
|
+
const stop = () => client.close();
|
|
167
|
+
signal?.addEventListener("abort", stop, { once: true });
|
|
168
|
+
try {
|
|
169
|
+
await client.initialize();
|
|
170
|
+
await login?.(client);
|
|
171
|
+
signal?.throwIfAborted();
|
|
172
|
+
return await readCodexListing(client, options.cwd);
|
|
173
|
+
} finally {
|
|
174
|
+
signal?.removeEventListener("abort", stop);
|
|
175
|
+
client.close();
|
|
176
|
+
}
|
|
177
|
+
}
|
|
@@ -1,8 +1,18 @@
|
|
|
1
1
|
import type { Codex, Thread, ThreadEvent, ThreadOptions } from "@openai/codex-sdk";
|
|
2
|
-
import type {
|
|
3
|
-
|
|
2
|
+
import type {
|
|
3
|
+
AgentCapabilities,
|
|
4
|
+
AgentInput,
|
|
5
|
+
ModelDescriptor,
|
|
6
|
+
PermissionMode,
|
|
7
|
+
} from "../../../protocol/index.ts";
|
|
8
|
+
import { codexReasoningEffort, modelThinkingLevels } from "../../../protocol/index.ts";
|
|
4
9
|
import { SessionResumeError } from "../../utils/errors.ts";
|
|
5
|
-
import type { AgentExecuteOptions, RawAgentEvent } from "../base.ts";
|
|
10
|
+
import type { AgentExecuteOptions, ListModelsOptions, RawAgentEvent } from "../base.ts";
|
|
11
|
+
import {
|
|
12
|
+
codexModelDescriptors,
|
|
13
|
+
codexThinkingLevel,
|
|
14
|
+
listCodexModels,
|
|
15
|
+
} from "../codex-app-server/models.ts";
|
|
6
16
|
import { configFingerprint } from "../config-fingerprint.ts";
|
|
7
17
|
import { SessionAgent } from "../session-agent.ts";
|
|
8
18
|
|
|
@@ -23,6 +33,8 @@ export interface LiveThread {
|
|
|
23
33
|
codexPathOverride?: string;
|
|
24
34
|
model?: string;
|
|
25
35
|
thinkingLevel?: string;
|
|
36
|
+
/** The effort the thread was started with; `codex exec` keeps it for the thread's life. */
|
|
37
|
+
effort?: string;
|
|
26
38
|
cwd: string;
|
|
27
39
|
permissionMode?: PermissionMode;
|
|
28
40
|
credentialsFingerprint: string;
|
|
@@ -101,7 +113,8 @@ function toCodexInput(input: AgentInput): string {
|
|
|
101
113
|
.join("\n\n");
|
|
102
114
|
}
|
|
103
115
|
|
|
104
|
-
|
|
116
|
+
/** `codex exec` options for a turn. Exported for tests. */
|
|
117
|
+
export function threadOptions(options: AgentExecuteOptions): ThreadOptions {
|
|
105
118
|
const opts: ThreadOptions = {
|
|
106
119
|
workingDirectory: options.cwd,
|
|
107
120
|
skipGitRepoCheck: true,
|
|
@@ -112,8 +125,15 @@ function threadOptions(options: AgentExecuteOptions): ThreadOptions {
|
|
|
112
125
|
: {}),
|
|
113
126
|
};
|
|
114
127
|
if (options.model) opts.model = options.model;
|
|
128
|
+
// `codex exec` cannot list models: the engine's list (or the catalog) says
|
|
129
|
+
// what a model accepts.
|
|
115
130
|
if (options.thinkingLevel)
|
|
116
|
-
opts.modelReasoningEffort = codexReasoningEffort(
|
|
131
|
+
opts.modelReasoningEffort = codexReasoningEffort(
|
|
132
|
+
options.thinkingLevel,
|
|
133
|
+
options.modelThinking ??
|
|
134
|
+
(options.model ? modelThinkingLevels("codex-sdk", options.model) : undefined),
|
|
135
|
+
"minimal",
|
|
136
|
+
);
|
|
117
137
|
return opts;
|
|
118
138
|
}
|
|
119
139
|
|
|
@@ -186,14 +206,16 @@ export class CodexSdkAgent extends SessionAgent {
|
|
|
186
206
|
if (signal?.aborted) throw new Error("turn aborted");
|
|
187
207
|
const fingerprint = credentialsFingerprint(options);
|
|
188
208
|
const resumeThreadId = options.resumeSessionId;
|
|
209
|
+
const settings = threadOptions(options);
|
|
189
210
|
const thread = resumeThreadId
|
|
190
|
-
? codex.resumeThread(resumeThreadId,
|
|
191
|
-
: codex.startThread(
|
|
211
|
+
? codex.resumeThread(resumeThreadId, settings)
|
|
212
|
+
: codex.startThread(settings);
|
|
192
213
|
const live: LiveThread = {
|
|
193
214
|
thread,
|
|
194
215
|
codexPathOverride,
|
|
195
216
|
model: options.model,
|
|
196
217
|
thinkingLevel: effort,
|
|
218
|
+
effort: settings.modelReasoningEffort,
|
|
197
219
|
cwd: options.cwd,
|
|
198
220
|
permissionMode: options.permissionMode,
|
|
199
221
|
credentialsFingerprint: fingerprint,
|
|
@@ -212,6 +234,9 @@ export class CodexSdkAgent extends SessionAgent {
|
|
|
212
234
|
controller.signal.throwIfAborted();
|
|
213
235
|
const live = await this.resolveThread(options, controller.signal);
|
|
214
236
|
controller.signal.throwIfAborted();
|
|
237
|
+
// Codex runs the effort it is given.
|
|
238
|
+
const applied = codexThinkingLevel(live.effort);
|
|
239
|
+
if (applied) options.onExecution?.({ thinkingLevel: applied });
|
|
215
240
|
let reported = false;
|
|
216
241
|
const { events } = await live.thread.runStreamed(toCodexInput(input), {
|
|
217
242
|
signal: controller.signal,
|
|
@@ -249,6 +274,25 @@ export class CodexSdkAgent extends SessionAgent {
|
|
|
249
274
|
}
|
|
250
275
|
}
|
|
251
276
|
|
|
277
|
+
/**
|
|
278
|
+
* The models Codex offers the CLI's stored login (its built-in list when
|
|
279
|
+
* logged out), from `codex app-server`: `codex exec` cannot list them, and
|
|
280
|
+
* the app-server ignores the `CODEX_API_KEY` a turn's `apiKey` becomes.
|
|
281
|
+
*/
|
|
282
|
+
async listModels(options: ListModelsOptions): Promise<ModelDescriptor[]> {
|
|
283
|
+
const codexPath =
|
|
284
|
+
(await this.agentOptions.resolveCliPath?.()) ?? process.env.CODEX_CLI_PATH ?? "codex";
|
|
285
|
+
const listing = await listCodexModels(
|
|
286
|
+
{
|
|
287
|
+
codexPath,
|
|
288
|
+
cwd: options.cwd,
|
|
289
|
+
env: options.env ? ({ ...process.env, ...options.env } as NodeJS.ProcessEnv) : process.env,
|
|
290
|
+
},
|
|
291
|
+
{ signal: options.signal },
|
|
292
|
+
);
|
|
293
|
+
return codexModelDescriptors(listing.models, this.harness, listing.configuredModel);
|
|
294
|
+
}
|
|
295
|
+
|
|
252
296
|
override async release(sessionId: string): Promise<void> {
|
|
253
297
|
await super.release(sessionId);
|
|
254
298
|
this.threads.delete(sessionId);
|
|
@@ -0,0 +1,9 @@
|
|
|
1
|
+
import { THINKING_LEVELS, type ThinkingLevel, isThinkingLevel } from "../../protocol/index.ts";
|
|
2
|
+
|
|
3
|
+
/** The values that are thinking levels, once each, lowest first. */
|
|
4
|
+
export function knownThinkingLevels(values: Iterable<unknown>): ThinkingLevel[] {
|
|
5
|
+
const levels = new Set<ThinkingLevel>();
|
|
6
|
+
for (const value of values)
|
|
7
|
+
if (typeof value === "string" && isThinkingLevel(value)) levels.add(value);
|
|
8
|
+
return THINKING_LEVELS.filter((level) => levels.has(level));
|
|
9
|
+
}
|
package/src/core/index.ts
CHANGED
|
@@ -9,13 +9,16 @@ export {
|
|
|
9
9
|
CollectingSink,
|
|
10
10
|
teeSink,
|
|
11
11
|
} from "./runtime/event-sink.ts";
|
|
12
|
-
export { EventProcessor } from "./runtime/event-processor.ts";
|
|
12
|
+
export { EventProcessor, endedExecution } from "./runtime/event-processor.ts";
|
|
13
|
+
export { type ModelsQuery, catalogModelList } from "./runtime/model-lists.ts";
|
|
13
14
|
|
|
14
15
|
// Agent abstraction
|
|
15
16
|
export {
|
|
16
17
|
type Agent,
|
|
17
18
|
type AgentExecuteOptions,
|
|
19
|
+
type AppliedExecution,
|
|
18
20
|
type CancelResult,
|
|
21
|
+
type ListModelsOptions,
|
|
19
22
|
type RawAgentEvent,
|
|
20
23
|
BaseAgent,
|
|
21
24
|
} from "./agents/base.ts";
|
|
@@ -25,6 +25,9 @@ export type CliTool = (typeof CLI_TOOLS)[number];
|
|
|
25
25
|
* pair). The pin governs downloads (compiled binaries / bare sandboxes /
|
|
26
26
|
* `pinned` mode); embedders wanting an exact codex-sdk pairing there can
|
|
27
27
|
* set `provision.pins.codex` to their SDK's vendored version.
|
|
28
|
+
*
|
|
29
|
+
* After any bump, run `bun run models:refresh`: the model catalog records the
|
|
30
|
+
* versions that listed it, and a test fails until it matches these pins.
|
|
28
31
|
*/
|
|
29
32
|
export const DEFAULT_PINS: Record<CliTool, string> = {
|
|
30
33
|
// 0.3.288 verified 2026-10-03: the paired CLI honors `--thinking-display
|