@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.
@@ -1,44 +1,110 @@
1
- import type { AgentHarness } from "./harness.ts";
2
- import { THINKING_LEVELS, type ThinkingLevel } from "./thinking.ts";
1
+ import { z } from "zod";
2
+ import { type AgentHarness, AgentHarnessSchema } from "./harness.ts";
3
+ import { MetaSchema } from "./meta.ts";
4
+ import { GENERATED_MODEL_CATALOG } from "./model-catalog.ts";
5
+ import { type ThinkingLevel, ThinkingLevelSchema, isThinkingLevel } from "./thinking.ts";
6
+ import { EpochMsSchema } from "./time.ts";
3
7
 
4
8
  /**
5
- * Advisory catalog of known-good models per harness. `model` on a RunConfig is
6
- * a free-form string (harnesses pass it through), so unlisted models still work;
7
- * this catalog drives UIs/pickers and supplies sensible per-harness defaults.
9
+ * One model a harness can run, in the same shape for every harness. Claude
10
+ * fills it from `supportedModels()`, Codex from `model/list`. Pass `id` as
11
+ * `config.model`.
12
+ */
13
+ export const ModelDescriptorSchema = z.object({
14
+ id: z.string(),
15
+ harness: AgentHarnessSchema,
16
+ label: z.string(),
17
+ description: z.string().optional(),
18
+ /** The model an alias runs (Claude `opus` -> `claude-opus-5-5`), when it differs from `id`. */
19
+ resolvedId: z.string().optional(),
20
+ /** The model a turn runs when its config names none. */
21
+ isDefault: z.boolean().optional(),
22
+ thinking: z.object({
23
+ /** The levels the model runs as distinct settings, lowest first. Empty: no thinking control. */
24
+ levels: z.array(ThinkingLevelSchema),
25
+ /** The model's own default, when the harness states one. Never invented. */
26
+ default: ThinkingLevelSchema.optional(),
27
+ }),
28
+ /** Accepts image input. Absent: the harness does not say. */
29
+ images: z.boolean().optional(),
30
+ _meta: MetaSchema,
31
+ });
32
+ export type ModelDescriptor = z.infer<typeof ModelDescriptorSchema>;
33
+
34
+ /** The models a harness offers, as `models/list` and `AgentRuntime.models()` return them. */
35
+ export const ModelListSchema = z.object({
36
+ harness: AgentHarnessSchema,
37
+ models: z.array(ModelDescriptorSchema),
38
+ /**
39
+ * `live`: the harness's CLI listed them for this login and folder.
40
+ * `catalog`: agent-server's built-in list, generated from the pinned CLIs.
41
+ * Served when the harness cannot list models or its CLI did not answer.
42
+ */
43
+ source: z.enum(["live", "catalog"]),
44
+ /**
45
+ * Why the last read of the CLI failed (a logged-out CLI, a timeout). With
46
+ * `source: "catalog"` there is no live list yet; with `"live"` this is the
47
+ * previous list, from `fetchedAt`.
48
+ */
49
+ error: z.string().optional(),
50
+ /** When the list was read. */
51
+ fetchedAt: EpochMsSchema,
52
+ });
53
+ export type ModelList = z.infer<typeof ModelListSchema>;
54
+
55
+ /**
56
+ * Parse a model list from a server that may be newer than this build: a
57
+ * thinking level this build does not know is dropped from `levels` (and from
58
+ * `default`) instead of failing the whole list.
59
+ */
60
+ export function decodeModelList(value: unknown): ModelList {
61
+ const raw = value as { models?: unknown };
62
+ if (!Array.isArray(raw?.models)) return ModelListSchema.parse(value);
63
+ const models = raw.models.map((model: unknown) => {
64
+ const thinking = (model as { thinking?: { levels?: unknown; default?: unknown } })?.thinking;
65
+ if (!thinking || typeof thinking !== "object") return model;
66
+ const levels = Array.isArray(thinking.levels)
67
+ ? thinking.levels.filter((level) => typeof level === "string" && isThinkingLevel(level))
68
+ : thinking.levels;
69
+ const known = typeof thinking.default === "string" && isThinkingLevel(thinking.default);
70
+ const { default: _dropped, ...rest } = thinking;
71
+ return {
72
+ ...(model as object),
73
+ thinking: { ...rest, levels, ...(known && { default: thinking.default }) },
74
+ };
75
+ });
76
+ return ModelListSchema.parse({ ...(value as object), models });
77
+ }
78
+
79
+ /**
80
+ * Advisory catalog entry in the 0.3 shape. `model` on a RunConfig is a
81
+ * free-form string (harnesses pass it through), so unlisted models still work.
8
82
  */
