@dhzh/foundry 7.1.1 → 7.1.2

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/index.mjs CHANGED
@@ -1,5 +1,5 @@
1
1
  #!/usr/bin/env node
2
- import{defineCommand as e,runMain as t,showUsage as n}from"citty";import r from"node:path";import i from"node:process";import{spawn as a}from"node:child_process";import{chmodSync as o,closeSync as s,createReadStream as c,createWriteStream as l,existsSync as u,fsyncSync as d,mkdirSync as f,openSync as p,readFileSync as m,readdirSync as h,renameSync as ee,rmSync as g,rmdirSync as _,statSync as v,watch as te,writeFileSync as ne}from"node:fs";import re,{homedir as ie,tmpdir as ae}from"node:os";import{createCipheriv as oe,createDecipheriv as se,createHash as ce,randomBytes as le,randomUUID as y,scrypt as ue}from"node:crypto";import{chmod as de,cp as fe,link as pe,mkdir as me,mkdtemp as he,open as ge,readFile as _e,readdir as ve,rename as ye,rm as b,stat as be,writeFile as xe}from"node:fs/promises";import{fileURLToPath as Se}from"node:url";import{Buffer as x}from"node:buffer";import{pipeline as S}from"node:stream/promises";import Ce from"yauzl";import we from"yazl";import{Readable as Te,Transform as Ee}from"node:stream";import{parse as De,stringify as Oe}from"yaml";import{serve as ke}from"@hono/node-server";import{serveStatic as Ae}from"@hono/node-server/serve-static";import{Hono as C}from"hono";import{StringDecoder as je}from"node:string_decoder";import{createZstdDecompress as Me}from"node:zlib";import{streamSSE as Ne}from"hono/streaming";import{FirecrawlClient as Pe}from"firecrawl";import{parse as Fe}from"tldts";import{createServer as Ie}from"node:net";var Le=`7.1.1`,w=class extends Error{code;status;constructor(e,t,n){super(t),this.code=e,this.status=n,this.name=`AgentError`}};function Re(e){if(!Be(e)||!Array.isArray(e.instructionIds))throw new w(`INVALID_AGENT_INPUT`,`instructionIds must be an array`,400);let t=e.instructionIds.map(e=>{if(typeof e!=`string`||!e.trim())throw new w(`INVALID_AGENT_INPUT`,`instructionIds must contain non-empty strings`,400);return e});if(new Set(t).size!==t.length)throw new w(`DUPLICATE_INSTRUCTION`,`An instruction cannot be added to the same agent twice`,400);return{instructionIds:t}}function ze(e){if(!Be(e)||typeof e.enabled!=`boolean`)throw new w(`INVALID_AGENT_INPUT`,`enabled must be a boolean`,400);return{enabled:e.enabled}}function Be(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Ve=[`chatgpt`,`claude-code`];function He(e){return[{id:`chatgpt`,name:`ChatGPT`,icon:`openai`,instructionFile:`~/.codex/AGENTS.md`,filePath:r.join(e,`.codex`,`AGENTS.md`)},{id:`claude-code`,name:`Claude Code`,icon:`claudecode`,instructionFile:`~/.claude/CLAUDE.md`,filePath:r.join(e,`.claude`,`CLAUDE.md`)}]}function Ue(e){let t=i.platform===`darwin`?`open`:i.platform===`win32`?`explorer`:`xdg-open`,n=a(t,[e],{detached:!0,stdio:`ignore`});n.on(`error`,t=>{console.error(`Unable to open ${e}: ${t.message}`)}),n.unref()}function We(){return r.join(re.homedir(),`.foundry`)}function Ge(){let e=We();return f(e,{recursive:!0,mode:448}),i.platform!==`win32`&&o(e,448),e}const Ke=[`system`,`light`,`dark`],qe=[`system`,`en-US`,`zh-CN`,`zh-TW`,`ja-JP`],Je=[`YYYY-MM-DD HH:mm:ss`,`YYYY-MM-DD`,`YYYY/MM/DD HH:mm:ss`,`DD/MM/YYYY HH:mm:ss`,`MM/DD/YYYY hh:mm:ss A`,`HH:mm:ss`],Ye=[`America/Los_Angeles`,`America/New_York`,`Asia/Shanghai`,`Asia/Taipei`,`Asia/Tokyo`,`UTC`],Xe=[`firecrawl`],Ze=`data/foundry.json`,Qe=`settings.changed`,$e=[`agents`,`claude`],et={workspace:{appearance:{"color-theme":`system`},"language-region":{language:`system`,"time-format":`YYYY-MM-DD HH:mm:ss`,"time-zone":`America/Los_Angeles`}},features:{library:{captures:{fetcher:null,maxAttempts:1,fetchConcurrency:2,firecrawl:{apiKey:``}}}}};function tt(e){let t=D(e)?e:{},n=D(t.workspace)?t.workspace:{},r=D(n.appearance)?n.appearance:{},i=D(n[`language-region`])?n[`language-region`]:{},a=D(t.features)?t.features:{},o=D(a.library)?a.library:{},s=D(o.captures)?o.captures:{},c=D(s.firecrawl)?s.firecrawl:{};return{workspace:{appearance:{"color-theme":ct(r[`color-theme`],Ke,et.workspace.appearance[`color-theme`])},"language-region":{language:ct(i.language,qe,et.workspace[`language-region`].language),"time-format":ct(i[`time-format`],Je,et.workspace[`language-region`][`time-format`]),"time-zone":ct(i[`time-zone`],Ye,et.workspace[`language-region`][`time-zone`])}},features:{...a,library:{...o,captures:{fetcher:ot(s.fetcher),maxAttempts:lt(s.maxAttempts,10,1),fetchConcurrency:lt(s.fetchConcurrency,5,2),firecrawl:{apiKey:ft(c.apiKey)}}}}}}function nt(e){if(!D(e))throw new T(`Settings patch must be an object`);let t={};return`theme`in e&&(t.theme=E(e.theme,Ke,`theme`)),`language`in e&&(t.language=E(e.language,qe,`language`)),`timeFormat`in e&&(t.timeFormat=E(e.timeFormat,Je,`timeFormat`)),`timezone`in e&&(t.timezone=E(e.timezone,Ye,`timezone`)),`captures`in e&&(t.captures=at(e.captures)),t}function rt(e,t){let n=e.features.library.captures,r=t.captures;return{workspace:{appearance:{"color-theme":t.theme??e.workspace.appearance[`color-theme`]},"language-region":{language:t.language??e.workspace[`language-region`].language,"time-format":t.timeFormat??e.workspace[`language-region`][`time-format`],"time-zone":t.timezone??e.workspace[`language-region`][`time-zone`]}},features:{...e.features,library:{...e.features.library,captures:{...n,fetcher:r?.fetcher===void 0?n.fetcher:r.fetcher,maxAttempts:r?.maxAttempts??n.maxAttempts,fetchConcurrency:r?.fetchConcurrency??n.fetchConcurrency,firecrawl:{apiKey:r?.firecrawlApiKey??n.firecrawl.apiKey}}}}}}function it(e){let t=e.features.library.captures;return{workspace:e.workspace,features:{...e.features,library:{...e.features.library,captures:{fetcher:t.fetcher,maxAttempts:t.maxAttempts,fetchConcurrency:t.fetchConcurrency,firecrawl:{apiKey:t.firecrawl.apiKey}}}}}}var T=class extends Error{code=`INVALID_SETTINGS_INPUT`;constructor(e){super(e),this.name=`SettingsInputError`}};function at(e){if(!D(e))throw new T(`captures must be an object`);let t={};return`fetcher`in e&&(t.fetcher=e.fetcher===null?null:E(e.fetcher,Xe,`captures.fetcher`)),`maxAttempts`in e&&(t.maxAttempts=ut(e.maxAttempts,10,`captures.maxAttempts`)),`fetchConcurrency`in e&&(t.fetchConcurrency=ut(e.fetchConcurrency,5,`captures.fetchConcurrency`)),`firecrawlApiKey`in e&&(t.firecrawlApiKey=dt(e.firecrawlApiKey,`captures.firecrawlApiKey`)),t}function ot(e){return st(e,Xe)}function st(e,t){return typeof e==`string`&&t.includes(e)?e:null}function ct(e,t,n){return typeof e==`string`&&t.includes(e)?e:n}function E(e,t,n){if(typeof e!=`string`||!t.includes(e))throw new T(`Invalid ${n}`);return e}function lt(e,t,n){return typeof e==`number`&&Number.isSafeInteger(e)?Math.min(t,Math.max(1,e)):n}function ut(e,t,n){if(typeof e!=`number`||!Number.isSafeInteger(e)||e<1||e>t)throw new T(`${n} must be an integer from 1 to ${t}`);return e}function dt(e,t){if(typeof e!=`string`)throw new T(`${t} must be a string`);return e.trim()}function ft(e){return typeof e==`string`?e:``}function D(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function O(e){return e.readSettings()}function pt(e){return it(O(e))}function mt(e,t){return t.transaction(()=>{let n=rt(O(t),e);return t.setSettings(n),it(n)})}var ht=class{storage;configFolder;openFolder;constructor(e,t=We(),n=Ue){this.storage=e,this.configFolder=t,this.openFolder=n}read(){return{...pt(this.storage),configFolder:this.configFolder}}update(e){return{...mt(e,this.storage),configFolder:this.configFolder}}openConfigFolder(){return this.openFolder(this.configFolder),{configFolder:this.configFolder}}};const gt=r.dirname(Se(import.meta.url)),k=`foundry`;var _t=class{configuredSkillRoot;homeDirectory;constructor({skillRoot:e,homeDirectory:t=ie()}={}){this.configuredSkillRoot=e,this.homeDirectory=r.resolve(t)}async copyToTarget(e){let t=this.configuredSkillRoot??vt();if(!u(r.join(t,`SKILL.md`)))throw new A(`SKILL_NOT_FOUND`,`The bundled Foundry skill was not found`);let n=this.getTargetParent(e),i=r.join(n,k),a=r.join(n,`.${k}-${y()}.tmp`),o=r.join(n,`.${k}-${y()}.backup`);try{return await me(n,{recursive:!0}),await fe(t,a,{recursive:!0,errorOnExist:!0}),await yt(a,i,o),{target:e,destination:i}}catch(e){throw await b(a,{recursive:!0,force:!0}),e instanceof A?e:new A(`SKILL_COPY_FAILED`,`Unable to copy the Foundry skill`,{cause:e})}}getTargetParent(e){return r.join(this.homeDirectory,e===`agents`?`.agents`:`.claude`,`skills`)}getTargets(){return{targets:[`agents`,`claude`].map(e=>({target:e,destination:r.join(this.getTargetParent(e),k)}))}}async copyToTargets(e){return{installed:await Array.fromAsync(e,e=>this.copyToTarget(e))}}},A=class extends Error{code;constructor(e,t,n){super(t,n),this.code=e,this.name=`BundledSkillCopyError`}};function vt(){let e=[r.resolve(gt,`skills`,k),r.resolve(gt,`..`,`..`,`..`,`skills`,k),r.resolve(i.cwd(),`skills`,k)].find(e=>u(r.join(e,`SKILL.md`)));if(!e)throw new A(`SKILL_NOT_FOUND`,`The bundled Foundry skill was not found`);return e}async function yt(e,t,n){let r=u(t);r&&await ye(t,n);try{await ye(e,t),r&&await b(n,{recursive:!0,force:!0})}catch(e){throw r&&!u(t)&&u(n)&&await ye(n,t),e}}const bt=`manifest.json`,xt=/* @__PURE__ */ new Set([bt,Ze]);async function St(e,t){let n=new we.ZipFile;for(let t of e)n.addFile(t.filePath,t.entryPath,{compress:!0});n.end(),await S(n.outputStream,l(t,{mode:384}))}async function Ct(e,t){let n=await wt(e),i=/* @__PURE__ */ new Map;try{await new Promise((e,a)=>{let o=e=>{a(e instanceof Error?e:Error(String(e)))},s=async e=>{if(!xt.has(e.fileName))throw Error(`The Foundry archive contains an unexpected file`);if(e.fileName.endsWith(`/`)||i.has(e.fileName))throw Error(`The Foundry archive contains an invalid file`);let a=r.join(t,e.fileName);await me(r.dirname(a),{recursive:!0,mode:448}),await S(await Tt(n,e),l(a,{mode:384})),i.set(e.fileName,a),n.readEntry()};n.on(`error`,o),n.on(`end`,e),n.on(`entry`,e=>{s(e).catch(o)}),n.readEntry()})}catch(e){throw await b(t,{recursive:!0,force:!0}),e}finally{n.close()}if(!i.has(bt)||!i.has(`data/foundry.json`))throw await b(t,{recursive:!0,force:!0}),Error(`The Foundry archive is incomplete`);return Object.fromEntries(i)}function wt(e){return new Promise((t,n)=>{Ce.open(e,{autoClose:!1,lazyEntries:!0,strictFileNames:!0},(e,r)=>{e?n(e):t(r)})})}function Tt(e,t){return new Promise((n,r)=>{e.openReadStream(t,(e,t)=>{e?r(e):n(t)})})}var Et=class extends Error{code;constructor(e,t,n){super(t,n),this.code=e,this.name=`BackupCryptoError`}};const j=x.from(`FOUNDRYX`,`ascii`),M=`aes-256-gcm`,Dt=`scrypt`,Ot={N:131072,r:8,p:1};async function kt(e,t,n){let r=le(16),i=le(12),a={version:1,cipher:M,kdf:{name:Dt,salt:r.toString(`base64`),...Ot,keyLength:32},nonce:i.toString(`base64`)},o=x.from(JSON.stringify(a),`utf8`),s=jt(o.byteLength),u=x.concat([s,o]),d=await Mt(n,r,Ot);try{let n=await ge(t,`w`,384);try{await n.write(u)}finally{await n.close()}await S(c(e),Nt(d,i,u),l(t,{flags:`r+`,mode:384,start:u.byteLength})),await de(t,384)}catch(e){throw await b(t,{force:!0}),new Et(`IO_ERROR`,`Unable to encrypt the Foundry export`,{cause:e})}finally{d.fill(0)}}async function At(e,t,n){let r;try{let i=(await be(e)).size;if(i<=29)throw Error(`The export file is incomplete`);let a=await ge(e,`r`),o,s,u;try{if(o=await It(a,13,0),!o.subarray(0,j.byteLength).equals(j))throw Error(`The export file signature is invalid`);if(o.readUInt8(j.byteLength)!==1)throw Error(`The export envelope version is unsupported`);let e=o.readUInt32BE(j.byteLength+1);if(e===0||e>65536)throw Error(`The export header is invalid`);if(i<=13+e+16)throw Error(`The export file is incomplete`);s=await It(a,e,13),u=await It(a,16,i-16)}finally{await a.close()}let d=Ft(JSON.parse(s.toString(`utf8`)));r=await Mt(n,d.salt,d.header.kdf);let f=x.concat([o,s]);await S(c(e,{start:13+s.byteLength,end:i-16-1}),Pt(r,d.nonce,f,u),l(t,{mode:384})),await de(t,384)}catch(e){throw await b(t,{force:!0}),e instanceof Et?e:new Et(Rt(e)?`IO_ERROR`:`INVALID_PASSPHRASE`,Rt(e)?`Unable to read or decrypt the backup file`:`The passphrase is incorrect or the export file is corrupted`,{cause:e})}finally{r?.fill(0)}}function jt(e){let t=x.alloc(13);return j.copy(t,0),t.writeUInt8(1,j.byteLength),t.writeUInt32BE(e,j.byteLength+1),t}function Mt(e,t,n){return new Promise((r,i)=>{ue(e,t,32,{...n,maxmem:Math.max(67108864,256*n.N*n.r)},(e,t)=>{e?i(e):r(t)})})}function Nt(e,t,n){let r=oe(M,e,t,{authTagLength:16});return r.setAAD(n),new Ee({transform(e,t,n){try{n(null,r.update(e))}catch(e){n(e instanceof Error?e:Error(String(e)))}},flush(e){try{e(null,x.concat([r.final(),r.getAuthTag()]))}catch(t){e(t instanceof Error?t:Error(String(t)))}}})}function Pt(e,t,n,r){let i=se(M,e,t,{authTagLength:16});return i.setAAD(n),new Ee({transform(e,t,n){try{n(null,i.update(e))}catch(e){n(e instanceof Error?e:Error(String(e)))}},flush(e){try{i.setAuthTag(r),e(null,i.final())}catch(t){e(t instanceof Error?t:Error(String(t)))}}})}function Ft(e){if(!Lt(e)||e.version!==1)throw Error(`Unsupported export envelope`);if(e.cipher!==M||!Lt(e.kdf))throw Error(`Unsupported export encryption`);let t=e.kdf;if(t.name!==Dt||typeof t.salt!=`string`||t.N!==Ot.N||t.r!==Ot.r||t.p!==Ot.p||t.keyLength!==32||typeof e.nonce!=`string`)throw Error(`Invalid export key derivation parameters`);let n=x.from(t.salt,`base64`),r=x.from(e.nonce,`base64`);if(n.byteLength!==16||r.byteLength!==12)throw Error(`Invalid export encryption parameters`);return{header:{version:1,cipher:M,kdf:{name:Dt,salt:t.salt,N:t.N,r:t.r,p:t.p,keyLength:t.keyLength},nonce:e.nonce},salt:n,nonce:r}}async function It(e,t,n){let r=x.alloc(t),{bytesRead:i}=await e.read(r,0,t,n);if(i!==t)throw Error(`Unexpected end of export file`);return r}function Lt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function Rt(e){return e instanceof Error&&`code`in e&&typeof e.code==`string`&&/^E[A-Z]+$/u.test(e.code)}async function zt(e){try{await b(e,{recursive:!0,force:!0})}catch{console.error(`Unable to remove backup temporary files`)}}function Bt(e){f(e,{recursive:!0,mode:448}),i.platform!==`win32`&&o(e,448)}function N(e){if(i.platform===`win32`)return;let t=p(e,`r`);try{d(t)}finally{s(t)}}function Vt(e,t){Ht(e,`${JSON.stringify(t,null,2)}\n`)}function Ht(e,t){Bt(r.dirname(e));let n=`${e}.${y()}.tmp`;try{let i=p(n,`wx`,384);try{ne(i,t),d(i)}finally{s(i)}ee(n,e),N(r.dirname(e))}finally{g(n,{force:!0})}}const Ut=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u;var P=class extends Error{code=`INVALID_INSTRUCTION_INPUT`;constructor(e){super(e),this.name=`InstructionInputError`}},Wt=class extends Error{code=`INSTRUCTION_NOT_FOUND`;constructor(e){super(`Instruction not found: ${e}`),this.name=`InstructionNotFoundError`}},Gt=class extends Error{agentIds;code=`INSTRUCTION_IN_USE`;constructor(e){super(`Instruction is assigned to: ${e.join(`, `)}`),this.agentIds=e,this.name=`InstructionInUseError`}};function Kt(e){if(!qt(e))throw new P(`Instruction input must be an object`);if(typeof e.name!=`string`)throw new P(`name is required`);let t=e.name.trim();if(!t)throw new P(`name is required`);if(t.length>200)throw new P(`name must be at most 200 characters`);if(typeof e.content!=`string`||!e.content.trim())throw new P(`content is required`);if(e.content.length>1e6)throw new P(`content must be at most 1000000 characters`);return{name:t,content:e.content}}function qt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Jt=/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/u;function Yt(e,t){let n=Date.now(),r={id:y(),name:e.name,content:e.content,createdAt:n,updatedAt:n},i=cn(t,r.id),a=en(r);return t.transaction(()=>{Ht(i,a)}),r}function Xt(e){let t=sn(e),n;try{n=h(t,{withFileTypes:!0}).filter(e=>e.isFile()&&e.name.endsWith(`.md`)).map(e=>e.name)}catch(e){if(ln(e))return[];throw e}return n.map(e=>nn(r.join(t,e),e)).toSorted((e,t)=>t.updatedAt-e.updatedAt)}function Zt(e,t){if(!Ut.test(e))return;let n=`${e}.md`;try{return nn(r.join(sn(t),n),n)}catch(e){if(ln(e))return;throw e}}function Qt(e,t,n){let r=Zt(e,n);if(!r)return;let i={...r,name:t.name,content:t.content,updatedAt:Date.now()};return n.transaction(()=>{Ht(cn(n,i.id),en(i))}),i}function $t(e,t){if(!Ut.test(e))return`missing`;let n=sn(t),i=r.join(n,`${e}.md`);return t.transaction(()=>{try{return g(i),N(n),`removed`}catch(e){if(ln(e))return`missing`;throw e}})}function en(e){return`---\n${Oe({id:e.id,name:e.name,createdAt:e.createdAt,updatedAt:e.updatedAt}).trimEnd()}\n---\n\n${e.content}`}function tn(e,t){let n=Jt.exec(e);if(!n)throw F(t);let r=rn(n[1]);if(`${r.id}.md`!==t)throw F(t);let i=e.slice(n[0].length);return i.startsWith(`\r
2
+ import{defineCommand as e,runMain as t,showUsage as n}from"citty";import r from"node:path";import i from"node:process";import{spawn as a}from"node:child_process";import{chmodSync as o,closeSync as s,createReadStream as c,createWriteStream as l,existsSync as u,fsyncSync as d,mkdirSync as f,openSync as p,readFileSync as m,readdirSync as h,renameSync as ee,rmSync as g,rmdirSync as _,statSync as v,watch as te,writeFileSync as ne}from"node:fs";import re,{homedir as ie,tmpdir as ae}from"node:os";import{createCipheriv as oe,createDecipheriv as se,createHash as ce,randomBytes as le,randomUUID as y,scrypt as ue}from"node:crypto";import{chmod as de,cp as fe,link as pe,mkdir as me,mkdtemp as he,open as ge,readFile as _e,readdir as ve,rename as ye,rm as b,stat as be,writeFile as xe}from"node:fs/promises";import{fileURLToPath as Se}from"node:url";import{Buffer as x}from"node:buffer";import{pipeline as S}from"node:stream/promises";import Ce from"yauzl";import we from"yazl";import{Readable as Te,Transform as Ee}from"node:stream";import{parse as De,stringify as Oe}from"yaml";import{serve as ke}from"@hono/node-server";import{serveStatic as Ae}from"@hono/node-server/serve-static";import{Hono as C}from"hono";import{StringDecoder as je}from"node:string_decoder";import{createZstdDecompress as Me}from"node:zlib";import{streamSSE as Ne}from"hono/streaming";import{FirecrawlClient as Pe}from"firecrawl";import{parse as Fe}from"tldts";import{createServer as Ie}from"node:net";var Le=`7.1.2`,w=class extends Error{code;status;constructor(e,t,n){super(t),this.code=e,this.status=n,this.name=`AgentError`}};function Re(e){if(!Be(e)||!Array.isArray(e.instructionIds))throw new w(`INVALID_AGENT_INPUT`,`instructionIds must be an array`,400);let t=e.instructionIds.map(e=>{if(typeof e!=`string`||!e.trim())throw new w(`INVALID_AGENT_INPUT`,`instructionIds must contain non-empty strings`,400);return e});if(new Set(t).size!==t.length)throw new w(`DUPLICATE_INSTRUCTION`,`An instruction cannot be added to the same agent twice`,400);return{instructionIds:t}}function ze(e){if(!Be(e)||typeof e.enabled!=`boolean`)throw new w(`INVALID_AGENT_INPUT`,`enabled must be a boolean`,400);return{enabled:e.enabled}}function Be(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Ve=[`chatgpt`,`claude-code`];function He(e){return[{id:`chatgpt`,name:`ChatGPT`,icon:`openai`,instructionFile:`~/.codex/AGENTS.md`,filePath:r.join(e,`.codex`,`AGENTS.md`)},{id:`claude-code`,name:`Claude Code`,icon:`claudecode`,instructionFile:`~/.claude/CLAUDE.md`,filePath:r.join(e,`.claude`,`CLAUDE.md`)}]}function Ue(e){let t=i.platform===`darwin`?`open`:i.platform===`win32`?`explorer`:`xdg-open`,n=a(t,[e],{detached:!0,stdio:`ignore`});n.on(`error`,t=>{console.error(`Unable to open ${e}: ${t.message}`)}),n.unref()}function We(){return r.join(re.homedir(),`.foundry`)}function Ge(){let e=We();return f(e,{recursive:!0,mode:448}),i.platform!==`win32`&&o(e,448),e}const Ke=[`system`,`light`,`dark`],qe=[`system`,`en-US`,`zh-CN`,`zh-TW`,`ja-JP`],Je=[`YYYY-MM-DD HH:mm:ss`,`YYYY-MM-DD`,`YYYY/MM/DD HH:mm:ss`,`DD/MM/YYYY HH:mm:ss`,`MM/DD/YYYY hh:mm:ss A`,`HH:mm:ss`],Ye=[`America/Los_Angeles`,`America/New_York`,`Asia/Shanghai`,`Asia/Taipei`,`Asia/Tokyo`,`UTC`],Xe=[`firecrawl`],Ze=`data/foundry.json`,Qe=`settings.changed`,$e=[`agents`,`claude`],et={workspace:{appearance:{"color-theme":`system`},"language-region":{language:`system`,"time-format":`YYYY-MM-DD HH:mm:ss`,"time-zone":`America/Los_Angeles`}},features:{library:{captures:{fetcher:null,maxAttempts:1,fetchConcurrency:2,firecrawl:{apiKey:``}}}}};function tt(e){let t=D(e)?e:{},n=D(t.workspace)?t.workspace:{},r=D(n.appearance)?n.appearance:{},i=D(n[`language-region`])?n[`language-region`]:{},a=D(t.features)?t.features:{},o=D(a.library)?a.library:{},s=D(o.captures)?o.captures:{},c=D(s.firecrawl)?s.firecrawl:{};return{workspace:{appearance:{"color-theme":ct(r[`color-theme`],Ke,et.workspace.appearance[`color-theme`])},"language-region":{language:ct(i.language,qe,et.workspace[`language-region`].language),"time-format":ct(i[`time-format`],Je,et.workspace[`language-region`][`time-format`]),"time-zone":ct(i[`time-zone`],Ye,et.workspace[`language-region`][`time-zone`])}},features:{...a,library:{...o,captures:{fetcher:ot(s.fetcher),maxAttempts:lt(s.maxAttempts,10,1),fetchConcurrency:lt(s.fetchConcurrency,5,2),firecrawl:{apiKey:ft(c.apiKey)}}}}}}function nt(e){if(!D(e))throw new T(`Settings patch must be an object`);let t={};return`theme`in e&&(t.theme=E(e.theme,Ke,`theme`)),`language`in e&&(t.language=E(e.language,qe,`language`)),`timeFormat`in e&&(t.timeFormat=E(e.timeFormat,Je,`timeFormat`)),`timezone`in e&&(t.timezone=E(e.timezone,Ye,`timezone`)),`captures`in e&&(t.captures=at(e.captures)),t}function rt(e,t){let n=e.features.library.captures,r=t.captures;return{workspace:{appearance:{"color-theme":t.theme??e.workspace.appearance[`color-theme`]},"language-region":{language:t.language??e.workspace[`language-region`].language,"time-format":t.timeFormat??e.workspace[`language-region`][`time-format`],"time-zone":t.timezone??e.workspace[`language-region`][`time-zone`]}},features:{...e.features,library:{...e.features.library,captures:{...n,fetcher:r?.fetcher===void 0?n.fetcher:r.fetcher,maxAttempts:r?.maxAttempts??n.maxAttempts,fetchConcurrency:r?.fetchConcurrency??n.fetchConcurrency,firecrawl:{apiKey:r?.firecrawlApiKey??n.firecrawl.apiKey}}}}}}function it(e){let t=e.features.library.captures;return{workspace:e.workspace,features:{...e.features,library:{...e.features.library,captures:{fetcher:t.fetcher,maxAttempts:t.maxAttempts,fetchConcurrency:t.fetchConcurrency,firecrawl:{apiKey:t.firecrawl.apiKey}}}}}}var T=class extends Error{code=`INVALID_SETTINGS_INPUT`;constructor(e){super(e),this.name=`SettingsInputError`}};function at(e){if(!D(e))throw new T(`captures must be an object`);let t={};return`fetcher`in e&&(t.fetcher=e.fetcher===null?null:E(e.fetcher,Xe,`captures.fetcher`)),`maxAttempts`in e&&(t.maxAttempts=ut(e.maxAttempts,10,`captures.maxAttempts`)),`fetchConcurrency`in e&&(t.fetchConcurrency=ut(e.fetchConcurrency,5,`captures.fetchConcurrency`)),`firecrawlApiKey`in e&&(t.firecrawlApiKey=dt(e.firecrawlApiKey,`captures.firecrawlApiKey`)),t}function ot(e){return st(e,Xe)}function st(e,t){return typeof e==`string`&&t.includes(e)?e:null}function ct(e,t,n){return typeof e==`string`&&t.includes(e)?e:n}function E(e,t,n){if(typeof e!=`string`||!t.includes(e))throw new T(`Invalid ${n}`);return e}function lt(e,t,n){return typeof e==`number`&&Number.isSafeInteger(e)?Math.min(t,Math.max(1,e)):n}function ut(e,t,n){if(typeof e!=`number`||!Number.isSafeInteger(e)||e<1||e>t)throw new T(`${n} must be an integer from 1 to ${t}`);return e}function dt(e,t){if(typeof e!=`string`)throw new T(`${t} must be a string`);return e.trim()}function ft(e){return typeof e==`string`?e:``}function D(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function O(e){return e.readSettings()}function pt(e){return it(O(e))}function mt(e,t){return t.transaction(()=>{let n=rt(O(t),e);return t.setSettings(n),it(n)})}var ht=class{storage;configFolder;openFolder;constructor(e,t=We(),n=Ue){this.storage=e,this.configFolder=t,this.openFolder=n}read(){return{...pt(this.storage),configFolder:this.configFolder}}update(e){return{...mt(e,this.storage),configFolder:this.configFolder}}openConfigFolder(){return this.openFolder(this.configFolder),{configFolder:this.configFolder}}};const gt=r.dirname(Se(import.meta.url)),k=`foundry`;var _t=class{configuredSkillRoot;homeDirectory;constructor({skillRoot:e,homeDirectory:t=ie()}={}){this.configuredSkillRoot=e,this.homeDirectory=r.resolve(t)}async copyToTarget(e){let t=this.configuredSkillRoot??vt();if(!u(r.join(t,`SKILL.md`)))throw new A(`SKILL_NOT_FOUND`,`The bundled Foundry skill was not found`);let n=this.getTargetParent(e),i=r.join(n,k),a=r.join(n,`.${k}-${y()}.tmp`),o=r.join(n,`.${k}-${y()}.backup`);try{return await me(n,{recursive:!0}),await fe(t,a,{recursive:!0,errorOnExist:!0}),await yt(a,i,o),{target:e,destination:i}}catch(e){throw await b(a,{recursive:!0,force:!0}),e instanceof A?e:new A(`SKILL_COPY_FAILED`,`Unable to copy the Foundry skill`,{cause:e})}}getTargetParent(e){return r.join(this.homeDirectory,e===`agents`?`.agents`:`.claude`,`skills`)}getTargets(){return{targets:[`agents`,`claude`].map(e=>({target:e,destination:r.join(this.getTargetParent(e),k)}))}}async copyToTargets(e){return{installed:await Array.fromAsync(e,e=>this.copyToTarget(e))}}},A=class extends Error{code;constructor(e,t,n){super(t,n),this.code=e,this.name=`BundledSkillCopyError`}};function vt(){let e=[r.resolve(gt,`skills`,k),r.resolve(gt,`..`,`..`,`..`,`skills`,k),r.resolve(i.cwd(),`skills`,k)].find(e=>u(r.join(e,`SKILL.md`)));if(!e)throw new A(`SKILL_NOT_FOUND`,`The bundled Foundry skill was not found`);return e}async function yt(e,t,n){let r=u(t);r&&await ye(t,n);try{await ye(e,t),r&&await b(n,{recursive:!0,force:!0})}catch(e){throw r&&!u(t)&&u(n)&&await ye(n,t),e}}const bt=`manifest.json`,xt=/* @__PURE__ */ new Set([bt,Ze]);async function St(e,t){let n=new we.ZipFile;for(let t of e)n.addFile(t.filePath,t.entryPath,{compress:!0});n.end(),await S(n.outputStream,l(t,{mode:384}))}async function Ct(e,t){let n=await wt(e),i=/* @__PURE__ */ new Map;try{await new Promise((e,a)=>{let o=e=>{a(e instanceof Error?e:Error(String(e)))},s=async e=>{if(!xt.has(e.fileName))throw Error(`The Foundry archive contains an unexpected file`);if(e.fileName.endsWith(`/`)||i.has(e.fileName))throw Error(`The Foundry archive contains an invalid file`);let a=r.join(t,e.fileName);await me(r.dirname(a),{recursive:!0,mode:448}),await S(await Tt(n,e),l(a,{mode:384})),i.set(e.fileName,a),n.readEntry()};n.on(`error`,o),n.on(`end`,e),n.on(`entry`,e=>{s(e).catch(o)}),n.readEntry()})}catch(e){throw await b(t,{recursive:!0,force:!0}),e}finally{n.close()}if(!i.has(bt)||!i.has(`data/foundry.json`))throw await b(t,{recursive:!0,force:!0}),Error(`The Foundry archive is incomplete`);return Object.fromEntries(i)}function wt(e){return new Promise((t,n)=>{Ce.open(e,{autoClose:!1,lazyEntries:!0,strictFileNames:!0},(e,r)=>{e?n(e):t(r)})})}function Tt(e,t){return new Promise((n,r)=>{e.openReadStream(t,(e,t)=>{e?r(e):n(t)})})}var Et=class extends Error{code;constructor(e,t,n){super(t,n),this.code=e,this.name=`BackupCryptoError`}};const j=x.from(`FOUNDRYX`,`ascii`),M=`aes-256-gcm`,Dt=`scrypt`,Ot={N:131072,r:8,p:1};async function kt(e,t,n){let r=le(16),i=le(12),a={version:1,cipher:M,kdf:{name:Dt,salt:r.toString(`base64`),...Ot,keyLength:32},nonce:i.toString(`base64`)},o=x.from(JSON.stringify(a),`utf8`),s=jt(o.byteLength),u=x.concat([s,o]),d=await Mt(n,r,Ot);try{let n=await ge(t,`w`,384);try{await n.write(u)}finally{await n.close()}await S(c(e),Nt(d,i,u),l(t,{flags:`r+`,mode:384,start:u.byteLength})),await de(t,384)}catch(e){throw await b(t,{force:!0}),new Et(`IO_ERROR`,`Unable to encrypt the Foundry export`,{cause:e})}finally{d.fill(0)}}async function At(e,t,n){let r;try{let i=(await be(e)).size;if(i<=29)throw Error(`The export file is incomplete`);let a=await ge(e,`r`),o,s,u;try{if(o=await It(a,13,0),!o.subarray(0,j.byteLength).equals(j))throw Error(`The export file signature is invalid`);if(o.readUInt8(j.byteLength)!==1)throw Error(`The export envelope version is unsupported`);let e=o.readUInt32BE(j.byteLength+1);if(e===0||e>65536)throw Error(`The export header is invalid`);if(i<=13+e+16)throw Error(`The export file is incomplete`);s=await It(a,e,13),u=await It(a,16,i-16)}finally{await a.close()}let d=Ft(JSON.parse(s.toString(`utf8`)));r=await Mt(n,d.salt,d.header.kdf);let f=x.concat([o,s]);await S(c(e,{start:13+s.byteLength,end:i-16-1}),Pt(r,d.nonce,f,u),l(t,{mode:384})),await de(t,384)}catch(e){throw await b(t,{force:!0}),e instanceof Et?e:new Et(Rt(e)?`IO_ERROR`:`INVALID_PASSPHRASE`,Rt(e)?`Unable to read or decrypt the backup file`:`The passphrase is incorrect or the export file is corrupted`,{cause:e})}finally{r?.fill(0)}}function jt(e){let t=x.alloc(13);return j.copy(t,0),t.writeUInt8(1,j.byteLength),t.writeUInt32BE(e,j.byteLength+1),t}function Mt(e,t,n){return new Promise((r,i)=>{ue(e,t,32,{...n,maxmem:Math.max(67108864,256*n.N*n.r)},(e,t)=>{e?i(e):r(t)})})}function Nt(e,t,n){let r=oe(M,e,t,{authTagLength:16});return r.setAAD(n),new Ee({transform(e,t,n){try{n(null,r.update(e))}catch(e){n(e instanceof Error?e:Error(String(e)))}},flush(e){try{e(null,x.concat([r.final(),r.getAuthTag()]))}catch(t){e(t instanceof Error?t:Error(String(t)))}}})}function Pt(e,t,n,r){let i=se(M,e,t,{authTagLength:16});return i.setAAD(n),new Ee({transform(e,t,n){try{n(null,i.update(e))}catch(e){n(e instanceof Error?e:Error(String(e)))}},flush(e){try{i.setAuthTag(r),e(null,i.final())}catch(t){e(t instanceof Error?t:Error(String(t)))}}})}function Ft(e){if(!Lt(e)||e.version!==1)throw Error(`Unsupported export envelope`);if(e.cipher!==M||!Lt(e.kdf))throw Error(`Unsupported export encryption`);let t=e.kdf;if(t.name!==Dt||typeof t.salt!=`string`||t.N!==Ot.N||t.r!==Ot.r||t.p!==Ot.p||t.keyLength!==32||typeof e.nonce!=`string`)throw Error(`Invalid export key derivation parameters`);let n=x.from(t.salt,`base64`),r=x.from(e.nonce,`base64`);if(n.byteLength!==16||r.byteLength!==12)throw Error(`Invalid export encryption parameters`);return{header:{version:1,cipher:M,kdf:{name:Dt,salt:t.salt,N:t.N,r:t.r,p:t.p,keyLength:t.keyLength},nonce:e.nonce},salt:n,nonce:r}}async function It(e,t,n){let r=x.alloc(t),{bytesRead:i}=await e.read(r,0,t,n);if(i!==t)throw Error(`Unexpected end of export file`);return r}function Lt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function Rt(e){return e instanceof Error&&`code`in e&&typeof e.code==`string`&&/^E[A-Z]+$/u.test(e.code)}async function zt(e){try{await b(e,{recursive:!0,force:!0})}catch{console.error(`Unable to remove backup temporary files`)}}function Bt(e){f(e,{recursive:!0,mode:448}),i.platform!==`win32`&&o(e,448)}function N(e){if(i.platform===`win32`)return;let t=p(e,`r`);try{d(t)}finally{s(t)}}function Vt(e,t){Ht(e,`${JSON.stringify(t,null,2)}\n`)}function Ht(e,t){Bt(r.dirname(e));let n=`${e}.${y()}.tmp`;try{let i=p(n,`wx`,384);try{ne(i,t),d(i)}finally{s(i)}ee(n,e),N(r.dirname(e))}finally{g(n,{force:!0})}}const Ut=/^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/u;var P=class extends Error{code=`INVALID_INSTRUCTION_INPUT`;constructor(e){super(e),this.name=`InstructionInputError`}},Wt=class extends Error{code=`INSTRUCTION_NOT_FOUND`;constructor(e){super(`Instruction not found: ${e}`),this.name=`InstructionNotFoundError`}},Gt=class extends Error{agentIds;code=`INSTRUCTION_IN_USE`;constructor(e){super(`Instruction is assigned to: ${e.join(`, `)}`),this.agentIds=e,this.name=`InstructionInUseError`}};function Kt(e){if(!qt(e))throw new P(`Instruction input must be an object`);if(typeof e.name!=`string`)throw new P(`name is required`);let t=e.name.trim();if(!t)throw new P(`name is required`);if(t.length>200)throw new P(`name must be at most 200 characters`);if(typeof e.content!=`string`||!e.content.trim())throw new P(`content is required`);if(e.content.length>1e6)throw new P(`content must be at most 1000000 characters`);return{name:t,content:e.content}}function qt(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}const Jt=/^---\r?\n([\s\S]*?)\r?\n---(?:\r?\n|$)/u;function Yt(e,t){let n=Date.now(),r={id:y(),name:e.name,content:e.content,createdAt:n,updatedAt:n},i=cn(t,r.id),a=en(r);return t.transaction(()=>{Ht(i,a)}),r}function Xt(e){let t=sn(e),n;try{n=h(t,{withFileTypes:!0}).filter(e=>e.isFile()&&e.name.endsWith(`.md`)).map(e=>e.name)}catch(e){if(ln(e))return[];throw e}return n.map(e=>nn(r.join(t,e),e)).toSorted((e,t)=>t.updatedAt-e.updatedAt)}function Zt(e,t){if(!Ut.test(e))return;let n=`${e}.md`;try{return nn(r.join(sn(t),n),n)}catch(e){if(ln(e))return;throw e}}function Qt(e,t,n){let r=Zt(e,n);if(!r)return;let i={...r,name:t.name,content:t.content,updatedAt:Date.now()};return n.transaction(()=>{Ht(cn(n,i.id),en(i))}),i}function $t(e,t){if(!Ut.test(e))return`missing`;let n=sn(t),i=r.join(n,`${e}.md`);return t.transaction(()=>{try{return g(i),N(n),`removed`}catch(e){if(ln(e))return`missing`;throw e}})}function en(e){return`---\n${Oe({id:e.id,name:e.name,createdAt:e.createdAt,updatedAt:e.updatedAt}).trimEnd()}\n---\n\n${e.content}`}function tn(e,t){let n=Jt.exec(e);if(!n)throw F(t);let r=rn(n[1]);if(`${r.id}.md`!==t)throw F(t);let i=e.slice(n[0].length);return i.startsWith(`\r
3
3
  `)?i=i.slice(2):i.startsWith(`
4
4
  `)&&(i=i.slice(1)),{...r,content:i}}function nn(e,t){return tn(m(e,`utf8`),t)}function rn(e){let t;try{t=De(e)}catch{throw F(`frontmatter`)}if(!un(t))throw F(`frontmatter`);let n=an(t.id),r=an(t.name);if(!r||r.length>200||!Ut.test(n))throw F(`frontmatter`);return{id:n,name:r,createdAt:on(t.createdAt),updatedAt:on(t.updatedAt)}}function an(e){return typeof e==`string`?e:``}function on(e){if(typeof e!=`number`||!Number.isSafeInteger(e)||e<0)throw F(`frontmatter`);return e}function sn(e){return r.join(e.path,`instructions`)}function cn(e,t){if(!Ut.test(t))throw Error(`Invalid instruction ID`);return r.join(sn(e),`${t}.md`)}function F(e){return/* @__PURE__ */ Error(`Invalid instruction file: ${e}`)}function ln(e){return e instanceof Error&&Reflect.get(e,`code`)===`ENOENT`}function un(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function dn(e){return e.transaction(()=>{let t=new Set(Ve);return{settings:e.readSettings(),instructions:Xt(e),agents:e.all(`agents`).filter(({id:e})=>t.has(e)).toSorted((e,t)=>e.id.localeCompare(t.id))}})}var fn=class{storage;constructor(e){this.storage=e}async createExport(e){let t=dn(this.storage),n=await he(r.join(ae(),`foundry-export-`));try{let i=(/* @__PURE__ */ new Date()).toISOString(),a=JSON.stringify(t,null,2),o=r.join(n,`foundry.json`),s=r.join(n,`manifest.json`),c=r.join(n,`foundry.zip`),l=r.join(n,`foundry.foundry-backup`),u={format:`foundry.data`,archiveVersion:2,appVersion:Le,createdAt:i,data:{path:Ze,bytes:x.byteLength(a,`utf8`),sha256:ce(`sha256`).update(a).digest(`hex`)}};return await xe(o,a,{mode:384}),await xe(s,JSON.stringify(u,null,2),{mode:384}),await St([{entryPath:`manifest.json`,filePath:s},{entryPath:Ze,filePath:o}],c),await kt(c,l,e),{fileName:`foundry-${i.replaceAll(`:`,`-`)}.foundry-backup`,contents:await _e(l)}}finally{await zt(n)}}},I=class extends Error{code;constructor(e,t){super(t),this.code=e}};function L(e){return typeof e==`object`&&!!e&&!Array.isArray(e)}function pn(e){if(!L(e)||e.format!==`foundry.data`||e.archiveVersion!==2||!L(e.data)||e.data.path!==`data/foundry.json`||typeof e.data.bytes!=`number`||!Number.isSafeInteger(e.data.bytes)||e.data.bytes<=0||typeof e.data.sha256!=`string`||!/^[a-f0-9]{64}$/u.test(e.data.sha256))throw new I(`INVALID_BACKUP`,`Unsupported or invalid backup manifest`);return{data:{path:Ze,bytes:e.data.bytes,sha256:e.data.sha256}}}function mn(e){if(!L(e)||!L(e.settings)||!Array.isArray(e.instructions)||!Array.isArray(e.agents))throw R(`Backup data must contain settings, instructions, and agents`);let t=hn(e.instructions),n=new Set(t.map(({id:e})=>e));return{settings:tt(e.settings),instructions:t,agents:gn(e.agents,n)}}function hn(e){let t=/* @__PURE__ */ new Set;return e.map((e,n)=>{if(!L(e))throw R(`Invalid instruction at index ${n}`);let r=e.id;if(typeof r!=`string`||!Ut.test(r)||t.has(r))throw R(`Invalid instruction at index ${n}`);let i=typeof e.name==`string`?e.name.trim():``;if(!i||i.length>200)throw R(`Invalid instruction name at index ${n}`);if(typeof e.content!=`string`||!e.content.trim()||e.content.length>1e6)throw R(`Invalid instruction content at index ${n}`);let a={id:r,name:i,content:e.content,createdAt:_n(e.createdAt,n),updatedAt:_n(e.updatedAt,n)};return t.add(r),a})}function gn(e,t){let n=new Set(Ve),r=/* @__PURE__ */ new Set;return e.map((e,i)=>{if(!L(e)||!L(e.instructions))throw R(`Invalid agent at index ${i}`);let a=e.id;if(typeof a!=`string`||!n.has(a)||r.has(a))throw R(`Invalid agent at index ${i}`);let o=e.instructions,s=o.instructionIds;if(!Array.isArray(s))throw R(`Invalid instruction assignment for agent ${a}`);let c=s.map(e=>{if(typeof e!=`string`)throw R(`Invalid instruction assignment for agent ${a}`);return e});if(new Set(c).size!==c.length||c.some(e=>!t.has(e)))throw R(`Invalid instruction assignment for agent ${a}`);if(typeof o.enabled!=`boolean`)throw R(`Invalid enabled state for agent ${a}`);if(o.enabled&&c.length===0)throw R(`Enabled agent ${a} has no instructions`);let l={id:a,instructions:{instructionIds:c,enabled:o.enabled,createdAt:_n(o.createdAt,i),updatedAt:_n(o.updatedAt,i)}};return r.add(a),l})}function _n(e,t){if(typeof e!=`number`||!Number.isSafeInteger(e)||e<0)throw R(`Invalid timestamp at index ${t}`);return e}function R(e){return new I(`INVALID_BACKUP`,e)}function vn(e,t){e.transaction(()=>{let n=new Set(t.instructions.map(({id:e})=>e));for(let t of Xt(e))n.has(t.id)||e.removeTextFile(yn(t.id));for(let n of t.instructions)e.putTextFile(yn(n.id),en(n));let r=new Set(t.agents.map(({id:e})=>e));for(let t of e.all(`agents`))r.has(t.id)||e.remove(`agents`,t.id);for(let n of t.agents)e.put(`agents`,n);e.setSettings(t.settings)})}function yn(e){return r.join(`instructions`,`${e}.md`)}var bn=class{storage;constructor(e){this.storage=e}async importData(e,t){let n=await he(r.join(ae(),`foundry-import-`));try{let i=r.join(n,`backup.zip`);await At(e,i,t);let a=await Ct(i,r.join(n,`extracted`)),o=pn(JSON.parse(await _e(a[`manifest.json`],`utf8`))),s=await _e(a[Ze]);if(s.byteLength!==o.data.bytes||ce(`sha256`).update(s).digest(`hex`)!==o.data.sha256)throw new I(`INVALID_BACKUP`,`Backup checksum does not match`);let c=mn(JSON.parse(s.toString(`utf8`)));try{vn(this.storage,c)}catch{throw new I(`IMPORT_FAILED`,`Unable to import data`)}}catch(e){throw e instanceof I?e:e instanceof Et?e.code===`INVALID_PASSPHRASE`?new I(`INVALID_PASSPHRASE`,`Incorrect passphrase or corrupted backup`):new I(`IMPORT_FAILED`,`Unable to read or decrypt the backup file`):new I(`INVALID_BACKUP`,`The backup is invalid or unsupported`)}finally{await zt(n)}}};function xn(e){return e.all(`agents`)}function Sn(e,t){return e.get(`agents`,t)}function Cn(e,t){return xn(e).filter(({instructions:e})=>e.instructionIds.includes(t)).map(({id:e})=>e)}function wn(e,t){return e.put(`agents`,t),t}var Tn=class{storage;constructor(e){this.storage=e}list(){return Xt(this.storage)}get(e){let t=Zt(e,this.storage);if(!t)throw new Wt(e);return t}create(e){return Yt(e,this.storage)}update(e,t){let n=Qt(e,t,this.storage);if(!n)throw new Wt(e);return n}delete(e){return this.storage.transaction(()=>{let t=Zt(e,this.storage);if(!t)throw new Wt(e);let n=Cn(this.storage,e);if(n.length>0)throw new Gt(n);if($t(e,this.storage)===`missing`)throw new Wt(e);return t})}};const En=`<!-- foundry:instructions:start -->`,Dn=`<!-- foundry:instructions:end -->`;function On(e,t){let n=kn(e),r=jn(t);return`${n}${An(n)}${r}\n`}function kn(e){let t=``,n=0;for(let r of Mn(e)){let e=Nn(r);e===`start`?n+=1:e===`end`?n=Math.max(0,n-1):e===`other`||n>0||(t+=r)}return t}function An(e){return!e||e.endsWith(`
5
5
  `)?``:`
Binary file
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "@dhzh/foundry",
3
3
  "type": "module",
4
- "version": "7.1.1",
4
+ "version": "7.1.2",
5
5
  "devEngines": {
6
6
  "packageManager": {
7
7
  "name": "pnpm",