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 (
|
import (
|
||||||
"context"
|
"context"
|
||||||
|
"encoding/json"
|
||||||
"fmt"
|
"fmt"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
json "encoding/json"
|
|
||||||
|
|
||||||
copilot "github.com/github/copilot-sdk/go"
|
copilot "github.com/github/copilot-sdk/go"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
@ -19,16 +18,15 @@ type GitHubCopilotProvider struct {
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error) {
|
func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*GitHubCopilotProvider, error) {
|
||||||
|
|
||||||
var session *copilot.Session
|
var session *copilot.Session
|
||||||
var client *copilot.Client
|
var client *copilot.Client
|
||||||
if connectMode == "" {
|
if connectMode == "" {
|
||||||
connectMode = "grpc"
|
connectMode = "grpc"
|
||||||
}
|
}
|
||||||
switch connectMode {
|
switch connectMode {
|
||||||
|
|
||||||
case "stdio":
|
case "stdio":
|
||||||
//todo
|
// TODO: implement stdio connect mode
|
||||||
|
return nil, fmt.Errorf("stdio connect mode is not yet implemented")
|
||||||
case "grpc":
|
case "grpc":
|
||||||
client = copilot.NewClient(&copilot.ClientOptions{
|
client = copilot.NewClient(&copilot.ClientOptions{
|
||||||
CLIUrl: uri,
|
CLIUrl: uri,
|
||||||
|
|
@ -36,7 +34,7 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
||||||
connectCtx, connectCancel := context.WithTimeout(context.Background(), 15*time.Second)
|
connectCtx, connectCancel := context.WithTimeout(context.Background(), 15*time.Second)
|
||||||
defer connectCancel()
|
defer connectCancel()
|
||||||
if err := client.Start(connectCtx); err != nil {
|
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
|
var err error
|
||||||
session, err = client.CreateSession(connectCtx, &copilot.SessionConfig{
|
session, err = client.CreateSession(connectCtx, &copilot.SessionConfig{
|
||||||
|
|
@ -47,7 +45,8 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
||||||
client.Stop()
|
client.Stop()
|
||||||
return nil, fmt.Errorf("failed to create Copilot session: %w", err)
|
return nil, fmt.Errorf("failed to create Copilot session: %w", err)
|
||||||
}
|
}
|
||||||
|
default:
|
||||||
|
return nil, fmt.Errorf("unsupported connect mode %q", connectMode)
|
||||||
}
|
}
|
||||||
|
|
||||||
return &GitHubCopilotProvider{
|
return &GitHubCopilotProvider{
|
||||||
|
|
@ -58,10 +57,11 @@ func NewGitHubCopilotProvider(uri string, connectMode string, model string) (*Gi
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GitHubCopilotProvider) Close() {
|
func (p *GitHubCopilotProvider) Close() error {
|
||||||
if p.client != nil {
|
if p.client != nil {
|
||||||
p.client.Stop()
|
p.client.Stop()
|
||||||
}
|
}
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Chat sends a chat request to GitHub Copilot
|
// 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{
|
event, err := p.session.SendAndWait(ctx, copilot.MessageOptions{
|
||||||
Prompt: string(fullcontent),
|
Prompt: string(fullcontent),
|
||||||
|
|
@ -96,10 +103,8 @@ func (p *GitHubCopilotProvider) Chat(ctx context.Context, messages []Message, to
|
||||||
FinishReason: "stop",
|
FinishReason: "stop",
|
||||||
Content: *event.Data.Content,
|
Content: *event.Data.Content,
|
||||||
}, nil
|
}, nil
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (p *GitHubCopilotProvider) GetDefaultModel() string {
|
func (p *GitHubCopilotProvider) GetDefaultModel() string {
|
||||||
|
|
||||||
return "gpt-4.1"
|
return "gpt-4.1"
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue