ai-project-manage-cli 7.1.32 → 7.1.33

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
@@ -7259,10 +7259,27 @@ function readRegistry(path19) {
7259
7259
  try {
7260
7260
  const parsed = JSON.parse(readFileSync17(path19, "utf8"));
7261
7261
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
7262
- const agentId = parsed.agentId;
7263
- if (typeof agentId === "string" && agentId.trim()) {
7264
- return { agentId: agentId.trim() };
7262
+ const raw = parsed;
7263
+ const state = {};
7264
+ if (typeof raw.agentId === "string" && raw.agentId.trim()) {
7265
+ state.agentId = raw.agentId.trim();
7265
7266
  }
7267
+ if (typeof raw.taskId === "string" && raw.taskId.trim()) {
7268
+ state.taskId = raw.taskId.trim();
7269
+ }
7270
+ if (typeof raw.messageId === "string" && raw.messageId.trim()) {
7271
+ state.messageId = raw.messageId.trim();
7272
+ }
7273
+ if (typeof raw.action === "string" && raw.action.trim()) {
7274
+ state.action = raw.action.trim();
7275
+ }
7276
+ if (raw.status === "TYPING" || raw.status === "SUCCESS" || raw.status === "FAILED" || raw.status === "CANCELLED") {
7277
+ state.status = raw.status;
7278
+ }
7279
+ if (typeof raw.updatedAt === "string" && raw.updatedAt.trim()) {
7280
+ state.updatedAt = raw.updatedAt.trim();
7281
+ }
7282
+ return state;
7266
7283
  }
7267
7284
  } catch {
7268
7285
  }
@@ -8385,6 +8402,19 @@ function formatCursorRunFailure(runId, options) {
8385
8402
  }
8386
8403
  return `Cursor run \u5931\u8D25: ${runId} \u2014 ${details.join("\uFF1B")}`;
8387
8404
  }
8405
+ function persistSessionAgentId(ctx, agentId) {
8406
+ if (ctx.skipSessionAgentRegistry || !ctx.user) return;
8407
+ saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agentId);
8408
+ }
8409
+ function invalidatePersistedAgentId(ctx) {
8410
+ if (ctx.skipSessionAgentRegistry) {
8411
+ ctx.onInvalidatePersistedAgentId?.();
8412
+ return;
8413
+ }
8414
+ if (ctx.user) {
8415
+ clearSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user);
8416
+ }
8417
+ }
8388
8418
  async function obtainAgent(ctx) {
8389
8419
  const agentOptions = {
8390
8420
  apiKey: ctx.apiKey,
@@ -8403,32 +8433,28 @@ async function obtainAgent(ctx) {
8403
8433
  ...ctx.mcpServers ? { mcpServers: ctx.mcpServers } : {}
8404
8434
  };
8405
8435
  const explicitAgentId = ctx.resumeAgentId?.trim();
8406
- const savedAgentId = explicitAgentId || (ctx.user ? loadSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user) : void 0);
8436
+ const savedAgentId = explicitAgentId || (!ctx.skipSessionAgentRegistry && ctx.user ? loadSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user) : void 0);
8407
8437
  if (savedAgentId) {
8408
8438
  try {
8409
8439
  const agent = await Agent.resume(savedAgentId, agentOptions);
8410
8440
  console.log(
8411
8441
  `[apm] \u590D\u7528 Agent user=${ctx.user} agentId=${savedAgentId}${explicitAgentId ? "\uFF08\u53C2\u6570\u6307\u5B9A\uFF09" : ""}`
8412
8442
  );
8413
- if (ctx.user) {
8414
- saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agent.agentId);
8415
- }
8443
+ persistSessionAgentId(ctx, agent.agentId);
8416
8444
  return { agent, resumed: true };
8417
8445
  } catch (err) {
8418
8446
  console.warn(
8419
8447
  `[apm] \u590D\u7528 Agent \u5931\u8D25\uFF08agentId=${savedAgentId}\uFF09\uFF0C\u56DE\u9000\u4E3A\u65B0\u5EFA:`,
8420
8448
  err instanceof Error ? err.message : err
8421
8449
  );
8422
- if (!explicitAgentId && ctx.user) {
8423
- clearSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user);
8450
+ if (!explicitAgentId) {
8451
+ invalidatePersistedAgentId(ctx);
8424
8452
  }
8425
8453
  }
