feat: 动作库查看/管理(接口 + AI 控制台「🎬 动作库」面板)+ 未沉淀灰卡提示

- 后端:GET /api/agent/actions(列表)、POST /api/agent/actions/delete、
  POST /api/agent/actions/save(新增/编辑,steps 走 _sanitize_actions 校验:
  type 白名单 + 必填 + **拒绝坐标 click_xy**),均 @admin_required
- 前端:AI 控制台右上角新增「🎬 动作库」按钮与模态框——列出 名称/app/别名/
  步骤摘要(含元素定位)/命中次数/更新时间;支持编辑(表单 + steps JSON)、删除、
  手动新建;保存失败(如写坐标)即时 toast 原因
- 未沉淀提示:本轮无可沉淀动作时也推送一张 🧠 动作经验 灰卡
  「本轮未沉淀动作(步骤以坐标定位为主,缺少可复用的元素定位信息)」
- doc/API.md:登记三个新接口
实测:列表返回自动沉淀的动作;保存合法动作 200;提交 click_xy 步骤被 400 拒绝;
删除生效
This commit is contained in:
2026-09-10 13:58:38 +08:00
parent 104964aa53
commit 91b2785c6b
4 changed files with 224 additions and 0 deletions
+99
View File
@@ -512,6 +512,105 @@ function endRun(){
scrollChat();
}
// ================== 动作库(可复用动作:查看/编辑/删除/新建) ==================
let _actCache = [];
function openActLib(){
document.getElementById('agent-act-overlay').style.display = 'flex';
loadActLib();
}
function closeActLib(){
document.getElementById('agent-act-overlay').style.display = 'none';
}
function _actStepBrief(a){
return (a.steps||[]).map(s=>{
const p = s.params || {};
const loc = p.selector_value || p.package || p.fixed_text || '';
return (s.type || '?') + (loc ? (':' + loc) : '');
}).join(' → ');
}
function loadActLib(){
const list = document.getElementById('act-lib-list');
apiGet('/api/agent/actions').then(r=>{
if(!r||!r.ok){ showToast((r&&r.error)||'加载失败','error'); return; }
_actCache = r.actions || [];
document.getElementById('act-lib-hint').textContent = '共 ' + _actCache.length + ' 个动作';
if(!_actCache.length){
list.innerHTML = '<div class="text-muted" style="padding:20px;text-align:center">暂无动作——跑一轮以元素/文字点击为主的任务后会自动沉淀</div>';
return;
}
list.innerHTML = '';
_actCache.forEach(a=>{
const card = document.createElement('div');
card.style.cssText = 'border:1px solid var(--card-line);border-radius:10px;padding:9px 13px;background:#0d1117';
card.innerHTML = '<div style="display:flex;justify-content:space-between;gap:10px;align-items:flex-start">'
+ '<div style="flex:1;min-width:0">'
+ '<div style="font-weight:600">' + esc(a.name||'')
+ (a.app ? '<span class="text-muted" style="font-size:11px;font-weight:400;margin-left:6px">' + esc(a.app) + '</span>' : '')
+ '</div>'
+ '<div style="font-size:11.5px;color:#9fb3d1;margin-top:2px;word-break:break-all">' + esc(_actStepBrief(a) || '—') + '</div>'
+ '<div class="text-muted" style="font-size:11px;margin-top:2px">别名: ' + esc((a.aliases||[]).join('、')||'—')
+ ' | 命中 ' + (a.hits||0) + ' 次 | ' + esc(a.updated_at||'') + '</div>'
+ '</div>'
+ '<div style="display:flex;flex-direction:column;gap:4px;flex:none">'
+ '<button class="btn btn-xs" onclick="actEdit(' + a.id + ')">编辑</button>'
+ '<button class="btn btn-xs btn-danger" onclick="actDelete(' + a.id + ')">删除</button>'
+ '</div></div>';
list.appendChild(card);
});
});
}
function actDelete(id){
if(!confirm('确认删除该动作?删除后同类任务不再自动复用它。')) return;
apiPost('/api/agent/actions/delete',{id:id}).then(r=>{
showToast((r&&(r.msg||r.error))||'删除失败', (r&&r.ok)?'success':'error');
loadActLib();
});
}
function actNew(){ _actForm({name:'', app:'', aliases:[], params:[], steps:[]}); }
function actEdit(id){
const a = _actCache.find(x=>x.id===id);
if(a) _actForm(a);
}
function _actForm(a){
const list = document.getElementById('act-lib-list');
const isNew = !a.id;
list.innerHTML = '';
const box = document.createElement('div');
box.style.cssText = 'border:1px solid var(--card-line);border-radius:10px;padding:12px 14px;background:#0d1117';
box.innerHTML =
'<div style="font-weight:600;margin-bottom:8px">' + (isNew?'新建动作':'编辑动作 #'+a.id) + '</div>'
+ '<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:6px">'
+ '<input id="act-f-name" class="form-control" style="flex:1;min-width:180px" placeholder="动作名(如 打开抖音)" value="' + esc(a.name||'') + '">'
+ '<input id="act-f-app" class="form-control" style="flex:1;min-width:180px" placeholder="包名(可空,如 com.ss.android.ugc.aweme)" value="' + esc(a.app||'') + '">'
+ '</div>'
+ '<div style="display:flex;gap:8px;flex-wrap:wrap;margin-bottom:6px">'
+ '<input id="act-f-alias" class="form-control" style="flex:1;min-width:180px" placeholder="别名(逗号分隔)" value="' + esc((a.aliases||[]).join(',')) + '">'
+ '<input id="act-f-params" class="form-control" style="flex:1;min-width:180px" placeholder="参数名(逗号分隔,可空)" value="' + esc((a.params||[]).join(',')) + '">'
+ '</div>'
+ '<textarea id="act-f-steps" class="form-control" rows="9" style="font-family:monospace;font-size:12px" '
+ 'placeholder=\'步骤 JSON 数组,例:[{"type":"click","params":{"selector_type":"text","selector_value":"搜索"}}]\'>'
+ esc(JSON.stringify(a.steps||[], null, 2)) + '</textarea>'
+ '<div class="text-muted" style="font-size:11px;margin:6px 0">可用 type:open_app/click/input_text/swipe/wait/key_event/group…;click 必须有 selector_value(xpath/text/resourceId/description…);<b>不接受坐标(click_xy)</b>。</div>'
+ '<div style="display:flex;gap:8px"><button class="btn btn-primary btn-sm" onclick="actSave(' + (a.id||0) + ')">保存</button>'
+ '<button class="btn btn-sm" onclick="loadActLib()">取消</button></div>';
list.appendChild(box);
}
function actSave(id){
const g = (i)=>document.getElementById(i).value;
const split = (s)=>String(s||'').split(/[,,]/).map(x=>x.trim()).filter(x=>x);
let steps;
try{ steps = JSON.parse(g('act-f-steps')||'[]'); }
catch(e){ showToast('steps 不是合法 JSON: '+e.message,'error'); return; }
apiPost('/api/agent/actions/save', {
id: id||0, name: g('act-f-name'), app: g('act-f-app'),
aliases: split(g('act-f-alias')), params: split(g('act-f-params')), steps: steps
}).then(r=>{
if(!r||!r.ok){ showToast((r&&r.error)||'保存失败','error'); return; }
showToast(r.msg||'已保存','success');
loadActLib();
});
}
// ================== 经验库(自进化记忆管理) ==================
function openExpLib(){
document.getElementById('agent-exp-overlay').style.display = 'flex';