9
83
  export interface ModelInfo {
10
84
  readonly id: string;
11
85
  readonly harness: AgentHarness;
12
86
  readonly label: string;
87
+ /** The levels the model runs as distinct settings, lowest first. Empty: no control. */
13
88
  readonly thinking: readonly ThinkingLevel[];
14
89
  }
15
90
 
16
- const ALL: readonly ThinkingLevel[] = THINKING_LEVELS;
17
-
18
- // As listed by Codex CLI 0.160.0. The GPT-6 family needs CLI 0.159 or newer.
19
- const CODEX_MODELS: ReadonlyArray<readonly [id: string, label: string]> = [
20
- ["gpt-6.1-sol", "GPT-6.1 Sol"],
21
- ["gpt-6-astra", "GPT-6 Astra"],
22
- ["gpt-6-sol", "GPT-6 Sol"],
23
- ["gpt-6-luna", "GPT-6 Luna"],
24
- ["gpt-5.5", "GPT-5.5"],
25
- ];
26
-
27
- export const MODEL_CATALOG: readonly ModelInfo[] = [
28
- // Claude (the claude CLI also accepts the bare aliases "sonnet"/"opus"/"haiku").
29
- { id: "sonnet", harness: "claude-code", label: "Claude Sonnet (latest)", thinking: ALL },
30
- { id: "opus", harness: "claude-code", label: "Claude Opus (latest)", thinking: ALL },
31
- { id: "haiku", harness: "claude-code", label: "Claude Haiku (latest)", thinking: ALL },
32
- // Codex (shared between codex-sdk and codex-app-server).
33
- ...CODEX_MODELS.flatMap(([id, label]) =>
34
- (["codex-sdk", "codex-app-server"] as const).map((harness) => ({
35
- id,
36
- harness,
37
- label,
38
- thinking: ALL,
39
- })),
40
- ),
41
- ];
91
+ /**
92
+ * The models each harness's pinned CLI listed when this release was built
93
+ * (`bun run models:refresh`), for a picker without a live list. The live list
94
+ * is the account's own: prefer `AgentRuntime.models()` / `client.listModels()`.
95
+ * `codex-sdk` runs the same CLI as `codex-app-server`.
96
+ */
97
+ export const CATALOG_MODELS: readonly ModelDescriptor[] = GENERATED_MODEL_CATALOG.models.flatMap(
98
+ (model) =>
99
+ model.harness === "codex-app-server" ? [model, { ...model, harness: "codex-sdk" }] : [model],
100
+ );
101
+
102
+ export const MODEL_CATALOG: readonly ModelInfo[] = CATALOG_MODELS.map((model) => ({
103
+ id: model.id,
104
+ harness: model.harness,
105
+ label: model.label,
106
+ thinking: model.thinking.levels,
107
+ }));
42
108
 
