/ src / tools / BashTool / utils.ts
utils.ts
 1  import { queryHaiku } from '../../services/claude.js'
 2  import { extractTag } from '../../utils/messages.js'
 3  import { MAX_OUTPUT_LENGTH } from './prompt.js'
 4  
 5  export function formatOutput(content: string): {
 6    totalLines: number
 7    truncatedContent: string
 8  } {
 9    if (content.length <= MAX_OUTPUT_LENGTH) {
10      return {
11        totalLines: content.split('\n').length,
12        truncatedContent: content,
13      }
14    }
15    const halfLength = MAX_OUTPUT_LENGTH / 2
16    const start = content.slice(0, halfLength)
17    const end = content.slice(-halfLength)
18    const truncated = `${start}\n\n... [${content.slice(halfLength, -halfLength).split('\n').length} lines truncated] ...\n\n${end}`
19  
20    return {
21      totalLines: content.split('\n').length,
22      truncatedContent: truncated,
23    }
24  }
25  
26  export async function getCommandFilePaths(
27    command: string,
28    output: string,
29  ): Promise<string[]> {
30    const response = await queryHaiku({
31      systemPrompt: [
32        `Extract any file paths that this command reads or modifies. For commands like "git diff" and "cat", include the paths of files being shown. Use paths verbatim -- don't add any slashes or try to resolve them. Do not try to infer paths that were not explicitly listed in the command output.
33  Format your response as:
34  <filepaths>
35  path/to/file1
36  path/to/file2
37  </filepaths>
38  
39  If no files are read or modified, return empty filepaths tags:
40  <filepaths>
41  </filepaths>
42  
43  Do not include any other text in your response.`,
44      ],
45      userPrompt: `Command: ${command}\nOutput: ${output}`,
46      enablePromptCaching: true,
47    })
48    const content = response.message.content
49      .filter(_ => _.type === 'text')
50      .map(_ => _.text)
51      .join('')
52  
53    return (
54      extractTag(content, 'filepaths')?.trim().split('\n').filter(Boolean) || []
55    )
56  }