Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 9 additions & 1 deletion web/app/agents.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,15 @@ function renderAgentsLegacy(){
function renderAgents(){
const list=$('#agentList');if(!list)return;renderCatalogScopes();
const query=($('#agentSearch')?.value||'').trim().toLowerCase(),agents=scopedAgents().filter(a=>!query||`${a.name} ${a.id} ${a.description} ${(a.rules||[]).join(' ')} ${(a.skills||[]).join(' ')}`.toLowerCase().includes(query));
const used=new Map();(state.data.workflows||[]).forEach(w=>(w.nodes||[]).forEach(n=>{if(n.agentId)used.set(n.agentId,(used.get(n.agentId)||0)+1)}));
const used=new Map(),workflows=state.data.workflows||[],roles=new Map((state.data.agents||[]).map(a=>[a.id,a]));
const templateAgentIds=new Set(workflows.filter(w=>w.generic).flatMap(w=>(w.nodes||[]).map(n=>n.agentId?.trim()).filter(Boolean)));
workflows.forEach(w=>(w.nodes||[]).forEach(n=>{
const id=n.agentId?.trim();if(!id)return;
const a=roles.get(id);
// Match AgentShadowsTemplateOutside: private template copies only bind in their own project.
if(a?.projectId&&!a.shared&&a.projectId!==w.projectId&&templateAgentIds.has(id))return;
used.set(id,(used.get(id)||0)+1);
}));
$('#agentCatalogStats').innerHTML=`<span><b>${agents.length}</b> 个${state.agentScope==='shared'?'共享':'项目'}角色</span><span><b>${agents.reduce((n,a)=>n+(a.skills?.length||0),0)}</b> 项技能绑定</span><span><b>${agents.reduce((n,a)=>n+(a.mcpServers?.length||0),0)}</b> 个 MCP 连接</span>`;
const empty=state.agentScope==='shared'?'还没有共享给当前项目的角色。':'当前项目还没有角色。可以新建,或从共享角色复制后改。';
closeAgentRowMenu();
Expand Down
54 changes: 54 additions & 0 deletions webtest/tests/assets-agents.test.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,60 @@ test('agent catalog search, presets, validation, persistence, clone, and delete
assert.equal(app.evaluate(`state.data.agents.some(a=>a.id==='agent-copy')`), false);
});

test('agent usage ignores template shadows and keeps real workflow references', async (t) => {
const app = await loadApp();
t.after(app.close);
app.evaluate(`
state.projectFilter='project-1';state.agentScope='project';
state.data.agents=[{id:'template-role',projectId:'project-1',name:'Developer'}];
state.data.workflows=[
{id:'generic-flow',generic:true,nodes:[{agentId:'template-role'}]},
{id:'other-flow',projectId:'project-2',nodes:[{agentId:'template-role'}]}
];
switchView('agents');renderAgents();
`);
const usage = () => app.document.querySelector('[data-agent-id="template-role"] .agent-row-uses').textContent;
const deleteButton = () => {
app.document.querySelector('[data-agent-more="template-role"]').click();
return app.document.querySelector('#agentRowMenu [data-agent-act="delete"]');
};
assert.equal(usage(), '未使用');
assert.equal(deleteButton().disabled, false);

app.evaluate(`
state.data.workflows.push({id:'project-flow',projectId:'project-1',nodes:[
{agentId:'template-role'},{agentId:'template-role'},{}
]});renderAgents();
`);
assert.equal(usage(), '2 个节点');
assert.equal(deleteButton().disabled, true);

app.evaluate(`state.data.workflows.pop();renderAgents()`);
assert.equal(usage(), '未使用');
assert.equal(deleteButton().disabled, false);

// Shared roles resolve across projects and remain protected from deletion.
app.evaluate(`state.data.agents[0].shared=true;renderAgents()`);
assert.equal(usage(), '2 个节点');
assert.equal(deleteButton().disabled, true);

// A private role whose ID is not in a template still binds across projects.
app.evaluate(`
state.data.agents[0].shared=false;
state.data.workflows=state.data.workflows.filter(w=>!w.generic);renderAgents();
`);
assert.equal(usage(), '1 个节点');
assert.equal(deleteButton().disabled, true);

// Platform roles also keep references from generic templates.
app.evaluate(`
state.data.agents[0].projectId='';state.agentScope='shared';
state.data.workflows.push({id:'generic-flow',generic:true,nodes:[{agentId:'template-role'}]});
renderAgents();
`);
assert.equal(usage(), '2 个节点');
});

test('role editor refreshes configured CLIs and preserves an unavailable saved executor', async (t) => {
const app = await loadApp();
t.after(app.close);
Expand Down