@xmoxmo/bncr 0.2.6 → 0.2.8

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 +7 -1
  2. package/index.ts +30 -15
  3. package/package.json +4 -3
  4. package/scripts/check-pack.mjs +77 -0
  5. package/scripts/selfcheck.mjs +10 -0
  6. package/src/channel.ts +398 -642
  7. package/src/core/extended-diagnostics.ts +10 -0
  8. package/src/core/file-ack.ts +9 -0
  9. package/src/core/file-transfer-payloads.ts +72 -0
  10. package/src/core/register-trace.ts +79 -0
  11. package/src/core/targets.ts +10 -1
  12. package/src/messaging/inbound/commands.ts +20 -10
  13. package/src/messaging/inbound/context-facts.ts +200 -0
  14. package/src/messaging/inbound/dispatch.ts +66 -14
  15. package/src/messaging/inbound/gate.ts +66 -26
  16. package/src/messaging/inbound/runtime-compat.ts +41 -0
  17. package/src/messaging/inbound/session-label.ts +7 -7
  18. package/src/messaging/outbound/durable-message-adapter.ts +107 -0
  19. package/src/messaging/outbound/durable-queue-adapter.ts +157 -0
  20. package/src/messaging/outbound/session-route.ts +2 -2
  21. package/src/openclaw/config-runtime.ts +52 -0
  22. package/src/openclaw/inbound-session-runtime.ts +94 -0
  23. package/src/openclaw/ingress-runtime.ts +35 -0
  24. package/src/openclaw/media-runtime.ts +73 -0
  25. package/src/openclaw/reply-runtime.ts +104 -0
  26. package/src/openclaw/routing-runtime.ts +48 -0
  27. package/src/openclaw/sdk-helpers.ts +20 -0
  28. package/src/openclaw/session-route-runtime.ts +15 -0
  29. package/src/plugin/capabilities.ts +8 -0
  30. package/src/plugin/config.ts +35 -0
  31. package/src/plugin/gateway-methods.ts +12 -0
  32. package/src/plugin/gateway-runtime.ts +11 -0
  33. package/src/plugin/message-policy.ts +4 -0
  34. package/src/plugin/message-send.ts +13 -0
  35. package/src/plugin/messaging.ts +142 -0
  36. package/src/plugin/meta.ts +10 -0
  37. package/src/plugin/outbound.ts +51 -0
  38. package/src/plugin/setup.ts +24 -0
  39. package/src/plugin/status.ts +38 -0
  40. package/src/runtime/log-dedupe.ts +56 -0
  41. package/src/runtime/outbound-ack-timeout.ts +96 -0
  42. package/src/runtime/outbound-flags.ts +81 -0
  43. package/src/runtime/outbox-transitions.ts +119 -0
  44. package/src/runtime/status-snapshots.ts +108 -0
  45. package/src/runtime/status-worker.ts +172 -0
