fix(providers): address Copilot reviewer feedback
- Remove json import alias (use standard encoding/json) - Fix capitalization: 'Github' -> 'GitHub' in error message - Fix //todo -> // TODO: comment formatting - stdio case now returns explicit error (not yet implemented) - Add default case returning error for unsupported connect modes - Close() now returns error to satisfy io.Closer interface - Add nil guard for p.session in Chat() - Handle json.Marshal error instead of ignoring it - Remove extra blank lines for consistency with codebase style
This commit is contained in:
parent
c7da41d023
commit
ce22c31c87
1 changed files with 16 additions and 11 deletions
|
|
@ -2,11 +2,10 @@ package providers
|
|||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"time"
|
||||
|
||||
json "encoding/json"
|
||||
|
||||
copilot "github.com/github/copilot-sdk/go"
|
||||
)
|
||||
|
||||
|
|
@ -19,16 +18,15 @@ type GitHubCopilotProvider struct {
|
|||
}
|
||||
|
||||
func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error) {
|
||||
|
||||
var session *copilot.Session
|
||||
var client *copilot.Client
|
||||
if connectMode == "" {
|
||||
connectMode = "grpc"
|
||||
}
|
||||
switch connectMode {
|
||||
|
||||
case "stdio":
|
||||
//todo
|
||||
// TODO: implement stdio connect mode
|
||||
return nil, fmt.Errorf("stdio connect mode is not yet implemented")
|
||||
case "grpc":
|
||||
client = copilot.NewClient(&copilot.ClientOptions{
|
||||
CLIUrl: uri,
|
||||
|
|
@ -36,7 +34,7 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
|||
connectCtx, connectCancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||
defer connectCancel()
|
||||
if err := client.Start(connectCtx); err != nil {
|
||||
return nil, fmt.Errorf("can't connect to Github Copilot: %w", err)
|
||||
return nil, fmt.Errorf("can't connect to GitHub Copilot: %w", err)
|
||||
}
|
||||
var err error
|
||||
session, err = client.CreateSession(connectCtx, &copilot.SessionConfig{
|
||||
|
|
@ -47,7 +45,8 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
|||
client.Stop()
|
||||
return nil, fmt.Errorf("failed to create Copilot session: %w", err)
|
||||
}
|
||||
|
||||
default:
|
||||
return nil, fmt.Errorf("unsupported connect mode %q", connectMode)
|
||||
}
|
||||
|
||||
return &GitHubCopilotProvider{
|
||||
|
|
@ -58,10 +57,11 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
|||
}, nil
|
||||
}
|
||||
|
||||
func (p *GitHubCopilotProvider) Close() {
|
||||
func (p *GitHubCopilotProvider) Close() error {
|
||||
if p.client != nil {
|
||||
p.client.Stop()
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// Chat sends a chat request to GitHub Copilot
|
||||
|
|
@ -79,7 +79,14 @@ func (p *GitHubCopilotProvider) Chat(ctx context.Context, messages []Message, to
|
|||
})
|
||||
}
|
||||
|
||||
fullcontent, _ := json.Marshal(out)
|
||||
if p.session == nil {
|
||||
return nil, fmt.Errorf("copilot session is not initialized")
|
||||
}
|
||||
|
||||
fullcontent, err := json.Marshal(out)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to marshal messages: %w", err)
|
||||
}
|
||||
|
||||
event, err := p.session.SendAndWait(ctx, copilot.MessageOptions{
|
||||
Prompt: string(fullcontent),
|
||||
|
|
@ -96,10 +103,8 @@ func (p *GitHubCopilotProvider) Chat(ctx context.Context, messages []Message, to
|
|||
FinishReason: "stop",
|
||||
Content: *event.Data.Content,
|
||||
}, nil
|
||||
|
||||
}
|
||||
|
||||
func (p *GitHubCopilotProvider) GetDefaultModel() string {
|
||||
|
||||
return "gpt-4.1"
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue