@@ -53,9 +53,25 @@ function textPartValue(parts: Part[]) {
5353 * Extract prompt content from message parts for restoring into the prompt input.
5454 * This is used by undo to restore the original user prompt.
5555 */
56- export function extractPromptFromParts ( parts : Part [ ] ) : Prompt {
56+ export function extractPromptFromParts ( parts : Part [ ] , opts ?: { directory ?: string } ) : Prompt {
5757 const textPart = textPartValue ( parts )
5858 const text = textPart ?. text ?? ""
59+ const directory = opts ?. directory
60+
61+ const toRelative = ( path : string ) => {
62+ if ( ! directory ) return path
63+
64+ const prefix = directory . endsWith ( "/" ) ? directory : directory + "/"
65+ if ( path . startsWith ( prefix ) ) return path . slice ( prefix . length )
66+
67+ if ( path . startsWith ( directory ) ) {
68+ const next = path . slice ( directory . length )
69+ if ( next . startsWith ( "/" ) ) return next . slice ( 1 )
70+ return next
71+ }
72+
73+ return path
74+ }
5975
6076 const inline : Inline [ ] = [ ]
6177 const images : ImageAttachmentPart [ ] = [ ]
@@ -78,7 +94,7 @@ export function extractPromptFromParts(parts: Part[]): Prompt {
7894 start,
7995 end,
8096 value,
81- path,
97+ path : toRelative ( path ) ,
8298 selection : selectionFromFileUrl ( filePart . url ) ,
8399 } )
84100 continue
@@ -158,20 +174,21 @@ export function extractPromptFromParts(parts: Part[]): Prompt {
158174
159175 for ( const item of inline ) {
160176 if ( item . start < 0 || item . end < item . start ) continue
161- if ( item . end > text . length ) continue
162- if ( item . start < cursor ) continue
163177
164- pushText ( text . slice ( cursor , item . start ) )
178+ const expected = item . value
179+ if ( ! expected ) continue
165180
166- if ( item . type === "file" ) {
167- pushFile ( item )
168- }
181+ const mismatch = item . end > text . length || item . start < cursor || text . slice ( item . start , item . end ) !== expected
182+ const start = mismatch ? text . indexOf ( expected , cursor ) : item . start
183+ if ( start === - 1 ) continue
184+ const end = mismatch ? start + expected . length : item . end
169185
170- if ( item . type === "agent" ) {
171- pushAgent ( item )
172- }
186+ pushText ( text . slice ( cursor , start ) )
187+
188+ if ( item . type === "file" ) pushFile ( item )
189+ if ( item . type === "agent" ) pushAgent ( item )
173190
174- cursor = item . end
191+ cursor = end
175192 }
176193
177194 pushText ( text . slice ( cursor ) )
0 commit comments