Skip to content

Commit

Permalink
Merge pull request #122 from basenana/friday/update
Browse files Browse the repository at this point in the history
fix: openroom with entryid
  • Loading branch information
zwwhdls authored May 29, 2024
2 parents 462deb4 + 2d44bd8 commit adaa8cb
Show file tree
Hide file tree
Showing 16 changed files with 161 additions and 49 deletions.
31 changes: 16 additions & 15 deletions cmd/apps/apis/fsapi/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,25 +103,26 @@ func (s *services) OpenRoom(ctx context.Context, request *OpenRoomRequest) (*Ope
if request.EntryID == 0 {
return nil, status.Error(codes.InvalidArgument, "entry id is empty")
}
if request.RoomID == 0 {
// need create a new one
prompt := ""
if request.Option != nil {
prompt = request.Option.Prompt
}
room, err := s.ctrl.CreateRoom(ctx, request.EntryID, prompt)
if err != nil {
return nil, status.Error(common.FsApiError(err), "create room failed")
}
return &OpenRoomResponse{
Room: &RoomInfo{Id: room.ID, EntryID: room.EntryId, Namespace: room.Namespace, Title: room.Title, Prompt: room.Prompt, CreatedAt: timestamppb.New(room.CreatedAt)},
}, nil
}

room, err := s.ctrl.GetRoom(ctx, request.RoomID)
room, err := s.ctrl.FindRoom(ctx, request.EntryID)
if err != nil {
if err == types.ErrNotFound {
// need create a new one
prompt := ""
if request.Option != nil {
prompt = request.Option.Prompt
}
room, err := s.ctrl.CreateRoom(ctx, request.EntryID, prompt)
if err != nil {
return nil, status.Error(common.FsApiError(err), "create room failed")
}
return &OpenRoomResponse{
Room: &RoomInfo{Id: room.ID, EntryID: room.EntryId, Namespace: room.Namespace, Title: room.Title, Prompt: room.Prompt, CreatedAt: timestamppb.New(room.CreatedAt)},
}, nil
}
return nil, status.Error(common.FsApiError(err), "get room failed")
}

msg := make([]*RoomMessage, 0, len(room.Messages))
for _, m := range room.Messages {
msg = append(msg, &RoomMessage{
Expand Down
6 changes: 0 additions & 6 deletions cmd/apps/apis/fsapi/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,12 +128,6 @@ var _ = Describe("testRoomService", func() {
It("delete should be succeed", func() {
_, err := serviceClient.DeleteRoom(ctx, &DeleteRoomRequest{RoomID: roomId}, grpc.UseCompressor(gzip.Name))
Expect(err).Should(BeNil())
res, err := serviceClient.OpenRoom(ctx, &OpenRoomRequest{
EntryID: 1,
RoomID: roomId,
}, grpc.UseCompressor(gzip.Name))
Expect(err).ShouldNot(BeNil())
Expect(res).Should(BeNil())
})
})
})
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ require (
github.com/aws/aws-sdk-go-v2/credentials v1.13.26
github.com/aws/aws-sdk-go-v2/service/s3 v1.36.0
github.com/aws/smithy-go v1.13.5
github.com/basenana/friday v0.0.0-20240514065549-962b40b3faf1
github.com/basenana/friday v0.0.0-20240529034549-d8f6efb8f215
github.com/blevesearch/bleve/v2 v2.4.0
github.com/bluele/gcache v0.0.2
github.com/getsentry/sentry-go v0.22.0
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ github.com/aymerick/douceur v0.2.0 h1:Mv+mAeH1Q+n9Fr+oyamOlAkUNPWPlA8PPGR0QAaYuP
github.com/aymerick/douceur v0.2.0/go.mod h1:wlT5vV2O3h55X9m7iVYN0TBM0NH/MmbLnd30/FjWUq4=
github.com/basenana/friday v0.0.0-20240514065549-962b40b3faf1 h1:8vEnxa3CEbTtEqJIk44ZzhvZG+6DuJlLO+Mfa9psuAs=
github.com/basenana/friday v0.0.0-20240514065549-962b40b3faf1/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/basenana/friday v0.0.0-20240529034549-d8f6efb8f215 h1:kCuKG91mxPXnkbsmFiRLBlni6qtmwwFnhe0Bm1O2cD4=
github.com/basenana/friday v0.0.0-20240529034549-d8f6efb8f215/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/benbjohnson/clock v1.1.0 h1:Q92kusRqC1XV2MjkWETPvjJVqKetz1OzxZB7mHJLju8=
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
github.com/beorn7/perks v1.0.1/go.mod h1:G2ZrVWU2WbWT9wwq4/hrbKbnv/1ERSJQ0ibhJ6rlkpw=
Expand Down
1 change: 1 addition & 0 deletions pkg/controller/controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,7 @@ type Controller interface {
ListRooms(ctx context.Context, entryId int64) ([]*types.Room, error)
CreateRoom(ctx context.Context, entryId int64, prompt string) (*types.Room, error)
GetRoom(ctx context.Context, id int64) (*types.Room, error)
FindRoom(ctx context.Context, entryId int64) (*types.Room, error)
UpdateRoom(ctx context.Context, roomId int64, prompt string) error
DeleteRoom(ctx context.Context, id int64) error
ClearRoom(ctx context.Context, id int64) error
Expand Down
26 changes: 21 additions & 5 deletions pkg/controller/dialogue.go
Original file line number Diff line number Diff line change
Expand Up @@ -63,6 +63,15 @@ func (c *controller) GetRoom(ctx context.Context, id int64) (*types.Room, error)
return result, nil
}

func (c *controller) FindRoom(ctx context.Context, entryId int64) (*types.Room, error) {
result, err := c.dialogue.FindRoom(ctx, entryId)
if err != nil {
c.logger.Errorw("find room failed", "err", err)
return nil, err
}
return result, nil
}

func (c *controller) DeleteRoom(ctx context.Context, id int64) error {
err := c.dialogue.DeleteRoom(ctx, id)
if err != nil {
Expand All @@ -88,7 +97,13 @@ func (c *controller) ClearRoom(ctx context.Context, id int64) error {
}

func (c *controller) CreateRoomMessage(ctx context.Context, roomID int64, sender, msg string, sendAt time.Time) (*types.RoomMessage, error) {
room, err := c.dialogue.GetRoom(ctx, roomID)
if err != nil {
c.logger.Errorw("get room failed", "err", err)
return nil, err
}
result, err := c.dialogue.SaveMessage(ctx, &types.RoomMessage{
Namespace: room.Namespace,
RoomID: roomID,
Sender: sender,
Message: msg,
Expand Down Expand Up @@ -183,11 +198,12 @@ func (c *controller) ChatInRoom(ctx context.Context, roomId int64, newMsg string

// save model message
response, err := c.dialogue.SaveMessage(ctx, &types.RoomMessage{
ID: responseMsgId,
RoomID: roomId,
Sender: model,
Message: respMsg,
SendAt: time.Now(),
ID: responseMsgId,
Namespace: room.Namespace,
RoomID: roomId,
Sender: model,
Message: respMsg,
SendAt: time.Now(),
})
if err != nil {
c.logger.Errorw("save message failed", "err", err)
Expand Down
15 changes: 14 additions & 1 deletion pkg/dialogue/dialogue.go
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ type Manager interface {
CreateRoom(ctx context.Context, entryId int64, prompt string) (*types.Room, error)
UpdateRoom(ctx context.Context, room *types.Room) error
GetRoom(ctx context.Context, id int64) (*types.Room, error)
FindRoom(ctx context.Context, entryId int64) (*types.Room, error)
DeleteRoom(ctx context.Context, id int64) error
DeleteRoomMessages(ctx context.Context, roomId int64) error
SaveMessage(ctx context.Context, roomMessage *types.RoomMessage) (*types.RoomMessage, error)
Expand Down Expand Up @@ -109,6 +110,19 @@ func (m *manager) GetRoom(ctx context.Context, id int64) (*types.Room, error) {
return room, nil
}

func (m *manager) FindRoom(ctx context.Context, entryId int64) (*types.Room, error) {
room, err := m.recorder.FindRoom(ctx, entryId)
if err != nil {
return nil, err
}
msgs, err := m.recorder.ListRoomMessage(ctx, room.ID)
if err != nil {
return nil, err
}
room.Messages = msgs
return room, nil
}

func (m *manager) DeleteRoom(ctx context.Context, id int64) error {
err := m.recorder.DeleteRoomMessages(ctx, id)
if err != nil {
Expand All @@ -132,7 +146,6 @@ func (m *manager) SaveMessage(ctx context.Context, roomMessage *types.RoomMessag

if crtMsg == nil {
roomMessage.CreatedAt = time.Now()
roomMessage.Namespace = types.GetNamespace(ctx).String()
return roomMessage, m.recorder.SaveRoomMessage(ctx, roomMessage)
}

Expand Down
8 changes: 8 additions & 0 deletions pkg/metastore/instrumental.go
Original file line number Diff line number Diff line change
Expand Up @@ -530,6 +530,14 @@ func (i instrumentalStore) GetRoom(ctx context.Context, id int64) (*types.Room,
return room, err
}

func (i instrumentalStore) FindRoom(ctx context.Context, entryId int64) (*types.Room, error) {
const operation = "find_room"
defer logOperationLatency(operation, time.Now())
room, err := i.store.FindRoom(ctx, entryId)
logOperationError(operation, err)
return room, err
}

func (i instrumentalStore) DeleteRoom(ctx context.Context, id int64) error {
const operation = "delete_room"
defer logOperationLatency(operation, time.Now())
Expand Down
1 change: 1 addition & 0 deletions pkg/metastore/interface.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ type DEntry interface {

SaveRoom(ctx context.Context, room *types.Room) error
GetRoom(ctx context.Context, id int64) (*types.Room, error)
FindRoom(ctx context.Context, entryId int64) (*types.Room, error)
DeleteRoom(ctx context.Context, id int64) error
ListRooms(ctx context.Context, entryId int64) ([]*types.Room, error)
ListRoomMessage(ctx context.Context, roomId int64) ([]*types.RoomMessage, error)
Expand Down
16 changes: 16 additions & 0 deletions pkg/metastore/sql.go
Original file line number Diff line number Diff line change
Expand Up @@ -410,6 +410,12 @@ func (s *sqliteMetaStore) GetRoom(ctx context.Context, id int64) (*types.Room, e
return s.dbStore.GetRoom(ctx, id)
}

func (s *sqliteMetaStore) FindRoom(ctx context.Context, entryId int64) (*types.Room, error) {
s.mux.Lock()
defer s.mux.Unlock()
return s.dbStore.FindRoom(ctx, entryId)
}

func (s *sqliteMetaStore) DeleteRoom(ctx context.Context, id int64) error {
s.mux.Lock()
defer s.mux.Unlock()
Expand Down Expand Up @@ -2066,6 +2072,16 @@ func (s *sqlMetaStore) GetRoom(ctx context.Context, id int64) (*types.Room, erro
return room.To()
}

func (s *sqlMetaStore) FindRoom(ctx context.Context, entryId int64) (*types.Room, error) {
defer trace.StartRegion(ctx, "metastore.sql.FindRoom").End()
room := &db.Room{}
res := s.WithNamespace(ctx).Where("entry_id = ?", entryId).First(room)
if res.Error != nil {
return nil, db.SqlError2Error(res.Error)
}
return room.To()
}

func (s *sqlMetaStore) DeleteRoom(ctx context.Context, id int64) error {
defer trace.StartRegion(ctx, "metastore.sql.DeleteRoom").End()
err := s.WithContext(ctx).Transaction(func(tx *gorm.DB) error {
Expand Down
4 changes: 2 additions & 2 deletions vendor/github.com/basenana/friday/pkg/friday/friday.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions vendor/github.com/basenana/friday/pkg/friday/question.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 51 additions & 0 deletions vendor/github.com/basenana/friday/pkg/models/namespace.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

12 changes: 12 additions & 0 deletions vendor/github.com/basenana/friday/pkg/vectorstore/db/entity.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion vendor/modules.txt
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ github.com/aws/smithy-go/waiter
## explicit
github.com/aymerick/douceur/css
github.com/aymerick/douceur/parser
# github.com/basenana/friday v0.0.0-20240514065549-962b40b3faf1
# github.com/basenana/friday v0.0.0-20240529034549-d8f6efb8f215
## explicit; go 1.20
github.com/basenana/friday/config
github.com/basenana/friday/pkg/build/withvector
Expand Down

0 comments on commit adaa8cb

Please sign in to comment.