mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-06 05:20:43 +00:00
refactor: share matrix qa event matcher
This commit is contained in:
@@ -17,7 +17,7 @@ import type {
|
||||
MessageEventContent,
|
||||
} from "@openclaw/matrix/test-api.js";
|
||||
import { buildMatrixQaMessageContent } from "./client.js";
|
||||
import { normalizeMatrixQaObservedEvent } from "./events.js";
|
||||
import { findMatrixQaObservedEventMatch, normalizeMatrixQaObservedEvent } from "./events.js";
|
||||
import type { MatrixQaObservedEvent } from "./events.js";
|
||||
import type { MatrixQaRoomEventWaitResult } from "./sync.js";
|
||||
|
||||
@@ -186,27 +186,6 @@ async function createMatrixQaE2eeMatrixClient(params: MatrixQaE2eeClientParams)
|
||||
});
|
||||
}
|
||||
|
||||
function findMatrixQaObservedEventMatch(params: {
|
||||
cursorIndex: number;
|
||||
events: MatrixQaObservedEvent[];
|
||||
predicate: (event: MatrixQaObservedEvent) => boolean;
|
||||
roomId: string;
|
||||
}) {
|
||||
for (let index = params.cursorIndex; index < params.events.length; index += 1) {
|
||||
const event = params.events[index];
|
||||
if (event?.roomId !== params.roomId) {
|
||||
continue;
|
||||
}
|
||||
if (params.predicate(event)) {
|
||||
return {
|
||||
event,
|
||||
nextCursorIndex: index + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export async function createMatrixQaE2eeScenarioClient(
|
||||
params: MatrixQaE2eeClientParams & {
|
||||
observedEvents: MatrixQaObservedEvent[];
|
||||
|
||||
@@ -224,3 +224,24 @@ export function normalizeMatrixQaObservedEvent(
|
||||
...(attachment ? { attachment } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export function findMatrixQaObservedEventMatch(params: {
|
||||
cursorIndex: number;
|
||||
events: MatrixQaObservedEvent[];
|
||||
predicate: (event: MatrixQaObservedEvent) => boolean;
|
||||
roomId: string;
|
||||
}) {
|
||||
for (let index = params.cursorIndex; index < params.events.length; index += 1) {
|
||||
const event = params.events[index];
|
||||
if (event?.roomId !== params.roomId) {
|
||||
continue;
|
||||
}
|
||||
if (params.predicate(event)) {
|
||||
return {
|
||||
event,
|
||||
nextCursorIndex: index + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
@@ -1,5 +1,9 @@
|
||||
import type { MatrixQaObservedEvent } from "./events.js";
|
||||
import { normalizeMatrixQaObservedEvent, type MatrixQaRoomEvent } from "./events.js";
|
||||
import {
|
||||
findMatrixQaObservedEventMatch,
|
||||
normalizeMatrixQaObservedEvent,
|
||||
type MatrixQaObservedEvent,
|
||||
type MatrixQaRoomEvent,
|
||||
} from "./events.js";
|
||||
import { requestMatrixJson, type MatrixQaFetchLike } from "./request.js";
|
||||
|
||||
type MatrixQaSyncResponse = {
|
||||
@@ -116,27 +120,6 @@ async function pollMatrixQaRoomObserver(
|
||||
}
|
||||
}
|
||||
|
||||
function findObservedEventMatch(params: {
|
||||
cursorIndex: number;
|
||||
events: MatrixQaObservedEvent[];
|
||||
predicate: (event: MatrixQaObservedEvent) => boolean;
|
||||
roomId: string;
|
||||
}) {
|
||||
for (let index = params.cursorIndex; index < params.events.length; index += 1) {
|
||||
const event = params.events[index];
|
||||
if (event?.roomId !== params.roomId) {
|
||||
continue;
|
||||
}
|
||||
if (params.predicate(event)) {
|
||||
return {
|
||||
event,
|
||||
nextCursorIndex: index + 1,
|
||||
};
|
||||
}
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function createMatrixQaRoomObserver(
|
||||
params: MatrixQaSyncParams & {
|
||||
observedEvents: MatrixQaObservedEvent[];
|
||||
@@ -162,7 +145,7 @@ export function createMatrixQaRoomObserver(
|
||||
const startedAt = Date.now();
|
||||
let cursorIndex = roomObserver.cursorIndex;
|
||||
while (Date.now() - startedAt < waitParams.timeoutMs) {
|
||||
const matched = findObservedEventMatch({
|
||||
const matched = findMatrixQaObservedEventMatch({
|
||||
cursorIndex,
|
||||
events: roomObserver.events,
|
||||
predicate: waitParams.predicate,
|
||||
|
||||
Reference in New Issue
Block a user