fix(agent): Agent 对话 - 修复无效 URL 与上游取消日志
This commit is contained in:
@@ -3054,12 +3054,11 @@ func recordProviderRequest(req *http.Request, startedAt time.Time, statusCode in
|
||||
return
|
||||
}
|
||||
status := model.ApiCallStatusSucceeded
|
||||
errorCode := ""
|
||||
errorText := ""
|
||||
if requestErr != nil || statusCode < 200 || statusCode >= 300 {
|
||||
status = model.ApiCallStatusFailed
|
||||
if requestErr != nil {
|
||||
errorText = safeProviderLogError(requestErr)
|
||||
}
|
||||
errorCode, errorText = providerRequestErrorDetails(requestErr)
|
||||
}
|
||||
requestKind := providerRequestKind(req.Method, req.URL.Path)
|
||||
if metadata.RequestKind != "" {
|
||||
@@ -3075,7 +3074,7 @@ func recordProviderRequest(req *http.Request, startedAt time.Time, statusCode in
|
||||
RequestKind: requestKind, Billable: req.Method == http.MethodPost && requestKind != "cancel",
|
||||
APIFormat: apiFormat, Method: req.Method, Path: req.URL.Path, Model: metadata.Model,
|
||||
Status: status, StatusCode: statusCode, DurationMs: time.Since(startedAt).Milliseconds(),
|
||||
Error: errorText, ConcurrencyLimit: metadata.ConcurrencyLimit, UpstreamURL: req.URL.Scheme + "://" + req.URL.Host + req.URL.Path,
|
||||
ErrorCode: errorCode, Error: errorText, ConcurrencyLimit: metadata.ConcurrencyLimit, UpstreamURL: req.URL.Scheme + "://" + req.URL.Host + req.URL.Path,
|
||||
ProviderRequestID: metadata.ProviderRequestID, RequestContentType: req.Header.Get("Content-Type"), RequestBody: requestPayloadForLog(req), ResponseBody: SanitizeAPICallPayload(responseBody, ""),
|
||||
}
|
||||
channelSlotFailure := false
|
||||
@@ -3102,6 +3101,19 @@ func recordProviderRequest(req *http.Request, startedAt time.Time, statusCode in
|
||||
}
|
||||
}
|
||||
|
||||
func providerRequestErrorDetails(err error) (string, string) {
|
||||
if err == nil {
|
||||
return "", ""
|
||||
}
|
||||
if errors.Is(err, context.Canceled) {
|
||||
return "request_cancelled", "任务取消,中断上游请求"
|
||||
}
|
||||
if errors.Is(err, context.DeadlineExceeded) {
|
||||
return "upstream_timeout", "等待上游响应超时"
|
||||
}
|
||||
return "", safeProviderLogError(err)
|
||||
}
|
||||
|
||||
func safeProviderLogError(err error) string {
|
||||
var httpErr providerHTTPError
|
||||
if errors.As(err, &httpErr) {
|
||||
|
||||
@@ -21,6 +21,27 @@ import (
|
||||
const testReferenceImageDataURL = "data:image/png;base64,aGVsbG8="
|
||||
const testGeminiReferenceImageDataURL = "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAAAEAAAABCAQAAAC1HAwCAAAAC0lEQVR42mNk+A8AAQUBAScY42YAAAAASUVORK5CYII="
|
||||
|
||||
func TestProviderRequestErrorDetails(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
err error
|
||||
code string
|
||||
text string
|
||||
}{
|
||||
{name: "cancelled", err: context.Canceled, code: "request_cancelled", text: "任务取消,中断上游请求"},
|
||||
{name: "timeout", err: context.DeadlineExceeded, code: "upstream_timeout", text: "等待上游响应超时"},
|
||||
{name: "network error", err: errors.New("dial tcp: connection refused"), text: "dial tcp: connection refused"},
|
||||
}
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
code, text := providerRequestErrorDetails(tt.err)
|
||||
if code != tt.code || text != tt.text {
|
||||
t.Fatalf("providerRequestErrorDetails() = (%q, %q), want (%q, %q)", code, text, tt.code, tt.text)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWriteMediaPartSanitizesFilenameAndSetsMimeType(t *testing.T) {
|
||||
var body bytes.Buffer
|
||||
writer := multipart.NewWriter(&body)
|
||||
|
||||
Reference in New Issue
Block a user