| 68 | } |
| 69 | |
| 70 | func (cs *ChatStore) PostMessage(chatId string, aiOpts *uctypes.AIOptsType, message uctypes.GenAIMessage) error { |
| 71 | cs.lock.Lock() |
| 72 | defer cs.lock.Unlock() |
| 73 | |
| 74 | chat := cs.chats[chatId] |
| 75 | if chat == nil { |
| 76 | // Create new chat |
| 77 | chat = &uctypes.AIChat{ |
| 78 | ChatId: chatId, |
| 79 | APIType: aiOpts.APIType, |
| 80 | Model: aiOpts.Model, |
| 81 | APIVersion: aiOpts.APIVersion, |
| 82 | NativeMessages: make([]uctypes.GenAIMessage, 0), |
| 83 | } |
| 84 | cs.chats[chatId] = chat |
| 85 | } else { |
| 86 | // Verify that the AI options match |
| 87 | if chat.APIType != aiOpts.APIType { |
| 88 | return fmt.Errorf("API type mismatch: expected %s, got %s (must start a new chat)", chat.APIType, aiOpts.APIType) |
| 89 | } |
| 90 | if !uctypes.AreModelsCompatible(chat.APIType, chat.Model, aiOpts.Model) { |
| 91 | return fmt.Errorf("model mismatch: expected %s, got %s (must start a new chat)", chat.Model, aiOpts.Model) |
| 92 | } |
| 93 | if chat.APIVersion != aiOpts.APIVersion { |
| 94 | return fmt.Errorf("API version mismatch: expected %s, got %s (must start a new chat)", chat.APIVersion, aiOpts.APIVersion) |
| 95 | } |
| 96 | } |
| 97 | |
| 98 | // Check for existing message with same ID (idempotency) |
| 99 | messageId := message.GetMessageId() |
| 100 | for i, existingMessage := range chat.NativeMessages { |
| 101 | if existingMessage.GetMessageId() == messageId { |
| 102 | // Replace existing message with same ID |
| 103 | chat.NativeMessages[i] = message |
| 104 | return nil |
| 105 | } |
| 106 | } |
| 107 | |
| 108 | // Append the new message if no duplicate found |
| 109 | chat.NativeMessages = append(chat.NativeMessages, message) |
| 110 | |
| 111 | return nil |
| 112 | } |
| 113 | |
| 114 | func (cs *ChatStore) RemoveMessage(chatId string, messageId string) bool { |
| 115 | cs.lock.Lock() |