mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-26 16:41:49 +00:00
Matrix: rebuild plugin migration branch
This commit is contained in:
@@ -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({
|
||||
|
||||
@@ -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;
|
||||
|
||||
Reference in New Issue
Block a user