mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-11 11:47:43 +08:00
feat: align Go/Node DSML tool-call parsing drift tolerance and update API docs
Co-Authored-By: Claude Opus 4.6 <noreply@anthropic.com>
This commit is contained in:
@@ -5,9 +5,7 @@ import (
|
||||
"context"
|
||||
dsprotocol "ds2api/internal/deepseek/protocol"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"net/http"
|
||||
"time"
|
||||
|
||||
"ds2api/internal/auth"
|
||||
"ds2api/internal/config"
|
||||
@@ -15,39 +13,33 @@ import (
|
||||
)
|
||||
|
||||
func (c *Client) CallCompletion(ctx context.Context, a *auth.RequestAuth, payload map[string]any, powResp string, maxAttempts int) (*http.Response, error) {
|
||||
if maxAttempts <= 0 {
|
||||
maxAttempts = c.maxRetries
|
||||
}
|
||||
_ = maxAttempts
|
||||
clients := c.requestClientsForAuth(ctx, a)
|
||||
headers := c.authHeaders(a.DeepSeekToken)
|
||||
headers["x-ds-pow-response"] = powResp
|
||||
captureSession := c.capture.Start("deepseek_completion", dsprotocol.DeepSeekCompletionURL, a.AccountID, payload)
|
||||
attempts := 0
|
||||
for attempts < maxAttempts {
|
||||
resp, err := c.streamPost(ctx, clients.stream, dsprotocol.DeepSeekCompletionURL, headers, payload)
|
||||
if err != nil {
|
||||
attempts++
|
||||
time.Sleep(time.Second)
|
||||
continue
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
if captureSession != nil {
|
||||
resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode)
|
||||
}
|
||||
resp = c.wrapCompletionWithAutoContinue(ctx, a, payload, powResp, resp)
|
||||
return resp, nil
|
||||
}
|
||||
if captureSession != nil {
|
||||
resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode)
|
||||
}
|
||||
_ = resp.Body.Close()
|
||||
attempts++
|
||||
time.Sleep(time.Second)
|
||||
resp, err := c.streamPostOnce(ctx, clients.stream, dsprotocol.DeepSeekCompletionURL, headers, payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return nil, errors.New("completion failed")
|
||||
if captureSession != nil {
|
||||
resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode)
|
||||
}
|
||||
if resp.StatusCode == http.StatusOK {
|
||||
resp = c.wrapCompletionWithAutoContinue(ctx, a, payload, powResp, resp)
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
func (c *Client) streamPost(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any) (*http.Response, error) {
|
||||
return c.streamPostWithFallback(ctx, doer, url, headers, payload, true)
|
||||
}
|
||||
|
||||
func (c *Client) streamPostOnce(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any) (*http.Response, error) {
|
||||
return c.streamPostWithFallback(ctx, doer, url, headers, payload, false)
|
||||
}
|
||||
|
||||
func (c *Client) streamPostWithFallback(ctx context.Context, doer trans.Doer, url string, headers map[string]string, payload any, allowFallback bool) (*http.Response, error) {
|
||||
b, err := json.Marshal(payload)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -63,15 +55,18 @@ func (c *Client) streamPost(ctx context.Context, doer trans.Doer, url string, he
|
||||
}
|
||||
resp, err := doer.Do(req)
|
||||
if err != nil {
|
||||
config.Logger.Warn("[deepseek] fingerprint stream request failed, fallback to std transport", "url", url, "error", err)
|
||||
req2, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b))
|
||||
if reqErr != nil {
|
||||
return nil, reqErr
|
||||
if allowFallback {
|
||||
config.Logger.Warn("[deepseek] fingerprint stream request failed, fallback to std transport", "url", url, "error", err)
|
||||
req2, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(b))
|
||||
if reqErr != nil {
|
||||
return nil, reqErr
|
||||
}
|
||||
for k, v := range headers {
|
||||
req2.Header.Set(k, v)
|
||||
}
|
||||
return clients.fallbackS.Do(req2)
|
||||
}
|
||||
for k, v := range headers {
|
||||
req2.Header.Set(k, v)
|
||||
}
|
||||
return clients.fallbackS.Do(req2)
|
||||
return nil, err
|
||||
}
|
||||
return resp, nil
|
||||
}
|
||||
|
||||
36
internal/deepseek/client/client_completion_test.go
Normal file
36
internal/deepseek/client/client_completion_test.go
Normal file
@@ -0,0 +1,36 @@
|
||||
package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"errors"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"ds2api/internal/auth"
|
||||
)
|
||||
|
||||
func TestCallCompletionDoesNotFallbackForNonIdempotentCompletion(t *testing.T) {
|
||||
var fallbackCalled bool
|
||||
client := &Client{
|
||||
stream: doerFunc(func(*http.Request) (*http.Response, error) {
|
||||
return nil, errors.New("ambiguous completion write failure")
|
||||
}),
|
||||
fallbackS: &http.Client{Transport: roundTripperFunc(func(*http.Request) (*http.Response, error) {
|
||||
fallbackCalled = true
|
||||
return &http.Response{StatusCode: http.StatusOK}, nil
|
||||
})},
|
||||
}
|
||||
_, err := client.CallCompletion(
|
||||
context.Background(),
|
||||
&auth.RequestAuth{DeepSeekToken: "token"},
|
||||
map[string]any{"prompt": "hello"},
|
||||
"pow",
|
||||
3,
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected completion error")
|
||||
}
|
||||
if fallbackCalled {
|
||||
t.Fatal("completion fallback should not be called for a non-idempotent request")
|
||||
}
|
||||
}
|
||||
@@ -95,11 +95,7 @@ func (c *Client) UploadFile(ctx context.Context, a *auth.RequestAuth, req Upload
|
||||
resp, err := c.doUpload(ctx, clients.regular, clients.fallback, dsprotocol.DeepSeekUploadFileURL, headers, body)
|
||||
if err != nil {
|
||||
config.Logger.Warn("[upload_file] request error", "error", err, "account", a.AccountID, "filename", filename)
|
||||
powHeader = ""
|
||||
lastFailureKind = FailureUnknown
|
||||
lastFailureMessage = err.Error()
|
||||
attempts++
|
||||
continue
|
||||
return nil, err
|
||||
}
|
||||
if captureSession != nil {
|
||||
resp.Body = captureSession.WrapBody(resp.Body, resp.StatusCode)
|
||||
@@ -201,7 +197,7 @@ func escapeMultipartFilename(filename string) string {
|
||||
return filename
|
||||
}
|
||||
|
||||
func (c *Client) doUpload(ctx context.Context, doer trans.Doer, fallback trans.Doer, url string, headers map[string]string, body []byte) (*http.Response, error) {
|
||||
func (c *Client) doUpload(ctx context.Context, doer trans.Doer, _ trans.Doer, url string, headers map[string]string, body []byte) (*http.Response, error) {
|
||||
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
||||
if err != nil {
|
||||
return nil, err
|
||||
@@ -213,15 +209,7 @@ func (c *Client) doUpload(ctx context.Context, doer trans.Doer, fallback trans.D
|
||||
if err == nil {
|
||||
return resp, nil
|
||||
}
|
||||
config.Logger.Warn("[deepseek] fingerprint upload request failed, fallback to std transport", "url", url, "error", err)
|
||||
req2, reqErr := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
|
||||
if reqErr != nil {
|
||||
return nil, reqErr
|
||||
}
|
||||
for k, v := range headers {
|
||||
req2.Header.Set(k, v)
|
||||
}
|
||||
return fallback.Do(req2)
|
||||
return nil, err
|
||||
}
|
||||
|
||||
func extractUploadFileResult(resp map[string]any) *UploadFileResult {
|
||||
|
||||
@@ -6,6 +6,7 @@ import (
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"io"
|
||||
"net/http"
|
||||
"strings"
|
||||
@@ -39,6 +40,31 @@ func TestBuildUploadMultipartBodyOmitsPurposeAndIncludesFilePart(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestDoUploadDoesNotFallbackForNonIdempotentUpload(t *testing.T) {
|
||||
var fallbackCalled bool
|
||||
client := &Client{}
|
||||
_, err := client.doUpload(
|
||||
context.Background(),
|
||||
doerFunc(func(req *http.Request) (*http.Response, error) {
|
||||
_, _ = io.ReadAll(req.Body)
|
||||
return nil, errors.New("ambiguous upload write failure")
|
||||
}),
|
||||
doerFunc(func(*http.Request) (*http.Response, error) {
|
||||
fallbackCalled = true
|
||||
return &http.Response{StatusCode: http.StatusOK, Header: make(http.Header), Body: io.NopCloser(strings.NewReader("{}"))}, nil
|
||||
}),
|
||||
dsprotocol.DeepSeekUploadFileURL,
|
||||
map[string]string{"Content-Type": "multipart/form-data"},
|
||||
[]byte("body"),
|
||||
)
|
||||
if err == nil {
|
||||
t.Fatal("expected upload error")
|
||||
}
|
||||
if fallbackCalled {
|
||||
t.Fatal("upload fallback should not be called for a non-idempotent request")
|
||||
}
|
||||
}
|
||||
|
||||
func TestExtractUploadFileResultSupportsNestedShapes(t *testing.T) {
|
||||
got := extractUploadFileResult(map[string]any{
|
||||
"data": map[string]any{
|
||||
|
||||
Reference in New Issue
Block a user