Skip to content
Source
Sign inSign up
pingdotgg/t3code
Open History to see the last commit

handlers.ts

62 lines2.3 KB

1import { OrchestratorMcpFailure } from "@t3tools/contracts";
2import * as Effect from "effect/Effect";
3​
4import * as HtmlRender from "../../../htmlRender/HtmlRender.ts";
5import * as McpInvocationContext from "../../McpInvocationContext.ts";
6import * as McpToolAccess from "../../McpToolAccess.ts";
7import { HtmlPreviewToolkit, HtmlRenderToolkit, type HtmlToolkit } from "./tools.ts";
8​
9const INVALID_PAGE_ERRORS = new Set([
10 "HtmlRenderImagesNotFoundError",
11 "HtmlRenderImageTooLargeError",
12 "HtmlRenderPageTooLargeError",
13 "HtmlRenderViewportHeightError",
14]);
15​
16const toFailure = (error: { readonly _tag: string; readonly message: string }) =>
17 new OrchestratorMcpFailure({
18 code: INVALID_PAGE_ERRORS.has(error._tag) ? "invalid_request" : "orchestration_error",
19 message: error.message,
20 });
21​
22const handlers = {
23 // Local-file access limits this host browser to T3-launched agents.
24 html_preview: McpToolAccess.readsAsCaller((input) =>
25 Effect.gen(function* () {
26 const htmlRender = yield* HtmlRender.HtmlRender;
27 const { png, ...preview } = yield* htmlRender.preview(input).pipe(Effect.mapError(toFailure));
28 return {
29 ...preview,
30 screenshot: {
31 mimeType: "image/png" as const,
32 data: png,
33 width: preview.width,
34 height: preview.capturedHeight,
35 },
36 };
37 }),
38 ),
39 html_render: McpToolAccess.actsAsCaller((input) =>
40 Effect.gen(function* () {
41 const scope = yield* McpInvocationContext.McpInvocationContext;
42 const { thread } = yield* McpInvocationContext.requireThreadScope(scope, "html_render");
43 const htmlRender = yield* HtmlRender.HtmlRender;
44 const reference = yield* htmlRender
45 .publish({ threadId: thread.threadId, ...input })
46 .pipe(Effect.mapError(toFailure));
47 return {
48 htmlRender: reference,
49 message:
50 "Shown to the reader above your reply. Don't mention or describe the page; reply with only what it doesn't already say.",
51 };
52 }),
53 ),
54} satisfies McpToolAccess.Handlers<typeof HtmlToolkit.tools>;
55​
56export const layerPreview = McpToolAccess.toLayer(HtmlPreviewToolkit, {
57 html_preview: handlers.html_preview,
58});
59​
60export const layerRender = McpToolAccess.toLayer(HtmlRenderToolkit, {
61 html_render: handlers.html_render,
62});