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: "Inline HTML.",
21});
22
23const HtmlPath = Schema.String.annotate({
24 description: "Absolute .html file path.",
25});
26
27const PAGE_RULES =
28 "Self-contained: inline <style> and <script>. Local images as absolute paths are inlined. Remote URLs load as-is.";
29
30export const HtmlPreviewTool = Tool.make("html_preview", {
31 description: `Check an HTML page before html_render. Returns PNG, contentHeight, and console output. ${PAGE_RULES} Theme and layout rules are in html_render.`,
32 parameters: Schema.Struct({
33 path: Schema.optional(HtmlPath),
34 html: Schema.optional(Html),
35 width: Schema.optional(
36 Schema.Int.annotate({
37 description: `CSS pixels, 240 to 1600. Default ${HTML_RENDER_COLUMN_WIDTH}. Use about 390 for phones.`,
38 }),
39 ),
40 appearance: Schema.optional(
41 Schema.Literals(["dark", "light"]).annotate({
42 description: "Default dark.",
43 }),
44 ),
45 }),
46 success: Schema.Struct({
47 width: Schema.Int,
48 contentHeight: Schema.Int,
49 capturedHeight: Schema.Int,
50 consoleMessages: Schema.Array(
51 Schema.Struct({
52 level: Schema.Literals(["log", "info", "warning", "error"]),
53 text: Schema.String,
54 }),
55 ),
56 missingImages: Schema.optional(Schema.Array(Schema.String)),
57 screenshot: Schema.Struct({
58 mimeType: Schema.Literal("image/png"),
59 data: Schema.String,
60 width: Schema.Int,
61 height: Schema.Int,
62 }),
63 }),
64 failure: OrchestratorMcpFailure,
65 dependencies: [
66 McpInvocationContext.McpInvocationContext,
67 ThreadManagementService.ThreadManagementService,
68 HtmlRender.HtmlRender,
69 ],
70})
71 .annotate(Tool.Title, "Preview HTML")
72 .annotate(Tool.Readonly, true)
73 .annotate(Tool.Destructive, false)
74 .annotate(Tool.Idempotent, true)
75 .annotate(Tool.OpenWorld, true);
76
77// This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it.
78const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
79 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}`,
80 parameters: Schema.Struct({
81 path: Schema.optional(HtmlPath),
82 html: Schema.optional(Html),
83 title: Schema.String.check(
84 Schema.isMinLength(1),
85 Schema.isMaxLength(HTML_RENDER_MAX_TITLE_LENGTH),
86 ),
87 height: Schema.Int.annotate({
88 description: `Frame height in CSS pixels, ${HTML_RENDER_MIN_HEIGHT} to ${HTML_RENDER_MAX_HEIGHT}. Use the contentHeight from html_preview.`,
89 }),
90 }),
91 success: Schema.Struct({
92 htmlRender: Schema.Struct({
93 attachmentId: Schema.String,
94 title: Schema.String,
95 height: Schema.Number,
96 heights: Schema.optional(Schema.Array(Schema.Tuple([Schema.Int, Schema.Int]))),
97 }),
98 message: Schema.String,
99 }),
100 failure: OrchestratorMcpFailure,
101 failureMode: "return",
102 dependencies: [
103 McpInvocationContext.McpInvocationContext,
104 ThreadManagementService.ThreadManagementService,
105 HtmlRender.HtmlRender,
106 ],
107})
108 .annotate(Tool.Title, "Render HTML")
109 .annotate(Tool.Readonly, true)
110 .annotate(Tool.Destructive, false)
111 .annotate(Tool.Idempotent, false)
112 .annotate(Tool.OpenWorld, true);
113
114export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool);
115
116export const HtmlRenderToolkit = Toolkit.make(HtmlRenderTool);
117
118export const HtmlToolkit = Toolkit.make(HtmlPreviewTool, HtmlRenderTool);