Skip to content
This repository was archived by the owner on May 13, 2026. It is now read-only.

Commit d1f08cb

Browse files
authored
Merge pull request #83 from CJackHwang/dev
Merge pull request #81 from CJackHwang/codex/linear-mention-cja-8 Drop nameless assistant tool_calls and emit parsed tool_calls atomically in sieve
2 parents 13562cf + 5484d6e commit d1f08cb

16 files changed

Lines changed: 579 additions & 24 deletions

‎README.MD‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ flowchart LR
106106
可通过配置中的 `claude_mapping` 或 `claude_model_mapping` 覆盖映射关系。
107107
另外,`/anthropic/v1/models` 现已包含 Claude 1.x/2.x/3.x/4.x 历史模型 ID 与常见别名,便于旧客户端直接兼容。
108108

109+
110+
#### Claude Code 接入避坑(实测)
111+
112+
- `ANTHROPIC_BASE_URL` 推荐直接指向 DS2API 根地址(例如 `http://127.0.0.1:5001`),Claude Code 会请求 `/v1/messages?beta=true`。
113+
- `ANTHROPIC_API_KEY` 需要与 `config.json` 中 `keys` 一致;建议同时保留常规 key 与 `sk-ant-*` 形态 key,兼容不同客户端校验习惯。
114+
- 若系统设置了代理,建议对 DS2API 地址配置 `NO_PROXY=127.0.0.1,localhost,<你的主机IP>`,避免本地回环请求被代理拦截。
115+
- 如遇“工具调用输出成文本、未执行”问题,请升级到包含 Claude 工具调用多格式解析(JSON/XML/ANTML/invoke)的版本。
116+
109117
### Gemini 接口
110118

111119
Gemini 适配器将模型名通过 `model_aliases` 或内置规则映射到 DeepSeek 原生模型,支持 `generateContent` 和 `streamGenerateContent` 两种调用方式,并完整支持 Tool Calling(`functionDeclarations` → `functionCall` 输出)。

‎README.en.md‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,14 @@ flowchart LR
106106
Override mapping via `claude_mapping` or `claude_model_mapping` in config.
107107
In addition, `/anthropic/v1/models` now includes historical Claude 1.x/2.x/3.x/4.x IDs and common aliases for legacy client compatibility.
108108

109+
110+
#### Claude Code integration pitfalls (validated)
111+
112+
- Set `ANTHROPIC_BASE_URL` to the DS2API root URL (for example `http://127.0.0.1:5001`). Claude Code sends requests to `/v1/messages?beta=true`.
113+
- `ANTHROPIC_API_KEY` must match an entry in `keys` from `config.json`. Keeping both a regular key and an `sk-ant-*` style key improves client compatibility.
114+
- If your environment has proxy variables, set `NO_PROXY=127.0.0.1,localhost,<your_host_ip>` for DS2API to avoid proxy interception of local traffic.
115+
- If tool calls are rendered as plain text and not executed, upgrade to a build that includes multi-format Claude tool-call parsing (JSON/XML/ANTML/invoke).
116+
109117
### Gemini Endpoint
110118

111119
The Gemini adapter maps model names to DeepSeek native models via `model_aliases` or built-in heuristics, supporting both `generateContent` and `streamGenerateContent` call patterns with full Tool Calling support (`functionDeclarations` → `functionCall` output).

