agent-relay 9.2.1 → 9.2.2

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 (45) hide show
  1. package/README.md +26 -15
  2. package/dist/cli/bootstrap.d.ts.map +1 -1
  3. package/dist/cli/bootstrap.js +16 -2
  4. package/dist/cli/bootstrap.js.map +1 -1
  5. package/dist/cli/commands/cloud.d.ts +9 -0
  6. package/dist/cli/commands/cloud.d.ts.map +1 -1
  7. package/dist/cli/commands/cloud.js +71 -1
  8. package/dist/cli/commands/cloud.js.map +1 -1
  9. package/dist/cli/commands/core.d.ts +18 -1
  10. package/dist/cli/commands/core.d.ts.map +1 -1
  11. package/dist/cli/commands/core.js +15 -9
  12. package/dist/cli/commands/core.js.map +1 -1
  13. package/dist/cli/commands/fleet.d.ts +0 -12
  14. package/dist/cli/commands/fleet.d.ts.map +1 -1
  15. package/dist/cli/commands/fleet.js +17 -261
  16. package/dist/cli/commands/fleet.js.map +1 -1
  17. package/dist/cli/commands/local-workflow.d.ts.map +1 -1
  18. package/dist/cli/commands/local-workflow.js +15 -2
  19. package/dist/cli/commands/local-workflow.js.map +1 -1
  20. package/dist/cli/commands/node.d.ts +19 -0
  21. package/dist/cli/commands/node.d.ts.map +1 -0
  22. package/dist/cli/commands/node.js +86 -0
  23. package/dist/cli/commands/node.js.map +1 -0
  24. package/dist/cli/commands/reflex.d.ts +0 -2
  25. package/dist/cli/commands/reflex.d.ts.map +1 -1
  26. package/dist/cli/commands/reflex.js +25 -36
  27. package/dist/cli/commands/reflex.js.map +1 -1
  28. package/dist/cli/lib/broker-lifecycle.d.ts +10 -0
  29. package/dist/cli/lib/broker-lifecycle.d.ts.map +1 -1
  30. package/dist/cli/lib/broker-lifecycle.js +65 -10
  31. package/dist/cli/lib/broker-lifecycle.js.map +1 -1
  32. package/dist/cli/lib/fleet-sidecar.d.ts +16 -42
  33. package/dist/cli/lib/fleet-sidecar.d.ts.map +1 -1
  34. package/dist/cli/lib/fleet-sidecar.js +18 -380
  35. package/dist/cli/lib/fleet-sidecar.js.map +1 -1
  36. package/dist/cli/lib/node-definition-loader.d.ts +17 -0
  37. package/dist/cli/lib/node-definition-loader.d.ts.map +1 -0
  38. package/dist/cli/lib/node-definition-loader.js +124 -0
  39. package/dist/cli/lib/node-definition-loader.js.map +1 -0
  40. package/dist/cli/lib/reflex-capture.d.ts +41 -0
  41. package/dist/cli/lib/reflex-capture.d.ts.map +1 -0
  42. package/dist/cli/lib/reflex-capture.js +128 -0
  43. package/dist/cli/lib/reflex-capture.js.map +1 -0
  44. package/dist/index.cjs +73 -267
  45. package/package.json +10 -7
