Skip to content

Commit

Permalink
Merge branch 'main' into dlm/structured-logging
Browse files Browse the repository at this point in the history
  • Loading branch information
dleviminzi committed Nov 25, 2024
2 parents 9bfecbf + 949d3aa commit 62e35a4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 5 deletions.
5 changes: 5 additions & 0 deletions pkg/abstractions/experimental/bot/bot.go
Original file line number Diff line number Diff line change
Expand Up @@ -338,6 +338,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"
Expand Down Expand Up @@ -366,6 +367,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)
}
Expand Down
5 changes: 4 additions & 1 deletion pkg/abstractions/experimental/bot/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}

Expand Down
1 change: 1 addition & 0 deletions pkg/abstractions/experimental/bot/instance.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)]
Expand Down
33 changes: 32 additions & 1 deletion pkg/abstractions/experimental/bot/state.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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()
}
Expand Down Expand Up @@ -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
}

Expand Down Expand Up @@ -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
Expand Down
7 changes: 4 additions & 3 deletions pkg/abstractions/experimental/bot/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down

0 comments on commit 62e35a4

Please sign in to comment.