43
109
  /**
44
110
  * The model a turn runs on when its config names none. `undefined` means the
@@ -57,3 +123,11 @@ export const DEFAULT_MODELS: Record<AgentHarness, string | undefined> = {
57
123
  export function modelsForHarness(harness: AgentHarness): readonly ModelInfo[] {
58
124
  return MODEL_CATALOG.filter((m) => m.harness === harness);
59
125
  }
126
+
127
+ /** The catalog's thinking levels for a model; `undefined` when it isn't listed. */
128
+ export function modelThinkingLevels(
129
+ harness: AgentHarness,
130
+ model: string,
131
+ ): readonly ThinkingLevel[] | undefined {
132
+ return MODEL_CATALOG.find((m) => m.harness === harness && m.id === model)?.thinking;
133
+ }
@@ -3,8 +3,9 @@ import { z } from "zod";
3
3
  /**
4
4
  * A single, harness-agnostic reasoning dial. Each harness maps it to its own
5
5
  * native control: Claude -> adaptive `thinking` + `effort`; Codex ->
6
- * `ReasoningEffort`. The top levels depend on the model: Claude's ceiling is
7
- * `max` (it runs `ultra` as `max`), and not every Codex model offers `ultra`.
6
+ * `ReasoningEffort`. What runs depends on the model: Claude's ceiling is `max`
7
+ * (it runs `ultra` as `max`), and Codex runs the nearest level the model
8
+ * accepts (see `codexReasoningEffort`).
8
9
  */
9
10
  export const THINKING_LEVELS = ["off", "low", "medium", "high", "xhigh", "max", "ultra"] as const;
10
11
  export type ThinkingLevel = (typeof THINKING_LEVELS)[number];
@@ -15,8 +16,31 @@ export function isThinkingLevel(v: string): v is ThinkingLevel {
15
16
  return (THINKING_LEVELS as readonly string[]).includes(v);
16
17
  }
17
18
 
19
+ const rank = (level: ThinkingLevel) => THINKING_LEVELS.indexOf(level);
20
+
21
+ /**
22
+ * The level a model runs when it offers only `supported`: the highest one at
23
+ * or below `level`, else its lowest. Use it when switching models, so `ultra`
24
+ * on a model topping out at `max` becomes `max`, not a fixed fallback.
25
+ * `undefined` when the model offers none.
26
+ */
27
+ export function clampThinkingLevel(
28
+ level: ThinkingLevel,
29
+ supported: readonly ThinkingLevel[],
30
+ ): ThinkingLevel | undefined {
31
+ let below: ThinkingLevel | undefined;
32
+ let lowest: ThinkingLevel | undefined;
33
+ for (const candidate of supported) {
34
+ if (lowest === undefined || rank(candidate) < rank(lowest)) lowest = candidate;
35
+ if (rank(candidate) <= rank(level) && (below === undefined || rank(candidate) > rank(below)))
36
+ below = candidate;
37
+ }
38
+ return below ?? lowest;
39
+ }
40
+
18
41
  /** Codex reasoning-effort string for a level (matches the `ReasoningEffort` enum). */
19
42
  export type CodexReasoningEffort =
43
+ | "none"
20
44
  | "minimal"
21
45
  | "low"
22
46
  | "medium"
@@ -24,21 +48,20 @@ export type CodexReasoningEffort =
24
48
  | "xhigh"
25
49
  | "max"
26
50
  | "ultra";