‎internal/adapter/claude/handler_stream_test.go‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,3 +315,78 @@ func asString(v any) string {
315315
s, _ := v.(string)
316316
return s
317317
}
318+
319+
func TestHandleClaudeStreamRealtimeToolSafetyAcrossStructuredFormats(t *testing.T) {
320+
tests := []struct {
321+
name string
322+
payload string
323+
}{
324+
{name: "xml_tool_call", payload: `<tool_call><tool_name>Bash</tool_name><parameters><command>pwd</command></parameters></tool_call>`},
325+
{name: "xml_json_tool_call", payload: `<tool_call>{"tool":"Bash","params":{"command":"pwd"}}</tool_call>`},
326+
{name: "nested_tool_tag_style", payload: `<tool_call><tool name="Bash"><command>pwd</command></tool></tool_call>`},
327+
{name: "function_tag_style", payload: `<function_call>Bash</function_call><function parameter name="command">pwd</function parameter>`},
328+
{name: "antml_argument_style", payload: `<antml:function_calls><antml:function_call id="1" name="Bash"><antml:argument name="command">pwd</antml:argument></antml:function_call></antml:function_calls>`},
329+
{name: "antml_function_attr_parameters", payload: `<antml:function_calls><antml:function_call id="1" function="Bash"><antml:parameters>{"command":"pwd"}</antml:parameters></antml:function_call></antml:function_calls>`},
330+
{name: "invoke_parameter_style", payload: `<function_calls><invoke name="Bash"><parameter name="command">pwd</parameter></invoke></function_calls>`},
331+
}
332+
333+
for _, tc := range tests {
334+
t.Run(tc.name, func(t *testing.T) {
335+
h := &Handler{}
336+
resp := makeClaudeSSEHTTPResponse(
337+
`data: {"p":"response/content","v":"`+strings.ReplaceAll(tc.payload, `"`, `\"`)+`"}`,
338+
`data: [DONE]`,
339+
)
340+
rec := httptest.NewRecorder()
341+
req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil)
342+
343+
h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "use tool"}}, false, false, []string{"Bash"})
344+
345+
frames := parseClaudeFrames(t, rec.Body.String())
346+
foundToolUse := false
347+
for _, f := range findClaudeFrames(frames, "content_block_start") {
348+
contentBlock, _ := f.Payload["content_block"].(map[string]any)
349+
if contentBlock["type"] == "tool_use" {
350+
foundToolUse = true
351+
break
352+
}
353+
}
354+
if !foundToolUse {
355+
t.Fatalf("expected tool_use block for format %s, body=%s", tc.name, rec.Body.String())
356+
}
357+
})
358+
}
359+
}
360+
361+
func TestHandleClaudeStreamRealtimeDoesNotStopOnUnclosedFencedToolExample(t *testing.T) {
362+
h := &Handler{}
363+
resp := makeClaudeSSEHTTPResponse(
364+
"data: {\"p\":\"response/content\",\"v\":\"Here is an example:\\n```json\\n{\\\"tool_calls\\\":[{\\\"name\\\":\\\"Bash\\\",\\\"input\\\":{\\\"command\\\":\\\"pwd\\\"}}]}\"}",
365+
"data: {\"p\":\"response/content\",\"v\":\"\\n```\\nDo not execute it.\"}",
366+
`data: [DONE]`,
367+
)
368+
rec := httptest.NewRecorder()
369+
req := httptest.NewRequest(http.MethodPost, "/anthropic/v1/messages", nil)
370+
371+
h.handleClaudeStreamRealtime(rec, req, resp, "claude-sonnet-4-5", []any{map[string]any{"role": "user", "content": "show example only"}}, false, false, []string{"Bash"})
372+
373+
frames := parseClaudeFrames(t, rec.Body.String())
374+
for _, f := range findClaudeFrames(frames, "content_block_start") {
375+
contentBlock, _ := f.Payload["content_block"].(map[string]any)
376+
if contentBlock["type"] == "tool_use" {
377+
t.Fatalf("unexpected tool_use for fenced example, body=%s", rec.Body.String())
378+
}
379+
}
380+
381+
foundEndTurn := false
382+
for _, f := range findClaudeFrames(frames, "message_delta") {
383+
delta, _ := f.Payload["delta"].(map[string]any)
384+
if delta["stop_reason"] == "end_turn" {
385+
foundEndTurn = true
386+
break
387+
}
388+
}
389+
if !foundEndTurn {
390+
t.Fatalf("expected stop_reason=end_turn, body=%s", rec.Body.String())
391+
}
392+
}

‎internal/adapter/claude/handler_util_test.go‎

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -125,8 +125,11 @@ func TestBuildClaudeToolPromptSingleTool(t *testing.T) {
125125
if !containsStr(prompt, "Search the web") {
126126
t.Fatalf("expected description in prompt")
127127
}
128-
if !containsStr(prompt, "tool_calls") {
129-
t.Fatalf("expected tool_calls instruction in prompt")
128+
if !containsStr(prompt, "tool_use") {
129+
t.Fatalf("expected tool_use instruction in prompt")
130+
}
131+
if containsStr(prompt, "tool_calls") {
132+
t.Fatalf("expected prompt to avoid tool_calls JSON instruction")
130133
}
131134
}
132135

‎internal/adapter/claude/handler_utils.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ func buildClaudeToolPrompt(tools []any) string {
5151
parts = append(parts, fmt.Sprintf("Tool: %s\nDescription: %s\nParameters: %s", name, desc, schema))
5252
}
5353
parts = append(parts,
54-
"When you need to use tools, you can call multiple tools in one response. Output ONLY JSON like {\"tool_calls\":[{\"name\":\"tool\",\"input\":{}}]}",
54+
"When you need a tool, respond with Claude-native tool use (tool_use) using the provided tool schema. Do not print tool-call JSON in text.",
5555
"History markers in conversation: [TOOL_CALL_HISTORY]...[/TOOL_CALL_HISTORY] are your previous tool calls; [TOOL_RESULT_HISTORY]...[/TOOL_RESULT_HISTORY] are runtime tool outputs, not user input.",
5656
"After a valid [TOOL_RESULT_HISTORY], continue with final answer instead of repeating the same call unless required fields are still missing.",
5757
)

‎internal/adapter/claude/stream_runtime_core.go‎

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88

99
"ds2api/internal/sse"
1010
streamengine "ds2api/internal/stream"
11+
"ds2api/internal/util"
1112
)
1213

