@@ -27,6 +27,7 @@ import * as Layer from "effect/Layer";
2727import * as Stream from "effect/Stream" ;
2828import * as TestClock from "effect/testing/TestClock" ;
2929
30+ import { toolOutputImages } from "@t3tools/shared/toolOutput" ;
3031import * as SqlitePersistence from "../persistence/Sqlite.ts" ;
3132import * as EventSink from "./EventSink.ts" ;
3233import * as EventStore from "./EventStore.ts" ;
@@ -1169,6 +1170,97 @@ layer("ProviderEventIngestorV2", (it) => {
11691170 } ) ,
11701171 ) ;
11711172
1173+ it . effect ( "stores tool image bytes only where a tool-output-image asset serves them" , ( ) =>
1174+ Effect . gen ( function * ( ) {
1175+ const now = yield * DateTime . now ;
1176+ const eventSink = yield * EventSink . EventSinkV2 ;
1177+ const eventStore = yield * EventStore . EventStoreV2 ;
1178+ const projectionStore = yield * ProjectionStore . ProjectionStoreV2 ;
1179+ const ingestor = yield * ProviderEventIngestor . ProviderEventIngestorV2 ;
1180+ const idAllocator = yield * IdAllocator . IdAllocatorV2 ;
1181+ const threadEvent = yield * threadCreatedEvent ( now ) ;
1182+ const providerSessionId = yield * idAllocator . allocate . providerSession ( {
1183+ providerInstanceId : modelSelection . instanceId ,
1184+ threadId : threadEvent . threadId ,
1185+ } ) ;
1186+ const readBase64 = Buffer . alloc ( 30_000 , 7 ) . toString ( "base64" ) ;
1187+ const screenshotBase64 = Buffer . alloc ( 20_000 , 9 ) . toString ( "base64" ) ;
1188+ const toolItem = (
1189+ id : string ,
1190+ ordinal : number ,
1191+ toolName : string ,
1192+ output : unknown ,
1193+ ) : OrchestrationV2TurnItem => ( {
1194+ id : TurnItemId . make ( id ) ,
1195+ threadId : threadEvent . threadId ,
1196+ runId : null ,
1197+ nodeId : null ,
1198+ providerThreadId : null ,
1199+ providerTurnId : null ,
1200+ nativeItemRef : null ,
1201+ parentItemId : null ,
1202+ ordinal,
1203+ status : "completed" ,
1204+ title : toolName ,
1205+ startedAt : now ,
1206+ completedAt : now ,
1207+ updatedAt : now ,
1208+ type : "dynamic_tool" ,
1209+ toolName,
1210+ input : { } ,
1211+ output,
1212+ } ) ;
1213+ const read = toolItem ( "turn-item:read-image" , 1 , "Read" , {
1214+ type : "image" ,
1215+ file : { base64 : readBase64 , type : "image/png" , originalSize : 30_000 } ,
1216+ } ) ;
1217+ const screenshot = toolItem ( "turn-item:screenshot" , 2 , "mcp__t3-code__device_screenshot" , {
1218+ content : [
1219+ {
1220+ type : "image" ,
1221+ source : { type : "base64" , media_type : "image/png" , data : screenshotBase64 } ,
1222+ } ,
1223+ ] ,
1224+ } ) ;
1225+
1226+ yield * eventSink . write ( { events : [ threadEvent ] } ) ;
1227+ for ( const turnItem of [ read , screenshot ] ) {
1228+ yield * ingestor . ingestNormalized ( {
1229+ providerSessionId,
1230+ providerInstanceId : modelSelection . instanceId ,
1231+ threadId : threadEvent . threadId ,
1232+ event : { type : "turn_item.updated" , driver : CODEX_DRIVER , turnItem } ,
1233+ } ) ;
1234+ }
1235+
1236+ const storedEvents = yield * eventStore
1237+ . read ( { threadId : threadEvent . threadId , eventType : "turn-item.updated" } )
1238+ . pipe ( Stream . runCollect ) ;
1239+ const storedJson = JSON . stringify ( Array . from ( storedEvents , ( stored ) => stored . event ) ) ;
1240+ const projectedRead = yield * projectionStore . getTurnItem ( {
1241+ threadId : threadEvent . threadId ,
1242+ itemId : read . id ,
1243+ } ) ;
1244+ const projectedScreenshot = yield * projectionStore . getTurnItem ( {
1245+ threadId : threadEvent . threadId ,
1246+ itemId : screenshot . id ,
1247+ } ) ;
1248+
1249+ assert . equal ( storedJson . includes ( readBase64 ) , false ) ;
1250+ assert . equal ( storedJson . includes ( screenshotBase64 ) , true ) ;
1251+ assert . deepEqual ( projectedRead ?. type === "dynamic_tool" ? projectedRead . output : null , {
1252+ type : "image" ,
1253+ file : { type : "image/png" , originalSize : 30_000 , sizeBytes : 30_000 } ,
1254+ } ) ;
1255+ assert . deepEqual (
1256+ toolOutputImages (
1257+ projectedScreenshot ?. type === "dynamic_tool" ? projectedScreenshot . output : null ,
1258+ ) ,
1259+ [ { mimeType : "image/png" , data : screenshotBase64 } ] ,
1260+ ) ;
1261+ } ) ,
1262+ ) ;
1263+
11721264 it . effect ( "routes provider-owned child artifacts to their child app thread" , ( ) =>
11731265 Effect . gen ( function * ( ) {
11741266 const now = yield * DateTime . now ;
0 commit comments