# Conflicts: # cmd/server/main.go # internal/translator/claude/openai/chat-completions/claude_openai_response.go
Showing the changes to internal/runtime/executor/github_copilot_executor.go (1 of 87 files). Show all
Unified · Split
| @@ -221,13 +221,13 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 220 unmodified lines | |||
| 221 | } | 221 | } |
| 222 | 222 | ||
| 223 | var param any | 223 | var param any |
| 224 | converted := "" | 224 | var converted []byte |
| 225 | if useResponses && from.String() == "claude" { | 225 | if useResponses && from.String() == "claude" { |
| 226 | converted = translateGitHubCopilotResponsesNonStreamToClaude(data) | 226 | converted = translateGitHubCopilotResponsesNonStreamToClaude(data) |
| 227 | } else { | 227 | } else { |
| 228 | converted = sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) | 228 | converted = sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, ¶m) |
| 229 | } | 229 | } |
| 230 | resp = cliproxyexecutor.Response{Payload: []byte(converted)} | 230 | resp = cliproxyexecutor.Response{Payload: converted} |
| 231 | reporter.ensurePublished(ctx) | 231 | reporter.ensurePublished(ctx) |
| 232 | return resp, nil | 232 | return resp, nil |
| 233 | } | 233 | } |
| @@ -374,14 +374,14 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 140 unmodified lines | |||
| 374 | } | 374 | } |
| 375 | } | 375 | } |
| 376 | 376 | ||
| 377 | var chunks []string | 377 | var chunks [][]byte |
| 378 | if useResponses && from.String() == "claude" { | 378 | if useResponses && from.String() == "claude" { |
| 379 | chunks = translateGitHubCopilotResponsesStreamToClaude(bytes.Clone(line), ¶m) | 379 | chunks = translateGitHubCopilotResponsesStreamToClaude(bytes.Clone(line), ¶m) |
| 380 | } else { | 380 | } else { |
| 381 | chunks = sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) | 381 | chunks = sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) |
| 382 | } | 382 | } |
| 383 | for i := range chunks { | 383 | for i := range chunks { |
| 384 | out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])} | 384 | out <- cliproxyexecutor.StreamChunk{Payload: bytes.Clone(chunks[i])} |
| 385 | } | 385 | } |
| 386 | } | 386 | } |
| 387 | 387 | ||
| @@ -977,7 +977,7 @@ type githubCopilotResponsesStreamState struct { · 589 unmodified lines | |||
| 977 | ItemIDToTool map[string]*githubCopilotResponsesStreamToolState | 977 | ItemIDToTool map[string]*githubCopilotResponsesStreamToolState |
| 978 | } | 978 | } |
| 979 | 979 | ||
| 980 | func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string { | 980 | func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) []byte { |
| 981 | root := gjson.ParseBytes(data) | 981 | root := gjson.ParseBytes(data) |
| 982 | out := `{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}` | 982 | out := `{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}` |
| 983 | out, _ = sjson.Set(out, "id", root.Get("id").String()) | 983 | out, _ = sjson.Set(out, "id", root.Get("id").String()) |
| @@ -1067,10 +1067,10 @@ func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string { · 83 unmodified lines | |||
| 1067 | } else { | 1067 | } else { |
| 1068 | out, _ = sjson.Set(out, "stop_reason", "end_turn") | 1068 | out, _ = sjson.Set(out, "stop_reason", "end_turn") |
| 1069 | } | 1069 | } |
| 1070 | return out | 1070 | return []byte(out) |
| 1071 | } | 1071 | } |
| 1072 | 1072 | ||
| 1073 | func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []string { | 1073 | func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) [][]byte { |
| 1074 | if *param == nil { | 1074 | if *param == nil { |
| 1075 | *param = &githubCopilotResponsesStreamState{ | 1075 | *param = &githubCopilotResponsesStreamState{ |
| 1076 | TextBlockIndex: -1, | 1076 | TextBlockIndex: -1, |
| @@ -1092,7 +1092,10 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 15 unmodified lines | |||
| 1092 | } | 1092 | } |
| 1093 | 1093 | ||
| 1094 | event := gjson.GetBytes(payload, "type").String() | 1094 | event := gjson.GetBytes(payload, "type").String() |
| 1095 | results := make([]string, 0, 4) | 1095 | results := make([][]byte, 0, 4) |
| 1096 | appendResult := func(chunk string) { | ||
| 1097 | results = append(results, []byte(chunk)) | ||
| 1098 | } | ||
| 1096 | ensureMessageStart := func() { | 1099 | ensureMessageStart := func() { |
| 1097 | if state.MessageStarted { | 1100 | if state.MessageStarted { |
| 1098 | return | 1101 | return |
| @@ -1100,7 +1103,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 1 unmodified line | |||
| 1100 | messageStart := `{"type":"message_start","message":{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}}` | 1103 | messageStart := `{"type":"message_start","message":{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}}` |
| 1101 | messageStart, _ = sjson.Set(messageStart, "message.id", gjson.GetBytes(payload, "response.id").String()) | 1104 | messageStart, _ = sjson.Set(messageStart, "message.id", gjson.GetBytes(payload, "response.id").String()) |
| 1102 | messageStart, _ = sjson.Set(messageStart, "message.model", gjson.GetBytes(payload, "response.model").String()) | 1105 | messageStart, _ = sjson.Set(messageStart, "message.model", gjson.GetBytes(payload, "response.model").String()) |
| 1103 | results = append(results, "event: message_start\ndata: "+messageStart+"\n\n") | 1106 | appendResult("event: message_start\ndata: " + messageStart + "\n\n") |
| 1104 | state.MessageStarted = true | 1107 | state.MessageStarted = true |
| 1105 | } | 1108 | } |
| 1106 | startTextBlockIfNeeded := func() { | 1109 | startTextBlockIfNeeded := func() { |
| @@ -1113,7 +1116,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 6 unmodified lines | |||
| 1113 | } | 1116 | } |
| 1114 | contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` | 1117 | contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` |
| 1115 | contentBlockStart, _ = sjson.Set(contentBlockStart, "index", state.TextBlockIndex) | 1118 | contentBlockStart, _ = sjson.Set(contentBlockStart, "index", state.TextBlockIndex) |
| 1116 | results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n") | 1119 | appendResult("event: content_block_start\ndata: " + contentBlockStart + "\n\n") |
| 1117 | state.TextBlockStarted = true | 1120 | state.TextBlockStarted = true |
| 1118 | } | 1121 | } |
| 1119 | stopTextBlockIfNeeded := func() { | 1122 | stopTextBlockIfNeeded := func() { |
| @@ -1122,7 +1125,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 2 unmodified lines | |||
| 1122 | } | 1125 | } |
| 1123 | contentBlockStop := `{"type":"content_block_stop","index":0}` | 1126 | contentBlockStop := `{"type":"content_block_stop","index":0}` |
| 1124 | contentBlockStop, _ = sjson.Set(contentBlockStop, "index", state.TextBlockIndex) | 1127 | contentBlockStop, _ = sjson.Set(contentBlockStop, "index", state.TextBlockIndex) |
| 1125 | results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n") | 1128 | appendResult("event: content_block_stop\ndata: " + contentBlockStop + "\n\n") |
| 1126 | state.TextBlockStarted = false | 1129 | state.TextBlockStarted = false |
| 1127 | state.TextBlockIndex = -1 | 1130 | state.TextBlockIndex = -1 |
| 1128 | } | 1131 | } |
| @@ -1152,7 +1155,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 23 unmodified lines | |||
| 1152 | contentDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` | 1155 | contentDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` |
| 1153 | contentDelta, _ = sjson.Set(contentDelta, "index", state.TextBlockIndex) | 1156 | contentDelta, _ = sjson.Set(contentDelta, "index", state.TextBlockIndex) |
| 1154 | contentDelta, _ = sjson.Set(contentDelta, "delta.text", delta) | 1157 | contentDelta, _ = sjson.Set(contentDelta, "delta.text", delta) |
| 1155 | results = append(results, "event: content_block_delta\ndata: "+contentDelta+"\n\n") | 1158 | appendResult("event: content_block_delta\ndata: " + contentDelta + "\n\n") |
| 1156 | } | 1159 | } |
| 1157 | case "response.reasoning_summary_part.added": | 1160 | case "response.reasoning_summary_part.added": |
| 1158 | ensureMessageStart() | 1161 | ensureMessageStart() |
| @@ -1161,7 +1164,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 2 unmodified lines | |||
| 1161 | state.NextContentIndex++ | 1164 | state.NextContentIndex++ |
| 1162 | thinkingStart := `{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}` | 1165 | thinkingStart := `{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}` |
| 1163 | thinkingStart, _ = sjson.Set(thinkingStart, "index", state.ReasoningIndex) | 1166 | thinkingStart, _ = sjson.Set(thinkingStart, "index", state.ReasoningIndex) |
| 1164 | results = append(results, "event: content_block_start\ndata: "+thinkingStart+"\n\n") | 1167 | appendResult("event: content_block_start\ndata: " + thinkingStart + "\n\n") |
| 1165 | case "response.reasoning_summary_text.delta": | 1168 | case "response.reasoning_summary_text.delta": |
| 1166 | if state.ReasoningActive { | 1169 | if state.ReasoningActive { |
| 1167 | delta := gjson.GetBytes(payload, "delta").String() | 1170 | delta := gjson.GetBytes(payload, "delta").String() |
| @@ -1169,14 +1172,14 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 1 unmodified line | |||
| 1169 | thinkingDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}` | 1172 | thinkingDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}` |
| 1170 | thinkingDelta, _ = sjson.Set(thinkingDelta, "index", state.ReasoningIndex) | 1173 | thinkingDelta, _ = sjson.Set(thinkingDelta, "index", state.ReasoningIndex) |
| 1171 | thinkingDelta, _ = sjson.Set(thinkingDelta, "delta.thinking", delta) | 1174 | thinkingDelta, _ = sjson.Set(thinkingDelta, "delta.thinking", delta) |
| 1172 | results = append(results, "event: content_block_delta\ndata: "+thinkingDelta+"\n\n") | 1175 | appendResult("event: content_block_delta\ndata: " + thinkingDelta + "\n\n") |
| 1173 | } | 1176 | } |
| 1174 | } | 1177 | } |
| 1175 | case "response.reasoning_summary_part.done": | 1178 | case "response.reasoning_summary_part.done": |
| 1176 | if state.ReasoningActive { | 1179 | if state.ReasoningActive { |
| 1177 | thinkingStop := `{"type":"content_block_stop","index":0}` | 1180 | thinkingStop := `{"type":"content_block_stop","index":0}` |
| 1178 | thinkingStop, _ = sjson.Set(thinkingStop, "index", state.ReasoningIndex) | 1181 | thinkingStop, _ = sjson.Set(thinkingStop, "index", state.ReasoningIndex) |
| 1179 | results = append(results, "event: content_block_stop\ndata: "+thinkingStop+"\n\n") | 1182 | appendResult("event: content_block_stop\ndata: " + thinkingStop + "\n\n") |
| 1180 | state.ReasoningActive = false | 1183 | state.ReasoningActive = false |
| 1181 | } | 1184 | } |
| 1182 | case "response.output_item.added": | 1185 | case "response.output_item.added": |
| @@ -1204,7 +1207,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 21 unmodified lines | |||
| 1204 | contentBlockStart, _ = sjson.Set(contentBlockStart, "index", tool.Index) | 1207 | contentBlockStart, _ = sjson.Set(contentBlockStart, "index", tool.Index) |
| 1205 | contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.id", tool.ID) | 1208 | contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.id", tool.ID) |
| 1206 | contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.name", tool.Name) | 1209 | contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.name", tool.Name) |
| 1207 | results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n") | 1210 | appendResult("event: content_block_start\ndata: " + contentBlockStart + "\n\n") |
| 1208 | case "response.output_item.delta": | 1211 | case "response.output_item.delta": |
| 1209 | item := gjson.GetBytes(payload, "item") | 1212 | item := gjson.GetBytes(payload, "item") |
| 1210 | if item.Get("type").String() != "function_call" { | 1213 | if item.Get("type").String() != "function_call" { |
| @@ -1224,7 +1227,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 13 unmodified lines | |||
| 1224 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` | 1227 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` |
| 1225 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) | 1228 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) |
| 1226 | inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial) | 1229 | inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial) |
| 1227 | results = append(results, "event: content_block_delta\ndata: "+inputDelta+"\n\n") | 1230 | appendResult("event: content_block_delta\ndata: " + inputDelta + "\n\n") |
| 1228 | case "response.function_call_arguments.delta": | 1231 | case "response.function_call_arguments.delta": |
| 1229 | // Copilot sends tool call arguments via this event type (not response.output_item.delta). | 1232 | // Copilot sends tool call arguments via this event type (not response.output_item.delta). |
| 1230 | // Data format: {"delta":"...", "item_id":"...", "output_index":N, ...} | 1233 | // Data format: {"delta":"...", "item_id":"...", "output_index":N, ...} |
| @@ -1241,7 +1244,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 10 unmodified lines | |||
| 1241 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` | 1244 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` |
| 1242 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) | 1245 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) |
| 1243 | inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial) | 1246 | inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial) |
| 1244 | results = append(results, "event: content_block_delta\ndata: "+inputDelta+"\n\n") | 1247 | appendResult("event: content_block_delta\ndata: " + inputDelta + "\n\n") |
| 1245 | case "response.output_item.done": | 1248 | case "response.output_item.done": |
| 1246 | if gjson.GetBytes(payload, "item.type").String() != "function_call" { | 1249 | if gjson.GetBytes(payload, "item.type").String() != "function_call" { |
| 1247 | break | 1250 | break |
| @@ -1252,7 +1255,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 4 unmodified lines | |||
| 1252 | } | 1255 | } |
| 1253 | contentBlockStop := `{"type":"content_block_stop","index":0}` | 1256 | contentBlockStop := `{"type":"content_block_stop","index":0}` |
| 1254 | contentBlockStop, _ = sjson.Set(contentBlockStop, "index", tool.Index) | 1257 | contentBlockStop, _ = sjson.Set(contentBlockStop, "index", tool.Index) |
| 1255 | results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n") | 1258 | appendResult("event: content_block_stop\ndata: " + contentBlockStop + "\n\n") |
| 1256 | case "response.completed": | 1259 | case "response.completed": |
| 1257 | ensureMessageStart() | 1260 | ensureMessageStart() |
| 1258 | stopTextBlockIfNeeded() | 1261 | stopTextBlockIfNeeded() |
| @@ -1276,8 +1279,8 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 17 unmodified lines | |||
| 1276 | if cachedTokens > 0 { | 1279 | if cachedTokens > 0 { |
| 1277 | messageDelta, _ = sjson.Set(messageDelta, "usage.cache_read_input_tokens", cachedTokens) | 1280 | messageDelta, _ = sjson.Set(messageDelta, "usage.cache_read_input_tokens", cachedTokens) |
| 1278 | } | 1281 | } |
| 1279 | results = append(results, "event: message_delta\ndata: "+messageDelta+"\n\n") | 1282 | appendResult("event: message_delta\ndata: " + messageDelta + "\n\n") |
| 1280 | results = append(results, "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") | 1283 | appendResult("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") |
| 1281 | state.MessageStopSent = true | 1284 | state.MessageStopSent = true |
| 1282 | } | 1285 | } |
| 1283 | } | 1286 | } |