Open History to see the last commit
1import { Effect, Schema } from "effect";
2import { ExecutionEnvironmentDescriptor } from "./environment.ts";
3import { ServerAuthDescriptor } from "./auth.ts";
4import {
5 IsoDateTime,
6 NonNegativeInt,
7 ProjectId,
8 ThreadId,
9 TrimmedNonEmptyString,
10} from "./baseSchemas.ts";
11import { KeybindingRule, ResolvedKeybindingsConfig } from "./keybindings.ts";
12import { EditorId } from "./editor.ts";
13import { ModelCapabilities } from "./model.ts";
14import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
15import { ServerSettings } from "./settings.ts";
16
17const KeybindingsMalformedConfigIssue = Schema.Struct({
18 kind: Schema.Literal("keybindings.malformed-config"),
19 message: TrimmedNonEmptyString,
20});
21
22const KeybindingsInvalidEntryIssue = Schema.Struct({
23 kind: Schema.Literal("keybindings.invalid-entry"),
24 message: TrimmedNonEmptyString,
25 index: Schema.Number,
26});
27
28export const ServerConfigIssue = Schema.Union([
29 KeybindingsMalformedConfigIssue,
30 KeybindingsInvalidEntryIssue,
31]);
32export type ServerConfigIssue = typeof ServerConfigIssue.Type;
33
34const ServerConfigIssues = Schema.Array(ServerConfigIssue);
35
36export const ServerProviderState = Schema.Literals(["ready", "warning", "error", "disabled"]);
37export type ServerProviderState = typeof ServerProviderState.Type;
38
39export const ServerProviderAuthStatus = Schema.Literals([
40 "authenticated",
41 "unauthenticated",
42 "unknown",
43]);
44export type ServerProviderAuthStatus = typeof ServerProviderAuthStatus.Type;
45
46export const ServerProviderAuth = Schema.Struct({
47 status: ServerProviderAuthStatus,
48 type: Schema.optional(TrimmedNonEmptyString),
49 label: Schema.optional(TrimmedNonEmptyString),
50 email: Schema.optional(TrimmedNonEmptyString),
51});
52export type ServerProviderAuth = typeof ServerProviderAuth.Type;
53
54export const ServerProviderModel = Schema.Struct({
55 slug: TrimmedNonEmptyString,
56 name: TrimmedNonEmptyString,
57 shortName: Schema.optional(TrimmedNonEmptyString),
58 subProvider: Schema.optional(TrimmedNonEmptyString),
59 isCustom: Schema.Boolean,
60 capabilities: Schema.NullOr(ModelCapabilities),
61});
62export type ServerProviderModel = typeof ServerProviderModel.Type;
63
64export const ServerProviderSlashCommandInput = Schema.Struct({
65 hint: TrimmedNonEmptyString,
66});
67export type ServerProviderSlashCommandInput = typeof ServerProviderSlashCommandInput.Type;
68
69export const ServerProviderSlashCommand = Schema.Struct({
70 name: TrimmedNonEmptyString,
71 description: Schema.optional(TrimmedNonEmptyString),
72 input: Schema.optional(ServerProviderSlashCommandInput),
73});
74export type ServerProviderSlashCommand = typeof ServerProviderSlashCommand.Type;
75
76export const ServerProviderSkill = Schema.Struct({
77 name: TrimmedNonEmptyString,
78 description: Schema.optional(TrimmedNonEmptyString),
79 path: TrimmedNonEmptyString,
80 scope: Schema.optional(TrimmedNonEmptyString),
81 enabled: Schema.Boolean,
82 displayName: Schema.optional(TrimmedNonEmptyString),
83 shortDescription: Schema.optional(TrimmedNonEmptyString),
84});
85export type ServerProviderSkill = typeof ServerProviderSkill.Type;
86
87/**
88 * Availability of a configured provider instance from the runtime's POV.
89 *
90 * - `available` — the build ships this driver and an instance is wired
91 * up. Default for legacy snapshots produced from the closed
92 * `ServerSettings.providers` map.
93 * - `unavailable` — the user's `ServerSettings.providerInstances` (or a
94 * persisted thread / session binding) references a driver this build
95 * doesn't ship. Common after rolling back from a fork or PR branch
96 * that introduced a new driver. The snapshot is preserved so the UI
97 * can render "missing driver" affordances and so the data round-trips
98 * when the user moves back to the fork.
99 *
100 * Snapshots with `availability: "unavailable"` MUST set
101 * `installed: false` and `enabled: false`; the runtime refuses turn
102 * starts against them with a structured error.
103 */
104export const ServerProviderAvailability = Schema.Literals(["available", "unavailable"]);
105export type ServerProviderAvailability = typeof ServerProviderAvailability.Type;
106
107export const ServerProviderContinuation = Schema.Struct({
108 groupKey: TrimmedNonEmptyString,
109});
110export type ServerProviderContinuation = typeof ServerProviderContinuation.Type;
111
112export const ServerProvider = Schema.Struct({
113 // Routing key for the configured instance this snapshot represents. This
114 // is the only stable identity consumers may use for provider routing.
115 instanceId: ProviderInstanceId,
116 // Open driver kind slug that selects the implementation handling this
117 // instance. It is metadata/capability context, not a routing key.
118 driver: ProviderDriverKind,
119 displayName: Schema.optional(TrimmedNonEmptyString),
120 accentColor: Schema.optional(TrimmedNonEmptyString),
121 badgeLabel: Schema.optional(TrimmedNonEmptyString),
122 continuation: Schema.optional(ServerProviderContinuation),
123 showInteractionModeToggle: Schema.optional(Schema.Boolean),
124 enabled: Schema.Boolean,
125 installed: Schema.Boolean,
126 version: Schema.NullOr(TrimmedNonEmptyString),
127 status: ServerProviderState,
128 auth: ServerProviderAuth,
129 checkedAt: IsoDateTime,
130 message: Schema.optional(TrimmedNonEmptyString),
131 // Optional for back-compat: every legacy producer omits this field and
132 // an absent value is interpreted as `"available"` by consumers (see
133 // `isProviderAvailable`). New `ProviderInstanceRegistry` outputs set it
134 // explicitly so the UI can render unavailable shadows from
135 // `ServerSettings.providerInstances`.
136 availability: Schema.optional(ServerProviderAvailability),
137 // Human-readable reason populated when `availability === "unavailable"`.
138 // Surfaces in the UI alongside the missing-driver affordance.
139 unavailableReason: Schema.optional(TrimmedNonEmptyString),
140 models: Schema.Array(ServerProviderModel),
141 slashCommands: Schema.Array(ServerProviderSlashCommand).pipe(
142 Schema.withDecodingDefault(Effect.succeed([])),
143 ),
144 skills: Schema.Array(ServerProviderSkill).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
145});
146export type ServerProvider = typeof ServerProvider.Type;
147
148export const ServerProviders = Schema.Array(ServerProvider);
149export type ServerProviders = typeof ServerProviders.Type;
150
151/**
152 * Treat the optional `availability` as "available" when absent. This is
153 * the rule legacy producers (which omit the field) and new producers
154 * (which set it explicitly) agree on so consumers never have to thread
155 * `?? "available"` defaults through their code paths.
156 */
157export const isProviderAvailable = (snapshot: ServerProvider): boolean =>
158 snapshot.availability !== "unavailable";
159
160export const ServerObservability = Schema.Struct({
161 logsDirectoryPath: TrimmedNonEmptyString,
162 localTracingEnabled: Schema.Boolean,
163 otlpTracesUrl: Schema.optional(TrimmedNonEmptyString),
164 otlpTracesEnabled: Schema.Boolean,
165 otlpMetricsUrl: Schema.optional(TrimmedNonEmptyString),
166 otlpMetricsEnabled: Schema.Boolean,
167});
168export type ServerObservability = typeof ServerObservability.Type;
169
170export const ServerConfig = Schema.Struct({
171 environment: ExecutionEnvironmentDescriptor,
172 auth: ServerAuthDescriptor,
173 cwd: TrimmedNonEmptyString,
174 keybindingsConfigPath: TrimmedNonEmptyString,
175 keybindings: ResolvedKeybindingsConfig,
176 issues: ServerConfigIssues,
177 providers: ServerProviders,
178 availableEditors: Schema.Array(EditorId),
179 observability: ServerObservability,
180 settings: ServerSettings,
181});
182export type ServerConfig = typeof ServerConfig.Type;
183
184export const ServerUpsertKeybindingInput = KeybindingRule;
185export type ServerUpsertKeybindingInput = typeof ServerUpsertKeybindingInput.Type;
186
187export const ServerUpsertKeybindingResult = Schema.Struct({
188 keybindings: ResolvedKeybindingsConfig,
189 issues: ServerConfigIssues,
190});
191export type ServerUpsertKeybindingResult = typeof ServerUpsertKeybindingResult.Type;
192
193export const ServerConfigUpdatedPayload = Schema.Struct({
194 issues: ServerConfigIssues,
195 providers: ServerProviders,
196 settings: Schema.optional(ServerSettings),
197});
198export type ServerConfigUpdatedPayload = typeof ServerConfigUpdatedPayload.Type;
199
200export const ServerConfigKeybindingsUpdatedPayload = Schema.Struct({
201 issues: ServerConfigIssues,
202});
203export type ServerConfigKeybindingsUpdatedPayload =
204 typeof ServerConfigKeybindingsUpdatedPayload.Type;
205
206export const ServerConfigProviderStatusesPayload = Schema.Struct({
207 providers: ServerProviders,
208});
209export type ServerConfigProviderStatusesPayload = typeof ServerConfigProviderStatusesPayload.Type;
210
211export const ServerConfigSettingsUpdatedPayload = Schema.Struct({
212 settings: ServerSettings,
213});
214export type ServerConfigSettingsUpdatedPayload = typeof ServerConfigSettingsUpdatedPayload.Type;
215
216export const ServerConfigStreamSnapshotEvent = Schema.Struct({
217 version: Schema.Literal(1),
218 type: Schema.Literal("snapshot"),
219 config: ServerConfig,
220});
221export type ServerConfigStreamSnapshotEvent = typeof ServerConfigStreamSnapshotEvent.Type;
222
223export const ServerConfigStreamKeybindingsUpdatedEvent = Schema.Struct({
224 version: Schema.Literal(1),
225 type: Schema.Literal("keybindingsUpdated"),
226 payload: ServerConfigKeybindingsUpdatedPayload,
227});
228export type ServerConfigStreamKeybindingsUpdatedEvent =
229 typeof ServerConfigStreamKeybindingsUpdatedEvent.Type;
230
231export const ServerConfigStreamProviderStatusesEvent = Schema.Struct({
232 version: Schema.Literal(1),
233 type: Schema.Literal("providerStatuses"),
234 payload: ServerConfigProviderStatusesPayload,
235});
236export type ServerConfigStreamProviderStatusesEvent =
237 typeof ServerConfigStreamProviderStatusesEvent.Type;
238
239export const ServerConfigStreamSettingsUpdatedEvent = Schema.Struct({
240 version: Schema.Literal(1),
241 type: Schema.Literal("settingsUpdated"),
242 payload: ServerConfigSettingsUpdatedPayload,
243});
244export type ServerConfigStreamSettingsUpdatedEvent =
245 typeof ServerConfigStreamSettingsUpdatedEvent.Type;
246
247export const ServerConfigStreamEvent = Schema.Union([
248 ServerConfigStreamSnapshotEvent,
249 ServerConfigStreamKeybindingsUpdatedEvent,
250 ServerConfigStreamProviderStatusesEvent,
251 ServerConfigStreamSettingsUpdatedEvent,
252]);
253export type ServerConfigStreamEvent = typeof ServerConfigStreamEvent.Type;
254
255export const ServerLifecycleReadyPayload = Schema.Struct({
256 at: IsoDateTime,
257 environment: ExecutionEnvironmentDescriptor,
258});
259export type ServerLifecycleReadyPayload = typeof ServerLifecycleReadyPayload.Type;
260
261export const ServerLifecycleWelcomePayload = Schema.Struct({
262 environment: ExecutionEnvironmentDescriptor,
263 cwd: TrimmedNonEmptyString,
264 projectName: TrimmedNonEmptyString,
265 bootstrapProjectId: Schema.optional(ProjectId),
266 bootstrapThreadId: Schema.optional(ThreadId),
267});
268export type ServerLifecycleWelcomePayload = typeof ServerLifecycleWelcomePayload.Type;
269
270export const ServerLifecycleStreamWelcomeEvent = Schema.Struct({
271 version: Schema.Literal(1),
272 sequence: NonNegativeInt,
273 type: Schema.Literal("welcome"),
274 payload: ServerLifecycleWelcomePayload,
275});
276export type ServerLifecycleStreamWelcomeEvent = typeof ServerLifecycleStreamWelcomeEvent.Type;
277
278export const ServerLifecycleStreamReadyEvent = Schema.Struct({
279 version: Schema.Literal(1),
280 sequence: NonNegativeInt,
281 type: Schema.Literal("ready"),
282 payload: ServerLifecycleReadyPayload,
283});
284export type ServerLifecycleStreamReadyEvent = typeof ServerLifecycleStreamReadyEvent.Type;
285
286export const ServerLifecycleStreamEvent = Schema.Union([
287 ServerLifecycleStreamWelcomeEvent,
288 ServerLifecycleStreamReadyEvent,
289]);
290export type ServerLifecycleStreamEvent = typeof ServerLifecycleStreamEvent.Type;
291
292export const ServerProviderUpdatedPayload = Schema.Struct({
293 providers: ServerProviders,
294});
295export type ServerProviderUpdatedPayload = typeof ServerProviderUpdatedPayload.Type;