Skip to content

Commit

Permalink
Merge pull request #99 from basenana/chat/waiting
Browse files Browse the repository at this point in the history
update: message waiting
  • Loading branch information
zwwhdls authored Apr 28, 2024
2 parents 094c5bb + c2b8403 commit 9b3dc96
Show file tree
Hide file tree
Showing 17 changed files with 187 additions and 143 deletions.
7 changes: 4 additions & 3 deletions cmd/apps/apis/fsapi/v1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,24 +173,25 @@ func (s *services) ChatInRoom(request *ChatRequest, server Room_ChatInRoomServer
errCh = make(chan error, 1)
)
defer timeoutF()

go func() {
defer close(errCh)
errCh <- s.ctrl.ChatInRoom(ctx, request.RoomID, request.NewRequest, chatCh)
}()
for {
select {
case <-ctx.Done():
err := errors.New("chat in room timeout")
err = errors.New("chat in room timeout")
return status.Error(common.FsApiError(err), "context timeout")
case err := <-errCh:
case err = <-errCh:
if err != nil {
return status.Error(common.FsApiError(err), "chat in room failed")
}
case reply, ok := <-chatCh:
if !ok {
return nil
}
if err := server.Send(&ChatResponse{
if err = server.Send(&ChatResponse{
ResponseID: reply.ResponseId,
ResponseMessage: reply.Line,
Sender: reply.Sender,
Expand Down
2 changes: 1 addition & 1 deletion cmd/apps/apis/fsapi/v1/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ var _ = Describe("testRoomService", func() {
content += resp.ResponseMessage
}

Expect(content).Should(Equal("hello, I am response\nabc"))
Expect(content).Should(Equal("🤔hello, I am response\nabc"))

// message should be saved
res, err := serviceClient.OpenRoom(ctx, &OpenRoomRequest{
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-20240422083937-ff7ccfab99af
github.com/basenana/friday v0.0.0-20240427115004-0dd2292b54db
github.com/blevesearch/bleve/v2 v2.3.10
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 @@ -54,6 +54,8 @@ github.com/basenana/friday v0.0.0-20240422082254-9359a3415e9c h1:Wmh19nZdLL+RnSc
github.com/basenana/friday v0.0.0-20240422082254-9359a3415e9c/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/basenana/friday v0.0.0-20240422083937-ff7ccfab99af h1:hkKKv0W4F08N7+vJK4qXKY3yJyilZO+4gV+8UumllRM=
github.com/basenana/friday v0.0.0-20240422083937-ff7ccfab99af/go.mod h1:izPqWGUN5Kxz6mb7Xwhz3Zoe5GdOa/O+Ht6VhIHmjaA=
github.com/basenana/friday v0.0.0-20240427115004-0dd2292b54db h1:njCwQm13kdU+x3DtvhDDy0Thnst92RUZENWKoesnrv4=
github.com/basenana/friday v0.0.0-20240427115004-0dd2292b54db/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
14 changes: 10 additions & 4 deletions pkg/controller/dialogue.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

friday2 "github.com/basenana/nanafs/pkg/friday"
"github.com/basenana/nanafs/pkg/types"
"github.com/basenana/nanafs/utils"
)

func (c *controller) ListRooms(ctx context.Context, entryId int64) ([]*types.Room, error) {
Expand Down Expand Up @@ -104,7 +105,7 @@ func (c *controller) ChatInRoom(ctx context.Context, roomId int64, newMsg string
respMsg string
realHistory = room.History
errCh = make(chan error, 1)
responseMsgId int64
responseMsgId = utils.GenerateNewID()
)
if entry.Kind == types.GroupKind {
isDir = true
Expand All @@ -124,6 +125,14 @@ func (c *controller) ChatInRoom(ctx context.Context, roomId int64, newMsg string
return err
}

reply <- types.ReplyChannel{
Line: "🤔",
ResponseId: responseMsgId,
Sender: "thinking",
SendAt: time.Now(),
CreatedAt: time.Now(),
}

go func() {
defer close(errCh)
realHistory, err = friday2.ChatWithEntry(ctx, room.EntryId, isDir, realHistory, responseCh)
Expand Down Expand Up @@ -166,9 +175,6 @@ func (c *controller) ChatInRoom(ctx context.Context, roomId int64, newMsg string
c.logger.Errorw("save message failed", "err", err)
return err
}
if responseMsgId == 0 {
responseMsgId = response.ID
}
reply <- types.ReplyChannel{
Line: line["content"],
ResponseId: responseMsgId,
Expand Down
10 changes: 7 additions & 3 deletions pkg/dialogue/dialogue.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ package dialogue

import (
"context"
"errors"
"time"

"go.uber.org/zap"
Expand Down Expand Up @@ -117,14 +118,17 @@ func (m *manager) DeleteRoom(ctx context.Context, id int64) error {
func (m *manager) SaveMessage(ctx context.Context, roomMessage *types.RoomMessage) (*types.RoomMessage, error) {
if roomMessage.ID == 0 {
roomMessage.ID = utils.GenerateNewID()
roomMessage.CreatedAt = time.Now()
return roomMessage, m.recorder.SaveRoomMessage(ctx, roomMessage)
}
crtMsg, err := m.recorder.GetRoomMessage(ctx, roomMessage.ID)
if err != nil {
if err != nil && !errors.Is(err, types.ErrNotFound) {
return nil, err
}

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

if roomMessage.Message != "" {
crtMsg.Message = roomMessage.Message
}
Expand Down
19 changes: 8 additions & 11 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.

69 changes: 33 additions & 36 deletions vendor/github.com/basenana/friday/pkg/llm/client/gemini/chat.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.

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.

Loading

0 comments on commit 9b3dc96

Please sign in to comment.