(self, payload: MemoryWritePayload)
| 241 | return max(0.1, ideal_ratio_max / ratio) |
| 242 | |
| 243 | def update(self, payload: MemoryWritePayload) -> None: |
| 244 | if not self.embedding: |
| 245 | return |
| 246 | |
| 247 | snapshot = payload.output_snapshot |
| 248 | if not snapshot or not snapshot.text.strip(): |
| 249 | return |
| 250 | |
| 251 | raw_content = self.update_prompt.format( |
| 252 | input=payload.inputs_text, |
| 253 | output=snapshot.text, |
| 254 | ) |
| 255 | extracted_content = self._extract_key_content(raw_content) |
| 256 | |
| 257 | if len(extracted_content) < self.min_content_length: |
| 258 | return |
| 259 | |
| 260 | content_hash = self._generate_content_hash(extracted_content) |
| 261 | for existing_item in self.contents: |
| 262 | existing_hash = self._generate_content_hash(existing_item.content_summary) |
| 263 | if existing_hash == content_hash: |
| 264 | return |
| 265 | |
| 266 | embedding_vector = self.embedding.get_embedding(extracted_content) |
| 267 | if isinstance(embedding_vector, list): |
| 268 | embedding_vector = np.array(embedding_vector, dtype=np.float32) |
| 269 | if embedding_vector is None: |
| 270 | return |
| 271 | embedding_array = np.array(embedding_vector, dtype=np.float32).reshape(1, -1) |
| 272 | faiss.normalize_L2(embedding_array) |
| 273 | |
| 274 | metadata = { |
| 275 | "agent_role": payload.agent_role, |
| 276 | "input_preview": (payload.inputs_text or "")[:200], |
| 277 | "content_length": len(extracted_content), |
| 278 | "attachments": snapshot.attachment_overview(), |
| 279 | } |
| 280 | |
| 281 | memory_item = MemoryItem( |
| 282 | id=f"{content_hash}_{int(time.time())}", |
| 283 | content_summary=extracted_content, |
| 284 | metadata=metadata, |
| 285 | embedding=embedding_array.tolist()[0], |
| 286 | input_snapshot=payload.input_snapshot, |
| 287 | output_snapshot=snapshot, |
| 288 | ) |
| 289 | |
| 290 | self.contents.append(memory_item) |
| 291 | |
| 292 | max_memories = 1000 |
| 293 | if len(self.contents) > max_memories: |
| 294 | self.contents = self.contents[-max_memories:] |
nothing calls this directly
no test coverage detected