-
Notifications
You must be signed in to change notification settings - Fork 829
feat: implement command history persistence with database integration #1254
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,136 @@ | ||||||||||||||||||||||||||||||||||||||||
| package commandhistory | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| import ( | ||||||||||||||||||||||||||||||||||||||||
| "context" | ||||||||||||||||||||||||||||||||||||||||
| "database/sql" | ||||||||||||||||||||||||||||||||||||||||
| "strings" | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| "github.com/charmbracelet/crush/internal/db" | ||||||||||||||||||||||||||||||||||||||||
| "github.com/charmbracelet/crush/internal/pubsub" | ||||||||||||||||||||||||||||||||||||||||
| "github.com/google/uuid" | ||||||||||||||||||||||||||||||||||||||||
| ) | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| type CommandHistory struct { | ||||||||||||||||||||||||||||||||||||||||
| ID string | ||||||||||||||||||||||||||||||||||||||||
| SessionID string | ||||||||||||||||||||||||||||||||||||||||
| Command string | ||||||||||||||||||||||||||||||||||||||||
| CreatedAt int64 | ||||||||||||||||||||||||||||||||||||||||
| UpdatedAt int64 | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| type Service interface { | ||||||||||||||||||||||||||||||||||||||||
| pubsub.Suscriber[CommandHistory] | ||||||||||||||||||||||||||||||||||||||||
| Add(ctx context.Context, sessionID, command string) (CommandHistory, error) | ||||||||||||||||||||||||||||||||||||||||
| ListBySession(ctx context.Context, sessionID string, limit int) ([]CommandHistory, error) | ||||||||||||||||||||||||||||||||||||||||
| DeleteSessionHistory(ctx context.Context, sessionID string) error | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| type service struct { | ||||||||||||||||||||||||||||||||||||||||
| *pubsub.Broker[CommandHistory] | ||||||||||||||||||||||||||||||||||||||||
| db *sql.DB | ||||||||||||||||||||||||||||||||||||||||
| q *db.Queries | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| const MaxHistorySize = 1000 | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func NewService(q *db.Queries, db *sql.DB) Service { | ||||||||||||||||||||||||||||||||||||||||
| return &service{ | ||||||||||||||||||||||||||||||||||||||||
| Broker: pubsub.NewBroker[CommandHistory](), | ||||||||||||||||||||||||||||||||||||||||
| q: q, | ||||||||||||||||||||||||||||||||||||||||
| db: db, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (s *service) Add(ctx context.Context, sessionID, command string) (CommandHistory, error) { | ||||||||||||||||||||||||||||||||||||||||
| command = strings.TrimSpace(command) | ||||||||||||||||||||||||||||||||||||||||
| if command == "" { | ||||||||||||||||||||||||||||||||||||||||
| return CommandHistory{}, nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Get current count for this session | ||||||||||||||||||||||||||||||||||||||||
| countRow, err := s.q.GetCommandHistoryCount(ctx, db.GetCommandHistoryCountParams{ | ||||||||||||||||||||||||||||||||||||||||
| SessionID: sessionID, | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return CommandHistory{}, err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // If we're at the limit, remove oldest entries | ||||||||||||||||||||||||||||||||||||||||
| if int(countRow.Count) >= MaxHistorySize { | ||||||||||||||||||||||||||||||||||||||||
| history, err := s.q.ListCommandHistoryBySession(ctx, db.ListCommandHistoryBySessionParams{ | ||||||||||||||||||||||||||||||||||||||||
| SessionID: sessionID, | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return CommandHistory{}, err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| // Remove oldest entries to make room | ||||||||||||||||||||||||||||||||||||||||
| toRemove := int(countRow.Count) - MaxHistorySize + 1 | ||||||||||||||||||||||||||||||||||||||||
| for i := 0; i < toRemove && i < len(history); i++ { | ||||||||||||||||||||||||||||||||||||||||
| // Simple deletion - in a more sophisticated implementation, | ||||||||||||||||||||||||||||||||||||||||
| // we might want to batch delete | ||||||||||||||||||||||||||||||||||||||||
| if _, err := s.db.ExecContext(ctx, "DELETE FROM command_history WHERE id = ?", history[i].ID); err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return CommandHistory{}, err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| dbHistory, err := s.q.CreateCommandHistory(ctx, db.CreateCommandHistoryParams{ | ||||||||||||||||||||||||||||||||||||||||
| ID: uuid.New().String(), | ||||||||||||||||||||||||||||||||||||||||
| SessionID: sessionID, | ||||||||||||||||||||||||||||||||||||||||
| Command: command, | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
| if err != nil { | ||||||||||||||||||||||||||||||||||||||||
| return CommandHistory{}, err | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| history := CommandHistory{ | ||||||||||||||||||||||||||||||||||||||||
| ID: dbHistory.ID, | ||||||||||||||||||||||||||||||||||||||||
| SessionID: dbHistory.SessionID, | ||||||||||||||||||||||||||||||||||||||||
| Command: dbHistory.Command, | ||||||||||||||||||||||||||||||||||||||||
| CreatedAt: dbHistory.CreatedAt, | ||||||||||||||||||||||||||||||||||||||||
| UpdatedAt: dbHistory.UpdatedAt, | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| s.Publish(pubsub.CreatedEvent, history) | ||||||||||||||||||||||||||||||||||||||||
| return history, nil | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| func (s *service) ListBySession(ctx context.Context, sessionID string, limit int) ([]CommandHistory, error) { | ||||||||||||||||||||||||||||||||||||||||
| if limit <= 0 { | ||||||||||||||||||||||||||||||||||||||||
| limit = MaxHistorySize | ||||||||||||||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||||||||||||||
| dbHistory, err := s.q.ListLatestCommandHistoryBySession(ctx, db.ListLatestCommandHistoryBySessionParams{ | ||||||||||||||||||||||||||||||||||||||||
| SessionID: sessionID, | ||||||||||||||||||||||||||||||||||||||||
| Limit: int64(limit), | ||||||||||||||||||||||||||||||||||||||||
| }) | ||||||||||||||||||||||||||||||||||||||||
|
Comment on lines
+100
to
+107
|
||||||||||||||||||||||||||||||||||||||||
| if limit <= 0 { | |
| limit = MaxHistorySize | |
| } | |
| dbHistory, err := s.q.ListLatestCommandHistoryBySession(ctx, db.ListLatestCommandHistoryBySessionParams{ | |
| SessionID: sessionID, | |
| Limit: int64(limit), | |
| }) | |
| var dbHistory []db.CommandHistory | |
| var err error | |
| if limit <= 0 { | |
| // Unbounded: get all history for the session | |
| dbHistory, err = s.q.ListCommandHistoryBySession(ctx, sessionID) | |
| } else { | |
| dbHistory, err = s.q.ListLatestCommandHistoryBySession(ctx, db.ListLatestCommandHistoryBySessionParams{ | |
| SessionID: sessionID, | |
| Limit: int64(limit), | |
| }) | |
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,70 @@ | ||
| package commandhistory | ||
|
|
||
| import ( | ||
| "context" | ||
| "testing" | ||
|
|
||
| "github.com/charmbracelet/crush/internal/db" | ||
| "github.com/charmbracelet/crush/internal/session" | ||
| "github.com/stretchr/testify/require" | ||
| ) | ||
|
|
||
| func setupTestService(t *testing.T) (context.Context, *service, *db.Queries) { | ||
| t.Helper() | ||
|
|
||
| ctx := context.Background() | ||
| conn, err := db.Connect(ctx, t.TempDir()) | ||
| require.NoError(t, err) | ||
| t.Cleanup(func() { | ||
| require.NoError(t, conn.Close()) | ||
| }) | ||
|
|
||
| queries := db.New(conn) | ||
| svc := NewService(queries, conn) | ||
| return ctx, svc.(*service), queries | ||
| } | ||
|
|
||
| func TestListBySessionReturnsChronologicalHistory(t *testing.T) { | ||
| ctx, svc, queries := setupTestService(t) | ||
|
|
||
| sessionSvc := session.NewService(queries) | ||
| sess, err := sessionSvc.Create(ctx, "test session") | ||
| require.NoError(t, err) | ||
|
|
||
| // Seed deterministic history with known timestamps. | ||
| rows := []struct { | ||
| id string | ||
| command string | ||
| ts int64 | ||
| }{ | ||
| {id: "cmd-1", command: "first", ts: 1}, | ||
| {id: "cmd-2", command: "second", ts: 2}, | ||
| {id: "cmd-3", command: "third", ts: 3}, | ||
| } | ||
|
|
||
| for _, row := range rows { | ||
| _, err := svc.db.ExecContext(ctx, ` | ||
| INSERT INTO command_history (id, session_id, command, created_at, updated_at) | ||
| VALUES (?, ?, ?, ?, ?)`, | ||
| row.id, sess.ID, row.command, row.ts, row.ts, | ||
| ) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| history, err := svc.ListBySession(ctx, sess.ID, 0) | ||
| require.NoError(t, err) | ||
| require.Len(t, history, 3) | ||
| require.Equal(t, []string{"first", "second", "third"}, []string{ | ||
| history[0].Command, | ||
| history[1].Command, | ||
| history[2].Command, | ||
| }) | ||
|
|
||
| limitedHistory, err := svc.ListBySession(ctx, sess.ID, 2) | ||
| require.NoError(t, err) | ||
| require.Len(t, limitedHistory, 2) | ||
| require.Equal(t, []string{"second", "third"}, []string{ | ||
| limitedHistory[0].Command, | ||
| limitedHistory[1].Command, | ||
| }) | ||
| } |
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Deleting oldest entries one-by-one after loading the full history is inefficient. Prefer a single statement that deletes the oldest N rows, e.g., DELETE by id IN a subquery limited by created_at ASC, to avoid loading and N round-trips.