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

Commit fe87ded

Browse files
authored
Merge pull request #415 from CJackHwang/dev
[codex] unify response history session management across API backends
2 parents c32fe30 + 8ace349 commit fe87ded

42 files changed

Lines changed: 1100 additions & 126 deletions

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎API.md‎

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
- Tool Calling 的解析策略在 Go 与 Node Runtime 间保持一致:推荐模型输出 DSML 外壳 `<|DSML|tool_calls>` → `<|DSML|invoke name="...">` → `<|DSML|parameter name="...">`;兼容层也接受 DSML wrapper 别名 `<dsml|tool_calls>`、`<|tool_calls>`、`<|tool_calls>`、常见 DSML 分隔符漏写形态(如 `<|DSML tool_calls>`)、`DSML` 与工具标签名黏连的常见 typo(如 `<DSMLtool_calls>`),以及旧式 canonical XML `<tool_calls>` → `<invoke name="...">` → `<parameter name="...">`。实现上采用窄容错结构扫描:只有 `tool_calls` wrapper 或可修复的缺失 opening wrapper 会进入工具路径,裸 `<invoke>` 不计为已支持语法;流式场景继续执行防泄漏筛分。若参数体本身是合法 JSON 字面量(如 `123`、`true`、`null`、数组或对象),会按结构化值输出,不再一律当作字符串;若 CDATA 偶发漏闭合,则会在最终 parse / flush 恢复阶段做窄修复,尽量保住已完整包裹的外层工具调用。
4343
- `Admin API` 将配置与运行时策略分开:`/admin/config*` 管静态配置,`/admin/settings*` 管运行时行为。
4444
- 当上游返回 thinking-only 响应(模型输出了推理链但无可见文本)时,非流式补全会自动重试一次:以多轮对话 follow-up 方式追加 prompt 后缀 `"Previous reply had no visible output. Please regenerate the visible final answer or tool call now."` 并设置 `parent_message_id` 在同一 DeepSeek session 内让模型重新输出;重试最大 1 次。
45-
- 引用标记剥离(strip reference markers)当前为固定开启的运行时行为,所有协议适配层统一生效。
45+
- 引用标记处理边界:流式输出默认隐藏 `[citation:N]` / `[reference:N]` 这类上游内部占位符;非流式输出默认把 DeepSeek 搜索引用标记转换为 Markdown 引用链接。
4646

4747
---
4848

@@ -168,6 +168,8 @@ Gemini 兼容客户端还可以使用 `x-goog-api-key`、`?key=` 或 `?api_key=`
168168
| GET | `/admin/chat-history/{id}` | Admin | 查看单条服务器端对话记录 |
169169
| DELETE | `/admin/chat-history/{id}` | Admin | 删除单条服务器端对话记录 |
170170
| PUT | `/admin/chat-history/settings` | Admin | 更新对话记录保留条数 |
171+
172+
服务器端记录本质上是 DeepSeek 上游响应归档:OpenAI Chat、OpenAI Responses、Claude Messages、Gemini GenerateContent 等直连 DeepSeek 的生成接口,在收到上游响应后会于各协议回译/裁剪前写入记录;列表按请求创建时间倒序展示,流式请求会在生成过程中持续刷新状态与详情。WebUI「API 测试」发出的请求也会进入该记录。
171173
| GET | `/admin/version` | Admin | 查询当前版本与最新 Release |
172174

173175
OpenAI `/v1/*` 仍是规范路径。对于只配置 DS2API 根地址的客户端,同一套 OpenAI handler 也通过根路径快捷路由暴露:`/models`、`/models/{id}`、`/chat/completions`、`/responses`、`/responses/{response_id}`、`/embeddings`、`/files`、`/files/{file_id}`。

‎VERSION‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1 +1 @@
1-
4.4.0
1+
4.4.1

‎docs/DEVELOPMENT.md‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ gofmt -w <changed-go-files>
7474

7575
- Admin API:`/admin/chat-history`、`/admin/chat-history/{id}`。
7676
- 后端存储:`internal/chathistory/store.go`。
77+
- 输出归档:`internal/responsehistory` 在协议回译/裁剪前记录 DeepSeek 上游 assistant text / thinking;即使工具调用已被对外响应转成结构化 `tool_calls` 并从可见正文剔除,后台历史仍应保留原始 DSML / XML 片段,方便排查格式漂移。
7778
- 前端轮询和 ETag:`webui/src/features/chatHistory/ChatHistoryContainer.jsx`。
7879

