aws-runtime-bridge 1.9.81 → 1.9.82
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.
|
@@ -20,7 +20,7 @@ export interface CliCommandOptions {
|
|
|
20
20
|
platform?: NodeJS.Platform;
|
|
21
21
|
serviceUnitPath?: string;
|
|
22
22
|
executablePath?: string;
|
|
23
|
-
/** Windows 服务日志文件路径(默认
|
|
23
|
+
/** Windows 服务日志文件路径(默认 ~/.aws-bridge/log/awsb.out.log),供 install 重定向与 log 读取。 */
|
|
24
24
|
windowsServiceLogPath?: string;
|
|
25
25
|
/** Windows 服务管理器(基于 node-windows),用于 install 操作;测试时可注入 mock。 */
|
|
26
26
|
windowsServiceManager?: WindowsServiceManager;
|
|
@@ -1,6 +1,6 @@
|
|
|
1
|
-
import{spawnSync as S}from"node:child_process";import{createInterface as
|
|
1
|
+
import{spawnSync as S}from"node:child_process";import{createInterface as T}from"node:readline/promises";import{existsSync as h,mkdirSync as A,readFileSync as L,rmSync as j,unlinkSync as O,writeFileSync as F}from"node:fs";import f from"node:path";import W from"node:os";import{fileURLToPath as R}from"node:url";import{configureStartupConfig as B,printConfigExampleCommand as G}from"./startup-config-wizard.js";import{createWindowsServiceManager as V,isNodeWindowsAvailable as q}from"./windows-service-manager.js";const o="awsb.service",_=`/etc/systemd/system/${o}`,I="awsb",C=`${I}.exe`,k=f.join(W.homedir(),".aws-bridge","log"),D=f.join(k,"awsb.out.log");function H(){const e=R(import.meta.url);return f.resolve(f.dirname(e),"..","..")}function K(e=H()){const r=f.join(e,"package.json");return JSON.parse(L(r,"utf-8")).version||"unknown"}function E(e,r){const t=S(e,r,{shell:process.platform==="win32",stdio:"inherit"});return{status:t.status,error:t.error}}function Y(){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 J(e){process.stdout.write(`${e}
|
|
2
2
|
`)}function z(e){process.stderr.write(`${e}
|
|
3
|
-
`)}function Q(e){return e==="-v"||e==="--version"||e==="version"}function X(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}function Z(){return process.argv[1]?f.resolve(process.argv[1]):
|
|
4
|
-
`)}function c(e,r){return e("systemctl",r)}function l(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 P(e,r){return e==="linux"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd \u73AF\u5883\u3002"),!1)}function
|
|
5
|
-
npm install -g aws-runtime-bridge`),{handled:!0,exitCode:1}}async function ue(e){const r=e.windowsServiceManager;if(!r&&!q())return ae(e.stderr);const t=e.windowsServiceLogPath||
|
|
3
|
+
`)}function Q(e){return e==="-v"||e==="--version"||e==="version"}function X(e){return`"${e.replace(/\\/g,"\\\\").replace(/"/g,'\\"')}"`}function Z(){return process.argv[1]?f.resolve(process.argv[1]):R(import.meta.url)}function ee(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=>X(t)).join(" ")}`,"Restart=on-failure","RestartSec=5s","","[Install]","WantedBy=multi-user.target",""].join(`
|
|
4
|
+
`)}function c(e,r){return e("systemctl",r)}function l(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 P(e,r){return e==="linux"?!0:(r("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd \u73AF\u5883\u3002"),!1)}function U(e){e("Usage: awsb service <install|uninstall|start|stop|restart>")}function re(e){if(!P(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=ee(e.executablePath);try{A(f.dirname(e.serviceUnitPath),{recursive:!0}),F(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=c(e.runCommand,["daemon-reload"]);if(!d(t))return e.stderr(l("\u91CD\u8F7D systemd",t)),{handled:!0,exitCode:t.status??1};const i=c(e.runCommand,["enable",o]);if(!d(i))return e.stderr(l("\u542F\u7528 awsb \u5F00\u673A\u81EA\u542F",i)),{handled:!0,exitCode:i.status??1};const s=c(e.runCommand,["is-active","--quiet",o]);if(s.error)return e.stderr(l("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",s)),{handled:!0,exitCode:1};if(s.status!==0){const n=c(e.runCommand,["start",o]);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(l("\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 te(e){if(!P(e.platform,e.stderr))return{handled:!0,exitCode:1};const r=h(e.serviceUnitPath),t=c(e.runCommand,["is-active","--quiet",o]);if(t.error)return e.stderr(l("\u68C0\u67E5 awsb \u670D\u52A1\u72B6\u6001",t)),{handled:!0,exitCode:1};if(t.status===0){const n=c(e.runCommand,["stop",o]);if(!d(n))return e.stderr(l("\u505C\u6B62 awsb \u670D\u52A1",n)),{handled:!0,exitCode:n.status??1}}const i=c(e.runCommand,["disable",o]);if(i.error||i.status!==0&&r)return e.stderr(l("\u7981\u7528 awsb \u5F00\u673A\u81EA\u542F",i)),{handled:!0,exitCode:i.status??1};if(r)try{O(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 s=c(e.runCommand,["daemon-reload"]);return d(s)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u5378\u8F7D\u3002"),{handled:!0,exitCode:0}):(e.stderr(l("\u91CD\u8F7D systemd",s)),{handled:!0,exitCode:s.status??1})}function ne(e){if(!P(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!h(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=c(e.runCommand,["restart",o]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(l("\u91CD\u542F awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function se(e){if(!P(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!h(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=c(e.runCommand,["start",o]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(l("\u542F\u52A8 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function ie(e){if(!P(e.platform,e.stderr))return{handled:!0,exitCode:1};if(!h(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=c(e.runCommand,["stop",o]);return d(r)?(e.stdout("[runtime-bridge] awsb systemd \u670D\u52A1\u5DF2\u505C\u6B62\u3002"),{handled:!0,exitCode:0}):(e.stderr(l("\u505C\u6B62 awsb \u670D\u52A1",r)),{handled:!0,exitCode:r.status??1})}function ae(e){return e(`[runtime-bridge] \u672A\u68C0\u6D4B\u5230 node-windows \u6A21\u5757\u3002\u8BF7\u91CD\u65B0\u5B89\u88C5 aws-runtime-bridge\uFF1A
|
|
5
|
+
npm install -g aws-runtime-bridge`),{handled:!0,exitCode:1}}async function ue(e){const r=e.windowsServiceManager;if(!r&&!q())return ae(e.stderr);const t=e.windowsServiceLogPath||D,i=f.dirname(e.executablePath),s=W.homedir();try{await(r||V()).install({name:I,description:"awsb runtime bridge",script:e.executablePath,workingDirectory:i,env:[{name:"AWS_BRIDGE_SKIP_SETUP",value:"true"},{name:"AWS_RUNTIME_HOME_DIR",value:s}],logpath:k})}catch(a){const g=a instanceof Error?a:new Error(String(a));return e.stderr(`[runtime-bridge] \u5B89\u88C5 Windows \u670D\u52A1\u5931\u8D25: ${g.message}`),{handled:!0,exitCode:1}}const n=e.runCommand("sc.exe",["start",C]);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 de(e){const r=i=>e.runCommand("sc.exe",i);r(["stop",C]);const t=r(["delete",C]);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})}function oe(e){const r=i=>e.runCommand("sc.exe",i);r(["stop",C]);const t=r(["start",C]);return d(t)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u91CD\u542F\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u91CD\u542F Windows \u670D\u52A1\u5931\u8D25: ${t.error?.message??`\u9000\u51FA\u7801 ${t.status??"unknown"}`}`),{handled:!0,exitCode:t.status??1})}function ce(e){const r=e.runCommand("sc.exe",["start",C]);return d(r)?(e.stdout("[runtime-bridge] awsb Windows \u670D\u52A1\u5DF2\u542F\u52A8\u3002"),{handled:!0,exitCode:0}):(e.stderr(`[runtime-bridge] \u542F\u52A8 Windows \u670D\u52A1\u5931\u8D25: ${r.error?.message??`\u9000\u51FA\u7801 ${r.status??"unknown"}`}`),{handled:!0,exitCode:r.status??1})}function le(e){const r=e.runCommand("sc.exe",["stop",C]);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})}async function me(e,r,t,i){const s=e[1];if(!s||s==="-h"||s==="--help"||s==="help")return U(t),{handled:!0,exitCode:s?0:1};const n={stdout:t,stderr:i,runCommand:r.runCommand||E,platform:r.platform||process.platform,serviceUnitPath:r.serviceUnitPath||_,executablePath:r.executablePath||Z(),windowsServiceManager:r.windowsServiceManager};return n.platform!=="linux"&&n.platform!=="win32"?(i("[runtime-bridge] service \u547D\u4EE4\u4EC5\u652F\u6301 Linux systemd \u4E0E Windows \u670D\u52A1\u73AF\u5883\u3002"),{handled:!0,exitCode:1}):s==="install"?n.platform==="win32"?await ue(n):re(n):s==="uninstall"?n.platform==="win32"?de(n):te(n):s==="restart"?n.platform==="win32"?oe(n):ne(n):s==="start"?n.platform==="win32"?ce(n):se(n):s==="stop"?n.platform==="win32"?le(n):ie(n):(i(`[runtime-bridge] \u672A\u77E5 service \u5B50\u547D\u4EE4: ${s}`),U(t),{handled:!0,exitCode:1})}function M(e){return e?e.split(/\r?\n/).map(r=>r.trim()).filter(r=>/^\d+$/.test(r)).map(Number):[]}function fe(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 M(t.stdout)}const r=S("pgrep",["-f","aws-runtime-bridge"],{encoding:"utf-8"});return M(r.stdout).filter(t=>t!==process.pid)}catch{return[]}}function ge(e,r){if(r==="win32"){S("taskkill",["/PID",String(e),"/F","/T"],{stdio:"ignore"});return}S("kill",["-9",String(e)],{stdio:"ignore"})}function we(e){if(!process.stdin.isTTY)return Promise.resolve("n");const r=T({input:process.stdin,output:process.stdout});return r.question(e).finally(()=>r.close())}async function be(e,r,t){const i=e.runCommand||E,s=e.platform||process.platform,n=e.findRunningBridgeProcesses||fe,a=e.prompt||we,g=e.killBridgeProcess||ge,u=e.globalModulePath||Y;r("[runtime-bridge] \u6B63\u5728\u66F4\u65B0 aws-runtime-bridge \u5230\u6700\u65B0\u7248\u672C...");const m=n(s).filter(w=>w!==process.pid);let x=!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 w=(await a("\u662F\u5426\u5173\u95ED\u4E0A\u8FF0\u8FDB\u7A0B\u4EE5\u7EE7\u7EED\u66F4\u65B0? (y/N) ")).trim().toLowerCase();if(w==="y"||w==="yes")for(const b of m)try{g(b,s),r(`[runtime-bridge] \u5DF2\u5173\u95ED\u8FDB\u7A0B PID: ${b}`)}catch(y){const N=y instanceof Error?y:new Error(String(y));t(`[runtime-bridge] \u5173\u95ED\u8FDB\u7A0B PID ${b} \u5931\u8D25: ${N.message}`)}else x=!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 $=u();if($){const w=f.join($,"aws-runtime-bridge");if(h(w)){r("[runtime-bridge] \u6E05\u7406\u65E7\u7248\u672C\u6B8B\u7559\u76EE\u5F55...");try{j(w,{recursive:!0,force:!0})}catch(b){const y=b instanceof Error?b:new Error(String(b));t(`[runtime-bridge] \u6E05\u7406\u6B8B\u7559\u76EE\u5F55\u5931\u8D25\uFF08\u5C06\u5C1D\u8BD5\u7EE7\u7EED\u5B89\u88C5\uFF09: ${y.message}`)}}}const p=["install","-g","aws-runtime-bridge@latest","--omit=optional"];x&&p.push("--force");const v=i("npm",p);return v.error?(t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25: ${v.error.message}`),{handled:!0,exitCode:1}):v.status!==0?(t(`[runtime-bridge] \u66F4\u65B0\u5931\u8D25\uFF0Cnpm \u9000\u51FA\u7801: ${v.status??"unknown"}`),{handled:!0,exitCode:v.status??1}):(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})}function he(e,r,t){const s=(e.argv||process.argv.slice(2)).slice(1).some(u=>u==="--follow"||u==="-f"),n=e.platform||process.platform,a=e.runCommand||E,g=e.serviceUnitPath||_;if(n==="linux"){if(!h(g))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",o];s?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||D;if(!h(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}"`+(s?" -Wait":""),x=a("powershell",["-NoProfile","-Command",m]);return x.error?(t(`[runtime-bridge] \u8BFB\u53D6\u65E5\u5FD7\u5931\u8D25: ${x.error.message}`),{handled:!0,exitCode:1}):{handled:!0,exitCode:x.status??0}}return t("[runtime-bridge] awsb log \u4EC5\u652F\u6301 Linux systemd \u4E0E Windows \u670D\u52A1\u73AF\u5883\u3002"),{handled:!0,exitCode:1}}async function pe(e={}){const r=e.argv||process.argv.slice(2),t=r[0],i=e.stdout||J,s=e.stderr||z;if(Q(t))return i(`aws-runtime-bridge ${K(e.packageRoot)}`),{handled:!0,exitCode:0};if(t==="update")return await be(e,i,s);if(t==="config"){const n=r[1];if(n==="example"||n==="minimal"||n==="--minimal"||n==="-m")return{handled:!0,exitCode:await(e.configureExample||G)()==="created"?0:1};const g=await(e.configure||B)();return{handled:!0,exitCode:g==="configured"||g==="created"?0:1}}return t==="service"?await me(r,e,i,s):t==="log"?he(e,i,s):{handled:!1,exitCode:0}}export{ee as createSystemdUnitContent,pe as handleCliCommand,K as readPackageVersion};
|
|
6
6
|
|
|
@@ -9,8 +9,9 @@ export declare function formatStartupTimestamp(date: Date): string;
|
|
|
9
9
|
export declare function sanitizeLogFileNameSegment(segment: string): string;
|
|
10
10
|
/**
|
|
11
11
|
* 构建 MCP 启动日志文件路径。
|
|
12
|
-
* 路径格式:{logDir}/{roleName}-{instanceName}
|
|
12
|
+
* 路径格式:{logDir}/agent/{roleName}-{instanceName}-{startupTimestamp}.log
|
|
13
13
|
* roleName/instanceName 优先使用传入值,未传入时使用环境变量或 "unknown-*" 兜底。
|
|
14
|
+
* 所有 agent 日志统一放在 agent/ 子目录下,通过文件名中的 role-instance 区分。
|
|
14
15
|
*/
|
|
15
16
|
export declare function buildStartupLogFilePath(options?: {
|
|
16
17
|
homeDir?: string;
|
|
@@ -22,7 +23,8 @@ export declare function buildStartupLogFilePath(options?: {
|
|
|
22
23
|
export declare function initializeStartupLogFile(): string | null;
|
|
23
24
|
/**
|
|
24
25
|
* 在 Agent 注册后更新日志文件路径的角色-实例标识。
|
|
25
|
-
* 主流程:使用真实的 roleName/instanceName
|
|
26
|
+
* 主流程:使用真实的 roleName/instanceName 重新计算文件名,若与原路径不同则重命名文件。
|
|
27
|
+
* 所有 agent 日志统一存放在 agent/ 子目录下,仅文件名中的 role-instance 部分发生变化。
|
|
26
28
|
*/
|
|
27
29
|
export declare function updateStartupLogIdentity(roleName: string, instanceName: string): void;
|
|
28
30
|
/**
|
|
@@ -1,3 +1,3 @@
|
|
|
1
|
-
import*as
|
|
2
|
-
`;try{
|
|
1
|
+
import*as u from"node:fs";import*as p from"node:os";import*as i from"node:path";import{format as m}from"node:util";const S=new Date,w=new Set(["0","false","off","no"]),d=200;let r,c=null,g=null;const l=[];function a(e,n=2){return String(e).padStart(n,"0")}function D(e){return[e.getFullYear(),a(e.getMonth()+1),a(e.getDate()),"-",a(e.getHours()),a(e.getMinutes()),a(e.getSeconds()),"-",a(e.getMilliseconds(),3)].join("")}function L(e){const n=new RegExp("[\0-]","g");return e.replace(/[<>:"/\\|?*]/g,"_").replace(n,"_").replace(/\s+/g," ").trim().replace(/[. ]+$/g,"")||"workspace"}function _(e={}){const n=e.logDir||i.join(e.homeDir||p.homedir(),".aws-bridge","log"),t=L(e.roleName||process.env.AWS_ROLE_NAME||"unknown-role"),o=L(e.instanceName||process.env.AWS_AGENT_ID||"unknown-instance"),s=i.join(n,"agent"),h=D(e.startupTime||S);return i.join(s,`${t}-${o}-${h}.log`)}function M(){if(r!==void 0)return r;const e=String(process.env.AWS_MCP_FILE_LOG||"").trim().toLowerCase();if(w.has(e))return r=null,null;try{return c=process.env.AWS_MCP_LOG_DIR||void 0||i.join(p.homedir(),".aws-bridge","log"),r=_({logDir:c,roleName:process.env.AWS_ROLE_NAME||void 0,instanceName:process.env.AWS_AGENT_ID||void 0}),u.mkdirSync(i.dirname(r),{recursive:!0}),r}catch(n){return r=null,c=null,console.warn("[Logger] \u65E0\u6CD5\u521B\u5EFA MCP \u542F\u52A8\u65E5\u5FD7\u6587\u4EF6:",n),null}}function C(e,n){if(r==null||!c)return;const t=r,o=_({logDir:c,roleName:e,instanceName:n,startupTime:S});if(t!==o)try{u.mkdirSync(i.dirname(o),{recursive:!0}),u.renameSync(t,o),r=o,console.log(`[Logger] MCP \u542F\u52A8\u65E5\u5FD7\u8DEF\u5F84\u5DF2\u66F4\u65B0: ${t} -> ${o}`)}catch(s){console.warn("[Logger] \u65E0\u6CD5\u66F4\u65B0 MCP \u542F\u52A8\u65E5\u5FD7\u8EAB\u4EFD\u6807\u8BC6:",s)}}function E(e,n){if(r===void 0)return;const t=r;if(!t)return;const o=`${new Date().toISOString()} [${e.toUpperCase()}] ${m(...n)}
|
|
2
|
+
`;try{u.appendFileSync(t,o,"utf8")}catch(s){r=null,console.warn("[Logger] \u65E0\u6CD5\u5199\u5165 MCP \u542F\u52A8\u65E5\u5FD7\u6587\u4EF6:",s)}}function f(e,n){const t={level:e,message:m(...n),timestamp:new Date().toISOString()};switch(e){case"info":console.error(...n);break;case"warn":console.warn(...n);break;case"error":console.error(...n);break}E(e,n),N(t)}function P(e){if(g=e,!!e)for(const n of l)e(n)}function N(e){l.push(e),l.length>d&&l.splice(0,l.length-d),g&&g(e)}const F={info:(...e)=>f("info",e),warn:(...e)=>f("warn",e),error:(...e)=>f("error",e)};export{_ as buildStartupLogFilePath,D as formatStartupTimestamp,M as initializeStartupLogFile,F as logger,L as sanitizeLogFileNameSegment,P as setMcpNodeLogSink,C as updateStartupLogIdentity};
|
|
3
3
|
|
|
@@ -10,7 +10,7 @@ describe("logger startup log path", () => {
|
|
|
10
10
|
it("sanitizes unsafe filename characters", () => {
|
|
11
11
|
assert.equal(sanitizeLogFileNameSegment('demo:bad*name?'), "demo_bad_name_");
|
|
12
12
|
});
|
|
13
|
-
it("builds log path under the aws bridge log directory with role-instance
|
|
13
|
+
it("builds log path under the aws bridge log agent directory with role-instance in filename", () => {
|
|
14
14
|
const startupTime = new Date(2026, 4, 5, 9, 8, 7, 6);
|
|
15
15
|
const logFilePath = buildStartupLogFilePath({
|
|
16
16
|
homeDir: "C:/Users/dev",
|
|
@@ -18,6 +18,6 @@ describe("logger startup log path", () => {
|
|
|
18
18
|
instanceName: "128M",
|
|
19
19
|
startupTime,
|
|
20
20
|
});
|
|
21
|
-
assert.equal(logFilePath, path.join("C:/Users/dev", ".aws-bridge", "log", "
|
|
21
|
+
assert.equal(logFilePath, path.join("C:/Users/dev", ".aws-bridge", "log", "agent", "测试助手-128M-20260505-090807-006.log"));
|
|
22
22
|
});
|
|
23
23
|
});
|