Open History to see the last commit
1import * as Effect from "effect/Effect";
2import * as Schema from "effect/Schema";
3import { ExecutionEnvironmentDescriptor } from "./environment.ts";
4import { ServerAuthDescriptor } from "./auth.ts";
5import {
6 IsoDateTime,
7 NonNegativeInt,
8 PositiveInt,
9 ProjectId,
10 ThreadId,
11 TrimmedNonEmptyString,
12} from "./baseSchemas.ts";
13import {
14 KeybindingCommand,
15 KeybindingValue,
16 KeybindingWhen,
17 ResolvedKeybindingsConfig,
18} from "./keybindings.ts";
19import { EditorId } from "./editor.ts";
20import { ModelCapabilities } from "./model.ts";
21import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
22import { ServerSettings } from "./settings.ts";
23
24const KeybindingsMalformedConfigIssue = Schema.Struct({
25 kind: Schema.Literal("keybindings.malformed-config"),
26 message: TrimmedNonEmptyString,
27});
28
29const KeybindingsInvalidEntryIssue = Schema.Struct({
30 kind: Schema.Literal("keybindings.invalid-entry"),
31 message: TrimmedNonEmptyString,
32 index: Schema.Number,
33});
34
35export const ServerConfigIssue = Schema.Union([
36 KeybindingsMalformedConfigIssue,
37 KeybindingsInvalidEntryIssue,
38]);
39export type ServerConfigIssue = typeof ServerConfigIssue.Type;
40
41const ServerConfigIssues = Schema.Array(ServerConfigIssue);
42
43export const ServerProviderState = Schema.Literals(["ready", "warning", "error", "disabled"]);
44export type ServerProviderState = typeof ServerProviderState.Type;
45
46export const ServerProviderAuthStatus = Schema.Literals([
47 "authenticated",
48 "unauthenticated",
49 "unknown",
50]);
51export type ServerProviderAuthStatus = typeof ServerProviderAuthStatus.Type;
52
53export const ServerProviderAuth = Schema.Struct({
54 status: ServerProviderAuthStatus,
55 type: Schema.optional(TrimmedNonEmptyString),
56 label: Schema.optional(TrimmedNonEmptyString),
57 email: Schema.optional(TrimmedNonEmptyString),
58});
59export type ServerProviderAuth = typeof ServerProviderAuth.Type;
60
61export const ServerProviderModel = Schema.Struct({
62 slug: TrimmedNonEmptyString,
63 name: TrimmedNonEmptyString,
64 shortName: Schema.optional(TrimmedNonEmptyString),
65 subProvider: Schema.optional(TrimmedNonEmptyString),
66 isCustom: Schema.Boolean,
67 capabilities: Schema.NullOr(ModelCapabilities),
68});
69export type ServerProviderModel = typeof ServerProviderModel.Type;
70
71export const ServerProviderSlashCommandInput = Schema.Struct({
72 hint: TrimmedNonEmptyString,
73});
74export type ServerProviderSlashCommandInput = typeof ServerProviderSlashCommandInput.Type;
75
76export const ServerProviderSlashCommand = Schema.Struct({
77 name: TrimmedNonEmptyString,
78 description: Schema.optional(TrimmedNonEmptyString),
79 input: Schema.optional(ServerProviderSlashCommandInput),
80});
81export type ServerProviderSlashCommand = typeof ServerProviderSlashCommand.Type;
82
83export const ServerProviderSkill = Schema.Struct({
84 name: TrimmedNonEmptyString,
85 description: Schema.optional(TrimmedNonEmptyString),
86 path: TrimmedNonEmptyString,
87 scope: Schema.optional(TrimmedNonEmptyString),
88 enabled: Schema.Boolean,
89 displayName: Schema.optional(TrimmedNonEmptyString),
90 shortDescription: Schema.optional(TrimmedNonEmptyString),
91});
92export type ServerProviderSkill = typeof ServerProviderSkill.Type;
93
94/**
95 * Availability of a configured provider instance from the runtime's POV.
96 *
97 * - `available` — the build ships this driver and an instance is wired
98 * up. Default for legacy snapshots produced from the closed
99 * `ServerSettings.providers` map.
100 * - `unavailable` — the user's `ServerSettings.providerInstances` (or a
101 * persisted thread / session binding) references a driver this build
102 * doesn't ship. Common after rolling back from a fork or PR branch
103 * that introduced a new driver. The snapshot is preserved so the UI
104 * can render "missing driver" affordances and so the data round-trips
105 * when the user moves back to the fork.
106 *
107 * Snapshots with `availability: "unavailable"` MUST set
108 * `installed: false` and `enabled: false`; the runtime refuses turn
109 * starts against them with a structured error.
110 */
111export const ServerProviderAvailability = Schema.Literals(["available", "unavailable"]);
112export type ServerProviderAvailability = typeof ServerProviderAvailability.Type;
113
114export const ServerProviderContinuation = Schema.Struct({
115 groupKey: TrimmedNonEmptyString,
116});
117export type ServerProviderContinuation = typeof ServerProviderContinuation.Type;
118
119export const ServerProviderVersionAdvisoryStatus = Schema.Literals([
120 "unknown",
121 "current",
122 "behind_latest",
123]);
124export type ServerProviderVersionAdvisoryStatus = typeof ServerProviderVersionAdvisoryStatus.Type;
125
126export const ServerProviderVersionAdvisory = Schema.Struct({
127 status: ServerProviderVersionAdvisoryStatus,
128 currentVersion: Schema.NullOr(TrimmedNonEmptyString),
129 latestVersion: Schema.NullOr(TrimmedNonEmptyString),
130 updateCommand: Schema.NullOr(TrimmedNonEmptyString),
131 canUpdate: Schema.Boolean.pipe(Schema.withDecodingDefault(Effect.succeed(false))),
132 checkedAt: Schema.NullOr(IsoDateTime),
133 message: Schema.NullOr(TrimmedNonEmptyString),
134});
135export type ServerProviderVersionAdvisory = typeof ServerProviderVersionAdvisory.Type;
136
137export const ServerProviderUpdateStatus = Schema.Literals([
138 "idle",
139 "queued",
140 "running",
141 "succeeded",
142 "failed",
143 "unchanged",
144]);
145export type ServerProviderUpdateStatus = typeof ServerProviderUpdateStatus.Type;
146
147export const ServerProviderUpdateState = Schema.Struct({
148 status: ServerProviderUpdateStatus,
149 startedAt: Schema.NullOr(IsoDateTime),
150 finishedAt: Schema.NullOr(IsoDateTime),
151 message: Schema.NullOr(TrimmedNonEmptyString),
152 output: Schema.NullOr(Schema.String.check(Schema.isMaxLength(10_000))),
153});
154export type ServerProviderUpdateState = typeof ServerProviderUpdateState.Type;
155
156export const ServerProvider = Schema.Struct({
157 // Routing key for the configured instance this snapshot represents. This
158 // is the only stable identity consumers may use for provider routing.
159 instanceId: ProviderInstanceId,
160 // Open driver kind slug that selects the implementation handling this
161 // instance. It is metadata/capability context, not a routing key.
162 driver: ProviderDriverKind,
163 displayName: Schema.optional(TrimmedNonEmptyString),
164 accentColor: Schema.optional(TrimmedNonEmptyString),
165 badgeLabel: Schema.optional(TrimmedNonEmptyString),
166 continuation: Schema.optional(ServerProviderContinuation),
167 showInteractionModeToggle: Schema.optional(Schema.Boolean),
168 enabled: Schema.Boolean,
169 installed: Schema.Boolean,
170 version: Schema.NullOr(TrimmedNonEmptyString),
171 status: ServerProviderState,
172 auth: ServerProviderAuth,
173 checkedAt: IsoDateTime,
174 message: Schema.optional(TrimmedNonEmptyString),
175 // Optional for back-compat: every legacy producer omits this field and
176 // an absent value is interpreted as `"available"` by consumers (see
177 // `isProviderAvailable`). New `ProviderInstanceRegistry` outputs set it
178 // explicitly so the UI can render unavailable shadows from
179 // `ServerSettings.providerInstances`.
180 availability: Schema.optional(ServerProviderAvailability),
181 // Human-readable reason populated when `availability === "unavailable"`.
182 // Surfaces in the UI alongside the missing-driver affordance.
183 unavailableReason: Schema.optional(TrimmedNonEmptyString),
184 models: Schema.Array(ServerProviderModel),
185 slashCommands: Schema.Array(ServerProviderSlashCommand).pipe(
186 Schema.withDecodingDefault(Effect.succeed([])),
187 ),
188 skills: Schema.Array(ServerProviderSkill).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
189 versionAdvisory: Schema.optionalKey(ServerProviderVersionAdvisory),
190 updateState: Schema.optionalKey(ServerProviderUpdateState),
191});
192export type ServerProvider = typeof ServerProvider.Type;
193
194export const ServerProviders = Schema.Array(ServerProvider);
195export type ServerProviders = typeof ServerProviders.Type;
196
197/**
198 * Treat the optional `availability` as "available" when absent. This is
199 * the rule legacy producers (which omit the field) and new producers
200 * (which set it explicitly) agree on so consumers never have to thread
201 * `?? "available"` defaults through their code paths.
202 */
203export const isProviderAvailable = (snapshot: ServerProvider): boolean =>
204 snapshot.availability !== "unavailable";
205
206export const ServerObservability = Schema.Struct({
207 logsDirectoryPath: TrimmedNonEmptyString,
208 localTracingEnabled: Schema.Boolean,
209 otlpTracesUrl: Schema.optional(TrimmedNonEmptyString),
210 otlpTracesEnabled: Schema.Boolean,
211 otlpMetricsUrl: Schema.optional(TrimmedNonEmptyString),
212 otlpMetricsEnabled: Schema.Boolean,
213});
214export type ServerObservability = typeof ServerObservability.Type;
215
216export const ServerTraceDiagnosticsErrorKind = Schema.Literals([
217 "trace-file-not-found",
218 "trace-file-read-failed",
219]);
220export type ServerTraceDiagnosticsErrorKind = typeof ServerTraceDiagnosticsErrorKind.Type;
221
222export const ServerTraceDiagnosticsSpanSummary = Schema.Struct({
223 name: TrimmedNonEmptyString,
224 count: NonNegativeInt,
225 failureCount: NonNegativeInt,
226 totalDurationMs: Schema.Number,
227 averageDurationMs: Schema.Number,
228 maxDurationMs: Schema.Number,
229});
230export type ServerTraceDiagnosticsSpanSummary = typeof ServerTraceDiagnosticsSpanSummary.Type;
231
232export const ServerTraceDiagnosticsFailureSummary = Schema.Struct({
233 name: TrimmedNonEmptyString,
234 cause: TrimmedNonEmptyString,
235 count: NonNegativeInt,
236 lastSeenAt: Schema.DateTimeUtc,
237 traceId: TrimmedNonEmptyString,
238 spanId: TrimmedNonEmptyString,
239});
240export type ServerTraceDiagnosticsFailureSummary = typeof ServerTraceDiagnosticsFailureSummary.Type;
241
242export const ServerTraceDiagnosticsRecentFailure = Schema.Struct({
243 name: TrimmedNonEmptyString,
244 cause: TrimmedNonEmptyString,
245 durationMs: Schema.Number,
246 endedAt: Schema.DateTimeUtc,
247 traceId: TrimmedNonEmptyString,
248 spanId: TrimmedNonEmptyString,
249});
250export type ServerTraceDiagnosticsRecentFailure = typeof ServerTraceDiagnosticsRecentFailure.Type;
251
252export const ServerTraceDiagnosticsSpanOccurrence = Schema.Struct({
253 name: TrimmedNonEmptyString,
254 durationMs: Schema.Number,
255 endedAt: Schema.DateTimeUtc,
256 traceId: TrimmedNonEmptyString,
257 spanId: TrimmedNonEmptyString,
258});
259export type ServerTraceDiagnosticsSpanOccurrence = typeof ServerTraceDiagnosticsSpanOccurrence.Type;
260
261export const ServerTraceDiagnosticsLogEvent = Schema.Struct({
262 spanName: TrimmedNonEmptyString,
263 level: TrimmedNonEmptyString,
264 message: TrimmedNonEmptyString,
265 seenAt: Schema.DateTimeUtc,
266 traceId: TrimmedNonEmptyString,
267 spanId: TrimmedNonEmptyString,
268});
269export type ServerTraceDiagnosticsLogEvent = typeof ServerTraceDiagnosticsLogEvent.Type;
270
271export const ServerTraceDiagnosticsResult = Schema.Struct({
272 traceFilePath: TrimmedNonEmptyString,
273 scannedFilePaths: Schema.Array(TrimmedNonEmptyString),
274 readAt: Schema.DateTimeUtc,
275 recordCount: NonNegativeInt,
276 parseErrorCount: NonNegativeInt,
277 firstSpanAt: Schema.Option(Schema.DateTimeUtc),
278 lastSpanAt: Schema.Option(Schema.DateTimeUtc),
279 failureCount: NonNegativeInt,
280 interruptionCount: NonNegativeInt,
281 slowSpanThresholdMs: NonNegativeInt,
282 slowSpanCount: NonNegativeInt,
283 logLevelCounts: Schema.Record(TrimmedNonEmptyString, NonNegativeInt),
284 topSpansByCount: Schema.Array(ServerTraceDiagnosticsSpanSummary),
285 slowestSpans: Schema.Array(ServerTraceDiagnosticsSpanOccurrence),
286 commonFailures: Schema.Array(ServerTraceDiagnosticsFailureSummary),
287 latestFailures: Schema.Array(ServerTraceDiagnosticsRecentFailure),
288 latestWarningAndErrorLogs: Schema.Array(ServerTraceDiagnosticsLogEvent),
289 partialFailure: Schema.Option(Schema.Boolean),
290 error: Schema.Option(
291 Schema.Struct({
292 kind: ServerTraceDiagnosticsErrorKind,
293 message: TrimmedNonEmptyString,
294 }),
295 ),
296});
297export type ServerTraceDiagnosticsResult = typeof ServerTraceDiagnosticsResult.Type;
298
299export const ServerProcessSignal = Schema.Literals(["SIGINT", "SIGKILL"]);
300export type ServerProcessSignal = typeof ServerProcessSignal.Type;
301
302export const ServerProcessDiagnosticsEntry = Schema.Struct({
303 pid: PositiveInt,
304 ppid: NonNegativeInt,
305 pgid: Schema.Option(Schema.Int),
306 status: TrimmedNonEmptyString,
307 cpuPercent: Schema.Number,
308 rssBytes: NonNegativeInt,
309 elapsed: TrimmedNonEmptyString,
310 command: TrimmedNonEmptyString,
311 depth: NonNegativeInt,
312 childPids: Schema.Array(PositiveInt),
313});
314export type ServerProcessDiagnosticsEntry = typeof ServerProcessDiagnosticsEntry.Type;
315
316export const ServerProcessDiagnosticsResult = Schema.Struct({
317 serverPid: PositiveInt,
318 readAt: Schema.DateTimeUtc,
319 processCount: NonNegativeInt,
320 totalRssBytes: NonNegativeInt,
321 totalCpuPercent: Schema.Number,
322 processes: Schema.Array(ServerProcessDiagnosticsEntry),
323 error: Schema.Option(
324 Schema.Struct({
325 message: TrimmedNonEmptyString,
326 }),
327 ),
328});
329export type ServerProcessDiagnosticsResult = typeof ServerProcessDiagnosticsResult.Type;
330
331export const ServerSignalProcessInput = Schema.Struct({
332 pid: PositiveInt,
333 signal: ServerProcessSignal,
334});
335export type ServerSignalProcessInput = typeof ServerSignalProcessInput.Type;
336
337export const ServerSignalProcessResult = Schema.Struct({
338 pid: PositiveInt,
339 signal: ServerProcessSignal,
340 signaled: Schema.Boolean,
341 message: Schema.Option(TrimmedNonEmptyString),
342});
343export type ServerSignalProcessResult = typeof ServerSignalProcessResult.Type;
344
345export const ServerConfig = Schema.Struct({
346 environment: ExecutionEnvironmentDescriptor,
347 auth: ServerAuthDescriptor,
348 cwd: TrimmedNonEmptyString,
349 keybindingsConfigPath: TrimmedNonEmptyString,
350 keybindings: ResolvedKeybindingsConfig,
351 issues: ServerConfigIssues,
352 providers: ServerProviders,
353 availableEditors: Schema.Array(EditorId),
354 observability: ServerObservability,
355 settings: ServerSettings,
356});
357export type ServerConfig = typeof ServerConfig.Type;
358
359const ServerUpsertKeybindingReplaceTarget = Schema.Struct({
360 key: KeybindingValue,
361 command: KeybindingCommand,
362 when: Schema.optional(KeybindingWhen),
363});
364
365export const ServerUpsertKeybindingInput = Schema.Struct({
366 key: KeybindingValue,
367 command: KeybindingCommand,
368 when: Schema.optional(KeybindingWhen),
369 replace: Schema.optional(ServerUpsertKeybindingReplaceTarget),
370});
371export type ServerUpsertKeybindingInput = typeof ServerUpsertKeybindingInput.Type;
372
373export const ServerRemoveKeybindingInput = ServerUpsertKeybindingReplaceTarget;
374export type ServerRemoveKeybindingInput = typeof ServerRemoveKeybindingInput.Type;
375
376export const ServerUpsertKeybindingResult = Schema.Struct({
377 keybindings: ResolvedKeybindingsConfig,
378 issues: ServerConfigIssues,
379});
380export type ServerUpsertKeybindingResult = typeof ServerUpsertKeybindingResult.Type;
381
382export const ServerRemoveKeybindingResult = ServerUpsertKeybindingResult;
383export type ServerRemoveKeybindingResult = typeof ServerRemoveKeybindingResult.Type;
384
385export const ServerConfigUpdatedPayload = Schema.Struct({
386 issues: ServerConfigIssues,
387 providers: ServerProviders,
388 settings: Schema.optional(ServerSettings),
389});
390export type ServerConfigUpdatedPayload = typeof ServerConfigUpdatedPayload.Type;
391
392export const ServerConfigKeybindingsUpdatedPayload = Schema.Struct({
393 keybindings: ResolvedKeybindingsConfig,
394 issues: ServerConfigIssues,
395});
396export type ServerConfigKeybindingsUpdatedPayload =
397 typeof ServerConfigKeybindingsUpdatedPayload.Type;
398
399export const ServerConfigProviderStatusesPayload = Schema.Struct({
400 providers: ServerProviders,
401});
402export type ServerConfigProviderStatusesPayload = typeof ServerConfigProviderStatusesPayload.Type;
403
404export const ServerConfigSettingsUpdatedPayload = Schema.Struct({
405 settings: ServerSettings,
406});
407export type ServerConfigSettingsUpdatedPayload = typeof ServerConfigSettingsUpdatedPayload.Type;
408
409export const ServerConfigStreamSnapshotEvent = Schema.Struct({
410 version: Schema.Literal(1),
411 type: Schema.Literal("snapshot"),
412 config: ServerConfig,
413});
414export type ServerConfigStreamSnapshotEvent = typeof ServerConfigStreamSnapshotEvent.Type;
415
416export const ServerConfigStreamKeybindingsUpdatedEvent = Schema.Struct({
417 version: Schema.Literal(1),
418 type: Schema.Literal("keybindingsUpdated"),
419 payload: ServerConfigKeybindingsUpdatedPayload,
420});
421export type ServerConfigStreamKeybindingsUpdatedEvent =
422 typeof ServerConfigStreamKeybindingsUpdatedEvent.Type;
423
424export const ServerConfigStreamProviderStatusesEvent = Schema.Struct({
425 version: Schema.Literal(1),
426 type: Schema.Literal("providerStatuses"),
427 payload: ServerConfigProviderStatusesPayload,
428});
429export type ServerConfigStreamProviderStatusesEvent =
430 typeof ServerConfigStreamProviderStatusesEvent.Type;
431
432export const ServerConfigStreamSettingsUpdatedEvent = Schema.Struct({
433 version: Schema.Literal(1),
434 type: Schema.Literal("settingsUpdated"),
435 payload: ServerConfigSettingsUpdatedPayload,
436});
437export type ServerConfigStreamSettingsUpdatedEvent =
438 typeof ServerConfigStreamSettingsUpdatedEvent.Type;
439
440export const ServerConfigStreamEvent = Schema.Union([
441 ServerConfigStreamSnapshotEvent,
442 ServerConfigStreamKeybindingsUpdatedEvent,
443 ServerConfigStreamProviderStatusesEvent,
444 ServerConfigStreamSettingsUpdatedEvent,
445]);
446export type ServerConfigStreamEvent = typeof ServerConfigStreamEvent.Type;
447
448export const ServerLifecycleReadyPayload = Schema.Struct({
449 at: IsoDateTime,
450 environment: ExecutionEnvironmentDescriptor,
451});
452export type ServerLifecycleReadyPayload = typeof ServerLifecycleReadyPayload.Type;
453
454export const ServerLifecycleWelcomePayload = Schema.Struct({
455 environment: ExecutionEnvironmentDescriptor,
456 cwd: TrimmedNonEmptyString,
457 projectName: TrimmedNonEmptyString,
458 bootstrapProjectId: Schema.optional(ProjectId),
459 bootstrapThreadId: Schema.optional(ThreadId),
460});
461export type ServerLifecycleWelcomePayload = typeof ServerLifecycleWelcomePayload.Type;
462
463export const ServerLifecycleStreamWelcomeEvent = Schema.Struct({
464 version: Schema.Literal(1),
465 sequence: NonNegativeInt,
466 type: Schema.Literal("welcome"),
467 payload: ServerLifecycleWelcomePayload,
468});
469export type ServerLifecycleStreamWelcomeEvent = typeof ServerLifecycleStreamWelcomeEvent.Type;
470
471export const ServerLifecycleStreamReadyEvent = Schema.Struct({
472 version: Schema.Literal(1),
473 sequence: NonNegativeInt,
474 type: Schema.Literal("ready"),
475 payload: ServerLifecycleReadyPayload,
476});
477export type ServerLifecycleStreamReadyEvent = typeof ServerLifecycleStreamReadyEvent.Type;
478
479export const ServerLifecycleStreamEvent = Schema.Union([
480 ServerLifecycleStreamWelcomeEvent,
481 ServerLifecycleStreamReadyEvent,
482]);
483export type ServerLifecycleStreamEvent = typeof ServerLifecycleStreamEvent.Type;
484
485export const ServerProviderUpdatedPayload = Schema.Struct({
486 providers: ServerProviders,
487});
488export type ServerProviderUpdatedPayload = typeof ServerProviderUpdatedPayload.Type;
489
490export const ServerProviderUpdateInput = Schema.Struct({
491 provider: ProviderDriverKind,
492 instanceId: Schema.optionalKey(ProviderInstanceId),
493});
494export type ServerProviderUpdateInput = typeof ServerProviderUpdateInput.Type;
495
496export class ServerProviderUpdateError extends Schema.TaggedErrorClass<ServerProviderUpdateError>()(
497 "ServerProviderUpdateError",
498 {
499 provider: ProviderDriverKind,
500 reason: TrimmedNonEmptyString,
501 cause: Schema.optional(Schema.Defect),
502 },
503) {
504 override get message(): string {
505 return `Provider update failed for ${this.provider}: ${this.reason}`;
506 }
507}