7980
Tool call 问题优先跑:

‎docs/prompt-compatibility.md‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -114,7 +114,8 @@ DS2API 当前的核心思路,不是把客户端传来的 `messages`、`tools`
114114
- 对 OpenAI Chat / Responses 的非流式收尾,如果最终可见正文为空,兼容层会优先尝试把思维链中的独立 DSML / XML 工具块当作真实工具调用解析出来。流式链路也会在收尾阶段做同样的 fallback 检测,但不会因为思维链内容去中途拦截或改写流式输出;真正的工具识别始终基于原始上游文本,而不是基于“已经做过可见输出清洗”的版本,因此即使最终可见层会剥离完整 leaked DSML / XML `tool_calls` wrapper、并抑制全空参数或无效 wrapper 块,也不会影响真实工具调用转成结构化 `tool_calls` / `function_call`。补发结果会作为本轮 assistant 的结构化 `tool_calls` / `function_call` 输出返回,而不是塞进 `content` 文本;如果客户端没有开启 thinking / reasoning,思维链只用于检测,不会作为 `reasoning_content` 或可见正文暴露。只有正文为空且思维链里也没有可执行工具调用时,才继续按空回复错误处理。
115115
- OpenAI Chat / Responses 的空回复错误处理之前会默认做一次内部补偿重试:第一次上游完整结束后,如果最终可见正文为空、没有解析到工具调用、也没有已经向客户端流式发出工具调用,并且终止原因不是 `content_filter`,兼容层会复用同一个 `chat_session_id`、账号、token 与工具策略,把原始 completion `prompt` 追加固定后缀 `Previous reply had no visible output. Please regenerate the visible final answer or tool call now.` 后重新提交一次。重试遵循 DeepSeek 多轮对话协议:从第一次上游 SSE 流中提取 `response_message_id`,并在重试 payload 中设置 `parent_message_id` 为该值,使重试成为同一会话的后续轮次而非断裂的根消息;同时重新获取一次 PoW(若 PoW 获取失败则回退到原始 PoW)。该重试不会重新标准化消息、不会新建 session、不会切换账号,也不会向流式客户端插入重试标记;第二次 thinking / reasoning 会按正常增量直接接到第一次之后,并继续使用 overlap trim 去重。若第二次仍为空,终端错误码仍保持现有 `upstream_empty_output`;若任一尝试触发空 `content_filter`,不做补偿重试并保持 `content_filter` 错误。JS Vercel 运行时同样设置 `parent_message_id`,但因无法直接调用 PoW API 而复用原始 PoW。
116116

117-
- OpenAI Chat / Responses 在最终可见正文渲染阶段,会把 DeepSeek 搜索返回中的 `[citation:N]` / `[reference:N]` 标记替换成对应 Markdown 链接。`citation` 标记按一基序号解析;`reference` 标记只有在同一段正文中出现 `[reference:0]`(允许冒号后有空格)时才按零基序号映射,并且不会影响同段正文里的 `citation` 标记。
117+
- 非流式 OpenAI Chat / Responses、Claude Messages、Gemini generateContent 在最终可见正文渲染阶段,会把 DeepSeek 搜索返回中的 `[citation:N]` / `[reference:N]` 标记替换成对应 Markdown 链接。`citation` 标记按一基序号解析;`reference` 标记只有在同一段正文中出现 `[reference:0]`(允许冒号后有空格)时才按零基序号映射,并且不会影响同段正文里的 `citation` 标记。
118+
- 流式输出仍默认隐藏 `[citation:N]` / `[reference:N]` 这类上游内部标记,避免分片输出中泄漏尚未完成映射的引用占位符。
118119

119120
## 5. prompt 是怎么拼出来的
120121

‎internal/assistantturn/turn_test.go‎

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ func TestBuildTurnFromCollectedTextCitation(t *testing.T) {
1111
turn := BuildTurnFromCollected(sse.CollectResult{
1212
Text: "See [citation:1]",
1313
CitationLinks: map[int]string{1: "https://example.com"},
14-
}, BuildOptions{Model: "deepseek-v4-flash", Prompt: "prompt", SearchEnabled: true, StripReferenceMarkers: true})
14+
}, BuildOptions{Model: "deepseek-v4-flash", Prompt: "prompt", SearchEnabled: true})
1515
if turn.Text != "See [1](https://example.com)" {
1616
t.Fatalf("text mismatch: %q", turn.Text)
1717
}
@@ -23,6 +23,20 @@ func TestBuildTurnFromCollectedTextCitation(t *testing.T) {
2323
}
2424
}
2525

