7 "github.com/google/uuid"
8 "github.com/opencode-ai/opencode/internal/db"
9 "github.com/opencode-ai/opencode/internal/pubsub"
14 ParentSessionID string
18 CompletionTokens int64
24type Service interface {
25 pubsub.Suscriber[Session]
26 Create(ctx context.Context, title string) (Session, error)
27 CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error)
28 CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error)
29 Get(ctx context.Context, id string) (Session, error)
30 List(ctx context.Context) ([]Session, error)
31 Save(ctx context.Context, session Session) (Session, error)
32 Delete(ctx context.Context, id string) error
36 *pubsub.Broker[Session]
40func (s *service) Create(ctx context.Context, title string) (Session, error) {
41 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
42 ID: uuid.New().String(),
48 session := s.fromDBItem(dbSession)
49 s.Publish(pubsub.CreatedEvent, session)
53func (s *service) CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error) {
54 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
56 ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
62 session := s.fromDBItem(dbSession)
63 s.Publish(pubsub.CreatedEvent, session)
67func (s *service) CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error) {
68 dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
69 ID: "title-" + parentSessionID,
70 ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
71 Title: "Generate a title",
76 session := s.fromDBItem(dbSession)
77 s.Publish(pubsub.CreatedEvent, session)
81func (s *service) Delete(ctx context.Context, id string) error {
82 session, err := s.Get(ctx, id)
86 err = s.q.DeleteSession(ctx, session.ID)
90 s.Publish(pubsub.DeletedEvent, session)
94func (s *service) Get(ctx context.Context, id string) (Session, error) {
95 dbSession, err := s.q.GetSessionByID(ctx, id)
99 return s.fromDBItem(dbSession), nil
102func (s *service) Save(ctx context.Context, session Session) (Session, error) {
103 dbSession, err := s.q.UpdateSession(ctx, db.UpdateSessionParams{
105 Title: session.Title,
106 PromptTokens: session.PromptTokens,
107 CompletionTokens: session.CompletionTokens,
111 return Session{}, err
113 session = s.fromDBItem(dbSession)
114 s.Publish(pubsub.UpdatedEvent, session)
118func (s *service) List(ctx context.Context) ([]Session, error) {
119 dbSessions, err := s.q.ListSessions(ctx)
123 sessions := make([]Session, len(dbSessions))
124 for i, dbSession := range dbSessions {
125 sessions[i] = s.fromDBItem(dbSession)
130func (s service) fromDBItem(item db.Session) Session {
133 ParentSessionID: item.ParentSessionID.String,
135 MessageCount: item.MessageCount,
136 PromptTokens: item.PromptTokens,
137 CompletionTokens: item.CompletionTokens,
139 CreatedAt: item.CreatedAt,
140 UpdatedAt: item.UpdatedAt,
144func NewService(q db.Querier) Service {
145 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