@zvada/agent-server 0.3.15 → 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,10 +1,84 @@
1
- import type { AgentHarness } from "./harness.ts";
2
- import 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;
@@ -14,51 +88,23 @@ export interface ModelInfo {
14
88
  readonly thinking: readonly ThinkingLevel[];
15
89
  }
16
90
 
17
- const UP_TO_XHIGH: readonly ThinkingLevel[] = ["low", "medium", "high", "xhigh"];
18
- const UP_TO_MAX: readonly ThinkingLevel[] = [...UP_TO_XHIGH, "max"];
19
- const UP_TO_ULTRA: readonly ThinkingLevel[] = [...UP_TO_MAX, "ultra"];
20
-
21
- // Claude Code 2.1.288 aliases. The 5.5 models reject disabled thinking and
22
- // Claude runs `ultra` as `max`, so neither `off` nor `ultra` is a setting of
23
- // its own. Haiku 4.5 has no effort control.
24
- const CLAUDE_MODELS: ReadonlyArray<readonly [string, string, readonly ThinkingLevel[]]> = [
25
- ["sonnet", "Claude Sonnet (latest)", UP_TO_MAX],
26
- ["opus", "Claude Opus (latest)", UP_TO_MAX],
27
- ["fable", "Claude Fable (latest)", UP_TO_MAX],
28
- ["haiku", "Claude Haiku (latest)", []],
29
- ];
30
-
31
- // As Codex CLI 0.160.0 `model/list` reports them. No model accepts `minimal`,
32
- // so none offers `off`. The GPT-6 family needs CLI 0.159 or newer.
33
- const CODEX_MODELS: ReadonlyArray<readonly [string, string, readonly ThinkingLevel[]]> = [
34
- ["gpt-6.1-sol", "GPT-6.1 Sol", UP_TO_ULTRA],
35
- ["gpt-6-astra", "GPT-6 Astra", UP_TO_ULTRA],
36
- ["gpt-6-sol", "GPT-6 Sol", UP_TO_ULTRA],
37
- ["gpt-6-luna", "GPT-6 Luna", UP_TO_MAX],
38
- ["gpt-5.6-sol", "GPT-5.6 Sol", UP_TO_ULTRA],
39
- ["gpt-5.6-terra", "GPT-5.6 Terra", UP_TO_ULTRA],
40
- ["gpt-5.6-luna", "GPT-5.6 Luna", UP_TO_MAX],
41
- // Codex refuses it for a ChatGPT login.
42
- ["gpt-5.5", "GPT-5.5 (API key)", UP_TO_XHIGH],
43
- ];
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
+ );
44
101
 
45
- export const MODEL_CATALOG: readonly ModelInfo[] = [
46
- ...CLAUDE_MODELS.map(([id, label, thinking]) => ({
47
- id,
48
- harness: "claude-code" as const,
49
- label,
50
- thinking,
51
- })),
52
- // Shared between codex-sdk and codex-app-server.
53
- ...CODEX_MODELS.flatMap(([id, label, thinking]) =>
54
- (["codex-sdk", "codex-app-server"] as const).map((harness) => ({
55
- id,
56
- harness,
57
- label,
58
- thinking,
59
- })),
60
- ),
61
- ];
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
+ }));
62
108
 
63
109
  /**
64
110
  * The model a turn runs on when its config names none. `undefined` means the
@@ -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