Skip to content

Commit

Permalink
update go structs for chat to hive feature
Browse files Browse the repository at this point in the history
  • Loading branch information
MahtabBukhari committed Dec 10, 2024
1 parent e68cbf3 commit cc0ec36
Show file tree
Hide file tree
Showing 2 changed files with 124 additions and 0 deletions.
70 changes: 70 additions & 0 deletions db/chat.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
package db

import (
"errors"
"fmt"
"time"

"gorm.io/gorm"
)

func (db database) AddChat(chat *Chat) (Chat, error) {
if chat.ID == "" {
return Chat{}, errors.New("chat ID is required")
}

now := time.Now()
chat.CreatedAt = now
chat.UpdatedAt = now

if err := db.db.Create(&chat).Error; err != nil {
return Chat{}, fmt.Errorf("failed to create chat: %w", err)
}

return *chat, nil
}

func (db database) GetChatByChatID(chatID string) (Chat, error) {
var chat Chat
result := db.db.Where("id = ?", chatID).First(&chat)

if result.Error != nil {
if errors.Is(result.Error, gorm.ErrRecordNotFound) {
return Chat{}, fmt.Errorf("chat not found")
}
return Chat{}, fmt.Errorf("failed to fetch chat: %w", result.Error)
}

return chat, nil
}

func (db database) AddChatMessage(chatMessage *ChatMessage) (ChatMessage, error) {
if chatMessage.ID == "" {
return ChatMessage{}, errors.New("message ID is required")
}

now := time.Now()
chatMessage.Timestamp = now

if err := db.db.Create(&chatMessage).Error; err != nil {
return ChatMessage{}, fmt.Errorf("failed to create chat message: %w", err)
}

return *chatMessage, nil
}

func (db database) GetChatMessagesForChatID(chatID string) ([]ChatMessage, error) {
var chatMessages []ChatMessage

result := db.db.Where("chat_id = ?", chatID).Order("timestamp ASC").Find(&chatMessages)

if result.Error != nil {
return nil, fmt.Errorf("failed to fetch chat messages: %w", result.Error)
}

if result.RowsAffected == 0 {
return []ChatMessage{}, nil
}

return chatMessages, nil
}
54 changes: 54 additions & 0 deletions db/structs.go
Original file line number Diff line number Diff line change
Expand Up @@ -1008,6 +1008,60 @@ type TicketMessage struct {
TicketDetails Tickets `json:"ticketDetails"`
}

type ContextTagType string

const (
ProductBriefContext ContextTagType = "productBrief"
FeatureBriefContext ContextTagType = "featureBrief"
SchematicContext ContextTagType = "schematic"
)

type ContextTag struct {
Type ContextTagType `json:"type"`
ID string `json:"id"`
}

type ChatRole string

const (
UserRole ChatRole = "user"
AssistantRole ChatRole = "assistant"
)

type ChatMessageStatus string

const (
SendingStatus ChatMessageStatus = "sending"
SentStatus ChatMessageStatus = "sent"
ErrorStatus ChatMessageStatus = "error"
)

type ChatSource string

const (
UserSource ChatSource = "user"
AgentSource ChatSource = "agent"
)

type ChatMessage struct {
ID string `json:"id" gorm:"primaryKey"`
ChatID string `json:"chatId" gorm:"index"`
Message string `json:"message"`
Role ChatRole `json:"role"`
Timestamp time.Time `json:"timestamp"`
ContextTags []ContextTag `json:"contextTags" gorm:"type:jsonb"`
Status ChatMessageStatus `json:"status"`
Source ChatSource `json:"source"`
}

type Chat struct {
ID string `json:"id" gorm:"primaryKey"`
WorkspaceID string `json:"workspaceId" gorm:"index"`
Title string `json:"title"`
CreatedAt time.Time `json:"createdAt"`
UpdatedAt time.Time `json:"updatedAt"`
}

func (Person) TableName() string {
return "people"
}
Expand Down

0 comments on commit cc0ec36

Please sign in to comment.