@tbox.cn/tboxcode-dsh-cli 0.1.0-rc.14 → 0.1.0-rc.16

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.
@@ -0,0 +1 @@
1
+ import"./identity-DoyHEplh.js";import{_ as e,a as t,c as n,d as r,f as i,g as a,h as o,i as s,l as c,m as l,n as u,o as d,p as f,r as p,s as m,t as h,u as g,v as _}from"./assembly-OSsJmFyv.js";export{h as OWN_PROFILES,u as PROFILE_CHAINS,p as PROFILE_CLEANUP_ROWS,s as PROFILE_OFFICIAL_ROWS,t as assemblyAddArgs,d as assemblyHint,m as checkAssembly,n as cliManifestVersion,c as dshLockVersion,g as ensureAssembly,r as healCorruptState,i as installTimeoutMs,f as planAssemblyInstalls,l as profileDir,o as resolveDshHome,a as resolveInstallSpec,e as selectOwnExpectedVersion,_ as versionDriftHint};
@@ -0,0 +1,3 @@
1
+ import{n as e,r as t}from"./identity-DoyHEplh.js";import{createRequire as n}from"node:module";import{existsSync as r,mkdirSync as i,readFileSync as a,readdirSync as o,rmSync as s,statSync as c}from"node:fs";import{dirname as l,join as u}from"node:path";import{homedir as d}from"node:os";import{parse as f,stringify as p}from"yaml";import{withFileLock as m,writeFileAtomic as h}from"@deepseek-ai/dsh-atomic-write";import{spawn as g}from"node:child_process";const _=n(import.meta.url);function v(){let e=_.resolve(`@deepseek-ai/dsh/package.json`),t=JSON.parse(a(e,`utf-8`)).bin?.dsh;if(t===void 0)throw Error(`@deepseek-ai/dsh 包缺 dsh bin 声明`);return u(l(e),t)}function y(e,t){return`tboxcode-dsh ${e}: dsh 非零退出(${t??`signal`})——若上方出现 ERR_MODULE_NOT_FOUND 或插件未激活,多为 profile 半装残缺,运行 tboxcode-dsh init 修复`}function b(e,t=16384){return Buffer.byteLength(e,`utf-8`)<=t?e:`…(前段截断,仅保留尾部 ${t} 字节)\n${e.slice(-t)}`}function x(e){if(process.platform===`win32`){g(`taskkill`,[`/PID`,String(e),`/T`,`/F`],{stdio:`ignore`}).on(`error`,()=>{});return}try{process.kill(-e,`SIGKILL`)}catch(e){if(e.code!==`ESRCH`)throw e}}function S(n){let r=v(),i={...process.env,DSH_HOME:n.dshHome,DSH_TELEMETRY_DISABLED:`1`};return new Promise((a,o)=>{let s=g(process.execPath,t(e.cli,[r,...n.args]),{env:i,stdio:n.capture===!1?`inherit`:[`ignore`,`pipe`,`pipe`],...n.timeoutMs===void 0?{}:{detached:!0}}),c=``,l=``;s.stdout?.on(`data`,e=>{c+=e.toString()}),s.stderr?.on(`data`,e=>{l+=e.toString()});let u;s.on(`error`,e=>{clearTimeout(u),o(e)}),s.on(`close`,e=>{clearTimeout(u),a({code:e??-1,stdout:c,stderr:l})}),n.timeoutMs!==void 0&&(u=setTimeout(()=>{s.pid!==void 0&&x(s.pid),o(Error(`dsh 进程超时(>${n.timeoutMs}ms),进程组已终止\nSTDOUT:\n${b(c)}\nSTDERR:\n${b(l)}`))},n.timeoutMs))})}const C=[`tboxcode-acp`,`tboxcode-server`,`tboxcode-web`,`tboxcode-headless`],w={"tboxcode-acp":[`@deepseek-ai/dsh-cmdline`,`@deepseek-ai/dsh-tool-ask-user`,`@tbox.cn/dsh-acp-full-app`,`@tbox.cn/dsh-acp-full`,`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-presets`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-server":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-web":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-headless":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-presets`,`@tbox.cn/tboxcode-dsh-bundle`]},T={"tboxcode-acp":[`@deepseek-ai/dsh-base`],"tboxcode-server":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`],"tboxcode-web":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`],"tboxcode-headless":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-headless`]},E=new Set([`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`,`@deepseek-ai/dsh-headless`]),D={"tboxcode-acp":[`@tbox.cn/dsh-acp-full`],"tboxcode-server":[`@tbox.cn/tboxcode-dsh-presets`],"tboxcode-web":[`@tbox.cn/tboxcode-dsh-presets`],"tboxcode-headless":[]};function O(e){let t=T[e],n=new Set([...D[e],...[...E].filter(e=>!t.includes(e))]),r=w[e].filter(e=>N(e)&&!n.has(e));return{officialRows:t,managedOwnRows:r,managedRows:new Set([...t,...r]),cleanupRows:n}}function k(e,t){return e.managedRows.has(t)||e.cleanupRows.has(t)}function A(e){let t=O(e);return[...t.officialRows,...t.managedOwnRows]}function j(){return process.env.TBOXCODE_DSH_HOME??process.env.DSH_HOME??u(d(),`.dsh`)}function M(e,t){return u(e,`profiles`,t)}function N(e){return e.startsWith(`@tbox.cn/`)}function P(e){let t=u(e,`package.json`);return r(t)?JSON.parse(a(t,`utf-8`)):null}function F(e,t){let n=t.startsWith(`./`)?t.slice(2):t;if(r(u(e,n)))return!0;for(let t of[`.js`,`.json`,`/index.js`,`/index.json`])if(r(u(e,n+t)))return!0;return!1}function I(e){let t=u(e,`node_modules`);if(!r(t))return[];let n=[],i=(e,t)=>{for(let r of o(e,{withFileTypes:!0})){if(r.name.startsWith(`.`)||!r.isDirectory()&&!r.isSymbolicLink())continue;if(r.isDirectory()&&r.name.startsWith(`@`)){i(u(e,r.name),`${t}${r.name}/`);continue}let o=`${t}${r.name}`,s;try{s=JSON.parse(a(u(e,r.name,`package.json`),`utf-8`))}catch{n.push(o);continue}if(typeof s!=`object`||!s)continue;let c=null;if(typeof s.main==`string`&&s.main.length>0)c=s.main;else if(s.exports!==null&&typeof s.exports==`object`){let e=s.exports[`.`],t=typeof e==`string`?e:typeof e==`object`&&e?e.default??e.import??e.require:void 0;typeof t==`string`&&t.length>0&&(c=t)}c!==null&&(F(u(e,r.name),c)||n.push(o))}};return i(t,``),n.sort()}function L(e,t){let n=M(e,t),i=w[t],a=O(t),o=A(t),s=r(n)?P(n):null;if(s===null)return{status:`missing`,profileDir:n,missingPackages:[...i],managedCorruptPackages:[],foreignCorruptPackages:[],forbiddenPackages:[],expectedStructureOrder:o,actualStructureOrder:[],versionMismatches:[]};let c=I(n),l=new Set(i),u=c.filter(e=>l.has(e)),d=c.filter(e=>!l.has(e)),f=s.dsh?.profile?.bundles??[],p=Object.keys(s.dependencies??{}).filter(e=>E.has(e)),m=i.filter(e=>K(n,e)===null),h=f.filter(e=>k(a,e)),g=[],_=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT===void 0?`registry`:`local`,v=_===`registry`?z():``;for(let e of i.filter(N)){let t=G(_,_===`local`?H(e):null,v),r=K(n,e);t!==null&&r!==null&&t!==r&&g.push({pkg:e,expected:t,actual:r})}for(let e of i.filter(e=>!N(e))){let t=R(),r=K(n,e);t!==``&&r!==null&&t!==r&&g.push({pkg:e,expected:t,actual:r})}let y=`ok`;return m.length>0||u.length>0||p.length>0?y=`incomplete`:g.length>0?y=`version-drift`:(h.length!==o.length||h.some((e,t)=>e!==o[t]))&&(y=`drift`),{status:y,profileDir:n,missingPackages:m,managedCorruptPackages:u,foreignCorruptPackages:d,forbiddenPackages:p,expectedStructureOrder:o,actualStructureOrder:h,versionMismatches:g}}function R(){let e=n(import.meta.url).resolve(`@deepseek-ai/dsh/package.json`);return JSON.parse(a(e,`utf-8`)).version??``}function z(){let e=n(import.meta.url).resolve(`../package.json`);return JSON.parse(a(e,`utf-8`)).version??``}function B(e){if(!N(e))return`${e}@${R()}`;let t=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT;if(t===void 0){let t=z();if(t===`0.0.0`)throw Error(`占位版本 0.0.0 不能用于 registry 装配——发布形态先 pnpm release bump;开发 / CI 形态设 TBOXCODE_ASSEMBLY_LOCAL_ROOT 走本地 link: 源`);return`${e}@${t}`}let n=V(t,e);if(n===null)throw Error(`装配安装源未找到包 ${e}(TBOXCODE_ASSEMBLY_LOCAL_ROOT=${t})`);return`link:${n}`}function V(e,t){for(let n of o(e)){let i=u(e,n);if(c(i).isDirectory())for(let e of o(i)){let n=u(i,e),o=u(n,`package.json`);if(!(!c(n).isDirectory()||!r(o))&&JSON.parse(a(o,`utf-8`)).name===t)return n}}return null}function H(e){let t=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT;if(t===void 0)return null;let n=V(t,e);return n===null?null:JSON.parse(a(u(n,`package.json`),`utf-8`)).version??null}function U(e,t){let n=new Map;for(let t of e.missingPackages){let e=B(t);n.set(t,{pkg:t,spec:e,kind:`install`,detail:`${t} ← ${e}`})}if(t===`explicit`)for(let t of e.versionMismatches){if(n.has(t.pkg))continue;let e=B(t.pkg);n.set(t.pkg,{pkg:t.pkg,spec:e,kind:`reinstall`,detail:`${t.pkg} ${t.actual} → ${t.expected}`})}let r=[...n.values()];return{specs:r.map(e=>e.spec),entries:r}}function W(e,t){return[`plugin`,`--profile`,e,`add`,...t.specs]}function G(e,t,n){return e===`local`?t===``?null:t:n===``||n===`0.0.0`?null:n}function K(e,t){let n=u(e,`node_modules`,t,`package.json`);if(!r(n))return null;try{return JSON.parse(a(n,`utf-8`)).version??null}catch{return null}}const q={"node-pty":!0,koffi:!0,"@deepseek-ai/dsh-subprocess-local":!0,"@google/genai":!1,protobufjs:!1};async function J(e){let t=u(e,`pnpm-workspace.yaml`);await m(t,async()=>{if(!r(t)){await h(t,p({packages:[`.`],nodeLinker:`hoisted`,autoInstallPeers:!1,allowBuilds:q}),{mode:420});return}let e=f(a(t,`utf-8`))??{},n=e.allowBuilds??{},i={...q,...n};JSON.stringify(n)!==JSON.stringify(i)&&(e.allowBuilds=i,await h(t,p(e),{mode:420}))})}const Y=5e3;function X(e=1){let t=Number(process.env.TBOXCODE_ASSEMBLY_INSTALL_TIMEOUT_MS);return Number.isFinite(t)&&t>0?t:12e4*Math.min(Math.max(e,1),3)}function Z(){let e=Number(process.env.TBOXCODE_ASSEMBLY_INSTALL_ATTEMPTS);return Number.isInteger(e)&&e>0?e:2}const ee=/^dsh: installation rejected: Plugin (\S+) is incompatible with dsh (\S+)/gm;function te(e,t,n){let r=[`装配安装失败(${t.entries.length} 包单事务):dsh plugin add 退出码 ${n.code}`,`STDOUT:\n${b(n.stdout)}`,`STDERR:\n${b(n.stderr)}`],i=[...n.stderr.matchAll(ee)],a=i[0]?.[2];return a!==void 0&&r.push(`诊断:以下已装插件被本次事务触碰且与 dsh ${a} 不兼容,dsh 已整单回滚:`,...i.map(e=>` - ${e[1]}`),`处置:升级或移除上述插件;确需保留时按 dsh 提示授权:`,` dsh plugin --profile ${e} allow-version <pkg>@<ver> --dsh-version ${a} --accept-risk`),r.join(`
2
+ `)}async function ne(e,t,n){if(n.specs.length===0)return;let r=await S({dshHome:e,args:W(t,n),capture:!0,timeoutMs:X(n.specs.length)});if(r.code!==0)throw Error(te(t,n,r))}async function re(e,t,n){let r=await S({dshHome:e,args:[`plugin`,`--profile`,t,`remove`,...n],capture:!0,timeoutMs:X()});if(r.code!==0)throw Error(`官方元包去污染失败(dsh plugin remove ${n.join(` `)}):退出码 ${r.code}\nSTDOUT:\n${b(r.stdout)}\nSTDERR:\n${b(r.stderr)}`)}async function Q(e,t,n={}){let i=M(e,t),a=u(i,`node_modules`),o=u(e,`profiles`,t,`node_modules`);if(!C.includes(t)||a!==o)throw Error(`装配自愈守卫拒绝:目标 ${a} 非 own profile node_modules(期望 ${o})`);return r(a)?await m(u(i,`.tboxcode-assembly-heal.lock`),async()=>{let e=I(i),r=new Set(w[t]);return!e.some(e=>r.has(e))&&n.includeForeign!==!0?!1:(s(a,{recursive:!0,force:!0}),!0)}):!1}async function ie(e,t,n,r={}){let i=n===`explicit`?Z():1,a=[],o;for(let s=1;s<=i;s++)try{return await ae(e,t,n,a,r)}catch(e){if(o=e,s<i){let t=String(e instanceof Error?e.message:e).split(`
3
+ `)[0];a.push({kind:`retry`,detail:`第 ${s} 次尝试失败(${t}),${Y/1e3}s 后重试`}),await new Promise(e=>setTimeout(e,Y))}}throw o}async function ae(e,t,n,r,a){let o=L(e,t);o.forbiddenPackages.length>0&&(await re(e,t,o.forbiddenPackages),r.push({kind:`decontaminate`,detail:`官方元包去污染:dsh plugin remove ${o.forbiddenPackages.join(` `)}(元包实体破坏 dsh 单实例——settings 持久化写与热重载全灭;官方基座行转 installation 解析)`}),o=L(e,t));let s=a.repairForeignCorruption===!0&&o.foreignCorruptPackages.length>0;if(o.managedCorruptPackages.length>0||s){let n=[...new Set([...o.managedCorruptPackages,...o.foreignCorruptPackages])];await Q(e,t,{includeForeign:s})&&r.push({kind:`heal`,detail:`半装态自愈${s?`(含 foreign;用户自装插件需自行重装)`:``}:node_modules 残缺 ${n.length} 包(${n.slice(0,3).join(`, `)}${n.length>3?` …`:``})→ nuke 后全链重装`}),o=L(e,t)}else o.foreignCorruptPackages.length>0&&r.push({kind:`warn-foreign-corrupt`,detail:`非受管包残缺(用户插件或传递依赖):${o.foreignCorruptPackages.slice(0,3).join(`, `)}${o.foreignCorruptPackages.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --profile ${t} --repair-all(会重建 node_modules,用户自装插件需自行重装)`});let c=U(o,n);if(c.entries.length>0){i(o.profileDir,{recursive:!0}),await J(o.profileDir),await ne(e,t,c);for(let e of c.entries)r.push({kind:e.kind,detail:e.detail});o=L(e,t)}return o.status!==`missing`&&(o.actualStructureOrder.length!==o.expectedStructureOrder.length||o.actualStructureOrder.some((e,t)=>e!==o.expectedStructureOrder[t]))&&(await se(e,t),r.push({kind:`correct-manifest`,detail:`结构行集校正 [${o.actualStructureOrder.join(`,`)}] → [${o.expectedStructureOrder.join(`,`)}](结构行是组合契约,用户领地不动)`}),o=L(e,t)),{status:o.status,actions:r,check:o}}function $(e,t){return e.status===`version-drift`?`${t} 版本偏差(${e.check.versionMismatches.map(e=>`${e.pkg} ${e.actual}→${e.expected}`).join(`, `)})——运行 tboxcode-dsh init --profile ${t} 升级`:null}function oe(e,t,n={}){let r=[];if(e.check.foreignCorruptPackages.length>0){let n=e.check.foreignCorruptPackages;r.push(`${t} 非受管包残缺(用户插件或传递依赖):${n.slice(0,3).join(`, `)}${n.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --profile ${t} --repair-all(会重建 node_modules,用户自装插件需自行重装)`)}if(n.includeVersionDrift!==!1){let n=$(e,t);n!==null&&r.push(n)}return e.check.forbiddenPackages.length>0&&r.push(`${t} 官方元包污染(${e.check.forbiddenPackages.join(`, `)})——运行 tboxcode-dsh init --profile ${t} 去污染`),r}async function se(e,t){let n=u(M(e,t),`package.json`);await m(n,async()=>{let e=JSON.parse(a(n,`utf-8`)),r=e.dsh?.profile?.bundles??[],i=O(t),o=[...new Set(r.filter(e=>!k(i,e)))],s=[...i.officialRows,...o,...i.managedOwnRows];e.dsh={...e.dsh,profile:{...e.dsh?.profile,bundles:s}},await h(n,`${JSON.stringify(e,null,2)}\n`,{mode:420})})}export{G as _,W as a,v as b,z as c,Q as d,X as f,B as g,j as h,T as i,R as l,M as m,w as n,oe as o,U as p,D as r,L as s,C as t,ie as u,$ as v,S as x,y};
package/lib/bin.js CHANGED
@@ -1,46 +1,6 @@
1
1
  #!/usr/bin/env node
2
- import{_ as e,a as t,c as n,d as r,l as i,m as a,o,p as s,s as c,t as l,u,v as d}from"./assembly-CxMZLSKV.js";import{createRequire as f}from"node:module";import{spawn as p}from"node:child_process";import{existsSync as m,mkdirSync as h,readFileSync as g,statSync as _,writeFileSync as v}from"node:fs";import{dirname as y,join as b}from"node:path";import{loadConfig as x,resolveConfigDir as S,translateConfig as C,writeOverlayPatch as w}from"@tbox.cn/tboxcode-dsh-runtime";import{createInterface as T}from"node:readline";import{fileURLToPath as E}from"node:url";function D(e){for(let t of e)console.error(`tboxcode: ${t.field}: ${t.reason}`)}function O(e,t,n){let r=[e,`--profile`,`tboxcode-web`,`--patch`,t];n.port!==void 0&&r.push(`--port`,String(n.port)),n.host!==void 0&&r.push(`--host`,n.host),n.noOpen===!0&&r.push(`--no-open`);for(let e of n.trustedHost??[])r.push(`--trusted-host`,e);return r}async function k(n){let o=r(),c=S(),l=x(c),u=C(l.config,c,l.path!==null,{profile:`web`});D(u.warnings);let f=await w(u.rows),m=await i(o,`tboxcode-web`,`startup`);if(m.status!==`ok`&&m.status!==`version-drift`)throw await f.dispose(),Error(`web 启动装配未就绪(${m.status})——运行 tboxcode-dsh init 显式装配`);for(let e of t(m,`tboxcode-web`))console.error(`tboxcode-dsh web: ${e}`);let h=O(a(),f.path,{...n,dshHome:o}),g=p(process.execPath,d(e.web,h),{stdio:`inherit`,env:{...process.env,DSH_HOME:o,DSH_TELEMETRY_DISABLED:`1`}});for(let e of[`SIGINT`,`SIGTERM`])process.on(e,()=>{g.kill(e)});let _=await new Promise(e=>{g.on(`close`,e),g.on(`error`,t=>{console.error(`tboxcode-dsh web: spawn 失败(${t.message})`),e(1)})});return _!==0&&console.error(s(`web`,_)),await f.dispose(),typeof _==`number`?_:1}const A=f(import.meta.url);function j(){return b(y(A.resolve(`@tbox.cn/tboxcode-dsh-runtime/package.json`)),`assets`,`stdout-guard.cjs`)}function M(e,t,n){let r=j(),i=process.env.NODE_OPTIONS??``,a=`--require ${r}`;return{NODE_OPTIONS:i.includes(r)?i:i===``?a:`${i} ${a}`,TBOXCODE_STDIO_GUARD:n,TBOXCODE_PROFILE_DIR:u(e,t)}}function N(e,t,n){let r=[e,`--profile`,`tboxcode-headless`,`--patch`,t];return n.sessionId!==void 0&&r.push(`--session-id`,n.sessionId),n.json===!0&&r.push(`--json`),r.push(...n.task),r}async function P(n){let o=r(),c=S(),l=x(c),u=C(l.config,c,l.path!==null,{profile:`headless`});D(u.warnings);let f=await w(u.rows),m=await i(o,`tboxcode-headless`,`startup`);if(m.status!==`ok`&&m.status!==`version-drift`)throw await f.dispose(),Error(`headless 启动装配未就绪(${m.status})——运行 tboxcode-dsh init 显式装配`);for(let e of t(m,`tboxcode-headless`))console.error(`tboxcode-dsh headless: ${e}`);let h=N(a(),f.path,n),g=p(process.execPath,d(e.headless,h),{stdio:`inherit`,env:{...process.env,...M(o,`tboxcode-headless`,`quiet`),DSH_HOME:o,DSH_TELEMETRY_DISABLED:`1`}}),_=await new Promise(e=>{g.on(`close`,e),g.on(`error`,t=>{console.error(`tboxcode-dsh headless: spawn 失败(${t.message})`),e(1)})});return _!==0&&console.error(s(`headless`,_)),await f.dispose(),typeof _==`number`?_:1}function F(){return JSON.stringify({v:1,event:`standby`})}function I(e,t){return JSON.stringify({v:1,event:`ready`,port:e,token:t})}function L(e,t,n){return JSON.stringify({stage:e,kind:t,error:n})}function R(e){let t;try{t=JSON.parse(e)}catch{return{kind:`malformed`}}if(typeof t!=`object`||!t||Array.isArray(t))return{kind:`malformed`};let n=t;return n.v===1?n.cmd===`start`?{kind:`start`}:{kind:`unknown`,cmd:String(n.cmd??``)}:{kind:`version-skew`,got:typeof n.v==`number`?n.v:void 0}}function z(e){if(!e.includes(`token=`))return null;let t=/https?:\/\/\S+/.exec(e);if(t===null)return null;try{let e=new URL(t[0]);return e.searchParams.get(`token`)===null?null:e}catch{return null}}function B(e){let t=e.searchParams.get(`token`),n=e.port===``?void 0:Number(e.port);return t===null||t===``||n===void 0||!Number.isInteger(n)?null:{port:n,token:t}}var V=class extends Error{};function H(e){if(e instanceof V)return`permanent`;let t=e?.name;return t===`ModelNotSetError`||t===`ModelMalformedError`||t===`SecretInTranslationError`?`permanent`:`transient`}function U(e){return e===null?1:e===0?0:e===2||e===3?e:1}async function W(e){let{io:t}=e;return await new Promise(n=>{let r=`standby`,i=!1,a=!1,o=!1,s=!1,c,l=!1,u=(e,r,i,a)=>{o||(o=!0,r!==void 0&&i!==void 0&&a!==void 0&&t.writeStderr(`${L(r,i,a)}\n`),n(e))};t.writeStdout(`${F()}\n`),e.log(`standby(零用户态自检通过,等待 start)`),t.onSignal(t=>{if(s=!0,e.log(`收到 ${t},正常终(dsh 子进程随停)`),c===void 0){u(0);return}c.kill(t);let n=setTimeout(()=>u(0),3e3);c.onExit(()=>{clearTimeout(n),u(0)})}),t.onStdinEof(()=>{if(!a){if(a=!0,i=!0,r!==`ready`){e.log(`stdin EOF 在 ready 前到达(父进程死亡)——退出`),c?.kill(),u(1);return}e.log(`stdin EOF 在 ready 后到达——孤儿语义,继续运行`)}}),t.onStdinLine(e=>{if(e.trim()===``)return;if(r===`ready`){t.writeStderr(`${L(`protocol`,`protocol`,`start 命令在 ready 后到达(stdin 已转惰性),忽略`)}\n`);return}let n=R(e);switch(n.kind){case`version-skew`:u(3,`protocol`,`protocol`,`信封版本偏斜:期待 v1,实得 ${n.got??`缺省`}——fail-loud(部署错误最显眼信号)`);return;case`start`:if(r!==`standby`)return;r=`starting`,d();return;case`unknown`:t.writeStderr(`${L(`protocol`,`protocol`,`未知 cmd "${n.cmd}" 忽略(additive-only 版本窗口)`)}\n`);return;case`malformed`:t.writeStderr(`${L(`protocol`,`protocol`,`stdin 行非 JSON 对象,忽略`)}\n`);return}});let d=async()=>{try{let n=await e.boot();if(c=n,o){n.kill();return}n.onStdoutLine(n=>{if(l)return;let i=z(n);if(i===null)return;let a=B(i);a!==null&&(l=!0,r=`ready`,t.writeStdout(`${I(a.port,a.token)}\n`),e.log(`ready(token 已交付,stdout 永久静默;token 已弃)`))}),n.onExit(t=>{let n=s?0:U(t);r===`ready`?(e.log(`dsh 退出(码 ${t??`signal`})——server 失活,退出 ${n}`),u(n)):u(n,`boot`,n===2?`permanent`:`transient`,`dsh 在 ready 前退出(码 ${t??`signal`},stdout 零 token 行)`)}),i&&r!==`ready`&&(e.log(`stdin EOF 已在 boot 窗口到达——退出`),n.kill(),u(1))}catch(e){let t=H(e),n=e instanceof Error?e.message:String(e);u(t===`permanent`?2:1,`boot`,t,n.split(`
3
- `)[0]??n)}}})}function G(e,t,n){let r=[t,`--profile`,e===`acp`?`tboxcode-acp`:`tboxcode-server`,`--patch`,n];return e===`server`&&r.push(`--port`,`0`,`--no-open`),r}async function K(n){let o=r(),c=S(),l=x(c),u=C(l.config,c,l.path!==null,{profile:`acp`});D(u.warnings);let f=await w(u.rows),m=`tboxcode-acp`,h=await i(o,m,`startup`);if(h.status!==`ok`&&h.status!==`version-drift`)throw await f.dispose(),Error(`${n} 启动装配未就绪(${h.status})——运行 tboxcode-dsh init 显式装配`);for(let e of t(h,m))console.error(`tboxcode-dsh ${n}: ${e}`);let g=G(n,a(),f.path),_=p(process.execPath,d(e[n],g),{stdio:`inherit`,env:{...process.env,...M(o,m,`strict`),DSH_HOME:o,DSH_TELEMETRY_DISABLED:`1`}}),v;for(let e of[`SIGINT`,`SIGTERM`])process.on(e,()=>{_.kill(e),v===void 0&&(v=setTimeout(()=>{try{_.kill(`SIGKILL`)}catch{}},8e3),v.unref?.())});process.on(`exit`,()=>{try{_.kill(`SIGKILL`)}catch{}});let y=await new Promise(e=>{_.on(`close`,t=>{v!==void 0&&clearTimeout(v),e(t??1)}),_.on(`error`,t=>{console.error(`tboxcode-dsh ${n}: spawn 失败(${t.message})`),e(1)})});return y!==0&&console.error(s(n,y)),await f.dispose(),typeof y==`number`?y:1}async function q(){return K(`acp`)}function J(){let e=process.stdin,t=T({input:e,crlfDelay:1/0});return{writeStdout:e=>process.stdout.write(e),writeStderr:e=>process.stderr.write(e),onStdinLine:e=>t.on(`line`,e),onStdinEof:t=>{e.on(`end`,t),e.on(`close`,t)},onSignal:e=>{for(let t of[`SIGINT`,`SIGTERM`])process.on(t,()=>e(t))}}}async function Y(n={flags:new Map,positional:[]}){if(n.flags.size>0||n.positional.length>0){let e=[...n.positional,...[...n.flags.keys()].map(e=>`--${e}`)].join(` `);return console.error(`tboxcode-dsh server: 裸命令零 flag(机器路径,Supervisor spawn 契约)——实得 "${e}",拒绝启动`),2}return await W({io:J(),log:e=>console.error(`tboxcode-dsh server: ${e}`),boot:async()=>{let n=r(),o=S(),s=x(o),c=C(s.config,o,s.path!==null,{profile:`server`});D(c.warnings);let l=await w(c.rows),u=await i(n,`tboxcode-server`,`startup`);if(u.status!==`ok`&&u.status!==`version-drift`)throw await l.dispose(),new V(`profile 装配未就绪(${u.status})——运行 tboxcode-dsh init 显式装配`);for(let e of t(u,`tboxcode-server`))console.error(`tboxcode-dsh server: ${e}`);let f=G(`server`,a(),l.path),m=(process.env.TBOXCODE_EXTRA_PATCHES??``).split(`:`).map(e=>e.trim()).filter(e=>e.length>0),h=[...f.slice(0,3),...m.flatMap(e=>[`--patch`,e]),...f.slice(3)],g=p(process.execPath,d(e.server,h),{stdio:[`ignore`,`pipe`,`pipe`],env:{...process.env,DSH_HOME:n,DSH_TELEMETRY_DISABLED:`1`}});g.stderr?.on(`data`,e=>{process.stderr.write(e)}),g.on(`error`,e=>{process.stderr.write(`tboxcode-dsh server: dsh spawn 失败(${e.message})\n`),g.kill()});let _=``;g.stdout?.on(`data`,e=>{for(_+=e.toString();;){let e=_.indexOf(`
4
- `);if(e<0)break;let t=_.slice(0,e);_=_.slice(e+1),v.forEach(e=>e(t))}});let v=[],y=[];return g.on(`close`,e=>{_!==``&&v.forEach(e=>e(_)),y.forEach(t=>t(e))}),{onStdoutLine:e=>v.push(e),onExit:e=>y.push(e),kill:e=>g.kill(e??`SIGTERM`)}}})}function X(e=S(),t=!1){let n=[];m(e)||(t?n.push(`将创建配置目录 ${e}`):(h(e,{recursive:!0}),n.push(`创建配置目录 ${e}`)));let r=b(e,`config.jsonc`);m(r)||(t?n.push(`将写入 config.jsonc 模板(已存在则不覆盖)`):(v(r,`// tboxcode-dsh 配置(规格:.agents/designs/implemented/2026-10-01-config-translation-contract.md)
5
- // 全部字段支持行注释与块注释;热载经 config watcher(LLM 域请求级生效)
6
- {
7
- // 默认模型,"<provider>/<model>"——未设置时 cli spawn 前置明错(不静默回落)
8
- "model": "deepseek-official/deepseek-flash",
9
-
10
- // 默认 agent preset——缺省 standard;acp/headless 仅支持 ptc / standard,
11
- // server/web 另支持 minimal / cordis。修改后需重启进程(startup-only)。
12
- // "agent_preset": "ptc",
13
-
14
- // provider 定义(LlmAdapter:anthropic / openai / openai-compatible;全量接入归 llm-stack)
15
- // "provider": {
16
- // "<id>": {
17
- // "protocol": "openai-compatible",
18
- // "options": { "baseURL": "https://...", "apiKey": "\${MY_ENV_KEY}" },
19
- // "models": { "<model-id>": { "limit": { "context": 128000, "output": 8192 } } }
20
- // }
21
- // },
22
- // 官方 DeepSeek API 直连示例(deepseek-official 官方 adapter 行已禁,id 可自由取用):
23
- // "deepseek-official": {
24
- // "protocol": "openai-compatible",
25
- // "options": { "baseURL": "https://api.deepseek.com" },
26
- // "models": { "deepseek-flash": { "limit": { "context": 128000, "output": 8192 } } }
27
- // },
28
-
29
- // 凭据优先序:本目录 auth.jsonc > provider.options.apiKey;\${VAR} 环境引用值内内插,零 env 注入
30
- }
31
-
32
- `,`utf-8`),n.push(`写入 config.jsonc 模板(已存在则不覆盖)`)));let i=b(e,`auth.jsonc`);return m(i)||(t?n.push(`将写入 auth.jsonc(0600 空)`):(v(i,`{}
33
- `,{encoding:`utf-8`,mode:384}),n.push(`写入 auth.jsonc(0600 空)`))),n}function Z(e,n,r){let i=n.actualStructureOrder.length!==n.expectedStructureOrder.length||n.actualStructureOrder.some((e,t)=>e!==n.expectedStructureOrder[t]);console.log(`tboxcode-dsh init ${e}: dry-run ${n.status}`),n.missingPackages.length>0&&console.log(` missing: ${n.missingPackages.join(`, `)}`),n.forbiddenPackages.length>0&&console.log(` forbidden: ${n.forbiddenPackages.join(`, `)}`),n.managedCorruptPackages.length>0&&console.log(` managed corrupt: ${n.managedCorruptPackages.join(`, `)}(init 将 nuke 重建)`),n.foreignCorruptPackages.length>0&&console.log(` foreign corrupt: ${n.foreignCorruptPackages.join(`, `)}(默认不重建;--repair-all 才重建)`),i?(console.log(` structure expected: [${n.expectedStructureOrder.join(`, `)}]`),console.log(` structure actual: [${n.actualStructureOrder.join(`, `)}]`)):console.log(` structure: 无漂移`);let a=r&&(n.managedCorruptPackages.length>0||n.foreignCorruptPackages.length>0);console.log(` repair-all: ${r?a?`是(将执行全量 nuke;用户自装插件需自行重装)`:`是(当前无残缺包,不会触发 nuke)`:`否`}`);for(let r of t({status:n.status,actions:[],check:n},e))console.warn(r)}async function Q(e){let n=r(),a=e.profile===void 0?l:[e.profile],s=!1;for(let r of a)try{if(e.dryRun===!0){Z(r,o(n,r),e.repairAll===!0);continue}let a=await i(n,r,`explicit`,{repairForeignCorruption:e.repairAll===!0}),s=a.actions.filter(e=>e.kind!==`warn-foreign-corrupt`),c=s.length>0?`(${s.map(e=>e.detail).join(`;`)})`:a.actions.length>0?`(装配无写入,另有诊断提示)`:`(已装配且无漂移,零动作)`;console.log(`tboxcode-dsh init ${r}: ${a.status} ${c}`),a.status===`version-drift`&&console.warn(`tboxcode-dsh init ${r}: 版本偏差放行(显式模式应已重装;残留偏差请核对安装源)`);for(let e of t(a,r,{includeVersionDrift:!1}))console.warn(e)}catch(e){s=!0,console.error(`tboxcode-dsh init ${r} 失败:${e instanceof Error?e.message:String(e)}`)}if(e.config===!0){let t=X(void 0,e.dryRun===!0);for(let e of t)console.log(`tboxcode-dsh init --config: ${e}`)}return s?1:0}const $=[{id:`profiles`,run:()=>{let e=r(),t=[],n=0;for(let r of l){let i=o(e,r),a=i.managedCorruptPackages.length>0?`,本仓链残缺 ${i.managedCorruptPackages.length} 包(${i.managedCorruptPackages.slice(0,3).join(`, `)}${i.managedCorruptPackages.length>3?` …`:``}——运行 tboxcode-dsh init 自愈)`:``,s=i.foreignCorruptPackages.length>0?`,非受管残缺 ${i.foreignCorruptPackages.length} 包(${i.foreignCorruptPackages.slice(0,3).join(`, `)}${i.foreignCorruptPackages.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --repair-all;会重建 node_modules,用户自装插件需自行重装)`:``,c=i.missingPackages.length>0?`,缺 ${i.missingPackages.length} 包`:``,l=i.forbiddenPackages.length>0?`,官方元包污染 ${i.forbiddenPackages.length} 包(${i.forbiddenPackages.join(`, `)}——破坏 dsh 单实例,运行 tboxcode-dsh init 去污染)`:``,u=i.status!==`missing`&&(i.actualStructureOrder.length!==i.expectedStructureOrder.length||i.actualStructureOrder.some((e,t)=>e!==i.expectedStructureOrder[t]))?`,结构漂移 expected=[${i.expectedStructureOrder.join(`, `)}] actual=[${i.actualStructureOrder.join(`, `)}](运行 tboxcode-dsh init 校正)`:``;t.push(`${r}: ${i.status}${a}${s}${c}${l}${u}`),i.status===`missing`||i.status===`incomplete`||i.status===`drift`?n+=1:i.status===`version-drift`&&t.push(` ${r}: 版本偏差 ${i.versionMismatches.map(e=>`${e.pkg} ${e.actual}→${e.expected}`).join(`, `)}(运行 tboxcode-dsh init --profile ${r} 升级)`)}let i=`${t.join(`;`)}${n>0?`——运行 tboxcode-dsh init 修复`:``}`;return{ok:n===0,detail:i}}},{id:`config`,run:()=>{let e=S();try{let t=x(e);if(t.path===null)return{ok:!0,detail:`config 缺席(${e},零配置合法态——spawn 前置会因未设 model 明错)`};let n=t.config.model;if(typeof n!=`string`||n.length===0)return{ok:!1,detail:`${t.path} 缺 model 字段——spawn 前置将明错(未设 model 不静默回落)`};let r=t.config.agent_preset,i=typeof r==`string`&&r.length>0?`;agent_preset = ${r}`:`;agent_preset 未设置(bundle default standard)`;return{ok:!0,detail:`${t.path} model = ${n}${i}`}}catch(e){return{ok:!1,detail:`config 解析失败:${e instanceof Error?e.message:String(e)}`}}}},{id:`auth`,run:()=>{let e=b(S(),`auth.jsonc`);if(!m(e))return{ok:!0,detail:"auth.jsonc 缺席(合法——凭据可走 provider.options 或 ${VAR} 引用)"};let t=_(e).mode&511;return t===384?{ok:!0,detail:`auth.jsonc 权限 600`}:{ok:!0,detail:`auth.jsonc 权限 ${t.toString(8)} → 建议 chmod 600(密钥铁律;llm 插件运行时会弱权限警告)`}}},{id:`versions`,run:()=>{let e=c(),t=n();return e===`0.0.0`?{ok:!0,detail:`cli ${e}(开发占位——未发布;dsh 锁版本 ${t})`}:{ok:!0,detail:`cli ${e};dsh 锁版本 ${t}`}}}];function ee(){console.log(`tboxcode-dsh doctor(诊断不修复——修复走 tboxcode-dsh init)`);let e=0;for(let t of $){let n;try{n=t.run()}catch(e){n={ok:!1,detail:e instanceof Error?e.message:String(e)}}n.ok||(e+=1);let r=n.ok?`✓`:`✗`;console.log(` ${r} [${t.id}] ${n.detail}`)}return console.log(e>0?`结果: ${e} 项红(退出码 1)`:`结果: 全绿(退出码 0)`),e>0?1:0}const te=new Set([`json`,`no-open`,`dry-run`,`repair-all`,`config`]);function ne(e){let t=new Map,n=[],r=0;for(;r<e.length;){let i=e[r]??``;if(i.startsWith(`--`)){let n=i.slice(2);if(te.has(n)){t.set(n,!0),r+=1;continue}let a=e[r+1];if(a!==void 0&&!a.startsWith(`--`)){let e=t.get(n);e===void 0?t.set(n,a):typeof e==`string`?t.set(n,[e,a]):Array.isArray(e)&&t.set(n,[...e,a]),r+=2;continue}t.set(n,!0),r+=1;continue}n.push(i),r+=1}return{flags:t,positional:n}}function re(e){let t={},n=e.flags.get(`port`);typeof n==`string`&&(t.port=Number(n));let r=e.flags.get(`host`);typeof r==`string`&&(t.host=r),e.flags.get(`no-open`)===!0&&(t.noOpen=!0);let i=e.flags.get(`trusted-host`);return i!==void 0&&(t.trustedHost=(Array.isArray(i)?i:[i]).filter(e=>typeof e==`string`)),t}function ie(e){let t={};e.flags.get(`config`)===!0&&(t.config=!0),e.flags.get(`dry-run`)===!0&&(t.dryRun=!0),e.flags.get(`repair-all`)===!0&&(t.repairAll=!0);let n=e.flags.get(`profile`);if(typeof n==`string`&&l.includes(n))t.profile=n;else if(typeof n==`string`)throw Error(`init --profile 仅接受四 profile 名(${l.join(` / `)}),实得 ${n}`);return t}function ae(e){let t={task:e.positional},n=e.flags.get(`session-id`);return typeof n==`string`&&(t.sessionId=n),e.flags.get(`json`)===!0&&(t.json=!0),t}async function oe(){process.title=e.cli;let t=process.argv.slice(2);if(t.includes(`--version`)||t.includes(`-v`)){let e=JSON.parse(g(E(new URL(`../package.json`,import.meta.url)),`utf-8`));return console.log(`tboxcode-dsh ${e.version??`unknown`}`),0}let n=t[0],r=ne(t.slice(1));switch(n){case`web`:return k(re(r));case`headless`:return P(ae(r));case`acp`:return q();case`server`:return Y({flags:r.flags,positional:r.positional});case`init`:return Q(ie(r));case`doctor`:return ee();default:return console.error(`tboxcode-dsh <command> [flags]
34
-
35
- 命令:
36
- web 浏览器直连 dsh 原生 web UI(零 ACP,人工姿态)
37
- acp ACP stdio 载体(Zed 等客户端直连)
38
- server 长驻服务(agtcodingbox spawn)
39
- headless 一次性任务(task → stdout → 退出)
40
- init profile 装配(--config 生成配置骨架)
41
- --profile <name> 指定单 profile
42
- --dry-run 只读预览,不写文件、不跑 dsh plugin
43
- --repair-all 显式允许 foreign corrupt 也重建 node_modules(用户自装插件需自行重装)
44
- doctor 诊断不修复
45
-
46
- 通用:--version`),2}}oe().then(e=>{process.exit(e)}).catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1)});export{};
2
+ import{n as e}from"./identity-DoyHEplh.js";import{readFileSync as t}from"node:fs";import{fileURLToPath as n}from"node:url";const r=[`web`,`headless`,`acp`,`server`,`init`,`doctor`],i={web:{name:`web`,summary:`浏览器直连 dsh 原生 web UI(零 ACP,人工姿态)`,usage:`tboxcode-dsh web [flags]`,flags:[{name:`port`,value:`<n>`,desc:`监听端口(0 由系统选);省略时用 dsh web 默认 3080,占用即明错`},{name:`host`,value:`<host>`,desc:`绑定地址:单个本地接口字面量(不接受通配地址)`},{name:`no-open`,desc:`不自动打开浏览器`},{name:`trusted-host`,value:`<a>`,repeatable:!0,desc:`/api 浏览器信任栅栏的额外 authority(host 或 host:port)`}],notes:[`装配:首次运行自动补装 tboxcode-web;装配未就绪时先跑 tboxcode-dsh init`,`原生面:--public-url / --tls-cert / --tls-key 等本命令不透传;完整清单见`,` dsh --profile tboxcode-web --help(需先装配该 profile)`]},headless:{name:`headless`,summary:`一次性任务:task → stdout → 退出`,usage:`tboxcode-dsh headless [flags] <task...>`,flags:[{name:`session-id`,value:`<id>`,desc:`复用已有会话(adopt;preset 会话会被拒绝)`},{name:`json`,desc:`事件流 JSON 输出`}],positionals:[{name:`<task...>`,variadic:!0,desc:`任务词序列,原样拼合后交给 dsh`}],notes:[`任务词以 - 开头时用 -- 终止符转义(-- 之后不再识别 flag)`]},acp:{name:`acp`,summary:`ACP stdio 载体(Zed 等客户端直连)`,usage:`tboxcode-dsh acp`,flags:[],notes:[`启动即装配检查(未就绪则补装);stdout 为 ACP 协议通道,零污染由守卫保证`]},server:{name:`server`,summary:`长驻服务(agtcodingbox spawn;裸命令零 flag)`,usage:`tboxcode-dsh server`,flags:[],validation:`passthrough`,exitCodes:`0 正常;1 dsh 退出(server 失活);2 用法错误(含裸命令零 flag);3 信封协议版本偏斜`,notes:[`裸命令零 flag:任何 flag 或位置参数都被拒(退出码 2)——机器路径,Supervisor spawn 契约`,`信封协议:stdout 输出 standby / ready,stdin 收 start`]},init:{name:`init`,summary:`profile 装配(四链 ensure + 配置骨架)`,usage:`tboxcode-dsh init [flags]`,flags:[{name:`config`,desc:`生成配置骨架(config.jsonc 模板 + 0600 空 auth.jsonc;幂等不覆盖)`},{name:`dry-run`,desc:`只读预览:不写文件、不跑 dsh plugin、不建骨架`},{name:`repair-all`,desc:`显式允许非受管残缺也重建 node_modules(用户自装插件需自行重装)`},{name:`profile`,value:`<name>`,desc:`只装单个 profile(缺省四链全装)`}],notes:[`profile 名:tboxcode-acp / tboxcode-server / tboxcode-web / tboxcode-headless`]},doctor:{name:`doctor`,summary:`诊断不修复(有红即退出码 1)`,usage:`tboxcode-dsh doctor`,flags:[],notes:[`修复走 tboxcode-dsh init`]}};function a(e){return r.includes(e)}function o(e){return new Set(e.flags.filter(e=>e.value===void 0).map(e=>e.name))}function s(e){return new Map(e.flags.filter(e=>e.value!==void 0).map(e=>[e.name,e]))}function c(e,t=` `){if(e.length===0)return``;let n=Math.max(...e.map(([e])=>e.length)),r=Math.min(30,Math.max(18,n));return e.map(([e,n])=>`${t}${e.padEnd(r+2)}${n}`).join(`
3
+ `)}const l=[[`-h, --help`,`显示本帮助并退出`],[`-v, --version`,`显示版本并退出`]];function u(){return[`tboxcode-dsh <command> [flags]`,``,`命令:`,c([...r.map(e=>[e,i[e].summary]),...l]),``,`每个命令的详细用法:tboxcode-dsh help <command>`].join(`
4
+ `)}function d(e){let t=i[e],n=[...t.flags.map(e=>[`--${e.name}${e.value===void 0?``:` ${e.value}`}`,e.repeatable===!0?`${e.desc}(可重复)`:e.desc]),...l],r=[`tboxcode-dsh ${e} — ${t.summary}`,``,`用法:`,` ${t.usage}`,``,`Flags:`,c(n)];return t.positionals!==void 0&&t.positionals.length>0&&r.push(``,`参数:`,c(t.positionals.map(e=>[e.name,e.desc]))),t.notes!==void 0&&t.notes.length>0&&r.push(``,`说明:`,t.notes.map(e=>` ${e}`).join(`
5
+ `)),r.push(``,`退出码:`,` ${t.exitCodes??`0 正常;2 用法错误;其余非零 = 运行时失败(spawn 类命令透传 dsh 退出码)`}`),r.join(`
6
+ `)}var f=class extends Error{};const p=new Set([`-h`,`--help`]),m=new Set([`-v`,`--version`]),h=new Set([`json`,`no-open`,`dry-run`,`repair-all`,`config`]);function g(e){let t=new Map,n=[],r=0;for(;r<e.length;){let i=e[r]??``;if(i.startsWith(`--`)){let n=i.slice(2);if(h.has(n)){t.set(n,!0),r+=1;continue}let a=e[r+1];if(a!==void 0&&!a.startsWith(`--`)){let e=t.get(n);e===void 0?t.set(n,a):typeof e==`string`?t.set(n,[e,a]):Array.isArray(e)&&t.set(n,[...e,a]),r+=2;continue}t.set(n,!0),r+=1;continue}n.push(i),r+=1}return{flags:t,positional:n}}function _(e,t){let n=o(t),r=s(t),i=new Map,a=[],c=[],l=!1,u=!1,d=!1;for(let t=0;t<e.length;t+=1){let o=e[t]??``;if(d){a.push(o);continue}if(o===`--`){d=!0;continue}if(o.startsWith(`--`)){let a=o.indexOf(`=`),s=a>=0?o.slice(2,a):o.slice(2),d=a>=0?o.slice(a+1):void 0;if(s===`help`||s===`version`){d===void 0?s===`help`?l=!0:u=!0:c.push(`--${s} 不接受值`);continue}if(n.has(s)){if(d!==void 0){c.push(`--${s} 不接受值`);continue}i.set(s,!0);continue}let f=r.get(s);if(f===void 0){c.push(`未知 flag --${s}`);continue}let p=d;if(p===void 0){let n=e[t+1];n!==void 0&&!n.startsWith(`--`)&&(p=n,t+=1)}if(p===void 0){c.push(`--${s} 需要值`);continue}if(f.repeatable===!0){let e=i.get(s);i.set(s,Array.isArray(e)?[...e,p]:typeof e==`string`?[e,p]:[p])}else i.set(s,p);continue}if(o.startsWith(`-`)&&o.length>1){if(o===`-h`){l=!0;continue}if(o===`-v`){u=!0;continue}c.push(`未知 flag ${o}`);continue}a.push(o)}return{flags:i,positional:a,help:l,version:u,errors:c}}function v(e,t){if(t.errors.length>0)throw new f(t.errors[0]??`用法错误`);let n=e.positionals??[];if(n.length===0){if(t.positional.length>0)throw new f(`不接受位置参数,实得 "${t.positional.join(` `)}"`);return}if(n[0]?.variadic!==!0&&t.positional.length>n.length)throw new f(`位置参数过多(最多 ${n.length} 个)`)}function y(){let e=JSON.parse(t(n(new URL(`../package.json`,import.meta.url)),`utf-8`));console.log(`tboxcode-dsh ${e.version??`unknown`}`)}function b(e){let t={},n=e.flags.get(`port`);if(typeof n==`string`){let e=Number(n);if(!Number.isInteger(e)||e<0||e>65535)throw new f(`--port 必须是 0..65535 的整数,实得 "${n}"`);t.port=e}let r=e.flags.get(`host`);typeof r==`string`&&(t.host=r),e.flags.get(`no-open`)===!0&&(t.noOpen=!0);let i=e.flags.get(`trusted-host`);return i!==void 0&&(t.trustedHost=(Array.isArray(i)?i:[i]).filter(e=>typeof e==`string`)),t}function x(e,t){let n={};e.flags.get(`config`)===!0&&(n.config=!0),e.flags.get(`dry-run`)===!0&&(n.dryRun=!0),e.flags.get(`repair-all`)===!0&&(n.repairAll=!0);let r=e.flags.get(`profile`);if(typeof r==`string`){if(!t.includes(r))throw new f(`--profile 仅接受四 profile 名(${t.join(` / `)}),实得 ${r}`);n.profile=r}return n}function S(e){let t={task:e.positional},n=e.flags.get(`session-id`);return typeof n==`string`&&(t.sessionId=n),e.flags.get(`json`)===!0&&(t.json=!0),t}async function C(e,t){switch(e){case`web`:{let{runWeb:e}=await import(`./web-dA-kJdsr.js`);return e(b(t))}case`headless`:{let{runHeadless:e}=await import(`./headless-ofA6qyIK.js`);return e(S(t))}case`acp`:{let{runAcp:e}=await import(`./forms-CQF321Hd.js`);return e()}case`init`:{let[{OWN_PROFILES:e},{runInit:n}]=await Promise.all([import(`./assembly-DNm73rzb.js`),import(`./init-B-hj4Nce.js`)]);return n(x(t,e))}case`doctor`:{let{runDoctor:e}=await import(`./doctor-5841wpA-.js`);return e()}}}async function w(e,t){let n=i[e];try{if(n.validation===`passthrough`){if(t.some(e=>p.has(e)))return console.log(d(e)),0;if(t.some(e=>m.has(e)))return y(),0;let n=g(t),{runServer:r}=await import(`./forms-CQF321Hd.js`);return await r({flags:n.flags,positional:n.positional})}let r=_(t,n);return r.help?(console.log(d(e)),0):r.version?(y(),0):(v(n,r),await C(e,r))}catch(t){if(t instanceof f)return console.error(`tboxcode-dsh ${e}: ${t.message}`),console.error(`运行 tboxcode-dsh ${e} --help 查看用法`),2;throw t}}async function T(){process.title=e.cli;let[t,...n]=process.argv.slice(2);if(t===void 0)return console.error(`tboxcode-dsh: 缺少命令`),console.error(u()),2;if(p.has(t))return console.log(u()),0;if(m.has(t))return y(),0;if(t===`help`){let e=n[0];return e===void 0?(console.log(u()),0):a(e)?(console.log(d(e)),0):(console.error(`tboxcode-dsh: 未知命令 "${e}"`),console.error(u()),2)}return a(t)?await w(t,n):(console.error(`tboxcode-dsh: 未知命令 "${t}"`),console.error(u()),2)}T().then(e=>{process.exit(e)}).catch(e=>{console.error(e instanceof Error?e.message:String(e)),process.exit(1)});export{};
@@ -0,0 +1 @@
1
+ import"./identity-DoyHEplh.js";import{c as e,h as t,l as n,s as r,t as i}from"./assembly-OSsJmFyv.js";import{existsSync as a,statSync as o}from"node:fs";import{join as s}from"node:path";import{loadConfig as c,resolveConfigDir as l}from"@tbox.cn/tboxcode-dsh-runtime";const u=[{id:`profiles`,run:()=>{let e=t(),n=[],a=0;for(let t of i){let i=r(e,t),o=i.managedCorruptPackages.length>0?`,本仓链残缺 ${i.managedCorruptPackages.length} 包(${i.managedCorruptPackages.slice(0,3).join(`, `)}${i.managedCorruptPackages.length>3?` …`:``}——运行 tboxcode-dsh init 自愈)`:``,s=i.foreignCorruptPackages.length>0?`,非受管残缺 ${i.foreignCorruptPackages.length} 包(${i.foreignCorruptPackages.slice(0,3).join(`, `)}${i.foreignCorruptPackages.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --repair-all;会重建 node_modules,用户自装插件需自行重装)`:``,c=i.missingPackages.length>0?`,缺 ${i.missingPackages.length} 包`:``,l=i.forbiddenPackages.length>0?`,官方元包污染 ${i.forbiddenPackages.length} 包(${i.forbiddenPackages.join(`, `)}——破坏 dsh 单实例,运行 tboxcode-dsh init 去污染)`:``,u=i.status!==`missing`&&(i.actualStructureOrder.length!==i.expectedStructureOrder.length||i.actualStructureOrder.some((e,t)=>e!==i.expectedStructureOrder[t]))?`,结构漂移 expected=[${i.expectedStructureOrder.join(`, `)}] actual=[${i.actualStructureOrder.join(`, `)}](运行 tboxcode-dsh init 校正)`:``;n.push(`${t}: ${i.status}${o}${s}${c}${l}${u}`),i.status===`missing`||i.status===`incomplete`||i.status===`drift`?a+=1:i.status===`version-drift`&&n.push(` ${t}: 版本偏差 ${i.versionMismatches.map(e=>`${e.pkg} ${e.actual}→${e.expected}`).join(`, `)}(运行 tboxcode-dsh init --profile ${t} 升级)`)}let o=`${n.join(`;`)}${a>0?`——运行 tboxcode-dsh init 修复`:``}`;return{ok:a===0,detail:o}}},{id:`config`,run:()=>{let e=l();try{let t=c(e);if(t.path===null)return{ok:!0,detail:`config 缺席(${e},零配置合法态——spawn 前置会因未设 model 明错)`};let n=t.config.model;if(typeof n!=`string`||n.length===0)return{ok:!1,detail:`${t.path} 缺 model 字段——spawn 前置将明错(未设 model 不静默回落)`};let r=t.config.agent_preset,i=typeof r==`string`&&r.length>0?`;agent_preset = ${r}`:`;agent_preset 未设置(bundle default standard)`;return{ok:!0,detail:`${t.path} model = ${n}${i}`}}catch(e){return{ok:!1,detail:`config 解析失败:${e instanceof Error?e.message:String(e)}`}}}},{id:`auth`,run:()=>{let e=s(l(),`auth.jsonc`);if(!a(e))return{ok:!0,detail:"auth.jsonc 缺席(合法——凭据可走 provider.options 或 ${VAR} 引用)"};let t=o(e).mode&511;return t===384?{ok:!0,detail:`auth.jsonc 权限 600`}:{ok:!0,detail:`auth.jsonc 权限 ${t.toString(8)} → 建议 chmod 600(密钥铁律;llm 插件运行时会弱权限警告)`}}},{id:`versions`,run:()=>{let t=e(),r=n();return t===`0.0.0`?{ok:!0,detail:`cli ${t}(开发占位——未发布;dsh 锁版本 ${r})`}:{ok:!0,detail:`cli ${t};dsh 锁版本 ${r}`}}}];function d(){console.log(`tboxcode-dsh doctor(诊断不修复——修复走 tboxcode-dsh init)`);let e=0;for(let t of u){let n;try{n=t.run()}catch(e){n={ok:!1,detail:e instanceof Error?e.message:String(e)}}n.ok||(e+=1);let r=n.ok?`✓`:`✗`;console.log(` ${r} [${t.id}] ${n.detail}`)}return console.log(e>0?`结果: ${e} 项红(退出码 1)`:`结果: 全绿(退出码 0)`),e>0?1:0}export{u as DOCTOR_CHECKS,d as runDoctor};
@@ -0,0 +1,3 @@
1
+ import{n as e,r as t}from"./identity-DoyHEplh.js";import{b as n,h as r,o as i,u as a,y as o}from"./assembly-OSsJmFyv.js";import{t as s}from"./guard-CYoEPKMv.js";import{t as c}from"./warnings-dt8YfUjo.js";import{spawn as l}from"node:child_process";import{loadConfig as u,resolveConfigDir as d,translateConfig as f,writeOverlayPatch as p}from"@tbox.cn/tboxcode-dsh-runtime";import{createInterface as m}from"node:readline";function h(){return JSON.stringify({v:1,event:`standby`})}function g(e,t){return JSON.stringify({v:1,event:`ready`,port:e,token:t})}function _(e,t,n){return JSON.stringify({stage:e,kind:t,error:n})}function v(e){let t;try{t=JSON.parse(e)}catch{return{kind:`malformed`}}if(typeof t!=`object`||!t||Array.isArray(t))return{kind:`malformed`};let n=t;return n.v===1?n.cmd===`start`?{kind:`start`}:{kind:`unknown`,cmd:String(n.cmd??``)}:{kind:`version-skew`,got:typeof n.v==`number`?n.v:void 0}}function y(e){if(!e.includes(`token=`))return null;let t=/https?:\/\/\S+/.exec(e);if(t===null)return null;try{let e=new URL(t[0]);return e.searchParams.get(`token`)===null?null:e}catch{return null}}function b(e){let t=e.searchParams.get(`token`),n=e.port===``?void 0:Number(e.port);return t===null||t===``||n===void 0||!Number.isInteger(n)?null:{port:n,token:t}}var x=class extends Error{};function S(e){if(e instanceof x)return`permanent`;let t=e?.name;return t===`ModelNotSetError`||t===`ModelMalformedError`||t===`SecretInTranslationError`?`permanent`:`transient`}function C(e){return e===null?1:e===0?0:e===2||e===3?e:1}async function w(e){let{io:t}=e;return await new Promise(n=>{let r=`standby`,i=!1,a=!1,o=!1,s=!1,c,l=!1,u=(e,r,i,a)=>{o||(o=!0,r!==void 0&&i!==void 0&&a!==void 0&&t.writeStderr(`${_(r,i,a)}\n`),n(e))};t.writeStdout(`${h()}\n`),e.log(`standby(零用户态自检通过,等待 start)`),t.onSignal(t=>{if(s=!0,e.log(`收到 ${t},正常终(dsh 子进程随停)`),c===void 0){u(0);return}c.kill(t);let n=setTimeout(()=>u(0),3e3);c.onExit(()=>{clearTimeout(n),u(0)})}),t.onStdinEof(()=>{if(!a){if(a=!0,i=!0,r!==`ready`){e.log(`stdin EOF 在 ready 前到达(父进程死亡)——退出`),c?.kill(),u(1);return}e.log(`stdin EOF 在 ready 后到达——孤儿语义,继续运行`)}}),t.onStdinLine(e=>{if(e.trim()===``)return;if(r===`ready`){t.writeStderr(`${_(`protocol`,`protocol`,`start 命令在 ready 后到达(stdin 已转惰性),忽略`)}\n`);return}let n=v(e);switch(n.kind){case`version-skew`:u(3,`protocol`,`protocol`,`信封版本偏斜:期待 v1,实得 ${n.got??`缺省`}——fail-loud(部署错误最显眼信号)`);return;case`start`:if(r!==`standby`)return;r=`starting`,d();return;case`unknown`:t.writeStderr(`${_(`protocol`,`protocol`,`未知 cmd "${n.cmd}" 忽略(additive-only 版本窗口)`)}\n`);return;case`malformed`:t.writeStderr(`${_(`protocol`,`protocol`,`stdin 行非 JSON 对象,忽略`)}\n`);return}});let d=async()=>{try{let n=await e.boot();if(c=n,o){n.kill();return}n.onStdoutLine(n=>{if(l)return;let i=y(n);if(i===null)return;let a=b(i);a!==null&&(l=!0,r=`ready`,t.writeStdout(`${g(a.port,a.token)}\n`),e.log(`ready(token 已交付,stdout 永久静默;token 已弃)`))}),n.onExit(t=>{let n=s?0:C(t);r===`ready`?(e.log(`dsh 退出(码 ${t??`signal`})——server 失活,退出 ${n}`),u(n)):u(n,`boot`,n===2?`permanent`:`transient`,`dsh 在 ready 前退出(码 ${t??`signal`},stdout 零 token 行)`)}),i&&r!==`ready`&&(e.log(`stdin EOF 已在 boot 窗口到达——退出`),n.kill(),u(1))}catch(e){let t=S(e),n=e instanceof Error?e.message:String(e);u(t===`permanent`?2:1,`boot`,t,n.split(`
2
+ `)[0]??n)}}})}function T(e,t,n){let r=[t,`--profile`,e===`acp`?`tboxcode-acp`:`tboxcode-server`,`--patch`,n];return e===`server`&&r.push(`--port`,`0`,`--no-open`),r}async function E(m){let h=r(),g=d(),_=u(g),v=f(_.config,g,_.path!==null,{profile:`acp`});c(v.warnings);let y=await p(v.rows),b=`tboxcode-acp`,x=await a(h,b,`startup`);if(x.status!==`ok`&&x.status!==`version-drift`)throw await y.dispose(),Error(`${m} 启动装配未就绪(${x.status})——运行 tboxcode-dsh init 显式装配`);for(let e of i(x,b))console.error(`tboxcode-dsh ${m}: ${e}`);let S=T(m,n(),y.path),C=l(process.execPath,t(e[m],S),{stdio:`inherit`,env:{...process.env,...s(h,b,`strict`),DSH_HOME:h,DSH_TELEMETRY_DISABLED:`1`}}),w;for(let e of[`SIGINT`,`SIGTERM`])process.on(e,()=>{C.kill(e),w===void 0&&(w=setTimeout(()=>{try{C.kill(`SIGKILL`)}catch{}},8e3),w.unref?.())});process.on(`exit`,()=>{try{C.kill(`SIGKILL`)}catch{}});let E=await new Promise(e=>{C.on(`close`,t=>{w!==void 0&&clearTimeout(w),e(t??1)}),C.on(`error`,t=>{console.error(`tboxcode-dsh ${m}: spawn 失败(${t.message})`),e(1)})});return E!==0&&console.error(o(m,E)),await y.dispose(),typeof E==`number`?E:1}async function D(){return E(`acp`)}function O(){let e=process.stdin,t=m({input:e,crlfDelay:1/0});return{writeStdout:e=>process.stdout.write(e),writeStderr:e=>process.stderr.write(e),onStdinLine:e=>t.on(`line`,e),onStdinEof:t=>{e.on(`end`,t),e.on(`close`,t)},onSignal:e=>{for(let t of[`SIGINT`,`SIGTERM`])process.on(t,()=>e(t))}}}async function k(o={flags:new Map,positional:[]}){if(o.flags.size>0||o.positional.length>0){let e=[...o.positional,...[...o.flags.keys()].map(e=>`--${e}`)].join(` `);return console.error(`tboxcode-dsh server: 裸命令零 flag(机器路径,Supervisor spawn 契约)——实得 "${e}",拒绝启动`),2}return await w({io:O(),log:e=>console.error(`tboxcode-dsh server: ${e}`),boot:async()=>{let o=r(),s=d(),m=u(s),h=f(m.config,s,m.path!==null,{profile:`server`});c(h.warnings);let g=await p(h.rows),_=await a(o,`tboxcode-server`,`startup`);if(_.status!==`ok`&&_.status!==`version-drift`)throw await g.dispose(),new x(`profile 装配未就绪(${_.status})——运行 tboxcode-dsh init 显式装配`);for(let e of i(_,`tboxcode-server`))console.error(`tboxcode-dsh server: ${e}`);let v=T(`server`,n(),g.path),y=(process.env.TBOXCODE_EXTRA_PATCHES??``).split(`:`).map(e=>e.trim()).filter(e=>e.length>0),b=[...v.slice(0,3),...y.flatMap(e=>[`--patch`,e]),...v.slice(3)],S=l(process.execPath,t(e.server,b),{stdio:[`ignore`,`pipe`,`pipe`],env:{...process.env,DSH_HOME:o,DSH_TELEMETRY_DISABLED:`1`}});S.stderr?.on(`data`,e=>{process.stderr.write(e)}),S.on(`error`,e=>{process.stderr.write(`tboxcode-dsh server: dsh spawn 失败(${e.message})\n`),S.kill()});let C=``;S.stdout?.on(`data`,e=>{for(C+=e.toString();;){let e=C.indexOf(`
3
+ `);if(e<0)break;let t=C.slice(0,e);C=C.slice(e+1),w.forEach(e=>e(t))}});let w=[],E=[];return S.on(`close`,e=>{C!==``&&w.forEach(e=>e(C)),E.forEach(t=>t(e))}),{onStdoutLine:e=>w.push(e),onExit:e=>E.push(e),kill:e=>S.kill(e??`SIGTERM`)}}})}export{T as profileSpawnArgs,D as runAcp,k as runServer};
@@ -0,0 +1 @@
1
+ import{m as e}from"./assembly-OSsJmFyv.js";import{createRequire as t}from"node:module";import{dirname as n,join as r}from"node:path";const i=t(import.meta.url);function a(){return r(n(i.resolve(`@tbox.cn/tboxcode-dsh-runtime/package.json`)),`assets`,`stdout-guard.cjs`)}function o(t,n,r){let i=a(),o=process.env.NODE_OPTIONS??``,s=`--require ${i}`;return{NODE_OPTIONS:o.includes(i)?o:o===``?s:`${o} ${s}`,TBOXCODE_STDIO_GUARD:r,TBOXCODE_PROFILE_DIR:e(t,n)}}export{o as t};
@@ -0,0 +1 @@
1
+ import{n as e,r as t}from"./identity-DoyHEplh.js";import{b as n,h as r,o as i,u as a,y as o}from"./assembly-OSsJmFyv.js";import{t as s}from"./guard-CYoEPKMv.js";import{t as c}from"./warnings-dt8YfUjo.js";import{spawn as l}from"node:child_process";import{loadConfig as u,resolveConfigDir as d,translateConfig as f,writeOverlayPatch as p}from"@tbox.cn/tboxcode-dsh-runtime";function m(e,t,n){let r=[e,`--profile`,`tboxcode-headless`,`--patch`,t];return n.sessionId!==void 0&&r.push(`--session-id`,n.sessionId),n.json===!0&&r.push(`--json`),r.push(...n.task),r}async function h(h){let g=r(),_=d(),v=u(_),y=f(v.config,_,v.path!==null,{profile:`headless`});c(y.warnings);let b=await p(y.rows),x=await a(g,`tboxcode-headless`,`startup`);if(x.status!==`ok`&&x.status!==`version-drift`)throw await b.dispose(),Error(`headless 启动装配未就绪(${x.status})——运行 tboxcode-dsh init 显式装配`);for(let e of i(x,`tboxcode-headless`))console.error(`tboxcode-dsh headless: ${e}`);let S=m(n(),b.path,h),C=l(process.execPath,t(e.headless,S),{stdio:`inherit`,env:{...process.env,...s(g,`tboxcode-headless`,`quiet`),DSH_HOME:g,DSH_TELEMETRY_DISABLED:`1`}}),w=await new Promise(e=>{C.on(`close`,e),C.on(`error`,t=>{console.error(`tboxcode-dsh headless: spawn 失败(${t.message})`),e(1)})});return w!==0&&console.error(o(`headless`,w)),await b.dispose(),typeof w==`number`?w:1}export{m as headlessSpawnArgs,h as runHeadless};
@@ -0,0 +1 @@
1
+ const e=`@tbox.cn/tboxcode-dsh-cli`,t={cli:`tboxcode-dsh`,acp:`tboxcode-dsh-acp`,server:`tboxcode-dsh-server`,web:`tboxcode-dsh-web`,headless:`tboxcode-dsh-headless`};function n(e,t){return[`--title=${e}`,...t]}export{t as n,n as r,e as t};
package/lib/index.js CHANGED
@@ -1 +1 @@
1
- import{a as e,d as t,f as n,g as r,h as i,i as a,l as o,m as s,n as c,o as l,r as u,t as d,u as f}from"./assembly-CxMZLSKV.js";export{d as OWN_PROFILES,r as PACKAGE_NAME,c as PROFILE_CHAINS,u as PROFILE_CLEANUP_ROWS,a as PROFILE_OFFICIAL_ROWS,e as assemblyHint,l as checkAssembly,o as ensureAssembly,f as profileDir,s as resolveDshEntry,t as resolveDshHome,i as runDsh,n as versionDriftHint};
1
+ import{t as e}from"./identity-DoyHEplh.js";import{b as t,h as n,i as r,m as i,n as a,o,r as s,s as c,t as l,u,v as d,x as f}from"./assembly-OSsJmFyv.js";export{l as OWN_PROFILES,e as PACKAGE_NAME,a as PROFILE_CHAINS,s as PROFILE_CLEANUP_ROWS,r as PROFILE_OFFICIAL_ROWS,o as assemblyHint,c as checkAssembly,u as ensureAssembly,i as profileDir,t as resolveDshEntry,n as resolveDshHome,f as runDsh,d as versionDriftHint};
@@ -0,0 +1,30 @@
1
+ import"./identity-DoyHEplh.js";import{h as e,o as t,s as n,t as r,u as i}from"./assembly-OSsJmFyv.js";import{existsSync as a,mkdirSync as o,writeFileSync as s}from"node:fs";import{join as c}from"node:path";import{resolveConfigDir as l}from"@tbox.cn/tboxcode-dsh-runtime";function u(e=l(),t=!1){let n=[];a(e)||(t?n.push(`将创建配置目录 ${e}`):(o(e,{recursive:!0}),n.push(`创建配置目录 ${e}`)));let r=c(e,`config.jsonc`);a(r)||(t?n.push(`将写入 config.jsonc 模板(已存在则不覆盖)`):(s(r,`// tboxcode-dsh 配置(规格:.agents/designs/implemented/2026-10-01-config-translation-contract.md)
2
+ // 全部字段支持行注释与块注释;热载经 config watcher(LLM 域请求级生效)
3
+ {
4
+ // 默认模型,"<provider>/<model>"——未设置时 cli spawn 前置明错(不静默回落)
5
+ "model": "deepseek-official/deepseek-flash",
6
+
7
+ // 默认 agent preset——缺省 standard;acp/headless 仅支持 ptc / standard,
8
+ // server/web 另支持 minimal / cordis。修改后需重启进程(startup-only)。
9
+ // "agent_preset": "ptc",
10
+
11
+ // provider 定义(LlmAdapter:anthropic / openai / openai-compatible;全量接入归 llm-stack)
12
+ // "provider": {
13
+ // "<id>": {
14
+ // "protocol": "openai-compatible",
15
+ // "options": { "baseURL": "https://...", "apiKey": "\${MY_ENV_KEY}" },
16
+ // "models": { "<model-id>": { "limit": { "context": 128000, "output": 8192 } } }
17
+ // }
18
+ // },
19
+ // 官方 DeepSeek API 直连示例(deepseek-official 官方 adapter 行已禁,id 可自由取用):
20
+ // "deepseek-official": {
21
+ // "protocol": "openai-compatible",
22
+ // "options": { "baseURL": "https://api.deepseek.com" },
23
+ // "models": { "deepseek-flash": { "limit": { "context": 128000, "output": 8192 } } }
24
+ // },
25
+
26
+ // 凭据优先序:本目录 auth.jsonc > provider.options.apiKey;\${VAR} 环境引用值内内插,零 env 注入
27
+ }
28
+
29
+ `,`utf-8`),n.push(`写入 config.jsonc 模板(已存在则不覆盖)`)));let i=c(e,`auth.jsonc`);return a(i)||(t?n.push(`将写入 auth.jsonc(0600 空)`):(s(i,`{}
30
+ `,{encoding:`utf-8`,mode:384}),n.push(`写入 auth.jsonc(0600 空)`))),n}function d(e,n,r){let i=n.actualStructureOrder.length!==n.expectedStructureOrder.length||n.actualStructureOrder.some((e,t)=>e!==n.expectedStructureOrder[t]);console.log(`tboxcode-dsh init ${e}: dry-run ${n.status}`),n.missingPackages.length>0&&console.log(` missing: ${n.missingPackages.join(`, `)}`),n.forbiddenPackages.length>0&&console.log(` forbidden: ${n.forbiddenPackages.join(`, `)}`),n.managedCorruptPackages.length>0&&console.log(` managed corrupt: ${n.managedCorruptPackages.join(`, `)}(init 将 nuke 重建)`),n.foreignCorruptPackages.length>0&&console.log(` foreign corrupt: ${n.foreignCorruptPackages.join(`, `)}(默认不重建;--repair-all 才重建)`),i?(console.log(` structure expected: [${n.expectedStructureOrder.join(`, `)}]`),console.log(` structure actual: [${n.actualStructureOrder.join(`, `)}]`)):console.log(` structure: 无漂移`);let a=r&&(n.managedCorruptPackages.length>0||n.foreignCorruptPackages.length>0);console.log(` repair-all: ${r?a?`是(将执行全量 nuke;用户自装插件需自行重装)`:`是(当前无残缺包,不会触发 nuke)`:`否`}`);for(let r of t({status:n.status,actions:[],check:n},e))console.warn(r)}async function f(a){let o=e(),s=a.profile===void 0?r:[a.profile],c=!1;for(let e of s)try{if(a.dryRun===!0){d(e,n(o,e),a.repairAll===!0);continue}let r=await i(o,e,`explicit`,{repairForeignCorruption:a.repairAll===!0}),s=r.actions.filter(e=>e.kind!==`warn-foreign-corrupt`),c=s.length>0?`(${s.map(e=>e.detail).join(`;`)})`:r.actions.length>0?`(装配无写入,另有诊断提示)`:`(已装配且无漂移,零动作)`;console.log(`tboxcode-dsh init ${e}: ${r.status} ${c}`),r.status===`version-drift`&&console.warn(`tboxcode-dsh init ${e}: 版本偏差放行(显式模式应已重装;残留偏差请核对安装源)`);for(let n of t(r,e,{includeVersionDrift:!1}))console.warn(n)}catch(t){c=!0,console.error(`tboxcode-dsh init ${e} 失败:${t instanceof Error?t.message:String(t)}`)}if(a.config===!0){let e=u(void 0,a.dryRun===!0);for(let t of e)console.log(`tboxcode-dsh init --config: ${t}`)}return c?1:0}export{u as ensureConfigSkeleton,f as runInit};
@@ -0,0 +1 @@
1
+ function e(e){for(let t of e)console.error(`tboxcode: ${t.field}: ${t.reason}`)}export{e as t};
@@ -0,0 +1 @@
1
+ import{n as e,r as t}from"./identity-DoyHEplh.js";import{b as n,h as r,o as i,u as a,y as o}from"./assembly-OSsJmFyv.js";import{t as s}from"./warnings-dt8YfUjo.js";import{spawn as c}from"node:child_process";import{loadConfig as l,resolveConfigDir as u,translateConfig as d,writeOverlayPatch as f}from"@tbox.cn/tboxcode-dsh-runtime";function p(e,t,n){let r=[e,`--profile`,`tboxcode-web`,`--patch`,t];n.port!==void 0&&r.push(`--port`,String(n.port)),n.host!==void 0&&r.push(`--host`,n.host),n.noOpen===!0&&r.push(`--no-open`);for(let e of n.trustedHost??[])r.push(`--trusted-host`,e);return r}async function m(m){let h=r(),g=u(),_=l(g),v=d(_.config,g,_.path!==null,{profile:`web`});s(v.warnings);let y=await f(v.rows),b=await a(h,`tboxcode-web`,`startup`);if(b.status!==`ok`&&b.status!==`version-drift`)throw await y.dispose(),Error(`web 启动装配未就绪(${b.status})——运行 tboxcode-dsh init 显式装配`);for(let e of i(b,`tboxcode-web`))console.error(`tboxcode-dsh web: ${e}`);let x=p(n(),y.path,{...m,dshHome:h}),S=c(process.execPath,t(e.web,x),{stdio:`inherit`,env:{...process.env,DSH_HOME:h,DSH_TELEMETRY_DISABLED:`1`}});for(let e of[`SIGINT`,`SIGTERM`])process.on(e,()=>{S.kill(e)});let C=await new Promise(e=>{S.on(`close`,e),S.on(`error`,t=>{console.error(`tboxcode-dsh web: spawn 失败(${t.message})`),e(1)})});return C!==0&&console.error(o(`web`,C)),await y.dispose(),typeof C==`number`?C:1}export{m as runWeb,p as webSpawnArgs};
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@tbox.cn/tboxcode-dsh-cli",
3
- "version": "0.1.0-rc.14",
3
+ "version": "0.1.0-rc.16",
4
4
  "type": "module",
5
5
  "description": "bin tboxcode-dsh:acp / server / web / headless / init / doctor——薄封装,spawn dsh --profile <name> --patch <generated>;装配模块住本包",
6
6
  "main": "lib/index.js",
@@ -24,7 +24,7 @@
24
24
  "@deepseek-ai/dsh": "0.2.2-rc.1",
25
25
  "@deepseek-ai/dsh-atomic-write": "0.2.2-rc.1",
26
26
  "yaml": "^2.8.0",
27
- "@tbox.cn/tboxcode-dsh-runtime": "0.1.0-rc.14"
27
+ "@tbox.cn/tboxcode-dsh-runtime": "0.1.0-rc.16"
28
28
  },
29
29
  "devDependencies": {
30
30
  "@deepseek-ai/dsh-llm-mock-server": "0.2.2-rc.1"
@@ -1,2 +0,0 @@
1
- import{createRequire as e}from"node:module";import{spawn as t}from"node:child_process";import{existsSync as n,mkdirSync as r,readFileSync as i,readdirSync as a,rmSync as o,statSync as s}from"node:fs";import{dirname as c,join as l}from"node:path";import{homedir as u}from"node:os";import{parse as d,stringify as f}from"yaml";import{withFileLock as p,writeFileAtomic as m}from"@deepseek-ai/dsh-atomic-write";const h=`@tbox.cn/tboxcode-dsh-cli`,g={cli:`tboxcode-dsh`,acp:`tboxcode-dsh-acp`,server:`tboxcode-dsh-server`,web:`tboxcode-dsh-web`,headless:`tboxcode-dsh-headless`};function _(e,t){return[`--title=${e}`,...t]}const v=e(import.meta.url);function y(){let e=v.resolve(`@deepseek-ai/dsh/package.json`),t=JSON.parse(i(e,`utf-8`)).bin?.dsh;if(t===void 0)throw Error(`@deepseek-ai/dsh 包缺 dsh bin 声明`);return l(c(e),t)}function b(e,t){return`tboxcode-dsh ${e}: dsh 非零退出(${t??`signal`})——若上方出现 ERR_MODULE_NOT_FOUND 或插件未激活,多为 profile 半装残缺,运行 tboxcode-dsh init 修复`}function x(e,t=16384){return Buffer.byteLength(e,`utf-8`)<=t?e:`…(前段截断,仅保留尾部 ${t} 字节)\n${e.slice(-t)}`}function S(e){if(process.platform===`win32`){t(`taskkill`,[`/PID`,String(e),`/T`,`/F`],{stdio:`ignore`}).on(`error`,()=>{});return}try{process.kill(-e,`SIGKILL`)}catch(e){if(e.code!==`ESRCH`)throw e}}function C(e){let n=y(),r={...process.env,DSH_HOME:e.dshHome,DSH_TELEMETRY_DISABLED:`1`};return new Promise((i,a)=>{let o=t(process.execPath,_(g.cli,[n,...e.args]),{env:r,stdio:e.capture===!1?`inherit`:[`ignore`,`pipe`,`pipe`],...e.timeoutMs===void 0?{}:{detached:!0}}),s=``,c=``;o.stdout?.on(`data`,e=>{s+=e.toString()}),o.stderr?.on(`data`,e=>{c+=e.toString()});let l;o.on(`error`,e=>{clearTimeout(l),a(e)}),o.on(`close`,e=>{clearTimeout(l),i({code:e??-1,stdout:s,stderr:c})}),e.timeoutMs!==void 0&&(l=setTimeout(()=>{o.pid!==void 0&&S(o.pid),a(Error(`dsh 进程超时(>${e.timeoutMs}ms),进程组已终止\nSTDOUT:\n${x(s)}\nSTDERR:\n${x(c)}`))},e.timeoutMs))})}const w=[`tboxcode-acp`,`tboxcode-server`,`tboxcode-web`,`tboxcode-headless`],T={"tboxcode-acp":[`@deepseek-ai/dsh-cmdline`,`@deepseek-ai/dsh-tool-ask-user`,`@tbox.cn/dsh-acp-full-app`,`@tbox.cn/dsh-acp-full`,`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-presets`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-server":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-web":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-bundle`],"tboxcode-headless":[`@tbox.cn/tboxcode-dsh-llm`,`@tbox.cn/tboxcode-dsh-tools`,`@tbox.cn/tboxcode-dsh-presets`,`@tbox.cn/tboxcode-dsh-bundle`]},E={"tboxcode-acp":[`@deepseek-ai/dsh-base`],"tboxcode-server":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`],"tboxcode-web":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`],"tboxcode-headless":[`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-headless`]},D=new Set([`@deepseek-ai/dsh-base`,`@deepseek-ai/dsh-web-app`,`@deepseek-ai/dsh-headless`]),O={"tboxcode-acp":[`@tbox.cn/dsh-acp-full`],"tboxcode-server":[`@tbox.cn/tboxcode-dsh-presets`],"tboxcode-web":[`@tbox.cn/tboxcode-dsh-presets`],"tboxcode-headless":[]};function k(e){let t=E[e],n=new Set([...O[e],...[...D].filter(e=>!t.includes(e))]),r=T[e].filter(e=>P(e)&&!n.has(e));return{officialRows:t,managedOwnRows:r,managedRows:new Set([...t,...r]),cleanupRows:n}}function A(e,t){return e.managedRows.has(t)||e.cleanupRows.has(t)}function j(e){let t=k(e);return[...t.officialRows,...t.managedOwnRows]}function M(){return process.env.TBOXCODE_DSH_HOME??process.env.DSH_HOME??l(u(),`.dsh`)}function N(e,t){return l(e,`profiles`,t)}function P(e){return e.startsWith(`@tbox.cn/`)}function F(e){let t=l(e,`package.json`);return n(t)?JSON.parse(i(t,`utf-8`)):null}function I(e,t){let r=t.startsWith(`./`)?t.slice(2):t;if(n(l(e,r)))return!0;for(let t of[`.js`,`.json`,`/index.js`,`/index.json`])if(n(l(e,r+t)))return!0;return!1}function L(e){let t=l(e,`node_modules`);if(!n(t))return[];let r=[],o=(e,t)=>{for(let n of a(e,{withFileTypes:!0})){if(n.name.startsWith(`.`)||!n.isDirectory()&&!n.isSymbolicLink())continue;if(n.isDirectory()&&n.name.startsWith(`@`)){o(l(e,n.name),`${t}${n.name}/`);continue}let a=`${t}${n.name}`,s;try{s=JSON.parse(i(l(e,n.name,`package.json`),`utf-8`))}catch{r.push(a);continue}if(typeof s!=`object`||!s)continue;let c=null;if(typeof s.main==`string`&&s.main.length>0)c=s.main;else if(s.exports!==null&&typeof s.exports==`object`){let e=s.exports[`.`],t=typeof e==`string`?e:typeof e==`object`&&e?e.default??e.import??e.require:void 0;typeof t==`string`&&t.length>0&&(c=t)}c!==null&&(I(l(e,n.name),c)||r.push(a))}};return o(t,``),r.sort()}function R(e,t){let r=N(e,t),i=T[t],a=k(t),o=j(t),s=n(r)?F(r):null;if(s===null)return{status:`missing`,profileDir:r,missingPackages:[...i],managedCorruptPackages:[],foreignCorruptPackages:[],forbiddenPackages:[],expectedStructureOrder:o,actualStructureOrder:[],versionMismatches:[]};let c=L(r),l=new Set(i),u=c.filter(e=>l.has(e)),d=c.filter(e=>!l.has(e)),f=s.dsh?.profile?.bundles??[],p=Object.keys(s.dependencies??{}).filter(e=>D.has(e)),m=i.filter(e=>G(r,e)===null),h=f.filter(e=>A(a,e)),g=[],_=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT===void 0?`registry`:`local`,v=_===`registry`?B():``;for(let e of i.filter(P)){let t=W(_,_===`local`?U(e):null,v),n=G(r,e);t!==null&&n!==null&&t!==n&&g.push({pkg:e,expected:t,actual:n})}for(let e of i.filter(e=>!P(e))){let t=z(),n=G(r,e);t!==``&&n!==null&&t!==n&&g.push({pkg:e,expected:t,actual:n})}let y=`ok`;return m.length>0||u.length>0||p.length>0?y=`incomplete`:g.length>0?y=`version-drift`:(h.length!==o.length||h.some((e,t)=>e!==o[t]))&&(y=`drift`),{status:y,profileDir:r,missingPackages:m,managedCorruptPackages:u,foreignCorruptPackages:d,forbiddenPackages:p,expectedStructureOrder:o,actualStructureOrder:h,versionMismatches:g}}function z(){let t=e(import.meta.url).resolve(`@deepseek-ai/dsh/package.json`);return JSON.parse(i(t,`utf-8`)).version??``}function B(){let t=e(import.meta.url).resolve(`../package.json`);return JSON.parse(i(t,`utf-8`)).version??``}function V(e){if(!P(e))return`${e}@${z()}`;let t=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT;if(t===void 0){let t=B();if(t===`0.0.0`)throw Error(`占位版本 0.0.0 不能用于 registry 装配——发布形态先 pnpm release bump;开发 / CI 形态设 TBOXCODE_ASSEMBLY_LOCAL_ROOT 走本地 link: 源`);return`${e}@${t}`}let n=H(t,e);if(n===null)throw Error(`装配安装源未找到包 ${e}(TBOXCODE_ASSEMBLY_LOCAL_ROOT=${t})`);return`link:${n}`}function H(e,t){for(let r of a(e)){let o=l(e,r);if(s(o).isDirectory())for(let e of a(o)){let r=l(o,e),a=l(r,`package.json`);if(!(!s(r).isDirectory()||!n(a))&&JSON.parse(i(a,`utf-8`)).name===t)return r}}return null}function U(e){let t=process.env.TBOXCODE_ASSEMBLY_LOCAL_ROOT;if(t===void 0)return null;let n=H(t,e);return n===null?null:JSON.parse(i(l(n,`package.json`),`utf-8`)).version??null}function W(e,t,n){return e===`local`?t===``?null:t:n===``||n===`0.0.0`?null:n}function G(e,t){let r=l(e,`node_modules`,t,`package.json`);if(!n(r))return null;try{return JSON.parse(i(r,`utf-8`)).version??null}catch{return null}}const K={"node-pty":!0,koffi:!0,"@deepseek-ai/dsh-subprocess-local":!0,"@google/genai":!1,protobufjs:!1};async function q(e){let t=l(e,`pnpm-workspace.yaml`);await p(t,async()=>{if(!n(t)){await m(t,f({packages:[`.`],nodeLinker:`hoisted`,autoInstallPeers:!1,allowBuilds:K}),{mode:420});return}let e=d(i(t,`utf-8`))??{},r=e.allowBuilds??{},a={...K,...r};JSON.stringify(r)!==JSON.stringify(a)&&(e.allowBuilds=a,await m(t,f(e),{mode:420}))})}const J=5e3;function Y(){let e=Number(process.env.TBOXCODE_ASSEMBLY_INSTALL_TIMEOUT_MS);return Number.isFinite(e)&&e>0?e:12e4}function X(){let e=Number(process.env.TBOXCODE_ASSEMBLY_INSTALL_ATTEMPTS);return Number.isInteger(e)&&e>0?e:2}async function Z(e,t,n,r){let i=await C({dshHome:e,args:[`plugin`,`--profile`,t,`add`,n],capture:!0,timeoutMs:Y()});if(i.code!==0)throw Error(`装配${r}失败(${n}):dsh plugin add 退出码 ${i.code}\nSTDOUT:\n${x(i.stdout)}\nSTDERR:\n${x(i.stderr)}`)}async function Q(e,t,n){let r=await C({dshHome:e,args:[`plugin`,`--profile`,t,`remove`,...n],capture:!0,timeoutMs:Y()});if(r.code!==0)throw Error(`官方元包去污染失败(dsh plugin remove ${n.join(` `)}):退出码 ${r.code}\nSTDOUT:\n${x(r.stdout)}\nSTDERR:\n${x(r.stderr)}`)}async function ee(e,t,r={}){let i=N(e,t),a=l(i,`node_modules`),s=l(e,`profiles`,t,`node_modules`);if(!w.includes(t)||a!==s)throw Error(`装配自愈守卫拒绝:目标 ${a} 非 own profile node_modules(期望 ${s})`);return n(a)?await p(l(i,`.tboxcode-assembly-heal.lock`),async()=>{let e=L(i),n=new Set(T[t]);return!e.some(e=>n.has(e))&&r.includeForeign!==!0?!1:(o(a,{recursive:!0,force:!0}),!0)}):!1}async function te(e,t,n,r={}){let i=n===`explicit`?X():1,a=[],o;for(let s=1;s<=i;s++)try{return await ne(e,t,n,a,r)}catch(e){if(o=e,s<i){let t=String(e instanceof Error?e.message:e).split(`
2
- `)[0];a.push({kind:`retry`,detail:`第 ${s} 次尝试失败(${t}),${J/1e3}s 后重试`}),await new Promise(e=>setTimeout(e,J))}}throw o}async function ne(e,t,n,i,a){let o=R(e,t);o.forbiddenPackages.length>0&&(await Q(e,t,o.forbiddenPackages),i.push({kind:`decontaminate`,detail:`官方元包去污染:dsh plugin remove ${o.forbiddenPackages.join(` `)}(元包实体破坏 dsh 单实例——settings 持久化写与热重载全灭;官方基座行转 installation 解析)`}),o=R(e,t));let s=a.repairForeignCorruption===!0&&o.foreignCorruptPackages.length>0;if(o.managedCorruptPackages.length>0||s){let n=[...new Set([...o.managedCorruptPackages,...o.foreignCorruptPackages])];await ee(e,t,{includeForeign:s})&&i.push({kind:`heal`,detail:`半装态自愈${s?`(含 foreign;用户自装插件需自行重装)`:``}:node_modules 残缺 ${n.length} 包(${n.slice(0,3).join(`, `)}${n.length>3?` …`:``})→ nuke 后全链重装`}),o=R(e,t)}else o.foreignCorruptPackages.length>0&&i.push({kind:`warn-foreign-corrupt`,detail:`非受管包残缺(用户插件或传递依赖):${o.foreignCorruptPackages.slice(0,3).join(`, `)}${o.foreignCorruptPackages.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --profile ${t} --repair-all(会重建 node_modules,用户自装插件需自行重装)`});if(o.missingPackages.length>0){r(o.profileDir,{recursive:!0}),await q(o.profileDir);for(let n of o.missingPackages){let r=V(n);await Z(e,t,r,`安装`),i.push({kind:`install`,detail:`${n} ← ${r}`})}o=R(e,t)}if(o.status===`version-drift`&&n===`explicit`){for(let n of o.versionMismatches)await Z(e,t,V(n.pkg),`重装`),i.push({kind:`reinstall`,detail:`${n.pkg} ${n.actual} → ${n.expected}`});o=R(e,t)}return o.status!==`missing`&&(o.actualStructureOrder.length!==o.expectedStructureOrder.length||o.actualStructureOrder.some((e,t)=>e!==o.expectedStructureOrder[t]))&&(await ie(e,t),i.push({kind:`correct-manifest`,detail:`结构行集校正 [${o.actualStructureOrder.join(`,`)}] → [${o.expectedStructureOrder.join(`,`)}](结构行是组合契约,用户领地不动)`}),o=R(e,t)),{status:o.status,actions:i,check:o}}function $(e,t){return e.status===`version-drift`?`${t} 版本偏差(${e.check.versionMismatches.map(e=>`${e.pkg} ${e.actual}→${e.expected}`).join(`, `)})——运行 tboxcode-dsh init --profile ${t} 升级`:null}function re(e,t,n={}){let r=[];if(e.check.foreignCorruptPackages.length>0){let n=e.check.foreignCorruptPackages;r.push(`${t} 非受管包残缺(用户插件或传递依赖):${n.slice(0,3).join(`, `)}${n.length>3?` …`:``}——默认不重建 node_modules;确认需要全量修复时运行 tboxcode-dsh init --profile ${t} --repair-all(会重建 node_modules,用户自装插件需自行重装)`)}if(n.includeVersionDrift!==!1){let n=$(e,t);n!==null&&r.push(n)}return e.check.forbiddenPackages.length>0&&r.push(`${t} 官方元包污染(${e.check.forbiddenPackages.join(`, `)})——运行 tboxcode-dsh init --profile ${t} 去污染`),r}async function ie(e,t){let n=l(N(e,t),`package.json`);await p(n,async()=>{let e=JSON.parse(i(n,`utf-8`)),r=e.dsh?.profile?.bundles??[],a=k(t),o=[...new Set(r.filter(e=>!A(a,e)))],s=[...a.officialRows,...o,...a.managedOwnRows];e.dsh={...e.dsh,profile:{...e.dsh?.profile,bundles:s}},await m(n,`${JSON.stringify(e,null,2)}\n`,{mode:420})})}export{g as _,re as a,z as c,M as d,$ as f,h as g,C as h,E as i,te as l,y as m,T as n,R as o,b as p,O as r,B as s,w as t,N as u,_ as v};