refactor: extract verify state helpers

This commit is contained in:
Eyejoker
2026-03-31 06:51:54 +09:00
parent 30f19f994b
commit b9541a7c5b
3 changed files with 286 additions and 84 deletions

View File

@@ -9,17 +9,16 @@
*
* Uses better-sqlite3 directly (no sqlite3 CLI), platform-aware service checks.
*/
import fs from 'fs';
import path from 'path';
import { Database } from 'bun:sqlite';
import { STORE_DIR } from '../src/config.js';
import { readEnvFile } from '../src/env.js';
import { logger } from '../src/logger.js';
import { getServiceManager } from './platform.js';
import { getServiceDefs } from './service-defs.js';
import { emitStatus } from './status.js';
import {
buildVerifySummary,
detectChannelAuth,
detectCredentials,
loadRegisteredGroupsSummary,
} from './verify-state.js';
import { getServiceChecks } from './verify-services.js';
/* ------------------------------------------------------------------ */
@@ -40,84 +39,23 @@ export async function run(_args: string[]): Promise<void> {
logger.info({ service: svc.name, status: svc.status }, 'Service status');
}
// 2. Check credentials
let credentials = 'missing';
const envFile = path.join(projectRoot, '.env');
if (fs.existsSync(envFile)) {
const envContent = fs.readFileSync(envFile, 'utf-8');
if (/^(CLAUDE_CODE_OAUTH_TOKEN|ANTHROPIC_API_KEY)=/m.test(envContent)) {
credentials = 'configured';
}
}
// 3. Check channel auth (detect configured channels by credentials)
const envVars = readEnvFile(['DISCORD_BOT_TOKEN']);
const channelAuth: Record<string, string> = {};
if (process.env.DISCORD_BOT_TOKEN || envVars.DISCORD_BOT_TOKEN) {
channelAuth.discord = 'configured';
}
const configuredChannels = Object.keys(channelAuth);
const anyChannelConfigured = configuredChannels.length > 0;
// 4. Check registered groups (using better-sqlite3, not sqlite3 CLI)
let registeredGroups = 0;
let groupsByAgent: Record<string, number> = {};
const dbPath = path.join(STORE_DIR, 'messages.db');
if (fs.existsSync(dbPath)) {
try {
const db = new Database(dbPath, { readonly: true });
const row = db
.prepare('SELECT COUNT(*) as count FROM registered_groups')
.get() as { count: number };
registeredGroups = row.count;
// Count by agent type
try {
const rows = db
.prepare(
'SELECT agent_type, COUNT(*) as count FROM registered_groups GROUP BY agent_type',
)
.all() as { agent_type: string; count: number }[];
for (const r of rows) {
groupsByAgent[r.agent_type || 'unknown'] = r.count;
}
} catch {
// agent_type column might not exist in older schema
}
db.close();
} catch {
// Table might not exist
}
}
// Determine overall status
const allConfiguredServicesRunning = services.every(
(service) => service.status === 'running',
const credentials = detectCredentials(projectRoot);
const channelAuth = detectChannelAuth();
const { registeredGroups, groupsByAgent } = loadRegisteredGroupsSummary();
const {
status,
servicesSummary,
configuredChannels,
codexConfigured,
reviewConfigured,
} = buildVerifySummary(
services,
serviceDefs,
credentials,
channelAuth,
registeredGroups,
groupsByAgent,
);
const codexConfigured = serviceDefs.some(
(service) => service.kind === 'codex',
);
const reviewConfigured = serviceDefs.some(
(service) => service.kind === 'review',
);
const status =
allConfiguredServicesRunning &&
credentials !== 'missing' &&
anyChannelConfigured &&
registeredGroups > 0
? 'success'
: 'failed';
// Build service status summary
const servicesSummary: Record<string, string> = {};
for (const svc of services) {
servicesSummary[svc.name] = svc.status;
}
logger.info({ status, channelAuth, servicesSummary }, 'Verification complete');