summaryrefslogtreecommitdiffhomepage
path: root/internal/session
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-03-23 19:19:08 +0100
committerKujtim Hoxha <[email protected]>2025-03-23 19:19:08 +0100
commit8daa6e774a6e02698c90392e7b2008542f789594 (patch)
tree8053fc1a4de87fc8dc3fd6e776577b0221c02ac5 /internal/session
parent796bbf4d66c0fc70e750c7f582019cb9a7298e59 (diff)
downloadopencode-8daa6e774a6e02698c90392e7b2008542f789594.tar.gz
opencode-8daa6e774a6e02698c90392e7b2008542f789594.zip
add initial stuff
Diffstat (limited to 'internal/session')
-rw-r--r--internal/session/session.go116
1 files changed, 116 insertions, 0 deletions
diff --git a/internal/session/session.go b/internal/session/session.go
new file mode 100644
index 000000000..60f35084f
--- /dev/null
+++ b/internal/session/session.go
@@ -0,0 +1,116 @@
+package session
+
+import (
+ "context"
+
+ "github.com/google/uuid"
+ "github.com/kujtimiihoxha/termai/internal/db"
+ "github.com/kujtimiihoxha/termai/internal/pubsub"
+)
+
+type Session struct {
+ ID string
+ Title string
+ MessageCount int64
+ Tokens int64
+ Cost float64
+ CreatedAt int64
+ UpdatedAt int64
+}
+
+type Service interface {
+ pubsub.Suscriber[Session]
+ Create(title string) (Session, error)
+ Get(id string) (Session, error)
+ List() ([]Session, error)
+ Save(session Session) (Session, error)
+ Delete(id string) error
+}
+
+type service struct {
+ *pubsub.Broker[Session]
+ q db.Querier
+ ctx context.Context
+}
+
+func (s *service) Create(title string) (Session, error) {
+ dbSession, err := s.q.CreateSession(s.ctx, db.CreateSessionParams{
+ ID: uuid.New().String(),
+ Title: title,
+ })
+ if err != nil {
+ return Session{}, err
+ }
+ session := s.fromDBItem(dbSession)
+ s.Publish(pubsub.CreatedEvent, session)
+ return session, nil
+}
+
+func (s *service) Delete(id string) error {
+ session, err := s.Get(id)
+ if err != nil {
+ return err
+ }
+ err = s.q.DeleteSession(s.ctx, session.ID)
+ if err != nil {
+ return err
+ }
+ s.Publish(pubsub.DeletedEvent, session)
+ return nil
+}
+
+func (s *service) Get(id string) (Session, error) {
+ dbSession, err := s.q.GetSessionByID(s.ctx, id)
+ if err != nil {
+ return Session{}, err
+ }
+ return s.fromDBItem(dbSession), nil
+}
+
+func (s *service) Save(session Session) (Session, error) {
+ dbSession, err := s.q.UpdateSession(s.ctx, db.UpdateSessionParams{
+ ID: session.ID,
+ Title: session.Title,
+ Tokens: session.Tokens,
+ Cost: session.Cost,
+ })
+ if err != nil {
+ return Session{}, err
+ }
+ session = s.fromDBItem(dbSession)
+ s.Publish(pubsub.UpdatedEvent, session)
+ return session, nil
+}
+
+func (s *service) List() ([]Session, error) {
+ dbSessions, err := s.q.ListSessions(s.ctx)
+ if err != nil {
+ return nil, err
+ }
+ sessions := make([]Session, len(dbSessions))
+ for i, dbSession := range dbSessions {
+ sessions[i] = s.fromDBItem(dbSession)
+ }
+ return sessions, nil
+}
+
+func (s service) fromDBItem(item db.Session) Session {
+ return Session{
+ ID: item.ID,
+ Title: item.Title,
+ MessageCount: item.MessageCount,
+ Tokens: item.Tokens,
+ Cost: item.Cost,
+ CreatedAt: item.CreatedAt,
+ UpdatedAt: item.UpdatedAt,
+ }
+}
+
+func NewService(ctx context.Context, q db.Querier) Service {
+ broker := pubsub.NewBroker[Session]()
+ return &service{
+ broker,
+ q,
+ ctx,
+ }
+}