* refactor: replace Anthropic MessageParam with ClineStorageMessage type Replace Anthropic SDK's MessageParam type with the new ClineStorageMessage type across API providers and tests in the effort of storing api messages in a type safe environment that we can expand from and avoid adding undocumented properties to Anthropc Message type that are not visible to the downstream services. This change: - Removes dependency on @anthropic-ai/sdk types in multiple providers - Introduces ClineStorageMessage from shared messages module - Updates method signatures in Dify, OpenAI, LiteLLM, and ClaudeCode handlers - Updates corresponding test files to use the new type This decouples the codebase from Anthropic-specific types and standardizes message handling using an internal storage format across all providers that improves type-safety, preparing for the properties added by the Response API use. As ClineStorageMessage is an extension of the Anthropic Message type, everything should work the same with no breaking changes. Green CI is expected. * clean up
173 lines
4.8 KiB
TypeScript
173 lines
4.8 KiB
TypeScript
import { filterMessagesForClaudeCode } from "@/integrations/claude-code/message-filter"
|
|
import { runClaudeCode } from "@/integrations/claude-code/run"
|
|
import { ClaudeCodeModelId, claudeCodeDefaultModelId, claudeCodeModels } from "@/shared/api"
|
|
import { ClineStorageMessage } from "@/shared/messages/content"
|
|
import { type ApiHandler, CommonApiHandlerOptions } from ".."
|
|
import { withRetry } from "../retry"
|
|
import { type ApiStream, ApiStreamUsageChunk } from "../transform/stream"
|
|
|
|
interface ClaudeCodeHandlerOptions extends CommonApiHandlerOptions {
|
|
claudeCodePath?: string
|
|
apiModelId?: string
|
|
thinkingBudgetTokens?: number
|
|
}
|
|
|
|
export class ClaudeCodeHandler implements ApiHandler {
|
|
private options: ClaudeCodeHandlerOptions
|
|
|
|
constructor(options: ClaudeCodeHandlerOptions) {
|
|
this.options = options
|
|
}
|
|
|
|
@withRetry({
|
|
maxRetries: 4,
|
|
baseDelay: 2000,
|
|
maxDelay: 15000,
|
|
})
|
|
async *createMessage(systemPrompt: string, messages: ClineStorageMessage[]): ApiStream {
|
|
// Filter out image blocks since Claude Code doesn't support them
|
|
const filteredMessages = filterMessagesForClaudeCode(messages)
|
|
|
|
const claudeProcess = runClaudeCode({
|
|
systemPrompt,
|
|
messages: filteredMessages,
|
|
path: this.options.claudeCodePath,
|
|
modelId: this.getModel().id,
|
|
thinkingBudgetTokens: this.options.thinkingBudgetTokens,
|
|
})
|
|
|
|
// Usage is included with assistant messages,
|
|
// but cost is included in the result chunk
|
|
const usage: ApiStreamUsageChunk = {
|
|
type: "usage",
|
|
inputTokens: 0,
|
|
outputTokens: 0,
|
|
cacheReadTokens: 0,
|
|
cacheWriteTokens: 0,
|
|
}
|
|
|
|
let isPaidUsage = true
|
|
|
|
for await (const chunk of claudeProcess) {
|
|
if (typeof chunk === "string") {
|
|
yield {
|
|
type: "text",
|
|
text: chunk,
|
|
}
|
|
|
|
continue
|
|
}
|
|
|
|
if (chunk.type === "system" && chunk.subtype === "init") {
|
|
// Based on my tests, subscription usage sets the `apiKeySource` to "none"
|
|
isPaidUsage = chunk.apiKeySource !== "none"
|
|
continue
|
|
}
|
|
|
|
if (chunk.type === "assistant" && "message" in chunk) {
|
|
const message = chunk.message
|
|
|
|
if (message.stop_reason !== null) {
|
|
const content = "text" in message.content[0] ? message.content[0] : undefined
|
|
|
|
const isError = content && content.text.startsWith(`API Error`)
|
|
if (isError) {
|
|
// Error messages are formatted as: `API Error: <<status code>> <<json>>`
|
|
const errorMessageStart = content.text.indexOf("{")
|
|
const errorMessage = content.text.slice(errorMessageStart)
|
|
|
|
const error = this.attemptParse(errorMessage)
|
|
if (!error) {
|
|
throw new Error(content.text)
|
|
}
|
|
|
|
if (error.error.message.includes("Invalid model name")) {
|
|
throw new Error(
|
|
content.text +
|
|
`\n\nAPI keys and subscription plans allow different models. Make sure the selected model is included in your plan.`,
|
|
)
|
|
}
|
|
|
|
throw new Error(errorMessage)
|
|
}
|
|
}
|
|
|
|
for (const content of message.content) {
|
|
switch (content.type) {
|
|
case "text":
|
|
yield {
|
|
type: "text",
|
|
text: content.text,
|
|
}
|
|
break
|
|
case "thinking":
|
|
yield {
|
|
type: "reasoning",
|
|
reasoning: content.thinking || "",
|
|
}
|
|
break
|
|
case "redacted_thinking":
|
|
yield {
|
|
type: "reasoning",
|
|
reasoning: "[Redacted thinking block]",
|
|
}
|
|
break
|
|
case "tool_use":
|
|
// Yield tool_use blocks to the streaming pipeline for proper tool execution
|
|
yield {
|
|
type: "tool_calls",
|
|
tool_call: {
|
|
call_id: content.id,
|
|
function: {
|
|
id: content.id,
|
|
name: content.name,
|
|
arguments: content.input,
|
|
},
|
|
},
|
|
}
|
|
break
|
|
}
|
|
}
|
|
|
|
// According to Anthropic's API documentation:
|
|
// https://docs.anthropic.com/en/api/messages#usage-object
|
|
// The `input_tokens` field already includes both `cache_read_input_tokens` and `cache_creation_input_tokens`.
|
|
// Therefore, we should not add cache tokens to the input_tokens count again, as this would result in double-counting.
|
|
usage.inputTokens = message.usage?.input_tokens ?? 0
|
|
usage.outputTokens = message.usage?.output_tokens ?? 0
|
|
usage.cacheReadTokens = message.usage?.cache_read_input_tokens ?? 0
|
|
usage.cacheWriteTokens = message.usage?.cache_creation_input_tokens ?? 0
|
|
|
|
continue
|
|
}
|
|
|
|
if (chunk.type === "result" && "result" in chunk) {
|
|
usage.totalCost = isPaidUsage ? chunk.total_cost_usd : 0
|
|
|
|
yield usage
|
|
}
|
|
}
|
|
}
|
|
|
|
private attemptParse(str: string) {
|
|
try {
|
|
return JSON.parse(str)
|
|
} catch (_err) {
|
|
return null
|
|
}
|
|
}
|
|
|
|
getModel() {
|
|
const modelId = this.options.apiModelId
|
|
if (modelId && modelId in claudeCodeModels) {
|
|
const id = modelId as ClaudeCodeModelId
|
|
return { id, info: claudeCodeModels[id] }
|
|
}
|
|
|
|
return {
|
|
id: claudeCodeDefaultModelId,
|
|
info: claudeCodeModels[claudeCodeDefaultModelId],
|
|
}
|
|
}
|
|
}
|