11import { App } from "../app/app"
2+ import { Fzf } from "../external/fzf"
3+ import { Ripgrep } from "../external/ripgrep"
24import { ListTool } from "../tool/ls"
35import { Filesystem } from "../util/filesystem"
46
@@ -22,8 +24,53 @@ export namespace SystemPrompt {
2224 return result
2325 }
2426
25- export async function environment ( sessionID : string ) {
27+ export async function environment ( ) {
2628 const app = App . info ( )
29+
30+ const tree = async ( ) => {
31+ const files = await Ripgrep . files ( app . path . cwd )
32+ type Node = {
33+ children : Record < string , Node >
34+ }
35+ const root : Node = {
36+ children : { } ,
37+ }
38+ for ( const file of files ) {
39+ const parts = file . split ( "/" )
40+ let node = root
41+ for ( const part of parts ) {
42+ const existing = node . children [ part ]
43+ if ( existing ) {
44+ node = existing
45+ continue
46+ }
47+ node . children [ part ] = {
48+ children : { } ,
49+ }
50+ node = node . children [ part ]
51+ }
52+ }
53+
54+ function render ( path : string [ ] , node : Node ) : string {
55+ const lines : string [ ] = [ ]
56+ const entries = Object . entries ( node . children ) . sort ( ( [ a ] , [ b ] ) =>
57+ a . localeCompare ( b ) ,
58+ )
59+
60+ for ( const [ name , child ] of entries ) {
61+ const currentPath = [ ...path , name ]
62+ const indent = "\t" . repeat ( path . length )
63+ const hasChildren = Object . keys ( child . children ) . length > 0
64+ lines . push ( `${ indent } ${ name } ` + ( hasChildren ? "/" : "" ) )
65+
66+ if ( hasChildren ) lines . push ( render ( currentPath , child ) )
67+ }
68+
69+ return lines . join ( "\n" )
70+ }
71+ return render ( [ ] , root )
72+ }
73+
2774 return [
2875 [
2976 `Here is some useful information about the environment you are running in:` ,
@@ -34,7 +81,7 @@ export namespace SystemPrompt {
3481 ` Today's date: ${ new Date ( ) . toDateString ( ) } ` ,
3582 `</env>` ,
3683 `<project>` ,
37- ` ${ app . git ? await ListTool . execute ( { path : app . path . cwd , ignore : [ ] } , { sessionID : sessionID , messageID : "" , abort : AbortSignal . any ( [ ] ) } ) . then ( ( x ) => x . output ) : "" } ` ,
84+ ` ${ app . git ? await tree ( ) : "" } ` ,
3885 `</project>` ,
3986 ] . join ( "\n" ) ,
4087 ]
0 commit comments