History

Merge branch 'main' into main

Luis Pater committed 9 months ago

Commit 294eac32 parents 795da13 a31104012 files · +1656 −1321Browse files

Showing the changes to internal/runtime/executor/github_copilot_executor.go (1 of 12 files). Show all

Unified · Split

internal/runtime/executor/github_copilot_executor.go+437−6
@@ -110,7 +110,7 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 109 unmodified lines
110 defer reporter.trackFailure(ctx, &err)110 defer reporter.trackFailure(ctx, &err)
111 111
112 from := opts.SourceFormat112 from := opts.SourceFormat
113 useResponses := useGitHubCopilotResponsesEndpoint(from)113 useResponses := useGitHubCopilotResponsesEndpoint(from, req.Model)
114 to := sdktranslator.FromString("openai")114 to := sdktranslator.FromString("openai")
115 if useResponses {115 if useResponses {
116 to = sdktranslator.FromString("openai-response")116 to = sdktranslator.FromString("openai-response")
@@ -133,6 +133,12 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 16 unmodified lines
133 return resp, err133 return resp, err
134 }134 }
135 135
136 if useResponses {
137 body = normalizeGitHubCopilotResponsesInput(body)
138 body = normalizeGitHubCopilotResponsesTools(body)
139 } else {
140 body = normalizeGitHubCopilotChatTools(body)
141 }
136 requestedModel := payloadRequestedModel(opts, req.Model)142 requestedModel := payloadRequestedModel(opts, req.Model)
137 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel)143 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel)
138 body, _ = sjson.SetBytes(body, "stream", false)144 body, _ = sjson.SetBytes(body, "stream", false)
@@ -209,7 +215,12 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 70 unmodified lines
209 }215 }
210 216
211 var param any217 var param any
212 converted := sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, &param)218 converted := ""
219 if useResponses && from.String() == "claude" {
220 converted = translateGitHubCopilotResponsesNonStreamToClaude(data)
221 } else {
222 converted = sdktranslator.TranslateNonStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, data, &param)
223 }
213 resp = cliproxyexecutor.Response{Payload: []byte(converted)}224 resp = cliproxyexecutor.Response{Payload: []byte(converted)}
214 reporter.ensurePublished(ctx)225 reporter.ensurePublished(ctx)
215 return resp, nil226 return resp, nil
@@ -226,7 +237,7 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 10 unmodified lines
226 defer reporter.trackFailure(ctx, &err)237 defer reporter.trackFailure(ctx, &err)
227 238
228 from := opts.SourceFormat239 from := opts.SourceFormat
229 useResponses := useGitHubCopilotResponsesEndpoint(from)240 useResponses := useGitHubCopilotResponsesEndpoint(from, req.Model)
230 to := sdktranslator.FromString("openai")241 to := sdktranslator.FromString("openai")
231 if useResponses {242 if useResponses {
232 to = sdktranslator.FromString("openai-response")243 to = sdktranslator.FromString("openai-response")
@@ -249,6 +260,12 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 16 unmodified lines
249 return nil, err260 return nil, err
250 }261 }
251 262
263 if useResponses {
264 body = normalizeGitHubCopilotResponsesInput(body)
265 body = normalizeGitHubCopilotResponsesTools(body)
266 } else {
267 body = normalizeGitHubCopilotChatTools(body)
268 }
252 requestedModel := payloadRequestedModel(opts, req.Model)269 requestedModel := payloadRequestedModel(opts, req.Model)
253 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel)270 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated, requestedModel)
254 body, _ = sjson.SetBytes(body, "stream", true)271 body, _ = sjson.SetBytes(body, "stream", true)
@@ -349,7 +366,12 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 94 unmodified lines
349 }366 }
350 }367 }
351 368
352 chunks := sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), &param)369 var chunks []string
370 if useResponses && from.String() == "claude" {
371 chunks = translateGitHubCopilotResponsesStreamToClaude(bytes.Clone(line), &param)
372 } else {
373 chunks = sdktranslator.TranslateStream(ctx, to, from, req.Model, bytes.Clone(opts.OriginalRequest), body, bytes.Clone(line), &param)
374 }
353 for i := range chunks {375 for i := range chunks {
354 out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])}376 out <- cliproxyexecutor.StreamChunk{Payload: []byte(chunks[i])}
355 }377 }
@@ -503,8 +525,12 @@ func (e *GitHubCopilotExecutor) normalizeModel(model string, body []byte) []byte · 147 unmodified lines
503 return body525 return body
504}526}
505 527
506func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format) bool {528func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format, model string) bool {
507 return sourceFormat.String() == "openai-response"529 if sourceFormat.String() == "openai-response" {
530 return true
531 }
532 baseModel := strings.ToLower(thinking.ParseSuffix(model).ModelName)
533 return strings.Contains(baseModel, "codex")
508}534}
509 535
510// flattenAssistantContent converts assistant message content from array format536// flattenAssistantContent converts assistant message content from array format
@@ -539,6 +565,411 @@ func flattenAssistantContent(body []byte) []byte { · 28 unmodified lines
539 return result565 return result
540}566}
541 567
568func normalizeGitHubCopilotChatTools(body []byte) []byte {
569 tools := gjson.GetBytes(body, "tools")
570 if tools.Exists() {
571 filtered := "[]"
572 if tools.IsArray() {
573 for _, tool := range tools.Array() {
574 if tool.Get("type").String() != "function" {
575 continue
576 }
577 filtered, _ = sjson.SetRaw(filtered, "-1", tool.Raw)
578 }
579 }
580 body, _ = sjson.SetRawBytes(body, "tools", []byte(filtered))
581 }
582
583 toolChoice := gjson.GetBytes(body, "tool_choice")
584 if !toolChoice.Exists() {
585 return body
586 }
587 if toolChoice.Type == gjson.String {
588 switch toolChoice.String() {
589 case "auto", "none", "required":
590 return body
591 }
592 }
593 body, _ = sjson.SetBytes(body, "tool_choice", "auto")
594 return body
595}
596
597func normalizeGitHubCopilotResponsesInput(body []byte) []byte {
598 input := gjson.GetBytes(body, "input")
599 if input.Exists() {
600 if input.Type == gjson.String {
601 return body
602 }
603 inputString := input.Raw
604 if input.Type != gjson.JSON {
605 inputString = input.String()
606 }
607 body, _ = sjson.SetBytes(body, "input", inputString)
608 return body
609 }
610
611 var parts []string
612 if system := gjson.GetBytes(body, "system"); system.Exists() {
613 if text := strings.TrimSpace(collectTextFromNode(system)); text != "" {
614 parts = append(parts, text)
615 }
616 }
617 if messages := gjson.GetBytes(body, "messages"); messages.Exists() && messages.IsArray() {
618 for _, msg := range messages.Array() {
619 if text := strings.TrimSpace(collectTextFromNode(msg.Get("content"))); text != "" {
620 parts = append(parts, text)
621 }
622 }
623 }
624 body, _ = sjson.SetBytes(body, "input", strings.Join(parts, "\n"))
625 return body
626}
627
628func normalizeGitHubCopilotResponsesTools(body []byte) []byte {
629 tools := gjson.GetBytes(body, "tools")
630 if tools.Exists() {
631 filtered := "[]"
632 if tools.IsArray() {
633 for _, tool := range tools.Array() {
634 toolType := tool.Get("type").String()
635 // Accept OpenAI format (type="function") and Claude format
636 // (no type field, but has top-level name + input_schema).
637 if toolType != "" && toolType != "function" {
638 continue
639 }
640 name := tool.Get("name").String()
641 if name == "" {
642 name = tool.Get("function.name").String()
643 }
644 if name == "" {
645 continue
646 }
647 normalized := `{"type":"function","name":""}`
648 normalized, _ = sjson.Set(normalized, "name", name)
649 if desc := tool.Get("description").String(); desc != "" {
650 normalized, _ = sjson.Set(normalized, "description", desc)
651 } else if desc = tool.Get("function.description").String(); desc != "" {
652 normalized, _ = sjson.Set(normalized, "description", desc)
653 }
654 if params := tool.Get("parameters"); params.Exists() {
655 normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw)
656 } else if params = tool.Get("function.parameters"); params.Exists() {
657 normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw)
658 } else if params = tool.Get("input_schema"); params.Exists() {
659 normalized, _ = sjson.SetRaw(normalized, "parameters", params.Raw)
660 }
661 filtered, _ = sjson.SetRaw(filtered, "-1", normalized)
662 }
663 }
664 body, _ = sjson.SetRawBytes(body, "tools", []byte(filtered))
665 }
666
667 toolChoice := gjson.GetBytes(body, "tool_choice")
668 if !toolChoice.Exists() {
669 return body
670 }
671 if toolChoice.Type == gjson.String {
672 switch toolChoice.String() {
673 case "auto", "none", "required":
674 return body
675 default:
676 body, _ = sjson.SetBytes(body, "tool_choice", "auto")
677 return body
678 }
679 }
680 if toolChoice.Type == gjson.JSON {
681 choiceType := toolChoice.Get("type").String()
682 if choiceType == "function" {
683 name := toolChoice.Get("name").String()
684 if name == "" {
685 name = toolChoice.Get("function.name").String()
686 }
687 if name != "" {
688 normalized := `{"type":"function","name":""}`
689 normalized, _ = sjson.Set(normalized, "name", name)
690 body, _ = sjson.SetRawBytes(body, "tool_choice", []byte(normalized))
691 return body
692 }
693 }
694 }
695 body, _ = sjson.SetBytes(body, "tool_choice", "auto")
696 return body
697}
698
699func collectTextFromNode(node gjson.Result) string {
700 if !node.Exists() {
701 return ""
702 }
703 if node.Type == gjson.String {
704 return node.String()
705 }
706 if node.IsArray() {
707 var parts []string
708 for _, item := range node.Array() {
709 if item.Type == gjson.String {
710 if text := item.String(); text != "" {
711 parts = append(parts, text)
712 }
713 continue
714 }
715 if text := item.Get("text").String(); text != "" {
716 parts = append(parts, text)
717 continue
718 }
719 if nested := collectTextFromNode(item.Get("content")); nested != "" {
720 parts = append(parts, nested)
721 }
722 }
723 return strings.Join(parts, "\n")
724 }
725 if node.Type == gjson.JSON {
726 if text := node.Get("text").String(); text != "" {
727 return text
728 }
729 if nested := collectTextFromNode(node.Get("content")); nested != "" {
730 return nested
731 }
732 return node.Raw
733 }
734 return node.String()
735}
736
737type githubCopilotResponsesStreamToolState struct {
738 Index int
739 ID string
740 Name string
741}
742
743type githubCopilotResponsesStreamState struct {
744 MessageStarted bool
745 MessageStopSent bool
746 TextBlockStarted bool
747 TextBlockIndex int
748 NextContentIndex int
749 HasToolUse bool
750 OutputIndexToTool map[int]*githubCopilotResponsesStreamToolState
751 ItemIDToTool map[string]*githubCopilotResponsesStreamToolState
752}
753
754func translateGitHubCopilotResponsesNonStreamToClaude(data []byte) string {
755 root := gjson.ParseBytes(data)
756 out := `{"id":"","type":"message","role":"assistant","model":"","content":[],"stop_reason":null,"stop_sequence":null,"usage":{"input_tokens":0,"output_tokens":0}}`
757 out, _ = sjson.Set(out, "id", root.Get("id").String())
758 out, _ = sjson.Set(out, "model", root.Get("model").String())
759
760 hasToolUse := false
761 if output := root.Get("output"); output.Exists() && output.IsArray() {
762 for _, item := range output.Array() {
763 switch item.Get("type").String() {
764 case "message":
765 if content := item.Get("content"); content.Exists() && content.IsArray() {
766 for _, part := range content.Array() {
767 if part.Get("type").String() != "output_text" {
768 continue
769 }
770 text := part.Get("text").String()
771 if text == "" {
772 continue
773 }
774 block := `{"type":"text","text":""}`
775 block, _ = sjson.Set(block, "text", text)
776 out, _ = sjson.SetRaw(out, "content.-1", block)
777 }
778 }
779 case "function_call":
780 hasToolUse = true
781 toolUse := `{"type":"tool_use","id":"","name":"","input":{}}`
782 toolID := item.Get("call_id").String()
783 if toolID == "" {
784 toolID = item.Get("id").String()
785 }
786 toolUse, _ = sjson.Set(toolUse, "id", toolID)
787 toolUse, _ = sjson.Set(toolUse, "name", item.Get("name").String())
788 if args := item.Get("arguments").String(); args != "" && gjson.Valid(args) {
789 argObj := gjson.Parse(args)
790 if argObj.IsObject() {
791 toolUse, _ = sjson.SetRaw(toolUse, "input", argObj.Raw)
792 }
793 }
794 out, _ = sjson.SetRaw(out, "content.-1", toolUse)
795 }
796 }
797 }
798
799 inputTokens := root.Get("usage.input_tokens").Int()
800 outputTokens := root.Get("usage.output_tokens").Int()
801 out, _ = sjson.Set(out, "usage.input_tokens", inputTokens)
802 out, _ = sjson.Set(out, "usage.output_tokens", outputTokens)
803 if hasToolUse {
804 out, _ = sjson.Set(out, "stop_reason", "tool_use")
805 } else {
806 out, _ = sjson.Set(out, "stop_reason", "end_turn")
807 }
808 return out
809}
810
811func translateGitHubCopilotResponsesStreamToClaude(line []byte, param *any) []string {
812 if *param == nil {
813 *param = &githubCopilotResponsesStreamState{
814 TextBlockIndex: -1,
815 OutputIndexToTool: make(map[int]*githubCopilotResponsesStreamToolState),
816 ItemIDToTool: make(map[string]*githubCopilotResponsesStreamToolState),
817 }
818 }
819 state := (*param).(*githubCopilotResponsesStreamState)
820
821 if !bytes.HasPrefix(line, dataTag) {
822 return nil
823 }
824 payload := bytes.TrimSpace(line[5:])
825 if bytes.Equal(payload, []byte("[DONE]")) {
826 return nil
827 }
828 if !gjson.ValidBytes(payload) {
829 return nil
830 }
831
832 event := gjson.GetBytes(payload, "type").String()
833 results := make([]string, 0, 4)
834 ensureMessageStart := func() {
835 if state.MessageStarted {
836 return
837 }
838 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}}}`
839 messageStart, _ = sjson.Set(messageStart, "message.id", gjson.GetBytes(payload, "response.id").String())
840 messageStart, _ = sjson.Set(messageStart, "message.model", gjson.GetBytes(payload, "response.model").String())
841 results = append(results, "event: message_start\ndata: "+messageStart+"\n\n")
842 state.MessageStarted = true
843 }
844 startTextBlockIfNeeded := func() {
845 if state.TextBlockStarted {
846 return
847 }
848 if state.TextBlockIndex < 0 {
849 state.TextBlockIndex = state.NextContentIndex
850 state.NextContentIndex++
851 }
852 contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"text","text":""}}`
853 contentBlockStart, _ = sjson.Set(contentBlockStart, "index", state.TextBlockIndex)
854 results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n")
855 state.TextBlockStarted = true
856 }
857 stopTextBlockIfNeeded := func() {
858 if !state.TextBlockStarted {
859 return
860 }
861 contentBlockStop := `{"type":"content_block_stop","index":0}`
862 contentBlockStop, _ = sjson.Set(contentBlockStop, "index", state.TextBlockIndex)
863 results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n")
864 state.TextBlockStarted = false
865 state.TextBlockIndex = -1
866 }
867 resolveTool := func(itemID string, outputIndex int) *githubCopilotResponsesStreamToolState {
868 if itemID != "" {
869 if tool, ok := state.ItemIDToTool[itemID]; ok {
870 return tool
871 }
872 }
873 if tool, ok := state.OutputIndexToTool[outputIndex]; ok {
874 if itemID != "" {
875 state.ItemIDToTool[itemID] = tool
876 }
877 return tool
878 }
879 return nil
880 }
881
882 switch event {
883 case "response.created":
884 ensureMessageStart()
885 case "response.output_text.delta":
886 ensureMessageStart()
887 startTextBlockIfNeeded()
888 delta := gjson.GetBytes(payload, "delta").String()
889 if delta != "" {
890 contentDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"text_delta","text":""}}`
891 contentDelta, _ = sjson.Set(contentDelta, "index", state.TextBlockIndex)
892 contentDelta, _ = sjson.Set(contentDelta, "delta.text", delta)
893 results = append(results, "event: content_block_delta\ndata: "+contentDelta+"\n\n")
894 }
895 case "response.output_item.added":
896 if gjson.GetBytes(payload, "item.type").String() != "function_call" {
897 break
898 }
899 ensureMessageStart()
900 stopTextBlockIfNeeded()
901 state.HasToolUse = true
902 tool := &githubCopilotResponsesStreamToolState{
903 Index: state.NextContentIndex,
904 ID: gjson.GetBytes(payload, "item.call_id").String(),
905 Name: gjson.GetBytes(payload, "item.name").String(),
906 }
907 if tool.ID == "" {
908 tool.ID = gjson.GetBytes(payload, "item.id").String()
909 }
910 state.NextContentIndex++
911 outputIndex := int(gjson.GetBytes(payload, "output_index").Int())
912 state.OutputIndexToTool[outputIndex] = tool
913 if itemID := gjson.GetBytes(payload, "item.id").String(); itemID != "" {
914 state.ItemIDToTool[itemID] = tool
915 }
916 contentBlockStart := `{"type":"content_block_start","index":0,"content_block":{"type":"tool_use","id":"","name":"","input":{}}}`
917 contentBlockStart, _ = sjson.Set(contentBlockStart, "index", tool.Index)
918 contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.id", tool.ID)
919 contentBlockStart, _ = sjson.Set(contentBlockStart, "content_block.name", tool.Name)
920 results = append(results, "event: content_block_start\ndata: "+contentBlockStart+"\n\n")
921 case "response.output_item.delta":
922 item := gjson.GetBytes(payload, "item")
923 if item.Get("type").String() != "function_call" {
924 break
925 }
926 tool := resolveTool(item.Get("id").String(), int(gjson.GetBytes(payload, "output_index").Int()))
927 if tool == nil {
928 break
929 }
930 partial := gjson.GetBytes(payload, "delta").String()
931 if partial == "" {
932 partial = item.Get("arguments").String()
933 }
934 if partial == "" {
935 break
936 }
937 inputDelta := `{"type":"content_block_delta","index":0,"delta":{"type":"input_json_delta","partial_json":""}}`
938 inputDelta, _ = sjson.Set(inputDelta, "index", tool.Index)
939 inputDelta, _ = sjson.Set(inputDelta, "delta.partial_json", partial)
940 results = append(results, "event: content_block_delta\ndata: "+inputDelta+"\n\n")
941 case "response.output_item.done":
942 if gjson.GetBytes(payload, "item.type").String() != "function_call" {
943 break
944 }
945 tool := resolveTool(gjson.GetBytes(payload, "item.id").String(), int(gjson.GetBytes(payload, "output_index").Int()))
946 if tool == nil {
947 break
948 }
949 contentBlockStop := `{"type":"content_block_stop","index":0}`
950 contentBlockStop, _ = sjson.Set(contentBlockStop, "index", tool.Index)
951 results = append(results, "event: content_block_stop\ndata: "+contentBlockStop+"\n\n")
952 case "response.completed":
953 ensureMessageStart()
954 stopTextBlockIfNeeded()
955 if !state.MessageStopSent {
956 stopReason := "end_turn"
957 if state.HasToolUse {
958 stopReason = "tool_use"
959 }
960 messageDelta := `{"type":"message_delta","delta":{"stop_reason":"","stop_sequence":null},"usage":{"input_tokens":0,"output_tokens":0}}`
961 messageDelta, _ = sjson.Set(messageDelta, "delta.stop_reason", stopReason)
962 messageDelta, _ = sjson.Set(messageDelta, "usage.input_tokens", gjson.GetBytes(payload, "response.usage.input_tokens").Int())
963 messageDelta, _ = sjson.Set(messageDelta, "usage.output_tokens", gjson.GetBytes(payload, "response.usage.output_tokens").Int())
964 results = append(results, "event: message_delta\ndata: "+messageDelta+"\n\n")
965 results = append(results, "event: message_stop\ndata: {\"type\":\"message_stop\"}\n\n")
966 state.MessageStopSent = true
967 }
968 }
969
970 return results
971}
972
542// isHTTPSuccess checks if the status code indicates success (2xx).973// isHTTPSuccess checks if the status code indicates success (2xx).
543func isHTTPSuccess(statusCode int) bool {974func isHTTPSuccess(statusCode int) bool {
544 return statusCode >= 200 && statusCode < 300975 return statusCode >= 200 && statusCode < 300

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t