Skip to content

Commit cb5434b

Browse files
committed
chore: format & lint
1 parent e88a2f8 commit cb5434b

File tree

21 files changed

+249
-187
lines changed

21 files changed

+249
-187
lines changed

docs/features/message-compaction.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ When agents run for extended periods, they accumulate a large history of message
77
### 1. Token Usage Tracking
88

99
The LLM abstraction now tracks and returns:
10+
1011
- Total tokens used in the current completion request
1112
- Maximum allowed tokens for the model/provider
1213

@@ -15,17 +16,20 @@ This information is used to monitor context window usage and trigger appropriate
1516
### 2. Status Updates
1617

1718
Agents receive status updates with information about:
19+
1820
- Current token usage and percentage of the maximum
1921
- Cost so far
2022
- Active sub-agents and their status
2123
- Active shell processes and their status
2224
- Active browser sessions and their status
2325

2426
Status updates are sent:
27+
2528
1. Every 5 agent interactions (periodic updates)
2629
2. Whenever token usage exceeds 50% of the maximum (threshold-based updates)
2730

2831
Example status update:
32+
2933
```
3034
--- STATUS UPDATE ---
3135
Token Usage: 45,235/100,000 (45%)
@@ -72,6 +76,7 @@ Agents are instructed to monitor their token usage through status updates and us
7276
## Configuration
7377

7478
The message compaction feature is enabled by default with reasonable defaults:
79+
7580
- Status updates every 5 agent interactions
7681
- Recommendation to compact at 70% token usage
7782
- Default preservation of 10 recent messages when compacting
@@ -81,17 +86,20 @@ The message compaction feature is enabled by default with reasonable defaults:
8186
The system includes token limits for various models:
8287

8388
### Anthropic Models
89+
8490
- claude-3-opus-20240229: 200,000 tokens
8591
- claude-3-sonnet-20240229: 200,000 tokens
8692
- claude-3-haiku-20240307: 200,000 tokens
8793
- claude-2.1: 100,000 tokens
8894

8995
### OpenAI Models
96+
9097
- gpt-4o: 128,000 tokens
9198
- gpt-4-turbo: 128,000 tokens
9299
- gpt-3.5-turbo: 16,385 tokens
93100

94101
### Ollama Models
102+
95103
- llama2: 4,096 tokens
96104
- mistral: 8,192 tokens
97105
- mixtral: 32,768 tokens
@@ -102,4 +110,4 @@ The system includes token limits for various models:
102110
- Maintains important context for agent operation
103111
- Enables longer-running agent sessions
104112
- Makes the system more robust for complex tasks
105-
- Gives agents self-awareness of resource usage
113+
- Gives agents self-awareness of resource usage

example-status-update.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -47,4 +47,4 @@ The agent can use the compactHistory tool like this:
4747
}
4848
```
4949

50-
This will summarize all but the 10 most recent messages into a single summary message, significantly reducing token usage while preserving important context.
50+
This will summarize all but the 10 most recent messages into a single summary message, significantly reducing token usage while preserving important context.

packages/agent/CHANGELOG.md

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
# [mycoder-agent-v1.6.0](https://github.com/drivecore/mycoder/compare/mycoder-agent-v1.5.0...mycoder-agent-v1.6.0) (2025-03-21)
22

3-
43
### Features
54

