Why: Account disable could be followed by late Backend business writes, API key creation could implicitly create users, account bootstrap locks could invert, and directory cursors were process-local or leaked out-of-scope changes. Evidence: Local go test ./... and isolated PostgreSQL package suites passed on the staged candidate; exact committed-head SELF replay and stage 6 human review remain pending. Impact: Adds final account transactions and external-effect checks across HTTP, Host, Bus and run manager; tightens API key issuance and persists versioned cursor signing. Delegated RDM rename still needs the separately proposed real-operator contract, so A1 and stage 6 are not complete. Refs: Documents U3 r1, approved A1-A4 amendment fb3a821, pending delegated-projection amendment 5783940, Dev feature/iam-u3-account-directory-recovery.
305 lines
9.0 KiB
Go
305 lines
9.0 KiB
Go
package server
|
|
|
|
import (
|
|
"encoding/json"
|
|
"net/http"
|
|
"regexp"
|
|
"strings"
|
|
|
|
agentcommand "agent-command"
|
|
"agent-system-backend/internal/store"
|
|
core "agent-system-core"
|
|
)
|
|
|
|
var userCommandSlug = regexp.MustCompile(`^[a-z][a-z0-9-]*$`)
|
|
|
|
func (s *Server) listSecrets(w http.ResponseWriter, r *http.Request) {
|
|
list, err := s.st.ListSecrets(s.owner(r))
|
|
writeJSONOr(w, list, err)
|
|
}
|
|
|
|
func (s *Server) createSecret(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Service string `json:"service"`
|
|
Label string `json:"label"`
|
|
Value string `json:"value"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if strings.TrimSpace(req.Service) == "" || strings.TrimSpace(req.Value) == "" {
|
|
http.Error(w, "store: secret service and value are required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
sec, err := s.st.AddSecretAccountChecked(s.owner(r), req.Service, req.Label, req.Value, s.Auth == nil || !s.Auth.Enabled)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, sec)
|
|
}
|
|
|
|
func (s *Server) activateSecret(w http.ResponseWriter, r *http.Request) {
|
|
writeOKOr(w, s.st.ActivateSecretAccountChecked(s.owner(r), r.PathValue("id"), s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) renameSecret(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Label string `json:"label"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
writeOKOr(w, s.st.RenameSecretAccountChecked(s.owner(r), r.PathValue("id"), req.Label, s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) deleteSecret(w http.ResponseWriter, r *http.Request) {
|
|
writeOKOr(w, s.st.DeleteSecretAccountChecked(s.owner(r), r.PathValue("id"), s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) listContextEntries(w http.ResponseWriter, r *http.Request) {
|
|
q := r.URL.Query()
|
|
list, err := s.st.ListContextEntries(s.owner(r), q.Get("projectId"), q.Get("conversationId"))
|
|
writeJSONOr(w, list, err)
|
|
}
|
|
|
|
func (s *Server) saveContextEntry(w http.ResponseWriter, r *http.Request) {
|
|
var e store.ContextEntry
|
|
if err := json.NewDecoder(r.Body).Decode(&e); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if strings.TrimSpace(e.Content) == "" {
|
|
http.Error(w, "store: entry content required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
saved, err := s.st.SaveContextEntryAccountChecked(s.owner(r), e, s.Auth == nil || !s.Auth.Enabled)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, saved)
|
|
}
|
|
|
|
func (s *Server) deleteContextEntry(w http.ResponseWriter, r *http.Request) {
|
|
writeOKOr(w, s.st.DeleteContextEntryAccountChecked(s.owner(r), r.PathValue("id"), s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) listUserPromptCommands(w http.ResponseWriter, r *http.Request) {
|
|
list, err := s.st.ListUserCommands(s.owner(r))
|
|
writeJSONOr(w, list, err)
|
|
}
|
|
|
|
func (s *Server) saveUserPromptCommand(w http.ResponseWriter, r *http.Request) {
|
|
var c store.UserCommand
|
|
if err := json.NewDecoder(r.Body).Decode(&c); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
if strings.TrimSpace(c.Template) == "" {
|
|
http.Error(w, "store: command template required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
owner := s.owner(r)
|
|
allowImplicitDefault := s.Auth == nil || !s.Auth.Enabled
|
|
saved, err := s.st.SaveUserCommandAccountChecked(owner, c, allowImplicitDefault)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if s.Bus != nil {
|
|
// The Bus is outside the database transaction. Recheck immediately before
|
|
// mutation; a disable between this check and RegisterUser remains possible.
|
|
if err := s.st.CheckEffectiveAccountForExternalEffect(owner, allowImplicitDefault); err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
s.registerUserPrompt(saved)
|
|
}
|
|
writeJSON(w, http.StatusOK, saved)
|
|
}
|
|
|
|
func (s *Server) deleteUserPromptCommand(w http.ResponseWriter, r *http.Request) {
|
|
owner := s.owner(r)
|
|
id := r.PathValue("id")
|
|
allowImplicitDefault := s.Auth == nil || !s.Auth.Enabled
|
|
deleted, err := s.st.DeleteUserCommandAccountChecked(owner, id, allowImplicitDefault)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if s.Bus != nil && deleted.Name != "" {
|
|
// The account check is the last database action before the Bus mutation;
|
|
// the two systems cannot share an atomic commit.
|
|
if err := s.st.CheckEffectiveAccountForExternalEffect(owner, allowImplicitDefault); err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
s.Bus.UnregisterUser(userCommandID(deleted.Name), deleted.Name)
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"deleted": id})
|
|
}
|
|
|
|
func (s *Server) excludeMessage(w http.ResponseWriter, r *http.Request) {
|
|
var req struct {
|
|
Excluded *bool `json:"excluded"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil {
|
|
http.Error(w, err.Error(), http.StatusBadRequest)
|
|
return
|
|
}
|
|
excluded := true
|
|
if req.Excluded != nil {
|
|
excluded = *req.Excluded
|
|
}
|
|
msg, err := s.st.MessageByID(r.PathValue("id"))
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if _, err := s.st.GetConversation(s.owner(r), msg.ConversationID); err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
writeOKOr(w, s.st.ExcludeRunGroupAccountChecked(s.owner(r), msg.ID, excluded, s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) selectVariant(w http.ResponseWriter, r *http.Request) {
|
|
msg, err := s.st.MessageByID(r.PathValue("id"))
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if _, err := s.st.GetConversation(s.owner(r), msg.ConversationID); err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
writeOKOr(w, s.st.SelectVariantAccountChecked(s.owner(r), msg.ID, s.Auth == nil || !s.Auth.Enabled))
|
|
}
|
|
|
|
func (s *Server) regenerateMessage(w http.ResponseWriter, r *http.Request) {
|
|
owner := s.owner(r)
|
|
convID := r.PathValue("id")
|
|
conv, err := s.st.GetConversation(owner, convID)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if err := s.projectWork(r.Context(), owner, conv.ProjectID); err != nil {
|
|
writeProjectErr(w, err)
|
|
return
|
|
}
|
|
var req struct {
|
|
MessageID string `json:"messageId"`
|
|
}
|
|
if err := json.NewDecoder(r.Body).Decode(&req); err != nil || req.MessageID == "" {
|
|
http.Error(w, "messageId required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
target, err := s.st.MessageByID(req.MessageID)
|
|
if err != nil || target.ConversationID != convID || target.Role != core.RoleAssistant {
|
|
http.Error(w, "assistant message required", http.StatusBadRequest)
|
|
return
|
|
}
|
|
hist, err := s.st.ListMessages(convID)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
var user core.Message
|
|
found := false
|
|
for _, m := range hist {
|
|
if m.ID == target.ID {
|
|
found = true
|
|
}
|
|
if !found && m.Role == core.RoleUser {
|
|
user = m
|
|
}
|
|
}
|
|
if user.ID == "" {
|
|
http.Error(w, "no user turn to regenerate", http.StatusBadRequest)
|
|
return
|
|
}
|
|
group, index, err := s.st.PrepareRegenerationAccountChecked(owner, convID, target.ID, s.Auth == nil || !s.Auth.Enabled)
|
|
if err != nil {
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
if _, err := s.mgr.RewindFiles(owner, convID, user.ID); err != nil {
|
|
// 没有文件检查点时仍然重新生成回复。
|
|
}
|
|
run, err := s.mgr.StartConversationRun(conv, user, nil, nil)
|
|
if err != nil {
|
|
_ = s.st.ClearRegenPendingAfterFailedStart(owner, convID, group, index, s.Auth == nil || !s.Auth.Enabled)
|
|
writeStoreErr(w, err)
|
|
return
|
|
}
|
|
writeJSON(w, http.StatusOK, map[string]any{"run": run, "variantGroup": group})
|
|
}
|
|
|
|
func (s *Server) expandUserCommand(owner, text string) (string, bool) {
|
|
fields := strings.Fields(text)
|
|
if len(fields) == 0 || !strings.HasPrefix(fields[0], "/") {
|
|
return "", false
|
|
}
|
|
list, err := s.st.ListUserCommands(owner)
|
|
if err != nil {
|
|
return "", false
|
|
}
|
|
name := strings.ToLower(fields[0])
|
|
rest := strings.TrimSpace(strings.TrimPrefix(text, fields[0]))
|
|
for _, c := range list {
|
|
if strings.ToLower(c.Name) != name {
|
|
continue
|
|
}
|
|
body := c.Template
|
|
if strings.Contains(body, "{{input}}") {
|
|
body = strings.ReplaceAll(body, "{{input}}", rest)
|
|
} else if rest != "" {
|
|
body = strings.TrimSpace(body + "\n" + rest)
|
|
}
|
|
return body, true
|
|
}
|
|
return "", false
|
|
}
|
|
|
|
func userCommandID(name string) string {
|
|
slug := strings.TrimPrefix(strings.ToLower(name), "/")
|
|
return "user." + slug
|
|
}
|
|
|
|
func (s *Server) registerUserPrompt(c store.UserCommand) {
|
|
if s.Bus == nil {
|
|
return
|
|
}
|
|
slug := strings.TrimPrefix(strings.ToLower(c.Name), "/")
|
|
if !userCommandSlug.MatchString(slug) {
|
|
return
|
|
}
|
|
_ = s.Bus.RegisterUser(agentcommand.CommandSpec{
|
|
ID: userCommandID(c.Name), Title: c.Title, Description: "展开为提示词,不自动执行",
|
|
}, c.Name)
|
|
}
|
|
|
|
func (s *Server) appendUserPromptCommands(owner string, out []map[string]any) []map[string]any {
|
|
list, err := s.st.ListUserCommands(owner)
|
|
if err != nil {
|
|
return out
|
|
}
|
|
for _, c := range list {
|
|
if s.Bus != nil {
|
|
if err := s.st.CheckEffectiveAccountForExternalEffect(owner, s.Auth == nil || !s.Auth.Enabled); err != nil {
|
|
return out
|
|
}
|
|
}
|
|
s.registerUserPrompt(c)
|
|
out = append(out, map[string]any{
|
|
"id": userCommandID(c.Name), "name": c.Name, "title": c.Title,
|
|
"description": "展开为提示词", "source": "user", "enabled": true,
|
|
})
|
|
}
|
|
return out
|
|
}
|