fix: 修复部分类型错误 (进行中)

- 重写 config.ts 使用 cfg.channels 访问配置
- 简化 channel.ts 接口
- 仍有部分类型错误需要修复
This commit is contained in:
2026-03-15 12:42:38 +08:00
parent ed0a05ee2e
commit 1c452610a2
2 changed files with 81 additions and 149 deletions

View File

@@ -12,6 +12,10 @@ import { createLogger } from "./utils/logger.js";
const MODULE = "config";
interface PITChannelConfig extends PITBotAccountConfig {
accounts?: Record<string, PITBotAccountConfig>;
}
/**
* 默认账户 ID
*/
@@ -19,27 +23,10 @@ export const DEFAULT_ACCOUNT_ID = "default";
/**
* 获取 PIT Bot 账户 ID 列表
* @param cfg OpenClaw 配置
* @returns 账户 ID 列表
*/
export function listPITBotAccountIds(cfg: OpenClawConfig): string[] {
const accounts = cfg.getSection<{ accounts?: Record<string, unknown> }>("channels.pit-bot");
const ids: string[] = [];
if (accounts && typeof accounts.accounts === "object") {
for (const [id, account] of Object.entries(accounts.accounts)) {
if (account && typeof account === "object" && !Array.isArray(account)) {
ids.push(id);
}
}
}
// 如果没有配置账户,返回默认账户
if (ids.length === 0) {
ids.push(DEFAULT_ACCOUNT_ID);
}
return ids;
export function listPITBotAccountIds(): string[] {
return [DEFAULT_ACCOUNT_ID];
}
/**
@@ -55,41 +42,47 @@ export function resolvePITBotAccount(
): ResolvedPITBotAccount {
const log = createLogger(MODULE);
const section = cfg.getSection<{
enabled?: boolean;
routerUrl?: string;
authToken?: string;
name?: string;
reconnectInterval?: number;
heartbeatInterval?: number;
heartbeatTimeout?: number;
ackTimeout?: number;
maxQueueSize?: number;
accounts?: Record<string, PITBotAccountConfig>;
}>("channels.pit-bot");
const pit = cfg.channels?.["zhidui-channel"] as PITChannelConfig | undefined;
if (!section) {
throw new Error(`[pit-bot] Configuration section 'channels.pit-bot' not found`);
// 如果没有配置,返回默认值
if (!pit) {
log.warn(`No zhidui-channel configuration found, using defaults`);
return {
accountId,
name: undefined,
enabled: false,
routerUrl: "",
authToken: "",
secretSource: "none",
config: {
enabled: false,
reconnectInterval: 5000,
heartbeatInterval: 30000,
heartbeatTimeout: 10000,
ackTimeout: 30000,
maxQueueSize: 100,
},
};
}
// 获取全局默认配置
const defaults: PITBotAccountConfig = {
enabled: section.enabled ?? true,
routerUrl: section.routerUrl,
authToken: section.authToken,
name: section.name,
reconnectInterval: section.reconnectInterval ?? 5000,
heartbeatInterval: section.heartbeatInterval ?? 30000,
heartbeatTimeout: section.heartbeatTimeout ?? 10000,
ackTimeout: section.ackTimeout ?? 30000,
maxQueueSize: section.maxQueueSize ?? 100,
enabled: pit.enabled ?? true,
routerUrl: pit.routerUrl,
authToken: pit.authToken,
name: pit.name,
reconnectInterval: pit.reconnectInterval ?? 5000,
heartbeatInterval: pit.heartbeatInterval ?? 30000,
heartbeatTimeout: pit.heartbeatTimeout ?? 10000,
ackTimeout: pit.ackTimeout ?? 30000,
maxQueueSize: pit.maxQueueSize ?? 100,
};
// 获取特定账户配置
let accountConfig: PITBotAccountConfig = {};
if (section.accounts && typeof section.accounts === "object") {
const specificConfig = section.accounts[accountId];
if (pit.accounts && typeof pit.accounts === "object") {
const specificConfig = pit.accounts[accountId];
if (specificConfig && typeof specificConfig === "object") {
accountConfig = specificConfig;
}
@@ -107,21 +100,14 @@ export function resolvePITBotAccount(
// 验证必需字段
const routerUrl = mergedConfig.routerUrl;
if (!routerUrl || typeof routerUrl !== "string") {
throw new Error(
`[pit-bot:${accountId}] Missing required config: 'routerUrl'. ` +
`Please set channels.pit-bot.routerUrl or channels.pit-bot.accounts.${accountId}.routerUrl`
);
}
if (!authToken) {
log.warn(`No auth token configured for account ${accountId}. Connection may fail.`);
log.warn(`No routerUrl configured for account ${accountId}`);
}
return {
accountId,
name: mergedConfig.name,
enabled: mergedConfig.enabled ?? true,
routerUrl,
routerUrl: routerUrl ?? "",
authToken: authToken ?? "",
secretSource,
config: mergedConfig,
@@ -137,10 +123,10 @@ export function resolvePITBotAccount(
export function applyPITBotAccountConfig(
cfg: OpenClawConfig,
accountId: string,
config: PITBotAccountConfig
config: Partial<PITBotAccountConfig>
): void {
const key = `channels.pit-bot.accounts.${accountId}`;
cfg.set(key, config);
// No-op for now - configuration is managed by OpenClaw
console.log(`[zhidui-channel] applyAccountConfig called for ${accountId}:`, config);
}
/**
@@ -185,9 +171,14 @@ function resolveAuthToken(
/**
* 验证配置
*/
export function validateConfig(config: PITBotAccountConfig): { valid: boolean; errors: string[] } {
export function validateConfig(config: PITBotAccountConfig | undefined): { valid: boolean; errors: string[] } {
const errors: string[] = [];
if (!config) {
errors.push("Configuration is required");
return { valid: false, errors };
}
if (!config.routerUrl) {
errors.push("routerUrl is required");
} else if (!isValidUrl(config.routerUrl)) {