@@ -32,6 +32,7 @@ import {
3232} from "@t3tools/shared/imageDimensions" ;
3333import { githubMediaFetchUrl , githubMediaFileName } from "@t3tools/shared/githubMedia" ;
3434import { MCP_APP_MAX_HTML_BYTES } from "@t3tools/shared/mcpApp" ;
35+ import { HTML_RENDER_MAX_SOURCE_LENGTH , isHtmlRenderTool } from "@t3tools/shared/htmlRender" ;
3536import {
3637 isProjectFaviconPath ,
3738 PROJECT_FAVICON_FALLBACK_MARKER ,
@@ -58,6 +59,7 @@ import * as ServerSecretStore from "../auth/ServerSecretStore.ts";
5859import { parseAttachmentFileExtension , resolveAttachmentPathById } from "../attachmentStore.ts" ;
5960import * as ServerConfig from "../config.ts" ;
6061import * as Orchestrator from "../orchestration-v2/Orchestrator.ts" ;
62+ import * as HtmlRender from "../htmlRender/HtmlRender.ts" ;
6163import { expandHomePath } from "@t3tools/provider-core/server/pathExpansion" ;
6264import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts" ;
6365import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
@@ -156,6 +158,14 @@ const AssetClaimsSchema = Schema.Union([
156158 index : Schema . Number ,
157159 expiresAt : Schema . Number ,
158160 } ) ,
161+ Schema . Struct ( {
162+ version : Schema . Literal ( 1 ) ,
163+ kind : Schema . Literal ( "tool-input-html" ) ,
164+ threadId : ThreadId ,
165+ itemId : TurnItemId ,
166+ revision : Schema . String ,
167+ expiresAt : Schema . Number ,
168+ } ) ,
159169 Schema . Struct ( {
160170 version : Schema . Literal ( 1 ) ,
161171 kind : Schema . Literal ( "github-media" ) ,
@@ -251,6 +261,24 @@ const readToolOutputImage = Effect.fn("AssetAccess.readToolOutputImage")(functio
251261 : { mimeType : image . mimeType , bytes : Buffer . from ( image . data , "base64" ) } ;
252262} ) ;
253263
264+ const readToolInputHtml = Effect . fn ( "AssetAccess.readToolInputHtml" ) ( function * ( input : {
265+ readonly threadId : ThreadId ;
266+ readonly itemId : TurnItemId ;
267+ } ) {
268+ const orchestrator = yield * Orchestrator . OrchestratorV2 ;
269+ const item = yield * orchestrator . getTurnItem ( input ) ;
270+ if ( item ?. type !== "dynamic_tool" || ! isHtmlRenderTool ( item . toolName ) ) return null ;
271+ const args = item . input ;
272+ return typeof args === "object" &&
273+ args !== null &&
274+ "html" in args &&
275+ typeof args . html === "string" &&
276+ args . html . length <= HTML_RENDER_MAX_SOURCE_LENGTH &&
277+ args . html . trim ( )
278+ ? args . html
279+ : null ;
280+ } ) ;
281+
254282/** Serves an `.icns` favicon as its embedded PNG, since browsers cannot draw it. */
255283const projectFaviconAsset = Effect . fn ( "AssetAccess.projectFaviconAsset" ) ( function * (
256284 filePath : string ,
@@ -747,6 +775,25 @@ export const issueAssetUrl = Effect.fn("AssetAccess.issueAssetUrl")(function* (i
747775 }
748776 break ;
749777 }
778+ case "tool-input-html" : {
779+ const html = yield * readToolInputHtml ( input . resource ) . pipe (
780+ Effect . mapError (
781+ ( cause ) => new AssetWorkspaceAssetInspectionError ( { resource : input . resource , cause } ) ,
782+ ) ,
783+ ) ;
784+ if ( html === null )
785+ return yield * new AssetWorkspaceAssetNotFoundError ( { resource : input . resource } ) ;
786+ claims = {
787+ version : 1 ,
788+ kind : "tool-input-html" ,
789+ threadId : input . resource . threadId ,
790+ itemId : input . resource . itemId ,
791+ revision : input . resource . revision ,
792+ expiresAt,
793+ } ;
794+ fileName = "preview.html" ;
795+ break ;
796+ }
750797 case "tool-output-image" : {
751798 const image = yield * readToolOutputImage ( input . resource ) . pipe (
752799 Effect . mapError (
@@ -923,6 +970,35 @@ export const resolveAsset = Effect.fn("AssetAccess.resolveAsset")(function* (
923970 return image ? ( { kind : "bytes" , ...image } satisfies ResolvedAsset ) : null ;
924971 }
925972
973+ if ( claims . kind === "tool-input-html" ) {
974+ const html = yield * readToolInputHtml ( claims ) . pipe (
975+ Effect . tapError ( ( cause ) =>
976+ Effect . logError ( "Failed to read tool input HTML." , {
977+ threadId : claims . threadId ,
978+ itemId : claims . itemId ,
979+ cause,
980+ } ) ,
981+ ) ,
982+ Effect . orElseSucceed ( ( ) => null ) ,
983+ ) ;
984+ const document =
985+ html === null
986+ ? null
987+ : yield * HtmlRender . prepareDocument ( html ) . pipe (
988+ Effect . tapError ( ( cause ) =>
989+ Effect . logWarning ( "Failed to prepare tool input HTML." , { cause } ) ,
990+ ) ,
991+ Effect . orElseSucceed ( ( ) => null ) ,
992+ ) ;
993+ return document === null
994+ ? null
995+ : ( {
996+ kind : "bytes" ,
997+ mimeType : "text/html" ,
998+ bytes : new TextEncoder ( ) . encode ( document . html ) ,
999+ } satisfies ResolvedAsset ) ;
1000+ }
1001+
9261002 if ( claims . kind === "native-app-icon" ) {
9271003 const nativeAppIconResolver = yield * NativeAppIconResolver . NativeAppIconResolver ;
9281004 const iconPath = yield * nativeAppIconResolver . resolve ( claims . app ) ;
0 commit comments