diff --git a/internal/config/config.go b/internal/config/config.go index b4682ce876bb4980bbe119c187538bf467d2f514..614fce08193659a7cd91e39a005606a60aab9a18 100644 --- a/internal/config/config.go +++ b/internal/config/config.go @@ -140,6 +140,7 @@ type Options struct { DebugLSP bool `json:"debug_lsp,omitempty" jsonschema:"description=Enable debug logging for LSP servers,default=false"` DisableAutoSummarize bool `json:"disable_auto_summarize,omitempty" jsonschema:"description=Disable automatic conversation summarization,default=false"` DataDirectory string `json:"data_directory,omitempty" jsonschema:"description=Directory for storing application data (relative to working directory),default=.crush,example=.crush"` // Relative to the cwd + MaxMessagesInContext int `json:"max_messages_in_context,omitempty" jsonschema:"description=Maximum number of messages to keep in context (sliding window),default=50,minimum=10,maximum=200"` } type MCPs map[string]MCPConfig diff --git a/internal/config/load.go b/internal/config/load.go index a2d2155048a8abf634958293056da8a4713e1400..4c1bd84d0cf0c1243f9101933dbf9115bfdd58a3 100644 --- a/internal/config/load.go +++ b/internal/config/load.go @@ -282,6 +282,9 @@ func (c *Config) setDefaults(workingDir string) { if c.Options.DataDirectory == "" { c.Options.DataDirectory = filepath.Join(workingDir, defaultDataDirectory) } + if c.Options.MaxMessagesInContext == 0 { + c.Options.MaxMessagesInContext = 50 // Default sliding window size + } if c.Providers == nil { c.Providers = csync.NewMap[string, ProviderConfig]() } diff --git a/internal/llm/agent/agent.go b/internal/llm/agent/agent.go index 80a7095ed381985b5155c8a29ac933156eb85ffd..3f3361359bf121b67dcc08917f34c2053b540257 100644 --- a/internal/llm/agent/agent.go +++ b/internal/llm/agent/agent.go @@ -362,6 +362,13 @@ func (a *agent) processGeneration(ctx context.Context, sessionID, content string if err != nil { return a.err(fmt.Errorf("failed to list messages: %w", err)) } + + // Implement sliding window to limit message history + maxMessages := cfg.Options.MaxMessagesInContext + if maxMessages > 0 && len(msgs) > maxMessages { + // Keep the first message (usually system/context) and the last N-1 messages + msgs = append(msgs[:1], msgs[len(msgs)-maxMessages+1:]...) + } if len(msgs) == 0 { go func() { defer log.RecoverPanic("agent.Run", func() {