1
1
import { type FC , useCallback , useEffect , useRef , useState } from 'react'
2
2
import { useQuery } from '@tanstack/react-query'
3
- import { type FileInfoTreeItem , travelTree , travelTreeDeepFirst } from '@nicepkg/gpt-runner-shared/common'
3
+ import { travelTree , travelTreeDeepFirst } from '@nicepkg/gpt-runner-shared/common'
4
4
import clsx from 'clsx'
5
5
import { VSCodeCheckbox , VSCodeLink } from '@vscode/webview-ui-toolkit/react'
6
6
import type { SidebarProps } from '../../components/sidebar'
7
7
import { Sidebar } from '../../components/sidebar'
8
8
import { ErrorView } from '../../components/error-view'
9
9
import { fetchCommonFilesTree } from '../../networks/common-files'
10
- import type { TreeItemBaseState , TreeItemProps , TreeItemState } from '../../components/tree-item'
10
+ import type { TreeItemProps , TreeItemState } from '../../components/tree-item'
11
11
import { Icon } from '../../components/icon'
12
12
import { IconButton } from '../../components/icon-button'
13
- import { formatNumWithK } from '../../helpers/utils'
13
+ import { countTokenQuick , formatNumWithK } from '../../helpers/utils'
14
14
import { useGlobalStore } from '../../store/zustand/global'
15
+ import type { FileInfoSidebarTreeItem , FileSidebarTreeItem } from '../../store/zustand/global/file-tree.slice'
15
16
import { FileTreeItemRightWrapper , FileTreeSidebarHighlight , FileTreeSidebarUnderSearchWrapper } from './chat.styles'
16
17
17
18
export interface FileTreeProps {
18
19
rootPath : string
19
20
}
20
21
21
- type FileInfoSidebarTreeItem = FileInfoTreeItem & {
22
- checked : boolean
23
- }
24
-
25
- type FileSidebarTreeItem = TreeItemBaseState < FileInfoSidebarTreeItem >
26
-
27
22
const FileTree : FC < FileTreeProps > = ( props : FileTreeProps ) => {
28
23
const { rootPath } = props
29
24
const [ filesTree , _setFilesTree ] = useState < FileSidebarTreeItem [ ] > ( [ ] )
@@ -33,6 +28,10 @@ const FileTree: FC<FileTreeProps> = (props: FileTreeProps) => {
33
28
updateExpendedFilePaths,
34
29
checkedFilePaths,
35
30
updateCheckedFilePaths,
31
+ provideFilePathsTreePromptToGpt,
32
+ updateProvideFilePathsTreePromptToGpt,
33
+ filePathsTreePrompt,
34
+ updateFilePathsTreePrompt,
36
35
} = useGlobalStore ( )
37
36
38
37
const updateMap = useCallback ( ( tree : FileSidebarTreeItem [ ] ) => {
@@ -74,7 +73,7 @@ const FileTree: FC<FileTreeProps> = (props: FileTreeProps) => {
74
73
75
74
// sync checked state
76
75
useEffect ( ( ) => {
77
- if ( ! Object . values ( fullPathFileMapRef . current ) . length )
76
+ if ( ! Object . values ( fullPathFileMapRef . current ) . length || ! filesTree . length )
78
77
return
79
78
80
79
// check all path in checkedFilePaths
@@ -123,9 +122,8 @@ const FileTree: FC<FileTreeProps> = (props: FileTreeProps) => {
123
122
return result
124
123
} )
125
124
126
- console . log ( 'finalFilesSidebarTree' , finalFilesSidebarTree )
127
-
128
125
setFilesTree ( finalFilesSidebarTree , true )
126
+ updateFilePathsTreePrompt ( finalFilesSidebarTree )
129
127
} , [ fetchCommonFilesTreeRes , setFilesTree ] )
130
128
131
129
const renderTreeItemLeftSlot = ( props : TreeItemState < FileInfoSidebarTreeItem > ) => {
@@ -265,11 +263,18 @@ const FileTree: FC<FileTreeProps> = (props: FileTreeProps) => {
265
263
if ( ! Object . keys ( fullPathFileMapRef . current ) . length )
266
264
return null
267
265
268
- const totalTokenNum = checkedFilePaths . reduce ( ( pre , cur ) => {
266
+ const filaPathsPromptTokenNum = countTokenQuick ( filePathsTreePrompt )
267
+
268
+ const checkedFilesContentPromptTokenNum = checkedFilePaths . reduce ( ( pre , cur ) => {
269
269
const file = fullPathFileMapRef . current [ cur ]
270
270
return pre + ( file . otherInfo ?. tokenNum ?? 0 )
271
271
} , 0 )
272
272
273
+ let totalTokenNum = checkedFilesContentPromptTokenNum
274
+
275
+ if ( provideFilePathsTreePromptToGpt )
276
+ totalTokenNum += filaPathsPromptTokenNum
277
+
273
278
const resetAllChecked = ( ) => {
274
279
updateCheckedFilePaths ( ( preState ) => {
275
280
preState . forEach ( ( item ) => {
@@ -280,19 +285,35 @@ const FileTree: FC<FileTreeProps> = (props: FileTreeProps) => {
280
285
281
286
return [ ]
282
287
} )
288
+ updateProvideFilePathsTreePromptToGpt ( false )
289
+ }
290
+
291
+ const handleProvideFilePathsTreePromptToGptChange = ( e : any ) => {
292
+ const checked = e . target ?. checked as boolean
293
+ updateProvideFilePathsTreePromptToGpt ( checked )
283
294
}
284
295
285
296
return < FileTreeSidebarUnderSearchWrapper >
286
- < FileTreeSidebarHighlight > { checkedFilePaths . length } </ FileTreeSidebarHighlight >
287
- files .
297
+ < FileTreeSidebarHighlight style = { { paddingLeft : 0 } } > { checkedFilePaths . length } </ FileTreeSidebarHighlight >
298
+ Files .
288
299
< FileTreeSidebarHighlight > { formatNumWithK ( totalTokenNum ) } </ FileTreeSidebarHighlight >
289
- tokens .
300
+ Tokens .
290
301
< VSCodeLink style = { {
291
302
display : 'inline-block' ,
292
303
marginLeft : '0.25rem' ,
293
304
} } onClick = { resetAllChecked } >
294
- clean
305
+ Clear Checked
295
306
</ VSCodeLink >
307
+ < VSCodeCheckbox
308
+ style = { {
309
+ marginTop : '0.5rem' ,
310
+ } }
311
+ checked = { provideFilePathsTreePromptToGpt }
312
+ onChange = { handleProvideFilePathsTreePromptToGptChange } >
313
+ All file path as prompt
314
+ < FileTreeSidebarHighlight > { formatNumWithK ( filaPathsPromptTokenNum ) } </ FileTreeSidebarHighlight >
315
+ token
316
+ </ VSCodeCheckbox >
296
317
</ FileTreeSidebarUnderSearchWrapper >
297
318
}
298
319
0 commit comments