refactor: normalize channel capabilities typing

This commit is contained in:
Peter Steinberger
2026-01-17 07:58:54 +00:00
parent 1a4fc8dea6
commit e6477363e9
3 changed files with 22 additions and 17 deletions

View File

@@ -1,5 +1,5 @@
// eslint-disable-next-line no-control-regex
const DSR_PATTERN = /\x1b\[\??6n/g;
const ESC = String.fromCharCode(0x1b);
const DSR_PATTERN = new RegExp(`${ESC}\\[\\??6n`, "g");
export function stripDsrRequests(input: string): { cleaned: string; requests: number } {
let requests = 0;

View File

@@ -16,8 +16,8 @@ describe("resolveChannelCapabilities", () => {
it("returns undefined for missing inputs", () => {
expect(resolveChannelCapabilities({})).toBeUndefined();
expect(resolveChannelCapabilities({ cfg: {} as ClawdbotConfig })).toBeUndefined();
expect(resolveChannelCapabilities({ cfg: {} as ClawdbotConfig, channel: "" })).toBeUndefined();
expect(resolveChannelCapabilities({ cfg: {} })).toBeUndefined();
expect(resolveChannelCapabilities({ cfg: {}, channel: "" })).toBeUndefined();
});
it("normalizes and prefers per-account capabilities", () => {
@@ -36,7 +36,7 @@ describe("resolveChannelCapabilities", () => {
expect(
resolveChannelCapabilities({
cfg: cfg as ClawdbotConfig,
cfg,
channel: "telegram",
accountId: "default",
}),
@@ -57,7 +57,7 @@ describe("resolveChannelCapabilities", () => {
expect(
resolveChannelCapabilities({
cfg: cfg as ClawdbotConfig,
cfg,
channel: "telegram",
accountId: "default",
}),
@@ -77,7 +77,7 @@ describe("resolveChannelCapabilities", () => {
expect(
resolveChannelCapabilities({
cfg: cfg as ClawdbotConfig,
cfg,
channel: "slack",
accountId: "family",
}),
@@ -100,7 +100,7 @@ describe("resolveChannelCapabilities", () => {
expect(
resolveChannelCapabilities({
cfg: cfg as ClawdbotConfig,
cfg,
channel: "msteams",
}),
).toEqual(["polls"]);
@@ -120,7 +120,7 @@ describe("resolveChannelCapabilities", () => {
// Should return undefined (not crash), allowing channel-specific handlers to process it.
expect(
resolveChannelCapabilities({
cfg: cfg as ClawdbotConfig,
cfg,
channel: "telegram",
}),
).toBeUndefined();

View File

@@ -1,19 +1,24 @@
import { normalizeChannelId } from "../channels/plugins/index.js";
import { normalizeAccountId } from "../routing/session-key.js";
import type { ClawdbotConfig } from "./config.js";
import type { TelegramCapabilitiesConfig } from "./types.telegram.js";
function normalizeCapabilities(capabilities: string[] | undefined): string[] | undefined {
if (!capabilities) return undefined;
type CapabilitiesConfig = TelegramCapabilitiesConfig;
const isStringArray = (value: unknown): value is string[] =>
Array.isArray(value) && value.every((entry) => typeof entry === "string");
function normalizeCapabilities(capabilities: CapabilitiesConfig | undefined): string[] | undefined {
// Handle object-format capabilities (e.g., { inlineButtons: "dm" }) gracefully.
// Channel-specific handlers (like resolveTelegramInlineButtonsScope) process these separately.
if (!Array.isArray(capabilities)) return undefined;
if (!isStringArray(capabilities)) return undefined;
const normalized = capabilities.map((entry) => entry.trim()).filter(Boolean);
return normalized.length > 0 ? normalized : undefined;
}
function resolveAccountCapabilities(params: {
cfg?: { accounts?: Record<string, { capabilities?: string[] }> } & {
capabilities?: string[];
cfg?: { accounts?: Record<string, { capabilities?: CapabilitiesConfig }> } & {
capabilities?: CapabilitiesConfig;
};
accountId?: string | null;
}): string[] | undefined {
@@ -40,7 +45,7 @@ function resolveAccountCapabilities(params: {
}
export function resolveChannelCapabilities(params: {
cfg?: ClawdbotConfig;
cfg?: Partial<ClawdbotConfig>;
channel?: string | null;
accountId?: string | null;
}): string[] | undefined {
@@ -51,8 +56,8 @@ export function resolveChannelCapabilities(params: {
const channelsConfig = cfg.channels as Record<string, unknown> | undefined;
const channelConfig = (channelsConfig?.[channel] ?? (cfg as Record<string, unknown>)[channel]) as
| {
accounts?: Record<string, { capabilities?: string[] }>;
capabilities?: string[];
accounts?: Record<string, { capabilities?: CapabilitiesConfig }>;
capabilities?: CapabilitiesConfig;
}
| undefined;
return resolveAccountCapabilities({