27
- export function codexReasoningEffort(level: ThinkingLevel): CodexReasoningEffort {
28
- switch (level) {
29
- case "off":
30
- return "minimal";
31
- case "low":
32
- return "low";
33
- case "medium":
34
- return "medium";
35
- case "high":
36
- return "high";
37
- case "xhigh":
38
- return "xhigh";
39
- case "max":
40
- return "max";
41
- case "ultra":
42
- return "ultra";
43
- }
51
+
52
+ /**
53
+ * The effort Codex runs for `level`. `supported` is what the model accepts, in
54
+ * this vocabulary, where `off` stands for its least effort: `offEffort`, the
55
+ * native `none` or `minimal` the model advertises. A level it lacks becomes the
56
+ * nearest one it has. Without `supported`, `off` becomes `low`: no model the
57
+ * pinned CLI lists accepts `none` or `minimal`, and every one accepts `low`.
58
+ */
59
+ export function codexReasoningEffort<Off extends "none" | "minimal" = "minimal">(
60
+ level: ThinkingLevel,
61
+ supported?: readonly ThinkingLevel[],
62
+ offEffort?: Off,
63
+ ): Exclude<CodexReasoningEffort, "none" | "minimal"> | Off {
64
+ const clamped = supported?.length ? clampThinkingLevel(level, supported) : undefined;
65
+ const effective = clamped ?? (level === "off" ? "low" : level);
66
+ return effective === "off" ? (offEffort ?? ("minimal" as Off)) : effective;
44
67
  }
@@ -1,6 +1,6 @@
1
1
  import { z } from "zod";
2
2
  import { RunConfigSchema } from "./config.ts";
3
- import { AgentCapabilitiesSchema } from "./harness.ts";
3
+ import { AgentCapabilitiesSchema, AgentHarnessSchema } from "./harness.ts";
4
4
  import {
5
5
  type DecodedLifecycleEvent,
6
6
  LifecycleEventSchema,
@@ -8,6 +8,7 @@ import {
8
8
  type UnknownEvent,
9
9
  decodeLifecycleEvent,
10
10
  } from "./lifecycle.ts";
11
+ import { ModelListSchema } from "./models.ts";
11
12
  import { AgentInputSchema } from "./part-input.ts";
12
13
 
13
14
  /**
@@ -64,6 +65,7 @@ export const WIRE_METHODS = {
64
65
  sessionClose: "session/close",
65
66
  permissionRespond: "permission/respond",
66
67
  eventsReplay: "events/replay",
68
+ modelsList: "models/list",
67
69
  shutdown: "shutdown",
68
70
  /** Server -> client notification carrying one sequenced lifecycle event. */
69
71
  event: "event",
@@ -256,6 +258,27 @@ export const EventsReplayResultSchema = z.object({
256
258
  });
257
259
  export type EventsReplayResult = z.infer<typeof EventsReplayResultSchema>;
258
260
 
261
+ /**
262
+ * `models/list`: the models a harness offers, read from its CLI for the login
263
+ * and folder a turn with the same `cwd`, `env` and `apiKey` would use (Claude
264
+ * narrows the list by the folder's `availableModels`; Codex by the account).
265
+ * Served from a cache; `refresh` reads the CLI again, e.g. after a login.
266
+ */
267
+ export const ModelsListParamsSchema = z.object({
268
+ harness: AgentHarnessSchema,
269
+ /** The folder whose settings apply. Defaults to the server's working directory. */
270
+ cwd: z.string().optional(),
271
+ /** List with this session's credentials (host-managed Codex auth resolves per session). */
272
+ sessionId: z.string().min(1).optional(),
273
+ env: z.record(z.string(), z.string()).optional(),
274
+ apiKey: z.string().optional(),
275
+ refresh: z.boolean().optional(),
276
+ });
277
+ export type ModelsListParams = z.infer<typeof ModelsListParamsSchema>;
278
+
279
+ /** Never an error for an available harness: without a live list it is the catalog, with `error`. */
280
+ export const ModelsListResultSchema = ModelListSchema;
281
+
259
282
  export const ShutdownParamsSchema = z.object({}).optional();
260
283
  export const ShutdownResultSchema = z.object({});
261
284
 
@@ -6,6 +6,7 @@ import {
6
6
  InitializeParamsSchema,
7
7
  type InitializeResult,
8
8
  type JsonRpcId,
9
+ ModelsListParamsSchema,
9
10
  PermissionRespondParamsSchema,
10
11
  type RunRequest,
11
12
  SessionCloseParamsSchema,
@@ -388,6 +389,22 @@ export class AgentServer {
388
389
  );
389
390
  }
390
391
 
392
+ case WIRE_METHODS.modelsList: {
393
+ const parsed = ModelsListParamsSchema.safeParse(params);
394
+ if (!parsed.success) return this.invalidParams(transport, id, parsed.error);
395
+ if (!this.runtime.harnesses.includes(parsed.data.harness)) {
396
+ return transport.send(
397
+ encodeErrorResponse(
398
+ id,
399
+ WIRE_ERROR_CODES.harnessNotAvailable,
400
+ `harness not available: ${parsed.data.harness}`,
401
+ { available: this.runtime.harnesses },
402
+ ),
403
+ );
404
+ }
405
+ return transport.send(encodeResponse(id, await this.runtime.models(parsed.data)));
406
+ }
407
+
391
408
  case WIRE_METHODS.shutdown: {
392
409
  transport.send(encodeResponse(id, {}));
393
410
  // The success ack is already committed on the wire; a throw out of