-
Notifications
You must be signed in to change notification settings - Fork 480
Expand file tree
/
Copy pathbash-context-processor.ts
More file actions
47 lines (41 loc) · 1.28 KB
/
bash-context-processor.ts
File metadata and controls
47 lines (41 loc) · 1.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
import {
buildBashHistoryMessages,
createRunTerminalToolResult,
formatBashContextForPrompt,
} from './bash-messages'
import type { PendingBashMessage } from '../state/chat-store'
import type { ChatMessage } from '../types/chat'
// Turns pending bash executions into chat history messages and prompt context.
export const processBashContext = (
pendingBashMessages: PendingBashMessage[],
): {
bashMessages: ChatMessage[]
bashContextForPrompt: string
} => {
const bashContextForPrompt = formatBashContextForPrompt(pendingBashMessages)
const bashMessages: ChatMessage[] = []
for (const bash of pendingBashMessages) {
if (bash.addedToHistory) {
continue
}
const toolCallId = crypto.randomUUID()
const cwd = bash.cwd || process.cwd()
const toolResultOutput = createRunTerminalToolResult({
command: bash.command,
cwd,
stdout: bash.stdout || null,
stderr: bash.stderr || null,
exitCode: bash.exitCode,
})
const outputJson = JSON.stringify(toolResultOutput)
const { assistantMessage } = buildBashHistoryMessages({
command: bash.command,
cwd,
toolCallId,
output: outputJson,
isComplete: true,
})
bashMessages.push(assistantMessage)
}
return { bashMessages, bashContextForPrompt }
}