|
1 | 1 | import {BaseSession} from "../../utils/session.ts" |
2 | | -import {BASH_SYSTEM_PROMPT, API_CONFIG} from "../../config/constants.ts" |
| 2 | +import {BASH_SYSTEM_PROMPT, API_CONFIG, MEMORY_TOOLS} from "../../config/constants.ts" |
3 | 3 | import {ToolResult} from "../../types/interfaces.ts" |
4 | 4 | import {log} from "../../config/logging.ts" |
5 | 5 | import {BashHandlers} from "./handlers.ts" |
| 6 | +import {MemoryManager} from "../memory/memory_manager.ts" |
6 | 7 |
|
7 | 8 | export class BashSession extends BaseSession { |
8 | 9 | private noAgi: boolean |
9 | 10 | private environment: Record<string, string> |
| 11 | + private memoryManager: MemoryManager |
| 12 | + |
10 | 13 | private handlers: BashHandlers |
11 | 14 |
|
12 | 15 | constructor(sessionId?: string, noAgi = false) { |
13 | 16 | super(sessionId) |
14 | 17 | this.noAgi = noAgi |
15 | 18 | this.environment = {...Deno.env.toObject()} |
16 | 19 | this.handlers = new BashHandlers(noAgi) |
| 20 | + this.memoryManager = new MemoryManager() |
17 | 21 | } |
18 | 22 |
|
19 | 23 | async processBashCommand(bashPrompt: string): Promise<void> { |
@@ -48,7 +52,9 @@ System Context: |
48 | 52 | model: API_CONFIG.MODEL, |
49 | 53 | max_tokens: API_CONFIG.MAX_TOKENS, |
50 | 54 | messages: this.messages, |
51 | | - tools: [{type: "bash_20241022", name: "bash"}], |
| 55 | + tools: [{type: "bash_20241022", name: "bash"} |
| 56 | + , ...MEMORY_TOOLS |
| 57 | + ], |
52 | 58 | system: systemContext, |
53 | 59 | betas: ["computer-use-2024-10-22"], |
54 | 60 | }) |
@@ -94,24 +100,69 @@ System Context: |
94 | 100 | const results: ToolResult[] = [] |
95 | 101 |
|
96 | 102 | for (const toolCall of content) { |
97 | | - if (toolCall.type === "tool_use" && toolCall.name === "bash") { |
98 | | - log.info(`Bash tool call input: ${JSON.stringify(toolCall.input)}`) |
99 | | - |
100 | | - const result = await this.handlers.handleBashCommand(toolCall.input) |
101 | | - const isError = "error" in result |
102 | | - const toolResultContent = isError |
103 | | - ? [{type: "text", text: result.error}] |
104 | | - : [{type: "text", text: result.content || ""}] |
105 | | - |
106 | | - results.push({ |
107 | | - tool_call_id: toolCall.id, |
108 | | - output: { |
109 | | - type: "tool_result", |
110 | | - content: toolResultContent, |
111 | | - tool_use_id: toolCall.id, |
112 | | - is_error: isError, |
113 | | - }, |
114 | | - }) |
| 103 | + if (toolCall.type === "tool_use") { |
| 104 | + let result |
| 105 | + let isError = false |
| 106 | + |
| 107 | + switch (toolCall.name) { |
| 108 | + case "bash": { |
| 109 | + |
| 110 | + log.info(`Bash tool call input: ${JSON.stringify(toolCall.input)}`) |
| 111 | + result = await this.handlers.handleBashCommand(toolCall.input) |
| 112 | + isError = "error" in result |
| 113 | + const toolResultContent = isError |
| 114 | + ? [{type: "text", text: result.error}] |
| 115 | + : [{type: "text", text: result.content || "Command executed successfully."}] |
| 116 | + |
| 117 | + results.push({ |
| 118 | + tool_call_id: toolCall.id, |
| 119 | + output: { |
| 120 | + type: "tool_result", |
| 121 | + content: toolResultContent, |
| 122 | + tool_use_id: toolCall.id, |
| 123 | + is_error: isError, |
| 124 | + }, |
| 125 | + }) |
| 126 | + break |
| 127 | + } |
| 128 | + case "add_memory": |
| 129 | + result = await this.memoryManager.addMemory(toolCall.input.content) |
| 130 | + results.push({ |
| 131 | + tool_call_id: toolCall.id, |
| 132 | + output: { |
| 133 | + type: "tool_result", |
| 134 | + content: [{type: "text", text: result.content}], |
| 135 | + tool_use_id: toolCall.id, |
| 136 | + is_error: false, |
| 137 | + }, |
| 138 | + }) |
| 139 | + break |
| 140 | + case "get_memories": |
| 141 | + result = await this.memoryManager.getMemories() |
| 142 | + results.push({ |
| 143 | + tool_call_id: toolCall.id, |
| 144 | + output: { |
| 145 | + type: "tool_result", |
| 146 | + content: [{type: "text", text: JSON.stringify(result)}], |
| 147 | + tool_use_id: toolCall.id, |
| 148 | + is_error: false, |
| 149 | + }, |
| 150 | + }) |
| 151 | + break |
| 152 | + case "clear_memories": |
| 153 | + await this.memoryManager.clearMemories() |
| 154 | + result = {message: "Memories cleared successfully"} |
| 155 | + results.push({ |
| 156 | + tool_call_id: toolCall.id, |
| 157 | + output: { |
| 158 | + type: "tool_result", |
| 159 | + content: [{type: "text", text: JSON.stringify(result)}], |
| 160 | + tool_use_id: toolCall.id, |
| 161 | + is_error: false, |
| 162 | + }, |
| 163 | + }) |
| 164 | + break |
| 165 | + } |
115 | 166 | } |
116 | 167 | } |
117 | 168 |
|
|
0 commit comments