@devries-cameron20 / t3code Mirror · read-only / commits / f7c422d581
Fork 0

The best way to control Claude Code, Codex and more from any computer or phone

Mirrored from https://github.com/Camo67/t3code.git

Synced Nov 25, 03:45 UTC

Refactor MCP services into top-level modules

- Move MCP session registry and preview broker out of `Layers/` and `Services/` - Update imports, tests, and server wiring to use the new module layout

Julius Marminge committed Jun 12, 2026 at 16:55 UTC f7c422d5815e7be03415a87b30558c7bcf2cf8e9
30 files changed +866 -942
apps/server/src/mcp/Layers/McpSessionRegistry.ts deleted
-173
@@ -1,173 +0,0 @@
1 -import { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2 -import * as Crypto from "effect/Crypto";
3 -import * as Effect from "effect/Effect";
4 -import * as Layer from "effect/Layer";
5 -import * as Ref from "effect/Ref";
6 -import { HttpServer } from "effect/unstable/http";
7 -
8 -import { ServerEnvironment } from "../../environment/Services/ServerEnvironment.ts";
9 -import type { McpInvocationScope } from "../Services/McpInvocationContext.ts";
10 -import {
11 - McpSessionRegistry,
12 - type McpCredentialRequest,
13 - type McpIssuedCredential,
14 - type McpSessionRegistryShape,
15 -} from "../Services/McpSessionRegistry.ts";
16 -
17 -interface CredentialRecord {
18 - readonly tokenHash: string;
19 - readonly scope: McpInvocationScope;
20 - readonly lastUsedAt: number;
21 -}
22 -
23 -interface RegistryState {
24 - readonly records: ReadonlyMap<string, CredentialRecord>;
25 -}
26 -
27 -export interface McpSessionRegistryOptions {
28 - readonly idleTimeoutMs?: number;
29 - readonly maximumLifetimeMs?: number;
30 - readonly now?: () => number;
31 -}
32 -
33 -const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;
34 -const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000;
35 -
36 -const bytesToHex = (bytes: Uint8Array): string =>
37 - Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
38 -
39 -const tokenFromBytes = (bytes: Uint8Array): string => Buffer.from(bytes).toString("base64url");
40 -
41 -export const makeMcpSessionRegistry = (options: McpSessionRegistryOptions = {}) =>
42 - Effect.gen(function* () {
43 - const crypto = yield* Crypto.Crypto;
44 - const environment = yield* ServerEnvironment;
45 - const environmentId = yield* environment.getEnvironmentId;
46 - const httpServer = yield* HttpServer.HttpServer;
47 - const state = yield* Ref.make<RegistryState>({ records: new Map() });
48 - const now = options.now ?? Date.now;
49 - const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
50 - const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;
51 - const endpoint =
52 - httpServer.address._tag === "TcpAddress"
53 - ? `http://127.0.0.1:${httpServer.address.port}/mcp`
54 - : "http://127.0.0.1/mcp";
55 -
56 - const hashToken = (token: string) =>
57 - crypto
58 - .digest("SHA-256", new TextEncoder().encode(token))
59 - .pipe(Effect.map(bytesToHex), Effect.orDie);
60 -
61 - const pruneExpired = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
62 - let changed = false;
63 - const next = new Map<string, CredentialRecord>();
64 - for (const [hash, record] of records) {
65 - if (timestamp <= record.scope.expiresAt && timestamp - record.lastUsedAt <= idleTimeoutMs) {
66 - next.set(hash, record);
67 - } else {
68 - changed = true;
69 - }
70 - }
71 - return changed ? next : records;
72 - };
73 -
74 - const issue: McpSessionRegistryShape["issue"] = (request) =>
75 - Effect.gen(function* () {
76 - const issuedAt = now();
77 - const providerSessionId = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
78 - const rawToken = yield* crypto
79 - .randomBytes(32)
80 - .pipe(Effect.map(tokenFromBytes), Effect.orDie);
81 - const tokenHash = yield* hashToken(rawToken);
82 - const expiresAt = issuedAt + maximumLifetimeMs;
83 - const scope: McpInvocationScope = {
84 - environmentId,
85 - threadId: ThreadId.make(request.threadId),
86 - providerSessionId,
87 - providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),
88 - capabilities: new Set(["preview"]),
89 - issuedAt,
90 - expiresAt,
91 - };
92 - yield* Ref.update(state, ({ records }) => {
93 - const next = new Map(pruneExpired(records, issuedAt));
94 - next.set(tokenHash, { tokenHash, scope, lastUsedAt: issuedAt });
95 - return { records: next };
96 - });
97 - return {
98 - config: {
99 - environmentId,
100 - threadId: scope.threadId,
101 - providerSessionId,
102 - providerInstanceId: scope.providerInstanceId,
103 - endpoint,
104 - authorizationHeader: `Bearer ${rawToken}`,
105 - },
106 - expiresAt,
107 - };
108 - });
109 -
110 - const resolve: McpSessionRegistryShape["resolve"] = (rawToken) =>
111 - Effect.gen(function* () {
112 - if (rawToken.length === 0) return undefined;
113 - const tokenHash = yield* hashToken(rawToken);
114 - const timestamp = now();
115 - let resolved: McpInvocationScope | undefined;
116 - yield* Ref.update(state, ({ records }) => {
117 - const current = pruneExpired(records, timestamp);
118 - const record = current.get(tokenHash);
119 - if (!record) return { records: current };
120 - resolved = record.scope;
121 - const next = new Map(current);
122 - next.set(tokenHash, { ...record, lastUsedAt: timestamp });
123 - return { records: next };
124 - });
125 - return resolved;
126 - });
127 -
128 - const revokeWhere = (predicate: (record: CredentialRecord) => boolean) =>
129 - Ref.update(state, ({ records }) => ({
130 - records: new Map(Array.from(records).filter(([, record]) => !predicate(record))),
131 - }));
132 -
133 - return McpSessionRegistry.of({
134 - issue,
135 - resolve,
136 - revokeProviderSession: (providerSessionId) =>
137 - revokeWhere((record) => record.scope.providerSessionId === providerSessionId),
138 - revokeThread: (threadId) => revokeWhere((record) => record.scope.threadId === threadId),
139 - revokeAll: Ref.set(state, { records: new Map() }),
140 - });
141 - });
142 -
143 -let activeMcpSessionRegistry: McpSessionRegistryShape | undefined;
144 -
145 -export const McpSessionRegistryLive: Layer.Layer<
146 - McpSessionRegistry,
147 - never,
148 - Crypto.Crypto | ServerEnvironment | HttpServer.HttpServer
149 -> = Layer.effect(
150 - McpSessionRegistry,
151 - makeMcpSessionRegistry().pipe(
152 - Effect.tap((registry) =>
153 - Effect.sync(() => {
154 - activeMcpSessionRegistry = registry;
155 - }),
156 - ),
157 - ),
158 -);
159 -
160 -export const issueActiveMcpCredential = (
161 - request: McpCredentialRequest,
162 -): Effect.Effect<McpIssuedCredential | undefined> =>
163 - activeMcpSessionRegistry
164 - ? activeMcpSessionRegistry
165 - .revokeThread(request.threadId)
166 - .pipe(Effect.andThen(activeMcpSessionRegistry.issue(request)))
167 - : Effect.sync((): McpIssuedCredential | undefined => undefined);
168 -
169 -export const revokeActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>
170 - activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeThread(threadId) : Effect.void;
171 -
172 -export const revokeAllActiveMcpCredentials = (): Effect.Effect<void> =>
173 - activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeAll : Effect.void;
apps/server/src/mcp/Layers/PreviewAutomationBroker.ts deleted
-270
@@ -1,270 +0,0 @@
1 -import {
2 - PreviewAutomationControlInterruptedError,
3 - PreviewAutomationExecutionError,
4 - PreviewAutomationInvalidSelectorError,
5 - PreviewAutomationNoFocusedOwnerError,
6 - PreviewAutomationResultTooLargeError,
7 - PreviewAutomationTabNotFoundError,
8 - PreviewAutomationTimeoutError,
9 - PreviewAutomationUnavailableError,
10 - PreviewAutomationUnsupportedClientError,
11 - type PreviewAutomationError,
12 - type PreviewAutomationOwner,
13 - type PreviewAutomationResponse,
14 -} from "@t3tools/contracts";
15 -import * as Deferred from "effect/Deferred";
16 -import * as Effect from "effect/Effect";
17 -import * as Layer from "effect/Layer";
18 -import * as Option from "effect/Option";
19 -import * as Queue from "effect/Queue";
20 -import * as Ref from "effect/Ref";
21 -import * as Stream from "effect/Stream";
22 -
23 -import {
24 - PreviewAutomationBroker,
25 - type PreviewAutomationBrokerShape,
26 -} from "../Services/PreviewAutomationBroker.ts";
27 -
28 -interface ClientConnection {
29 - readonly clientId: string;
30 - readonly queue: Queue.Queue<
31 - Parameters<PreviewAutomationBrokerShape["respond"]>[0] extends never
32 - ? never
33 - : import("@t3tools/contracts").PreviewAutomationRequest
34 - >;
35 -}
36 -
37 -interface PendingRequest {
38 - readonly clientId: string;
39 - readonly deferred: Deferred.Deferred<unknown, PreviewAutomationError>;
40 -}
41 -
42 -interface BrokerState {
43 - readonly clients: ReadonlyMap<string, ClientConnection>;
44 - readonly owners: ReadonlyMap<string, PreviewAutomationOwner>;
45 - readonly pending: ReadonlyMap<string, PendingRequest>;
46 -}
47 -
48 -const makeResponseError = (
49 - error: NonNullable<PreviewAutomationResponse["error"]>,
50 -): PreviewAutomationError => {
51 - switch (error._tag) {
52 - case "PreviewAutomationNoFocusedOwnerError":
53 - return new PreviewAutomationNoFocusedOwnerError({ message: error.message });
54 - case "PreviewAutomationUnsupportedClientError":
55 - return new PreviewAutomationUnsupportedClientError({ message: error.message });
56 - case "PreviewAutomationTabNotFoundError":
57 - return new PreviewAutomationTabNotFoundError({ message: error.message });
58 - case "PreviewAutomationTimeoutError":
59 - return new PreviewAutomationTimeoutError({ message: error.message });
60 - case "PreviewAutomationControlInterruptedError":
61 - return new PreviewAutomationControlInterruptedError({ message: error.message });
62 - case "PreviewAutomationInvalidSelectorError": {
63 - const detail =
64 - typeof error.detail === "object" && error.detail !== null ? error.detail : undefined;
65 - return new PreviewAutomationInvalidSelectorError({
66 - message: error.message,
67 - selector:
68 - detail && "selector" in detail && typeof detail.selector === "string"
69 - ? detail.selector
70 - : "",
71 - });
72 - }
73 - case "PreviewAutomationResultTooLargeError": {
74 - const detail =
75 - typeof error.detail === "object" && error.detail !== null ? error.detail : undefined;
76 - return new PreviewAutomationResultTooLargeError({
77 - message: error.message,
78 - maximumBytes:
79 - detail && "maximumBytes" in detail && typeof detail.maximumBytes === "number"
80 - ? detail.maximumBytes
81 - : 64_000,
82 - });
83 - }
84 - case "PreviewAutomationUnavailableError":
85 - return new PreviewAutomationUnavailableError({ message: error.message });
86 - default:
87 - return new PreviewAutomationExecutionError({
88 - message: error.message,
89 - detail: error.detail,
90 - });
91 - }
92 -};
93 -
94 -export const makePreviewAutomationBroker = (): PreviewAutomationBrokerShape => {
95 - const state = Effect.runSync(
96 - Ref.make<BrokerState>({
97 - clients: new Map(),
98 - owners: new Map(),
99 - pending: new Map(),
100 - }),
101 - );
102 - let requestSequence = 0;
103 -
104 - const disconnect = (clientId: string, queue: ClientConnection["queue"]) =>
105 - Effect.gen(function* () {
106 - const toFail: PendingRequest[] = [];
107 - yield* Ref.update(state, (current) => {
108 - if (current.clients.get(clientId)?.queue !== queue) return current;
109 - const clients = new Map(current.clients);
110 - const owners = new Map(current.owners);
111 - const pending = new Map(current.pending);
112 - clients.delete(clientId);
113 - owners.delete(clientId);
114 - for (const [requestId, entry] of pending) {
115 - if (entry.clientId === clientId) {
116 - pending.delete(requestId);
117 - toFail.push(entry);
118 - }
119 - }
120 - return { clients, owners, pending };
121 - });
122 - yield* Effect.forEach(
123 - toFail,
124 - ({ deferred }) =>
125 - Deferred.fail(
126 - deferred,
127 - new PreviewAutomationUnavailableError({
128 - message: "The preview automation client disconnected.",
129 - }),
130 - ),
131 - { discard: true },
132 - );
133 - yield* Queue.shutdown(queue);
134 - });
135 -
136 - const connect: PreviewAutomationBrokerShape["connect"] = (clientId) =>
137 - Effect.gen(function* () {
138 - const queue = yield* Queue.unbounded<import("@t3tools/contracts").PreviewAutomationRequest>();
139 - let previous: ClientConnection | undefined;
140 - yield* Ref.update(state, (current) => {
141 - previous = current.clients.get(clientId);
142 - const clients = new Map(current.clients);
143 - clients.set(clientId, { clientId, queue });
144 - return { ...current, clients };
145 - });
146 - if (previous) yield* disconnect(clientId, previous.queue);
147 - return Stream.fromQueue(queue).pipe(Stream.ensuring(disconnect(clientId, queue)));
148 - });
149 -
150 - const reportOwner: PreviewAutomationBrokerShape["reportOwner"] = (owner) =>
151 - Ref.update(state, (current) => {
152 - const owners = new Map(current.owners);
153 - owners.set(owner.clientId, owner);
154 - return { ...current, owners };
155 - });
156 -
157 - const clearOwner: PreviewAutomationBrokerShape["clearOwner"] = (clientId) =>
158 - Ref.update(state, (current) => {
159 - const owners = new Map(current.owners);
160 - owners.delete(clientId);
161 - return { ...current, owners };
162 - });
163 -
164 - const respond: PreviewAutomationBrokerShape["respond"] = (response) =>
165 - Effect.gen(function* () {
166 - let pending: PendingRequest | undefined;
167 - yield* Ref.update(state, (current) => {
168 - pending = current.pending.get(response.requestId);
169 - if (!pending) return current;
170 - const next = new Map(current.pending);
171 - next.delete(response.requestId);
172 - return { ...current, pending: next };
173 - });
174 - if (!pending) return;
175 - if (response.ok) {
176 - yield* Deferred.succeed(pending.deferred, response.result);
177 - } else {
178 - yield* Deferred.fail(
179 - pending.deferred,
180 - response.error
181 - ? makeResponseError(response.error)
182 - : new PreviewAutomationExecutionError({
183 - message: "Preview automation failed without an error payload.",
184 - }),
185 - );
186 - }
187 - });
188 -
189 - const invoke = <A = unknown>(
190 - input: Parameters<PreviewAutomationBrokerShape["invoke"]>[0],
191 - ): Effect.Effect<A, PreviewAutomationError> =>
192 - Effect.gen(function* () {
193 - const current = yield* Ref.get(state);
194 - const candidates = Array.from(current.owners.values())
195 - .filter(
196 - (owner) =>
197 - owner.environmentId === input.scope.environmentId &&
198 - owner.threadId === input.scope.threadId &&
199 - owner.supportsAutomation,
200 - )
201 - .sort((left, right) => right.focusedAt.localeCompare(left.focusedAt));
202 - const owner = candidates[0];
203 - if (!owner) {
204 - return yield* new PreviewAutomationNoFocusedOwnerError({
205 - message: "No desktop browser host is available for this thread.",
206 - });
207 - }
208 - const connection = current.clients.get(owner.clientId);
209 - if (!connection) {
210 - return yield* new PreviewAutomationUnavailableError({
211 - message: "The browser host is not connected.",
212 - });
213 - }
214 - if (
215 - input.operation !== "open" &&
216 - input.operation !== "status" &&
217 - !owner.tabId &&
218 - !input.tabId
219 - ) {
220 - return yield* new PreviewAutomationTabNotFoundError({
221 - message: "The browser host does not have an active tab.",
222 - });
223 - }
224 - const requestId = `preview-${requestSequence++}`;
225 - const timeoutMs = input.timeoutMs ?? 15_000;
226 - const deferred = yield* Deferred.make<unknown, PreviewAutomationError>();
227 - yield* Ref.update(state, (next) => {
228 - const pending = new Map(next.pending);
229 - pending.set(requestId, { clientId: owner.clientId, deferred });
230 - return { ...next, pending };
231 - });
232 - const offered = yield* Queue.offer(connection.queue, {
233 - requestId,
234 - threadId: input.scope.threadId,
235 - tabId: input.tabId ?? owner.tabId ?? undefined,
236 - operation: input.operation,
237 - input: input.input,
238 - timeoutMs,
239 - });
240 - if (!offered) {
241 - return yield* new PreviewAutomationUnavailableError({
242 - message: "The preview automation client is no longer accepting requests.",
243 - });
244 - }
245 - const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(timeoutMs));
246 - yield* Ref.update(state, (next) => {
247 - const pending = new Map(next.pending);
248 - pending.delete(requestId);
249 - return { ...next, pending };
250 - });
251 - return yield* Option.match(result, {
252 - onNone: () =>
253 - Effect.fail(
254 - new PreviewAutomationTimeoutError({
255 - message: `Preview automation timed out after ${timeoutMs}ms.`,
256 - }),
257 - ),
258 - onSome: (value) => Effect.succeed(value as A),
259 - });
260 - });
261 -
262 - return PreviewAutomationBroker.of({ connect, reportOwner, clearOwner, respond, invoke });
263 -};
264 -
265 -export const previewAutomationBroker = makePreviewAutomationBroker();
266 -
267 -export const PreviewAutomationBrokerLive: Layer.Layer<PreviewAutomationBroker> = Layer.succeed(
268 - PreviewAutomationBroker,
269 - previewAutomationBroker,
270 -);
apps/server/src/mcp/McpHttpServer.test.ts renamed
+15 -13
@@ -6,9 +6,9 @@ import * as Stream from "effect/Stream";
6 import { McpSchema, McpServer } from "effect/unstable/ai";
7 import { HttpServerResponse } from "effect/unstable/http";
8
9 -import { McpInvocationContext } from "../Services/McpInvocationContext.ts";
10 -import { normalizeMcpHttpResponse, PreviewToolkitRegistrationLive } from "./McpHttpServer.ts";
11 -import { previewAutomationBroker } from "./PreviewAutomationBroker.ts";
9 +import * as McpHttpServer from "./McpHttpServer.ts";
10 +import * as McpInvocationContext from "./McpInvocationContext.ts";
11 +import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
12
13 const environmentId = EnvironmentId.make("environment-mcp-test");
14 const threadId = ThreadId.make("thread-mcp-test");
@@ -31,17 +31,18 @@ const client = McpSchema.McpServerClient.of({
31 },
32 getClient: Effect.die("unused"),
33 });
34 -const TestLayer = PreviewToolkitRegistrationLive.pipe(
34 +const TestLayer = McpHttpServer.PreviewToolkitRegistrationLive.pipe(
35 Layer.provideMerge(McpServer.McpServer.layer),
36 + Layer.provideMerge(PreviewAutomationBroker.layer),
37 );
38
39 it("normalizes empty successful notification responses to accepted", () => {
39 - const notificationResponse = normalizeMcpHttpResponse(
40 + const notificationResponse = McpHttpServer.normalizeMcpHttpResponse(
41 HttpServerResponse.text("", { status: 200, contentType: "application/json" }),
42 );
43 expect(notificationResponse.status).toBe(202);
44
44 - const resultResponse = normalizeMcpHttpResponse(
45 + const resultResponse = McpHttpServer.normalizeMcpHttpResponse(
46 HttpServerResponse.jsonUnsafe({ jsonrpc: "2.0", id: 1, result: {} }),
47 );
48 expect(resultResponse.status).toBe(200);
@@ -51,9 +52,10 @@ it.effect("registers annotated tools and preserves authenticated request context
52 Effect.scoped(
53 Effect.gen(function* () {
54 const server = yield* McpServer.McpServer;
54 - const requests = yield* previewAutomationBroker.connect("mcp-test-client");
55 + const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
56 + const requests = yield* broker.connect("mcp-test-client");
57 yield* Stream.runForEach(requests, (request) =>
56 - previewAutomationBroker.respond({
58 + broker.respond({
59 requestId: request.requestId,
60 ok: true,
61 result:
@@ -88,7 +90,7 @@ it.effect("registers annotated tools and preserves authenticated request context
90 }),
91 ).pipe(Effect.forkScoped);
92 yield* Effect.yieldNow;
91 - yield* previewAutomationBroker.reportOwner({
93 + yield* broker.reportOwner({
94 clientId: "mcp-test-client",
95 environmentId,
96 threadId,
@@ -120,7 +122,7 @@ it.effect("registers annotated tools and preserves authenticated request context
122 const status = yield* server
123 .callTool({ name: "preview_status", arguments: {} })
124 .pipe(
123 - Effect.provideService(McpInvocationContext, invocation),
125 + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
126 Effect.provideService(McpSchema.McpServerClient, client),
127 );
128 expect(status.isError).toBe(false);
@@ -132,7 +134,7 @@ it.effect("registers annotated tools and preserves authenticated request context
134 const malformed = yield* server
135 .callTool({ name: "preview_click", arguments: { selector: "" } })
136 .pipe(
135 - Effect.provideService(McpInvocationContext, invocation),
137 + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
138 Effect.provideService(McpSchema.McpServerClient, client),
139 );
140 expect(malformed.isError).toBe(true);
@@ -140,7 +142,7 @@ it.effect("registers annotated tools and preserves authenticated request context
142 const snapshot = yield* server
143 .callTool({ name: "preview_snapshot", arguments: {} })
144 .pipe(
143 - Effect.provideService(McpInvocationContext, invocation),
145 + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
146 Effect.provideService(McpSchema.McpServerClient, client),
147 );
148 expect(snapshot.isError).toBe(false);
@@ -152,7 +154,7 @@ it.effect("registers annotated tools and preserves authenticated request context
154 const press = yield* server
155 .callTool({ name: "preview_press", arguments: { key: "Enter" } })
156 .pipe(
155 - Effect.provideService(McpInvocationContext, invocation),
157 + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
158 Effect.provideService(McpSchema.McpServerClient, client),
159 );
160 expect(press.isError).toBe(false);
apps/server/src/mcp/McpHttpServer.ts renamed
+25 -24
@@ -8,11 +8,12 @@ import * as Stream from "effect/Stream";
8 import { McpSchema, McpServer, Tool } from "effect/unstable/ai";
9 import { HttpRouter, HttpServerRequest, HttpServerResponse } from "effect/unstable/http";
10
11 -import packageJson from "../../../package.json" with { type: "json" };
12 -import { McpInvocationContext } from "../Services/McpInvocationContext.ts";
13 -import { McpSessionRegistry } from "../Services/McpSessionRegistry.ts";
14 -import { PreviewToolkitHandlersLive } from "../toolkits/preview/handlers.ts";
15 -import { PreviewToolkit } from "../toolkits/preview/tools.ts";
11 +import packageJson from "../../package.json" with { type: "json" };
12 +import * as McpInvocationContext from "./McpInvocationContext.ts";
13 +import * as McpSessionRegistry from "./McpSessionRegistry.ts";
14 +import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
15 +import { PreviewToolkitHandlersLive } from "./toolkits/preview/handlers.ts";
16 +import { PreviewToolkit } from "./toolkits/preview/tools.ts";
17
18 const unauthorized = HttpServerResponse.jsonUnsafe(
19 {
@@ -41,25 +42,24 @@ export const normalizeMcpHttpResponse = (
42 };
43
44 const McpAuthMiddlewareLive = HttpRouter.middleware<{
44 - provides: McpInvocationContext;
45 + provides: McpInvocationContext.McpInvocationContext;
46 }>()(
47 Effect.gen(function* () {
47 - const registry = yield* McpSessionRegistry;
48 - return (httpEffect) =>
49 - Effect.gen(function* () {
50 - const request = yield* HttpServerRequest.HttpServerRequest;
51 - const authorization = request.headers.authorization;
52 - const token =
53 - authorization?.startsWith("Bearer ") === true
54 - ? authorization.slice("Bearer ".length).trim()
55 - : "";
56 - const invocation = yield* registry.resolve(token);
57 - if (!invocation) return unauthorized;
58 - return yield* httpEffect.pipe(
59 - Effect.provideService(McpInvocationContext, invocation),
60 - Effect.map(normalizeMcpHttpResponse),
61 - );
62 - });
48 + const registry = yield* McpSessionRegistry.McpSessionRegistry;
49 + return Effect.fn("McpHttpServer.authenticateRequest")(function* (httpEffect) {
50 + const request = yield* HttpServerRequest.HttpServerRequest;
51 + const authorization = request.headers.authorization;
52 + const token =
53 + authorization?.startsWith("Bearer ") === true
54 + ? authorization.slice("Bearer ".length).trim()
55 + : "";
56 + const invocation = yield* registry.resolve(token);
57 + if (!invocation) return unauthorized;
58 + return yield* httpEffect.pipe(
59 + Effect.provideService(McpInvocationContext.McpInvocationContext, invocation),
60 + Effect.map(normalizeMcpHttpResponse),
61 + );
62 + });
63 }),
64 ).layer;
65
@@ -79,7 +79,7 @@ export const PreviewToolkitRegistrationLive = Layer.effectDiscard(
79 ) => Effect.Effect<
80 Stream.Stream<{ readonly encodedResult: unknown }, Error>,
81 Error,
82 - McpInvocationContext
82 + McpInvocationContext.McpInvocationContext
83 >;
84 for (const tool of Object.values(built.tools)) {
85 yield* server.addTool({
@@ -160,6 +160,7 @@ export const PreviewToolkitRegistrationLive = Layer.effectDiscard(
160 }),
161 ).pipe(Layer.provide(PreviewToolkitHandlersLive));
162
163 -export const McpHttpServerLive = Layer.mergeAll(PreviewToolkitRegistrationLive).pipe(
163 +export const layer = Layer.mergeAll(PreviewToolkitRegistrationLive).pipe(
164 Layer.provideMerge(McpTransportLive),
165 + Layer.provide(PreviewAutomationBroker.layer),
166 );
apps/server/src/mcp/McpInvocationContext.ts renamed
+1 -1
@@ -18,7 +18,7 @@ export interface McpInvocationScope {
18 export class McpInvocationContext extends Context.Service<
19 McpInvocationContext,
20 McpInvocationScope
21 ->()("t3/mcp/Services/McpInvocationContext") {}
21 +>()("t3/mcp/McpInvocationContext") {}
22
23 export const requireMcpCapability = Effect.fn("mcp.requireCapability")(function* (
24 capability: McpCapability,
apps/server/src/mcp/McpProviderSession.ts renamed
-6
@@ -1,5 +1,4 @@
1 import type { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2 -import * as Context from "effect/Context";
2
3 export interface McpProviderSessionConfig {
4 readonly environmentId: EnvironmentId;
@@ -10,11 +9,6 @@ export interface McpProviderSessionConfig {
9 readonly authorizationHeader: string;
10 }
11
13 -export class McpProviderSession extends Context.Service<
14 - McpProviderSession,
15 - McpProviderSessionConfig
16 ->()("t3/mcp/Services/McpProviderSession") {}
17 -
12 const sessionsByThread = new Map<ThreadId, McpProviderSessionConfig>();
13
14 export function setMcpProviderSession(config: McpProviderSessionConfig): void {
apps/server/src/mcp/McpSessionRegistry.test.ts renamed
+3 -3
@@ -4,8 +4,8 @@ import { EnvironmentId, ProviderInstanceId, ThreadId } from "@t3tools/contracts"
4 import * as Effect from "effect/Effect";
5 import { HttpServer } from "effect/unstable/http";
6
7 -import { ServerEnvironment } from "../../environment/Services/ServerEnvironment.ts";
8 -import { makeMcpSessionRegistry } from "./McpSessionRegistry.ts";
7 +import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts";
8 +import * as McpSessionRegistry from "./McpSessionRegistry.ts";
9
10 const environmentId = EnvironmentId.make("environment-1");
11 const fakeHttpServer = HttpServer.HttpServer.of({
@@ -18,7 +18,7 @@ const fakeEnvironment = ServerEnvironment.of({
18 });
19
20 const makeRegistry = (now: () => number) =>
21 - makeMcpSessionRegistry({
21 + McpSessionRegistry.makeForTest({
22 now,
23 idleTimeoutMs: 100,
24 maximumLifetimeMs: 1_000,
apps/server/src/mcp/McpSessionRegistry.ts new
+202
@@ -0,0 +1,202 @@
1 +import { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2 +import * as Context from "effect/Context";
3 +import * as Crypto from "effect/Crypto";
4 +import * as Effect from "effect/Effect";
5 +import * as Layer from "effect/Layer";
6 +import * as Ref from "effect/Ref";
7 +import { HttpServer } from "effect/unstable/http";
8 +
9 +import { ServerEnvironment } from "../environment/Services/ServerEnvironment.ts";
10 +import * as McpInvocationContext from "./McpInvocationContext.ts";
11 +import * as McpProviderSession from "./McpProviderSession.ts";
12 +
13 +export interface McpCredentialRequest {
14 + readonly threadId: ThreadId;
15 + readonly providerInstanceId: ProviderInstanceId;
16 +}
17 +
18 +export interface McpIssuedCredential {
19 + readonly config: McpProviderSession.McpProviderSessionConfig;
20 + readonly expiresAt: number;
21 +}
22 +
23 +export interface McpSessionRegistryShape {
24 + readonly issue: (request: McpCredentialRequest) => Effect.Effect<McpIssuedCredential>;
25 + readonly resolve: (
26 + rawToken: string,
27 + ) => Effect.Effect<McpInvocationContext.McpInvocationScope | undefined>;
28 + readonly revokeProviderSession: (providerSessionId: string) => Effect.Effect<void>;
29 + readonly revokeThread: (threadId: ThreadId) => Effect.Effect<void>;
30 + readonly revokeAll: Effect.Effect<void>;
31 +}
32 +
33 +export class McpSessionRegistry extends Context.Service<
34 + McpSessionRegistry,
35 + McpSessionRegistryShape
36 +>()("t3/mcp/McpSessionRegistry") {}
37 +
38 +interface CredentialRecord {
39 + readonly tokenHash: string;
40 + readonly scope: McpInvocationContext.McpInvocationScope;
41 + readonly lastUsedAt: number;
42 +}
43 +
44 +interface RegistryState {
45 + readonly records: ReadonlyMap<string, CredentialRecord>;
46 +}
47 +
48 +export interface McpSessionRegistryOptions {
49 + readonly idleTimeoutMs?: number;
50 + readonly maximumLifetimeMs?: number;
51 + readonly now?: () => number;
52 +}
53 +
54 +const DEFAULT_IDLE_TIMEOUT_MS = 30 * 60 * 1_000;
55 +const DEFAULT_MAXIMUM_LIFETIME_MS = 8 * 60 * 60 * 1_000;
56 +
57 +const bytesToHex = (bytes: Uint8Array): string =>
58 + Array.from(bytes, (byte) => byte.toString(16).padStart(2, "0")).join("");
59 +
60 +const tokenFromBytes = (bytes: Uint8Array): string => Buffer.from(bytes).toString("base64url");
61 +
62 +const make = Effect.fn("McpSessionRegistry.make")(function* (
63 + options: McpSessionRegistryOptions = {},
64 +) {
65 + const crypto = yield* Crypto.Crypto;
66 + const environment = yield* ServerEnvironment;
67 + const environmentId = yield* environment.getEnvironmentId;
68 + const httpServer = yield* HttpServer.HttpServer;
69 + const state = yield* Ref.make<RegistryState>({ records: new Map() });
70 + const now = options.now ?? Date.now;
71 + const idleTimeoutMs = options.idleTimeoutMs ?? DEFAULT_IDLE_TIMEOUT_MS;
72 + const maximumLifetimeMs = options.maximumLifetimeMs ?? DEFAULT_MAXIMUM_LIFETIME_MS;
73 + const endpoint =
74 + httpServer.address._tag === "TcpAddress"
75 + ? `http://127.0.0.1:${httpServer.address.port}/mcp`
76 + : "http://127.0.0.1/mcp";
77 +
78 + const hashToken = (token: string) =>
79 + crypto
80 + .digest("SHA-256", new TextEncoder().encode(token))
81 + .pipe(Effect.map(bytesToHex), Effect.orDie);
82 +
83 + const pruneExpired = (records: ReadonlyMap<string, CredentialRecord>, timestamp: number) => {
84 + let changed = false;
85 + const next = new Map<string, CredentialRecord>();
86 + for (const [hash, record] of records) {
87 + if (timestamp <= record.scope.expiresAt && timestamp - record.lastUsedAt <= idleTimeoutMs) {
88 + next.set(hash, record);
89 + } else {
90 + changed = true;
91 + }
92 + }
93 + return changed ? next : records;
94 + };
95 +
96 + const issue: McpSessionRegistryShape["issue"] = Effect.fn("McpSessionRegistry.issue")(
97 + function* (request) {
98 + const issuedAt = now();
99 + const providerSessionId = yield* crypto.randomUUIDv4.pipe(Effect.orDie);
100 + const rawToken = yield* crypto.randomBytes(32).pipe(Effect.map(tokenFromBytes), Effect.orDie);
101 + const tokenHash = yield* hashToken(rawToken);
102 + const expiresAt = issuedAt + maximumLifetimeMs;
103 + const scope: McpInvocationContext.McpInvocationScope = {
104 + environmentId,
105 + threadId: ThreadId.make(request.threadId),
106 + providerSessionId,
107 + providerInstanceId: ProviderInstanceId.make(request.providerInstanceId),
108 + capabilities: new Set(["preview"]),
109 + issuedAt,
110 + expiresAt,
111 + };
112 + yield* Ref.update(state, ({ records }) => {
113 + const next = new Map(pruneExpired(records, issuedAt));
114 + next.set(tokenHash, { tokenHash, scope, lastUsedAt: issuedAt });
115 + return { records: next };
116 + });
117 + return {
118 + config: {
119 + environmentId,
120 + threadId: scope.threadId,
121 + providerSessionId,
122 + providerInstanceId: scope.providerInstanceId,
123 + endpoint,
124 + authorizationHeader: `Bearer ${rawToken}`,
125 + },
126 + expiresAt,
127 + };
128 + },
129 + );
130 +
131 + const resolve: McpSessionRegistryShape["resolve"] = Effect.fn("McpSessionRegistry.resolve")(
132 + function* (rawToken) {
133 + if (rawToken.length === 0) return undefined;
134 + const tokenHash = yield* hashToken(rawToken);
135 + const timestamp = now();
136 + let resolved: McpInvocationContext.McpInvocationScope | undefined;
137 + yield* Ref.update(state, ({ records }) => {
138 + const current = pruneExpired(records, timestamp);
139 + const record = current.get(tokenHash);
140 + if (!record) return { records: current };
141 + resolved = record.scope;
142 + const next = new Map(current);
143 + next.set(tokenHash, { ...record, lastUsedAt: timestamp });
144 + return { records: next };
145 + });
146 + return resolved;
147 + },
148 + );
149 +
150 + const revokeWhere = (predicate: (record: CredentialRecord) => boolean) =>
151 + Ref.update(state, ({ records }) => ({
152 + records: new Map(Array.from(records).filter(([, record]) => !predicate(record))),
153 + }));
154 +
155 + return McpSessionRegistry.of({
156 + issue,
157 + resolve,
158 + revokeProviderSession: Effect.fn("McpSessionRegistry.revokeProviderSession")(
159 + function* (providerSessionId) {
160 + yield* revokeWhere((record) => record.scope.providerSessionId === providerSessionId);
161 + },
162 + ),
163 + revokeThread: Effect.fn("McpSessionRegistry.revokeThread")(function* (threadId) {
164 + yield* revokeWhere((record) => record.scope.threadId === threadId);
165 + }),
166 + revokeAll: Ref.set(state, { records: new Map() }),
167 + });
168 +});
169 +
170 +let activeMcpSessionRegistry: McpSessionRegistryShape | undefined;
171 +
172 +export const layer: Layer.Layer<
173 + McpSessionRegistry,
174 + never,
175 + Crypto.Crypto | ServerEnvironment | HttpServer.HttpServer
176 +> = Layer.effect(
177 + McpSessionRegistry,
178 + make().pipe(
179 + Effect.tap((registry) =>
180 + Effect.sync(() => {
181 + activeMcpSessionRegistry = registry;
182 + }),
183 + ),
184 + ),
185 +);
186 +
187 +export const makeForTest = make;
188 +
189 +export const issueActiveMcpCredential = (
190 + request: McpCredentialRequest,
191 +): Effect.Effect<McpIssuedCredential | undefined> =>
192 + activeMcpSessionRegistry
193 + ? activeMcpSessionRegistry
194 + .revokeThread(request.threadId)
195 + .pipe(Effect.andThen(activeMcpSessionRegistry.issue(request)))
196 + : Effect.sync((): McpIssuedCredential | undefined => undefined);
197 +
198 +export const revokeActiveMcpThread = (threadId: ThreadId): Effect.Effect<void> =>
199 + activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeThread(threadId) : Effect.void;
200 +
201 +export const revokeAllActiveMcpCredentials = (): Effect.Effect<void> =>
202 + activeMcpSessionRegistry ? activeMcpSessionRegistry.revokeAll : Effect.void;
apps/server/src/mcp/PreviewAutomationBroker.test.ts renamed
+4 -4
@@ -8,7 +8,7 @@ import {
8 import * as Effect from "effect/Effect";
9 import * as Stream from "effect/Stream";
10
11 -import { makePreviewAutomationBroker } from "./PreviewAutomationBroker.ts";
11 +import * as PreviewAutomationBroker from "./PreviewAutomationBroker.ts";
12
13 const scope = {
14 environmentId: EnvironmentId.make("environment-1"),
@@ -23,7 +23,7 @@ const scope = {
23 it.effect("routes a request to the focused owner and correlates its response", () =>
24 Effect.scoped(
25 Effect.gen(function* () {
26 - const broker = makePreviewAutomationBroker();
26 + const broker = yield* PreviewAutomationBroker.makeForTest;
27 const requests = yield* broker.connect("client-1");
28 yield* Stream.runForEach(requests, (request) =>
29 broker.respond({
@@ -56,7 +56,7 @@ it.effect("routes a request to the focused owner and correlates its response", (
56
57 it.effect("rejects calls when no focused owner exists", () =>
58 Effect.gen(function* () {
59 - const broker = makePreviewAutomationBroker();
59 + const broker = yield* PreviewAutomationBroker.makeForTest;
60 const error = yield* broker
61 .invoke<void>({ scope, operation: "status", input: {} })
62 .pipe(Effect.flip);
@@ -67,7 +67,7 @@ it.effect("rejects calls when no focused owner exists", () =>
67 it.effect("routes interactive commands to a hidden durable browser host", () =>
68 Effect.scoped(
69 Effect.gen(function* () {
70 - const broker = makePreviewAutomationBroker();
70 + const broker = yield* PreviewAutomationBroker.makeForTest;
71 const requests = yield* broker.connect("client-hidden");
72 yield* Stream.runForEach(requests, (request) =>
73 broker.respond({ requestId: request.requestId, ok: true }),
apps/server/src/mcp/PreviewAutomationBroker.ts new
+306
@@ -0,0 +1,306 @@
1 +import {
2 + PreviewAutomationControlInterruptedError,
3 + PreviewAutomationExecutionError,
4 + PreviewAutomationInvalidSelectorError,
5 + PreviewAutomationNoFocusedOwnerError,
6 + PreviewAutomationResultTooLargeError,
7 + PreviewAutomationTabNotFoundError,
8 + PreviewAutomationTimeoutError,
9 + PreviewAutomationUnavailableError,
10 + PreviewAutomationUnsupportedClientError,
11 + type PreviewAutomationError,
12 + type PreviewAutomationOperation,
13 + type PreviewAutomationOwner,
14 + type PreviewAutomationRequest,
15 + type PreviewAutomationResponse,
16 + type PreviewTabId,
17 +} from "@t3tools/contracts";
18 +import * as Context from "effect/Context";
19 +import * as Deferred from "effect/Deferred";
20 +import * as Effect from "effect/Effect";
21 +import * as Layer from "effect/Layer";
22 +import * as Option from "effect/Option";
23 +import * as Queue from "effect/Queue";
24 +import * as Ref from "effect/Ref";
25 +import * as Stream from "effect/Stream";
26 +
27 +import * as McpInvocationContext from "./McpInvocationContext.ts";
28 +
29 +export interface PreviewAutomationInvokeInput {
30 + readonly scope: McpInvocationContext.McpInvocationScope;
31 + readonly operation: PreviewAutomationOperation;
32 + readonly input: unknown;
33 + readonly tabId?: PreviewTabId;
34 + readonly timeoutMs?: number;
35 +}
36 +
37 +export interface PreviewAutomationBrokerShape {
38 + readonly connect: (clientId: string) => Effect.Effect<Stream.Stream<PreviewAutomationRequest>>;
39 + readonly reportOwner: (
40 + owner: PreviewAutomationOwner,
41 + ) => Effect.Effect<void, PreviewAutomationError>;
42 + readonly clearOwner: (clientId: string) => Effect.Effect<void>;
43 + readonly respond: (
44 + response: PreviewAutomationResponse,
45 + ) => Effect.Effect<void, PreviewAutomationError>;
46 + readonly invoke: <A = unknown>(
47 + request: PreviewAutomationInvokeInput,
48 + ) => Effect.Effect<A, PreviewAutomationError>;
49 +}
50 +
51 +export class PreviewAutomationBroker extends Context.Service<
52 + PreviewAutomationBroker,
53 + PreviewAutomationBrokerShape
54 +>()("t3/mcp/PreviewAutomationBroker") {}
55 +
56 +interface ClientConnection {
57 + readonly clientId: string;
58 + readonly queue: Queue.Queue<
59 + Parameters<PreviewAutomationBrokerShape["respond"]>[0] extends never
60 + ? never
61 + : import("@t3tools/contracts").PreviewAutomationRequest
62 + >;
63 +}
64 +
65 +interface PendingRequest {
66 + readonly clientId: string;
67 + readonly deferred: Deferred.Deferred<unknown, PreviewAutomationError>;
68 +}
69 +
70 +interface BrokerState {
71 + readonly clients: ReadonlyMap<string, ClientConnection>;
72 + readonly owners: ReadonlyMap<string, PreviewAutomationOwner>;
73 + readonly pending: ReadonlyMap<string, PendingRequest>;
74 +}
75 +
76 +const makeResponseError = (
77 + error: NonNullable<PreviewAutomationResponse["error"]>,
78 +): PreviewAutomationError => {
79 + switch (error._tag) {
80 + case "PreviewAutomationNoFocusedOwnerError":
81 + return new PreviewAutomationNoFocusedOwnerError({ message: error.message });
82 + case "PreviewAutomationUnsupportedClientError":
83 + return new PreviewAutomationUnsupportedClientError({ message: error.message });
84 + case "PreviewAutomationTabNotFoundError":
85 + return new PreviewAutomationTabNotFoundError({ message: error.message });
86 + case "PreviewAutomationTimeoutError":
87 + return new PreviewAutomationTimeoutError({ message: error.message });
88 + case "PreviewAutomationControlInterruptedError":
89 + return new PreviewAutomationControlInterruptedError({ message: error.message });
90 + case "PreviewAutomationInvalidSelectorError": {
91 + const detail =
92 + typeof error.detail === "object" && error.detail !== null ? error.detail : undefined;
93 + return new PreviewAutomationInvalidSelectorError({
94 + message: error.message,
95 + selector:
96 + detail && "selector" in detail && typeof detail.selector === "string"
97 + ? detail.selector
98 + : "",
99 + });
100 + }
101 + case "PreviewAutomationResultTooLargeError": {
102 + const detail =
103 + typeof error.detail === "object" && error.detail !== null ? error.detail : undefined;
104 + return new PreviewAutomationResultTooLargeError({
105 + message: error.message,
106 + maximumBytes:
107 + detail && "maximumBytes" in detail && typeof detail.maximumBytes === "number"
108 + ? detail.maximumBytes
109 + : 64_000,
110 + });
111 + }
112 + case "PreviewAutomationUnavailableError":
113 + return new PreviewAutomationUnavailableError({ message: error.message });
114 + default:
115 + return new PreviewAutomationExecutionError({
116 + message: error.message,
117 + detail: error.detail,
118 + });
119 + }
120 +};
121 +
122 +const makeService = (): PreviewAutomationBrokerShape => {
123 + const state = Effect.runSync(
124 + Ref.make<BrokerState>({
125 + clients: new Map(),
126 + owners: new Map(),
127 + pending: new Map(),
128 + }),
129 + );
130 + let requestSequence = 0;
131 +
132 + const disconnect = Effect.fn("PreviewAutomationBroker.disconnect")(function* (
133 + clientId: string,
134 + queue: ClientConnection["queue"],
135 + ) {
136 + const toFail: PendingRequest[] = [];
137 + yield* Ref.update(state, (current) => {
138 + if (current.clients.get(clientId)?.queue !== queue) return current;
139 + const clients = new Map(current.clients);
140 + const owners = new Map(current.owners);
141 + const pending = new Map(current.pending);
142 + clients.delete(clientId);
143 + owners.delete(clientId);
144 + for (const [requestId, entry] of pending) {
145 + if (entry.clientId === clientId) {
146 + pending.delete(requestId);
147 + toFail.push(entry);
148 + }
149 + }
150 + return { clients, owners, pending };
151 + });
152 + yield* Effect.forEach(
153 + toFail,
154 + ({ deferred }) =>
155 + Deferred.fail(
156 + deferred,
157 + new PreviewAutomationUnavailableError({
158 + message: "The preview automation client disconnected.",
159 + }),
160 + ),
161 + { discard: true },
162 + );
163 + yield* Queue.shutdown(queue);
164 + });
165 +
166 + const connect: PreviewAutomationBrokerShape["connect"] = Effect.fn(
167 + "PreviewAutomationBroker.connect",
168 + )(function* (clientId) {
169 + const queue = yield* Queue.unbounded<import("@t3tools/contracts").PreviewAutomationRequest>();
170 + let previous: ClientConnection | undefined;
171 + yield* Ref.update(state, (current) => {
172 + previous = current.clients.get(clientId);
173 + const clients = new Map(current.clients);
174 + clients.set(clientId, { clientId, queue });
175 + return { ...current, clients };
176 + });
177 + if (previous) yield* disconnect(clientId, previous.queue);
178 + return Stream.fromQueue(queue).pipe(Stream.ensuring(disconnect(clientId, queue)));
179 + });
180 +
181 + const reportOwner: PreviewAutomationBrokerShape["reportOwner"] = Effect.fn(
182 + "PreviewAutomationBroker.reportOwner",
183 + )(function* (owner) {
184 + yield* Ref.update(state, (current) => {
185 + const owners = new Map(current.owners);
186 + owners.set(owner.clientId, owner);
187 + return { ...current, owners };
188 + });
189 + });
190 +
191 + const clearOwner: PreviewAutomationBrokerShape["clearOwner"] = Effect.fn(
192 + "PreviewAutomationBroker.clearOwner",
193 + )(function* (clientId) {
194 + yield* Ref.update(state, (current) => {
195 + const owners = new Map(current.owners);
196 + owners.delete(clientId);
197 + return { ...current, owners };
198 + });
199 + });
200 +
201 + const respond: PreviewAutomationBrokerShape["respond"] = Effect.fn(
202 + "PreviewAutomationBroker.respond",
203 + )(function* (response) {
204 + let pending: PendingRequest | undefined;
205 + yield* Ref.update(state, (current) => {
206 + pending = current.pending.get(response.requestId);
207 + if (!pending) return current;
208 + const next = new Map(current.pending);
209 + next.delete(response.requestId);
210 + return { ...current, pending: next };
211 + });
212 + if (!pending) return;
213 + if (response.ok) {
214 + yield* Deferred.succeed(pending.deferred, response.result);
215 + } else {
216 + yield* Deferred.fail(
217 + pending.deferred,
218 + response.error
219 + ? makeResponseError(response.error)
220 + : new PreviewAutomationExecutionError({
221 + message: "Preview automation failed without an error payload.",
222 + }),
223 + );
224 + }
225 + });
226 +
227 + const invoke = Effect.fn("PreviewAutomationBroker.invoke")(function* <A = unknown>(
228 + input: Parameters<PreviewAutomationBrokerShape["invoke"]>[0],
229 + ): Effect.fn.Return<A, PreviewAutomationError> {
230 + const current = yield* Ref.get(state);
231 + const candidates = Array.from(current.owners.values())
232 + .filter(
233 + (owner) =>
234 + owner.environmentId === input.scope.environmentId &&
235 + owner.threadId === input.scope.threadId &&
236 + owner.supportsAutomation,
237 + )
238 + .sort((left, right) => right.focusedAt.localeCompare(left.focusedAt));
239 + const owner = candidates[0];
240 + if (!owner) {
241 + return yield* new PreviewAutomationNoFocusedOwnerError({
242 + message: "No desktop browser host is available for this thread.",
243 + });
244 + }
245 + const connection = current.clients.get(owner.clientId);
246 + if (!connection) {
247 + return yield* new PreviewAutomationUnavailableError({
248 + message: "The browser host is not connected.",
249 + });
250 + }
251 + if (
252 + input.operation !== "open" &&
253 + input.operation !== "status" &&
254 + !owner.tabId &&
255 + !input.tabId
256 + ) {
257 + return yield* new PreviewAutomationTabNotFoundError({
258 + message: "The browser host does not have an active tab.",
259 + });
260 + }
261 + const requestId = `preview-${requestSequence++}`;
262 + const timeoutMs = input.timeoutMs ?? 15_000;
263 + const deferred = yield* Deferred.make<unknown, PreviewAutomationError>();
264 + yield* Ref.update(state, (next) => {
265 + const pending = new Map(next.pending);
266 + pending.set(requestId, { clientId: owner.clientId, deferred });
267 + return { ...next, pending };
268 + });
269 + const offered = yield* Queue.offer(connection.queue, {
270 + requestId,
271 + threadId: input.scope.threadId,
272 + tabId: input.tabId ?? owner.tabId ?? undefined,
273 + operation: input.operation,
274 + input: input.input,
275 + timeoutMs,
276 + });
277 + if (!offered) {
278 + return yield* new PreviewAutomationUnavailableError({
279 + message: "The preview automation client is no longer accepting requests.",
280 + });
281 + }
282 + const result = yield* Deferred.await(deferred).pipe(Effect.timeoutOption(timeoutMs));
283 + yield* Ref.update(state, (next) => {
284 + const pending = new Map(next.pending);
285 + pending.delete(requestId);
286 + return { ...next, pending };
287 + });
288 + return yield* Option.match(result, {
289 + onNone: () =>
290 + Effect.fail(
291 + new PreviewAutomationTimeoutError({
292 + message: `Preview automation timed out after ${timeoutMs}ms.`,
293 + }),
294 + ),
295 + onSome: (value) => Effect.succeed(value as A),
296 + });
297 + });
298 +
299 + return PreviewAutomationBroker.of({ connect, reportOwner, clearOwner, respond, invoke });
300 +};
301 +
302 +const service = makeService();
303 +const make = Effect.succeed(service);
304 +export const layer = Layer.effect(PreviewAutomationBroker, make);
305 +
306 +export const makeForTest = Effect.sync(makeService);
apps/server/src/mcp/Services/McpSessionRegistry.ts deleted
-29
@@ -1,29 +0,0 @@
1 -import type { ProviderInstanceId, ThreadId } from "@t3tools/contracts";
2 -import * as Context from "effect/Context";
3 -import type * as Effect from "effect/Effect";
4 -
5 -import type { McpInvocationScope } from "./McpInvocationContext.ts";
6 -import type { McpProviderSessionConfig } from "./McpProviderSession.ts";
7 -
8 -export interface McpCredentialRequest {
9 - readonly threadId: ThreadId;
10 - readonly providerInstanceId: ProviderInstanceId;
11 -}
12 -
13 -export interface McpIssuedCredential {
14 - readonly config: McpProviderSessionConfig;
15 - readonly expiresAt: number;
16 -}
17 -
18 -export interface McpSessionRegistryShape {
19 - readonly issue: (request: McpCredentialRequest) => Effect.Effect<McpIssuedCredential, never>;
20 - readonly resolve: (rawToken: string) => Effect.Effect<McpInvocationScope | undefined, never>;
21 - readonly revokeProviderSession: (providerSessionId: string) => Effect.Effect<void>;
22 - readonly revokeThread: (threadId: ThreadId) => Effect.Effect<void>;
23 - readonly revokeAll: Effect.Effect<void>;
24 -}
25 -
26 -export class McpSessionRegistry extends Context.Service<
27 - McpSessionRegistry,
28 - McpSessionRegistryShape
29 ->()("t3/mcp/Services/McpSessionRegistry") {}
apps/server/src/mcp/Services/PreviewAutomationBroker.ts deleted
-40
@@ -1,40 +0,0 @@
1 -import type {
2 - PreviewAutomationError,
3 - PreviewAutomationOperation,
4 - PreviewAutomationOwner,
5 - PreviewAutomationRequest,
6 - PreviewAutomationResponse,
7 - PreviewTabId,
8 -} from "@t3tools/contracts";
9 -import * as Context from "effect/Context";
10 -import type * as Effect from "effect/Effect";
11 -import type * as Stream from "effect/Stream";
12 -
13 -import type { McpInvocationScope } from "./McpInvocationContext.ts";
14 -
15 -export interface PreviewAutomationInvokeInput {
16 - readonly scope: McpInvocationScope;
17 - readonly operation: PreviewAutomationOperation;
18 - readonly input: unknown;
19 - readonly tabId?: PreviewTabId;
20 - readonly timeoutMs?: number;
21 -}
22 -
23 -export interface PreviewAutomationBrokerShape {
24 - readonly connect: (clientId: string) => Effect.Effect<Stream.Stream<PreviewAutomationRequest>>;
25 - readonly reportOwner: (
26 - owner: PreviewAutomationOwner,
27 - ) => Effect.Effect<void, PreviewAutomationError>;
28 - readonly clearOwner: (clientId: string) => Effect.Effect<void>;
29 - readonly respond: (
30 - response: PreviewAutomationResponse,
31 - ) => Effect.Effect<void, PreviewAutomationError>;
32 - readonly invoke: <A = unknown>(
33 - request: PreviewAutomationInvokeInput,
34 - ) => Effect.Effect<A, PreviewAutomationError>;
35 -}
36 -
37 -export class PreviewAutomationBroker extends Context.Service<
38 - PreviewAutomationBroker,
39 - PreviewAutomationBrokerShape
40 ->()("t3/mcp/Services/PreviewAutomationBroker") {}
apps/server/src/mcp/toolkits/preview/handlers.ts
+14 -14
@@ -7,28 +7,28 @@ import type {
7 PreviewAutomationStatus,
8 } from "@t3tools/contracts";
9
10 -import { requireMcpCapability } from "../../Services/McpInvocationContext.ts";
11 -import { previewAutomationBroker } from "../../Layers/PreviewAutomationBroker.ts";
10 +import * as McpInvocationContext from "../../McpInvocationContext.ts";
11 +import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
12 import { PreviewToolkit } from "./tools.ts";
13
14 -const invoke = <A>(
14 +const invoke = Effect.fn("PreviewToolkit.invoke")(function* <A>(
15 operation: PreviewAutomationOperation,
16 input: unknown,
17 timeoutMs?: number,
18 -): Effect.Effect<
18 +): Effect.fn.Return<
19 A,
20 import("@t3tools/contracts").PreviewAutomationError,
21 - import("../../Services/McpInvocationContext.ts").McpInvocationContext
22 -> =>
23 - Effect.gen(function* () {
24 - const scope = yield* requireMcpCapability("preview");
25 - return yield* previewAutomationBroker.invoke<A>({
26 - scope,
27 - operation,
28 - input,
29 - ...(timeoutMs === undefined ? {} : { timeoutMs }),
30 - });
21 + McpInvocationContext.McpInvocationContext | PreviewAutomationBroker.PreviewAutomationBroker
22 +> {
23 + const scope = yield* McpInvocationContext.requireMcpCapability("preview");
24 + const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
25 + return yield* broker.invoke<A>({
26 + scope,
27 + operation,
28 + input,
29 + ...(timeoutMs === undefined ? {} : { timeoutMs }),
30 });
31 +});
32
33 export const PreviewToolkitHandlersLive = PreviewToolkit.toLayer({
34 preview_status: () => invoke<PreviewAutomationStatus>("status", {}),
apps/server/src/mcp/toolkits/preview/tools.ts
+19 -13
@@ -16,7 +16,13 @@ import {
16 import * as Schema from "effect/Schema";
17 import { Tool, Toolkit } from "effect/unstable/ai";
18
19 -import { McpInvocationContext } from "../../Services/McpInvocationContext.ts";
19 +import * as McpInvocationContext from "../../McpInvocationContext.ts";
20 +import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
21 +
22 +const dependencies = [
23 + McpInvocationContext.McpInvocationContext,
24 + PreviewAutomationBroker.PreviewAutomationBroker,
25 +];
26
27 const browserTool = <T extends Tool.Any>(tool: T): T =>
28 tool.annotate(Tool.OpenWorld, true).annotate(Tool.Destructive, true) as T;
@@ -32,7 +38,7 @@ export const PreviewStatusTool = Tool.make("preview_status", {
38 "Report whether the scoped thread has an automation-capable desktop preview, including its active tab, URL, title, visibility, and loading state.",
39 success: PreviewAutomationStatus,
40 failure: PreviewAutomationError,
35 - dependencies: [McpInvocationContext],
41 + dependencies,
42 })
43 .annotate(Tool.Title, "Get preview status")
44 .annotate(Tool.Readonly, true)
@@ -46,7 +52,7 @@ export const PreviewOpenTool = browserTool(
52 parameters: PreviewAutomationOpenInput,
53 success: PreviewAutomationStatus,
54 failure: PreviewAutomationError,
49 - dependencies: [McpInvocationContext],
55 + dependencies,
56 })
57 .annotate(Tool.Title, "Open browser preview")
58 .annotate(Tool.Destructive, false),
@@ -59,7 +65,7 @@ export const PreviewNavigateTool = safeBrowserTool(
65 parameters: PreviewAutomationNavigateInput,
66 success: PreviewAutomationStatus,
67 failure: PreviewAutomationError,
62 - dependencies: [McpInvocationContext],
68 + dependencies,
69 }).annotate(Tool.Title, "Navigate browser preview"),
70 );
71
@@ -69,7 +75,7 @@ export const PreviewSnapshotTool = readonlyBrowserTool(
75 "Inspect the current page before interacting. Returns URL/title/loading state, visible text, semantic interactive elements with reusable selectors and coordinates, accessibility data, recent console/network failures, action history, and a PNG screenshot.",
76 success: PreviewAutomationSnapshot,
77 failure: PreviewAutomationError,
72 - dependencies: [McpInvocationContext],
78 + dependencies,
79 }).annotate(Tool.Title, "Inspect browser page"),
80 );
81
@@ -79,7 +85,7 @@ export const PreviewClickTool = browserTool(
85 "Click exactly one page target. Prefer locator with a Playwright selector such as role=button[name='Send']; selector accepts legacy CSS; x and y are viewport CSS pixels and must be supplied together. Call preview_snapshot first when the target is unknown.",
86 parameters: PreviewAutomationClickInput,
87 failure: PreviewAutomationError,
82 - dependencies: [McpInvocationContext],
88 + dependencies,
89 }).annotate(Tool.Title, "Click preview page"),
90 );
91
@@ -89,7 +95,7 @@ export const PreviewTypeTool = browserTool(
95 "Insert literal text into one input. Prefer locator with a Playwright role/text selector; selector accepts legacy CSS. If neither is supplied, types into the currently focused element. Set clear=true to replace existing text.",
96 parameters: PreviewAutomationTypeInput,
97 failure: PreviewAutomationError,
92 - dependencies: [McpInvocationContext],
98 + dependencies,
99 }).annotate(Tool.Title, "Type into preview page"),
100 );
101
@@ -99,7 +105,7 @@ export const PreviewPressTool = browserTool(
105 "Press one keyboard key in the active page, for example {key:'Enter'}, {key:'Escape'}, or {key:'a',modifiers:['Meta']}. This targets the page's current focus.",
106 parameters: PreviewAutomationPressInput,
107 failure: PreviewAutomationError,
102 - dependencies: [McpInvocationContext],
108 + dependencies,
109 }).annotate(Tool.Title, "Press key in preview page"),
110 );
111
@@ -109,7 +115,7 @@ export const PreviewScrollTool = safeBrowserTool(
115 "Scroll by CSS pixels. Positive deltaY scrolls down and positive deltaX scrolls right. Without locator/selector it scrolls the viewport; otherwise it scrolls that container. At least one delta is required.",
116 parameters: PreviewAutomationScrollInput,
117 failure: PreviewAutomationError,
112 - dependencies: [McpInvocationContext],
118 + dependencies,
119 }).annotate(Tool.Title, "Scroll preview page"),
120 );
121
@@ -120,7 +126,7 @@ export const PreviewEvaluateTool = browserTool(
126 parameters: PreviewAutomationEvaluateInput,
127 success: Schema.Unknown,
128 failure: PreviewAutomationError,
123 - dependencies: [McpInvocationContext],
129 + dependencies,
130 }).annotate(Tool.Title, "Evaluate JavaScript in preview"),
131 );
132
@@ -130,7 +136,7 @@ export const PreviewWaitForTool = readonlyBrowserTool(
136 "Wait until all supplied conditions match: a Playwright locator, legacy CSS selector, visible-text substring, and/or URL substring. Provide at least one condition. Defaults to 15 seconds, maximum 60 seconds.",
137 parameters: PreviewAutomationWaitForInput,
138 failure: PreviewAutomationError,
133 - dependencies: [McpInvocationContext],
139 + dependencies,
140 }).annotate(Tool.Title, "Wait for preview page condition"),
141 );
142
@@ -140,7 +146,7 @@ export const PreviewRecordingStartTool = safeBrowserTool(
146 "Start recording the active collaborative browser tab while keeping it interactive for both agent and human use.",
147 success: PreviewAutomationRecordingStatus,
148 failure: PreviewAutomationError,
143 - dependencies: [McpInvocationContext],
149 + dependencies,
150 }).annotate(Tool.Title, "Start browser recording"),
151 );
152
@@ -149,7 +155,7 @@ export const PreviewRecordingStopTool = safeBrowserTool(
155 description: "Stop the active browser recording and save it as a local evidence artifact.",
156 success: PreviewAutomationRecordingArtifact,
157 failure: PreviewAutomationError,
152 - dependencies: [McpInvocationContext],
158 + dependencies,
159 }).annotate(Tool.Title, "Stop browser recording"),
160 );
161
apps/server/src/preview/Manager.test.ts renamed
+17 -18
@@ -3,8 +3,7 @@ import { type PreviewEvent, ThreadId } from "@t3tools/contracts";
3 import { Effect, PubSub } from "effect";
4 import { expect } from "vite-plus/test";
5
6 -import { PreviewManager } from "../Services/Manager.ts";
7 -import { PreviewManagerLive } from "./Manager.ts";
6 +import * as PreviewManager from "./Manager.ts";
7
8 const DRAIN_LIMIT = 100;
9
@@ -27,7 +26,7 @@ const freshThreadId = () => ThreadId.make(`thread-${++nextThreadId}`);
26 * no event can land between subscribe and the consumer drain.
27 */
28 const collectEvents = Effect.gen(function* () {
30 - const manager = yield* PreviewManager;
29 + const manager = yield* PreviewManager.PreviewManager;
30 const subscription = yield* manager.subscribeEvents;
31 const collector: EventCollector = {
32 drain: PubSub.takeUpTo(subscription, DRAIN_LIMIT),
@@ -35,11 +34,11 @@ const collectEvents = Effect.gen(function* () {
34 return collector;
35 }).pipe(Effect.withSpan("preview.test.collectEvents"));
36
38 -it.layer(PreviewManagerLive)("PreviewManager", (it) => {
37 +it.layer(PreviewManager.layer)("PreviewManager", (it) => {
38 it.effect("opens a session and emits opened with normalized URL", () =>
39 Effect.gen(function* () {
40 const threadId = freshThreadId();
42 - const manager = yield* PreviewManager;
41 + const manager = yield* PreviewManager.PreviewManager;
42 const collector = yield* collectEvents;
43
44 const snapshot = yield* manager.open({ threadId, url: "localhost:5173" });
@@ -61,7 +60,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
60 it.effect("opens an Idle tab when no URL is supplied", () =>
61 Effect.gen(function* () {
62 const threadId = freshThreadId();
64 - const manager = yield* PreviewManager;
63 + const manager = yield* PreviewManager.PreviewManager;
64 const snapshot = yield* manager.open({ threadId });
65 expect(snapshot.navStatus._tag).toBe("Idle");
66 }),
@@ -70,7 +69,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
69 it.effect("treats bare hosts as https", () =>
70 Effect.gen(function* () {
71 const threadId = freshThreadId();
73 - const manager = yield* PreviewManager;
72 + const manager = yield* PreviewManager.PreviewManager;
73 const snapshot = yield* manager.open({ threadId, url: "example.com" });
74 if (snapshot.navStatus._tag === "Loading") {
75 expect(snapshot.navStatus.url).toBe("https://example.com/");
@@ -81,7 +80,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
80 it.effect("rejects empty URL with PreviewInvalidUrlError", () =>
81 Effect.gen(function* () {
82 const threadId = freshThreadId();
84 - const manager = yield* PreviewManager;
83 + const manager = yield* PreviewManager.PreviewManager;
84 const error = yield* Effect.flip(manager.open({ threadId, url: " " }));
85 expect(error._tag).toBe("PreviewInvalidUrlError");
86 }),
@@ -90,7 +89,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
89 it.effect("navigate updates snapshot and emits navigated", () =>
90 Effect.gen(function* () {
91 const threadId = freshThreadId();
93 - const manager = yield* PreviewManager;
92 + const manager = yield* PreviewManager.PreviewManager;
93 const collector = yield* collectEvents;
94
95 const opened = yield* manager.open({ threadId, url: "http://localhost:5173" });
@@ -114,7 +113,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
113 it.effect("navigate fails for unknown tab", () =>
114 Effect.gen(function* () {
115 const threadId = freshThreadId();
117 - const manager = yield* PreviewManager;
116 + const manager = yield* PreviewManager.PreviewManager;
117 const error = yield* Effect.flip(
118 manager.navigate({
119 threadId,
@@ -129,7 +128,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
128 it.effect("reportStatus emits failed for LoadFailed nav", () =>
129 Effect.gen(function* () {
130 const threadId = freshThreadId();
132 - const manager = yield* PreviewManager;
131 + const manager = yield* PreviewManager.PreviewManager;
132 const collector = yield* collectEvents;
133
134 const opened = yield* manager.open({ threadId, url: "http://localhost:5173" });
@@ -160,7 +159,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
159 it.effect("close removes the session and emits closed", () =>
160 Effect.gen(function* () {
161 const threadId = freshThreadId();
163 - const manager = yield* PreviewManager;
162 + const manager = yield* PreviewManager.PreviewManager;
163 const collector = yield* collectEvents;
164
165 yield* manager.open({ threadId, url: "http://localhost:5173" });
@@ -177,7 +176,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
176 it.effect("close is idempotent for unknown threads", () =>
177 Effect.gen(function* () {
178 const threadId = freshThreadId();
180 - const manager = yield* PreviewManager;
179 + const manager = yield* PreviewManager.PreviewManager;
180 yield* manager.close({ threadId });
181 const result = yield* manager.list({ threadId });
182 expect(result.sessions).toHaveLength(0);
@@ -187,7 +186,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
186 it.effect("list returns every snapshot for the thread sorted by updatedAt", () =>
187 Effect.gen(function* () {
188 const threadId = freshThreadId();
190 - const manager = yield* PreviewManager;
189 + const manager = yield* PreviewManager.PreviewManager;
190 const first = yield* manager.open({ threadId, url: "http://localhost:5173" });
191 const second = yield* manager.open({ threadId, url: "http://localhost:3000" });
192 const result = yield* manager.list({ threadId });
@@ -201,7 +200,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
200 it.effect("open creates an independent tab on every call", () =>
201 Effect.gen(function* () {
202 const threadId = freshThreadId();
204 - const manager = yield* PreviewManager;
203 + const manager = yield* PreviewManager.PreviewManager;
204 const collector = yield* collectEvents;
205
206 const a = yield* manager.open({ threadId, url: "http://localhost:5173" });
@@ -219,7 +218,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
218 it.effect("close with mismatching tabId is a no-op", () =>
219 Effect.gen(function* () {
220 const threadId = freshThreadId();
222 - const manager = yield* PreviewManager;
221 + const manager = yield* PreviewManager.PreviewManager;
222 yield* manager.open({ threadId, url: "http://localhost:5173" });
223 yield* manager.close({ threadId, tabId: "tab_missing" });
224
@@ -231,7 +230,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
230 it.effect("close with explicit tabId removes only that tab", () =>
231 Effect.gen(function* () {
232 const threadId = freshThreadId();
234 - const manager = yield* PreviewManager;
233 + const manager = yield* PreviewManager.PreviewManager;
234 const a = yield* manager.open({ threadId, url: "http://localhost:5173" });
235 const b = yield* manager.open({ threadId, url: "http://localhost:3000" });
236
@@ -245,7 +244,7 @@ it.layer(PreviewManagerLive)("PreviewManager", (it) => {
244 it.effect("multiple subscribers receive every event independently", () =>
245 Effect.gen(function* () {
246 const threadId = freshThreadId();
248 - const manager = yield* PreviewManager;
247 + const manager = yield* PreviewManager.PreviewManager;
248 const aSub = yield* manager.subscribeEvents;
249 const bSub = yield* manager.subscribeEvents;
250
apps/server/src/preview/Manager.ts renamed
+115 -74
@@ -10,9 +10,16 @@
10 * fail an in-progress `navigate()`).
11 */
12 import {
13 + type PreviewCloseInput,
14 type PreviewEvent,
15 + type PreviewError,
16 PreviewInvalidUrlError,
17 + type PreviewListInput,
18 type PreviewListResult,
19 + type PreviewNavigateInput,
20 + type PreviewOpenInput,
21 + type PreviewRefreshInput,
22 + type PreviewReportStatusInput,
23 PreviewSessionLookupError,
24 type PreviewSessionSnapshot,
25 } from "@t3tools/contracts";
@@ -21,9 +28,33 @@ import {
28 normalizePreviewUrl,
29 PreviewUrlNormalizationError,
30 } from "@t3tools/shared/preview";
24 -import { DateTime, Effect, Layer, PubSub, Stream, SynchronizedRef } from "effect";
31 +import {
32 + Context,
33 + DateTime,
34 + Effect,
35 + Layer,
36 + PubSub,
37 + type Scope,
38 + Stream,
39 + SynchronizedRef,
40 +} from "effect";
41 +
42 +export interface PreviewManagerShape {
43 + readonly open: (input: PreviewOpenInput) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
44 + readonly navigate: (
45 + input: PreviewNavigateInput,
46 + ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
47 + readonly reportStatus: (input: PreviewReportStatusInput) => Effect.Effect<void, PreviewError>;
48 + readonly refresh: (input: PreviewRefreshInput) => Effect.Effect<void, PreviewError>;
49 + readonly close: (input: PreviewCloseInput) => Effect.Effect<void, PreviewError>;
50 + readonly list: (input: PreviewListInput) => Effect.Effect<PreviewListResult>;
51 + readonly events: Stream.Stream<PreviewEvent>;
52 + readonly subscribeEvents: Effect.Effect<PubSub.Subscription<PreviewEvent>, never, Scope.Scope>;
53 +}
54
26 -import { PreviewManager, type PreviewManagerShape } from "../Services/Manager.ts";
55 +export class PreviewManager extends Context.Service<PreviewManager, PreviewManagerShape>()(
56 + "t3/preview/Manager/PreviewManager",
57 +) {}
58
59 interface PreviewSessionState {
60 readonly threadId: string;
@@ -96,7 +127,7 @@ const buildIdleSnapshot = (input: {
127 updatedAt: input.updatedAt,
128 });
129
99 -export const makePreviewManager = Effect.gen(function* () {
130 +const make = Effect.gen(function* () {
131 const stateRef = yield* SynchronizedRef.make<ManagerState>(initialState);
132 // Unbounded PubSub is fine here — events are tiny and we don't want to
133 // block publishers if a subscriber is slow. WS clients backpressure on
@@ -134,8 +165,8 @@ export const makePreviewManager = Effect.gen(function* () {
165 ] as readonly [ModifyResult, ManagerState]);
166 }
167 return mutator(session).pipe(
137 - Effect.flatMap(({ next, emit, result }) =>
138 - Effect.gen(function* () {
168 + Effect.flatMap(
169 + Effect.fn("PreviewManager.commitMutation")(function* ({ next, emit, result }) {
170 if (emit) yield* PubSub.publish(eventsPubSub, emit);
171 const sessions = new Map(state.sessions);
172 sessions.set(compositeKey(threadId, tabId), next);
@@ -153,43 +184,44 @@ export const makePreviewManager = Effect.gen(function* () {
184 );
185 };
186
156 - const open: PreviewManagerShape["open"] = (input) =>
157 - Effect.gen(function* () {
158 - const tabId = newPreviewTabId();
159 - const updatedAt = yield* currentIsoTimestamp;
160 - const snapshot = input.url
161 - ? buildLoadingSnapshot({
162 - threadId: input.threadId,
163 - tabId,
164 - url: yield* normalizeUrl(input.url),
165 - title: "",
166 - updatedAt,
167 - })
168 - : buildIdleSnapshot({ threadId: input.threadId, tabId, updatedAt });
169 - yield* SynchronizedRef.update(stateRef, (state) => {
170 - const sessions = new Map(state.sessions);
171 - sessions.set(compositeKey(input.threadId, tabId), {
187 + const open: PreviewManagerShape["open"] = Effect.fn("PreviewManager.open")(function* (input) {
188 + const tabId = newPreviewTabId();
189 + const updatedAt = yield* currentIsoTimestamp;
190 + const snapshot = input.url
191 + ? buildLoadingSnapshot({
192 threadId: input.threadId,
193 tabId,
174 - snapshot,
175 - });
176 - return { sessions };
177 - });
178 - yield* PubSub.publish(eventsPubSub, {
179 - type: "opened",
194 + url: yield* normalizeUrl(input.url),
195 + title: "",
196 + updatedAt,
197 + })
198 + : buildIdleSnapshot({ threadId: input.threadId, tabId, updatedAt });
199 + yield* SynchronizedRef.update(stateRef, (state) => {
200 + const sessions = new Map(state.sessions);
201 + sessions.set(compositeKey(input.threadId, tabId), {
202 threadId: input.threadId,
203 tabId,
182 - createdAt: snapshot.updatedAt,
204 snapshot,
205 });
185 - return snapshot;
206 + return { sessions };
207 });
208 + yield* PubSub.publish(eventsPubSub, {
209 + type: "opened",
210 + threadId: input.threadId,
211 + tabId,
212 + createdAt: snapshot.updatedAt,
213 + snapshot,
214 + });
215 + return snapshot;
216 + });
217
188 - const navigate: PreviewManagerShape["navigate"] = (input) =>
189 - Effect.gen(function* () {
218 + const navigate: PreviewManagerShape["navigate"] = Effect.fn("PreviewManager.navigate")(
219 + function* (input) {
220 const url = yield* normalizeUrl(input.url);
191 - return yield* mutateExistingSession(input.threadId, input.tabId, (session) =>
192 - Effect.gen(function* () {
221 + return yield* mutateExistingSession(
222 + input.threadId,
223 + input.tabId,
224 + Effect.fn("PreviewManager.navigateSession")(function* (session) {
225 const updatedAt = yield* currentIsoTimestamp;
226 const previousTitle =
227 session.snapshot.navStatus._tag === "Idle" ? "" : session.snapshot.navStatus.title;
@@ -215,11 +247,16 @@ export const makePreviewManager = Effect.gen(function* () {
247 };
248 }),
249 );
218 - });
250 + },
251 + );
252
220 - const reportStatus: PreviewManagerShape["reportStatus"] = (input) =>
221 - mutateExistingSession(input.threadId, input.tabId, (session) =>
222 - Effect.gen(function* () {
253 + const reportStatus: PreviewManagerShape["reportStatus"] = Effect.fn(
254 + "PreviewManager.reportStatus",
255 + )(function* (input) {
256 + yield* mutateExistingSession(
257 + input.threadId,
258 + input.tabId,
259 + Effect.fn("PreviewManager.reportSessionStatus")(function* (session) {
260 const updatedAt = yield* currentIsoTimestamp;
261 const snapshot: PreviewSessionSnapshot = {
262 threadId: session.threadId,
@@ -255,48 +292,51 @@ export const makePreviewManager = Effect.gen(function* () {
292 };
293 }),
294 );
295 + });
296
259 - const refresh: PreviewManagerShape["refresh"] = (input) =>
260 - // Verify the session exists; the desktop bridge handles the actual reload
261 - // and will report progress back via `reportStatus`. No event emitted.
262 - mutateExistingSession(input.threadId, input.tabId, (session) =>
263 - Effect.succeed({ next: session, emit: null, result: undefined as void }),
264 - );
297 + const refresh: PreviewManagerShape["refresh"] = Effect.fn("PreviewManager.refresh")(
298 + function* (input) {
299 + // Verify the session exists; the desktop bridge handles the actual reload
300 + // and will report progress back via `reportStatus`. No event emitted.
301 + yield* mutateExistingSession(input.threadId, input.tabId, (session) =>
302 + Effect.succeed({ next: session, emit: null, result: undefined as void }),
303 + );
304 + },
305 + );
306
266 - const close: PreviewManagerShape["close"] = (input) =>
267 - Effect.gen(function* () {
268 - const createdAt = yield* currentIsoTimestamp;
269 - const events = yield* SynchronizedRef.modify(stateRef, (state) => {
270 - const eventsToEmit: PreviewEvent[] = [];
271 - const sessions = new Map(state.sessions);
272 - const targets = input.tabId
273 - ? [state.sessions.get(compositeKey(input.threadId, input.tabId))].filter(
274 - (entry): entry is PreviewSessionState => entry !== undefined,
275 - )
276 - : sessionsForThread(state, input.threadId);
277 - for (const target of targets) {
278 - sessions.delete(compositeKey(target.threadId, target.tabId));
279 - eventsToEmit.push({
280 - type: "closed",
281 - threadId: target.threadId,
282 - tabId: target.tabId,
283 - createdAt,
284 - });
285 - }
286 - if (eventsToEmit.length === 0) {
287 - return [eventsToEmit, state] as const;
288 - }
289 - return [eventsToEmit, { sessions }] as const;
290 - });
291 - if (events.length > 0) {
292 - yield* Effect.forEach(events, (event) => PubSub.publish(eventsPubSub, event), {
293 - discard: true,
307 + const close: PreviewManagerShape["close"] = Effect.fn("PreviewManager.close")(function* (input) {
308 + const createdAt = yield* currentIsoTimestamp;
309 + const events = yield* SynchronizedRef.modify(stateRef, (state) => {
310 + const eventsToEmit: PreviewEvent[] = [];
311 + const sessions = new Map(state.sessions);
312 + const targets = input.tabId
313 + ? [state.sessions.get(compositeKey(input.threadId, input.tabId))].filter(
314 + (entry): entry is PreviewSessionState => entry !== undefined,
315 + )
316 + : sessionsForThread(state, input.threadId);
317 + for (const target of targets) {
318 + sessions.delete(compositeKey(target.threadId, target.tabId));
319 + eventsToEmit.push({
320 + type: "closed",
321 + threadId: target.threadId,
322 + tabId: target.tabId,
323 + createdAt,
324 });
325 }
326 + if (eventsToEmit.length === 0) {
327 + return [eventsToEmit, state] as const;
328 + }
329 + return [eventsToEmit, { sessions }] as const;
330 });
331 + if (events.length > 0) {
332 + yield* Effect.forEach(events, (event) => PubSub.publish(eventsPubSub, event), {
333 + discard: true,
334 + });
335 + }
336 + });
337
298 - const list: PreviewManagerShape["list"] = (input) =>
299 - SynchronizedRef.get(stateRef).pipe(
338 + const list: PreviewManagerShape["list"] = Effect.fn("PreviewManager.list")(function* (input) {
339 + return yield* SynchronizedRef.get(stateRef).pipe(
340 Effect.map(
341 (state): PreviewListResult => ({
342 sessions: sessionsForThread(state, input.threadId)
@@ -305,6 +345,7 @@ export const makePreviewManager = Effect.gen(function* () {
345 }),
346 ),
347 );
348 + });
349
350 return {
351 open,
@@ -318,4 +359,4 @@ export const makePreviewManager = Effect.gen(function* () {
359 } satisfies PreviewManagerShape;
360 });
361
321 -export const PreviewManagerLive = Layer.effect(PreviewManager, makePreviewManager);
362 +export const layer = Layer.effect(PreviewManager, make);
apps/server/src/preview/PortScanner.test.ts renamed
+7 -8
@@ -5,16 +5,15 @@ import { ThreadId } from "@t3tools/contracts";
5 import { Effect, Layer } from "effect";
6 import { afterEach, beforeEach, describe, expect, it } from "vite-plus/test";
7
8 -import { COMMON_DEV_PORTS, PortDiscovery } from "../Services/PortScanner.ts";
9 -import { ProcessRunner } from "../../processRunner.ts";
10 -import { __testing, PortDiscoveryLive } from "./PortScanner.ts";
8 +import { ProcessRunner } from "../processRunner.ts";
9 +import * as PortScanner from "./PortScanner.ts";
10
11 const { parseLsofOutput, parsePortFromLsofName, parseWindowsListenerOutput, serversEqual } =
13 - __testing;
12 + PortScanner.__testing;
13 const TestProcessRunner = Layer.succeed(ProcessRunner, {
14 run: () => Effect.die("ProcessRunner should not be used by Windows TCP probe tests"),
15 });
17 -const TestPortDiscoveryLive = PortDiscoveryLive.pipe(Layer.provide(TestProcessRunner));
16 +const TestPortDiscoveryLive = PortScanner.layer.pipe(Layer.provide(TestProcessRunner));
17
18 describe("parsePortFromLsofName", () => {
19 it("parses *:port", () => {
@@ -189,7 +188,7 @@ describe("PortDiscovery integration (TCP probe fallback)", () => {
188 beforeEach(async () => {
189 originalPlatform = process.platform;
190 Object.defineProperty(process, "platform", { value: "win32", configurable: true });
192 - for (const candidate of COMMON_DEV_PORTS) {
191 + for (const candidate of PortScanner.COMMON_DEV_PORTS) {
192 const candidateServer = net.createServer();
193 const listening = await new Promise<boolean>((resolve) => {
194 candidateServer.once("error", () => resolve(false));
@@ -215,7 +214,7 @@ describe("PortDiscovery integration (TCP probe fallback)", () => {
214
215 effectIt.effect("scan() returns a server we just opened on a curated dev port", () =>
216 Effect.gen(function* () {
218 - const scanner = yield* PortDiscovery;
217 + const scanner = yield* PortScanner.PortDiscovery;
218 const result = yield* scanner.scan();
219 const found = result.find((server) => server.port === port);
220 expect(found).toBeDefined();
@@ -226,7 +225,7 @@ describe("PortDiscovery integration (TCP probe fallback)", () => {
225 effectIt.effect("retain() drives an immediate broadcast to subscribers", () => {
226 const received: number[] = [];
227 return Effect.gen(function* () {
229 - const scanner = yield* PortDiscovery;
228 + const scanner = yield* PortScanner.PortDiscovery;
229 const unsubscribe = yield* scanner.subscribe((servers) =>
230 Effect.sync(() => {
231 for (const server of servers) received.push(server.port);
apps/server/src/preview/PortScanner.ts renamed
+95 -68
@@ -15,14 +15,34 @@ import * as net from "node:net";
15
16 import { ThreadId, type DiscoveredLocalServer } from "@t3tools/contracts";
17 import { LSOF_LOCAL_HOST_TOKENS } from "@t3tools/shared/preview";
18 -import { Cause, Duration, Effect, Layer, Ref, Schedule } from "effect";
18 +import { Cause, Context, Duration, Effect, Layer, Ref, Schedule } from "effect";
19
20 -import { ProcessRunner } from "../../processRunner.ts";
21 -import {
22 - COMMON_DEV_PORTS,
23 - PortDiscovery,
24 - type PortDiscoveryShape,
25 -} from "../Services/PortScanner.ts";
20 +import { ProcessRunner } from "../processRunner.ts";
21 +
22 +export interface PortDiscoveryShape {
23 + readonly scan: () => Effect.Effect<ReadonlyArray<DiscoveredLocalServer>>;
24 + readonly subscribe: (
25 + listener: (servers: ReadonlyArray<DiscoveredLocalServer>) => Effect.Effect<void>,
26 + ) => Effect.Effect<() => void>;
27 + readonly retain: () => Effect.Effect<() => void>;
28 + readonly registerTerminalProcesses: (input: {
29 + readonly threadId: string;
30 + readonly terminalId: string;
31 + readonly processIds: ReadonlyArray<number>;
32 + }) => Effect.Effect<void>;
33 + readonly unregisterTerminal: (input: {
34 + readonly threadId: string;
35 + readonly terminalId: string;
36 + }) => Effect.Effect<void>;
37 +}
38 +
39 +export class PortDiscovery extends Context.Service<PortDiscovery, PortDiscoveryShape>()(
40 + "t3/preview/PortScanner/PortDiscovery",
41 +) {}
42 +
43 +export const COMMON_DEV_PORTS: ReadonlyArray<number> = Object.freeze([
44 + 3000, 3001, 3333, 4173, 4200, 4321, 5000, 5173, 5174, 5175, 5500, 8000, 8080, 8081, 8888, 9000,
45 +]);
46
47 const POLL_INTERVAL = Duration.seconds(3);
48 const TCP_PROBE_TIMEOUT_MS = 200;
@@ -186,7 +206,7 @@ const serversEqual = (
206 return true;
207 };
208
189 -export const makePortDiscovery = Effect.gen(function* () {
209 +const make = Effect.gen(function* () {
210 const processRunner = yield* ProcessRunner;
211 const stateRef = yield* Ref.make<ScannerState>({
212 lastSnapshot: [],
@@ -204,47 +224,46 @@ export const makePortDiscovery = Effect.gen(function* () {
224 // be a synchronous side-effect-only function.
225 let retainCount = 0;
226
207 - const scanOnce = (): Effect.Effect<ReadonlyArray<DiscoveredLocalServer>> =>
208 - Effect.gen(function* () {
209 - const terminalByProcessId = new Map<number, TerminalProcessOwner>();
210 - for (const registration of terminalProcesses.values()) {
211 - for (const processId of registration.processIds) {
212 - terminalByProcessId.set(processId, registration.owner);
213 - }
214 - }
215 - if (process.platform === "win32") {
216 - const command =
217 - 'Get-NetTCPConnection -State Listen -ErrorAction Stop | ForEach-Object { $processName = (Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).ProcessName; Write-Output "$($_.LocalAddress)|$($_.LocalPort)|$($_.OwningProcess)|$processName" }';
218 - const listeners = yield* processRunner
219 - .run({
220 - command: "powershell.exe",
221 - args: ["-NoProfile", "-NonInteractive", "-Command", command],
222 - timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS),
223 - maxOutputBytes: 1024 * 1024,
224 - outputMode: "truncate",
225 - })
226 - .pipe(
227 - Effect.map((result) => parseWindowsListenerOutput(result.stdout, terminalByProcessId)),
228 - Effect.catchCause(() => Effect.succeed(null)),
229 - );
230 - if (listeners !== null) return listeners;
231 - return yield* probeCommonPorts();
227 + const scanOnce = Effect.fn("PortDiscovery.scan")(function* () {
228 + const terminalByProcessId = new Map<number, TerminalProcessOwner>();
229 + for (const registration of terminalProcesses.values()) {
230 + for (const processId of registration.processIds) {
231 + terminalByProcessId.set(processId, registration.owner);
232 }
233 - const lsofResult = yield* processRunner
233 + }
234 + if (process.platform === "win32") {
235 + const command =
236 + 'Get-NetTCPConnection -State Listen -ErrorAction Stop | ForEach-Object { $processName = (Get-Process -Id $_.OwningProcess -ErrorAction SilentlyContinue).ProcessName; Write-Output "$($_.LocalAddress)|$($_.LocalPort)|$($_.OwningProcess)|$processName" }';
237 + const listeners = yield* processRunner
238 .run({
235 - command: "lsof",
236 - args: ["-iTCP", "-sTCP:LISTEN", "-P", "-n", "-F", "pcn"],
237 - timeout: Duration.millis(LSOF_TIMEOUT_MS),
239 + command: "powershell.exe",
240 + args: ["-NoProfile", "-NonInteractive", "-Command", command],
241 + timeout: Duration.millis(WINDOWS_LISTENER_TIMEOUT_MS),
242 maxOutputBytes: 1024 * 1024,
243 outputMode: "truncate",
244 })
245 .pipe(
242 - Effect.map((result) => parseLsofOutput(result.stdout, terminalByProcessId)),
246 + Effect.map((result) => parseWindowsListenerOutput(result.stdout, terminalByProcessId)),
247 Effect.catchCause(() => Effect.succeed(null)),
248 );
245 - if (lsofResult !== null) return lsofResult;
249 + if (listeners !== null) return listeners;
250 return yield* probeCommonPorts();
247 - });
251 + }
252 + const lsofResult = yield* processRunner
253 + .run({
254 + command: "lsof",
255 + args: ["-iTCP", "-sTCP:LISTEN", "-P", "-n", "-F", "pcn"],
256 + timeout: Duration.millis(LSOF_TIMEOUT_MS),
257 + maxOutputBytes: 1024 * 1024,
258 + outputMode: "truncate",
259 + })
260 + .pipe(
261 + Effect.map((result) => parseLsofOutput(result.stdout, terminalByProcessId)),
262 + Effect.catchCause(() => Effect.succeed(null)),
263 + );
264 + if (lsofResult !== null) return lsofResult;
265 + return yield* probeCommonPorts();
266 + });
267
268 const broadcast = (servers: ReadonlyArray<DiscoveredLocalServer>): Effect.Effect<void> =>
269 Effect.forEach(Array.from(listeners), (listener) => listener(servers), { discard: true });
@@ -266,33 +285,37 @@ export const makePortDiscovery = Effect.gen(function* () {
285 // currently retained, so the cost is one Ref.get every POLL_INTERVAL.
286 yield* Effect.forkScoped(pollTick.pipe(Effect.repeat(Schedule.spaced(POLL_INTERVAL))));
287
269 - const retain: PortDiscoveryShape["retain"] = () =>
270 - Effect.gen(function* () {
271 - const wasIdle = retainCount === 0;
272 - retainCount += 1;
273 - if (wasIdle) {
274 - // Run an immediate scan + broadcast so the new retainer doesn't have
275 - // to wait up to POLL_INTERVAL for the first emission.
276 - yield* pollTick;
277 - }
278 - let released = false;
279 - return () => {
280 - if (released) return;
281 - released = true;
282 - retainCount = Math.max(0, retainCount - 1);
283 - };
284 - });
288 + const retain: PortDiscoveryShape["retain"] = Effect.fn("PortDiscovery.retain")(function* () {
289 + const wasIdle = retainCount === 0;
290 + retainCount += 1;
291 + if (wasIdle) {
292 + // Run an immediate scan + broadcast so the new retainer doesn't have
293 + // to wait up to POLL_INTERVAL for the first emission.
294 + yield* pollTick;
295 + }
296 + let released = false;
297 + return () => {
298 + if (released) return;
299 + released = true;
300 + retainCount = Math.max(0, retainCount - 1);
301 + };
302 + });
303
286 - const subscribe: PortDiscoveryShape["subscribe"] = (listener) =>
287 - Effect.sync(() => {
288 - listeners.add(listener);
289 - return () => {
290 - listeners.delete(listener);
291 - };
292 - });
304 + const subscribe: PortDiscoveryShape["subscribe"] = Effect.fn("PortDiscovery.subscribe")(
305 + function* (listener) {
306 + return yield* Effect.sync(() => {
307 + listeners.add(listener);
308 + return () => {
309 + listeners.delete(listener);
310 + };
311 + });
312 + },
313 + );
314
294 - const registerTerminalProcesses: PortDiscoveryShape["registerTerminalProcesses"] = (input) =>
295 - Effect.sync(() => {
315 + const registerTerminalProcesses: PortDiscoveryShape["registerTerminalProcesses"] = Effect.fn(
316 + "PortDiscovery.registerTerminalProcesses",
317 + )(function* (input) {
318 + yield* Effect.sync(() => {
319 const owner = {
320 threadId: ThreadId.make(input.threadId),
321 terminalId: input.terminalId,
@@ -307,11 +330,15 @@ export const makePortDiscovery = Effect.gen(function* () {
330 }
331 terminalProcesses.set(key, { owner, processIds });
332 });
333 + });
334
311 - const unregisterTerminal: PortDiscoveryShape["unregisterTerminal"] = (input) =>
312 - Effect.sync(() => {
335 + const unregisterTerminal: PortDiscoveryShape["unregisterTerminal"] = Effect.fn(
336 + "PortDiscovery.unregisterTerminal",
337 + )(function* (input) {
338 + yield* Effect.sync(() => {
339 terminalProcesses.delete(terminalOwnerKey(input));
340 });
341 + });
342
343 return {
344 scan: scanOnce,
@@ -322,7 +349,7 @@ export const makePortDiscovery = Effect.gen(function* () {
349 } satisfies PortDiscoveryShape;
350 });
351
325 -export const PortDiscoveryLive = Layer.effect(PortDiscovery, makePortDiscovery);
352 +export const layer = Layer.effect(PortDiscovery, make);
353
354 /** Exposed for tests. */
355 export const __testing = {
apps/server/src/preview/Services/Manager.ts deleted
-94
@@ -1,94 +0,0 @@
1 -/**
2 - * PreviewManager - Per-thread browser preview session orchestration.
3 - *
4 - * Owns metadata-only preview sessions keyed by `(threadId, tabId)`. A thread
5 - * can host multiple concurrent tabs (browser-style). The actual Chromium
6 - * <webview> lives in the desktop renderer; this service tracks the canonical
7 - * URL/title/nav state per tab so sessions survive reconnects and are visible
8 - * to multiple connected clients.
9 - *
10 - * Event delivery uses Effect's `PubSub` so listener failures (e.g. a
11 - * disconnected WS subscriber's queue being closed) cannot propagate back
12 - * into the mutating call's failure channel — matches the codebase
13 - * convention used by `SessionCredentialService`.
14 - *
15 - * @module PreviewManager
16 - */
17 -import {
18 - type PreviewCloseInput,
19 - type PreviewError,
20 - type PreviewEvent,
21 - type PreviewListInput,
22 - type PreviewListResult,
23 - type PreviewNavigateInput,
24 - type PreviewOpenInput,
25 - type PreviewRefreshInput,
26 - type PreviewReportStatusInput,
27 - type PreviewSessionSnapshot,
28 -} from "@t3tools/contracts";
29 -import { Context, type Effect, type PubSub, type Scope, type Stream } from "effect";
30 -
31 -export interface PreviewManagerShape {
32 - /**
33 - * Open a brand new preview tab for `threadId`. When `url` is omitted the
34 - * tab starts in the `Idle` state so the user can type into the URL bar;
35 - * otherwise it transitions straight to `Loading`. Always emits `opened`.
36 - */
37 - readonly open: (input: PreviewOpenInput) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
38 -
39 - /**
40 - * Update the session's URL/title from the renderer (after the <webview>
41 - * resolved navigation, including redirects). Emits a `navigated` event.
42 - */
43 - readonly navigate: (
44 - input: PreviewNavigateInput,
45 - ) => Effect.Effect<PreviewSessionSnapshot, PreviewError>;
46 -
47 - /**
48 - * Renderer reports current nav status (Loading/Success/LoadFailed) and
49 - * back/forward availability. Drives `failed` and `navigated` events.
50 - */
51 - readonly reportStatus: (input: PreviewReportStatusInput) => Effect.Effect<void, PreviewError>;
52 -
53 - /**
54 - * Renderer requested a reload. Server records intent (no state change) so
55 - * subscribers can reconcile if needed; the desktop bridge actually reloads.
56 - */
57 - readonly refresh: (input: PreviewRefreshInput) => Effect.Effect<void, PreviewError>;
58 -
59 - /**
60 - * Close the session (drop server-side state, emit `closed`). When `tabId`
61 - * is omitted, closes every session for the thread.
62 - */
63 - readonly close: (input: PreviewCloseInput) => Effect.Effect<void, PreviewError>;
64 -
65 - /**
66 - * List active preview sessions for a thread. Returns an empty array when
67 - * the thread has no sessions.
68 - */
69 - readonly list: (input: PreviewListInput) => Effect.Effect<PreviewListResult>;
70 -
71 - /**
72 - * Stream of preview events. Each subscriber gets its own buffered stream;
73 - * subscriber failures are isolated and do not affect other subscribers
74 - * or publishers.
75 - *
76 - * NOTE: `Stream.fromPubSub` defers `PubSub.subscribe` until the stream
77 - * starts running, so `Stream.runForEach(...).pipe(Effect.forkScoped)` may
78 - * miss a publish landing between fork and stream-start. Callers that
79 - * cannot tolerate this gap should use `subscribeEvents` below.
80 - */
81 - readonly events: Stream.Stream<PreviewEvent>;
82 -
83 - /**
84 - * Acquire a PubSub subscription synchronously in the caller's fiber so
85 - * no publish can land in the narrow gap between subscribe and consumer
86 - * loop start. The subscription's lifetime is bound to the provided
87 - * `Scope`; release happens automatically on scope close.
88 - */
89 - readonly subscribeEvents: Effect.Effect<PubSub.Subscription<PreviewEvent>, never, Scope.Scope>;
90 -}
91 -
92 -export class PreviewManager extends Context.Service<PreviewManager, PreviewManagerShape>()(
93 - "t3/preview/Services/Manager/PreviewManager",
94 -) {}
apps/server/src/preview/Services/PortScanner.ts deleted
-46
@@ -1,46 +0,0 @@
1 -/**
2 - * PortDiscovery - Discovers listening localhost ports and attributes them to
3 - * registered terminal process families.
4 - *
5 - * Reference-counted polling: the scanner only runs when at least one client
6 - * has called `retain()`. This keeps idle desktops from running `lsof` every
7 - * 3 seconds for nothing.
8 - *
9 - * @module PortDiscovery
10 - */
11 -import type { DiscoveredLocalServer } from "@t3tools/contracts";
12 -import { Context, type Effect } from "effect";
13 -
14 -export interface PortDiscoveryShape {
15 - /** One-shot snapshot of currently listening localhost ports. */
16 - readonly scan: () => Effect.Effect<ReadonlyArray<DiscoveredLocalServer>>;
17 - /** Subscribe to changes. Listener invoked on every diff. Returns unsubscribe fn. */
18 - readonly subscribe: (
19 - listener: (servers: ReadonlyArray<DiscoveredLocalServer>) => Effect.Effect<void>,
20 - ) => Effect.Effect<() => void>;
21 - /**
22 - * Hint that at least one client is interested → starts polling. Returns
23 - * release fn. When release count hits 0, polling stops.
24 - */
25 - readonly retain: () => Effect.Effect<() => void>;
26 - /** Associate a terminal with its current process family for port attribution. */
27 - readonly registerTerminalProcesses: (input: {
28 - readonly threadId: string;
29 - readonly terminalId: string;
30 - readonly processIds: ReadonlyArray<number>;
31 - }) => Effect.Effect<void>;
32 - /** Remove process attribution for a terminal that stopped or closed. */
33 - readonly unregisterTerminal: (input: {
34 - readonly threadId: string;
35 - readonly terminalId: string;
36 - }) => Effect.Effect<void>;
37 -}
38 -
39 -export class PortDiscovery extends Context.Service<PortDiscovery, PortDiscoveryShape>()(
40 - "t3/preview/Services/PortScanner/PortDiscovery",
41 -) {}
42 -
43 -/** Curated list of common dev-server ports for the Windows TCP-probe fallback. */
44 -export const COMMON_DEV_PORTS: ReadonlyArray<number> = Object.freeze([
45 - 3000, 3001, 3333, 4173, 4200, 4321, 5000, 5173, 5174, 5175, 5500, 8000, 8080, 8081, 8888, 9000,
46 -]);
apps/server/src/provider/Layers/ClaudeAdapter.ts
+2 -2
@@ -69,7 +69,7 @@ import * as Stream from "effect/Stream";
69
70 import { resolveAttachmentPath } from "../../attachmentStore.ts";
71 import { ServerConfig } from "../../config.ts";
72 -import { readMcpProviderSession } from "../../mcp/Services/McpProviderSession.ts";
72 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
73 import { makeClaudeEnvironment } from "../Drivers/ClaudeHome.ts";
74 import {
75 getClaudeModelCapabilities,
@@ -3446,7 +3446,7 @@ export const makeClaudeAdapter = Effect.fn("makeClaudeAdapter")(function* (
3446 ...(fastMode ? { fastMode: true } : {}),
3447 ...(ultracode ? { ultracode: true } : {}),
3448 };
3449 - const mcpSession = readMcpProviderSession(input.threadId);
3449 + const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
3450 const queryOptions: ClaudeQueryOptions = {
3451 ...(input.cwd ? { cwd: input.cwd } : {}),
3452 ...(apiModelId ? { model: apiModelId } : {}),
apps/server/src/provider/Layers/CodexAdapter.ts
+2 -2
@@ -39,7 +39,7 @@ import * as EffectCodexSchema from "effect-codex-app-server/schema";
39
40 import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
41 import { getCodexServiceTierOptionValue } from "../../codexModelOptions.ts";
42 -import { readMcpProviderSession } from "../../mcp/Services/McpProviderSession.ts";
42 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
43
44 import {
45 ProviderAdapterRequestError,
@@ -1386,7 +1386,7 @@ export const makeCodexAdapter = Effect.fn("makeCodexAdapter")(function* (
1386 input.modelSelection?.instanceId === boundInstanceId
1387 ? getCodexServiceTierOptionValue(input.modelSelection)
1388 : undefined;
1389 - const mcpSession = readMcpProviderSession(input.threadId);
1389 + const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
1390 const runtimeInput: CodexSessionRuntimeOptions = {
1391 threadId: input.threadId,
1392 providerInstanceId: boundInstanceId,
apps/server/src/provider/Layers/CursorAdapter.ts
+2 -2
@@ -42,7 +42,7 @@ import type * as EffectAcpSchema from "effect-acp/schema";
42
43 import { resolveAttachmentPath } from "../../attachmentStore.ts";
44 import { ServerConfig } from "../../config.ts";
45 -import { readMcpProviderSession } from "../../mcp/Services/McpProviderSession.ts";
45 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
46 import {
47 ProviderAdapterProcessError,
48 ProviderAdapterRequestError,
@@ -531,7 +531,7 @@ export function makeCursorAdapter(
531 ? yield* options.resolveSettings
532 : cursorSettings;
533
534 - const mcpSession = readMcpProviderSession(input.threadId);
534 + const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
535 const acp = yield* makeCursorAcpRuntime({
536 cursorSettings: effectiveCursorSettings,
537 ...(options?.environment ? { environment: options.environment } : {}),
apps/server/src/provider/Layers/GrokAdapter.ts
+2 -2
@@ -33,7 +33,7 @@ import type * as EffectAcpSchema from "effect-acp/schema";
33
34 import { resolveAttachmentPath } from "../../attachmentStore.ts";
35 import { ServerConfig } from "../../config.ts";
36 -import { readMcpProviderSession } from "../../mcp/Services/McpProviderSession.ts";
36 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
37 import {
38 ProviderAdapterProcessError,
39 ProviderAdapterRequestError,
@@ -375,7 +375,7 @@ export function makeGrokAdapter(grokSettings: GrokSettings, options?: GrokAdapte
375 threadId: input.threadId,
376 });
377
378 - const mcpSession = readMcpProviderSession(input.threadId);
378 + const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
379 const acp = yield* makeGrokAcpRuntime({
380 grokSettings,
381 ...(options?.environment ? { environment: options.environment } : {}),
apps/server/src/provider/Layers/OpenCodeAdapter.ts
+2 -2
@@ -26,7 +26,7 @@ import { getModelSelectionStringOptionValue } from "@t3tools/shared/model";
26
27 import { resolveAttachmentPath } from "../../attachmentStore.ts";
28 import { ServerConfig } from "../../config.ts";
29 -import { readMcpProviderSession } from "../../mcp/Services/McpProviderSession.ts";
29 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
30 import { type EventNdjsonLogger, makeEventNdjsonLogger } from "./EventNdjsonLogger.ts";
31 import {
32 ProviderAdapterProcessError,
@@ -1054,7 +1054,7 @@ export function makeOpenCodeAdapter(
1054 directory,
1055 ...(server.external && serverPassword ? { serverPassword } : {}),
1056 });
1057 - const mcpSession = readMcpProviderSession(input.threadId);
1057 + const mcpSession = McpProviderSession.readMcpProviderSession(input.threadId);
1058 if (mcpSession && !server.external) {
1059 yield* runOpenCodeSdk("mcp.add", () =>
1060 client.mcp.add({
apps/server/src/provider/Layers/ProviderService.ts
+10 -16
@@ -56,16 +56,8 @@ import {
56 import { type EventNdjsonLogger } from "./EventNdjsonLogger.ts";
57 import { ProviderEventLoggers } from "./ProviderEventLoggers.ts";
58 import { AnalyticsService } from "../../telemetry/Services/AnalyticsService.ts";
59 -import {
60 - issueActiveMcpCredential,
61 - revokeActiveMcpThread,
62 - revokeAllActiveMcpCredentials,
63 -} from "../../mcp/Layers/McpSessionRegistry.ts";
64 -import {
65 - clearAllMcpProviderSessions,
66 - clearMcpProviderSession,
67 - setMcpProviderSession,
68 -} from "../../mcp/Services/McpProviderSession.ts";
59 +import * as McpProviderSession from "../../mcp/McpProviderSession.ts";
60 +import * as McpSessionRegistry from "../../mcp/McpSessionRegistry.ts";
61 const isModelSelection = Schema.is(ModelSelection);
62
63 /**
@@ -223,14 +215,16 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
215 const runtimeEventPubSub = yield* PubSub.unbounded<ProviderRuntimeEvent>();
216 const nowIso = Effect.map(DateTime.now, DateTime.formatIso);
217 const prepareMcpSession = (threadId: ThreadId, providerInstanceId: ProviderInstanceId) =>
226 - issueActiveMcpCredential({ threadId, providerInstanceId }).pipe(
218 + McpSessionRegistry.issueActiveMcpCredential({ threadId, providerInstanceId }).pipe(
219 Effect.tap((credential) =>
228 - credential ? Effect.sync(() => setMcpProviderSession(credential.config)) : Effect.void,
220 + credential
221 + ? Effect.sync(() => McpProviderSession.setMcpProviderSession(credential.config))
222 + : Effect.void,
223 ),
224 );
225 const clearMcpSession = (threadId: ThreadId) =>
232 - revokeActiveMcpThread(threadId).pipe(
233 - Effect.tap(() => Effect.sync(() => clearMcpProviderSession(threadId))),
226 + McpSessionRegistry.revokeActiveMcpThread(threadId).pipe(
227 + Effect.tap(() => Effect.sync(() => McpProviderSession.clearMcpProviderSession(threadId))),
228 );
229
230 const publishRuntimeEvent = (event: ProviderRuntimeEvent): Effect.Effect<void> =>
@@ -1027,8 +1021,8 @@ const makeProviderService = Effect.fn("makeProviderService")(function* (
1021 ),
1022 ).pipe(Effect.asVoid);
1023 yield* Effect.forEach(currentAdapters, ([, adapter]) => adapter.stopAll()).pipe(Effect.asVoid);
1030 - yield* revokeAllActiveMcpCredentials();
1031 - clearAllMcpProviderSessions();
1024 + yield* McpSessionRegistry.revokeAllActiveMcpCredentials();
1025 + McpProviderSession.clearAllMcpProviderSessions();
1026 const bindings = yield* directory.listBindings().pipe(Effect.orElseSucceed(() => []));
1027 yield* Effect.forEach(bindings, (binding) =>
1028 Effect.gen(function* () {
apps/server/src/server.test.ts
+4 -4
@@ -99,8 +99,8 @@ import { ServerLifecycleEvents, type ServerLifecycleEventsShape } from "./server
99 import { ServerRuntimeStartup, type ServerRuntimeStartupShape } from "./serverRuntimeStartup.ts";
100 import { ServerSettingsService, type ServerSettingsShape } from "./serverSettings.ts";
101 import { TerminalManager, type TerminalManagerShape } from "./terminal/Services/Manager.ts";
102 -import { PreviewManager } from "./preview/Services/Manager.ts";
103 -import { PortDiscovery } from "./preview/Services/PortScanner.ts";
102 +import * as PreviewManager from "./preview/Manager.ts";
103 +import * as PortScanner from "./preview/PortScanner.ts";
104 import {
105 BrowserTraceCollector,
106 type BrowserTraceCollectorShape,
@@ -669,7 +669,7 @@ const buildAppUnderTest = (options?: {
669 ),
670 Layer.provide(
671 Layer.mergeAll(
672 - Layer.mock(PreviewManager)({
672 + Layer.mock(PreviewManager.PreviewManager)({
673 open: () => Effect.die("PreviewManager not stubbed in this test"),
674 navigate: () => Effect.die("PreviewManager not stubbed in this test"),
675 reportStatus: () => Effect.void,
@@ -681,7 +681,7 @@ const buildAppUnderTest = (options?: {
681 PubSub.subscribe(pubsub),
682 ),
683 }),
684 - Layer.mock(PortDiscovery)({
684 + Layer.mock(PortScanner.PortDiscovery)({
685 scan: () => Effect.succeed([]),
686 subscribe: () => Effect.succeed(() => {}),
687 retain: () => Effect.succeed(() => {}),
apps/server/src/server.ts
+10 -7
@@ -35,10 +35,10 @@ import * as GitLabCli from "./sourceControl/GitLabCli.ts";
35 import * as TextGeneration from "./textGeneration/TextGeneration.ts";
36 import { ProviderInstanceRegistryHydrationLive } from "./provider/Layers/ProviderInstanceRegistryHydration.ts";
37 import { TerminalManagerLive } from "./terminal/Layers/Manager.ts";
38 -import { PreviewManagerLive } from "./preview/Layers/Manager.ts";
39 -import { PortDiscoveryLive } from "./preview/Layers/PortScanner.ts";
40 -import { McpHttpServerLive } from "./mcp/Layers/McpHttpServer.ts";
41 -import { McpSessionRegistryLive } from "./mcp/Layers/McpSessionRegistry.ts";
38 +import * as McpHttpServer from "./mcp/McpHttpServer.ts";
39 +import * as McpSessionRegistry from "./mcp/McpSessionRegistry.ts";
40 +import * as PreviewManager from "./preview/Manager.ts";
41 +import * as PortScanner from "./preview/PortScanner.ts";
42 import * as ProcessRunner from "./processRunner.ts";
43 import * as GitManager from "./git/GitManager.ts";
44 import { KeybindingsLive } from "./keybindings.ts";
@@ -236,14 +236,17 @@ const CheckpointingLayerLive = Layer.empty.pipe(
236 Layer.provideMerge(CheckpointStoreLive.pipe(Layer.provide(VcsDriverRegistryLayerLive))),
237 );
238
239 -const PortScannerLayerLive = PortDiscoveryLive.pipe(Layer.provide(ProcessRunner.layer));
239 +const PortScannerLayerLive = PortScanner.layer.pipe(Layer.provide(ProcessRunner.layer));
240
241 const TerminalLayerLive = TerminalManagerLive.pipe(
242 Layer.provide(PtyAdapterLive),
243 Layer.provide(PortScannerLayerLive),
244 );
245
246 -const PreviewLayerLive = Layer.mergeAll(PreviewManagerLive, PortScannerLayerLive);
246 +const PreviewLayerLive = Layer.empty.pipe(
247 + Layer.provideMerge(PreviewManager.layer),
248 + Layer.provideMerge(PortScannerLayerLive),
249 +);
250
251 const WorkspaceEntriesLayerLive = WorkspaceEntriesLive.pipe(
252 Layer.provide(WorkspacePathsLive),
@@ -353,7 +356,7 @@ export const makeRoutesLayer = Layer.mergeAll(
356 staticAndDevRouteLayer,
357 websocketRpcRouteLayer,
358 ),
356 - McpHttpServerLive.pipe(Layer.provide(McpSessionRegistryLive)),
359 + McpHttpServer.layer.pipe(Layer.provide(McpSessionRegistry.layer)),
360 ).pipe(Layer.provide(browserApiCorsLayer));
361
362 export const makeServerLayer = Layer.unwrap(
apps/server/src/terminal/Layers/Manager.ts
+2 -2
@@ -33,7 +33,7 @@ import {
33 terminalSessionsTotal,
34 } from "../../observability/Metrics.ts";
35 import * as ProcessRunner from "../../processRunner.ts";
36 -import { PortDiscovery } from "../../preview/Services/PortScanner.ts";
36 +import * as PortScanner from "../../preview/PortScanner.ts";
37 import {
38 TerminalCwdError,
39 TerminalHistoryError,
@@ -996,7 +996,7 @@ interface TerminalManagerOptions {
996 const makeTerminalManager = Effect.fn("makeTerminalManager")(function* () {
997 const { terminalLogsDir } = yield* ServerConfig;
998 const ptyAdapter = yield* PtyAdapter;
999 - const portDiscovery = yield* PortDiscovery;
999 + const portDiscovery = yield* PortScanner.PortDiscovery;
1000 return yield* makeTerminalManagerWithOptions({
1001 logsDir: terminalLogsDir,
1002 ptyAdapter,
apps/server/src/ws.ts
+7 -5
@@ -71,9 +71,9 @@ import { ServerLifecycleEvents } from "./serverLifecycleEvents.ts";
71 import { ServerRuntimeStartup } from "./serverRuntimeStartup.ts";
72 import { redactServerSettingsForClient, ServerSettingsService } from "./serverSettings.ts";
73 import { TerminalManager } from "./terminal/Services/Manager.ts";
74 -import { PreviewManager } from "./preview/Services/Manager.ts";
75 -import { PortDiscovery } from "./preview/Services/PortScanner.ts";
76 -import { previewAutomationBroker } from "./mcp/Layers/PreviewAutomationBroker.ts";
74 +import * as PreviewAutomationBroker from "./mcp/PreviewAutomationBroker.ts";
75 +import * as PreviewManager from "./preview/Manager.ts";
76 +import * as PortScanner from "./preview/PortScanner.ts";
77 import { WorkspaceEntries } from "./workspace/Services/WorkspaceEntries.ts";
78 import { WorkspaceFileSystem } from "./workspace/Services/WorkspaceFileSystem.ts";
79 import { WorkspacePathOutsideRootError } from "./workspace/Services/WorkspacePaths.ts";
@@ -256,8 +256,9 @@ const makeWsRpcLayer = (currentSession: AuthenticatedSession) =>
256 const vcsProvisioning = yield* VcsProvisioningService;
257 const vcsStatusBroadcaster = yield* VcsStatusBroadcaster;
258 const terminalManager = yield* TerminalManager;
259 - const previewManager = yield* PreviewManager;
260 - const portDiscovery = yield* PortDiscovery;
259 + const previewAutomationBroker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
260 + const previewManager = yield* PreviewManager.PreviewManager;
261 + const portDiscovery = yield* PortScanner.PortDiscovery;
262 const providerRegistry = yield* ProviderRegistry;
263 const providerMaintenanceRunner = yield* ProviderMaintenanceRunner.ProviderMaintenanceRunner;
264 const config = yield* ServerConfig;
@@ -1573,6 +1574,7 @@ export const websocketRpcRouteLayer = Layer.unwrap(
1574 Effect.provide(
1575 makeWsRpcLayer(session).pipe(
1576 Layer.provideMerge(RpcSerialization.layerJson),
1577 + Layer.provide(PreviewAutomationBroker.layer),
1578 Layer.provide(ProviderMaintenanceRunner.layer),
1579 Layer.provide(
1580 SourceControlDiscoveryLayer.layer.pipe(