refactor: share extension deferred and runtime helpers

This commit is contained in:
Peter Steinberger 2026-03-14 02:16:19 +00:00
parent 1ac4bac8b1
commit 6a61d5504c
8 changed files with 38 additions and 41 deletions

View File

@ -1,5 +1,6 @@
import type { RuntimeEnv, WizardPrompter } from "openclaw/plugin-sdk/irc";
import { describe, expect, it, vi } from "vitest";
import { createRuntimeEnv } from "../../test-utils/runtime-env.js";
import { ircOnboardingAdapter } from "./onboarding.js";
import type { CoreConfig } from "./types.js";
@ -63,13 +64,7 @@ describe("irc onboarding", () => {
}),
});
const runtime: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn((code: number): never => {
throw new Error(`exit ${code}`);
}),
};
const runtime: RuntimeEnv = createRuntimeEnv();
const result = await ircOnboardingAdapter.configure({
cfg: {} as CoreConfig,

View File

@ -1,5 +1,6 @@
import type { PluginRuntime, RuntimeEnv } from "openclaw/plugin-sdk/matrix";
import { beforeEach, describe, expect, it, vi } from "vitest";
import { createRuntimeEnv } from "../../test-utils/runtime-env.js";
import { matrixPlugin } from "./channel.js";
import { setMatrixRuntime } from "./runtime.js";
import { createMatrixBotSdkMock } from "./test-mocks.js";
@ -10,13 +11,7 @@ vi.mock("@vector-im/matrix-bot-sdk", () =>
);
describe("matrix directory", () => {
const runtimeEnv: RuntimeEnv = {
log: vi.fn(),
error: vi.fn(),
exit: vi.fn((code: number): never => {
throw new Error(`exit ${code}`);
}),
};
const runtimeEnv: RuntimeEnv = createRuntimeEnv();
beforeEach(() => {
setMatrixRuntime({

View File

@ -15,6 +15,7 @@ import {
PAIRING_APPROVED_MESSAGE,
type ChannelPlugin,
} from "openclaw/plugin-sdk/matrix";
import { buildTrafficStatusSummary } from "../../shared/channel-status-summary.js";
import { matrixMessageActions } from "./actions.js";
import { MatrixConfigSchema } from "./config-schema.js";
import { listMatrixDirectoryGroupsLive, listMatrixDirectoryPeersLive } from "./directory-live.js";
@ -410,8 +411,7 @@ export const matrixPlugin: ChannelPlugin<ResolvedMatrixAccount> = {
lastError: runtime?.lastError ?? null,
probe,
lastProbeAt: runtime?.lastProbeAt ?? null,
lastInboundAt: runtime?.lastInboundAt ?? null,
lastOutboundAt: runtime?.lastOutboundAt ?? null,
...buildTrafficStatusSummary(runtime),
}),
},
gateway: {

View File

@ -1,16 +1,7 @@
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
import { createDeferred } from "../../../shared/deferred.js";
import { DEFAULT_SEND_GAP_MS, enqueueSend } from "./send-queue.js";
function deferred<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
describe("enqueueSend", () => {
beforeEach(() => {
vi.useFakeTimers();
@ -21,7 +12,7 @@ describe("enqueueSend", () => {
});
it("serializes sends per room", async () => {
const gate = deferred<void>();
const gate = createDeferred<void>();
const events: string[] = [];
const first = enqueueSend("!room:example.org", async () => {
@ -91,7 +82,7 @@ describe("enqueueSend", () => {
});
it("continues queued work when the head task fails", async () => {
const gate = deferred<void>();
const gate = createDeferred<void>();
const events: string[] = [];
const first = enqueueSend("!room:example.org", async () => {

View File

@ -7,7 +7,10 @@ import {
mapAllowFromEntries,
type ChannelPlugin,
} from "openclaw/plugin-sdk/nostr";
import { buildPassiveChannelStatusSummary } from "../../shared/channel-status-summary.js";
import {
buildPassiveChannelStatusSummary,
buildTrafficStatusSummary,
} from "../../shared/channel-status-summary.js";
import type { NostrProfile } from "./config-schema.js";
import { NostrConfigSchema } from "./config-schema.js";
import type { MetricEvent, MetricsSnapshot } from "./metrics.js";
@ -176,8 +179,7 @@ export const nostrPlugin: ChannelPlugin<ResolvedNostrAccount> = {
lastStartAt: runtime?.lastStartAt ?? null,
lastStopAt: runtime?.lastStopAt ?? null,
lastError: runtime?.lastError ?? null,
lastInboundAt: runtime?.lastInboundAt ?? null,
lastOutboundAt: runtime?.lastOutboundAt ?? null,
...buildTrafficStatusSummary(runtime),
}),
},

View File

@ -8,6 +8,11 @@ type PassiveChannelStatusSnapshot = {
lastProbeAt?: number | null;
};
type TrafficStatusSnapshot = {
lastInboundAt?: number | null;
lastOutboundAt?: number | null;
};
export function buildPassiveChannelStatusSummary<TExtra extends object>(
snapshot: PassiveChannelStatusSnapshot,
extra?: TExtra,
@ -32,3 +37,12 @@ export function buildPassiveProbedChannelStatusSummary<TExtra extends object>(
lastProbeAt: snapshot.lastProbeAt ?? null,
};
}
export function buildTrafficStatusSummary<TSnapshot extends TrafficStatusSnapshot>(
snapshot?: TSnapshot | null,
) {
return {
lastInboundAt: snapshot?.lastInboundAt ?? null,
lastOutboundAt: snapshot?.lastOutboundAt ?? null,
};
}

View File

@ -0,0 +1,9 @@
export function createDeferred<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}

View File

@ -31,6 +31,7 @@ import {
summarizeMapping,
warnMissingProviderGroupPolicyFallbackOnce,
} from "openclaw/plugin-sdk/zalouser";
import { createDeferred } from "../../shared/deferred.js";
import {
buildZalouserGroupCandidates,
findZalouserGroupEntry,
@ -129,16 +130,6 @@ function resolveInboundQueueKey(message: ZaloInboundMessage): string {
return `direct:${senderId || threadId}`;
}
function createDeferred<T>() {
let resolve!: (value: T | PromiseLike<T>) => void;
let reject!: (reason?: unknown) => void;
const promise = new Promise<T>((res, rej) => {
resolve = res;
reject = rej;
});
return { promise, resolve, reject };
}
function resolveZalouserDmSessionScope(config: OpenClawConfig) {
const configured = config.session?.dmScope;
return configured === "main" || !configured ? "per-channel-peer" : configured;