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

main
ts 94 lines 3.57 KB
Raw
1 import { OrchestratorMcpFailure } from "@t3tools/contracts";
2 import * as Effect from "effect/Effect";
3
4 import * as HtmlRender from "../../../htmlRender/HtmlRender.ts";
5 import * as McpInvocationContext from "../../McpInvocationContext.ts";
6 import * as McpToolAccess from "../../McpToolAccess.ts";
7 import { loadCaller } from "../../threadAccess.ts";
8 import { HtmlPreviewToolkit, HtmlRenderToolkit, type HtmlToolkit } from "./tools.ts";
9
10 const INVALID_PAGE_ERRORS = new Set([
11 "HtmlRenderFileError",
12 "HtmlRenderFileTooLargeError",
13 "HtmlRenderImagesNotFoundError",
14 "HtmlRenderImageTooLargeError",
15 "HtmlRenderPageTooLargeError",
16 "HtmlRenderViewportHeightError",
17 ]);
18
19 const toFailure = (error: { readonly _tag: string; readonly message: string }) =>
20 new OrchestratorMcpFailure({
21 code: INVALID_PAGE_ERRORS.has(error._tag) ? "invalid_request" : "orchestration_error",
22 message: error.message,
23 });
24
25 // The server reads the file for the caller, so only a thread that reads files without asking may
26 // pass a path; a supervised thread passes the HTML its provider already let it read.
27 const pageSource = Effect.fn("HtmlToolkit.pageSource")(function* (input: {
28 readonly html?: string | undefined;
29 readonly path?: string | undefined;
30 }) {
31 if (input.html !== undefined && input.path === undefined) {
32 return { html: input.html } satisfies HtmlRender.HtmlRenderSource;
33 }
34 if (input.path === undefined || input.html !== undefined) {
35 return yield* new OrchestratorMcpFailure({
36 code: "invalid_request",
37 message: "Pass exactly one of html or path.",
38 });
39 }
40 const { limits } = yield* loadCaller();
41 if (limits.runtimeMode !== "full-access") {
42 return yield* new OrchestratorMcpFailure({
43 code: "capability_denied",
44 message: "Pass html instead of path: only full-access threads may pass a path.",
45 });
46 }
47 return { path: input.path } satisfies HtmlRender.HtmlRenderSource;
48 });
49
50 const handlers = {
51 // Local-file access limits this host browser to T3-launched agents.
52 html_preview: McpToolAccess.readsAsCaller((input) =>
53 Effect.gen(function* () {
54 const htmlRender = yield* HtmlRender.HtmlRender;
55 const source = yield* pageSource(input);
56 const { png, ...preview } = yield* htmlRender
57 .preview({ ...source, width: input.width, appearance: input.appearance })
58 .pipe(Effect.mapError(toFailure));
59 return {
60 ...preview,
61 screenshot: {
62 mimeType: "image/png" as const,
63 data: png,
64 width: preview.width,
65 height: preview.capturedHeight,
66 },
67 };
68 }),
69 ),
70 html_render: McpToolAccess.actsAsCaller((input) =>
71 Effect.gen(function* () {
72 const scope = yield* McpInvocationContext.McpInvocationContext;
73 const { thread } = yield* McpInvocationContext.requireThreadScope(scope, "html_render");
74 const htmlRender = yield* HtmlRender.HtmlRender;
75 const source = yield* pageSource(input);
76 const reference = yield* htmlRender
77 .publish({ ...source, threadId: thread.threadId, title: input.title, height: input.height })
78 .pipe(Effect.mapError(toFailure));
79 return {
80 htmlRender: reference,
81 message:
82 "Shown to the reader above your reply. Don't mention or describe the page; reply with only what it doesn't already say.",
83 };
84 }),
85 ),
86 } satisfies McpToolAccess.Handlers<typeof HtmlToolkit.tools>;
87
88 export const layerPreview = McpToolAccess.toLayer(HtmlPreviewToolkit, {
89 html_preview: handlers.html_preview,
90 });
91
92 export const layerRender = McpToolAccess.toLayer(HtmlRenderToolkit, {
93 html_render: handlers.html_render,
94 });