8426
8454
  }
8427
8455
  try {
8428
8456
  const agent = await Agent.create(agentOptions);
8429
- if (ctx.user) {
8430
- saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agent.agentId);
8431
- }
8457
+ persistSessionAgentId(ctx, agent.agentId);
8432
8458
  return { agent, resumed: false };
8433
8459
  } catch (err) {
8434
8460
  if (ctx.enableSandbox && err instanceof Error && /sandbox/i.test(err.message)) {
@@ -8474,8 +8500,19 @@ async function runCursorAgent(cfg, ctx, options) {
8474
8500
  mode: ctx.mode,
8475
8501
  resumeAgentId: ctx.resumeAgentId,
8476
8502
  customTools,
8477
- enableSandbox
8503
+ enableSandbox,
8504
+ skipSessionAgentRegistry: options?.skipSessionAgentRegistry,
8505
+ onInvalidatePersistedAgentId: options?.onInvalidatePersistedAgentId
8478
8506
  });
8507
+ const invalidateAgentMapping = () => {
8508
+ invalidatePersistedAgentId({
8509
+ workdir,
8510
+ sessionId: ctx.sessionId,
8511
+ user: ctx.user,
8512
+ skipSessionAgentRegistry: options?.skipSessionAgentRegistry,
8513
+ onInvalidatePersistedAgentId: options?.onInvalidatePersistedAgentId
8514
+ });
8515
+ };
8479
8516
  const eventSession = options?.eventSession ?? new EventSession(prompt);
8480
8517
  const syncRemoteLog = options?.createRemoteLogSync ? options.createRemoteLogSync(agent.agentId) : options?.skipRemoteLogSync ? noopRemoteLogSync : createThrottledCursorMessageLogSync(
8481
8518
  cfg,
@@ -8543,7 +8580,7 @@ async function runCursorAgent(cfg, ctx, options) {
8543
8580
  });
8544
8581
  console.error(`[apm] ${failureMessage}`);
8545
8582
  if (resumed) {
8546
- clearSessionAgentId(workdir, ctx.sessionId, ctx.user);
8583
+ invalidateAgentMapping();
8547
8584
  }
8548
8585
  throw new Error(failureMessage);
8549
8586
  }
@@ -8582,7 +8619,7 @@ async function runCursorAgent(cfg, ctx, options) {
8582
8619
  } catch (err) {
8583
8620
  if (err instanceof CursorAgentError) {
8584
8621
  if (resumed) {
8585
- clearSessionAgentId(workdir, ctx.sessionId, ctx.user);
8622
+ invalidateAgentMapping();
8586
8623
  }
8587
8624
  throw new Error(
8588
8625
  `Cursor \u542F\u52A8\u5931\u8D25: ${err.message}${err.isRetryable ? "\uFF08\u53EF\u91CD\u8BD5\uFF09" : ""}`
@@ -2300,6 +2300,19 @@ function formatCursorRunFailure(runId, options) {
2300
2300
  }
2301
2301
  return `Cursor run \u5931\u8D25: ${runId} \u2014 ${details.join("\uFF1B")}`;
2302
2302
  }
2303
+ function persistSessionAgentId(ctx, agentId) {
2304
+ if (ctx.skipSessionAgentRegistry || !ctx.user) return;
2305
+ saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agentId);
2306
+ }
2307
+ function invalidatePersistedAgentId(ctx) {
2308
+ if (ctx.skipSessionAgentRegistry) {
2309
+ ctx.onInvalidatePersistedAgentId?.();
2310
+ return;
2311
+ }
2312
+ if (ctx.user) {
2313
+ clearSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user);
2314
+ }
2315
+ }
2303
2316
  async function obtainAgent(ctx) {
2304
2317
  const agentOptions = {
2305
2318
  apiKey: ctx.apiKey,
@@ -2318,32 +2331,28 @@ async function obtainAgent(ctx) {
2318
2331
  ...ctx.mcpServers ? { mcpServers: ctx.mcpServers } : {}
2319
2332
  };
2320
2333
  const explicitAgentId = ctx.resumeAgentId?.trim();
2321
- const savedAgentId = explicitAgentId || (ctx.user ? loadSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user) : void 0);
2334
+ const savedAgentId = explicitAgentId || (!ctx.skipSessionAgentRegistry && ctx.user ? loadSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user) : void 0);
2322
2335
  if (savedAgentId) {
2323
2336
  try {
2324
2337
  const agent = await Agent.resume(savedAgentId, agentOptions);
2325
2338
  console.log(
2326
2339
  `[apm] \u590D\u7528 Agent user=${ctx.user} agentId=${savedAgentId}${explicitAgentId ? "\uFF08\u53C2\u6570\u6307\u5B9A\uFF09" : ""}`
2327
2340
  );
2328
- if (ctx.user) {
2329
- saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agent.agentId);
2330
- }
2341
+ persistSessionAgentId(ctx, agent.agentId);
2331
2342
  return { agent, resumed: true };
2332
2343
  } catch (err) {
2333
2344
  console.warn(
2334
2345
  `[apm] \u590D\u7528 Agent \u5931\u8D25\uFF08agentId=${savedAgentId}\uFF09\uFF0C\u56DE\u9000\u4E3A\u65B0\u5EFA:`,
2335
2346
  err instanceof Error ? err.message : err
2336
2347
  );
2337
- if (!explicitAgentId && ctx.user) {
2338
- clearSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user);
2348
+ if (!explicitAgentId) {
2349
+ invalidatePersistedAgentId(ctx);
2339
2350
  }
2340
2351
  }
