Open History to see the last commit
1/**
2 * Helpers for synthesizing "unavailable" `ServerProvider` snapshots.
3 *
4 * When `ServerSettings.providerInstances` (or persisted thread/session
5 * state) references a driver this build does not ship — typical after a
6 * downgrade from a fork or a feature-branch test session — the runtime
7 * needs to surface the entry to the UI without crashing. This module
8 * produces shadow snapshots that satisfy `ServerProvider`'s wire shape
9 * while signalling unavailability.
10 *
11 * @module unavailableProviderSnapshot
12 */
13import {
14 ProviderDriverKind,
15 type ProviderInstanceId,
16 type ServerProvider,
17} from "@t3tools/contracts";
18
19import { buildServerProvider } from "./providerSnapshot.ts";
20
21export interface UnavailableProviderSnapshotInput {
22 readonly driverKind: ProviderDriverKind | string;
23 readonly instanceId: ProviderInstanceId;
24 readonly displayName?: string | undefined;
25 readonly accentColor?: string | undefined;
26 readonly reason: string;
27 /**
28 * Optional override for `checkedAt`. Defaulted to `new Date()` so callers
29 * (notably tests) don't have to pass it.
30 */
31 readonly checkedAt?: string;
32}
33
34/**
35 * Produce a `ServerProvider` snapshot representing a configured instance
36 * whose driver the running build does not implement. The result is safe
37 * to broadcast over the wire and is structured so the web UI can render
38 * a "missing driver" affordance without special-casing.
39 */
40export function buildUnavailableProviderSnapshot(
41 input: UnavailableProviderSnapshotInput,
42): ServerProvider {
43 const checkedAt = input.checkedAt ?? new Date().toISOString();
44 const displayName = input.displayName?.trim() || (input.driverKind as string);
45
46 const base = buildServerProvider({
47 presentation: { displayName },
48 enabled: false,
49 checkedAt,
50 models: [],
51 skills: [],
52 probe: {
53 installed: false,
54 version: null,
55 status: "error",
56 auth: { status: "unknown" },
57 message: input.reason,
58 },
59 });
60
61 return {
62 ...base,
63 instanceId: input.instanceId,
64 ...(input.accentColor ? { accentColor: input.accentColor } : {}),
65 driver:
66 typeof input.driverKind === "string"
67 ? ProviderDriverKind.make(input.driverKind)
68 : input.driverKind,
69 availability: "unavailable",
70 unavailableReason: input.reason,
71 };
72}