pi-web-ui 0.114.2 → 0.115.0

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 (195) hide show
  1. package/CHANGELOG.md +55 -1
  2. package/bin/pi-web-ui.mjs +69 -7
  3. package/dist/server/agent-service-daemon.js +440 -32
  4. package/dist/server/agent-service.js +878 -383
  5. package/dist/server/canvas/canvas-dispatch.js +13 -1
  6. package/dist/server/client-state.js +25 -0
  7. package/dist/server/communicate-tool.js +2 -0
  8. package/dist/server/context-usage.js +105 -0
  9. package/dist/server/conversation-communication.js +14 -0
  10. package/dist/server/conversation-identity.js +27 -0
  11. package/dist/server/device-center/approval-summary.js +31 -0
  12. package/dist/server/device-center/bot-catalog.js +21 -7
  13. package/dist/server/device-center/bot-execution.js +72 -9
  14. package/dist/server/device-center/bot-presets.js +46 -0
  15. package/dist/server/device-center/collab-service.js +328 -10
  16. package/dist/server/device-center/collab-tool.js +738 -12
  17. package/dist/server/device-center/connector.js +359 -23
  18. package/dist/server/device-center/desktop-page.js +22 -16
  19. package/dist/server/device-center/device-integration.js +595 -0
  20. package/dist/server/device-center/execution-guides.js +32 -0
  21. package/dist/server/device-center/execution-instructions.js +19 -0
  22. package/dist/server/device-center/execution-settings.js +100 -0
  23. package/dist/server/device-center/execution-system-prompt.js +38 -0
  24. package/dist/server/device-center/execution-workspace.js +20 -0
  25. package/dist/server/device-center/executor.js +389 -40
  26. package/dist/server/device-center/file-sync-paths.js +171 -0
  27. package/dist/server/device-center/file-sync.js +180 -91
  28. package/dist/server/device-center/group-context-delta.js +37 -0
  29. package/dist/server/device-center/group-context-message.js +208 -0
  30. package/dist/server/device-center/group-context.js +109 -0
  31. package/dist/server/device-center/group-events.js +136 -0
  32. package/dist/server/device-center/group-plan-guard.js +138 -0
  33. package/dist/server/device-center/hub-conversations.js +178 -0
  34. package/dist/server/device-center/hub-deliveries.js +277 -0
  35. package/dist/server/device-center/hub-integration.js +451 -0
  36. package/dist/server/device-center/hub.js +1544 -80
  37. package/dist/server/device-center/human-interaction.js +232 -0
  38. package/dist/server/device-center/integration-errors.js +25 -0
  39. package/dist/server/device-center/local-hub.js +18 -0
  40. package/dist/server/device-center/local.js +356 -40
  41. package/dist/server/device-center/main.js +11 -1
  42. package/dist/server/device-center/path-portal.js +11 -3
  43. package/dist/server/device-center/relay.js +364 -35
  44. package/dist/server/device-center/report-metadata.js +66 -0
  45. package/dist/server/device-center/report-upload.js +63 -0
  46. package/dist/server/device-center/runtime.js +549 -41
  47. package/dist/server/device-center/store.js +66 -3
  48. package/dist/server/device-center/task-context-budget.js +161 -0
  49. package/dist/server/device-center/task-journal.js +385 -0
  50. package/dist/server/device-center/task-plan.js +33 -0
  51. package/dist/server/device-center/task-progress.js +141 -0
  52. package/dist/server/device-center/task-recovery.js +452 -0
  53. package/dist/server/device-center/task-report-outbox.js +216 -0
  54. package/dist/server/device-center/task-report.js +384 -0
  55. package/dist/server/device-center/task-scheduler.js +400 -0
  56. package/dist/server/device-center/task-state.js +333 -0
  57. package/dist/server/device-center/transfer-stream.js +1006 -0
  58. package/dist/server/device-center/transfers.js +815 -8
  59. package/dist/server/dispatch-domain-handlers.js +21 -3
  60. package/dist/server/dsh/dsh-agent-service.js +6 -0
  61. package/dist/server/goal-service.js +10 -5
  62. package/dist/server/index.js +89 -13
  63. package/dist/server/message-deletion.js +40 -0
  64. package/dist/server/message-window.js +9 -0
  65. package/dist/server/patch-executable-lookup.js +396 -0
  66. package/dist/server/payload-limits.js +2 -0
  67. package/dist/server/plugin-llm.js +100 -9
  68. package/dist/server/plugins.js +1 -0
  69. package/dist/server/preset-fields.js +24 -0
  70. package/dist/server/preset-share.js +2 -0
  71. package/dist/server/prompt-base-store.js +85 -0
  72. package/dist/server/prompt-composer.js +5 -2
  73. package/dist/server/protocol-version.js +1 -1
  74. package/dist/server/protocol.js +2 -2
  75. package/dist/server/remote-ssh-service.js +22 -9
  76. package/dist/server/resolve-global-sdk.js +2 -0
  77. package/dist/server/session-search.js +1 -1
  78. package/dist/server/settings-service.js +90 -39
  79. package/dist/server/skill-content-filler.js +29 -8
  80. package/dist/server/skill-tool.js +12 -3
  81. package/dist/server/subagent-helpers.js +4 -1
  82. package/dist/server/subagents.js +50 -36
  83. package/dist/server/tool-host-factories.js +1 -0
  84. package/dist/server/universal/universal-session.js +5 -0
  85. package/dist/server/webui-context.js +25 -5
  86. package/docs/README.md +94 -0
  87. package/docs/device-center.md +100 -12
  88. package/docs/dsh-engine.md +10 -10
  89. package/extensions/webui.ts +43 -1
  90. package/package.json +1 -1
  91. package/themes/aetheris.css +5 -0
  92. package/themes/ayu-light.css +6 -0
  93. package/themes/bases/flow-base.css +68 -68
  94. package/themes/catppuccin-latte.css +6 -0
  95. package/themes/catppuccin.css +6 -0
  96. package/themes/claude-code-dark.css +5 -0
  97. package/themes/codex.css +6 -0
  98. package/themes/cyberpunk.css +6 -0
  99. package/themes/dark-teal.css +6 -1
  100. package/themes/dazzle.css +6 -0
  101. package/themes/everforest-light.css +6 -0
  102. package/themes/flow-light.css +8 -1
  103. package/themes/flow.css +6 -1
  104. package/themes/geist.css +6 -0
  105. package/themes/gruvbox-light.css +6 -0
  106. package/themes/kanagawa-lotus.css +6 -0
  107. package/themes/md-preview.css +6 -0
  108. package/themes/mist.css +6 -0
  109. package/themes/nord.css +6 -0
  110. package/themes/one-dark.css +6 -0
  111. package/themes/paper.css +6 -0
  112. package/themes/rose-pine-dawn.css +6 -0
  113. package/themes/sakura.css +6 -0
  114. package/themes/solarized-light.css +6 -0
  115. package/themes/tokyo-night.css +6 -0
  116. package/themes/translucent.css +6 -0
  117. package/themes/transparent.css +6 -0
  118. package/themes/vs-code-dark.css +5 -0
  119. package/themes/wallpaper-flow.css +5 -1
  120. package/themes/wallpaper-obsidian.css +6 -0
  121. package/themes/white.css +6 -0
  122. package/themes/zhujian-dark.css +6 -1
  123. package/themes/zhujian.css +6 -1
  124. package/themes/zhupi-dark.css +6 -1
  125. package/themes/zhupi.css +6 -1
  126. package/web/dist/.build-assets.json +1 -1
  127. package/web/dist/assets/BgTasksModal-BYzPgmoq.js +1 -0
  128. package/web/dist/assets/BgTasksModal-ni0VTmAQ.js +1 -0
  129. package/web/dist/assets/{CanvasStudio-B3W6b_rA.js → CanvasStudio-BCUeEf25.js} +3 -3
  130. package/web/dist/assets/{CanvasStudio-Csv1Sj1-.js → CanvasStudio-DNmDNBod.js} +3 -3
  131. package/web/dist/assets/DeviceCenter-BcBhRgL6.js +3 -0
  132. package/web/dist/assets/DeviceCenter-CmL5FZmt.js +3 -0
  133. package/web/dist/assets/DeviceGroups-CLdpdnI7.js +1 -0
  134. package/web/dist/assets/DeviceGroups-CgBaDPrD.js +1 -0
  135. package/web/dist/assets/Dropdown-CGXD0DfT.js +1 -0
  136. package/web/dist/assets/FilePreview-B8iqnBjy.js +2 -0
  137. package/web/dist/assets/FilePreview-BqviZKCN.js +2 -0
  138. package/web/dist/assets/GlobalSearchModal-mevF-ySi.js +1 -0
  139. package/web/dist/assets/GlobalSearchModal-qL99FN_F.js +1 -0
  140. package/web/dist/assets/GroupConversation-B88Yy-lt.js +3 -0
  141. package/web/dist/assets/GroupConversation-BwP5NVOE.js +3 -0
  142. package/web/dist/assets/HintTip-LHwkS2jV.js +1 -0
  143. package/web/dist/assets/ModelConfigModal-Do3Fe3jH.js +2 -0
  144. package/web/dist/assets/PiSetupModal-DE5-gEtI.js +1 -0
  145. package/web/dist/assets/ProviderOAuthControls-6UMARucA.js +1 -0
  146. package/web/dist/assets/SCMPanel-Hi6QqC1Q.js +2 -0
  147. package/web/dist/assets/SCMPanel-ezY_ZtCu.js +2 -0
  148. package/web/dist/assets/SettingsModal-6HgeEsJ8.js +20 -0
  149. package/web/dist/assets/SettingsModal-CF-7MwqS.js +20 -0
  150. package/web/dist/assets/TerminalPanel-CTp_BZ4S.js +3 -0
  151. package/web/dist/assets/TerminalPanel-CkYxC-Gh.js +3 -0
  152. package/web/dist/assets/TuiOverlayModal-1cRLP8IF.js +1 -0
  153. package/web/dist/assets/TuiOverlayModal-2xlptqWX.js +1 -0
  154. package/web/dist/assets/app-globals-DZNa9v8u.js +325 -0
  155. package/web/dist/assets/canvas-client-Bye9nSFO.js +1 -0
  156. package/web/dist/assets/{desktop-YguLbrX1.js → desktop-kjBoE3Nu.js} +1 -1
  157. package/web/dist/assets/group-view-state-CGCzYF8k.js +1 -0
  158. package/web/dist/assets/index-CsFs_HMb.css +1 -0
  159. package/web/dist/assets/index-DHeeHS_l.js +85 -0
  160. package/web/dist/assets/index-DTZch8uz.js +85 -0
  161. package/web/dist/assets/plugin-host-C8mNgghx.js +1 -0
  162. package/web/dist/assets/remote-ssh-client-CeP3CLUW.js +1 -0
  163. package/web/dist/assets/theme-BXGpyTMh.js +1 -0
  164. package/web/dist/index.html +10 -9
  165. package/web/dist/sw.js +1 -1
  166. package/web/dist/assets/BgTasksModal-BIlQ89vr.js +0 -1
  167. package/web/dist/assets/BgTasksModal-Ck2sjlPM.js +0 -1
  168. package/web/dist/assets/DeviceCenter-BQdDcO8A.js +0 -3
  169. package/web/dist/assets/DeviceCenter-CHRKL0Vy.js +0 -3
  170. package/web/dist/assets/DeviceGroups-D2rogxEN.js +0 -1
  171. package/web/dist/assets/Dropdown-ZLS04O8p.js +0 -1
  172. package/web/dist/assets/FilePreview-B9xoU6ON.js +0 -2
  173. package/web/dist/assets/FilePreview-DUQpVZkH.js +0 -2
  174. package/web/dist/assets/GlobalSearchModal-BXEGvUZq.js +0 -1
  175. package/web/dist/assets/GlobalSearchModal-CnVPmfOI.js +0 -1
  176. package/web/dist/assets/GroupConversation-CyIpQzDd.js +0 -1
  177. package/web/dist/assets/GroupConversation-DgYV3uYS.js +0 -1
  178. package/web/dist/assets/ModelConfigModal-DDZccDYt.js +0 -2
  179. package/web/dist/assets/PiSetupModal-DHbqfx8T.js +0 -1
  180. package/web/dist/assets/ProviderOAuthControls-C-MK36Dk.js +0 -1
  181. package/web/dist/assets/SCMPanel-imYI2Ecw.js +0 -2
  182. package/web/dist/assets/SCMPanel-sVZcV7dR.js +0 -2
  183. package/web/dist/assets/SettingsModal-Xw1Yoi3_.js +0 -20
  184. package/web/dist/assets/SettingsModal-mb4e34-0.js +0 -20
  185. package/web/dist/assets/TerminalPanel-CLkJJvxg.js +0 -3
  186. package/web/dist/assets/TerminalPanel-DpcIellm.js +0 -3
  187. package/web/dist/assets/TuiOverlayModal-4-fFAZe7.js +0 -1
  188. package/web/dist/assets/TuiOverlayModal-DCu01lks.js +0 -1
  189. package/web/dist/assets/app-globals-DcyO0UPi.js +0 -1
  190. package/web/dist/assets/fi-DaSO0Q-m.js +0 -325
  191. package/web/dist/assets/index-Cdh3sibd.js +0 -85
  192. package/web/dist/assets/index-DDZMv6LO.css +0 -1
  193. package/web/dist/assets/index-DWw_SxRq.js +0 -85
  194. package/web/dist/assets/plugin-host-BRBg6zmQ.js +0 -1
  195. package/web/dist/assets/theme-DDl-Oy1b.js +0 -1
