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

main
ts 120 lines 4.39 KB
Raw
1 import { OrchestratorMcpFailure } from "@t3tools/contracts";
2 import {
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";
12 import * as Schema from "effect/Schema";
13 import { McpSchema, Tool, Toolkit } from "effect/ai";
14
15 import * as HtmlRender from "../../../htmlRender/HtmlRender.ts";
16 import * as ThreadManagementService from "../../../orchestration-v2/ThreadManagementService.ts";
17 import * as McpInvocationContext from "../../McpInvocationContext.ts";
18
19 const Html = Schema.String.check(Schema.isMinLength(1), Schema.isMaxLength(512_000)).annotate({
20 description: "Inline HTML.",
21 });
22
23 const HtmlPath = Schema.String.annotate({
24 description: "Absolute .html file path.",
25 });
26
27 const PAGE_RULES =
28 "Self-contained: inline <style> and <script>. Local images as absolute paths are inlined. Remote URLs load as-is.";
29
30 export 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 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
77
78 // This affects only the caller's thread, not its workspace, so read-only sandboxes and plan mode may use it.
79 const HtmlRenderTool = Tool.make(HTML_RENDER_TOOL_NAME, {
80 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}`,
81 parameters: Schema.Struct({
82 path: Schema.optional(HtmlPath),
83 html: Schema.optional(Html),
84 title: Schema.String.check(
85 Schema.isMinLength(1),
86 Schema.isMaxLength(HTML_RENDER_MAX_TITLE_LENGTH),
87 ),
88 height: Schema.Int.annotate({
89 description: `Frame height in CSS pixels, ${HTML_RENDER_MIN_HEIGHT} to ${HTML_RENDER_MAX_HEIGHT}. Use the contentHeight from html_preview.`,
90 }),
91 }),
92 success: Schema.Struct({
93 htmlRender: Schema.Struct({
94 attachmentId: Schema.String,
95 title: Schema.String,
96 height: Schema.Number,
97 heights: Schema.optional(Schema.Array(Schema.Tuple([Schema.Int, Schema.Int]))),
98 }),
99 message: Schema.String,
100 }),
101 failure: OrchestratorMcpFailure,
102 failureMode: "return",
103 dependencies: [
104 McpInvocationContext.McpInvocationContext,
105 ThreadManagementService.ThreadManagementService,
106 HtmlRender.HtmlRender,
107 ],
108 })
109 .annotate(Tool.Title, "Render HTML")
110 .annotate(Tool.Readonly, true)
111 .annotate(Tool.Destructive, false)
112 .annotate(Tool.Idempotent, false)
113 .annotate(Tool.OpenWorld, true)
114 .annotate(McpSchema.EnabledWhen, McpInvocationContext.hiddenFromOutsideClients);
115
116 export const HtmlPreviewToolkit = Toolkit.make(HtmlPreviewTool);
117
118 export const HtmlRenderToolkit = Toolkit.make(HtmlRenderTool);
119
120 export const HtmlToolkit = Toolkit.make(HtmlPreviewTool, HtmlRenderTool);