Files
t3code/packages/contracts/src/server.ts
T
2026-04-29 16:13:55 -07:00

296 lines
11 KiB
TypeScript

import { Effect, Schema } from "effect";
import { ExecutionEnvironmentDescriptor } from "./environment.ts";
import { ServerAuthDescriptor } from "./auth.ts";
import {
IsoDateTime,
NonNegativeInt,
ProjectId,
ThreadId,
TrimmedNonEmptyString,
} from "./baseSchemas.ts";
import { KeybindingRule, ResolvedKeybindingsConfig } from "./keybindings.ts";
import { EditorId } from "./editor.ts";
import { ModelCapabilities } from "./model.ts";
import { ProviderDriverKind, ProviderInstanceId } from "./providerInstance.ts";
import { ServerSettings } from "./settings.ts";
const KeybindingsMalformedConfigIssue = Schema.Struct({
kind: Schema.Literal("keybindings.malformed-config"),
message: TrimmedNonEmptyString,
});
const KeybindingsInvalidEntryIssue = Schema.Struct({
kind: Schema.Literal("keybindings.invalid-entry"),
message: TrimmedNonEmptyString,
index: Schema.Number,
});
export const ServerConfigIssue = Schema.Union([
KeybindingsMalformedConfigIssue,
KeybindingsInvalidEntryIssue,
]);
export type ServerConfigIssue = typeof ServerConfigIssue.Type;
const ServerConfigIssues = Schema.Array(ServerConfigIssue);
export const ServerProviderState = Schema.Literals(["ready", "warning", "error", "disabled"]);
export type ServerProviderState = typeof ServerProviderState.Type;
export const ServerProviderAuthStatus = Schema.Literals([
"authenticated",
"unauthenticated",
"unknown",
]);
export type ServerProviderAuthStatus = typeof ServerProviderAuthStatus.Type;
export const ServerProviderAuth = Schema.Struct({
status: ServerProviderAuthStatus,
type: Schema.optional(TrimmedNonEmptyString),
label: Schema.optional(TrimmedNonEmptyString),
email: Schema.optional(TrimmedNonEmptyString),
});
export type ServerProviderAuth = typeof ServerProviderAuth.Type;
export const ServerProviderModel = Schema.Struct({
slug: TrimmedNonEmptyString,
name: TrimmedNonEmptyString,
shortName: Schema.optional(TrimmedNonEmptyString),
subProvider: Schema.optional(TrimmedNonEmptyString),
isCustom: Schema.Boolean,
capabilities: Schema.NullOr(ModelCapabilities),
});
export type ServerProviderModel = typeof ServerProviderModel.Type;
export const ServerProviderSlashCommandInput = Schema.Struct({
hint: TrimmedNonEmptyString,
});
export type ServerProviderSlashCommandInput = typeof ServerProviderSlashCommandInput.Type;
export const ServerProviderSlashCommand = Schema.Struct({
name: TrimmedNonEmptyString,
description: Schema.optional(TrimmedNonEmptyString),
input: Schema.optional(ServerProviderSlashCommandInput),
});
export type ServerProviderSlashCommand = typeof ServerProviderSlashCommand.Type;
export const ServerProviderSkill = Schema.Struct({
name: TrimmedNonEmptyString,
description: Schema.optional(TrimmedNonEmptyString),
path: TrimmedNonEmptyString,
scope: Schema.optional(TrimmedNonEmptyString),
enabled: Schema.Boolean,
displayName: Schema.optional(TrimmedNonEmptyString),
shortDescription: Schema.optional(TrimmedNonEmptyString),
});
export type ServerProviderSkill = typeof ServerProviderSkill.Type;
/**
* Availability of a configured provider instance from the runtime's POV.
*
* - `available` — the build ships this driver and an instance is wired
* up. Default for legacy snapshots produced from the closed
* `ServerSettings.providers` map.
* - `unavailable` — the user's `ServerSettings.providerInstances` (or a
* persisted thread / session binding) references a driver this build
* doesn't ship. Common after rolling back from a fork or PR branch
* that introduced a new driver. The snapshot is preserved so the UI
* can render "missing driver" affordances and so the data round-trips
* when the user moves back to the fork.
*
* Snapshots with `availability: "unavailable"` MUST set
* `installed: false` and `enabled: false`; the runtime refuses turn
* starts against them with a structured error.
*/
export const ServerProviderAvailability = Schema.Literals(["available", "unavailable"]);
export type ServerProviderAvailability = typeof ServerProviderAvailability.Type;
export const ServerProviderContinuation = Schema.Struct({
groupKey: TrimmedNonEmptyString,
});
export type ServerProviderContinuation = typeof ServerProviderContinuation.Type;
export const ServerProvider = Schema.Struct({
// Routing key for the configured instance this snapshot represents. This
// is the only stable identity consumers may use for provider routing.
instanceId: ProviderInstanceId,
// Open driver kind slug that selects the implementation handling this
// instance. It is metadata/capability context, not a routing key.
driver: ProviderDriverKind,
displayName: Schema.optional(TrimmedNonEmptyString),
accentColor: Schema.optional(TrimmedNonEmptyString),
badgeLabel: Schema.optional(TrimmedNonEmptyString),
continuation: Schema.optional(ServerProviderContinuation),
showInteractionModeToggle: Schema.optional(Schema.Boolean),
enabled: Schema.Boolean,
installed: Schema.Boolean,
version: Schema.NullOr(TrimmedNonEmptyString),
status: ServerProviderState,
auth: ServerProviderAuth,
checkedAt: IsoDateTime,
message: Schema.optional(TrimmedNonEmptyString),
// Optional for back-compat: every legacy producer omits this field and
// an absent value is interpreted as `"available"` by consumers (see
// `isProviderAvailable`). New `ProviderInstanceRegistry` outputs set it
// explicitly so the UI can render unavailable shadows from
// `ServerSettings.providerInstances`.
availability: Schema.optional(ServerProviderAvailability),
// Human-readable reason populated when `availability === "unavailable"`.
// Surfaces in the UI alongside the missing-driver affordance.
unavailableReason: Schema.optional(TrimmedNonEmptyString),
models: Schema.Array(ServerProviderModel),
slashCommands: Schema.Array(ServerProviderSlashCommand).pipe(
Schema.withDecodingDefault(Effect.succeed([])),
),
skills: Schema.Array(ServerProviderSkill).pipe(Schema.withDecodingDefault(Effect.succeed([]))),
});
export type ServerProvider = typeof ServerProvider.Type;
export const ServerProviders = Schema.Array(ServerProvider);
export type ServerProviders = typeof ServerProviders.Type;
/**
* Treat the optional `availability` as "available" when absent. This is
* the rule legacy producers (which omit the field) and new producers
* (which set it explicitly) agree on so consumers never have to thread
* `?? "available"` defaults through their code paths.
*/
export const isProviderAvailable = (snapshot: ServerProvider): boolean =>
snapshot.availability !== "unavailable";
export const ServerObservability = Schema.Struct({
logsDirectoryPath: TrimmedNonEmptyString,
localTracingEnabled: Schema.Boolean,
otlpTracesUrl: Schema.optional(TrimmedNonEmptyString),
otlpTracesEnabled: Schema.Boolean,
otlpMetricsUrl: Schema.optional(TrimmedNonEmptyString),
otlpMetricsEnabled: Schema.Boolean,
});
export type ServerObservability = typeof ServerObservability.Type;
export const ServerConfig = Schema.Struct({
environment: ExecutionEnvironmentDescriptor,
auth: ServerAuthDescriptor,
cwd: TrimmedNonEmptyString,
keybindingsConfigPath: TrimmedNonEmptyString,
keybindings: ResolvedKeybindingsConfig,
issues: ServerConfigIssues,
providers: ServerProviders,
availableEditors: Schema.Array(EditorId),
observability: ServerObservability,
settings: ServerSettings,
});
export type ServerConfig = typeof ServerConfig.Type;
export const ServerUpsertKeybindingInput = KeybindingRule;
export type ServerUpsertKeybindingInput = typeof ServerUpsertKeybindingInput.Type;
export const ServerUpsertKeybindingResult = Schema.Struct({
keybindings: ResolvedKeybindingsConfig,
issues: ServerConfigIssues,
});
export type ServerUpsertKeybindingResult = typeof ServerUpsertKeybindingResult.Type;
export const ServerConfigUpdatedPayload = Schema.Struct({
issues: ServerConfigIssues,
providers: ServerProviders,
settings: Schema.optional(ServerSettings),
});
export type ServerConfigUpdatedPayload = typeof ServerConfigUpdatedPayload.Type;
export const ServerConfigKeybindingsUpdatedPayload = Schema.Struct({
issues: ServerConfigIssues,
});
export type ServerConfigKeybindingsUpdatedPayload =
typeof ServerConfigKeybindingsUpdatedPayload.Type;
export const ServerConfigProviderStatusesPayload = Schema.Struct({
providers: ServerProviders,
});
export type ServerConfigProviderStatusesPayload = typeof ServerConfigProviderStatusesPayload.Type;
export const ServerConfigSettingsUpdatedPayload = Schema.Struct({
settings: ServerSettings,
});
export type ServerConfigSettingsUpdatedPayload = typeof ServerConfigSettingsUpdatedPayload.Type;
export const ServerConfigStreamSnapshotEvent = Schema.Struct({
version: Schema.Literal(1),
type: Schema.Literal("snapshot"),
config: ServerConfig,
});
export type ServerConfigStreamSnapshotEvent = typeof ServerConfigStreamSnapshotEvent.Type;
export const ServerConfigStreamKeybindingsUpdatedEvent = Schema.Struct({
version: Schema.Literal(1),
type: Schema.Literal("keybindingsUpdated"),
payload: ServerConfigKeybindingsUpdatedPayload,
});
export type ServerConfigStreamKeybindingsUpdatedEvent =
typeof ServerConfigStreamKeybindingsUpdatedEvent.Type;
export const ServerConfigStreamProviderStatusesEvent = Schema.Struct({
version: Schema.Literal(1),
type: Schema.Literal("providerStatuses"),
payload: ServerConfigProviderStatusesPayload,
});
export type ServerConfigStreamProviderStatusesEvent =
typeof ServerConfigStreamProviderStatusesEvent.Type;
export const ServerConfigStreamSettingsUpdatedEvent = Schema.Struct({
version: Schema.Literal(1),
type: Schema.Literal("settingsUpdated"),
payload: ServerConfigSettingsUpdatedPayload,
});
export type ServerConfigStreamSettingsUpdatedEvent =
typeof ServerConfigStreamSettingsUpdatedEvent.Type;
export const ServerConfigStreamEvent = Schema.Union([
ServerConfigStreamSnapshotEvent,
ServerConfigStreamKeybindingsUpdatedEvent,
ServerConfigStreamProviderStatusesEvent,
ServerConfigStreamSettingsUpdatedEvent,
]);
export type ServerConfigStreamEvent = typeof ServerConfigStreamEvent.Type;
export const ServerLifecycleReadyPayload = Schema.Struct({
at: IsoDateTime,
environment: ExecutionEnvironmentDescriptor,
});
export type ServerLifecycleReadyPayload = typeof ServerLifecycleReadyPayload.Type;
export const ServerLifecycleWelcomePayload = Schema.Struct({
environment: ExecutionEnvironmentDescriptor,
cwd: TrimmedNonEmptyString,
projectName: TrimmedNonEmptyString,
bootstrapProjectId: Schema.optional(ProjectId),
bootstrapThreadId: Schema.optional(ThreadId),
});
export type ServerLifecycleWelcomePayload = typeof ServerLifecycleWelcomePayload.Type;
export const ServerLifecycleStreamWelcomeEvent = Schema.Struct({
version: Schema.Literal(1),
sequence: NonNegativeInt,
type: Schema.Literal("welcome"),
payload: ServerLifecycleWelcomePayload,
});
export type ServerLifecycleStreamWelcomeEvent = typeof ServerLifecycleStreamWelcomeEvent.Type;
export const ServerLifecycleStreamReadyEvent = Schema.Struct({
version: Schema.Literal(1),
sequence: NonNegativeInt,
type: Schema.Literal("ready"),
payload: ServerLifecycleReadyPayload,
});
export type ServerLifecycleStreamReadyEvent = typeof ServerLifecycleStreamReadyEvent.Type;
export const ServerLifecycleStreamEvent = Schema.Union([
ServerLifecycleStreamWelcomeEvent,
ServerLifecycleStreamReadyEvent,
]);
export type ServerLifecycleStreamEvent = typeof ServerLifecycleStreamEvent.Type;
export const ServerProviderUpdatedPayload = Schema.Struct({
providers: ServerProviders,
});
export type ServerProviderUpdatedPayload = typeof ServerProviderUpdatedPayload.Type;