blade-code 0.8.2 → 0.8.3

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.
Files changed (74) hide show
  1. package/dist/blade.js +2 -2
  2. package/dist/{chunk-0z5qe4m2.js → chunk-1m63zddw.js} +1 -1
  3. package/dist/chunk-23h9grdr.js +1 -0
  4. package/dist/{chunk-6jq8h5s9.js → chunk-2c69g6t9.js} +2 -2
  5. package/dist/{chunk-p4yrmzyr.js → chunk-5sccs44n.js} +1 -1
  6. package/dist/{chunk-5ad4qmfj.js → chunk-5yt80bn2.js} +1 -1
  7. package/dist/chunk-6msfa7jh.js +9 -0
  8. package/dist/{chunk-yfcfx0zk.js → chunk-6spmzx13.js} +1 -1
  9. package/dist/chunk-724djh7r.js +1 -0
  10. package/dist/{chunk-81p4kz0b.js → chunk-7bqkkjxf.js} +1 -1
  11. package/dist/chunk-804j7say.js +1 -0
  12. package/dist/chunk-8yk5w2ks.js +1 -0
  13. package/dist/chunk-97qxd4yb.js +2 -0
  14. package/dist/{chunk-qwfqp465.js → chunk-9cr3jexy.js} +1 -1
  15. package/dist/{chunk-kne7h7p5.js → chunk-ar5564sf.js} +1 -1
  16. package/dist/{chunk-f3pvwxp8.js → chunk-b922cq7k.js} +3 -3
  17. package/dist/{chunk-w4xcmzjm.js → chunk-be7qz4cv.js} +1 -1
  18. package/dist/chunk-bqhghfmt.js +6 -0
  19. package/dist/{chunk-s1y8w68s.js → chunk-crbh6x86.js} +1 -1
  20. package/dist/{chunk-5fw9k1ff.js → chunk-d7nrqg5b.js} +2 -2
  21. package/dist/{chunk-9xvnrq7m.js → chunk-eyypmc9m.js} +2 -2
  22. package/dist/{chunk-j77gyhpf.js → chunk-fma9cbq5.js} +1 -1
  23. package/dist/chunk-ghf5tgjr.js +1 -0
  24. package/dist/{chunk-11s7t9dp.js → chunk-hqtskyc1.js} +2 -2
  25. package/dist/chunk-jc7k8pfn.js +22 -0
  26. package/dist/{chunk-eycs00af.js → chunk-kf43f8sy.js} +218 -218
  27. package/dist/{chunk-rxjt7ma7.js → chunk-kjc2aqww.js} +1 -1
  28. package/dist/chunk-kjddt9xy.js +1 -0
  29. package/dist/{chunk-6avkjdjc.js → chunk-m0ded6ky.js} +27 -27
  30. package/dist/{chunk-fpdqvnmq.js → chunk-mqqajem9.js} +2 -2
  31. package/dist/{chunk-a90mfxv9.js → chunk-msr67f6q.js} +1 -1
  32. package/dist/{chunk-y08szyf9.js → chunk-mvww9cmc.js} +20 -20
  33. package/dist/{chunk-fkaebj8c.js → chunk-nbpjzzrg.js} +1 -1
  34. package/dist/chunk-ncvr73ev.js +1 -0
  35. package/dist/{chunk-9b297mzh.js → chunk-qvtahmff.js} +1 -1
  36. package/dist/chunk-qwn46cd6.js +2 -0
  37. package/dist/{chunk-887qxhn5.js → chunk-sva5bdgz.js} +2 -2
  38. package/dist/{chunk-4fye39a8.js → chunk-t9cqscfq.js} +1 -1
  39. package/dist/chunk-tzr992v4.js +1 -0
  40. package/dist/{chunk-1x3846v7.js → chunk-vxpdjtqc.js} +1 -1
  41. package/dist/{chunk-6x6zqjcs.js → chunk-wcf80p9y.js} +1 -1
  42. package/dist/chunk-x8xfx9nv.js +1 -0
  43. package/dist/{chunk-8fp3q919.js → chunk-yewg510f.js} +2 -2
  44. package/dist/{chunk-w11ht6f9.js → chunk-zj3q4btp.js} +1 -1
  45. package/dist/chunk-zjks0ncc.js +6 -0
  46. package/dist/{chunk-0g9cb41d.js → chunk-zsyff0ky.js} +2 -2
  47. package/dist/web/assets/{FilePreview-60E51igH.js → FilePreview-B5g8yi8d.js} +1 -1
  48. package/dist/web/assets/{MarkdownRenderer-DU7jPn9K.js → MarkdownRenderer-qBCxzEb_.js} +1 -1
  49. package/dist/web/assets/{McpModal-CAMU6yrS.js → McpModal-C7y2Fsqc.js} +1 -1
  50. package/dist/web/assets/{SettingsModal-Cjj8NUQy.js → SettingsModal-Dmc7Dw_E.js} +1 -1
  51. package/dist/web/assets/{SettingsStore-CjvkbtuZ.js → SettingsStore-BfoB9tNV.js} +1 -1
  52. package/dist/web/assets/{SkillsModal-Cyc2JAFj.js → SkillsModal-CqFAIfj5.js} +1 -1
  53. package/dist/web/assets/{TerminalPanel-DI-Ls-U8.js → TerminalPanel-EHh8-9b9.js} +1 -1
  54. package/dist/web/assets/index-B-BRpW1c.js +16 -0
  55. package/dist/web/assets/index-Ch0eqSLM.css +1 -0
  56. package/dist/web/index.html +2 -2
  57. package/package.json +1 -1
  58. package/dist/chunk-1evg2hdd.js +0 -2
  59. package/dist/chunk-3wghk9bp.js +0 -1
  60. package/dist/chunk-510rwhx6.js +0 -1
  61. package/dist/chunk-83dkggbx.js +0 -1
  62. package/dist/chunk-8epvwyg8.js +0 -6
  63. package/dist/chunk-9jkbykwq.js +0 -1
  64. package/dist/chunk-a83s9mww.js +0 -1
  65. package/dist/chunk-btst30by.js +0 -6
  66. package/dist/chunk-e9t5ga1b.js +0 -1
  67. package/dist/chunk-gvf4gff0.js +0 -9
  68. package/dist/chunk-j14rn2dx.js +0 -22
  69. package/dist/chunk-kcjgbbec.js +0 -1
  70. package/dist/chunk-q223bq79.js +0 -1
  71. package/dist/chunk-x83a97eh.js +0 -1
  72. package/dist/chunk-zns65xsy.js +0 -2
  73. package/dist/web/assets/index-BC0ssrep.js +0 -16
  74. package/dist/web/assets/index-Ci6gCUjf.css +0 -1
