Replace scheduled process shell probes with a persistent native collector, Electron power/process telemetry, Effect services, diagnostics UI, attribution, and release packaging. Co-authored-by: codex <[email protected]>
1755 lines
72 KiB
TypeScript
1755 lines
72 KiB
TypeScript
import * as Cause from "effect/Cause";
|
|
import * as Crypto from "effect/Crypto";
|
|
import * as DateTime from "effect/DateTime";
|
|
import * as Duration from "effect/Duration";
|
|
import * as Effect from "effect/Effect";
|
|
import * as Layer from "effect/Layer";
|
|
import * as Option from "effect/Option";
|
|
import * as Queue from "effect/Queue";
|
|
import * as Ref from "effect/Ref";
|
|
import * as Schema from "effect/Schema";
|
|
import * as Stream from "effect/Stream";
|
|
import {
|
|
DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL,
|
|
AuthOrchestrationOperateScope,
|
|
AuthOrchestrationReadScope,
|
|
AuthReviewWriteScope,
|
|
AuthRelayWriteScope,
|
|
AuthTerminalOperateScope,
|
|
AuthAccessReadScope,
|
|
AuthAccessStreamError,
|
|
type AuthAccessStreamEvent,
|
|
type AuthEnvironmentScope,
|
|
AuthSessionId,
|
|
CommandId,
|
|
type DiscoveredLocalServerList,
|
|
EventId,
|
|
type OrchestrationCommand,
|
|
type GitActionProgressEvent,
|
|
type GitManagerServiceError,
|
|
OrchestrationDispatchCommandError,
|
|
type OrchestrationEvent,
|
|
type OrchestrationShellStreamEvent,
|
|
OrchestrationGetFullThreadDiffError,
|
|
OrchestrationGetSnapshotError,
|
|
OrchestrationGetTurnDiffError,
|
|
ORCHESTRATION_WS_METHODS,
|
|
ProjectListEntriesError,
|
|
ProjectReadFileError,
|
|
ProjectSearchEntriesError,
|
|
ProjectWriteFileError,
|
|
RelayClientInstallFailedError,
|
|
type RelayClientInstallProgressEvent,
|
|
OrchestrationReplayEventsError,
|
|
FilesystemBrowseError,
|
|
RpcClientId,
|
|
AssetAccessError,
|
|
EnvironmentAuthorizationError,
|
|
ThreadId,
|
|
type TerminalAttachStreamEvent,
|
|
type TerminalError,
|
|
type TerminalEvent,
|
|
type TerminalMetadataStreamEvent,
|
|
WS_METHODS,
|
|
WsRpcGroup,
|
|
} from "@t3tools/contracts";
|
|
import { resolveServerBackgroundActivitySettings } from "@t3tools/shared/backgroundActivitySettings";
|
|
import { clamp } from "effect/Number";
|
|
import { HttpRouter, HttpServerRequest, HttpServerRespondable } from "effect/unstable/http";
|
|
import { RpcSerialization, RpcServer } from "effect/unstable/rpc";
|
|
|
|
import { CheckpointDiffQuery } from "./checkpointing/Services/CheckpointDiffQuery.ts";
|
|
import { ServerConfig } from "./config.ts";
|
|
import { Keybindings } from "./keybindings.ts";
|
|
import * as ExternalLauncher from "./process/externalLauncher.ts";
|
|
import { normalizeDispatchCommand } from "./orchestration/Normalizer.ts";
|
|
import { OrchestrationEngineService } from "./orchestration/Services/OrchestrationEngine.ts";
|
|
import { ProjectionSnapshotQuery } from "./orchestration/Services/ProjectionSnapshotQuery.ts";
|
|
import {
|
|
observeRpcEffect as instrumentRpcEffect,
|
|
observeRpcStream as instrumentRpcStream,
|
|
observeRpcStreamEffect as instrumentRpcStreamEffect,
|
|
} from "./observability/RpcInstrumentation.ts";
|
|
import { ProviderRegistry } from "./provider/Services/ProviderRegistry.ts";
|
|
import * as ProviderMaintenanceRunner from "./provider/providerMaintenanceRunner.ts";
|
|
import { ServerLifecycleEvents } from "./serverLifecycleEvents.ts";
|
|
import { ServerRuntimeStartup } from "./serverRuntimeStartup.ts";
|
|
import { redactServerSettingsForClient, ServerSettingsService } from "./serverSettings.ts";
|
|
import { TerminalManager } from "./terminal/Services/Manager.ts";
|
|
import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts";
|
|
import * as PreviewManager from "./preview/Manager.ts";
|
|
import { issueAssetUrl } from "./assets/AssetAccess.ts";
|
|
import * as PortScanner from "./preview/PortScanner.ts";
|
|
import * as WorkspaceEntries from "./workspace/WorkspaceEntries.ts";
|
|
import { WorkspaceFileSystem } from "./workspace/Services/WorkspaceFileSystem.ts";
|
|
import { WorkspacePathOutsideRootError } from "./workspace/Services/WorkspacePaths.ts";
|
|
import { VcsStatusBroadcaster } from "./vcs/VcsStatusBroadcaster.ts";
|
|
import { VcsProvisioningService } from "./vcs/VcsProvisioningService.ts";
|
|
import { GitWorkflowService } from "./git/GitWorkflowService.ts";
|
|
import { ReviewService } from "./review/ReviewService.ts";
|
|
import { ProjectSetupScriptRunner } from "./project/Services/ProjectSetupScriptRunner.ts";
|
|
import { RepositoryIdentityResolver } from "./project/Services/RepositoryIdentityResolver.ts";
|
|
import { ServerEnvironment } from "./environment/Services/ServerEnvironment.ts";
|
|
import * as BackgroundPolicy from "./background/BackgroundPolicy.ts";
|
|
import * as EnvironmentAuth from "./auth/EnvironmentAuth.ts";
|
|
import type { AuthenticatedSession } from "./auth/EnvironmentAuth.ts";
|
|
import * as ProcessDiagnostics from "./diagnostics/ProcessDiagnostics.ts";
|
|
import * as ProcessResourceMonitor from "./diagnostics/ProcessResourceMonitor.ts";
|
|
import * as ResourceTelemetry from "./resourceTelemetry/ResourceTelemetry.ts";
|
|
import * as TraceDiagnostics from "./diagnostics/TraceDiagnostics.ts";
|
|
import * as SourceControlDiscoveryLayer from "./sourceControl/SourceControlDiscovery.ts";
|
|
import { SourceControlRepositoryService } from "./sourceControl/SourceControlRepositoryService.ts";
|
|
import * as AzureDevOpsCli from "./sourceControl/AzureDevOpsCli.ts";
|
|
import * as BitbucketApi from "./sourceControl/BitbucketApi.ts";
|
|
import * as GitHubCli from "./sourceControl/GitHubCli.ts";
|
|
import * as GitLabCli from "./sourceControl/GitLabCli.ts";
|
|
import * as SourceControlProviderRegistry from "./sourceControl/SourceControlProviderRegistry.ts";
|
|
import * as GitVcsDriver from "./vcs/GitVcsDriver.ts";
|
|
import * as VcsDriverRegistry from "./vcs/VcsDriverRegistry.ts";
|
|
import * as VcsProjectConfig from "./vcs/VcsProjectConfig.ts";
|
|
import * as VcsProcess from "./vcs/VcsProcess.ts";
|
|
import * as PairingGrantStore from "./auth/PairingGrantStore.ts";
|
|
import * as SessionStore from "./auth/SessionStore.ts";
|
|
import { failEnvironmentAuthInvalid, failEnvironmentInternal } from "./auth/http.ts";
|
|
import * as RelayClient from "@t3tools/shared/relayClient";
|
|
const isOrchestrationDispatchCommandError = Schema.is(OrchestrationDispatchCommandError);
|
|
const isWorkspacePathOutsideRootError = Schema.is(WorkspacePathOutsideRootError);
|
|
|
|
const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
|
|
|
|
function isThreadDetailEvent(event: OrchestrationEvent): event is Extract<
|
|
OrchestrationEvent,
|
|
{
|
|
type:
|
|
| "thread.message-sent"
|
|
| "thread.proposed-plan-upserted"
|
|
| "thread.activity-appended"
|
|
| "thread.turn-diff-completed"
|
|
| "thread.reverted"
|
|
| "thread.session-set";
|
|
}
|
|
> {
|
|
return (
|
|
event.type === "thread.message-sent" ||
|
|
event.type === "thread.proposed-plan-upserted" ||
|
|
event.type === "thread.activity-appended" ||
|
|
event.type === "thread.turn-diff-completed" ||
|
|
event.type === "thread.reverted" ||
|
|
event.type === "thread.session-set"
|
|
);
|
|
}
|
|
|
|
const PROVIDER_STATUS_DEBOUNCE_MS = 200;
|
|
|
|
const RPC_REQUIRED_SCOPE = new Map<string, AuthEnvironmentScope>([
|
|
[ORCHESTRATION_WS_METHODS.dispatchCommand, AuthOrchestrationOperateScope],
|
|
[ORCHESTRATION_WS_METHODS.getTurnDiff, AuthOrchestrationReadScope],
|
|
[ORCHESTRATION_WS_METHODS.getFullThreadDiff, AuthOrchestrationReadScope],
|
|
[ORCHESTRATION_WS_METHODS.replayEvents, AuthOrchestrationReadScope],
|
|
[ORCHESTRATION_WS_METHODS.subscribeShell, AuthOrchestrationReadScope],
|
|
[ORCHESTRATION_WS_METHODS.getArchivedShellSnapshot, AuthOrchestrationReadScope],
|
|
[ORCHESTRATION_WS_METHODS.subscribeThread, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetConfig, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverRefreshProviders, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverUpdateProvider, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverUpsertKeybinding, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverRemoveKeybinding, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverGetSettings, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverUpdateSettings, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverDiscoverSourceControl, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetTraceDiagnostics, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetProcessDiagnostics, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetProcessResourceHistory, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetResourceTelemetry, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverGetResourceTelemetryHistory, AuthOrchestrationReadScope],
|
|
[WS_METHODS.serverRetryResourceTelemetry, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.serverSignalProcess, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.cloudGetRelayClientStatus, AuthRelayWriteScope],
|
|
[WS_METHODS.cloudInstallRelayClient, AuthRelayWriteScope],
|
|
[WS_METHODS.sourceControlLookupRepository, AuthOrchestrationReadScope],
|
|
[WS_METHODS.sourceControlCloneRepository, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.sourceControlPublishRepository, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.projectsListEntries, AuthOrchestrationReadScope],
|
|
[WS_METHODS.projectsReadFile, AuthOrchestrationReadScope],
|
|
[WS_METHODS.projectsSearchEntries, AuthOrchestrationReadScope],
|
|
[WS_METHODS.projectsWriteFile, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.shellOpenInEditor, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.filesystemBrowse, AuthOrchestrationReadScope],
|
|
[WS_METHODS.assetsCreateUrl, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeVcsStatus, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeResourceTelemetry, AuthOrchestrationReadScope],
|
|
[WS_METHODS.vcsRefreshStatus, AuthOrchestrationReadScope],
|
|
[WS_METHODS.vcsPull, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.gitRunStackedAction, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.gitResolvePullRequest, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.gitPreparePullRequestThread, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.vcsListRefs, AuthOrchestrationReadScope],
|
|
[WS_METHODS.vcsCreateWorktree, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.vcsRemoveWorktree, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.vcsCreateRef, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.vcsSwitchRef, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.vcsInit, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.reviewGetDiffPreview, AuthReviewWriteScope],
|
|
[WS_METHODS.terminalOpen, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalAttach, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalWrite, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalResize, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalClear, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalRestart, AuthTerminalOperateScope],
|
|
[WS_METHODS.terminalClose, AuthTerminalOperateScope],
|
|
[WS_METHODS.subscribeTerminalEvents, AuthTerminalOperateScope],
|
|
[WS_METHODS.subscribeTerminalMetadata, AuthTerminalOperateScope],
|
|
[WS_METHODS.previewOpen, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewNavigate, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewRefresh, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewClose, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewList, AuthOrchestrationReadScope],
|
|
[WS_METHODS.previewReportStatus, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewAutomationConnect, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewAutomationRespond, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewAutomationReportOwner, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.previewAutomationClearOwner, AuthOrchestrationOperateScope],
|
|
[WS_METHODS.subscribePreviewEvents, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeDiscoveredLocalServers, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeServerConfig, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeServerLifecycle, AuthOrchestrationReadScope],
|
|
[WS_METHODS.subscribeAuthAccess, AuthAccessReadScope],
|
|
]);
|
|
|
|
function toAuthAccessStreamEvent(
|
|
change: PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange,
|
|
revision: number,
|
|
currentSessionId: AuthSessionId,
|
|
): AuthAccessStreamEvent {
|
|
switch (change.type) {
|
|
case "pairingLinkUpserted":
|
|
return {
|
|
version: 1,
|
|
revision,
|
|
type: "pairingLinkUpserted",
|
|
payload: change.pairingLink,
|
|
};
|
|
case "pairingLinkRemoved":
|
|
return {
|
|
version: 1,
|
|
revision,
|
|
type: "pairingLinkRemoved",
|
|
payload: { id: change.id },
|
|
};
|
|
case "clientUpserted":
|
|
return {
|
|
version: 1,
|
|
revision,
|
|
type: "clientUpserted",
|
|
payload: {
|
|
...change.clientSession,
|
|
current: change.clientSession.sessionId === currentSessionId,
|
|
},
|
|
};
|
|
case "clientRemoved":
|
|
return {
|
|
version: 1,
|
|
revision,
|
|
type: "clientRemoved",
|
|
payload: { sessionId: change.sessionId },
|
|
};
|
|
}
|
|
}
|
|
|
|
const makeWsRpcLayer = (currentSession: AuthenticatedSession) =>
|
|
WsRpcGroup.toLayer(
|
|
Effect.gen(function* () {
|
|
const currentSessionId = currentSession.sessionId;
|
|
const crypto = yield* Crypto.Crypto;
|
|
const projectionSnapshotQuery = yield* ProjectionSnapshotQuery;
|
|
const orchestrationEngine = yield* OrchestrationEngineService;
|
|
const checkpointDiffQuery = yield* CheckpointDiffQuery;
|
|
const keybindings = yield* Keybindings;
|
|
const externalLauncher = yield* ExternalLauncher.ExternalLauncher;
|
|
const gitWorkflow = yield* GitWorkflowService;
|
|
const review = yield* ReviewService;
|
|
const vcsProvisioning = yield* VcsProvisioningService;
|
|
const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
|
|
const terminalManager = yield* TerminalManager;
|
|
const previewAutomationBroker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
|
|
const previewManager = yield* PreviewManager.PreviewManager;
|
|
const portDiscovery = yield* PortScanner.PortDiscovery;
|
|
const providerRegistry = yield* ProviderRegistry;
|
|
const providerMaintenanceRunner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner;
|
|
const config = yield* ServerConfig;
|
|
const lifecycleEvents = yield* ServerLifecycleEvents;
|
|
const serverSettings = yield* ServerSettingsService;
|
|
const startup = yield* ServerRuntimeStartup;
|
|
const workspaceEntries = yield* WorkspaceEntries.WorkspaceEntries;
|
|
const workspaceFileSystem = yield* WorkspaceFileSystem;
|
|
const projectSetupScriptRunner = yield* ProjectSetupScriptRunner;
|
|
const repositoryIdentityResolver = yield* RepositoryIdentityResolver;
|
|
const serverEnvironment = yield* ServerEnvironment;
|
|
const backgroundPolicy = yield* BackgroundPolicy.BackgroundPolicy;
|
|
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
|
|
const sourceControlDiscovery = yield* SourceControlDiscoveryLayer.SourceControlDiscovery;
|
|
const automaticGitFetchInterval = serverSettings.getSettings.pipe(
|
|
Effect.map(
|
|
(settings) => resolveServerBackgroundActivitySettings(settings).automaticGitFetchInterval,
|
|
),
|
|
Effect.catch((cause) =>
|
|
Effect.logWarning("Failed to read automatic Git fetch interval setting", {
|
|
detail: cause.message,
|
|
}).pipe(Effect.as(DEFAULT_AUTOMATIC_GIT_FETCH_INTERVAL)),
|
|
),
|
|
);
|
|
const sourceControlRepositories = yield* SourceControlRepositoryService;
|
|
const bootstrapCredentials = yield* PairingGrantStore.PairingGrantStore;
|
|
const sessions = yield* SessionStore.SessionStore;
|
|
const processDiagnostics = yield* ProcessDiagnostics.ProcessDiagnostics;
|
|
const processResourceMonitor = yield* ProcessResourceMonitor.ProcessResourceMonitor;
|
|
const resourceTelemetry = yield* ResourceTelemetry.ResourceTelemetry;
|
|
const relayClient = yield* RelayClient.RelayClient;
|
|
const authorizationError = (requiredScope: AuthEnvironmentScope) =>
|
|
new EnvironmentAuthorizationError({
|
|
message: `The authenticated token is missing required scope: ${requiredScope}.`,
|
|
requiredScope,
|
|
});
|
|
const authorizeEffect = <A, E, R>(
|
|
requiredScope: AuthEnvironmentScope,
|
|
effect: Effect.Effect<A, E, R>,
|
|
): Effect.Effect<A, E | EnvironmentAuthorizationError, R> =>
|
|
currentSession.scopes.includes(requiredScope)
|
|
? effect
|
|
: Effect.fail(authorizationError(requiredScope));
|
|
const authorizeStream = <A, E, R>(
|
|
requiredScope: AuthEnvironmentScope,
|
|
stream: Stream.Stream<A, E, R>,
|
|
): Stream.Stream<A, E | EnvironmentAuthorizationError, R> =>
|
|
currentSession.scopes.includes(requiredScope)
|
|
? stream
|
|
: Stream.fail(authorizationError(requiredScope));
|
|
const requiredScopeForMethod = (method: string): AuthEnvironmentScope => {
|
|
const requiredScope = RPC_REQUIRED_SCOPE.get(method);
|
|
if (requiredScope === undefined) {
|
|
throw new Error(`RPC method ${method} has no declared authorization scope.`);
|
|
}
|
|
return requiredScope;
|
|
};
|
|
const observeRpcEffect = <A, E, R>(
|
|
method: string,
|
|
effect: Effect.Effect<A, E, R>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) =>
|
|
instrumentRpcEffect(
|
|
method,
|
|
authorizeEffect(requiredScopeForMethod(method), effect),
|
|
traceAttributes,
|
|
);
|
|
const observeRpcStream = <A, E, R>(
|
|
method: string,
|
|
stream: Stream.Stream<A, E, R>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) =>
|
|
instrumentRpcStream(
|
|
method,
|
|
authorizeStream(requiredScopeForMethod(method), stream),
|
|
traceAttributes,
|
|
);
|
|
const observeRpcStreamEffect = <A, StreamError, StreamContext, EffectError, EffectContext>(
|
|
method: string,
|
|
effect: Effect.Effect<
|
|
Stream.Stream<A, StreamError, StreamContext>,
|
|
EffectError,
|
|
EffectContext
|
|
>,
|
|
traceAttributes?: Readonly<Record<string, unknown>>,
|
|
) =>
|
|
instrumentRpcStreamEffect(
|
|
method,
|
|
authorizeEffect(requiredScopeForMethod(method), effect),
|
|
traceAttributes,
|
|
);
|
|
const toDispatchCommandError = (cause: unknown, fallbackMessage: string) =>
|
|
isOrchestrationDispatchCommandError(cause)
|
|
? cause
|
|
: new OrchestrationDispatchCommandError({
|
|
message: cause instanceof Error ? cause.message : fallbackMessage,
|
|
cause,
|
|
});
|
|
const randomUUID = crypto.randomUUIDv4.pipe(
|
|
Effect.mapError((cause) =>
|
|
toDispatchCommandError(cause, "Failed to generate orchestration command identifier."),
|
|
),
|
|
);
|
|
const serverEventId = randomUUID.pipe(Effect.map(EventId.make));
|
|
const serverCommandId = (tag: string) =>
|
|
randomUUID.pipe(Effect.map((uuid) => CommandId.make(`server:${tag}:${uuid}`)));
|
|
|
|
const loadAuthAccessSnapshot = () =>
|
|
Effect.all({
|
|
pairingLinks: serverAuth.listPairingLinks(),
|
|
clientSessions: serverAuth.listClientSessions(currentSessionId),
|
|
}).pipe(
|
|
Effect.mapError(
|
|
(error) =>
|
|
new AuthAccessStreamError({
|
|
message: error.message,
|
|
}),
|
|
),
|
|
);
|
|
|
|
const appendSetupScriptActivity = (input: {
|
|
readonly threadId: ThreadId;
|
|
readonly kind: "setup-script.requested" | "setup-script.started" | "setup-script.failed";
|
|
readonly summary: string;
|
|
readonly createdAt: string;
|
|
readonly payload: Record<string, unknown>;
|
|
readonly tone: "info" | "error";
|
|
}) =>
|
|
Effect.all({
|
|
commandId: serverCommandId("setup-script-activity"),
|
|
activityId: serverEventId,
|
|
}).pipe(
|
|
Effect.flatMap(({ commandId, activityId }) =>
|
|
orchestrationEngine.dispatch({
|
|
type: "thread.activity.append",
|
|
commandId,
|
|
threadId: input.threadId,
|
|
activity: {
|
|
id: activityId,
|
|
tone: input.tone,
|
|
kind: input.kind,
|
|
summary: input.summary,
|
|
payload: input.payload,
|
|
turnId: null,
|
|
createdAt: input.createdAt,
|
|
},
|
|
createdAt: input.createdAt,
|
|
}),
|
|
),
|
|
);
|
|
|
|
const toBootstrapDispatchCommandCauseError = (cause: Cause.Cause<unknown>) => {
|
|
const error = Cause.squash(cause);
|
|
return isOrchestrationDispatchCommandError(error)
|
|
? error
|
|
: new OrchestrationDispatchCommandError({
|
|
message:
|
|
error instanceof Error ? error.message : "Failed to bootstrap thread turn start.",
|
|
cause,
|
|
});
|
|
};
|
|
|
|
const enrichProjectEvent = (
|
|
event: OrchestrationEvent,
|
|
): Effect.Effect<OrchestrationEvent, never, never> => {
|
|
switch (event.type) {
|
|
case "project.created":
|
|
return repositoryIdentityResolver.resolve(event.payload.workspaceRoot).pipe(
|
|
Effect.map((repositoryIdentity) => ({
|
|
...event,
|
|
payload: {
|
|
...event.payload,
|
|
repositoryIdentity,
|
|
},
|
|
})),
|
|
);
|
|
case "project.meta-updated":
|
|
return Effect.gen(function* () {
|
|
const workspaceRoot =
|
|
event.payload.workspaceRoot ??
|
|
Option.match(
|
|
yield* projectionSnapshotQuery.getProjectShellById(event.payload.projectId),
|
|
{
|
|
onNone: () => null,
|
|
onSome: (project) => project.workspaceRoot,
|
|
},
|
|
) ??
|
|
null;
|
|
if (workspaceRoot === null) {
|
|
return event;
|
|
}
|
|
|
|
const repositoryIdentity = yield* repositoryIdentityResolver.resolve(workspaceRoot);
|
|
return {
|
|
...event,
|
|
payload: {
|
|
...event.payload,
|
|
repositoryIdentity,
|
|
},
|
|
} satisfies OrchestrationEvent;
|
|
}).pipe(Effect.orElseSucceed(() => event));
|
|
default:
|
|
return Effect.succeed(event);
|
|
}
|
|
};
|
|
|
|
const enrichOrchestrationEvents = (events: ReadonlyArray<OrchestrationEvent>) =>
|
|
Effect.forEach(events, enrichProjectEvent, { concurrency: 4 });
|
|
|
|
const toShellStreamEvent = (
|
|
event: OrchestrationEvent,
|
|
): Effect.Effect<Option.Option<OrchestrationShellStreamEvent>, never, never> => {
|
|
switch (event.type) {
|
|
case "project.created":
|
|
case "project.meta-updated":
|
|
return projectionSnapshotQuery.getProjectShellById(event.payload.projectId).pipe(
|
|
Effect.map((project) =>
|
|
Option.map(project, (nextProject) => ({
|
|
kind: "project-upserted" as const,
|
|
sequence: event.sequence,
|
|
project: nextProject,
|
|
})),
|
|
),
|
|
Effect.orElseSucceed(() => Option.none()),
|
|
);
|
|
case "project.deleted":
|
|
return Effect.succeed(
|
|
Option.some({
|
|
kind: "project-removed" as const,
|
|
sequence: event.sequence,
|
|
projectId: event.payload.projectId,
|
|
}),
|
|
);
|
|
case "thread.deleted":
|
|
case "thread.archived":
|
|
return Effect.succeed(
|
|
Option.some({
|
|
kind: "thread-removed" as const,
|
|
sequence: event.sequence,
|
|
threadId: event.payload.threadId,
|
|
}),
|
|
);
|
|
case "thread.unarchived":
|
|
return projectionSnapshotQuery.getThreadShellById(event.payload.threadId).pipe(
|
|
Effect.map((thread) =>
|
|
Option.map(thread, (nextThread) => ({
|
|
kind: "thread-upserted" as const,
|
|
sequence: event.sequence,
|
|
thread: nextThread,
|
|
})),
|
|
),
|
|
Effect.orElseSucceed(() => Option.none()),
|
|
);
|
|
default:
|
|
if (event.aggregateKind !== "thread") {
|
|
return Effect.succeed(Option.none());
|
|
}
|
|
return projectionSnapshotQuery
|
|
.getThreadShellById(ThreadId.make(event.aggregateId))
|
|
.pipe(
|
|
Effect.map((thread) =>
|
|
Option.map(thread, (nextThread) => ({
|
|
kind: "thread-upserted" as const,
|
|
sequence: event.sequence,
|
|
thread: nextThread,
|
|
})),
|
|
),
|
|
Effect.orElseSucceed(() => Option.none()),
|
|
);
|
|
}
|
|
};
|
|
|
|
const dispatchBootstrapTurnStart = (
|
|
command: Extract<OrchestrationCommand, { type: "thread.turn.start" }>,
|
|
): Effect.Effect<{ readonly sequence: number }, OrchestrationDispatchCommandError> =>
|
|
Effect.gen(function* () {
|
|
const bootstrap = command.bootstrap;
|
|
const { bootstrap: _bootstrap, ...finalTurnStartCommand } = command;
|
|
let createdThread = false;
|
|
let targetProjectId = bootstrap?.createThread?.projectId;
|
|
let targetProjectCwd = bootstrap?.prepareWorktree?.projectCwd;
|
|
let targetWorktreePath = bootstrap?.createThread?.worktreePath ?? null;
|
|
|
|
const cleanupCreatedThread = () =>
|
|
createdThread
|
|
? serverCommandId("bootstrap-thread-delete").pipe(
|
|
Effect.flatMap((commandId) =>
|
|
orchestrationEngine.dispatch({
|
|
type: "thread.delete",
|
|
commandId,
|
|
threadId: command.threadId,
|
|
}),
|
|
),
|
|
Effect.ignoreCause({ log: true }),
|
|
)
|
|
: Effect.void;
|
|
|
|
const recordSetupScriptLaunchFailure = (input: {
|
|
readonly error: unknown;
|
|
readonly requestedAt: string;
|
|
readonly worktreePath: string;
|
|
}) => {
|
|
const detail =
|
|
input.error instanceof Error ? input.error.message : "Unknown setup failure.";
|
|
return appendSetupScriptActivity({
|
|
threadId: command.threadId,
|
|
kind: "setup-script.failed",
|
|
summary: "Setup script failed to start",
|
|
createdAt: input.requestedAt,
|
|
payload: {
|
|
detail,
|
|
worktreePath: input.worktreePath,
|
|
},
|
|
tone: "error",
|
|
}).pipe(
|
|
Effect.ignoreCause({ log: false }),
|
|
Effect.flatMap(() =>
|
|
Effect.logWarning("bootstrap turn start failed to launch setup script", {
|
|
threadId: command.threadId,
|
|
worktreePath: input.worktreePath,
|
|
detail,
|
|
}),
|
|
),
|
|
);
|
|
};
|
|
|
|
const recordSetupScriptStarted = (input: {
|
|
readonly requestedAt: string;
|
|
readonly worktreePath: string;
|
|
readonly scriptId: string;
|
|
readonly scriptName: string;
|
|
readonly terminalId: string;
|
|
}) =>
|
|
Effect.gen(function* () {
|
|
const startedAt = yield* nowIso;
|
|
const payload = {
|
|
scriptId: input.scriptId,
|
|
scriptName: input.scriptName,
|
|
terminalId: input.terminalId,
|
|
worktreePath: input.worktreePath,
|
|
};
|
|
yield* Effect.all([
|
|
appendSetupScriptActivity({
|
|
threadId: command.threadId,
|
|
kind: "setup-script.requested",
|
|
summary: "Starting setup script",
|
|
createdAt: input.requestedAt,
|
|
payload,
|
|
tone: "info",
|
|
}),
|
|
appendSetupScriptActivity({
|
|
threadId: command.threadId,
|
|
kind: "setup-script.started",
|
|
summary: "Setup script started",
|
|
createdAt: startedAt,
|
|
payload,
|
|
tone: "info",
|
|
}),
|
|
]).pipe(
|
|
Effect.asVoid,
|
|
Effect.catch((error) =>
|
|
Effect.logWarning(
|
|
"bootstrap turn start launched setup script but failed to record setup activity",
|
|
{
|
|
threadId: command.threadId,
|
|
worktreePath: input.worktreePath,
|
|
scriptId: input.scriptId,
|
|
terminalId: input.terminalId,
|
|
detail: error.message,
|
|
},
|
|
),
|
|
),
|
|
);
|
|
});
|
|
|
|
const runSetupProgram = () =>
|
|
Effect.gen(function* () {
|
|
if (!bootstrap?.runSetupScript || !targetWorktreePath) {
|
|
return;
|
|
}
|
|
const worktreePath = targetWorktreePath;
|
|
const requestedAt = yield* nowIso;
|
|
yield* projectSetupScriptRunner
|
|
.runForThread({
|
|
threadId: command.threadId,
|
|
...(targetProjectId ? { projectId: targetProjectId } : {}),
|
|
...(targetProjectCwd ? { projectCwd: targetProjectCwd } : {}),
|
|
worktreePath,
|
|
})
|
|
.pipe(
|
|
Effect.matchEffect({
|
|
onFailure: (error) =>
|
|
recordSetupScriptLaunchFailure({
|
|
error,
|
|
requestedAt,
|
|
worktreePath,
|
|
}),
|
|
onSuccess: (setupResult) => {
|
|
if (setupResult.status !== "started") {
|
|
return Effect.void;
|
|
}
|
|
return recordSetupScriptStarted({
|
|
requestedAt,
|
|
worktreePath,
|
|
scriptId: setupResult.scriptId,
|
|
scriptName: setupResult.scriptName,
|
|
terminalId: setupResult.terminalId,
|
|
});
|
|
},
|
|
}),
|
|
);
|
|
});
|
|
|
|
const bootstrapProgram = Effect.gen(function* () {
|
|
if (bootstrap?.createThread) {
|
|
yield* orchestrationEngine.dispatch({
|
|
type: "thread.create",
|
|
commandId: yield* serverCommandId("bootstrap-thread-create"),
|
|
threadId: command.threadId,
|
|
projectId: bootstrap.createThread.projectId,
|
|
title: bootstrap.createThread.title,
|
|
modelSelection: bootstrap.createThread.modelSelection,
|
|
runtimeMode: bootstrap.createThread.runtimeMode,
|
|
interactionMode: bootstrap.createThread.interactionMode,
|
|
branch: bootstrap.createThread.branch,
|
|
worktreePath: bootstrap.createThread.worktreePath,
|
|
createdAt: bootstrap.createThread.createdAt,
|
|
});
|
|
createdThread = true;
|
|
}
|
|
|
|
if (bootstrap?.prepareWorktree) {
|
|
const worktree = yield* gitWorkflow.createWorktree({
|
|
cwd: bootstrap.prepareWorktree.projectCwd,
|
|
refName: bootstrap.prepareWorktree.baseBranch,
|
|
newRefName: bootstrap.prepareWorktree.branch,
|
|
path: null,
|
|
});
|
|
targetWorktreePath = worktree.worktree.path;
|
|
yield* orchestrationEngine.dispatch({
|
|
type: "thread.meta.update",
|
|
commandId: yield* serverCommandId("bootstrap-thread-meta-update"),
|
|
threadId: command.threadId,
|
|
branch: worktree.worktree.refName,
|
|
worktreePath: targetWorktreePath,
|
|
});
|
|
yield* refreshGitStatus(targetWorktreePath);
|
|
}
|
|
|
|
yield* runSetupProgram();
|
|
|
|
return yield* orchestrationEngine.dispatch(finalTurnStartCommand);
|
|
});
|
|
|
|
return yield* bootstrapProgram.pipe(
|
|
Effect.catchCause((cause) => {
|
|
const dispatchError = toBootstrapDispatchCommandCauseError(cause);
|
|
if (Cause.hasInterruptsOnly(cause)) {
|
|
return Effect.fail(dispatchError);
|
|
}
|
|
return cleanupCreatedThread().pipe(Effect.flatMap(() => Effect.fail(dispatchError)));
|
|
}),
|
|
);
|
|
});
|
|
|
|
const dispatchNormalizedCommand = (
|
|
normalizedCommand: OrchestrationCommand,
|
|
): Effect.Effect<{ readonly sequence: number }, OrchestrationDispatchCommandError> => {
|
|
const dispatchEffect =
|
|
normalizedCommand.type === "thread.turn.start" && normalizedCommand.bootstrap
|
|
? dispatchBootstrapTurnStart(normalizedCommand)
|
|
: orchestrationEngine
|
|
.dispatch(normalizedCommand)
|
|
.pipe(
|
|
Effect.mapError((cause) =>
|
|
toDispatchCommandError(cause, "Failed to dispatch orchestration command"),
|
|
),
|
|
);
|
|
|
|
return startup
|
|
.enqueueCommand(dispatchEffect)
|
|
.pipe(
|
|
Effect.mapError((cause) =>
|
|
toDispatchCommandError(cause, "Failed to dispatch orchestration command"),
|
|
),
|
|
);
|
|
};
|
|
|
|
const loadServerConfig = Effect.gen(function* () {
|
|
const keybindingsConfig = yield* keybindings.loadConfigState;
|
|
const providers = yield* providerRegistry.getProviders;
|
|
const settings = redactServerSettingsForClient(yield* serverSettings.getSettings);
|
|
const environment = yield* serverEnvironment.getDescriptor;
|
|
const auth = yield* serverAuth.getDescriptor();
|
|
|
|
return {
|
|
environment,
|
|
auth,
|
|
cwd: config.cwd,
|
|
keybindingsConfigPath: config.keybindingsConfigPath,
|
|
keybindings: keybindingsConfig.keybindings,
|
|
issues: keybindingsConfig.issues,
|
|
providers,
|
|
availableEditors: yield* externalLauncher.resolveAvailableEditors(),
|
|
observability: {
|
|
logsDirectoryPath: config.logsDir,
|
|
localTracingEnabled: true,
|
|
...(config.otlpTracesUrl !== undefined ? { otlpTracesUrl: config.otlpTracesUrl } : {}),
|
|
otlpTracesEnabled: config.otlpTracesUrl !== undefined,
|
|
...(config.otlpMetricsUrl !== undefined
|
|
? { otlpMetricsUrl: config.otlpMetricsUrl }
|
|
: {}),
|
|
otlpMetricsEnabled: config.otlpMetricsUrl !== undefined,
|
|
},
|
|
settings,
|
|
};
|
|
});
|
|
|
|
const refreshGitStatus = (cwd: string) =>
|
|
vcsStatusBroadcaster
|
|
.refreshStatus(cwd)
|
|
.pipe(Effect.ignoreCause({ log: true }), Effect.forkDetach, Effect.asVoid);
|
|
|
|
return WsRpcGroup.of({
|
|
[ORCHESTRATION_WS_METHODS.dispatchCommand]: (command) =>
|
|
observeRpcEffect(
|
|
ORCHESTRATION_WS_METHODS.dispatchCommand,
|
|
Effect.gen(function* () {
|
|
const normalizedCommand = yield* normalizeDispatchCommand(command);
|
|
const shouldStopSessionAfterArchive =
|
|
normalizedCommand.type === "thread.archive"
|
|
? yield* projectionSnapshotQuery
|
|
.getThreadShellById(normalizedCommand.threadId)
|
|
.pipe(
|
|
Effect.map(
|
|
Option.match({
|
|
onNone: () => false,
|
|
onSome: (thread) =>
|
|
thread.session !== null && thread.session.status !== "stopped",
|
|
}),
|
|
),
|
|
Effect.orElseSucceed(() => false),
|
|
)
|
|
: false;
|
|
const result = yield* dispatchNormalizedCommand(normalizedCommand);
|
|
if (normalizedCommand.type === "thread.archive") {
|
|
if (shouldStopSessionAfterArchive) {
|
|
yield* Effect.gen(function* () {
|
|
const stopCommand = yield* normalizeDispatchCommand({
|
|
type: "thread.session.stop",
|
|
commandId: CommandId.make(
|
|
`session-stop-for-archive:${normalizedCommand.commandId}`,
|
|
),
|
|
threadId: normalizedCommand.threadId,
|
|
createdAt: yield* nowIso,
|
|
});
|
|
|
|
yield* dispatchNormalizedCommand(stopCommand);
|
|
}).pipe(
|
|
Effect.catchCause((cause) =>
|
|
Effect.logWarning("failed to stop provider session during archive", {
|
|
threadId: normalizedCommand.threadId,
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
|
|
yield* terminalManager.close({ threadId: normalizedCommand.threadId }).pipe(
|
|
Effect.catch((error) =>
|
|
Effect.logWarning("failed to close thread terminals after archive", {
|
|
threadId: normalizedCommand.threadId,
|
|
error: error.message,
|
|
}),
|
|
),
|
|
);
|
|
}
|
|
return result;
|
|
}).pipe(
|
|
Effect.mapError((cause) =>
|
|
isOrchestrationDispatchCommandError(cause)
|
|
? cause
|
|
: new OrchestrationDispatchCommandError({
|
|
message: "Failed to dispatch orchestration command",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.getTurnDiff]: (input) =>
|
|
observeRpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getTurnDiff,
|
|
checkpointDiffQuery.getTurnDiff(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetTurnDiffError({
|
|
message: "Failed to load turn diff",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.getFullThreadDiff]: (input) =>
|
|
observeRpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getFullThreadDiff,
|
|
checkpointDiffQuery.getFullThreadDiff(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetFullThreadDiffError({
|
|
message: "Failed to load full thread diff",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.replayEvents]: (input) =>
|
|
observeRpcEffect(
|
|
ORCHESTRATION_WS_METHODS.replayEvents,
|
|
Stream.runCollect(
|
|
orchestrationEngine.readEvents(
|
|
clamp(input.fromSequenceExclusive, {
|
|
maximum: Number.MAX_SAFE_INTEGER,
|
|
minimum: 0,
|
|
}),
|
|
),
|
|
).pipe(
|
|
Effect.map((events) => Array.from(events)),
|
|
Effect.flatMap(enrichOrchestrationEvents),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationReplayEventsError({
|
|
message: "Failed to replay orchestration events",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.subscribeShell]: (_input) =>
|
|
observeRpcStreamEffect(
|
|
ORCHESTRATION_WS_METHODS.subscribeShell,
|
|
Effect.gen(function* () {
|
|
const snapshot = yield* projectionSnapshotQuery.getShellSnapshot().pipe(
|
|
Effect.tapError((cause) =>
|
|
Effect.logError("orchestration shell snapshot load failed", { cause }),
|
|
),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetSnapshotError({
|
|
message: "Failed to load orchestration shell snapshot",
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
|
|
const liveStream = orchestrationEngine.streamDomainEvents.pipe(
|
|
Stream.mapEffect(toShellStreamEvent),
|
|
Stream.flatMap((event) =>
|
|
Option.isSome(event) ? Stream.succeed(event.value) : Stream.empty,
|
|
),
|
|
);
|
|
|
|
return Stream.concat(
|
|
Stream.make({
|
|
kind: "snapshot" as const,
|
|
snapshot,
|
|
}),
|
|
liveStream,
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.getArchivedShellSnapshot]: (_input) =>
|
|
observeRpcEffect(
|
|
ORCHESTRATION_WS_METHODS.getArchivedShellSnapshot,
|
|
projectionSnapshotQuery.getArchivedShellSnapshot().pipe(
|
|
Effect.tapError((cause) =>
|
|
Effect.logError("orchestration archived shell snapshot load failed", { cause }),
|
|
),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetSnapshotError({
|
|
message: "Failed to load archived orchestration shell snapshot",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[ORCHESTRATION_WS_METHODS.subscribeThread]: (input) =>
|
|
observeRpcStreamEffect(
|
|
ORCHESTRATION_WS_METHODS.subscribeThread,
|
|
Effect.gen(function* () {
|
|
const [threadDetail, snapshotSequence] = yield* Effect.all([
|
|
projectionSnapshotQuery.getThreadDetailById(input.threadId).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetSnapshotError({
|
|
message: `Failed to load thread ${input.threadId}`,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
projectionSnapshotQuery.getSnapshotSequence().pipe(
|
|
Effect.map(({ snapshotSequence }) => snapshotSequence),
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new OrchestrationGetSnapshotError({
|
|
message: "Failed to load orchestration snapshot sequence",
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
]);
|
|
|
|
if (Option.isNone(threadDetail)) {
|
|
return yield* new OrchestrationGetSnapshotError({
|
|
message: `Thread ${input.threadId} was not found`,
|
|
cause: input.threadId,
|
|
});
|
|
}
|
|
|
|
const liveStream = orchestrationEngine.streamDomainEvents.pipe(
|
|
Stream.filter(
|
|
(event) =>
|
|
event.aggregateKind === "thread" &&
|
|
event.aggregateId === input.threadId &&
|
|
isThreadDetailEvent(event),
|
|
),
|
|
Stream.map((event) => ({
|
|
kind: "event" as const,
|
|
event,
|
|
})),
|
|
);
|
|
|
|
return Stream.concat(
|
|
Stream.make({
|
|
kind: "snapshot" as const,
|
|
snapshot: {
|
|
snapshotSequence,
|
|
thread: threadDetail.value,
|
|
},
|
|
}),
|
|
liveStream,
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "orchestration" },
|
|
),
|
|
[WS_METHODS.serverGetConfig]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.serverGetConfig, loadServerConfig, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverRefreshProviders]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverRefreshProviders,
|
|
(input.instanceId !== undefined
|
|
? providerRegistry.refreshInstance(input.instanceId)
|
|
: providerRegistry.refresh()
|
|
).pipe(Effect.map((providers) => ({ providers }))),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverUpdateProvider]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverUpdateProvider,
|
|
providerMaintenanceRunner.updateProvider(input),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverUpsertKeybinding]: (rule) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverUpsertKeybinding,
|
|
Effect.gen(function* () {
|
|
const keybindingsConfig = yield* keybindings.upsertKeybindingRule(rule);
|
|
return { keybindings: keybindingsConfig, issues: [] };
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverRemoveKeybinding]: (rule) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverRemoveKeybinding,
|
|
Effect.gen(function* () {
|
|
const keybindingsConfig = yield* keybindings.removeKeybindingRule(rule);
|
|
return { keybindings: keybindingsConfig, issues: [] };
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverGetSettings]: (_input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverGetSettings,
|
|
serverSettings.getSettings.pipe(Effect.map(redactServerSettingsForClient)),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverUpdateSettings]: ({ patch }) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverUpdateSettings,
|
|
serverSettings.updateSettings(patch).pipe(Effect.map(redactServerSettingsForClient)),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverDiscoverSourceControl]: (_input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverDiscoverSourceControl,
|
|
sourceControlDiscovery.discover,
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverGetTraceDiagnostics]: (_input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverGetTraceDiagnostics,
|
|
TraceDiagnostics.readTraceDiagnostics({
|
|
traceFilePath: config.serverTracePath,
|
|
maxFiles: config.traceMaxFiles,
|
|
}),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverGetProcessDiagnostics]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.serverGetProcessDiagnostics, processDiagnostics.read, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverGetProcessResourceHistory]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverGetProcessResourceHistory,
|
|
processResourceMonitor.readHistory(input),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverGetResourceTelemetry]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.serverGetResourceTelemetry, resourceTelemetry.latest, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverGetResourceTelemetryHistory]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverGetResourceTelemetryHistory,
|
|
resourceTelemetry.readHistory(input),
|
|
{
|
|
"rpc.aggregate": "server",
|
|
},
|
|
),
|
|
[WS_METHODS.serverRetryResourceTelemetry]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.serverRetryResourceTelemetry, resourceTelemetry.retry, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverSignalProcess]: (input) =>
|
|
observeRpcEffect(WS_METHODS.serverSignalProcess, processDiagnostics.signal(input), {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.serverReportClientActivity]: (input, metadata) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverReportClientActivity,
|
|
backgroundPolicy.reportClientActivity(
|
|
currentSessionId,
|
|
RpcClientId.make(metadata.client.id),
|
|
input,
|
|
),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverReportHostPowerState]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.serverReportHostPowerState,
|
|
backgroundPolicy.reportHostPowerState(input),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.serverGetBackgroundPolicy]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.serverGetBackgroundPolicy, backgroundPolicy.snapshot, {
|
|
"rpc.aggregate": "server",
|
|
}),
|
|
[WS_METHODS.cloudGetRelayClientStatus]: (_input) =>
|
|
observeRpcEffect(WS_METHODS.cloudGetRelayClientStatus, relayClient.resolve, {
|
|
"rpc.aggregate": "cloud",
|
|
}),
|
|
[WS_METHODS.cloudInstallRelayClient]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.cloudInstallRelayClient,
|
|
Stream.callback<RelayClientInstallProgressEvent, RelayClientInstallFailedError>(
|
|
(queue) =>
|
|
relayClient
|
|
.installWithProgress((event) => Queue.offer(queue, event).pipe(Effect.asVoid))
|
|
.pipe(
|
|
Effect.flatMap((status) =>
|
|
Queue.offer(queue, {
|
|
type: "complete",
|
|
status,
|
|
}),
|
|
),
|
|
Effect.catchTag("RelayClientInstallError", (error) =>
|
|
Queue.fail(
|
|
queue,
|
|
new RelayClientInstallFailedError({
|
|
reason: error.reason,
|
|
message: error.message,
|
|
}),
|
|
),
|
|
),
|
|
Effect.andThen(Queue.end(queue)),
|
|
Effect.forkScoped,
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "cloud" },
|
|
),
|
|
[WS_METHODS.sourceControlLookupRepository]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.sourceControlLookupRepository,
|
|
sourceControlRepositories.lookupRepository(input),
|
|
{
|
|
"rpc.aggregate": "source-control",
|
|
},
|
|
),
|
|
[WS_METHODS.sourceControlCloneRepository]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.sourceControlCloneRepository,
|
|
sourceControlRepositories.cloneRepository(input),
|
|
{
|
|
"rpc.aggregate": "source-control",
|
|
},
|
|
),
|
|
[WS_METHODS.sourceControlPublishRepository]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.sourceControlPublishRepository,
|
|
sourceControlRepositories
|
|
.publishRepository(input)
|
|
.pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{
|
|
"rpc.aggregate": "source-control",
|
|
},
|
|
),
|
|
[WS_METHODS.projectsSearchEntries]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.projectsSearchEntries,
|
|
workspaceEntries.search(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProjectSearchEntriesError({
|
|
message: `Failed to search workspace entries: ${cause.detail}`,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.projectsListEntries]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.projectsListEntries,
|
|
workspaceEntries.list(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new ProjectListEntriesError({
|
|
message: `Failed to list workspace entries: ${cause.detail}`,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.projectsReadFile]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.projectsReadFile,
|
|
workspaceFileSystem.readFile(input).pipe(
|
|
Effect.mapError((cause) => {
|
|
const message = isWorkspacePathOutsideRootError(cause)
|
|
? "Workspace file path must stay within the project root."
|
|
: `Failed to read workspace file: ${cause.detail}`;
|
|
return new ProjectReadFileError({ message, cause });
|
|
}),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.projectsWriteFile]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.projectsWriteFile,
|
|
workspaceFileSystem.writeFile(input).pipe(
|
|
Effect.mapError((cause) => {
|
|
const message = isWorkspacePathOutsideRootError(cause)
|
|
? "Workspace file path must stay within the project root."
|
|
: "Failed to write workspace file";
|
|
return new ProjectWriteFileError({
|
|
message,
|
|
cause,
|
|
});
|
|
}),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.shellOpenInEditor]: (input) =>
|
|
observeRpcEffect(WS_METHODS.shellOpenInEditor, externalLauncher.launchEditor(input), {
|
|
"rpc.aggregate": "workspace",
|
|
}),
|
|
[WS_METHODS.filesystemBrowse]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.filesystemBrowse,
|
|
workspaceEntries.browse(input).pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new FilesystemBrowseError({
|
|
message: cause.detail,
|
|
cause,
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.assetsCreateUrl]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.assetsCreateUrl,
|
|
Effect.gen(function* () {
|
|
if (input.resource._tag !== "workspace-file") {
|
|
return yield* issueAssetUrl({ resource: input.resource });
|
|
}
|
|
const thread = yield* projectionSnapshotQuery
|
|
.getThreadShellById(input.resource.threadId)
|
|
.pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new AssetAccessError({
|
|
message: "Failed to resolve workspace context.",
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
if (Option.isNone(thread)) {
|
|
return yield* new AssetAccessError({
|
|
message: "Workspace context was not found.",
|
|
});
|
|
}
|
|
const project = yield* projectionSnapshotQuery
|
|
.getProjectShellById(thread.value.projectId)
|
|
.pipe(
|
|
Effect.mapError(
|
|
(cause) =>
|
|
new AssetAccessError({
|
|
message: "Failed to resolve workspace context.",
|
|
cause,
|
|
}),
|
|
),
|
|
);
|
|
if (Option.isNone(project)) {
|
|
return yield* new AssetAccessError({
|
|
message: "Workspace context was not found.",
|
|
});
|
|
}
|
|
return yield* issueAssetUrl({
|
|
resource: input.resource,
|
|
workspaceRoot: thread.value.worktreePath ?? project.value.workspaceRoot,
|
|
});
|
|
}),
|
|
{ "rpc.aggregate": "workspace" },
|
|
),
|
|
[WS_METHODS.subscribeVcsStatus]: (input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeVcsStatus,
|
|
vcsStatusBroadcaster.streamStatus(input, {
|
|
automaticRemoteRefreshInterval: automaticGitFetchInterval,
|
|
}),
|
|
{
|
|
"rpc.aggregate": "vcs",
|
|
},
|
|
),
|
|
[WS_METHODS.vcsRefreshStatus]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsRefreshStatus,
|
|
vcsStatusBroadcaster.refreshStatus(input.cwd),
|
|
{
|
|
"rpc.aggregate": "vcs",
|
|
},
|
|
),
|
|
[WS_METHODS.vcsPull]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsPull,
|
|
gitWorkflow.pullCurrentBranch(input.cwd).pipe(
|
|
Effect.matchCauseEffect({
|
|
onFailure: (cause) => Effect.failCause(cause),
|
|
onSuccess: (result) =>
|
|
refreshGitStatus(input.cwd).pipe(Effect.ignore({ log: true }), Effect.as(result)),
|
|
}),
|
|
),
|
|
{ "rpc.aggregate": "git" },
|
|
),
|
|
[WS_METHODS.gitRunStackedAction]: (input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.gitRunStackedAction,
|
|
Stream.callback<GitActionProgressEvent, GitManagerServiceError>((queue) =>
|
|
gitWorkflow
|
|
.runStackedAction(input, {
|
|
actionId: input.actionId,
|
|
progressReporter: {
|
|
publish: (event) => Queue.offer(queue, event).pipe(Effect.asVoid),
|
|
},
|
|
})
|
|
.pipe(
|
|
Effect.matchCauseEffect({
|
|
onFailure: (cause) => Queue.failCause(queue, cause),
|
|
onSuccess: () =>
|
|
refreshGitStatus(input.cwd).pipe(
|
|
Effect.andThen(Queue.end(queue).pipe(Effect.asVoid)),
|
|
),
|
|
}),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.gitResolvePullRequest]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.gitResolvePullRequest,
|
|
gitWorkflow.resolvePullRequest(input),
|
|
{
|
|
"rpc.aggregate": "git",
|
|
},
|
|
),
|
|
[WS_METHODS.gitPreparePullRequestThread]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.gitPreparePullRequestThread,
|
|
gitWorkflow
|
|
.preparePullRequestThread(input)
|
|
.pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "git" },
|
|
),
|
|
[WS_METHODS.vcsListRefs]: (input) =>
|
|
observeRpcEffect(WS_METHODS.vcsListRefs, gitWorkflow.listRefs(input), {
|
|
"rpc.aggregate": "vcs",
|
|
}),
|
|
[WS_METHODS.vcsCreateWorktree]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsCreateWorktree,
|
|
gitWorkflow.createWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.vcsRemoveWorktree]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsRemoveWorktree,
|
|
gitWorkflow.removeWorktree(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.vcsCreateRef]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsCreateRef,
|
|
gitWorkflow.createRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.vcsSwitchRef]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsSwitchRef,
|
|
gitWorkflow.switchRef(input).pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.vcsInit]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.vcsInit,
|
|
vcsProvisioning
|
|
.initRepository(input)
|
|
.pipe(Effect.tap(() => refreshGitStatus(input.cwd))),
|
|
{ "rpc.aggregate": "vcs" },
|
|
),
|
|
[WS_METHODS.reviewGetDiffPreview]: (input) =>
|
|
observeRpcEffect(WS_METHODS.reviewGetDiffPreview, review.getDiffPreview(input), {
|
|
"rpc.aggregate": "review",
|
|
}),
|
|
[WS_METHODS.terminalOpen]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalOpen, terminalManager.open(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalAttach]: (input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.terminalAttach,
|
|
Stream.callback<TerminalAttachStreamEvent, TerminalError>((queue) =>
|
|
Effect.acquireRelease(
|
|
terminalManager.attachStream(input, (event) => Queue.offer(queue, event)),
|
|
(unsubscribe) => Effect.sync(unsubscribe),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "terminal" },
|
|
),
|
|
[WS_METHODS.terminalWrite]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalWrite, terminalManager.write(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalResize]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalResize, terminalManager.resize(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalClear]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalClear, terminalManager.clear(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalRestart]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalRestart, terminalManager.restart(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.terminalClose]: (input) =>
|
|
observeRpcEffect(WS_METHODS.terminalClose, terminalManager.close(input), {
|
|
"rpc.aggregate": "terminal",
|
|
}),
|
|
[WS_METHODS.subscribeTerminalEvents]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeTerminalEvents,
|
|
Stream.callback<TerminalEvent>((queue) =>
|
|
Effect.acquireRelease(
|
|
terminalManager.subscribe((event) => Queue.offer(queue, event)),
|
|
(unsubscribe) => Effect.sync(unsubscribe),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "terminal" },
|
|
),
|
|
[WS_METHODS.subscribeTerminalMetadata]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeTerminalMetadata,
|
|
Stream.callback<TerminalMetadataStreamEvent>((queue) =>
|
|
Effect.acquireRelease(
|
|
terminalManager.subscribeMetadata((event) => Queue.offer(queue, event)),
|
|
(unsubscribe) => Effect.sync(unsubscribe),
|
|
),
|
|
),
|
|
{ "rpc.aggregate": "terminal" },
|
|
),
|
|
[WS_METHODS.previewOpen]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewOpen, previewManager.open(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewNavigate]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewNavigate, previewManager.navigate(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewRefresh]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewRefresh, previewManager.refresh(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewClose]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewClose, previewManager.close(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewList]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewList, previewManager.list(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewReportStatus]: (input) =>
|
|
observeRpcEffect(WS_METHODS.previewReportStatus, previewManager.reportStatus(input), {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.previewAutomationConnect]: (input) =>
|
|
observeRpcStreamEffect(
|
|
WS_METHODS.previewAutomationConnect,
|
|
previewAutomationBroker.connect(input.clientId),
|
|
{ "rpc.aggregate": "preview-automation" },
|
|
),
|
|
[WS_METHODS.previewAutomationRespond]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.previewAutomationRespond,
|
|
previewAutomationBroker.respond(input),
|
|
{ "rpc.aggregate": "preview-automation" },
|
|
),
|
|
[WS_METHODS.previewAutomationReportOwner]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.previewAutomationReportOwner,
|
|
previewAutomationBroker.reportOwner(input),
|
|
{ "rpc.aggregate": "preview-automation" },
|
|
),
|
|
[WS_METHODS.previewAutomationClearOwner]: (input) =>
|
|
observeRpcEffect(
|
|
WS_METHODS.previewAutomationClearOwner,
|
|
previewAutomationBroker.clearOwner(input.clientId),
|
|
{ "rpc.aggregate": "preview-automation" },
|
|
),
|
|
[WS_METHODS.subscribePreviewEvents]: (_input) =>
|
|
observeRpcStream(WS_METHODS.subscribePreviewEvents, previewManager.events, {
|
|
"rpc.aggregate": "preview",
|
|
}),
|
|
[WS_METHODS.subscribeDiscoveredLocalServers]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeDiscoveredLocalServers,
|
|
Stream.callback<DiscoveredLocalServerList>((queue) =>
|
|
Effect.gen(function* () {
|
|
yield* portDiscovery.retain;
|
|
const initial = yield* portDiscovery.scan();
|
|
const initialScannedAt = DateTime.formatIso(yield* DateTime.now);
|
|
yield* Queue.offer(queue, {
|
|
servers: initial,
|
|
scannedAt: initialScannedAt,
|
|
});
|
|
yield* portDiscovery.subscribe((servers) =>
|
|
Effect.gen(function* () {
|
|
const scannedAt = DateTime.formatIso(yield* DateTime.now);
|
|
yield* Queue.offer(queue, { servers, scannedAt });
|
|
}),
|
|
);
|
|
}),
|
|
),
|
|
{ "rpc.aggregate": "preview" },
|
|
),
|
|
[WS_METHODS.subscribeServerConfig]: (_input) =>
|
|
observeRpcStreamEffect(
|
|
WS_METHODS.subscribeServerConfig,
|
|
Effect.gen(function* () {
|
|
const keybindingsUpdates = keybindings.streamChanges.pipe(
|
|
Stream.map((event) => ({
|
|
version: 1 as const,
|
|
type: "keybindingsUpdated" as const,
|
|
payload: {
|
|
keybindings: event.keybindings,
|
|
issues: event.issues,
|
|
},
|
|
})),
|
|
);
|
|
const providerStatuses = providerRegistry.streamChanges.pipe(
|
|
Stream.map((providers) => ({
|
|
version: 1 as const,
|
|
type: "providerStatuses" as const,
|
|
payload: { providers },
|
|
})),
|
|
Stream.debounce(Duration.millis(PROVIDER_STATUS_DEBOUNCE_MS)),
|
|
);
|
|
const settingsUpdates = serverSettings.streamChanges.pipe(
|
|
Stream.map((settings) => redactServerSettingsForClient(settings)),
|
|
Stream.map((settings) => ({
|
|
version: 1 as const,
|
|
type: "settingsUpdated" as const,
|
|
payload: { settings },
|
|
})),
|
|
);
|
|
|
|
yield* providerRegistry
|
|
.refresh()
|
|
.pipe(Effect.ignoreCause({ log: true }), Effect.forkScoped);
|
|
|
|
const liveUpdates = Stream.merge(
|
|
keybindingsUpdates,
|
|
Stream.merge(providerStatuses, settingsUpdates),
|
|
);
|
|
|
|
return Stream.concat(
|
|
Stream.make({
|
|
version: 1 as const,
|
|
type: "snapshot" as const,
|
|
config: yield* loadServerConfig,
|
|
}),
|
|
liveUpdates,
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.subscribeServerLifecycle]: (_input) =>
|
|
observeRpcStreamEffect(
|
|
WS_METHODS.subscribeServerLifecycle,
|
|
Effect.gen(function* () {
|
|
const snapshot = yield* lifecycleEvents.snapshot;
|
|
const snapshotEvents = Array.from(snapshot.events).toSorted(
|
|
(left, right) => left.sequence - right.sequence,
|
|
);
|
|
const liveEvents = lifecycleEvents.stream.pipe(
|
|
Stream.filter((event) => event.sequence > snapshot.sequence),
|
|
);
|
|
return Stream.concat(Stream.fromIterable(snapshotEvents), liveEvents);
|
|
}),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.subscribeAuthAccess]: (_input) =>
|
|
observeRpcStreamEffect(
|
|
WS_METHODS.subscribeAuthAccess,
|
|
Effect.gen(function* () {
|
|
const initialSnapshot = yield* loadAuthAccessSnapshot();
|
|
const revisionRef = yield* Ref.make(1);
|
|
const accessChanges: Stream.Stream<
|
|
PairingGrantStore.BootstrapCredentialChange | SessionStore.SessionCredentialChange
|
|
> = Stream.merge(bootstrapCredentials.streamChanges, sessions.streamChanges);
|
|
|
|
const liveEvents: Stream.Stream<AuthAccessStreamEvent> = accessChanges.pipe(
|
|
Stream.mapEffect((change) =>
|
|
Ref.updateAndGet(revisionRef, (revision) => revision + 1).pipe(
|
|
Effect.map((revision) =>
|
|
toAuthAccessStreamEvent(change, revision, currentSessionId),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
|
|
return Stream.concat(
|
|
Stream.make({
|
|
version: 1 as const,
|
|
revision: 1,
|
|
type: "snapshot" as const,
|
|
payload: initialSnapshot,
|
|
}),
|
|
liveEvents,
|
|
);
|
|
}),
|
|
{ "rpc.aggregate": "auth" },
|
|
),
|
|
[WS_METHODS.subscribeBackgroundPolicy]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeBackgroundPolicy,
|
|
Stream.concat(
|
|
Stream.unwrap(Effect.map(backgroundPolicy.snapshot, Stream.make)),
|
|
backgroundPolicy.streamChanges,
|
|
),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
[WS_METHODS.subscribeResourceTelemetry]: (_input) =>
|
|
observeRpcStream(
|
|
WS_METHODS.subscribeResourceTelemetry,
|
|
Stream.concat(
|
|
Stream.unwrap(Effect.map(resourceTelemetry.latest, Stream.make)),
|
|
resourceTelemetry.changes,
|
|
),
|
|
{ "rpc.aggregate": "server" },
|
|
),
|
|
});
|
|
}),
|
|
);
|
|
|
|
export const websocketRpcRouteLayer = Layer.unwrap(
|
|
Effect.succeed(
|
|
HttpRouter.add(
|
|
"GET",
|
|
"/ws",
|
|
Effect.gen(function* () {
|
|
const request = yield* HttpServerRequest.HttpServerRequest;
|
|
const serverAuth = yield* EnvironmentAuth.EnvironmentAuth;
|
|
const sessions = yield* SessionStore.SessionStore;
|
|
const session = yield* serverAuth.authenticateWebSocketUpgrade(request).pipe(
|
|
Effect.catchTags({
|
|
ServerAuthInvalidCredentialError: (error) => failEnvironmentAuthInvalid(error.reason),
|
|
ServerAuthInternalError: (error) => failEnvironmentInternal("internal_error", error),
|
|
}),
|
|
);
|
|
const rpcWebSocketHttpEffect = yield* RpcServer.toHttpEffectWebsocket(WsRpcGroup, {
|
|
disableTracing: true,
|
|
}).pipe(
|
|
Effect.provide(
|
|
makeWsRpcLayer(session).pipe(
|
|
Layer.provideMerge(RpcSerialization.layerJson),
|
|
Layer.provide(PreviewAutomationBroker.layer),
|
|
Layer.provide(ProviderMaintenanceRunner.layer),
|
|
Layer.provide(
|
|
SourceControlDiscoveryLayer.layer.pipe(
|
|
Layer.provide(
|
|
SourceControlProviderRegistry.layer.pipe(
|
|
Layer.provide(
|
|
Layer.mergeAll(
|
|
AzureDevOpsCli.layer,
|
|
BitbucketApi.layer,
|
|
GitHubCli.layer,
|
|
GitLabCli.layer,
|
|
),
|
|
),
|
|
Layer.provideMerge(GitVcsDriver.layer),
|
|
Layer.provide(
|
|
VcsDriverRegistry.layer.pipe(Layer.provide(VcsProjectConfig.layer)),
|
|
),
|
|
),
|
|
),
|
|
Layer.provide(VcsProcess.layer),
|
|
),
|
|
),
|
|
),
|
|
),
|
|
);
|
|
return yield* Effect.acquireUseRelease(
|
|
sessions.markConnected(session.sessionId),
|
|
() => rpcWebSocketHttpEffect,
|
|
() => sessions.markDisconnected(session.sessionId),
|
|
);
|
|
}).pipe(
|
|
Effect.catchTags({
|
|
EnvironmentAuthInvalidError: HttpServerRespondable.toResponse,
|
|
EnvironmentInternalError: HttpServerRespondable.toResponse,
|
|
}),
|
|
),
|
|
),
|
|
),
|
|
);
|