Feat/perf dashboard (#770)
This commit is contained in:
committed by
GitHub
Unverified
parent
413244522e
commit
d8750e135b
@@ -6,8 +6,9 @@ import {
|
||||
deleteChannelConfig,
|
||||
cleanupDanglingWeChatPluginState,
|
||||
getChannelFormValues,
|
||||
listConfiguredChannelAccounts,
|
||||
listConfiguredChannelAccountsFromConfig,
|
||||
listConfiguredChannels,
|
||||
listConfiguredChannelsFromConfig,
|
||||
readOpenClawConfig,
|
||||
saveChannelConfig,
|
||||
setChannelDefaultAccount,
|
||||
@@ -20,6 +21,7 @@ import {
|
||||
clearAllBindingsForChannel,
|
||||
clearChannelBinding,
|
||||
listAgentsSnapshot,
|
||||
listAgentsSnapshotFromConfig,
|
||||
} from '../../utils/agent-config';
|
||||
import {
|
||||
ensureDingTalkPluginInstalled,
|
||||
@@ -344,16 +346,21 @@ const CHANNEL_TARGET_CACHE_ENABLED = process.env.VITEST !== 'true';
|
||||
const channelTargetCache = new Map<string, { expiresAt: number; targets: ChannelTargetOptionView[] }>();
|
||||
|
||||
async function buildChannelAccountsView(ctx: HostApiContext): Promise<ChannelAccountsView[]> {
|
||||
const [configuredChannels, configuredAccounts, openClawConfig, agentsSnapshot] = await Promise.all([
|
||||
listConfiguredChannels(),
|
||||
listConfiguredChannelAccounts(),
|
||||
readOpenClawConfig(),
|
||||
listAgentsSnapshot(),
|
||||
// Read config once and share across all sub-calls (was 5 readFile calls before).
|
||||
const openClawConfig = await readOpenClawConfig();
|
||||
|
||||
const [configuredChannels, configuredAccounts, agentsSnapshot] = await Promise.all([
|
||||
listConfiguredChannelsFromConfig(openClawConfig),
|
||||
Promise.resolve(listConfiguredChannelAccountsFromConfig(openClawConfig)),
|
||||
listAgentsSnapshotFromConfig(openClawConfig),
|
||||
]);
|
||||
|
||||
let gatewayStatus: GatewayChannelStatusPayload | null;
|
||||
try {
|
||||
gatewayStatus = await ctx.gatewayManager.rpc<GatewayChannelStatusPayload>('channels.status', { probe: true });
|
||||
// probe: false — use cached runtime state instead of active network probes
|
||||
// per channel. Real-time status updates arrive via channel.status events.
|
||||
// 8s timeout — fail fast when Gateway is busy with AI tasks.
|
||||
gatewayStatus = await ctx.gatewayManager.rpc<GatewayChannelStatusPayload>('channels.status', { probe: false }, 8000);
|
||||
} catch {
|
||||
gatewayStatus = null;
|
||||
}
|
||||
|
||||
@@ -414,7 +414,7 @@ export async function handleCronRoutes(
|
||||
|
||||
try {
|
||||
const [jobsResult, runs, sessionEntry] = await Promise.all([
|
||||
ctx.gatewayManager.rpc('cron.list', { includeDisabled: true })
|
||||
ctx.gatewayManager.rpc('cron.list', { includeDisabled: true }, 8000)
|
||||
.catch(() => ({ jobs: [] as GatewayCronJob[] })),
|
||||
readCronRunLog(parsedSession.jobId),
|
||||
readSessionStoreEntry(parsedSession.agentId, sessionKey),
|
||||
@@ -442,34 +442,66 @@ export async function handleCronRoutes(
|
||||
|
||||
if (url.pathname === '/api/cron/jobs' && req.method === 'GET') {
|
||||
try {
|
||||
const result = await ctx.gatewayManager.rpc('cron.list', { includeDisabled: true });
|
||||
const data = result as { jobs?: GatewayCronJob[] };
|
||||
const jobs = data?.jobs ?? [];
|
||||
for (const job of jobs) {
|
||||
const isIsolatedAgent =
|
||||
(job.sessionTarget === 'isolated' || !job.sessionTarget) &&
|
||||
job.payload?.kind === 'agentTurn';
|
||||
const needsRepair =
|
||||
isIsolatedAgent &&
|
||||
job.delivery?.mode === 'announce' &&
|
||||
!job.delivery?.channel;
|
||||
if (needsRepair) {
|
||||
try {
|
||||
await ctx.gatewayManager.rpc('cron.update', {
|
||||
id: job.id,
|
||||
patch: { delivery: { mode: 'none' } },
|
||||
});
|
||||
let jobs: GatewayCronJob[] = [];
|
||||
let usedFallback = false;
|
||||
|
||||
try {
|
||||
// 8s timeout — fail fast when Gateway is busy with AI tasks.
|
||||
const result = await ctx.gatewayManager.rpc('cron.list', { includeDisabled: true }, 8000);
|
||||
const data = result as { jobs?: GatewayCronJob[] };
|
||||
jobs = data?.jobs ?? (Array.isArray(result) ? result as GatewayCronJob[] : []);
|
||||
} catch {
|
||||
// Fallback: read cron.json directly when Gateway RPC fails/times out.
|
||||
try {
|
||||
const cronJsonPath = join(getOpenClawConfigDir(), 'cron', 'cron.json');
|
||||
const raw = await readFile(cronJsonPath, 'utf-8');
|
||||
const parsed = JSON.parse(raw);
|
||||
const fileJobs = Array.isArray(parsed) ? parsed : (parsed?.jobs ?? []);
|
||||
jobs = fileJobs as GatewayCronJob[];
|
||||
usedFallback = true;
|
||||
} catch {
|
||||
// No fallback data available either
|
||||
}
|
||||
}
|
||||
|
||||
// Run repair in background — don't block the response.
|
||||
if (!usedFallback && jobs.length > 0) {
|
||||
const jobsToRepair = jobs.filter((job) => {
|
||||
const isIsolatedAgent =
|
||||
(job.sessionTarget === 'isolated' || !job.sessionTarget) &&
|
||||
job.payload?.kind === 'agentTurn';
|
||||
return (
|
||||
isIsolatedAgent &&
|
||||
job.delivery?.mode === 'announce' &&
|
||||
!job.delivery?.channel
|
||||
);
|
||||
});
|
||||
if (jobsToRepair.length > 0) {
|
||||
// Fire-and-forget: repair in background
|
||||
void (async () => {
|
||||
for (const job of jobsToRepair) {
|
||||
try {
|
||||
await ctx.gatewayManager.rpc('cron.update', {
|
||||
id: job.id,
|
||||
patch: { delivery: { mode: 'none' } },
|
||||
});
|
||||
} catch {
|
||||
// ignore per-job repair failure
|
||||
}
|
||||
}
|
||||
})();
|
||||
// Optimistically fix the response data
|
||||
for (const job of jobsToRepair) {
|
||||
job.delivery = { mode: 'none' };
|
||||
if (job.state?.lastError?.includes('Channel is required')) {
|
||||
job.state.lastError = undefined;
|
||||
job.state.lastStatus = 'ok';
|
||||
}
|
||||
} catch {
|
||||
// ignore per-job repair failure
|
||||
}
|
||||
}
|
||||
}
|
||||
sendJson(res, 200, jobs.map(transformCronJob));
|
||||
|
||||
sendJson(res, 200, jobs.map((job) => ({ ...transformCronJob(job), ...(usedFallback ? { _fromFallback: true } : {}) })));
|
||||
} catch (error) {
|
||||
sendJson(res, 500, { success: false, error: String(error) });
|
||||
}
|
||||
|
||||
@@ -2,6 +2,7 @@ import { access, copyFile, mkdir, readdir, rm } from 'fs/promises';
|
||||
import { constants } from 'fs';
|
||||
import { join, normalize } from 'path';
|
||||
import { deleteAgentChannelAccounts, listConfiguredChannels, readOpenClawConfig, writeOpenClawConfig } from './channel-config';
|
||||
import type { OpenClawConfig } from './channel-config';
|
||||
import { withConfigLock } from './config-mutex';
|
||||
import { expandPath, getOpenClawConfigDir } from './paths';
|
||||
import * as logger from './logger';
|
||||
@@ -450,9 +451,9 @@ function listConfiguredAccountIdsForChannel(config: AgentConfigDocument, channel
|
||||
});
|
||||
}
|
||||
|
||||
async function buildSnapshotFromConfig(config: AgentConfigDocument): Promise<AgentsSnapshot> {
|
||||
async function buildSnapshotFromConfig(config: AgentConfigDocument, preloadedChannels?: string[]): Promise<AgentsSnapshot> {
|
||||
const { entries, defaultAgentId } = normalizeAgentsConfig(config);
|
||||
const configuredChannels = await listConfiguredChannels();
|
||||
const configuredChannels = preloadedChannels ?? await listConfiguredChannels();
|
||||
const { channelToAgent, accountToAgent } = getChannelBindingMap(config.bindings);
|
||||
const defaultAgentIdNorm = normalizeAgentIdForBinding(defaultAgentId);
|
||||
const channelOwners: Record<string, string> = {};
|
||||
@@ -539,6 +540,10 @@ export async function listAgentsSnapshot(): Promise<AgentsSnapshot> {
|
||||
return buildSnapshotFromConfig(config);
|
||||
}
|
||||
|
||||
export async function listAgentsSnapshotFromConfig(config: OpenClawConfig, configuredChannels?: string[]): Promise<AgentsSnapshot> {
|
||||
return buildSnapshotFromConfig(config as AgentConfigDocument, configuredChannels);
|
||||
}
|
||||
|
||||
export async function listConfiguredAgentIds(): Promise<string[]> {
|
||||
const config = await readOpenClawConfig() as AgentConfigDocument;
|
||||
const { entries } = normalizeAgentsConfig(config);
|
||||
|
||||
@@ -966,8 +966,7 @@ function channelHasAnyAccount(channelSection: ChannelConfigData): boolean {
|
||||
return false;
|
||||
}
|
||||
|
||||
export async function listConfiguredChannels(): Promise<string[]> {
|
||||
const config = await readOpenClawConfig();
|
||||
export async function listConfiguredChannelsFromConfig(config: OpenClawConfig): Promise<string[]> {
|
||||
const channels: string[] = [];
|
||||
|
||||
if (config.channels) {
|
||||
@@ -1005,13 +1004,17 @@ export async function listConfiguredChannels(): Promise<string[]> {
|
||||
return channels;
|
||||
}
|
||||
|
||||
export async function listConfiguredChannels(): Promise<string[]> {
|
||||
const config = await readOpenClawConfig();
|
||||
return listConfiguredChannelsFromConfig(config);
|
||||
}
|
||||
|
||||
export interface ConfiguredChannelAccounts {
|
||||
defaultAccountId: string;
|
||||
accountIds: string[];
|
||||
}
|
||||
|
||||
export async function listConfiguredChannelAccounts(): Promise<Record<string, ConfiguredChannelAccounts>> {
|
||||
const config = await readOpenClawConfig();
|
||||
export function listConfiguredChannelAccountsFromConfig(config: OpenClawConfig): Record<string, ConfiguredChannelAccounts> {
|
||||
const result: Record<string, ConfiguredChannelAccounts> = {};
|
||||
|
||||
if (!config.channels) {
|
||||
@@ -1059,6 +1062,11 @@ export async function listConfiguredChannelAccounts(): Promise<Record<string, Co
|
||||
return result;
|
||||
}
|
||||
|
||||
export async function listConfiguredChannelAccounts(): Promise<Record<string, ConfiguredChannelAccounts>> {
|
||||
const config = await readOpenClawConfig();
|
||||
return listConfiguredChannelAccountsFromConfig(config);
|
||||
}
|
||||
|
||||
export async function setChannelDefaultAccount(channelType: string, accountId: string): Promise<void> {
|
||||
return withConfigLock(async () => {
|
||||
const resolvedChannelType = resolveStoredChannelType(channelType);
|
||||
|
||||
Reference in New Issue
Block a user