( args: Record<string, unknown>, ctx: ToolContext )
| 67 | }; |
| 68 | |
| 69 | export async function handle( |
| 70 | args: Record<string, unknown>, |
| 71 | ctx: ToolContext |
| 72 | ): Promise<ToolResponse> { |
| 73 | const args_typed = args as { |
| 74 | type?: MemoryType; |
| 75 | category: MemoryCategory; |
| 76 | memory: string; |
| 77 | reason: string; |
| 78 | scope?: MemoryScope; |
| 79 | }; |
| 80 | |
| 81 | const { type = 'decision', category, memory, reason } = args_typed; |
| 82 | const scope = normalizeMemoryScope(args_typed.scope); |
| 83 | |
| 84 | try { |
| 85 | const crypto = await import('crypto'); |
| 86 | const memoryPath = ctx.paths.memory; |
| 87 | |
| 88 | const hashContent = buildMemoryIdentityParts({ type, category, memory, reason, scope }); |
| 89 | const hash = crypto.createHash('sha256').update(hashContent).digest('hex'); |
| 90 | const id = hash.substring(0, 12); |
| 91 | |
| 92 | const newMemory: Memory = { |
| 93 | id, |
| 94 | type, |
| 95 | category, |
| 96 | memory, |
| 97 | reason, |
| 98 | date: new Date().toISOString(), |
| 99 | ...(scope && { scope }) |
| 100 | }; |
| 101 | |
| 102 | const result = await appendMemoryFile(memoryPath, newMemory); |
| 103 | |
| 104 | if (result.status === 'duplicate') { |
| 105 | return { |
| 106 | content: [ |
| 107 | { |
| 108 | type: 'text', |
| 109 | text: JSON.stringify( |
| 110 | { |
| 111 | status: 'info', |
| 112 | message: 'This memory was already recorded.', |
| 113 | memory: result.memory |
| 114 | }, |
| 115 | null, |
| 116 | 2 |
| 117 | ) |
| 118 | } |
| 119 | ] |
| 120 | }; |
| 121 | } |
| 122 | |
| 123 | return { |
| 124 | content: [ |
| 125 | { |
| 126 | type: 'text', |
no test coverage detected