@openclaw/matrix 2026.3.13 → 2026.5.10-beta.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.
- package/dist/account-config-D2W-V1eQ.js +96 -0
- package/dist/account-selection-BWwIruri.js +158 -0
- package/dist/accounts-Bm90Rzvp.js +130 -0
- package/dist/active-client-uhlxdhEy.js +20 -0
- package/dist/allowlist-sTzpCn5d.js +68 -0
- package/dist/api.js +12 -0
- package/dist/approval-handler.runtime-DWTQfd4m.js +370 -0
- package/dist/approval-ids-DoC2z7tR.js +7 -0
- package/dist/approval-reaction-auth-DbcA1gGd.js +27 -0
- package/dist/approval-reactions-o2_tuH8D.js +162 -0
- package/dist/async-lock-uQfhfQIY.js +19 -0
- package/dist/auth-presence.js +26 -0
- package/dist/backup-health-Cabu_WQC.js +60 -0
- package/dist/channel-H_6lMgwf.js +1116 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-BnO9f0pR.js +246 -0
- package/dist/cli-CYZ9yVcB.js +1340 -0
- package/dist/cli-metadata-DPIHnoa6.js +22 -0
- package/dist/cli-metadata.js +2 -0
- package/dist/client-DkcXnm0X.js +25 -0
- package/dist/client-_hckQNGW.js +31 -0
- package/dist/client-bootstrap-Rb8oHvhH.js +114 -0
- package/dist/config--5-S2Akv.js +452 -0
- package/dist/config-paths-nsVaysCu.js +19 -0
- package/dist/config-schema-nPLpEgHl.js +200 -0
- package/dist/config-secret-input.runtime-DiKFehsE.js +2 -0
- package/dist/config-update-wZX-HLMn.js +143 -0
- package/dist/contract-api.js +9 -0
- package/dist/create-client-DCnqDaqd.js +64 -0
- package/dist/credentials-DV6fWXhC.js +56 -0
- package/dist/credentials-read-cmHgousK.js +112 -0
- package/dist/credentials-write.runtime-zniTq-Gr.js +17 -0
- package/dist/crypto-node.runtime-pihzdpY7.js +12 -0
- package/dist/crypto-runtime-ZI0zAtn3.js +1214 -0
- package/dist/deps-C6WqKY7m.js +235 -0
- package/dist/device-health-UVYpbA_W.js +16 -0
- package/dist/direct-management-DMMMgtTB.js +249 -0
- package/dist/direct-room-XkutHjES.js +76 -0
- package/dist/directory-live-DmOtMhyr.js +150 -0
- package/dist/doctor-C4__7c-U.js +153 -0
- package/dist/doctor-contract-D4-64QuJ.js +246 -0
- package/dist/doctor-contract-api.js +2 -0
- package/dist/draft-stream-BE2QevQQ.js +144 -0
- package/dist/encryption-guidance-BPi3A_m3.js +15 -0
- package/dist/env-auth-BJqGI8M6.js +63 -0
- package/dist/env-vars-C7uQCTKn.js +63 -0
- package/dist/errors-CTcpEDq-.js +17 -0
- package/dist/exec-approval-resolver-Bza9Dhlm.js +15 -0
- package/dist/exec-approvals-Crnh543m.js +196 -0
- package/dist/helper-api.js +4 -0
- package/dist/http-client-C7AeVJay.js +319 -0
- package/dist/index.js +46 -0
- package/dist/legacy-crypto-inspector-poDWldgy.js +41 -0
- package/dist/legacy-crypto-restore-Biw-w2ng.js +85 -0
- package/dist/logger-CnZRVrux.js +78 -0
- package/dist/logging-DZHSPP5N.js +99 -0
- package/dist/matrix-migration.runtime-WY6ffcrf.js +525 -0
- package/dist/media-text-DU6nWZuj.js +146 -0
- package/dist/messages-BpihMh82.js +140 -0
- package/dist/migration-snapshot-backup-DaCHTp8C.js +69 -0
- package/dist/migration-snapshot.runtime-CKHE3xF9.js +2 -0
- package/dist/monitor-BaRCKyLd.js +4175 -0
- package/dist/plugin-entry.handlers.runtime.js +51 -0
- package/dist/probe.runtime-BvAzYAIe.js +3 -0
- package/dist/profile-BlHu0wDX.js +111 -0
- package/dist/profile-update-DjeBNgIV.js +69 -0
- package/dist/reaction-common-ejrL19w-.js +71 -0
- package/dist/reaction-events-CiARZfjk.js +121 -0
- package/dist/record-shared-CHWJCTWf.js +2 -0
- package/dist/recovery-key-store-BTJ6jz5v.js +294 -0
- package/dist/resolve-targets-YtJnw1Tb.js +140 -0
- package/dist/resolver.runtime-D9piiGEl.js +5 -0
- package/dist/rolldown-runtime-DUslC3ob.js +14 -0
- package/dist/route-D6rg-iXN.js +161 -0
- package/dist/runtime-C6X4h_SJ.js +6 -0
- package/dist/runtime-Dog86njy.js +8 -0
- package/dist/runtime-api-DTKcXOhp.js +24 -0
- package/dist/runtime-api.js +25 -0
- package/dist/runtime-heavy-api.js +3 -0
- package/dist/runtime-setter-api.js +2 -0
- package/dist/sdk-B2vZA27-.js +1416 -0
- package/dist/secret-contract-DcrJWCQI.js +120 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/send-Bo0DU1ca.js +1200 -0
- package/dist/session-store-metadata-DQXjgNLt.js +77 -0
- package/dist/setup-bootstrap-ImenBsMt.js +62 -0
- package/dist/setup-core-CfZy05oW.js +116 -0
- package/dist/setup-dm-policy-2-r1FrQh.js +194 -0
- package/dist/setup-entry.js +19 -0
- package/dist/setup-plugin-api.js +44 -0
- package/dist/setup-surface-CqT_o61M.js +540 -0
- package/dist/shared-CpMoYKm1.js +195 -0
- package/dist/startup-abort-56edvmbM.js +32 -0
- package/dist/startup-verification-Demyp0bP.js +132 -0
- package/dist/storage-paths-BJLdnCjV.js +52 -0
- package/dist/storage-tC3ujLiW.js +281 -0
- package/dist/subagent-hooks-DQbyqq9V.js +149 -0
- package/dist/subagent-hooks-api.js +23 -0
- package/dist/sync-state-C_beeevA.js +12 -0
- package/dist/target-ids-80nQ2gql.js +77 -0
- package/dist/test-api.js +4 -0
- package/dist/thread-binding-api-Cq_E-E1K.js +17 -0
- package/dist/thread-binding-api.js +2 -0
- package/dist/thread-bindings-B9mesxXk.js +352 -0
- package/dist/thread-bindings-runtime.js +2 -0
- package/dist/thread-bindings-shared-DK-d-oYX.js +97 -0
- package/dist/timeout-abort-signal-CtaIaP1v.js +2 -0
- package/dist/tool-actions.runtime-ThYhfHtZ.js +532 -0
- package/dist/url-validation-DiK9j7jz.js +36 -0
- package/dist/verification-CZ2rDeHL.js +345 -0
- package/openclaw.plugin.json +796 -1
- package/package.json +82 -16
- package/CHANGELOG.md +0 -104
- package/index.ts +0 -22
- package/src/actions.ts +0 -195
- package/src/channel.directory.test.ts +0 -135
- package/src/channel.ts +0 -461
- package/src/config-schema.test.ts +0 -26
- package/src/config-schema.ts +0 -62
- package/src/directory-live.test.ts +0 -85
- package/src/directory-live.ts +0 -209
- package/src/group-mentions.ts +0 -52
- package/src/matrix/accounts.test.ts +0 -131
- package/src/matrix/accounts.ts +0 -114
- package/src/matrix/actions/client.ts +0 -47
- package/src/matrix/actions/limits.test.ts +0 -15
- package/src/matrix/actions/limits.ts +0 -6
- package/src/matrix/actions/messages.ts +0 -126
- package/src/matrix/actions/pins.test.ts +0 -74
- package/src/matrix/actions/pins.ts +0 -84
- package/src/matrix/actions/reactions.test.ts +0 -109
- package/src/matrix/actions/reactions.ts +0 -102
- package/src/matrix/actions/room.ts +0 -85
- package/src/matrix/actions/summary.ts +0 -75
- package/src/matrix/actions/types.ts +0 -85
- package/src/matrix/actions.ts +0 -15
- package/src/matrix/active-client.ts +0 -32
- package/src/matrix/client/config.ts +0 -245
- package/src/matrix/client/create-client.ts +0 -125
- package/src/matrix/client/logging.ts +0 -46
- package/src/matrix/client/runtime.ts +0 -4
- package/src/matrix/client/shared.test.ts +0 -85
- package/src/matrix/client/shared.ts +0 -210
- package/src/matrix/client/startup.test.ts +0 -49
- package/src/matrix/client/startup.ts +0 -29
- package/src/matrix/client/storage.ts +0 -131
- package/src/matrix/client/types.ts +0 -34
- package/src/matrix/client-bootstrap.ts +0 -47
- package/src/matrix/client.test.ts +0 -56
- package/src/matrix/client.ts +0 -14
- package/src/matrix/credentials.ts +0 -125
- package/src/matrix/deps.test.ts +0 -74
- package/src/matrix/deps.ts +0 -126
- package/src/matrix/format.test.ts +0 -33
- package/src/matrix/format.ts +0 -22
- package/src/matrix/index.ts +0 -11
- package/src/matrix/monitor/access-policy.ts +0 -126
- package/src/matrix/monitor/allowlist.test.ts +0 -45
- package/src/matrix/monitor/allowlist.ts +0 -94
- package/src/matrix/monitor/auto-join.ts +0 -72
- package/src/matrix/monitor/direct.test.ts +0 -396
- package/src/matrix/monitor/direct.ts +0 -152
- package/src/matrix/monitor/events.test.ts +0 -186
- package/src/matrix/monitor/events.ts +0 -168
- package/src/matrix/monitor/handler.body-for-agent.test.ts +0 -196
- package/src/matrix/monitor/handler.ts +0 -768
- package/src/matrix/monitor/inbound-body.test.ts +0 -73
- package/src/matrix/monitor/inbound-body.ts +0 -28
- package/src/matrix/monitor/index.test.ts +0 -18
- package/src/matrix/monitor/index.ts +0 -414
- package/src/matrix/monitor/location.ts +0 -100
- package/src/matrix/monitor/media.test.ts +0 -86
- package/src/matrix/monitor/media.ts +0 -118
- package/src/matrix/monitor/mentions.test.ts +0 -154
- package/src/matrix/monitor/mentions.ts +0 -62
- package/src/matrix/monitor/replies.test.ts +0 -184
- package/src/matrix/monitor/replies.ts +0 -124
- package/src/matrix/monitor/room-info.ts +0 -55
- package/src/matrix/monitor/rooms.test.ts +0 -124
- package/src/matrix/monitor/rooms.ts +0 -47
- package/src/matrix/monitor/threads.ts +0 -68
- package/src/matrix/monitor/types.ts +0 -39
- package/src/matrix/poll-types.test.ts +0 -21
- package/src/matrix/poll-types.ts +0 -167
- package/src/matrix/probe.ts +0 -69
- package/src/matrix/sdk-runtime.ts +0 -18
- package/src/matrix/send/client.ts +0 -99
- package/src/matrix/send/formatting.ts +0 -93
- package/src/matrix/send/media.ts +0 -230
- package/src/matrix/send/targets.test.ts +0 -98
- package/src/matrix/send/targets.ts +0 -150
- package/src/matrix/send/types.ts +0 -110
- package/src/matrix/send-queue.test.ts +0 -145
- package/src/matrix/send-queue.ts +0 -28
- package/src/matrix/send.test.ts +0 -319
- package/src/matrix/send.ts +0 -267
- package/src/onboarding.ts +0 -462
- package/src/outbound.test.ts +0 -159
- package/src/outbound.ts +0 -58
- package/src/resolve-targets.test.ts +0 -68
- package/src/resolve-targets.ts +0 -125
- package/src/runtime.ts +0 -6
- package/src/secret-input.ts +0 -13
- package/src/test-mocks.ts +0 -53
- package/src/tool-actions.ts +0 -164
- package/src/types.ts +0 -118
|
@@ -0,0 +1,352 @@
|
|
|
1
|
+
import { a as listBindingsForAccount, c as resolveBindingKey, f as setMatrixThreadBindingManagerEntry, h as toSessionBindingRecord, l as resolveEffectiveBindingExpiry, m as toMatrixBindingTargetKind, o as removeBindingRecord, r as getMatrixThreadBindingManagerEntry, t as deleteMatrixThreadBindingManagerEntry, u as setBindingRecord } from "./thread-bindings-shared-DK-d-oYX.js";
|
|
2
|
+
import { i as resolveMatrixStateFilePath, t as claimCurrentTokenStorageState } from "./storage-tC3ujLiW.js";
|
|
3
|
+
import { a as sendMessageMatrix } from "./send-Bo0DU1ca.js";
|
|
4
|
+
import { normalizeOptionalString } from "openclaw/plugin-sdk/string-coerce-runtime";
|
|
5
|
+
import { registerSessionBindingAdapter, resolveThreadBindingFarewellText, unregisterSessionBindingAdapter } from "openclaw/plugin-sdk/thread-bindings-session-runtime";
|
|
6
|
+
import path from "node:path";
|
|
7
|
+
import { readJsonFileWithFallback, writeJsonFileAtomically } from "openclaw/plugin-sdk/json-store";
|
|
8
|
+
import { resolveAgentIdFromSessionKey } from "openclaw/plugin-sdk/session-key-runtime";
|
|
9
|
+
//#region extensions/matrix/src/matrix/thread-bindings.ts
|
|
10
|
+
const STORE_VERSION = 1;
|
|
11
|
+
const THREAD_BINDINGS_SWEEP_INTERVAL_MS = 6e4;
|
|
12
|
+
const TOUCH_PERSIST_DELAY_MS = 3e4;
|
|
13
|
+
function resolveBindingsPath(params) {
|
|
14
|
+
return resolveMatrixStateFilePath({
|
|
15
|
+
auth: params.auth,
|
|
16
|
+
accountId: params.accountId,
|
|
17
|
+
env: params.env,
|
|
18
|
+
stateDir: params.stateDir,
|
|
19
|
+
filename: "thread-bindings.json"
|
|
20
|
+
});
|
|
21
|
+
}
|
|
22
|
+
async function loadBindingsFromDisk(filePath, accountId) {
|
|
23
|
+
const { value } = await readJsonFileWithFallback(filePath, null);
|
|
24
|
+
if (value?.version !== STORE_VERSION || !Array.isArray(value.bindings)) return [];
|
|
25
|
+
const loaded = [];
|
|
26
|
+
for (const entry of value.bindings) {
|
|
27
|
+
const conversationId = normalizeOptionalString(entry?.conversationId);
|
|
28
|
+
const parentConversationId = normalizeOptionalString(entry?.parentConversationId);
|
|
29
|
+
const targetSessionKey = normalizeOptionalString(entry?.targetSessionKey) ?? "";
|
|
30
|
+
if (!conversationId || !targetSessionKey) continue;
|
|
31
|
+
const boundAt = typeof entry?.boundAt === "number" && Number.isFinite(entry.boundAt) ? Math.floor(entry.boundAt) : Date.now();
|
|
32
|
+
const lastActivityAt = typeof entry?.lastActivityAt === "number" && Number.isFinite(entry.lastActivityAt) ? Math.floor(entry.lastActivityAt) : boundAt;
|
|
33
|
+
loaded.push({
|
|
34
|
+
accountId,
|
|
35
|
+
conversationId,
|
|
36
|
+
...parentConversationId ? { parentConversationId } : {},
|
|
37
|
+
targetKind: entry?.targetKind === "subagent" ? "subagent" : "acp",
|
|
38
|
+
targetSessionKey,
|
|
39
|
+
agentId: normalizeOptionalString(entry?.agentId) || void 0,
|
|
40
|
+
label: normalizeOptionalString(entry?.label) || void 0,
|
|
41
|
+
boundBy: normalizeOptionalString(entry?.boundBy) || void 0,
|
|
42
|
+
boundAt,
|
|
43
|
+
lastActivityAt: Math.max(lastActivityAt, boundAt),
|
|
44
|
+
idleTimeoutMs: typeof entry?.idleTimeoutMs === "number" && Number.isFinite(entry.idleTimeoutMs) ? Math.max(0, Math.floor(entry.idleTimeoutMs)) : void 0,
|
|
45
|
+
maxAgeMs: typeof entry?.maxAgeMs === "number" && Number.isFinite(entry.maxAgeMs) ? Math.max(0, Math.floor(entry.maxAgeMs)) : void 0
|
|
46
|
+
});
|
|
47
|
+
}
|
|
48
|
+
return loaded;
|
|
49
|
+
}
|
|
50
|
+
function toStoredBindingsState(bindings) {
|
|
51
|
+
return {
|
|
52
|
+
version: STORE_VERSION,
|
|
53
|
+
bindings: [...bindings].toSorted((a, b) => a.boundAt - b.boundAt)
|
|
54
|
+
};
|
|
55
|
+
}
|
|
56
|
+
async function persistBindingsSnapshot(filePath, bindings) {
|
|
57
|
+
await writeJsonFileAtomically(filePath, toStoredBindingsState(bindings));
|
|
58
|
+
claimCurrentTokenStorageState({ rootDir: path.dirname(filePath) });
|
|
59
|
+
}
|
|
60
|
+
function buildMatrixBindingIntroText(params) {
|
|
61
|
+
const introText = normalizeOptionalString(params.metadata?.introText);
|
|
62
|
+
if (introText) return introText;
|
|
63
|
+
const label = normalizeOptionalString(params.metadata?.label);
|
|
64
|
+
const agentId = normalizeOptionalString(params.metadata?.agentId) || resolveAgentIdFromSessionKey(params.targetSessionKey);
|
|
65
|
+
return `⚙️ ${label || agentId || "session"} session active. Messages here go directly to this session.`;
|
|
66
|
+
}
|
|
67
|
+
async function sendBindingMessage(params) {
|
|
68
|
+
const trimmed = params.text.trim();
|
|
69
|
+
if (!trimmed) return null;
|
|
70
|
+
return (await sendMessageMatrix(`room:${params.roomId}`, trimmed, {
|
|
71
|
+
cfg: params.cfg,
|
|
72
|
+
client: params.client,
|
|
73
|
+
accountId: params.accountId,
|
|
74
|
+
...params.threadId ? { threadId: params.threadId } : {}
|
|
75
|
+
})).messageId || null;
|
|
76
|
+
}
|
|
77
|
+
async function sendFarewellMessage(params) {
|
|
78
|
+
const roomId = params.record.parentConversationId ?? params.record.conversationId;
|
|
79
|
+
const idleTimeoutMs = typeof params.record.idleTimeoutMs === "number" ? params.record.idleTimeoutMs : params.defaultIdleTimeoutMs;
|
|
80
|
+
const maxAgeMs = typeof params.record.maxAgeMs === "number" ? params.record.maxAgeMs : params.defaultMaxAgeMs;
|
|
81
|
+
const farewellText = resolveThreadBindingFarewellText({
|
|
82
|
+
reason: params.reason,
|
|
83
|
+
idleTimeoutMs,
|
|
84
|
+
maxAgeMs
|
|
85
|
+
});
|
|
86
|
+
await sendBindingMessage({
|
|
87
|
+
cfg: params.cfg,
|
|
88
|
+
client: params.client,
|
|
89
|
+
accountId: params.accountId,
|
|
90
|
+
roomId,
|
|
91
|
+
threadId: params.record.parentConversationId && params.record.parentConversationId !== params.record.conversationId ? params.record.conversationId : void 0,
|
|
92
|
+
text: farewellText
|
|
93
|
+
}).catch(() => {});
|
|
94
|
+
}
|
|
95
|
+
async function createMatrixThreadBindingManager(params) {
|
|
96
|
+
if (params.auth.accountId !== params.accountId) throw new Error(`Matrix thread binding account mismatch: requested ${params.accountId}, auth resolved ${params.auth.accountId}`);
|
|
97
|
+
const filePath = resolveBindingsPath({
|
|
98
|
+
auth: params.auth,
|
|
99
|
+
accountId: params.accountId,
|
|
100
|
+
env: params.env,
|
|
101
|
+
stateDir: params.stateDir
|
|
102
|
+
});
|
|
103
|
+
const existingEntry = getMatrixThreadBindingManagerEntry(params.accountId);
|
|
104
|
+
if (existingEntry) {
|
|
105
|
+
if (existingEntry.filePath === filePath) return existingEntry.manager;
|
|
106
|
+
existingEntry.manager.stop();
|
|
107
|
+
}
|
|
108
|
+
const loaded = await loadBindingsFromDisk(filePath, params.accountId);
|
|
109
|
+
for (const record of loaded) setBindingRecord(record);
|
|
110
|
+
let persistQueue = Promise.resolve();
|
|
111
|
+
const enqueuePersist = (bindings) => {
|
|
112
|
+
const snapshot = bindings ?? listBindingsForAccount(params.accountId);
|
|
113
|
+
const next = persistQueue.catch(() => {}).then(async () => {
|
|
114
|
+
await persistBindingsSnapshot(filePath, snapshot);
|
|
115
|
+
});
|
|
116
|
+
persistQueue = next;
|
|
117
|
+
return next;
|
|
118
|
+
};
|
|
119
|
+
const persist = async () => await enqueuePersist();
|
|
120
|
+
const persistSafely = (reason, bindings) => {
|
|
121
|
+
enqueuePersist(bindings).catch((err) => {
|
|
122
|
+
params.logVerboseMessage?.(`matrix: failed persisting thread bindings account=${params.accountId} action=${reason}: ${String(err)}`);
|
|
123
|
+
});
|
|
124
|
+
};
|
|
125
|
+
const defaults = {
|
|
126
|
+
idleTimeoutMs: params.idleTimeoutMs,
|
|
127
|
+
maxAgeMs: params.maxAgeMs
|
|
128
|
+
};
|
|
129
|
+
let persistTimer = null;
|
|
130
|
+
const schedulePersist = (delayMs) => {
|
|
131
|
+
if (persistTimer) return;
|
|
132
|
+
persistTimer = setTimeout(() => {
|
|
133
|
+
persistTimer = null;
|
|
134
|
+
persistSafely("delayed-touch");
|
|
135
|
+
}, delayMs);
|
|
136
|
+
persistTimer.unref?.();
|
|
137
|
+
};
|
|
138
|
+
const updateBindingsBySessionKey = (input) => {
|
|
139
|
+
const targetSessionKey = input.targetSessionKey.trim();
|
|
140
|
+
if (!targetSessionKey) return [];
|
|
141
|
+
const now = Date.now();
|
|
142
|
+
const nextBindings = listBindingsForAccount(params.accountId).filter((entry) => entry.targetSessionKey === targetSessionKey).map((entry) => input.update(entry, now));
|
|
143
|
+
if (nextBindings.length === 0) return [];
|
|
144
|
+
for (const entry of nextBindings) setBindingRecord(entry);
|
|
145
|
+
persistSafely(input.persistReason);
|
|
146
|
+
return nextBindings;
|
|
147
|
+
};
|
|
148
|
+
const manager = {
|
|
149
|
+
accountId: params.accountId,
|
|
150
|
+
getIdleTimeoutMs: () => defaults.idleTimeoutMs,
|
|
151
|
+
getMaxAgeMs: () => defaults.maxAgeMs,
|
|
152
|
+
persist,
|
|
153
|
+
getByConversation: ({ conversationId, parentConversationId }) => listBindingsForAccount(params.accountId).find((entry) => {
|
|
154
|
+
if (entry.conversationId !== conversationId.trim()) return false;
|
|
155
|
+
if (!parentConversationId) return true;
|
|
156
|
+
return (entry.parentConversationId ?? "") === parentConversationId.trim();
|
|
157
|
+
}),
|
|
158
|
+
listBySessionKey: (targetSessionKey) => listBindingsForAccount(params.accountId).filter((entry) => entry.targetSessionKey === targetSessionKey.trim()),
|
|
159
|
+
listBindings: () => listBindingsForAccount(params.accountId),
|
|
160
|
+
touchBinding: (bindingId, at) => {
|
|
161
|
+
const record = listBindingsForAccount(params.accountId).find((entry) => resolveBindingKey(entry) === bindingId.trim());
|
|
162
|
+
if (!record) return null;
|
|
163
|
+
const nextRecord = {
|
|
164
|
+
...record,
|
|
165
|
+
lastActivityAt: typeof at === "number" && Number.isFinite(at) ? Math.max(record.lastActivityAt, Math.floor(at)) : Date.now()
|
|
166
|
+
};
|
|
167
|
+
setBindingRecord(nextRecord);
|
|
168
|
+
schedulePersist(TOUCH_PERSIST_DELAY_MS);
|
|
169
|
+
return nextRecord;
|
|
170
|
+
},
|
|
171
|
+
setIdleTimeoutBySessionKey: ({ targetSessionKey, idleTimeoutMs }) => {
|
|
172
|
+
return updateBindingsBySessionKey({
|
|
173
|
+
targetSessionKey,
|
|
174
|
+
persistReason: "idle-timeout-update",
|
|
175
|
+
update: (entry, now) => ({
|
|
176
|
+
...entry,
|
|
177
|
+
idleTimeoutMs: Math.max(0, Math.floor(idleTimeoutMs)),
|
|
178
|
+
lastActivityAt: now
|
|
179
|
+
})
|
|
180
|
+
});
|
|
181
|
+
},
|
|
182
|
+
setMaxAgeBySessionKey: ({ targetSessionKey, maxAgeMs }) => {
|
|
183
|
+
return updateBindingsBySessionKey({
|
|
184
|
+
targetSessionKey,
|
|
185
|
+
persistReason: "max-age-update",
|
|
186
|
+
update: (entry, now) => ({
|
|
187
|
+
...entry,
|
|
188
|
+
maxAgeMs: Math.max(0, Math.floor(maxAgeMs)),
|
|
189
|
+
lastActivityAt: now
|
|
190
|
+
})
|
|
191
|
+
});
|
|
192
|
+
},
|
|
193
|
+
stop: () => {
|
|
194
|
+
if (sweepTimer) clearInterval(sweepTimer);
|
|
195
|
+
if (persistTimer) {
|
|
196
|
+
clearTimeout(persistTimer);
|
|
197
|
+
persistTimer = null;
|
|
198
|
+
persistSafely("shutdown-flush");
|
|
199
|
+
}
|
|
200
|
+
unregisterSessionBindingAdapter({
|
|
201
|
+
channel: "matrix",
|
|
202
|
+
accountId: params.accountId,
|
|
203
|
+
adapter: sessionBindingAdapter
|
|
204
|
+
});
|
|
205
|
+
if (getMatrixThreadBindingManagerEntry(params.accountId)?.manager === manager) deleteMatrixThreadBindingManagerEntry(params.accountId);
|
|
206
|
+
for (const record of listBindingsForAccount(params.accountId)) removeBindingRecord(record);
|
|
207
|
+
}
|
|
208
|
+
};
|
|
209
|
+
let sweepTimer = null;
|
|
210
|
+
const removeRecords = (records) => {
|
|
211
|
+
if (records.length === 0) return [];
|
|
212
|
+
return records.map((record) => removeBindingRecord(record)).filter((record) => Boolean(record));
|
|
213
|
+
};
|
|
214
|
+
const sendFarewellMessages = async (removed, reason) => {
|
|
215
|
+
await Promise.all(removed.map(async (record) => {
|
|
216
|
+
await sendFarewellMessage({
|
|
217
|
+
cfg: params.cfg,
|
|
218
|
+
client: params.client,
|
|
219
|
+
accountId: params.accountId,
|
|
220
|
+
record,
|
|
221
|
+
defaultIdleTimeoutMs: defaults.idleTimeoutMs,
|
|
222
|
+
defaultMaxAgeMs: defaults.maxAgeMs,
|
|
223
|
+
reason: typeof reason === "function" ? reason(record) : reason
|
|
224
|
+
});
|
|
225
|
+
}));
|
|
226
|
+
};
|
|
227
|
+
const unbindRecords = async (records, reason) => {
|
|
228
|
+
const removed = removeRecords(records);
|
|
229
|
+
if (removed.length === 0) return [];
|
|
230
|
+
await persist();
|
|
231
|
+
await sendFarewellMessages(removed, reason);
|
|
232
|
+
return removed.map((record) => toSessionBindingRecord(record, defaults));
|
|
233
|
+
};
|
|
234
|
+
const sessionBindingAdapter = {
|
|
235
|
+
channel: "matrix",
|
|
236
|
+
accountId: params.accountId,
|
|
237
|
+
capabilities: {
|
|
238
|
+
placements: ["current", "child"],
|
|
239
|
+
bindSupported: true,
|
|
240
|
+
unbindSupported: true
|
|
241
|
+
},
|
|
242
|
+
bind: async (input) => {
|
|
243
|
+
const conversationId = input.conversation.conversationId.trim();
|
|
244
|
+
const parentConversationId = normalizeOptionalString(input.conversation.parentConversationId);
|
|
245
|
+
const targetSessionKey = input.targetSessionKey.trim();
|
|
246
|
+
if (!conversationId || !targetSessionKey) return null;
|
|
247
|
+
let boundConversationId = conversationId;
|
|
248
|
+
let boundParentConversationId = parentConversationId;
|
|
249
|
+
const introText = buildMatrixBindingIntroText({
|
|
250
|
+
metadata: input.metadata,
|
|
251
|
+
targetSessionKey
|
|
252
|
+
});
|
|
253
|
+
if (input.placement === "child") {
|
|
254
|
+
const roomId = parentConversationId || conversationId;
|
|
255
|
+
const rootEventId = await sendBindingMessage({
|
|
256
|
+
cfg: params.cfg,
|
|
257
|
+
client: params.client,
|
|
258
|
+
accountId: params.accountId,
|
|
259
|
+
roomId,
|
|
260
|
+
text: introText
|
|
261
|
+
});
|
|
262
|
+
if (!rootEventId) return null;
|
|
263
|
+
boundConversationId = rootEventId;
|
|
264
|
+
boundParentConversationId = roomId;
|
|
265
|
+
}
|
|
266
|
+
const now = Date.now();
|
|
267
|
+
const record = {
|
|
268
|
+
accountId: params.accountId,
|
|
269
|
+
conversationId: boundConversationId,
|
|
270
|
+
...boundParentConversationId ? { parentConversationId: boundParentConversationId } : {},
|
|
271
|
+
targetKind: toMatrixBindingTargetKind(input.targetKind),
|
|
272
|
+
targetSessionKey,
|
|
273
|
+
agentId: normalizeOptionalString(input.metadata?.agentId) || resolveAgentIdFromSessionKey(targetSessionKey),
|
|
274
|
+
label: normalizeOptionalString(input.metadata?.label) || void 0,
|
|
275
|
+
boundBy: normalizeOptionalString(input.metadata?.boundBy) || "system",
|
|
276
|
+
boundAt: now,
|
|
277
|
+
lastActivityAt: now,
|
|
278
|
+
idleTimeoutMs: defaults.idleTimeoutMs,
|
|
279
|
+
maxAgeMs: defaults.maxAgeMs
|
|
280
|
+
};
|
|
281
|
+
setBindingRecord(record);
|
|
282
|
+
await persist();
|
|
283
|
+
if (input.placement === "current" && introText) {
|
|
284
|
+
const roomId = boundParentConversationId || boundConversationId;
|
|
285
|
+
const threadId = boundParentConversationId && boundParentConversationId !== boundConversationId ? boundConversationId : void 0;
|
|
286
|
+
await sendBindingMessage({
|
|
287
|
+
cfg: params.cfg,
|
|
288
|
+
client: params.client,
|
|
289
|
+
accountId: params.accountId,
|
|
290
|
+
roomId,
|
|
291
|
+
threadId,
|
|
292
|
+
text: introText
|
|
293
|
+
}).catch(() => {});
|
|
294
|
+
}
|
|
295
|
+
return toSessionBindingRecord(record, defaults);
|
|
296
|
+
},
|
|
297
|
+
listBySession: (targetSessionKey) => manager.listBySessionKey(targetSessionKey).map((record) => toSessionBindingRecord(record, defaults)),
|
|
298
|
+
resolveByConversation: (ref) => {
|
|
299
|
+
const record = manager.getByConversation({
|
|
300
|
+
conversationId: ref.conversationId,
|
|
301
|
+
parentConversationId: ref.parentConversationId
|
|
302
|
+
});
|
|
303
|
+
return record ? toSessionBindingRecord(record, defaults) : null;
|
|
304
|
+
},
|
|
305
|
+
touch: (bindingId, at) => {
|
|
306
|
+
manager.touchBinding(bindingId, at);
|
|
307
|
+
},
|
|
308
|
+
unbind: async (input) => {
|
|
309
|
+
return await unbindRecords(listBindingsForAccount(params.accountId).filter((record) => {
|
|
310
|
+
if (input.bindingId?.trim()) return resolveBindingKey(record) === input.bindingId.trim();
|
|
311
|
+
if (input.targetSessionKey?.trim()) return record.targetSessionKey === input.targetSessionKey.trim();
|
|
312
|
+
return false;
|
|
313
|
+
}), input.reason);
|
|
314
|
+
}
|
|
315
|
+
};
|
|
316
|
+
registerSessionBindingAdapter(sessionBindingAdapter);
|
|
317
|
+
if (params.enableSweeper !== false) {
|
|
318
|
+
sweepTimer = setInterval(() => {
|
|
319
|
+
const now = Date.now();
|
|
320
|
+
const expired = listBindingsForAccount(params.accountId).map((record) => ({
|
|
321
|
+
record,
|
|
322
|
+
lifecycle: resolveEffectiveBindingExpiry({
|
|
323
|
+
record,
|
|
324
|
+
defaultIdleTimeoutMs: defaults.idleTimeoutMs,
|
|
325
|
+
defaultMaxAgeMs: defaults.maxAgeMs
|
|
326
|
+
})
|
|
327
|
+
})).filter((entry) => typeof entry.lifecycle.expiresAt === "number" && entry.lifecycle.expiresAt <= now && Boolean(entry.lifecycle.reason));
|
|
328
|
+
if (expired.length === 0) return;
|
|
329
|
+
const reasonByBindingKey = new Map(expired.map(({ record, lifecycle }) => [resolveBindingKey(record), lifecycle.reason]));
|
|
330
|
+
(async () => {
|
|
331
|
+
const removed = removeRecords(expired.map(({ record }) => record));
|
|
332
|
+
if (removed.length === 0) return;
|
|
333
|
+
for (const record of removed) {
|
|
334
|
+
const reason = reasonByBindingKey.get(resolveBindingKey(record));
|
|
335
|
+
params.logVerboseMessage?.(`matrix: auto-unbinding ${record.conversationId} due to ${reason}`);
|
|
336
|
+
}
|
|
337
|
+
await persist();
|
|
338
|
+
await sendFarewellMessages(removed, (record) => reasonByBindingKey.get(resolveBindingKey(record)));
|
|
339
|
+
})().catch((err) => {
|
|
340
|
+
params.logVerboseMessage?.(`matrix: failed auto-unbinding expired bindings account=${params.accountId}: ${String(err)}`);
|
|
341
|
+
});
|
|
342
|
+
}, THREAD_BINDINGS_SWEEP_INTERVAL_MS);
|
|
343
|
+
sweepTimer.unref?.();
|
|
344
|
+
}
|
|
345
|
+
setMatrixThreadBindingManagerEntry(params.accountId, {
|
|
346
|
+
filePath,
|
|
347
|
+
manager
|
|
348
|
+
});
|
|
349
|
+
return manager;
|
|
350
|
+
}
|
|
351
|
+
//#endregion
|
|
352
|
+
export { createMatrixThreadBindingManager as t };
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { resolveThreadBindingLifecycle } from "openclaw/plugin-sdk/thread-bindings-session-runtime";
|
|
2
|
+
//#region extensions/matrix/src/matrix/thread-bindings-shared.ts
|
|
3
|
+
const MANAGERS_BY_ACCOUNT_ID = /* @__PURE__ */ new Map();
|
|
4
|
+
const BINDINGS_BY_ACCOUNT_CONVERSATION = /* @__PURE__ */ new Map();
|
|
5
|
+
function resolveBindingKey(params) {
|
|
6
|
+
return `${params.accountId}:${params.parentConversationId?.trim() || "-"}:${params.conversationId}`;
|
|
7
|
+
}
|
|
8
|
+
function toSessionBindingTargetKind(raw) {
|
|
9
|
+
return raw === "subagent" ? "subagent" : "session";
|
|
10
|
+
}
|
|
11
|
+
function toMatrixBindingTargetKind(raw) {
|
|
12
|
+
return raw === "subagent" ? "subagent" : "acp";
|
|
13
|
+
}
|
|
14
|
+
function resolveEffectiveBindingExpiry(params) {
|
|
15
|
+
return resolveThreadBindingLifecycle(params);
|
|
16
|
+
}
|
|
17
|
+
function toSessionBindingRecord(record, defaults) {
|
|
18
|
+
const lifecycle = resolveEffectiveBindingExpiry({
|
|
19
|
+
record,
|
|
20
|
+
defaultIdleTimeoutMs: defaults.idleTimeoutMs,
|
|
21
|
+
defaultMaxAgeMs: defaults.maxAgeMs
|
|
22
|
+
});
|
|
23
|
+
const idleTimeoutMs = typeof record.idleTimeoutMs === "number" ? record.idleTimeoutMs : defaults.idleTimeoutMs;
|
|
24
|
+
const maxAgeMs = typeof record.maxAgeMs === "number" ? record.maxAgeMs : defaults.maxAgeMs;
|
|
25
|
+
return {
|
|
26
|
+
bindingId: resolveBindingKey(record),
|
|
27
|
+
targetSessionKey: record.targetSessionKey,
|
|
28
|
+
targetKind: toSessionBindingTargetKind(record.targetKind),
|
|
29
|
+
conversation: {
|
|
30
|
+
channel: "matrix",
|
|
31
|
+
accountId: record.accountId,
|
|
32
|
+
conversationId: record.conversationId,
|
|
33
|
+
parentConversationId: record.parentConversationId
|
|
34
|
+
},
|
|
35
|
+
status: "active",
|
|
36
|
+
boundAt: record.boundAt,
|
|
37
|
+
expiresAt: lifecycle.expiresAt,
|
|
38
|
+
metadata: {
|
|
39
|
+
agentId: record.agentId,
|
|
40
|
+
label: record.label,
|
|
41
|
+
boundBy: record.boundBy,
|
|
42
|
+
lastActivityAt: record.lastActivityAt,
|
|
43
|
+
idleTimeoutMs,
|
|
44
|
+
maxAgeMs
|
|
45
|
+
}
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
function setBindingRecord(record) {
|
|
49
|
+
BINDINGS_BY_ACCOUNT_CONVERSATION.set(resolveBindingKey(record), record);
|
|
50
|
+
}
|
|
51
|
+
function removeBindingRecord(record) {
|
|
52
|
+
const key = resolveBindingKey(record);
|
|
53
|
+
const removed = BINDINGS_BY_ACCOUNT_CONVERSATION.get(key) ?? null;
|
|
54
|
+
if (removed) BINDINGS_BY_ACCOUNT_CONVERSATION.delete(key);
|
|
55
|
+
return removed;
|
|
56
|
+
}
|
|
57
|
+
function listBindingsForAccount(accountId) {
|
|
58
|
+
return [...BINDINGS_BY_ACCOUNT_CONVERSATION.values()].filter((entry) => entry.accountId === accountId);
|
|
59
|
+
}
|
|
60
|
+
function listAllBindings() {
|
|
61
|
+
return [...BINDINGS_BY_ACCOUNT_CONVERSATION.values()];
|
|
62
|
+
}
|
|
63
|
+
function getMatrixThreadBindingManagerEntry(accountId) {
|
|
64
|
+
return MANAGERS_BY_ACCOUNT_ID.get(accountId) ?? null;
|
|
65
|
+
}
|
|
66
|
+
function setMatrixThreadBindingManagerEntry(accountId, entry) {
|
|
67
|
+
MANAGERS_BY_ACCOUNT_ID.set(accountId, entry);
|
|
68
|
+
}
|
|
69
|
+
function deleteMatrixThreadBindingManagerEntry(accountId) {
|
|
70
|
+
MANAGERS_BY_ACCOUNT_ID.delete(accountId);
|
|
71
|
+
}
|
|
72
|
+
function getMatrixThreadBindingManager(accountId) {
|
|
73
|
+
return MANAGERS_BY_ACCOUNT_ID.get(accountId)?.manager ?? null;
|
|
74
|
+
}
|
|
75
|
+
function setMatrixThreadBindingIdleTimeoutBySessionKey(params) {
|
|
76
|
+
const manager = MANAGERS_BY_ACCOUNT_ID.get(params.accountId)?.manager;
|
|
77
|
+
if (!manager) return [];
|
|
78
|
+
return manager.setIdleTimeoutBySessionKey(params).map((record) => toSessionBindingRecord(record, {
|
|
79
|
+
idleTimeoutMs: manager.getIdleTimeoutMs(),
|
|
80
|
+
maxAgeMs: manager.getMaxAgeMs()
|
|
81
|
+
}));
|
|
82
|
+
}
|
|
83
|
+
function setMatrixThreadBindingMaxAgeBySessionKey(params) {
|
|
84
|
+
const manager = MANAGERS_BY_ACCOUNT_ID.get(params.accountId)?.manager;
|
|
85
|
+
if (!manager) return [];
|
|
86
|
+
return manager.setMaxAgeBySessionKey(params).map((record) => toSessionBindingRecord(record, {
|
|
87
|
+
idleTimeoutMs: manager.getIdleTimeoutMs(),
|
|
88
|
+
maxAgeMs: manager.getMaxAgeMs()
|
|
89
|
+
}));
|
|
90
|
+
}
|
|
91
|
+
function resetMatrixThreadBindingsForTests() {
|
|
92
|
+
for (const { manager } of MANAGERS_BY_ACCOUNT_ID.values()) manager.stop();
|
|
93
|
+
MANAGERS_BY_ACCOUNT_ID.clear();
|
|
94
|
+
BINDINGS_BY_ACCOUNT_CONVERSATION.clear();
|
|
95
|
+
}
|
|
96
|
+
//#endregion
|
|
97
|
+
export { listBindingsForAccount as a, resolveBindingKey as c, setMatrixThreadBindingIdleTimeoutBySessionKey as d, setMatrixThreadBindingManagerEntry as f, toSessionBindingRecord as h, listAllBindings as i, resolveEffectiveBindingExpiry as l, toMatrixBindingTargetKind as m, getMatrixThreadBindingManager as n, removeBindingRecord as o, setMatrixThreadBindingMaxAgeBySessionKey as p, getMatrixThreadBindingManagerEntry as r, resetMatrixThreadBindingsForTests as s, deleteMatrixThreadBindingManagerEntry as t, setBindingRecord as u };
|