@gakr-gakr/matrix 0.1.0
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/CHANGELOG.md +285 -0
- package/SPEC-SUPPORT.md +116 -0
- package/api.ts +38 -0
- package/auth-presence.ts +56 -0
- package/autobot.plugin.json +28 -0
- package/channel-plugin-api.ts +3 -0
- package/cli-metadata.ts +11 -0
- package/contract-api.ts +17 -0
- package/doctor-contract-api.ts +1 -0
- package/helper-api.ts +3 -0
- package/index.ts +55 -0
- package/package.json +101 -0
- package/plugin-entry.handlers.runtime.ts +1 -0
- package/runtime-api.ts +72 -0
- package/runtime-heavy-api.ts +1 -0
- package/runtime-setter-api.ts +3 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +17 -0
- package/setup-plugin-api.ts +3 -0
- package/src/account-selection.ts +223 -0
- package/src/actions.ts +346 -0
- package/src/approval-auth.ts +25 -0
- package/src/approval-handler.runtime.ts +595 -0
- package/src/approval-ids.ts +6 -0
- package/src/approval-native.ts +348 -0
- package/src/approval-reaction-auth.ts +45 -0
- package/src/approval-reactions.ts +313 -0
- package/src/auth-precedence.ts +61 -0
- package/src/channel-account-paths.ts +97 -0
- package/src/channel.runtime.ts +17 -0
- package/src/channel.setup.ts +48 -0
- package/src/channel.ts +667 -0
- package/src/cli-metadata.ts +19 -0
- package/src/cli.ts +2298 -0
- package/src/config-adapter.ts +41 -0
- package/src/config-schema.ts +159 -0
- package/src/config-ui-hints.ts +56 -0
- package/src/directory-live.ts +238 -0
- package/src/doctor-contract.ts +287 -0
- package/src/doctor.ts +262 -0
- package/src/env-vars.ts +92 -0
- package/src/exec-approval-resolver.ts +23 -0
- package/src/exec-approvals.ts +293 -0
- package/src/group-mentions.ts +41 -0
- package/src/legacy-crypto-inspector-availability.ts +60 -0
- package/src/legacy-crypto.ts +531 -0
- package/src/legacy-state.ts +156 -0
- package/src/matrix/account-config.ts +175 -0
- package/src/matrix/accounts.ts +194 -0
- package/src/matrix/actions/client.ts +31 -0
- package/src/matrix/actions/devices.ts +34 -0
- package/src/matrix/actions/limits.ts +6 -0
- package/src/matrix/actions/messages.ts +129 -0
- package/src/matrix/actions/pins.ts +63 -0
- package/src/matrix/actions/polls.ts +109 -0
- package/src/matrix/actions/profile.ts +37 -0
- package/src/matrix/actions/reactions.ts +59 -0
- package/src/matrix/actions/room.ts +71 -0
- package/src/matrix/actions/summary.ts +88 -0
- package/src/matrix/actions/types.ts +63 -0
- package/src/matrix/actions/verification.ts +589 -0
- package/src/matrix/actions.ts +37 -0
- package/src/matrix/active-client.ts +26 -0
- package/src/matrix/async-lock.ts +18 -0
- package/src/matrix/backup-health.ts +124 -0
- package/src/matrix/client/config-runtime-api.ts +9 -0
- package/src/matrix/client/config-secret-input.runtime.ts +1 -0
- package/src/matrix/client/config.ts +853 -0
- package/src/matrix/client/create-client.ts +105 -0
- package/src/matrix/client/env-auth.ts +95 -0
- package/src/matrix/client/file-sync-store.ts +289 -0
- package/src/matrix/client/logging.ts +140 -0
- package/src/matrix/client/migration-snapshot.runtime.ts +1 -0
- package/src/matrix/client/private-network-host.ts +1 -0
- package/src/matrix/client/runtime.ts +4 -0
- package/src/matrix/client/shared.ts +316 -0
- package/src/matrix/client/storage.ts +543 -0
- package/src/matrix/client/types.ts +50 -0
- package/src/matrix/client/url-validation.ts +76 -0
- package/src/matrix/client-bootstrap.ts +173 -0
- package/src/matrix/client.ts +23 -0
- package/src/matrix/config-paths.ts +31 -0
- package/src/matrix/config-update.ts +292 -0
- package/src/matrix/credentials-read.ts +207 -0
- package/src/matrix/credentials-write.runtime.ts +35 -0
- package/src/matrix/credentials.ts +95 -0
- package/src/matrix/deps.ts +309 -0
- package/src/matrix/device-health.ts +31 -0
- package/src/matrix/direct-management.ts +349 -0
- package/src/matrix/direct-room.ts +128 -0
- package/src/matrix/draft-stream.ts +225 -0
- package/src/matrix/encryption-guidance.ts +24 -0
- package/src/matrix/errors.ts +21 -0
- package/src/matrix/format.ts +426 -0
- package/src/matrix/legacy-crypto-inspector.ts +95 -0
- package/src/matrix/media-errors.ts +20 -0
- package/src/matrix/media-text.ts +162 -0
- package/src/matrix/monitor/access-state.ts +145 -0
- package/src/matrix/monitor/ack-config.ts +27 -0
- package/src/matrix/monitor/allowlist.ts +92 -0
- package/src/matrix/monitor/auto-join.ts +86 -0
- package/src/matrix/monitor/config.ts +569 -0
- package/src/matrix/monitor/context-summary.ts +43 -0
- package/src/matrix/monitor/direct.ts +296 -0
- package/src/matrix/monitor/events.ts +397 -0
- package/src/matrix/monitor/handler.ts +2271 -0
- package/src/matrix/monitor/inbound-dedupe.ts +267 -0
- package/src/matrix/monitor/index.ts +540 -0
- package/src/matrix/monitor/legacy-crypto-restore.ts +139 -0
- package/src/matrix/monitor/location.ts +108 -0
- package/src/matrix/monitor/media.ts +119 -0
- package/src/matrix/monitor/mentions.ts +256 -0
- package/src/matrix/monitor/reaction-events.ts +197 -0
- package/src/matrix/monitor/recent-invite.ts +30 -0
- package/src/matrix/monitor/replies.ts +136 -0
- package/src/matrix/monitor/reply-context.ts +92 -0
- package/src/matrix/monitor/room-history.ts +301 -0
- package/src/matrix/monitor/room-info.ts +126 -0
- package/src/matrix/monitor/rooms.ts +52 -0
- package/src/matrix/monitor/route.ts +179 -0
- package/src/matrix/monitor/runtime-api.ts +28 -0
- package/src/matrix/monitor/startup-verification.ts +237 -0
- package/src/matrix/monitor/startup.ts +218 -0
- package/src/matrix/monitor/status.ts +120 -0
- package/src/matrix/monitor/sync-lifecycle.ts +91 -0
- package/src/matrix/monitor/task-runner.ts +38 -0
- package/src/matrix/monitor/test-events.ts +21 -0
- package/src/matrix/monitor/thread-context.ts +108 -0
- package/src/matrix/monitor/threads.ts +85 -0
- package/src/matrix/monitor/types.ts +30 -0
- package/src/matrix/monitor/verification-events.ts +643 -0
- package/src/matrix/monitor/verification-utils.ts +46 -0
- package/src/matrix/outbound-media-runtime.ts +1 -0
- package/src/matrix/poll-summary.ts +110 -0
- package/src/matrix/poll-types.ts +429 -0
- package/src/matrix/probe.runtime.ts +4 -0
- package/src/matrix/probe.ts +97 -0
- package/src/matrix/profile.ts +184 -0
- package/src/matrix/reaction-common.ts +147 -0
- package/src/matrix/sdk/crypto-bootstrap.ts +438 -0
- package/src/matrix/sdk/crypto-facade.ts +242 -0
- package/src/matrix/sdk/crypto-node.runtime.ts +17 -0
- package/src/matrix/sdk/crypto-runtime.ts +14 -0
- package/src/matrix/sdk/decrypt-bridge.ts +410 -0
- package/src/matrix/sdk/event-helpers.ts +83 -0
- package/src/matrix/sdk/http-client.ts +87 -0
- package/src/matrix/sdk/idb-persistence-lock.ts +51 -0
- package/src/matrix/sdk/idb-persistence.ts +286 -0
- package/src/matrix/sdk/logger.ts +108 -0
- package/src/matrix/sdk/read-response-with-limit.ts +19 -0
- package/src/matrix/sdk/recovery-key-store.ts +453 -0
- package/src/matrix/sdk/timeout-abort-signal.ts +1 -0
- package/src/matrix/sdk/transport-runtime-api.ts +18 -0
- package/src/matrix/sdk/transport.ts +352 -0
- package/src/matrix/sdk/types.ts +245 -0
- package/src/matrix/sdk/verification-manager.ts +795 -0
- package/src/matrix/sdk/verification-status.ts +23 -0
- package/src/matrix/sdk.ts +2152 -0
- package/src/matrix/send/client.ts +93 -0
- package/src/matrix/send/formatting.ts +189 -0
- package/src/matrix/send/media.ts +244 -0
- package/src/matrix/send/targets.ts +104 -0
- package/src/matrix/send/types.ts +131 -0
- package/src/matrix/send.ts +660 -0
- package/src/matrix/session-store-metadata.ts +108 -0
- package/src/matrix/startup-abort.ts +44 -0
- package/src/matrix/subagent-hooks.ts +308 -0
- package/src/matrix/sync-state.ts +27 -0
- package/src/matrix/target-ids.ts +79 -0
- package/src/matrix/thread-bindings-shared.ts +206 -0
- package/src/matrix/thread-bindings.ts +580 -0
- package/src/matrix-migration.runtime.ts +9 -0
- package/src/migration-config.ts +243 -0
- package/src/migration-snapshot-backup.ts +116 -0
- package/src/migration-snapshot.ts +53 -0
- package/src/onboarding.ts +775 -0
- package/src/outbound.ts +248 -0
- package/src/plugin-entry.runtime.js +115 -0
- package/src/plugin-entry.runtime.ts +70 -0
- package/src/profile-update.ts +71 -0
- package/src/record-shared.ts +3 -0
- package/src/resolve-targets.ts +175 -0
- package/src/resolver.runtime.ts +5 -0
- package/src/resolver.ts +21 -0
- package/src/runtime-api.ts +106 -0
- package/src/runtime.ts +13 -0
- package/src/secret-contract.ts +174 -0
- package/src/session-route.ts +126 -0
- package/src/setup-bootstrap.ts +102 -0
- package/src/setup-config.ts +222 -0
- package/src/setup-contract.ts +90 -0
- package/src/setup-core.ts +146 -0
- package/src/setup-dm-policy.ts +15 -0
- package/src/setup-surface.ts +4 -0
- package/src/startup-maintenance.ts +114 -0
- package/src/storage-paths.ts +92 -0
- package/src/thread-binding-api.ts +23 -0
- package/src/tool-actions.runtime.ts +1 -0
- package/src/tool-actions.ts +498 -0
- package/src/types.ts +257 -0
- package/subagent-hooks-api.ts +31 -0
- package/test-api.ts +21 -0
- package/thread-binding-api.ts +4 -0
- package/thread-bindings-runtime.ts +4 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,2271 @@
|
|
|
1
|
+
import {
|
|
2
|
+
createPreviewMessageReceipt,
|
|
3
|
+
defineFinalizableLivePreviewAdapter,
|
|
4
|
+
deliverWithFinalizableLivePreviewAdapter,
|
|
5
|
+
type MessageReceipt,
|
|
6
|
+
} from "autobot/plugin-sdk/channel-message";
|
|
7
|
+
import {
|
|
8
|
+
buildChannelProgressDraftLineForEntry,
|
|
9
|
+
createChannelProgressDraftGate,
|
|
10
|
+
type ChannelProgressDraftLine,
|
|
11
|
+
formatChannelProgressDraftLine,
|
|
12
|
+
formatChannelProgressDraftLineForEntry,
|
|
13
|
+
formatChannelProgressDraftText,
|
|
14
|
+
isChannelProgressDraftWorkToolName,
|
|
15
|
+
mergeChannelProgressDraftLine,
|
|
16
|
+
normalizeChannelProgressDraftLineIdentity,
|
|
17
|
+
resolveChannelProgressDraftMaxLines,
|
|
18
|
+
} from "autobot/plugin-sdk/channel-streaming";
|
|
19
|
+
import {
|
|
20
|
+
evaluateSupplementalContextVisibility,
|
|
21
|
+
resolveChannelContextVisibilityMode,
|
|
22
|
+
} from "autobot/plugin-sdk/context-visibility-runtime";
|
|
23
|
+
import { isDangerousNameMatchingEnabled } from "autobot/plugin-sdk/dangerous-name-runtime";
|
|
24
|
+
import { hasFinalInboundReplyDispatch } from "autobot/plugin-sdk/inbound-reply-dispatch";
|
|
25
|
+
import type { ChannelBotLoopProtectionFacts } from "autobot/plugin-sdk/inbound-reply-dispatch";
|
|
26
|
+
import { mergePairLoopGuardConfig } from "autobot/plugin-sdk/pair-loop-guard-runtime";
|
|
27
|
+
import { buildInboundHistoryFromEntries } from "autobot/plugin-sdk/reply-history";
|
|
28
|
+
import {
|
|
29
|
+
buildTtsSupplementMediaPayload,
|
|
30
|
+
getReplyPayloadTtsSupplement,
|
|
31
|
+
} from "autobot/plugin-sdk/reply-payload";
|
|
32
|
+
import type { GetReplyOptions } from "autobot/plugin-sdk/reply-runtime";
|
|
33
|
+
import { resolveInboundLastRouteSessionKey } from "autobot/plugin-sdk/routing";
|
|
34
|
+
import { resolvePinnedMainDmOwnerFromAllowlist } from "autobot/plugin-sdk/security-runtime";
|
|
35
|
+
import {
|
|
36
|
+
loadSessionStore,
|
|
37
|
+
resolveSessionStoreEntry,
|
|
38
|
+
} from "autobot/plugin-sdk/session-store-runtime";
|
|
39
|
+
import { normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
40
|
+
import type {
|
|
41
|
+
CoreConfig,
|
|
42
|
+
MatrixConfig,
|
|
43
|
+
MatrixRoomConfig,
|
|
44
|
+
MatrixStreamingMode,
|
|
45
|
+
ReplyToMode,
|
|
46
|
+
} from "../../types.js";
|
|
47
|
+
import {
|
|
48
|
+
resolveMatrixAccountAllowlistConfig,
|
|
49
|
+
resolveMatrixAccountConfig,
|
|
50
|
+
} from "../account-config.js";
|
|
51
|
+
import { formatMatrixErrorMessage } from "../errors.js";
|
|
52
|
+
import { isMatrixMediaSizeLimitError } from "../media-errors.js";
|
|
53
|
+
import {
|
|
54
|
+
formatMatrixMediaTooLargeText,
|
|
55
|
+
formatMatrixMediaUnavailableText,
|
|
56
|
+
formatMatrixMessageText,
|
|
57
|
+
resolveMatrixMessageAttachment,
|
|
58
|
+
resolveMatrixMessageBody,
|
|
59
|
+
} from "../media-text.js";
|
|
60
|
+
import { fetchMatrixPollSnapshot, type MatrixPollSnapshot } from "../poll-summary.js";
|
|
61
|
+
import {
|
|
62
|
+
formatPollAsText,
|
|
63
|
+
isPollEventType,
|
|
64
|
+
isPollStartType,
|
|
65
|
+
parsePollStartContent,
|
|
66
|
+
} from "../poll-types.js";
|
|
67
|
+
import type { LocationMessageEventContent, MatrixClient } from "../sdk.js";
|
|
68
|
+
import { MATRIX_AUTOBOT_FINALIZED_PREVIEW_KEY } from "../send/types.js";
|
|
69
|
+
import { resolveMatrixStoredSessionMeta } from "../session-store-metadata.js";
|
|
70
|
+
import {
|
|
71
|
+
resolveMatrixMonitorAccessState,
|
|
72
|
+
resolveMatrixMonitorCommandAccess,
|
|
73
|
+
} from "./access-state.js";
|
|
74
|
+
import { resolveMatrixAckReactionConfig } from "./ack-config.js";
|
|
75
|
+
import { normalizeMatrixUserId, resolveMatrixAllowListMatch } from "./allowlist.js";
|
|
76
|
+
import {
|
|
77
|
+
resolveMatrixMonitorLiveUserAllowlist,
|
|
78
|
+
type MatrixResolvedAllowlistEntry,
|
|
79
|
+
} from "./config.js";
|
|
80
|
+
import type { MatrixInboundEventDeduper } from "./inbound-dedupe.js";
|
|
81
|
+
import { resolveMatrixLocation, type MatrixLocationPayload } from "./location.js";
|
|
82
|
+
import { downloadMatrixMedia } from "./media.js";
|
|
83
|
+
import { resolveMentions, stripMatrixMentionPrefix } from "./mentions.js";
|
|
84
|
+
import { deliverMatrixReplies } from "./replies.js";
|
|
85
|
+
import { createMatrixReplyContextResolver } from "./reply-context.js";
|
|
86
|
+
import { createRoomHistoryTracker } from "./room-history.js";
|
|
87
|
+
import type { HistoryEntry } from "./room-history.js";
|
|
88
|
+
import { resolveMatrixRoomConfig } from "./rooms.js";
|
|
89
|
+
import { resolveMatrixInboundRoute } from "./route.js";
|
|
90
|
+
import {
|
|
91
|
+
createReplyPrefixOptions,
|
|
92
|
+
createTypingCallbacks,
|
|
93
|
+
getAgentScopedMediaLocalRoots,
|
|
94
|
+
logInboundDrop,
|
|
95
|
+
logTypingFailure,
|
|
96
|
+
type BlockReplyContext,
|
|
97
|
+
type PluginRuntime,
|
|
98
|
+
type ReplyPayload,
|
|
99
|
+
type RuntimeEnv,
|
|
100
|
+
type RuntimeLogger,
|
|
101
|
+
} from "./runtime-api.js";
|
|
102
|
+
import { createMatrixThreadContextResolver } from "./thread-context.js";
|
|
103
|
+
import {
|
|
104
|
+
resolveMatrixReplyToEventId,
|
|
105
|
+
resolveMatrixThreadRootId,
|
|
106
|
+
resolveMatrixThreadRouting,
|
|
107
|
+
} from "./threads.js";
|
|
108
|
+
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
|
|
109
|
+
import { EventType, RelationType } from "./types.js";
|
|
110
|
+
import { isMatrixVerificationRoomMessage } from "./verification-utils.js";
|
|
111
|
+
|
|
112
|
+
const ALLOW_FROM_STORE_CACHE_TTL_MS = 30_000;
|
|
113
|
+
const PAIRING_REPLY_COOLDOWN_MS = 5 * 60_000;
|
|
114
|
+
const MATRIX_TOOL_PROGRESS_MAX_CHARS = 300;
|
|
115
|
+
let matrixSendModulePromise: Promise<typeof import("../send.js")> | undefined;
|
|
116
|
+
let acpBindingRuntimePromise:
|
|
117
|
+
| Promise<typeof import("autobot/plugin-sdk/acp-binding-runtime")>
|
|
118
|
+
| undefined;
|
|
119
|
+
let sessionBindingRuntimePromise:
|
|
120
|
+
| Promise<typeof import("autobot/plugin-sdk/session-binding-runtime")>
|
|
121
|
+
| undefined;
|
|
122
|
+
let matrixReactionEventsPromise: Promise<typeof import("./reaction-events.js")> | undefined;
|
|
123
|
+
let matrixDraftStreamPromise: Promise<typeof import("../draft-stream.js")> | undefined;
|
|
124
|
+
|
|
125
|
+
function loadMatrixSendModule(): Promise<typeof import("../send.js")> {
|
|
126
|
+
matrixSendModulePromise ??= import("../send.js");
|
|
127
|
+
return matrixSendModulePromise;
|
|
128
|
+
}
|
|
129
|
+
|
|
130
|
+
function loadAcpBindingRuntime(): Promise<
|
|
131
|
+
typeof import("autobot/plugin-sdk/acp-binding-runtime")
|
|
132
|
+
> {
|
|
133
|
+
acpBindingRuntimePromise ??= import("autobot/plugin-sdk/acp-binding-runtime");
|
|
134
|
+
return acpBindingRuntimePromise;
|
|
135
|
+
}
|
|
136
|
+
|
|
137
|
+
function loadSessionBindingRuntime(): Promise<
|
|
138
|
+
typeof import("autobot/plugin-sdk/session-binding-runtime")
|
|
139
|
+
> {
|
|
140
|
+
sessionBindingRuntimePromise ??= import("autobot/plugin-sdk/session-binding-runtime");
|
|
141
|
+
return sessionBindingRuntimePromise;
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
function loadMatrixReactionEvents(): Promise<typeof import("./reaction-events.js")> {
|
|
145
|
+
matrixReactionEventsPromise ??= import("./reaction-events.js");
|
|
146
|
+
return matrixReactionEventsPromise;
|
|
147
|
+
}
|
|
148
|
+
|
|
149
|
+
function loadMatrixDraftStream(): Promise<typeof import("../draft-stream.js")> {
|
|
150
|
+
matrixDraftStreamPromise ??= import("../draft-stream.js");
|
|
151
|
+
return matrixDraftStreamPromise;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
const MAX_TRACKED_PAIRING_REPLY_SENDERS = 512;
|
|
155
|
+
const MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES = 512;
|
|
156
|
+
type MatrixAllowBotsMode = "off" | "mentions" | "all";
|
|
157
|
+
type MatrixDraftStreamHandle = {
|
|
158
|
+
update: (text: string) => void;
|
|
159
|
+
stop: () => Promise<string | undefined>;
|
|
160
|
+
discardPending: () => Promise<void>;
|
|
161
|
+
eventId: () => string | undefined;
|
|
162
|
+
mustDeliverFinalNormally: () => boolean;
|
|
163
|
+
matchesPreparedText: (text: string) => boolean;
|
|
164
|
+
finalizeLive: () => Promise<boolean>;
|
|
165
|
+
reset: () => void;
|
|
166
|
+
};
|
|
167
|
+
|
|
168
|
+
export class MatrixRetryableInboundError extends Error {
|
|
169
|
+
constructor(message: string, options?: ErrorOptions) {
|
|
170
|
+
super(message, options);
|
|
171
|
+
this.name = "MatrixRetryableInboundError";
|
|
172
|
+
}
|
|
173
|
+
}
|
|
174
|
+
|
|
175
|
+
async function redactMatrixDraftEvent(
|
|
176
|
+
client: MatrixClient,
|
|
177
|
+
roomId: string,
|
|
178
|
+
draftEventId: string,
|
|
179
|
+
): Promise<void> {
|
|
180
|
+
await client.redactEvent(roomId, draftEventId).catch(() => {});
|
|
181
|
+
}
|
|
182
|
+
|
|
183
|
+
function buildMatrixFinalizedPreviewContent(): Record<string, unknown> {
|
|
184
|
+
return { [MATRIX_AUTOBOT_FINALIZED_PREVIEW_KEY]: true };
|
|
185
|
+
}
|
|
186
|
+
|
|
187
|
+
export type MatrixMonitorHandlerParams = {
|
|
188
|
+
client: MatrixClient;
|
|
189
|
+
core: PluginRuntime;
|
|
190
|
+
cfg: CoreConfig;
|
|
191
|
+
accountId: string;
|
|
192
|
+
accountConfig?: MatrixConfig;
|
|
193
|
+
runtime: RuntimeEnv;
|
|
194
|
+
logger: RuntimeLogger;
|
|
195
|
+
logVerboseMessage: (message: string) => void;
|
|
196
|
+
allowFrom: string[];
|
|
197
|
+
allowFromResolvedEntries?: readonly MatrixResolvedAllowlistEntry[];
|
|
198
|
+
groupAllowFrom?: string[];
|
|
199
|
+
groupAllowFromResolvedEntries?: readonly MatrixResolvedAllowlistEntry[];
|
|
200
|
+
roomsConfig?: Record<string, MatrixRoomConfig>;
|
|
201
|
+
accountAllowBots?: boolean | "mentions";
|
|
202
|
+
configuredBotUserIds?: ReadonlySet<string>;
|
|
203
|
+
groupPolicy: "open" | "allowlist" | "disabled";
|
|
204
|
+
replyToMode: ReplyToMode;
|
|
205
|
+
threadReplies: "off" | "inbound" | "always";
|
|
206
|
+
/** DM-specific threadReplies override. Falls back to threadReplies when absent. */
|
|
207
|
+
dmThreadReplies?: "off" | "inbound" | "always";
|
|
208
|
+
/** DM session grouping behavior. */
|
|
209
|
+
dmSessionScope?: "per-user" | "per-room";
|
|
210
|
+
streaming: MatrixStreamingMode;
|
|
211
|
+
previewToolProgressEnabled: boolean;
|
|
212
|
+
blockStreamingEnabled: boolean;
|
|
213
|
+
dmEnabled: boolean;
|
|
214
|
+
dmPolicy: "open" | "pairing" | "allowlist" | "disabled";
|
|
215
|
+
textLimit: number;
|
|
216
|
+
mediaMaxBytes: number;
|
|
217
|
+
historyLimit: number;
|
|
218
|
+
startupMs: number;
|
|
219
|
+
startupGraceMs: number;
|
|
220
|
+
dropPreStartupMessages: boolean;
|
|
221
|
+
inboundDeduper?: Pick<MatrixInboundEventDeduper, "claimEvent" | "commitEvent" | "releaseEvent">;
|
|
222
|
+
directTracker: {
|
|
223
|
+
isDirectMessage: (params: {
|
|
224
|
+
roomId: string;
|
|
225
|
+
senderId: string;
|
|
226
|
+
selfUserId: string;
|
|
227
|
+
}) => Promise<boolean>;
|
|
228
|
+
};
|
|
229
|
+
getRoomInfo: (
|
|
230
|
+
roomId: string,
|
|
231
|
+
opts?: { includeAliases?: boolean },
|
|
232
|
+
) => Promise<{ name?: string; canonicalAlias?: string; altAliases: string[] }>;
|
|
233
|
+
getMemberDisplayName: (roomId: string, userId: string) => Promise<string>;
|
|
234
|
+
needsRoomAliasesForConfig: boolean;
|
|
235
|
+
resolveLiveUserAllowlist?: typeof resolveMatrixMonitorLiveUserAllowlist;
|
|
236
|
+
};
|
|
237
|
+
|
|
238
|
+
function resolveMatrixMentionPrecheckText(params: {
|
|
239
|
+
eventType: string;
|
|
240
|
+
content: RoomMessageEventContent;
|
|
241
|
+
locationText?: string | null;
|
|
242
|
+
}): string {
|
|
243
|
+
if (params.locationText?.trim()) {
|
|
244
|
+
return params.locationText.trim();
|
|
245
|
+
}
|
|
246
|
+
if (typeof params.content.body === "string" && params.content.body.trim()) {
|
|
247
|
+
return params.content.body.trim();
|
|
248
|
+
}
|
|
249
|
+
if (isPollStartType(params.eventType)) {
|
|
250
|
+
const parsed = parsePollStartContent(params.content as never);
|
|
251
|
+
if (parsed) {
|
|
252
|
+
return formatPollAsText(parsed);
|
|
253
|
+
}
|
|
254
|
+
}
|
|
255
|
+
return "";
|
|
256
|
+
}
|
|
257
|
+
|
|
258
|
+
function hasBundledMatrixReplacementRelation(event: MatrixRawEvent) {
|
|
259
|
+
const relations = event.unsigned?.["m.relations"];
|
|
260
|
+
if (!relations || typeof relations !== "object") {
|
|
261
|
+
return false;
|
|
262
|
+
}
|
|
263
|
+
return relations[RelationType.Replace] !== undefined;
|
|
264
|
+
}
|
|
265
|
+
|
|
266
|
+
function resolveMatrixInboundBodyText(params: {
|
|
267
|
+
rawBody: string;
|
|
268
|
+
filename?: string;
|
|
269
|
+
mediaPlaceholder?: string;
|
|
270
|
+
msgtype?: string;
|
|
271
|
+
hadMediaUrl: boolean;
|
|
272
|
+
mediaDownloadFailed: boolean;
|
|
273
|
+
mediaSizeLimitExceeded?: boolean;
|
|
274
|
+
}): string {
|
|
275
|
+
if (params.mediaPlaceholder) {
|
|
276
|
+
return params.rawBody || params.mediaPlaceholder;
|
|
277
|
+
}
|
|
278
|
+
if (!params.mediaDownloadFailed || !params.hadMediaUrl) {
|
|
279
|
+
return params.rawBody;
|
|
280
|
+
}
|
|
281
|
+
if (params.mediaSizeLimitExceeded) {
|
|
282
|
+
return formatMatrixMediaTooLargeText({
|
|
283
|
+
body: params.rawBody,
|
|
284
|
+
filename: params.filename,
|
|
285
|
+
msgtype: params.msgtype,
|
|
286
|
+
});
|
|
287
|
+
}
|
|
288
|
+
return formatMatrixMediaUnavailableText({
|
|
289
|
+
body: params.rawBody,
|
|
290
|
+
filename: params.filename,
|
|
291
|
+
msgtype: params.msgtype,
|
|
292
|
+
});
|
|
293
|
+
}
|
|
294
|
+
|
|
295
|
+
function markTrackedRoomIfFirst(set: Set<string>, roomId: string): boolean {
|
|
296
|
+
if (set.has(roomId)) {
|
|
297
|
+
return false;
|
|
298
|
+
}
|
|
299
|
+
set.add(roomId);
|
|
300
|
+
if (set.size > MAX_TRACKED_SHARED_DM_CONTEXT_NOTICES) {
|
|
301
|
+
const oldest = set.keys().next().value;
|
|
302
|
+
if (typeof oldest === "string") {
|
|
303
|
+
set.delete(oldest);
|
|
304
|
+
}
|
|
305
|
+
}
|
|
306
|
+
return true;
|
|
307
|
+
}
|
|
308
|
+
|
|
309
|
+
function resolveMatrixSharedDmContextNotice(params: {
|
|
310
|
+
storePath: string;
|
|
311
|
+
sessionKey: string;
|
|
312
|
+
roomId: string;
|
|
313
|
+
accountId: string;
|
|
314
|
+
dmSessionScope?: "per-user" | "per-room";
|
|
315
|
+
sentRooms: Set<string>;
|
|
316
|
+
logVerboseMessage: (message: string) => void;
|
|
317
|
+
}): string | null {
|
|
318
|
+
if ((params.dmSessionScope ?? "per-user") === "per-room") {
|
|
319
|
+
return null;
|
|
320
|
+
}
|
|
321
|
+
if (params.sentRooms.has(params.roomId)) {
|
|
322
|
+
return null;
|
|
323
|
+
}
|
|
324
|
+
|
|
325
|
+
try {
|
|
326
|
+
const store = loadSessionStore(params.storePath);
|
|
327
|
+
const currentSession = resolveMatrixStoredSessionMeta(
|
|
328
|
+
resolveSessionStoreEntry({
|
|
329
|
+
store,
|
|
330
|
+
sessionKey: params.sessionKey,
|
|
331
|
+
}).existing,
|
|
332
|
+
);
|
|
333
|
+
if (!currentSession) {
|
|
334
|
+
return null;
|
|
335
|
+
}
|
|
336
|
+
if (currentSession.channel && currentSession.channel !== "matrix") {
|
|
337
|
+
return null;
|
|
338
|
+
}
|
|
339
|
+
if (currentSession.accountId && currentSession.accountId !== params.accountId) {
|
|
340
|
+
return null;
|
|
341
|
+
}
|
|
342
|
+
if (!currentSession.directUserId) {
|
|
343
|
+
return null;
|
|
344
|
+
}
|
|
345
|
+
if (!currentSession.roomId || currentSession.roomId === params.roomId) {
|
|
346
|
+
return null;
|
|
347
|
+
}
|
|
348
|
+
|
|
349
|
+
return [
|
|
350
|
+
"This Matrix DM is sharing a session with another Matrix DM room.",
|
|
351
|
+
"Use /focus here for a one-off isolated thread session when thread bindings are enabled, or set",
|
|
352
|
+
"channels.matrix.dm.sessionScope to per-room to isolate each Matrix DM room.",
|
|
353
|
+
].join(" ");
|
|
354
|
+
} catch (err) {
|
|
355
|
+
params.logVerboseMessage(
|
|
356
|
+
`matrix: failed checking shared DM session notice room=${params.roomId} (${String(err)})`,
|
|
357
|
+
);
|
|
358
|
+
return null;
|
|
359
|
+
}
|
|
360
|
+
}
|
|
361
|
+
|
|
362
|
+
function resolveMatrixPendingHistoryText(params: {
|
|
363
|
+
mentionPrecheckText: string;
|
|
364
|
+
content: RoomMessageEventContent;
|
|
365
|
+
mediaUrl?: string;
|
|
366
|
+
}): string {
|
|
367
|
+
if (params.mentionPrecheckText) {
|
|
368
|
+
return params.mentionPrecheckText;
|
|
369
|
+
}
|
|
370
|
+
if (!params.mediaUrl) {
|
|
371
|
+
return "";
|
|
372
|
+
}
|
|
373
|
+
const body = typeof params.content.body === "string" ? params.content.body.trim() : undefined;
|
|
374
|
+
const filename =
|
|
375
|
+
typeof params.content.filename === "string" ? params.content.filename.trim() : undefined;
|
|
376
|
+
const msgtype = typeof params.content.msgtype === "string" ? params.content.msgtype : undefined;
|
|
377
|
+
return (
|
|
378
|
+
formatMatrixMessageText({
|
|
379
|
+
body: resolveMatrixMessageBody({ body, filename, msgtype }),
|
|
380
|
+
attachment: resolveMatrixMessageAttachment({ body, filename, msgtype }),
|
|
381
|
+
}) ?? ""
|
|
382
|
+
);
|
|
383
|
+
}
|
|
384
|
+
|
|
385
|
+
function resolveMatrixAllowBotsMode(value?: boolean | "mentions"): MatrixAllowBotsMode {
|
|
386
|
+
if (value === true) {
|
|
387
|
+
return "all";
|
|
388
|
+
}
|
|
389
|
+
if (value === "mentions") {
|
|
390
|
+
return "mentions";
|
|
391
|
+
}
|
|
392
|
+
return "off";
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
function formatMatrixToolProgressMarkdownCode(text: string): string {
|
|
396
|
+
const clipped =
|
|
397
|
+
text.length <= MATRIX_TOOL_PROGRESS_MAX_CHARS
|
|
398
|
+
? text
|
|
399
|
+
: `${text.slice(0, MATRIX_TOOL_PROGRESS_MAX_CHARS - 1).trimEnd()}...`;
|
|
400
|
+
const safe = clipped.replaceAll("`", "'");
|
|
401
|
+
return `\`${safe}\``;
|
|
402
|
+
}
|
|
403
|
+
|
|
404
|
+
export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParams) {
|
|
405
|
+
const {
|
|
406
|
+
client,
|
|
407
|
+
core,
|
|
408
|
+
cfg,
|
|
409
|
+
accountId,
|
|
410
|
+
accountConfig,
|
|
411
|
+
runtime,
|
|
412
|
+
logger,
|
|
413
|
+
logVerboseMessage,
|
|
414
|
+
allowFromResolvedEntries = [],
|
|
415
|
+
groupAllowFromResolvedEntries = [],
|
|
416
|
+
roomsConfig,
|
|
417
|
+
accountAllowBots,
|
|
418
|
+
configuredBotUserIds = new Set<string>(),
|
|
419
|
+
groupPolicy,
|
|
420
|
+
replyToMode,
|
|
421
|
+
threadReplies,
|
|
422
|
+
dmThreadReplies,
|
|
423
|
+
dmSessionScope,
|
|
424
|
+
streaming,
|
|
425
|
+
previewToolProgressEnabled,
|
|
426
|
+
blockStreamingEnabled,
|
|
427
|
+
dmEnabled,
|
|
428
|
+
dmPolicy,
|
|
429
|
+
textLimit,
|
|
430
|
+
mediaMaxBytes,
|
|
431
|
+
historyLimit,
|
|
432
|
+
startupMs,
|
|
433
|
+
startupGraceMs,
|
|
434
|
+
dropPreStartupMessages,
|
|
435
|
+
inboundDeduper,
|
|
436
|
+
directTracker,
|
|
437
|
+
getRoomInfo,
|
|
438
|
+
getMemberDisplayName,
|
|
439
|
+
needsRoomAliasesForConfig,
|
|
440
|
+
resolveLiveUserAllowlist = resolveMatrixMonitorLiveUserAllowlist,
|
|
441
|
+
} = params;
|
|
442
|
+
const contextVisibilityMode = resolveChannelContextVisibilityMode({
|
|
443
|
+
cfg,
|
|
444
|
+
channel: "matrix",
|
|
445
|
+
accountId,
|
|
446
|
+
});
|
|
447
|
+
let cachedStoreAllowFrom: {
|
|
448
|
+
value: string[];
|
|
449
|
+
expiresAtMs: number;
|
|
450
|
+
} | null = null;
|
|
451
|
+
type LiveAllowlistCacheEntry = { signature: string; entries: string[] };
|
|
452
|
+
let liveDmAllowlistCache: LiveAllowlistCacheEntry | null = null;
|
|
453
|
+
let liveGroupAllowlistCache: LiveAllowlistCacheEntry | null = null;
|
|
454
|
+
const resolveCachedLiveAllowlist = async (params: {
|
|
455
|
+
cfg: CoreConfig;
|
|
456
|
+
entries?: ReadonlyArray<string | number>;
|
|
457
|
+
failClosedOnUnresolved?: boolean;
|
|
458
|
+
startupResolvedEntries?: readonly MatrixResolvedAllowlistEntry[];
|
|
459
|
+
cache: LiveAllowlistCacheEntry | null;
|
|
460
|
+
updateCache: (next: LiveAllowlistCacheEntry) => void;
|
|
461
|
+
}): Promise<string[]> => {
|
|
462
|
+
const accountConfig = resolveMatrixAccountConfig({ cfg: params.cfg, accountId });
|
|
463
|
+
const signature = JSON.stringify({
|
|
464
|
+
entries: (params.entries ?? []).map((entry) => String(entry).trim()),
|
|
465
|
+
failClosedOnUnresolved: params.failClosedOnUnresolved === true,
|
|
466
|
+
dangerouslyAllowNameMatching: isDangerousNameMatchingEnabled(accountConfig),
|
|
467
|
+
});
|
|
468
|
+
if (params.cache?.signature === signature) {
|
|
469
|
+
return params.cache.entries;
|
|
470
|
+
}
|
|
471
|
+
const entries = await resolveLiveUserAllowlist({
|
|
472
|
+
cfg: params.cfg,
|
|
473
|
+
accountId,
|
|
474
|
+
entries: params.entries,
|
|
475
|
+
failClosedOnUnresolved: params.failClosedOnUnresolved,
|
|
476
|
+
startupResolvedEntries: params.startupResolvedEntries,
|
|
477
|
+
runtime,
|
|
478
|
+
});
|
|
479
|
+
const next = { signature, entries };
|
|
480
|
+
params.updateCache(next);
|
|
481
|
+
return entries;
|
|
482
|
+
};
|
|
483
|
+
const pairingReplySentAtMsBySender = new Map<string, number>();
|
|
484
|
+
const resolveThreadContext = createMatrixThreadContextResolver({
|
|
485
|
+
client,
|
|
486
|
+
getMemberDisplayName,
|
|
487
|
+
logVerboseMessage,
|
|
488
|
+
});
|
|
489
|
+
const resolveReplyContext = createMatrixReplyContextResolver({
|
|
490
|
+
client,
|
|
491
|
+
getMemberDisplayName,
|
|
492
|
+
logVerboseMessage,
|
|
493
|
+
});
|
|
494
|
+
const roomHistoryTracker = createRoomHistoryTracker();
|
|
495
|
+
const roomIngressTails = new Map<string, Promise<void>>();
|
|
496
|
+
const sharedDmContextNoticeRooms = new Set<string>();
|
|
497
|
+
|
|
498
|
+
const readStoreAllowFrom = async (): Promise<string[]> => {
|
|
499
|
+
const now = Date.now();
|
|
500
|
+
if (cachedStoreAllowFrom && now < cachedStoreAllowFrom.expiresAtMs) {
|
|
501
|
+
return cachedStoreAllowFrom.value;
|
|
502
|
+
}
|
|
503
|
+
const value = await core.channel.pairing
|
|
504
|
+
.readAllowFromStore({
|
|
505
|
+
channel: "matrix",
|
|
506
|
+
env: process.env,
|
|
507
|
+
accountId,
|
|
508
|
+
})
|
|
509
|
+
.catch(() => []);
|
|
510
|
+
cachedStoreAllowFrom = {
|
|
511
|
+
value,
|
|
512
|
+
expiresAtMs: now + ALLOW_FROM_STORE_CACHE_TTL_MS,
|
|
513
|
+
};
|
|
514
|
+
return value;
|
|
515
|
+
};
|
|
516
|
+
|
|
517
|
+
const shouldSendPairingReply = (senderId: string, created: boolean): boolean => {
|
|
518
|
+
const now = Date.now();
|
|
519
|
+
if (created) {
|
|
520
|
+
pairingReplySentAtMsBySender.set(senderId, now);
|
|
521
|
+
return true;
|
|
522
|
+
}
|
|
523
|
+
const lastSentAtMs = pairingReplySentAtMsBySender.get(senderId);
|
|
524
|
+
if (typeof lastSentAtMs === "number" && now - lastSentAtMs < PAIRING_REPLY_COOLDOWN_MS) {
|
|
525
|
+
return false;
|
|
526
|
+
}
|
|
527
|
+
pairingReplySentAtMsBySender.set(senderId, now);
|
|
528
|
+
if (pairingReplySentAtMsBySender.size > MAX_TRACKED_PAIRING_REPLY_SENDERS) {
|
|
529
|
+
const oldestSender = pairingReplySentAtMsBySender.keys().next().value;
|
|
530
|
+
if (typeof oldestSender === "string") {
|
|
531
|
+
pairingReplySentAtMsBySender.delete(oldestSender);
|
|
532
|
+
}
|
|
533
|
+
}
|
|
534
|
+
return true;
|
|
535
|
+
};
|
|
536
|
+
|
|
537
|
+
const runRoomIngress = async <T>(roomId: string, task: () => Promise<T>): Promise<T> => {
|
|
538
|
+
const previous = roomIngressTails.get(roomId) ?? Promise.resolve();
|
|
539
|
+
let releaseCurrent!: () => void;
|
|
540
|
+
const current = new Promise<void>((resolve) => {
|
|
541
|
+
releaseCurrent = resolve;
|
|
542
|
+
});
|
|
543
|
+
const chain = previous.catch(() => {}).then(() => current);
|
|
544
|
+
roomIngressTails.set(roomId, chain);
|
|
545
|
+
await previous.catch(() => {});
|
|
546
|
+
try {
|
|
547
|
+
return await task();
|
|
548
|
+
} finally {
|
|
549
|
+
releaseCurrent();
|
|
550
|
+
if (roomIngressTails.get(roomId) === chain) {
|
|
551
|
+
roomIngressTails.delete(roomId);
|
|
552
|
+
}
|
|
553
|
+
}
|
|
554
|
+
};
|
|
555
|
+
|
|
556
|
+
return async (roomId: string, event: MatrixRawEvent) => {
|
|
557
|
+
const eventId = typeof event.event_id === "string" ? event.event_id.trim() : "";
|
|
558
|
+
let claimedInboundEvent = false;
|
|
559
|
+
let draftStreamRef: MatrixDraftStreamHandle | undefined;
|
|
560
|
+
let draftConsumed = false;
|
|
561
|
+
try {
|
|
562
|
+
const eventType = event.type;
|
|
563
|
+
if (eventType === EventType.RoomMessageEncrypted) {
|
|
564
|
+
// Encrypted payloads are emitted separately after decryption.
|
|
565
|
+
return;
|
|
566
|
+
}
|
|
567
|
+
|
|
568
|
+
const isPollEvent = isPollEventType(eventType);
|
|
569
|
+
const isReactionEvent = eventType === EventType.Reaction;
|
|
570
|
+
const locationContent = event.content as LocationMessageEventContent;
|
|
571
|
+
const isLocationEvent =
|
|
572
|
+
eventType === EventType.Location ||
|
|
573
|
+
(eventType === EventType.RoomMessage && locationContent.msgtype === EventType.Location);
|
|
574
|
+
if (
|
|
575
|
+
eventType !== EventType.RoomMessage &&
|
|
576
|
+
!isPollEvent &&
|
|
577
|
+
!isLocationEvent &&
|
|
578
|
+
!isReactionEvent
|
|
579
|
+
) {
|
|
580
|
+
return;
|
|
581
|
+
}
|
|
582
|
+
logVerboseMessage(
|
|
583
|
+
`matrix: inbound event room=${roomId} type=${eventType} id=${event.event_id ?? "unknown"}`,
|
|
584
|
+
);
|
|
585
|
+
if (event.unsigned?.redacted_because) {
|
|
586
|
+
return;
|
|
587
|
+
}
|
|
588
|
+
const senderId = event.sender;
|
|
589
|
+
if (!senderId) {
|
|
590
|
+
return;
|
|
591
|
+
}
|
|
592
|
+
const eventTs = event.origin_server_ts;
|
|
593
|
+
const eventAge = event.unsigned?.age;
|
|
594
|
+
const commitInboundEventIfClaimed = async () => {
|
|
595
|
+
if (!claimedInboundEvent || !inboundDeduper || !eventId) {
|
|
596
|
+
return;
|
|
597
|
+
}
|
|
598
|
+
await inboundDeduper.commitEvent({ roomId, eventId });
|
|
599
|
+
claimedInboundEvent = false;
|
|
600
|
+
};
|
|
601
|
+
const readIngressPrefix = async () => {
|
|
602
|
+
const selfUserId = await client.getUserId();
|
|
603
|
+
if (senderId === selfUserId) {
|
|
604
|
+
return undefined;
|
|
605
|
+
}
|
|
606
|
+
if (dropPreStartupMessages) {
|
|
607
|
+
if (typeof eventTs === "number" && eventTs < startupMs - startupGraceMs) {
|
|
608
|
+
return undefined;
|
|
609
|
+
}
|
|
610
|
+
if (
|
|
611
|
+
typeof eventTs !== "number" &&
|
|
612
|
+
typeof eventAge === "number" &&
|
|
613
|
+
eventAge > startupGraceMs
|
|
614
|
+
) {
|
|
615
|
+
return undefined;
|
|
616
|
+
}
|
|
617
|
+
}
|
|
618
|
+
|
|
619
|
+
let content = event.content as RoomMessageEventContent;
|
|
620
|
+
|
|
621
|
+
if (
|
|
622
|
+
eventType === EventType.RoomMessage &&
|
|
623
|
+
isMatrixVerificationRoomMessage({
|
|
624
|
+
msgtype: (content as { msgtype?: unknown }).msgtype,
|
|
625
|
+
body: content.body,
|
|
626
|
+
})
|
|
627
|
+
) {
|
|
628
|
+
logVerboseMessage(`matrix: skip verification/system room message room=${roomId}`);
|
|
629
|
+
return undefined;
|
|
630
|
+
}
|
|
631
|
+
|
|
632
|
+
const locationPayload: MatrixLocationPayload | null = resolveMatrixLocation({
|
|
633
|
+
eventType,
|
|
634
|
+
content: content as LocationMessageEventContent,
|
|
635
|
+
});
|
|
636
|
+
|
|
637
|
+
const relates = content["m.relates_to"];
|
|
638
|
+
if (relates && "rel_type" in relates && relates.rel_type === RelationType.Replace) {
|
|
639
|
+
return undefined;
|
|
640
|
+
}
|
|
641
|
+
if (hasBundledMatrixReplacementRelation(event)) {
|
|
642
|
+
return undefined;
|
|
643
|
+
}
|
|
644
|
+
if (eventId && inboundDeduper) {
|
|
645
|
+
claimedInboundEvent = inboundDeduper.claimEvent({ roomId, eventId });
|
|
646
|
+
if (!claimedInboundEvent) {
|
|
647
|
+
logVerboseMessage(`matrix: skip duplicate inbound event room=${roomId} id=${eventId}`);
|
|
648
|
+
return undefined;
|
|
649
|
+
}
|
|
650
|
+
}
|
|
651
|
+
|
|
652
|
+
const isDirectMessage = await directTracker.isDirectMessage({
|
|
653
|
+
roomId,
|
|
654
|
+
senderId,
|
|
655
|
+
selfUserId,
|
|
656
|
+
});
|
|
657
|
+
return { content, isDirectMessage, locationPayload, selfUserId };
|
|
658
|
+
};
|
|
659
|
+
const continueIngress = async (params: {
|
|
660
|
+
content: RoomMessageEventContent;
|
|
661
|
+
isDirectMessage: boolean;
|
|
662
|
+
locationPayload: MatrixLocationPayload | null;
|
|
663
|
+
selfUserId: string;
|
|
664
|
+
}) => {
|
|
665
|
+
let content = params.content;
|
|
666
|
+
const isDirectMessage = params.isDirectMessage;
|
|
667
|
+
const isRoom = !isDirectMessage;
|
|
668
|
+
const { locationPayload, selfUserId } = params;
|
|
669
|
+
if (isRoom && groupPolicy === "disabled") {
|
|
670
|
+
await commitInboundEventIfClaimed();
|
|
671
|
+
return undefined;
|
|
672
|
+
}
|
|
673
|
+
|
|
674
|
+
const roomInfoForConfig =
|
|
675
|
+
isRoom && needsRoomAliasesForConfig
|
|
676
|
+
? await getRoomInfo(roomId, { includeAliases: true })
|
|
677
|
+
: undefined;
|
|
678
|
+
const roomAliasesForConfig = roomInfoForConfig
|
|
679
|
+
? [roomInfoForConfig.canonicalAlias ?? "", ...roomInfoForConfig.altAliases].filter(
|
|
680
|
+
Boolean,
|
|
681
|
+
)
|
|
682
|
+
: [];
|
|
683
|
+
const roomConfigInfo = isRoom
|
|
684
|
+
? resolveMatrixRoomConfig({
|
|
685
|
+
rooms: roomsConfig,
|
|
686
|
+
roomId,
|
|
687
|
+
aliases: roomAliasesForConfig,
|
|
688
|
+
})
|
|
689
|
+
: undefined;
|
|
690
|
+
const roomConfig = roomConfigInfo?.config;
|
|
691
|
+
const allowBotsMode = resolveMatrixAllowBotsMode(roomConfig?.allowBots ?? accountAllowBots);
|
|
692
|
+
const isConfiguredBotSender = configuredBotUserIds.has(senderId);
|
|
693
|
+
const roomMatchMeta = roomConfigInfo
|
|
694
|
+
? `matchKey=${roomConfigInfo.matchKey ?? "none"} matchSource=${
|
|
695
|
+
roomConfigInfo.matchSource ?? "none"
|
|
696
|
+
}`
|
|
697
|
+
: "matchKey=none matchSource=none";
|
|
698
|
+
|
|
699
|
+
if (isConfiguredBotSender && allowBotsMode === "off") {
|
|
700
|
+
logVerboseMessage(
|
|
701
|
+
`matrix: drop configured bot sender=${senderId} (allowBots=false${isDirectMessage ? "" : `, ${roomMatchMeta}`})`,
|
|
702
|
+
);
|
|
703
|
+
await commitInboundEventIfClaimed();
|
|
704
|
+
return undefined;
|
|
705
|
+
}
|
|
706
|
+
const botLoopProtection: ChannelBotLoopProtectionFacts | undefined =
|
|
707
|
+
isConfiguredBotSender && senderId !== selfUserId
|
|
708
|
+
? {
|
|
709
|
+
scopeId: accountId,
|
|
710
|
+
conversationId: roomId,
|
|
711
|
+
senderId,
|
|
712
|
+
receiverId: selfUserId,
|
|
713
|
+
config: mergePairLoopGuardConfig(
|
|
714
|
+
accountConfig?.botLoopProtection,
|
|
715
|
+
roomConfig?.botLoopProtection,
|
|
716
|
+
),
|
|
717
|
+
defaultsConfig: cfg.channels?.defaults?.botLoopProtection,
|
|
718
|
+
defaultEnabled: true,
|
|
719
|
+
nowMs: eventTs ?? undefined,
|
|
720
|
+
}
|
|
721
|
+
: undefined;
|
|
722
|
+
|
|
723
|
+
if (isRoom && roomConfig && !roomConfigInfo?.allowed) {
|
|
724
|
+
logVerboseMessage(`matrix: room disabled room=${roomId} (${roomMatchMeta})`);
|
|
725
|
+
await commitInboundEventIfClaimed();
|
|
726
|
+
return undefined;
|
|
727
|
+
}
|
|
728
|
+
if (isRoom && groupPolicy === "allowlist") {
|
|
729
|
+
if (!roomConfigInfo?.allowlistConfigured) {
|
|
730
|
+
logVerboseMessage(`matrix: drop room message (no allowlist, ${roomMatchMeta})`);
|
|
731
|
+
await commitInboundEventIfClaimed();
|
|
732
|
+
return undefined;
|
|
733
|
+
}
|
|
734
|
+
if (!roomConfig) {
|
|
735
|
+
logVerboseMessage(`matrix: drop room message (not in allowlist, ${roomMatchMeta})`);
|
|
736
|
+
await commitInboundEventIfClaimed();
|
|
737
|
+
return undefined;
|
|
738
|
+
}
|
|
739
|
+
}
|
|
740
|
+
|
|
741
|
+
let senderNamePromise: Promise<string> | null = null;
|
|
742
|
+
const getSenderName = async (): Promise<string> => {
|
|
743
|
+
senderNamePromise ??= getMemberDisplayName(roomId, senderId).catch(() => senderId);
|
|
744
|
+
return await senderNamePromise;
|
|
745
|
+
};
|
|
746
|
+
const storeAllowFrom =
|
|
747
|
+
isDirectMessage && dmPolicy !== "allowlist" && dmPolicy !== "open"
|
|
748
|
+
? await readStoreAllowFrom()
|
|
749
|
+
: [];
|
|
750
|
+
const roomUsers = roomConfig?.users ?? [];
|
|
751
|
+
const liveCfg = core.config.current() as CoreConfig;
|
|
752
|
+
const liveAccountAllowlists = resolveMatrixAccountAllowlistConfig({
|
|
753
|
+
cfg: liveCfg,
|
|
754
|
+
accountId,
|
|
755
|
+
});
|
|
756
|
+
const liveDmAllowFrom = await resolveCachedLiveAllowlist({
|
|
757
|
+
cfg: liveCfg,
|
|
758
|
+
entries: liveAccountAllowlists.dmAllowFrom,
|
|
759
|
+
startupResolvedEntries: allowFromResolvedEntries,
|
|
760
|
+
cache: liveDmAllowlistCache,
|
|
761
|
+
updateCache: (next) => {
|
|
762
|
+
liveDmAllowlistCache = next;
|
|
763
|
+
},
|
|
764
|
+
});
|
|
765
|
+
const liveGroupAllowFrom = await resolveCachedLiveAllowlist({
|
|
766
|
+
cfg: liveCfg,
|
|
767
|
+
entries: liveAccountAllowlists.groupAllowFrom,
|
|
768
|
+
failClosedOnUnresolved: true,
|
|
769
|
+
startupResolvedEntries: groupAllowFromResolvedEntries,
|
|
770
|
+
cache: liveGroupAllowlistCache,
|
|
771
|
+
updateCache: (next) => {
|
|
772
|
+
liveGroupAllowlistCache = next;
|
|
773
|
+
},
|
|
774
|
+
});
|
|
775
|
+
const accessState = await resolveMatrixMonitorAccessState({
|
|
776
|
+
allowFrom: liveDmAllowFrom,
|
|
777
|
+
storeAllowFrom,
|
|
778
|
+
dmPolicy,
|
|
779
|
+
groupPolicy,
|
|
780
|
+
groupAllowFrom: liveGroupAllowFrom,
|
|
781
|
+
roomUsers,
|
|
782
|
+
senderId,
|
|
783
|
+
isRoom,
|
|
784
|
+
accountId,
|
|
785
|
+
eventKind: isReactionEvent ? "reaction" : "message",
|
|
786
|
+
});
|
|
787
|
+
const { effectiveGroupAllowFrom, effectiveRoomUsers, messageIngress } = accessState;
|
|
788
|
+
const ingressDecision = messageIngress.ingress;
|
|
789
|
+
|
|
790
|
+
if (isDirectMessage) {
|
|
791
|
+
if (!dmEnabled || dmPolicy === "disabled") {
|
|
792
|
+
await commitInboundEventIfClaimed();
|
|
793
|
+
return undefined;
|
|
794
|
+
}
|
|
795
|
+
const senderReason = messageIngress.senderAccess.reasonCode;
|
|
796
|
+
if (ingressDecision.decision !== "allow") {
|
|
797
|
+
if (ingressDecision.admission === "pairing-required") {
|
|
798
|
+
const senderName = await getSenderName();
|
|
799
|
+
const { code, created } = await core.channel.pairing.upsertPairingRequest({
|
|
800
|
+
channel: "matrix",
|
|
801
|
+
id: senderId,
|
|
802
|
+
accountId,
|
|
803
|
+
meta: { name: senderName },
|
|
804
|
+
});
|
|
805
|
+
if (shouldSendPairingReply(senderId, created)) {
|
|
806
|
+
const pairingReply = core.channel.pairing.buildPairingReply({
|
|
807
|
+
channel: "matrix",
|
|
808
|
+
idLine: `Your Matrix user id: ${senderId}`,
|
|
809
|
+
code,
|
|
810
|
+
});
|
|
811
|
+
logVerboseMessage(
|
|
812
|
+
created
|
|
813
|
+
? `matrix pairing request sender=${senderId} name=${senderName ?? "unknown"} (reason=${senderReason})`
|
|
814
|
+
: `matrix pairing reminder sender=${senderId} name=${senderName ?? "unknown"} (reason=${senderReason})`,
|
|
815
|
+
);
|
|
816
|
+
try {
|
|
817
|
+
const { sendMessageMatrix } = await loadMatrixSendModule();
|
|
818
|
+
await sendMessageMatrix(
|
|
819
|
+
`room:${roomId}`,
|
|
820
|
+
created
|
|
821
|
+
? pairingReply
|
|
822
|
+
: `${pairingReply}\n\nPairing request is still pending approval. Reusing existing code.`,
|
|
823
|
+
{
|
|
824
|
+
client,
|
|
825
|
+
cfg,
|
|
826
|
+
accountId,
|
|
827
|
+
},
|
|
828
|
+
);
|
|
829
|
+
await commitInboundEventIfClaimed();
|
|
830
|
+
} catch (err) {
|
|
831
|
+
logVerboseMessage(`matrix pairing reply failed for ${senderId}: ${String(err)}`);
|
|
832
|
+
return undefined;
|
|
833
|
+
}
|
|
834
|
+
} else {
|
|
835
|
+
logVerboseMessage(
|
|
836
|
+
`matrix pairing reminder suppressed sender=${senderId} (cooldown)`,
|
|
837
|
+
);
|
|
838
|
+
await commitInboundEventIfClaimed();
|
|
839
|
+
}
|
|
840
|
+
}
|
|
841
|
+
if (isReactionEvent || dmPolicy !== "pairing") {
|
|
842
|
+
logVerboseMessage(
|
|
843
|
+
`matrix: blocked ${isReactionEvent ? "reaction" : "dm"} sender ${senderId} (dmPolicy=${dmPolicy}, reason=${senderReason})`,
|
|
844
|
+
);
|
|
845
|
+
await commitInboundEventIfClaimed();
|
|
846
|
+
}
|
|
847
|
+
return undefined;
|
|
848
|
+
}
|
|
849
|
+
}
|
|
850
|
+
|
|
851
|
+
if (isRoom && ingressDecision.decision !== "allow") {
|
|
852
|
+
logVerboseMessage(
|
|
853
|
+
`matrix: blocked sender ${senderId} (ingress=${ingressDecision.reasonCode}, ${roomMatchMeta})`,
|
|
854
|
+
);
|
|
855
|
+
await commitInboundEventIfClaimed();
|
|
856
|
+
return undefined;
|
|
857
|
+
}
|
|
858
|
+
if (isRoom) {
|
|
859
|
+
logVerboseMessage(`matrix: allow room ${roomId} (${roomMatchMeta})`);
|
|
860
|
+
}
|
|
861
|
+
|
|
862
|
+
if (isReactionEvent) {
|
|
863
|
+
const senderName = await getSenderName();
|
|
864
|
+
const { handleInboundMatrixReaction } = await loadMatrixReactionEvents();
|
|
865
|
+
await handleInboundMatrixReaction({
|
|
866
|
+
client,
|
|
867
|
+
core,
|
|
868
|
+
cfg,
|
|
869
|
+
accountId,
|
|
870
|
+
roomId,
|
|
871
|
+
event,
|
|
872
|
+
senderId,
|
|
873
|
+
senderLabel: senderName,
|
|
874
|
+
selfUserId,
|
|
875
|
+
isDirectMessage,
|
|
876
|
+
logVerboseMessage,
|
|
877
|
+
});
|
|
878
|
+
await commitInboundEventIfClaimed();
|
|
879
|
+
return undefined;
|
|
880
|
+
}
|
|
881
|
+
|
|
882
|
+
let pollSnapshotPromise: Promise<MatrixPollSnapshot | null> | null = null;
|
|
883
|
+
const getPollSnapshot = async (): Promise<MatrixPollSnapshot | null> => {
|
|
884
|
+
if (!isPollEvent) {
|
|
885
|
+
return null;
|
|
886
|
+
}
|
|
887
|
+
pollSnapshotPromise ??= fetchMatrixPollSnapshot(client, roomId, event).catch((err) => {
|
|
888
|
+
logVerboseMessage(
|
|
889
|
+
`matrix: failed resolving poll snapshot room=${roomId} id=${event.event_id ?? "unknown"}: ${String(err)}`,
|
|
890
|
+
);
|
|
891
|
+
return null;
|
|
892
|
+
});
|
|
893
|
+
return await pollSnapshotPromise;
|
|
894
|
+
};
|
|
895
|
+
|
|
896
|
+
const mentionPrecheckText = resolveMatrixMentionPrecheckText({
|
|
897
|
+
eventType,
|
|
898
|
+
content,
|
|
899
|
+
locationText: locationPayload?.text,
|
|
900
|
+
});
|
|
901
|
+
const contentUrl =
|
|
902
|
+
"url" in content && typeof content.url === "string" ? content.url : undefined;
|
|
903
|
+
const contentFile =
|
|
904
|
+
"file" in content && content.file && typeof content.file === "object"
|
|
905
|
+
? content.file
|
|
906
|
+
: undefined;
|
|
907
|
+
const mediaUrl = contentUrl ?? contentFile?.url;
|
|
908
|
+
const pendingHistoryText = resolveMatrixPendingHistoryText({
|
|
909
|
+
mentionPrecheckText,
|
|
910
|
+
content,
|
|
911
|
+
mediaUrl,
|
|
912
|
+
});
|
|
913
|
+
const pendingHistoryPollText =
|
|
914
|
+
!pendingHistoryText && isPollEvent && historyLimit > 0
|
|
915
|
+
? (await getPollSnapshot())?.text
|
|
916
|
+
: "";
|
|
917
|
+
if (!mentionPrecheckText && !mediaUrl && !isPollEvent) {
|
|
918
|
+
await commitInboundEventIfClaimed();
|
|
919
|
+
return undefined;
|
|
920
|
+
}
|
|
921
|
+
|
|
922
|
+
const messageId = event.event_id ?? "";
|
|
923
|
+
const threadRootId = resolveMatrixThreadRootId({ event, content });
|
|
924
|
+
const thread = resolveMatrixThreadRouting({
|
|
925
|
+
isDirectMessage,
|
|
926
|
+
threadReplies,
|
|
927
|
+
dmThreadReplies,
|
|
928
|
+
messageId,
|
|
929
|
+
threadRootId,
|
|
930
|
+
});
|
|
931
|
+
const {
|
|
932
|
+
route: _route,
|
|
933
|
+
configuredBinding: _configuredBinding,
|
|
934
|
+
runtimeBindingId: _runtimeBindingId,
|
|
935
|
+
} = resolveMatrixInboundRoute({
|
|
936
|
+
cfg,
|
|
937
|
+
accountId,
|
|
938
|
+
roomId,
|
|
939
|
+
senderId,
|
|
940
|
+
isDirectMessage,
|
|
941
|
+
dmSessionScope,
|
|
942
|
+
threadId: thread.threadId,
|
|
943
|
+
eventTs: eventTs ?? undefined,
|
|
944
|
+
resolveAgentRoute: core.channel.routing.resolveAgentRoute,
|
|
945
|
+
});
|
|
946
|
+
const hasExplicitSessionBinding = _configuredBinding !== null || _runtimeBindingId !== null;
|
|
947
|
+
const agentMentionRegexes = core.channel.mentions.buildMentionRegexes(cfg, _route.agentId);
|
|
948
|
+
const selfDisplayName = content.formatted_body
|
|
949
|
+
? await getMemberDisplayName(roomId, selfUserId).catch(() => undefined)
|
|
950
|
+
: undefined;
|
|
951
|
+
const { wasMentioned, hasExplicitMention } = resolveMentions({
|
|
952
|
+
content,
|
|
953
|
+
userId: selfUserId,
|
|
954
|
+
displayName: selfDisplayName,
|
|
955
|
+
text: mentionPrecheckText,
|
|
956
|
+
mentionRegexes: agentMentionRegexes,
|
|
957
|
+
});
|
|
958
|
+
if (
|
|
959
|
+
isConfiguredBotSender &&
|
|
960
|
+
allowBotsMode === "mentions" &&
|
|
961
|
+
!isDirectMessage &&
|
|
962
|
+
!wasMentioned
|
|
963
|
+
) {
|
|
964
|
+
logVerboseMessage(
|
|
965
|
+
`matrix: drop configured bot sender=${senderId} (allowBots=mentions, missing mention, ${roomMatchMeta})`,
|
|
966
|
+
);
|
|
967
|
+
await commitInboundEventIfClaimed();
|
|
968
|
+
return undefined;
|
|
969
|
+
}
|
|
970
|
+
const allowTextCommands = core.channel.commands.shouldHandleTextCommands({
|
|
971
|
+
cfg,
|
|
972
|
+
surface: "matrix",
|
|
973
|
+
});
|
|
974
|
+
const useAccessGroups = cfg.commands?.useAccessGroups !== false;
|
|
975
|
+
// Keep mention stripping on the command-only path so history and agent
|
|
976
|
+
// prompt text continue to see the original Matrix message.
|
|
977
|
+
const commandCheckText = stripMatrixMentionPrefix({
|
|
978
|
+
text: mentionPrecheckText,
|
|
979
|
+
userId: selfUserId,
|
|
980
|
+
displayName: selfDisplayName,
|
|
981
|
+
mentionRegexes: agentMentionRegexes,
|
|
982
|
+
});
|
|
983
|
+
const hasControlCommandInMessage = core.channel.text.hasControlCommand(
|
|
984
|
+
commandCheckText,
|
|
985
|
+
cfg,
|
|
986
|
+
);
|
|
987
|
+
const commandAccess = await resolveMatrixMonitorCommandAccess(accessState, {
|
|
988
|
+
useAccessGroups,
|
|
989
|
+
allowTextCommands,
|
|
990
|
+
hasControlCommand: hasControlCommandInMessage,
|
|
991
|
+
});
|
|
992
|
+
const commandAuthorized = commandAccess.authorized;
|
|
993
|
+
if (isRoom && commandAccess.shouldBlockControlCommand) {
|
|
994
|
+
logInboundDrop({
|
|
995
|
+
log: logVerboseMessage,
|
|
996
|
+
channel: "matrix",
|
|
997
|
+
reason: "control command (unauthorized)",
|
|
998
|
+
target: senderId,
|
|
999
|
+
});
|
|
1000
|
+
await commitInboundEventIfClaimed();
|
|
1001
|
+
return undefined;
|
|
1002
|
+
}
|
|
1003
|
+
const shouldRequireMention = isRoom
|
|
1004
|
+
? roomConfig?.autoReply === true
|
|
1005
|
+
? false
|
|
1006
|
+
: roomConfig?.autoReply === false
|
|
1007
|
+
? true
|
|
1008
|
+
: typeof roomConfig?.requireMention === "boolean"
|
|
1009
|
+
? roomConfig?.requireMention
|
|
1010
|
+
: true
|
|
1011
|
+
: false;
|
|
1012
|
+
const shouldBypassMention =
|
|
1013
|
+
allowTextCommands &&
|
|
1014
|
+
isRoom &&
|
|
1015
|
+
shouldRequireMention &&
|
|
1016
|
+
!wasMentioned &&
|
|
1017
|
+
!hasExplicitMention &&
|
|
1018
|
+
commandAuthorized &&
|
|
1019
|
+
hasControlCommandInMessage;
|
|
1020
|
+
const canDetectMention = agentMentionRegexes.length > 0 || hasExplicitMention;
|
|
1021
|
+
if (isRoom && shouldRequireMention && !wasMentioned && !shouldBypassMention) {
|
|
1022
|
+
const pendingHistoryBody = pendingHistoryText || pendingHistoryPollText;
|
|
1023
|
+
if (historyLimit > 0 && pendingHistoryBody) {
|
|
1024
|
+
const pendingEntry: HistoryEntry = {
|
|
1025
|
+
sender: senderId,
|
|
1026
|
+
body: pendingHistoryBody,
|
|
1027
|
+
timestamp: eventTs ?? undefined,
|
|
1028
|
+
messageId,
|
|
1029
|
+
};
|
|
1030
|
+
roomHistoryTracker.recordPending(roomId, pendingEntry);
|
|
1031
|
+
}
|
|
1032
|
+
logger.info("skipping room message", { roomId, reason: "no-mention" });
|
|
1033
|
+
await commitInboundEventIfClaimed();
|
|
1034
|
+
return undefined;
|
|
1035
|
+
}
|
|
1036
|
+
|
|
1037
|
+
if (isPollEvent) {
|
|
1038
|
+
const pollSnapshot = await getPollSnapshot();
|
|
1039
|
+
if (!pollSnapshot) {
|
|
1040
|
+
return undefined;
|
|
1041
|
+
}
|
|
1042
|
+
content = {
|
|
1043
|
+
msgtype: "m.text",
|
|
1044
|
+
body: pollSnapshot.text,
|
|
1045
|
+
} as unknown as RoomMessageEventContent;
|
|
1046
|
+
}
|
|
1047
|
+
|
|
1048
|
+
let media: {
|
|
1049
|
+
path: string;
|
|
1050
|
+
contentType?: string;
|
|
1051
|
+
placeholder: string;
|
|
1052
|
+
} | null = null;
|
|
1053
|
+
let mediaDownloadFailed = false;
|
|
1054
|
+
let mediaSizeLimitExceeded = false;
|
|
1055
|
+
const finalContentUrl =
|
|
1056
|
+
"url" in content && typeof content.url === "string" ? content.url : undefined;
|
|
1057
|
+
const finalContentFile =
|
|
1058
|
+
"file" in content && content.file && typeof content.file === "object"
|
|
1059
|
+
? content.file
|
|
1060
|
+
: undefined;
|
|
1061
|
+
const finalMediaUrl = finalContentUrl ?? finalContentFile?.url;
|
|
1062
|
+
const contentBody = typeof content.body === "string" ? content.body.trim() : "";
|
|
1063
|
+
const contentFilename = typeof content.filename === "string" ? content.filename.trim() : "";
|
|
1064
|
+
const originalFilename = contentFilename || contentBody || undefined;
|
|
1065
|
+
const contentInfo =
|
|
1066
|
+
"info" in content && content.info && typeof content.info === "object"
|
|
1067
|
+
? (content.info as { mimetype?: string; size?: number })
|
|
1068
|
+
: undefined;
|
|
1069
|
+
const contentType = contentInfo?.mimetype;
|
|
1070
|
+
const contentSize = typeof contentInfo?.size === "number" ? contentInfo.size : undefined;
|
|
1071
|
+
if (finalMediaUrl?.startsWith("mxc://")) {
|
|
1072
|
+
try {
|
|
1073
|
+
media = await downloadMatrixMedia({
|
|
1074
|
+
client,
|
|
1075
|
+
mxcUrl: finalMediaUrl,
|
|
1076
|
+
contentType,
|
|
1077
|
+
sizeBytes: contentSize,
|
|
1078
|
+
maxBytes: mediaMaxBytes,
|
|
1079
|
+
file: finalContentFile,
|
|
1080
|
+
originalFilename,
|
|
1081
|
+
});
|
|
1082
|
+
} catch (err) {
|
|
1083
|
+
mediaDownloadFailed = true;
|
|
1084
|
+
if (isMatrixMediaSizeLimitError(err)) {
|
|
1085
|
+
mediaSizeLimitExceeded = true;
|
|
1086
|
+
}
|
|
1087
|
+
const errorText = formatMatrixErrorMessage(err);
|
|
1088
|
+
logVerboseMessage(
|
|
1089
|
+
`matrix: media download failed room=${roomId} id=${event.event_id ?? "unknown"} type=${content.msgtype} error=${errorText}`,
|
|
1090
|
+
);
|
|
1091
|
+
logger.warn("matrix media download failed", {
|
|
1092
|
+
roomId,
|
|
1093
|
+
eventId: event.event_id,
|
|
1094
|
+
msgtype: content.msgtype,
|
|
1095
|
+
encrypted: Boolean(finalContentFile),
|
|
1096
|
+
error: errorText,
|
|
1097
|
+
});
|
|
1098
|
+
}
|
|
1099
|
+
}
|
|
1100
|
+
|
|
1101
|
+
const rawBody = locationPayload?.text ?? contentBody;
|
|
1102
|
+
const bodyText = resolveMatrixInboundBodyText({
|
|
1103
|
+
rawBody,
|
|
1104
|
+
filename: typeof content.filename === "string" ? content.filename : undefined,
|
|
1105
|
+
mediaPlaceholder: media?.placeholder,
|
|
1106
|
+
msgtype: content.msgtype,
|
|
1107
|
+
hadMediaUrl: Boolean(finalMediaUrl),
|
|
1108
|
+
mediaDownloadFailed,
|
|
1109
|
+
mediaSizeLimitExceeded,
|
|
1110
|
+
});
|
|
1111
|
+
if (!bodyText) {
|
|
1112
|
+
await commitInboundEventIfClaimed();
|
|
1113
|
+
return undefined;
|
|
1114
|
+
}
|
|
1115
|
+
const commandBodyText = hasControlCommandInMessage ? commandCheckText : bodyText;
|
|
1116
|
+
const senderName = await getSenderName();
|
|
1117
|
+
if (_configuredBinding) {
|
|
1118
|
+
const { ensureConfiguredAcpBindingReady } = await loadAcpBindingRuntime();
|
|
1119
|
+
const ensured = await ensureConfiguredAcpBindingReady({
|
|
1120
|
+
cfg,
|
|
1121
|
+
configuredBinding: _configuredBinding,
|
|
1122
|
+
});
|
|
1123
|
+
if (!ensured.ok) {
|
|
1124
|
+
logInboundDrop({
|
|
1125
|
+
log: logVerboseMessage,
|
|
1126
|
+
channel: "matrix",
|
|
1127
|
+
reason: "configured ACP binding unavailable",
|
|
1128
|
+
target: _configuredBinding.spec.conversationId,
|
|
1129
|
+
});
|
|
1130
|
+
return undefined;
|
|
1131
|
+
}
|
|
1132
|
+
}
|
|
1133
|
+
if (_runtimeBindingId) {
|
|
1134
|
+
const { getSessionBindingService } = await loadSessionBindingRuntime();
|
|
1135
|
+
getSessionBindingService().touch(_runtimeBindingId, eventTs ?? undefined);
|
|
1136
|
+
}
|
|
1137
|
+
const preparedTrigger =
|
|
1138
|
+
isRoom && historyLimit > 0
|
|
1139
|
+
? roomHistoryTracker.prepareTrigger(_route.agentId, roomId, historyLimit, {
|
|
1140
|
+
sender: senderName,
|
|
1141
|
+
body: bodyText,
|
|
1142
|
+
timestamp: eventTs ?? undefined,
|
|
1143
|
+
messageId,
|
|
1144
|
+
})
|
|
1145
|
+
: undefined;
|
|
1146
|
+
const inboundHistory = preparedTrigger
|
|
1147
|
+
? buildInboundHistoryFromEntries({
|
|
1148
|
+
entries: preparedTrigger.history,
|
|
1149
|
+
limit: historyLimit,
|
|
1150
|
+
})
|
|
1151
|
+
: undefined;
|
|
1152
|
+
const triggerSnapshot = preparedTrigger;
|
|
1153
|
+
|
|
1154
|
+
return {
|
|
1155
|
+
route: _route,
|
|
1156
|
+
hasExplicitSessionBinding,
|
|
1157
|
+
roomConfig,
|
|
1158
|
+
isDirectMessage,
|
|
1159
|
+
isRoom,
|
|
1160
|
+
shouldRequireMention,
|
|
1161
|
+
wasMentioned,
|
|
1162
|
+
shouldBypassMention,
|
|
1163
|
+
canDetectMention,
|
|
1164
|
+
commandAuthorized,
|
|
1165
|
+
inboundHistory,
|
|
1166
|
+
senderName,
|
|
1167
|
+
bodyText,
|
|
1168
|
+
commandBodyText,
|
|
1169
|
+
media,
|
|
1170
|
+
locationPayload,
|
|
1171
|
+
messageId,
|
|
1172
|
+
triggerSnapshot,
|
|
1173
|
+
threadRootId,
|
|
1174
|
+
thread,
|
|
1175
|
+
botLoopProtection,
|
|
1176
|
+
effectiveGroupAllowFrom,
|
|
1177
|
+
effectiveRoomUsers,
|
|
1178
|
+
};
|
|
1179
|
+
};
|
|
1180
|
+
const ingressResult =
|
|
1181
|
+
historyLimit > 0
|
|
1182
|
+
? await runRoomIngress(roomId, async () => {
|
|
1183
|
+
const prefix = await readIngressPrefix();
|
|
1184
|
+
if (!prefix) {
|
|
1185
|
+
return undefined;
|
|
1186
|
+
}
|
|
1187
|
+
if (prefix.isDirectMessage) {
|
|
1188
|
+
return { deferredPrefix: prefix } as const;
|
|
1189
|
+
}
|
|
1190
|
+
return { ingressResult: await continueIngress(prefix) } as const;
|
|
1191
|
+
})
|
|
1192
|
+
: undefined;
|
|
1193
|
+
const resolvedIngressResult =
|
|
1194
|
+
historyLimit > 0
|
|
1195
|
+
? ingressResult?.deferredPrefix
|
|
1196
|
+
? await continueIngress(ingressResult.deferredPrefix)
|
|
1197
|
+
: ingressResult?.ingressResult
|
|
1198
|
+
: await (async () => {
|
|
1199
|
+
const prefix = await readIngressPrefix();
|
|
1200
|
+
if (!prefix) {
|
|
1201
|
+
return undefined;
|
|
1202
|
+
}
|
|
1203
|
+
return await continueIngress(prefix);
|
|
1204
|
+
})();
|
|
1205
|
+
if (!resolvedIngressResult) {
|
|
1206
|
+
return;
|
|
1207
|
+
}
|
|
1208
|
+
|
|
1209
|
+
const {
|
|
1210
|
+
route: _route,
|
|
1211
|
+
hasExplicitSessionBinding,
|
|
1212
|
+
roomConfig,
|
|
1213
|
+
isDirectMessage,
|
|
1214
|
+
isRoom,
|
|
1215
|
+
shouldRequireMention,
|
|
1216
|
+
wasMentioned,
|
|
1217
|
+
shouldBypassMention,
|
|
1218
|
+
canDetectMention,
|
|
1219
|
+
commandAuthorized,
|
|
1220
|
+
inboundHistory,
|
|
1221
|
+
senderName,
|
|
1222
|
+
bodyText,
|
|
1223
|
+
commandBodyText,
|
|
1224
|
+
media,
|
|
1225
|
+
locationPayload,
|
|
1226
|
+
messageId,
|
|
1227
|
+
triggerSnapshot,
|
|
1228
|
+
threadRootId,
|
|
1229
|
+
thread,
|
|
1230
|
+
botLoopProtection,
|
|
1231
|
+
effectiveGroupAllowFrom,
|
|
1232
|
+
effectiveRoomUsers,
|
|
1233
|
+
} = resolvedIngressResult;
|
|
1234
|
+
|
|
1235
|
+
// Keep the per-room ingress gate focused on ordering-sensitive state updates.
|
|
1236
|
+
// Prompt/session enrichment below can run concurrently after the history snapshot is fixed.
|
|
1237
|
+
const replyToEventId = resolveMatrixReplyToEventId(event.content as RoomMessageEventContent);
|
|
1238
|
+
const threadTarget = thread.threadId;
|
|
1239
|
+
const isRoomContextSenderAllowed = (contextSenderId?: string): boolean => {
|
|
1240
|
+
if (!isRoom || !contextSenderId) {
|
|
1241
|
+
return true;
|
|
1242
|
+
}
|
|
1243
|
+
if (effectiveRoomUsers.length > 0) {
|
|
1244
|
+
return resolveMatrixAllowListMatch({
|
|
1245
|
+
allowList: effectiveRoomUsers,
|
|
1246
|
+
userId: contextSenderId,
|
|
1247
|
+
}).allowed;
|
|
1248
|
+
}
|
|
1249
|
+
if (groupPolicy === "allowlist" && effectiveGroupAllowFrom.length > 0) {
|
|
1250
|
+
return resolveMatrixAllowListMatch({
|
|
1251
|
+
allowList: effectiveGroupAllowFrom,
|
|
1252
|
+
userId: contextSenderId,
|
|
1253
|
+
}).allowed;
|
|
1254
|
+
}
|
|
1255
|
+
return true;
|
|
1256
|
+
};
|
|
1257
|
+
const shouldIncludeRoomContextSender = (
|
|
1258
|
+
kind: "thread" | "quote" | "history",
|
|
1259
|
+
contextSenderId?: string,
|
|
1260
|
+
): boolean =>
|
|
1261
|
+
evaluateSupplementalContextVisibility({
|
|
1262
|
+
mode: contextVisibilityMode,
|
|
1263
|
+
kind,
|
|
1264
|
+
senderAllowed: isRoomContextSenderAllowed(contextSenderId),
|
|
1265
|
+
}).include;
|
|
1266
|
+
let threadContext = threadRootId
|
|
1267
|
+
? await resolveThreadContext({ roomId, threadRootId })
|
|
1268
|
+
: undefined;
|
|
1269
|
+
let threadContextBlockedByPolicy = false;
|
|
1270
|
+
if (
|
|
1271
|
+
threadContext?.senderId &&
|
|
1272
|
+
!shouldIncludeRoomContextSender("thread", threadContext.senderId)
|
|
1273
|
+
) {
|
|
1274
|
+
logVerboseMessage(`matrix: drop thread root context (mode=${contextVisibilityMode})`);
|
|
1275
|
+
threadContextBlockedByPolicy = true;
|
|
1276
|
+
threadContext = undefined;
|
|
1277
|
+
}
|
|
1278
|
+
let replyContext: Awaited<ReturnType<typeof resolveReplyContext>> | undefined;
|
|
1279
|
+
if (replyToEventId && replyToEventId === threadRootId && threadContext?.summary) {
|
|
1280
|
+
replyContext = {
|
|
1281
|
+
replyToBody: threadContext.summary,
|
|
1282
|
+
replyToSender: threadContext.senderLabel,
|
|
1283
|
+
replyToSenderId: threadContext.senderId,
|
|
1284
|
+
};
|
|
1285
|
+
} else if (
|
|
1286
|
+
replyToEventId &&
|
|
1287
|
+
replyToEventId === threadRootId &&
|
|
1288
|
+
threadContextBlockedByPolicy
|
|
1289
|
+
) {
|
|
1290
|
+
replyContext = await resolveReplyContext({ roomId, eventId: replyToEventId });
|
|
1291
|
+
} else {
|
|
1292
|
+
replyContext = replyToEventId
|
|
1293
|
+
? await resolveReplyContext({ roomId, eventId: replyToEventId })
|
|
1294
|
+
: undefined;
|
|
1295
|
+
}
|
|
1296
|
+
if (
|
|
1297
|
+
replyContext?.replyToSenderId &&
|
|
1298
|
+
!shouldIncludeRoomContextSender("quote", replyContext.replyToSenderId)
|
|
1299
|
+
) {
|
|
1300
|
+
logVerboseMessage(`matrix: drop reply context (mode=${contextVisibilityMode})`);
|
|
1301
|
+
replyContext = undefined;
|
|
1302
|
+
}
|
|
1303
|
+
const roomInfo = isRoom ? await getRoomInfo(roomId) : undefined;
|
|
1304
|
+
const roomName = roomInfo?.name;
|
|
1305
|
+
const envelopeFrom = isDirectMessage ? senderName : (roomName ?? roomId);
|
|
1306
|
+
const textWithId = `${bodyText}\n[matrix event id: ${messageId} room: ${roomId}]`;
|
|
1307
|
+
const storePath = core.channel.session.resolveStorePath(cfg.session?.store, {
|
|
1308
|
+
agentId: _route.agentId,
|
|
1309
|
+
});
|
|
1310
|
+
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(cfg);
|
|
1311
|
+
const previousTimestamp = core.channel.session.readSessionUpdatedAt({
|
|
1312
|
+
storePath,
|
|
1313
|
+
sessionKey: _route.sessionKey,
|
|
1314
|
+
});
|
|
1315
|
+
const sharedDmNoticeSessionKey = threadTarget
|
|
1316
|
+
? _route.mainSessionKey || _route.sessionKey
|
|
1317
|
+
: _route.sessionKey;
|
|
1318
|
+
const sharedDmContextNotice = isDirectMessage
|
|
1319
|
+
? hasExplicitSessionBinding
|
|
1320
|
+
? null
|
|
1321
|
+
: resolveMatrixSharedDmContextNotice({
|
|
1322
|
+
storePath,
|
|
1323
|
+
sessionKey: sharedDmNoticeSessionKey,
|
|
1324
|
+
roomId,
|
|
1325
|
+
accountId: _route.accountId,
|
|
1326
|
+
dmSessionScope,
|
|
1327
|
+
sentRooms: sharedDmContextNoticeRooms,
|
|
1328
|
+
logVerboseMessage,
|
|
1329
|
+
})
|
|
1330
|
+
: null;
|
|
1331
|
+
const body = core.channel.reply.formatAgentEnvelope({
|
|
1332
|
+
channel: "Matrix",
|
|
1333
|
+
from: envelopeFrom,
|
|
1334
|
+
timestamp: eventTs ?? undefined,
|
|
1335
|
+
previousTimestamp,
|
|
1336
|
+
envelope: envelopeOptions,
|
|
1337
|
+
body: textWithId,
|
|
1338
|
+
});
|
|
1339
|
+
const groupSystemPrompt = normalizeOptionalString(roomConfig?.systemPrompt);
|
|
1340
|
+
const ctxPayload = core.channel.reply.finalizeInboundContext({
|
|
1341
|
+
Body: body,
|
|
1342
|
+
RawBody: bodyText,
|
|
1343
|
+
CommandBody: commandBodyText,
|
|
1344
|
+
BodyForAgent: bodyText,
|
|
1345
|
+
BodyForCommands: commandBodyText,
|
|
1346
|
+
InboundHistory: inboundHistory && inboundHistory.length > 0 ? inboundHistory : undefined,
|
|
1347
|
+
From: isDirectMessage ? `matrix:${senderId}` : `matrix:channel:${roomId}`,
|
|
1348
|
+
To: `room:${roomId}`,
|
|
1349
|
+
SessionKey: _route.sessionKey,
|
|
1350
|
+
AccountId: _route.accountId,
|
|
1351
|
+
ChatType: isDirectMessage ? "direct" : "channel",
|
|
1352
|
+
ConversationLabel: envelopeFrom,
|
|
1353
|
+
SenderName: senderName,
|
|
1354
|
+
SenderId: senderId,
|
|
1355
|
+
SenderUsername: senderId.split(":")[0]?.replace(/^@/, ""),
|
|
1356
|
+
GroupSubject: isRoom ? (roomName ?? roomId) : undefined,
|
|
1357
|
+
GroupId: isRoom ? roomId : undefined,
|
|
1358
|
+
GroupChannel: isRoom ? roomId : undefined,
|
|
1359
|
+
GroupSystemPrompt: isRoom ? groupSystemPrompt : undefined,
|
|
1360
|
+
Provider: "matrix" as const,
|
|
1361
|
+
Surface: "matrix" as const,
|
|
1362
|
+
WasMentioned: isRoom ? wasMentioned : undefined,
|
|
1363
|
+
MessageSid: messageId,
|
|
1364
|
+
ReplyToId: threadTarget ? undefined : (replyToEventId ?? undefined),
|
|
1365
|
+
ReplyToBody: replyContext?.replyToBody,
|
|
1366
|
+
ReplyToSender: replyContext?.replyToSender,
|
|
1367
|
+
MessageThreadId: threadTarget,
|
|
1368
|
+
ThreadStarterBody: threadContext?.threadStarterBody,
|
|
1369
|
+
Timestamp: eventTs ?? undefined,
|
|
1370
|
+
MediaPath: media?.path,
|
|
1371
|
+
MediaType: media?.contentType,
|
|
1372
|
+
MediaUrl: media?.path,
|
|
1373
|
+
...locationPayload?.context,
|
|
1374
|
+
CommandAuthorized: commandAuthorized,
|
|
1375
|
+
CommandSource: "text" as const,
|
|
1376
|
+
NativeChannelId: roomId,
|
|
1377
|
+
NativeDirectUserId: isDirectMessage ? senderId : undefined,
|
|
1378
|
+
OriginatingChannel: "matrix" as const,
|
|
1379
|
+
OriginatingTo: `room:${roomId}`,
|
|
1380
|
+
});
|
|
1381
|
+
|
|
1382
|
+
const preview = bodyText.slice(0, 200).replace(/\n/g, "\\n");
|
|
1383
|
+
logVerboseMessage(`matrix inbound: room=${roomId} from=${senderId} preview="${preview}"`);
|
|
1384
|
+
|
|
1385
|
+
const replyTarget = ctxPayload.To;
|
|
1386
|
+
if (!replyTarget) {
|
|
1387
|
+
runtime.error?.("matrix: missing reply target");
|
|
1388
|
+
return;
|
|
1389
|
+
}
|
|
1390
|
+
|
|
1391
|
+
const { ackReaction, ackReactionScope: ackScope } = resolveMatrixAckReactionConfig({
|
|
1392
|
+
cfg,
|
|
1393
|
+
agentId: _route.agentId,
|
|
1394
|
+
accountId,
|
|
1395
|
+
});
|
|
1396
|
+
const shouldAckReaction = () =>
|
|
1397
|
+
Boolean(
|
|
1398
|
+
ackReaction &&
|
|
1399
|
+
core.channel.reactions.shouldAckReaction({
|
|
1400
|
+
scope: ackScope,
|
|
1401
|
+
isDirect: isDirectMessage,
|
|
1402
|
+
isGroup: isRoom,
|
|
1403
|
+
isMentionableGroup: isRoom,
|
|
1404
|
+
requireMention: shouldRequireMention,
|
|
1405
|
+
canDetectMention,
|
|
1406
|
+
effectiveWasMentioned: wasMentioned || shouldBypassMention,
|
|
1407
|
+
shouldBypassMention,
|
|
1408
|
+
}),
|
|
1409
|
+
);
|
|
1410
|
+
if (shouldAckReaction() && messageId) {
|
|
1411
|
+
loadMatrixSendModule()
|
|
1412
|
+
.then(({ reactMatrixMessage }) =>
|
|
1413
|
+
reactMatrixMessage(roomId, messageId, ackReaction, client),
|
|
1414
|
+
)
|
|
1415
|
+
.catch((err) => {
|
|
1416
|
+
logVerboseMessage(`matrix react failed for room ${roomId}: ${String(err)}`);
|
|
1417
|
+
});
|
|
1418
|
+
}
|
|
1419
|
+
|
|
1420
|
+
if (messageId) {
|
|
1421
|
+
loadMatrixSendModule()
|
|
1422
|
+
.then(({ sendReadReceiptMatrix }) => sendReadReceiptMatrix(roomId, messageId, client))
|
|
1423
|
+
.catch((err) => {
|
|
1424
|
+
logVerboseMessage(
|
|
1425
|
+
`matrix: read receipt failed room=${roomId} id=${messageId}: ${String(err)}`,
|
|
1426
|
+
);
|
|
1427
|
+
});
|
|
1428
|
+
}
|
|
1429
|
+
|
|
1430
|
+
const tableMode = core.channel.text.resolveMarkdownTableMode({
|
|
1431
|
+
cfg,
|
|
1432
|
+
channel: "matrix",
|
|
1433
|
+
accountId: _route.accountId,
|
|
1434
|
+
});
|
|
1435
|
+
const mediaLocalRoots = getAgentScopedMediaLocalRoots(cfg, _route.agentId);
|
|
1436
|
+
let finalReplyDeliveryFailed = false;
|
|
1437
|
+
let nonFinalReplyDeliveryFailed = false;
|
|
1438
|
+
let retryableReplyDeliveryFailed = false;
|
|
1439
|
+
const { onModelSelected, ...prefixOptions } = createReplyPrefixOptions({
|
|
1440
|
+
cfg,
|
|
1441
|
+
agentId: _route.agentId,
|
|
1442
|
+
channel: "matrix",
|
|
1443
|
+
accountId: _route.accountId,
|
|
1444
|
+
});
|
|
1445
|
+
const typingCallbacks = createTypingCallbacks({
|
|
1446
|
+
start: async () => {
|
|
1447
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
1448
|
+
await sendTypingMatrix(roomId, true, undefined, client);
|
|
1449
|
+
},
|
|
1450
|
+
stop: async () => {
|
|
1451
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
1452
|
+
await sendTypingMatrix(roomId, false, undefined, client);
|
|
1453
|
+
},
|
|
1454
|
+
onStartError: (err) => {
|
|
1455
|
+
logTypingFailure({
|
|
1456
|
+
log: logVerboseMessage,
|
|
1457
|
+
channel: "matrix",
|
|
1458
|
+
action: "start",
|
|
1459
|
+
target: roomId,
|
|
1460
|
+
error: err,
|
|
1461
|
+
});
|
|
1462
|
+
},
|
|
1463
|
+
onStopError: (err) => {
|
|
1464
|
+
logTypingFailure({
|
|
1465
|
+
log: logVerboseMessage,
|
|
1466
|
+
channel: "matrix",
|
|
1467
|
+
action: "stop",
|
|
1468
|
+
target: roomId,
|
|
1469
|
+
error: err,
|
|
1470
|
+
});
|
|
1471
|
+
},
|
|
1472
|
+
});
|
|
1473
|
+
const draftStreamingEnabled = streaming !== "off";
|
|
1474
|
+
const quietDraftStreaming = streaming === "quiet" || streaming === "progress";
|
|
1475
|
+
const progressDraftStreaming = streaming === "progress";
|
|
1476
|
+
const draftReplyToId = replyToMode !== "off" && !threadTarget ? messageId : undefined;
|
|
1477
|
+
const draftStream: MatrixDraftStreamHandle | undefined = draftStreamingEnabled
|
|
1478
|
+
? await loadMatrixDraftStream().then(({ createMatrixDraftStream }) =>
|
|
1479
|
+
createMatrixDraftStream({
|
|
1480
|
+
roomId,
|
|
1481
|
+
client,
|
|
1482
|
+
cfg,
|
|
1483
|
+
mode: quietDraftStreaming ? "quiet" : "partial",
|
|
1484
|
+
threadId: threadTarget,
|
|
1485
|
+
replyToId: draftReplyToId,
|
|
1486
|
+
preserveReplyId: replyToMode === "all",
|
|
1487
|
+
accountId: _route.accountId,
|
|
1488
|
+
log: logVerboseMessage,
|
|
1489
|
+
}),
|
|
1490
|
+
)
|
|
1491
|
+
: undefined;
|
|
1492
|
+
draftStreamRef = draftStream;
|
|
1493
|
+
const shouldStreamPreviewToolProgress = Boolean(draftStream) && previewToolProgressEnabled;
|
|
1494
|
+
const shouldSuppressDefaultToolProgressMessages =
|
|
1495
|
+
Boolean(draftStream) &&
|
|
1496
|
+
(shouldStreamPreviewToolProgress || params.streaming === "progress");
|
|
1497
|
+
type PendingDraftBoundary = {
|
|
1498
|
+
messageGeneration: number;
|
|
1499
|
+
endOffset: number;
|
|
1500
|
+
};
|
|
1501
|
+
// Track the current draft block start plus any queued block-end offsets
|
|
1502
|
+
// inside the model's cumulative partial text so multiple block
|
|
1503
|
+
// boundaries can drain in order even when Matrix delivery lags behind.
|
|
1504
|
+
let currentDraftMessageGeneration = 0;
|
|
1505
|
+
let currentDraftBlockOffset = 0;
|
|
1506
|
+
let latestDraftFullText = "";
|
|
1507
|
+
const pendingDraftBoundaries: PendingDraftBoundary[] = [];
|
|
1508
|
+
const latestQueuedDraftBoundaryOffsets = new Map<number, number>();
|
|
1509
|
+
let currentDraftReplyToId = draftReplyToId;
|
|
1510
|
+
let previewToolProgressSuppressed = false;
|
|
1511
|
+
let previewToolProgressLines: Array<string | ChannelProgressDraftLine> = [];
|
|
1512
|
+
const progressConfigEntry = params.accountConfig ?? cfg.channels?.matrix;
|
|
1513
|
+
const progressSeed = `${_route.accountId}:${roomId}`;
|
|
1514
|
+
// Set after the first final payload consumes or discards the draft event
|
|
1515
|
+
// so subsequent finals go through normal delivery.
|
|
1516
|
+
|
|
1517
|
+
const renderProgressDraft = () => {
|
|
1518
|
+
if (!draftStream || !progressDraftStreaming) {
|
|
1519
|
+
return;
|
|
1520
|
+
}
|
|
1521
|
+
const previewText = formatChannelProgressDraftText({
|
|
1522
|
+
entry: progressConfigEntry,
|
|
1523
|
+
lines: previewToolProgressLines,
|
|
1524
|
+
seed: progressSeed,
|
|
1525
|
+
formatLine: formatMatrixToolProgressMarkdownCode,
|
|
1526
|
+
bullet: "-",
|
|
1527
|
+
});
|
|
1528
|
+
if (!previewText) {
|
|
1529
|
+
return;
|
|
1530
|
+
}
|
|
1531
|
+
draftStream.update(previewText);
|
|
1532
|
+
};
|
|
1533
|
+
const progressDraftGate = createChannelProgressDraftGate({
|
|
1534
|
+
onStart: renderProgressDraft,
|
|
1535
|
+
});
|
|
1536
|
+
|
|
1537
|
+
const pushPreviewToolProgress = async (
|
|
1538
|
+
line?: string | ChannelProgressDraftLine,
|
|
1539
|
+
options?: { toolName?: string },
|
|
1540
|
+
) => {
|
|
1541
|
+
if (!draftStream) {
|
|
1542
|
+
return;
|
|
1543
|
+
}
|
|
1544
|
+
if (
|
|
1545
|
+
options?.toolName !== undefined &&
|
|
1546
|
+
!isChannelProgressDraftWorkToolName(options.toolName)
|
|
1547
|
+
) {
|
|
1548
|
+
return;
|
|
1549
|
+
}
|
|
1550
|
+
const normalized = normalizeChannelProgressDraftLineIdentity(line);
|
|
1551
|
+
const progressLine = typeof line === "object" && line !== undefined ? line : normalized;
|
|
1552
|
+
if (!progressDraftStreaming) {
|
|
1553
|
+
if (!shouldStreamPreviewToolProgress || previewToolProgressSuppressed || !normalized) {
|
|
1554
|
+
return;
|
|
1555
|
+
}
|
|
1556
|
+
const nextLines = mergeChannelProgressDraftLine(previewToolProgressLines, progressLine, {
|
|
1557
|
+
maxLines: resolveChannelProgressDraftMaxLines(progressConfigEntry),
|
|
1558
|
+
});
|
|
1559
|
+
if (nextLines === previewToolProgressLines) {
|
|
1560
|
+
return;
|
|
1561
|
+
}
|
|
1562
|
+
previewToolProgressLines = nextLines;
|
|
1563
|
+
draftStream.update(
|
|
1564
|
+
formatChannelProgressDraftText({
|
|
1565
|
+
entry: progressConfigEntry,
|
|
1566
|
+
lines: previewToolProgressLines,
|
|
1567
|
+
seed: progressSeed,
|
|
1568
|
+
formatLine: formatMatrixToolProgressMarkdownCode,
|
|
1569
|
+
bullet: "-",
|
|
1570
|
+
}),
|
|
1571
|
+
);
|
|
1572
|
+
return;
|
|
1573
|
+
}
|
|
1574
|
+
if (shouldStreamPreviewToolProgress && !previewToolProgressSuppressed && normalized) {
|
|
1575
|
+
previewToolProgressLines = mergeChannelProgressDraftLine(
|
|
1576
|
+
previewToolProgressLines,
|
|
1577
|
+
progressLine,
|
|
1578
|
+
{
|
|
1579
|
+
maxLines: resolveChannelProgressDraftMaxLines(progressConfigEntry),
|
|
1580
|
+
},
|
|
1581
|
+
);
|
|
1582
|
+
}
|
|
1583
|
+
const alreadyStarted = progressDraftGate.hasStarted;
|
|
1584
|
+
await progressDraftGate.noteWork();
|
|
1585
|
+
if (alreadyStarted && progressDraftGate.hasStarted) {
|
|
1586
|
+
renderProgressDraft();
|
|
1587
|
+
}
|
|
1588
|
+
};
|
|
1589
|
+
|
|
1590
|
+
const suppressPreviewToolProgressForAnswerText = (text: string | undefined) => {
|
|
1591
|
+
if (!text?.trim()) {
|
|
1592
|
+
return;
|
|
1593
|
+
}
|
|
1594
|
+
previewToolProgressSuppressed = true;
|
|
1595
|
+
previewToolProgressLines = [];
|
|
1596
|
+
};
|
|
1597
|
+
|
|
1598
|
+
const resetPreviewToolProgress = () => {
|
|
1599
|
+
previewToolProgressSuppressed = false;
|
|
1600
|
+
previewToolProgressLines = [];
|
|
1601
|
+
};
|
|
1602
|
+
|
|
1603
|
+
const buildPreviewToolProgressReplyOptions = (): Partial<GetReplyOptions> => {
|
|
1604
|
+
if (!shouldSuppressDefaultToolProgressMessages) {
|
|
1605
|
+
return {};
|
|
1606
|
+
}
|
|
1607
|
+
const options: Partial<GetReplyOptions> = {
|
|
1608
|
+
suppressDefaultToolProgressMessages: true,
|
|
1609
|
+
};
|
|
1610
|
+
if (!shouldStreamPreviewToolProgress) {
|
|
1611
|
+
return options;
|
|
1612
|
+
}
|
|
1613
|
+
return {
|
|
1614
|
+
...options,
|
|
1615
|
+
onToolStart: async (payload) => {
|
|
1616
|
+
const toolName = payload.name?.trim();
|
|
1617
|
+
await pushPreviewToolProgress(
|
|
1618
|
+
formatChannelProgressDraftLineForEntry(
|
|
1619
|
+
progressConfigEntry,
|
|
1620
|
+
{
|
|
1621
|
+
event: "tool",
|
|
1622
|
+
name: toolName,
|
|
1623
|
+
phase: payload.phase,
|
|
1624
|
+
args: payload.args,
|
|
1625
|
+
},
|
|
1626
|
+
payload.detailMode ? { detailMode: payload.detailMode } : undefined,
|
|
1627
|
+
),
|
|
1628
|
+
{ toolName },
|
|
1629
|
+
);
|
|
1630
|
+
},
|
|
1631
|
+
onItemEvent: async (payload) => {
|
|
1632
|
+
await pushPreviewToolProgress(
|
|
1633
|
+
buildChannelProgressDraftLineForEntry(progressConfigEntry, {
|
|
1634
|
+
event: "item",
|
|
1635
|
+
itemId: payload.itemId,
|
|
1636
|
+
itemKind: payload.kind,
|
|
1637
|
+
title: payload.title,
|
|
1638
|
+
name: payload.name,
|
|
1639
|
+
phase: payload.phase,
|
|
1640
|
+
status: payload.status,
|
|
1641
|
+
summary: payload.summary,
|
|
1642
|
+
progressText: payload.progressText,
|
|
1643
|
+
meta: payload.meta,
|
|
1644
|
+
}),
|
|
1645
|
+
);
|
|
1646
|
+
},
|
|
1647
|
+
onPlanUpdate: async (payload) => {
|
|
1648
|
+
if (payload.phase !== "update") {
|
|
1649
|
+
return;
|
|
1650
|
+
}
|
|
1651
|
+
await pushPreviewToolProgress(
|
|
1652
|
+
formatChannelProgressDraftLine({
|
|
1653
|
+
event: "plan",
|
|
1654
|
+
phase: payload.phase,
|
|
1655
|
+
title: payload.title,
|
|
1656
|
+
explanation: payload.explanation,
|
|
1657
|
+
steps: payload.steps,
|
|
1658
|
+
}),
|
|
1659
|
+
);
|
|
1660
|
+
},
|
|
1661
|
+
onApprovalEvent: async (payload) => {
|
|
1662
|
+
if (payload.phase !== "requested") {
|
|
1663
|
+
return;
|
|
1664
|
+
}
|
|
1665
|
+
await pushPreviewToolProgress(
|
|
1666
|
+
formatChannelProgressDraftLine({
|
|
1667
|
+
event: "approval",
|
|
1668
|
+
phase: payload.phase,
|
|
1669
|
+
title: payload.title,
|
|
1670
|
+
command: payload.command,
|
|
1671
|
+
reason: payload.reason,
|
|
1672
|
+
message: payload.message,
|
|
1673
|
+
}),
|
|
1674
|
+
);
|
|
1675
|
+
},
|
|
1676
|
+
onCommandOutput: async (payload) => {
|
|
1677
|
+
if (payload.phase !== "end") {
|
|
1678
|
+
return;
|
|
1679
|
+
}
|
|
1680
|
+
await pushPreviewToolProgress(
|
|
1681
|
+
formatChannelProgressDraftLine({
|
|
1682
|
+
event: "command-output",
|
|
1683
|
+
phase: payload.phase,
|
|
1684
|
+
title: payload.title,
|
|
1685
|
+
name: payload.name,
|
|
1686
|
+
status: payload.status,
|
|
1687
|
+
exitCode: payload.exitCode,
|
|
1688
|
+
}),
|
|
1689
|
+
);
|
|
1690
|
+
},
|
|
1691
|
+
onPatchSummary: async (payload) => {
|
|
1692
|
+
if (payload.phase !== "end") {
|
|
1693
|
+
return;
|
|
1694
|
+
}
|
|
1695
|
+
await pushPreviewToolProgress(
|
|
1696
|
+
formatChannelProgressDraftLine({
|
|
1697
|
+
event: "patch",
|
|
1698
|
+
phase: payload.phase,
|
|
1699
|
+
title: payload.title,
|
|
1700
|
+
name: payload.name,
|
|
1701
|
+
added: payload.added,
|
|
1702
|
+
modified: payload.modified,
|
|
1703
|
+
deleted: payload.deleted,
|
|
1704
|
+
summary: payload.summary,
|
|
1705
|
+
}),
|
|
1706
|
+
);
|
|
1707
|
+
},
|
|
1708
|
+
};
|
|
1709
|
+
};
|
|
1710
|
+
|
|
1711
|
+
const getDisplayableDraftText = () => {
|
|
1712
|
+
const nextDraftBoundaryOffset = pendingDraftBoundaries.find(
|
|
1713
|
+
(boundary) => boundary.messageGeneration === currentDraftMessageGeneration,
|
|
1714
|
+
)?.endOffset;
|
|
1715
|
+
if (nextDraftBoundaryOffset === undefined) {
|
|
1716
|
+
return latestDraftFullText.slice(currentDraftBlockOffset);
|
|
1717
|
+
}
|
|
1718
|
+
return latestDraftFullText.slice(currentDraftBlockOffset, nextDraftBoundaryOffset);
|
|
1719
|
+
};
|
|
1720
|
+
|
|
1721
|
+
const updateDraftFromLatestFullText = () => {
|
|
1722
|
+
const blockText = getDisplayableDraftText();
|
|
1723
|
+
if (blockText) {
|
|
1724
|
+
draftStream?.update(blockText);
|
|
1725
|
+
}
|
|
1726
|
+
};
|
|
1727
|
+
|
|
1728
|
+
const queueDraftBlockBoundary = (payload: ReplyPayload, context?: BlockReplyContext) => {
|
|
1729
|
+
const payloadTextLength = payload.text?.length ?? 0;
|
|
1730
|
+
const messageGeneration = context?.assistantMessageIndex ?? currentDraftMessageGeneration;
|
|
1731
|
+
const lastQueuedDraftBoundaryOffset =
|
|
1732
|
+
latestQueuedDraftBoundaryOffsets.get(messageGeneration) ?? 0;
|
|
1733
|
+
// Logical block boundaries must follow emitted block text, not whichever
|
|
1734
|
+
// later partial preview has already arrived by the time the async
|
|
1735
|
+
// boundary callback drains.
|
|
1736
|
+
const nextDraftBoundaryOffset = lastQueuedDraftBoundaryOffset + payloadTextLength;
|
|
1737
|
+
latestQueuedDraftBoundaryOffsets.set(messageGeneration, nextDraftBoundaryOffset);
|
|
1738
|
+
pendingDraftBoundaries.push({
|
|
1739
|
+
messageGeneration,
|
|
1740
|
+
endOffset: nextDraftBoundaryOffset,
|
|
1741
|
+
});
|
|
1742
|
+
};
|
|
1743
|
+
|
|
1744
|
+
const advanceDraftBlockBoundary = (options?: { fallbackToLatestEnd?: boolean }) => {
|
|
1745
|
+
const completedBoundary = pendingDraftBoundaries.shift();
|
|
1746
|
+
if (completedBoundary) {
|
|
1747
|
+
if (
|
|
1748
|
+
!pendingDraftBoundaries.some(
|
|
1749
|
+
(entry) => entry.messageGeneration === completedBoundary.messageGeneration,
|
|
1750
|
+
)
|
|
1751
|
+
) {
|
|
1752
|
+
latestQueuedDraftBoundaryOffsets.delete(completedBoundary.messageGeneration);
|
|
1753
|
+
}
|
|
1754
|
+
if (completedBoundary.messageGeneration === currentDraftMessageGeneration) {
|
|
1755
|
+
currentDraftBlockOffset = completedBoundary.endOffset;
|
|
1756
|
+
}
|
|
1757
|
+
return;
|
|
1758
|
+
}
|
|
1759
|
+
if (options?.fallbackToLatestEnd) {
|
|
1760
|
+
currentDraftBlockOffset = latestDraftFullText.length;
|
|
1761
|
+
}
|
|
1762
|
+
};
|
|
1763
|
+
|
|
1764
|
+
const resetDraftBlockOffsets = () => {
|
|
1765
|
+
currentDraftMessageGeneration += 1;
|
|
1766
|
+
currentDraftBlockOffset = 0;
|
|
1767
|
+
latestDraftFullText = "";
|
|
1768
|
+
};
|
|
1769
|
+
|
|
1770
|
+
const { dispatcher, replyOptions, markDispatchIdle, markRunComplete } =
|
|
1771
|
+
core.channel.reply.createReplyDispatcherWithTyping({
|
|
1772
|
+
...prefixOptions,
|
|
1773
|
+
humanDelay: core.channel.reply.resolveHumanDelayConfig(cfg, _route.agentId),
|
|
1774
|
+
deliver: async (payload: ReplyPayload, info: { kind: string }) => {
|
|
1775
|
+
if (draftStream && info.kind !== "tool" && !payload.isCompactionNotice) {
|
|
1776
|
+
const hasMedia = Boolean(payload.mediaUrl) || (payload.mediaUrls?.length ?? 0) > 0;
|
|
1777
|
+
const ttsSupplement = getReplyPayloadTtsSupplement(payload);
|
|
1778
|
+
const fallbackPayload =
|
|
1779
|
+
ttsSupplement &&
|
|
1780
|
+
ttsSupplement.visibleTextAlreadyDelivered !== true &&
|
|
1781
|
+
!payload.text?.trim()
|
|
1782
|
+
? { ...payload, text: ttsSupplement.spokenText }
|
|
1783
|
+
: payload;
|
|
1784
|
+
|
|
1785
|
+
if (draftConsumed) {
|
|
1786
|
+
await draftStream.discardPending();
|
|
1787
|
+
await deliverMatrixReplies({
|
|
1788
|
+
cfg,
|
|
1789
|
+
replies: [fallbackPayload],
|
|
1790
|
+
roomId,
|
|
1791
|
+
client,
|
|
1792
|
+
runtime,
|
|
1793
|
+
textLimit,
|
|
1794
|
+
replyToMode,
|
|
1795
|
+
threadId: threadTarget,
|
|
1796
|
+
accountId: _route.accountId,
|
|
1797
|
+
mediaLocalRoots,
|
|
1798
|
+
tableMode,
|
|
1799
|
+
});
|
|
1800
|
+
return;
|
|
1801
|
+
}
|
|
1802
|
+
|
|
1803
|
+
const payloadReplyToId = normalizeOptionalString(payload.replyToId);
|
|
1804
|
+
const payloadReplyMismatch =
|
|
1805
|
+
replyToMode !== "off" &&
|
|
1806
|
+
!threadTarget &&
|
|
1807
|
+
payloadReplyToId !== currentDraftReplyToId;
|
|
1808
|
+
let mustDeliverFinalNormally = draftStream.mustDeliverFinalNormally();
|
|
1809
|
+
const canPotentiallyFinalizeDraft =
|
|
1810
|
+
Boolean(payload.text?.trim()) &&
|
|
1811
|
+
!payload.isError &&
|
|
1812
|
+
!payloadReplyMismatch &&
|
|
1813
|
+
!mustDeliverFinalNormally;
|
|
1814
|
+
|
|
1815
|
+
if (canPotentiallyFinalizeDraft) {
|
|
1816
|
+
await draftStream.stop();
|
|
1817
|
+
mustDeliverFinalNormally = draftStream.mustDeliverFinalNormally();
|
|
1818
|
+
} else {
|
|
1819
|
+
await draftStream.discardPending();
|
|
1820
|
+
}
|
|
1821
|
+
const draftEventId = draftStream.eventId();
|
|
1822
|
+
|
|
1823
|
+
if (
|
|
1824
|
+
draftEventId &&
|
|
1825
|
+
payload.text &&
|
|
1826
|
+
!payload.isError &&
|
|
1827
|
+
!hasMedia &&
|
|
1828
|
+
!payloadReplyMismatch &&
|
|
1829
|
+
!mustDeliverFinalNormally
|
|
1830
|
+
) {
|
|
1831
|
+
const finalPreviewText = payload.text;
|
|
1832
|
+
await deliverWithFinalizableLivePreviewAdapter<
|
|
1833
|
+
ReplyPayload,
|
|
1834
|
+
string,
|
|
1835
|
+
{
|
|
1836
|
+
text: string;
|
|
1837
|
+
finalizeLive: boolean;
|
|
1838
|
+
extraContent?: Record<string, unknown>;
|
|
1839
|
+
}
|
|
1840
|
+
>({
|
|
1841
|
+
kind: "final",
|
|
1842
|
+
payload,
|
|
1843
|
+
adapter: defineFinalizableLivePreviewAdapter({
|
|
1844
|
+
draft: {
|
|
1845
|
+
flush: async () => {},
|
|
1846
|
+
clear: async () => {},
|
|
1847
|
+
discardPending: async () => {},
|
|
1848
|
+
id: () => draftEventId,
|
|
1849
|
+
},
|
|
1850
|
+
buildFinalEdit: () => ({
|
|
1851
|
+
text: finalPreviewText,
|
|
1852
|
+
finalizeLive: !(
|
|
1853
|
+
quietDraftStreaming || !draftStream.matchesPreparedText(finalPreviewText)
|
|
1854
|
+
),
|
|
1855
|
+
...(quietDraftStreaming
|
|
1856
|
+
? { extraContent: buildMatrixFinalizedPreviewContent() }
|
|
1857
|
+
: {}),
|
|
1858
|
+
}),
|
|
1859
|
+
editFinal: async (_draftEventId, edit) => {
|
|
1860
|
+
if (edit.finalizeLive) {
|
|
1861
|
+
if (!(await draftStream.finalizeLive())) {
|
|
1862
|
+
throw new Error("Matrix draft live finalize failed");
|
|
1863
|
+
}
|
|
1864
|
+
return;
|
|
1865
|
+
}
|
|
1866
|
+
const { editMessageMatrix } = await loadMatrixSendModule();
|
|
1867
|
+
await editMessageMatrix(roomId, _draftEventId, edit.text, {
|
|
1868
|
+
client,
|
|
1869
|
+
cfg,
|
|
1870
|
+
threadId: threadTarget,
|
|
1871
|
+
accountId: _route.accountId,
|
|
1872
|
+
extraContent: edit.extraContent,
|
|
1873
|
+
});
|
|
1874
|
+
},
|
|
1875
|
+
createPreviewReceipt: (id): MessageReceipt =>
|
|
1876
|
+
createPreviewMessageReceipt({
|
|
1877
|
+
id,
|
|
1878
|
+
...(threadTarget ? { threadId: threadTarget } : {}),
|
|
1879
|
+
...(currentDraftReplyToId ? { replyToId: currentDraftReplyToId } : {}),
|
|
1880
|
+
}),
|
|
1881
|
+
logPreviewEditFailure: (err) => {
|
|
1882
|
+
logVerboseMessage(`matrix: preview final edit failed: ${String(err)}`);
|
|
1883
|
+
},
|
|
1884
|
+
}),
|
|
1885
|
+
deliverNormally: async () => {
|
|
1886
|
+
await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
1887
|
+
await deliverMatrixReplies({
|
|
1888
|
+
cfg,
|
|
1889
|
+
replies: [fallbackPayload],
|
|
1890
|
+
roomId,
|
|
1891
|
+
client,
|
|
1892
|
+
runtime,
|
|
1893
|
+
textLimit,
|
|
1894
|
+
replyToMode,
|
|
1895
|
+
threadId: threadTarget,
|
|
1896
|
+
accountId: _route.accountId,
|
|
1897
|
+
mediaLocalRoots,
|
|
1898
|
+
tableMode,
|
|
1899
|
+
});
|
|
1900
|
+
},
|
|
1901
|
+
});
|
|
1902
|
+
draftConsumed = true;
|
|
1903
|
+
} else if (draftEventId && hasMedia && !payloadReplyMismatch) {
|
|
1904
|
+
let textEditOk = !mustDeliverFinalNormally;
|
|
1905
|
+
const payloadText = payload.text ?? ttsSupplement?.spokenText;
|
|
1906
|
+
const payloadTextMatchesDraft =
|
|
1907
|
+
typeof payloadText === "string" && draftStream.matchesPreparedText(payloadText);
|
|
1908
|
+
const reusesDraftTextUnchanged =
|
|
1909
|
+
typeof payloadText === "string" &&
|
|
1910
|
+
Boolean(payloadText.trim()) &&
|
|
1911
|
+
payloadTextMatchesDraft;
|
|
1912
|
+
const requiresFinalTextEdit =
|
|
1913
|
+
quietDraftStreaming ||
|
|
1914
|
+
(typeof payloadText === "string" && !payloadTextMatchesDraft);
|
|
1915
|
+
if (textEditOk && payloadText && requiresFinalTextEdit) {
|
|
1916
|
+
const { editMessageMatrix } = await loadMatrixSendModule();
|
|
1917
|
+
textEditOk = await editMessageMatrix(roomId, draftEventId, payloadText, {
|
|
1918
|
+
client,
|
|
1919
|
+
cfg,
|
|
1920
|
+
threadId: threadTarget,
|
|
1921
|
+
accountId: _route.accountId,
|
|
1922
|
+
extraContent: quietDraftStreaming
|
|
1923
|
+
? buildMatrixFinalizedPreviewContent()
|
|
1924
|
+
: undefined,
|
|
1925
|
+
}).then(
|
|
1926
|
+
() => true,
|
|
1927
|
+
() => false,
|
|
1928
|
+
);
|
|
1929
|
+
} else if (textEditOk && reusesDraftTextUnchanged) {
|
|
1930
|
+
textEditOk = await draftStream.finalizeLive();
|
|
1931
|
+
}
|
|
1932
|
+
const reusesDraftAsFinalText = Boolean(payloadText?.trim()) && textEditOk;
|
|
1933
|
+
if (!reusesDraftAsFinalText) {
|
|
1934
|
+
await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
1935
|
+
}
|
|
1936
|
+
const mediaPayload =
|
|
1937
|
+
ttsSupplement && reusesDraftAsFinalText
|
|
1938
|
+
? buildTtsSupplementMediaPayload(payload)
|
|
1939
|
+
: {
|
|
1940
|
+
...payload,
|
|
1941
|
+
text: reusesDraftAsFinalText
|
|
1942
|
+
? undefined
|
|
1943
|
+
: (payload.text ??
|
|
1944
|
+
(ttsSupplement?.visibleTextAlreadyDelivered === true
|
|
1945
|
+
? undefined
|
|
1946
|
+
: ttsSupplement?.spokenText)),
|
|
1947
|
+
};
|
|
1948
|
+
await deliverMatrixReplies({
|
|
1949
|
+
cfg,
|
|
1950
|
+
replies: [mediaPayload],
|
|
1951
|
+
roomId,
|
|
1952
|
+
client,
|
|
1953
|
+
runtime,
|
|
1954
|
+
textLimit,
|
|
1955
|
+
replyToMode,
|
|
1956
|
+
threadId: threadTarget,
|
|
1957
|
+
accountId: _route.accountId,
|
|
1958
|
+
mediaLocalRoots,
|
|
1959
|
+
tableMode,
|
|
1960
|
+
});
|
|
1961
|
+
draftConsumed = true;
|
|
1962
|
+
} else {
|
|
1963
|
+
const draftRedacted =
|
|
1964
|
+
Boolean(draftEventId) &&
|
|
1965
|
+
(payload.isError || payloadReplyMismatch || mustDeliverFinalNormally);
|
|
1966
|
+
if (draftRedacted && draftEventId) {
|
|
1967
|
+
await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
1968
|
+
}
|
|
1969
|
+
const deliveredFallback = await deliverMatrixReplies({
|
|
1970
|
+
cfg,
|
|
1971
|
+
replies: [fallbackPayload],
|
|
1972
|
+
roomId,
|
|
1973
|
+
client,
|
|
1974
|
+
runtime,
|
|
1975
|
+
textLimit,
|
|
1976
|
+
replyToMode,
|
|
1977
|
+
threadId: threadTarget,
|
|
1978
|
+
accountId: _route.accountId,
|
|
1979
|
+
mediaLocalRoots,
|
|
1980
|
+
tableMode,
|
|
1981
|
+
});
|
|
1982
|
+
if (draftRedacted || deliveredFallback) {
|
|
1983
|
+
draftConsumed = true;
|
|
1984
|
+
}
|
|
1985
|
+
}
|
|
1986
|
+
|
|
1987
|
+
if (info.kind === "block") {
|
|
1988
|
+
draftConsumed = false;
|
|
1989
|
+
advanceDraftBlockBoundary({ fallbackToLatestEnd: true });
|
|
1990
|
+
draftStream.reset();
|
|
1991
|
+
currentDraftReplyToId = replyToMode === "all" ? draftReplyToId : undefined;
|
|
1992
|
+
updateDraftFromLatestFullText();
|
|
1993
|
+
|
|
1994
|
+
// Re-assert typing so the user still sees the indicator while
|
|
1995
|
+
// the next block generates.
|
|
1996
|
+
const { sendTypingMatrix } = await loadMatrixSendModule();
|
|
1997
|
+
await sendTypingMatrix(roomId, true, undefined, client).catch(() => {});
|
|
1998
|
+
}
|
|
1999
|
+
} else {
|
|
2000
|
+
await deliverMatrixReplies({
|
|
2001
|
+
cfg,
|
|
2002
|
+
replies: [payload],
|
|
2003
|
+
roomId,
|
|
2004
|
+
client,
|
|
2005
|
+
runtime,
|
|
2006
|
+
textLimit,
|
|
2007
|
+
replyToMode,
|
|
2008
|
+
threadId: threadTarget,
|
|
2009
|
+
accountId: _route.accountId,
|
|
2010
|
+
mediaLocalRoots,
|
|
2011
|
+
tableMode,
|
|
2012
|
+
});
|
|
2013
|
+
}
|
|
2014
|
+
},
|
|
2015
|
+
onError: (err: unknown, info: { kind: "tool" | "block" | "final" }) => {
|
|
2016
|
+
if (err instanceof MatrixRetryableInboundError) {
|
|
2017
|
+
retryableReplyDeliveryFailed = true;
|
|
2018
|
+
}
|
|
2019
|
+
if (info.kind === "final") {
|
|
2020
|
+
finalReplyDeliveryFailed = true;
|
|
2021
|
+
} else {
|
|
2022
|
+
nonFinalReplyDeliveryFailed = true;
|
|
2023
|
+
}
|
|
2024
|
+
if (info.kind === "block") {
|
|
2025
|
+
advanceDraftBlockBoundary({ fallbackToLatestEnd: true });
|
|
2026
|
+
}
|
|
2027
|
+
runtime.error?.(`matrix ${info.kind} reply failed: ${String(err)}`);
|
|
2028
|
+
},
|
|
2029
|
+
onReplyStart: typingCallbacks.onReplyStart,
|
|
2030
|
+
onIdle: typingCallbacks.onIdle,
|
|
2031
|
+
});
|
|
2032
|
+
const pinnedMainDmOwner = isDirectMessage
|
|
2033
|
+
? await (async () => {
|
|
2034
|
+
const livePinnedCfg = core.config.current() as CoreConfig;
|
|
2035
|
+
const livePinnedAllowlists = resolveMatrixAccountAllowlistConfig({
|
|
2036
|
+
cfg: livePinnedCfg,
|
|
2037
|
+
accountId,
|
|
2038
|
+
});
|
|
2039
|
+
const livePinnedDmAllowFrom = await resolveCachedLiveAllowlist({
|
|
2040
|
+
cfg: livePinnedCfg,
|
|
2041
|
+
entries: livePinnedAllowlists.dmAllowFrom,
|
|
2042
|
+
startupResolvedEntries: allowFromResolvedEntries,
|
|
2043
|
+
cache: liveDmAllowlistCache,
|
|
2044
|
+
updateCache: (next) => {
|
|
2045
|
+
liveDmAllowlistCache = next;
|
|
2046
|
+
},
|
|
2047
|
+
});
|
|
2048
|
+
return resolvePinnedMainDmOwnerFromAllowlist({
|
|
2049
|
+
dmScope: livePinnedCfg.session?.dmScope,
|
|
2050
|
+
allowFrom: livePinnedDmAllowFrom,
|
|
2051
|
+
normalizeEntry: normalizeMatrixUserId,
|
|
2052
|
+
});
|
|
2053
|
+
})()
|
|
2054
|
+
: null;
|
|
2055
|
+
|
|
2056
|
+
const inboundLastRouteSessionKey = resolveInboundLastRouteSessionKey({
|
|
2057
|
+
route: _route,
|
|
2058
|
+
sessionKey: _route.sessionKey,
|
|
2059
|
+
});
|
|
2060
|
+
|
|
2061
|
+
const turnResult = await core.channel.turn.run({
|
|
2062
|
+
channel: "matrix",
|
|
2063
|
+
accountId: _route.accountId,
|
|
2064
|
+
raw: event,
|
|
2065
|
+
adapter: {
|
|
2066
|
+
ingest: () => ({
|
|
2067
|
+
id: messageId,
|
|
2068
|
+
rawText: bodyText,
|
|
2069
|
+
textForAgent: ctxPayload.BodyForAgent,
|
|
2070
|
+
textForCommands: ctxPayload.CommandBody,
|
|
2071
|
+
raw: event,
|
|
2072
|
+
}),
|
|
2073
|
+
resolveTurn: () => ({
|
|
2074
|
+
channel: "matrix",
|
|
2075
|
+
accountId: _route.accountId,
|
|
2076
|
+
routeSessionKey: _route.sessionKey,
|
|
2077
|
+
storePath,
|
|
2078
|
+
ctxPayload,
|
|
2079
|
+
recordInboundSession: core.channel.session.recordInboundSession,
|
|
2080
|
+
botLoopProtection,
|
|
2081
|
+
record: {
|
|
2082
|
+
updateLastRoute: isDirectMessage
|
|
2083
|
+
? {
|
|
2084
|
+
sessionKey: inboundLastRouteSessionKey,
|
|
2085
|
+
channel: "matrix",
|
|
2086
|
+
to: `room:${roomId}`,
|
|
2087
|
+
accountId: _route.accountId,
|
|
2088
|
+
mainDmOwnerPin:
|
|
2089
|
+
inboundLastRouteSessionKey === _route.mainSessionKey && pinnedMainDmOwner
|
|
2090
|
+
? {
|
|
2091
|
+
ownerRecipient: pinnedMainDmOwner,
|
|
2092
|
+
senderRecipient: normalizeMatrixUserId(senderId),
|
|
2093
|
+
onSkip: ({
|
|
2094
|
+
ownerRecipient,
|
|
2095
|
+
senderRecipient,
|
|
2096
|
+
}: {
|
|
2097
|
+
ownerRecipient: string;
|
|
2098
|
+
senderRecipient: string;
|
|
2099
|
+
}) => {
|
|
2100
|
+
logVerboseMessage(
|
|
2101
|
+
`matrix: skip main-session last route for ${senderRecipient} (pinned owner ${ownerRecipient})`,
|
|
2102
|
+
);
|
|
2103
|
+
},
|
|
2104
|
+
}
|
|
2105
|
+
: undefined,
|
|
2106
|
+
}
|
|
2107
|
+
: undefined,
|
|
2108
|
+
onRecordError: (err) => {
|
|
2109
|
+
logger.warn("failed updating session meta", {
|
|
2110
|
+
error: String(err),
|
|
2111
|
+
storePath,
|
|
2112
|
+
sessionKey: ctxPayload.SessionKey ?? _route.sessionKey,
|
|
2113
|
+
});
|
|
2114
|
+
},
|
|
2115
|
+
},
|
|
2116
|
+
onPreDispatchFailure: () =>
|
|
2117
|
+
core.channel.reply.settleReplyDispatcher({
|
|
2118
|
+
dispatcher,
|
|
2119
|
+
onSettled: () => {
|
|
2120
|
+
markRunComplete();
|
|
2121
|
+
markDispatchIdle();
|
|
2122
|
+
},
|
|
2123
|
+
}),
|
|
2124
|
+
runDispatch: async () => {
|
|
2125
|
+
if (
|
|
2126
|
+
sharedDmContextNotice &&
|
|
2127
|
+
markTrackedRoomIfFirst(sharedDmContextNoticeRooms, roomId)
|
|
2128
|
+
) {
|
|
2129
|
+
client
|
|
2130
|
+
.sendMessage(roomId, {
|
|
2131
|
+
msgtype: "m.notice",
|
|
2132
|
+
body: sharedDmContextNotice,
|
|
2133
|
+
})
|
|
2134
|
+
.catch((err) => {
|
|
2135
|
+
logVerboseMessage(
|
|
2136
|
+
`matrix: failed sending shared DM session notice room=${roomId}: ${String(err)}`,
|
|
2137
|
+
);
|
|
2138
|
+
});
|
|
2139
|
+
}
|
|
2140
|
+
|
|
2141
|
+
return await core.channel.reply.withReplyDispatcher({
|
|
2142
|
+
dispatcher,
|
|
2143
|
+
onSettled: () => {
|
|
2144
|
+
markDispatchIdle();
|
|
2145
|
+
},
|
|
2146
|
+
run: async () => {
|
|
2147
|
+
try {
|
|
2148
|
+
return await core.channel.reply.dispatchReplyFromConfig({
|
|
2149
|
+
ctx: ctxPayload,
|
|
2150
|
+
cfg,
|
|
2151
|
+
dispatcher,
|
|
2152
|
+
replyOptions: {
|
|
2153
|
+
...replyOptions,
|
|
2154
|
+
skillFilter: roomConfig?.skills,
|
|
2155
|
+
// Keep block streaming enabled when explicitly requested, even
|
|
2156
|
+
// with draft previews on. The draft remains the live preview
|
|
2157
|
+
// for the current assistant block, while block deliveries
|
|
2158
|
+
// finalize completed blocks into their own preserved events.
|
|
2159
|
+
disableBlockStreaming: !blockStreamingEnabled,
|
|
2160
|
+
onPartialReply: draftStream
|
|
2161
|
+
? (payload) => {
|
|
2162
|
+
if (progressDraftStreaming) {
|
|
2163
|
+
return;
|
|
2164
|
+
}
|
|
2165
|
+
latestDraftFullText = payload.text ?? "";
|
|
2166
|
+
suppressPreviewToolProgressForAnswerText(latestDraftFullText);
|
|
2167
|
+
updateDraftFromLatestFullText();
|
|
2168
|
+
}
|
|
2169
|
+
: undefined,
|
|
2170
|
+
onBlockReplyQueued: draftStream
|
|
2171
|
+
? (payload, context) => {
|
|
2172
|
+
if (payload.isCompactionNotice === true) {
|
|
2173
|
+
return;
|
|
2174
|
+
}
|
|
2175
|
+
queueDraftBlockBoundary(payload, context);
|
|
2176
|
+
}
|
|
2177
|
+
: undefined,
|
|
2178
|
+
// Reset draft boundary bookkeeping on assistant message
|
|
2179
|
+
// boundaries so post-tool blocks stream from a fresh
|
|
2180
|
+
// cumulative payload (payload.text resets upstream).
|
|
2181
|
+
onAssistantMessageStart: draftStream
|
|
2182
|
+
? () => {
|
|
2183
|
+
resetDraftBlockOffsets();
|
|
2184
|
+
resetPreviewToolProgress();
|
|
2185
|
+
}
|
|
2186
|
+
: undefined,
|
|
2187
|
+
...buildPreviewToolProgressReplyOptions(),
|
|
2188
|
+
onModelSelected,
|
|
2189
|
+
},
|
|
2190
|
+
});
|
|
2191
|
+
} finally {
|
|
2192
|
+
progressDraftGate.cancel();
|
|
2193
|
+
markRunComplete();
|
|
2194
|
+
}
|
|
2195
|
+
},
|
|
2196
|
+
});
|
|
2197
|
+
},
|
|
2198
|
+
}),
|
|
2199
|
+
},
|
|
2200
|
+
});
|
|
2201
|
+
if (!turnResult.dispatched) {
|
|
2202
|
+
if (
|
|
2203
|
+
turnResult.admission.kind === "drop" &&
|
|
2204
|
+
turnResult.admission.reason === "bot-loop-protection"
|
|
2205
|
+
) {
|
|
2206
|
+
await commitInboundEventIfClaimed();
|
|
2207
|
+
}
|
|
2208
|
+
return;
|
|
2209
|
+
}
|
|
2210
|
+
const { dispatchResult } = turnResult;
|
|
2211
|
+
const { queuedFinal, counts } = dispatchResult;
|
|
2212
|
+
if (finalReplyDeliveryFailed) {
|
|
2213
|
+
if (retryableReplyDeliveryFailed) {
|
|
2214
|
+
logVerboseMessage(
|
|
2215
|
+
`matrix: final reply delivery failed room=${roomId} id=${messageId}; leaving event uncommitted`,
|
|
2216
|
+
);
|
|
2217
|
+
// Explicit retryable failures reopen replay so the same history can be retried.
|
|
2218
|
+
return;
|
|
2219
|
+
}
|
|
2220
|
+
logVerboseMessage(
|
|
2221
|
+
`matrix: final reply delivery failed room=${roomId} id=${messageId}; keeping replay committed`,
|
|
2222
|
+
);
|
|
2223
|
+
await commitInboundEventIfClaimed();
|
|
2224
|
+
return;
|
|
2225
|
+
}
|
|
2226
|
+
if (!queuedFinal && nonFinalReplyDeliveryFailed) {
|
|
2227
|
+
if (retryableReplyDeliveryFailed) {
|
|
2228
|
+
logVerboseMessage(
|
|
2229
|
+
`matrix: non-final reply delivery failed room=${roomId} id=${messageId}; leaving event uncommitted`,
|
|
2230
|
+
);
|
|
2231
|
+
// Explicit retryable failures reopen replay.
|
|
2232
|
+
return;
|
|
2233
|
+
}
|
|
2234
|
+
logVerboseMessage(
|
|
2235
|
+
`matrix: non-final reply delivery failed room=${roomId} id=${messageId}; keeping replay committed`,
|
|
2236
|
+
);
|
|
2237
|
+
await commitInboundEventIfClaimed();
|
|
2238
|
+
return;
|
|
2239
|
+
}
|
|
2240
|
+
// Advance the per-agent watermark now that the reply succeeded (or no reply was needed).
|
|
2241
|
+
// Only advance to the snapshot position — messages added during async processing remain
|
|
2242
|
+
// visible for the next trigger.
|
|
2243
|
+
if (isRoom && triggerSnapshot) {
|
|
2244
|
+
roomHistoryTracker.consumeHistory(_route.agentId, roomId, triggerSnapshot, messageId);
|
|
2245
|
+
}
|
|
2246
|
+
if (!hasFinalInboundReplyDispatch({ queuedFinal, counts })) {
|
|
2247
|
+
await commitInboundEventIfClaimed();
|
|
2248
|
+
return;
|
|
2249
|
+
}
|
|
2250
|
+
const finalCount = counts.final;
|
|
2251
|
+
logVerboseMessage(
|
|
2252
|
+
`matrix: delivered ${finalCount} reply${finalCount === 1 ? "" : "ies"} to ${replyTarget}`,
|
|
2253
|
+
);
|
|
2254
|
+
await commitInboundEventIfClaimed();
|
|
2255
|
+
} catch (err) {
|
|
2256
|
+
runtime.error?.(`matrix handler failed: ${String(err)}`);
|
|
2257
|
+
} finally {
|
|
2258
|
+
// Stop the draft stream timer so partial drafts don't leak if the
|
|
2259
|
+
// model run throws or times out mid-stream.
|
|
2260
|
+
if (draftStreamRef) {
|
|
2261
|
+
const draftEventId = await draftStreamRef.stop().catch(() => undefined);
|
|
2262
|
+
if (draftEventId && !draftConsumed) {
|
|
2263
|
+
await redactMatrixDraftEvent(client, roomId, draftEventId);
|
|
2264
|
+
}
|
|
2265
|
+
}
|
|
2266
|
+
if (claimedInboundEvent && inboundDeduper && eventId) {
|
|
2267
|
+
inboundDeduper.releaseEvent({ roomId, eventId });
|
|
2268
|
+
}
|
|
2269
|
+
}
|
|
2270
|
+
};
|
|
2271
|
+
}
|