@@ -0,0 +1,172 @@
1
+ import { normalizeAccountId } from '../core/accounts.ts';
2
+
3
+ type StatusWorkerContext = {
4
+ accountId: string;
5
+ getStatus?: () => Record<string, any>;
6
+ setStatus?: (status: Record<string, any>) => void;
7
+ abortSignal?: {
8
+ aborted?: boolean;
9
+ addEventListener?: (event: 'abort', listener: () => void, options?: { once?: boolean }) => void;
10
+ removeEventListener?: (event: 'abort', listener: () => void) => void;
11
+ };
12
+ };
13
+
14
+ export type ChannelAccountWorkerHandle = {
15
+ timer: NodeJS.Timeout;
16
+ finish: (reason: string) => void;
17
+ cleanupAbortListener?: () => void;
18
+ };
19
+
20
+ type StatusWorkerHooks = {
21
+ isOnline: (accountId: string) => boolean;
22
+ hasRecentInboundReachability: (accountId: string) => boolean;
23
+ getLastActivityAt: (accountId: string, previous: Record<string, any>) => number | null;
24
+ getActiveConnectionKey: (accountId: string) => string | null;
25
+ getActiveConnections: (accountId: string) => Array<Record<string, unknown>>;
26
+ buildStatusMeta: (accountId: string) => Record<string, any>;
27
+ logInfo: (scope: string | undefined, message: string, options?: { debugOnly?: boolean }) => void;
28
+ logInfoDedup: (
29
+ scope: string | undefined,
30
+ message: string,
31
+ options: { key: string; sig: string; debugOnly?: boolean; windowMs?: number },
32
+ ) => void;
33
+ };
34
+
35
+ type StatusWorkerRuntime = {
36
+ workers: Map<string, ChannelAccountWorkerHandle>;
37
+ bridgeId: string;
38
+ hooks: StatusWorkerHooks;
39
+ };
40
+
41
+ export function clearBncrStatusWorker(
42
+ runtime: StatusWorkerRuntime,
43
+ accountId: string,
44
+ reason: string,
45
+ ) {
46
+ const worker = runtime.workers.get(accountId);
47
+ if (!worker) return false;
48
+ worker.finish(reason);
49
+ runtime.hooks.logInfo(
50
+ 'health',
51
+ `status-worker cleared ${JSON.stringify({ bridge: runtime.bridgeId, accountId, reason })}`,
52
+ { debugOnly: true },
53
+ );
54
+ return true;
55
+ }
56
+
57
+ export function clearAllBncrStatusWorkers(runtime: StatusWorkerRuntime, reason: string) {
58
+ for (const accountId of Array.from(runtime.workers.keys())) {
59
+ clearBncrStatusWorker(runtime, accountId, reason);
60
+ }
61
+ }
62
+
63
+ export async function startBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: StatusWorkerContext) {
64
+ const accountId = normalizeAccountId(ctx.accountId);
65
+ clearBncrStatusWorker(runtime, accountId, 'start-replace');
66
+
67
+ const tick = () => {
68
+ const previous = ctx.getStatus?.() || {};
69
+ const onlineByConn = runtime.hooks.isOnline(accountId);
70
+ const recentInboundReachable = runtime.hooks.hasRecentInboundReachability(accountId);
71
+ const connected = onlineByConn || recentInboundReachable;
72
+ const lastActAt = runtime.hooks.getLastActivityAt(accountId, previous);
73
+ const activeConnections = runtime.hooks.getActiveConnections(accountId);
74
+ const healthSig = JSON.stringify({
75
+ bridge: runtime.bridgeId,
76
+ accountId,
77
+ connected,
78
+ onlineByConn,
79
+ recentInboundReachable,
80
+ activeConnectionKey: runtime.hooks.getActiveConnectionKey(accountId),
81
+ activeConnections,
82
+ });
83
+ const conns = activeConnections.length;
84
+ runtime.hooks.logInfoDedup(
85
+ 'health',
86
+ `status-tick ${accountId}|changed|${connected ? 'linked' : 'configured'}|onlineByConn=${onlineByConn}|recentInboundReachable=${recentInboundReachable}|conns=${conns}`,
87
+ {
88
+ key: `health-status-tick:${accountId}`,
89
+ sig: healthSig,
90
+ },
91
+ );
92
+ runtime.hooks.logInfoDedup('health', `status-tick ${healthSig}`, {
93
+ key: `health-status-tick-debug:${accountId}`,
94
+ sig: healthSig,
95
+ debugOnly: true,
96
+ });
97
+
98
+ ctx.setStatus?.({
99
+ ...previous,
100
+ accountId,
101
+ running: true,
102
+ connected,
103
+ lastEventAt: lastActAt,
104
+ // 状态映射:在线=linked,离线=configured
105
+ mode: connected ? 'linked' : 'configured',
106
+ lastError: previous?.lastError ?? null,
107
+ meta: runtime.hooks.buildStatusMeta(accountId),
108
+ });
109
+ };
110
+
111
+ tick();
112
+ const timer = setInterval(tick, 5_000);
113
+ let worker!: ChannelAccountWorkerHandle;
114
+ const done = new Promise<void>((resolve) => {
115
+ let settled = false;
116
+ const finish = (reason: string) => {
117
+ if (settled) return;
118
+ settled = true;
119
+ const activeWorker = runtime.workers.get(accountId);
120
+ if (activeWorker === worker) {
121
+ runtime.workers.delete(accountId);
122
+ }
123
+ clearInterval(timer);
124
+ worker.cleanupAbortListener?.();
125
+ worker.cleanupAbortListener = undefined;
126
+ runtime.hooks.logInfo(
127
+ 'health',
128
+ `status-worker finished ${JSON.stringify({ bridge: runtime.bridgeId, accountId, reason })}`,
129
+ { debugOnly: true },
130
+ );
131
+ runtime.hooks.logInfo('health', `status-worker finished ${accountId}|${reason}`);
132
+ resolve();
133
+ };
134
+
135
+ worker = { timer, finish };
136
+ runtime.workers.set(accountId, worker);
137
+
138
+ const onAbort = () => finish('abort');
139
+ const abortSignal = ctx.abortSignal;
140
+
141
+ if (abortSignal?.aborted) {
142
+ onAbort();
143
+ return;
144
+ }
145
+
146
+ abortSignal?.addEventListener?.('abort', onAbort, { once: true });
147
+ if (abortSignal?.removeEventListener) {
148
+ worker.cleanupAbortListener = () => abortSignal.removeEventListener?.('abort', onAbort);
149
+ }
150
+ });
151
+ await done;
152
+ }
153
+
154
+ export async function stopBncrStatusWorker(runtime: StatusWorkerRuntime, ctx: Partial<StatusWorkerContext>) {
155
+ const accountId = normalizeAccountId(ctx?.accountId);
156
+ const cleared = clearBncrStatusWorker(runtime, accountId, 'explicit-stop');
157
+ const previous = ctx?.getStatus?.() || {};
158
+ ctx?.setStatus?.({
159
+ ...previous,
160
+ accountId,
161
+ running: false,
162
+ restartPending: false,
163
+ lastStopAt: Date.now(),
164
+ meta: runtime.hooks.buildStatusMeta(accountId),
165
+ });
166
+ runtime.hooks.logInfo(
167
+ 'health',
168
+ `status-stop ${JSON.stringify({ bridge: runtime.bridgeId, accountId, cleared })}`,
169
+ { debugOnly: true },
170
+ );
171
+ runtime.hooks.logInfo('health', `status-stop ${accountId}|cleared=${cleared}`);
172
+ }