import{spawn as $}from"node:child_process";import{existsSync as u}from"node:fs";import{homedir as w}from"node:os";import f from"node:path";const r="[qqbrowser-skill-control]";function d(o){const l=w(),s=process.platform==="win32"?f.join(l,".qbotclaw","python","Scripts","qqbrowser-skill.exe"):f.join(l,".qbotclaw","python","bin","qqbrowser-skill");if(o.logger.info(`${r} \u89E3\u6790\u53EF\u6267\u884C\u6587\u4EF6\u8DEF\u5F84: ${s} (platform=${process.platform})`),!u(s))throw new Error(`qqbrowser-skill \u53EF\u6267\u884C\u6587\u4EF6\u4E0D\u5B58\u5728: ${s}\uFF0C\u8BF7\u786E\u8BA4\u662F\u5426\u5DF2\u6B63\u786E\u5B89\u88C5`);return o.logger.info(`${r} \u2713 \u627E\u5230\u53EF\u6267\u884C\u6587\u4EF6: ${s}`),s}function q(){const o=process.platform==="win32",l=f.join(w(),".qbotclaw","python"),p=o?f.join(l,"Scripts"):f.join(l,"bin"),s=process.env.PATH??"",e=o?";":":",n=[p];o&&(n.push(f.join(l,"Lib")),n.push(f.join(l,"Lib","site-packages")));const t=s.split(e),a=n.filter(g=>!t.includes(g));return a.length===0?s:`${a.join(e)}${e}${s}`}const b={id:"qqbrowser-skill-control",name:"QQBrowser Skill Control",description:"\u7BA1\u7406 qqbrowser-skill \u5B88\u62A4\u8FDB\u7A0B\u7684\u751F\u547D\u5468\u671F\uFF0C\u81EA\u52A8\u542F\u52A8\u548C\u5173\u95ED\u6D4F\u89C8\u5668\u81EA\u52A8\u5316\u670D\u52A1\u3002",register(o){const l=o.pluginConfig??{},p=l.enabled!==!1,s=l.wsPort??8765;if(o.logger.info(`${r} \u63D2\u4EF6\u6CE8\u518C\u4E2D\uFF0C\u914D\u7F6E: ${JSON.stringify({enabled:p,wsPort:s})}`),!p){o.logger.info(`${r} \u63D2\u4EF6\u5DF2\u7981\u7528\uFF0C\u8DF3\u8FC7\u542F\u52A8`);return}let e=null;o.registerService({id:"qqbrowser-skill-control",start:async()=>{o.logger.info(`${r} \u6B63\u5728\u542F\u52A8 qqbrowser-skill \u5B88\u62A4\u8FDB\u7A0B...`);try{const n=d(o),t=["serve","--daemon","--from-qbotclaw",`--ws-port=${s}`],a=q();o.logger.info(`${r} \u6267\u884C\u547D\u4EE4: ${n} ${t.join(" ")}`),o.logger.info(`${r} \u589E\u5F3A\u540E\u7684 PATH: ${a}`);const g={...process.env,PATH:a};process.platform==="win32"&&(g.PYTHONPATH=f.join(w(),".qbotclaw","python","Lib","site-packages")),e=$(n,t,{env:g,windowsHide:!0,stdio:["pipe","pipe","pipe"],detached:!1}),e.stdout?.on("data",i=>{o.logger.info(`${r} ${i.toString().trim()}`)}),e.stderr?.on("data",i=>{o.logger.error(`${r} ${i.toString().trim()}`)}),e.on("exit",(i,c)=>{i!==0&&i!==null?o.logger.error(`${r} \u8FDB\u7A0B\u5F02\u5E38\u9000\u51FA code=${i} signal=${c}`):o.logger.info(`${r} \u8FDB\u7A0B\u9000\u51FA code=${i} signal=${c}`),e=null}),e.on("error",i=>{o.logger.error(`${r} \u8FDB\u7A0B\u542F\u52A8\u5931\u8D25: ${i.message}`),e=null}),o.logger.info(`${r} qqbrowser-skill \u5B88\u62A4\u8FDB\u7A0B\u5DF2\u542F\u52A8 (pid=${e.pid}, ws_port=${s})`)}catch(n){const t=n;o.logger.error(`${r} \u542F\u52A8 qqbrowser-skill \u5931\u8D25: ${t.message}`),o.logger.error(`${r} \u9519\u8BEF\u5806\u6808: ${t.stack}`),e=null}},stop:async()=>{o.logger.info(`${r} \u6B63\u5728\u5173\u95ED qqbrowser-skill \u5B88\u62A4\u8FDB\u7A0B...`);try{const n=d(o);o.logger.info(`${r} \u6267\u884C\u505C\u6B62\u547D\u4EE4: ${n} stop`),await new Promise(t=>{const a={...process.env,PATH:q()};process.platform==="win32"&&(a.PYTHONPATH=f.join(w(),".qbotclaw","python","Lib","site-packages"));const g=$(n,["stop","--force"],{env:a,stdio:["pipe","pipe","pipe"]}),i=setTimeout(()=>{o.logger.warn(`${r} qqbrowser-skill stop \u547D\u4EE4\u6267\u884C\u8D85\u65F6(10s)`),g.kill("SIGKILL"),t()},1e4);g.stdout?.on("data",c=>{o.logger.info(`${r} [stop] ${c.toString().trim()}`)}),g.stderr?.on("data",c=>{o.logger.error(`${r} [stop] ${c.toString().trim()}`)}),g.on("exit",c=>{clearTimeout(i),o.logger.info(`${r} qqbrowser-skill stop \u547D\u4EE4\u6267\u884C\u5B8C\u6BD5 (code=${c})`),t()}),g.on("error",c=>{clearTimeout(i),o.logger.error(`${r} qqbrowser-skill stop \u547D\u4EE4\u6267\u884C\u5931\u8D25: ${c.message}`),t()})})}catch(n){const t=n;o.logger.error(`${r} \u6267\u884C qqbrowser-skill stop \u5931\u8D25: ${t.message}`)}e=null,o.logger.info(`${r} qqbrowser-skill \u5B88\u62A4\u8FDB\u7A0B\u5DF2\u5173\u95ED`)}})}};var y=b;export{y as default};
