-
Notifications
You must be signed in to change notification settings - Fork 7
better handle token in CopilotProvider #29
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,9 +2,11 @@ package provider | |
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
| "net/http" | ||
| "os" | ||
| "runtime" | ||
| "strings" | ||
| "time" | ||
|
|
||
|
|
@@ -58,6 +60,39 @@ func normalizeCopilotModel(model string) string { | |
| return m | ||
| } | ||
|
|
||
| func (c *CopilotProvider) exchangeGitHubToken(ctx context.Context, githubToken string) (string, error) { | ||
| req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/copilot_internal/v2/token", nil) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed creating token request: %w", err) | ||
| } | ||
| req.Header.Set("Authorization", "Token "+githubToken) | ||
| req.Header.Set("User-Agent", "lazycommit/1.0") | ||
|
|
||
| resp, err := c.httpClient.Do(req) | ||
| if err != nil { | ||
| return "", fmt.Errorf("failed exchanging token: %w", err) | ||
| } | ||
| defer resp.Body.Close() | ||
| if resp.StatusCode != http.StatusOK { | ||
| var body struct { | ||
| Message string `json:"message"` | ||
| } | ||
| _ = json.NewDecoder(resp.Body).Decode(&body) | ||
| return "", fmt.Errorf("token exchange failed: %d %s", resp.StatusCode, body.Message) | ||
| } | ||
|
Comment on lines
+76
to
+82
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The error from if resp.StatusCode != http.StatusOK {
var body struct {
Message string `json:"message"`
}
if err := json.NewDecoder(resp.Body).Decode(&body); err == nil && body.Message != "" {
return "", fmt.Errorf("token exchange failed: %d %s", resp.StatusCode, body.Message)
}
return "", fmt.Errorf("token exchange failed with status code: %d", resp.StatusCode)
} |
||
| var tr struct { | ||
| Token string `json:"token"` | ||
| ExpiresAt int64 `json:"expires_at"` | ||
| } | ||
|
Comment on lines
+83
to
+86
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil { | ||
| return "", fmt.Errorf("failed decoding token response: %w", err) | ||
| } | ||
| if tr.Token == "" { | ||
| return "", fmt.Errorf("empty copilot bearer token") | ||
| } | ||
| return tr.Token, nil | ||
| } | ||
|
|
||
| func (c *CopilotProvider) getGitHubToken() string { | ||
| if c.apiKey != "" { | ||
| return c.apiKey | ||
|
|
@@ -88,7 +123,19 @@ func (c *CopilotProvider) GenerateCommitMessages(ctx context.Context, diff strin | |
| return nil, fmt.Errorf("GitHub token is required for Copilot provider") | ||
| } | ||
|
|
||
| bearer := githubToken | ||
| var bearer string | ||
| var err error | ||
|
|
||
| // On Windows, use the token directly; on other platforms, exchange it for a Copilot token | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. |
||
| if runtime.GOOS == "windows" { | ||
| bearer = githubToken | ||
| } else { | ||
| bearer, err = c.exchangeGitHubToken(ctx, githubToken) | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
| } | ||
|
|
||
|
|
||
| client := openai.NewClient( | ||
| option.WithBaseURL(c.endpoint), | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
For better code clarity and maintainability, consider replacing this anonymous struct with a named struct defined at the package level, for example
copilotErrorResponse. This makes the code easier to read and allows for reuse.