-
Notifications
You must be signed in to change notification settings - Fork 212
Expand file tree
/
Copy pathdeepseek.ts
More file actions
216 lines (184 loc) · 7.69 KB
/
Copy pathdeepseek.ts
File metadata and controls
216 lines (184 loc) · 7.69 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
import { Anthropic } from "@anthropic-ai/sdk"
import OpenAI from "openai"
import { logger } from "../../utils/logging"
import {
deepSeekModels,
deepSeekDefaultModelId,
DEEP_SEEK_DEFAULT_TEMPERATURE,
OPENAI_AZURE_AI_INFERENCE_PATH,
type ModelInfo,
} from "@roo-code/types"
import type { ApiHandlerOptions } from "../../shared/api"
import { ApiStream, ApiStreamUsageChunk } from "../transform/stream"
import { getModelParams } from "../transform/model-params"
import { convertToR1Format } from "../transform/r1-format"
import { historyHasToolCallsWithoutReasoning } from "./utils/reasoning-history-guard"
import { historyHasToolCallsWithoutReasoning } from "./utils/reasoning-history-guard"
import { OpenAiHandler } from "./openai"
import { extractReasoningFromDelta } from "./utils/extract-reasoning"
import type { ApiHandlerCreateMessageMetadata } from "../index"
import { handleOpenAIError } from "./utils/error-handler"
// Custom interface for DeepSeek params to support thinking mode
type DeepSeekChatCompletionParams = Omit<OpenAI.Chat.ChatCompletionCreateParamsStreaming, "reasoning_effort"> & {
thinking?: { type: "enabled" | "disabled" }
reasoning_effort?: "high" | "max"
}
const deepSeekV4ThinkingModels = new Set(["deepseek-v4-flash", "deepseek-v4-pro"])
const supportsDeepSeekThinkingToggle = (modelId: string) => deepSeekV4ThinkingModels.has(modelId)
// Only known V4 models and the legacy reasoner alias support DeepSeek's
// thinking fields. Custom model IDs still fall back to default metadata, but
// should not receive V4-only request parameters.
const isDeepSeekThinkingEnabled = (modelId: string, options: ApiHandlerOptions) => {
if (options.enableReasoningEffort === false || options.reasoningEffort === "disable") {
return false
}
return modelId === "deepseek-reasoner" || supportsDeepSeekThinkingToggle(modelId)
}
const normalizeDeepSeekReasoningEffort = (reasoningEffort?: string): "high" | "max" | undefined => {
if (!reasoningEffort || reasoningEffort === "disable") {
return undefined
}
// DeepSeek currently maps low/medium to high and xhigh to max in thinking mode.
return reasoningEffort === "xhigh" ? "max" : "high"
}
// Use the computed maxTokens from getModelParams rather than raw model metadata.
// V4 advertises a 384K maximum output, but the project convention caps most
// models to 20% of context unless the user explicitly overrides modelMaxTokens.
const addDeepSeekMaxTokensIfNeeded = (
requestOptions: DeepSeekChatCompletionParams,
options: ApiHandlerOptions,
computedMaxTokens?: number,
) => {
if (options.includeMaxTokens === true) {
requestOptions.max_completion_tokens = options.modelMaxTokens || computedMaxTokens
}
}
export class DeepSeekHandler extends OpenAiHandler {
constructor(options: ApiHandlerOptions) {
super({
...options,
openAiApiKey: options.deepSeekApiKey ?? "not-provided",
openAiModelId: options.apiModelId ?? deepSeekDefaultModelId,
openAiBaseUrl: options.deepSeekBaseUrl || "https://api.deepseek.com",
openAiStreamingEnabled: true,
includeMaxTokens: true,
})
}
override getModel() {
const id = this.options.apiModelId ?? deepSeekDefaultModelId
const info = deepSeekModels[id as keyof typeof deepSeekModels] || deepSeekModels[deepSeekDefaultModelId]
const params = getModelParams({
format: "openai",
modelId: id,
model: info,
settings: this.options,
defaultTemperature: DEEP_SEEK_DEFAULT_TEMPERATURE,
})
return { id, info, ...params }
}
override async *createMessage(
systemPrompt: string,
messages: Anthropic.Messages.MessageParam[],
metadata?: ApiHandlerCreateMessageMetadata,
): ApiStream {
const modelId = this.options.apiModelId ?? deepSeekDefaultModelId
const { info: modelInfo, temperature, reasoningEffort, maxTokens } = this.getModel()
const isThinkingModel = isDeepSeekThinkingEnabled(modelId, this.options)
// Convert messages to R1 format (merges consecutive same-role messages)
// This is required for DeepSeek which does not support successive messages with the same role
// For thinking models, enable mergeToolResultText to preserve reasoning_content
// during tool call sequences. Without this, environment_details text after tool_results would
// create user messages that cause DeepSeek to drop all previous reasoning_content.
// See: https://api-docs.deepseek.com/guides/thinking_mode
const convertedMessages = convertToR1Format([{ role: "user", content: systemPrompt }, ...messages], {
mergeToolResultText: isThinkingModel,
})
// Layer 1 Guard: detect incompatible history (tool_calls without reasoning_content)
// and disable thinking mode for this request to prevent a 400 error from the API.
const hasIncompatibleHistory = historyHasToolCallsWithoutReasoning(convertedMessages)
const effectiveThinkingEnabled = isThinkingModel && !hasIncompatibleHistory
if (hasIncompatibleHistory) {
logger.warn("provider_reasoning_guard_triggered", {
ctx: "deepseek",
provider: "deepseek",
modelId,
taskId: metadata?.taskId,
})
}
const thinking = supportsDeepSeekThinkingToggle(modelId)
? ({ type: effectiveThinkingEnabled ? "enabled" : "disabled" } as const)
: effectiveThinkingEnabled
? ({ type: "enabled" } as const)
: undefined
const deepSeekReasoningEffort = effectiveThinkingEnabled ? normalizeDeepSeekReasoningEffort(reasoningEffort) : undefined
const requestOptions: DeepSeekChatCompletionParams = {
model: modelId,
...(!effectiveThinkingEnabled && { temperature: temperature ?? DEEP_SEEK_DEFAULT_TEMPERATURE }),
messages: convertedMessages,
stream: true as const,
stream_options: { include_usage: true },
...(thinking && { thinking }),
...(deepSeekReasoningEffort && { reasoning_effort: deepSeekReasoningEffort }),
tools: this.convertToolsForOpenAI(metadata?.tools),
tool_choice: metadata?.tool_choice,
parallel_tool_calls: metadata?.parallelToolCalls ?? true,
}
addDeepSeekMaxTokensIfNeeded(requestOptions, this.options, maxTokens)
// Check if base URL is Azure AI Inference (for DeepSeek via Azure)
const isAzureAiInference = this._isAzureAiInference(this.options.deepSeekBaseUrl)
let stream
try {
stream = await this.client.chat.completions.create(
requestOptions as OpenAI.Chat.Completions.ChatCompletionCreateParamsStreaming,
isAzureAiInference ? { path: OPENAI_AZURE_AI_INFERENCE_PATH } : {},
)
} catch (error) {
throw handleOpenAIError(error, "DeepSeek")
}
let lastUsage
for await (const chunk of stream) {
const delta = chunk.choices?.[0]?.delta ?? {}
// Handle regular text content
if (delta.content) {
yield {
type: "text",
text: delta.content,
}
}
// Handle reasoning_content from DeepSeek's interleaved thinking
// This is the proper way DeepSeek sends thinking content in streaming
const reasoningText = extractReasoningFromDelta(delta)
if (reasoningText) {
yield { type: "reasoning", text: reasoningText }
}
// Handle tool calls
if (delta.tool_calls) {
for (const toolCall of delta.tool_calls) {
yield {
type: "tool_call_partial",
index: toolCall.index,
id: toolCall.id,
name: toolCall.function?.name,
arguments: toolCall.function?.arguments,
}
}
}
if (chunk.usage) {
lastUsage = chunk.usage
}
}
if (lastUsage) {
yield this.processUsageMetrics(lastUsage, modelInfo)
}
}
// Override to handle DeepSeek's usage metrics, including caching.
protected override processUsageMetrics(usage: any, _modelInfo?: any): ApiStreamUsageChunk {
return {
type: "usage",
inputTokens: usage?.prompt_tokens || 0,
outputTokens: usage?.completion_tokens || 0,
cacheWriteTokens: usage?.prompt_tokens_details?.cache_miss_tokens,
cacheReadTokens: usage?.prompt_tokens_details?.cached_tokens,
}
}
}