History

feat(registry): add supported endpoints for GitHub Copilot models

Enhance model definitions by including supported API endpoints for each model. This allows for better integration and usage tracking with the GitHub Copilot API.

ChrAlpha committed 10 months ago

Commit 0ffcce31 parent f4fcfc54 files · +132 −9Browse files

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

Unified · Split

internal/runtime/executor/github_copilot_executor.go+33−3
@@ -23,6 +23,7 @@ import ( · 22 unmodified lines
2323 const (
2424 githubCopilotBaseURL = "https://api.githubcopilot.com"
2525 githubCopilotChatPath = "/chat/completions"
26+ githubCopilotResponsesPath = "/responses"
2627 githubCopilotAuthType = "github-copilot"
2728 githubCopilotTokenCacheTTL = 25 * time.Minute
2829 // tokenExpiryBuffer is the time before expiry when we should refresh the token.
@@ -106,7 +107,11 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 77 unmodified lines
106107 defer reporter.trackFailure(ctx, &err)
107108
108109 from := opts.SourceFormat
110+ useResponses := useGitHubCopilotResponsesEndpoint(from)
109111 to := sdktranslator.FromString("openai")
112+ if useResponses {
113+ to = sdktranslator.FromString("openai-response")
114+ }
110115 originalPayload := bytes.Clone(req.Payload)
111116 if len(opts.OriginalRequest) > 0 {
112117 originalPayload = bytes.Clone(opts.OriginalRequest)
@@ -117,7 +122,11 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 4 unmodified lines
117122 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated)
118123 body, _ = sjson.SetBytes(body, "stream", false)
119124
120− url := githubCopilotBaseURL + githubCopilotChatPath
125+ path := githubCopilotChatPath
126+ if useResponses {
127+ path = githubCopilotResponsesPath
128+ }
129+ url := githubCopilotBaseURL + path
121130 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
122131 if err != nil {
123132 return resp, err
@@ -172,6 +181,9 @@ func (e *GitHubCopilotExecutor) Execute(ctx context.Context, auth *cliproxyauth. · 48 unmodified lines
172181 appendAPIResponseChunk(ctx, e.cfg, data)
173182
174183 detail := parseOpenAIUsage(data)
184+ if useResponses && detail.TotalTokens == 0 {
185+ detail = parseOpenAIResponsesUsage(data)
186+ }
175187 if detail.TotalTokens > 0 {
176188 reporter.publish(ctx, detail)
177189 }
@@ -194,7 +206,11 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 16 unmodified lines
194206 defer reporter.trackFailure(ctx, &err)
195207
196208 from := opts.SourceFormat
209+ useResponses := useGitHubCopilotResponsesEndpoint(from)
197210 to := sdktranslator.FromString("openai")
211+ if useResponses {
212+ to = sdktranslator.FromString("openai-response")
213+ }
198214 originalPayload := bytes.Clone(req.Payload)
199215 if len(opts.OriginalRequest) > 0 {
200216 originalPayload = bytes.Clone(opts.OriginalRequest)
@@ -205,9 +221,15 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 4 unmodified lines
205221 body = applyPayloadConfigWithRoot(e.cfg, req.Model, to.String(), "", body, originalTranslated)
206222 body, _ = sjson.SetBytes(body, "stream", true)
207223 // Enable stream options for usage stats in stream
208− body, _ = sjson.SetBytes(body, "stream_options.include_usage", true)
224+ if !useResponses {
225+ body, _ = sjson.SetBytes(body, "stream_options.include_usage", true)
226+ }
209227
210− url := githubCopilotBaseURL + githubCopilotChatPath
228+ path := githubCopilotChatPath
229+ if useResponses {
230+ path = githubCopilotResponsesPath
231+ }
232+ url := githubCopilotBaseURL + path
211233 httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
212234 if err != nil {
213235 return nil, err
@@ -283,6 +305,10 @@ func (e *GitHubCopilotExecutor) ExecuteStream(ctx context.Context, auth *cliprox · 69 unmodified lines
283305 }
284306 if detail, ok := parseOpenAIStreamUsage(line); ok {
285307 reporter.publish(ctx, detail)
308+ } else if useResponses {
309+ if detail, ok := parseOpenAIResponsesStreamUsage(line); ok {
310+ reporter.publish(ctx, detail)
311+ }
286312 }
287313 }
288314
@@ -393,6 +419,10 @@ func (e *GitHubCopilotExecutor) normalizeModel(_ string, body []byte) []byte { · 104 unmodified lines
393419 return body
394420 }
395421
422+func useGitHubCopilotResponsesEndpoint(sourceFormat sdktranslator.Format) bool {
423+ return sourceFormat.String() == "openai-response"
424+}
425+
396426 // isHTTPSuccess checks if the status code indicates success (2xx).
397427 func isHTTPSuccess(statusCode int) bool {
398428 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