* feat: add chunking mode for outbound messages - Introduced `chunkMode` option in various account configurations to allow splitting messages by "length" or "newline". - Updated message processing to handle chunking based on the selected mode. - Added tests for new chunking functionality, ensuring correct behavior for both modes. * feat: enhance chunking mode documentation and configuration - Added `chunkMode` option to the BlueBubbles account configuration, allowing users to choose between "length" and "newline" for message chunking. - Updated documentation to clarify the behavior of the `chunkMode` setting. - Adjusted account merging logic to incorporate the new `chunkMode` configuration. * refactor: simplify chunk mode handling for BlueBubbles - Removed `chunkMode` configuration from various account schemas and types, centralizing chunk mode logic to BlueBubbles only. - Updated `processMessage` to default to "newline" for BlueBubbles chunking. - Adjusted tests to reflect changes in chunk mode handling for BlueBubbles, ensuring proper functionality. * fix: update default chunk mode to 'length' for BlueBubbles - Changed the default value of `chunkMode` from 'newline' to 'length' in the BlueBubbles configuration and related processing functions. - Updated documentation to reflect the new default behavior for chunking messages. - Adjusted tests to ensure the correct default value is returned for BlueBubbles chunk mode.
81 lines
2.9 KiB
TypeScript
81 lines
2.9 KiB
TypeScript
import type { ClawdbotConfig } from "clawdbot/plugin-sdk";
|
|
import { DEFAULT_ACCOUNT_ID, normalizeAccountId } from "clawdbot/plugin-sdk";
|
|
import { normalizeBlueBubblesServerUrl, type BlueBubblesAccountConfig } from "./types.js";
|
|
|
|
export type ResolvedBlueBubblesAccount = {
|
|
accountId: string;
|
|
enabled: boolean;
|
|
name?: string;
|
|
config: BlueBubblesAccountConfig;
|
|
configured: boolean;
|
|
baseUrl?: string;
|
|
};
|
|
|
|
function listConfiguredAccountIds(cfg: ClawdbotConfig): string[] {
|
|
const accounts = cfg.channels?.bluebubbles?.accounts;
|
|
if (!accounts || typeof accounts !== "object") return [];
|
|
return Object.keys(accounts).filter(Boolean);
|
|
}
|
|
|
|
export function listBlueBubblesAccountIds(cfg: ClawdbotConfig): string[] {
|
|
const ids = listConfiguredAccountIds(cfg);
|
|
if (ids.length === 0) return [DEFAULT_ACCOUNT_ID];
|
|
return ids.sort((a, b) => a.localeCompare(b));
|
|
}
|
|
|
|
export function resolveDefaultBlueBubblesAccountId(cfg: ClawdbotConfig): string {
|
|
const ids = listBlueBubblesAccountIds(cfg);
|
|
if (ids.includes(DEFAULT_ACCOUNT_ID)) return DEFAULT_ACCOUNT_ID;
|
|
return ids[0] ?? DEFAULT_ACCOUNT_ID;
|
|
}
|
|
|
|
function resolveAccountConfig(
|
|
cfg: ClawdbotConfig,
|
|
accountId: string,
|
|
): BlueBubblesAccountConfig | undefined {
|
|
const accounts = cfg.channels?.bluebubbles?.accounts;
|
|
if (!accounts || typeof accounts !== "object") return undefined;
|
|
return accounts[accountId] as BlueBubblesAccountConfig | undefined;
|
|
}
|
|
|
|
function mergeBlueBubblesAccountConfig(
|
|
cfg: ClawdbotConfig,
|
|
accountId: string,
|
|
): BlueBubblesAccountConfig {
|
|
const base = (cfg.channels?.bluebubbles ?? {}) as BlueBubblesAccountConfig & {
|
|
accounts?: unknown;
|
|
};
|
|
const { accounts: _ignored, ...rest } = base;
|
|
const account = resolveAccountConfig(cfg, accountId) ?? {};
|
|
const chunkMode = account.chunkMode ?? rest.chunkMode ?? "length";
|
|
return { ...rest, ...account, chunkMode };
|
|
}
|
|
|
|
export function resolveBlueBubblesAccount(params: {
|
|
cfg: ClawdbotConfig;
|
|
accountId?: string | null;
|
|
}): ResolvedBlueBubblesAccount {
|
|
const accountId = normalizeAccountId(params.accountId);
|
|
const baseEnabled = params.cfg.channels?.bluebubbles?.enabled;
|
|
const merged = mergeBlueBubblesAccountConfig(params.cfg, accountId);
|
|
const accountEnabled = merged.enabled !== false;
|
|
const serverUrl = merged.serverUrl?.trim();
|
|
const password = merged.password?.trim();
|
|
const configured = Boolean(serverUrl && password);
|
|
const baseUrl = serverUrl ? normalizeBlueBubblesServerUrl(serverUrl) : undefined;
|
|
return {
|
|
accountId,
|
|
enabled: baseEnabled !== false && accountEnabled,
|
|
name: merged.name?.trim() || undefined,
|
|
config: merged,
|
|
configured,
|
|
baseUrl,
|
|
};
|
|
}
|
|
|
|
export function listEnabledBlueBubblesAccounts(cfg: ClawdbotConfig): ResolvedBlueBubblesAccount[] {
|
|
return listBlueBubblesAccountIds(cfg)
|
|
.map((accountId) => resolveBlueBubblesAccount({ cfg, accountId }))
|
|
.filter((account) => account.enabled);
|
|
}
|