@xmoxmo/bncr 0.3.5 → 0.3.7

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 (164) hide show
  1. package/README.md +5 -0
  2. package/dist/index.js +28 -5
  3. package/index.ts +55 -721
  4. package/package.json +8 -4
  5. package/scripts/check-pack.mjs +93 -18
  6. package/scripts/check-register-drift.mjs +35 -13
  7. package/scripts/selfcheck.mjs +80 -11
  8. package/src/bootstrap/channel-plugin-runtime.ts +81 -0
  9. package/src/bootstrap/cli.ts +97 -0
  10. package/src/bootstrap/register-runtime-gateway.ts +129 -0
  11. package/src/bootstrap/register-runtime-helpers.ts +140 -0
  12. package/src/bootstrap/register-runtime-singleton.ts +137 -0
  13. package/src/bootstrap/register-runtime.ts +201 -0
  14. package/src/bootstrap/runtime-discovery.ts +187 -0
  15. package/src/bootstrap/runtime-loader.ts +54 -0
  16. package/src/channel.ts +1590 -4967
  17. package/src/core/accounts.ts +23 -4
  18. package/src/core/dead-letter-diagnostics.ts +37 -5
  19. package/src/core/diagnostics.ts +31 -15
  20. package/src/core/downlink-health.ts +3 -11
  21. package/src/core/extended-diagnostics.ts +78 -36
  22. package/src/core/file-transfer-payloads.ts +1 -1
  23. package/src/core/logging.ts +1 -0
  24. package/src/core/outbox-enqueue.ts +13 -2
  25. package/src/core/outbox-entry-builders.ts +2 -0
  26. package/src/core/outbox-summary.ts +75 -3
  27. package/src/core/permissions.ts +15 -2
  28. package/src/core/persisted-outbox-entry.ts +21 -6
  29. package/src/core/policy.ts +45 -4
  30. package/src/core/probe.ts +3 -15
  31. package/src/core/register-trace.ts +3 -3
  32. package/src/core/status.ts +43 -4
  33. package/src/core/targets.ts +216 -205
  34. package/src/core/types.ts +221 -0
  35. package/src/core/value-sanitize.ts +29 -0
  36. package/src/messaging/inbound/commands.ts +147 -172
  37. package/src/messaging/inbound/context-facts.ts +4 -2
  38. package/src/messaging/inbound/contracts.ts +70 -0
  39. package/src/messaging/inbound/dispatch-prep.ts +303 -0
  40. package/src/messaging/inbound/dispatch.ts +49 -462
  41. package/src/messaging/inbound/gate.ts +18 -5
  42. package/src/messaging/inbound/last-route.ts +10 -4
  43. package/src/messaging/inbound/media-url-download.ts +109 -0
  44. package/src/messaging/inbound/native-command-runtime.ts +225 -0
  45. package/src/messaging/inbound/parse.ts +2 -1
  46. package/src/messaging/inbound/remote-media.ts +49 -0
  47. package/src/messaging/inbound/reply-config.ts +16 -4
  48. package/src/messaging/inbound/reply-dispatch.ts +162 -0
  49. package/src/messaging/inbound/runtime-compat.ts +31 -10
  50. package/src/messaging/inbound/session-label.ts +15 -7
  51. package/src/messaging/inbound/turn-context.ts +131 -0
  52. package/src/messaging/outbound/actions.ts +24 -10
  53. package/src/messaging/outbound/diagnostics-debug-builders.ts +365 -0
  54. package/src/messaging/outbound/diagnostics.ts +31 -355
  55. package/src/messaging/outbound/durable-message-adapter.ts +20 -16
  56. package/src/messaging/outbound/durable-queue-adapter.ts +20 -7
  57. package/src/messaging/outbound/media.ts +24 -13
  58. package/src/messaging/outbound/reply-enqueue-media.ts +181 -0
  59. package/src/messaging/outbound/reply-enqueue.ts +57 -134
  60. package/src/messaging/outbound/send-params.ts +3 -0
  61. package/src/messaging/outbound/send.ts +19 -10
  62. package/src/messaging/outbound/session-route.ts +18 -3
  63. package/src/openclaw/channel-runtime-contracts.ts +76 -0
  64. package/src/openclaw/config-runtime.ts +13 -7
  65. package/src/openclaw/inbound-session-runtime.ts +7 -3
  66. package/src/openclaw/ingress-runtime.ts +17 -27
  67. package/src/openclaw/reply-runtime.ts +54 -59
  68. package/src/openclaw/routing-runtime.ts +35 -18
  69. package/src/openclaw/runtime-surface.ts +156 -12
  70. package/src/openclaw/sdk-helpers.ts +8 -1
  71. package/src/openclaw/session-route-runtime.ts +12 -12
  72. package/src/plugin/ack-outbox-runtime-group.ts +264 -0
  73. package/src/plugin/bridge-ack-facade.ts +137 -0
  74. package/src/plugin/bridge-connection-facade.ts +111 -0
  75. package/src/plugin/bridge-diagnostics-facade.ts +23 -0
  76. package/src/plugin/bridge-drain-facade.ts +98 -0
  77. package/src/plugin/bridge-extended-diagnostics-facade.ts +149 -0
  78. package/src/plugin/bridge-file-transfer-push-facade.ts +140 -0
  79. package/src/plugin/bridge-lifecycle.ts +156 -0
  80. package/src/plugin/bridge-media-facade.ts +241 -0
  81. package/src/plugin/bridge-outbox-facade.ts +182 -0
  82. package/src/plugin/bridge-runtime-helpers.ts +266 -0
  83. package/src/plugin/bridge-runtime-snapshots.ts +104 -0
  84. package/src/plugin/bridge-runtime-surface-facade.ts +8 -0
  85. package/src/plugin/bridge-status-facade.ts +76 -0
  86. package/src/plugin/bridge-status-worker-facade.ts +72 -0
  87. package/src/plugin/bridge-support-runtime.ts +137 -0
  88. package/src/plugin/bridge-surface-handlers-group.ts +242 -0
  89. package/src/plugin/bridge-surface-helpers.ts +28 -0
  90. package/src/plugin/capabilities.ts +1 -3
  91. package/src/plugin/channel-components.ts +289 -0
  92. package/src/plugin/channel-inbound-helpers.ts +149 -0
  93. package/src/plugin/channel-plugin-bridge-group.ts +129 -0
  94. package/src/plugin/channel-plugin-surface-group.ts +202 -0
  95. package/src/plugin/channel-runtime-builders-delivery.ts +513 -0
  96. package/src/plugin/channel-runtime-builders-status.ts +331 -0
  97. package/src/plugin/channel-runtime-builders.ts +25 -0
  98. package/src/plugin/channel-runtime-constants.ts +40 -0
  99. package/src/plugin/channel-runtime-types.ts +146 -0
  100. package/src/plugin/channel-send-runtime-group.ts +37 -0
  101. package/src/plugin/channel-send.ts +226 -0
  102. package/src/plugin/channel-utils.ts +102 -0
  103. package/src/plugin/config.ts +24 -3
  104. package/src/plugin/connection-handlers-helpers.ts +254 -0
  105. package/src/plugin/connection-handlers.ts +440 -0
  106. package/src/plugin/connection-state-helpers.ts +159 -0
  107. package/src/plugin/connection-state-runtime-group.ts +51 -0
  108. package/src/plugin/connection-state.ts +527 -0
  109. package/src/plugin/diagnostics-handlers.ts +211 -0
  110. package/src/plugin/error-message.ts +15 -0
  111. package/src/plugin/file-ack-runtime.ts +284 -0
  112. package/src/plugin/file-inbound-abort.ts +112 -0
  113. package/src/plugin/file-inbound-chunk.ts +146 -0
  114. package/src/plugin/file-inbound-complete.ts +153 -0
  115. package/src/plugin/file-inbound-handlers.ts +19 -0
  116. package/src/plugin/file-inbound-init.ts +122 -0
  117. package/src/plugin/file-inbound-runtime.ts +51 -0
  118. package/src/plugin/file-inbound-state.ts +62 -0
  119. package/src/plugin/file-transfer-logs.ts +227 -0
  120. package/src/plugin/file-transfer-orchestrator-chunk.ts +135 -0
  121. package/src/plugin/file-transfer-orchestrator.ts +304 -0
  122. package/src/plugin/file-transfer-runtime-group.ts +102 -0
  123. package/src/plugin/file-transfer-send.ts +89 -0
  124. package/src/plugin/file-transfer-setup.ts +206 -0
  125. package/src/plugin/gateway-event-context.ts +41 -0
  126. package/src/plugin/gateway-runtime.ts +14 -4
  127. package/src/plugin/inbound-acceptance.ts +107 -0
  128. package/src/plugin/inbound-handlers.ts +248 -0
  129. package/src/plugin/inbound-surface-handlers-group.ts +152 -0
  130. package/src/plugin/media-dedupe-runtime.ts +90 -0
  131. package/src/plugin/media-orchestrators-runtime-group.ts +316 -0
  132. package/src/plugin/message-ack-runtime.ts +284 -0
  133. package/src/plugin/message-send.ts +16 -6
  134. package/src/plugin/messaging.ts +98 -36
  135. package/src/plugin/outbound.ts +50 -8
  136. package/src/plugin/outbox-ack-logs.ts +136 -0
  137. package/src/plugin/outbox-ack-outcome.ts +128 -0
  138. package/src/plugin/outbox-drain-ack.ts +145 -0
  139. package/src/plugin/outbox-drain-failure.ts +84 -0
  140. package/src/plugin/outbox-drain-loop.ts +554 -0
  141. package/src/plugin/outbox-drain-post-push.ts +159 -0
  142. package/src/plugin/outbox-drain-runtime.ts +141 -0
  143. package/src/plugin/outbox-drain-schedule.ts +116 -0
  144. package/src/plugin/outbox-file-push-flow.ts +69 -0
  145. package/src/plugin/outbox-push-route-runtime-group.ts +81 -0
  146. package/src/plugin/outbox-push.ts +267 -0
  147. package/src/plugin/outbox-route.ts +181 -0
  148. package/src/plugin/outbox-text-push-flow.ts +90 -0
  149. package/src/plugin/runtime-diagnostics-assembler.ts +183 -0
  150. package/src/plugin/runtime-diagnostics-helpers.ts +302 -0
  151. package/src/plugin/runtime-diagnostics-payload-builders.ts +171 -0
  152. package/src/plugin/runtime-diagnostics-snapshot.ts +31 -0
  153. package/src/plugin/setup.ts +33 -6
  154. package/src/plugin/state-store.ts +249 -0
  155. package/src/plugin/state-transient-runtime-group.ts +105 -0
  156. package/src/plugin/status-runtime.ts +251 -0
  157. package/src/plugin/status.ts +33 -7
  158. package/src/plugin/target-runtime.ts +141 -0
  159. package/src/plugin/target-status-runtime-group.ts +130 -0
  160. package/src/plugin/transient-state-runtime.ts +82 -0
  161. package/src/runtime/outbound-ack-timeout.ts +5 -3
  162. package/src/runtime/outbound-flags.ts +24 -8
  163. package/src/runtime/status-snapshots.ts +36 -7
  164. package/src/runtime/status-worker.ts +34 -4
