# 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 | 223 | var param any |
| 224 | − converted := "" | |
| 224 | + var converted []byte | |
| 225 | 225 | if useResponses && from.String() == "claude" { |
| 226 | 226 | converted = translateGitHubCopilotResponsesNonStreamToClaude(data) |
| 227 | 227 | } else { |
| 228 | 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 | 231 | reporter.ensurePublished(ctx) |
| 232 | 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 | 378 | if useResponses && from.String() == "claude" { |
| 379 | 379 | chunks = translateGitHubCopilotResponsesStreamToClaude(bytes.Clone(line), ¶m) |
| 380 | 380 | } else { |
| 381 | 381 | chunks = sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), ¶m) |
| 382 | 382 | } |
| 383 | 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 | 977 | ItemIDToTool map[string]*githubCopilotResponsesStreamToolState |
| 978 | 978 | } |
| 979 | 979 | |
| 980 | −func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string { | |
| 980 | +func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) []byte { | |
| 981 | 981 | root := gjson.ParseBytes(data) |
| 982 | 982 | out := `{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}` |
| 983 | 983 | out, _ = sjson.Set(out, "id", root.Get("id").String()) |
| @@ -1067,10 +1067,10 @@ func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string { · 83 unmodified lines | ||
| 1067 | 1067 | } else { |
| 1068 | 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 | 1074 | if *param == nil { |
| 1075 | 1075 | *param = &githubCopilotResponsesStreamState{ |
| 1076 | 1076 | TextBlockIndex: -1, |
| @@ -1092,7 +1092,10 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 15 unmodified lines | ||
| 1092 | 1092 | } |
| 1093 | 1093 | |
| 1094 | 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 | 1099 | ensureMessageStart := func() { |
| 1097 | 1100 | if state.MessageStarted { |
| 1098 | 1101 | return |
| @@ -1100,7 +1103,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 1 unmodified line | ||
| 1100 | 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 | 1104 | messageStart, _ = sjson.Set(messageStart, "message.id", gjson.GetBytes(payload, "response.id").String()) |
| 1102 | 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 | 1107 | state.MessageStarted = true |
| 1105 | 1108 | } |
| 1106 | 1109 | startTextBlockIfNeeded := func() { |
| @@ -1113,7 +1116,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 6 unmodified lines | ||
| 1113 | 1116 | } |
| 1114 | 1117 | contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}` |
| 1115 | 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 | 1120 | state.TextBlockStarted = true |
| 1118 | 1121 | } |
| 1119 | 1122 | stopTextBlockIfNeeded := func() { |
| @@ -1122,7 +1125,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 2 unmodified lines | ||
| 1122 | 1125 | } |
| 1123 | 1126 | contentBlockStop := `{"type":"content_block_stop","index":0}` |
| 1124 | 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 | 1129 | state.TextBlockStarted = false |
| 1127 | 1130 | state.TextBlockIndex = -1 |
| 1128 | 1131 | } |
| @@ -1152,7 +1155,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 23 unmodified lines | ||
| 1152 | 1155 | contentDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}` |
| 1153 | 1156 | contentDelta, _ = sjson.Set(contentDelta, "index", state.TextBlockIndex) |
| 1154 | 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 | 1160 | case "response.reasoning_summary_part.added": |
| 1158 | 1161 | ensureMessageStart() |
| @@ -1161,7 +1164,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 2 unmodified lines | ||
| 1161 | 1164 | state.NextContentIndex++ |
| 1162 | 1165 | thinkingStart := `{"type":"content_block_start","index":0,"content_block":{"type":"thinking","thinking":""}}` |
| 1163 | 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 | 1168 | case "response.reasoning_summary_text.delta": |
| 1166 | 1169 | if state.ReasoningActive { |
| 1167 | 1170 | delta := gjson.GetBytes(payload, "delta").String() |
| @@ -1169,14 +1172,14 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 1 unmodified line | ||
| 1169 | 1172 | thinkingDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"thinking_delta","thinking":""}}` |
| 1170 | 1173 | thinkingDelta, _ = sjson.Set(thinkingDelta, "index", state.ReasoningIndex) |
| 1171 | 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 | 1178 | case "response.reasoning_summary_part.done": |
| 1176 | 1179 | if state.ReasoningActive { |
| 1177 | 1180 | thinkingStop := `{"type":"content_block_stop","index":0}` |
| 1178 | 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 | 1183 | state.ReasoningActive = false |
| 1181 | 1184 | } |
| 1182 | 1185 | case "response.output_item.added": |
| @@ -1204,7 +1207,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 21 unmodified lines | ||
| 1204 | 1207 | contentBlockStart, _ = sjson.Set(contentBlockStart, "index", tool.Index) |
| 1205 | 1208 | contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.id", tool.ID) |
| 1206 | 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 | 1211 | case "response.output_item.delta": |
| 1209 | 1212 | item := gjson.GetBytes(payload, "item") |
| 1210 | 1213 | if item.Get("type").String() != "function_call" { |
| @@ -1224,7 +1227,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 13 unmodified lines | ||
| 1224 | 1227 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` |
| 1225 | 1228 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) |
| 1226 | 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 | 1231 | case "response.function_call_arguments.delta": |
| 1229 | 1232 | // Copilot sends tool call arguments via this event type (not response.output_item.delta). |
| 1230 | 1233 | // Data format: {"delta":"...", "item_id":"...", "output_index":N, ...} |
| @@ -1241,7 +1244,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 10 unmodified lines | ||
| 1241 | 1244 | inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}` |
| 1242 | 1245 | inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index) |
| 1243 | 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 | 1248 | case "response.output_item.done": |
| 1246 | 1249 | if gjson.GetBytes(payload, "item.type").String() != "function_call" { |
| 1247 | 1250 | break |
| @@ -1252,7 +1255,7 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 4 unmodified lines | ||
| 1252 | 1255 | } |
| 1253 | 1256 | contentBlockStop := `{"type":"content_block_stop","index":0}` |
| 1254 | 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 | 1259 | case "response.completed": |
| 1257 | 1260 | ensureMessageStart() |
| 1258 | 1261 | stopTextBlockIfNeeded() |
| @@ -1276,8 +1279,8 @@ func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []st · 17 unmodified lines | ||
| 1276 | 1279 | if cachedTokens > 0 { |
| 1277 | 1280 | messageDelta, _ = sjson.Set(messageDelta, "usage.cache_read_input_tokens", cachedTokens) |
| 1278 | 1281 | } |
| 1279 | − results = append(results, "event: message_delta\ndata: "+messageDelta+"\n\n") | |
| 1280 | − results = append(results, "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") | |
| 1282 | + appendResult("event: message_delta\ndata: " + messageDelta + "\n\n") | |
| 1283 | + appendResult("event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n") | |
| 1281 | 1284 | state.MessageStopSent = true |
| 1282 | 1285 | } |
| 1283 | 1286 | } |