@@ -11,6 +11,9 @@ import { Filesystem } from "../util/filesystem"
1111import { Instance } from "../project/instance"
1212import { Agent } from "../agent/agent"
1313
14+ const MAX_DIAGNOSTICS_PER_FILE = 20
15+ const MAX_PROJECT_DIAGNOSTICS_FILES = 5
16+
1417export const WriteTool = Tool . define ( "write" , {
1518 description : DESCRIPTION ,
1619 parameters : z . object ( {
@@ -77,13 +80,20 @@ export const WriteTool = Tool.define("write", {
7780 let output = ""
7881 await LSP . touchFile ( filepath , true )
7982 const diagnostics = await LSP . diagnostics ( )
83+ let projectDiagnosticsCount = 0
8084 for ( const [ file , issues ] of Object . entries ( diagnostics ) ) {
8185 if ( issues . length === 0 ) continue
86+ const sorted = issues . toSorted ( ( a , b ) => ( a . severity ?? 4 ) - ( b . severity ?? 4 ) )
87+ const limited = sorted . slice ( 0 , MAX_DIAGNOSTICS_PER_FILE )
88+ const suffix =
89+ issues . length > MAX_DIAGNOSTICS_PER_FILE ? `\n... and ${ issues . length - MAX_DIAGNOSTICS_PER_FILE } more` : ""
8290 if ( file === filepath ) {
83- output += `\nThis file has errors, please fix\n<file_diagnostics>\n${ issues . map ( LSP . Diagnostic . pretty ) . join ( "\n" ) } \n</file_diagnostics>\n`
91+ output += `\nThis file has errors, please fix\n<file_diagnostics>\n${ limited . map ( LSP . Diagnostic . pretty ) . join ( "\n" ) } ${ suffix } \n</file_diagnostics>\n`
8492 continue
8593 }
86- output += `\n<project_diagnostics>\n${ file } \n${ issues . map ( LSP . Diagnostic . pretty ) . join ( "\n" ) } \n</project_diagnostics>\n`
94+ if ( projectDiagnosticsCount >= MAX_PROJECT_DIAGNOSTICS_FILES ) continue
95+ projectDiagnosticsCount ++
96+ output += `\n<project_diagnostics>\n${ file } \n${ limited . map ( LSP . Diagnostic . pretty ) . join ( "\n" ) } ${ suffix } \n</project_diagnostics>\n`
8797 }
8898
8999 return {
0 commit comments