Reuse status dashboard message and simplify settings actions
This commit is contained in:
@@ -2,7 +2,7 @@ import fs from 'fs';
|
||||
import path from 'path';
|
||||
|
||||
import { CACHE_DIR } from './config.js';
|
||||
import { writeJsonFile } from './utils.js';
|
||||
import { readJsonFile, writeJsonFile } from './utils.js';
|
||||
import type { GroupStatus } from './group-queue.js';
|
||||
import type { AgentType } from './types.js';
|
||||
|
||||
@@ -37,6 +37,61 @@ export interface StatusSnapshot {
|
||||
}
|
||||
|
||||
const STATUS_SNAPSHOT_DIR = path.join(CACHE_DIR, 'status-dashboard');
|
||||
const STATUS_MESSAGE_STATE_DIR = path.join(STATUS_SNAPSHOT_DIR, 'messages');
|
||||
|
||||
interface StatusMessageState {
|
||||
messageId: string;
|
||||
statusChannelId: string;
|
||||
updatedAt: string;
|
||||
}
|
||||
|
||||
function sanitizeStatusChannelId(statusChannelId: string): string {
|
||||
return statusChannelId.replace(/[^a-zA-Z0-9_-]/g, '_');
|
||||
}
|
||||
|
||||
function getStatusMessageStatePath(statusChannelId: string): string {
|
||||
return path.join(
|
||||
STATUS_MESSAGE_STATE_DIR,
|
||||
`${sanitizeStatusChannelId(statusChannelId)}.json`,
|
||||
);
|
||||
}
|
||||
|
||||
export function readDashboardStatusMessageId(
|
||||
statusChannelId: string,
|
||||
): string | null {
|
||||
if (!statusChannelId) return null;
|
||||
const parsed = readJsonFile<StatusMessageState>(
|
||||
getStatusMessageStatePath(statusChannelId),
|
||||
);
|
||||
if (
|
||||
parsed?.statusChannelId !== statusChannelId ||
|
||||
typeof parsed.messageId !== 'string' ||
|
||||
parsed.messageId.trim() === ''
|
||||
) {
|
||||
return null;
|
||||
}
|
||||
return parsed.messageId;
|
||||
}
|
||||
|
||||
export function writeDashboardStatusMessageId(
|
||||
statusChannelId: string,
|
||||
messageId: string,
|
||||
): void {
|
||||
if (!statusChannelId || !messageId) return;
|
||||
fs.mkdirSync(STATUS_MESSAGE_STATE_DIR, { recursive: true });
|
||||
const targetPath = getStatusMessageStatePath(statusChannelId);
|
||||
const tempPath = `${targetPath}.tmp`;
|
||||
writeJsonFile(
|
||||
tempPath,
|
||||
{
|
||||
messageId,
|
||||
statusChannelId,
|
||||
updatedAt: new Date().toISOString(),
|
||||
} satisfies StatusMessageState,
|
||||
true,
|
||||
);
|
||||
fs.renameSync(tempPath, targetPath);
|
||||
}
|
||||
|
||||
export function writeStatusSnapshot(snapshot: StatusSnapshot): void {
|
||||
fs.mkdirSync(STATUS_SNAPSHOT_DIR, { recursive: true });
|
||||
|
||||
Reference in New Issue
Block a user