storkit: merge 90_story_fetch_real_context_window_size_from_anthropic_models_api

This commit is contained in:
Dave
2026-03-21 11:58:33 +00:00
parent 30a56d03e5
commit a6c8cf0daf
3 changed files with 664 additions and 608 deletions

File diff suppressed because it is too large Load Diff

View File

@@ -4,7 +4,7 @@ import { Prism as SyntaxHighlighter } from "react-syntax-highlighter";
import { oneDark } from "react-syntax-highlighter/dist/esm/styles/prism";
import type { AgentConfigInfo } from "../api/agents";
import { agentsApi } from "../api/agents";
import type { PipelineState } from "../api/client";
import type { AnthropicModelInfo, PipelineState } from "../api/client";
import { api, ChatWebSocket } from "../api/client";
import { useChatHistory } from "../hooks/useChatHistory";
import type { Message, ProviderConfig } from "../types";
@@ -143,8 +143,13 @@ function formatToolActivity(toolName: string): string {
const estimateTokens = (text: string): number => Math.ceil(text.length / 4);
const getContextWindowSize = (modelName: string): number => {
if (modelName.startsWith("claude-")) return 200000;
const getContextWindowSize = (
modelName: string,
claudeContextWindows?: Map<string, number>,
): number => {
if (modelName.startsWith("claude-")) {
return claudeContextWindows?.get(modelName) ?? 200000;
}
if (modelName.includes("llama3")) return 8192;
if (modelName.includes("qwen2.5")) return 32768;
if (modelName.includes("deepseek")) return 16384;
@@ -163,6 +168,9 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
const [enableTools, setEnableTools] = useState(true);
const [availableModels, setAvailableModels] = useState<string[]>([]);
const [claudeModels, setClaudeModels] = useState<string[]>([]);
const [claudeContextWindowMap, setClaudeContextWindowMap] = useState<
Map<string, number>
>(new Map());
const [streamingContent, setStreamingContent] = useState("");
const [streamingThinking, setStreamingThinking] = useState("");
const [showApiKeyDialog, setShowApiKeyDialog] = useState(false);
@@ -285,7 +293,7 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
totalTokens += estimateTokens(streamingContent);
}
const contextWindow = getContextWindowSize(model);
const contextWindow = getContextWindowSize(model, claudeContextWindowMap);
const percentage = Math.round((totalTokens / contextWindow) * 100);
return {
@@ -293,7 +301,7 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
total: contextWindow,
percentage,
};
}, [messages, streamingContent, model]);
}, [messages, streamingContent, model, claudeContextWindowMap]);
useEffect(() => {
try {
@@ -337,14 +345,18 @@ export function Chat({ projectPath, onCloseProject }: ChatProps) {
.then((exists) => {
setHasAnthropicKey(exists);
if (!exists) return;
return api.getAnthropicModels().then((models) => {
return api.getAnthropicModels().then((models: AnthropicModelInfo[]) => {
if (models.length > 0) {
const sortedModels = models.sort((a, b) =>
a.toLowerCase().localeCompare(b.toLowerCase()),
a.id.toLowerCase().localeCompare(b.id.toLowerCase()),
);
setClaudeModels(sortedModels.map((m) => m.id));
setClaudeContextWindowMap(
new Map(sortedModels.map((m) => [m.id, m.context_window])),
);
setClaudeModels(sortedModels);
} else {
setClaudeModels([]);
setClaudeContextWindowMap(new Map());
}
});
})