7 "github.com/charmbracelet/crush/internal/db"
8 "github.com/charmbracelet/crush/internal/pubsub"
9 "github.com/google/uuid"
14 ParentSessionID string
18 CompletionTokens int64
19 SummaryMessageID string
25type Service interface {
26 pubsub.Suscriber[Session]
27 Create(ctx context.Context, title string) (Session, error)
28 CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error)
29 CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error)
30 Get(ctx context.Context, id string) (Session, error)
31 List(ctx context.Context) ([]Session, error)
32 Save(ctx context.Context, session Session) (Session, error)
33 Delete(ctx context.Context, id string) error
37 *pubsub.Broker[Session]
41func (s *service) Create(ctx context.Context, title string) (Session, error) {
42 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
43 ID: uuid.New().String(),
49 session := s.fromDBItem(dbSession)
50 s.Publish(pubsub.CreatedEvent, session)
54func (s *service) CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error) {
55 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
57 ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
63 session := s.fromDBItem(dbSession)
64 s.Publish(pubsub.CreatedEvent, session)
68func (s *service) CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error) {
69 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
70 ID: "title-" + parentSessionID,
71 ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
72 Title: "Generate a title",
77 session := s.fromDBItem(dbSession)
78 s.Publish(pubsub.CreatedEvent, session)
82func (s *service) Delete(ctx context.Context, id string) error {
83 session, err := s.Get(ctx, id)
87 err = s.q.DeleteSession(ctx, session.ID)
91 s.Publish(pubsub.DeletedEvent, session)
95func (s *service) Get(ctx context.Context, id string) (Session, error) {
96 dbSession, err := s.q.GetSessionByID(ctx, id)
100 return s.fromDBItem(dbSession), nil
103func (s *service) Save(ctx context.Context, session Session) (Session, error) {
104 dbSession, err := s.q.UpdateSession(ctx, db.UpdateSessionParams{
106 Title: session.Title,
107 PromptTokens: session.PromptTokens,
108 CompletionTokens: session.CompletionTokens,
109 SummaryMessageID: sql.NullString{
110 String: session.SummaryMessageID,
111 Valid: session.SummaryMessageID != "",
116 return Session{}, err
118 session = s.fromDBItem(dbSession)
119 s.Publish(pubsub.UpdatedEvent, session)
123func (s *service) List(ctx context.Context) ([]Session, error) {
124 dbSessions, err := s.q.ListSessions(ctx)
128 sessions := make([]Session, len(dbSessions))
129 for i, dbSession := range dbSessions {
130 sessions[i] = s.fromDBItem(dbSession)
135func (s service) fromDBItem(item db.Session) Session {
138 ParentSessionID: item.ParentSessionID.String,
140 MessageCount: item.MessageCount,
141 PromptTokens: item.PromptTokens,
142 CompletionTokens: item.CompletionTokens,
143 SummaryMessageID: item.SummaryMessageID.String,
145 CreatedAt: item.CreatedAt,
146 UpdatedAt: item.UpdatedAt,
150func NewService(q db.Querier) Service {
151 broker := pubsub.NewBroker[Session]()
keysesc to close
- j k
- move down, up
- enter
- open
- esc
- back, or up a level
- 1 2 3 4
- readme, files, commits, refs
- /
- filter
- y
- copy the clone command
- c
- compare the chosen commit with another (or shift-click both)
- ?
- show or hide this list