Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
85 changes: 85 additions & 0 deletions apps/server/src/mcp/toolkits/previewControls/handlers.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,10 @@ import { expect, it } from "@effect/vitest";
import {
DEFAULT_SERVER_SETTINGS,
EnvironmentId,
PreviewAutomationControlInterruptedError,
ProjectId,
ProviderInstanceId,
SERVER_BROWSER_AUTOMATION_CLIENT_ID,
ThreadId,
} from "@t3tools/contracts";
import { resolveProjectSettings } from "@t3tools/shared/projectSettings";
Expand Down Expand Up @@ -101,3 +103,86 @@ it.effect.each([
}),
),
);

it.effect("close reports why the server browser refused to close a tab", () =>
Effect.scoped(
Effect.gen(function* () {
const threadId = ThreadId.make("preview-controls-thread");
const scope: McpInvocationContext.McpInvocationScope = {
environmentId: EnvironmentId.make("preview-controls-environment"),
requestNamespace: "preview-controls-provider-session",
thread: {
threadId,
providerSessionId: "preview-controls-provider-session",
providerInstanceId: ProviderInstanceId.make("codex"),
},
client: undefined,
capabilities: new Set(["preview"]),
issuedAt: 0,
};
const manager = yield* Preview.make.pipe(
Effect.provide(
Layer.merge(
ServerConfig.layerTest(process.cwd(), { prefix: "t3-preview-controls-" }).pipe(
Layer.provide(NodeServices.layer),
),
NodeCrypto.layer,
),
),
);
const tab = yield* manager.open({
threadId,
url: "http://localhost:3000",
runtime: "server",
});
const layerDependencies = Layer.mergeAll(
Layer.mock(PreviewAutomationBroker.PreviewAutomationBroker)({
invoke: () =>
Effect.fail(
new PreviewAutomationControlInterruptedError({
operation: "close",
environmentId: scope.environmentId,
threadId,
providerSessionId: "preview-controls-provider-session",
providerInstanceId: ProviderInstanceId.make("codex"),
clientId: SERVER_BROWSER_AUTOMATION_CLIENT_ID,
connectionId: "server-browser-connection",
requestId: "close-request",
tabId: tab.tabId,
timeoutMs: 1_000,
remoteTag: "PreviewAutomationControlInterruptedError",
remoteMessageLength: 0,
cause: null,
reason: "agentMismatch",
}),
),
}),
Layer.succeed(Preview.PreviewManager, manager),
Layer.succeed(McpInvocationContext.McpInvocationContext, scope),
McpToolAccessTestkit.liveThreadsLayer,
Layer.mock(ServerSettings.ServerSettingsService)({
getSettings: Effect.succeed({
...DEFAULT_SERVER_SETTINGS,
enableAgentBrowserAccess: true,
}),
}),
);
const toolkit = yield* PreviewControlsToolkit.pipe(
Effect.provide(
McpToolAccess.HandlersLayer.layer(PreviewControlsHandlers.layer).pipe(
Layer.provide(layerDependencies),
),
),
);

const closed = yield* toolkit
.handle("t3_preview_close", { tabId: tab.tabId })
.pipe(Stream.unwrap, Stream.runCollect, Effect.provide(layerDependencies));

expect(closed.at(-1)?.result).toMatchObject({
_tag: "PreviewAutomationControlInterruptedError",
reason: "agentMismatch",
});
}),
),
);
4 changes: 1 addition & 3 deletions apps/server/src/mcp/toolkits/previewControls/handlers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,7 @@ export const layer = McpToolAccess.toLayer(PreviewControlsToolkit, {
sessions.some((session) => session.tabId === input.tabId && session.runtime === "server")
) {
const broker = yield* PreviewAutomationBroker.PreviewAutomationBroker;
yield* broker
.invoke({ scope, operation: "close", input: {}, tabId: input.tabId })
.pipe(Effect.mapError(unavailable));
yield* broker.invoke({ scope, operation: "close", input: {}, tabId: input.tabId });
return {};
}
yield* manager
Expand Down
4 changes: 2 additions & 2 deletions apps/server/src/mcp/toolkits/previewControls/tools.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
NonNegativeInt,
OrchestratorMcpFailure,
PreviewAutomationUnavailableError,
PreviewAutomationError,
PreviewListResult,
PreviewTabId,
} from "@t3tools/contracts";
Expand All @@ -13,7 +13,7 @@ import * as PreviewAutomationBroker from "../../PreviewAutomationBroker.ts";
import * as McpInvocationContext from "../../McpInvocationContext.ts";

const shared = {
failure: Schema.Union([OrchestratorMcpFailure, PreviewAutomationUnavailableError]),
failure: Schema.Union([PreviewAutomationError, OrchestratorMcpFailure]),
failureMode: "return" as const,
dependencies: [
McpInvocationContext.McpInvocationContext,
Expand Down
Loading