ad38beeb9e
CI / docker (push) Successful in 11m2s
引入用户端对话/生图与管理端 Provider 配置,统一 invoke 编排、Gateway、请求日志与模型管理;前端按设计规范拆分布局,Go module 更名为 github.com/rose_cat707/Atlas;生图长任务进度改为 SSE 推送;重写 README;移除 Dockerfile syntax 指令以修复 CI 构建超时。 Co-authored-by: Cursor <cursoragent@cursor.com>
118 lines
3.8 KiB
Go
118 lines
3.8 KiB
Go
package store
|
|
|
|
import "context"
|
|
|
|
type Conversation struct {
|
|
ID int64 `json:"id"`
|
|
SessionID string `json:"session_id"`
|
|
Title string `json:"title"`
|
|
Model string `json:"model"`
|
|
ParamsJSON string `json:"params_json"`
|
|
CreatedAt string `json:"created_at"`
|
|
UpdatedAt string `json:"updated_at"`
|
|
}
|
|
|
|
type ChatMessageRow struct {
|
|
ID int64 `json:"id"`
|
|
ConversationID int64 `json:"conversation_id"`
|
|
Role string `json:"role"`
|
|
Content string `json:"content"`
|
|
CreatedAt string `json:"created_at"`
|
|
}
|
|
|
|
func (s *Store) ListConversations(ctx context.Context, sessionID string) ([]Conversation, error) {
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT id, session_id, title, model, params_json, created_at, updated_at FROM chat_conversations WHERE session_id = ? ORDER BY updated_at DESC`, sessionID,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Conversation
|
|
for rows.Next() {
|
|
var c Conversation
|
|
if err := rows.Scan(&c.ID, &c.SessionID, &c.Title, &c.Model, &c.ParamsJSON, &c.CreatedAt, &c.UpdatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, c)
|
|
}
|
|
return items, rows.Err()
|
|
}
|
|
|
|
func (s *Store) GetConversation(ctx context.Context, sessionID string, id int64) (*Conversation, error) {
|
|
var c Conversation
|
|
err := s.db.QueryRowContext(ctx,
|
|
`SELECT id, session_id, title, model, params_json, created_at, updated_at FROM chat_conversations WHERE id = ? AND session_id = ?`, id, sessionID,
|
|
).Scan(&c.ID, &c.SessionID, &c.Title, &c.Model, &c.ParamsJSON, &c.CreatedAt, &c.UpdatedAt)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
return &c, nil
|
|
}
|
|
|
|
func (s *Store) CreateConversation(ctx context.Context, c *Conversation) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO chat_conversations (session_id, title, model, params_json) VALUES (?, ?, ?, ?)`,
|
|
c.SessionID, c.Title, c.Model, c.ParamsJSON,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (s *Store) UpdateConversation(ctx context.Context, c *Conversation) error {
|
|
_, err := s.db.ExecContext(ctx,
|
|
`UPDATE chat_conversations SET title=?, model=?, params_json=?, updated_at=datetime('now') WHERE id=? AND session_id=?`,
|
|
c.Title, c.Model, c.ParamsJSON, c.ID, c.SessionID,
|
|
)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) DeleteConversation(ctx context.Context, sessionID string, id int64) error {
|
|
_, err := s.db.ExecContext(ctx, `DELETE FROM chat_conversations WHERE id = ? AND session_id = ?`, id, sessionID)
|
|
return err
|
|
}
|
|
|
|
func (s *Store) ListMessages(ctx context.Context, conversationID int64, limit int) ([]ChatMessageRow, error) {
|
|
if limit <= 0 {
|
|
limit = 80
|
|
}
|
|
rows, err := s.db.QueryContext(ctx,
|
|
`SELECT id, conversation_id, role, content, created_at FROM chat_messages WHERE conversation_id = ? ORDER BY id DESC LIMIT ?`, conversationID, limit,
|
|
)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []ChatMessageRow
|
|
for rows.Next() {
|
|
var m ChatMessageRow
|
|
if err := rows.Scan(&m.ID, &m.ConversationID, &m.Role, &m.Content, &m.CreatedAt); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, m)
|
|
}
|
|
for i, j := 0, len(items)-1; i < j; i, j = i+1, j-1 {
|
|
items[i], items[j] = items[j], items[i]
|
|
}
|
|
return items, rows.Err()
|
|
}
|
|
|
|
func (s *Store) InsertMessage(ctx context.Context, m *ChatMessageRow) (int64, error) {
|
|
res, err := s.db.ExecContext(ctx,
|
|
`INSERT INTO chat_messages (conversation_id, role, content) VALUES (?, ?, ?)`,
|
|
m.ConversationID, m.Role, m.Content,
|
|
)
|
|
if err != nil {
|
|
return 0, err
|
|
}
|
|
_, _ = s.db.ExecContext(ctx, `UPDATE chat_conversations SET updated_at=datetime('now') WHERE id=?`, m.ConversationID)
|
|
return res.LastInsertId()
|
|
}
|
|
|
|
func (s *Store) ClearMessages(ctx context.Context, conversationID int64) error {
|
|
_, err := s.db.ExecContext(ctx, `DELETE FROM chat_messages WHERE conversation_id = ?`, conversationID)
|
|
return err
|
|
}
|