2026-05-08 13:25:30 +10:00
|
|
|
package agentchat
|
2026-03-06 10:47:55 +01:00
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
|
|
"github.com/google/uuid"
|
|
|
|
|
|
|
|
|
|
"github.com/coder/coder/v2/codersdk/workspacesdk"
|
|
|
|
|
)
|
|
|
|
|
|
2026-05-08 13:25:30 +10:00
|
|
|
// extractContext reads chat identity headers from the request.
|
2026-03-06 10:47:55 +01:00
|
|
|
// Returns zero values if headers are absent (non-chat request).
|
2026-05-08 13:25:30 +10:00
|
|
|
func extractContext(r *http.Request) (chatID uuid.UUID, ancestorIDs []uuid.UUID, ok bool) {
|
2026-03-06 10:47:55 +01:00
|
|
|
raw := r.Header.Get(workspacesdk.CoderChatIDHeader)
|
|
|
|
|
if raw == "" {
|
|
|
|
|
return uuid.Nil, nil, false
|
|
|
|
|
}
|
|
|
|
|
chatID, err := uuid.Parse(raw)
|
|
|
|
|
if err != nil {
|
|
|
|
|
return uuid.Nil, nil, false
|
|
|
|
|
}
|
|
|
|
|
rawAncestors := r.Header.Get(workspacesdk.CoderAncestorChatIDsHeader)
|
|
|
|
|
if rawAncestors != "" {
|
|
|
|
|
var ids []string
|
|
|
|
|
if err := json.Unmarshal([]byte(rawAncestors), &ids); err == nil {
|
|
|
|
|
for _, s := range ids {
|
|
|
|
|
if id, err := uuid.Parse(s); err == nil {
|
|
|
|
|
ancestorIDs = append(ancestorIDs, id)
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
return chatID, ancestorIDs, true
|
|
|
|
|
}
|