maybeCompact checks if memory compaction should run and executes it.
(ctx context.Context)
| 378 | |
| 379 | // maybeCompact checks if memory compaction should run and executes it. |
| 380 | func (mw *memoryWorker) maybeCompact(ctx context.Context) { |
| 381 | if mw.cli.memoryStore == nil { |
| 382 | return |
| 383 | } |
| 384 | |
| 385 | mgr := mw.cli.memoryStore.Manager() |
| 386 | if !mgr.NeedsCompaction() { |
| 387 | return |
| 388 | } |
| 389 | |
| 390 | mw.logger.Info("Memory worker: starting compaction") |
| 391 | |
| 392 | llmClient := mw.cli.getClient() |
| 393 | var sendPrompt func(ctx context.Context, prompt string) (string, error) |
| 394 | |
| 395 | if llmClient != nil { |
| 396 | sendPrompt = func(ctx context.Context, prompt string) (string, error) { |
| 397 | history := []models.Message{ |
| 398 | {Role: "user", Content: prompt}, |
| 399 | } |
| 400 | return llmClient.SendPrompt(ctx, prompt, history, 0) |
| 401 | } |
| 402 | } |
| 403 | |
| 404 | ctx, cancel := context.WithTimeout(ctx, 2*time.Minute) |
| 405 | defer cancel() |
| 406 | |
| 407 | if err := mgr.RunCompaction(ctx, sendPrompt); err != nil { |
| 408 | mw.logger.Warn("Memory worker: compaction failed", zap.Error(err)) |
| 409 | } else { |
| 410 | mw.logger.Info("Memory worker: compaction complete") |
| 411 | if mw.cli.contextBuilder != nil { |
| 412 | mw.cli.contextBuilder.InvalidateCache() |
| 413 | } |
| 414 | } |
| 415 | } |
| 416 | |
| 417 | // cleanupDailyNotes removes old daily notes. |
| 418 | func (mw *memoryWorker) cleanupDailyNotes() { |
no test coverage detected