2026-09-09 09:46:36 +00:00
|
|
|
package session
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/event"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types/interfaces"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
// capturingStreamManager records every appended event so a handler's output
|
|
|
|
|
// can be asserted without a real stream backend. Embedding the interface keeps
|
|
|
|
|
// everything else nil-panicky so an un-stubbed call fails loudly.
|
|
|
|
|
type capturingStreamManager struct {
|
|
|
|
|
interfaces.StreamManager
|
|
|
|
|
events []interfaces.StreamEvent
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (s *capturingStreamManager) AppendEvent(
|
|
|
|
|
_ context.Context, _, _ string, evt interfaces.StreamEvent,
|
|
|
|
|
) error {
|
|
|
|
|
s.events = append(s.events, evt)
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// A failed tool execution is still a tool result: the SSE event must keep
|
|
|
|
|
// response_type=tool_result with success=false in its metadata, because
|
|
|
|
|
// response_type=error is reserved for internal agent failures (handleError).
|
|
|
|
|
func TestAgentStreamHandlerToolResultFailureKeepsToolResultType(t *testing.T) {
|
|
|
|
|
streams := &capturingStreamManager{}
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(), "sess-1", "msg-1", "req-1", 1, time.Now(),
|
|
|
|
|
&types.Message{}, streams, nil, nil, nil, nil,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err := h.handleToolResult(context.Background(), event.Event{
|
|
|
|
|
ID: "evt-1", Type: event.EventAgentToolResult,
|
|
|
|
|
Data: event.AgentToolResultData{
|
|
|
|
|
ToolName: "shell_exec",
|
|
|
|
|
ToolCallID: "call-1",
|
|
|
|
|
Success: false,
|
|
|
|
|
Error: "exit status 1",
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, streams.events, 1)
|
|
|
|
|
|
|
|
|
|
evt := streams.events[0]
|
|
|
|
|
require.Equal(t, types.ResponseTypeToolResult, evt.Type)
|
|
|
|
|
require.Equal(t, false, evt.Data["success"])
|
|
|
|
|
require.Equal(t, "exit status 1", evt.Data["error"])
|
|
|
|
|
require.Equal(t, "call-1", evt.Data["tool_call_id"])
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Internal error events keep response_type=error with done=true, so clients
|
|
|
|
|
// can still distinguish a crashed run from a failed tool call.
|
|
|
|
|
func TestAgentStreamHandlerInternalErrorKeepsErrorType(t *testing.T) {
|
|
|
|
|
streams := &capturingStreamManager{}
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(), "sess-1", "msg-1", "req-1", 1, time.Now(),
|
|
|
|
|
&types.Message{}, streams, nil, nil, nil, nil,
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
err := h.handleError(context.Background(), event.Event{
|
|
|
|
|
ID: "evt-err", Type: event.EventError,
|
|
|
|
|
Data: event.ErrorData{Stage: "think", Error: "llm unavailable"},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, streams.events, 1)
|
|
|
|
|
|
|
|
|
|
evt := streams.events[0]
|
|
|
|
|
require.Equal(t, types.ResponseTypeError, evt.Type)
|
|
|
|
|
require.True(t, evt.Done)
|
|
|
|
|
}
|
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
|
|
|
|
|
|
|
|
func TestAgentStreamHandlerForwardsOperationDataChange(t *testing.T) {
|
|
|
|
|
streams := &capturingStreamManager{}
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(), "sess-1", "msg-1", "req-1", 1, time.Now(),
|
|
|
|
|
&types.Message{}, streams, nil, nil, nil, nil,
|
|
|
|
|
)
|
|
|
|
|
marker := map[string]any{"operation": "agent.delete", "area": "agent"}
|
|
|
|
|
err := h.handleToolResult(context.Background(), event.Event{
|
|
|
|
|
Type: event.EventAgentToolResult,
|
|
|
|
|
Data: event.AgentToolResultData{
|
|
|
|
|
ToolName: "run_operation", Success: true, Data: map[string]any{"data_change": marker},
|
|
|
|
|
},
|
|
|
|
|
})
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Len(t, streams.events, 1)
|
|
|
|
|
require.Equal(t, types.ResponseTypeToolResult, streams.events[0].Type)
|
|
|
|
|
require.Equal(t, marker, streams.events[0].Data["data_change"])
|
|
|
|
|
}
|
2026-10-19 04:14:15 +00:00
|
|
|
|
|
|
|
|
func TestAgentStreamHandlerProgressIsSeparateFromReasoningAndFinalAnswer(t *testing.T) {
|
|
|
|
|
streams := &capturingStreamManager{}
|
|
|
|
|
bus := event.NewEventBus()
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(),
|
|
|
|
|
"s",
|
|
|
|
|
"m",
|
|
|
|
|
"r",
|
|
|
|
|
1,
|
|
|
|
|
time.Now(),
|
|
|
|
|
&types.Message{},
|
|
|
|
|
streams,
|
|
|
|
|
bus,
|
|
|
|
|
nil,
|
|
|
|
|
nil,
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
h.Subscribe()
|
|
|
|
|
require.NoError(
|
|
|
|
|
t,
|
|
|
|
|
bus.Emit(
|
|
|
|
|
t.Context(),
|
|
|
|
|
event.Event{
|
|
|
|
|
ID: "progress-1",
|
|
|
|
|
Type: event.EventAgentProgress,
|
|
|
|
|
Data: event.AgentProgressData{Content: "Two tasks finished; one awaits approval.", Iteration: 3},
|
|
|
|
|
},
|
|
|
|
|
),
|
|
|
|
|
)
|
|
|
|
|
require.Len(t, streams.events, 1)
|
|
|
|
|
e := streams.events[0]
|
|
|
|
|
require.Equal(t, types.ResponseTypeProgress, e.Type)
|
|
|
|
|
require.Equal(t, "progress-1", e.Data["event_id"])
|
|
|
|
|
require.Equal(t, 3, e.Data["iteration"])
|
|
|
|
|
require.True(t, e.Done)
|
|
|
|
|
require.Empty(t, h.finalAnswer)
|
|
|
|
|
require.Empty(t, h.answerSegments)
|
|
|
|
|
require.Empty(t, h.eventStartTimes)
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 06:47:43 +00:00
|
|
|
func TestPageButtonKeepsTheAnswerBeforeIt(t *testing.T) {
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(), "s", "m", "r", 1, time.Now(), &types.Message{},
|
|
|
|
|
&capturingStreamManager{}, nil, nil, nil, nil,
|
|
|
|
|
)
|
|
|
|
|
ctx := context.Background()
|
|
|
|
|
answer := func(id, content string) {
|
|
|
|
|
require.NoError(t, h.handleFinalAnswer(ctx, event.Event{
|
|
|
|
|
ID: id, Type: event.EventAgentFinalAnswer, Data: event.AgentFinalAnswerData{Content: content},
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
call := func(name string, args map[string]any) {
|
|
|
|
|
require.NoError(t, h.handleToolCall(ctx, event.Event{
|
|
|
|
|
ID: "call", Type: event.EventAgentToolCall,
|
|
|
|
|
Data: event.AgentToolCallData{ToolCallID: "c-" + name, ToolName: name, Arguments: args},
|
|
|
|
|
}))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
answer("a1", "the report")
|
|
|
|
|
call("open_page", nil)
|
|
|
|
|
call("open_page", map[string]any{"page": "agents"})
|
|
|
|
|
require.Equal(t, "the report", h.finalAnswer, "a page button follows the answer")
|
|
|
|
|
|
|
|
|
|
call("view_page", map[string]any{"page": "settings"})
|
|
|
|
|
require.Empty(t, h.finalAnswer, "viewing a page is more work, so the text was a preamble")
|
|
|
|
|
|
|
|
|
|
answer("a2", "second report")
|
|
|
|
|
require.NoError(t, h.handleProgress(ctx, event.Event{
|
|
|
|
|
ID: "a2", Type: event.EventAgentProgress, Data: event.AgentProgressData{Content: "second report"},
|
|
|
|
|
}))
|
|
|
|
|
require.Empty(t, h.finalAnswer, "progress with the answer's id retracts it")
|
|
|
|
|
}
|
|
|
|
|
|
2026-10-19 04:14:15 +00:00
|
|
|
func TestProtocolAnswerReplacementOnlyResetsMatchingSegment(t *testing.T) {
|
|
|
|
|
streams := &capturingStreamManager{}
|
|
|
|
|
h := NewAgentStreamHandler(
|
|
|
|
|
context.Background(),
|
|
|
|
|
"s",
|
|
|
|
|
"m",
|
|
|
|
|
"r",
|
|
|
|
|
1,
|
|
|
|
|
time.Now(),
|
|
|
|
|
&types.Message{},
|
|
|
|
|
streams,
|
|
|
|
|
nil,
|
|
|
|
|
nil,
|
|
|
|
|
nil,
|
|
|
|
|
nil,
|
|
|
|
|
)
|
|
|
|
|
send := func(id, content string, replace bool) {
|
|
|
|
|
require.NoError(
|
|
|
|
|
t,
|
|
|
|
|
h.handleFinalAnswer(context.Background(), event.Event{
|
|
|
|
|
ID: id, Type: event.EventAgentFinalAnswer,
|
|
|
|
|
Data: event.AgentFinalAnswerData{Content: content, Replace: replace},
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
}
|
|
|
|
|
send("prior", "Earlier legitimate answer.", false)
|
|
|
|
|
send("current", "refs\nprivate prefix", false)
|
|
|
|
|
send("current", "", true)
|
|
|
|
|
send("current", "## 进展\ncomplete report", false)
|
|
|
|
|
require.Equal(t, "Earlier legitimate answer.", h.findAnswerSegment("prior").content)
|
|
|
|
|
require.Equal(t, "## 进展\ncomplete report", h.findAnswerSegment("current").content)
|
|
|
|
|
require.NotContains(t, h.finalAnswer, "private prefix")
|
|
|
|
|
require.Equal(t, true, streams.events[2].Data["replace"])
|
|
|
|
|
require.Equal(t, "current", streams.events[2].Data["event_id"])
|
|
|
|
|
}
|