@songsid/agend 2.1.4-beta.13 → 2.1.4-beta.14

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.
@@ -36,6 +36,25 @@ export declare function selectLruEvictions(warm: string[], cap: number, opts: {
36
36
  isIdle: (name: string) => boolean;
37
37
  lastInboundAt: (name: string) => number;
38
38
  }): string[];
39
+ /**
40
+ * Answer shape for `list_models`. `scope` reports where the LIST came from —
41
+ * "instance" only when it was read through that instance's own backend config,
42
+ * "global" for the account/CLI catalog — so a caller can tell an authoritative
43
+ * per-instance list from a best-effort account-wide one.
44
+ */
45
+ export interface ModelCatalog {
46
+ backend: string;
47
+ scope: "instance" | "global";
48
+ /** Set whenever an instance was asked about, even if the list is global. */
49
+ instance?: string;
50
+ current_model: string | null;
51
+ models: import("./backend/types.js").ModelOption[];
52
+ /** "cache" = startup probe cache, "live" = probed now, "fallback" = none available. */
53
+ source: "cache" | "live" | "fallback";
54
+ probed_at?: string;
55
+ /** Caveat the caller should read before trusting the list. */
56
+ note?: string;
57
+ }
39
58
  export interface DeliveryOptions {
40
59
  /** Explicitly identify agent-to-agent delivery when metadata is unavailable. */
41
60
  isCrossInstance?: boolean;
@@ -848,6 +867,34 @@ export declare class FleetManager implements FleetContext, LifecycleContext, Arc
848
867
  private probeCliEnvs;
849
868
  /** Best-effort model list for `/model`: cached CLI env first, else live probe. Never throws. */
850
869
  private getModelOptions;
870
+ /**
871
+ * Model catalog behind the `list_models` tool.
872
+ *
873
+ * The two scopes are not cosmetic. "global" is the account/CLI catalog served
874
+ * from the startup probe cache; "instance" is resolved through that instance's
875
+ * OWN backend config, and for a Codex instance on a custom provider that is a
876
+ * different catalog entirely — `listModels()` reads models_cache.json out of
877
+ * the instance's private CODEX_HOME. Answering such an instance with the
878
+ * account list would name models its CLI rejects, which is exactly the
879
+ * mistake this tool exists to prevent.
880
+ *
881
+ * `scope` always describes where the returned LIST came from, not what was
882
+ * asked for: an instance query that falls back to the account catalog reports
883
+ * scope "global" and says so in `note`, rather than implying instance-level
884
+ * accuracy it does not have.
885
+ *
886
+ * Never throws — a model listing is an aid, and failing it must not fail a turn.
887
+ */
888
+ listModelCatalog(opts?: {
889
+ backend?: string;
890
+ instanceName?: string;
891
+ }): Promise<ModelCatalog>;
892
+ /** The custom provider an instance overrides its backend with, if any. */
893
+ private customProviderFor;
894
+ /** Ask a backend for its catalog using ONE instance's real config. Never throws. */
895
+ private instanceScopedModels;
896
+ /** Account-wide catalog: probe cache first, live probe on miss. */
897
+ private globalModelCatalog;
851
898
  /** `/model` slash handler (admin only). No arg → DC menu; `/model <name>` → apply directly. */
852
899
  /** Label an effort choice, marking the one currently configured. */
853
900
  private effortChoiceLabel;
@@ -6457,6 +6457,17 @@ Plus the operational skills (fleet-health, instance-lifecycle, scheduling, sessi
6457
6457
  return null;
6458
6458
  const probed = await be.probeCLIEnv({ workingDirectory: "", instanceDir: join(getAgendHome(), "cli-env"), instanceName: `probe-${backend}`, mcpServers: {} });
6459
6459
  const env = { backend, probedAt: Date.now(), ...probed };
6460
+ // An empty result must never overwrite a catalog we already have. Some
6461
+ // probes hit the network (`agy models` fetches, 5s cap), so a slow moment
6462
+ // returns [] — and writing that would blank the list for the whole 24h
6463
+ // TTL, long after the CLI recovered. Observed live: a good 11-model
6464
+ // antigravity cache replaced by an empty one. Keep the known models and
6465
+ // let the fresher currentModel/version through.
6466
+ if (!env.models?.length) {
6467
+ const previous = this.readCliEnv(backend);
6468
+ if (previous?.models?.length)
6469
+ env.models = previous.models;
6470
+ }
6460
6471
  const path = this.cliEnvPath(backend);
6461
6472
  mkdirSync(dirname(path), { recursive: true });
6462
6473
  writeFileSync(path, JSON.stringify(env, null, 2));
@@ -6495,6 +6506,108 @@ Plus the operational skills (fleet-health, instance-lifecycle, scheduling, sessi
6495
6506
  const env = await this.probeBackend(backendName);
6496
6507
  return env?.models ?? [];
6497
6508
  }
6509
+ /**
6510
+ * Model catalog behind the `list_models` tool.
6511
+ *
6512
+ * The two scopes are not cosmetic. "global" is the account/CLI catalog served
6513
+ * from the startup probe cache; "instance" is resolved through that instance's
6514
+ * OWN backend config, and for a Codex instance on a custom provider that is a
6515
+ * different catalog entirely — `listModels()` reads models_cache.json out of
6516
+ * the instance's private CODEX_HOME. Answering such an instance with the
6517
+ * account list would name models its CLI rejects, which is exactly the
6518
+ * mistake this tool exists to prevent.
6519
+ *
6520
+ * `scope` always describes where the returned LIST came from, not what was
6521
+ * asked for: an instance query that falls back to the account catalog reports
6522
+ * scope "global" and says so in `note`, rather than implying instance-level
6523
+ * accuracy it does not have.
6524
+ *
6525
+ * Never throws — a model listing is an aid, and failing it must not fail a turn.
6526
+ */
6527
+ async listModelCatalog(opts = {}) {
6528
+ const { instanceName } = opts;
6529
+ if (instanceName) {
6530
+ const backend = this.backendNameForInstance(instanceName);
6531
+ const resolved = this.resolveInstanceModel(instanceName);
6532
+ const currentModel = resolved.source === "unresolved" ? null : resolved.model;
6533
+ const provider = this.customProviderFor(instanceName, backend);
6534
+ const scoped = await this.instanceScopedModels(instanceName, backend);
6535
+ if (scoped.length) {
6536
+ return {
6537
+ backend, scope: "instance", instance: instanceName,
6538
+ current_model: currentModel, models: scoped, source: "live",
6539
+ ...(provider ? { note: `Catalog read through this instance's ${backend} provider "${provider}" — it may differ from the account catalog.` } : {}),
6540
+ };
6541
+ }
6542
+ // No instance-local catalog (never launched, or the backend has no
6543
+ // per-instance list). The account catalog is the best available answer,
6544
+ // but it is labelled honestly rather than dressed up as instance scope.
6545
+ const global = await this.globalModelCatalog(backend);
6546
+ return {
6547
+ ...global, instance: instanceName, current_model: currentModel,
6548
+ note: provider
6549
+ ? `No instance-local catalog yet; showing the account catalog, which may NOT match this instance's ${backend} provider "${provider}".`
6550
+ : "No instance-local catalog yet; showing the account catalog.",
6551
+ };
6552
+ }
6553
+ return this.globalModelCatalog(opts.backend ?? this.fleetConfig?.defaults?.backend ?? "claude-code");
6554
+ }
6555
+ /** The custom provider an instance overrides its backend with, if any. */
6556
+ customProviderFor(instanceName, backend) {
6557
+ const opts = this.fleetConfig?.instances?.[instanceName]?.backend_options?.[backend]
6558
+ ?? this.fleetConfig?.defaults?.backend_options?.[backend];
6559
+ const provider = opts?.provider;
6560
+ return typeof provider === "string" && provider.trim() ? provider.trim() : null;
6561
+ }
6562
+ /** Ask a backend for its catalog using ONE instance's real config. Never throws. */
6563
+ async instanceScopedModels(instanceName, backend) {
6564
+ try {
6565
+ const inst = this.fleetConfig?.instances?.[instanceName];
6566
+ const instanceDir = this.getInstanceDir(instanceName);
6567
+ const be = createBackend(backend, instanceDir);
6568
+ if (!be.listModels)
6569
+ return [];
6570
+ return await be.listModels({
6571
+ workingDirectory: inst?.working_directory ?? "",
6572
+ instanceDir,
6573
+ instanceName,
6574
+ mcpServers: {},
6575
+ model: inst?.model,
6576
+ backendOptions: inst?.backend_options?.[backend] ?? this.fleetConfig?.defaults?.backend_options?.[backend],
6577
+ }) ?? [];
6578
+ }
6579
+ catch {
6580
+ // listModels is documented never to throw, but a backend constructor can
6581
+ // (missing binary). A catalog is an aid; degrade to the account list.
6582
+ return [];
6583
+ }
6584
+ }
6585
+ /** Account-wide catalog: probe cache first, live probe on miss. */
6586
+ async globalModelCatalog(backend) {
6587
+ const cached = this.readCliEnv(backend);
6588
+ if (cached?.models?.length) {
6589
+ return {
6590
+ backend, scope: "global", current_model: cached.currentModel ?? null,
6591
+ models: cached.models, source: "cache",
6592
+ probed_at: new Date(cached.probedAt).toISOString(),
6593
+ };
6594
+ }
6595
+ const env = await this.probeBackend(backend);
6596
+ if (env?.models?.length) {
6597
+ return {
6598
+ backend, scope: "global", current_model: env.currentModel ?? null,
6599
+ models: env.models, source: "live",
6600
+ probed_at: new Date(env.probedAt).toISOString(),
6601
+ };
6602
+ }
6603
+ // Reported rather than thrown: "we could not enumerate" is a useful answer,
6604
+ // and the caller can still set a model by name (AgEnD passes it through).
6605
+ return {
6606
+ backend, scope: "global", current_model: env?.currentModel ?? null,
6607
+ models: [], source: "fallback",
6608
+ note: `Could not enumerate models for ${backend} (CLI missing, not logged in, or it offers no list). Model names are passed through to the CLI, so a known-good name still works.`,
6609
+ };
6610
+ }
6498
6611
  /** `/model` slash handler (admin only). No arg → DC menu; `/model <name>` → apply directly. */
6499
6612
  /** Label an effort choice, marking the one currently configured. */
6500
6613
  effortChoiceLabel(level, current) {