mirror of
https://gitcode.com/JianFeeeee/HomeAgent.git
synced 2026-09-21 09:28:14 +00:00
- 删除 OpenAIProvider/OllamaProvider 死代码,LuaAdaptedProvider 独存 - DisableThinking 从 ExtraBody 移到 CompletionRequest 顶层字段 - ContextWindow 从 Provider 签名移到 BaseConfig/ModelContextWindow() 统管 - 确认 CleanText 仅做基本空白 trim,QQ 模板剥离归插件 Cleaner - Cleaner/NoMemory 仅作用于向量计算和 jieba 分词层,原文不变 - context.ContextEvent/Doc.Content 始终保存原文 - 删除 nlp/download.go 死代码 - media.go: context.Background() -> a.ctx 级联 - clawhubadapter: HTTP 超时 - cut.go: 跨平台 mod cache 路径 (GOMODCACHE->GOPATH->HomeDir) - bridge_e2e_test: 移除未用 runtime import - lua 适配器: disable_thinking 传参
87 lines
2.5 KiB
Lua
87 lines
2.5 KiB
Lua
local adapter = {}
|
|
|
|
adapter.name = "anthropic"
|
|
adapter.version = "2.0.0"
|
|
adapter.endpoint = "/v1/messages"
|
|
adapter.headers = {
|
|
["anthropic-version"] = "2023-06-01"
|
|
}
|
|
|
|
function adapter.transform_request(raw_body)
|
|
local ok, req = pcall(json.decode, raw_body)
|
|
if not ok then return raw_body end
|
|
|
|
local msgs = {}
|
|
local system = ""
|
|
for _, m in ipairs(req.messages or {}) do
|
|
if m.role == "system" then
|
|
system = system .. m.content .. "\n"
|
|
else
|
|
table.insert(msgs, { role = m.role, content = m.content })
|
|
end
|
|
end
|
|
|
|
local anthropic_req = {
|
|
model = req.model or "claude-sonnet-4-20250514",
|
|
max_tokens = req.max_tokens or 4096,
|
|
messages = msgs,
|
|
stream = req.stream or false,
|
|
}
|
|
|
|
if not req.disable_thinking then
|
|
anthropic_req.thinking = { type = "enabled", budget_tokens = 4096 }
|
|
end
|
|
|
|
if system ~= "" then
|
|
anthropic_req.system = system
|
|
end
|
|
|
|
return json.encode(anthropic_req)
|
|
end
|
|
|
|
function adapter.transform_response(raw_body)
|
|
local ok, resp = pcall(json.decode, raw_body)
|
|
if not ok then return raw_body end
|
|
|
|
local unified = {
|
|
content = "",
|
|
finish_reason = "",
|
|
token_usage = { prompt = 0, completion = 0, total = 0 }
|
|
}
|
|
|
|
if resp.usage then
|
|
unified.token_usage.prompt = resp.usage.input_tokens or 0
|
|
unified.token_usage.completion = resp.usage.output_tokens or 0
|
|
unified.token_usage.total = (resp.usage.input_tokens or 0) + (resp.usage.output_tokens or 0)
|
|
end
|
|
|
|
if resp.content and #resp.content > 0 then
|
|
for _, block in ipairs(resp.content) do
|
|
if block.type == "text" then
|
|
unified.content = unified.content .. (block.text or "")
|
|
end
|
|
end
|
|
end
|
|
unified.finish_reason = resp.stop_reason or ""
|
|
|
|
return json.encode(unified)
|
|
end
|
|
|
|
function adapter.transform_stream_chunk(raw_chunk)
|
|
local ok, chunk = pcall(json.decode, raw_chunk)
|
|
if not ok then return "" end
|
|
if chunk.type == "message_start" then return "" end
|
|
if chunk.type == "message_delta" then
|
|
return json.encode({ content = "", done = (chunk.delta and chunk.delta.stop_reason ~= nil) })
|
|
end
|
|
if chunk.type == "content_block_delta" and chunk.delta then
|
|
return json.encode({ content = chunk.delta.text or "", done = false })
|
|
end
|
|
if chunk.type == "message_stop" then
|
|
return json.encode({ content = "", done = true })
|
|
end
|
|
return ""
|
|
end
|
|
|
|
return adapter
|