Skip to content

Commit 5faa67b

Browse files
committed
feat: add Github Copilot provider
1 parent a9557aa commit 5faa67b

File tree

4 files changed

+98
-0
lines changed

4 files changed

+98
-0
lines changed

go.mod

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,12 +19,16 @@ require (
1919
golang.org/x/oauth2 v0.35.0
2020
)
2121

22+
23+
2224
require (
2325
github.com/andybalholm/brotli v1.2.0 // indirect
2426
github.com/bytedance/gopkg v0.1.3 // indirect
2527
github.com/bytedance/sonic v1.15.0 // indirect
2628
github.com/bytedance/sonic/loader v0.5.0 // indirect
2729
github.com/cloudwego/base64x v0.1.6 // indirect
30+
github.com/github/copilot-sdk/go v0.1.23
31+
github.com/google/jsonschema-go v0.4.2 // indirect
2832
github.com/go-resty/resty/v2 v2.17.1 // indirect
2933
github.com/gogo/protobuf v1.3.2 // indirect
3034
github.com/grbit/go-json v0.11.0 // indirect
@@ -43,4 +47,5 @@ require (
4347
golang.org/x/net v0.50.0 // indirect
4448
golang.org/x/sync v0.19.0 // indirect
4549
golang.org/x/sys v0.41.0 // indirect
50+
4651
)

go.sum

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,8 @@ github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSs
3232
github.com/dgryski/go-rendezvous v0.0.0-20200823014737-9f7001d12a5f/go.mod h1:cuUVRXasLTGF7a8hSLbxyZXjz+1KgoB3wDUb6vlszIc=
3333
github.com/fsnotify/fsnotify v1.4.7/go.mod h1:jwhsz4b93w/PPRr/qN1Yymfu8t87LnFCMoQvtojpjFo=
3434
github.com/fsnotify/fsnotify v1.4.9/go.mod h1:znqG4EE+3YCdAaPaxE2ZRY/06pZUdp0tY4IgpuI1SZQ=
35+
github.com/github/copilot-sdk/go v0.1.23 h1:uExtO/inZQndCZMiSAA1hvXINiz9tqo/MZgQzFzurxw=
36+
github.com/github/copilot-sdk/go v0.1.23/go.mod h1:GdwwBfMbm9AABLEM3x5IZKw4ZfwCYxZ1BgyytmZenQ0=
3537
github.com/go-redis/redis/v8 v8.11.4/go.mod h1:2Z2wHZXdQpCDXEGzqMockDpNyYvi2l4Pxt6RJr792+w=
3638
github.com/go-resty/resty/v2 v2.6.0/go.mod h1:PwvJS6hvaPkjtjNg9ph+VrSD92bi5Zq73w/BIH7cC3Q=
3739
github.com/go-resty/resty/v2 v2.17.1 h1:x3aMpHK1YM9e4va/TMDRlusDDoZiQ+ViDu/WpA6xTM4=
@@ -56,6 +58,8 @@ github.com/google/go-cmp v0.4.0/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/
5658
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5759
github.com/google/go-cmp v0.5.6/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
5860
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
61+
github.com/google/jsonschema-go v0.4.2 h1:tmrUohrwoLZZS/P3x7ex0WAVknEkBZM46iALbcqoRA8=
62+
github.com/google/jsonschema-go v0.4.2/go.mod h1:r5quNTdLOYEz95Ru18zA0ydNbBuYoo9tgaYcxEYhJVE=
5963
github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=
6064
github.com/google/uuid v1.6.0 h1:NIvaJDMOsjHA8n1jAhLSgzrAzy1Hgr+hNrb57e+94F0=
6165
github.com/google/uuid v1.6.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo=

pkg/config/config.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -168,15 +168,19 @@ type ProvidersConfig struct {
168168
Moonshot ProviderConfig `json:"moonshot"`
169169
ShengSuanYun ProviderConfig `json:"shengsuanyun"`
170170
DeepSeek ProviderConfig `json:"deepseek"`
171+
GitHubCopilot ProviderConfig `json:"github_copilot"`
171172
}
172173

173174
type ProviderConfig struct {
174175
APIKey string `json:"api_key" env:"PICOCLAW_PROVIDERS_{{.Name}}_API_KEY"`
175176
APIBase string `json:"api_base" env:"PICOCLAW_PROVIDERS_{{.Name}}_API_BASE"`
176177
Proxy string `json:"proxy,omitempty" env:"PICOCLAW_PROVIDERS_{{.Name}}_PROXY"`
177178
AuthMethod string `json:"auth_method,omitempty" env:"PICOCLAW_PROVIDERS_{{.Name}}_AUTH_METHOD"`
179+
ConnectMode string `json:"connect_mode,omitempty" env:"PICOCLAW_PROVIDERS_{{.Name}}_CONNECT_MODE"` //only for Github Copilot, `stdio` or `grpc`
178180
}
179181

182+
183+
180184
type GatewayConfig struct {
181185
Host string `json:"host" env:"PICOCLAW_GATEWAY_HOST"`
182186
Port int `json:"port" env:"PICOCLAW_GATEWAY_PORT"`
Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,85 @@
1+
package providers
2+
3+
import (
4+
"context"
5+
"fmt"
6+
7+
json "encoding/json"
8+
copilot "github.com/github/copilot-sdk/go"
9+
)
10+
11+
type GitHubCopilotProvider struct {
12+
uri string
13+
connectMode string // `stdio` or `grpc``
14+
15+
session *copilot.Session
16+
}
17+
18+
func NewGitHubCopilotProvider(uri string, connectMode string, model string) *GitHubCopilotProvider {
19+
20+
var session *copilot.Session
21+
22+
switch connectMode {
23+
24+
case "stdio":
25+
26+
case "grpc":
27+
client := copilot.NewClient(&copilot.ClientOptions{
28+
CLIUrl: uri,
29+
})
30+
if err := client.Start(context.Background()); err != nil {
31+
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")
32+
}
33+
defer client.Stop()
34+
session, _ = client.CreateSession(context.Background(), &copilot.SessionConfig{
35+
Model: model,
36+
Hooks: &copilot.SessionHooks{
37+
38+
},
39+
})
40+
}
41+
42+
return &GitHubCopilotProvider{
43+
uri: uri,
44+
connectMode: connectMode,
45+
46+
session: session,
47+
}
48+
}
49+
50+
// Chat sends a chat request to GitHub Copilot
51+
func (p *GitHubCopilotProvider) Chat(ctx context.Context, messages []Message, tools []ToolDefinition, model string, options map[string]interface{}) (*LLMResponse, error) {
52+
type tempMessage struct {
53+
Role string `json:"role"`
54+
Content string `json:"content"`
55+
}
56+
out := make([]tempMessage, 0, len(messages))
57+
58+
for _, msg := range messages {
59+
out = append(out, tempMessage{
60+
Role: msg.Role,
61+
Content: msg.Content,
62+
})
63+
}
64+
65+
fullcontent,_ := json.Marshal(out)
66+
67+
68+
69+
70+
content,_ := p.session.Send(ctx,copilot.MessageOptions{
71+
Prompt: string(fullcontent),
72+
})
73+
74+
return &LLMResponse{
75+
FinishReason : "stop",
76+
Content: content,
77+
}, nil
78+
79+
}
80+
81+
82+
func (p *GitHubCopilotProvider) GetDefaultModel() string {
83+
84+
return "gpt-4.1"
85+
}

0 commit comments

Comments
 (0)