@@ -21,8 +21,6 @@ const FAST_APPLY_TIMEOUT = parseInt(process.env.FAST_APPLY_TIMEOUT || "30000", 1
2121const FAST_APPLY_TEMPERATURE = parseFloat ( process . env . FAST_APPLY_TEMPERATURE || "0.05" )
2222const FAST_APPLY_MAX_TOKENS = parseInt ( process . env . FAST_APPLY_MAX_TOKENS || "8000" , 10 )
2323
24- const PLUGIN_VERSION = "2.0.0"
25-
2624const FAST_APPLY_SYSTEM_PROMPT = "Merge code edits into original files. Preserve structure, indentation, and comments exactly."
2725
2826const FAST_APPLY_USER_PROMPT = `Task: {instruction}
@@ -177,6 +175,74 @@ function countChanges(diff: string): { added: number; removed: number } {
177175 return { added, removed }
178176}
179177
178+ function formatTokenCount ( tokens : number ) : string {
179+ if ( tokens >= 1000 ) {
180+ return `${ ( tokens / 1000 ) . toFixed ( 1 ) } K` . replace ( ".0K" , "K" )
181+ }
182+ return tokens . toString ( )
183+ }
184+
185+ function shortenPath ( filePath : string , workingDir : string ) : string {
186+ if ( filePath . startsWith ( workingDir + "/" ) ) {
187+ return filePath . slice ( workingDir . length + 1 )
188+ }
189+ if ( filePath === workingDir ) {
190+ return "."
191+ }
192+ return filePath
193+ }
194+
195+ function truncate ( str : string , maxLen : number = 80 ) : string {
196+ if ( str . length <= maxLen ) return str
197+ return str . slice ( 0 , maxLen - 3 ) + "..."
198+ }
199+
200+ function estimateTokens ( text : string ) : number {
201+ return Math . ceil ( text . length / 4 )
202+ }
203+
204+ function formatFastApplyResult (
205+ filePath : string ,
206+ workingDir : string ,
207+ insertions : number ,
208+ deletions : number ,
209+ diffPreview : string ,
210+ modifiedTokens : number
211+ ) : string {
212+ const shortPath = shortenPath ( filePath , workingDir )
213+ const tokenStr = formatTokenCount ( modifiedTokens )
214+
215+ const diffLines = diffPreview . split ( "\n" )
216+ const previewLines = diffLines . slice ( 0 , 10 )
217+ const truncatedDiff = previewLines . join ( "\n" )
218+ const hasMore = diffLines . length > 10
219+
220+ const lines = [
221+ "✓ Fast Apply complete" ,
222+ "" ,
223+ "Applied changes:" ,
224+ `→ ${ shortPath } ` ,
225+ ` +${ insertions } lines, -${ deletions } lines (~${ tokenStr } tokens modified)` ,
226+ "" ,
227+ "Diff preview (first 10 lines):" ,
228+ truncatedDiff + ( hasMore ? "\n..." : "" )
229+ ]
230+
231+ return lines . join ( "\n" )
232+ }
233+
234+ function formatErrorOutput ( error : string , filePath : string , workingDir : string ) : string {
235+ const shortPath = shortenPath ( filePath , workingDir )
236+ return [
237+ "✗ Fast Apply failed" ,
238+ "" ,
239+ `→ ${ shortPath } ` ,
240+ ` Error: ${ truncate ( error , 100 ) } ` ,
241+ "" ,
242+ "Fallback: Use native 'edit' tool with exact string matching"
243+ ] . join ( "\n" )
244+ }
245+
180246/**
181247 * Call OpenAI's Fast Apply API to merge code edits
182248 */
@@ -353,42 +419,35 @@ write({
353419 )
354420
355421 if ( ! result . success || ! result . content ) {
356- // Return error with suggestion to use native edit
357- return `OpenAI Fast Apply API failed: ${ result . error }
358-
359- Suggestion: Try using the native 'edit' tool instead with exact string replacement.
360- The edit tool requires matching the exact text in the file.`
422+ return formatErrorOutput ( result . error || "Unknown error" , target_filepath , directory )
361423 }
362424
363425 const mergedCode = result . content
364426
365- // Write the merged result
366427 try {
367428 await writeFile ( filepath , mergedCode , "utf-8" )
368429 } catch ( err ) {
369430 const error = err as Error
370- return `Error writing file ${ target_filepath } : ${ error . message } `
431+ return formatErrorOutput ( error . message , target_filepath , directory )
371432 }
372433
373- // Generate unified diff
374434 const diff = generateUnifiedDiff (
375435 target_filepath ,
376436 originalCode ,
377437 mergedCode
378438 )
379439
380- // Calculate change stats
381440 const { added, removed } = countChanges ( diff )
382- const originalLines = originalCode . split ( "\n" ) . length
383- const mergedLines = mergedCode . split ( "\n" ) . length
384-
385- return `Applied edit to ${ target_filepath }
441+ const modifiedTokens = estimateTokens ( diff )
386442
387- +${ added } -${ removed } lines | ${ originalLines } -> ${ mergedLines } total
388-
389- \`\`\`diff
390- ${ diff . slice ( 0 , 3000 ) } ${ diff . length > 3000 ? "\n... (truncated)" : "" }
391- \`\`\``
443+ return formatFastApplyResult (
444+ target_filepath ,
445+ directory ,
446+ added ,
447+ removed ,
448+ diff ,
449+ modifiedTokens
450+ )
392451 } ,
393452 } ) ,
394453 } ,
0 commit comments