@xiaohhhh1/canvas-agent 0.4.5 → 0.4.6

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.
package/dist/index.js CHANGED
@@ -2,9 +2,12 @@
2
2
  import { startHttpServer } from "./server/http.js";
3
3
  import { ensureHttpServer } from "./server/ensure-http.js";
4
4
  import { startMcpServer } from "./server/mcp.js";
5
+ import { startHttpSupervisor } from "./server/supervisor.js";
5
6
  if (process.argv[2] === "mcp") {
6
7
  await ensureHttpServer();
7
8
  await startMcpServer();
8
9
  }
10
+ else if (process.argv[2] === "watch")
11
+ startHttpSupervisor();
9
12
  else
10
13
  startHttpServer();
@@ -19,7 +19,7 @@ export async function ensureHttpServer() {
19
19
  const entry = process.argv[1];
20
20
  if (!entry)
21
21
  throw new Error("无法定位 Canvas Agent 启动文件");
22
- const child = spawn(process.execPath, [entry, "serve"], { detached: true, stdio: "ignore", windowsHide: true });
22
+ const child = spawn(process.execPath, [entry, "watch"], { detached: true, stdio: "ignore", windowsHide: true });
23
23
  child.unref();
24
24
  for (let attempt = 0; attempt < 40; attempt += 1) {
25
25
  await new Promise((resolve) => setTimeout(resolve, 250));
@@ -10,6 +10,7 @@ import { startRelayBridge } from "../relay-bridge.js";
10
10
  import { logger } from "../utils/logger.js";
11
11
  import { windowsRootExecutable, windowsSystemExecutable } from "../utils/windows.js";
12
12
  import { WorkflowManager } from "../workflow/manager.js";
13
+ import { AGENT_REPLACED_EXIT_CODE } from "./supervisor.js";
13
14
  /** 启动仅监听本机的 Canvas Agent HTTP 服务。 */
14
15
  export function startHttpServer() {
15
16
  const config = loadConfig(true);
@@ -64,8 +65,8 @@ export function startHttpServer() {
64
65
  app.post("/agent/shutdown", (_req, res) => {
65
66
  res.json({ ok: true, version: VERSION });
66
67
  setTimeout(() => {
67
- httpServer?.close(() => process.exit(0));
68
- setTimeout(() => process.exit(0), 2000).unref();
68
+ httpServer?.close(() => process.exit(AGENT_REPLACED_EXIT_CODE));
69
+ setTimeout(() => process.exit(AGENT_REPLACED_EXIT_CODE), 2000).unref();
69
70
  }, 50).unref();
70
71
  });
71
72
  app.get("/events", (req, res) => session.openEvents(requestUrl(req, config), res));
@@ -0,0 +1,4 @@
1
+ export declare const AGENT_REPLACED_EXIT_CODE = 75;
2
+ export declare function shouldRestartAgent(code: number | null, stopping: boolean): boolean;
3
+ /** Keep the local HTTP worker alive, but step aside when a newer package replaces it. */
4
+ export declare function startHttpSupervisor(): void;
@@ -0,0 +1,38 @@
1
+ import { spawn } from "node:child_process";
2
+ export const AGENT_REPLACED_EXIT_CODE = 75;
3
+ const RESTART_DELAY_MS = 1000;
4
+ export function shouldRestartAgent(code, stopping) {
5
+ return !stopping && code !== AGENT_REPLACED_EXIT_CODE;
6
+ }
7
+ /** Keep the local HTTP worker alive, but step aside when a newer package replaces it. */
8
+ export function startHttpSupervisor() {
9
+ const entry = process.argv[1];
10
+ if (!entry)
11
+ throw new Error("无法定位 Canvas Agent 启动文件");
12
+ let child;
13
+ let stopping = false;
14
+ let restartTimer;
15
+ const launch = () => {
16
+ if (stopping)
17
+ return;
18
+ child = spawn(process.execPath, [entry, "serve"], { stdio: "ignore", windowsHide: true });
19
+ child.once("exit", (code) => {
20
+ child = undefined;
21
+ if (!shouldRestartAgent(code, stopping))
22
+ return void process.exit(0);
23
+ restartTimer = setTimeout(launch, RESTART_DELAY_MS);
24
+ });
25
+ };
26
+ const stop = () => {
27
+ if (stopping)
28
+ return;
29
+ stopping = true;
30
+ if (restartTimer)
31
+ clearTimeout(restartTimer);
32
+ child?.kill();
33
+ setTimeout(() => process.exit(0), 2000).unref();
34
+ };
35
+ process.once("SIGINT", stop);
36
+ process.once("SIGTERM", stop);
37
+ launch();
38
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@xiaohhhh1/canvas-agent",
3
- "version": "0.4.5",
3
+ "version": "0.4.6",
4
4
  "type": "module",
5
5
  "main": "./dist/index.js",
6
6
  "types": "./dist/index.d.ts",