summaryrefslogtreecommitdiffhomepage
path: root/internal/session
diff options
context:
space:
mode:
authorKujtim Hoxha <[email protected]>2025-04-21 19:59:35 +0200
committerGitHub <[email protected]>2025-04-21 19:59:35 +0200
commitf33dff87725764af0b675b5e5b2e011b21c14c90 (patch)
tree4fe2c022305f13775f2cab3cdd80cd808259765b /internal/session
parent6b1c64bcc75b89c530294b6a2d4404682b435d56 (diff)
parent3a6a26981a8074b6ab0eaadb520db986e04799ff (diff)
downloadopencode-f33dff87725764af0b675b5e5b2e011b21c14c90.tar.gz
opencode-f33dff87725764af0b675b5e5b2e011b21c14c90.zip
Merge pull request #27 from kujtimiihoxha/opencode
OpenCode - Initial Implementation
Diffstat (limited to 'internal/session')
-rw-r--r--internal/session/session.go63
1 files changed, 38 insertions, 25 deletions
diff --git a/internal/session/session.go b/internal/session/session.go
index 13f420b7c..280da1ff0 100644
--- a/internal/session/session.go
+++ b/internal/session/session.go
@@ -5,8 +5,8 @@ import (
"database/sql"
"github.com/google/uuid"
- "github.com/kujtimiihoxha/termai/internal/db"
- "github.com/kujtimiihoxha/termai/internal/pubsub"
+ "github.com/kujtimiihoxha/opencode/internal/db"
+ "github.com/kujtimiihoxha/opencode/internal/pubsub"
)
type Session struct {
@@ -23,22 +23,22 @@ type Session struct {
type Service interface {
pubsub.Suscriber[Session]
- Create(title string) (Session, error)
- CreateTaskSession(toolCallID, parentSessionID, title string) (Session, error)
- Get(id string) (Session, error)
- List() ([]Session, error)
- Save(session Session) (Session, error)
- Delete(id string) error
+ Create(ctx context.Context, title string) (Session, error)
+ CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error)
+ CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error)
+ Get(ctx context.Context, id string) (Session, error)
+ List(ctx context.Context) ([]Session, error)
+ Save(ctx context.Context, session Session) (Session, error)
+ Delete(ctx context.Context, id string) error
}
type service struct {
*pubsub.Broker[Session]
- q db.Querier
- ctx context.Context
+ q db.Querier
}
-func (s *service) Create(title string) (Session, error) {
- dbSession, err := s.q.CreateSession(s.ctx, db.CreateSessionParams{
+func (s *service) Create(ctx context.Context, title string) (Session, error) {
+ dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
ID: uuid.New().String(),
Title: title,
})
@@ -50,8 +50,8 @@ func (s *service) Create(title string) (Session, error) {
return session, nil
}
-func (s *service) CreateTaskSession(toolCallID, parentSessionID, title string) (Session, error) {
- dbSession, err := s.q.CreateSession(s.ctx, db.CreateSessionParams{
+func (s *service) CreateTaskSession(ctx context.Context, toolCallID, parentSessionID, title string) (Session, error) {
+ dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
ID: toolCallID,
ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
Title: title,
@@ -64,12 +64,26 @@ func (s *service) CreateTaskSession(toolCallID, parentSessionID, title string) (
return session, nil
}
-func (s *service) Delete(id string) error {
- session, err := s.Get(id)
+func (s *service) CreateTitleSession(ctx context.Context, parentSessionID string) (Session, error) {
+ dbSession, err := s.q.CreateSession(ctx, db.CreateSessionParams{
+ ID: "title-" + parentSessionID,
+ ParentSessionID: sql.NullString{String: parentSessionID, Valid: true},
+ Title: "Generate a title",
+ })
+ if err != nil {
+ return Session{}, err
+ }
+ session := s.fromDBItem(dbSession)
+ s.Publish(pubsub.CreatedEvent, session)
+ return session, nil
+}
+
+func (s *service) Delete(ctx context.Context, id string) error {
+ session, err := s.Get(ctx, id)
if err != nil {
return err
}
- err = s.q.DeleteSession(s.ctx, session.ID)
+ err = s.q.DeleteSession(ctx, session.ID)
if err != nil {
return err
}
@@ -77,16 +91,16 @@ func (s *service) Delete(id string) error {
return nil
}
-func (s *service) Get(id string) (Session, error) {
- dbSession, err := s.q.GetSessionByID(s.ctx, id)
+func (s *service) Get(ctx context.Context, id string) (Session, error) {
+ dbSession, err := s.q.GetSessionByID(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{
+func (s *service) Save(ctx context.Context, session Session) (Session, error) {
+ dbSession, err := s.q.UpdateSession(ctx, db.UpdateSessionParams{
ID: session.ID,
Title: session.Title,
PromptTokens: session.PromptTokens,
@@ -101,8 +115,8 @@ func (s *service) Save(session Session) (Session, error) {
return session, nil
}
-func (s *service) List() ([]Session, error) {
- dbSessions, err := s.q.ListSessions(s.ctx)
+func (s *service) List(ctx context.Context) ([]Session, error) {
+ dbSessions, err := s.q.ListSessions(ctx)
if err != nil {
return nil, err
}
@@ -127,11 +141,10 @@ func (s service) fromDBItem(item db.Session) Session {
}
}
-func NewService(ctx context.Context, q db.Querier) Service {
+func NewService(q db.Querier) Service {
broker := pubsub.NewBroker[Session]()
return &service{
broker,
q,
- ctx,
}
}