This repository was archived by the owner on Aug 3, 2026. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconversation.go
More file actions
657 lines (529 loc) · 14.7 KB
/
Copy pathconversation.go
File metadata and controls
657 lines (529 loc) · 14.7 KB
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
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
package sdk
import (
"context"
"encoding/json"
"errors"
"fmt"
"maps"
"sync"
"time"
llm "github.com/xraph/ai-sdk/llm"
)
// Conversation represents a chat conversation with history management.
type Conversation struct {
ID string `json:"id"`
Title string `json:"title,omitempty"`
Messages []ConversationMessage `json:"messages"`
Metadata map[string]any `json:"metadata,omitempty"`
Branches map[string]*Conversation `json:"-"`
ParentID string `json:"parentId,omitempty"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
// Configuration
maxTokens int
pruner HistoryPruner
llmManager *llm.LLMManager
model string
provider string
systemPrompt string
temperature float64
branchingEnabled bool
mu sync.RWMutex
}
// ConversationMessage represents a message in the conversation.
type ConversationMessage struct {
ID string `json:"id"`
Role string `json:"role"`
Content string `json:"content"`
Name string `json:"name,omitempty"`
ToolCalls []llm.ToolCall `json:"toolCalls,omitempty"`
ToolCallID string `json:"toolCallId,omitempty"`
Tokens int `json:"tokens,omitempty"`
Timestamp time.Time `json:"timestamp"`
Metadata map[string]any `json:"metadata,omitempty"`
}
// ConversationConfig configures a conversation.
type ConversationConfig struct {
ID string
Title string
MaxTokens int
Pruner HistoryPruner
LLMManager *llm.LLMManager
Model string
Provider string
SystemPrompt string
Temperature float64
BranchingEnabled bool
Metadata map[string]any
}
// NewConversation creates a new conversation.
func NewConversation(config ConversationConfig) *Conversation {
if config.ID == "" {
config.ID = fmt.Sprintf("conv_%d", time.Now().UnixNano())
}
if config.MaxTokens == 0 {
config.MaxTokens = 4000
}
if config.Pruner == nil {
config.Pruner = &OldestFirstPruner{}
}
conv := &Conversation{
ID: config.ID,
Title: config.Title,
Messages: make([]ConversationMessage, 0),
Metadata: config.Metadata,
Branches: make(map[string]*Conversation),
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
maxTokens: config.MaxTokens,
pruner: config.Pruner,
llmManager: config.LLMManager,
model: config.Model,
provider: config.Provider,
systemPrompt: config.SystemPrompt,
temperature: config.Temperature,
branchingEnabled: config.BranchingEnabled,
}
if conv.Metadata == nil {
conv.Metadata = make(map[string]any)
}
return conv
}
// AddMessage adds a message to the conversation.
func (c *Conversation) AddMessage(msg ConversationMessage) {
c.mu.Lock()
defer c.mu.Unlock()
if msg.ID == "" {
msg.ID = fmt.Sprintf("msg_%d", time.Now().UnixNano())
}
if msg.Timestamp.IsZero() {
msg.Timestamp = time.Now()
}
c.Messages = append(c.Messages, msg)
c.UpdatedAt = time.Now()
}
// AddUserMessage adds a user message.
func (c *Conversation) AddUserMessage(content string) {
c.AddMessage(ConversationMessage{
Role: "user",
Content: content,
})
}
// AddAssistantMessage adds an assistant message.
func (c *Conversation) AddAssistantMessage(content string) {
c.AddMessage(ConversationMessage{
Role: "assistant",
Content: content,
})
}
// AddSystemMessage adds a system message.
func (c *Conversation) AddSystemMessage(content string) {
c.AddMessage(ConversationMessage{
Role: "system",
Content: content,
})
}
// GetMessages returns all messages.
func (c *Conversation) GetMessages() []ConversationMessage {
c.mu.RLock()
defer c.mu.RUnlock()
msgs := make([]ConversationMessage, len(c.Messages))
copy(msgs, c.Messages)
return msgs
}
// GetPrunedMessages returns messages pruned to fit within token limits.
func (c *Conversation) GetPrunedMessages() []ConversationMessage {
c.mu.RLock()
defer c.mu.RUnlock()
if c.pruner == nil {
return c.Messages
}
return c.pruner.Prune(c.Messages, c.maxTokens)
}
// TotalTokens returns the total tokens in the conversation.
func (c *Conversation) TotalTokens() int {
c.mu.RLock()
defer c.mu.RUnlock()
total := 0
for _, msg := range c.Messages {
if msg.Tokens > 0 {
total += msg.Tokens
} else {
// Estimate tokens (rough approximation)
total += len(msg.Content) / 4
}
}
return total
}
// Send sends a message and gets a response.
func (c *Conversation) Send(ctx context.Context, content string) (*ConversationMessage, error) {
// Add user message
c.AddUserMessage(content)
// Build request
prunedMessages := c.GetPrunedMessages()
llmMessages := c.toLLMMessages(prunedMessages)
request := llm.ChatRequest{
Provider: c.provider,
Model: c.model,
Messages: llmMessages,
}
if c.temperature != 0 {
request.Temperature = &c.temperature
}
// Send request
response, err := c.llmManager.Chat(ctx, request)
if err != nil {
return nil, err
}
if len(response.Choices) == 0 {
return nil, errors.New("no response from LLM")
}
// Extract response
choice := response.Choices[0]
assistantMsg := ConversationMessage{
Role: "assistant",
Content: choice.Message.Content,
}
if response.Usage != nil {
assistantMsg.Tokens = int(response.Usage.OutputTokens)
}
// Add assistant message
c.AddMessage(assistantMsg)
return &assistantMsg, nil
}
// SendStream sends a message and streams the response.
func (c *Conversation) SendStream(ctx context.Context, content string, onDelta func(string)) (*ConversationMessage, error) {
// Add user message
c.AddUserMessage(content)
// Build request
prunedMessages := c.GetPrunedMessages()
llmMessages := c.toLLMMessages(prunedMessages)
request := llm.ChatRequest{
Provider: c.provider,
Model: c.model,
Messages: llmMessages,
Stream: true,
}
if c.temperature != 0 {
request.Temperature = &c.temperature
}
// Collect response
var (
fullContent string
tokens int
)
err := c.llmManager.ChatStream(ctx, request, func(event llm.ChatStreamEvent) error {
if len(event.Choices) > 0 {
delta := ""
if event.Choices[0].Delta != nil {
delta = event.Choices[0].Delta.Content
}
if delta != "" {
fullContent += delta
if onDelta != nil {
onDelta(delta)
}
}
}
if event.Usage != nil {
tokens = int(event.Usage.OutputTokens)
}
return nil
})
if err != nil {
return nil, err
}
// Create and add assistant message
assistantMsg := ConversationMessage{
Role: "assistant",
Content: fullContent,
Tokens: tokens,
}
c.AddMessage(assistantMsg)
return &assistantMsg, nil
}
// toLLMMessages converts conversation messages to LLM messages.
func (c *Conversation) toLLMMessages(msgs []ConversationMessage) []llm.ChatMessage {
llmMsgs := make([]llm.ChatMessage, 0, len(msgs)+1)
// Add system prompt if set
if c.systemPrompt != "" {
llmMsgs = append(llmMsgs, llm.ChatMessage{
Role: "system",
Content: c.systemPrompt,
})
}
for _, msg := range msgs {
llmMsg := llm.ChatMessage{
Role: msg.Role,
Content: msg.Content,
Name: msg.Name,
ToolCalls: msg.ToolCalls,
ToolCallID: msg.ToolCallID,
}
llmMsgs = append(llmMsgs, llmMsg)
}
return llmMsgs
}
// Branch creates a branch of the conversation at the current point.
func (c *Conversation) Branch(name string) (*Conversation, error) {
if !c.branchingEnabled {
return nil, errors.New("branching is not enabled")
}
c.mu.Lock()
defer c.mu.Unlock()
if _, exists := c.Branches[name]; exists {
return nil, fmt.Errorf("branch %s already exists", name)
}
// Create branch with same configuration
branch := &Conversation{
ID: fmt.Sprintf("%s_%s", c.ID, name),
Title: fmt.Sprintf("%s (branch: %s)", c.Title, name),
Messages: make([]ConversationMessage, len(c.Messages)),
Metadata: make(map[string]any),
Branches: make(map[string]*Conversation),
ParentID: c.ID,
CreatedAt: time.Now(),
UpdatedAt: time.Now(),
maxTokens: c.maxTokens,
pruner: c.pruner,
llmManager: c.llmManager,
model: c.model,
provider: c.provider,
systemPrompt: c.systemPrompt,
temperature: c.temperature,
branchingEnabled: c.branchingEnabled,
}
// Copy messages
copy(branch.Messages, c.Messages)
// Copy metadata
maps.Copy(branch.Metadata, c.Metadata)
c.Branches[name] = branch
return branch, nil
}
// GetBranch returns a branch by name.
func (c *Conversation) GetBranch(name string) (*Conversation, bool) {
c.mu.RLock()
defer c.mu.RUnlock()
branch, ok := c.Branches[name]
return branch, ok
}
// ListBranches returns all branch names.
func (c *Conversation) ListBranches() []string {
c.mu.RLock()
defer c.mu.RUnlock()
names := make([]string, 0, len(c.Branches))
for name := range c.Branches {
names = append(names, name)
}
return names
}
// Clear clears all messages.
func (c *Conversation) Clear() {
c.mu.Lock()
defer c.mu.Unlock()
c.Messages = make([]ConversationMessage, 0)
c.UpdatedAt = time.Now()
}
// Rollback removes the last N messages.
func (c *Conversation) Rollback(n int) {
c.mu.Lock()
defer c.mu.Unlock()
if n >= len(c.Messages) {
c.Messages = make([]ConversationMessage, 0)
} else {
c.Messages = c.Messages[:len(c.Messages)-n]
}
c.UpdatedAt = time.Now()
}
// ToJSON serializes the conversation to JSON.
func (c *Conversation) ToJSON() ([]byte, error) {
c.mu.RLock()
defer c.mu.RUnlock()
return json.Marshal(c)
}
// ConversationFromJSON deserializes a conversation from JSON.
func ConversationFromJSON(data []byte) (*Conversation, error) {
var conv Conversation
if err := json.Unmarshal(data, &conv); err != nil {
return nil, err
}
if conv.Branches == nil {
conv.Branches = make(map[string]*Conversation)
}
if conv.Metadata == nil {
conv.Metadata = make(map[string]any)
}
return &conv, nil
}
// ConversationStore interface for persisting conversations.
type ConversationStore interface {
Save(ctx context.Context, conv *Conversation) error
Load(ctx context.Context, id string) (*Conversation, error)
Delete(ctx context.Context, id string) error
List(ctx context.Context) ([]string, error)
}
// InMemoryConversationStore stores conversations in memory.
type InMemoryConversationStore struct {
conversations map[string]*Conversation
mu sync.RWMutex
}
// NewInMemoryConversationStore creates a new in-memory store.
func NewInMemoryConversationStore() *InMemoryConversationStore {
return &InMemoryConversationStore{
conversations: make(map[string]*Conversation),
}
}
// Save implements ConversationStore.
func (s *InMemoryConversationStore) Save(ctx context.Context, conv *Conversation) error {
s.mu.Lock()
defer s.mu.Unlock()
s.conversations[conv.ID] = conv
return nil
}
// Load implements ConversationStore.
func (s *InMemoryConversationStore) Load(ctx context.Context, id string) (*Conversation, error) {
s.mu.RLock()
defer s.mu.RUnlock()
conv, ok := s.conversations[id]
if !ok {
return nil, fmt.Errorf("conversation not found: %s", id)
}
return conv, nil
}
// Delete implements ConversationStore.
func (s *InMemoryConversationStore) Delete(ctx context.Context, id string) error {
s.mu.Lock()
defer s.mu.Unlock()
delete(s.conversations, id)
return nil
}
// List implements ConversationStore.
func (s *InMemoryConversationStore) List(ctx context.Context) ([]string, error) {
s.mu.RLock()
defer s.mu.RUnlock()
ids := make([]string, 0, len(s.conversations))
for id := range s.conversations {
ids = append(ids, id)
}
return ids, nil
}
// ConversationManager manages multiple conversations.
type ConversationManager struct {
store ConversationStore
activeConv map[string]*Conversation
defaultConfig ConversationConfig
llmManager *llm.LLMManager
mu sync.RWMutex
}
// NewConversationManager creates a new conversation manager.
func NewConversationManager(store ConversationStore, llmManager *llm.LLMManager) *ConversationManager {
return &ConversationManager{
store: store,
activeConv: make(map[string]*Conversation),
llmManager: llmManager,
}
}
// SetDefaultConfig sets default configuration for new conversations.
func (m *ConversationManager) SetDefaultConfig(config ConversationConfig) {
m.defaultConfig = config
}
// Create creates a new conversation.
func (m *ConversationManager) Create(config ConversationConfig) *Conversation {
// Merge with defaults
if config.MaxTokens == 0 {
config.MaxTokens = m.defaultConfig.MaxTokens
}
if config.Pruner == nil {
config.Pruner = m.defaultConfig.Pruner
}
if config.Model == "" {
config.Model = m.defaultConfig.Model
}
if config.Provider == "" {
config.Provider = m.defaultConfig.Provider
}
if config.SystemPrompt == "" {
config.SystemPrompt = m.defaultConfig.SystemPrompt
}
conv := NewConversation(config)
m.mu.Lock()
m.activeConv[conv.ID] = conv
m.mu.Unlock()
return conv
}
// Get returns a conversation by ID.
func (m *ConversationManager) Get(ctx context.Context, id string) (*Conversation, error) {
m.mu.RLock()
conv, ok := m.activeConv[id]
m.mu.RUnlock()
if ok {
return conv, nil
}
// Try loading from store
if m.store != nil {
conv, err := m.store.Load(ctx, id)
if err != nil {
return nil, err
}
// Restore runtime configuration
conv.llmManager = m.llmManager
if conv.pruner == nil {
conv.pruner = m.defaultConfig.Pruner
}
m.mu.Lock()
m.activeConv[id] = conv
m.mu.Unlock()
return conv, nil
}
return nil, fmt.Errorf("conversation not found: %s", id)
}
// Save saves a conversation to the store.
func (m *ConversationManager) Save(ctx context.Context, id string) error {
m.mu.RLock()
conv, ok := m.activeConv[id]
m.mu.RUnlock()
if !ok {
return fmt.Errorf("conversation not found: %s", id)
}
if m.store != nil {
return m.store.Save(ctx, conv)
}
return nil
}
// Delete removes a conversation.
func (m *ConversationManager) Delete(ctx context.Context, id string) error {
m.mu.Lock()
delete(m.activeConv, id)
m.mu.Unlock()
if m.store != nil {
return m.store.Delete(ctx, id)
}
return nil
}
// List returns all conversation IDs.
func (m *ConversationManager) List(ctx context.Context) ([]string, error) {
m.mu.RLock()
ids := make([]string, 0, len(m.activeConv))
for id := range m.activeConv {
ids = append(ids, id)
}
m.mu.RUnlock()
// Add stored conversations
if m.store != nil {
storedIDs, err := m.store.List(ctx)
if err != nil {
return ids, err
}
// Deduplicate
idSet := make(map[string]bool)
for _, id := range ids {
idSet[id] = true
}
for _, id := range storedIDs {
if !idSet[id] {
ids = append(ids, id)
}
}
}
return ids, nil
}