2026-08-25 03:28:12 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"context"
|
|
|
|
|
"encoding/json"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/gin-gonic/gin"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/middleware"
|
|
|
|
|
"github.com/Tencent/WeKnora/internal/types"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
type fakeUsableSkillLister struct {
|
|
|
|
|
tenantID uint64
|
|
|
|
|
configID string
|
|
|
|
|
skills []*types.TenantSkillEntity
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (f *fakeUsableSkillLister) ListUsableSkills(
|
|
|
|
|
_ context.Context, tenantID uint64, configID string,
|
|
|
|
|
) []*types.TenantSkillEntity {
|
|
|
|
|
f.tenantID = tenantID
|
|
|
|
|
f.configID = configID
|
|
|
|
|
if configID == "" {
|
|
|
|
|
return nil
|
|
|
|
|
}
|
|
|
|
|
return f.skills
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func newChatSkillRouter(h *SkillHandler) *gin.Engine {
|
|
|
|
|
gin.SetMode(gin.TestMode)
|
|
|
|
|
r := gin.New()
|
|
|
|
|
r.Use(middleware.ErrorHandler())
|
|
|
|
|
r.Use(func(c *gin.Context) {
|
|
|
|
|
c.Set(types.TenantIDContextKey.String(), testSkillTenantID)
|
|
|
|
|
c.Next()
|
|
|
|
|
})
|
|
|
|
|
r.GET("/skills", h.ListSkills)
|
|
|
|
|
return r
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListSkillsHidesThePickerWhenNoSandboxConfigIsSelected(t *testing.T) {
|
|
|
|
|
lister := &fakeUsableSkillLister{
|
|
|
|
|
skills: []*types.TenantSkillEntity{{Name: "ppt-generator", Description: "make ppt"}},
|
|
|
|
|
}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newChatSkillRouter(NewSkillHandler(lister, nil, nil))
|
2026-08-25 03:28:12 +00:00
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, httptest.NewRequest(http.MethodGet, "/skills", nil))
|
|
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
var body struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Data []SkillInfoResponse
|
|
|
|
|
SkillsAvailable bool `json:"skills_available"`
|
|
|
|
|
}
|
|
|
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
|
|
|
require.True(t, body.Success)
|
2026-09-05 08:45:59 +00:00
|
|
|
require.Empty(t, body.Data, "unscoped skills must not appear in @")
|
2026-08-25 03:28:12 +00:00
|
|
|
require.False(t, body.SkillsAvailable)
|
|
|
|
|
require.Empty(t, lister.configID)
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestListSkillsReturnsUsableInstalledSkillsForTheSelectedConfig(t *testing.T) {
|
|
|
|
|
lister := &fakeUsableSkillLister{
|
|
|
|
|
skills: []*types.TenantSkillEntity{
|
|
|
|
|
{Name: "ppt-generator", Description: "make ppt"},
|
|
|
|
|
},
|
|
|
|
|
}
|
fix(agent): scope the @Skill and @MCP pickers to the agent that will run (#3475)
* fix(agent): scope the @Skill and @MCP pickers to the agent that will run
Both pickers listed the caller's own workspace, but a shared agent invokes its
OWNER's skills and MCP services. The two never line up, so for every shared
agent:
* @Skill was always empty, in every skills mode. GET /skills resolves the
sandbox config with (tenant, id), and the agent's config id does not exist
in the borrower's workspace - effectiveTenantSkills short-circuits before
it reads a single skill row. The response still reported
skills_available: true, so the category appeared with nothing under it.
* @MCP in "selected" mode was empty for the same reason: the preset holds the
owner's service ids, which match nothing in the borrower's catalog.
* @MCP in "all" mode was worse than empty. isMCPAllowedByAgent returns true
for every enabled service, so the picker offered the BORROWER's own
services; resolvePerRequestMCPScope then intersected the mention against
the agent's preset, found nothing, and dropped it with only a warning.
Resolve the agent when the request carries agent_id + agent_source_tenant_id
and read its resources where they live. Only an explicit source workspace takes
the share path, so an own-agent request is byte-for-byte unchanged; built-in
agent ids exist in every workspace, and falling back to a share lookup without
one would let an org member take over the caller's default agent, exactly as
session.Handler.resolveAgent already documents.
Both endpoints serve only what the agent can actually invoke:
* skills: the sandbox config comes from the agent, never from the query, so a
borrower cannot name another config in the owner's workspace and enumerate
it; "selected" narrows to the configured names and a disabled picker never
reaches the owner's inventory at all.
* MCP: the agent's explicit preset and nothing else, because that is exactly
what resolvePerRequestMCPScope accepts for a shared agent. "all" therefore
offers nothing rather than offering services every mention would drop.
Whether "all" should become mentionable is a separate decision about that
guard, not about the picker.
The cross-workspace MCP rows use a narrowed response built field by field.
NewMCPServiceResponse reveals endpoint URL, headers, env vars and stdio command
to an Admin, and a borrower can be an Admin of their own workspace - that shape
must be narrow by construction, not by role.
On the client the two lists now follow the selected agent: their cache keys
carry it, and the chat input refetches on a switch behind a request token, so a
slow response for the agent the user just left cannot overwrite the picker with
another workspace's services.
* fix(agent): check the c.Error return on the shared-agent picker paths
golangci-lint judges pull requests on the lines they introduce, so the
repository's older bare c.Error calls are grandfathered while these three are
not. Match the _ = c.Error form already used in mcp_service.go.
2026-09-21 05:02:34 +00:00
|
|
|
router := newChatSkillRouter(NewSkillHandler(lister, nil, nil))
|
2026-08-25 03:28:12 +00:00
|
|
|
|
|
|
|
|
w := httptest.NewRecorder()
|
|
|
|
|
router.ServeHTTP(w, httptest.NewRequest(
|
|
|
|
|
http.MethodGet, "/skills?sandbox_config_id=cfg-1", nil,
|
|
|
|
|
))
|
|
|
|
|
|
|
|
|
|
require.Equal(t, http.StatusOK, w.Code)
|
|
|
|
|
require.Equal(t, testSkillTenantID, lister.tenantID)
|
|
|
|
|
require.Equal(t, "cfg-1", lister.configID)
|
|
|
|
|
|
|
|
|
|
var body struct {
|
|
|
|
|
Success bool `json:"success"`
|
|
|
|
|
Data []SkillInfoResponse
|
|
|
|
|
SkillsAvailable bool `json:"skills_available"`
|
|
|
|
|
}
|
|
|
|
|
require.NoError(t, json.Unmarshal(w.Body.Bytes(), &body))
|
|
|
|
|
require.True(t, body.Success)
|
|
|
|
|
require.True(t, body.SkillsAvailable)
|
|
|
|
|
require.Equal(t, []SkillInfoResponse{
|
|
|
|
|
{Name: "ppt-generator", Description: "make ppt"},
|
|
|
|
|
}, body.Data)
|
|
|
|
|
}
|