MCPcopy Create free account
hub / github.com/Zoo-Code-Org/Zoo-Code / handleContextWindowExceededError

Method handleContextWindowExceededError

src/core/task/Task.ts:3803–3935  ·  view source on GitHub ↗
()

Source from the content-addressed store, hash-verified

3801 }
3802
3803 private async handleContextWindowExceededError(): Promise<void> {
3804 const state = await this.providerRef.deref()?.getState()
3805 const { profileThresholds = {}, mode, apiConfiguration } = state ?? {}
3806
3807 const { contextTokens } = this.getTokenUsage()
3808 const modelInfo = this.api.getModel().info
3809
3810 const maxTokens = getModelMaxOutputTokens({
3811 modelId: this.api.getModel().id,
3812 model: modelInfo,
3813 settings: this.apiConfiguration,
3814 })
3815
3816 // vscode-lm condenses against its static-table maxInputTokens (not the inflated live window);
3817 // only it implements getCondenseContextWindow, so others fall back to the full contextWindow.
3818 const contextWindow = this.api.getCondenseContextWindow?.() ?? modelInfo.contextWindow
3819 const useAvailableInputForContextPercent = typeof this.api.getCondenseContextWindow === "function"
3820
3821 // Get the current profile ID using the helper method
3822 const currentProfileId = this.getCurrentProfileId(state)
3823
3824 // Log the context window error for debugging
3825 console.warn(
3826 `[Task#${this.taskId}] Context window exceeded for model ${this.api.getModel().id}. ` +
3827 `Current tokens: ${contextTokens}, Context window: ${contextWindow}. ` +
3828 `Forcing truncation to ${FORCED_CONTEXT_REDUCTION_PERCENT}% of current context.`,
3829 )
3830 // Send condenseTaskContextStarted to show in-progress indicator
3831 await this.providerRef.deref()?.postMessageToWebview({ type: "condenseTaskContextStarted", text: this.taskId })
3832
3833 // Build tools for condensing metadata (same tools used for normal API calls)
3834 const provider = this.providerRef.deref()
3835 let allTools: import("openai").default.Chat.ChatCompletionTool[] = []
3836 if (provider) {
3837 const toolsResult = await buildNativeToolsArrayWithRestrictions({
3838 provider,
3839 cwd: this.cwd,
3840 mode,
3841 customModes: state?.customModes,
3842 experiments: state?.experiments,
3843 apiConfiguration,
3844 disabledTools: state?.disabledTools,
3845 modelInfo,
3846 includeAllToolsWithRestrictions: false,
3847 })
3848 allTools = toolsResult.tools
3849 }
3850
3851 // Build metadata with tools and taskId for the condensing API call
3852 const metadata: ApiHandlerCreateMessageMetadata = {
3853 mode,
3854 taskId: this.taskId,
3855 ...(this.currentRequestAbortController?.signal
3856 ? {
3857 abortSignal: this.currentRequestAbortController.signal,
3858 }
3859 : {}),
3860 ...(allTools.length > 0

Callers 1

attemptApiRequestMethod · 0.95

Calls 14

getTokenUsageMethod · 0.95
getCurrentProfileIdMethod · 0.95
getSystemPromptMethod · 0.95
sayMethod · 0.95
getModelMaxOutputTokensFunction · 0.90
getEnvironmentDetailsFunction · 0.90
manageContextFunction · 0.90
postMessageToWebviewMethod · 0.80
getStateMethod · 0.65
getModelMethod · 0.65

Tested by

no test coverage detected