26+
func TestBuildTurnFromCollectedKeepsNonStreamReferenceLinks(t *testing.T) {
27+
turn := BuildTurnFromCollected(sse.CollectResult{
28+
Text: "结论[reference:0],补充[reference:1]。",
29+
CitationLinks: map[int]string{
30+
1: "https://example.com/a",
31+
2: "https://example.com/b",
32+
},
33+
}, BuildOptions{Model: "deepseek-v4-flash-search", Prompt: "prompt", SearchEnabled: true})
34+
want := "结论[0](https://example.com/a),补充[1](https://example.com/b)。"
35+
if turn.Text != want {
36+
t.Fatalf("text mismatch: got %q want %q", turn.Text, want)
37+
}
38+
}
39+
2640
func TestBuildTurnFromCollectedToolCall(t *testing.T) {
2741
turn := BuildTurnFromCollected(sse.CollectResult{
2842
Text: `<tool_calls><invoke name="Write"><parameter name="content">{"x":1}</parameter></invoke></tool_calls>`,

‎internal/chathistory/store.go‎

Lines changed: 11 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ type Entry struct {
4343
Status string `json:"status"`
4444
CallerID string `json:"caller_id,omitempty"`
4545
AccountID string `json:"account_id,omitempty"`
46+
Surface string `json:"surface,omitempty"`
4647
Model string `json:"model,omitempty"`
4748
Stream bool `json:"stream"`
4849
UserInput string `json:"user_input,omitempty"`
@@ -72,6 +73,7 @@ type SummaryEntry struct {
7273
Status string `json:"status"`
7374
CallerID string `json:"caller_id,omitempty"`
7475
AccountID string `json:"account_id,omitempty"`
76+
Surface string `json:"surface,omitempty"`
7577
Model string `json:"model,omitempty"`
7678
Stream bool `json:"stream"`
7779
UserInput string `json:"user_input,omitempty"`
@@ -92,6 +94,7 @@ type File struct {
9294
type StartParams struct {
9395
CallerID string
9496
AccountID string
97+
Surface string
9598
Model string
9699
Stream bool
97100
UserInput string
@@ -271,6 +274,7 @@ func (s *Store) Start(params StartParams) (Entry, error) {
271274
Status: "streaming",
272275
CallerID: strings.TrimSpace(params.CallerID),
273276
AccountID: strings.TrimSpace(params.AccountID),
277+
Surface: strings.TrimSpace(params.Surface),
274278
Model: strings.TrimSpace(params.Model),
275279
Stream: params.Stream,
276280
UserInput: strings.TrimSpace(params.UserInput),
@@ -546,10 +550,13 @@ func (s *Store) rebuildIndexLocked() {
546550
summaries = append(summaries, summaryFromEntry(item))
547551
}
548552
sort.Slice(summaries, func(i, j int) bool {
549-
if summaries[i].UpdatedAt == summaries[j].UpdatedAt {
550-
return summaries[i].CreatedAt > summaries[j].CreatedAt
553+
if summaries[i].CreatedAt == summaries[j].CreatedAt {
554+
if summaries[i].Revision == summaries[j].Revision {
555+
return summaries[i].UpdatedAt > summaries[j].UpdatedAt
556+
}
557+
return summaries[i].Revision > summaries[j].Revision
551558
}
552-
return summaries[i].UpdatedAt > summaries[j].UpdatedAt
559+
return summaries[i].CreatedAt > summaries[j].CreatedAt
553560
})
554561
if s.state.Limit < DisabledLimit || !isAllowedLimit(s.state.Limit) {
555562
s.state.Limit = DefaultLimit
@@ -593,6 +600,7 @@ func summaryFromEntry(item Entry) SummaryEntry {
593600
Status: item.Status,
594601
CallerID: item.CallerID,
595602
AccountID: item.AccountID,
603+
Surface: item.Surface,
596604
Model: item.Model,
597605
Stream: item.Stream,
598606
UserInput: item.UserInput,

‎internal/chathistory/store_test.go‎

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
"strings"
99
"sync"
1010
"testing"
11+
"time"
1112
"unicode/utf8"
1213
)
1314

@@ -494,6 +495,36 @@ func TestStoreWritesOnlyChangedDetailFiles(t *testing.T) {
494495
}
495496
}
496497

498+
func TestStoreOrdersByCreationTimeNotStreamingUpdates(t *testing.T) {
499+
path := filepath.Join(t.TempDir(), "chat_history.json")
500+
store := New(path)
501+
502+
first, err := store.Start(StartParams{UserInput: "first"})
503+
if err != nil {
504+
t.Fatalf("start first failed: %v", err)
505+
}
506+
time.Sleep(time.Millisecond)
507+
second, err := store.Start(StartParams{UserInput: "second"})
508+
if err != nil {
509+
t.Fatalf("start second failed: %v", err)
510+
}
511+
time.Sleep(time.Millisecond)
512+
if _, err := store.Update(first.ID, UpdateParams{Status: "streaming", Content: "still running"}); err != nil {
513+
t.Fatalf("update first failed: %v", err)
514+
}
515+
516+
snapshot, err := store.Snapshot()
517+
if err != nil {
518+
t.Fatalf("snapshot failed: %v", err)
519+
}
520+
if len(snapshot.Items) != 2 {
521+
t.Fatalf("expected two items, got %#v", snapshot.Items)
522+
}
523+
if snapshot.Items[0].ID != second.ID || snapshot.Items[1].ID != first.ID {
524+
t.Fatalf("expected creation-time order to stay stable, got %#v", snapshot.Items)
525+
}
526+
}
527+
497528
func TestUpdatePreservesContentWhenNewContentIsEmpty(t *testing.T) {
498529
path := filepath.Join(t.TempDir(), "chat_history.json")
499530
store := New(path)

‎internal/completionruntime/nonstream_test.go‎

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,29 @@ func TestExecuteNonStreamWithRetryUsesParentMessageForEmptyRetry(t *testing.T) {
119119
}
120120
}
121121

122+
func TestExecuteNonStreamWithRetryConvertsReferenceMarkers(t *testing.T) {
123+
ds := &fakeDeepSeekCaller{responses: []*http.Response{sseHTTPResponse(
124+
http.StatusOK,
125+
`data: {"p":"response/content","v":"答案[reference:0]。","citation":{"cite_index":0,"url":"https://example.com/ref"}}`,
126+
)}}
127+
stdReq := promptcompat.StandardRequest{
128+
Surface: "test",
129+
ResponseModel: "deepseek-v4-flash-search",
130+
PromptTokenText: "prompt",
131+
FinalPrompt: "final prompt",
132+
Search: true,
133+
}
134+
135+
result, outErr := ExecuteNonStreamWithRetry(context.Background(), ds, &auth.RequestAuth{}, stdReq, Options{})
136+
if outErr != nil {
137+
t.Fatalf("unexpected output error: %#v", outErr)
138+
}
139+
want := "答案[0](https://example.com/ref)。"
140+
if result.Turn.Text != want {
141+
t.Fatalf("text mismatch: got %q want %q", result.Turn.Text, want)
142+
}
143+
}
144+
122145
func TestStartCompletionAppliesCurrentInputFileGlobally(t *testing.T) {
123146
ds := &fakeDeepSeekCaller{responses: []*http.Response{sseHTTPResponse(http.StatusOK, `data: {"p":"response/content","v":"ok"}`)}}
124147
stdReq := promptcompat.StandardRequest{

‎internal/httpapi/claude/current_input_file_test.go‎

Lines changed: 76 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,25 @@ import (
55
"io"
66
"net/http"
77
"net/http/httptest"
8+
"path/filepath"
89
"strings"
910
"testing"
1011

1112
"ds2api/internal/auth"
13+
"ds2api/internal/chathistory"
1214
dsclient "ds2api/internal/deepseek/client"
1315
)
1416

1517
type claudeCurrentInputAuth struct{}
1618

19+
type claudeHistoryConfig struct {
20+
aliases map[string]string
21+
}
22+
23+
func (m claudeHistoryConfig) ModelAliases() map[string]string { return m.aliases }
24+
func (claudeHistoryConfig) CurrentInputFileEnabled() bool { return false }
25+
func (claudeHistoryConfig) CurrentInputFileMinChars() int { return 0 }
26+
1727
func (claudeCurrentInputAuth) Determine(*http.Request) (*auth.RequestAuth, error) {
1828
return &auth.RequestAuth{
1929
DeepSeekToken: "direct-token",
@@ -22,6 +32,50 @@ func (claudeCurrentInputAuth) Determine(*http.Request) (*auth.RequestAuth, error
2232
}, nil
2333
}
2434

35+
func TestClaudeDirectRecordsResponseHistory(t *testing.T) {
36+
ds := &claudeCurrentInputDS{}
37+
historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json"))
38+
h := &Handler{
39+
Store: claudeHistoryConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}},
40+
Auth: claudeCurrentInputAuth{},
41+
DS: ds,
42+
ChatHistory: historyStore,
43+
}
44+
reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}`
45+
req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(reqBody))
46+
req.Header.Set("Content-Type", "application/json")
47+
rec := httptest.NewRecorder()
48+
49+
h.Messages(rec, req)
50+
51+
if rec.Code != http.StatusOK {
52+
t.Fatalf("expected 200, got %d body=%s", rec.Code, rec.Body.String())
53+
}
54+
snapshot, err := historyStore.Snapshot()
55+
if err != nil {
56+
t.Fatalf("snapshot history: %v", err)
57+
}
58+
if len(snapshot.Items) != 1 {
59+
t.Fatalf("expected one history item, got %d", len(snapshot.Items))
60+
}
61+
item, err := historyStore.Get(snapshot.Items[0].ID)
62+
if err != nil {
63+
t.Fatalf("get history item: %v", err)
64+
}
65+
if item.Surface != "claude.messages" {
66+
t.Fatalf("unexpected surface: %q", item.Surface)
67+
}
68+
if item.Model != "claude-sonnet-4-6" {
69+
t.Fatalf("unexpected model: %q", item.Model)
70+
}
71+
if item.UserInput != "hello from claude" {
72+
t.Fatalf("unexpected user input: %q", item.UserInput)
73+
}
74+
if item.Content != "ok" {
75+
t.Fatalf("expected raw upstream content, got %q", item.Content)
76+
}
77+
}
78+
2579
func (claudeCurrentInputAuth) Release(*auth.RequestAuth) {}
2680

2781
type claudeCurrentInputDS struct {
@@ -53,10 +107,12 @@ func (d *claudeCurrentInputDS) CallCompletion(_ context.Context, _ *auth.Request
53107

54108
func TestClaudeDirectAppliesCurrentInputFile(t *testing.T) {
55109
ds := &claudeCurrentInputDS{}
110+
historyStore := chathistory.New(filepath.Join(t.TempDir(), "history.json"))
56111
h := &Handler{
57-
Store: mockClaudeConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}},
58-
Auth: claudeCurrentInputAuth{},
59-
DS: ds,
112+
Store: mockClaudeConfig{aliases: map[string]string{"claude-sonnet-4-6": "deepseek-v4-flash"}},
113+
Auth: claudeCurrentInputAuth{},
114+
DS: ds,
115+
ChatHistory: historyStore,
60116
}
61117
reqBody := `{"model":"claude-sonnet-4-6","messages":[{"role":"user","content":"hello from claude"}],"max_tokens":1024}`
62118
req := httptest.NewRequest(http.MethodPost, "/v1/messages", strings.NewReader(reqBody))
@@ -82,4 +138,21 @@ func TestClaudeDirectAppliesCurrentInputFile(t *testing.T) {
82138
if !strings.Contains(prompt, "Continue from the latest state in the attached DS2API_HISTORY.txt context.") {
83139
t.Fatalf("expected continuation prompt, got %q", prompt)
84140
}
141+
snapshot, err := historyStore.Snapshot()
142+
if err != nil {
143+
t.Fatalf("snapshot history: %v", err)
144+
}
145+
if len(snapshot.Items) != 1 {
146+
t.Fatalf("expected one history item, got %d", len(snapshot.Items))
147+
}
148+
full, err := historyStore.Get(snapshot.Items[0].ID)
149+
if err != nil {
150+
t.Fatalf("get history item: %v", err)
151+
}
152+
if full.HistoryText != string(ds.uploads[0].Data) {
153+
t.Fatalf("expected uploaded current input file to be persisted in history text")
154+
}
155+
if len(full.Messages) != 1 || !strings.Contains(full.Messages[0].Content, "Continue from the latest state in the attached DS2API_HISTORY.txt context.") {
156+
t.Fatalf("expected persisted message to match upstream continuation prompt, got %#v", full.Messages)
157+
}
85158
}

0 commit comments

Comments
 (0)