@xmoxmo/bncr 0.5.6 → 0.5.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.
- package/dist/index.js +1 -9
- package/index.ts +1 -9
- package/package.json +1 -1
- package/scripts/check-pack.mjs +0 -6
- package/scripts/selfcheck.mjs +0 -7
- package/src/channel.ts +79 -43
- package/src/core/accounts.ts +1 -6
- package/src/core/connection-reachability.ts +1 -5
- package/src/core/dead-letter-diagnostics.ts +4 -10
- package/src/core/outbox-entry-builders.ts +34 -18
- package/src/core/outbox-file-transfer-failure.ts +5 -0
- package/src/core/outbox-file-transfer-guards.ts +6 -0
- package/src/core/outbox-file-transfer-prep.ts +8 -5
- package/src/core/outbox-file-transfer-success.ts +5 -0
- package/src/core/outbox-queue.ts +1 -7
- package/src/core/outbox-summary.ts +15 -27
- package/src/core/outbox-text-push-prep.ts +1 -36
- package/src/core/permissions.ts +1 -9
- package/src/core/persisted-outbox-entry.ts +54 -11
- package/src/core/policy.ts +1 -11
- package/src/core/register-trace.ts +2 -5
- package/src/core/status-meta.ts +1 -9
- package/src/core/status.ts +1 -21
- package/src/core/targets.ts +52 -70
- package/src/core/types.ts +1 -17
- package/src/core/value-sanitize.ts +38 -1
- package/src/messaging/inbound/commands.ts +2 -24
- package/src/messaging/inbound/dispatch-prep.ts +1 -1
- package/src/messaging/inbound/gate.ts +2 -31
- package/src/messaging/inbound/group-history.ts +0 -75
- package/src/messaging/inbound/parse.ts +1 -31
- package/src/messaging/inbound/scene-admin.ts +8 -8
- package/src/messaging/outbound/diagnostics.ts +1 -6
- package/src/messaging/outbound/durable-queue-adapter.ts +17 -11
- package/src/messaging/outbound/marker-parser.ts +62 -28
- package/src/messaging/outbound/media-dedupe.ts +2 -6
- package/src/messaging/outbound/media.ts +1 -6
- package/src/messaging/outbound/normalize-outbound-send.ts +327 -0
- package/src/messaging/outbound/queue-selectors.ts +1 -5
- package/src/messaging/outbound/reply-enqueue.ts +121 -70
- package/src/messaging/outbound/session-route.ts +6 -11
- package/src/messaging/outbound/target-resolver.ts +1 -6
- package/src/openclaw/routing-runtime.ts +0 -11
- package/src/openclaw/sdk-helpers.ts +0 -2
- package/src/plugin/bridge-file-transfer-push-facade.ts +6 -6
- package/src/plugin/bridge-media-facade.ts +0 -1
- package/src/plugin/channel-components.ts +0 -13
- package/src/plugin/channel-plugin-surface-group.ts +52 -65
- package/src/plugin/channel-runtime-builders-delivery.ts +1 -0
- package/src/plugin/channel-runtime-types.ts +2 -24
- package/src/plugin/channel-send.ts +205 -221
- package/src/plugin/channel-utils.ts +8 -29
- package/src/plugin/connection-handlers-helpers.ts +0 -15
- package/src/plugin/connection-handlers.ts +6 -5
- package/src/plugin/file-inbound-abort.ts +5 -0
- package/src/plugin/file-inbound-chunk.ts +5 -0
- package/src/plugin/file-inbound-complete.ts +5 -0
- package/src/plugin/media-dedupe-runtime.ts +1 -6
- package/src/plugin/message-send.ts +7 -8
- package/src/plugin/outbound.ts +6 -26
- package/src/plugin/outbox-drain-loop.ts +2 -2
- package/src/plugin/outbox-file-push-flow.ts +7 -7
- package/src/plugin/outbox-text-push-flow.ts +1 -1
- package/src/plugin/target-runtime.ts +1 -6
- package/src/runtime/outbound-ack-timeout.ts +1 -8
- package/src/runtime/outbox-transitions.ts +1 -4
- package/src/runtime/status-worker.ts +1 -9
- package/src/messaging/outbound/build-send-action.ts +0 -130
- package/src/messaging/outbound/durable-message-adapter.ts +0 -121
- package/src/messaging/outbound/send-params.ts +0 -114
- package/src/messaging/outbound/send.ts +0 -127
- package/src/openclaw/ingress-runtime.ts +0 -25
- package/src/plugin/channel-send-runtime-group.ts +0 -37
|
@@ -1,23 +1,16 @@
|
|
|
1
1
|
import type { OutboxEntry } from './types.ts';
|
|
2
2
|
|
|
3
|
-
type OutboxSummaryMeta = {
|
|
4
|
-
kind?: string;
|
|
5
|
-
asVoice?: boolean;
|
|
6
|
-
audioAsVoice?: boolean;
|
|
7
|
-
type?: unknown;
|
|
8
|
-
mediaUrl?: unknown;
|
|
9
|
-
text?: unknown;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
3
|
type OutboxSummaryMessage = {
|
|
13
4
|
type?: unknown;
|
|
14
5
|
msg?: unknown;
|
|
6
|
+
mediaUrl?: unknown;
|
|
7
|
+
audioAsVoice?: boolean;
|
|
8
|
+
asVoice?: boolean;
|
|
15
9
|
};
|
|
16
10
|
|
|
17
11
|
type OutboxSummaryPayload = {
|
|
18
12
|
type?: unknown;
|
|
19
13
|
message?: OutboxSummaryMessage;
|
|
20
|
-
_meta?: OutboxSummaryMeta;
|
|
21
14
|
};
|
|
22
15
|
|
|
23
16
|
function inferMediaTypeFromUrl(raw: string): 'image' | 'video' | 'audio' | 'file' {
|
|
@@ -47,27 +40,22 @@ function summarizeOutboxType(
|
|
|
47
40
|
) {
|
|
48
41
|
const directType = asString(msg.type || '');
|
|
49
42
|
if (directType) return directType;
|
|
50
|
-
|
|
51
|
-
const
|
|
52
|
-
if (
|
|
53
|
-
if (
|
|
54
|
-
const
|
|
55
|
-
if (
|
|
56
|
-
return inferMediaTypeFromUrl(asString(meta.mediaUrl || ''));
|
|
43
|
+
// Infer type from mediaUrl when no direct type is set
|
|
44
|
+
const mediaUrl = asString(msg.mediaUrl || '');
|
|
45
|
+
if (mediaUrl) {
|
|
46
|
+
if (msg.audioAsVoice === true || msg.asVoice === true) return 'voice';
|
|
47
|
+
const inferred = inferMediaTypeFromUrl(mediaUrl);
|
|
48
|
+
if (inferred) return inferred;
|
|
57
49
|
}
|
|
58
|
-
|
|
59
50
|
return asString(payload?.type || 'unknown');
|
|
60
51
|
}
|
|
61
52
|
|
|
62
|
-
function summarizeOutboxText(
|
|
63
|
-
|
|
64
|
-
msg: OutboxSummaryMessage,
|
|
65
|
-
asString: (value: unknown) => string,
|
|
66
|
-
) {
|
|
67
|
-
const text = asString(msg.msg || payload?._meta?.text || '').trim();
|
|
53
|
+
function summarizeOutboxText(msg: OutboxSummaryMessage, asString: (value: unknown) => string) {
|
|
54
|
+
const text = asString(msg.msg || '').trim();
|
|
68
55
|
if (text) return text;
|
|
69
|
-
|
|
70
|
-
|
|
56
|
+
// Fall back to filename from mediaUrl when no caption is set
|
|
57
|
+
const mediaUrl = asString(msg.mediaUrl || '');
|
|
58
|
+
if (mediaUrl) return filenameFromUrl(mediaUrl);
|
|
71
59
|
return '';
|
|
72
60
|
}
|
|
73
61
|
|
|
@@ -80,7 +68,7 @@ export function summarizeOutboxEntry(args: {
|
|
|
80
68
|
const payload = args.entry.payload as OutboxSummaryPayload;
|
|
81
69
|
const msg = payload?.message || {};
|
|
82
70
|
const type = summarizeOutboxType(payload, msg, args.asString);
|
|
83
|
-
const text = summarizeOutboxText(
|
|
71
|
+
const text = summarizeOutboxText(msg, args.asString);
|
|
84
72
|
const preview = args.summarizeTextPreview(text);
|
|
85
73
|
return [type, args.formatDisplayScope(args.entry.route), preview].join('|');
|
|
86
74
|
}
|
|
@@ -1,36 +1 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
export function prepareTextPushRouteSelection(args: {
|
|
4
|
-
entry: OutboxEntry;
|
|
5
|
-
owner: BncrConnection | null;
|
|
6
|
-
resolvePushConnIds: (accountId: string) => Iterable<string>;
|
|
7
|
-
resolveRecentInboundConnIds: (accountId: string) => Iterable<string>;
|
|
8
|
-
hasRecentInboundReachability: (accountId: string) => boolean;
|
|
9
|
-
isRevalidatedAttemptedConn: (connId: string) => boolean;
|
|
10
|
-
selectOutboxRouteCandidates: (args: {
|
|
11
|
-
routeCandidates: Iterable<string>;
|
|
12
|
-
attemptedConnIds: string[];
|
|
13
|
-
recentInboundConnIds: Iterable<string>;
|
|
14
|
-
ownerConnId?: string;
|
|
15
|
-
recentInboundReachable: boolean;
|
|
16
|
-
isRevalidatedAttemptedConn: (connId: string) => boolean;
|
|
17
|
-
}) => {
|
|
18
|
-
connIds: Iterable<string>;
|
|
19
|
-
recentInboundReachable: boolean;
|
|
20
|
-
routeReason: string;
|
|
21
|
-
ownerConnId?: string;
|
|
22
|
-
};
|
|
23
|
-
}) {
|
|
24
|
-
const attemptedConnIds = Array.isArray(args.entry.routeAttemptConnIds)
|
|
25
|
-
? args.entry.routeAttemptConnIds.filter((v): v is string => typeof v === 'string' && !!v)
|
|
26
|
-
: [];
|
|
27
|
-
|
|
28
|
-
return args.selectOutboxRouteCandidates({
|
|
29
|
-
routeCandidates: args.resolvePushConnIds(args.entry.accountId),
|
|
30
|
-
attemptedConnIds,
|
|
31
|
-
recentInboundConnIds: args.resolveRecentInboundConnIds(args.entry.accountId),
|
|
32
|
-
ownerConnId: args.owner?.connId,
|
|
33
|
-
recentInboundReachable: args.hasRecentInboundReachability(args.entry.accountId),
|
|
34
|
-
isRevalidatedAttemptedConn: args.isRevalidatedAttemptedConn,
|
|
35
|
-
});
|
|
36
|
-
}
|
|
1
|
+
export { prepareRouteSelection as prepareTextPushRouteSelection } from './outbox-file-transfer-prep.ts';
|
package/src/core/permissions.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
if (typeof v === 'string') return v;
|
|
3
|
-
if (v == null) return fallback;
|
|
4
|
-
return String(v);
|
|
5
|
-
}
|
|
1
|
+
import { asString } from '../core/value-sanitize.ts';
|
|
6
2
|
|
|
7
3
|
type BncrElevatedConfigRoot = {
|
|
8
4
|
tools?: {
|
|
@@ -14,21 +10,18 @@ type BncrElevatedConfigRoot = {
|
|
|
14
10
|
};
|
|
15
11
|
};
|
|
16
12
|
};
|
|
17
|
-
|
|
18
13
|
export function getBncrElevatedConfig(rootCfg: BncrElevatedConfigRoot | null | undefined) {
|
|
19
14
|
const elevated = rootCfg?.tools?.elevated || {};
|
|
20
15
|
const allowFrom = elevated?.allowFrom || {};
|
|
21
16
|
const bncrRules = Array.isArray(allowFrom?.bncr)
|
|
22
17
|
? allowFrom.bncr.map((x: unknown) => asString(x).trim()).filter(Boolean)
|
|
23
18
|
: [];
|
|
24
|
-
|
|
25
19
|
return {
|
|
26
20
|
enabled: elevated?.enabled === true,
|
|
27
21
|
bncrAllowed: bncrRules.length > 0,
|
|
28
22
|
bncrRules,
|
|
29
23
|
};
|
|
30
24
|
}
|
|
31
|
-
|
|
32
25
|
export function buildBncrPermissionSummary(rootCfg: BncrElevatedConfigRoot | null | undefined) {
|
|
33
26
|
const elevated = getBncrElevatedConfig(rootCfg);
|
|
34
27
|
return {
|
|
@@ -40,5 +33,4 @@ export function buildBncrPermissionSummary(rootCfg: BncrElevatedConfigRoot | nul
|
|
|
40
33
|
: 'bncr elevated not explicitly allowed',
|
|
41
34
|
};
|
|
42
35
|
}
|
|
43
|
-
|
|
44
36
|
export type BncrPermissionSummary = ReturnType<typeof buildBncrPermissionSummary>;
|
|
@@ -1,22 +1,12 @@
|
|
|
1
1
|
import { normalizeAccountId } from './accounts.ts';
|
|
2
2
|
import { normalizeStoredSessionKey, parseRouteLike } from './targets.ts';
|
|
3
3
|
import type { OutboxEntry } from './types.ts';
|
|
4
|
+
import { asString, finiteNumberOr } from './value-sanitize.ts';
|
|
4
5
|
|
|
5
6
|
type PersistedOutboxEntryInput = Partial<OutboxEntry> & {
|
|
6
7
|
payload?: Record<string, unknown>;
|
|
7
8
|
};
|
|
8
9
|
|
|
9
|
-
function asString(v: unknown, fallback = ''): string {
|
|
10
|
-
if (typeof v === 'string') return v;
|
|
11
|
-
if (v == null) return fallback;
|
|
12
|
-
return String(v);
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function finiteNumberOr(value: unknown, fallback: number): number {
|
|
16
|
-
const n = Number(value);
|
|
17
|
-
return Number.isFinite(n) ? n : fallback;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
10
|
function optionalFiniteNumber(value: unknown): number | undefined {
|
|
21
11
|
if (value == null || value === '') return undefined;
|
|
22
12
|
const n = Number(value);
|
|
@@ -43,6 +33,59 @@ export function normalizePersistedOutboxEntry(args: {
|
|
|
43
33
|
payload.groupId = route.groupId;
|
|
44
34
|
payload.userId = route.userId;
|
|
45
35
|
|
|
36
|
+
// Migrate old _meta format entries to the new message format.
|
|
37
|
+
// Old entries persisted before the _meta→message refactor carry media
|
|
38
|
+
// metadata in payload._meta. New code reads from payload.message.
|
|
39
|
+
const oldMeta = payload._meta as Record<string, unknown> | undefined;
|
|
40
|
+
if (oldMeta && !payload.message) {
|
|
41
|
+
const oldKind = asString(oldMeta.kind);
|
|
42
|
+
if (oldKind === 'file-transfer') {
|
|
43
|
+
// Move replyToId from _meta to payload top level
|
|
44
|
+
const replyToId = asString(oldMeta.replyToId || '').trim() || undefined;
|
|
45
|
+
if (replyToId) payload.replyToId = replyToId;
|
|
46
|
+
|
|
47
|
+
// Build new message object from old _meta fields
|
|
48
|
+
const msg: Record<string, unknown> = {
|
|
49
|
+
type: (oldMeta.type as string) || 'file',
|
|
50
|
+
msg: oldMeta.text || '',
|
|
51
|
+
mediaUrl: oldMeta.mediaUrl,
|
|
52
|
+
mediaLocalRoots: oldMeta.mediaLocalRoots,
|
|
53
|
+
asVoice: oldMeta.asVoice === true,
|
|
54
|
+
audioAsVoice: oldMeta.audioAsVoice === true,
|
|
55
|
+
downloadMedia: oldMeta.downloadMedia === true,
|
|
56
|
+
kind: oldMeta.messageKind,
|
|
57
|
+
transferMode: 'media',
|
|
58
|
+
path: '',
|
|
59
|
+
base64: '',
|
|
60
|
+
fileName: '',
|
|
61
|
+
};
|
|
62
|
+
// Strip consumed _meta keys, pass through remaining as extra
|
|
63
|
+
const consumed = new Set([
|
|
64
|
+
'kind',
|
|
65
|
+
'mediaUrl',
|
|
66
|
+
'mediaLocalRoots',
|
|
67
|
+
'text',
|
|
68
|
+
'asVoice',
|
|
69
|
+
'audioAsVoice',
|
|
70
|
+
'replyToId',
|
|
71
|
+
'finalEvent',
|
|
72
|
+
'type',
|
|
73
|
+
'downloadMedia',
|
|
74
|
+
'messageKind',
|
|
75
|
+
'retryCount',
|
|
76
|
+
'nextAttemptAt',
|
|
77
|
+
]);
|
|
78
|
+
for (const [key, value] of Object.entries(oldMeta)) {
|
|
79
|
+
if (!consumed.has(key)) {
|
|
80
|
+
msg[key] = value;
|
|
81
|
+
}
|
|
82
|
+
}
|
|
83
|
+
payload.message = msg;
|
|
84
|
+
}
|
|
85
|
+
// Remove old _meta after migration
|
|
86
|
+
delete payload._meta;
|
|
87
|
+
}
|
|
88
|
+
|
|
46
89
|
return {
|
|
47
90
|
...entry,
|
|
48
91
|
messageId: asString(entry.messageId).trim(),
|
package/src/core/policy.ts
CHANGED
|
@@ -1,8 +1,4 @@
|
|
|
1
|
-
|
|
2
|
-
if (typeof v === 'string') return v;
|
|
3
|
-
if (v == null) return fallback;
|
|
4
|
-
return String(v);
|
|
5
|
-
}
|
|
1
|
+
import { asBoolean, asString } from './value-sanitize.ts';
|
|
6
2
|
|
|
7
3
|
export type BncrChannelPolicyConfig = {
|
|
8
4
|
enabled?: boolean;
|
|
@@ -21,12 +17,6 @@ function asList(v: unknown): string[] {
|
|
|
21
17
|
return v.map((x) => asString(x).trim()).filter(Boolean);
|
|
22
18
|
}
|
|
23
19
|
|
|
24
|
-
function asBoolean(v: unknown, fallback = false): boolean {
|
|
25
|
-
if (typeof v === 'boolean') return v;
|
|
26
|
-
if (v == null) return fallback;
|
|
27
|
-
return String(v).trim().toLowerCase() === 'true';
|
|
28
|
-
}
|
|
29
|
-
|
|
30
20
|
function normalizeDmPolicy(value: unknown): BncrDmPolicy {
|
|
31
21
|
const normalized = asString(value || 'open')
|
|
32
22
|
.trim()
|
|
@@ -1,9 +1,6 @@
|
|
|
1
|
-
|
|
1
|
+
import { finiteNumberOr } from './value-sanitize.ts';
|
|
2
2
|
|
|
3
|
-
|
|
4
|
-
const n = Number(value);
|
|
5
|
-
return Number.isFinite(n) ? n : fallback;
|
|
6
|
-
}
|
|
3
|
+
const DEFAULT_REGISTER_WARMUP_WINDOW_MS = 30_000;
|
|
7
4
|
|
|
8
5
|
export type RegisterTraceEntry = {
|
|
9
6
|
ts: number;
|
package/src/core/status-meta.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { PendingAdmission } from './types.ts';
|
|
2
|
+
import { finiteNumberOrNull, now } from './value-sanitize.ts';
|
|
2
3
|
|
|
3
4
|
type RuntimeStatusMetaDisplayInput = {
|
|
4
5
|
pendingAdmissions?: PendingAdmission[];
|
|
@@ -8,15 +9,6 @@ type RuntimeStatusMetaDisplayInput = {
|
|
|
8
9
|
lastOutboundAt?: number | null;
|
|
9
10
|
};
|
|
10
11
|
|
|
11
|
-
function now() {
|
|
12
|
-
return Date.now();
|
|
13
|
-
}
|
|
14
|
-
|
|
15
|
-
function finiteNumberOrNull(value: unknown): number | null {
|
|
16
|
-
const n = Number(value);
|
|
17
|
-
return Number.isFinite(n) ? n : null;
|
|
18
|
-
}
|
|
19
|
-
|
|
20
12
|
function formatPendingAdmissionScope(route: unknown): string | null {
|
|
21
13
|
if (!route || typeof route !== 'object') return null;
|
|
22
14
|
const record = route as Record<string, unknown>;
|
package/src/core/status.ts
CHANGED
|
@@ -2,6 +2,7 @@ import fs from 'node:fs';
|
|
|
2
2
|
import path from 'node:path';
|
|
3
3
|
import { buildRuntimeStatusMetaDisplay } from './status-meta.ts';
|
|
4
4
|
import type { BncrDiagnosticsSummary, BncrRuntimeLastSession, PendingAdmission } from './types.ts';
|
|
5
|
+
import { finiteNumberOr, nonNegativeFiniteNumberOr, now } from './value-sanitize.ts';
|
|
5
6
|
|
|
6
7
|
type RuntimeStatusInput = {
|
|
7
8
|
accountId: string;
|
|
@@ -27,19 +28,6 @@ type RuntimeStatusInput = {
|
|
|
27
28
|
channelRoot?: string;
|
|
28
29
|
};
|
|
29
30
|
|
|
30
|
-
function now() {
|
|
31
|
-
return Date.now();
|
|
32
|
-
}
|
|
33
|
-
|
|
34
|
-
function finiteNumberOr(value: unknown, fallback: number): number {
|
|
35
|
-
const n = Number(value);
|
|
36
|
-
return Number.isFinite(n) ? n : fallback;
|
|
37
|
-
}
|
|
38
|
-
|
|
39
|
-
function nonNegativeFiniteNumberOr(value: unknown, fallback: number): number {
|
|
40
|
-
return Math.max(0, finiteNumberOr(value, fallback));
|
|
41
|
-
}
|
|
42
|
-
|
|
43
31
|
export function buildIntegratedDiagnostics(input: RuntimeStatusInput): BncrDiagnosticsSummary {
|
|
44
32
|
const root = input.channelRoot || path.join(process.cwd(), 'plugins', 'bncr');
|
|
45
33
|
const pluginIndexExists = fs.existsSync(path.join(root, 'index.ts'));
|
|
@@ -230,11 +218,3 @@ export function buildAccountStatusSnapshot(input: {
|
|
|
230
218
|
diagnostics,
|
|
231
219
|
};
|
|
232
220
|
}
|
|
233
|
-
|
|
234
|
-
export function buildChannelSummaryFromRuntime(input: RuntimeStatusInput) {
|
|
235
|
-
const headline = buildStatusHeadlineFromRuntime(input);
|
|
236
|
-
return {
|
|
237
|
-
linked: input.connected,
|
|
238
|
-
self: { e164: headline },
|
|
239
|
-
};
|
|
240
|
-
}
|
package/src/core/targets.ts
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
1
|
import type { BncrRoute } from './types.ts';
|
|
2
|
+
import { asString } from './value-sanitize.ts';
|
|
2
3
|
|
|
3
4
|
type RouteInputLike = {
|
|
4
5
|
platform?: unknown;
|
|
@@ -28,14 +29,8 @@ export type BncrExplicitTarget = {
|
|
|
28
29
|
groupId?: string;
|
|
29
30
|
};
|
|
30
31
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
if (value == null) return fallback;
|
|
34
|
-
return String(value);
|
|
35
|
-
}
|
|
36
|
-
|
|
37
|
-
export function parseRouteFromStandardDisplayScope(scope: string): BncrRoute | null {
|
|
38
|
-
const parts = asTargetString(scope).trim().split(':');
|
|
32
|
+
function parseRouteFromStandardDisplayScope(scope: string): BncrRoute | null {
|
|
33
|
+
const parts = asString(scope).trim().split(':');
|
|
39
34
|
if (parts.length === 3) {
|
|
40
35
|
const [platform, kind, id] = parts;
|
|
41
36
|
if (!platform || !kind || !id) return null;
|
|
@@ -65,7 +60,7 @@ export function parseRouteFromStandardDisplayScope(scope: string): BncrRoute | n
|
|
|
65
60
|
}
|
|
66
61
|
|
|
67
62
|
function parseGroupScope(scope: string): { platform: string; groupId: string } | null {
|
|
68
|
-
const parts =
|
|
63
|
+
const parts = asString(scope).trim().split(':');
|
|
69
64
|
if (parts.length !== 2) return null;
|
|
70
65
|
const [platform, groupId] = parts;
|
|
71
66
|
if (!platform || !groupId) return null;
|
|
@@ -73,15 +68,15 @@ function parseGroupScope(scope: string): { platform: string; groupId: string } |
|
|
|
73
68
|
}
|
|
74
69
|
|
|
75
70
|
function parseDirectScope(scope: string): { platform: string; userId: string } | null {
|
|
76
|
-
const parts =
|
|
71
|
+
const parts = asString(scope).trim().split(':');
|
|
77
72
|
if (parts.length !== 2) return null;
|
|
78
73
|
const [platform, userId] = parts;
|
|
79
74
|
if (!platform || !userId) return null;
|
|
80
75
|
return { platform, userId };
|
|
81
76
|
}
|
|
82
77
|
|
|
83
|
-
|
|
84
|
-
const raw =
|
|
78
|
+
function normalizeDisplayScopePrefix(scope: string): string {
|
|
79
|
+
const raw = asString(scope).trim();
|
|
85
80
|
if (!raw) return '';
|
|
86
81
|
if (raw.startsWith('Bncr:')) return raw;
|
|
87
82
|
if (/^bncr[:-]/i.test(raw)) return raw;
|
|
@@ -89,11 +84,7 @@ export function normalizeDisplayScopePrefix(scope: string): string {
|
|
|
89
84
|
return `Bncr:${raw}`;
|
|
90
85
|
}
|
|
91
86
|
|
|
92
|
-
|
|
93
|
-
return 'direct';
|
|
94
|
-
}
|
|
95
|
-
|
|
96
|
-
export function buildExplicitTargetResult(args: {
|
|
87
|
+
function buildExplicitTargetResult(args: {
|
|
97
88
|
raw: string;
|
|
98
89
|
source:
|
|
99
90
|
| 'display-scope'
|
|
@@ -121,8 +112,8 @@ export function buildExplicitTargetResult(args: {
|
|
|
121
112
|
};
|
|
122
113
|
}
|
|
123
114
|
|
|
124
|
-
|
|
125
|
-
const raw =
|
|
115
|
+
function isLowerHex(input: string): boolean {
|
|
116
|
+
const raw = asString(input).trim();
|
|
126
117
|
return !!raw && /^[0-9a-fA-F]+$/.test(raw) && raw.length % 2 === 0;
|
|
127
118
|
}
|
|
128
119
|
|
|
@@ -131,18 +122,18 @@ export function routeScopeToHex(route: BncrRoute): string {
|
|
|
131
122
|
return Buffer.from(raw, 'utf8').toString('hex').toLowerCase();
|
|
132
123
|
}
|
|
133
124
|
|
|
134
|
-
|
|
125
|
+
function groupScopeToHex(route: BncrRoute): string {
|
|
135
126
|
const raw = `${route.platform}:${route.groupId}`;
|
|
136
127
|
return Buffer.from(raw, 'utf8').toString('hex').toLowerCase();
|
|
137
128
|
}
|
|
138
129
|
|
|
139
|
-
|
|
130
|
+
function directScopeToHex(route: BncrRoute): string {
|
|
140
131
|
const raw = `${route.platform}:${route.userId}`;
|
|
141
132
|
return Buffer.from(raw, 'utf8').toString('hex').toLowerCase();
|
|
142
133
|
}
|
|
143
134
|
|
|
144
|
-
|
|
145
|
-
const parts =
|
|
135
|
+
function parseRouteFromScope(scope: string): BncrRoute | null {
|
|
136
|
+
const parts = asString(scope).trim().split(':');
|
|
146
137
|
if (parts.length < 3) return null;
|
|
147
138
|
const [platform, groupId, userId] = parts;
|
|
148
139
|
if (!platform || !groupId || !userId) return null;
|
|
@@ -150,8 +141,8 @@ export function parseRouteFromScope(scope: string): BncrRoute | null {
|
|
|
150
141
|
return { platform, groupId, userId };
|
|
151
142
|
}
|
|
152
143
|
|
|
153
|
-
|
|
154
|
-
const rawHex =
|
|
144
|
+
function parseRouteFromHexScope(scopeHex: string): BncrRoute | null {
|
|
145
|
+
const rawHex = asString(scopeHex).trim();
|
|
155
146
|
if (!isLowerHex(rawHex)) return null;
|
|
156
147
|
|
|
157
148
|
try {
|
|
@@ -162,8 +153,8 @@ export function parseRouteFromHexScope(scopeHex: string): BncrRoute | null {
|
|
|
162
153
|
}
|
|
163
154
|
}
|
|
164
155
|
|
|
165
|
-
|
|
166
|
-
const rawHex =
|
|
156
|
+
function parseGroupRouteFromHexScope(scopeHex: string): BncrRoute | null {
|
|
157
|
+
const rawHex = asString(scopeHex).trim();
|
|
167
158
|
if (!isLowerHex(rawHex)) return null;
|
|
168
159
|
|
|
169
160
|
try {
|
|
@@ -176,8 +167,8 @@ export function parseGroupRouteFromHexScope(scopeHex: string): BncrRoute | null
|
|
|
176
167
|
}
|
|
177
168
|
}
|
|
178
169
|
|
|
179
|
-
|
|
180
|
-
const rawHex =
|
|
170
|
+
function parseDirectRouteFromHexScope(scopeHex: string): BncrRoute | null {
|
|
171
|
+
const rawHex = asString(scopeHex).trim();
|
|
181
172
|
if (!isLowerHex(rawHex)) return null;
|
|
182
173
|
|
|
183
174
|
try {
|
|
@@ -197,15 +188,15 @@ export function parseStrictBncrSessionKey(input: string): {
|
|
|
197
188
|
scopeHex: string;
|
|
198
189
|
route: BncrRoute;
|
|
199
190
|
} | null {
|
|
200
|
-
const raw =
|
|
191
|
+
const raw = asString(input).trim();
|
|
201
192
|
if (!raw) return null;
|
|
202
193
|
|
|
203
194
|
const m = raw.match(/^agent:([^:]+):bncr:(direct|group):(.+)$/);
|
|
204
195
|
if (!m?.[1] || !m?.[2] || !m?.[3]) return null;
|
|
205
196
|
|
|
206
|
-
const inputAgentId =
|
|
197
|
+
const inputAgentId = asString(m[1]).trim();
|
|
207
198
|
const inputKind = m[2] as BncrSessionKind;
|
|
208
|
-
const payload =
|
|
199
|
+
const payload = asString(m[3]).trim();
|
|
209
200
|
let route: BncrRoute | null = null;
|
|
210
201
|
let scopeHex = '';
|
|
211
202
|
|
|
@@ -243,13 +234,13 @@ export function parseStrictBncrSessionKey(input: string): {
|
|
|
243
234
|
};
|
|
244
235
|
}
|
|
245
236
|
|
|
246
|
-
|
|
237
|
+
function parseLegacySessionKey(input: string): {
|
|
247
238
|
route: BncrRoute;
|
|
248
239
|
inputKind: BncrSessionKind;
|
|
249
240
|
inputAgentId?: string;
|
|
250
241
|
source: 'legacy-direct' | 'legacy-bncr' | 'legacy-agent' | 'hex';
|
|
251
242
|
} | null {
|
|
252
|
-
const raw =
|
|
243
|
+
const raw = asString(input).trim();
|
|
253
244
|
if (!raw) return null;
|
|
254
245
|
|
|
255
246
|
const directLegacy = raw.match(/^agent:([^:]+):bncr:direct:([0-9a-fA-F]+):0$/);
|
|
@@ -304,10 +295,10 @@ export function parseLegacySessionKey(input: string): {
|
|
|
304
295
|
return null;
|
|
305
296
|
}
|
|
306
297
|
|
|
307
|
-
|
|
308
|
-
const platform =
|
|
309
|
-
const groupId =
|
|
310
|
-
const userId =
|
|
298
|
+
function isLegacyNoiseRoute(route: BncrRoute): boolean {
|
|
299
|
+
const platform = asString(route.platform).trim().toLowerCase();
|
|
300
|
+
const groupId = asString(route.groupId).trim().toLowerCase();
|
|
301
|
+
const userId = asString(route.userId).trim().toLowerCase();
|
|
311
302
|
|
|
312
303
|
if (platform === 'agent' && groupId === 'main' && userId === 'bncr') return true;
|
|
313
304
|
if (platform === 'bncr' && userId === '0' && isLowerHex(groupId)) return true;
|
|
@@ -326,7 +317,7 @@ export function parseRouteFromDisplayScope(scope: string): BncrRoute | null {
|
|
|
326
317
|
}
|
|
327
318
|
|
|
328
319
|
export function buildCanonicalBncrSessionKey(route: BncrRoute, canonicalAgentId: string): string {
|
|
329
|
-
const agentId =
|
|
320
|
+
const agentId = asString(canonicalAgentId).trim() || 'main';
|
|
330
321
|
if (route.groupId !== '0') {
|
|
331
322
|
return `agent:${agentId}:bncr:group:${groupScopeToHex(route)}`;
|
|
332
323
|
}
|
|
@@ -338,7 +329,7 @@ export function normalizeStoredSessionKey(
|
|
|
338
329
|
canonicalAgentId?: string | null,
|
|
339
330
|
helpers?: { normalizeTaskKey?: (input: unknown) => string | null },
|
|
340
331
|
): { sessionKey: string; route: BncrRoute } | null {
|
|
341
|
-
const raw =
|
|
332
|
+
const raw = asString(input).trim();
|
|
342
333
|
if (!raw) return null;
|
|
343
334
|
|
|
344
335
|
let taskKey: string | null = null;
|
|
@@ -346,7 +337,7 @@ export function normalizeStoredSessionKey(
|
|
|
346
337
|
|
|
347
338
|
const taskTagged = raw.match(/^(.*):task:([a-z0-9_-]{1,32})$/i);
|
|
348
339
|
if (taskTagged) {
|
|
349
|
-
base =
|
|
340
|
+
base = asString(taskTagged[1]).trim();
|
|
350
341
|
taskKey = helpers?.normalizeTaskKey?.(taskTagged[2]) || null;
|
|
351
342
|
}
|
|
352
343
|
|
|
@@ -370,7 +361,7 @@ export function normalizeStoredSessionKey(
|
|
|
370
361
|
if (!route) return null;
|
|
371
362
|
if (isLegacyNoiseRoute(route)) return null;
|
|
372
363
|
|
|
373
|
-
const finalAgentId =
|
|
364
|
+
const finalAgentId = asString(canonicalAgentId).trim() || passthroughAgentId;
|
|
374
365
|
if (!finalAgentId) return null;
|
|
375
366
|
|
|
376
367
|
const finalSessionKey = buildCanonicalBncrSessionKey(route, finalAgentId);
|
|
@@ -385,7 +376,7 @@ export function normalizeInboundSessionKey(
|
|
|
385
376
|
route: BncrRoute,
|
|
386
377
|
canonicalAgentId: string,
|
|
387
378
|
): string | null {
|
|
388
|
-
const raw =
|
|
379
|
+
const raw = asString(scope).trim();
|
|
389
380
|
let finalRoute: BncrRoute | null = null;
|
|
390
381
|
|
|
391
382
|
if (!raw) finalRoute = route;
|
|
@@ -399,15 +390,11 @@ export function normalizeInboundSessionKey(
|
|
|
399
390
|
return buildCanonicalBncrSessionKey(finalRoute, canonicalAgentId);
|
|
400
391
|
}
|
|
401
392
|
|
|
402
|
-
export function buildFallbackSessionKey(route: BncrRoute, canonicalAgentId: string): string {
|
|
403
|
-
return buildCanonicalBncrSessionKey(route, canonicalAgentId);
|
|
404
|
-
}
|
|
405
|
-
|
|
406
393
|
export function parseRouteLike(input: unknown): BncrRoute | null {
|
|
407
394
|
const routeInput = input && typeof input === 'object' ? (input as RouteInputLike) : null;
|
|
408
|
-
const platform =
|
|
409
|
-
const groupId =
|
|
410
|
-
const userId =
|
|
395
|
+
const platform = asString(routeInput?.platform || '').trim();
|
|
396
|
+
const groupId = asString(routeInput?.groupId || '').trim();
|
|
397
|
+
const userId = asString(routeInput?.userId || '').trim();
|
|
411
398
|
if (!platform || !groupId || !userId) return null;
|
|
412
399
|
if (groupId !== '0' && userId !== '0') return null;
|
|
413
400
|
return { platform, groupId, userId };
|
|
@@ -421,28 +408,23 @@ export type BncrExplicitTargetSource =
|
|
|
421
408
|
| 'route-scope';
|
|
422
409
|
|
|
423
410
|
export function formatDisplayScope(route: BncrRoute): string {
|
|
424
|
-
const platform =
|
|
425
|
-
const groupId =
|
|
426
|
-
const userId =
|
|
411
|
+
const platform = asString(route?.platform).trim();
|
|
412
|
+
const groupId = asString(route?.groupId || '0').trim() || '0';
|
|
413
|
+
const userId = asString(route?.userId || '0').trim() || '0';
|
|
427
414
|
if (groupId !== '0') return `Bncr:${platform}:${groupId}:0`;
|
|
428
415
|
return `Bncr:${platform}:0:${userId}`;
|
|
429
416
|
}
|
|
430
417
|
|
|
431
418
|
export function formatHumanDisplayScope(route: BncrRoute): string {
|
|
432
|
-
const platform =
|
|
433
|
-
const groupId =
|
|
434
|
-
const userId =
|
|
419
|
+
const platform = asString(route?.platform).trim();
|
|
420
|
+
const groupId = asString(route?.groupId || '0').trim() || '0';
|
|
421
|
+
const userId = asString(route?.userId || '0').trim() || '0';
|
|
435
422
|
if (groupId !== '0') return `Bncr:${platform}:Group:${groupId}`;
|
|
436
423
|
if (userId !== '0') return `Bncr:${platform}:User:${userId}`;
|
|
437
424
|
return formatDisplayScope(route);
|
|
438
425
|
}
|
|
439
426
|
|
|
440
|
-
|
|
441
|
-
const candidates = [formatDisplayScope(route)].filter(Boolean);
|
|
442
|
-
return Array.from(new Set(candidates.map((x) => asTargetString(x).trim()).filter(Boolean)));
|
|
443
|
-
}
|
|
444
|
-
|
|
445
|
-
export function resolveExplicitTargetRoute(raw: string): {
|
|
427
|
+
function resolveExplicitTargetRoute(raw: string): {
|
|
446
428
|
route: BncrRoute | null;
|
|
447
429
|
source: BncrExplicitTargetSource | null;
|
|
448
430
|
} {
|
|
@@ -491,10 +473,10 @@ export function resolveExplicitTargetRoute(raw: string): {
|
|
|
491
473
|
}
|
|
492
474
|
|
|
493
475
|
export function parseExplicitTarget(input: string, options?: { canonicalAgentId?: string | null }) {
|
|
494
|
-
const raw =
|
|
476
|
+
const raw = asString(input).trim();
|
|
495
477
|
if (!raw) return null;
|
|
496
478
|
|
|
497
|
-
const canonicalAgentId =
|
|
479
|
+
const canonicalAgentId = asString(options?.canonicalAgentId).trim() || undefined;
|
|
498
480
|
const { route, source } = resolveExplicitTargetRoute(raw);
|
|
499
481
|
if (!route || !source) return null;
|
|
500
482
|
|
|
@@ -510,8 +492,8 @@ export function parseExplicitTarget(input: string, options?: { canonicalAgentId?
|
|
|
510
492
|
});
|
|
511
493
|
}
|
|
512
494
|
|
|
513
|
-
|
|
514
|
-
const raw =
|
|
495
|
+
function normalizeTaskKey(input: unknown): string | null {
|
|
496
|
+
const raw = asString(input).trim().toLowerCase();
|
|
515
497
|
if (!raw) return null;
|
|
516
498
|
const normalized = raw
|
|
517
499
|
.replace(/[^a-z0-9_-]+/g, '-')
|
|
@@ -521,7 +503,7 @@ export function normalizeTaskKey(input: unknown): string | null {
|
|
|
521
503
|
}
|
|
522
504
|
|
|
523
505
|
export function extractInlineTaskKey(text: string): { taskKey: string | null; text: string } {
|
|
524
|
-
const raw =
|
|
506
|
+
const raw = asString(text);
|
|
525
507
|
if (!raw) return { taskKey: null, text: '' };
|
|
526
508
|
|
|
527
509
|
const tagged = raw.match(
|
|
@@ -530,7 +512,7 @@ export function extractInlineTaskKey(text: string): { taskKey: string | null; te
|
|
|
530
512
|
if (tagged) {
|
|
531
513
|
return {
|
|
532
514
|
taskKey: normalizeTaskKey(tagged[1]),
|
|
533
|
-
text:
|
|
515
|
+
text: asString(tagged[2]),
|
|
534
516
|
};
|
|
535
517
|
}
|
|
536
518
|
|
|
@@ -538,7 +520,7 @@ export function extractInlineTaskKey(text: string): { taskKey: string | null; te
|
|
|
538
520
|
if (spaced) {
|
|
539
521
|
return {
|
|
540
522
|
taskKey: normalizeTaskKey(spaced[1]),
|
|
541
|
-
text:
|
|
523
|
+
text: asString(spaced[2]),
|
|
542
524
|
};
|
|
543
525
|
}
|
|
544
526
|
|
|
@@ -546,7 +528,7 @@ export function extractInlineTaskKey(text: string): { taskKey: string | null; te
|
|
|
546
528
|
}
|
|
547
529
|
|
|
548
530
|
export function withTaskSessionKey(sessionKey: string, taskKey?: string | null): string {
|
|
549
|
-
const base =
|
|
531
|
+
const base = asString(sessionKey).trim();
|
|
550
532
|
const tk = normalizeTaskKey(taskKey);
|
|
551
533
|
if (!base || !tk) return base;
|
|
552
534
|
if (/:task:[a-z0-9_-]+(?:$|:)/i.test(base)) return base;
|