@agimon-ai/po-file-edit 0.0.0-alpha.197 → 0.0.0-alpha.199

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.
@@ -24,6 +24,7 @@ var __toESM = (mod, isNodeMode, target) => (target = mod != null ? __create(__ge
24
24
  enumerable: true
25
25
  }) : target, mod));
26
26
  //#endregion
27
+ const require_package = require("../package-ZnJMUINx.cjs");
27
28
  let _agimon_ai_po_core_serverPlugin = require("@agimon-ai/po-core/serverPlugin");
28
29
  let _agimon_ai_po_core_extensionFile = require("@agimon-ai/po-core/extensionFile");
29
30
  let _agimon_ai_po_core_platform = require("@agimon-ai/po-core/platform");
@@ -1487,20 +1488,35 @@ function createSessionPresence(options) {
1487
1488
  }
1488
1489
  };
1489
1490
  return {
1490
- toolResult: {
1491
- event: "tool_result",
1491
+ operationAfter: {
1492
+ event: "operation_after",
1492
1493
  async handle(event, execution) {
1493
- const toolName = event.toolName;
1494
- if (execution.sessionId !== sessionId || typeof toolName !== "string" || !Array.isArray(event.content)) return;
1495
- const content = event.content;
1494
+ if (execution.sessionId !== sessionId || event.outcome !== "executed") return;
1495
+ const direct = event.operation.kind === "tool";
1496
+ const name = event.operation.name;
1497
+ const fsWrite = event.operation.kind === "codemode" && [
1498
+ "fs.writeFile",
1499
+ "fs.appendFile",
1500
+ "fs.copyFile",
1501
+ "fs.rename"
1502
+ ].includes(name);
1503
+ const result = event.result;
1504
+ if (!direct && !fsWrite) return;
1505
+ const content = result && typeof result === "object" && "content" in result && Array.isArray(result.content) ? result.content : [];
1506
+ const toolName = fsWrite ? "write" : name;
1507
+ const args = fsWrite ? { path: event.args[["fs.copyFile", "fs.rename"].includes(name) ? 1 : 0] } : event.args[0];
1496
1508
  const next = await safely(() => observePresence(presence, sessionAgent, {
1497
1509
  toolName,
1498
- args: event.args,
1510
+ args,
1499
1511
  content,
1500
- isError: event.isError === true,
1512
+ isError: !!(result && typeof result === "object" && "isError" in result && result.isError),
1501
1513
  cwd: execution.cwd
1502
1514
  }, void 0, options.files));
1503
- return next === void 0 ? void 0 : { content: next };
1515
+ if (next === void 0) return void 0;
1516
+ return direct && result && typeof result === "object" ? { result: {
1517
+ ...result,
1518
+ content: next
1519
+ } } : { notes: next.slice(content.length).flatMap((part) => "text" in part && typeof part.text === "string" ? [part.text] : []) };
1504
1520
  }
1505
1521
  },
1506
1522
  childHooks: { bind(scope) {
@@ -1702,6 +1718,7 @@ var EditTracker = class {
1702
1718
  * A candidate whose hash has not moved was touched, not edited.
1703
1719
  */
1704
1720
  contents = /* @__PURE__ */ new Map();
1721
+ onChanged;
1705
1722
  now;
1706
1723
  git;
1707
1724
  onScanFailed;
@@ -1715,6 +1732,7 @@ var EditTracker = class {
1715
1732
  this.timeline = timeline;
1716
1733
  this.snapshots = snapshots;
1717
1734
  this.manifests = manifests;
1735
+ this.onChanged = options.onChanged;
1718
1736
  this.now = options.now ?? Date.now;
1719
1737
  this.git = options.git;
1720
1738
  this.onScanFailed = options.onScanFailed;
@@ -1924,6 +1942,11 @@ var EditTracker = class {
1924
1942
  async append(event) {
1925
1943
  if (event.before !== void 0 && event.before === event.after) return;
1926
1944
  await this.timeline.append(event);
1945
+ if (event.after !== void 0 && (event.origin === "tool" || event.verified)) try {
1946
+ await this.onChanged?.([event.path]);
1947
+ } catch (error) {
1948
+ this.onScanFailed(error);
1949
+ }
1927
1950
  }
1928
1951
  /**
1929
1952
  * Folds a path the tool layer just handled into the tracked manifest, so the
@@ -2235,8 +2258,16 @@ function createFileEditSession({ agent, host }, fs, storage = createFileEditStor
2235
2258
  enableLogs: true,
2236
2259
  enableTraces: true
2237
2260
  });
2261
+ const changeListeners = /* @__PURE__ */ new Set();
2238
2262
  const editTracker = new EditTracker(timeline, snapshots, storage.manifests(), {
2239
2263
  git: storage.git(),
2264
+ onChanged: async (paths) => {
2265
+ for (const listener of changeListeners) try {
2266
+ await listener(paths);
2267
+ } catch (error) {
2268
+ await telemetry.recordWarning("po_file_edit.subscriber_failed", error);
2269
+ }
2270
+ },
2240
2271
  onScanFailed: (error) => void track(() => telemetry.recordWarning("po_file_edit.scan_failed", error))
2241
2272
  });
2242
2273
  if (host.context.directEvents === void 0) throw new Error("File-edit server plugin requires the session direct event bus.");
@@ -2303,107 +2334,121 @@ function createFileEditSession({ agent, host }, fs, storage = createFileEditStor
2303
2334
  ], "File-edit session cleanup failed.");
2304
2335
  return disposal;
2305
2336
  };
2306
- return {
2307
- api: [fileEditsApi],
2308
- activities: [{
2309
- name: "file-edits",
2310
- start: async (executionContext) => {
2311
- if (closed) throw new Error("File-edit session is closed.");
2312
- if (starting) return starting;
2313
- if (active) return stopActivity;
2314
- if (stopping) await stopping;
2315
- if (closed) throw new Error("File-edit session is closed.");
2316
- if (starting) return starting;
2317
- stopping = void 0;
2318
- starting = track(async () => {
2319
- const sessionKey = paths.sessionKey(executionContext.sessionId);
2320
- const timelinePath = await paths.timelinePath(executionContext.cwd, sessionKey);
2321
- const snapshotsPath = await paths.snapshotsPath(executionContext.cwd, sessionKey);
2322
- timeline.initialize(timelinePath);
2323
- snapshots.initialize(snapshotsPath);
2324
- const isIgnored = await readPoIgnore(executionContext.cwd, fs);
2325
- editTracker.reset({
2326
- exclude: [
2327
- timelinePath,
2328
- `${timelinePath}.lock`,
2329
- snapshotsPath
2330
- ],
2331
- ...isIgnored === void 0 ? {} : { isIgnored }
2332
- });
2333
- const runtime = {
2334
- context: executionContext,
2337
+ const activity = {
2338
+ name: "file-edits",
2339
+ start: async (executionContext) => {
2340
+ if (closed) throw new Error("File-edit session is closed.");
2341
+ if (starting) return starting;
2342
+ if (active) return stopActivity;
2343
+ if (stopping) await stopping;
2344
+ if (closed) throw new Error("File-edit session is closed.");
2345
+ if (starting) return starting;
2346
+ stopping = void 0;
2347
+ starting = track(async () => {
2348
+ const sessionKey = paths.sessionKey(executionContext.sessionId);
2349
+ const timelinePath = await paths.timelinePath(executionContext.cwd, sessionKey);
2350
+ const snapshotsPath = await paths.snapshotsPath(executionContext.cwd, sessionKey);
2351
+ timeline.initialize(timelinePath);
2352
+ snapshots.initialize(snapshotsPath);
2353
+ const isIgnored = await readPoIgnore(executionContext.cwd, fs);
2354
+ editTracker.reset({
2355
+ exclude: [
2335
2356
  timelinePath,
2357
+ `${timelinePath}.lock`,
2336
2358
  snapshotsPath
2337
- };
2338
- active = runtime;
2339
- lastRuntime = runtime;
2340
- await publish(runtime);
2341
- track(async () => {
2342
- try {
2343
- await storage.sweepAbandoned?.(paths.stateDirectory(), timelinePath);
2344
- } catch (error) {
2345
- await telemetry.recordWarning("po_file_edit.sweep_failed", error);
2346
- }
2347
- });
2348
- if (!initialized) {
2349
- initialized = true;
2350
- await track(() => telemetry.recordEvent("po_file_edit.timeline_started", { outcome: "initialized" }));
2359
+ ],
2360
+ ...isIgnored === void 0 ? {} : { isIgnored }
2361
+ });
2362
+ const runtime = {
2363
+ context: executionContext,
2364
+ timelinePath,
2365
+ snapshotsPath
2366
+ };
2367
+ active = runtime;
2368
+ lastRuntime = runtime;
2369
+ await publish(runtime);
2370
+ track(async () => {
2371
+ try {
2372
+ await storage.sweepAbandoned?.(paths.stateDirectory(), timelinePath);
2373
+ } catch (error) {
2374
+ await telemetry.recordWarning("po_file_edit.sweep_failed", error);
2351
2375
  }
2352
- return () => active === runtime ? stopActivity() : Promise.resolve();
2353
2376
  });
2354
- try {
2355
- return await starting;
2356
- } finally {
2357
- starting = void 0;
2377
+ if (!initialized) {
2378
+ initialized = true;
2379
+ await track(() => telemetry.recordEvent("po_file_edit.timeline_started", { outcome: "initialized" }));
2358
2380
  }
2381
+ return () => active === runtime ? stopActivity() : Promise.resolve();
2382
+ });
2383
+ try {
2384
+ return await starting;
2385
+ } finally {
2386
+ starting = void 0;
2359
2387
  }
2360
- }],
2361
- hooks: [{
2362
- event: "tool_execution_start",
2363
- handle: (event, executionContext) => {
2364
- if (closed || stopping) return;
2365
- const runtime = active;
2366
- const toolCallId = stringValue(event, "toolCallId");
2367
- const toolName = stringValue(event, "toolName");
2368
- if (runtime === void 0 || runtime.context.sessionId !== executionContext.sessionId || !toolCallId || !toolName) return;
2369
- return track(async () => {
2370
- const hookStartedAt = Date.now();
2371
- if (!await editTracker.start(toolCallId, toolName, event.args, executionContext.cwd) || active !== runtime) return;
2372
- toolStartedAt.set(toolCallId, hookStartedAt);
2373
- await telemetry.recordEvent("po_file_edit.edit_started", {
2374
- "tool.name": toolName,
2375
- hook_ms: Date.now() - hookStartedAt
2376
- });
2388
+ }
2389
+ };
2390
+ const startHook = {
2391
+ event: "tool_execution_start",
2392
+ handle: (event, executionContext) => {
2393
+ if (closed || stopping) return;
2394
+ const runtime = active;
2395
+ const toolCallId = stringValue(event, "toolCallId");
2396
+ const toolName = stringValue(event, "toolName");
2397
+ if (runtime === void 0 || runtime.context.sessionId !== executionContext.sessionId || !toolCallId || !toolName) return;
2398
+ return track(async () => {
2399
+ const hookStartedAt = Date.now();
2400
+ if (!await editTracker.start(toolCallId, toolName, event.args, executionContext.cwd) || active !== runtime) return;
2401
+ toolStartedAt.set(toolCallId, hookStartedAt);
2402
+ await telemetry.recordEvent("po_file_edit.edit_started", {
2403
+ "tool.name": toolName,
2404
+ hook_ms: Date.now() - hookStartedAt
2377
2405
  });
2378
- }
2379
- }, {
2380
- event: "tool_execution_end",
2381
- handle: (event, executionContext) => {
2382
- if (closed || stopping) return;
2383
- const runtime = active;
2384
- const toolCallId = stringValue(event, "toolCallId");
2385
- const toolName = stringValue(event, "toolName");
2386
- if (runtime === void 0 || runtime.context.sessionId !== executionContext.sessionId || !toolCallId || !toolName) return;
2387
- return track(async () => {
2388
- const startedAt = toolStartedAt.get(toolCallId);
2389
- if (startedAt === void 0) return;
2390
- toolStartedAt.delete(toolCallId);
2391
- const hookStartedAt = Date.now();
2392
- const isError = event.isError === true;
2393
- await editTracker.end(toolCallId, isError, executionContext.cwd);
2394
- if (active !== runtime) return;
2395
- await publish(runtime);
2396
- await telemetry.recordEvent("po_file_edit.edit_finished", {
2397
- "tool.name": toolName,
2398
- "tool.result.error": isError,
2399
- ...failureAttributes(event.result),
2400
- ...startedAt === void 0 ? {} : { duration_ms: Date.now() - startedAt },
2401
- hook_ms: Date.now() - hookStartedAt,
2402
- outcome: isError ? "failed" : "completed"
2403
- });
2406
+ });
2407
+ }
2408
+ };
2409
+ const endHook = {
2410
+ event: "tool_execution_end",
2411
+ handle: (event, executionContext) => {
2412
+ if (closed || stopping) return;
2413
+ const runtime = active;
2414
+ const toolCallId = stringValue(event, "toolCallId");
2415
+ const toolName = stringValue(event, "toolName");
2416
+ if (runtime === void 0 || runtime.context.sessionId !== executionContext.sessionId || !toolCallId || !toolName) return;
2417
+ return track(async () => {
2418
+ const startedAt = toolStartedAt.get(toolCallId);
2419
+ if (startedAt === void 0) return;
2420
+ toolStartedAt.delete(toolCallId);
2421
+ const hookStartedAt = Date.now();
2422
+ const isError = event.isError === true;
2423
+ await editTracker.end(toolCallId, isError, executionContext.cwd);
2424
+ if (active !== runtime) return;
2425
+ await publish(runtime);
2426
+ await telemetry.recordEvent("po_file_edit.edit_finished", {
2427
+ "tool.name": toolName,
2428
+ "tool.result.error": isError,
2429
+ ...failureAttributes(event.result),
2430
+ ...startedAt === void 0 ? {} : { duration_ms: Date.now() - startedAt },
2431
+ hook_ms: Date.now() - hookStartedAt,
2432
+ outcome: isError ? "failed" : "completed"
2404
2433
  });
2405
- }
2406
- }],
2434
+ });
2435
+ }
2436
+ };
2437
+ const changes = {
2438
+ sessionId: agent.context.sessionId,
2439
+ subscribe(listener) {
2440
+ changeListeners.add(listener);
2441
+ return () => {
2442
+ changeListeners.delete(listener);
2443
+ };
2444
+ },
2445
+ flush: () => editTracker.flush()
2446
+ };
2447
+ return {
2448
+ services: [(scope) => scope.provide(require_package.PO_FILE_EDIT_CHANGES_SERVICE, changes)],
2449
+ api: [fileEditsApi],
2450
+ activities: [activity],
2451
+ hooks: [startHook, endHook],
2407
2452
  onDispose: dispose
2408
2453
  };
2409
2454
  }
@@ -2438,7 +2483,7 @@ const createFileEditServerRoot = (context, fs) => {
2438
2483
  }
2439
2484
  },
2440
2485
  activities: runtime.activities,
2441
- services: [(cordis) => {
2486
+ services: [...runtime.services ?? [], (cordis) => {
2442
2487
  cordis.inject([_agimon_ai_po_core_packageApi.PO_EDIT_PRESENCE_SERVICE], (presenceContext) => {
2443
2488
  if (closed) return;
2444
2489
  const bound = createPresence(context, presenceContext.get(_agimon_ai_po_core_packageApi.PO_EDIT_PRESENCE_SERVICE), storage.presenceFiles());
@@ -2488,18 +2533,18 @@ var fileEdits_server_default = (0, _agimon_ai_po_core_extensionFile.defineChanne
2488
2533
  */
2489
2534
  var route_server_default = (0, _agimon_ai_po_core_extensionFile.defineRoute)((context) => createFileEditsPoApi(createFileEditStorage((0, _agimon_ai_po_core_platform.readPoFs)(context.context))));
2490
2535
  //#endregion
2536
+ //#region src/extensions/workspaces/sessions/(backend)/hook/operation-after.server.ts
2537
+ var operation_after_server_default = (0, _agimon_ai_po_core_extensionFile.defineHook)(({ root }) => ({
2538
+ event: "operation_after",
2539
+ handle: async (event, execution) => root.presence?.operationAfter.handle(event, execution)
2540
+ }));
2541
+ //#endregion
2491
2542
  //#region src/extensions/workspaces/sessions/(backend)/hook/tool-execution-end.server.ts
2492
2543
  var tool_execution_end_server_default = (0, _agimon_ai_po_core_extensionFile.defineHook)((context) => context.root.hooks[1]);
2493
2544
  //#endregion
2494
2545
  //#region src/extensions/workspaces/sessions/(backend)/hook/tool-execution-start.server.ts
2495
2546
  var tool_execution_start_server_default = (0, _agimon_ai_po_core_extensionFile.defineHook)((context) => context.root.hooks[0]);
2496
2547
  //#endregion
2497
- //#region src/extensions/workspaces/sessions/(backend)/hook/tool-result.server.ts
2498
- var tool_result_server_default = (0, _agimon_ai_po_core_extensionFile.defineHook)(({ root }) => ({
2499
- event: "tool_result",
2500
- handle: async (event, execution) => root.presence?.toolResult.handle(event, execution)
2501
- }));
2502
- //#endregion
2503
2548
  //#region generated/server.ts
2504
2549
  const at = (value, context) => typeof value === "function" ? value(context) : value;
2505
2550
  /* v8 ignore next -- generated normalization branch */
@@ -2534,9 +2579,9 @@ const serverPlugin = (0, _agimon_ai_po_core_serverPlugin.defineServerPlugin)({
2534
2579
  },
2535
2580
  get hooks() {
2536
2581
  return [
2582
+ via({ event: "operation_after" }, at(operation_after_server_default, contextSession)),
2537
2583
  via({ event: "tool_execution_end" }, at(tool_execution_end_server_default, contextSession)),
2538
- via({ event: "tool_execution_start" }, at(tool_execution_start_server_default, contextSession)),
2539
- via({ event: "tool_result" }, at(tool_result_server_default, contextSession))
2584
+ via({ event: "tool_execution_start" }, at(tool_execution_start_server_default, contextSession))
2540
2585
  ];
2541
2586
  },
2542
2587
  services: [...scopeSession.services ?? []],