From e4f121f415c45f2d299d8ad1ffeb105b34450ce5 Mon Sep 17 00:00:00 2001 From: luke-lombardi <33990301+luke-lombardi@users.noreply.github.com> Date: Mon, 25 Nov 2024 14:49:57 -0500 Subject: [PATCH 1/2] Bot: Add event history to bot session response (#736) - Adds full event history to bot session response object --- pkg/abstractions/experimental/bot/bot.go | 5 +++ pkg/abstractions/experimental/bot/instance.go | 1 + pkg/abstractions/experimental/bot/state.go | 33 ++++++++++++++++++- pkg/abstractions/experimental/bot/types.go | 7 ++-- 4 files changed, 42 insertions(+), 4 deletions(-) diff --git a/pkg/abstractions/experimental/bot/bot.go b/pkg/abstractions/experimental/bot/bot.go index 8aaea7389..620a0844e 100644 --- a/pkg/abstractions/experimental/bot/bot.go +++ b/pkg/abstractions/experimental/bot/bot.go @@ -339,6 +339,7 @@ var ( botInputBuffer string = "bot:%s:%s:input_buffer:%s" botEventPair string = "bot:%s:%s:event_pair:%s:%s" botEventBuffer string = "bot:%s:%s:event_buffer:%s" + botEventHistory string = "bot:%s:%s:event_history:%s" botSessionIndex string = "bot:%s:%s:session_index" botSessionState string = "bot:%s:%s:session_state:%s" botSessionKeepAlive string = "bot:%s:%s:session_keep_alive:%s" @@ -367,6 +368,10 @@ func (k *keys) botEventBuffer(workspaceName, stubId, sessionId string) string { return fmt.Sprintf(botEventBuffer, workspaceName, stubId, sessionId) } +func (k *keys) botEventHistory(workspaceName, stubId, sessionId string) string { + return fmt.Sprintf(botEventHistory, workspaceName, stubId, sessionId) +} + func (k *keys) botSessionState(workspaceName, stubId, sessionId string) string { return fmt.Sprintf(botSessionState, workspaceName, stubId, sessionId) } diff --git a/pkg/abstractions/experimental/bot/instance.go b/pkg/abstractions/experimental/bot/instance.go index 0e25f2e06..8ad69bde3 100644 --- a/pkg/abstractions/experimental/bot/instance.go +++ b/pkg/abstractions/experimental/bot/instance.go @@ -375,6 +375,7 @@ func (i *botInstance) monitorEvents() error { string(MetadataTransitionName): event.Metadata[string(MetadataTransitionName)], string(MetadataTaskId): event.Metadata[string(MetadataTaskId)], }, + PairId: event.PairId, }, event) case BotEventTypeAcceptTransition, BotEventTypeRejectTransition: taskId := event.Metadata[string(MetadataTaskId)] diff --git a/pkg/abstractions/experimental/bot/state.go b/pkg/abstractions/experimental/bot/state.go index 0ca0c5e37..b1197768b 100644 --- a/pkg/abstractions/experimental/bot/state.go +++ b/pkg/abstractions/experimental/bot/state.go @@ -89,6 +89,7 @@ func (m *botStateManager) deleteSession(workspaceName, stubId, sessionId string) stateKey := Keys.botSessionState(workspaceName, stubId, sessionId) indexKey := Keys.botSessionIndex(workspaceName, stubId) taskIndexKey := Keys.botTaskIndex(workspaceName, stubId, sessionId) + historyKey := Keys.botEventHistory(workspaceName, stubId, sessionId) err := m.rdb.Del(ctx, stateKey).Err() if err != nil { @@ -112,6 +113,11 @@ func (m *botStateManager) deleteSession(workspaceName, stubId, sessionId string) return err } + err = m.rdb.Del(ctx, historyKey).Err() + if err != nil { + return err + } + // TODO: delete all existing markers return m.rdb.SRem(ctx, indexKey, sessionId).Err() } @@ -145,6 +151,24 @@ func (m *botStateManager) getSession(workspaceName, stubId, sessionId string) (* return nil, err } + historyKey := Keys.botEventHistory(workspaceName, stubId, sessionId) + history, err := m.rdb.LRange(context.TODO(), historyKey, 0, -1) + if err != nil { + return nil, err + } + + events := []*BotEvent{} + for _, jsonData := range history { + event := &BotEvent{} + err = json.Unmarshal([]byte(jsonData), event) + if err != nil { + return nil, err + } + + events = append(events, event) + } + + session.EventHistory = events return session, nil } @@ -266,7 +290,14 @@ func (m *botStateManager) pushEvent(workspaceName, stubId, sessionId string, eve } messageKey := Keys.botEventBuffer(workspaceName, stubId, sessionId) - return m.rdb.RPush(context.TODO(), messageKey, jsonData).Err() + historyKey := Keys.botEventHistory(workspaceName, stubId, sessionId) + + err = m.rdb.RPush(context.TODO(), messageKey, jsonData).Err() + if err != nil { + return err + } + + return m.rdb.RPush(context.TODO(), historyKey, jsonData).Err() } const eventPairTtlS = 120 * time.Second diff --git a/pkg/abstractions/experimental/bot/types.go b/pkg/abstractions/experimental/bot/types.go index 7762eb0bd..958bec390 100644 --- a/pkg/abstractions/experimental/bot/types.go +++ b/pkg/abstractions/experimental/bot/types.go @@ -17,9 +17,10 @@ const ( var ErrBotSessionNotFound = fmt.Errorf("bot session not found") type BotSession struct { - Id string `json:"id" redis:"id"` - Messages []BotChatCompletionMessage `json:"messages" redis:"messages"` - CreatedAt int64 `json:"created_at" redis:"created_at"` + Id string `json:"id" redis:"id"` + Messages []BotChatCompletionMessage `json:"messages" redis:"messages"` + CreatedAt int64 `json:"created_at" redis:"created_at"` + EventHistory []*BotEvent `json:"event_history" redis:"event_history"` } type botContainer struct { From 949d3aa230047215a948ae7d126070a6602a391b Mon Sep 17 00:00:00 2001 From: luke-lombardi <33990301+luke-lombardi@users.noreply.github.com> Date: Mon, 25 Nov 2024 15:27:18 -0500 Subject: [PATCH 2/2] add public getsession endpoint (#737) --- pkg/abstractions/experimental/bot/http.go | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/pkg/abstractions/experimental/bot/http.go b/pkg/abstractions/experimental/bot/http.go index 780e17508..6821819cb 100644 --- a/pkg/abstractions/experimental/bot/http.go +++ b/pkg/abstractions/experimental/bot/http.go @@ -39,11 +39,14 @@ func registerBotRoutes(g *echo.Group, pbs *PetriBotService) *botGroup { g.GET("/:deploymentName", auth.WithAuth(group.BotOpenSession)) g.GET("/:deploymentName/latest", auth.WithAuth(group.BotOpenSession)) g.GET("/:deploymentName/v:version", auth.WithAuth(group.BotOpenSession)) - g.GET("/public/:stubId", auth.WithAssumedStubAuth(group.BotOpenSession, group.pbs.isPublic)) g.DELETE("/:stubId/:sessionId", auth.WithAuth(group.BotDeleteSession)) g.GET("/:stubId/:sessionId", auth.WithAuth(group.BotGetSession)) g.GET("/:stubId/sessions", auth.WithAuth(group.BotListSessions)) + // Public endpoints + g.GET("/public/:stubId", auth.WithAssumedStubAuth(group.BotOpenSession, group.pbs.isPublic)) + g.GET("/public/:stubId/:sessionId", auth.WithAssumedStubAuth(group.BotGetSession, group.pbs.isPublic)) + return group }