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

ipc.ts

568 lines20.1 KB

1import type {
2 VcsSwitchRefInput,
3 VcsSwitchRefResult,
4 VcsCreateRefInput,
5 GitPreparePullRequestThreadInput,
6 GitPreparePullRequestThreadResult,
7 GitPullRequestRefInput,
8 VcsCreateWorktreeInput,
9 VcsCreateWorktreeResult,
10 VcsInitInput,
11 VcsListRefsInput,
12 VcsListRefsResult,
13 VcsPullInput,
14 VcsPullResult,
15 VcsRemoveWorktreeInput,
16 GitResolvePullRequestResult,
17 VcsStatusInput,
18 VcsStatusResult,
19 VcsCreateRefResult,
20} from "./git.ts";
21import type { FilesystemBrowseInput, FilesystemBrowseResult } from "./filesystem.ts";
22import type {
23 ProjectSearchEntriesInput,
24 ProjectSearchEntriesResult,
25 ProjectWriteFileInput,
26 ProjectWriteFileResult,
27} from "./project.ts";
28import type { ProviderInstanceId } from "./providerInstance.ts";
29import type {
30 ServerConfig,
31 ServerProcessDiagnosticsResult,
32 ServerProcessResourceHistoryInput,
33 ServerProcessResourceHistoryResult,
34 ServerProviderUpdateInput,
35 ServerProviderUpdatedPayload,
36 ServerRemoveKeybindingResult,
37 ServerSignalProcessInput,
38 ServerSignalProcessResult,
39 ServerTraceDiagnosticsResult,
40 ServerUpsertKeybindingResult,
41} from "./server.ts";
42import type {
43 TerminalClearInput,
44 TerminalCloseInput,
45 TerminalEvent,
46 TerminalOpenInput,
47 TerminalResizeInput,
48 TerminalRestartInput,
49 TerminalSessionSnapshot,
50 TerminalWriteInput,
51} from "./terminal.ts";
52import type { ServerRemoveKeybindingInput, ServerUpsertKeybindingInput } from "./server.ts";
53import * as Schema from "effect/Schema";
54import type {
55 ClientOrchestrationCommand,
56 OrchestrationGetFullThreadDiffInput,
57 OrchestrationGetFullThreadDiffResult,
58 OrchestrationGetTurnDiffInput,
59 OrchestrationGetTurnDiffResult,
60 OrchestrationShellSnapshot,
61 OrchestrationShellStreamItem,
62 OrchestrationSubscribeThreadInput,
63 OrchestrationThreadStreamItem,
64} from "./orchestration.ts";
65import { EnvironmentId } from "./baseSchemas.ts";
66import { AuthBearerBootstrapResult, AuthSessionState, AuthWebSocketTokenResult } from "./auth.ts";
67import { AdvertisedEndpoint } from "./remoteAccess.ts";
68import { EditorId } from "./editor.ts";
69import { ExecutionEnvironmentDescriptor } from "./environment.ts";
70import type { ClientSettings, ServerSettings, ServerSettingsPatch } from "./settings.ts";
71import type {
72 SourceControlCloneRepositoryInput,
73 SourceControlCloneRepositoryResult,
74 SourceControlDiscoveryResult,
75 SourceControlPublishRepositoryInput,
76 SourceControlPublishRepositoryResult,
77 SourceControlRepositoryInfo,
78 SourceControlRepositoryLookupInput,
79} from "./sourceControl.ts";
80​
81export interface ContextMenuItem<T extends string = string> {
82 id: T;
83 label: string;
84 destructive?: boolean;
85 disabled?: boolean;
86 children?: readonly ContextMenuItem<T>[];
88​
89export interface ContextMenuItemSchemaType {
90 readonly id: string;
91 readonly label: string;
92 readonly destructive?: boolean;
93 readonly disabled?: boolean;
94 readonly children?: readonly ContextMenuItemSchemaType[];
96​
97export const ContextMenuItemSchema: Schema.Codec<ContextMenuItemSchemaType> = Schema.Struct({
98 id: Schema.String,
99 label: Schema.String,
100 destructive: Schema.optionalKey(Schema.Boolean),
101 disabled: Schema.optionalKey(Schema.Boolean),
102 children: Schema.optionalKey(
103 Schema.Array(
104 Schema.suspend((): Schema.Codec<ContextMenuItemSchemaType> => ContextMenuItemSchema),
105 ),
106 ),
107});
108​
109export type DesktopUpdateStatus =
110 | "disabled"
111 | "idle"
112 | "checking"
113 | "up-to-date"
114 | "available"
115 | "downloading"
116 | "downloaded"
117 | "error";
118​
119export type DesktopRuntimeArch = "arm64" | "x64" | "other";
120export type DesktopTheme = "light" | "dark" | "system";
121export type DesktopUpdateChannel = "latest" | "nightly";
122export type DesktopAppStageLabel = "Bscholar" | "Dev" | "Nightly";
123​
124export const DesktopUpdateStatusSchema = Schema.Literals([
125 "disabled",
126 "idle",
127 "checking",
128 "up-to-date",
129 "available",
130 "downloading",
131 "downloaded",
132 "error",
133]);
134export const DesktopRuntimeArchSchema = Schema.Literals(["arm64", "x64", "other"]);
135export const DesktopThemeSchema = Schema.Literals(["light", "dark", "system"]);
136export const DesktopUpdateChannelSchema = Schema.Literals(["latest", "nightly"]);
137export const DesktopAppStageLabelSchema = Schema.Literals(["Bscholar", "Dev", "Nightly"]);
138​
139export interface DesktopAppBranding {
140 baseName: string;
141 stageLabel: DesktopAppStageLabel;
142 displayName: string;
144​
145export const DesktopAppBrandingSchema = Schema.Struct({
146 baseName: Schema.String,
147 stageLabel: DesktopAppStageLabelSchema,
148 displayName: Schema.String,
149});
150​
151export interface DesktopRuntimeInfo {
152 hostArch: DesktopRuntimeArch;
153 appArch: DesktopRuntimeArch;
154 runningUnderArm64Translation: boolean;
156​
157export const DesktopRuntimeInfoSchema = Schema.Struct({
158 hostArch: DesktopRuntimeArchSchema,
159 appArch: DesktopRuntimeArchSchema,
160 runningUnderArm64Translation: Schema.Boolean,
161});
162​
163export interface DesktopUpdateState {
164 enabled: boolean;
165 status: DesktopUpdateStatus;
166 channel: DesktopUpdateChannel;
167 currentVersion: string;
168 hostArch: DesktopRuntimeArch;
169 appArch: DesktopRuntimeArch;
170 runningUnderArm64Translation: boolean;
171 availableVersion: string | null;
172 downloadedVersion: string | null;
173 downloadPercent: number | null;
174 checkedAt: string | null;
175 message: string | null;
176 errorContext: "check" | "download" | "install" | null;
177 canRetry: boolean;
179​
180export const DesktopUpdateStateSchema = Schema.Struct({
181 enabled: Schema.Boolean,
182 status: DesktopUpdateStatusSchema,
183 channel: DesktopUpdateChannelSchema,
184 currentVersion: Schema.String,
185 hostArch: DesktopRuntimeArchSchema,
186 appArch: DesktopRuntimeArchSchema,
187 runningUnderArm64Translation: Schema.Boolean,
188 availableVersion: Schema.NullOr(Schema.String),
189 downloadedVersion: Schema.NullOr(Schema.String),
190 downloadPercent: Schema.NullOr(Schema.Number),
191 checkedAt: Schema.NullOr(Schema.String),
192 message: Schema.NullOr(Schema.String),
193 errorContext: Schema.NullOr(Schema.Literals(["check", "download", "install"])),
194 canRetry: Schema.Boolean,
195});
196​
197export interface DesktopUpdateActionResult {
198 accepted: boolean;
199 completed: boolean;
200 state: DesktopUpdateState;
202​
203export const DesktopUpdateActionResultSchema = Schema.Struct({
204 accepted: Schema.Boolean,
205 completed: Schema.Boolean,
206 state: DesktopUpdateStateSchema,
207});
208​
209export interface DesktopUpdateCheckResult {
210 checked: boolean;
211 state: DesktopUpdateState;
213​
214export const DesktopUpdateCheckResultSchema = Schema.Struct({
215 checked: Schema.Boolean,
216 state: DesktopUpdateStateSchema,
217});
218​
219export interface DesktopEnvironmentBootstrap {
220 label: string;
221 httpBaseUrl: string | null;
222 wsBaseUrl: string | null;
223 bootstrapToken?: string;
225​
226export const DesktopEnvironmentBootstrapSchema = Schema.Struct({
227 label: Schema.String,
228 httpBaseUrl: Schema.NullOr(Schema.String),
229 wsBaseUrl: Schema.NullOr(Schema.String),
230 bootstrapToken: Schema.optionalKey(Schema.String),
231});
232​
233export const DesktopSshEnvironmentTargetSchema = Schema.Struct({
234 alias: Schema.String,
235 hostname: Schema.String,
236 username: Schema.NullOr(Schema.String),
237 port: Schema.NullOr(Schema.Number),
238});
239export type DesktopSshEnvironmentTarget = typeof DesktopSshEnvironmentTargetSchema.Type;
240​
241export type DesktopSshHostSource = "ssh-config" | "known-hosts";
242export const DesktopSshHostSourceSchema = Schema.Literals(["ssh-config", "known-hosts"]);
243​
244export interface DesktopDiscoveredSshHost extends DesktopSshEnvironmentTarget {
245 source: DesktopSshHostSource;
247​
248export const DesktopDiscoveredSshHostSchema = Schema.Struct({
249 alias: Schema.String,
250 hostname: Schema.String,
251 username: Schema.NullOr(Schema.String),
252 port: Schema.NullOr(Schema.Number),
253 source: DesktopSshHostSourceSchema,
254});
255​
256export interface DesktopSshEnvironmentBootstrap {
257 target: DesktopSshEnvironmentTarget;
258 httpBaseUrl: string;
259 wsBaseUrl: string;
260 pairingToken: string | null;
261 remotePort?: number;
262 remoteServerKind?: "external" | "managed";
264​
265export const DesktopSshEnvironmentBootstrapSchema = Schema.Struct({
266 target: DesktopSshEnvironmentTargetSchema,
267 httpBaseUrl: Schema.String,
268 wsBaseUrl: Schema.String,
269 pairingToken: Schema.NullOr(Schema.String),
270 remotePort: Schema.optionalKey(Schema.Number),
271 remoteServerKind: Schema.optionalKey(Schema.Literals(["external", "managed"])),
272});
273​
274export interface DesktopSshPasswordPromptRequest {
275 requestId: string;
276 destination: string;
277 username: string | null;
278 prompt: string;
279 expiresAt: string;
281​
282export const DesktopSshPasswordPromptRequestSchema = Schema.Struct({
283 requestId: Schema.String,
284 destination: Schema.String,
285 username: Schema.NullOr(Schema.String),
286 prompt: Schema.String,
287 expiresAt: Schema.String,
288});
289​
290export const DesktopSshPasswordPromptCancelledType = "ssh-password-prompt-cancelled" as const;
291​
292export const DesktopSshPasswordPromptCancelledResultSchema = Schema.Struct({
293 type: Schema.Literal(DesktopSshPasswordPromptCancelledType),
294 message: Schema.String,
295});
296​
297export const DesktopSshEnvironmentEnsureOptionsSchema = Schema.Struct({
298 issuePairingToken: Schema.optionalKey(Schema.Boolean),
299});
300​
301export const DesktopSshEnvironmentEnsureInputSchema = Schema.Struct({
302 target: DesktopSshEnvironmentTargetSchema,
303 options: Schema.optionalKey(DesktopSshEnvironmentEnsureOptionsSchema),
304});
305​
306export const DesktopSshEnvironmentEnsureResultSchema = Schema.Union([
307 DesktopSshEnvironmentBootstrapSchema,
308 DesktopSshPasswordPromptCancelledResultSchema,
309]);
310​
311export const DesktopSshHttpBaseUrlInputSchema = Schema.Struct({
312 httpBaseUrl: Schema.String,
313});
314​
315export const DesktopSshBearerRequestInputSchema = Schema.Struct({
316 httpBaseUrl: Schema.String,
317 bearerToken: Schema.String,
318});
319​
320export const DesktopSshBearerBootstrapInputSchema = Schema.Struct({
321 httpBaseUrl: Schema.String,
322 credential: Schema.String,
323});
324​
325export const DesktopSshPasswordPromptResolutionInputSchema = Schema.Struct({
326 requestId: Schema.String,
327 password: Schema.NullOr(Schema.String),
328});
329​
330export const PersistedSavedEnvironmentRecordSchema = Schema.Struct({
331 environmentId: EnvironmentId,
332 label: Schema.String,
333 wsBaseUrl: Schema.String,
334 httpBaseUrl: Schema.String,
335 createdAt: Schema.String,
336 lastConnectedAt: Schema.NullOr(Schema.String),
337 desktopSsh: Schema.optionalKey(DesktopSshEnvironmentTargetSchema),
338});
339export type PersistedSavedEnvironmentRecord = typeof PersistedSavedEnvironmentRecordSchema.Type;
340​
341export type DesktopServerExposureMode = "local-only" | "network-accessible";
342​
343export const DesktopServerExposureModeSchema = Schema.Literals([
344 "local-only",
345 "network-accessible",
346]);
347​
348export interface DesktopServerExposureState {
349 mode: DesktopServerExposureMode;
350 endpointUrl: string | null;
351 advertisedHost: string | null;
352 tailscaleServeEnabled: boolean;
353 tailscaleServePort: number;
355​
356export const DesktopServerExposureStateSchema = Schema.Struct({
357 mode: DesktopServerExposureModeSchema,
358 endpointUrl: Schema.NullOr(Schema.String),
359 advertisedHost: Schema.NullOr(Schema.String),
360 tailscaleServeEnabled: Schema.Boolean,
361 tailscaleServePort: Schema.Number,
362});
363​
364export interface PickFolderOptions {
365 initialPath?: string | null;
367​
368export const PickFolderOptionsSchema = Schema.Struct({
369 initialPath: Schema.optionalKey(Schema.NullOr(Schema.String)),
370});
371​
372export interface DesktopBridge {
373 getAppBranding: () => DesktopAppBranding | null;
374 getLocalEnvironmentBootstrap: () => DesktopEnvironmentBootstrap | null;
375 getClientSettings: () => Promise<ClientSettings | null>;
376 setClientSettings: (settings: ClientSettings) => Promise<void>;
377 getSavedEnvironmentRegistry: () => Promise<readonly PersistedSavedEnvironmentRecord[]>;
378 setSavedEnvironmentRegistry: (
379 records: readonly PersistedSavedEnvironmentRecord[],
380 ) => Promise<void>;
381 getSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise<string | null>;
382 setSavedEnvironmentSecret: (environmentId: EnvironmentId, secret: string) => Promise<boolean>;
383 removeSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise<void>;
384 discoverSshHosts: () => Promise<readonly DesktopDiscoveredSshHost[]>;
385 ensureSshEnvironment: (
386 target: DesktopSshEnvironmentTarget,
387 options?: { issuePairingToken?: boolean },
388 ) => Promise<DesktopSshEnvironmentBootstrap>;
389 disconnectSshEnvironment: (target: DesktopSshEnvironmentTarget) => Promise<void>;
390 fetchSshEnvironmentDescriptor: (httpBaseUrl: string) => Promise<ExecutionEnvironmentDescriptor>;
391 bootstrapSshBearerSession: (
392 httpBaseUrl: string,
393 credential: string,
394 ) => Promise<AuthBearerBootstrapResult>;
395 fetchSshSessionState: (httpBaseUrl: string, bearerToken: string) => Promise<AuthSessionState>;
396 issueSshWebSocketToken: (
397 httpBaseUrl: string,
398 bearerToken: string,
399 ) => Promise<AuthWebSocketTokenResult>;
400 onSshPasswordPrompt: (listener: (request: DesktopSshPasswordPromptRequest) => void) => () => void;
401 resolveSshPasswordPrompt: (requestId: string, password: string | null) => Promise<void>;
402 getServerExposureState: () => Promise<DesktopServerExposureState>;
403 setServerExposureMode: (mode: DesktopServerExposureMode) => Promise<DesktopServerExposureState>;
404 setTailscaleServeEnabled: (input: {
405 readonly enabled: boolean;
406 readonly port?: number;
407 }) => Promise<DesktopServerExposureState>;
408 getAdvertisedEndpoints: () => Promise<readonly AdvertisedEndpoint[]>;
409 pickFolder: (options?: PickFolderOptions) => Promise<string | null>;
410 confirm: (message: string) => Promise<boolean>;
411 setTheme: (theme: DesktopTheme) => Promise<void>;
412 showContextMenu: <T extends string>(
413 items: readonly ContextMenuItem<T>[],
414 position?: { x: number; y: number },
415 ) => Promise<T | null>;
416 openExternal: (url: string) => Promise<boolean>;
417 onMenuAction: (listener: (action: string) => void) => () => void;
418 getUpdateState: () => Promise<DesktopUpdateState>;
419 setUpdateChannel: (channel: DesktopUpdateChannel) => Promise<DesktopUpdateState>;
420 checkForUpdate: () => Promise<DesktopUpdateCheckResult>;
421 downloadUpdate: () => Promise<DesktopUpdateActionResult>;
422 installUpdate: () => Promise<DesktopUpdateActionResult>;
423 onUpdateState: (listener: (state: DesktopUpdateState) => void) => () => void;
425​
426/**
427 * APIs bound to the local app shell, not to any particular backend environment.
428 *
429 * These capabilities describe the desktop/browser host that the user is
430 * currently running: dialogs, editor/external-link opening, context menus, and
431 * app-level settings/config access. They must not be used as a proxy for
432 * "whatever environment the user is targeting", because in a multi-environment
433 * world the local shell and a selected backend environment are distinct
434 * concepts.
435 */
436export interface LocalApi {
437 dialogs: {
438 pickFolder: (options?: PickFolderOptions) => Promise<string | null>;
439 confirm: (message: string) => Promise<boolean>;
440 };
441 shell: {
442 openInEditor: (cwd: string, editor: EditorId) => Promise<void>;
443 openExternal: (url: string) => Promise<void>;
444 };
445 contextMenu: {
446 show: <T extends string>(
447 items: readonly ContextMenuItem<T>[],
448 position?: { x: number; y: number },
449 ) => Promise<T | null>;
450 };
451 persistence: {
452 getClientSettings: () => Promise<ClientSettings | null>;
453 setClientSettings: (settings: ClientSettings) => Promise<void>;
454 getSavedEnvironmentRegistry: () => Promise<readonly PersistedSavedEnvironmentRecord[]>;
455 setSavedEnvironmentRegistry: (
456 records: readonly PersistedSavedEnvironmentRecord[],
457 ) => Promise<void>;
458 getSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise<string | null>;
459 setSavedEnvironmentSecret: (environmentId: EnvironmentId, secret: string) => Promise<boolean>;
460 removeSavedEnvironmentSecret: (environmentId: EnvironmentId) => Promise<void>;
461 };
462 server: {
463 getConfig: () => Promise<ServerConfig>;
464 /**
465 * Refresh provider snapshots. When `input.instanceId` is supplied only that
466 * configured instance is probed; otherwise every configured instance is
467 * refreshed (legacy untargeted refresh).
468 */
469 refreshProviders: (input?: {
470 readonly instanceId?: ProviderInstanceId;
471 }) => Promise<ServerProviderUpdatedPayload>;
472 updateProvider: (input: ServerProviderUpdateInput) => Promise<ServerProviderUpdatedPayload>;
473 upsertKeybinding: (input: ServerUpsertKeybindingInput) => Promise<ServerUpsertKeybindingResult>;
474 removeKeybinding: (input: ServerRemoveKeybindingInput) => Promise<ServerRemoveKeybindingResult>;
475 getSettings: () => Promise<ServerSettings>;
476 updateSettings: (patch: ServerSettingsPatch) => Promise<ServerSettings>;
477 discoverSourceControl: () => Promise<SourceControlDiscoveryResult>;
478 getTraceDiagnostics: () => Promise<ServerTraceDiagnosticsResult>;
479 getProcessDiagnostics: () => Promise<ServerProcessDiagnosticsResult>;
480 getProcessResourceHistory: (
481 input: ServerProcessResourceHistoryInput,
482 ) => Promise<ServerProcessResourceHistoryResult>;
483 signalProcess: (input: ServerSignalProcessInput) => Promise<ServerSignalProcessResult>;
484 };
486​
487/**
488 * APIs bound to a specific backend environment connection.
489 *
490 * These operations must always be routed with explicit environment context.
491 * They represent remote stateful capabilities such as orchestration, terminal,
492 * project, VCS, and provider operations. In multi-environment mode, each environment gets
493 * its own instance of this surface, and callers should resolve it by
494 * `environmentId` rather than reaching through the local desktop bridge.
495 */
496export interface EnvironmentApi {
497 terminal: {
498 open: (input: typeof TerminalOpenInput.Encoded) => Promise<TerminalSessionSnapshot>;
499 write: (input: typeof TerminalWriteInput.Encoded) => Promise<void>;
500 resize: (input: typeof TerminalResizeInput.Encoded) => Promise<void>;
501 clear: (input: typeof TerminalClearInput.Encoded) => Promise<void>;
502 restart: (input: typeof TerminalRestartInput.Encoded) => Promise<TerminalSessionSnapshot>;
503 close: (input: typeof TerminalCloseInput.Encoded) => Promise<void>;
504 onEvent: (callback: (event: TerminalEvent) => void) => () => void;
505 };
506 projects: {
507 searchEntries: (input: ProjectSearchEntriesInput) => Promise<ProjectSearchEntriesResult>;
508 writeFile: (input: ProjectWriteFileInput) => Promise<ProjectWriteFileResult>;
509 };
510 filesystem: {
511 browse: (input: FilesystemBrowseInput) => Promise<FilesystemBrowseResult>;
512 };
513 sourceControl: {
514 lookupRepository: (
515 input: SourceControlRepositoryLookupInput,
516 ) => Promise<SourceControlRepositoryInfo>;
517 cloneRepository: (
518 input: SourceControlCloneRepositoryInput,
519 ) => Promise<SourceControlCloneRepositoryResult>;
520 publishRepository: (
521 input: SourceControlPublishRepositoryInput,
522 ) => Promise<SourceControlPublishRepositoryResult>;
523 };
524 vcs: {
525 listRefs: (input: VcsListRefsInput) => Promise<VcsListRefsResult>;
526 createWorktree: (input: VcsCreateWorktreeInput) => Promise<VcsCreateWorktreeResult>;
527 removeWorktree: (input: VcsRemoveWorktreeInput) => Promise<void>;
528 createRef: (input: VcsCreateRefInput) => Promise<VcsCreateRefResult>;
529 switchRef: (input: VcsSwitchRefInput) => Promise<VcsSwitchRefResult>;
530 init: (input: VcsInitInput) => Promise<void>;
531 pull: (input: VcsPullInput) => Promise<VcsPullResult>;
532 refreshStatus: (input: VcsStatusInput) => Promise<VcsStatusResult>;
533 onStatus: (
534 input: VcsStatusInput,
535 callback: (status: VcsStatusResult) => void,
536 options?: {
537 onResubscribe?: () => void;
538 },
539 ) => () => void;
540 };
541 git: {
542 resolvePullRequest: (input: GitPullRequestRefInput) => Promise<GitResolvePullRequestResult>;
543 preparePullRequestThread: (
544 input: GitPreparePullRequestThreadInput,
545 ) => Promise<GitPreparePullRequestThreadResult>;
546 };
547 orchestration: {
548 dispatchCommand: (command: ClientOrchestrationCommand) => Promise<{ sequence: number }>;
549 getTurnDiff: (input: OrchestrationGetTurnDiffInput) => Promise<OrchestrationGetTurnDiffResult>;
550 getFullThreadDiff: (
551 input: OrchestrationGetFullThreadDiffInput,
552 ) => Promise<OrchestrationGetFullThreadDiffResult>;
553 getArchivedShellSnapshot: () => Promise<OrchestrationShellSnapshot>;
554 subscribeShell: (
555 callback: (event: OrchestrationShellStreamItem) => void,
556 options?: {
557 onResubscribe?: () => void;
558 },
559 ) => () => void;
560 subscribeThread: (
561 input: OrchestrationSubscribeThreadInput,
562 callback: (event: OrchestrationThreadStreamItem) => void,
563 options?: {
564 onResubscribe?: () => void;
565 },
566 ) => () => void;
567 };