@@ -0,0 +1,202 @@
1
+ import { randomUUID } from 'node:crypto';
2
+ import type { ChannelMessageSendResult } from 'openclaw/plugin-sdk/channel-message';
3
+ import { listAccountIds, resolveAccount } from '../core/accounts.ts';
4
+ import { BncrConfigSchema } from '../core/config-schema.ts';
5
+ import { resolveBncrChannelPolicy } from '../core/policy.ts';
6
+ import type { ReplyPayloadInput } from '../messaging/outbound/reply-enqueue.ts';
7
+ import type { OutboundReplyTargetPolicy } from '../messaging/outbound/reply-target-policy.ts';
8
+ import { sendBncrMedia, sendBncrText } from '../messaging/outbound/send.ts';
9
+ import { normalizeBncrSendParams } from '../messaging/outbound/send-params.ts';
10
+ import type { OpenClawChannelToolSend, openClawJsonResult } from '../openclaw/sdk-helpers.ts';
11
+ import type {
12
+ BncrChannelConfigRoot,
13
+ BncrStatusRuntimeSnapshot,
14
+ BncrVerifiedTarget,
15
+ ChannelMessageActionAdapter,
16
+ } from './channel-runtime-types.ts';
17
+ import type { BNCR_CONFIG_SURFACE } from './config.ts';
18
+ import type { BncrGatewayAccountBridge } from './gateway-runtime.ts';
19
+ import { createBncrGatewayRuntime } from './gateway-runtime.ts';
20
+ import { BNCR_MESSAGE_RECEIVE_POLICY } from './message-policy.ts';
21
+ import { createBncrMessageSend } from './message-send.ts';
22
+ import { createBncrMessagingSurface } from './messaging.ts';
23
+ import { type BncrOutboundBridge, createBncrOutboundRuntime } from './outbound.ts';
24
+ import type { BNCR_SETUP_SURFACE } from './setup.ts';
25
+ import { createBncrStatusSurface } from './status.ts';
26
+
27
+ type BncrChannelMeta = {
28
+ id: string;
29
+ label: string;
30
+ selectionLabel: string;
31
+ docsPath: string;
32
+ blurb: string;
33
+ [key: string]: unknown;
34
+ };
35
+
36
+ type PluginSurfaceResult = ReturnType<typeof openClawJsonResult>;
37
+
38
+ export function createBncrChannelPluginSurfaceGroup(runtime: {
39
+ channelId: string;
40
+ getMessageSendBridge: () => {
41
+ channelMessageSendText: (
42
+ ctx: Record<string, unknown>,
43
+ ) => ChannelMessageSendResult | Promise<ChannelMessageSendResult>;
44
+ channelMessageSendMedia: (
45
+ ctx: Record<string, unknown>,
46
+ ) => ChannelMessageSendResult | Promise<ChannelMessageSendResult>;
47
+ channelMessageSendPayload: (
48
+ ctx: Record<string, unknown>,
49
+ ) => ChannelMessageSendResult | Promise<ChannelMessageSendResult>;
50
+ };
51
+ getOutboundBridge: () => BncrOutboundBridge;
52
+ getMessagingBridge: () => {
53
+ canonicalAgentId: undefined;
54
+ ensureCanonicalAgentId: (params: { cfg: BncrChannelConfigRoot; accountId: string }) => string;
55
+ resolveRouteBySession: (raw: string, accountId: string) => BncrVerifiedTarget['route'] | null;
56
+ };
57
+ getStatusBridge: () => {
58
+ getChannelSummary: (defaultAccountId: string) => Record<string, unknown>;
59
+ getAccountRuntimeSnapshot: (accountId?: string) => BncrStatusRuntimeSnapshot;
60
+ getStatusHeadline: (accountId?: string) => string;
61
+ };
62
+ getToolActionBridge: () => {
63
+ resolveVerifiedTarget: (to: string, accountId: string) => BncrVerifiedTarget;
64
+ rememberSessionRoute: (
65
+ sessionKey: string,
66
+ accountId: string,
67
+ route: BncrVerifiedTarget['route'],
68
+ ) => void;
69
+ enqueueFromReply: (args: {
70
+ accountId: string;
71
+ sessionKey: string;
72
+ route: BncrVerifiedTarget['route'];
73
+ payload: ReplyPayloadInput;
74
+ mediaLocalRoots?: readonly string[];
75
+ replyTargetPolicy?: OutboundReplyTargetPolicy;
76
+ }) => Promise<void>;
77
+ };
78
+ getGatewayBridge: () => {
79
+ channelStartAccount: BncrGatewayAccountBridge['channelStartAccount'];
80
+ channelStopAccount: BncrGatewayAccountBridge['channelStopAccount'];
81
+ };
82
+ channelMeta: BncrChannelMeta;
83
+ channelCapabilities: {
84
+ chatTypes: Array<'direct' | 'group' | 'thread'>;
85
+ media: boolean;
86
+ reply: boolean;
87
+ nativeCommands: boolean;
88
+ [key: string]: unknown;
89
+ };
90
+ gatewayMethods: string[];
91
+ configSurface: typeof BNCR_CONFIG_SURFACE;
92
+ setupSurface: typeof BNCR_SETUP_SURFACE;
93
+ extractToolSend: (
94
+ args: Record<string, unknown>,
95
+ action: string,
96
+ ) => OpenClawChannelToolSend | null;
97
+ openClawJsonResult: (payload: Record<string, unknown>) => PluginSurfaceResult;
98
+ }) {
99
+ const messageActions: ChannelMessageActionAdapter = {
100
+ describeMessageTool: ({ cfg }) => {
101
+ const channelCfg = cfg?.channels?.[runtime.channelId];
102
+ const accounts = channelCfg?.accounts;
103
+ const hasExplicitConfiguredAccount =
104
+ Boolean(channelCfg && typeof channelCfg === 'object') &&
105
+ resolveBncrChannelPolicy(channelCfg).enabled !== false &&
106
+ Boolean(accounts && typeof accounts === 'object') &&
107
+ Object.keys(accounts || {}).some(
108
+ (accountId) => resolveAccount(cfg, accountId).enabled !== false,
109
+ );
110
+
111
+ const runtimeBridge = runtime.getStatusBridge();
112
+ const hasConnectedRuntime = listAccountIds(cfg).some((accountId) => {
113
+ const resolved = resolveAccount(cfg, accountId);
114
+ const runtimeSnapshot = runtimeBridge.getAccountRuntimeSnapshot(resolved.accountId);
115
+ return Boolean(runtimeSnapshot?.connected);
116
+ });
117
+
118
+ if (!hasExplicitConfiguredAccount && !hasConnectedRuntime) {
119
+ return null;
120
+ }
121
+
122
+ return {
123
+ actions: ['send'] as const,
124
+ capabilities: [] as const,
125
+ };
126
+ },
127
+ supportsAction: ({ action }) => action === 'send',
128
+ extractToolSend: ({ args }) =>
129
+ runtime.extractToolSend(
130
+ (args && typeof args === 'object' ? (args as Record<string, unknown>) : {}) as Record<
131
+ string,
132
+ unknown
133
+ >,
134
+ 'sendMessage',
135
+ ) || null,
136
+ handleAction: async ({ action, params, accountId, mediaLocalRoots }) => {
137
+ if (action !== 'send') {
138
+ throw new Error(`Action ${action} is not supported for provider ${runtime.channelId}.`);
139
+ }
140
+ const normalized = normalizeBncrSendParams({ params, accountId: accountId || '' });
141
+
142
+ const toolActionBridge = runtime.getToolActionBridge();
143
+ const result = normalized.mediaUrl
144
+ ? await sendBncrMedia({
145
+ channelId: runtime.channelId,
146
+ accountId: normalized.accountId,
147
+ to: normalized.to,
148
+ text: normalized.caption,
149
+ mediaUrl: normalized.mediaUrl,
150
+ asVoice: normalized.asVoice,
151
+ audioAsVoice: normalized.audioAsVoice,
152
+ type: normalized.type,
153
+ mediaLocalRoots,
154
+ resolveVerifiedTarget: (to, accountId) =>
155
+ toolActionBridge.resolveVerifiedTarget(to, accountId),
156
+ rememberSessionRoute: (sessionKey, accountId, route) =>
157
+ toolActionBridge.rememberSessionRoute(sessionKey, accountId, route),
158
+ enqueueFromReply: (args) => toolActionBridge.enqueueFromReply(args),
159
+ createMessageId: () => randomUUID(),
160
+ })
161
+ : await sendBncrText({
162
+ channelId: runtime.channelId,
163
+ accountId: normalized.accountId,
164
+ to: normalized.to,
165
+ text: normalized.message,
166
+ mediaLocalRoots,
167
+ resolveVerifiedTarget: (to, accountId) =>
168
+ toolActionBridge.resolveVerifiedTarget(to, accountId),
169
+ rememberSessionRoute: (sessionKey, accountId, route) =>
170
+ toolActionBridge.rememberSessionRoute(sessionKey, accountId, route),
171
+ enqueueFromReply: (args) => toolActionBridge.enqueueFromReply(args),
172
+ createMessageId: () => randomUUID(),
173
+ });
174
+
175
+ return runtime.openClawJsonResult({ ok: true, ...result });
176
+ },
177
+ };
178
+
179
+ const plugin = {
180
+ id: runtime.channelId,
181
+ meta: runtime.channelMeta,
182
+ actions: messageActions,
183
+ message: {
184
+ receive: BNCR_MESSAGE_RECEIVE_POLICY,
185
+ send: createBncrMessageSend(runtime.getMessageSendBridge),
186
+ },
187
+ capabilities: runtime.channelCapabilities,
188
+ messaging: createBncrMessagingSurface(runtime.getMessagingBridge),
189
+ configSchema: BncrConfigSchema,
190
+ config: runtime.configSurface,
191
+ setup: runtime.setupSurface,
192
+ outbound: createBncrOutboundRuntime(runtime.getOutboundBridge),
193
+ status: createBncrStatusSurface(runtime.getStatusBridge),
194
+ gatewayMethods: runtime.gatewayMethods,
195
+ gateway: createBncrGatewayRuntime(runtime.getGatewayBridge),
196
+ };
197
+
198
+ return {
199
+ messageActions,
200
+ plugin,
201
+ };
202
+ }