@@ -41,7 +41,7 @@ import {
4141 dynamicToolTitle ,
4242} from "@t3tools/shared/toolActivity" ;
4343import { getModelSelectionStringOptionValue , modelSelectionsEqual } from "@t3tools/shared/model" ;
44- import { isWindowsAbsolutePath } from "@t3tools/shared/path" ;
44+ import { isUncPath , isWindowsDrivePath } from "@t3tools/shared/path" ;
4545import { resolveSpawnCommand } from "@t3tools/shared/shell" ;
4646import type {
4747 ChatAttachment ,
@@ -1107,6 +1107,32 @@ function parseCodexRetryProgress(
11071107 return { attempt, maxAttempts } ;
11081108}
11091109
1110+ /**
1111+ * Appends a Markdown image for each saved path the text does not already embed.
1112+ * Markdown reads the `\.` in `C:\Users\me\.codex` as an escape, and clients without
1113+ * Windows path handling, such as released mobile apps, would load the wrong file. Drive
1114+ * paths use forward slashes; UNC paths use `file:` URLs, since `//host` reads as a web URL.
1115+ */
1116+ export function appendGeneratedImageEmbeds ( text : string , savedPaths : Iterable < string > ) : string {
1117+ let result = text ;
1118+ for ( const savedPath of savedPaths ) {
1119+ const embedPath = isWindowsDrivePath ( savedPath )
1120+ ? savedPath . replaceAll ( "\\" , "/" )
1121+ : isUncPath ( savedPath )
1122+ ? `file:${ savedPath . replaceAll ( "\\" , "/" ) } `
1123+ : savedPath ;
1124+ // Only an existing image embed counts; a plain path mention or link still needs a preview.
1125+ const embedded = [ savedPath , embedPath ] . some ( ( candidate ) => {
1126+ const escapedPath = candidate . replace ( / [ . * + ? ^ $ { } ( ) | [ \] \\ ] / g, "\\$&" ) ;
1127+ return new RegExp ( `!\\[[^\\]]*\\]\\(<?${ escapedPath } >?[\\s)]` ) . test ( result ) ;
1128+ } ) ;
1129+ if ( ! embedded ) {
1130+ result += `${ result . length > 0 ? "\n\n" : "" }  } >)` ;
1131+ }
1132+ }
1133+ return result ;
1134+ }
1135+
11101136function codexErrorInfoCode ( value : unknown ) : string | null {
11111137 if ( typeof value === "string" ) {
11121138 return value ;
@@ -5002,6 +5028,10 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
50025028 updated . set ( payload . turnId , images ) ;
50035029 return updated ;
50045030 } ) ;
5031+ // Work that outlives turn/completed, such as a backgrounded exec, can finish later.
5032+ if ( settled ) {
5033+ yield * projectGeneratedImages ( context , payload . turnId ) ;
5034+ }
50055035 }
50065036 return ;
50075037 }
@@ -5763,6 +5793,48 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
57635793 } ,
57645794 ) ;
57655795
5796+ // Settled timelines retain the final assistant message, so generated images attach there.
5797+ const projectGeneratedImages = ( context : ActiveCodexTurnContext , nativeTurnId : string ) =>
5798+ Effect . gen ( function * ( ) {
5799+ const images = yield * Ref . modify ( generatedImagesByTurn , ( current ) => {
5800+ const pending = current . get ( nativeTurnId ) ;
5801+ if ( pending === undefined ) return [ undefined , current ] as const ;
5802+ const updated = new Map ( current ) ;
5803+ updated . delete ( nativeTurnId ) ;
5804+ return [ pending , updated ] as const ;
5805+ } ) ;
5806+ const firstImageItemId = images ?. keys ( ) . next ( ) . value ;
5807+ if ( images === undefined || firstImageItemId === undefined ) {
5808+ return ;
5809+ }
5810+ const finalAnswer = ( yield * Ref . get ( lastFinalAnswerByTurn ) ) . get ( nativeTurnId ) ;
5811+ const text = appendGeneratedImageEmbeds ( finalAnswer ?. text ?? "" , images . values ( ) ) ;
5812+ if ( text === ( finalAnswer ?. text ?? "" ) ) {
5813+ return ;
5814+ }
5815+ const message = { id : finalAnswer ?. id ?? firstImageItemId , text } ;
5816+ // A later image on a settled turn appends to this text instead of replacing it.
5817+ yield * Ref . update ( lastFinalAnswerByTurn , ( current ) =>
5818+ new Map ( current ) . set ( nativeTurnId , message ) ,
5819+ ) ;
5820+ const artifacts = yield * buildAgentMessageArtifacts ( context , message , true ) ;
5821+ yield * emitProviderEvent ( {
5822+ type : "node.updated" ,
5823+ driver : CODEX_PROVIDER ,
5824+ node : artifacts . node ,
5825+ } ) ;
5826+ yield * emitProviderEvent ( {
5827+ type : "message.updated" ,
5828+ driver : CODEX_PROVIDER ,
5829+ message : artifacts . message ,
5830+ } ) ;
5831+ yield * emitProviderEvent ( {
5832+ type : "turn_item.updated" ,
5833+ driver : CODEX_PROVIDER ,
5834+ turnItem : artifacts . turnItem ,
5835+ } ) ;
5836+ } ) ;
5837+
57665838 const finalizeCodexTurn = ( input : {
57675839 readonly context : ActiveCodexTurnContext ;
57685840 readonly nativeTurnId : string ;
@@ -5821,6 +5893,7 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
58215893 for ( const [ key , part ] of reasoningParts ) {
58225894 if ( part . turnId === input . nativeTurnId ) reasoningParts . delete ( key ) ;
58235895 }
5896+ yield * projectGeneratedImages ( input . context , input . nativeTurnId ) ;
58245897 const nativeThreadId = input . context . providerThread . nativeThreadRef ?. nativeId ;
58255898 // Codex continues an active goal with another turn, so the run stays
58265899 // open and this turn reads as running until that turn starts.
@@ -6050,50 +6123,6 @@ export const makeCodexAdapterV2 = Effect.fn("makeCodexAdapterV2")(function* (
60506123 ( yield * Ref . get ( interruptingNativeTurns ) ) . has ( payload . turn . id )
60516124 ? "interrupted"
60526125 : 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- }
60976126 yield * finalizeCodexTurn ( {
60986127 context,
60996128 nativeTurnId : payload . turn . id ,
0 commit comments