( memoryService: MemoryService, ctx: MemoryScopeContext, input: MemoryCommandInput, checkWriteAccess: (virtualPath: string) => MemoryToolResult | null )
| 124 | * short-circuits the dispatch. |
| 125 | */ |
| 126 | export async function executeMemoryCommand( |
| 127 | memoryService: MemoryService, |
| 128 | ctx: MemoryScopeContext, |
| 129 | input: MemoryCommandInput, |
| 130 | checkWriteAccess: (virtualPath: string) => MemoryToolResult | null |
| 131 | ): Promise<MemoryToolResult> { |
| 132 | try { |
| 133 | switch (input.command) { |
| 134 | case "view": { |
| 135 | if (input.path == null) { |
| 136 | return { success: false, error: "view requires 'path'" }; |
| 137 | } |
| 138 | return await memoryService.view(ctx, input.path, { |
| 139 | offset: input.offset ?? undefined, |
| 140 | limit: input.limit ?? undefined, |
| 141 | }); |
| 142 | } |
| 143 | case "create": { |
| 144 | if (input.path == null || input.file_text == null) { |
| 145 | return { success: false, error: "create requires 'path' and 'file_text'" }; |
| 146 | } |
| 147 | return ( |
| 148 | checkWriteAccess(input.path) ?? |
| 149 | (await memoryService.create(ctx, input.path, input.file_text, "agent")) |
| 150 | ); |
| 151 | } |
| 152 | case "str_replace": { |
| 153 | if (input.path == null || input.old_str == null) { |
| 154 | return { success: false, error: "str_replace requires 'path' and 'old_str'" }; |
| 155 | } |
| 156 | return ( |
| 157 | checkWriteAccess(input.path) ?? |
| 158 | (await memoryService.strReplace( |
| 159 | ctx, |
| 160 | input.path, |
| 161 | input.old_str, |
| 162 | input.new_str ?? "", |
| 163 | "agent" |
| 164 | )) |
| 165 | ); |
| 166 | } |
| 167 | case "insert": { |
| 168 | if (input.path == null || input.insert_line == null || input.insert_text == null) { |
| 169 | return { |
| 170 | success: false, |
| 171 | error: "insert requires 'path', 'insert_line' and 'insert_text'", |
| 172 | }; |
| 173 | } |
| 174 | return ( |
| 175 | checkWriteAccess(input.path) ?? |
| 176 | (await memoryService.insert( |
| 177 | ctx, |
| 178 | input.path, |
| 179 | input.insert_line, |
| 180 | input.insert_text, |
| 181 | "agent" |
| 182 | )) |
| 183 | ); |
no test coverage detected