1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
|
// Summary: GitHub Copilot client for chat and Codex-style code completion.
package llm
import (
"bytes"
"context"
"encoding/base64"
"encoding/json"
"errors"
"fmt"
"io"
"net/http"
"regexp"
"strings"
"time"
appver "codeberg.org/snonux/hexai/internal"
"codeberg.org/snonux/hexai/internal/logging"
)
// copilotClient implements Client against GitHub Copilot's Chat Completions API.
type copilotClient struct {
httpClient *http.Client
apiKey string
baseURL string
defaultModel string
chatLogger logging.ChatLogger
defaultTemperature *float64
// cached Copilot session token retrieved from GitHub API using apiKey
sessionToken string
tokenExpiry time.Time
}
type copilotChatRequest struct {
Model string `json:"model"`
Messages []copilotMessage `json:"messages"`
Temperature *float64 `json:"temperature,omitempty"`
MaxTokens *int `json:"max_tokens,omitempty"`
Stop []string `json:"stop,omitempty"`
}
type copilotMessage struct {
Role string `json:"role"`
Content string `json:"content"`
}
type copilotChatResponse struct {
Choices []struct {
Index int `json:"index"`
Message struct {
Role string `json:"role"`
Content string `json:"content"`
} `json:"message"`
FinishReason string `json:"finish_reason"`
} `json:"choices"`
Error *struct {
Message string `json:"message"`
Type string `json:"type"`
Param any `json:"param"`
Code any `json:"code"`
} `json:"error,omitempty"`
}
// Constructor (kept among the first functions by convention)
func newCopilot(baseURL, model, apiKey string, defaultTemp *float64) Client {
if strings.TrimSpace(baseURL) == "" {
baseURL = "https://api.githubcopilot.com"
}
if strings.TrimSpace(model) == "" {
// GitHub Models (Copilot API) commonly supports gpt-4o/gpt-4o-mini.
// Default to a broadly available, cost-effective option.
model = "gpt-4o-mini"
}
return copilotClient{
httpClient: &http.Client{Timeout: 30 * time.Second},
apiKey: apiKey,
baseURL: strings.TrimRight(baseURL, "/"),
defaultModel: model,
chatLogger: logging.NewChatLogger("copilot"),
defaultTemperature: defaultTemp,
}
}
func (c copilotClient) Chat(ctx context.Context, messages []Message, opts ...RequestOption) (string, error) {
if strings.TrimSpace(c.apiKey) == "" {
return nilStringErr("missing Copilot API key")
}
// Ensure we have a fresh session token
if err := c.ensureSession(ctx); err != nil {
return "", err
}
o := Options{Model: c.defaultModel}
for _, opt := range opts {
opt(&o)
}
if o.Model == "" {
o.Model = c.defaultModel
}
start := time.Now()
logMessages := make([]struct{ Role, Content string }, len(messages))
for i, m := range messages {
logMessages[i] = struct{ Role, Content string }{m.Role, m.Content}
}
c.chatLogger.LogStart(false, o.Model, o.Temperature, o.MaxTokens, o.Stop, logMessages)
req := buildCopilotChatRequest(o, messages, c.defaultTemperature)
body, err := json.Marshal(req)
if err != nil {
logging.Logf("llm/copilot ", "marshal error: %v", err)
return "", err
}
endpoint := c.baseURL + "/chat/completions"
logging.Logf("llm/copilot ", "POST %s", endpoint)
resp, err := c.postJSON(ctx, endpoint, body, c.headersChat())
if err != nil {
logging.Logf("llm/copilot ", "%shttp error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
return "", err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logging.Logf("llm/copilot", "failed to close response body: %v", err)
}
}()
if err := handleCopilotNon2xx(resp, start); err != nil {
return "", err
}
out, err := decodeCopilotChat(resp, start)
if err != nil {
return "", err
}
if len(out.Choices) == 0 {
logging.Logf("llm/copilot ", "%sno choices returned duration=%s%s", logging.AnsiRed, time.Since(start), logging.AnsiBase)
return "", errors.New("copilot: no choices returned")
}
content := out.Choices[0].Message.Content
logging.Logf("llm/copilot ", "success choice=0 finish=%s size=%d preview=%s%s%s duration=%s", out.Choices[0].FinishReason, len(content), logging.AnsiGreen, logging.PreviewForLog(content), logging.AnsiBase, time.Since(start))
return content, nil
}
// Provider metadata
func (c copilotClient) Name() string { return "copilot" }
func (c copilotClient) DefaultModel() string { return c.defaultModel }
// helpers
func buildCopilotChatRequest(o Options, messages []Message, defaultTemp *float64) copilotChatRequest {
req := copilotChatRequest{Model: o.Model}
req.Messages = make([]copilotMessage, len(messages))
for i, m := range messages {
req.Messages[i] = copilotMessage(m)
}
if o.Temperature != 0 {
req.Temperature = &o.Temperature
} else if defaultTemp != nil {
t := *defaultTemp
req.Temperature = &t
}
if o.MaxTokens > 0 {
req.MaxTokens = &o.MaxTokens
}
if len(o.Stop) > 0 {
req.Stop = o.Stop
}
return req
}
func (c copilotClient) postJSON(ctx context.Context, url string, body []byte, headers map[string]string) (*http.Response, error) {
req, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
if err != nil {
return nil, err
}
for k, v := range headers {
req.Header.Set(k, v)
}
return c.httpClient.Do(req)
}
func handleCopilotNon2xx(resp *http.Response, start time.Time) error {
if resp.StatusCode >= 200 && resp.StatusCode < 300 {
return nil
}
var apiErr copilotChatResponse
_ = json.NewDecoder(resp.Body).Decode(&apiErr)
if apiErr.Error != nil && strings.TrimSpace(apiErr.Error.Message) != "" {
logging.Logf("llm/copilot ", "%sapi error status=%d type=%s msg=%s duration=%s%s", logging.AnsiRed, resp.StatusCode, apiErr.Error.Type, apiErr.Error.Message, time.Since(start), logging.AnsiBase)
return fmt.Errorf("copilot error: %s (status %d)", apiErr.Error.Message, resp.StatusCode)
}
logging.Logf("llm/copilot ", "%shttp non-2xx status=%d duration=%s%s", logging.AnsiRed, resp.StatusCode, time.Since(start), logging.AnsiBase)
return fmt.Errorf("copilot http error: status %d", resp.StatusCode)
}
func decodeCopilotChat(resp *http.Response, start time.Time) (copilotChatResponse, error) {
var out copilotChatResponse
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
logging.Logf("llm/copilot ", "%sdecode error after %s: %v%s", logging.AnsiRed, time.Since(start), err, logging.AnsiBase)
return copilotChatResponse{}, err
}
return out, nil
}
// --- Copilot session token management ---
type ghCopilotTokenResp struct {
Token string `json:"token"`
}
func (c *copilotClient) ensureSession(ctx context.Context) error {
// If token valid for >60s, reuse
if c.sessionToken != "" && time.Now().Add(60*time.Second).Before(c.tokenExpiry) {
return nil
}
if strings.TrimSpace(c.apiKey) == "" {
return errors.New("missing Copilot API key")
}
req, err := http.NewRequestWithContext(ctx, http.MethodGet, "https://api.github.com/copilot_internal/v2/token", nil)
if err != nil {
return err
}
req.Header.Set("Authorization", "Bearer "+c.apiKey)
req.Header.Set("Accept", "application/json")
req.Header.Set("User-Agent", "hexai/"+appver.Version)
resp, err := c.httpClient.Do(req)
if err != nil {
return err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logging.Logf("llm/copilot", "failed to close response body: %v", err)
}
}()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return fmt.Errorf("copilot token http error: %d", resp.StatusCode)
}
var out ghCopilotTokenResp
if err := json.NewDecoder(resp.Body).Decode(&out); err != nil {
return err
}
if strings.TrimSpace(out.Token) == "" {
return errors.New("empty copilot session token")
}
// Parse JWT exp
exp := parseJWTExp(out.Token)
if exp.IsZero() {
exp = time.Now().Add(10 * time.Minute)
}
c.sessionToken = out.Token
c.tokenExpiry = exp
return nil
}
var jwtExpRe = regexp.MustCompile(`"exp"\s*:\s*([0-9]+)`) // fallback if we can't base64 decode
func parseJWTExp(token string) time.Time {
parts := strings.Split(token, ".")
if len(parts) < 2 {
return time.Time{}
}
b, err := base64.RawURLEncoding.DecodeString(parts[1])
if err != nil {
if m := jwtExpRe.FindStringSubmatch(token); len(m) == 2 {
if n, err2 := parseInt64(m[1]); err2 == nil {
return time.Unix(n, 0)
}
}
return time.Time{}
}
var payload struct {
Exp int64 `json:"exp"`
}
_ = json.Unmarshal(b, &payload)
if payload.Exp == 0 {
return time.Time{}
}
return time.Unix(payload.Exp, 0)
}
func parseInt64(s string) (int64, error) { var n int64; _, err := fmt.Sscan(s, &n); return n, err }
// --- Copilot headers ---
func (c *copilotClient) headersChat() map[string]string {
_ = c.ensureSession(context.Background())
h := map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Accept": "application/json",
"Authorization": "Bearer " + c.sessionToken,
"User-Agent": "GitHubCopilotChat/0.8.0",
"Editor-Plugin-Version": "copilot-chat/0.8.0",
"Editor-Version": "vscode/1.85.1",
"Openai-Intent": "conversation-panel",
"Openai-Organization": "github-copilot",
"VScode-MachineId": randHex(64),
"VScode-SessionId": randHex(8) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(12),
"X-Request-Id": randHex(8) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(12),
}
return h
}
func (c *copilotClient) headersGhost() map[string]string {
_ = c.ensureSession(context.Background())
h := map[string]string{
"Content-Type": "application/json; charset=utf-8",
"Accept": "*/*",
"Authorization": "Bearer " + c.sessionToken,
"User-Agent": "GithubCopilot/1.155.0",
"Editor-Plugin-Version": "copilot/1.155.0",
"Editor-Version": "vscode/1.85.1",
"Openai-Intent": "copilot-ghost",
"Openai-Organization": "github-copilot",
"VScode-MachineId": randHex(64),
"VScode-SessionId": randHex(8) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(12),
"X-Request-Id": randHex(8) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(4) + "-" + randHex(12),
}
return h
}
func randHex(n int) string {
const hex = "0123456789abcdef"
b := make([]byte, n)
for i := range b {
b[i] = hex[int(time.Now().UnixNano()+int64(i))%len(hex)]
}
return string(b)
}
// --- Codex-style code completion ---
// CodeCompletion implements CodeCompleter; returns up to n suggestions.
func (c copilotClient) CodeCompletion(ctx context.Context, prompt string, suffix string, n int, language string, temperature float64) ([]string, error) {
if strings.TrimSpace(c.apiKey) == "" {
return nil, errors.New("missing Copilot API key")
}
if err := c.ensureSession(ctx); err != nil {
return nil, err
}
if n <= 0 {
n = 1
}
maxTokens := 500
body := map[string]any{
"extra": map[string]any{
"language": language,
"next_indent": 0,
"prompt_tokens": 500,
"suffix_tokens": 400,
"trim_by_indentation": true,
},
"max_tokens": maxTokens,
"n": n,
"nwo": "hexai",
"prompt": prompt,
"stop": []string{"\n\n"},
"stream": true,
"suffix": suffix,
"temperature": temperature,
"top_p": 1,
}
buf, _ := json.Marshal(body)
url := "https://copilot-proxy.githubusercontent.com/v1/engines/copilot-codex/completions"
resp, err := c.postJSON(ctx, url, buf, c.headersGhost())
if err != nil {
return nil, err
}
defer func() {
if err := resp.Body.Close(); err != nil {
logging.Logf("llm/copilot", "failed to close response body: %v", err)
}
}()
if resp.StatusCode < 200 || resp.StatusCode >= 300 {
return nil, fmt.Errorf("copilot codex http error: %d", resp.StatusCode)
}
// Read all and parse lines that start with "data: " accumulating by index
raw, _ := io.ReadAll(resp.Body)
byIndex := make(map[int]string)
lines := strings.Split(string(raw), "\n")
for _, ln := range lines {
if !strings.HasPrefix(ln, "data: ") {
continue
}
var evt struct {
Choices []struct {
Index int `json:"index"`
Text string `json:"text"`
} `json:"choices"`
}
if err := json.Unmarshal([]byte(strings.TrimPrefix(ln, "data: ")), &evt); err != nil {
continue
}
for _, ch := range evt.Choices {
byIndex[ch.Index] += ch.Text
}
}
out := make([]string, 0, len(byIndex))
for i := 0; i < n; i++ {
if s, ok := byIndex[i]; ok && strings.TrimSpace(s) != "" {
out = append(out, s)
}
}
return out, nil
}
// newLineDataReader wraps a streaming body and exposes a JSON decoder that
// decodes successive objects from lines prefixed by "data: ".
// (no streaming decoder needed; we parse whole body lines)
|