6-
* **browser:** add system browser detection for Playwright ([00bd879](https://github.com/drivecore/mycoder/commit/00bd879443c9de51c6ee5e227d4838905506382a)), closes [#333](https://github.com/drivecore/mycoder/issues/333)
5+
- **browser:** add system browser detection for Playwright ([00bd879](https://github.com/drivecore/mycoder/commit/00bd879443c9de51c6ee5e227d4838905506382a)), closes [#333](https://github.com/drivecore/mycoder/issues/333)
76

87
# [mycoder-agent-v1.5.0](https://github.com/drivecore/mycoder/compare/mycoder-agent-v1.4.2...mycoder-agent-v1.5.0) (2025-03-20)
98

packages/agent/src/core/llm/providers/anthropic.ts

Lines changed: 18 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,21 @@ import {
1212
ProviderOptions,
1313
} from '../types.js';
1414

15+
// Define model context window sizes for Anthropic models
16+
const ANTHROPIC_MODEL_LIMITS: Record<string, number> = {
17+
default: 200000,
18+
'claude-3-7-sonnet-20250219': 200000,
19+
'claude-3-7-sonnet-latest': 200000,
20+
'claude-3-5-sonnet-20241022': 200000,
21+
'claude-3-5-sonnet-latest': 200000,
22+
'claude-3-haiku-20240307': 200000,
23+
'claude-3-opus-20240229': 200000,
24+
'claude-3-sonnet-20240229': 200000,
25+
'claude-2.1': 100000,
26+
'claude-2.0': 100000,
27+
'claude-instant-1.2': 100000,
28+
};
29+
1530
/**
1631
* Anthropic-specific options
1732
*/
@@ -81,28 +96,16 @@ function addCacheControlToMessages(
8196
});
8297
}
8398

84-
// Define model context window sizes for Anthropic models
85-
const ANTHROPIC_MODEL_LIMITS: Record<string, number> = {
86-
'claude-3-opus-20240229': 200000,
87-
'claude-3-sonnet-20240229': 200000,
88-
'claude-3-haiku-20240307': 200000,
89-
'claude-3-7-sonnet-20250219': 200000,
90-
'claude-2.1': 100000,
91-
'claude-2.0': 100000,
92-
'claude-instant-1.2': 100000,
93-
// Add other models as needed
94-
};
95-
9699
function tokenUsageFromMessage(message: Anthropic.Message, model: string) {
97100
const usage = new TokenUsage();
98101
usage.input = message.usage.input_tokens;
99102
usage.cacheWrites = message.usage.cache_creation_input_tokens ?? 0;
100103
usage.cacheReads = message.usage.cache_read_input_tokens ?? 0;
101104
usage.output = message.usage.output_tokens;
102-
105+
103106
const totalTokens = usage.input + usage.output;
104107
const maxTokens = ANTHROPIC_MODEL_LIMITS[model] || 100000; // Default fallback
105-
108+
106109
return {
107110
usage,
108111
totalTokens,
@@ -196,7 +199,7 @@ export class AnthropicProvider implements LLMProvider {
196199
});
197200

198201
const tokenInfo = tokenUsageFromMessage(response, this.model);
199-
202+
200203
return {
201204
text: content,
202205
toolCalls: toolCalls,

packages/agent/src/core/llm/providers/ollama.ts

Lines changed: 23 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -13,22 +13,6 @@ import {
1313

1414
import { TokenUsage } from '../../tokens.js';
1515
import { ToolCall } from '../../types.js';
16-
// Define model context window sizes for Ollama models
17-
// These are approximate and may vary based on specific model configurations
18-
const OLLAMA_MODEL_LIMITS: Record<string, number> = {
19-
'llama2': 4096,
20-
'llama2-uncensored': 4096,
21-
'llama2:13b': 4096,
22-
'llama2:70b': 4096,
23-
'mistral': 8192,
24-
'mistral:7b': 8192,
25-
'mixtral': 32768,
26-
'codellama': 16384,
27-
'phi': 2048,
28-
'phi2': 2048,
29-
'openchat': 8192,
30-
// Add other models as needed
31-
};
3216
import { LLMProvider } from '../provider.js';
3317
import {
3418
GenerateOptions,
@@ -38,6 +22,23 @@ import {
3822
FunctionDefinition,
3923
} from '../types.js';
4024

25+
// Define model context window sizes for Ollama models
26+
// These are approximate and may vary based on specific model configurations
27+
const OLLAMA_MODEL_LIMITS: Record<string, number> = {
28+
default: 4096,
29+
llama2: 4096,
30+
'llama2-uncensored': 4096,
31+
'llama2:13b': 4096,
32+
'llama2:70b': 4096,
33+
mistral: 8192,
34+
'mistral:7b': 8192,
35+
mixtral: 32768,
36+
codellama: 16384,
37+
phi: 2048,
38+
phi2: 2048,
39+
openchat: 8192,
40+
};
41+
4142
/**
4243
* Ollama-specific options
4344
*/
@@ -130,16 +131,17 @@ export class OllamaProvider implements LLMProvider {
130131
const tokenUsage = new TokenUsage();
131132
tokenUsage.output = response.eval_count || 0;
132133
tokenUsage.input = response.prompt_eval_count || 0;
133-
134+
134135
// Calculate total tokens and get max tokens for the model
135136
const totalTokens = tokenUsage.input + tokenUsage.output;
136-
137+
137138
// Extract the base model name without specific parameters
138139
const baseModelName = this.model.split(':')[0];
139140
// Check if model exists in limits, otherwise use base model or default
140-
const modelMaxTokens = OLLAMA_MODEL_LIMITS[this.model] ||
141-
(baseModelName ? OLLAMA_MODEL_LIMITS[baseModelName] : undefined) ||
142-
4096; // Default fallback
141+
const modelMaxTokens =
142+
OLLAMA_MODEL_LIMITS[this.model] ||
143+
(baseModelName ? OLLAMA_MODEL_LIMITS[baseModelName] : undefined) ||
144+
4096; // Default fallback
143145

144146
return {
145147
text: content,

packages/agent/src/core/llm/providers/openai.ts

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,11 @@ import type {
2121

2222
// Define model context window sizes for OpenAI models
2323
const OPENAI_MODEL_LIMITS: Record<string, number> = {
24+
default: 128000,
25+
'o3-mini': 200000,
26+
'o1-pro': 200000,
27+
o1: 200000,
28+
'o1-mini': 128000,
2429
'gpt-4o': 128000,
2530
'gpt-4-turbo': 128000,
2631
'gpt-4-0125-preview': 128000,
@@ -29,7 +34,6 @@ const OPENAI_MODEL_LIMITS: Record<string, number> = {
2934
'gpt-4-32k': 32768,
3035
'gpt-3.5-turbo': 16385,
3136
'gpt-3.5-turbo-16k': 16385,
32-
// Add other models as needed
3337
};
3438

3539
/**
@@ -129,7 +133,7 @@ export class OpenAIProvider implements LLMProvider {
129133
const tokenUsage = new TokenUsage();
130134
tokenUsage.input = response.usage?.prompt_tokens || 0;
131135
tokenUsage.output = response.usage?.completion_tokens || 0;
132-
136+
133137
// Calculate total tokens and get max tokens for the model
134138
const totalTokens = tokenUsage.input + tokenUsage.output;
135139
const modelMaxTokens = OPENAI_MODEL_LIMITS[this.model] || 8192; // Default fallback
@@ -217,4 +221,4 @@ export class OpenAIProvider implements LLMProvider {
217221
},
218222
}));
219223
}
220-
}
224+
}

packages/agent/src/core/llm/types.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -81,8 +81,8 @@ export interface LLMResponse {
8181
toolCalls: ToolCall[];
8282
tokenUsage: TokenUsage;
8383
// Add new fields for context window tracking
84-
totalTokens?: number; // Total tokens used in this request
85-
maxTokens?: number; // Maximum allowed tokens for this model
84+
totalTokens?: number; // Total tokens used in this request
85+
maxTokens?: number; // Maximum allowed tokens for this model
8686
}
8787

8888
/**

packages/agent/src/core/toolAgent/__tests__/statusUpdates.test.ts

Lines changed: 37 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -3,11 +3,11 @@
33
*/
44
import { describe, expect, it, vi } from 'vitest';
55

6-
import { TokenTracker } from '../../tokens.js';
7-
import { ToolContext } from '../../types.js';
86
import { AgentStatus } from '../../../tools/agent/AgentTracker.js';
9-
import { ShellStatus } from '../../../tools/shell/ShellTracker.js';
107
import { SessionStatus } from '../../../tools/session/SessionTracker.js';
8+
import { ShellStatus } from '../../../tools/shell/ShellTracker.js';
9+
import { TokenTracker } from '../../tokens.js';
10+
import { ToolContext } from '../../types.js';
1111
import { generateStatusUpdate } from '../statusUpdates.js';
1212

1313
describe('Status Updates', () => {
@@ -16,7 +16,7 @@ describe('Status Updates', () => {
1616
const totalTokens = 50000;
1717
const maxTokens = 100000;
1818
const tokenTracker = new TokenTracker('test');
19-
19+
2020
// Mock the context
2121
const context = {
2222
agentTracker: {
@@ -29,14 +29,21 @@ describe('Status Updates', () => {
2929
getSessionsByStatus: vi.fn().mockReturnValue([]),
3030
},
3131
} as unknown as ToolContext;
32-
32+
3333
// Execute
34-
const statusMessage = generateStatusUpdate(totalTokens, maxTokens, tokenTracker, context);
35-
34+
const statusMessage = generateStatusUpdate(
35+
totalTokens,
36+
maxTokens,
37+
tokenTracker,
38+
context,
39+
);
40+
3641
// Verify
3742
expect(statusMessage.role).toBe('system');
3843
expect(statusMessage.content).toContain('--- STATUS UPDATE ---');
39-
expect(statusMessage.content).toContain('Token Usage: 50,000/100,000 (50%)');
44+
expect(statusMessage.content).toContain(
45+
'Token Usage: 50,000/100,000 (50%)',
46+
);
4047
expect(statusMessage.content).toContain('Active Sub-Agents: 0');
4148
expect(statusMessage.content).toContain('Active Shell Processes: 0');
4249
expect(statusMessage.content).toContain('Active Browser Sessions: 0');
@@ -47,13 +54,13 @@ describe('Status Updates', () => {
4754
// With 50% usage, it should now show the high usage warning
4855
expect(statusMessage.content).toContain('Your token usage is high');
4956
});
50-
57+
5158
it('should include active agents, shells, and sessions', () => {
5259
// Setup
5360
const totalTokens = 70000;
5461
const maxTokens = 100000;
5562
const tokenTracker = new TokenTracker('test');
56-
63+
5764
// Mock the context with active agents, shells, and sessions
5865
const context = {
5966
agentTracker: {
@@ -64,29 +71,36 @@ describe('Status Updates', () => {
6471
},
6572
shellTracker: {
6673
getShells: vi.fn().mockReturnValue([
67-
{
68-
id: 'shell1',
69-
status: ShellStatus.RUNNING,
70-
metadata: { command: 'npm test' }
74+
{
75+
id: 'shell1',
76+
status: ShellStatus.RUNNING,
77+
metadata: { command: 'npm test' },
7178
},
7279
]),
7380
},
7481
browserTracker: {
7582
getSessionsByStatus: vi.fn().mockReturnValue([
76-
{
77-
id: 'session1',
78-
status: SessionStatus.RUNNING,
79-
metadata: { url: 'https://example.com' }
83+
{
84+
id: 'session1',
85+
status: SessionStatus.RUNNING,
86+
metadata: { url: 'https://example.com' },
8087
},
8188
]),
8289
},
8390
} as unknown as ToolContext;
84-
91+
8592
// Execute
86-
const statusMessage = generateStatusUpdate(totalTokens, maxTokens, tokenTracker, context);
87-
93+
const statusMessage = generateStatusUpdate(
94+
totalTokens,
95+
maxTokens,
96+
tokenTracker,
97+
context,
98+
);
99+
88100
// Verify
89-
expect(statusMessage.content).toContain('Token Usage: 70,000/100,000 (70%)');
101+
expect(statusMessage.content).toContain(
102+
'Token Usage: 70,000/100,000 (70%)',
103+
);
90104
expect(statusMessage.content).toContain('Your token usage is high (70%)');
91105
expect(statusMessage.content).toContain('recommended to use');
92106
expect(statusMessage.content).toContain('Active Sub-Agents: 2');
@@ -97,4 +111,4 @@ describe('Status Updates', () => {
97111
expect(statusMessage.content).toContain('Active Browser Sessions: 1');
98112
expect(statusMessage.content).toContain('- session1: https://example.com');
99113
});
100-
});
114+
});

0 commit comments

Comments
 (0)