2341
2352
  }
2342
2353
  try {
2343
2354
  const agent = await Agent.create(agentOptions);
2344
- if (ctx.user) {
2345
- saveSessionAgentId(ctx.workdir, ctx.sessionId, ctx.user, agent.agentId);
2346
- }
2355
+ persistSessionAgentId(ctx, agent.agentId);
2347
2356
  return { agent, resumed: false };
2348
2357
  } catch (err) {
2349
2358
  if (ctx.enableSandbox && err instanceof Error && /sandbox/i.test(err.message)) {
@@ -2389,8 +2398,19 @@ async function runCursorAgent(cfg, ctx, options) {
2389
2398
  mode: ctx.mode,
2390
2399
  resumeAgentId: ctx.resumeAgentId,
2391
2400
  customTools,
2392
- enableSandbox
2401
+ enableSandbox,
2402
+ skipSessionAgentRegistry: options?.skipSessionAgentRegistry,
2403
+ onInvalidatePersistedAgentId: options?.onInvalidatePersistedAgentId
2393
2404
  });
2405
+ const invalidateAgentMapping = () => {
2406
+ invalidatePersistedAgentId({
2407
+ workdir,
2408
+ sessionId: ctx.sessionId,
2409
+ user: ctx.user,
2410
+ skipSessionAgentRegistry: options?.skipSessionAgentRegistry,
2411
+ onInvalidatePersistedAgentId: options?.onInvalidatePersistedAgentId
2412
+ });
2413
+ };
2394
2414
  const eventSession = options?.eventSession ?? new EventSession(prompt);
2395
2415
  const syncRemoteLog = options?.createRemoteLogSync ? options.createRemoteLogSync(agent.agentId) : options?.skipRemoteLogSync ? noopRemoteLogSync : createThrottledCursorMessageLogSync(
2396
2416
  cfg,
@@ -2458,7 +2478,7 @@ async function runCursorAgent(cfg, ctx, options) {
2458
2478
  });
2459
2479
  console.error(`[apm] ${failureMessage}`);
2460
2480
  if (resumed) {
2461
- clearSessionAgentId(workdir, ctx.sessionId, ctx.user);
2481
+ invalidateAgentMapping();
2462
2482
  }
2463
2483
  throw new Error(failureMessage);
2464
2484
  }
