11import { BaseSession } from "../../utils/session.ts"
2- import { EDITOR_SYSTEM_PROMPT , API_CONFIG } from "../../config/constants.ts"
3- import { ToolCall , ToolResult } from "../../types/interfaces.ts"
2+ import { EDITOR_SYSTEM_PROMPT , API_CONFIG , MEMORY_TOOLS } from "../../config/constants.ts"
3+ import { ToolResult } from "../../types/interfaces.ts"
44import { log } from "../../config/logging.ts"
55import { EditorHandlers } from "./handlers.ts"
6+ import { MemoryManager } from "../memory/memory_manager.ts"
67
78export class EditorSession extends BaseSession {
89 private handlers : EditorHandlers
10+ private memoryManager : MemoryManager
911
1012 constructor ( sessionId ?: string ) {
1113 super ( sessionId )
1214 this . handlers = new EditorHandlers ( )
15+ this . memoryManager = new MemoryManager ( )
1316 }
1417
1518 async processEdit ( prompt : string ) : Promise < void > {
@@ -20,14 +23,17 @@ export class EditorSession extends BaseSession {
2023 }
2124 this . messages = [ message ]
2225
23- log . info ( `User input: ${ JSON . stringify ( message ) } , System Prompt: ${ EDITOR_SYSTEM_PROMPT } ` )
26+ log . info ( `User input: ${ JSON . stringify ( message ) } ` )
2427
2528 while ( true ) {
2629 const response = await this . client . beta . messages . create ( {
2730 model : API_CONFIG . MODEL ,
2831 max_tokens : API_CONFIG . MAX_TOKENS ,
2932 messages : this . messages ,
30- tools : [ { type : "text_editor_20241022" , name : "str_replace_editor" } ] ,
33+ tools : [
34+ { type : "text_editor_20241022" , name : "str_replace_editor" } ,
35+ ...MEMORY_TOOLS
36+ ] ,
3137 system : EDITOR_SYSTEM_PROMPT ,
3238 betas : [ "computer-use-2024-10-22" ] ,
3339 } )
@@ -77,20 +83,35 @@ export class EditorSession extends BaseSession {
7783 const results : ToolResult [ ] = [ ]
7884
7985 for ( const toolCall of content ) {
80- if ( toolCall . type === "tool_use" && toolCall . name === "str_replace_editor" ) {
81- log . info ( `Editor tool call input: ${ JSON . stringify ( toolCall . input ) } ` )
82-
83- const result = await this . handlers . handleTextEditorTool ( toolCall . input )
84- const isError = "error" in result
85- const toolResultContent = isError
86- ? [ { type : "text" , text : result . error } ]
87- : [ { type : "text" , text : result . content || "" } ]
86+ if ( toolCall . type === "tool_use" ) {
87+ let result
88+ let isError = false
89+
90+ switch ( toolCall . name ) {
91+ case "str_replace_editor" :
92+ result = await this . handlers . handleTextEditorTool ( toolCall . input )
93+ isError = "error" in result
94+ break
95+ case "add_memory" :
96+ result = await this . memoryManager . addMemory ( toolCall . input . content )
97+ break
98+ case "get_memories" :
99+ result = await this . memoryManager . getMemories ( )
100+ break
101+ case "clear_memories" :
102+ await this . memoryManager . clearMemories ( )
103+ result = { message : "Memories cleared successfully" }
104+ break
105+ }
88106
89107 results . push ( {
90108 tool_call_id : toolCall . id ,
91109 output : {
92110 type : "tool_result" ,
93- content : toolResultContent ,
111+ content : [ {
112+ type : "text" ,
113+ text : JSON . stringify ( result )
114+ } ] ,
94115 tool_use_id : toolCall . id ,
95116 is_error : isError ,
96117 } ,
0 commit comments