|
@@ -2,12 +2,18 @@ package dto
|
|
|
|
|
|
|
|
import (
|
|
import (
|
|
|
"encoding/json"
|
|
"encoding/json"
|
|
|
|
|
+ "fmt"
|
|
|
"one-api/types"
|
|
"one-api/types"
|
|
|
)
|
|
)
|
|
|
|
|
|
|
|
type SimpleResponse struct {
|
|
type SimpleResponse struct {
|
|
|
Usage `json:"usage"`
|
|
Usage `json:"usage"`
|
|
|
- Error *OpenAIError `json:"error"`
|
|
|
|
|
|
|
+ Error any `json:"error"`
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
|
|
+// GetOpenAIError 从动态错误类型中提取OpenAIError结构
|
|
|
|
|
+func (s *SimpleResponse) GetOpenAIError() *types.OpenAIError {
|
|
|
|
|
+ return GetOpenAIError(s.Error)
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
type TextResponse struct {
|
|
type TextResponse struct {
|
|
@@ -31,10 +37,15 @@ type OpenAITextResponse struct {
|
|
|
Object string `json:"object"`
|
|
Object string `json:"object"`
|
|
|
Created any `json:"created"`
|
|
Created any `json:"created"`
|
|
|
Choices []OpenAITextResponseChoice `json:"choices"`
|
|
Choices []OpenAITextResponseChoice `json:"choices"`
|
|
|
- Error *types.OpenAIError `json:"error,omitempty"`
|
|
|
|
|
|
|
+ Error any `json:"error,omitempty"`
|
|
|
Usage `json:"usage"`
|
|
Usage `json:"usage"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetOpenAIError 从动态错误类型中提取OpenAIError结构
|
|
|
|
|
+func (o *OpenAITextResponse) GetOpenAIError() *types.OpenAIError {
|
|
|
|
|
+ return GetOpenAIError(o.Error)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
type OpenAIEmbeddingResponseItem struct {
|
|
type OpenAIEmbeddingResponseItem struct {
|
|
|
Object string `json:"object"`
|
|
Object string `json:"object"`
|
|
|
Index int `json:"index"`
|
|
Index int `json:"index"`
|
|
@@ -217,7 +228,7 @@ type OpenAIResponsesResponse struct {
|
|
|
Object string `json:"object"`
|
|
Object string `json:"object"`
|
|
|
CreatedAt int `json:"created_at"`
|
|
CreatedAt int `json:"created_at"`
|
|
|
Status string `json:"status"`
|
|
Status string `json:"status"`
|
|
|
- Error *types.OpenAIError `json:"error,omitempty"`
|
|
|
|
|
|
|
+ Error any `json:"error,omitempty"`
|
|
|
IncompleteDetails *IncompleteDetails `json:"incomplete_details,omitempty"`
|
|
IncompleteDetails *IncompleteDetails `json:"incomplete_details,omitempty"`
|
|
|
Instructions string `json:"instructions"`
|
|
Instructions string `json:"instructions"`
|
|
|
MaxOutputTokens int `json:"max_output_tokens"`
|
|
MaxOutputTokens int `json:"max_output_tokens"`
|
|
@@ -237,6 +248,11 @@ type OpenAIResponsesResponse struct {
|
|
|
Metadata json.RawMessage `json:"metadata"`
|
|
Metadata json.RawMessage `json:"metadata"`
|
|
|
}
|
|
}
|
|
|
|
|
|
|
|
|
|
+// GetOpenAIError 从动态错误类型中提取OpenAIError结构
|
|
|
|
|
+func (o *OpenAIResponsesResponse) GetOpenAIError() *types.OpenAIError {
|
|
|
|
|
+ return GetOpenAIError(o.Error)
|
|
|
|
|
+}
|
|
|
|
|
+
|
|
|
type IncompleteDetails struct {
|
|
type IncompleteDetails struct {
|
|
|
Reasoning string `json:"reasoning"`
|
|
Reasoning string `json:"reasoning"`
|
|
|
}
|
|
}
|
|
@@ -276,3 +292,45 @@ type ResponsesStreamResponse struct {
|
|
|
Delta string `json:"delta,omitempty"`
|
|
Delta string `json:"delta,omitempty"`
|
|
|
Item *ResponsesOutput `json:"item,omitempty"`
|
|
Item *ResponsesOutput `json:"item,omitempty"`
|
|
|
}
|
|
}
|
|
|
|
|
+
|
|
|
|
|
+// GetOpenAIError 从动态错误类型中提取OpenAIError结构
|
|
|
|
|
+func GetOpenAIError(errorField any) *types.OpenAIError {
|
|
|
|
|
+ if errorField == nil {
|
|
|
|
|
+ return nil
|
|
|
|
|
+ }
|
|
|
|
|
+
|
|
|
|
|
+ switch err := errorField.(type) {
|
|
|
|
|
+ case types.OpenAIError:
|
|
|
|
|
+ return &err
|
|
|
|
|
+ case *types.OpenAIError:
|
|
|
|
|
+ return err
|
|
|
|
|
+ case map[string]interface{}:
|
|
|
|
|
+ // 处理从JSON解析来的map结构
|
|
|
|
|
+ openaiErr := &types.OpenAIError{}
|
|
|
|
|
+ if errType, ok := err["type"].(string); ok {
|
|
|
|
|
+ openaiErr.Type = errType
|
|
|
|
|
+ }
|
|
|
|
|
+ if errMsg, ok := err["message"].(string); ok {
|
|
|
|
|
+ openaiErr.Message = errMsg
|
|
|
|
|
+ }
|
|
|
|
|
+ if errParam, ok := err["param"].(string); ok {
|
|
|
|
|
+ openaiErr.Param = errParam
|
|
|
|
|
+ }
|
|
|
|
|
+ if errCode, ok := err["code"]; ok {
|
|
|
|
|
+ openaiErr.Code = errCode
|
|
|
|
|
+ }
|
|
|
|
|
+ return openaiErr
|
|
|
|
|
+ case string:
|
|
|
|
|
+ // 处理简单字符串错误
|
|
|
|
|
+ return &types.OpenAIError{
|
|
|
|
|
+ Type: "error",
|
|
|
|
|
+ Message: err,
|
|
|
|
|
+ }
|
|
|
|
|
+ default:
|
|
|
|
|
+ // 未知类型,尝试转换为字符串
|
|
|
|
|
+ return &types.OpenAIError{
|
|
|
|
|
+ Type: "unknown_error",
|
|
|
|
|
+ Message: fmt.Sprintf("%v", err),
|
|
|
|
|
+ }
|
|
|
|
|
+ }
|
|
|
|
|
+}
|