internal/core: ядро-машина состояний (ProcessTurn поверх storage) + история задач в БД
All checks were successful
build-test / build (push) Successful in 1m11s
All checks were successful
build-test / build (push) Successful in 1m11s
- status = единственный источник истины (collecting/ready как фазы диалога) - команды /start /cancel /skip /retry N /status N /continue N - Decider интерфейс (аналитик→opencode, мок для тестов) - task_history таблица для промпта аналитика - allow ready→collecting (правка черновика)
This commit is contained in:
55
internal/storage/history.go
Normal file
55
internal/storage/history.go
Normal file
@@ -0,0 +1,55 @@
|
||||
package storage
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
)
|
||||
|
||||
// HistoryMsg — одна запись истории диалога.
|
||||
type HistoryMsg struct {
|
||||
Role string `json:"role"` // "user" | "assistant"
|
||||
Content string `json:"content"`
|
||||
}
|
||||
|
||||
// AppendHistory добавляет сообщение в историю задачи.
|
||||
func (s *Storage) AppendHistory(ctx context.Context, taskID int64, role, content string) error {
|
||||
now := Now()
|
||||
_, err := s.db.ExecContext(ctx, `
|
||||
INSERT INTO task_history (task_id, role, content, created_at)
|
||||
VALUES (?, ?, ?, ?)`, taskID, role, content, now,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: append history: %w", ErrDB, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetHistory возвращает историю задачи, отсортированную по времени.
|
||||
func (s *Storage) GetHistory(ctx context.Context, taskID int64) ([]HistoryMsg, error) {
|
||||
rows, err := s.db.QueryContext(ctx, `
|
||||
SELECT role, content FROM task_history
|
||||
WHERE task_id = ? ORDER BY id ASC`, taskID)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("%w: get history: %w", ErrDB, err)
|
||||
}
|
||||
defer rows.Close()
|
||||
|
||||
var msgs []HistoryMsg
|
||||
for rows.Next() {
|
||||
var m HistoryMsg
|
||||
if err := rows.Scan(&m.Role, &m.Content); err != nil {
|
||||
return nil, fmt.Errorf("%w: scan history: %w", ErrDB, err)
|
||||
}
|
||||
msgs = append(msgs, m)
|
||||
}
|
||||
return msgs, rows.Err()
|
||||
}
|
||||
|
||||
// ClearHistory удаляет всю историю задачи (при /start).
|
||||
func (s *Storage) ClearHistory(ctx context.Context, taskID int64) error {
|
||||
_, err := s.db.ExecContext(ctx, `DELETE FROM task_history WHERE task_id = ?`, taskID)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%w: clear history: %w", ErrDB, err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
Reference in New Issue
Block a user