@@ -236,6 +236,8 @@ import { useAtomQueryRunner } from "../../state/use-atom-query-runner";
236236import { readEnvironmentScope , usePreparedConnection } from "../../state/session" ;
237237import { environmentProjects , projectEnvironment } from "../../state/projects" ;
238238import {
239+ agentWorkingDirectories ,
240+ findAgentDirectoryMatch ,
239241 needsWorkspaceBasenameLookup ,
240242 pickProjectLinkMatch ,
241243 pickWorkspaceLinkMatch ,
@@ -256,7 +258,11 @@ import { useModelMentions } from "../../state/model-mentions";
256258import { composerDocumentAttachmentRecord } from "../../lib/composerContext" ;
257259import * as Option from "effect/Option" ;
258260import { appAtomRegistry } from "../../state/atom-registry" ;
259- import { environmentThreadShells , threadEnvironment } from "../../state/threads" ;
261+ import {
262+ environmentThreadDetails ,
263+ environmentThreadShells ,
264+ threadEnvironment ,
265+ } from "../../state/threads" ;
260266import { ThreadWorkflowOpening , useWorkflowOpening } from "./thread-workflow-opening" ;
261267import { useAtomCommand } from "../../state/use-atom-command" ;
262268import { useV2ItemSupport } from "../../state/v2-item-support" ;
@@ -1072,10 +1078,17 @@ function ThreadMediaVisibility(props: { readonly children: ReactNode }) {
10721078 return < ThreadMediaVisibleContext value = { visible } > { props . children } </ ThreadMediaVisibleContext > ;
10731079}
10741080
1081+ /** The reply a link sits in; a file it cites may be where the agent worked before writing it. */
1082+ type MarkdownLinkReply = Pick < OrchestrationV2ProjectedTurnItem , "sourceThreadId" | "sourceItemId" > ;
1083+
10751084interface MarkdownLinkHandlers {
1076- readonly onLinkPress : ( href : string ) => void ;
1085+ readonly onLinkPress : ( href : string , reply ?: MarkdownLinkReply ) => void ;
10771086 readonly fileContextMenu : ( href : string ) => MarkdownFileContextMenu | undefined ;
1078- readonly onFileContextMenuAction : ( href : string , actionId : string ) => void ;
1087+ readonly onFileContextMenuAction : (
1088+ href : string ,
1089+ actionId : string ,
1090+ reply ?: MarkdownLinkReply ,
1091+ ) => void ;
10791092 readonly onImagePress ?: ( href : string ) => void ;
10801093 readonly resolveImageSource ?: MarkdownImageSourceResolver ;
10811094 readonly fileEmbedLabel ?: ( href : string , standalone : boolean ) => string | null ;
@@ -1146,6 +1159,7 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
11461159 readonly environmentId : EnvironmentId ;
11471160 readonly markdownStyles : MarkdownStyleSet ;
11481161 readonly linkHandlers : MarkdownLinkHandlers ;
1162+ readonly reply ?: MarkdownLinkReply | undefined ;
11491163 readonly onUseArtifactTemplate ?: ( ( template : CodexArtifactTemplate ) => void ) | undefined ;
11501164 readonly renderImage : MarkdownImageRenderer ;
11511165 readonly skills ?: ReadonlyArray < SelectableMarkdownSkill > | undefined ;
@@ -1155,6 +1169,9 @@ const AssistantMarkdownContent = memo(function AssistantMarkdownContent(props: {
11551169 const threadIcons = useThreadChipIcons ( props . environmentId , liveMarkdown ) ;
11561170 const linkHandlers : MarkdownLinkHandlers = {
11571171 ...props . linkHandlers ,
1172+ onLinkPress : ( href ) => props . linkHandlers . onLinkPress ( href , props . reply ) ,
1173+ onFileContextMenuAction : ( href , actionId ) =>
1174+ props . linkHandlers . onFileContextMenuAction ( href , actionId , props . reply ) ,
11581175 linkCustomization : ( href ) =>
11591176 withChipIcon ( props . linkHandlers . linkCustomization ?.( href ) , threadIcons . get ( href ) ) ,
11601177 } ;
@@ -2289,6 +2306,7 @@ function renderFeedEntry(
22892306 environmentId = { props . environmentId }
22902307 markdownStyles = { styles }
22912308 linkHandlers = { props . markdownLinkHandlers }
2309+ reply = { message . projectedItem }
22922310 onUseArtifactTemplate = { props . onUseArtifactTemplate }
22932311 renderImage = { props . renderMarkdownImage }
22942312 skills = { props . skills }
@@ -2878,6 +2896,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
28782896 linkPath : string ,
28792897 path : string ,
28802898 signal : AbortSignal ,
2899+ reply : MarkdownLinkReply | undefined ,
28812900 ) : Promise < string > => {
28822901 const relativePath = resolveWorkspaceRelativeFilePath ( props . workspaceRoot , path ) ;
28832902 if (
@@ -2909,6 +2928,15 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29092928 root,
29102929 entries : result . _tag === "Success" ? result . value . entries : [ ] ,
29112930 } ) ) ;
2931+ const metadataOf = ( target : string ) =>
2932+ executeAtomQuery (
2933+ appAtomRegistry ,
2934+ projectEnvironment . fileMetadata ( {
2935+ environmentId : props . environmentId ,
2936+ input : { path : target } ,
2937+ } ) ,
2938+ queryOptions ,
2939+ ) ;
29122940 const lookup = async ( ) => {
29132941 const own = await searchRoot ( workspaceRoot ) ;
29142942 let match = pickWorkspaceLinkMatch ( relativePath , workspaceRoot , own . entries ) ;
@@ -2933,15 +2961,35 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29332961 ? null
29342962 : pickProjectLinkMatch ( relativePath , await Promise . all ( otherRoots . map ( searchRoot ) ) ) ;
29352963 }
2964+ if ( match === null && reply !== undefined && ! signal . aborted ) {
2965+ // No project has the file, so it opens where the agent last worked, as written.
2966+ const agentRoots = agentWorkingDirectories (
2967+ appAtomRegistry . get (
2968+ environmentThreadDetails . quietThreadAtom (
2969+ scopeThreadRef ( props . environmentId , props . threadId ) ,
2970+ ) ,
2971+ ) ?. projection . visibleTurnItems ?? [ ] ,
2972+ reply ,
2973+ ) . filter ( ( root ) => root !== workspaceRoot ) ;
2974+ const agentPaths = agentRoots . map ( ( root ) => resolveWorkspaceFilePath ( root , relativePath ) ) ;
2975+ if ( agentPaths . length === 0 ) return relativePath ;
2976+ const [ original , ...found ] = await Promise . all (
2977+ [ originalPath , ...agentPaths ] . map ( metadataOf ) ,
2978+ ) ;
2979+ if ( original ?. _tag !== "Success" || original . value !== null ) return relativePath ;
2980+ const index = found . findIndex (
2981+ ( result ) => result . _tag === "Success" && result . value ?. kind === "file" ,
2982+ ) ;
2983+ const literal = agentPaths [ index ] ;
2984+ if ( literal !== undefined || ! needsWorkspaceBasenameLookup ( relativePath ) ) {
2985+ return literal ?? relativePath ;
2986+ }
2987+ return (
2988+ ( await findAgentDirectoryMatch ( relativePath , agentRoots , searchRoot ) ) ?? relativePath
2989+ ) ;
2990+ }
29362991 if ( ! match || match === relativePath || signal . aborted ) return relativePath ;
2937- const metadata = await executeAtomQuery (
2938- appAtomRegistry ,
2939- projectEnvironment . fileMetadata ( {
2940- environmentId : props . environmentId ,
2941- input : { path : originalPath } ,
2942- } ) ,
2943- queryOptions ,
2944- ) ;
2992+ const metadata = await metadataOf ( originalPath ) ;
29452993 // Another project's file never replaces a thread-local file the search missed.
29462994 return metadata . _tag === "Success" &&
29472995 ( needsWorkspaceBasenameLookup ( relativePath ) && ! otherProject
@@ -2952,7 +3000,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
29523000 } ;
29533001 return lookup ( ) . catch ( ( ) => relativePath ) ;
29543002 } ;
2955- const onMarkdownLinkPress = ( href : string , imageEmbed = false ) => {
3003+ const onMarkdownLinkPress = ( href : string , imageEmbed = false , reply ?: MarkdownLinkReply ) => {
29563004 markdownLinkRequest . current ?. abort ( ) ;
29573005 const request = new AbortController ( ) ;
29583006 markdownLinkRequest . current = request ;
@@ -3019,7 +3067,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
30193067 if ( presentation . kind === "file" ) {
30203068 const path = resolveFileLinkPath ( props . workspaceRoot , presentation . path ) ;
30213069 if ( path === null ) return ;
3022- void findFileLinkTarget ( presentation . path , path , request . signal ) . then ( ( target ) => {
3070+ void findFileLinkTarget ( presentation . path , path , request . signal , reply ) . then ( ( target ) => {
30233071 if ( request . signal . aborted ) return ;
30243072 void Haptics . selectionAsync ( ) ;
30253073 navigation . navigate ( "ThreadFile" , {
@@ -3058,7 +3106,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
30583106 } ,
30593107 standalone ,
30603108 ) ,
3061- onLinkPress : onMarkdownLinkPress ,
3109+ onLinkPress : ( href , reply ) => onMarkdownLinkPress ( href , false , reply ) ,
30623110 linkCustomization : enrichedThreadLinkCustomization ,
30633111 onImagePress : ( href ) => onMarkdownLinkPress ( href , true ) ,
30643112 resolveImageSource,
@@ -3091,7 +3139,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
30913139 const target = resolveFileChipTarget ( href , props . workspaceRoot ) ;
30923140 return target ? fileChipMenu ( target ) : undefined ;
30933141 } ,
3094- onFileContextMenuAction : ( href , actionId ) => {
3142+ onFileContextMenuAction : ( href , actionId , reply ) => {
30953143 const skill = enrichedSkillReference ( href , props . skills ) ;
30963144 if ( skill ) {
30973145 const instructionsPath = enrichedSkillInstructionsPath ( skill ) ;
@@ -3119,7 +3167,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
31193167 markdownLinkRequest . current ?. abort ( ) ;
31203168 const request = new AbortController ( ) ;
31213169 markdownLinkRequest . current = request ;
3122- void findFileLinkTarget ( presentation . path , path , request . signal ) . then ( ( found ) => {
3170+ void findFileLinkTarget ( presentation . path , path , request . signal , reply ) . then ( ( found ) => {
31233171 if ( request . signal . aborted ) return ;
31243172 const fullPath =
31253173 props . workspaceRoot && ! isAbsolutePath ( found )
@@ -3144,7 +3192,7 @@ export const ThreadFeed = memo(function ThreadFeed(props: ThreadFeedProps) {
31443192 return ;
31453193 }
31463194 case "open-file" :
3147- onMarkdownLinkPress ( href ) ;
3195+ onMarkdownLinkPress ( href , false , reply ) ;
31483196 return ;
31493197 case "save" :
31503198 shareFileChip ( target ) ;
0 commit comments