@@ -3,47 +3,14 @@ import { TextAttributes } from '@opentui/core'
33import { DiffViewer } from './diff-viewer'
44import { defineToolComponent } from './types'
55import { useTheme } from '../../hooks/use-theme'
6+ import {
7+ extractDiff ,
8+ extractFilePath ,
9+ isCreateFile ,
10+ } from '../../utils/implementor-helpers'
611
712import type { ToolRenderConfig } from './types'
813
9- function extractValueForKey ( output : string , key : string ) : string | null {
10- if ( ! output ) return null
11- const lines = output . split ( '\n' )
12- for ( let i = 0 ; i < lines . length ; i ++ ) {
13- const line = lines [ i ]
14- const match = line . match ( / ^ \s * ( [ A - Z a - z 0 - 9 _ ] + ) : \s * ( .* ) $ / )
15- if ( match && match [ 1 ] === key ) {
16- const rest = match [ 2 ]
17- if ( rest . trim ( ) . startsWith ( '|' ) ) {
18- const baseIndent = lines [ i + 1 ] ?. match ( / ^ \s * / ) ?. [ 0 ] . length ?? 0
19- const acc : string [ ] = [ ]
20- for ( let j = i + 1 ; j < lines . length ; j ++ ) {
21- const l = lines [ j ]
22- const indent = l . match ( / ^ \s * / ) ?. [ 0 ] . length ?? 0
23- if ( l . trim ( ) . length === 0 ) {
24- acc . push ( '' )
25- continue
26- }
27- if ( indent < baseIndent ) break
28- acc . push ( l . slice ( baseIndent ) )
29- }
30- return acc . join ( '\n' )
31- } else {
32- let val = rest . trim ( )
33- if ( val . startsWith ( '"' ) && val . endsWith ( '"' ) ) {
34- val = val . slice ( 1 , - 1 )
35- }
36- return val
37- }
38- }
39- }
40- return null
41- }
42-
43- function isCreatedFileMessage ( message : string | null ) : boolean {
44- return message === 'Created file successfully.' || message === 'Created new file'
45- }
46-
4714interface EditHeaderProps {
4815 name : string
4916 filePath : string | null
@@ -70,14 +37,13 @@ interface EditBodyProps {
7037 name : string
7138 filePath : string | null
7239 diffText : string
73- isCreate : boolean
7440}
7541
76- const EditBody = ( { name, filePath, diffText, isCreate } : EditBodyProps ) => {
42+ const EditBody = ( { name, filePath, diffText } : EditBodyProps ) => {
7743 return (
7844 < box style = { { flexDirection : 'column' , gap : 0 , width : '100%' } } >
7945 < EditHeader name = { name } filePath = { filePath } />
80- { ! isCreate && diffText . length > 0 && (
46+ { diffText . length > 0 && (
8147 < box style = { { paddingLeft : 2 , width : '100%' } } >
8248 < DiffViewer diffText = { diffText } />
8349 </ box >
@@ -90,26 +56,16 @@ export const StrReplaceComponent = defineToolComponent({
9056 toolName : 'str_replace' ,
9157
9258 render ( toolBlock ) : ToolRenderConfig {
93- const outputStr =
94- typeof toolBlock . output === 'string' ? toolBlock . output : ''
95- const diff =
96- extractValueForKey ( outputStr , 'unifiedDiff' ) ||
97- extractValueForKey ( outputStr , 'patch' )
98- const filePath =
99- extractValueForKey ( outputStr , 'file' ) ||
100- ( typeof ( toolBlock . input as any ) ?. path === 'string'
101- ? ( toolBlock . input as any ) . path
102- : null )
103- const message = extractValueForKey ( outputStr , 'message' )
104- const isCreate = isCreatedFileMessage ( message )
59+ const diff = extractDiff ( toolBlock )
60+ const filePath = extractFilePath ( toolBlock )
61+ const isCreate = isCreateFile ( toolBlock )
10562
10663 return {
10764 content : (
10865 < EditBody
10966 name = { isCreate ? 'Create' : 'Edit' }
11067 filePath = { filePath }
11168 diffText = { diff ?? '' }
112- isCreate = { isCreate }
11369 />
11470 ) ,
11571 }
0 commit comments