-
Notifications
You must be signed in to change notification settings - Fork 0
/
maingo.go
577 lines (487 loc) · 12.3 KB
/
maingo.go
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
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
package main
import (
"bufio"
"container/ring"
"context"
"encoding/json"
"fmt"
"log"
"net"
"os"
"os/signal"
"path/filepath"
"strings"
"sync"
"sync/atomic"
"time"
"github.com/gorilla/websocket"
)
// Configuration types
type ClientConfig struct {
ReadTimeout time.Duration
WriteTimeout time.Duration
PingInterval time.Duration
MaxRetries int
BufferSize int
ShutdownTimeout time.Duration
}
func DefaultConfig() ClientConfig {
return ClientConfig{
ReadTimeout: 30 * time.Second,
WriteTimeout: 10 * time.Second,
PingInterval: 30 * time.Second,
MaxRetries: 3,
BufferSize: 100,
ShutdownTimeout: 5 * time.Second,
}
}
// Message types for WebSocket communication
type SessionUpdate struct {
Type string `json:"type"`
Session Session `json:"session"`
}
type Session struct {
Modalities []string `json:"modalities"`
Instructions string `json:"instructions"`
Temperature float64 `json:"temperature"`
MaxResponseOutputTokens int `json:"max_response_output_tokens"`
}
type ConversationItem struct {
Type string `json:"type"`
Item struct {
Type string `json:"type"`
Role string `json:"role"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
} `json:"item"`
}
type ResponseCreate struct {
Type string `json:"type"`
}
type ResponseMessage struct {
Type string `json:"type"`
Response Response `json:"response"`
}
type Response struct {
Output []OutputItem `json:"output"`
Status string `json:"status"`
}
type OutputItem struct {
Content []ContentItem `json:"content"`
Role string `json:"role"`
Status string `json:"status"`
}
type ContentItem struct {
Type string `json:"type"`
Text string `json:"text"`
}
type ChatMessage struct {
Role string
Content string
}
// Metrics tracking
type Metrics struct {
messagesSent int64
messagesReceived int64
errors int64
latencies []time.Duration
mu sync.Mutex
}
func (m *Metrics) recordLatency(start time.Time) {
m.mu.Lock()
defer m.mu.Unlock()
m.latencies = append(m.latencies, time.Since(start))
}
func (m *Metrics) recordError() {
atomic.AddInt64(&m.errors, 1)
}
// Logger implementation
type Logger struct {
file *os.File
mu sync.Mutex
encoder *json.Encoder
}
type LogEntry struct {
Timestamp string `json:"timestamp"`
Direction string `json:"direction"`
Type string `json:"type"`
RawJSON interface{} `json:"raw_json"`
}
// ChatClient structure
type ChatClient struct {
conn *websocket.Conn
messageChannel chan string
displayChannel chan ChatMessage
done chan struct{}
shutdownOnce sync.Once
wg sync.WaitGroup
logger *Logger
config ClientConfig
metrics *Metrics
messageBuffer *ring.Ring
bufferMutex sync.Mutex
}
func NewLogger() (*Logger, error) {
exePath, err := os.Executable()
if err != nil {
return nil, fmt.Errorf("get executable path: %w", err)
}
exeDir := filepath.Dir(exePath)
logDir := filepath.Join(exeDir, "logs")
if err := os.MkdirAll(logDir, 0755); err != nil {
return nil, fmt.Errorf("create log directory: %w", err)
}
timestamp := time.Now().Format("20060102_150405")
filename := filepath.Join(logDir, fmt.Sprintf("Chat:%s.log", timestamp))
file, err := os.Create(filename)
if err != nil {
return nil, fmt.Errorf("create log file: %w", err)
}
log.Printf("Logging to: %s", filename)
return &Logger{
file: file,
encoder: json.NewEncoder(file),
}, nil
}
func (l *Logger) Log(direction, msgType string, content interface{}) {
l.mu.Lock()
defer l.mu.Unlock()
entry := LogEntry{
Timestamp: time.Now().Format(time.RFC3339Nano),
Direction: direction,
Type: msgType,
RawJSON: content,
}
if err := l.encoder.Encode(entry); err != nil {
log.Printf("Error writing to log: %v", err)
}
l.file.Sync()
}
func (l *Logger) Close() error {
return l.file.Close()
}
func NewChatClient(conn *websocket.Conn, config ClientConfig) (*ChatClient, error) {
logger, err := NewLogger()
if err != nil {
return nil, fmt.Errorf("create logger: %w", err)
}
// Setup ping handler
conn.SetPingHandler(func(appData string) error {
return conn.WriteControl(websocket.PongMessage, []byte(appData), time.Now().Add(time.Second))
})
client := &ChatClient{
conn: conn,
messageChannel: make(chan string, 1),
displayChannel: make(chan ChatMessage),
done: make(chan struct{}),
logger: logger,
config: config,
metrics: &Metrics{},
messageBuffer: ring.New(config.BufferSize),
}
// Start ping routine
go client.pingRoutine()
return client, nil
}
func (c *ChatClient) pingRoutine() {
ticker := time.NewTicker(c.config.PingInterval)
defer ticker.Stop()
for {
select {
case <-c.done:
return
case <-ticker.C:
if err := c.conn.WriteControl(websocket.PingMessage, []byte{}, time.Now().Add(c.config.WriteTimeout)); err != nil {
log.Printf("Ping error: %v", err)
c.metrics.recordError()
}
}
}
}
func (c *ChatClient) bufferMessage(msg string) {
c.bufferMutex.Lock()
defer c.bufferMutex.Unlock()
c.messageBuffer = c.messageBuffer.Next()
c.messageBuffer.Value = msg
}
func isExitCommand(text string) bool {
exitCommands := []string{
".exit", ".quit",
"exit", "quit",
":exit", ":quit",
"/exit", "/quit",
}
lowercaseText := strings.ToLower(strings.TrimSpace(text))
for _, cmd := range exitCommands {
if lowercaseText == cmd {
return true
}
}
return false
}
func (c *ChatClient) shutdown() {
c.shutdownOnce.Do(func() {
log.Println("Starting graceful shutdown...")
// Signal shutdown
close(c.done)
// Setup shutdown deadline
shutdownCtx, cancel := context.WithTimeout(context.Background(), c.config.ShutdownTimeout)
defer cancel()
// Channel to signal completion
complete := make(chan struct{})
go func() {
// Close websocket
c.conn.WriteControl(
websocket.CloseMessage,
websocket.FormatCloseMessage(websocket.CloseNormalClosure, ""),
time.Now().Add(time.Second),
)
c.conn.Close()
// Close logger
if err := c.logger.Close(); err != nil {
log.Printf("Error closing logger: %v", err)
}
// Wait for goroutines
c.wg.Wait()
// Close channels
close(c.messageChannel)
close(c.displayChannel)
close(complete)
}()
// Wait for completion or timeout
select {
case <-complete:
log.Println("Shutdown completed successfully")
case <-shutdownCtx.Done():
log.Println("Shutdown timed out")
}
})
}
func (c *ChatClient) displayRoutine() {
defer c.wg.Done()
for {
select {
case <-c.done:
return
case msg, ok := <-c.displayChannel:
if !ok {
return
}
if msg.Role == "assistant" {
fmt.Printf("Assistant: %s\n\n", msg.Content)
fmt.Print("You: ")
}
}
}
}
func (c *ChatClient) inputRoutine() {
defer c.wg.Done()
scanner := bufio.NewScanner(os.Stdin)
for scanner.Scan() {
select {
case <-c.done:
return
default:
text := strings.TrimSpace(scanner.Text())
if text == "" {
continue
}
if isExitCommand(text) {
fmt.Println("\nGoodbye! Thanks for chatting.")
c.shutdown()
return
}
c.bufferMessage(text) // Buffer the message
select {
case c.messageChannel <- text:
case <-c.done:
return
}
}
}
if err := scanner.Err(); err != nil {
log.Printf("Error reading input: %v", err)
c.shutdown()
}
}
func (c *ChatClient) sendWithRetry(msg interface{}) error {
var lastErr error
for i := 0; i < c.config.MaxRetries; i++ {
c.conn.SetWriteDeadline(time.Now().Add(c.config.WriteTimeout))
if err := c.conn.WriteJSON(msg); err != nil {
lastErr = err
c.metrics.recordError()
time.Sleep(time.Second * time.Duration(i+1)) // Exponential backoff
continue
}
atomic.AddInt64(&c.metrics.messagesSent, 1)
return nil
}
return fmt.Errorf("failed after %d retries: %v", c.config.MaxRetries, lastErr)
}
func (c *ChatClient) sendRoutine() {
defer c.wg.Done()
for {
select {
case <-c.done:
return
case text, ok := <-c.messageChannel:
if !ok {
return
}
start := time.Now()
if err := c.sendUserMessage(text); err != nil {
log.Printf("Error sending message: %v", err)
c.metrics.recordError()
c.shutdown()
return
}
c.metrics.recordLatency(start)
}
}
}
func (c *ChatClient) receiveRoutine() {
defer c.wg.Done()
for {
select {
case <-c.done:
return
default:
c.conn.SetReadDeadline(time.Now().Add(c.config.ReadTimeout))
_, message, err := c.conn.ReadMessage()
if err != nil {
if websocket.IsCloseError(err, websocket.CloseNormalClosure, websocket.CloseGoingAway) {
return
}
if netErr, ok := err.(net.Error); ok && netErr.Timeout() {
continue
}
log.Printf("Read error: %v", err)
c.metrics.recordError()
c.shutdown()
return
}
atomic.AddInt64(&c.metrics.messagesReceived, 1)
var rawJSON interface{}
if err := json.Unmarshal(message, &rawJSON); err == nil {
c.logger.Log("received", "raw", rawJSON)
}
var resp ResponseMessage
if err := json.Unmarshal(message, &resp); err != nil {
continue
}
if resp.Type == "response.done" && resp.Response.Status == "completed" {
for _, output := range resp.Response.Output {
for _, content := range output.Content {
if content.Type == "text" {
select {
case c.displayChannel <- ChatMessage{Role: "assistant", Content: content.Text}:
case <-c.done:
return
}
}
}
}
}
}
}
}
func (c *ChatClient) sendUserMessage(text string) error {
conversationItem := ConversationItem{
Type: "conversation.item.create",
Item: struct {
Type string `json:"type"`
Role string `json:"role"`
Content []struct {
Type string `json:"type"`
Text string `json:"text"`
} `json:"content"`
}{
Type: "message",
Role: "user",
Content: []struct {
Type string `json:"type"`
Text string `json:"text"`
}{
{
Type: "input_text",
Text: text,
},
},
},
}
c.logger.Log("sent", "conversation.item.create", conversationItem)
if err := c.sendWithRetry(conversationItem); err != nil {
return fmt.Errorf("write conversation item: %w", err)
}
time.Sleep(time.Second)
responseCreate := ResponseCreate{
Type: "response.create",
}
c.logger.Log("sent", "response.create", responseCreate)
if err := c.sendWithRetry(responseCreate); err != nil {
return fmt.Errorf("write response create: %w", err)
}
return nil
}
func (c *ChatClient) Start() {
defer c.shutdown()
sessionUpdate := SessionUpdate{
Type: "session.update",
Session: Session{
Modalities: []string{"text"},
Temperature: 0.8,
MaxResponseOutputTokens: 4096,
Instructions: "System settings:\nInstructions:\n- You are an artificial intelligence agent\n- Be kind, helpful, and curteous\n- It is okay to ask the user questions\n- Be open to exploration and conversation\n- Remember: this is just for fun and testing!\n\nPersonality:\n- Be upbeat and genuine\n",
},
}
c.logger.Log("sent", "session.update", sessionUpdate)
if err := c.sendWithRetry(sessionUpdate); err != nil {
log.Fatal("write session update:", err)
}
c.wg.Add(4)
fmt.Print("You: ")
go c.displayRoutine()
go c.inputRoutine()
go c.sendRoutine()
go c.receiveRoutine()
c.wg.Wait()
}
func main() {
apiKey := os.Getenv("OPENAI_API_KEY")
if apiKey == "" {
log.Fatal("OPENAI_API_KEY environment variable is not set")
}
// Get configuration
config := DefaultConfig()
header := make(map[string][]string)
header["Authorization"] = []string{"Bearer " + apiKey}
header["OpenAI-Beta"] = []string{"realtime=v1"}
// Create dialer with timeout
dialer := websocket.Dialer{
HandshakeTimeout: 10 * time.Second,
}
// Set up context with timeout for connection
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
url := "wss://api.openai.com/v1/realtime?model=gpt-4o-realtime-preview-2024-10-01"
conn, _, err := dialer.DialContext(ctx, url, header)
if err != nil {
log.Fatal("dial:", err)
}
client, err := NewChatClient(conn, config)
if err != nil {
log.Fatal("create chat client:", err)
}
// Handle interrupt signal
sigChan := make(chan os.Signal, 1)
signal.Notify(sigChan, os.Interrupt)
go func() {
<-sigChan
fmt.Println("\nReceived interrupt signal. Shutting down...")
client.shutdown()
}()
client.Start()
}