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 ESC = String.fromCharCode(0x1b);
const DSR_PATTERN = /\x1b\[\??6n/g; const DSR_PATTERN = new RegExp(`${ESC}\\[\\??6n`, "g");
export function stripDsrRequests(input: string): { cleaned: string; requests: number } { export function stripDsrRequests(input: string): { cleaned: string; requests: number } {
let requests = 0; let requests = 0;

View File

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

View File

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