@agegr/pi-web 0.3.8 → 0.3.9

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 (121) hide show
  1. package/.next/BUILD_ID +1 -1
  2. package/.next/app-path-routes-manifest.json +3 -0
  3. package/.next/build-manifest.json +2 -2
  4. package/.next/required-server-files.js +1 -1
  5. package/.next/required-server-files.json +1 -1
  6. package/.next/routes-manifest.json +22 -0
  7. package/.next/server/app/_global-error/page.js +3 -3
  8. package/.next/server/app/_global-error/page_client-reference-manifest.js +1 -1
  9. package/.next/server/app/_global-error.html +1 -1
  10. package/.next/server/app/_global-error.rsc +1 -1
  11. package/.next/server/app/_global-error.segments/_full.segment.rsc +1 -1
  12. package/.next/server/app/_global-error.segments/_global-error/__PAGE__.segment.rsc +1 -1
  13. package/.next/server/app/_global-error.segments/_global-error.segment.rsc +1 -1
  14. package/.next/server/app/_global-error.segments/_head.segment.rsc +1 -1
  15. package/.next/server/app/_global-error.segments/_index.segment.rsc +1 -1
  16. package/.next/server/app/_global-error.segments/_tree.segment.rsc +1 -1
  17. package/.next/server/app/_not-found/page.js +2 -2
  18. package/.next/server/app/_not-found/page_client-reference-manifest.js +1 -1
  19. package/.next/server/app/_not-found.html +1 -1
  20. package/.next/server/app/_not-found.rsc +1 -1
  21. package/.next/server/app/_not-found.segments/_full.segment.rsc +1 -1
  22. package/.next/server/app/_not-found.segments/_head.segment.rsc +1 -1
  23. package/.next/server/app/_not-found.segments/_index.segment.rsc +1 -1
  24. package/.next/server/app/_not-found.segments/_not-found/__PAGE__.segment.rsc +1 -1
  25. package/.next/server/app/_not-found.segments/_not-found.segment.rsc +1 -1
  26. package/.next/server/app/_not-found.segments/_tree.segment.rsc +1 -1
  27. package/.next/server/app/api/agent/[id]/events/route.js +3 -3
  28. package/.next/server/app/api/agent/[id]/route.js +2 -2
  29. package/.next/server/app/api/agent/new/route.js +2 -2
  30. package/.next/server/app/api/auth/login/[provider]/route.js +3 -0
  31. package/.next/server/app/api/auth/login/[provider]/route.js.nft.json +1 -0
  32. package/.next/server/app/api/auth/login/[provider]/route_client-reference-manifest.js +1 -0
  33. package/.next/server/app/api/auth/logout/[provider]/route.js +1 -0
  34. package/.next/server/app/api/auth/logout/[provider]/route.js.nft.json +1 -0
  35. package/.next/server/app/api/auth/logout/[provider]/route_client-reference-manifest.js +1 -0
  36. package/.next/server/app/api/auth/providers/route.js +1 -0
  37. package/.next/server/app/api/auth/providers/route.js.nft.json +1 -0
  38. package/.next/server/app/api/auth/providers/route_client-reference-manifest.js +1 -0
  39. package/.next/server/app/api/default-cwd/route.js +1 -1
  40. package/.next/server/app/api/files/[...path]/route.js +2 -2
  41. package/.next/server/app/api/home/route.js +1 -1
  42. package/.next/server/app/api/models/route.js +2 -2
  43. package/.next/server/app/api/models/route.js.nft.json +1 -1
  44. package/.next/server/app/api/models-config/route.js +1 -1
  45. package/.next/server/app/api/sessions/[id]/context/route.js +2 -2
  46. package/.next/server/app/api/sessions/[id]/route.js +2 -2
  47. package/.next/server/app/api/sessions/new/route.js +1 -1
  48. package/.next/server/app/api/sessions/route.js +2 -2
  49. package/.next/server/app/api/skills/install/route.js +1 -1
  50. package/.next/server/app/api/skills/route.js +2 -2
  51. package/.next/server/app/api/skills/search/route.js +1 -1
  52. package/.next/server/app/favicon.ico/route.js +1 -1
  53. package/.next/server/app/index.html +1 -1
  54. package/.next/server/app/index.rsc +2 -2
  55. package/.next/server/app/index.segments/__PAGE__.segment.rsc +2 -2
  56. package/.next/server/app/index.segments/_full.segment.rsc +2 -2
  57. package/.next/server/app/index.segments/_head.segment.rsc +1 -1
  58. package/.next/server/app/index.segments/_index.segment.rsc +1 -1
  59. package/.next/server/app/index.segments/_tree.segment.rsc +1 -1
  60. package/.next/server/app/page.js +2 -2
  61. package/.next/server/app/page_client-reference-manifest.js +1 -1
  62. package/.next/server/app-paths-manifest.json +3 -0
  63. package/.next/server/chunks/573.js +1 -1
  64. package/.next/server/middleware-build-manifest.js +1 -1
  65. package/.next/server/pages/404.html +1 -1
  66. package/.next/server/pages/500.html +1 -1
  67. package/.next/static/X_3Dgu61a8CB6nStor2lA/_buildManifest.js +1 -0
  68. package/.next/static/chunks/app/_global-error/page-20395aed6b86fc74.js +1 -0
  69. package/.next/static/chunks/app/api/agent/[id]/events/route-20395aed6b86fc74.js +1 -0
  70. package/.next/static/chunks/app/api/agent/[id]/route-20395aed6b86fc74.js +1 -0
  71. package/.next/static/chunks/app/api/agent/new/route-20395aed6b86fc74.js +1 -0
  72. package/.next/static/chunks/app/api/auth/login/[provider]/route-20395aed6b86fc74.js +1 -0
  73. package/.next/static/chunks/app/api/auth/logout/[provider]/route-20395aed6b86fc74.js +1 -0
  74. package/.next/static/chunks/app/api/auth/providers/route-20395aed6b86fc74.js +1 -0
  75. package/.next/static/chunks/app/api/default-cwd/route-20395aed6b86fc74.js +1 -0
  76. package/.next/static/chunks/app/api/files/[...path]/route-20395aed6b86fc74.js +1 -0
  77. package/.next/static/chunks/app/api/home/route-20395aed6b86fc74.js +1 -0
  78. package/.next/static/chunks/app/api/models/route-20395aed6b86fc74.js +1 -0
  79. package/.next/static/chunks/app/api/models-config/route-20395aed6b86fc74.js +1 -0
  80. package/.next/static/chunks/app/api/sessions/[id]/context/route-20395aed6b86fc74.js +1 -0
  81. package/.next/static/chunks/app/api/sessions/[id]/route-20395aed6b86fc74.js +1 -0
  82. package/.next/static/chunks/app/api/sessions/new/route-20395aed6b86fc74.js +1 -0
  83. package/.next/static/chunks/app/api/sessions/route-20395aed6b86fc74.js +1 -0
  84. package/.next/static/chunks/app/api/skills/install/route-20395aed6b86fc74.js +1 -0
  85. package/.next/static/chunks/app/api/skills/route-20395aed6b86fc74.js +1 -0
  86. package/.next/static/chunks/app/api/skills/search/route-20395aed6b86fc74.js +1 -0
  87. package/.next/static/chunks/app/{page-8f53109554288387.js → page-77ca0b65b114cc74.js} +1 -1
  88. package/.next/static/chunks/next/dist/client/components/builtin/app-error-20395aed6b86fc74.js +1 -0
  89. package/.next/static/chunks/next/dist/client/components/builtin/forbidden-20395aed6b86fc74.js +1 -0
  90. package/.next/static/chunks/next/dist/client/components/builtin/not-found-20395aed6b86fc74.js +1 -0
  91. package/.next/static/chunks/next/dist/client/components/builtin/unauthorized-20395aed6b86fc74.js +1 -0
  92. package/.next/trace +4 -3
  93. package/.next/trace-build +1 -1
  94. package/.next/types/app/api/auth/login/[provider]/route.ts +351 -0
  95. package/.next/types/app/api/auth/logout/[provider]/route.ts +351 -0
  96. package/.next/types/app/api/auth/providers/route.ts +351 -0
  97. package/.next/types/routes.d.ts +4 -1
  98. package/.next/types/validator.ts +27 -0
  99. package/package.json +1 -1
  100. package/.next/static/chunks/app/_global-error/page-60ef9a2f388b61b3.js +0 -1
  101. package/.next/static/chunks/app/api/agent/[id]/events/route-60ef9a2f388b61b3.js +0 -1
  102. package/.next/static/chunks/app/api/agent/[id]/route-60ef9a2f388b61b3.js +0 -1
  103. package/.next/static/chunks/app/api/agent/new/route-60ef9a2f388b61b3.js +0 -1
  104. package/.next/static/chunks/app/api/default-cwd/route-60ef9a2f388b61b3.js +0 -1
  105. package/.next/static/chunks/app/api/files/[...path]/route-60ef9a2f388b61b3.js +0 -1
  106. package/.next/static/chunks/app/api/home/route-60ef9a2f388b61b3.js +0 -1
  107. package/.next/static/chunks/app/api/models/route-60ef9a2f388b61b3.js +0 -1
  108. package/.next/static/chunks/app/api/models-config/route-60ef9a2f388b61b3.js +0 -1
  109. package/.next/static/chunks/app/api/sessions/[id]/context/route-60ef9a2f388b61b3.js +0 -1
  110. package/.next/static/chunks/app/api/sessions/[id]/route-60ef9a2f388b61b3.js +0 -1
  111. package/.next/static/chunks/app/api/sessions/new/route-60ef9a2f388b61b3.js +0 -1
  112. package/.next/static/chunks/app/api/sessions/route-60ef9a2f388b61b3.js +0 -1
  113. package/.next/static/chunks/app/api/skills/install/route-60ef9a2f388b61b3.js +0 -1
  114. package/.next/static/chunks/app/api/skills/route-60ef9a2f388b61b3.js +0 -1
  115. package/.next/static/chunks/app/api/skills/search/route-60ef9a2f388b61b3.js +0 -1
  116. package/.next/static/chunks/next/dist/client/components/builtin/app-error-60ef9a2f388b61b3.js +0 -1
  117. package/.next/static/chunks/next/dist/client/components/builtin/forbidden-60ef9a2f388b61b3.js +0 -1
  118. package/.next/static/chunks/next/dist/client/components/builtin/not-found-60ef9a2f388b61b3.js +0 -1
  119. package/.next/static/chunks/next/dist/client/components/builtin/unauthorized-60ef9a2f388b61b3.js +0 -1
  120. package/.next/static/iYZ8zDYTOujyV-5pZNJR7/_buildManifest.js +0 -1
  121. /package/.next/static/{iYZ8zDYTOujyV-5pZNJR7 → X_3Dgu61a8CB6nStor2lA}/_ssgManifest.js +0 -0