@@ -2497,7 +2517,7 @@ async function runCursorAgent(cfg, ctx, options) {
2497
2517
  } catch (err) {
2498
2518
  if (err instanceof CursorAgentError) {
2499
2519
  if (resumed) {
2500
- clearSessionAgentId(workdir, ctx.sessionId, ctx.user);
2520
+ invalidateAgentMapping();
2501
2521
  }
2502
2522
  throw new Error(
2503
2523
  `Cursor \u542F\u52A8\u5931\u8D25: ${err.message}${err.isRetryable ? "\uFF08\u53EF\u91CD\u8BD5\uFF09" : ""}`
@@ -2525,10 +2545,27 @@ function readRegistry2(path) {
2525
2545
  try {
2526
2546
  const parsed = JSON.parse(readFileSync5(path, "utf8"));
2527
2547
  if (parsed && typeof parsed === "object" && !Array.isArray(parsed)) {
2528
- const agentId = parsed.agentId;
2529
- if (typeof agentId === "string" && agentId.trim()) {
2530
- return { agentId: agentId.trim() };
2548
+ const raw = parsed;
2549
+ const state = {};
2550
+ if (typeof raw.agentId === "string" && raw.agentId.trim()) {
2551
+ state.agentId = raw.agentId.trim();
2552
+ }
2553
+ if (typeof raw.taskId === "string" && raw.taskId.trim()) {
2554
+ state.taskId = raw.taskId.trim();
2555
+ }
2556
+ if (typeof raw.messageId === "string" && raw.messageId.trim()) {
2557
+ state.messageId = raw.messageId.trim();
2531
2558
  }
2559
+ if (typeof raw.action === "string" && raw.action.trim()) {
2560
+ state.action = raw.action.trim();
2561
+ }
2562
+ if (raw.status === "TYPING" || raw.status === "SUCCESS" || raw.status === "FAILED" || raw.status === "CANCELLED") {
2563
+ state.status = raw.status;
2564
+ }
2565
+ if (typeof raw.updatedAt === "string" && raw.updatedAt.trim()) {
2566
+ state.updatedAt = raw.updatedAt.trim();
2567
+ }
2568
+ return state;
2532
2569
  }
2533
2570
  } catch {
2534
2571
  }
@@ -2539,16 +2576,46 @@ function writeRegistry2(path, registry) {
2539
2576
  writeFileSync5(path, `${JSON.stringify(registry, null, 2)}
2540
2577
  `, "utf8");
2541
2578
  }
2579
+ function syncWebIdeTaskState(workdir, taskId, patch) {
2580
+ const trimmedTaskId = taskId.trim();
2581
+ if (!trimmedTaskId) return;
2582
+ const path = registryPath2(workdir, trimmedTaskId);
2583
+ const current = readRegistry2(path);
2584
+ const next = {
2585
+ ...current,
2586
+ taskId: trimmedTaskId,
2587
+ updatedAt: patch.updatedAt ?? (/* @__PURE__ */ new Date()).toISOString()
2588
+ };
2589
+ if (patch.agentId !== void 0) {
2590
+ const agentId = patch.agentId.trim();
2591
+ if (agentId) next.agentId = agentId;
2592
+ else delete next.agentId;
2593
+ }
2594
+ if (patch.messageId !== void 0) {
2595
+ const messageId = patch.messageId.trim();
2596
+ if (messageId) next.messageId = messageId;
2597
+ else delete next.messageId;
2598
+ }
2599
+ if (patch.action !== void 0) {
2600
+ const action = patch.action.trim();
2601
+ if (action) next.action = action;
2602
+ else delete next.action;
2603
+ }
2604
+ if (patch.status !== void 0) {
2605
+ next.status = patch.status;
2606
+ }
2607
+ writeRegistry2(path, next);
2608
+ }
2542
2609
  function loadWebIdeAgentId(workdir, taskId) {
2543
2610
  return readRegistry2(registryPath2(workdir, taskId)).agentId;
2544
2611
  }
2545
2612
  function saveWebIdeAgentId(workdir, taskId, agentId) {
2546
- writeRegistry2(registryPath2(workdir, taskId), { agentId });
2613
+ syncWebIdeTaskState(workdir, taskId, { agentId });
2547
2614
  }
2548
2615
  function clearWebIdeAgentId(workdir, taskId) {
2549
2616
  const path = registryPath2(workdir, taskId);
2550
2617
  if (!existsSync4(path)) return;
2551
- writeRegistry2(path, {});
2618
+ syncWebIdeTaskState(workdir, taskId, { agentId: "" });
2552
2619
  }
2553
2620
 
2554
2621
  // src/commands/clean-webide-cache.ts
@@ -3873,6 +3940,13 @@ var WEBIDE_CODE_CHANGE_ACTIONS = /* @__PURE__ */ new Set([
3873
3940
  function shouldCommitAfterWebIdeMessage(action) {
3874
3941
  return WEBIDE_CODE_CHANGE_ACTIONS.has(action);
3875
3942
  }
3943
+ function syncLocalTaskStatus(workdir, taskId, msg, status) {
3944
+ syncWebIdeTaskState(workdir, taskId, {
3945
+ messageId: msg.messageId,
3946
+ action: msg.action,
3947
+ status
3948
+ });
3949
+ }
3876
3950
  async function updateStatus(cfg, messageId, status) {
3877
3951
  const api = createApmApiClient(cfg);
3878
3952
  await api.cli.webideUpdateMessageStatus({ id: messageId, status });
@@ -3894,6 +3968,7 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
3894
3968
  `[apm] webide-message action=${msg.action} taskId=${taskId} messageId=${messageId}`
3895
3969
  );
3896
3970
  await updateStatus(cfg, messageId, "TYPING");
3971
+ syncLocalTaskStatus(workdir, taskId, msg, "TYPING");
3897
3972
  const eventSession = new EventSession(msg.content);
3898
3973
  const prepAgentId = loadWebIdeAgentId(workdir, taskId) ?? "webide-cli-prep";
3899
3974
  const logSyncRef = {
@@ -4101,6 +4176,8 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
4101
4176
  }),
4102
4177
  enableWebIdePlanTools: true,
4103
4178
  enableSandbox: false,
4179
+ skipSessionAgentRegistry: true,
4180
+ onInvalidatePersistedAgentId: () => clearWebIdeAgentId(workdir, taskId),
4104
4181
  taskId,
4105
4182
  terminalRpc: options?.terminalRpc,
4106
4183
  createRemoteLogSync: (agentId) => {
@@ -4151,6 +4228,7 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
4151
4228
  tokenUsage
4152
4229
  );
4153
4230
  await setError(cfg, messageId, detail);
4231
+ syncLocalTaskStatus(workdir, taskId, msg, "FAILED");
4154
4232
  return;
4155
4233
  }
4156
4234
  if (outcome.status === "cancelled" || signal.aborted) {
@@ -4161,6 +4239,7 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
4161
4239
  tokenUsage
4162
4240
  );
4163
4241
  await updateStatus(cfg, messageId, "CANCELLED");
4242
+ syncLocalTaskStatus(workdir, taskId, msg, "CANCELLED");
4164
4243
  return;
4165
4244
  }
4166
4245
  const fallback = resolveMessageReplyFallback({
@@ -4260,6 +4339,7 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
4260
4339
  tokenUsage
4261
4340
  );
4262
4341
  await updateStatus(cfg, messageId, "SUCCESS");
4342
+ syncLocalTaskStatus(workdir, taskId, msg, "SUCCESS");
4263
4343
  console.log(
4264
4344
  `[apm] webide-message \u5B8C\u6210 action=${msg.action} messageId=${messageId} agentId=${outcome.agentId}`
4265
4345
  );
@@ -4287,6 +4367,7 @@ async function handleWebIdeInboundMessage(cfg, msg, signal, options) {
4287
4367
  statusErr instanceof Error ? statusErr.message : statusErr
4288
4368
  );
4289
4369
  }
4370
+ syncLocalTaskStatus(workdir, taskId, msg, "FAILED");
4290
4371
  }
4291
4372
  }
4292
4373
 
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "ai-project-manage-cli",
3
- "version": "7.1.32",
3
+ "version": "7.1.33",
4
4
  "description": "命令行工具:后续用于调用平台后端 API 完成运维与自动化操作",
5
5
  "type": "module",
6
6
  "private": false,