@agent-idle/cli 0.1.0 → 0.1.1

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 (2) hide show
  1. package/index.js +54 -7
  2. package/package.json +1 -1
package/index.js CHANGED
@@ -412,6 +412,32 @@ function topicWord(prompt) {
412
412
  return pool.reduce((best, w) => w.length > best.length ? w : best, "");
413
413
  }
414
414
  var ingestEvent = makeFunctionReference("events:ingestEvent");
415
+ var ingestEvents = makeFunctionReference("events:ingestEvents");
416
+ var FLUSH_CHUNK = 100;
417
+ function isPureRenewal(item) {
418
+ if (item.type !== "activity") return false;
419
+ const p = item.payload;
420
+ if (!p || typeof p !== "object") return false;
421
+ const keys = Object.keys(p);
422
+ return p.working === true && keys.every((k) => k === "working" || k === "action");
423
+ }
424
+ function coalesceRenewals(items) {
425
+ const keep = new Array(items.length).fill(true);
426
+ const lastRenewal = /* @__PURE__ */ new Map();
427
+ items.forEach((item, i) => {
428
+ if (isPureRenewal(item)) {
429
+ const prev = lastRenewal.get(item.sessionId);
430
+ const prevItem = prev === void 0 ? void 0 : items[prev];
431
+ if (prevItem && prevItem.payload?.action === item.payload?.action) {
432
+ keep[prev] = false;
433
+ }
434
+ lastRenewal.set(item.sessionId, i);
435
+ } else {
436
+ lastRenewal.delete(item.sessionId);
437
+ }
438
+ });
439
+ return items.filter((_, i) => keep[i]);
440
+ }
415
441
  function classifyToolAction(toolName) {
416
442
  switch (toolName) {
417
443
  case "Bash":
@@ -794,13 +820,34 @@ function startDaemon() {
794
820
  const client = new ConvexHttpClient(convexUrl);
795
821
  client.setAuth(token);
796
822
  const remaining = [];
797
- for (const item of items) {
798
- try {
799
- const res = await client.mutation(ingestEvent, item);
800
- debug(` ${item.type} session=${tag(item.sessionId)} \u2192`, res);
801
- } catch (err) {
802
- remaining.push(item);
803
- debug(` ${item.type} session=${tag(item.sessionId)} FAILED:`, err?.message ?? err);
823
+ const queue = coalesceRenewals(items);
824
+ let batchSupported = true;
825
+ for (let i = 0; i < queue.length; i += FLUSH_CHUNK) {
826
+ const chunk = queue.slice(i, i + FLUSH_CHUNK);
827
+ if (batchSupported) {
828
+ try {
829
+ const res = await client.mutation(ingestEvents, { events: chunk });
830
+ debug(` batch of ${chunk.length} \u2192`, res);
831
+ continue;
832
+ } catch (err) {
833
+ const msg = err?.message ?? String(err);
834
+ if (!/Could not find public function/i.test(msg)) {
835
+ remaining.push(...chunk);
836
+ debug(` batch of ${chunk.length} FAILED:`, msg);
837
+ continue;
838
+ }
839
+ batchSupported = false;
840
+ debug(" batch unsupported by backend \u2014 falling back to per-event");
841
+ }
842
+ }
843
+ for (const item of chunk) {
844
+ try {
845
+ const res = await client.mutation(ingestEvent, item);
846
+ debug(` ${item.type} session=${tag(item.sessionId)} \u2192`, res);
847
+ } catch (err) {
848
+ remaining.push(item);
849
+ debug(` ${item.type} session=${tag(item.sessionId)} FAILED:`, err?.message ?? err);
850
+ }
804
851
  }
805
852
  }
806
853
  writeOutbox(remaining);
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@agent-idle/cli",
3
- "version": "0.1.0",
3
+ "version": "0.1.1",
4
4
  "description": "agent-idle CLI: setup, ui, and the headless sensor daemon.",
5
5
  "license": "AGPL-3.0-only",
6
6
  "type": "module",