import { type GitActionProgressEvent, type GitRunStackedActionInput, type GitRunStackedActionResult, type VcsStatusResult, type VcsStatusStreamEvent, type LocalApi, ORCHESTRATION_WS_METHODS, type ServerSettingsPatch, WS_METHODS, } from "@t3tools/contracts"; import { applyGitStatusStreamEvent } from "@t3tools/shared/git"; import { Effect, Stream } from "effect"; import { type WsRpcProtocolClient } from "./protocol"; import { resetWsReconnectBackoff } from "./wsConnectionState"; import { WsTransport } from "./wsTransport"; type RpcTag = keyof WsRpcProtocolClient & string; type RpcMethod = WsRpcProtocolClient[TTag]; type RpcInput = Parameters>[0]; interface StreamSubscriptionOptions { readonly onResubscribe?: () => void; } type RpcUnaryMethod = RpcMethod extends (input: any, options?: any) => Effect.Effect ? (input: RpcInput) => Promise : never; type RpcUnaryNoArgMethod = RpcMethod extends (input: any, options?: any) => Effect.Effect ? () => Promise : never; type RpcStreamMethod = RpcMethod extends (input: any, options?: any) => Stream.Stream ? (listener: (event: TEvent) => void, options?: StreamSubscriptionOptions) => () => void : never; type RpcInputStreamMethod = RpcMethod extends (input: any, options?: any) => Stream.Stream ? ( input: RpcInput, listener: (event: TEvent) => void, options?: StreamSubscriptionOptions, ) => () => void : never; interface GitRunStackedActionOptions { readonly onProgress?: (event: GitActionProgressEvent) => void; } export interface WsRpcClient { readonly dispose: () => Promise; readonly reconnect: () => Promise; readonly terminal: { readonly open: RpcUnaryMethod; readonly write: RpcUnaryMethod; readonly resize: RpcUnaryMethod; readonly clear: RpcUnaryMethod; readonly restart: RpcUnaryMethod; readonly close: RpcUnaryMethod; readonly onEvent: RpcStreamMethod; }; readonly projects: { readonly searchEntries: RpcUnaryMethod; readonly writeFile: RpcUnaryMethod; }; readonly filesystem: { readonly browse: RpcUnaryMethod; }; readonly sourceControl: { readonly lookupRepository: RpcUnaryMethod; readonly cloneRepository: RpcUnaryMethod; readonly publishRepository: RpcUnaryMethod; }; readonly shell: { readonly openInEditor: (input: { readonly cwd: Parameters[0]; readonly editor: Parameters[1]; }) => ReturnType; }; readonly vcs: { readonly pull: RpcUnaryMethod; readonly refreshStatus: RpcUnaryMethod; readonly onStatus: ( input: RpcInput, listener: (status: VcsStatusResult) => void, options?: StreamSubscriptionOptions, ) => () => void; readonly listRefs: RpcUnaryMethod; readonly createWorktree: RpcUnaryMethod; readonly removeWorktree: RpcUnaryMethod; readonly createRef: RpcUnaryMethod; readonly switchRef: RpcUnaryMethod; readonly init: RpcUnaryMethod; }; /** * Git-specific workflows. Local repository mechanics live under `vcs`. */ readonly git: { readonly runStackedAction: ( input: GitRunStackedActionInput, options?: GitRunStackedActionOptions, ) => Promise; readonly resolvePullRequest: RpcUnaryMethod; readonly preparePullRequestThread: RpcUnaryMethod< typeof WS_METHODS.gitPreparePullRequestThread >; }; readonly server: { readonly getConfig: RpcUnaryNoArgMethod; /** * Refresh provider snapshots. Pass `{ instanceId }` to refresh a single * configured instance; pass no argument (or `{}`) to refresh all. */ readonly refreshProviders: ( input?: RpcInput, ) => ReturnType>; readonly updateProvider: RpcUnaryMethod; readonly upsertKeybinding: RpcUnaryMethod; readonly removeKeybinding: RpcUnaryMethod; readonly getSettings: RpcUnaryNoArgMethod; readonly updateSettings: ( patch: ServerSettingsPatch, ) => ReturnType>; readonly discoverSourceControl: RpcUnaryNoArgMethod< typeof WS_METHODS.serverDiscoverSourceControl >; readonly getTraceDiagnostics: RpcUnaryNoArgMethod; readonly getProcessDiagnostics: RpcUnaryNoArgMethod< typeof WS_METHODS.serverGetProcessDiagnostics >; readonly signalProcess: RpcUnaryMethod; readonly subscribeConfig: RpcStreamMethod; readonly subscribeLifecycle: RpcStreamMethod; readonly subscribeAuthAccess: RpcStreamMethod; }; readonly orchestration: { readonly dispatchCommand: RpcUnaryMethod; readonly getTurnDiff: RpcUnaryMethod; readonly getFullThreadDiff: RpcUnaryMethod; readonly subscribeShell: RpcStreamMethod; readonly subscribeThread: RpcInputStreamMethod; }; } export function createWsRpcClient(transport: WsTransport): WsRpcClient { return { dispose: () => transport.dispose(), reconnect: async () => { resetWsReconnectBackoff(); await transport.reconnect(); }, terminal: { open: (input) => transport.request((client) => client[WS_METHODS.terminalOpen](input)), write: (input) => transport.request((client) => client[WS_METHODS.terminalWrite](input)), resize: (input) => transport.request((client) => client[WS_METHODS.terminalResize](input)), clear: (input) => transport.request((client) => client[WS_METHODS.terminalClear](input)), restart: (input) => transport.request((client) => client[WS_METHODS.terminalRestart](input)), close: (input) => transport.request((client) => client[WS_METHODS.terminalClose](input)), onEvent: (listener, options) => transport.subscribe((client) => client[WS_METHODS.subscribeTerminalEvents]({}), listener, { ...options, tag: WS_METHODS.subscribeTerminalEvents, }), }, projects: { searchEntries: (input) => transport.request((client) => client[WS_METHODS.projectsSearchEntries](input)), writeFile: (input) => transport.request((client) => client[WS_METHODS.projectsWriteFile](input)), }, filesystem: { browse: (input) => transport.request((client) => client[WS_METHODS.filesystemBrowse](input)), }, sourceControl: { lookupRepository: (input) => transport.request((client) => client[WS_METHODS.sourceControlLookupRepository](input)), cloneRepository: (input) => transport.request((client) => client[WS_METHODS.sourceControlCloneRepository](input)), publishRepository: (input) => transport.request((client) => client[WS_METHODS.sourceControlPublishRepository](input)), }, shell: { openInEditor: (input) => transport.request((client) => client[WS_METHODS.shellOpenInEditor](input)), }, vcs: { pull: (input) => transport.request((client) => client[WS_METHODS.vcsPull](input)), refreshStatus: (input) => transport.request((client) => client[WS_METHODS.vcsRefreshStatus](input)), onStatus: (input, listener, options) => { let current: VcsStatusResult | null = null; return transport.subscribe( (client) => client[WS_METHODS.subscribeVcsStatus](input), (event: VcsStatusStreamEvent) => { current = applyGitStatusStreamEvent(current, event); listener(current); }, { ...options, tag: WS_METHODS.subscribeVcsStatus }, ); }, listRefs: (input) => transport.request((client) => client[WS_METHODS.vcsListRefs](input)), createWorktree: (input) => transport.request((client) => client[WS_METHODS.vcsCreateWorktree](input)), removeWorktree: (input) => transport.request((client) => client[WS_METHODS.vcsRemoveWorktree](input)), createRef: (input) => transport.request((client) => client[WS_METHODS.vcsCreateRef](input)), switchRef: (input) => transport.request((client) => client[WS_METHODS.vcsSwitchRef](input)), init: (input) => transport.request((client) => client[WS_METHODS.vcsInit](input)), }, git: { runStackedAction: async (input, options) => { let result: GitRunStackedActionResult | null = null; await transport.requestStream( (client) => client[WS_METHODS.gitRunStackedAction](input), (event) => { options?.onProgress?.(event); if (event.kind === "action_finished") { result = event.result; } }, ); if (result) { return result; } throw new Error("Git action stream completed without a final result."); }, resolvePullRequest: (input) => transport.request((client) => client[WS_METHODS.gitResolvePullRequest](input)), preparePullRequestThread: (input) => transport.request((client) => client[WS_METHODS.gitPreparePullRequestThread](input)), }, server: { getConfig: () => transport.request((client) => client[WS_METHODS.serverGetConfig]({})), refreshProviders: (input) => transport.request((client) => client[WS_METHODS.serverRefreshProviders](input ?? {})), updateProvider: (input) => transport.request((client) => client[WS_METHODS.serverUpdateProvider](input)), upsertKeybinding: (input) => transport.request((client) => client[WS_METHODS.serverUpsertKeybinding](input)), removeKeybinding: (input) => transport.request((client) => client[WS_METHODS.serverRemoveKeybinding](input)), getSettings: () => transport.request((client) => client[WS_METHODS.serverGetSettings]({})), updateSettings: (patch) => transport.request((client) => client[WS_METHODS.serverUpdateSettings]({ patch })), discoverSourceControl: () => transport.request((client) => client[WS_METHODS.serverDiscoverSourceControl]({})), getTraceDiagnostics: () => transport.request((client) => client[WS_METHODS.serverGetTraceDiagnostics]({}).pipe(Effect.withTracerEnabled(false)), ), getProcessDiagnostics: () => transport.request((client) => client[WS_METHODS.serverGetProcessDiagnostics]({}).pipe(Effect.withTracerEnabled(false)), ), signalProcess: (input) => transport.request((client) => client[WS_METHODS.serverSignalProcess](input).pipe(Effect.withTracerEnabled(false)), ), subscribeConfig: (listener, options) => transport.subscribe((client) => client[WS_METHODS.subscribeServerConfig]({}), listener, { ...options, tag: WS_METHODS.subscribeServerConfig, }), subscribeLifecycle: (listener, options) => transport.subscribe((client) => client[WS_METHODS.subscribeServerLifecycle]({}), listener, { ...options, tag: WS_METHODS.subscribeServerLifecycle, }), subscribeAuthAccess: (listener, options) => transport.subscribe((client) => client[WS_METHODS.subscribeAuthAccess]({}), listener, { ...options, tag: WS_METHODS.subscribeAuthAccess, }), }, orchestration: { dispatchCommand: (input) => transport.request((client) => client[ORCHESTRATION_WS_METHODS.dispatchCommand](input)), getTurnDiff: (input) => transport.request((client) => client[ORCHESTRATION_WS_METHODS.getTurnDiff](input)), getFullThreadDiff: (input) => transport.request((client) => client[ORCHESTRATION_WS_METHODS.getFullThreadDiff](input)), subscribeShell: (listener, options) => transport.subscribe( (client) => client[ORCHESTRATION_WS_METHODS.subscribeShell]({}), listener, { ...options, tag: ORCHESTRATION_WS_METHODS.subscribeShell }, ), subscribeThread: (input, listener, options) => transport.subscribe( (client) => client[ORCHESTRATION_WS_METHODS.subscribeThread](input), listener, { ...options, tag: ORCHESTRATION_WS_METHODS.subscribeThread }, ), }, }; }