typebulb 0.9.5 → 0.9.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/index.js CHANGED
@@ -1,17 +1,21 @@
1
1
  #!/usr/bin/env node
2
- import*as st from"fs/promises";import*as P from"path";import{spawn as Xs}from"child_process";import{EventEmitter as Ht}from"events";import*as Gt from"fs/promises";import{existsSync as re,readFileSync as hn}from"fs";import*as R from"path";import{pathToFileURL as mn}from"url";import{resolve as at}from"resolve.exports";import{init as Kt,parse as Yt}from"es-module-lexer";function Xt(r,e){let t=R.join(r,"package.json");try{return JSON.parse(hn(t,"utf8"))}catch{throw new Error(`--replace package '${e}' has no readable package.json at ${t}`)}}var it=["browser","import","default"],zt=["node","import","default"];function Qt(r){let e=r.indexOf("=");if(e===-1)throw new Error(`--replace must be <name>=<path> (got '${r}')`);let t=r.slice(0,e).trim(),n=r.slice(e+1).trim();if(!t)throw new Error(`--replace missing package name (got '${r}')`);if(!n)throw new Error(`--replace missing path for '${t}'`);if(t.startsWith("@"))throw new Error(`--replace does not support scoped names yet; '${t}' is scoped`);return{name:t,dir:R.resolve(n)}}async function Zt(r){let{name:e,dir:t}=r;if(!re(t))throw new Error(`--replace path for '${e}' does not exist: ${t}`);let n=Xt(t,e),s=gn(n,e),o=R.resolve(t,s);if(!re(o))throw new Error(`--replace package '${e}' entry not found on disk: ${o} \u2014 did you build it (e.g. \`pnpm run build\`)?`);let i=R.dirname(o),a=`/local/${e}/${R.basename(o)}`,c=yn(n,t);return await bn(e,o,i),{name:e,dir:t,entryAbs:o,serveDir:i,entryUrl:a,typesAbs:c}}function gn(r,e){if(r.exports!==void 0){let n;try{n=at(r,".",{browser:!0,conditions:it})}catch(o){throw new Error(`--replace package '${e}' "exports" does not resolve a browser entry (conditions: ${it.join(", ")}): ${o instanceof Error?o.message:o}`)}let s=ct(n);if(!s)throw new Error(`--replace package '${e}' "exports" did not resolve "." to a single file (conditions: ${it.join(", ")}); too complex to override.`);return s}let t=r.module??r.main;if(!t)throw new Error(`--replace package '${e}' has no "exports", "module", or "main" entry to resolve.`);return t}function yn(r,e){if(r.exports!==void 0)try{let n=at(r,".",{conditions:["types"]}),s=ct(n);if(s){let o=R.resolve(e,s);if(re(o))return o}}catch{}let t=r.types??r.typings;if(t){let n=R.resolve(e,t);if(re(n))return n}}function ct(r){if(typeof r=="string")return r;if(Array.isArray(r))return r.find(e=>typeof e=="string")}async function bn(r,e,t){await Kt;let n=R.normalize(t),s=new Set,o=[e];for(;o.length;){let i=o.shift();if(s.has(i))continue;s.add(i);let a;try{a=await Gt.readFile(i,"utf8")}catch{continue}let c,l;try{[c,,,l]=Yt(a,i)}catch{continue}if(i===e&&!l)throw new Error(`override package '${r}' entry is not an ES module (no import/export syntax); CommonJS or non-module entries aren't supported (esm.sh would have to transform it).`);for(let p of c){if(p.d===-2)continue;let d=p.n;if(d&&!d.startsWith("node:")){if(d.startsWith("./")||d.startsWith("../")||d.startsWith("/")){let h=vn(i,d,n);h&&!s.has(h)&&o.push(h);continue}throw new Error(`override package must ship self-contained (bundle its dependencies); ${r} externalizes '${d}'`)}}}}function vn(r,e,t){let n=e.replace(/[?#].*$/,""),s=R.resolve(R.dirname(r),n),o=[s,s+".js",s+".mjs",R.join(s,"index.js"),R.join(s,"index.mjs")];for(let i of o)if(R.normalize(i).startsWith(t)&&re(i))return i}function wn(r,e,t){let n=Xt(r,e),s;if(n.exports!==void 0)try{s=ct(at(n,t,{conditions:zt}))}catch(i){throw new Error(`--replace package '${e}' "exports" does not resolve a node entry for '${t}' (conditions: ${zt.join(", ")}): ${i instanceof Error?i.message:i}`)}else t==="."&&(s=n.main??n.module);if(!s)throw new Error(`--replace package '${e}' has no node export for '${t}'.`);let o=R.resolve(r,s);if(!re(o))throw new Error(`--replace package '${e}' built file not found: ${o} \u2014 did you build it (e.g. \`pnpm run build\`)?`);return mn(o).href}async function er(r,e){await Kt;let t;try{[t]=Yt(r)}catch{return r}let n="",s=0;for(let o of t){if(o.d!==-1)continue;let i=o.n;if(!i||i!==e.name&&!i.startsWith(e.name+"/"))continue;let a=i===e.name?".":"."+i.slice(e.name.length),c=wn(e.dir,e.name,a);n+=r.slice(s,o.s)+c,s=o.e}return n+r.slice(s)}function tr(r){let e={subcommand:"run",file:"",port:3e3,watch:!0,open:!0,server:!1,trust:!1,noTrust:!1,follow:!1,help:!1,version:!1};["check","predict","logs","stop","trust","untrust"].includes(r[0])&&(e.subcommand=r[0],r=r.slice(1));for(let n=0;n<r.length;n++){let s=r[n];if(s==="--help"||s==="-h")e.help=!0;else if(s==="--version"||s==="-V")e.version=!0;else if(s==="--no-watch")e.watch=!1;else if(s==="--no-open")e.open=!1;else if(s==="--server")e.server=!0;else if(s==="--trust")e.trust=!0;else if(s==="--no-trust")e.noTrust=!0;else if(s==="--mode"){let o=r[++n];(!o||o.startsWith("-"))&&(console.error("Missing value for --mode (e.g. --mode staging)"),process.exit(1)),e.mode=o}else if(s==="--follow"||s==="-f")e.follow=!0;else if(s==="--lines"||s==="-n"){let o=parseInt(r[++n],10);(isNaN(o)||o<0)&&(console.error(`Invalid --lines value: ${r[n]}`),process.exit(1)),e.lines=o}else if(s==="--port"||s==="-p"){let o=r[++n],i=parseInt(o,10);isNaN(i)&&(console.error(`Invalid port: ${o}`),process.exit(1)),e.port=i}else if(s==="--replace"||s.startsWith("--replace=")){let o=s.startsWith("--replace=")?s.slice(10):r[++n]??"";try{let i=Qt(o);if(e.local)throw new Error(`--replace can only be used once (got '${e.local.name}' and '${i.name}')`);e.local=i}catch(i){console.error(i instanceof Error?i.message:String(i)),process.exit(1)}}else s.startsWith("-")||(e.file=s)}return e}function rr(){console.log(`
2
+ import*as at from"fs/promises";import*as P from"path";import{spawn as ro}from"child_process";import{EventEmitter as zt}from"events";import*as Kt from"fs/promises";import{existsSync as ne,readFileSync as yn}from"fs";import*as R from"path";import{pathToFileURL as bn}from"url";import{resolve as dt}from"resolve.exports";import{init as Xt,parse as Qt}from"es-module-lexer";function Zt(r,e){let t=R.join(r,"package.json");try{return JSON.parse(yn(t,"utf8"))}catch{throw new Error(`--replace package '${e}' has no readable package.json at ${t}`)}}var lt=["browser","import","default"],Yt=["node","import","default"];function er(r){let e=r.indexOf("=");if(e===-1)throw new Error(`--replace must be <name>=<path> (got '${r}')`);let t=r.slice(0,e).trim(),n=r.slice(e+1).trim();if(!t)throw new Error(`--replace missing package name (got '${r}')`);if(!n)throw new Error(`--replace missing path for '${t}'`);if(t.startsWith("@"))throw new Error(`--replace does not support scoped names yet; '${t}' is scoped`);return{name:t,dir:R.resolve(n)}}async function tr(r){let{name:e,dir:t}=r;if(!ne(t))throw new Error(`--replace path for '${e}' does not exist: ${t}`);let n=Zt(t,e),s=vn(n,e),o=R.resolve(t,s);if(!ne(o))throw new Error(`--replace package '${e}' entry not found on disk: ${o} \u2014 did you build it (e.g. \`pnpm run build\`)?`);let i=R.dirname(o),a=`/local/${e}/${R.basename(o)}`,c=wn(n,t);return await xn(e,o,i),{name:e,dir:t,entryAbs:o,serveDir:i,entryUrl:a,typesAbs:c}}function vn(r,e){if(r.exports!==void 0){let n;try{n=dt(r,".",{browser:!0,conditions:lt})}catch(o){throw new Error(`--replace package '${e}' "exports" does not resolve a browser entry (conditions: ${lt.join(", ")}): ${o instanceof Error?o.message:o}`)}let s=ut(n);if(!s)throw new Error(`--replace package '${e}' "exports" did not resolve "." to a single file (conditions: ${lt.join(", ")}); too complex to override.`);return s}let t=r.module??r.main;if(!t)throw new Error(`--replace package '${e}' has no "exports", "module", or "main" entry to resolve.`);return t}function wn(r,e){if(r.exports!==void 0)try{let n=dt(r,".",{conditions:["types"]}),s=ut(n);if(s){let o=R.resolve(e,s);if(ne(o))return o}}catch{}let t=r.types??r.typings;if(t){let n=R.resolve(e,t);if(ne(n))return n}}function ut(r){if(typeof r=="string")return r;if(Array.isArray(r))return r.find(e=>typeof e=="string")}async function xn(r,e,t){await Xt;let n=R.normalize(t),s=new Set,o=[e];for(;o.length;){let i=o.shift();if(s.has(i))continue;s.add(i);let a;try{a=await Kt.readFile(i,"utf8")}catch{continue}let c,l;try{[c,,,l]=Qt(a,i)}catch{continue}if(i===e&&!l)throw new Error(`override package '${r}' entry is not an ES module (no import/export syntax); CommonJS or non-module entries aren't supported (esm.sh would have to transform it).`);for(let p of c){if(p.d===-2)continue;let d=p.n;if(d&&!d.startsWith("node:")){if(d.startsWith("./")||d.startsWith("../")||d.startsWith("/")){let h=Sn(i,d,n);h&&!s.has(h)&&o.push(h);continue}throw new Error(`override package must ship self-contained (bundle its dependencies); ${r} externalizes '${d}'`)}}}}function Sn(r,e,t){let n=e.replace(/[?#].*$/,""),s=R.resolve(R.dirname(r),n),o=[s,s+".js",s+".mjs",R.join(s,"index.js"),R.join(s,"index.mjs")];for(let i of o)if(R.normalize(i).startsWith(t)&&ne(i))return i}function Rn(r,e,t){let n=Zt(r,e),s;if(n.exports!==void 0)try{s=ut(dt(n,t,{conditions:Yt}))}catch(i){throw new Error(`--replace package '${e}' "exports" does not resolve a node entry for '${t}' (conditions: ${Yt.join(", ")}): ${i instanceof Error?i.message:i}`)}else t==="."&&(s=n.main??n.module);if(!s)throw new Error(`--replace package '${e}' has no node export for '${t}'.`);let o=R.resolve(r,s);if(!ne(o))throw new Error(`--replace package '${e}' built file not found: ${o} \u2014 did you build it (e.g. \`pnpm run build\`)?`);return bn(o).href}async function rr(r,e){await Xt;let t;try{[t]=Qt(r)}catch{return r}let n="",s=0;for(let o of t){if(o.d!==-1)continue;let i=o.n;if(!i||i!==e.name&&!i.startsWith(e.name+"/"))continue;let a=i===e.name?".":"."+i.slice(e.name.length),c=Rn(e.dir,e.name,a);n+=r.slice(s,o.s)+c,s=o.e}return n+r.slice(s)}function nr(r){let e={subcommand:"run",file:"",port:3e3,watch:!0,open:!0,server:!1,trust:!1,noTrust:!1,follow:!1,help:!1,version:!1};["check","predict","logs","stop","trust","untrust"].includes(r[0])&&(e.subcommand=r[0],r=r.slice(1));for(let n=0;n<r.length;n++){let s=r[n];if(s==="--help"||s==="-h")e.help=!0;else if(s==="--version"||s==="-V")e.version=!0;else if(s==="--no-watch")e.watch=!1;else if(s==="--no-open")e.open=!1;else if(s==="--server")e.server=!0;else if(s==="--trust")e.trust=!0;else if(s==="--no-trust")e.noTrust=!0;else if(s==="--mode"){let o=r[++n];(!o||o.startsWith("-"))&&(console.error("Missing value for --mode (e.g. --mode staging)"),process.exit(1)),e.mode=o}else if(s==="--follow"||s==="-f")e.follow=!0;else if(s==="--lines"||s==="-n"){let o=parseInt(r[++n],10);(isNaN(o)||o<0)&&(console.error(`Invalid --lines value: ${r[n]}`),process.exit(1)),e.lines=o}else if(s==="--port"||s==="-p"){let o=r[++n],i=parseInt(o,10);isNaN(i)&&(console.error(`Invalid port: ${o}`),process.exit(1)),e.port=i}else if(s==="--replace"||s.startsWith("--replace=")){let o=s.startsWith("--replace=")?s.slice(10):r[++n]??"";try{let i=er(o);if(e.local)throw new Error(`--replace can only be used once (got '${e.local.name}' and '${i.name}')`);e.local=i}catch(i){console.error(i instanceof Error?i.message:String(i)),process.exit(1)}}else s.startsWith("-")||(e.file=s)}return e}function sr(){console.log(`
3
3
  typebulb - Local bulb runner for Typebulb
4
4
 
5
5
  Usage:
6
6
  typebulb [file.bulb.md] Run a bulb (defaults to .bulb.md in cwd)
7
+ typebulb claude Launch claude.bulb, the built-in viewer for
8
+ Claude Code sessions (ships inside the CLI; no
9
+ download, no file to place, runs trusted).
7
10
  typebulb check [file.bulb.md] Type-check a bulb without running it
8
11
  typebulb predict [file] Report the capability a bulb probably needs
9
12
  (fs / AI / server.ts) without running it.
10
13
  typebulb logs [file|pid] Print a running bulb server's captured console
11
- (no arg: list running servers). For agents:
12
- fetch tb.server.log / errors of a bulb you
13
- launched without watching its terminal.
14
- typebulb stop [file|pid] Stop a running bulb server (no arg: list them).
14
+ (no arg: list this project's running servers).
15
+ For agents: fetch tb.server.log / errors of a
16
+ bulb you launched without watching its terminal.
17
+ typebulb stop [file|pid] Stop a running bulb server (no arg: list this
18
+ project's running servers; a pid stops any).
15
19
  typebulb trust [file] Remember a bulb as Trusted, so a later run grants
16
20
  fs/AI/server.ts without --trust (no arg: list the
17
21
  remembered-trusted bulbs).
@@ -76,10 +80,10 @@ Examples:
76
80
  typebulb my-editor.bulb.md
77
81
  typebulb --no-watch --port 8080 my-editor.bulb.md
78
82
  typebulb .
79
- `)}import{readFileSync as xn,existsSync as Sn}from"fs";import*as U from"path";function Rn(r){return[...r?[`.env.${r}.local`,`.env.${r}`]:[],".env.local",".env"]}function lt(r,e=process.cwd()){let t={},n=[];for(let s of Rn(r)){let o;try{o=xn(U.resolve(e,s),"utf-8")}catch{continue}n.push(s);for(let[i,a]of Pn(o))process.env[i]===void 0&&(process.env[i]=a,t[i]=s)}return{mode:r,sources:t,loaded:n}}function*Pn(r){for(let e of r.split(`
80
- `)){let t=e.trim();if(!t||t.startsWith("#"))continue;let n=t.indexOf("=");if(n===-1)continue;let s=t.slice(0,n).trim(),o=t.slice(n+1).trim();(o.startsWith('"')&&o.endsWith('"')||o.startsWith("'")&&o.endsWith("'"))&&(o=o.slice(1,-1)),yield[s,o]}}function En(r){let e=new Set,t=/process\.env\.([A-Za-z_$][\w$]*)|process\.env\[\s*['"]([^'"]+)['"]\s*\]/g;for(let n;n=t.exec(r);)e.add(n[1]??n[2]);return[...e]}function dt(r,e,t){let{sources:n,mode:s,loaded:o}=r,i=process.cwd(),a=U.dirname(e),c=`(mode: ${s??"none"})`,l=t?En(t):[],p=l.filter(m=>n[m]).map(m=>`${m} \u2190 ${n[m]}`);p.length?console.log(`env: ${p.join(", ")} ${c}`):o.length&&console.log(`env: loaded ${o.join(", ")} ${c}`),s&&!o.some(m=>m===`.env.${s}`||m===`.env.${s}.local`)&&console.log(`env: mode=${s} \u2014 no .env.${s} found; using .env`);let d=U.resolve(a)!==U.resolve(i);if(d)for(let m of[".env.local",".env"])Sn(U.join(a,m))&&console.log(`env: ${U.join(a,m)} present but not loaded (cwd is ${i})`);let h=l.filter(m=>process.env[m]===void 0);if(h.length){let m=d?` \u2014 try running from ${a}`:"";console.log(`env: server.ts reads ${h.join(", ")} but ${h.length>1?"they are":"it's"} unset${m}`)}}import*as G from"fs/promises";import*as le from"path";import{pathToFileURL as ps}from"url";var kn={code:{path:"code.tsx",language:"typescript"},css:{path:"styles.css",language:"css"},html:{path:"index.html",language:"html"},config:{path:"config.json",language:"json"},notes:{path:"notes.md",language:"markdown"},data:{path:"data.txt",language:"text"},infer:{path:"infer.md",language:"markdown"},insight:{path:"insight.json",language:"json"},server:{path:"server.ts",language:"typescript"}};function nr(r){try{let e=r.split(`
81
- `),t=0;if(e[t]?.trim()!=="---")return null;t++;let n=[];for(;t<e.length&&e[t]?.trim()!=="---";)n.push(e[t]),t++;if(e[t]?.trim()!=="---")return null;t++;let s=Tn(n);if(!s)return null;let o=new Map;for(;t<e.length;){let a=e[t]?.trim()?.match(/^\*\*(.+)\*\*$/);if(a){let c=a[1].trim();for(t++;t<e.length&&e[t]?.trim()==="";)t++;let l=e[t]?.match(/^(`{3,})(\w*)\s*$/);if(!l){t++;continue}let p=l[1];t++;let d=[];for(;t<e.length&&!e[t]?.match(new RegExp(`^${p}\\s*$`));)d.push(e[t]),t++;t++,o.set(c,d.join(`
82
- `))}else t++}return{frontmatter:s,files:o}}catch{return null}}function Tn(r){let e={};for(let t of r){let n=t.indexOf(":");if(n===-1)continue;let s=t.slice(0,n).trim(),o=t.slice(n+1).trim();switch(s){case"format":e.format=o;break;case"name":e.name=_n(o);break}}return!e.format?.startsWith("typebulb")||!e.name?null:e}function _n(r){return r.startsWith('"')&&r.endsWith('"')?r.slice(1,-1).replace(/\\"/g,'"'):r.startsWith("'")&&r.endsWith("'")?r.slice(1,-1):r}function sr(r){let e=t=>r.files.get(kn[t].path)||"";return{name:r.frontmatter.name,code:e("code"),css:e("css"),html:e("html"),config:e("config"),notes:e("notes"),data:e("data"),infer:e("infer"),insight:e("insight"),server:e("server")}}function Cn(r){try{return JSON.parse(r),!0}catch{}return!!(/^\s*<[\s\S]*>/.test(r)||/^---\s*$/m.test(r)||/^\w[\w\s]*:[ \t]/m.test(r))}function or(r){let e=r.trim();return e?Cn(e)?[e]:r.split(/\n\n\n+/).map(t=>t.trim()).filter(Boolean):[]}function ir(r){if(!r.trim())return{};try{return JSON.parse(r)}catch{return{}}}import{transform as On}from"sucrase";function ut(r,e={}){let t=e.serverOnly?["typescript"]:["typescript","jsx"];try{let{code:n}=On(r,{transforms:t,jsxRuntime:"automatic",jsxImportSource:e.jsxImportSource||"react",production:!0});return{code:n}}catch(n){return{code:"",error:String(n)}}}var me="https://esm.sh",ge="https://cdn.jsdelivr.net/npm/",pt="https://data.jsdelivr.com/v1/package/npm/";function ar(r){let e=(r||"").replace(/^\/+/,"").replace(/\/+$/,"");return e?e.split("/"):[]}var b=class r{constructor(e,t,n){let s=typeof e=="string"?r.parse(e):e;this.name=s.name,this.version=ne(t??s.version),this.subpath=ne(n??s.subpath)}static parse(e){let t=ar(e||"");if(!t.length)return new r({name:""});if(t[0].startsWith("@")){let s=t[0],[o,i]=cr(t[1]??""),a=ne(t.slice(2).join("/"));return new r({name:`${s}/${o}`,version:i,subpath:a})}else{let[s,o]=cr(t[0]),i=ne(t.slice(1).join("/"));return new r({name:s,version:o,subpath:i})}}static fromUrl(e){try{let t=new URL(e),n=new URL(me).host,s=new URL(ge).host;if(t.host===n){let o=ar(t.pathname.replace(/^\/v\d+\//,"/"));if(!o.length)return;let i=o[0].startsWith("@")?`${o[0]}/${o[1]??""}`:o[0];return r.parse(i)}if(t.host===s){let o=t.pathname.split("/npm/")[1];if(!o)return;let i=o.split("/")[0]||"";return r.parse(i)}return}catch{return}}static versionFromUrl(e){return r.fromUrl(e)?.version}format(){let e=this.version?`${this.name}@${this.version}`:this.name;return this.subpath?`${e}/${this.subpath}`:e}root(){return this.name}static rootOf(e){return r.parse(e).name}withVersion(e){return new r({name:this.name,version:ne(e),subpath:this.subpath})}withPreferredVersion(e,t){let n=e||t;return n?this.withVersion(n):this}static isBare(e){if(!e||e.startsWith(".")||e.startsWith("/"))return!1;let t=e.toLowerCase();return!t.startsWith("http://")&&!t.startsWith("https://")}},ne=r=>r&&r.length?r:void 0,cr=r=>{let e=r.indexOf("@");return e<0?[r,void 0]:[r.slice(0,e),ne(r.slice(e+1))]};async function _(r){try{return await r()}catch{return}}var ye=class{constructor(e,t){this.cache=e,this.http=t,this.esmHost=me,this.jsDelivrBase=ge,this.jsDelivrMeta=pt,this.pinMs=1e4,this.versionsIndexMs=1440*60*1e3,this.metaTtlMs=10080*60*1e3,this.pinCache=new Map}normalizeRelative(e){let t=e||"";return t.startsWith("./")?t.slice(2):t.replace(/^\/+/,"")}ensureLeadingDotSlash(e){return e.startsWith("./")?e:`./${e}`}baseDir(e){let t=typeof e=="string"?new b(e):e;return`${this.jsDelivrBase}${t.name}${t.version?`@${t.version}`:""}/`}file(e,t){return new URL(this.normalizeRelative(t),this.baseDir(e)).toString()}packageJson(e){return this.file(e,"package.json")}buildEsmUrl(e,t={}){let{target:n="es2022",bundle:s=!1,external:o}=t,i=new URLSearchParams({target:n});return s&&i.append("bundle",""),o?.length&&i.append("external",o.join(",")),`${this.esmHost}/${e}?${i.toString()}`}async pinEsmUrl(e,t="es2022"){let n=this.buildEsmUrl(e,{target:t}),s=await _(()=>this.http.head(n));return s?.ok?s.url||n:void 0}async resolveExactVersion(e){let t=Date.now(),n=this.pinCache.get(e);if(n&&t-n.ts<this.pinMs)return n.value;let s=await this.tryResolveFromUrls([this.buildEsmUrl(e),`${this.esmHost}/${e}`]);return this.pinCache.set(e,{value:s,ts:t}),s}async tryResolveFromUrls(e){for(let t of e){let n=await _(()=>this.http.head(t)),s=this.parseVersionFromUrl(n?.url||t);if(s)return s}}async fetchVersionsIndex(e){if(await this.cache.isNegative(e))return;let t=await this.cache.getIndex(e);if(t&&Date.now()-t.updatedAt<this.versionsIndexMs)return{versions:t.versions,distTags:t.distTags};let n=await _(()=>this.http.getJson(`${this.jsDelivrMeta}${encodeURIComponent(e)}`));if(!n?.versions?.length){await this.cache.recordNegative(e);return}await this.cache.clearNegative(e);let s=n.distTags&&Object.keys(n.distTags).length?n.distTags:void 0;return await this.cache.setIndex(e,n.versions,s),n}parseVersionFromUrl(e){let t=b.fromUrl(e)?.version;return t&&/\d+\.\d+\.\d+/.test(t)?t:void 0}async fetchPackageMeta(e,t){let n=await this.cache.getMeta(e,t);if(n&&Date.now()-n.updatedAt<this.metaTtlMs){let{dependencies:p,peerDependencies:d,peerDependenciesMeta:h}=n;return{name:e,version:t,dependencies:p,peerDependencies:d,peerDependenciesMeta:h}}let s=this.packageJson(new b(`${e}@${t}`)),o=await _(()=>this.http.getJson(s));if(!o)return;let i=p=>p&&Object.keys(p).length?p:void 0,a=i(o.dependencies),c=i(o.peerDependencies),l=i(o.peerDependenciesMeta);return await this.cache.setMeta(e,t,a,c,l),{name:e,version:t,dependencies:a,peerDependencies:c,peerDependenciesMeta:l}}};var lr=r=>r.startsWith("@types/"),be=r=>Object.keys(r?.peerDependencies||{}).filter(e=>!lr(e)),ft=r=>Object.keys(r?.dependencies||{}).filter(e=>!lr(e)),In=r=>be(r).filter(e=>!r?.peerDependenciesMeta?.[e]?.optional),ve=class{constructor(e){this.cdn=e}async resolve(e,t){let n=await this.fetchMeta(e),{allRoots:s,autoAddedPeers:o}=await this.expandWithPeers(n,t),i=this.computeFlags(s);return{allRoots:s,flags:i,autoAddedPeers:o}}async fetchMeta(e){return Promise.all(e.map(async({name:t,version:n})=>({name:t,version:n,meta:await this.cdn.fetchPackageMeta(t,n)})))}async expandWithPeers(e,t){let n=new Map(e.map(o=>[o.name,o])),s=[];for(let o of e)for(let i of In(o.meta))!n.has(i)&&!s.some(a=>a.name===i)&&s.push({name:i,requiredBy:o.name});for(let{name:o,requiredBy:i}of s)try{let a=await t(o),c=await this.cdn.fetchPackageMeta(o,a);n.set(o,{name:o,version:a,meta:c})}catch(a){console.warn(`[typebulb] Failed to resolve peer "${o}" for "${i}":`,a)}return{allRoots:[...n.values()],autoAddedPeers:s.filter(o=>n.has(o.name))}}computeFlags(e){let t=new Set(e.flatMap(o=>be(o.meta))),n=new Map;for(let o of e)for(let i of ft(o.meta))n.set(i,(n.get(i)||0)+1);let s=new Set([...n.entries()].filter(([,o])=>o>=2).map(([o])=>o));return new Map(e.map(o=>[o.name,{isPeerRoot:t.has(o.name),hasPeers:be(o.meta).length>0,isSharedDep:s.has(o.name)}]))}};var we=class{constructor(e,t,n){this.cache=e,this.cdn=t,this.semver=n}selectVersionFromIndex(e,t,n){return this.semver.selectBestVersion(e,{range:t,distTags:n})}async learnExactVersion(e){let t=await _(()=>this.cdn.fetchVersionsIndex(e));if(t?.versions?.length){let n=this.semver.selectBestVersion(t.versions,{distTags:t.distTags});if(n)return n}return this.cdn.resolveExactVersion(e)}async resolveExactForRoot(e,t){if(!t)return this.learnExactVersion(e);let n=await this.cache.getPinnedExact(e,t);if(n){if(this.semver.isExactVersion(n))return n;console.warn("[versionResolver] Rejecting invalid cached version for",e,":",n)}let s=await _(()=>this.cdn.fetchVersionsIndex(e));if(s?.versions?.length){let i=this.selectVersionFromIndex(s.versions,t,s.distTags);if(i){if(this.semver.isExactVersion(i))return await this.cache.setPinnedExact(e,t,i),i}else{console.warn("[versionResolver] Invalidating stale versions cache for",e,"- range",t,"not satisfied"),await this.cache.invalidateVersionsCache(e);let a=await _(()=>this.cdn.fetchVersionsIndex(e));if(a?.versions?.length){let c=this.selectVersionFromIndex(a.versions,t,a.distTags);if(c&&this.semver.isExactVersion(c))return await this.cache.setPinnedExact(e,t,c),c}}}let o=await this.cdn.resolveExactVersion(`${e}@${t}`);if(o&&this.semver.isExactVersion(o))return await this.cache.setPinnedExact(e,t,o),o}async effectivePackage(e,t){let n=new b(e),s=n.root(),o=t[s],i=o?await _(()=>this.cache.getPinnedExact(s,o))??await _(()=>this.resolveExactForRoot(s,o)):void 0;return{effectivePackage:i?n.withVersion(i).format():e,root:s,range:o,pinned:i}}};import{init as An,parse as Dn}from"es-module-lexer";var se=class r{constructor(e,t,n,s){this.version=e,this.cdn=t,this.peer=n,this.cache=s}extractImportsSync(e){let t=new Set;for(let n of r.importPatterns){n.lastIndex=0;for(let s of e.matchAll(n))b.isBare(s[1])&&t.add(s[1])}return Array.from(t)}async extractImports(e){let t=new Set,n=s=>{b.isBare(s)&&t.add(s)};try{await An;let[s]=Dn(e);s.forEach(o=>n(e.slice(o.s,o.e).trim()))}catch{return this.extractImportsSync(e)}return Array.from(t)}async buildImportMap(e,t,n){let s=(await this.extractImports(e)).filter(d=>!n?.has(b.rootOf(d))),o=[...new Set(s.map(b.rootOf))],i=await Promise.all(o.map(async d=>({name:d,version:await this.resolveVersion(d,t)}))),{allRoots:a,flags:c,autoAddedPeers:l}=await this.peer.resolve(i,d=>this.resolveVersion(d,t)),p=this.buildEntries([...s,...l.map(d=>d.name)],a,c,t);return{importMap:{imports:Object.fromEntries(p)},prefetchUrls:p.map(([,d])=>d)}}async resolveVersion(e,t){let n=t[e],s=n?`${e}@${n}`:e,o=await this.version.resolveExactForRoot(e,n);if(!o){let i=await _(()=>this.cdn.pinEsmUrl(s));if(!i)throw new Error(`Cannot resolve ${s}: no matching version is published (the package or version may not exist, or the registry was unreachable).`);o=b.versionFromUrl(i),o&&n&&await _(()=>this.cache.setPinnedExact(e,n,o))}if(!o)throw new Error(`Cannot resolve ${s}: no concrete version found.`);return o}buildEntries(e,t,n,s){let o=new Map(t.map(g=>[g.name,g])),i=g=>{let S=o.get(b.rootOf(g));return new b(g).withPreferredVersion(S.version,s[S.name]).format()},a=new Set([...n.entries()].filter(([,g])=>g.isPeerRoot||g.isSharedDep).map(([g])=>g)),c=new Set(e.filter(g=>g!==b.rootOf(g)).map(b.rootOf)),l=[],p=new Set,d=new Set(e.filter(g=>g===b.rootOf(g))),h=new Set;for(let g of e){let S=b.rootOf(g),j=o.get(S),{isPeerRoot:B,hasPeers:T,isSharedDep:u}=n.get(S),f=c.has(S),y=g!==S,v=!(B||u)&&(f||!T),C=this.singletonDepsOf(j,a),V=y&&d.has(S);V&&h.add(S);let N=V?[...C,S]:C.length?C:void 0;l.push([g,this.cdn.buildEsmUrl(i(g),{bundle:v,external:N})]),p.add(g)}let m=new Set([...a,...h]);for(let g of m)o.has(g)&&(p.has(g)||l.push([g,this.cdn.buildEsmUrl(i(g),{})]),l.push([`${g}/`,`${this.cdn.esmHost}/${i(g)}/`]));return l}singletonDepsOf(e,t){return[...new Set([...be(e.meta),...ft(e.meta)])].filter(n=>t.has(n))}};se.importPatterns=[/\bimport\s+(?:[^'";]*?from\s*)?['"]([^'"]+)['"]/g,/\bexport\s+[^'";]*?from\s*['"]([^'"]+)['"]/g];import{gt as dr,satisfies as $n,maxSatisfying as ht,major as Mn,prerelease as Fn,rsort as jn,valid as Nn}from"semver";var je=class{cmp(e,t){return e===t?0:dr(e,t)?1:dr(t,e)?-1:0}satisfies(e,t){return!e||!e.trim()?!0:!!$n(t,e,{includePrerelease:!0})}pickMaxSatisfying(e,t){if(!e?.length)return;let n=ht(e,t,{includePrerelease:!0});return n===null?void 0:n}pickLatest(e){return e?.length?jn(e)[0]:void 0}selectBestVersion(e,t){if(!e?.length)return;let n=t?.range?.trim()||"*",s=t?.preferStable??!0,o=t?.distTags?.latest;if(o&&e.includes(o)&&this.satisfies(n,o))return o;if(s){let a=ht(e,n,{includePrerelease:!1});if(a)return a}return ht(e,n,{includePrerelease:!0})??void 0}majorOf(e){return Mn(e)}isPrerelease(e){return Fn(e)!==null}isExactVersion(e){return Nn(e)!==null}},K=new je;function ur(r,e){let t=new ye(r,e),n=new ve(t),s=new we(r,t,K);return{packageService:new se(s,t,n,r),versionResolver:s,cdnClient:t,peerResolver:n}}import*as kr from"fs/promises";import*as $ from"path";var mt=[{name:"es5"},{name:"es2015.core"},{name:"es2015.collection"},{name:"es2015.promise"},{name:"es2015.iterable"},{name:"es2015.symbol"},{name:"es2015.symbol.wellknown"},{name:"es2015.generator"},{name:"es2015.proxy"},{name:"es2015.reflect"},{name:"es2016.array.include"},{name:"es2016.intl"},{name:"es2017.object"},{name:"es2017.string"},{name:"es2017.sharedmemory"},{name:"es2017.typedarrays"},{name:"es2017.date"},{name:"es2017.intl"},{name:"es2018.asynciterable"},{name:"es2018.asyncgenerator"},{name:"es2018.promise"},{name:"es2018.regexp"},{name:"es2018.intl"},{name:"es2019.array"},{name:"es2019.object"},{name:"es2019.string"},{name:"es2019.symbol"},{name:"es2019.intl"},{name:"es2020.bigint"},{name:"es2020.promise"},{name:"es2020.string"},{name:"es2020.sharedmemory"},{name:"es2020.date"},{name:"es2020.number"},{name:"es2020.symbol.wellknown"},{name:"es2020.intl"},{name:"es2021.promise"},{name:"es2021.string"},{name:"es2021.weakref"},{name:"es2021.intl"},{name:"es2022.array"},{name:"es2022.object"},{name:"es2022.error"},{name:"es2022.string"},{name:"es2022.regexp"},{name:"es2022.intl"},{name:"es2023.array"},{name:"es2023.collection"},{name:"es2023.intl",since:"5.5"},{name:"es2024.arraybuffer",since:"5.5"},{name:"es2024.collection",since:"5.5"},{name:"es2024.object",since:"5.5"},{name:"es2024.promise",since:"5.5"},{name:"es2024.regexp",since:"5.5"},{name:"es2024.sharedmemory",since:"5.5"},{name:"es2024.string",since:"5.5"},{name:"esnext.array",since:"5.5"},{name:"esnext.collection"},{name:"esnext.decorators"},{name:"esnext.disposable"},{name:"esnext.error",since:"5.5"},{name:"esnext.float16",since:"5.5"},{name:"esnext.iterator",since:"5.5"},{name:"esnext.object"},{name:"esnext.promise"},{name:"esnext.sharedmemory",since:"5.5"},{name:"esnext.intl"}],gt=[{name:"dom"},{name:"dom.iterable"},{name:"dom.asynciterable"}];var pr=`
83
+ `)}import{readFileSync as Pn,existsSync as En}from"fs";import*as U from"path";function kn(r){return[...r?[`.env.${r}.local`,`.env.${r}`]:[],".env.local",".env"]}function pt(r,e=process.cwd()){let t={},n=[];for(let s of kn(r)){let o;try{o=Pn(U.resolve(e,s),"utf-8")}catch{continue}n.push(s);for(let[i,a]of Tn(o))process.env[i]===void 0&&(process.env[i]=a,t[i]=s)}return{mode:r,sources:t,loaded:n}}function*Tn(r){for(let e of r.split(`
84
+ `)){let t=e.trim();if(!t||t.startsWith("#"))continue;let n=t.indexOf("=");if(n===-1)continue;let s=t.slice(0,n).trim(),o=t.slice(n+1).trim();(o.startsWith('"')&&o.endsWith('"')||o.startsWith("'")&&o.endsWith("'"))&&(o=o.slice(1,-1)),yield[s,o]}}function _n(r){let e=new Set,t=/process\.env\.([A-Za-z_$][\w$]*)|process\.env\[\s*['"]([^'"]+)['"]\s*\]/g;for(let n;n=t.exec(r);)e.add(n[1]??n[2]);return[...e]}function ft(r,e,t){let{sources:n,mode:s,loaded:o}=r,i=process.cwd(),a=U.dirname(e),c=`(mode: ${s??"none"})`,l=t?_n(t):[],p=l.filter(m=>n[m]).map(m=>`${m} \u2190 ${n[m]}`);p.length?console.log(`env: ${p.join(", ")} ${c}`):o.length&&console.log(`env: loaded ${o.join(", ")} ${c}`),s&&!o.some(m=>m===`.env.${s}`||m===`.env.${s}.local`)&&console.log(`env: mode=${s} \u2014 no .env.${s} found; using .env`);let d=U.resolve(a)!==U.resolve(i);if(d)for(let m of[".env.local",".env"])En(U.join(a,m))&&console.log(`env: ${U.join(a,m)} present but not loaded (cwd is ${i})`);let h=l.filter(m=>process.env[m]===void 0);if(h.length){let m=d?` \u2014 try running from ${a}`:"";console.log(`env: server.ts reads ${h.join(", ")} but ${h.length>1?"they are":"it's"} unset${m}`)}}import*as G from"fs/promises";import*as Qe from"path";import{pathToFileURL as ms}from"url";var Cn={code:{path:"code.tsx",language:"typescript"},css:{path:"styles.css",language:"css"},html:{path:"index.html",language:"html"},config:{path:"config.json",language:"json"},notes:{path:"notes.md",language:"markdown"},data:{path:"data.txt",language:"text"},infer:{path:"infer.md",language:"markdown"},insight:{path:"insight.json",language:"json"},server:{path:"server.ts",language:"typescript"}};function or(r){try{let e=r.split(`
85
+ `),t=0;if(e[t]?.trim()!=="---")return null;t++;let n=[];for(;t<e.length&&e[t]?.trim()!=="---";)n.push(e[t]),t++;if(e[t]?.trim()!=="---")return null;t++;let s=In(n);if(!s)return null;let o=new Map;for(;t<e.length;){let a=e[t]?.trim()?.match(/^\*\*(.+)\*\*$/);if(a){let c=a[1].trim();for(t++;t<e.length&&e[t]?.trim()==="";)t++;let l=e[t]?.match(/^(`{3,})(\w*)\s*$/);if(!l){t++;continue}let p=l[1];t++;let d=[];for(;t<e.length&&!e[t]?.match(new RegExp(`^${p}\\s*$`));)d.push(e[t]),t++;t++,o.set(c,d.join(`
86
+ `))}else t++}return{frontmatter:s,files:o}}catch{return null}}function In(r){let e={};for(let t of r){let n=t.indexOf(":");if(n===-1)continue;let s=t.slice(0,n).trim(),o=t.slice(n+1).trim();switch(s){case"format":e.format=o;break;case"name":e.name=On(o);break}}return!e.format?.startsWith("typebulb")||!e.name?null:e}function On(r){return r.startsWith('"')&&r.endsWith('"')?r.slice(1,-1).replace(/\\"/g,'"'):r.startsWith("'")&&r.endsWith("'")?r.slice(1,-1):r}function ir(r){let e=t=>r.files.get(Cn[t].path)||"";return{name:r.frontmatter.name,code:e("code"),css:e("css"),html:e("html"),config:e("config"),notes:e("notes"),data:e("data"),infer:e("infer"),insight:e("insight"),server:e("server")}}function An(r){try{return JSON.parse(r),!0}catch{}return!!(/^\s*<[\s\S]*>/.test(r)||/^---\s*$/m.test(r)||/^\w[\w\s]*:[ \t]/m.test(r))}function ar(r){let e=r.trim();return e?An(e)?[e]:r.split(/\n\n\n+/).map(t=>t.trim()).filter(Boolean):[]}function cr(r){if(!r.trim())return{};try{return JSON.parse(r)}catch{return{}}}import{transform as $n}from"sucrase";function ht(r,e={}){let t=e.serverOnly?["typescript"]:["typescript","jsx"];try{let{code:n}=$n(r,{transforms:t,jsxRuntime:"automatic",jsxImportSource:e.jsxImportSource||"react",production:!0});return{code:n}}catch(n){return{code:"",error:String(n)}}}var me="https://esm.sh",ge="https://cdn.jsdelivr.net/npm/",mt="https://data.jsdelivr.com/v1/package/npm/";function lr(r){let e=(r||"").replace(/^\/+/,"").replace(/\/+$/,"");return e?e.split("/"):[]}var b=class r{constructor(e,t,n){let s=typeof e=="string"?r.parse(e):e;this.name=s.name,this.version=se(t??s.version),this.subpath=se(n??s.subpath)}static parse(e){let t=lr(e||"");if(!t.length)return new r({name:""});if(t[0].startsWith("@")){let s=t[0],[o,i]=dr(t[1]??""),a=se(t.slice(2).join("/"));return new r({name:`${s}/${o}`,version:i,subpath:a})}else{let[s,o]=dr(t[0]),i=se(t.slice(1).join("/"));return new r({name:s,version:o,subpath:i})}}static fromUrl(e){try{let t=new URL(e),n=new URL(me).host,s=new URL(ge).host;if(t.host===n){let o=lr(t.pathname.replace(/^\/v\d+\//,"/"));if(!o.length)return;let i=o[0].startsWith("@")?`${o[0]}/${o[1]??""}`:o[0];return r.parse(i)}if(t.host===s){let o=t.pathname.split("/npm/")[1];if(!o)return;let i=o.split("/")[0]||"";return r.parse(i)}return}catch{return}}static versionFromUrl(e){return r.fromUrl(e)?.version}format(){let e=this.version?`${this.name}@${this.version}`:this.name;return this.subpath?`${e}/${this.subpath}`:e}root(){return this.name}static rootOf(e){return r.parse(e).name}withVersion(e){return new r({name:this.name,version:se(e),subpath:this.subpath})}withPreferredVersion(e,t){let n=e||t;return n?this.withVersion(n):this}static isBare(e){if(!e||e.startsWith(".")||e.startsWith("/"))return!1;let t=e.toLowerCase();return!t.startsWith("http://")&&!t.startsWith("https://")}},se=r=>r&&r.length?r:void 0,dr=r=>{let e=r.indexOf("@");return e<0?[r,void 0]:[r.slice(0,e),se(r.slice(e+1))]};async function T(r){try{return await r()}catch{return}}var ye=class{constructor(e,t){this.cache=e,this.http=t,this.esmHost=me,this.jsDelivrBase=ge,this.jsDelivrMeta=mt,this.pinMs=1e4,this.versionsIndexMs=1440*60*1e3,this.metaTtlMs=10080*60*1e3,this.pinCache=new Map}normalizeRelative(e){let t=e||"";return t.startsWith("./")?t.slice(2):t.replace(/^\/+/,"")}ensureLeadingDotSlash(e){return e.startsWith("./")?e:`./${e}`}baseDir(e){let t=typeof e=="string"?new b(e):e;return`${this.jsDelivrBase}${t.name}${t.version?`@${t.version}`:""}/`}file(e,t){return new URL(this.normalizeRelative(t),this.baseDir(e)).toString()}packageJson(e){return this.file(e,"package.json")}buildEsmUrl(e,t={}){let{target:n="es2022",bundle:s=!1,external:o}=t,i=new URLSearchParams({target:n});return s&&i.append("bundle",""),o?.length&&i.append("external",o.join(",")),`${this.esmHost}/${e}?${i.toString()}`}async pinEsmUrl(e,t="es2022"){let n=this.buildEsmUrl(e,{target:t}),s=await T(()=>this.http.head(n));return s?.ok?s.url||n:void 0}async resolveExactVersion(e){let t=Date.now(),n=this.pinCache.get(e);if(n&&t-n.ts<this.pinMs)return n.value;let s=await this.tryResolveFromUrls([this.buildEsmUrl(e),`${this.esmHost}/${e}`]);return this.pinCache.set(e,{value:s,ts:t}),s}async tryResolveFromUrls(e){for(let t of e){let n=await T(()=>this.http.head(t)),s=this.parseVersionFromUrl(n?.url||t);if(s)return s}}async fetchVersionsIndex(e){if(await this.cache.isNegative(e))return;let t=await this.cache.getIndex(e);if(t&&Date.now()-t.updatedAt<this.versionsIndexMs)return{versions:t.versions,distTags:t.distTags};let n=await T(()=>this.http.getJson(`${this.jsDelivrMeta}${encodeURIComponent(e)}`));if(!n?.versions?.length){await this.cache.recordNegative(e);return}await this.cache.clearNegative(e);let s=n.distTags&&Object.keys(n.distTags).length?n.distTags:void 0;return await this.cache.setIndex(e,n.versions,s),n}parseVersionFromUrl(e){let t=b.fromUrl(e)?.version;return t&&/\d+\.\d+\.\d+/.test(t)?t:void 0}async fetchPackageMeta(e,t){let n=await this.cache.getMeta(e,t);if(n&&Date.now()-n.updatedAt<this.metaTtlMs){let{dependencies:p,peerDependencies:d,peerDependenciesMeta:h}=n;return{name:e,version:t,dependencies:p,peerDependencies:d,peerDependenciesMeta:h}}let s=this.packageJson(new b(`${e}@${t}`)),o=await T(()=>this.http.getJson(s));if(!o)return;let i=p=>p&&Object.keys(p).length?p:void 0,a=i(o.dependencies),c=i(o.peerDependencies),l=i(o.peerDependenciesMeta);return await this.cache.setMeta(e,t,a,c,l),{name:e,version:t,dependencies:a,peerDependencies:c,peerDependenciesMeta:l}}};var ur=r=>r.startsWith("@types/"),be=r=>Object.keys(r?.peerDependencies||{}).filter(e=>!ur(e)),gt=r=>Object.keys(r?.dependencies||{}).filter(e=>!ur(e)),Dn=r=>be(r).filter(e=>!r?.peerDependenciesMeta?.[e]?.optional),ve=class{constructor(e){this.cdn=e}async resolve(e,t){let n=await this.fetchMeta(e),{allRoots:s,autoAddedPeers:o}=await this.expandWithPeers(n,t),i=this.computeFlags(s);return{allRoots:s,flags:i,autoAddedPeers:o}}async fetchMeta(e){return Promise.all(e.map(async({name:t,version:n})=>({name:t,version:n,meta:await this.cdn.fetchPackageMeta(t,n)})))}async expandWithPeers(e,t){let n=new Map(e.map(o=>[o.name,o])),s=[];for(let o of e)for(let i of Dn(o.meta))!n.has(i)&&!s.some(a=>a.name===i)&&s.push({name:i,requiredBy:o.name});for(let{name:o,requiredBy:i}of s)try{let a=await t(o),c=await this.cdn.fetchPackageMeta(o,a);n.set(o,{name:o,version:a,meta:c})}catch(a){console.warn(`[typebulb] Failed to resolve peer "${o}" for "${i}":`,a)}return{allRoots:[...n.values()],autoAddedPeers:s.filter(o=>n.has(o.name))}}computeFlags(e){let t=new Set(e.flatMap(o=>be(o.meta))),n=new Map;for(let o of e)for(let i of gt(o.meta))n.set(i,(n.get(i)||0)+1);let s=new Set([...n.entries()].filter(([,o])=>o>=2).map(([o])=>o));return new Map(e.map(o=>[o.name,{isPeerRoot:t.has(o.name),hasPeers:be(o.meta).length>0,isSharedDep:s.has(o.name)}]))}};var we=class{constructor(e,t,n){this.cache=e,this.cdn=t,this.semver=n}selectVersionFromIndex(e,t,n){return this.semver.selectBestVersion(e,{range:t,distTags:n})}async learnExactVersion(e){let t=await T(()=>this.cdn.fetchVersionsIndex(e));if(t?.versions?.length){let n=this.semver.selectBestVersion(t.versions,{distTags:t.distTags});if(n)return n}return this.cdn.resolveExactVersion(e)}async resolveExactForRoot(e,t){if(!t)return this.learnExactVersion(e);let n=await this.cache.getPinnedExact(e,t);if(n){if(this.semver.isExactVersion(n))return n;console.warn("[versionResolver] Rejecting invalid cached version for",e,":",n)}let s=await T(()=>this.cdn.fetchVersionsIndex(e));if(s?.versions?.length){let i=this.selectVersionFromIndex(s.versions,t,s.distTags);if(i){if(this.semver.isExactVersion(i))return await this.cache.setPinnedExact(e,t,i),i}else{console.warn("[versionResolver] Invalidating stale versions cache for",e,"- range",t,"not satisfied"),await this.cache.invalidateVersionsCache(e);let a=await T(()=>this.cdn.fetchVersionsIndex(e));if(a?.versions?.length){let c=this.selectVersionFromIndex(a.versions,t,a.distTags);if(c&&this.semver.isExactVersion(c))return await this.cache.setPinnedExact(e,t,c),c}}}let o=await this.cdn.resolveExactVersion(`${e}@${t}`);if(o&&this.semver.isExactVersion(o))return await this.cache.setPinnedExact(e,t,o),o}async effectivePackage(e,t){let n=new b(e),s=n.root(),o=t[s],i=o?await T(()=>this.cache.getPinnedExact(s,o))??await T(()=>this.resolveExactForRoot(s,o)):void 0;return{effectivePackage:i?n.withVersion(i).format():e,root:s,range:o,pinned:i}}};import{init as Mn,parse as Fn}from"es-module-lexer";var oe=class r{constructor(e,t,n,s){this.version=e,this.cdn=t,this.peer=n,this.cache=s}extractImportsSync(e){let t=new Set;for(let n of r.importPatterns){n.lastIndex=0;for(let s of e.matchAll(n))b.isBare(s[1])&&t.add(s[1])}return Array.from(t)}async extractImports(e){let t=new Set,n=s=>{b.isBare(s)&&t.add(s)};try{await Mn;let[s]=Fn(e);s.forEach(o=>n(e.slice(o.s,o.e).trim()))}catch{return this.extractImportsSync(e)}return Array.from(t)}async buildImportMap(e,t,n){let s=(await this.extractImports(e)).filter(d=>!n?.has(b.rootOf(d))),o=[...new Set(s.map(b.rootOf))],i=await Promise.all(o.map(async d=>({name:d,version:await this.resolveVersion(d,t)}))),{allRoots:a,flags:c,autoAddedPeers:l}=await this.peer.resolve(i,d=>this.resolveVersion(d,t)),p=this.buildEntries([...s,...l.map(d=>d.name)],a,c,t);return{importMap:{imports:Object.fromEntries(p)},prefetchUrls:p.map(([,d])=>d)}}async resolveVersion(e,t){let n=t[e],s=n?`${e}@${n}`:e,o=await this.version.resolveExactForRoot(e,n);if(!o){let i=await T(()=>this.cdn.pinEsmUrl(s));if(!i)throw new Error(`Cannot resolve ${s}: no matching version is published (the package or version may not exist, or the registry was unreachable).`);o=b.versionFromUrl(i),o&&n&&await T(()=>this.cache.setPinnedExact(e,n,o))}if(!o)throw new Error(`Cannot resolve ${s}: no concrete version found.`);return o}buildEntries(e,t,n,s){let o=new Map(t.map(g=>[g.name,g])),i=g=>{let S=o.get(b.rootOf(g));return new b(g).withPreferredVersion(S.version,s[S.name]).format()},a=new Set([...n.entries()].filter(([,g])=>g.isPeerRoot||g.isSharedDep).map(([g])=>g)),c=new Set(e.filter(g=>g!==b.rootOf(g)).map(b.rootOf)),l=[],p=new Set,d=new Set(e.filter(g=>g===b.rootOf(g))),h=new Set;for(let g of e){let S=b.rootOf(g),j=o.get(S),{isPeerRoot:B,hasPeers:C,isSharedDep:u}=n.get(S),f=c.has(S),y=g!==S,v=!(B||u)&&(f||!C),_=this.singletonDepsOf(j,a),H=y&&d.has(S);H&&h.add(S);let N=H?[..._,S]:_.length?_:void 0;l.push([g,this.cdn.buildEsmUrl(i(g),{bundle:v,external:N})]),p.add(g)}let m=new Set([...a,...h]);for(let g of m)o.has(g)&&(p.has(g)||l.push([g,this.cdn.buildEsmUrl(i(g),{})]),l.push([`${g}/`,`${this.cdn.esmHost}/${i(g)}/`]));return l}singletonDepsOf(e,t){return[...new Set([...be(e.meta),...gt(e.meta)])].filter(n=>t.has(n))}};oe.importPatterns=[/\bimport\s+(?:[^'";]*?from\s*)?['"]([^'"]+)['"]/g,/\bexport\s+[^'";]*?from\s*['"]([^'"]+)['"]/g];import{gt as pr,satisfies as jn,maxSatisfying as yt,major as Nn,prerelease as Ln,rsort as Bn,valid as Un}from"semver";var Ne=class{cmp(e,t){return e===t?0:pr(e,t)?1:pr(t,e)?-1:0}satisfies(e,t){return!e||!e.trim()?!0:!!jn(t,e,{includePrerelease:!0})}pickMaxSatisfying(e,t){if(!e?.length)return;let n=yt(e,t,{includePrerelease:!0});return n===null?void 0:n}pickLatest(e){return e?.length?Bn(e)[0]:void 0}selectBestVersion(e,t){if(!e?.length)return;let n=t?.range?.trim()||"*",s=t?.preferStable??!0,o=t?.distTags?.latest;if(o&&e.includes(o)&&this.satisfies(n,o))return o;if(s){let a=yt(e,n,{includePrerelease:!1});if(a)return a}return yt(e,n,{includePrerelease:!0})??void 0}majorOf(e){return Nn(e)}isPrerelease(e){return Ln(e)!==null}isExactVersion(e){return Un(e)!==null}},K=new Ne;function fr(r,e){let t=new ye(r,e),n=new ve(t),s=new we(r,t,K);return{packageService:new oe(s,t,n,r),versionResolver:s,cdnClient:t,peerResolver:n}}import*as _r from"fs/promises";import*as D from"path";var bt=[{name:"es5"},{name:"es2015.core"},{name:"es2015.collection"},{name:"es2015.promise"},{name:"es2015.iterable"},{name:"es2015.symbol"},{name:"es2015.symbol.wellknown"},{name:"es2015.generator"},{name:"es2015.proxy"},{name:"es2015.reflect"},{name:"es2016.array.include"},{name:"es2016.intl"},{name:"es2017.object"},{name:"es2017.string"},{name:"es2017.sharedmemory"},{name:"es2017.typedarrays"},{name:"es2017.date"},{name:"es2017.intl"},{name:"es2018.asynciterable"},{name:"es2018.asyncgenerator"},{name:"es2018.promise"},{name:"es2018.regexp"},{name:"es2018.intl"},{name:"es2019.array"},{name:"es2019.object"},{name:"es2019.string"},{name:"es2019.symbol"},{name:"es2019.intl"},{name:"es2020.bigint"},{name:"es2020.promise"},{name:"es2020.string"},{name:"es2020.sharedmemory"},{name:"es2020.date"},{name:"es2020.number"},{name:"es2020.symbol.wellknown"},{name:"es2020.intl"},{name:"es2021.promise"},{name:"es2021.string"},{name:"es2021.weakref"},{name:"es2021.intl"},{name:"es2022.array"},{name:"es2022.object"},{name:"es2022.error"},{name:"es2022.string"},{name:"es2022.regexp"},{name:"es2022.intl"},{name:"es2023.array"},{name:"es2023.collection"},{name:"es2023.intl",since:"5.5"},{name:"es2024.arraybuffer",since:"5.5"},{name:"es2024.collection",since:"5.5"},{name:"es2024.object",since:"5.5"},{name:"es2024.promise",since:"5.5"},{name:"es2024.regexp",since:"5.5"},{name:"es2024.sharedmemory",since:"5.5"},{name:"es2024.string",since:"5.5"},{name:"esnext.array",since:"5.5"},{name:"esnext.collection"},{name:"esnext.decorators"},{name:"esnext.disposable"},{name:"esnext.error",since:"5.5"},{name:"esnext.float16",since:"5.5"},{name:"esnext.iterator",since:"5.5"},{name:"esnext.object"},{name:"esnext.promise"},{name:"esnext.sharedmemory",since:"5.5"},{name:"esnext.intl"}],vt=[{name:"dom"},{name:"dom.iterable"},{name:"dom.asynciterable"}];var hr=`
83
87
  /**
84
88
  * Get raw data chunk from the Data tab.
85
89
  * @param index - Chunk index (0-based). Separate chunks with 2 blank lines.
@@ -90,7 +94,7 @@ Examples:
90
94
  * @param index - Chunk index (0-based)
91
95
  * @throws If chunk is not valid JSON/JSON-ish
92
96
  */
93
- json<T = unknown>(index: number): T;`,fr=`
97
+ json<T = unknown>(index: number): T;`,mr=`
94
98
  /**
95
99
  * Get the insight data produced by the inference layer.
96
100
  *
@@ -99,7 +103,7 @@ Examples:
99
103
  *
100
104
  * @returns The parsed insight JSON, or undefined if no insight is available
101
105
  */
102
- insight<T = unknown>(): T | undefined;`,hr=`
106
+ insight<T = unknown>(): T | undefined;`,gr=`
103
107
  /**
104
108
  * General-purpose AI call.
105
109
  *
@@ -116,7 +120,7 @@ Examples:
116
120
  model?: string;
117
121
  /** Enable/disable web search. Default: on for BYOK, always off for free model. */
118
122
  webSearch?: boolean;
119
- }): Promise<{ text: string }>;`,mr=`
123
+ }): Promise<{ text: string }>;`,yr=`
120
124
  /**
121
125
  * Returns AI models available to the current user.
122
126
  * Models are filtered by the user's configured API keys.
@@ -131,7 +135,7 @@ Examples:
131
135
  friendlyName: string;
132
136
  /** Provider display name, e.g. "Anthropic" */
133
137
  providerName: string;
134
- }>>;`,Ln="\n /**\n * The bulb's theme override (`<html data-theme>`).\n *\n * - Get: the current override \u2014 `'dark'` | `'light'`, or `undefined` when\n * following the OS preference.\n * - Set `'dark'`/`'light'` to force and persist it (per-bulb); set\n * `undefined` to clear the override and follow the OS again.\n *\n * Drives `<html data-theme>`, so render off `html[data-theme=\"\u2026\"]` selectors\n * (or observe the attribute) rather than reading `tb.theme`.\n */\n theme: 'light' | 'dark' | undefined;",gr=`
138
+ }>>;`,Wn="\n /**\n * The bulb's theme override (`<html data-theme>`).\n *\n * - Get: the current override \u2014 `'dark'` | `'light'`, or `undefined` when\n * following the OS preference.\n * - Set `'dark'`/`'light'` to force and persist it (per-bulb); set\n * `undefined` to clear the override and follow the OS again.\n *\n * Drives `<html data-theme>`, so render off `html[data-theme=\"\u2026\"]` selectors\n * (or observe the attribute) rather than reading `tb.theme`.\n */\n theme: 'light' | 'dark' | undefined;",br=`
135
139
  /**
136
140
  * The mode this bulb is running in.
137
141
  *
@@ -141,7 +145,7 @@ Examples:
141
145
  * - \`'embedded'\` \u2014 Running as a bulb embedded inside another bulb (sandboxed,
142
146
  * client-only: AI, filesystem, and server RPC are unavailable)
143
147
  */
144
- mode: 'local' | 'editor' | 'published' | 'embedded';`,yr=`
148
+ mode: 'local' | 'editor' | 'published' | 'embedded';`,vr=`
145
149
  /**
146
150
  * Local filesystem access (CLI only).
147
151
  *
@@ -155,7 +159,7 @@ Examples:
155
159
  readBytes(path: string): Promise<Uint8Array>;
156
160
  /** Write text or raw bytes to a file. Creates parent directories if needed. */
157
161
  write(path: string, content: string | Uint8Array): Promise<boolean>;
158
- };`,Bn=`
162
+ };`,Jn=`
159
163
  /**
160
164
  * Server-side function proxy. The built-in \`log\` prints to CLI stdout
161
165
  * (falls back to console.log on web). On the server side, this object only
@@ -163,7 +167,7 @@ Examples:
163
167
  */
164
168
  server: {
165
169
  log(...args: any[]): Promise<void>;
166
- };`,Un=`
170
+ };`,Hn=`
167
171
  /**
168
172
  * Async value inspector for tensor-like objects.
169
173
  *
@@ -230,7 +234,7 @@ Examples:
230
234
  *
231
235
  * @returns The full canonical URL
232
236
  */
233
- url(): Promise<string>;`,Wn=`
237
+ url(): Promise<string>;`,Vn=`
234
238
  /**
235
239
  * Server-side function proxy.
236
240
  *
@@ -238,23 +242,23 @@ Examples:
238
242
  * \`tb.server.log(...)\` is a built-in that prints to CLI stdout (falls back to console.log on web).
239
243
  * User exports override built-ins of the same name.
240
244
  */
241
- server: Record<string, (...args: any[]) => Promise<any>>;`,yt=`
245
+ server: Record<string, (...args: any[]) => Promise<any>>;`,wt=`
242
246
  /**
243
247
  * Typebulb utilities namespace.
244
248
  * Type \`tb.\` to discover available helpers.
245
249
  */
246
- declare const tb: {${pr}${Un}${fr}${Wn}${hr}${yr}${mr}${Ln}${gr}
250
+ declare const tb: {${hr}${Hn}${mr}${Vn}${gr}${vr}${yr}${Wn}${br}
247
251
  };
248
- `,bt=`
252
+ `,xt=`
249
253
  /**
250
254
  * Typebulb utilities namespace (server-side).
251
255
  * Type \`tb.\` to discover available helpers.
252
256
  */
253
- declare const tb: {${pr}${fr}${hr}${yr}${Bn}${mr}${gr}
257
+ declare const tb: {${hr}${mr}${gr}${vr}${Jn}${yr}${br}
254
258
  };
255
- `;var Y=class{constructor(e){this.store=e}async isNegative(e){let t=await Ne(()=>this.store.get(e));return!!t&&t.until>Date.now()}async recordNegative(e){let n=((await Ne(()=>this.store.get(e)))?.attempts||0)+1;await Ne(()=>this.store.set(e,{until:Date.now()+Jn(n),attempts:n}))}async clearNegative(e){await Ne(()=>this.store.delete(e))}};function Jn(r){return Math.min(9e5*Math.pow(2,Math.max(0,r-1)),864e5)}async function Ne(r){try{return await r()}catch{return}}import Kn from"p-limit";import{resolve as br}from"resolve.exports";var oe=class{constructor(e){this.fetchDts=e}fetchDtsText(e){return this.tryUrls([e])}async tryUrls(e){for(let t of e){let n=await this.fetchDts(t);if(n&&(this.looksLikeDts(n.dts)||/\.(d\.ts|d\.mts)(?:[?#].*)?$/i.test(n.url)||/[?&]dts(?:[&#]|$)/i.test(n.url)))return n}}looksLikeDts(e){return/^\s*export\s*\{\s*\}\s*;?\s*$/m.test(e)?!0:/declare\s+(module|namespace|class|interface|function|const|var|let)/.test(e)||/interface\s+\w+/.test(e)||/type\s+\w+\s*=/.test(e)}};var H={maxRelativeTypeRefs:500,maxBareDeps:8,maxBareDepth:3,prefetchConcurrency:4,negativeTtlMs:1e4},xe=["index.d.ts","index.d.mts"],Se=/\.d\.(ts|mts)$/i;function vt(r){if(Se.test(r))return!0;try{return new URL(r,"file://").search.includes("dts")}catch{return r.includes("?")&&r.includes("dts")}}function Le(r){return[`${r}.d.ts`,`${r}.d.mts`]}async function Re(r,{retries:e=2,timeoutMs:t=15e3,init:n}={}){for(let s=0;s<=e;s++){let o=new AbortController,i=setTimeout(()=>o.abort(),t);try{let a=await fetch(r,{...n,signal:o.signal});if(Vn(a.status)&&s<e)continue;return a}catch{if(s<e)continue;return}finally{clearTimeout(i)}}}function Vn(r){return r===408||r===429||r>=500&&r<600}var Pe=class extends oe{constructor(e,t){super(e),this.cdnClient=t}async loadPackageAtVersionedRoot(e,t){let n=this.cdnClient.packageJson(new b({name:e,version:t})),s=await Re(n);if(!s?.ok)return;let o;try{o=await s.json()}catch{return}if(o)return{pkg:o,baseDir:this.cdnClient.baseDir(new b({name:e,version:t??o.version})),version:t}}extractPathFromResult(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.find(t=>typeof t=="string")}async tryUntilSuccess(e,t){for(let n of e){let s=await t(n);if(s)return s}}async resolveFromSelected(e,t){if(e.kind==="types"){let s=this.cdnClient.normalizeRelative(e.path);if(!s||s==="/"||s===".")return this.tryUntilSuccess([...xe],t);if(!Se.test(s)){let o=await this.tryUntilSuccess(this.declarationCandidatesFor(s),t);if(o)return o}return t(s)}let n=this.declarationCandidatesFor(e.path);return this.tryUntilSuccess([...xe,...n],t)}toResolutionResult(e){return{kind:Se.test(e)?"types":"probe",path:e}}resolveExportsPath(e,t){let n=t||".",s=e;try{let o=this.extractPathFromResult(br(s,n,{conditions:["types"]}));if(o)return o}catch{}try{return this.extractPathFromResult(br(s,n,{browser:!0,conditions:["import","default","module","browser","node"]}))}catch{return}}async resolve(e){try{let t=b.parse(e),{pkg:n,baseDir:s}=await this.loadPackageAtVersionedRoot(t.name,t.version)||{};if(!n||!s)return;let o={name:t.name,version:t.version},i=new b(o).format(),a=new b({...o,subpath:t.subpath}).format(),c=d=>this.fetchCandidateFrom(s,t.name,d);if(t.subpath){let d=this.cdnClient.ensureLeadingDotSlash(t.subpath),h=this.resolveExportsPath(n,d);if(h){let g=await this.resolveFromSelected(this.toResolutionResult(h),c);if(g)return{...g,resolvedPkg:a}}let m=await this.tryUntilSuccess(this.declarationCandidatesFor(d),c);if(m)return{...m,resolvedPkg:a}}let l=n.types??n.typings;if(l){let d=await this.resolveFromSelected({kind:"types",path:l},c);if(d)return{...d,resolvedPkg:i}}let p=this.resolveExportsPath(n,".");if(p){let d=await this.resolveFromSelected(this.toResolutionResult(p),c);if(d)return{...d,resolvedPkg:i}}return}catch{return}}async fetchCandidateFrom(e,t,n){let s=this.cdnClient.normalizeRelative(n),o=new URL(s,e).toString(),i=await this.fetchDtsText(o);return i?{dts:i.dts,url:i.url,resolvedPkg:t}:void 0}declarationCandidatesFor(e){if(!e||e==="./"||e==="/")return[...xe];let t=e.replace(/\.(mjs|cjs|js|mts|cts|ts)$/i,""),n=Le(t),s=t.endsWith("/")?t:`${t}/`;return n.push(...Le(`${s}index`)),n}};import{gunzipSync as Hn}from"fflate";var ie=class{async fetchAndExtract(e,t){let n=this.getTarballUrl(e,t),s=await Re(n,{timeoutMs:3e4});if(!s?.ok)return new Map;let o=new Uint8Array(await s.arrayBuffer());return this.extractDtsFiles(o)}getTarballUrl(e,t){return`https://registry.npmjs.org/${e.replace("/","%2F")}/-/${e.split("/").pop()}-${t}.tgz`}normalizeTarPath(e){let t=e.replace(/^package\//,""),n=t.indexOf("/");return n>0?t.substring(n+1):t}extractDtsFiles(e){let t=new Map;try{let n=Hn(e),s=new TextDecoder("utf-8"),o=0;for(;o<n.length-512;){let i=n.slice(o,o+512);if(i[0]===0)break;let a=i.slice(0,100),c=a.indexOf(0),l=s.decode(a.slice(0,c>0?c:100)).trim(),p=i.slice(124,136),d=s.decode(p).trim().replace(/\0/g,""),h=parseInt(d,8)||0,m=String.fromCharCode(i[156]);if(o+=512,(m==="0"||m==="\0")&&(l.endsWith(".d.ts")||l.endsWith(".d.mts"))){let g=n.slice(o,o+h);t.set(this.normalizeTarPath(l),s.decode(g))}o+=Math.ceil(h/512)*512}}catch{}return t}},qn=new ie;var Ee=class extends oe{constructor(e,t,n,s=new ie){super(e),this.cdnClient=t,this.cache=n,this.tarballFetcher=s}typesNameCandidates(e){let t=e.startsWith("@")?e.slice(1).replace("/","__"):e;return t.includes(".")?[t,t.split(".").join("-"),t.split(".").join("")]:[t]}selectTypesVersion(e,t,n){try{if(n){let o=K.majorOf(n),i=e.filter(a=>K.majorOf(a)===o);if(i.length)return i.sort((a,c)=>K.cmp(c,a))[0]}let s=t?.latest;return s&&e.includes(s)?s:e[0]}catch{return e[0]}}async fetchFromVersionedRoot(e,t){let n=new b(e),s=n.version;if(!s)return;let o;try{o=await this.tarballFetcher.fetchAndExtract(n.name,s)}catch{return}if(!o||o.size===0)return;for(let[a,c]of o.entries()){let l=this.cdnClient.file(e,a);try{await this.cache.setCachedFile(l,c)}catch{}}let i=t.subpath?[t.subpath+".d.ts",t.subpath+"/index.d.ts"]:["index.d.ts"];for(let a of i){let c=o.get(a);if(c)return{dts:c,url:this.cdnClient.file(e,a),resolvedPkg:t.subpath?new b(t).format():t.name}}}async resolve(e){let t=b.parse(e);if(t.name)for(let n of this.typesNameCandidates(t.name)){let s=`@types/${n}`,o;try{o=await this.cdnClient.fetchVersionsIndex(s)}catch{continue}if(!o?.versions?.length)continue;let i=this.selectTypesVersion(o.versions,o.distTags,t.version),a=await this.fetchFromVersionedRoot(`${s}@${i}`,t);if(a)return a}}};var ke=class{collectRelativeTypeRefs(e){return this.collectRefs(e).filter(t=>t.startsWith("./")||t.startsWith("../"))}collectBareModuleRefs(e){return this.collectRefs(e).filter(t=>this.isBare(t))}collectRefs(e){let t=new Set,n=(s,o)=>(t.add(s[o]),null);return this.matchAll(e,/(import|export)\s+[^'"\n]*from\s*['"]([^'"\n]+)['"]/,s=>n(s,2)),this.matchAll(e,/export\s*\*\s*from\s*['"]([^'"\n]+)['"]/,s=>n(s,1)),Array.from(t)}matchAll(e,t,n){let s=[];try{let o=new RegExp(t.source,"g"),i;for(;i=o.exec(e);){let a=n(i);a!==null&&s.push(a)}}catch{}return s}isBare(e){return!(e.startsWith("./")||e.startsWith("../")||e.startsWith("file:")||e.startsWith("http://")||e.startsWith("https://"))}};var vr="file:///node_modules",Te=class{constructor(){this.epoch=0,this.listeners=new Set}getEpoch(){return this.epoch}bumpEpoch(){this.epoch+=1;for(let e of this.listeners)try{e(this.epoch)}catch{}return this.epoch}onEpochChange(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}epochDir(){return`__tsepoch_${this.epoch}`}pathForMain(e){return`${vr}/${this.epochDir()}/${e}/index.d.ts`}pathFor(e,t){let n=zn(t);return`${vr}/${this.epochDir()}/${e}/${n}`}};function zn(r){let e=r||"";return e.startsWith("./")?e.slice(2):e.replace(/^\/+/,"")}import{LRUCache as Gn}from"lru-cache";function wt(r){let e=new Gn({ttl:1e4,max:500}),t=new Map;return async function(s){try{if(await r.isNegative(s))return;let o=e.get(s);if(o&&Date.now()-o<1e4)return;let i=await r.getCachedFile(s);if(i)return{dts:i,url:s};let a=t.get(s);if(a)return a;let c=(async()=>{let l=await fetch(s,{cache:"no-store"});if(!l.ok){l.status===404&&(e.set(s,Date.now()),await r.recordNegative(s));return}let p=await l.text(),d=l.url||s;return await r.clearNegative(s),await r.setCachedFile(d,p),{dts:p,url:d}})();return t.set(s,c),await c.finally(()=>t.delete(s))}catch{return}}}var Be=class{constructor(e){this.inFlight=new Map,this.scanner=new ke,this.cache=e.cache,this.cdnClient=e.cdnClient,this.versionResolver=e.versionResolver,this.packageService=e.packageService,this.fetchDts=wt(e.cache),this.typescriptProvider=new Pe(this.fetchDts,e.cdnClient),this.definitelyTypedProvider=new Ee(this.fetchDts,e.cdnClient,e.cache),this.virtualFs=new Te}withInFlight(e,t){let n=this.inFlight.get(e);if(n)return n;let s=t().finally(()=>this.inFlight.delete(e));return this.inFlight.set(e,s),s}invalidate(){this.virtualFs.bumpEpoch(),this.inFlight.clear()}capArray(e,t){return e.length<=t?e:e.slice(0,t)}pushFileIfNew(e,t,n){e.some(s=>s.path===t)||e.push({path:t,content:n})}createStubDef(e){let t=this.virtualFs.pathForMain(e);return{pkg:e,mainPath:t,files:[{path:t,content:"export const _shim: any; export default _shim;"}],shims:[{module:e,path:t}]}}async trySubpathWithRootFallback(e,t){let n=new b(e);return n.subpath?this.fetchRootDts(n.name,t):void 0}async fetchViaProviders(e){for(let t of[this.typescriptProvider,this.definitelyTypedProvider])try{let n=await t.resolve(e);if(n?.dts)return n}catch{}}subpathsMatch(e,t){return new b(e).subpath===new b(t).subpath}async fetchRootDts(e,t){let{effectivePackage:n,root:s,pinned:o}=await this.versionResolver.effectivePackage(e,t),i=await this.cache.getCachedDts(n);if(i?.content){let c=i.url??this.cdnClient.file(o?`${s}@${o}`:s,"index.d.ts");return{dts:i.content,url:c,resolvedPkg:n}}let a;try{a=await this.fetchViaProviders(n)}catch{return}if(!(!a?.dts||!a.url)){try{await this.cache.setCachedFile(a.url,a.dts)}catch{}if(this.subpathsMatch(n,a.resolvedPkg||n)){try{await this.cache.setCachedDts(n,a.dts,a.url)}catch{}return a}}}extractPackageRootUrl(e){let t=e.pathname.match(/^(.+@[^/]+\/)/);return t?new URL(t[1],e.origin):new URL("./",e)}toVirtualPath(e,t,n){let s=t.pathname.startsWith(e.pathname)?t.pathname.slice(e.pathname.length):`__deps__/${encodeURIComponent(t.toString()).replace(/%/g,"_")}.d.ts`;return this.virtualFs.pathFor(n,s)}computeEntryPath(e,t){if(!e)return{mainPath:this.virtualFs.pathForMain(t),packageRootUrl:void 0};let n=new URL(e),s=this.extractPackageRootUrl(n);return{mainPath:this.toVirtualPath(s,n,t),packageRootUrl:s}}async expandRelativeRefs(e,t,n,s,o=new Set,i){if(!o.has(t)&&(o.add(t),!(o.size>H.maxRelativeTypeRefs)))try{let a=new URL(t),c=new URL("./",a);i??(i=this.extractPackageRootUrl(a));let l=this.capArray(this.scanner.collectRelativeTypeRefs(e),H.maxRelativeTypeRefs);for(let p of l)await this.tryRelativeRef(p,c,i,n,s,o)}catch{}}async tryRelativeRef(e,t,n,s,o,i){try{let a=new URL(e,t),c=a.pathname+a.search,l=vt(c)?[c]:this.typescriptProvider.declarationCandidatesFor(c);for(let p of l){let h=new URL(p,a).toString();if(i.has(h))return;let m=await this.fetchDts(h);if(m?.dts){let g=this.toVirtualPath(n,new URL(m.url),s);this.pushFileIfNew(o,g,m.dts),await this.expandRelativeRefs(m.dts,m.url,s,o,i,n);return}}}catch{}}isDifferentPackage(e,t){return e===t?!1:new b(e).name!==new b(t).name}ambientlyDeclares(e,t){for(let n of e.matchAll(/declare\s+module\s+['"]([^'"]+)['"]/g)){let s=n[1];if(s===t||s.endsWith("/*")&&t.startsWith(s.slice(0,-1)))return!0}return!1}markFileAmbient(e,t){let n=e.find(s=>s.path===t);n&&(n.ambient=!0)}async prefetchBareDeps(e,t,n,s,o){try{let i=this.capArray(this.scanner.collectBareModuleRefs(e),H.maxBareDeps).filter(l=>this.isDifferentPackage(l,t));if(!i.length)return;let a=new Set([t]),c=Kn(H.prefetchConcurrency);await Promise.all(i.map(l=>c(()=>this.prefetchBareDepsRecursive(l,n,s,H.maxBareDepth,a,o).catch(()=>{}))))}catch{}}async prefetchBareDepsRecursive(e,t,n,s,o,i){if(s<=0||o.has(e))return;o.add(e);let a=await this.fetchRootDts(e,i);if(!a?.dts)return;let c=a.resolvedPkg||e,l=this.virtualFs.pathForMain(c);this.pushFileIfNew(t,l,a.dts),this.ambientlyDeclares(a.dts,e)?this.markFileAmbient(t,l):n.push({module:e,path:l}),a.url&&await this.expandRelativeRefs(a.dts,a.url,c,t);let p=this.capArray(this.scanner.collectBareModuleRefs(a.dts),H.maxBareDeps).filter(d=>!o.has(d));for(let d of p)await this.prefetchBareDepsRecursive(d,t,n,s-1,o,i)}async resolve(e,t,n){let s=await this.packageService.extractImports(e),o=n?[...s,...n]:s;return(await Promise.all(o.map(a=>this.resolveOne(a,t)))).filter(a=>!!a)}async resolveOne(e,t){let{effectivePackage:n}=await this.versionResolver.effectivePackage(e,t);return this.withInFlight(n,async()=>{let s=await this.fetchRootDts(e,t)??await this.trySubpathWithRootFallback(e,t);return s?this.buildDefFromContent(e,s,s.resolvedPkg||e,t):this.createStubDef(e)})}async buildDefFromContent(e,t,n,s){let{dts:o,url:i,resolvedPkg:a}=t,c=new b(a||n),l=c.version?`${c.name}@${c.version}`:c.name,{mainPath:p,packageRootUrl:d}=this.computeEntryPath(i,l),h=[{path:p,content:o}],m=[];i&&await this.expandRelativeRefs(o,i,l,h,void 0,d);let g=h.map(T=>T.content).join(`
256
- `);await this.prefetchBareDeps(g,l,h,m,s);let S=c.format(),j=e===S?[e]:[e,S],B=j.some(T=>this.ambientlyDeclares(o,T));return B?this.markFileAmbient(h,p):m.push(...j.map(T=>({module:T,path:p}))),{pkg:e,mainPath:p,files:h,shims:m,ambient:B}}};function xt(r){return new Be(r)}import*as L from"fs/promises";import*as W from"path";import*as Sr from"os";import*as St from"fs/promises";import*as wr from"crypto";function D(r){return wr.createHash("sha1").update(r).digest("hex")}async function X(r){try{return JSON.parse(await St.readFile(r,"utf8"))}catch{return}}async function Ue(r){try{return await St.readFile(r,"utf8")}catch{return}}var xr=1,q=W.join(Sr.homedir(),".typebulb","cache"),_e=W.join(q,"packages"),Ce=W.join(q,"proxy"),We=W.join(q,"dts"),Yn=W.join(q,"emit"),Rt;function x(){return Rt||(Rt=Xn()),Rt}function Je(r,e){return W.join(Yn,D(r),e)}async function Xn(){await L.mkdir(q,{recursive:!0});let r=W.join(q,"version.json");if((await Qn(r))?.version===xr)return;let t=await L.readdir(q).catch(()=>[]);await Promise.all(t.map(n=>L.rm(W.join(q,n),{recursive:!0,force:!0}))),await L.writeFile(r,JSON.stringify({version:xr})+`
257
- `,"utf8")}async function Qn(r){try{let e=await L.readFile(r,"utf8");return JSON.parse(e)}catch{return}}import*as z from"fs/promises";import*as Rr from"path";async function O(r,e){await z.mkdir(Rr.dirname(r),{recursive:!0});let t=`${r}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;await z.writeFile(t,e,"utf8"),await z.rename(t,r).catch(async n=>{throw await z.rm(t,{force:!0}).catch(()=>{}),n})}var ae=class{constructor(e){this.filePath=e}mem;loadPromise;load(){return this.mem?Promise.resolve(this.mem):(this.loadPromise||(this.loadPromise=X(this.filePath).then(e=>(this.mem=new Map(Object.entries(e??{})),this.mem))),this.loadPromise)}async get(e){return(await this.load()).get(e)}async set(e,t){let n=await this.load();n.set(e,t),await this.persist(n)}async delete(e){let t=await this.load();t.delete(e)&&await this.persist(t)}async persist(e){await O(this.filePath,JSON.stringify(Object.fromEntries(e)))}};var Pt=$.join(_e,"indexes"),Pr=$.join(_e,"pinned"),Zn=$.join(_e,"meta"),es=$.join(_e,"negative.json"),ce=Symbol("missing"),He=class{pinnedMem=new Map;indexMem=new Map;metaMem=new Map;negativeCache=new Y(new ae(es));async getPinnedExact(e,t){let n=`${e}@${t}`,s=this.pinnedMem.get(n);if(s!==void 0)return s===ce?void 0:s;await x();let o=await Ue($.join(Pr,D(n)+".txt"));return this.pinnedMem.set(n,o??ce),o}async setPinnedExact(e,t,n){let s=`${e}@${t}`;this.pinnedMem.set(s,n),await x(),await O($.join(Pr,D(s)+".txt"),n)}async getIndex(e){let t=this.indexMem.get(e);if(t!==void 0)return t===ce?void 0:t;await x();let n=await X($.join(Pt,Ve(e)+".json"));return this.indexMem.set(e,n??ce),n}async setIndex(e,t,n){let s={versions:t,distTags:n,updatedAt:Date.now()};this.indexMem.set(e,s),await x(),await O($.join(Pt,Ve(e)+".json"),JSON.stringify(s))}async invalidateVersionsCache(e){this.indexMem.delete(e),await kr.rm($.join(Pt,Ve(e)+".json"),{force:!0})}async isNegative(e){return await x(),this.negativeCache.isNegative(e)}async recordNegative(e){await x(),await this.negativeCache.recordNegative(e)}async clearNegative(e){await this.negativeCache.clearNegative(e)}async getMeta(e,t){let n=`${e}@${t}`,s=this.metaMem.get(n);if(s!==void 0)return s===ce?void 0:s;await x();let o=await X(Er(e,t));return this.metaMem.set(n,o??ce),o}async setMeta(e,t,n,s,o){let i={dependencies:n,peerDependencies:s,peerDependenciesMeta:o,updatedAt:Date.now()};this.metaMem.set(`${e}@${t}`,i),await x(),await O(Er(e,t),JSON.stringify(i))}};function Er(r,e){return $.join(Zn,Ve(r),encodeURIComponent(e)+".json")}function Ve(r){return r.replace(/\//g,"__")}var Tr={async getJson(r){try{let e=await fetch(r,{redirect:"follow"});return e.ok?await e.json():void 0}catch{return}},async head(r){try{let e=await fetch(r,{method:"HEAD",redirect:"follow"});return{ok:e.ok,url:e.url}}catch{return}}};var ts=new He,{packageService:qe,versionResolver:_r,cdnClient:Cr,peerResolver:da}=ur(ts,Tr);var Or=`
259
+ `;var X=class{constructor(e){this.store=e}async isNegative(e){let t=await Le(()=>this.store.get(e));return!!t&&t.until>Date.now()}async recordNegative(e){let n=((await Le(()=>this.store.get(e)))?.attempts||0)+1;await Le(()=>this.store.set(e,{until:Date.now()+qn(n),attempts:n}))}async clearNegative(e){await Le(()=>this.store.delete(e))}};function qn(r){return Math.min(9e5*Math.pow(2,Math.max(0,r-1)),864e5)}async function Le(r){try{return await r()}catch{return}}import Qn from"p-limit";import{resolve as wr}from"resolve.exports";var ie=class{constructor(e){this.fetchDts=e}fetchDtsText(e){return this.tryUrls([e])}async tryUrls(e){for(let t of e){let n=await this.fetchDts(t);if(n&&(this.looksLikeDts(n.dts)||/\.(d\.ts|d\.mts)(?:[?#].*)?$/i.test(n.url)||/[?&]dts(?:[&#]|$)/i.test(n.url)))return n}}looksLikeDts(e){return/^\s*export\s*\{\s*\}\s*;?\s*$/m.test(e)?!0:/declare\s+(module|namespace|class|interface|function|const|var|let)/.test(e)||/interface\s+\w+/.test(e)||/type\s+\w+\s*=/.test(e)}};var V={maxRelativeTypeRefs:500,maxBareDeps:8,maxBareDepth:3,prefetchConcurrency:4,negativeTtlMs:1e4},xe=["index.d.ts","index.d.mts"],Se=/\.d\.(ts|mts)$/i;function St(r){if(Se.test(r))return!0;try{return new URL(r,"file://").search.includes("dts")}catch{return r.includes("?")&&r.includes("dts")}}function Be(r){return[`${r}.d.ts`,`${r}.d.mts`]}async function Re(r,{retries:e=2,timeoutMs:t=15e3,init:n}={}){for(let s=0;s<=e;s++){let o=new AbortController,i=setTimeout(()=>o.abort(),t);try{let a=await fetch(r,{...n,signal:o.signal});if(zn(a.status)&&s<e)continue;return a}catch{if(s<e)continue;return}finally{clearTimeout(i)}}}function zn(r){return r===408||r===429||r>=500&&r<600}var Pe=class extends ie{constructor(e,t){super(e),this.cdnClient=t}async loadPackageAtVersionedRoot(e,t){let n=this.cdnClient.packageJson(new b({name:e,version:t})),s=await Re(n);if(!s?.ok)return;let o;try{o=await s.json()}catch{return}if(o)return{pkg:o,baseDir:this.cdnClient.baseDir(new b({name:e,version:t??o.version})),version:t}}extractPathFromResult(e){if(typeof e=="string")return e;if(Array.isArray(e))return e.find(t=>typeof t=="string")}async tryUntilSuccess(e,t){for(let n of e){let s=await t(n);if(s)return s}}async resolveFromSelected(e,t){if(e.kind==="types"){let s=this.cdnClient.normalizeRelative(e.path);if(!s||s==="/"||s===".")return this.tryUntilSuccess([...xe],t);if(!Se.test(s)){let o=await this.tryUntilSuccess(this.declarationCandidatesFor(s),t);if(o)return o}return t(s)}let n=this.declarationCandidatesFor(e.path);return this.tryUntilSuccess([...xe,...n],t)}toResolutionResult(e){return{kind:Se.test(e)?"types":"probe",path:e}}resolveExportsPath(e,t){let n=t||".",s=e;try{let o=this.extractPathFromResult(wr(s,n,{conditions:["types"]}));if(o)return o}catch{}try{return this.extractPathFromResult(wr(s,n,{browser:!0,conditions:["import","default","module","browser","node"]}))}catch{return}}async resolve(e){try{let t=b.parse(e),{pkg:n,baseDir:s}=await this.loadPackageAtVersionedRoot(t.name,t.version)||{};if(!n||!s)return;let o={name:t.name,version:t.version},i=new b(o).format(),a=new b({...o,subpath:t.subpath}).format(),c=d=>this.fetchCandidateFrom(s,t.name,d);if(t.subpath){let d=this.cdnClient.ensureLeadingDotSlash(t.subpath),h=this.resolveExportsPath(n,d);if(h){let g=await this.resolveFromSelected(this.toResolutionResult(h),c);if(g)return{...g,resolvedPkg:a}}let m=await this.tryUntilSuccess(this.declarationCandidatesFor(d),c);if(m)return{...m,resolvedPkg:a}}let l=n.types??n.typings;if(l){let d=await this.resolveFromSelected({kind:"types",path:l},c);if(d)return{...d,resolvedPkg:i}}let p=this.resolveExportsPath(n,".");if(p){let d=await this.resolveFromSelected(this.toResolutionResult(p),c);if(d)return{...d,resolvedPkg:i}}return}catch{return}}async fetchCandidateFrom(e,t,n){let s=this.cdnClient.normalizeRelative(n),o=new URL(s,e).toString(),i=await this.fetchDtsText(o);return i?{dts:i.dts,url:i.url,resolvedPkg:t}:void 0}declarationCandidatesFor(e){if(!e||e==="./"||e==="/")return[...xe];let t=e.replace(/\.(mjs|cjs|js|mts|cts|ts)$/i,""),n=Be(t),s=t.endsWith("/")?t:`${t}/`;return n.push(...Be(`${s}index`)),n}};import{gunzipSync as Gn}from"fflate";var ae=class{async fetchAndExtract(e,t){let n=this.getTarballUrl(e,t),s=await Re(n,{timeoutMs:3e4});if(!s?.ok)return new Map;let o=new Uint8Array(await s.arrayBuffer());return this.extractDtsFiles(o)}getTarballUrl(e,t){return`https://registry.npmjs.org/${e.replace("/","%2F")}/-/${e.split("/").pop()}-${t}.tgz`}normalizeTarPath(e){let t=e.replace(/^package\//,""),n=t.indexOf("/");return n>0?t.substring(n+1):t}extractDtsFiles(e){let t=new Map;try{let n=Gn(e),s=new TextDecoder("utf-8"),o=0;for(;o<n.length-512;){let i=n.slice(o,o+512);if(i[0]===0)break;let a=i.slice(0,100),c=a.indexOf(0),l=s.decode(a.slice(0,c>0?c:100)).trim(),p=i.slice(124,136),d=s.decode(p).trim().replace(/\0/g,""),h=parseInt(d,8)||0,m=String.fromCharCode(i[156]);if(o+=512,(m==="0"||m==="\0")&&(l.endsWith(".d.ts")||l.endsWith(".d.mts"))){let g=n.slice(o,o+h);t.set(this.normalizeTarPath(l),s.decode(g))}o+=Math.ceil(h/512)*512}}catch{}return t}},Yn=new ae;var Ee=class extends ie{constructor(e,t,n,s=new ae){super(e),this.cdnClient=t,this.cache=n,this.tarballFetcher=s}typesNameCandidates(e){let t=e.startsWith("@")?e.slice(1).replace("/","__"):e;return t.includes(".")?[t,t.split(".").join("-"),t.split(".").join("")]:[t]}selectTypesVersion(e,t,n){try{if(n){let o=K.majorOf(n),i=e.filter(a=>K.majorOf(a)===o);if(i.length)return i.sort((a,c)=>K.cmp(c,a))[0]}let s=t?.latest;return s&&e.includes(s)?s:e[0]}catch{return e[0]}}async fetchFromVersionedRoot(e,t){let n=new b(e),s=n.version;if(!s)return;let o;try{o=await this.tarballFetcher.fetchAndExtract(n.name,s)}catch{return}if(!o||o.size===0)return;for(let[a,c]of o.entries()){let l=this.cdnClient.file(e,a);try{await this.cache.setCachedFile(l,c)}catch{}}let i=t.subpath?[t.subpath+".d.ts",t.subpath+"/index.d.ts"]:["index.d.ts"];for(let a of i){let c=o.get(a);if(c)return{dts:c,url:this.cdnClient.file(e,a),resolvedPkg:t.subpath?new b(t).format():t.name}}}async resolve(e){let t=b.parse(e);if(t.name)for(let n of this.typesNameCandidates(t.name)){let s=`@types/${n}`,o;try{o=await this.cdnClient.fetchVersionsIndex(s)}catch{continue}if(!o?.versions?.length)continue;let i=this.selectTypesVersion(o.versions,o.distTags,t.version),a=await this.fetchFromVersionedRoot(`${s}@${i}`,t);if(a)return a}}};var ke=class{collectRelativeTypeRefs(e){return this.collectRefs(e).filter(t=>t.startsWith("./")||t.startsWith("../"))}collectBareModuleRefs(e){return this.collectRefs(e).filter(t=>this.isBare(t))}collectRefs(e){let t=new Set,n=(s,o)=>(t.add(s[o]),null);return this.matchAll(e,/(import|export)\s+[^'"\n]*from\s*['"]([^'"\n]+)['"]/,s=>n(s,2)),this.matchAll(e,/export\s*\*\s*from\s*['"]([^'"\n]+)['"]/,s=>n(s,1)),Array.from(t)}matchAll(e,t,n){let s=[];try{let o=new RegExp(t.source,"g"),i;for(;i=o.exec(e);){let a=n(i);a!==null&&s.push(a)}}catch{}return s}isBare(e){return!(e.startsWith("./")||e.startsWith("../")||e.startsWith("file:")||e.startsWith("http://")||e.startsWith("https://"))}};var xr="file:///node_modules",Te=class{constructor(){this.epoch=0,this.listeners=new Set}getEpoch(){return this.epoch}bumpEpoch(){this.epoch+=1;for(let e of this.listeners)try{e(this.epoch)}catch{}return this.epoch}onEpochChange(e){return this.listeners.add(e),()=>{this.listeners.delete(e)}}epochDir(){return`__tsepoch_${this.epoch}`}pathForMain(e){return`${xr}/${this.epochDir()}/${e}/index.d.ts`}pathFor(e,t){let n=Kn(t);return`${xr}/${this.epochDir()}/${e}/${n}`}};function Kn(r){let e=r||"";return e.startsWith("./")?e.slice(2):e.replace(/^\/+/,"")}import{LRUCache as Xn}from"lru-cache";function Rt(r){let e=new Xn({ttl:1e4,max:500}),t=new Map;return async function(s){try{if(await r.isNegative(s))return;let o=e.get(s);if(o&&Date.now()-o<1e4)return;let i=await r.getCachedFile(s);if(i)return{dts:i,url:s};let a=t.get(s);if(a)return a;let c=(async()=>{let l=await fetch(s,{cache:"no-store"});if(!l.ok){l.status===404&&(e.set(s,Date.now()),await r.recordNegative(s));return}let p=await l.text(),d=l.url||s;return await r.clearNegative(s),await r.setCachedFile(d,p),{dts:p,url:d}})();return t.set(s,c),await c.finally(()=>t.delete(s))}catch{return}}}var Ue=class{constructor(e){this.inFlight=new Map,this.scanner=new ke,this.cache=e.cache,this.cdnClient=e.cdnClient,this.versionResolver=e.versionResolver,this.packageService=e.packageService,this.fetchDts=Rt(e.cache),this.typescriptProvider=new Pe(this.fetchDts,e.cdnClient),this.definitelyTypedProvider=new Ee(this.fetchDts,e.cdnClient,e.cache),this.virtualFs=new Te}withInFlight(e,t){let n=this.inFlight.get(e);if(n)return n;let s=t().finally(()=>this.inFlight.delete(e));return this.inFlight.set(e,s),s}invalidate(){this.virtualFs.bumpEpoch(),this.inFlight.clear()}capArray(e,t){return e.length<=t?e:e.slice(0,t)}pushFileIfNew(e,t,n){e.some(s=>s.path===t)||e.push({path:t,content:n})}createStubDef(e){let t=this.virtualFs.pathForMain(e);return{pkg:e,mainPath:t,files:[{path:t,content:"export const _shim: any; export default _shim;"}],shims:[{module:e,path:t}]}}async trySubpathWithRootFallback(e,t){let n=new b(e);return n.subpath?this.fetchRootDts(n.name,t):void 0}async fetchViaProviders(e){for(let t of[this.typescriptProvider,this.definitelyTypedProvider])try{let n=await t.resolve(e);if(n?.dts)return n}catch{}}subpathsMatch(e,t){return new b(e).subpath===new b(t).subpath}async fetchRootDts(e,t){let{effectivePackage:n,root:s,pinned:o}=await this.versionResolver.effectivePackage(e,t),i=await this.cache.getCachedDts(n);if(i?.content){let c=i.url??this.cdnClient.file(o?`${s}@${o}`:s,"index.d.ts");return{dts:i.content,url:c,resolvedPkg:n}}let a;try{a=await this.fetchViaProviders(n)}catch{return}if(!(!a?.dts||!a.url)){try{await this.cache.setCachedFile(a.url,a.dts)}catch{}if(this.subpathsMatch(n,a.resolvedPkg||n)){try{await this.cache.setCachedDts(n,a.dts,a.url)}catch{}return a}}}extractPackageRootUrl(e){let t=e.pathname.match(/^(.+@[^/]+\/)/);return t?new URL(t[1],e.origin):new URL("./",e)}toVirtualPath(e,t,n){let s=t.pathname.startsWith(e.pathname)?t.pathname.slice(e.pathname.length):`__deps__/${encodeURIComponent(t.toString()).replace(/%/g,"_")}.d.ts`;return this.virtualFs.pathFor(n,s)}computeEntryPath(e,t){if(!e)return{mainPath:this.virtualFs.pathForMain(t),packageRootUrl:void 0};let n=new URL(e),s=this.extractPackageRootUrl(n);return{mainPath:this.toVirtualPath(s,n,t),packageRootUrl:s}}async expandRelativeRefs(e,t,n,s,o=new Set,i){if(!o.has(t)&&(o.add(t),!(o.size>V.maxRelativeTypeRefs)))try{let a=new URL(t),c=new URL("./",a);i??(i=this.extractPackageRootUrl(a));let l=this.capArray(this.scanner.collectRelativeTypeRefs(e),V.maxRelativeTypeRefs);for(let p of l)await this.tryRelativeRef(p,c,i,n,s,o)}catch{}}async tryRelativeRef(e,t,n,s,o,i){try{let a=new URL(e,t),c=a.pathname+a.search,l=St(c)?[c]:this.typescriptProvider.declarationCandidatesFor(c);for(let p of l){let h=new URL(p,a).toString();if(i.has(h))return;let m=await this.fetchDts(h);if(m?.dts){let g=this.toVirtualPath(n,new URL(m.url),s);this.pushFileIfNew(o,g,m.dts),await this.expandRelativeRefs(m.dts,m.url,s,o,i,n);return}}}catch{}}isDifferentPackage(e,t){return e===t?!1:new b(e).name!==new b(t).name}ambientlyDeclares(e,t){for(let n of e.matchAll(/declare\s+module\s+['"]([^'"]+)['"]/g)){let s=n[1];if(s===t||s.endsWith("/*")&&t.startsWith(s.slice(0,-1)))return!0}return!1}markFileAmbient(e,t){let n=e.find(s=>s.path===t);n&&(n.ambient=!0)}async prefetchBareDeps(e,t,n,s,o){try{let i=this.capArray(this.scanner.collectBareModuleRefs(e),V.maxBareDeps).filter(l=>this.isDifferentPackage(l,t));if(!i.length)return;let a=new Set([t]),c=Qn(V.prefetchConcurrency);await Promise.all(i.map(l=>c(()=>this.prefetchBareDepsRecursive(l,n,s,V.maxBareDepth,a,o).catch(()=>{}))))}catch{}}async prefetchBareDepsRecursive(e,t,n,s,o,i){if(s<=0||o.has(e))return;o.add(e);let a=await this.fetchRootDts(e,i);if(!a?.dts)return;let c=a.resolvedPkg||e,l=this.virtualFs.pathForMain(c);this.pushFileIfNew(t,l,a.dts),this.ambientlyDeclares(a.dts,e)?this.markFileAmbient(t,l):n.push({module:e,path:l}),a.url&&await this.expandRelativeRefs(a.dts,a.url,c,t);let p=this.capArray(this.scanner.collectBareModuleRefs(a.dts),V.maxBareDeps).filter(d=>!o.has(d));for(let d of p)await this.prefetchBareDepsRecursive(d,t,n,s-1,o,i)}async resolve(e,t,n){let s=await this.packageService.extractImports(e),o=n?[...s,...n]:s;return(await Promise.all(o.map(a=>this.resolveOne(a,t)))).filter(a=>!!a)}async resolveOne(e,t){let{effectivePackage:n}=await this.versionResolver.effectivePackage(e,t);return this.withInFlight(n,async()=>{let s=await this.fetchRootDts(e,t)??await this.trySubpathWithRootFallback(e,t);return s?this.buildDefFromContent(e,s,s.resolvedPkg||e,t):this.createStubDef(e)})}async buildDefFromContent(e,t,n,s){let{dts:o,url:i,resolvedPkg:a}=t,c=new b(a||n),l=c.version?`${c.name}@${c.version}`:c.name,{mainPath:p,packageRootUrl:d}=this.computeEntryPath(i,l),h=[{path:p,content:o}],m=[];i&&await this.expandRelativeRefs(o,i,l,h,void 0,d);let g=h.map(C=>C.content).join(`
260
+ `);await this.prefetchBareDeps(g,l,h,m,s);let S=c.format(),j=e===S?[e]:[e,S],B=j.some(C=>this.ambientlyDeclares(o,C));return B?this.markFileAmbient(h,p):m.push(...j.map(C=>({module:C,path:p}))),{pkg:e,mainPath:p,files:h,shims:m,ambient:B}}};function Pt(r){return new Ue(r)}import*as L from"fs/promises";import*as W from"path";import*as Pr from"os";import*as Et from"fs/promises";import*as Sr from"crypto";function $(r){return Sr.createHash("sha1").update(r).digest("hex")}async function Q(r){try{return JSON.parse(await Et.readFile(r,"utf8"))}catch{return}}async function We(r){try{return await Et.readFile(r,"utf8")}catch{return}}var Rr=1,q=W.join(Pr.homedir(),".typebulb","cache"),_e=W.join(q,"packages"),Ce=W.join(q,"proxy"),Je=W.join(q,"dts"),Zn=W.join(q,"emit"),kt;function x(){return kt||(kt=es()),kt}function He(r,e){return W.join(Zn,$(r),e)}async function es(){await L.mkdir(q,{recursive:!0});let r=W.join(q,"version.json");if((await ts(r))?.version===Rr)return;let t=await L.readdir(q).catch(()=>[]);await Promise.all(t.map(n=>L.rm(W.join(q,n),{recursive:!0,force:!0}))),await L.writeFile(r,JSON.stringify({version:Rr})+`
261
+ `,"utf8")}async function ts(r){try{let e=await L.readFile(r,"utf8");return JSON.parse(e)}catch{return}}import*as z from"fs/promises";import*as Er from"path";async function I(r,e){await z.mkdir(Er.dirname(r),{recursive:!0});let t=`${r}.tmp-${process.pid}-${Date.now()}-${Math.random().toString(36).slice(2,8)}`;await z.writeFile(t,e,"utf8"),await z.rename(t,r).catch(async n=>{throw await z.rm(t,{force:!0}).catch(()=>{}),n})}var ce=class{constructor(e){this.filePath=e}mem;loadPromise;load(){return this.mem?Promise.resolve(this.mem):(this.loadPromise||(this.loadPromise=Q(this.filePath).then(e=>(this.mem=new Map(Object.entries(e??{})),this.mem))),this.loadPromise)}async get(e){return(await this.load()).get(e)}async set(e,t){let n=await this.load();n.set(e,t),await this.persist(n)}async delete(e){let t=await this.load();t.delete(e)&&await this.persist(t)}async persist(e){await I(this.filePath,JSON.stringify(Object.fromEntries(e)))}};var Tt=D.join(_e,"indexes"),kr=D.join(_e,"pinned"),rs=D.join(_e,"meta"),ns=D.join(_e,"negative.json"),le=Symbol("missing"),qe=class{pinnedMem=new Map;indexMem=new Map;metaMem=new Map;negativeCache=new X(new ce(ns));async getPinnedExact(e,t){let n=`${e}@${t}`,s=this.pinnedMem.get(n);if(s!==void 0)return s===le?void 0:s;await x();let o=await We(D.join(kr,$(n)+".txt"));return this.pinnedMem.set(n,o??le),o}async setPinnedExact(e,t,n){let s=`${e}@${t}`;this.pinnedMem.set(s,n),await x(),await I(D.join(kr,$(s)+".txt"),n)}async getIndex(e){let t=this.indexMem.get(e);if(t!==void 0)return t===le?void 0:t;await x();let n=await Q(D.join(Tt,Ve(e)+".json"));return this.indexMem.set(e,n??le),n}async setIndex(e,t,n){let s={versions:t,distTags:n,updatedAt:Date.now()};this.indexMem.set(e,s),await x(),await I(D.join(Tt,Ve(e)+".json"),JSON.stringify(s))}async invalidateVersionsCache(e){this.indexMem.delete(e),await _r.rm(D.join(Tt,Ve(e)+".json"),{force:!0})}async isNegative(e){return await x(),this.negativeCache.isNegative(e)}async recordNegative(e){await x(),await this.negativeCache.recordNegative(e)}async clearNegative(e){await this.negativeCache.clearNegative(e)}async getMeta(e,t){let n=`${e}@${t}`,s=this.metaMem.get(n);if(s!==void 0)return s===le?void 0:s;await x();let o=await Q(Tr(e,t));return this.metaMem.set(n,o??le),o}async setMeta(e,t,n,s,o){let i={dependencies:n,peerDependencies:s,peerDependenciesMeta:o,updatedAt:Date.now()};this.metaMem.set(`${e}@${t}`,i),await x(),await I(Tr(e,t),JSON.stringify(i))}};function Tr(r,e){return D.join(rs,Ve(r),encodeURIComponent(e)+".json")}function Ve(r){return r.replace(/\//g,"__")}var Cr={async getJson(r){try{let e=await fetch(r,{redirect:"follow"});return e.ok?await e.json():void 0}catch{return}},async head(r){try{let e=await fetch(r,{method:"HEAD",redirect:"follow"});return{ok:e.ok,url:e.url}}catch{return}}};var ss=new qe,{packageService:ze,versionResolver:Ir,cdnClient:Or,peerResolver:ma}=fr(ss,Cr);var Ar=`
258
262
  (() => {
259
263
  // Embedded (bulb-in-a-bulb): runs inside a sandboxed iframe with no parent
260
264
  // bridge, so privileged tb.* (AI, fs, server RPC) can't reach a host and would
@@ -464,12 +468,12 @@ declare const tb: {${pr}${fr}${hr}${yr}${Bn}${mr}${gr}
464
468
  };
465
469
  }
466
470
  })();
467
- `;function Ir(r){let{name:e,code:t,css:n,html:s,data:o,insight:i,importMap:a,watch:c,theme:l}=r,p=s.trim()||'<div id="app"></div>',d=m=>m.replace(/<\/script/gi,"<\\/script"),h={imports:ss(a.imports)};return`<!DOCTYPE html>
471
+ `;function $r(r){let{name:e,code:t,css:n,html:s,data:o,insight:i,importMap:a,watch:c,theme:l}=r,p=s.trim()||'<div id="app"></div>',d=m=>m.replace(/<\/script/gi,"<\\/script"),h={imports:as(a.imports)};return`<!DOCTYPE html>
468
472
  <html>
469
473
  <head>
470
474
  <meta charset="utf-8">
471
475
  <meta name="viewport" content="width=device-width, initial-scale=1">
472
- <title>${ns(e)} - typebulb</title>
476
+ <title>${is(e)} - typebulb</title>
473
477
  <script>
474
478
  // Theme engine. Sets html[data-theme] before stylesheets paint (no flash) and
475
479
  // exposes the tb.theme accessor via window.__tbTheme. The override is persisted
@@ -535,16 +539,16 @@ ${i?`<script>window.__TB_INSIGHT__ = ${d(JSON.stringify(i))};</script>`:""}
535
539
  ${c?"<script>window.__TYPEBULB_WATCH__ = true;</script>":""}
536
540
 
537
541
  <script>
538
- ${Or}
542
+ ${Ar}
539
543
  </script>
540
544
 
541
- ${rs}
545
+ ${os}
542
546
 
543
547
  <script type="module">
544
548
  ${d(t)}
545
549
  </script>
546
550
  </body>
547
- </html>`}var rs=`<script>
551
+ </html>`}var os=`<script>
548
552
  (function () {
549
553
  if (window.parent === window) return;
550
554
  var de = document.documentElement;
@@ -615,36 +619,36 @@ ${d(t)}
615
619
  }
616
620
  });
617
621
  })();
618
- </script>`;function ns(r){return r.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function ss(r){let e={};for(let[t,n]of Object.entries(r))e[t]=n.startsWith("https://")?"/proxy/"+n:n;return e}import*as Ge from"fs/promises";import*as ze from"path";import{existsSync as Ar,readFileSync as os}from"fs";import{execFile as is}from"child_process";import{promisify as as}from"util";import{satisfies as cs}from"semver";var ls=as(is);async function Ke(r,e,t){let s=r.map(i=>ds(i,t)).filter(i=>!us(i,e));if(s.length===0)return;await Ge.mkdir(e,{recursive:!0});let o=ze.join(e,"package.json");Ar(o)||await Ge.writeFile(o,JSON.stringify({name:"typebulb-server",private:!0})),console.log(` Installing: ${s.join(", ")}`),await ls("npm",["install","--no-audit","--no-fund",...s],{cwd:e,shell:!0})}function ds(r,e){if(!e||$r(r))return r;let t=e[r];return t?`${r}@${t}`:r}function us(r,e){let t=ze.join(e,"node_modules",Dr(r));if(!Ar(t))return!1;let n=$r(r);if(!n)return!0;try{let s=JSON.parse(os(ze.join(t,"package.json"),"utf-8")).version;return cs(s,n)}catch{return!1}}function Ye(r){let e=new Set,t=/\bimport\s+(?:[\s\S]*?\s+from\s+)?['"]([^./][^'"]*)['"]/g,n;for(;n=t.exec(r);){let s=n[1];if(s.includes(":"))continue;let o=s.startsWith("@")?s.split("/").slice(0,2).join("/"):s.split("/")[0];e.add(o)}return[...e]}function Dr(r){if(r.startsWith("@")){let t=r.indexOf("/");if(t<0)return r;let n=r.indexOf("@",t+1);return n<0?r:r.slice(0,n)}let e=r.indexOf("@");return e<0?r:r.slice(0,e)}function $r(r){let e=Dr(r);return r.length>e.length?r.slice(e.length+1):""}async function Mr(r){let t=(await G.readdir(r)).find(n=>n.endsWith(".bulb.md"));return t?le.join(r,t):null}async function de(r){let e=await G.readFile(r,"utf-8"),t=nr(e);if(!t)throw new Error("Invalid .bulb.md file format");let n=sr(t);return{bulb:n,config:ir(n.config)}}async function Et(r,e,t,n){let s=ut(r,{serverOnly:!0});if(s.error)throw new Error(`Server compilation error: ${s.error}`);let o=s.code;t&&(o=await er(o,t));let i=le.join(e,".typebulb");await G.mkdir(i,{recursive:!0});let a=Ye(o);a.length>0&&await Ke(a,i,n);let c=le.join(i,"server.mjs");return await G.writeFile(c,o,"utf-8"),await import(`${ps(c).href}?t=${Date.now()}`)}async function kt(r,e,t,n){let{bulb:s,config:o}=await de(r),i=or(s.data),a=ut(s.code,{jsxImportSource:o.jsxImportSource});a.error&&console.error("Compilation error:",a.error);let{importMap:c}=await qe.buildImportMap(a.code,o.dependencies??{},n?new Set([n.name]):void 0);n&&(c.imports[n.name]=n.entryUrl);let l=Ir({name:s.name,code:a.code,css:s.css,html:s.html,data:i,insight:s.insight,importMap:c,watch:e}),p=le.dirname(r),d=null;return s.server&&t&&(d=await Et(s.server,p,n,o.dependencies)),{html:l,bulb:s,serverExports:d}}function Tt(r){if(r.server.trim())return"server-side code (server.ts)";let e=r.code;if(/\btb\s*\.\s*fs\b/.test(e)||e.includes("/__fs"))return"the filesystem";if(/\btb\s*\.\s*ai\b/.test(e)||e.includes("/__ai"))return"AI (your API keys)";if(/\btb\s*\.\s*server\s*\.\s*(?!log\b)\w/.test(e)||e.includes("/__api"))return"server-side code (server.ts)"}import{Hono as xs}from"hono";import{serve as Ss}from"@hono/node-server";import{streamSSE as Rs}from"hono/streaming";import*as ee from"fs/promises";import*as F from"path";var I=class extends Error{constructor(e,t="unknown",n=!1){super(e),this.code=t,this.retryable=n}},M=class{getPath(e,t){return this.path}parseStreamChunk(e){return this.checkAndThrowError(e),this.parseProviderStreamChunk(e)}isReasoningEnabled(e){return e?.reasoning!==void 0&&e.reasoning>0}extractSystemMessages(e,t=`
622
+ </script>`;function is(r){return r.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;").replace(/"/g,"&quot;")}function as(r){let e={};for(let[t,n]of Object.entries(r))e[t]=n.startsWith("https://")?"/proxy/"+n:n;return e}import*as Ye from"fs/promises";import*as Ge from"path";import{existsSync as Dr,readFileSync as cs}from"fs";import{execFile as ls}from"child_process";import{promisify as ds}from"util";import{satisfies as us}from"semver";var ps=ds(ls);async function Ke(r,e,t){let s=r.map(i=>fs(i,t)).filter(i=>!hs(i,e));if(s.length===0)return;await Ye.mkdir(e,{recursive:!0});let o=Ge.join(e,"package.json");Dr(o)||await Ye.writeFile(o,JSON.stringify({name:"typebulb-server",private:!0})),console.log(` Installing: ${s.join(", ")}`),await ps("npm",["install","--no-audit","--no-fund",...s],{cwd:e,shell:!0})}function fs(r,e){if(!e||Fr(r))return r;let t=e[r];return t?`${r}@${t}`:r}function hs(r,e){let t=Ge.join(e,"node_modules",Mr(r));if(!Dr(t))return!1;let n=Fr(r);if(!n)return!0;try{let s=JSON.parse(cs(Ge.join(t,"package.json"),"utf-8")).version;return us(s,n)}catch{return!1}}function Xe(r){let e=new Set,t=/\bimport\s+(?:[\s\S]*?\s+from\s+)?['"]([^./][^'"]*)['"]/g,n;for(;n=t.exec(r);){let s=n[1];if(s.includes(":"))continue;let o=s.startsWith("@")?s.split("/").slice(0,2).join("/"):s.split("/")[0];e.add(o)}return[...e]}function Mr(r){if(r.startsWith("@")){let t=r.indexOf("/");if(t<0)return r;let n=r.indexOf("@",t+1);return n<0?r:r.slice(0,n)}let e=r.indexOf("@");return e<0?r:r.slice(0,e)}function Fr(r){let e=Mr(r);return r.length>e.length?r.slice(e.length+1):""}async function jr(r){let t=(await G.readdir(r)).find(n=>n.endsWith(".bulb.md"));return t?Qe.join(r,t):null}async function de(r){let e=await G.readFile(r,"utf-8"),t=or(e);if(!t)throw new Error("Invalid .bulb.md file format");let n=ir(t);return{bulb:n,config:cr(n.config)}}async function _t(r,e,t,n){let s=ht(r,{serverOnly:!0});if(s.error)throw new Error(`Server compilation error: ${s.error}`);let o=s.code;t&&(o=await rr(o,t));let i=Qe.join(e,".typebulb");await G.mkdir(i,{recursive:!0});let a=Xe(o);a.length>0&&await Ke(a,i,n);let c=Qe.join(i,"server.mjs");return await G.writeFile(c,o,"utf-8"),await import(`${ms(c).href}?t=${Date.now()}`)}async function Ct(r,e,t,n,s){let{bulb:o,config:i}=await de(r),a=ar(o.data),c=ht(o.code,{jsxImportSource:i.jsxImportSource});c.error&&console.error("Compilation error:",c.error);let{importMap:l}=await ze.buildImportMap(c.code,i.dependencies??{},n?new Set([n.name]):void 0);n&&(l.imports[n.name]=n.entryUrl);let p=$r({name:o.name,code:c.code,css:o.css,html:o.html,data:a,insight:o.insight,importMap:l,watch:e}),d=null;return o.server&&t&&(d=await _t(o.server,s,n,i.dependencies)),{html:p,bulb:o,serverExports:d}}function It(r){if(r.server.trim())return"server-side code (server.ts)";let e=r.code;if(/\btb\s*\.\s*fs\b/.test(e)||e.includes("/__fs"))return"the filesystem";if(/\btb\s*\.\s*ai\b/.test(e)||e.includes("/__ai"))return"AI (your API keys)";if(/\btb\s*\.\s*server\s*\.\s*(?!log\b)\w/.test(e)||e.includes("/__api"))return"server-side code (server.ts)"}import{Hono as Ps}from"hono";import{serve as Es}from"@hono/node-server";import{streamSSE as ks}from"hono/streaming";import*as te from"fs/promises";import*as F from"path";var O=class extends Error{constructor(e,t="unknown",n=!1){super(e),this.code=t,this.retryable=n}},M=class{getPath(e,t){return this.path}parseStreamChunk(e){return this.checkAndThrowError(e),this.parseProviderStreamChunk(e)}isReasoningEnabled(e){return e?.reasoning!==void 0&&e.reasoning>0}extractSystemMessages(e,t=`
619
623
 
620
- `){let n=e.filter(s=>s.role==="system").map(s=>s.content);return{system:n.length?n.join(t):void 0,conversationMessages:e.filter(s=>s.role!=="system")}}parseJsonError(e,t,n=!1){if(!e)return{message:`HTTP ${t}`};try{let s=JSON.parse(e);if(s.error&&typeof s.error=="object"){let o={message:s.error.message||`HTTP ${t}`,type:s.error.type};return n&&(o.code=s.error.code),o}return s.message?{message:s.message}:{message:e}}catch{return{message:e}}}checkAndThrowError(e){if("type"in e&&e.type==="error"&&"message"in e){let t=e.message,n=e.code||"unknown",s=!!e.retryable;throw new I(t,n,s)}if("error"in e&&e.error){let t=this.extractErrorMessage(e.error);throw new I(t)}}extractErrorMessage(e){if(typeof e=="string")try{let t=JSON.parse(e);return t.error?.message||t.message||e}catch{return e}return typeof e=="object"&&e!==null?e.message||JSON.stringify(e):`${this.providerName} returned an error`}};var Oe=class extends M{constructor(){super(...arguments),this.providerName="Anthropic",this.defaultBaseUrl="https://api.anthropic.com",this.path="/v1/messages"}buildHeaders(e){return{"x-api-key":e,"anthropic-version":"2023-06-01","Content-Type":"application/json",Accept:"application/json"}}buildPayload(e,t,n,s){let{system:o,conversationMessages:i}=this.extractSystemMessages(e),a={model:t,max_tokens:this.getMaxTokens(t),messages:this.withLastMessageCached(i),stream:s};if(n?.webSearch!==!1&&(a.tools=[{type:"web_search_20250305",name:"web_search"}]),o&&(a.system=[{type:"text",text:o,cache_control:{type:"ephemeral"}}]),this.isReasoningEnabled(n)){let c=n.reasoning;if(this.isModernModel(t)){let l={0:"low",1:"low",2:"medium",3:"high"};a.thinking={type:"adaptive"},a.output_config={effort:l[c]}}else{let l={0:0,1:2048,2:4096,3:8192};a.thinking={type:"enabled",budget_tokens:l[c]}}}return a}parseError(e,t){return this.parseJsonError(e,t)}parseNonStreamingResponse(e){if(!this.isAnthropicResponse(e))return{text:""};let t=e.content.filter(s=>s.type==="text").map(s=>s.text).join(""),n=e.content.filter(s=>s.type==="thinking").map(s=>s.thinking).join(`
624
+ `){let n=e.filter(s=>s.role==="system").map(s=>s.content);return{system:n.length?n.join(t):void 0,conversationMessages:e.filter(s=>s.role!=="system")}}parseJsonError(e,t,n=!1){if(!e)return{message:`HTTP ${t}`};try{let s=JSON.parse(e);if(s.error&&typeof s.error=="object"){let o={message:s.error.message||`HTTP ${t}`,type:s.error.type};return n&&(o.code=s.error.code),o}return s.message?{message:s.message}:{message:e}}catch{return{message:e}}}checkAndThrowError(e){if("type"in e&&e.type==="error"&&"message"in e){let t=e.message,n=e.code||"unknown",s=!!e.retryable;throw new O(t,n,s)}if("error"in e&&e.error){let t=this.extractErrorMessage(e.error);throw new O(t)}}extractErrorMessage(e){if(typeof e=="string")try{let t=JSON.parse(e);return t.error?.message||t.message||e}catch{return e}return typeof e=="object"&&e!==null?e.message||JSON.stringify(e):`${this.providerName} returned an error`}};var Ie=class extends M{constructor(){super(...arguments),this.providerName="Anthropic",this.defaultBaseUrl="https://api.anthropic.com",this.path="/v1/messages"}buildHeaders(e){return{"x-api-key":e,"anthropic-version":"2023-06-01","Content-Type":"application/json",Accept:"application/json"}}buildPayload(e,t,n,s){let{system:o,conversationMessages:i}=this.extractSystemMessages(e),a={model:t,max_tokens:this.getMaxTokens(t),messages:this.withLastMessageCached(i),stream:s};if(n?.webSearch!==!1&&(a.tools=[{type:"web_search_20250305",name:"web_search"}]),o&&(a.system=[{type:"text",text:o,cache_control:{type:"ephemeral"}}]),this.isReasoningEnabled(n)){let c=n.reasoning;if(this.isModernModel(t)){let l={0:"low",1:"low",2:"medium",3:"high"};a.thinking={type:"adaptive"},a.output_config={effort:l[c]}}else{let l={0:0,1:2048,2:4096,3:8192};a.thinking={type:"enabled",budget_tokens:l[c]}}}return a}parseError(e,t){return this.parseJsonError(e,t)}parseNonStreamingResponse(e){if(!this.isAnthropicResponse(e))return{text:""};let t=e.content.filter(s=>s.type==="text").map(s=>s.text).join(""),n=e.content.filter(s=>s.type==="thinking").map(s=>s.thinking).join(`
621
625
 
622
- `);return{text:t,reasoning:n||void 0}}parseProviderStreamChunk(e){if(!("type"in e))return null;switch(e.type){case"content_block_delta":return e.delta.type==="text_delta"?{text:e.delta.text||""}:e.delta.type==="thinking_delta"?{reasoning:e.delta.thinking||""}:null;case"message_start":case"content_block_start":case"content_block_stop":case"message_delta":case"message_stop":case"ping":return null;default:return null}}withLastMessageCached(e){let t=e.length-1;return e.map((n,s)=>s===t?{role:n.role,content:[{type:"text",text:n.content,cache_control:{type:"ephemeral"}}]}:n)}isAnthropicResponse(e){return"content"in e&&Array.isArray(e.content)&&"type"in e&&e.type==="message"}isModernModel(e){let t=e.match(/^claude-\w+-(\d+)-(\d+)/);return!!t&&(+t[1]>4||+t[1]==4&&+t[2]>=6)}getMaxTokens(e){return this.isModernModel(e)?64e3:32e3}};var Ie=class extends M{constructor(){super(...arguments),this.providerName="OpenAI",this.defaultBaseUrl="https://api.openai.com",this.path="/v1/responses",this.effortMap={0:"minimal",1:"low",2:"medium",3:"high"}}buildHeaders(e){return{Authorization:`Bearer ${e}`,"Content-Type":"application/json",Accept:"application/json"}}buildPayload(e,t,n,s){let o=this.convertMessagesToInput(e),i={model:t,input:o,stream:s};return n?.webSearch!==!1&&(i.tools=[{type:"web_search"}]),this.isReasoningEnabled(n)&&(i.reasoning={effort:this.effortMap[n.reasoning],summary:"auto"}),i}parseError(e,t){return this.parseJsonError(e,t,!0)}parseNonStreamingResponse(e){if(!this.isResponsesApiResponse(e))return{text:""};let t=e.output_text||"",n;if(e.output&&Array.isArray(e.output))for(let s of e.output)s.type==="reasoning"&&s.summary&&(n=s.summary.map(o=>o.text).join(`
623
- `)),!t&&s.type==="message"&&s.content&&(t=s.content.filter(o=>o.type==="output_text").map(o=>o.text).join(""));return{text:t,reasoning:n}}parseProviderStreamChunk(e){if(!this.isResponsesApiEvent(e))return null;switch(e.type){case"error":return null;case"response.failed":{let s=e.response?.error,o=s?.message||"Response failed",i=s?.code==="insufficient_quota"||s?.code==="rate_limit_exceeded";throw new I(o,i?"rate_limit":"unknown",i)}case"response.output_text.delta":return{text:e.delta};case"response.reasoning_summary_text.delta":return{reasoning:e.delta};case"response.created":case"response.in_progress":case"response.output_item.added":case"response.output_item.done":case"response.content_part.added":case"response.content_part.done":case"response.output_text.done":case"response.output_text.annotation.added":case"response.reasoning_summary_text.done":case"response.completed":case"response.web_search_call.in_progress":case"response.web_search_call.searching":case"response.web_search_call.completed":return null;default:return null}}convertMessagesToInput(e){return e.map(t=>t.role==="system"?`System: ${t.content}`:t.role==="user"?`User: ${t.content}`:t.role==="assistant"?`Assistant: ${t.content}`:t.content).join(`
626
+ `);return{text:t,reasoning:n||void 0}}parseProviderStreamChunk(e){if(!("type"in e))return null;switch(e.type){case"content_block_delta":return e.delta.type==="text_delta"?{text:e.delta.text||""}:e.delta.type==="thinking_delta"?{reasoning:e.delta.thinking||""}:null;case"message_start":case"content_block_start":case"content_block_stop":case"message_delta":case"message_stop":case"ping":return null;default:return null}}withLastMessageCached(e){let t=e.length-1;return e.map((n,s)=>s===t?{role:n.role,content:[{type:"text",text:n.content,cache_control:{type:"ephemeral"}}]}:n)}isAnthropicResponse(e){return"content"in e&&Array.isArray(e.content)&&"type"in e&&e.type==="message"}isModernModel(e){let t=e.match(/^claude-\w+-(\d+)-(\d+)/);return!!t&&(+t[1]>4||+t[1]==4&&+t[2]>=6)}getMaxTokens(e){return this.isModernModel(e)?64e3:32e3}};var Oe=class extends M{constructor(){super(...arguments),this.providerName="OpenAI",this.defaultBaseUrl="https://api.openai.com",this.path="/v1/responses",this.effortMap={0:"minimal",1:"low",2:"medium",3:"high"}}buildHeaders(e){return{Authorization:`Bearer ${e}`,"Content-Type":"application/json",Accept:"application/json"}}buildPayload(e,t,n,s){let o=this.convertMessagesToInput(e),i={model:t,input:o,stream:s};return n?.webSearch!==!1&&(i.tools=[{type:"web_search"}]),this.isReasoningEnabled(n)&&(i.reasoning={effort:this.effortMap[n.reasoning],summary:"auto"}),i}parseError(e,t){return this.parseJsonError(e,t,!0)}parseNonStreamingResponse(e){if(!this.isResponsesApiResponse(e))return{text:""};let t=e.output_text||"",n;if(e.output&&Array.isArray(e.output))for(let s of e.output)s.type==="reasoning"&&s.summary&&(n=s.summary.map(o=>o.text).join(`
627
+ `)),!t&&s.type==="message"&&s.content&&(t=s.content.filter(o=>o.type==="output_text").map(o=>o.text).join(""));return{text:t,reasoning:n}}parseProviderStreamChunk(e){if(!this.isResponsesApiEvent(e))return null;switch(e.type){case"error":return null;case"response.failed":{let s=e.response?.error,o=s?.message||"Response failed",i=s?.code==="insufficient_quota"||s?.code==="rate_limit_exceeded";throw new O(o,i?"rate_limit":"unknown",i)}case"response.output_text.delta":return{text:e.delta};case"response.reasoning_summary_text.delta":return{reasoning:e.delta};case"response.created":case"response.in_progress":case"response.output_item.added":case"response.output_item.done":case"response.content_part.added":case"response.content_part.done":case"response.output_text.done":case"response.output_text.annotation.added":case"response.reasoning_summary_text.done":case"response.completed":case"response.web_search_call.in_progress":case"response.web_search_call.searching":case"response.web_search_call.completed":return null;default:return null}}convertMessagesToInput(e){return e.map(t=>t.role==="system"?`System: ${t.content}`:t.role==="user"?`User: ${t.content}`:t.role==="assistant"?`Assistant: ${t.content}`:t.content).join(`
624
628
 
625
629
  `)}isResponsesApiEvent(e){return"type"in e&&typeof e.type=="string"}isResponsesApiResponse(e){return"object"in e&&e.object==="response"}};var Ae=class extends M{constructor(){super(...arguments),this.providerName="Gemini",this.defaultBaseUrl="https://generativelanguage.googleapis.com",this.path="/v1beta/models"}getPath(e,t){return`/v1beta/models/${e}:${t?"streamGenerateContent":"generateContent"}${t?"?alt=sse":""}`}buildHeaders(e){return{"Content-Type":"application/json","x-goog-api-key":e}}buildPayload(e,t,n,s){let{system:o,conversationMessages:i}=this.extractSystemMessages(e,`
626
630
  `),c={contents:i.map(l=>({role:l.role==="assistant"?"model":"user",parts:[{text:l.content}]}))};return n?.webSearch!==!1&&(c.tools=[{google_search:{}}]),o&&(c.systemInstruction={role:"system",parts:[{text:o}]}),this.isReasoningEnabled(n)&&(c.generationConfig={temperature:.7+n.reasoning*.1}),c}parseError(e,t){if(!e)return{message:`HTTP ${t}`};try{let n=JSON.parse(e),s=Array.isArray(n)?n[0]?.error:n?.error;return s&&typeof s=="object"?{message:(s.message||`HTTP ${t}`).split(`
627
- `)[0],type:s.status,code:s.code?.toString()}:n.message?{message:n.message}:{message:e}}catch{return{message:e}}}parseNonStreamingResponse(e){if(this.checkGeminiError(e),!this.isGeminiResponse(e))return{text:"",status:"failed",error:"Invalid response format"};let t=this.extractText(e)||"",n=e.candidates?.[0]?.finishReason,s="complete";return n==="MAX_TOKENS"?s="interrupted":(n==="SAFETY"||n==="RECITATION")&&(s="failed"),{text:t,status:s}}parseProviderStreamChunk(e){if(this.checkGeminiError(e),!this.isGeminiResponse(e))return null;let t=this.extractText(e);return t?{text:t}:null}isGeminiResponse(e){return typeof e=="object"&&e!==null&&"candidates"in e&&Array.isArray(e.candidates)}extractText(e){let t=e.candidates?.[0];if(t?.content?.parts)return t.content.parts.map(n=>n.text).filter(Boolean).join("")}checkGeminiError(e){if(typeof e=="object"&&e!==null&&"error"in e){let t=e.error,n;if(typeof t=="string")n=t;else if(typeof t=="object"&&t!==null){let s=t;n=s.message||s.status||"Gemini returned an error"}else n="Gemini returned an error";throw new I(n)}if(this.isGeminiResponse(e)&&e.promptFeedback?.blockReason){let t=e.promptFeedback.blockReason;throw new I(`Prompt blocked: ${t}`)}}};var De=class extends M{constructor(){super(...arguments),this.providerName="OpenRouter",this.defaultBaseUrl="https://openrouter.ai/api",this.path="/api/v1/chat/completions",this.effortMap={0:"low",1:"low",2:"medium",3:"high"}}buildHeaders(e,t){let n={Authorization:`Bearer ${e}`,"x-api-key":e,"Content-Type":"application/json",Accept:"application/json","X-Title":"Typebulb"};return t&&(n["HTTP-Referer"]=t,n.Referer=t,n.Origin=t),n}buildPayload(e,t,n,s){let o={model:t,messages:e,stream:s};return n?.webSearch===!0&&(o.plugins=[{id:"web"}]),this.isReasoningEnabled(n)&&(o.reasoning={effort:this.effortMap[n.reasoning]}),o}parseError(e,t){return this.parseJsonError(e,t,!0)}parseNonStreamingResponse(e){if(!this.hasChoices(e))return{text:""};let t=e.choices[0],n=t?.message?.content??t?.text??"",s=t?.message?.reasoning??e.reasoning;return{text:n,reasoning:s}}parseProviderStreamChunk(e){if(!this.hasChoices(e))return null;let t=e.choices[0];if(!t)return null;let n=t.delta||t.message;if(!n)return null;let s=n.content||void 0,o=n.reasoning||void 0;return!s&&!o?null:{text:s,reasoning:o}}hasChoices(e){return typeof e=="object"&&e!==null&&"choices"in e&&Array.isArray(e.choices)}};var fs=new Map([["openai",new Ie],["openrouter",new De],["anthropic",new Oe],["gemini",new Ae]]);function Q(r){let e=fs.get(r);if(!e)throw new Error(`Unsupported protocol: ${r}`);return e}async function _t(r,e){let t=Q(r.protocol),n=t.getPath(e.model,e.stream),s=new URL(n,r.baseUrl).toString(),o=t.buildHeaders(r.apiKey,e.origin),i=t.buildPayload(e.messages,e.model,{reasoning:e.reasoning,webSearch:e.webSearch},e.stream);return e.modifyPayload?.(i),fetch(s,{method:"POST",headers:o,body:JSON.stringify(i),signal:e.signal})}async function Ct(r,e){let t=Q(e),n=await r.text().catch(()=>""),{message:s}=t.parseError(n,r.status),o=r.status,i="unknown";return o===429?i="rate_limit":o===413&&(i="context_exceeded"),{code:i,message:s,retryable:o===429}}function Fr(r){let e=r.indexOf(`\r
631
+ `)[0],type:s.status,code:s.code?.toString()}:n.message?{message:n.message}:{message:e}}catch{return{message:e}}}parseNonStreamingResponse(e){if(this.checkGeminiError(e),!this.isGeminiResponse(e))return{text:"",status:"failed",error:"Invalid response format"};let t=this.extractText(e)||"",n=e.candidates?.[0]?.finishReason,s="complete";return n==="MAX_TOKENS"?s="interrupted":(n==="SAFETY"||n==="RECITATION")&&(s="failed"),{text:t,status:s}}parseProviderStreamChunk(e){if(this.checkGeminiError(e),!this.isGeminiResponse(e))return null;let t=this.extractText(e);return t?{text:t}:null}isGeminiResponse(e){return typeof e=="object"&&e!==null&&"candidates"in e&&Array.isArray(e.candidates)}extractText(e){let t=e.candidates?.[0];if(t?.content?.parts)return t.content.parts.map(n=>n.text).filter(Boolean).join("")}checkGeminiError(e){if(typeof e=="object"&&e!==null&&"error"in e){let t=e.error,n;if(typeof t=="string")n=t;else if(typeof t=="object"&&t!==null){let s=t;n=s.message||s.status||"Gemini returned an error"}else n="Gemini returned an error";throw new O(n)}if(this.isGeminiResponse(e)&&e.promptFeedback?.blockReason){let t=e.promptFeedback.blockReason;throw new O(`Prompt blocked: ${t}`)}}};var $e=class extends M{constructor(){super(...arguments),this.providerName="OpenRouter",this.defaultBaseUrl="https://openrouter.ai/api",this.path="/api/v1/chat/completions",this.effortMap={0:"low",1:"low",2:"medium",3:"high"}}buildHeaders(e,t){let n={Authorization:`Bearer ${e}`,"x-api-key":e,"Content-Type":"application/json",Accept:"application/json","X-Title":"Typebulb"};return t&&(n["HTTP-Referer"]=t,n.Referer=t,n.Origin=t),n}buildPayload(e,t,n,s){let o={model:t,messages:e,stream:s};return n?.webSearch===!0&&(o.plugins=[{id:"web"}]),this.isReasoningEnabled(n)&&(o.reasoning={effort:this.effortMap[n.reasoning]}),o}parseError(e,t){return this.parseJsonError(e,t,!0)}parseNonStreamingResponse(e){if(!this.hasChoices(e))return{text:""};let t=e.choices[0],n=t?.message?.content??t?.text??"",s=t?.message?.reasoning??e.reasoning;return{text:n,reasoning:s}}parseProviderStreamChunk(e){if(!this.hasChoices(e))return null;let t=e.choices[0];if(!t)return null;let n=t.delta||t.message;if(!n)return null;let s=n.content||void 0,o=n.reasoning||void 0;return!s&&!o?null:{text:s,reasoning:o}}hasChoices(e){return typeof e=="object"&&e!==null&&"choices"in e&&Array.isArray(e.choices)}};var gs=new Map([["openai",new Oe],["openrouter",new $e],["anthropic",new Ie],["gemini",new Ae]]);function Z(r){let e=gs.get(r);if(!e)throw new Error(`Unsupported protocol: ${r}`);return e}async function Ot(r,e){let t=Z(r.protocol),n=t.getPath(e.model,e.stream),s=new URL(n,r.baseUrl).toString(),o=t.buildHeaders(r.apiKey,e.origin),i=t.buildPayload(e.messages,e.model,{reasoning:e.reasoning,webSearch:e.webSearch},e.stream);return e.modifyPayload?.(i),fetch(s,{method:"POST",headers:o,body:JSON.stringify(i),signal:e.signal})}async function At(r,e){let t=Z(e),n=await r.text().catch(()=>""),{message:s}=t.parseError(n,r.status),o=r.status,i="unknown";return o===429?i="rate_limit":o===413&&(i="context_exceeded"),{code:i,message:s,retryable:o===429}}function Nr(r){let e=r.indexOf(`\r
628
632
  \r
629
633
  `),t=r.indexOf(`
630
634
 
631
- `);return e!==-1&&(t===-1||e<t)?{pos:e,len:4}:t!==-1?{pos:t,len:2}:{pos:-1,len:0}}function Ot(r){let t=r.split(/\r?\n/).filter(s=>s.startsWith("data:"));if(!t.length)return null;let n=t.map(s=>s.replace(/^data:\s?/,"")).join(`
632
- `).trim();if(!n)return null;if(n==="[DONE]")return"done";try{return JSON.parse(n)}catch{return null}}async function jr(r,e,t){let n=new TextDecoder,s="",o=!1,i=t?new Promise((a,c)=>{t.aborted&&c(new Error("Aborted")),t.addEventListener("abort",()=>c(new Error("Aborted")),{once:!0})}):null;for(;;){let a=i?await Promise.race([r.read(),i]):await r.read(),{done:c,value:l}=a;if(c){if(s.trim()){let h=Ot(s);h!==null&&h!=="done"&&e(h)}break}o=!0,s+=n.decode(l,{stream:!0});let{pos:p,len:d}=Fr(s);for(;p!==-1;){let h=s.slice(0,p);s=s.slice(p+d);let m=Ot(h);if(m==="done"){s="";break}m!==null&&e(m),{pos:p,len:d}=Fr(s)}}return{receivedAnyData:o}}async function It(r,e){let t=e??(r.headers.get("X-Provider-Protocol")||"openai"),n=Q(t);if(!r.body)throw new Error("Response body is missing");let s=r.body.getReader(),o="";return await jr(s,i=>{let a=n.parseStreamChunk(i);a?.text&&(o+=a.text)}),o}import*as At from"fs/promises";import*as $e from"path";var Xe=class{async get(e){let t=D(e),n=$e.join(Ce,t+".bin"),s=$e.join(Ce,t+".json");try{let[o,i]=await Promise.all([At.readFile(n),At.readFile(s,"utf8")]),a=JSON.parse(i);return{body:o,contentType:a.contentType,cacheControl:a.cacheControl}}catch{return}}async set(e,t){await x();let n=D(e),s=$e.join(Ce,n+".bin"),o=$e.join(Ce,n+".json"),i={url:e,contentType:t.contentType,cacheControl:t.cacheControl};await Promise.all([O(s,t.body),O(o,JSON.stringify(i))])}};import{readdir as gs,readFile as Br,writeFile as Ur,unlink as Me,mkdir as ys}from"fs/promises";import{mkdirSync as bs,writeFileSync as Nr,appendFileSync as vs,readFileSync as Wr}from"fs";import*as pe from"path";import{join as Dt,resolve as hs}from"path";import{homedir as ms}from"os";function Qe(){let r=process.env.TYPEBULB_SERVERS_DIR;return r?Dt(r,".."):Dt(ms(),".typebulb")}function Z(){return process.env.TYPEBULB_SERVERS_DIR||Dt(Qe(),"servers")}function ue(r){let e=hs(r);return(process.platform==="win32"?e.toLowerCase():e).replace(/\\/g,"/")}var Lr=1e6;function $t(r){return pe.join(Z(),`${r}.json`)}function Ze(r){return pe.join(Z(),`${r}.log`)}function Jr(r){let e=Ze(r);try{bs(Z(),{recursive:!0}),Nr(e,"")}catch{return()=>{}}let t=process.stdout.write.bind(process.stdout),n=process.stderr.write.bind(process.stderr),s=0,o=!1,i=a=>{if(!o)try{let c=typeof a=="string"?Buffer.from(a,"utf8"):Buffer.from(a);if(vs(e,c),s+=c.length,s>2*Lr){let l=Wr(e),p=l.subarray(Math.max(0,l.length-Lr));Nr(e,p),s=p.length}}catch{o=!0}};return process.stdout.write=((a,...c)=>(i(a),t(a,...c))),process.stderr.write=((a,...c)=>(i(a),n(a,...c))),()=>{process.stdout.write=t,process.stderr.write=n}}function Mt(r,e=0){try{let t=Wr(Ze(r)),n=e>=0&&e<=t.length?e:0;return{text:t.subarray(n).toString("utf8"),offset:t.length}}catch{return{text:"",offset:0}}}function ws(r){try{return process.kill(r,0),!0}catch(e){return e.code==="EPERM"}}async function Vr(r){await ys(Z(),{recursive:!0}),await Ur($t(r.pid),JSON.stringify(r))}async function Hr(r,e){let t=$t(r);try{let n=JSON.parse(await Br(t,"utf8"));if(n.denied===e)return;n.denied=e,await Ur(t,JSON.stringify(n))}catch{}}async function Ft(r){await Me($t(r)).catch(()=>{}),await Me(Ze(r)).catch(()=>{})}async function jt(){let r;try{r=await gs(Z())}catch{return[]}let e=[];return await Promise.all(r.map(async t=>{if(!t.endsWith(".json"))return;let n=pe.join(Z(),t),s;try{s=JSON.parse(await Br(n,"utf8"))}catch{await Me(n).catch(()=>{});return}s&&typeof s.pid=="number"&&ws(s.pid)?e.push(s):(await Me(n).catch(()=>{}),s?.pid&&await Me(Ze(s.pid)).catch(()=>{}))})),e.sort((t,n)=>t.startedAt-n.startedAt)}async function qr(r){try{process.kill(r,"SIGTERM")}catch{}await Ft(r)}var Kr="127.0.0.1",Ps=new Set(["localhost","127.0.0.1","::1"]);function Es(r){if(r)try{return new URL(r.includes("://")?r:`http://${r}`).hostname}catch{return}}function zr(r){let e=Es(r);return!!e&&Ps.has(e)}function et(r){return r instanceof Error?r.message:"Unknown error"}async function Yr(r){let{getHtml:e,basePath:t,port:n,reloadEmitter:s,getServerExports:o,localOverride:i,trusted:a=!1,trustHint:c}=r,l=new xs;l.use("*",async(u,f)=>{if(!zr(u.req.header("host")))return u.text("Forbidden: untrusted Host",403);await f()}),l.use("*",async(u,f)=>{await f(),u.res.headers.set("Cross-Origin-Opener-Policy","same-origin"),u.res.headers.set("Cross-Origin-Embedder-Policy","credentialless")});let p=["/__fs/*","/__api/*","/__ai"],d=async(u,f)=>{if(!a){let y=new URL(u.req.url).pathname,w=y.startsWith("/__fs")?"the filesystem":y==="/__ai"?"AI (your API keys)":"server-side code (server.ts)";Hr(process.pid,w);let v=c?`
633
- ${c}`:"";return u.text(`Forbidden: this capability requires --trust.${v}`,403)}await f()},h=async(u,f)=>{let y=u.req.header("sec-fetch-site");if(y){if(y==="cross-site")return u.text("Forbidden: cross-site request",403)}else{let w=u.req.header("origin");if(w&&!zr(w))return u.text("Forbidden: cross-origin request",403)}await f()};for(let u of p)l.use(u,d),l.use(u,h);l.use("/__log",h),l.post("/__log",async u=>{try{let{args:f}=await u.req.json();console.log(...f||[])}catch{}return u.json({ok:!0})}),l.get("/",u=>u.html(e())),l.post("/__fs/read",async u=>{try{let{path:f}=await u.req.json(),y=Lt(f,t),w=await ee.readFile(y);return new Response(new Uint8Array(w),{headers:{"Content-Type":"application/octet-stream"}})}catch(f){let y=et(f);return u.json({error:y},400)}}),l.post("/__fs/write",async u=>{try{let f=u.req.query("path");if(!f)return u.json({error:"Missing path"},400);let y=Lt(f,t);return await ee.mkdir(F.dirname(y),{recursive:!0}),await ee.writeFile(y,Buffer.from(await u.req.arrayBuffer())),u.json({success:!0})}catch(f){let y=et(f);return u.json({error:y},400)}});let m={log:console.log};l.post("/__api/:name",async u=>{try{let f=o?.(),y=u.req.param("name"),w=f?.[y]??m[y];if(!w||typeof w!="function")return u.json({error:`API function '${y}' not found`},404);let{args:v}=await u.req.json(),C=await w(...v||[]);return u.json({result:C})}catch(f){let y=et(f);return u.json({error:y},500)}}),l.post("/__ai",async u=>{try{let{messages:f,system:y,reasoning:w,provider:v,model:C,webSearch:V}=await u.req.json();if(!f||!Array.isArray(f)||f.length===0)return u.json({message:"messages array is required",code:"unknown",retryable:!1},400);let N=ks(v,C);if(typeof N=="string")return u.json({message:N,code:"unknown",retryable:!1},400);let ot=[...y?[{role:"system",content:y}]:[],...f.map(Fe=>({role:Fe.role,content:Fe.content}))],te=await _t(N,{model:N.model,messages:ot,stream:!0,reasoning:w??0,webSearch:V??!0});if(!te.ok){let Fe=await Ct(te,N.protocol);return u.json(Fe,te.status)}let qt=await It(te,N.protocol);return qt||console.warn("[tb.ai] Empty response from provider"),u.json({text:qt})}catch(f){if(f instanceof I)return u.json({message:f.message,code:f.code,retryable:f.retryable},500);let y=et(f);return u.json({message:y,code:"unknown",retryable:!1},500)}}),l.get("/__models",async u=>{try{let f=await Cs();return u.json(f)}catch{return u.json([],200)}});let g=["esm.sh","unpkg.com","cdn.jsdelivr.net","cdnjs.cloudflare.com"],S=new Xe;if(l.get("/proxy/*",async u=>{let f=new URL(u.req.url),w=(f.pathname+f.search).slice(7),v=w.lastIndexOf("https://");return v===-1?u.text("Invalid proxy URL",400):j(u,w.slice(v))}),i){let u=`/local/${i.name}/`;l.get("/local/*",async f=>{let{pathname:y}=new URL(f.req.url);if(!y.startsWith(u))return f.text("Not Found",404);let w=decodeURIComponent(y.slice(u.length));try{let v=Lt(w,i.serveDir),C=await ee.readFile(v);return new Response(C,{headers:{"Content-Type":Os(v)}})}catch{return f.text("Not Found",404)}})}async function j(u,f){let y;try{y=new URL(f)}catch{return u.text("Invalid URL",400)}if(y.protocol!=="https:")return u.text("HTTPS only",400);if(!g.includes(y.hostname))return u.text("Host not allowed",403);let w=await S.get(f);if(w)return new Response(w.body,{status:200,headers:Nt(w.contentType,w.cacheControl)});try{let v=await fetch(f,{headers:{Accept:u.req.header("Accept")||"*/*"},redirect:"follow"});if(!v.ok)return u.text(`Upstream ${v.status}`,v.status);let C=v.headers.get("Content-Type")||void 0,V=v.headers.get("Cache-Control")||void 0;if(v.body){let[N,ot]=v.body.tee();return(async()=>{try{let te=await new Response(ot).arrayBuffer();await S.set(f,{body:Buffer.from(te),contentType:C,cacheControl:V})}catch{}})(),new Response(N,{status:v.status,headers:Nt(C,V)})}return new Response(null,{status:v.status,headers:Nt(C,V)})}catch(v){return u.text(`Proxy fetch failed: ${v instanceof Error?v.message:v}`,502)}}s&&l.get("/__reload",u=>Rs(u,async f=>{let y=()=>{f.writeSSE({event:"reload",data:""})};for(s.on("reload",y),f.onAbort(()=>{s.removeListener("reload",y)});;)await f.sleep(3e4)}));let B=/^\/(v\d+\/|stable\/|node\/|gh\/|@[^/]+\/[^@/]+@|[^@/]+@)/;l.notFound(async u=>{if(u.req.method!=="GET")return u.text("Not Found",404);let f=new URL(u.req.url);return B.test(f.pathname)?j(u,"https://esm.sh"+f.pathname+f.search):u.text("Not Found",404)});let T=Ss({fetch:l.fetch,port:n,hostname:Kr});return{port:n,close:()=>T.close()}}var Xr={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",gemini:"GOOGLE_API_KEY",openrouter:"OPENROUTER_API_KEY"};function ks(r,e){let t=r??process.env.TB_AI_PROVIDER,n=e??process.env.TB_AI_MODEL;if(!t)return"No provider specified. Set TB_AI_PROVIDER in your .env file or pass provider in the tb.ai() call.";if(!n)return"No model specified. Set TB_AI_MODEL in your .env file or pass model in the tb.ai() call.";let s;try{s=Q(t)}catch{return`Unknown provider '${t}'.`}let o=Xr[t],i=process.env[o];return i?{apiKey:i,baseUrl:s.defaultBaseUrl,protocol:t,model:n,isFreeModel:!1}:`No API key for '${t}'. Set ${o} in your .env file.`}var Ts="https://api.typebulb.com/api/models",_s=1440*60*1e3,fe=null;async function Cs(){if(!fe||Date.now()-fe.fetchedAt>_s){let r=await fetch(Ts);if(!r.ok)return fe?Gr(fe.models):[];fe={models:await r.json(),fetchedAt:Date.now()}}return Gr(fe.models)}function Gr(r){let e=new Set(Object.entries(Xr).filter(([,t])=>!!process.env[t]).map(([t])=>t));return r.filter(t=>e.has(t.provider))}function Nt(r,e){let t=new Headers;return r&&t.set("Content-Type",r),e&&t.set("Cache-Control",e),t.set("Access-Control-Allow-Origin","*"),t.set("Cross-Origin-Resource-Policy","cross-origin"),t}function Os(r){switch(F.extname(r).toLowerCase()){case".js":case".mjs":return"text/javascript";case".wasm":return"application/wasm";case".json":case".map":return"application/json";default:return"application/octet-stream"}}function Lt(r,e){let t=F.resolve(e,r),n=F.normalize(e),s=F.normalize(t);if(s!==n&&!s.startsWith(n+F.sep))throw new Error("Path traversal detected - access denied");return t}async function Bt(r){let e=await import("net");return new Promise(t=>{let n=e.createServer();n.listen(r,Kr,()=>{let s=n.address(),o=typeof s=="object"&&s?s.port:r;n.close(()=>t(o))}),n.on("error",()=>{t(Bt(r+1))})})}import Is from"open";async function Qr(r){await Is(r)}import Zr from"chokidar";var en={persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}};function Ut(r){let{bulbPath:e,emitter:t}=r,n=Zr.watch(e,en);return n.on("change",()=>{t.emit("reload")}),()=>n.close()}function tn(r){let{dir:e,onChange:t,debounceMs:n=150}=r,s,o=Zr.watch(e,en);return o.on("all",()=>{s&&clearTimeout(s),s=setTimeout(t,n)}),()=>{s&&clearTimeout(s),o.close()}}import*as k from"fs/promises";import*as E from"path";import*as he from"path";var As=he.join(We,"pkg"),Ds=he.join(We,"files"),$s=he.join(We,"negative.json"),rt=class{pkgMem=new Map;fileMem=new Map;negativeCache=new Y(new ae($s));async getCachedDts(e){if(this.pkgMem.has(e))return this.pkgMem.get(e);await x();let t=await X(rn(e));return this.pkgMem.set(e,t),t}async setCachedDts(e,t,n){let s={content:t,url:n};this.pkgMem.set(e,s),await tt(async()=>{await x(),await O(rn(e),JSON.stringify(s))})}async getCachedFile(e){if(this.fileMem.has(e))return this.fileMem.get(e);await x();let t=await Ue(nn(e));return this.fileMem.set(e,t),t}async setCachedFile(e,t){this.fileMem.set(e,t),await tt(async()=>{await x(),await O(nn(e),t)})}isNegative(e){return this.negativeCache.isNegative(e)}async recordNegative(e){await tt(async()=>{await x(),await this.negativeCache.recordNegative(e)})}clearNegative(e){return tt(()=>this.negativeCache.clearNegative(e))}};async function tt(r){try{await r()}catch{}}function rn(r){return he.join(As,D(r)+".json")}function nn(r){return he.join(Ds,D(r)+".txt")}var Wt;function Ms(){return Wt||(Wt=xt({cache:new rt,cdnClient:Cr,packageService:qe,versionResolver:_r})),Wt}var sn="file:///node_modules/";async function on(r){let e=Je(r.emitKey,"typecheck");await x(),await k.rm(e,{recursive:!0,force:!0}),await k.mkdir(e,{recursive:!0});let t=Ls(r.jsxImportSource,r.dependencies),n=await Ms().resolve(r.code,r.dependencies,t),s=r.local?.name,o=s?n.filter(p=>p.pkg!==s):n,i=new Set;for(let p of o)for(let d of p.files){let h=nt(d.path);if(!h||i.has(h))continue;i.add(h);let m=E.join(e,"node_modules",h);await k.mkdir(E.dirname(m),{recursive:!0}),await k.writeFile(m,d.content,"utf8")}let a={};for(let p of o)for(let d of p.shims){let h=nt(d.path);h&&(a[d.module]=[`./node_modules/${h}`])}for(let p of o){if(p.ambient)continue;let d=nt(p.mainPath);d&&(a[p.pkg]=[`./node_modules/${d}`])}let c=new Set;for(let p of o)for(let d of p.files){if(!d.ambient)continue;let h=nt(d.path);h&&c.add(`node_modules/${h}`)}if(r.local){delete a[r.local.name];let p=await Bs(r.local,e);p?a[r.local.name]=[`./node_modules/${r.local.name}/${p}`]:console.warn(` local: '${r.local.name}' ships no type defs; check cannot type against it.`)}let l=Fs(r.jsxImportSource,a,[...c]);return await k.writeFile(E.join(e,"tsconfig.json"),JSON.stringify(l,null,2)+`
634
- `,"utf8"),await k.writeFile(E.join(e,"code.tsx"),r.code,"utf8"),await k.writeFile(E.join(e,"tb.d.ts"),yt,"utf8"),{dir:e}}function Fs(r,e,t=[]){return{compilerOptions:{target:"es2023",module:"esnext",moduleResolution:"bundler",lib:js([...mt,...gt]),jsx:"react-jsx",jsxImportSource:r??"react",strict:!0,noEmit:!0,skipLibCheck:!0,esModuleInterop:!0,allowSyntheticDefaultImports:!0,forceConsistentCasingInFileNames:!0,baseUrl:".",paths:e},include:["code.tsx","tb.d.ts",...t]}}function js(r){return r.filter(e=>!e.since).map(e=>Ns(e.name))}function Ns(r){return r.split(".").map(e=>/^es\d+$/i.test(e)?e.toUpperCase():e==="dom"?"DOM":e==="esnext"?"ESNext":e.charAt(0).toUpperCase()+e.slice(1)).join(".")}function Ls(r,e){let t=r??("react"in e?"react":void 0);return t?[`${t}/jsx-runtime`,`${t}/jsx-dev-runtime`]:[]}function nt(r){if(!r.startsWith(sn))return;let e=r.slice(sn.length),t=e.indexOf("/");if(!(t<0))return e.slice(t+1)}async function Bs(r,e){if(!r.typesAbs)return;let t=E.dirname(r.typesAbs),n=E.join(e,"node_modules",r.name);for(let s of await Us(t)){let o=E.join(n,E.relative(t,s));await k.mkdir(E.dirname(o),{recursive:!0}),await k.copyFile(s,o)}return E.basename(r.typesAbs)}async function Us(r){let e=[];async function t(n){let s=await k.readdir(n,{withFileTypes:!0});for(let o of s){let i=E.join(n,o.name);o.isDirectory()?await t(i):/\.d\.ts(\.map)?$/.test(o.name)&&e.push(i)}}return await t(r),e}import*as A from"fs/promises";import*as J from"path";import{existsSync as Ws}from"fs";var Js="^22";async function an(r){let e=Je(r.emitKey,"typecheck-server");await x(),await A.rm(e,{recursive:!0,force:!0}),await A.mkdir(e,{recursive:!0});let t=J.join(r.bulbDir,".typebulb"),n=Ye(r.server).filter(i=>i!==r.local?.name);await Ke([`@types/node@${Js}`,...n],t,r.dependencies);let s=J.join(t,"node_modules"),o=J.join(e,"node_modules");return await Hs(s,o),await A.writeFile(J.join(e,"server.ts"),r.server,"utf8"),await A.writeFile(J.join(e,"tb.d.ts"),bt,"utf8"),await A.writeFile(J.join(e,"tsconfig.json"),JSON.stringify(Vs(r.local),null,2)+`
635
- `,"utf8"),{dir:e}}function Vs(r){let e={target:"es2023",module:"esnext",moduleResolution:"bundler",lib:["ES2023"],types:["node"],strict:!0,noEmit:!0,skipLibCheck:!0,esModuleInterop:!0,allowSyntheticDefaultImports:!0,forceConsistentCasingInFileNames:!0};if(r?.typesAbs){let t=o=>o.replace(/\\/g,"/"),n=t(J.dirname(r.typesAbs)),s=t(r.typesAbs).replace(/\.d\.ts$/,"");e.baseUrl=".",e.paths={[r.name]:[s],[`${r.name}/*`]:[`${n}/*`]}}return{compilerOptions:e,include:["server.ts","tb.d.ts"]}}async function Hs(r,e){if(!Ws(r)){await A.mkdir(e,{recursive:!0});return}await A.rm(e,{recursive:!0,force:!0});let t=process.platform==="win32"?"junction":"dir";await A.symlink(r,e,t)}import{readFileSync as qs,writeFileSync as zs,mkdirSync as Gs}from"fs";import{join as Ks}from"path";function cn(){return Ks(Qe(),"trust.json")}function Jt(){try{let r=JSON.parse(qs(cn(),"utf8"));return new Set(Array.isArray(r)?r:[])}catch{return new Set}}function Ys(r){Gs(Qe(),{recursive:!0}),zs(cn(),JSON.stringify([...r]))}function Vt(r){return Jt().has(ue(r))}function ln(r,e){let t=Jt(),n=ue(r);(e?t.has(n):!t.has(n))||(e?t.add(n):t.delete(n),Ys(t))}function dn(){return[...Jt()]}var Qs="0.9.5";async function Zs(r,e){let{bulb:t,config:n}=await de(r);!t.code&&!t.server&&(console.error("Bulb has neither **code.tsx** nor **server.ts**; nothing to check."),process.exit(1));let s=[];if(t.code){let{dir:i}=await on({code:t.code,dependencies:n.dependencies??{},jsxImportSource:n.jsxImportSource,emitKey:r,local:e});s.push({role:"client",dir:i})}if(t.server){let{dir:i}=await an({server:t.server,bulbDir:P.dirname(r),emitKey:r,local:e?{name:e.name,typesAbs:e.typesAbs}:void 0,dependencies:n.dependencies});s.push({role:"server",dir:i})}let o=!1;for(let{role:i,dir:a}of s){let{stdout:c,exitCode:l}=await to(a);for(let p of c.split(/\r?\n/))p.trim()&&console.log(`${i} ${p}`);l!==0&&(o=!0)}o&&process.exit(1)}async function eo(r,e){let{bulb:t}=await de(r),n=Tt(t);if(Vt(r)){console.log("remembered-trusted \u2014 runs with filesystem / AI / server.ts automatically (`typebulb untrust` to revoke)."),n&&console.log(` (it uses ${n})`);return}n?(console.log(`This bulb appears to use ${n}; it runs Restricted unless you grant trust:`),console.log(` ${e}`)):console.log("No privileged capability detected \u2014 runs Restricted by default. (A clean scan is a hint, not a guarantee.)")}function to(r){return new Promise(e=>{let t=Xs("npx",["tsc","--noEmit"],{cwd:r,shell:!0}),n="";t.stdout?.on("data",s=>{n+=s.toString()}),t.stderr?.on("data",s=>{n+=s.toString()}),t.on("close",s=>e({stdout:n,exitCode:s??1}))})}async function ro(r,e,t,n){let s=lt(t),o=P.dirname(r),i=!1,a=async()=>{let{bulb:c,config:l}=await de(r);i||(dt(s,r,c.server),i=!0),await Et(c.server,o,n,l.dependencies)};if(console.log(`Running ${P.basename(r)}...`),await a(),e){console.log(`Watching for changes...
636
- `);let c=new Ht;c.on("reload",async()=>{try{console.log("Re-running..."),await a()}catch(l){console.error("Error:",l)}}),Ut({bulbPath:r,emitter:c})}}async function no(r,e,t,n){let s=process.cwd(),o=Jr(process.pid),i=e.watch?new Ht:void 0,a=lt(e.mode);console.log(`Loading ${P.basename(r)}...`);let{html:c,bulb:l,serverExports:p}=await kt(r,e.watch,e.trust,n);dt(a,r,l.server);let d=await Bt(e.port),h=await Yr({getHtml:()=>c,basePath:s,port:d,reloadEmitter:i,getServerExports:()=>p,localOverride:n?{name:n.name,serveDir:n.serveDir}:void 0,trusted:e.trust,trustHint:t}),m=`http://localhost:${d}`,g=e.trust?void 0:Tt(l);await Vr({pid:process.pid,port:d,url:m,file:r,startedAt:Date.now(),trust:e.trust,predicted:g}),console.log(`
637
- ${l.name}`),console.log(` ${m}`),e.trust?console.log(" trust: granted (filesystem, AI, server.ts enabled)"):console.log(g?` trust: sandboxed \u2014 this bulb appears to use ${g}; re-run with --trust to enable it:
635
+ `);return e!==-1&&(t===-1||e<t)?{pos:e,len:4}:t!==-1?{pos:t,len:2}:{pos:-1,len:0}}function $t(r){let t=r.split(/\r?\n/).filter(s=>s.startsWith("data:"));if(!t.length)return null;let n=t.map(s=>s.replace(/^data:\s?/,"")).join(`
636
+ `).trim();if(!n)return null;if(n==="[DONE]")return"done";try{return JSON.parse(n)}catch{return null}}async function Lr(r,e,t){let n=new TextDecoder,s="",o=!1,i=t?new Promise((a,c)=>{t.aborted&&c(new Error("Aborted")),t.addEventListener("abort",()=>c(new Error("Aborted")),{once:!0})}):null;for(;;){let a=i?await Promise.race([r.read(),i]):await r.read(),{done:c,value:l}=a;if(c){if(s.trim()){let h=$t(s);h!==null&&h!=="done"&&e(h)}break}o=!0,s+=n.decode(l,{stream:!0});let{pos:p,len:d}=Nr(s);for(;p!==-1;){let h=s.slice(0,p);s=s.slice(p+d);let m=$t(h);if(m==="done"){s="";break}m!==null&&e(m),{pos:p,len:d}=Nr(s)}}return{receivedAnyData:o}}async function Dt(r,e){let t=e??(r.headers.get("X-Provider-Protocol")||"openai"),n=Z(t);if(!r.body)throw new Error("Response body is missing");let s=r.body.getReader(),o="";return await Lr(s,i=>{let a=n.parseStreamChunk(i);a?.text&&(o+=a.text)}),o}import*as Mt from"fs/promises";import*as De from"path";var Ze=class{async get(e){let t=$(e),n=De.join(Ce,t+".bin"),s=De.join(Ce,t+".json");try{let[o,i]=await Promise.all([Mt.readFile(n),Mt.readFile(s,"utf8")]),a=JSON.parse(i);return{body:o,contentType:a.contentType,cacheControl:a.cacheControl}}catch{return}}async set(e,t){await x();let n=$(e),s=De.join(Ce,n+".bin"),o=De.join(Ce,n+".json"),i={url:e,contentType:t.contentType,cacheControl:t.cacheControl};await Promise.all([I(s,t.body),I(o,JSON.stringify(i))])}};import{readdir as vs,readFile as Jr,writeFile as Hr,unlink as Me,mkdir as ws}from"fs/promises";import{mkdirSync as xs,writeFileSync as Ur,appendFileSync as Ss,readFileSync as Vr}from"fs";import*as ue from"path";import{join as Ft,resolve as ys}from"path";import{homedir as bs}from"os";function et(){let r=process.env.TYPEBULB_SERVERS_DIR;return r?Ft(r,".."):Ft(bs(),".typebulb")}function ee(){return process.env.TYPEBULB_SERVERS_DIR||Ft(et(),"servers")}function Y(r){let e=ys(r);return(process.platform==="win32"?e.toLowerCase():e).replace(/\\/g,"/")}function Br(r,e){let t=Y(r),n=Y(e);return t.startsWith(n.endsWith("/")?n:n+"/")?!t.includes("/node_modules/"):!1}var Wr=1e6;function jt(r){return ue.join(ee(),`${r}.json`)}function tt(r){return ue.join(ee(),`${r}.log`)}function qr(r){let e=tt(r);try{xs(ee(),{recursive:!0}),Ur(e,"")}catch{return()=>{}}let t=process.stdout.write.bind(process.stdout),n=process.stderr.write.bind(process.stderr),s=0,o=!1,i=a=>{if(!o)try{let c=typeof a=="string"?Buffer.from(a,"utf8"):Buffer.from(a);if(Ss(e,c),s+=c.length,s>2*Wr){let l=Vr(e),p=l.subarray(Math.max(0,l.length-Wr));Ur(e,p),s=p.length}}catch{o=!0}};return process.stdout.write=((a,...c)=>(i(a),t(a,...c))),process.stderr.write=((a,...c)=>(i(a),n(a,...c))),()=>{process.stdout.write=t,process.stderr.write=n}}function Nt(r,e=0){try{let t=Vr(tt(r)),n=e>=0&&e<=t.length?e:0;return{text:t.subarray(n).toString("utf8"),offset:t.length}}catch{return{text:"",offset:0}}}function Rs(r){try{return process.kill(r,0),!0}catch(e){return e.code==="EPERM"}}async function zr(r){await ws(ee(),{recursive:!0}),await Hr(jt(r.pid),JSON.stringify(r))}async function Gr(r,e){let t=jt(r);try{let n=JSON.parse(await Jr(t,"utf8"));if(n.denied===e)return;n.denied=e,await Hr(t,JSON.stringify(n))}catch{}}async function Lt(r){await Me(jt(r)).catch(()=>{}),await Me(tt(r)).catch(()=>{})}async function Fe(r){let e;try{e=await vs(ee())}catch{return[]}let t=[];return await Promise.all(e.map(async s=>{if(!s.endsWith(".json"))return;let o=ue.join(ee(),s),i;try{i=JSON.parse(await Jr(o,"utf8"))}catch{await Me(o).catch(()=>{});return}i&&typeof i.pid=="number"&&Rs(i.pid)?t.push(i):(await Me(o).catch(()=>{}),i?.pid&&await Me(tt(i.pid)).catch(()=>{}))})),(r?t.filter(s=>Br(s.file,r)):t).sort((s,o)=>s.startedAt-o.startedAt)}async function Yr(r){try{process.kill(r,"SIGTERM")}catch{}await Lt(r)}var Qr="127.0.0.1",Ts=new Set(["localhost","127.0.0.1","::1"]);function _s(r){if(r)try{return new URL(r.includes("://")?r:`http://${r}`).hostname}catch{return}}function Kr(r){let e=_s(r);return!!e&&Ts.has(e)}function rt(r){return r instanceof Error?r.message:"Unknown error"}async function Zr(r){let{getHtml:e,basePath:t,port:n,reloadEmitter:s,getServerExports:o,localOverride:i,trusted:a=!1,trustHint:c}=r,l=new Ps;l.use("*",async(u,f)=>{if(!Kr(u.req.header("host")))return u.text("Forbidden: untrusted Host",403);await f()}),l.use("*",async(u,f)=>{await f(),u.res.headers.set("Cross-Origin-Opener-Policy","same-origin"),u.res.headers.set("Cross-Origin-Embedder-Policy","credentialless")});let p=["/__fs/*","/__api/*","/__ai"],d=async(u,f)=>{if(!a){let y=new URL(u.req.url).pathname,w=y.startsWith("/__fs")?"the filesystem":y==="/__ai"?"AI (your API keys)":"server-side code (server.ts)";Gr(process.pid,w);let v=c?`
637
+ ${c}`:"";return u.text(`Forbidden: this capability requires --trust.${v}`,403)}await f()},h=async(u,f)=>{let y=u.req.header("sec-fetch-site");if(y){if(y==="cross-site")return u.text("Forbidden: cross-site request",403)}else{let w=u.req.header("origin");if(w&&!Kr(w))return u.text("Forbidden: cross-origin request",403)}await f()};for(let u of p)l.use(u,d),l.use(u,h);l.use("/__log",h),l.post("/__log",async u=>{try{let{args:f}=await u.req.json();console.log(...f||[])}catch{}return u.json({ok:!0})}),l.get("/",u=>u.html(e())),l.post("/__fs/read",async u=>{try{let{path:f}=await u.req.json(),y=Ut(f,t),w=await te.readFile(y);return new Response(new Uint8Array(w),{headers:{"Content-Type":"application/octet-stream"}})}catch(f){let y=rt(f);return u.json({error:y},400)}}),l.post("/__fs/write",async u=>{try{let f=u.req.query("path");if(!f)return u.json({error:"Missing path"},400);let y=Ut(f,t);return await te.mkdir(F.dirname(y),{recursive:!0}),await te.writeFile(y,Buffer.from(await u.req.arrayBuffer())),u.json({success:!0})}catch(f){let y=rt(f);return u.json({error:y},400)}});let m={log:console.log};l.post("/__api/:name",async u=>{try{let f=o?.(),y=u.req.param("name"),w=f?.[y]??m[y];if(!w||typeof w!="function")return u.json({error:`API function '${y}' not found`},404);let{args:v}=await u.req.json(),_=await w(...v||[]);return u.json({result:_})}catch(f){let y=rt(f);return u.json({error:y},500)}}),l.post("/__ai",async u=>{try{let{messages:f,system:y,reasoning:w,provider:v,model:_,webSearch:H}=await u.req.json();if(!f||!Array.isArray(f)||f.length===0)return u.json({message:"messages array is required",code:"unknown",retryable:!1},400);let N=Cs(v,_);if(typeof N=="string")return u.json({message:N,code:"unknown",retryable:!1},400);let ct=[...y?[{role:"system",content:y}]:[],...f.map(je=>({role:je.role,content:je.content}))],re=await Ot(N,{model:N.model,messages:ct,stream:!0,reasoning:w??0,webSearch:H??!0});if(!re.ok){let je=await At(re,N.protocol);return u.json(je,re.status)}let Gt=await Dt(re,N.protocol);return Gt||console.warn("[tb.ai] Empty response from provider"),u.json({text:Gt})}catch(f){if(f instanceof O)return u.json({message:f.message,code:f.code,retryable:f.retryable},500);let y=rt(f);return u.json({message:y,code:"unknown",retryable:!1},500)}}),l.get("/__models",async u=>{try{let f=await As();return u.json(f)}catch{return u.json([],200)}});let g=["esm.sh","unpkg.com","cdn.jsdelivr.net","cdnjs.cloudflare.com"],S=new Ze;if(l.get("/proxy/*",async u=>{let f=new URL(u.req.url),w=(f.pathname+f.search).slice(7),v=w.lastIndexOf("https://");return v===-1?u.text("Invalid proxy URL",400):j(u,w.slice(v))}),i){let u=`/local/${i.name}/`;l.get("/local/*",async f=>{let{pathname:y}=new URL(f.req.url);if(!y.startsWith(u))return f.text("Not Found",404);let w=decodeURIComponent(y.slice(u.length));try{let v=Ut(w,i.serveDir),_=await te.readFile(v);return new Response(_,{headers:{"Content-Type":$s(v)}})}catch{return f.text("Not Found",404)}})}async function j(u,f){let y;try{y=new URL(f)}catch{return u.text("Invalid URL",400)}if(y.protocol!=="https:")return u.text("HTTPS only",400);if(!g.includes(y.hostname))return u.text("Host not allowed",403);let w=await S.get(f);if(w)return new Response(w.body,{status:200,headers:Bt(w.contentType,w.cacheControl)});try{let v=await fetch(f,{headers:{Accept:u.req.header("Accept")||"*/*"},redirect:"follow"});if(!v.ok)return u.text(`Upstream ${v.status}`,v.status);let _=v.headers.get("Content-Type")||void 0,H=v.headers.get("Cache-Control")||void 0;if(v.body){let[N,ct]=v.body.tee();return(async()=>{try{let re=await new Response(ct).arrayBuffer();await S.set(f,{body:Buffer.from(re),contentType:_,cacheControl:H})}catch{}})(),new Response(N,{status:v.status,headers:Bt(_,H)})}return new Response(null,{status:v.status,headers:Bt(_,H)})}catch(v){return u.text(`Proxy fetch failed: ${v instanceof Error?v.message:v}`,502)}}s&&l.get("/__reload",u=>ks(u,async f=>{let y=()=>{f.writeSSE({event:"reload",data:""})};for(s.on("reload",y),f.onAbort(()=>{s.removeListener("reload",y)});;)await f.sleep(3e4)}));let B=/^\/(v\d+\/|stable\/|node\/|gh\/|@[^/]+\/[^@/]+@|[^@/]+@)/;l.notFound(async u=>{if(u.req.method!=="GET")return u.text("Not Found",404);let f=new URL(u.req.url);return B.test(f.pathname)?j(u,"https://esm.sh"+f.pathname+f.search):u.text("Not Found",404)});let C=Es({fetch:l.fetch,port:n,hostname:Qr});return{port:n,close:()=>C.close()}}var en={anthropic:"ANTHROPIC_API_KEY",openai:"OPENAI_API_KEY",gemini:"GOOGLE_API_KEY",openrouter:"OPENROUTER_API_KEY"};function Cs(r,e){let t=r??process.env.TB_AI_PROVIDER,n=e??process.env.TB_AI_MODEL;if(!t)return"No provider specified. Set TB_AI_PROVIDER in your .env file or pass provider in the tb.ai() call.";if(!n)return"No model specified. Set TB_AI_MODEL in your .env file or pass model in the tb.ai() call.";let s;try{s=Z(t)}catch{return`Unknown provider '${t}'.`}let o=en[t],i=process.env[o];return i?{apiKey:i,baseUrl:s.defaultBaseUrl,protocol:t,model:n,isFreeModel:!1}:`No API key for '${t}'. Set ${o} in your .env file.`}var Is="https://api.typebulb.com/api/models",Os=1440*60*1e3,pe=null;async function As(){if(!pe||Date.now()-pe.fetchedAt>Os){let r=await fetch(Is);if(!r.ok)return pe?Xr(pe.models):[];pe={models:await r.json(),fetchedAt:Date.now()}}return Xr(pe.models)}function Xr(r){let e=new Set(Object.entries(en).filter(([,t])=>!!process.env[t]).map(([t])=>t));return r.filter(t=>e.has(t.provider))}function Bt(r,e){let t=new Headers;return r&&t.set("Content-Type",r),e&&t.set("Cache-Control",e),t.set("Access-Control-Allow-Origin","*"),t.set("Cross-Origin-Resource-Policy","cross-origin"),t}function $s(r){switch(F.extname(r).toLowerCase()){case".js":case".mjs":return"text/javascript";case".wasm":return"application/wasm";case".json":case".map":return"application/json";default:return"application/octet-stream"}}function Ut(r,e){let t=F.resolve(e,r),n=F.normalize(e),s=F.normalize(t);if(s!==n&&!s.startsWith(n+F.sep))throw new Error("Path traversal detected - access denied");return t}async function Wt(r){let e=await import("net");return new Promise(t=>{let n=e.createServer();n.listen(r,Qr,()=>{let s=n.address(),o=typeof s=="object"&&s?s.port:r;n.close(()=>t(o))}),n.on("error",()=>{t(Wt(r+1))})})}import Ds from"open";async function tn(r){await Ds(r)}import rn from"chokidar";var nn={persistent:!0,ignoreInitial:!0,awaitWriteFinish:{stabilityThreshold:100,pollInterval:50}};function Jt(r){let{bulbPath:e,emitter:t}=r,n=rn.watch(e,nn);return n.on("change",()=>{t.emit("reload")}),()=>n.close()}function sn(r){let{dir:e,onChange:t,debounceMs:n=150}=r,s,o=rn.watch(e,nn);return o.on("all",()=>{s&&clearTimeout(s),s=setTimeout(t,n)}),()=>{s&&clearTimeout(s),o.close()}}import*as k from"fs/promises";import*as E from"path";import*as fe from"path";var Ms=fe.join(Je,"pkg"),Fs=fe.join(Je,"files"),js=fe.join(Je,"negative.json"),st=class{pkgMem=new Map;fileMem=new Map;negativeCache=new X(new ce(js));async getCachedDts(e){if(this.pkgMem.has(e))return this.pkgMem.get(e);await x();let t=await Q(on(e));return this.pkgMem.set(e,t),t}async setCachedDts(e,t,n){let s={content:t,url:n};this.pkgMem.set(e,s),await nt(async()=>{await x(),await I(on(e),JSON.stringify(s))})}async getCachedFile(e){if(this.fileMem.has(e))return this.fileMem.get(e);await x();let t=await We(an(e));return this.fileMem.set(e,t),t}async setCachedFile(e,t){this.fileMem.set(e,t),await nt(async()=>{await x(),await I(an(e),t)})}isNegative(e){return this.negativeCache.isNegative(e)}async recordNegative(e){await nt(async()=>{await x(),await this.negativeCache.recordNegative(e)})}clearNegative(e){return nt(()=>this.negativeCache.clearNegative(e))}};async function nt(r){try{await r()}catch{}}function on(r){return fe.join(Ms,$(r)+".json")}function an(r){return fe.join(Fs,$(r)+".txt")}var Ht;function Ns(){return Ht||(Ht=Pt({cache:new st,cdnClient:Or,packageService:ze,versionResolver:Ir})),Ht}var cn="file:///node_modules/";async function ln(r){let e=He(r.emitKey,"typecheck");await x(),await k.rm(e,{recursive:!0,force:!0}),await k.mkdir(e,{recursive:!0});let t=Ws(r.jsxImportSource,r.dependencies),n=await Ns().resolve(r.code,r.dependencies,t),s=r.local?.name,o=s?n.filter(p=>p.pkg!==s):n,i=new Set;for(let p of o)for(let d of p.files){let h=ot(d.path);if(!h||i.has(h))continue;i.add(h);let m=E.join(e,"node_modules",h);await k.mkdir(E.dirname(m),{recursive:!0}),await k.writeFile(m,d.content,"utf8")}let a={};for(let p of o)for(let d of p.shims){let h=ot(d.path);h&&(a[d.module]=[`./node_modules/${h}`])}for(let p of o){if(p.ambient)continue;let d=ot(p.mainPath);d&&(a[p.pkg]=[`./node_modules/${d}`])}let c=new Set;for(let p of o)for(let d of p.files){if(!d.ambient)continue;let h=ot(d.path);h&&c.add(`node_modules/${h}`)}if(r.local){delete a[r.local.name];let p=await Js(r.local,e);p?a[r.local.name]=[`./node_modules/${r.local.name}/${p}`]:console.warn(` local: '${r.local.name}' ships no type defs; check cannot type against it.`)}let l=Ls(r.jsxImportSource,a,[...c]);return await k.writeFile(E.join(e,"tsconfig.json"),JSON.stringify(l,null,2)+`
638
+ `,"utf8"),await k.writeFile(E.join(e,"code.tsx"),r.code,"utf8"),await k.writeFile(E.join(e,"tb.d.ts"),wt,"utf8"),{dir:e}}function Ls(r,e,t=[]){return{compilerOptions:{target:"es2023",module:"esnext",moduleResolution:"bundler",lib:Bs([...bt,...vt]),jsx:"react-jsx",jsxImportSource:r??"react",strict:!0,noEmit:!0,skipLibCheck:!0,esModuleInterop:!0,allowSyntheticDefaultImports:!0,forceConsistentCasingInFileNames:!0,baseUrl:".",paths:e},include:["code.tsx","tb.d.ts",...t]}}function Bs(r){return r.filter(e=>!e.since).map(e=>Us(e.name))}function Us(r){return r.split(".").map(e=>/^es\d+$/i.test(e)?e.toUpperCase():e==="dom"?"DOM":e==="esnext"?"ESNext":e.charAt(0).toUpperCase()+e.slice(1)).join(".")}function Ws(r,e){let t=r??("react"in e?"react":void 0);return t?[`${t}/jsx-runtime`,`${t}/jsx-dev-runtime`]:[]}function ot(r){if(!r.startsWith(cn))return;let e=r.slice(cn.length),t=e.indexOf("/");if(!(t<0))return e.slice(t+1)}async function Js(r,e){if(!r.typesAbs)return;let t=E.dirname(r.typesAbs),n=E.join(e,"node_modules",r.name);for(let s of await Hs(t)){let o=E.join(n,E.relative(t,s));await k.mkdir(E.dirname(o),{recursive:!0}),await k.copyFile(s,o)}return E.basename(r.typesAbs)}async function Hs(r){let e=[];async function t(n){let s=await k.readdir(n,{withFileTypes:!0});for(let o of s){let i=E.join(n,o.name);o.isDirectory()?await t(i):/\.d\.ts(\.map)?$/.test(o.name)&&e.push(i)}}return await t(r),e}import*as A from"fs/promises";import*as J from"path";import{existsSync as Vs}from"fs";var qs="^22";async function dn(r){let e=He(r.emitKey,"typecheck-server");await x(),await A.rm(e,{recursive:!0,force:!0}),await A.mkdir(e,{recursive:!0});let t=J.join(r.bulbDir,".typebulb"),n=Xe(r.server).filter(i=>i!==r.local?.name);await Ke([`@types/node@${qs}`,...n],t,r.dependencies);let s=J.join(t,"node_modules"),o=J.join(e,"node_modules");return await Gs(s,o),await A.writeFile(J.join(e,"server.ts"),r.server,"utf8"),await A.writeFile(J.join(e,"tb.d.ts"),xt,"utf8"),await A.writeFile(J.join(e,"tsconfig.json"),JSON.stringify(zs(r.local),null,2)+`
639
+ `,"utf8"),{dir:e}}function zs(r){let e={target:"es2023",module:"esnext",moduleResolution:"bundler",lib:["ES2023"],types:["node"],strict:!0,noEmit:!0,skipLibCheck:!0,esModuleInterop:!0,allowSyntheticDefaultImports:!0,forceConsistentCasingInFileNames:!0};if(r?.typesAbs){let t=o=>o.replace(/\\/g,"/"),n=t(J.dirname(r.typesAbs)),s=t(r.typesAbs).replace(/\.d\.ts$/,"");e.baseUrl=".",e.paths={[r.name]:[s],[`${r.name}/*`]:[`${n}/*`]}}return{compilerOptions:e,include:["server.ts","tb.d.ts"]}}async function Gs(r,e){if(!Vs(r)){await A.mkdir(e,{recursive:!0});return}await A.rm(e,{recursive:!0,force:!0});let t=process.platform==="win32"?"junction":"dir";await A.symlink(r,e,t)}import{readFileSync as Ys,writeFileSync as Ks,mkdirSync as Xs}from"fs";import{join as Qs}from"path";function un(){return Qs(et(),"trust.json")}function Vt(){try{let r=JSON.parse(Ys(un(),"utf8"));return new Set(Array.isArray(r)?r:[])}catch{return new Set}}function Zs(r){Xs(et(),{recursive:!0}),Ks(un(),JSON.stringify([...r]))}function qt(r){return Vt().has(Y(r))}function pn(r,e){let t=Vt(),n=Y(r);(e?t.has(n):!t.has(n))||(e?t.add(n):t.delete(n),Zs(t))}function fn(){return[...Vt()]}import*as he from"path";import{fileURLToPath as eo}from"url";var to={claude:{file:"claude.bulb.md",srcEnv:"TYPEBULB_CLAUDE_SRC"}};function it(r){let e=to[r];if(!e)return;let t=e.srcEnv?process.env[e.srcEnv]:void 0;return t?he.resolve(t):he.join(he.dirname(eo(import.meta.url)),"bulbs",e.file)}var no="0.9.7";async function so(r,e){let{bulb:t,config:n}=await de(r);!t.code&&!t.server&&(console.error("Bulb has neither **code.tsx** nor **server.ts**; nothing to check."),process.exit(1));let s=[];if(t.code){let{dir:i}=await ln({code:t.code,dependencies:n.dependencies??{},jsxImportSource:n.jsxImportSource,emitKey:r,local:e});s.push({role:"client",dir:i})}if(t.server){let{dir:i}=await dn({server:t.server,bulbDir:P.dirname(r),emitKey:r,local:e?{name:e.name,typesAbs:e.typesAbs}:void 0,dependencies:n.dependencies});s.push({role:"server",dir:i})}let o=!1;for(let{role:i,dir:a}of s){let{stdout:c,exitCode:l}=await io(a);for(let p of c.split(/\r?\n/))p.trim()&&console.log(`${i} ${p}`);l!==0&&(o=!0)}o&&process.exit(1)}async function oo(r,e){let{bulb:t}=await de(r),n=It(t);if(qt(r)){console.log("remembered-trusted \u2014 runs with filesystem / AI / server.ts automatically (`typebulb untrust` to revoke)."),n&&console.log(` (it uses ${n})`);return}n?(console.log(`This bulb appears to use ${n}; it runs Restricted unless you grant trust:`),console.log(` ${e}`)):console.log("No privileged capability detected \u2014 runs Restricted by default. (A clean scan is a hint, not a guarantee.)")}function io(r){return new Promise(e=>{let t=ro("npx",["tsc","--noEmit"],{cwd:r,shell:!0}),n="";t.stdout?.on("data",s=>{n+=s.toString()}),t.stderr?.on("data",s=>{n+=s.toString()}),t.on("close",s=>e({stdout:n,exitCode:s??1}))})}async function ao(r,e,t,n,s){let o=pt(t),i=!1,a=async()=>{let{bulb:c,config:l}=await de(r);i||(ft(o,r,c.server),i=!0),await _t(c.server,s,n,l.dependencies)};if(console.log(`Running ${P.basename(r)}...`),await a(),e){console.log(`Watching for changes...
640
+ `);let c=new zt;c.on("reload",async()=>{try{console.log("Re-running..."),await a()}catch(l){console.error("Error:",l)}}),Jt({bulbPath:r,emitter:c})}}async function co(r,e,t,n,s){let o=process.cwd(),i=qr(process.pid),a=e.watch?new zt:void 0,c=pt(e.mode);console.log(`Loading ${P.basename(r)}...`);let{html:l,bulb:p,serverExports:d}=await Ct(r,e.watch,e.trust,n,s);ft(c,r,p.server);let h=await Wt(e.port),m=await Zr({getHtml:()=>l,basePath:o,port:h,reloadEmitter:a,getServerExports:()=>d,localOverride:n?{name:n.name,serveDir:n.serveDir}:void 0,trusted:e.trust,trustHint:t}),g=`http://localhost:${h}`,S=e.trust?void 0:It(p);await zr({pid:process.pid,port:h,url:g,file:r,startedAt:Date.now(),trust:e.trust,predicted:S}),console.log(`
641
+ ${p.name}`),console.log(` ${g}`),e.trust?console.log(" trust: granted (filesystem, AI, server.ts enabled)"):console.log(S?` trust: sandboxed \u2014 this bulb appears to use ${S}; re-run with --trust to enable it:
638
642
  ${t}
639
643
  `:` trust: sandboxed \u2014 re-run with --trust to enable filesystem / AI / server.ts
640
644
  `),e.watch&&console.log(` Watching for changes...
641
- `);let S,j;if(e.watch&&i){let T=new Ht;if(T.on("reload",async()=>{try{console.log("Recompiling...");let u=await kt(r,!0,e.trust,n);c=u.html,p=u.serverExports,i.emit("reload"),console.log(`Done. Browser reloading...
642
- `)}catch(u){console.error("Compile error:",u)}}),S=Ut({bulbPath:r,emitter:T}),n){let{name:u,serveDir:f}=n;j=tn({dir:f,onChange:()=>{console.log(`Local package '${u}' changed. Browser reloading...
643
- `),i.emit("reload")}})}}e.open&&await Qr(m);let B=async()=>{console.log(`
644
- Shutting down...`),h.close(),S?.(),j?.(),o(),await Ft(process.pid);let T=P.join(P.dirname(r),".typebulb","server.mjs");await st.rm(T,{force:!0}).catch(()=>{}),process.exit(0)};process.on("SIGINT",B),process.on("SIGTERM",B)}function so(r,e){return/^\d+$/.test(e)?r.find(t=>t.pid===parseInt(e,10)):r.find(t=>ue(t.file)===ue(e))}function un(r,e){for(let t of r)e(` ${t.url} pid ${t.pid} ${t.trust?"trusted":"restricted"} ${t.file}`)}function pn(r,e){if(!r.length){console.log("No running bulb servers.");return}console.log("Running bulb servers:"),un(r,t=>console.log(t)),console.log(`
645
- `+e)}function fn(r,e,t){let n=so(r,e);if(n)return n;console.error(`No running server for '${e}'.`),r.length?(console.error(`Running servers (try \`typebulb ${t} <file|pid>\`):`),un(r,s=>console.error(s))):console.error("No bulb servers are running."),process.exit(1)}async function oo(r,e){let t=await jt();if(!r){pn(t,"Run `typebulb logs <file|pid>` to print one server's console.");return}let n=fn(t,r,"logs"),s=Mt(n.pid),o=s.text;if(e.lines&&e.lines>0){let i=o.split(`
646
- `);i.length&&i[i.length-1]===""&&i.pop(),o=i.slice(-e.lines).join(`
647
- `)}if(process.stdout.write(o),o&&!o.endsWith(`
645
+ `);let j,B;if(e.watch&&a){let u=new zt;if(u.on("reload",async()=>{try{console.log("Recompiling...");let f=await Ct(r,!0,e.trust,n,s);l=f.html,d=f.serverExports,a.emit("reload"),console.log(`Done. Browser reloading...
646
+ `)}catch(f){console.error("Compile error:",f)}}),j=Jt({bulbPath:r,emitter:u}),n){let{name:f,serveDir:y}=n;B=sn({dir:y,onChange:()=>{console.log(`Local package '${f}' changed. Browser reloading...
647
+ `),a.emit("reload")}})}}e.open&&await tn(g);let C=async()=>{console.log(`
648
+ Shutting down...`),m.close(),j?.(),B?.(),i(),await Lt(process.pid);let u=P.join(P.dirname(r),".typebulb","server.mjs");await at.rm(u,{force:!0}).catch(()=>{}),process.exit(0)};process.on("SIGINT",C),process.on("SIGTERM",C)}function lo(r,e){return/^\d+$/.test(e)?r.find(t=>t.pid===parseInt(e,10)):r.find(t=>Y(t.file)===Y(e))}function hn(r,e){for(let t of r)e(` ${t.url} pid ${t.pid} ${t.trust?"trusted":"restricted"} ${t.file}`)}function mn(r,e){if(!r.length){console.log("No running bulb servers.");return}console.log("Running bulb servers:"),hn(r,t=>console.log(t)),console.log(`
649
+ `+e)}function gn(r,e,t){let n=lo(r,e);if(n)return n;console.error(`No running server for '${e}'.`),r.length?(console.error(`Running servers (try \`typebulb ${t} <file|pid>\`):`),hn(r,s=>console.error(s))):console.error("No bulb servers are running."),process.exit(1)}async function uo(r,e){if(!r){mn(await Fe(process.cwd()),"Run `typebulb logs <file|pid>` to print one server's console.");return}let t=gn(await Fe(),it(r)??r,"logs"),n=Nt(t.pid),s=n.text;if(e.lines&&e.lines>0){let o=s.split(`
650
+ `);o.length&&o[o.length-1]===""&&o.pop(),s=o.slice(-e.lines).join(`
651
+ `)}if(process.stdout.write(s),s&&!s.endsWith(`
648
652
  `)&&process.stdout.write(`
649
- `),e.follow){let i=s.offset,a=setInterval(()=>{let l=Mt(n.pid,i);i=l.offset,l.text&&process.stdout.write(l.text)},500),c=()=>{clearInterval(a),process.exit(0)};process.on("SIGINT",c),process.on("SIGTERM",c),await new Promise(()=>{})}}async function io(r){let e=await jt();if(!r){pn(e,"Run `typebulb stop <file|pid>` to stop one.");return}let t=fn(e,r,"stop");await qr(t.pid),console.log(`Stopped ${P.basename(t.file)} (pid ${t.pid}, ${t.url}).`)}async function ao(r,e){if(!r){e||(console.error("Usage: typebulb untrust <file.bulb.md>"),process.exit(1));let n=dn();if(!n.length){console.log("No bulbs are remembered as trusted.");return}console.log("Trusted bulbs (run with fs/AI/server.ts without --trust):");for(let s of n)console.log(` ${s}`);return}let t=P.resolve(r);t.endsWith(".bulb.md")||(console.error("File must have .bulb.md extension"),process.exit(1)),ln(t,e),console.log(e?`Trusted ${P.basename(t)} \u2014 runs with fs / AI / server.ts (no --trust needed).`:`Untrusted ${P.basename(t)} \u2014 runs Restricted.`),console.log(` ${t}`)}async function co(){let r=tr(process.argv.slice(2));if(r.version&&(console.log(`typebulb ${Qs}`),process.exit(0)),r.help&&(rr(),process.exit(0)),r.subcommand==="logs"){await oo(r.file||void 0,{follow:r.follow,lines:r.lines});return}if(r.subcommand==="stop"){await io(r.file||void 0);return}if(r.subcommand==="trust"||r.subcommand==="untrust"){await ao(r.file||void 0,r.subcommand==="trust");return}let e;if(!r.file||r.file==="."){let a=await Mr(process.cwd());a||(console.error("No .bulb.md file found in current directory"),process.exit(1)),e=a}else e=P.resolve(r.file);try{await st.access(e)}catch{console.error(`File not found: ${e}`),process.exit(1)}e.endsWith(".bulb.md")||(console.error("File must have .bulb.md extension"),process.exit(1));let t=r.file&&r.file!=="."?r.file:P.relative(process.cwd(),e)||P.basename(e),n=`npx typebulb --trust ${t.includes(" ")?`"${t}"`:t}`;if(r.subcommand==="predict"){await eo(e,n);return}let s=!r.noTrust&&Vt(e);s&&!r.trust&&console.log("trust: granted from memory (run `typebulb untrust` to revoke)"),r.trust=r.noTrust?!1:r.trust||s;let o;try{o=await de(e)}catch{}let i;if(r.local){o&&!(r.local.name in(o.config.dependencies??{}))&&(console.error(`--replace: '${r.local.name}' is not a dependency in this bulb's config.json; nothing to replace.`),process.exit(1)),o&&(!o.bulb.code||r.server)&&console.warn("warning: --replace has no effect in server mode (the override is client-only).");try{i=await Zt(r.local)}catch(a){console.error(a instanceof Error?a.message:String(a)),process.exit(1)}console.log(`replace: ${i.name} \u2192 ${P.relative(process.cwd(),i.dir)||"."}`)}if(r.subcommand==="check"){await Zs(e,i);return}if(o&&o.bulb.server&&(!o.bulb.code||r.server)){r.trust||(console.error(`This bulb runs server-side Node code (server.ts), which --trust must authorize:
650
- ${n}`),process.exit(1)),await ro(e,r.watch,r.mode,i);return}await no(e,r,n,i)}co().catch(r=>{console.error("Error:",r.message),process.exit(1)});
653
+ `),e.follow){let o=n.offset,i=setInterval(()=>{let c=Nt(t.pid,o);o=c.offset,c.text&&process.stdout.write(c.text)},500),a=()=>{clearInterval(i),process.exit(0)};process.on("SIGINT",a),process.on("SIGTERM",a),await new Promise(()=>{})}}async function po(r){if(!r){mn(await Fe(process.cwd()),"Run `typebulb stop <file|pid>` to stop one.");return}let e=gn(await Fe(),it(r)??r,"stop");await Yr(e.pid),console.log(`Stopped ${P.basename(e.file)} (pid ${e.pid}, ${e.url}).`)}async function fo(r,e){if(!r){e||(console.error("Usage: typebulb untrust <file.bulb.md>"),process.exit(1));let n=fn();if(!n.length){console.log("No bulbs are remembered as trusted.");return}console.log("Trusted bulbs (run with fs/AI/server.ts without --trust):");for(let s of n)console.log(` ${s}`);return}let t=P.resolve(r);t.endsWith(".bulb.md")||(console.error("File must have .bulb.md extension"),process.exit(1)),pn(t,e),console.log(e?`Trusted ${P.basename(t)} \u2014 runs with fs / AI / server.ts (no --trust needed).`:`Untrusted ${P.basename(t)} \u2014 runs Restricted.`),console.log(` ${t}`)}async function ho(){let r=nr(process.argv.slice(2));if(r.version&&(console.log(`typebulb ${no}`),process.exit(0)),r.help&&(sr(),process.exit(0)),r.subcommand==="logs"){await uo(r.file||void 0,{follow:r.follow,lines:r.lines});return}if(r.subcommand==="stop"){await po(r.file||void 0);return}if(r.subcommand==="trust"||r.subcommand==="untrust"){await fo(r.file||void 0,r.subcommand==="trust");return}let e,t=!1;if(!r.file||r.file==="."){let l=await jr(process.cwd());l||(console.error("No .bulb.md file found in current directory"),process.exit(1)),e=l}else e=P.resolve(r.file);if(!await at.access(e).then(()=>!0,()=>!1)){let l=it(r.file);l?(e=l,t=!0):(console.error(`File not found: ${e}`),process.exit(1))}e.endsWith(".bulb.md")||(console.error("File must have .bulb.md extension"),process.exit(1));let s=r.file&&r.file!=="."?r.file:P.relative(process.cwd(),e)||P.basename(e),o=`npx typebulb --trust ${s.includes(" ")?`"${s}"`:s}`;if(r.subcommand==="predict"){await oo(e,o);return}if(t)r.trust=!r.noTrust,r.trust&&console.log("trust: granted (built-in bulb)");else{let l=!r.noTrust&&qt(e);l&&!r.trust&&console.log("trust: granted from memory (run `typebulb untrust` to revoke)"),r.trust=r.noTrust?!1:r.trust||l}let i;try{i=await de(e)}catch{}let a;if(r.local){i&&!(r.local.name in(i.config.dependencies??{}))&&(console.error(`--replace: '${r.local.name}' is not a dependency in this bulb's config.json; nothing to replace.`),process.exit(1)),i&&(!i.bulb.code||r.server)&&console.warn("warning: --replace has no effect in server mode (the override is client-only).");try{a=await tr(r.local)}catch(l){console.error(l instanceof Error?l.message:String(l)),process.exit(1)}console.log(`replace: ${a.name} \u2192 ${P.relative(process.cwd(),a.dir)||"."}`)}if(r.subcommand==="check"){await so(e,a);return}let c=t?process.cwd():P.dirname(e);if(process.env.TYPEBULB_BULB_PATH=e,i&&i.bulb.server&&(!i.bulb.code||r.server)){r.trust||(console.error(`This bulb runs server-side Node code (server.ts), which --trust must authorize:
654
+ ${o}`),process.exit(1)),await ao(e,r.watch,r.mode,a,c);return}await co(e,r,o,a,c)}ho().catch(r=>{console.error("Error:",r.message),process.exit(1)});