@@ -0,0 +1,3 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{At as t,B as n,Ft as r,Kt as i,L as a,Lt as o,O as s,Sn as c,Tt as l,Z as u,_n as ee,dt as d,gn as f,p,sn as m,vt as h,wn as te}from"./app-globals-DZNa9v8u.js";import{i as g,t as _}from"./react-CR5VJ85Q.js";import{Rt as v,T as ne,_t as y}from"./index-DTZch8uz.js";import{DeviceGroups as b}from"./DeviceGroups-CgBaDPrD.js";var x=e(g(),1),S=_();function re({href:e,children:t}){let n=ee(),[r,i]=(0,x.useState)(!1),[a,o]=(0,x.useState)(!1);return(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`a`,{className:`dc-button`,href:e,target:`_blank`,rel:`noopener noreferrer`,onClick:t=>{let n=window.piDesktop;n?.openHub&&(t.preventDefault(),!a&&(o(!0),i(!1),n.openHub(e).then(e=>i(!e),()=>i(!0)).finally(()=>o(!1))))},"aria-busy":a,children:t}),r&&(0,S.jsxs)(`span`,{role:`alert`,children:[n(`deviceHubNetworkError`),` `,(0,S.jsx)(`a`,{href:e,target:`_blank`,rel:`noopener noreferrer`,children:e})]})]})}function C(e){if(!w(e.host)||!Number.isInteger(e.port)||e.port<1024||e.port>65535||e.publicUrl.length>2048||/[\s\u0000-\u001f\u007f]/.test(e.publicUrl))return!1;try{let t=new URL(e.publicUrl);return[`http:`,`https:`].includes(t.protocol)&&!!t.hostname&&!t.username&&!t.password&&!t.search&&!t.hash&&t.pathname===`/`&&![`0.0.0.0`,`[::]`].includes(t.hostname)}catch{return!1}}function w(e){if(e===`localhost`)return!0;if(/^(?:\d{1,3}\.){3}\d{1,3}$/.test(e))return e.split(`.`).every(e=>String(Number(e))===e&&Number(e)<=255);if(!e.includes(`:`)||!/^[0-9a-fA-F:.]+$/.test(e))return!1;try{return new URL(`http://[${e}]/`).hostname.startsWith(`[`)}catch{return!1}}function T(e){try{let t=new URL(e);return[`http:`,`https:`].includes(t.protocol)&&[`localhost`,`127.0.0.1`,`[::1]`].includes(t.hostname)}catch{return!1}}var E=`https://raw.githubusercontent.com/xing-shuyin/pi-web-ui/main/docs/device-center-deployment.md`,D=`https://github.com/xing-shuyin/pi-web-ui/blob/main/docs/device-center-deployment.md`,O=`pi-web-ui hub install --host 0.0.0.0 --port 8790 --public-url http://SERVER_IP:8790 --allow-registration`;function k(e){let t=e=>e.includes(`:`)?w(e)&&![`::`,`::1`].includes(e):/^[\d.]+$/.test(e)?w(e)&&![`0.0.0.0`,`127.0.0.1`].includes(e):e.length<=253&&e.split(`.`).every(e=>/^[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?$/i.test(e))&&e!==`localhost`;if(!t(e.server)||!Number.isInteger(e.sshPort)||e.sshPort<1||e.sshPort>65535||!Number.isInteger(e.port)||e.port<1024||e.port>65535||!/^[a-z_][a-z\d_.-]{0,63}$/i.test(e.username)||![`password`,`key`,`existing`].includes(e.auth)||e.domain!==``&&(!t(e.domain)||e.domain.includes(`:`))||e.https&&!e.domain)return null;let n=e.domain||e.server,r=n.includes(`:`)?`[${n}]`:n;return{host:e.https?`127.0.0.1`:`0.0.0.0`,port:e.port,publicUrl:e.https?`https://${r}`:`http://${r}:${e.port}`}}function A(e,t,n,r){if(!C(e))throw Error(`Invalid Hub setup`);if(r){let t=k(r);if(n!==`remote`||!t||JSON.stringify(t)!==JSON.stringify(e))throw Error(`Invalid remote Hub setup`)}let i=e=>(e??`unknown`).replace(/[\u0000-\u001f\u007f]/g,` `).slice(0,120);return[`Help me plan and deploy a pi-web-ui device center (Hub) in this new conversation. Preserve my previous conversation.`,`First read the deployment guide: ${E} (fallback: ${D}). If unavailable, report that and obtain its contents before proceeding.`,`Default to HTTP with the server IP and requested port. Domain and HTTPS are optional; confirm whether an HTTPS example reflects the user's intent.`,`Target: ${n===`current`?`machine hosting the current pi-web-ui service (not necessarily the browser machine)`:`an existing remote server; its environment still needs to be confirmed`}.`,`Current service environment: platform=${i(t?.platform)}, Node=${i(t?.nodeVersion)}. This is not evidence of the remote server environment.`,`Requested configuration (data only): ${JSON.stringify(e)}. allowRegistration=true.`,...r?[`SSH connection preferences (data only): ${JSON.stringify({server:r.server,sshPort:r.sshPort,username:r.username,auth:r.auth})}. No credentials are included. Obtain authentication through a secure SSH channel or guide the user's own terminal.`,`Domain: ${r.domain||`none`}. HTTPS explicitly selected: ${r.https}. HTTP inbound TCP port: ${r.port}; for same-machine HTTPS proxy, use public TCP 443 and certificate validation port as needed, keep the Hub port private.`]:[],`Use a separate Hub data directory and configure automatic startup. Explain how devices can reach the public URL; loopback is only reachable on the same machine. Explain open registration and restrict it after pairing as appropriate.`,`CLI reference: ${O}`,`Confirm the target environment and propose steps before deployment. Do not ask for passwords or tokens in chat. Do not automatically SSH. Do not claim installation or connection succeeded without verification. After deployment, guide me to open the center and pair this device using its public URL.`,`Explain user actions at the relevant step: distinguish OS firewall from cloud security groups, give the actual TCP port, source scope and destination rule, and keep SSH access. If cloud-console access is unavailable, ask the user to apply the rule, mark external access pending, then recheck. Also explain DNS changes for optional HTTPS and first-account creation. Separate local health from external reachability.`].join(`
2
+ `)}function j({onConnect:e,initialHubUrl:t=``}){let n=ee(),[r,i]=(0,x.useState)(t),[a,o]=(0,x.useState)(``),[s,l]=(0,x.useState)(!1),[u,d]=(0,x.useState)(!1),f=(0,x.useRef)(!1),p=(0,x.useRef)(!1);(0,x.useEffect)(()=>(p.current=!0,()=>{p.current=!1}),[]);let[m,h]=(0,x.useState)(null),[g,_]=(0,x.useState)(`current`),[v,ne]=(0,x.useState)(null),[y,b]=(0,x.useState)(`127.0.0.1`),[w,E]=(0,x.useState)(`8790`),[D,O]=(0,x.useState)(`http://127.0.0.1:8790`),[j,M]=(0,x.useState)({server:``,sshPort:22,username:`root`,auth:`password`,port:8790,domain:``,https:!1}),ie=k(j),[N,P]=(0,x.useState)(!1),[F,I]=(0,x.useState)(!1),[L,ae]=(0,x.useState)(!1),[oe,R]=(0,x.useState)(!1),[z,B]=(0,x.useState)(null),V=(0,x.useRef)(null),H=(0,x.useRef)(!1),U=T(window.location.origin);(0,x.useEffect)(()=>()=>V.current?.abort(),[]);let W=async()=>{if(H.current)return;V.current?.abort();let e=new AbortController;V.current=e,I(!1),ae(!1),R(!1);try{let t=await fetch(c(te(`/api/device-center/setup`)),{credentials:`same-origin`,signal:e.signal});if(!t.ok)throw Error();let n=await t.json();if(!n||typeof n.platform!=`string`||typeof n.nodeVersion!=`string`||typeof n.dataDir!=`string`||typeof n.cliPath!=`string`||!C({host:`127.0.0.1`,port:n.defaultPort,publicUrl:n.defaultPublicUrl}))throw Error();if(e.signal.aborted)return;ne(n),n.installation?.installed?(b(n.installation.host??`127.0.0.1`),E(String(n.installation.port)),O(n.installation.publicUrl??``),i(n.installation.publicUrl??``)):v||(E(String(n.defaultPort)),O(n.defaultPublicUrl))}catch{e.signal.aborted||(I(!0),R(!0))}finally{e.signal.aborted||ae(!0)}},G=async()=>{let e={host:y,port:Number(w),publicUrl:D};if(H.current||!U||!v||!L||oe||v.busy||v.installation?.installed||!C(e))return;H.current=!0,V.current?.abort(),P(!0),I(!1),o(``);let t=new AbortController;V.current=t;try{let n=await fetch(c(te(`/api/device-center/setup`)),{credentials:`same-origin`,method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({...e,allowRegistration:!0}),signal:t.signal});if(n.status===409){ne(e=>e&&{...e,busy:!0}),H.current=!1,P(!1),await W();return}if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(typeof e.error==`string`?e.error.slice(0,4096):`HTTP ${n.status}`)}let r=await n.json();if(!C({...e,publicUrl:r.publicUrl})||typeof r.output!=`string`)throw Error();t.signal.aborted||B(r)}catch(e){t.signal.aborted||(I(!0),o(e instanceof Error?e.message.slice(0,4096):``))}finally{H.current=!1,t.signal.aborted||P(!1)}},K=v?.installation,q=!!K?.installed,J=q&&K.running&&K.owned,Y=g===`remote`?ie:{host:y,port:Number(w),publicUrl:D},X=!!Y&&C(Y);return(0,S.jsxs)(`section`,{className:`dc-onboarding`,children:[(0,S.jsx)(`h3`,{children:n(`deviceSetupWelcome`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupIntro`)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(`button`,{className:`dc-button`,disabled:N,"aria-pressed":m===`connect`,onClick:()=>h(`connect`),children:n(`deviceSetupConnect`)}),(0,S.jsx)(`button`,{className:`dc-button`,disabled:N,"aria-pressed":m===`create`,onClick:()=>{h(`create`),!v&&U&&W()},children:n(`deviceSetupCreate`)})]}),m===`connect`&&(0,S.jsxs)(`form`,{onSubmit:t=>{t.preventDefault(),e(r.trim())},children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{type:`url`,required:!0,maxLength:2048,value:r,onChange:e=>i(e.target.value),placeholder:`https://hub.example.com`})]}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,disabled:!C({host:`localhost`,port:8790,publicUrl:r.trim()}),children:n(`deviceSetupPair`)})]}),m===`create`&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupTarget`)}),(0,S.jsxs)(`select`,{disabled:N,value:g,onChange:e=>_(e.target.value),children:[(0,S.jsx)(`option`,{value:`current`,children:n(`deviceSetupCurrent`)}),(0,S.jsx)(`option`,{value:`remote`,children:n(`deviceSetupRemote`)})]})]}),(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n(g===`current`?`deviceSetupCurrentHint`:`deviceSetupRemoteHint`)}),g===`current`&&(0,S.jsxs)(`div`,{className:`dc-setup-plan`,children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupPlan`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupPlanHint`)}),v&&(0,S.jsxs)(`dl`,{children:[(0,S.jsx)(`dt`,{children:n(`deviceSetupEnvironment`)}),(0,S.jsxs)(`dd`,{children:[v.platform,` · Node `,v.nodeVersion]}),(0,S.jsx)(`dt`,{children:n(`deviceSetupDataDir`)}),(0,S.jsx)(`dd`,{children:v.dataDir}),(0,S.jsx)(`dt`,{children:n(`deviceSetupCli`)}),(0,S.jsx)(`dd`,{children:v.cliPath})]}),!U&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupLocalOnly`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupBackground`)}),q&&(0,S.jsxs)(`div`,{role:`status`,children:[(0,S.jsx)(`h4`,{children:n(J?`deviceSetupRestored`:`deviceSetupUnhealthy`)}),(0,S.jsx)(`p`,{children:K.publicUrl}),J&&X&&K.publicUrl?(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(re,{href:K.publicUrl,children:n(`deviceHubOpenHub`)}),(0,S.jsx)(`button`,{className:`dc-button`,onClick:()=>e(K.publicUrl),children:n(`deviceSetupPair`)})]}):(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n(`deviceSetupRecovery`)}),(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:`pi-web-ui hub status --json
3
+ pi-web-ui hub --help`})]})]}),v?.busy&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupBusy`)}),U&&(0,S.jsx)(`button`,{className:`dc-button`,disabled:N||!L,onClick:()=>void W(),children:n(L?`deviceSetupRetry`:`deviceHubWorking`)})]}),g===`remote`?(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-setup-fields`,children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupServer`)}),(0,S.jsx)(`input`,{autoComplete:`off`,maxLength:253,placeholder:`203.0.113.10`,value:j.server,onChange:e=>M({...j,server:e.target.value.trim()})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupSshPort`)}),(0,S.jsx)(`input`,{type:`number`,min:1,max:65535,value:j.sshPort,onChange:e=>M({...j,sshPort:Number(e.target.value)})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupUsername`)}),(0,S.jsx)(`input`,{autoComplete:`off`,maxLength:64,value:j.username,onChange:e=>M({...j,username:e.target.value})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupAuth`)}),(0,S.jsxs)(`select`,{value:j.auth,onChange:e=>M({...j,auth:e.target.value}),children:[(0,S.jsx)(`option`,{value:`password`,children:n(`deviceSetupAuthPassword`)}),(0,S.jsx)(`option`,{value:`key`,children:n(`deviceSetupAuthKey`)}),(0,S.jsx)(`option`,{value:`existing`,children:n(`deviceSetupAuthExisting`)})]})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupServicePort`)}),(0,S.jsx)(`input`,{type:`number`,min:1024,max:65535,value:j.port,onChange:e=>M({...j,port:Number(e.target.value)})})]})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupCredentialHint`)}),(0,S.jsxs)(`details`,{children:[(0,S.jsx)(`summary`,{children:n(`deviceSetupOptionalTls`)}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupDomain`)}),(0,S.jsx)(`input`,{maxLength:253,placeholder:`hub.example.com`,value:j.domain,onChange:e=>M({...j,domain:e.target.value.trim()})})]}),(0,S.jsxs)(`label`,{children:[(0,S.jsx)(`input`,{type:`checkbox`,checked:j.https,onChange:e=>M({...j,https:e.target.checked})}),` `,n(`deviceSetupEnableHttps`)]}),(0,S.jsx)(`p`,{children:n(`deviceSetupTlsHint`)})]}),(0,S.jsxs)(`p`,{children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupPreview`)}),` `,ie?.publicUrl??n(`deviceSetupPreviewEmpty`)]}),(0,S.jsxs)(`div`,{className:`dc-setup-plan`,children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupUserActions`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupCloudPorts`).replace(`{ports}`,j.https?`443 / 80`:String(j.port))}),(0,S.jsx)(`p`,{children:n(`deviceSetupSourceScope`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupAfterStart`)})]}),(0,S.jsx)(`a`,{href:`https://github.com/xing-shuyin/pi-web-ui/blob/main/docs/device-center-deployment.md`,target:`_blank`,rel:`noopener noreferrer`,children:n(`deviceSetupGuide`)})]}):(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-setup-fields`,children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupHost`)}),(0,S.jsx)(`input`,{disabled:N||!!z,maxLength:253,value:y,onChange:e=>b(e.target.value)})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupPort`)}),(0,S.jsx)(`input`,{disabled:N||!!z,type:`number`,min:1024,max:65535,value:w,onChange:e=>E(e.target.value)})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{disabled:N||!!z,maxLength:2048,value:D,onChange:e=>O(e.target.value)})]})]}),(0,S.jsx)(`p`,{className:`dc-setup-warning`,children:n(`deviceSetupReachable`)})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupRegistration`)}),!X&&(0,S.jsx)(`p`,{role:`alert`,children:n(g===`remote`?`deviceSetupRemoteInvalid`:`deviceSetupInvalid`)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[g===`current`&&!z&&!q&&(0,S.jsx)(`button`,{className:`dc-button is-primary`,disabled:N||!U||!v||!L||oe||!!v.busy||!X,onClick:()=>void G(),children:n(N?`deviceSetupInstalling`:`deviceSetupInstall`)}),(0,S.jsx)(`button`,{className:`dc-button`,disabled:N||s||!X,onClick:()=>{if(!X||!Y||f.current)return;f.current=!0,l(!0),d(!1);let e=e=>{f.current=!1,p.current&&(l(!1),d(!e))},t=window.__piWebUiHost;try{t?.startChat({newChat:!0,prompt:A(Y,v,g,g===`remote`?j:void 0),onResult:e})||e(!1)}catch{e(!1)}},children:n(g===`remote`?`deviceSetupStartAi`:`deviceSetupAi`)})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupAiHint`)}),s&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupAiCreating`)}),u&&(0,S.jsx)(`p`,{role:`alert`,children:n(`deviceSetupAiFailed`)})]})]}),F&&(0,S.jsxs)(`div`,{role:`alert`,children:[(0,S.jsx)(`p`,{children:n(`deviceSetupFailed`)}),a&&(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:a})]}),z&&(0,S.jsxs)(`div`,{role:`status`,children:[(0,S.jsx)(`h4`,{children:n(`deviceSetupSuccess`)}),(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:z.output.slice(0,16e3)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(re,{href:z.publicUrl,children:n(`deviceHubOpenHub`)}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,onClick:()=>e(z.publicUrl),children:n(`deviceSetupPair`)})]})]})]})}var M={maxActiveTasks:4,maxWaitingTasks:32};function ie(e){let t={settings:{...M},activeCount:0,waitingCount:0,blockedCount:0,running:[],waiting:[],blocked:[]};if(!e||typeof e!=`object`||Array.isArray(e))return t;let n=e,r=n.settings&&typeof n.settings==`object`&&!Array.isArray(n.settings)?n.settings:{},i=typeof r.maxActiveTasks==`number`&&Number.isFinite(r.maxActiveTasks)?Math.max(1,Math.min(16,r.maxActiveTasks)):M.maxActiveTasks,a=typeof r.maxWaitingTasks==`number`&&Number.isFinite(r.maxWaitingTasks)?Math.max(0,Math.min(128,r.maxWaitingTasks)):M.maxWaitingTasks,o=Array.isArray(n.running)?n.running:[],s=Array.isArray(n.waiting)?n.waiting:[],c=Array.isArray(n.blocked)?n.blocked:[];return{settings:{maxActiveTasks:i,maxWaitingTasks:a},activeCount:typeof n.activeCount==`number`?n.activeCount:o.length,waitingCount:typeof n.waitingCount==`number`?n.waitingCount:s.length,blockedCount:typeof n.blockedCount==`number`?n.blockedCount:c.length,running:o,waiting:s,blocked:c}}function N({hubUrl:e,deviceId:t,onNotice:n}){let l=ee(),[u,d]=(0,x.useState)(null),[f,h]=(0,x.useState)(M.maxActiveTasks),[g,_]=(0,x.useState)(M.maxWaitingTasks),[v,ne]=(0,x.useState)(!1),[y,b]=(0,x.useState)(!1),[re,C]=(0,x.useState)(null),[w,T]=(0,x.useState)(null),[E,D]=(0,x.useState)(null),O=(0,x.useRef)(!0),k=(0,x.useRef)(0),A=(0,x.useRef)(new Set),j=(0,x.useRef)(!1),N=(0,x.useRef)(!1),P=`${e??``}:${t??``}`,F=(0,x.useCallback)(async(e,t,n)=>{let r=new AbortController;A.current.add(r);let i=()=>r.abort();n&&(n.aborted?r.abort():n.addEventListener(`abort`,i,{once:!0}));try{let n=await fetch(c(te(`/api/device-center/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:r.signal,headers:t?{"Content-Type":`application/json`}:void 0,body:t?JSON.stringify(t):void 0}),i=await n.json().catch(()=>({}));if(!n.ok){let e=typeof i.error==`string`?i.error:`HTTP ${n.status}`;throw Error(e)}return i}finally{n&&n.removeEventListener(`abort`,i),A.current.delete(r)}},[]),I=(0,x.useCallback)(async(e=!1)=>{if(!O.current||j.current)return;j.current=!0;let t=k.current;e||ne(!0);try{let e=await F(`execution-queue`);if(!O.current||t!==k.current)return;let n=ie(e);d(n),T(null),N.current||=(h(n.settings.maxActiveTasks),_(n.settings.maxWaitingTasks),!0)}catch(e){if(!O.current||t!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{j.current=!1,O.current&&t===k.current&&!e&&ne(!1)}},[F]);(0,x.useEffect)(()=>{O.current=!0,k.current++;for(let e of A.current)e.abort();A.current.clear(),j.current=!1,N.current=!1,d(null),T(null),D(null),I(!1);let e=setInterval(()=>{I(!0)},3e3);return()=>{O.current=!1,k.current++;for(let e of A.current)e.abort();A.current.clear(),clearInterval(e)}},[P,I]);let L=async e=>{e.preventDefault();let t=k.current;b(!0),T(null),D(null);try{let e=await F(`execution-settings`,{maxActiveTasks:Number(f),maxWaitingTasks:Number(g)});if(!O.current||t!==k.current)return;e?.settings&&(h(e.settings.maxActiveTasks??M.maxActiveTasks),_(e.settings.maxWaitingTasks??M.maxWaitingTasks)),D(l(`deviceExecSavedNotice`)),n?.(l(`deviceExecSavedNotice`)),await I(!0)}catch(e){if(!O.current||t!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{O.current&&t===k.current&&b(!1)}},ae=async(e,t)=>{let r=k.current;C(e),T(null);try{if(await F(`execution-queue/cancel`,{taskId:e,attemptId:t}),!O.current||r!==k.current)return;n?.(l(`deviceExecTaskCancelled`)),await I(!0)}catch(e){if(!O.current||r!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{O.current&&r===k.current&&C(null)}},oe=e=>{switch(e){case`max_concurrency_reached`:return l(`deviceExecReasonMaxConcurrency`);case`bot_busy`:return l(`deviceExecReasonBotBusy`);case`workspace_conflict`:return l(`deviceExecReasonWorkspaceConflict`);case`workspace_blocked`:return l(`deviceExecReasonWorkspaceBlocked`);case`room_fairness_yield`:return l(`deviceExecReasonFairness`);default:return e}},R=u?.settings?.maxActiveTasks??M.maxActiveTasks,z=u?.settings?.maxWaitingTasks??M.maxWaitingTasks;return(0,S.jsxs)(`div`,{className:`des-container`,children:[(0,S.jsxs)(`div`,{className:`des-header`,children:[(0,S.jsxs)(`div`,{className:`des-header-left`,children:[(0,S.jsx)(`h3`,{className:`des-title`,children:l(`deviceExecTitle`)}),(0,S.jsx)(`p`,{className:`des-subtitle`,children:l(`deviceExecSubtitle`)})]}),(0,S.jsx)(`button`,{type:`button`,className:`des-refresh-btn`,onClick:()=>void I(!1),disabled:v,title:l(`deviceExecRefresh`),children:(0,S.jsx)(o,{className:v?`des-spin`:``})})]}),w&&(0,S.jsxs)(`div`,{className:`des-alert des-alert-error`,role:`alert`,children:[(0,S.jsx)(p,{className:`des-alert-icon`}),(0,S.jsx)(`div`,{className:`des-alert-text`,children:w}),(0,S.jsx)(`button`,{type:`button`,className:`des-alert-close`,onClick:()=>T(null),children:(0,S.jsx)(m,{})})]}),E&&(0,S.jsxs)(`div`,{className:`des-alert des-alert-success`,role:`status`,children:[(0,S.jsx)(s,{className:`des-alert-icon`}),(0,S.jsx)(`div`,{className:`des-alert-text`,children:E}),(0,S.jsx)(`button`,{type:`button`,className:`des-alert-close`,onClick:()=>D(null),children:(0,S.jsx)(m,{})})]}),(0,S.jsxs)(`div`,{className:`des-stats-row`,children:[(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(r,{className:`des-stat-icon is-running`}),` `,l(`deviceExecRunning`)]}),(0,S.jsxs)(`div`,{className:`des-stat-value`,children:[u?.activeCount??0,` `,(0,S.jsxs)(`span`,{className:`des-stat-sub`,children:[`/ `,R]})]})]}),(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(a,{className:`des-stat-icon is-waiting`}),` `,l(`deviceExecQueued`)]}),(0,S.jsxs)(`div`,{className:`des-stat-value`,children:[u?.waitingCount??0,` `,(0,S.jsxs)(`span`,{className:`des-stat-sub`,children:[`/ `,z]})]})]}),(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(i,{className:`des-stat-icon is-blocked`}),` `,l(`deviceExecBlocked`)]}),(0,S.jsx)(`div`,{className:`des-stat-value${(u?.blockedCount??0)>0?` is-warn`:``}`,children:u?.blockedCount??0})]})]}),(0,S.jsxs)(`form`,{className:`des-form-card`,onSubmit:e=>void L(e),children:[(0,S.jsx)(`h4`,{className:`des-form-title`,children:l(`deviceExecQuotaTitle`)}),(0,S.jsxs)(`div`,{className:`des-fields-row`,children:[(0,S.jsxs)(`div`,{className:`des-field`,children:[(0,S.jsx)(`label`,{htmlFor:`des-max-active`,className:`des-label`,children:l(`deviceExecMaxActive`)}),(0,S.jsx)(`input`,{id:`des-max-active`,type:`number`,min:1,max:16,className:`des-input`,value:f,onChange:e=>h(parseInt(e.target.value,10)||1),required:!0}),(0,S.jsx)(`span`,{className:`des-hint`,children:l(`deviceExecMaxActiveHint`)})]}),(0,S.jsxs)(`div`,{className:`des-field`,children:[(0,S.jsx)(`label`,{htmlFor:`des-max-waiting`,className:`des-label`,children:l(`deviceExecMaxWaiting`)}),(0,S.jsx)(`input`,{id:`des-max-waiting`,type:`number`,min:0,max:128,className:`des-input`,value:g,onChange:e=>_(parseInt(e.target.value,10)||0),required:!0}),(0,S.jsx)(`span`,{className:`des-hint`,children:l(`deviceExecMaxWaitingHint`)})]})]}),(0,S.jsxs)(`div`,{className:`des-form-actions`,children:[(0,S.jsx)(`button`,{type:`button`,className:`des-btn des-btn-secondary`,disabled:y,onClick:()=>{h(R),_(z)},children:l(`deviceExecReset`)}),(0,S.jsx)(`button`,{type:`submit`,className:`des-btn des-btn-primary`,disabled:y,children:l(y?`deviceExecSaving`:`deviceExecSave`)})]})]}),u&&u.blocked&&u.blocked.length>0&&(0,S.jsxs)(`div`,{className:`des-section des-blocked-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title is-danger`,children:[(0,S.jsx)(p,{}),` `,l(`deviceExecBlockedTitle`)]}),(0,S.jsx)(`p`,{className:`des-section-desc`,children:l(`deviceExecBlockedDesc`)}),(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecReason`)})]})}),(0,S.jsx)(`tbody`,{children:u.blocked.map(e=>(0,S.jsxs)(`tr`,{className:`des-row-blocked`,children:[(0,S.jsxs)(`td`,{className:`des-mono`,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:(0,S.jsx)(`span`,{className:`des-badge is-danger`,children:e.reason})})]},`${e.taskId}-${e.attemptId}`))})]})})]}),(0,S.jsxs)(`div`,{className:`des-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title`,children:[(0,S.jsx)(r,{}),` `,l(`deviceExecActiveTasks`),` (`,u?.running?.length??0,`)`]}),u&&u.running&&u.running.length>0?(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecQueuedTime`)})]})}),(0,S.jsx)(`tbody`,{children:u.running.map(e=>(0,S.jsxs)(`tr`,{children:[(0,S.jsxs)(`td`,{className:`des-mono`,title:e.taskId,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:new Date(e.startedAt).toLocaleTimeString()})]},`${e.taskId}-${e.attemptId}`))})]})}):(0,S.jsx)(`div`,{className:`des-empty`,children:l(`deviceExecNoActive`)})]}),(0,S.jsxs)(`div`,{className:`des-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title`,children:[(0,S.jsx)(a,{}),` `,l(`deviceExecWaitingQueue`),` (`,u?.waiting?.length??0,`)`]}),u&&u.waiting&&u.waiting.length>0?(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:l(`deviceExecPosition`)}),(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecReason`)}),(0,S.jsx)(`th`,{children:l(`deviceExecQueuedTime`)}),(0,S.jsx)(`th`,{children:l(`deviceExecActions`)})]})}),(0,S.jsx)(`tbody`,{children:u.waiting.map(e=>(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`td`,{children:(0,S.jsxs)(`span`,{className:`des-pos-badge`,children:[`#`,e.queuePosition]})}),(0,S.jsxs)(`td`,{className:`des-mono`,title:e.taskId,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:(0,S.jsxs)(`div`,{className:`des-reason-cell`,children:[(0,S.jsx)(`span`,{className:`des-badge des-badge-${e.blockReason}`,children:oe(e.blockReason)}),(0,S.jsx)(`span`,{className:`des-reason-detail`,children:e.blockReasonDetail})]})}),(0,S.jsx)(`td`,{children:new Date(e.queuedAt).toLocaleTimeString()}),(0,S.jsx)(`td`,{children:(0,S.jsx)(`button`,{type:`button`,className:`des-btn des-btn-danger-sm`,disabled:re===e.taskId,onClick:()=>void ae(e.taskId,e.attemptId),children:re===e.taskId?l(`deviceExecCancelling`):l(`deviceExecCancel`)})})]},`${e.taskId}-${e.attemptId}`))})]})}):(0,S.jsx)(`div`,{className:`des-empty`,children:l(`deviceExecNoWaiting`)})]})]})}var P=class extends Error{status;constructor(e,t){super(t),this.status=e}};function F(e){try{let t=new URL(e);return[`http:`,`https:`].includes(t.protocol)&&!t.username&&!t.password?t.href:null}catch{return null}}function I(){let e=ee(),n=f().startsWith(`zh`),[r,i]=(0,x.useState)(null),[a,s]=(0,x.useState)(!1),[l,u]=(0,x.useState)(!1),[d,p]=(0,x.useState)(!1),[m,h]=(0,x.useState)(!1),g=(0,x.useRef)(0),_=(0,x.useRef)(null);async function v(e,t){if(_.current)return;let n=++g.current,r=new AbortController;_.current=r,s(!0),u(!1);try{let a=await fetch(c(te(`/api/device-desktop/local/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:r.signal,...t?{headers:{"Content-Type":`application/json`},body:JSON.stringify(t)}:{}});if(g.current!==n||r.signal.aborted)return;if(a.status===403){p(!0),i(null);return}if(!a.ok)throw Error(`Request failed`);let o=await a.json();if(g.current!==n||r.signal.aborted)return;if(e===`permissions`){if(typeof o?.screen!=`boolean`||typeof o?.accessibility!=`boolean`)throw Error(`Invalid permission status`);i(e=>e&&{...e,permissions:{supported:!0,screen:o.screen,accessibility:o.accessibility}}),h(!0)}else{if(typeof o?.os!=`string`||typeof o.enabled!=`boolean`||typeof o.environmentEnabled!=`boolean`||typeof o.supported!=`boolean`)throw Error(`Invalid desktop status`);i(o)}}catch{g.current===n&&!r.signal.aborted&&u(!0)}finally{g.current===n&&!r.signal.aborted&&(_.current=null,s(!1))}}return(0,x.useEffect)(()=>(v(`status`),()=>{g.current++,_.current?.abort(),_.current=null}),[]),(0,S.jsxs)(`section`,{className:`dc-card`,"aria-label":n?`本机远程桌面`:`Local remote desktop`,"aria-busy":a,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(t,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:n?`本机远程桌面`:`Local remote desktop`}),(0,S.jsx)(`p`,{children:n?`管理这台设备的远程桌面与系统权限,无需连接设备中心。`:`Manage remote desktop and system permissions on this device, even while the device center is offline.`})]})]}),d?(0,S.jsx)(`p`,{role:`status`,children:n?`请在设备本机打开此页面以管理远程桌面权限。`:`Open this page on the device itself to manage remote desktop permissions.`}):(0,S.jsxs)(S.Fragment,{children:[r&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-permission-row`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{id:`dc-desktop-label`,children:n?`启用远程桌面`:`Enable remote desktop`}),(0,S.jsx)(`p`,{children:r.enabled?n?`已启用`:`Enabled`:n?`未启用`:`Disabled`})]}),(0,S.jsx)(`button`,{type:`button`,className:`dc-toggle${r.enabled?` is-on`:``}`,role:`switch`,"aria-checked":r.enabled,"aria-labelledby":`dc-desktop-label`,disabled:a||r.environmentEnabled||!r.supported&&!r.enabled,onClick:()=>void v(`enable`,{enabled:!r.enabled}),children:(0,S.jsx)(`span`,{className:`dc-toggle-thumb`})})]}),!r.supported&&(0,S.jsx)(`p`,{children:n?`这台设备暂不支持远程桌面。`:`Remote desktop is currently unavailable on this device.`}),r.environmentEnabled&&(0,S.jsx)(`p`,{children:n?`远程桌面由本机服务设置保持启用。`:`Remote desktop is kept enabled by the service settings on this device.`}),r.os===`darwin`&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n?`点击下方按钮发起系统授权,请在这台 Mac 的系统设置中确认。授权后可能需要重启服务。`:`Use the buttons below to request system permission, then confirm in System Settings on this Mac. You may need to restart the service afterward.`}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(`button`,{type:`button`,className:`dc-button`,disabled:a||r.permissions?.supported===!1,onClick:()=>void v(`permissions`,{kind:`screen`}),children:n?`授予屏幕录制权限`:`Grant screen recording permission`}),(0,S.jsx)(`button`,{type:`button`,className:`dc-button`,disabled:a||r.permissions?.supported===!1,onClick:()=>void v(`permissions`,{kind:`control`}),children:n?`授予远程控制权限`:`Grant remote control permission`})]}),r.permissions?.screen===!0&&(0,S.jsx)(`p`,{children:n?`屏幕录制:已授权`:`Screen recording: granted`}),r.permissions?.accessibility===!0&&(0,S.jsx)(`p`,{children:n?`远程控制:已授权`:`Remote control: granted`}),m&&(0,S.jsx)(`p`,{role:`status`,children:n?`已发起授权请求,请确认系统设置后刷新状态。`:`Permission requested. Confirm in System Settings, then refresh the status.`})]})]}),l&&(0,S.jsx)(`p`,{className:`dc-notice is-error`,role:`alert`,children:n?`无法更新本机远程桌面设置,请重试。`:`Unable to update local remote desktop settings. Please retry.`}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,disabled:a,onClick:()=>void v(`status`),children:[(0,S.jsx)(o,{"aria-hidden":!0}),a?e(`deviceHubWorking`):n?`刷新远程桌面状态`:`Refresh remote desktop status`]})]})]})}function L({pathCompletions:e=[]}){let r=ee(),a=ne(),[f,p]=(0,x.useState)(a.revision?`groups`:`overview`);(0,x.useEffect)(()=>{a.revision&&p(`groups`)},[a.revision]);let[m,g]=(0,x.useState)(null),[_,C]=(0,x.useState)(``),[w,T]=(0,x.useState)(``),[E,D]=(0,x.useState)(null),[O,k]=(0,x.useState)(Date.now()),[A,M]=(0,x.useState)(null),[ie,L]=(0,x.useState)(``),[ae,oe]=(0,x.useState)(``),[R,z]=(0,x.useState)(``),[B,V]=(0,x.useState)(!1),[H,U]=(0,x.useState)(!0),W=(0,x.useRef)(!1),G=(0,x.useRef)(0),K=(0,x.useRef)(new Set),q=(0,x.useRef)(!1),J=(0,x.useRef)(!1),Y=(0,x.useRef)(null),X=(0,x.useRef)(r);X.current=r;async function Z(e,t){let n=new AbortController;K.current.add(n);try{let r=await fetch(c(te(`/api/device-center/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:n.signal,...t?{headers:{"Content-Type":`application/json`},body:JSON.stringify(t)}:{}}),i=await r.json().catch(()=>({}));if(!r.ok)throw new P(r.status,typeof i.error==`string`?i.error:X.current(`deviceHubRequestFailed`,{status:r.status}));return i}finally{K.current.delete(n)}}function Q(e){return W.current&&G.current===e}function se(e){return e instanceof P&&e.status===403?(V(!0),X.current(`deviceHubLocalOnly`)):e instanceof Error?e.message:X.current(`deviceHubNetworkError`)}function ce(e){Y.current=e,g(e),J.current||(T(e.name),C(e.hubUrl??``))}async function le(){let e=G.current;try{let t=await Z(`status`);if(!Q(e)||q.current)return;ce(t),oe(``),V(!1)}catch(t){Q(e)&&oe(se(t))}finally{Q(e)&&U(!1)}}(0,x.useEffect)(()=>{W.current=!0;let e=!1,t,n=async()=>{await le(),!e&&W.current&&(t=setTimeout(n,5e3))};return n(),()=>{e=!0,W.current=!1,G.current++,clearTimeout(t);for(let e of K.current)e.abort()}},[]),(0,x.useEffect)(()=>{if(!E)return;let e=G.current,t=!1,n,r=setInterval(()=>k(Date.now()),1e3),i=async()=>{if(!(t||!Q(e)||Date.now()>=E.expiresAt)){try{let n=await Z(`poll`,{});if(t||!Q(e))return;if(n.paired){let n=await Z(`status`);if(t||!Q(e))return;J.current=!1,ce(n),D(null),L(``),z(``);return}L(``)}catch(n){!t&&Q(e)&&L(`${X.current(`deviceHubPollError`)} ${se(n)}`)}!t&&Q(e)&&(n=setTimeout(i,2e3))}};return n=setTimeout(i,2e3),()=>{t=!0,clearTimeout(n),clearInterval(r)}},[E]);async function ue(){if(q.current)return;let e=F(_.trim());if(!e){L(r(`deviceHubInvalidUrl`));return}if(!w.trim()||w.trim().length>80){L(r(`deviceHubInvalidName`));return}let t=++G.current;q.current=!0,M(`pair`),L(``),z(``),D(null);try{let n=await Z(`pair`,{hubUrl:e,name:w.trim()});if(!Q(t))return;if(!n.code||!Number.isFinite(n.expiresAt)||!F(n.hubUrl))throw Error(r(`deviceHubPairError`));k(Date.now()),D(n)}catch(e){Q(t)&&L(`${r(`deviceHubPairError`)} ${se(e)}`)}finally{Q(t)&&(q.current=!1,M(null))}}async function de(){let e=Y.current;if(!e?.deviceId||q.current)return;let t=++G.current;q.current=!0,M(`permission`),L(``),z(``),ce({...e,allowExecution:!e.allowExecution});try{await Z(`execution`,{allowExecution:!e.allowExecution}),Q(t)&&z(r(`deviceHubSaved`))}catch(n){Q(t)&&(ce(e),L(`${r(`deviceHubPermissionError`)} ${se(n)}`))}finally{Q(t)&&(q.current=!1,M(null))}}async function fe(){let e=Y.current;if(!e?.deviceId||q.current||!window.confirm(r(`deviceHubDisconnectConfirm`,{name:e.name})))return;let t=++G.current;q.current=!0,M(`disconnect`),L(``),z(``);try{if(await Z(`disconnect`,{}),!Q(t))return;J.current=!1,ce({name:e.name,allowExecution:!1,connected:!1}),D(null)}catch(e){Q(t)&&L(`${r(`deviceHubDisconnectError`)} ${se(e)}`)}finally{Q(t)&&(q.current=!1,M(null))}}async function pe(){if(!E)return;let e=G.current;try{await navigator.clipboard.writeText(E.code),Q(e)&&z(r(`deviceHubCopied`))}catch{Q(e)&&L(r(`deviceHubCopyFailed`))}}let $=!!m?.deviceId,me=F($?m?.hubUrl??``:E?.hubUrl??``),he=Math.max(0,Math.ceil(((E?.expiresAt??0)-O)/1e3)),ge=`${Math.floor(he/60)}:${String(he%60).padStart(2,`0`)}`,_e=me?(0,S.jsxs)(re,{href:me,children:[(0,S.jsx)(u,{"aria-hidden":!0}),r(`deviceHubOpenHub`)]}):null;return(0,S.jsxs)(`section`,{className:`dc-page`,"aria-label":r(`deviceHubTitle`),children:[(0,S.jsxs)(`header`,{className:`dc-header`,children:[(0,S.jsxs)(`div`,{className:`dc-heading`,children:[(0,S.jsxs)(`span`,{className:`dc-eyebrow`,children:[(0,S.jsx)(t,{"aria-hidden":!0}),r(`deviceHubTitle`)]}),(0,S.jsx)(`h1`,{children:r(`deviceHubTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubSubtitle`)})]}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[$&&_e,(0,S.jsxs)(`span`,{className:`dc-status ${$?m?.connected?`is-online`:`is-offline`:`is-unpaired`}`,children:[(0,S.jsx)(`span`,{className:`dc-status-dot`}),r($?m?.connected?`deviceHubOnline`:`deviceHubOffline`:`deviceHubUnpaired`)]})]})]}),(0,S.jsxs)(`div`,{className:`dc-layout`,children:[(0,S.jsxs)(`nav`,{className:`dc-sidebar`,"aria-label":r(`deviceHubTitle`),children:[(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`overview`?` is-active`:``}`,"aria-current":f===`overview`?`page`:void 0,onClick:()=>p(`overview`),children:[(0,S.jsx)(t,{"aria-hidden":!0}),r(`deviceHubOverview`)]}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`groups`?` is-active`:``}`,"aria-current":f===`groups`?`page`:void 0,onClick:()=>p(`groups`),children:[(0,S.jsx)(l,{"aria-hidden":!0}),r(`deviceHubGroups`)]}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`settings`?` is-active`:``}`,"aria-current":f===`settings`?`page`:void 0,onClick:()=>p(`settings`),children:[(0,S.jsx)(h,{"aria-hidden":!0}),r(`deviceHubSettings`)]})]}),(0,S.jsxs)(`main`,{className:`dc-content`,children:[H&&(0,S.jsxs)(`div`,{className:`dc-loading`,role:`status`,children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubLoading`)]}),(B||ae)&&(0,S.jsxs)(`div`,{className:`dc-notice is-error`,role:`alert`,children:[(0,S.jsx)(`span`,{children:B?r(`deviceHubLocalOnly`):`${r(`deviceHubStatusError`)} ${ae}`}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,onClick:()=>void le(),children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubRetry`)]})]}),ie&&!B&&(0,S.jsx)(`div`,{className:`dc-notice is-error`,role:`alert`,children:ie}),R&&(0,S.jsxs)(`div`,{className:`dc-notice is-success`,role:`status`,children:[(0,S.jsx)(s,{"aria-hidden":!0}),R]}),!$&&(m||B)&&f===`overview`&&(0,S.jsx)(j,{initialHubUrl:_,onConnect:e=>{if(B){z(r(`deviceHubLocalOnly`));return}J.current=!0,C(e),p(`settings`)}}),!B&&f===`overview`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsx)(`h3`,{children:r(`deviceSetupNext`)}),(0,S.jsx)(`p`,{children:r(`deviceSetupNextHint`)}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,onClick:()=>p(`groups`),children:r(`deviceSetupManage`)})]}),f===`settings`&&(0,S.jsx)(I,{}),m&&!B&&!$&&f===`settings`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(h,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:r(`deviceHubPairTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubPairHint`)})]})]}),(0,S.jsx)(`ol`,{className:`dc-steps`,children:[`deviceHubStepDetails`,`deviceHubStepApprove`,`deviceHubStepWait`].map((e,t)=>(0,S.jsxs)(`li`,{className:E?t===0?`is-complete`:`is-active`:t===0?`is-active`:``,children:[(0,S.jsx)(`span`,{className:`dc-step-number`,children:E&&t===0?(0,S.jsx)(s,{"aria-hidden":!0}):t+1}),(0,S.jsx)(`span`,{children:r(e)})]},e))}),(0,S.jsxs)(`form`,{className:`dc-form`,onSubmit:e=>{e.preventDefault(),ue()},children:[(0,S.jsxs)(`div`,{className:`dc-field`,children:[(0,S.jsx)(`label`,{htmlFor:`dc-hub-url`,children:r(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{id:`dc-hub-url`,type:`url`,autoComplete:`url`,required:!0,placeholder:r(`deviceHubHubPlaceholder`),value:_,disabled:!!E||!!A,onChange:e=>{J.current=!0,C(e.target.value)}})]}),(0,S.jsxs)(`div`,{className:`dc-field`,children:[(0,S.jsx)(`label`,{htmlFor:`dc-device-name`,children:r(`deviceHubName`)}),(0,S.jsx)(`input`,{id:`dc-device-name`,maxLength:80,required:!0,autoComplete:`off`,placeholder:r(`deviceHubNamePlaceholder`),value:w,disabled:!!E||!!A,onChange:e=>{J.current=!0,T(e.target.value)}})]}),!E&&(0,S.jsx)(`div`,{className:`dc-actions`,children:(0,S.jsxs)(`button`,{className:`dc-button is-primary`,type:`submit`,disabled:!!A,children:[(0,S.jsx)(h,{"aria-hidden":!0}),r(A?`deviceHubWorking`:`deviceHubGetCode`)]})})]}),E&&(0,S.jsxs)(`div`,{className:`dc-pairing`,children:[(0,S.jsx)(`span`,{className:`dc-code-label`,children:r(`deviceHubCode`)}),(0,S.jsxs)(`div`,{className:`dc-code-row`,children:[(0,S.jsx)(`code`,{className:`dc-code`,children:E.code}),(0,S.jsx)(`button`,{className:`dc-icon-button`,type:`button`,"aria-label":r(`deviceHubCopy`),onClick:()=>void pe(),children:(0,S.jsx)(n,{"aria-hidden":!0})})]}),(0,S.jsx)(`div`,{className:`dc-pair-meta`,children:r(he?`deviceHubExpires`:`deviceHubExpired`,{time:ge})}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[_e,(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,disabled:!!A,onClick:()=>{++G.current,D(null),L(``),z(``)},children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubNewCode`)]})]}),he>0&&(0,S.jsxs)(`p`,{className:`dc-progress`,role:`status`,children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubWaiting`)]})]})]}),$&&m&&!B&&(0,S.jsxs)(S.Fragment,{children:[f===`overview`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-device-summary`,children:[(0,S.jsx)(`span`,{className:`dc-device-icon`,children:(0,S.jsx)(t,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:m.name}),(0,S.jsx)(`p`,{children:r(`deviceHubDeviceTitle`)})]})]}),(0,S.jsxs)(`dl`,{className:`dc-details`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`dt`,{children:r(`deviceHubDeviceId`)}),(0,S.jsx)(`dd`,{children:m.deviceId})]}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`dt`,{children:r(`deviceHubHubUrl`)}),(0,S.jsx)(`dd`,{children:m.hubUrl})]})]})]}),f===`settings`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(i,{"aria-hidden":!0})}),(0,S.jsx)(`div`,{children:(0,S.jsx)(`h2`,{children:r(`deviceHubPermissions`)})})]}),(0,S.jsxs)(`div`,{className:`dc-permission-row`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{id:`dc-permission-label`,children:r(`deviceHubPermissionLabel`)}),(0,S.jsx)(`p`,{id:`dc-permission-hint`,children:r(`deviceHubPermissionHint`)})]}),(0,S.jsx)(`button`,{type:`button`,className:`dc-toggle${m.allowExecution?` is-on`:``}`,role:`switch`,"aria-checked":m.allowExecution,"aria-labelledby":`dc-permission-label`,"aria-describedby":`dc-permission-hint`,disabled:!!A,onClick:()=>void de(),children:(0,S.jsx)(`span`,{className:`dc-toggle-thumb`})})]}),(0,S.jsx)(`p`,{className:`dc-permission-state`,role:`status`,children:r(A===`permission`?`deviceHubSaving`:m.allowExecution?`deviceHubEnabled`:`deviceHubReadOnly`)})]}),f===`settings`&&(0,S.jsxs)(`details`,{className:`dc-advanced`,children:[(0,S.jsx)(`summary`,{children:r(`deviceHubAdvanced`)}),(0,S.jsxs)(`div`,{className:`dc-danger-zone`,children:[(0,S.jsx)(`h3`,{children:r(`deviceHubDisconnect`)}),(0,S.jsx)(`p`,{children:r(`deviceHubDisconnectHint`)}),(0,S.jsx)(`button`,{type:`button`,className:`dc-button is-danger`,disabled:!!A,onClick:()=>void fe(),children:r(A===`disconnect`?`deviceHubWorking`:`deviceHubDisconnect`)})]})]})]}),$&&m?.hubUrl&&m.deviceId&&!B&&f!==`groups`&&(0,S.jsx)(v,{hubUrl:m.hubUrl,currentDeviceId:m.deviceId},`directory:${m.hubUrl}:${m.deviceId}`),!B&&(f===`settings`||f===`groups`)&&(0,S.jsx)(y,{pathCompletions:e},`bots:${m?.hubUrl??``}:${m?.deviceId??`unpaired`}`),f===`settings`&&(0,S.jsx)(N,{hubUrl:m?.hubUrl,deviceId:m?.deviceId,onNotice:z}),f===`groups`&&(0,S.jsx)(b,{navigation:a}),(0,S.jsxs)(`aside`,{className:`dc-help`,children:[(0,S.jsx)(d,{className:`dc-help-icon`,"aria-hidden":!0}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{children:r(`deviceHubHelpTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubHelpText`)})]})]})]})]})]})}export{L as DeviceCenter};
@@ -0,0 +1,3 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{At as t,B as n,Ft as r,Kt as i,L as a,Lt as o,O as s,Sn as c,Tt as l,Z as u,_n as ee,dt as d,gn as f,p,sn as m,vt as h,wn as te}from"./app-globals-DZNa9v8u.js";import{i as g,t as _}from"./react-CR5VJ85Q.js";import{Rt as v,T as ne,_t as y}from"./index-DHeeHS_l.js";import{DeviceGroups as b}from"./DeviceGroups-CLdpdnI7.js";var x=e(g(),1),S=_();function re({href:e,children:t}){let n=ee(),[r,i]=(0,x.useState)(!1),[a,o]=(0,x.useState)(!1);return(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`a`,{className:`dc-button`,href:e,target:`_blank`,rel:`noopener noreferrer`,onClick:t=>{let n=window.piDesktop;n?.openHub&&(t.preventDefault(),!a&&(o(!0),i(!1),n.openHub(e).then(e=>i(!e),()=>i(!0)).finally(()=>o(!1))))},"aria-busy":a,children:t}),r&&(0,S.jsxs)(`span`,{role:`alert`,children:[n(`deviceHubNetworkError`),` `,(0,S.jsx)(`a`,{href:e,target:`_blank`,rel:`noopener noreferrer`,children:e})]})]})}function C(e){if(!w(e.host)||!Number.isInteger(e.port)||e.port<1024||e.port>65535||e.publicUrl.length>2048||/[\s\u0000-\u001f\u007f]/.test(e.publicUrl))return!1;try{let t=new URL(e.publicUrl);return[`http:`,`https:`].includes(t.protocol)&&!!t.hostname&&!t.username&&!t.password&&!t.search&&!t.hash&&t.pathname===`/`&&![`0.0.0.0`,`[::]`].includes(t.hostname)}catch{return!1}}function w(e){if(e===`localhost`)return!0;if(/^(?:\d{1,3}\.){3}\d{1,3}$/.test(e))return e.split(`.`).every(e=>String(Number(e))===e&&Number(e)<=255);if(!e.includes(`:`)||!/^[0-9a-fA-F:.]+$/.test(e))return!1;try{return new URL(`http://[${e}]/`).hostname.startsWith(`[`)}catch{return!1}}function T(e){try{let t=new URL(e);return[`http:`,`https:`].includes(t.protocol)&&[`localhost`,`127.0.0.1`,`[::1]`].includes(t.hostname)}catch{return!1}}var E=`https://raw.githubusercontent.com/xing-shuyin/pi-web-ui/main/docs/device-center-deployment.md`,D=`https://github.com/xing-shuyin/pi-web-ui/blob/main/docs/device-center-deployment.md`,O=`pi-web-ui hub install --host 0.0.0.0 --port 8790 --public-url http://SERVER_IP:8790 --allow-registration`;function k(e){let t=e=>e.includes(`:`)?w(e)&&![`::`,`::1`].includes(e):/^[\d.]+$/.test(e)?w(e)&&![`0.0.0.0`,`127.0.0.1`].includes(e):e.length<=253&&e.split(`.`).every(e=>/^[a-z\d](?:[a-z\d-]{0,61}[a-z\d])?$/i.test(e))&&e!==`localhost`;if(!t(e.server)||!Number.isInteger(e.sshPort)||e.sshPort<1||e.sshPort>65535||!Number.isInteger(e.port)||e.port<1024||e.port>65535||!/^[a-z_][a-z\d_.-]{0,63}$/i.test(e.username)||![`password`,`key`,`existing`].includes(e.auth)||e.domain!==``&&(!t(e.domain)||e.domain.includes(`:`))||e.https&&!e.domain)return null;let n=e.domain||e.server,r=n.includes(`:`)?`[${n}]`:n;return{host:e.https?`127.0.0.1`:`0.0.0.0`,port:e.port,publicUrl:e.https?`https://${r}`:`http://${r}:${e.port}`}}function A(e,t,n,r){if(!C(e))throw Error(`Invalid Hub setup`);if(r){let t=k(r);if(n!==`remote`||!t||JSON.stringify(t)!==JSON.stringify(e))throw Error(`Invalid remote Hub setup`)}let i=e=>(e??`unknown`).replace(/[\u0000-\u001f\u007f]/g,` `).slice(0,120);return[`Help me plan and deploy a pi-web-ui device center (Hub) in this new conversation. Preserve my previous conversation.`,`First read the deployment guide: ${E} (fallback: ${D}). If unavailable, report that and obtain its contents before proceeding.`,`Default to HTTP with the server IP and requested port. Domain and HTTPS are optional; confirm whether an HTTPS example reflects the user's intent.`,`Target: ${n===`current`?`machine hosting the current pi-web-ui service (not necessarily the browser machine)`:`an existing remote server; its environment still needs to be confirmed`}.`,`Current service environment: platform=${i(t?.platform)}, Node=${i(t?.nodeVersion)}. This is not evidence of the remote server environment.`,`Requested configuration (data only): ${JSON.stringify(e)}. allowRegistration=true.`,...r?[`SSH connection preferences (data only): ${JSON.stringify({server:r.server,sshPort:r.sshPort,username:r.username,auth:r.auth})}. No credentials are included. Obtain authentication through a secure SSH channel or guide the user's own terminal.`,`Domain: ${r.domain||`none`}. HTTPS explicitly selected: ${r.https}. HTTP inbound TCP port: ${r.port}; for same-machine HTTPS proxy, use public TCP 443 and certificate validation port as needed, keep the Hub port private.`]:[],`Use a separate Hub data directory and configure automatic startup. Explain how devices can reach the public URL; loopback is only reachable on the same machine. Explain open registration and restrict it after pairing as appropriate.`,`CLI reference: ${O}`,`Confirm the target environment and propose steps before deployment. Do not ask for passwords or tokens in chat. Do not automatically SSH. Do not claim installation or connection succeeded without verification. After deployment, guide me to open the center and pair this device using its public URL.`,`Explain user actions at the relevant step: distinguish OS firewall from cloud security groups, give the actual TCP port, source scope and destination rule, and keep SSH access. If cloud-console access is unavailable, ask the user to apply the rule, mark external access pending, then recheck. Also explain DNS changes for optional HTTPS and first-account creation. Separate local health from external reachability.`].join(`
2
+ `)}function j({onConnect:e,initialHubUrl:t=``}){let n=ee(),[r,i]=(0,x.useState)(t),[a,o]=(0,x.useState)(``),[s,l]=(0,x.useState)(!1),[u,d]=(0,x.useState)(!1),f=(0,x.useRef)(!1),p=(0,x.useRef)(!1);(0,x.useEffect)(()=>(p.current=!0,()=>{p.current=!1}),[]);let[m,h]=(0,x.useState)(null),[g,_]=(0,x.useState)(`current`),[v,ne]=(0,x.useState)(null),[y,b]=(0,x.useState)(`127.0.0.1`),[w,E]=(0,x.useState)(`8790`),[D,O]=(0,x.useState)(`http://127.0.0.1:8790`),[j,M]=(0,x.useState)({server:``,sshPort:22,username:`root`,auth:`password`,port:8790,domain:``,https:!1}),ie=k(j),[N,P]=(0,x.useState)(!1),[F,I]=(0,x.useState)(!1),[L,ae]=(0,x.useState)(!1),[oe,R]=(0,x.useState)(!1),[z,B]=(0,x.useState)(null),V=(0,x.useRef)(null),H=(0,x.useRef)(!1),U=T(window.location.origin);(0,x.useEffect)(()=>()=>V.current?.abort(),[]);let W=async()=>{if(H.current)return;V.current?.abort();let e=new AbortController;V.current=e,I(!1),ae(!1),R(!1);try{let t=await fetch(c(te(`/api/device-center/setup`)),{credentials:`same-origin`,signal:e.signal});if(!t.ok)throw Error();let n=await t.json();if(!n||typeof n.platform!=`string`||typeof n.nodeVersion!=`string`||typeof n.dataDir!=`string`||typeof n.cliPath!=`string`||!C({host:`127.0.0.1`,port:n.defaultPort,publicUrl:n.defaultPublicUrl}))throw Error();if(e.signal.aborted)return;ne(n),n.installation?.installed?(b(n.installation.host??`127.0.0.1`),E(String(n.installation.port)),O(n.installation.publicUrl??``),i(n.installation.publicUrl??``)):v||(E(String(n.defaultPort)),O(n.defaultPublicUrl))}catch{e.signal.aborted||(I(!0),R(!0))}finally{e.signal.aborted||ae(!0)}},G=async()=>{let e={host:y,port:Number(w),publicUrl:D};if(H.current||!U||!v||!L||oe||v.busy||v.installation?.installed||!C(e))return;H.current=!0,V.current?.abort(),P(!0),I(!1),o(``);let t=new AbortController;V.current=t;try{let n=await fetch(c(te(`/api/device-center/setup`)),{credentials:`same-origin`,method:`POST`,headers:{"Content-Type":`application/json`},body:JSON.stringify({...e,allowRegistration:!0}),signal:t.signal});if(n.status===409){ne(e=>e&&{...e,busy:!0}),H.current=!1,P(!1),await W();return}if(!n.ok){let e=await n.json().catch(()=>({}));throw Error(typeof e.error==`string`?e.error.slice(0,4096):`HTTP ${n.status}`)}let r=await n.json();if(!C({...e,publicUrl:r.publicUrl})||typeof r.output!=`string`)throw Error();t.signal.aborted||B(r)}catch(e){t.signal.aborted||(I(!0),o(e instanceof Error?e.message.slice(0,4096):``))}finally{H.current=!1,t.signal.aborted||P(!1)}},K=v?.installation,q=!!K?.installed,J=q&&K.running&&K.owned,Y=g===`remote`?ie:{host:y,port:Number(w),publicUrl:D},X=!!Y&&C(Y);return(0,S.jsxs)(`section`,{className:`dc-onboarding`,children:[(0,S.jsx)(`h3`,{children:n(`deviceSetupWelcome`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupIntro`)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(`button`,{className:`dc-button`,disabled:N,"aria-pressed":m===`connect`,onClick:()=>h(`connect`),children:n(`deviceSetupConnect`)}),(0,S.jsx)(`button`,{className:`dc-button`,disabled:N,"aria-pressed":m===`create`,onClick:()=>{h(`create`),!v&&U&&W()},children:n(`deviceSetupCreate`)})]}),m===`connect`&&(0,S.jsxs)(`form`,{onSubmit:t=>{t.preventDefault(),e(r.trim())},children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{type:`url`,required:!0,maxLength:2048,value:r,onChange:e=>i(e.target.value),placeholder:`https://hub.example.com`})]}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,disabled:!C({host:`localhost`,port:8790,publicUrl:r.trim()}),children:n(`deviceSetupPair`)})]}),m===`create`&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupTarget`)}),(0,S.jsxs)(`select`,{disabled:N,value:g,onChange:e=>_(e.target.value),children:[(0,S.jsx)(`option`,{value:`current`,children:n(`deviceSetupCurrent`)}),(0,S.jsx)(`option`,{value:`remote`,children:n(`deviceSetupRemote`)})]})]}),(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n(g===`current`?`deviceSetupCurrentHint`:`deviceSetupRemoteHint`)}),g===`current`&&(0,S.jsxs)(`div`,{className:`dc-setup-plan`,children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupPlan`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupPlanHint`)}),v&&(0,S.jsxs)(`dl`,{children:[(0,S.jsx)(`dt`,{children:n(`deviceSetupEnvironment`)}),(0,S.jsxs)(`dd`,{children:[v.platform,` · Node `,v.nodeVersion]}),(0,S.jsx)(`dt`,{children:n(`deviceSetupDataDir`)}),(0,S.jsx)(`dd`,{children:v.dataDir}),(0,S.jsx)(`dt`,{children:n(`deviceSetupCli`)}),(0,S.jsx)(`dd`,{children:v.cliPath})]}),!U&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupLocalOnly`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupBackground`)}),q&&(0,S.jsxs)(`div`,{role:`status`,children:[(0,S.jsx)(`h4`,{children:n(J?`deviceSetupRestored`:`deviceSetupUnhealthy`)}),(0,S.jsx)(`p`,{children:K.publicUrl}),J&&X&&K.publicUrl?(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(re,{href:K.publicUrl,children:n(`deviceHubOpenHub`)}),(0,S.jsx)(`button`,{className:`dc-button`,onClick:()=>e(K.publicUrl),children:n(`deviceSetupPair`)})]}):(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n(`deviceSetupRecovery`)}),(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:`pi-web-ui hub status --json
3
+ pi-web-ui hub --help`})]})]}),v?.busy&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupBusy`)}),U&&(0,S.jsx)(`button`,{className:`dc-button`,disabled:N||!L,onClick:()=>void W(),children:n(L?`deviceSetupRetry`:`deviceHubWorking`)})]}),g===`remote`?(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-setup-fields`,children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupServer`)}),(0,S.jsx)(`input`,{autoComplete:`off`,maxLength:253,placeholder:`203.0.113.10`,value:j.server,onChange:e=>M({...j,server:e.target.value.trim()})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupSshPort`)}),(0,S.jsx)(`input`,{type:`number`,min:1,max:65535,value:j.sshPort,onChange:e=>M({...j,sshPort:Number(e.target.value)})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupUsername`)}),(0,S.jsx)(`input`,{autoComplete:`off`,maxLength:64,value:j.username,onChange:e=>M({...j,username:e.target.value})})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupAuth`)}),(0,S.jsxs)(`select`,{value:j.auth,onChange:e=>M({...j,auth:e.target.value}),children:[(0,S.jsx)(`option`,{value:`password`,children:n(`deviceSetupAuthPassword`)}),(0,S.jsx)(`option`,{value:`key`,children:n(`deviceSetupAuthKey`)}),(0,S.jsx)(`option`,{value:`existing`,children:n(`deviceSetupAuthExisting`)})]})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupServicePort`)}),(0,S.jsx)(`input`,{type:`number`,min:1024,max:65535,value:j.port,onChange:e=>M({...j,port:Number(e.target.value)})})]})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupCredentialHint`)}),(0,S.jsxs)(`details`,{children:[(0,S.jsx)(`summary`,{children:n(`deviceSetupOptionalTls`)}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupDomain`)}),(0,S.jsx)(`input`,{maxLength:253,placeholder:`hub.example.com`,value:j.domain,onChange:e=>M({...j,domain:e.target.value.trim()})})]}),(0,S.jsxs)(`label`,{children:[(0,S.jsx)(`input`,{type:`checkbox`,checked:j.https,onChange:e=>M({...j,https:e.target.checked})}),` `,n(`deviceSetupEnableHttps`)]}),(0,S.jsx)(`p`,{children:n(`deviceSetupTlsHint`)})]}),(0,S.jsxs)(`p`,{children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupPreview`)}),` `,ie?.publicUrl??n(`deviceSetupPreviewEmpty`)]}),(0,S.jsxs)(`div`,{className:`dc-setup-plan`,children:[(0,S.jsx)(`strong`,{children:n(`deviceSetupUserActions`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupCloudPorts`).replace(`{ports}`,j.https?`443 / 80`:String(j.port))}),(0,S.jsx)(`p`,{children:n(`deviceSetupSourceScope`)}),(0,S.jsx)(`p`,{children:n(`deviceSetupAfterStart`)})]}),(0,S.jsx)(`a`,{href:`https://github.com/xing-shuyin/pi-web-ui/blob/main/docs/device-center-deployment.md`,target:`_blank`,rel:`noopener noreferrer`,children:n(`deviceSetupGuide`)})]}):(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-setup-fields`,children:[(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupHost`)}),(0,S.jsx)(`input`,{disabled:N||!!z,maxLength:253,value:y,onChange:e=>b(e.target.value)})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceSetupPort`)}),(0,S.jsx)(`input`,{disabled:N||!!z,type:`number`,min:1024,max:65535,value:w,onChange:e=>E(e.target.value)})]}),(0,S.jsxs)(`label`,{className:`dc-field`,children:[(0,S.jsx)(`span`,{children:n(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{disabled:N||!!z,maxLength:2048,value:D,onChange:e=>O(e.target.value)})]})]}),(0,S.jsx)(`p`,{className:`dc-setup-warning`,children:n(`deviceSetupReachable`)})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupRegistration`)}),!X&&(0,S.jsx)(`p`,{role:`alert`,children:n(g===`remote`?`deviceSetupRemoteInvalid`:`deviceSetupInvalid`)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[g===`current`&&!z&&!q&&(0,S.jsx)(`button`,{className:`dc-button is-primary`,disabled:N||!U||!v||!L||oe||!!v.busy||!X,onClick:()=>void G(),children:n(N?`deviceSetupInstalling`:`deviceSetupInstall`)}),(0,S.jsx)(`button`,{className:`dc-button`,disabled:N||s||!X,onClick:()=>{if(!X||!Y||f.current)return;f.current=!0,l(!0),d(!1);let e=e=>{f.current=!1,p.current&&(l(!1),d(!e))},t=window.__piWebUiHost;try{t?.startChat({newChat:!0,prompt:A(Y,v,g,g===`remote`?j:void 0),onResult:e})||e(!1)}catch{e(!1)}},children:n(g===`remote`?`deviceSetupStartAi`:`deviceSetupAi`)})]}),(0,S.jsx)(`p`,{children:n(`deviceSetupAiHint`)}),s&&(0,S.jsx)(`p`,{role:`status`,children:n(`deviceSetupAiCreating`)}),u&&(0,S.jsx)(`p`,{role:`alert`,children:n(`deviceSetupAiFailed`)})]})]}),F&&(0,S.jsxs)(`div`,{role:`alert`,children:[(0,S.jsx)(`p`,{children:n(`deviceSetupFailed`)}),a&&(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:a})]}),z&&(0,S.jsxs)(`div`,{role:`status`,children:[(0,S.jsx)(`h4`,{children:n(`deviceSetupSuccess`)}),(0,S.jsx)(`pre`,{className:`dc-setup-output`,children:z.output.slice(0,16e3)}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(re,{href:z.publicUrl,children:n(`deviceHubOpenHub`)}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,onClick:()=>e(z.publicUrl),children:n(`deviceSetupPair`)})]})]})]})}var M={maxActiveTasks:4,maxWaitingTasks:32};function ie(e){let t={settings:{...M},activeCount:0,waitingCount:0,blockedCount:0,running:[],waiting:[],blocked:[]};if(!e||typeof e!=`object`||Array.isArray(e))return t;let n=e,r=n.settings&&typeof n.settings==`object`&&!Array.isArray(n.settings)?n.settings:{},i=typeof r.maxActiveTasks==`number`&&Number.isFinite(r.maxActiveTasks)?Math.max(1,Math.min(16,r.maxActiveTasks)):M.maxActiveTasks,a=typeof r.maxWaitingTasks==`number`&&Number.isFinite(r.maxWaitingTasks)?Math.max(0,Math.min(128,r.maxWaitingTasks)):M.maxWaitingTasks,o=Array.isArray(n.running)?n.running:[],s=Array.isArray(n.waiting)?n.waiting:[],c=Array.isArray(n.blocked)?n.blocked:[];return{settings:{maxActiveTasks:i,maxWaitingTasks:a},activeCount:typeof n.activeCount==`number`?n.activeCount:o.length,waitingCount:typeof n.waitingCount==`number`?n.waitingCount:s.length,blockedCount:typeof n.blockedCount==`number`?n.blockedCount:c.length,running:o,waiting:s,blocked:c}}function N({hubUrl:e,deviceId:t,onNotice:n}){let l=ee(),[u,d]=(0,x.useState)(null),[f,h]=(0,x.useState)(M.maxActiveTasks),[g,_]=(0,x.useState)(M.maxWaitingTasks),[v,ne]=(0,x.useState)(!1),[y,b]=(0,x.useState)(!1),[re,C]=(0,x.useState)(null),[w,T]=(0,x.useState)(null),[E,D]=(0,x.useState)(null),O=(0,x.useRef)(!0),k=(0,x.useRef)(0),A=(0,x.useRef)(new Set),j=(0,x.useRef)(!1),N=(0,x.useRef)(!1),P=`${e??``}:${t??``}`,F=(0,x.useCallback)(async(e,t,n)=>{let r=new AbortController;A.current.add(r);let i=()=>r.abort();n&&(n.aborted?r.abort():n.addEventListener(`abort`,i,{once:!0}));try{let n=await fetch(c(te(`/api/device-center/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:r.signal,headers:t?{"Content-Type":`application/json`}:void 0,body:t?JSON.stringify(t):void 0}),i=await n.json().catch(()=>({}));if(!n.ok){let e=typeof i.error==`string`?i.error:`HTTP ${n.status}`;throw Error(e)}return i}finally{n&&n.removeEventListener(`abort`,i),A.current.delete(r)}},[]),I=(0,x.useCallback)(async(e=!1)=>{if(!O.current||j.current)return;j.current=!0;let t=k.current;e||ne(!0);try{let e=await F(`execution-queue`);if(!O.current||t!==k.current)return;let n=ie(e);d(n),T(null),N.current||=(h(n.settings.maxActiveTasks),_(n.settings.maxWaitingTasks),!0)}catch(e){if(!O.current||t!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{j.current=!1,O.current&&t===k.current&&!e&&ne(!1)}},[F]);(0,x.useEffect)(()=>{O.current=!0,k.current++;for(let e of A.current)e.abort();A.current.clear(),j.current=!1,N.current=!1,d(null),T(null),D(null),I(!1);let e=setInterval(()=>{I(!0)},3e3);return()=>{O.current=!1,k.current++;for(let e of A.current)e.abort();A.current.clear(),clearInterval(e)}},[P,I]);let L=async e=>{e.preventDefault();let t=k.current;b(!0),T(null),D(null);try{let e=await F(`execution-settings`,{maxActiveTasks:Number(f),maxWaitingTasks:Number(g)});if(!O.current||t!==k.current)return;e?.settings&&(h(e.settings.maxActiveTasks??M.maxActiveTasks),_(e.settings.maxWaitingTasks??M.maxWaitingTasks)),D(l(`deviceExecSavedNotice`)),n?.(l(`deviceExecSavedNotice`)),await I(!0)}catch(e){if(!O.current||t!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{O.current&&t===k.current&&b(!1)}},ae=async(e,t)=>{let r=k.current;C(e),T(null);try{if(await F(`execution-queue/cancel`,{taskId:e,attemptId:t}),!O.current||r!==k.current)return;n?.(l(`deviceExecTaskCancelled`)),await I(!0)}catch(e){if(!O.current||r!==k.current)return;e.name!==`AbortError`&&T(e.message)}finally{O.current&&r===k.current&&C(null)}},oe=e=>{switch(e){case`max_concurrency_reached`:return l(`deviceExecReasonMaxConcurrency`);case`bot_busy`:return l(`deviceExecReasonBotBusy`);case`workspace_conflict`:return l(`deviceExecReasonWorkspaceConflict`);case`workspace_blocked`:return l(`deviceExecReasonWorkspaceBlocked`);case`room_fairness_yield`:return l(`deviceExecReasonFairness`);default:return e}},R=u?.settings?.maxActiveTasks??M.maxActiveTasks,z=u?.settings?.maxWaitingTasks??M.maxWaitingTasks;return(0,S.jsxs)(`div`,{className:`des-container`,children:[(0,S.jsxs)(`div`,{className:`des-header`,children:[(0,S.jsxs)(`div`,{className:`des-header-left`,children:[(0,S.jsx)(`h3`,{className:`des-title`,children:l(`deviceExecTitle`)}),(0,S.jsx)(`p`,{className:`des-subtitle`,children:l(`deviceExecSubtitle`)})]}),(0,S.jsx)(`button`,{type:`button`,className:`des-refresh-btn`,onClick:()=>void I(!1),disabled:v,title:l(`deviceExecRefresh`),children:(0,S.jsx)(o,{className:v?`des-spin`:``})})]}),w&&(0,S.jsxs)(`div`,{className:`des-alert des-alert-error`,role:`alert`,children:[(0,S.jsx)(p,{className:`des-alert-icon`}),(0,S.jsx)(`div`,{className:`des-alert-text`,children:w}),(0,S.jsx)(`button`,{type:`button`,className:`des-alert-close`,onClick:()=>T(null),children:(0,S.jsx)(m,{})})]}),E&&(0,S.jsxs)(`div`,{className:`des-alert des-alert-success`,role:`status`,children:[(0,S.jsx)(s,{className:`des-alert-icon`}),(0,S.jsx)(`div`,{className:`des-alert-text`,children:E}),(0,S.jsx)(`button`,{type:`button`,className:`des-alert-close`,onClick:()=>D(null),children:(0,S.jsx)(m,{})})]}),(0,S.jsxs)(`div`,{className:`des-stats-row`,children:[(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(r,{className:`des-stat-icon is-running`}),` `,l(`deviceExecRunning`)]}),(0,S.jsxs)(`div`,{className:`des-stat-value`,children:[u?.activeCount??0,` `,(0,S.jsxs)(`span`,{className:`des-stat-sub`,children:[`/ `,R]})]})]}),(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(a,{className:`des-stat-icon is-waiting`}),` `,l(`deviceExecQueued`)]}),(0,S.jsxs)(`div`,{className:`des-stat-value`,children:[u?.waitingCount??0,` `,(0,S.jsxs)(`span`,{className:`des-stat-sub`,children:[`/ `,z]})]})]}),(0,S.jsxs)(`div`,{className:`des-stat-card`,children:[(0,S.jsxs)(`div`,{className:`des-stat-label`,children:[(0,S.jsx)(i,{className:`des-stat-icon is-blocked`}),` `,l(`deviceExecBlocked`)]}),(0,S.jsx)(`div`,{className:`des-stat-value${(u?.blockedCount??0)>0?` is-warn`:``}`,children:u?.blockedCount??0})]})]}),(0,S.jsxs)(`form`,{className:`des-form-card`,onSubmit:e=>void L(e),children:[(0,S.jsx)(`h4`,{className:`des-form-title`,children:l(`deviceExecQuotaTitle`)}),(0,S.jsxs)(`div`,{className:`des-fields-row`,children:[(0,S.jsxs)(`div`,{className:`des-field`,children:[(0,S.jsx)(`label`,{htmlFor:`des-max-active`,className:`des-label`,children:l(`deviceExecMaxActive`)}),(0,S.jsx)(`input`,{id:`des-max-active`,type:`number`,min:1,max:16,className:`des-input`,value:f,onChange:e=>h(parseInt(e.target.value,10)||1),required:!0}),(0,S.jsx)(`span`,{className:`des-hint`,children:l(`deviceExecMaxActiveHint`)})]}),(0,S.jsxs)(`div`,{className:`des-field`,children:[(0,S.jsx)(`label`,{htmlFor:`des-max-waiting`,className:`des-label`,children:l(`deviceExecMaxWaiting`)}),(0,S.jsx)(`input`,{id:`des-max-waiting`,type:`number`,min:0,max:128,className:`des-input`,value:g,onChange:e=>_(parseInt(e.target.value,10)||0),required:!0}),(0,S.jsx)(`span`,{className:`des-hint`,children:l(`deviceExecMaxWaitingHint`)})]})]}),(0,S.jsxs)(`div`,{className:`des-form-actions`,children:[(0,S.jsx)(`button`,{type:`button`,className:`des-btn des-btn-secondary`,disabled:y,onClick:()=>{h(R),_(z)},children:l(`deviceExecReset`)}),(0,S.jsx)(`button`,{type:`submit`,className:`des-btn des-btn-primary`,disabled:y,children:l(y?`deviceExecSaving`:`deviceExecSave`)})]})]}),u&&u.blocked&&u.blocked.length>0&&(0,S.jsxs)(`div`,{className:`des-section des-blocked-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title is-danger`,children:[(0,S.jsx)(p,{}),` `,l(`deviceExecBlockedTitle`)]}),(0,S.jsx)(`p`,{className:`des-section-desc`,children:l(`deviceExecBlockedDesc`)}),(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecReason`)})]})}),(0,S.jsx)(`tbody`,{children:u.blocked.map(e=>(0,S.jsxs)(`tr`,{className:`des-row-blocked`,children:[(0,S.jsxs)(`td`,{className:`des-mono`,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:(0,S.jsx)(`span`,{className:`des-badge is-danger`,children:e.reason})})]},`${e.taskId}-${e.attemptId}`))})]})})]}),(0,S.jsxs)(`div`,{className:`des-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title`,children:[(0,S.jsx)(r,{}),` `,l(`deviceExecActiveTasks`),` (`,u?.running?.length??0,`)`]}),u&&u.running&&u.running.length>0?(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecQueuedTime`)})]})}),(0,S.jsx)(`tbody`,{children:u.running.map(e=>(0,S.jsxs)(`tr`,{children:[(0,S.jsxs)(`td`,{className:`des-mono`,title:e.taskId,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:new Date(e.startedAt).toLocaleTimeString()})]},`${e.taskId}-${e.attemptId}`))})]})}):(0,S.jsx)(`div`,{className:`des-empty`,children:l(`deviceExecNoActive`)})]}),(0,S.jsxs)(`div`,{className:`des-section`,children:[(0,S.jsxs)(`h4`,{className:`des-section-title`,children:[(0,S.jsx)(a,{}),` `,l(`deviceExecWaitingQueue`),` (`,u?.waiting?.length??0,`)`]}),u&&u.waiting&&u.waiting.length>0?(0,S.jsx)(`div`,{className:`des-table-wrap`,children:(0,S.jsxs)(`table`,{className:`des-table`,children:[(0,S.jsx)(`thead`,{children:(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`th`,{children:l(`deviceExecPosition`)}),(0,S.jsx)(`th`,{children:`Task ID`}),(0,S.jsx)(`th`,{children:`Room ID`}),(0,S.jsx)(`th`,{children:`Bot ID`}),(0,S.jsx)(`th`,{children:l(`deviceExecReason`)}),(0,S.jsx)(`th`,{children:l(`deviceExecQueuedTime`)}),(0,S.jsx)(`th`,{children:l(`deviceExecActions`)})]})}),(0,S.jsx)(`tbody`,{children:u.waiting.map(e=>(0,S.jsxs)(`tr`,{children:[(0,S.jsx)(`td`,{children:(0,S.jsxs)(`span`,{className:`des-pos-badge`,children:[`#`,e.queuePosition]})}),(0,S.jsxs)(`td`,{className:`des-mono`,title:e.taskId,children:[e.taskId.slice(0,8),`...`]}),(0,S.jsx)(`td`,{children:e.roomId}),(0,S.jsx)(`td`,{children:e.botId}),(0,S.jsx)(`td`,{children:(0,S.jsxs)(`div`,{className:`des-reason-cell`,children:[(0,S.jsx)(`span`,{className:`des-badge des-badge-${e.blockReason}`,children:oe(e.blockReason)}),(0,S.jsx)(`span`,{className:`des-reason-detail`,children:e.blockReasonDetail})]})}),(0,S.jsx)(`td`,{children:new Date(e.queuedAt).toLocaleTimeString()}),(0,S.jsx)(`td`,{children:(0,S.jsx)(`button`,{type:`button`,className:`des-btn des-btn-danger-sm`,disabled:re===e.taskId,onClick:()=>void ae(e.taskId,e.attemptId),children:re===e.taskId?l(`deviceExecCancelling`):l(`deviceExecCancel`)})})]},`${e.taskId}-${e.attemptId}`))})]})}):(0,S.jsx)(`div`,{className:`des-empty`,children:l(`deviceExecNoWaiting`)})]})]})}var P=class extends Error{status;constructor(e,t){super(t),this.status=e}};function F(e){try{let t=new URL(e);return[`http:`,`https:`].includes(t.protocol)&&!t.username&&!t.password?t.href:null}catch{return null}}function I(){let e=ee(),n=f().startsWith(`zh`),[r,i]=(0,x.useState)(null),[a,s]=(0,x.useState)(!1),[l,u]=(0,x.useState)(!1),[d,p]=(0,x.useState)(!1),[m,h]=(0,x.useState)(!1),g=(0,x.useRef)(0),_=(0,x.useRef)(null);async function v(e,t){if(_.current)return;let n=++g.current,r=new AbortController;_.current=r,s(!0),u(!1);try{let a=await fetch(c(te(`/api/device-desktop/local/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:r.signal,...t?{headers:{"Content-Type":`application/json`},body:JSON.stringify(t)}:{}});if(g.current!==n||r.signal.aborted)return;if(a.status===403){p(!0),i(null);return}if(!a.ok)throw Error(`Request failed`);let o=await a.json();if(g.current!==n||r.signal.aborted)return;if(e===`permissions`){if(typeof o?.screen!=`boolean`||typeof o?.accessibility!=`boolean`)throw Error(`Invalid permission status`);i(e=>e&&{...e,permissions:{supported:!0,screen:o.screen,accessibility:o.accessibility}}),h(!0)}else{if(typeof o?.os!=`string`||typeof o.enabled!=`boolean`||typeof o.environmentEnabled!=`boolean`||typeof o.supported!=`boolean`)throw Error(`Invalid desktop status`);i(o)}}catch{g.current===n&&!r.signal.aborted&&u(!0)}finally{g.current===n&&!r.signal.aborted&&(_.current=null,s(!1))}}return(0,x.useEffect)(()=>(v(`status`),()=>{g.current++,_.current?.abort(),_.current=null}),[]),(0,S.jsxs)(`section`,{className:`dc-card`,"aria-label":n?`本机远程桌面`:`Local remote desktop`,"aria-busy":a,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(t,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:n?`本机远程桌面`:`Local remote desktop`}),(0,S.jsx)(`p`,{children:n?`管理这台设备的远程桌面与系统权限,无需连接设备中心。`:`Manage remote desktop and system permissions on this device, even while the device center is offline.`})]})]}),d?(0,S.jsx)(`p`,{role:`status`,children:n?`请在设备本机打开此页面以管理远程桌面权限。`:`Open this page on the device itself to manage remote desktop permissions.`}):(0,S.jsxs)(S.Fragment,{children:[r&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsxs)(`div`,{className:`dc-permission-row`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{id:`dc-desktop-label`,children:n?`启用远程桌面`:`Enable remote desktop`}),(0,S.jsx)(`p`,{children:r.enabled?n?`已启用`:`Enabled`:n?`未启用`:`Disabled`})]}),(0,S.jsx)(`button`,{type:`button`,className:`dc-toggle${r.enabled?` is-on`:``}`,role:`switch`,"aria-checked":r.enabled,"aria-labelledby":`dc-desktop-label`,disabled:a||r.environmentEnabled||!r.supported&&!r.enabled,onClick:()=>void v(`enable`,{enabled:!r.enabled}),children:(0,S.jsx)(`span`,{className:`dc-toggle-thumb`})})]}),!r.supported&&(0,S.jsx)(`p`,{children:n?`这台设备暂不支持远程桌面。`:`Remote desktop is currently unavailable on this device.`}),r.environmentEnabled&&(0,S.jsx)(`p`,{children:n?`远程桌面由本机服务设置保持启用。`:`Remote desktop is kept enabled by the service settings on this device.`}),r.os===`darwin`&&(0,S.jsxs)(S.Fragment,{children:[(0,S.jsx)(`p`,{children:n?`点击下方按钮发起系统授权,请在这台 Mac 的系统设置中确认。授权后可能需要重启服务。`:`Use the buttons below to request system permission, then confirm in System Settings on this Mac. You may need to restart the service afterward.`}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[(0,S.jsx)(`button`,{type:`button`,className:`dc-button`,disabled:a||r.permissions?.supported===!1,onClick:()=>void v(`permissions`,{kind:`screen`}),children:n?`授予屏幕录制权限`:`Grant screen recording permission`}),(0,S.jsx)(`button`,{type:`button`,className:`dc-button`,disabled:a||r.permissions?.supported===!1,onClick:()=>void v(`permissions`,{kind:`control`}),children:n?`授予远程控制权限`:`Grant remote control permission`})]}),r.permissions?.screen===!0&&(0,S.jsx)(`p`,{children:n?`屏幕录制:已授权`:`Screen recording: granted`}),r.permissions?.accessibility===!0&&(0,S.jsx)(`p`,{children:n?`远程控制:已授权`:`Remote control: granted`}),m&&(0,S.jsx)(`p`,{role:`status`,children:n?`已发起授权请求,请确认系统设置后刷新状态。`:`Permission requested. Confirm in System Settings, then refresh the status.`})]})]}),l&&(0,S.jsx)(`p`,{className:`dc-notice is-error`,role:`alert`,children:n?`无法更新本机远程桌面设置,请重试。`:`Unable to update local remote desktop settings. Please retry.`}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,disabled:a,onClick:()=>void v(`status`),children:[(0,S.jsx)(o,{"aria-hidden":!0}),a?e(`deviceHubWorking`):n?`刷新远程桌面状态`:`Refresh remote desktop status`]})]})]})}function L({pathCompletions:e=[]}){let r=ee(),a=ne(),[f,p]=(0,x.useState)(a.revision?`groups`:`overview`);(0,x.useEffect)(()=>{a.revision&&p(`groups`)},[a.revision]);let[m,g]=(0,x.useState)(null),[_,C]=(0,x.useState)(``),[w,T]=(0,x.useState)(``),[E,D]=(0,x.useState)(null),[O,k]=(0,x.useState)(Date.now()),[A,M]=(0,x.useState)(null),[ie,L]=(0,x.useState)(``),[ae,oe]=(0,x.useState)(``),[R,z]=(0,x.useState)(``),[B,V]=(0,x.useState)(!1),[H,U]=(0,x.useState)(!0),W=(0,x.useRef)(!1),G=(0,x.useRef)(0),K=(0,x.useRef)(new Set),q=(0,x.useRef)(!1),J=(0,x.useRef)(!1),Y=(0,x.useRef)(null),X=(0,x.useRef)(r);X.current=r;async function Z(e,t){let n=new AbortController;K.current.add(n);try{let r=await fetch(c(te(`/api/device-center/${e}`)),{method:t?`POST`:`GET`,credentials:`same-origin`,signal:n.signal,...t?{headers:{"Content-Type":`application/json`},body:JSON.stringify(t)}:{}}),i=await r.json().catch(()=>({}));if(!r.ok)throw new P(r.status,typeof i.error==`string`?i.error:X.current(`deviceHubRequestFailed`,{status:r.status}));return i}finally{K.current.delete(n)}}function Q(e){return W.current&&G.current===e}function se(e){return e instanceof P&&e.status===403?(V(!0),X.current(`deviceHubLocalOnly`)):e instanceof Error?e.message:X.current(`deviceHubNetworkError`)}function ce(e){Y.current=e,g(e),J.current||(T(e.name),C(e.hubUrl??``))}async function le(){let e=G.current;try{let t=await Z(`status`);if(!Q(e)||q.current)return;ce(t),oe(``),V(!1)}catch(t){Q(e)&&oe(se(t))}finally{Q(e)&&U(!1)}}(0,x.useEffect)(()=>{W.current=!0;let e=!1,t,n=async()=>{await le(),!e&&W.current&&(t=setTimeout(n,5e3))};return n(),()=>{e=!0,W.current=!1,G.current++,clearTimeout(t);for(let e of K.current)e.abort()}},[]),(0,x.useEffect)(()=>{if(!E)return;let e=G.current,t=!1,n,r=setInterval(()=>k(Date.now()),1e3),i=async()=>{if(!(t||!Q(e)||Date.now()>=E.expiresAt)){try{let n=await Z(`poll`,{});if(t||!Q(e))return;if(n.paired){let n=await Z(`status`);if(t||!Q(e))return;J.current=!1,ce(n),D(null),L(``),z(``);return}L(``)}catch(n){!t&&Q(e)&&L(`${X.current(`deviceHubPollError`)} ${se(n)}`)}!t&&Q(e)&&(n=setTimeout(i,2e3))}};return n=setTimeout(i,2e3),()=>{t=!0,clearTimeout(n),clearInterval(r)}},[E]);async function ue(){if(q.current)return;let e=F(_.trim());if(!e){L(r(`deviceHubInvalidUrl`));return}if(!w.trim()||w.trim().length>80){L(r(`deviceHubInvalidName`));return}let t=++G.current;q.current=!0,M(`pair`),L(``),z(``),D(null);try{let n=await Z(`pair`,{hubUrl:e,name:w.trim()});if(!Q(t))return;if(!n.code||!Number.isFinite(n.expiresAt)||!F(n.hubUrl))throw Error(r(`deviceHubPairError`));k(Date.now()),D(n)}catch(e){Q(t)&&L(`${r(`deviceHubPairError`)} ${se(e)}`)}finally{Q(t)&&(q.current=!1,M(null))}}async function de(){let e=Y.current;if(!e?.deviceId||q.current)return;let t=++G.current;q.current=!0,M(`permission`),L(``),z(``),ce({...e,allowExecution:!e.allowExecution});try{await Z(`execution`,{allowExecution:!e.allowExecution}),Q(t)&&z(r(`deviceHubSaved`))}catch(n){Q(t)&&(ce(e),L(`${r(`deviceHubPermissionError`)} ${se(n)}`))}finally{Q(t)&&(q.current=!1,M(null))}}async function fe(){let e=Y.current;if(!e?.deviceId||q.current||!window.confirm(r(`deviceHubDisconnectConfirm`,{name:e.name})))return;let t=++G.current;q.current=!0,M(`disconnect`),L(``),z(``);try{if(await Z(`disconnect`,{}),!Q(t))return;J.current=!1,ce({name:e.name,allowExecution:!1,connected:!1}),D(null)}catch(e){Q(t)&&L(`${r(`deviceHubDisconnectError`)} ${se(e)}`)}finally{Q(t)&&(q.current=!1,M(null))}}async function pe(){if(!E)return;let e=G.current;try{await navigator.clipboard.writeText(E.code),Q(e)&&z(r(`deviceHubCopied`))}catch{Q(e)&&L(r(`deviceHubCopyFailed`))}}let $=!!m?.deviceId,me=F($?m?.hubUrl??``:E?.hubUrl??``),he=Math.max(0,Math.ceil(((E?.expiresAt??0)-O)/1e3)),ge=`${Math.floor(he/60)}:${String(he%60).padStart(2,`0`)}`,_e=me?(0,S.jsxs)(re,{href:me,children:[(0,S.jsx)(u,{"aria-hidden":!0}),r(`deviceHubOpenHub`)]}):null;return(0,S.jsxs)(`section`,{className:`dc-page`,"aria-label":r(`deviceHubTitle`),children:[(0,S.jsxs)(`header`,{className:`dc-header`,children:[(0,S.jsxs)(`div`,{className:`dc-heading`,children:[(0,S.jsxs)(`span`,{className:`dc-eyebrow`,children:[(0,S.jsx)(t,{"aria-hidden":!0}),r(`deviceHubTitle`)]}),(0,S.jsx)(`h1`,{children:r(`deviceHubTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubSubtitle`)})]}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[$&&_e,(0,S.jsxs)(`span`,{className:`dc-status ${$?m?.connected?`is-online`:`is-offline`:`is-unpaired`}`,children:[(0,S.jsx)(`span`,{className:`dc-status-dot`}),r($?m?.connected?`deviceHubOnline`:`deviceHubOffline`:`deviceHubUnpaired`)]})]})]}),(0,S.jsxs)(`div`,{className:`dc-layout`,children:[(0,S.jsxs)(`nav`,{className:`dc-sidebar`,"aria-label":r(`deviceHubTitle`),children:[(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`overview`?` is-active`:``}`,"aria-current":f===`overview`?`page`:void 0,onClick:()=>p(`overview`),children:[(0,S.jsx)(t,{"aria-hidden":!0}),r(`deviceHubOverview`)]}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`groups`?` is-active`:``}`,"aria-current":f===`groups`?`page`:void 0,onClick:()=>p(`groups`),children:[(0,S.jsx)(l,{"aria-hidden":!0}),r(`deviceHubGroups`)]}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-nav-item${f===`settings`?` is-active`:``}`,"aria-current":f===`settings`?`page`:void 0,onClick:()=>p(`settings`),children:[(0,S.jsx)(h,{"aria-hidden":!0}),r(`deviceHubSettings`)]})]}),(0,S.jsxs)(`main`,{className:`dc-content`,children:[H&&(0,S.jsxs)(`div`,{className:`dc-loading`,role:`status`,children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubLoading`)]}),(B||ae)&&(0,S.jsxs)(`div`,{className:`dc-notice is-error`,role:`alert`,children:[(0,S.jsx)(`span`,{children:B?r(`deviceHubLocalOnly`):`${r(`deviceHubStatusError`)} ${ae}`}),(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,onClick:()=>void le(),children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubRetry`)]})]}),ie&&!B&&(0,S.jsx)(`div`,{className:`dc-notice is-error`,role:`alert`,children:ie}),R&&(0,S.jsxs)(`div`,{className:`dc-notice is-success`,role:`status`,children:[(0,S.jsx)(s,{"aria-hidden":!0}),R]}),!$&&(m||B)&&f===`overview`&&(0,S.jsx)(j,{initialHubUrl:_,onConnect:e=>{if(B){z(r(`deviceHubLocalOnly`));return}J.current=!0,C(e),p(`settings`)}}),!B&&f===`overview`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsx)(`h3`,{children:r(`deviceSetupNext`)}),(0,S.jsx)(`p`,{children:r(`deviceSetupNextHint`)}),(0,S.jsx)(`button`,{className:`dc-button is-primary`,onClick:()=>p(`groups`),children:r(`deviceSetupManage`)})]}),f===`settings`&&(0,S.jsx)(I,{}),m&&!B&&!$&&f===`settings`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(h,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:r(`deviceHubPairTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubPairHint`)})]})]}),(0,S.jsx)(`ol`,{className:`dc-steps`,children:[`deviceHubStepDetails`,`deviceHubStepApprove`,`deviceHubStepWait`].map((e,t)=>(0,S.jsxs)(`li`,{className:E?t===0?`is-complete`:`is-active`:t===0?`is-active`:``,children:[(0,S.jsx)(`span`,{className:`dc-step-number`,children:E&&t===0?(0,S.jsx)(s,{"aria-hidden":!0}):t+1}),(0,S.jsx)(`span`,{children:r(e)})]},e))}),(0,S.jsxs)(`form`,{className:`dc-form`,onSubmit:e=>{e.preventDefault(),ue()},children:[(0,S.jsxs)(`div`,{className:`dc-field`,children:[(0,S.jsx)(`label`,{htmlFor:`dc-hub-url`,children:r(`deviceHubHubUrl`)}),(0,S.jsx)(`input`,{id:`dc-hub-url`,type:`url`,autoComplete:`url`,required:!0,placeholder:r(`deviceHubHubPlaceholder`),value:_,disabled:!!E||!!A,onChange:e=>{J.current=!0,C(e.target.value)}})]}),(0,S.jsxs)(`div`,{className:`dc-field`,children:[(0,S.jsx)(`label`,{htmlFor:`dc-device-name`,children:r(`deviceHubName`)}),(0,S.jsx)(`input`,{id:`dc-device-name`,maxLength:80,required:!0,autoComplete:`off`,placeholder:r(`deviceHubNamePlaceholder`),value:w,disabled:!!E||!!A,onChange:e=>{J.current=!0,T(e.target.value)}})]}),!E&&(0,S.jsx)(`div`,{className:`dc-actions`,children:(0,S.jsxs)(`button`,{className:`dc-button is-primary`,type:`submit`,disabled:!!A,children:[(0,S.jsx)(h,{"aria-hidden":!0}),r(A?`deviceHubWorking`:`deviceHubGetCode`)]})})]}),E&&(0,S.jsxs)(`div`,{className:`dc-pairing`,children:[(0,S.jsx)(`span`,{className:`dc-code-label`,children:r(`deviceHubCode`)}),(0,S.jsxs)(`div`,{className:`dc-code-row`,children:[(0,S.jsx)(`code`,{className:`dc-code`,children:E.code}),(0,S.jsx)(`button`,{className:`dc-icon-button`,type:`button`,"aria-label":r(`deviceHubCopy`),onClick:()=>void pe(),children:(0,S.jsx)(n,{"aria-hidden":!0})})]}),(0,S.jsx)(`div`,{className:`dc-pair-meta`,children:r(he?`deviceHubExpires`:`deviceHubExpired`,{time:ge})}),(0,S.jsxs)(`div`,{className:`dc-actions`,children:[_e,(0,S.jsxs)(`button`,{type:`button`,className:`dc-button`,disabled:!!A,onClick:()=>{++G.current,D(null),L(``),z(``)},children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubNewCode`)]})]}),he>0&&(0,S.jsxs)(`p`,{className:`dc-progress`,role:`status`,children:[(0,S.jsx)(o,{"aria-hidden":!0}),r(`deviceHubWaiting`)]})]})]}),$&&m&&!B&&(0,S.jsxs)(S.Fragment,{children:[f===`overview`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-device-summary`,children:[(0,S.jsx)(`span`,{className:`dc-device-icon`,children:(0,S.jsx)(t,{"aria-hidden":!0})}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h2`,{children:m.name}),(0,S.jsx)(`p`,{children:r(`deviceHubDeviceTitle`)})]})]}),(0,S.jsxs)(`dl`,{className:`dc-details`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`dt`,{children:r(`deviceHubDeviceId`)}),(0,S.jsx)(`dd`,{children:m.deviceId})]}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`dt`,{children:r(`deviceHubHubUrl`)}),(0,S.jsx)(`dd`,{children:m.hubUrl})]})]})]}),f===`settings`&&(0,S.jsxs)(`section`,{className:`dc-card`,children:[(0,S.jsxs)(`div`,{className:`dc-card-header`,children:[(0,S.jsx)(`span`,{className:`dc-icon`,children:(0,S.jsx)(i,{"aria-hidden":!0})}),(0,S.jsx)(`div`,{children:(0,S.jsx)(`h2`,{children:r(`deviceHubPermissions`)})})]}),(0,S.jsxs)(`div`,{className:`dc-permission-row`,children:[(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{id:`dc-permission-label`,children:r(`deviceHubPermissionLabel`)}),(0,S.jsx)(`p`,{id:`dc-permission-hint`,children:r(`deviceHubPermissionHint`)})]}),(0,S.jsx)(`button`,{type:`button`,className:`dc-toggle${m.allowExecution?` is-on`:``}`,role:`switch`,"aria-checked":m.allowExecution,"aria-labelledby":`dc-permission-label`,"aria-describedby":`dc-permission-hint`,disabled:!!A,onClick:()=>void de(),children:(0,S.jsx)(`span`,{className:`dc-toggle-thumb`})})]}),(0,S.jsx)(`p`,{className:`dc-permission-state`,role:`status`,children:r(A===`permission`?`deviceHubSaving`:m.allowExecution?`deviceHubEnabled`:`deviceHubReadOnly`)})]}),f===`settings`&&(0,S.jsxs)(`details`,{className:`dc-advanced`,children:[(0,S.jsx)(`summary`,{children:r(`deviceHubAdvanced`)}),(0,S.jsxs)(`div`,{className:`dc-danger-zone`,children:[(0,S.jsx)(`h3`,{children:r(`deviceHubDisconnect`)}),(0,S.jsx)(`p`,{children:r(`deviceHubDisconnectHint`)}),(0,S.jsx)(`button`,{type:`button`,className:`dc-button is-danger`,disabled:!!A,onClick:()=>void fe(),children:r(A===`disconnect`?`deviceHubWorking`:`deviceHubDisconnect`)})]})]})]}),$&&m?.hubUrl&&m.deviceId&&!B&&f!==`groups`&&(0,S.jsx)(v,{hubUrl:m.hubUrl,currentDeviceId:m.deviceId},`directory:${m.hubUrl}:${m.deviceId}`),!B&&(f===`settings`||f===`groups`)&&(0,S.jsx)(y,{pathCompletions:e},`bots:${m?.hubUrl??``}:${m?.deviceId??`unpaired`}`),f===`settings`&&(0,S.jsx)(N,{hubUrl:m?.hubUrl,deviceId:m?.deviceId,onNotice:z}),f===`groups`&&(0,S.jsx)(b,{navigation:a}),(0,S.jsxs)(`aside`,{className:`dc-help`,children:[(0,S.jsx)(d,{className:`dc-help-icon`,"aria-hidden":!0}),(0,S.jsxs)(`div`,{children:[(0,S.jsx)(`h3`,{children:r(`deviceHubHelpTitle`)}),(0,S.jsx)(`p`,{children:r(`deviceHubHelpText`)})]})]})]})]})]})}export{L as DeviceCenter};
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{At as t,It as n,Sn as r,Tt as i,Vt as a,an as o,bn as s,hn as c,wn as l}from"./app-globals-DZNa9v8u.js";import{i as u,r as d,t as f}from"./react-CR5VJ85Q.js";import{t as p}from"./shortcut-stack-hhrDyRBn.js";import{_t as ee}from"./index-DHeeHS_l.js";import{r as te,t as ne}from"./group-view-state-CGCzYF8k.js";var m=e(u(),1),h=d(),g=f(),re={zh:{groups:`群聊`,manage:`管理成员`,save:`保存设置`,create:`建群`,name:`群名称`,devices:`选择 Bot(设备仅承载 Bot)`,empty:`还没有群聊,创建一个群开始协作。`,choose:`选择一个群聊`,open:`打开聊天`,online:`在线`,offline:`离线`,cancel:`取消`,loading:`加载中…`,rebind:`设备绑定已失效或已解除,请在设备设置中重新绑定。`,forbidden:`本机访问被拒绝,请检查本机登录状态与访问权限。`,failed:`请求失败`,searchPlaceholder:`搜索群或任务…`,matchedBot:`Bot: `,matchedTask:`任务: `},en:{groups:`Groups`,manage:`Manage members`,save:`Save settings`,create:`Create group`,name:`Group name`,devices:`Select Bots (devices host Bots)`,empty:`No groups yet. Create a group to collaborate.`,choose:`Select a group`,open:`Open chat`,online:`Online`,offline:`Offline`,cancel:`Cancel`,loading:`Loading…`,rebind:`The device binding has expired or was removed. Bind again in device settings.`,forbidden:`Local access denied. Check your local sign-in and access permissions.`,failed:`Request failed`,searchPlaceholder:`Search groups or tasks…`,matchedBot:`Bot: `,matchedTask:`Task: `}},_=class extends Error{status;code;constructor(e,t,n){super(n),this.status=e,this.code=t}};async function v(e,t,n){let i=await fetch(r(l(`/api/device-center/${e}`)),{method:n?`POST`:`GET`,credentials:`same-origin`,signal:t,...n?{headers:{"Content-Type":`application/json`},body:JSON.stringify(n)}:{}}),a=await i.json().catch(()=>({}));if(!i.ok)throw new _(i.status,a.code??``,typeof a.error==`string`?a.error:String(i.status));return a}function y(e){window.dispatchEvent(new CustomEvent(`pi-web-ui:device-group-open`,{detail:{roomId:e}}))}function b({onClose:e}){let{locale:t}=c(),n=re[s(t)?`zh`:`en`];return p(()=>{document.querySelector(`dialog[open]`)||e()}),(0,h.createPortal)((0,g.jsx)(`div`,{className:`modal-backdrop`,onClick:e,children:(0,g.jsx)(`div`,{className:`device-group-create-dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":n.create,onClick:e=>e.stopPropagation(),children:(0,g.jsx)(x,{createOnly:!0,onClose:e})})}),document.body)}function x({active:e=!0,navigation:r,createOnly:l=!1,onClose:u}){let{locale:d}=c(),f=re[s(d)?`zh`:`en`],[p,h]=(0,m.useState)([]),[b,x]=(0,m.useState)([]),[S,ie]=(0,m.useState)(``),[C,w]=(0,m.useState)(null),[T,E]=(0,m.useState)(!0),[ae,D]=(0,m.useState)(``),[oe,O]=(0,m.useState)(!1),[k,A]=(0,m.useState)(l),[j,M]=(0,m.useState)(!1),[N,P]=(0,m.useState)(!1),[F,I]=(0,m.useState)(``),[L,R]=(0,m.useState)([]),[z,B]=(0,m.useState)(``),[V,H]=(0,m.useState)(!1),[se,U]=(0,m.useState)(0),[W,ce]=(0,m.useState)(``),[le,ue]=(0,m.useState)([]),G=(0,m.useRef)(new ne),K=(0,m.useRef)(null),q=(0,m.useRef)(void 0),J=(0,m.useRef)(0),Y=(0,m.useRef)(null),X=(0,m.useRef)(f);X.current=f;function de(e){let t=X.current;if(e instanceof _){if(e.status===409&&/^(REPAIR_REQUIRED|NOT_PAIRED|DEVICE_CHANGED)$/.test(e.code)||e.status===403&&/binding|credential|unpaired|revoked|not_bound|hub_forbidden/i.test(e.code))return t.rebind;if(e.status===403)return t.forbidden}return`${t.failed}: ${e instanceof Error?e.message:String(e)}`}function Z(){Y.current?.abort(),Y.current=null,H(!1)}function Q(e,t=!1){++J.current,Z(),ie(e),w(null),A(!1),M(t),O(!1),B(``),K.current=t?e:null,R([]),E(!0),D(``),U(e=>e+1)}function fe(){++J.current,Z(),A(!0),O(!1),B(``),M(!1),K.current=null,w(null),R([]),I(``),D(``),U(e=>e+1)}(0,m.useEffect)(()=>{let t=++J.current,n=null,r=!1,i=()=>J.current===t;async function a(){if(!e||document.visibilityState===`hidden`||r)return;r=!0;let t=new AbortController;n=t;try{let[e,n,r]=await Promise.all([v(`bots`,t.signal),v(`rooms`,t.signal),j&&S?v(`rooms/${encodeURIComponent(S)}`,t.signal):Promise.resolve(null)]);if(!i()||t.signal.aborted)return;if(h(e.bots),x(n.rooms),w(r),r?.tasks){ue(r.tasks);for(let e of r.tasks)e.status===`completed`?G.current.onTaskCompleted(e.roomId,e.id):e.status===`failed`&&G.current.onTaskFailed(e.roomId,e.id)}r&&K.current===r.room.id&&(K.current=null,R(r.room.botIds),B(r.room.primaryBotId??``)),D(``)}catch(e){i()&&!t.signal.aborted&&D(de(e))}finally{t.abort(),r=!1,i()&&E(!1)}}a();let o=window.setInterval(()=>void a(),3e3),s=()=>{document.visibilityState===`hidden`?n?.abort():a()};return document.addEventListener(`visibilitychange`,s),()=>{++J.current,n?.abort(),clearInterval(o),document.removeEventListener(`visibilitychange`,s)}},[e,S,j,se]),(0,m.useEffect)(()=>()=>Y.current?.abort(),[]),(0,m.useEffect)(()=>{e||Z()},[e]),(0,m.useEffect)(()=>{e&&r&&r.revision!==q.current&&(q.current=r.revision,r.create?fe():r.roomId&&Q(r.roomId,r.manage))},[e,r?.revision]);async function pe(){if(!e||Y.current||(k?!F.trim():!C))return;let t=new AbortController;Y.current=t,H(!0),D(``),O(!1);let n=L.includes(z)?z:``,r=k,i=C?.room.id;try{let{room:e}=await v(r?`rooms`:`rooms/${encodeURIComponent(i)}/members`,t.signal,{...r?{name:F.trim()}:{},botIds:L,...z&&L.includes(z)?{primaryBotId:z}:!r&&C?.room.primaryBotId?{primaryBotId:null}:{}});if(t.signal.aborted||Y.current!==t)return;if((e.primaryBotId??``)!==n)throw Error(s(d)?`中心未保存主 Bot 设置,请更新群组所属的设备中心(Hub)后重试。`:`The Hub did not save the primary Bot setting. Update the group's Hub and retry.`);O(!0),x(t=>[...t.filter(t=>t.id!==e.id),e]),ie(e.id),w({room:e}),A(!1),M(!r),I(``),R(r?[]:e.botIds),B(e.primaryBotId??``),U(e=>e+1),window.dispatchEvent(new CustomEvent(`pi-web-ui:device-groups-changed`)),r&&(y(e.id),l&&u?.())}catch(e){!t.signal.aborted&&Y.current===t&&D(de(e))}finally{Y.current===t&&(Y.current=null,H(!1))}}let $=b.find(e=>e.id===S);return(0,g.jsxs)(`section`,{className:`dg-root`,"aria-label":f.groups,children:[!l&&(0,g.jsxs)(`div`,{className:`dc-card dg-sidebar`,children:[(0,g.jsxs)(`div`,{className:`dg-heading`,children:[(0,g.jsxs)(`h2`,{children:[(0,g.jsx)(o,{}),` `,f.groups]}),(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:fe,children:[(0,g.jsx)(n,{}),` `,f.create]})]}),(0,g.jsx)(`div`,{className:`dg-search-bar`,style:{padding:`0 10px 8px 10px`},children:(0,g.jsxs)(`div`,{style:{display:`flex`,alignItems:`center`,gap:`6px`,background:`var(--bg)`,border:`1px solid var(--border)`,borderRadius:`6px`,padding:`4px 8px`},children:[(0,g.jsx)(a,{style:{color:`var(--text-dim)`,flex:`none`}}),(0,g.jsx)(`input`,{type:`search`,value:W,onChange:e=>ce(e.target.value),placeholder:f.searchPlaceholder,style:{border:`none`,background:`transparent`,color:`var(--text)`,width:`100%`,fontSize:`12px`,outline:`none`}})]})}),(0,g.jsx)(`div`,{className:`dg-rooms`,children:te(W,b,le).map(t=>{let n=b.find(e=>e.id===t.roomId);if(!n)return null;let r=G.current.getPendingInteractionsCount(n.id);return(0,g.jsxs)(`button`,{className:`dc-button dg-room`,type:`button`,"aria-current":n.id===S?`page`:void 0,disabled:!e,onClick:()=>{Q(n.id),y(n.id)},children:[(0,g.jsx)(i,{}),(0,g.jsxs)(`span`,{style:{flex:1,minWidth:0,overflow:`hidden`,textOverflow:`ellipsis`,whiteSpace:`nowrap`,textAlign:`left`},children:[n.name,t.matchedBy===`bot_id`&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--text-dim)`,fontSize:`11px`},children:[f.matchedBot,` `,W]}),t.matchedBy===`task_title`&&t.matchedTask&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--accent)`,fontSize:`11px`},children:[f.matchedTask,` `,t.matchedTask.title]}),t.matchedBy===`task_id`&&t.matchedTask&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--text-dim)`,fontSize:`11px`},children:[`ID: `,t.matchedTask.id.slice(0,8)]})]}),r>0&&(0,g.jsx)(`span`,{style:{flex:`none`,background:`var(--accent)`,color:`var(--accent-contrast, #fff)`,borderRadius:`10px`,padding:`1px 6px`,fontSize:`11px`,fontWeight:`bold`},children:r})]},t.matchedTask?`${n.id}:${t.matchedTask.id}`:n.id)})}),!T&&!b.length&&(0,g.jsx)(`p`,{className:`dg-empty`,children:f.empty})]}),(0,g.jsxs)(`div`,{className:`dc-card dg-chat`,children:[oe&&(0,g.jsx)(`p`,{role:`status`,children:s(d)?`已保存成员和主 Bot 设置。`:`Members and primary Bot settings saved.`}),ae&&(0,g.jsx)(`p`,{className:`dc-error`,role:`alert`,children:ae}),T&&(0,g.jsx)(`p`,{role:`status`,children:f.loading}),k||j?(0,g.jsxs)(`form`,{className:`dg-create`,onSubmit:e=>{e.preventDefault(),pe()},children:[(0,g.jsx)(`h2`,{children:k?f.create:f.manage}),k&&(0,g.jsxs)(`div`,{className:`dc-field`,children:[(0,g.jsx)(`label`,{htmlFor:`device-group-name`,children:f.name}),(0,g.jsx)(`input`,{id:`device-group-name`,value:F,onChange:e=>I(e.target.value),disabled:V||!e})]}),(0,g.jsxs)(`fieldset`,{disabled:V||!e||j&&!C,children:[(0,g.jsx)(`legend`,{children:f.devices}),p.map(e=>(0,g.jsxs)(`label`,{className:`dg-device`,children:[(0,g.jsx)(`input`,{type:`checkbox`,disabled:!e.enabled&&!L.includes(e.id),checked:L.includes(e.id),onChange:t=>R(n=>t.target.checked?[...n,e.id]:n.filter(t=>t!==e.id))}),(0,g.jsx)(t,{}),(0,g.jsxs)(`span`,{children:[e.name,` · `,e.id,` · `,e.deviceId,` · `,e.online?f.online:f.offline]})]},e.id))]}),(0,g.jsxs)(`div`,{className:`dc-field`,children:[(0,g.jsx)(`label`,{htmlFor:`device-group-primary-bot`,children:s(d)?`主 Bot`:`Primary Bot`}),(0,g.jsxs)(`select`,{id:`device-group-primary-bot`,value:L.includes(z)?z:``,disabled:V||!e||j&&!C,onChange:e=>B(e.target.value),children:[(0,g.jsx)(`option`,{value:``,children:s(d)?`不设置(需 @ Bot)`:`None (mention a Bot)`}),p.filter(e=>e.enabled&&L.includes(e.id)).map(e=>(0,g.jsxs)(`option`,{value:e.id,children:[e.name,` · `,e.id]},e.id))]}),(0,g.jsx)(`p`,{children:s(d)?`未 @ Bot 的消息自动发送给主 Bot。`:`Messages without a Bot mention go to the primary Bot.`})]}),(0,g.jsxs)(`div`,{className:`dc-actions`,children:[(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:V||!e||N,onClick:()=>P(!0),children:[(0,g.jsx)(n,{}),` `,s(d)?`新建 Bot`:`Create Bot`]}),(0,g.jsx)(`button`,{className:`dc-button`,type:`submit`,disabled:V||!e||(k?!F.trim():!C),children:k?f.create:f.save}),(0,g.jsx)(`button`,{className:`dc-button`,type:`button`,onClick:()=>{Z(),K.current=null,l&&u?.(),A(!1),M(!1),D(``)},children:f.cancel})]})]}):$?(0,g.jsxs)(g.Fragment,{children:[(0,g.jsx)(`div`,{className:`dg-title`,children:(0,g.jsxs)(`h2`,{children:[(0,g.jsx)(o,{}),` `,$.name]})}),(0,g.jsxs)(`div`,{className:`dc-actions`,children:[(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:()=>y($.id),children:[(0,g.jsx)(i,{}),f.open]}),(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:()=>Q($.id,!0),children:[(0,g.jsx)(o,{}),f.manage]})]})]}):(0,g.jsx)(`p`,{className:`dg-empty`,children:f.choose})]}),N&&(0,g.jsx)(ee,{createOnly:!0,onCancel:()=>P(!1),onCreated:(e,t)=>{P(!1),t&&e.enabled?(h(t=>[...t.filter(t=>t.id!==e.id),e]),R(t=>[...new Set([...t,e.id])])):D(s(d)?`Bot 已保存;请启用并同步到中心后,再加入群组。`:`Bot saved. Enable and sync it to the hub before adding it to the group.`)}})]})}export{b as DeviceGroupCreateDialog,x as DeviceGroups};
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{At as t,It as n,Sn as r,Tt as i,Vt as a,an as o,bn as s,hn as c,wn as l}from"./app-globals-DZNa9v8u.js";import{i as u,r as d,t as f}from"./react-CR5VJ85Q.js";import{t as p}from"./shortcut-stack-hhrDyRBn.js";import{_t as ee}from"./index-DTZch8uz.js";import{r as te,t as ne}from"./group-view-state-CGCzYF8k.js";var m=e(u(),1),h=d(),g=f(),re={zh:{groups:`群聊`,manage:`管理成员`,save:`保存设置`,create:`建群`,name:`群名称`,devices:`选择 Bot(设备仅承载 Bot)`,empty:`还没有群聊,创建一个群开始协作。`,choose:`选择一个群聊`,open:`打开聊天`,online:`在线`,offline:`离线`,cancel:`取消`,loading:`加载中…`,rebind:`设备绑定已失效或已解除,请在设备设置中重新绑定。`,forbidden:`本机访问被拒绝,请检查本机登录状态与访问权限。`,failed:`请求失败`,searchPlaceholder:`搜索群或任务…`,matchedBot:`Bot: `,matchedTask:`任务: `},en:{groups:`Groups`,manage:`Manage members`,save:`Save settings`,create:`Create group`,name:`Group name`,devices:`Select Bots (devices host Bots)`,empty:`No groups yet. Create a group to collaborate.`,choose:`Select a group`,open:`Open chat`,online:`Online`,offline:`Offline`,cancel:`Cancel`,loading:`Loading…`,rebind:`The device binding has expired or was removed. Bind again in device settings.`,forbidden:`Local access denied. Check your local sign-in and access permissions.`,failed:`Request failed`,searchPlaceholder:`Search groups or tasks…`,matchedBot:`Bot: `,matchedTask:`Task: `}},_=class extends Error{status;code;constructor(e,t,n){super(n),this.status=e,this.code=t}};async function v(e,t,n){let i=await fetch(r(l(`/api/device-center/${e}`)),{method:n?`POST`:`GET`,credentials:`same-origin`,signal:t,...n?{headers:{"Content-Type":`application/json`},body:JSON.stringify(n)}:{}}),a=await i.json().catch(()=>({}));if(!i.ok)throw new _(i.status,a.code??``,typeof a.error==`string`?a.error:String(i.status));return a}function y(e){window.dispatchEvent(new CustomEvent(`pi-web-ui:device-group-open`,{detail:{roomId:e}}))}function b({onClose:e}){let{locale:t}=c(),n=re[s(t)?`zh`:`en`];return p(()=>{document.querySelector(`dialog[open]`)||e()}),(0,h.createPortal)((0,g.jsx)(`div`,{className:`modal-backdrop`,onClick:e,children:(0,g.jsx)(`div`,{className:`device-group-create-dialog`,role:`dialog`,"aria-modal":`true`,"aria-label":n.create,onClick:e=>e.stopPropagation(),children:(0,g.jsx)(x,{createOnly:!0,onClose:e})})}),document.body)}function x({active:e=!0,navigation:r,createOnly:l=!1,onClose:u}){let{locale:d}=c(),f=re[s(d)?`zh`:`en`],[p,h]=(0,m.useState)([]),[b,x]=(0,m.useState)([]),[S,ie]=(0,m.useState)(``),[C,w]=(0,m.useState)(null),[T,E]=(0,m.useState)(!0),[ae,D]=(0,m.useState)(``),[oe,O]=(0,m.useState)(!1),[k,A]=(0,m.useState)(l),[j,M]=(0,m.useState)(!1),[N,P]=(0,m.useState)(!1),[F,I]=(0,m.useState)(``),[L,R]=(0,m.useState)([]),[z,B]=(0,m.useState)(``),[V,H]=(0,m.useState)(!1),[se,U]=(0,m.useState)(0),[W,ce]=(0,m.useState)(``),[le,ue]=(0,m.useState)([]),G=(0,m.useRef)(new ne),K=(0,m.useRef)(null),q=(0,m.useRef)(void 0),J=(0,m.useRef)(0),Y=(0,m.useRef)(null),X=(0,m.useRef)(f);X.current=f;function de(e){let t=X.current;if(e instanceof _){if(e.status===409&&/^(REPAIR_REQUIRED|NOT_PAIRED|DEVICE_CHANGED)$/.test(e.code)||e.status===403&&/binding|credential|unpaired|revoked|not_bound|hub_forbidden/i.test(e.code))return t.rebind;if(e.status===403)return t.forbidden}return`${t.failed}: ${e instanceof Error?e.message:String(e)}`}function Z(){Y.current?.abort(),Y.current=null,H(!1)}function Q(e,t=!1){++J.current,Z(),ie(e),w(null),A(!1),M(t),O(!1),B(``),K.current=t?e:null,R([]),E(!0),D(``),U(e=>e+1)}function fe(){++J.current,Z(),A(!0),O(!1),B(``),M(!1),K.current=null,w(null),R([]),I(``),D(``),U(e=>e+1)}(0,m.useEffect)(()=>{let t=++J.current,n=null,r=!1,i=()=>J.current===t;async function a(){if(!e||document.visibilityState===`hidden`||r)return;r=!0;let t=new AbortController;n=t;try{let[e,n,r]=await Promise.all([v(`bots`,t.signal),v(`rooms`,t.signal),j&&S?v(`rooms/${encodeURIComponent(S)}`,t.signal):Promise.resolve(null)]);if(!i()||t.signal.aborted)return;if(h(e.bots),x(n.rooms),w(r),r?.tasks){ue(r.tasks);for(let e of r.tasks)e.status===`completed`?G.current.onTaskCompleted(e.roomId,e.id):e.status===`failed`&&G.current.onTaskFailed(e.roomId,e.id)}r&&K.current===r.room.id&&(K.current=null,R(r.room.botIds),B(r.room.primaryBotId??``)),D(``)}catch(e){i()&&!t.signal.aborted&&D(de(e))}finally{t.abort(),r=!1,i()&&E(!1)}}a();let o=window.setInterval(()=>void a(),3e3),s=()=>{document.visibilityState===`hidden`?n?.abort():a()};return document.addEventListener(`visibilitychange`,s),()=>{++J.current,n?.abort(),clearInterval(o),document.removeEventListener(`visibilitychange`,s)}},[e,S,j,se]),(0,m.useEffect)(()=>()=>Y.current?.abort(),[]),(0,m.useEffect)(()=>{e||Z()},[e]),(0,m.useEffect)(()=>{e&&r&&r.revision!==q.current&&(q.current=r.revision,r.create?fe():r.roomId&&Q(r.roomId,r.manage))},[e,r?.revision]);async function pe(){if(!e||Y.current||(k?!F.trim():!C))return;let t=new AbortController;Y.current=t,H(!0),D(``),O(!1);let n=L.includes(z)?z:``,r=k,i=C?.room.id;try{let{room:e}=await v(r?`rooms`:`rooms/${encodeURIComponent(i)}/members`,t.signal,{...r?{name:F.trim()}:{},botIds:L,...z&&L.includes(z)?{primaryBotId:z}:!r&&C?.room.primaryBotId?{primaryBotId:null}:{}});if(t.signal.aborted||Y.current!==t)return;if((e.primaryBotId??``)!==n)throw Error(s(d)?`中心未保存主 Bot 设置,请更新群组所属的设备中心(Hub)后重试。`:`The Hub did not save the primary Bot setting. Update the group's Hub and retry.`);O(!0),x(t=>[...t.filter(t=>t.id!==e.id),e]),ie(e.id),w({room:e}),A(!1),M(!r),I(``),R(r?[]:e.botIds),B(e.primaryBotId??``),U(e=>e+1),window.dispatchEvent(new CustomEvent(`pi-web-ui:device-groups-changed`)),r&&(y(e.id),l&&u?.())}catch(e){!t.signal.aborted&&Y.current===t&&D(de(e))}finally{Y.current===t&&(Y.current=null,H(!1))}}let $=b.find(e=>e.id===S);return(0,g.jsxs)(`section`,{className:`dg-root`,"aria-label":f.groups,children:[!l&&(0,g.jsxs)(`div`,{className:`dc-card dg-sidebar`,children:[(0,g.jsxs)(`div`,{className:`dg-heading`,children:[(0,g.jsxs)(`h2`,{children:[(0,g.jsx)(o,{}),` `,f.groups]}),(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:fe,children:[(0,g.jsx)(n,{}),` `,f.create]})]}),(0,g.jsx)(`div`,{className:`dg-search-bar`,style:{padding:`0 10px 8px 10px`},children:(0,g.jsxs)(`div`,{style:{display:`flex`,alignItems:`center`,gap:`6px`,background:`var(--bg)`,border:`1px solid var(--border)`,borderRadius:`6px`,padding:`4px 8px`},children:[(0,g.jsx)(a,{style:{color:`var(--text-dim)`,flex:`none`}}),(0,g.jsx)(`input`,{type:`search`,value:W,onChange:e=>ce(e.target.value),placeholder:f.searchPlaceholder,style:{border:`none`,background:`transparent`,color:`var(--text)`,width:`100%`,fontSize:`12px`,outline:`none`}})]})}),(0,g.jsx)(`div`,{className:`dg-rooms`,children:te(W,b,le).map(t=>{let n=b.find(e=>e.id===t.roomId);if(!n)return null;let r=G.current.getPendingInteractionsCount(n.id);return(0,g.jsxs)(`button`,{className:`dc-button dg-room`,type:`button`,"aria-current":n.id===S?`page`:void 0,disabled:!e,onClick:()=>{Q(n.id),y(n.id)},children:[(0,g.jsx)(i,{}),(0,g.jsxs)(`span`,{style:{flex:1,minWidth:0,overflow:`hidden`,textOverflow:`ellipsis`,whiteSpace:`nowrap`,textAlign:`left`},children:[n.name,t.matchedBy===`bot_id`&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--text-dim)`,fontSize:`11px`},children:[f.matchedBot,` `,W]}),t.matchedBy===`task_title`&&t.matchedTask&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--accent)`,fontSize:`11px`},children:[f.matchedTask,` `,t.matchedTask.title]}),t.matchedBy===`task_id`&&t.matchedTask&&(0,g.jsxs)(`small`,{style:{display:`block`,color:`var(--text-dim)`,fontSize:`11px`},children:[`ID: `,t.matchedTask.id.slice(0,8)]})]}),r>0&&(0,g.jsx)(`span`,{style:{flex:`none`,background:`var(--accent)`,color:`var(--accent-contrast, #fff)`,borderRadius:`10px`,padding:`1px 6px`,fontSize:`11px`,fontWeight:`bold`},children:r})]},t.matchedTask?`${n.id}:${t.matchedTask.id}`:n.id)})}),!T&&!b.length&&(0,g.jsx)(`p`,{className:`dg-empty`,children:f.empty})]}),(0,g.jsxs)(`div`,{className:`dc-card dg-chat`,children:[oe&&(0,g.jsx)(`p`,{role:`status`,children:s(d)?`已保存成员和主 Bot 设置。`:`Members and primary Bot settings saved.`}),ae&&(0,g.jsx)(`p`,{className:`dc-error`,role:`alert`,children:ae}),T&&(0,g.jsx)(`p`,{role:`status`,children:f.loading}),k||j?(0,g.jsxs)(`form`,{className:`dg-create`,onSubmit:e=>{e.preventDefault(),pe()},children:[(0,g.jsx)(`h2`,{children:k?f.create:f.manage}),k&&(0,g.jsxs)(`div`,{className:`dc-field`,children:[(0,g.jsx)(`label`,{htmlFor:`device-group-name`,children:f.name}),(0,g.jsx)(`input`,{id:`device-group-name`,value:F,onChange:e=>I(e.target.value),disabled:V||!e})]}),(0,g.jsxs)(`fieldset`,{disabled:V||!e||j&&!C,children:[(0,g.jsx)(`legend`,{children:f.devices}),p.map(e=>(0,g.jsxs)(`label`,{className:`dg-device`,children:[(0,g.jsx)(`input`,{type:`checkbox`,disabled:!e.enabled&&!L.includes(e.id),checked:L.includes(e.id),onChange:t=>R(n=>t.target.checked?[...n,e.id]:n.filter(t=>t!==e.id))}),(0,g.jsx)(t,{}),(0,g.jsxs)(`span`,{children:[e.name,` · `,e.id,` · `,e.deviceId,` · `,e.online?f.online:f.offline]})]},e.id))]}),(0,g.jsxs)(`div`,{className:`dc-field`,children:[(0,g.jsx)(`label`,{htmlFor:`device-group-primary-bot`,children:s(d)?`主 Bot`:`Primary Bot`}),(0,g.jsxs)(`select`,{id:`device-group-primary-bot`,value:L.includes(z)?z:``,disabled:V||!e||j&&!C,onChange:e=>B(e.target.value),children:[(0,g.jsx)(`option`,{value:``,children:s(d)?`不设置(需 @ Bot)`:`None (mention a Bot)`}),p.filter(e=>e.enabled&&L.includes(e.id)).map(e=>(0,g.jsxs)(`option`,{value:e.id,children:[e.name,` · `,e.id]},e.id))]}),(0,g.jsx)(`p`,{children:s(d)?`未 @ Bot 的消息自动发送给主 Bot。`:`Messages without a Bot mention go to the primary Bot.`})]}),(0,g.jsxs)(`div`,{className:`dc-actions`,children:[(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:V||!e||N,onClick:()=>P(!0),children:[(0,g.jsx)(n,{}),` `,s(d)?`新建 Bot`:`Create Bot`]}),(0,g.jsx)(`button`,{className:`dc-button`,type:`submit`,disabled:V||!e||(k?!F.trim():!C),children:k?f.create:f.save}),(0,g.jsx)(`button`,{className:`dc-button`,type:`button`,onClick:()=>{Z(),K.current=null,l&&u?.(),A(!1),M(!1),D(``)},children:f.cancel})]})]}):$?(0,g.jsxs)(g.Fragment,{children:[(0,g.jsx)(`div`,{className:`dg-title`,children:(0,g.jsxs)(`h2`,{children:[(0,g.jsx)(o,{}),` `,$.name]})}),(0,g.jsxs)(`div`,{className:`dc-actions`,children:[(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:()=>y($.id),children:[(0,g.jsx)(i,{}),f.open]}),(0,g.jsxs)(`button`,{className:`dc-button`,type:`button`,disabled:!e,onClick:()=>Q($.id,!0),children:[(0,g.jsx)(o,{}),f.manage]})]})]}):(0,g.jsx)(`p`,{className:`dg-empty`,children:f.choose})]}),N&&(0,g.jsx)(ee,{createOnly:!0,onCancel:()=>P(!1),onCreated:(e,t)=>{P(!1),t&&e.enabled?(h(t=>[...t.filter(t=>t.id!==e.id),e]),R(t=>[...new Set([...t,e.id])])):D(s(d)?`Bot 已保存;请启用并同步到中心后,再加入群组。`:`Bot saved. Enable and sync it to the hub before adding it to the group.`)}})]})}export{b as DeviceGroupCreateDialog,x as DeviceGroups};
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{j as t}from"./app-globals-DZNa9v8u.js";import{i as n,r,t as i}from"./react-CR5VJ85Q.js";import{t as a}from"./shortcut-stack-hhrDyRBn.js";var o=r(),s=e(n(),1);function c(e,t,n={}){let{enabled:r=!0,ignore:i}=n,a=(0,s.useRef)(t);(0,s.useLayoutEffect)(()=>{a.current=t}),(0,s.useEffect)(()=>{if(!r)return;let t=(e,t)=>{if(!t)return!1;let n=`current`in t?t.current:t;return n?n.contains(e):!1},n=n=>{let r=n.target;r instanceof Node&&(e.current?.contains(r)||i&&(Array.isArray(i)?i:[i]).some(e=>t(r,e))||a.current(n))};return document.addEventListener(`mousedown`,n),document.addEventListener(`touchstart`,n),()=>{document.removeEventListener(`mousedown`,n),document.removeEventListener(`touchstart`,n)}},[r,e,i])}var l=i();function u({align:e,triggerLeft:t,triggerRight:n,menuWidth:r,viewportWidth:i,margin:a=8}){if(r<=0||i<=0)return 0;let o=e===`left`?t:n-r,s=Math.max(a,i-a-r);return Math.round(Math.max(a,Math.min(o,s))-o)}function d({triggerRect:e,menuWidth:t,menuHeight:n,viewportWidth:r,viewportHeight:i,direction:a=`down`,align:o=`right`,gap:s=6,margin:c=8}){if(t<=0||r<=0||i<=0)return{left:Math.round(e.left),top:Math.round(e.bottom+s)};let l=Math.max(c,r-c-t),u=Math.max(c,i-c-n),d=c,f=c,p,m=Math.max(0,e.top-s-c),h=Math.max(0,i-e.bottom-s-c);if(a===`right`)d=e.right+s,d+t>r-c&&e.left-t-s>=c&&(d=e.left-t-s),d=Math.max(c,Math.min(d,l)),f=e.top,f+n>i-c&&(f=i-c-n),f=Math.max(c,Math.min(f,u)),p=Math.max(0,i-2*c);else if(a===`left`)d=e.left-t-s,d<c&&e.right+s+t<=r-c&&(d=e.right+s),d=Math.max(c,Math.min(d,l)),f=e.top,f+n>i-c&&(f=i-c-n),f=Math.max(c,Math.min(f,u)),p=Math.max(0,i-2*c);else if(a===`up`){m>=n?(f=e.top-n-s,p=m):h>=n||h>m?(f=e.bottom+s,p=h):(p=m,f=e.top-Math.min(n,m)-s);let r=Math.max(c,i-c-Math.min(n,p));f=Math.max(c,Math.min(f,r));let a=o===`left`?e.left:e.right-t;d=Math.max(c,Math.min(a,l))}else{h>=n?(f=e.bottom+s,p=h):m>=n?(f=e.top-n-s,p=m):m>h?(f=e.top-Math.min(n,m)-s,p=m):(f=e.bottom+s,p=h);let r=Math.max(c,i-c-Math.min(n,p));f=Math.max(c,Math.min(f,r));let a=o===`left`?e.left:e.right-t;d=Math.max(c,Math.min(a,l))}return{left:Math.round(d),top:Math.round(f),...p===void 0?{}:{maxHeight:Math.round(p)}}}function f({trigger:e,open:n,onOpenChange:r,children:i,align:f=`right`,fit:p=!1,direction:m=`down`,portal:h=!1,tip:g,caret:_=!0,menuClassName:v,triggerClassName:y,onContextMenu:b,dataBarItem:x,menuRef:S,menuStyle:C,className:w}){let T=(0,s.useRef)(null),E=(0,s.useRef)(null),[D,O]=(0,s.useState)(0),[k,A]=(0,s.useState)(null),j=e=>{E.current=e,typeof S==`function`?S(e):S&&`current`in S&&(S.current=e)},M=()=>{let e=T.current,t=E.current;if(!e||!t)return;if(window.innerWidth<=768){D!==0&&O(0);return}let n=t.getBoundingClientRect(),r=e.getBoundingClientRect();if(r.width===0&&r.height===0||n.width===0){D!==0&&O(0);return}if(h){let e=d({triggerRect:r,menuWidth:n.width,menuHeight:n.height,viewportWidth:window.innerWidth,viewportHeight:window.innerHeight,direction:m,align:f,gap:6,margin:8});A(t=>t?.left===e.left&&t?.top===e.top&&t?.maxHeight===e.maxHeight?t:e)}else{let e=n.width,t=u({align:f,triggerLeft:r.left,triggerRight:r.right,menuWidth:e,viewportWidth:window.innerWidth,margin:8});t!==D&&O(t)}};(0,s.useLayoutEffect)(()=>{n?M():(D!==0&&O(0),k!==null&&A(null))}),(0,s.useEffect)(()=>{if(n)return window.addEventListener(`resize`,M),h&&window.addEventListener(`scroll`,M,!0),()=>{window.removeEventListener(`resize`,M),h&&window.removeEventListener(`scroll`,M,!0)}},[n,D,h,m,f]),c(T,()=>r(!1),{enabled:n,ignore:E}),a(()=>r(!1),n);let N=typeof window<`u`&&window.innerWidth<=768?{...C,pointerEvents:`auto`}:{...C,position:`fixed`,left:k?`${k.left}px`:`-9999px`,top:k?`${k.top}px`:`-9999px`,maxHeight:k?.maxHeight==null?C?.maxHeight:C?.maxHeight==null?`${k.maxHeight}px`:`min(${k.maxHeight}px, ${C.maxHeight})`,right:`auto`,bottom:`auto`,pointerEvents:`auto`,visibility:k?`visible`:`hidden`},P=k!=null&&T.current?k.top<T.current.getBoundingClientRect().top:m===`up`;return(0,l.jsxs)(`div`,{className:`dropdown ${f} ${p?`fit`:``} ${m===`up`?`dd-up`:``} ${w??``}`.trim(),ref:T,children:[(0,l.jsxs)(`button`,{type:`button`,className:y??`chip`,onClick:()=>r(!n),onContextMenu:b,"aria-expanded":n,"data-tip":g,"data-bar-item":x,children:[e,_&&(0,l.jsx)(t,{className:`dd-caret ${n?`up`:``}`})]}),n&&(h?(0,o.createPortal)((0,l.jsx)(`div`,{className:`dd-portal-wrapper dropdown ${f} ${p?`fit`:``} ${P?`dd-up`:``}`,style:{position:`fixed`,left:0,top:0,width:`100%`,height:`100%`,pointerEvents:`none`,zIndex:9999},children:(0,l.jsx)(`div`,{className:`dd-menu ${v??``}`,ref:j,style:N,onPointerDown:e=>e.stopPropagation(),onMouseDown:e=>e.stopPropagation(),onWheel:e=>e.stopPropagation(),children:i})}),document.body):(0,l.jsx)(`div`,{className:`dd-menu ${v??``}`,ref:j,onPointerDown:e=>e.stopPropagation(),onMouseDown:e=>e.stopPropagation(),onWheel:e=>e.stopPropagation(),style:D===0?C:{...C,transform:C?.transform?`${C.transform} translateX(${D}px)`:`translateX(${D}px)`},children:i}))]})}function p({active:e,disabled:t=!1,title:n,onClick:r,children:i}){return(0,l.jsx)(`button`,{type:`button`,className:`dd-item ${e?`active`:``}`,disabled:t,title:n,onClick:r,children:i})}export{p as n,f as t};
@@ -0,0 +1,2 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{Dt as t,J as n,O as r,Q as i,R as a,Sn as ee,St as te,V as ne,X as re,Z as ie,_n as ae,ln as oe,sn as se,t as ce,un as le,vt as ue,wn as de,yt as fe,zt as pe}from"./app-globals-DZNa9v8u.js";import{i as o,t as s}from"./react-CR5VJ85Q.js";import{K as me,U as he,ct as ge,dt as _e,pt as ve}from"./index-DHeeHS_l.js";var c=e(o(),1),l=`未命名章节`;function u(e){return String(e??``).trim().replace(/[\s\u3000]+/g,`-`).replace(/[^\p{L}\p{N}_-]+/gu,`-`).replace(/-{2,}/g,`-`).replace(/^-+|-+$/g,``).slice(0,80)||`section`}function ye(e){let t=new Map;return e.map(({level:e,label:n})=>{let r=String(n??``).replace(/\s+/g,` `).trim(),i=u(r||l),a=(t.get(i)??0)+1;return t.set(i,a),{level:e,id:a===1?i:`${i}-${a}`,label:r||l}})}function d(e){return e.length>=2}var f=s(),p=5e3,be=`pi-web-ui:preview-toc`;function m({file:e,content:o,onAddLines:s,onAttach:l,onClose:u,uiFilePreviewToolbar:m,onUiAction:xe}){let v=ae(),[y,Se]=(0,c.useState)(null),[b,Ce]=(0,c.useState)(!1),[x,S]=(0,c.useState)(null),[we,Te]=(0,c.useState)(!1),[Ee,De]=(0,c.useState)(!1),[C,w]=(0,c.useState)(!1),[T,E]=(0,c.useState)(``),[D,O]=(0,c.useState)(!0),[k,A]=(0,c.useState)(!0),[j,Oe]=(0,c.useState)(!1),M=(0,c.useRef)(!1),[N,ke]=(0,c.useState)(!0),[P,Ae]=(0,c.useState)(!1),[F,je]=(0,c.useState)(100),I=(0,c.useRef)(0),L=(0,c.useRef)(!1),R=(0,c.useRef)(null),[Me,z]=(0,c.useState)(!1),[Ne,B]=(0,c.useState)(!1),V=(0,c.useRef)(!1),H=(0,c.useRef)(null);(0,c.useEffect)(()=>()=>{R.current&&clearTimeout(R.current),H.current&&clearTimeout(H.current)},[]),(0,c.useEffect)(()=>{Ce(!0),Se(null),S(null),w(!1),E(``),O(!0),A(!0),Oe(!1),M.current=!1,V.current=!1,z(!1),B(!1),H.current&&clearTimeout(H.current),ce({type:`read_file`,path:e.path})},[e.path]),(0,c.useEffect)(()=>{o&&o.path===e.path&&(Se(o),C||E(o.text),Ce(!1),V.current&&(V.current=!1,H.current&&clearTimeout(H.current),z(!1),B(!1),w(!1),E(o.text),M.current&&(h(e.name)&&O(!0),_(e.name)&&A(!0)),S(null)))},[o,C,e.path]),(0,c.useEffect)(()=>{let e=e=>{if(e.key===`Escape`){G();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()===`s`&&C){e.preventDefault(),Ve();return}let t=e.target,n=t&&(t.tagName===`INPUT`||t.tagName===`TEXTAREA`||t.isContentEditable);(e.ctrlKey||e.metaKey)&&e.key.toLowerCase()===`a`&&!n&&(e.preventDefault(),Le())};return document.addEventListener(`keydown`,e),()=>document.removeEventListener(`keydown`,e)},[u,y,C,T]),(0,c.useEffect)(()=>{let e=()=>{L.current=!1,Te(!1)};return window.addEventListener(`mouseup`,e),()=>window.removeEventListener(`mouseup`,e)},[]);let U=(0,c.useMemo)(()=>{if(!y)return[];let e=y.text.split(`
2
+ `);return e.length>0&&e[e.length-1]===``&&e.pop(),e.slice(0,p)},[y]),Pe=y?.lines??0,Fe=Pe>p,Ie=(e,t)=>{if(t){let t=I.current>0?I.current:e;S({start:Math.min(t,e),end:Math.max(t,e)})}else I.current=e,S({start:e,end:e})},Le=()=>{U.length!==0&&(I.current=1,S({start:1,end:U.length}))},Re=()=>{x&&(s(e.path,e.name,x.start,x.end),De(!0),R.current&&clearTimeout(R.current),R.current=setTimeout(()=>De(!1),1400))},W=y!==null&&y.kind===`text`&&!y.binary&&!y.truncated&&!g(e.name),ze=()=>{V.current=!1,H.current&&clearTimeout(H.current),z(!1),B(!1),E(y?.text??``),w(!1),M.current&&(h(e.name)&&O(!0),_(e.name)&&A(!0))},Be=()=>{if(C){if(T!==(y?.text??``)&&!window.confirm(v(`discardFileChanges`)))return;ze();return}W&&y&&(M.current=h(e.name)&&D||_(e.name)&&k,h(e.name)&&O(!1),_(e.name)&&A(!1),S(null),E(y.text),w(!0))},Ve=()=>{C&&y&&W&&ce({type:`write_file`,path:e.path,text:T})&&(V.current=!0,B(!1),z(!0),H.current&&clearTimeout(H.current),H.current=setTimeout(()=>{V.current=!1,z(!1),B(!0)},5e3))},G=()=>{(!C||T===(y?.text??``)||window.confirm(v(`discardFileChanges`)))&&u()},He=e=>{je(Math.min(200,Math.max(50,e)))},Ue=x?x.end-x.start+1:0,K=y?.binary??!1,We=y?.truncated??!1,q=y?.kind??`text`,Ge=h(e.name)||g(e.name),Ke=_(e.name),qe=g(e.name),J=Ge&&D&&!C&&q===`text`&&!K,Y=Ke&&k&&!C&&q===`text`&&!K,[X,Je]=(0,c.useState)(()=>{try{return localStorage.getItem(be)===`1`}catch{return!1}});(0,c.useEffect)(()=>{try{localStorage.setItem(be,X?`1`:`0`)}catch{}},[X]);let[Z,Ye]=(0,c.useState)([]),[Xe,Ze]=(0,c.useState)(``),Q=(0,c.useRef)(null);(0,c.useEffect)(()=>{let e=Q.current;if(!e||!J){Ye([]);return}let t=[...e.querySelectorAll(`h1, h2, h3, h4, h5, h6`)],n=ye(t.map(e=>({level:Number(e.tagName.slice(1)),label:e.textContent??``})));t.forEach((e,t)=>{e.id||=n[t].id}),Ye(n)},[J,y?.text,e.path]),(0,c.useEffect)(()=>{let e=Q.current;if(!e||!X||!d(Z))return;let t=Z.map(t=>e.querySelector(`#${CSS.escape(t.id)}`)).filter(e=>e!==null);if(t.length===0)return;let n=new IntersectionObserver(e=>{let t=e.filter(e=>e.isIntersecting).sort((e,t)=>e.boundingClientRect.top-t.boundingClientRect.top)[0];t?.target.id&&Ze(t.target.id)},{rootMargin:`0px 0px -70% 0px`});for(let e of t)n.observe(e);return()=>n.disconnect()},[Z,X]);let Qe={"host:fp-md":Ge&&q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach markdown ${D?`on`:``}`,"data-tip":v(D?`showMarkdownSource`:`showMarkdownPreview`),disabled:C,onClick:()=>O(e=>!e),children:D?(0,f.jsx)(i,{}):(0,f.jsx)(a,{})}):null,"host:fp-toc":J&&d(Z)?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach toc ${X?`on`:``}`,"data-tip":v(X?`previewTocHide`:`previewTocShow`),onClick:()=>Je(e=>!e),children:(0,f.jsx)(fe,{})}):null,"host:fp-html":Ke&&q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach html ${k?`on`:``}`,"data-tip":v(k?`showHtmlSource`:`showHtmlPreview`),disabled:C,onClick:()=>A(e=>!e),children:k?(0,f.jsx)(a,{}):(0,f.jsx)(i,{})}):null,"host:fp-edit":q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach edit ${C?`on`:``}`,"data-tip":v(We?`fileEditTruncated`:C?`exitEditFile`:`editFile`),disabled:!W&&!C,onClick:Be,children:(0,f.jsx)(re,{})}):null,"host:fp-wrap":q===`text`&&!K&&!J&&!Y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach wrap ${N?`on`:``}`,"data-tip":v(N?`disableWrap`:`enableWrap`),onClick:()=>ke(e=>!e),children:(0,f.jsx)(ne,{})}):null,"host:fp-zoom":q===`text`&&y?(0,f.jsxs)(`span`,{className:`fp-zoom`,children:[(0,f.jsx)(`button`,{type:`button`,className:`fp-attach zoom-out`,"data-tip":v(`zoomOut`),disabled:F<=50,onClick:()=>He(F-10),children:(0,f.jsx)(le,{})}),(0,f.jsxs)(`button`,{type:`button`,className:`fp-zoom-val`,title:v(`resetZoom`),onClick:()=>je(100),children:[F,`%`]}),(0,f.jsx)(`button`,{type:`button`,className:`fp-attach zoom-in`,"data-tip":v(`zoomIn`),disabled:F>=200,onClick:()=>He(F+10),children:(0,f.jsx)(oe,{})})]}):null,"host:fp-download":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach download`,"data-tip":v(`downloadFile`),onClick:()=>void _e(e.path,e.name),children:(0,f.jsx)(n,{})}),"host:fp-ref":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach ref`,"data-tip":v(`referenceTip`),onClick:()=>l(e.path,e.name,`reference`),children:(0,f.jsx)(ue,{})}),"host:fp-full":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach full ${P?`on`:``}`,"data-tip":v(P?`exitFullscreen`:`fullscreen`),onClick:()=>Ae(e=>!e),children:P?(0,f.jsx)(t,{}):(0,f.jsx)(te,{})}),"host:fp-close":null},$e=m===void 0?[`host:fp-md`,`host:fp-toc`,`host:fp-html`,`host:fp-edit`,`host:fp-wrap`,`host:fp-zoom`,`host:fp-download`,`host:fp-ref`,`host:fp-full`].map(e=>({id:e,source:`host`})):m.filter(e=>!e.hidden&&e.id!==`host:fp-close`),$=e=>ee(de(`/api/file?clientId=${encodeURIComponent(me())}&path=${encodeURIComponent(e)}`)),et=e=>{let t=e.split(`/`).map(e=>encodeURIComponent(e)).join(`/`),n=/^[A-Za-z]:([\\/]|$)/.test(e)||e.startsWith(`/`)?`/api/preview/__abs__/${t}`:`/api/preview/${t}`;return ee(de(`${n}?clientId=${encodeURIComponent(me())}${j?`&allowJs=1`:``}`))};return(0,f.jsx)(`div`,{className:`fp-overlay ${P?`fullscreen`:``}`,onMouseDown:e=>{e.target===e.currentTarget&&G()},children:(0,f.jsxs)(`div`,{className:`fp ${P?`fullscreen`:``} ${qe?`is-ppt`:``}`,style:{"--fp-zoom":F/100},children:[(0,f.jsxs)(`div`,{className:`fp-head`,onDoubleClick:e=>{let t=e.target;t&&!t.closest(`button, a, input`)&&Ae(e=>!e)},children:[(0,f.jsx)(`span`,{className:`fp-name`,title:e.path,children:e.name}),(0,f.jsx)(`span`,{className:`fp-path`,children:e.path}),(0,f.jsxs)(`span`,{className:`fp-meta`,children:[y&&q===`text`&&!K&&v(`fileLines`,{n:Pe}),y&&` · ${ge(y.size)}`]}),(0,f.jsx)(`span`,{className:`fp-head-actions`,children:he($e,Qe,xe)}),(0,f.jsx)(`button`,{type:`button`,className:`fp-close`,title:v(`close`),"aria-label":v(`close`),onClick:G,children:(0,f.jsx)(se,{})})]}),We&&q===`text`&&!K&&(0,f.jsx)(`div`,{className:`fp-notice`,children:g(e.name)?v(`previewLinesTruncated`,{n:U.length}):v(`previewTruncated`)}),b&&!y&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`loading`)}),!b&&q===`none`&&!K&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`previewNotSupported`)}),!b&&q===`image`&&(0,f.jsx)(`div`,{className:`fp-media-wrap clickable`,onClick:G,title:v(`clickToCloseImage`),children:(0,f.jsx)(`img`,{className:`fp-media fp-media-clickable`,src:$(e.path),alt:e.name,onClick:G,title:v(`clickToCloseImage`)})}),!b&&q===`video`&&(0,f.jsx)(`div`,{className:`fp-media-wrap`,children:(0,f.jsx)(`video`,{className:`fp-media`,src:$(e.path),controls:!0,preload:`metadata`})}),!b&&q===`audio`&&(0,f.jsx)(`div`,{className:`fp-media-wrap fp-audio-wrap`,children:(0,f.jsxs)(`div`,{className:`fp-audio-card`,children:[(0,f.jsx)(`div`,{className:`fp-audio-icon`,"aria-hidden":`true`,children:`🎵`}),(0,f.jsxs)(`div`,{className:`fp-audio-info`,children:[(0,f.jsx)(`div`,{className:`fp-audio-name`,title:e.name,children:e.name}),y?.size?(0,f.jsx)(`div`,{className:`fp-audio-size`,children:ge(y.size)}):null]}),(0,f.jsx)(`audio`,{className:`fp-audio-element`,src:$(e.path),controls:!0,autoPlay:!0,preload:`metadata`})]})}),!b&&Y&&y&&(0,f.jsxs)(`div`,{className:`fp-html`,children:[(0,f.jsxs)(`div`,{className:`fp-html-bar`,children:[(0,f.jsx)(`span`,{className:`fp-html-hint`,title:v(j?`htmlJsOnTip`:`htmlJsOffTip`),children:v(j?`htmlJsOn`:`htmlJsOff`)}),(0,f.jsx)(`button`,{type:`button`,className:`fp-html-js ${j?`on`:``}`,"data-tip":v(j?`htmlDisableJs`:`htmlEnableJs`),onClick:()=>Oe(e=>!e),children:v(j?`htmlDisableJs`:`htmlEnableJs`)}),(0,f.jsxs)(`a`,{className:`fp-html-btn fp-html-external`,href:et(e.path),target:`_blank`,rel:`noopener noreferrer`,title:v(`openInNewTab`),children:[(0,f.jsx)(ie,{}),(0,f.jsx)(`span`,{children:v(`openInNewTab`)})]})]}),(0,f.jsx)(`iframe`,{className:`fp-html-frame`,src:et(e.path),title:e.name,sandbox:j?`allow-scripts`:``,referrerPolicy:`no-referrer`},j?`js`:`nojs`)]}),!b&&J&&y&&(0,f.jsx)(`div`,{className:g(e.name)?`fp-markdown msg-text fp-office`:`fp-markdown msg-text`,children:(0,f.jsxs)(`div`,{className:X&&d(Z)?`fp-toc-layout`:void 0,children:[X&&d(Z)&&(0,f.jsx)(`nav`,{className:`fp-toc`,"aria-label":v(`previewTocLabel`),children:Z.map(e=>(0,f.jsx)(`a`,{className:`l${Math.min(e.level,6)}${e.id===Xe?` on`:``}`,href:`#${e.id}`,title:e.label,onClick:t=>{t.preventDefault(),Q.current?.querySelector(`#${CSS.escape(e.id)}`)?.scrollIntoView({behavior:`smooth`,block:`start`})},children:e.label},e.id))}),(0,f.jsx)(`div`,{className:`fp-markdown-zoom`,ref:Q,children:(0,f.jsx)(ve,{text:y.text})})]})}),!b&&K&&q!==`image`&&q!==`video`&&y&&(0,f.jsxs)(`div`,{className:`fp-hex-wrap`,children:[(0,f.jsxs)(`div`,{className:`fp-notice`,children:[v(`binaryFile`),y.truncated&&v(`binaryHexTruncated`)]}),(0,f.jsx)(`pre`,{className:`fp-hex`,children:y.text})]}),!b&&C&&q===`text`&&!K&&y&&(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`textarea`,{className:`fp-editor ${N?``:`no-wrap`}`,value:T,onChange:e=>E(e.target.value),wrap:N?`soft`:`off`,spellCheck:!1,autoFocus:!0}),Ne&&(0,f.jsx)(`div`,{className:`fp-notice`,role:`alert`,children:v(`saveResultUnknown`)})]}),!b&&!J&&!Y&&!C&&q===`text`&&!K&&y&&U.length===0&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`emptyFile`)}),!b&&!J&&!Y&&!C&&q===`text`&&!K&&U.length>0&&(0,f.jsxs)(`div`,{className:`fp-code ${we?`dragging`:``} ${N?``:`no-wrap`}`,onMouseDown:e=>{e.button===0&&e.preventDefault()},children:[U.map((e,t)=>{let n=t+1,r=x!==null&&n>=x.start&&n<=x.end;return(0,f.jsxs)(`div`,{className:`fp-line ${r?`sel`:``}`,onMouseDown:e=>{e.button===0&&(Ie(n,e.shiftKey),L.current=!0,Te(!0))},onMouseEnter:()=>{L.current&&Ie(n,!0)},children:[(0,f.jsx)(`span`,{className:`fp-num`,children:n}),(0,f.jsx)(`span`,{className:`fp-code-text`,children:e})]},n)}),Fe&&(0,f.jsx)(`div`,{className:`fp-lines-note`,children:v(`previewLinesTruncated`,{n:p})})]}),(C||!J&&!Y&&q===`text`)&&(0,f.jsx)(`div`,{className:`fp-foot`,children:C?(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`span`,{className:`fp-hint`,children:v(`editFile`)}),(0,f.jsxs)(`div`,{className:`fp-actions`,children:[(0,f.jsx)(`button`,{type:`button`,className:`btn`,onClick:Be,children:v(`cancel`)}),(0,f.jsxs)(`button`,{type:`button`,className:`btn primary`,disabled:Me||T===(y?.text??``),onClick:Ve,children:[(0,f.jsx)(pe,{}),` `,v(`saveFile`)]})]})]}):(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`span`,{className:`fp-hint`,children:x?v(`selectedRange`,{n:Ue,start:x.start,end:x.end}):v(`selectLinesHint`)}),(0,f.jsxs)(`div`,{className:`fp-actions`,children:[(0,f.jsx)(`button`,{type:`button`,className:`btn`,disabled:U.length===0,onClick:Le,children:v(`selectAll`)}),(0,f.jsx)(`button`,{type:`button`,className:`btn`,disabled:!x,onClick:()=>S(null),children:v(`clearSelection`)}),(0,f.jsxs)(`button`,{type:`button`,className:`btn primary`,disabled:!x||K,onClick:Re,children:[Ee?(0,f.jsx)(r,{}):null,v(Ee?`addedToChat`:`addToChat`)]})]})]})})]})})}function h(e){let t=e.toLowerCase();return t.endsWith(`.md`)||t.endsWith(`.markdown`)}function g(e){let t=e.toLowerCase();return t.endsWith(`.docx`)||t.endsWith(`.xlsx`)||t.endsWith(`.xlsm`)||t.endsWith(`.pptx`)||t.endsWith(`.pptm`)||t.endsWith(`.ppsx`)||t.endsWith(`.potx`)}function _(e){let t=e.toLowerCase();return t.endsWith(`.html`)||t.endsWith(`.htm`)||t.endsWith(`.xhtml`)}export{m as FilePreview};
@@ -0,0 +1,2 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{Dt as t,J as n,O as r,Q as i,R as a,Sn as ee,St as te,V as ne,X as re,Z as ie,_n as ae,ln as oe,sn as se,t as ce,un as le,vt as ue,wn as de,yt as fe,zt as pe}from"./app-globals-DZNa9v8u.js";import{i as o,t as s}from"./react-CR5VJ85Q.js";import{K as me,U as he,ct as ge,dt as _e,pt as ve}from"./index-DTZch8uz.js";var c=e(o(),1),l=`未命名章节`;function u(e){return String(e??``).trim().replace(/[\s\u3000]+/g,`-`).replace(/[^\p{L}\p{N}_-]+/gu,`-`).replace(/-{2,}/g,`-`).replace(/^-+|-+$/g,``).slice(0,80)||`section`}function ye(e){let t=new Map;return e.map(({level:e,label:n})=>{let r=String(n??``).replace(/\s+/g,` `).trim(),i=u(r||l),a=(t.get(i)??0)+1;return t.set(i,a),{level:e,id:a===1?i:`${i}-${a}`,label:r||l}})}function d(e){return e.length>=2}var f=s(),p=5e3,be=`pi-web-ui:preview-toc`;function m({file:e,content:o,onAddLines:s,onAttach:l,onClose:u,uiFilePreviewToolbar:m,onUiAction:xe}){let v=ae(),[y,Se]=(0,c.useState)(null),[b,Ce]=(0,c.useState)(!1),[x,S]=(0,c.useState)(null),[we,Te]=(0,c.useState)(!1),[Ee,De]=(0,c.useState)(!1),[C,w]=(0,c.useState)(!1),[T,E]=(0,c.useState)(``),[D,O]=(0,c.useState)(!0),[k,A]=(0,c.useState)(!0),[j,Oe]=(0,c.useState)(!1),M=(0,c.useRef)(!1),[N,ke]=(0,c.useState)(!0),[P,Ae]=(0,c.useState)(!1),[F,je]=(0,c.useState)(100),I=(0,c.useRef)(0),L=(0,c.useRef)(!1),R=(0,c.useRef)(null),[Me,z]=(0,c.useState)(!1),[Ne,B]=(0,c.useState)(!1),V=(0,c.useRef)(!1),H=(0,c.useRef)(null);(0,c.useEffect)(()=>()=>{R.current&&clearTimeout(R.current),H.current&&clearTimeout(H.current)},[]),(0,c.useEffect)(()=>{Ce(!0),Se(null),S(null),w(!1),E(``),O(!0),A(!0),Oe(!1),M.current=!1,V.current=!1,z(!1),B(!1),H.current&&clearTimeout(H.current),ce({type:`read_file`,path:e.path})},[e.path]),(0,c.useEffect)(()=>{o&&o.path===e.path&&(Se(o),C||E(o.text),Ce(!1),V.current&&(V.current=!1,H.current&&clearTimeout(H.current),z(!1),B(!1),w(!1),E(o.text),M.current&&(h(e.name)&&O(!0),_(e.name)&&A(!0)),S(null)))},[o,C,e.path]),(0,c.useEffect)(()=>{let e=e=>{if(e.key===`Escape`){G();return}if((e.ctrlKey||e.metaKey)&&e.key.toLowerCase()===`s`&&C){e.preventDefault(),Ve();return}let t=e.target,n=t&&(t.tagName===`INPUT`||t.tagName===`TEXTAREA`||t.isContentEditable);(e.ctrlKey||e.metaKey)&&e.key.toLowerCase()===`a`&&!n&&(e.preventDefault(),Le())};return document.addEventListener(`keydown`,e),()=>document.removeEventListener(`keydown`,e)},[u,y,C,T]),(0,c.useEffect)(()=>{let e=()=>{L.current=!1,Te(!1)};return window.addEventListener(`mouseup`,e),()=>window.removeEventListener(`mouseup`,e)},[]);let U=(0,c.useMemo)(()=>{if(!y)return[];let e=y.text.split(`
2
+ `);return e.length>0&&e[e.length-1]===``&&e.pop(),e.slice(0,p)},[y]),Pe=y?.lines??0,Fe=Pe>p,Ie=(e,t)=>{if(t){let t=I.current>0?I.current:e;S({start:Math.min(t,e),end:Math.max(t,e)})}else I.current=e,S({start:e,end:e})},Le=()=>{U.length!==0&&(I.current=1,S({start:1,end:U.length}))},Re=()=>{x&&(s(e.path,e.name,x.start,x.end),De(!0),R.current&&clearTimeout(R.current),R.current=setTimeout(()=>De(!1),1400))},W=y!==null&&y.kind===`text`&&!y.binary&&!y.truncated&&!g(e.name),ze=()=>{V.current=!1,H.current&&clearTimeout(H.current),z(!1),B(!1),E(y?.text??``),w(!1),M.current&&(h(e.name)&&O(!0),_(e.name)&&A(!0))},Be=()=>{if(C){if(T!==(y?.text??``)&&!window.confirm(v(`discardFileChanges`)))return;ze();return}W&&y&&(M.current=h(e.name)&&D||_(e.name)&&k,h(e.name)&&O(!1),_(e.name)&&A(!1),S(null),E(y.text),w(!0))},Ve=()=>{C&&y&&W&&ce({type:`write_file`,path:e.path,text:T})&&(V.current=!0,B(!1),z(!0),H.current&&clearTimeout(H.current),H.current=setTimeout(()=>{V.current=!1,z(!1),B(!0)},5e3))},G=()=>{(!C||T===(y?.text??``)||window.confirm(v(`discardFileChanges`)))&&u()},He=e=>{je(Math.min(200,Math.max(50,e)))},Ue=x?x.end-x.start+1:0,K=y?.binary??!1,We=y?.truncated??!1,q=y?.kind??`text`,Ge=h(e.name)||g(e.name),Ke=_(e.name),qe=g(e.name),J=Ge&&D&&!C&&q===`text`&&!K,Y=Ke&&k&&!C&&q===`text`&&!K,[X,Je]=(0,c.useState)(()=>{try{return localStorage.getItem(be)===`1`}catch{return!1}});(0,c.useEffect)(()=>{try{localStorage.setItem(be,X?`1`:`0`)}catch{}},[X]);let[Z,Ye]=(0,c.useState)([]),[Xe,Ze]=(0,c.useState)(``),Q=(0,c.useRef)(null);(0,c.useEffect)(()=>{let e=Q.current;if(!e||!J){Ye([]);return}let t=[...e.querySelectorAll(`h1, h2, h3, h4, h5, h6`)],n=ye(t.map(e=>({level:Number(e.tagName.slice(1)),label:e.textContent??``})));t.forEach((e,t)=>{e.id||=n[t].id}),Ye(n)},[J,y?.text,e.path]),(0,c.useEffect)(()=>{let e=Q.current;if(!e||!X||!d(Z))return;let t=Z.map(t=>e.querySelector(`#${CSS.escape(t.id)}`)).filter(e=>e!==null);if(t.length===0)return;let n=new IntersectionObserver(e=>{let t=e.filter(e=>e.isIntersecting).sort((e,t)=>e.boundingClientRect.top-t.boundingClientRect.top)[0];t?.target.id&&Ze(t.target.id)},{rootMargin:`0px 0px -70% 0px`});for(let e of t)n.observe(e);return()=>n.disconnect()},[Z,X]);let Qe={"host:fp-md":Ge&&q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach markdown ${D?`on`:``}`,"data-tip":v(D?`showMarkdownSource`:`showMarkdownPreview`),disabled:C,onClick:()=>O(e=>!e),children:D?(0,f.jsx)(i,{}):(0,f.jsx)(a,{})}):null,"host:fp-toc":J&&d(Z)?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach toc ${X?`on`:``}`,"data-tip":v(X?`previewTocHide`:`previewTocShow`),onClick:()=>Je(e=>!e),children:(0,f.jsx)(fe,{})}):null,"host:fp-html":Ke&&q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach html ${k?`on`:``}`,"data-tip":v(k?`showHtmlSource`:`showHtmlPreview`),disabled:C,onClick:()=>A(e=>!e),children:k?(0,f.jsx)(a,{}):(0,f.jsx)(i,{})}):null,"host:fp-edit":q===`text`&&!K&&y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach edit ${C?`on`:``}`,"data-tip":v(We?`fileEditTruncated`:C?`exitEditFile`:`editFile`),disabled:!W&&!C,onClick:Be,children:(0,f.jsx)(re,{})}):null,"host:fp-wrap":q===`text`&&!K&&!J&&!Y?(0,f.jsx)(`button`,{type:`button`,className:`fp-attach wrap ${N?`on`:``}`,"data-tip":v(N?`disableWrap`:`enableWrap`),onClick:()=>ke(e=>!e),children:(0,f.jsx)(ne,{})}):null,"host:fp-zoom":q===`text`&&y?(0,f.jsxs)(`span`,{className:`fp-zoom`,children:[(0,f.jsx)(`button`,{type:`button`,className:`fp-attach zoom-out`,"data-tip":v(`zoomOut`),disabled:F<=50,onClick:()=>He(F-10),children:(0,f.jsx)(le,{})}),(0,f.jsxs)(`button`,{type:`button`,className:`fp-zoom-val`,title:v(`resetZoom`),onClick:()=>je(100),children:[F,`%`]}),(0,f.jsx)(`button`,{type:`button`,className:`fp-attach zoom-in`,"data-tip":v(`zoomIn`),disabled:F>=200,onClick:()=>He(F+10),children:(0,f.jsx)(oe,{})})]}):null,"host:fp-download":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach download`,"data-tip":v(`downloadFile`),onClick:()=>void _e(e.path,e.name),children:(0,f.jsx)(n,{})}),"host:fp-ref":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach ref`,"data-tip":v(`referenceTip`),onClick:()=>l(e.path,e.name,`reference`),children:(0,f.jsx)(ue,{})}),"host:fp-full":(0,f.jsx)(`button`,{type:`button`,className:`fp-attach full ${P?`on`:``}`,"data-tip":v(P?`exitFullscreen`:`fullscreen`),onClick:()=>Ae(e=>!e),children:P?(0,f.jsx)(t,{}):(0,f.jsx)(te,{})}),"host:fp-close":null},$e=m===void 0?[`host:fp-md`,`host:fp-toc`,`host:fp-html`,`host:fp-edit`,`host:fp-wrap`,`host:fp-zoom`,`host:fp-download`,`host:fp-ref`,`host:fp-full`].map(e=>({id:e,source:`host`})):m.filter(e=>!e.hidden&&e.id!==`host:fp-close`),$=e=>ee(de(`/api/file?clientId=${encodeURIComponent(me())}&path=${encodeURIComponent(e)}`)),et=e=>{let t=e.split(`/`).map(e=>encodeURIComponent(e)).join(`/`),n=/^[A-Za-z]:([\\/]|$)/.test(e)||e.startsWith(`/`)?`/api/preview/__abs__/${t}`:`/api/preview/${t}`;return ee(de(`${n}?clientId=${encodeURIComponent(me())}${j?`&allowJs=1`:``}`))};return(0,f.jsx)(`div`,{className:`fp-overlay ${P?`fullscreen`:``}`,onMouseDown:e=>{e.target===e.currentTarget&&G()},children:(0,f.jsxs)(`div`,{className:`fp ${P?`fullscreen`:``} ${qe?`is-ppt`:``}`,style:{"--fp-zoom":F/100},children:[(0,f.jsxs)(`div`,{className:`fp-head`,onDoubleClick:e=>{let t=e.target;t&&!t.closest(`button, a, input`)&&Ae(e=>!e)},children:[(0,f.jsx)(`span`,{className:`fp-name`,title:e.path,children:e.name}),(0,f.jsx)(`span`,{className:`fp-path`,children:e.path}),(0,f.jsxs)(`span`,{className:`fp-meta`,children:[y&&q===`text`&&!K&&v(`fileLines`,{n:Pe}),y&&` · ${ge(y.size)}`]}),(0,f.jsx)(`span`,{className:`fp-head-actions`,children:he($e,Qe,xe)}),(0,f.jsx)(`button`,{type:`button`,className:`fp-close`,title:v(`close`),"aria-label":v(`close`),onClick:G,children:(0,f.jsx)(se,{})})]}),We&&q===`text`&&!K&&(0,f.jsx)(`div`,{className:`fp-notice`,children:g(e.name)?v(`previewLinesTruncated`,{n:U.length}):v(`previewTruncated`)}),b&&!y&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`loading`)}),!b&&q===`none`&&!K&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`previewNotSupported`)}),!b&&q===`image`&&(0,f.jsx)(`div`,{className:`fp-media-wrap clickable`,onClick:G,title:v(`clickToCloseImage`),children:(0,f.jsx)(`img`,{className:`fp-media fp-media-clickable`,src:$(e.path),alt:e.name,onClick:G,title:v(`clickToCloseImage`)})}),!b&&q===`video`&&(0,f.jsx)(`div`,{className:`fp-media-wrap`,children:(0,f.jsx)(`video`,{className:`fp-media`,src:$(e.path),controls:!0,preload:`metadata`})}),!b&&q===`audio`&&(0,f.jsx)(`div`,{className:`fp-media-wrap fp-audio-wrap`,children:(0,f.jsxs)(`div`,{className:`fp-audio-card`,children:[(0,f.jsx)(`div`,{className:`fp-audio-icon`,"aria-hidden":`true`,children:`🎵`}),(0,f.jsxs)(`div`,{className:`fp-audio-info`,children:[(0,f.jsx)(`div`,{className:`fp-audio-name`,title:e.name,children:e.name}),y?.size?(0,f.jsx)(`div`,{className:`fp-audio-size`,children:ge(y.size)}):null]}),(0,f.jsx)(`audio`,{className:`fp-audio-element`,src:$(e.path),controls:!0,autoPlay:!0,preload:`metadata`})]})}),!b&&Y&&y&&(0,f.jsxs)(`div`,{className:`fp-html`,children:[(0,f.jsxs)(`div`,{className:`fp-html-bar`,children:[(0,f.jsx)(`span`,{className:`fp-html-hint`,title:v(j?`htmlJsOnTip`:`htmlJsOffTip`),children:v(j?`htmlJsOn`:`htmlJsOff`)}),(0,f.jsx)(`button`,{type:`button`,className:`fp-html-js ${j?`on`:``}`,"data-tip":v(j?`htmlDisableJs`:`htmlEnableJs`),onClick:()=>Oe(e=>!e),children:v(j?`htmlDisableJs`:`htmlEnableJs`)}),(0,f.jsxs)(`a`,{className:`fp-html-btn fp-html-external`,href:et(e.path),target:`_blank`,rel:`noopener noreferrer`,title:v(`openInNewTab`),children:[(0,f.jsx)(ie,{}),(0,f.jsx)(`span`,{children:v(`openInNewTab`)})]})]}),(0,f.jsx)(`iframe`,{className:`fp-html-frame`,src:et(e.path),title:e.name,sandbox:j?`allow-scripts`:``,referrerPolicy:`no-referrer`},j?`js`:`nojs`)]}),!b&&J&&y&&(0,f.jsx)(`div`,{className:g(e.name)?`fp-markdown msg-text fp-office`:`fp-markdown msg-text`,children:(0,f.jsxs)(`div`,{className:X&&d(Z)?`fp-toc-layout`:void 0,children:[X&&d(Z)&&(0,f.jsx)(`nav`,{className:`fp-toc`,"aria-label":v(`previewTocLabel`),children:Z.map(e=>(0,f.jsx)(`a`,{className:`l${Math.min(e.level,6)}${e.id===Xe?` on`:``}`,href:`#${e.id}`,title:e.label,onClick:t=>{t.preventDefault(),Q.current?.querySelector(`#${CSS.escape(e.id)}`)?.scrollIntoView({behavior:`smooth`,block:`start`})},children:e.label},e.id))}),(0,f.jsx)(`div`,{className:`fp-markdown-zoom`,ref:Q,children:(0,f.jsx)(ve,{text:y.text})})]})}),!b&&K&&q!==`image`&&q!==`video`&&y&&(0,f.jsxs)(`div`,{className:`fp-hex-wrap`,children:[(0,f.jsxs)(`div`,{className:`fp-notice`,children:[v(`binaryFile`),y.truncated&&v(`binaryHexTruncated`)]}),(0,f.jsx)(`pre`,{className:`fp-hex`,children:y.text})]}),!b&&C&&q===`text`&&!K&&y&&(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`textarea`,{className:`fp-editor ${N?``:`no-wrap`}`,value:T,onChange:e=>E(e.target.value),wrap:N?`soft`:`off`,spellCheck:!1,autoFocus:!0}),Ne&&(0,f.jsx)(`div`,{className:`fp-notice`,role:`alert`,children:v(`saveResultUnknown`)})]}),!b&&!J&&!Y&&!C&&q===`text`&&!K&&y&&U.length===0&&(0,f.jsx)(`div`,{className:`fp-empty`,children:v(`emptyFile`)}),!b&&!J&&!Y&&!C&&q===`text`&&!K&&U.length>0&&(0,f.jsxs)(`div`,{className:`fp-code ${we?`dragging`:``} ${N?``:`no-wrap`}`,onMouseDown:e=>{e.button===0&&e.preventDefault()},children:[U.map((e,t)=>{let n=t+1,r=x!==null&&n>=x.start&&n<=x.end;return(0,f.jsxs)(`div`,{className:`fp-line ${r?`sel`:``}`,onMouseDown:e=>{e.button===0&&(Ie(n,e.shiftKey),L.current=!0,Te(!0))},onMouseEnter:()=>{L.current&&Ie(n,!0)},children:[(0,f.jsx)(`span`,{className:`fp-num`,children:n}),(0,f.jsx)(`span`,{className:`fp-code-text`,children:e})]},n)}),Fe&&(0,f.jsx)(`div`,{className:`fp-lines-note`,children:v(`previewLinesTruncated`,{n:p})})]}),(C||!J&&!Y&&q===`text`)&&(0,f.jsx)(`div`,{className:`fp-foot`,children:C?(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`span`,{className:`fp-hint`,children:v(`editFile`)}),(0,f.jsxs)(`div`,{className:`fp-actions`,children:[(0,f.jsx)(`button`,{type:`button`,className:`btn`,onClick:Be,children:v(`cancel`)}),(0,f.jsxs)(`button`,{type:`button`,className:`btn primary`,disabled:Me||T===(y?.text??``),onClick:Ve,children:[(0,f.jsx)(pe,{}),` `,v(`saveFile`)]})]})]}):(0,f.jsxs)(f.Fragment,{children:[(0,f.jsx)(`span`,{className:`fp-hint`,children:x?v(`selectedRange`,{n:Ue,start:x.start,end:x.end}):v(`selectLinesHint`)}),(0,f.jsxs)(`div`,{className:`fp-actions`,children:[(0,f.jsx)(`button`,{type:`button`,className:`btn`,disabled:U.length===0,onClick:Le,children:v(`selectAll`)}),(0,f.jsx)(`button`,{type:`button`,className:`btn`,disabled:!x,onClick:()=>S(null),children:v(`clearSelection`)}),(0,f.jsxs)(`button`,{type:`button`,className:`btn primary`,disabled:!x||K,onClick:Re,children:[Ee?(0,f.jsx)(r,{}):null,v(Ee?`addedToChat`:`addToChat`)]})]})]})})]})})}function h(e){let t=e.toLowerCase();return t.endsWith(`.md`)||t.endsWith(`.markdown`)}function g(e){let t=e.toLowerCase();return t.endsWith(`.docx`)||t.endsWith(`.xlsx`)||t.endsWith(`.xlsm`)||t.endsWith(`.pptx`)||t.endsWith(`.pptm`)||t.endsWith(`.ppsx`)||t.endsWith(`.potx`)}function _(e){let t=e.toLowerCase();return t.endsWith(`.html`)||t.endsWith(`.htm`)||t.endsWith(`.xhtml`)}export{m as FilePreview};
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{Mt as t,Tt as n,Vt as r,_n as i,i as a,it as o,o as s,rt as c,sn as l,t as u}from"./app-globals-DZNa9v8u.js";import{i as d,t as f}from"./react-CR5VJ85Q.js";import{b as p,d as m,h,s as g}from"./plugin-host-C8mNgghx.js";import{F as _}from"./index-DHeeHS_l.js";var v=e(d(),1),y=f();function b(e,t){return e.toLowerCase().includes(t)}function x({open:e,projects:d,fileSearch:f,sessionSearch:x,onClose:S,onSwitchSession:C,onSwitchProject:w,onPreviewFile:T}){let E=i(),D=s(),O=E(`pluginMenuTitle`),k=a(`cwd`),A=(0,v.useRef)(null),[j,M]=(0,v.useState)(``),N=(0,v.useDeferredValue)(j),[P,F]=(0,v.useState)(0),I=(0,v.useRef)(0),L=(0,v.useRef)(null),[R,z]=(0,v.useState)(null);(0,v.useEffect)(()=>{if(!e)return;u({type:`list_sessions`}),u({type:`list_projects`});let t=j.trim();if(t){let e=_();I.current=e,V(!0),u({type:`search_files`,reqId:e,query:t}),u({type:`search_sessions`,reqId:e,query:t})}requestAnimationFrame(()=>A.current?.select())},[e]);let[B,V]=(0,v.useState)(!1);(0,v.useEffect)(()=>{L.current&&clearTimeout(L.current);let e=N.trim();if(!e){V(!1),z(null);return}return L.current=setTimeout(()=>{let t=_();I.current=t,V(!0),u({type:`search_files`,reqId:t,query:e}),u({type:`search_sessions`,reqId:t,query:e})},300),()=>{L.current&&clearTimeout(L.current)}},[N]),(0,v.useEffect)(()=>{f&&f.reqId===I.current&&z(f)},[f]);let H=N.trim().toLowerCase(),U=(0,v.useMemo)(()=>!H||!x||x.reqId!==I.current||!x.ok?[]:x.results,[x,H]),W=(0,v.useMemo)(()=>H?d.filter(e=>b(e.path,H)).slice(0,10):[],[d,H]),G=(0,v.useMemo)(()=>!H||!R||!R.ok?[]:R.results,[R,H]);(0,v.useEffect)(()=>{R&&R.reqId===I.current&&x&&x.reqId===I.current&&I.current!==0&&V(!1)},[R,x]);let K=!!R&&R.ok&&R.truncated,[q,J]=(0,v.useState)([]);(0,v.useEffect)(()=>{if(!e)return;let t=N.trim();if(!t){J([]);return}let n=!1,r=[];try{let e=window,t=e.__piWebUiHost?.searchProviders?.list;if(typeof t==`function`){let n=t.call(e.__piWebUiHost);Array.isArray(n)&&(r=n)}else typeof m==`function`&&(r=m())}catch{r=[]}let i=r.filter(e=>e&&typeof e.id==`string`).map(e=>{if(typeof e.search==`function`)return e;try{let t=g(e.id);return t&&typeof t.search==`function`?{...e,search:t.search}:null}catch{return null}}).filter(e=>!!e);if(i.length===0){J([]);return}return Promise.allSettled(i.map(e=>Promise.race([e.search(t),new Promise((e,t)=>setTimeout(()=>t(Error(`timeout`)),3e3))]).then(t=>({provider:e,items:Array.isArray(t)?t:[]}),()=>({provider:e,items:[]})))).then(e=>{if(n)return;let t=[];for(let n of e)if(n.status===`fulfilled`)for(let e of n.value.items){if(!e||typeof e!=`object`)continue;let r=e;typeof r.title==`string`&&typeof r.action==`string`&&t.push({providerId:n.value.provider.id,providerLabel:n.value.provider.label,title:r.title,...typeof r.hint==`string`?{hint:r.hint}:{},action:r.action})}J(t.slice(0,20))}),()=>{n=!0}},[e,N]);let Y=(0,v.useMemo)(()=>[...U.map(e=>({kind:`session`,path:e.path,anchors:e.anchors})),...W.map(e=>({kind:`project`,path:e.path})),...G.filter(e=>e.type===`file`).map(e=>({kind:`file`,path:e.path,name:e.name})),...q.map(e=>({kind:`plugin`,providerId:e.providerId,providerLabel:e.providerLabel,title:e.title,action:e.action}))],[U,W,G,q]);(0,v.useEffect)(()=>{F(0)},[N]);let X=(0,v.useCallback)(e=>{if(e){if(e.kind===`session`)C(e.path,e.anchors),S();else if(e.kind===`project`){w(e.path),u({type:`list_sessions`}),u({type:`list_projects`});let t=N.trim();if(t){let e=_();I.current=e,V(!0),u({type:`search_files`,reqId:e,query:t}),u({type:`search_sessions`,reqId:e,query:t})}}else e.kind===`plugin`?h(e.providerId,e.action,e.action).catch(()=>{}):T(e.path,e.name)}},[C,w,T,N]);(0,v.useEffect)(()=>{if(!e)return;let t=e=>{e.key===`Escape`?(e.preventDefault(),e.stopPropagation(),S()):e.key===`ArrowDown`?(e.preventDefault(),F(e=>Math.max(0,Math.min(e+1,Y.length-1)))):e.key===`ArrowUp`?(e.preventDefault(),F(e=>Math.max(e-1,0))):e.key===`Enter`&&(e.preventDefault(),X(Y[P]))};return window.addEventListener(`keydown`,t,!0),()=>window.removeEventListener(`keydown`,t,!0)},[Y,P,X,S,e]);let Z=U.length+W.length+G.length+q.length,Q=(e,t,n)=>(0,y.jsxs)(`div`,{className:`gs-section-head`,children:[n,(0,y.jsx)(`span`,{children:e}),(0,y.jsx)(`em`,{children:t})]}),$=-1;return e?(0,y.jsx)(`div`,{className:`modal-backdrop gs-backdrop`,onClick:S,children:(0,y.jsxs)(`div`,{className:`gs-modal`,onClick:e=>e.stopPropagation(),children:[(0,y.jsxs)(`div`,{className:`gs-input-row`,children:[(0,y.jsx)(r,{}),(0,y.jsx)(`input`,{ref:A,type:`text`,value:j,placeholder:E(`gsPlaceholder`),onChange:e=>M(e.target.value)}),(0,y.jsx)(`button`,{type:`button`,className:`gs-close`,title:E(`close`),onClick:S,children:(0,y.jsx)(l,{})})]}),(0,y.jsxs)(`div`,{className:`gs-results`,children:[!H&&(0,y.jsx)(`div`,{className:`gs-empty`,children:E(`gsHint`)}),H&&Z===0&&!K&&(0,y.jsx)(`div`,{className:`gs-empty`,children:E(B?`gsSearching`:`gsNoResults`)}),U.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsSessions`),U.length,(0,y.jsx)(n,{})),U.map(e=>{$++;let t=$,n=e.displayName||e.name||e.firstMessage?.trim()||(e.path.split(/[\\/]/).pop()||e.path).replace(/\.jsonl$/i,``);return(0,y.jsxs)(`div`,{className:`gs-item-wrap`,children:[(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,onMouseEnter:()=>F(t),onClick:()=>X({kind:`session`,path:e.path,anchors:e.anchors}),children:[(0,y.jsxs)(`span`,{className:`gs-item-title`,children:[n,(0,y.jsx)(`em`,{className:`gs-item-meta`,children:e.messageCount})]}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.firstMessage})]}),D.conversationRef&&(0,y.jsxs)(`button`,{type:`button`,className:`gs-quote`,title:E(`quoteConversation`),onClick:t=>{t.stopPropagation(),p({attachments:[{path:``,key:`conv||${e.path}`,name:n,mode:`conversation`,sessionPath:e.path}]})},children:[`💬 `,E(`quoteConversationShort`)]})]},e.path)})]}),W.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsProjects`),W.length,(0,y.jsx)(o,{})),W.map(e=>{$++;let t=$,n=k&&e.path===k;return(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,onMouseEnter:()=>F(t),onClick:()=>X({kind:`project`,path:e.path}),children:[(0,y.jsxs)(`span`,{className:`gs-item-title`,children:[e.path.split(/[\\/]/).pop(),n&&(0,y.jsx)(`em`,{className:`gs-item-meta`,children:E(`gsCurrentProject`)})]}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.path})]},e.path)})]}),(G.length>0||H&&K)&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsFiles`),G.length,(0,y.jsx)(c,{})),G.map(e=>{$++;let t=$;return(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,disabled:e.type===`dir`,title:e.type===`dir`?void 0:E(`gsOpenPreview`),onMouseEnter:()=>F(t),onClick:()=>{e.type===`file`&&X({kind:`file`,path:e.path,name:e.name})},children:[(0,y.jsx)(`span`,{className:`gs-item-title`,children:e.name}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.path})]},`${e.type}:${e.path}`)}),K&&(0,y.jsx)(`div`,{className:`gs-truncated`,children:E(`gsTruncated`)})]}),q.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(O,q.length,(0,y.jsx)(t,{})),q.map((e,t)=>{$++;let n=$;return(0,y.jsxs)(`button`,{type:`button`,className:n===P?`gs-item active`:`gs-item`,title:e.providerLabel,onMouseEnter:()=>F(n),onClick:()=>X({kind:`plugin`,providerId:e.providerId,providerLabel:e.providerLabel,title:e.title,action:e.action}),children:[(0,y.jsx)(`span`,{className:`gs-item-title`,children:e.title}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.hint??e.providerLabel})]},`${e.providerId}:${e.action}:${t}`)})]})]}),(0,y.jsxs)(`div`,{className:`gs-foot`,children:[(0,y.jsxs)(`span`,{children:[`↑↓ `,E(`gsNavigate`)]}),(0,y.jsxs)(`span`,{children:[`Enter `,E(`gsOpen`)]}),(0,y.jsxs)(`span`,{children:[`Esc `,E(`gsCloseHint`)]})]})]})}):null}export{x as GlobalSearchModal};
@@ -0,0 +1 @@
1
+ import{r as e}from"./rolldown-runtime-hePW80VL.js";import{Mt as t,Tt as n,Vt as r,_n as i,i as a,it as o,o as s,rt as c,sn as l,t as u}from"./app-globals-DZNa9v8u.js";import{i as d,t as f}from"./react-CR5VJ85Q.js";import{b as p,d as m,h,s as g}from"./plugin-host-C8mNgghx.js";import{F as _}from"./index-DTZch8uz.js";var v=e(d(),1),y=f();function b(e,t){return e.toLowerCase().includes(t)}function x({open:e,projects:d,fileSearch:f,sessionSearch:x,onClose:S,onSwitchSession:C,onSwitchProject:w,onPreviewFile:T}){let E=i(),D=s(),O=E(`pluginMenuTitle`),k=a(`cwd`),A=(0,v.useRef)(null),[j,M]=(0,v.useState)(``),N=(0,v.useDeferredValue)(j),[P,F]=(0,v.useState)(0),I=(0,v.useRef)(0),L=(0,v.useRef)(null),[R,z]=(0,v.useState)(null);(0,v.useEffect)(()=>{if(!e)return;u({type:`list_sessions`}),u({type:`list_projects`});let t=j.trim();if(t){let e=_();I.current=e,V(!0),u({type:`search_files`,reqId:e,query:t}),u({type:`search_sessions`,reqId:e,query:t})}requestAnimationFrame(()=>A.current?.select())},[e]);let[B,V]=(0,v.useState)(!1);(0,v.useEffect)(()=>{L.current&&clearTimeout(L.current);let e=N.trim();if(!e){V(!1),z(null);return}return L.current=setTimeout(()=>{let t=_();I.current=t,V(!0),u({type:`search_files`,reqId:t,query:e}),u({type:`search_sessions`,reqId:t,query:e})},300),()=>{L.current&&clearTimeout(L.current)}},[N]),(0,v.useEffect)(()=>{f&&f.reqId===I.current&&z(f)},[f]);let H=N.trim().toLowerCase(),U=(0,v.useMemo)(()=>!H||!x||x.reqId!==I.current||!x.ok?[]:x.results,[x,H]),W=(0,v.useMemo)(()=>H?d.filter(e=>b(e.path,H)).slice(0,10):[],[d,H]),G=(0,v.useMemo)(()=>!H||!R||!R.ok?[]:R.results,[R,H]);(0,v.useEffect)(()=>{R&&R.reqId===I.current&&x&&x.reqId===I.current&&I.current!==0&&V(!1)},[R,x]);let K=!!R&&R.ok&&R.truncated,[q,J]=(0,v.useState)([]);(0,v.useEffect)(()=>{if(!e)return;let t=N.trim();if(!t){J([]);return}let n=!1,r=[];try{let e=window,t=e.__piWebUiHost?.searchProviders?.list;if(typeof t==`function`){let n=t.call(e.__piWebUiHost);Array.isArray(n)&&(r=n)}else typeof m==`function`&&(r=m())}catch{r=[]}let i=r.filter(e=>e&&typeof e.id==`string`).map(e=>{if(typeof e.search==`function`)return e;try{let t=g(e.id);return t&&typeof t.search==`function`?{...e,search:t.search}:null}catch{return null}}).filter(e=>!!e);if(i.length===0){J([]);return}return Promise.allSettled(i.map(e=>Promise.race([e.search(t),new Promise((e,t)=>setTimeout(()=>t(Error(`timeout`)),3e3))]).then(t=>({provider:e,items:Array.isArray(t)?t:[]}),()=>({provider:e,items:[]})))).then(e=>{if(n)return;let t=[];for(let n of e)if(n.status===`fulfilled`)for(let e of n.value.items){if(!e||typeof e!=`object`)continue;let r=e;typeof r.title==`string`&&typeof r.action==`string`&&t.push({providerId:n.value.provider.id,providerLabel:n.value.provider.label,title:r.title,...typeof r.hint==`string`?{hint:r.hint}:{},action:r.action})}J(t.slice(0,20))}),()=>{n=!0}},[e,N]);let Y=(0,v.useMemo)(()=>[...U.map(e=>({kind:`session`,path:e.path,anchors:e.anchors})),...W.map(e=>({kind:`project`,path:e.path})),...G.filter(e=>e.type===`file`).map(e=>({kind:`file`,path:e.path,name:e.name})),...q.map(e=>({kind:`plugin`,providerId:e.providerId,providerLabel:e.providerLabel,title:e.title,action:e.action}))],[U,W,G,q]);(0,v.useEffect)(()=>{F(0)},[N]);let X=(0,v.useCallback)(e=>{if(e){if(e.kind===`session`)C(e.path,e.anchors),S();else if(e.kind===`project`){w(e.path),u({type:`list_sessions`}),u({type:`list_projects`});let t=N.trim();if(t){let e=_();I.current=e,V(!0),u({type:`search_files`,reqId:e,query:t}),u({type:`search_sessions`,reqId:e,query:t})}}else e.kind===`plugin`?h(e.providerId,e.action,e.action).catch(()=>{}):T(e.path,e.name)}},[C,w,T,N]);(0,v.useEffect)(()=>{if(!e)return;let t=e=>{e.key===`Escape`?(e.preventDefault(),e.stopPropagation(),S()):e.key===`ArrowDown`?(e.preventDefault(),F(e=>Math.max(0,Math.min(e+1,Y.length-1)))):e.key===`ArrowUp`?(e.preventDefault(),F(e=>Math.max(e-1,0))):e.key===`Enter`&&(e.preventDefault(),X(Y[P]))};return window.addEventListener(`keydown`,t,!0),()=>window.removeEventListener(`keydown`,t,!0)},[Y,P,X,S,e]);let Z=U.length+W.length+G.length+q.length,Q=(e,t,n)=>(0,y.jsxs)(`div`,{className:`gs-section-head`,children:[n,(0,y.jsx)(`span`,{children:e}),(0,y.jsx)(`em`,{children:t})]}),$=-1;return e?(0,y.jsx)(`div`,{className:`modal-backdrop gs-backdrop`,onClick:S,children:(0,y.jsxs)(`div`,{className:`gs-modal`,onClick:e=>e.stopPropagation(),children:[(0,y.jsxs)(`div`,{className:`gs-input-row`,children:[(0,y.jsx)(r,{}),(0,y.jsx)(`input`,{ref:A,type:`text`,value:j,placeholder:E(`gsPlaceholder`),onChange:e=>M(e.target.value)}),(0,y.jsx)(`button`,{type:`button`,className:`gs-close`,title:E(`close`),onClick:S,children:(0,y.jsx)(l,{})})]}),(0,y.jsxs)(`div`,{className:`gs-results`,children:[!H&&(0,y.jsx)(`div`,{className:`gs-empty`,children:E(`gsHint`)}),H&&Z===0&&!K&&(0,y.jsx)(`div`,{className:`gs-empty`,children:E(B?`gsSearching`:`gsNoResults`)}),U.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsSessions`),U.length,(0,y.jsx)(n,{})),U.map(e=>{$++;let t=$,n=e.displayName||e.name||e.firstMessage?.trim()||(e.path.split(/[\\/]/).pop()||e.path).replace(/\.jsonl$/i,``);return(0,y.jsxs)(`div`,{className:`gs-item-wrap`,children:[(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,onMouseEnter:()=>F(t),onClick:()=>X({kind:`session`,path:e.path,anchors:e.anchors}),children:[(0,y.jsxs)(`span`,{className:`gs-item-title`,children:[n,(0,y.jsx)(`em`,{className:`gs-item-meta`,children:e.messageCount})]}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.firstMessage})]}),D.conversationRef&&(0,y.jsxs)(`button`,{type:`button`,className:`gs-quote`,title:E(`quoteConversation`),onClick:t=>{t.stopPropagation(),p({attachments:[{path:``,key:`conv||${e.path}`,name:n,mode:`conversation`,sessionPath:e.path}]})},children:[`💬 `,E(`quoteConversationShort`)]})]},e.path)})]}),W.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsProjects`),W.length,(0,y.jsx)(o,{})),W.map(e=>{$++;let t=$,n=k&&e.path===k;return(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,onMouseEnter:()=>F(t),onClick:()=>X({kind:`project`,path:e.path}),children:[(0,y.jsxs)(`span`,{className:`gs-item-title`,children:[e.path.split(/[\\/]/).pop(),n&&(0,y.jsx)(`em`,{className:`gs-item-meta`,children:E(`gsCurrentProject`)})]}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.path})]},e.path)})]}),(G.length>0||H&&K)&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(E(`gsFiles`),G.length,(0,y.jsx)(c,{})),G.map(e=>{$++;let t=$;return(0,y.jsxs)(`button`,{type:`button`,className:t===P?`gs-item active`:`gs-item`,disabled:e.type===`dir`,title:e.type===`dir`?void 0:E(`gsOpenPreview`),onMouseEnter:()=>F(t),onClick:()=>{e.type===`file`&&X({kind:`file`,path:e.path,name:e.name})},children:[(0,y.jsx)(`span`,{className:`gs-item-title`,children:e.name}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.path})]},`${e.type}:${e.path}`)}),K&&(0,y.jsx)(`div`,{className:`gs-truncated`,children:E(`gsTruncated`)})]}),q.length>0&&(0,y.jsxs)(`div`,{className:`gs-section`,children:[Q(O,q.length,(0,y.jsx)(t,{})),q.map((e,t)=>{$++;let n=$;return(0,y.jsxs)(`button`,{type:`button`,className:n===P?`gs-item active`:`gs-item`,title:e.providerLabel,onMouseEnter:()=>F(n),onClick:()=>X({kind:`plugin`,providerId:e.providerId,providerLabel:e.providerLabel,title:e.title,action:e.action}),children:[(0,y.jsx)(`span`,{className:`gs-item-title`,children:e.title}),(0,y.jsx)(`span`,{className:`gs-item-sub`,children:e.hint??e.providerLabel})]},`${e.providerId}:${e.action}:${t}`)})]})]}),(0,y.jsxs)(`div`,{className:`gs-foot`,children:[(0,y.jsxs)(`span`,{children:[`↑↓ `,E(`gsNavigate`)]}),(0,y.jsxs)(`span`,{children:[`Enter `,E(`gsOpen`)]}),(0,y.jsxs)(`span`,{children:[`Esc `,E(`gsCloseHint`)]})]})]})}):null}export{x as GlobalSearchModal};