Matrix: clean monitor history helpers

This commit is contained in:
Gustavo Madeira Santana 2026-03-30 02:37:55 -04:00
parent c7052853f3
commit e03327203c
No known key found for this signature in database
3 changed files with 29 additions and 18 deletions

View File

@ -50,7 +50,11 @@ import type { HistoryEntry } from "./room-history.js";
import { resolveMatrixRoomConfig } from "./rooms.js";
import { resolveMatrixInboundRoute } from "./route.js";
import { createMatrixThreadContextResolver } from "./thread-context.js";
import { resolveMatrixThreadRootId, resolveMatrixThreadTarget } from "./threads.js";
import {
resolveMatrixReplyToEventId,
resolveMatrixThreadRootId,
resolveMatrixThreadTarget,
} from "./threads.js";
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
import { EventType, RelationType } from "./types.js";
import { isMatrixVerificationRoomMessage } from "./verification-utils.js";
@ -806,8 +810,6 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
return {
route: _route,
configuredBinding: _configuredBinding,
runtimeBindingId: _runtimeBindingId,
roomConfig,
isDirectMessage,
isRoom,
@ -832,8 +834,6 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
const {
route: _route,
configuredBinding: _configuredBinding,
runtimeBindingId: _runtimeBindingId,
roomConfig,
isDirectMessage,
isRoom,
@ -854,9 +854,7 @@ export function createMatrixRoomMessageHandler(params: MatrixMonitorHandlerParam
// Keep the per-room ingress gate focused on ordering-sensitive state updates.
// Prompt/session enrichment below can run concurrently after the history snapshot is fixed.
const replyToEventId = (event.content as RoomMessageEventContent)["m.relates_to"]?.[
"m.in_reply_to"
]?.event_id;
const replyToEventId = resolveMatrixReplyToEventId(event.content as RoomMessageEventContent);
const threadTarget = resolveMatrixThreadTarget({
threadReplies,
messageId: _messageId,

View File

@ -63,7 +63,8 @@ export type RoomHistoryTracker = {
) => PreparedTriggerResult;
/**
* Advance the agent's watermark to the snapshot index returned by recordTrigger.
* Advance the agent's watermark to the snapshot index returned by prepareTrigger
* (or the lower-level recordTrigger helper used in tests).
* Only messages appended after that snapshot remain visible on the next trigger.
*/
consumeHistory: (

View File

@ -1,6 +1,22 @@
import type { MatrixRawEvent, RoomMessageEventContent } from "./types.js";
import { RelationType } from "./types.js";
function resolveMatrixRelatedReplyToEventId(relates: unknown): string | undefined {
if (!relates || typeof relates !== "object") {
return undefined;
}
if (
"m.in_reply_to" in relates &&
typeof relates["m.in_reply_to"] === "object" &&
relates["m.in_reply_to"] &&
"event_id" in relates["m.in_reply_to"] &&
typeof relates["m.in_reply_to"].event_id === "string"
) {
return relates["m.in_reply_to"].event_id;
}
return undefined;
}
export function resolveMatrixThreadTarget(params: {
threadReplies: "off" | "inbound" | "always";
messageId: string;
@ -34,15 +50,11 @@ export function resolveMatrixThreadRootId(params: {
if ("event_id" in relates && typeof relates.event_id === "string") {
return relates.event_id;
}
if (
"m.in_reply_to" in relates &&
typeof relates["m.in_reply_to"] === "object" &&
relates["m.in_reply_to"] &&
"event_id" in relates["m.in_reply_to"] &&
typeof relates["m.in_reply_to"].event_id === "string"
) {
return relates["m.in_reply_to"].event_id;
}
return resolveMatrixRelatedReplyToEventId(relates);
}
return undefined;
}
export function resolveMatrixReplyToEventId(content: RoomMessageEventContent): string | undefined {
return resolveMatrixRelatedReplyToEventId(content["m.relates_to"]);
}