@@ -0,0 +1,128 @@
1
+ /**
2
+ * Reflex in-process cloud capture.
3
+ *
4
+ * When Reflex is enabled (`agent-relay reflex on`), the long-running
5
+ * `agent-relay up` host periodically syncs local agent history into the ai-hist
6
+ * DB and pushes new records to relayhistory-cloud — no launchd/cron, no CLI the
7
+ * user runs by hand, and **no subprocess**. Mirrors the telemetry client: an
8
+ * unref'd timer that never blocks the event loop, plus a best-effort final
9
+ * flush on shutdown.
10
+ *
11
+ * The work runs in-process through the `ai-hist-native` napi addon
12
+ * (`syncAndPush()`), which ships as a per-platform optional-dependency package
13
+ * so a plain `agent-relay` install works with no extra setup. Everything is a
14
+ * silent no-op when the addon is unavailable or the user isn't authenticated.
15
+ */
16
+ import { isReflexEnabled } from '@agent-relay/config';
17
+ const DEFAULT_INTERVAL_MS = 5 * 60_000;
18
+ const DEFAULT_INITIAL_DELAY_MS = 30_000;
19
+ /** Lazily load the `ai-hist-native` napi addon; null when it isn't installed. */
20
+ async function loadNative() {
21
+ // Non-literal spec keeps the native addon out of the esbuild bundle; it
22
+ // resolves from node_modules (the per-platform optional dep) at runtime.
23
+ const spec = 'ai-hist-native';
24
+ let mod;
25
+ try {
26
+ mod = (await import(spec));
27
+ }
28
+ catch (err) {
29
+ // Not installed for this platform → a clean no-op. Anything else (ABI
30
+ // mismatch, missing system lib, addon init failure) is a real problem —
31
+ // rethrow so the caller logs it instead of silently doing nothing.
32
+ const code = err?.code;
33
+ if (code === 'ERR_MODULE_NOT_FOUND' || code === 'MODULE_NOT_FOUND') {
34
+ return null;
35
+ }
36
+ throw err;
37
+ }
38
+ const fn = mod.syncAndPush ?? mod.default?.syncAndPush;
39
+ return typeof fn === 'function' ? { syncAndPush: fn } : null;
40
+ }
41
+ /**
42
+ * Sync local agent history into the ai-hist DB and push new records to
43
+ * relayhistory-cloud, **in-process** via the native addon — no subprocess. This
44
+ * is what makes `reflex on` "just work": no separate ai-hist install, no CLI.
45
+ * Resolves null when the addon is unavailable or the user isn't authenticated.
46
+ */
47
+ export async function reflexSyncAndPush(opts = {}) {
48
+ const native = opts.native !== undefined ? opts.native : await loadNative();
49
+ if (!native)
50
+ return null;
51
+ const result = await native.syncAndPush();
52
+ if (!result.authenticated)
53
+ return null; // not logged in yet
54
+ return { sent: result.sent, accepted: result.accepted };
55
+ }
56
+ function withDefaults(overrides) {
57
+ return {
58
+ isEnabled: isReflexEnabled,
59
+ push: () => reflexSyncAndPush(),
60
+ log: (message) => console.error(message),
61
+ intervalMs: DEFAULT_INTERVAL_MS,
62
+ initialDelayMs: DEFAULT_INITIAL_DELAY_MS,
63
+ ...overrides,
64
+ };
65
+ }
66
+ export function startReflexCapture(overrides = {}) {
67
+ const deps = withDefaults(overrides);
68
+ let stopped = false;
69
+ // Dedup concurrent ticks: a slow push must not overlap the next interval.
70
+ let inFlight = null;
71
+ // The recurring interval starts only after the first (delayed) push.
72
+ let timer = null;
73
+ const tick = () => {
74
+ if (inFlight)
75
+ return inFlight;
76
+ // Re-check enablement every tick so `agent-relay reflex off` (or `on`)
77
+ // takes effect immediately in an already-running `agent-relay up`, without
78
+ // restarting the host.
79
+ if (!deps.isEnabled())
80
+ return Promise.resolve();
81
+ inFlight = (async () => {
82
+ try {
83
+ const result = await deps.push();
84
+ if (result && result.sent > 0) {
85
+ deps.log(`[reflex] synced ${result.sent} record(s) to relayhistory-cloud`);
86
+ }
87
+ }
88
+ catch (err) {
89
+ deps.log(`[reflex] cloud sync failed: ${err instanceof Error ? err.message : String(err)}`);
90
+ }
91
+ finally {
92
+ inFlight = null;
93
+ }
94
+ })();
95
+ return inFlight;
96
+ };
97
+ const kickoff = setTimeout(() => {
98
+ if (stopped)
99
+ return;
100
+ void tick();
101
+ // Start the interval only now, so the first push can never fire before
102
+ // initialDelayMs regardless of how small intervalMs is.
103
+ timer = setInterval(() => {
104
+ if (!stopped)
105
+ void tick();
106
+ }, deps.intervalMs);
107
+ // Don't keep the process alive just for the capture timer.
108
+ timer.unref?.();
109
+ }, deps.initialDelayMs);
110
+ kickoff.unref?.();
111
+ return {
112
+ stop: async () => {
113
+ stopped = true;
114
+ clearTimeout(kickoff);
115
+ if (timer)
116
+ clearInterval(timer);
117
+ // Let an in-flight push finish, then flush one final batch (a no-op if
118
+ // Reflex was disabled in the meantime — tick() re-checks).
119
+ if (inFlight) {
120
+ await inFlight;
121
+ }
122
+ else {
123
+ await tick();
124
+ }
125
+ },
126
+ };
127
+ }
128
+ //# sourceMappingURL=reflex-capture.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"reflex-capture.js","sourceRoot":"","sources":["../../../src/cli/lib/reflex-capture.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;;;GAcG;AACH,OAAO,EAAE,eAAe,EAAE,MAAM,qBAAqB,CAAC;AAyBtD,MAAM,mBAAmB,GAAG,CAAC,GAAG,MAAM,CAAC;AACvC,MAAM,wBAAwB,GAAG,MAAM,CAAC;AAOxC,iFAAiF;AACjF,KAAK,UAAU,UAAU;IACvB,wEAAwE;IACxE,yEAAyE;IACzE,MAAM,IAAI,GAAG,gBAAgB,CAAC;IAC9B,IAAI,GAAgE,CAAC;IACrE,IAAI,CAAC;QACH,GAAG,GAAG,CAAC,MAAM,MAAM,CAAC,IAAI,CAAC,CAAe,CAAC;IAC3C,CAAC;IAAC,OAAO,GAAG,EAAE,CAAC;QACb,sEAAsE;QACtE,wEA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