1314
type claudeStreamRuntime struct {
@@ -116,6 +117,18 @@ func (s *claudeStreamRuntime) onParsed(parsed sse.LineResult) streamengine.Parse
116117

117118
s.text.WriteString(p.Text)
118119
if s.bufferToolContent {
120+
if hasUnclosedCodeFence(s.text.String()) {
121+
continue
122+
}
123+
detected := util.ParseToolCalls(s.text.String(), s.toolNames)
124+
if len(detected) > 0 {
125+
s.finalize("tool_use")
126+
return streamengine.ParsedDecision{
127+
ContentSeen: true,
128+
Stop: true,
129+
StopReason: streamengine.StopReason("tool_use_detected"),
130+
}
131+
}
119132
continue
120133
}
121134
s.closeThinkingBlock()
@@ -144,3 +157,7 @@ func (s *claudeStreamRuntime) onParsed(parsed sse.LineResult) streamengine.Parse
144157

145158
return streamengine.ParsedDecision{ContentSeen: contentSeen}
146159
}
160+
161+
func hasUnclosedCodeFence(text string) bool {
162+
return strings.Count(text, "```")%2 == 1
163+
}

‎internal/adapter/openai/message_normalize.go‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ func formatAssistantToolCallsForPrompt(msg map[string]any, traceID string) strin
7878
args = normalizeOpenAIArgumentsForPrompt(fn["arguments"])
7979
}
8080
if name == "" {
81-
name = "unknown"
81+
continue
8282
}
8383
if args == "" {
8484
args = normalizeOpenAIArgumentsForPrompt(call["arguments"])

‎internal/adapter/openai/message_normalize_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,29 @@ func TestNormalizeOpenAIMessagesForPrompt_PreservesConcatenatedToolArguments(t *
194194
}
195195
}
196196

197+
198+
func TestNormalizeOpenAIMessagesForPrompt_AssistantToolCallsMissingNameAreDropped(t *testing.T) {
199+
raw := []any{
200+
map[string]any{
201+
"role": "assistant",
202+
"tool_calls": []any{
203+
map[string]any{
204+
"id": "call_missing_name",
205+
"type": "function",
206+
"function": map[string]any{
207+
"arguments": `{"path":"README.MD"}`,
208+
},
209+
},
210+
},
211+
},
212+
}
213+
214+
normalized := normalizeOpenAIMessagesForPrompt(raw, "")
215+
if len(normalized) != 0 {
216+
t.Fatalf("expected nameless assistant tool_calls to be dropped, got %#v", normalized)
217+
}
218+
}
219+
197220
func TestNormalizeOpenAIMessagesForPrompt_AssistantNilContentDoesNotInjectNullLiteral(t *testing.T) {
198221
raw := []any{
199222
map[string]any{

‎internal/adapter/openai/responses_stream_runtime_toolcalls.go‎

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,16 @@ func (s *responsesStreamRuntime) closeMessageItem() {
9494
outputIndex := s.ensureMessageOutputIndex()
9595
text := s.visibleText.String()
9696
if s.messagePartAdded {
97+
s.sendEvent(
98+
"response.output_text.done",
99+
openaifmt.BuildResponsesTextDonePayload(
100+
s.responseID,
101+
itemID,
102+
outputIndex,
103+
0,
104+
text,
105+
),
106+
)
97107
s.sendEvent(
98108
"response.content_part.done",
99109
openaifmt.BuildResponsesContentPartDonePayload(

‎internal/adapter/openai/responses_stream_test.go‎

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,40 @@ func TestHandleResponsesStreamMultiToolCallKeepsNameAndCallIDAligned(t *testing.
226226
}
227227
}
228228

229+
func TestHandleResponsesStreamEmitsOutputTextDoneBeforeContentPartDone(t *testing.T) {
230+
h := &Handler{}
231+
req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil)
232+
rec := httptest.NewRecorder()
233+
234+
sseLine := func(v string) string {
235+
b, _ := json.Marshal(map[string]any{
236+
"p": "response/content",
237+
"v": v,
238+
})
239+
return "data: " + string(b) + "\n"
240+
}
241+
242+
streamBody := sseLine("hello") + "data: [DONE]\n"
243+
resp := &http.Response{
244+
StatusCode: http.StatusOK,
245+
Body: io.NopCloser(strings.NewReader(streamBody)),
246+
}
247+
248+
h.handleResponsesStream(rec, req, resp, "owner-a", "resp_test", "deepseek-chat", "prompt", false, false, nil, util.DefaultToolChoicePolicy(), "")
249+
body := rec.Body.String()
250+
if !strings.Contains(body, "event: response.output_text.done") {
251+
t.Fatalf("expected response.output_text.done payload, body=%s", body)
252+
}
253+
textDoneIdx := strings.Index(body, "event: response.output_text.done")
254+
partDoneIdx := strings.Index(body, "event: response.content_part.done")
255+
if textDoneIdx < 0 || partDoneIdx < 0 {
256+
t.Fatalf("expected output_text.done + content_part.done, body=%s", body)
257+
}
258+
if textDoneIdx > partDoneIdx {
259+
t.Fatalf("expected output_text.done before content_part.done, body=%s", body)
260+
}
261+
}
262+
229263
func TestHandleResponsesStreamOutputTextDeltaCarriesItemIndexes(t *testing.T) {
230264
h := &Handler{}
231265
req := httptest.NewRequest(http.MethodPost, "/v1/responses", nil)

0 commit comments

Comments
 (0)