Matrix: rebuild plugin migration branch

This commit is contained in:
Gustavo Madeira Santana
2026-03-19 01:53:57 -04:00
parent 513b4869d8
commit 94693f7ff0
244 changed files with 39118 additions and 5946 deletions

View File

@@ -1,10 +1,12 @@
import { describe, expect, it } from "vitest";
import {
formatConversationTarget,
deliveryContextKey,
deliveryContextFromSession,
mergeDeliveryContext,
normalizeDeliveryContext,
normalizeSessionDeliveryFields,
resolveConversationDeliveryTarget,
} from "./delivery-context.js";
describe("delivery context helpers", () => {
@@ -77,6 +79,36 @@ describe("delivery context helpers", () => {
);
});
it("formats channel-aware conversation targets", () => {
expect(formatConversationTarget({ channel: "discord", conversationId: "123" })).toBe(
"channel:123",
);
expect(formatConversationTarget({ channel: "matrix", conversationId: "!room:example" })).toBe(
"room:!room:example",
);
expect(
formatConversationTarget({
channel: "matrix",
conversationId: "$thread",
parentConversationId: "!room:example",
}),
).toBe("room:!room:example");
expect(formatConversationTarget({ channel: "matrix", conversationId: " " })).toBeUndefined();
});
it("resolves delivery targets for Matrix child threads", () => {
expect(
resolveConversationDeliveryTarget({
channel: "matrix",
conversationId: "$thread",
parentConversationId: "!room:example",
}),
).toEqual({
to: "room:!room:example",
threadId: "$thread",
});
});
it("derives delivery context from a session entry", () => {
expect(
deliveryContextFromSession({

View File

@@ -49,6 +49,75 @@ export function normalizeDeliveryContext(context?: DeliveryContext): DeliveryCon
return normalized;
}
export function formatConversationTarget(params: {
channel?: string;
conversationId?: string | number;
parentConversationId?: string | number;
}): string | undefined {
const channel =
typeof params.channel === "string"
? (normalizeMessageChannel(params.channel) ?? params.channel.trim())
: undefined;
const conversationId =
typeof params.conversationId === "number" && Number.isFinite(params.conversationId)
? String(Math.trunc(params.conversationId))
: typeof params.conversationId === "string"
? params.conversationId.trim()
: undefined;
if (!channel || !conversationId) {
return undefined;
}
if (channel === "matrix") {
const parentConversationId =
typeof params.parentConversationId === "number" &&
Number.isFinite(params.parentConversationId)
? String(Math.trunc(params.parentConversationId))
: typeof params.parentConversationId === "string"
? params.parentConversationId.trim()
: undefined;
const roomId =
parentConversationId && parentConversationId !== conversationId
? parentConversationId
: conversationId;
return `room:${roomId}`;
}
return `channel:${conversationId}`;
}
export function resolveConversationDeliveryTarget(params: {
channel?: string;
conversationId?: string | number;
parentConversationId?: string | number;
}): { to?: string; threadId?: string } {
const to = formatConversationTarget(params);
const channel =
typeof params.channel === "string"
? (normalizeMessageChannel(params.channel) ?? params.channel.trim())
: undefined;
const conversationId =
typeof params.conversationId === "number" && Number.isFinite(params.conversationId)
? String(Math.trunc(params.conversationId))
: typeof params.conversationId === "string"
? params.conversationId.trim()
: undefined;
const parentConversationId =
typeof params.parentConversationId === "number" && Number.isFinite(params.parentConversationId)
? String(Math.trunc(params.parentConversationId))
: typeof params.parentConversationId === "string"
? params.parentConversationId.trim()
: undefined;
if (
channel === "matrix" &&
to &&
conversationId &&
parentConversationId &&
parentConversationId !== conversationId
) {
return { to, threadId: conversationId };
}
return { to };
}
export function normalizeSessionDeliveryFields(source?: DeliveryContextSessionSource): {
deliveryContext?: DeliveryContext;
lastChannel?: string;