MCPcopy Create free account
hub / github.com/diillson/chatcli / maybeCompact

Method maybeCompact

cli/memory_worker.go:380–415  ·  view source on GitHub ↗

maybeCompact checks if memory compaction should run and executes it.

(ctx context.Context)

Source from the content-addressed store, hash-verified

378
379// maybeCompact checks if memory compaction should run and executes it.
380func (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.
418func (mw *memoryWorker) cleanupDailyNotes() {

Callers 1

loopMethod · 0.95

Calls 9

ManagerMethod · 0.80
RunCompactionMethod · 0.80
WarnMethod · 0.80
InfoMethod · 0.65
SendPromptMethod · 0.65
ErrorMethod · 0.65
NeedsCompactionMethod · 0.45
getClientMethod · 0.45
InvalidateCacheMethod · 0.45

Tested by

no test coverage detected