Skip to content

Commit a9fc083

Browse files
committed
fix: correct syntax errors in model context window detection
1 parent 9e32afe commit a9fc083

File tree

2 files changed

+48
-10
lines changed

2 files changed

+48
-10
lines changed

packages/agent/CHANGELOG.md

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,13 @@
11
# [mycoder-agent-v1.7.0](https://github.com/drivecore/mycoder/compare/mycoder-agent-v1.6.0...mycoder-agent-v1.7.0) (2025-03-21)
22

3-
43
### Bug Fixes
54

6-
* Fix TypeScript errors and tests for message compaction feature ([d4f1fb5](https://github.com/drivecore/mycoder/commit/d4f1fb5d197e623bf98f2221352f9132dcb3e5de))
7-
5+
- Fix TypeScript errors and tests for message compaction feature ([d4f1fb5](https://github.com/drivecore/mycoder/commit/d4f1fb5d197e623bf98f2221352f9132dcb3e5de))
86

97
### Features
108

11-
* Add automatic compaction of historical messages for agents ([a5caf46](https://github.com/drivecore/mycoder/commit/a5caf464a0a8dca925c7b46023ebde4727e211f8)), closes [#338](https://github.com/drivecore/mycoder/issues/338)
12-
* Improve message compaction with proactive suggestions ([6276bc0](https://github.com/drivecore/mycoder/commit/6276bc0bc5fa27c4f1e9be61ff4375690ad04c62))
9+
- Add automatic compaction of historical messages for agents ([a5caf46](https://github.com/drivecore/mycoder/commit/a5caf464a0a8dca925c7b46023ebde4727e211f8)), closes [#338](https://github.com/drivecore/mycoder/issues/338)
10+
- Improve message compaction with proactive suggestions ([6276bc0](https://github.com/drivecore/mycoder/commit/6276bc0bc5fa27c4f1e9be61ff4375690ad04c62))
1311

1412
# [mycoder-agent-v1.6.0](https://github.com/drivecore/mycoder/compare/mycoder-agent-v1.5.0...mycoder-agent-v1.6.0) (2025-03-21)
1513

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

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -154,29 +154,69 @@ export class AnthropicProvider implements LLMProvider {
154154
});
155155

156156
// Initialize model context window detection
157-
this.initializeModelContextWindow();
157+
// This is async but we don't need to await it here
158+
// If it fails, we'll fall back to hardcoded limits
159+
this.initializeModelContextWindow().catch((error) => {
160+
console.warn(
161+
`Failed to initialize model context window: ${error.message}`,
162+
);
163+
});
158164
}
159165

160166
/**
161167
* Fetches the model context window size from the Anthropic API
168+
*
169+
* @returns The context window size if successfully fetched, otherwise undefined
162170
*/
163-
private async initializeModelContextWindow(): Promise<void> {
171+
private async initializeModelContextWindow(): Promise<number | undefined> {
164172
try {
165173
const response = await this.client.models.list();
166-
const model = response.data.find((m) => m.id === this.model);
174+
175+
if (!response?.data || !Array.isArray(response.data)) {
176+
console.warn(`Invalid response from models.list() for ${this.model}`);
177+
return undefined;
178+
}
179+
180+
// Try to find the exact model
181+
let model = response.data.find((m) => m.id === this.model);
182+
183+
// If not found, try to find a model that starts with the same name
184+
// This helps with model aliases like 'claude-3-sonnet-latest'
185+
if (!model) {
186+
// Split by '-latest' or '-20' to get the base model name
187+
const parts = this.model.split('-latest');
188+
const modelPrefix =
189+
parts.length > 1 ? parts[0] : this.model.split('-20')[0];
190+
191+
if (modelPrefix) {
192+
model = response.data.find((m) => m.id.startsWith(modelPrefix));
193+
194+
if (model) {
195+
console.info(
196+
`Model ${this.model} not found, using ${model.id} for context window size`,
197+
);
198+
}
199+
}
200+
}
167201

168202
// Using type assertion to access context_window property
169203
// The Anthropic API returns context_window but it may not be in the TypeScript definitions
170204
if (model && 'context_window' in model) {
171-
this.modelContextWindow = (model as any).context_window;
205+
const contextWindow = (model as any).context_window;
206+
this.modelContextWindow = contextWindow;
172207
// Cache the result for future use
173-
modelContextWindowCache[this.model] = (model as any).context_window;
208+
modelContextWindowCache[this.model] = contextWindow;
209+
return contextWindow;
210+
} else {
211+
console.warn(`No context window information found for ${this.model}`);
212+
return undefined;
174213
}
175214
} catch (error) {
176215
console.warn(
177216
`Failed to fetch model context window for ${this.model}: ${(error as Error).message}`,
178217
);
179218
// Will fall back to hardcoded limits
219+
return undefined;
180220
}
181221
}
182222

0 commit comments

Comments
 (0)