Skip to content

Commit

Permalink
bug: deal with bad agent names
Browse files Browse the repository at this point in the history
  • Loading branch information
ibuildthecloud committed Dec 17, 2024
1 parent ef8de8f commit 569ed35
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion pkg/api/handlers/assistants.go
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
package handlers

import (
"crypto/sha256"
"fmt"
"net/http"
"regexp"
"slices"
"sort"
"strings"
Expand Down Expand Up @@ -141,13 +143,22 @@ func convertAssistant(agent v1.Agent) types.Assistant {
return assistant
}

var validAgentID = regexp.MustCompile(`^[a-z][a-z0-9-]*[a-z0-9]$`)

func normalizeAgentID(id string) string {
if !validAgentID.MatchString(id) {
return fmt.Sprintf("%x", sha256.Sum256([]byte(id)))[:12]
}
return id
}

func getUserThread(req api.Context, agentID string) (*v1.Thread, error) {
id := req.User.GetUID()
if id == "" {
id = "none"
}
id = name.SafeConcatNameWithSeparatorAndLength(64, ".", system.ThreadPrefix,
agentID, id)
normalizeAgentID(agentID), id)

var thread v1.Thread
if err := req.Get(&thread, id); kclient.IgnoreNotFound(err) != nil {
Expand Down

0 comments on commit 569ed35

Please sign in to comment.