mirror of
https://github.com/router-for-me/CLIProxyAPI.git
synced 2026-02-18 20:30:51 +08:00
Fixed: #941
fix(translator): ensure fallback to valid originalRequestRawJSON in response handling
This commit is contained in:
@@ -40,6 +40,16 @@ type claudeToResponsesState struct {
|
|||||||
|
|
||||||
var dataTag = []byte("data:")
|
var dataTag = []byte("data:")
|
||||||
|
|
||||||
|
func pickRequestJSON(originalRequestRawJSON, requestRawJSON []byte) []byte {
|
||||||
|
if len(originalRequestRawJSON) > 0 && gjson.ValidBytes(originalRequestRawJSON) {
|
||||||
|
return originalRequestRawJSON
|
||||||
|
}
|
||||||
|
if len(requestRawJSON) > 0 && gjson.ValidBytes(requestRawJSON) {
|
||||||
|
return requestRawJSON
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
func emitEvent(event string, payload string) string {
|
func emitEvent(event string, payload string) string {
|
||||||
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
return fmt.Sprintf("event: %s\ndata: %s", event, payload)
|
||||||
}
|
}
|
||||||
@@ -279,8 +289,9 @@ func ConvertClaudeResponseToOpenAIResponses(ctx context.Context, modelName strin
|
|||||||
completed, _ = sjson.Set(completed, "response.created_at", st.CreatedAt)
|
completed, _ = sjson.Set(completed, "response.created_at", st.CreatedAt)
|
||||||
// Inject original request fields into response as per docs/response.completed.json
|
// Inject original request fields into response as per docs/response.completed.json
|
||||||
|
|
||||||
if requestRawJSON != nil {
|
reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON)
|
||||||
req := gjson.ParseBytes(requestRawJSON)
|
if len(reqBytes) > 0 {
|
||||||
|
req := gjson.ParseBytes(reqBytes)
|
||||||
if v := req.Get("instructions"); v.Exists() {
|
if v := req.Get("instructions"); v.Exists() {
|
||||||
completed, _ = sjson.Set(completed, "response.instructions", v.String())
|
completed, _ = sjson.Set(completed, "response.instructions", v.String())
|
||||||
}
|
}
|
||||||
@@ -549,8 +560,9 @@ func ConvertClaudeResponseToOpenAIResponsesNonStream(_ context.Context, _ string
|
|||||||
out, _ = sjson.Set(out, "created_at", createdAt)
|
out, _ = sjson.Set(out, "created_at", createdAt)
|
||||||
|
|
||||||
// Inject request echo fields as top-level (similar to streaming variant)
|
// Inject request echo fields as top-level (similar to streaming variant)
|
||||||
if requestRawJSON != nil {
|
reqBytes := pickRequestJSON(originalRequestRawJSON, requestRawJSON)
|
||||||
req := gjson.ParseBytes(requestRawJSON)
|
if len(reqBytes) > 0 {
|
||||||
|
req := gjson.ParseBytes(reqBytes)
|
||||||
if v := req.Get("instructions"); v.Exists() {
|
if v := req.Get("instructions"); v.Exists() {
|
||||||
out, _ = sjson.Set(out, "instructions", v.String())
|
out, _ = sjson.Set(out, "instructions", v.String())
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user