@@ -2,60 +2,78 @@ package providers
22
33import (
44 "context"
5+ "encoding/json"
56 "fmt"
6-
7- json "encoding/json"
7+ "sync"
88
99 copilot "github.com/github/copilot-sdk/go"
1010)
1111
1212type GitHubCopilotProvider struct {
1313 uri string
14- connectMode string // ` stdio` or ` grpc``
14+ connectMode string // " stdio" or " grpc"
1515
16+ client * copilot.Client
1617 session * copilot.Session
18+
19+
20+ mu sync.Mutex
1721}
1822
1923func NewGitHubCopilotProvider (uri string , connectMode string , model string ) (* GitHubCopilotProvider , error ) {
20-
21- var session * copilot.Session
2224 if connectMode == "" {
2325 connectMode = "grpc"
2426 }
25- switch connectMode {
2627
28+ switch connectMode {
2729 case "stdio" :
28- //todo
30+ // TODO:
31+ return nil , fmt .Errorf ("stdio mode not implemented" )
2932 case "grpc" :
3033 client := copilot .NewClient (& copilot.ClientOptions {
3134 CLIUrl : uri ,
3235 })
3336 if err := client .Start (context .Background ()); err != nil {
34- return nil , fmt .Errorf ("Can 't connect to Github Copilot, https://github.com/github/copilot-sdk/blob/main/ docs/getting-started.md#connecting-to-an-external-cli-server for details" )
37+ return nil , fmt .Errorf ("can 't connect to Github Copilot: %w; see docs for details" , err )
3538 }
36- defer client .Stop ()
37- session , _ = client .CreateSession (context .Background (), & copilot.SessionConfig {
39+
40+
41+ session , err := client .CreateSession (context .Background (), & copilot.SessionConfig {
3842 Model : model ,
3943 Hooks : & copilot.SessionHooks {},
4044 })
45+ if err != nil {
46+
47+ client .Stop ()
48+ return nil , fmt .Errorf ("create session failed: %w" , err )
49+ }
4150
51+ return & GitHubCopilotProvider {
52+ uri : uri ,
53+ connectMode : connectMode ,
54+ client : client ,
55+ session : session ,
56+ }, nil
57+ default :
58+ return nil , fmt .Errorf ("unknown connect mode: %s" , connectMode )
4259 }
60+ }
4361
44- return & GitHubCopilotProvider {
45- uri : uri ,
46- connectMode : connectMode ,
47- session : session ,
48- }, nil
62+ func (p * GitHubCopilotProvider ) Close () {
63+ if p .client != nil {
64+ p .client .Stop ()
65+ p .client = nil
66+ p .session = nil
67+ }
4968}
5069
51- // Chat sends a chat request to GitHub Copilot
70+
5271func (p * GitHubCopilotProvider ) Chat (ctx context.Context , messages []Message , tools []ToolDefinition , model string , options map [string ]interface {}) (* LLMResponse , error ) {
5372 type tempMessage struct {
5473 Role string `json:"role"`
5574 Content string `json:"content"`
5675 }
5776 out := make ([]tempMessage , 0 , len (messages ))
58-
5977 for _ , msg := range messages {
6078 out = append (out , tempMessage {
6179 Role : msg .Role ,
@@ -64,18 +82,28 @@ func (p *GitHubCopilotProvider) Chat(ctx context.Context, messages []Message, to
6482 }
6583
6684 fullcontent , _ := json .Marshal (out )
85+ p .mu .Lock ()
86+ defer p .mu .Unlock ()
6787
68- content , _ := p .session .Send (ctx , copilot.MessageOptions {
88+
89+ resp , err := p .session .SendAndWait (ctx , copilot.MessageOptions {
6990 Prompt : string (fullcontent ),
7091 })
92+ if err != nil {
93+ return nil , err
94+ }
95+
96+
97+ var content string
98+ if resp != nil && resp .Data .Content != nil {
99+ content = * resp .Data .Content
100+ }
71101
72102 return & LLMResponse {
73103 FinishReason : "stop" ,
74104 Content : content ,
75105 }, nil
76-
77106}
78-
79107func (p * GitHubCopilotProvider ) GetDefaultModel () string {
80108
81109 return "gpt-4.1"
0 commit comments