aws-runtime-bridge 1.9.98 → 1.9.101

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 (126) hide show
  1. package/dist/adapter/AcodeSdkAdapter.d.ts +7 -1
  2. package/dist/adapter/AcodeSdkAdapter.js +5 -5
  3. package/dist/adapter/ClaudeSdkAdapter.d.ts +4 -0
  4. package/dist/adapter/ClaudeSdkAdapter.js +2 -2
  5. package/dist/adapter/CodexSdkAdapter.d.ts +2 -0
  6. package/dist/adapter/CodexSdkAdapter.js +3 -3
  7. package/dist/adapter/OpencodeSdkAdapter.d.ts +2 -0
  8. package/dist/adapter/OpencodeSdkAdapter.js +2 -2
  9. package/dist/adapter/idle-poll-loop.d.ts +4 -1
  10. package/dist/adapter/idle-poll-loop.js +1 -1
  11. package/dist/adapter/types.d.ts +4 -2
  12. package/dist/browser/browser-bootstrap.d.ts +11 -0
  13. package/dist/browser/browser-bootstrap.js +4 -0
  14. package/dist/browser/browser-connect-token.d.ts +10 -0
  15. package/dist/browser/browser-connect-token.js +2 -0
  16. package/dist/browser/browser-pool.d.ts +75 -0
  17. package/dist/browser/browser-pool.js +2 -0
  18. package/dist/browser/browser-profile-store.d.ts +28 -0
  19. package/dist/browser/browser-profile-store.js +2 -0
  20. package/dist/browser/browser-ws-bridge.d.ts +12 -0
  21. package/dist/browser/browser-ws-bridge.js +6 -0
  22. package/dist/browser/built-in-browser-tools.d.ts +27 -0
  23. package/dist/browser/built-in-browser-tools.js +2 -0
  24. package/dist/browser/element-locator.d.ts +24 -0
  25. package/dist/browser/element-locator.js +2 -0
  26. package/dist/browser/element-picker.d.ts +17 -0
  27. package/dist/browser/element-picker.js +2 -0
  28. package/dist/browser/input-dispatcher.d.ts +34 -0
  29. package/dist/browser/input-dispatcher.js +2 -0
  30. package/dist/browser/listener-store.d.ts +32 -0
  31. package/dist/browser/listener-store.js +2 -0
  32. package/dist/browser/page-listener-manager.d.ts +106 -0
  33. package/dist/browser/page-listener-manager.js +307 -0
  34. package/dist/browser/page-listener-types.d.ts +90 -0
  35. package/dist/browser/page-listener-types.js +1 -0
  36. package/dist/browser/screencast-manager.d.ts +42 -0
  37. package/dist/browser/screencast-manager.js +2 -0
  38. package/dist/browser/snapshot-serializer.d.ts +15 -0
  39. package/dist/browser/snapshot-serializer.js +5 -0
  40. package/dist/browser/ssrf-guard.d.ts +15 -0
  41. package/dist/browser/ssrf-guard.js +2 -0
  42. package/dist/browser/stealth.d.ts +7 -0
  43. package/dist/browser/stealth.js +173 -0
  44. package/dist/browser/step-archive.d.ts +52 -0
  45. package/dist/browser/step-archive.js +2 -0
  46. package/dist/browser/types.d.ts +192 -0
  47. package/dist/browser/types.js +2 -0
  48. package/dist/browser/ws-server.d.ts +67 -0
  49. package/dist/browser/ws-server.js +2 -0
  50. package/dist/index.js +1 -1
  51. package/dist/middleware/auth.d.ts +2 -0
  52. package/dist/middleware/auth.js +1 -1
  53. package/dist/remote-desktop/computer-control.d.ts +30 -0
  54. package/dist/remote-desktop/computer-control.js +2 -0
  55. package/dist/remote-desktop/coords.d.ts +13 -0
  56. package/dist/remote-desktop/coords.js +2 -0
  57. package/dist/remote-desktop/frame-backpressure.d.ts +7 -0
  58. package/dist/remote-desktop/frame-backpressure.js +2 -0
  59. package/dist/remote-desktop/index.d.ts +7 -0
  60. package/dist/remote-desktop/index.js +4 -0
  61. package/dist/remote-desktop/input-backends/interface.d.ts +17 -0
  62. package/dist/remote-desktop/input-backends/interface.js +1 -0
  63. package/dist/remote-desktop/input-backends/key-map.d.ts +2 -0
  64. package/dist/remote-desktop/input-backends/key-map.js +2 -0
  65. package/dist/remote-desktop/input-dispatcher.d.ts +11 -0
  66. package/dist/remote-desktop/input-dispatcher.js +2 -0
  67. package/dist/remote-desktop/permission-manager.d.ts +7 -0
  68. package/dist/remote-desktop/permission-manager.js +2 -0
  69. package/dist/remote-desktop/screen-capture.d.ts +27 -0
  70. package/dist/remote-desktop/screen-capture.js +2 -0
  71. package/dist/remote-desktop/session-manager.d.ts +59 -0
  72. package/dist/remote-desktop/session-manager.js +2 -0
  73. package/dist/remote-desktop/system-backend.d.ts +65 -0
  74. package/dist/remote-desktop/system-backend.js +5 -0
  75. package/dist/remote-desktop/types.d.ts +68 -0
  76. package/dist/remote-desktop/types.js +1 -0
  77. package/dist/remote-desktop/ui-automation.d.ts +48 -0
  78. package/dist/remote-desktop/ui-automation.js +31 -0
  79. package/dist/remote-desktop/win32-api.d.ts +29 -0
  80. package/dist/remote-desktop/win32-api.js +2 -0
  81. package/dist/routes/background-tasks.d.ts +2 -0
  82. package/dist/routes/background-tasks.js +2 -0
  83. package/dist/routes/browser-listeners.d.ts +2 -0
  84. package/dist/routes/browser-listeners.js +2 -0
  85. package/dist/routes/browser-sessions.d.ts +2 -0
  86. package/dist/routes/browser-sessions.js +2 -0
  87. package/dist/routes/pty.js +3 -3
  88. package/dist/routes/terminal.js +30 -30
  89. package/dist/services/agent-pty-manager.d.ts +22 -0
  90. package/dist/services/agent-pty-manager.js +5 -0
  91. package/dist/services/browser-session-manager.d.ts +55 -0
  92. package/dist/services/browser-session-manager.js +12 -0
  93. package/dist/services/cli-commands.d.ts +4 -0
  94. package/dist/services/cli-commands.js +5 -5
  95. package/package/acode/dist/await-complete-tool.d.ts +17 -0
  96. package/package/acode/dist/await-complete-tool.js +21 -0
  97. package/package/acode/dist/background-command-manager.d.ts +148 -0
  98. package/package/acode/dist/background-command-manager.js +8 -0
  99. package/package/acode/dist/background-command-store.d.ts +46 -0
  100. package/package/acode/dist/background-command-store.js +2 -0
  101. package/package/acode/dist/background-command-tools.d.ts +5 -0
  102. package/package/acode/dist/background-command-tools.js +18 -0
  103. package/package/acode/dist/background-output-ringbuffer.d.ts +23 -0
  104. package/package/acode/dist/background-output-ringbuffer.js +3 -0
  105. package/package/acode/dist/built-in-file-tools.d.ts +8 -1
  106. package/package/acode/dist/built-in-file-tools.js +2 -2
  107. package/package/acode/dist/compaction.js +6 -6
  108. package/package/acode/dist/index.d.ts +1 -0
  109. package/package/acode/dist/index.js +1 -1
  110. package/package/acode/dist/runtime.d.ts +6 -0
  111. package/package/acode/dist/runtime.js +27 -27
  112. package/package/acode/dist/sub-agent-tools.d.ts +0 -1
  113. package/package/acode/dist/sub-agent-tools.js +11 -28
  114. package/package/acode/dist/terminal-tool.d.ts +7 -0
  115. package/package/acode/dist/terminal-tool.js +2 -0
  116. package/package/acode/dist/terminal-types.d.ts +38 -0
  117. package/package/acode/dist/terminal-types.js +1 -0
  118. package/package/acode/dist/types.d.ts +10 -1
  119. package/package/aws-client-agent-mcp/dist/agent-client.d.ts +3 -1
  120. package/package/aws-client-agent-mcp/dist/agent-client.js +1 -1
  121. package/package/aws-client-agent-mcp/dist/logger.d.ts +2 -0
  122. package/package/aws-client-agent-mcp/dist/logger.js +1 -1
  123. package/package/aws-client-agent-mcp/dist/mcp-server.d.ts +8 -0
  124. package/package/aws-client-agent-mcp/dist/mcp-server.js +30 -19
  125. package/package/aws-client-agent-mcp/dist/types.d.ts +25 -0
  126. package/package.json +6 -2
