blade-code 0.10.5 → 0.10.7
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/dist/blade.js +2 -2
- package/dist/{chunk-z906d5tb.js → chunk-0n04bmta.js} +2 -2
- package/dist/{chunk-drrdzmaa.js → chunk-0xm8hn2h.js} +2 -2
- package/dist/chunk-0z7q89w7.js +2 -0
- package/dist/{chunk-gxmrakrk.js → chunk-1sw9z975.js} +1 -1
- package/dist/{chunk-948qe2pp.js → chunk-1v7hpr34.js} +1 -1
- package/dist/{chunk-fvhc1yj8.js → chunk-3fdyzgpv.js} +1 -1
- package/dist/{chunk-15d49qty.js → chunk-43j2f1b5.js} +1 -1
- package/dist/{chunk-njyvrtje.js → chunk-4hhkbyja.js} +2 -2
- package/dist/{chunk-6eragc74.js → chunk-4rhssb85.js} +2 -2
- package/dist/chunk-598egka3.js +22 -0
- package/dist/chunk-5ak9tw83.js +100 -0
- package/dist/chunk-738pkjf8.js +1 -0
- package/dist/{chunk-xv0maezy.js → chunk-7a200q5g.js} +2 -2
- package/dist/{chunk-5twefby5.js → chunk-7pkrwh10.js} +1 -1
- package/dist/chunk-7xfmzvxp.js +1 -0
- package/dist/{chunk-9ffr01c3.js → chunk-847e0sk9.js} +1 -1
- package/dist/chunk-867rz2ss.js +1 -0
- package/dist/chunk-a3e97syg.js +3 -0
- package/dist/{chunk-0n97186m.js → chunk-ahxc3n3g.js} +2 -2
- package/dist/{chunk-tq52pbde.js → chunk-akev4wsz.js} +1 -1
- package/dist/{chunk-epj56s0n.js → chunk-az4b6106.js} +1 -1
- package/dist/{chunk-63ysxybd.js → chunk-e9ckf8e7.js} +1 -1
- package/dist/chunk-f7854pbx.js +187 -0
- package/dist/chunk-fwt0kje3.js +1 -0
- package/dist/{chunk-1vx4sf1y.js → chunk-g1q27q6q.js} +2 -2
- package/dist/chunk-g3wgzyhx.js +160 -0
- package/dist/chunk-g4snq0dx.js +1 -0
- package/dist/chunk-ggm04adj.js +1 -0
- package/dist/chunk-hpytxsge.js +1 -0
- package/dist/chunk-htr91q9c.js +1 -0
- package/dist/chunk-hwcp940f.js +1 -0
- package/dist/chunk-j5ysjbyr.js +10 -0
- package/dist/{chunk-jza1rsan.js → chunk-jpy61wna.js} +1 -1
- package/dist/chunk-kqpwz9b7.js +8 -0
- package/dist/{chunk-gxzbv67r.js → chunk-m86ekqp0.js} +2 -2
- package/dist/{chunk-51wtvmzn.js → chunk-m8k1mv0j.js} +1 -1
- package/dist/{chunk-ydtcvxsm.js → chunk-mt4328yf.js} +1 -1
- package/dist/{chunk-35yktzm1.js → chunk-ne7zdjrh.js} +1 -1
- package/dist/{chunk-bexgcc2d.js → chunk-p0j8994h.js} +1 -1
- package/dist/{chunk-gb54hc78.js → chunk-p3ahf1fh.js} +1 -1
- package/dist/chunk-pt659n0v.js +1466 -0
- package/dist/chunk-q1gfeadm.js +1 -0
- package/dist/{chunk-vntzy7bc.js → chunk-qk2z6gag.js} +2 -2
- package/dist/{chunk-ea7wsges.js → chunk-qqwgb9ja.js} +2 -2
- package/dist/{chunk-am8dtvff.js → chunk-qycwavck.js} +72 -17
- package/dist/chunk-rk1w5pry.js +2 -0
- package/dist/{chunk-s645azsm.js → chunk-rn908m3v.js} +2 -2
- package/dist/{chunk-dqvcgwhe.js → chunk-rz2ftt6h.js} +2 -2
- package/dist/{chunk-19kg7yvw.js → chunk-rz9yh6e4.js} +2 -2
- package/dist/{chunk-qm11hjd3.js → chunk-tc83e1b0.js} +1 -1
- package/dist/{chunk-v93hcxe7.js → chunk-tcc3v4n2.js} +1 -1
- package/dist/{chunk-g6h6h8xr.js → chunk-tp9zk241.js} +1 -1
- package/dist/chunk-tzh4ahry.js +6 -0
- package/dist/chunk-v0rn831t.js +1 -0
- package/dist/{chunk-fz1vyc15.js → chunk-vafn0spm.js} +1 -1
- package/dist/{chunk-rh7v814a.js → chunk-vcq624qv.js} +2 -2
- package/dist/chunk-vynsgc2g.js +1 -0
- package/dist/{chunk-qda309d9.js → chunk-w01sec4f.js} +2 -2
- package/dist/chunk-w0ena7fe.js +9 -0
- package/dist/{chunk-6tx8k249.js → chunk-wmdgb5pz.js} +2 -2
- package/dist/{chunk-an96c54z.js → chunk-wrbm6rch.js} +8 -8
- package/dist/{chunk-sanhjqpd.js → chunk-x3780wpy.js} +2 -2
- package/dist/{chunk-yc53wz4r.js → chunk-y573s5r8.js} +2 -2
- package/dist/chunk-ydwjttdz.js +1 -0
- package/dist/{chunk-vpj0sv73.js → chunk-yyvx1avz.js} +3 -3
- package/dist/chunk-z9fsh3kn.js +1 -0
- package/dist/{chunk-ygx03mdh.js → chunk-zjf1z27e.js} +1 -1
- package/dist/web/assets/ChatView-BYfHmxKJ.js +7 -0
- package/dist/web/assets/{CodeBlockHighlighter-CFW6qjnA.js → CodeBlockHighlighter-CC4e9nFc.js} +1 -1
- package/dist/web/assets/{FilePreview-CR_XetY0.js → FilePreview-C3HEoLYS.js} +1 -1
- package/dist/web/assets/{MarkdownRenderer-CVrxJswQ.js → MarkdownRenderer-COycrGUX.js} +3 -3
- package/dist/web/assets/{McpModal-BO-85gll.js → McpModal-CDgr9kVq.js} +1 -1
- package/dist/web/assets/{SettingsModal-CW87OnxI.js → SettingsModal-CvdVIbeG.js} +1 -1
- package/dist/web/assets/{SkillsModal-Q8ZFPhys.js → SkillsModal-lSX1ZKTu.js} +1 -1
- package/dist/web/assets/{TaskSwitcher-BzH-0QXr.js → TaskSwitcher-peKi9KvN.js} +1 -1
- package/dist/web/assets/{TerminalPanel-DTqvx8yB.js → TerminalPanel-DqQ-7CTG.js} +1 -1
- package/dist/web/assets/index-BF5HEN_f.js +32 -0
- package/dist/web/assets/index-BrkTYxtS.css +1 -0
- package/dist/web/index.html +2 -2
- package/package.json +1 -1
- package/dist/chunk-069zpw09.js +0 -6
- package/dist/chunk-1m5r6mp2.js +0 -180
- package/dist/chunk-1q21sxhx.js +0 -1
- package/dist/chunk-1r6szcrj.js +0 -1
- package/dist/chunk-1x4fpe47.js +0 -1
- package/dist/chunk-2nkd1kjx.js +0 -1
- package/dist/chunk-417wefq7.js +0 -99
- package/dist/chunk-4ncbd4k0.js +0 -1
- package/dist/chunk-62881c2s.js +0 -1484
- package/dist/chunk-7443b95r.js +0 -1
- package/dist/chunk-79s052sw.js +0 -2
- package/dist/chunk-7jy60gnx.js +0 -1
- package/dist/chunk-c5tds5v0.js +0 -9
- package/dist/chunk-ccwskwb3.js +0 -1
- package/dist/chunk-f6mszdsd.js +0 -1
- package/dist/chunk-hh85pk77.js +0 -160
- package/dist/chunk-nz78b5c5.js +0 -10
- package/dist/chunk-r1764q6f.js +0 -1
- package/dist/chunk-r6mwyg3h.js +0 -3
- package/dist/chunk-rzzt21ef.js +0 -22
- package/dist/chunk-t8x2qpe2.js +0 -1
- package/dist/chunk-tjnfhm9v.js +0 -1
- package/dist/chunk-v0ygsjry.js +0 -1
- package/dist/chunk-zyz82mcd.js +0 -1
- package/dist/web/assets/ChatView-Y-6k-ayR.js +0 -7
- package/dist/web/assets/index-B7etZcne.css +0 -1
- package/dist/web/assets/index-ClOgGy-I.js +0 -32
|
@@ -1,4 +1,59 @@
|
|
|
1
|
-
import{
|
|
1
|
+
import{nd as J1}from"./chunk-0z7q89w7.js";import{qd as r1,rd as LO}from"./chunk-ne7zdjrh.js";import{ud as Y1}from"./chunk-e9ckf8e7.js";import{vd as B0,xd as M0}from"./chunk-yyvx1avz.js";import{Cd as h,Dd as B}from"./chunk-w01sec4f.js";import{ee as H0,fe as l1,oe as S0}from"./chunk-vcq624qv.js";import{re as u,te as Y,we as C1}from"./chunk-akev4wsz.js";import{Ge as A}from"./chunk-az4b6106.js";import{Te as P0}from"./chunk-g1q27q6q.js";import{Ye as T}from"./chunk-0n04bmta.js";import Z1 from"node:path";import J$ from"node:path";import w0 from"node:fs";import Z0 from"node:os";import R from"node:path";import FO from"yaml";var WO=`# Code Review Agent
|
|
2
|
+
|
|
3
|
+
You are an independent senior code reviewer. Find actionable defects introduced
|
|
4
|
+
by the requested change. Do not implement fixes and do not praise the author.
|
|
5
|
+
|
|
6
|
+
## Security boundary
|
|
7
|
+
|
|
8
|
+
- You are strictly read-only. Never modify files, Git state, configuration, or
|
|
9
|
+
dependencies.
|
|
10
|
+
- Do not use network tools or delegate to another agent.
|
|
11
|
+
- Use Git and file-reading tools to inspect the exact target supplied by the
|
|
12
|
+
host. Do not silently expand the review to unrelated pre-existing code.
|
|
13
|
+
- A command may run only when it is read-only or an existing verification
|
|
14
|
+
command. The runtime enforces this independently of these instructions.
|
|
15
|
+
|
|
16
|
+
## Finding rules
|
|
17
|
+
|
|
18
|
+
Report a finding only when all of these are true:
|
|
19
|
+
|
|
20
|
+
1. It is caused by the reviewed change.
|
|
21
|
+
2. It has a concrete correctness, security, reliability, performance, or
|
|
22
|
+
maintainability impact.
|
|
23
|
+
3. The author can act on it independently.
|
|
24
|
+
4. You can cite the smallest relevant changed line range.
|
|
25
|
+
|
|
26
|
+
Priority:
|
|
27
|
+
|
|
28
|
+
- 0: release blocker or broadly exploitable issue.
|
|
29
|
+
- 1: high-impact defect that should be fixed before merge.
|
|
30
|
+
- 2: normal defect worth fixing.
|
|
31
|
+
- 3: low-impact issue; omit pure style preferences.
|
|
32
|
+
|
|
33
|
+
## Output contract
|
|
34
|
+
|
|
35
|
+
Your final response must be exactly one JSON object and no Markdown fence:
|
|
36
|
+
|
|
37
|
+
{
|
|
38
|
+
"overall_explanation": "Brief assessment grounded in evidence.",
|
|
39
|
+
"findings": [
|
|
40
|
+
{
|
|
41
|
+
"title": "[P1] Imperative title, at most 80 characters",
|
|
42
|
+
"body": "Why this is a defect, the triggering scenario, and impact.",
|
|
43
|
+
"priority": 1,
|
|
44
|
+
"confidence_score": 0.98,
|
|
45
|
+
"code_location": {
|
|
46
|
+
"path": "relative/path/to/file.ts",
|
|
47
|
+
"line_start": 10,
|
|
48
|
+
"line_end": 12
|
|
49
|
+
}
|
|
50
|
+
}
|
|
51
|
+
]
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
Use workspace-relative paths. Keep line ranges within 10 lines and overlapping
|
|
55
|
+
the reviewed diff. Return an empty findings array when no qualifying defect is
|
|
56
|
+
found.`,K1={name:J1,description:"Independent read-only code reviewer for uncommitted changes, base branches, and commits.",tools:["Read","Glob","Grep","Bash"],disallowedTools:["Task","WebFetch","WebSearch"],permissionMode:"default",maxTurns:24,systemPrompt:WO,source:"builtin"};var G1={name:"verification",description:"Independent verification agent that validates implementation by running builds, tests, linters, and adversarial"+" probes. Strictly read-only — cannot modify code. Use"+" after completing implementation to get an independent quality assessment.",tools:["Read","Glob","Grep","Bash"],systemPrompt:`# Verification Agent
|
|
2
57
|
|
|
3
58
|
You are an **independent verification engineer**. Your sole purpose is to find problems — not to praise or reassure. You are the last line of defense before code ships.
|
|
4
59
|
|
|
@@ -156,9 +211,9 @@ You are a software architect specializing in implementation planning.
|
|
|
156
211
|
**Risks**:
|
|
157
212
|
- [Potential issue and mitigation]
|
|
158
213
|
|
|
159
|
-
Be thorough but concise. Focus on actionable steps.`},{name:"statusline-setup",description:"Use this agent to configure the user's Claude Code status line setting.",tools:["Read","Edit"]},
|
|
214
|
+
Be thorough but concise. Focus on actionable steps.`},{name:"statusline-setup",description:"Use this agent to configure the user's Claude Code status line setting.",tools:["Read","Edit"]},K1,G1];var J0=h("Agent"),C0=new Set(["verification"]);class y{workspaceRoot;static instances=new Map;subagents=new Map;constructor(O=A()){this.workspaceRoot=O}static getInstance(O=A()){let $=R.resolve(O),Q=y.instances.get($);if(!Q)Q=new y($),y.instances.set($,Q);return Q}static resetInstances(){y.instances.clear()}register(O){if(C0.has(O.name))throw Error(`Subagent '${O.name}' is reserved by Blade`);if(this.subagents.has(O.name))throw Error(`Subagent '${O.name}' already registered`);this.subagents.set(O.name,O)}applyOverrides(O){for(let $ of O){if(C0.has($.name))throw Error(`Subagent '${$.name}' is reserved by Blade`);this.subagents.set($.name,$)}}getSubagent(O){return this.subagents.get(O)}getAllNames(){return Array.from(this.subagents.keys())}getAllSubagents(){return Array.from(this.subagents.values())}getDescriptionsForPrompt(){let O=this.getAllSubagents();if(O.length===0)return"No subagents available.";return`Available agent types and the tools they have access to:
|
|
160
215
|
${O.map((Q)=>{let q=!Q.tools||Q.tools.length===0?"All tools":Q.tools.join(", ");return`- ${Q.name}: ${Q.description} (Tools: ${q})`}).join(`
|
|
161
|
-
`)}`}loadFromDirectory(O,$){if(!w0.existsSync(O))return;let Q=w0.readdirSync(O);for(let q of Q){if(!q.endsWith(".md"))continue;let Z=R.join(O,q);try{let J=this.parseConfigFile(Z,$);if(C0.has(J.name)){J0.warn(`Skipping reserved subagent configuration: ${J.name}`);continue}this.subagents.set(J.name,J)}catch(J){J0.warn(`Failed to load subagent config from ${Z}:`,J)}}}parseConfigFile(O,$){let q=w0.readFileSync(O,"utf-8").match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/);if(!q)throw Error(`No YAML frontmatter found in ${O}`);let[,Z,J]=q,K=
|
|
216
|
+
`)}`}loadFromDirectory(O,$){if(!w0.existsSync(O))return;let Q=w0.readdirSync(O);for(let q of Q){if(!q.endsWith(".md"))continue;let Z=R.join(O,q);try{let J=this.parseConfigFile(Z,$);if(C0.has(J.name)){J0.warn(`Skipping reserved subagent configuration: ${J.name}`);continue}this.subagents.set(J.name,J)}catch(J){J0.warn(`Failed to load subagent config from ${Z}:`,J)}}}parseConfigFile(O,$){let q=w0.readFileSync(O,"utf-8").match(/^---\r?\n([\s\S]*?)\r?\n---\r?\n([\s\S]*)$/);if(!q)throw Error(`No YAML frontmatter found in ${O}`);let[,Z,J]=q,K=FO.parse(Z);if(!K.name||!K.description)throw Error(`Missing required fields (name, description) in ${O}`);if(K.isolation!==void 0&&K.isolation!=="none"&&K.isolation!=="worktree")throw Error(`Invalid isolation mode "${String(K.isolation)}" in ${O}`);let L=J.trim(),G=this.parseStringOrArray(K.tools),X=this.parseStringOrArray(K.skills),W=Y1(K.permissionMode);return{name:K.name,description:K.description,systemPrompt:L,tools:G,color:K.color,configPath:O,model:K.model||"inherit",permissionMode:W,skills:X,isolation:K.isolation,source:$}}parseStringOrArray(O){if(!O)return;if(Array.isArray(O))return O.map(($)=>$.trim()).filter(Boolean);return O.split(",").map(($)=>$.trim()).filter(Boolean)}loadFromStandardLocations(O){let $=O??T.getInstance().isTrustedCached(this.workspaceRoot);this.loadBuiltinAgents();let Q=R.join(Z0.homedir(),".claude","agents");if(this.loadFromDirectory(Q,"claude-code-user"),$){let J=R.join(this.workspaceRoot,".claude","agents");this.loadFromDirectory(J,"claude-code-project")}let q=R.join(Z0.homedir(),".blade","agents");if(this.loadFromDirectory(q,"blade-user"),$){let J=R.join(this.workspaceRoot,".blade","agents");this.loadFromDirectory(J,"blade-project")}let Z=this.getAllNames().length;return J0.debug(`Loaded ${Z} subagents from standard locations`),Z}loadBuiltinAgents(){for(let O of T0)this.subagents.set(O.name,{...O,model:O.model||"inherit",source:"builtin"});J0.debug(`Loaded ${T0.length} builtin subagents`)}clear(){this.subagents.clear()}snapshot(){let O=new y(this.workspaceRoot);for(let $ of this.subagents.values())O.subagents.set($.name,{...$,...$.tools?{tools:[...$.tools]}:{},...$.disallowedTools?{disallowedTools:[...$.disallowedTools]}:{},...$.skills?{skills:[...$.skills]}:{}});return O}getSubagentsBySource(){let O={builtin:[],"claude-code-user":[],"claude-code-project":[],"blade-user":[],"blade-project":[],flag:[],plugin:[]};for(let $ of this.subagents.values()){let Q=$.source||"builtin",q=Q.startsWith("plugin:")?"plugin":Q;O[q].push($)}return O}clearPluginAgents(){let O=[];for(let[$,Q]of this.subagents.entries())if(Q.source?.startsWith("plugin:"))O.push($);for(let $ of O)this.subagents.delete($)}static getClaudeCodeAgentsDir(O){if(O==="user")return R.join(Z0.homedir(),".claude","agents");return R.join(A(),".claude","agents")}static getBladeAgentsDir(O){if(O==="user")return R.join(Z0.homedir(),".blade","agents");return R.join(A(),".blade","agents")}}function i(O=A()){return y.getInstance(O)}var r$=new Proxy({},{get(O,$){let Q=i(),q=Reflect.get(Q,$,Q);return typeof q==="function"?q.bind(Q):q}});import*as W0 from"node:fs/promises";import{homedir as B1}from"node:os";import*as _ from"node:path";var K0={name:"skill-creator",description:"Create new Skills interactively. Use when the user wants to create a new Skill, define a custom workflow, or add a specialized capability to Blade.",allowedTools:["Read","Write","Glob","Bash","AskUserQuestion"],version:"1.0.0",argumentHint:void 0,userInvocable:!0,disableModelInvocation:!1,model:void 0,whenToUse:"User wants to create a new skill, define a custom workflow, or add a specialized capability.",path:"builtin://skill-creator",basePath:"",source:"builtin"};function L1(){return{metadata:K0,instructions:`# Skill Creator
|
|
162
217
|
|
|
163
218
|
帮助用户创建新的 Blade Skills。
|
|
164
219
|
|
|
@@ -341,7 +396,7 @@ user-invocable: true
|
|
|
341
396
|
<footer>
|
|
342
397
|
\`\`\`
|
|
343
398
|
\`\`\`
|
|
344
|
-
`}}var G0={name:"update-config",description:"配置 Blade harness(settings/hooks/permissions)。"+"用户请求自动化行为时使用此 Skill。",allowedTools:["ConfigTool","Read","Bash","AskUserQuestion"],version:"1.0.0",userInvocable:!0,disableModelInvocation:!1,whenToUse:'用户说"从现在起..."、"每次..."、"当...时..."、'+"需要改配置、安装 hooks、修改权限",path:"builtin://update-config",basePath:"",source:"builtin"};function
|
|
399
|
+
`}}var G0={name:"update-config",description:"配置 Blade harness(settings/hooks/permissions)。"+"用户请求自动化行为时使用此 Skill。",allowedTools:["ConfigTool","Read","Bash","AskUserQuestion"],version:"1.0.0",userInvocable:!0,disableModelInvocation:!1,whenToUse:'用户说"从现在起..."、"每次..."、"当...时..."、'+"需要改配置、安装 hooks、修改权限",path:"builtin://update-config",basePath:"",source:"builtin"};function X1(){return{metadata:G0,instructions:`# Update Config Skill
|
|
345
400
|
|
|
346
401
|
配置 Blade 运行环境。管理 settings、hooks、permissions、环境变量等。
|
|
347
402
|
|
|
@@ -614,23 +669,23 @@ ConfigTool({
|
|
|
614
669
|
2. **permissions 字段使用 replace 策略**:设置 permissions 会完全替换现有值。如果只想添加规则,先 GET 当前值,合并后再 SET
|
|
615
670
|
3. **env 字段使用 deep-merge 策略**:可以逐个添加环境变量
|
|
616
671
|
4. **scope 默认值**:不指定 scope 时,根据字段路由表决定(hooks/permissions/env 默认 local,temperature/theme 等默认 global)
|
|
617
|
-
`}}import{exec as
|
|
618
|
-
`)}get size(){return this.skills.size}async refresh(){return this.skills.clear(),this.pluginSkills.clear(),this.initialized=!1,this.initialize()}snapshot(){let O=new k({...this.config});return O.skills=new Map(Array.from(this.skills,([$,Q])=>[$,{...Q,...Q.allowedTools?{allowedTools:[...Q.allowedTools]}:{}}])),O.pluginSkills=new Map(Array.from(this.pluginSkills,([$,Q])=>[$,{...Q,metadata:{...Q.metadata,...Q.metadata.allowedTools?{allowedTools:[...Q.metadata.allowedTools]}:{}}}])),O.initialized=this.initialized,O}registerPluginSkill(O){this.pluginSkills.set(O.namespacedName,O),this.skills.set(O.namespacedName,O.metadata)}findPluginSkill(O){let $=this.pluginSkills.get(O);if($)return $;let Q=[];for(let q of this.pluginSkills.values())if(q.originalName===O)Q.push(q);if(Q.length===1)return Q[0];return}getAllPluginSkills(){return Array.from(this.pluginSkills.values())}clearPluginSkills(){for(let O of this.pluginSkills.values())this.skills.delete(O.namespacedName);this.pluginSkills.clear()}getPluginSkillCount(){return this.pluginSkills.size}}function P(O){return k.getInstance(O)}async function
|
|
619
|
-
${
|
|
620
|
-
\`\`\``;Z=Z.replace(J,
|
|
672
|
+
`}}import{exec as HO}from"node:child_process";import*as U from"node:fs/promises";import{homedir as VO}from"node:os";import*as x from"node:path";import{promisify as UO}from"node:util";var Y0=UO(HO),I=h("General"),N0={url:"https://github.com/anthropics/skills.git",branch:"main",defaultSkills:["skill-creator"]};class W1{skillsDir;constructor(O){this.skillsDir=O||x.join(VO(),".blade","skills")}async isInstalled(O){let $=x.join(this.skillsDir,O,"SKILL.md");try{return await U.access($),!0}catch{return!1}}async isGitAvailable(){try{return await Y0("git --version"),!0}catch{return!1}}async installOfficialSkill(O){let{url:$,branch:Q}=N0,q=x.join(this.skillsDir,O),Z=x.join(this.skillsDir,`.tmp-${O}-${Date.now()}`);try{if(!await this.isGitAvailable())return I.warn("Git not available, skipping skill installation"),!1;I.info(`Installing official skill: ${O}...`),await U.mkdir(this.skillsDir,{recursive:!0,mode:493}),await Y0(`git clone --depth 1 --branch ${Q} --single-branch ${$} "${Z}"`,{timeout:30000});let J=x.join(Z,"skills",O);try{await U.access(J)}catch{return I.warn(`Skill ${O} not found in official repository`),await U.rm(Z,{recursive:!0,force:!0}),!1}try{await U.rm(q,{recursive:!0,force:!0})}catch{}return await U.cp(J,q,{recursive:!0}),await U.rm(Z,{recursive:!0,force:!0}),I.info(`Successfully installed: ${O}`),!0}catch(J){try{await U.rm(Z,{recursive:!0,force:!0})}catch{}return I.warn(`Failed to install ${O}: ${J instanceof Error?J.message:"Unknown error"}`),!1}}async ensureDefaultSkillsInstalled(){await U.mkdir(this.skillsDir,{recursive:!0,mode:493});for(let O of N0.defaultSkills)if(!await this.isInstalled(O))await this.installOfficialSkill(O)}async installFromRepo(O,$){let Q=$||this.extractRepoName(O),q=x.join(this.skillsDir,Q),Z=x.join(this.skillsDir,`.tmp-repo-${Q}-${Date.now()}`);try{if(!await this.isGitAvailable())return I.warn("Git not available, cannot install from repo"),!1;I.info(`Installing skill from repo: ${O}...`),await U.mkdir(this.skillsDir,{recursive:!0,mode:493}),await Y0(`git clone --depth 1 "${O}" "${Z}"`,{timeout:60000});let J=x.join(Z,"SKILL.md");try{await U.access(J)}catch{return I.warn(`No SKILL.md found in repository ${O}`),await U.rm(Z,{recursive:!0,force:!0}),!1}await U.rm(q,{recursive:!0,force:!0}),await U.rename(Z,q);try{await U.rm(x.join(q,".git"),{recursive:!0,force:!0})}catch{}return I.info(`Successfully installed skill from repo: ${Q}`),!0}catch(J){try{await U.rm(Z,{recursive:!0,force:!0})}catch{}return I.warn(`Failed to install from repo ${O}: ${J instanceof Error?J.message:"Unknown error"}`),!1}}async installFromLocal(O,$,Q=!0){let q=$||x.basename(O),Z=x.join(this.skillsDir,q);try{let J=x.resolve(O);try{await U.access(J)}catch{return I.warn(`Local path does not exist: ${J}`),!1}let K=x.join(J,"SKILL.md");try{await U.access(K)}catch{return I.warn(`No SKILL.md found in local path: ${J}`),!1}if(I.info(`Installing skill from local path: ${J}...`),await U.mkdir(this.skillsDir,{recursive:!0,mode:493}),await U.rm(Z,{recursive:!0,force:!0}),Q)await U.symlink(J,Z,"dir"),I.info(`Created symlink for skill: ${q}`);else await U.cp(J,Z,{recursive:!0}),I.info(`Copied skill to: ${q}`);return!0}catch(J){return I.warn(`Failed to install from local path ${O}: ${J instanceof Error?J.message:"Unknown error"}`),!1}}extractRepoName(O){return O.match(/\/([^/]+?)(\.git)?$/)?.[1]||"unknown-skill"}async installAllOfficialSkills(){let{url:O,branch:$}=N0,Q=x.join(this.skillsDir,`.tmp-all-${Date.now()}`),q=[],Z=[];try{if(!await this.isGitAvailable())return I.warn("Git not available, skipping skill installation"),{installed:q,failed:Z};await U.mkdir(this.skillsDir,{recursive:!0,mode:493}),I.info("Cloning official skills repository..."),await Y0(`git clone --depth 1 --branch ${$} --single-branch ${O} "${Q}"`,{timeout:60000});let J=x.join(Q,"skills"),K=await U.readdir(J,{withFileTypes:!0});for(let L of K){if(!L.isDirectory())continue;let G=L.name,X=x.join(J,G),W=x.join(this.skillsDir,G);try{await U.access(x.join(X,"SKILL.md")),await U.rm(W,{recursive:!0,force:!0}),await U.cp(X,W,{recursive:!0}),I.info(`Installed: ${G}`),q.push(G)}catch(V){I.warn(`Failed to install ${G}`),Z.push(G)}}await U.rm(Q,{recursive:!0,force:!0})}catch(J){try{await U.rm(Q,{recursive:!0,force:!0})}catch{}I.warn(`Failed to install skills: ${J instanceof Error?J.message:"Unknown error"}`)}return{installed:q,failed:Z}}}var R0=null;function F1(O){if(!R0)R0=new W1(O);return R0}import*as t from"node:fs/promises";import*as L0 from"node:path";import{parse as SO}from"yaml";var jO=/^---\r?\n([\s\S]*?)\r?\n---\r?\n?([\s\S]*)$/,BO=/^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]?$/,H1=1024;function MO(O){if(!O)return;if(typeof O==="string")return O.split(",").map(($)=>$.trim()).filter(Boolean);if(Array.isArray(O))return O.map(($)=>String($).trim()).filter(Boolean);return}function V1(O){if(O===void 0)return;if(typeof O==="boolean")return O;if(typeof O==="string"){let $=O.toLowerCase().trim();if($==="true"||$==="yes"||$==="1")return!0;if($==="false"||$==="no"||$==="0")return!1}return}function zO(O,$){if(!O.name)return{valid:!1,error:"Missing required field: name"};if(!BO.test(O.name))return{valid:!1,error:`Invalid name "${O.name}": must be lowercase letters, numbers, and hyphens only, 1-64 characters`};if(!O.description)return{valid:!1,error:"Missing required field: description"};if(O.description.length>H1)return{valid:!1,error:`Description too long: ${O.description.length} characters (max ${H1})`};let Q;if(O.model)Q=O.model==="inherit"?"inherit":O.model;return{valid:!0,metadata:{name:O.name,description:O.description.trim(),allowedTools:MO(O["allowed-tools"]),version:O.version,argumentHint:O["argument-hint"]?.trim(),userInvocable:V1(O["user-invocable"]),disableModelInvocation:V1(O["disable-model-invocation"]),model:Q,whenToUse:O.when_to_use?.trim()}}}function U1(O,$,Q){let q=O.match(jO);if(!q)return{success:!1,error:"Invalid SKILL.md format: missing YAML frontmatter (must start with ---)"};let[,Z,J]=q,K;try{K=SO(Z)}catch(X){return{success:!1,error:`Failed to parse YAML frontmatter: ${X instanceof Error?X.message:String(X)}`}}let L=zO(K,$);if(!L.valid)return{success:!1,error:L.error};let G=L0.dirname($);return{success:!0,content:{metadata:{...L.metadata,path:$,basePath:G,source:Q},instructions:J.trim()}}}async function X0(O,$){try{let Q=await t.readFile(O,"utf-8");return U1(Q,O,$)}catch(Q){if(Q.code==="ENOENT")return{success:!1,error:`File not found: ${O}`};return{success:!1,error:`Failed to read file: ${Q instanceof Error?Q.message:String(Q)}`}}}async function S1(O){try{let $=await t.readFile(O.path,"utf-8"),Q=U1($,O.path,O.source);return Q.success?Q.content:null}catch{return null}}async function j1(O){try{return await t.access(L0.join(O,"SKILL.md")),!0}catch{return!1}}var IO={userSkillsDir:_.join(B1(),".blade","skills"),projectSkillsDir:".blade/skills",claudeUserSkillsDir:_.join(B1(),".claude","skills"),claudeProjectSkillsDir:".claude/skills"},v0=new Map;function xO(O){return _.resolve(O?.cwd??A())}class k{skills=new Map;pluginSkills=new Map;config;initialized=!1;constructor(O){this.config={...IO,cwd:A(),...O}}static getInstance(O){let $=xO(O),Q=v0.get($);if(!Q)Q=new k({...O,cwd:_.resolve(O?.cwd??A())}),v0.set($,Q);return Q}static resetInstance(){v0.clear()}async initialize(){if(this.initialized)return{skills:Array.from(this.skills.values()),errors:[]};let O=[],$=[];try{await F1(this.config.userSkillsDir).ensureDefaultSkillsInstalled()}catch(J){O.push({path:"SkillInstaller",error:`Failed to install default skills: ${J instanceof Error?J.message:"Unknown error"}`})}this.loadBuiltinSkills();let Q=await this.scanDirectory(this.config.claudeUserSkillsDir,"user");$.push(...Q.skills),O.push(...Q.errors);let q=await this.scanDirectory(this.config.userSkillsDir,"user");if($.push(...q.skills),O.push(...q.errors),(await T.getInstance().getStatus(this.config.cwd)).state==="trusted"){let J=_.isAbsolute(this.config.claudeProjectSkillsDir)?this.config.claudeProjectSkillsDir:_.join(this.config.cwd,this.config.claudeProjectSkillsDir),K=await this.scanDirectory(J,"project");$.push(...K.skills),O.push(...K.errors);let L=_.isAbsolute(this.config.projectSkillsDir)?this.config.projectSkillsDir:_.join(this.config.cwd,this.config.projectSkillsDir),G=await this.scanDirectory(L,"project");$.push(...G.skills),O.push(...G.errors)}for(let J of $)this.skills.set(J.name,J);return this.initialized=!0,{skills:Array.from(this.skills.values()),errors:O}}loadBuiltinSkills(){this.skills.set(K0.name,K0),this.skills.set(G0.name,G0)}async scanDirectory(O,$){let Q=[],q=[];try{await W0.access(O)}catch{return{skills:Q,errors:q}}try{let Z=await W0.readdir(O,{withFileTypes:!0});for(let J of Z){if(!J.isDirectory())continue;let K=_.join(O,J.name),L=_.join(K,"SKILL.md");if(!await j1(K))continue;let G=await X0(L,$);if(G.success&&G.content)Q.push(G.content.metadata);else q.push({path:L,error:G.error||"Unknown error"})}}catch(Z){q.push({path:O,error:`Failed to scan directory: ${Z instanceof Error?Z.message:String(Z)}`})}return{skills:Q,errors:q}}getAll(){return Array.from(this.skills.values())}get(O){return this.skills.get(O)}has(O){return this.skills.has(O)}async loadContent(O){let $=this.skills.get(O);if(!$)return null;if($.source==="builtin")return this.loadBuiltinContent(O);return S1($)}loadBuiltinContent(O){switch(O){case"skill-creator":return L1();case"update-config":return X1();default:return null}}getModelInvocableSkills(){return Array.from(this.skills.values()).filter((O)=>!O.disableModelInvocation)}getUserInvocableSkills(){return Array.from(this.skills.values()).filter((O)=>O.userInvocable===!0)}generateAvailableSkillsList(){let O=this.getModelInvocableSkills();if(O.length===0)return"";let $=[];for(let Q of O){let q=Q.description.length>100?`${Q.description.substring(0,97)}...`:Q.description,Z=Q.argumentHint?`${Q.name} ${Q.argumentHint}`:Q.name;$.push(`- ${Z}: ${q}`)}return $.join(`
|
|
673
|
+
`)}get size(){return this.skills.size}async refresh(){return this.skills.clear(),this.pluginSkills.clear(),this.initialized=!1,this.initialize()}snapshot(){let O=new k({...this.config});return O.skills=new Map(Array.from(this.skills,([$,Q])=>[$,{...Q,...Q.allowedTools?{allowedTools:[...Q.allowedTools]}:{}}])),O.pluginSkills=new Map(Array.from(this.pluginSkills,([$,Q])=>[$,{...Q,metadata:{...Q.metadata,...Q.metadata.allowedTools?{allowedTools:[...Q.metadata.allowedTools]}:{}}}])),O.initialized=this.initialized,O}registerPluginSkill(O){this.pluginSkills.set(O.namespacedName,O),this.skills.set(O.namespacedName,O.metadata)}findPluginSkill(O){let $=this.pluginSkills.get(O);if($)return $;let Q=[];for(let q of this.pluginSkills.values())if(q.originalName===O)Q.push(q);if(Q.length===1)return Q[0];return}getAllPluginSkills(){return Array.from(this.pluginSkills.values())}clearPluginSkills(){for(let O of this.pluginSkills.values())this.skills.delete(O.namespacedName);this.pluginSkills.clear()}getPluginSkillCount(){return this.pluginSkills.size}}function P(O){return k.getInstance(O)}async function AO(O){return P(O).initialize()}import wO from"node:path";import{execSync as bO}from"node:child_process";import M1 from"node:fs";import z1 from"node:path";class y0{async execute(O,$){let Q=O.content;if($.signal?.aborted)throw Error("Command execution aborted");return Q=this.interpolateArgs(Q,$.args),Q=await this.executeBashEmbeds(Q,$),Q=await this.resolveFileReferences(Q,$.workspaceRoot),Q}interpolateArgs(O,$){O=O.replace(/\$ARGUMENTS/g,$.join(" "));for(let Q=9;Q>=1;Q--){let q=`$${Q}`,Z=$[Q-1]??"";O=O.split(q).join(Z)}return O}async executeBashEmbeds(O,$){let Q=/!`([^`]+)`/g,q=[];for(let J of O.matchAll(Q))q.push({match:J[0],command:J[1]});let Z=O;for(let{match:J,command:K}of q){if($.signal?.aborted){Z=Z.replace(J,"[Execution aborted]");continue}try{let L=bO(K,{cwd:$.workspaceRoot,encoding:"utf-8",timeout:30000,maxBuffer:1048576,stdio:["pipe","pipe","pipe"]}).trim();Z=Z.replace(J,L)}catch(L){let G=L instanceof Error?L.message:String(L);Z=Z.replace(J,`[Error executing '${K}': ${G}]`)}}return Z}async resolveFileReferences(O,$){let Q=/@([\w./-]+(?:\/[\w./-]+|\.[\w]+))/g,q=[];for(let J of O.matchAll(Q))q.push({match:J[0],relativePath:J[1]});let Z=O;for(let{match:J,relativePath:K}of q){let L=z1.resolve($,K);try{if(M1.statSync(L).isFile()){let X=M1.readFileSync(L,"utf-8"),V=`\`\`\`${z1.extname(K).slice(1)||"text"}
|
|
674
|
+
${X}
|
|
675
|
+
\`\`\``;Z=Z.replace(J,V)}}catch{}}return Z}hasDynamicContent(O){return{hasArgs:/\$ARGUMENTS|\$\d/.test(O),hasBashEmbeds:/!`[^`]+`/.test(O),hasFileRefs:/@[\w./-]+(?:\/[\w./-]+|\.[\w]+)/.test(O)}}}import f0 from"node:fs";import x1 from"node:os";import E from"node:path";import _O from"node:fs";import I1 from"node:path";import TO from"gray-matter";class E0{parse(O,$,Q,q){try{let Z=_O.readFileSync(O,"utf-8"),{data:J,content:K}=TO(Z),{name:L,namespace:G}=this.extractNameAndNamespace(O,$);if(!L)return null;return{name:L,namespace:G,config:this.normalizeConfig(J),content:K.trim(),path:O,source:Q,sourceDir:q}}catch(Z){return console.error(`Failed to parse command file: ${O}`,Z),null}}normalizeConfig(O){return{description:this.asString(O.description),allowedTools:this.parseAllowedTools(O["allowed-tools"]),argumentHint:this.asString(O["argument-hint"]),model:this.asString(O.model),disableModelInvocation:O["disable-model-invocation"]===!0}}asString(O){if(typeof O==="string"&&O.trim())return O.trim();return}parseAllowedTools(O){if(!O)return;if(Array.isArray(O))return O.map(($)=>String($).trim()).filter(Boolean);if(typeof O==="string")return O.split(",").map(($)=>$.trim()).filter(Boolean);return}extractNameAndNamespace(O,$){let q=I1.relative($,O).split(I1.sep),Z=q.pop();if(!Z)return{name:""};let J=Z.replace(/\.md$/i,""),K=q.length>0?q.join("/"):void 0;return{name:J,namespace:K}}validateConfig(O){let $=[];if(O.model&&!this.isValidModelId(O.model))$.push(`Invalid model ID: ${O.model}`);return $}isValidModelId(O){return O.length>0&&O.length<200}}class h0{parser=new E0;async discover(O){let $=[],Q=[],q=[],Z=this.getSearchDirs(O);for(let J of Z){if(!f0.existsSync(J.path))continue;Q.push(J.path);try{let K=await this.scanDirectory(J.path);for(let L of K)try{let G=this.parser.parse(L,J.path,J.source,J.sourceDir);if(G)$.push(G)}catch(G){q.push({path:L,error:G instanceof Error?G.message:String(G)})}}catch(K){q.push({path:J.path,error:K instanceof Error?K.message:String(K)})}}return{commands:$,scannedDirs:Q,errors:q}}getSearchDirs(O){let $=x1.homedir();return[{path:E.join($,".blade","commands"),source:"user",sourceDir:"blade"},{path:E.join($,".claude","commands"),source:"user",sourceDir:"claude"},{path:E.join(O,".blade","commands"),source:"project",sourceDir:"blade"},{path:E.join(O,".claude","commands"),source:"project",sourceDir:"claude"}]}async scanDirectory(O){let $=[],Q=async(q)=>{let Z=await f0.promises.readdir(q,{withFileTypes:!0});for(let J of Z){let K=E.join(q,J.name);if(J.isDirectory())await Q(K);else if(J.isFile()&&J.name.endsWith(".md"))$.push(K)}};return await Q(O),$}async hasCommands(O){let $=this.getSearchDirs(O);for(let Q of $){if(!f0.existsSync(Q.path))continue;if((await this.scanDirectory(Q.path)).length>0)return!0}return!1}getCommandDirs(O){let $=x1.homedir();return{projectBlade:E.join(O,".blade","commands"),projectClaude:E.join(O,".claude","commands"),userBlade:E.join($,".blade","commands"),userClaude:E.join($,".claude","commands")}}}class w{static instances=new Map;commands=new Map;pluginCommands=new Map;loader=new h0;executor=new y0;initialized=!1;workspaceRoot="";lastDiscoveryResult=null;static getInstance(O=A()){let $=wO.resolve(O),Q=w.instances.get($);if(!Q)Q=new w,w.instances.set($,Q);return Q}static resetInstance(){w.instances.clear()}constructor(){}async initialize(O){this.workspaceRoot=O;let $=await this.loader.discover(O),Q=(await T.getInstance().getStatus(O)).state==="trusted",q={...$,commands:$.commands.filter((Z)=>Z.source!=="project"||Q)};this.lastDiscoveryResult=q,this.commands.clear();for(let Z of q.commands)this.commands.set(Z.name,Z);return this.initialized=!0,q}async refresh(){if(!this.workspaceRoot)throw Error("Registry not initialized. Call initialize() first.");return this.initialize(this.workspaceRoot)}isInitialized(){return this.initialized}getWorkspaceRoot(){return this.workspaceRoot}getCommand(O){return this.commands.get(O)}hasCommand(O){return this.commands.has(O)}getAllCommands(){return Array.from(this.commands.values())}getCommandCount(){return this.commands.size}getModelInvocableCommands(){return this.getAllCommands().filter((O)=>O.config.description&&!O.config.disableModelInvocation)}getModelInvocablePluginCommands(){return this.getAllPluginCommands().filter((O)=>O.config.description&&!O.config.disableModelInvocation)}async executeCommand(O,$){let Q=this.getCommand(O);if(!Q)return null;return this.executor.execute(Q,$)}getCommandLabel(O){let $=O.source==="project"?"project":"user";if(O.namespace)return`(${$}:${O.namespace})`;return`(${$})`}getCommandDisplayName(O){let $=[`/${O.name}`];if(O.config.argumentHint)$.push(O.config.argumentHint);if(O.config.description)$.push("-",O.config.description);return $.push(this.getCommandLabel(O)),$.join(" ")}getCommandsBySource(){let O=[],$=[];for(let Q of this.commands.values())if(Q.source==="project")O.push(Q);else $.push(Q);return{project:O,user:$}}getLastDiscoveryResult(){return this.lastDiscoveryResult}getCommandDirs(){if(!this.workspaceRoot)return null;return this.loader.getCommandDirs(this.workspaceRoot)}generateCommandListDescription(O=15000){let $=[...this.getModelInvocableCommands().map((K)=>({name:K.name,description:K.config.description,argumentHint:K.config.argumentHint,label:this.getCommandLabel(K)})),...this.getModelInvocablePluginCommands().map((K)=>({name:K.namespacedName,description:K.config.description,argumentHint:K.config.argumentHint,label:`(plugin:${K.pluginName})`}))],Q=$.length;if(Q===0)return{text:"No custom commands available.",includedCount:0,totalCount:0};let q=`Available custom commands:
|
|
621
676
|
|
|
622
677
|
`,Z=q.length,J=0;for(let K of $){let L=K.argumentHint?` ${K.argumentHint}`:"",G=`- /${K.name}${L}: ${K.description} ${K.label}
|
|
623
678
|
`;if(Z+G.length>O)break;q+=G,Z+=G.length,J++}if(J<Q)q+=`
|
|
624
|
-
(${J} of ${Q} commands shown due to character budget)`;return{text:q,includedCount:J,totalCount:Q}}snapshot(){let O=new w;return O.workspaceRoot=this.workspaceRoot,O.commands=new Map(Array.from(this.commands,([$,Q])=>[$,{...Q,config:{...Q.config,...Q.config.allowedTools?{allowedTools:[...Q.config.allowedTools]}:{}}}])),O.pluginCommands=new Map(Array.from(this.pluginCommands,([$,Q])=>[$,{...Q,config:{...Q.config,...Q.config.allowedTools?{allowedTools:[...Q.config.allowedTools]}:{}}}])),O.initialized=this.initialized,O.lastDiscoveryResult=this.lastDiscoveryResult?{commands:Array.from(O.commands.values()),scannedDirs:[...this.lastDiscoveryResult.scannedDirs],errors:this.lastDiscoveryResult.errors.map(($)=>({...$}))}:null,O}registerPluginCommand(O){this.pluginCommands.set(O.namespacedName,O)}findPluginCommand(O){let $=this.pluginCommands.get(O);if($)return $;let Q=[];for(let q of this.pluginCommands.values())if(q.originalName===O)Q.push(q);if(Q.length===1)return Q[0];return}hasPluginCommand(O){return this.findPluginCommand(O)!==void 0}getAllPluginCommands(){return Array.from(this.pluginCommands.values())}async executePluginCommand(O,$){let Q=this.findPluginCommand(O);if(!Q)return null;let q={name:Q.namespacedName,config:Q.config,content:Q.content,path:Q.path,source:"project",sourceDir:"blade"};return this.executor.execute(q,$)}clearPluginCommands(){this.pluginCommands.clear()}getPluginCommandCount(){return this.pluginCommands.size}hasPluginCommandConflict(O){let $=0;for(let Q of this.pluginCommands.values())if(Q.originalName===O){if($++,$>1)return!0}return!1}getPluginCommandProviders(O){let $=[];for(let Q of this.pluginCommands.values())if(Q.originalName===O)$.push(Q.pluginName);return $}}import $$ from"node:path";import{satisfies as k0,validRange as z1}from"semver";var _O=/^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$|^[a-z0-9]$/;function d0(O){if(O.bladeVersion!==void 0&&(!O.bladeVersion.trim()||!z1(O.bladeVersion)))throw Error(`Invalid Blade version range in plugin "${O.name}": ${O.bladeVersion}`);for(let[$,Q]of Object.entries(O.dependencies??{})){if(!_O.test($))throw Error(`Invalid dependency name in plugin "${O.name}": ${$}`);if($===O.name)throw Error(`Plugin "${O.name}" cannot depend on itself`);if(!Q.trim()||!z1(Q))throw Error(`Invalid dependency range for ${$} in plugin "${O.name}": ${Q}`)}}function x1(O,$=P0()){if(d0(O),O.bladeVersion&&!k0($,O.bladeVersion))throw Error(`Plugin "${O.name}" requires Blade ${O.bladeVersion}, current ${$}`)}function A1(O,$){O.status="error",O.compatibilityIssues=[...O.compatibilityIssues??[],$],O.error=O.compatibilityIssues.map((Q)=>Q.message).join("; ")}function b1(O,$=P0()){let Q=[],q=new Map(O.map((J)=>[J.manifest.name,J]));for(let J of O){if(J.status!=="active")continue;if(J.manifest.bladeVersion&&!k0($,J.manifest.bladeVersion)){let K={code:"blade-version",expected:J.manifest.bladeVersion,actual:$,message:`Requires Blade ${J.manifest.bladeVersion}, current ${$}`};A1(J,K),Q.push(K)}}let Z=!0;while(Z){Z=!1;for(let J of O){if(J.status!=="active")continue;for(let[K,L]of Object.entries(J.manifest.dependencies??{})){let G=q.get(K),W;if(!G)W={code:"dependency-missing",dependency:K,expected:L,message:`Missing dependency ${K}@${L}`};else if(!k0(G.manifest.version,L))W={code:"dependency-version",dependency:K,expected:L,actual:G.manifest.version,message:`Dependency ${K} requires ${L}, found ${G.manifest.version}`};else if(G.status!=="active")W={code:"dependency-inactive",dependency:K,expected:L,actual:G.status,message:`Dependency ${K} is ${G.status}`};if(!W)continue;A1(J,W),Q.push(W),Z=!0;break}}}return Q}import{execFile as mO}from"node:child_process";import{createHash as gO,randomUUID as f1}from"node:crypto";import{constants as e,promises as V}from"node:fs";import{homedir as g0}from"node:os";import S from"node:path";import{promisify as uO}from"node:util";import{Mutex as cO}from"async-mutex";import{satisfies as u0}from"semver";import lO from"write-file-atomic";import*as F0 from"node:fs/promises";import*as v1 from"node:path";var c=Y.String({minLength:2,maxLength:64,pattern:"^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]{1,2}$"}),p0=Y.String({pattern:"^\\d+\\.\\d+\\.\\d+(-[\\w.]+)?(\\+[\\w.]+)?$"}),TO=Y.Object({name:Y.String({minLength:1}),email:Y.Optional(Y.String({format:"email"})),url:Y.Optional(Y.String({format:"uri"}))}),w1=Y.Object({name:c,description:Y.String({minLength:1,maxLength:500}),version:p0,author:Y.Optional(TO),license:Y.Optional(Y.String()),repository:Y.Optional(Y.String({format:"uri"})),homepage:Y.Optional(Y.String({format:"uri"})),keywords:Y.Optional(Y.Array(Y.String())),dependencies:Y.Optional(Y.Record(Y.String(),Y.String())),bladeVersion:Y.Optional(Y.String())}),wO=Y.Union([Y.Object({type:Y.Literal("git"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1}))},{additionalProperties:!1}),Y.Object({type:Y.Literal("local"),path:Y.String({minLength:1})},{additionalProperties:!1}),Y.Object({type:Y.Literal("marketplace"),marketplace:c},{additionalProperties:!1})]),CO=Y.Object({name:c,source:wO,installPath:Y.String({minLength:1}),version:p0,revision:Y.String({minLength:1,maxLength:128}),contentDigest:Y.String({pattern:"^[a-f0-9]{64}$"}),installedAt:Y.String({format:"date-time"}),updatedAt:Y.String({format:"date-time"})},{additionalProperties:!1}),DO=Y.Union([Y.Object({type:Y.Literal("git"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1}))},{additionalProperties:!1}),Y.Object({type:Y.Literal("local"),path:Y.String({minLength:1})},{additionalProperties:!1})]),NO=Y.Object({name:c,source:DO,installPath:Y.String({minLength:1}),revision:Y.String({minLength:1,maxLength:128}),contentDigest:Y.String({pattern:"^[a-f0-9]{64}$"}),addedAt:Y.String({format:"date-time"}),updatedAt:Y.String({format:"date-time"})},{additionalProperties:!1}),RO=Y.Object({version:Y.Literal(1),installed:Y.Record(Y.String(),CO),marketplaces:Y.Record(Y.String(),NO)},{additionalProperties:!1}),C1=Y.Object({name:Y.String({minLength:1}),email:Y.Optional(Y.String({format:"email"})),url:Y.Optional(Y.String({format:"uri"}))}),vO=Y.Object({name:c,description:Y.Optional(Y.String({maxLength:500})),version:Y.Optional(p0),author:Y.Optional(Y.Union([Y.String({minLength:1}),C1])),source:Y.Union([Y.String({minLength:1}),Y.Object({source:Y.Literal("url"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1})),sha:Y.Optional(Y.String({pattern:"^[a-fA-F0-9]{40}$"}))})]),category:Y.Optional(Y.String()),homepage:Y.Optional(Y.String({format:"uri"})),tags:Y.Optional(Y.Array(Y.String()))}),EO=Y.Object({name:c,description:Y.Optional(Y.String({maxLength:500})),owner:Y.Optional(C1),metadata:Y.Optional(Y.Object({description:Y.Optional(Y.String({maxLength:500})),version:Y.Optional(Y.String())})),plugins:Y.Array(vO,{maxItems:5000})}),_1=Y.Object({type:T1(["stdio","sse","http"]),command:Y.Optional(Y.String()),args:Y.Optional(Y.Array(Y.String())),env:Y.Optional(Y.Record(Y.String(),Y.String())),cwd:Y.Optional(Y.String()),url:Y.Optional(Y.String({format:"uri"})),headers:Y.Optional(Y.Record(Y.String(),Y.String())),timeout:Y.Optional(Y.Integer({minimum:1000,maximum:1800000})),idleTimeout:Y.Optional(Y.Integer({minimum:1000,maximum:1800000})),sampling:Y.Optional(Y.Object({enabled:Y.Boolean(),maxTokens:Y.Optional(Y.Integer({minimum:1,maximum:4096})),maxRequestsPerToolCall:Y.Optional(Y.Integer({minimum:1,maximum:8})),maxInputBytes:Y.Optional(Y.Integer({minimum:1,maximum:1048576}))})),oauth:Y.Optional(Y.Object({enabled:Y.Optional(Y.Boolean()),clientId:Y.Optional(Y.String({minLength:1,maxLength:4096})),scopes:Y.Optional(Y.Array(Y.String({minLength:1,maxLength:128}),{maxItems:32})),callbackPort:Y.Optional(Y.Integer({minimum:1024,maximum:65535}))},{additionalProperties:!1})),healthCheck:Y.Optional(Y.Object({enabled:Y.Optional(Y.Boolean()),interval:Y.Optional(Y.Number({exclusiveMinimum:0})),timeout:Y.Optional(Y.Number({exclusiveMinimum:0})),failureThreshold:Y.Optional(Y.Number({exclusiveMinimum:0}))}))}),yO=Y.Union([Y.Object({mcpServers:Y.Record(Y.String(),_1)}),Y.Record(Y.String(),_1)]);function D1(O){return u(yO,O)}function N1(O){return u(RO,O)}function R1(O){return u(EO,O)}var fO=[{dir:".blade-plugin",source:"blade"},{dir:".claude-plugin",source:"claude"}];async function a(O){for(let{dir:$,source:Q}of fO){let q=v1.join(O,$,"plugin.json");try{await F0.access(q)}catch{continue}try{let Z=await F0.readFile(q,"utf-8"),J=JSON.parse(Z),K=u(w1,J);if(!K.success){let L=K.error.issues.map((G)=>`${G.path.join(".")}: ${G.message}`).join("; ");throw Error(`Invalid plugin.json: ${L}`)}return d0(K.data),M.debug(`Parsed plugin manifest from ${q}`),{manifest:K.data,source:Q,manifestPath:q}}catch(Z){if(Z instanceof SyntaxError)throw Error(`Invalid JSON in ${q}: ${Z.message}`);throw Z}}return null}import{realpathSync as hO}from"node:fs";import m0 from"node:path";var kO=/^[a-fA-F0-9]{40}$/;class v extends Error{code;constructor(O,$){super($);this.code=O;this.name="PluginSourcePolicyError"}}function PO(O,$){let Q=(Z)=>{try{return hO.native(Z)}catch{return m0.resolve(Z)}},q=m0.relative(Q(O),Q($));return q===""||!q.startsWith("..")&&!m0.isAbsolute(q)}function dO(O){let $=O.match(/^[^@]+@([^:]+):/);if($?.[1])return $[1].toLowerCase();try{return new URL(O).hostname.toLowerCase()}catch{return}}function pO(O,$){if(!$.startsWith("*."))return O===$;let Q=$.slice(1);return O.endsWith(Q)&&O.length>Q.length}function E1(O,$,Q){if(O.requireGitCommitSha&&(!$.ref||!kO.test($.ref)))throw new v("PLUGIN_GIT_SHA_REQUIRED",`${Q} must pin a full 40-character Git commit SHA`);if(!O.restrictToAllowedSources)return;let q=dO($.url);if(!q||!O.allowedGitHosts.some((Z)=>pO(q,Z)))throw new v("PLUGIN_SOURCE_BLOCKED",`${Q} Git host is not allowed by plugin source policy`)}function y1(O,$,Q){if(!O.restrictToAllowedSources)return;if(!O.allowedLocalRoots.some((q)=>PO(q,$)))throw new v("PLUGIN_SOURCE_BLOCKED",`${Q} local path is not allowed by plugin source policy`)}function l(O,$,Q="Marketplace"){if($.type==="git")E1(O,$,Q);else y1(O,$.path,Q)}function r(O,$,Q,q="Plugin"){if($.type==="git"){E1(O,$,q);return}if($.type==="local"){y1(O,$.path,q);return}if(O.restrictToAllowedSources&&!O.allowedMarketplaces.includes($.marketplace))throw new v("PLUGIN_SOURCE_BLOCKED",`Marketplace "${$.marketplace}" is not allowed by plugin source policy`);let Z=Q[$.marketplace];if(!Z)throw new v("MARKETPLACE_NOT_FOUND",`Marketplace "${$.marketplace}" is not configured`);l(O,Z.source,`Marketplace "${$.marketplace}"`)}var sO=uO(mO),nO=1,h1=2097152,k1=1e4,P1=104857600,oO=120000,iO=15000,tO=600000,s0=/^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$|^[a-z0-9]$/,d1=/^[a-fA-F0-9]{40}$/,aO=[S.join(".blade-plugin","marketplace.json"),S.join(".claude-plugin","marketplace.json"),"marketplace.json"];class F extends Error{code;constructor(O,$){super($);this.code=O;this.name="PluginPackageError"}}function rO(){return{version:nO,installed:{},marketplaces:{}}}function s(O){return O.code==="ENOENT"}function n(O){if(O instanceof F||O instanceof v)return{code:O.code,message:O.message};return{code:"PLUGIN_PACKAGE_ERROR",message:O instanceof Error?O.message:String(O)}}function c0(O){return S.isAbsolute(O)||O.startsWith("./")||O.startsWith("../")||O==="~"||O.startsWith("~/")}function O0(O,$){let Q=S.relative(O,$);return Q===""||!Q.startsWith("..")&&!S.isAbsolute(Q)}function o(O,$="plugin"){if(!s0.test(O))throw new F("INVALID_PLUGIN_NAME",`Invalid ${$} name: ${O}`);return O}function eO(O){if(O.startsWith("git@"))return;let $=O.lastIndexOf("@");if($<=0||$===O.length-1)return;let Q=O.slice(0,$),q=O.slice($+1);if(!s0.test(Q)||!s0.test(q))return;return{pluginName:Q,marketplace:q}}class p1{userPluginsDir;stateRoot;statePath;mutex=new cO;constructor(O,$){this.userPluginsDir=O??S.join(g0(),".blade","plugins"),this.stateRoot=$??S.join(S.dirname(this.userPluginsDir),"plugin-state"),this.statePath=S.join(this.stateRoot,"state.json")}async install(O,$={}){return this.runMutation(async()=>{let Q=[];try{this.requireSourceTrust($.trusted);let q=await this.readState(),Z=await this.resolvePluginSource(O,q,$),J=this.effectivePolicy($.policy);if(r(J,Z.recordSource,q.marketplaces),Z.expectedName&&q.installed[Z.expectedName])throw new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${Z.expectedName}" is already installed`);let K=await this.preparePluginClosure({state:q,source:Z.source,recordSource:Z.recordSource,expectedName:Z.expectedName,policy:J,updateRoot:!1});Q=K.prepared;let L=await this.commitPreparedPlugins(q,Q),G=L.records.get(K.root.name);if(!G)throw new F("PLUGIN_PACKAGE_ERROR","Root plugin was not committed");let W=Q.map((X)=>X.name).filter((X)=>X!==K.root.name);return M.info(`Installed managed plugin ${K.root.name} at revision ${G.revision}`),{success:!0,pluginName:K.root.name,pluginPath:G.installPath,manifest:K.root.manifest,installation:G,installedDependencies:W,changed:L.changed.has(K.root.name)}}catch(q){await this.cleanupPreparedPlugins(Q);let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async update(O,$={}){return this.runMutation(async()=>{let Q=[];try{this.requireSourceTrust($.trusted),o(O);let q=await this.readState(),Z=q.installed[O];if(!Z)throw new F("PLUGIN_NOT_MANAGED",`Plugin "${O}" is not managed by Blade's package store`);let J=this.effectivePolicy($.policy);r(J,Z.source,q.marketplaces);let K=await this.preparePluginClosure({state:q,source:Z.source,recordSource:Z.source,expectedName:O,policy:J,updateRoot:!0});Q=K.prepared;let L=await this.commitPreparedPlugins(q,Q),G=L.records.get(O);if(!G)throw new F("PLUGIN_PACKAGE_ERROR","Updated root plugin was not committed");let W=Q.map((X)=>X.name).filter((X)=>X!==O&&L.changed.has(X));return M.info(`Updated managed plugin ${O} to revision ${G.revision}`),{success:!0,pluginName:O,pluginPath:G.installPath,manifest:K.root.manifest,installation:G,updatedDependencies:W,changed:L.changed.has(O)}}catch(q){await this.cleanupPreparedPlugins(Q);let Z=n(q);return{success:!1,pluginName:O,code:Z.code,error:Z.message}}})}async uninstall(O,$=!1){return this.runMutation(async()=>{try{if(o(O),!$)throw new F("CONFIRMATION_REQUIRED",`Uninstalling "${O}" requires explicit confirmation`);let Q=await this.readState(),q=Q.installed[O];if(!q)throw new F("PLUGIN_NOT_MANAGED",`Plugin "${O}" is not managed by Blade's package store`);return delete Q.installed[O],await this.writeState(Q),M.info(`Uninstalled managed plugin ${O}`),{success:!0,pluginName:O,installation:q}}catch(Q){let q=n(Q);return{success:!1,pluginName:O,code:q.code,error:q.message}}})}async listInstalled(){return Object.keys((await this.readState()).installed).sort()}async listInstallationRecords(){return Object.values((await this.readState()).installed).sort((O,$)=>O.name.localeCompare($.name))}async getInstallation(O){return(await this.readState()).installed[O]}async verifyInstallation(O){let $=await V.lstat(O.installPath);if(!$.isDirectory()||$.isSymbolicLink())throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" has an invalid package path`);let Q=await V.realpath(this.stateRoot),q=await V.realpath(O.installPath);if(!O0(Q,q))throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" has an invalid package path`);if(await this.digestTree(q)!==O.contentDigest)throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" failed content verification`);let J=await a(q);if(!J||J.manifest.name!==O.name||J.manifest.version!==O.version)throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" failed manifest verification`)}async addMarketplace(O,$={}){return this.runMutation(async()=>{let Q;try{let q=await this.normalizeMarketplaceSource(O,$);l(this.effectivePolicy($.policy),q);let Z=await this.readState();Q=await this.materializeSource(q);let J=await this.readMarketplaceManifest(Q.stagingPath),K=o(J.name,"marketplace");if(Z.marketplaces[K])throw new F("MARKETPLACE_ALREADY_EXISTS",`Marketplace "${K}" is already configured`);let{revision:L,contentDigest:G}=Q,W=await this.publishPackage("marketplaces",K,Q);Q=void 0;let X=new Date().toISOString(),H={name:K,source:q,installPath:W,revision:L,contentDigest:G,addedAt:X,updatedAt:X};return Z.marketplaces[K]=H,await this.writeState(Z),{success:!0,marketplace:H,manifest:J,changed:!0}}catch(q){if(Q)await V.rm(Q.stagingPath,{recursive:!0,force:!0});let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async refreshMarketplace(O,$){return this.runMutation(async()=>{let Q;try{o(O,"marketplace");let q=await this.readState(),Z=q.marketplaces[O];if(!Z)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O}" is not configured`);l(this.effectivePolicy($),Z.source,`Marketplace "${O}"`),Q=await this.materializeSource(Z.source);let J=await this.readMarketplaceManifest(Q.stagingPath);if(J.name!==O)throw new F("MARKETPLACE_IDENTITY_MISMATCH",`Marketplace "${O}" now declares "${J.name}"`);if(Z.revision===Q.revision&&Z.contentDigest===Q.contentDigest)return await V.rm(Q.stagingPath,{recursive:!0,force:!0}),Q=void 0,{success:!0,marketplace:Z,manifest:J,changed:!1};let{revision:K,contentDigest:L}=Q,G=await this.publishPackage("marketplaces",O,Q);Q=void 0;let W={...Z,installPath:G,revision:K,contentDigest:L,updatedAt:new Date().toISOString()};return q.marketplaces[O]=W,await this.writeState(q),{success:!0,marketplace:W,manifest:J,changed:!0}}catch(q){if(Q)await V.rm(Q.stagingPath,{recursive:!0,force:!0});let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async removeMarketplace(O,$=!1){return this.runMutation(async()=>{try{if(o(O,"marketplace"),!$)throw new F("CONFIRMATION_REQUIRED",`Removing marketplace "${O}" requires explicit confirmation`);let Q=await this.readState(),q=Q.marketplaces[O];if(!q)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O}" is not configured`);let Z=Object.values(Q.installed).filter((J)=>J.source.type==="marketplace"&&J.source.marketplace===O).map((J)=>J.name).sort();if(Z.length>0)throw new F("MARKETPLACE_IN_USE",`Marketplace "${O}" still owns installed plugins: ${Z.join(", ")}`);return delete Q.marketplaces[O],await this.writeState(Q),{success:!0,marketplace:q,changed:!0}}catch(Q){let q=n(Q);return{success:!1,code:q.code,error:q.message}}})}async listMarketplaces(){return Object.values((await this.readState()).marketplaces).sort((O,$)=>O.name.localeCompare($.name))}async listCatalogs(){let O=await this.listMarketplaces();return Promise.all(O.map(async($)=>({marketplace:$,manifest:await this.readMarketplaceManifest($.installPath)})))}async runMutation(O){return this.mutex.runExclusive(async()=>{let $=await this.acquireStoreLock();try{return await O()}finally{await $()}})}async acquireStoreLock(){await this.ensureStoreDirectory();let O=S.join(this.stateRoot,".operation.lock"),$=f1(),Q=Date.now()+iO;while(Date.now()<Q)try{let q=await V.open(O,e.O_CREAT|e.O_EXCL|e.O_WRONLY,384);return await q.writeFile(`${JSON.stringify({version:1,pid:process.pid,token:$,createdAt:new Date().toISOString()})}
|
|
625
|
-
`,"utf8"),await q.close(),async()=>{try{if(JSON.parse(await V.readFile(O,"utf8")).token===$)await V.unlink(O)}catch(Z){if(!s(Z))M.warn(`Failed to release plugin package lock: ${Z}`)}}}catch(q){if(q.code!=="EEXIST")throw q;await this.removeStaleStoreLock(O),await new Promise((Z)=>setTimeout(Z,50))}throw new F("PLUGIN_STORE_BUSY","Plugin package store is busy; retry the operation")}async removeStaleStoreLock(O){try{let $=await V.lstat(O);if(!$.isFile()||$.isSymbolicLink())throw new F("PLUGIN_STORE_INVALID","Plugin package lock must be a regular file");if(process.getuid&&$.uid!==process.getuid()||($.mode&511)!==384)throw new F("PLUGIN_STORE_INVALID","Plugin package lock ownership or permissions are invalid");if(Date.now()-$.mtimeMs<tO)return;await V.unlink(O)}catch($){if(!s($)&&$ instanceof F)throw $}}requireSourceTrust(O){if(O!==!0)throw new F("SOURCE_TRUST_REQUIRED","Plugin installation or update requires explicit source trust")}effectivePolicy(O){return O??V0.pluginSourcePolicy}async preparePluginClosure(O){let $=[],Q=new Map,q=[],Z=(K,L,G,W)=>{if(!u0(L,G))throw new F("DEPENDENCY_VERSION_MISMATCH",`Plugin "${W}" requires ${K}@${G}, found ${L}`)},J=async(K)=>{let L=K.expectedName?O.state.installed[K.expectedName]:void 0;if(L&&!K.root&&K.requiredRange){if(u0(L.version,K.requiredRange))return L;if(K.recordSource.type!=="marketplace"||L.source.type!=="marketplace"||L.source.marketplace!==K.recordSource.marketplace)Z(L.name,L.version,K.requiredRange,K.requiredBy??"dependency");K={...K,source:L.source,recordSource:L.source}}if(L&&K.root&&!O.updateRoot)throw new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${L.name}" is already installed`);if(K.expectedName){let j=Q.get(K.expectedName);if(j){if(K.requiredRange)Z(j.name,j.manifest.version,K.requiredRange,K.requiredBy??"dependency");return j}if(q.includes(K.expectedName))throw new F("DEPENDENCY_CYCLE",`Plugin dependency cycle: ${[...q,K.expectedName].join(" -> ")}`)}let G=await this.materializePluginSource(K.source,O.state,K.expectedName,O.policy),W=await a(G.stagingPath);if(!W)throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),new F("INVALID_PLUGIN","Plugin manifest was not found");let X=o(W.manifest.name);if(K.expectedName&&K.expectedName!==X)throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),new F("PLUGIN_IDENTITY_MISMATCH",`Requested "${K.expectedName}" but source declares "${X}"`);if(q.includes(X))throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),new F("DEPENDENCY_CYCLE",`Plugin dependency cycle: ${[...q,X].join(" -> ")}`);if(K.root&&O.state.installed[X]&&!O.updateRoot)throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${X}" is already installed`);try{x1(W.manifest)}catch(j){throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),new F("VERSION_INCOMPATIBLE",j instanceof Error?j.message:String(j))}let H={name:X,source:K.recordSource,manifest:W.manifest,materialized:G,existing:O.state.installed[X]};q.push(X);try{for(let[j,g]of Object.entries(W.manifest.dependencies??{}).sort(([q0],[N])=>q0.localeCompare(N))){let q0=Q.get(j);if(q0){Z(j,q0.manifest.version,g,X);continue}let N=O.state.installed[j];if(N&&u0(N.version,g))continue;if(K.recordSource.type!=="marketplace")throw new F(N?"DEPENDENCY_VERSION_MISMATCH":"DEPENDENCY_MISSING",N?`Plugin "${X}" requires ${j}@${g}, found ${N.version}`:`Plugin "${X}" requires missing dependency ${j}@${g}; direct sources cannot auto-install dependencies`);let _0=await J({source:N?.source.type==="marketplace"&&N.source.marketplace===K.recordSource.marketplace?N.source:{type:"marketplace",marketplace:K.recordSource.marketplace},recordSource:{type:"marketplace",marketplace:K.recordSource.marketplace},expectedName:j,requiredRange:g,requiredBy:X,root:!1});Z(j,"manifest"in _0?_0.manifest.version:_0.version,g,X)}}catch(j){throw await V.rm(G.stagingPath,{recursive:!0,force:!0}),j}finally{q.pop()}return Q.set(X,H),$.push(H),H};try{let K=await J({source:O.source,recordSource:O.recordSource,expectedName:O.expectedName,root:!0});if(!("manifest"in K))throw new F("PLUGIN_PACKAGE_ERROR","Root plugin unexpectedly resolved to an existing dependency");return{root:K,prepared:$}}catch(K){throw await this.cleanupPreparedPlugins($),K}}async commitPreparedPlugins(O,$){let Q=new Map,q=new Set,Z=new Date().toISOString();for(let J of $){let K=J.existing;if(K?.revision===J.materialized.revision&&K.contentDigest===J.materialized.contentDigest){await V.rm(J.materialized.stagingPath,{recursive:!0,force:!0}),Q.set(J.name,K);continue}let L=await this.publishPackage("packages",J.name,J.materialized),G={name:J.name,source:J.source,installPath:L,version:J.manifest.version,revision:J.materialized.revision,contentDigest:J.materialized.contentDigest,installedAt:K?.installedAt??Z,updatedAt:Z};O.installed[J.name]=G,Q.set(J.name,G),q.add(J.name)}if(q.size>0)await this.writeState(O);return{records:Q,changed:q}}async cleanupPreparedPlugins(O){await Promise.all(O.map(($)=>V.rm($.materialized.stagingPath,{recursive:!0,force:!0})))}async resolvePluginSource(O,$,Q){let q=O.trim();if(!q)throw new F("INVALID_SOURCE","Plugin source is required");let Z=eO(q);if(Z){if(!$.marketplaces[Z.marketplace])throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${Z.marketplace}" is not configured`);return{source:{type:"marketplace",marketplace:Z.marketplace},recordSource:{type:"marketplace",marketplace:Z.marketplace},expectedName:Z.pluginName}}if(!c0(q)&&!this.looksLikeGitSource(q)){let K=[];for(let L of await this.catalogsFromState($))if(L.manifest.plugins.some((G)=>G.name===q))K.push({pluginName:q,marketplace:L.marketplace.name});if(K.length===1)return{source:{type:"marketplace",marketplace:K[0].marketplace},recordSource:{type:"marketplace",marketplace:K[0].marketplace},expectedName:q};if(K.length>1)throw new F("AMBIGUOUS_PLUGIN_SOURCE",`Plugin "${q}" exists in multiple marketplaces; use name@marketplace`)}if(c0(q)){let L={type:"local",path:await this.resolveLocalSource(q,Q.workspaceRoot)};return{source:L,recordSource:L}}let J=this.normalizeGitSource(q,Q.ref);return{source:J,recordSource:J}}async materializePluginSource(O,$,Q,q){if(O.type!=="marketplace")return this.materializeSource(O);let Z=$.marketplaces[O.marketplace];if(!Z)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O.marketplace}" is not configured`);let J=await this.readMarketplaceManifest(Z.installPath),K=Q?J.plugins.filter((L)=>L.name===Q):J.plugins;if(K.length!==1)throw new F("MARKETPLACE_PLUGIN_NOT_FOUND",Q?`Plugin "${Q}" was not found in marketplace "${O.marketplace}"`:`Marketplace "${O.marketplace}" requires an explicit plugin name`);return this.materializeMarketplaceEntry(Z,K[0],q)}async materializeMarketplaceEntry(O,$,Q){if(typeof $.source!=="string"){let K=this.normalizeGitSource($.source.url,$.source.sha??$.source.ref);return l(Q,K,`Plugin "${$.name}"`),this.materializeSource(K)}if($.source.startsWith("https://")||$.source.startsWith("ssh://")||$.source.startsWith("git@")){let K=this.normalizeGitSource($.source);return l(Q,K,`Plugin "${$.name}"`),this.materializeSource(K)}let q=await V.realpath(O.installPath),Z=S.resolve(q,$.source);if(!O0(q,Z)||Z===q)throw new F("MARKETPLACE_PATH_ESCAPE",`Plugin "${$.name}" source escapes its marketplace`);let J=await V.realpath(Z);if(!O0(q,J))throw new F("MARKETPLACE_PATH_ESCAPE",`Plugin "${$.name}" source escapes its marketplace`);return this.materializeSource({type:"local",path:J})}async normalizeMarketplaceSource(O,$){let Q=O.trim();if(!Q)throw new F("INVALID_SOURCE","Marketplace source is required");if(c0(Q))return{type:"local",path:await this.resolveLocalSource(Q,$.workspaceRoot)};return this.normalizeGitSource(Q,$.ref)}normalizeGitSource(O,$){let Q=O;if(/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(O))Q=`https://github.com/${O}.git`;else if(O.startsWith("git@")){if(!/^git@[A-Za-z0-9.-]+:[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+(?:\.git)?$/.test(O))throw new F("INVALID_SOURCE","Invalid SSH Git source")}else{let q;try{q=new URL(O)}catch{throw new F("INVALID_SOURCE","Invalid Git source")}if(q.protocol!=="https:"&&q.protocol!=="ssh:")throw new F("UNSAFE_GIT_PROTOCOL","Git sources must use HTTPS or SSH");if(q.password||q.protocol==="https:"&&q.username)throw new F("GIT_CREDENTIALS_IN_URL","Git credentials must not be embedded in plugin source URLs");if(q.hash||q.search)throw new F("INVALID_SOURCE","Use the ref option instead of URL query or fragment parameters");Q=q.toString()}if($&&(!$.trim()||$.startsWith("-")||$.includes("\x00")))throw new F("INVALID_GIT_REF","Invalid Git ref");return{type:"git",url:Q,...$?{ref:$}:{}}}looksLikeGitSource(O){return O.startsWith("https://")||O.startsWith("ssh://")||O.startsWith("git@")||/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(O)}async resolveLocalSource(O,$){let Q=O==="~"?g0():O.startsWith("~/")?S.join(g0(),O.slice(2)):O,q=S.resolve($??process.cwd(),Q),Z;try{Z=await V.realpath(q)}catch{throw new F("LOCAL_SOURCE_NOT_FOUND",`Local source does not exist: ${O}`)}let J=await V.lstat(Z);if(!J.isDirectory()||J.isSymbolicLink())throw new F("INVALID_LOCAL_SOURCE","Local plugin sources must be regular directories");return Z}async materializeSource(O){await this.ensureStoreDirectory();let $=S.join(this.stateRoot,"staging",f1());if(await V.mkdir(S.dirname($),{recursive:!0,mode:448}),O.type==="git"){await this.cloneGitSource(O,$);let q=await this.runGit(["rev-parse","HEAD"],$);if(O.ref&&d1.test(O.ref)&&q.toLowerCase()!==O.ref.toLowerCase())throw await V.rm($,{recursive:!0,force:!0}),new F("GIT_PIN_MISMATCH","Plugin checkout does not match the requested Git commit SHA");await V.rm(S.join($,".git"),{recursive:!0,force:!0});let Z=await this.digestTree($);return{stagingPath:$,revision:q,contentDigest:Z}}await this.copyLocalSource(O.path,$);let Q=await this.digestTree($);return{stagingPath:$,revision:`local-${Q.slice(0,40)}`,contentDigest:Q}}async cloneGitSource(O,$){try{if(O.ref&&d1.test(O.ref))await V.mkdir($,{recursive:!0,mode:448}),await this.runGit(["init"],$),await this.runGit(["remote","add","origin",O.url],$),await this.runGit(["fetch","--depth","1","origin",O.ref],$),await this.runGit(["checkout","--detach","FETCH_HEAD"],$);else{let Q=["clone","--depth","1","--no-tags"];if(O.ref)Q.push("--branch",O.ref);Q.push("--",O.url,$),await this.runGit(Q)}}catch(Q){throw await V.rm($,{recursive:!0,force:!0}),new F("GIT_CLONE_FAILED",`Failed to clone plugin source: ${this.sanitizeError(Q)}`)}}async runGit(O,$){let Q=await sO("git",O,{cwd:$,encoding:"utf8",timeout:oO,maxBuffer:4194304,env:{...process.env,GIT_TERMINAL_PROMPT:"0"}}),q=typeof Q==="string"?Q:Q.stdout;if(q===void 0)throw Error("Git process returned an invalid result");return String(q).trim()}sanitizeError(O){return(O instanceof Error?O.message:String(O)).replaceAll(this.stateRoot,"<plugin-store>").slice(0,500)}async copyLocalSource(O,$){await this.digestTree(O),await V.cp(O,$,{recursive:!0,force:!1,errorOnExist:!0,filter:(Q)=>{return!S.relative(O,Q).split(S.sep).includes(".git")}})}async digestTree(O){let $=[],Q=0,q=async(J)=>{let K=await V.readdir(J,{withFileTypes:!0});K.sort((L,G)=>L.name.localeCompare(G.name));for(let L of K){if(L.name===".git")continue;let G=S.join(J,L.name),W=S.relative(O,G),X=await V.lstat(G);if(X.isSymbolicLink())throw new F("PACKAGE_SYMLINK_FORBIDDEN",`Plugin packages may not contain symbolic links: ${W}`);if(X.isDirectory()){await q(G);continue}if(!X.isFile())throw new F("PACKAGE_ENTRY_FORBIDDEN",`Plugin packages may contain only files and directories: ${W}`);if(Q+=X.size,$.push({relative:W,absolute:G,executable:(X.mode&73)!==0}),$.length>k1||Q>P1)throw new F("PACKAGE_TOO_LARGE",`Plugin package exceeds ${k1} files or ${P1} bytes`)}};await q(O);let Z=gO("sha256");for(let J of $)Z.update(J.relative),Z.update("\x00"),Z.update(J.executable?"x":"-"),Z.update("\x00"),Z.update(await V.readFile(J.absolute)),Z.update("\x00");return Z.digest("hex")}async publishPackage(O,$,Q){let q=S.join(this.stateRoot,O,$,Q.contentDigest);await V.mkdir(S.dirname(q),{recursive:!0,mode:448});try{let Z=await V.lstat(q);if(!Z.isDirectory()||Z.isSymbolicLink())throw new F("PACKAGE_STORE_CONFLICT","Plugin package destination is not a regular directory");return await V.rm(Q.stagingPath,{recursive:!0,force:!0}),q}catch(Z){if(!s(Z))throw Z}return await V.rename(Q.stagingPath,q),q}async readMarketplaceManifest(O){for(let $ of aO){let Q=S.join(O,$);try{let q=await V.lstat(Q);if(!q.isFile()||q.isSymbolicLink()||q.size>h1)throw new F("INVALID_MARKETPLACE","Marketplace manifest must be a regular bounded file");let Z=JSON.parse(await V.readFile(Q,"utf8")),J=R1(Z);if(!J.success)throw new F("INVALID_MARKETPLACE",`Invalid marketplace manifest: ${J.error.issues.map((L)=>`${L.path.join(".")}: ${L.message}`).join("; ")}`);let K=new Set;for(let L of J.data.plugins){if(K.has(L.name))throw new F("INVALID_MARKETPLACE",`Marketplace contains duplicate plugin: ${L.name}`);K.add(L.name)}return J.data}catch(q){if(s(q))continue;if(q instanceof SyntaxError)throw new F("INVALID_MARKETPLACE","Marketplace manifest contains invalid JSON");throw q}}throw new F("INVALID_MARKETPLACE","Marketplace manifest was not found")}async catalogsFromState(O){let $=Object.values(O.marketplaces).sort((Q,q)=>Q.name.localeCompare(q.name));return Promise.all($.map(async(Q)=>({marketplace:Q,manifest:await this.readMarketplaceManifest(Q.installPath)})))}async readState(){let O;try{O=await V.open(this.statePath,e.O_RDONLY|e.O_NOFOLLOW)}catch($){if(s($))return rO();throw new F("PLUGIN_STATE_INVALID","Plugin package state must be a regular file")}try{let $=await O.stat();if(!$.isFile()||$.size>h1)throw new F("PLUGIN_STATE_INVALID","Plugin package state is invalid");if(process.getuid&&$.uid!==process.getuid())throw new F("PLUGIN_STATE_INVALID","Plugin package state must be owned by the current user");if(($.mode&511)!==384)throw new F("PLUGIN_STATE_INVALID","Plugin package state permissions must be 0600");let Q=JSON.parse(await O.readFile("utf8")),q=N1(Q);if(!q.success)throw new F("PLUGIN_STATE_INVALID","Plugin package state schema is invalid");let Z=q.data;for(let[J,K]of Object.entries(Z.installed)){let L=S.join(this.stateRoot,"packages",J);if(J!==K.name||!S.isAbsolute(K.installPath)||!O0(L,K.installPath)||S.basename(K.installPath)!==K.contentDigest||K.source.type==="local"&&!S.isAbsolute(K.source.path))throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid installation");if(K.source.type==="git"){let G=this.normalizeGitSource(K.source.url,K.source.ref);if(G.url!==K.source.url||G.ref!==K.source.ref)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid Git source")}}for(let[J,K]of Object.entries(Z.marketplaces)){let L=S.join(this.stateRoot,"marketplaces",J);if(J!==K.name||!S.isAbsolute(K.installPath)||!O0(L,K.installPath)||S.basename(K.installPath)!==K.contentDigest||K.source.type==="local"&&!S.isAbsolute(K.source.path))throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid marketplace");if(K.source.type==="git"){let G=this.normalizeGitSource(K.source.url,K.source.ref);if(G.url!==K.source.url||G.ref!==K.source.ref)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid Marketplace Git source")}}for(let J of Object.values(Z.installed))if(J.source.type==="marketplace"&&!Z.marketplaces[J.source.marketplace])throw new F("PLUGIN_STATE_INVALID","Plugin package state references a missing marketplace");return Z}catch($){if($ instanceof SyntaxError)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains invalid JSON");throw $}finally{await O.close()}}async writeState(O){await this.ensureStoreDirectory(),await lO(this.statePath,`${JSON.stringify(O,null,2)}
|
|
626
|
-
`,{mode:384,encoding:"utf8"}),await V.chmod(this.statePath,384)}async ensureStoreDirectory(){try{let O=await V.lstat(this.stateRoot);if(!O.isDirectory()||O.isSymbolicLink())throw new F("PLUGIN_STORE_INVALID","Plugin package store must be a regular directory");if(process.getuid&&O.uid!==process.getuid())throw new F("PLUGIN_STORE_INVALID","Plugin package store must be owned by the current user");if((O.mode&511)!==448)throw new F("PLUGIN_STORE_INVALID","Plugin package store permissions must be 0700")}catch(O){if(!s(O))throw O;await V.mkdir(this.stateRoot,{recursive:!0,mode:448}),await V.chmod(this.stateRoot,448)}}}var l0=null;function n0(O,$){if(!l0)l0=new p1(O,$);return l0}import*as b from"node:fs/promises";import{homedir as O$}from"node:os";import*as I from"node:path";import g1 from"gray-matter";function H0(O,$){return`${O}:${$}`}function m1(O,$){return`${O}__${$}`}class U0{async loadPlugin(O,$,Q={}){let q=await a(O);if(!q)throw Error(`Not a valid plugin directory: ${O}`);let{manifest:Z,source:J}=q,K=Z.name;M.debug(`Loading plugin "${K}" from ${O}`);let L=Q.skipCommands?[]:await this.loadCommands(O,K),G=Q.skipAgents?[]:await this.loadAgents(O,K),W=Q.skipSkills?[]:await this.loadSkills(O,K),X=Q.skipHooks?void 0:await this.loadHooks(O),H=Q.skipMcp?void 0:await this.loadMcpConfig(O,K),j=Q.skipLsp?void 0:await this.loadLspConfig(O,K);return M.debug(`Plugin "${K}" loaded: ${L.length} commands, ${G.length} agents, ${W.length} skills`),{manifest:Z,basePath:O,source:$,manifestSource:J,commands:L,agents:G,skills:W,hooks:X,mcpServers:H,lspServers:j,status:"active",loadedAt:new Date}}async loadCommands(O,$){let Q=I.join(O,"commands"),q=[];if(!await this.dirExists(Q))return q;let Z=await this.scanMarkdownFiles(Q);for(let J of Z)try{let K=await this.parseCommandFile(J,Q,$);if(K)q.push(K)}catch(K){M.warn(`Failed to load command from ${J}: ${K instanceof Error?K.message:String(K)}`)}return q}async parseCommandFile(O,$,Q){let q=await b.readFile(O,"utf-8"),{data:Z,content:J}=g1(q),L=I.relative($,O).split(I.sep),G=L.pop();if(!G)return null;let W=G.replace(/\.md$/i,""),X=W;if(L.length>0)X=[...L,W].join("/");let H=H0(Q,X),j=this.normalizeCommandConfig(Z);return{originalName:X,namespacedName:H,pluginName:Q,config:j,content:J.trim(),path:O}}normalizeCommandConfig(O){return{description:this.asString(O.description),allowedTools:this.parseStringArray(O["allowed-tools"]),argumentHint:this.asString(O["argument-hint"]),model:this.asString(O.model),disableModelInvocation:O["disable-model-invocation"]===!0}}async loadAgents(O,$){let Q=I.join(O,"agents"),q=[];if(!await this.dirExists(Q))return q;let Z=await this.scanMarkdownFiles(Q);for(let J of Z)try{let K=await this.parseAgentFile(J,Q,$);if(K)q.push(K)}catch(K){M.warn(`Failed to load agent from ${J}: ${K instanceof Error?K.message:String(K)}`)}return q}async parseAgentFile(O,$,Q){let q=await b.readFile(O,"utf-8"),{data:Z,content:J}=g1(q),K=I.relative($,O),L=I.basename(K,".md"),G=Z.name||L,W=H0(Q,G),X={name:W,description:this.asString(Z.description)||"",tools:this.parseStringArray(Z.tools),color:this.asString(Z.color),model:this.asString(Z.model),permissionMode:this.asString(Z.permissionMode),skills:this.parseStringArray(Z.skills),systemPrompt:J.trim(),source:`plugin:${Q}`};return{originalName:G,namespacedName:W,pluginName:Q,config:X,path:O}}async loadSkills(O,$){let Q=I.join(O,"skills"),q=[];if(!await this.dirExists(Q))return q;let Z=await b.readdir(Q,{withFileTypes:!0});for(let J of Z){if(!J.isDirectory())continue;let K=I.join(Q,J.name),L=I.join(K,"SKILL.md");try{let G=await W0(L,"project");if(G.success&&G.content){let W=G.content.metadata.name,X=H0($,W);q.push({originalName:W,namespacedName:X,pluginName:$,metadata:{...G.content.metadata,name:X},path:K})}}catch(G){M.warn(`Failed to load skill from ${L}: ${G instanceof Error?G.message:String(G)}`)}}return q}async loadHooks(O){let $=I.join(O,"hooks","hooks.json");try{let Q=await b.readFile($,"utf-8"),q=JSON.parse(Q);return q.hooks||q}catch{return}}async loadMcpConfig(O,$){let Q=I.join(O,".mcp.json");try{let q=await b.readFile(Q,"utf-8"),Z=JSON.parse(q),J=D1(Z);if(!J.success){M.warn(`Invalid .mcp.json in ${O}: ${J.error}`);return}let K="mcpServers"in Z?Z.mcpServers:Z,L={};for(let[G,W]of Object.entries(K)){let X=m1($,G);L[X]=W}return L}catch{return}}async loadLspConfig(O,$){let Q=I.join(O,".lsp.json");try{let q=JSON.parse(await b.readFile(Q,"utf8")),Z=q&&typeof q==="object"&&!Array.isArray(q)&&"lspServers"in q?q.lspServers:q,J=u1(Z),K={};for(let[L,G]of Object.entries(J)){let W=(X)=>X.replaceAll("${CLAUDE_PLUGIN_ROOT}",O).replaceAll("${BLADE_PLUGIN_ROOT}",O);K[`plugin:${$}:${L}`]={...G,command:W(G.command),args:G.args?.map(W),env:G.env?Object.fromEntries(Object.entries(G.env).map(([X,H])=>[X,W(H)])):void 0}}return K}catch(q){if(q.code!=="ENOENT")M.warn(`Invalid .lsp.json in ${O}: ${q instanceof Error?q.message:String(q)}`);return}}async scanMarkdownFiles(O){let $=[],Q=async(q)=>{let Z=await b.readdir(q,{withFileTypes:!0});for(let J of Z){let K=I.join(q,J.name);if(J.isDirectory())await Q(K);else if(J.name.endsWith(".md"))$.push(K)}};return await Q(O),$}async dirExists(O){try{return(await b.stat(O)).isDirectory()}catch{return!1}}asString(O){if(typeof O==="string"&&O.trim())return O.trim();return}parseStringArray(O){if(!O)return;if(Array.isArray(O))return O.map(($)=>String($).trim()).filter(Boolean);if(typeof O==="string")return O.split(",").map(($)=>$.trim()).filter(Boolean);return}static getPluginDirs(O){let $=O$();return[{path:I.join($,".claude","plugins"),source:"user",type:"claude"},{path:I.join($,".blade","plugins"),source:"user",type:"blade"},{path:I.join(O,".claude","plugins"),source:"project",type:"claude"},{path:I.join(O,".blade","plugins"),source:"project",type:"blade"}]}async discoverPluginsInDir(O,$){let Q=[],q=[];try{await b.access(O)}catch{return{plugins:Q,errors:q}}let Z=await b.readdir(O,{withFileTypes:!0});for(let J of Z){if(!J.isDirectory())continue;let K=I.join(O,J.name);try{let L=await this.loadPlugin(K,$);Q.push(L)}catch(L){q.push({path:K,error:L instanceof Error?L.message:String(L)})}}return{plugins:Q,errors:q}}}class f{static instances=new Map;plugins=new Map;loader=new U0;initialized=!1;workspaceRoot="";cliPluginDirs=[];enabledSettings={};sourcePolicy={...V0.pluginSourcePolicy};marketplaces={};constructor(O){this.workspaceRoot=O}static getInstance(O=x()){let $=$$.resolve(O),Q=f.instances.get($);if(!Q)Q=new f($),f.instances.set($,Q);return Q}static resetInstance(){f.instances.clear()}static getInitializedInstances(){return Array.from(f.instances.values()).filter((O)=>O.isInitialized())}async initialize(O,$=[]){this.workspaceRoot=O,this.cliPluginDirs=$,this.enabledSettings=await S0.getInstance().loadWorkspacePluginSettings(O),await this.refreshPolicyContext();let Q=[],q=[];for(let G of $)try{let W=await this.loader.loadPlugin(G,"cli");this.plugins.set(W.manifest.name,W),Q.push(W),M.info(`Loaded CLI plugin: ${W.manifest.name} from ${G}`)}catch(W){q.push({path:G,error:W instanceof Error?W.message:String(W)}),M.warn(`Failed to load CLI plugin from ${G}: ${W}`)}let Z=(await T.getInstance().getStatus(O)).state==="trusted",J=U0.getPluginDirs(O).filter((G)=>G.source!=="project"||Z),K=!1,L=async()=>{if(K)return;K=!0;let G=n0(),W=await G.listInstallationRecords();for(let X of W)try{await G.verifyInstallation(X);let H=await this.loader.loadPlugin(X.installPath,"user");if(H.manifest.name!==X.name)throw Error(`Managed plugin identity mismatch: ${X.name}`);H.installation=Object.freeze({...X});let j=this.plugins.get(H.manifest.name);if(!j||j.source==="user")this.plugins.set(H.manifest.name,H),Q.push(H)}catch(H){q.push({path:X.installPath,code:"IO_ERROR",error:H instanceof Error?H.message:String(H)})}};for(let{path:G,source:W}of J){if(W==="project")await L();let X=await this.loader.discoverPluginsInDir(G,W);for(let H of X.plugins){let j=this.plugins.get(H.manifest.name);if(!j||j.source!=="cli")this.plugins.set(H.manifest.name,H),Q.push(H)}q.push(...X.errors)}await L(),this.applyEnabledSettings();for(let G of this.plugins.values()){if(G.status!=="error")continue;q.push({path:G.basePath,code:G.compatibilityIssues?.some((W)=>W.code==="source-policy")?"SOURCE_POLICY_BLOCKED":G.compatibilityIssues?.some((W)=>W.code==="blade-version")?"VERSION_INCOMPATIBLE":"DEPENDENCY_MISSING",error:G.error??"Plugin compatibility check failed"})}return this.initialized=!0,M.info(`Plugin system initialized: ${this.plugins.size} plugins loaded`+(q.length>0?`, ${q.length} errors`:"")),{plugins:Array.from(this.plugins.values()),errors:q}}isInitialized(){return this.initialized}getWorkspaceRoot(){return this.workspaceRoot}getSourcePolicy(){return{...this.sourcePolicy,allowedGitHosts:[...this.sourcePolicy.allowedGitHosts],allowedMarketplaces:[...this.sourcePolicy.allowedMarketplaces],allowedLocalRoots:[...this.sourcePolicy.allowedLocalRoots]}}getAll(){return Array.from(this.plugins.values())}getActive(){return Array.from(this.plugins.values()).filter((O)=>O.status==="active")}get(O){return this.plugins.get(O)}has(O){return this.plugins.has(O)}async reapplyEnabledSettings(){this.enabledSettings=await S0.getInstance().loadWorkspacePluginSettings(this.workspaceRoot),await this.refreshPolicyContext(),this.applyEnabledSettings()}applyEnabledSettings(){for(let O of this.plugins.values()){if(O.compatibilityIssues=[],O.error=void 0,O.status=O.source==="cli"||this.enabledSettings[O.manifest.name]!==!1?"active":"inactive",O.status!=="active")continue;try{r(this.sourcePolicy,O.installation?.source??{type:"local",path:O.basePath},this.marketplaces,`Plugin "${O.manifest.name}"`)}catch($){if(!($ instanceof v))throw $;O.status="error",O.error=$.message,O.compatibilityIssues=[{code:"source-policy",message:$.message}]}}b1(Array.from(this.plugins.values()))}async refreshPolicyContext(){this.sourcePolicy=await S0.getInstance().loadWorkspacePluginSourcePolicy(this.workspaceRoot),this.marketplaces=Object.fromEntries((await n0().listMarketplaces()).map((O)=>[O.name,O]))}getBySource(){let O={cli:[],project:[],user:[]};for(let $ of this.plugins.values())O[$.source].push($);return O}getAllCommands(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.commands);return O}getAllSkills(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.skills);return O}getAllAgents(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.agents);return O}findCommand(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}findSkill(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.skills)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.skills)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}findAgent(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.agents)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.agents)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}hasCommandConflict(O){let $=0;for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O){if($++,$>1)return!0}}return!1}getCommandProviders(O){let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O){$.push(Q.manifest.name);break}}return $}disable(O){let $=this.plugins.get(O);if($&&$.status==="active")return $.status="inactive",M.info(`Plugin "${O}" disabled`),!0;return!1}enable(O){let $=this.plugins.get(O);if($&&$.status==="inactive")return $.status="active",M.info(`Plugin "${O}" enabled`),!0;return!1}async refresh(){return M.info("Refreshing plugin list..."),this.plugins.clear(),this.initialized=!1,this.initialize(this.workspaceRoot,this.cliPluginDirs)}getStats(){let O=0,$=0,Q=0,q=0,Z=0;for(let J of this.plugins.values())if(J.status==="active")O++,Q+=J.commands.length,q+=J.skills.length,Z+=J.agents.length;else $++;return{total:this.plugins.size,active:O,inactive:$,commands:Q,skills:q,agents:Z}}formatPluginList(){if(this.getAll().length===0)return"没有已加载的插件。";let $=[],Q=this.getBySource();if(Q.cli.length>0){$.push("## CLI 指定的插件");for(let q of Q.cli){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}$.push("")}if(Q.project.length>0){$.push("## 项目级插件");for(let q of Q.project){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}$.push("")}if(Q.user.length>0){$.push("## 用户级插件");for(let q of Q.user){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}}return $.join(`
|
|
627
|
-
`)}}function j0(O=x()){return f.getInstance(O)}var d=new Map,c1=Object.values(M0);function o0(O){let $={...O};for(let Q of c1)$[Q]=(O[Q]??[]).map((q)=>({...q,matcher:q.matcher?{...q.matcher}:void 0,hooks:q.hooks.map((Z)=>({...Z}))}));return $}class i0{commandRegistry;hookManager;skillRegistry;subagentRegistry;workspaceRoot;constructor(O=x()){this.workspaceRoot=Q$.resolve(O),this.commandRegistry=w.getInstance(this.workspaceRoot),this.hookManager=B0.getInstance(),this.skillRegistry=P({cwd:this.workspaceRoot}),this.subagentRegistry=i(this.workspaceRoot)}clearAllPluginResources(){this.commandRegistry.clearPluginCommands(),this.skillRegistry.clearPluginSkills(),this.subagentRegistry.clearPluginAgents();let O=d.get(this.workspaceRoot);if(O)this.hookManager.loadConfig(O,this.workspaceRoot),d.delete(this.workspaceRoot);M.debug("Cleared all plugin resources from subsystems")}async integrateAll(){let $=j0(this.workspaceRoot).getActive(),Q=[],q=[],Z=0,J=0,K=0,L=0,G=0;for(let W of $){let X=this.integratePlugin(W);Q.push(X),Z+=X.commandsRegistered,J+=X.skillsRegistered,K+=X.agentsRegistered,L+=X.mcpServersRegistered,G+=X.lspServersRegistered,q.push(...X.errors)}if(this.integrateHooks($),Z+J+K>0)M.info(`Plugin integration complete: ${Z} commands, ${J} skills, ${K} agents, ${L} MCP servers, ${G} LSP servers`);return{plugins:Q,totalCommands:Z,totalSkills:J,totalAgents:K,totalMcpServers:L,errors:q}}integratePlugin(O){let $={pluginName:O.manifest.name,commandsRegistered:0,skillsRegistered:0,agentsRegistered:0,hooksRegistered:!1,mcpServersRegistered:0,lspServersRegistered:0,errors:[]};try{$.commandsRegistered=this.integrateCommands(O)}catch(Q){$.errors.push(`Failed to integrate commands: ${Q instanceof Error?Q.message:String(Q)}`)}try{$.skillsRegistered=this.integrateSkills(O)}catch(Q){$.errors.push(`Failed to integrate skills: ${Q instanceof Error?Q.message:String(Q)}`)}try{$.agentsRegistered=this.integrateAgents(O)}catch(Q){$.errors.push(`Failed to integrate agents: ${Q instanceof Error?Q.message:String(Q)}`)}$.hooksRegistered=O.hooks!==void 0;try{$.mcpServersRegistered=this.integrateMcp(O)}catch(Q){$.errors.push(`Failed to integrate MCP servers: ${Q instanceof Error?Q.message:String(Q)}`)}return $.lspServersRegistered=Object.keys(O.lspServers??{}).length,M.debug(`Integrated plugin "${O.manifest.name}": ${$.commandsRegistered} commands, ${$.skillsRegistered} skills, ${$.agentsRegistered} agents`),$}integrateCommands(O){let $=0;for(let Q of O.commands)this.commandRegistry.registerPluginCommand(Q),$++;return $}integrateSkills(O){let $=0;for(let Q of O.skills)this.skillRegistry.registerPluginSkill(Q),$++;return $}integrateAgents(O){let $=0;for(let Q of O.agents)this.subagentRegistry.register({...Q.config,name:Q.namespacedName}),$++;return $}integrateHooks(O){let $=O.filter((J)=>J.hooks);if($.length===0){let J=d.get(this.workspaceRoot);if(J)this.hookManager.loadConfig(J,this.workspaceRoot),d.delete(this.workspaceRoot);return}let Q=o0(this.hookManager.getConfig(this.workspaceRoot));if(!d.has(this.workspaceRoot))d.set(this.workspaceRoot,o0(Q));let q=d.get(this.workspaceRoot)??Q,Z=o0(q);Z.enabled=!0;for(let J of c1){let K=q.enabled?[...q[J]??[]]:[],L=[];for(let G of $)for(let W of G.hooks?.[J]??[])L.push({...W,name:W.name?`plugin:${G.manifest.name}:${W.name}`:`plugin:${G.manifest.name}:${J}`,hooks:W.hooks.map((X)=>({...X,source:{kind:"plugin",pluginName:G.manifest.name,pluginSource:G.source,pluginRoot:G.basePath}}))});Z[J]=[...K,...L]}this.hookManager.loadConfig(Z,this.workspaceRoot)}integrateMcp(O){return Object.keys(O.mcpServers??{}).length}}async function t0(O=x()){return new i0(O).integrateAll()}function I0(O=x()){new i0(O).clearAllPluginResources()}import{createHash as q$}from"node:crypto";import*as D from"node:fs/promises";import Z$ from"node:os";import C from"node:path";import J$ from"gray-matter";var z0=32,l1=24576,s1=16384,n1=131072,A0=4,o1=80,i1=256,a1=/^[a-z0-9][a-z0-9._-]{0,63}$/,K$=h("Agent");function a0(O){for(let $ of O){let Q=$.codePointAt(0)??0;if(Q<=8||Q===11||Q===12||Q>=14&&Q<=31||Q===127||Q>=8203&&Q<=8207||Q>=8234&&Q<=8238||Q>=8288&&Q<=8303||Q===65279)return!0}return!1}function G$(O,$,Q){let q=typeof O==="string"?O.trim():"",Z=$.split(/\r?\n/).map((K)=>K.trim()).find(Boolean)?.replace(/^#+\s*/,"")??"",J=q||Z||Q;if(J.length>i1)throw Error(`Description exceeds ${i1} characters`);if(a0(J))throw Error("Description contains hidden control characters");return J}function Y$(O,$){let Q=typeof O==="string"&&O.trim()?O.trim():$;if(Q.length>o1)throw Error(`Name exceeds ${o1} characters`);if(a0(Q))throw Error("Name contains hidden control characters");return Q}function L$(O){let Q=O.replace(/\.md$/i,"").split(C.sep);if(Q.length===0||Q.length>A0||Q.some((q)=>!a1.test(q)))throw Error(`Style path must use 1-${A0} lowercase safe segments`);return Q.join(":")}async function W$(O){let $;try{$=await D.lstat(O)}catch(J){if(J.code==="ENOENT")return[];throw J}if($.isSymbolicLink())throw Error(`Output style root cannot be a symlink: ${O}`);if(!$.isDirectory())return[];let Q=await D.realpath(O),q=[],Z=async(J,K)=>{if(K>A0)throw Error(`Output style directory exceeds depth ${A0}`);let L=await D.readdir(J,{withFileTypes:!0});L.sort((G,W)=>G.name.localeCompare(W.name));for(let G of L){let W=C.join(J,G.name);if(G.isSymbolicLink())throw Error(`Output style path cannot be a symlink: ${W}`);if(G.isDirectory()){await Z(W,K+1);continue}if(!G.isFile()||!G.name.toLowerCase().endsWith(".md"))continue;let X=await D.realpath(W),H=C.relative(Q,X);if(!H||H.startsWith("..")||C.isAbsolute(H))throw Error(`Output style path escapes its source root: ${W}`);if(q.push(X),q.length>z0)throw Error(`Output style catalog exceeds ${z0} files`)}};return await Z(Q,1),q}async function X$(O,$,Q){let q=await D.lstat(Q);if(!q.isFile()||q.isSymbolicLink())throw Error("Output style must be a regular file");if(q.size>l1)throw Error(`Output style exceeds ${l1} bytes`);let Z=await D.readFile(Q,"utf8"),J=J$(Z),K=J.content.trim(),L=Buffer.byteLength(K);if(L===0)throw Error("Output style prompt must not be empty");if(L>s1)throw Error(`Output style prompt exceeds ${s1} bytes`);if(a0(K))throw Error("Output style prompt contains hidden control characters");let G=C.relative($,Q),W=L$(G);return{id:`${O.idPrefix}:${W}`,name:Y$(J.data.name,W),description:G$(J.data.description,K,`Custom ${W} communication style`),source:O.source,contentSha256:q$("sha256").update(K,"utf8").digest("hex"),prompt:K}}async function F$(O,$){let Q=await W$(O.directory);if(Q.length===0)return;let q=await D.realpath(O.directory);for(let Z of Q)try{let J=await X$(O,q,Z);$.set(J.id,J)}catch(J){K$.warn(`[CommunicationStyles] Ignoring ${O.source} style: ${J instanceof Error?J.message:String(J)}`)}}function V$(O,$,Q,q){let Z=[{directory:C.join(q,".claude","output-styles"),source:"user",idPrefix:"user"},{directory:C.join(q,".blade","output-styles"),source:"user",idPrefix:"user"}];if($)Z.push({directory:C.join(O,".claude","output-styles"),source:"project",idPrefix:"project"},{directory:C.join(O,".blade","output-styles"),source:"project",idPrefix:"project"});for(let J of Q){if(J.status!=="active"||!a1.test(J.manifest.name))continue;Z.push({directory:C.join(J.basePath,"output-styles"),source:"plugin",idPrefix:`plugin:${J.manifest.name}`})}return Z}async function r0(O,$){let Q=new Map;for(let Z of V$(C.resolve(O),$.projectTrusted,$.plugins??[],$.homeDirectory??Z$.homedir()))await F$(Z,Q);let q=0;for(let Z of Q.values())q+=Buffer.byteLength(Z.prompt??"");if(Q.size>z0)throw Error(`Output style catalog exceeds ${z0} styles`);if(q>n1)throw Error(`Output style catalog exceeds ${n1} bytes`);return new t1([...Q.values()])}import{createHash as H$}from"node:crypto";import{existsSync as x0,realpathSync as U$}from"node:fs";import*as m from"node:fs/promises";import B from"node:path";import S$ from"fast-glob";import j$ from"gray-matter";import ZO from"picomatch";var r1=128,e1=32768,OO=262144,M$=49152,B$=32768,I$=24,$O=16,z$=256,A$=h("Agent"),x$=["**/CLAUDE.md","**/.claude/CLAUDE.md","**/CLAUDE.local.md","**/AGENTS.md","**/AGENTS.override.md","**/BLADE.md","**/.claude/rules/**/*.md","**/.blade/rules/**/*.md"],b$=["**/.git/**","**/node_modules/**","**/.venv/**","**/dist/**","**/build/**","**/coverage/**","**/target/**"];function b0(O){return H$("sha256").update(O,"utf8").digest("hex")}function JO(O){for(let $ of O){let Q=$.codePointAt(0)??0;if(Q<=8||Q===11||Q===12||Q>=14&&Q<=31||Q===127||Q>=8203&&Q<=8207||Q>=8234&&Q<=8238||Q>=8288&&Q<=8303||Q===65279)return!0}return!1}function Q1(O){return O.split(B.sep).join("/")}function e0(O){let $=B.resolve(O),Q=[];while(!0)try{return B.join(U$($),...Q.reverse())}catch(q){if(q.code!=="ENOENT")return B.resolve(O);let Z=B.dirname($);if(Z===$)return B.resolve(O);Q.push(B.basename($)),$=Z}}function _$(O){let $=B.resolve(O),Q;while(!0){if(x0(B.join($,".git")))return $;if(!Q&&(x0(B.join($,".blade"))||x0(B.join($,".claude"))||x0(B.join($,"package.json"))))Q=$;let q=B.dirname($);if(q===$)return Q??B.resolve(O);$=q}}function q1(O){return Boolean(O)&&O!==".."&&!O.startsWith("../")}function O1(O,$){return O===""||$===O||$.startsWith(`${O}/`)}function QO(O){return O.replaceAll("&","&").replaceAll('"',""").replaceAll("<","<").replaceAll(">",">")}function T$(O){let $=typeof O==="string"?O.split(/[,\n]/u):Array.isArray(O)?O:[],Q=[...new Set($.filter((q)=>typeof q==="string").map((q)=>q.trim().replaceAll("\\","/")).filter(Boolean))];if(Q.length===0||Q.every((q)=>q==="**"))return;if(Q.length>$O)throw Error(`paths exceeds ${$O} patterns`);for(let q of Q){if(q.length>z$||B.posix.isAbsolute(q)||q.split("/").includes("..")||JO(q))throw Error(`Unsafe project rule path pattern: ${q.slice(0,80)}`);ZO(q,{dot:!0})}return Q}function w$(O){if(O.endsWith("/CLAUDE.local.md")||O==="CLAUDE.local.md")return{source:"local",kind:"instruction",scopeDirectory:B.posix.dirname(O).replace(/^\.$/u,""),priority:90};for(let q of[".claude/rules/",".blade/rules/"]){let Z=O.indexOf(q);if(Z>=0)return{source:"project",kind:"rule",scopeDirectory:O.slice(0,Z).replace(/\/$/u,""),priority:q.startsWith(".blade")?80:70}}if(O.endsWith("/.claude/CLAUDE.md")||O===".claude/CLAUDE.md")return{source:"project",kind:"instruction",scopeDirectory:O.slice(0,-17).replace(/\/$/u,""),priority:30};let $=B.posix.basename(O),Q=$==="CLAUDE.md"?20:$==="AGENTS.md"?40:$==="AGENTS.override.md"?50:60;return{source:"project",kind:"instruction",scopeDirectory:B.posix.dirname(O).replace(/^\.$/u,""),priority:Q}}function $1(O,$){let Q=O.scopeDirectory?O.scopeDirectory.split("/").length:0,q=$.scopeDirectory?$.scopeDirectory.split("/").length:0;return Q-q||O.priority-$.priority||O.relativePath.localeCompare($.relativePath)}function C$(O){return{id:O.id,relativePath:O.relativePath,source:O.source,contentSha256:O.contentSha256}}function $0(O,$,Q){let q=O.map((K)=>{let L=K.conditional?' conditional="true"':"";return`<instruction-file path="${QO(K.relativePath)}" source="${K.source}" sha256="${K.contentSha256}"${L}>
|
|
679
|
+
(${J} of ${Q} commands shown due to character budget)`;return{text:q,includedCount:J,totalCount:Q}}snapshot(){let O=new w;return O.workspaceRoot=this.workspaceRoot,O.commands=new Map(Array.from(this.commands,([$,Q])=>[$,{...Q,config:{...Q.config,...Q.config.allowedTools?{allowedTools:[...Q.config.allowedTools]}:{}}}])),O.pluginCommands=new Map(Array.from(this.pluginCommands,([$,Q])=>[$,{...Q,config:{...Q.config,...Q.config.allowedTools?{allowedTools:[...Q.config.allowedTools]}:{}}}])),O.initialized=this.initialized,O.lastDiscoveryResult=this.lastDiscoveryResult?{commands:Array.from(O.commands.values()),scannedDirs:[...this.lastDiscoveryResult.scannedDirs],errors:this.lastDiscoveryResult.errors.map(($)=>({...$}))}:null,O}registerPluginCommand(O){this.pluginCommands.set(O.namespacedName,O)}findPluginCommand(O){let $=this.pluginCommands.get(O);if($)return $;let Q=[];for(let q of this.pluginCommands.values())if(q.originalName===O)Q.push(q);if(Q.length===1)return Q[0];return}hasPluginCommand(O){return this.findPluginCommand(O)!==void 0}getAllPluginCommands(){return Array.from(this.pluginCommands.values())}async executePluginCommand(O,$){let Q=this.findPluginCommand(O);if(!Q)return null;let q={name:Q.namespacedName,config:Q.config,content:Q.content,path:Q.path,source:"project",sourceDir:"blade"};return this.executor.execute(q,$)}clearPluginCommands(){this.pluginCommands.clear()}getPluginCommandCount(){return this.pluginCommands.size}hasPluginCommandConflict(O){let $=0;for(let Q of this.pluginCommands.values())if(Q.originalName===O){if($++,$>1)return!0}return!1}getPluginCommandProviders(O){let $=[];for(let Q of this.pluginCommands.values())if(Q.originalName===O)$.push(Q.pluginName);return $}}import Z$ from"node:path";import{satisfies as k0,validRange as A1}from"semver";var CO=/^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$|^[a-z0-9]$/;function d0(O){if(O.bladeVersion!==void 0&&(!O.bladeVersion.trim()||!A1(O.bladeVersion)))throw Error(`Invalid Blade version range in plugin "${O.name}": ${O.bladeVersion}`);for(let[$,Q]of Object.entries(O.dependencies??{})){if(!CO.test($))throw Error(`Invalid dependency name in plugin "${O.name}": ${$}`);if($===O.name)throw Error(`Plugin "${O.name}" cannot depend on itself`);if(!Q.trim()||!A1(Q))throw Error(`Invalid dependency range for ${$} in plugin "${O.name}": ${Q}`)}}function _1(O,$=P0()){if(d0(O),O.bladeVersion&&!k0($,O.bladeVersion))throw Error(`Plugin "${O.name}" requires Blade ${O.bladeVersion}, current ${$}`)}function b1(O,$){O.status="error",O.compatibilityIssues=[...O.compatibilityIssues??[],$],O.error=O.compatibilityIssues.map((Q)=>Q.message).join("; ")}function T1(O,$=P0()){let Q=[],q=new Map(O.map((J)=>[J.manifest.name,J]));for(let J of O){if(J.status!=="active")continue;if(J.manifest.bladeVersion&&!k0($,J.manifest.bladeVersion)){let K={code:"blade-version",expected:J.manifest.bladeVersion,actual:$,message:`Requires Blade ${J.manifest.bladeVersion}, current ${$}`};b1(J,K),Q.push(K)}}let Z=!0;while(Z){Z=!1;for(let J of O){if(J.status!=="active")continue;for(let[K,L]of Object.entries(J.manifest.dependencies??{})){let G=q.get(K),X;if(!G)X={code:"dependency-missing",dependency:K,expected:L,message:`Missing dependency ${K}@${L}`};else if(!k0(G.manifest.version,L))X={code:"dependency-version",dependency:K,expected:L,actual:G.manifest.version,message:`Dependency ${K} requires ${L}, found ${G.manifest.version}`};else if(G.status!=="active")X={code:"dependency-inactive",dependency:K,expected:L,actual:G.status,message:`Dependency ${K} is ${G.status}`};if(!X)continue;b1(J,X),Q.push(X),Z=!0;break}}}return Q}import{execFile as cO}from"node:child_process";import{createHash as lO,randomUUID as k1}from"node:crypto";import{constants as e,promises as H}from"node:fs";import{homedir as g0}from"node:os";import S from"node:path";import{promisify as sO}from"node:util";import{Mutex as nO}from"async-mutex";import{satisfies as u0}from"semver";import oO from"write-file-atomic";import*as F0 from"node:fs/promises";import*as E1 from"node:path";var c=Y.String({minLength:2,maxLength:64,pattern:"^[a-z0-9][a-z0-9-]*[a-z0-9]$|^[a-z0-9]{1,2}$"}),p0=Y.String({pattern:"^\\d+\\.\\d+\\.\\d+(-[\\w.]+)?(\\+[\\w.]+)?$"}),DO=Y.Object({name:Y.String({minLength:1}),email:Y.Optional(Y.String({format:"email"})),url:Y.Optional(Y.String({format:"uri"}))}),D1=Y.Object({name:c,description:Y.String({minLength:1,maxLength:500}),version:p0,author:Y.Optional(DO),license:Y.Optional(Y.String()),repository:Y.Optional(Y.String({format:"uri"})),homepage:Y.Optional(Y.String({format:"uri"})),keywords:Y.Optional(Y.Array(Y.String())),dependencies:Y.Optional(Y.Record(Y.String(),Y.String())),bladeVersion:Y.Optional(Y.String())}),NO=Y.Union([Y.Object({type:Y.Literal("git"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1}))},{additionalProperties:!1}),Y.Object({type:Y.Literal("local"),path:Y.String({minLength:1})},{additionalProperties:!1}),Y.Object({type:Y.Literal("marketplace"),marketplace:c},{additionalProperties:!1})]),RO=Y.Object({name:c,source:NO,installPath:Y.String({minLength:1}),version:p0,revision:Y.String({minLength:1,maxLength:128}),contentDigest:Y.String({pattern:"^[a-f0-9]{64}$"}),installedAt:Y.String({format:"date-time"}),updatedAt:Y.String({format:"date-time"})},{additionalProperties:!1}),vO=Y.Union([Y.Object({type:Y.Literal("git"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1}))},{additionalProperties:!1}),Y.Object({type:Y.Literal("local"),path:Y.String({minLength:1})},{additionalProperties:!1})]),yO=Y.Object({name:c,source:vO,installPath:Y.String({minLength:1}),revision:Y.String({minLength:1,maxLength:128}),contentDigest:Y.String({pattern:"^[a-f0-9]{64}$"}),addedAt:Y.String({format:"date-time"}),updatedAt:Y.String({format:"date-time"})},{additionalProperties:!1}),EO=Y.Object({version:Y.Literal(1),installed:Y.Record(Y.String(),RO),marketplaces:Y.Record(Y.String(),yO)},{additionalProperties:!1}),N1=Y.Object({name:Y.String({minLength:1}),email:Y.Optional(Y.String({format:"email"})),url:Y.Optional(Y.String({format:"uri"}))}),fO=Y.Object({name:c,description:Y.Optional(Y.String({maxLength:500})),version:Y.Optional(p0),author:Y.Optional(Y.Union([Y.String({minLength:1}),N1])),source:Y.Union([Y.String({minLength:1}),Y.Object({source:Y.Literal("url"),url:Y.String({minLength:1}),ref:Y.Optional(Y.String({minLength:1})),sha:Y.Optional(Y.String({pattern:"^[a-fA-F0-9]{40}$"}))})]),category:Y.Optional(Y.String()),homepage:Y.Optional(Y.String({format:"uri"})),tags:Y.Optional(Y.Array(Y.String()))}),hO=Y.Object({name:c,description:Y.Optional(Y.String({maxLength:500})),owner:Y.Optional(N1),metadata:Y.Optional(Y.Object({description:Y.Optional(Y.String({maxLength:500})),version:Y.Optional(Y.String())})),plugins:Y.Array(fO,{maxItems:5000})}),w1=Y.Object({type:C1(["stdio","sse","http"]),command:Y.Optional(Y.String()),args:Y.Optional(Y.Array(Y.String())),env:Y.Optional(Y.Record(Y.String(),Y.String())),cwd:Y.Optional(Y.String()),url:Y.Optional(Y.String({format:"uri"})),headers:Y.Optional(Y.Record(Y.String(),Y.String())),timeout:Y.Optional(Y.Integer({minimum:1000,maximum:1800000})),idleTimeout:Y.Optional(Y.Integer({minimum:1000,maximum:1800000})),sampling:Y.Optional(Y.Object({enabled:Y.Boolean(),maxTokens:Y.Optional(Y.Integer({minimum:1,maximum:4096})),maxRequestsPerToolCall:Y.Optional(Y.Integer({minimum:1,maximum:8})),maxInputBytes:Y.Optional(Y.Integer({minimum:1,maximum:1048576}))})),oauth:Y.Optional(Y.Object({enabled:Y.Optional(Y.Boolean()),clientId:Y.Optional(Y.String({minLength:1,maxLength:4096})),scopes:Y.Optional(Y.Array(Y.String({minLength:1,maxLength:128}),{maxItems:32})),callbackPort:Y.Optional(Y.Integer({minimum:1024,maximum:65535}))},{additionalProperties:!1})),healthCheck:Y.Optional(Y.Object({enabled:Y.Optional(Y.Boolean()),interval:Y.Optional(Y.Number({exclusiveMinimum:0})),timeout:Y.Optional(Y.Number({exclusiveMinimum:0})),failureThreshold:Y.Optional(Y.Number({exclusiveMinimum:0}))}))}),kO=Y.Union([Y.Object({mcpServers:Y.Record(Y.String(),w1)}),Y.Record(Y.String(),w1)]);function R1(O){return u(kO,O)}function v1(O){return u(EO,O)}function y1(O){return u(hO,O)}var PO=[{dir:".blade-plugin",source:"blade"},{dir:".claude-plugin",source:"claude"}];async function a(O){for(let{dir:$,source:Q}of PO){let q=E1.join(O,$,"plugin.json");try{await F0.access(q)}catch{continue}try{let Z=await F0.readFile(q,"utf-8"),J=JSON.parse(Z),K=u(D1,J);if(!K.success){let L=K.error.issues.map((G)=>`${G.path.join(".")}: ${G.message}`).join("; ");throw Error(`Invalid plugin.json: ${L}`)}return d0(K.data),B.debug(`Parsed plugin manifest from ${q}`),{manifest:K.data,source:Q,manifestPath:q}}catch(Z){if(Z instanceof SyntaxError)throw Error(`Invalid JSON in ${q}: ${Z.message}`);throw Z}}return null}import{realpathSync as dO}from"node:fs";import m0 from"node:path";var pO=/^[a-fA-F0-9]{40}$/;class v extends Error{code;constructor(O,$){super($);this.code=O;this.name="PluginSourcePolicyError"}}function mO(O,$){let Q=(Z)=>{try{return dO.native(Z)}catch{return m0.resolve(Z)}},q=m0.relative(Q(O),Q($));return q===""||!q.startsWith("..")&&!m0.isAbsolute(q)}function gO(O){let $=O.match(/^[^@]+@([^:]+):/);if($?.[1])return $[1].toLowerCase();try{return new URL(O).hostname.toLowerCase()}catch{return}}function uO(O,$){if(!$.startsWith("*."))return O===$;let Q=$.slice(1);return O.endsWith(Q)&&O.length>Q.length}function f1(O,$,Q){if(O.requireGitCommitSha&&(!$.ref||!pO.test($.ref)))throw new v("PLUGIN_GIT_SHA_REQUIRED",`${Q} must pin a full 40-character Git commit SHA`);if(!O.restrictToAllowedSources)return;let q=gO($.url);if(!q||!O.allowedGitHosts.some((Z)=>uO(q,Z)))throw new v("PLUGIN_SOURCE_BLOCKED",`${Q} Git host is not allowed by plugin source policy`)}function h1(O,$,Q){if(!O.restrictToAllowedSources)return;if(!O.allowedLocalRoots.some((q)=>mO(q,$)))throw new v("PLUGIN_SOURCE_BLOCKED",`${Q} local path is not allowed by plugin source policy`)}function l(O,$,Q="Marketplace"){if($.type==="git")f1(O,$,Q);else h1(O,$.path,Q)}function r(O,$,Q,q="Plugin"){if($.type==="git"){f1(O,$,q);return}if($.type==="local"){h1(O,$.path,q);return}if(O.restrictToAllowedSources&&!O.allowedMarketplaces.includes($.marketplace))throw new v("PLUGIN_SOURCE_BLOCKED",`Marketplace "${$.marketplace}" is not allowed by plugin source policy`);let Z=Q[$.marketplace];if(!Z)throw new v("MARKETPLACE_NOT_FOUND",`Marketplace "${$.marketplace}" is not configured`);l(O,Z.source,`Marketplace "${$.marketplace}"`)}var iO=sO(cO),tO=1,P1=2097152,d1=1e4,p1=104857600,aO=120000,rO=15000,eO=600000,s0=/^[a-z0-9][a-z0-9-]{0,62}[a-z0-9]$|^[a-z0-9]$/,m1=/^[a-fA-F0-9]{40}$/,O$=[S.join(".blade-plugin","marketplace.json"),S.join(".claude-plugin","marketplace.json"),"marketplace.json"];class F extends Error{code;constructor(O,$){super($);this.code=O;this.name="PluginPackageError"}}function $$(){return{version:tO,installed:{},marketplaces:{}}}function s(O){return O.code==="ENOENT"}function n(O){if(O instanceof F||O instanceof v)return{code:O.code,message:O.message};return{code:"PLUGIN_PACKAGE_ERROR",message:O instanceof Error?O.message:String(O)}}function c0(O){return S.isAbsolute(O)||O.startsWith("./")||O.startsWith("../")||O==="~"||O.startsWith("~/")}function O0(O,$){let Q=S.relative(O,$);return Q===""||!Q.startsWith("..")&&!S.isAbsolute(Q)}function o(O,$="plugin"){if(!s0.test(O))throw new F("INVALID_PLUGIN_NAME",`Invalid ${$} name: ${O}`);return O}function Q$(O){if(O.startsWith("git@"))return;let $=O.lastIndexOf("@");if($<=0||$===O.length-1)return;let Q=O.slice(0,$),q=O.slice($+1);if(!s0.test(Q)||!s0.test(q))return;return{pluginName:Q,marketplace:q}}class g1{userPluginsDir;stateRoot;statePath;mutex=new nO;constructor(O,$){this.userPluginsDir=O??S.join(g0(),".blade","plugins"),this.stateRoot=$??S.join(S.dirname(this.userPluginsDir),"plugin-state"),this.statePath=S.join(this.stateRoot,"state.json")}async install(O,$={}){return this.runMutation(async()=>{let Q=[];try{this.requireSourceTrust($.trusted);let q=await this.readState(),Z=await this.resolvePluginSource(O,q,$),J=this.effectivePolicy($.policy);if(r(J,Z.recordSource,q.marketplaces),Z.expectedName&&q.installed[Z.expectedName])throw new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${Z.expectedName}" is already installed`);let K=await this.preparePluginClosure({state:q,source:Z.source,recordSource:Z.recordSource,expectedName:Z.expectedName,policy:J,updateRoot:!1});Q=K.prepared;let L=await this.commitPreparedPlugins(q,Q),G=L.records.get(K.root.name);if(!G)throw new F("PLUGIN_PACKAGE_ERROR","Root plugin was not committed");let X=Q.map((W)=>W.name).filter((W)=>W!==K.root.name);return B.info(`Installed managed plugin ${K.root.name} at revision ${G.revision}`),{success:!0,pluginName:K.root.name,pluginPath:G.installPath,manifest:K.root.manifest,installation:G,installedDependencies:X,changed:L.changed.has(K.root.name)}}catch(q){await this.cleanupPreparedPlugins(Q);let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async update(O,$={}){return this.runMutation(async()=>{let Q=[];try{this.requireSourceTrust($.trusted),o(O);let q=await this.readState(),Z=q.installed[O];if(!Z)throw new F("PLUGIN_NOT_MANAGED",`Plugin "${O}" is not managed by Blade's package store`);let J=this.effectivePolicy($.policy);r(J,Z.source,q.marketplaces);let K=await this.preparePluginClosure({state:q,source:Z.source,recordSource:Z.source,expectedName:O,policy:J,updateRoot:!0});Q=K.prepared;let L=await this.commitPreparedPlugins(q,Q),G=L.records.get(O);if(!G)throw new F("PLUGIN_PACKAGE_ERROR","Updated root plugin was not committed");let X=Q.map((W)=>W.name).filter((W)=>W!==O&&L.changed.has(W));return B.info(`Updated managed plugin ${O} to revision ${G.revision}`),{success:!0,pluginName:O,pluginPath:G.installPath,manifest:K.root.manifest,installation:G,updatedDependencies:X,changed:L.changed.has(O)}}catch(q){await this.cleanupPreparedPlugins(Q);let Z=n(q);return{success:!1,pluginName:O,code:Z.code,error:Z.message}}})}async uninstall(O,$=!1){return this.runMutation(async()=>{try{if(o(O),!$)throw new F("CONFIRMATION_REQUIRED",`Uninstalling "${O}" requires explicit confirmation`);let Q=await this.readState(),q=Q.installed[O];if(!q)throw new F("PLUGIN_NOT_MANAGED",`Plugin "${O}" is not managed by Blade's package store`);return delete Q.installed[O],await this.writeState(Q),B.info(`Uninstalled managed plugin ${O}`),{success:!0,pluginName:O,installation:q}}catch(Q){let q=n(Q);return{success:!1,pluginName:O,code:q.code,error:q.message}}})}async listInstalled(){return Object.keys((await this.readState()).installed).sort()}async listInstallationRecords(){return Object.values((await this.readState()).installed).sort((O,$)=>O.name.localeCompare($.name))}async getInstallation(O){return(await this.readState()).installed[O]}async verifyInstallation(O){let $=await H.lstat(O.installPath);if(!$.isDirectory()||$.isSymbolicLink())throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" has an invalid package path`);let Q=await H.realpath(this.stateRoot),q=await H.realpath(O.installPath);if(!O0(Q,q))throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" has an invalid package path`);if(await this.digestTree(q)!==O.contentDigest)throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" failed content verification`);let J=await a(q);if(!J||J.manifest.name!==O.name||J.manifest.version!==O.version)throw new F("PLUGIN_PACKAGE_TAMPERED",`Managed plugin "${O.name}" failed manifest verification`)}async addMarketplace(O,$={}){return this.runMutation(async()=>{let Q;try{let q=await this.normalizeMarketplaceSource(O,$);l(this.effectivePolicy($.policy),q);let Z=await this.readState();Q=await this.materializeSource(q);let J=await this.readMarketplaceManifest(Q.stagingPath),K=o(J.name,"marketplace");if(Z.marketplaces[K])throw new F("MARKETPLACE_ALREADY_EXISTS",`Marketplace "${K}" is already configured`);let{revision:L,contentDigest:G}=Q,X=await this.publishPackage("marketplaces",K,Q);Q=void 0;let W=new Date().toISOString(),V={name:K,source:q,installPath:X,revision:L,contentDigest:G,addedAt:W,updatedAt:W};return Z.marketplaces[K]=V,await this.writeState(Z),{success:!0,marketplace:V,manifest:J,changed:!0}}catch(q){if(Q)await H.rm(Q.stagingPath,{recursive:!0,force:!0});let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async refreshMarketplace(O,$){return this.runMutation(async()=>{let Q;try{o(O,"marketplace");let q=await this.readState(),Z=q.marketplaces[O];if(!Z)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O}" is not configured`);l(this.effectivePolicy($),Z.source,`Marketplace "${O}"`),Q=await this.materializeSource(Z.source);let J=await this.readMarketplaceManifest(Q.stagingPath);if(J.name!==O)throw new F("MARKETPLACE_IDENTITY_MISMATCH",`Marketplace "${O}" now declares "${J.name}"`);if(Z.revision===Q.revision&&Z.contentDigest===Q.contentDigest)return await H.rm(Q.stagingPath,{recursive:!0,force:!0}),Q=void 0,{success:!0,marketplace:Z,manifest:J,changed:!1};let{revision:K,contentDigest:L}=Q,G=await this.publishPackage("marketplaces",O,Q);Q=void 0;let X={...Z,installPath:G,revision:K,contentDigest:L,updatedAt:new Date().toISOString()};return q.marketplaces[O]=X,await this.writeState(q),{success:!0,marketplace:X,manifest:J,changed:!0}}catch(q){if(Q)await H.rm(Q.stagingPath,{recursive:!0,force:!0});let Z=n(q);return{success:!1,code:Z.code,error:Z.message}}})}async removeMarketplace(O,$=!1){return this.runMutation(async()=>{try{if(o(O,"marketplace"),!$)throw new F("CONFIRMATION_REQUIRED",`Removing marketplace "${O}" requires explicit confirmation`);let Q=await this.readState(),q=Q.marketplaces[O];if(!q)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O}" is not configured`);let Z=Object.values(Q.installed).filter((J)=>J.source.type==="marketplace"&&J.source.marketplace===O).map((J)=>J.name).sort();if(Z.length>0)throw new F("MARKETPLACE_IN_USE",`Marketplace "${O}" still owns installed plugins: ${Z.join(", ")}`);return delete Q.marketplaces[O],await this.writeState(Q),{success:!0,marketplace:q,changed:!0}}catch(Q){let q=n(Q);return{success:!1,code:q.code,error:q.message}}})}async listMarketplaces(){return Object.values((await this.readState()).marketplaces).sort((O,$)=>O.name.localeCompare($.name))}async listCatalogs(){let O=await this.listMarketplaces();return Promise.all(O.map(async($)=>({marketplace:$,manifest:await this.readMarketplaceManifest($.installPath)})))}async runMutation(O){return this.mutex.runExclusive(async()=>{let $=await this.acquireStoreLock();try{return await O()}finally{await $()}})}async acquireStoreLock(){await this.ensureStoreDirectory();let O=S.join(this.stateRoot,".operation.lock"),$=k1(),Q=Date.now()+rO;while(Date.now()<Q)try{let q=await H.open(O,e.O_CREAT|e.O_EXCL|e.O_WRONLY,384);return await q.writeFile(`${JSON.stringify({version:1,pid:process.pid,token:$,createdAt:new Date().toISOString()})}
|
|
680
|
+
`,"utf8"),await q.close(),async()=>{try{if(JSON.parse(await H.readFile(O,"utf8")).token===$)await H.unlink(O)}catch(Z){if(!s(Z))B.warn(`Failed to release plugin package lock: ${Z}`)}}}catch(q){if(q.code!=="EEXIST")throw q;await this.removeStaleStoreLock(O),await new Promise((Z)=>setTimeout(Z,50))}throw new F("PLUGIN_STORE_BUSY","Plugin package store is busy; retry the operation")}async removeStaleStoreLock(O){try{let $=await H.lstat(O);if(!$.isFile()||$.isSymbolicLink())throw new F("PLUGIN_STORE_INVALID","Plugin package lock must be a regular file");if(process.getuid&&$.uid!==process.getuid()||($.mode&511)!==384)throw new F("PLUGIN_STORE_INVALID","Plugin package lock ownership or permissions are invalid");if(Date.now()-$.mtimeMs<eO)return;await H.unlink(O)}catch($){if(!s($)&&$ instanceof F)throw $}}requireSourceTrust(O){if(O!==!0)throw new F("SOURCE_TRUST_REQUIRED","Plugin installation or update requires explicit source trust")}effectivePolicy(O){return O??H0.pluginSourcePolicy}async preparePluginClosure(O){let $=[],Q=new Map,q=[],Z=(K,L,G,X)=>{if(!u0(L,G))throw new F("DEPENDENCY_VERSION_MISMATCH",`Plugin "${X}" requires ${K}@${G}, found ${L}`)},J=async(K)=>{let L=K.expectedName?O.state.installed[K.expectedName]:void 0;if(L&&!K.root&&K.requiredRange){if(u0(L.version,K.requiredRange))return L;if(K.recordSource.type!=="marketplace"||L.source.type!=="marketplace"||L.source.marketplace!==K.recordSource.marketplace)Z(L.name,L.version,K.requiredRange,K.requiredBy??"dependency");K={...K,source:L.source,recordSource:L.source}}if(L&&K.root&&!O.updateRoot)throw new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${L.name}" is already installed`);if(K.expectedName){let j=Q.get(K.expectedName);if(j){if(K.requiredRange)Z(j.name,j.manifest.version,K.requiredRange,K.requiredBy??"dependency");return j}if(q.includes(K.expectedName))throw new F("DEPENDENCY_CYCLE",`Plugin dependency cycle: ${[...q,K.expectedName].join(" -> ")}`)}let G=await this.materializePluginSource(K.source,O.state,K.expectedName,O.policy),X=await a(G.stagingPath);if(!X)throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),new F("INVALID_PLUGIN","Plugin manifest was not found");let W=o(X.manifest.name);if(K.expectedName&&K.expectedName!==W)throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),new F("PLUGIN_IDENTITY_MISMATCH",`Requested "${K.expectedName}" but source declares "${W}"`);if(q.includes(W))throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),new F("DEPENDENCY_CYCLE",`Plugin dependency cycle: ${[...q,W].join(" -> ")}`);if(K.root&&O.state.installed[W]&&!O.updateRoot)throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),new F("PLUGIN_ALREADY_INSTALLED",`Plugin "${W}" is already installed`);try{_1(X.manifest)}catch(j){throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),new F("VERSION_INCOMPATIBLE",j instanceof Error?j.message:String(j))}let V={name:W,source:K.recordSource,manifest:X.manifest,materialized:G,existing:O.state.installed[W]};q.push(W);try{for(let[j,g]of Object.entries(X.manifest.dependencies??{}).sort(([q0],[N])=>q0.localeCompare(N))){let q0=Q.get(j);if(q0){Z(j,q0.manifest.version,g,W);continue}let N=O.state.installed[j];if(N&&u0(N.version,g))continue;if(K.recordSource.type!=="marketplace")throw new F(N?"DEPENDENCY_VERSION_MISMATCH":"DEPENDENCY_MISSING",N?`Plugin "${W}" requires ${j}@${g}, found ${N.version}`:`Plugin "${W}" requires missing dependency ${j}@${g}; direct sources cannot auto-install dependencies`);let _0=await J({source:N?.source.type==="marketplace"&&N.source.marketplace===K.recordSource.marketplace?N.source:{type:"marketplace",marketplace:K.recordSource.marketplace},recordSource:{type:"marketplace",marketplace:K.recordSource.marketplace},expectedName:j,requiredRange:g,requiredBy:W,root:!1});Z(j,"manifest"in _0?_0.manifest.version:_0.version,g,W)}}catch(j){throw await H.rm(G.stagingPath,{recursive:!0,force:!0}),j}finally{q.pop()}return Q.set(W,V),$.push(V),V};try{let K=await J({source:O.source,recordSource:O.recordSource,expectedName:O.expectedName,root:!0});if(!("manifest"in K))throw new F("PLUGIN_PACKAGE_ERROR","Root plugin unexpectedly resolved to an existing dependency");return{root:K,prepared:$}}catch(K){throw await this.cleanupPreparedPlugins($),K}}async commitPreparedPlugins(O,$){let Q=new Map,q=new Set,Z=new Date().toISOString();for(let J of $){let K=J.existing;if(K?.revision===J.materialized.revision&&K.contentDigest===J.materialized.contentDigest){await H.rm(J.materialized.stagingPath,{recursive:!0,force:!0}),Q.set(J.name,K);continue}let L=await this.publishPackage("packages",J.name,J.materialized),G={name:J.name,source:J.source,installPath:L,version:J.manifest.version,revision:J.materialized.revision,contentDigest:J.materialized.contentDigest,installedAt:K?.installedAt??Z,updatedAt:Z};O.installed[J.name]=G,Q.set(J.name,G),q.add(J.name)}if(q.size>0)await this.writeState(O);return{records:Q,changed:q}}async cleanupPreparedPlugins(O){await Promise.all(O.map(($)=>H.rm($.materialized.stagingPath,{recursive:!0,force:!0})))}async resolvePluginSource(O,$,Q){let q=O.trim();if(!q)throw new F("INVALID_SOURCE","Plugin source is required");let Z=Q$(q);if(Z){if(!$.marketplaces[Z.marketplace])throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${Z.marketplace}" is not configured`);return{source:{type:"marketplace",marketplace:Z.marketplace},recordSource:{type:"marketplace",marketplace:Z.marketplace},expectedName:Z.pluginName}}if(!c0(q)&&!this.looksLikeGitSource(q)){let K=[];for(let L of await this.catalogsFromState($))if(L.manifest.plugins.some((G)=>G.name===q))K.push({pluginName:q,marketplace:L.marketplace.name});if(K.length===1)return{source:{type:"marketplace",marketplace:K[0].marketplace},recordSource:{type:"marketplace",marketplace:K[0].marketplace},expectedName:q};if(K.length>1)throw new F("AMBIGUOUS_PLUGIN_SOURCE",`Plugin "${q}" exists in multiple marketplaces; use name@marketplace`)}if(c0(q)){let L={type:"local",path:await this.resolveLocalSource(q,Q.workspaceRoot)};return{source:L,recordSource:L}}let J=this.normalizeGitSource(q,Q.ref);return{source:J,recordSource:J}}async materializePluginSource(O,$,Q,q){if(O.type!=="marketplace")return this.materializeSource(O);let Z=$.marketplaces[O.marketplace];if(!Z)throw new F("MARKETPLACE_NOT_FOUND",`Marketplace "${O.marketplace}" is not configured`);let J=await this.readMarketplaceManifest(Z.installPath),K=Q?J.plugins.filter((L)=>L.name===Q):J.plugins;if(K.length!==1)throw new F("MARKETPLACE_PLUGIN_NOT_FOUND",Q?`Plugin "${Q}" was not found in marketplace "${O.marketplace}"`:`Marketplace "${O.marketplace}" requires an explicit plugin name`);return this.materializeMarketplaceEntry(Z,K[0],q)}async materializeMarketplaceEntry(O,$,Q){if(typeof $.source!=="string"){let K=this.normalizeGitSource($.source.url,$.source.sha??$.source.ref);return l(Q,K,`Plugin "${$.name}"`),this.materializeSource(K)}if($.source.startsWith("https://")||$.source.startsWith("ssh://")||$.source.startsWith("git@")){let K=this.normalizeGitSource($.source);return l(Q,K,`Plugin "${$.name}"`),this.materializeSource(K)}let q=await H.realpath(O.installPath),Z=S.resolve(q,$.source);if(!O0(q,Z)||Z===q)throw new F("MARKETPLACE_PATH_ESCAPE",`Plugin "${$.name}" source escapes its marketplace`);let J=await H.realpath(Z);if(!O0(q,J))throw new F("MARKETPLACE_PATH_ESCAPE",`Plugin "${$.name}" source escapes its marketplace`);return this.materializeSource({type:"local",path:J})}async normalizeMarketplaceSource(O,$){let Q=O.trim();if(!Q)throw new F("INVALID_SOURCE","Marketplace source is required");if(c0(Q))return{type:"local",path:await this.resolveLocalSource(Q,$.workspaceRoot)};return this.normalizeGitSource(Q,$.ref)}normalizeGitSource(O,$){let Q=O;if(/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(O))Q=`https://github.com/${O}.git`;else if(O.startsWith("git@")){if(!/^git@[A-Za-z0-9.-]+:[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+(?:\.git)?$/.test(O))throw new F("INVALID_SOURCE","Invalid SSH Git source")}else{let q;try{q=new URL(O)}catch{throw new F("INVALID_SOURCE","Invalid Git source")}if(q.protocol!=="https:"&&q.protocol!=="ssh:")throw new F("UNSAFE_GIT_PROTOCOL","Git sources must use HTTPS or SSH");if(q.password||q.protocol==="https:"&&q.username)throw new F("GIT_CREDENTIALS_IN_URL","Git credentials must not be embedded in plugin source URLs");if(q.hash||q.search)throw new F("INVALID_SOURCE","Use the ref option instead of URL query or fragment parameters");Q=q.toString()}if($&&(!$.trim()||$.startsWith("-")||$.includes("\x00")))throw new F("INVALID_GIT_REF","Invalid Git ref");return{type:"git",url:Q,...$?{ref:$}:{}}}looksLikeGitSource(O){return O.startsWith("https://")||O.startsWith("ssh://")||O.startsWith("git@")||/^[A-Za-z0-9_.-]+\/[A-Za-z0-9_.-]+$/.test(O)}async resolveLocalSource(O,$){let Q=O==="~"?g0():O.startsWith("~/")?S.join(g0(),O.slice(2)):O,q=S.resolve($??process.cwd(),Q),Z;try{Z=await H.realpath(q)}catch{throw new F("LOCAL_SOURCE_NOT_FOUND",`Local source does not exist: ${O}`)}let J=await H.lstat(Z);if(!J.isDirectory()||J.isSymbolicLink())throw new F("INVALID_LOCAL_SOURCE","Local plugin sources must be regular directories");return Z}async materializeSource(O){await this.ensureStoreDirectory();let $=S.join(this.stateRoot,"staging",k1());if(await H.mkdir(S.dirname($),{recursive:!0,mode:448}),O.type==="git"){await this.cloneGitSource(O,$);let q=await this.runGit(["rev-parse","HEAD"],$);if(O.ref&&m1.test(O.ref)&&q.toLowerCase()!==O.ref.toLowerCase())throw await H.rm($,{recursive:!0,force:!0}),new F("GIT_PIN_MISMATCH","Plugin checkout does not match the requested Git commit SHA");await H.rm(S.join($,".git"),{recursive:!0,force:!0});let Z=await this.digestTree($);return{stagingPath:$,revision:q,contentDigest:Z}}await this.copyLocalSource(O.path,$);let Q=await this.digestTree($);return{stagingPath:$,revision:`local-${Q.slice(0,40)}`,contentDigest:Q}}async cloneGitSource(O,$){try{if(O.ref&&m1.test(O.ref))await H.mkdir($,{recursive:!0,mode:448}),await this.runGit(["init"],$),await this.runGit(["remote","add","origin",O.url],$),await this.runGit(["fetch","--depth","1","origin",O.ref],$),await this.runGit(["checkout","--detach","FETCH_HEAD"],$);else{let Q=["clone","--depth","1","--no-tags"];if(O.ref)Q.push("--branch",O.ref);Q.push("--",O.url,$),await this.runGit(Q)}}catch(Q){throw await H.rm($,{recursive:!0,force:!0}),new F("GIT_CLONE_FAILED",`Failed to clone plugin source: ${this.sanitizeError(Q)}`)}}async runGit(O,$){let Q=await iO("git",O,{cwd:$,encoding:"utf8",timeout:aO,maxBuffer:4194304,env:{...process.env,GIT_TERMINAL_PROMPT:"0"}}),q=typeof Q==="string"?Q:Q.stdout;if(q===void 0)throw Error("Git process returned an invalid result");return String(q).trim()}sanitizeError(O){return(O instanceof Error?O.message:String(O)).replaceAll(this.stateRoot,"<plugin-store>").slice(0,500)}async copyLocalSource(O,$){await this.digestTree(O),await H.cp(O,$,{recursive:!0,force:!1,errorOnExist:!0,filter:(Q)=>{return!S.relative(O,Q).split(S.sep).includes(".git")}})}async digestTree(O){let $=[],Q=0,q=async(J)=>{let K=await H.readdir(J,{withFileTypes:!0});K.sort((L,G)=>L.name.localeCompare(G.name));for(let L of K){if(L.name===".git")continue;let G=S.join(J,L.name),X=S.relative(O,G),W=await H.lstat(G);if(W.isSymbolicLink())throw new F("PACKAGE_SYMLINK_FORBIDDEN",`Plugin packages may not contain symbolic links: ${X}`);if(W.isDirectory()){await q(G);continue}if(!W.isFile())throw new F("PACKAGE_ENTRY_FORBIDDEN",`Plugin packages may contain only files and directories: ${X}`);if(Q+=W.size,$.push({relative:X,absolute:G,executable:(W.mode&73)!==0}),$.length>d1||Q>p1)throw new F("PACKAGE_TOO_LARGE",`Plugin package exceeds ${d1} files or ${p1} bytes`)}};await q(O);let Z=lO("sha256");for(let J of $)Z.update(J.relative),Z.update("\x00"),Z.update(J.executable?"x":"-"),Z.update("\x00"),Z.update(await H.readFile(J.absolute)),Z.update("\x00");return Z.digest("hex")}async publishPackage(O,$,Q){let q=S.join(this.stateRoot,O,$,Q.contentDigest);await H.mkdir(S.dirname(q),{recursive:!0,mode:448});try{let Z=await H.lstat(q);if(!Z.isDirectory()||Z.isSymbolicLink())throw new F("PACKAGE_STORE_CONFLICT","Plugin package destination is not a regular directory");return await H.rm(Q.stagingPath,{recursive:!0,force:!0}),q}catch(Z){if(!s(Z))throw Z}return await H.rename(Q.stagingPath,q),q}async readMarketplaceManifest(O){for(let $ of O$){let Q=S.join(O,$);try{let q=await H.lstat(Q);if(!q.isFile()||q.isSymbolicLink()||q.size>P1)throw new F("INVALID_MARKETPLACE","Marketplace manifest must be a regular bounded file");let Z=JSON.parse(await H.readFile(Q,"utf8")),J=y1(Z);if(!J.success)throw new F("INVALID_MARKETPLACE",`Invalid marketplace manifest: ${J.error.issues.map((L)=>`${L.path.join(".")}: ${L.message}`).join("; ")}`);let K=new Set;for(let L of J.data.plugins){if(K.has(L.name))throw new F("INVALID_MARKETPLACE",`Marketplace contains duplicate plugin: ${L.name}`);K.add(L.name)}return J.data}catch(q){if(s(q))continue;if(q instanceof SyntaxError)throw new F("INVALID_MARKETPLACE","Marketplace manifest contains invalid JSON");throw q}}throw new F("INVALID_MARKETPLACE","Marketplace manifest was not found")}async catalogsFromState(O){let $=Object.values(O.marketplaces).sort((Q,q)=>Q.name.localeCompare(q.name));return Promise.all($.map(async(Q)=>({marketplace:Q,manifest:await this.readMarketplaceManifest(Q.installPath)})))}async readState(){let O;try{O=await H.open(this.statePath,e.O_RDONLY|e.O_NOFOLLOW)}catch($){if(s($))return $$();throw new F("PLUGIN_STATE_INVALID","Plugin package state must be a regular file")}try{let $=await O.stat();if(!$.isFile()||$.size>P1)throw new F("PLUGIN_STATE_INVALID","Plugin package state is invalid");if(process.getuid&&$.uid!==process.getuid())throw new F("PLUGIN_STATE_INVALID","Plugin package state must be owned by the current user");if(($.mode&511)!==384)throw new F("PLUGIN_STATE_INVALID","Plugin package state permissions must be 0600");let Q=JSON.parse(await O.readFile("utf8")),q=v1(Q);if(!q.success)throw new F("PLUGIN_STATE_INVALID","Plugin package state schema is invalid");let Z=q.data;for(let[J,K]of Object.entries(Z.installed)){let L=S.join(this.stateRoot,"packages",J);if(J!==K.name||!S.isAbsolute(K.installPath)||!O0(L,K.installPath)||S.basename(K.installPath)!==K.contentDigest||K.source.type==="local"&&!S.isAbsolute(K.source.path))throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid installation");if(K.source.type==="git"){let G=this.normalizeGitSource(K.source.url,K.source.ref);if(G.url!==K.source.url||G.ref!==K.source.ref)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid Git source")}}for(let[J,K]of Object.entries(Z.marketplaces)){let L=S.join(this.stateRoot,"marketplaces",J);if(J!==K.name||!S.isAbsolute(K.installPath)||!O0(L,K.installPath)||S.basename(K.installPath)!==K.contentDigest||K.source.type==="local"&&!S.isAbsolute(K.source.path))throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid marketplace");if(K.source.type==="git"){let G=this.normalizeGitSource(K.source.url,K.source.ref);if(G.url!==K.source.url||G.ref!==K.source.ref)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains an invalid Marketplace Git source")}}for(let J of Object.values(Z.installed))if(J.source.type==="marketplace"&&!Z.marketplaces[J.source.marketplace])throw new F("PLUGIN_STATE_INVALID","Plugin package state references a missing marketplace");return Z}catch($){if($ instanceof SyntaxError)throw new F("PLUGIN_STATE_INVALID","Plugin package state contains invalid JSON");throw $}finally{await O.close()}}async writeState(O){await this.ensureStoreDirectory(),await oO(this.statePath,`${JSON.stringify(O,null,2)}
|
|
681
|
+
`,{mode:384,encoding:"utf8"}),await H.chmod(this.statePath,384)}async ensureStoreDirectory(){try{let O=await H.lstat(this.stateRoot);if(!O.isDirectory()||O.isSymbolicLink())throw new F("PLUGIN_STORE_INVALID","Plugin package store must be a regular directory");if(process.getuid&&O.uid!==process.getuid())throw new F("PLUGIN_STORE_INVALID","Plugin package store must be owned by the current user");if((O.mode&511)!==448)throw new F("PLUGIN_STORE_INVALID","Plugin package store permissions must be 0700")}catch(O){if(!s(O))throw O;await H.mkdir(this.stateRoot,{recursive:!0,mode:448}),await H.chmod(this.stateRoot,448)}}}var l0=null;function n0(O,$){if(!l0)l0=new g1(O,$);return l0}import*as b from"node:fs/promises";import{homedir as q$}from"node:os";import*as z from"node:path";import c1 from"gray-matter";function V0(O,$){return`${O}:${$}`}function u1(O,$){return`${O}__${$}`}class U0{async loadPlugin(O,$,Q={}){let q=await a(O);if(!q)throw Error(`Not a valid plugin directory: ${O}`);let{manifest:Z,source:J}=q,K=Z.name;B.debug(`Loading plugin "${K}" from ${O}`);let L=Q.skipCommands?[]:await this.loadCommands(O,K),G=Q.skipAgents?[]:await this.loadAgents(O,K),X=Q.skipSkills?[]:await this.loadSkills(O,K),W=Q.skipHooks?void 0:await this.loadHooks(O),V=Q.skipMcp?void 0:await this.loadMcpConfig(O,K),j=Q.skipLsp?void 0:await this.loadLspConfig(O,K);return B.debug(`Plugin "${K}" loaded: ${L.length} commands, ${G.length} agents, ${X.length} skills`),{manifest:Z,basePath:O,source:$,manifestSource:J,commands:L,agents:G,skills:X,hooks:W,mcpServers:V,lspServers:j,status:"active",loadedAt:new Date}}async loadCommands(O,$){let Q=z.join(O,"commands"),q=[];if(!await this.dirExists(Q))return q;let Z=await this.scanMarkdownFiles(Q);for(let J of Z)try{let K=await this.parseCommandFile(J,Q,$);if(K)q.push(K)}catch(K){B.warn(`Failed to load command from ${J}: ${K instanceof Error?K.message:String(K)}`)}return q}async parseCommandFile(O,$,Q){let q=await b.readFile(O,"utf-8"),{data:Z,content:J}=c1(q),L=z.relative($,O).split(z.sep),G=L.pop();if(!G)return null;let X=G.replace(/\.md$/i,""),W=X;if(L.length>0)W=[...L,X].join("/");let V=V0(Q,W),j=this.normalizeCommandConfig(Z);return{originalName:W,namespacedName:V,pluginName:Q,config:j,content:J.trim(),path:O}}normalizeCommandConfig(O){return{description:this.asString(O.description),allowedTools:this.parseStringArray(O["allowed-tools"]),argumentHint:this.asString(O["argument-hint"]),model:this.asString(O.model),disableModelInvocation:O["disable-model-invocation"]===!0}}async loadAgents(O,$){let Q=z.join(O,"agents"),q=[];if(!await this.dirExists(Q))return q;let Z=await this.scanMarkdownFiles(Q);for(let J of Z)try{let K=await this.parseAgentFile(J,Q,$);if(K)q.push(K)}catch(K){B.warn(`Failed to load agent from ${J}: ${K instanceof Error?K.message:String(K)}`)}return q}async parseAgentFile(O,$,Q){let q=await b.readFile(O,"utf-8"),{data:Z,content:J}=c1(q),K=z.relative($,O),L=z.basename(K,".md"),G=Z.name||L,X=V0(Q,G),W={name:X,description:this.asString(Z.description)||"",tools:this.parseStringArray(Z.tools),color:this.asString(Z.color),model:this.asString(Z.model),permissionMode:this.asString(Z.permissionMode),skills:this.parseStringArray(Z.skills),systemPrompt:J.trim(),source:`plugin:${Q}`};return{originalName:G,namespacedName:X,pluginName:Q,config:W,path:O}}async loadSkills(O,$){let Q=z.join(O,"skills"),q=[];if(!await this.dirExists(Q))return q;let Z=await b.readdir(Q,{withFileTypes:!0});for(let J of Z){if(!J.isDirectory())continue;let K=z.join(Q,J.name),L=z.join(K,"SKILL.md");try{let G=await X0(L,"project");if(G.success&&G.content){let X=G.content.metadata.name,W=V0($,X);q.push({originalName:X,namespacedName:W,pluginName:$,metadata:{...G.content.metadata,name:W},path:K})}}catch(G){B.warn(`Failed to load skill from ${L}: ${G instanceof Error?G.message:String(G)}`)}}return q}async loadHooks(O){let $=z.join(O,"hooks","hooks.json");try{let Q=await b.readFile($,"utf-8"),q=JSON.parse(Q);return q.hooks||q}catch{return}}async loadMcpConfig(O,$){let Q=z.join(O,".mcp.json");try{let q=await b.readFile(Q,"utf-8"),Z=JSON.parse(q),J=R1(Z);if(!J.success){B.warn(`Invalid .mcp.json in ${O}: ${J.error}`);return}let K="mcpServers"in Z?Z.mcpServers:Z,L={};for(let[G,X]of Object.entries(K)){let W=u1($,G);L[W]=X}return L}catch{return}}async loadLspConfig(O,$){let Q=z.join(O,".lsp.json");try{let q=JSON.parse(await b.readFile(Q,"utf8")),Z=q&&typeof q==="object"&&!Array.isArray(q)&&"lspServers"in q?q.lspServers:q,J=l1(Z),K={};for(let[L,G]of Object.entries(J)){let X=(W)=>W.replaceAll("${CLAUDE_PLUGIN_ROOT}",O).replaceAll("${BLADE_PLUGIN_ROOT}",O);K[`plugin:${$}:${L}`]={...G,command:X(G.command),args:G.args?.map(X),env:G.env?Object.fromEntries(Object.entries(G.env).map(([W,V])=>[W,X(V)])):void 0}}return K}catch(q){if(q.code!=="ENOENT")B.warn(`Invalid .lsp.json in ${O}: ${q instanceof Error?q.message:String(q)}`);return}}async scanMarkdownFiles(O){let $=[],Q=async(q)=>{let Z=await b.readdir(q,{withFileTypes:!0});for(let J of Z){let K=z.join(q,J.name);if(J.isDirectory())await Q(K);else if(J.name.endsWith(".md"))$.push(K)}};return await Q(O),$}async dirExists(O){try{return(await b.stat(O)).isDirectory()}catch{return!1}}asString(O){if(typeof O==="string"&&O.trim())return O.trim();return}parseStringArray(O){if(!O)return;if(Array.isArray(O))return O.map(($)=>String($).trim()).filter(Boolean);if(typeof O==="string")return O.split(",").map(($)=>$.trim()).filter(Boolean);return}static getPluginDirs(O){let $=q$();return[{path:z.join($,".claude","plugins"),source:"user",type:"claude"},{path:z.join($,".blade","plugins"),source:"user",type:"blade"},{path:z.join(O,".claude","plugins"),source:"project",type:"claude"},{path:z.join(O,".blade","plugins"),source:"project",type:"blade"}]}async discoverPluginsInDir(O,$){let Q=[],q=[];try{await b.access(O)}catch{return{plugins:Q,errors:q}}let Z=await b.readdir(O,{withFileTypes:!0});for(let J of Z){if(!J.isDirectory())continue;let K=z.join(O,J.name);try{let L=await this.loadPlugin(K,$);Q.push(L)}catch(L){q.push({path:K,error:L instanceof Error?L.message:String(L)})}}return{plugins:Q,errors:q}}}class f{static instances=new Map;plugins=new Map;loader=new U0;initialized=!1;workspaceRoot="";cliPluginDirs=[];enabledSettings={};sourcePolicy={...H0.pluginSourcePolicy};marketplaces={};constructor(O){this.workspaceRoot=O}static getInstance(O=A()){let $=Z$.resolve(O),Q=f.instances.get($);if(!Q)Q=new f($),f.instances.set($,Q);return Q}static resetInstance(){f.instances.clear()}static getInitializedInstances(){return Array.from(f.instances.values()).filter((O)=>O.isInitialized())}async initialize(O,$=[]){this.workspaceRoot=O,this.cliPluginDirs=$,this.enabledSettings=await S0.getInstance().loadWorkspacePluginSettings(O),await this.refreshPolicyContext();let Q=[],q=[];for(let G of $)try{let X=await this.loader.loadPlugin(G,"cli");this.plugins.set(X.manifest.name,X),Q.push(X),B.info(`Loaded CLI plugin: ${X.manifest.name} from ${G}`)}catch(X){q.push({path:G,error:X instanceof Error?X.message:String(X)}),B.warn(`Failed to load CLI plugin from ${G}: ${X}`)}let Z=(await T.getInstance().getStatus(O)).state==="trusted",J=U0.getPluginDirs(O).filter((G)=>G.source!=="project"||Z),K=!1,L=async()=>{if(K)return;K=!0;let G=n0(),X=await G.listInstallationRecords();for(let W of X)try{await G.verifyInstallation(W);let V=await this.loader.loadPlugin(W.installPath,"user");if(V.manifest.name!==W.name)throw Error(`Managed plugin identity mismatch: ${W.name}`);V.installation=Object.freeze({...W});let j=this.plugins.get(V.manifest.name);if(!j||j.source==="user")this.plugins.set(V.manifest.name,V),Q.push(V)}catch(V){q.push({path:W.installPath,code:"IO_ERROR",error:V instanceof Error?V.message:String(V)})}};for(let{path:G,source:X}of J){if(X==="project")await L();let W=await this.loader.discoverPluginsInDir(G,X);for(let V of W.plugins){let j=this.plugins.get(V.manifest.name);if(!j||j.source!=="cli")this.plugins.set(V.manifest.name,V),Q.push(V)}q.push(...W.errors)}await L(),this.applyEnabledSettings();for(let G of this.plugins.values()){if(G.status!=="error")continue;q.push({path:G.basePath,code:G.compatibilityIssues?.some((X)=>X.code==="source-policy")?"SOURCE_POLICY_BLOCKED":G.compatibilityIssues?.some((X)=>X.code==="blade-version")?"VERSION_INCOMPATIBLE":"DEPENDENCY_MISSING",error:G.error??"Plugin compatibility check failed"})}return this.initialized=!0,B.info(`Plugin system initialized: ${this.plugins.size} plugins loaded`+(q.length>0?`, ${q.length} errors`:"")),{plugins:Array.from(this.plugins.values()),errors:q}}isInitialized(){return this.initialized}getWorkspaceRoot(){return this.workspaceRoot}getSourcePolicy(){return{...this.sourcePolicy,allowedGitHosts:[...this.sourcePolicy.allowedGitHosts],allowedMarketplaces:[...this.sourcePolicy.allowedMarketplaces],allowedLocalRoots:[...this.sourcePolicy.allowedLocalRoots]}}getAll(){return Array.from(this.plugins.values())}getActive(){return Array.from(this.plugins.values()).filter((O)=>O.status==="active")}get(O){return this.plugins.get(O)}has(O){return this.plugins.has(O)}async reapplyEnabledSettings(){this.enabledSettings=await S0.getInstance().loadWorkspacePluginSettings(this.workspaceRoot),await this.refreshPolicyContext(),this.applyEnabledSettings()}applyEnabledSettings(){for(let O of this.plugins.values()){if(O.compatibilityIssues=[],O.error=void 0,O.status=O.source==="cli"||this.enabledSettings[O.manifest.name]!==!1?"active":"inactive",O.status!=="active")continue;try{r(this.sourcePolicy,O.installation?.source??{type:"local",path:O.basePath},this.marketplaces,`Plugin "${O.manifest.name}"`)}catch($){if(!($ instanceof v))throw $;O.status="error",O.error=$.message,O.compatibilityIssues=[{code:"source-policy",message:$.message}]}}T1(Array.from(this.plugins.values()))}async refreshPolicyContext(){this.sourcePolicy=await S0.getInstance().loadWorkspacePluginSourcePolicy(this.workspaceRoot),this.marketplaces=Object.fromEntries((await n0().listMarketplaces()).map((O)=>[O.name,O]))}getBySource(){let O={cli:[],project:[],user:[]};for(let $ of this.plugins.values())O[$.source].push($);return O}getAllCommands(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.commands);return O}getAllSkills(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.skills);return O}getAllAgents(){let O=[];for(let $ of this.plugins.values())if($.status==="active")O.push(...$.agents);return O}findCommand(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}findSkill(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.skills)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.skills)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}findAgent(O){for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.agents)if(q.namespacedName===O)return q}let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.agents)if(q.originalName===O)$.push(q)}if($.length===1)return $[0];return}hasCommandConflict(O){let $=0;for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O){if($++,$>1)return!0}}return!1}getCommandProviders(O){let $=[];for(let Q of this.plugins.values()){if(Q.status!=="active")continue;for(let q of Q.commands)if(q.originalName===O){$.push(Q.manifest.name);break}}return $}disable(O){let $=this.plugins.get(O);if($&&$.status==="active")return $.status="inactive",B.info(`Plugin "${O}" disabled`),!0;return!1}enable(O){let $=this.plugins.get(O);if($&&$.status==="inactive")return $.status="active",B.info(`Plugin "${O}" enabled`),!0;return!1}async refresh(){return B.info("Refreshing plugin list..."),this.plugins.clear(),this.initialized=!1,this.initialize(this.workspaceRoot,this.cliPluginDirs)}getStats(){let O=0,$=0,Q=0,q=0,Z=0;for(let J of this.plugins.values())if(J.status==="active")O++,Q+=J.commands.length,q+=J.skills.length,Z+=J.agents.length;else $++;return{total:this.plugins.size,active:O,inactive:$,commands:Q,skills:q,agents:Z}}formatPluginList(){if(this.getAll().length===0)return"没有已加载的插件。";let $=[],Q=this.getBySource();if(Q.cli.length>0){$.push("## CLI 指定的插件");for(let q of Q.cli){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}$.push("")}if(Q.project.length>0){$.push("## 项目级插件");for(let q of Q.project){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}$.push("")}if(Q.user.length>0){$.push("## 用户级插件");for(let q of Q.user){let Z=q.status==="inactive"?" (禁用)":"";$.push(`- **${q.manifest.name}** v${q.manifest.version}${Z}`),$.push(` ${q.manifest.description}`)}}return $.join(`
|
|
682
|
+
`)}}function j0(O=A()){return f.getInstance(O)}var d=new Map,s1=Object.values(B0);function o0(O){let $={...O};for(let Q of s1)$[Q]=(O[Q]??[]).map((q)=>({...q,matcher:q.matcher?{...q.matcher}:void 0,hooks:q.hooks.map((Z)=>({...Z}))}));return $}class i0{commandRegistry;hookManager;skillRegistry;subagentRegistry;workspaceRoot;constructor(O=A()){this.workspaceRoot=J$.resolve(O),this.commandRegistry=w.getInstance(this.workspaceRoot),this.hookManager=M0.getInstance(),this.skillRegistry=P({cwd:this.workspaceRoot}),this.subagentRegistry=i(this.workspaceRoot)}clearAllPluginResources(){this.commandRegistry.clearPluginCommands(),this.skillRegistry.clearPluginSkills(),this.subagentRegistry.clearPluginAgents();let O=d.get(this.workspaceRoot);if(O)this.hookManager.loadConfig(O,this.workspaceRoot),d.delete(this.workspaceRoot);B.debug("Cleared all plugin resources from subsystems")}async integrateAll(){let $=j0(this.workspaceRoot).getActive(),Q=[],q=[],Z=0,J=0,K=0,L=0,G=0;for(let X of $){let W=this.integratePlugin(X);Q.push(W),Z+=W.commandsRegistered,J+=W.skillsRegistered,K+=W.agentsRegistered,L+=W.mcpServersRegistered,G+=W.lspServersRegistered,q.push(...W.errors)}if(this.integrateHooks($),Z+J+K>0)B.info(`Plugin integration complete: ${Z} commands, ${J} skills, ${K} agents, ${L} MCP servers, ${G} LSP servers`);return{plugins:Q,totalCommands:Z,totalSkills:J,totalAgents:K,totalMcpServers:L,errors:q}}integratePlugin(O){let $={pluginName:O.manifest.name,commandsRegistered:0,skillsRegistered:0,agentsRegistered:0,hooksRegistered:!1,mcpServersRegistered:0,lspServersRegistered:0,errors:[]};try{$.commandsRegistered=this.integrateCommands(O)}catch(Q){$.errors.push(`Failed to integrate commands: ${Q instanceof Error?Q.message:String(Q)}`)}try{$.skillsRegistered=this.integrateSkills(O)}catch(Q){$.errors.push(`Failed to integrate skills: ${Q instanceof Error?Q.message:String(Q)}`)}try{$.agentsRegistered=this.integrateAgents(O)}catch(Q){$.errors.push(`Failed to integrate agents: ${Q instanceof Error?Q.message:String(Q)}`)}$.hooksRegistered=O.hooks!==void 0;try{$.mcpServersRegistered=this.integrateMcp(O)}catch(Q){$.errors.push(`Failed to integrate MCP servers: ${Q instanceof Error?Q.message:String(Q)}`)}return $.lspServersRegistered=Object.keys(O.lspServers??{}).length,B.debug(`Integrated plugin "${O.manifest.name}": ${$.commandsRegistered} commands, ${$.skillsRegistered} skills, ${$.agentsRegistered} agents`),$}integrateCommands(O){let $=0;for(let Q of O.commands)this.commandRegistry.registerPluginCommand(Q),$++;return $}integrateSkills(O){let $=0;for(let Q of O.skills)this.skillRegistry.registerPluginSkill(Q),$++;return $}integrateAgents(O){let $=0;for(let Q of O.agents)this.subagentRegistry.register({...Q.config,name:Q.namespacedName}),$++;return $}integrateHooks(O){let $=O.filter((J)=>J.hooks);if($.length===0){let J=d.get(this.workspaceRoot);if(J)this.hookManager.loadConfig(J,this.workspaceRoot),d.delete(this.workspaceRoot);return}let Q=o0(this.hookManager.getConfig(this.workspaceRoot));if(!d.has(this.workspaceRoot))d.set(this.workspaceRoot,o0(Q));let q=d.get(this.workspaceRoot)??Q,Z=o0(q);Z.enabled=!0;for(let J of s1){let K=q.enabled?[...q[J]??[]]:[],L=[];for(let G of $)for(let X of G.hooks?.[J]??[])L.push({...X,name:X.name?`plugin:${G.manifest.name}:${X.name}`:`plugin:${G.manifest.name}:${J}`,hooks:X.hooks.map((W)=>({...W,source:{kind:"plugin",pluginName:G.manifest.name,pluginSource:G.source,pluginRoot:G.basePath}}))});Z[J]=[...K,...L]}this.hookManager.loadConfig(Z,this.workspaceRoot)}integrateMcp(O){return Object.keys(O.mcpServers??{}).length}}async function t0(O=A()){return new i0(O).integrateAll()}function z0(O=A()){new i0(O).clearAllPluginResources()}import{createHash as K$}from"node:crypto";import*as D from"node:fs/promises";import G$ from"node:os";import C from"node:path";import Y$ from"gray-matter";var I0=32,n1=24576,o1=16384,i1=131072,x0=4,t1=80,a1=256,e1=/^[a-z0-9][a-z0-9._-]{0,63}$/,L$=h("Agent");function a0(O){for(let $ of O){let Q=$.codePointAt(0)??0;if(Q<=8||Q===11||Q===12||Q>=14&&Q<=31||Q===127||Q>=8203&&Q<=8207||Q>=8234&&Q<=8238||Q>=8288&&Q<=8303||Q===65279)return!0}return!1}function X$(O,$,Q){let q=typeof O==="string"?O.trim():"",Z=$.split(/\r?\n/).map((K)=>K.trim()).find(Boolean)?.replace(/^#+\s*/,"")??"",J=q||Z||Q;if(J.length>a1)throw Error(`Description exceeds ${a1} characters`);if(a0(J))throw Error("Description contains hidden control characters");return J}function W$(O,$){let Q=typeof O==="string"&&O.trim()?O.trim():$;if(Q.length>t1)throw Error(`Name exceeds ${t1} characters`);if(a0(Q))throw Error("Name contains hidden control characters");return Q}function F$(O){let Q=O.replace(/\.md$/i,"").split(C.sep);if(Q.length===0||Q.length>x0||Q.some((q)=>!e1.test(q)))throw Error(`Style path must use 1-${x0} lowercase safe segments`);return Q.join(":")}async function H$(O){let $;try{$=await D.lstat(O)}catch(J){if(J.code==="ENOENT")return[];throw J}if($.isSymbolicLink())throw Error(`Output style root cannot be a symlink: ${O}`);if(!$.isDirectory())return[];let Q=await D.realpath(O),q=[],Z=async(J,K)=>{if(K>x0)throw Error(`Output style directory exceeds depth ${x0}`);let L=await D.readdir(J,{withFileTypes:!0});L.sort((G,X)=>G.name.localeCompare(X.name));for(let G of L){let X=C.join(J,G.name);if(G.isSymbolicLink())throw Error(`Output style path cannot be a symlink: ${X}`);if(G.isDirectory()){await Z(X,K+1);continue}if(!G.isFile()||!G.name.toLowerCase().endsWith(".md"))continue;let W=await D.realpath(X),V=C.relative(Q,W);if(!V||V.startsWith("..")||C.isAbsolute(V))throw Error(`Output style path escapes its source root: ${X}`);if(q.push(W),q.length>I0)throw Error(`Output style catalog exceeds ${I0} files`)}};return await Z(Q,1),q}async function V$(O,$,Q){let q=await D.lstat(Q);if(!q.isFile()||q.isSymbolicLink())throw Error("Output style must be a regular file");if(q.size>n1)throw Error(`Output style exceeds ${n1} bytes`);let Z=await D.readFile(Q,"utf8"),J=Y$(Z),K=J.content.trim(),L=Buffer.byteLength(K);if(L===0)throw Error("Output style prompt must not be empty");if(L>o1)throw Error(`Output style prompt exceeds ${o1} bytes`);if(a0(K))throw Error("Output style prompt contains hidden control characters");let G=C.relative($,Q),X=F$(G);return{id:`${O.idPrefix}:${X}`,name:W$(J.data.name,X),description:X$(J.data.description,K,`Custom ${X} communication style`),source:O.source,contentSha256:K$("sha256").update(K,"utf8").digest("hex"),prompt:K}}async function U$(O,$){let Q=await H$(O.directory);if(Q.length===0)return;let q=await D.realpath(O.directory);for(let Z of Q)try{let J=await V$(O,q,Z);$.set(J.id,J)}catch(J){L$.warn(`[CommunicationStyles] Ignoring ${O.source} style: ${J instanceof Error?J.message:String(J)}`)}}function S$(O,$,Q,q){let Z=[{directory:C.join(q,".claude","output-styles"),source:"user",idPrefix:"user"},{directory:C.join(q,".blade","output-styles"),source:"user",idPrefix:"user"}];if($)Z.push({directory:C.join(O,".claude","output-styles"),source:"project",idPrefix:"project"},{directory:C.join(O,".blade","output-styles"),source:"project",idPrefix:"project"});for(let J of Q){if(J.status!=="active"||!e1.test(J.manifest.name))continue;Z.push({directory:C.join(J.basePath,"output-styles"),source:"plugin",idPrefix:`plugin:${J.manifest.name}`})}return Z}async function r0(O,$){let Q=new Map;for(let Z of S$(C.resolve(O),$.projectTrusted,$.plugins??[],$.homeDirectory??G$.homedir()))await U$(Z,Q);let q=0;for(let Z of Q.values())q+=Buffer.byteLength(Z.prompt??"");if(Q.size>I0)throw Error(`Output style catalog exceeds ${I0} styles`);if(q>i1)throw Error(`Output style catalog exceeds ${i1} bytes`);return new r1([...Q.values()])}import{createHash as j$}from"node:crypto";import{existsSync as A0,realpathSync as B$}from"node:fs";import*as m from"node:fs/promises";import M from"node:path";import M$ from"fast-glob";import z$ from"gray-matter";import KO from"picomatch";var OO=128,$O=32768,QO=262144,I$=49152,x$=32768,A$=24,qO=16,b$=256,_$=h("Agent"),T$=["**/CLAUDE.md","**/.claude/CLAUDE.md","**/CLAUDE.local.md","**/AGENTS.md","**/AGENTS.override.md","**/BLADE.md","**/.claude/rules/**/*.md","**/.blade/rules/**/*.md"],w$=["**/.git/**","**/node_modules/**","**/.venv/**","**/dist/**","**/build/**","**/coverage/**","**/target/**"];function b0(O){return j$("sha256").update(O,"utf8").digest("hex")}function GO(O){for(let $ of O){let Q=$.codePointAt(0)??0;if(Q<=8||Q===11||Q===12||Q>=14&&Q<=31||Q===127||Q>=8203&&Q<=8207||Q>=8234&&Q<=8238||Q>=8288&&Q<=8303||Q===65279)return!0}return!1}function Q1(O){return O.split(M.sep).join("/")}function e0(O){let $=M.resolve(O),Q=[];while(!0)try{return M.join(B$($),...Q.reverse())}catch(q){if(q.code!=="ENOENT")return M.resolve(O);let Z=M.dirname($);if(Z===$)return M.resolve(O);Q.push(M.basename($)),$=Z}}function C$(O){let $=M.resolve(O),Q;while(!0){if(A0(M.join($,".git")))return $;if(!Q&&(A0(M.join($,".blade"))||A0(M.join($,".claude"))||A0(M.join($,"package.json"))))Q=$;let q=M.dirname($);if(q===$)return Q??M.resolve(O);$=q}}function q1(O){return Boolean(O)&&O!==".."&&!O.startsWith("../")}function O1(O,$){return O===""||$===O||$.startsWith(`${O}/`)}function ZO(O){return O.replaceAll("&","&").replaceAll('"',""").replaceAll("<","<").replaceAll(">",">")}function D$(O){let $=typeof O==="string"?O.split(/[,\n]/u):Array.isArray(O)?O:[],Q=[...new Set($.filter((q)=>typeof q==="string").map((q)=>q.trim().replaceAll("\\","/")).filter(Boolean))];if(Q.length===0||Q.every((q)=>q==="**"))return;if(Q.length>qO)throw Error(`paths exceeds ${qO} patterns`);for(let q of Q){if(q.length>b$||M.posix.isAbsolute(q)||q.split("/").includes("..")||GO(q))throw Error(`Unsafe project rule path pattern: ${q.slice(0,80)}`);KO(q,{dot:!0})}return Q}function N$(O){if(O.endsWith("/CLAUDE.local.md")||O==="CLAUDE.local.md")return{source:"local",kind:"instruction",scopeDirectory:M.posix.dirname(O).replace(/^\.$/u,""),priority:90};for(let q of[".claude/rules/",".blade/rules/"]){let Z=O.indexOf(q);if(Z>=0)return{source:"project",kind:"rule",scopeDirectory:O.slice(0,Z).replace(/\/$/u,""),priority:q.startsWith(".blade")?80:70}}if(O.endsWith("/.claude/CLAUDE.md")||O===".claude/CLAUDE.md")return{source:"project",kind:"instruction",scopeDirectory:O.slice(0,-17).replace(/\/$/u,""),priority:30};let $=M.posix.basename(O),Q=$==="CLAUDE.md"?20:$==="AGENTS.md"?40:$==="AGENTS.override.md"?50:60;return{source:"project",kind:"instruction",scopeDirectory:M.posix.dirname(O).replace(/^\.$/u,""),priority:Q}}function $1(O,$){let Q=O.scopeDirectory?O.scopeDirectory.split("/").length:0,q=$.scopeDirectory?$.scopeDirectory.split("/").length:0;return Q-q||O.priority-$.priority||O.relativePath.localeCompare($.relativePath)}function R$(O){return{id:O.id,relativePath:O.relativePath,source:O.source,contentSha256:O.contentSha256}}function $0(O,$,Q){let q=O.map((K)=>{let L=K.conditional?' conditional="true"':"";return`<instruction-file path="${ZO(K.relativePath)}" source="${K.source}" sha256="${K.contentSha256}"${L}>
|
|
628
683
|
${K.content}
|
|
629
|
-
</instruction-file>`}),Z=Q==="contextual-project-instructions"&&$.length>0?` trigger-paths="${
|
|
684
|
+
</instruction-file>`}),Z=Q==="contextual-project-instructions"&&$.length>0?` trigger-paths="${ZO($.join(","))}"`:"",J=q.length>0?`<${Q}${Z}>
|
|
630
685
|
${q.join(`
|
|
631
686
|
|
|
632
687
|
`)}
|
|
633
|
-
</${Q}>`:"";return{content:J,files:[...O],references:O.map(
|
|
634
|
-
`))}}function
|
|
635
|
-
`))}static empty(O){return new p(O)}list(){return this.definitions.map((O)=>({id:O.id,relativePath:O.relativePath,source:O.source,kind:O.kind,conditional:O.conditional,contentSha256:O.contentSha256}))}snapshot(){return new p(this.projectRoot,this.definitions.map((O)=>({...O,patterns:O.patterns?[...O.patterns]:void 0})))}staticRules(O,$=
|
|
636
|
-
export{i as
|
|
688
|
+
</${Q}>`:"";return{content:J,files:[...O],references:O.map(R$),triggerPaths:[...$],contentBytes:Buffer.byteLength(J,"utf8"),provenanceSha256:b0(O.map((K)=>`${K.id}:${K.contentSha256}`).join(`
|
|
689
|
+
`))}}function JO(O,$){let Q=$,q=[];for(let Z=O.length-1;Z>=0;Z-=1){let J=O[Z];if(!J)continue;let K=Buffer.byteLength(J.content,"utf8");if(K>Q)continue;q.push(J),Q-=K}return q.reverse()}class p{projectRoot;catalogSha256;definitions;byId;constructor(O,$=[]){this.projectRoot=O;this.projectRoot=e0(O),this.definitions=Object.freeze([...$].sort($1).map((Q)=>Object.freeze({...Q}))),this.byId=new Map(this.definitions.map((Q)=>[Q.id,Q])),this.catalogSha256=b0(this.definitions.map((Q)=>`${Q.id}:${Q.contentSha256}`).join(`
|
|
690
|
+
`))}static empty(O){return new p(O)}list(){return this.definitions.map((O)=>({id:O.id,relativePath:O.relativePath,source:O.source,kind:O.kind,conditional:O.conditional,contentSha256:O.contentSha256}))}snapshot(){return new p(this.projectRoot,this.definitions.map((O)=>({...O,patterns:O.patterns?[...O.patterns]:void 0})))}staticRules(O,$=I$){if(this.definitions.length===0)return $0([],[],"project-instructions");let Q=this.relativeSourcePath(O),q=this.definitions.filter((Z)=>!Z.conditional&&O1(Z.scopeDirectory,Q));return $0(JO(q,$),[],"project-instructions")}contextualRules(O,$,Q,q=x$){if(this.definitions.length===0)return $0([],[],"contextual-project-instructions");let Z=this.relativeSourcePath(O),J=[...new Set($.map((G)=>Q1(M.relative(this.projectRoot,e0(G)))).filter(q1))].sort(),K=new Map;for(let G of J)for(let X of this.definitions){if(Q.has(X.id))continue;if(!O1(X.scopeDirectory,G))continue;if(X.conditional){let W=M.posix.relative(X.scopeDirectory||".",G);if(!X.patterns?.some((V)=>KO(V,{dot:!0})(W)))continue}else if(O1(X.scopeDirectory,Z))continue;K.set(X.id,X)}let L=JO([...K.values()].sort($1),q);return $0(L,J,"contextual-project-instructions")}hydrate(O){let $=O.map((Q)=>{let q=this.byId.get(Q.id);if(!q||q.contentSha256!==Q.contentSha256||q.relativePath!==Q.relativePath||q.source!==Q.source)throw Error(`Project rule provenance mismatch: ${Q.relativePath}`);return q});return $0([...new Map($.map((Q)=>[Q.id,Q])).values()].sort($1),[],"contextual-project-instructions")}relativeSourcePath(O){let $=Q1(M.relative(this.projectRoot,e0(O)));if($&&!q1($))throw Error("Project instruction source is outside the catalog root");return $}}async function v$(O,$){let Q=await m.lstat($);if(!Q.isFile()||Q.isSymbolicLink())throw Error("Project rule must be a regular file");if(Q.size>$O)throw Error(`Project rule exceeds ${$O} bytes`);let q=await m.realpath($),Z=Q1(M.relative(O,q));if(!q1(Z))throw Error("Project rule escapes its repository root");let J=await m.readFile(q,"utf8");if(GO(J))throw Error("Project rule contains hidden control characters");let K=z$(J),L=K.content.trim();if(!L)throw Error("Project rule content must not be empty");let G=D$(K.data.paths),X=N$(Z),W=b0(JSON.stringify({relativePath:Z,patterns:G??[],content:L}));return{id:`${X.source}:${b0(Z).slice(0,20)}`,relativePath:Z,source:X.source,kind:X.kind,scopeDirectory:X.scopeDirectory,priority:X.priority,conditional:Boolean(G),...G?{patterns:G}:{},content:L,contentSha256:W}}async function YO(O,$){let Q=C$(O);if(!$.projectTrusted)return p.empty(Q);let q=await m.realpath(Q),Z=M$.stream([...T$],{cwd:q,absolute:!0,dot:!0,deep:A$,onlyFiles:!0,followSymbolicLinks:!1,unique:!0,ignore:[...w$]}),J=[];for await(let X of Z)if(J.push(X.toString()),J.length>OO)throw Error(`Project rule catalog exceeds ${OO} files`);J.sort();let K=new Set(J.filter((X)=>M.basename(X)==="AGENTS.override.md").map((X)=>M.dirname(X))),L=[],G=0;for(let X of J){if(M.basename(X)==="AGENTS.md"&&K.has(M.dirname(X)))continue;try{let W=await v$(q,X);if(G+=Buffer.byteLength(W.content,"utf8"),G>QO)throw Error(`Project rule catalog exceeds ${QO} bytes`);L.push(W)}catch(W){_$.warn(`[ProjectRules] Ignoring rule: ${W instanceof Error?W.message:String(W)}`)}}return new p(q,L)}var Q0=new Map,XO=[];async function y$(O){let $=await T.getInstance().getStatus(O.workspaceRoot);O.communicationStyles=await r0(O.workspaceRoot,{projectTrusted:$.state==="trusted",plugins:O.plugins.getActive()})}function E$(O){let $={...O};for(let Q of Object.values(B0))$[Q]=(O[Q]??[]).map((q)=>({...q,matcher:q.matcher?{...q.matcher}:void 0,hooks:q.hooks.map((Z)=>({...Z}))}));return $}function I2(O){XO=[...new Set(O.map(($)=>Z1.resolve($)))]}async function x2(O,$={}){let Q=Z1.resolve(O),q=Q0.get(Q);if(!q)q=(async()=>{let J=await T.getInstance().getStatus(Q),K=i(Q),L=P({cwd:Q}),G=w.getInstance(Q),X=j0(Q);if(K.getAllNames().length===0)K.loadFromStandardLocations(J.state==="trusted");await Promise.all([L.initialize(),G.isInitialized()?void 0:G.initialize(Q),X.isInitialized()?void 0:X.initialize(Q,[...$.cliPluginDirs??XO])]),z0(Q),await t0(Q);let W=await r0(Q,{projectTrusted:J.state==="trusted",plugins:X.getActive()}),V=await YO(Q,{projectTrusted:J.state==="trusted"});return{workspaceRoot:Q,subagents:K,skills:L,commands:G,plugins:X,communicationStyles:W,projectRules:V}})().catch((J)=>{throw Q0.delete(Q),J}),Q0.set(Q,q);let Z=await q;if($.reconcilePlugins)z0(Q),await t0(Q),await y$(Z);return Z}function A2(O){let $="projectRoot"in O?O.projectRoot:O.workspaceRoot;return{projectRoot:Z1.resolve($),subagents:O.subagents.snapshot(),skills:O.skills.snapshot(),commands:O.commands.snapshot(),communicationStyles:O.communicationStyles?.snapshot()??LO.snapshot(),projectRules:O.projectRules?.snapshot()??p.empty($),hooks:E$("hooks"in O&&O.hooks?O.hooks:M0.getInstance().getConfig($))}}function b2(){for(let O of Q0.keys())z0(O);Q0.clear(),y.resetInstances(),k.resetInstance(),w.resetInstance(),f.resetInstance()}
|
|
691
|
+
export{K1 as Vc,i as Wc,r$ as Xc,F1 as Yc,P as Zc,AO as _c,w as $c,n0 as ad,U0 as bd,f as cd,j0 as dd,t0 as ed,z0 as fd,p as gd,y$ as hd,I2 as id,x2 as jd,A2 as kd,b2 as ld};
|