@@ -41,6 +41,7 @@ import {
4141 dynamicToolTitle ,
4242} from "@t3tools/shared/toolActivity" ;
4343import { getModelSelectionStringOptionValue , modelSelectionsEqual } from "@t3tools/shared/model" ;
44+ import { isWindowsAbsolutePath } from "@t3tools/shared/path" ;
4445import { resolveSpawnCommand } from "@t3tools/shared/shell" ;
4546import type {
4647 ChatAttachment ,
@@ -1890,6 +1891,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
18901891 const offeredContinuationItemsByTurn = yield * Ref . make ( new Map < string , Set < string > > ( ) ) ;
18911892 const finalAnswerItemIdsByTurn = yield * Ref . make ( new Map < string , Set < string > > ( ) ) ;
18921893 const completedFinalAnswerTextsByTurn = yield * Ref . make ( new Map < string , Set < string > > ( ) ) ;
1894+ const lastFinalAnswerByTurn = yield * Ref . make (
1895+ new Map < string , { readonly id : string ; readonly text : string } > ( ) ,
1896+ ) ;
1897+ const generatedImagesByTurn = yield * Ref . make ( new Map < string , Map < string , string > > ( ) ) ;
18931898 // Native completion and the interrupt timeout share one finalization
18941899 // path. Serialize the race so only one can publish terminal events.
18951900 const turnTerminalizationPermit = yield * Semaphore . make ( 1 ) ;
@@ -2215,6 +2220,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
22152220 updated . delete ( nativeTurnId ) ;
22162221 return updated ;
22172222 } ) ;
2223+ yield * Ref . update ( lastFinalAnswerByTurn , ( current ) => {
2224+ if ( ! current . has ( nativeTurnId ) ) {
2225+ return current ;
2226+ }
2227+ const updated = new Map ( current ) ;
2228+ updated . delete ( nativeTurnId ) ;
2229+ return updated ;
2230+ } ) ;
2231+ yield * Ref . update ( generatedImagesByTurn , ( current ) => {
2232+ if ( ! current . has ( nativeTurnId ) ) {
2233+ return current ;
2234+ }
2235+ const updated = new Map ( current ) ;
2236+ updated . delete ( nativeTurnId ) ;
2237+ return updated ;
2238+ } ) ;
22182239 yield * Ref . update ( finalAnswerItemIdsByTurn , ( current ) => {
22192240 if ( ! current . has ( nativeTurnId ) ) {
22202241 return current ;
@@ -3416,6 +3437,9 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
34163437 turnItem : artifacts . turnItem ,
34173438 } ) ;
34183439 if ( finalAnswerItem && update . completed ) {
3440+ yield * Ref . update ( lastFinalAnswerByTurn , ( current ) =>
3441+ new Map ( current ) . set ( update . turnId , { id : update . itemId , text : update . text } ) ,
3442+ ) ;
34193443 yield * Ref . update ( completedFinalAnswerTextsByTurn , ( current ) => {
34203444 const updated = new Map ( current ) ;
34213445 const texts = new Set ( updated . get ( update . turnId ) ?? [ ] ) ;
@@ -4968,6 +4992,20 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
49684992 return ;
49694993 }
49704994
4995+ if ( payload . item . type === "imageGeneration" ) {
4996+ const { id, savedPath, status, failure } = payload . item ;
4997+ if ( savedPath ?. trim ( ) && status !== "failed" && failure == null ) {
4998+ yield * Ref . update ( generatedImagesByTurn , ( current ) => {
4999+ const updated = new Map ( current ) ;
5000+ const images = new Map ( updated . get ( payload . turnId ) ) ;
5001+ images . set ( id , savedPath ) ;
5002+ updated . set ( payload . turnId , images ) ;
5003+ return updated ;
5004+ } ) ;
5005+ }
5006+ return ;
5007+ }
5008+
49715009 if ( payload . item . type !== "agentMessage" ) {
49725010 return ;
49735011 }
@@ -5971,6 +6009,22 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59716009 updated . delete ( input . nativeTurnId ) ;
59726010 return updated ;
59736011 } ) ;
6012+ yield * Ref . update ( lastFinalAnswerByTurn , ( current ) => {
6013+ if ( ! current . has ( input . nativeTurnId ) ) {
6014+ return current ;
6015+ }
6016+ const updated = new Map ( current ) ;
6017+ updated . delete ( input . nativeTurnId ) ;
6018+ return updated ;
6019+ } ) ;
6020+ yield * Ref . update ( generatedImagesByTurn , ( current ) => {
6021+ if ( ! current . has ( input . nativeTurnId ) ) {
6022+ return current ;
6023+ }
6024+ const updated = new Map ( current ) ;
6025+ updated . delete ( input . nativeTurnId ) ;
6026+ return updated ;
6027+ } ) ;
59746028 yield * Ref . update ( finalAnswerItemIdsByTurn , ( current ) => {
59756029 if ( ! current . has ( input . nativeTurnId ) ) {
59766030 return current ;
@@ -5996,6 +6050,50 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
59966050 ( yield * Ref . get ( interruptingNativeTurns ) ) . has ( payload . turn . id )
59976051 ? "interrupted"
59986052 : nativeStatus ;
6053+ const images = ( yield * Ref . get ( generatedImagesByTurn ) ) . get ( payload . turn . id ) ;
6054+ const firstImageItemId = images ?. keys ( ) . next ( ) . value ;
6055+ if ( images !== undefined && firstImageItemId !== undefined ) {
6056+ const finalAnswer = ( yield * Ref . get ( lastFinalAnswerByTurn ) ) . get ( payload . turn . id ) ;
6057+ let text = finalAnswer ?. text ?? "" ;
6058+ for ( const savedPath of images . values ( ) ) {
6059+ // Markdown reads the `\.` in `C:\Users\me\.codex` as an escape. Clients without
6060+ // Windows path handling, such as released mobile apps, would load the wrong file.
6061+ const embedPath = isWindowsAbsolutePath ( savedPath )
6062+ ? savedPath . replaceAll ( "\\" , "/" )
6063+ : savedPath ;
6064+ // Only an existing image embed counts; a plain path mention or link still needs a preview.
6065+ const embedded = [ savedPath , embedPath ] . some ( ( candidate ) => {
6066+ const escapedPath = candidate . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
6067+ return new RegExp ( `!\\[[^\\]]*\\]\\(<?${ escapedPath } >?[\\s)]` ) . test ( text ) ;
6068+ } ) ;
6069+ if ( ! embedded ) {
6070+ text += `${ text . length > 0 ? "\n\n" : "" }  } >)` ;
6071+ }
6072+ }
6073+ // Settled timelines retain the final assistant message, so attach images there.
6074+ if ( text !== ( finalAnswer ?. text ?? "" ) ) {
6075+ const artifacts = yield * buildAgentMessageArtifacts (
6076+ context ,
6077+ { id : finalAnswer ?. id ?? firstImageItemId , text } ,
6078+ true ,
6079+ ) ;
6080+ yield * emitProviderEvent ( {
6081+ type : "node.updated" ,
6082+ driver : CODEX_PROVIDER ,
6083+ node : artifacts . node ,
6084+ } ) ;
6085+ yield * emitProviderEvent ( {
6086+ type : "message.updated" ,
6087+ driver : CODEX_PROVIDER ,
6088+ message : artifacts . message ,
6089+ } ) ;
6090+ yield * emitProviderEvent ( {
6091+ type : "turn_item.updated" ,
6092+ driver : CODEX_PROVIDER ,
6093+ turnItem : artifacts . turnItem ,
6094+ } ) ;
6095+ }
6096+ }
59996097 yield * finalizeCodexTurn ( {
60006098 context,
60016099 nativeTurnId : payload . turn . id ,
0 commit comments