Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

providerInstance.ts

148 lines6.7 KB

1/**
2 * Provider-instance contracts.
3 *
4 * Splits the historical "provider kind" concept into two:
5 *
6 * - `ProviderDriverKind` is the implementation kind selector (e.g. codex,
7 * claudeAgent, a fork's `ollama`, …). It picks which driver package
8 * handles the protocol, the probe, the adapter, and text generation.
9 *
10 * - `ProviderInstanceId` is the routing key (a user-defined slug).
11 * Threads, sessions, runtime events, and persisted bindings reference
12 * instance ids — never driver kinds — so a user can configure multiple
13 * instances of the same driver (e.g. `codex_personal` + `codex_work`),
14 * each with independent driver-specific configuration.
15 *
16 * Forward/backward compatibility invariant
17 * ----------------------------------------
18 * `ProviderDriverKind` is intentionally an **open** branded slug, not a closed
19 * literal union. The server hosts forks, ships in PRs that add drivers, and
20 * users frequently roll between branches and forks. Any of those paths can
21 * leave `ServerSettings`, persisted thread state, or session bindings
22 * referencing a driver that the currently-running build does not know about.
23 *
24 * The rule: parsing any of those payloads must always succeed, and the
25 * runtime is responsible for marking the unknown driver/instance as
26 * "unavailable" rather than crashing. Built-in drivers shipped by the core
27 * product happens to register in a given build is not part of the contract
28 * layer. Driver availability is discovered through the runtime registry.
29 *
30 * Driver-specific configuration is similarly opaque at the contracts layer:
31 * drivers live in (or will be extracted to) their own packages and own their
32 * config schemas. The contracts package only knows the envelope.
33 *
34 * @module providerInstance
35 */
36import { Effect, Schema } from "effect";
37import { TrimmedNonEmptyString } from "./baseSchemas.ts";
38​
39const PROVIDER_SLUG_MAX_CHARS = 64;
40/**
41 * Slug pattern shared by driver kinds and instance ids — letters, digits,
42 * dashes, underscores. The first character must be a letter so slugs remain
43 * JS-identifier friendly when used as object keys, log fields, or telemetry
44 * attributes. Mixed case is permitted so historical driver kinds (e.g.
45 * `claudeAgent`) can be used verbatim during the migration and so external
46 * fork authors retain reasonable freedom.
47 */
48const PROVIDER_SLUG_PATTERN = /^[a-zA-Z][a-zA-Z0-9_-]*$/;
49const ENVIRONMENT_VARIABLE_NAME_MAX_CHARS = 128;
50const ENVIRONMENT_VARIABLE_NAME_PATTERN = /^[a-zA-Z_][a-zA-Z0-9_]*$/;
51​
52const slugSchema = TrimmedNonEmptyString.check(
53 Schema.isMaxLength(PROVIDER_SLUG_MAX_CHARS),
54 Schema.isPattern(PROVIDER_SLUG_PATTERN),
55);
56​
57/**
58 * `ProviderDriverKind` — open branded slug naming a driver implementation.
59 *
60 * Constraints (validated at the schema layer):
61 * - starts with a letter
62 * - only letters, digits, `-`, `_` after the first char
63 * - 1..64 characters
64 *
65 * Notably **not** validated: that the driver is one we know how to load.
66 * That check belongs to the runtime registry, which downgrades unknown
67 * drivers gracefully (see module docs).
68 */
69export const ProviderDriverKind = slugSchema.pipe(Schema.brand("ProviderDriverKind"));
70export type ProviderDriverKind = typeof ProviderDriverKind.Type;
71​
72const isProviderDriverKindValue = Schema.is(ProviderDriverKind);
73export const isProviderDriverKind = (value: unknown): value is ProviderDriverKind =>
74 isProviderDriverKindValue(value);
75​
76/**
77 * `ProviderInstanceId` — user-defined routing key for a configured provider
78 * instance. Same slug rules as `ProviderDriverKind`; branded separately so the
79 * type system cannot confuse the two.
80 */
81export const ProviderInstanceId = slugSchema.pipe(Schema.brand("ProviderInstanceId"));
82export type ProviderInstanceId = typeof ProviderInstanceId.Type;
83​
84/**
85 * Lightweight reference identifying which driver implements an instance.
86 * Carried alongside `ProviderInstanceId` on wire shapes so consumers can
87 * branch on driver behavior (icons, capabilities, presentation) without
88 * having to look up the instance in the registry.
89 */
90export const ProviderInstanceRef = Schema.Struct({
91 instanceId: ProviderInstanceId,
92 driver: ProviderDriverKind,
93});
94export type ProviderInstanceRef = typeof ProviderInstanceRef.Type;
95​
96export const ProviderInstanceEnvironmentVariableName = TrimmedNonEmptyString.check(
97 Schema.isMaxLength(ENVIRONMENT_VARIABLE_NAME_MAX_CHARS),
98 Schema.isPattern(ENVIRONMENT_VARIABLE_NAME_PATTERN),
99);
100export type ProviderInstanceEnvironmentVariableName =
101 typeof ProviderInstanceEnvironmentVariableName.Type;
102​
103export const ProviderInstanceEnvironmentVariable = Schema.Struct({
104 name: ProviderInstanceEnvironmentVariableName,
105 value: Schema.String.pipe(Schema.withDecodingDefault(Effect.succeed(""))),
106 sensitive: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
107 valueRedacted: Schema.optionalKey(Schema.Boolean),
108});
109export type ProviderInstanceEnvironmentVariable = typeof ProviderInstanceEnvironmentVariable.Type;
110​
111export const ProviderInstanceEnvironment = Schema.Array(ProviderInstanceEnvironmentVariable);
112export type ProviderInstanceEnvironment = typeof ProviderInstanceEnvironment.Type;
113​
114/**
115 * Envelope shape for a provider instance configuration in `ServerSettings`.
116 *
117 * `driver` is intentionally accepted as any well-formed slug (see module
118 * docs). The driver-specific config payload is left as `Schema.Unknown`;
119 * each driver registers its own decoder with the runtime registry, and
120 * envelopes for unknown drivers are preserved verbatim so they round-trip
121 * across version changes without data loss.
122 */
123export const ProviderInstanceConfig = Schema.Struct({
124 driver: ProviderDriverKind,
125 displayName: Schema.optional(TrimmedNonEmptyString),
126 accentColor: Schema.optional(TrimmedNonEmptyString),
127 environment: Schema.optionalKey(ProviderInstanceEnvironment),
128 enabled: Schema.optionalKey(Schema.Boolean),
129 config: Schema.optionalKey(Schema.Unknown),
130});
131export type ProviderInstanceConfig = typeof ProviderInstanceConfig.Type;
132​
133/**
134 * Map shape for `ServerSettings.providerInstances`. Keyed by
135 * `ProviderInstanceId`, values are envelopes the registry feeds to drivers.
136 */
137export const ProviderInstanceConfigMap = Schema.Record(ProviderInstanceId, ProviderInstanceConfig);
138export type ProviderInstanceConfigMap = typeof ProviderInstanceConfigMap.Type;
139​
140/**
141 * Construct the canonical `ProviderInstanceId` used as a back-compat default
142 * for a built-in driver. The legacy single-instance-per-driver world used
143 * the driver kind itself as the instance id; preserving that mapping keeps
144 * existing persisted threads, bindings, and cache files routable across the
145 * migration without rewriting their stored selection payloads.
146 */
147export const defaultInstanceIdForDriver = (driver: ProviderDriverKind): ProviderInstanceId =>
148 ProviderInstanceId.make(driver);