Skip to content

Commit

Permalink
Move username and ID out of AssistantMessage (#25964)
Browse files Browse the repository at this point in the history
* Move username and ID out of AssistantMessage

#25810 added username and conversation ID to AssistantMessage. They aren't needed inside the message and can be moved out of it.
This PR also fixes ` grpc: error while marshaling: proto: Marshal called with nil` in `CreateAssistantMessage`.

Note: It's safe to change protobuf numbers as this code hasn't been released or backported yet.

* Fix test

* make grpc
  • Loading branch information
jakule committed May 10, 2023
1 parent 9663415 commit 55d3600
Show file tree
Hide file tree
Showing 5 changed files with 59 additions and 61 deletions.
80 changes: 40 additions & 40 deletions api/gen/proto/go/assist/v1/assist.pb.go

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

19 changes: 7 additions & 12 deletions api/proto/teleport/assist/v1/assist.proto
Original file line number Diff line number Diff line change
Expand Up @@ -26,34 +26,29 @@ message GetAssistantMessagesRequest {
// ConversationId identifies a conversation.
// It's used to tie all messages in a one conversation.
string conversation_id = 1;

// username is a username of the user who sent the message.
string username = 2;
}

// AssistantMessage is a message sent to the assistant service. The conversation
// must be created first.
message AssistantMessage {
// ConversationId is used to tie all messages into a conversation.
string conversation_id = 1;

// username is a username of the user who sent the message.
string username = 2;

// type is a type of message. It can be Chat response/query or a command to run.
string type = 3;

string type = 1;
// CreatedTime is the time when the event occurred.
google.protobuf.Timestamp created_time = 4;

google.protobuf.Timestamp created_time = 2;
// payload is a JSON message
string payload = 5;
string payload = 3;
}

// CreateAssistantMessageRequest is a request to the assistant service.
message CreateAssistantMessageRequest {
// message is a message sent to the assistant service.
AssistantMessage message = 1;
// ConversationId is used to tie all messages into a conversation.
string conversation_id = 2;
// username is a username of the user who sent the message.
string username = 3;
}

// GetAssistantMessagesResponse is a response from the assistant service.
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/assist/assistv1/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -83,5 +83,5 @@ func (a *Service) GetAssistantMessages(ctx context.Context, req *assist.GetAssis

// CreateAssistantMessage adds the message to the backend.
func (a *Service) CreateAssistantMessage(ctx context.Context, req *assist.CreateAssistantMessageRequest) (*emptypb.Empty, error) {
return nil, trace.Wrap(a.backend.CreateAssistantMessage(ctx, req))
return &emptypb.Empty{}, trace.Wrap(a.backend.CreateAssistantMessage(ctx, req))
}
9 changes: 6 additions & 3 deletions lib/services/local/assistant.go
Original file line number Diff line number Diff line change
Expand Up @@ -211,11 +211,14 @@ func (s *AssistService) GetAssistantMessages(ctx context.Context, req *assist.Ge

// CreateAssistantMessage adds the message to the backend.
func (s *AssistService) CreateAssistantMessage(ctx context.Context, req *assist.CreateAssistantMessageRequest) error {
msg := req.GetMessage()
if msg.Username == "" {
if req.Username == "" {
return trace.BadParameter("missing username")
}
if req.ConversationId == "" {
return trace.BadParameter("missing conversation ID")
}

msg := req.GetMessage()
value, err := json.Marshal(msg)
if err != nil {
return trace.Wrap(err)
Expand All @@ -224,7 +227,7 @@ func (s *AssistService) CreateAssistantMessage(ctx context.Context, req *assist.
messageID := uuid.New().String()

item := backend.Item{
Key: backend.Key(assistantMessagePrefix, msg.Username, msg.ConversationId, messageID),
Key: backend.Key(assistantMessagePrefix, req.Username, req.ConversationId, messageID),
Value: value,
}

Expand Down
10 changes: 5 additions & 5 deletions lib/services/local/assistant_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,12 @@ func TestAssistantCRUD(t *testing.T) {

t.Run("create message", func(t *testing.T) {
msg := &assist.CreateAssistantMessageRequest{
Username: username,
ConversationId: conversationID,
Message: &assist.AssistantMessage{
Username: username,
CreatedTime: timestamppb.New(time.Now()),
ConversationId: conversationID,
Payload: "foo",
Type: "USER_MSG",
CreatedTime: timestamppb.New(time.Now()),
Payload: "foo",
Type: "USER_MSG",
},
}
err := identity.CreateAssistantMessage(ctx, msg)
Expand Down

0 comments on commit 55d3600

Please sign in to comment.