fix: address feedback

This commit is contained in:
Nate 2025-08-22 15:54:31 -07:00
parent 4793da0ab3
commit feefb7d5c2
4 changed files with 138 additions and 7 deletions

View file

@ -28,6 +28,7 @@ import { logger } from "../util/logger.js";
import {
calculateContextUsagePercentage,
countChatHistoryTokens,
getAutoCompactMessage,
shouldAutoCompact,
} from "../util/tokenizer.js";
@ -170,11 +171,7 @@ async function handleAutoCompaction(
logger.info("Auto-compacting triggered due to context limit");
if (!isHeadless) {
console.info(
chalk.yellow(
"\nApproaching context limit. Auto-compacting chat history...",
),
);
console.info(chalk.yellow(`\n${getAutoCompactMessage(model)}`));
} else if (format === "json") {
safeStdout(
JSON.stringify({

View file

@ -7,7 +7,10 @@ import { posthogService } from "../../telemetry/posthogService.js";
import { telemetryService } from "../../telemetry/telemetryService.js";
import { formatError } from "../../util/formatError.js";
import { logger } from "../../util/logger.js";
import { shouldAutoCompact } from "../../util/tokenizer.js";
import {
getAutoCompactMessage,
shouldAutoCompact,
} from "../../util/tokenizer.js";
import { DisplayMessage } from "../types.js";
import { SlashCommandResult } from "./useChat.types.js";
@ -305,7 +308,7 @@ export async function handleAutoCompaction({
...prev,
{
role: "system",
content: "Approaching context limit. Auto-compacting chat history...",
content: getAutoCompactMessage(model),
messageType: "system" as const,
},
]);

View file

@ -1,4 +1,5 @@
import { ModelConfig } from "@continuedev/config-yaml";
<<<<<<< Updated upstream
import { ChatCompletionMessageParam } from "openai/resources.mjs";
import { beforeEach, describe, expect, it, vi } from "vitest";
@ -36,12 +37,70 @@ describe("tokenizer", () => {
const model: ModelConfig = {
name: "test-model",
model: "test",
=======
import { getAutoCompactMessage, getModelContextLimit } from "./tokenizer.js";
describe("tokenizer", () => {
describe("getAutoCompactMessage", () => {
it("should show context limit for all models", () => {
const smallContextModel: ModelConfig = {
name: "gpt-3.5-turbo",
model: "gpt-3.5-turbo",
provider: "openai",
defaultCompletionOptions: {
contextLength: 4096,
},
};
const message = getAutoCompactMessage(smallContextModel);
expect(message).toBe(
"Approaching context limit (4K tokens). Auto-compacting chat history...",
);
});
it("should return message with context limit for large context models", () => {
const largeContextModel: ModelConfig = {
name: "gpt-4",
model: "gpt-4",
provider: "openai",
defaultCompletionOptions: {
contextLength: 128000,
},
};
const message = getAutoCompactMessage(largeContextModel);
expect(message).toBe(
"Approaching context limit (128K tokens). Auto-compacting chat history...",
);
});
it("should handle models without explicit context length", () => {
const modelWithoutContext: ModelConfig = {
name: "claude-3-sonnet",
model: "claude-3-sonnet-20240229",
provider: "anthropic",
// No defaultCompletionOptions
};
const message = getAutoCompactMessage(modelWithoutContext);
expect(message).toBe(
"Approaching context limit (200K tokens). Auto-compacting chat history...",
);
});
it("should properly format context length in thousands", () => {
const model8k: ModelConfig = {
name: "test-model",
model: "test-model",
>>>>>>> Stashed changes
provider: "openai",
defaultCompletionOptions: {
contextLength: 8192,
},
};
<<<<<<< Updated upstream
expect(getModelContextLimit(model)).toBe(8192);
});
@ -49,12 +108,72 @@ describe("tokenizer", () => {
const model: ModelConfig = {
name: "test-model",
model: "test",
=======
const message = getAutoCompactMessage(model8k);
expect(message).toBe(
"Approaching context limit (8K tokens). Auto-compacting chat history...",
);
});
it("should handle edge case at 16K boundary", () => {
const model16k: ModelConfig = {
name: "test-model",
model: "test-model",
provider: "openai",
defaultCompletionOptions: {
contextLength: 16000,
},
};
const message = getAutoCompactMessage(model16k);
expect(message).toBe(
"Approaching context limit (16K tokens). Auto-compacting chat history...",
);
});
it("should round context limit to nearest K", () => {
const model15k: ModelConfig = {
name: "test-model",
model: "test-model",
provider: "openai",
defaultCompletionOptions: {
contextLength: 15999,
},
};
const message = getAutoCompactMessage(model15k);
expect(message).toBe(
"Approaching context limit (16K tokens). Auto-compacting chat history...",
);
});
});
describe("getModelContextLimit", () => {
it("should return context length from model config when available", () => {
const model: ModelConfig = {
name: "test-model",
model: "test-model",
provider: "openai",
defaultCompletionOptions: {
contextLength: 4096,
},
};
expect(getModelContextLimit(model)).toBe(4096);
});
it("should return default context length when not specified", () => {
const model: ModelConfig = {
name: "test-model",
model: "test-model",
>>>>>>> Stashed changes
provider: "openai",
};
expect(getModelContextLimit(model)).toBe(200_000);
});
});
<<<<<<< Updated upstream
describe("countMessageTokens", () => {
it("should count tokens for string content", () => {
@ -289,4 +408,6 @@ describe("tokenizer", () => {
});
});
});
=======
>>>>>>> Stashed changes
});

View file

@ -155,3 +155,13 @@ export function shouldAutoCompact(
return usage >= AUTO_COMPACT_THRESHOLD;
}
/**
* Get a descriptive message for auto-compaction that shows the context limit
* @param model The model configuration
* @returns A descriptive message explaining why compaction is needed
*/
export function getAutoCompactMessage(model: ModelConfig): string {
const limit = getModelContextLimit(model);
return `Approaching context limit (${(limit / 1000).toFixed(0)}K tokens). Auto-compacting chat history...`;
}