@@ -3,10 +3,16 @@ import * as NodeServices from "@effect/platform-node/NodeServices";
33import * as NodeHttpPlatform from "@effect/platform-node/NodeHttpPlatform" ;
44import * as NodeFSP from "node:fs/promises" ;
55import * as NodeOS from "node:os" ;
6- import { AssetAccessError , AssetPreviewTypeValidationError , ThreadId } from "@t3tools/contracts" ;
6+ import {
7+ AssetAccessError ,
8+ AssetPreviewTypeValidationError ,
9+ ThreadId ,
10+ TurnItemId ,
11+ } from "@t3tools/contracts" ;
712import { PROJECT_FAVICON_FALLBACK_MARKER } from "@t3tools/shared/projectFavicon" ;
813import { describe , expect , it } from "@effect/vitest" ;
914import * as Crypto from "effect/Crypto" ;
15+ import * as DateTime from "effect/DateTime" ;
1016import * as Effect from "effect/Effect" ;
1117import * as FileSystem from "effect/FileSystem" ;
1218import * as Layer from "effect/Layer" ;
@@ -20,6 +26,7 @@ import { vi } from "vite-plus/test";
2026
2127import * as ServerSecretStore from "../auth/ServerSecretStore.ts" ;
2228import * as ServerConfig from "../config.ts" ;
29+ import * as Orchestrator from "../orchestration-v2/Orchestrator.ts" ;
2330import * as ProjectFaviconResolver from "../project/ProjectFaviconResolver.ts" ;
2431import * as T3ProjectFileLoader from "../project/T3ProjectFileLoader.ts" ;
2532import * as WorkspacePaths from "../workspace/WorkspacePaths.ts" ;
@@ -44,8 +51,70 @@ vi.mock("node:os", async (importOriginal) => {
4451const configLayer = ServerConfig . ServerConfig . layerTest ( process . cwd ( ) , {
4552 prefix : "t3-asset-access-test-" ,
4653} ) ;
54+ // A PNG header is enough for the dimension read: signature, then IHDR width and height.
55+ const screenshotPng = new Uint8Array ( 24 ) ;
56+ screenshotPng . set ( [
57+ 0x89 , 0x50 , 0x4e , 0x47 , 0x0d , 0x0a , 0x1a , 0x0a , 0 , 0 , 0 , 13 , 0x49 , 0x48 , 0x44 , 0x52 ,
58+ ] ) ;
59+ new DataView ( screenshotPng . buffer ) . setUint32 ( 16 , 390 ) ;
60+ new DataView ( screenshotPng . buffer ) . setUint32 ( 20 , 844 ) ;
61+ const screenshotItem = {
62+ id : TurnItemId . make ( "tool-screenshot" ) ,
63+ type : "dynamic_tool" as const ,
64+ threadId : ThreadId . make ( "thread-1" ) ,
65+ runId : null ,
66+ nodeId : null ,
67+ providerThreadId : null ,
68+ providerTurnId : null ,
69+ nativeItemRef : null ,
70+ parentItemId : null ,
71+ ordinal : 1 ,
72+ status : "completed" as const ,
73+ title : null ,
74+ toolName : "mcp__t3-code__device_screenshot" ,
75+ input : { deviceId : "phone" } ,
76+ output : {
77+ content : [
78+ {
79+ type : "image" ,
80+ source : {
81+ type : "base64" ,
82+ media_type : "image/png" ,
83+ data : Buffer . from ( screenshotPng ) . toString ( "base64" ) ,
84+ } ,
85+ } ,
86+ ] ,
87+ } ,
88+ startedAt : DateTime . makeUnsafe ( "2026-10-05T00:00:00.000Z" ) ,
89+ completedAt : DateTime . makeUnsafe ( "2026-10-05T00:00:01.000Z" ) ,
90+ updatedAt : DateTime . makeUnsafe ( "2026-10-05T00:00:01.000Z" ) ,
91+ } ;
92+
93+ const oversizedScreenshotItem = {
94+ ...screenshotItem ,
95+ id : TurnItemId . make ( "tool-screenshot-oversized" ) ,
96+ output : {
97+ content : [
98+ {
99+ type : "image" ,
100+ source : { type : "base64" , media_type : "image/png" , data : "A" . repeat ( 14 * 1024 * 1024 ) } ,
101+ } ,
102+ ] ,
103+ } ,
104+ } ;
105+
47106const testLayer = Layer . mergeAll (
48107 NodeHttpPlatform . layer ,
108+ Layer . mock ( Orchestrator . OrchestratorV2 ) ( {
109+ getTurnItem : ( { itemId } ) =>
110+ Effect . succeed (
111+ itemId === screenshotItem . id
112+ ? screenshotItem
113+ : itemId === oversizedScreenshotItem . id
114+ ? oversizedScreenshotItem
115+ : null ,
116+ ) ,
117+ } ) ,
49118 configLayer ,
50119 WorkspacePaths . layer ,
51120 ProjectFaviconResolver . layer . pipe (
@@ -103,6 +172,35 @@ describe("AssetAccess", () => {
103172 ) ;
104173 } ) ;
105174
175+ it . effect ( "serves an image a tool returned inline from the stored item" , ( ) =>
176+ Effect . gen ( function * ( ) {
177+ const resource = {
178+ _tag : "tool-output-image" as const ,
179+ threadId : screenshotItem . threadId ,
180+ itemId : screenshotItem . id ,
181+ index : 0 ,
182+ } ;
183+ const result = yield * issueAssetUrl ( { resource } ) ;
184+ expect ( result . imageDimensions ) . toEqual ( { width : 390 , height : 844 } ) ;
185+ const suffix = result . relativeUrl . slice ( `${ ASSET_ROUTE_PREFIX } /` . length ) ;
186+ const separator = suffix . indexOf ( "/" ) ;
187+ expect ( yield * resolveAsset ( suffix . slice ( 0 , separator ) , suffix . slice ( separator + 1 ) ) ) . toEqual ( {
188+ kind : "bytes" ,
189+ mimeType : "image/png" ,
190+ bytes : Buffer . from ( screenshotPng ) ,
191+ } ) ;
192+ const missing = yield * issueAssetUrl ( { resource : { ...resource , index : 1 } } ) . pipe (
193+ Effect . flip ,
194+ ) ;
195+ expect ( missing . _tag ) . toBe ( "AssetWorkspaceAssetNotFoundError" ) ;
196+ // Larger than a provider turn accepts, so it is never decoded.
197+ const oversized = yield * issueAssetUrl ( {
198+ resource : { ...resource , itemId : oversizedScreenshotItem . id } ,
199+ } ) . pipe ( Effect . flip ) ;
200+ expect ( oversized . _tag ) . toBe ( "AssetWorkspaceAssetNotFoundError" ) ;
201+ } ) . pipe ( Effect . provide ( testLayer ) ) ,
202+ ) ;
203+
106204 it . effect ( "issues exact URLs for media and browser documents outside the workspace" , ( ) =>
107205 Effect . gen ( function * ( ) {
108206 const fs = yield * FileSystem . FileSystem ;
0 commit comments