@@ -1,7 +1,7 @@
1
- import{spawnSync as S}from"node:child_process";import{createInterface as z}from"node:readline/promises";import{existsSync as m,mkdirSync as T,readFileSync as X,rmSync as Z,unlinkSync as O,writeFileSync as F}from"node:fs";import o from"node:path";import v from"node:os";import{fileURLToPath as j}from"node:url";import{configureStartupConfig as ee,printConfigExampleCommand as re}from"./startup-config-wizard.js";import{createWindowsServiceManager as te,isNodeWindowsAvailable as ne}from"./windows-service-manager.js";const f="awsb.service",L=`/etc/systemd/system/${f}`,H="awsb",g=`${H}.exe`,B=o.join(v.homedir(),".aws-bridge","log"),G=o.join(B,"awsb.out.log"),I="com.agentsworkstudio.awsb",k=o.join(v.homedir(),"Library","LaunchAgents",`${I}.plist`),ae=o.join(v.homedir(),".aws-bridge","log"),U=o.join(ae,"awsb.out.log");function ie(){const e=j(import.meta.url);return o.resolve(o.dirname(e),"..","..")}function se(e=ie()){const r=o.join(e,"package.json");return JSON.parse(X(r,"utf-8")).version||"unknown"}function p(e,r){const t=S(e,r,{shell:process.platform==="win32",stdio:"inherit"});return{status:t.status,error:t.error}}function ue(){try{const e=S("npm",["root","-g"],{shell:process.platform==="win32",encoding:"utf-8",stdio:["ignore","pipe","ignore"]});if(e.status===0&&e.stdout){const r=e.stdout.trim();if(r)return r}}catch{}return null}function de(e){try{const r=new SharedArrayBuffer(4);Atomics.wait(new Int32Array(r),0,0,e)}catch{const r=Date.now()+e;for(;Date.now()<r;);}}function q(e){if(process.platform!=="win32")return null;try{const r=S("sc.exe",["query",e],{shell:!0,encoding:"utf-8",stdio:["ignore","pipe","ignore"]});if(r.status!==0||!r.stdout)return null;const t=r.stdout.match(/STATE\s*:\s*\d+\s+(\S+)/);return t?t[1]:null}catch{return null}}function ce(e,r,t=15e3,a=500){const i=Date.now()+t;for(;Date.now()<i;){const n=e(r);if(n===null||n==="STOPPED")return!0;de(a)}return!1}function oe(e){process.stdout.write(`${e}
1
+ import{spawnSync as f}from"node:child_process";import{createInterface as X}from"node:readline/promises";import{existsSync as g,mkdirSync as T,readFileSync as Z,rmSync as ee,unlinkSync as F,writeFileSync as G}from"node:fs";import c from"node:path";import v from"node:os";import{fileURLToPath as H}from"node:url";import{configureStartupConfig as re,printConfigExampleCommand as te}from"./startup-config-wizard.js";import{createWindowsServiceManager as ne,isNodeWindowsAvailable as se}from"./windows-service-manager.js";const h="awsb.service",I=`/etc/systemd/system/${h}`,j="awsb",l=`${j}.exe`,q=c.join(v.homedir(),".aws-bridge","log"),B=c.join(q,"awsb.out.log"),N="com.agentsworkstudio.awsb",D=c.join(v.homedir(),"Library","LaunchAgents",`${N}.plist`),ie=c.join(v.homedir(),".aws-bridge","log"),L=c.join(ie,"awsb.out.log");function ae(){const e=H(import.meta.url);return c.resolve(c.dirname(e),"..","..")}function ue(e=ae()){const r=c.join(e,"package.json");return JSON.parse(Z(r,"utf-8")).version||"unknown"}function k(e,r){const t=f(e,r,{shell:process.platform==="win32",stdio:"inherit"});return{status:t.status,error:t.error}}function de(){try{const e=f("npm",["root","-g"],{shell:process.platform==="win32",encoding:"utf-8",stdio:["ignore","pipe","ignore"]});if(e.status===0&&e.stdout){const r=e.stdout.trim();if(r)return r}}catch{}return null}function oe(e){try{const r=new SharedArrayBuffer(4);Atomics.wait(new Int32Array(r),0,0,e)}catch{const r=Date.now()+e;for(;Date.now()<r;);}}function A(e){if(process.platform!=="win32")return null;try{const r=f("sc.exe",["query",e],{shell:!0,encoding:"utf-8",stdio:["ignore","pipe","ignore"]});if(r.status!==0||!r.stdout)return null;const t=r.stdout.match(/STATE\s*:\s*\d+\s+(\S+)/);return t?t[1]:null}catch{return null}}function V(e,r,t=15e3,s=500){const i=Date.now()+t;for(;Date.now()<i;){const n=e(r);if(n===null||n==="STOPPED")return!0;oe(s)}return!1}function ce(e){process.stdout.write(`${e}
2
2
  `)}function le(e){process.stderr.write(`${e}
3
- `)}function me(e){return e==="-v"||e==="--version"||e==="version"}function fe(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}function V(){return process.argv[1]?o.resolve(process.argv[1]):j(import.meta.url)}function ge(e){return["[Unit]","Description=AgentsWorkStudio Runtime Bridge","After=network-online.target","Wants=network-online.target","","[Service]","Type=simple","Environment=AWS_BRIDGE_SKIP_SETUP=true",`ExecStart=${[process.execPath,e].map(t=>fe(t)).join(" ")}`,"Restart=on-failure","RestartSec=5s","","[Install]","WantedBy=multi-user.target",""].join(`
4
- `)}function P(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function he(e,r={}){const t=r.userHomeDir??v.homedir(),a=r.logPath??U,i=a.replace(/\.out\.log$/,".err.log");return['<?xml version="1.0" encoding="UTF-8"?>','<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">','<plist version="1.0">',"<dict>"," <key>Label</key>",` <string>${P(I)}</string>`," <key>ProgramArguments</key>"," <array>",` <string>${P(process.execPath)}</string>`,` <string>${P(e)}</string>`," </array>"," <key>EnvironmentVariables</key>"," <dict>"," <key>AWS_BRIDGE_SKIP_SETUP</key>"," <string>true</string>"," <key>AWS_RUNTIME_HOME_DIR</key>",` <string>${P(t)}</string>`," </dict>"," <key>RunAtLoad</key>"," <true/>"," <key>KeepAlive</key>"," <true/>"," <key>StandardOutPath</key>",` <string>${P(a)}</string>`," <key>StandardErrorPath</key>",` <string>${P(i)}</string>`,"</dict>","</plist>",""].join(`
5
- `)}function h(e,r){return e("systemctl",r)}function w(e,r){return r.error?`[runtime-bridge] ${e}\u5931\u8D25: ${r.error.message}`:`[runtime-bridge] ${e}\u5931\u8D25\uFF0Csystemctl \u9000\u51FA\u7801: ${r.status??"unknown"}`}function d(e){return!e.error&&e.status===0}function $(e,r){return e==="linux"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd \u73AF\u5883\u3002"),!1)}function R(e,r){return e==="darwin"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 macOS launchd \u73AF\u5883\u3002"),!1)}function K(e){e("Usage: awsb service <install|uninstall|start|stop|restart>")}function we(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=ge(e.executablePath);try{T(o.dirname(e.serviceUnitPath),{recursive:!0}),F(e.serviceUnitPath,r,{encoding:"utf-8",mode:420})}catch(n){const s=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5199\u5165 systemd unit \u5931\u8D25: ${s.message}`),{handled:!0,exitCode:1}}const t=h(e.runCommand,["daemon-reload"]);if(!d(t))return e.stderr(w("\u91CD\u8F7D systemd",t)),{handled:!0,exitCode:t.status??1};const a=h(e.runCommand,["enable",f]);if(!d(a))return e.stderr(w("\u542F\u7528 awsb \u5F00\u673A\u81EA\u542F",a)),{handled:!0,exitCode:a.status??1};const i=h(e.runCommand,["is-active","--quiet",f]);if(i.error)return e.stderr(w("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",i)),{handled:!0,exitCode:1};if(i.status!==0){const n=h(e.runCommand,["start",f]);return d(n)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5B89\u88C5\u3001\u5DF2\u542F\u7528\u5F00\u673A\u81EA\u542F\u5E76\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(w("\u542F\u52A8 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}return e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u7528\u5F00\u673A\u81EA\u542F\uFF1B\u670D\u52A1\u5DF2\u5728\u8FD0\u884C\u3002"),{handled:!0,exitCode:0}}function be(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=m(e.serviceUnitPath),t=h(e.runCommand,["is-active","--quiet",f]);if(t.error)return e.stderr(w("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",t)),{handled:!0,exitCode:1};if(t.status===0){const n=h(e.runCommand,["stop",f]);if(!d(n))return e.stderr(w("\u505C\u6B62 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1}}const a=h(e.runCommand,["disable",f]);if(a.error||a.status!==0&&r)return e.stderr(w("\u7981\u7528 awsb \u5F00\u673A\u81EA\u542F",a)),{handled:!0,exitCode:a.status??1};if(r)try{O(e.serviceUnitPath)}catch(n){const s=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5220\u9664 systemd unit \u5931\u8D25: ${s.message}`),{handled:!0,exitCode:1}}else e.stdout("[runtime-bridge] awsb systemd unit \u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664\u3002");const i=h(e.runCommand,["daemon-reload"]);return d(i)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}):(e.stderr(w("\u91CD\u8F7D systemd",i)),{handled:!0,exitCode:i.status??1})}function Ce(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!m(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=h(e.runCommand,["restart",f]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(w("\u91CD\u542F awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function xe(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!m(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=h(e.runCommand,["start",f]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(w("\u542F\u52A8 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function Se(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!m(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=h(e.runCommand,["stop",f]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(w("\u505C\u6B62 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function ve(e){return e(`[runtime-bridge] \u672A\u68C0\u6D4B\u5230 node-windows \u6A21\u5757\u3002\u8BF7\u91CD\u65B0\u5B89\u88C5 aws-runtime-bridge\uFF1A
6
- npm install -g aws-runtime-bridge`),{handled:!0,exitCode:1}}async function A(e){const r=e.windowsServiceManager;if(!r&&!ne())return ve(e.stderr);const t=e.windowsServiceLogPath||G,a=o.dirname(e.executablePath),i=v.homedir();try{await(r||te()).install({name:H,description:"awsb runtime bridge",script:e.executablePath,workingDirectory:a,env:[{name:"AWS_BRIDGE_SKIP_SETUP",value:"true"},{name:"AWS_RUNTIME_HOME_DIR",value:i}],logpath:B})}catch(s){const c=s instanceof Error?s:new Error(String(s));return e.stderr(`[runtime-bridge] \u5B89\u88C5 Windows \u670D\u52A1\u5931\u8D25: ${c.message}`),{handled:!0,exitCode:1}}const n=e.runCommand("sc.exe",["start",g]);return d(n)?(e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u52A8\uFF08\u65E5\u5FD7: ${t}\uFF09\u3002`),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u542F\u52A8 Windows \u670D\u52A1\u5931\u8D25: ${n.error?.message??`\u9000\u51FA\u7801 ${n.status??"unknown"}`}`),{handled:!0,exitCode:n.status??1})}function Pe(e){const r=a=>e.runCommand("sc.exe",a);r(["stop",g]);const t=r(["delete",g]);return d(t)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u5378\u8F7D Windows \u670D\u52A1\u5931\u8D25: ${t.error?.message??`\u9000\u51FA\u7801 ${t.status??"unknown"}`}`),{handled:!0,exitCode:t.status??1})}async function ye(e){e.runCommand("sc.exe",["stop",g]);const r=e.queryWindowsServiceState||q;return ce(r,g)||e.stdout("[runtime-bridge] \u7B49\u5F85 Windows \u670D\u52A1\u505C\u6B62\u8D85\u65F6\uFF0C\u4ECD\u5C1D\u8BD5\u542F\u52A8\uFF08\u53EF\u80FD\u62A5 1056\uFF09\u3002"),Y(e,"\u91CD\u542F")}async function Ee(e){return Y(e,"\u542F\u52A8")}const $e=2,Re=1056;async function Y(e,r){const t=e.runCommand("sc.exe",["start",g]);if(d(t))return e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2${r}\u3002`),{handled:!0,exitCode:0};if(t.status===Re)return e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5728\u8FD0\u884C\u4E2D\uFF08\u65E0\u9700${r}\uFF09\u3002`),{handled:!0,exitCode:0};if(t.status===$e){e.stdout("[runtime-bridge] \u68C0\u6D4B\u5230 Windows \u670D\u52A1\u4E8C\u8FDB\u5236\u6587\u4EF6\u4E22\u5931\uFF08winsw.exe\uFF09\uFF0C\u6B63\u5728\u91CD\u5EFA..."),e.runCommand("sc.exe",["delete",g]);try{const a=await A(e);return a.exitCode!==0&&e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25\u4E14\u81EA\u52A8\u91CD\u5EFA\u5931\u8D25\u3002\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u3002`),a}catch(a){const i=a instanceof Error?a:new Error(String(a));return e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25\u4E14\u81EA\u52A8\u91CD\u5EFA\u5F02\u5E38: ${i.message}\u3002\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u3002`),{handled:!0,exitCode:1}}}return e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25: ${t.error?.message??`\u9000\u51FA\u7801 ${t.status??"unknown"}`}`),{handled:!0,exitCode:t.status??1}}function We(e){const r=e.runCommand("sc.exe",["stop",g]);return d(r)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u505C\u6B62 Windows \u670D\u52A1\u5931\u8D25: ${r.error?.message??`\u9000\u51FA\u7801 ${r.status??"unknown"}`}`),{handled:!0,exitCode:r.status??1})}function _e(){return`gui/${process.getuid?.()??0}`}function W(e){return e.launchdDomainTarget||_e()}function _(e){return`${e}/${I}`}function D(e,r){return r.error?`[runtime-bridge] ${e}\u5931\u8D25: ${r.error.message}`:`[runtime-bridge] ${e}\u5931\u8D25\uFF0Claunchctl \u9000\u51FA\u7801: ${r.status??"unknown"}`}function De(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||k,t=e.launchdLogPath||U,a=he(e.executablePath,{userHomeDir:v.homedir(),logPath:t});try{T(o.dirname(r),{recursive:!0}),T(o.dirname(t),{recursive:!0}),F(r,a,{encoding:"utf-8",mode:420})}catch(s){const c=s instanceof Error?s:new Error(String(s));return e.stderr(`[runtime-bridge] \u5199\u5165 launchd plist \u5931\u8D25: ${c.message}`),{handled:!0,exitCode:1}}const i=W(e);e.runCommand("launchctl",["bootout",_(i)]);const n=e.runCommand("launchctl",["bootstrap",i,r]);return d(n)?(e.stdout(`[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u52A8\uFF08\u65E5\u5FD7: ${t}\uFF09\u3002`),{handled:!0,exitCode:0}):(e.stderr(D("\u5B89\u88C5 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}function ke(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||k,t=W(e),a=_(t),i=e.runCommand("launchctl",["bootout",a]);if(i.error)return e.stderr(D("\u5378\u8F7D awsb \u670D\u52A1",i)),{handled:!0,exitCode:1};if(m(r))try{O(r)}catch(n){const s=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5220\u9664 launchd plist \u5931\u8D25: ${s.message}`),{handled:!0,exitCode:1}}else e.stdout("[runtime-bridge] awsb launchd plist \u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664\u3002");return e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}}function pe(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||k;if(!m(r))return e.stderr("[runtime-bridge] awsb launchd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const t=W(e),a=_(t),i=e.runCommand("launchctl",["kickstart",a]);if(d(i))return e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0};const n=e.runCommand("launchctl",["bootstrap",t,r]);return d(n)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(D("\u542F\u52A8 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}function Te(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=W(e),t=e.runCommand("launchctl",["bootout",_(r)]);return d(t)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(D("\u505C\u6B62 awsb \u670D\u52A1",t)),{handled:!0,exitCode:t.status??1})}function Le(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||k;if(!m(r))return e.stderr("[runtime-bridge] awsb launchd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const t=W(e);e.runCommand("launchctl",["bootout",_(t)]);const a=e.runCommand("launchctl",["bootstrap",t,r]);return d(a)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(D("\u91CD\u542F awsb \u670D\u52A1",a)),{handled:!0,exitCode:a.status??1})}async function Ie(e,r,t,a){const i=e[1];if(!i||i==="-h"||i==="--help"||i==="help")return K(t),{handled:!0,exitCode:i?0:1};const n={stdout:t,stderr:a,runCommand:r.runCommand||p,platform:r.platform||process.platform,serviceUnitPath:r.serviceUnitPath||L,executablePath:r.executablePath||V(),windowsServiceManager:r.windowsServiceManager,launchdPlistPath:r.launchdPlistPath,launchdLogPath:r.launchdLogPath,launchdDomainTarget:r.launchdDomainTarget,queryWindowsServiceState:r.queryWindowsServiceState||q};return n.platform!=="linux"&&n.platform!=="win32"&&n.platform!=="darwin"?(a("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd\u3001Windows \u670D\u52A1\u4E0E macOS launchd \u73AF\u5883\u3002"),{handled:!0,exitCode:1}):i==="install"?n.platform==="win32"?await A(n):n.platform==="darwin"?De(n):we(n):i==="uninstall"?n.platform==="win32"?Pe(n):n.platform==="darwin"?ke(n):be(n):i==="restart"?n.platform==="win32"?ye(n):n.platform==="darwin"?Le(n):Ce(n):i==="start"?n.platform==="win32"?Ee(n):n.platform==="darwin"?pe(n):xe(n):i==="stop"?n.platform==="win32"?We(n):n.platform==="darwin"?Te(n):Se(n):(a(`[runtime-bridge] \u672A\u77E5 service \u5B50\u547D\u4EE4: ${i}`),K(t),{handled:!0,exitCode:1})}function J(e){return e?e.split(/\r?\n/).map(r=>r.trim()).filter(r=>/^\d+$/.test(r)).map(Number):[]}function Ue(e){try{if(e==="win32"){const t=S("powershell",["-NoProfile","-Command",`Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object { $_.CommandLine -like '*aws-runtime-bridge*' -and $_.ProcessId -ne ${process.pid} } | ForEach-Object { $_.ProcessId }`],{encoding:"utf-8"});return J(t.stdout)}const r=S("pgrep",["-f","aws-runtime-bridge"],{encoding:"utf-8"});return J(r.stdout).filter(t=>t!==process.pid)}catch{return[]}}function Ae(e,r){if(r==="win32"){S("taskkill",["/PID",String(e),"/F","/T"],{stdio:"ignore"});return}S("kill",["-9",String(e)],{stdio:"ignore"})}function Ne(e){if(!process.stdin.isTTY)return Promise.resolve("n");const r=z({input:process.stdin,output:process.stdout});return r.question(e).finally(()=>r.close())}async function Me(e,r,t){const a=e.runCommand||p,i=e.platform||process.platform,n=e.findRunningBridgeProcesses||Ue,s=e.prompt||Ne,c=e.killBridgeProcess||Ae,u=e.globalModulePath||ue;r("[runtime-bridge] \u6B63\u5728\u66F4\u65B0 aws-runtime-bridge \u5230\u6700\u65B0\u7248\u672C...");const l=n(i).filter(C=>C!==process.pid);let b=!1;if(l.length>0){r(`[runtime-bridge] \u68C0\u6D4B\u5230 aws-runtime-bridge \u6B63\u5728\u8FD0\u884C (PID: ${l.join(", ")})\uFF0C\u66F4\u65B0\u65F6\u8FD9\u4E9B\u8FDB\u7A0B\u53EF\u80FD\u5360\u7528\u6587\u4EF6\u5BFC\u81F4\u8986\u76D6\u5931\u8D25\u3002`);const C=(await s("\u662F\u5426\u5173\u95ED\u4E0A\u8FF0\u8FDB\u7A0B\u4EE5\u7EE7\u7EED\u66F4\u65B0? (y/N) ")).trim().toLowerCase();if(C==="y"||C==="yes")for(const x of l)try{c(x,i),r(`[runtime-bridge] \u5DF2\u5173\u95ED\u8FDB\u7A0B PID: ${x}`)}catch(E){const Q=E instanceof Error?E:new Error(String(E));t(`[runtime-bridge] \u5173\u95ED\u8FDB\u7A0B PID ${x} \u5931\u8D25: ${Q.message}`)}else b=!0,r("[runtime-bridge] \u5DF2\u9009\u62E9\u4E0D\u5173\u95ED\u8FDB\u7A0B\uFF0C\u5C06\u4EE5 --force \u65B9\u5F0F\u5F3A\u5236\u5B89\u88C5\uFF08\u82E5\u6587\u4EF6\u4ECD\u88AB\u5360\u7528\u53EF\u80FD\u4F1A\u6709\u8B66\u544A\uFF09\u3002")}const N=u();if(N){const C=o.join(N,"aws-runtime-bridge");if(m(C)){r("[runtime-bridge] \u6E05\u7406\u65E7\u7248\u672C\u6B8B\u7559\u76EE\u5F55...");try{Z(C,{recursive:!0,force:!0})}catch(x){const E=x instanceof Error?x:new Error(String(x));t(`[runtime-bridge] \u6E05\u7406\u6B8B\u7559\u76EE\u5F55\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u7EE7\u7EED\u5B89\u88C5\uFF09: ${E.message}`)}}}const M=["install","-g","aws-runtime-bridge@latest","--omit=optional"];b&&M.push("--force");const y=a("npm",M);if(y.error)return t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25: ${y.error.message}`),{handled:!0,exitCode:1};if(y.status!==0)return t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25\uFF0Cnpm \u9000\u51FA\u7801: ${y.status??"unknown"}`),{handled:!0,exitCode:y.status??1};if(i==="win32"){const C=await Oe(e,r,t);if(C.exitCode!==0)return C}return r("[runtime-bridge] \u66F4\u65B0\u5B8C\u6210\u3002\u8BF7\u91CD\u65B0\u8FD0\u884C awsb -v \u67E5\u770B\u5F53\u524D\u7248\u672C\u3002"),{handled:!0,exitCode:0}}async function Oe(e,r,t){const a=e.runCommand||p,i=a("sc.exe",["query",g]);if(!d(i))return{handled:!0,exitCode:0};r("[runtime-bridge] \u68C0\u6D4B\u5230 Windows \u670D\u52A1\u5DF2\u6CE8\u518C\uFF0C\u6B63\u5728\u91CD\u5EFA\u670D\u52A1\u6587\u4EF6\uFF08daemon/winsw.exe\uFF09..."),a("sc.exe",["stop",g]);const n=a("sc.exe",["delete",g]);d(n)||t(`[runtime-bridge] \u5220\u9664\u65E7 SCM \u8BB0\u5F55\u5931\u8D25: ${n.error?.message??`\u9000\u51FA\u7801 ${n.status??"unknown"}`}\uFF08\u5C1D\u8BD5\u7EE7\u7EED\u5B89\u88C5\uFF09`);const s={stdout:r,stderr:t,runCommand:a,platform:"win32",serviceUnitPath:e.serviceUnitPath||L,executablePath:e.executablePath||V(),windowsServiceManager:e.windowsServiceManager};try{return(await A(s)).exitCode!==0?(t("[runtime-bridge] \u91CD\u5EFA Windows \u670D\u52A1\u6587\u4EF6\u5931\u8D25\u3002\u5305\u5DF2\u66F4\u65B0\uFF0C\u8BF7\u624B\u52A8\u6267\u884C `awsb service uninstall && awsb service install` \u4FEE\u590D\u670D\u52A1\u3002"),{handled:!0,exitCode:0}):(r("[runtime-bridge] Windows \u670D\u52A1\u6587\u4EF6\u5DF2\u91CD\u5EFA\u3002"),{handled:!0,exitCode:0})}catch(c){const u=c instanceof Error?c:new Error(String(c));return t(`[runtime-bridge] \u91CD\u5EFA Windows \u670D\u52A1\u6587\u4EF6\u5F02\u5E38: ${u.message}\u3002\u5305\u5DF2\u66F4\u65B0\uFF0C\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u670D\u52A1\u3002`),{handled:!0,exitCode:0}}}function Fe(e,r,t){const i=(e.argv||process.argv.slice(2)).slice(1).some(u=>u==="--follow"||u==="-f"),n=e.platform||process.platform,s=e.runCommand||p,c=e.serviceUnitPath||L;if(n==="linux"){if(!m(c))return t("[runtime-bridge] awsb \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF08awsb service install\uFF09\uFF0C\u65E0\u65E5\u5FD7\u53EF\u67E5\u770B\u3002"),{handled:!0,exitCode:1};const u=["-u",f];i?u.push("-f"):u.push("-n","200","--no-pager");const l=s("journalctl",u);return l.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${l.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:l.status??0}}if(n==="win32"){const u=e.windowsServiceLogPath||G;if(!m(u))return t(`[runtime-bridge] \u5C1A\u672A\u751F\u6210\u65E5\u5FD7\u6587\u4EF6: ${u}\u3002\u8BF7\u5148\u6267\u884C \`awsb service install\` \u5E76\u8FD0\u884C\u670D\u52A1\u3002`),{handled:!0,exitCode:1};const l=`Get-Content -Encoding UTF8 -Tail 200 "${u}"`+(i?" -Wait":""),b=s("powershell",["-NoProfile","-Command",l]);return b.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${b.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:b.status??0}}if(n==="darwin"){const u=e.launchdLogPath||U;if(!m(u))return t(`[runtime-bridge] \u5C1A\u672A\u751F\u6210\u65E5\u5FD7\u6587\u4EF6: ${u}\u3002\u8BF7\u5148\u6267\u884C \`awsb service install\` \u5E76\u8FD0\u884C\u670D\u52A1\u3002`),{handled:!0,exitCode:1};const b=s("tail",i?["-f",u]:["-n","200",u]);return b.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${b.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:b.status??0}}return t("[runtime-bridge] awsb log \u4EC5\u652F\u6301 Linux systemd\u3001Windows \u670D\u52A1\u4E0E macOS launchd \u73AF\u5883\u3002"),{handled:!0,exitCode:1}}async function Je(e={}){const r=e.argv||process.argv.slice(2),t=r[0],a=e.stdout||oe,i=e.stderr||le;if(me(t))return a(`aws-runtime-bridge ${se(e.packageRoot)}`),{handled:!0,exitCode:0};if(t==="update")return await Me(e,a,i);if(t==="config"){const n=r[1];if(n==="example"||n==="minimal"||n==="--minimal"||n==="-m")return{handled:!0,exitCode:await(e.configureExample||re)()==="created"?0:1};const c=await(e.configure||ee)();return{handled:!0,exitCode:c==="configured"||c==="created"?0:1}}return t==="service"?await Ie(r,e,a,i):t==="log"?Fe(e,a,i):{handled:!1,exitCode:0}}export{he as createLaunchdPlistContent,ge as createSystemdUnitContent,Je as handleCliCommand,se as readPackageVersion};
3
+ `)}function me(e){return e==="-v"||e==="--version"||e==="version"}function fe(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}function K(){return process.argv[1]?c.resolve(process.argv[1]):H(import.meta.url)}function ge(e){return["[Unit]","Description=AgentsWorkStudio Runtime Bridge","After=network-online.target","Wants=network-online.target","","[Service]","Type=simple","Environment=AWS_BRIDGE_SKIP_SETUP=true",`ExecStart=${[process.execPath,e].map(t=>fe(t)).join(" ")}`,"Restart=on-failure","RestartSec=5s","","[Install]","WantedBy=multi-user.target",""].join(`
4
+ `)}function P(e){return e.replace(/&/g,"&amp;").replace(/</g,"&lt;").replace(/>/g,"&gt;")}function he(e,r={}){const t=r.userHomeDir??v.homedir(),s=r.logPath??L,i=s.replace(/\.out\.log$/,".err.log");return['<?xml version="1.0" encoding="UTF-8"?>','<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">','<plist version="1.0">',"<dict>"," <key>Label</key>",` <string>${P(N)}</string>`," <key>ProgramArguments</key>"," <array>",` <string>${P(process.execPath)}</string>`,` <string>${P(e)}</string>`," </array>"," <key>EnvironmentVariables</key>"," <dict>"," <key>AWS_BRIDGE_SKIP_SETUP</key>"," <string>true</string>"," <key>AWS_RUNTIME_HOME_DIR</key>",` <string>${P(t)}</string>`," </dict>"," <key>RunAtLoad</key>"," <true/>"," <key>KeepAlive</key>"," <true/>"," <key>StandardOutPath</key>",` <string>${P(s)}</string>`," <key>StandardErrorPath</key>",` <string>${P(i)}</string>`,"</dict>","</plist>",""].join(`
5
+ `)}function w(e,r){return e("systemctl",r)}function b(e,r){return r.error?`[runtime-bridge] ${e}\u5931\u8D25: ${r.error.message}`:`[runtime-bridge] ${e}\u5931\u8D25\uFF0Csystemctl \u9000\u51FA\u7801: ${r.status??"unknown"}`}function d(e){return!e.error&&e.status===0}function $(e,r){return e==="linux"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd \u73AF\u5883\u3002"),!1)}function R(e,r){return e==="darwin"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 macOS launchd \u73AF\u5883\u3002"),!1)}function Y(e){e("Usage: awsb service <install|uninstall|start|stop|restart>")}function we(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=ge(e.executablePath);try{T(c.dirname(e.serviceUnitPath),{recursive:!0}),G(e.serviceUnitPath,r,{encoding:"utf-8",mode:420})}catch(n){const a=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5199\u5165 systemd unit \u5931\u8D25: ${a.message}`),{handled:!0,exitCode:1}}const t=w(e.runCommand,["daemon-reload"]);if(!d(t))return e.stderr(b("\u91CD\u8F7D systemd",t)),{handled:!0,exitCode:t.status??1};const s=w(e.runCommand,["enable",h]);if(!d(s))return e.stderr(b("\u542F\u7528 awsb \u5F00\u673A\u81EA\u542F",s)),{handled:!0,exitCode:s.status??1};const i=w(e.runCommand,["is-active","--quiet",h]);if(i.error)return e.stderr(b("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",i)),{handled:!0,exitCode:1};if(i.status!==0){const n=w(e.runCommand,["start",h]);return d(n)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5B89\u88C5\u3001\u5DF2\u542F\u7528\u5F00\u673A\u81EA\u542F\u5E76\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(b("\u542F\u52A8 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}return e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u7528\u5F00\u673A\u81EA\u542F\uFF1B\u670D\u52A1\u5DF2\u5728\u8FD0\u884C\u3002"),{handled:!0,exitCode:0}}function be(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=g(e.serviceUnitPath),t=w(e.runCommand,["is-active","--quiet",h]);if(t.error)return e.stderr(b("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",t)),{handled:!0,exitCode:1};if(t.status===0){const n=w(e.runCommand,["stop",h]);if(!d(n))return e.stderr(b("\u505C\u6B62 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1}}const s=w(e.runCommand,["disable",h]);if(s.error||s.status!==0&&r)return e.stderr(b("\u7981\u7528 awsb \u5F00\u673A\u81EA\u542F",s)),{handled:!0,exitCode:s.status??1};if(r)try{F(e.serviceUnitPath)}catch(n){const a=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5220\u9664 systemd unit \u5931\u8D25: ${a.message}`),{handled:!0,exitCode:1}}else e.stdout("[runtime-bridge] awsb systemd unit \u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664\u3002");const i=w(e.runCommand,["daemon-reload"]);return d(i)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}):(e.stderr(b("\u91CD\u8F7D systemd",i)),{handled:!0,exitCode:i.status??1})}function Ce(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!g(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=w(e.runCommand,["restart",h]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(b("\u91CD\u542F awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function xe(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!g(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=w(e.runCommand,["start",h]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(b("\u542F\u52A8 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function Se(e){if(!$(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!g(e.serviceUnitPath))return e.stderr("[runtime-bridge] awsb systemd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const r=w(e.runCommand,["stop",h]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(b("\u505C\u6B62 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function ve(e){return e(`[runtime-bridge] \u672A\u68C0\u6D4B\u5230 node-windows \u6A21\u5757\u3002\u8BF7\u91CD\u65B0\u5B89\u88C5 aws-runtime-bridge\uFF1A
6
+ npm install -g aws-runtime-bridge`),{handled:!0,exitCode:1}}async function U(e){const r=e.windowsServiceManager;if(!r&&!se())return ve(e.stderr);const t=e.windowsServiceLogPath||B,s=c.dirname(e.executablePath),i=v.homedir();try{await(r||ne()).install({name:j,description:"awsb runtime bridge",script:e.executablePath,workingDirectory:s,env:[{name:"AWS_BRIDGE_SKIP_SETUP",value:"true"},{name:"AWS_RUNTIME_HOME_DIR",value:i}],logpath:q})}catch(a){const o=a instanceof Error?a:new Error(String(a));return e.stderr(`[runtime-bridge] \u5B89\u88C5 Windows \u670D\u52A1\u5931\u8D25: ${o.message}`),{handled:!0,exitCode:1}}const n=e.runCommand("sc.exe",["start",l]);return d(n)?(e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u52A8\uFF08\u65E5\u5FD7: ${t}\uFF09\u3002`),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u542F\u52A8 Windows \u670D\u52A1\u5931\u8D25: ${n.error?.message??`\u9000\u51FA\u7801 ${n.status??"unknown"}`}`),{handled:!0,exitCode:n.status??1})}function Pe(e){const r=s=>e.runCommand("sc.exe",s);r(["stop",l]);const t=r(["delete",l]);return d(t)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u5378\u8F7D Windows \u670D\u52A1\u5931\u8D25: ${t.error?.message??`\u9000\u51FA\u7801 ${t.status??"unknown"}`}`),{handled:!0,exitCode:t.status??1})}async function ye(e){e.runCommand("sc.exe",["stop",l]);const r=e.queryWindowsServiceState||A;return V(r,l)||e.stdout("[runtime-bridge] \u7B49\u5F85 Windows \u670D\u52A1\u505C\u6B62\u8D85\u65F6\uFF0C\u4ECD\u5C1D\u8BD5\u542F\u52A8\uFF08\u53EF\u80FD\u62A5 1056\uFF09\u3002"),J(e,"\u91CD\u542F")}async function Ee(e){return J(e,"\u542F\u52A8")}const $e=2,Re=1056;async function J(e,r){const t=e.runCommand("sc.exe",["start",l]);if(d(t))return e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2${r}\u3002`),{handled:!0,exitCode:0};if(t.status===Re)return e.stdout(`[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u5728\u8FD0\u884C\u4E2D\uFF08\u65E0\u9700${r}\uFF09\u3002`),{handled:!0,exitCode:0};if(t.status===$e){e.stdout("[runtime-bridge] \u68C0\u6D4B\u5230 Windows \u670D\u52A1\u4E8C\u8FDB\u5236\u6587\u4EF6\u4E22\u5931\uFF08winsw.exe\uFF09\uFF0C\u6B63\u5728\u91CD\u5EFA..."),e.runCommand("sc.exe",["delete",l]);try{const s=await U(e);return s.exitCode!==0&&e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25\u4E14\u81EA\u52A8\u91CD\u5EFA\u5931\u8D25\u3002\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u3002`),s}catch(s){const i=s instanceof Error?s:new Error(String(s));return e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25\u4E14\u81EA\u52A8\u91CD\u5EFA\u5F02\u5E38: ${i.message}\u3002\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u3002`),{handled:!0,exitCode:1}}}return e.stderr(`[runtime-bridge] ${r} Windows \u670D\u52A1\u5931\u8D25: ${t.error?.message??`\u9000\u51FA\u7801 ${t.status??"unknown"}`}`),{handled:!0,exitCode:t.status??1}}function We(e){const r=e.runCommand("sc.exe",["stop",l]);return d(r)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u505C\u6B62 Windows \u670D\u52A1\u5931\u8D25: ${r.error?.message??`\u9000\u51FA\u7801 ${r.status??"unknown"}`}`),{handled:!0,exitCode:r.status??1})}function pe(){return`gui/${process.getuid?.()??0}`}function W(e){return e.launchdDomainTarget||pe()}function p(e){return`${e}/${N}`}function _(e,r){return r.error?`[runtime-bridge] ${e}\u5931\u8D25: ${r.error.message}`:`[runtime-bridge] ${e}\u5931\u8D25\uFF0Claunchctl \u9000\u51FA\u7801: ${r.status??"unknown"}`}function _e(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||D,t=e.launchdLogPath||L,s=he(e.executablePath,{userHomeDir:v.homedir(),logPath:t});try{T(c.dirname(r),{recursive:!0}),T(c.dirname(t),{recursive:!0}),G(r,s,{encoding:"utf-8",mode:420})}catch(a){const o=a instanceof Error?a:new Error(String(a));return e.stderr(`[runtime-bridge] \u5199\u5165 launchd plist \u5931\u8D25: ${o.message}`),{handled:!0,exitCode:1}}const i=W(e);e.runCommand("launchctl",["bootout",p(i)]);const n=e.runCommand("launchctl",["bootstrap",i,r]);return d(n)?(e.stdout(`[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u5B89\u88C5\u5E76\u5DF2\u542F\u52A8\uFF08\u65E5\u5FD7: ${t}\uFF09\u3002`),{handled:!0,exitCode:0}):(e.stderr(_("\u5B89\u88C5 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}function De(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||D,t=W(e),s=p(t),i=e.runCommand("launchctl",["bootout",s]);if(i.error)return e.stderr(_("\u5378\u8F7D awsb \u670D\u52A1",i)),{handled:!0,exitCode:1};if(g(r))try{F(r)}catch(n){const a=n instanceof Error?n:new Error(String(n));return e.stderr(`[runtime-bridge] \u5220\u9664 launchd plist \u5931\u8D25: ${a.message}`),{handled:!0,exitCode:1}}else e.stdout("[runtime-bridge] awsb launchd plist \u4E0D\u5B58\u5728\uFF0C\u8DF3\u8FC7\u5220\u9664\u3002");return e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}}function ke(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||D;if(!g(r))return e.stderr("[runtime-bridge] awsb launchd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const t=W(e),s=p(t),i=e.runCommand("launchctl",["kickstart",s]);if(d(i))return e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0};const n=e.runCommand("launchctl",["bootstrap",t,r]);return d(n)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(_("\u542F\u52A8 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1})}function Te(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=W(e),t=e.runCommand("launchctl",["bootout",p(r)]);return d(t)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(_("\u505C\u6B62 awsb \u670D\u52A1",t)),{handled:!0,exitCode:t.status??1})}function Ie(e){if(!R(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=e.launchdPlistPath||D;if(!g(r))return e.stderr("[runtime-bridge] awsb launchd \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF0C\u8BF7\u5148\u6267\u884C `awsb service install`\u3002"),{handled:!0,exitCode:1};const t=W(e);e.runCommand("launchctl",["bootout",p(t)]);const s=e.runCommand("launchctl",["bootstrap",t,r]);return d(s)?(e.stdout("[runtime-bridge] awsb launchd \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(_("\u91CD\u542F awsb \u670D\u52A1",s)),{handled:!0,exitCode:s.status??1})}async function Ne(e,r,t,s){const i=e[1];if(!i||i==="-h"||i==="--help"||i==="help")return Y(t),{handled:!0,exitCode:i?0:1};const n={stdout:t,stderr:s,runCommand:r.runCommand||k,platform:r.platform||process.platform,serviceUnitPath:r.serviceUnitPath||I,executablePath:r.executablePath||K(),windowsServiceManager:r.windowsServiceManager,launchdPlistPath:r.launchdPlistPath,launchdLogPath:r.launchdLogPath,launchdDomainTarget:r.launchdDomainTarget,queryWindowsServiceState:r.queryWindowsServiceState||A};return n.platform!=="linux"&&n.platform!=="win32"&&n.platform!=="darwin"?(s("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd\u3001Windows \u670D\u52A1\u4E0E macOS launchd \u73AF\u5883\u3002"),{handled:!0,exitCode:1}):i==="install"?n.platform==="win32"?await U(n):n.platform==="darwin"?_e(n):we(n):i==="uninstall"?n.platform==="win32"?Pe(n):n.platform==="darwin"?De(n):be(n):i==="restart"?n.platform==="win32"?ye(n):n.platform==="darwin"?Ie(n):Ce(n):i==="start"?n.platform==="win32"?Ee(n):n.platform==="darwin"?ke(n):xe(n):i==="stop"?n.platform==="win32"?We(n):n.platform==="darwin"?Te(n):Se(n):(s(`[runtime-bridge] \u672A\u77E5 service \u5B50\u547D\u4EE4: ${i}`),Y(t),{handled:!0,exitCode:1})}function Q(e){return e?e.split(/\r?\n/).map(r=>r.trim()).filter(r=>/^\d+$/.test(r)).map(Number):[]}function Le(e){try{if(e==="win32"){const t=f("powershell",["-NoProfile","-Command",`Get-CimInstance Win32_Process -Filter "Name='node.exe'" | Where-Object { $_.CommandLine -like '*aws-runtime-bridge*' -and $_.ProcessId -ne ${process.pid} } | ForEach-Object { $_.ProcessId }`],{encoding:"utf-8"}),s=Ae(e);return Ue([...Q(t.stdout),...s]).filter(i=>i!==process.pid)}const r=f("pgrep",["-f","aws-runtime-bridge"],{encoding:"utf-8"});return Q(r.stdout).filter(t=>t!==process.pid)}catch{return[]}}function Ae(e){if(e!=="win32")return[];try{const r=f("sc.exe",["query",l],{encoding:"utf-8",shell:!0,stdio:["ignore","pipe","ignore"]});if(r.status!==0||!r.stdout)return[];const t=r.stdout.match(/STATE\s*:\s*\d+\s+(\S+)/);if(!t)return[];const s=t[1];if(s!=="RUNNING"&&s!=="START_PENDING"&&s!=="STOP_PENDING")return[];const i=f("tasklist",["/FI","IMAGENAME eq awsb.exe","/FO","CSV","/NH"],{encoding:"utf-8",shell:!0,stdio:["ignore","pipe","ignore"]});return i.status!==0||!i.stdout?[]:i.stdout.split(/\r?\n/).map(n=>n.trim()).filter(n=>n.length>0).map(n=>{const a=n.match(/^"awsb\.exe","(\d+)"/i);return a?Number(a[1]):NaN}).filter(n=>Number.isFinite(n)&&n>0)}catch{return[]}}function Ue(e){const r=new Set,t=[];for(const s of e)r.has(s)||(r.add(s),t.push(s));return t}function Me(e,r){if(r==="win32"){if(Oe(e,r)){f("sc.exe",["stop",l],{stdio:"ignore",shell:!0}),V(A,l);return}f("taskkill",["/PID",String(e),"/F","/T"],{stdio:"ignore"});return}f("kill",["-9",String(e)],{stdio:"ignore"})}function Oe(e,r){if(r!=="win32"||!Number.isFinite(e)||e<=0)return!1;try{const t=f("powershell",["-NoProfile","-Command",`(Get-CimInstance Win32_Process -Filter "ProcessId=${e}" -ErrorAction SilentlyContinue).Name`],{encoding:"utf-8",stdio:["ignore","pipe","ignore"]});return t.status!==0||!t.stdout?!1:/^awsb\.exe$/i.test(t.stdout.trim())}catch{return!1}}function Fe(e){if(!process.stdin.isTTY)return Promise.resolve("n");const r=X({input:process.stdin,output:process.stdout});return r.question(e).finally(()=>r.close())}async function Ge(e,r,t){const s=e.runCommand||k,i=e.platform||process.platform,n=e.findRunningBridgeProcesses||Le,a=e.prompt||Fe,o=e.killBridgeProcess||Me,u=e.globalModulePath||de;r("[runtime-bridge] \u6B63\u5728\u66F4\u65B0 aws-runtime-bridge \u5230\u6700\u65B0\u7248\u672C...");const m=n(i).filter(x=>x!==process.pid);let C=!1;if(m.length>0){r(`[runtime-bridge] \u68C0\u6D4B\u5230 aws-runtime-bridge \u6B63\u5728\u8FD0\u884C (PID: ${m.join(", ")})\uFF0C\u66F4\u65B0\u65F6\u8FD9\u4E9B\u8FDB\u7A0B\u53EF\u80FD\u5360\u7528\u6587\u4EF6\u5BFC\u81F4\u8986\u76D6\u5931\u8D25\u3002`);const x=(await a("\u662F\u5426\u5173\u95ED\u4E0A\u8FF0\u8FDB\u7A0B\u4EE5\u7EE7\u7EED\u66F4\u65B0? (y/N) ")).trim().toLowerCase();if(x==="y"||x==="yes")for(const S of m)try{o(S,i),r(`[runtime-bridge] \u5DF2\u5173\u95ED\u8FDB\u7A0B PID: ${S}`)}catch(E){const z=E instanceof Error?E:new Error(String(E));t(`[runtime-bridge] \u5173\u95ED\u8FDB\u7A0B PID ${S} \u5931\u8D25: ${z.message}`)}else C=!0,r("[runtime-bridge] \u5DF2\u9009\u62E9\u4E0D\u5173\u95ED\u8FDB\u7A0B\uFF0C\u5C06\u4EE5 --force \u65B9\u5F0F\u5F3A\u5236\u5B89\u88C5\uFF08\u82E5\u6587\u4EF6\u4ECD\u88AB\u5360\u7528\u53EF\u80FD\u4F1A\u6709\u8B66\u544A\uFF09\u3002")}const M=u();if(M){const x=c.join(M,"aws-runtime-bridge");if(g(x)){r("[runtime-bridge] \u6E05\u7406\u65E7\u7248\u672C\u6B8B\u7559\u76EE\u5F55...");try{ee(x,{recursive:!0,force:!0})}catch(S){const E=S instanceof Error?S:new Error(String(S));t(`[runtime-bridge] \u6E05\u7406\u6B8B\u7559\u76EE\u5F55\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u7EE7\u7EED\u5B89\u88C5\uFF09: ${E.message}`)}}}const O=["install","-g","aws-runtime-bridge@latest","--omit=optional"];C&&O.push("--force");const y=s("npm",O);if(y.error)return t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25: ${y.error.message}`),{handled:!0,exitCode:1};if(y.status!==0)return t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25\uFF0Cnpm \u9000\u51FA\u7801: ${y.status??"unknown"}`),{handled:!0,exitCode:y.status??1};if(i==="win32"){const x=await He(e,r,t);if(x.exitCode!==0)return x}return r("[runtime-bridge] \u66F4\u65B0\u5B8C\u6210\u3002\u8BF7\u91CD\u65B0\u8FD0\u884C awsb -v \u67E5\u770B\u5F53\u524D\u7248\u672C\u3002"),{handled:!0,exitCode:0}}async function He(e,r,t){const s=e.runCommand||k,i=s("sc.exe",["query",l]);if(!d(i))return{handled:!0,exitCode:0};r("[runtime-bridge] \u68C0\u6D4B\u5230 Windows \u670D\u52A1\u5DF2\u6CE8\u518C\uFF0C\u6B63\u5728\u91CD\u5EFA\u670D\u52A1\u6587\u4EF6\uFF08daemon/winsw.exe\uFF09..."),s("sc.exe",["stop",l]);const n=s("sc.exe",["delete",l]);d(n)||t(`[runtime-bridge] \u5220\u9664\u65E7 SCM \u8BB0\u5F55\u5931\u8D25: ${n.error?.message??`\u9000\u51FA\u7801 ${n.status??"unknown"}`}\uFF08\u5C1D\u8BD5\u7EE7\u7EED\u5B89\u88C5\uFF09`);const a={stdout:r,stderr:t,runCommand:s,platform:"win32",serviceUnitPath:e.serviceUnitPath||I,executablePath:e.executablePath||K(),windowsServiceManager:e.windowsServiceManager};try{return(await U(a)).exitCode!==0?(t("[runtime-bridge] \u91CD\u5EFA Windows \u670D\u52A1\u6587\u4EF6\u5931\u8D25\u3002\u5305\u5DF2\u66F4\u65B0\uFF0C\u8BF7\u624B\u52A8\u6267\u884C `awsb service uninstall && awsb service install` \u4FEE\u590D\u670D\u52A1\u3002"),{handled:!0,exitCode:0}):(r("[runtime-bridge] Windows \u670D\u52A1\u6587\u4EF6\u5DF2\u91CD\u5EFA\u3002"),{handled:!0,exitCode:0})}catch(o){const u=o instanceof Error?o:new Error(String(o));return t(`[runtime-bridge] \u91CD\u5EFA Windows \u670D\u52A1\u6587\u4EF6\u5F02\u5E38: ${u.message}\u3002\u5305\u5DF2\u66F4\u65B0\uFF0C\u8BF7\u624B\u52A8\u6267\u884C \`awsb service uninstall && awsb service install\` \u4FEE\u590D\u670D\u52A1\u3002`),{handled:!0,exitCode:0}}}function je(e,r,t){const i=(e.argv||process.argv.slice(2)).slice(1).some(u=>u==="--follow"||u==="-f"),n=e.platform||process.platform,a=e.runCommand||k,o=e.serviceUnitPath||I;if(n==="linux"){if(!g(o))return t("[runtime-bridge] awsb \u670D\u52A1\u5C1A\u672A\u5B89\u88C5\uFF08awsb service install\uFF09\uFF0C\u65E0\u65E5\u5FD7\u53EF\u67E5\u770B\u3002"),{handled:!0,exitCode:1};const u=["-u",h];i?u.push("-f"):u.push("-n","200","--no-pager");const m=a("journalctl",u);return m.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${m.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:m.status??0}}if(n==="win32"){const u=e.windowsServiceLogPath||B;if(!g(u))return t(`[runtime-bridge] \u5C1A\u672A\u751F\u6210\u65E5\u5FD7\u6587\u4EF6: ${u}\u3002\u8BF7\u5148\u6267\u884C \`awsb service install\` \u5E76\u8FD0\u884C\u670D\u52A1\u3002`),{handled:!0,exitCode:1};const m=`Get-Content -Encoding UTF8 -Tail 200 "${u}"`+(i?" -Wait":""),C=a("powershell",["-NoProfile","-Command",m]);return C.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${C.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:C.status??0}}if(n==="darwin"){const u=e.launchdLogPath||L;if(!g(u))return t(`[runtime-bridge] \u5C1A\u672A\u751F\u6210\u65E5\u5FD7\u6587\u4EF6: ${u}\u3002\u8BF7\u5148\u6267\u884C \`awsb service install\` \u5E76\u8FD0\u884C\u670D\u52A1\u3002`),{handled:!0,exitCode:1};const C=a("tail",i?["-f",u]:["-n","200",u]);return C.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${C.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:C.status??0}}return t("[runtime-bridge] awsb log \u4EC5\u652F\u6301 Linux systemd\u3001Windows \u670D\u52A1\u4E0E macOS launchd \u73AF\u5883\u3002"),{handled:!0,exitCode:1}}async function Xe(e={}){const r=e.argv||process.argv.slice(2),t=r[0],s=e.stdout||ce,i=e.stderr||le;if(me(t))return s(`aws-runtime-bridge ${ue(e.packageRoot)}`),{handled:!0,exitCode:0};if(t==="update")return await Ge(e,s,i);if(t==="config"){const n=r[1];if(n==="example"||n==="minimal"||n==="--minimal"||n==="-m")return{handled:!0,exitCode:await(e.configureExample||te)()==="created"?0:1};const o=await(e.configure||re)();return{handled:!0,exitCode:o==="configured"||o==="created"?0:1}}return t==="service"?await Ne(r,e,s,i):t==="log"?je(e,s,i):{handled:!1,exitCode:0}}export{he as createLaunchdPlistContent,ge as createSystemdUnitContent,Le as defaultFindRunningBridgeProcesses,Me as defaultKillBridgeProcess,Xe as handleCliCommand,ue as readPackageVersion};
7
7
 
@@ -0,0 +1,17 @@
1
+
2
+ import type { ACodeBuiltInTool } from "./built-in-file-tools.js";
3
+ import type { AwaitBgCommandResult } from "./background-command-manager.js";
4
+ export interface AwaitCompleteSubAgentResult {
5
+ subId: string;
6
+ status: string;
7
+ output: string;
8
+ error?: string;
9
+ }
10
+ export interface ACodeAwaitCompleteDeps {
11
+ agentId: string;
12
+
13
+ awaitSubAgents: ((subIds: string[], timeoutMs?: number) => Promise<AwaitCompleteSubAgentResult[]>) | null;
14
+
15
+ awaitBgCommands: (taskIds: string[], timeoutMs?: number) => Promise<Array<AwaitBgCommandResult | null>>;
16
+ }
17
+ export declare function createAwaitCompleteTool(deps: ACodeAwaitCompleteDeps): ACodeBuiltInTool;
@@ -0,0 +1,21 @@
1
+ const p=`Block and wait for one or more sub-agents AND/OR background commands to complete.
2
+
3
+ This is the unified replacement for the old await_sub_agents: it can wait for BOTH
4
+ sub-agents (spawned via sub_agent) and background commands (started via Bash with
5
+ wait_seconds=0 or n) in a single call.
6
+
7
+ All specified items must reach a terminal state before this returns (JOIN semantics):
8
+ - sub-agent: completed / failed / cancelled
9
+ - background command: completed / failed / killed
10
+
11
+ Returns a structured result with each item's outcome. For background commands only a
12
+ short status + exitCode is returned; use ReadBackgroundOutput(task_id, mode="tail")
13
+ to fetch full output.
14
+
15
+ Guidelines:
16
+ - Pass sub-agent IDs in 'sub_agent_ids' and/or background command task_ids in 'bg_cmd_ids'
17
+ - At least one of the two arrays must be non-empty
18
+ - Use timeout_ms to avoid waiting forever (0 = no timeout, default 0)
19
+ - After this returns, call poll_message to receive any pending messages`;function h(o){return{kind:"builtin",toolName:"await_complete",exposedName:"await_complete",description:p,inputSchema:{type:"object",properties:{sub_agent_ids:{type:"array",items:{type:"string"},description:"Array of sub-agent IDs to wait for (from sub_agent). Empty if none."},bg_cmd_ids:{type:"array",items:{type:"string"},description:"Array of background command task_ids to wait for (from Bash wait_seconds or ListBackgroundCommands). Empty if none."},timeout_ms:{type:"number",description:"Maximum time to wait in milliseconds, applied to the whole JOIN. 0 = no timeout (wait forever). Default: 0."}},additionalProperties:!1},async execute(i){const l=typeof i.timeout_ms=="number"?i.timeout_ms:0,m=i.sub_agent_ids,c=i.bg_cmd_ids,n=Array.isArray(m)?m.filter(t=>typeof t=="string"&&t.trim().length>0):[],u=Array.isArray(c)?c.filter(t=>typeof t=="string"&&t.trim().length>0):[];if(n.length===0&&u.length===0)return{title:"await_complete validation error",output:"Invalid input: provide at least one of 'sub_agent_ids' or 'bg_cmd_ids'.",metadata:{error:!0}};if(n.length>0&&!o.awaitSubAgents)return{title:"await_complete error",output:"sub_agent_ids provided but sub-agent manager is not enabled for this agent.",metadata:{error:!0}};try{let t=[];n.length>0&&o.awaitSubAgents&&(t=await o.awaitSubAgents(n,l)??[]);let a=[];u.length>0&&(a=await o.awaitBgCommands(u,l));const e=[],g=[],d=[];if(t.length>0){e.push(`=== ${t.length} sub-agent(s) ===`);for(const s of t)e.push(`[${s.status}] ${s.subId}`),s.error?(e.push("<task_error>"),e.push(s.error),e.push("</task_error>")):(e.push("<task_result>"),e.push(s.output||"(empty output)"),e.push("</task_result>")),e.push(""),g.push(s)}if(a.length>0){e.push(`=== ${a.length} background command(s) ===`);for(const s of a){if(!s){e.push("[not_found] (unknown task_id)"),e.push(""),d.push(null);continue}const r=[];s.timedOut&&r.push("timed_out"),s.aborted&&r.push("aborted"),e.push(`[${s.status}] ${s.taskId} exit=${s.exitCode??"?"}`+(r.length?` (${r.join(",")})`:"")),e.push(""),d.push(s)}}return{title:`awaited ${t.length+a.length} item(s)`,output:e.join(`
20
+ `),metadata:{total:t.length+a.length,sub_agents:g,bg_cmds:d}}}catch(t){return{title:"await_complete error",output:`Failed to await: ${t instanceof Error?t.message:String(t)}`,metadata:{error:!0}}}}}}export{h as createAwaitCompleteTool};
21
+
@@ -0,0 +1,148 @@
1
+
2
+ import { type BackgroundTaskStatus } from "./background-command-store.js";
3
+ export type { BackgroundTaskRecord, BackgroundTaskStatus } from "./background-command-store.js";
4
+
5
+ export interface BackgroundCommandResult {
6
+ taskId: string;
7
+ exitCode: number | null;
8
+ signal: string | null;
9
+ timedOut: boolean;
10
+ stdout: string;
11
+ stderr: string;
12
+ durationMs: number;
13
+ }
14
+
15
+ export interface AwaitBgCommandResult {
16
+ taskId: string;
17
+
18
+ status: BackgroundTaskStatus;
19
+ exitCode: number | null;
20
+ signal: string | null;
21
+ timedOut: boolean;
22
+
23
+ aborted: boolean;
24
+ durationMs: number;
25
+ }
26
+
27
+ export interface StartCommandInput {
28
+ commandText: string;
29
+ agentId: string;
30
+ workspaceRoot: string;
31
+ cwd: string;
32
+ timeoutMs?: number;
33
+ onOutput?: (event: {
34
+ stream: "stdout" | "stderr";
35
+ text: string;
36
+ }) => void;
37
+
38
+ onCompleted?: (event: BackgroundCompletedEvent) => void;
39
+ }
40
+ export interface BackgroundCompletedEvent {
41
+ taskId: string;
42
+ agentId: string;
43
+ status: BackgroundTaskStatus;
44
+ exitCode: number | null;
45
+ signal: string | null;
46
+ commandPreview: string;
47
+ outputLineCount: number;
48
+ outputBytes: number;
49
+ outputPath: string;
50
+ finishedAt: number;
51
+ killedReason?: string | null;
52
+ }
53
+ export interface TaskSummary {
54
+ taskId: string;
55
+ status: BackgroundTaskStatus;
56
+ command: string;
57
+ commandPreview: string;
58
+ cwd: string;
59
+ startedAt: number;
60
+ finishedAt: number | null;
61
+ exitCode: number | null;
62
+ signal: string | null;
63
+ outputLineCount: number;
64
+ outputBytes: number;
65
+ }
66
+ export interface ReadOutputArgs {
67
+ task_id: string;
68
+ mode?: "head" | "tail" | "offset" | "grep";
69
+ lines?: number;
70
+ offset?: number;
71
+ pattern?: string;
72
+ include_metadata?: boolean;
73
+ }
74
+ export interface ReadOutputResult {
75
+ taskId: string;
76
+ status: BackgroundTaskStatus;
77
+ mode: string;
78
+ lines: string[];
79
+ lineCount: number;
80
+ metadata?: {
81
+ totalLines: number;
82
+ totalBytes: number;
83
+ returnedBytes: number;
84
+ truncated: boolean;
85
+ hasMore: boolean;
86
+ outputFile: string;
87
+ };
88
+ }
89
+ export interface KillResult {
90
+ taskId: string;
91
+ status: BackgroundTaskStatus;
92
+ signal?: string;
93
+ exitCode?: number | null;
94
+ error?: string;
95
+ reason?: string;
96
+ }
97
+ export declare class BackgroundCommandManager {
98
+ private tasksByAgent;
99
+ private store;
100
+
101
+ private globalCount;
102
+
103
+ startCommand(input: StartCommandInput): {
104
+ taskId: string;
105
+ completion: Promise<BackgroundCommandResult>;
106
+ };
107
+ private spawnAndAttach;
108
+ private onOutput;
109
+ private appendToFile;
110
+ private finalizeTask;
111
+
112
+ private evictTask;
113
+ listTasks(agentId: string, statusFilter?: string): TaskSummary[];
114
+ private toSummary;
115
+ readOutput(agentId: string, args: ReadOutputArgs): Promise<ReadOutputResult>;
116
+
117
+ private readFromFile;
118
+
119
+ awaitCompletion(agentId: string, taskIds: string[], timeoutMs?: number): Promise<Array<AwaitBgCommandResult | null>>;
120
+ private awaitOne;
121
+ private taskToAwaitResult;
122
+
123
+ interruptAwaits(agentId: string, taskIds: Iterable<string>): void;
124
+ killTask(agentId: string, taskId: string): Promise<KillResult>;
125
+
126
+ private killProcessGroup;
127
+
128
+ disposeAgent(agentId: string): Promise<void>;
129
+
130
+ disposeAll(): Promise<void>;
131
+
132
+ reconcileOrphans(agentId: string): Promise<void>;
133
+
134
+ readSessionState(agentId: string): Promise<import("./background-command-store.js").BackgroundSessionState | null>;
135
+
136
+ markNotified(agentId: string, taskIds: string[]): Promise<void>;
137
+ private getOrCreateBucket;
138
+ private getTask;
139
+ private generateTaskId;
140
+ private persistTask;
141
+ }
142
+
143
+ export declare class BackgroundTaskError extends Error {
144
+ readonly code: string;
145
+ constructor(code: string, message: string);
146
+ }
147
+
148
+ export declare const backgroundCommandManager: BackgroundCommandManager;
@@ -0,0 +1,8 @@
1
+ import{spawn as S}from"node:child_process";import{promises as g}from"node:fs";import{createInterface as P}from"node:readline";import{randomBytes as T}from"node:crypto";import{prepareShellCommand as I,shellExecutable as L}from"./builtins/shellCommandPreparation.js";import{createShellOutputDecoder as w,decodeShellOutputChunk as p}from"./builtins/shellOutputDecoder.js";import{BackgroundOutputRingBuffer as O}from"./background-output-ringbuffer.js";import{BackgroundCommandStore as R}from"./background-command-store.js";const b=1e3,A=10*60*1e3,B=50*1024*1024,y=16,C=256,x=256*1024,_=500,v=5e3,D=256,k=5*60*1e3;class z{constructor(){this.tasksByAgent=new Map,this.store=new R}globalCount(){let t=0;for(const i of this.tasksByAgent.values())t+=i.size;return t}startCommand(t){const i=t.agentId,e=this.getOrCreateBucket(i);if(e.size>=y)throw new M("too_many_tasks",`Agent ${i} has ${e.size} background tasks (limit ${y}). Kill or wait for completion first.`);if(this.globalCount()>=C)throw new M("global_limit_reached",`Global background task limit ${C} reached.`);const n=this.generateTaskId(e),o=Math.min(t.timeoutMs??A,A),s=this.store.outputPathFor(i,n),u=t.commandText.length>80?`${t.commandText.slice(0,77)}...`:t.commandText,r={taskId:n,agentId:i,command:t.commandText,commandPreview:u,cwd:t.cwd,status:"running",startedAt:Date.now(),finishedAt:null,exitCode:null,signal:null,killedReason:null,timeoutMs:o,child:null,ringBuffer:new O,outputPath:s,outputWriteChain:Promise.resolve(),outputTruncated:!1,outputLineCount:0,outputBytes:0,notified:!1,completionWaiters:[],onCompleted:t.onCompleted,onOutput:t.onOutput,timeoutTimer:null,killEscalationTimer:null,retentionTimer:null,finalized:!1,stdoutDecoder:w(),stderrDecoder:w()},l=new Promise(a=>{r.completionResolve=a});return e.set(n,r),this.spawnAndAttach(r,t).catch(a=>{r.ringBuffer.push(`[ACode] spawn failed: ${a?.message??String(a)}
2
+ `),this.finalizeTask(r,-1,null,"spawn_error")}),this.persistTask(r),{taskId:n,completion:l}}async spawnAndAttach(t,i){const e=L(),n=await I(i.commandText,i.workspaceRoot,e),o=process.platform==="win32",s=S(e.command,[...e.argsPrefix,n.commandText],{cwd:i.cwd,env:process.env,detached:!o,stdio:["ignore","pipe","pipe"],windowsVerbatimArguments:o,windowsHide:!0});t.child=s,s.stdout?.on("data",u=>{const r=p(t.stdoutDecoder,u);this.onOutput(t,"stdout",r)}),s.stderr?.on("data",u=>{const r=p(t.stderrDecoder,u);this.onOutput(t,"stderr",r)}),s.on("error",u=>{this.onOutput(t,"stderr",`[ACode] child error: ${u.message}
3
+ `),t.finalized||this.finalizeTask(t,-1,null,"child_error")}),s.on("close",(u,r)=>{const l=p(t.stdoutDecoder,void 0,!0),a=p(t.stderrDecoder,void 0,!0);if(l&&this.onOutput(t,"stdout",l),a&&this.onOutput(t,"stderr",a),t.ringBuffer.flush(),!t.finalized){const c=t.killedReason??null;this.finalizeTask(t,u,r,c??void 0)}}),t.timeoutTimer=setTimeout(()=>{t.finalized||(t.killedReason="timeout",this.killProcessGroup(s))},t.timeoutMs),t.timeoutTimer.unref?.()}onOutput(t,i,e){if(!e)return;if(t.outputBytes>=B){if(!t.outputTruncated){t.outputTruncated=!0;const o=`
4
+ [ACode] output truncated at 50MB
5
+ `;t.ringBuffer.push(o),this.appendToFile(t,o)}t.ringBuffer.push(e);return}t.ringBuffer.push(e),t.onOutput?.({stream:i,text:e});const n=(e.match(/\n/g)??[]).length;t.outputLineCount+=n,t.outputBytes+=Buffer.byteLength(e,"utf-8"),this.appendToFile(t,e)}appendToFile(t,i){return t.outputBytes>B?Promise.resolve():(t.outputWriteChain=t.outputWriteChain.then(()=>g.appendFile(t.outputPath,i,"utf-8")).catch(e=>{t.ringBuffer.push(`[ACode] output write failed: ${e?.message??e}
6
+ `)}),t.outputWriteChain)}finalizeTask(t,i,e,n){if(t.finalized)return;t.finalized=!0,t.timeoutTimer&&(clearTimeout(t.timeoutTimer),t.timeoutTimer=null),t.killEscalationTimer&&(clearTimeout(t.killEscalationTimer),t.killEscalationTimer=null),t.child=null,t.finishedAt=Date.now();let o;n?(o="killed",t.killedReason=n):i===0?o="completed":o="failed",t.status=o,t.exitCode=i,t.signal=e;const s=t.ringBuffer.tail(1e3),u={taskId:t.taskId,exitCode:i,signal:e,timedOut:n==="timeout",stdout:s.join(`
7
+ `),stderr:"",durationMs:t.finishedAt-t.startedAt};t.completionResolve?.(u);const r={taskId:t.taskId,status:o,exitCode:i,signal:e,timedOut:n==="timeout",aborted:!1,durationMs:t.finishedAt-t.startedAt},l=t.completionWaiters;t.completionWaiters=[];for(const a of l)a(r);this.persistTask(t),t.onCompleted?.({taskId:t.taskId,agentId:t.agentId,status:o,exitCode:i,signal:e,commandPreview:t.commandPreview,outputLineCount:t.outputLineCount,outputBytes:t.outputBytes,outputPath:t.outputPath,finishedAt:t.finishedAt,killedReason:t.killedReason}),t.retentionTimer=setTimeout(()=>{this.evictTask(t.agentId,t.taskId)},k),t.retentionTimer.unref?.()}evictTask(t,i){const e=this.tasksByAgent.get(t);if(!e)return;const n=e.get(i);n&&(g.unlink(n.outputPath).catch(()=>{}),e.delete(i),e.size===0&&this.tasksByAgent.delete(t))}listTasks(t,i){const e=this.tasksByAgent.get(t);if(!e)return[];const n=[];for(const o of e.values())i&&i!=="all"&&o.status!==i||n.push(this.toSummary(o));return n.sort((o,s)=>s.startedAt-o.startedAt)}toSummary(t){return{taskId:t.taskId,status:t.status,command:t.command,commandPreview:t.commandPreview,cwd:t.cwd,startedAt:t.startedAt,finishedAt:t.finishedAt,exitCode:t.exitCode,signal:t.signal,outputLineCount:t.outputLineCount,outputBytes:t.outputBytes}}async readOutput(t,i){const e=String(i.task_id??""),n=this.getTask(t,e),o=i.mode??"tail",s=Math.min(Math.max(1,i.lines??50),_);if(!n)return{taskId:e,status:"killed",mode:o,lines:[],lineCount:0,metadata:{totalLines:0,totalBytes:0,returnedBytes:0,truncated:!1,hasMore:!1,outputFile:""}};let u=[],r=!1;if(o==="head")u=n.ringBuffer.head(s);else if(o==="tail")u=n.ringBuffer.tail(s);else if(o==="offset"){const d=Math.max(0,i.offset??0),f=n.ringBuffer.offset(d,s);if(f.length>0||n.ringBuffer.hasLine(d))u=f;else{const h=await this.readFromFile(n,"offset",d,s,void 0);u=h.lines,r=h.hasMore}}else if(o==="grep"){const d=String(i.pattern??"");if(d.length>D)return{taskId:e,status:n.status,mode:o,lines:[],lineCount:0,metadata:{totalLines:n.outputLineCount,totalBytes:n.outputBytes,returnedBytes:0,truncated:!1,hasMore:!1,outputFile:n.outputPath}};let f;try{f=new RegExp(d,"i")}catch{return{taskId:e,status:n.status,mode:o,lines:[],lineCount:0,metadata:{totalLines:n.outputLineCount,totalBytes:n.outputBytes,returnedBytes:0,truncated:!1,hasMore:!1,outputFile:n.outputPath}}}const h=await this.readFromFile(n,"grep",0,s,f);u=h.lines,r=h.hasMore}let l=0;const a=[];let c=!1;for(const d of u){if(a.length>=_){c=!0,r=!0;break}const f=Buffer.byteLength(d,"utf-8")+1;if(l+f>x){c=!0,r=!0;break}a.push(d),l+=f}const m=i.include_metadata!==!1;return{taskId:e,status:n.status,mode:o,lines:a,lineCount:a.length,metadata:m?{totalLines:n.outputLineCount,totalBytes:n.outputBytes,returnedBytes:l,truncated:c,hasMore:r,outputFile:n.outputPath}:void 0}}async readFromFile(t,i,e,n,o){const s=[];let u=!1,r=0,l=0;try{const a=await g.open(t.outputPath,"r"),c=P({input:a.createReadStream({encoding:"utf-8"}),crlfDelay:1/0});for await(const m of c){if(i==="grep"&&s.length>=v){u=!0;break}if(i==="offset"){if(r<e){r++;continue}if(l>=n){u=!0;break}s.push(m),l++,r++}else{if(o.test(m)){if(l>=n){u=!0;break}s.push(m),l++}r++}}c.close(),await a.close()}catch{return{lines:[],hasMore:!1}}return{lines:s,hasMore:u}}async awaitCompletion(t,i,e){return Promise.all(i.map(n=>this.awaitOne(t,n,e)))}async awaitOne(t,i,e){const n=this.getTask(t,i);if(n)return n.status!=="running"?this.taskToAwaitResult(n):new Promise(u=>{let r=null;const l=a=>{r&&clearTimeout(r),u(a)};n.completionWaiters.push(l),e&&e>0&&(r=setTimeout(()=>{n.completionWaiters=n.completionWaiters.filter(a=>a!==l),u({taskId:i,status:"running",exitCode:null,signal:null,timedOut:!0,aborted:!1,durationMs:Date.now()-n.startedAt})},e),r.unref?.())});const s=(await this.store.readSession(t))?.tasks.find(u=>u.taskId===i);return s?s.status!=="running"?{taskId:i,status:s.status,exitCode:s.exitCode,signal:s.signal,timedOut:s.killedReason==="timeout",aborted:!1,durationMs:(s.finishedAt??Date.now())-s.startedAt}:{taskId:i,status:"killed",exitCode:null,signal:null,timedOut:!1,aborted:!1,durationMs:(s.finishedAt??Date.now())-s.startedAt}:null}taskToAwaitResult(t){return{taskId:t.taskId,status:t.status,exitCode:t.exitCode,signal:t.signal,timedOut:t.killedReason==="timeout",aborted:!1,durationMs:(t.finishedAt??Date.now())-t.startedAt}}interruptAwaits(t,i){const e=this.tasksByAgent.get(t);if(e)for(const n of i){const o=e.get(n);if(!o)continue;const s=o.completionWaiters;if(s.length===0)continue;o.completionWaiters=[];const u={taskId:n,status:"running",exitCode:null,signal:null,timedOut:!1,aborted:!0,durationMs:Date.now()-o.startedAt};for(const r of s)r(u)}}async killTask(t,i){const e=this.getTask(t,i);return e?e.status!=="running"?{taskId:i,status:e.status,exitCode:e.exitCode??void 0}:e.child?(e.killedReason="user_kill",this.killProcessGroup(e.child)?(e.killEscalationTimer&&clearTimeout(e.killEscalationTimer),e.killEscalationTimer=setTimeout(()=>{e.child&&!e.finalized&&this.killProcessGroup(e.child,!0)},b),e.killEscalationTimer.unref?.(),{taskId:i,status:"killed",signal:"SIGTERM"}):{taskId:i,status:e.status,error:"kill_failed",reason:"permission_denied"}):{taskId:i,status:e.status,error:"kill_failed",reason:"no_child"}:{taskId:i,status:"killed",error:"task_not_found"}}killProcessGroup(t,i=!1){const e=t.pid;if(!e)return!1;try{if(process.platform==="win32"){const n="/F",{spawnSync:o}=require("node:child_process");return o("taskkill",["/PID",String(e),"/T",n],{windowsHide:!0}),!0}try{process.kill(-e,i?"SIGKILL":"SIGTERM")}catch{process.kill(e,i?"SIGKILL":"SIGTERM")}return!0}catch{return!1}}async disposeAgent(t){const i=this.tasksByAgent.get(t);if(i){for(const e of i.values())e.status==="running"&&e.child&&(e.killedReason="session_disposed",this.killProcessGroup(e.child,!0)),e.timeoutTimer&&clearTimeout(e.timeoutTimer),e.killEscalationTimer&&clearTimeout(e.killEscalationTimer),e.retentionTimer&&clearTimeout(e.retentionTimer);this.tasksByAgent.delete(t)}await this.store.deleteAgent(t)}async disposeAll(){for(const[t,i]of this.tasksByAgent){for(const e of i.values())e.status==="running"&&e.child&&(e.killedReason="process_exit",this.killProcessGroup(e.child,!0)),e.timeoutTimer&&clearTimeout(e.timeoutTimer),e.killEscalationTimer&&clearTimeout(e.killEscalationTimer),e.retentionTimer&&clearTimeout(e.retentionTimer);this.tasksByAgent.delete(t)}}async reconcileOrphans(t){const i=await this.store.readSession(t);if(!i||i.tasks.length===0)return;let e=!1;for(const n of i.tasks)n.status==="running"&&(n.status="killed",n.killedReason="runtime_crashed",n.finishedAt=Date.now(),n.notified=!0,e=!0);e&&await this.store.persistSession(t,i.tasks)}async readSessionState(t){return this.store.readSession(t)}async markNotified(t,i){await this.store.markNotified(t,i)}getOrCreateBucket(t){let i=this.tasksByAgent.get(t);return i||(i=new Map,this.tasksByAgent.set(t,i)),i}getTask(t,i){const e=this.tasksByAgent.get(t);if(e&&/^[A-Za-z0-9]{8}$/.test(i))return e.get(i)}generateTaskId(t){for(let i=0;i<3;i++){const e=T(4).toString("hex").slice(0,8);if(!t.has(e))return e}return T(6).toString("hex").slice(0,8)}async persistTask(t){const i=await this.store.readSession(t.agentId)??{agentId:t.agentId,updatedAt:0,tasks:[]},e={taskId:t.taskId,agentId:t.agentId,command:t.command,commandPreview:t.commandPreview,cwd:t.cwd,status:t.status,startedAt:t.startedAt,finishedAt:t.finishedAt,exitCode:t.exitCode,signal:t.signal,outputPath:t.outputPath,outputLineCount:t.outputLineCount,outputBytes:t.outputBytes,notified:t.notified,killedReason:t.killedReason},n=i.tasks.findIndex(s=>s.taskId===t.taskId);n>=0?i.tasks[n]=e:i.tasks.push(e);const o=Date.now();i.tasks=i.tasks.filter(s=>s.status==="running"||!s.finishedAt||o-s.finishedAt<k),await this.store.persistSession(t.agentId,i.tasks)}}class M extends Error{constructor(t,i){super(i),this.name="BackgroundTaskError",this.code=t}}const Y=new z;export{z as BackgroundCommandManager,M as BackgroundTaskError,Y as backgroundCommandManager};
8
+
@@ -0,0 +1,46 @@
1
+
2
+ export type BackgroundTaskStatus = "running" | "completed" | "failed" | "killed";
3
+
4
+ export interface BackgroundTaskRecord {
5
+ taskId: string;
6
+ agentId: string;
7
+ command: string;
8
+ commandPreview: string;
9
+ cwd: string;
10
+ status: BackgroundTaskStatus;
11
+ startedAt: number;
12
+ finishedAt: number | null;
13
+ exitCode: number | null;
14
+ signal: string | null;
15
+ outputPath: string;
16
+ outputLineCount: number;
17
+ outputBytes: number;
18
+
19
+ notified: boolean;
20
+
21
+ killedReason?: string | null;
22
+ }
23
+ export interface BackgroundSessionState {
24
+ agentId: string;
25
+ updatedAt: number;
26
+ tasks: BackgroundTaskRecord[];
27
+ }
28
+ export declare class BackgroundCommandStore {
29
+
30
+ private writeChains;
31
+ private chain;
32
+ private setChain;
33
+
34
+ readSession(agentId: string): Promise<BackgroundSessionState | null>;
35
+
36
+ persistSession(agentId: string, tasks: BackgroundTaskRecord[]): Promise<void>;
37
+
38
+ markNotified(agentId: string, taskIds: string[]): Promise<void>;
39
+
40
+ outputPathFor(agentId: string, taskId: string): string;
41
+
42
+ deleteAgent(agentId: string): Promise<void>;
43
+
44
+ listAgents(): Promise<string[]>;
45
+ private backupCorrupt;
46
+ }
@@ -0,0 +1,2 @@
1
+ import{promises as a}from"node:fs";import f from"node:os";import c from"node:path";import{renameWithRetry as w}from"./fs-utils.js";const p=c.join(f.homedir(),".acode","bg-tasks");function m(s){return String(s).replace(/[^a-zA-Z0-9_-]/g,"_")}function u(s){return c.join(p,m(s))}function h(s){return c.join(u(s),"state.json")}class S{constructor(){this.writeChains=new Map}chain(t){return this.writeChains.get(t)??Promise.resolve()}setChain(t,r){this.writeChains.set(t,r.catch(()=>{}))}async readSession(t){try{const r=await a.readFile(h(t),"utf-8"),e=JSON.parse(r);return{agentId:t,updatedAt:typeof e.updatedAt=="number"?e.updatedAt:0,tasks:Array.isArray(e.tasks)?e.tasks:[]}}catch(r){if(r?.code==="ENOENT")return null;if(r instanceof SyntaxError)return await this.backupCorrupt(t).catch(()=>{}),null;throw r}}async persistSession(t,r){const e=h(t),n=this.chain(t).then(async()=>{await a.mkdir(u(t),{recursive:!0});const o={agentId:t,updatedAt:Date.now(),tasks:r},i=`${e}.tmp`;await a.writeFile(i,JSON.stringify(o,null,2),"utf-8"),await w(i,e)});this.setChain(t,n),await n}async markNotified(t,r){const e=await this.readSession(t);if(!e)return;const n=new Set(r);let o=!1;for(const i of e.tasks)n.has(i.taskId)&&!i.notified&&(i.notified=!0,o=!0);o&&await this.persistSession(t,e.tasks)}outputPathFor(t,r){return c.join(u(t),`${r}.out`)}async deleteAgent(t){const r=this.chain(t).then(()=>a.rm(u(t),{recursive:!0,force:!0}));this.setChain(t,r),await r.catch(()=>{})}async listAgents(){try{return(await a.readdir(p,{withFileTypes:!0})).filter(r=>r.isDirectory()).map(r=>r.name)}catch(t){if(t?.code==="ENOENT")return[];throw t}}async backupCorrupt(t){const r=h(t),e=`${r}.corrupt-${Date.now()}`;await a.copyFile(r,e).catch(()=>{})}}export{S as BackgroundCommandStore};
2
+
@@ -0,0 +1,5 @@
1
+
2
+ import type { ACodeBuiltInTool, BackgroundToolDeps } from "./built-in-file-tools.js";
3
+ export declare function createListBackgroundCommandsTool(agentId: string, deps: BackgroundToolDeps): ACodeBuiltInTool;
4
+ export declare function createKillBackgroundCommandTool(agentId: string, deps: BackgroundToolDeps): ACodeBuiltInTool;
5
+ export declare function createReadBackgroundOutputTool(agentId: string, deps: BackgroundToolDeps): ACodeBuiltInTool;
@@ -0,0 +1,18 @@
1
+ const s=`List all background commands for the current agent, including running and recently completed ones.
2
+
3
+ Returns task_id, status, command (preview), startedAt, finishedAt, exitCode, outputLineCount, outputBytes.
4
+ Does NOT include output content (use read_background_output to fetch output).`,d=`Terminate a running background command by task_id.
5
+
6
+ Sends SIGTERM to the process group, then SIGKILL after 1s if still alive.
7
+ No-op (idempotent) if the task already finished.
8
+ Cross-agent task_id returns task_not_found (does not leak existence).`,r=`Read the output (stdout+stderr merged, interleaved in arrival order) of a background command.
9
+
10
+ Query modes:
11
+ - head: first N lines
12
+ - tail (default): last N lines (fastest, in-memory)
13
+ - offset: from line X (0-based) take N lines (reads output file, bypasses memory cap)
14
+ - grep: filter lines by regex (JS syntax, case-insensitive)
15
+
16
+ Output capped at 256KB / 500 lines per call; truncated=true means more is available.`;function u(a,o){return{kind:"builtin",toolName:"ListBackgroundCommands",exposedName:"ListBackgroundCommands",description:s,inputSchema:{type:"object",properties:{status_filter:{type:"string",enum:["running","completed","failed","killed","all"],description:"Filter by status. Defaults to 'all'.",default:"all"}},additionalProperties:!1},async execute(t){const e=typeof t.status_filter=="string"?t.status_filter:"all",i=o.manager.listTasks(a,e);return{title:`${i.length} background commands`,output:JSON.stringify(i.map(n=>({taskId:n.taskId,status:n.status,command:n.commandPreview,cwd:n.cwd,startedAt:new Date(n.startedAt).toISOString(),finishedAt:n.finishedAt?new Date(n.finishedAt).toISOString():null,exitCode:n.exitCode,outputLineCount:n.outputLineCount,outputBytes:n.outputBytes})),null,2),metadata:{tasks:i,count:i.length}}}}}function l(a,o){return{kind:"builtin",toolName:"KillBackgroundCommand",exposedName:"KillBackgroundCommand",description:d,inputSchema:{type:"object",properties:{task_id:{type:"string",description:"The task_id returned by Bash(wait_seconds=...) or ListBackgroundCommands."}},required:["task_id"],additionalProperties:!1},async execute(t){const e=typeof t.task_id=="string"?t.task_id:"",i=await o.manager.killTask(a,e);return{title:`kill ${e}: ${i.status}`,output:JSON.stringify(i,null,2),metadata:i}}}}function p(a,o){return{kind:"builtin",toolName:"ReadBackgroundOutput",exposedName:"ReadBackgroundOutput",description:r,inputSchema:{type:"object",properties:{task_id:{type:"string",description:"The task_id to read output from."},mode:{type:"string",enum:["head","tail","offset","grep"],description:"head=first N, tail=last N (default), offset=from line X take N, grep=regex filter.",default:"tail"},lines:{type:"integer",description:"Number of lines (head/tail/offset). Default 50, max 500.",default:50,minimum:1,maximum:500},offset:{type:"integer",description:"Starting line number (0-based) for offset mode.",minimum:0},pattern:{type:"string",description:"Regex pattern for grep mode (JS syntax, case-insensitive)."},include_metadata:{type:"boolean",description:"Include totalLines/totalBytes/truncated/hasMore/outputFile in response.",default:!0}},required:["task_id"],additionalProperties:!1},async execute(t){const e=await o.manager.readOutput(a,{task_id:typeof t.task_id=="string"?t.task_id:"",mode:t.mode==="head"||t.mode==="tail"||t.mode==="offset"||t.mode==="grep"?t.mode:"tail",lines:typeof t.lines=="number"?t.lines:50,offset:typeof t.offset=="number"?t.offset:0,pattern:typeof t.pattern=="string"?t.pattern:void 0,include_metadata:t.include_metadata!==!1});return{title:`read ${e.taskId} (${e.mode}): ${e.lineCount} lines`,output:e.lines.join(`
17
+ `),metadata:e}}}}export{l as createKillBackgroundCommandTool,u as createListBackgroundCommandsTool,p as createReadBackgroundOutputTool};
18
+
@@ -0,0 +1,23 @@
1
+
2
+ export declare class BackgroundOutputRingBuffer {
3
+ private lines;
4
+ private bytes;
5
+ private partialLine;
6
+
7
+ push(text: string): void;
8
+
9
+ flush(): void;
10
+ private enforceLimits;
11
+
12
+ head(n: number): string[];
13
+
14
+ tail(n: number): string[];
15
+
16
+ offset(offset: number, limit: number): string[];
17
+
18
+ getLineCount(): number;
19
+
20
+ getByteCount(): number;
21
+
22
+ hasLine(lineIndex: number): boolean;
23
+ }
@@ -0,0 +1,3 @@
1
+ const n=1e3,h=262144;class r{constructor(){this.lines=[],this.bytes=0,this.partialLine=""}push(t){if(!t)return;this.partialLine+=t;const i=this.partialLine.split(`
2
+ `);this.partialLine=i.pop()??"";for(const s of i)this.lines.push(s),this.bytes+=Buffer.byteLength(s,"utf-8");this.enforceLimits()}flush(){this.partialLine&&(this.lines.push(this.partialLine),this.bytes+=Buffer.byteLength(this.partialLine,"utf-8"),this.partialLine="",this.enforceLimits())}enforceLimits(){for(;this.lines.length>1e3;){const t=this.lines.shift();t!==void 0&&(this.bytes-=Buffer.byteLength(t,"utf-8"))}for(;this.bytes>262144&&this.lines.length>1;){const t=this.lines.shift();t!==void 0&&(this.bytes-=Buffer.byteLength(t,"utf-8"))}this.bytes<0&&(this.bytes=0)}head(t){return this.lines.slice(0,Math.max(0,t))}tail(t){const i=Math.max(0,this.lines.length-t);return this.lines.slice(i)}offset(t,i){const s=Math.max(0,t);return this.lines.slice(s,s+Math.max(0,i))}getLineCount(){return this.lines.length}getByteCount(){return this.bytes+Buffer.byteLength(this.partialLine,"utf-8")}hasLine(t){return t>=0&&t<this.lines.length}}export{r as BackgroundOutputRingBuffer};
3
+
@@ -1,4 +1,11 @@
1
1
  import { type CommandOutputSink } from "./builtins/shellCommandRunner.js";
2
+ import type { BackgroundCommandManager, BackgroundCompletedEvent } from "./background-command-manager.js";
3
+
4
+ export interface BackgroundToolDeps {
5
+ manager: BackgroundCommandManager;
6
+
7
+ emitCompleted?: (event: BackgroundCompletedEvent) => void;
8
+ }
2
9
  export type JsonValue = string | number | boolean | null | JsonValue[] | {
3
10
  [key: string]: JsonValue;
4
11
  };
@@ -34,4 +41,4 @@ export interface ResolvedWorkspaceTarget {
34
41
  export declare function ensureInsideWorkspace(workspaceRoot: string, targetPath: string): string;
35
42
 
36
43
  export declare function resolveWorkspaceTarget(workingDirectory: string, rawFilePath: string): Promise<ResolvedWorkspaceTarget>;
37
- export declare function createBuiltInFileTools(workingDirectory: string, agentId?: string, onFileChange?: OnAgentFileChange): ACodeBuiltInTool[];
44
+ export declare function createBuiltInFileTools(workingDirectory: string, agentId?: string, onFileChange?: OnAgentFileChange, backgroundDeps?: BackgroundToolDeps): ACodeBuiltInTool[];
@@ -1,4 +1,4 @@
1
- import{randomUUID as Y}from"node:crypto";import{promises as u,createReadStream as C}from"node:fs";import f from"node:path";import D from"node:readline";import{runShellCommand as K}from"./builtins/shellCommandRunner.js";import{symbolsTool as V}from"./built-in-symbols-tool.js";const H=2e3,v=2e3,Z=50*1024,U=4096,J=.3,Q=new Set([".png",".jpg",".jpeg",".gif",".bmp",".ico",".webp",".tiff",".tif",".zip",".gz",".tar",".rar",".7z",".bz2",".xz",".lz",".exe",".dll",".so",".dylib",".bin",".o",".a",".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".mp3",".mp4",".avi",".mov",".wav",".flv",".mkv",".ogg",".woff",".woff2",".ttf",".otf",".eot",".class",".jar",".war",".ear",".pyc",".pyo",".node",".sqlite",".db",".mdb",".wasm"]),tt=1e3,et=1024*1024,nt=256*1024,ot=1e3,it=new Set([".git","node_modules","dist","build",".next",".nuxt",".cache",".turbo",".output","coverage",".nyc_output"]);function y(o){return o.replace(/\\/g,"/")}async function q(o){try{return await u.access(o),!0}catch{return!1}}function rt(o,n){if(!n)return 0;let t=0,e=o.indexOf(n);for(;e>=0;)t+=1,e=o.indexOf(n,e+n.length);return t}function _(o,n,t){for(const e of n){const i=o[e];if(typeof i=="string")return i}throw new Error(`${t} is required`)}function O(o,n){for(const t of n){const e=o[t];if(typeof e=="string"&&e.trim())return e}}function A(o,n,t){for(const e of n){const i=o[e],r=typeof i=="number"?i:typeof i=="string"?Number(i):NaN;if(Number.isFinite(r)&&r>0)return Math.floor(r)}return t}async function W(o){const n=f.extname(o).toLowerCase();if(Q.has(n))return!0;const t=await u.open(o,"r");try{const e=Buffer.alloc(U),{bytesRead:i}=await t.read(e,0,U,0);if(i===0)return!1;const r=e.subarray(0,i);if(r.includes(0))return!0;let c=0;for(let s=0;s<i;s++){const a=r[s];(a<9||a>13&&a<32)&&c++}return c/i>J}finally{await t.close()}}function E(o,n){const t=f.resolve(n),e=f.relative(o,t);if(e.startsWith("..")||f.isAbsolute(e))throw new Error(`Target path is outside workspace: ${t}`);return t}async function at(o,n){const t=f.resolve(String(o||"").trim()),e=await u.realpath(t);if(!(await u.stat(e)).isDirectory())throw new Error(`Workspace path is not a directory: ${t}`);if(!n?.trim())return{workspaceRoot:e,cwd:e};const r=n.trim(),c=f.isAbsolute(r)?f.resolve(r):f.resolve(t,r),s=f.relative(t,c);if(s.startsWith("..")||f.isAbsolute(s))throw new Error(`Command cwd is outside workspace: ${c}`);const a=E(e,await u.realpath(c));if(!(await u.stat(a)).isDirectory())throw new Error(`Command cwd is not a directory: ${a}`);return{workspaceRoot:e,cwd:a}}async function st(o,n){const t=await u.realpath(f.dirname(n));E(o,t);try{if((await u.lstat(n)).isSymbolicLink())throw new Error(`Refusing to write through symbolic link: ${n}`)}catch(e){if((typeof e=="object"&&e!==null?Reflect.get(e,"code"):void 0)!=="ENOENT")throw e}}async function R(o,n){const t=f.resolve(String(o||"").trim()),e=await u.realpath(t);if(!(await u.stat(e)).isDirectory())throw new Error(`Workspace path is not a directory: ${t}`);const r=n.trim();if(!r)throw new Error("file_path is required");const c=f.isAbsolute(r)?f.resolve(r):f.resolve(t,r),s=f.relative(t,c);if(s.startsWith("..")||f.isAbsolute(s))throw new Error(`Target path is outside workspace: ${c}`);return{workspaceRoot:e,requestedPath:c,targetPath:E(e,f.join(e,s))}}async function j(o,n,t){const e=await R(o,n),i=f.dirname(e.targetPath),r=!await q(i);await u.mkdir(i,{recursive:!0}),await st(e.workspaceRoot,e.targetPath);const c=f.join(i,`.${f.basename(e.targetPath)}.${process.pid}.${Y()}.tmp`);try{await u.writeFile(c,t,"utf-8"),await u.rename(c,e.targetPath)}catch(s){throw await u.rm(c,{force:!0}),s}return{workspaceRoot:e.workspaceRoot,targetPath:e.targetPath,bytes:Buffer.byteLength(t,"utf-8"),createdParentDirectory:r}}async function ct(o,n){const t=await R(o,n),e=E(t.workspaceRoot,await u.realpath(t.targetPath));if(!(await u.stat(e)).isFile())throw new Error(`Path is not a file: ${e}`);return{workspaceRoot:t.workspaceRoot,targetPath:e,content:await u.readFile(e,"utf-8")}}async function lt(o,n,t,e){const i=await R(o,n),r=E(i.workspaceRoot,await u.realpath(i.targetPath));if(!(await u.stat(r)).isFile())throw new Error(`Path is not a file: ${r}`);if(await W(r))return{workspaceRoot:i.workspaceRoot,targetPath:r,content:"[Binary file, content skipped]",isBinary:!0,truncated:!1,totalLines:0,shownFromLine:0,shownToLine:0};const s=C(r,{encoding:"utf-8"}),a=D.createInterface({input:s,crlfDelay:1/0});try{const d=[];let p=0,l=0,I=0,m=!1,w=!1;const S=t-1;for await(const L of a){if(l++,p<S){p++;continue}if(d.length>=e){m=!0,p++;continue}let x=L;x.length>v&&(x=x.slice(0,v)+`... (line truncated to ${v} chars)`);const k=`${p+1}: ${x}`,T=Buffer.byteLength(k,"utf-8")+1;if(I+T>Z){w=!0;break}d.push(k),I+=T,p++}if(l>0&&d.length===0&&!m&&!w&&S>=l)throw new Error(`offset ${t} exceeds total line count ${l} in file: ${r}`);const P=d.length>0?t:0,h=d.length>0?t+d.length-1:0;let b;w?b=`Output capped at 50 KB. Showing lines ${P}-${h}. Use offset=${h+1} to continue.`:m?b=`Showing lines ${P}-${h} of ${l}. Use offset=${h+1} to continue.`:b=`End of file - total ${l} lines`;const M=d.length>0?d.join(`
1
+ import{randomUUID as Y}from"node:crypto";import{promises as d,createReadStream as N}from"node:fs";import f from"node:path";import D from"node:readline";import{runShellCommand as K}from"./builtins/shellCommandRunner.js";import{symbolsTool as H}from"./built-in-symbols-tool.js";import{createListBackgroundCommandsTool as V,createKillBackgroundCommandTool as Z,createReadBackgroundOutputTool as J}from"./background-command-tools.js";const Q=2e3,E=2e3,tt=50*1024,U=4096,et=.3,ot=new Set([".png",".jpg",".jpeg",".gif",".bmp",".ico",".webp",".tiff",".tif",".zip",".gz",".tar",".rar",".7z",".bz2",".xz",".lz",".exe",".dll",".so",".dylib",".bin",".o",".a",".pdf",".doc",".docx",".xls",".xlsx",".ppt",".pptx",".mp3",".mp4",".avi",".mov",".wav",".flv",".mkv",".ogg",".woff",".woff2",".ttf",".otf",".eot",".class",".jar",".war",".ear",".pyc",".pyo",".node",".sqlite",".db",".mdb",".wasm"]),nt=1e3,it=1024*1024,rt=256*1024,at=1e3,st=new Set([".git","node_modules","dist","build",".next",".nuxt",".cache",".turbo",".output","coverage",".nyc_output"]);function _(i){return i.replace(/\\/g,"/")}async function q(i){try{return await d.access(i),!0}catch{return!1}}function ct(i,o){if(!o)return 0;let e=0,t=i.indexOf(o);for(;t>=0;)e+=1,t=i.indexOf(o,t+o.length);return e}function k(i,o,e){for(const t of o){const r=i[t];if(typeof r=="string")return r}throw new Error(`${e} is required`)}function A(i,o){for(const e of o){const t=i[e];if(typeof t=="string"&&t.trim())return t}}function O(i,o,e){for(const t of o){const r=i[t],n=typeof r=="number"?r:typeof r=="string"?Number(r):NaN;if(Number.isFinite(n)&&n>0)return Math.floor(n)}return e}async function W(i){const o=f.extname(i).toLowerCase();if(ot.has(o))return!0;const e=await d.open(i,"r");try{const t=Buffer.alloc(U),{bytesRead:r}=await e.read(t,0,U,0);if(r===0)return!1;const n=t.subarray(0,r);if(n.includes(0))return!0;let c=0;for(let s=0;s<r;s++){const a=n[s];(a<9||a>13&&a<32)&&c++}return c/r>et}finally{await e.close()}}function R(i,o){const e=f.resolve(o),t=f.relative(i,e);if(t.startsWith("..")||f.isAbsolute(t))throw new Error(`Target path is outside workspace: ${e}`);return e}async function lt(i,o){const e=f.resolve(String(i||"").trim()),t=await d.realpath(e);if(!(await d.stat(t)).isDirectory())throw new Error(`Workspace path is not a directory: ${e}`);if(!o?.trim())return{workspaceRoot:t,cwd:t};const n=o.trim(),c=f.isAbsolute(n)?f.resolve(n):f.resolve(e,n),s=f.relative(e,c);if(s.startsWith("..")||f.isAbsolute(s))throw new Error(`Command cwd is outside workspace: ${c}`);const a=R(t,await d.realpath(c));if(!(await d.stat(a)).isDirectory())throw new Error(`Command cwd is not a directory: ${a}`);return{workspaceRoot:t,cwd:a}}async function ut(i,o){const e=await d.realpath(f.dirname(o));R(i,e);try{if((await d.lstat(o)).isSymbolicLink())throw new Error(`Refusing to write through symbolic link: ${o}`)}catch(t){if((typeof t=="object"&&t!==null?Reflect.get(t,"code"):void 0)!=="ENOENT")throw t}}async function S(i,o){const e=f.resolve(String(i||"").trim()),t=await d.realpath(e);if(!(await d.stat(t)).isDirectory())throw new Error(`Workspace path is not a directory: ${e}`);const n=o.trim();if(!n)throw new Error("file_path is required");const c=f.isAbsolute(n)?f.resolve(n):f.resolve(e,n),s=f.relative(e,c);if(s.startsWith("..")||f.isAbsolute(s))throw new Error(`Target path is outside workspace: ${c}`);return{workspaceRoot:t,requestedPath:c,targetPath:R(t,f.join(t,s))}}async function j(i,o,e){const t=await S(i,o),r=f.dirname(t.targetPath),n=!await q(r);await d.mkdir(r,{recursive:!0}),await ut(t.workspaceRoot,t.targetPath);const c=f.join(r,`.${f.basename(t.targetPath)}.${process.pid}.${Y()}.tmp`);try{await d.writeFile(c,e,"utf-8"),await d.rename(c,t.targetPath)}catch(s){throw await d.rm(c,{force:!0}),s}return{workspaceRoot:t.workspaceRoot,targetPath:t.targetPath,bytes:Buffer.byteLength(e,"utf-8"),createdParentDirectory:n}}async function dt(i,o){const e=await S(i,o),t=R(e.workspaceRoot,await d.realpath(e.targetPath));if(!(await d.stat(t)).isFile())throw new Error(`Path is not a file: ${t}`);return{workspaceRoot:e.workspaceRoot,targetPath:t,content:await d.readFile(t,"utf-8")}}async function ft(i,o,e,t){const r=await S(i,o),n=R(r.workspaceRoot,await d.realpath(r.targetPath));if(!(await d.stat(n)).isFile())throw new Error(`Path is not a file: ${n}`);if(await W(n))return{workspaceRoot:r.workspaceRoot,targetPath:n,content:"[Binary file, content skipped]",isBinary:!0,truncated:!1,totalLines:0,shownFromLine:0,shownToLine:0};const s=N(n,{encoding:"utf-8"}),a=D.createInterface({input:s,crlfDelay:1/0});try{const u=[];let m=0,p=0,g=0,h=!1,l=!1;const M=e-1;for await(const L of a){if(p++,m<M){m++;continue}if(u.length>=t){h=!0,m++;continue}let T=L;T.length>E&&(T=T.slice(0,E)+`... (line truncated to ${E} chars)`);const x=`${m+1}: ${T}`,v=Buffer.byteLength(x,"utf-8")+1;if(g+v>tt){l=!0;break}u.push(x),g+=v,m++}if(p>0&&u.length===0&&!h&&!l&&M>=p)throw new Error(`offset ${e} exceeds total line count ${p} in file: ${n}`);const b=u.length>0?e:0,w=u.length>0?e+u.length-1:0;let P;l?P=`Output capped at 50 KB. Showing lines ${b}-${w}. Use offset=${w+1} to continue.`:h?P=`Showing lines ${b}-${w} of ${p}. Use offset=${w+1} to continue.`:P=`End of file - total ${p} lines`;const B=u.length>0?u.join(`
2
2
  `)+`
3
- `+b:b;return{workspaceRoot:i.workspaceRoot,targetPath:r,content:M,isBinary:!1,truncated:m||w,totalLines:w?-1:l,shownFromLine:P,shownToLine:h}}finally{a.close(),s.destroy()}}function G(o){let n="",t=0;for(;t<o.length;){const e=o[t];if(e==="*")n+="[^/]*";else if(e==="?")n+="[^/]";else if(e==="{"){const i=o.indexOf("}",t);if(i>=0){const r=o.slice(t+1,i);n+=`(${r.split(",").join("|")})`,t=i}else n+="\\{"}else if(e==="["){const i=o.indexOf("]",t);i>=0?(n+=o.slice(t,i+1),t=i):n+="\\["}else"+^$.()|\\".includes(e)?n+="\\"+e:n+=e;t++}return new RegExp(`^${n}$`)}async function ut(o,n,t,e){const i=[];async function r(c){if(i.length>=e)return;const s=await u.readdir(c,{withFileTypes:!0});for(const a of s){if(i.length>=e)return;if(a.name.startsWith(".")&&a.isDirectory())continue;const d=f.join(c,a.name);if(a.isDirectory()){if(it.has(a.name))continue;await r(d)}else if(a.isFile()){if(n&&!n.test(a.name)||t&&t.test(a.name))continue;i.push(d)}}}return await r(o),i}async function dt(o,n,t){if((await u.stat(o)).size>et)return{matches:[],count:0};if(await W(o))return{matches:[],count:0};const i=f.relative(n,o).replace(/\\/g,"/"),r=[];let c=0;const s=C(o,{encoding:"utf-8"}),a=D.createInterface({input:s,crlfDelay:1/0});try{let d=0;for await(const p of a)if(d++,t.regex.test(p)){if(c++,t.collectContent){let l=p;if(l.length>v&&(l=l.slice(0,v)+`... (line truncated to ${v} chars)`),r.push({file:i,line:d,content:l}),r.length>=t.maxResults)break}if(t.stopOnFirstMatch)break}}finally{a.close(),s.destroy()}return{matches:r,count:c}}const X=10*6e4,ft=X,pt=6e4,ht=10*6e4;function mt(o,n){return{kind:"builtin",toolName:"write_file",exposedName:"write_file",description:"Create or overwrite a UTF-8 text file inside the current workspace. Parent directories are created automatically.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to write."},content:{type:"string",description:"Complete UTF-8 file content."}},required:["file_path","content"],additionalProperties:!1},async execute(t){const e=_(t,["file_path","filePath"],"file_path"),i=_(t,["content"],"content"),r=await R(o,e),c=await q(r.targetPath),s=c?await u.readFile(r.targetPath,"utf-8").catch(()=>null):null,a=await j(o,e,i);if(n){const d=f.relative(a.workspaceRoot,a.targetPath).replace(/\\/g,"/");await n({filePath:d,operation:c?"UPDATE":"CREATE",beforeContent:s,afterContent:i})}return{ok:!0,tool:"write_file",workspacePath:y(a.workspaceRoot),filePath:y(a.targetPath),bytes:a.bytes,createdParentDirectory:a.createdParentDirectory}}}}function wt(o,n){return{kind:"builtin",toolName:"edit_file",exposedName:"edit_file",description:"Replace exactly one occurrence of old_string with new_string in a UTF-8 workspace text file.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to edit."},old_string:{type:"string",description:"Existing text to replace. Must occur exactly once."},new_string:{type:"string",description:"Replacement text."}},required:["file_path","old_string","new_string"],additionalProperties:!1},async execute(t){const e=_(t,["file_path","filePath"],"file_path"),i=_(t,["old_string","oldString"],"old_string"),r=_(t,["new_string","newString"],"new_string");if(!i)throw new Error("old_string must not be empty");const c=await ct(o,e),s=rt(c.content,i);if(s===0)throw new Error("old_string was not found in file");if(s>1)throw new Error(`old_string occurs ${s} times; provide a unique match`);const a=c.content.replace(i,r),d=await j(o,e,a);if(n){const p=f.relative(d.workspaceRoot,d.targetPath).replace(/\\/g,"/");await n({filePath:p,operation:"UPDATE",beforeContent:c.content,afterContent:a})}return{ok:!0,tool:"edit_file",workspacePath:y(d.workspaceRoot),filePath:y(d.targetPath),replacements:1,bytes:d.bytes}}}}function gt(o,n){return{kind:"builtin",toolName:"delete_file",exposedName:"delete_file",description:"Delete a file inside the current workspace. Returns an error if the file does not exist.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to delete."}},required:["file_path"],additionalProperties:!1},async execute(t){const e=_(t,["file_path","filePath"],"file_path"),i=await R(o,e),r=E(i.workspaceRoot,await u.realpath(i.targetPath).catch(()=>i.targetPath)),c=await u.stat(r).catch(()=>null);if(!c)throw new Error(`File does not exist: ${r}`);if(!c.isFile())throw new Error(`Path is not a file: ${r}`);const s=await u.readFile(r,"utf-8").catch(()=>null);if(await u.rm(r,{force:!0}),n){const a=f.relative(i.workspaceRoot,r).replace(/\\/g,"/");await n({filePath:a,operation:"DELETE",beforeContent:s,afterContent:null})}return{ok:!0,tool:"delete_file",workspacePath:y(i.workspaceRoot),filePath:y(r),deleted:!0}}}}function yt(o){return{kind:"builtin",toolName:"read_file",exposedName:"read_file",description:"Read a UTF-8 text file inside the current workspace. Returns content with line numbers in the format '<line>: <content>'. Supports offset (1-indexed line number to start from) and limit (max lines to return, default 2000). Output is capped at 2000 lines or 50 KB, whichever is reached first. Individual lines longer than 2000 characters are truncated. Avoid tiny repeated slices (e.g. 30-line chunks); read a larger window if more context is needed.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to read."},offset:{type:"number",description:"Line number to start reading from (1-indexed). Defaults to 1."},limit:{type:"number",description:"Maximum number of lines to return. Defaults to 2000."}},required:["file_path"],additionalProperties:!1},async execute(n){const t=_(n,["file_path","filePath"],"file_path"),e=A(n,["offset"],1),i=A(n,["limit"],H),r=await lt(o,t,e,i);return{ok:!0,tool:"read_file",workspacePath:y(r.workspaceRoot),filePath:y(r.targetPath),content:r.content,isBinary:r.isBinary,truncated:r.truncated,totalLines:r.totalLines,shownFromLine:r.shownFromLine,shownToLine:r.shownToLine}}}}function _t(o,n){return{kind:"builtin",toolName:"Bash",exposedName:"Bash",description:"Execute a shell command in the current workspace. Use for local diagnostics, builds, tests, and OS commands. The command runs with a bounded timeout and returns stdout, stderr, and exit code.",inputSchema:{type:"object",properties:{command:{type:"string",description:"Shell command to execute."},cwd:{type:"string",description:"Optional workspace-relative working directory. Defaults to the workspace root."},timeout_ms:{type:"number",description:"Optional timeout in milliseconds. Defaults to 600000 and is capped at 600000."},inactivity_timeout_ms:{type:"number",description:"Optional no-output timeout in milliseconds. Defaults to 60000 and is capped at 600000."},description:{type:"string",description:"Optional short reason for running the command."}},required:["command"],additionalProperties:!1},async execute(t,e){const i=_(t,["command"],"command").trim();if(!i)throw new Error("command must not be empty");const r=A(t,["timeout_ms","timeoutMs"],ft),c=Math.min(r,X),s=A(t,["inactivity_timeout_ms","inactivityTimeoutMs"],pt),a=Math.min(s,ht),d=O(t,["cwd"]),p=await at(o,d),l=await K({commandText:i,agentId:n,workspaceRoot:p.workspaceRoot,cwd:p.cwd,timeoutMs:c,inactivityTimeoutMs:a,onOutput:e?.onOutput});return{ok:l.exitCode===0&&!l.timedOut,tool:"Bash",command:i,cwd:y(p.cwd),exitCode:l.exitCode,signal:l.signal,timedOut:l.timedOut,inactivityTimedOut:l.inactivityTimedOut,timeoutMs:c,inactivityTimeoutMs:a,durationMs:l.durationMs,outputSpilled:l.outputSpilled,outputFilePath:l.outputFilePath,outputLineCount:l.outputLineCount,commandFilePath:l.commandFilePath,commandLineCount:l.commandLineCount,stdout:l.stdout,stderr:l.stderr}}}}function Pt(o){return{kind:"builtin",toolName:"grep",exposedName:"grep",description:"Search file contents using regular expressions within the workspace. Returns matched lines with file paths and line numbers. Supports include/exclude glob patterns, case-insensitive matching, and three output modes (content, files_with_matches, count). Searches recursively through directories, skipping binary files, hidden directories, and common build/dependency directories (node_modules, .git, dist, etc.).",inputSchema:{type:"object",properties:{pattern:{type:"string",description:"Regular expression pattern to search for in file contents."},path:{type:"string",description:"Workspace-relative file or directory path to search in. Defaults to the workspace root."},include:{type:"string",description:"Glob pattern to filter files by name (e.g. '*.ts', '*.{js,ts}'). Only matching files are searched."},exclude:{type:"string",description:"Glob pattern to exclude files by name (e.g. '*.test.ts'). Matching files are skipped."},case_insensitive:{type:"boolean",description:"Case-insensitive matching. Default false."},output_mode:{type:"string",enum:["content","files_with_matches","count"],description:"Output format: 'content' returns matched lines with line numbers (default), 'files_with_matches' returns only file paths with matches, 'count' returns match counts per file."},max_results:{type:"number",description:"Maximum number of matched lines to return (content mode). Default 1000."}},required:["pattern"],additionalProperties:!1},async execute(n){const t=_(n,["pattern"],"pattern"),e=O(n,["path"])||".",i=O(n,["include"]),r=O(n,["exclude"]),c=n.case_insensitive===!0,s=(()=>{const g=n.output_mode;return g==="files_with_matches"||g==="count"?g:"content"})(),a=A(n,["max_results"],ot),d=c?"i":"";let p;try{p=new RegExp(t,d)}catch(g){throw new Error(`Invalid regex pattern: ${g instanceof Error?g.message:String(g)}`)}const l=i?G(i):null,I=r?G(r):null,m=await R(o,e),w=E(m.workspaceRoot,await u.realpath(m.targetPath)),S=await u.stat(w);let P;if(S.isFile())P=[w];else if(S.isDirectory())P=await ut(w,l,I,tt);else throw new Error(`Path is neither a file nor a directory: ${w}`);const h=[],b=[],M=[];let L=0,x=0,k=!1;for(const g of P){if(s==="content"&&h.length>=a){k=!0;break}const z={regex:p,maxResults:s==="content"?a-h.length:Number.MAX_SAFE_INTEGER,collectContent:s==="content",stopOnFirstMatch:s==="files_with_matches"},F=await dt(g,m.workspaceRoot,z);if(F.count>0){const N=f.relative(m.workspaceRoot,g).replace(/\\/g,"/");if(M.push(N),L+=F.count,s==="content"){for(const $ of F.matches){const B=Buffer.byteLength($.content,"utf-8")+64;if(x+B>nt){k=!0;break}x+=B,h.push($)}if(k)break;if(h.length>=a){k=!0;break}}else s==="count"&&b.push({file:N,count:F.count})}}const T={ok:!0,tool:"grep",pattern:t,searchPath:y(m.targetPath),outputMode:s,filesSearched:P.length,filesMatched:M.length,totalMatches:L,truncated:k};return s==="content"?T.matches=h:s==="files_with_matches"?T.matches=M:T.matches=b,T}}}function Rt(o,n,t){const e=n||"unknown";return[yt(o),mt(o,t),wt(o,t),gt(o,t),Pt(o),_t(o,e),V(o)]}export{Rt as createBuiltInFileTools,E as ensureInsideWorkspace,R as resolveWorkspaceTarget};
3
+ `+P:P;return{workspaceRoot:r.workspaceRoot,targetPath:n,content:B,isBinary:!1,truncated:h||l,totalLines:l?-1:p,shownFromLine:b,shownToLine:w}}finally{a.close(),s.destroy()}}function G(i){let o="",e=0;for(;e<i.length;){const t=i[e];if(t==="*")o+="[^/]*";else if(t==="?")o+="[^/]";else if(t==="{"){const r=i.indexOf("}",e);if(r>=0){const n=i.slice(e+1,r);o+=`(${n.split(",").join("|")})`,e=r}else o+="\\{"}else if(t==="["){const r=i.indexOf("]",e);r>=0?(o+=i.slice(e,r+1),e=r):o+="\\["}else"+^$.()|\\".includes(t)?o+="\\"+t:o+=t;e++}return new RegExp(`^${o}$`)}async function pt(i,o,e,t){const r=[];async function n(c){if(r.length>=t)return;const s=await d.readdir(c,{withFileTypes:!0});for(const a of s){if(r.length>=t)return;if(a.name.startsWith(".")&&a.isDirectory())continue;const u=f.join(c,a.name);if(a.isDirectory()){if(st.has(a.name))continue;await n(u)}else if(a.isFile()){if(o&&!o.test(a.name)||e&&e.test(a.name))continue;r.push(u)}}}return await n(i),r}async function mt(i,o,e){if((await d.stat(i)).size>it)return{matches:[],count:0};if(await W(i))return{matches:[],count:0};const r=f.relative(o,i).replace(/\\/g,"/"),n=[];let c=0;const s=N(i,{encoding:"utf-8"}),a=D.createInterface({input:s,crlfDelay:1/0});try{let u=0;for await(const m of a)if(u++,e.regex.test(m)){if(c++,e.collectContent){let p=m;if(p.length>E&&(p=p.slice(0,E)+`... (line truncated to ${E} chars)`),n.push({file:r,line:u,content:p}),n.length>=e.maxResults)break}if(e.stopOnFirstMatch)break}}finally{a.close(),s.destroy()}return{matches:n,count:c}}const X=10*6e4,ht=X,wt=6e4,gt=10*6e4;function yt(i,o){return{kind:"builtin",toolName:"write_file",exposedName:"write_file",description:"Create or overwrite a UTF-8 text file inside the current workspace. Parent directories are created automatically.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to write."},content:{type:"string",description:"Complete UTF-8 file content."}},required:["file_path","content"],additionalProperties:!1},async execute(e){const t=k(e,["file_path","filePath"],"file_path"),r=k(e,["content"],"content"),n=await S(i,t),c=await q(n.targetPath),s=c?await d.readFile(n.targetPath,"utf-8").catch(()=>null):null,a=await j(i,t,r);if(o){const u=f.relative(a.workspaceRoot,a.targetPath).replace(/\\/g,"/");await o({filePath:u,operation:c?"UPDATE":"CREATE",beforeContent:s,afterContent:r})}return{ok:!0,tool:"write_file",workspacePath:_(a.workspaceRoot),filePath:_(a.targetPath),bytes:a.bytes,createdParentDirectory:a.createdParentDirectory}}}}function _t(i,o){return{kind:"builtin",toolName:"edit_file",exposedName:"edit_file",description:"Replace exactly one occurrence of old_string with new_string in a UTF-8 workspace text file.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to edit."},old_string:{type:"string",description:"Existing text to replace. Must occur exactly once."},new_string:{type:"string",description:"Replacement text."}},required:["file_path","old_string","new_string"],additionalProperties:!1},async execute(e){const t=k(e,["file_path","filePath"],"file_path"),r=k(e,["old_string","oldString"],"old_string"),n=k(e,["new_string","newString"],"new_string");if(!r)throw new Error("old_string must not be empty");const c=await dt(i,t),s=ct(c.content,r);if(s===0)throw new Error("old_string was not found in file");if(s>1)throw new Error(`old_string occurs ${s} times; provide a unique match`);const a=c.content.replace(r,n),u=await j(i,t,a);if(o){const m=f.relative(u.workspaceRoot,u.targetPath).replace(/\\/g,"/");await o({filePath:m,operation:"UPDATE",beforeContent:c.content,afterContent:a})}return{ok:!0,tool:"edit_file",workspacePath:_(u.workspaceRoot),filePath:_(u.targetPath),replacements:1,bytes:u.bytes}}}}function kt(i,o){return{kind:"builtin",toolName:"delete_file",exposedName:"delete_file",description:"Delete a file inside the current workspace. Returns an error if the file does not exist.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to delete."}},required:["file_path"],additionalProperties:!1},async execute(e){const t=k(e,["file_path","filePath"],"file_path"),r=await S(i,t),n=R(r.workspaceRoot,await d.realpath(r.targetPath).catch(()=>r.targetPath)),c=await d.stat(n).catch(()=>null);if(!c)throw new Error(`File does not exist: ${n}`);if(!c.isFile())throw new Error(`Path is not a file: ${n}`);const s=await d.readFile(n,"utf-8").catch(()=>null);if(await d.rm(n,{force:!0}),o){const a=f.relative(r.workspaceRoot,n).replace(/\\/g,"/");await o({filePath:a,operation:"DELETE",beforeContent:s,afterContent:null})}return{ok:!0,tool:"delete_file",workspacePath:_(r.workspaceRoot),filePath:_(n),deleted:!0}}}}function bt(i){return{kind:"builtin",toolName:"read_file",exposedName:"read_file",description:"Read a UTF-8 text file inside the current workspace. Returns content with line numbers in the format '<line>: <content>'. Supports offset (1-indexed line number to start from) and limit (max lines to return, default 2000). Output is capped at 2000 lines or 50 KB, whichever is reached first. Individual lines longer than 2000 characters are truncated. Avoid tiny repeated slices (e.g. 30-line chunks); read a larger window if more context is needed.",inputSchema:{type:"object",properties:{file_path:{type:"string",description:"Workspace-relative file path to read."},offset:{type:"number",description:"Line number to start reading from (1-indexed). Defaults to 1."},limit:{type:"number",description:"Maximum number of lines to return. Defaults to 2000."}},required:["file_path"],additionalProperties:!1},async execute(o){const e=k(o,["file_path","filePath"],"file_path"),t=O(o,["offset"],1),r=O(o,["limit"],Q),n=await ft(i,e,t,r);return{ok:!0,tool:"read_file",workspacePath:_(n.workspaceRoot),filePath:_(n.targetPath),content:n.content,isBinary:n.isBinary,truncated:n.truncated,totalLines:n.totalLines,shownFromLine:n.shownFromLine,shownToLine:n.shownToLine}}}}function Pt(i,o,e){return{kind:"builtin",toolName:"Bash",exposedName:"Bash",description:"Execute a shell command in the current workspace. Use for local diagnostics, builds, tests, and OS commands. The command runs with a bounded timeout and returns stdout, stderr, and exit code. Set wait_seconds to 0 or n>0 to move long-running commands to the background and return a task_id immediately (or after n seconds); then use ListBackgroundCommands / ReadBackgroundOutput / KillBackgroundCommand to manage them.",inputSchema:{type:"object",properties:{command:{type:"string",description:"Shell command to execute."},cwd:{type:"string",description:"Optional workspace-relative working directory. Defaults to the workspace root."},timeout_ms:{type:"number",description:"Optional timeout in milliseconds. Defaults to 600000 and is capped at 600000."},inactivity_timeout_ms:{type:"number",description:"Optional no-output timeout in milliseconds. Defaults to 60000 and is capped at 600000. Only applies to foreground (wait_seconds=-1); ignored once a command moves to background."},wait_seconds:{type:"number",description:"How long to block waiting for the command to finish. -1 (default): block until completion (current behavior, capped at timeout_ms). 0: spawn and immediately move to background, return task_id. n>0: wait up to n seconds; if still running, move to background and return task_id. Background commands remain subject to timeout_ms (total upper bound); inactivity_timeout_ms is ignored once backgrounded.",default:-1,minimum:-1},description:{type:"string",description:"Optional short reason for running the command."}},required:["command"],additionalProperties:!1},async execute(t,r){const n=k(t,["command"],"command").trim();if(!n)throw new Error("command must not be empty");const c=O(t,["timeout_ms","timeoutMs"],ht),s=Math.min(c,X),a=O(t,["inactivity_timeout_ms","inactivityTimeoutMs"],wt),u=Math.min(a,gt),m=A(t,["cwd"]),p=await lt(i,m),g=t.wait_seconds,h=typeof g=="number"&&Number.isFinite(g)?g:-1;if(h!==-1&&e)return await xt({deps:e,command:n,agentId:o,workspaceRoot:p.workspaceRoot,cwd:p.cwd,timeoutMs:s,waitSeconds:h});const l=await K({commandText:n,agentId:o,workspaceRoot:p.workspaceRoot,cwd:p.cwd,timeoutMs:s,inactivityTimeoutMs:u,onOutput:r?.onOutput});return{ok:l.exitCode===0&&!l.timedOut,tool:"Bash",command:n,cwd:_(p.cwd),exitCode:l.exitCode,signal:l.signal,timedOut:l.timedOut,inactivityTimedOut:l.inactivityTimedOut,timeoutMs:s,inactivityTimeoutMs:u,durationMs:l.durationMs,outputSpilled:l.outputSpilled,outputFilePath:l.outputFilePath,outputLineCount:l.outputLineCount,commandFilePath:l.commandFilePath,commandLineCount:l.commandLineCount,stdout:l.stdout,stderr:l.stderr}}}}async function xt(i){const{deps:o,command:e,agentId:t,workspaceRoot:r,cwd:n,timeoutMs:c,waitSeconds:s}=i;try{const{taskId:a,completion:u}=o.manager.startCommand({commandText:e,agentId:t,workspaceRoot:r,cwd:n,timeoutMs:c,onCompleted:o.emitCompleted});if(s===0)return{ok:!0,tool:"Bash",command:e,backgrounded:!0,taskId:a,status:"backgrounded",reason:"immediate",message:`Command moved to background. Use ListBackgroundCommands to view, ReadBackgroundOutput(task_id="${a}", mode="tail") to read output, KillBackgroundCommand(task_id="${a}") to stop.`};const m=Math.max(0,Math.floor(s*1e3));let p;const g=new Promise(h=>{p=setTimeout(()=>h("__timeout__"),m),p.unref?.()});try{const h=await Promise.race([u,g]);if(h==="__timeout__")return{ok:!0,tool:"Bash",command:e,backgrounded:!0,taskId:a,status:"backgrounded",reason:"timeout",waitedMs:m,message:`Command still running after ${s}s; moved to background. Use ListBackgroundCommands / ReadBackgroundOutput(task_id="${a}", mode="tail") to inspect.`};const l=h;return{ok:l.exitCode===0&&!l.timedOut,tool:"Bash",command:e,backgrounded:!1,taskId:a,exitCode:l.exitCode,signal:l.signal,timedOut:l.timedOut,durationMs:l.durationMs,stdout:l.stdout,stderr:l.stderr}}finally{p&&clearTimeout(p)}}catch(a){if(a?.code==="too_many_tasks"||a?.code==="global_limit_reached")return{ok:!1,tool:"Bash",command:e,backgrounded:!1,error:a.code,message:a.message};throw a}}function Tt(i){return{kind:"builtin",toolName:"grep",exposedName:"grep",description:"Search file contents using regular expressions within the workspace. Returns matched lines with file paths and line numbers. Supports include/exclude glob patterns, case-insensitive matching, and three output modes (content, files_with_matches, count). Searches recursively through directories, skipping binary files, hidden directories, and common build/dependency directories (node_modules, .git, dist, etc.).",inputSchema:{type:"object",properties:{pattern:{type:"string",description:"Regular expression pattern to search for in file contents."},path:{type:"string",description:"Workspace-relative file or directory path to search in. Defaults to the workspace root."},include:{type:"string",description:"Glob pattern to filter files by name (e.g. '*.ts', '*.{js,ts}'). Only matching files are searched."},exclude:{type:"string",description:"Glob pattern to exclude files by name (e.g. '*.test.ts'). Matching files are skipped."},case_insensitive:{type:"boolean",description:"Case-insensitive matching. Default false."},output_mode:{type:"string",enum:["content","files_with_matches","count"],description:"Output format: 'content' returns matched lines with line numbers (default), 'files_with_matches' returns only file paths with matches, 'count' returns match counts per file."},max_results:{type:"number",description:"Maximum number of matched lines to return (content mode). Default 1000."}},required:["pattern"],additionalProperties:!1},async execute(o){const e=k(o,["pattern"],"pattern"),t=A(o,["path"])||".",r=A(o,["include"]),n=A(o,["exclude"]),c=o.case_insensitive===!0,s=(()=>{const y=o.output_mode;return y==="files_with_matches"||y==="count"?y:"content"})(),a=O(o,["max_results"],at),u=c?"i":"";let m;try{m=new RegExp(e,u)}catch(y){throw new Error(`Invalid regex pattern: ${y instanceof Error?y.message:String(y)}`)}const p=r?G(r):null,g=n?G(n):null,h=await S(i,t),l=R(h.workspaceRoot,await d.realpath(h.targetPath)),M=await d.stat(l);let b;if(M.isFile())b=[l];else if(M.isDirectory())b=await pt(l,p,g,nt);else throw new Error(`Path is neither a file nor a directory: ${l}`);const w=[],P=[],B=[];let L=0,T=0,x=!1;for(const y of b){if(s==="content"&&w.length>=a){x=!0;break}const z={regex:m,maxResults:s==="content"?a-w.length:Number.MAX_SAFE_INTEGER,collectContent:s==="content",stopOnFirstMatch:s==="files_with_matches"},C=await mt(y,h.workspaceRoot,z);if(C.count>0){const I=f.relative(h.workspaceRoot,y).replace(/\\/g,"/");if(B.push(I),L+=C.count,s==="content"){for(const $ of C.matches){const F=Buffer.byteLength($.content,"utf-8")+64;if(T+F>rt){x=!0;break}T+=F,w.push($)}if(x)break;if(w.length>=a){x=!0;break}}else s==="count"&&P.push({file:I,count:C.count})}}const v={ok:!0,tool:"grep",pattern:e,searchPath:_(h.targetPath),outputMode:s,filesSearched:b.length,filesMatched:B.length,totalMatches:L,truncated:x};return s==="content"?v.matches=w:s==="files_with_matches"?v.matches=B:v.matches=P,v}}}function Lt(i,o,e,t){const r=o||"unknown",n=[bt(i),yt(i,e),_t(i,e),kt(i,e),Tt(i),Pt(i,r,t),H(i)];return t&&n.push(V(r,t),Z(r,t),J(r,t)),n}export{Lt as createBuiltInFileTools,R as ensureInsideWorkspace,S as resolveWorkspaceTarget};
4
4