Skip to content

Commit f29166e

Browse files
authored
Merge pull request #28 from m7medVision/25-fix-windows-problem
fix all problem and test the windows version
2 parents 1cfe0cf + 373c4dd commit f29166e

File tree

3 files changed

+26
-58
lines changed

3 files changed

+26
-58
lines changed

cmd/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -122,7 +122,7 @@ func runInteractiveConfig() {
122122
// Dynamically generate available models for OpenAI
123123
availableModels := map[string][]string{
124124
"openai": {},
125-
"copilot": {"gpt-5-mini"}, // TODO: update if copilot models are dynamic
125+
"copilot": {"openai/gpt-5-mini"}, // TODO: update if copilot models are dynamic
126126
}
127127

128128
modelDisplayToID := map[string]string{}

internal/config/config.go

Lines changed: 24 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import (
55
"fmt"
66
"net/url"
77
"os"
8+
"os/exec"
89
"path/filepath"
910
"runtime"
1011
"strings"
@@ -13,8 +14,8 @@ import (
1314
)
1415

1516
type ProviderConfig struct {
16-
APIKey string `mapstructure:"api_key"`
17-
Model string `mapstructure:"model"`
17+
APIKey string `mapstructure:"api_key"`
18+
Model string `mapstructure:"model"`
1819
EndpointURL string `mapstructure:"endpoint_url"`
1920
}
2021

@@ -37,7 +38,7 @@ func InitConfig() {
3738
viper.SetDefault("providers.copilot.model", "openai/gpt-5-mini")
3839
} else {
3940
viper.SetDefault("active_provider", "openai")
40-
viper.SetDefault("providers.openai.model", "gpt-5-mini")
41+
viper.SetDefault("providers.openai.model", "openai/gpt-5-mini")
4142
}
4243

4344
viper.AutomaticEnv()
@@ -90,9 +91,6 @@ func GetAPIKey() (string, error) {
9091
if cfg == nil {
9192
InitConfig()
9293
}
93-
if cfg.ActiveProvider == "copilot" {
94-
return LoadGitHubToken()
95-
}
9694

9795
providerConfig, err := GetActiveProviderConfig()
9896
if err != nil {
@@ -213,12 +211,9 @@ func SetEndpoint(provider, endpoint string) error {
213211
}
214212

215213
func LoadGitHubToken() (string, error) {
216-
if token := os.Getenv("GITHUB_TOKEN"); token != "" {
217-
return token, nil
218-
}
219-
220-
if token := os.Getenv("GITHUB_MODELS_TOKEN"); token != "" {
221-
return token, nil
214+
tok, err := tryGetTokenFromGHCLI()
215+
if err == nil && tok != "" {
216+
return tok, nil
222217
}
223218

224219
configDir := getConfigDir()
@@ -248,20 +243,30 @@ func LoadGitHubToken() (string, error) {
248243
}
249244
}
250245

251-
return "", fmt.Errorf("GitHub token not found. Please set GITHUB_TOKEN or GITHUB_MODELS_TOKEN environment variable with a Personal Access Token that has 'models' scope")
246+
return "", fmt.Errorf("GitHub token not found via 'gh auth token'; run 'gh auth login' to authenticate the GitHub CLI")
247+
}
248+
func tryGetTokenFromGHCLI() (string, error) {
249+
out, err := exec.Command("gh", "auth", "token").Output()
250+
if err != nil {
251+
return "", err
252+
}
253+
tok := strings.TrimSpace(string(out))
254+
if tok == "" {
255+
return "", fmt.Errorf("gh returned empty token")
256+
}
257+
return tok, nil
252258
}
253259

254260
func getConfigDir() string {
255261
if xdgConfig := os.Getenv("XDG_CONFIG_HOME"); xdgConfig != "" {
256262
return xdgConfig
257-
258-
// WARNING: The code is not woking
259263
} else if runtime.GOOS == "windows" {
260-
if localAppData := os.Getenv("LOCALAPPDATA"); localAppData != "" {
261-
return localAppData
262-
} else {
263-
return filepath.Join(os.Getenv("HOME"), "AppData", "Local")
264+
homeDir, err := os.UserHomeDir()
265+
if err != nil {
266+
fmt.Println("Error getting user home directory:", err)
267+
os.Exit(1)
264268
}
269+
return filepath.Join(homeDir, "AppData", "Local")
265270
} else {
266271
return filepath.Join(os.Getenv("HOME"), ".config")
267272
}

internal/provider/copilot.go

Lines changed: 1 addition & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ package provider
22

33
import (
44
"context"
5-
"encoding/json"
65
"fmt"
76
"net/http"
87
"os"
@@ -59,39 +58,6 @@ func normalizeCopilotModel(model string) string {
5958
return m
6059
}
6160

62-
func (c *CopilotProvider) exchangeGitHubToken(ctx context.Context, githubToken string) (string, error) {
63-
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/copilot_internal/v2/token", nil)
64-
if err != nil {
65-
return "", fmt.Errorf("failed creating token request: %w", err)
66-
}
67-
req.Header.Set("Authorization", "Token "+githubToken)
68-
req.Header.Set("User-Agent", "lazycommit/1.0")
69-
70-
resp, err := c.httpClient.Do(req)
71-
if err != nil {
72-
return "", fmt.Errorf("failed exchanging token: %w", err)
73-
}
74-
defer resp.Body.Close()
75-
if resp.StatusCode != http.StatusOK {
76-
var body struct {
77-
Message string `json:"message"`
78-
}
79-
_ = json.NewDecoder(resp.Body).Decode(&body)
80-
return "", fmt.Errorf("token exchange failed: %d %s", resp.StatusCode, body.Message)
81-
}
82-
var tr struct {
83-
Token string `json:"token"`
84-
ExpiresAt int64 `json:"expires_at"`
85-
}
86-
if err := json.NewDecoder(resp.Body).Decode(&tr); err != nil {
87-
return "", fmt.Errorf("failed decoding token response: %w", err)
88-
}
89-
if tr.Token == "" {
90-
return "", fmt.Errorf("empty copilot bearer token")
91-
}
92-
return tr.Token, nil
93-
}
94-
9561
func (c *CopilotProvider) getGitHubToken() string {
9662
if c.apiKey != "" {
9763
return c.apiKey
@@ -122,10 +88,7 @@ func (c *CopilotProvider) GenerateCommitMessages(ctx context.Context, diff strin
12288
return nil, fmt.Errorf("GitHub token is required for Copilot provider")
12389
}
12490

125-
bearer, err := c.exchangeGitHubToken(ctx, githubToken)
126-
if err != nil {
127-
return nil, err
128-
}
91+
bearer := githubToken
12992

13093
client := openai.NewClient(
13194
option.WithBaseURL(c.endpoint),

0 commit comments

Comments
 (0)