@@ -1 +1 @@
1
- (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[974],{5597:(e,t,r)=>{Promise.resolve().then(r.bind(r,6482))},6482:(e,t,r)=>{"use strict";r.d(t,{AppShell:()=>e$});var n=r(5155),o=r(2115),i=r(3321);let l="var(--text-dim)";function s({size:e=14,open:t=!1}){return t?(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:[(0,n.jsx)("path",{d:"M1 4.5A1 1 0 0 1 2 3.5H5.5L7 5h7.5v1H1V4.5Z",fill:l}),(0,n.jsx)("path",{d:"M1 6h14.5L14 13H2L1 6Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.12"})]}):(0,n.jsx)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:(0,n.jsx)("path",{d:"M1 4.5A1 1 0 0 1 2 3.5H5.5L7 5H14a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V4.5Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.1"})})}function a({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:[(0,n.jsx)("path",{d:"M3 2h7l3 3v9H3V2Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.08"}),(0,n.jsx)("path",{d:"M10 2v3h3",stroke:l,strokeWidth:"1",fill:"none",strokeLinejoin:"round"})]})}function d({label:e,size:t=14}){let r=t/14;return(0,n.jsxs)("svg",{width:t,height:t,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("text",{x:"7",y:"9.5",textAnchor:"middle",fontSize:3.4*r,fontFamily:"var(--font-mono), monospace",fontWeight:"600",fill:l,letterSpacing:"0",children:e})]})}function c({size:e=14}){return(0,n.jsx)(d,{label:"TS",size:e})}function u({size:e=14}){return(0,n.jsx)(d,{label:"TSX",size:e})}function p({size:e=14}){return(0,n.jsx)(d,{label:"JS",size:e})}function x({size:e=14}){return(0,n.jsx)(d,{label:"JSX",size:e})}function h({size:e=14}){return(0,n.jsx)(d,{label:"PY",size:e})}function g({size:e=14}){return(0,n.jsx)(d,{label:"{}",size:e})}function f({size:e=14}){return(0,n.jsx)(d,{label:"CSS",size:e})}function v({size:e=14}){return(0,n.jsx)(d,{label:"SC",size:e})}function y({size:e=14}){return(0,n.jsx)(d,{label:"HTM",size:e})}function b({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M3.5 9.5V7l1.5 1.5L6.5 7v2.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"}),(0,n.jsx)("path",{d:"M8 7v2.5M7 9l1 1.5 1-1.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"})]})}function m({size:e=14}){return(0,n.jsx)(d,{label:"YML",size:e})}function j({size:e=14}){return(0,n.jsx)(d,{label:"TOM",size:e})}function k({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M4 7.5l2 1.5-2 1.5",stroke:l,strokeWidth:"0.95",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"}),(0,n.jsx)("path",{d:"M7.5 10.5h2.5",stroke:l,strokeWidth:"0.95",strokeLinecap:"round"})]})}function w({size:e=14}){return(0,n.jsx)(d,{label:"RS",size:e})}function S({size:e=14}){return(0,n.jsx)(d,{label:"GO",size:e})}function C({size:e=14}){return(0,n.jsx)(d,{label:"SQL",size:e})}function L({size:e=14}){return(0,n.jsx)(d,{label:"GQL",size:e})}function T({size:e=14}){return(0,n.jsx)(d,{label:"TF",size:e})}function z({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("rect",{x:"3.5",y:"6.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"}),(0,n.jsx)("rect",{x:"6",y:"6.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"}),(0,n.jsx)("rect",{x:"3.5",y:"8.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"})]})}function I({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"5.5",cy:"8.5",r:"1.5",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M7 8.5h2.5M8.5 8.5v1.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round"})]})}function M({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"5",cy:"6.5",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("circle",{cx:"9",cy:"6.5",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("circle",{cx:"5",cy:"10",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("path",{d:"M5 7.5V9",stroke:l,strokeWidth:"0.85",strokeLinecap:"round"}),(0,n.jsx)("path",{d:"M9 7.5v.5a2 2 0 0 1-2 2H6",stroke:l,strokeWidth:"0.85",strokeLinecap:"round",fill:"none"})]})}function R({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("rect",{x:"4.5",y:"8.5",width:"5",height:"3",rx:"0.6",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M5.5 8.5V7.5a1.5 1.5 0 0 1 3 0v1",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",fill:"none"})]})}function W({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"7",cy:"8.5",r:"1.3",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M7 6.5v.7M7 10.3v.7M5 8.5h.7M8.3 8.5H9M5.5 6.9l.5.5M8.5 9.6l-.5-.5M5.5 10.1l.5-.5M8.5 7.4l-.5.5",stroke:l,strokeWidth:"0.8",strokeLinecap:"round"})]})}function E(e,t=14){let r=e.toLowerCase(),o=r.split(".").pop()??"";if("dockerfile"===r||r.startsWith("dockerfile."))return(0,n.jsx)(z,{size:t});if(".env"===r||r.startsWith(".env."))return(0,n.jsx)(I,{size:t});if(".gitignore"===r||".gitattributes"===r||".gitmodules"===r)return(0,n.jsx)(M,{size:t});if("package-lock.json"===r||"yarn.lock"===r||"bun.lock"===r||"pnpm-lock.yaml"===r||"cargo.lock"===r)return(0,n.jsx)(R,{size:t});if(r.endsWith(".config.ts")||r.endsWith(".config.js")||r.endsWith(".config.mjs")||r.endsWith(".config.cjs")||[".eslintrc",".eslintrc.js",".eslintrc.json",".eslintrc.yml","eslint.config.mjs","eslint.config.js"].includes(r))return(0,n.jsx)(W,{size:t});switch(o){case"ts":return(0,n.jsx)(c,{size:t});case"tsx":return(0,n.jsx)(u,{size:t});case"js":case"mjs":case"cjs":return(0,n.jsx)(p,{size:t});case"jsx":return(0,n.jsx)(x,{size:t});case"py":return(0,n.jsx)(h,{size:t});case"json":case"jsonl":return(0,n.jsx)(g,{size:t});case"css":case"less":return(0,n.jsx)(f,{size:t});case"scss":return(0,n.jsx)(v,{size:t});case"html":case"htm":return(0,n.jsx)(y,{size:t});case"md":case"mdx":return(0,n.jsx)(b,{size:t});case"yaml":case"yml":return(0,n.jsx)(m,{size:t});case"toml":return(0,n.jsx)(j,{size:t});case"sh":case"bash":case"zsh":case"fish":return(0,n.jsx)(k,{size:t});case"rs":return(0,n.jsx)(w,{size:t});case"go":return(0,n.jsx)(S,{size:t});case"sql":return(0,n.jsx)(C,{size:t});case"graphql":case"gql":return(0,n.jsx)(L,{size:t});case"tf":case"hcl":return(0,n.jsx)(T,{size:t});case"lock":return(0,n.jsx)(R,{size:t});default:return(0,n.jsx)(a,{size:t})}}function B({node:e,depth:t,onOpenFile:r}){let[i,l]=(0,o.useState)(!1),[a,d]=(0,o.useState)(e.children??[]),[c,u]=(0,o.useState)(e.loaded??!1),[p,x]=(0,o.useState)(!1),[h,g]=(0,o.useState)(!1),f=(0,o.useCallback)(async()=>{if(!c){x(!0);try{let t=e.fullPath.split("/").filter(Boolean).join("/"),r=await fetch(`/api/files/${t}?type=list`);if(!r.ok)return;let n=await r.json();d(n.entries.map(t=>({name:t.name,fullPath:e.fullPath.replace(/\/$/,"")+"/"+t.name,isDir:t.isDir,size:t.size,children:t.isDir?[]:void 0,loaded:!t.isDir}))),u(!0)}catch{}finally{x(!1)}}},[c,e.fullPath]),v=(0,o.useCallback)(()=>{e.isDir?(c||i||f(),l(e=>!e)):r(e.fullPath,e.name)},[e.isDir,e.fullPath,e.name,c,i,f,r]);return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{onClick:v,onMouseEnter:()=>g(!0),onMouseLeave:()=>g(!1),style:{display:"flex",alignItems:"center",gap:4,paddingLeft:8+14*t,paddingRight:8,height:24,cursor:"pointer",background:h?"var(--bg-hover)":"transparent",borderRadius:4,userSelect:"none"},children:[e.isDir&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:i?"rotate(90deg)":"none",transition:"transform 0.1s"},children:(0,n.jsx)("polyline",{points:"3 2 7 5 3 8"})}),!e.isDir&&(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),(0,n.jsx)("span",{style:{flexShrink:0,display:"flex",alignItems:"center"},children:e.isDir?(0,n.jsx)(s,{size:14,open:i}):E(e.name,14)}),(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},title:e.fullPath,children:e.name}),p&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 24 24",fill:"none",stroke:"var(--text-dim)",strokeWidth:"2",strokeLinecap:"round",children:(0,n.jsx)("path",{d:"M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4"})})]}),e.isDir&&i&&(0,n.jsxs)("div",{children:[a.map(e=>(0,n.jsx)(B,{node:e,depth:t+1,onOpenFile:r},e.fullPath)),0===a.length&&c&&(0,n.jsx)("div",{style:{paddingLeft:8+(t+1)*14,fontSize:11,color:"var(--text-dim)",height:22,display:"flex",alignItems:"center"},children:"empty"})]})]})}function $({cwd:e,onOpenFile:t,refreshKey:r}){let[i,l]=(0,o.useState)([]),[s,a]=(0,o.useState)(!0),[d,c]=(0,o.useState)(null),[u,p]=(0,o.useState)(0);return((0,o.useEffect)(()=>{p(e=>e+1)},[r]),(0,o.useEffect)(()=>{a(!0),c(null);let t=e.split("/").filter(Boolean).join("/");fetch(`/api/files/${t}?type=list`).then(e=>e.json()).then(t=>{t.error?c(t.error):l((t.entries??[]).map(t=>({name:t.name,fullPath:e.replace(/\/$/,"")+"/"+t.name,isDir:t.isDir,size:t.size,children:t.isDir?[]:void 0,loaded:!t.isDir})))}).catch(e=>c(String(e))).finally(()=>a(!1))},[e,u]),s)?(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"var(--text-dim)"},children:"Loading files..."}):d?(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"#f87171"},children:d}):(0,n.jsxs)("div",{style:{padding:"2px 4px"},children:[i.map(e=>(0,n.jsx)(B,{node:e,depth:0,onOpenFile:t},`${u}:${e.fullPath}`)),0===i.length&&(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"var(--text-dim)"},children:"No files found"})]})}function O(e){let t=new Map;for(let r of e){if(!r.cwd)continue;let e=t.get(r.cwd);(!e||r.modified>e)&&t.set(r.cwd,r.modified)}return[...t.entries()].sort((e,t)=>t[1].localeCompare(e[1])).slice(0,5).map(([e])=>e)}function P(e,t){let r=t&&e.startsWith(t)?"~"+e.slice(t.length):e,n=r.includes("/")?"/":"\\",o=r.split(n).filter(Boolean);return o.length<=2?r:"…/"+o.slice(-2).join(n)}let D="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";function F(){let[e,t]=(0,o.useState)(!1),[r,i]=(0,o.useState)(!1),l=(0,o.useRef)(null),s=function(e,t){let[r,n]=(0,o.useState)(e),i=(0,o.useRef)(null),l=(0,o.useRef)(0);return(0,o.useEffect)(()=>{if(!t)return void n(e);l.current=0;let r=4*e.length,o=()=>{l.current+=1;let t=Math.floor(l.current/r*e.length);n(e.split("").map((e,r)=>" "===e?" ":r<t?e:D[Math.floor(Math.random()*D.length)]).join("")),l.current<r?i.current=requestAnimationFrame(o):n(e)};return i.current=requestAnimationFrame(o),()=>{i.current&&cancelAnimationFrame(i.current)}},[e,t]),r}(e?"v0.3.8":"Pi Agent Web",r),a=(0,o.useCallback)(e=>{t(e),i(!0),setTimeout(()=>i(!1),(e?6:8)*4*(1e3/60)+100)},[]),d=(0,o.useCallback)(()=>{l.current&&clearTimeout(l.current);let t=!e;a(t),t&&(l.current=setTimeout(()=>a(!1),3e3))},[e,a]);return(0,o.useEffect)(()=>()=>{l.current&&clearTimeout(l.current)},[]),(0,n.jsx)("button",{onClick:d,style:{background:"none",border:"none",padding:0,cursor:"default",fontWeight:700,fontSize:15,letterSpacing:"-0.01em",color:e?"var(--accent)":"var(--text)",fontFamily:"var(--font-mono)",minWidth:"6ch"},children:s})}function H({selectedSessionId:e,onSelectSession:t,onNewSession:r,initialSessionId:i,onInitialRestoreDone:l,refreshKey:s,onSessionDeleted:a,selectedCwd:d,onCwdChange:c,onOpenFile:u,explorerRefreshKey:p}){let[x,h]=(0,o.useState)([]),[g,f]=(0,o.useState)(!0),[v,y]=(0,o.useState)(null),[b,m]=(0,o.useState)(null),[j,k]=(0,o.useState)(""),[w,S]=(0,o.useState)(!1),[C,L]=(0,o.useState)(!1),[T,z]=(0,o.useState)(""),I=(0,o.useRef)(null),M=(0,o.useRef)(null),[R,W]=(0,o.useState)(!0),[E,B]=(0,o.useState)(0),D=(0,o.useCallback)(async(e=!1)=>{try{e&&f(!0);let t=await fetch("/api/sessions");if(!t.ok)throw Error(`HTTP ${t.status}`);let r=await t.json();h(r.sessions),y(null)}catch(e){y(String(e))}finally{e&&f(!1)}},[]),A=(0,o.useRef)(!1);(0,o.useEffect)(()=>{let e=!A.current;A.current=!0,D(e)},[D,s]),(0,o.useEffect)(()=>{void 0!==p&&B(e=>e+1)},[p]),(0,o.useEffect)(()=>{fetch("/api/home").then(e=>e.json()).then(e=>{e.home&&k(e.home)}).catch(()=>{})},[]);let U=(0,o.useRef)(!1);(0,o.useEffect)(()=>{c?.(b)},[b,c]),(0,o.useEffect)(()=>{if(0!==x.length&&null===b){if(i&&!U.current){U.current=!0;let e=x.find(e=>e.id===i);if(e){m(e.cwd),t(e,!0);return}l?.()}let e=O(x);e.length>0&&m(e[0])}},[x,b,i,t,l]);let _=(0,o.useCallback)(()=>{let e=T.trim();e&&m(e),L(!1),z(""),S(!1)},[T]),V=(0,o.useCallback)(async()=>{try{let e=await fetch("/api/default-cwd",{method:"POST"}),t=await e.json();t.cwd&&(m(t.cwd),S(!1))}catch{}},[]);(0,o.useEffect)(()=>{let e=e=>{M.current&&!M.current.contains(e.target)&&(S(!1),L(!1),z(""))};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]);let J=(0,o.useCallback)(()=>{if(!b)return;let e="function"==typeof crypto.randomUUID?crypto.randomUUID():`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}`;r?.(e,b)},[b,r]),Y=O(x),Z=b?x.filter(e=>e.cwd===b):x,K=function(e){let t=new Map;for(let r of e)t.set(r.id,{session:r,children:[]});let r=new Map;for(let t of e)t.parentSessionId&&r.set(t.id,t.parentSessionId);let n=[];for(let e of t.values()){let o=function(e){let n=r.get(e),o=new Set;for(;n&&!o.has(n);){if(o.add(n),t.has(n))return n;n=r.get(n)}return null}(e.session.id);o?t.get(o).children.push(e):n.push(e)}let o=e=>{e.sort((e,t)=>t.session.modified.localeCompare(e.session.modified)),e.forEach(e=>o(e.children))};return o(n),n}(Z);return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{padding:"12px 10px 10px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:10},children:[(0,n.jsx)(F,{}),(0,n.jsxs)("div",{style:{display:"flex",gap:6},children:[(0,n.jsxs)("button",{onClick:J,disabled:!b,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:5,background:"var(--bg-hover)",border:"1px solid var(--border)",color:b?"var(--text-muted)":"var(--text-dim)",cursor:b?"pointer":"not-allowed",height:32,paddingLeft:10,paddingRight:12,borderRadius:7,fontSize:12,fontWeight:500,letterSpacing:"-0.01em",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},title:b?`New session in ${b}`:"Select a project first",onMouseEnter:e=>{b&&(e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)")},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color=b?"var(--text-muted)":"var(--text-dim)",e.currentTarget.style.borderColor="var(--border)"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"2.2",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"6",y1:"1",x2:"6",y2:"11"}),(0,n.jsx)("line",{x1:"1",y1:"6",x2:"11",y2:"6"})]}),"New"]}),(0,n.jsx)("button",{onClick:()=>D(!0),style:{display:"flex",alignItems:"center",justifyContent:"center",background:"var(--bg-hover)",border:"1px solid var(--border)",color:"var(--text-muted)",cursor:"pointer",width:32,height:32,borderRadius:7,padding:0,flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},title:"Refresh",children:(0,n.jsxs)("svg",{width:"15",height:"15",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]})})]})]}),(0,n.jsxs)("div",{ref:M,style:{position:"relative"},children:[(0,n.jsx)("button",{onClick:()=>S(e=>!e),style:{width:"100%",display:"flex",alignItems:"center",padding:"6px 10px",background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,cursor:"pointer",fontSize:12,color:"var(--text)",textAlign:"left"},children:(0,n.jsx)("span",{style:{flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",fontFamily:"var(--font-mono)",fontSize:11,color:b?"var(--text)":"var(--text-dim)"},title:b??"",children:b?P(b,j):i&&!U.current?"":"Select project…"})}),w&&(0,n.jsxs)("div",{style:{position:"absolute",top:"calc(100% + 4px)",left:0,right:0,zIndex:100,background:"var(--bg)",border:"1px solid var(--border)",borderRadius:8,boxShadow:"0 6px 20px rgba(0,0,0,0.10)",overflow:"hidden"},children:[Y.map(e=>(0,n.jsxs)("button",{onClick:()=>{m(e),L(!1),z(""),S(!1)},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:e===b?"var(--bg-selected)":"none",border:"none",borderBottom:"1px solid var(--border)",color:e===b?"var(--text)":"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11,fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},title:e,children:[e===b&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--accent)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:(0,n.jsx)("polyline",{points:"1.5 5 4 7.5 8.5 2.5"})}),e!==b&&(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),(0,n.jsx)("span",{style:{flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:P(e,j)})]},e)),!C&&(0,n.jsxs)("button",{onClick:e=>{e.stopPropagation(),V()},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:"none",border:"none",borderTop:Y.length>0?"1px solid var(--border)":"none",color:"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11},children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.1",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:(0,n.jsx)("path",{d:"M1 3A1 1 0 0 1 2 2H4L5 3.5H8.5a.5.5 0 0 1 .5.5v4a.5.5 0 0 1-.5.5h-7A.5.5 0 0 1 1 8V3Z"})}),(0,n.jsx)("span",{children:"Use default directory"})]}),C?(0,n.jsxs)("div",{style:{padding:"6px 8px",borderTop:Y.length>0?"none":void 0},children:[(0,n.jsx)("input",{ref:I,value:T,onChange:e=>z(e.target.value),onKeyDown:e=>{"Enter"===e.key&&_(),"Escape"===e.key&&(L(!1),z(""))},placeholder:"/path/to/project",style:{width:"100%",fontSize:11,fontFamily:"var(--font-mono)",padding:"5px 8px",border:"1px solid var(--accent)",borderRadius:5,outline:"none",background:"var(--bg)",color:"var(--text)",boxSizing:"border-box"}}),(0,n.jsxs)("div",{style:{display:"flex",gap:5,marginTop:5},children:[(0,n.jsx)("button",{onClick:_,style:{flex:1,padding:"4px 0",background:"var(--accent)",border:"none",borderRadius:5,color:"#fff",fontSize:11,fontWeight:600,cursor:"pointer"},children:"Open"}),(0,n.jsx)("button",{onClick:()=>{L(!1),z("")},style:{flex:1,padding:"4px 0",background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:5,color:"var(--text-muted)",fontSize:11,cursor:"pointer"},children:"Cancel"})]})]}):(0,n.jsxs)("button",{onClick:e=>{e.stopPropagation(),L(!0),setTimeout(()=>I.current?.focus(),0)},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.1",strokeLinecap:"round",style:{flexShrink:0},children:[(0,n.jsx)("line",{x1:"5",y1:"1",x2:"5",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"5",x2:"9",y2:"5"})]}),(0,n.jsx)("span",{children:"Custom path…"})]})]})]})]}),(0,n.jsxs)("div",{style:{flex:R&&(d||b)?"1 1 0":"1 1 auto",overflowY:"auto",padding:"0",minHeight:80},children:[g&&(0,n.jsx)("div",{style:{padding:"16px 14px",color:"var(--text-muted)",fontSize:12},children:"Loading..."}),v&&(0,n.jsx)("div",{style:{padding:"12px 14px",color:"#f87171",fontSize:12},children:v}),!g&&!v&&0===Z.length&&(0,n.jsx)("div",{style:{padding:"16px 14px",color:"var(--text-muted)",fontSize:12},children:"No sessions found"}),K.map(r=>(0,n.jsx)(N,{node:r,selectedSessionId:e,onSelectSession:t,onRenamed:D,onSessionDeleted:e=>{a?.(e),D()},depth:0},r.session.id))]}),(d||b)&&(0,n.jsxs)("div",{style:{borderTop:"1px solid var(--border)",display:"flex",flexDirection:"column",flex:R?"1 1 0":"0 0 auto",minHeight:0,overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",flexShrink:0},children:[(0,n.jsxs)("button",{onClick:()=>W(e=>!e),style:{display:"flex",alignItems:"center",gap:6,flex:1,padding:"6px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:11,fontWeight:600,letterSpacing:"0.05em",textTransform:"uppercase",textAlign:"left"},children:[(0,n.jsx)("svg",{width:"9",height:"9",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{transform:R?"rotate(90deg)":"none",transition:"transform 0.15s",flexShrink:0},children:(0,n.jsx)("polyline",{points:"3 2 7 5 3 8"})}),"Explorer"]}),(0,n.jsx)("button",{onClick:()=>B(e=>e+1),title:"Refresh explorer",style:{display:"flex",alignItems:"center",justifyContent:"center",width:26,height:26,padding:0,marginRight:6,background:"none",border:"none",color:"var(--text-dim)",cursor:"pointer",borderRadius:5,flexShrink:0,transition:"color 0.12s, background 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.background="var(--bg-hover)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)",e.currentTarget.style.background="none"},children:(0,n.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]})})]}),R&&(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",overflowX:"hidden"},children:(0,n.jsx)($,{cwd:d??b,onOpenFile:u??(()=>{}),refreshKey:E})})]})]})}function N({node:e,selectedSessionId:t,onSelectSession:r,onRenamed:i,onSessionDeleted:l,depth:s}){let[a,d]=(0,o.useState)(!1),c=e.children.length>0;return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{style:{position:"relative"},children:[s>0&&(0,n.jsx)("div",{style:{position:"absolute",left:12*s+6,top:0,bottom:0,width:1,background:"var(--border)",pointerEvents:"none"}}),(0,n.jsx)(A,{session:e.session,isSelected:e.session.id===t,onClick:()=>r(e.session),onRenamed:i,onDeleted:e=>l?.(e),depth:s,hasChildren:c,collapsed:a,onToggleCollapse:()=>d(e=>!e)})]}),c&&!a&&(0,n.jsx)("div",{children:e.children.map(e=>(0,n.jsx)(N,{node:e,selectedSessionId:t,onSelectSession:r,onRenamed:i,onSessionDeleted:l,depth:s+1},e.session.id))})]})}function A({session:e,isSelected:t,onClick:r,onRenamed:i,onDeleted:l,depth:s=0,hasChildren:a=!1,collapsed:d=!1,onToggleCollapse:c}){let u,p,x,h,g,[f,v]=(0,o.useState)(!1),[y,b]=(0,o.useState)(!1),[m,j]=(0,o.useState)(""),[k,w]=(0,o.useState)(!1),[S,C]=(0,o.useState)(!1),L=(0,o.useRef)(null),T=e.name||e.firstMessage.slice(0,50)||e.id.slice(0,12),z=(0,o.useCallback)(t=>{t.stopPropagation(),j(e.name??""),b(!0),setTimeout(()=>L.current?.select(),0)},[e.name]),I=(0,o.useCallback)(async()=>{let t=m.trim();if(b(!1),t!==(e.name??""))try{await fetch(`/api/sessions/${encodeURIComponent(e.id)}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:t})}),i?.()}catch{}},[m,e.id,e.name,i]),M=(0,o.useCallback)(e=>{e.stopPropagation(),w(!0)},[]),R=(0,o.useCallback)(async t=>{t.stopPropagation(),w(!1),C(!0);try{await fetch(`/api/sessions/${encodeURIComponent(e.id)}`,{method:"DELETE"}),l?.(e.id)}catch{C(!1)}},[e.id,l]),W=(0,o.useCallback)(e=>{e.stopPropagation(),w(!1)},[]);return(0,n.jsx)("div",{onClick:k||y?void 0:r,onMouseEnter:()=>v(!0),onMouseLeave:()=>{v(!1)},style:{height:54,display:"flex",alignItems:"center",paddingLeft:s>0?12*s+14:14,paddingRight:8,cursor:k||y?"default":"pointer",background:k?"rgba(239,68,68,0.06)":t?"var(--bg-selected)":f?"var(--bg-hover)":"transparent",borderLeft:k?"2px solid #ef4444":t?"2px solid var(--accent)":"2px solid transparent",transition:"background 0.1s",opacity:S?.5:1,gap:6,overflow:"hidden"},children:k?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("div",{style:{flex:1,minWidth:0,fontSize:12,color:"var(--text)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:["Delete ",(0,n.jsxs)("span",{style:{fontWeight:600},children:["“",T.slice(0,22),T.length>22?"…":"","”"]}),"?"]}),(0,n.jsxs)("div",{style:{display:"flex",gap:5,flexShrink:0},children:[(0,n.jsxs)("button",{onClick:R,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:4,height:30,padding:"0 11px",background:"#ef4444",border:"none",borderRadius:6,color:"#fff",cursor:"pointer",fontSize:12,fontWeight:600,whiteSpace:"nowrap"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"3 6 5 6 21 6"}),(0,n.jsx)("path",{d:"M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"}),(0,n.jsx)("path",{d:"M10 11v6M14 11v6"}),(0,n.jsx)("path",{d:"M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"})]}),"Delete"]}),(0,n.jsx)("button",{onClick:W,style:{display:"flex",alignItems:"center",justifyContent:"center",height:30,padding:"0 11px",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:12,fontWeight:500,whiteSpace:"nowrap"},children:"Cancel"})]})]}):y?(0,n.jsx)("input",{ref:L,value:m,onChange:e=>j(e.target.value),onBlur:I,onKeyDown:e=>{"Enter"===e.key&&I(),"Escape"===e.key&&b(!1)},autoFocus:!0,style:{flex:1,fontSize:12,padding:"5px 8px",border:"1px solid var(--accent)",borderRadius:5,outline:"none",background:"var(--bg)",color:"var(--text)",height:30}}):(0,n.jsxs)(n.Fragment,{children:[s>0&&(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 24 24",fill:"none",stroke:"var(--text-dim)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),(0,n.jsxs)("div",{style:{flex:1,minWidth:0},children:[(0,n.jsx)("div",{style:{fontSize:12,fontWeight:t?500:400,lineHeight:1.4,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",color:"var(--text)"},title:T,children:T}),(0,n.jsxs)("div",{style:{marginTop:2,display:"flex",gap:8,color:"var(--text-dim)",fontSize:11},children:[(0,n.jsx)("span",{title:e.modified,children:(u=new Date(e.modified),x=Math.floor((p=new Date().getTime()-u.getTime())/6e4),h=Math.floor(p/36e5),g=Math.floor(p/864e5),x<1?"just now":x<60?`${x}m ago`:h<24?`${h}h ago`:g<7?`${g}d ago`:u.toLocaleDateString())}),(0,n.jsxs)("span",{children:[e.messageCount," msgs"]})]})]}),a&&(0,n.jsx)("button",{onClick:e=>{e.stopPropagation(),c?.()},title:d?"Expand forks":"Collapse forks",style:{display:"flex",alignItems:"center",justifyContent:"center",width:20,height:20,padding:0,flexShrink:0,background:"none",border:"none",color:"var(--text-dim)",cursor:"pointer",transform:d?"rotate(-90deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})}),f&&(0,n.jsxs)("div",{style:{display:"flex",gap:4,flexShrink:0},children:[(0,n.jsx)("button",{onClick:z,title:"Rename",style:{display:"flex",alignItems:"center",justifyContent:"center",width:32,height:32,padding:0,background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},children:(0,n.jsx)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("path",{d:"M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"})})}),(0,n.jsx)("button",{onClick:M,title:"Delete",style:{display:"flex",alignItems:"center",justifyContent:"center",width:32,height:32,padding:0,background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="rgba(239,68,68,0.08)",e.currentTarget.style.color="#ef4444",e.currentTarget.style.borderColor="rgba(239,68,68,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},children:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"3 6 5 6 21 6"}),(0,n.jsx)("path",{d:"M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"}),(0,n.jsx)("path",{d:"M10 11v6M14 11v6"}),(0,n.jsx)("path",{d:"M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"})]})})]})]})})}function U(e){if("assistant"!==e.role||!Array.isArray(e.content))return e;let t=e.content.map(e=>"toolCall"!==e.type?e:{type:"toolCall",toolCallId:e.toolCallId??e.id,toolName:e.toolName??e.name,input:e.input??e.arguments});return{...e,content:t}}var _=r(8426),V=r(2957);function J(e){if(navigator.clipboard?.writeText)return navigator.clipboard.writeText(e);try{let t=document.createElement("textarea");return t.value=e,t.style.position="fixed",t.style.opacity="0",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t),Promise.resolve()}catch{return Promise.reject()}}function Y({message:e,isStreaming:t,toolResults:r,modelNames:o,entryId:i,onFork:l,forking:s,onNavigate:a,prevAssistantEntryId:d}){return"user"===e.role?(0,n.jsx)(Z,{message:e,entryId:i,onFork:l,forking:s,onNavigate:a,prevAssistantEntryId:d}):"assistant"===e.role?(0,n.jsx)(K,{message:e,isStreaming:t,toolResults:r,modelNames:o}):(e.role,null)}function Z({message:e,entryId:t,onFork:r,forking:i,onNavigate:l,prevAssistantEntryId:s}){let[a,d]=(0,o.useState)(!1),[c,u]=(0,o.useState)(!1),p="string"==typeof e.content?e.content:e.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"),x=function(e){if(!e)return null;let t=new Date(e),r=new Date,n=t.getFullYear()===r.getFullYear()&&t.getMonth()===r.getMonth()&&t.getDate()===r.getDate(),o=t.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});if(n)return o;let i=t.toLocaleDateString([],{month:"short",day:"numeric",year:t.getFullYear()!==r.getFullYear()?"numeric":void 0});return`${i} ${o}`}(e.timestamp),h=!!t&&!!r,g=!!s&&!!l;return(0,n.jsxs)("div",{style:{marginBottom:16,display:"flex",flexDirection:"column",alignItems:"flex-end"},onMouseEnter:()=>d(!0),onMouseLeave:()=>d(!1),children:[(0,n.jsx)("div",{style:{display:"flex",alignItems:"flex-end",gap:6,maxWidth:"85%"},children:(0,n.jsx)("div",{style:{flex:1,minWidth:0,background:"var(--user-bg)",border:"1px solid rgba(59,130,246,0.2)",borderRadius:12,padding:"8px 12px",fontSize:14,lineHeight:1.6,color:"var(--text)",whiteSpace:"pre-wrap",wordBreak:"break-word"},children:p})}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",gap:6,marginTop:3},children:[(0,n.jsx)("div",{style:{display:"flex",gap:3,opacity:+!!a,pointerEvents:a?"auto":"none",transition:"opacity 0.12s"},children:(0,n.jsxs)("button",{onClick:()=>{J(p).then(()=>{u(!0),setTimeout(()=>u(!1),1500)})},title:"Copy message",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:c?"var(--accent)":"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{c||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{c||(e.currentTarget.style.color="var(--text-dim)")},children:[c?(0,n.jsx)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"20 6 9 17 4 12"})}):(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),(0,n.jsx)("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),c?"Copied":"Copy"]})}),(h||g)&&(0,n.jsxs)("div",{style:{display:"flex",gap:3,opacity:a||i?1:0,pointerEvents:a||i?"auto":"none",transition:"opacity 0.12s"},children:[g&&(0,n.jsxs)("button",{onClick:()=>l(s),title:"Edit from here — branches within this session",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--accent)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)"},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"15 10 20 15 15 20"}),(0,n.jsx)("path",{d:"M4 4v7a4 4 0 0 0 4 4h12"})]}),"Edit from here"]}),h&&(0,n.jsxs)("button",{onClick:()=>r(t),disabled:i,title:i?"Creating new session…":"New session — creates an independent copy from here",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:i?"var(--accent)":"var(--text-dim)",cursor:i?"not-allowed":"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{i||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{i||(e.currentTarget.style.color="var(--text-dim)")},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),i?"Creating…":"New session"]})]}),x&&(0,n.jsx)("span",{style:{fontSize:10,color:"var(--text-dim)"},children:x})]})]})}function K({message:e,isStreaming:t,toolResults:r,modelNames:i}){var l;let s,a=e.content??[],[d,c]=(0,o.useState)(!1),[u,p]=(0,o.useState)(!1),x=a.filter(e=>"text"===e.type).map(e=>e.text).join("\n");return(0,n.jsxs)("div",{style:{marginBottom:16},onMouseEnter:()=>c(!0),onMouseLeave:()=>c(!1),children:[(0,n.jsxs)("div",{style:{fontSize:11,color:"var(--text-dim)",marginBottom:4,display:"flex",alignItems:"center",gap:6},children:[e.provider&&(0,n.jsx)("span",{children:i?.[e.model]??e.model}),t&&(0,n.jsx)("span",{style:{display:"inline-block",width:6,height:6,borderRadius:"50%",background:"var(--accent)",animation:"blink 1s infinite"}})]}),(0,n.jsx)("div",{style:{display:"flex",flexDirection:"column",gap:8},children:a.map((e,t)=>(0,n.jsx)(q,{block:e,toolResults:r},t))}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:8,marginTop:4},children:[e.usage&&!t&&(0,n.jsx)("div",{style:{fontSize:11,color:"var(--text-dim)"},children:(l=e.usage,s=[],l.input&&s.push(`${l.input.toLocaleString()} in`),l.output&&s.push(`${l.output.toLocaleString()} out`),l.cacheRead&&s.push(`${l.cacheRead.toLocaleString()} cache`),l.cost?.total&&s.push(`$${l.cost.total.toFixed(4)}`),s.join(" \xb7 "))}),x&&!t&&(0,n.jsxs)("button",{onClick:()=>{J(x).then(()=>{p(!0),setTimeout(()=>p(!1),1500)})},title:"Copy message",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:u?"var(--accent)":"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",opacity:+!!d,pointerEvents:d?"auto":"none",transition:"opacity 0.12s, color 0.12s"},onMouseEnter:e=>{u||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{u||(e.currentTarget.style.color="var(--text-dim)")},children:[u?(0,n.jsx)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"20 6 9 17 4 12"})}):(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),(0,n.jsx)("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),u?"Copied":"Copy"]})]})]})}function q({block:e,toolResults:t}){if("text"===e.type)return(0,n.jsx)(X,{block:e});if("thinking"===e.type)return(0,n.jsx)(Q,{block:e});if("toolCall"===e.type){let r=t?.get(e.toolCallId);return(0,n.jsx)(G,{block:e,result:r})}return null}function X({block:e}){return(0,n.jsx)("div",{className:"markdown-body",children:(0,n.jsx)(_.oz,{remarkPlugins:[V.A],components:{code({className:e,children:t,...r}){let o=e?.includes("language-"),i=e?.replace("language-","")??"";return o?(0,n.jsx)(et,{code:String(t).replace(/\n$/,""),lang:i}):(0,n.jsx)("code",{style:{background:"var(--bg-selected)",padding:"1px 4px",borderRadius:3,fontFamily:"var(--font-mono)",fontSize:"0.9em"},...r,children:t})},pre:({children:e})=>(0,n.jsx)(n.Fragment,{children:e})},children:e.text})})}function Q({block:e}){let[t,r]=(0,o.useState)(!1);return(0,n.jsxs)("div",{style:{border:"1px solid var(--border)",borderRadius:6,overflow:"hidden",fontSize:13},children:[(0,n.jsxs)("button",{onClick:()=>r(e=>!e),style:{display:"flex",alignItems:"center",gap:6,width:"100%",padding:"6px 10px",background:"var(--bg-panel)",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left"},children:[(0,n.jsx)("span",{children:t?"▼":"▶"}),(0,n.jsx)("span",{children:"Thinking"})]}),t&&(0,n.jsx)("div",{style:{padding:"8px 10px",color:"var(--text-muted)",fontSize:12,lineHeight:1.6,whiteSpace:"pre-wrap",background:"var(--bg-panel)",borderTop:"1px solid var(--border)"},children:e.thinking})]})}function G({block:e,result:t}){let[r,i]=(0,o.useState)(!1),l=JSON.stringify(e.input,null,2),s=t?t.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"):null,a=null!==s&&("(no output)"===s.trim()||""===s.trim()),d=a?"(no output)":(s??"").slice(0,200).replace(/\n/g," "),c=t?.isError??!1;return(0,n.jsxs)("div",{style:{borderRadius:7,overflow:"hidden",fontSize:12,border:"1px solid rgba(34,197,94,0.25)",background:"rgba(34,197,94,0.04)"},children:[(0,n.jsxs)("button",{onClick:()=>i(e=>!e),style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"6px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left",minWidth:0},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 12 12",fill:"none",style:{flexShrink:0,color:"#16a34a"},children:[(0,n.jsx)("circle",{cx:"6",cy:"6",r:"5",stroke:"currentColor",strokeWidth:"1.4"}),(0,n.jsx)("polygon",{points:"4.5,3.5 9,6 4.5,8.5",fill:"currentColor"})]}),(0,n.jsx)("span",{style:{color:"#16a34a",fontFamily:"var(--font-mono)",fontWeight:600,fontSize:11,flexShrink:0},children:e.toolName}),(0,n.jsx)("span",{style:{color:"var(--text-dim)",fontFamily:"var(--font-mono)",fontSize:11,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0},children:function(e){let t=e.input;if(!t||"object"!=typeof t)return"";let r=Object.keys(t);return 0===r.length?"":"command"in t?String(t.command).slice(0,120):"path"in t?String(t.path).slice(0,120):"file_path"in t?String(t.file_path).slice(0,120):"pattern"in t?String(t.pattern).slice(0,120):"query"in t?String(t.query).slice(0,120):String(t[r[0]]).slice(0,120)}(e)}),(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:r?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),r&&(0,n.jsx)("pre",{style:{margin:0,padding:"8px 10px",color:"var(--text-muted)",fontSize:12,lineHeight:1.5,overflow:"auto",background:"rgba(0,0,0,0.03)",borderTop:"1px solid rgba(34,197,94,0.2)",whiteSpace:"pre-wrap",wordBreak:"break-all"},children:l}),t&&(0,n.jsx)(ee,{text:s??"",preview:d,isEmpty:a,isError:c,forceExpanded:r,onToggle:()=>i(e=>!e)})]})}function ee({text:e,preview:t,isEmpty:r,isError:o,forceExpanded:i,onToggle:l}){let s=i??!1;return(0,n.jsxs)("div",{style:{borderTop:`1px solid ${o?"rgba(248,113,113,0.3)":"rgba(34,197,94,0.15)"}`,background:o?"rgba(248,113,113,0.04)":"rgba(0,0,0,0.02)"},children:[(0,n.jsxs)("button",{onClick:()=>!r&&l?l():void 0,style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"5px 10px",background:"none",border:"none",color:o?"#f87171":"var(--text-dim)",cursor:r?"default":"pointer",textAlign:"left",fontSize:11,minWidth:0},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 12 12",fill:"none",style:{flexShrink:0,color:o?"#f87171":"var(--text-dim)"},children:[(0,n.jsx)("polyline",{points:"4 2 2 6 4 10",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M2 6h7a2 2 0 0 0 0-4H7",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]}),!s&&(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0,fontStyle:r?"italic":"normal",opacity:r?.5:1,color:o?"#f87171":"var(--text-dim)"},children:t}),!r&&!o&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:s?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),s&&(0,n.jsx)("pre",{style:{margin:0,padding:"8px 10px",color:o?"#f87171":"var(--text-muted)",fontSize:12,lineHeight:1.5,overflow:"auto",maxHeight:400,background:"var(--bg)",borderTop:`1px solid ${o?"rgba(248,113,113,0.2)":"rgba(34,197,94,0.15)"}`,whiteSpace:"pre-wrap",wordBreak:"break-all"},children:e})]})}function et({code:e,lang:t}){let[r,i]=(0,o.useState)(!1);return(0,n.jsxs)("div",{style:{position:"relative",marginTop:4,marginBottom:4,borderRadius:6,overflow:"hidden",border:"1px solid var(--border)"},children:[t&&(0,n.jsxs)("div",{style:{padding:"3px 10px",background:"var(--bg-panel)",borderBottom:"1px solid var(--border)",fontSize:11,color:"var(--text-dim)",display:"flex",justifyContent:"space-between",alignItems:"center"},children:[(0,n.jsx)("span",{children:t}),(0,n.jsx)("button",{onClick:()=>{J(e).then(()=>{i(!0),setTimeout(()=>i(!1),1500)})},style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:11},children:r?"copied":"copy"})]}),(0,n.jsx)("pre",{style:{margin:0,padding:"10px 12px",background:"var(--bg)",fontSize:12.5,lineHeight:1.6,overflowX:"auto",color:"var(--text)"},children:(0,n.jsx)("code",{children:e})})]})}function er(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(0)}k`:String(e)}let en=["off","default","full"],eo={off:"none",default:"default",full:"full"};function ei({onSend:e,onAbort:t,onSteer:r,onFollowUp:i,isStreaming:l,model:s,modelNames:a,modelList:d,onModelChange:c,onCompact:u,onAbortCompaction:p,isCompacting:x,compactError:h,toolPreset:g,onToolPresetChange:f,sessionStats:v,retryInfo:y,contextUsage:b}){let[m,j]=(0,o.useState)(""),[k,w]=(0,o.useState)("steer"),[S,C]=(0,o.useState)(!1),L=(0,o.useRef)(null),T=(0,o.useRef)(null),z=(0,o.useCallback)(()=>{let t=m.trim();t&&!l&&(e(t),j(""),L.current&&(L.current.style.height="auto"))},[m,l,e]),I=(0,o.useCallback)(()=>{let e=m.trim();e&&("steer"===k&&r?r(e):"followup"===k&&i&&i(e),j(""),L.current&&(L.current.style.height="auto"))},[m,k,r,i]),M=(0,o.useCallback)(e=>{"Enter"!==e.key||e.shiftKey||e.nativeEvent.isComposing||(e.preventDefault(),l&&(r||i)?I():z())},[l,r,i,I,z]),R=(0,o.useCallback)(()=>{let e=L.current;e&&(e.style.height="auto",e.style.height=`${Math.min(e.scrollHeight,200)}px`)},[]),W=d&&d.length>0?d.map(e=>({provider:e.provider,modelId:e.id,name:`${e.name} (${e.provider})`})):Object.entries(a??{}).map(([e,t])=>({provider:s?.provider??"unknown",modelId:e,name:t})),E=s?W.find(e=>e.modelId===s.modelId&&e.provider===s.provider)?.name??s.modelId:W.length>0?W[0].name:null;return(0,o.useEffect)(()=>{let e=e=>{T.current&&!T.current.contains(e.target)&&C(!1)};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]),(0,n.jsx)("div",{style:{flexShrink:0,borderTop:"1px solid var(--border)",background:"var(--bg-panel)",padding:"10px 16px 12px",paddingRight:52},children:(0,n.jsxs)("div",{style:{maxWidth:820,margin:"0 auto"},children:[y&&(0,n.jsxs)("div",{style:{marginBottom:8,padding:"5px 10px",background:"rgba(234,179,8,0.08)",border:"1px solid rgba(234,179,8,0.25)",borderRadius:6,fontSize:12,color:"rgba(180,130,0,0.9)",display:"flex",alignItems:"center",gap:6},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]}),"Retrying (",y.attempt,"/",y.maxAttempts,")…"]}),(0,n.jsxs)("div",{style:{display:"flex",gap:8,alignItems:"center",background:"var(--bg)",border:`1px solid ${l&&(r||i)?"steer"===k?"rgba(234,179,8,0.4)":"rgba(99,102,241,0.4)":"var(--border)"}`,borderRadius:8,padding:"8px 8px 8px 12px",transition:"border-color 0.15s"},children:[(0,n.jsx)("textarea",{ref:L,value:m,onChange:e=>j(e.target.value),onKeyDown:M,onInput:R,placeholder:l&&(r||i)?"steer"===k?"Inject guidance mid-run…":"Queue a message for after agent finishes…":l?"Agent is running…":"Message…",rows:1,style:{flex:1,background:"none",border:"none",outline:"none",resize:"none",color:"var(--text)",fontSize:14,lineHeight:1.6,fontFamily:"inherit",minHeight:24,maxHeight:200,overflow:"auto"}}),l?(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6,flexShrink:0},children:[(r||i)&&(0,n.jsxs)("button",{onClick:I,disabled:!m.trim(),style:{display:"flex",alignItems:"center",gap:5,padding:"7px 14px",background:m.trim()?"steer"===k?"rgba(234,179,8,0.12)":"rgba(129,140,248,0.12)":"none",border:`1px solid ${"steer"===k?"rgba(234,179,8,0.35)":"rgba(129,140,248,0.35)"}`,borderRadius:8,color:m.trim()?"steer"===k?"rgba(180,130,0,1)":"rgba(99,102,241,1)":"var(--text-dim)",cursor:m.trim()?"pointer":"not-allowed",fontSize:13,fontWeight:600,letterSpacing:"-0.01em",transition:"background 0.12s"},children:[(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"7",x2:"11",y2:"7"}),(0,n.jsx)("polyline",{points:"7.5 3 12 7 7.5 11"})]}),"Send"]}),(0,n.jsxs)("button",{onClick:t,style:{flexShrink:0,display:"flex",alignItems:"center",gap:6,padding:"7px 14px",background:"rgba(239,68,68,0.08)",border:"1px solid rgba(239,68,68,0.3)",borderRadius:8,color:"#ef4444",cursor:"pointer",fontSize:13,fontWeight:600,whiteSpace:"nowrap",letterSpacing:"-0.01em"},children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",children:(0,n.jsx)("rect",{x:"1.5",y:"1.5",width:"7",height:"7",rx:"1.5",fill:"currentColor"})}),"Stop"]})]}):(0,n.jsxs)("button",{onClick:z,disabled:!m.trim(),style:{flexShrink:0,display:"flex",alignItems:"center",gap:6,padding:"7px 14px",background:m.trim()?"var(--accent)":"var(--bg-panel)",border:"none",borderRadius:8,color:m.trim()?"#fff":"var(--text-dim)",cursor:m.trim()?"pointer":"not-allowed",fontSize:13,fontWeight:600,letterSpacing:"-0.01em",boxShadow:m.trim()?"0 1px 3px rgba(37,99,235,0.25)":"none",transition:"background 0.15s, box-shadow 0.15s"},children:[(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"7",x2:"11",y2:"7"}),(0,n.jsx)("polyline",{points:"7.5 3 12 7 7.5 11"})]}),"Send"]})]}),(0,n.jsxs)("div",{style:{marginTop:6,display:"flex",alignItems:"center",gap:8},children:[(0,n.jsx)("div",{style:{flex:"0 0 auto",display:"flex",alignItems:"center",gap:6},children:l&&(r||i)?(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-dim)"},children:"Send Mode"}),(0,n.jsx)("div",{style:{display:"flex",border:"1px solid var(--border)",borderRadius:6,overflow:"hidden"},children:["steer","followup"].map((e,t)=>{let r=k===e,o="steer"===e?"rgba(234,179,8,1)":"rgba(129,140,248,1)",i="steer"===e?"rgba(234,179,8,0.1)":"rgba(129,140,248,0.1)";return(0,n.jsx)("button",{onClick:()=>w(e),title:"steer"===e?"打断 Agent 当前运行":"在 Agent 完成后排队发送",style:{display:"flex",alignItems:"center",gap:5,padding:"4px 10px",background:r?i:"none",border:"none",borderLeft:t>0?`1px solid ${r?"transparent":"var(--border)"}`:"none",color:r?o:"var(--text-dim)",cursor:"pointer",fontSize:12,fontWeight:r?600:400,whiteSpace:"nowrap",transition:"background 0.12s, color 0.12s"},children:"steer"===e?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M5 1 L9 5 L5 9"}),(0,n.jsx)("line",{x1:"1",y1:"5",x2:"9",y2:"5"})]}),"Steer"]}):(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"1",x2:"5",y2:"6"}),(0,n.jsx)("polyline",{points:"2.5 3.5 5 1 7.5 3.5"}),(0,n.jsx)("line",{x1:"2",y1:"9",x2:"8",y2:"9"})]}),"Follow-up"]})},e)})})]}):W.length>0&&E&&c&&(0,n.jsxs)("div",{ref:T,style:{position:"relative"},children:[(0,n.jsxs)("button",{onClick:()=>C(e=>!e),disabled:l,style:{display:"flex",alignItems:"center",gap:5,padding:"4px 10px",background:"none",border:"1px solid var(--border)",borderRadius:5,color:"var(--text-muted)",cursor:l?"not-allowed":"pointer",fontSize:12,opacity:l?.5:1,transition:"border-color 0.12s, color 0.12s"},onMouseEnter:e=>{l||(e.currentTarget.style.borderColor="rgba(37,99,235,0.4)",e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{e.currentTarget.style.borderColor="var(--border)",e.currentTarget.style.color="var(--text-muted)"},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]}),E,(0,n.jsx)("svg",{width:"9",height:"9",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{transform:S?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),S&&(0,n.jsx)("div",{style:{position:"absolute",bottom:"calc(100% + 6px)",left:0,zIndex:100,background:"var(--bg)",border:"1px solid var(--border)",borderRadius:8,boxShadow:"0 -4px 16px rgba(0,0,0,0.10)",overflow:"hidden",minWidth:160},children:W.map(e=>{let t=e.modelId===s?.modelId&&e.provider===s?.provider;return(0,n.jsxs)("button",{onClick:()=>{C(!1),t||c(e.provider,e.modelId)},style:{display:"flex",alignItems:"center",gap:8,width:"100%",padding:"8px 12px",background:t?"var(--bg-selected)":"none",border:"none",borderBottom:"1px solid var(--border)",color:t?"var(--text)":"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left",fontWeight:t?600:400},onMouseEnter:e=>{t||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{t||(e.currentTarget.style.background="none")},children:[t?(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--accent)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"1.5 5 4 7.5 8.5 2.5"})}):(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),e.name]},`${e.provider}:${e.modelId}`)})})]})}),(0,n.jsx)("div",{className:"chat-stats-center",style:{flex:1,display:"flex",justifyContent:"center"},children:(v||b)&&(()=>{let e=[],t=[];if(v){let r=v.tokens;(r.input||0)+(r.output||0)+(r.cacheRead||0)+(r.cacheWrite||0)&&(r.input&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"8.5",x2:"5",y2:"1.5"}),(0,n.jsx)("polyline",{points:"2 4 5 1.5 8 4"})]}),er(r.input)]},"in")),r.output&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"1.5",x2:"5",y2:"8.5"}),(0,n.jsx)("polyline",{points:"2 6 5 8.5 8 6"})]}),er(r.output)]},"out")),r.cacheRead&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M8.5 5a3.5 3.5 0 1 1-1-2.45"}),(0,n.jsx)("polyline",{points:"6.5 1.5 8.5 2.5 7.5 4.5"})]}),er(r.cacheRead)]},"cache")),t.push(`in: ${r.input?.toLocaleString()??0} out: ${r.output?.toLocaleString()??0} cache read: ${r.cacheRead?.toLocaleString()??0} cache write: ${r.cacheWrite?.toLocaleString()??0}`))}let r="var(--text-dim)";if(b?.contextWindow){let o=b.percent;null!==o&&o>90?r="#ef4444":null!==o&&o>70&&(r="rgba(234,179,8,0.9)");let i=null!==o?`${o.toFixed(0)}% / ${er(b.contextWindow)}`:`? / ${er(b.contextWindow)}`;e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3,color:r},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M1 9 L1 5 Q1 1 5 1 Q9 1 9 5 L9 9"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"9",y2:"9"})]}),i]},"ctx")),t.push(`context: ${null!==o?o.toFixed(1)+"%":"unknown"} of ${b.contextWindow.toLocaleString()} tokens`)}return e.length?(0,n.jsx)("div",{title:t.join(" | "),style:{display:"flex",alignItems:"center",gap:7,fontSize:11,color:"var(--text-dim)",whiteSpace:"nowrap",cursor:"default"},children:e}):null})()}),(0,n.jsxs)("div",{style:{flex:"0 0 auto",display:"flex",alignItems:"center",gap:6,marginLeft:"auto"},children:[f&&(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:4},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-dim)"},children:"tools"}),(0,n.jsx)("div",{style:{display:"flex",alignItems:"center",border:"1px solid var(--border)",borderRadius:5,overflow:"hidden"},children:en.map((e,t)=>{let r=eo[e],o=(g??"default")===r,i=t>0?eo[en[t-1]]:null;return(0,n.jsx)("button",{onClick:()=>!l&&f(r),disabled:l,title:"off"===e?"无工具,纯聊天,不浪费一个 token":"default"===e?"4 项内置工具":"使用全部内置工具",style:{padding:"4px 10px",background:o?"var(--bg-selected)":"none",border:"none",borderLeft:t>0?`1px solid ${o||null!==i&&(g??"default")===i?"transparent":"var(--border)"}`:"none",color:o?"var(--accent)":"var(--text-dim)",cursor:l?"not-allowed":"pointer",fontSize:12,fontWeight:o?600:400,opacity:l?.5:1},children:e},e)})})]}),u&&(0,n.jsxs)("div",{style:{position:"relative"},children:[h&&(0,n.jsx)("div",{style:{position:"absolute",bottom:"calc(100% + 6px)",right:0,background:"#1f2937",color:"#f87171",fontSize:11,padding:"4px 8px",borderRadius:5,whiteSpace:"nowrap",pointerEvents:"none",boxShadow:"0 2px 8px rgba(0,0,0,0.2)",zIndex:50},children:h}),(0,n.jsx)("button",{onClick:x?p:u,disabled:l&&!x,style:{display:"flex",alignItems:"center",gap:4,padding:"4px 10px",background:"none",border:`1px solid ${x?"rgba(239,68,68,0.4)":"var(--border)"}`,borderRadius:5,color:x?"#ef4444":"var(--text-muted)",cursor:l&&!x?"not-allowed":"pointer",fontSize:12,opacity:l&&!x?.5:1,transition:"border-color 0.12s, color 0.12s"},onMouseEnter:e=>{(!l||x)&&(e.currentTarget.style.borderColor=x?"rgba(239,68,68,0.7)":"rgba(37,99,235,0.4)",e.currentTarget.style.color=x?"#ef4444":"var(--accent)")},onMouseLeave:e=>{e.currentTarget.style.borderColor=x?"rgba(239,68,68,0.4)":"var(--border)",e.currentTarget.style.color=x?"#ef4444":"var(--text-muted)"},title:x?"停止压缩":"压缩上下文",children:x?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",children:(0,n.jsx)("rect",{x:"2",y:"2",width:"6",height:"6",rx:"1",fill:"currentColor"})}),"Compacting…"]}):(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"4 14 10 14 10 20"}),(0,n.jsx)("polyline",{points:"20 10 14 10 14 4"}),(0,n.jsx)("line",{x1:"10",y1:"14",x2:"3",y2:"21"}),(0,n.jsx)("line",{x1:"21",y1:"3",x2:"14",y2:"10"})]}),"Compact"]})})]})]})]})]})})}function el(e){let t=e,r=0;for(;1===t.children.length;)t=t.children[0],r++;return{node:t,skipped:r}}function es({node:e,activePathIds:t,depth:r,isLast:o,parentLines:i,onSelect:l}){let{node:s,skipped:a}=el(e),d=t.has(s.entry.id),c=t.has(e.entry.id)||t.has(s.entry.id),u=function(e){if("message"===e.type&&"message"in e){let t=e.message,r=t.content,n="";if("string"==typeof r?n=r:Array.isArray(r)&&(n=r.filter(e=>"text"===e.type).map(e=>e.text).join(" ")),n.length>40&&(n=n.slice(0,40)+"…"),n)return n;if("assistant"===t.role)return"[assistant]"}return e.type}(s.entry),p="message"===s.entry.type&&"message"in s.entry?s.entry.message.role:null;return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",height:24,cursor:"pointer"},onClick:()=>l(s.entry.id),children:[i.map((e,t)=>(0,n.jsx)("div",{style:{width:16,flexShrink:0,position:"relative",height:"100%",alignSelf:"stretch"},children:e&&(0,n.jsx)("div",{style:{position:"absolute",left:7,top:0,bottom:0,width:1,background:"var(--border)"}})},t)),(0,n.jsxs)("div",{style:{width:16,flexShrink:0,position:"relative",height:"100%",alignSelf:"stretch"},children:[(0,n.jsx)("div",{style:{position:"absolute",left:7,top:0,bottom:o?"50%":0,width:1,background:"var(--border)"}}),(0,n.jsx)("div",{style:{position:"absolute",left:7,top:"50%",width:9,height:1,background:"var(--border)"}})]}),(0,n.jsx)("div",{style:{width:7,height:7,borderRadius:"50%",flexShrink:0,background:d?"var(--accent)":c?"var(--text-muted)":"var(--border)",border:d?"none":"1px solid var(--text-dim)",marginRight:6,transition:"background 0.12s"}}),p&&(0,n.jsx)("span",{style:{fontSize:9,fontFamily:"var(--font-mono)",color:"user"===p?"var(--accent)":"var(--text-dim)",background:"user"===p?"rgba(37,99,235,0.08)":"var(--bg-hover)",border:`1px solid ${"user"===p?"rgba(37,99,235,0.2)":"var(--border)"}`,borderRadius:3,padding:"0 4px",marginRight:5,flexShrink:0,lineHeight:"16px"},children:"user"===p?"U":"A"}),a>0&&(0,n.jsxs)("span",{style:{fontSize:10,color:"var(--text-dim)",marginRight:5,flexShrink:0},children:["+",a]}),(0,n.jsx)("span",{style:{fontSize:11,color:d?"var(--text)":c?"var(--text-muted)":"var(--text-dim)",fontWeight:d?500:400,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0},children:u})]}),s.children.map((e,a)=>(0,n.jsx)(es,{node:e,activePathIds:t,depth:r+1,isLast:a===s.children.length-1,parentLines:[...i,!o],onSelect:l},e.entry.id))]})}function ea({tree:e,activeLeafId:t,onLeafChange:r}){let[i,l]=(0,o.useState)(!1),s=(0,o.useMemo)(()=>t?new Set(function e(r,n){for(let o of r){let r=[...n,o.entry.id];if(o.entry.id===t)return r;let i=e(o.children,r);if(i)return i}return null}(e,[])??[]):new Set,[e,t]),a=(0,o.useCallback)(e=>{r(e)},[r]);if(!function e(t){for(let r of t)if(r.children.length>1||e(r.children))return!0;return!1}(e))return null;let{node:d}=e.length>0?el(e[0]):{node:e[0]};return!d||d.children.length<=1?null:(0,n.jsxs)("div",{style:{borderBottom:"1px solid var(--border)",background:"var(--bg)",flexShrink:0,position:"relative"},children:[(0,n.jsxs)("button",{onClick:()=>l(e=>!e),style:{display:"flex",alignItems:"center",gap:6,width:"100%",padding:"5px 12px",background:"none",border:"none",cursor:"pointer",color:"var(--text-muted)",fontSize:11,textAlign:"left"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{color:"var(--accent)",flexShrink:0},children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),(0,n.jsx)("span",{style:{color:"var(--text-muted)"},children:"Branches"}),(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{marginLeft:2,transform:i?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),i&&(0,n.jsx)("div",{style:{position:"absolute",top:"100%",left:0,right:0,padding:"4px 12px 8px 12px",maxHeight:260,overflowY:"auto",background:"var(--bg)",borderBottom:"1px solid var(--border)",boxShadow:"0 4px 12px rgba(0,0,0,0.1)",zIndex:100},children:d.children.map((e,t)=>(0,n.jsx)(es,{node:e,activePathIds:s,depth:0,isLast:t===d.children.length-1,parentLines:[],onSelect:a},e.entry.id))})]})}function ed(e){if("user"===e.role){let t=e.content;return"string"==typeof t?t.slice(0,200):Array.isArray(t)?t.filter(e=>"text"===e.type&&e.text).map(e=>e.text).join("\n").slice(0,200):""}if("assistant"===e.role){let t=e.content??[],r=t.filter(e=>"text"===e.type).map(e=>e.text).join(" ");if(r)return r.slice(0,200);let n=t.filter(e=>"toolCall"===e.type).map(e=>e.toolName);if(n.length)return n.join(", ")}return""}function ec(e){return"user"===e.role?{bg:"rgba(37,99,235,0.18)",border:"rgba(37,99,235,0.7)"}:{bg:"rgba(107,114,128,0.12)",border:"rgba(107,114,128,0.5)"}}function eu({messages:e,streamingMessage:t,scrollContainer:r,messageRefs:i}){let[l,s]=(0,o.useState)(0),[a,d]=(0,o.useState)(1),[c,u]=(0,o.useState)(!1),[p,x]=(0,o.useState)([]),[h,g]=(0,o.useState)(!1),[f,v]=(0,o.useState)(null),y=(0,o.useRef)(!1),b=(0,o.useRef)(null),m=(0,o.useMemo)(()=>t?[...e,t]:e,[e,t]),j=(0,o.useCallback)(()=>{let e=r.current;if(!e)return;let t=e.scrollHeight,n=e.clientHeight,o=t-n;u(o>20),o<=0?(s(0),d(1)):(s(e.scrollTop/o),d(n/t));let l=i.current,a=[],c=0;for(let r=0;r<m.length;r++){let n=m[r];if("user"!==n.role&&"assistant"!==n.role||!("user"===n.role||"assistant"===n.role&&(n.content??[]).some(e=>"text"===e.type)))continue;let o=l?.[c];if(c++,o&&t>0){let r=o.getBoundingClientRect(),i=e.getBoundingClientRect(),l=r.top-i.top+e.scrollTop,s=r.height;a.push({topRatio:l/t,heightRatio:s/t,msg:n,index:a.length})}}x(a)},[r,i,m]);(0,o.useEffect)(()=>{let e=r.current;if(!e)return;e.addEventListener("scroll",j,{passive:!0});let t=new ResizeObserver(j);return t.observe(e),e.firstElementChild&&t.observe(e.firstElementChild),j(),()=>{e.removeEventListener("scroll",j),t.disconnect()}},[r,j]),(0,o.useEffect)(()=>{let e=setTimeout(j,50);return()=>clearTimeout(e)},[e.length,t,j]);let k=(0,o.useCallback)(e=>{let t=r.current;if(!t)return;let n=t.scrollHeight-t.clientHeight;n<=0||(t.scrollTop=Math.max(0,Math.min(1-a,e))/(1-a)*n)},[r,a]),w=(0,o.useCallback)(e=>{if(!c)return;y.current=!0;let t=e.currentTarget.getBoundingClientRect(),r=(e.clientY-t.top)/t.height,n=r-l*(1-a),o=n>=0&&n<=a?n:a/2;k(r-o);let i=e=>{y.current&&k((e.clientY-t.top)/t.height-o)},s=()=>{y.current=!1,window.removeEventListener("mousemove",i),window.removeEventListener("mouseup",s)};window.addEventListener("mousemove",i),window.addEventListener("mouseup",s)},[c,a,l,k]),S=b.current?.clientHeight??600,C=(0,o.useMemo)(()=>{if(!h||0===p.length)return[];let e=p.map(e=>Math.round(e.topRatio*S-11));for(let t=0;t<10;t++){for(let t=1;t<e.length;t++){let r=e[t-1]+22+2;e[t]<r&&(e[t]=r)}for(let t=e.length-2;t>=0;t--){let r=e[t+1]-22-2;e[t]>r&&(e[t]=r)}}for(let t=0;t<e.length;t++)e[t]=Math.max(0,Math.min(S-22,e[t]));return e},[h,p,S]);if(!c)return null;let L=l*(1-a)*100,T=100*a,z=null!==f&&p.length>0?p.reduce((e,t)=>Math.abs(t.topRatio-f)<Math.abs(p[e].topRatio-f)?t.index:e,0):null;return(0,n.jsxs)("div",{ref:b,onMouseDown:w,onMouseEnter:()=>g(!0),onMouseLeave:()=>{g(!1),v(null)},onMouseMove:e=>{let t=e.currentTarget.getBoundingClientRect();v((e.clientY-t.top)/t.height)},style:{width:36,flexShrink:0,position:"relative",cursor:"default",userSelect:"none",borderLeft:"1px solid var(--border)",background:"var(--bg-panel)",overflow:"visible"},children:[(0,n.jsx)("div",{style:{position:"absolute",left:0,right:0,top:`${L}%`,height:`${T}%`,background:"rgba(100,100,100,0.1)",borderTop:"1px solid rgba(100,100,100,0.2)",borderBottom:"1px solid rgba(100,100,100,0.2)",pointerEvents:"none",zIndex:1}}),p.map(e=>{let t=ec(e.msg),r=h&&z===e.index;ed(e.msg);let o="user"===e.msg.role,i=100*e.topRatio;return(0,n.jsx)("div",{style:{position:"absolute",top:`${i}%`,transform:"translateY(-50%)",left:0,right:0,height:"12px",display:"flex",alignItems:"center",justifyContent:"center",cursor:"pointer",zIndex:2},children:(0,n.jsx)("div",{style:{width:o?8:6,height:o?8:6,borderRadius:o?2:"50%",background:t.bg,border:`1.5px solid ${t.border}`,flexShrink:0,transition:"transform 0.1s",transform:r?"scale(1.6)":"scale(1)"}})},e.index)}),(0,n.jsx)("div",{style:{position:"absolute",left:"50%",top:0,bottom:0,width:1,background:"var(--border)",transform:"translateX(-50%)",zIndex:0}}),h&&p.map((e,t)=>{let r=ed(e.msg),o=ec(e.msg),i=z===e.index;return r&&0!==C.length?(0,n.jsx)("div",{style:{position:"absolute",top:C[t],right:"100%",marginRight:6,background:"var(--bg)",borderTop:`1px solid ${i?o.border:"var(--border)"}`,borderRight:`1px solid ${i?o.border:"var(--border)"}`,borderBottom:`1px solid ${i?o.border:"var(--border)"}`,borderLeft:`2px solid ${o.border}`,borderRadius:4,padding:"2px 7px",width:200,zIndex:100,pointerEvents:"none",opacity:i?1:.45,transition:"top 0.1s, opacity 0.1s"},children:(0,n.jsx)("div",{style:{fontSize:11,color:i?"var(--text)":"var(--text-muted)",lineHeight:1.4,whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis"},children:r})},e.index):null})]})}function ep(e,t){switch(t.type){case"start":return{isStreaming:!0,streamingMessage:null};case"update":return{isStreaming:!0,streamingMessage:t.message};case"end":case"reset":return{isStreaming:!1,streamingMessage:null};default:return e}}function ex({session:e,newSessionCwd:t,onAgentEnd:i,onSessionCreated:l,onSessionForked:s}){var a;let d,c=null===e&&null!==t,[u,p]=(0,o.useState)(null),[x,h]=(0,o.useState)(!c),[g,f]=(0,o.useState)(null),[v,y]=(0,o.useState)(null),[b,m]=(0,o.useState)([]),[j,k]=(0,o.useState)([]),[w,S]=(0,o.useReducer)(ep,{isStreaming:!1,streamingMessage:null}),[C,L]=(0,o.useState)(!1),[T,z]=(0,o.useState)({}),[I,M]=(0,o.useState)([]),[R,W]=(0,o.useState)(null),[E,B]=(0,o.useState)("default"),[$,O]=(0,o.useState)(null),[P,D]=(0,o.useState)(null),F=(0,o.useRef)(null),H=(0,o.useRef)(null),N=(a=b.filter(e=>"user"===e.role||"assistant"===e.role).length,(d=(0,o.useRef)([])).current=Array(a).fill(null).map((e,t)=>d.current[t]??null),d),A=(0,o.useRef)(null),_=(0,o.useRef)(e?.id??null),V=(0,o.useRef)(!1),J=(0,o.useRef)(!1),Z=(0,o.useRef)(null),K=(0,o.useRef)(!1),q=(0,o.useCallback)((e="smooth")=>{F.current?.scrollIntoView({behavior:e})},[]),X=(0,o.useCallback)(()=>{let e=H.current,t=Z.current;if(!e||!t)return;let r=t.getBoundingClientRect().top-e.getBoundingClientRect().top+e.scrollTop;e.scrollTo({top:r-16,behavior:"smooth"})},[]),Q=(0,o.useCallback)(async(e,t=!1)=>{try{t&&h(!0);let r=await fetch(`/api/sessions/${encodeURIComponent(e)}`);if(404===r.status){t&&(p(null),y(null),m([]),f(null));return}if(!r.ok)throw Error(`HTTP ${r.status}`);let n=await r.json();p(n),y(n.leafId),m(n.context.messages),k(n.context.entryIds??[]),ef(null),f(null)}catch(e){f(String(e))}finally{t&&h(!1)}},[]),G=(0,o.useMemo)(()=>{let e={input:0,output:0,cacheRead:0,cacheWrite:0};for(let t of b){if("assistant"!==t.role)continue;let r=t.usage;r&&(e.input+=r.input??0,e.output+=r.output??0,e.cacheRead+=r.cacheRead??0,e.cacheWrite+=r.cacheWrite??0)}return e.input+e.output+e.cacheRead+e.cacheWrite>0?{tokens:e}:null},[b]),ee=(0,o.useCallback)(async(e,t)=>{try{let r=t?`/api/sessions/${encodeURIComponent(e)}/context?leafId=${encodeURIComponent(t)}`:`/api/sessions/${encodeURIComponent(e)}/context`,n=await fetch(r);if(!n.ok)throw Error(`HTTP ${n.status}`);let o=await n.json();m(o.context.messages)}catch(e){console.error("Failed to load context:",e)}},[]),et=(0,o.useCallback)(e=>{A.current&&(A.current.close(),A.current=null);let t=new EventSource(`/api/agent/${encodeURIComponent(e)}/events`);A.current=t,t.onmessage=e=>{try{let t=JSON.parse(e.data);er(t)}catch{}},t.onerror=()=>{}},[]),er=(0,o.useCallback)(e=>{switch(e.type){case"agent_start":L(!0),S({type:"start"});break;case"agent_end":L(!1),O(null),J.current=!1,S({type:"end"}),_.current&&(Q(_.current),fetch(`/api/agent/${encodeURIComponent(_.current)}`).then(e=>e.json()).then(e=>{e.state?.contextUsage!==void 0&&D(e.state.contextUsage??null)}).catch(()=>{})),i?.();break;case"message_update":{let t=e.message;t&&S({type:"update",message:U(t)});break}case"message_end":{let t=e.message;t&&m(e=>[...e,U(t)]),S({type:"reset"});break}case"auto_retry_start":O({attempt:e.attempt,maxAttempts:e.maxAttempts});break;case"auto_retry_end":O(null);break;case"auto_compaction_start":em(!0),ek(null);break;case"auto_compaction_end":em(!1),e.errorMessage?ek(e.errorMessage):!e.aborted&&_.current&&Q(_.current)}},[Q,i]);(0,o.useEffect)(()=>(fetch("/api/models").then(e=>e.json()).then(e=>{if(z(e.models),e.modelList&&(M(e.modelList),c&&e.modelList.length>0)){let t=e.defaultModel,r=t&&e.modelList.find(e=>e.id===t.modelId&&e.provider===t.provider);W(r?{provider:r.provider,modelId:r.id}:{provider:e.modelList[0].provider,modelId:e.modelList[0].id})}}).catch(()=>{}),e&&(_.current=e.id,Q(e.id,!0),eI(e.id),fetch(`/api/agent/${encodeURIComponent(e.id)}`).then(e=>e.json()).then(t=>{t.running&&t.state?.isStreaming&&(L(!0),et(e.id)),t.state&&(void 0!==t.state.isCompacting&&em(t.state.isCompacting),void 0!==t.state.contextUsage&&D(t.state.contextUsage??null))}).catch(()=>{})),()=>{A.current?.close(),A.current=null}),[]),(0,o.useEffect)(()=>{b.length>0&&(K.current?(K.current=!1,V.current=!0,X()):V.current?J.current||q("smooth"):(V.current=!0,q("instant")))},[b.length,q,X]);let en=(0,o.useCallback)(async e=>{y(e);let t=_.current;t&&(await ee(t,e),e&&fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"navigate_tree",targetId:e})}).catch(()=>{}))},[ee]),eo=(0,o.useCallback)(async n=>{if(!n.trim()||C)return;let o={role:"user",content:n,timestamp:Date.now()};m(e=>[...e,o]),L(!0),S({type:"start"}),J.current=!0,K.current=!0;try{if(c&&t){let{PRESET_NONE:e,PRESET_DEFAULT:o,PRESET_FULL:i}=await r.e(451).then(r.bind(r,8451)),s="none"===E?e:"default"===E?o:i,a=await fetch("/api/agent/new",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({cwd:t,type:"prompt",message:n,toolNames:s,...R?{provider:R.provider,modelId:R.modelId}:{}})});if(!a.ok)throw Error(`HTTP ${a.status}`);let d=(await a.json()).sessionId;_.current=d,et(d),l?.({id:d,path:"",cwd:t,name:void 0,created:new Date().toISOString(),modified:new Date().toISOString(),messageCount:1,firstMessage:n})}else if(e){et(e.id);let t=await fetch(`/api/agent/${encodeURIComponent(e.id)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"prompt",message:n})});if(!t.ok)throw Error(`HTTP ${t.status}`)}}catch(e){console.error("Failed to send message:",e),L(!1),J.current=!1,S({type:"end"})}},[c,t,R,E,e,C,X,et,l]),el=(0,o.useCallback)(async()=>{let e=_.current;if(e)try{await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"abort"})})}catch(e){console.error("Failed to abort:",e)}},[]),[es,ed]=(0,o.useState)(null),ec=(0,o.useCallback)(async e=>{let t=_.current;if(t){ed(e);try{let r=await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"fork",entryId:e})});if(!r.ok)throw Error(`HTTP ${r.status}`);let{cancelled:n,newSessionId:o}=(await r.json()).data??{};!n&&o&&s?.(o)}catch(e){console.error("Fork failed:",e)}finally{ed(null)}}},[s]),eh=(0,o.useCallback)(async e=>{let t=_.current;t&&(fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"navigate_tree",targetId:e})}).catch(()=>{}),y(e),await ee(t,e))},[ee]),[eg,ef]=(0,o.useState)(null),ev=eg??u?.context.model??null,ey=c?R:ev,[eb,em]=(0,o.useState)(!1),[ej,ek]=(0,o.useState)(null);(0,o.useEffect)(()=>{if(!ej)return;let e=setTimeout(()=>ek(null),3e3);return()=>clearTimeout(e)},[ej]);let ew=(0,o.useCallback)(e=>{B(e)},[]),eS=(0,o.useCallback)(async(e,t)=>{if(c)return void W({provider:e,modelId:t});let r=_.current;if(r)try{await fetch(`/api/agent/${encodeURIComponent(r)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"set_model",provider:e,modelId:t})}),ef({provider:e,modelId:t})}catch(e){console.error("Failed to set model:",e)}},[c]),eC=(0,o.useCallback)(async()=>{let e=_.current;if(e&&!eb){em(!0),ek(null);try{let t=await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"compact"})}),r=await t.json();if(!t.ok||r.error)return void ek(r.error??`HTTP ${t.status}`);await Q(e,!0)}catch(e){ek(String(e))}finally{em(!1)}}},[eb,Q]),eL=(0,o.useCallback)(async e=>{let t=_.current;if(t)try{await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"steer",message:e})})}catch(e){console.error("Failed to steer:",e)}},[]),eT=(0,o.useCallback)(async e=>{let t=_.current;if(t)try{await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"follow_up",message:e})})}catch(e){console.error("Failed to follow up:",e)}},[]),ez=(0,o.useCallback)(async()=>{let e=_.current;if(e)try{await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"abort_compaction"})})}catch(e){console.error("Failed to abort compaction:",e)}},[]),eI=(0,o.useCallback)(async e=>{try{let t=await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"get_tools"})});if(!t.ok)return;let n=await t.json();if(n.data){let{getPresetFromTools:e}=await r.e(451).then(r.bind(r,8451));ew(e(n.data))}}catch(e){console.error("Failed to load tools:",e)}},[ew]),eM=(0,o.useCallback)(async e=>{let{PRESET_NONE:t,PRESET_DEFAULT:n,PRESET_FULL:o}=await r.e(451).then(r.bind(r,8451)),i="none"===e?t:"default"===e?n:o;ew(e);let l=_.current;if(l)try{await fetch(`/api/agent/${encodeURIComponent(l)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"set_tools",toolNames:i})})}catch(e){console.error("Failed to set tools:",e)}},[ew]);return x?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)"},children:"Loading session..."}):g?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"#f87171"},children:g}):(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[u&&u.tree.length>0&&(0,n.jsx)(ea,{tree:u.tree,activeLeafId:v,onLeafChange:en}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsx)("div",{ref:H,style:{flex:1,overflowY:"auto",padding:"16px 0",scrollbarWidth:"none"},children:(0,n.jsxs)("div",{style:{maxWidth:820,margin:"0 auto",padding:"0 16px"},children:[(()=>{let e=new Map;for(let t of b)"toolResult"===t.role&&e.set(t.toolCallId,t);let t=-1;for(let e=b.length-1;e>=0;e--)if("user"===b[e].role){t=e;break}let r=0;return b.map((o,i)=>{let l="user"===o.role&&i>0&&"assistant"===b[i-1].role?j[i-1]:void 0,s="user"===o.role||"assistant"===o.role,a=s?r++:-1,d=(0,n.jsx)(Y,{message:o,toolResults:e,modelNames:T,entryId:j[i],onFork:C||c||0===i&&"user"===o.role?void 0:ec,forking:es===j[i],onNavigate:C?void 0:eh,prevAssistantEntryId:C?void 0:l},i);return s?(0,n.jsx)("div",{ref:e=>{N.current[a]=e,i===t&&(Z.current=e)},children:d},i):d})})(),w.isStreaming&&w.streamingMessage&&(0,n.jsx)(Y,{message:w.streamingMessage,isStreaming:!0,modelNames:T}),w.isStreaming&&!w.streamingMessage&&(0,n.jsx)("div",{style:{padding:"8px 0",color:"var(--text-muted)",fontSize:13},children:(0,n.jsx)("span",{style:{animation:"pulse 1.5s infinite"},children:"Thinking..."})}),C&&(0,n.jsx)("div",{style:{height:H.current?H.current.clientHeight:"80vh"}}),(0,n.jsx)("div",{ref:F})]})}),(0,n.jsx)(eu,{messages:b,streamingMessage:w.streamingMessage,scrollContainer:H,messageRefs:N})]}),(0,n.jsx)("div",{style:{position:"relative"},children:(0,n.jsx)(ei,{onSend:eo,onAbort:el,onSteer:C?eL:void 0,onFollowUp:C?eT:void 0,isStreaming:C,model:ey,modelNames:T,modelList:I,onModelChange:eS,onCompact:e||c?eC:void 0,onAbortCompaction:ez,isCompacting:eb,compactError:ej,toolPreset:E,onToolPresetChange:e||c?eM:void 0,sessionStats:G,retryInfo:$,contextUsage:P})})]})}function eh({filePath:e}){var t;let[r,i]=(0,o.useState)(null),[l,s]=(0,o.useState)(!0),[a,d]=(0,o.useState)(null),[c,u]=(0,o.useState)(!1);if((0,o.useEffect)(()=>{s(!0),d(null),i(null),u(!1);let t=e.split("/").filter(Boolean).join("/");fetch(`/api/files/${t}?type=read`).then(e=>e.json()).then(e=>{e.error?d(e.error):i(e)}).catch(e=>d(String(e))).finally(()=>s(!1))},[e]),l)return(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)",fontSize:13},children:"Loading..."});if(a)return(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"#f87171",fontSize:13},children:a});if(!r)return null;let p="html"===r.language,x=r.content.split("\n");return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12,padding:"4px 16px",borderBottom:"1px solid var(--border)",fontSize:11,color:"var(--text-dim)",background:"var(--bg)",flexShrink:0},children:[(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)"},children:e}),(0,n.jsx)("span",{style:{marginLeft:"auto"},children:r.language}),!c&&(0,n.jsxs)("span",{children:[x.length," lines"]}),(0,n.jsx)("span",{children:(t=r.size)<1024?`${t} B`:t<1048576?`${(t/1024).toFixed(1)} KB`:`${(t/1048576).toFixed(1)} MB`}),p&&(0,n.jsxs)("div",{style:{display:"flex",borderRadius:5,overflow:"hidden",border:"1px solid var(--border)"},children:[(0,n.jsx)("button",{onClick:()=>u(!1),style:{padding:"2px 8px",fontSize:11,border:"none",cursor:"pointer",background:c?"var(--bg-hover)":"var(--bg-selected)",color:c?"var(--text-muted)":"var(--text)",fontWeight:c?400:600},children:"Source"}),(0,n.jsx)("button",{onClick:()=>u(!0),style:{padding:"2px 8px",fontSize:11,border:"none",borderLeft:"1px solid var(--border)",cursor:"pointer",background:c?"var(--bg-selected)":"var(--bg-hover)",color:c?"var(--text)":"var(--text-muted)",fontWeight:c?600:400},children:"Preview"})]})]}),p&&c?(0,n.jsx)("iframe",{srcDoc:r.content,sandbox:"allow-scripts",style:{flex:1,border:"none",background:"#fff"},title:"HTML preview"}):(0,n.jsx)("div",{style:{flex:1,overflow:"auto",background:"var(--bg)"},children:(0,n.jsx)("table",{style:{borderCollapse:"collapse",width:"100%",fontFamily:"var(--font-mono)",fontSize:13,lineHeight:1.6},children:(0,n.jsx)("tbody",{children:x.map((e,t)=>(0,n.jsxs)("tr",{style:{verticalAlign:"top"},children:[(0,n.jsx)("td",{style:{padding:"0 12px 0 16px",textAlign:"right",color:"var(--text-dim)",userSelect:"none",minWidth:48,fontSize:12,lineHeight:1.6,borderRight:"1px solid var(--border)",background:"var(--bg-panel)"},children:t+1}),(0,n.jsx)("td",{style:{padding:"0 16px",whiteSpace:"pre",color:"var(--text)",width:"100%"},children:e||"\xa0"})]},t))})})})]})}function eg(){return(0,n.jsx)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"})})}function ef({tabs:e,activeTabId:t,onSelectTab:r,onCloseTab:i}){let[l,s]=(0,o.useState)(null);return(0,n.jsx)("div",{style:{display:"flex",alignItems:"flex-end",background:"var(--bg-panel)",overflowX:"auto",flexShrink:0,height:36},children:e.map(e=>{let o=e.id===t;return(0,n.jsxs)("div",{onClick:()=>r(e.id),style:{display:"flex",alignItems:"center",gap:6,height:36,paddingLeft:12,paddingRight:"chat"===e.type?12:6,borderRight:"1px solid var(--border)",background:o?"var(--bg)":"var(--bg-panel)",cursor:"pointer",fontSize:12,color:o?"var(--text)":"var(--text-muted)",whiteSpace:"nowrap",maxWidth:180,minWidth:80,flexShrink:0,userSelect:"none",transition:"background 0.1s, color 0.1s"},children:[(0,n.jsx)("span",{style:{flexShrink:0,opacity:o?1:.7,display:"flex",alignItems:"center"},children:"chat"===e.type?(0,n.jsx)(eg,{}):E(e.label,13)}),(0,n.jsx)("span",{style:{overflow:"hidden",textOverflow:"ellipsis",flex:1,fontWeight:o?500:400},title:e.filePath??e.label,children:e.label}),"file"===e.type&&(0,n.jsx)("button",{onClick:t=>{t.stopPropagation(),i(e.id)},onMouseEnter:()=>s(e.id),onMouseLeave:()=>s(null),style:{display:"flex",alignItems:"center",justifyContent:"center",width:16,height:16,background:l===e.id?"var(--bg-hover)":"transparent",border:"none",borderRadius:3,color:l===e.id?"var(--text)":"var(--text-dim)",cursor:"pointer",padding:0,flexShrink:0,transition:"background 0.1s, color 0.1s"},title:"Close",children:(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"2",x2:"8",y2:"8"}),(0,n.jsx)("line",{x1:"8",y1:"2",x2:"2",y2:"8"})]})})]},e.id)})})}let ev=["openai-completions","openai-responses","anthropic-messages","google-generative-ai"];function ey({label:e,children:t}){return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[(0,n.jsx)("label",{style:{fontSize:11,color:"var(--text-muted)",fontWeight:500},children:e}),t]})}let eb={padding:"6px 9px",background:"var(--bg-panel)",border:"1px solid var(--border)",borderRadius:5,color:"var(--text)",fontSize:12,outline:"none",width:"100%",boxSizing:"border-box"};function em({value:e,onChange:t,placeholder:r,mono:o}){return(0,n.jsx)("input",{value:e,onChange:e=>t(e.target.value),placeholder:r,style:{...eb,fontFamily:o?"var(--font-mono)":"inherit"}})}function ej({value:e,onChange:t,placeholder:r}){return(0,n.jsx)("input",{type:"number",value:e,onChange:e=>t(e.target.value),placeholder:r,style:eb})}function ek({value:e,onChange:t,options:r}){return(0,n.jsxs)("select",{value:e,onChange:e=>t(e.target.value),style:{...eb,color:e?"var(--text)":"var(--text-dim)"},children:[(0,n.jsx)("option",{value:"",children:"— inherit / none —"}),r.map(e=>(0,n.jsx)("option",{value:e,children:e},e))]})}function ew({label:e,checked:t,onChange:r}){return(0,n.jsxs)("label",{style:{display:"flex",alignItems:"center",gap:6,cursor:"pointer",fontSize:12,color:"var(--text-muted)"},children:[(0,n.jsx)("input",{type:"checkbox",checked:t,onChange:e=>r(e.target.checked),style:{width:13,height:13,accentColor:"var(--accent)",cursor:"pointer"}}),e]})}function eS({children:e}){return(0,n.jsx)("div",{style:{fontSize:11,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.06em",marginBottom:2},children:e})}function eC({name:e,provider:t,onChange:r,onRename:i,onDelete:l}){let[s,a]=(0,o.useState)(e);(0,o.useEffect)(()=>a(e),[e]);let d=(e,n)=>r({...t,[e]:n});return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[(0,n.jsx)(eS,{children:"Provider"}),(0,n.jsx)("button",{onClick:l,style:{padding:"3px 8px",background:"none",border:"1px solid rgba(239,68,68,0.3)",borderRadius:4,color:"#ef4444",cursor:"pointer",fontSize:11},children:"Delete"})]}),(0,n.jsxs)(ey,{label:"Provider name",children:[(0,n.jsx)(em,{value:s,onChange:a,placeholder:"provider-name",mono:!0}),s!==e&&s.trim()&&(0,n.jsx)("button",{onClick:()=>i(s.trim()),style:{marginTop:4,padding:"3px 10px",background:"var(--accent)",border:"none",borderRadius:4,color:"#fff",cursor:"pointer",fontSize:11,alignSelf:"flex-start"},children:"Rename"})]}),(0,n.jsx)(ey,{label:"Base URL",children:(0,n.jsx)(em,{value:t.baseUrl??"",onChange:e=>d("baseUrl",e||void 0),placeholder:"https://api.example.com/v1",mono:!0})}),(0,n.jsxs)(ey,{label:"API Key",children:[(0,n.jsx)(em,{value:t.apiKey??"",onChange:e=>d("apiKey",e||void 0),placeholder:"ENV_VAR_NAME, !shell-command, or literal key",mono:!0}),(0,n.jsxs)("span",{style:{fontSize:10,color:"var(--text-dim)",marginTop:2},children:["Prefix with ",(0,n.jsx)("code",{style:{fontFamily:"var(--font-mono)"},children:"!"})," to run a shell command, or use an env var name"]})]}),(0,n.jsx)(ey,{label:"API",children:(0,n.jsx)(ek,{value:t.api??"",onChange:e=>d("api",e||void 0),options:ev})})]})}function eL({model:e,onChange:t,onDelete:r}){let o=(r,n)=>t({...e,[r]:n});return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[(0,n.jsx)(eS,{children:"Model"}),(0,n.jsx)("button",{onClick:r,style:{padding:"3px 8px",background:"none",border:"1px solid rgba(239,68,68,0.3)",borderRadius:4,color:"#ef4444",cursor:"pointer",fontSize:11},children:"Remove"})]}),(0,n.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10},children:[(0,n.jsx)(ey,{label:"ID *",children:(0,n.jsx)(em,{value:e.id,onChange:e=>o("id",e),placeholder:"model-id",mono:!0})}),(0,n.jsx)(ey,{label:"Name",children:(0,n.jsx)(em,{value:e.name??"",onChange:e=>o("name",e||void 0),placeholder:"Display name"})})]}),(0,n.jsx)(ey,{label:"API override",children:(0,n.jsx)(ek,{value:e.api??"",onChange:e=>o("api",e||void 0),options:ev})}),(0,n.jsxs)("div",{style:{display:"flex",gap:20,flexWrap:"wrap"},children:[(0,n.jsx)(ew,{label:"Reasoning / thinking",checked:e.reasoning??!1,onChange:e=>o("reasoning",e||void 0)}),(0,n.jsx)(ew,{label:"Image input",checked:e.input?.includes("image")??!1,onChange:e=>o("input",e?["text","image"]:void 0)})]}),(0,n.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10},children:[(0,n.jsx)(ey,{label:"Context window (tokens)",children:(0,n.jsx)(ej,{value:void 0!==e.contextWindow?String(e.contextWindow):"",onChange:e=>o("contextWindow",e?parseInt(e):void 0),placeholder:"128000"})}),(0,n.jsx)(ey,{label:"Max output tokens",children:(0,n.jsx)(ej,{value:void 0!==e.maxTokens?String(e.maxTokens):"",onChange:e=>o("maxTokens",e?parseInt(e):void 0),placeholder:"16384"})})]}),(0,n.jsxs)("div",{children:[(0,n.jsx)(eS,{children:"Cost (per million tokens)"}),(0,n.jsx)("div",{style:{marginTop:8,display:"grid",gridTemplateColumns:"1fr 1fr 1fr 1fr",gap:8},children:["input","output","cacheRead","cacheWrite"].map(r=>(0,n.jsx)(ey,{label:r,children:(0,n.jsx)(ej,{value:e.cost?.[r]!==void 0?String(e.cost[r]):"",onChange:n=>{let o;return o=parseFloat(n),void t({...e,cost:{...e.cost??{},[r]:isNaN(o)?void 0:o}})},placeholder:"0"})},r))})]})]})}function eT({onClose:e}){let[t,r]=(0,o.useState)({providers:{}}),[i,l]=(0,o.useState)(!0),[s,a]=(0,o.useState)(!1),[d,c]=(0,o.useState)(null),[u,p]=(0,o.useState)(!1),[x,h]=(0,o.useState)(null);(0,o.useEffect)(()=>{fetch("/api/models-config").then(e=>e.json()).then(e=>{let t=e.providers?e:{...e,providers:{}};r(t);let n=Object.keys(t.providers??{});n.length>0&&h({type:"provider",name:n[0]})}).catch(()=>r({providers:{}})).finally(()=>l(!1))},[]);let g=(0,o.useCallback)(()=>{let e="new-provider",n=1;for(;t.providers?.[e];)e=`new-provider-${n++}`;r(t=>({...t,providers:{...t.providers??{},[e]:{}}})),h({type:"provider",name:e})},[t.providers]),f=(0,o.useCallback)((e,t)=>{r(r=>({...r,providers:{...r.providers??{},[e]:t}}))},[]),v=(0,o.useCallback)((e,t)=>{r(r=>{let n=Object.entries(r.providers??{}),o=n.findIndex(([t])=>t===e);return -1===o?r:(n[o]=[t,n[o][1]],{...r,providers:Object.fromEntries(n)})}),h(r=>r?"provider"===r.type&&r.name===e?{type:"provider",name:t}:"model"===r.type&&r.providerName===e?{...r,providerName:t}:r:r)},[]),y=(0,o.useCallback)(e=>{r(t=>{let r={...t.providers??{}};return delete r[e],{...t,providers:r}}),r(e=>{let t=Object.keys(e.providers??{});return h(t.length>0?{type:"provider",name:t[0]}:null),e})},[]),b=(0,o.useCallback)(e=>{r(t=>{let r=t.providers?.[e]??{},n=[...r.models??[],{id:""}];return{...t,providers:{...t.providers??{},[e]:{...r,models:n}}}}),r(t=>{let r=(t.providers?.[e]?.models?.length??1)-1;return h({type:"model",providerName:e,index:r}),t})},[]),m=(0,o.useCallback)((e,t,n)=>{r(r=>{let o=r.providers?.[e]??{},i=[...o.models??[]];return i[t]=n,{...r,providers:{...r.providers??{},[e]:{...o,models:i}}}})},[]),j=(0,o.useCallback)((e,t)=>{r(r=>{let n=r.providers?.[e]??{},o=[...n.models??[]];return o.splice(t,1),{...r,providers:{...r.providers??{},[e]:{...n,models:o.length?o:void 0}}}}),h({type:"provider",name:e})},[]),k=(0,o.useCallback)(async()=>{a(!0),c(null),p(!1);try{let e=await fetch("/api/models-config",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}),r=await e.json();!e.ok||r.error?c(r.error??`HTTP ${e.status}`):(p(!0),setTimeout(()=>p(!1),2e3))}catch(e){c(String(e))}finally{a(!1)}},[t]),w=Object.entries(t.providers??{}),S=(()=>{if(!x)return null;if("provider"===x.type){let e=t.providers?.[x.name];return e?(0,n.jsx)(eC,{name:x.name,provider:e,onChange:e=>f(x.name,e),onRename:e=>v(x.name,e),onDelete:()=>y(x.name)},x.name):null}let e=t.providers?.[x.providerName],r=e?.models?.[x.index];return r?(0,n.jsx)(eL,{model:r,onChange:e=>m(x.providerName,x.index,e),onDelete:()=>j(x.providerName,x.index)},`${x.providerName}-${x.index}`):null})();return(0,n.jsx)("div",{style:{position:"fixed",inset:0,zIndex:1e3,background:"rgba(0,0,0,0.35)",display:"flex",alignItems:"center",justifyContent:"center"},onClick:t=>{t.target===t.currentTarget&&e()},children:(0,n.jsxs)("div",{style:{width:860,height:"78vh",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:10,display:"flex",flexDirection:"column",boxShadow:"0 8px 32px rgba(0,0,0,0.18)",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",padding:"12px 18px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"baseline",gap:10},children:[(0,n.jsx)("span",{style:{fontSize:15,fontWeight:700,color:"var(--text)"},children:"Models"}),(0,n.jsx)("code",{style:{fontSize:11,color:"var(--text-muted)",fontFamily:"var(--font-mono)"},children:"~/.pi/agent/models.json"})]}),(0,n.jsx)("button",{onClick:e,style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:20,lineHeight:1,padding:"2px 6px"},children:"\xd7"})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{width:210,borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",flexShrink:0,background:"var(--bg-panel)"},children:[(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:"8px 6px"},children:i?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:12,color:"var(--text-muted)"},children:"Loading…"}):0===w.length?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:11,color:"var(--text-dim)"},children:"No providers yet"}):w.map(([e,t])=>{let r=x?.type==="provider"&&x.name===e,o=t.models??[];return(0,n.jsxs)("div",{style:{marginBottom:2},children:[(0,n.jsxs)("div",{onClick:()=>h({type:"provider",name:e}),style:{display:"flex",alignItems:"center",gap:6,padding:"7px 8px",borderRadius:5,cursor:"pointer",background:r?"var(--bg-selected)":"none"},onMouseEnter:e=>{r||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{r||(e.currentTarget.style.background="none")},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{color:"var(--text-dim)",flexShrink:0},children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]}),(0,n.jsx)("span",{style:{fontSize:12,fontWeight:r?600:400,color:"var(--text)",fontFamily:"var(--font-mono)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e})]}),o.map((t,r)=>{let o=x?.type==="model"&&x.providerName===e&&x.index===r;return(0,n.jsxs)("div",{onClick:()=>h({type:"model",providerName:e,index:r}),style:{display:"flex",alignItems:"center",gap:6,padding:"5px 8px 5px 26px",borderRadius:5,cursor:"pointer",background:o?"var(--bg-selected)":"none"},onMouseEnter:e=>{o||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{o||(e.currentTarget.style.background="none")},children:[(0,n.jsx)("span",{style:{fontSize:11,fontFamily:"var(--font-mono)",color:t.id?"var(--text-muted)":"var(--text-dim)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:t.id||"new model"}),t.reasoning&&(0,n.jsx)("span",{style:{fontSize:9,padding:"1px 4px",background:"rgba(99,102,241,0.12)",color:"rgba(99,102,241,0.8)",borderRadius:3,flexShrink:0},children:"T"})]},r)}),(0,n.jsx)("div",{onClick:t=>{t.stopPropagation(),b(e)},style:{display:"flex",alignItems:"center",gap:4,padding:"4px 8px 4px 26px",borderRadius:5,cursor:"pointer",color:"var(--text-dim)"},onMouseEnter:e=>{e.currentTarget.style.color="var(--accent)",e.currentTarget.style.background="var(--bg-hover)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)",e.currentTarget.style.background="none"},children:(0,n.jsx)("span",{style:{fontSize:11},children:"+ model"})})]},e)})}),(0,n.jsx)("div",{style:{borderTop:"1px solid var(--border)",padding:"8px 6px"},children:(0,n.jsx)("button",{onClick:g,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:5,width:"100%",padding:"6px 0",background:"none",border:"1px dashed var(--border)",borderRadius:5,color:"var(--text-muted)",cursor:"pointer",fontSize:12},onMouseEnter:e=>{e.currentTarget.style.borderColor="var(--accent)",e.currentTarget.style.color="var(--accent)"},onMouseLeave:e=>{e.currentTarget.style.borderColor="var(--border)",e.currentTarget.style.color="var(--text-muted)"},children:"+ Add provider"})})]}),(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:20},children:i?null:S??(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-dim)",fontSize:13},children:"Select a provider or model"})})]}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",gap:10,padding:"10px 18px",borderTop:"1px solid var(--border)",flexShrink:0},children:[d&&(0,n.jsx)("span",{style:{fontSize:12,color:"#f87171",flex:1},children:d}),u&&(0,n.jsx)("span",{style:{fontSize:12,color:"#4ade80",flex:1},children:"Saved"}),(0,n.jsx)("button",{onClick:e,style:{padding:"6px 14px",background:"none",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:13},children:"Cancel"}),(0,n.jsx)("button",{onClick:k,disabled:s,style:{padding:"6px 16px",background:s?"var(--bg-panel)":"var(--accent)",border:"none",borderRadius:6,color:s?"var(--text-muted)":"#fff",cursor:s?"not-allowed":"pointer",fontSize:13,fontWeight:600},children:s?"Saving…":"Save"})]})]})})}function ez(e){return e.replace(/^\/(?:Users|home)\/[^/]+/,"~")}function eI(e){let t=e.sourceInfo?.source,r=e.sourceInfo?.scope;return"user"===r||"user"===t?"global":"project"===r||"project"===t?"project":"path"}function eM({enabled:e,loading:t,onToggle:r}){return(0,n.jsx)("button",{onClick:r,disabled:t,title:e?"Visible in model prompt — click to disable":"Hidden from model prompt — click to enable",style:{flexShrink:0,width:40,height:22,borderRadius:11,border:"none",padding:0,cursor:t?"wait":"pointer",background:e?"var(--accent)":"var(--border)",position:"relative",transition:"background 0.18s",outline:"none"},children:(0,n.jsx)("span",{style:{position:"absolute",top:3,left:e?21:3,width:16,height:16,borderRadius:"50%",background:"#fff",boxShadow:"0 1px 4px rgba(0,0,0,0.22)",transition:"left 0.18s cubic-bezier(.4,0,.2,1)"}})})}function eR({skill:e,cwd:t,onToggle:r,toggling:o,saveError:i}){let l=eI(e),s=!e.disableModelInvocation;return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:20},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:7},children:[(0,n.jsx)("span",{style:{fontSize:10,padding:"1px 5px",borderRadius:3,flexShrink:0,background:"project"===l?"rgba(99,102,241,0.12)":"rgba(120,120,120,0.12)",color:"project"===l?"rgba(99,102,241,0.8)":"var(--text-dim)"},children:l}),(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:11,color:"var(--text-dim)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:function(e){if("project"===l&&e.startsWith(t)){let r=e.slice(t.length).replace(/^\//,"");return`./${r}`}return ez(e)}(e.filePath)}),(0,n.jsx)(eM,{enabled:s,loading:o,onToggle:()=>r(e)}),i&&(0,n.jsx)("span",{style:{fontSize:12,color:"#f87171",flexShrink:0},children:i})]}),(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:5},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:"Name"}),(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:14,color:"var(--text)"},children:e.name})]}),(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:5},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:"Description"}),(0,n.jsx)("span",{style:{fontSize:14,color:"var(--text-muted)",lineHeight:1.6},children:e.description})]})]})}function eW({cwd:e,onInstalled:t}){let[r,i]=(0,o.useState)(""),[l,s]=(0,o.useState)([]),[a,d]=(0,o.useState)(!1),[c,u]=(0,o.useState)(null),[p,x]=(0,o.useState)(null),[h,g]=(0,o.useState)(null),[f,v]=(0,o.useState)(new Set),[y,b]=(0,o.useState)("global"),m=(0,o.useRef)(null);(0,o.useEffect)(()=>{m.current?.focus()},[]);let j=(0,o.useCallback)(async e=>{if(e.trim()){d(!0),u(null),s([]);try{let t=await fetch("/api/skills/search",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:e.trim()})}),r=await t.json();if(r.error)return void u(r.error);s(r.results??[]),0===(r.results??[]).length&&u("No skills found")}catch(e){u(String(e))}finally{d(!1)}}},[]),k=(0,o.useCallback)(async r=>{x(r),g(null);try{let n=await fetch("/api/skills/install",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({package:r,scope:y,cwd:e})}),o=await n.json();if(!n.ok||o.error)return void g(o.error??`HTTP ${n.status}`);v(e=>new Set(e).add(r)),t()}catch(e){g(String(e))}finally{x(null)}},[t,y,e]),w="global"===y?"~/.agents/skills/":`${ez(e)}/.agents/skills/`;return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%"},children:[(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:12,marginBottom:20},children:[(0,n.jsx)("div",{style:{fontSize:14,fontWeight:600,color:"var(--text)"},children:"Add Skill"}),(0,n.jsxs)("div",{style:{display:"flex",gap:8},children:[(0,n.jsx)("input",{ref:m,value:r,onChange:e=>i(e.target.value),onKeyDown:e=>{"Enter"===e.key&&j(r)},placeholder:"e.g. react, testing, deploy",style:{flex:1,padding:"7px 10px",fontSize:13,background:"var(--bg-panel)",border:"1px solid var(--border)",borderRadius:6,color:"var(--text)",outline:"none"}}),(0,n.jsx)("button",{onClick:()=>j(r),disabled:a||!r.trim(),style:{padding:"7px 16px",fontSize:13,borderRadius:6,border:"none",background:"var(--accent)",color:"#fff",cursor:a||!r.trim()?"not-allowed":"pointer",opacity:a||!r.trim()?.5:1,flexShrink:0},children:a?"Searching…":"Search"})]}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10},children:[(0,n.jsx)("div",{style:{display:"flex",borderRadius:5,border:"1px solid var(--border)",overflow:"hidden",fontSize:12,flexShrink:0},children:["global","project"].map(e=>(0,n.jsx)("button",{onClick:()=>b(e),style:{padding:"3px 10px",border:"none",cursor:"pointer",background:y===e?"var(--bg-selected)":"none",color:y===e?"var(--text)":"var(--text-dim)",fontWeight:y===e?600:400,borderRight:"global"===e?"1px solid var(--border)":"none"},children:e},e))}),(0,n.jsxs)("span",{style:{fontSize:12,color:"var(--text-dim)",fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:["→ ",w]})]}),c&&(0,n.jsx)("div",{style:{fontSize:12,color:"#f87171"},children:c}),h&&(0,n.jsx)("div",{style:{fontSize:12,color:"#f87171",wordBreak:"break-word"},children:h})]}),l.length>0?(0,n.jsx)("div",{style:{flex:1,overflowY:"auto"},children:l.map(e=>{let t=f.has(e.package),r=p===e.package,o=e.package.indexOf("@"),i=o>-1?e.package.slice(0,o):e.package,l=o>-1?e.package.slice(o+1):null;return(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:14,padding:"12px 0",borderBottom:"1px solid var(--border)"},children:[(0,n.jsxs)("div",{style:{flex:1,minWidth:0},children:[(0,n.jsx)("div",{style:{fontSize:13,fontWeight:600,color:"var(--text)",marginBottom:3},children:l??i}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10,flexWrap:"wrap"},children:[(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:11,color:"var(--text-dim)"},children:i}),(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:e.installs}),e.url&&(0,n.jsx)("a",{href:e.url,target:"_blank",rel:"noreferrer",style:{fontSize:12,color:"var(--accent)",textDecoration:"none"},children:"skills.sh ↗"})]})]}),(0,n.jsx)("button",{onClick:()=>!t&&!r&&k(e.package),disabled:t||r||null!==p,style:{flexShrink:0,padding:"5px 14px",fontSize:12,fontWeight:500,borderRadius:5,border:"1px solid var(--border)",cursor:t||r||null!==p?"not-allowed":"pointer",background:t?"rgba(34,197,94,0.1)":"none",color:t?"#16a34a":r?"var(--accent)":"var(--text-muted)",transition:"color 0.12s"},children:t?"✓ Installed":r?"Installing…":"Install"})]},e.package)})}):!c&&!a&&(0,n.jsxs)("div",{style:{fontSize:13,color:"var(--text-dim)",lineHeight:1.8},children:["Search ",(0,n.jsx)("a",{href:"https://skills.sh",target:"_blank",rel:"noreferrer",style:{color:"var(--accent)",textDecoration:"none"},children:"skills.sh"})," to discover and install skills for your agent."]})]})}function eE({cwd:e,onClose:t}){let[r,i]=(0,o.useState)([]),[l,s]=(0,o.useState)(!0),[a,d]=(0,o.useState)(null),[c,u]=(0,o.useState)(null),[p,x]=(0,o.useState)(new Set),[h,g]=(0,o.useState)(null),[f,v]=(0,o.useState)(!1),y=(0,o.useCallback)(()=>{s(!0),d(null),fetch(`/api/skills?cwd=${encodeURIComponent(e)}`).then(e=>e.json()).then(e=>{if(e.error)return void d(e.error);let t=e.skills??[];i(t),t.length>0&&!c&&u(t[0].filePath)}).catch(e=>d(String(e))).finally(()=>s(!1))},[e,c]);(0,o.useEffect)(()=>{y()},[e]);let b=(0,o.useCallback)(async e=>{let t=!e.disableModelInvocation;x(t=>new Set(t).add(e.filePath)),g(null);try{let r=await fetch("/api/skills",{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({filePath:e.filePath,disableModelInvocation:t})}),n=await r.json();if(!r.ok||n.error)return void g(n.error??`HTTP ${r.status}`);i(r=>r.map(r=>r.filePath===e.filePath?{...r,disableModelInvocation:t}:r))}catch(e){g(String(e))}finally{x(t=>{let r=new Set(t);return r.delete(e.filePath),r})}},[]),m=r.find(e=>e.filePath===c)??null;return(0,n.jsx)("div",{style:{position:"fixed",inset:0,zIndex:1e3,background:"rgba(0,0,0,0.35)",display:"flex",alignItems:"center",justifyContent:"center"},onClick:e=>{e.target===e.currentTarget&&t()},children:(0,n.jsxs)("div",{style:{width:860,height:"78vh",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:10,display:"flex",flexDirection:"column",boxShadow:"0 8px 32px rgba(0,0,0,0.18)",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",padding:"12px 18px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"baseline",gap:10},children:[(0,n.jsx)("span",{style:{fontSize:15,fontWeight:700,color:"var(--text)"},children:"Skills"}),(0,n.jsx)("code",{style:{fontSize:11,color:"var(--text-muted)",fontFamily:"var(--font-mono)",maxWidth:320,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:ez(e)})]}),(0,n.jsx)("button",{onClick:t,style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:20,lineHeight:1,padding:"2px 6px"},children:"\xd7"})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{width:210,borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",flexShrink:0,background:"var(--bg-panel)"},children:[(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:"8px 6px"},children:l?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:12,color:"var(--text-muted)"},children:"Loading…"}):a?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:11,color:"#f87171"},children:a}):0===r.length?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:11,color:"var(--text-dim)"},children:"No skills found"}):(()=>{let e=[];for(let t of["project","global","path"]){let n=r.filter(e=>eI(e)===t);n.length>0&&e.push({label:t,skills:n})}return e.map(({label:e,skills:t})=>(0,n.jsxs)("div",{style:{marginBottom:6},children:[(0,n.jsx)("div",{style:{padding:"4px 8px 3px",fontSize:10,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.06em"},children:e}),t.map(e=>{let t=!f&&c===e.filePath,r=e.disableModelInvocation;return(0,n.jsxs)("div",{onClick:()=>{u(e.filePath),v(!1)},style:{display:"flex",alignItems:"center",gap:7,padding:"8px 8px",borderRadius:5,cursor:"pointer",background:t?"var(--bg-selected)":"none"},onMouseEnter:e=>{t||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{t||(e.currentTarget.style.background="none")},children:[(0,n.jsx)("span",{style:{flexShrink:0,width:7,height:7,borderRadius:"50%",background:r?"var(--border)":"var(--accent)",boxShadow:r?"none":"0 0 4px var(--accent)",transition:"background 0.15s, box-shadow 0.15s"}}),(0,n.jsx)("span",{style:{fontSize:12,fontWeight:t?600:400,color:r?"var(--text-dim)":"var(--text)",fontFamily:"var(--font-mono)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e.name})]},e.filePath)})]},e))})()}),(0,n.jsx)("div",{style:{padding:"8px 6px",borderTop:"1px solid var(--border)",flexShrink:0},children:(0,n.jsxs)("div",{onClick:()=>v(!0),style:{display:"flex",alignItems:"center",gap:6,padding:"7px 8px",borderRadius:5,cursor:"pointer",background:f?"var(--bg-selected)":"none",color:f?"var(--accent)":"var(--text-dim)",fontSize:12},onMouseEnter:e=>{f||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{f||(e.currentTarget.style.background="none")},children:[(0,n.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"12",y1:"5",x2:"12",y2:"19"}),(0,n.jsx)("line",{x1:"5",y1:"12",x2:"19",y2:"12"})]}),"Add skill"]})})]}),(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:20},children:f?(0,n.jsx)(eW,{cwd:e,onInstalled:()=>{y()}}):l?null:m?(0,n.jsx)(eR,{skill:m,cwd:e,onToggle:b,toggling:p.has(m.filePath),saveError:h},m.filePath):(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-dim)",fontSize:13},children:"Select a skill"})})]}),(0,n.jsx)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",padding:"10px 18px",borderTop:"1px solid var(--border)",flexShrink:0},children:(0,n.jsx)("button",{onClick:t,style:{padding:"6px 14px",background:"none",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:13},children:"Close"})})]})})}let eB={id:"chat",type:"chat",label:"Chat"};function e$(){let e=(0,i.useRouter)(),t=(0,i.useSearchParams)(),[r,l]=(0,o.useState)(null),[s,a]=(0,o.useState)(null),[d,c]=(0,o.useState)(0),[u,p]=(0,o.useState)(0),[x,h]=(0,o.useState)(0),[g,f]=(0,o.useState)(!1),[v,y]=(0,o.useState)(!1),[b,m]=(0,o.useState)(!0),[j]=(0,o.useState)(()=>t.get("session")),[k,w]=(0,o.useState)(null),[S,C]=(0,o.useState)(()=>!t.get("session")),[L,T]=(0,o.useState)([eB]),[z,I]=(0,o.useState)("chat"),M=(0,o.useCallback)(e=>{w(e),l(e=>(null===e&&p(e=>e+1),e))},[]),R=(0,o.useCallback)((t,r=!1)=>{a(null),l(t),p(e=>e+1),C(!0),r||e.replace(`?session=${encodeURIComponent(t.id)}`,{scroll:!1})},[e]),W=(0,o.useCallback)((t,r)=>{l(null),a(r),p(e=>e+1),e.replace("/",{scroll:!1})},[e]),E=(0,o.useCallback)(t=>{a(null),l(t),c(e=>e+1),e.replace(`?session=${encodeURIComponent(t.id)}`,{scroll:!1})},[e]),B=(0,o.useCallback)(()=>{c(e=>e+1),h(e=>e+1)},[]),$=(0,o.useCallback)(t=>{c(e=>e+1),p(e=>e+1),a(null),l(e=>({...e??{path:"",cwd:"",created:"",modified:"",messageCount:0,firstMessage:""},id:t})),e.replace(`?session=${encodeURIComponent(t)}`,{scroll:!1})},[e]),O=(0,o.useCallback)(()=>{C(!0)},[]),P=(0,o.useCallback)(t=>{c(e=>e+1),r?.id===t&&(l(null),a(null),e.replace("/",{scroll:!1}))},[r,e]),D=(0,o.useCallback)((e,t)=>{let r=`file:${e}`;T(n=>n.find(e=>e.id===r)?n:[...n,{id:r,type:"file",label:t,filePath:e}]),I(r)},[]),F=(0,o.useCallback)(e=>{T(t=>t.filter(t=>t.id!==e)),I(t=>t===e?"chat":t)},[]),N=s??(null===r&&k?k:null),A=null!==r||null!==N,U=L.find(e=>e.id===z)??eB,_=(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(H,{selectedSessionId:r?.id??null,onSelectSession:R,onNewSession:W,initialSessionId:j,onInitialRestoreDone:O,refreshKey:d,onSessionDeleted:P,selectedCwd:r?.cwd??s??null,onCwdChange:M,onOpenFile:D,explorerRefreshKey:x}),(0,n.jsx)("div",{style:{padding:"6px 10px",flexShrink:0,display:"flex",justifyContent:"space-between"},children:[{label:"Models",onClick:()=>f(!0),disabled:!1,icon:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]})},{label:"Skills",onClick:()=>y(!0),disabled:!k&&!r?.cwd&&!s,icon:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M12 2L2 7l10 5 10-5-10-5z"}),(0,n.jsx)("path",{d:"M2 17l10 5 10-5"}),(0,n.jsx)("path",{d:"M2 12l10 5 10-5"})]})}].map(({label:e,onClick:t,disabled:r,icon:o})=>(0,n.jsxs)("button",{onClick:t,disabled:r,title:e,style:{flex:1,display:"flex",alignItems:"center",justifyContent:"center",gap:5,padding:"7px 0",background:"none",border:"none",borderRadius:5,color:"var(--text-muted)",cursor:r?"default":"pointer",fontSize:11,opacity:r?.35:1},onMouseEnter:e=>{r||(e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text)")},onMouseLeave:e=>{e.currentTarget.style.background="none",e.currentTarget.style.color="var(--text-muted)"},children:[o,e]},e))})]});return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("div",{style:{display:"flex",height:"100vh",overflow:"hidden",background:"var(--bg)"},children:[(0,n.jsx)("div",{className:"sidebar-inline",style:{width:260*!!b,minWidth:260*!!b,borderRight:b?"1px solid var(--border)":"none",background:"var(--bg-panel)",display:"flex",flexDirection:"column",overflow:"hidden",transition:"width 0.2s ease, min-width 0.2s ease"},children:(0,n.jsx)("div",{style:{width:260,minWidth:260,display:"flex",flexDirection:"column",height:"100%"},children:_})}),(0,n.jsxs)("div",{className:"sidebar-overlay",style:{position:"fixed",inset:0,zIndex:200,display:"none",pointerEvents:b?"auto":"none"},children:[(0,n.jsx)("div",{onClick:()=>m(!1),style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.4)",opacity:+!!b,transition:"opacity 0.25s ease"}}),(0,n.jsx)("div",{style:{position:"absolute",top:0,left:0,bottom:0,width:280,maxWidth:"85vw",background:"var(--bg-panel)",borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",boxShadow:b?"4px 0 20px rgba(0,0,0,0.15)":"none",transform:b?"translateX(0)":"translateX(-100%)",transition:"transform 0.25s ease"},children:_})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",flexShrink:0},children:[(0,n.jsx)("button",{onClick:()=>m(e=>!e),title:b?"Hide sidebar":"Show sidebar",style:{display:"flex",alignItems:"center",justifyContent:"center",width:36,height:36,padding:0,background:"var(--bg-panel)",border:"none",borderRight:"1px solid var(--border)",color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--text)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-muted)"},children:b?(0,n.jsxs)("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"3",y:"3",width:"18",height:"18",rx:"2"}),(0,n.jsx)("line",{x1:"9",y1:"3",x2:"9",y2:"21"})]}):(0,n.jsxs)("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"3",y1:"6",x2:"21",y2:"6"}),(0,n.jsx)("line",{x1:"3",y1:"12",x2:"21",y2:"12"}),(0,n.jsx)("line",{x1:"3",y1:"18",x2:"21",y2:"18"})]})}),(0,n.jsx)("div",{style:{flex:1,overflow:"hidden"},children:(0,n.jsx)(ef,{tabs:L,activeTabId:z,onSelectTab:I,onCloseTab:F})})]}),(0,n.jsxs)("div",{style:{flex:1,overflow:"hidden",position:"relative"},children:[(0,n.jsx)("div",{style:{position:"absolute",inset:0,display:"chat"===U.type?"flex":"none",flexDirection:"column"},children:A?(0,n.jsx)(ex,{session:r,newSessionCwd:N,onAgentEnd:B,onSessionCreated:E,onSessionForked:$},u):S&&!A?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)",fontSize:15},children:"Select a session from the sidebar"}):null}),"file"===U.type&&U.filePath&&(0,n.jsx)("div",{style:{position:"absolute",inset:0},children:(0,n.jsx)(eh,{filePath:U.filePath})})]})]})]}),g&&(0,n.jsx)(eT,{onClose:()=>f(!1)}),v&&(k??r?.cwd??s)&&(0,n.jsx)(eE,{cwd:k??r?.cwd??s,onClose:()=>y(!1)})]})}}},e=>{e.O(0,[110,441,794,358],()=>e(e.s=5597)),_N_E=e.O()}]);
1
+ (self.webpackChunk_N_E=self.webpackChunk_N_E||[]).push([[974],{5597:(e,t,r)=>{Promise.resolve().then(r.bind(r,6482))},6482:(e,t,r)=>{"use strict";r.d(t,{AppShell:()=>eO});var n=r(5155),o=r(2115),i=r(3321);let l="var(--text-dim)";function s({size:e=14,open:t=!1}){return t?(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:[(0,n.jsx)("path",{d:"M1 4.5A1 1 0 0 1 2 3.5H5.5L7 5h7.5v1H1V4.5Z",fill:l}),(0,n.jsx)("path",{d:"M1 6h14.5L14 13H2L1 6Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.12"})]}):(0,n.jsx)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:(0,n.jsx)("path",{d:"M1 4.5A1 1 0 0 1 2 3.5H5.5L7 5H14a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H2a1 1 0 0 1-1-1V4.5Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.1"})})}function a({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 16 16",fill:"none",children:[(0,n.jsx)("path",{d:"M3 2h7l3 3v9H3V2Z",stroke:l,strokeWidth:"1",fill:l,fillOpacity:"0.08"}),(0,n.jsx)("path",{d:"M10 2v3h3",stroke:l,strokeWidth:"1",fill:"none",strokeLinejoin:"round"})]})}function d({label:e,size:t=14}){let r=t/14;return(0,n.jsxs)("svg",{width:t,height:t,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("text",{x:"7",y:"9.5",textAnchor:"middle",fontSize:3.4*r,fontFamily:"var(--font-mono), monospace",fontWeight:"600",fill:l,letterSpacing:"0",children:e})]})}function c({size:e=14}){return(0,n.jsx)(d,{label:"TS",size:e})}function p({size:e=14}){return(0,n.jsx)(d,{label:"TSX",size:e})}function u({size:e=14}){return(0,n.jsx)(d,{label:"JS",size:e})}function x({size:e=14}){return(0,n.jsx)(d,{label:"JSX",size:e})}function h({size:e=14}){return(0,n.jsx)(d,{label:"PY",size:e})}function g({size:e=14}){return(0,n.jsx)(d,{label:"{}",size:e})}function f({size:e=14}){return(0,n.jsx)(d,{label:"CSS",size:e})}function v({size:e=14}){return(0,n.jsx)(d,{label:"SC",size:e})}function y({size:e=14}){return(0,n.jsx)(d,{label:"HTM",size:e})}function m({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M3.5 9.5V7l1.5 1.5L6.5 7v2.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"}),(0,n.jsx)("path",{d:"M8 7v2.5M7 9l1 1.5 1-1.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"})]})}function b({size:e=14}){return(0,n.jsx)(d,{label:"YML",size:e})}function j({size:e=14}){return(0,n.jsx)(d,{label:"TOM",size:e})}function k({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M4 7.5l2 1.5-2 1.5",stroke:l,strokeWidth:"0.95",strokeLinecap:"round",strokeLinejoin:"round",fill:"none"}),(0,n.jsx)("path",{d:"M7.5 10.5h2.5",stroke:l,strokeWidth:"0.95",strokeLinecap:"round"})]})}function S({size:e=14}){return(0,n.jsx)(d,{label:"RS",size:e})}function w({size:e=14}){return(0,n.jsx)(d,{label:"GO",size:e})}function C({size:e=14}){return(0,n.jsx)(d,{label:"SQL",size:e})}function L({size:e=14}){return(0,n.jsx)(d,{label:"GQL",size:e})}function z({size:e=14}){return(0,n.jsx)(d,{label:"TF",size:e})}function T({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("rect",{x:"3.5",y:"6.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"}),(0,n.jsx)("rect",{x:"6",y:"6.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"}),(0,n.jsx)("rect",{x:"3.5",y:"8.5",width:"2",height:"1.5",rx:"0.3",stroke:l,strokeWidth:"0.8"})]})}function I({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"5.5",cy:"8.5",r:"1.5",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M7 8.5h2.5M8.5 8.5v1.5",stroke:l,strokeWidth:"0.9",strokeLinecap:"round"})]})}function R({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"5",cy:"6.5",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("circle",{cx:"9",cy:"6.5",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("circle",{cx:"5",cy:"10",r:"1",stroke:l,strokeWidth:"0.85"}),(0,n.jsx)("path",{d:"M5 7.5V9",stroke:l,strokeWidth:"0.85",strokeLinecap:"round"}),(0,n.jsx)("path",{d:"M9 7.5v.5a2 2 0 0 1-2 2H6",stroke:l,strokeWidth:"0.85",strokeLinecap:"round",fill:"none"})]})}function M({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("rect",{x:"4.5",y:"8.5",width:"5",height:"3",rx:"0.6",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M5.5 8.5V7.5a1.5 1.5 0 0 1 3 0v1",stroke:l,strokeWidth:"0.9",strokeLinecap:"round",fill:"none"})]})}function W({size:e=14}){return(0,n.jsxs)("svg",{width:e,height:e,viewBox:"0 0 14 14",fill:"none",children:[(0,n.jsx)("path",{d:"M2.5 1h6l3 3v9h-9V1Z",stroke:l,strokeWidth:"0.9",fill:l,fillOpacity:"0.07",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M8.5 1v3h3",stroke:l,strokeWidth:"0.9",fill:"none",strokeLinejoin:"round"}),(0,n.jsx)("circle",{cx:"7",cy:"8.5",r:"1.3",stroke:l,strokeWidth:"0.9"}),(0,n.jsx)("path",{d:"M7 6.5v.7M7 10.3v.7M5 8.5h.7M8.3 8.5H9M5.5 6.9l.5.5M8.5 9.6l-.5-.5M5.5 10.1l.5-.5M8.5 7.4l-.5.5",stroke:l,strokeWidth:"0.8",strokeLinecap:"round"})]})}function E(e,t=14){let r=e.toLowerCase(),o=r.split(".").pop()??"";if("dockerfile"===r||r.startsWith("dockerfile."))return(0,n.jsx)(T,{size:t});if(".env"===r||r.startsWith(".env."))return(0,n.jsx)(I,{size:t});if(".gitignore"===r||".gitattributes"===r||".gitmodules"===r)return(0,n.jsx)(R,{size:t});if("package-lock.json"===r||"yarn.lock"===r||"bun.lock"===r||"pnpm-lock.yaml"===r||"cargo.lock"===r)return(0,n.jsx)(M,{size:t});if(r.endsWith(".config.ts")||r.endsWith(".config.js")||r.endsWith(".config.mjs")||r.endsWith(".config.cjs")||[".eslintrc",".eslintrc.js",".eslintrc.json",".eslintrc.yml","eslint.config.mjs","eslint.config.js"].includes(r))return(0,n.jsx)(W,{size:t});switch(o){case"ts":return(0,n.jsx)(c,{size:t});case"tsx":return(0,n.jsx)(p,{size:t});case"js":case"mjs":case"cjs":return(0,n.jsx)(u,{size:t});case"jsx":return(0,n.jsx)(x,{size:t});case"py":return(0,n.jsx)(h,{size:t});case"json":case"jsonl":return(0,n.jsx)(g,{size:t});case"css":case"less":return(0,n.jsx)(f,{size:t});case"scss":return(0,n.jsx)(v,{size:t});case"html":case"htm":return(0,n.jsx)(y,{size:t});case"md":case"mdx":return(0,n.jsx)(m,{size:t});case"yaml":case"yml":return(0,n.jsx)(b,{size:t});case"toml":return(0,n.jsx)(j,{size:t});case"sh":case"bash":case"zsh":case"fish":return(0,n.jsx)(k,{size:t});case"rs":return(0,n.jsx)(S,{size:t});case"go":return(0,n.jsx)(w,{size:t});case"sql":return(0,n.jsx)(C,{size:t});case"graphql":case"gql":return(0,n.jsx)(L,{size:t});case"tf":case"hcl":return(0,n.jsx)(z,{size:t});case"lock":return(0,n.jsx)(M,{size:t});default:return(0,n.jsx)(a,{size:t})}}function B({node:e,depth:t,onOpenFile:r}){let[i,l]=(0,o.useState)(!1),[a,d]=(0,o.useState)(e.children??[]),[c,p]=(0,o.useState)(e.loaded??!1),[u,x]=(0,o.useState)(!1),[h,g]=(0,o.useState)(!1),f=(0,o.useCallback)(async()=>{if(!c){x(!0);try{let t=e.fullPath.split("/").filter(Boolean).join("/"),r=await fetch(`/api/files/${t}?type=list`);if(!r.ok)return;let n=await r.json();d(n.entries.map(t=>({name:t.name,fullPath:e.fullPath.replace(/\/$/,"")+"/"+t.name,isDir:t.isDir,size:t.size,children:t.isDir?[]:void 0,loaded:!t.isDir}))),p(!0)}catch{}finally{x(!1)}}},[c,e.fullPath]),v=(0,o.useCallback)(()=>{e.isDir?(c||i||f(),l(e=>!e)):r(e.fullPath,e.name)},[e.isDir,e.fullPath,e.name,c,i,f,r]);return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{onClick:v,onMouseEnter:()=>g(!0),onMouseLeave:()=>g(!1),style:{display:"flex",alignItems:"center",gap:4,paddingLeft:8+14*t,paddingRight:8,height:24,cursor:"pointer",background:h?"var(--bg-hover)":"transparent",borderRadius:4,userSelect:"none"},children:[e.isDir&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:i?"rotate(90deg)":"none",transition:"transform 0.1s"},children:(0,n.jsx)("polyline",{points:"3 2 7 5 3 8"})}),!e.isDir&&(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),(0,n.jsx)("span",{style:{flexShrink:0,display:"flex",alignItems:"center"},children:e.isDir?(0,n.jsx)(s,{size:14,open:i}):E(e.name,14)}),(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1},title:e.fullPath,children:e.name}),u&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 24 24",fill:"none",stroke:"var(--text-dim)",strokeWidth:"2",strokeLinecap:"round",children:(0,n.jsx)("path",{d:"M12 2v4M12 18v4M4.93 4.93l2.83 2.83M16.24 16.24l2.83 2.83M2 12h4M18 12h4"})})]}),e.isDir&&i&&(0,n.jsxs)("div",{children:[a.map(e=>(0,n.jsx)(B,{node:e,depth:t+1,onOpenFile:r},e.fullPath)),0===a.length&&c&&(0,n.jsx)("div",{style:{paddingLeft:8+(t+1)*14,fontSize:11,color:"var(--text-dim)",height:22,display:"flex",alignItems:"center"},children:"empty"})]})]})}function $({cwd:e,onOpenFile:t,refreshKey:r}){let[i,l]=(0,o.useState)([]),[s,a]=(0,o.useState)(!0),[d,c]=(0,o.useState)(null),[p,u]=(0,o.useState)(0);return((0,o.useEffect)(()=>{u(e=>e+1)},[r]),(0,o.useEffect)(()=>{a(!0),c(null);let t=e.split("/").filter(Boolean).join("/");fetch(`/api/files/${t}?type=list`).then(e=>e.json()).then(t=>{t.error?c(t.error):l((t.entries??[]).map(t=>({name:t.name,fullPath:e.replace(/\/$/,"")+"/"+t.name,isDir:t.isDir,size:t.size,children:t.isDir?[]:void 0,loaded:!t.isDir})))}).catch(e=>c(String(e))).finally(()=>a(!1))},[e,p]),s)?(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"var(--text-dim)"},children:"Loading files..."}):d?(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"#f87171"},children:d}):(0,n.jsxs)("div",{style:{padding:"2px 4px"},children:[i.map(e=>(0,n.jsx)(B,{node:e,depth:0,onOpenFile:t},`${p}:${e.fullPath}`)),0===i.length&&(0,n.jsx)("div",{style:{padding:"8px 12px",fontSize:11,color:"var(--text-dim)"},children:"No files found"})]})}function O(e){let t=new Map;for(let r of e){if(!r.cwd)continue;let e=t.get(r.cwd);(!e||r.modified>e)&&t.set(r.cwd,r.modified)}return[...t.entries()].sort((e,t)=>t[1].localeCompare(e[1])).slice(0,5).map(([e])=>e)}function P(e,t){let r=t&&e.startsWith(t)?"~"+e.slice(t.length):e,n=r.includes("/")?"/":"\\",o=r.split(n).filter(Boolean);return o.length<=2?r:"…/"+o.slice(-2).join(n)}let D="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!@#$%^&*";function F(){let[e,t]=(0,o.useState)(!1),[r,i]=(0,o.useState)(!1),l=(0,o.useRef)(null),s=function(e,t){let[r,n]=(0,o.useState)(e),i=(0,o.useRef)(null),l=(0,o.useRef)(0);return(0,o.useEffect)(()=>{if(!t)return void n(e);l.current=0;let r=4*e.length,o=()=>{l.current+=1;let t=Math.floor(l.current/r*e.length);n(e.split("").map((e,r)=>" "===e?" ":r<t?e:D[Math.floor(Math.random()*D.length)]).join("")),l.current<r?i.current=requestAnimationFrame(o):n(e)};return i.current=requestAnimationFrame(o),()=>{i.current&&cancelAnimationFrame(i.current)}},[e,t]),r}(e?"v0.3.9":"Pi Agent Web",r),a=(0,o.useCallback)(e=>{t(e),i(!0),setTimeout(()=>i(!1),(e?6:8)*4*(1e3/60)+100)},[]),d=(0,o.useCallback)(()=>{l.current&&clearTimeout(l.current);let t=!e;a(t),t&&(l.current=setTimeout(()=>a(!1),3e3))},[e,a]);return(0,o.useEffect)(()=>()=>{l.current&&clearTimeout(l.current)},[]),(0,n.jsx)("button",{onClick:d,style:{background:"none",border:"none",padding:0,cursor:"default",fontWeight:700,fontSize:15,letterSpacing:"-0.01em",color:e?"var(--accent)":"var(--text)",fontFamily:"var(--font-mono)",minWidth:"6ch"},children:s})}function H({selectedSessionId:e,onSelectSession:t,onNewSession:r,initialSessionId:i,onInitialRestoreDone:l,refreshKey:s,onSessionDeleted:a,selectedCwd:d,onCwdChange:c,onOpenFile:p,explorerRefreshKey:u}){let[x,h]=(0,o.useState)([]),[g,f]=(0,o.useState)(!0),[v,y]=(0,o.useState)(null),[m,b]=(0,o.useState)(null),[j,k]=(0,o.useState)(""),[S,w]=(0,o.useState)(!1),[C,L]=(0,o.useState)(!1),[z,T]=(0,o.useState)(""),I=(0,o.useRef)(null),R=(0,o.useRef)(null),[M,W]=(0,o.useState)(!0),[E,B]=(0,o.useState)(0),D=(0,o.useCallback)(async(e=!1)=>{try{e&&f(!0);let t=await fetch("/api/sessions");if(!t.ok)throw Error(`HTTP ${t.status}`);let r=await t.json();h(r.sessions),y(null)}catch(e){y(String(e))}finally{e&&f(!1)}},[]),A=(0,o.useRef)(!1);(0,o.useEffect)(()=>{let e=!A.current;A.current=!0,D(e)},[D,s]),(0,o.useEffect)(()=>{void 0!==u&&B(e=>e+1)},[u]),(0,o.useEffect)(()=>{fetch("/api/home").then(e=>e.json()).then(e=>{e.home&&k(e.home)}).catch(()=>{})},[]);let U=(0,o.useRef)(!1);(0,o.useEffect)(()=>{c?.(m)},[m,c]),(0,o.useEffect)(()=>{if(0!==x.length&&null===m){if(i&&!U.current){U.current=!0;let e=x.find(e=>e.id===i);if(e){b(e.cwd),t(e,!0);return}l?.()}let e=O(x);e.length>0&&b(e[0])}},[x,m,i,t,l]);let _=(0,o.useCallback)(()=>{let e=z.trim();e&&b(e),L(!1),T(""),w(!1)},[z]),V=(0,o.useCallback)(async()=>{try{let e=await fetch("/api/default-cwd",{method:"POST"}),t=await e.json();t.cwd&&(b(t.cwd),w(!1))}catch{}},[]);(0,o.useEffect)(()=>{let e=e=>{R.current&&!R.current.contains(e.target)&&(w(!1),L(!1),T(""))};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]);let J=(0,o.useCallback)(()=>{if(!m)return;let e="function"==typeof crypto.randomUUID?crypto.randomUUID():`${Date.now().toString(36)}-${Math.random().toString(36).slice(2)}-${Math.random().toString(36).slice(2)}`;r?.(e,m)},[m,r]),Y=O(x),K=m?x.filter(e=>e.cwd===m):x,Z=function(e){let t=new Map;for(let r of e)t.set(r.id,{session:r,children:[]});let r=new Map;for(let t of e)t.parentSessionId&&r.set(t.id,t.parentSessionId);let n=[];for(let e of t.values()){let o=function(e){let n=r.get(e),o=new Set;for(;n&&!o.has(n);){if(o.add(n),t.has(n))return n;n=r.get(n)}return null}(e.session.id);o?t.get(o).children.push(e):n.push(e)}let o=e=>{e.sort((e,t)=>t.session.modified.localeCompare(e.session.modified)),e.forEach(e=>o(e.children))};return o(n),n}(K);return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{padding:"12px 10px 10px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",marginBottom:10},children:[(0,n.jsx)(F,{}),(0,n.jsxs)("div",{style:{display:"flex",gap:6},children:[(0,n.jsxs)("button",{onClick:J,disabled:!m,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:5,background:"var(--bg-hover)",border:"1px solid var(--border)",color:m?"var(--text-muted)":"var(--text-dim)",cursor:m?"pointer":"not-allowed",height:32,paddingLeft:10,paddingRight:12,borderRadius:7,fontSize:12,fontWeight:500,letterSpacing:"-0.01em",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},title:m?`New session in ${m}`:"Select a project first",onMouseEnter:e=>{m&&(e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)")},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color=m?"var(--text-muted)":"var(--text-dim)",e.currentTarget.style.borderColor="var(--border)"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 12 12",fill:"none",stroke:"currentColor",strokeWidth:"2.2",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"6",y1:"1",x2:"6",y2:"11"}),(0,n.jsx)("line",{x1:"1",y1:"6",x2:"11",y2:"6"})]}),"New"]}),(0,n.jsx)("button",{onClick:()=>D(!0),style:{display:"flex",alignItems:"center",justifyContent:"center",background:"var(--bg-hover)",border:"1px solid var(--border)",color:"var(--text-muted)",cursor:"pointer",width:32,height:32,borderRadius:7,padding:0,flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},title:"Refresh",children:(0,n.jsxs)("svg",{width:"15",height:"15",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]})})]})]}),(0,n.jsxs)("div",{ref:R,style:{position:"relative"},children:[(0,n.jsx)("button",{onClick:()=>w(e=>!e),style:{width:"100%",display:"flex",alignItems:"center",padding:"6px 10px",background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,cursor:"pointer",fontSize:12,color:"var(--text)",textAlign:"left"},children:(0,n.jsx)("span",{style:{flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",fontFamily:"var(--font-mono)",fontSize:11,color:m?"var(--text)":"var(--text-dim)"},title:m??"",children:m?P(m,j):i&&!U.current?"":"Select project…"})}),S&&(0,n.jsxs)("div",{style:{position:"absolute",top:"calc(100% + 4px)",left:0,right:0,zIndex:100,background:"var(--bg)",border:"1px solid var(--border)",borderRadius:8,boxShadow:"0 6px 20px rgba(0,0,0,0.10)",overflow:"hidden"},children:[Y.map(e=>(0,n.jsxs)("button",{onClick:()=>{b(e),L(!1),T(""),w(!1)},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:e===m?"var(--bg-selected)":"none",border:"none",borderBottom:"1px solid var(--border)",color:e===m?"var(--text)":"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11,fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},title:e,children:[e===m&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--accent)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:(0,n.jsx)("polyline",{points:"1.5 5 4 7.5 8.5 2.5"})}),e!==m&&(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),(0,n.jsx)("span",{style:{flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:P(e,j)})]},e)),!C&&(0,n.jsxs)("button",{onClick:e=>{e.stopPropagation(),V()},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:"none",border:"none",borderTop:Y.length>0?"1px solid var(--border)":"none",color:"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11},children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.1",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:(0,n.jsx)("path",{d:"M1 3A1 1 0 0 1 2 2H4L5 3.5H8.5a.5.5 0 0 1 .5.5v4a.5.5 0 0 1-.5.5h-7A.5.5 0 0 1 1 8V3Z"})}),(0,n.jsx)("span",{children:"Use default directory"})]}),C?(0,n.jsxs)("div",{style:{padding:"6px 8px",borderTop:Y.length>0?"none":void 0},children:[(0,n.jsx)("input",{ref:I,value:z,onChange:e=>T(e.target.value),onKeyDown:e=>{"Enter"===e.key&&_(),"Escape"===e.key&&(L(!1),T(""))},placeholder:"/path/to/project",style:{width:"100%",fontSize:11,fontFamily:"var(--font-mono)",padding:"5px 8px",border:"1px solid var(--accent)",borderRadius:5,outline:"none",background:"var(--bg)",color:"var(--text)",boxSizing:"border-box"}}),(0,n.jsxs)("div",{style:{display:"flex",gap:5,marginTop:5},children:[(0,n.jsx)("button",{onClick:_,style:{flex:1,padding:"4px 0",background:"var(--accent)",border:"none",borderRadius:5,color:"#fff",fontSize:11,fontWeight:600,cursor:"pointer"},children:"Open"}),(0,n.jsx)("button",{onClick:()=>{L(!1),T("")},style:{flex:1,padding:"4px 0",background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:5,color:"var(--text-muted)",fontSize:11,cursor:"pointer"},children:"Cancel"})]})]}):(0,n.jsxs)("button",{onClick:e=>{e.stopPropagation(),L(!0),setTimeout(()=>I.current?.focus(),0)},style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"8px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",textAlign:"left",fontSize:11},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.1",strokeLinecap:"round",style:{flexShrink:0},children:[(0,n.jsx)("line",{x1:"5",y1:"1",x2:"5",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"5",x2:"9",y2:"5"})]}),(0,n.jsx)("span",{children:"Custom path…"})]})]})]})]}),(0,n.jsxs)("div",{style:{flex:M&&(d||m)?"1 1 0":"1 1 auto",overflowY:"auto",padding:"0",minHeight:80},children:[g&&(0,n.jsx)("div",{style:{padding:"16px 14px",color:"var(--text-muted)",fontSize:12},children:"Loading..."}),v&&(0,n.jsx)("div",{style:{padding:"12px 14px",color:"#f87171",fontSize:12},children:v}),!g&&!v&&0===K.length&&(0,n.jsx)("div",{style:{padding:"16px 14px",color:"var(--text-muted)",fontSize:12},children:"No sessions found"}),Z.map(r=>(0,n.jsx)(N,{node:r,selectedSessionId:e,onSelectSession:t,onRenamed:D,onSessionDeleted:e=>{a?.(e),D()},depth:0},r.session.id))]}),(d||m)&&(0,n.jsxs)("div",{style:{borderTop:"1px solid var(--border)",display:"flex",flexDirection:"column",flex:M?"1 1 0":"0 0 auto",minHeight:0,overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",flexShrink:0},children:[(0,n.jsxs)("button",{onClick:()=>W(e=>!e),style:{display:"flex",alignItems:"center",gap:6,flex:1,padding:"6px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:11,fontWeight:600,letterSpacing:"0.05em",textTransform:"uppercase",textAlign:"left"},children:[(0,n.jsx)("svg",{width:"9",height:"9",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{transform:M?"rotate(90deg)":"none",transition:"transform 0.15s",flexShrink:0},children:(0,n.jsx)("polyline",{points:"3 2 7 5 3 8"})}),"Explorer"]}),(0,n.jsx)("button",{onClick:()=>B(e=>e+1),title:"Refresh explorer",style:{display:"flex",alignItems:"center",justifyContent:"center",width:26,height:26,padding:0,marginRight:6,background:"none",border:"none",color:"var(--text-dim)",cursor:"pointer",borderRadius:5,flexShrink:0,transition:"color 0.12s, background 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.background="var(--bg-hover)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)",e.currentTarget.style.background="none"},children:(0,n.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]})})]}),M&&(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",overflowX:"hidden"},children:(0,n.jsx)($,{cwd:d??m,onOpenFile:p??(()=>{}),refreshKey:E})})]})]})}function N({node:e,selectedSessionId:t,onSelectSession:r,onRenamed:i,onSessionDeleted:l,depth:s}){let[a,d]=(0,o.useState)(!1),c=e.children.length>0;return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{style:{position:"relative"},children:[s>0&&(0,n.jsx)("div",{style:{position:"absolute",left:12*s+6,top:0,bottom:0,width:1,background:"var(--border)",pointerEvents:"none"}}),(0,n.jsx)(A,{session:e.session,isSelected:e.session.id===t,onClick:()=>r(e.session),onRenamed:i,onDeleted:e=>l?.(e),depth:s,hasChildren:c,collapsed:a,onToggleCollapse:()=>d(e=>!e)})]}),c&&!a&&(0,n.jsx)("div",{children:e.children.map(e=>(0,n.jsx)(N,{node:e,selectedSessionId:t,onSelectSession:r,onRenamed:i,onSessionDeleted:l,depth:s+1},e.session.id))})]})}function A({session:e,isSelected:t,onClick:r,onRenamed:i,onDeleted:l,depth:s=0,hasChildren:a=!1,collapsed:d=!1,onToggleCollapse:c}){let p,u,x,h,g,[f,v]=(0,o.useState)(!1),[y,m]=(0,o.useState)(!1),[b,j]=(0,o.useState)(""),[k,S]=(0,o.useState)(!1),[w,C]=(0,o.useState)(!1),L=(0,o.useRef)(null),z=e.name||e.firstMessage.slice(0,50)||e.id.slice(0,12),T=(0,o.useCallback)(t=>{t.stopPropagation(),j(e.name??""),m(!0),setTimeout(()=>L.current?.select(),0)},[e.name]),I=(0,o.useCallback)(async()=>{let t=b.trim();if(m(!1),t!==(e.name??""))try{await fetch(`/api/sessions/${encodeURIComponent(e.id)}`,{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({name:t})}),i?.()}catch{}},[b,e.id,e.name,i]),R=(0,o.useCallback)(e=>{e.stopPropagation(),S(!0)},[]),M=(0,o.useCallback)(async t=>{t.stopPropagation(),S(!1),C(!0);try{await fetch(`/api/sessions/${encodeURIComponent(e.id)}`,{method:"DELETE"}),l?.(e.id)}catch{C(!1)}},[e.id,l]),W=(0,o.useCallback)(e=>{e.stopPropagation(),S(!1)},[]);return(0,n.jsx)("div",{onClick:k||y?void 0:r,onMouseEnter:()=>v(!0),onMouseLeave:()=>{v(!1)},style:{height:54,display:"flex",alignItems:"center",paddingLeft:s>0?12*s+14:14,paddingRight:8,cursor:k||y?"default":"pointer",background:k?"rgba(239,68,68,0.06)":t?"var(--bg-selected)":f?"var(--bg-hover)":"transparent",borderLeft:k?"2px solid #ef4444":t?"2px solid var(--accent)":"2px solid transparent",transition:"background 0.1s",opacity:w?.5:1,gap:6,overflow:"hidden"},children:k?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("div",{style:{flex:1,minWidth:0,fontSize:12,color:"var(--text)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:["Delete ",(0,n.jsxs)("span",{style:{fontWeight:600},children:["“",z.slice(0,22),z.length>22?"…":"","”"]}),"?"]}),(0,n.jsxs)("div",{style:{display:"flex",gap:5,flexShrink:0},children:[(0,n.jsxs)("button",{onClick:M,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:4,height:30,padding:"0 11px",background:"#ef4444",border:"none",borderRadius:6,color:"#fff",cursor:"pointer",fontSize:12,fontWeight:600,whiteSpace:"nowrap"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"3 6 5 6 21 6"}),(0,n.jsx)("path",{d:"M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"}),(0,n.jsx)("path",{d:"M10 11v6M14 11v6"}),(0,n.jsx)("path",{d:"M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"})]}),"Delete"]}),(0,n.jsx)("button",{onClick:W,style:{display:"flex",alignItems:"center",justifyContent:"center",height:30,padding:"0 11px",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:12,fontWeight:500,whiteSpace:"nowrap"},children:"Cancel"})]})]}):y?(0,n.jsx)("input",{ref:L,value:b,onChange:e=>j(e.target.value),onBlur:I,onKeyDown:e=>{"Enter"===e.key&&I(),"Escape"===e.key&&m(!1)},autoFocus:!0,style:{flex:1,fontSize:12,padding:"5px 8px",border:"1px solid var(--accent)",borderRadius:5,outline:"none",background:"var(--bg)",color:"var(--text)",height:30}}):(0,n.jsxs)(n.Fragment,{children:[s>0&&(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 24 24",fill:"none",stroke:"var(--text-dim)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),(0,n.jsxs)("div",{style:{flex:1,minWidth:0},children:[(0,n.jsx)("div",{style:{fontSize:12,fontWeight:t?500:400,lineHeight:1.4,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",color:"var(--text)"},title:z,children:z}),(0,n.jsxs)("div",{style:{marginTop:2,display:"flex",gap:8,color:"var(--text-dim)",fontSize:11},children:[(0,n.jsx)("span",{title:e.modified,children:(p=new Date(e.modified),x=Math.floor((u=new Date().getTime()-p.getTime())/6e4),h=Math.floor(u/36e5),g=Math.floor(u/864e5),x<1?"just now":x<60?`${x}m ago`:h<24?`${h}h ago`:g<7?`${g}d ago`:p.toLocaleDateString())}),(0,n.jsxs)("span",{children:[e.messageCount," msgs"]})]})]}),a&&(0,n.jsx)("button",{onClick:e=>{e.stopPropagation(),c?.()},title:d?"Expand forks":"Collapse forks",style:{display:"flex",alignItems:"center",justifyContent:"center",width:20,height:20,padding:0,flexShrink:0,background:"none",border:"none",color:"var(--text-dim)",cursor:"pointer",transform:d?"rotate(-90deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})}),f&&(0,n.jsxs)("div",{style:{display:"flex",gap:4,flexShrink:0},children:[(0,n.jsx)("button",{onClick:T,title:"Rename",style:{display:"flex",alignItems:"center",justifyContent:"center",width:32,height:32,padding:0,background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="var(--bg-selected)",e.currentTarget.style.color="var(--accent)",e.currentTarget.style.borderColor="rgba(37,99,235,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},children:(0,n.jsx)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("path",{d:"M17 3a2.828 2.828 0 1 1 4 4L7.5 20.5 2 22l1.5-5.5L17 3z"})})}),(0,n.jsx)("button",{onClick:R,title:"Delete",style:{display:"flex",alignItems:"center",justifyContent:"center",width:32,height:32,padding:0,background:"var(--bg-hover)",border:"1px solid var(--border)",borderRadius:7,color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"background 0.12s, color 0.12s, border-color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.background="rgba(239,68,68,0.08)",e.currentTarget.style.color="#ef4444",e.currentTarget.style.borderColor="rgba(239,68,68,0.35)"},onMouseLeave:e=>{e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text-muted)",e.currentTarget.style.borderColor="var(--border)"},children:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"3 6 5 6 21 6"}),(0,n.jsx)("path",{d:"M19 6l-1 14a2 2 0 0 1-2 2H8a2 2 0 0 1-2-2L5 6"}),(0,n.jsx)("path",{d:"M10 11v6M14 11v6"}),(0,n.jsx)("path",{d:"M9 6V4a1 1 0 0 1 1-1h4a1 1 0 0 1 1 1v2"})]})})]})]})})}function U(e){if("assistant"!==e.role||!Array.isArray(e.content))return e;let t=e.content.map(e=>"toolCall"!==e.type?e:{type:"toolCall",toolCallId:e.toolCallId??e.id,toolName:e.toolName??e.name,input:e.input??e.arguments});return{...e,content:t}}var _=r(8426),V=r(2957);function J(e){if(navigator.clipboard?.writeText)return navigator.clipboard.writeText(e);try{let t=document.createElement("textarea");return t.value=e,t.style.position="fixed",t.style.opacity="0",document.body.appendChild(t),t.select(),document.execCommand("copy"),document.body.removeChild(t),Promise.resolve()}catch{return Promise.reject()}}function Y({message:e,isStreaming:t,toolResults:r,modelNames:o,entryId:i,onFork:l,forking:s,onNavigate:a,prevAssistantEntryId:d}){return"user"===e.role?(0,n.jsx)(K,{message:e,entryId:i,onFork:l,forking:s,onNavigate:a,prevAssistantEntryId:d}):"assistant"===e.role?(0,n.jsx)(Z,{message:e,isStreaming:t,toolResults:r,modelNames:o}):(e.role,null)}function K({message:e,entryId:t,onFork:r,forking:i,onNavigate:l,prevAssistantEntryId:s}){let[a,d]=(0,o.useState)(!1),[c,p]=(0,o.useState)(!1),u="string"==typeof e.content?e.content:e.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"),x=function(e){if(!e)return null;let t=new Date(e),r=new Date,n=t.getFullYear()===r.getFullYear()&&t.getMonth()===r.getMonth()&&t.getDate()===r.getDate(),o=t.toLocaleTimeString([],{hour:"2-digit",minute:"2-digit"});if(n)return o;let i=t.toLocaleDateString([],{month:"short",day:"numeric",year:t.getFullYear()!==r.getFullYear()?"numeric":void 0});return`${i} ${o}`}(e.timestamp),h=!!t&&!!r,g=!!s&&!!l;return(0,n.jsxs)("div",{style:{marginBottom:16,display:"flex",flexDirection:"column",alignItems:"flex-end"},onMouseEnter:()=>d(!0),onMouseLeave:()=>d(!1),children:[(0,n.jsx)("div",{style:{display:"flex",alignItems:"flex-end",gap:6,maxWidth:"85%"},children:(0,n.jsx)("div",{style:{flex:1,minWidth:0,background:"var(--user-bg)",border:"1px solid rgba(59,130,246,0.2)",borderRadius:12,padding:"8px 12px",fontSize:14,lineHeight:1.6,color:"var(--text)",whiteSpace:"pre-wrap",wordBreak:"break-word"},children:u})}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",gap:6,marginTop:3},children:[(0,n.jsx)("div",{style:{display:"flex",gap:3,opacity:+!!a,pointerEvents:a?"auto":"none",transition:"opacity 0.12s"},children:(0,n.jsxs)("button",{onClick:()=>{J(u).then(()=>{p(!0),setTimeout(()=>p(!1),1500)})},title:"Copy message",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:c?"var(--accent)":"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{c||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{c||(e.currentTarget.style.color="var(--text-dim)")},children:[c?(0,n.jsx)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"20 6 9 17 4 12"})}):(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),(0,n.jsx)("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),c?"Copied":"Copy"]})}),(h||g)&&(0,n.jsxs)("div",{style:{display:"flex",gap:3,opacity:a||i?1:0,pointerEvents:a||i?"auto":"none",transition:"opacity 0.12s"},children:[g&&(0,n.jsxs)("button",{onClick:()=>l(s),title:"Edit from here — branches within this session",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--accent)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)"},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"15 10 20 15 15 20"}),(0,n.jsx)("path",{d:"M4 4v7a4 4 0 0 0 4 4h12"})]}),"Edit from here"]}),h&&(0,n.jsxs)("button",{onClick:()=>r(t),disabled:i,title:i?"Creating new session…":"New session — creates an independent copy from here",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:i?"var(--accent)":"var(--text-dim)",cursor:i?"not-allowed":"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",transition:"color 0.12s"},onMouseEnter:e=>{i||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{i||(e.currentTarget.style.color="var(--text-dim)")},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),i?"Creating…":"New session"]})]}),x&&(0,n.jsx)("span",{style:{fontSize:10,color:"var(--text-dim)"},children:x})]})]})}function Z({message:e,isStreaming:t,toolResults:r,modelNames:i}){var l;let s,a=e.content??[],[d,c]=(0,o.useState)(!1),[p,u]=(0,o.useState)(!1),x=a.filter(e=>"text"===e.type).map(e=>e.text).join("\n");return(0,n.jsxs)("div",{style:{marginBottom:16},onMouseEnter:()=>c(!0),onMouseLeave:()=>c(!1),children:[(0,n.jsxs)("div",{style:{fontSize:11,color:"var(--text-dim)",marginBottom:4,display:"flex",alignItems:"center",gap:6},children:[e.provider&&(0,n.jsx)("span",{children:i?.[e.model]??e.model}),t&&(0,n.jsx)("span",{style:{display:"inline-block",width:6,height:6,borderRadius:"50%",background:"var(--accent)",animation:"blink 1s infinite"}})]}),(0,n.jsx)("div",{style:{display:"flex",flexDirection:"column",gap:8},children:a.map((e,t)=>(0,n.jsx)(q,{block:e,toolResults:r},t))}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:8,marginTop:4},children:[e.usage&&!t&&(0,n.jsx)("div",{style:{fontSize:11,color:"var(--text-dim)"},children:(l=e.usage,s=[],l.input&&s.push(`${l.input.toLocaleString()} in`),l.output&&s.push(`${l.output.toLocaleString()} out`),l.cacheRead&&s.push(`${l.cacheRead.toLocaleString()} cache`),l.cost?.total&&s.push(`$${l.cost.total.toFixed(4)}`),s.join(" \xb7 "))}),x&&!t&&(0,n.jsxs)("button",{onClick:()=>{J(x).then(()=>{u(!0),setTimeout(()=>u(!1),1500)})},title:"Copy message",style:{display:"flex",alignItems:"center",gap:4,padding:"3px 8px",height:22,background:"none",border:"none",borderRadius:5,color:p?"var(--accent)":"var(--text-dim)",cursor:"pointer",fontSize:11,fontWeight:400,whiteSpace:"nowrap",opacity:+!!d,pointerEvents:d?"auto":"none",transition:"opacity 0.12s, color 0.12s"},onMouseEnter:e=>{p||(e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{p||(e.currentTarget.style.color="var(--text-dim)")},children:[p?(0,n.jsx)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"20 6 9 17 4 12"})}):(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"9",y:"9",width:"13",height:"13",rx:"2",ry:"2"}),(0,n.jsx)("path",{d:"M5 15H4a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h9a2 2 0 0 1 2 2v1"})]}),p?"Copied":"Copy"]})]})]})}function q({block:e,toolResults:t}){if("text"===e.type)return(0,n.jsx)(X,{block:e});if("thinking"===e.type)return(0,n.jsx)(Q,{block:e});if("toolCall"===e.type){let r=t?.get(e.toolCallId);return(0,n.jsx)(G,{block:e,result:r})}return null}function X({block:e}){return(0,n.jsx)("div",{className:"markdown-body",children:(0,n.jsx)(_.oz,{remarkPlugins:[V.A],components:{code({className:e,children:t,...r}){let o=e?.includes("language-"),i=e?.replace("language-","")??"";return o?(0,n.jsx)(et,{code:String(t).replace(/\n$/,""),lang:i}):(0,n.jsx)("code",{style:{background:"var(--bg-selected)",padding:"1px 4px",borderRadius:3,fontFamily:"var(--font-mono)",fontSize:"0.9em"},...r,children:t})},pre:({children:e})=>(0,n.jsx)(n.Fragment,{children:e})},children:e.text})})}function Q({block:e}){let[t,r]=(0,o.useState)(!1);return(0,n.jsxs)("div",{style:{border:"1px solid var(--border)",borderRadius:6,overflow:"hidden",fontSize:13},children:[(0,n.jsxs)("button",{onClick:()=>r(e=>!e),style:{display:"flex",alignItems:"center",gap:6,width:"100%",padding:"6px 10px",background:"var(--bg-panel)",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left"},children:[(0,n.jsx)("span",{children:t?"▼":"▶"}),(0,n.jsx)("span",{children:"Thinking"})]}),t&&(0,n.jsx)("div",{style:{padding:"8px 10px",color:"var(--text-muted)",fontSize:12,lineHeight:1.6,whiteSpace:"pre-wrap",background:"var(--bg-panel)",borderTop:"1px solid var(--border)"},children:e.thinking})]})}function G({block:e,result:t}){let[r,i]=(0,o.useState)(!1),l=JSON.stringify(e.input,null,2),s=t?t.content.filter(e=>"text"===e.type).map(e=>e.text).join("\n"):null,a=null!==s&&("(no output)"===s.trim()||""===s.trim()),d=a?"(no output)":(s??"").slice(0,200).replace(/\n/g," "),c=t?.isError??!1;return(0,n.jsxs)("div",{style:{borderRadius:7,overflow:"hidden",fontSize:12,border:"1px solid rgba(34,197,94,0.25)",background:"rgba(34,197,94,0.04)"},children:[(0,n.jsxs)("button",{onClick:()=>i(e=>!e),style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"6px 10px",background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left",minWidth:0},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 12 12",fill:"none",style:{flexShrink:0,color:"#16a34a"},children:[(0,n.jsx)("circle",{cx:"6",cy:"6",r:"5",stroke:"currentColor",strokeWidth:"1.4"}),(0,n.jsx)("polygon",{points:"4.5,3.5 9,6 4.5,8.5",fill:"currentColor"})]}),(0,n.jsx)("span",{style:{color:"#16a34a",fontFamily:"var(--font-mono)",fontWeight:600,fontSize:11,flexShrink:0},children:e.toolName}),(0,n.jsx)("span",{style:{color:"var(--text-dim)",fontFamily:"var(--font-mono)",fontSize:11,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0},children:function(e){let t=e.input;if(!t||"object"!=typeof t)return"";let r=Object.keys(t);return 0===r.length?"":"command"in t?String(t.command).slice(0,120):"path"in t?String(t.path).slice(0,120):"file_path"in t?String(t.file_path).slice(0,120):"pattern"in t?String(t.pattern).slice(0,120):"query"in t?String(t.query).slice(0,120):String(t[r[0]]).slice(0,120)}(e)}),(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:r?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),r&&(0,n.jsx)("pre",{style:{margin:0,padding:"8px 10px",color:"var(--text-muted)",fontSize:12,lineHeight:1.5,overflow:"auto",background:"rgba(0,0,0,0.03)",borderTop:"1px solid rgba(34,197,94,0.2)",whiteSpace:"pre-wrap",wordBreak:"break-all"},children:l}),t&&(0,n.jsx)(ee,{text:s??"",preview:d,isEmpty:a,isError:c,forceExpanded:r,onToggle:()=>i(e=>!e)})]})}function ee({text:e,preview:t,isEmpty:r,isError:o,forceExpanded:i,onToggle:l}){let s=i??!1;return(0,n.jsxs)("div",{style:{borderTop:`1px solid ${o?"rgba(248,113,113,0.3)":"rgba(34,197,94,0.15)"}`,background:o?"rgba(248,113,113,0.04)":"rgba(0,0,0,0.02)"},children:[(0,n.jsxs)("button",{onClick:()=>!r&&l?l():void 0,style:{display:"flex",alignItems:"center",gap:7,width:"100%",padding:"5px 10px",background:"none",border:"none",color:o?"#f87171":"var(--text-dim)",cursor:r?"default":"pointer",textAlign:"left",fontSize:11,minWidth:0},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 12 12",fill:"none",style:{flexShrink:0,color:o?"#f87171":"var(--text-dim)"},children:[(0,n.jsx)("polyline",{points:"4 2 2 6 4 10",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round",strokeLinejoin:"round"}),(0,n.jsx)("path",{d:"M2 6h7a2 2 0 0 0 0-4H7",stroke:"currentColor",strokeWidth:"1.5",strokeLinecap:"round"})]}),!s&&(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0,fontStyle:r?"italic":"normal",opacity:r?.5:1,color:o?"#f87171":"var(--text-dim)"},children:t}),!r&&!o&&(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0,transform:s?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),s&&(0,n.jsx)("pre",{style:{margin:0,padding:"8px 10px",color:o?"#f87171":"var(--text-muted)",fontSize:12,lineHeight:1.5,overflow:"auto",maxHeight:400,background:"var(--bg)",borderTop:`1px solid ${o?"rgba(248,113,113,0.2)":"rgba(34,197,94,0.15)"}`,whiteSpace:"pre-wrap",wordBreak:"break-all"},children:e})]})}function et({code:e,lang:t}){let[r,i]=(0,o.useState)(!1);return(0,n.jsxs)("div",{style:{position:"relative",marginTop:4,marginBottom:4,borderRadius:6,overflow:"hidden",border:"1px solid var(--border)"},children:[t&&(0,n.jsxs)("div",{style:{padding:"3px 10px",background:"var(--bg-panel)",borderBottom:"1px solid var(--border)",fontSize:11,color:"var(--text-dim)",display:"flex",justifyContent:"space-between",alignItems:"center"},children:[(0,n.jsx)("span",{children:t}),(0,n.jsx)("button",{onClick:()=>{J(e).then(()=>{i(!0),setTimeout(()=>i(!1),1500)})},style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:11},children:r?"copied":"copy"})]}),(0,n.jsx)("pre",{style:{margin:0,padding:"10px 12px",background:"var(--bg)",fontSize:12.5,lineHeight:1.6,overflowX:"auto",color:"var(--text)"},children:(0,n.jsx)("code",{children:e})})]})}function er(e){return e>=1e6?`${(e/1e6).toFixed(1)}M`:e>=1e3?`${(e/1e3).toFixed(0)}k`:String(e)}let en=["off","default","full"],eo={off:"none",default:"default",full:"full"};function ei({onSend:e,onAbort:t,onSteer:r,onFollowUp:i,isStreaming:l,model:s,modelNames:a,modelList:d,onModelChange:c,onCompact:p,onAbortCompaction:u,isCompacting:x,compactError:h,toolPreset:g,onToolPresetChange:f,sessionStats:v,retryInfo:y,contextUsage:m}){let[b,j]=(0,o.useState)(""),[k,S]=(0,o.useState)("steer"),[w,C]=(0,o.useState)(!1),L=(0,o.useRef)(null),z=(0,o.useRef)(null),T=(0,o.useCallback)(()=>{let t=b.trim();t&&!l&&(e(t),j(""),L.current&&(L.current.style.height="auto"))},[b,l,e]),I=(0,o.useCallback)(()=>{let e=b.trim();e&&("steer"===k&&r?r(e):"followup"===k&&i&&i(e),j(""),L.current&&(L.current.style.height="auto"))},[b,k,r,i]),R=(0,o.useCallback)(e=>{"Enter"!==e.key||e.shiftKey||e.nativeEvent.isComposing||(e.preventDefault(),l&&(r||i)?I():T())},[l,r,i,I,T]),M=(0,o.useCallback)(()=>{let e=L.current;e&&(e.style.height="auto",e.style.height=`${Math.min(e.scrollHeight,200)}px`)},[]),W=d&&d.length>0?d.map(e=>({provider:e.provider,modelId:e.id,name:`${e.name} (${e.provider})`})):Object.entries(a??{}).map(([e,t])=>({provider:s?.provider??"unknown",modelId:e,name:t})),E=s?W.find(e=>e.modelId===s.modelId&&e.provider===s.provider)?.name??s.modelId:W.length>0?W[0].name:null;return(0,o.useEffect)(()=>{let e=e=>{z.current&&!z.current.contains(e.target)&&C(!1)};return document.addEventListener("mousedown",e),()=>document.removeEventListener("mousedown",e)},[]),(0,n.jsx)("div",{style:{flexShrink:0,borderTop:"1px solid var(--border)",background:"var(--bg-panel)",padding:"10px 16px 12px",paddingRight:52},children:(0,n.jsxs)("div",{style:{maxWidth:820,margin:"0 auto"},children:[y&&(0,n.jsxs)("div",{style:{marginBottom:8,padding:"5px 10px",background:"rgba(234,179,8,0.08)",border:"1px solid rgba(234,179,8,0.25)",borderRadius:6,fontSize:12,color:"rgba(180,130,0,0.9)",display:"flex",alignItems:"center",gap:6},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{flexShrink:0},children:[(0,n.jsx)("path",{d:"M3 12a9 9 0 1 0 9-9 9.75 9.75 0 0 0-6.74 2.74L3 8"}),(0,n.jsx)("path",{d:"M3 3v5h5"})]}),"Retrying (",y.attempt,"/",y.maxAttempts,")…"]}),(0,n.jsxs)("div",{style:{display:"flex",gap:8,alignItems:"center",background:"var(--bg)",border:`1px solid ${l&&(r||i)?"steer"===k?"rgba(234,179,8,0.4)":"rgba(99,102,241,0.4)":"var(--border)"}`,borderRadius:8,padding:"8px 8px 8px 12px",transition:"border-color 0.15s"},children:[(0,n.jsx)("textarea",{ref:L,value:b,onChange:e=>j(e.target.value),onKeyDown:R,onInput:M,placeholder:l&&(r||i)?"steer"===k?"Inject guidance mid-run…":"Queue a message for after agent finishes…":l?"Agent is running…":"Message…",rows:1,style:{flex:1,background:"none",border:"none",outline:"none",resize:"none",color:"var(--text)",fontSize:14,lineHeight:1.6,fontFamily:"inherit",minHeight:24,maxHeight:200,overflow:"auto"}}),l?(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6,flexShrink:0},children:[(r||i)&&(0,n.jsxs)("button",{onClick:I,disabled:!b.trim(),style:{display:"flex",alignItems:"center",gap:5,padding:"7px 14px",background:b.trim()?"steer"===k?"rgba(234,179,8,0.12)":"rgba(129,140,248,0.12)":"none",border:`1px solid ${"steer"===k?"rgba(234,179,8,0.35)":"rgba(129,140,248,0.35)"}`,borderRadius:8,color:b.trim()?"steer"===k?"rgba(180,130,0,1)":"rgba(99,102,241,1)":"var(--text-dim)",cursor:b.trim()?"pointer":"not-allowed",fontSize:13,fontWeight:600,letterSpacing:"-0.01em",transition:"background 0.12s"},children:[(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"7",x2:"11",y2:"7"}),(0,n.jsx)("polyline",{points:"7.5 3 12 7 7.5 11"})]}),"Send"]}),(0,n.jsxs)("button",{onClick:t,style:{flexShrink:0,display:"flex",alignItems:"center",gap:6,padding:"7px 14px",background:"rgba(239,68,68,0.08)",border:"1px solid rgba(239,68,68,0.3)",borderRadius:8,color:"#ef4444",cursor:"pointer",fontSize:13,fontWeight:600,whiteSpace:"nowrap",letterSpacing:"-0.01em"},children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",children:(0,n.jsx)("rect",{x:"1.5",y:"1.5",width:"7",height:"7",rx:"1.5",fill:"currentColor"})}),"Stop"]})]}):(0,n.jsxs)("button",{onClick:T,disabled:!b.trim(),style:{flexShrink:0,display:"flex",alignItems:"center",gap:6,padding:"7px 14px",background:b.trim()?"var(--accent)":"var(--bg-panel)",border:"none",borderRadius:8,color:b.trim()?"#fff":"var(--text-dim)",cursor:b.trim()?"pointer":"not-allowed",fontSize:13,fontWeight:600,letterSpacing:"-0.01em",boxShadow:b.trim()?"0 1px 3px rgba(37,99,235,0.25)":"none",transition:"background 0.15s, box-shadow 0.15s"},children:[(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 14 14",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"7",x2:"11",y2:"7"}),(0,n.jsx)("polyline",{points:"7.5 3 12 7 7.5 11"})]}),"Send"]})]}),(0,n.jsxs)("div",{style:{marginTop:6,display:"flex",alignItems:"center",gap:8},children:[(0,n.jsx)("div",{style:{flex:"0 0 auto",display:"flex",alignItems:"center",gap:6},children:l&&(r||i)?(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-dim)"},children:"Send Mode"}),(0,n.jsx)("div",{style:{display:"flex",border:"1px solid var(--border)",borderRadius:6,overflow:"hidden"},children:["steer","followup"].map((e,t)=>{let r=k===e,o="steer"===e?"rgba(234,179,8,1)":"rgba(129,140,248,1)",i="steer"===e?"rgba(234,179,8,0.1)":"rgba(129,140,248,0.1)";return(0,n.jsx)("button",{onClick:()=>S(e),title:"steer"===e?"打断 Agent 当前运行":"在 Agent 完成后排队发送",style:{display:"flex",alignItems:"center",gap:5,padding:"4px 10px",background:r?i:"none",border:"none",borderLeft:t>0?`1px solid ${r?"transparent":"var(--border)"}`:"none",color:r?o:"var(--text-dim)",cursor:"pointer",fontSize:12,fontWeight:r?600:400,whiteSpace:"nowrap",transition:"background 0.12s, color 0.12s"},children:"steer"===e?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M5 1 L9 5 L5 9"}),(0,n.jsx)("line",{x1:"1",y1:"5",x2:"9",y2:"5"})]}),"Steer"]}):(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"1",x2:"5",y2:"6"}),(0,n.jsx)("polyline",{points:"2.5 3.5 5 1 7.5 3.5"}),(0,n.jsx)("line",{x1:"2",y1:"9",x2:"8",y2:"9"})]}),"Follow-up"]})},e)})})]}):W.length>0&&E&&c&&(0,n.jsxs)("div",{ref:z,style:{position:"relative"},children:[(0,n.jsxs)("button",{onClick:()=>C(e=>!e),disabled:l,style:{display:"flex",alignItems:"center",gap:5,padding:"4px 10px",background:"none",border:"1px solid var(--border)",borderRadius:5,color:"var(--text-muted)",cursor:l?"not-allowed":"pointer",fontSize:12,opacity:l?.5:1,transition:"border-color 0.12s, color 0.12s"},onMouseEnter:e=>{l||(e.currentTarget.style.borderColor="rgba(37,99,235,0.4)",e.currentTarget.style.color="var(--accent)")},onMouseLeave:e=>{e.currentTarget.style.borderColor="var(--border)",e.currentTarget.style.color="var(--text-muted)"},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]}),E,(0,n.jsx)("svg",{width:"9",height:"9",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",strokeLinejoin:"round",style:{transform:w?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),w&&(0,n.jsx)("div",{style:{position:"absolute",bottom:"calc(100% + 6px)",left:0,zIndex:100,background:"var(--bg)",border:"1px solid var(--border)",borderRadius:8,boxShadow:"0 -4px 16px rgba(0,0,0,0.10)",overflow:"hidden",minWidth:160},children:W.map(e=>{let t=e.modelId===s?.modelId&&e.provider===s?.provider;return(0,n.jsxs)("button",{onClick:()=>{C(!1),t||c(e.provider,e.modelId)},style:{display:"flex",alignItems:"center",gap:8,width:"100%",padding:"8px 12px",background:t?"var(--bg-selected)":"none",border:"none",borderBottom:"1px solid var(--border)",color:t?"var(--text)":"var(--text-muted)",cursor:"pointer",fontSize:12,textAlign:"left",fontWeight:t?600:400},onMouseEnter:e=>{t||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{t||(e.currentTarget.style.background="none")},children:[t?(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--accent)",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("polyline",{points:"1.5 5 4 7.5 8.5 2.5"})}):(0,n.jsx)("span",{style:{width:10,flexShrink:0}}),e.name]},`${e.provider}:${e.modelId}`)})})]})}),(0,n.jsx)("div",{className:"chat-stats-center",style:{flex:1,display:"flex",justifyContent:"center"},children:(v||m)&&(()=>{let e=[],t=[];if(v){let r=v.tokens;(r.input||0)+(r.output||0)+(r.cacheRead||0)+(r.cacheWrite||0)&&(r.input&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"8.5",x2:"5",y2:"1.5"}),(0,n.jsx)("polyline",{points:"2 4 5 1.5 8 4"})]}),er(r.input)]},"in")),r.output&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"5",y1:"1.5",x2:"5",y2:"8.5"}),(0,n.jsx)("polyline",{points:"2 6 5 8.5 8 6"})]}),er(r.output)]},"out")),r.cacheRead&&e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M8.5 5a3.5 3.5 0 1 1-1-2.45"}),(0,n.jsx)("polyline",{points:"6.5 1.5 8.5 2.5 7.5 4.5"})]}),er(r.cacheRead)]},"cache")),t.push(`in: ${r.input?.toLocaleString()??0} out: ${r.output?.toLocaleString()??0} cache read: ${r.cacheRead?.toLocaleString()??0} cache write: ${r.cacheWrite?.toLocaleString()??0}`))}let r="var(--text-dim)";if(m?.contextWindow){let o=m.percent;null!==o&&o>90?r="#ef4444":null!==o&&o>70&&(r="rgba(234,179,8,0.9)");let i=null!==o?`${o.toFixed(0)}% / ${er(m.contextWindow)}`:`? / ${er(m.contextWindow)}`;e.push((0,n.jsxs)("span",{style:{display:"flex",alignItems:"center",gap:3,color:r},children:[(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M1 9 L1 5 Q1 1 5 1 Q9 1 9 5 L9 9"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"9",y2:"9"})]}),i]},"ctx")),t.push(`context: ${null!==o?o.toFixed(1)+"%":"unknown"} of ${m.contextWindow.toLocaleString()} tokens`)}return e.length?(0,n.jsx)("div",{title:t.join(" | "),style:{display:"flex",alignItems:"center",gap:7,fontSize:11,color:"var(--text-dim)",whiteSpace:"nowrap",cursor:"default"},children:e}):null})()}),(0,n.jsxs)("div",{style:{flex:"0 0 auto",display:"flex",alignItems:"center",gap:6,marginLeft:"auto"},children:[f&&(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:4},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-dim)"},children:"tools"}),(0,n.jsx)("div",{style:{display:"flex",alignItems:"center",border:"1px solid var(--border)",borderRadius:5,overflow:"hidden"},children:en.map((e,t)=>{let r=eo[e],o=(g??"default")===r,i=t>0?eo[en[t-1]]:null;return(0,n.jsx)("button",{onClick:()=>!l&&f(r),disabled:l,title:"off"===e?"无工具,纯聊天,不浪费一个 token":"default"===e?"4 项内置工具":"使用全部内置工具",style:{padding:"4px 10px",background:o?"var(--bg-selected)":"none",border:"none",borderLeft:t>0?`1px solid ${o||null!==i&&(g??"default")===i?"transparent":"var(--border)"}`:"none",color:o?"var(--accent)":"var(--text-dim)",cursor:l?"not-allowed":"pointer",fontSize:12,fontWeight:o?600:400,opacity:l?.5:1},children:e},e)})})]}),p&&(0,n.jsxs)("div",{style:{position:"relative"},children:[h&&(0,n.jsx)("div",{style:{position:"absolute",bottom:"calc(100% + 6px)",right:0,background:"#1f2937",color:"#f87171",fontSize:11,padding:"4px 8px",borderRadius:5,whiteSpace:"nowrap",pointerEvents:"none",boxShadow:"0 2px 8px rgba(0,0,0,0.2)",zIndex:50},children:h}),(0,n.jsx)("button",{onClick:x?u:p,disabled:l&&!x,style:{display:"flex",alignItems:"center",gap:4,padding:"4px 10px",background:"none",border:`1px solid ${x?"rgba(239,68,68,0.4)":"var(--border)"}`,borderRadius:5,color:x?"#ef4444":"var(--text-muted)",cursor:l&&!x?"not-allowed":"pointer",fontSize:12,opacity:l&&!x?.5:1,transition:"border-color 0.12s, color 0.12s"},onMouseEnter:e=>{(!l||x)&&(e.currentTarget.style.borderColor=x?"rgba(239,68,68,0.7)":"rgba(37,99,235,0.4)",e.currentTarget.style.color=x?"#ef4444":"var(--accent)")},onMouseLeave:e=>{e.currentTarget.style.borderColor=x?"rgba(239,68,68,0.4)":"var(--border)",e.currentTarget.style.color=x?"#ef4444":"var(--text-muted)"},title:x?"停止压缩":"压缩上下文",children:x?(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",children:(0,n.jsx)("rect",{x:"2",y:"2",width:"6",height:"6",rx:"1",fill:"currentColor"})}),"Compacting…"]}):(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("polyline",{points:"4 14 10 14 10 20"}),(0,n.jsx)("polyline",{points:"20 10 14 10 14 4"}),(0,n.jsx)("line",{x1:"10",y1:"14",x2:"3",y2:"21"}),(0,n.jsx)("line",{x1:"21",y1:"3",x2:"14",y2:"10"})]}),"Compact"]})})]})]})]})]})})}function el(e){let t=e,r=0;for(;1===t.children.length;)t=t.children[0],r++;return{node:t,skipped:r}}function es({node:e,activePathIds:t,depth:r,isLast:o,parentLines:i,onSelect:l}){let{node:s,skipped:a}=el(e),d=t.has(s.entry.id),c=t.has(e.entry.id)||t.has(s.entry.id),p=function(e){if("message"===e.type&&"message"in e){let t=e.message,r=t.content,n="";if("string"==typeof r?n=r:Array.isArray(r)&&(n=r.filter(e=>"text"===e.type).map(e=>e.text).join(" ")),n.length>40&&(n=n.slice(0,40)+"…"),n)return n;if("assistant"===t.role)return"[assistant]"}return e.type}(s.entry),u="message"===s.entry.type&&"message"in s.entry?s.entry.message.role:null;return(0,n.jsxs)("div",{children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",height:24,cursor:"pointer"},onClick:()=>l(s.entry.id),children:[i.map((e,t)=>(0,n.jsx)("div",{style:{width:16,flexShrink:0,position:"relative",height:"100%",alignSelf:"stretch"},children:e&&(0,n.jsx)("div",{style:{position:"absolute",left:7,top:0,bottom:0,width:1,background:"var(--border)"}})},t)),(0,n.jsxs)("div",{style:{width:16,flexShrink:0,position:"relative",height:"100%",alignSelf:"stretch"},children:[(0,n.jsx)("div",{style:{position:"absolute",left:7,top:0,bottom:o?"50%":0,width:1,background:"var(--border)"}}),(0,n.jsx)("div",{style:{position:"absolute",left:7,top:"50%",width:9,height:1,background:"var(--border)"}})]}),(0,n.jsx)("div",{style:{width:7,height:7,borderRadius:"50%",flexShrink:0,background:d?"var(--accent)":c?"var(--text-muted)":"var(--border)",border:d?"none":"1px solid var(--text-dim)",marginRight:6,transition:"background 0.12s"}}),u&&(0,n.jsx)("span",{style:{fontSize:9,fontFamily:"var(--font-mono)",color:"user"===u?"var(--accent)":"var(--text-dim)",background:"user"===u?"rgba(37,99,235,0.08)":"var(--bg-hover)",border:`1px solid ${"user"===u?"rgba(37,99,235,0.2)":"var(--border)"}`,borderRadius:3,padding:"0 4px",marginRight:5,flexShrink:0,lineHeight:"16px"},children:"user"===u?"U":"A"}),a>0&&(0,n.jsxs)("span",{style:{fontSize:10,color:"var(--text-dim)",marginRight:5,flexShrink:0},children:["+",a]}),(0,n.jsx)("span",{style:{fontSize:11,color:d?"var(--text)":c?"var(--text-muted)":"var(--text-dim)",fontWeight:d?500:400,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap",flex:1,minWidth:0},children:p})]}),s.children.map((e,a)=>(0,n.jsx)(es,{node:e,activePathIds:t,depth:r+1,isLast:a===s.children.length-1,parentLines:[...i,!o],onSelect:l},e.entry.id))]})}function ea({tree:e,activeLeafId:t,onLeafChange:r}){let[i,l]=(0,o.useState)(!1),s=(0,o.useMemo)(()=>t?new Set(function e(r,n){for(let o of r){let r=[...n,o.entry.id];if(o.entry.id===t)return r;let i=e(o.children,r);if(i)return i}return null}(e,[])??[]):new Set,[e,t]),a=(0,o.useCallback)(e=>{r(e)},[r]);if(!function e(t){for(let r of t)if(r.children.length>1||e(r.children))return!0;return!1}(e))return null;let{node:d}=e.length>0?el(e[0]):{node:e[0]};return!d||d.children.length<=1?null:(0,n.jsxs)("div",{style:{borderBottom:"1px solid var(--border)",background:"var(--bg)",flexShrink:0,position:"relative"},children:[(0,n.jsxs)("button",{onClick:()=>l(e=>!e),style:{display:"flex",alignItems:"center",gap:6,width:"100%",padding:"5px 12px",background:"none",border:"none",cursor:"pointer",color:"var(--text-muted)",fontSize:11,textAlign:"left"},children:[(0,n.jsxs)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{color:"var(--accent)",flexShrink:0},children:[(0,n.jsx)("line",{x1:"6",y1:"3",x2:"6",y2:"15"}),(0,n.jsx)("circle",{cx:"18",cy:"6",r:"3"}),(0,n.jsx)("circle",{cx:"6",cy:"18",r:"3"}),(0,n.jsx)("path",{d:"M18 9a9 9 0 0 1-9 9"})]}),(0,n.jsx)("span",{style:{color:"var(--text-muted)"},children:"Branches"}),(0,n.jsx)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"var(--text-dim)",strokeWidth:"1.6",strokeLinecap:"round",strokeLinejoin:"round",style:{marginLeft:2,transform:i?"rotate(180deg)":"none",transition:"transform 0.15s"},children:(0,n.jsx)("polyline",{points:"2 3.5 5 6.5 8 3.5"})})]}),i&&(0,n.jsx)("div",{style:{position:"absolute",top:"100%",left:0,right:0,padding:"4px 12px 8px 12px",maxHeight:260,overflowY:"auto",background:"var(--bg)",borderBottom:"1px solid var(--border)",boxShadow:"0 4px 12px rgba(0,0,0,0.1)",zIndex:100},children:d.children.map((e,t)=>(0,n.jsx)(es,{node:e,activePathIds:s,depth:0,isLast:t===d.children.length-1,parentLines:[],onSelect:a},e.entry.id))})]})}function ed(e){if("user"===e.role){let t=e.content;return"string"==typeof t?t.slice(0,200):Array.isArray(t)?t.filter(e=>"text"===e.type&&e.text).map(e=>e.text).join("\n").slice(0,200):""}if("assistant"===e.role){let t=e.content??[],r=t.filter(e=>"text"===e.type).map(e=>e.text).join(" ");if(r)return r.slice(0,200);let n=t.filter(e=>"toolCall"===e.type).map(e=>e.toolName);if(n.length)return n.join(", ")}return""}function ec(e){return"user"===e.role?{bg:"rgba(37,99,235,0.18)",border:"rgba(37,99,235,0.7)"}:{bg:"rgba(107,114,128,0.12)",border:"rgba(107,114,128,0.5)"}}function ep({messages:e,streamingMessage:t,scrollContainer:r,messageRefs:i}){let[l,s]=(0,o.useState)(0),[a,d]=(0,o.useState)(1),[c,p]=(0,o.useState)(!1),[u,x]=(0,o.useState)([]),[h,g]=(0,o.useState)(!1),[f,v]=(0,o.useState)(null),y=(0,o.useRef)(!1),m=(0,o.useRef)(null),b=(0,o.useMemo)(()=>t?[...e,t]:e,[e,t]),j=(0,o.useCallback)(()=>{let e=r.current;if(!e)return;let t=e.scrollHeight,n=e.clientHeight,o=t-n;p(o>20),o<=0?(s(0),d(1)):(s(e.scrollTop/o),d(n/t));let l=i.current,a=[],c=0;for(let r=0;r<b.length;r++){let n=b[r];if("user"!==n.role&&"assistant"!==n.role||!("user"===n.role||"assistant"===n.role&&(n.content??[]).some(e=>"text"===e.type)))continue;let o=l?.[c];if(c++,o&&t>0){let r=o.getBoundingClientRect(),i=e.getBoundingClientRect(),l=r.top-i.top+e.scrollTop,s=r.height;a.push({topRatio:l/t,heightRatio:s/t,msg:n,index:a.length})}}x(a)},[r,i,b]);(0,o.useEffect)(()=>{let e=r.current;if(!e)return;e.addEventListener("scroll",j,{passive:!0});let t=new ResizeObserver(j);return t.observe(e),e.firstElementChild&&t.observe(e.firstElementChild),j(),()=>{e.removeEventListener("scroll",j),t.disconnect()}},[r,j]),(0,o.useEffect)(()=>{let e=setTimeout(j,50);return()=>clearTimeout(e)},[e.length,t,j]);let k=(0,o.useCallback)(e=>{let t=r.current;if(!t)return;let n=t.scrollHeight-t.clientHeight;n<=0||(t.scrollTop=Math.max(0,Math.min(1-a,e))/(1-a)*n)},[r,a]),S=(0,o.useCallback)(e=>{if(!c)return;y.current=!0;let t=e.currentTarget.getBoundingClientRect(),r=(e.clientY-t.top)/t.height,n=r-l*(1-a),o=n>=0&&n<=a?n:a/2;k(r-o);let i=e=>{y.current&&k((e.clientY-t.top)/t.height-o)},s=()=>{y.current=!1,window.removeEventListener("mousemove",i),window.removeEventListener("mouseup",s)};window.addEventListener("mousemove",i),window.addEventListener("mouseup",s)},[c,a,l,k]),w=m.current?.clientHeight??600,C=(0,o.useMemo)(()=>{if(!h||0===u.length)return[];let e=u.map(e=>Math.round(e.topRatio*w-11));for(let t=0;t<10;t++){for(let t=1;t<e.length;t++){let r=e[t-1]+22+2;e[t]<r&&(e[t]=r)}for(let t=e.length-2;t>=0;t--){let r=e[t+1]-22-2;e[t]>r&&(e[t]=r)}}for(let t=0;t<e.length;t++)e[t]=Math.max(0,Math.min(w-22,e[t]));return e},[h,u,w]);if(!c)return null;let L=l*(1-a)*100,z=100*a,T=null!==f&&u.length>0?u.reduce((e,t)=>Math.abs(t.topRatio-f)<Math.abs(u[e].topRatio-f)?t.index:e,0):null;return(0,n.jsxs)("div",{ref:m,onMouseDown:S,onMouseEnter:()=>g(!0),onMouseLeave:()=>{g(!1),v(null)},onMouseMove:e=>{let t=e.currentTarget.getBoundingClientRect();v((e.clientY-t.top)/t.height)},style:{width:36,flexShrink:0,position:"relative",cursor:"default",userSelect:"none",borderLeft:"1px solid var(--border)",background:"var(--bg-panel)",overflow:"visible"},children:[(0,n.jsx)("div",{style:{position:"absolute",left:0,right:0,top:`${L}%`,height:`${z}%`,background:"rgba(100,100,100,0.1)",borderTop:"1px solid rgba(100,100,100,0.2)",borderBottom:"1px solid rgba(100,100,100,0.2)",pointerEvents:"none",zIndex:1}}),u.map(e=>{let t=ec(e.msg),r=h&&T===e.index;ed(e.msg);let o="user"===e.msg.role,i=100*e.topRatio;return(0,n.jsx)("div",{style:{position:"absolute",top:`${i}%`,transform:"translateY(-50%)",left:0,right:0,height:"12px",display:"flex",alignItems:"center",justifyContent:"center",cursor:"pointer",zIndex:2},children:(0,n.jsx)("div",{style:{width:o?8:6,height:o?8:6,borderRadius:o?2:"50%",background:t.bg,border:`1.5px solid ${t.border}`,flexShrink:0,transition:"transform 0.1s",transform:r?"scale(1.6)":"scale(1)"}})},e.index)}),(0,n.jsx)("div",{style:{position:"absolute",left:"50%",top:0,bottom:0,width:1,background:"var(--border)",transform:"translateX(-50%)",zIndex:0}}),h&&u.map((e,t)=>{let r=ed(e.msg),o=ec(e.msg),i=T===e.index;return r&&0!==C.length?(0,n.jsx)("div",{style:{position:"absolute",top:C[t],right:"100%",marginRight:6,background:"var(--bg)",borderTop:`1px solid ${i?o.border:"var(--border)"}`,borderRight:`1px solid ${i?o.border:"var(--border)"}`,borderBottom:`1px solid ${i?o.border:"var(--border)"}`,borderLeft:`2px solid ${o.border}`,borderRadius:4,padding:"2px 7px",width:200,zIndex:100,pointerEvents:"none",opacity:i?1:.45,transition:"top 0.1s, opacity 0.1s"},children:(0,n.jsx)("div",{style:{fontSize:11,color:i?"var(--text)":"var(--text-muted)",lineHeight:1.4,whiteSpace:"nowrap",overflow:"hidden",textOverflow:"ellipsis"},children:r})},e.index):null})]})}function eu(e,t){switch(t.type){case"start":return{isStreaming:!0,streamingMessage:null};case"update":return{isStreaming:!0,streamingMessage:t.message};case"end":case"reset":return{isStreaming:!1,streamingMessage:null};default:return e}}function ex({session:e,newSessionCwd:t,onAgentEnd:i,onSessionCreated:l,onSessionForked:s}){var a;let d,c=null===e&&null!==t,[p,u]=(0,o.useState)(null),[x,h]=(0,o.useState)(!c),[g,f]=(0,o.useState)(null),[v,y]=(0,o.useState)(null),[m,b]=(0,o.useState)([]),[j,k]=(0,o.useState)([]),[S,w]=(0,o.useReducer)(eu,{isStreaming:!1,streamingMessage:null}),[C,L]=(0,o.useState)(!1),[z,T]=(0,o.useState)({}),[I,R]=(0,o.useState)([]),[M,W]=(0,o.useState)(null),[E,B]=(0,o.useState)("default"),[$,O]=(0,o.useState)(null),[P,D]=(0,o.useState)(null),F=(0,o.useRef)(null),H=(0,o.useRef)(null),N=(a=m.filter(e=>"user"===e.role||"assistant"===e.role).length,(d=(0,o.useRef)([])).current=Array(a).fill(null).map((e,t)=>d.current[t]??null),d),A=(0,o.useRef)(null),_=(0,o.useRef)(e?.id??null),V=(0,o.useRef)(!1),J=(0,o.useRef)(!1),K=(0,o.useRef)(null),Z=(0,o.useRef)(!1),q=(0,o.useCallback)((e="smooth")=>{F.current?.scrollIntoView({behavior:e})},[]),X=(0,o.useCallback)(()=>{let e=H.current,t=K.current;if(!e||!t)return;let r=t.getBoundingClientRect().top-e.getBoundingClientRect().top+e.scrollTop;e.scrollTo({top:r-16,behavior:"smooth"})},[]),Q=(0,o.useCallback)(async(e,t=!1)=>{try{t&&h(!0);let r=await fetch(`/api/sessions/${encodeURIComponent(e)}`);if(404===r.status){t&&(u(null),y(null),b([]),f(null));return}if(!r.ok)throw Error(`HTTP ${r.status}`);let n=await r.json();u(n),y(n.leafId),b(n.context.messages),k(n.context.entryIds??[]),ef(null),f(null)}catch(e){f(String(e))}finally{t&&h(!1)}},[]),G=(0,o.useMemo)(()=>{let e={input:0,output:0,cacheRead:0,cacheWrite:0};for(let t of m){if("assistant"!==t.role)continue;let r=t.usage;r&&(e.input+=r.input??0,e.output+=r.output??0,e.cacheRead+=r.cacheRead??0,e.cacheWrite+=r.cacheWrite??0)}return e.input+e.output+e.cacheRead+e.cacheWrite>0?{tokens:e}:null},[m]),ee=(0,o.useCallback)(async(e,t)=>{try{let r=t?`/api/sessions/${encodeURIComponent(e)}/context?leafId=${encodeURIComponent(t)}`:`/api/sessions/${encodeURIComponent(e)}/context`,n=await fetch(r);if(!n.ok)throw Error(`HTTP ${n.status}`);let o=await n.json();b(o.context.messages)}catch(e){console.error("Failed to load context:",e)}},[]),et=(0,o.useCallback)(e=>{A.current&&(A.current.close(),A.current=null);let t=new EventSource(`/api/agent/${encodeURIComponent(e)}/events`);A.current=t,t.onmessage=e=>{try{let t=JSON.parse(e.data);er(t)}catch{}},t.onerror=()=>{}},[]),er=(0,o.useCallback)(e=>{switch(e.type){case"agent_start":L(!0),w({type:"start"});break;case"agent_end":L(!1),O(null),J.current=!1,w({type:"end"}),_.current&&(Q(_.current),fetch(`/api/agent/${encodeURIComponent(_.current)}`).then(e=>e.json()).then(e=>{e.state?.contextUsage!==void 0&&D(e.state.contextUsage??null)}).catch(()=>{})),i?.();break;case"message_update":{let t=e.message;t&&w({type:"update",message:U(t)});break}case"message_end":{let t=e.message;t&&b(e=>[...e,U(t)]),w({type:"reset"});break}case"auto_retry_start":O({attempt:e.attempt,maxAttempts:e.maxAttempts});break;case"auto_retry_end":O(null);break;case"auto_compaction_start":eb(!0),ek(null);break;case"auto_compaction_end":eb(!1),e.errorMessage?ek(e.errorMessage):!e.aborted&&_.current&&Q(_.current)}},[Q,i]);(0,o.useEffect)(()=>(fetch("/api/models").then(e=>e.json()).then(e=>{if(T(e.models),e.modelList&&(R(e.modelList),c&&e.modelList.length>0)){let t=e.defaultModel,r=t&&e.modelList.find(e=>e.id===t.modelId&&e.provider===t.provider);W(r?{provider:r.provider,modelId:r.id}:{provider:e.modelList[0].provider,modelId:e.modelList[0].id})}}).catch(()=>{}),e&&(_.current=e.id,Q(e.id,!0),eI(e.id),fetch(`/api/agent/${encodeURIComponent(e.id)}`).then(e=>e.json()).then(t=>{t.running&&t.state?.isStreaming&&(L(!0),et(e.id)),t.state&&(void 0!==t.state.isCompacting&&eb(t.state.isCompacting),void 0!==t.state.contextUsage&&D(t.state.contextUsage??null))}).catch(()=>{})),()=>{A.current?.close(),A.current=null}),[]),(0,o.useEffect)(()=>{m.length>0&&(Z.current?(Z.current=!1,V.current=!0,X()):V.current?J.current||q("smooth"):(V.current=!0,q("instant")))},[m.length,q,X]);let en=(0,o.useCallback)(async e=>{y(e);let t=_.current;t&&(await ee(t,e),e&&fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"navigate_tree",targetId:e})}).catch(()=>{}))},[ee]),eo=(0,o.useCallback)(async n=>{if(!n.trim()||C)return;let o={role:"user",content:n,timestamp:Date.now()};b(e=>[...e,o]),L(!0),w({type:"start"}),J.current=!0,Z.current=!0;try{if(c&&t){let{PRESET_NONE:e,PRESET_DEFAULT:o,PRESET_FULL:i}=await r.e(451).then(r.bind(r,8451)),s="none"===E?e:"default"===E?o:i,a=await fetch("/api/agent/new",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({cwd:t,type:"prompt",message:n,toolNames:s,...M?{provider:M.provider,modelId:M.modelId}:{}})});if(!a.ok)throw Error(`HTTP ${a.status}`);let d=(await a.json()).sessionId;_.current=d,et(d),l?.({id:d,path:"",cwd:t,name:void 0,created:new Date().toISOString(),modified:new Date().toISOString(),messageCount:1,firstMessage:n})}else if(e){et(e.id);let t=await fetch(`/api/agent/${encodeURIComponent(e.id)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"prompt",message:n})});if(!t.ok)throw Error(`HTTP ${t.status}`)}}catch(e){console.error("Failed to send message:",e),L(!1),J.current=!1,w({type:"end"})}},[c,t,M,E,e,C,X,et,l]),el=(0,o.useCallback)(async()=>{let e=_.current;if(e)try{await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"abort"})})}catch(e){console.error("Failed to abort:",e)}},[]),[es,ed]=(0,o.useState)(null),ec=(0,o.useCallback)(async e=>{let t=_.current;if(t){ed(e);try{let r=await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"fork",entryId:e})});if(!r.ok)throw Error(`HTTP ${r.status}`);let{cancelled:n,newSessionId:o}=(await r.json()).data??{};!n&&o&&s?.(o)}catch(e){console.error("Fork failed:",e)}finally{ed(null)}}},[s]),eh=(0,o.useCallback)(async e=>{let t=_.current;t&&(fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"navigate_tree",targetId:e})}).catch(()=>{}),y(e),await ee(t,e))},[ee]),[eg,ef]=(0,o.useState)(null),ev=eg??p?.context.model??null,ey=c?M:ev,[em,eb]=(0,o.useState)(!1),[ej,ek]=(0,o.useState)(null);(0,o.useEffect)(()=>{if(!ej)return;let e=setTimeout(()=>ek(null),3e3);return()=>clearTimeout(e)},[ej]);let eS=(0,o.useCallback)(e=>{B(e)},[]),ew=(0,o.useCallback)(async(e,t)=>{if(c)return void W({provider:e,modelId:t});let r=_.current;if(r)try{await fetch(`/api/agent/${encodeURIComponent(r)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"set_model",provider:e,modelId:t})}),ef({provider:e,modelId:t})}catch(e){console.error("Failed to set model:",e)}},[c]),eC=(0,o.useCallback)(async()=>{let e=_.current;if(e&&!em){eb(!0),ek(null);try{let t=await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"compact"})}),r=await t.json();if(!t.ok||r.error)return void ek(r.error??`HTTP ${t.status}`);await Q(e,!0)}catch(e){ek(String(e))}finally{eb(!1)}}},[em,Q]),eL=(0,o.useCallback)(async e=>{let t=_.current;if(t)try{await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"steer",message:e})})}catch(e){console.error("Failed to steer:",e)}},[]),ez=(0,o.useCallback)(async e=>{let t=_.current;if(t)try{await fetch(`/api/agent/${encodeURIComponent(t)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"follow_up",message:e})})}catch(e){console.error("Failed to follow up:",e)}},[]),eT=(0,o.useCallback)(async()=>{let e=_.current;if(e)try{await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"abort_compaction"})})}catch(e){console.error("Failed to abort compaction:",e)}},[]),eI=(0,o.useCallback)(async e=>{try{let t=await fetch(`/api/agent/${encodeURIComponent(e)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"get_tools"})});if(!t.ok)return;let n=await t.json();if(n.data){let{getPresetFromTools:e}=await r.e(451).then(r.bind(r,8451));eS(e(n.data))}}catch(e){console.error("Failed to load tools:",e)}},[eS]),eR=(0,o.useCallback)(async e=>{let{PRESET_NONE:t,PRESET_DEFAULT:n,PRESET_FULL:o}=await r.e(451).then(r.bind(r,8451)),i="none"===e?t:"default"===e?n:o;eS(e);let l=_.current;if(l)try{await fetch(`/api/agent/${encodeURIComponent(l)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({type:"set_tools",toolNames:i})})}catch(e){console.error("Failed to set tools:",e)}},[eS]);return x?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)"},children:"Loading session..."}):g?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"#f87171"},children:g}):(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[p&&p.tree.length>0&&(0,n.jsx)(ea,{tree:p.tree,activeLeafId:v,onLeafChange:en}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsx)("div",{ref:H,style:{flex:1,overflowY:"auto",padding:"16px 0",scrollbarWidth:"none"},children:(0,n.jsxs)("div",{style:{maxWidth:820,margin:"0 auto",padding:"0 16px"},children:[(()=>{let e=new Map;for(let t of m)"toolResult"===t.role&&e.set(t.toolCallId,t);let t=-1;for(let e=m.length-1;e>=0;e--)if("user"===m[e].role){t=e;break}let r=0;return m.map((o,i)=>{let l="user"===o.role&&i>0&&"assistant"===m[i-1].role?j[i-1]:void 0,s="user"===o.role||"assistant"===o.role,a=s?r++:-1,d=(0,n.jsx)(Y,{message:o,toolResults:e,modelNames:z,entryId:j[i],onFork:C||c||0===i&&"user"===o.role?void 0:ec,forking:es===j[i],onNavigate:C?void 0:eh,prevAssistantEntryId:C?void 0:l},i);return s?(0,n.jsx)("div",{ref:e=>{N.current[a]=e,i===t&&(K.current=e)},children:d},i):d})})(),S.isStreaming&&S.streamingMessage&&(0,n.jsx)(Y,{message:S.streamingMessage,isStreaming:!0,modelNames:z}),S.isStreaming&&!S.streamingMessage&&(0,n.jsx)("div",{style:{padding:"8px 0",color:"var(--text-muted)",fontSize:13},children:(0,n.jsx)("span",{style:{animation:"pulse 1.5s infinite"},children:"Thinking..."})}),C&&(0,n.jsx)("div",{style:{height:H.current?H.current.clientHeight:"80vh"}}),(0,n.jsx)("div",{ref:F})]})}),(0,n.jsx)(ep,{messages:m,streamingMessage:S.streamingMessage,scrollContainer:H,messageRefs:N})]}),(0,n.jsx)("div",{style:{position:"relative"},children:(0,n.jsx)(ei,{onSend:eo,onAbort:el,onSteer:C?eL:void 0,onFollowUp:C?ez:void 0,isStreaming:C,model:ey,modelNames:z,modelList:I,onModelChange:ew,onCompact:e||c?eC:void 0,onAbortCompaction:eT,isCompacting:em,compactError:ej,toolPreset:E,onToolPresetChange:e||c?eR:void 0,sessionStats:G,retryInfo:$,contextUsage:P})})]})}function eh({filePath:e}){var t;let[r,i]=(0,o.useState)(null),[l,s]=(0,o.useState)(!0),[a,d]=(0,o.useState)(null),[c,p]=(0,o.useState)(!1);if((0,o.useEffect)(()=>{s(!0),d(null),i(null),p(!1);let t=e.split("/").filter(Boolean).join("/");fetch(`/api/files/${t}?type=read`).then(e=>e.json()).then(e=>{e.error?d(e.error):i(e)}).catch(e=>d(String(e))).finally(()=>s(!1))},[e]),l)return(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)",fontSize:13},children:"Loading..."});if(a)return(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"#f87171",fontSize:13},children:a});if(!r)return null;let u="html"===r.language,x=r.content.split("\n");return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:12,padding:"4px 16px",borderBottom:"1px solid var(--border)",fontSize:11,color:"var(--text-dim)",background:"var(--bg)",flexShrink:0},children:[(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)"},children:e}),(0,n.jsx)("span",{style:{marginLeft:"auto"},children:r.language}),!c&&(0,n.jsxs)("span",{children:[x.length," lines"]}),(0,n.jsx)("span",{children:(t=r.size)<1024?`${t} B`:t<1048576?`${(t/1024).toFixed(1)} KB`:`${(t/1048576).toFixed(1)} MB`}),u&&(0,n.jsxs)("div",{style:{display:"flex",borderRadius:5,overflow:"hidden",border:"1px solid var(--border)"},children:[(0,n.jsx)("button",{onClick:()=>p(!1),style:{padding:"2px 8px",fontSize:11,border:"none",cursor:"pointer",background:c?"var(--bg-hover)":"var(--bg-selected)",color:c?"var(--text-muted)":"var(--text)",fontWeight:c?400:600},children:"Source"}),(0,n.jsx)("button",{onClick:()=>p(!0),style:{padding:"2px 8px",fontSize:11,border:"none",borderLeft:"1px solid var(--border)",cursor:"pointer",background:c?"var(--bg-selected)":"var(--bg-hover)",color:c?"var(--text)":"var(--text-muted)",fontWeight:c?600:400},children:"Preview"})]})]}),u&&c?(0,n.jsx)("iframe",{srcDoc:r.content,sandbox:"allow-scripts",style:{flex:1,border:"none",background:"#fff"},title:"HTML preview"}):(0,n.jsx)("div",{style:{flex:1,overflow:"auto",background:"var(--bg)"},children:(0,n.jsx)("table",{style:{borderCollapse:"collapse",width:"100%",fontFamily:"var(--font-mono)",fontSize:13,lineHeight:1.6},children:(0,n.jsx)("tbody",{children:x.map((e,t)=>(0,n.jsxs)("tr",{style:{verticalAlign:"top"},children:[(0,n.jsx)("td",{style:{padding:"0 12px 0 16px",textAlign:"right",color:"var(--text-dim)",userSelect:"none",minWidth:48,fontSize:12,lineHeight:1.6,borderRight:"1px solid var(--border)",background:"var(--bg-panel)"},children:t+1}),(0,n.jsx)("td",{style:{padding:"0 16px",whiteSpace:"pre",color:"var(--text)",width:"100%"},children:e||"\xa0"})]},t))})})})]})}function eg(){return(0,n.jsx)("svg",{width:"12",height:"12",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:(0,n.jsx)("path",{d:"M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"})})}function ef({tabs:e,activeTabId:t,onSelectTab:r,onCloseTab:i}){let[l,s]=(0,o.useState)(null);return(0,n.jsx)("div",{style:{display:"flex",alignItems:"flex-end",background:"var(--bg-panel)",overflowX:"auto",flexShrink:0,height:36},children:e.map(e=>{let o=e.id===t;return(0,n.jsxs)("div",{onClick:()=>r(e.id),style:{display:"flex",alignItems:"center",gap:6,height:36,paddingLeft:12,paddingRight:"chat"===e.type?12:6,borderRight:"1px solid var(--border)",background:o?"var(--bg)":"var(--bg-panel)",cursor:"pointer",fontSize:12,color:o?"var(--text)":"var(--text-muted)",whiteSpace:"nowrap",maxWidth:180,minWidth:80,flexShrink:0,userSelect:"none",transition:"background 0.1s, color 0.1s"},children:[(0,n.jsx)("span",{style:{flexShrink:0,opacity:o?1:.7,display:"flex",alignItems:"center"},children:"chat"===e.type?(0,n.jsx)(eg,{}):E(e.label,13)}),(0,n.jsx)("span",{style:{overflow:"hidden",textOverflow:"ellipsis",flex:1,fontWeight:o?500:400},title:e.filePath??e.label,children:e.label}),"file"===e.type&&(0,n.jsx)("button",{onClick:t=>{t.stopPropagation(),i(e.id)},onMouseEnter:()=>s(e.id),onMouseLeave:()=>s(null),style:{display:"flex",alignItems:"center",justifyContent:"center",width:16,height:16,background:l===e.id?"var(--bg-hover)":"transparent",border:"none",borderRadius:3,color:l===e.id?"var(--text)":"var(--text-dim)",cursor:"pointer",padding:0,flexShrink:0,transition:"background 0.1s, color 0.1s"},title:"Close",children:(0,n.jsxs)("svg",{width:"10",height:"10",viewBox:"0 0 10 10",fill:"none",stroke:"currentColor",strokeWidth:"1.8",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"2",y1:"2",x2:"8",y2:"8"}),(0,n.jsx)("line",{x1:"8",y1:"2",x2:"2",y2:"8"})]})})]},e.id)})})}let ev=["openai-completions","openai-responses","anthropic-messages","google-generative-ai"];function ey({label:e,children:t}){return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:4},children:[(0,n.jsx)("label",{style:{fontSize:11,color:"var(--text-muted)",fontWeight:500},children:e}),t]})}let em={padding:"6px 9px",background:"var(--bg-panel)",border:"1px solid var(--border)",borderRadius:5,color:"var(--text)",fontSize:12,outline:"none",width:"100%",boxSizing:"border-box"};function eb({value:e,onChange:t,placeholder:r,mono:o}){return(0,n.jsx)("input",{value:e,onChange:e=>t(e.target.value),placeholder:r,style:{...em,fontFamily:o?"var(--font-mono)":"inherit"}})}function ej({value:e,onChange:t,placeholder:r}){return(0,n.jsx)("input",{type:"number",value:e,onChange:e=>t(e.target.value),placeholder:r,style:em})}function ek({value:e,onChange:t,options:r}){return(0,n.jsxs)("select",{value:e,onChange:e=>t(e.target.value),style:{...em,color:e?"var(--text)":"var(--text-dim)"},children:[(0,n.jsx)("option",{value:"",children:"— inherit / none —"}),r.map(e=>(0,n.jsx)("option",{value:e,children:e},e))]})}function eS({label:e,checked:t,onChange:r}){return(0,n.jsxs)("label",{style:{display:"flex",alignItems:"center",gap:6,cursor:"pointer",fontSize:12,color:"var(--text-muted)"},children:[(0,n.jsx)("input",{type:"checkbox",checked:t,onChange:e=>r(e.target.checked),style:{width:13,height:13,accentColor:"var(--accent)",cursor:"pointer"}}),e]})}function ew({children:e}){return(0,n.jsx)("div",{style:{fontSize:11,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.06em",marginBottom:2},children:e})}function eC({name:e,provider:t,onChange:r,onRename:i,onDelete:l}){let[s,a]=(0,o.useState)(e);(0,o.useEffect)(()=>a(e),[e]);let d=(e,n)=>r({...t,[e]:n});return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[(0,n.jsx)(ew,{children:"Provider"}),(0,n.jsx)("button",{onClick:l,style:{padding:"3px 8px",background:"none",border:"1px solid rgba(239,68,68,0.3)",borderRadius:4,color:"#ef4444",cursor:"pointer",fontSize:11},children:"Delete"})]}),(0,n.jsxs)(ey,{label:"Provider name",children:[(0,n.jsx)(eb,{value:s,onChange:a,placeholder:"provider-name",mono:!0}),s!==e&&s.trim()&&(0,n.jsx)("button",{onClick:()=>i(s.trim()),style:{marginTop:4,padding:"3px 10px",background:"var(--accent)",border:"none",borderRadius:4,color:"#fff",cursor:"pointer",fontSize:11,alignSelf:"flex-start"},children:"Rename"})]}),(0,n.jsx)(ey,{label:"Base URL",children:(0,n.jsx)(eb,{value:t.baseUrl??"",onChange:e=>d("baseUrl",e||void 0),placeholder:"https://api.example.com/v1",mono:!0})}),(0,n.jsxs)(ey,{label:"API Key",children:[(0,n.jsx)(eb,{value:t.apiKey??"",onChange:e=>d("apiKey",e||void 0),placeholder:"ENV_VAR_NAME, !shell-command, or literal key",mono:!0}),(0,n.jsxs)("span",{style:{fontSize:10,color:"var(--text-dim)",marginTop:2},children:["Prefix with ",(0,n.jsx)("code",{style:{fontFamily:"var(--font-mono)"},children:"!"})," to run a shell command, or use an env var name"]})]}),(0,n.jsx)(ey,{label:"API",children:(0,n.jsx)(ek,{value:t.api??"",onChange:e=>d("api",e||void 0),options:ev})})]})}function eL({model:e,onChange:t,onDelete:r}){let o=(r,n)=>t({...e,[r]:n});return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[(0,n.jsx)(ew,{children:"Model"}),(0,n.jsx)("button",{onClick:r,style:{padding:"3px 8px",background:"none",border:"1px solid rgba(239,68,68,0.3)",borderRadius:4,color:"#ef4444",cursor:"pointer",fontSize:11},children:"Remove"})]}),(0,n.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10},children:[(0,n.jsx)(ey,{label:"ID *",children:(0,n.jsx)(eb,{value:e.id,onChange:e=>o("id",e),placeholder:"model-id",mono:!0})}),(0,n.jsx)(ey,{label:"Name",children:(0,n.jsx)(eb,{value:e.name??"",onChange:e=>o("name",e||void 0),placeholder:"Display name"})})]}),(0,n.jsx)(ey,{label:"API override",children:(0,n.jsx)(ek,{value:e.api??"",onChange:e=>o("api",e||void 0),options:ev})}),(0,n.jsxs)("div",{style:{display:"flex",gap:20,flexWrap:"wrap"},children:[(0,n.jsx)(eS,{label:"Reasoning / thinking",checked:e.reasoning??!1,onChange:e=>o("reasoning",e||void 0)}),(0,n.jsx)(eS,{label:"Image input",checked:e.input?.includes("image")??!1,onChange:e=>o("input",e?["text","image"]:void 0)})]}),(0,n.jsxs)("div",{style:{display:"grid",gridTemplateColumns:"1fr 1fr",gap:10},children:[(0,n.jsx)(ey,{label:"Context window (tokens)",children:(0,n.jsx)(ej,{value:void 0!==e.contextWindow?String(e.contextWindow):"",onChange:e=>o("contextWindow",e?parseInt(e):void 0),placeholder:"128000"})}),(0,n.jsx)(ey,{label:"Max output tokens",children:(0,n.jsx)(ej,{value:void 0!==e.maxTokens?String(e.maxTokens):"",onChange:e=>o("maxTokens",e?parseInt(e):void 0),placeholder:"16384"})})]}),(0,n.jsxs)("div",{children:[(0,n.jsx)(ew,{children:"Cost (per million tokens)"}),(0,n.jsx)("div",{style:{marginTop:8,display:"grid",gridTemplateColumns:"1fr 1fr 1fr 1fr",gap:8},children:["input","output","cacheRead","cacheWrite"].map(r=>(0,n.jsx)(ey,{label:r,children:(0,n.jsx)(ej,{value:e.cost?.[r]!==void 0?String(e.cost[r]):"",onChange:n=>{let o;return o=parseFloat(n),void t({...e,cost:{...e.cost??{},[r]:isNaN(o)?void 0:o}})},placeholder:"0"})},r))})]})]})}function ez({provider:e,onRefresh:t}){let[r,i]=(0,o.useState)({phase:"idle"}),[l,s]=(0,o.useState)(""),a=(0,o.useRef)(null),d=(0,o.useRef)(null);(0,o.useEffect)(()=>{("auth"===r.phase||"prompt"===r.phase)&&setTimeout(()=>d.current?.focus(),50)},[r.phase]),(0,o.useEffect)(()=>{i({phase:"idle"}),s(""),a.current?.close(),a.current=null},[e.id]),(0,o.useEffect)(()=>()=>{a.current?.close()},[]);let c=(0,o.useCallback)(()=>{a.current?.close(),i({phase:"connecting"}),s("");let r=new EventSource(`/api/auth/login/${encodeURIComponent(e.id)}`);a.current=r,r.onmessage=e=>{let n=JSON.parse(e.data);"auth"===n.type?(i({phase:"auth",url:n.url,instructions:n.instructions??null,token:n.token}),window.open(n.url,"_blank","noopener,noreferrer")):"prompt_request"===n.type?i({phase:"prompt",message:n.message,placeholder:n.placeholder??null,token:n.token}):"progress"===n.type?i({phase:"progress",message:n.message}):"success"===n.type?(r.close(),i({phase:"success"}),t()):"error"===n.type?(r.close(),i({phase:"error",message:n.message})):"cancelled"===n.type&&(r.close(),i({phase:"idle"}))},r.onerror=()=>{r.close(),i(e=>"success"===e.phase?e:{phase:"error",message:"Connection lost"})}},[e.id,t]),p=(0,o.useCallback)(async()=>{await fetch(`/api/auth/logout/${encodeURIComponent(e.id)}`,{method:"POST"}),i({phase:"idle"}),t()},[e.id,t]),u=(0,o.useCallback)(async(t,r)=>{r.trim()&&(i({phase:"progress",message:"Verifying…"}),await fetch(`/api/auth/login/${encodeURIComponent(e.id)}`,{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({token:t,code:r.trim()})}),s(""))},[e.id]),x="connecting"===r.phase||"progress"===r.phase||"auth"===r.phase||"prompt"===r.phase;return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:16},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between"},children:[(0,n.jsx)(ew,{children:"Subscription"}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:6},children:[(0,n.jsx)("span",{style:{width:7,height:7,borderRadius:"50%",background:e.loggedIn?"#4ade80":"var(--border)",display:"inline-block"}}),(0,n.jsx)("span",{style:{fontSize:11,color:e.loggedIn?"#4ade80":"var(--text-dim)"},children:e.loggedIn?"connected":"not connected"})]})]}),(0,n.jsxs)("div",{style:{minHeight:48},children:["idle"===r.phase&&(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"var(--text-muted)",lineHeight:1.5},children:e.loggedIn?"Already connected. You can re-login or disconnect.":`Connect your ${e.name} account.`}),"connecting"===r.phase&&(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"var(--text-muted)"},children:"Opening browser…"}),("auth"===r.phase||"prompt"===r.phase)&&(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:10},children:[(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"var(--text-muted)",lineHeight:1.5},children:"auth"===r.phase?"Complete sign-in in the browser, then copy the redirect URL from the address bar and paste it below.":r.message}),(0,n.jsxs)("div",{style:{display:"flex",gap:6},children:[(0,n.jsx)("input",{ref:d,value:l,onChange:e=>s(e.target.value),onKeyDown:e=>{"Enter"===e.key&&u(r.token,l)},placeholder:"auth"===r.phase?"http://localhost:1455/auth/callback?code=…":r.placeholder??"Enter value…",style:{flex:1,padding:"6px 9px",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:5,color:"var(--text)",fontSize:12,outline:"none",fontFamily:"var(--font-mono)",boxSizing:"border-box"}}),(0,n.jsx)("button",{onClick:()=>u(r.token,l),disabled:!l.trim(),style:{padding:"6px 12px",background:l.trim()?"var(--accent)":"var(--bg-panel)",border:"none",borderRadius:5,color:l.trim()?"#fff":"var(--text-dim)",cursor:l.trim()?"pointer":"not-allowed",fontSize:12,fontWeight:600,flexShrink:0},children:"Submit"})]})]}),"progress"===r.phase&&(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"var(--text-muted)"},children:r.message}),"success"===r.phase&&(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"#4ade80"},children:"Connected successfully."}),"error"===r.phase&&(0,n.jsx)("p",{style:{margin:0,fontSize:12,color:"#f87171"},children:r.message})]}),(0,n.jsx)("div",{style:{display:"flex",gap:8},children:x?(0,n.jsx)("button",{onClick:()=>{a.current?.close(),i({phase:"idle"})},style:{padding:"5px 12px",background:"none",border:"1px solid var(--border)",borderRadius:5,color:"var(--text-muted)",cursor:"pointer",fontSize:12},children:"Cancel"}):(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)("button",{onClick:c,style:{padding:"5px 14px",background:"var(--accent)",border:"none",borderRadius:5,color:"#fff",cursor:"pointer",fontSize:12,fontWeight:600},children:e.loggedIn?"Re-login":"Login"}),e.loggedIn&&(0,n.jsx)("button",{onClick:p,style:{padding:"5px 12px",background:"none",border:"1px solid rgba(239,68,68,0.3)",borderRadius:5,color:"#ef4444",cursor:"pointer",fontSize:12},children:"Disconnect"})]})})]})}function eT({onClose:e}){let[t,r]=(0,o.useState)({providers:{}}),[i,l]=(0,o.useState)(!0),[s,a]=(0,o.useState)(!1),[d,c]=(0,o.useState)(null),[p,u]=(0,o.useState)(!1),[x,h]=(0,o.useState)(null),[g,f]=(0,o.useState)([]),v=(0,o.useCallback)(()=>{fetch("/api/auth/providers").then(e=>e.json()).then(e=>f(e.providers)).catch(()=>{})},[]);(0,o.useEffect)(()=>{fetch("/api/models-config").then(e=>e.json()).then(e=>{let t=e.providers?e:{...e,providers:{}};r(t);let n=Object.keys(t.providers??{});n.length>0&&h({type:"provider",name:n[0]})}).catch(()=>r({providers:{}})).finally(()=>l(!1)),v()},[v]);let y=(0,o.useCallback)(()=>{let e="new-provider",n=1;for(;t.providers?.[e];)e=`new-provider-${n++}`;r(t=>({...t,providers:{...t.providers??{},[e]:{}}})),h({type:"provider",name:e})},[t.providers]),m=(0,o.useCallback)((e,t)=>{r(r=>({...r,providers:{...r.providers??{},[e]:t}}))},[]),b=(0,o.useCallback)((e,t)=>{r(r=>{let n=Object.entries(r.providers??{}),o=n.findIndex(([t])=>t===e);return -1===o?r:(n[o]=[t,n[o][1]],{...r,providers:Object.fromEntries(n)})}),h(r=>r?"provider"===r.type&&r.name===e?{type:"provider",name:t}:"model"===r.type&&r.providerName===e?{...r,providerName:t}:r:r)},[]),j=(0,o.useCallback)(e=>{r(t=>{let r={...t.providers??{}};return delete r[e],{...t,providers:r}}),r(e=>{let t=Object.keys(e.providers??{});return h(t.length>0?{type:"provider",name:t[0]}:null),e})},[]),k=(0,o.useCallback)(e=>{r(t=>{let r=t.providers?.[e]??{},n=[...r.models??[],{id:""}];return{...t,providers:{...t.providers??{},[e]:{...r,models:n}}}}),r(t=>{let r=(t.providers?.[e]?.models?.length??1)-1;return h({type:"model",providerName:e,index:r}),t})},[]),S=(0,o.useCallback)((e,t,n)=>{r(r=>{let o=r.providers?.[e]??{},i=[...o.models??[]];return i[t]=n,{...r,providers:{...r.providers??{},[e]:{...o,models:i}}}})},[]),w=(0,o.useCallback)((e,t)=>{r(r=>{let n=r.providers?.[e]??{},o=[...n.models??[]];return o.splice(t,1),{...r,providers:{...r.providers??{},[e]:{...n,models:o.length?o:void 0}}}}),h({type:"provider",name:e})},[]),C=(0,o.useCallback)(async()=>{a(!0),c(null),u(!1);try{let e=await fetch("/api/models-config",{method:"PUT",headers:{"Content-Type":"application/json"},body:JSON.stringify(t)}),r=await e.json();!e.ok||r.error?c(r.error??`HTTP ${e.status}`):(u(!0),setTimeout(()=>u(!1),2e3))}catch(e){c(String(e))}finally{a(!1)}},[t]),L=Object.entries(t.providers??{}),z=(()=>{if(!x)return null;if("oauth"===x.type){let e=g.find(e=>e.id===x.providerId);return e?(0,n.jsx)(ez,{provider:e,onRefresh:v},e.id):null}if("provider"===x.type){let e=t.providers?.[x.name];return e?(0,n.jsx)(eC,{name:x.name,provider:e,onChange:e=>m(x.name,e),onRename:e=>b(x.name,e),onDelete:()=>j(x.name)},x.name):null}let e=t.providers?.[x.providerName],r=e?.models?.[x.index];return r?(0,n.jsx)(eL,{model:r,onChange:e=>S(x.providerName,x.index,e),onDelete:()=>w(x.providerName,x.index)},`${x.providerName}-${x.index}`):null})();return(0,n.jsx)("div",{style:{position:"fixed",inset:0,zIndex:1e3,background:"rgba(0,0,0,0.35)",display:"flex",alignItems:"center",justifyContent:"center"},onClick:t=>{t.target===t.currentTarget&&e()},children:(0,n.jsxs)("div",{style:{width:860,height:"78vh",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:10,display:"flex",flexDirection:"column",boxShadow:"0 8px 32px rgba(0,0,0,0.18)",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",padding:"12px 18px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"baseline",gap:10},children:[(0,n.jsx)("span",{style:{fontSize:15,fontWeight:700,color:"var(--text)"},children:"Models"}),(0,n.jsx)("code",{style:{fontSize:11,color:"var(--text-muted)",fontFamily:"var(--font-mono)"},children:"~/.pi/agent/models.json"})]}),(0,n.jsx)("button",{onClick:e,style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:20,lineHeight:1,padding:"2px 6px"},children:"\xd7"})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{width:210,borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",flexShrink:0,background:"var(--bg-panel)"},children:[(0,n.jsxs)("div",{style:{flex:1,overflowY:"auto",padding:"8px 6px"},children:[g.length>0&&(0,n.jsxs)("div",{style:{marginBottom:10},children:[(0,n.jsx)("div",{style:{padding:"4px 8px 6px",fontSize:10,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.07em"},children:"Subscriptions"}),g.map(e=>{let t=x?.type==="oauth"&&x.providerId===e.id;return(0,n.jsxs)("div",{onClick:()=>h({type:"oauth",providerId:e.id}),style:{display:"flex",alignItems:"center",gap:6,padding:"7px 8px",borderRadius:5,cursor:"pointer",background:t?"var(--bg-selected)":"none"},onMouseEnter:e=>{t||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{t||(e.currentTarget.style.background="none")},children:[(0,n.jsx)("span",{style:{width:6,height:6,borderRadius:"50%",background:e.loggedIn?"#4ade80":"var(--border)",flexShrink:0}}),(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e.name})]},e.id)}),(0,n.jsx)("div",{style:{margin:"8px 8px 0",borderTop:"1px solid var(--border)"}}),(0,n.jsx)("div",{style:{padding:"6px 8px 2px",fontSize:10,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.07em"},children:"Custom providers"})]}),i?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:12,color:"var(--text-muted)"},children:"Loading…"}):0===L.length?(0,n.jsx)("div",{style:{padding:"4px 8px",fontSize:11,color:"var(--text-dim)"},children:"No providers yet"}):L.map(([e,t])=>{let r=x?.type==="provider"&&x.name===e,o=t.models??[];return(0,n.jsxs)("div",{style:{marginBottom:2},children:[(0,n.jsxs)("div",{onClick:()=>h({type:"provider",name:e}),style:{display:"flex",alignItems:"center",gap:6,padding:"7px 8px",borderRadius:5,cursor:"pointer",background:r?"var(--bg-selected)":"none"},onMouseEnter:e=>{r||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{r||(e.currentTarget.style.background="none")},children:[(0,n.jsxs)("svg",{width:"11",height:"11",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",style:{color:"var(--text-dim)",flexShrink:0},children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]}),(0,n.jsx)("span",{style:{fontSize:12,fontWeight:r?600:400,color:"var(--text)",fontFamily:"var(--font-mono)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e})]}),o.map((t,r)=>{let o=x?.type==="model"&&x.providerName===e&&x.index===r;return(0,n.jsxs)("div",{onClick:()=>h({type:"model",providerName:e,index:r}),style:{display:"flex",alignItems:"center",gap:6,padding:"5px 8px 5px 26px",borderRadius:5,cursor:"pointer",background:o?"var(--bg-selected)":"none"},onMouseEnter:e=>{o||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{o||(e.currentTarget.style.background="none")},children:[(0,n.jsx)("span",{style:{fontSize:11,fontFamily:"var(--font-mono)",color:t.id?"var(--text-muted)":"var(--text-dim)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:t.id||"new model"}),t.reasoning&&(0,n.jsx)("span",{style:{fontSize:9,padding:"1px 4px",background:"rgba(99,102,241,0.12)",color:"rgba(99,102,241,0.8)",borderRadius:3,flexShrink:0},children:"T"})]},r)}),(0,n.jsx)("div",{onClick:t=>{t.stopPropagation(),k(e)},style:{display:"flex",alignItems:"center",gap:4,padding:"4px 8px 4px 26px",borderRadius:5,cursor:"pointer",color:"var(--text-dim)"},onMouseEnter:e=>{e.currentTarget.style.color="var(--accent)",e.currentTarget.style.background="var(--bg-hover)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-dim)",e.currentTarget.style.background="none"},children:(0,n.jsx)("span",{style:{fontSize:11},children:"+ model"})})]},e)})]}),(0,n.jsx)("div",{style:{borderTop:"1px solid var(--border)",padding:"8px 6px"},children:(0,n.jsx)("button",{onClick:y,style:{display:"flex",alignItems:"center",justifyContent:"center",gap:5,width:"100%",padding:"6px 0",background:"none",border:"1px dashed var(--border)",borderRadius:5,color:"var(--text-muted)",cursor:"pointer",fontSize:12},onMouseEnter:e=>{e.currentTarget.style.borderColor="var(--accent)",e.currentTarget.style.color="var(--accent)"},onMouseLeave:e=>{e.currentTarget.style.borderColor="var(--border)",e.currentTarget.style.color="var(--text-muted)"},children:"+ Add provider"})})]}),(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:20},children:i?null:z??(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-dim)",fontSize:13},children:"Select a provider or model"})})]}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",gap:10,padding:"10px 18px",borderTop:"1px solid var(--border)",flexShrink:0},children:[d&&(0,n.jsx)("span",{style:{fontSize:12,color:"#f87171",flex:1},children:d}),p&&(0,n.jsx)("span",{style:{fontSize:12,color:"#4ade80",flex:1},children:"Saved"}),(0,n.jsx)("button",{onClick:e,style:{padding:"6px 14px",background:"none",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:13},children:"Cancel"}),(0,n.jsx)("button",{onClick:C,disabled:s,style:{padding:"6px 16px",background:s?"var(--bg-panel)":"var(--accent)",border:"none",borderRadius:6,color:s?"var(--text-muted)":"#fff",cursor:s?"not-allowed":"pointer",fontSize:13,fontWeight:600},children:s?"Saving…":"Save"})]})]})})}function eI(e){return e.replace(/^\/(?:Users|home)\/[^/]+/,"~")}function eR(e){let t=e.sourceInfo?.source,r=e.sourceInfo?.scope;return"user"===r||"user"===t?"global":"project"===r||"project"===t?"project":"path"}function eM({enabled:e,loading:t,onToggle:r}){return(0,n.jsx)("button",{onClick:r,disabled:t,title:e?"Visible in model prompt — click to disable":"Hidden from model prompt — click to enable",style:{flexShrink:0,width:40,height:22,borderRadius:11,border:"none",padding:0,cursor:t?"wait":"pointer",background:e?"var(--accent)":"var(--border)",position:"relative",transition:"background 0.18s",outline:"none"},children:(0,n.jsx)("span",{style:{position:"absolute",top:3,left:e?21:3,width:16,height:16,borderRadius:"50%",background:"#fff",boxShadow:"0 1px 4px rgba(0,0,0,0.22)",transition:"left 0.18s cubic-bezier(.4,0,.2,1)"}})})}function eW({skill:e,cwd:t,onToggle:r,toggling:o,saveError:i}){let l=eR(e),s=!e.disableModelInvocation;return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:20},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:7},children:[(0,n.jsx)("span",{style:{fontSize:10,padding:"1px 5px",borderRadius:3,flexShrink:0,background:"project"===l?"rgba(99,102,241,0.12)":"rgba(120,120,120,0.12)",color:"project"===l?"rgba(99,102,241,0.8)":"var(--text-dim)"},children:l}),(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:11,color:"var(--text-dim)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:function(e){if("project"===l&&e.startsWith(t)){let r=e.slice(t.length).replace(/^\//,"");return`./${r}`}return eI(e)}(e.filePath)}),(0,n.jsx)(eM,{enabled:s,loading:o,onToggle:()=>r(e)}),i&&(0,n.jsx)("span",{style:{fontSize:12,color:"#f87171",flexShrink:0},children:i})]}),(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:5},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:"Name"}),(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:14,color:"var(--text)"},children:e.name})]}),(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:5},children:[(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:"Description"}),(0,n.jsx)("span",{style:{fontSize:14,color:"var(--text-muted)",lineHeight:1.6},children:e.description})]})]})}function eE({cwd:e,onInstalled:t}){let[r,i]=(0,o.useState)(""),[l,s]=(0,o.useState)([]),[a,d]=(0,o.useState)(!1),[c,p]=(0,o.useState)(null),[u,x]=(0,o.useState)(null),[h,g]=(0,o.useState)(null),[f,v]=(0,o.useState)(new Set),[y,m]=(0,o.useState)("global"),b=(0,o.useRef)(null);(0,o.useEffect)(()=>{b.current?.focus()},[]);let j=(0,o.useCallback)(async e=>{if(e.trim()){d(!0),p(null),s([]);try{let t=await fetch("/api/skills/search",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({query:e.trim()})}),r=await t.json();if(r.error)return void p(r.error);s(r.results??[]),0===(r.results??[]).length&&p("No skills found")}catch(e){p(String(e))}finally{d(!1)}}},[]),k=(0,o.useCallback)(async r=>{x(r),g(null);try{let n=await fetch("/api/skills/install",{method:"POST",headers:{"Content-Type":"application/json"},body:JSON.stringify({package:r,scope:y,cwd:e})}),o=await n.json();if(!n.ok||o.error)return void g(o.error??`HTTP ${n.status}`);v(e=>new Set(e).add(r)),t()}catch(e){g(String(e))}finally{x(null)}},[t,y,e]),S="global"===y?"~/.agents/skills/":`${eI(e)}/.agents/skills/`;return(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",height:"100%"},children:[(0,n.jsxs)("div",{style:{display:"flex",flexDirection:"column",gap:12,marginBottom:20},children:[(0,n.jsx)("div",{style:{fontSize:14,fontWeight:600,color:"var(--text)"},children:"Add Skill"}),(0,n.jsxs)("div",{style:{display:"flex",gap:8},children:[(0,n.jsx)("input",{ref:b,value:r,onChange:e=>i(e.target.value),onKeyDown:e=>{"Enter"===e.key&&j(r)},placeholder:"e.g. react, testing, deploy",style:{flex:1,padding:"7px 10px",fontSize:13,background:"var(--bg-panel)",border:"1px solid var(--border)",borderRadius:6,color:"var(--text)",outline:"none"}}),(0,n.jsx)("button",{onClick:()=>j(r),disabled:a||!r.trim(),style:{padding:"7px 16px",fontSize:13,borderRadius:6,border:"none",background:"var(--accent)",color:"#fff",cursor:a||!r.trim()?"not-allowed":"pointer",opacity:a||!r.trim()?.5:1,flexShrink:0},children:a?"Searching…":"Search"})]}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10},children:[(0,n.jsx)("div",{style:{display:"flex",borderRadius:5,border:"1px solid var(--border)",overflow:"hidden",fontSize:12,flexShrink:0},children:["global","project"].map(e=>(0,n.jsx)("button",{onClick:()=>m(e),style:{padding:"3px 10px",border:"none",cursor:"pointer",background:y===e?"var(--bg-selected)":"none",color:y===e?"var(--text)":"var(--text-dim)",fontWeight:y===e?600:400,borderRight:"global"===e?"1px solid var(--border)":"none"},children:e},e))}),(0,n.jsxs)("span",{style:{fontSize:12,color:"var(--text-dim)",fontFamily:"var(--font-mono)",overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:["→ ",S]})]}),c&&(0,n.jsx)("div",{style:{fontSize:12,color:"#f87171"},children:c}),h&&(0,n.jsx)("div",{style:{fontSize:12,color:"#f87171",wordBreak:"break-word"},children:h})]}),l.length>0?(0,n.jsx)("div",{style:{flex:1,overflowY:"auto"},children:l.map(e=>{let t=f.has(e.package),r=u===e.package,o=e.package.indexOf("@"),i=o>-1?e.package.slice(0,o):e.package,l=o>-1?e.package.slice(o+1):null;return(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:14,padding:"12px 0",borderBottom:"1px solid var(--border)"},children:[(0,n.jsxs)("div",{style:{flex:1,minWidth:0},children:[(0,n.jsx)("div",{style:{fontSize:13,fontWeight:600,color:"var(--text)",marginBottom:3},children:l??i}),(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",gap:10,flexWrap:"wrap"},children:[(0,n.jsx)("span",{style:{fontFamily:"var(--font-mono)",fontSize:11,color:"var(--text-dim)"},children:i}),(0,n.jsx)("span",{style:{fontSize:12,color:"var(--text-muted)",fontWeight:500},children:e.installs}),e.url&&(0,n.jsx)("a",{href:e.url,target:"_blank",rel:"noreferrer",style:{fontSize:12,color:"var(--accent)",textDecoration:"none"},children:"skills.sh ↗"})]})]}),(0,n.jsx)("button",{onClick:()=>!t&&!r&&k(e.package),disabled:t||r||null!==u,style:{flexShrink:0,padding:"5px 14px",fontSize:12,fontWeight:500,borderRadius:5,border:"1px solid var(--border)",cursor:t||r||null!==u?"not-allowed":"pointer",background:t?"rgba(34,197,94,0.1)":"none",color:t?"#16a34a":r?"var(--accent)":"var(--text-muted)",transition:"color 0.12s"},children:t?"✓ Installed":r?"Installing…":"Install"})]},e.package)})}):!c&&!a&&(0,n.jsxs)("div",{style:{fontSize:13,color:"var(--text-dim)",lineHeight:1.8},children:["Search ",(0,n.jsx)("a",{href:"https://skills.sh",target:"_blank",rel:"noreferrer",style:{color:"var(--accent)",textDecoration:"none"},children:"skills.sh"})," to discover and install skills for your agent."]})]})}function eB({cwd:e,onClose:t}){let[r,i]=(0,o.useState)([]),[l,s]=(0,o.useState)(!0),[a,d]=(0,o.useState)(null),[c,p]=(0,o.useState)(null),[u,x]=(0,o.useState)(new Set),[h,g]=(0,o.useState)(null),[f,v]=(0,o.useState)(!1),y=(0,o.useCallback)(()=>{s(!0),d(null),fetch(`/api/skills?cwd=${encodeURIComponent(e)}`).then(e=>e.json()).then(e=>{if(e.error)return void d(e.error);let t=e.skills??[];i(t),t.length>0&&!c&&p(t[0].filePath)}).catch(e=>d(String(e))).finally(()=>s(!1))},[e,c]);(0,o.useEffect)(()=>{y()},[e]);let m=(0,o.useCallback)(async e=>{let t=!e.disableModelInvocation;x(t=>new Set(t).add(e.filePath)),g(null);try{let r=await fetch("/api/skills",{method:"PATCH",headers:{"Content-Type":"application/json"},body:JSON.stringify({filePath:e.filePath,disableModelInvocation:t})}),n=await r.json();if(!r.ok||n.error)return void g(n.error??`HTTP ${r.status}`);i(r=>r.map(r=>r.filePath===e.filePath?{...r,disableModelInvocation:t}:r))}catch(e){g(String(e))}finally{x(t=>{let r=new Set(t);return r.delete(e.filePath),r})}},[]),b=r.find(e=>e.filePath===c)??null;return(0,n.jsx)("div",{style:{position:"fixed",inset:0,zIndex:1e3,background:"rgba(0,0,0,0.35)",display:"flex",alignItems:"center",justifyContent:"center"},onClick:e=>{e.target===e.currentTarget&&t()},children:(0,n.jsxs)("div",{style:{width:860,height:"78vh",background:"var(--bg)",border:"1px solid var(--border)",borderRadius:10,display:"flex",flexDirection:"column",boxShadow:"0 8px 32px rgba(0,0,0,0.18)",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",justifyContent:"space-between",padding:"12px 18px",borderBottom:"1px solid var(--border)",flexShrink:0},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"baseline",gap:10},children:[(0,n.jsx)("span",{style:{fontSize:15,fontWeight:700,color:"var(--text)"},children:"Skills"}),(0,n.jsx)("code",{style:{fontSize:11,color:"var(--text-muted)",fontFamily:"var(--font-mono)",maxWidth:320,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:eI(e)})]}),(0,n.jsx)("button",{onClick:t,style:{background:"none",border:"none",color:"var(--text-muted)",cursor:"pointer",fontSize:20,lineHeight:1,padding:"2px 6px"},children:"\xd7"})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{width:210,borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",flexShrink:0,background:"var(--bg-panel)"},children:[(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:"8px 6px"},children:l?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:12,color:"var(--text-muted)"},children:"Loading…"}):a?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:11,color:"#f87171"},children:a}):0===r.length?(0,n.jsx)("div",{style:{padding:"10px 8px",fontSize:11,color:"var(--text-dim)"},children:"No skills found"}):(()=>{let e=[];for(let t of["project","global","path"]){let n=r.filter(e=>eR(e)===t);n.length>0&&e.push({label:t,skills:n})}return e.map(({label:e,skills:t})=>(0,n.jsxs)("div",{style:{marginBottom:6},children:[(0,n.jsx)("div",{style:{padding:"4px 8px 3px",fontSize:10,fontWeight:600,color:"var(--text-dim)",textTransform:"uppercase",letterSpacing:"0.06em"},children:e}),t.map(e=>{let t=!f&&c===e.filePath,r=e.disableModelInvocation;return(0,n.jsxs)("div",{onClick:()=>{p(e.filePath),v(!1)},style:{display:"flex",alignItems:"center",gap:7,padding:"8px 8px",borderRadius:5,cursor:"pointer",background:t?"var(--bg-selected)":"none"},onMouseEnter:e=>{t||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{t||(e.currentTarget.style.background="none")},children:[(0,n.jsx)("span",{style:{flexShrink:0,width:7,height:7,borderRadius:"50%",background:r?"var(--border)":"var(--accent)",boxShadow:r?"none":"0 0 4px var(--accent)",transition:"background 0.15s, box-shadow 0.15s"}}),(0,n.jsx)("span",{style:{fontSize:12,fontWeight:t?600:400,color:r?"var(--text-dim)":"var(--text)",fontFamily:"var(--font-mono)",flex:1,overflow:"hidden",textOverflow:"ellipsis",whiteSpace:"nowrap"},children:e.name})]},e.filePath)})]},e))})()}),(0,n.jsx)("div",{style:{padding:"8px 6px",borderTop:"1px solid var(--border)",flexShrink:0},children:(0,n.jsxs)("div",{onClick:()=>v(!0),style:{display:"flex",alignItems:"center",gap:6,padding:"7px 8px",borderRadius:5,cursor:"pointer",background:f?"var(--bg-selected)":"none",color:f?"var(--accent)":"var(--text-dim)",fontSize:12},onMouseEnter:e=>{f||(e.currentTarget.style.background="var(--bg-hover)")},onMouseLeave:e=>{f||(e.currentTarget.style.background="none")},children:[(0,n.jsxs)("svg",{width:"13",height:"13",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("line",{x1:"12",y1:"5",x2:"12",y2:"19"}),(0,n.jsx)("line",{x1:"5",y1:"12",x2:"19",y2:"12"})]}),"Add skill"]})})]}),(0,n.jsx)("div",{style:{flex:1,overflowY:"auto",padding:20},children:f?(0,n.jsx)(eE,{cwd:e,onInstalled:()=>{y()}}):l?null:b?(0,n.jsx)(eW,{skill:b,cwd:e,onToggle:m,toggling:u.has(b.filePath),saveError:h},b.filePath):(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-dim)",fontSize:13},children:"Select a skill"})})]}),(0,n.jsx)("div",{style:{display:"flex",alignItems:"center",justifyContent:"flex-end",padding:"10px 18px",borderTop:"1px solid var(--border)",flexShrink:0},children:(0,n.jsx)("button",{onClick:t,style:{padding:"6px 14px",background:"none",border:"1px solid var(--border)",borderRadius:6,color:"var(--text-muted)",cursor:"pointer",fontSize:13},children:"Close"})})]})})}let e$={id:"chat",type:"chat",label:"Chat"};function eO(){let e=(0,i.useRouter)(),t=(0,i.useSearchParams)(),[r,l]=(0,o.useState)(null),[s,a]=(0,o.useState)(null),[d,c]=(0,o.useState)(0),[p,u]=(0,o.useState)(0),[x,h]=(0,o.useState)(0),[g,f]=(0,o.useState)(!1),[v,y]=(0,o.useState)(!1),[m,b]=(0,o.useState)(!0),[j]=(0,o.useState)(()=>t.get("session")),[k,S]=(0,o.useState)(null),[w,C]=(0,o.useState)(()=>!t.get("session")),[L,z]=(0,o.useState)([e$]),[T,I]=(0,o.useState)("chat"),R=(0,o.useCallback)(e=>{S(e),l(e=>(null===e&&u(e=>e+1),e))},[]),M=(0,o.useCallback)((t,r=!1)=>{a(null),l(t),u(e=>e+1),C(!0),r||e.replace(`?session=${encodeURIComponent(t.id)}`,{scroll:!1})},[e]),W=(0,o.useCallback)((t,r)=>{l(null),a(r),u(e=>e+1),e.replace("/",{scroll:!1})},[e]),E=(0,o.useCallback)(t=>{a(null),l(t),c(e=>e+1),e.replace(`?session=${encodeURIComponent(t.id)}`,{scroll:!1})},[e]),B=(0,o.useCallback)(()=>{c(e=>e+1),h(e=>e+1)},[]),$=(0,o.useCallback)(t=>{c(e=>e+1),u(e=>e+1),a(null),l(e=>({...e??{path:"",cwd:"",created:"",modified:"",messageCount:0,firstMessage:""},id:t})),e.replace(`?session=${encodeURIComponent(t)}`,{scroll:!1})},[e]),O=(0,o.useCallback)(()=>{C(!0)},[]),P=(0,o.useCallback)(t=>{c(e=>e+1),r?.id===t&&(l(null),a(null),e.replace("/",{scroll:!1}))},[r,e]),D=(0,o.useCallback)((e,t)=>{let r=`file:${e}`;z(n=>n.find(e=>e.id===r)?n:[...n,{id:r,type:"file",label:t,filePath:e}]),I(r)},[]),F=(0,o.useCallback)(e=>{z(t=>t.filter(t=>t.id!==e)),I(t=>t===e?"chat":t)},[]),N=s??(null===r&&k?k:null),A=null!==r||null!==N,U=L.find(e=>e.id===T)??e$,_=(0,n.jsxs)(n.Fragment,{children:[(0,n.jsx)(H,{selectedSessionId:r?.id??null,onSelectSession:M,onNewSession:W,initialSessionId:j,onInitialRestoreDone:O,refreshKey:d,onSessionDeleted:P,selectedCwd:r?.cwd??s??null,onCwdChange:R,onOpenFile:D,explorerRefreshKey:x}),(0,n.jsx)("div",{style:{padding:"6px 10px",flexShrink:0,display:"flex",justifyContent:"space-between"},children:[{label:"Models",onClick:()=>f(!0),disabled:!1,icon:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"4",y:"4",width:"16",height:"16",rx:"2"}),(0,n.jsx)("rect",{x:"9",y:"9",width:"6",height:"6"}),(0,n.jsx)("line",{x1:"9",y1:"1",x2:"9",y2:"4"}),(0,n.jsx)("line",{x1:"15",y1:"1",x2:"15",y2:"4"}),(0,n.jsx)("line",{x1:"9",y1:"20",x2:"9",y2:"23"}),(0,n.jsx)("line",{x1:"15",y1:"20",x2:"15",y2:"23"}),(0,n.jsx)("line",{x1:"20",y1:"9",x2:"23",y2:"9"}),(0,n.jsx)("line",{x1:"20",y1:"14",x2:"23",y2:"14"}),(0,n.jsx)("line",{x1:"1",y1:"9",x2:"4",y2:"9"}),(0,n.jsx)("line",{x1:"1",y1:"14",x2:"4",y2:"14"})]})},{label:"Skills",onClick:()=>y(!0),disabled:!k&&!r?.cwd&&!s,icon:(0,n.jsxs)("svg",{width:"14",height:"14",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("path",{d:"M12 2L2 7l10 5 10-5-10-5z"}),(0,n.jsx)("path",{d:"M2 17l10 5 10-5"}),(0,n.jsx)("path",{d:"M2 12l10 5 10-5"})]})}].map(({label:e,onClick:t,disabled:r,icon:o})=>(0,n.jsxs)("button",{onClick:t,disabled:r,title:e,style:{flex:1,display:"flex",alignItems:"center",justifyContent:"center",gap:5,padding:"7px 0",background:"none",border:"none",borderRadius:5,color:"var(--text-muted)",cursor:r?"default":"pointer",fontSize:11,opacity:r?.35:1},onMouseEnter:e=>{r||(e.currentTarget.style.background="var(--bg-hover)",e.currentTarget.style.color="var(--text)")},onMouseLeave:e=>{e.currentTarget.style.background="none",e.currentTarget.style.color="var(--text-muted)"},children:[o,e]},e))})]});return(0,n.jsxs)(n.Fragment,{children:[(0,n.jsxs)("div",{style:{display:"flex",height:"100vh",overflow:"hidden",background:"var(--bg)"},children:[(0,n.jsx)("div",{className:"sidebar-inline",style:{width:260*!!m,minWidth:260*!!m,borderRight:m?"1px solid var(--border)":"none",background:"var(--bg-panel)",display:"flex",flexDirection:"column",overflow:"hidden",transition:"width 0.2s ease, min-width 0.2s ease"},children:(0,n.jsx)("div",{style:{width:260,minWidth:260,display:"flex",flexDirection:"column",height:"100%"},children:_})}),(0,n.jsxs)("div",{className:"sidebar-overlay",style:{position:"fixed",inset:0,zIndex:200,display:"none",pointerEvents:m?"auto":"none"},children:[(0,n.jsx)("div",{onClick:()=>b(!1),style:{position:"absolute",inset:0,background:"rgba(0,0,0,0.4)",opacity:+!!m,transition:"opacity 0.25s ease"}}),(0,n.jsx)("div",{style:{position:"absolute",top:0,left:0,bottom:0,width:280,maxWidth:"85vw",background:"var(--bg-panel)",borderRight:"1px solid var(--border)",display:"flex",flexDirection:"column",boxShadow:m?"4px 0 20px rgba(0,0,0,0.15)":"none",transform:m?"translateX(0)":"translateX(-100%)",transition:"transform 0.25s ease"},children:_})]}),(0,n.jsxs)("div",{style:{flex:1,display:"flex",flexDirection:"column",overflow:"hidden"},children:[(0,n.jsxs)("div",{style:{display:"flex",alignItems:"center",flexShrink:0},children:[(0,n.jsx)("button",{onClick:()=>b(e=>!e),title:m?"Hide sidebar":"Show sidebar",style:{display:"flex",alignItems:"center",justifyContent:"center",width:36,height:36,padding:0,background:"var(--bg-panel)",border:"none",borderRight:"1px solid var(--border)",color:"var(--text-muted)",cursor:"pointer",flexShrink:0,transition:"color 0.12s"},onMouseEnter:e=>{e.currentTarget.style.color="var(--text)"},onMouseLeave:e=>{e.currentTarget.style.color="var(--text-muted)"},children:m?(0,n.jsxs)("svg",{width:"16",height:"16",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",strokeLinejoin:"round",children:[(0,n.jsx)("rect",{x:"3",y:"3",width:"18",height:"18",rx:"2"}),(0,n.jsx)("line",{x1:"9",y1:"3",x2:"9",y2:"21"})]}):(0,n.jsxs)("svg",{width:"18",height:"18",viewBox:"0 0 24 24",fill:"none",stroke:"currentColor",strokeWidth:"2",strokeLinecap:"round",children:[(0,n.jsx)("line",{x1:"3",y1:"6",x2:"21",y2:"6"}),(0,n.jsx)("line",{x1:"3",y1:"12",x2:"21",y2:"12"}),(0,n.jsx)("line",{x1:"3",y1:"18",x2:"21",y2:"18"})]})}),(0,n.jsx)("div",{style:{flex:1,overflow:"hidden"},children:(0,n.jsx)(ef,{tabs:L,activeTabId:T,onSelectTab:I,onCloseTab:F})})]}),(0,n.jsxs)("div",{style:{flex:1,overflow:"hidden",position:"relative"},children:[(0,n.jsx)("div",{style:{position:"absolute",inset:0,display:"chat"===U.type?"flex":"none",flexDirection:"column"},children:A?(0,n.jsx)(ex,{session:r,newSessionCwd:N,onAgentEnd:B,onSessionCreated:E,onSessionForked:$},p):w&&!A?(0,n.jsx)("div",{style:{height:"100%",display:"flex",alignItems:"center",justifyContent:"center",color:"var(--text-muted)",fontSize:15},children:"Select a session from the sidebar"}):null}),"file"===U.type&&U.filePath&&(0,n.jsx)("div",{style:{position:"absolute",inset:0},children:(0,n.jsx)(eh,{filePath:U.filePath})})]})]})]}),g&&(0,n.jsx)(eT,{onClose:()=>f(!1)}),v&&(k??r?.cwd??s)&&(0,n.jsx)(eB,{cwd:k??r?.cwd??s,onClose:()=>y(!1)})]})}}},e=>{e.O(0,[110,441,794,358],()=>e(e.s=5597)),_N_E=e.O()}]);