Skip to content

Commit 90084ce

Browse files
Context Window Warning (#152)
* context window warning & compact command * auto compact * fix permissions * update readme * fix 3.5 context window * small update * remove unused interface * remove unused msg
1 parent 9345830 commit 90084ce

File tree

12 files changed

+537
-98
lines changed

12 files changed

+537
-98
lines changed

README.md

Lines changed: 31 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,12 +62,29 @@ OpenCode looks for configuration in the following locations:
6262
- `$XDG_CONFIG_HOME/opencode/.opencode.json`
6363
- `./.opencode.json` (local directory)
6464

65+
### Auto Compact Feature
66+
67+
OpenCode includes an auto compact feature that automatically summarizes your conversation when it approaches the model's context window limit. When enabled (default setting), this feature:
68+
69+
- Monitors token usage during your conversation
70+
- Automatically triggers summarization when usage reaches 95% of the model's context window
71+
- Creates a new session with the summary, allowing you to continue your work without losing context
72+
- Helps prevent "out of context" errors that can occur with long conversations
73+
74+
You can enable or disable this feature in your configuration file:
75+
76+
```json
77+
{
78+
"autoCompact": true // default is true
79+
}
80+
```
81+
6582
### Environment Variables
6683

6784
You can configure OpenCode using environment variables:
6885

6986
| Environment Variable | Purpose |
70-
|----------------------------|--------------------------------------------------------|
87+
| -------------------------- | ------------------------------------------------------ |
7188
| `ANTHROPIC_API_KEY` | For Claude models |
7289
| `OPENAI_API_KEY` | For OpenAI models |
7390
| `GEMINI_API_KEY` | For Google Gemini models |
@@ -79,7 +96,6 @@ You can configure OpenCode using environment variables:
7996
| `AZURE_OPENAI_API_KEY` | For Azure OpenAI models (optional when using Entra ID) |
8097
| `AZURE_OPENAI_API_VERSION` | For Azure OpenAI models |
8198

82-
8399
### Configuration File Structure
84100

85101
```json
@@ -134,7 +150,8 @@ You can configure OpenCode using environment variables:
134150
}
135151
},
136152
"debug": false,
137-
"debugLSP": false
153+
"debugLSP": false,
154+
"autoCompact": true
138155
}
139156
```
140157

@@ -327,9 +344,11 @@ OpenCode supports custom commands that can be created by users to quickly send p
327344
Custom commands are predefined prompts stored as Markdown files in one of three locations:
328345

329346
1. **User Commands** (prefixed with `user:`):
347+
330348
```
331349
$XDG_CONFIG_HOME/opencode/commands/
332350
```
351+
333352
(typically `~/.config/opencode/commands/` on Linux/macOS)
334353

335354
or
@@ -382,6 +401,15 @@ This creates a command with ID `user:git:commit`.
382401

383402
The content of the command file will be sent as a message to the AI assistant.
384403

404+
### Built-in Commands
405+
406+
OpenCode includes several built-in commands:
407+
408+
| Command | Description |
409+
| ------------------ | --------------------------------------------------------------------------------------------------- |
410+
| Initialize Project | Creates or updates the OpenCode.md memory file with project-specific information |
411+
| Compact Session | Manually triggers the summarization of the current session, creating a new session with the summary |
412+
385413
## MCP (Model Context Protocol)
386414

387415
OpenCode implements the Model Context Protocol (MCP) to extend its capabilities through external tools. MCP provides a standardized way for the AI assistant to interact with external services and tools.

cmd/root.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,7 @@ func setupSubscriptions(app *app.App, parentCtx context.Context) (chan tea.Msg,
218218
setupSubscriber(ctx, &wg, "sessions", app.Sessions.Subscribe, ch)
219219
setupSubscriber(ctx, &wg, "messages", app.Messages.Subscribe, ch)
220220
setupSubscriber(ctx, &wg, "permissions", app.Permissions.Subscribe, ch)
221+
setupSubscriber(ctx, &wg, "coderAgent", app.CoderAgent.Subscribe, ch)
221222

222223
cleanupFunc := func() {
223224
logging.Info("Cancelling all subscriptions")

internal/config/config.go

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,10 @@ type MCPServer struct {
3636
type AgentName string
3737

3838
const (
39-
AgentCoder AgentName = "coder"
40-
AgentTask AgentName = "task"
41-
AgentTitle AgentName = "title"
39+
AgentCoder AgentName = "coder"
40+
AgentSummarizer AgentName = "summarizer"
41+
AgentTask AgentName = "task"
42+
AgentTitle AgentName = "title"
4243
)
4344

4445
// Agent defines configuration for different LLM models and their token limits.
@@ -84,6 +85,7 @@ type Config struct {
8485
DebugLSP bool `json:"debugLSP,omitempty"`
8586
ContextPaths []string `json:"contextPaths,omitempty"`
8687
TUI TUIConfig `json:"tui"`
88+
AutoCompact bool `json:"autoCompact,omitempty"`
8789
}
8890

8991
// Application constants
@@ -213,6 +215,7 @@ func setDefaults(debug bool) {
213215
viper.SetDefault("data.directory", defaultDataDirectory)
214216
viper.SetDefault("contextPaths", defaultContextPaths)
215217
viper.SetDefault("tui.theme", "opencode")
218+
viper.SetDefault("autoCompact", true)
216219

217220
if debug {
218221
viper.SetDefault("debug", true)
@@ -262,6 +265,7 @@ func setProviderDefaults() {
262265
// Anthropic configuration
263266
if key := viper.GetString("providers.anthropic.apiKey"); strings.TrimSpace(key) != "" {
264267
viper.SetDefault("agents.coder.model", models.Claude37Sonnet)
268+
viper.SetDefault("agents.summarizer.model", models.Claude37Sonnet)
265269
viper.SetDefault("agents.task.model", models.Claude37Sonnet)
266270
viper.SetDefault("agents.title.model", models.Claude37Sonnet)
267271
return
@@ -270,6 +274,7 @@ func setProviderDefaults() {
270274
// OpenAI configuration
271275
if key := viper.GetString("providers.openai.apiKey"); strings.TrimSpace(key) != "" {
272276
viper.SetDefault("agents.coder.model", models.GPT41)
277+
viper.SetDefault("agents.summarizer.model", models.GPT41)
273278
viper.SetDefault("agents.task.model", models.GPT41Mini)
274279
viper.SetDefault("agents.title.model", models.GPT41Mini)
275280
return
@@ -278,6 +283,7 @@ func setProviderDefaults() {
278283
// Google Gemini configuration
279284
if key := viper.GetString("providers.gemini.apiKey"); strings.TrimSpace(key) != "" {
280285
viper.SetDefault("agents.coder.model", models.Gemini25)
286+
viper.SetDefault("agents.summarizer.model", models.Gemini25)
281287
viper.SetDefault("agents.task.model", models.Gemini25Flash)
282288
viper.SetDefault("agents.title.model", models.Gemini25Flash)
283289
return
@@ -286,6 +292,7 @@ func setProviderDefaults() {
286292
// Groq configuration
287293
if key := viper.GetString("providers.groq.apiKey"); strings.TrimSpace(key) != "" {
288294
viper.SetDefault("agents.coder.model", models.QWENQwq)
295+
viper.SetDefault("agents.summarizer.model", models.QWENQwq)
289296
viper.SetDefault("agents.task.model", models.QWENQwq)
290297
viper.SetDefault("agents.title.model", models.QWENQwq)
291298
return
@@ -294,6 +301,7 @@ func setProviderDefaults() {
294301
// OpenRouter configuration
295302
if key := viper.GetString("providers.openrouter.apiKey"); strings.TrimSpace(key) != "" {
296303
viper.SetDefault("agents.coder.model", models.OpenRouterClaude37Sonnet)
304+
viper.SetDefault("agents.summarizer.model", models.OpenRouterClaude37Sonnet)
297305
viper.SetDefault("agents.task.model", models.OpenRouterClaude37Sonnet)
298306
viper.SetDefault("agents.title.model", models.OpenRouterClaude35Haiku)
299307
return
@@ -302,6 +310,7 @@ func setProviderDefaults() {
302310
// XAI configuration
303311
if key := viper.GetString("providers.xai.apiKey"); strings.TrimSpace(key) != "" {
304312
viper.SetDefault("agents.coder.model", models.XAIGrok3Beta)
313+
viper.SetDefault("agents.summarizer.model", models.XAIGrok3Beta)
305314
viper.SetDefault("agents.task.model", models.XAIGrok3Beta)
306315
viper.SetDefault("agents.title.model", models.XAiGrok3MiniFastBeta)
307316
return
@@ -310,6 +319,7 @@ func setProviderDefaults() {
310319
// AWS Bedrock configuration
311320
if hasAWSCredentials() {
312321
viper.SetDefault("agents.coder.model", models.BedrockClaude37Sonnet)
322+
viper.SetDefault("agents.summarizer.model", models.BedrockClaude37Sonnet)
313323
viper.SetDefault("agents.task.model", models.BedrockClaude37Sonnet)
314324
viper.SetDefault("agents.title.model", models.BedrockClaude37Sonnet)
315325
return
@@ -318,6 +328,7 @@ func setProviderDefaults() {
318328
// Azure OpenAI configuration
319329
if os.Getenv("AZURE_OPENAI_ENDPOINT") != "" {
320330
viper.SetDefault("agents.coder.model", models.AzureGPT41)
331+
viper.SetDefault("agents.summarizer.model", models.AzureGPT41)
321332
viper.SetDefault("agents.task.model", models.AzureGPT41Mini)
322333
viper.SetDefault("agents.title.model", models.AzureGPT41Mini)
323334
return

internal/llm/agent/agent-tool.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -69,11 +69,11 @@ func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolRes
6969
return tools.ToolResponse{}, fmt.Errorf("error generating agent: %s", err)
7070
}
7171
result := <-done
72-
if result.Err() != nil {
73-
return tools.ToolResponse{}, fmt.Errorf("error generating agent: %s", result.Err())
72+
if result.Error != nil {
73+
return tools.ToolResponse{}, fmt.Errorf("error generating agent: %s", result.Error)
7474
}
7575

76-
response := result.Response()
76+
response := result.Message
7777
if response.Role != message.Assistant {
7878
return tools.NewTextErrorResponse("no response"), nil
7979
}
@@ -88,8 +88,6 @@ func (b *agentTool) Run(ctx context.Context, call tools.ToolCall) (tools.ToolRes
8888
}
8989

9090
parentSession.Cost += updatedSession.Cost
91-
parentSession.PromptTokens += updatedSession.PromptTokens
92-
parentSession.CompletionTokens += updatedSession.CompletionTokens
9391

9492
_, err = b.sessions.Save(ctx, parentSession)
9593
if err != nil {

0 commit comments

Comments
 (0)