Open History to see the last commit
1import { OrchestratorMcpFailure } from "@t3tools/contracts";
2import {
3 HTML_RENDER_COLUMN_WIDTH,
4 HTML_RENDER_LAYOUT_GUIDE,
5 HTML_RENDER_MAX_FIT_HEIGHT,
6 HTML_RENDER_MAX_HEIGHT,
7 HTML_RENDER_MAX_TITLE_LENGTH,
8 HTML_RENDER_MIN_HEIGHT,
9 HTML_RENDER_THEME_GUIDE,
10 HTML_RENDER_TOOL_NAME,
11} from "@t3tools/shared/htmlRender";
12import * as Schema from "effect/Schema";
13import { Tool, Toolkit } from "effect/ai";
14
15import * as HtmlRender from "../../../htmlRender/HtmlRender.ts";
16import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
17import * as McpInvocationContext from "../../McpInvocationContext.ts";
18
19const Html = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512_000)).annotate({
20 description: "A complete, self-contained HTML document.",
21});
22
23const PAGE_RULES =
24 "Inline <style> and <script>. Local images as absolute paths are inlined. Remote URLs load as-is.";
25
26export const HtmlPreviewTool = Tool.make("html_preview", {
27 description: `Check an HTML page before html_render. Returns PNG, contentHeight, and console output. If it says the browser is still installing, call again in a minute. ${PAGE_RULES} Theme and layout rules are in html_render.`,
28 parameters: Schema.Struct({
29 html: Html,
30 width: Schema.optional(
31 Schema.Int.annotate({
32 description: `CSS pixels, 240 to 1600. Default ${HTML_RENDER_COLUMN_WIDTH}. Use about 390 for phones.`,
33 }),
34 ),
35 appearance: Schema.optional(
36 Schema.Literals(["dark", "light"]).annotate({
37 description: "Default dark.",
38 }),
39 ),
40 }),
41 success: Schema.Struct({
42 width: Schema.Int,
43 contentHeight: Schema.Int,
44 capturedHeight: Schema.Int,
45 consoleMessages: Schema.Array(
46 Schema.Struct({
47 level: Schema.Literals(["log", "info", "warning", "error"]),
48 text: Schema.String,
49 }),
50 ),
51 missingImages: Schema.optional(Schema.Array(Schema.String)),
52 screenshot: Schema.Struct({
53 mimeType: Schema.Literal("image/png"),
54 data: Schema.String,
55 width: Schema.Int,
56 height: Schema.Int,
57 }),
58 }),
59 failure: OrchestratorMcpFailure,
60 dependencies: [
61 McpInvocationContext.McpInvocationContext,
62 ThreadManagementService.ThreadManagementService,
63 HtmlRender.HtmlRender,
64 ],
65})
66 .annotate(Tool.Title, "Preview HTML")
67 .annotate(Tool.Readonly, true)
68 .annotate(Tool.Destructive, false)
69 .annotate(Tool.Idempotent, true)
70 .annotate(Tool.OpenWorld, true);
71
72// This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it.
73const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
74 description: `Show a finished HTML page (chart, diagram, mockup) inline in this thread, above your final reply. Do not announce or restate the page. Frame fits height to ${HTML_RENDER_MAX_FIT_HEIGHT}px. On mobile, content past the given height scrolls. ${HTML_RENDER_LAYOUT_GUIDE} ${HTML_RENDER_THEME_GUIDE}`,
75 parameters: Schema.Struct({
76 html: Html,
77 title: Schema.String.check(
78 Schema.isMinLength(1),
79 Schema.isMaxLength(HTML_RENDER_MAX_TITLE_LENGTH),
80 ),
81 height: Schema.Int.annotate({
82 description: `Frame height in CSS pixels, ${HTML_RENDER_MIN_HEIGHT} to ${HTML_RENDER_MAX_HEIGHT}. Use the contentHeight from html_preview.`,
83 }),
84 }),
85 success: Schema.Struct({
86 htmlRender: Schema.Struct({
87 attachmentId: Schema.String,
88 title: Schema.String,
89 height: Schema.Number,
90 heights: Schema.optional(Schema.Array(Schema.Tuple([Schema.Int, Schema.Int]))),
91 }),
92 message: Schema.String,
93 }),
94 failure: OrchestratorMcpFailure,
95 failureMode: "return",
96 dependencies: [
97 McpInvocationContext.McpInvocationContext,
98 ThreadManagementService.ThreadManagementService,
99 HtmlRender.HtmlRender,
100 ],
101})
102 .annotate(Tool.Title, "Render HTML")
103 .annotate(Tool.Readonly, true)
104 .annotate(Tool.Destructive, false)
105 .annotate(Tool.Idempotent, false)
106 .annotate(Tool.OpenWorld, true);
107
108export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool);
109
110export const HtmlRenderToolkit = Toolkit.make(HtmlRenderTool);
111
112export const HtmlToolkit = Toolkit.make(HtmlPreviewTool, HtmlRenderTool);