WeKnora/internal/handler/custom_agent_patch_test.go

278 lines
11 KiB
Go
Raw Normal View History

feat(assistant): manage agents and skills, separate history, refresh pages (#4344) * feat(assistant): let the assistant manage agents Expose custom agents to the operation catalog (list, get, create, copy, update, delete) plus read-only model and MCP service lookups, so the assistant can set up agents end to end. - Add PATCH /api/v1/agents/:id. PUT replaces the whole config, so a one-field change from the assistant would wipe the rest; PATCH only overlays the fields it carries and reuses the PUT validation path. - Add x-agent-detail so long fields such as the system prompt are only projected for single-object reads, not lists. - Add label_param so a create without a target is labelled by its name. - The approval card lists agent setting changes and resolves model, knowledge base and MCP IDs to names. - Deny sharing, publishing channels, credentials and OAuth routes; model and MCP endpoints, headers and secrets are never projected. - Update the assistant prompt, starters, API docs and Go SDK. * feat(assistant): list skills and configure agent skill scope - Expose usable skill names for an agent sandbox without exposing skill administration or connection settings to the model. - Validate PATCH selections against the workspace sandbox usable set so invented names and missing sandbox selections cannot be saved. - Show skill changes in approvals and align the assistant prompt, API documentation, and Go SDK with focused refusal and projection tests. * feat(assistant): isolate conversation history - Keep assistant conversations out of ordinary session lists, search, artifacts and delete-all cleanup. - Add private, paginated assistant history with session switching, confirmed deletion and stale-session recovery. - Backfill session kinds safely on Postgres and SQLite, preserve kinds on forks, and align API docs and SDK coverage. * feat(assistant): refresh pages after successful data changes - Carry decoded mutation targets through live tool-result events without exposing refresh metadata to the model or persisted chat history. - Coalesce page and selector refreshes, update document drawers, and warn about changed agents while preserving open editor drafts. - Cover mutation, approval, stream, lifecycle and cache behavior with Go and Node tests using the existing frontend runner and component fixtures. * feat(assistant): add agent page context and resource scope limits - Attach verified agent context and persist personal scope across turns and approvals. - Enforce resource boundaries before operation and Wiki approvals, including edited arguments. - Add scope controls, translations, API and SDK support, and regression coverage. * refactor(assistant): remove the scope limit The scope picker confused users and duplicated what RBAC and the access level already enforce. Keep the agent page context: the agent editor still publishes agent_id and the backend still verifies it. * feat(assistant): render operation and product-doc results Show find/run operations and WeKnora docs as structured cards instead of raw JSON. * feat(assistant): default new agents to smart-reasoning Quick-answer cannot call MCP, and agent.update could not change the mode. New agents now start in smart-reasoning, and a patch can switch agent_mode. * fix: resolve assistant management CI failures
2026-10-15 13:43:20 +00:00
package handler
import (
"context"
"encoding/json"
"net/http"
"net/http/httptest"
"strings"
"testing"
"github.com/Tencent/WeKnora/internal/application/service"
"github.com/Tencent/WeKnora/internal/middleware"
"github.com/Tencent/WeKnora/internal/sandbox"
"github.com/Tencent/WeKnora/internal/types"
"github.com/gin-gonic/gin"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func patchTestAgent() *types.CustomAgent {
return &types.CustomAgent{
ID: "custom-1", Name: "Support", Description: "Answers tickets", Avatar: "🤖", TenantID: 1,
Config: types.CustomAgentConfig{
AgentMode: types.AgentModeQuickAnswer,
SystemPrompt: "Be brief.",
ModelID: "model-1",
KBSelectionMode: "selected",
KnowledgeBases: []string{"kb-1"},
MCPSelectionMode: "none",
Temperature: 0.3,
WebSearchEnabled: true,
},
}
}
func servePatch(t *testing.T, repo *agentAvatarRepository, body string) *httptest.ResponseRecorder {
t.Helper()
return servePatchWithSkills(t, repo, body, nil)
}
func servePatchWithSkills(
t *testing.T, repo *agentAvatarRepository, body string, skills usableSkillLister,
) *httptest.ResponseRecorder {
t.Helper()
return servePatchWith(t, repo, body, &CustomAgentHandler{usableSkills: skills})
}
func servePatchWith(
t *testing.T, repo *agentAvatarRepository, body string, h *CustomAgentHandler,
) *httptest.ResponseRecorder {
t.Helper()
gin.SetMode(gin.TestMode)
h.service = service.NewCustomAgentService(repo, nil, nil, nil, nil, nil, nil, nil)
router := gin.New()
router.Use(middleware.ErrorHandler())
router.PATCH("/agents/:id", h.PatchAgent)
req := httptest.NewRequest(http.MethodPatch, "/agents/custom-1", strings.NewReader(body))
req.Header.Set("Content-Type", "application/json")
req = req.WithContext(context.WithValue(req.Context(), types.TenantIDContextKey, uint64(1)))
rec := httptest.NewRecorder()
router.ServeHTTP(rec, req)
return rec
}
func TestPatchAgentKeepsFieldsItDoesNotCarry(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
rec := servePatch(t, repo, `{"name":"Helpdesk","model_id":"model-2"}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
require.Equal(t, 1, repo.writes)
got := repo.agent
assert.Equal(t, "Helpdesk", got.Name)
assert.Equal(t, "model-2", got.Config.ModelID)
assert.Equal(t, "Answers tickets", got.Description)
assert.Equal(t, "🤖", got.Avatar)
assert.Equal(t, "Be brief.", got.Config.SystemPrompt)
assert.Equal(t, []string{"kb-1"}, got.Config.KnowledgeBases)
assert.Equal(t, 0.3, got.Config.Temperature)
assert.True(t, got.Config.WebSearchEnabled)
assert.Equal(t, types.AgentModeQuickAnswer, got.Config.AgentMode)
}
func TestPatchAgentSelectionLists(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
repo.agent.Config.KBSelectionMode = "all"
rec := servePatch(t, repo,
`{"knowledge_bases":["kb-2","kb-3"],"mcp_services":["mcp-1"],"web_search_enabled":false}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
cfg := repo.agent.Config
assert.Equal(t, "selected", cfg.KBSelectionMode, "a list alone switches the scope to selected")
assert.Equal(t, []string{"kb-2", "kb-3"}, cfg.KnowledgeBases)
assert.Equal(t, "selected", cfg.MCPSelectionMode)
assert.Equal(t, []string{"mcp-1"}, cfg.MCPServices)
assert.False(t, cfg.WebSearchEnabled)
rec = servePatch(t, repo, `{"knowledge_bases":[],"kb_selection_mode":"none"}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, "none", repo.agent.Config.KBSelectionMode, "an explicit mode wins")
assert.Empty(t, repo.agent.Config.KnowledgeBases)
}
func TestPatchAgentRejectsBadInput(t *testing.T) {
for name, body := range map[string]string{
"unknown mode": `{"kb_selection_mode":"some"}`,
"unknown agent": `{"agent_mode":"agent"}`,
"blank name": `{"name":" "}`,
"not json": `{`,
} {
t.Run(name, func(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
rec := servePatch(t, repo, body)
assert.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
assert.Zero(t, repo.writes)
})
}
}
func TestPatchAgentMode(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
rec := servePatch(t, repo, `{"agent_mode":" smart-reasoning "}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, types.AgentModeSmartReasoning, repo.agent.Config.AgentMode)
assert.True(t, repo.agent.Config.MultiTurnEnabled, "smart-reasoning keeps earlier turns")
assert.Equal(t, "Be brief.", repo.agent.Config.SystemPrompt)
assert.Equal(t, "Support", repo.agent.Name)
rec = servePatch(t, repo, `{"agent_mode":"quick-answer"}`)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, types.AgentModeQuickAnswer, repo.agent.Config.AgentMode)
assert.True(t, repo.agent.Config.MultiTurnEnabled, "leaving smart-reasoning does not turn multi-turn off")
}
func TestPatchAgentNotFound(t *testing.T) {
repo := &agentAvatarRepository{err: service.ErrAgentNotFound}
rec := servePatch(t, repo, `{"name":"x"}`)
assert.Equal(t, http.StatusNotFound, rec.Code, rec.Body.String())
}
func TestPatchAgentSkills(t *testing.T) {
for _, tc := range []struct {
name, body, mode string
skills []string
}{
{"names alone", `{"selected_skills":["ppt-generator"]}`, "selected", []string{"ppt-generator"}},
{"explicit selected", `{"skills_selection_mode":"selected"}`, "selected", []string{"ppt-generator"}},
{"empty selection", `{"selected_skills":[]}`, "selected", []string{}},
{"all clears names", `{"skills_selection_mode":"all"}`, "all", []string{}},
{"none clears names", `{"skills_selection_mode":"none"}`, "none", []string{}},
{"explicit all wins", `{"skills_selection_mode":"all","selected_skills":["unknown"]}`, "all", []string{}},
{"explicit none wins", `{"skills_selection_mode":"none","selected_skills":["unknown"]}`, "none", []string{}},
} {
t.Run(tc.name, func(t *testing.T) {
agent := patchTestAgent()
agent.Config.SandboxConfigID = "cfg-1"
agent.Config.SkillsSelectionMode = "none"
agent.Config.SelectedSkills = []string{"ppt-generator"}
agent.EnsureDefaults()
want := agent.Config
want.SkillsSelectionMode, want.SelectedSkills = tc.mode, tc.skills
repo := &agentAvatarRepository{agent: agent}
lister := &fakeUsableSkillLister{skills: []*types.TenantSkillEntity{nil, {Name: "ppt-generator"}}}
rec := servePatchWithSkills(t, repo, tc.body, lister)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
require.Equal(t, 1, repo.writes)
assert.Equal(t, want, repo.agent.Config, "unmentioned config fields must be kept")
assert.Equal(t, "Support", repo.agent.Name)
assert.Equal(t, "Answers tickets", repo.agent.Description)
assert.Equal(t, "🤖", repo.agent.Avatar)
if tc.mode == "selected" && len(tc.skills) > 0 {
assert.Equal(t, uint64(1), lister.tenantID)
assert.Equal(t, "cfg-1", lister.configID)
} else {
assert.Empty(t, lister.configID)
}
})
}
}
func TestPatchAgentRejectsUnavailableSkills(t *testing.T) {
for _, body := range []string{
`{"selected_skills":["ppt-generator","unknown","PPT-generator"]}`,
`{"skills_selection_mode":"selected"}`,
} {
t.Run(body, func(t *testing.T) {
agent := patchTestAgent()
agent.Config.SandboxConfigID = "cfg-1"
agent.Config.SkillsSelectionMode = "selected"
agent.Config.SelectedSkills = []string{"unknown", "PPT-generator"}
repo := &agentAvatarRepository{agent: agent}
lister := &fakeUsableSkillLister{skills: []*types.TenantSkillEntity{{Name: "ppt-generator"}}}
rec := servePatchWithSkills(t, repo, body, lister)
require.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
assert.Contains(t, rec.Body.String(), "Unknown or unavailable skills: unknown, PPT-generator")
assert.Zero(t, repo.writes)
assert.Equal(t, "Support", repo.agent.Name)
assert.Equal(t, []string{"unknown", "PPT-generator"}, repo.agent.Config.SelectedSkills)
})
}
}
func TestPatchAgentKeepsSavedSkillsOnUnrelatedChange(t *testing.T) {
agent := patchTestAgent()
agent.Config.SandboxConfigID = "cfg-1"
agent.Config.SkillsSelectionMode = "selected"
agent.Config.SelectedSkills = []string{"uninstalled"}
repo := &agentAvatarRepository{agent: agent}
lister := &fakeUsableSkillLister{}
rec := servePatchWithSkills(t, repo, `{"name":"Renamed"}`, lister)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, "Renamed", repo.agent.Name)
assert.Equal(t, []string{"uninstalled"}, repo.agent.Config.SelectedSkills)
assert.Zero(t, lister.tenantID, "saved skills are not re-checked")
}
func TestPatchAgentSkillsOnLiteUseHostTarget(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
lister := &fakeUsableSkillLister{skills: []*types.TenantSkillEntity{{Name: "ppt-generator"}}}
h := &CustomAgentHandler{usableSkills: lister, desktop: true}
rec := servePatchWith(t, repo, `{"selected_skills":["ppt-generator"]}`, h)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
assert.Equal(t, sandbox.HostSkillTargetID, lister.configID)
assert.Empty(t, repo.agent.Config.SandboxConfigID)
assert.Equal(t, []string{"ppt-generator"}, repo.agent.Config.SelectedSkills)
}
func TestPatchAgentSkillsRequireSandbox(t *testing.T) {
for _, body := range []string{
`{"skills_selection_mode":"all"}`,
`{"skills_selection_mode":"selected"}`,
`{"selected_skills":["ppt-generator"]}`,
`{"selected_skills":[]}`,
} {
t.Run(body, func(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
lister := &fakeUsableSkillLister{}
rec := servePatchWithSkills(t, repo, body, lister)
require.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
assert.Contains(t, rec.Body.String(), "Choose a sandbox in the agent editor")
assert.Zero(t, repo.writes)
assert.Zero(t, lister.tenantID)
})
}
for _, body := range []string{`{"skills_selection_mode":"none"}`, `{"name":"Renamed"}`} {
repo := &agentAvatarRepository{agent: patchTestAgent()}
repo.agent.Config.SkillsSelectionMode = "all"
rec := servePatch(t, repo, body)
require.Equal(t, http.StatusOK, rec.Code, rec.Body.String())
}
}
func TestPatchAgentRejectsInvalidSkillsInput(t *testing.T) {
tooMany, err := json.Marshal(map[string]any{"selected_skills": make([]string, 51)})
require.NoError(t, err)
for _, body := range []string{
`{"skills_selection_mode":"some"}`, `{"skills_selection_mode":""}`,
`{"selected_skills":"ppt-generator"}`, string(tooMany),
} {
t.Run(body, func(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
repo.agent.Config.SandboxConfigID = "cfg-1"
rec := servePatch(t, repo, body)
assert.Equal(t, http.StatusBadRequest, rec.Code, rec.Body.String())
assert.Zero(t, repo.writes)
})
}
}
func TestPatchAgentSkillsFailClosedWithoutLister(t *testing.T) {
repo := &agentAvatarRepository{agent: patchTestAgent()}
repo.agent.Config.SandboxConfigID = "cfg-1"
rec := servePatch(t, repo, `{"selected_skills":["ppt-generator"]}`)
assert.Equal(t, http.StatusInternalServerError, rec.Code, rec.Body.String())
assert.Zero(t, repo.writes)
}