chore: Lint extensions folder.

This commit is contained in:
cpojer
2026-01-31 22:13:48 +09:00
parent 4f2166c503
commit 230ca789e2
221 changed files with 4006 additions and 1583 deletions
+16 -6
View File
@@ -6,21 +6,29 @@ import { resolveZaloToken } from "./token.js";
function listConfiguredAccountIds(cfg: OpenClawConfig): string[] {
const accounts = (cfg.channels?.zalo as ZaloConfig | undefined)?.accounts;
if (!accounts || typeof accounts !== "object") return [];
if (!accounts || typeof accounts !== "object") {
return [];
}
return Object.keys(accounts).filter(Boolean);
}
export function listZaloAccountIds(cfg: OpenClawConfig): string[] {
const ids = listConfiguredAccountIds(cfg);
if (ids.length === 0) return [DEFAULT_ACCOUNT_ID];
return ids.sort((a, b) => a.localeCompare(b));
if (ids.length === 0) {
return [DEFAULT_ACCOUNT_ID];
}
return ids.toSorted((a, b) => a.localeCompare(b));
}
export function resolveDefaultZaloAccountId(cfg: OpenClawConfig): string {
const zaloConfig = cfg.channels?.zalo as ZaloConfig | undefined;
if (zaloConfig?.defaultAccount?.trim()) return zaloConfig.defaultAccount.trim();
if (zaloConfig?.defaultAccount?.trim()) {
return zaloConfig.defaultAccount.trim();
}
const ids = listZaloAccountIds(cfg);
if (ids.includes(DEFAULT_ACCOUNT_ID)) return DEFAULT_ACCOUNT_ID;
if (ids.includes(DEFAULT_ACCOUNT_ID)) {
return DEFAULT_ACCOUNT_ID;
}
return ids[0] ?? DEFAULT_ACCOUNT_ID;
}
@@ -29,7 +37,9 @@ function resolveAccountConfig(
accountId: string,
): ZaloAccountConfig | undefined {
const accounts = (cfg.channels?.zalo as ZaloConfig | undefined)?.accounts;
if (!accounts || typeof accounts !== "object") return undefined;
if (!accounts || typeof accounts !== "object") {
return undefined;
}
return accounts[accountId] as ZaloAccountConfig | undefined;
}
+11 -5
View File
@@ -18,17 +18,23 @@ function listEnabledAccounts(cfg: OpenClawConfig) {
export const zaloMessageActions: ChannelMessageActionAdapter = {
listActions: ({ cfg }) => {
const accounts = listEnabledAccounts(cfg as OpenClawConfig);
if (accounts.length === 0) return [];
const accounts = listEnabledAccounts(cfg);
if (accounts.length === 0) {
return [];
}
const actions = new Set<ChannelMessageActionName>(["send"]);
return Array.from(actions);
},
supportsButtons: () => false,
extractToolSend: ({ args }) => {
const action = typeof args.action === "string" ? args.action.trim() : "";
if (action !== "sendMessage") return null;
if (action !== "sendMessage") {
return null;
}
const to = typeof args.to === "string" ? args.to : undefined;
if (!to) return null;
if (!to) {
return null;
}
const accountId = typeof args.accountId === "string" ? args.accountId.trim() : undefined;
return { to, accountId };
},
@@ -44,7 +50,7 @@ export const zaloMessageActions: ChannelMessageActionAdapter = {
const result = await sendMessageZalo(to ?? "", content ?? "", {
accountId: accountId ?? undefined,
mediaUrl: mediaUrl ?? undefined,
cfg: cfg as OpenClawConfig,
cfg: cfg,
});
if (!result.ok) {
+3 -1
View File
@@ -122,7 +122,9 @@ export async function callZaloApi<T = unknown>(
return data;
} finally {
if (timeoutId) clearTimeout(timeoutId);
if (timeoutId) {
clearTimeout(timeoutId);
}
}
}
+46 -31
View File
@@ -44,7 +44,9 @@ const meta = {
function normalizeZaloMessagingTarget(raw: string): string | undefined {
const trimmed = raw?.trim();
if (!trimmed) return undefined;
if (!trimmed) {
return undefined;
}
return trimmed.replace(/^(zalo|zl):/i, "");
}
@@ -58,8 +60,8 @@ export const zaloDock: ChannelDock = {
outbound: { textChunkLimit: 2000 },
config: {
resolveAllowFrom: ({ cfg, accountId }) =>
(resolveZaloAccount({ cfg: cfg as OpenClawConfig, accountId }).config.allowFrom ?? []).map(
(entry) => String(entry),
(resolveZaloAccount({ cfg: cfg, accountId }).config.allowFrom ?? []).map((entry) =>
String(entry),
),
formatAllowFrom: ({ allowFrom }) =>
allowFrom
@@ -92,13 +94,12 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
reload: { configPrefixes: ["channels.zalo"] },
configSchema: buildChannelConfigSchema(ZaloConfigSchema),
config: {
listAccountIds: (cfg) => listZaloAccountIds(cfg as OpenClawConfig),
resolveAccount: (cfg, accountId) =>
resolveZaloAccount({ cfg: cfg as OpenClawConfig, accountId }),
defaultAccountId: (cfg) => resolveDefaultZaloAccountId(cfg as OpenClawConfig),
listAccountIds: (cfg) => listZaloAccountIds(cfg),
resolveAccount: (cfg, accountId) => resolveZaloAccount({ cfg: cfg, accountId }),
defaultAccountId: (cfg) => resolveDefaultZaloAccountId(cfg),
setAccountEnabled: ({ cfg, accountId, enabled }) =>
setAccountEnabledInConfigSection({
cfg: cfg as OpenClawConfig,
cfg: cfg,
sectionKey: "zalo",
accountId,
enabled,
@@ -106,7 +107,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
}),
deleteAccount: ({ cfg, accountId }) =>
deleteAccountFromConfigSection({
cfg: cfg as OpenClawConfig,
cfg: cfg,
sectionKey: "zalo",
accountId,
clearBaseFields: ["botToken", "tokenFile", "name"],
@@ -120,8 +121,8 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
tokenSource: account.tokenSource,
}),
resolveAllowFrom: ({ cfg, accountId }) =>
(resolveZaloAccount({ cfg: cfg as OpenClawConfig, accountId }).config.allowFrom ?? []).map(
(entry) => String(entry),
(resolveZaloAccount({ cfg: cfg, accountId }).config.allowFrom ?? []).map((entry) =>
String(entry),
),
formatAllowFrom: ({ allowFrom }) =>
allowFrom
@@ -133,9 +134,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
security: {
resolveDmPolicy: ({ cfg, accountId, account }) => {
const resolvedAccountId = accountId ?? account.accountId ?? DEFAULT_ACCOUNT_ID;
const useAccountPath = Boolean(
(cfg as OpenClawConfig).channels?.zalo?.accounts?.[resolvedAccountId],
);
const useAccountPath = Boolean(cfg.channels?.zalo?.accounts?.[resolvedAccountId]);
const basePath = useAccountPath
? `channels.zalo.accounts.${resolvedAccountId}.`
: "channels.zalo.";
@@ -161,7 +160,9 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
targetResolver: {
looksLikeId: (raw) => {
const trimmed = raw.trim();
if (!trimmed) return false;
if (!trimmed) {
return false;
}
return /^\d{3,}$/.test(trimmed);
},
hint: "<chatId>",
@@ -170,7 +171,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
directory: {
self: async () => null,
listPeers: async ({ cfg, accountId, query, limit }) => {
const account = resolveZaloAccount({ cfg: cfg as OpenClawConfig, accountId });
const account = resolveZaloAccount({ cfg: cfg, accountId });
const q = query?.trim().toLowerCase() || "";
const peers = Array.from(
new Set(
@@ -191,7 +192,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
resolveAccountId: ({ accountId }) => normalizeAccountId(accountId),
applyAccountName: ({ cfg, accountId, name }) =>
applyAccountNameToChannelSection({
cfg: cfg as OpenClawConfig,
cfg: cfg,
channelKey: "zalo",
accountId,
name,
@@ -207,7 +208,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
},
applyAccountConfig: ({ cfg, accountId, input }) => {
const namedConfig = applyAccountNameToChannelSection({
cfg: cfg as OpenClawConfig,
cfg: cfg,
channelKey: "zalo",
accountId,
name: input.name,
@@ -246,9 +247,9 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
...next.channels?.zalo,
enabled: true,
accounts: {
...(next.channels?.zalo?.accounts ?? {}),
...next.channels?.zalo?.accounts,
[accountId]: {
...(next.channels?.zalo?.accounts?.[accountId] ?? {}),
...next.channels?.zalo?.accounts?.[accountId],
enabled: true,
...(input.tokenFile
? { tokenFile: input.tokenFile }
@@ -266,16 +267,22 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
idLabel: "zaloUserId",
normalizeAllowEntry: (entry) => entry.replace(/^(zalo|zl):/i, ""),
notifyApproval: async ({ cfg, id }) => {
const account = resolveZaloAccount({ cfg: cfg as OpenClawConfig });
if (!account.token) throw new Error("Zalo token not configured");
const account = resolveZaloAccount({ cfg: cfg });
if (!account.token) {
throw new Error("Zalo token not configured");
}
await sendMessageZalo(id, PAIRING_APPROVED_MESSAGE, { token: account.token });
},
},
outbound: {
deliveryMode: "direct",
chunker: (text, limit) => {
if (!text) return [];
if (limit <= 0 || text.length <= limit) return [text];
if (!text) {
return [];
}
if (limit <= 0 || text.length <= limit) {
return [text];
}
const chunks: string[] = [];
let remaining = text;
while (remaining.length > limit) {
@@ -283,15 +290,21 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
const lastNewline = window.lastIndexOf("\n");
const lastSpace = window.lastIndexOf(" ");
let breakIdx = lastNewline > 0 ? lastNewline : lastSpace;
if (breakIdx <= 0) breakIdx = limit;
if (breakIdx <= 0) {
breakIdx = limit;
}
const rawChunk = remaining.slice(0, breakIdx);
const chunk = rawChunk.trimEnd();
if (chunk.length > 0) chunks.push(chunk);
if (chunk.length > 0) {
chunks.push(chunk);
}
const brokeOnSeparator = breakIdx < remaining.length && /\s/.test(remaining[breakIdx]);
const nextStart = Math.min(remaining.length, breakIdx + (brokeOnSeparator ? 1 : 0));
remaining = remaining.slice(nextStart).trimStart();
}
if (remaining.length) chunks.push(remaining);
if (remaining.length) {
chunks.push(remaining);
}
return chunks;
},
chunkerMode: "text",
@@ -299,7 +312,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
sendText: async ({ to, text, accountId, cfg }) => {
const result = await sendMessageZalo(to, text, {
accountId: accountId ?? undefined,
cfg: cfg as OpenClawConfig,
cfg: cfg,
});
return {
channel: "zalo",
@@ -312,7 +325,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
const result = await sendMessageZalo(to, text, {
accountId: accountId ?? undefined,
mediaUrl,
cfg: cfg as OpenClawConfig,
cfg: cfg,
});
return {
channel: "zalo",
@@ -372,7 +385,9 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
try {
const probe = await probeZalo(token, 2500, fetcher);
const name = probe.ok ? probe.bot?.name?.trim() : null;
if (name) zaloBotLabel = ` (${name})`;
if (name) {
zaloBotLabel = ` (${name})`;
}
ctx.setStatus({
accountId: account.accountId,
bot: probe.bot,
@@ -385,7 +400,7 @@ export const zaloPlugin: ChannelPlugin<ResolvedZaloAccount> = {
return monitorZaloProvider({
token,
account,
config: ctx.cfg as OpenClawConfig,
config: ctx.cfg,
runtime: ctx.runtime,
abortSignal: ctx.abortSignal,
useWebhook: Boolean(account.config.webhookUrl),
+21 -7
View File
@@ -52,7 +52,9 @@ function logVerbose(core: ZaloCoreRuntime, runtime: ZaloRuntimeEnv, message: str
}
function isSenderAllowed(senderId: string, allowFrom: string[]): boolean {
if (allowFrom.includes("*")) return true;
if (allowFrom.includes("*")) {
return true;
}
const normalizedSenderId = senderId.toLowerCase();
return allowFrom.some((entry) => {
const normalized = entry.toLowerCase().replace(/^(zalo|zl):/i, "");
@@ -108,7 +110,9 @@ const webhookTargets = new Map<string, WebhookTarget[]>();
function normalizeWebhookPath(raw: string): string {
const trimmed = raw.trim();
if (!trimmed) return "/";
if (!trimmed) {
return "/";
}
const withSlash = trimmed.startsWith("/") ? trimmed : `/${trimmed}`;
if (withSlash.length > 1 && withSlash.endsWith("/")) {
return withSlash.slice(0, -1);
@@ -118,7 +122,9 @@ function normalizeWebhookPath(raw: string): string {
function resolveWebhookPath(webhookPath?: string, webhookUrl?: string): string | null {
const trimmedPath = webhookPath?.trim();
if (trimmedPath) return normalizeWebhookPath(trimmedPath);
if (trimmedPath) {
return normalizeWebhookPath(trimmedPath);
}
if (webhookUrl?.trim()) {
try {
const parsed = new URL(webhookUrl);
@@ -153,7 +159,9 @@ export async function handleZaloWebhookRequest(
const url = new URL(req.url ?? "/", "http://localhost");
const path = normalizeWebhookPath(url.pathname);
const targets = webhookTargets.get(path);
if (!targets || targets.length === 0) return false;
if (!targets || targets.length === 0) {
return false;
}
if (req.method !== "POST") {
res.statusCode = 405;
@@ -238,7 +246,9 @@ function startPollingLoop(params: {
const pollTimeout = 30;
const poll = async () => {
if (isStopped() || abortSignal.aborted) return;
if (isStopped() || abortSignal.aborted) {
return;
}
try {
const response = await getUpdates(token, { timeout: pollTimeout }, fetcher);
@@ -285,7 +295,9 @@ async function processUpdate(
fetcher?: ZaloFetch,
): Promise<void> {
const { event_name, message } = update;
if (!message) return;
if (!message) {
return;
}
switch (event_name) {
case "message.text.received":
@@ -326,7 +338,9 @@ async function handleTextMessage(
fetcher?: ZaloFetch,
): Promise<void> {
const { text } = message;
if (!text?.trim()) return;
if (!text?.trim()) {
return;
}
await processMessageWithPipeline({
message,
+3 -1
View File
@@ -16,7 +16,9 @@ async function withServer(
server.listen(0, "127.0.0.1", () => resolve());
});
const address = server.address() as AddressInfo | null;
if (!address) throw new Error("missing server address");
if (!address) {
throw new Error("missing server address");
}
try {
await fn(`http://127.0.0.1:${address.port}`);
} finally {
+24 -24
View File
@@ -61,10 +61,7 @@ function setZaloUpdateMode(
},
} as OpenClawConfig;
}
const accounts = { ...(cfg.channels?.zalo?.accounts ?? {}) } as Record<
string,
Record<string, unknown>
>;
const accounts = { ...cfg.channels?.zalo?.accounts } as Record<string, Record<string, unknown>>;
const existing = accounts[accountId] ?? {};
const { webhookUrl: _url, webhookSecret: _secret, webhookPath: _path, ...rest } = existing;
accounts[accountId] = rest;
@@ -95,12 +92,9 @@ function setZaloUpdateMode(
} as OpenClawConfig;
}
const accounts = { ...(cfg.channels?.zalo?.accounts ?? {}) } as Record<
string,
Record<string, unknown>
>;
const accounts = { ...cfg.channels?.zalo?.accounts } as Record<string, Record<string, unknown>>;
accounts[accountId] = {
...(accounts[accountId] ?? {}),
...accounts[accountId],
webhookUrl,
webhookSecret,
webhookPath,
@@ -144,8 +138,12 @@ async function promptZaloAllowFrom(params: {
initialValue: existingAllowFrom[0] ? String(existingAllowFrom[0]) : undefined,
validate: (value) => {
const raw = String(value ?? "").trim();
if (!raw) return "Required";
if (!/^\d+$/.test(raw)) return "Use a numeric Zalo user id";
if (!raw) {
return "Required";
}
if (!/^\d+$/.test(raw)) {
return "Use a numeric Zalo user id";
}
return undefined;
},
});
@@ -179,9 +177,9 @@ async function promptZaloAllowFrom(params: {
...cfg.channels?.zalo,
enabled: true,
accounts: {
...(cfg.channels?.zalo?.accounts ?? {}),
...cfg.channels?.zalo?.accounts,
[accountId]: {
...(cfg.channels?.zalo?.accounts?.[accountId] ?? {}),
...cfg.channels?.zalo?.accounts?.[accountId],
enabled: cfg.channels?.zalo?.accounts?.[accountId]?.enabled ?? true,
dmPolicy: "allowlist",
allowFrom: unique,
@@ -198,14 +196,14 @@ const dmPolicy: ChannelOnboardingDmPolicy = {
policyKey: "channels.zalo.dmPolicy",
allowFromKey: "channels.zalo.allowFrom",
getCurrent: (cfg) => (cfg.channels?.zalo?.dmPolicy ?? "pairing") as "pairing",
setPolicy: (cfg, policy) => setZaloDmPolicy(cfg as OpenClawConfig, policy),
setPolicy: (cfg, policy) => setZaloDmPolicy(cfg, policy),
promptAllowFrom: async ({ cfg, prompter, accountId }) => {
const id =
accountId && normalizeAccountId(accountId)
? (normalizeAccountId(accountId) ?? DEFAULT_ACCOUNT_ID)
: resolveDefaultZaloAccountId(cfg as OpenClawConfig);
: resolveDefaultZaloAccountId(cfg);
return promptZaloAllowFrom({
cfg: cfg as OpenClawConfig,
cfg: cfg,
prompter,
accountId: id,
});
@@ -216,8 +214,8 @@ export const zaloOnboardingAdapter: ChannelOnboardingAdapter = {
channel,
dmPolicy,
getStatus: async ({ cfg }) => {
const configured = listZaloAccountIds(cfg as OpenClawConfig).some((accountId) =>
Boolean(resolveZaloAccount({ cfg: cfg as OpenClawConfig, accountId }).token),
const configured = listZaloAccountIds(cfg).some((accountId) =>
Boolean(resolveZaloAccount({ cfg: cfg, accountId }).token),
);
return {
channel,
@@ -235,11 +233,11 @@ export const zaloOnboardingAdapter: ChannelOnboardingAdapter = {
forceAllowFrom,
}) => {
const zaloOverride = accountOverrides.zalo?.trim();
const defaultZaloAccountId = resolveDefaultZaloAccountId(cfg as OpenClawConfig);
const defaultZaloAccountId = resolveDefaultZaloAccountId(cfg);
let zaloAccountId = zaloOverride ? normalizeAccountId(zaloOverride) : defaultZaloAccountId;
if (shouldPromptAccountIds && !zaloOverride) {
zaloAccountId = await promptAccountId({
cfg: cfg as OpenClawConfig,
cfg: cfg,
prompter,
label: "Zalo",
currentId: zaloAccountId,
@@ -248,7 +246,7 @@ export const zaloOnboardingAdapter: ChannelOnboardingAdapter = {
});
}
let next = cfg as OpenClawConfig;
let next = cfg;
const resolvedAccount = resolveZaloAccount({ cfg: next, accountId: zaloAccountId });
const accountConfigured = Boolean(resolvedAccount.token);
const allowEnv = zaloAccountId === DEFAULT_ACCOUNT_ID;
@@ -329,9 +327,9 @@ export const zaloOnboardingAdapter: ChannelOnboardingAdapter = {
...next.channels?.zalo,
enabled: true,
accounts: {
...(next.channels?.zalo?.accounts ?? {}),
...next.channels?.zalo?.accounts,
[zaloAccountId]: {
...(next.channels?.zalo?.accounts?.[zaloAccountId] ?? {}),
...next.channels?.zalo?.accounts?.[zaloAccountId],
enabled: true,
botToken: token,
},
@@ -366,7 +364,9 @@ export const zaloOnboardingAdapter: ChannelOnboardingAdapter = {
message: "Webhook secret (8-256 chars)",
validate: (value) => {
const raw = String(value ?? "");
if (raw.length < 8 || raw.length > 256) return "8-256 chars";
if (raw.length < 8 || raw.length > 256) {
return "8-256 chars";
}
return undefined;
},
}),
+7 -3
View File
@@ -7,12 +7,16 @@ const proxyCache = new Map<string, ZaloFetch>();
export function resolveZaloProxyFetch(proxyUrl?: string | null): ZaloFetch | undefined {
const trimmed = proxyUrl?.trim();
if (!trimmed) return undefined;
if (!trimmed) {
return undefined;
}
const cached = proxyCache.get(trimmed);
if (cached) return cached;
if (cached) {
return cached;
}
const agent = new ProxyAgent(trimmed);
const fetcher: ZaloFetch = (input, init) =>
undiciFetch(input, { ...(init ?? {}), dispatcher: agent as Dispatcher });
undiciFetch(input, { ...init, dispatcher: agent as Dispatcher });
proxyCache.set(trimmed, fetcher);
return fetcher;
}
+9 -3
View File
@@ -14,7 +14,9 @@ const asString = (value: unknown): string | undefined =>
typeof value === "string" ? value : typeof value === "number" ? String(value) : undefined;
function readZaloAccountStatus(value: ChannelAccountSnapshot): ZaloAccountStatus | null {
if (!isRecord(value)) return null;
if (!isRecord(value)) {
return null;
}
return {
accountId: value.accountId,
enabled: value.enabled,
@@ -27,11 +29,15 @@ export function collectZaloStatusIssues(accounts: ChannelAccountSnapshot[]): Cha
const issues: ChannelStatusIssue[] = [];
for (const entry of accounts) {
const account = readZaloAccountStatus(entry);
if (!account) continue;
if (!account) {
continue;
}
const accountId = asString(account.accountId) ?? "default";
const enabled = account.enabled !== false;
const configured = account.configured === true;
if (!enabled || !configured) continue;
if (!enabled || !configured) {
continue;
}
if (account.dmPolicy === "open") {
issues.push({
+15 -5
View File
@@ -23,12 +23,16 @@ export function resolveZaloToken(
if (accountConfig) {
const token = accountConfig.botToken?.trim();
if (token) return { token, source: "config" };
if (token) {
return { token, source: "config" };
}
const tokenFile = accountConfig.tokenFile?.trim();
if (tokenFile) {
try {
const fileToken = readFileSync(tokenFile, "utf8").trim();
if (fileToken) return { token: fileToken, source: "configFile" };
if (fileToken) {
return { token: fileToken, source: "configFile" };
}
} catch {
// ignore read failures
}
@@ -37,18 +41,24 @@ export function resolveZaloToken(
if (isDefaultAccount) {
const token = baseConfig?.botToken?.trim();
if (token) return { token, source: "config" };
if (token) {
return { token, source: "config" };
}
const tokenFile = baseConfig?.tokenFile?.trim();
if (tokenFile) {
try {
const fileToken = readFileSync(tokenFile, "utf8").trim();
if (fileToken) return { token: fileToken, source: "configFile" };
if (fileToken) {
return { token: fileToken, source: "configFile" };
}
} catch {
// ignore read failures
}
}
const envToken = process.env.ZALO_BOT_TOKEN?.trim();
if (envToken) return { token: envToken, source: "env" };
if (envToken) {
return { token: envToken, source: "env" };
}
}
return { token: "", source: "none" };