publishport-opencli 1.0.61 → 1.0.63
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/clis/_shared/article/publish.js +12 -7
- package/clis/imooc/article.js +3 -3
- package/clis/medium/article.js +5 -5
- package/clis/toutiao/article.js +1 -1
- package/clis/yuque/article.js +1 -1
- package/dist/src/browser/extract.d.ts +7 -0
- package/dist/src/browser/extract.js +7 -5
- package/dist/src/cli.js +12 -12
- package/package.json +1 -1
- package/skills/opencli-browser/SKILL.md +2 -1
|
@@ -1,11 +1,11 @@
|
|
|
1
|
-
import{CommandExecutionError as
|
|
1
|
+
import{ArgumentError as K,CommandExecutionError as _}from"@jackwener/opencli/errors";import{normalizeContent as P}from"./format.js";import{assertLiteralContent as N}from"../content-guard.js";import{imageRefKey as S,inlineLocalImages as x,isLocalImagePath as A,localImageToDataUri as C}from"./images.js";import{PAGE_RUNTIME as M}from"./page-runtime.js";export function selectContent(r,e,n="auto"){const a=P(r,{format:n});return e.outputFormat==="markdown"?a.markdown:a.html}export function originReFromHome(r){let e="";try{e=new URL(r).host}catch(a){e=""}return"^https?://([^/]*\\.)?"+e.replace(/[.*+?^${}()|[\]\\]/g,"\\$&")+"(/|$)"}export async function gotoWritePage(r,e,n){const a=n||originReFromHome(e);let l=null;for(let s=0;s<8;s++){await r.goto(e);await r.wait({time:1});const u=await r.evaluate(`(() => {
|
|
2
2
|
var href = ''; try { href = location.href; } catch (e) {}
|
|
3
|
-
var onSite = new RegExp(${JSON.stringify(
|
|
3
|
+
var onSite = new RegExp(${JSON.stringify(a)}).test(href);
|
|
4
4
|
var cookieOk = true; try { void document.cookie; } catch (e) { cookieOk = false; }
|
|
5
5
|
return { href: href, onSite: onSite, cookieOk: cookieOk };
|
|
6
|
-
})()`);if(u&&u.onSite&&u.cookieOk)return;if(u)
|
|
7
|
-
`+
|
|
8
|
-
const I = `+JSON.stringify(
|
|
6
|
+
})()`);if(u&&u.onSite&&u.cookieOk)return;if(u)l=u;await r.wait({time:1})}const c=l&&l.href||"",t=new _(`无法在浏览器中打开已登录的写作页(未落到 ${e}${c?`,当前停留在 ${c}`:",页面停留在空白页"})。`+"请确认登录该平台的 Chrome 已连接 ppcli 浏览器桥。");t.lastHref=c;t.landedOffSite=/^https?:/i.test(c);throw t}export function buildPublishJs(r,e,n){return`(async () => {
|
|
7
|
+
`+M+`
|
|
8
|
+
const I = `+JSON.stringify(r)+`;
|
|
9
9
|
const __publish = (`+e+`);
|
|
10
10
|
`+(n?"const __upload = ("+n+`);
|
|
11
11
|
`:"")+`const __content0 = I.content;
|
|
@@ -14,6 +14,9 @@ if (I.outputFormat === "html" && I.preprocessConfig) content = PP.preprocess(con
|
|
|
14
14
|
`+(n?`const __t = await PP.processImagesWith(content, (src) => __upload(src, PP), { skip: I.imageSkip, allowRelative: I.imageAllowRelative });
|
|
15
15
|
`:`const __t = await PP.transferImages(content, I.imageSpec, I.imageSkip);
|
|
16
16
|
`)+`content = __t.content;
|
|
17
|
+
if (__t.failed.length && !I.draftOnly) {
|
|
18
|
+
`+` return { ok: false, stage: "images", message: "正文图片转存到平台图床失败,已在提交前中止,平台上未产生任何内容", uploaded: __t.uploaded, failed: __t.failed };
|
|
19
|
+
`+`}
|
|
17
20
|
if (I.publishParams && typeof I.publishParams.cover === "string" && I.publishParams.cover) {
|
|
18
21
|
var __cvWrap = '<img src="' + I.publishParams.cover + '">';
|
|
19
22
|
`+(n?` var __cv = await PP.processImagesWith(__cvWrap, (src) => __upload(src, PP), { skip: I.imageSkip, allowRelative: I.imageAllowRelative });
|
|
@@ -36,6 +39,8 @@ if (!__pub || __pub.ok === false) {
|
|
|
36
39
|
return { ok: false, stage: (__pub && __pub.stage) || "publish", status: __pub && __pub.status, message: (__pub && __pub.message) || "publish failed", detail: __pub && __pub.detail, unknownOutcome: !!(__pub && __pub.unknownOutcome), uploaded: __upN, failed: __failN };
|
|
37
40
|
}
|
|
38
41
|
return { ok: true, id: String(__pub.id == null ? "" : __pub.id), url: __pub.url || "", publicUrl: __pub.publicUrl || "", draft: !!__pub.draft, uploaded: __upN, failed: __failN };
|
|
39
|
-
})()`}export function labelImageEntries(
|
|
42
|
+
})()`}export function labelImageEntries(r,e){return(r||[]).map((n)=>{if(!n||typeof n!=="object")return n;const{key:a,...l}=n,c=String(l.src||"");let t=a?e.byKey.get(a):void 0;if(t===void 0&&c.startsWith("data:")){const s=e.byPrefix.filter((m)=>m.prefix.startsWith(c)||c.startsWith(m.prefix)),u=[...new Set(s.map((m)=>m.src))];if(u.length===1)t=u[0]}if(t!==void 0)l.src=t;if(a&&e.coverKey&&a===e.coverKey)l.cover=!0;return l})}export function formatFailedImageList(r){return(r||[]).map((e)=>{const n=String(e&&e.src||""),a=n.startsWith("data:")?"内联图片("+n.slice(0,24)+"…)":n.length>200?n.slice(0,200)+"…":n;return`
|
|
43
|
+
- `+(e&&e.cover?"封面 ":"")+(a||"(空地址)")+":"+String(e&&e.error||"未知原因")}).join("")}export function formatImageReport(r){const e=r&&r.uploaded||[],n=r&&r.failed||[];if(!e.length&&!n.length)return"";const a=(t)=>{const s=String(t&&t.src||""),u=s.startsWith("data:")?"内联图片("+s.slice(0,24)+"…)":s.length>100?s.slice(0,100)+"…":s;return(t&&t.cover?"封面 ":"")+(u||"(空地址)")},l=[];for(const t of n){const s=String(t&&t.error||"未知原因");l.push("✗ "+a(t)+"("+(s.length>200?s.slice(0,200)+"…":s)+")")}for(const t of e)l.push("✓ "+a(t));let c="。图片明细:"+l.join(";");if(n.length)c+="。⚠️ 失败的图不在草稿里:按原因修正后到平台后台编辑该草稿补图,再发布;不要直接重发本命令(会多出一篇草稿)";return c}export function localImagesMissingError(r){const e=r.map((n)=>`
|
|
44
|
+
- `+n.src+":"+n.error).join("");return new K(`正文引用的 ${r.length} 张本机图片读不到,未打开平台页面、未发布:${e}`,"修正图片路径(相对路径按正文文件所在目录解析,也可改用绝对路径),或从正文删去这些图片后重发")}export async function publishArticle(r,e){const{title:n,body:a,format:l="auto",draftOnly:c=!1,profile:t,publishParams:s=null}=e;if(!t)throw Error("publishArticle: profile is required");if(!t.home)throw Error("publishArticle: profile.home is required");if(typeof t.publish!=="function")throw Error("publishArticle: profile.publish must be a function");N(a,{fileFlag:"--file"});const u=P(a,{format:l}),m=t.outputFormat==="markdown"||t.needsMarkdown===!0,O=t.outputFormat==="html"||t.needsHtml===!0,g=m?await x(u.markdown):{content:null,missing:[]},h=O?await x(u.html):{content:null,missing:[]},b=g.content,k=h.content,F=t.outputFormat==="markdown"?b:k,w=[],v=new Set;for(const o of[...g.missing,...h.missing]){if(v.has(o.src))continue;v.add(o.src);w.push(o)}if(w.length)throw localImagesMissingError(w);const f={byKey:new Map,byPrefix:[],coverKey:""};for(const o of[...g.inlined||[],...h.inlined||[]]){if(!o.dataUri)continue;f.byKey.set(S(o.dataUri),o.src);f.byPrefix.push({prefix:o.dataUri.slice(0,120),src:o.src})}let d=s;if(d&&typeof d.cover==="string"&&d.cover){const o=d.cover.trim();if(/["<>\s]/.test(o))throw new _("封面图路径/URL 含非法字符(引号/尖括号/空白):"+o.slice(0,120));if(A(o))try{const{dataUri:p}=await C(o);d={...d,cover:p};f.coverKey=S(p);f.byKey.set(f.coverKey,o);f.byPrefix.push({prefix:p.slice(0,120),src:o})}catch(p){throw new _("封面图读取失败:"+o+"("+String(p&&p.message||p)+")")}else d={...d,cover:o}}await gotoWritePage(r,t.home,t.originRe);const R={title:n,content:F,markdown:t.needsMarkdown===!0&&t.outputFormat!=="markdown"?b:null,html:t.needsHtml===!0&&t.outputFormat!=="html"?k:null,draftOnly:!!c,outputFormat:t.outputFormat,preprocessConfig:t.preprocessConfig||null,imageSpec:t.image?.spec||null,imageSkip:t.image?.skip||[],imageAllowRelative:t.image?.allowRelative===!0,publishParams:d},U=typeof t.image?.uploadFn==="function"?t.image.uploadFn.toString():null,E=buildPublishJs(R,t.publish.toString(),U),i=await r.evaluate(E);if(!i||i.ok===!1){const o=i?.stage||"publish",p=i?.status!=null?` (HTTP ${i.status})`:"",I=i?.detail!=null?`;接口原始响应:${typeof i.detail==="string"?i.detail:JSON.stringify(i.detail)}`:"";if(i?.unknownOutcome){const y=new _(`[${o}] 提交请求已发出但未收到平台响应,本次发布结果未知:${i?.message||"网络异常"}${p}${I}`+`
|
|
40
45
|
⚠️ 禁止直接重试本命令——文章可能已经创建成功,重试会发出重复内容。`+`
|
|
41
|
-
正确做法:先用该平台的列表/统计命令(如 stats / lists / articles)按标题回查最近内容,`+"确认未创建后再重发;已创建则直接以该条为准。");
|
|
46
|
+
正确做法:先用该平台的列表/统计命令(如 stats / lists / articles)按标题回查最近内容,`+"确认未创建后再重发;已创建则直接以该条为准。");y.unknownOutcome=!0;throw y}const W=o==="images"?formatFailedImageList(labelImageEntries(i?.failed||[],f)):"";throw new _(`[${o}] ${i?.message||"发布失败"}${p}${I}${W}`)}return{id:i.id,url:i.url,publicUrl:i.publicUrl||"",draft:i.draft,images:{uploaded:labelImageEntries(i.uploaded||[],f),failed:labelImageEntries(i.failed||[],f)}}}export const __test__={selectContent,originReFromHome,buildPublishJs,publishArticle,labelImageEntries,formatFailedImageList,localImagesMissingError,formatImageReport};
|
package/clis/imooc/article.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import{CliError as c,CommandExecutionError as
|
|
1
|
+
import{CliError as c,CommandExecutionError as w}from"@jackwener/opencli/errors";import{cli as P,Strategy as L}from"@jackwener/opencli/registry";import{formatFailedImageList as A,formatImageReport as S,gotoWritePage as N,localImagesMissingError as T,publishArticle as U}from"../_shared/article/publish.js";import{normalizeContent as C}from"../_shared/article/format.js";import{inlineLocalImages as D,isLocalImagePath as F,localImageToDataUri as E,rememberArticleFile as j}from"../_shared/article/images.js";import{readFile as R,stat as O}from"node:fs/promises";export const imoocProfile={home:"https://www.imooc.com/article",outputFormat:"markdown",checkAuth:async(t)=>{let e=await(await fetch("https://www.imooc.com/u/card",{credentials:"include"})).text();e=e.replace(/^jsonpcallback\(/,"").replace(/\}\)$/,"}");let n;try{n=JSON.parse(e)}catch(o){return{isAuthenticated:!1,error:"JSONP 解析失败:"+String(o&&o.message||o)}}if(!n||n.result!==0)return{isAuthenticated:!1,error:n&&n.msg||"未登录"};return{isAuthenticated:!0,userId:String(n.data.uid),username:n.data.nickname,avatar:n.data.img}},image:{uploadFn:async(t,r)=>{const e=await fetch(t,{credentials:"omit"});if(!e.ok)throw Error("图片下载失败:HTTP "+e.status);const n=await e.blob(),o=Date.now()+".jpg",i=new File([n],o,{type:n.type||"image/jpeg"}),l=new FormData;l.append("photo",i,o);l.append("type",i.type);l.append("id","WU_FILE_0");l.append("name",o);l.append("lastModifiedDate",new Date().toString());l.append("size",String(i.size));const u=await(await fetch("https://www.imooc.com/article/ajaxuploadimg",{method:"POST",credentials:"include",body:l})).json();if(!u||u.result!==0)throw Error(u&&u.msg||"图片上传失败");let d=u.data.imgpath;if(d&&d.indexOf("//")===0)d="https:"+d;return{url:d}},skip:["img.imooc.com","imooc.com"]},publish:async(t,r)=>{const e=await fetch("https://www.imooc.com/article/savedraft",{method:"POST",credentials:"include",headers:{"Content-Type":"application/x-www-form-urlencoded"},body:new URLSearchParams({editor:"0",draft_id:"0",title:t.title,content:t.content})});if(!e.ok){const i=await e.text();return{ok:!1,stage:"savedraft",status:e.status,message:i.slice(0,300)}}const n=await e.json();if(!n||!n.data){const i=JSON.stringify(n).slice(0,300);return{ok:!1,stage:"savedraft",status:e.status,message:"保存草稿失败:"+i}}const o=String(n.data);return{ok:!0,id:o,url:"https://www.imooc.com/article/draft/id/"+o,draft:!0}}};function V(t){if(!t.execute)throw new c("INVALID_INPUT","此命令需要 --execute 才会真正写入。注意:本报错仅表示缺少 --execute,并未校验其余参数,不能当 dry-run 预览用")}async function q(t){const r=typeof t.text==="string"?t.text:void 0,e=typeof t.file==="string"?t.file:void 0;if(r&&e)throw new c("INVALID_INPUT","<text> 和 --file 不能同时使用");let n=r??"";if(e){j(e);let o;try{o=await O(e)}catch{throw new c("INVALID_INPUT","文件不存在:"+e)}if(!o.isFile())throw new c("INVALID_INPUT","路径必须是可读文本文件:"+e);let i;try{i=await R(e)}catch{throw new c("INVALID_INPUT","文件读取失败:"+e)}try{n=new TextDecoder("utf-8",{fatal:!0}).decode(i)}catch{throw new c("INVALID_INPUT","文件不是有效 UTF-8:"+e)}}if(!n.trim())throw new c("INVALID_INPUT","正文不能为空");return n}function b(t,r,e,n,o={}){return[{status:"success",outcome:n,message:t,target_type:r,target:e,...o}]}const M="https://www.imooc.com/article/publish",J="https?://www\\.imooc\\.com/article/(\\d+)",B=imoocProfile.image.uploadFn.toString();function z(t,r){return`(async () => {
|
|
2
2
|
const I = `+JSON.stringify(t)+`;
|
|
3
3
|
const __upload = (`+r+`);
|
|
4
4
|
const PP = {};
|
|
@@ -23,7 +23,7 @@ window.app.setEditorContent(md);
|
|
|
23
23
|
await sleep(400);
|
|
24
24
|
var plen = (window.parsinCtx && window.parsinCtx.text) ? window.parsinCtx.text.length : 0;
|
|
25
25
|
return { ok: true, uploaded: uploaded, failed: failed, cover: cover, parsinLen: plen };
|
|
26
|
-
})()`}function
|
|
26
|
+
})()`}function H(t){return`(async () => {
|
|
27
27
|
const I = `+JSON.stringify(t)+`;
|
|
28
28
|
const sleep = (ms) => new Promise(r => setTimeout(r, ms));
|
|
29
29
|
var pub = document.querySelector(".js-article-publish");
|
|
@@ -56,4 +56,4 @@ var chosenCount = document.querySelectorAll(".js-chosen-tags .tag-item").length;
|
|
|
56
56
|
document.getElementById("save_article").click();
|
|
57
57
|
await sleep(400);
|
|
58
58
|
return { ok: true, chosen: chosen, notFound: notFound, column: col.textContent.trim() };
|
|
59
|
-
})()`}async function
|
|
59
|
+
})()`}async function Q(t,r){const{title:e,body:n,format:o,column:i,tags:l,original:s,cover:u}=r,d=C(n,{format:o}),g=await D(d.markdown);if(g.missing.length)throw T(g.missing);let f=g.content,y="";if(u){const p=u.trim();if(/["<>\s]/.test(p))throw new w("封面图路径/URL 含非法字符(引号/尖括号/空白):"+p.slice(0,120));if(F(p))try{y=(await E(p)).dataUri}catch(h){throw new w("封面图读取失败:"+p+"("+String(h&&h.message||h)+")")}else y=p}await N(t,M,imoocProfile.originRe);const a=await t.evaluate(z({title:e,md:f,cover:y,skip:imoocProfile.image.skip||[]},B));if(!a||a.ok===!1)throw new w("["+(a?.stage||"setup")+"] "+(a?.message||"内容准备失败"));if(a.failed&&a.failed.length)throw new w("[images] 正文图片转存到慕课图床失败,已在发表前中止,未发表"+A(a.failed));if(!a.parsinLen||a.parsinLen<200)throw new w("正文进入编辑器后不足 200 字(慕课发表要求正文≥200字),当前约 "+(a.parsinLen||0)+" 字。");const m=await t.evaluate(H({column:String(i),tags:l,original:s,cover:a.cover}));if(!m||m.ok===!1)throw new w(m?.message||"填写发表信息失败");const x=new RegExp(J);let I="";for(let p=0;p<75;p++){await t.wait({time:2});let h="";try{h=await t.getCurrentUrl()}catch{}const v=h&&h.match(x);if(v){I=v[1];break}}const k=a.uploaded||[],_=a.failed||[];if(!I)throw new w("内容与发表信息已全部填好并进入发表流程,但未检测到发表成功跳转"+"(约 150 秒内未完成点选文字验证码)。请在弹出的慕课浏览器窗口按提示点选文字完成验证,"+"页面会自动跳到文章页即代表发表成功;或稍后重试本命令。");return{id:I,url:"https://www.imooc.com/article/"+I,images:{uploaded:k,failed:_},column:m.column,tags:m.chosen||[],notFoundTags:m.notFound||[]}}P({site:"imooc",name:"article",access:"write",description:"发布慕课手记文章(Markdown)。默认走「发表」(半自动:AI 填全部内容/栏目/标签/封面,"+"最后的点选文字验证码需用户在弹出的浏览器窗口点一下);加 --draft 则只存草稿(无验证码)。外链图自动转存到慕课图床。",domain:"www.imooc.com",strategy:L.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题(≤80字)"},{name:"text",positional:!0,help:"文章正文(Markdown,默认;发表要求正文≥200字)"},{name:"file",help:"正文文件路径(UTF-8,Markdown)"},{name:"column",help:"栏目(发表必填):名称或 data-columnid,如「后端开发」/120、「前端开发」/119、「人工智能」/117"},{name:"tags",help:"标签(发表必填,逗号分隔,至少 1 个):名称或 data-tag,如 MySQL,Java 或 11,3"},{name:"cover",help:"封面图 URL 或本机路径(发表可选,缺省用慕课随机封面)"},{name:"original",help:"文章类型(发表可选,默认「原创」):原创 / 首发 / 转载"},{name:"html",type:"boolean",help:"将正文视为原始 HTML 而非 Markdown"},{name:"draft",type:"boolean",help:"只存草稿(不发表,无需验证码)"},{name:"execute",type:"boolean",help:"真正执行写入,不加此标志时拒绝写操作"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(t,r)=>{if(!t)throw new w("慕课手记发布需要浏览器会话");V(r);const e=String(r.title??"").trim();if(!e)throw new c("INVALID_INPUT","文章标题不能为空");if(e.length>80)throw new c("INVALID_INPUT","标题不能超过 80 字,当前 "+e.length+" 字");const n=await q(r);if(r.draft){const f=await U(t,{title:e,body:n,format:r.html?"html":"markdown",draftOnly:!0,profile:imoocProfile}),y=f.images.uploaded.length|0,a=f.images.failed.length|0;let m="已保存慕课手记草稿";if(y||a)m+=`・图片:${y} 张已转存${a?`,${a} 张失败`:""}`;m+=S(f.images);return b(m,"article","","draft",{created_target:"article:"+f.id,created_url:f.url})}const o=String(r.column??"").trim();if(!o)throw new c("INVALID_INPUT","发表必填 --column(栏目)。可选:前端开发/后端开发/人工智能/对话ChatGPT/移动开发/云计算大数据/产品设计/工具资源/职场生活/经验分享/其它。"+"只想存草稿加 --draft。");const i=String(r.tags??"").split(",").map((f)=>f.trim()).filter(Boolean);if(i.length===0)throw new c("INVALID_INPUT","发表必填 --tags(标签,逗号分隔,至少 1 个),如 --tags MySQL,Java。只想存草稿加 --draft。");const l=String(r.original??"原创").trim()||"原创";if(!["原创","首发","转载","其他"].includes(l))throw new c("INVALID_INPUT","--original 只能是 原创 / 首发 / 转载,当前:"+l);const s=await Q(t,{title:e,body:n,format:r.html?"html":"markdown",column:o,tags:i,original:l,cover:typeof r.cover==="string"?r.cover:""}),u=s.images.uploaded.length|0,d=s.images.failed.length|0;let g=`已发表慕课手记・栏目「${s.column}」・标签 ${s.tags.join("/")}`;if(u||d)g+=`・图片:${u} 张已转存${d?`,${d} 张失败`:""}`;g+=S(s.images);if(s.notFoundTags.length)g+=`・未匹配标签:${s.notFoundTags.join(",")}`;return b(g,"article","","published",{created_target:"article:"+s.id,created_url:s.url})}});
|
package/clis/medium/article.js
CHANGED
|
@@ -1,12 +1,12 @@
|
|
|
1
|
-
import{cli as
|
|
1
|
+
import{cli as et,Strategy as at}from"@jackwener/opencli/registry";import{CliError as U,CommandExecutionError as it}from"@jackwener/opencli/errors";import{readFile as st,stat as nt}from"node:fs/promises";import{formatImageReport as rt,publishArticle as ot}from"../_shared/article/publish.js";import{rememberArticleFile as lt}from"../_shared/article/images.js";export const mediumProfile={home:"https://medium.com/new-story",outputFormat:"markdown",checkAuth:async(i)=>{try{const r=await(await fetch("https://medium.com/_/graphql",{method:"POST",credentials:"include",headers:{"content-type":"application/json",accept:"application/json"},body:JSON.stringify({operationName:"ViewerQuery",query:"query ViewerQuery { viewer { id name username imageId } }",variables:{}})})).json(),c=r&&r.data&&r.data.viewer;if(c&&c.id)return{isAuthenticated:!0,userId:c.id,username:c.username||c.name||"",avatar:c.imageId?"https://miro.medium.com/v2/resize:fill:96:96/"+c.imageId:""};return{isAuthenticated:!1}}catch(s){return{isAuthenticated:!1,error:String(s&&s.message||s)}}},publish:async(i,s)=>{const r=(t)=>t.replace(/^\s*\]\)\}while\(1\);(<\/x>)?/,""),c=document.documentElement.outerHTML.match(/"xsrfToken":"([^"]+)"/),w=c?c[1]:null;if(!w)return{ok:!1,stage:"xsrf",message:"未能从编辑器页提取 xsrfToken(请确认已登录 medium.com 且停留在 /new-story)"};const A={accept:"application/json","x-requested-with":"XMLHttpRequest","x-xsrf-token":w},g=async(t,a,u)=>{const o=Object.assign({},A);if(u!==void 0)o["content-type"]="application/json";const l=await fetch("https://medium.com"+t,{method:a||"GET",credentials:"include",headers:o,body:u!==void 0?JSON.stringify(u):void 0});let m=r(await l.text()),e=null;try{e=JSON.parse(m)}catch(p){}return{status:l.status,json:e,text:m}},y=(t)=>{const a=[];let u=0,o="";const l=(m)=>{if(m)o+=m};while(u<t.length){const e=t.slice(u).match(/^(\[([^\]]+)\]\(([^)]+)\)|\*\*([^*]+)\*\*|`([^`]+)`|\*([^*]+)\*|(https?:\/\/[^\s<]+[^\s<.,;:!?")\]]))/);if(!e){l(t[u]);u+=1;continue}const p=u+(e.index||0);if(p>u)l(t.slice(u,p));const b=e[1],d=o.length;if(e[2]!==void 0&&e[3]!==void 0){l(e[2]);a.push({type:3,start:d,end:o.length,href:e[3],title:"",rel:"nofollow",anchorType:0})}else if(e[4]!==void 0){l(e[4]);a.push({type:1,start:d,end:o.length})}else if(e[5]!==void 0)l(e[5]);else if(e[6]!==void 0){l(e[6]);a.push({type:2,start:d,end:o.length})}else if(e[7]!==void 0){l(e[7]);a.push({type:3,start:d,end:o.length,href:e[7],title:"",rel:"nofollow",anchorType:0})}else l(b);u=p+b.length}return{text:o,markups:a}},O=(t)=>{const a={paragraph:1,h1:12,h2:2,h3:3,h4:13,blockquote:6,code:8,"ul-li":9,"ol-li":10},u=(t||"").replace(/\r\n/g,`
|
|
2
2
|
`).split(`
|
|
3
|
-
`),o=[];let l=[],m,e=[],
|
|
4
|
-
`),markups:[]},m?{codeLang:m}:{}));e=[];m=void 0};for(const n of u){const I=n.trim().match(/^!\[([^\]]*)\]\(([^)\s]+)(?:\s+"[^"]*")?\)$/i),N=n.trim().match(/^<img\b[^>]*?\bsrc=["']([^"']+)["'][^>]*>$/i);if(I){
|
|
3
|
+
`),o=[];let l=[],m,e=[],p=!1;const b=(n,I,N)=>{const P=(n||"").trim();if(!P)return;const S=a[I]||a.paragraph,G=S===a.code?{text:P,markups:[]}:y(P);o.push(Object.assign({type:S,text:G.text,markups:G.markups},N?{codeLang:N}:{}))},d=()=>{if(!l.length)return;const n=l.join(" ").trim();l=[];if(!n)return;const I=n.match(/^(#{1,4})\s+(.*)$/);if(I){b(I[2].trim(),"h"+I[1].length);return}const N=n.match(/^>\s?(.*)$/);if(N){b(N[1].trim(),"blockquote");return}const P=n.match(/^\d+\.\s+(.*)$/);if(P){b(P[1].trim(),"ol-li");return}const S=n.match(/^[-*]\s+(.*)$/);if(S){b(S[1].trim(),"ul-li");return}b(n,"paragraph")},X=()=>{if(!e.length){m=void 0;return}o.push(Object.assign({type:a.code,text:e.join(`
|
|
4
|
+
`),markups:[]},m?{codeLang:m}:{}));e=[];m=void 0};for(const n of u){const I=n.trim().match(/^!\[([^\]]*)\]\(([^)\s]+)(?:\s+"[^"]*")?\)$/i),N=n.trim().match(/^<img\b[^>]*?\bsrc=["']([^"']+)["'][^>]*>$/i);if(I){d();o.push({image:!0,imageUrl:I[2],imageAlt:I[1]||""});continue}if(N){d();const S=n.match(/\balt=["']([^"']+)["']/i);o.push({image:!0,imageUrl:N[1],imageAlt:S&&S[1]||""});continue}const P=n.match(/^```(\S+)?\s*$/);if(P){if(p){X();p=!1}else{d();p=!0;m=P[1]||void 0}continue}if(p){e.push(n);continue}if(!n.trim()){d();continue}if(/^(#{1,4})\s+/.test(n)||/^>\s?/.test(n)||/^\d+\.\s+/.test(n)||/^[-*]\s+/.test(n)){d();l.push(n);d();continue}l.push(n.trim())}d();if(p)X();return o},v=async(t)=>{let a,u="image.png";if(/^data:/i.test(t)){a=s.dataUriToBlob(t);u="image."+(((a.type||"").split("/")[1]||"png").split("+")[0].split(";")[0]||"png")}else{const p=await fetch(t,{credentials:"omit"});if(!p.ok)throw Error("拉取图片失败 HTTP "+p.status);a=await p.blob();try{const d=new URL(t).pathname.split("/").filter(Boolean).pop()||"image.png";u=/\.[a-z0-9]+$/i.test(d)?d:d+".png"}catch(b){}}const o=new FormData;o.append("uploadedFile",a,u);const l=await fetch("https://medium.com/_/upload?is2x=true",{method:"POST",credentials:"include",headers:{accept:"application/json","x-requested-with":"XMLHttpRequest","x-xsrf-token":w},body:o}),m=JSON.parse(r(await l.text())),e=m&&m.payload&&m.payload.value||{};if(!e.fileId||!e.imgWidth||!e.imgHeight)throw Error("上传响应缺 fileId/尺寸");return{fileId:e.fileId,w:e.imgWidth,h:e.imgHeight}},x=[],h=[],j=await g("/new-story","POST",{}),q=j.json&&j.json.payload||{},f=q.id||q.value&&q.value.id;if(!f)return{ok:!1,stage:"create",status:j.status,message:"Medium 未返回草稿 postId:"+j.text.slice(0,160)};const F=await g("/_/api/posts/"+f+"/draft"),_=F.json&&F.json.payload||{},H=Array.isArray(_.normalizingDeltas)?_.normalizingDeltas:[],B=_.value&&typeof _.value.latestRev==="number"?_.value.latestRev:0;if(B<0&&H.length){const t=await g("/p/"+f+"/deltas","POST",{baseRev:B,deltas:H});if(t.status>=400)return{ok:!1,stage:"init",status:t.status,message:"初始化草稿失败"}}const D=await g("/_/api/posts/"+f),J=D.json&&D.json.payload&&D.json.payload.value||{},K=typeof J.latestRev==="number"?J.latestRev:0,T=i.params||{};let k=O(i.markdown||i.content||"");if(i.title&&k.length&&!k[0].image&&k[0].type===12&&(k[0].text||"").trim()===i.title.trim())k=k.slice(1);const R=[];if(i.title&&i.title.trim())R.push({type:3,text:i.title.trim(),markups:[]});if(T.subtitle&&String(T.subtitle).trim())R.push({type:4,text:String(T.subtitle).trim(),markups:[]});const Y=R.concat(k),L=[];let M=0;for(const t of Y){if(t.image){try{const a=await v(t.imageUrl);L.push({type:1,index:M,paragraph:{type:4,text:t.imageAlt||"",markups:[],layout:1,metadata:{id:a.fileId,originalWidth:a.w,originalHeight:a.h}}});x.push(t.imageUrl);M+=1}catch(a){h.push({src:t.imageUrl,error:String(a&&a.message||a)})}continue}L.push(Object.assign({type:1,index:M,paragraph:Object.assign({type:t.type,text:t.text,markups:t.markups||[]},t.codeLang?{codeLang:t.codeLang}:{})}));M+=1}if(!L.length)return{ok:!1,stage:"content",message:"正文为空,无可写入的段落"};const Z=h.length,E=await g("/p/"+f+"/deltas","POST",{baseRev:K,deltas:L});if(E.status>=400)return{ok:!1,stage:"write",status:E.status,message:"写入正文失败:"+E.text.slice(0,160),uploaded:x,failed:h};const z=Array.isArray(T.tags)?T.tags.filter(Boolean).slice(0,5):[];if(z.length){const t=await g("/_/graphql","POST",{operationName:"SetPostTagsMutation",query:`mutation SetPostTagsMutation($targetPostId: ID!, $tagNames: [String!]!) {
|
|
5
5
|
setPostTags(targetPostId: $targetPostId, tagNames: $tagNames) {
|
|
6
6
|
id
|
|
7
7
|
}
|
|
8
|
-
}`,variables:{targetPostId:f,tagNames:z}});if(t.status>=400||t.json&&t.json.errors)
|
|
8
|
+
}`,variables:{targetPostId:f,tagNames:z}});if(t.status>=400||t.json&&t.json.errors)h.push({src:"tags",error:"设置标签失败:"+(t.text||"").slice(0,120)})}if(T.canonicalUrl&&String(T.canonicalUrl).trim())await g("/_/graphql","POST",{operationName:"UpdateCanonicalUrl",query:`mutation UpdateCanonicalUrl($input: UpdateCanonicalUrlInput!) {
|
|
9
9
|
updateCanonicalUrl(input: $input) {
|
|
10
10
|
__typename
|
|
11
11
|
}
|
|
12
|
-
}`,variables:{input:{postId:f,url:String(T.canonicalUrl).trim()}}});const
|
|
12
|
+
}`,variables:{input:{postId:f,url:String(T.canonicalUrl).trim()}}});const W="https://medium.com/p/"+f+"/edit";if(Z&&!i.draftOnly)return{ok:!1,stage:"images",message:"正文图片转存到 Medium 失败,已保存为草稿、未发布(草稿:"+W+")",uploaded:x,failed:h};if(i.draftOnly)return{ok:!0,id:f,url:W,draft:!0,uploaded:x,failed:h};const V=await g("/p/"+f+"/publish","POST",{});if(V.status>=400)return{ok:!1,stage:"publish",status:V.status,message:"发布失败:"+V.text.slice(0,200),uploaded:x,failed:h};const C=await g("/_/api/posts/"+f),Q=C.json&&C.json.payload&&C.json.payload.value||{},tt=Q.mediumUrl||(Q.uniqueSlug?"https://medium.com/p/"+f:"https://medium.com/p/"+f);return{ok:!0,id:f,url:tt,draft:!1,uploaded:x,failed:h}}},mediumAuthProfile={home:"https://medium.com/",checkAuth:mediumProfile.checkAuth};function ct(i){if(!i.execute)throw new U("INVALID_INPUT","此命令需要 --execute 参数才能执行写操作。注意:本报错仅表示缺少 --execute,并未校验其余参数,不能当 dry-run 预览用")}async function ut(i){const s=typeof i.text==="string"?i.text:void 0,r=typeof i.file==="string"?i.file:void 0;if(s&&r)throw new U("INVALID_INPUT","不能同时指定正文参数和 --file,请二选一");let c=s??"";if(r){lt(r);let w;try{w=await nt(r)}catch{throw new U("INVALID_INPUT","文件不存在: "+r)}if(!w.isFile())throw new U("INVALID_INPUT","必须是可读的文本文件: "+r);let A;try{A=await st(r)}catch{throw new U("INVALID_INPUT","无法读取文件: "+r)}try{c=new TextDecoder("utf-8",{fatal:!0}).decode(A)}catch{throw new U("INVALID_INPUT","文件必须是 UTF-8 编码: "+r)}}if(!c.trim())throw new U("INVALID_INPUT","正文不能为空");return c}function mt(i,s,r,c,w={}){return[{status:"success",outcome:c,message:i,target_type:"article",target:s,...w}]}et({site:"medium",name:"article",access:"write",description:"发布 Medium 文章(走 medium.com 内部编辑器端点,非官方 API)。默认正式发布;加 --draft 仅存草稿。正文 Markdown,图片自动转存 Medium 图床。可选 --tags/--subtitle/--canonical-url。",domain:"medium.com",strategy:at.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题(作为 Medium 文章标题)"},{name:"text",positional:!0,help:"文章正文(Markdown)"},{name:"file",help:"正文文件路径(UTF-8,Markdown)"},{name:"tags",help:'Medium 话题标签,逗号分隔,最多 5 个(如 "AI,Programming,Open Source")'},{name:"subtitle",help:"副标题(可选,显示在标题下方)"},{name:"canonical-url",help:"原文规范链接(可选,内容首发别处时填,避免 SEO 重复)"},{name:"draft",type:"boolean",help:"仅保存草稿,不正式发布"},{name:"execute",type:"boolean",help:"确认执行写操作。不加此参数则拒绝写入。"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(i,s)=>{if(!i)throw new it("Medium 文章发布需要浏览器会话");ct(s);const r=String(s.title??"").trim();if(!r)throw new U("INVALID_INPUT","文章标题不能为空");const c=await ut(s),w=Boolean(s.draft),g={tags:String(s.tags??"").split(",").map((j)=>j.trim()).filter(Boolean),subtitle:typeof s.subtitle==="string"?s.subtitle:"",canonicalUrl:typeof s["canonical-url"]==="string"?s["canonical-url"]:""},y=await ot(i,{title:r,body:c,format:"markdown",draftOnly:w,profile:mediumProfile,publishParams:g}),O=y.images.uploaded.length|0,v=y.images.failed.length|0;let x=y.draft?"Medium 草稿已保存(可在编辑器内手动发布)":"Medium 文章已正式发布";if(O||v)x+=";图片:"+O+" 张已转存"+(v?","+v+" 张失败(失败明细见 images_failed)":"");x+=rt(y.images);const h=mt(x,"",y.url,y.draft?"draft":"publish",{created_target:"article:"+y.id,created_url:y.url});if(v){h[0].status="partial";h[0].images_failed=JSON.stringify(y.images.failed)}return h}});
|
package/clis/toutiao/article.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{CliError as p,CommandExecutionError as D}from"@jackwener/opencli/errors";import{cli as j,Strategy as F}from"@jackwener/opencli/registry";import{formatImageReport as V,publishArticle as M}from"../_shared/article/publish.js";import{readFile as O,stat as R}from"node:fs/promises";import{rememberArticleFile as H}from"../_shared/article/images.js";function q(
|
|
1
|
+
import{CliError as p,CommandExecutionError as D}from"@jackwener/opencli/errors";import{cli as j,Strategy as F}from"@jackwener/opencli/registry";import{formatImageReport as V,publishArticle as M}from"../_shared/article/publish.js";import{readFile as O,stat as R}from"node:fs/promises";import{rememberArticleFile as H}from"../_shared/article/images.js";function q(c){if(!c.execute)throw new p("INVALID_INPUT","此头条号写操作需要 --execute 参数才能真正发布。注意:本报错仅表示缺少 --execute,并未校验其余参数,不能当 dry-run 预览用")}async function K(c){const i=typeof c.text==="string"?c.text:void 0,t=typeof c.file==="string"?c.file:void 0;if(i&&t)throw new p("INVALID_INPUT","正文和 --file 只能选一个");let r=i??"";if(t){H(t);let u;try{u=await R(t)}catch{throw new p("INVALID_INPUT",`找不到文件: ${t}`)}if(!u.isFile())throw new p("INVALID_INPUT",`--file 必须是可读文本文件: ${t}`);let g;try{g=await O(t)}catch{throw new p("INVALID_INPUT",`文件无法读取: ${t}`)}try{r=new TextDecoder("utf-8",{fatal:!0}).decode(g)}catch{throw new p("INVALID_INPUT",`文件必须是 UTF-8 编码: ${t}`)}}if(!r.trim())throw new p("INVALID_INPUT","正文不能为空");return r}function z(c,i,t,r,u={}){return[{status:"success",outcome:r,message:c,target_type:i,target:t,...u}]}export const toutiaoProfile={home:"https://mp.toutiao.com/profile_v3/graphic/publish",outputFormat:"html",preprocessConfig:{},publish:async(c,i)=>{const t=c.params||{},r=[],u=[];async function g(e,l){const s="/spice/image?upload_source="+l+"&need_enhance=true&aid=1231&device_platform=web",o=new FormData;let a;if(/^https?:/i.test(e)){o.append("imageUrl",e);a=await fetch(s+"&scene=paste",{method:"POST",credentials:"include",body:o})}else{const y=await fetch(e,{credentials:"omit"});if(!y.ok)throw Error("图片下载失败 HTTP "+y.status+":"+e.slice(0,80));const U=await y.blob(),E=((U.type||"").split("/")[1]||"jpg").split("+")[0].split(";")[0]||"jpg";o.append("image",U,"image."+E);a=await fetch(s,{method:"POST",credentials:"include",body:o})}const m=await a.json(),w=m&&m.data;if(!m||m.code!==0||!w||!w.image_uri)throw Error((m&&m.message||"图片上传失败")+(m&&m.code!=null?"(接口 code "+m.code+")":""));return{uri:w.image_uri,url:w.image_url,width:w.image_width,height:w.image_height}}async function f(e){const l=new FormData;l.append("imageUrl",e);const o=await(await fetch("/spice/image?upload_source=20020003&aid=1231&device_platform=web&need_cover_url=1",{method:"POST",credentials:"include",body:l})).json(),a=o&&o.data;if(!o||o.code!==0||!a||!a.image_uri)throw Error((o&&o.message||"封面图生成失败")+(o&&o.code!=null?"(接口 code "+o.code+")":""));return{uri:a.image_uri,url:a.image_url,width:a.image_width,height:a.image_height}}const _=document.createElement("div");_.innerHTML=c.content;const b=Array.prototype.slice.call(_.querySelectorAll("img"));let d=null;for(const e of b){const l=e.getAttribute("src")||"";if(!l){e.remove();continue}let s;try{s=await g(l,20020002)}catch(y){u.push({src:l.slice(0,120),key:i.refKey(l),error:String(y&&y.message||y)});e.remove();continue}r.push({src:l.slice(0,120),key:i.refKey(l),url:s.url});if(!d)d=s;const o=document.createElement("div");o.className="pgc-img";const a=document.createElement("img");a.setAttribute("src",s.url);a.setAttribute("class","");a.setAttribute("image_type","");a.setAttribute("mime_type","");a.setAttribute("web_uri",s.uri);a.setAttribute("img_width",String(s.width));a.setAttribute("img_height",String(s.height));const m=document.createElement("p");m.className="pgc-img-caption";o.appendChild(a);o.appendChild(m);(e.parentElement||_).replaceChild(o,e)}if(u.length)return{ok:!1,stage:"images",message:"正文图片转存到头条图床失败,已在提交前中止,未发布",uploaded:r,failed:u};const P=_.innerHTML,S=[];let h=null,I="";if(t.cover&&String(t.cover).trim())try{h=await g(String(t.cover),20020002);r.push({src:String(t.cover).slice(0,120),key:i.refKey(String(t.cover)),url:h.url})}catch(e){return{ok:!1,stage:"cover",message:"封面图转存失败:"+String(e&&e.message||e),uploaded:r,failed:u}}else if(d){h=d;I=d.uri}if(h){let e;try{e=await f(h.url)}catch(s){return{ok:!1,stage:"cover",message:"封面图生成失败:"+String(s&&s.message||s),uploaded:r,failed:u}}const l={url:e.url,uri:e.uri,width:e.width,height:e.height,thumb_width:e.width,thumb_height:e.height,natural_width:h.width,natural_height:h.height,back_up_url:h.url,need_check:!1,paste:!1,auto_upload:!1,show_optimize_icon:!1,ic:!1,id:0,pos:0};if(I)l.extra={from_content:"1",from_content_uri:I};S.push(l)}await fetch("https://mp.toutiao.com/profile_v4/graphic/publish",{credentials:"include"});function k(){const e=document.cookie.match(/passport_csrf_token=([^;]+)/);return e?e[1]:""}const T=k(),x=new URLSearchParams({title:c.title,article_ad_type:"2",article_type:"0",from_diagnosis:"0",origin_debut_check_pgc_normal:"0",tree_plan_article:"0",save:"1",pgc_id:"0",content:P,pgc_feed_covers:JSON.stringify(S)}),A={"Content-Type":"application/x-www-form-urlencoded"};if(T)A["X-CSRFToken"]=T;const N=await fetch("https://mp.toutiao.com/mp/agw/article/publish?source=mp&type=article&aid=1231",{method:"POST",credentials:"include",headers:A,body:x});let n=null;try{n=await N.json()}catch(e){}const L=n&&(n.code===0||n.err_no===0),v=n&&n.data&&n.data.pgc_id;if(!L||!v||String(v)==="0"){let e=n&&(n.message||n.reason)||"头条号发布失败";if(n&&(n.code===7112||n.err_no===7112))e+="(code 7112「图片URI异常」:正文图或封面图不是头条编辑器图床里的资源)";if(n&&(n.code===7050||n.err_no===7050))e+="(code 7050「保存失败」:内容可能被风控拦截或账号被平台限制发文,"+"请登录 mp.toutiao.com 查看站内通知)";return{ok:!1,stage:"publish",status:N.status,message:e,detail:n,uploaded:r,failed:u}}const C="https://www.toutiao.com/article/"+String(v)+"/";return{ok:!0,id:String(v),url:C,editUrl:"https://mp.toutiao.com/profile_v4/graphic/publish?pgc_id="+String(v),draft:!1,uploaded:r,failed:u}},checkAuth:async(c)=>{try{const t=await(await fetch("https://mp.toutiao.com/mp/agw/media/get_media_info",{credentials:"include"})).json(),r=t&&t.data&&t.data.user;if(t&&t.code===0&&r&&r.id)return{isAuthenticated:!0,userId:String(r.id),username:r.screen_name||r.name||"",avatar:r.https_avatar_url||r.avatar_url||""};return{isAuthenticated:!1}}catch(i){return{isAuthenticated:!1,error:String(i&&i.message||i)}}}};j({site:"toutiao",name:"article",access:"write",description:"正式发布头条号图文文章(会立即公开,头条无可靠的存草稿接口,故不支持草稿)。正文默认 Markdown,图片自动转存到头条图床。标题需 2~30 字;封面可选(--cover,不传则头条自动从正文图选封面)。",domain:"mp.toutiao.com",strategy:F.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题(2~30 字)"},{name:"text",positional:!0,help:"文章正文(默认 Markdown;传 --html 则视为原始 HTML)"},{name:"file",help:"正文文件路径(UTF-8,默认 Markdown)"},{name:"html",type:"boolean",help:"将正文视为原始 HTML 而非 Markdown"},{name:"cover",help:"封面图 URL 或本机路径(可选;会先转存到头条图床)"},{name:"execute",type:"boolean",help:"真正执行写操作。不加此参数时命令拒绝写入。"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(c,i)=>{if(!c)throw new D("头条号文章发布需要浏览器会话");q(i);const t=String(i.title??"").trim();if(!t)throw new p("INVALID_INPUT","文章标题不能为空");if(t.length<2||t.length>30)throw new p("INVALID_INPUT",`头条号标题需 2~30 字,当前 ${t.length} 字`);const r=await K(i),g={cover:typeof i.cover==="string"?i.cover.trim():""},f=await M(c,{title:t,body:r,format:i.html?"html":"markdown",profile:toutiaoProfile,publishParams:g}),_=f.images.uploaded.length|0,b=f.images.failed.length|0;let d="已正式发布头条号文章";if(_||b)d+=`(图片:${_} 张已转存${b?`,${b} 张失败`:""})`;d+=V(f.images);return z(d,"article","","created",{created_target:"article:"+f.id,created_url:f.url})}});
|
package/clis/yuque/article.js
CHANGED
|
@@ -1 +1 @@
|
|
|
1
|
-
import{CliError as
|
|
1
|
+
import{CliError as l,CommandExecutionError as R}from"@jackwener/opencli/errors";import{cli as V,Strategy as j}from"@jackwener/opencli/registry";import{formatImageReport as C,publishArticle as E}from"../_shared/article/publish.js";import{rememberArticleFile as F}from"../_shared/article/images.js";export const yuqueProfile={home:"https://www.yuque.com/dashboard",outputFormat:"markdown",preprocessConfig:null,image:null,publish:async(i,o)=>{const m=o.cookie("yuque_ctoken");if(!m)return{ok:!1,stage:"auth",message:"未检测到语雀登录态,请先在 Chrome 中登录语雀(缺少 yuque_ctoken cookie)"};const s={"Content-Type":"application/json","x-csrf-token":m},p=await fetch("https://www.yuque.com/api/mine/common_used",{method:"GET",credentials:"include",headers:s});let t=null;try{t=await p.json()}catch(a){}if(!p.ok||!t?.data?.books?.length)return{ok:!1,stage:"auth",status:p.status,message:"无法获取语雀知识库列表,请确认已登录语雀"};const r=t.data.books[0].target_id,h=await fetch("https://www.yuque.com/api/docs",{method:"POST",credentials:"include",headers:s,body:JSON.stringify({title:i.title,type:"Doc",format:"lake",book_id:r,status:0})});let c=null;try{c=await h.json()}catch(a){}if(!h.ok||!c?.data?.id)return{ok:!1,stage:"create",status:h.status,message:c?.message||"创建语雀草稿文档失败"};const e=c.data.id,k="https://www.yuque.com/go/doc/"+e+"/edit";let w=i.content;const I=["yuque.com","cdn.nlark.com"],_=/!\[([^\]]*)\]\(\s*<?([^)\s>]+)>?\s*\)/g,y=[];let T;while((T=_.exec(w))!==null)y.push({full:T[0],alt:T[1],src:T[2]});const g=[],d=[],A="https://www.yuque.com/api/upload/attach?attachable_type=Doc&attachable_id="+e+"&type=image";for(let a=0;a<y.length;a++){const u=y[a],f=u.src;if(!f)continue;if(f.indexOf("data:")===0)continue;if(I.some(function(n){return f.indexOf(n)!==-1}))continue;try{const n=await fetch(f,{credentials:"omit"});if(!n.ok)throw Error("图片下载失败 HTTP "+n.status);const L=await n.blob(),U=new FormData;U.append("file",L,"image.jpg");const P=await fetch(A,{method:"POST",credentials:"include",headers:{"x-csrf-token":m},body:U});let b=null;try{b=await P.json()}catch(B){}if(!P.ok||!b?.data?.url)throw Error(b&&b.message||"图片上传失败 HTTP "+P.status);const v=b.data.url;w=w.split(u.full).join("");g.push({src:f.slice(0,120),url:v})}catch(n){d.push({src:f.slice(0,120),error:String(n&&n.message||n)})}}const N=await fetch("https://www.yuque.com/api/docs/convert",{method:"POST",credentials:"include",headers:s,body:JSON.stringify({from:"markdown",to:"lake",content:w})});let x=null;try{x=await N.json()}catch(a){}if(!N.ok||!x?.data?.content)return{ok:!1,stage:"convert",status:N.status,message:"内容格式转换失败(markdown→lake)",id:String(e),uploaded:g,failed:d};const q=x.data.content,O='<div class="lake-content" typography="traditional">'+q+"</div>",D=await fetch("https://www.yuque.com/api/docs/"+e+"/content",{method:"PUT",credentials:"include",headers:s,body:JSON.stringify({format:"lake",body_asl:q,body:O,body_html:O,draft_version:0,sync_dynamic_data:!1,save_type:"auto",edit_type:"Lake"})});if(!D.ok){let a="";try{a=await D.text()}catch(u){}return{ok:!1,stage:"save",status:D.status,message:"保存正文失败:"+a.slice(0,300),id:String(e),uploaded:g,failed:d}}if(d.length&&!i.draftOnly)return{ok:!1,stage:"images",message:"正文图片转存到语雀失败,已保存为草稿、未发布(草稿:"+k+")",id:String(e),url:k,uploaded:g,failed:d};if(!i.draftOnly){const a=await fetch("https://www.yuque.com/api/docs/"+e+"/publish",{method:"PUT",credentials:"include",headers:s,body:JSON.stringify({status:1})});if(!a.ok){let u="";try{u=await a.text()}catch(f){}return{ok:!1,stage:"publish",status:a.status,message:"发布失败:"+u.slice(0,300),id:String(e),url:k,uploaded:g,failed:d}}}return{ok:!0,id:String(e),url:k,draft:Boolean(i.draftOnly),uploaded:g,failed:d}}};V({site:"yuque",name:"article",access:"write",description:"发布语雀文章(知识库文档)。正文默认为 Markdown,外链图片自动转存到语雀图床。",domain:"www.yuque.com",strategy:j.COOKIE,browser:!0,args:[{name:"title",positional:!0,required:!0,help:"文章标题"},{name:"text",positional:!0,help:"文章正文(Markdown 格式;或用 --file 读文件)"},{name:"file",help:"正文文件路径(UTF-8,Markdown 格式)"},{name:"html",type:"boolean",help:"把正文当 HTML 输入(语雀仍转 lake 发布)"},{name:"draft",type:"boolean",help:"仅保存草稿,不发布"},{name:"execute",type:"boolean",help:"实际执行写操作。不加此参数命令拒绝写入。"}],columns:["status","outcome","message","target_type","target","created_target","created_url"],func:async(i,o)=>{if(!i)throw new R("语雀文章发布需要浏览器会话");if(!o.execute)throw new l("INVALID_INPUT","此命令需要加 --execute 才会实际写入(防误操作)");const m=String(o.title??"").trim();if(!m)throw new l("INVALID_INPUT","文章标题不能为空");let s="";const p=typeof o.text==="string"?o.text:void 0,t=typeof o.file==="string"?o.file:void 0;if(p&&t)throw new l("INVALID_INPUT","正文和 --file 不能同时使用");if(t){F(t);const{readFile:w,stat:I}=await import("node:fs/promises");let _;try{_=await I(t)}catch{throw new l("INVALID_INPUT",`文件不存在:${t}`)}if(!_.isFile())throw new l("INVALID_INPUT",`路径不是文件:${t}`);let y;try{y=await w(t)}catch{throw new l("INVALID_INPUT",`文件无法读取:${t}`)}try{s=new TextDecoder("utf-8",{fatal:!0}).decode(y)}catch{throw new l("INVALID_INPUT",`文件无法以 UTF-8 解码:${t}`)}}else s=p??"";if(!s.trim())throw new l("INVALID_INPUT","正文不能为空");const S=Boolean(o.draft),r=await E(i,{title:m,body:s,format:o.html?"html":"markdown",draftOnly:S,profile:yuqueProfile}),h=r.images.uploaded.length|0,c=r.images.failed.length|0;let e=r.draft?"已保存语雀草稿":"语雀文章发布成功";if(h||c)e+=`(图片:${h} 张已转存${c?`,${c} 张失败`:""})`;e+=C(r.images);return[{status:"success",outcome:r.draft?"draft":"created",message:e,target_type:"article",target:"",created_target:"article:"+r.id,created_url:r.url}]}});
|
|
@@ -48,6 +48,8 @@ export interface RunExtractOptions {
|
|
|
48
48
|
selector: string | null;
|
|
49
49
|
start: number;
|
|
50
50
|
chunkSize: number;
|
|
51
|
+
/** Keep only markdown lines matching this case-insensitive regex (before chunking). */
|
|
52
|
+
grep?: string | null;
|
|
51
53
|
}
|
|
52
54
|
export interface RunExtractResult {
|
|
53
55
|
url: string;
|
|
@@ -59,6 +61,11 @@ export interface RunExtractResult {
|
|
|
59
61
|
end: number;
|
|
60
62
|
next_start_char: number | null;
|
|
61
63
|
content: string;
|
|
64
|
+
/** Present only with `--grep`: the pattern, matched / total line counts, and the unfiltered page length. */
|
|
65
|
+
grep?: string;
|
|
66
|
+
matched_lines?: number;
|
|
67
|
+
total_lines?: number;
|
|
68
|
+
page_chars?: number;
|
|
62
69
|
}
|
|
63
70
|
/** End-to-end host-side pipeline: HTML → markdown → chunked envelope. */
|
|
64
71
|
export declare function runExtractFromHtml(opts: RunExtractOptions): RunExtractResult;
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import{htmlToMarkdown as f}from"../utils.js";const
|
|
2
|
-
const sel = ${
|
|
1
|
+
import{htmlToMarkdown as f}from"../utils.js";const d=20000,h=100,m=200000,g=0.15;export function buildExtractHtmlJs(t){return`(() => {
|
|
2
|
+
const sel = ${t?JSON.stringify(t):"null"};
|
|
3
3
|
let root = null;
|
|
4
4
|
if (sel) {
|
|
5
5
|
try { root = document.querySelector(sel); }
|
|
@@ -36,7 +36,9 @@ import{htmlToMarkdown as f}from"../utils.js";const u=20000,d=100,m=200000,S=0.15
|
|
|
36
36
|
n = walker.nextNode();
|
|
37
37
|
}
|
|
38
38
|
return { ok: true, url: location.href, title: document.title || '', html: clone.outerHTML || '' };
|
|
39
|
-
})()`}export function chunkMarkdown(
|
|
39
|
+
})()`}export function chunkMarkdown(t){const{content:r,start:e}=t,a=Math.max(h,Math.min(m,t.chunkSize));if(e>=r.length)return{content:"",start:e,end:e,nextStartChar:null};const n=Math.min(r.length,e+a);if(n===r.length)return{content:r.slice(e,n),start:e,end:n,nextStartChar:null};const s=Math.max(1,Math.floor(a*g)),o=Math.max(e+1,n-s),c=r.slice(o,n),l=c.lastIndexOf(`
|
|
40
40
|
|
|
41
|
-
`);let
|
|
42
|
-
`);if(
|
|
41
|
+
`);let i=n;if(l>=0)i=o+l+2;else{const u=c.lastIndexOf(`
|
|
42
|
+
`);if(u>=0)i=o+u+1}return{content:r.slice(e,i),start:e,end:i,nextStartChar:i}}export function runExtractFromHtml(t){const r=f(t.html);let e=r,a={};if(t.grep){const s=new RegExp(t.grep,"i"),o=r.split(`
|
|
43
|
+
`).filter((l)=>l.trim().length>0),c=o.filter((l)=>s.test(l));e=c.join(`
|
|
44
|
+
`);a={grep:t.grep,matched_lines:c.length,total_lines:o.length,page_chars:r.length}}const n=chunkMarkdown({content:e,start:Math.max(0,t.start),chunkSize:t.chunkSize||d});return{url:t.url,title:t.title,selector:t.selector,total_chars:e.length,chunk_size:n.end-n.start,start:n.start,end:n.end,next_start_char:n.nextStartChar,content:n.content,...a}}export const __extractInternals={DEFAULT_CHUNK_SIZE:d,MIN_CHUNK_SIZE:h,MAX_CHUNK_SIZE:m};
|
package/dist/src/cli.js
CHANGED
|
@@ -1,5 +1,5 @@
|
|
|
1
1
|
import*as A from"node:fs";import*as ge from"node:os";import*as k from"node:path";import{fileURLToPath as fo}from"node:url";import{Command as Ne,InvalidArgumentError as Et,Option as go}from"commander";import ho from"js-yaml";import{findPackageRoot,getBuiltEntryCandidates as wo}from"./package-paths.js";import{fullName as He,getRegistry as Ee,strategyLabel as _t}from"./registry.js";import{serializeCommand as yo,formatArgSummary as bo}from"./serialization.js";import{render as _e}from"./output.js";import{PKG_VERSION as ke}from"./version.js";import{printCompletionScript as xo}from"./completion.js";import{loadExternalClis as qe,executeExternalCli as So,installExternalCli as vo,registerExternalCli as Ro,isBinaryInstalled as Ve,formatExternalCliLabel as Ot}from"./external.js";import{listOpenCliSkills as Co,readOpenCliSkill as Eo}from"./skills.js";import{registerAllCommands as _o}from"./commanderAdapter.js";import{classifyAdapter as $t,formatRootAdapterHelpText as Oo,installCommanderNamespaceStructuredHelp as he,installStructuredHelp as $o,leadingPositionalFromUsage as No,rootHelpData as ko}from"./help.js";import{EXIT_CODES as f,getErrorMessage as J,BrowserConnectError as Ao,CliError as Xe,CommandExecutionError as F,toEnvelope as To}from"./errors.js";import{TargetError as Nt}from"./browser/target-errors.js";import{resolveTargetJs as Io,getTextResolvedJs as Po,getValueResolvedJs as Jo,getAttributesResolvedJs as jo,selectResolvedJs as Mo,isAutocompleteResolvedJs as Do}from"./browser/target-resolver.js";import{wrapTopLevelAwait as Uo}from"./browser/utils.js";import{buildFindJs as Go,buildSemanticFindJs as kt,isFindError as At}from"./browser/find.js";import{inferShape as Tt}from"./browser/shape.js";import{assignKeys as It}from"./browser/network-key.js";import{DEFAULT_TTL_MS as Pt,findEntry as Fo,loadNetworkCache as Bo,saveNetworkCache as Lo}from"./browser/network-cache.js";import{NETWORK_INTERCEPTOR_JS as Ae}from"./browser/network-interceptor.js";import{parseFilter as zo,shapeMatchesFilter as Wo}from"./browser/shape-filter.js";import{buildHtmlTreeJs as Ho}from"./browser/html-tree.js";import{buildExtractHtmlJs as qo,runExtractFromHtml as Vo}from"./browser/extract.js";import{analyzeSite as Xo}from"./browser/analyze.js";import{registerAuthCommands as Ko}from"./commands/auth.js";import{daemonRestart as Zo,daemonStatus as Yo,daemonStop as Qo,daemonWarm as er}from"./commands/daemon.js";import{log as Q}from"./logger.js";import{bindTab as tr,BrowserCommandError as Jt,sendCommand as or}from"./browser/daemon-client.js";import{fetchDaemonStatus as rr}from"./browser/daemon-transport.js";import{aliasForContextId as jt,listManagedProfiles as Ke,loadProfileConfig as nr,renameProfile as ir,resolveProfile as Mt,resolveProfileContextId as sr,setDefaultProfile as ar}from"./browser/profile.js";import{createManagedProfile as cr,ensureManagedChrome as Ze,isManagedChromeRunning as lr,removeManagedProfile as dr,stopManagedChrome as ur,resolveManagedOpts as Dt,updateManagedProfileConfig as mr,resolveChromeExecutable as pr}from"./browser/managed-chrome.js";import{testProxy as fr,parseProxyUrl as Ye}from"./browser/proxy-test.js";import{formatDaemonVersion as gr,isDaemonStale as hr}from"./browser/daemon-version.js";import{DEFAULT_BROWSER_CONNECT_TIMEOUT as Qe}from"./browser/config.js";import{isRemoteExecution as wr}from"./runtime.js";import{budgetStateText as yr,DEFAULT_STATE_MAX_CHARS as Ut}from"./browser/state-budget.js";const et=fo(import.meta.url),br='Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"',Gt=1000;function Te(c,l){if(c===void 0||c===null||c==="")return null;const a=String(c).trim(),p=/^(\d+(?:\.\d+)?)(ms|s|m|h)?$/.exec(a);if(!p)return{error:`--${l} must be a duration like 500ms, 30s, 2m, got "${a}"`};const v=Number.parseFloat(p[1]),h=p[2]??"ms";return Math.round(v*(h==="h"?3600000:h==="m"?60000:h==="s"?1000:1))}function tt(c){return typeof c==="number"&&Number.isFinite(c)&&c>0?c:Date.now()}function we(c){if(typeof c!=="number"||!Number.isFinite(c)||c<=0)return;return new Date(c).toISOString()}function ot(c,l,a=Date.now()){const p=l.sinceMs!=null?a-l.sinceMs:void 0,v=l.untilMs!=null?a-l.untilMs:void 0;return c.filter((h)=>{const T=h.timestamp??a;if(p!==void 0&&T<p)return!1;if(v!==void 0&&T>v)return!1;return!0})}export function selectFreshByTimestamp(c,l){const a=c.filter((v)=>Number(v.timestamp??0)>l),p=a.length>0?Math.max(l,...a.map((v)=>Number(v.timestamp??0)).filter(Number.isFinite)):l;return{fresh:a,lastSeenTs:p}}async function ye(c){if(c.readNetworkCapture){const a=await c.readNetworkCapture();if(Array.isArray(a)&&a.length>0)return a.map((p)=>{const v=p.responsePreview??null;let h=null;if(v)try{h=JSON.parse(v)}catch{h=v}const T=typeof p.responseBodyFullSize==="number"?p.responseBodyFullSize:v?v.length:0,E=p.responseBodyTruncated===!0;return{url:p.url||"",method:p.method||"GET",status:p.responseStatus||0,size:T,ct:p.responseContentType||"",body:h,bodyFullSize:T,bodyTruncated:E,timestamp:tt(p.timestamp),...p.channel?{channel:p.channel}:{},...p.direction?{direction:p.direction}:{},...p.eventName?{eventName:String(p.eventName)}:{},...p.eventId?{eventId:String(p.eventId)}:{}}})}const l=await c.evaluate("(function(){ var out = window.__opencli_net || []; window.__opencli_net = []; return JSON.stringify(out); })()");try{return JSON.parse(l).map((p)=>({...p,timestamp:tt(p.timestamp)}))}catch{if(process.env.OPENCLI_VERBOSE)Q.warn(`[network] Failed to parse interceptor buffer: ${typeof l==="string"?l.slice(0,200):String(l)}`);return[]}}const Ft=4000;function xr(c){if(typeof c!=="string")return c??null;return c.length>Ft?`${c.slice(0,Ft)}…[truncated ${c.length} chars]`:c}function Bt(c){return c.filter((l)=>{if(l.channel==="websocket"||l.channel==="eventsource")return!0;const a=l.ct?.toLowerCase()??"";return(a.includes("json")||a.includes("xml")||a.includes("text/plain")||a.includes("javascript"))&&!/\.(js|css|png|jpg|gif|svg|woff|ico|map)(\?|$)/i.test(l.url)&&!/analytics|tracking|telemetry|beacon|pixel|gtag|fbevents/i.test(l.url)})}const Sr={invalid_args:f.USAGE_ERROR,invalid_filter:f.USAGE_ERROR,invalid_max_body:f.USAGE_ERROR};function Z(c,l,a={}){console.log(JSON.stringify({error:{code:c,message:l,...a}},null,2));process.exitCode=Sr[c]??f.GENERIC_ERROR}const vr="Site sitemap available. For navigation context, use the opencli-browser-sitemap skill; treat browser state as truth if it disagrees.";function Rr(c,l=Ee()){let a;try{a=new URL(c).hostname.toLowerCase().replace(/^www\./,"")}catch{return[]}const p=new Map;for(const E of l.values()){if(!E.domain)continue;let N=E.domain.toLowerCase().trim();try{N=new URL(N.includes("://")?N:`https://${N}`).hostname.toLowerCase()}catch{N=N.split("/")[0]??N}N=N.replace(/^www\./,"");if(!N)continue;if(a===N||a.endsWith(`.${N}`))p.set(E.site,Math.max(p.get(E.site)??0,N.length))}const v=[...p.entries()].sort((E,N)=>N[1]-E[1]||E[0].localeCompare(N[0])).map(([E])=>E),h=a.split(".").filter(Boolean),T=h.length>=2?h[h.length-2]:h[0];return[...new Set([...v,...T?[T]:[]])]}function Lt(c,l){return c.find((a)=>l(a))}function Cr(c,l){const a=c.replace(/[^a-zA-Z0-9_-]+/g,"-");if(!a)return{};const p=k.join(l.homeDir,".ppcli","sites",a);return{local:Lt([k.join(p,"sitemap"),k.join(p,"sitemap.md")],l.fileExists),global:Lt([k.join(l.packageRoot,"sitemaps",a),k.join(l.packageRoot,"sitemaps",`${a}.md`)],l.fileExists)}}export function resolveSitemapAvailabilityForUrl(c,l={}){const a=l.homeDir??ge.homedir(),p=l.packageRoot??findPackageRoot(et),v=l.registry??Ee(),h=l.fileExists??A.existsSync;for(const T of Rr(c,v)){const E=Cr(T,{homeDir:a,packageRoot:p,fileExists:h});if(!E.local&&!E.global)continue;const N=E.local&&E.global?"local+global":E.local?"local":"global";return{site:T,available:!0,source:N,hint:vr,paths:E}}return null}function zt(c){const l=c.replace(/[^a-zA-Z0-9_-]+/g,"_");return k.join(Ht(),"browser-sitemap-hints",`${l}.json`)}function Wt(c){try{const l=JSON.parse(A.readFileSync(zt(c),"utf-8"));if(l&&typeof l==="object"&&Array.isArray(l.seenSites))return{seenSites:l.seenSites.filter((a)=>typeof a==="string"),updatedAt:typeof l.updatedAt==="string"?l.updatedAt:new Date(0).toISOString()}}catch{}return{seenSites:[],updatedAt:new Date(0).toISOString()}}function Er(c,l){const a=Wt(c);if(!a.seenSites.includes(l))a.seenSites.push(l);const p=zt(c);A.mkdirSync(k.dirname(p),{recursive:!0});A.writeFileSync(p,JSON.stringify({seenSites:a.seenSites,updatedAt:new Date().toISOString()}),"utf-8")}function _r(c,l,a){const p=resolveSitemapAvailabilityForUrl(c);if(!p)return null;if(!a.oncePerSession)return p;if(Wt(l).seenSites.includes(p.site))return null;Er(l,p.site);return p}export function checkSiteMemory(c){const l=k.join(ge.homedir(),".ppcli","sites",c),a=k.join(l,"endpoints.json"),p=k.join(l,"notes.md");let v=0,h=A.existsSync(a);if(h)try{const E=JSON.parse(A.readFileSync(a,"utf-8"));if(E&&typeof E==="object"&&!Array.isArray(E))v=Object.keys(E).length;else if(Array.isArray(E))v=E.length}catch{h=!1}const T=A.existsSync(p);return{ok:h&&v>0&&T,siteDir:l,endpoints:{present:h,count:v,path:a},notes:{present:T,path:p}}}export function printSiteMemoryReport(c,l){if(c.ok){console.log(` ✓ Memory: endpoints.json (${c.endpoints.count}), notes.md present at ${c.siteDir}`);return}const a=l?"✗":"⚠",p=[];if(!c.endpoints.present)p.push("endpoints.json");else if(c.endpoints.count===0)p.push("endpoints.json (empty)");if(!c.notes.present)p.push("notes.md");console.log(` ${a} Memory: missing ${p.join(", ")} under ${c.siteDir}`);console.log(" Write the endpoint you just verified + a 1-line session note so the next agent starts from minute 0, not minute 95.");if(!l)console.log(" (Re-run with --strict-memory to fail instead of warn.)")}export function normalizeVerifyRows(c){if(Array.isArray(c))return c.map((l)=>l&&typeof l==="object"?l:{value:l});if(c&&typeof c==="object"){const l=c;for(const a of["rows","items","data","results"])if(Array.isArray(l[a]))return l[a].map((p)=>p&&typeof p==="object"?p:{value:p});return[l]}return[]}export function renderVerifyPreview(c,l={}){const a=l.maxRows??10,p=l.maxCols??6,v=l.cellMax??40;if(c.length===0)return" (no rows)";const h=Array.from(new Set(c.flatMap((P)=>Object.keys(P)))),T=h.slice(0,p),E=c.slice(0,a),N=(P)=>{if(P===null||P===void 0)return"";return(typeof P==="object"?JSON.stringify(P):String(P)).replace(/\s+/g," ").slice(0,v)},te=T.map((P)=>Math.max(P.length,...E.map((K)=>N(K[P]).length))),Y=(P)=>P.map((K,x)=>K.padEnd(te[x])).join(" "),X=[];X.push(` ${Y(T)}`);X.push(` ${te.map((P)=>"-".repeat(P)).join(" ")}`);for(const P of E)X.push(` ${Y(T.map((K)=>N(P[K])))}`);if(c.length>a)X.push(` ... and ${c.length-a} more row(s)`);if(h.length>p)X.push(` (${h.length-p} more column(s) hidden)`);return X.join(`
|
|
2
|
-
`)}function Ht(){return process.env.OPENCLI_CACHE_DIR||k.join(ge.homedir(),".ppcli","cache")}function qt(c){const l=c.replace(/[^a-zA-Z0-9_-]+/g,"_");return k.join(Ht(),"browser-state",`${l}.json`)}function Vt(c){try{const l=A.readFileSync(qt(c),"utf-8"),a=JSON.parse(l);return a&&typeof a==="object"?a:null}catch{return null}}function be(c,l){const a=qt(l);if(!c){A.rmSync(a,{force:!0});return}A.mkdirSync(k.dirname(a),{recursive:!0});A.writeFileSync(a,JSON.stringify({defaultPage:c,updatedAt:new Date().toISOString()}),"utf-8")}function Or(c,l){return c.some((a)=>{return typeof a==="object"&&a!==null&&"page"in a&&typeof a.page==="string"&&a.page===l})}async function rt(c,l,a){const p=l.trim();if(!p)return;let v;try{v=await c.tabs()}catch(h){if(a.source==="saved"){be(void 0,a.scope);return}throw Error(`Target tab ${p} could not be validated in the current browser session. The Browser Bridge session may have restarted; re-run "ppcli browser tab list" and choose a current target.`,{cause:h})}if(Array.isArray(v)&&Or(v,p))return p;if(a.source==="saved"){be(void 0,a.scope);return}throw Error(`Target tab ${p} is not part of the current browser session. The Browser Bridge session may have restarted; re-run "ppcli browser tab list" and choose a current target.`)}function Ie(c,l){return l?`${l}:${c}`:c}async function $r(c,l){const a=Vt(l)?.defaultPage?.trim();if(!a)return;return rt(c,a,{scope:l,source:"saved"})}async function Nr(c,l,a,p={}){if(p.adapter&&!/^site:[a-z0-9][a-z0-9_-]*$/.test(c))throw new F("--adapter requires the existing adapter session name: site:<site>, e.g. site:douyin");const v=p.adapter?"adapter":"browser",h=Mt(p.profile);if(h.kind==="managed"){if(l)throw Error(`托管环境「${h.name}」是单窗口复用标签模型,不支持 --tab 定位。直接省略 --tab 即可。`);const K=p.windowMode??nt(void 0,"background"),{ensureManagedChrome:x}=await import("./browser/managed-chrome.js"),{CDPBridge:Oe}=await import("./browser/cdp.js"),{generateFingerprintJs:le}=await import("./browser/fingerprint.js"),xe=await x(h.name,h.entry,{windowMode:K}),oe=h.entry.fingerprint,ie=h.entry.proxy,Se=new Oe;return{page:await Se.connect({timeout:Qe,session:c,surface:v,...p.adapter?{siteSession:"persistent"}:{},cdpEndpoint:xe,contextId:h.name,windowMode:K,...oe?.enabled?{initScript:le(oe)}:{},...ie?.username?{proxyAuth:{username:ie.username,password:ie.password??""}}:{}}),close:()=>Se.close()}}const{BrowserBridge:T}=await import("./browser/index.js"),E=new T,N=process.env.OPENCLI_BROWSER_IDLE_TIMEOUT,te=N?parseInt(N,10):void 0,Y=await E.connect({timeout:Qe,session:c,surface:v,...p.adapter?{siteSession:"persistent"}:{},...a&&{contextId:a},...te&&te>0&&{idleTimeout:te},windowMode:p.windowMode??nt(void 0,"background")}),X=Ie(p.adapter?`adapter:${c}`:c,a),P=l?await rt(Y,l,{scope:X,source:"explicit"}):await $r(Y,X);if(P){if(!Y.setActivePage)throw Error("This browser session does not support explicit tab targeting");Y.setActivePage(P)}return{page:Y,close:()=>E.close()}}function nt(c,l){if(wr())return"background";const a=Pe(c,"window");if(a!==void 0&&a!==""){if(a==="foreground"||a==="background")return a;throw Error(`--window must be one of: foreground, background. Received: "${String(a)}"`)}const p=process.env.OPENCLI_WINDOW;if(p!==void 0&&p!==""){if(p==="foreground"||p==="background")return p;throw Error(`OPENCLI_WINDOW must be one of: foreground, background. Received: "${p}"`)}return l}function C(c){return c.option("--tab <targetId>",br)}function kr(c){if(!c)return;const l=c.optsWithGlobals?c.optsWithGlobals():c.opts();return typeof l.tab==="string"&&l.tab.trim()?l.tab.trim():void 0}function Pe(c,l){let a=c;while(a){const p=a.opts();if(Object.prototype.hasOwnProperty.call(p,l)&&p[l]!==void 0)return p[l];a=a.parent}return}function it(c){const l=Pe(c,"session");if(typeof l==="string"&&l.trim())return l.trim();throw Error("<session> is a required positional argument: ppcli browser <session> <command>")}function Xt(c){return sr(st(c))}function st(c){const l=Pe(c,"profile");return typeof l==="string"&&l.trim()?l.trim():void 0}function at(c){const l=c.session;if(typeof l==="string"&&l.trim())return l.trim();throw Error("Browser page is missing a session")}function Je(c){const l=c.contextId;return Ie(at(c),typeof l==="string"&&l.trim()?l.trim():void 0)}function Ar(c){return typeof c==="string"?c:JSON.stringify(c,null,2)}function Tr(c,l){const a=Ar(c),p=a.match(/^interactive:\s*(\d+)\s*$/m);return{ok:!0,chars:a.length,bytes:Buffer.byteLength(a,"utf8"),lines:a?a.split(/\r?\n/).length:0,approx_tokens:Math.ceil(a.length/4),refs:(a.match(/(^|\n)\s*\[\d+\]/g)??[]).length,frame_sections:(a.match(/(^|\n)frame /g)??[]).length,...p?{interactive:Number(p[1])}:{},elapsed_ms:l}}async function Kt(c,l){const a=Date.now();try{const p=await c.snapshot({viewportExpand:2000,source:l});return Tr(p,Date.now()-a)}catch(p){return{ok:!1,elapsed_ms:Date.now()-a,error:{...p instanceof Error&&"code"in p?{code:String(p.code)}:{},message:p instanceof Error?p.message:String(p)}}}}function Zt(c,l){if(typeof c==="string"&&c.trim())return c.trim();const a=l instanceof Ne?l.opts().tab:l?.tab;if(typeof a==="string"&&a.trim())return a.trim();return}function Yt(c,l,a){if(c===void 0)return a;const p=parseInt(c,10);if(Number.isNaN(p)||p<=0){console.error(`[cli] Invalid ${l}="${c}", using default ${a}`);return a}return p}function Qt(c,l){if(!/^\d+$/.test(c))throw new Et(`--${l} must be a positive integer (got "${c}")`);const a=parseInt(c,10);if(a<=0)throw new Et(`--${l} must be a positive integer (got "${c}")`);return a}function Ir(c){if(c.verbose)process.env.OPENCLI_VERBOSE="1"}function Pr(c){return[...new Set(c.commands.map((l)=>l.name()))].sort((l,a)=>l.localeCompare(a)).join(", ")}function Jr(c){for(const l of c.commands){if(l.commands.length===0)continue;const a=Pr(l);if(a)l.description(a)}}export function createProgram(c,l){const a=new Ne;a.name("ppcli").description("Make any website your CLI. Zero setup. AI-powered.").version(ke).option("--profile <name>","Chrome profile/context alias for Browser Bridge commands").enablePositionalOptions();a.command("list").description("List all available CLI commands").option("-f, --format <fmt>","Output format: table, json, yaml, md, csv","table").action((e)=>{const t=Ee(),o=[...new Set(t.values())].sort((
|
|
2
|
+
`)}function Ht(){return process.env.OPENCLI_CACHE_DIR||k.join(ge.homedir(),".ppcli","cache")}function qt(c){const l=c.replace(/[^a-zA-Z0-9_-]+/g,"_");return k.join(Ht(),"browser-state",`${l}.json`)}function Vt(c){try{const l=A.readFileSync(qt(c),"utf-8"),a=JSON.parse(l);return a&&typeof a==="object"?a:null}catch{return null}}function be(c,l){const a=qt(l);if(!c){A.rmSync(a,{force:!0});return}A.mkdirSync(k.dirname(a),{recursive:!0});A.writeFileSync(a,JSON.stringify({defaultPage:c,updatedAt:new Date().toISOString()}),"utf-8")}function Or(c,l){return c.some((a)=>{return typeof a==="object"&&a!==null&&"page"in a&&typeof a.page==="string"&&a.page===l})}async function rt(c,l,a){const p=l.trim();if(!p)return;let v;try{v=await c.tabs()}catch(h){if(a.source==="saved"){be(void 0,a.scope);return}throw Error(`Target tab ${p} could not be validated in the current browser session. The Browser Bridge session may have restarted; re-run "ppcli browser tab list" and choose a current target.`,{cause:h})}if(Array.isArray(v)&&Or(v,p))return p;if(a.source==="saved"){be(void 0,a.scope);return}throw Error(`Target tab ${p} is not part of the current browser session. The Browser Bridge session may have restarted; re-run "ppcli browser tab list" and choose a current target.`)}function Ie(c,l){return l?`${l}:${c}`:c}async function $r(c,l){const a=Vt(l)?.defaultPage?.trim();if(!a)return;return rt(c,a,{scope:l,source:"saved"})}async function Nr(c,l,a,p={}){if(p.adapter&&!/^site:[a-z0-9][a-z0-9_-]*$/.test(c))throw new F("--adapter requires the existing adapter session name: site:<site>, e.g. site:douyin");const v=p.adapter?"adapter":"browser",h=Mt(p.profile);if(h.kind==="managed"){if(l)throw Error(`托管环境「${h.name}」是单窗口复用标签模型,不支持 --tab 定位。直接省略 --tab 即可。`);const K=p.windowMode??nt(void 0,"background"),{ensureManagedChrome:x}=await import("./browser/managed-chrome.js"),{CDPBridge:Oe}=await import("./browser/cdp.js"),{generateFingerprintJs:le}=await import("./browser/fingerprint.js"),xe=await x(h.name,h.entry,{windowMode:K}),oe=h.entry.fingerprint,ie=h.entry.proxy,Se=new Oe;return{page:await Se.connect({timeout:Qe,session:c,surface:v,...p.adapter?{siteSession:"persistent"}:{},cdpEndpoint:xe,contextId:h.name,windowMode:K,...oe?.enabled?{initScript:le(oe)}:{},...ie?.username?{proxyAuth:{username:ie.username,password:ie.password??""}}:{}}),close:()=>Se.close()}}const{BrowserBridge:T}=await import("./browser/index.js"),E=new T,N=process.env.OPENCLI_BROWSER_IDLE_TIMEOUT,te=N?parseInt(N,10):void 0,Y=await E.connect({timeout:Qe,session:c,surface:v,...p.adapter?{siteSession:"persistent"}:{},...a&&{contextId:a},...te&&te>0&&{idleTimeout:te},windowMode:p.windowMode??nt(void 0,"background")}),X=Ie(p.adapter?`adapter:${c}`:c,a),P=l?await rt(Y,l,{scope:X,source:"explicit"}):await $r(Y,X);if(P){if(!Y.setActivePage)throw Error("This browser session does not support explicit tab targeting");Y.setActivePage(P)}return{page:Y,close:()=>E.close()}}function nt(c,l){if(wr())return"background";const a=Pe(c,"window");if(a!==void 0&&a!==""){if(a==="foreground"||a==="background")return a;throw Error(`--window must be one of: foreground, background. Received: "${String(a)}"`)}const p=process.env.OPENCLI_WINDOW;if(p!==void 0&&p!==""){if(p==="foreground"||p==="background")return p;throw Error(`OPENCLI_WINDOW must be one of: foreground, background. Received: "${p}"`)}return l}function C(c){return c.option("--tab <targetId>",br)}function kr(c){if(!c)return;const l=c.optsWithGlobals?c.optsWithGlobals():c.opts();return typeof l.tab==="string"&&l.tab.trim()?l.tab.trim():void 0}function Pe(c,l){let a=c;while(a){const p=a.opts();if(Object.prototype.hasOwnProperty.call(p,l)&&p[l]!==void 0)return p[l];a=a.parent}return}function it(c){const l=Pe(c,"session");if(typeof l==="string"&&l.trim())return l.trim();throw Error("<session> is a required positional argument: ppcli browser <session> <command>")}function Xt(c){return sr(st(c))}function st(c){const l=Pe(c,"profile");return typeof l==="string"&&l.trim()?l.trim():void 0}function at(c){const l=c.session;if(typeof l==="string"&&l.trim())return l.trim();throw Error("Browser page is missing a session")}function Je(c){const l=c.contextId;return Ie(at(c),typeof l==="string"&&l.trim()?l.trim():void 0)}function Ar(c){return typeof c==="string"?c:JSON.stringify(c,null,2)}function Tr(c,l){const a=Ar(c),p=a.match(/^interactive:\s*(\d+)\s*$/m);return{ok:!0,chars:a.length,bytes:Buffer.byteLength(a,"utf8"),lines:a?a.split(/\r?\n/).length:0,approx_tokens:Math.ceil(a.length/4),refs:(a.match(/(^|\n)\s*\[\d+\]/g)??[]).length,frame_sections:(a.match(/(^|\n)frame /g)??[]).length,...p?{interactive:Number(p[1])}:{},elapsed_ms:l}}async function Kt(c,l){const a=Date.now();try{const p=await c.snapshot({viewportExpand:2000,source:l});return Tr(p,Date.now()-a)}catch(p){return{ok:!1,elapsed_ms:Date.now()-a,error:{...p instanceof Error&&"code"in p?{code:String(p.code)}:{},message:p instanceof Error?p.message:String(p)}}}}function Zt(c,l){if(typeof c==="string"&&c.trim())return c.trim();const a=l instanceof Ne?l.opts().tab:l?.tab;if(typeof a==="string"&&a.trim())return a.trim();return}function Yt(c,l,a){if(c===void 0)return a;const p=parseInt(c,10);if(Number.isNaN(p)||p<=0){console.error(`[cli] Invalid ${l}="${c}", using default ${a}`);return a}return p}function Qt(c,l){if(!/^\d+$/.test(c))throw new Et(`--${l} must be a positive integer (got "${c}")`);const a=parseInt(c,10);if(a<=0)throw new Et(`--${l} must be a positive integer (got "${c}")`);return a}function Ir(c){if(c.verbose)process.env.OPENCLI_VERBOSE="1"}function Pr(c){return[...new Set(c.commands.map((l)=>l.name()))].sort((l,a)=>l.localeCompare(a)).join(", ")}function Jr(c){for(const l of c.commands){if(l.commands.length===0)continue;const a=Pr(l);if(a)l.description(a)}}export function createProgram(c,l){const a=new Ne;a.name("ppcli").description("Make any website your CLI. Zero setup. AI-powered.").version(ke).option("--profile <name>","Chrome profile/context alias for Browser Bridge commands").enablePositionalOptions();a.command("list").description("List all available CLI commands").option("-f, --format <fmt>","Output format: table, json, yaml, md, csv","table").action((e)=>{const t=Ee(),o=[...new Set(t.values())].sort((u,g)=>He(u).localeCompare(He(g))),r=e.format,n=r==="json"||r==="yaml";if(r!=="table"){const u=n?o.map(yo):o.map((g)=>({command:He(g),site:g.site,name:g.name,aliases:g.aliases?.join(", ")??"",description:g.description,access:g.access,strategy:_t(g),browser:!!g.browser,args:bo(g.args)}));_e(u,{fmt:r,columns:["command","site","name","aliases","description","access","strategy","browser","args",...n?["columns","domain"]:[]],title:"ppcli/list",source:"ppcli list"});return}const i=new Map,s=new Map;for(const u of o){const g=$t(u.domain)==="app"?i:s,b=g.get(u.site)??[];b.push(u);g.set(u.site,b)}const d=(u,g)=>{console.log(` ${u}`);for(const b of g){const R=_t(b),I=R==="public"?"[public]":`[${R}]`,G=b.aliases?.length?` (aliases: ${b.aliases.join(", ")})`:"";console.log(` ${b.name} ${I}${G}${b.description?` — ${b.description}`:""}`)}console.log()};console.log();console.log(" ppcli"+" — available commands");console.log();if(i.size>0){console.log(" App adapters");console.log();for(const[u,g]of i)d(u,g)}if(s.size>0){console.log(" Site adapters");console.log();for(const[u,g]of s)d(u,g)}const m=qe();if(m.length>0){console.log(" external CLIs");for(const u of m){const b=Ve(u.binary)?"[installed]":"[auto-install]";console.log(` ${Ot(u)} ${b}${u.description?` — ${u.description}`:""}`)}console.log()}console.log(` ${o.length} built-in commands across ${i.size} apps + ${s.size} sites, ${m.length} external CLIs`);console.log()});a.command("validate").description("Validate CLI definitions").argument("[target]","site or site/name").action(async(e)=>{const{validateClisWithTarget:t,renderValidationReport:o}=await import("./validate.js");console.log(o(t([c,l],e)))});a.command("verify").description("Validate + smoke test").argument("[target]").option("--smoke","Run smoke tests",!1).action(async(e,t)=>{const{verifyClis:o,renderVerifyReport:r}=await import("./verify.js"),n=await o({builtinClis:c,userClis:l,target:e,smoke:t.smoke});console.log(r(n));process.exitCode=n.ok?f.SUCCESS:f.GENERIC_ERROR});const p=a.command("skills").description("Read bundled ppcli skills");p.command("list").description("List bundled ppcli-* skills").option("-f, --format <fmt>","Output format: table, json, yaml, md, csv","table").action((e)=>{const t=Co();_e(t,{fmt:e.format,fmtExplicit:!!e.format,columns:["name","description","version","path"],title:"ppcli/skills/list",source:"ppcli skills list"})});p.command("read").description("Print an ppcli-* skill's SKILL.md or reference file").argument("<skill>","Skill name, or skill/path like opencli-browser/references/foo.md").argument("[path]","Path under the skill directory").option("--json","Output a JSON envelope instead of raw markdown",!1).action((e,t,o)=>{let r;try{r=Eo(e,t??"")}catch(n){console.error(`Error: ${J(n)}`);if(n instanceof Xe&&n.hint)console.error(`Hint: ${n.hint}`);process.exitCode=n instanceof Xe?n.exitCode:f.GENERIC_ERROR;return}if(o.json){console.log(JSON.stringify(r,null,2));return}process.stdout.write(r.content);if(!r.content.endsWith(`
|
|
3
3
|
`))process.stdout.write(`
|
|
4
4
|
`)});const v=Ko(a);a.command("convention-audit").description("Scan adapters for agent-native convention violations").argument("[target]","site or site/name").option("--site <site>","Limit audit to one site").option("-f, --format <fmt>","Output format: table, json, yaml","table").option("--strict","Exit non-zero when violations are found",!1).action(async(e,t)=>{const{runConventionAudit:o,renderConventionAuditText:r}=await import("./convention-audit.js"),n=o({projectRoot:findPackageRoot(et),target:e,site:t.site}),i=String(t.format??"table").toLowerCase();if(i==="json"||i==="yaml"||i==="yml")_e(n,{fmt:i});else console.log(r(n));if(t.strict&&!n.ok)process.exitCode=f.GENERIC_ERROR});const h=a.command("browser").addOption(new go("--session <name>","Internal — set automatically from the <session> positional").hideHelp()).option("--window <mode>","Browser window mode: foreground or background").description("Browser control — navigate, click, type, extract, wait (no LLM needed)").usage("<session> <command> [options]").addHelpText("after",`
|
|
5
5
|
<session> is a required positional: pass the name of the browser session every subcommand should operate on. Reuse the same name across calls to keep the tab/state alive; pick a different name to isolate parallel browser work.
|
|
@@ -11,7 +11,7 @@ Examples:
|
|
|
11
11
|
$ ppcli browser work state
|
|
12
12
|
$ ppcli browser work bind
|
|
13
13
|
$ ppcli browser work unbind
|
|
14
|
-
`),T=h.description();async function E(e,t,o={}){const r=await e.evaluate(Io(t,o));if(!r.ok)throw new Nt({code:r.code,message:r.message,hint:r.hint,candidates:r.candidates,matches_n:r.matches_n});return{matches_n:r.matches_n,match_level:r.match_level}}function N(e){if(e===void 0||e===null||e==="")return null;const t=String(e);if(!/^\d+$/.test(t))return{error:`--nth must be a non-negative integer, got "${t}"`};return Number.parseInt(t,10)}function te(e){console.log(JSON.stringify({error:{code:e.code,message:e.message,hint:e.hint,...e.candidates&&{candidates:e.candidates},...e.matches_n!==void 0&&{matches_n:e.matches_n}}},null,2))}function Y(e){return e.toLowerCase().includes("javascript dialog")}function X(e){console.log(JSON.stringify({error:{code:"javascript_dialog_open",message:e,hint:"Handle the modal first: ppcli browser dialog accept (or dismiss). Use --text for prompt dialogs."}},null,2))}function P(e){if(!e.code)return;console.log(JSON.stringify({error:{code:e.code,message:e.message,...e.hint?{hint:e.hint}:{}}},null,2))}function K(e){Q.error(e.message);if(e.hint)Q.error(`Hint: ${e.hint}`)}function x(e){return async(...t)=>{let o=null;try{const r=t.at(-1)instanceof Ne?t.at(-1):void 0,n=kr(r),i=it(r),s=Xt(r),d=nt(r,"background");o=await Nr(i,n,s,{windowMode:d,profile:st(r),adapter:Pe(r,"adapter")===!0});await e(o.page,...t)}catch(r){if(r instanceof Ao){Q.error(r.message);if(r.hint)Q.error(`Hint: ${r.hint}`)}else if(r instanceof Jt){if(Y(r.message))X(r.message);else P(r);K(r)}else if(r instanceof Nt){te(r);Q.error(`[${r.code}] ${r.message}`);if(r.hint)Q.error(`Hint: ${r.hint}`)}else{const n=J(r);if(Y(n)){X(n);Q.error(n)}else if(n.includes("attach failed")||n.includes("chrome-extension://"))Q.error("Browser attach failed — another extension may be interfering. Try disabling 1Password.");else Q.error(n)}process.exitCode=f.GENERIC_ERROR}finally{await o?.close()}}}function Oe(e,t){return async(o,r)=>{const n=o instanceof Ne?o:r,i=it(n),s=Xt(n);try{if(t?.managedGuardMessage&&Mt(st(n)).kind==="managed")throw Error(t.managedGuardMessage);const{BrowserBridge:d}=await import("./browser/index.js");await new d().connect({timeout:Qe,session:i,surface:"browser",...s&&{contextId:s}});await e({session:i,contextId:s})}catch(d){if(d instanceof Jt){P(d);K(d)}else Q.error(d instanceof Error?d.message:String(d));process.exitCode=f.GENERIC_ERROR}}}h.command("bind").description("Bind the current Chrome tab/window to the browser session named by <session>").action(Oe(async({session:e,contextId:t})=>{const o=await tr(e,{...t&&{contextId:t}});be(void 0,Ie(e,t));console.log(JSON.stringify({session:e,...o&&typeof o==="object"?o:{data:o}},null,2))},{managedGuardMessage:"托管环境不支持 bind:它是独立自动化窗口,直接用 browser open/eval 等命令即可。"}));h.command("unbind").description("Detach the bound browser session named by <session> without closing the user tab/window").action(Oe(async({session:e,contextId:t})=>{await or("close-window",{session:e,surface:"browser",...t&&{contextId:t}});be(void 0,Ie(e,t));console.log(JSON.stringify({unbound:!0,session:e},null,2))},{managedGuardMessage:"托管环境不支持 unbind:它没有绑定到扩展的会话。"}));const le=h.command("tab").description("Tab management — list, create, and close tabs in the browser session");le.command("list").description("List tabs in the browser session with target IDs").action(x(async(e)=>{const t=await e.tabs();console.log(JSON.stringify(t,null,2))}));le.command("new").argument("[url]","Optional URL to open in the new tab").description("Create a new tab and print its target ID").action(x(async(e,t)=>{if(!e.newTab)throw Error("This browser session does not support creating tabs");const o=await e.newTab(t);console.log(JSON.stringify({page:o,url:t??null},null,2))}));C(le.command("select").argument("[targetId]",'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"').description("Select a tab by target ID and make it the default browser tab")).action(x(async(e,t,o)=>{const r=Zt(t,o);if(!r)throw Error("Target tab required. Pass it as an argument or --tab <targetId>.");await e.selectTab(r);be(r,Je(e));console.log(JSON.stringify({selected:r},null,2))}));C(le.command("close").argument("[targetId]",'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"').description("Close a tab by target ID")).action(x(async(e,t,o)=>{const r=Zt(t,o);if(!e.closeTab)throw Error("This browser session does not support closing tabs");if(!r)throw Error("Target tab required. Pass it as an argument or --tab <targetId>.");const n=await rt(e,r,{scope:Je(e),source:"explicit"});if(!n)throw Error(`Target tab ${r} is not part of the current browser session.`);await e.closeTab(n);const i=Je(e);if(Vt(i)?.defaultPage===n)be(void 0,i);console.log(JSON.stringify({closed:n},null,2))}));C(h.command("open").argument("<url>").description("Open URL in the browser session")).action(x(async(e,t,o)=>{const r=await e.startNetworkCapture?.()??!1;await e.goto(t);await e.wait(2);if(!r)try{await e.evaluate(Ae)}catch{}const n=await e.getCurrentUrl?.()??t,i=_r(n,Je(e),{oncePerSession:!0});console.log(JSON.stringify({url:n,...e.getActivePage?.()?{page:e.getActivePage?.()}:{},...i?{sitemap:i}:{}},null,2))}));C(h.command("back").description("Go back in browser history")).action(x(async(e,t)=>{await e.evaluate("history.back()");await e.wait(2);console.log("Navigated back")}));C(h.command("scroll").argument("<direction>","up or down").option("--amount <pixels>","Pixels to scroll","500")).description("Scroll page").action(x(async(e,t,o)=>{if(t!=="up"&&t!=="down"){console.error(`Invalid direction "${t}". Use "up" or "down".`);process.exitCode=f.USAGE_ERROR;return}await e.scroll(t,parseInt(o.amount,10));console.log(`Scrolled ${t}`)}));C(h.command("state").description("Page state: URL, title, interactive elements with [N] indices").option("--source <source>","Snapshot backend: dom (default) or ax prototype","dom").option("--compare-sources","Print DOM vs AX snapshot metrics for observation promotion decisions",!1).option("--max-chars <n>","Output budget in characters; past it the tree is cut with a note on how to reach the rest (0 = no limit)",String(Ut))).action(x(async(e,t,o)=>{if(t.compareSources===!0){const[u
|
|
14
|
+
`),T=h.description();async function E(e,t,o={}){const r=await e.evaluate(Io(t,o));if(!r.ok)throw new Nt({code:r.code,message:r.message,hint:r.hint,candidates:r.candidates,matches_n:r.matches_n});return{matches_n:r.matches_n,match_level:r.match_level}}function N(e){if(e===void 0||e===null||e==="")return null;const t=String(e);if(!/^\d+$/.test(t))return{error:`--nth must be a non-negative integer, got "${t}"`};return Number.parseInt(t,10)}function te(e){console.log(JSON.stringify({error:{code:e.code,message:e.message,hint:e.hint,...e.candidates&&{candidates:e.candidates},...e.matches_n!==void 0&&{matches_n:e.matches_n}}},null,2))}function Y(e){return e.toLowerCase().includes("javascript dialog")}function X(e){console.log(JSON.stringify({error:{code:"javascript_dialog_open",message:e,hint:"Handle the modal first: ppcli browser dialog accept (or dismiss). Use --text for prompt dialogs."}},null,2))}function P(e){if(!e.code)return;console.log(JSON.stringify({error:{code:e.code,message:e.message,...e.hint?{hint:e.hint}:{}}},null,2))}function K(e){Q.error(e.message);if(e.hint)Q.error(`Hint: ${e.hint}`)}function x(e){return async(...t)=>{let o=null;try{const r=t.at(-1)instanceof Ne?t.at(-1):void 0,n=kr(r),i=it(r),s=Xt(r),d=nt(r,"background");o=await Nr(i,n,s,{windowMode:d,profile:st(r),adapter:Pe(r,"adapter")===!0});await e(o.page,...t)}catch(r){if(r instanceof Ao){Q.error(r.message);if(r.hint)Q.error(`Hint: ${r.hint}`)}else if(r instanceof Jt){if(Y(r.message))X(r.message);else P(r);K(r)}else if(r instanceof Nt){te(r);Q.error(`[${r.code}] ${r.message}`);if(r.hint)Q.error(`Hint: ${r.hint}`)}else{const n=J(r);if(Y(n)){X(n);Q.error(n)}else if(n.includes("attach failed")||n.includes("chrome-extension://"))Q.error("Browser attach failed — another extension may be interfering. Try disabling 1Password.");else Q.error(n)}process.exitCode=f.GENERIC_ERROR}finally{await o?.close()}}}function Oe(e,t){return async(o,r)=>{const n=o instanceof Ne?o:r,i=it(n),s=Xt(n);try{if(t?.managedGuardMessage&&Mt(st(n)).kind==="managed")throw Error(t.managedGuardMessage);const{BrowserBridge:d}=await import("./browser/index.js");await new d().connect({timeout:Qe,session:i,surface:"browser",...s&&{contextId:s}});await e({session:i,contextId:s})}catch(d){if(d instanceof Jt){P(d);K(d)}else Q.error(d instanceof Error?d.message:String(d));process.exitCode=f.GENERIC_ERROR}}}h.command("bind").description("Bind the current Chrome tab/window to the browser session named by <session>").action(Oe(async({session:e,contextId:t})=>{const o=await tr(e,{...t&&{contextId:t}});be(void 0,Ie(e,t));console.log(JSON.stringify({session:e,...o&&typeof o==="object"?o:{data:o}},null,2))},{managedGuardMessage:"托管环境不支持 bind:它是独立自动化窗口,直接用 browser open/eval 等命令即可。"}));h.command("unbind").description("Detach the bound browser session named by <session> without closing the user tab/window").action(Oe(async({session:e,contextId:t})=>{await or("close-window",{session:e,surface:"browser",...t&&{contextId:t}});be(void 0,Ie(e,t));console.log(JSON.stringify({unbound:!0,session:e},null,2))},{managedGuardMessage:"托管环境不支持 unbind:它没有绑定到扩展的会话。"}));const le=h.command("tab").description("Tab management — list, create, and close tabs in the browser session");le.command("list").description("List tabs in the browser session with target IDs").action(x(async(e)=>{const t=await e.tabs();console.log(JSON.stringify(t,null,2))}));le.command("new").argument("[url]","Optional URL to open in the new tab").description("Create a new tab and print its target ID").action(x(async(e,t)=>{if(!e.newTab)throw Error("This browser session does not support creating tabs");const o=await e.newTab(t);console.log(JSON.stringify({page:o,url:t??null},null,2))}));C(le.command("select").argument("[targetId]",'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"').description("Select a tab by target ID and make it the default browser tab")).action(x(async(e,t,o)=>{const r=Zt(t,o);if(!r)throw Error("Target tab required. Pass it as an argument or --tab <targetId>.");await e.selectTab(r);be(r,Je(e));console.log(JSON.stringify({selected:r},null,2))}));C(le.command("close").argument("[targetId]",'Target tab/page identity returned by "browser open", "browser tab new", or "browser tab list"').description("Close a tab by target ID")).action(x(async(e,t,o)=>{const r=Zt(t,o);if(!e.closeTab)throw Error("This browser session does not support closing tabs");if(!r)throw Error("Target tab required. Pass it as an argument or --tab <targetId>.");const n=await rt(e,r,{scope:Je(e),source:"explicit"});if(!n)throw Error(`Target tab ${r} is not part of the current browser session.`);await e.closeTab(n);const i=Je(e);if(Vt(i)?.defaultPage===n)be(void 0,i);console.log(JSON.stringify({closed:n},null,2))}));C(h.command("open").argument("<url>").description("Open URL in the browser session")).action(x(async(e,t,o)=>{const r=await e.startNetworkCapture?.()??!1;await e.goto(t);await e.wait(2);if(!r)try{await e.evaluate(Ae)}catch{}const n=await e.getCurrentUrl?.()??t,i=_r(n,Je(e),{oncePerSession:!0});console.log(JSON.stringify({url:n,...e.getActivePage?.()?{page:e.getActivePage?.()}:{},...i?{sitemap:i}:{}},null,2))}));C(h.command("back").description("Go back in browser history")).action(x(async(e,t)=>{await e.evaluate("history.back()");await e.wait(2);console.log("Navigated back")}));C(h.command("scroll").argument("<direction>","up or down").option("--amount <pixels>","Pixels to scroll","500")).description("Scroll page").action(x(async(e,t,o)=>{if(t!=="up"&&t!=="down"){console.error(`Invalid direction "${t}". Use "up" or "down".`);process.exitCode=f.USAGE_ERROR;return}await e.scroll(t,parseInt(o.amount,10));console.log(`Scrolled ${t}`)}));C(h.command("state").description("Page state: URL, title, interactive elements with [N] indices").option("--source <source>","Snapshot backend: dom (default) or ax prototype","dom").option("--compare-sources","Print DOM vs AX snapshot metrics for observation promotion decisions",!1).option("--max-chars <n>","Output budget in characters; past it the tree is cut with a note on how to reach the rest (0 = no limit)",String(Ut))).action(x(async(e,t,o)=>{if(t.compareSources===!0){const[m,u]=await Promise.all([Kt(e,"dom"),Kt(e,"ax")]);console.log(JSON.stringify({url:await e.getCurrentUrl?.()??"",sources:{dom:m,ax:u}},null,2));return}const r=String(t.source??"dom").toLowerCase();if(r!=="dom"&&r!=="ax"){console.log(JSON.stringify({error:{code:"invalid_source",message:`--source must be "dom" or "ax", got "${t.source}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=Number(t.maxChars??Ut);if(!Number.isInteger(n)||n<0){console.log(JSON.stringify({error:{code:"invalid_max_chars",message:`--max-chars must be a non-negative integer, got "${t.maxChars}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const i=await e.snapshot({viewportExpand:2000,source:r}),s=await e.getCurrentUrl?.()??"",d=typeof i==="string"?i:JSON.stringify(i,null,2);console.log(`URL: ${s}
|
|
15
15
|
`);console.log(yr(d,n,it(o)))}));C(h.command("frames").description("List cross-origin iframe targets in snapshot order")).action(x(async(e)=>{const t=await e.frames?.()??[];console.log(JSON.stringify(t,null,2))}));C(h.command("screenshot").argument("[path]","Save to file (base64 if omitted)")).option("--full-page","Capture the full scrollable page, not just the viewport",!1).option("--annotate","Overlay visible browser state ref labels on the screenshot",!1).option("--width <n>","Override viewport width in CSS pixels for this screenshot only",(e)=>Qt(e,"width")).option("--height <n>","Override viewport height in CSS pixels for this screenshot only (ignored with --full-page)",(e)=>Qt(e,"height")).option("--scroll-to <selector>","Scroll this element into view (centered) before capturing").description("Take screenshot").action(x(async(e,t,o)=>{if(o.scrollTo){const i=JSON.stringify(String(o.scrollTo));if(!await e.evaluate(`(() => { const el = document.querySelector(${i}); if (!el) return false; el.scrollIntoView({ block: 'center', inline: 'center' }); return true; })()`))throw new F(`--scroll-to 没匹配到元素: ${o.scrollTo}`);await e.wait(150)}const r={fullPage:o.fullPage===!0,annotate:o.annotate===!0,width:o.width,height:o.height},n=o.annotate===!0?(e.annotatedScreenshot??e.screenshot).bind(e):e.screenshot.bind(e);if(t){await n({...r,path:t});console.log(`Screenshot saved to: ${t}`)}else console.log(await n({...r,format:"png"}))}));const xe=['iframe[src*="/verifycenter/captcha/"]',".js-mocaptcha-img",".moclickcaptcha-img",".moclickcaptcha-draw",".geetest_item_img",".geetest_widget canvas",".captcha-image img",".captcha img",".captcha canvas",'img[src*="captcha" i]','img[class*="captcha" i]','canvas[class*="captcha" i]'],oe=6;function ie(e){const t=e&&e.trim()?[e.trim()]:xe;return`(() => {
|
|
16
16
|
var sels = ${JSON.stringify(t)};
|
|
17
17
|
var el = null, used = '';
|
|
@@ -71,7 +71,7 @@ Examples:
|
|
|
71
71
|
}
|
|
72
72
|
} catch (e) {}
|
|
73
73
|
return { captcha_present: present, status_text: status, url: (location.href || '').slice(0, 100) };
|
|
74
|
-
})()`}function $e(e,t){const o=Math.max(0,e.x-oe),r=Math.max(0,e.y-oe),n=Math.min(t.w,e.x+e.w+oe)-o,i=Math.min(t.h,e.y+e.h+oe)-r;if(n<=0||i<=0)throw new F("验证码区域不在可见视口内,请重新截图定位");return{x:o,y:r,w:n,h:i}}const je="__pp_captcha_grid";function eo(e){const t=Math.min(e.w,e.h)>=240?50:Math.min(e.w,e.h)>=100?25:10,o=[],r=(i,s,d,
|
|
74
|
+
})()`}function $e(e,t){const o=Math.max(0,e.x-oe),r=Math.max(0,e.y-oe),n=Math.min(t.w,e.x+e.w+oe)-o,i=Math.min(t.h,e.y+e.h+oe)-r;if(n<=0||i<=0)throw new F("验证码区域不在可见视口内,请重新截图定位");return{x:o,y:r,w:n,h:i}}const je="__pp_captcha_grid";function eo(e){const t=Math.min(e.w,e.h)>=240?50:Math.min(e.w,e.h)>=100?25:10,o=[],r=(i,s,d,m)=>`<text x="${i}" y="${s}" text-anchor="${m}" font-size="10" font-family="sans-serif" fill="#e00" stroke="#fff" stroke-width="2.5" paint-order="stroke">${d}</text>`;for(let i=t;i<e.w;i+=t)o.push(`<line x1="${i}" y1="0" x2="${i}" y2="${e.h}"/>`,r(i,10,i,"middle"));for(let i=t;i<e.h;i+=t)o.push(`<line x1="0" y1="${i}" x2="${e.w}" y2="${i}"/>`,r(2,i+3,i,"start"));const n=`<svg width="${e.w}" height="${e.h}" style="display:block"><g stroke="rgba(255,0,0,.35)" stroke-width="1">${o.join("")}</g></svg>`;return`(() => {
|
|
75
75
|
document.getElementById(${JSON.stringify(je)})?.remove();
|
|
76
76
|
const div = document.createElement('div');
|
|
77
77
|
div.id = ${JSON.stringify(je)};
|
|
@@ -79,7 +79,7 @@ Examples:
|
|
|
79
79
|
div.innerHTML = ${JSON.stringify(n)};
|
|
80
80
|
document.documentElement.appendChild(div);
|
|
81
81
|
return new Promise((r) => requestAnimationFrame(() => requestAnimationFrame(() => r(true))));
|
|
82
|
-
})()`}async function Me(e,t){const o=await e.evaluate(ie(t));if(!o||!o.found||!o.rect)return null;return{crop:$e(o.rect,o.viewport),scroll:o.scroll??{x:0,y:0},viewport:o.viewport}}function re(e,t,o){const r=String(e??"").split(",").map((n)=>Number(n.trim()));if(r.length!==t||r.some((n)=>!Number.isFinite(n)||n<0))throw new F(`${o} 应为 ${t} 个逗号分隔的非负数(crop 网格刻度,CSS 像素)`);return r}async function De(e,t,o,r){await new Promise((s)=>setTimeout(s,900));let n={};try{n=await e.evaluate(Se(t))}catch(s){n={read_error:J(s)}}const i=o?String(o):void 0;if(i)await e.screenshot({path:i,format:"png"});console.log(JSON.stringify({ok:!0,...r,after:n,...i?{saved:i}:{}},null,2))}C(h.command("captcha").argument("[selector]","验证码图片元素 CSS 选择器(缺省自动探测常见点选验证码)")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--save <path>","把全视口截图存到该路径(缺省存到临时 PNG 文件)").option("--scale <n>","验证码区域裁剪图的放大倍数(1..6,缺省 3;放大后认扭曲字更准)","3").option("--grid","在裁剪图上画坐标网格(刻度是裁剪图里的 CSS 像素),给 captcha-slide / captcha-rotate / captcha-drag 读位置用").description("抓取验证码:存整图 + 只含验证码的放大裁剪图 + 返回坐标与提示文字,供 AI 视觉识别后配合 captcha-click / captcha-slide / captcha-rotate / captcha-drag").action(x(async(e,t,o)=>{const r=await e.evaluate(ie(typeof t==="string"?t:void 0));if(!r||!r.found){console.log(JSON.stringify({error:{code:"captcha_not_found",message:'未探测到点选验证码图片。可显式传选择器,如 ppcli browser <s> captcha ".js-mocaptcha-img"',tried:xe}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=o?.save&&String(o.save)||k.join(ge.tmpdir(),`opencli-captcha-${Date.now()}.png`);await e.screenshot({format:"png",path:n});const i=r.rect,s=r.dpr||1,d=$e(i,r.viewport),u=Math.max(1,Math.min(6,Number(o?.scale??3)||3)),m=oe;let g=null;const b=e.cdp;if(typeof b==="function"){const R={x:d.x+(r.scroll?.x??0),y:d.y+(r.scroll?.y??0),width:d.w,height:d.h,scale:u};if(o?.grid)await e.evaluate(eo(d));const I=await b.call(e,"Page.captureScreenshot",{format:"jpeg",quality:82,clip:R,captureBeyondViewport:!1}).finally(()=>o?.grid?e.evaluate(`document.getElementById(${JSON.stringify(je)})?.remove()`):void 0);if(I&&I.data){g=n.replace(/\.png$/i,"")+".crop.jpg";A.writeFileSync(g,Buffer.from(I.data,"base64"))}}console.log(JSON.stringify({ok:!0,selector:r.selector,iframe:r.iframe===!0,crop_rect_css:g?d:null,prompt:r.prompt||"",crop:g,crop_scale:g?u:null,crop_pad:g?m:null,saved:n,rect_css:i,rect_screenshot_px:{x:Math.round(i.x*s),y:Math.round(i.y*s),w:Math.round(i.w*s),h:Math.round(i.h*s)},dpr:s,natural:r.natural,viewport:r.viewport,...o?.grid?{grid_guide:"网格刻度是 crop 里的 CSS 像素(左上角 0,0),captcha-slide / captcha-rotate / captcha-drag 的坐标都按刻度读,并传同一个 --in 选择器。"}:{},guide:"① read_local_file 打开 crop(只含验证码、已放大)认清每个待点字与顺序(注意会混入不在 prompt 里的干扰字,只点 prompt 要求的)。"+"② 坐标直接读成「在 crop 这张图里的比例」(0..1,左上角为 0,0)——不用管 pad/rect,看到字在 crop 的哪就填哪。"+"③ 执行:ppcli browser <session> captcha-click"+(o.adapter?" --adapter":"")+" --crop --in '"+r.selector.replace(/'/g,"'\\''")+"'"+' "x1,y1 x2,y2 ..."(--crop 表示坐标是 crop 图比例;空格分隔,顺序即点击顺序)。'+"iframe 的提示与确认按钮请从截图读取;点完文字后可能仍需点确认。验证码仍在不代表可以重复点字;先截图检查。"},null,2))}));C(h.command("captcha-click").argument("<points>",'待点击坐标,空格分隔且顺序即点击顺序,如 "0.30,0.46 0.68,0.66"')).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--save <path>","点击后保存全视口截图,以核对点选标记、确认按钮及验证结果").option("--in <selector>","坐标映射基准元素(缺省自动探测验证码图片)").option("--crop","坐标按「captcha 裁剪图(crop)里的比例」解释(0..1,左上角为0,0)——AI 看哪张就按那张填,内部自动扣掉 pad。推荐").option("--px","坐标按「截图设备像素」解释(自动按 devicePixelRatio 换算成 CSS 像素)").option("--css","坐标按「CSS 视口像素」解释(直接点击,不做映射)").option("--delay <ms>","每次点击之间的间隔毫秒","250").description("按坐标依次点击(点选验证码用):把 AI 从图上读到的位置一次性点上,点完回读验证码是否还在/是否跳转。默认坐标为相对基准元素的比例(0..1),--crop 则为裁剪图比例").action(x(async(e,t,o)=>{const n=String(t??"").trim().split(/\s+/).filter(Boolean).map((y)=>{const S=y.split(",");if(S.length!==2||S.some((U)=>!U.trim()))return{x:NaN,y:NaN};return{x:Number(S[0]),y:Number(S[1])}});if(n.length===0||n.some((y)=>!Number.isFinite(y.x)||!Number.isFinite(y.y))){console.log(JSON.stringify({error:{code:"usage_error",message:'坐标格式应为 "x,y x,y ...",如 "0.30,0.46 0.68,0.66"'}},null,2));process.exitCode=f.USAGE_ERROR;return}if([o?.css,o?.px,o?.crop].filter(Boolean).length>1)throw new F("--css / --px / --crop 不能同时使用");const i=o?.css?"css":o?.px?"px":o?.crop?"crop":"frac";if(n.some((y)=>y.x<0||y.y<0||(i==="frac"||i==="crop")&&(y.x>1||y.y>1)))throw new F("相对坐标必须在 0..1 内,绝对坐标必须非负;尚未点击");const s=Number(o?.delay??250);if(!Number.isFinite(s)||s<0||s>1e4)throw new F("--delay 必须在 0..10000 毫秒内");let d={x:0,y:0,w:0,h:0},u=1,m;if(i==="css"||i==="px"){const y=await e.evaluate("({w: innerWidth, h: innerHeight, dpr: devicePixelRatio})");m=y;u=y.dpr}else{const y=await e.evaluate(ie(typeof o?.in==="string"?o.in:void 0));if(!y||!y.found||!y.rect){console.log(JSON.stringify({error:{code:"captcha_not_found",message:"captcha-click 找不到基准元素,请用 --in <selector> 指定,或用 --css 传绝对坐标"}},null,2));process.exitCode=f.USAGE_ERROR;return}d=y.rect;u=y.dpr||1;m=y.viewport}const g=i==="crop"?$e(d,m):d,b=(y)=>{if(i==="css")return y;if(i==="px")return{x:y.x/u,y:y.y/u};if(i==="crop")return{x:g.x+y.x*g.w,y:g.y+y.y*g.h};return{x:d.x+y.x*d.w,y:d.y+y.y*d.h}},{cdp:R,nativeClick:I}=e,G=n.map(b).map((y)=>({x:Math.round(y.x),y:Math.round(y.y)}));if(G.some((y)=>y.x<0||y.y<0||y.x>=m.w||y.y>=m.h))throw new F("点击坐标超出视口;尚未点击,请重新截图定位");if(typeof R!=="function"&&typeof I!=="function")throw new F("当前浏览器不支持原生坐标点击");const _=(y)=>new Promise((S)=>setTimeout(S,y));async function D(y,S,U,L){if(typeof R==="function"){const ae=12+L%4;for(let q=1;q<=ae;q++){const H=q/ae,ce=H*H*(3-2*H),fe=Math.sin(H*Math.PI)*10*(L%2===0?1:-1),M=U.x+(y-U.x)*ce,V=U.y+(S-U.y)*ce+fe;await R.call(e,"Input.dispatchMouseEvent",{type:"mouseMoved",x:Math.round(M),y:Math.round(V)});await _(8+q%3*6)}await R.call(e,"Input.dispatchMouseEvent",{type:"mouseMoved",x:y,y:S});await _(40+L%3*25);await R.call(e,"Input.dispatchMouseEvent",{type:"mousePressed",x:y,y:S,button:"left",clickCount:1});await _(45+L%4*20);await R.call(e,"Input.dispatchMouseEvent",{type:"mouseReleased",x:y,y:S,button:"left",clickCount:1});return"cdp"}if(typeof I==="function"){await I.call(e,y,S);return"native"}throw new F("当前浏览器不支持原生坐标点击")}const j=[];let W="",ne={x:Math.round(d.x+d.w/2)||400,y:Math.round(d.y-40)||200};for(let y=0;y<G.length;y++){const S=G[y],U=Math.round(S.x),L=Math.round(S.y);W=await D(U,L,ne,y);ne={x:U,y:L};j.push({x:U,y:L});if(y<n.length-1&&s>0)await _(s)}await _(700);let w={};try{w=await e.evaluate(Se(typeof o?.in==="string"?o.in:void 0))}catch(y){w={read_error:J(y)}}const O=o.save?String(o.save):void 0;if(O)await e.screenshot({path:O,format:"png"});console.log(JSON.stringify({ok:!0,mode:i,method:W,clicked_n:j.length,points_css:j,after:w,...O?{saved:O}:{}},null,2))}));const Ue='与 captcha 相同的验证码元素选择器(缺省自动探测;抖音为 iframe[src*="/verifycenter/captcha/"])';C(h.command("captcha-slide")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--picture <x,y,w,h>","带拼图和缺口的那张图片的区域(crop 网格刻度)").option("--gap <px>","目测拼图大概要往右挪多远(可选,只用来缩小搜索范围)").option("--save <path>","松手后保存全视口截图").description("缺口拼图(「拖动滑块填充拼图」):按住先拖一小段,自己量拖动比例、找缺口,拖到位纠偏后松手").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i,s,d,u]=re(t.picture,4,"--picture");if(d<20||u<20)throw new F("--picture 太小:框的应是整张带缺口的图片");const m=t.gap===void 0?void 0:Number(t.gap);if(m!==void 0&&!Number.isFinite(m))throw new F("--gap 必须是数字");const g=await Me(e,o);if(!g)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const{solveSlidePuzzle:b}=await import("./browser/captcha-drag.js"),R=await b(e,{x:g.crop.x+r,y:g.crop.y+n,clip:{x:g.crop.x+i+g.scroll.x,y:g.crop.y+s+g.scroll.y,width:d,height:u},hint:m});if(!R)throw new F("没认出拼图和缺口,已拖回原位未提交:重新 captcha --grid 看清 --picture 框的是不是整张图");await De(e,o,t.save,R)}));C(h.command("captcha-rotate")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--track <px>","把手大概能往右拖多远(工具会自己量轨道,量不出才用它)").requiredOption("--center <x,y>","圆图圆心(crop 网格刻度)").requiredOption("--radius <px>","内圈那块圆图的半径(不含外圈)").option("--save <path>","松手后保存全视口截图").description("旋转拼图(中间圆图和外圈一起转,抖音 / 字节验证中心那种):自己量内外圈差几度,拖到对齐、纠偏后松手").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i,s]=re(t.center,2,"--center"),[d]=re(t.track,1,"--track"),[u]=re(t.radius,1,"--radius");if(u<15)throw new F("--radius 太小:量的是内圈圆图的半径");const m=await Me(e,o);if(!m)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const g=u*1.5,{solveRotate:b}=await import("./browser/captcha-drag.js"),R=await b(e,{x:m.crop.x+r,y:m.crop.y+n,track:d,clip:{x:m.crop.x+i-g+m.scroll.x,y:m.crop.y+s-g+m.scroll.y,width:g*2,height:g*2},ring:{cx:g,cy:g,r:u},bar:{x:m.crop.x+m.scroll.x,y:m.crop.y+n+m.scroll.y,width:m.crop.w,handleX:r}});await De(e,o,t.save,R)}));C(h.command("captcha-drag")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--dx <px>","往右拖多远(「拖到最右边」就给轨道剩余长度)").option("--save <path>","松手后保存全视口截图").description("按人手轨迹按住把手拖一段再松开:「请按住滑块拖到最右边」这类;拼图 / 旋转用 captcha-slide / captcha-rotate").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i]=re(t.dx,1,"--dx"),s=await Me(e,o);if(!s)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const{humanDrag:d}=await import("./browser/captcha-drag.js"),u=await d(e,s.crop.x+r,s.crop.y+n,i);await De(e,o,t.save,u)}));C(h.command("console")).option("--level <level>","Console level: all, error, warning, log, info, debug","all").option("--since <duration>","Only include messages from the last duration (for example: 30s, 2m)").option("--until <duration>","Only include messages older than the duration from now").option("--follow","Continuously print new console messages as JSON lines",!1).description("Read recent browser console messages").action(x(async(e,t)=>{const o=Te(t.since,"since"),r=Te(t.until,"until");if(o&&typeof o==="object"){console.log(JSON.stringify({error:{code:"invalid_since",message:o.error}},null,2));process.exitCode=f.USAGE_ERROR;return}if(r&&typeof r==="object"){console.log(JSON.stringify({error:{code:"invalid_until",message:r.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=(d)=>d.map((u)=>{if(u&&typeof u==="object"){const m=u;return{...m,timestamp:tt(m.timestamp)}}return{type:"log",text:String(u),timestamp:Date.now()}}),i=(d)=>ot(d,{sinceMs:o,untilMs:r}).filter((u)=>{if(t.level==="all")return!0;const m=String(u.type??u.level??"").toLowerCase();return t.level==="error"?m==="error"||m==="warning":m===String(t.level).toLowerCase()});if(t.follow){let d=0;while(!0){const u=i(n(await e.consoleMessages("all"))),m=selectFreshByTimestamp(u,d);for(const g of m.fresh)console.log(JSON.stringify({...g,timestamp:we(g.timestamp)}));d=m.lastSeenTs;await new Promise((g)=>setTimeout(g,Gt))}}const s=i(n(await e.consoleMessages(t.level)));console.log(JSON.stringify({session:at(e),captured_at:new Date().toISOString(),count:s.length,messages:s.map((d)=>({...d,timestamp:we(d.timestamp)}))},null,2))}));C(h.command("analyze").argument("<url>")).description("Classify site: anti-bot vendor, real-data API candidates, pattern (A/B/C/D), nearest adapter, next step").action(x(async(e,t)=>{const o=await e.startNetworkCapture?.()??!1;await e.goto(t);await e.wait(2);if(!o)try{await e.evaluate(Ae)}catch{}await ye(e);await e.wait(1);const n=(await ye(e)).map((R)=>({url:R.url,status:R.status,contentType:R.ct,bodyPreview:typeof R.body==="string"?R.body.slice(0,2000):R.body?JSON.stringify(R.body).slice(0,2000):null})),i=`(function(){
|
|
82
|
+
})()`}async function Me(e,t){const o=await e.evaluate(ie(t));if(!o||!o.found||!o.rect)return null;return{crop:$e(o.rect,o.viewport),scroll:o.scroll??{x:0,y:0},viewport:o.viewport}}function re(e,t,o){const r=String(e??"").split(",").map((n)=>Number(n.trim()));if(r.length!==t||r.some((n)=>!Number.isFinite(n)||n<0))throw new F(`${o} 应为 ${t} 个逗号分隔的非负数(crop 网格刻度,CSS 像素)`);return r}async function De(e,t,o,r){await new Promise((s)=>setTimeout(s,900));let n={};try{n=await e.evaluate(Se(t))}catch(s){n={read_error:J(s)}}const i=o?String(o):void 0;if(i)await e.screenshot({path:i,format:"png"});console.log(JSON.stringify({ok:!0,...r,after:n,...i?{saved:i}:{}},null,2))}C(h.command("captcha").argument("[selector]","验证码图片元素 CSS 选择器(缺省自动探测常见点选验证码)")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--save <path>","把全视口截图存到该路径(缺省存到临时 PNG 文件)").option("--scale <n>","验证码区域裁剪图的放大倍数(1..6,缺省 3;放大后认扭曲字更准)","3").option("--grid","在裁剪图上画坐标网格(刻度是裁剪图里的 CSS 像素),给 captcha-slide / captcha-rotate / captcha-drag 读位置用").description("抓取验证码:存整图 + 只含验证码的放大裁剪图 + 返回坐标与提示文字,供 AI 视觉识别后配合 captcha-click / captcha-slide / captcha-rotate / captcha-drag").action(x(async(e,t,o)=>{const r=await e.evaluate(ie(typeof t==="string"?t:void 0));if(!r||!r.found){console.log(JSON.stringify({error:{code:"captcha_not_found",message:'未探测到点选验证码图片。可显式传选择器,如 ppcli browser <s> captcha ".js-mocaptcha-img"',tried:xe}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=o?.save&&String(o.save)||k.join(ge.tmpdir(),`opencli-captcha-${Date.now()}.png`);await e.screenshot({format:"png",path:n});const i=r.rect,s=r.dpr||1,d=$e(i,r.viewport),m=Math.max(1,Math.min(6,Number(o?.scale??3)||3)),u=oe;let g=null;const b=e.cdp;if(typeof b==="function"){const R={x:d.x+(r.scroll?.x??0),y:d.y+(r.scroll?.y??0),width:d.w,height:d.h,scale:m};if(o?.grid)await e.evaluate(eo(d));const I=await b.call(e,"Page.captureScreenshot",{format:"jpeg",quality:82,clip:R,captureBeyondViewport:!1}).finally(()=>o?.grid?e.evaluate(`document.getElementById(${JSON.stringify(je)})?.remove()`):void 0);if(I&&I.data){g=n.replace(/\.png$/i,"")+".crop.jpg";A.writeFileSync(g,Buffer.from(I.data,"base64"))}}console.log(JSON.stringify({ok:!0,selector:r.selector,iframe:r.iframe===!0,crop_rect_css:g?d:null,prompt:r.prompt||"",crop:g,crop_scale:g?m:null,crop_pad:g?u:null,saved:n,rect_css:i,rect_screenshot_px:{x:Math.round(i.x*s),y:Math.round(i.y*s),w:Math.round(i.w*s),h:Math.round(i.h*s)},dpr:s,natural:r.natural,viewport:r.viewport,...o?.grid?{grid_guide:"网格刻度是 crop 里的 CSS 像素(左上角 0,0),captcha-slide / captcha-rotate / captcha-drag 的坐标都按刻度读,并传同一个 --in 选择器。"}:{},guide:"① read_local_file 打开 crop(只含验证码、已放大)认清每个待点字与顺序(注意会混入不在 prompt 里的干扰字,只点 prompt 要求的)。"+"② 坐标直接读成「在 crop 这张图里的比例」(0..1,左上角为 0,0)——不用管 pad/rect,看到字在 crop 的哪就填哪。"+"③ 执行:ppcli browser <session> captcha-click"+(o.adapter?" --adapter":"")+" --crop --in '"+r.selector.replace(/'/g,"'\\''")+"'"+' "x1,y1 x2,y2 ..."(--crop 表示坐标是 crop 图比例;空格分隔,顺序即点击顺序)。'+"iframe 的提示与确认按钮请从截图读取;点完文字后可能仍需点确认。验证码仍在不代表可以重复点字;先截图检查。"},null,2))}));C(h.command("captcha-click").argument("<points>",'待点击坐标,空格分隔且顺序即点击顺序,如 "0.30,0.46 0.68,0.66"')).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--save <path>","点击后保存全视口截图,以核对点选标记、确认按钮及验证结果").option("--in <selector>","坐标映射基准元素(缺省自动探测验证码图片)").option("--crop","坐标按「captcha 裁剪图(crop)里的比例」解释(0..1,左上角为0,0)——AI 看哪张就按那张填,内部自动扣掉 pad。推荐").option("--px","坐标按「截图设备像素」解释(自动按 devicePixelRatio 换算成 CSS 像素)").option("--css","坐标按「CSS 视口像素」解释(直接点击,不做映射)").option("--delay <ms>","每次点击之间的间隔毫秒","250").description("按坐标依次点击(点选验证码用):把 AI 从图上读到的位置一次性点上,点完回读验证码是否还在/是否跳转。默认坐标为相对基准元素的比例(0..1),--crop 则为裁剪图比例").action(x(async(e,t,o)=>{const n=String(t??"").trim().split(/\s+/).filter(Boolean).map((y)=>{const S=y.split(",");if(S.length!==2||S.some((U)=>!U.trim()))return{x:NaN,y:NaN};return{x:Number(S[0]),y:Number(S[1])}});if(n.length===0||n.some((y)=>!Number.isFinite(y.x)||!Number.isFinite(y.y))){console.log(JSON.stringify({error:{code:"usage_error",message:'坐标格式应为 "x,y x,y ...",如 "0.30,0.46 0.68,0.66"'}},null,2));process.exitCode=f.USAGE_ERROR;return}if([o?.css,o?.px,o?.crop].filter(Boolean).length>1)throw new F("--css / --px / --crop 不能同时使用");const i=o?.css?"css":o?.px?"px":o?.crop?"crop":"frac";if(n.some((y)=>y.x<0||y.y<0||(i==="frac"||i==="crop")&&(y.x>1||y.y>1)))throw new F("相对坐标必须在 0..1 内,绝对坐标必须非负;尚未点击");const s=Number(o?.delay??250);if(!Number.isFinite(s)||s<0||s>1e4)throw new F("--delay 必须在 0..10000 毫秒内");let d={x:0,y:0,w:0,h:0},m=1,u;if(i==="css"||i==="px"){const y=await e.evaluate("({w: innerWidth, h: innerHeight, dpr: devicePixelRatio})");u=y;m=y.dpr}else{const y=await e.evaluate(ie(typeof o?.in==="string"?o.in:void 0));if(!y||!y.found||!y.rect){console.log(JSON.stringify({error:{code:"captcha_not_found",message:"captcha-click 找不到基准元素,请用 --in <selector> 指定,或用 --css 传绝对坐标"}},null,2));process.exitCode=f.USAGE_ERROR;return}d=y.rect;m=y.dpr||1;u=y.viewport}const g=i==="crop"?$e(d,u):d,b=(y)=>{if(i==="css")return y;if(i==="px")return{x:y.x/m,y:y.y/m};if(i==="crop")return{x:g.x+y.x*g.w,y:g.y+y.y*g.h};return{x:d.x+y.x*d.w,y:d.y+y.y*d.h}},{cdp:R,nativeClick:I}=e,G=n.map(b).map((y)=>({x:Math.round(y.x),y:Math.round(y.y)}));if(G.some((y)=>y.x<0||y.y<0||y.x>=u.w||y.y>=u.h))throw new F("点击坐标超出视口;尚未点击,请重新截图定位");if(typeof R!=="function"&&typeof I!=="function")throw new F("当前浏览器不支持原生坐标点击");const _=(y)=>new Promise((S)=>setTimeout(S,y));async function D(y,S,U,L){if(typeof R==="function"){const ae=12+L%4;for(let q=1;q<=ae;q++){const H=q/ae,ce=H*H*(3-2*H),fe=Math.sin(H*Math.PI)*10*(L%2===0?1:-1),M=U.x+(y-U.x)*ce,V=U.y+(S-U.y)*ce+fe;await R.call(e,"Input.dispatchMouseEvent",{type:"mouseMoved",x:Math.round(M),y:Math.round(V)});await _(8+q%3*6)}await R.call(e,"Input.dispatchMouseEvent",{type:"mouseMoved",x:y,y:S});await _(40+L%3*25);await R.call(e,"Input.dispatchMouseEvent",{type:"mousePressed",x:y,y:S,button:"left",clickCount:1});await _(45+L%4*20);await R.call(e,"Input.dispatchMouseEvent",{type:"mouseReleased",x:y,y:S,button:"left",clickCount:1});return"cdp"}if(typeof I==="function"){await I.call(e,y,S);return"native"}throw new F("当前浏览器不支持原生坐标点击")}const j=[];let W="",ne={x:Math.round(d.x+d.w/2)||400,y:Math.round(d.y-40)||200};for(let y=0;y<G.length;y++){const S=G[y],U=Math.round(S.x),L=Math.round(S.y);W=await D(U,L,ne,y);ne={x:U,y:L};j.push({x:U,y:L});if(y<n.length-1&&s>0)await _(s)}await _(700);let w={};try{w=await e.evaluate(Se(typeof o?.in==="string"?o.in:void 0))}catch(y){w={read_error:J(y)}}const O=o.save?String(o.save):void 0;if(O)await e.screenshot({path:O,format:"png"});console.log(JSON.stringify({ok:!0,mode:i,method:W,clicked_n:j.length,points_css:j,after:w,...O?{saved:O}:{}},null,2))}));const Ue='与 captcha 相同的验证码元素选择器(缺省自动探测;抖音为 iframe[src*="/verifycenter/captcha/"])';C(h.command("captcha-slide")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--picture <x,y,w,h>","带拼图和缺口的那张图片的区域(crop 网格刻度)").option("--gap <px>","目测拼图大概要往右挪多远(可选,只用来缩小搜索范围)").option("--save <path>","松手后保存全视口截图").description("缺口拼图(「拖动滑块填充拼图」):按住先拖一小段,自己量拖动比例、找缺口,拖到位纠偏后松手").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i,s,d,m]=re(t.picture,4,"--picture");if(d<20||m<20)throw new F("--picture 太小:框的应是整张带缺口的图片");const u=t.gap===void 0?void 0:Number(t.gap);if(u!==void 0&&!Number.isFinite(u))throw new F("--gap 必须是数字");const g=await Me(e,o);if(!g)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const{solveSlidePuzzle:b}=await import("./browser/captcha-drag.js"),R=await b(e,{x:g.crop.x+r,y:g.crop.y+n,clip:{x:g.crop.x+i+g.scroll.x,y:g.crop.y+s+g.scroll.y,width:d,height:m},hint:u});if(!R)throw new F("没认出拼图和缺口,已拖回原位未提交:重新 captcha --grid 看清 --picture 框的是不是整张图");await De(e,o,t.save,R)}));C(h.command("captcha-rotate")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--track <px>","把手大概能往右拖多远(工具会自己量轨道,量不出才用它)").requiredOption("--center <x,y>","圆图圆心(crop 网格刻度)").requiredOption("--radius <px>","内圈那块圆图的半径(不含外圈)").option("--save <path>","松手后保存全视口截图").description("旋转拼图(中间圆图和外圈一起转,抖音 / 字节验证中心那种):自己量内外圈差几度,拖到对齐、纠偏后松手").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i,s]=re(t.center,2,"--center"),[d]=re(t.track,1,"--track"),[m]=re(t.radius,1,"--radius");if(m<15)throw new F("--radius 太小:量的是内圈圆图的半径");const u=await Me(e,o);if(!u)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const g=m*1.5,{solveRotate:b}=await import("./browser/captcha-drag.js"),R=await b(e,{x:u.crop.x+r,y:u.crop.y+n,track:d,clip:{x:u.crop.x+i-g+u.scroll.x,y:u.crop.y+s-g+u.scroll.y,width:g*2,height:g*2},ring:{cx:g,cy:g,r:m},bar:{x:u.crop.x+u.scroll.x,y:u.crop.y+n+u.scroll.y,width:u.crop.w,handleX:r}});await De(e,o,t.save,R)}));C(h.command("captcha-drag")).option("--adapter","定位已有平台适配器会话;session 必须是 site:<site>,如 site:douyin").option("--in <selector>",Ue).requiredOption("--handle <x,y>","滑块把手中心(crop 网格刻度)").requiredOption("--dx <px>","往右拖多远(「拖到最右边」就给轨道剩余长度)").option("--save <path>","松手后保存全视口截图").description("按人手轨迹按住把手拖一段再松开:「请按住滑块拖到最右边」这类;拼图 / 旋转用 captcha-slide / captcha-rotate").action(x(async(e,t)=>{const o=typeof t?.in==="string"?t.in:void 0,[r,n]=re(t.handle,2,"--handle"),[i]=re(t.dx,1,"--dx"),s=await Me(e,o);if(!s)throw new F("找不到验证码元素:传与 captcha 相同的 --in <selector>");const{humanDrag:d}=await import("./browser/captcha-drag.js"),m=await d(e,s.crop.x+r,s.crop.y+n,i);await De(e,o,t.save,m)}));C(h.command("console")).option("--level <level>","Console level: all, error, warning, log, info, debug","all").option("--since <duration>","Only include messages from the last duration (for example: 30s, 2m)").option("--until <duration>","Only include messages older than the duration from now").option("--follow","Continuously print new console messages as JSON lines",!1).description("Read recent browser console messages").action(x(async(e,t)=>{const o=Te(t.since,"since"),r=Te(t.until,"until");if(o&&typeof o==="object"){console.log(JSON.stringify({error:{code:"invalid_since",message:o.error}},null,2));process.exitCode=f.USAGE_ERROR;return}if(r&&typeof r==="object"){console.log(JSON.stringify({error:{code:"invalid_until",message:r.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=(d)=>d.map((m)=>{if(m&&typeof m==="object"){const u=m;return{...u,timestamp:tt(u.timestamp)}}return{type:"log",text:String(m),timestamp:Date.now()}}),i=(d)=>ot(d,{sinceMs:o,untilMs:r}).filter((m)=>{if(t.level==="all")return!0;const u=String(m.type??m.level??"").toLowerCase();return t.level==="error"?u==="error"||u==="warning":u===String(t.level).toLowerCase()});if(t.follow){let d=0;while(!0){const m=i(n(await e.consoleMessages("all"))),u=selectFreshByTimestamp(m,d);for(const g of u.fresh)console.log(JSON.stringify({...g,timestamp:we(g.timestamp)}));d=u.lastSeenTs;await new Promise((g)=>setTimeout(g,Gt))}}const s=i(n(await e.consoleMessages(t.level)));console.log(JSON.stringify({session:at(e),captured_at:new Date().toISOString(),count:s.length,messages:s.map((d)=>({...d,timestamp:we(d.timestamp)}))},null,2))}));C(h.command("analyze").argument("<url>")).description("Classify site: anti-bot vendor, real-data API candidates, pattern (A/B/C/D), nearest adapter, next step").action(x(async(e,t)=>{const o=await e.startNetworkCapture?.()??!1;await e.goto(t);await e.wait(2);if(!o)try{await e.evaluate(Ae)}catch{}await ye(e);await e.wait(1);const n=(await ye(e)).map((R)=>({url:R.url,status:R.status,contentType:R.ct,bodyPreview:typeof R.body==="string"?R.body.slice(0,2000):R.body?JSON.stringify(R.body).slice(0,2000):null})),i=`(function(){
|
|
83
83
|
return {
|
|
84
84
|
cookieNames: (document.cookie || '').split(';').map(function(c){ return c.trim().split('=')[0]; }).filter(Boolean),
|
|
85
85
|
initialState: {
|
|
@@ -91,7 +91,7 @@ Examples:
|
|
|
91
91
|
title: document.title || '',
|
|
92
92
|
finalUrl: location.href,
|
|
93
93
|
};
|
|
94
|
-
})()`,s=await e.evaluate(i),d=(await e.getCookies({url:s.finalUrl||t}).catch(()=>[])).map((R)=>R.name).filter(Boolean),u=[...new Set([...s.cookieNames,...d])],m={requestedUrl:t,finalUrl:s.finalUrl,cookieNames:u,networkEntries:n,initialState:s.initialState,title:s.title},g=Xo(m,Ee()),b=resolveSitemapAvailabilityForUrl(s.finalUrl||t);console.log(JSON.stringify({...g,...b?{sitemap:b}:{}},null,2))}));const z=(e)=>e.option("--role <role>","Semantic role (button, link, textbox, option, etc.)").option("--name <text>","Accessible name contains text (aria-label, label, title, placeholder, or visible text)").option("--label <text>","Associated label contains text").option("--text <text>","Visible text contains text").option("--testid <id>","data-testid / data-test / test-id contains id"),ct=(e,t)=>e.option(`--${t}-role <role>`,`${t} semantic role`).option(`--${t}-name <text>`,`${t} accessible name contains text`).option(`--${t}-label <text>`,`${t} associated label contains text`).option(`--${t}-text <text>`,`${t} visible text contains text`).option(`--${t}-testid <id>`,`${t} data-testid / data-test / test-id contains id`),ve=(e)=>{const t={};for(const o of["role","name","label","text","testid"]){const r=e[o];if(typeof r==="string"&&r.trim())t[o]=r.trim()}return Object.keys(t).length>0?t:null},lt=(e,t)=>{const o={},r={role:`${t}Role`,name:`${t}Name`,label:`${t}Label`,text:`${t}Text`,testid:`${t}Testid`};for(const n of["role","name","label","text","testid"]){const i=e[r[n]];if(typeof i==="string"&&i.trim())o[n]=i.trim()}return Object.keys(o).length>0?o:null},dt=async(e,t,o)=>{const r=await e.evaluate(kt({...t,limit:6}));if(At(r))return r;if(o==="write"&&r.matches_n!==1)return{error:{code:"semantic_ambiguous",message:`Semantic locator matched ${r.matches_n} elements; write actions require a unique target.`,hint:"Add --name/--label/--text/--testid or use browser find with a narrower locator.",matches_n:r.matches_n,entries:r.entries}};const n=r.entries[0];if(!n)return{error:{code:"semantic_not_found",message:"Semantic locator matched 0 elements",hint:"Try browser state, --source ax, or relax the semantic locator."}};const i=String(n.ref);if(o==="read")return{target:i,...r.matches_n>1?{total_matches:r.matches_n}:{}};return i},to=async(e,t,o)=>{const r=ve(t);if(!r)return null;return dt(e,r,o)},Ge=async(e,t,o,r)=>{const n=typeof t==="string"&&t.trim()?t.trim():"",i=!!ve(o);if(n&&i)return{error:{code:"usage_error",message:"Pass either <target> or semantic locator flags, not both."}};if(n)return n;const s=await to(e,o,r);if(s)return s;return{error:{code:"usage_error",message:"Missing target. Pass a numeric ref/CSS selector, or semantic flags like --role button --name Submit."}}},de=(e)=>{console.log(JSON.stringify(e,null,2));process.exitCode=f.USAGE_ERROR},ue=async(e,t,o)=>{const r=await Ge(e,t,o,"write");if(typeof r==="string")return r;if("error"in r)de(r);return null},Fe=async(e,t,o,r,n)=>{const i=!!ve(r);if(i&&o!==void 0){de({error:{code:"usage_error",message:`When using semantic locator flags, pass only <${n}> as the positional argument.`}});return null}const s=i?t:o;if(s===void 0){de({error:{code:"usage_error",message:`Missing ${n}.`,hint:i?`With semantic locator flags, pass the ${n} as the only positional argument.`:`Pass both a target and ${n}.`}});return null}const d=await ue(e,i?void 0:t,r);if(!d)return null;return{target:d,value:String(s)}},ut=async(e,t,o,r,n)=>{const i=typeof t==="string"&&t.trim()?t.trim():"",s=lt(o,r);if(i&&s){de({error:{code:"usage_error",message:`Pass either <${n}> or --${r}-* semantic locator flags, not both.`}});return null}if(i)return i;if(s){const d=await dt(e,s,"write");if(typeof d==="string")return d;if("error"in d)de(d);return null}de({error:{code:"usage_error",message:`Missing ${n}. Pass a numeric ref/CSS selector, or --${r}-role/--${r}-name semantic flags.`}});return null};C(z(h.command("find")).option("--css <selector>","CSS selector; combine with --text to keep only matches whose text / aria-label / title / placeholder / value contains it").option("--visible","Keep only visible elements",!1).option("--limit <n>","Max entries returned","50").option("--text-max <n>","Max chars of trimmed text per entry","120").description("Find DOM elements by CSS or semantic locator — returns JSON {matches_n, visible_n, filtered_n?, entries[]}; visible entries are listed first")).action(x(async(e,t)=>{let o=ve(t),r;if(t.css&&o&&Object.keys(o).length===1&&typeof o.text==="string"){r=o.text;o=null}const n=t.visible===!0;if((!t.css||typeof t.css!=="string")&&!o){console.log(JSON.stringify({error:{code:"usage_error",message:"--css <selector> or a semantic locator flag is required",hint:'Examples: ppcli browser find --css ".btn.primary"; ppcli browser find --css button --visible --text 发布; ppcli browser find --role button --name Save'}},null,2));process.exitCode=f.USAGE_ERROR;return}if(t.css&&o){console.log(JSON.stringify({error:{code:"usage_error",message:"Pass either --css or semantic locator flags (--role/--name/--label/--testid), not both.",hint:"Only --text and --visible combine with --css."}},null,2));process.exitCode=f.USAGE_ERROR;return}const i=N(t.limit);if(i&&typeof i==="object"&&"error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error.replace("--nth","--limit")}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=N(t.textMax);if(s&&typeof s==="object"&&"error"in s){console.log(JSON.stringify({error:{code:"usage_error",message:s.error.replace("--nth","--text-max")}},null,2));process.exitCode=f.USAGE_ERROR;return}const d=await e.evaluate(o?kt({...o,limit:i??void 0,textMax:s??void 0,visibleOnly:n,visibleFirst:!0}):Go(t.css,{limit:i??void 0,textMax:s??void 0,visibleOnly:n,visibleFirst:!0,textFilter:r}));if(At(d)){console.log(JSON.stringify(d,null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify(d,null,2))}));const me=h.command("get").description("Get page properties");C(me.command("title").description("Page title")).action(x(async(e)=>{console.log(await e.evaluate("document.title"))}));C(me.command("url").description("Current page URL")).action(x(async(e)=>{console.log(await e.getCurrentUrl?.()??await e.evaluate("location.href"))}));const Be=async(e,t,o,r,n)=>{const i=await Ge(e,t,o,"read");if(typeof i!=="string"&&"error"in i){console.log(JSON.stringify(i,null,2));process.exitCode=f.USAGE_ERROR;return}const s=typeof i==="string"?i:i.target,d=typeof i==="string"?void 0:i.total_matches,u=N(o.nth);if(u&&typeof u==="object"&&"error"in u){console.log(JSON.stringify({error:{code:"usage_error",message:u.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:m,match_level:g}=await E(e,s,{firstOnMulti:u===null,...typeof u==="number"?{nth:u}:{}}),b=await e.evaluate(r);let R;if(n==="attributes")try{R=b==null?{}:JSON.parse(String(b))}catch{R=b}else R=b??null;console.log(JSON.stringify({value:R,matches_n:m,match_level:g,...d&&d>1?{total_matches:d}:{}},null,2))};C(z(me.command("text")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Element text content — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},Po(),"text")));C(z(me.command("value")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Input/textarea value — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},Jo(),"value")));C(me.command("html").option("--selector <css>","CSS selector scope (first match)").option("--as <format>",'Output format: "html" (default) or "json" for structured tree',"html").option("--max <n>","Max characters of raw HTML to return (0 = unlimited)","0").option("--depth <n>","(--as json) Max tree depth below root (0 = root only, 0 disables = unlimited via empty)","").option("--children-max <n>","(--as json) Max element children kept per node (empty = unlimited)","").option("--text-max <n>","(--as json) Max chars of direct text kept per node (empty = unlimited)","").description("Page HTML (or scoped); use --as json for a {tag, attrs, text, children} tree")).action(x(async(e,t)=>{const o=String(t.as||"html").toLowerCase();if(o!=="html"&&o!=="json"){console.log(JSON.stringify({error:{code:"invalid_format",message:`--as must be "html" or "json", got "${t.as}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const r=String(t.max??"0");if(!/^\d+$/.test(r)){console.log(JSON.stringify({error:{code:"invalid_max",message:`--max must be a non-negative integer, got "${t.max}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=Number.parseInt(r,10);if(o==="json"){const u=(_,D)=>{const j=D===void 0||D===null?"":String(D);if(j==="")return null;if(!/^\d+$/.test(j))return{error:`${_} must be a non-negative integer, got "${j}"`};return Number.parseInt(j,10)},m=u("--depth",t.depth),g=u("--children-max",t.childrenMax),b=u("--text-max",t.textMax);for(const _ of[m,g,b])if(_&&typeof _==="object"&&"error"in _){console.log(JSON.stringify({error:{code:"invalid_budget",message:_.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const R=Ho({selector:t.selector??null,depth:m,childrenMax:g,textMax:b}),I=await e.evaluate(R);if(I&&typeof I==="object"&&"invalidSelector"in I&&I.invalidSelector){console.log(JSON.stringify({error:{code:"invalid_selector",message:`Selector "${t.selector}" is not a valid CSS selector: ${I.reason}`}},null,2));process.exitCode=f.USAGE_ERROR;return}const G=I;if(!G||G.matched===0){console.log(JSON.stringify({error:{code:"selector_not_found",message:t.selector?`Selector "${t.selector}" matched 0 elements.`:"Page has no documentElement."}},null,2));process.exitCode=f.USAGE_ERROR;return}console.log(JSON.stringify(G,null,2));return}const i=t.selector?JSON.stringify(t.selector):"null",s=await e.evaluate(`(() => {
|
|
94
|
+
})()`,s=await e.evaluate(i),d=(await e.getCookies({url:s.finalUrl||t}).catch(()=>[])).map((R)=>R.name).filter(Boolean),m=[...new Set([...s.cookieNames,...d])],u={requestedUrl:t,finalUrl:s.finalUrl,cookieNames:m,networkEntries:n,initialState:s.initialState,title:s.title},g=Xo(u,Ee()),b=resolveSitemapAvailabilityForUrl(s.finalUrl||t);console.log(JSON.stringify({...g,...b?{sitemap:b}:{}},null,2))}));const z=(e)=>e.option("--role <role>","Semantic role (button, link, textbox, option, etc.)").option("--name <text>","Accessible name contains text (aria-label, label, title, placeholder, or visible text)").option("--label <text>","Associated label contains text").option("--text <text>","Visible text contains text").option("--testid <id>","data-testid / data-test / test-id contains id"),ct=(e,t)=>e.option(`--${t}-role <role>`,`${t} semantic role`).option(`--${t}-name <text>`,`${t} accessible name contains text`).option(`--${t}-label <text>`,`${t} associated label contains text`).option(`--${t}-text <text>`,`${t} visible text contains text`).option(`--${t}-testid <id>`,`${t} data-testid / data-test / test-id contains id`),ve=(e)=>{const t={};for(const o of["role","name","label","text","testid"]){const r=e[o];if(typeof r==="string"&&r.trim())t[o]=r.trim()}return Object.keys(t).length>0?t:null},lt=(e,t)=>{const o={},r={role:`${t}Role`,name:`${t}Name`,label:`${t}Label`,text:`${t}Text`,testid:`${t}Testid`};for(const n of["role","name","label","text","testid"]){const i=e[r[n]];if(typeof i==="string"&&i.trim())o[n]=i.trim()}return Object.keys(o).length>0?o:null},dt=async(e,t,o)=>{const r=await e.evaluate(kt({...t,limit:6}));if(At(r))return r;if(o==="write"&&r.matches_n!==1)return{error:{code:"semantic_ambiguous",message:`Semantic locator matched ${r.matches_n} elements; write actions require a unique target.`,hint:"Add --name/--label/--text/--testid or use browser find with a narrower locator.",matches_n:r.matches_n,entries:r.entries}};const n=r.entries[0];if(!n)return{error:{code:"semantic_not_found",message:"Semantic locator matched 0 elements",hint:"Try browser state, --source ax, or relax the semantic locator."}};const i=String(n.ref);if(o==="read")return{target:i,...r.matches_n>1?{total_matches:r.matches_n}:{}};return i},to=async(e,t,o)=>{const r=ve(t);if(!r)return null;return dt(e,r,o)},Ge=async(e,t,o,r)=>{const n=typeof t==="string"&&t.trim()?t.trim():"",i=!!ve(o);if(n&&i)return{error:{code:"usage_error",message:"Pass either <target> or semantic locator flags, not both."}};if(n)return n;const s=await to(e,o,r);if(s)return s;return{error:{code:"usage_error",message:"Missing target. Pass a numeric ref/CSS selector, or semantic flags like --role button --name Submit."}}},de=(e)=>{console.log(JSON.stringify(e,null,2));process.exitCode=f.USAGE_ERROR},ue=async(e,t,o)=>{const r=await Ge(e,t,o,"write");if(typeof r==="string")return r;if("error"in r)de(r);return null},Fe=async(e,t,o,r,n)=>{const i=!!ve(r);if(i&&o!==void 0){de({error:{code:"usage_error",message:`When using semantic locator flags, pass only <${n}> as the positional argument.`}});return null}const s=i?t:o;if(s===void 0){de({error:{code:"usage_error",message:`Missing ${n}.`,hint:i?`With semantic locator flags, pass the ${n} as the only positional argument.`:`Pass both a target and ${n}.`}});return null}const d=await ue(e,i?void 0:t,r);if(!d)return null;return{target:d,value:String(s)}},ut=async(e,t,o,r,n)=>{const i=typeof t==="string"&&t.trim()?t.trim():"",s=lt(o,r);if(i&&s){de({error:{code:"usage_error",message:`Pass either <${n}> or --${r}-* semantic locator flags, not both.`}});return null}if(i)return i;if(s){const d=await dt(e,s,"write");if(typeof d==="string")return d;if("error"in d)de(d);return null}de({error:{code:"usage_error",message:`Missing ${n}. Pass a numeric ref/CSS selector, or --${r}-role/--${r}-name semantic flags.`}});return null};C(z(h.command("find")).option("--css <selector>","CSS selector; combine with --text to keep only matches whose text / aria-label / title / placeholder / value contains it").option("--visible","Keep only visible elements",!1).option("--limit <n>","Max entries returned","50").option("--text-max <n>","Max chars of trimmed text per entry","120").description("Find DOM elements by CSS or semantic locator — returns JSON {matches_n, visible_n, filtered_n?, entries[]}; visible entries are listed first")).action(x(async(e,t)=>{let o=ve(t),r;if(t.css&&o&&Object.keys(o).length===1&&typeof o.text==="string"){r=o.text;o=null}const n=t.visible===!0;if((!t.css||typeof t.css!=="string")&&!o){console.log(JSON.stringify({error:{code:"usage_error",message:"--css <selector> or a semantic locator flag is required",hint:'Examples: ppcli browser find --css ".btn.primary"; ppcli browser find --css button --visible --text 发布; ppcli browser find --role button --name Save'}},null,2));process.exitCode=f.USAGE_ERROR;return}if(t.css&&o){console.log(JSON.stringify({error:{code:"usage_error",message:"Pass either --css or semantic locator flags (--role/--name/--label/--testid), not both.",hint:"Only --text and --visible combine with --css."}},null,2));process.exitCode=f.USAGE_ERROR;return}const i=N(t.limit);if(i&&typeof i==="object"&&"error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error.replace("--nth","--limit")}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=N(t.textMax);if(s&&typeof s==="object"&&"error"in s){console.log(JSON.stringify({error:{code:"usage_error",message:s.error.replace("--nth","--text-max")}},null,2));process.exitCode=f.USAGE_ERROR;return}const d=await e.evaluate(o?kt({...o,limit:i??void 0,textMax:s??void 0,visibleOnly:n,visibleFirst:!0}):Go(t.css,{limit:i??void 0,textMax:s??void 0,visibleOnly:n,visibleFirst:!0,textFilter:r}));if(At(d)){console.log(JSON.stringify(d,null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify(d,null,2))}));const me=h.command("get").description("Get page properties");C(me.command("title").description("Page title")).action(x(async(e)=>{console.log(await e.evaluate("document.title"))}));C(me.command("url").description("Current page URL")).action(x(async(e)=>{console.log(await e.getCurrentUrl?.()??await e.evaluate("location.href"))}));const Be=async(e,t,o,r,n)=>{const i=await Ge(e,t,o,"read");if(typeof i!=="string"&&"error"in i){console.log(JSON.stringify(i,null,2));process.exitCode=f.USAGE_ERROR;return}const s=typeof i==="string"?i:i.target,d=typeof i==="string"?void 0:i.total_matches,m=N(o.nth);if(m&&typeof m==="object"&&"error"in m){console.log(JSON.stringify({error:{code:"usage_error",message:m.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:u,match_level:g}=await E(e,s,{firstOnMulti:m===null,...typeof m==="number"?{nth:m}:{}}),b=await e.evaluate(r);let R;if(n==="attributes")try{R=b==null?{}:JSON.parse(String(b))}catch{R=b}else R=b??null;console.log(JSON.stringify({value:R,matches_n:u,match_level:g,...d&&d>1?{total_matches:d}:{}},null,2))};C(z(me.command("text")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Element text content — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},Po(),"text")));C(z(me.command("value")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Input/textarea value — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},Jo(),"value")));C(me.command("html").option("--selector <css>","CSS selector scope (first match)").option("--as <format>",'Output format: "html" (default) or "json" for structured tree',"html").option("--max <n>","Max characters of raw HTML to return (0 = unlimited)","0").option("--depth <n>","(--as json) Max tree depth below root (0 = root only, 0 disables = unlimited via empty)","").option("--children-max <n>","(--as json) Max element children kept per node (empty = unlimited)","").option("--text-max <n>","(--as json) Max chars of direct text kept per node (empty = unlimited)","").description("Page HTML (or scoped); use --as json for a {tag, attrs, text, children} tree")).action(x(async(e,t)=>{const o=String(t.as||"html").toLowerCase();if(o!=="html"&&o!=="json"){console.log(JSON.stringify({error:{code:"invalid_format",message:`--as must be "html" or "json", got "${t.as}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const r=String(t.max??"0");if(!/^\d+$/.test(r)){console.log(JSON.stringify({error:{code:"invalid_max",message:`--max must be a non-negative integer, got "${t.max}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=Number.parseInt(r,10);if(o==="json"){const m=(_,D)=>{const j=D===void 0||D===null?"":String(D);if(j==="")return null;if(!/^\d+$/.test(j))return{error:`${_} must be a non-negative integer, got "${j}"`};return Number.parseInt(j,10)},u=m("--depth",t.depth),g=m("--children-max",t.childrenMax),b=m("--text-max",t.textMax);for(const _ of[u,g,b])if(_&&typeof _==="object"&&"error"in _){console.log(JSON.stringify({error:{code:"invalid_budget",message:_.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const R=Ho({selector:t.selector??null,depth:u,childrenMax:g,textMax:b}),I=await e.evaluate(R);if(I&&typeof I==="object"&&"invalidSelector"in I&&I.invalidSelector){console.log(JSON.stringify({error:{code:"invalid_selector",message:`Selector "${t.selector}" is not a valid CSS selector: ${I.reason}`}},null,2));process.exitCode=f.USAGE_ERROR;return}const G=I;if(!G||G.matched===0){console.log(JSON.stringify({error:{code:"selector_not_found",message:t.selector?`Selector "${t.selector}" matched 0 elements.`:"Page has no documentElement."}},null,2));process.exitCode=f.USAGE_ERROR;return}console.log(JSON.stringify(G,null,2));return}const i=t.selector?JSON.stringify(t.selector):"null",s=await e.evaluate(`(() => {
|
|
95
95
|
const s = ${i};
|
|
96
96
|
if (s) {
|
|
97
97
|
try {
|
|
@@ -103,7 +103,7 @@ Examples:
|
|
|
103
103
|
}
|
|
104
104
|
return { kind: 'ok', html: document.documentElement ? document.documentElement.outerHTML : null };
|
|
105
105
|
})()`);if(s.kind==="invalid_selector"){console.log(JSON.stringify({error:{code:"invalid_selector",message:`Selector "${t.selector}" is not a valid CSS selector: ${s.reason}`}},null,2));process.exitCode=f.USAGE_ERROR;return}const d=s.html;if(d===null){if(t.selector){console.log(JSON.stringify({error:{code:"selector_not_found",message:`Selector "${t.selector}" matched 0 elements.`}},null,2));process.exitCode=f.USAGE_ERROR;return}console.log("(empty)");return}if(n>0&&d.length>n){console.log(`<!-- ppcli: truncated ${n} of ${d.length} chars; re-run without --max (or --max 0) for full -->
|
|
106
|
-
${d.slice(0,n)}`);return}console.log(d)}));C(z(me.command("attributes")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Element attributes — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},jo(),"attributes")));function ee(e){const t=N(e);if(t&&typeof t==="object"&&"error"in t)return t;if(typeof t==="number")return{opts:{nth:t}};return{opts:{}}}function oo(e){const t=Array.isArray(e)?e:[];if(t.length===0)return{error:{code:"usage_error",message:"At least one file path is required.",hint:'Example: ppcli browser upload "input[type=file]" ./receipt.pdf'}};const o=[];for(const r of t){const n=String(r),i=n==="~"||n.startsWith(`~${k.sep}`)?k.join(ge.homedir(),n.slice(2)):n,s=k.resolve(i);if(!A.existsSync(s))return{error:{code:"file_not_found",message:`File not found: ${s}`}};if(!A.statSync(s).isFile())return{error:{code:"not_a_file",message:`Not a regular file: ${s}`}};o.push(s)}return{files:o}}function Le(e,t){const o=N(e);if(o&&typeof o==="object"&&"error"in o)return{error:o.error.replace("--nth",t)};if(typeof o==="number")return{opts:{nth:o}};return{opts:{}}}C(z(h.command("click")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Click element — JSON envelope {clicked, target, matches_n}")).action(x(async(e,t,o)=>{const r=await Ge(e,t,o??{},"write");if(typeof r!=="string"){console.log(JSON.stringify(r,null,2));process.exitCode=f.USAGE_ERROR;return}const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s,click_method:d,hit:u,retargeted:m}=await e.click(r,n.opts);console.log(JSON.stringify({clicked:!0,target:r,matches_n:i,match_level:s,...d&&{click_method:d},...u&&{hit:u},...m&&{retargeted:m}},null,2))}));C(z(h.command("type")).argument("[targetOrText]","Numeric ref/CSS target, or text when using --role/--name/etc.").argument("[text]","Text to type").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Click element, then type text — JSON envelope {typed, text, target, matches_n, autocomplete}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"text");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}await e.click(n.target,i.opts);await e.wait(0.3);const{matches_n:s,match_level:d}=await e.typeText(n.target,n.value,i.opts),u=await e.evaluate(Do());if(u)await e.wait(0.4);console.log(JSON.stringify({typed:!0,text:n.value,target:n.target,matches_n:s,match_level:d,autocomplete:!!u},null,2))}));C(z(h.command("hover")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Move the mouse over an element — JSON envelope {hovered, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.hover!=="function")throw Error("browser hover is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s}=await e.hover(r,n.opts);console.log(JSON.stringify({hovered:!0,target:r,matches_n:i,match_level:s},null,2))}));C(z(h.command("focus")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Focus an element — JSON envelope {focused, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.focus!=="function")throw Error("browser focus is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{focused:i,matches_n:s,match_level:d}=await e.focus(r,n.opts);console.log(JSON.stringify({focused:i,target:r,matches_n:s,match_level:d},null,2))}));C(z(h.command("dblclick")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Double-click element — JSON envelope {dblclicked, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.dblClick!=="function")throw Error("browser dblclick is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s}=await e.dblClick(r,n.opts);console.log(JSON.stringify({dblclicked:!0,target:r,matches_n:i,match_level:s},null,2))}));const mt=async(e,t,o,r)=>{if(typeof e.setChecked!=="function")throw Error(`browser ${r?"check":"uncheck"} is not supported by this browser backend`);const n=await ue(e,t,o);if(!n)return;const i=ee(o?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=await e.setChecked(n,r,i.opts);console.log(JSON.stringify({checked:s.checked,changed:s.changed,target:n,matches_n:s.matches_n,match_level:s.match_level,...s.kind?{kind:s.kind}:{}},null,2))};C(z(h.command("check")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Ensure a checkbox/radio/aria-checked control is checked — JSON envelope {checked, changed, target, matches_n}")).action(x(async(e,t,o)=>{await mt(e,t,o??{},!0)}));C(z(h.command("uncheck")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Ensure a checkbox/aria-checked control is unchecked — JSON envelope {checked, changed, target, matches_n}")).action(x(async(e,t,o)=>{await mt(e,t,o??{},!1)}));C(z(h.command("upload")).argument("[targetOrFile]","Numeric ref/CSS target, or first file when using --role/--name/etc.").argument("[files...]","Local file path(s) to attach").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Attach local files to a file input — JSON envelope {uploaded, files, file_names, target, matches_n}")).action(x(async(e,t,o,r)=>{if(typeof e.uploadFiles!=="function")throw Error("browser upload is not supported by this browser backend");const n=!!ve(r??{}),s=await ue(e,n?void 0:t,r??{});if(!s)return;const d=ee(r?.nth);if("error"in d){console.log(JSON.stringify({error:{code:"usage_error",message:d.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const u=n?[t,...Array.isArray(o)?o:[]].filter((b)=>b!==void 0):o,m=oo(u);if("error"in m){console.log(JSON.stringify({error:m.error},null,2));process.exitCode=f.USAGE_ERROR;return}const g=await e.uploadFiles(s,m.files,d.opts);console.log(JSON.stringify(g,null,2))}));C(ct(ct(h.command("drag"),"from"),"to").argument("[source]","Numeric ref/CSS selector to drag from, or omit with --from-role/--from-name/etc.").argument("[target]","Numeric ref/CSS selector to drop onto, or omit with --to-role/--to-name/etc. or --dx/--dy").option("--from-nth <n>","When <source> is a multi-match CSS selector, pick the nth match (0-based)").option("--to-nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").option("--dx <px>","Drag by this many CSS pixels horizontally from the source centre instead of onto a target (negative = left)").option("--dy <px>","Drag by this many CSS pixels vertically from the source centre instead of onto a target (negative = up)").option("--steps <n>","Pointer-move samples between press and release (default: 2 for element drops, ~1 per 12px for --dx/--dy)").description("Drag one element onto another, or by a pixel offset with --dx/--dy (sliders) — JSON envelope {dragged, source, target | dx, dy, from, to}")).action(x(async(e,t,o,r)=>{if(typeof e.drag!=="function")throw Error("browser drag is not supported by this browser backend");const n=(_,D)=>{console.log(JSON.stringify({error:{code:"usage_error",message:_,...D?{hint:D}:{}}},null,2));process.exitCode=f.USAGE_ERROR},i=(_,D)=>{if(_===void 0||_===null||_==="")return null;const j=Number(String(_).trim().replace(/px$/i,""));if(!Number.isFinite(j))return{error:`${D} must be a number of CSS pixels, got "${String(_)}"`};return j},s=i(r?.dx,"--dx"),d=i(r?.dy,"--dy");for(const _ of[s,d])if(_&&typeof _==="object")return n(_.error);let u;if(r?.steps!==void 0){u=Number(r.steps);if(!Number.isInteger(u)||u<1||u>200)return n(`--steps must be an integer between 1 and 200, got "${String(r.steps)}"`)}const m=s!==null||d!==null;if(m){if(typeof o==="string"&&o.length>0||lt(r??{},"to")!==null||r?.toNth!==void 0)return n("Pass either a drop target or --dx/--dy, not both.","Element drop: browser drag <source> <target>. Pixel drag: browser drag <source> --dx 180.");if((s??0)===0&&(d??0)===0)return n("--dx/--dy are both 0 — nothing to drag.");if(typeof e.dragBy!=="function")throw Error("browser drag --dx/--dy is not supported by this browser backend")}const g=await ut(e,t,r??{},"from","source");if(!g)return;if(m){const _=Le(r?.fromNth,"--from-nth");if("error"in _)return n(_.error);const D=await e.dragBy(g,{dx:s??0,dy:d??0},{from:_.opts,...u!==void 0?{steps:u}:{}});console.log(JSON.stringify(D,null,2));return}const b=await ut(e,o,r??{},"to","target");if(!b)return;const R=Le(r?.fromNth,"--from-nth");if("error"in R){console.log(JSON.stringify({error:{code:"usage_error",message:R.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const I=Le(r?.toNth,"--to-nth");if("error"in I){console.log(JSON.stringify({error:{code:"usage_error",message:I.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const G=await e.drag(g,b,{from:R.opts,to:I.opts,...u!==void 0?{steps:u}:{}});console.log(JSON.stringify(G,null,2))}));C(z(h.command("fill")).argument("[targetOrText]","Numeric ref/CSS target, or text when using --role/--name/etc.").argument("[text]","Text to set exactly").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Set input/textarea/contenteditable text exactly and verify the value — JSON envelope {filled, verified, text, actual}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"text");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=await e.fillText(n.target,n.value,i.opts);if(!s.verified)process.exitCode=f.GENERIC_ERROR;console.log(JSON.stringify({filled:s.filled,verified:s.verified,target:n.target,text:n.value,actual:s.actual,length:s.length,matches_n:s.matches_n,match_level:s.match_level,...s.mode?{mode:s.mode}:{}},null,2))}));C(z(h.command("select")).argument("[targetOrOption]","Numeric ref/CSS target, or option text when using --role/--name/etc.").argument("[option]","Option text (or value) to select").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Select dropdown option — JSON envelope {selected, target, matches_n}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"option");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:s,match_level:d}=await E(e,n.target,i.opts),u=await e.evaluate(Mo(n.value));if(u?.error){console.log(JSON.stringify({error:{code:u.error==="Not a <select>"?"not_a_select":"option_not_found",message:u.error,...u.available&&{available:u.available},matches_n:s}},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify({selected:u?.selected??n.value,target:n.target,matches_n:s,match_level:d},null,2))}));C(h.command("keys").argument("<key>","Key to press (Enter, Escape, Tab, Control+a)")).description("Press keyboard key").action(x(async(e,t)=>{await e.pressKey(t);console.log(`Pressed: ${t}`)}));const pt=h.command("dialog").description("Handle a blocking JavaScript alert/confirm/prompt dialog");C(pt.command("accept").option("--text <text>","Prompt text to submit for prompt() dialogs").description("Accept the currently open JavaScript dialog")).action(x(async(e,t)=>{if(!e.handleJavaScriptDialog)throw Error("This browser session does not support JavaScript dialog handling");try{await e.handleJavaScriptDialog(!0,t?.text)}catch(o){if(J(o).toLowerCase().includes("no dialog")){console.log(JSON.stringify({error:{code:"no_javascript_dialog",message:"No JavaScript dialog is currently open."}},null,2));process.exitCode=f.USAGE_ERROR;return}throw o}console.log(JSON.stringify({handled:!0,action:"accept",...t?.text!==void 0&&{text:t.text}},null,2))}));C(pt.command("dismiss").description("Dismiss the currently open JavaScript dialog")).action(x(async(e)=>{if(!e.handleJavaScriptDialog)throw Error("This browser session does not support JavaScript dialog handling");try{await e.handleJavaScriptDialog(!1)}catch(t){if(J(t).toLowerCase().includes("no dialog")){console.log(JSON.stringify({error:{code:"no_javascript_dialog",message:"No JavaScript dialog is currently open."}},null,2));process.exitCode=f.USAGE_ERROR;return}throw t}console.log(JSON.stringify({handled:!0,action:"dismiss"},null,2))}));C(h.command("wait")).argument("<type>","selector, text, time, xhr, or download").argument("[value]","CSS selector, text string, seconds, XHR URL regex, or download filename/URL pattern").option("--timeout <ms>","Timeout in milliseconds","10000").description('Wait for selector, text, time, matching XHR, or browser download (e.g. wait selector ".loaded", wait text "Success", wait time 3, wait xhr "/api/search", wait download receipt.pdf)').action(x(async(e,t,o,r)=>{const n=parseInt(r.timeout,10);if(t==="time"){const i=parseFloat(o??"2");await e.wait(i);console.log(`Waited ${i}s`)}else if(t==="selector"){if(!o){console.error("Missing CSS selector");process.exitCode=f.USAGE_ERROR;return}await e.wait({selector:o,timeout:n/1000});console.log(`Element "${o}" appeared`)}else if(t==="text"){if(!o){console.error("Missing text");process.exitCode=f.USAGE_ERROR;return}await e.wait({text:o,timeout:n/1000});console.log(`Text "${o}" appeared`)}else if(t==="xhr"){if(!o){console.error("Missing XHR URL regex");process.exitCode=f.USAGE_ERROR;return}let i;try{i=new RegExp(o)}catch(g){console.error(`Invalid regex "${o}": ${g instanceof Error?g.message:String(g)}`);process.exitCode=f.USAGE_ERROR;return}if(!(await e.startNetworkCapture?.()??!1))try{await e.evaluate(Ae)}catch{}await ye(e);const d=Date.now()+n,u=400;let m=null;while(Date.now()<d&&!m){m=(await ye(e)).find((b)=>i.test(b.url))??null;if(!m)await new Promise((b)=>setTimeout(b,u))}if(!m){console.log(JSON.stringify({error:{code:"xhr_not_seen",message:`No captured XHR matched /${o}/ within ${n}ms`,hint:"Check the pattern against `browser network` output; the endpoint may not have fired yet, or capture is disabled."}},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify({matched:{url:m.url,status:m.status,contentType:m.ct}},null,2))}else if(t==="download"){if(typeof e.waitForDownload!=="function"){console.log(JSON.stringify({error:{code:"download_wait_unavailable",message:"The active browser backend does not support download lifecycle waits.",hint:"Use the Browser Bridge extension version 1.0.8 or newer, then retry the command."}},null,2));process.exitCode=f.GENERIC_ERROR;return}const i=await e.waitForDownload(String(o??""),n);if(!i.downloaded){const s=i.state==="interrupted"&&i.id!==void 0?"download_failed":"download_not_seen";console.log(JSON.stringify({error:{code:s,message:i.error??`No download matched "${o??"*"}" within ${n}ms`,hint:"Check the pattern against the expected filename or URL; use a longer --timeout if the download starts slowly."},download:i},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify(i,null,2))}else{console.error(`Unknown wait type "${t}". Use: selector, text, time, xhr, or download`);process.exitCode=f.USAGE_ERROR}}));C(h.command("eval").argument("<js>","JavaScript code").option("--frame <index>",'Cross-origin iframe index from "browser frames"').description("Execute JS in page context, return result (top-level await is supported; the trailing expression is the result)")).action(x(async(e,t,o)=>{const r=Uo(t);let n;if(o.frame!==void 0){const i=Number.parseInt(o.frame,10);if(!Number.isInteger(i)||i<0){console.error(`Invalid frame index "${o.frame}". Use a 0-based index from "browser frames".`);process.exitCode=f.USAGE_ERROR;return}if(!e.evaluateInFrame)throw Error("This browser session does not support frame-targeted evaluation");n=await e.evaluateInFrame(r,i)}else n=await e.evaluate(r);if(typeof n==="string")console.log(n);else console.log(JSON.stringify(n,null,2))}));C(h.command("extract").option("--selector <css>","CSS selector scope; defaults to <main>/<article>/<body>").option("--chunk-size <chars>","Target chunk size in chars","20000").option("--start <char>","Start offset (use next_start_char from a previous extract)","0").description("Extract page content as markdown, paragraph-aware chunks for long pages")).action(x(async(e,t)=>{const o=String(t.chunkSize??"20000");if(!/^\d+$/.test(o)||Number.parseInt(o,10)<=0){console.log(JSON.stringify({error:{code:"invalid_chunk_size",message:`--chunk-size must be a positive integer, got "${t.chunkSize}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const r=String(t.start??"0");if(!/^\d+$/.test(r)){console.log(JSON.stringify({error:{code:"invalid_start",message:`--start must be a non-negative integer, got "${t.start}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=Number.parseInt(o,10),i=Number.parseInt(r,10),s=typeof t.selector==="string"&&t.selector.length>0?t.selector:null,d=qo(s),u=await e.evaluate(d);if(!u){console.log(JSON.stringify({error:{code:"extract_failed",message:"Page returned no root element."}},null,2));process.exitCode=f.USAGE_ERROR;return}if("invalidSelector"in u){console.log(JSON.stringify({error:{code:"invalid_selector",message:`Selector "${s}" is not a valid CSS selector: ${u.reason}`}},null,2));process.exitCode=f.USAGE_ERROR;return}if("notFound"in u){console.log(JSON.stringify({error:{code:"selector_not_found",message:s?`Selector "${s}" matched 0 elements.`:"Page has no body/main/article element."}},null,2));process.exitCode=f.USAGE_ERROR;return}const m=Vo({html:u.html,url:u.url,title:u.title,selector:s,start:i,chunkSize:n});console.log(JSON.stringify(m,null,2))}));C(h.command("network")).option("--detail <key>","Emit full body for the entry with this key").option("--all","Include static resources (js/css/images/telemetry)").option("--raw","Emit full bodies for every entry (skip shape preview)").option("--filter <fields>","Comma-separated field names; keep only entries whose body shape has ALL names as path segments").option("--since <duration>","Only include entries from the last duration (for example: 30s, 2m)").option("--until <duration>","Only include entries older than the duration from now").option("--follow","Continuously print new matching entries as JSON lines",!1).option("--failed","Only include failed HTTP requests (status 0 or >= 400)",!1).option("--max-body <chars>","With --detail: cap the emitted body at N chars (0 = unlimited, default)","0").option("--ttl <ms>","Cache TTL in ms for --detail lookups",String(Pt)).description("Capture network requests as shape previews; retrieve full bodies by key").action(x(async(e,t)=>{const o=Yt(t.ttl,"ttl",Pt),r=at(e),n=typeof t.detail==="string"&&t.detail.length>0,i=typeof t.filter==="string",s=Te(t.since,"since"),d=Te(t.until,"until");if(s&&typeof s==="object"){Z("invalid_since",s.error);return}if(d&&typeof d==="object"){Z("invalid_until",d.error);return}if(n&&i){Z("invalid_args","--filter and --detail cannot be used together (one narrows a list, the other fetches a specific entry).");return}let u=null;if(i){const w=zo(t.filter);if("reason"in w){Z("invalid_filter",w.reason);return}u=w.fields}if(n&&t.follow){Z("invalid_args","--follow cannot be used with --detail.");return}if(n){const w=Bo(r,{ttlMs:o});if(w.status==="missing"){Z("cache_missing",`No cached capture. Run "browser network" first (in session "${r}").`);return}if(w.status==="expired"){Z("cache_expired",`Cache is stale (age ${w.ageMs}ms > ttl ${o}ms). Re-run "browser network" to refresh.`);return}if(w.status==="corrupt"||!w.file){Z("cache_corrupt",'Cache file is malformed; re-run "browser network" to regenerate.');return}const O=Fo(w.file,t.detail);if(!O){Z("key_not_found",`Key "${t.detail}" not in cache.`,{available_keys:w.file.entries.map((H)=>H.key)});return}const y=String(t.maxBody??"0");if(!/^\d+$/.test(y)){Z("invalid_max_body",`--max-body must be a non-negative integer, got "${t.maxBody}"`);return}const S=Number.parseInt(y,10);let U=O.body,L=!1;if(S>0&&typeof O.body==="string"&&O.body.length>S){U=O.body.slice(0,S);L=!0}const ae=O.body_truncated===!0,q={key:O.key,url:O.url,method:O.method,status:O.status,ct:O.ct,size:O.size,...typeof O.timestamp==="number"?{timestamp:we(O.timestamp)}:{},shape:Tt(O.body),body:U};if(ae||L){q.body_truncated=!0;q.body_full_size=O.body_full_size??O.size;q.body_truncation_reason=ae?"capture-limit":"max-body"}console.log(JSON.stringify(q,null,2));return}if(t.follow){if(!await e.startNetworkCapture?.())try{await e.evaluate(Ae)}catch{}while(!0){const w=await ye(e).catch((S)=>{Z("capture_failed",`Could not read network capture: ${S.message}`);return[]});let O=t.all?w:Bt(w);O=ot(O,{sinceMs:s,untilMs:d});if(t.failed)O=O.filter((S)=>S.status===0||S.status>=400);const y=It(O);for(const S of y){const U=S.channel==="websocket"||S.channel==="eventsource";console.log(JSON.stringify({key:S.key,timestamp:we(S.timestamp),method:S.method,status:S.status,url:S.url,ct:S.ct,size:S.size,...U?{channel:S.channel,direction:S.direction}:{},...S.eventName?{event_name:S.eventName}:{},...U?{body:xr(S.body)}:{},...S.bodyTruncated?{body_truncated:!0}:{}}))}await new Promise((S)=>setTimeout(S,Gt))}}let m;try{m=await ye(e)}catch(w){Z("capture_failed",`Could not read network capture: ${w.message}`);return}let g=t.all?m:Bt(m);g=ot(g,{sinceMs:s,untilMs:d});if(t.failed)g=g.filter((w)=>w.status===0||w.status>=400);const b=m.length-g.length,I=It(g).map((w)=>({key:w.key,url:w.url,method:w.method,status:w.status,size:w.size,ct:w.ct,body:w.body,...typeof w.timestamp==="number"?{timestamp:w.timestamp}:{},...w.bodyTruncated?{body_truncated:!0}:{},...w.bodyTruncated&&typeof w.bodyFullSize==="number"?{body_full_size:w.bodyFullSize}:{}}));let G=null;try{Lo(r,I)}catch(w){G=`Could not persist capture cache: ${w.message}. --detail lookups may miss this capture.`}const _=I.map((w)=>({entry:w,shape:Tt(w.body)})),D=u?_.filter((w)=>Wo(w.shape,u)):_,j=u?_.length-D.length:0,W={session:r,captured_at:new Date().toISOString(),count:D.length,filtered_out:b};if(u){W.filter=u;W.filter_dropped=j}if(G)W.cache_warning=G;const ne=D.filter((w)=>w.entry.body_truncated).length;if(ne>0){W.body_truncated_count=ne;W.body_truncated_hint="Some bodies exceeded the capture limit; their `shape` reflects only the captured prefix."}if(t.raw)W.entries=D.map((w)=>({...w.entry,...typeof w.entry.timestamp==="number"?{timestamp:we(w.entry.timestamp)}:{}}));else{W.entries=D.map((w)=>({key:w.entry.key,method:w.entry.method,...typeof w.entry.timestamp==="number"?{timestamp:we(w.entry.timestamp)}:{},status:w.entry.status,url:w.entry.url,ct:w.entry.ct,size:w.entry.size,shape:w.shape,...w.entry.body_truncated?{body_truncated:!0}:{}}));W.detail_hint='Run "browser network --detail <key>" for full body.'}console.log(JSON.stringify(W,null,2))}));h.command("init").argument("<name>","Adapter name in site/command format (e.g. hn/top)").description("Generate adapter scaffold in ~/.ppcli/clis/").action(async(e)=>{try{const t=e.split("/");if(t.length!==2||!t[0]||!t[1]){console.error("Name must be site/command format (e.g. hn/top)");process.exitCode=f.USAGE_ERROR;return}const[o,r]=t;if(!/^[a-zA-Z0-9_-]+$/.test(o)||!/^[a-zA-Z0-9_-]+$/.test(r)){console.error("Name parts must be alphanumeric/dash/underscore only");process.exitCode=f.USAGE_ERROR;return}const n=await import("node:os"),i=await import("node:fs"),s=await import("node:path"),d=s.join(n.homedir(),".ppcli","clis",o),u=s.join(d,`${r}.js`);if(i.existsSync(u)){console.log(`Adapter already exists: ${u}`);return}const g=`import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
106
|
+
${d.slice(0,n)}`);return}console.log(d)}));C(z(me.command("attributes")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","Pick the nth match (0-based) when <target> is a multi-match CSS selector").description("Element attributes — JSON envelope {value, matches_n}")).action(x(async(e,t,o)=>Be(e,t,o??{},jo(),"attributes")));function ee(e){const t=N(e);if(t&&typeof t==="object"&&"error"in t)return t;if(typeof t==="number")return{opts:{nth:t}};return{opts:{}}}function oo(e){const t=Array.isArray(e)?e:[];if(t.length===0)return{error:{code:"usage_error",message:"At least one file path is required.",hint:'Example: ppcli browser upload "input[type=file]" ./receipt.pdf'}};const o=[];for(const r of t){const n=String(r),i=n==="~"||n.startsWith(`~${k.sep}`)?k.join(ge.homedir(),n.slice(2)):n,s=k.resolve(i);if(!A.existsSync(s))return{error:{code:"file_not_found",message:`File not found: ${s}`}};if(!A.statSync(s).isFile())return{error:{code:"not_a_file",message:`Not a regular file: ${s}`}};o.push(s)}return{files:o}}function Le(e,t){const o=N(e);if(o&&typeof o==="object"&&"error"in o)return{error:o.error.replace("--nth",t)};if(typeof o==="number")return{opts:{nth:o}};return{opts:{}}}C(z(h.command("click")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Click element — JSON envelope {clicked, target, matches_n}")).action(x(async(e,t,o)=>{const r=await Ge(e,t,o??{},"write");if(typeof r!=="string"){console.log(JSON.stringify(r,null,2));process.exitCode=f.USAGE_ERROR;return}const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s,click_method:d,hit:m,retargeted:u}=await e.click(r,n.opts);console.log(JSON.stringify({clicked:!0,target:r,matches_n:i,match_level:s,...d&&{click_method:d},...m&&{hit:m},...u&&{retargeted:u}},null,2))}));C(z(h.command("type")).argument("[targetOrText]","Numeric ref/CSS target, or text when using --role/--name/etc.").argument("[text]","Text to type").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Click element, then type text — JSON envelope {typed, text, target, matches_n, autocomplete}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"text");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}await e.click(n.target,i.opts);await e.wait(0.3);const{matches_n:s,match_level:d}=await e.typeText(n.target,n.value,i.opts),m=await e.evaluate(Do());if(m)await e.wait(0.4);console.log(JSON.stringify({typed:!0,text:n.value,target:n.target,matches_n:s,match_level:d,autocomplete:!!m},null,2))}));C(z(h.command("hover")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Move the mouse over an element — JSON envelope {hovered, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.hover!=="function")throw Error("browser hover is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s}=await e.hover(r,n.opts);console.log(JSON.stringify({hovered:!0,target:r,matches_n:i,match_level:s},null,2))}));C(z(h.command("focus")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Focus an element — JSON envelope {focused, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.focus!=="function")throw Error("browser focus is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{focused:i,matches_n:s,match_level:d}=await e.focus(r,n.opts);console.log(JSON.stringify({focused:i,target:r,matches_n:s,match_level:d},null,2))}));C(z(h.command("dblclick")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Double-click element — JSON envelope {dblclicked, target, matches_n}")).action(x(async(e,t,o)=>{if(typeof e.dblClick!=="function")throw Error("browser dblclick is not supported by this browser backend");const r=await ue(e,t,o??{});if(!r)return;const n=ee(o?.nth);if("error"in n){console.log(JSON.stringify({error:{code:"usage_error",message:n.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:i,match_level:s}=await e.dblClick(r,n.opts);console.log(JSON.stringify({dblclicked:!0,target:r,matches_n:i,match_level:s},null,2))}));const mt=async(e,t,o,r)=>{if(typeof e.setChecked!=="function")throw Error(`browser ${r?"check":"uncheck"} is not supported by this browser backend`);const n=await ue(e,t,o);if(!n)return;const i=ee(o?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=await e.setChecked(n,r,i.opts);console.log(JSON.stringify({checked:s.checked,changed:s.changed,target:n,matches_n:s.matches_n,match_level:s.match_level,...s.kind?{kind:s.kind}:{}},null,2))};C(z(h.command("check")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Ensure a checkbox/radio/aria-checked control is checked — JSON envelope {checked, changed, target, matches_n}")).action(x(async(e,t,o)=>{await mt(e,t,o??{},!0)}));C(z(h.command("uncheck")).argument("[target]","Numeric ref (from browser state / find), CSS selector, or omit when using --role/--name/etc.").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Ensure a checkbox/aria-checked control is unchecked — JSON envelope {checked, changed, target, matches_n}")).action(x(async(e,t,o)=>{await mt(e,t,o??{},!1)}));C(z(h.command("upload")).argument("[targetOrFile]","Numeric ref/CSS target, or first file when using --role/--name/etc.").argument("[files...]","Local file path(s) to attach").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Attach local files to a file input — JSON envelope {uploaded, files, file_names, target, matches_n}")).action(x(async(e,t,o,r)=>{if(typeof e.uploadFiles!=="function")throw Error("browser upload is not supported by this browser backend");const n=!!ve(r??{}),s=await ue(e,n?void 0:t,r??{});if(!s)return;const d=ee(r?.nth);if("error"in d){console.log(JSON.stringify({error:{code:"usage_error",message:d.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const m=n?[t,...Array.isArray(o)?o:[]].filter((b)=>b!==void 0):o,u=oo(m);if("error"in u){console.log(JSON.stringify({error:u.error},null,2));process.exitCode=f.USAGE_ERROR;return}const g=await e.uploadFiles(s,u.files,d.opts);console.log(JSON.stringify(g,null,2))}));C(ct(ct(h.command("drag"),"from"),"to").argument("[source]","Numeric ref/CSS selector to drag from, or omit with --from-role/--from-name/etc.").argument("[target]","Numeric ref/CSS selector to drop onto, or omit with --to-role/--to-name/etc. or --dx/--dy").option("--from-nth <n>","When <source> is a multi-match CSS selector, pick the nth match (0-based)").option("--to-nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").option("--dx <px>","Drag by this many CSS pixels horizontally from the source centre instead of onto a target (negative = left)").option("--dy <px>","Drag by this many CSS pixels vertically from the source centre instead of onto a target (negative = up)").option("--steps <n>","Pointer-move samples between press and release (default: 2 for element drops, ~1 per 12px for --dx/--dy)").description("Drag one element onto another, or by a pixel offset with --dx/--dy (sliders) — JSON envelope {dragged, source, target | dx, dy, from, to}")).action(x(async(e,t,o,r)=>{if(typeof e.drag!=="function")throw Error("browser drag is not supported by this browser backend");const n=(_,D)=>{console.log(JSON.stringify({error:{code:"usage_error",message:_,...D?{hint:D}:{}}},null,2));process.exitCode=f.USAGE_ERROR},i=(_,D)=>{if(_===void 0||_===null||_==="")return null;const j=Number(String(_).trim().replace(/px$/i,""));if(!Number.isFinite(j))return{error:`${D} must be a number of CSS pixels, got "${String(_)}"`};return j},s=i(r?.dx,"--dx"),d=i(r?.dy,"--dy");for(const _ of[s,d])if(_&&typeof _==="object")return n(_.error);let m;if(r?.steps!==void 0){m=Number(r.steps);if(!Number.isInteger(m)||m<1||m>200)return n(`--steps must be an integer between 1 and 200, got "${String(r.steps)}"`)}const u=s!==null||d!==null;if(u){if(typeof o==="string"&&o.length>0||lt(r??{},"to")!==null||r?.toNth!==void 0)return n("Pass either a drop target or --dx/--dy, not both.","Element drop: browser drag <source> <target>. Pixel drag: browser drag <source> --dx 180.");if((s??0)===0&&(d??0)===0)return n("--dx/--dy are both 0 — nothing to drag.");if(typeof e.dragBy!=="function")throw Error("browser drag --dx/--dy is not supported by this browser backend")}const g=await ut(e,t,r??{},"from","source");if(!g)return;if(u){const _=Le(r?.fromNth,"--from-nth");if("error"in _)return n(_.error);const D=await e.dragBy(g,{dx:s??0,dy:d??0},{from:_.opts,...m!==void 0?{steps:m}:{}});console.log(JSON.stringify(D,null,2));return}const b=await ut(e,o,r??{},"to","target");if(!b)return;const R=Le(r?.fromNth,"--from-nth");if("error"in R){console.log(JSON.stringify({error:{code:"usage_error",message:R.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const I=Le(r?.toNth,"--to-nth");if("error"in I){console.log(JSON.stringify({error:{code:"usage_error",message:I.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const G=await e.drag(g,b,{from:R.opts,to:I.opts,...m!==void 0?{steps:m}:{}});console.log(JSON.stringify(G,null,2))}));C(z(h.command("fill")).argument("[targetOrText]","Numeric ref/CSS target, or text when using --role/--name/etc.").argument("[text]","Text to set exactly").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Set input/textarea/contenteditable text exactly and verify the value — JSON envelope {filled, verified, text, actual}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"text");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const s=await e.fillText(n.target,n.value,i.opts);if(!s.verified)process.exitCode=f.GENERIC_ERROR;console.log(JSON.stringify({filled:s.filled,verified:s.verified,target:n.target,text:n.value,actual:s.actual,length:s.length,matches_n:s.matches_n,match_level:s.match_level,...s.mode?{mode:s.mode}:{}},null,2))}));C(z(h.command("select")).argument("[targetOrOption]","Numeric ref/CSS target, or option text when using --role/--name/etc.").argument("[option]","Option text (or value) to select").option("--nth <n>","When <target> is a multi-match CSS selector, pick the nth match (0-based)").description("Select dropdown option — JSON envelope {selected, target, matches_n}")).action(x(async(e,t,o,r)=>{const n=await Fe(e,t,o,r??{},"option");if(!n)return;const i=ee(r?.nth);if("error"in i){console.log(JSON.stringify({error:{code:"usage_error",message:i.error}},null,2));process.exitCode=f.USAGE_ERROR;return}const{matches_n:s,match_level:d}=await E(e,n.target,i.opts),m=await e.evaluate(Mo(n.value));if(m?.error){console.log(JSON.stringify({error:{code:m.error==="Not a <select>"?"not_a_select":"option_not_found",message:m.error,...m.available&&{available:m.available},matches_n:s}},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify({selected:m?.selected??n.value,target:n.target,matches_n:s,match_level:d},null,2))}));C(h.command("keys").argument("<key>","Key to press (Enter, Escape, Tab, Control+a)")).description("Press keyboard key").action(x(async(e,t)=>{await e.pressKey(t);console.log(`Pressed: ${t}`)}));const pt=h.command("dialog").description("Handle a blocking JavaScript alert/confirm/prompt dialog");C(pt.command("accept").option("--text <text>","Prompt text to submit for prompt() dialogs").description("Accept the currently open JavaScript dialog")).action(x(async(e,t)=>{if(!e.handleJavaScriptDialog)throw Error("This browser session does not support JavaScript dialog handling");try{await e.handleJavaScriptDialog(!0,t?.text)}catch(o){if(J(o).toLowerCase().includes("no dialog")){console.log(JSON.stringify({error:{code:"no_javascript_dialog",message:"No JavaScript dialog is currently open."}},null,2));process.exitCode=f.USAGE_ERROR;return}throw o}console.log(JSON.stringify({handled:!0,action:"accept",...t?.text!==void 0&&{text:t.text}},null,2))}));C(pt.command("dismiss").description("Dismiss the currently open JavaScript dialog")).action(x(async(e)=>{if(!e.handleJavaScriptDialog)throw Error("This browser session does not support JavaScript dialog handling");try{await e.handleJavaScriptDialog(!1)}catch(t){if(J(t).toLowerCase().includes("no dialog")){console.log(JSON.stringify({error:{code:"no_javascript_dialog",message:"No JavaScript dialog is currently open."}},null,2));process.exitCode=f.USAGE_ERROR;return}throw t}console.log(JSON.stringify({handled:!0,action:"dismiss"},null,2))}));C(h.command("wait")).argument("<type>","selector, text, time, xhr, or download").argument("[value]","CSS selector, text string, seconds, XHR URL regex, or download filename/URL pattern").option("--timeout <ms>","Timeout in milliseconds","10000").description('Wait for selector, text, time, matching XHR, or browser download (e.g. wait selector ".loaded", wait text "Success", wait time 3, wait xhr "/api/search", wait download receipt.pdf)').action(x(async(e,t,o,r)=>{const n=parseInt(r.timeout,10);if(t==="time"){const i=parseFloat(o??"2");await e.wait(i);console.log(`Waited ${i}s`)}else if(t==="selector"){if(!o){console.error("Missing CSS selector");process.exitCode=f.USAGE_ERROR;return}await e.wait({selector:o,timeout:n/1000});console.log(`Element "${o}" appeared`)}else if(t==="text"){if(!o){console.error("Missing text");process.exitCode=f.USAGE_ERROR;return}await e.wait({text:o,timeout:n/1000});console.log(`Text "${o}" appeared`)}else if(t==="xhr"){if(!o){console.error("Missing XHR URL regex");process.exitCode=f.USAGE_ERROR;return}let i;try{i=new RegExp(o)}catch(g){console.error(`Invalid regex "${o}": ${g instanceof Error?g.message:String(g)}`);process.exitCode=f.USAGE_ERROR;return}if(!(await e.startNetworkCapture?.()??!1))try{await e.evaluate(Ae)}catch{}await ye(e);const d=Date.now()+n,m=400;let u=null;while(Date.now()<d&&!u){u=(await ye(e)).find((b)=>i.test(b.url))??null;if(!u)await new Promise((b)=>setTimeout(b,m))}if(!u){console.log(JSON.stringify({error:{code:"xhr_not_seen",message:`No captured XHR matched /${o}/ within ${n}ms`,hint:"Check the pattern against `browser network` output; the endpoint may not have fired yet, or capture is disabled."}},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify({matched:{url:u.url,status:u.status,contentType:u.ct}},null,2))}else if(t==="download"){if(typeof e.waitForDownload!=="function"){console.log(JSON.stringify({error:{code:"download_wait_unavailable",message:"The active browser backend does not support download lifecycle waits.",hint:"Use the Browser Bridge extension version 1.0.8 or newer, then retry the command."}},null,2));process.exitCode=f.GENERIC_ERROR;return}const i=await e.waitForDownload(String(o??""),n);if(!i.downloaded){const s=i.state==="interrupted"&&i.id!==void 0?"download_failed":"download_not_seen";console.log(JSON.stringify({error:{code:s,message:i.error??`No download matched "${o??"*"}" within ${n}ms`,hint:"Check the pattern against the expected filename or URL; use a longer --timeout if the download starts slowly."},download:i},null,2));process.exitCode=f.GENERIC_ERROR;return}console.log(JSON.stringify(i,null,2))}else{console.error(`Unknown wait type "${t}". Use: selector, text, time, xhr, or download`);process.exitCode=f.USAGE_ERROR}}));C(h.command("eval").argument("<js>","JavaScript code").option("--frame <index>",'Cross-origin iframe index from "browser frames"').description("Execute JS in page context, return result (top-level await is supported; the trailing expression is the result)")).action(x(async(e,t,o)=>{const r=Uo(t);let n;if(o.frame!==void 0){const i=Number.parseInt(o.frame,10);if(!Number.isInteger(i)||i<0){console.error(`Invalid frame index "${o.frame}". Use a 0-based index from "browser frames".`);process.exitCode=f.USAGE_ERROR;return}if(!e.evaluateInFrame)throw Error("This browser session does not support frame-targeted evaluation");n=await e.evaluateInFrame(r,i)}else n=await e.evaluate(r);if(typeof n==="string")console.log(n);else console.log(JSON.stringify(n,null,2))}));C(h.command("extract").option("--selector <css>","CSS selector scope; defaults to <main>/<article>/<body>").option("--chunk-size <chars>","Target chunk size in chars","20000").option("--start <char>","Start offset (use next_start_char from a previous extract)","0").option("--grep <regex>",'Return only markdown lines matching this case-insensitive regex, e.g. "余额|优惠券|登录"').description("Extract page content as markdown (up to 20000 chars per chunk; --grep <regex> returns only matching lines)")).action(x(async(e,t)=>{const o=String(t.chunkSize??"20000");if(!/^\d+$/.test(o)||Number.parseInt(o,10)<=0){console.log(JSON.stringify({error:{code:"invalid_chunk_size",message:`--chunk-size must be a positive integer, got "${t.chunkSize}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const r=String(t.start??"0");if(!/^\d+$/.test(r)){console.log(JSON.stringify({error:{code:"invalid_start",message:`--start must be a non-negative integer, got "${t.start}"`}},null,2));process.exitCode=f.USAGE_ERROR;return}const n=Number.parseInt(o,10),i=Number.parseInt(r,10),s=typeof t.selector==="string"&&t.selector.length>0?t.selector:null,d=typeof t.grep==="string"&&t.grep.length>0?t.grep:null;if(d)try{new RegExp(d,"i")}catch(b){console.log(JSON.stringify({error:{code:"invalid_grep",message:`--grep "${d}" is not a valid regex: ${b.message}`}},null,2));process.exitCode=f.USAGE_ERROR;return}const m=qo(s),u=await e.evaluate(m);if(!u){console.log(JSON.stringify({error:{code:"extract_failed",message:"Page returned no root element."}},null,2));process.exitCode=f.USAGE_ERROR;return}if("invalidSelector"in u){console.log(JSON.stringify({error:{code:"invalid_selector",message:`Selector "${s}" is not a valid CSS selector: ${u.reason}`}},null,2));process.exitCode=f.USAGE_ERROR;return}if("notFound"in u){console.log(JSON.stringify({error:{code:"selector_not_found",message:s?`Selector "${s}" matched 0 elements.`:"Page has no body/main/article element."}},null,2));process.exitCode=f.USAGE_ERROR;return}const g=Vo({html:u.html,url:u.url,title:u.title,selector:s,start:i,chunkSize:n,grep:d});console.log(JSON.stringify(g,null,2))}));C(h.command("network")).option("--detail <key>","Emit full body for the entry with this key").option("--all","Include static resources (js/css/images/telemetry)").option("--raw","Emit full bodies for every entry (skip shape preview)").option("--filter <fields>","Comma-separated field names; keep only entries whose body shape has ALL names as path segments").option("--since <duration>","Only include entries from the last duration (for example: 30s, 2m)").option("--until <duration>","Only include entries older than the duration from now").option("--follow","Continuously print new matching entries as JSON lines",!1).option("--failed","Only include failed HTTP requests (status 0 or >= 400)",!1).option("--max-body <chars>","With --detail: cap the emitted body at N chars (0 = unlimited, default)","0").option("--ttl <ms>","Cache TTL in ms for --detail lookups",String(Pt)).description("Capture network requests as shape previews; retrieve full bodies by key").action(x(async(e,t)=>{const o=Yt(t.ttl,"ttl",Pt),r=at(e),n=typeof t.detail==="string"&&t.detail.length>0,i=typeof t.filter==="string",s=Te(t.since,"since"),d=Te(t.until,"until");if(s&&typeof s==="object"){Z("invalid_since",s.error);return}if(d&&typeof d==="object"){Z("invalid_until",d.error);return}if(n&&i){Z("invalid_args","--filter and --detail cannot be used together (one narrows a list, the other fetches a specific entry).");return}let m=null;if(i){const w=zo(t.filter);if("reason"in w){Z("invalid_filter",w.reason);return}m=w.fields}if(n&&t.follow){Z("invalid_args","--follow cannot be used with --detail.");return}if(n){const w=Bo(r,{ttlMs:o});if(w.status==="missing"){Z("cache_missing",`No cached capture. Run "browser network" first (in session "${r}").`);return}if(w.status==="expired"){Z("cache_expired",`Cache is stale (age ${w.ageMs}ms > ttl ${o}ms). Re-run "browser network" to refresh.`);return}if(w.status==="corrupt"||!w.file){Z("cache_corrupt",'Cache file is malformed; re-run "browser network" to regenerate.');return}const O=Fo(w.file,t.detail);if(!O){Z("key_not_found",`Key "${t.detail}" not in cache.`,{available_keys:w.file.entries.map((H)=>H.key)});return}const y=String(t.maxBody??"0");if(!/^\d+$/.test(y)){Z("invalid_max_body",`--max-body must be a non-negative integer, got "${t.maxBody}"`);return}const S=Number.parseInt(y,10);let U=O.body,L=!1;if(S>0&&typeof O.body==="string"&&O.body.length>S){U=O.body.slice(0,S);L=!0}const ae=O.body_truncated===!0,q={key:O.key,url:O.url,method:O.method,status:O.status,ct:O.ct,size:O.size,...typeof O.timestamp==="number"?{timestamp:we(O.timestamp)}:{},shape:Tt(O.body),body:U};if(ae||L){q.body_truncated=!0;q.body_full_size=O.body_full_size??O.size;q.body_truncation_reason=ae?"capture-limit":"max-body"}console.log(JSON.stringify(q,null,2));return}if(t.follow){if(!await e.startNetworkCapture?.())try{await e.evaluate(Ae)}catch{}while(!0){const w=await ye(e).catch((S)=>{Z("capture_failed",`Could not read network capture: ${S.message}`);return[]});let O=t.all?w:Bt(w);O=ot(O,{sinceMs:s,untilMs:d});if(t.failed)O=O.filter((S)=>S.status===0||S.status>=400);const y=It(O);for(const S of y){const U=S.channel==="websocket"||S.channel==="eventsource";console.log(JSON.stringify({key:S.key,timestamp:we(S.timestamp),method:S.method,status:S.status,url:S.url,ct:S.ct,size:S.size,...U?{channel:S.channel,direction:S.direction}:{},...S.eventName?{event_name:S.eventName}:{},...U?{body:xr(S.body)}:{},...S.bodyTruncated?{body_truncated:!0}:{}}))}await new Promise((S)=>setTimeout(S,Gt))}}let u;try{u=await ye(e)}catch(w){Z("capture_failed",`Could not read network capture: ${w.message}`);return}let g=t.all?u:Bt(u);g=ot(g,{sinceMs:s,untilMs:d});if(t.failed)g=g.filter((w)=>w.status===0||w.status>=400);const b=u.length-g.length,I=It(g).map((w)=>({key:w.key,url:w.url,method:w.method,status:w.status,size:w.size,ct:w.ct,body:w.body,...typeof w.timestamp==="number"?{timestamp:w.timestamp}:{},...w.bodyTruncated?{body_truncated:!0}:{},...w.bodyTruncated&&typeof w.bodyFullSize==="number"?{body_full_size:w.bodyFullSize}:{}}));let G=null;try{Lo(r,I)}catch(w){G=`Could not persist capture cache: ${w.message}. --detail lookups may miss this capture.`}const _=I.map((w)=>({entry:w,shape:Tt(w.body)})),D=m?_.filter((w)=>Wo(w.shape,m)):_,j=m?_.length-D.length:0,W={session:r,captured_at:new Date().toISOString(),count:D.length,filtered_out:b};if(m){W.filter=m;W.filter_dropped=j}if(G)W.cache_warning=G;const ne=D.filter((w)=>w.entry.body_truncated).length;if(ne>0){W.body_truncated_count=ne;W.body_truncated_hint="Some bodies exceeded the capture limit; their `shape` reflects only the captured prefix."}if(t.raw)W.entries=D.map((w)=>({...w.entry,...typeof w.entry.timestamp==="number"?{timestamp:we(w.entry.timestamp)}:{}}));else{W.entries=D.map((w)=>({key:w.entry.key,method:w.entry.method,...typeof w.entry.timestamp==="number"?{timestamp:we(w.entry.timestamp)}:{},status:w.entry.status,url:w.entry.url,ct:w.entry.ct,size:w.entry.size,shape:w.shape,...w.entry.body_truncated?{body_truncated:!0}:{}}));W.detail_hint='Run "browser network --detail <key>" for full body.'}console.log(JSON.stringify(W,null,2))}));h.command("init").argument("<name>","Adapter name in site/command format (e.g. hn/top)").description("Generate adapter scaffold in ~/.ppcli/clis/").action(async(e)=>{try{const t=e.split("/");if(t.length!==2||!t[0]||!t[1]){console.error("Name must be site/command format (e.g. hn/top)");process.exitCode=f.USAGE_ERROR;return}const[o,r]=t;if(!/^[a-zA-Z0-9_-]+$/.test(o)||!/^[a-zA-Z0-9_-]+$/.test(r)){console.error("Name parts must be alphanumeric/dash/underscore only");process.exitCode=f.USAGE_ERROR;return}const n=await import("node:os"),i=await import("node:fs"),s=await import("node:path"),d=s.join(n.homedir(),".ppcli","clis",o),m=s.join(d,`${r}.js`);if(i.existsSync(m)){console.log(`Adapter already exists: ${m}`);return}const g=`import { cli, Strategy } from '@jackwener/opencli/registry';
|
|
107
107
|
|
|
108
108
|
cli({
|
|
109
109
|
site: '${o}',
|
|
@@ -125,7 +125,7 @@ cli({
|
|
|
125
125
|
return [];
|
|
126
126
|
},
|
|
127
127
|
});
|
|
128
|
-
`;i.mkdirSync(d,{recursive:!0});i.writeFileSync(
|
|
128
|
+
`;i.mkdirSync(d,{recursive:!0});i.writeFileSync(m,g,"utf-8");console.log(`Created: ${m}`);console.log("First time on this site? Run: ppcli browser analyze <url>");console.log(`Edit the file to implement your adapter, then run: ppcli browser verify ${e}`)}catch(t){console.error(`Error: ${t instanceof Error?t.message:String(t)}`);process.exitCode=f.GENERIC_ERROR}});h.command("verify").argument("<name>","Adapter name in site/command format (e.g. hn/top)").option("--write-fixture","Write a starter fixture to ~/.ppcli/sites/<site>/verify/<command>.json if none exists").option("--update-fixture","Overwrite an existing fixture with one derived from current output").option("--no-fixture","Ignore any fixture file for this run (no value-level validation)").option("--strict-memory","Fail (not just warn) when ~/.ppcli/sites/<site>/endpoints.json or notes.md is missing").option("--seed-args <value>","Seed args when no fixture exists; use JSON array/object for multiple args or flags").option("--trace <mode>","Trace capture for the adapter subprocess: off, on, retain-on-failure","off").description("Execute an adapter and validate output; uses fixture at ~/.ppcli/sites/<site>/verify/<cmd>.json when present").action(async(e,t={})=>{try{const o=e.split("/");if(o.length!==2){console.error("Name must be site/command format");process.exitCode=f.USAGE_ERROR;return}const[r,n]=o;if(!/^[a-zA-Z0-9_-]+$/.test(r)||!/^[a-zA-Z0-9_-]+$/.test(n)){console.error("Name parts must be alphanumeric/dash/underscore only");process.exitCode=f.USAGE_ERROR;return}const{execFileSync:i}=await import("node:child_process"),{loadFixture:s,writeFixture:d,deriveFixture:m,validateRows:u,validateRowShape:g,fixturePath:b,expandFixtureArgs:R,parseSeedArgs:I}=await import("./browser/verify-fixture.js"),G=k.join(l||k.join(ge.homedir(),".ppcli","clis"),r,`${n}.js`),_=A.existsSync(G)||!c?G:k.join(c,r,`${n}.js`);if(!A.existsSync(_)){console.error(`Adapter not found: ${_}`);console.error(`Run "ppcli browser init ${e}" to create it.`);process.exitCode=f.GENERIC_ERROR;return}console.log(`\uD83D\uDD0D Verifying ${e}...
|
|
129
129
|
`);console.log(` Loading: ${_}`);let j=t.fixture!==!1?s(r,n):null;const W=A.readFileSync(_,"utf-8"),ne=/['"]limit['"]/.test(W),w=I(t.seedArgs),O=j?.args??w,y=R(O);if(O===void 0&&y.length===0&&ne)y.push("--limit","3");const S=t.trace&&t.trace!=="off"?["--trace",t.trace]:[],U=[...y,...S].join(" "),L=resolveBrowserVerifyInvocation(),ae=[...L.args,r,n,...y,...S,"--format","json"];let q;try{q=i(L.binary,ae,{cwd:L.cwd,timeout:30000,encoding:"utf-8",env:process.env,stdio:["pipe","pipe","pipe"],...L.shell?{shell:!0}:{}})}catch(M){console.log(` Executing: ppcli ${r} ${n} ${U}
|
|
130
130
|
`);const V=M;if(V.stdout)console.log(String(V.stdout));if(V.stderr)console.error(String(V.stderr).slice(0,500));console.log(`
|
|
131
131
|
✗ Adapter failed. Fix the code and try again.`);process.exitCode=f.GENERIC_ERROR;return}console.log(` Executing: ppcli ${r} ${n} ${U}
|
|
@@ -133,10 +133,10 @@ cli({
|
|
|
133
133
|
→ ${H.length} row${H.length===1?"":"s"}`);const ce=g(H);if(ce.length>0){console.log(`
|
|
134
134
|
✗ Adapter output violates row shape conventions:`);for(const M of ce.slice(0,20)){const V=M.rowIndex!==void 0?`row[${M.rowIndex}] `:"";console.log(` - [${M.rule}] ${V}${M.detail}`)}if(ce.length>20)console.log(` ... and ${ce.length-20} more failure(s)`);console.log(`
|
|
135
135
|
Keep rows agent-native: <=12 top-level keys, nesting depth <=1, and id-shaped fields at top level.`);process.exitCode=f.GENERIC_ERROR;return}if(t.writeFixture||t.updateFixture)if(j&&!t.updateFixture){console.log(`
|
|
136
|
-
Fixture already exists at ${b(r,n)}.`);console.log(" Use --update-fixture to overwrite.")}else{const V=
|
|
136
|
+
Fixture already exists at ${b(r,n)}.`);console.log(" Use --update-fixture to overwrite.")}else{const V=m(H,O!==void 0?O:ne?{limit:3}:void 0),po=d(r,n,V);console.log(`
|
|
137
137
|
${j?"↻ Updated":"✎ Wrote"} fixture: ${po}`);console.log(" Review and hand-tune the derived expectations (add patterns / notEmpty, tighten rowCount).");j=V}if(!j){console.log(`
|
|
138
|
-
✓ Adapter runs. (No fixture at ${b(r,n)} — consider --write-fixture to seed one.)`);const M=checkSiteMemory(r);printSiteMemoryReport(M,t.strictMemory);if(!M.ok&&t.strictMemory)process.exitCode=f.GENERIC_ERROR;return}const fe=
|
|
138
|
+
✓ Adapter runs. (No fixture at ${b(r,n)} — consider --write-fixture to seed one.)`);const M=checkSiteMemory(r);printSiteMemoryReport(M,t.strictMemory);if(!M.ok&&t.strictMemory)process.exitCode=f.GENERIC_ERROR;return}const fe=u(H,j);if(fe.length===0){console.log(`
|
|
139
139
|
✓ Adapter matches fixture (${b(r,n)}).`);const M=checkSiteMemory(r);printSiteMemoryReport(M,t.strictMemory);if(!M.ok&&t.strictMemory)process.exitCode=f.GENERIC_ERROR;return}console.log(`
|
|
140
|
-
✗ Adapter output does not match fixture:`);for(const M of fe.slice(0,20)){const V=M.rowIndex!==void 0?`row[${M.rowIndex}] `:"";console.log(` - [${M.rule}] ${V}${M.detail}`)}if(fe.length>20)console.log(` ... and ${fe.length-20} more failure(s)`);process.exitCode=f.GENERIC_ERROR}catch(o){console.error(`Error: ${o instanceof Error?o.message:String(o)}`);process.exitCode=f.GENERIC_ERROR}});h.command("close").description("Release the current browser session tab lease").action(x(async(e)=>{await e.closeWindow?.();console.log("Browser session tab lease released")}));a.command("doctor").description("Diagnose ppcli browser bridge connectivity").option("-v, --verbose","Debug output").action(async(e)=>{Ir(e);const{runBrowserDoctor:t,renderBrowserDoctorReport:o}=await import("./doctor.js"),r=await t({cliVersion:ke});console.log(o(r))});a.command("completion").description("Output shell completion script").argument("<shell>","Shell type: bash, zsh, or fish").action((e)=>{xo(e)});const se=a.command("plugin").description("Manage ppcli plugins"),ro=se.description();se.command("install").description("Install a plugin from a git repository").argument("<source>","Plugin source (e.g. github:user/repo)").action(async(e)=>{const{installPlugin:t}=await import("./plugin.js"),{discoverPlugins:o}=await import("./discovery.js");try{const r=t(e);await o();if(Array.isArray(r))if(r.length===0)console.log("No plugins were installed (all skipped or incompatible).");else console.log(`✅ Installed ${r.length} plugin(s) from monorepo: ${r.join(", ")}`);else console.log(`✅ Plugin "${r}" installed successfully. Commands are ready to use.`)}catch(r){console.error(`Error: ${J(r)}`);process.exitCode=f.GENERIC_ERROR}});se.command("uninstall").description("Uninstall a plugin").argument("<name>","Plugin name").action(async(e)=>{const{uninstallPlugin:t}=await import("./plugin.js");try{t(e);console.log(`✅ Plugin "${e}" uninstalled.`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.GENERIC_ERROR}});se.command("update").description("Update a plugin (or all plugins) to the latest version").argument("[name]","Plugin name (required unless --all is passed)").option("--all","Update all installed plugins").action(async(e,t)=>{if(!e&&!t.all){console.error("Error: Please specify a plugin name or use the --all flag.");process.exitCode=f.USAGE_ERROR;return}if(e&&t.all){console.error("Error: Cannot specify both a plugin name and --all.");process.exitCode=f.USAGE_ERROR;return}const{updatePlugin:o,updateAllPlugins:r}=await import("./plugin.js"),{discoverPlugins:n}=await import("./discovery.js");if(t.all){const i=r();if(i.length>0)await n();let s=!1;console.log(" Update Results:");for(const d of i){if(d.success){console.log(` ✓ ${d.name}`);continue}s=!0;console.log(` ✗ ${d.name} — ${String(d.error)}`)}if(i.length===0){console.log(" No plugins installed.");return}console.log();if(s){console.error("Completed with some errors.");process.exitCode=f.GENERIC_ERROR}else console.log("✅ All plugins updated successfully.");return}try{o(e);await n();console.log(`✅ Plugin "${e}" updated successfully.`)}catch(i){console.error(`Error: ${J(i)}`);process.exitCode=f.GENERIC_ERROR}});se.command("list").description("List installed plugins").option("-f, --format <fmt>","Output format: table, json","table").action(async(e)=>{const{listPlugins:t}=await import("./plugin.js"),o=t();if(o.length===0){console.log(" No plugins installed.");console.log(" Install one with: ppcli plugin install github:user/repo");return}if(e.format==="json"){_e(o,{fmt:"json",columns:["name","commands","source"],title:"ppcli/plugins",source:"ppcli plugin list"});return}console.log();console.log(" Installed plugins");console.log();const r=o.filter((i)=>!i.monorepoName),n=new Map;for(const i of o){if(!i.monorepoName)continue;const s=n.get(i.monorepoName)??[];s.push(i);n.set(i.monorepoName,s)}for(const i of r){const s=i.version?` @${i.version}`:"",d=i.description?` — ${i.description}`:"",
|
|
141
|
-
`);for(const s of n){const
|
|
142
|
-
Official baseline: ${i.length} sites in package`)}catch{console.log("No local adapter overrides. All sites use the official baseline.")}});Re.command("eject").description("Copy an official adapter to ~/.ppcli/clis/ for local editing").argument("<site>","Site name (e.g. twitter, bilibili)").action(async(e)=>{const t=await import("node:os"),o=k.join(t.homedir(),".ppcli","clis"),r=k.join(c,e),n=k.join(o,e);try{await A.promises.access(r)}catch{console.error(`Error: Site "${e}" not found in official adapters.`);process.exitCode=f.USAGE_ERROR;return}try{await A.promises.access(n);console.error(`Site "${e}" already exists in ~/.ppcli/clis/. Use "ppcli adapter reset ${e}" first to restore official version.`);process.exitCode=f.USAGE_ERROR;return}catch{}A.cpSync(r,n,{recursive:!0});console.log(`✅ Ejected "${e}" to ~/.ppcli/clis/${e}/`);console.log("You can now edit the adapter files. Changes take effect immediately.");console.log("Note: Official updates to this adapter will overwrite your changes.")});Re.command("reset").description("Remove local override and restore official adapter version").argument("[site]","Site name (e.g. twitter, bilibili)").option("--all","Reset all local overrides").action(async(e,t)=>{const o=await import("node:os"),r=k.join(o.homedir(),".ppcli","clis");if(t.all){try{const d=(await A.promises.readdir(r,{withFileTypes:!0})).filter((u)=>u.isDirectory());if(d.length===0){console.log("No local sites to reset.");return}for(const u of d)A.rmSync(k.join(r,u.name),{recursive:!0,force:!0});console.log(`✅ Reset ${d.length} site(s). All adapters now use official baseline.`)}catch{console.log("No local sites to reset.")}return}if(!e){console.error("Error: Please specify a site name or use --all.");process.exitCode=f.USAGE_ERROR;return}const n=k.join(r,e);try{await A.promises.access(n)}catch{console.error(`Site "${e}" has no local override.`);return}const i=A.existsSync(k.join(c,e));A.rmSync(n,{recursive:!0,force:!0});console.log(i?`✅ Reset "${e}". Now using official baseline.`:`✅ Removed custom site "${e}".`)});const B=a.command("profile").description("Manage Browser Bridge Chrome profiles"),io=B.description();B.command("list").description("List Chrome profiles: extension-connected and managed (dedicated Chrome instances)").option("-f, --format <fmt>","Output format: text, json","text").action(async(e)=>{const t=await rr(),o=nr(),r=t?.profiles??[],n=!!t&&!hr(t,ke)&&Array.isArray(t.profiles),i=await Promise.all(Ke().map(async({name:u,entry:m})=>({name:u,port:m.port,userDataDir:m.userDataDir,running:await lr(m),isDefault:o.defaultContextId===u,createdAt:m.createdAt??"",label:m.label??"",proxy:m.proxy?{scheme:m.proxy.scheme,host:m.proxy.host,port:m.proxy.port,hasAuth:!!m.proxy.username,username:m.proxy.username??""}:null,fingerprint:m.fingerprint?.enabled?{enabled:!0,timezone:m.fingerprint.timezone??"",locale:m.fingerprint.locale??"",hardwareConcurrency:m.fingerprint.hardwareConcurrency??0,deviceMemory:m.fingerprint.deviceMemory??0,screen:m.fingerprint.screen??null,webglVendor:m.fingerprint.webglVendor??"",webglRenderer:m.fingerprint.webglRenderer??"",canvasNoise:m.fingerprint.canvasNoise===!0}:{enabled:!1},webrtc:m.webrtc!==!1,clean:m.clean!==!1})));if(e.format==="json"){const u=n?r.map((m)=>({contextId:m.contextId,alias:jt(o,m.contextId)??"",connected:!0,version:m.extensionVersion??"",isDefault:o.defaultContextId===m.contextId})):[];console.log(JSON.stringify({managed:i,extension:u,daemonRunning:!!t,daemonStale:!!t&&!n,defaultProfile:o.defaultContextId??""},null,2));return}if(i.length>0){console.log("Managed profiles (dedicated Chrome instances)");console.log();for(const u of i){const m=u.isDefault?" default":"";console.log(` ${u.name}${m} — ${u.running?`running on port ${u.port}`:"stopped"}`)}console.log()}if(!t){console.log("Daemon is not running. Run ppcli doctor after opening Chrome.");return}if(!n){console.log(`Daemon ${gr(t)} is stale for CLI v${ke}.`);console.log("Run: ppcli daemon restart");return}if(r.length===0){console.log("No Browser Bridge profiles connected.");console.log("Open a Chrome profile with the ppcli extension installed, then run ppcli profile list again.");return}const s=new Set(r.map((u)=>u.contextId));console.log("Connected Browser Bridge profiles");console.log();for(const u of r){const m=jt(o,u.contextId),g=o.defaultContextId===u.contextId?" default":"",b=m?` ${m}`:"",R=u.extensionVersion?` v${u.extensionVersion}`:" version unknown";console.log(` ${u.contextId}${b}${g} — connected${R}`)}if(i.some((u)=>u.running)){console.log();console.log("注意:托管 Chrome 也装了同一个扩展,上面的某个 contextId 可能就是它,");console.log(" 无法从这份列表分辨。写操作请显式带 --profile <名字>,不要依赖默认路由。")}const d=Object.entries(o.aliases).filter(([,u])=>!s.has(u));if(d.length>0||o.defaultContextId&&!s.has(o.defaultContextId)){console.log();console.log("Disconnected saved profiles:");const u=new Set;for(const[m,g]of d){u.add(g);console.log(` ${g} ${m} — not connected`)}if(o.defaultContextId&&!u.has(o.defaultContextId)&&!s.has(o.defaultContextId))console.log(` ${o.defaultContextId} — default, not connected`)}});const ft=(e)=>{if(e.config){const r=JSON.parse(e.config);if(typeof r.proxy==="string")r.proxy=Ye(r.proxy);return r}const t={};if(e.label)t.label=e.label;if(e.proxy)t.proxy=Ye(e.proxy);if(e.webrtc===!1)t.webrtc=!1;if(e.clean===!1)t.clean=!1;const o=e.fingerprint;if(o&&o!=="off")t.fingerprint={enabled:!0,spoof:o!=="lite",...e.fpTimezone?{timezone:e.fpTimezone}:{},...e.fpLocale?{locale:e.fpLocale}:{},...e.fpUa?{userAgent:e.fpUa}:{},...e.fpPlatform?{platform:e.fpPlatform}:{},...e.fpSeed?{seed:Number(e.fpSeed)}:{}};return t},gt=(e)=>e?{scheme:e.scheme,host:e.host,port:e.port,hasAuth:!!e.username,username:e.username??""}:null,ht=(e)=>e?.enabled?{enabled:!0,seed:e.seed,timezone:e.timezone??"",locale:e.locale??"",userAgent:e.userAgent??"",platform:e.platform??"",hardwareConcurrency:e.hardwareConcurrency??0,deviceMemory:e.deviceMemory??0,screen:e.screen??null,webglVendor:e.webglVendor??"",webglRenderer:e.webglRenderer??"",canvasNoise:e.canvasNoise===!0}:{enabled:!1},wt=(e)=>e.option("--label <text>","Display label / note for the environment").option("--proxy <url>","Per-env proxy: scheme://[user:pass@]host:port (scheme = http/https/socks5)").option("--fingerprint <mode>","Fingerprint mode: off | on (spoof soft signals) | lite (timezone/locale follow only)").option("--fp-timezone <iana>","Fingerprint timezone, e.g. America/New_York (must be set explicitly)").option("--fp-locale <locale>","Fingerprint locale, e.g. en-US (must be set explicitly)").option("--fp-ua <ua>","Override navigator.userAgent (use with care)").option("--fp-platform <p>","Override navigator.platform, e.g. Win32").option("--fp-seed <n>","Fixed fingerprint seed (for reproducible fingerprint)").option("--no-webrtc","Disable WebRTC leak protection (on by default)").option("--no-clean","Disable lean launch flags / telemetry suppression (on by default)").option("--config <json>","Full config as JSON: { label, proxy, fingerprint } — overrides granular flags");B.command("create").description("Create a managed profile: a dedicated Chrome instance with its own user data dir (for a second account)").argument("<name>","Profile name, e.g. work / brand2").option("--browser-path <path>","Explicit browser executable (defaults to auto-discovered Chrome)").option("--no-start","Create only; do not launch Chrome now").option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=ft(t),{opts:r}=await Dt({...o,browserPath:t.browserPath}),n=await cr(e,{...r,browserPath:t.browserPath});if(t.start!==!1)await Ze(e,n);if(t.format==="json"){console.log(JSON.stringify({ok:!0,name:e,port:n.port,userDataDir:n.userDataDir,label:n.label??"",proxy:gt(n.proxy),fingerprint:ht(n.fingerprint),webrtc:n.webrtc!==!1,clean:n.clean!==!1,started:t.start!==!1},null,2));return}console.log(`✅ 托管 profile "${e}" 已创建(端口 ${n.port})`);console.log(` 数据目录:${n.userDataDir}`);if(n.proxy)console.log(` 代理:${n.proxy.scheme}://${n.proxy.host}:${n.proxy.port}`);if(n.fingerprint?.enabled)console.log(` 指纹:已开启${n.fingerprint.timezone?` 时区 ${n.fingerprint.timezone}`:""}${n.fingerprint.locale?` 语言 ${n.fingerprint.locale}`:""}`);if(t.start!==!1)console.log(` Chrome 已启动。用 ppcli <site> login --profile ${e} 在新窗口里登录账号。`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});wt(B.commands[B.commands.length-1]);B.command("start").description("Launch the dedicated Chrome instance of a managed profile").argument("<name>","Managed profile name").action(async(e)=>{try{const t=Ke().find((r)=>r.name===e.trim());if(!t)throw Error(`托管 profile "${e}" 不存在,先 ppcli profile create ${e}`);const o=await Ze(t.name,t.entry);console.log(`✅ profile "${e}" 运行中(${o})`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});B.command("stop").description("Gracefully close the dedicated Chrome instance of a managed profile").argument("<name>","Managed profile name").action(async(e)=>{try{const t=Ke().find((r)=>r.name===e.trim());if(!t)throw Error(`托管 profile "${e}" 不存在`);const o=await ur(t.entry);console.log(o?`✅ profile "${e}" 已关闭`:`profile "${e}" 本来就没在运行`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});B.command("remove").description("Remove a managed profile (closes Chrome and deletes its login data)").argument("<name>","Managed profile name").option("--keep-data","Keep the user data dir on disk",!1).action(async(e,t)=>{try{await dr(e,{keepData:t.keepData===!0});console.log(`✅ 托管 profile "${e}" 已删除${t.keepData?"(保留数据目录)":"(含登录数据)"}`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("update").description("Reconfigure a managed profile (label / proxy / fingerprint); takes effect on next start").argument("<name>","Managed profile name").option("--clear-proxy","Remove the proxy from this environment").option("--restart","Relaunch the environment after updating",!1).option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=ft(t),{opts:r}=await Dt(o),n={};if(o.label!==void 0)n.label=o.label??null;if(t.clearProxy)n.proxy=null;else if(r.proxy)n.proxy=r.proxy;if(t.fingerprint==="off")n.fingerprint=null;else if(r.fingerprint)n.fingerprint=r.fingerprint;if(o.webrtc!==void 0)n.webrtc=o.webrtc;if(o.clean!==void 0)n.clean=o.clean;const{entry:i,wasRunning:s}=await mr(e,n);if(t.restart&&s)await Ze(e,i);if(t.format==="json"){console.log(JSON.stringify({ok:!0,name:e,label:i.label??"",proxy:gt(i.proxy),fingerprint:ht(i.fingerprint),webrtc:i.webrtc!==!1,clean:i.clean!==!1,wasRunning:s,restarted:!!(t.restart&&s)},null,2));return}console.log(`✅ 托管 profile "${e}" 配置已更新${s?"(已关闭,重启后生效)":""}`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});wt(B.commands[B.commands.length-1]);B.command("test-proxy").description("Test proxy connectivity and detect exit IP / country / timezone").argument("<url>","Proxy: scheme://[user:pass@]host:port (scheme = http/https/socks5)").option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=Ye(e),r=await fr(o);if(t.format==="json"){console.log(JSON.stringify(r,null,2));return}if(r.ok){console.log(`✅ 代理可用 — 出口 IP ${r.ip}`);console.log(` 位置:${r.country??"?"}${r.city?" / "+r.city:""}`);console.log(` 时区:${r.timezone??"?"} 语言:${r.locale??"?"}`)}else{console.log(`❌ 代理不可用:${r.error}`);process.exitCode=f.USAGE_ERROR}}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("prepare-browser").description("Make sure managed profiles have a browser to launch; downloads Chrome for Testing from npmmirror when no Chrome / Edge is installed").option("-f, --format <fmt>","Output format: text, json","text").action(async(e)=>{try{const t=await pr({});if(e.format==="json")console.log(JSON.stringify({ok:!0,executable:t},null,2));else console.log(t)}catch(t){if(e.format==="json")console.log(JSON.stringify({ok:!1,error:J(t)},null,2));else console.error(`Error: ${J(t)}`);process.exitCode=f.GENERIC_ERROR}});B.command("rename").description("Assign a local alias to a connected Browser Bridge profile").argument("<contextId>","Profile contextId from ppcli profile list").argument("<alias>","Local alias, e.g. work or personal").action((e,t)=>{try{ir(e,t);console.log(`Profile ${e} is now aliased as ${t}.`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("use").description("Set the default Browser Bridge profile for future commands").argument("<profile>","Profile alias or contextId").action((e)=>{try{const t=ar(e);console.log(`Default Browser Bridge profile: ${t.defaultContextId??e}`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});const pe=a.command("daemon").description("Manage the ppcli daemon"),so=pe.description();pe.command("status").description("Show daemon status").option("--json","Machine-readable status (includes bridgeReady)").action(async(e)=>{await Yo(e)});pe.command("stop").description("Stop the daemon").action(async()=>{await Qo()});pe.command("restart").description("Restart the daemon").action(async()=>{await Zo()});pe.command("warm").description("Pre-open the background automation window (keeps a tab alive for reuse)").option("--url <url>","http(s) info page to keep in the warm tab (falls back to about:blank)").action(async(e)=>{await er(e.url)});const Ce=qe(),ze=a.command("external").description("Manage external CLI passthrough commands");ze.command("install").description("Install an external CLI").argument("<name>","Name of the external CLI").action((e)=>{const t=Ce.find((o)=>o.name===e);if(!t){console.error(`External CLI '${e}' not found in registry.`);process.exitCode=f.USAGE_ERROR;return}vo(t)});ze.command("register").description("Register an external CLI").argument("<name>","Name of the CLI").option("--binary <bin>","Binary name if different from name").option("--install <cmd>","Auto-install command").option("--desc <text>","Description").action((e,t)=>{Ro(e,{binary:t.binary,install:t.install,description:t.desc})});ze.command("list").description("List registered external CLIs").option("-f, --format <fmt>","Output format: table, json, yaml, md, csv","table").action((e)=>{const t=qe().map((o)=>({name:o.name,package:o.package??"",binary:o.binary,installed:Ve(o.binary),description:o.description??"",homepage:o.homepage??"",tags:o.tags?.join(", ")??""}));_e(t,{fmt:e.format,columns:["name","package","binary","installed","description","homepage","tags"],title:"ppcli/external/list",source:"ppcli external list"})});function ao(e,t){const o=t??(()=>{const r=process.argv.indexOf(e);return process.argv.slice(r+1)})();try{So(e,o,Ce)}catch(r){if(r instanceof Xe){process.stderr.write(ho.dump(To(r),{sortKeys:!1,lineWidth:120,noRefs:!0}));process.exitCode=r.exitCode}else{console.error(`Error: ${J(r)}`);process.exitCode=f.GENERIC_ERROR}}}for(const e of Ce){if(a.commands.some((t)=>t.name()===e.name))continue;a.command(e.name).description(`(External) ${e.description||e.name}`).argument("[args...]").allowUnknownOption().passThroughOptions().helpOption(!1).action((t)=>ao(e.name,t))}const yt=a.command("antigravity").description("antigravity commands");yt.command("serve").description("Start Anthropic-compatible API proxy for Antigravity").option("--port <port>","Server port (default: 8082)","8082").option("--timeout <seconds>","Maximum time to wait for a reply (default: 120s)").action(async(e)=>{const{startServe:t}=await import("../../clis/antigravity/serve.js");await t({port:parseInt(e.port,10),timeout:e.timeout?Yt(e.timeout,"--timeout",120):void 0})});const bt=new Map;bt.set("antigravity",yt);const xt=_o(a,bt);Jr(a);const co=Ce.map((e)=>e.name),lo=Ce.map((e)=>({name:e.name,label:Ot(e)})),We=new Map;for(const[,e]of Ee())if(!We.has(e.site))We.set(e.site,e.domain);const St=[],vt=[];for(const e of xt)if($t(We.get(e))==="app")St.push(e);else vt.push(e);const Rt={external:lo,apps:St,sites:vt},uo=new Set([...co,...xt]);he(h,{globalCommand:a,description:T});he(v,{globalCommand:a,description:"Inspect website login status"});he(pe,{globalCommand:a,description:so});he(se,{globalCommand:a,description:ro});he(Re,{globalCommand:a,description:no});he(B,{globalCommand:a,description:io});a.configureHelp({visibleCommands:(e)=>e.commands.filter((t)=>e!==a||!uo.has(t.name()))});const mo=(e)=>{const t=[];let o=e.parent;while(o){const r=No(o);t.unshift(r?`${o.name()} ${r}`:o.name());o=o.parent}return[...t,e.name(),e.usage()].filter(Boolean).join(" ").trim()};function Ct(e){e.configureHelp({commandUsage:mo});for(const t of e.commands)Ct(t)}Ct(h);$o(a,()=>ko(a,Rt),()=>Oo(Rt));a.on("command:*",(e)=>{const t=e[0];console.error(`error: unknown command '${t}'`);if(Ve(t))console.error(` Tip: '${t}' exists on your PATH. Use 'ppcli external register ${t}' to add it as an external CLI.`);a.outputHelp();process.exitCode=f.USAGE_ERROR});return a}export function runCli(c,l){createProgram(c,l).parse()}export{findPackageRoot};export function resolveBrowserVerifyInvocation(c={}){const l=c.platform??process.platform,a=c.fileExists??A.existsSync,p=c.readFile??((E)=>A.readFileSync(E,"utf-8")),v=c.projectRoot??findPackageRoot(et,a);for(const E of wo(v,p))if(a(E))return{binary:process.execPath,args:[E],cwd:v};const h=k.join(v,"src","main.ts");if(!a(h))throw Error(`Could not find ppcli entrypoint under ${v}. Expected built entry from package.json or src/main.ts.`);const T=k.join(v,"node_modules",".bin",l==="win32"?"tsx.cmd":"tsx");if(a(T))return{binary:T,args:[h],cwd:v,...l==="win32"?{shell:!0}:{}};return{binary:l==="win32"?"npx.cmd":"npx",args:["tsx",h],cwd:v,...l==="win32"?{shell:!0}:{}}}
|
|
140
|
+
✗ Adapter output does not match fixture:`);for(const M of fe.slice(0,20)){const V=M.rowIndex!==void 0?`row[${M.rowIndex}] `:"";console.log(` - [${M.rule}] ${V}${M.detail}`)}if(fe.length>20)console.log(` ... and ${fe.length-20} more failure(s)`);process.exitCode=f.GENERIC_ERROR}catch(o){console.error(`Error: ${o instanceof Error?o.message:String(o)}`);process.exitCode=f.GENERIC_ERROR}});h.command("close").description("Release the current browser session tab lease").action(x(async(e)=>{await e.closeWindow?.();console.log("Browser session tab lease released")}));a.command("doctor").description("Diagnose ppcli browser bridge connectivity").option("-v, --verbose","Debug output").action(async(e)=>{Ir(e);const{runBrowserDoctor:t,renderBrowserDoctorReport:o}=await import("./doctor.js"),r=await t({cliVersion:ke});console.log(o(r))});a.command("completion").description("Output shell completion script").argument("<shell>","Shell type: bash, zsh, or fish").action((e)=>{xo(e)});const se=a.command("plugin").description("Manage ppcli plugins"),ro=se.description();se.command("install").description("Install a plugin from a git repository").argument("<source>","Plugin source (e.g. github:user/repo)").action(async(e)=>{const{installPlugin:t}=await import("./plugin.js"),{discoverPlugins:o}=await import("./discovery.js");try{const r=t(e);await o();if(Array.isArray(r))if(r.length===0)console.log("No plugins were installed (all skipped or incompatible).");else console.log(`✅ Installed ${r.length} plugin(s) from monorepo: ${r.join(", ")}`);else console.log(`✅ Plugin "${r}" installed successfully. Commands are ready to use.`)}catch(r){console.error(`Error: ${J(r)}`);process.exitCode=f.GENERIC_ERROR}});se.command("uninstall").description("Uninstall a plugin").argument("<name>","Plugin name").action(async(e)=>{const{uninstallPlugin:t}=await import("./plugin.js");try{t(e);console.log(`✅ Plugin "${e}" uninstalled.`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.GENERIC_ERROR}});se.command("update").description("Update a plugin (or all plugins) to the latest version").argument("[name]","Plugin name (required unless --all is passed)").option("--all","Update all installed plugins").action(async(e,t)=>{if(!e&&!t.all){console.error("Error: Please specify a plugin name or use the --all flag.");process.exitCode=f.USAGE_ERROR;return}if(e&&t.all){console.error("Error: Cannot specify both a plugin name and --all.");process.exitCode=f.USAGE_ERROR;return}const{updatePlugin:o,updateAllPlugins:r}=await import("./plugin.js"),{discoverPlugins:n}=await import("./discovery.js");if(t.all){const i=r();if(i.length>0)await n();let s=!1;console.log(" Update Results:");for(const d of i){if(d.success){console.log(` ✓ ${d.name}`);continue}s=!0;console.log(` ✗ ${d.name} — ${String(d.error)}`)}if(i.length===0){console.log(" No plugins installed.");return}console.log();if(s){console.error("Completed with some errors.");process.exitCode=f.GENERIC_ERROR}else console.log("✅ All plugins updated successfully.");return}try{o(e);await n();console.log(`✅ Plugin "${e}" updated successfully.`)}catch(i){console.error(`Error: ${J(i)}`);process.exitCode=f.GENERIC_ERROR}});se.command("list").description("List installed plugins").option("-f, --format <fmt>","Output format: table, json","table").action(async(e)=>{const{listPlugins:t}=await import("./plugin.js"),o=t();if(o.length===0){console.log(" No plugins installed.");console.log(" Install one with: ppcli plugin install github:user/repo");return}if(e.format==="json"){_e(o,{fmt:"json",columns:["name","commands","source"],title:"ppcli/plugins",source:"ppcli plugin list"});return}console.log();console.log(" Installed plugins");console.log();const r=o.filter((i)=>!i.monorepoName),n=new Map;for(const i of o){if(!i.monorepoName)continue;const s=n.get(i.monorepoName)??[];s.push(i);n.set(i.monorepoName,s)}for(const i of r){const s=i.version?` @${i.version}`:"",d=i.description?` — ${i.description}`:"",m=i.commands.length>0?` (${i.commands.join(", ")})`:"",u=i.source?` ← ${i.source}`:"";console.log(` ${i.name}${s}${d}${m}${u}`)}for(const[i,s]of n){console.log();console.log(` \uD83D\uDCE6 ${i} (monorepo)`);for(const d of s){const m=d.version?` @${d.version}`:"",u=d.description?` — ${d.description}`:"",g=d.commands.length>0?` (${d.commands.join(", ")})`:"";console.log(` ${d.name}${m}${u}${g}`)}}console.log();console.log(` ${o.length} plugin(s) installed`);console.log()});se.command("create").description("Create a new plugin scaffold").argument("<name>","Plugin name (lowercase, hyphens allowed)").option("-d, --dir <path>","Output directory (default: ./<name>)").option("--description <text>","Plugin description").action(async(e,t)=>{const{createPluginScaffold:o}=await import("./plugin-scaffold.js");try{const r=o(e,{dir:t.dir,description:t.description});console.log(`✅ Plugin scaffold created at ${r.dir}`);console.log();console.log(" Files created:");for(const n of r.files)console.log(` ${n}`);console.log();console.log(" Next steps:");console.log(` cd ${r.dir}`);console.log(` ppcli plugin install file://${r.dir}`);console.log(` ppcli ${e} hello`)}catch(r){console.error(`Error: ${J(r)}`);process.exitCode=f.GENERIC_ERROR}});const Re=a.command("adapter").description("Manage CLI adapters"),no=Re.description();Re.command("status").description("Show which sites have local overrides vs using official baseline").action(async()=>{const e=await import("node:os"),t=k.join(e.homedir(),".ppcli","clis"),o=c;try{const n=(await A.promises.readdir(t,{withFileTypes:!0})).filter((s)=>s.isDirectory()).map((s)=>s.name).sort();let i=[];try{i=(await A.promises.readdir(o,{withFileTypes:!0})).filter((d)=>d.isDirectory()).map((d)=>d.name).sort()}catch{}if(n.length===0){console.log("No local adapter overrides. All sites use the official baseline.");return}console.log(`Local overrides in ~/.ppcli/clis/ (${n.length} sites):
|
|
141
|
+
`);for(const s of n){const m=i.includes(s)?"override":"custom";console.log(` ${s} [${m}]`)}console.log(`
|
|
142
|
+
Official baseline: ${i.length} sites in package`)}catch{console.log("No local adapter overrides. All sites use the official baseline.")}});Re.command("eject").description("Copy an official adapter to ~/.ppcli/clis/ for local editing").argument("<site>","Site name (e.g. twitter, bilibili)").action(async(e)=>{const t=await import("node:os"),o=k.join(t.homedir(),".ppcli","clis"),r=k.join(c,e),n=k.join(o,e);try{await A.promises.access(r)}catch{console.error(`Error: Site "${e}" not found in official adapters.`);process.exitCode=f.USAGE_ERROR;return}try{await A.promises.access(n);console.error(`Site "${e}" already exists in ~/.ppcli/clis/. Use "ppcli adapter reset ${e}" first to restore official version.`);process.exitCode=f.USAGE_ERROR;return}catch{}A.cpSync(r,n,{recursive:!0});console.log(`✅ Ejected "${e}" to ~/.ppcli/clis/${e}/`);console.log("You can now edit the adapter files. Changes take effect immediately.");console.log("Note: Official updates to this adapter will overwrite your changes.")});Re.command("reset").description("Remove local override and restore official adapter version").argument("[site]","Site name (e.g. twitter, bilibili)").option("--all","Reset all local overrides").action(async(e,t)=>{const o=await import("node:os"),r=k.join(o.homedir(),".ppcli","clis");if(t.all){try{const d=(await A.promises.readdir(r,{withFileTypes:!0})).filter((m)=>m.isDirectory());if(d.length===0){console.log("No local sites to reset.");return}for(const m of d)A.rmSync(k.join(r,m.name),{recursive:!0,force:!0});console.log(`✅ Reset ${d.length} site(s). All adapters now use official baseline.`)}catch{console.log("No local sites to reset.")}return}if(!e){console.error("Error: Please specify a site name or use --all.");process.exitCode=f.USAGE_ERROR;return}const n=k.join(r,e);try{await A.promises.access(n)}catch{console.error(`Site "${e}" has no local override.`);return}const i=A.existsSync(k.join(c,e));A.rmSync(n,{recursive:!0,force:!0});console.log(i?`✅ Reset "${e}". Now using official baseline.`:`✅ Removed custom site "${e}".`)});const B=a.command("profile").description("Manage Browser Bridge Chrome profiles"),io=B.description();B.command("list").description("List Chrome profiles: extension-connected and managed (dedicated Chrome instances)").option("-f, --format <fmt>","Output format: text, json","text").action(async(e)=>{const t=await rr(),o=nr(),r=t?.profiles??[],n=!!t&&!hr(t,ke)&&Array.isArray(t.profiles),i=await Promise.all(Ke().map(async({name:m,entry:u})=>({name:m,port:u.port,userDataDir:u.userDataDir,running:await lr(u),isDefault:o.defaultContextId===m,createdAt:u.createdAt??"",label:u.label??"",proxy:u.proxy?{scheme:u.proxy.scheme,host:u.proxy.host,port:u.proxy.port,hasAuth:!!u.proxy.username,username:u.proxy.username??""}:null,fingerprint:u.fingerprint?.enabled?{enabled:!0,timezone:u.fingerprint.timezone??"",locale:u.fingerprint.locale??"",hardwareConcurrency:u.fingerprint.hardwareConcurrency??0,deviceMemory:u.fingerprint.deviceMemory??0,screen:u.fingerprint.screen??null,webglVendor:u.fingerprint.webglVendor??"",webglRenderer:u.fingerprint.webglRenderer??"",canvasNoise:u.fingerprint.canvasNoise===!0}:{enabled:!1},webrtc:u.webrtc!==!1,clean:u.clean!==!1})));if(e.format==="json"){const m=n?r.map((u)=>({contextId:u.contextId,alias:jt(o,u.contextId)??"",connected:!0,version:u.extensionVersion??"",isDefault:o.defaultContextId===u.contextId})):[];console.log(JSON.stringify({managed:i,extension:m,daemonRunning:!!t,daemonStale:!!t&&!n,defaultProfile:o.defaultContextId??""},null,2));return}if(i.length>0){console.log("Managed profiles (dedicated Chrome instances)");console.log();for(const m of i){const u=m.isDefault?" default":"";console.log(` ${m.name}${u} — ${m.running?`running on port ${m.port}`:"stopped"}`)}console.log()}if(!t){console.log("Daemon is not running. Run ppcli doctor after opening Chrome.");return}if(!n){console.log(`Daemon ${gr(t)} is stale for CLI v${ke}.`);console.log("Run: ppcli daemon restart");return}if(r.length===0){console.log("No Browser Bridge profiles connected.");console.log("Open a Chrome profile with the ppcli extension installed, then run ppcli profile list again.");return}const s=new Set(r.map((m)=>m.contextId));console.log("Connected Browser Bridge profiles");console.log();for(const m of r){const u=jt(o,m.contextId),g=o.defaultContextId===m.contextId?" default":"",b=u?` ${u}`:"",R=m.extensionVersion?` v${m.extensionVersion}`:" version unknown";console.log(` ${m.contextId}${b}${g} — connected${R}`)}if(i.some((m)=>m.running)){console.log();console.log("注意:托管 Chrome 也装了同一个扩展,上面的某个 contextId 可能就是它,");console.log(" 无法从这份列表分辨。写操作请显式带 --profile <名字>,不要依赖默认路由。")}const d=Object.entries(o.aliases).filter(([,m])=>!s.has(m));if(d.length>0||o.defaultContextId&&!s.has(o.defaultContextId)){console.log();console.log("Disconnected saved profiles:");const m=new Set;for(const[u,g]of d){m.add(g);console.log(` ${g} ${u} — not connected`)}if(o.defaultContextId&&!m.has(o.defaultContextId)&&!s.has(o.defaultContextId))console.log(` ${o.defaultContextId} — default, not connected`)}});const ft=(e)=>{if(e.config){const r=JSON.parse(e.config);if(typeof r.proxy==="string")r.proxy=Ye(r.proxy);return r}const t={};if(e.label)t.label=e.label;if(e.proxy)t.proxy=Ye(e.proxy);if(e.webrtc===!1)t.webrtc=!1;if(e.clean===!1)t.clean=!1;const o=e.fingerprint;if(o&&o!=="off")t.fingerprint={enabled:!0,spoof:o!=="lite",...e.fpTimezone?{timezone:e.fpTimezone}:{},...e.fpLocale?{locale:e.fpLocale}:{},...e.fpUa?{userAgent:e.fpUa}:{},...e.fpPlatform?{platform:e.fpPlatform}:{},...e.fpSeed?{seed:Number(e.fpSeed)}:{}};return t},gt=(e)=>e?{scheme:e.scheme,host:e.host,port:e.port,hasAuth:!!e.username,username:e.username??""}:null,ht=(e)=>e?.enabled?{enabled:!0,seed:e.seed,timezone:e.timezone??"",locale:e.locale??"",userAgent:e.userAgent??"",platform:e.platform??"",hardwareConcurrency:e.hardwareConcurrency??0,deviceMemory:e.deviceMemory??0,screen:e.screen??null,webglVendor:e.webglVendor??"",webglRenderer:e.webglRenderer??"",canvasNoise:e.canvasNoise===!0}:{enabled:!1},wt=(e)=>e.option("--label <text>","Display label / note for the environment").option("--proxy <url>","Per-env proxy: scheme://[user:pass@]host:port (scheme = http/https/socks5)").option("--fingerprint <mode>","Fingerprint mode: off | on (spoof soft signals) | lite (timezone/locale follow only)").option("--fp-timezone <iana>","Fingerprint timezone, e.g. America/New_York (must be set explicitly)").option("--fp-locale <locale>","Fingerprint locale, e.g. en-US (must be set explicitly)").option("--fp-ua <ua>","Override navigator.userAgent (use with care)").option("--fp-platform <p>","Override navigator.platform, e.g. Win32").option("--fp-seed <n>","Fixed fingerprint seed (for reproducible fingerprint)").option("--no-webrtc","Disable WebRTC leak protection (on by default)").option("--no-clean","Disable lean launch flags / telemetry suppression (on by default)").option("--config <json>","Full config as JSON: { label, proxy, fingerprint } — overrides granular flags");B.command("create").description("Create a managed profile: a dedicated Chrome instance with its own user data dir (for a second account)").argument("<name>","Profile name, e.g. work / brand2").option("--browser-path <path>","Explicit browser executable (defaults to auto-discovered Chrome)").option("--no-start","Create only; do not launch Chrome now").option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=ft(t),{opts:r}=await Dt({...o,browserPath:t.browserPath}),n=await cr(e,{...r,browserPath:t.browserPath});if(t.start!==!1)await Ze(e,n);if(t.format==="json"){console.log(JSON.stringify({ok:!0,name:e,port:n.port,userDataDir:n.userDataDir,label:n.label??"",proxy:gt(n.proxy),fingerprint:ht(n.fingerprint),webrtc:n.webrtc!==!1,clean:n.clean!==!1,started:t.start!==!1},null,2));return}console.log(`✅ 托管 profile "${e}" 已创建(端口 ${n.port})`);console.log(` 数据目录:${n.userDataDir}`);if(n.proxy)console.log(` 代理:${n.proxy.scheme}://${n.proxy.host}:${n.proxy.port}`);if(n.fingerprint?.enabled)console.log(` 指纹:已开启${n.fingerprint.timezone?` 时区 ${n.fingerprint.timezone}`:""}${n.fingerprint.locale?` 语言 ${n.fingerprint.locale}`:""}`);if(t.start!==!1)console.log(` Chrome 已启动。用 ppcli <site> login --profile ${e} 在新窗口里登录账号。`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});wt(B.commands[B.commands.length-1]);B.command("start").description("Launch the dedicated Chrome instance of a managed profile").argument("<name>","Managed profile name").action(async(e)=>{try{const t=Ke().find((r)=>r.name===e.trim());if(!t)throw Error(`托管 profile "${e}" 不存在,先 ppcli profile create ${e}`);const o=await Ze(t.name,t.entry);console.log(`✅ profile "${e}" 运行中(${o})`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});B.command("stop").description("Gracefully close the dedicated Chrome instance of a managed profile").argument("<name>","Managed profile name").action(async(e)=>{try{const t=Ke().find((r)=>r.name===e.trim());if(!t)throw Error(`托管 profile "${e}" 不存在`);const o=await ur(t.entry);console.log(o?`✅ profile "${e}" 已关闭`:`profile "${e}" 本来就没在运行`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});B.command("remove").description("Remove a managed profile (closes Chrome and deletes its login data)").argument("<name>","Managed profile name").option("--keep-data","Keep the user data dir on disk",!1).action(async(e,t)=>{try{await dr(e,{keepData:t.keepData===!0});console.log(`✅ 托管 profile "${e}" 已删除${t.keepData?"(保留数据目录)":"(含登录数据)"}`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("update").description("Reconfigure a managed profile (label / proxy / fingerprint); takes effect on next start").argument("<name>","Managed profile name").option("--clear-proxy","Remove the proxy from this environment").option("--restart","Relaunch the environment after updating",!1).option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=ft(t),{opts:r}=await Dt(o),n={};if(o.label!==void 0)n.label=o.label??null;if(t.clearProxy)n.proxy=null;else if(r.proxy)n.proxy=r.proxy;if(t.fingerprint==="off")n.fingerprint=null;else if(r.fingerprint)n.fingerprint=r.fingerprint;if(o.webrtc!==void 0)n.webrtc=o.webrtc;if(o.clean!==void 0)n.clean=o.clean;const{entry:i,wasRunning:s}=await mr(e,n);if(t.restart&&s)await Ze(e,i);if(t.format==="json"){console.log(JSON.stringify({ok:!0,name:e,label:i.label??"",proxy:gt(i.proxy),fingerprint:ht(i.fingerprint),webrtc:i.webrtc!==!1,clean:i.clean!==!1,wasRunning:s,restarted:!!(t.restart&&s)},null,2));return}console.log(`✅ 托管 profile "${e}" 配置已更新${s?"(已关闭,重启后生效)":""}`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});wt(B.commands[B.commands.length-1]);B.command("test-proxy").description("Test proxy connectivity and detect exit IP / country / timezone").argument("<url>","Proxy: scheme://[user:pass@]host:port (scheme = http/https/socks5)").option("-f, --format <fmt>","Output format: text, json","text").action(async(e,t)=>{try{const o=Ye(e),r=await fr(o);if(t.format==="json"){console.log(JSON.stringify(r,null,2));return}if(r.ok){console.log(`✅ 代理可用 — 出口 IP ${r.ip}`);console.log(` 位置:${r.country??"?"}${r.city?" / "+r.city:""}`);console.log(` 时区:${r.timezone??"?"} 语言:${r.locale??"?"}`)}else{console.log(`❌ 代理不可用:${r.error}`);process.exitCode=f.USAGE_ERROR}}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("prepare-browser").description("Make sure managed profiles have a browser to launch; downloads Chrome for Testing from npmmirror when no Chrome / Edge is installed").option("-f, --format <fmt>","Output format: text, json","text").action(async(e)=>{try{const t=await pr({});if(e.format==="json")console.log(JSON.stringify({ok:!0,executable:t},null,2));else console.log(t)}catch(t){if(e.format==="json")console.log(JSON.stringify({ok:!1,error:J(t)},null,2));else console.error(`Error: ${J(t)}`);process.exitCode=f.GENERIC_ERROR}});B.command("rename").description("Assign a local alias to a connected Browser Bridge profile").argument("<contextId>","Profile contextId from ppcli profile list").argument("<alias>","Local alias, e.g. work or personal").action((e,t)=>{try{ir(e,t);console.log(`Profile ${e} is now aliased as ${t}.`)}catch(o){console.error(`Error: ${J(o)}`);process.exitCode=f.USAGE_ERROR}});B.command("use").description("Set the default Browser Bridge profile for future commands").argument("<profile>","Profile alias or contextId").action((e)=>{try{const t=ar(e);console.log(`Default Browser Bridge profile: ${t.defaultContextId??e}`)}catch(t){console.error(`Error: ${J(t)}`);process.exitCode=f.USAGE_ERROR}});const pe=a.command("daemon").description("Manage the ppcli daemon"),so=pe.description();pe.command("status").description("Show daemon status").option("--json","Machine-readable status (includes bridgeReady)").action(async(e)=>{await Yo(e)});pe.command("stop").description("Stop the daemon").action(async()=>{await Qo()});pe.command("restart").description("Restart the daemon").action(async()=>{await Zo()});pe.command("warm").description("Pre-open the background automation window (keeps a tab alive for reuse)").option("--url <url>","http(s) info page to keep in the warm tab (falls back to about:blank)").action(async(e)=>{await er(e.url)});const Ce=qe(),ze=a.command("external").description("Manage external CLI passthrough commands");ze.command("install").description("Install an external CLI").argument("<name>","Name of the external CLI").action((e)=>{const t=Ce.find((o)=>o.name===e);if(!t){console.error(`External CLI '${e}' not found in registry.`);process.exitCode=f.USAGE_ERROR;return}vo(t)});ze.command("register").description("Register an external CLI").argument("<name>","Name of the CLI").option("--binary <bin>","Binary name if different from name").option("--install <cmd>","Auto-install command").option("--desc <text>","Description").action((e,t)=>{Ro(e,{binary:t.binary,install:t.install,description:t.desc})});ze.command("list").description("List registered external CLIs").option("-f, --format <fmt>","Output format: table, json, yaml, md, csv","table").action((e)=>{const t=qe().map((o)=>({name:o.name,package:o.package??"",binary:o.binary,installed:Ve(o.binary),description:o.description??"",homepage:o.homepage??"",tags:o.tags?.join(", ")??""}));_e(t,{fmt:e.format,columns:["name","package","binary","installed","description","homepage","tags"],title:"ppcli/external/list",source:"ppcli external list"})});function ao(e,t){const o=t??(()=>{const r=process.argv.indexOf(e);return process.argv.slice(r+1)})();try{So(e,o,Ce)}catch(r){if(r instanceof Xe){process.stderr.write(ho.dump(To(r),{sortKeys:!1,lineWidth:120,noRefs:!0}));process.exitCode=r.exitCode}else{console.error(`Error: ${J(r)}`);process.exitCode=f.GENERIC_ERROR}}}for(const e of Ce){if(a.commands.some((t)=>t.name()===e.name))continue;a.command(e.name).description(`(External) ${e.description||e.name}`).argument("[args...]").allowUnknownOption().passThroughOptions().helpOption(!1).action((t)=>ao(e.name,t))}const yt=a.command("antigravity").description("antigravity commands");yt.command("serve").description("Start Anthropic-compatible API proxy for Antigravity").option("--port <port>","Server port (default: 8082)","8082").option("--timeout <seconds>","Maximum time to wait for a reply (default: 120s)").action(async(e)=>{const{startServe:t}=await import("../../clis/antigravity/serve.js");await t({port:parseInt(e.port,10),timeout:e.timeout?Yt(e.timeout,"--timeout",120):void 0})});const bt=new Map;bt.set("antigravity",yt);const xt=_o(a,bt);Jr(a);const co=Ce.map((e)=>e.name),lo=Ce.map((e)=>({name:e.name,label:Ot(e)})),We=new Map;for(const[,e]of Ee())if(!We.has(e.site))We.set(e.site,e.domain);const St=[],vt=[];for(const e of xt)if($t(We.get(e))==="app")St.push(e);else vt.push(e);const Rt={external:lo,apps:St,sites:vt},uo=new Set([...co,...xt]);he(h,{globalCommand:a,description:T});he(v,{globalCommand:a,description:"Inspect website login status"});he(pe,{globalCommand:a,description:so});he(se,{globalCommand:a,description:ro});he(Re,{globalCommand:a,description:no});he(B,{globalCommand:a,description:io});a.configureHelp({visibleCommands:(e)=>e.commands.filter((t)=>e!==a||!uo.has(t.name()))});const mo=(e)=>{const t=[];let o=e.parent;while(o){const r=No(o);t.unshift(r?`${o.name()} ${r}`:o.name());o=o.parent}return[...t,e.name(),e.usage()].filter(Boolean).join(" ").trim()};function Ct(e){e.configureHelp({commandUsage:mo});for(const t of e.commands)Ct(t)}Ct(h);$o(a,()=>ko(a,Rt),()=>Oo(Rt));a.on("command:*",(e)=>{const t=e[0];console.error(`error: unknown command '${t}'`);if(Ve(t))console.error(` Tip: '${t}' exists on your PATH. Use 'ppcli external register ${t}' to add it as an external CLI.`);a.outputHelp();process.exitCode=f.USAGE_ERROR});return a}export function runCli(c,l){createProgram(c,l).parse()}export{findPackageRoot};export function resolveBrowserVerifyInvocation(c={}){const l=c.platform??process.platform,a=c.fileExists??A.existsSync,p=c.readFile??((E)=>A.readFileSync(E,"utf-8")),v=c.projectRoot??findPackageRoot(et,a);for(const E of wo(v,p))if(a(E))return{binary:process.execPath,args:[E],cwd:v};const h=k.join(v,"src","main.ts");if(!a(h))throw Error(`Could not find ppcli entrypoint under ${v}. Expected built entry from package.json or src/main.ts.`);const T=k.join(v,"node_modules",".bin",l==="win32"?"tsx.cmd":"tsx");if(a(T))return{binary:T,args:[h],cwd:v,...l==="win32"?{shell:!0}:{}};return{binary:l==="win32"?"npx.cmd":"npx",args:["tsx",h],cwd:v,...l==="win32"?{shell:!0}:{}}}
|
package/package.json
CHANGED
|
@@ -247,7 +247,7 @@ state, elapsedMs}` on success and a JSON error envelope on timeout/failure.
|
|
|
247
247
|
|
|
248
248
|
- **`web read --url <url>`** — One-shot Markdown reader for arbitrary pages. It expands relevant same-origin iframes by default, so old iframe-shell sites work better than with a top-document-only scrape. Use `--frames all-same-origin` when completeness matters more than Markdown noise. For AJAX shell pages use `ppcli web read --url <url> --wait-for "<selector>" --wait-until networkidle --diagnose`; diagnostics show frame URLs, empty containers, and API-like XHRs. If the value you need is table/API data, switch to `browser network` or a dedicated adapter instead of relying on Markdown.
|
|
249
249
|
- **`browser eval <js> [--frame N]`** — Run an expression in the page (or in a cross-origin frame via `--frame`). Wrap in an IIFE and return JSON. Read-only: no `document.forms[0].submit()`, no clicks, no navigations. If the result is a string, stdout is the raw string; otherwise it's JSON.
|
|
250
|
-
- **`browser extract [--selector <css>] [--chunk-size N] [--start N]`** — Markdown extraction of long-form content with a continuation cursor. Returns `{url, title, selector, total_chars, chunk_size, start, end, next_start_char, content}`. Loop on `next_start_char` until it is `null`. Auto-scopes to `<main>`/`<article>`/`<body>` if you don't pass `--selector`.
|
|
250
|
+
- **`browser extract [--selector <css>] [--chunk-size N] [--start N]`** — Markdown extraction of long-form content with a continuation cursor. Returns `{url, title, selector, total_chars, chunk_size, start, end, next_start_char, content}`. Loop on `next_start_char` until it is `null`. Auto-scopes to `<main>`/`<article>`/`<body>` if you don't pass `--selector`. To check a few facts on a long page (balance, login name, coupon count), pass `--grep "余额|优惠券"`: `content` holds only the matching lines and the envelope adds `matched_lines`, `total_lines`, `page_chars`.
|
|
251
251
|
|
|
252
252
|
### Network
|
|
253
253
|
|
|
@@ -349,6 +349,7 @@ Think about payload size per call. Budgets exist for a reason.
|
|
|
349
349
|
| `get html --as json --depth 3 --children-max 20` | medium | When you need to reason about structure, not a specific field. |
|
|
350
350
|
| `screenshot` | large | Only when the page is visual (CAPTCHA, charts). Prefer `state`. |
|
|
351
351
|
| `extract` | medium per chunk | Long-form reading. Loop via `next_start_char`. |
|
|
352
|
+
| `extract --grep <regex>` | small | A few facts from a long page; only matching lines. |
|
|
352
353
|
| `network` (default) | small | First look at APIs. |
|
|
353
354
|
| `network --detail <key>` | varies | Pull one body. |
|
|
354
355
|
| `network --raw` | huge | Only after `--filter` narrowed the candidate set. |
|