@@ -1,2 +0,0 @@
1
- import{e as kO,f as EO}from"./chunk-1x3846v7.js";import{p as bO}from"./chunk-qwfqp465.js";import{B as CO,D as XO,E as hS,F as IS,H as yS,I as GS,J as q,K as M,L as _O,M as F,N as qS,P as DO,R as FS,S as CS,U as mO,ba as PO}from"./chunk-eycs00af.js";import{ea as e}from"./chunk-6jq8h5s9.js";import{Aa as eS,Ba as sS,Ea as r,xa as $S,za as rS}from"./chunk-8fp3q919.js";import{Ma as NS}from"./chunk-887qxhn5.js";import{Sa as zO,Ua as DS,cb as ES}from"./chunk-6avkjdjc.js";import{ub as yO}from"./chunk-p4yrmzyr.js";import{Ab as O,Bb as h,Cb as pS,Db as E,yb as t}from"./chunk-0z5qe4m2.js";import{Eb as kS}from"./chunk-81p4kz0b.js";import{Kb as L}from"./chunk-rxjt7ma7.js";import{Vb as W}from"./chunk-9xvnrq7m.js";import{_b as wO,cc as oS,fc as s}from"./chunk-0g9cb41d.js";import{gc as tS}from"./chunk-w4xcmzjm.js";var G0={port:{type:"number",describe:"Port to listen on (0 for auto)",default:0},hostname:{type:"string",describe:"Hostname to listen on",default:"127.0.0.1"},cors:{type:"array",describe:"Additional domains to allow for CORS",default:[]}};function Q0(S){return{port:S.port??0,hostname:S.hostname??"127.0.0.1",cors:S.cors??[]}}import{existsSync as oO,readFileSync as BS}from"node:fs";import{createServer as S0}from"node:http";import{networkInterfaces as O0}from"node:os";import{dirname as x0,extname as j0,join as jS}from"node:path";import{fileURLToPath as $0}from"node:url";import{Hono as N0}from"hono";import{cors as V0}from"hono/cors";import{WebSocketServer as J0}from"ws";class g extends Error{code;statusCode;constructor(S,x,j=500){super(x);this.code=S;this.statusCode=j;this.name="BladeServerError"}toObject(){return{error:{code:this.code,message:this.message}}}}class z extends g{constructor(S,x){super("NOT_FOUND",x?`${S} not found: ${x}`:`${S} not found`,404)}}class m extends g{constructor(S){super("BAD_REQUEST",S,400)}}class I extends g{constructor(S){super("CONFLICT",S,409)}}class o extends g{constructor(){super("AMBIGUOUS_SESSION","Multiple workspaces contain this session ID; projectPath is required",409)}}class u extends g{constructor(S="Internal server error"){super("INTERNAL_ERROR",S,500)}}var f0=O.Object({error:O.Object({code:O.String(),message:O.String()})});import{Hono as eO}from"hono";var HS=W("Service"),sO=O.Object({updates:O.Record(O.String(),O.Any()),options:O.Optional(O.Object({scope:O.Optional(E(["local","project","global"])),immediate:O.Optional(O.Boolean())}))});function Sx(S){return{currentModelId:S.currentModelId,permissionMode:S.permissionMode,language:S.language,theme:S.theme,uiTheme:S.uiTheme,autoSaveSessions:S.autoSaveSessions,notifyBuild:S.notifyBuild,notifyErrors:S.notifyErrors,notifySounds:S.notifySounds,privacyTelemetry:S.privacyTelemetry,privacyCrash:S.privacyCrash}}var dS=()=>{let S=new eO;return S.get("/",async(x)=>{try{let j=$S();return x.json(j?Sx(j):{})}catch(j){return HS.error("[ConfigRoutes] Failed to get config:",j),x.json({})}}),S.put("/",async(x)=>{try{let j=await x.req.json(),$=t(sO,j);if(!$.success)throw new m("Invalid config update format");let{updates:V,options:A}=$.data;return await r().updateConfig(V,A),x.json({success:!0,updates:V})}catch(j){throw HS.error("[ConfigRoutes] Failed to update config:",j),j}}),S.get("/permissions",async(x)=>{try{let j=$S();return x.json(j?.permissions||{})}catch(j){return HS.error("[ConfigRoutes] Failed to get permissions:",j),x.json({})}}),S};import{Hono as Ox}from"hono";import{streamSSE as xx}from"hono/streaming";var jx=15000,$x=new Set(["task.status"]),Nx=new Set(["queued","running","completed","failed","cancelled","interrupted"]);function Vx(S){if(!S||typeof S!=="object"||Array.isArray(S))return;let x=S,j=["changedFiles","additions","deletions","commits"];if(j.some(($)=>typeof x[$]!=="number"||!Number.isInteger(x[$])||x[$]<0))return;return Object.fromEntries(j.map(($)=>[$,x[$]]))}var aS=()=>{let S=new Ox;return S.get("/",(x)=>xx(x,async(j)=>{let $,V,A=!1,Y=()=>{if(V!==void 0)clearInterval(V),V=void 0;$?.(),$=void 0},f=()=>{if(A)return;A=!0,Y()};j.onAbort(f),$=M.subscribe((Z)=>{if(!$x.has(Z.type))return;let w=Z.properties.taskStatus;if(typeof w!=="string"||!Nx.has(w))return;let b=Vx(Z.properties.taskDiffStat);j.writeSSE({data:JSON.stringify({type:Z.type,properties:{sessionId:Z.sessionId,projectPath:Z.projectPath,taskStatus:w,...typeof Z.properties.taskStatusReason==="string"?{taskStatusReason:Z.properties.taskStatusReason}:{},...typeof Z.properties.taskStartedAt==="string"?{taskStartedAt:Z.properties.taskStartedAt}:{},...typeof Z.properties.taskCompletedAt==="string"?{taskCompletedAt:Z.properties.taskCompletedAt}:{},...typeof Z.properties.updatedAt==="string"?{updatedAt:Z.properties.updatedAt}:{},...b?{taskDiffStat:b}:{}}})}).catch(f)});try{if(j.aborted||A)return;if(await j.writeSSE({data:JSON.stringify({type:"connected",properties:{timestamp:Date.now()}})}),j.aborted||A)return;V=setInterval(()=>{if(j.aborted||A)return;j.writeSSE({data:JSON.stringify({type:"heartbeat",properties:{timestamp:Date.now()}})}).catch(f)},jx);while(!j.aborted&&!A)await new Promise((Z)=>setTimeout(Z,1000))}finally{f()}})),S};import{Hono as Jx}from"hono";var iS=()=>{let S=new Jx;return S.get("/health",(x)=>{return x.json({healthy:!0,version:NS()})}),S.get("/info",(x)=>{let j=L();return x.json({version:NS(),platform:process.platform,arch:process.arch,nodeVersion:process.version,cwd:j,gitBranch:oS(j)})}),S};import{Hono as Tx}from"hono";var YS=W("Service"),nS=()=>{let S=new Tx;return S.get("/",async(x)=>{try{let $=e.getInstance().getAllServers(),V=Array.from($.entries()).map(([A,Y])=>({id:A,name:A,status:Y.status==="connected"?"connected":Y.status==="connecting"?"connecting":Y.lastError?"error":"offline",endpoint:Y.config.command?`${Y.config.command} ${(Y.config.args||[]).join(" ")}`.trim():Y.config.url||"Unknown",description:`MCP server: ${A}`,tools:Y.tools.map((f)=>f.name),connectedAt:Y.connectedAt?.toISOString(),error:Y.lastError?.message}));return x.json(V)}catch(j){return YS.error("[McpRoutes] Failed to get MCP servers:",j),x.json([])}}),S.post("/:name/connect",async(x)=>{try{let j=x.req.param("name");return await e.getInstance().connectServer(j),x.json({success:!0})}catch(j){return YS.error("[McpRoutes] Failed to connect MCP server:",j),x.json({success:!1,error:j.message},500)}}),S.post("/:name/disconnect",async(x)=>{try{let j=x.req.param("name");return await e.getInstance().disconnectServer(j),x.json({success:!0})}catch(j){return YS.error("[McpRoutes] Failed to disconnect MCP server:",j),x.json({success:!1,error:j.message},500)}}),S.delete("/:name",async(x)=>{try{let j=x.req.param("name");await e.getInstance().unregisterServer(j);let V=$S();if(V?.mcpServers){let{[j]:A,...Y}=V.mcpServers;await r().updateConfig({mcpServers:Y})}return x.json({success:!0})}catch(j){return YS.error("[McpRoutes] Failed to delete MCP server:",j),x.json({success:!1,error:j.message},500)}}),S.post("/",async(x)=>{try{let j=await x.req.json(),{name:$,config:V}=j;if(!$||!V)return x.json({success:!1,error:"Missing name or config"},400);await e.getInstance().registerServer($,V);let Y=$S();return await r().updateConfig({mcpServers:{...Y?.mcpServers,[$]:V}}),x.json({success:!0})}catch(j){return YS.error("[McpRoutes] Failed to add MCP server:",j),x.json({success:!1,error:j.message},500)}}),S};import{Hono as Ax}from"hono";var US=W("Service");function Yx(S,x){return{id:S.id,displayName:S.displayName,provider:S.provider,model:S.model,overrides:S.overrides,fallbackModels:S.fallbackModels,contextWindow:x.contextWindow,maxTokens:x.maxTokens,reasoning:x.reasoning,input:x.input}}function px(S){let x={displayName:S.displayName||void 0,overrides:S.overrides||void 0};if(S.model)x.model=S.model;return x}var SO=()=>{let S=new Ax;return S.get("/",async(x)=>{try{let j=rS(),$=eS(),V=s(),A=(Y)=>Yx(Y,V.resolveConfig(Y));return x.json({configured:j.map(A),current:$?A($):null})}catch(j){return US.error("[ModelsRoutes] Failed to get models:",j),x.json({configured:[],current:null})}}),S.post("/",async(x)=>{try{let j=await x.req.json(),{provider:$,displayName:V,model:A,overrides:Y,apiKey:f}=j;if(!$||!A)throw new m("provider and model are required");if(s().getModel($,A),f?.trim())await s().setApiKey($,f.trim());let Z=await r().addModel({provider:$,displayName:V||void 0,model:A,overrides:Y||void 0});return x.json({success:!0,model:Z})}catch(j){throw US.error("[ModelsRoutes] Failed to add model:",j),j}}),S.put("/:modelId",async(x)=>{try{let j=x.req.param("modelId"),$=await x.req.json(),{displayName:V,overrides:A,model:Y}=$,f=sS(j);if(!f)throw new m("model config not found");if(Y)s().getModel(f.provider,Y);return await r().updateModel(j,px({displayName:V,overrides:A,model:Y})),x.json({success:!0})}catch(j){throw US.error("[ModelsRoutes] Failed to update model:",j),j}}),S.delete("/:modelId",async(x)=>{try{let j=x.req.param("modelId");return await r().removeModel(j),x.json({success:!0})}catch(j){throw US.error("[ModelsRoutes] Failed to delete model:",j),j}}),S};import{Hono as Fx}from"hono";import ZO from"node:path";import{Mutex as _x}from"async-mutex";import{Hono as Dx}from"hono";import{streamSSE as mx}from"hono/streaming";import{LRUCache as bx}from"lru-cache";import{nanoid as VS}from"nanoid";var KS=h(E(["default","autoEdit","yolo","plan"]));var WS=h(E(["user","assistant","system","tool"])),Gx=h(O.Union([O.Object({type:O.Literal("text"),text:O.String()}),O.Object({type:O.Literal("image_url"),image_url:O.Object({url:O.String()})})])),xO=O.Union([O.String(),O.Array(Gx)]),o0=h(O.Object({id:O.String(),role:WS,content:xO,timestamp:O.Number(),metadata:O.Optional(O.Record(O.String(),O.Unknown())),thinkingContent:O.Optional(O.String()),tool_call_id:O.Optional(O.String()),name:O.Optional(O.String()),tool_calls:O.Optional(O.Unknown())})),Qx=h(E(["queued","running","completed","failed","cancelled","interrupted"])),jO=h(E(["local","worktree"])),Zx=h(O.Object({changedFiles:O.Integer({minimum:0}),additions:O.Integer({minimum:0}),deletions:O.Integer({minimum:0}),commits:O.Integer({minimum:0})})),Ux=h(O.Object({path:O.String({minLength:1}),patch:O.String({maxLength:2097152}),additions:O.Integer({minimum:0}),deletions:O.Integer({minimum:0}),binary:O.Boolean(),truncated:O.Boolean()})),$O=h(O.Object({sessionId:O.String(),projectPath:O.String(),baseCommit:O.String(),files:O.Array(Ux,{maxItems:100}),truncated:O.Boolean()})),NO=h(O.Object({sessionId:O.String(),projectPath:O.String(),title:O.Optional(O.String()),gitBranch:O.Optional(O.String()),rootId:O.String(),parentId:O.Optional(O.String()),relationType:O.Optional(E(["subagent","fork"])),resumedFrom:O.Optional(O.String()),rootAgentId:O.Optional(O.String()),resumeDepth:O.Optional(O.Integer({minimum:0})),taskStatus:pS(Qx,"completed"),taskStatusReason:O.Optional(O.String()),taskStartedAt:O.Optional(O.String()),taskCompletedAt:O.Optional(O.String()),taskPromptSummary:O.Optional(O.String()),taskIsolation:O.Optional(jO),taskSourceProjectPath:O.Optional(O.String()),taskWorktreePath:O.Optional(O.String()),taskWorktreeBranch:O.Optional(O.String()),taskBaseCommit:O.Optional(O.String()),taskDiffStat:O.Optional(Zx),messageCount:O.Number(),firstMessageTime:O.String(),lastMessageTime:O.String(),hasErrors:O.Boolean()})),i0=h(O.Object({version:O.Literal(1),sessionId:O.String(),goalId:O.String(),objective:O.String(),status:E(["active","paused","blocked","usage_limited","budget_limited","complete"]),tokenBudget:O.Optional(O.Number()),tokensUsed:O.Number(),timeUsedSeconds:O.Number(),continuationCount:O.Number(),statusReason:O.Optional(O.String()),createdAt:O.String(),updatedAt:O.String()})),VO=h(O.Object({role:WS,content:xO,metadata:O.Optional(O.Unknown()),reasoningContent:O.Optional(O.String()),thinkingContent:O.Optional(O.String()),tool_call_id:O.Optional(O.String()),name:O.Optional(O.String()),tool_calls:O.Optional(O.Unknown())})),n0=h(O.Object({session:NO,messages:O.Array(VO)})),JO=h(E(["conversation","code","both"])),fx=h(O.Object({messageId:O.String(),preview:O.String(),createdAt:O.String(),fileCount:O.Integer({minimum:0})})),TO=h(O.Object({targetMessageId:O.String({minLength:1}),mode:pS(JO,"conversation")})),r0=h(O.Object({checkpoint:fx,mode:JO,removedTurns:O.Integer({minimum:1}),restoredFiles:O.Array(O.String()),messages:O.Array(VO)})),wx=E(["running","completed","failed","cancelled"]),OO=h(O.Object({id:O.String(),subagentType:O.String(),description:O.String(),status:wx,rootAgentId:O.String(),resumedFrom:O.Optional(O.String()),resumeDepth:O.Integer({minimum:0}),createdAt:O.Number(),lastActiveAt:O.Number(),completedAt:O.Optional(O.Number()),result:O.Optional(O.Object({success:O.Boolean(),message:O.String(),error:O.Optional(O.String()),verificationCommands:O.Optional(O.Array(O.String()))})),stats:O.Optional(O.Object({tokens:O.Optional(O.Number()),toolCalls:O.Optional(O.Number()),duration:O.Optional(O.Number())}))})),AO=h(O.Object({prompt:O.String({minLength:1,maxLength:32000,pattern:"\\S"})})),e0=h(O.Object({source:OO,session:OO})),s0=h(O.Object({sessionId:O.String(),projectPath:O.String()})),S1=h(O.Object({type:O.String(),properties:O.Record(O.String(),O.Unknown())})),YO=O.Object({type:E(["file","image","url"]),path:O.Optional(O.String()),url:O.Optional(O.String()),content:O.Optional(O.String()),mimeType:O.Optional(O.String()),name:O.Optional(O.String())}),pO=h(O.Object({prompt:O.String({minLength:1,maxLength:32000,pattern:"\\S"}),title:O.Optional(O.String({minLength:1,maxLength:200})),projectPath:O.Optional(O.String()),isolation:pS(jO,"worktree"),permissionMode:pS(KS,"default"),attachments:O.Optional(O.Array(YO))})),GO=h(O.Object({session:NO,runId:O.String(),messageId:O.String(),status:O.Literal("running")})),QO=h(O.Object({content:O.String(),projectPath:O.Optional(O.String()),permissionMode:O.Optional(KS),attachments:O.Optional(O.Array(YO))})),O1=h(O.Object({messageId:O.String(),role:WS,content:O.String(),timestamp:O.String()})),x1=h(O.Object({approved:O.Boolean(),remember:O.Optional(O.Boolean()),scope:O.Optional(E(["once","session","project"])),targetMode:O.Optional(KS),feedback:O.Optional(O.String()),answers:O.Optional(O.Record(O.String(),O.Union([O.String(),O.Array(O.String())])))})),j1=h(O.Object({id:O.String(),displayName:O.Optional(O.String()),provider:O.String(),model:O.String(),contextWindow:O.Optional(O.Number()),maxTokens:O.Optional(O.Number()),reasoning:O.Optional(O.Boolean()),input:O.Optional(O.Array(E(["text","image"]))),overrides:O.Optional(O.Object({baseUrl:O.Optional(O.String()),temperature:O.Optional(O.Number()),maxOutputTokens:O.Optional(O.Number()),timeout:O.Optional(O.Number())}))})),$1=h(E(["vs-dark","vs-light","hc-black"])),yx=h(E(["light","dark","system"])),Xx=h(O.Object({language:O.String(),theme:O.String(),uiTheme:yx,autoSaveSessions:O.Boolean(),notifyBuild:O.Boolean(),notifyErrors:O.Boolean(),notifySounds:O.Boolean(),privacyTelemetry:O.Boolean(),privacyCrash:O.Boolean()})),N1=h(O.Partial(Xx));var H=W("Service"),hx=O.Object({title:O.Optional(O.String()),projectPath:O.Optional(O.String())}),Bx=QO,Mx=O.Object({title:O.Optional(O.String()),projectPath:O.Optional(O.String())}),Hx=O.Object({projectPath:O.String()}),Kx=O.Object({objective:O.String({minLength:1}),tokenBudget:O.Optional(O.Integer({minimum:1})),permissionMode:O.Optional(E(["default","autoEdit","plan","yolo"]))}),Wx=O.Union([O.Object({action:O.Literal("pause")}),O.Object({action:O.Literal("resume")}),O.Object({action:O.Literal("edit"),objective:O.String({minLength:1})})]),R=new Map,C=new bx({max:100,ttl:1800000,dispose:(S,x)=>{if(S.status==="running"||S.status==="waiting_permission")S.abortController.abort(),H.debug(`[SessionRoutes] Run ${x} disposed due to cache eviction`)}});function kx(S){return{sessionId:S.sessionId,projectPath:S.projectPath}}function _S(S,x="user-cancel"){if(S.status==="cancelled"||S.status==="completed"||S.status==="failed")return!1;let j=S.pendingPermission;return S.pendingPermission=void 0,j?.resolve({approved:!1,reason:yO}),S.abortController.abort(x),S.status="cancelled",M.publish(kx(S),"run.cancelled",{runId:S.id}),!0}function hO(S,x=!1){let{permissionId:j,details:$}=S;if($.type==="askUserQuestion"&&$.questions)return{type:"question.required",properties:{requestId:j,toolCallId:j,questions:$.questions,details:$,...x?{replayed:!0}:{}}};return{type:"permission.asked",properties:{requestId:j,toolName:$.toolName,description:$.message,args:$.args,details:$,...x?{replayed:!0}:{}}}}function Ex(){for(let S of C.values())_S(S,"route-reset");C.clear(),R.clear()}var BO=(S)=>{if(!S||typeof S!=="object")return S;let x={...S},j=200000;if(typeof x.oldContent==="string"&&x.oldContent.length>j)delete x.oldContent;if(typeof x.newContent==="string"&&x.newContent.length>j)delete x.newContent;return x};function qx(S,x,j){switch(j.kind){case"tool_start":if("function"in j.toolCall)M.publish(S,"subagent.update",{subagentSessionId:x,toolName:j.toolCall.function.name}),M.publish(S,"subagent.tool.start",{subagentSessionId:x,toolCallId:j.toolCall.id,toolName:j.toolCall.function.name,arguments:j.toolCall.function.arguments,toolKind:j.toolKind});break;case"tool_result":if("function"in j.toolCall)M.publish(S,"subagent.tool.result",{subagentSessionId:x,toolCallId:j.toolCall.id,toolName:j.toolCall.function.name,success:!j.result.error,summary:j.result.metadata?.summary,output:CS(FS(j.toolCall.function.name,j.result)),metadata:BO(j.result.metadata)});break;case"content_delta":M.publish(S,"subagent.delta",{subagentSessionId:x,delta:j.delta});break;case"thinking_delta":M.publish(S,"subagent.thinking.delta",{subagentSessionId:x,delta:j.delta});break;case"stream_end":M.publish(S,"subagent.stream.end",{subagentSessionId:x});break;default:break}}function XS(S,x="projectPath"){if(!ZO.isAbsolute(S))throw new m(`${x} must be absolute`);return ZO.resolve(S)}function zS(S){try{wO(S)}catch(x){throw new m(x instanceof Error?x.message:`Invalid session ID: ${S}`)}}function v(S){return{sessionId:S.id,projectPath:S.projectPath}}function fS(S,x,j){return{id:S.sessionId,projectPath:S.projectPath,title:S.title??`Session ${S.sessionId.slice(0,6)}`,createdAt:new Date(S.firstMessageTime),updatedAt:new Date(S.lastMessageTime),rootId:S.rootId,parentId:S.parentId,relationType:S.relationType,taskStatus:S.taskStatus,taskStatusReason:S.taskStatusReason,taskStartedAt:S.taskStartedAt,taskCompletedAt:S.taskCompletedAt,taskPromptSummary:S.taskPromptSummary,taskIsolation:S.taskIsolation,taskSourceProjectPath:S.taskSourceProjectPath,taskWorktreePath:S.taskWorktreePath,taskWorktreeBranch:S.taskWorktreeBranch,taskBaseCommit:S.taskBaseCommit,taskDiffStat:S.taskDiffStat,taskWorktree:j,messages:x}}function zx(S,x){S.taskStatus=x.taskStatus,S.taskStatusReason=x.taskStatusReason,S.taskStartedAt=x.taskStartedAt,S.taskCompletedAt=x.taskCompletedAt,S.taskPromptSummary=x.taskPromptSummary,S.taskIsolation=x.taskIsolation,S.taskSourceProjectPath=x.taskSourceProjectPath,S.taskWorktreePath=x.taskWorktreePath,S.taskWorktreeBranch=x.taskWorktreeBranch,S.taskBaseCommit=x.taskBaseCommit,S.taskDiffStat=x.taskDiffStat,S.updatedAt=new Date(x.lastMessageTime)}async function UO(S){let x=await F.findSessionMetadata(S.id,S.projectPath);if(x)zx(S,x)}function wS(S){let x=S.currentRunId?C.get(S.currentRunId):void 0,j=x?.status==="waiting_permission"?"running":x?.status??S.taskStatus;return{sessionId:S.id,projectPath:S.projectPath,title:S.title,rootId:S.rootId,parentId:S.parentId,relationType:S.relationType,status:void 0,taskStatus:j,taskStatusReason:S.taskStatusReason,taskStartedAt:S.taskStartedAt,taskCompletedAt:S.taskCompletedAt,taskPromptSummary:S.taskPromptSummary,taskIsolation:S.taskIsolation,taskSourceProjectPath:S.taskSourceProjectPath,taskWorktreePath:S.taskWorktreePath,taskWorktreeBranch:S.taskWorktreeBranch,taskBaseCommit:S.taskBaseCommit,taskDiffStat:S.taskDiffStat,agentType:void 0,model:void 0,messageCount:S.messages.length,firstMessageTime:S.createdAt.toISOString(),lastMessageTime:S.updatedAt.toISOString(),hasErrors:!1,isActive:!0}}async function n(S,x){if(zS(S),x!==void 0){let $=GS({sessionId:S,projectPath:XS(x)});if(R.has(q($)))return $;let V=await F.findSessionMetadata($.sessionId,$.projectPath);if(!V)throw new z("Session",S);return GS({sessionId:V.sessionId,projectPath:V.projectPath})}let j=new Map;for(let $ of R.values()){if($.id!==S)continue;let V=v($);j.set(q(V),V)}for(let $ of await F.listSessions()){if($.sessionId!==S)continue;let V=GS({sessionId:$.sessionId,projectPath:$.projectPath});j.set(q(V),V)}if(j.size===0)try{let $=await F.findSessionMetadata(S);if(!$)throw new z("Session",S);return GS({sessionId:$.sessionId,projectPath:$.projectPath})}catch($){if($ instanceof z)throw $;if($ instanceof Error&&$.message.startsWith("Ambiguous session ID:"))throw new o;throw $}if(j.size>1)throw new o;return j.values().next().value}function LS(S){if(typeof S==="string")return S;return S.filter((x)=>x.type==="text").map((x)=>x.text).join(`
2
- `)}function fO(S,x){let j=(x??[]).filter((V)=>V.type==="image"&&typeof V.content==="string").map((V)=>({type:"image_url",image_url:{url:V.content}}));if(j.length===0)return S;let $=[];if(S.trim())$.push({type:"text",text:S});return $.push(...j),$}var MO=()=>{Ex();let S=new Dx;S.onError((N,T)=>{if(N instanceof g)return T.json(N.toObject(),N.statusCode);return H.error("[SessionRoutes] Unhandled route error:",N),T.json({error:{code:"INTERNAL_ERROR",message:"Internal server error"}},500)});let x=new Map,j=new Map,$=new Map,V=new Map,A=(N)=>{let T=q(N),J=V.get(T);if(!J)J=new _x,V.set(T,J);return J},Y=async(N)=>{let T=q(v(N)),J=x.get(T);if(J)return J;let G=j.get(T);if(!G)G=DO.create({sessionId:N.id,workspaceRoot:N.projectPath,...N.taskWorktree?{taskWorktree:N.taskWorktree}:{}}),j.set(T,G);try{let p=await G;return x.set(T,p),p}finally{if(j.get(T)===G)j.delete(T)}},f=async(N)=>{let T=q(N),J=R.get(T);if(J)return J;let G=$.get(T);if(!G)G=(async()=>{let p=await F.findSessionMetadata(N.sessionId,N.projectPath);if(!p)throw new z("Session",N.sessionId);let[X,U]=await Promise.all([F.loadSession(N.sessionId,N.projectPath),F.findSessionTaskWorktree(N.sessionId,N.projectPath)]),_=fS(p,X,U);return R.set(T,_),_})(),$.set(T,G);try{return await G}finally{if($.get(T)===G)$.delete(T)}},Z=async(N,T)=>{return f(await n(N,T))},w=(N,T,J,G={})=>{let p=VS(12),X={id:p,sessionId:N.id,projectPath:N.projectPath,status:"running",abortController:new AbortController,createdAt:new Date};return C.set(p,X),N.currentRunId=p,X.completion=Lx(X,N,T,J,Y,{pendingInputOnly:G.pendingInputOnly,preparedInputTurn:G.preparedInputTurn,goalContinuationOnly:G.goalContinuationOnly}).catch((U)=>{H.error(`[SessionRoutes] Run ${p} failed:`,U)}),X},b=async(N)=>{let T=kS("task",12),J=XS(N.sourceProjectPath),G,p;try{let X=await bO.createSessionTask({sessionId:T,prompt:N.prompt,title:N.title,sourceProjectPath:J,isolation:N.isolation}),{metadata:U}=X;G=X.taskWorktree,p=fS(U,[],G);let _=v(p);R.set(q(_),p),M.publish(_,"task.status",{taskStatus:U.taskStatus,updatedAt:U.lastMessageTime});let D=fO(N.prompt,N.attachments),y=await(await Y(p)).prepareInputTurn(D);if(!y.accepted)throw new I(`Task prompt was not accepted: ${y.reason}`);let l=w(p,D,N.permissionMode,{preparedInputTurn:y});return{session:wS(p),runId:l.id,messageId:y.messageId,status:"running"}}catch(X){if(p){let U=await F.findSessionMetadata(p.id,p.projectPath).catch(()=>{return});if(!U||U.taskStatus==="queued"){let _=await F.updateSessionMetadata(p.id,p.projectPath,{taskStatus:"failed",taskStatusReason:"Task dispatch failed",taskCompletedAt:new Date().toISOString(),taskOwnerPid:null}).catch(()=>{return});p.taskStatus=_?.taskStatus??"failed",p.taskStatusReason=_?.taskStatusReason??"Task dispatch failed",p.taskCompletedAt=_?.taskCompletedAt}else p.taskStatus=U.taskStatus,p.taskStatusReason=U.taskStatusReason,p.taskCompletedAt=U.taskCompletedAt}throw X}},B=async(N,T)=>{let J=await Z(N,T);if(!J.taskWorktree)throw new m("Session does not have a task worktree");try{await qS.restoreSession(J.taskWorktree);let G=await qS.getDiffArtifact(J.id);if(!G)throw new I("Task diff artifact is unavailable");return{sessionId:J.id,projectPath:J.projectPath,...G}}catch(G){if(G instanceof I)throw G;throw H.error("[SessionRoutes] Failed to load task diff artifact:",G),new I("Task diff artifact is unavailable")}},K=async(N)=>{let T=N.currentRunId?C.get(N.currentRunId):void 0;if(T&&(T.status==="running"||T.status==="waiting_permission"))return;let J=await Y(N),G=N.currentRunId?C.get(N.currentRunId):void 0;if(G&&(G.status==="running"||G.status==="waiting_permission"))return;let p=J.getPendingSteeringCount()>0,U=(p?null:await J.getGoal())?.status==="active";if(!p&&!U||J.hasTurnOwner())return;w(N,"","default",{pendingInputOnly:p,goalContinuationOnly:U})};return S.get("/",async(N)=>{try{let T=await F.listSessions(),J=new Set(T.filter((Q)=>Q.relationType==="subagent").map((Q)=>q({sessionId:Q.sessionId,projectPath:Q.projectPath}))),G=Array.from(R.values()).filter((Q)=>!J.has(q(v(Q)))&&Q.relationType!=="subagent").map((Q)=>wS(Q)),p=new Set(G.map((Q)=>q({sessionId:Q.sessionId,projectPath:Q.projectPath}))),X=T.filter((Q)=>{if(Q.relationType==="subagent")return!1;return!p.has(q({sessionId:Q.sessionId,projectPath:Q.projectPath}))}),U=new Set(p),_=X.filter((Q)=>{let y=q({sessionId:Q.sessionId,projectPath:Q.projectPath});if(U.has(y))return!1;return U.add(y),!0}),D=[...G,..._];return N.json(D)}catch(T){throw H.error("[SessionRoutes] Failed to list sessions:",T),new u("Failed to list sessions")}}),S.post("/",async(N)=>{try{let T=await N.req.json(),J=t(hx,T);if(!J.success)throw new m("Invalid request body");let{title:G,projectPath:p}=J.data,X=kS("web",12),U=XS(p||N.get("directory")||L(),p?"projectPath":"directory"),_=await F.createSessionMetadata(X,U,{title:G}),D=fS(_,[]),Q=v(D);return R.set(q(Q),D),M.publish(Q,"task.status",{taskStatus:_.taskStatus,updatedAt:_.lastMessageTime}),N.json({...wS(D),status:void 0,agentType:void 0,model:void 0})}catch(T){if(H.error("[SessionRoutes] Failed to create session:",T),T instanceof m)throw T;throw new u("Failed to create session")}}),S.post("/:sessionId/fork",async(N)=>{let T=N.req.param("sessionId");try{zS(T);let J=await N.req.json(),G=t(Hx,J);if(!G.success)throw new m("Invalid request body");let p=XS(G.data.projectPath),X=await F.findSessionMetadata(T,p);if(!X)throw new z("Session",T);let U=await F.forkSession(T,{sourceProjectPath:X.projectPath,targetProjectPath:X.projectPath}),_=fS(U.metadata,U.messages),D=v(_);return R.set(q(D),_),M.publish(D,"task.status",{taskStatus:U.metadata.taskStatus,...U.metadata.taskCompletedAt?{taskCompletedAt:U.metadata.taskCompletedAt}:{},updatedAt:U.metadata.lastMessageTime}),N.json({session:U.metadata,messages:U.messages},201)}catch(J){if(J instanceof m||J instanceof z||J instanceof I)throw J;if(J instanceof _O)throw new I("Session has no durable creation record");throw H.error("[SessionRoutes] Failed to fork session:",J),new u("Failed to fork session")}}),S.get("/:sessionId",async(N)=>{let T=N.req.param("sessionId");try{let J=await n(T,N.req.query("projectPath")),G=R.get(q(J));if(G)return N.json(wS(G));let p=await F.findSessionMetadata(J.sessionId,J.projectPath);if(!p)throw new z("Session",T);return N.json(p)}catch(J){if(J instanceof m||J instanceof z||J instanceof o)throw J;throw H.error("[SessionRoutes] Failed to get session:",J),new u("Failed to get session")}}),S.patch("/:sessionId",async(N)=>{let T=N.req.param("sessionId");try{let J=await N.req.json(),G=t(Mx,J);if(!G.success)throw new m("Invalid request body");if(G.data.title===void 0)throw new m("title is required");let p=await n(T,G.data.projectPath),X=await F.updateSessionMetadata(p.sessionId,p.projectPath,{title:G.data.title}),U=R.get(q(p));if(U)U.title=X.title??U.title,U.updatedAt=new Date(X.lastMessageTime);return N.json({success:!0,title:X.title})}catch(J){if(H.error("[SessionRoutes] Failed to update session:",J),J instanceof m||J instanceof z||J instanceof o)throw J;throw new u("Failed to update session")}}),S.get("/:sessionId/rewind",async(N)=>{let T=await Z(N.req.param("sessionId"),N.req.query("projectPath")),J=await Y(T);return N.json({checkpoints:await J.listRewindCheckpoints()})}),S.post("/:sessionId/rewind",async(N)=>{let T=t(TO,await N.req.json());if(!T.success)throw new m("Invalid rewind request");let J=await Z(N.req.param("sessionId"),N.req.query("projectPath")),G=v(J);return A(G).runExclusive(async()=>{let p=J.currentRunId?C.get(J.currentRunId):void 0;if(p&&(p.status==="running"||p.status==="waiting_permission"))throw new I("Cannot rewind while a run is active");let X=await Y(J),U;try{U=await X.rewindSession(T.data)}catch(_){if(_ instanceof Error&&_.message.startsWith("Cannot rewind while"))throw new I(_.message);throw _}return J.messages=[...U.messages],J.updatedAt=new Date,M.publish(G,"session.rewound",{targetMessageId:U.checkpoint.messageId,mode:U.mode,removedTurns:U.removedTurns,restoredFiles:U.restoredFiles,messages:U.messages}),N.json(U)})}),S.get("/:sessionId/subagents",async(N)=>{let T=await Z(N.req.param("sessionId"),N.req.query("projectPath")),J=await Y(T);return N.json({subagents:J.listSubagents().map(yS)})}),S.post("/:sessionId/subagents/:agentId/resume",async(N)=>{zS(N.req.param("agentId"));let T=AO.safeParse(await N.req.json());if(!T.success)throw new m("Invalid subagent resume request");let J=await Z(N.req.param("sessionId"),N.req.query("projectPath")),G=v(J);return A(G).runExclusive(async()=>{let p=J.currentRunId?C.get(J.currentRunId):void 0;if(p&&(p.status==="running"||p.status==="waiting_permission"))throw new I("Cannot resume a subagent while a parent run is active");let X=await Y(J),U=!1,_,D=(y)=>{M.publish(G,"subagent.complete",{subagentSessionId:y.id,success:y.status==="completed",status:y.status,summary:y.result?.message?.slice(0,500),resumedFrom:y.resumedFrom,rootAgentId:y.rootAgentId,resumeDepth:y.resumeDepth})},Q;try{Q=X.resumeSubagent({agentId:N.req.param("agentId"),prompt:T.data.prompt,onEvent:(y,l)=>{qx(G,l,y)},onCompleted:(y)=>{if(U)D(y);else _=y}})}catch(y){if(y instanceof Error&&(y.message.startsWith("Cannot resume")||y.message.startsWith("Subagent cannot")))throw new I(y.message);if(y instanceof Error&&y.message.startsWith("Subagent not found"))throw new z(y.message);throw y}if(M.publish(G,"subagent.start",{subagentSessionId:Q.session.id,type:Q.session.subagentType,description:Q.session.description,resumedFrom:Q.source.id,rootAgentId:Q.session.rootAgentId,resumeDepth:Q.session.resumeDepth}),U=!0,_)D(_);return N.json({source:yS(Q.source),session:yS(Q.session)})})}),S.get("/:sessionId/goal",async(N)=>{let T=await Z(N.req.param("sessionId"),N.req.query("projectPath"));return N.json({goal:await new XO(T.projectPath,T.id).get()})}),S.put("/:sessionId/goal",async(N)=>{let T=t(Kx,await N.req.json());if(!T.success)throw new m("Invalid goal request");let J=await Z(N.req.param("sessionId"),N.req.query("projectPath")),G=v(J);return A(G).runExclusive(async()=>{let p=J.currentRunId?C.get(J.currentRunId):void 0;if(p&&(p.status==="running"||p.status==="waiting_permission"))return N.json({status:"rejected",reason:"run_active"},409);let U=await(await Y(J)).createGoal(T.data);M.publish(G,"goal.updated",{goal:U});let _=T.data.permissionMode??"default",D=w(J,"",_,{goalContinuationOnly:!0});return N.json({status:"running",runId:D.id,goal:U},202)})}),S.patch("/:sessionId/goal",async(N)=>{let T=t(Wx,await N.req.json());if(!T.success)throw new m("Invalid goal update");let J=await Z(N.req.param("sessionId"),N.req.query("projectPath")),G=v(J);return A(G).runExclusive(async()=>{let p=await Y(J),X;if(T.data.action==="pause")X=await p.pauseGoal();else if(T.data.action==="edit")X=await p.editGoal(T.data.objective);else X=await p.resumeGoal();if(M.publish(G,"goal.updated",{goal:X}),X.status==="active"&&(!J.currentRunId||!["running","waiting_permission"].includes(C.get(J.currentRunId)?.status??""))){let U=w(J,"","default",{goalContinuationOnly:!0});return N.json({status:"running",runId:U.id,goal:X},202)}return N.json({status:X.status,goal:X})})}),S.delete("/:sessionId/goal",async(N)=>{let T=await Z(N.req.param("sessionId"),N.req.query("projectPath")),J=v(T),p=await(await Y(T)).clearGoal();if(p)M.publish(J,"goal.cleared",{});return N.json({cleared:p})}),S.delete("/:sessionId",async(N)=>{let T=N.req.param("sessionId"),J=N.req.query("projectPath");try{let G=await n(T,J),p=q(G),X=R.get(p),U=x.get(p),_;if(X?.currentRunId){let D=C.get(X.currentRunId);if(D)_S(D),await D.completion,_=D.id}if(U)await U.clearGoal().catch((D)=>{H.warn("[SessionRoutes] Failed to clear session goal:",D)});if(await F.deleteSession(G.sessionId,G.projectPath),_)C.delete(_);if(R.delete(p),$.delete(p),j.delete(p),V.delete(p),U){if(await U.dispose(),x.delete(p),x.size===0)await e.getInstance().disconnectAll()}return N.json({success:!0})}catch(G){if(H.error("[SessionRoutes] Failed to delete session:",G),G instanceof m||G instanceof z||G instanceof o)throw G;throw new u("Failed to delete session")}}),S.get("/:sessionId/message",async(N)=>{let T=N.req.param("sessionId");try{let J=await n(T,N.req.query("projectPath")),G=R.get(q(J)),p=G?.messages?G.messages:await F.loadSession(J.sessionId,J.projectPath);return N.json(p)}catch(J){if(H.error("[SessionRoutes] Failed to get messages:",J),J instanceof m||J instanceof z||J instanceof o)throw J;throw new u("Failed to get messages")}}),S.get("/:sessionId/events",async(N)=>{let T=N.req.param("sessionId"),J=await n(T,N.req.query("projectPath")),G=await f(J);return mx(N,async(p)=>{let U,_,D=!1,Q=()=>{if(_!==void 0)clearInterval(_),_=void 0;U?.(),U=void 0},y=()=>{if(D)return;D=!0,Q()},l=new Set;p.onAbort(y),U=M.subscribe((k)=>{if(k.sessionId!==J.sessionId||k.projectPath!==J.projectPath)return;let d=k.properties.requestId;if((k.type==="permission.asked"||k.type==="question.required")&&typeof d==="string"){if(l.has(d))return;l.add(d)}p.writeSSE({data:JSON.stringify({type:k.type,properties:{...k.properties,sessionId:k.sessionId,projectPath:k.projectPath}})}).catch(y)});try{if(p.aborted||D)return;if(await p.writeSSE({data:JSON.stringify({type:"connected",properties:{sessionId:J.sessionId,projectPath:J.projectPath,timestamp:Date.now()}})}).catch((P)=>{throw y(),P}),p.aborted||D)return;let d=(G.currentRunId?C.get(G.currentRunId):void 0)?.pendingPermission;if(d){l.add(d.permissionId);let P=hO(d,!0);await p.writeSSE({data:JSON.stringify({type:P.type,properties:{...P.properties,sessionId:J.sessionId,projectPath:J.projectPath}})})}K(G).catch((P)=>{H.error(`[SessionRoutes] Failed to resume pending input for ${T}:`,P)}),_=setInterval(()=>{if(!p.aborted)p.writeSSE({data:JSON.stringify({type:"heartbeat",properties:{timestamp:Date.now()}})}).catch(y)},15000);while(!p.aborted&&!D)await new Promise((P)=>setTimeout(P,1000))}finally{y()}})}),S.post("/:sessionId/message",async(N)=>{let T=N.req.param("sessionId"),J=await N.req.json(),G=t(Bx,J);if(!G.success)throw new m("Invalid message format");let{content:p,attachments:X,permissionMode:U,projectPath:_}=G.data,D=U||"default",Q=fO(p,X),y=await Z(T,_??N.req.query("projectPath")),l=v(y);return A(l).runExclusive(async()=>{let k=y.currentRunId?C.get(y.currentRunId):void 0;if(k&&(k.status==="running"||k.status==="waiting_permission")){let a=await(await Y(y)).enqueueSteering(Q,{allowBeforeTurn:!0});if(!a.accepted)return N.json({status:"rejected",reason:a.reason??"turn_unavailable"},409);let AS=a.messageId??VS(12),lS=a.queued;M.publish(l,"message.created",{messageId:AS,role:"user",content:LS(Q)});let nO=a.delivery==="next_turn"?"follow_up.queued":"steering.queued";if(M.publish(l,nO,{runId:k.id,messageId:AS,queued:lS}),a.delivery==="next_turn"&&k.status!=="running"&&k.status!=="waiting_permission")K(y).catch((rO)=>{H.error(`[SessionRoutes] Failed to wake queued follow-up for ${T}:`,rO)});if(a.delivery==="next_turn")k.pendingFollowUpRequested=!0;return N.json({runId:k.id,messageId:AS,status:a.delivery==="next_turn"?"follow_up_queued":"steering_queued",queued:lS},202)}let P=await(await Y(y)).prepareInputTurn(Q);if(!P.accepted)return N.json({status:"rejected",reason:P.reason},P.reason==="queue_full"?429:409);let ZS=w(y,Q,D,{preparedInputTurn:P});return N.json({runId:ZS.id,messageId:P.messageId,status:"running"},202)})}),S.post("/:sessionId/abort",async(N)=>{let T=N.req.param("sessionId"),J=await n(T,N.req.query("projectPath")),G=R.get(q(J));if(G?.currentRunId){let p=C.get(G.currentRunId);if(p)_S(p),await p.completion}return N.json({success:!0})}),S.get("/:sessionId/status",async(N)=>{let T=N.req.param("sessionId"),J=await n(T,N.req.query("projectPath")),G=R.get(q(J));if(!G?.currentRunId)return N.json({sessionId:T,projectPath:J.projectPath,status:"idle"});let p=C.get(G.currentRunId);return N.json({sessionId:T,projectPath:J.projectPath,runId:G.currentRunId,status:p?.status||"idle"})}),{app:S,dispatchTask:b,getTaskDiff:B}};async function Lx(S,x,j,$,V,A={}){let{abortController:Y,sessionId:f,id:Z}=S,w=A.preparedInputTurn?.messageId??VS(12),B=A.pendingInputOnly===!0||A.preparedInputTurn?.mode==="pending"||A.goalContinuationOnly===!0?void 0:VS(12),K,N=v(x),T=(J,G)=>{M.publish(N,J,G)};try{if(x.taskStatus="running",x.taskStatusReason=void 0,x.taskStartedAt=new Date().toISOString(),x.taskCompletedAt=void 0,!A.pendingInputOnly&&!A.goalContinuationOnly)T("message.created",{messageId:w,role:"user",content:LS(j)});if(T("session.status",{status:"running"}),B)T("message.created",{messageId:B,role:"assistant",content:""});K=await V(x);let J=K,G=await mO.createWithRuntime(J,{sessionId:f,...x.taskWorktree?{toolBlacklist:["EnterWorktree","ExitWorktree"]}:{}}),p=async(Q)=>{let y=VS(12),l=300000;S.status="waiting_permission";let k=new Promise((MS)=>{let a=setTimeout(()=>{H.warn(`[SessionRoutes] Permission ${y} timed out after 300000ms`),T("permission.timeout",{requestId:y}),MS({approved:!1,reason:"timeout"})},300000);S.pendingPermission={permissionId:y,resolve:(AS)=>{clearTimeout(a),MS(AS)},details:Q}}),d=S.pendingPermission;if(!d)throw Error("Permission request was not registered");let P=hO(d);T(P.type,P.properties),H.info(`[SessionRoutes] Permission request created: ${y}, runId: ${Z}`);let ZS=await k;if(H.info(`[SessionRoutes] Permission response received: ${y}, approved: ${ZS.approved}`),!Y.signal.aborted)S.status="running";if(S.pendingPermission===d)S.pendingPermission=void 0;return ZS},X={messages:[...x.messages],userId:"web-user",sessionId:f,workspaceRoot:x.projectPath,signal:Y.signal,permissionMode:$,...x.taskWorktree?{worktreeActive:!0}:{},confirmationHandler:{requestConfirmation:p}},U=()=>{if(!B)B=VS(12),T("message.created",{messageId:B,role:"assistant",content:""});return B},_=async(Q)=>{switch(Q.kind){case"content_delta":T("message.delta",{messageId:U(),delta:Q.delta});break;case"thinking_delta":T("thinking.delta",{delta:Q.delta});break;case"tool_start":if("function"in Q.toolCall)T("tool.start",{messageId:U(),toolName:Q.toolCall.function.name,toolCallId:Q.toolCall.id,arguments:Q.toolCall.function.arguments,toolKind:Q.toolKind});break;case"tool_result":if("function"in Q.toolCall)T("tool.result",{messageId:U(),toolName:Q.toolCall.function.name,toolCallId:Q.toolCall.id,success:Q.result.success,summary:Q.result.metadata?.summary,output:CS(FS(Q.toolCall.function.name,Q.result)),metadata:BO(Q.result.metadata)});break;case"token_usage":T("token.usage",{...Q.usage});break;case"turn_start":T("turn.started",{turn:Q.turn,maxTurns:Q.maxTurns});break;case"steering_applied":T("steering.applied",{runId:Z,messageIds:Q.messageIds,count:Q.count,recovered:Q.recovered,delivery:Q.delivery,queued:J.getPendingSteeringCount()});break;case"follow_up_started":{if(B)T("message.complete",{messageId:B}),B=void 0;for(let y of Q.messages){if(!y.recovered||y.persisted)continue;T("message.created",{messageId:y.id,role:"user",content:LS(y.content),recovered:!0})}T("follow_up.started",{runId:Z,queued:Q.queued,recovered:Q.recovered}),U();break}case"goal_updated":T("goal.updated",{goal:Q.goal});break;case"goal_continuation_started":T("goal.continuation.started",{goal:Q.goal,continuation:Q.continuation}),U();break;case"compaction":T(Q.phase==="start"?"compaction.started":"compaction.completed",{});break;case"model_fallback":T("model.fallback",{});break;case"task_update":T("task.updated",{tasks:Q.tasks});break;default:break}},D=await ES(G.chatStream(j,X,{stream:!0,pendingInputOnly:A.pendingInputOnly,preparedInputTurn:A.preparedInputTurn,goalContinuationOnly:A.goalContinuationOnly}),_);if(!D.success)throw Error(D.error?.message??"Agent run failed");for(let Q=0;Q<20;Q++){let y=S.pendingFollowUpRequested===!0;if(S.pendingFollowUpRequested=!1,J.getPendingSteeringCount()===0){if(!y)break;continue}if(Y.signal.aborted)break;if(D=await ES(G.chatStream("",X,{stream:!0,pendingInputOnly:!0}),_),!D.success)throw Error(D.error?.message??"Agent follow-up failed")}if(x.messages=[...X.messages],x.updatedAt=new Date,await UO(x),Y.signal.aborted||S.status==="cancelled"){x.taskStatus="cancelled",x.taskCompletedAt??=new Date().toISOString(),T("session.status",{status:"idle"});return}if(B)T("message.complete",{messageId:B});T("thinking.completed",{}),S.status="completed",x.taskStatus="completed",x.taskCompletedAt??=new Date().toISOString(),T("session.completed",{runId:Z,outputTruncated:D.metadata?.outputTruncated??!1}),T("session.status",{status:"idle"})}catch(J){if(K&&A.preparedInputTurn)await K.finishTurn(A.preparedInputTurn.handle).catch(()=>{return});if(await UO(x).catch(()=>{return}),Y.signal.aborted||S.status==="cancelled"){_S(S,"runtime-abort"),x.taskStatus="cancelled",x.taskCompletedAt??=new Date().toISOString(),T("session.status",{status:"idle"});return}H.error("[SessionRoutes] Agent execution error:",J),S.status="failed",x.taskStatus="failed",x.taskStatusReason??="Agent execution failed",x.taskCompletedAt??=new Date().toISOString(),T("session.error",{error:J instanceof Error?J.message:"Unknown error"}),T("session.status",{status:"error"})}}function HO(S,x,j){H.info(`[SessionRoutes] Looking for permission ${x} in session ${S.sessionId}`),H.info(`[SessionRoutes] Active runs: ${C.size}`);for(let[$,V]of C.entries())if(H.info(`[SessionRoutes] Checking run ${$}: sessionId=${V.sessionId}, projectPath=${V.projectPath}, pendingPermission=${V.pendingPermission?.permissionId}`),V.sessionId===S.sessionId&&V.projectPath===S.projectPath&&V.pendingPermission?.permissionId===x)return V.pendingPermission.resolve(j),H.info(`[SessionRoutes] Permission ${x} responded, runId: ${V.id}`),!0;return H.error(`[SessionRoutes] Permission not found: ${x}`),!1}var PS=W("Service"),Cx=O.Object({approved:O.Boolean(),remember:O.Optional(O.Boolean()),scope:O.Optional(E(["once","session","project"])),targetMode:O.Optional(E(["default","autoEdit","plan","yolo"])),feedback:O.Optional(O.String()),answers:O.Optional(O.Record(O.String(),O.Union([O.String(),O.Array(O.String())])))}),KO=()=>{let S=new Fx;return S.post("/:permissionId",async(x)=>{let j=x.req.param("permissionId"),$=x.req.query("sessionId"),V=x.req.query("projectPath");PS.info(`[PermissionRoutes] Received permission response: permissionId=${j}, sessionId=${$}`);try{let A=await x.req.json(),Y=t(Cx,A);if(!Y.success)throw new m("Invalid permission response format");let{approved:f,remember:Z,scope:w,targetMode:b,feedback:B,answers:K}=Y.data;if(!$)throw new m("sessionId query parameter is required");let N=await n($,V);if(!HO(N,j,{approved:f,reason:B,scope:w,targetMode:b,feedback:B,answers:K}))throw new z("Permission request",j);return PS.info(`[PermissionRoutes] Permission ${j} ${f?"approved":"denied"}`),x.json({success:!0,approved:f,remember:Z})}catch(A){if(PS.error("[PermissionRoutes] Failed to respond to permission:",A),A instanceof m||A instanceof o||A instanceof z||A instanceof g)throw A;throw A}}),S};import{Hono as Px}from"hono";var WO=W("Service"),qO=()=>{let S=new Px;return S.get("/",async(x)=>{try{let j=await kO();return x.json(j)}catch(j){return WO.error("[ProviderRoutes] Failed to list providers:",j),x.json([])}}),S.get("/:provider/models",async(x)=>{let j=x.req.param("provider");try{let $=await EO(j);return x.json($)}catch($){return WO.error("[ProviderRoutes] Failed to list models:",$),x.json([])}}),S.put("/:provider/credential",async(x)=>{let j=x.req.param("provider"),$=await x.req.json();if(!$.apiKey?.trim())throw new m("apiKey is required");return await s().setApiKey(j,$.apiKey.trim()),x.json({success:!0})}),S.delete("/:provider/credential",async(x)=>{return await s().clearCredential(x.req.param("provider")),x.json({success:!0})}),S};import{Hono as Rx}from"hono";import*as SS from"node:fs/promises";import{homedir as Ix}from"node:os";import*as mS from"node:path";var xS=W("Service"),RS=mS.join(Ix(),".blade","skills-config.json");async function LO(){try{let S=await SS.readFile(RS,"utf-8");return JSON.parse(S)}catch{return{disabled:[]}}}async function gx(S){await SS.mkdir(mS.dirname(RS),{recursive:!0}),await SS.writeFile(RS,JSON.stringify(S,null,2))}async function tx(S,x){let j=await LO();if(x)j.disabled=j.disabled.filter(($)=>$!==S);else if(!j.disabled.includes(S))j.disabled.push(S);await gx(j)}var JS=null,ux=300000;async function vx(){if(JS&&Date.now()-JS.timestamp<ux)return JS.data;try{let S=await fetch("https://api.github.com/repos/anthropics/skills/contents/skills",{headers:{Accept:"application/vnd.github.v3+json","User-Agent":"Blade-Skills-Catalog"}});if(!S.ok)return xS.warn(`GitHub API returned ${S.status}`),JS?.data||[];let x=await S.json(),j=[];for(let $ of x){if($.type!=="dir")continue;let V=`Official ${$.name} skill`;try{let A=await fetch(`https://raw.githubusercontent.com/anthropics/skills/main/skills/${$.name}/SKILL.md`,{headers:{"User-Agent":"Blade-Skills-Catalog"}});if(A.ok){let f=(await A.text()).match(/^#[^\n]*\n+([^\n#]+)/);if(f)V=f[1].trim().slice(0,100)}}catch{}j.push({name:$.name,description:V,tag:"Official",author:"Anthropic"})}return JS={data:j,timestamp:Date.now()},j}catch(S){return xS.warn("Failed to fetch skills catalog from GitHub:",S),JS?.data||[]}}var FO=()=>{let S=new Rx;return S.get("/",async(x)=>{try{let j=DS();await j.initialize();let $=j.getAll(),V=await LO(),A=$.map((Y)=>({id:Y.name,name:Y.name,enabled:!V.disabled.includes(Y.name),description:Y.description||"",version:Y.version||"1.0.0",provider:"Local",location:Y.source==="builtin"?"Built-in":Y.basePath,capabilities:["prompts"],allowedTools:Y.allowedTools||[]}));return x.json(A)}catch(j){return xS.error("[SkillsRoutes] Failed to get skills:",j),x.json([])}}),S.post("/:name/toggle",async(x)=>{try{let j=x.req.param("name"),$=await x.req.json();return await tx(j,$.enabled),x.json({success:!0,enabled:$.enabled})}catch(j){return xS.error("[SkillsRoutes] Failed to toggle skill:",j),x.json({success:!1,error:j.message},500)}}),S.delete("/:name",async(x)=>{try{let j=x.req.param("name"),$=`${process.env.HOME}/.blade/skills/${j}`;return await SS.rm($,{recursive:!0,force:!0}),await DS().refresh(),x.json({success:!0})}catch(j){return xS.error("[SkillsRoutes] Failed to uninstall skill:",j),x.json({success:!1,error:j.message},500)}}),S.post("/install",async(x)=>{try{let j=await x.req.json(),$=zO(),V=DS(),A=!1;if(j.source==="catalog"&&j.name){if(A=await $.installOfficialSkill(j.name),!A)return x.json({success:!1,error:"Failed to install skill from catalog"},500)}else if(j.source==="repo"&&j.url){if(A=await $.installFromRepo(j.url,j.name),!A)return x.json({success:!1,error:"Failed to install skill from repository. Make sure the repo contains a SKILL.md file."},500)}else if(j.source==="local"&&j.path){if(A=await $.installFromLocal(j.path,j.name),!A)return x.json({success:!1,error:"Failed to install skill from local path. Make sure the path exists and contains a SKILL.md file."},500)}else return x.json({success:!1,error:"Invalid install parameters. Required: source with corresponding url/path/name"},400);return await V.refresh(),x.json({success:!0})}catch(j){return xS.error("[SkillsRoutes] Failed to install skill:",j),x.json({success:!1,error:j.message},500)}}),S.get("/catalog",async(x)=>{try{let j=await vx();return x.json(j)}catch(j){return xS.error("[SkillsRoutes] Failed to get catalog:",j),x.json([])}}),S};import cx from"fast-glob";import{Hono as lx}from"hono";import{execSync as dx}from"node:child_process";import{readdir as ax}from"node:fs/promises";import bS from"node:path";var QS=W("Service"),ox=(S)=>{try{return dx("git rev-parse --abbrev-ref HEAD",{cwd:S,encoding:"utf-8",stdio:["pipe","pipe","pipe"]}).trim()||null}catch{return null}},ix=[...hS.map((S)=>`${S}/**`),...hS,...IS.map((S)=>`**/${S}`)],nx=new Set(["/theme","/model","/permissions","/mcp","/skills","/exit","/resume","/ide","/login","/logout"]),RO=()=>{let S=new lx;return S.get("/commands",async(x)=>{try{let j=x.req.query("q")||"",$=PO(j).filter((V)=>!nx.has(V.command));return x.json($)}catch(j){return QS.error("[SuggestionsRoutes] Failed to get command suggestions:",j),x.json([])}}),S.get("/files",async(x)=>{try{let j=x.req.query("q")||"",$=x.get("directory")||L(),V=Math.min(Number(x.req.query("limit"))||100,1000),Y=(await cx("**/*",{cwd:$,dot:!1,followSymbolicLinks:!1,onlyFiles:!1,markDirectories:!0,unique:!0,ignore:ix})).map((w)=>w.replace(/\\/g,"/"));if(!j)return x.json(Y.slice(0,V));let f=j.toLowerCase(),Z=Y.filter((w)=>w.toLowerCase().includes(f)).slice(0,V);return x.json(Z)}catch(j){return QS.error("[SuggestionsRoutes] Failed to get file suggestions:",j),x.json([])}}),S.get("/files/tree",async(x)=>{try{let j=x.get("directory")||L(),$=x.req.query("path")||"",V=$?bS.join(j,$):j,Y=(await ax(V,{withFileTypes:!0})).map((w)=>({name:w.name,path:$?`${$}/${w.name}`:w.name,type:w.isDirectory()?"dir":"file"}));Y.sort((w,b)=>{if(w.type!==b.type)return w.type==="dir"?-1:1;return w.name.localeCompare(b.name)});let f=new Set(hS),Z=Y.filter((w)=>{let b=w.name;if(b.startsWith("."))return!1;if(f.has(b))return!1;for(let B of IS)if(b.match(new RegExp(B.replace("*",".*"))))return!1;return!0});return x.json(Z)}catch(j){return QS.error("[SuggestionsRoutes] Failed to get file tree:",j),x.json([])}}),S.get("/files/content",async(x)=>{try{let j=x.req.query("path");if(!j)return x.json({error:"Missing file path"},400);let $=x.get("directory")||L(),V=bS.resolve($,j),A=bS.relative($,V);if(A.startsWith("..")||bS.isAbsolute(A))return x.json({error:"Invalid file path"},400);let Y=CO(),f=await Y.stat(V);if(!f?.isFile)return x.json({error:"File not found"},404);let Z=200000,w=await Y.readTextFile(V),b=w.length>Z;return x.json({path:j,content:b?w.slice(0,Z):w,truncated:b,size:f.size})}catch(j){return QS.error("[SuggestionsRoutes] Failed to get file content:",j),x.json({error:"Failed to read file"},500)}}),S.get("/git-info",async(x)=>{try{let j=x.get("directory")||L(),$=ox(j);return x.json({branch:$})}catch(j){return QS.error("[SuggestionsRoutes] Failed to get git info:",j),x.json({branch:null})}}),S};import{Hono as rx}from"hono";var gS=W("Service"),IO=(S)=>{let x=new rx;return x.onError((j,$)=>{if(j instanceof g)return $.json(j.toObject(),j.statusCode);return gS.error("[TaskRoutes] Unhandled route error:",j),$.json(new u("Failed to dispatch task").toObject(),500)}),x.post("/",async(j)=>{let $;try{$=await j.req.json()}catch{throw new m("Invalid request body")}let V=t(pO,$);if(!V.success)throw new m("Invalid task request");try{let A=await S.dispatchTask({prompt:V.data.prompt,title:V.data.title,sourceProjectPath:V.data.projectPath||j.get("directory")||L(),isolation:V.data.isolation,permissionMode:V.data.permissionMode,attachments:V.data.attachments});return j.json(GO.parse(A),202)}catch(A){if(A instanceof m||A instanceof I)throw A;throw gS.error("[TaskRoutes] Failed to dispatch task:",A),new u("Failed to dispatch task")}}),x.get("/:sessionId/diff",async(j)=>{try{let $=await S.getTaskDiff(j.req.param("sessionId"),j.req.query("projectPath"));return j.json($O.parse($))}catch($){if($ instanceof m||$ instanceof z||$ instanceof I)throw $;throw gS.error("[TaskRoutes] Failed to load task diff:",$),new u("Failed to load task diff")}}),x};import{Hono as ex}from"hono";var OS=W("Service"),TS=new Map,gO=2097152,tO=65536;function uO(){return typeof globalThis.Bun<"u"}async function sx(S,x,j){if(uO()){let{spawn:A}=await import("bun-pty"),Y=A(S,x,{name:"xterm-256color",cwd:j.cwd,env:j.env});return{pid:Y.pid,write:(f)=>Y.write(f),resize:(f,Z)=>Y.resize(f,Z),kill:()=>Y.kill(),onData:(f)=>Y.onData(f),onExit:(f)=>Y.onExit(f)}}let V=(await import("node-pty")).spawn(S,x,{name:"xterm-256color",cwd:j.cwd,env:j.env,cols:80,rows:24});return{pid:V.pid,write:(A)=>V.write(A),resize:(A,Y)=>V.resize(A,Y),kill:()=>V.kill(),onData:(A)=>{V.onData(A)},onExit:(A)=>{V.onExit(A)}}}var vS=void 0,uS;if(uO())try{let{createBunWebSocket:S}=await import("hono/bun"),x=S();uS=x.upgradeWebSocket,vS=x.websocket}catch(S){OS.warn("[Terminal] Failed to initialize Bun WebSocket support:",S)}async function vO(S,x){let j=`term-${Date.now()}`;OS.info(`[Terminal] New connection: ${j}, cwd: ${S}`);let $=process.platform==="win32"?"powershell.exe":process.env.SHELL||"/bin/zsh",V=$.endsWith("sh")?["-l"]:[],A=await sx($,V,{cwd:S,env:{...process.env,TERM:"xterm-256color",COLORTERM:"truecolor"}}),Y={id:j,process:A,cwd:S,buffer:"",subscribers:new Set};if(TS.set(j,Y),Y.subscribers.add(x),A.onData((f)=>{let Z=!1;for(let w of Y.subscribers)try{w.send(f),Z=!0}catch{Y.subscribers.delete(w)}if(Z)return;if(Y.buffer+=f,Y.buffer.length>gO)Y.buffer=Y.buffer.slice(-gO)}),A.onExit(({exitCode:f})=>{OS.info(`[Terminal] Process exited: ${j}, code: ${f}`);for(let Z of Y.subscribers)try{Z.close()}catch{}TS.delete(j)}),Y.buffer){let f=Y.buffer;Y.buffer="";for(let Z=0;Z<f.length;Z+=tO)x.send(f.slice(Z,Z+tO))}return{session:Y,cleanup:()=>{if(OS.info(`[Terminal] Connection closed: ${j}`),Y.subscribers.delete(x),Y.subscribers.size===0){try{Y.process.kill()}catch{}TS.delete(j)}}}}function cO(S,x){S.on("connection",async(j,$)=>{let V=new URL($.url||"/",`http://${$.headers.host}`);if(!V.pathname.endsWith("/terminal/ws")){j.close();return}let A=V.searchParams.get("cwd")||x()||L();try{let{session:Y,cleanup:f}=await vO(A,{send:(Z)=>j.send(Z),close:()=>j.close()});j.on("message",(Z)=>{try{Y.process.write(String(Z))}catch(w){OS.error("[Terminal] Failed to write to PTY:",w)}}),j.on("close",f),j.on("error",f)}catch(Y){OS.error("[Terminal] Failed to spawn PTY:",Y),j.close()}})}var lO=()=>{let S=new ex;if(S.get("/status",(x)=>{let j=Array.from(TS.values()).map(($)=>({id:$.id,cwd:$.cwd}));return x.json({active:j.length,terminals:j})}),S.post("/kill/:terminalId",(x)=>{let j=x.req.param("terminalId"),$=TS.get(j);if($){try{$.process.kill()}catch{}for(let V of $.subscribers)V.close();return TS.delete(j),x.json({success:!0})}return x.json({success:!1,error:"Terminal not found"},404)}),uS)S.get("/ws",uS((x)=>{let j=x.req.query("cwd")||x.get("directory")||L(),$;return{async onOpen(V,A){try{$=await vO(j,{send:(Y)=>A.send(Y),close:()=>A.close()})}catch(Y){OS.error("[Terminal] Failed to spawn PTY:",Y),A.close()}},onMessage(V){if(!$)return;try{$.session.process.write(String(V.data))}catch(A){OS.error("[Terminal] Failed to write to PTY:",A)}},onClose(){$?.cleanup()}}}));else S.get("/ws",(x)=>{return x.json({message:"WebSocket endpoint - connect via ws:// protocol",hint:"Use WebSocket client to connect to this endpoint"})});return S};var c=W("Service"),cS=[];function T0(){let S=x0($0(import.meta.url)),x=[jS(S,"web"),jS(L(),"dist/web"),jS(L(),"packages/cli/dist/web")];for(let j of x){let $=jS(j,"index.html");if(oO($)){if(BS($,"utf-8").includes("/assets/"))return c.debug(`[Server] Found web dist at: ${j}`),j}}return null}function A0(){let S=new N0;S.onError(($,V)=>{if(c.error("[Server] Request error:",$),$ instanceof g)return V.json($.toObject(),$.statusCode);let A=$ instanceof Error?$.message:String($);return V.json({error:{code:"INTERNAL_ERROR",message:A}},500)}),S.use(async($,V)=>{let A=process.env.BLADE_SERVER_PASSWORD;if(!A)return V();let Y=$.req.path;if(Y==="/"||Y.startsWith("/assets/")||Y.endsWith(".html")||Y.endsWith(".js")||Y.endsWith(".css"))return V();let f=$.req.header("Authorization");if(!f?.startsWith("Basic "))return $.header("WWW-Authenticate",'Basic realm="Blade Server"'),$.json({error:{code:"UNAUTHORIZED",message:"Authentication required"}},401);let Z=Buffer.from(f.slice(6),"base64").toString(),[w,b]=Z.split(":"),B=process.env.BLADE_SERVER_USERNAME??"blade";if(w!==B||b!==A)return $.json({error:{code:"UNAUTHORIZED",message:"Invalid credentials"}},401);return V()}),S.use(async($,V)=>{let A=$.req.path==="/health"||$.req.path==="/global/health"||$.req.path.startsWith("/assets/");if(!A)c.debug(`[Server] ${$.req.method} ${$.req.path}`);let Y=Date.now();if(await V(),!A){let f=Date.now()-Y;c.debug(`[Server] ${$.req.method} ${$.req.path} - ${$.res.status} (${f}ms)`)}}),S.use(V0({origin($){if(!$)return;if($.startsWith("http://localhost:"))return $;if($.startsWith("http://127.0.0.1:"))return $;if($==="tauri://localhost"||$==="http://tauri.localhost")return $;if(cS.includes($))return $;return}})),S.use(async($,V)=>{let A=$.req.query("directory")||$.req.header("x-blade-directory")||L();try{A=decodeURIComponent(A)}catch{}return $.set("directory",A),V()});let x=MO();S.route("/global",iS()),S.route("/events",aS()),S.route("/sessions",x.app),S.route("/tasks",IO(x)),S.route("/configs",dS()),S.route("/permissions",KO()),S.route("/providers",qO()),S.route("/models",SO()),S.route("/suggestions",RO()),S.route("/terminal",lO()),S.route("/mcp",nS()),S.route("/skills",FO()),S.get("/health",($)=>{return $.json({healthy:!0,version:NS()})});let j=T0();if(j)c.info(`[Server] Serving static files from ${j}`),S.get("/assets/*",($)=>{let V=jS(j,$.req.path);if(!oO(V))return $.json({error:{code:"NOT_FOUND",message:`File not found: ${$.req.path}`}},404);let A=BS(V),Y=j0(V).toLowerCase(),Z={".js":"application/javascript",".css":"text/css",".html":"text/html",".json":"application/json",".png":"image/png",".jpg":"image/jpeg",".jpeg":"image/jpeg",".gif":"image/gif",".svg":"image/svg+xml",".ico":"image/x-icon",".woff":"font/woff",".woff2":"font/woff2",".ttf":"font/ttf",".eot":"application/vnd.ms-fontobject"}[Y]||"application/octet-stream";return new Response(A,{headers:{"Content-Type":Z,"Cache-Control":"public, max-age=31536000, immutable"}})}),S.get("/",($)=>{let V=jS(j,"index.html"),A=BS(V,"utf-8");return $.html(A)}),S.get("*",($)=>{let V=$.req.path;if(V.includes("."))return $.json({error:{code:"NOT_FOUND",message:`File not found: ${V}`}},404);let A=jS(j,"index.html"),Y=BS(A,"utf-8");return $.html(Y)});else c.warn('[Server] Web UI not found. Run "cd packages/cli/web && bun run build" to enable web interface.'),S.get("/",($)=>{return $.json({message:"Blade API Server",version:NS(),webUI:!1,hint:'Web UI not built. Run "cd packages/cli/web && bun run build" to enable.',endpoints:{health:"/health",sessions:"/sessions",configs:"/configs",permissions:"/permissions",providers:"/providers"}})}),S.all("*",($)=>{return $.json({error:{code:"NOT_FOUND",message:`Route not found: ${$.req.path}`}},404)});return S}function Y0(){let S=O0(),x=[];for(let j of Object.keys(S)){let $=S[j];if(!$)continue;for(let V of $){if(V.internal||V.family!=="IPv4")continue;if(V.address.startsWith("172."))continue;x.push(V.address)}}return x}function dO(){return typeof globalThis.Bun<"u"}function aO(S,x){let j=globalThis.Bun,$=(A)=>{try{return j.serve({hostname:x.hostname,port:A,fetch:S.fetch,idleTimeout:0,websocket:vS})}catch(Y){c.error("Failed to start Bun server:",Y);return}},V=x.port===0?$(4096)??$(0):$(x.port);if(!V)throw Error(`Failed to start Bun server on port ${x.port}`);return{url:V.url,port:V.port,hostname:V.hostname,stop:async()=>{V.stop(!0)}}}function p0(S,x){return new Promise((j,$)=>{let V=S0(async(w,b)=>{let B=new URL(w.url||"/",`http://${w.headers.host}`),K=new Headers;for(let[J,G]of Object.entries(w.headers))if(G)if(Array.isArray(G))for(let p of G)K.append(J,p);else K.set(J,G);let N;if(w.method!=="GET"&&w.method!=="HEAD")N=(await new Promise((G)=>{let p=[];w.on("data",(X)=>p.push(X)),w.on("end",()=>G(Buffer.concat(p)))})).toString();let T=new Request(B.toString(),{method:w.method,headers:K,body:N});try{let J=await S.fetch(T);if(b.statusCode=J.status,J.headers.forEach((G,p)=>{b.setHeader(p,G)}),J.body){let G=J.body.getReader(),p=async()=>{let{done:X,value:U}=await G.read();if(X){b.end();return}return b.write(U),p()};await p()}else{let G=await J.text();b.end(G)}}catch(J){c.error("[Server] Node request error:",J),b.statusCode=500,b.end(JSON.stringify({error:{code:"INTERNAL_ERROR",message:"Internal server error"}}))}}),A=new J0({noServer:!0}),Y=L();cO(A,()=>Y),V.on("upgrade",(w,b,B)=>{if(new URL(w.url||"/",`http://${w.headers.host}`).pathname==="/terminal/ws")A.handleUpgrade(w,b,B,(N)=>{A.emit("connection",N,w)});else b.destroy()});let f=(w)=>{return new Promise((b,B)=>{V.once("error",(K)=>{if(K.code==="EADDRINUSE")B(K);else B(K)}),V.listen(w,x.hostname,()=>{let K=V.address();if(K&&typeof K==="object")b(K.port);else b(w)})})};(async()=>{let w;if(x.port===0)try{w=await f(4096)}catch{w=await f(0)}else w=await f(x.port);let b=new URL(`http://${x.hostname==="0.0.0.0"?"localhost":x.hostname}:${w}`);j({url:b,port:w,hostname:x.hostname,stop:async()=>{return new Promise((B)=>{A.close(()=>{V.close(()=>B())})})}})})().catch($)})}var iO;((aj)=>{let S,x;function j(){if(!x)x=A0();return x}aj.getApp=j;function $(Y){cS=Y.cors??[];let f=j();if(dO())S=aO(f,Y),c.info(`[Server] Blade server listening on ${S.url} (Bun runtime)`);else throw Error("Blade web server requires Bun runtime. Please run with Bun: `bun run blade web` or install Bun from https://bun.sh");let Z=S;return{url:Z.url,port:Z.port,hostname:Z.hostname,stop:async()=>{if(S)await S.stop(),S=void 0,x=void 0,c.info("[Server] Blade server stopped")}}}aj.listen=$;async function V(Y){cS=Y.cors??[];let f=j();if(dO())S=aO(f,Y),c.info(`[Server] Blade server listening on ${S.url} (Bun runtime)`);else S=await p0(f,Y),c.info(`[Server] Blade server listening on ${S.url} (Node.js runtime)`);let Z=S;return{url:Z.url,port:Z.port,hostname:Z.hostname,stop:async()=>{if(S)await S.stop(),S=void 0,x=void 0,c.info("[Server] Blade server stopped")}}}aj.listenAsync=V;function A(){return S!==void 0}aj.isRunning=A})(iO||={});export{G0 as a,Q0 as b,Y0 as c,iO as d};