refactor: trim channel contract registry helpers

This commit is contained in:
Peter Steinberger
2026-05-02 04:52:29 +01:00
parent e65b490f11
commit e73c6ff609
3 changed files with 3 additions and 203 deletions

View File

@@ -1,57 +1,15 @@
import type { OpenClawConfig } from "../../../../config/config.js";
import type { ChannelId } from "../../channel-id.types.js";
import type { ChannelPlugin } from "../../types.js";
import {
getBundledChannelPlugin,
listBundledChannelPluginIds,
listBundledChannelPlugins,
} from "./bundled-channel-plugin-loader.js";
import { channelPluginSurfaceKeys, type ChannelPluginSurface } from "./manifest.js";
type SurfaceContractEntry = {
id: string;
plugin: Pick<
ChannelPlugin,
| "id"
| "actions"
| "setup"
| "status"
| "outbound"
| "messaging"
| "threading"
| "directory"
| "gateway"
>;
surfaces: readonly ChannelPluginSurface[];
};
type ThreadingContractEntry = {
id: string;
plugin: Pick<ChannelPlugin, "id" | "threading">;
};
import { listBundledChannelPluginIds } from "./bundled-channel-plugin-loader.js";
type ThreadingContractRef = {
id: ChannelId;
};
type DirectoryContractEntry = {
id: string;
plugin: Pick<ChannelPlugin, "id" | "directory">;
coverage: "lookups" | "presence";
cfg?: OpenClawConfig;
accountId?: string;
};
type DirectoryContractRef = {
id: ChannelId;
coverage: "lookups" | "presence";
};
let surfaceContractRegistryCache: SurfaceContractEntry[] | undefined;
const surfaceContractEntryCache = new Map<ChannelId, SurfaceContractEntry | null>();
let threadingContractRegistryCache: ThreadingContractEntry[] | undefined;
let directoryContractRegistryCache: DirectoryContractEntry[] | undefined;
const threadingContractPluginIds = new Set<ChannelId>([
"bluebubbles",
"discord",
@@ -82,14 +40,6 @@ const directoryContractPluginIds = new Set<ChannelId>([
"zalouser",
]);
function toSurfaceContractEntry(plugin: ChannelPlugin): SurfaceContractEntry {
return {
id: plugin.id,
plugin,
surfaces: channelPluginSurfaceKeys.filter((surface) => Boolean(plugin[surface])),
};
}
function getBundledChannelPluginIdsForShard(params: {
shardIndex: number;
shardCount: number;
@@ -99,31 +49,6 @@ function getBundledChannelPluginIdsForShard(params: {
);
}
function getSurfaceContractEntry(id: ChannelId): SurfaceContractEntry | undefined {
if (surfaceContractEntryCache.has(id)) {
return surfaceContractEntryCache.get(id) ?? undefined;
}
const plugin = getBundledChannelPlugin(id);
const entry = plugin ? toSurfaceContractEntry(plugin) : null;
surfaceContractEntryCache.set(id, entry);
return entry ?? undefined;
}
export function getSurfaceContractRegistry(): SurfaceContractEntry[] {
surfaceContractRegistryCache ??= listBundledChannelPlugins().map(toSurfaceContractEntry);
return surfaceContractRegistryCache;
}
export function getSurfaceContractRegistryShard(params: {
shardIndex: number;
shardCount: number;
}): SurfaceContractEntry[] {
return getBundledChannelPluginIdsForShard(params).flatMap((id) => {
const entry = getSurfaceContractEntry(id);
return entry ? [entry] : [];
});
}
export function getSurfaceContractRegistryShardIds(params: {
shardIndex: number;
shardCount: number;
@@ -131,42 +56,6 @@ export function getSurfaceContractRegistryShardIds(params: {
return getBundledChannelPluginIdsForShard(params);
}
export function getThreadingContractRegistry(): ThreadingContractEntry[] {
threadingContractRegistryCache ??= listBundledChannelPluginIds()
.filter((id) => threadingContractPluginIds.has(id))
.flatMap((id) => {
const entry = getSurfaceContractEntry(id);
return entry && entry.surfaces.includes("threading")
? [
{
id: entry.id,
plugin: entry.plugin,
},
]
: [];
});
return threadingContractRegistryCache;
}
export function getThreadingContractRegistryShard(params: {
shardIndex: number;
shardCount: number;
}): ThreadingContractEntry[] {
return getBundledChannelPluginIdsForShard(params)
.filter((id) => threadingContractPluginIds.has(id))
.flatMap((id) => {
const entry = getSurfaceContractEntry(id);
return entry && entry.surfaces.includes("threading")
? [
{
id: entry.id,
plugin: entry.plugin,
},
]
: [];
});
}
export function getThreadingContractRegistryShardRefs(params: {
shardIndex: number;
shardCount: number;
@@ -178,44 +67,6 @@ export function getThreadingContractRegistryShardRefs(params: {
const directoryPresenceOnlyIds = new Set(["whatsapp", "zalouser"]);
export function getDirectoryContractRegistry(): DirectoryContractEntry[] {
directoryContractRegistryCache ??= listBundledChannelPluginIds()
.filter((id) => directoryContractPluginIds.has(id))
.flatMap((id) => {
const entry = getSurfaceContractEntry(id);
return entry && entry.surfaces.includes("directory")
? [
{
id: entry.id,
plugin: entry.plugin,
coverage: directoryPresenceOnlyIds.has(entry.id) ? "presence" : "lookups",
},
]
: [];
});
return directoryContractRegistryCache;
}
export function getDirectoryContractRegistryShard(params: {
shardIndex: number;
shardCount: number;
}): DirectoryContractEntry[] {
return getBundledChannelPluginIdsForShard(params)
.filter((id) => directoryContractPluginIds.has(id))
.flatMap((id) => {
const entry = getSurfaceContractEntry(id);
return entry && entry.surfaces.includes("directory")
? [
{
id: entry.id,
plugin: entry.plugin,
coverage: directoryPresenceOnlyIds.has(entry.id) ? "presence" : "lookups",
},
]
: [];
});
}
export function getDirectoryContractRegistryShardRefs(params: {
shardIndex: number;
shardCount: number;

View File

@@ -1,34 +1,6 @@
import { expect, it } from "vitest";
import { expect } from "vitest";
import type { ChannelPlugin } from "../../types.js";
export function installChannelSurfaceContractSuite(params: {
plugin: Pick<
ChannelPlugin,
| "id"
| "actions"
| "setup"
| "status"
| "outbound"
| "messaging"
| "threading"
| "directory"
| "gateway"
>;
surface:
| "actions"
| "setup"
| "status"
| "outbound"
| "messaging"
| "threading"
| "directory"
| "gateway";
}) {
it(`exposes the ${params.surface} surface contract`, () => {
expectChannelSurfaceContract(params);
});
}
export function expectChannelSurfaceContract(params: {
plugin: Pick<
ChannelPlugin,

View File

@@ -1,4 +1,4 @@
import { expect, it } from "vitest";
import { expect } from "vitest";
import type { OpenClawConfig } from "../../../../config/config.js";
import type { RuntimeEnv } from "../../../../runtime.js";
import type {
@@ -82,18 +82,6 @@ function expectFocusedBindingShape(binding: ChannelFocusedBindingContext) {
expect(binding.labelNoun.trim()).not.toBe("");
}
export function installChannelThreadingContractSuite(params: {
plugin: Pick<ChannelPlugin, "id" | "threading">;
}) {
it("exposes the base threading contract", () => {
expectChannelThreadingBaseContract(params.plugin);
});
it("keeps threading return values normalized", () => {
expectChannelThreadingReturnValuesNormalized(params.plugin);
});
}
export function expectChannelThreadingBaseContract(
plugin: Pick<ChannelPlugin, "id" | "threading">,
) {
@@ -185,17 +173,6 @@ export function expectChannelThreadingReturnValuesNormalized(
}
}
export function installChannelDirectoryContractSuite(params: {
plugin: Pick<ChannelPlugin, "id" | "directory">;
coverage?: "lookups" | "presence";
cfg?: OpenClawConfig;
accountId?: string;
}) {
it("exposes the base directory contract", async () => {
await expectChannelDirectoryBaseContract(params);
});
}
export async function expectChannelDirectoryBaseContract(params: {
plugin: Pick<ChannelPlugin, "id" | "directory">;
coverage?: "lookups" | "presence";