33// found in the LICENSE file.
44
55import * as Host from '../../../core/host/host.js' ;
6- import * as TextUtils from '../../../models/text_utils/text_utils.js' ;
76import type * as Workspace from '../../../models/workspace/workspace.js' ;
7+ import { AgentProject } from '../AgentProject.js' ;
88import { debugLog } from '../debug.js' ;
99
1010import {
@@ -18,24 +18,8 @@ import {
1818 ResponseType ,
1919} from './AiAgent.js' ;
2020
21- function getFiles ( project : Workspace . Workspace . Project ) :
22- { files : string [ ] , map : Map < string , Workspace . UISourceCode . UISourceCode > } {
23- const files = [ ] ;
24- const map = new Map ( ) ;
25- for ( const uiSourceCode of project . uiSourceCodes ( ) ) {
26- let path = uiSourceCode . fullDisplayName ( ) ;
27- const idx = path . indexOf ( '/' ) ;
28- if ( idx !== - 1 ) {
29- path = path . substring ( idx + 1 ) ;
30- }
31- files . push ( path ) ;
32- map . set ( path , uiSourceCode ) ;
33- }
34- return { files, map} ;
35- }
36-
3721export class PatchAgent extends AiAgent < Workspace . Workspace . Project > {
38- #project: Workspace . Workspace . Project ;
22+ #project: AgentProject ;
3923 #fileUpdateAgent: FileUpdateAgent ;
4024 #changeSummary = '' ;
4125
@@ -63,7 +47,7 @@ export class PatchAgent extends AiAgent<Workspace.Workspace.Project> {
6347
6448 constructor ( opts : BaseAgentOptions & { fileUpdateAgent ?: FileUpdateAgent , project : Workspace . Workspace . Project } ) {
6549 super ( opts ) ;
66- this . #project = opts . project ;
50+ this . #project = new AgentProject ( opts . project ) ;
6751 this . #fileUpdateAgent = opts . fileUpdateAgent ?? new FileUpdateAgent ( opts ) ;
6852 this . declareFunction < Record < never , unknown > > ( 'listFiles' , {
6953 description : 'Returns a list of all files in the project.' ,
@@ -74,16 +58,9 @@ export class PatchAgent extends AiAgent<Workspace.Workspace.Project> {
7458 properties : { } ,
7559 } ,
7660 handler : async ( ) => {
77- if ( ! this . #project) {
78- return {
79- error : 'No project available' ,
80- } ;
81- }
82- const project = this . #project;
83- const { files} = getFiles ( project ) ;
8461 return {
8562 result : {
86- files,
63+ files : this . #project . getFiles ( ) ,
8764 }
8865 } ;
8966 } ,
@@ -119,33 +96,9 @@ export class PatchAgent extends AiAgent<Workspace.Workspace.Project> {
11996 } ,
12097 } ,
12198 handler : async params => {
122- if ( ! this . #project) {
123- return {
124- error : 'No project available' ,
125- } ;
126- }
127- const project = this . #project;
128- const { map} = getFiles ( project ) ;
129- const matches = [ ] ;
130- for ( const [ filepath , file ] of map . entries ( ) ) {
131- await file . requestContentData ( ) ;
132- debugLog ( 'searching in' , filepath , 'for' , params . query ) ;
133- const content = file . isDirty ( ) ? file . workingCopyContentData ( ) : await file . requestContentData ( ) ;
134- const results = TextUtils . TextUtils . performSearchInContentData (
135- content , params . query , params . caseSensitive ?? true , params . isRegex ?? false ) ;
136- for ( const result of results ) {
137- debugLog ( 'matches in' , filepath ) ;
138- matches . push ( {
139- filepath,
140- lineNumber : result . lineNumber ,
141- columnNumber : result . columnNumber ,
142- matchLength : result . matchLength
143- } ) ;
144- }
145- }
14699 return {
147100 result : {
148- matches,
101+ matches : await this . #project . searchFiles ( params . query , params . caseSensitive , params . isRegex ) ,
149102 }
150103 } ;
151104 } ,
@@ -170,17 +123,10 @@ export class PatchAgent extends AiAgent<Workspace.Workspace.Project> {
170123 } ,
171124 handler : async args => {
172125 debugLog ( 'updateFiles' , args . files ) ;
173- if ( ! this . #project) {
174- return {
175- error : 'No project available' ,
176- } ;
177- }
178- const project = this . #project;
179- const { map} = getFiles ( project ) ;
180126 for ( const file of args . files . slice ( 0 , 3 ) ) {
181127 debugLog ( 'updating' , file ) ;
182- const uiSourceCode = map . get ( file ) ;
183- if ( ! uiSourceCode ) {
128+ const content = this . #project . readFile ( file ) ;
129+ if ( content === undefined ) {
184130 debugLog ( file , 'not found' ) ;
185131 continue ;
186132 }
@@ -194,7 +140,7 @@ Following '===' I provide the source code file. Update the file to apply the sam
194140CRITICAL: Output the entire file with changes without any other modifications! DO NOT USE MARKDOWN.
195141
196142===
197- ${ uiSourceCode . workingCopyContentData ( ) . text }
143+ ${ content }
198144` ;
199145 let response ;
200146 for await ( response of this . #fileUpdateAgent. run ( prompt , { selected : null } ) ) {
@@ -206,7 +152,7 @@ ${uiSourceCode.workingCopyContentData().text}
206152 continue ;
207153 }
208154 const updated = response . text ;
209- uiSourceCode . setWorkingCopy ( updated ) ;
155+ this . #project . writeFile ( file , updated ) ;
210156 debugLog ( 'updated' , updated ) ;
211157 }
212158 return {
0 commit comments