chore: initialize clean metapi repository
This commit is contained in:
@@ -0,0 +1,56 @@
|
||||
import 'dotenv/config';
|
||||
|
||||
function parseBoolean(value: string | undefined, fallback = false): boolean {
|
||||
if (value === undefined) return fallback;
|
||||
const normalized = value.trim().toLowerCase();
|
||||
return normalized === '1' || normalized === 'true' || normalized === 'yes' || normalized === 'on';
|
||||
}
|
||||
|
||||
function parseNumber(value: string | undefined, fallback: number): number {
|
||||
if (value === undefined) return fallback;
|
||||
const parsed = Number(value);
|
||||
if (!Number.isFinite(parsed)) return fallback;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function parseCsvList(value: string | undefined): string[] {
|
||||
if (!value) return [];
|
||||
return value
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item.length > 0);
|
||||
}
|
||||
|
||||
export const config = {
|
||||
authToken: process.env.AUTH_TOKEN || 'change-me',
|
||||
proxyToken: process.env.PROXY_TOKEN || 'change-me-proxy',
|
||||
accountCredentialSecret: process.env.ACCOUNT_CREDENTIAL_SECRET || process.env.AUTH_TOKEN || 'change-me',
|
||||
checkinCron: process.env.CHECKIN_CRON || '0 8 * * *',
|
||||
balanceRefreshCron: process.env.BALANCE_REFRESH_CRON || '0 * * * *',
|
||||
webhookUrl: process.env.WEBHOOK_URL || '',
|
||||
barkUrl: process.env.BARK_URL || '',
|
||||
webhookEnabled: parseBoolean(process.env.WEBHOOK_ENABLED, true),
|
||||
barkEnabled: parseBoolean(process.env.BARK_ENABLED, true),
|
||||
serverChanEnabled: parseBoolean(process.env.SERVERCHAN_ENABLED, true),
|
||||
serverChanKey: process.env.SERVERCHAN_KEY || '',
|
||||
smtpEnabled: parseBoolean(process.env.SMTP_ENABLED, false),
|
||||
smtpHost: process.env.SMTP_HOST || '',
|
||||
smtpPort: parseInt(process.env.SMTP_PORT || '587'),
|
||||
smtpSecure: parseBoolean(process.env.SMTP_SECURE, false),
|
||||
smtpUser: process.env.SMTP_USER || '',
|
||||
smtpPass: process.env.SMTP_PASS || '',
|
||||
smtpFrom: process.env.SMTP_FROM || '',
|
||||
smtpTo: process.env.SMTP_TO || '',
|
||||
notifyCooldownSec: Math.max(0, Math.trunc(parseNumber(process.env.NOTIFY_COOLDOWN_SEC, 300))),
|
||||
adminIpAllowlist: parseCsvList(process.env.ADMIN_IP_ALLOWLIST),
|
||||
port: Math.trunc(parseNumber(process.env.PORT, 4000)),
|
||||
dataDir: process.env.DATA_DIR || './data',
|
||||
routingFallbackUnitCost: Math.max(1e-6, parseNumber(process.env.ROUTING_FALLBACK_UNIT_COST, 1)),
|
||||
routingWeights: {
|
||||
baseWeightFactor: parseNumber(process.env.BASE_WEIGHT_FACTOR, 0.5),
|
||||
valueScoreFactor: parseNumber(process.env.VALUE_SCORE_FACTOR, 0.5),
|
||||
costWeight: parseNumber(process.env.COST_WEIGHT, 0.4),
|
||||
balanceWeight: parseNumber(process.env.BALANCE_WEIGHT, 0.3),
|
||||
usageWeight: parseNumber(process.env.USAGE_WEIGHT, 0.3),
|
||||
},
|
||||
};
|
||||
@@ -0,0 +1,106 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||
import * as schema from './schema.js';
|
||||
import { config } from '../config.js';
|
||||
import { mkdirSync } from 'fs';
|
||||
import { dirname } from 'path';
|
||||
|
||||
const dbPath = `${config.dataDir}/hub.db`;
|
||||
mkdirSync(dirname(dbPath), { recursive: true });
|
||||
|
||||
const sqlite = new Database(dbPath);
|
||||
sqlite.pragma('journal_mode = WAL');
|
||||
sqlite.pragma('foreign_keys = ON');
|
||||
|
||||
function tableColumnExists(table: string, column: string): boolean {
|
||||
const rows = sqlite.prepare(`PRAGMA table_info(${table})`).all() as Array<{ name?: string }>;
|
||||
return rows.some((row) => row.name === column);
|
||||
}
|
||||
|
||||
function ensureTokenManagementSchema() {
|
||||
sqlite.exec(`
|
||||
CREATE TABLE IF NOT EXISTS account_tokens (
|
||||
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
account_id integer NOT NULL,
|
||||
name text NOT NULL,
|
||||
token text NOT NULL,
|
||||
source text DEFAULT 'manual',
|
||||
enabled integer DEFAULT true,
|
||||
is_default integer DEFAULT false,
|
||||
created_at text DEFAULT (datetime('now')),
|
||||
updated_at text DEFAULT (datetime('now')),
|
||||
FOREIGN KEY (account_id) REFERENCES accounts(id) ON DELETE cascade
|
||||
);
|
||||
`);
|
||||
|
||||
if (!tableColumnExists('route_channels', 'token_id')) {
|
||||
sqlite.exec('ALTER TABLE route_channels ADD COLUMN token_id integer;');
|
||||
}
|
||||
|
||||
sqlite.exec(`
|
||||
INSERT INTO account_tokens (account_id, name, token, source, enabled, is_default, created_at, updated_at)
|
||||
SELECT
|
||||
a.id,
|
||||
'default',
|
||||
a.api_token,
|
||||
'legacy',
|
||||
true,
|
||||
true,
|
||||
datetime('now'),
|
||||
datetime('now')
|
||||
FROM accounts AS a
|
||||
WHERE
|
||||
a.api_token IS NOT NULL
|
||||
AND trim(a.api_token) <> ''
|
||||
AND NOT EXISTS (
|
||||
SELECT 1 FROM account_tokens AS t
|
||||
WHERE t.account_id = a.id
|
||||
AND t.token = a.api_token
|
||||
);
|
||||
`);
|
||||
|
||||
sqlite.exec(`
|
||||
CREATE TABLE IF NOT EXISTS token_model_availability (
|
||||
id integer PRIMARY KEY AUTOINCREMENT NOT NULL,
|
||||
token_id integer NOT NULL,
|
||||
model_name text NOT NULL,
|
||||
available integer,
|
||||
latency_ms integer,
|
||||
checked_at text DEFAULT (datetime('now')),
|
||||
FOREIGN KEY (token_id) REFERENCES account_tokens(id) ON DELETE cascade
|
||||
);
|
||||
`);
|
||||
|
||||
sqlite.exec(`
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS token_model_availability_token_model_unique
|
||||
ON token_model_availability(token_id, model_name);
|
||||
`);
|
||||
}
|
||||
|
||||
function ensureSiteStatusSchema() {
|
||||
if (!tableColumnExists('sites', 'status')) {
|
||||
sqlite.exec(`ALTER TABLE sites ADD COLUMN status text DEFAULT 'active';`);
|
||||
}
|
||||
|
||||
sqlite.exec(`
|
||||
UPDATE sites
|
||||
SET status = lower(trim(status))
|
||||
WHERE status IS NOT NULL
|
||||
AND lower(trim(status)) IN ('active', 'disabled')
|
||||
AND status != lower(trim(status));
|
||||
`);
|
||||
|
||||
sqlite.exec(`
|
||||
UPDATE sites
|
||||
SET status = 'active'
|
||||
WHERE status IS NULL
|
||||
OR trim(status) = ''
|
||||
OR lower(trim(status)) NOT IN ('active', 'disabled');
|
||||
`);
|
||||
}
|
||||
|
||||
ensureTokenManagementSchema();
|
||||
ensureSiteStatusSchema();
|
||||
|
||||
export const db = drizzle(sqlite, { schema });
|
||||
export { schema };
|
||||
@@ -0,0 +1,15 @@
|
||||
import Database from 'better-sqlite3';
|
||||
import { drizzle } from 'drizzle-orm/better-sqlite3';
|
||||
import { migrate } from 'drizzle-orm/better-sqlite3/migrator';
|
||||
import { config } from '../config.js';
|
||||
import { mkdirSync } from 'fs';
|
||||
import { dirname } from 'path';
|
||||
|
||||
const dbPath = `${config.dataDir}/hub.db`;
|
||||
mkdirSync(dirname(dbPath), { recursive: true });
|
||||
|
||||
const sqlite = new Database(dbPath);
|
||||
const db = drizzle(sqlite);
|
||||
migrate(db, { migrationsFolder: './drizzle' });
|
||||
sqlite.close();
|
||||
console.log('Migration complete.');
|
||||
@@ -0,0 +1,176 @@
|
||||
import { sqliteTable, text, integer, real, uniqueIndex, index } from 'drizzle-orm/sqlite-core';
|
||||
import { sql } from 'drizzle-orm';
|
||||
|
||||
export const sites = sqliteTable('sites', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
name: text('name').notNull(),
|
||||
url: text('url').notNull(),
|
||||
platform: text('platform').notNull(), // 'new-api' | 'one-api' | 'veloera' | 'one-hub' | 'done-hub' | 'sub2api'
|
||||
status: text('status').notNull().default('active'), // 'active' | 'disabled'
|
||||
apiKey: text('api_key'),
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
statusIdx: index('sites_status_idx').on(table.status),
|
||||
}));
|
||||
|
||||
export const accounts = sqliteTable('accounts', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
siteId: integer('site_id').notNull().references(() => sites.id, { onDelete: 'cascade' }),
|
||||
username: text('username'),
|
||||
accessToken: text('access_token').notNull(),
|
||||
apiToken: text('api_token'),
|
||||
balance: real('balance').default(0),
|
||||
balanceUsed: real('balance_used').default(0),
|
||||
quota: real('quota').default(0),
|
||||
unitCost: real('unit_cost'),
|
||||
valueScore: real('value_score').default(0),
|
||||
status: text('status').default('active'), // 'active' | 'disabled' | 'expired'
|
||||
checkinEnabled: integer('checkin_enabled', { mode: 'boolean' }).default(true),
|
||||
lastCheckinAt: text('last_checkin_at'),
|
||||
lastBalanceRefresh: text('last_balance_refresh'),
|
||||
extraConfig: text('extra_config'), // JSON string
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
siteIdIdx: index('accounts_site_id_idx').on(table.siteId),
|
||||
statusIdx: index('accounts_status_idx').on(table.status),
|
||||
siteStatusIdx: index('accounts_site_status_idx').on(table.siteId, table.status),
|
||||
}));
|
||||
|
||||
export const accountTokens = sqliteTable('account_tokens', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
accountId: integer('account_id').notNull().references(() => accounts.id, { onDelete: 'cascade' }),
|
||||
name: text('name').notNull(),
|
||||
token: text('token').notNull(),
|
||||
source: text('source').default('manual'), // 'manual' | 'sync' | 'legacy'
|
||||
enabled: integer('enabled', { mode: 'boolean' }).default(true),
|
||||
isDefault: integer('is_default', { mode: 'boolean' }).default(false),
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
accountIdIdx: index('account_tokens_account_id_idx').on(table.accountId),
|
||||
accountEnabledIdx: index('account_tokens_account_enabled_idx').on(table.accountId, table.enabled),
|
||||
enabledIdx: index('account_tokens_enabled_idx').on(table.enabled),
|
||||
}));
|
||||
|
||||
export const checkinLogs = sqliteTable('checkin_logs', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
accountId: integer('account_id').notNull().references(() => accounts.id, { onDelete: 'cascade' }),
|
||||
status: text('status').notNull(), // 'success' | 'failed' | 'skipped'
|
||||
message: text('message'),
|
||||
reward: text('reward'),
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
accountCreatedIdx: index('checkin_logs_account_created_at_idx').on(table.accountId, table.createdAt),
|
||||
createdAtIdx: index('checkin_logs_created_at_idx').on(table.createdAt),
|
||||
statusIdx: index('checkin_logs_status_idx').on(table.status),
|
||||
}));
|
||||
|
||||
export const modelAvailability = sqliteTable('model_availability', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
accountId: integer('account_id').notNull().references(() => accounts.id, { onDelete: 'cascade' }),
|
||||
modelName: text('model_name').notNull(),
|
||||
available: integer('available', { mode: 'boolean' }),
|
||||
latencyMs: integer('latency_ms'),
|
||||
checkedAt: text('checked_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
accountModelUnique: uniqueIndex('model_availability_account_model_unique').on(table.accountId, table.modelName),
|
||||
accountAvailableIdx: index('model_availability_account_available_idx').on(table.accountId, table.available),
|
||||
modelNameIdx: index('model_availability_model_name_idx').on(table.modelName),
|
||||
}));
|
||||
|
||||
export const tokenModelAvailability = sqliteTable('token_model_availability', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
tokenId: integer('token_id').notNull().references(() => accountTokens.id, { onDelete: 'cascade' }),
|
||||
modelName: text('model_name').notNull(),
|
||||
available: integer('available', { mode: 'boolean' }),
|
||||
latencyMs: integer('latency_ms'),
|
||||
checkedAt: text('checked_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
tokenModelUnique: uniqueIndex('token_model_availability_token_model_unique').on(table.tokenId, table.modelName),
|
||||
tokenAvailableIdx: index('token_model_availability_token_available_idx').on(table.tokenId, table.available),
|
||||
modelNameIdx: index('token_model_availability_model_name_idx').on(table.modelName),
|
||||
availableIdx: index('token_model_availability_available_idx').on(table.available),
|
||||
}));
|
||||
|
||||
export const tokenRoutes = sqliteTable('token_routes', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
modelPattern: text('model_pattern').notNull(),
|
||||
modelMapping: text('model_mapping'), // JSON
|
||||
enabled: integer('enabled', { mode: 'boolean' }).default(true),
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
updatedAt: text('updated_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
modelPatternIdx: index('token_routes_model_pattern_idx').on(table.modelPattern),
|
||||
enabledIdx: index('token_routes_enabled_idx').on(table.enabled),
|
||||
}));
|
||||
|
||||
export const routeChannels = sqliteTable('route_channels', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
routeId: integer('route_id').notNull().references(() => tokenRoutes.id, { onDelete: 'cascade' }),
|
||||
accountId: integer('account_id').notNull().references(() => accounts.id, { onDelete: 'cascade' }),
|
||||
tokenId: integer('token_id').references(() => accountTokens.id, { onDelete: 'set null' }),
|
||||
priority: integer('priority').default(0),
|
||||
weight: integer('weight').default(10),
|
||||
enabled: integer('enabled', { mode: 'boolean' }).default(true),
|
||||
manualOverride: integer('manual_override', { mode: 'boolean' }).default(false),
|
||||
successCount: integer('success_count').default(0),
|
||||
failCount: integer('fail_count').default(0),
|
||||
totalLatencyMs: integer('total_latency_ms').default(0),
|
||||
totalCost: real('total_cost').default(0),
|
||||
lastUsedAt: text('last_used_at'),
|
||||
lastFailAt: text('last_fail_at'),
|
||||
cooldownUntil: text('cooldown_until'),
|
||||
}, (table) => ({
|
||||
routeIdIdx: index('route_channels_route_id_idx').on(table.routeId),
|
||||
accountIdIdx: index('route_channels_account_id_idx').on(table.accountId),
|
||||
tokenIdIdx: index('route_channels_token_id_idx').on(table.tokenId),
|
||||
routeEnabledIdx: index('route_channels_route_enabled_idx').on(table.routeId, table.enabled),
|
||||
routeTokenIdx: index('route_channels_route_token_idx').on(table.routeId, table.tokenId),
|
||||
}));
|
||||
|
||||
export const proxyLogs = sqliteTable('proxy_logs', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
routeId: integer('route_id'),
|
||||
channelId: integer('channel_id'),
|
||||
accountId: integer('account_id'),
|
||||
modelRequested: text('model_requested'),
|
||||
modelActual: text('model_actual'),
|
||||
status: text('status'), // 'success' | 'failed' | 'retried'
|
||||
httpStatus: integer('http_status'),
|
||||
latencyMs: integer('latency_ms'),
|
||||
promptTokens: integer('prompt_tokens'),
|
||||
completionTokens: integer('completion_tokens'),
|
||||
totalTokens: integer('total_tokens'),
|
||||
estimatedCost: real('estimated_cost'),
|
||||
errorMessage: text('error_message'),
|
||||
retryCount: integer('retry_count').default(0),
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
createdAtIdx: index('proxy_logs_created_at_idx').on(table.createdAt),
|
||||
accountCreatedIdx: index('proxy_logs_account_created_at_idx').on(table.accountId, table.createdAt),
|
||||
statusCreatedIdx: index('proxy_logs_status_created_at_idx').on(table.status, table.createdAt),
|
||||
modelActualCreatedIdx: index('proxy_logs_model_actual_created_at_idx').on(table.modelActual, table.createdAt),
|
||||
}));
|
||||
|
||||
export const settings = sqliteTable('settings', {
|
||||
key: text('key').primaryKey(),
|
||||
value: text('value'), // JSON
|
||||
});
|
||||
|
||||
export const events = sqliteTable('events', {
|
||||
id: integer('id').primaryKey({ autoIncrement: true }),
|
||||
type: text('type').notNull(), // 'checkin' | 'balance' | 'token' | 'proxy' | 'status'
|
||||
title: text('title').notNull(),
|
||||
message: text('message'),
|
||||
level: text('level').notNull().default('info'), // 'info' | 'warning' | 'error'
|
||||
read: integer('read', { mode: 'boolean' }).default(false),
|
||||
relatedId: integer('related_id'),
|
||||
relatedType: text('related_type'), // 'account' | 'site' | 'route'
|
||||
createdAt: text('created_at').default(sql`(datetime('now'))`),
|
||||
}, (table) => ({
|
||||
readCreatedIdx: index('events_read_created_at_idx').on(table.read, table.createdAt),
|
||||
typeCreatedIdx: index('events_type_created_at_idx').on(table.type, table.createdAt),
|
||||
createdAtIdx: index('events_created_at_idx').on(table.createdAt),
|
||||
}));
|
||||
@@ -0,0 +1,204 @@
|
||||
import Fastify from 'fastify';
|
||||
import cors from '@fastify/cors';
|
||||
import fastifyStatic from '@fastify/static';
|
||||
import { config } from './config.js';
|
||||
import { authMiddleware } from './middleware/auth.js';
|
||||
import { sitesRoutes } from './routes/api/sites.js';
|
||||
import { accountsRoutes } from './routes/api/accounts.js';
|
||||
import { checkinRoutes } from './routes/api/checkin.js';
|
||||
import { tokensRoutes } from './routes/api/tokens.js';
|
||||
import { statsRoutes } from './routes/api/stats.js';
|
||||
import { authRoutes } from './routes/api/auth.js';
|
||||
import { settingsRoutes } from './routes/api/settings.js';
|
||||
import { accountTokensRoutes } from './routes/api/accountTokens.js';
|
||||
import { searchRoutes } from './routes/api/search.js';
|
||||
import { eventsRoutes } from './routes/api/events.js';
|
||||
import { taskRoutes } from './routes/api/tasks.js';
|
||||
import { testRoutes } from './routes/api/test.js';
|
||||
import { monitorRoutes } from './routes/api/monitor.js';
|
||||
import { proxyRoutes } from './routes/proxy/router.js';
|
||||
import { startScheduler } from './services/checkinScheduler.js';
|
||||
import { buildStartupSummaryLines } from './services/startupInfo.js';
|
||||
import { existsSync } from 'fs';
|
||||
import { normalize, resolve, sep } from 'path';
|
||||
import { db, schema } from './db/index.js';
|
||||
|
||||
// Load runtime config overrides from settings
|
||||
try {
|
||||
const rows = db.select().from(schema.settings).all();
|
||||
const settingsMap = new Map(rows.map((row) => [row.key, row.value]));
|
||||
|
||||
const parseSetting = <T>(key: string): T | undefined => {
|
||||
const raw = settingsMap.get(key);
|
||||
if (!raw) return undefined;
|
||||
try {
|
||||
return JSON.parse(raw) as T;
|
||||
} catch {
|
||||
return undefined;
|
||||
}
|
||||
};
|
||||
|
||||
const toStringList = (value: unknown): string[] => {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
||||
.filter((item) => item.length > 0);
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item.length > 0);
|
||||
}
|
||||
return [];
|
||||
};
|
||||
|
||||
const authToken = parseSetting<string>('auth_token');
|
||||
if (typeof authToken === 'string' && authToken) config.authToken = authToken;
|
||||
|
||||
const proxyToken = parseSetting<string>('proxy_token');
|
||||
if (typeof proxyToken === 'string' && proxyToken) config.proxyToken = proxyToken;
|
||||
|
||||
const checkinCron = parseSetting<string>('checkin_cron');
|
||||
if (typeof checkinCron === 'string' && checkinCron) config.checkinCron = checkinCron;
|
||||
|
||||
const balanceRefreshCron = parseSetting<string>('balance_refresh_cron');
|
||||
if (typeof balanceRefreshCron === 'string' && balanceRefreshCron) config.balanceRefreshCron = balanceRefreshCron;
|
||||
|
||||
const routingWeights = parseSetting<Partial<typeof config.routingWeights>>('routing_weights');
|
||||
if (routingWeights && typeof routingWeights === 'object') {
|
||||
config.routingWeights = {
|
||||
...config.routingWeights,
|
||||
...routingWeights,
|
||||
};
|
||||
}
|
||||
|
||||
const routingFallbackUnitCost = parseSetting<number>('routing_fallback_unit_cost');
|
||||
if (typeof routingFallbackUnitCost === 'number' && Number.isFinite(routingFallbackUnitCost) && routingFallbackUnitCost > 0) {
|
||||
config.routingFallbackUnitCost = Math.max(1e-6, routingFallbackUnitCost);
|
||||
}
|
||||
|
||||
const webhookUrl = parseSetting<string>('webhook_url');
|
||||
if (typeof webhookUrl === 'string') config.webhookUrl = webhookUrl;
|
||||
|
||||
const barkUrl = parseSetting<string>('bark_url');
|
||||
if (typeof barkUrl === 'string') config.barkUrl = barkUrl;
|
||||
|
||||
const serverChanKey = parseSetting<string>('serverchan_key');
|
||||
if (typeof serverChanKey === 'string') config.serverChanKey = serverChanKey;
|
||||
|
||||
const smtpEnabled = parseSetting<boolean>('smtp_enabled');
|
||||
if (typeof smtpEnabled === 'boolean') config.smtpEnabled = smtpEnabled;
|
||||
|
||||
const smtpHost = parseSetting<string>('smtp_host');
|
||||
if (typeof smtpHost === 'string') config.smtpHost = smtpHost;
|
||||
|
||||
const smtpPort = parseSetting<number>('smtp_port');
|
||||
if (typeof smtpPort === 'number' && Number.isFinite(smtpPort) && smtpPort > 0) {
|
||||
config.smtpPort = smtpPort;
|
||||
}
|
||||
|
||||
const smtpSecure = parseSetting<boolean>('smtp_secure');
|
||||
if (typeof smtpSecure === 'boolean') config.smtpSecure = smtpSecure;
|
||||
|
||||
const smtpUser = parseSetting<string>('smtp_user');
|
||||
if (typeof smtpUser === 'string') config.smtpUser = smtpUser;
|
||||
|
||||
const smtpPass = parseSetting<string>('smtp_pass');
|
||||
if (typeof smtpPass === 'string') config.smtpPass = smtpPass;
|
||||
|
||||
const smtpFrom = parseSetting<string>('smtp_from');
|
||||
if (typeof smtpFrom === 'string') config.smtpFrom = smtpFrom;
|
||||
|
||||
const smtpTo = parseSetting<string>('smtp_to');
|
||||
if (typeof smtpTo === 'string') config.smtpTo = smtpTo;
|
||||
|
||||
const notifyCooldownSec = parseSetting<number>('notify_cooldown_sec');
|
||||
if (typeof notifyCooldownSec === 'number' && Number.isFinite(notifyCooldownSec) && notifyCooldownSec >= 0) {
|
||||
config.notifyCooldownSec = Math.trunc(notifyCooldownSec);
|
||||
}
|
||||
|
||||
const adminIpAllowlist = parseSetting<string[] | string>('admin_ip_allowlist');
|
||||
if (adminIpAllowlist !== undefined) {
|
||||
config.adminIpAllowlist = toStringList(adminIpAllowlist);
|
||||
}
|
||||
|
||||
console.log('Loaded runtime settings overrides');
|
||||
} catch { /* first run, table may not exist */ }
|
||||
|
||||
const app = Fastify({ logger: true });
|
||||
|
||||
await app.register(cors);
|
||||
|
||||
// Auth middleware for /api routes
|
||||
app.addHook('onRequest', async (request, reply) => {
|
||||
if (request.url.startsWith('/api/')) {
|
||||
await authMiddleware(request, reply);
|
||||
}
|
||||
});
|
||||
|
||||
// Register API routes
|
||||
await app.register(sitesRoutes);
|
||||
await app.register(accountsRoutes);
|
||||
await app.register(checkinRoutes);
|
||||
await app.register(tokensRoutes);
|
||||
await app.register(statsRoutes);
|
||||
await app.register(authRoutes);
|
||||
await app.register(settingsRoutes);
|
||||
await app.register(accountTokensRoutes);
|
||||
await app.register(searchRoutes);
|
||||
await app.register(eventsRoutes);
|
||||
await app.register(taskRoutes);
|
||||
await app.register(testRoutes);
|
||||
await app.register(monitorRoutes);
|
||||
|
||||
// Register OpenAI-compatible proxy routes
|
||||
await app.register(proxyRoutes);
|
||||
|
||||
// Serve static web frontend in production
|
||||
const webDir = resolve('dist/web');
|
||||
if (existsSync(webDir)) {
|
||||
await app.register(fastifyStatic, {
|
||||
root: webDir,
|
||||
prefix: '/',
|
||||
wildcard: false,
|
||||
setHeaders: (res, filePath) => {
|
||||
const normalizedPath = normalize(filePath);
|
||||
if (normalizedPath.includes(`${sep}assets${sep}`)) {
|
||||
res.setHeader('Cache-Control', 'public, max-age=31536000, immutable');
|
||||
return;
|
||||
}
|
||||
if (normalizedPath.endsWith(`${sep}index.html`)) {
|
||||
res.setHeader('Cache-Control', 'no-cache');
|
||||
}
|
||||
},
|
||||
});
|
||||
// SPA fallback
|
||||
app.setNotFoundHandler(async (request, reply) => {
|
||||
if (!request.url.startsWith('/api/') && !request.url.startsWith('/v1/')) {
|
||||
return reply.sendFile('index.html');
|
||||
}
|
||||
reply.code(404).send({ error: 'Not found' });
|
||||
});
|
||||
}
|
||||
|
||||
// Start scheduler
|
||||
startScheduler();
|
||||
|
||||
// Start server
|
||||
try {
|
||||
const listenHost = '0.0.0.0';
|
||||
await app.listen({ port: config.port, host: listenHost });
|
||||
const summaryLines = buildStartupSummaryLines({
|
||||
port: config.port,
|
||||
host: listenHost,
|
||||
authToken: config.authToken,
|
||||
proxyToken: config.proxyToken,
|
||||
});
|
||||
for (const line of summaryLines) {
|
||||
console.log(line);
|
||||
}
|
||||
} catch (err) {
|
||||
app.log.error(err);
|
||||
process.exit(1);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { extractClientIp, isIpAllowed } from './auth.js';
|
||||
|
||||
describe('auth middleware IP helpers', () => {
|
||||
it('extracts first forwarded IP and normalizes ipv4-mapped address', () => {
|
||||
const ip = extractClientIp('::ffff:10.0.0.1', '198.51.100.7, 203.0.113.2');
|
||||
expect(ip).toBe('198.51.100.7');
|
||||
});
|
||||
|
||||
it('allows request when allowlist is empty', () => {
|
||||
expect(isIpAllowed('203.0.113.8', [])).toBe(true);
|
||||
});
|
||||
|
||||
it('rejects non-allowlisted IP when allowlist is configured', () => {
|
||||
expect(isIpAllowed('203.0.113.8', ['203.0.113.9'])).toBe(false);
|
||||
expect(isIpAllowed('203.0.113.9', ['203.0.113.9'])).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,70 @@
|
||||
import { FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { config } from '../config.js';
|
||||
|
||||
function normalizeIp(rawIp: string | null | undefined): string {
|
||||
const ip = (rawIp || '').trim();
|
||||
if (!ip) return '';
|
||||
if (ip.startsWith('::ffff:')) return ip.slice('::ffff:'.length).trim();
|
||||
if (ip === '::1') return '127.0.0.1';
|
||||
return ip;
|
||||
}
|
||||
|
||||
export function extractClientIp(remoteIp: string | null | undefined, xForwardedFor?: string | string[] | undefined): string {
|
||||
if (Array.isArray(xForwardedFor)) {
|
||||
const first = xForwardedFor.find((item) => item && item.trim().length > 0);
|
||||
if (first) {
|
||||
return normalizeIp(first.split(',')[0]);
|
||||
}
|
||||
} else if (typeof xForwardedFor === 'string' && xForwardedFor.trim().length > 0) {
|
||||
return normalizeIp(xForwardedFor.split(',')[0]);
|
||||
}
|
||||
return normalizeIp(remoteIp);
|
||||
}
|
||||
|
||||
export function isIpAllowed(clientIp: string, allowlist: string[]): boolean {
|
||||
if (!allowlist || allowlist.length === 0) return true;
|
||||
const normalizedClientIp = normalizeIp(clientIp);
|
||||
if (!normalizedClientIp) return false;
|
||||
return allowlist.some((item) => normalizeIp(item) === normalizedClientIp);
|
||||
}
|
||||
|
||||
export async function authMiddleware(request: FastifyRequest, reply: FastifyReply) {
|
||||
const clientIp = extractClientIp(request.ip, request.headers['x-forwarded-for']);
|
||||
if (!isIpAllowed(clientIp, config.adminIpAllowlist)) {
|
||||
reply.code(403).send({ error: 'IP not allowed' });
|
||||
return;
|
||||
}
|
||||
|
||||
const auth = request.headers.authorization;
|
||||
if (!auth) {
|
||||
reply.code(401).send({ error: 'Missing Authorization header' });
|
||||
return;
|
||||
}
|
||||
const token = auth.replace('Bearer ', '');
|
||||
if (token !== config.authToken) {
|
||||
reply.code(403).send({ error: 'Invalid token' });
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
export async function proxyAuthMiddleware(request: FastifyRequest, reply: FastifyReply) {
|
||||
const auth = typeof request.headers.authorization === 'string'
|
||||
? request.headers.authorization
|
||||
: '';
|
||||
const apiKeyHeader = typeof request.headers['x-api-key'] === 'string'
|
||||
? request.headers['x-api-key']
|
||||
: '';
|
||||
const token = auth
|
||||
? auth.replace(/^Bearer\s+/i, '').trim()
|
||||
: apiKeyHeader.trim();
|
||||
|
||||
if (!token) {
|
||||
reply.code(401).send({ error: 'Missing Authorization or x-api-key header' });
|
||||
return;
|
||||
}
|
||||
|
||||
if (token !== config.proxyToken) {
|
||||
reply.code(403).send({ error: 'Invalid API key' });
|
||||
return;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,378 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll, vi } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { and, eq, sql } from 'drizzle-orm';
|
||||
|
||||
const getApiTokensMock = vi.fn();
|
||||
const getApiTokenMock = vi.fn();
|
||||
const createApiTokenMock = vi.fn();
|
||||
const getUserGroupsMock = vi.fn();
|
||||
const deleteApiTokenMock = vi.fn();
|
||||
|
||||
vi.mock('../../services/platforms/index.js', () => ({
|
||||
getAdapter: () => ({
|
||||
getApiTokens: (...args: unknown[]) => getApiTokensMock(...args),
|
||||
getApiToken: (...args: unknown[]) => getApiTokenMock(...args),
|
||||
createApiToken: (...args: unknown[]) => createApiTokenMock(...args),
|
||||
getUserGroups: (...args: unknown[]) => getUserGroupsMock(...args),
|
||||
deleteApiToken: (...args: unknown[]) => deleteApiTokenMock(...args),
|
||||
}),
|
||||
}));
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('account tokens sync routes with site status', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
let seedId = 0;
|
||||
|
||||
const nextSeed = () => {
|
||||
seedId += 1;
|
||||
return seedId;
|
||||
};
|
||||
|
||||
const seedAccount = (input: { siteStatus?: 'active' | 'disabled'; accountStatus?: string; accessToken?: string | null }) => {
|
||||
const id = nextSeed();
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: `site-${id}`,
|
||||
url: `https://site-${id}.example.com`,
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
if (input.siteStatus === 'disabled') {
|
||||
db.run(sql`update sites set status = 'disabled' where id = ${site.id}`);
|
||||
}
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: `user-${id}`,
|
||||
accessToken: input.accessToken ?? `access-token-${id}`,
|
||||
status: input.accountStatus ?? 'active',
|
||||
}).returning().get();
|
||||
|
||||
return { site, account };
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-account-tokens-sync-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./accountTokens.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.accountTokensRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
getApiTokensMock.mockReset();
|
||||
getApiTokenMock.mockReset();
|
||||
createApiTokenMock.mockReset();
|
||||
getUserGroupsMock.mockReset();
|
||||
deleteApiTokenMock.mockReset();
|
||||
seedId = 0;
|
||||
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.checkinLogs).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('returns skipped for single-account sync when site is disabled', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'disabled' });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/api/account-tokens/sync/${account.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: true,
|
||||
synced: false,
|
||||
status: 'skipped',
|
||||
reason: 'site_disabled',
|
||||
});
|
||||
expect(getApiTokensMock).not.toHaveBeenCalled();
|
||||
expect(getApiTokenMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns skipped when upstream has no api tokens', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
getApiTokensMock.mockResolvedValue([]);
|
||||
getApiTokenMock.mockResolvedValue(null);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: `/api/account-tokens/sync/${account.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: true,
|
||||
synced: false,
|
||||
status: 'skipped',
|
||||
reason: 'no_upstream_tokens',
|
||||
});
|
||||
|
||||
const tokenRows = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, account.id))
|
||||
.all();
|
||||
expect(tokenRows.length).toBe(0);
|
||||
});
|
||||
|
||||
it('sync-all skips disabled-site accounts and syncs active-site accounts', async () => {
|
||||
const disabled = seedAccount({ siteStatus: 'disabled' });
|
||||
const active = seedAccount({ siteStatus: 'active' });
|
||||
|
||||
getApiTokensMock.mockResolvedValue([
|
||||
{ name: 'default', key: 'sk-synced-token', enabled: true },
|
||||
]);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/account-tokens/sync-all',
|
||||
payload: { wait: true },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const body = response.json() as {
|
||||
success: boolean;
|
||||
summary: {
|
||||
total: number;
|
||||
synced: number;
|
||||
skipped: number;
|
||||
failed: number;
|
||||
};
|
||||
results: Array<{ accountId: number; status: string; reason?: string; synced?: boolean }>;
|
||||
};
|
||||
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.summary).toMatchObject({
|
||||
total: 2,
|
||||
synced: 1,
|
||||
skipped: 1,
|
||||
failed: 0,
|
||||
});
|
||||
|
||||
const skipped = body.results.find((item) => item.accountId === disabled.account.id);
|
||||
const synced = body.results.find((item) => item.accountId === active.account.id);
|
||||
|
||||
expect(skipped).toMatchObject({
|
||||
accountId: disabled.account.id,
|
||||
status: 'skipped',
|
||||
reason: 'site_disabled',
|
||||
});
|
||||
expect(synced).toMatchObject({
|
||||
accountId: active.account.id,
|
||||
status: 'synced',
|
||||
synced: true,
|
||||
});
|
||||
|
||||
const syncedDefaultToken = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, active.account.id), eq(schema.accountTokens.isDefault, true)))
|
||||
.get();
|
||||
expect(syncedDefaultToken?.token).toBe('sk-synced-token');
|
||||
});
|
||||
|
||||
it('creates token via upstream api and syncs into local store when manual token is omitted', async () => {
|
||||
const { account, site } = seedAccount({ siteStatus: 'active' });
|
||||
createApiTokenMock.mockResolvedValue(true);
|
||||
getApiTokensMock.mockResolvedValue([
|
||||
{ name: 'created-from-upstream', key: 'sk-created-upstream-token', enabled: true },
|
||||
]);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/account-tokens',
|
||||
payload: {
|
||||
accountId: account.id,
|
||||
name: 'created-from-upstream',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: true,
|
||||
createdViaUpstream: true,
|
||||
synced: true,
|
||||
status: 'synced',
|
||||
});
|
||||
expect(createApiTokenMock).toHaveBeenCalledTimes(1);
|
||||
expect(createApiTokenMock.mock.calls[0][0]).toBe(site.url);
|
||||
expect(createApiTokenMock.mock.calls[0][1]).toBe(account.accessToken);
|
||||
|
||||
const tokenRows = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, account.id))
|
||||
.all();
|
||||
|
||||
expect(tokenRows.length).toBe(1);
|
||||
expect(tokenRows[0].name).toBe('created-from-upstream');
|
||||
expect(tokenRows[0].token).toBe('sk-created-upstream-token');
|
||||
expect(tokenRows[0].source).toBe('sync');
|
||||
});
|
||||
|
||||
it('passes token creation options to upstream adapter', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
createApiTokenMock.mockResolvedValue(true);
|
||||
getApiTokensMock.mockResolvedValue([
|
||||
{ name: 'custom-token', key: 'sk-created-upstream-token', enabled: true },
|
||||
]);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/account-tokens',
|
||||
payload: {
|
||||
accountId: account.id,
|
||||
name: 'custom-token',
|
||||
group: 'vip',
|
||||
unlimitedQuota: false,
|
||||
remainQuota: 123456,
|
||||
expiredTime: 2_000_000_000,
|
||||
allowIps: '1.1.1.1,2.2.2.2',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(createApiTokenMock).toHaveBeenCalledTimes(1);
|
||||
expect(createApiTokenMock.mock.calls[0][3]).toMatchObject({
|
||||
name: 'custom-token',
|
||||
group: 'vip',
|
||||
unlimitedQuota: false,
|
||||
remainQuota: 123456,
|
||||
expiredTime: 2_000_000_000,
|
||||
allowIps: '1.1.1.1,2.2.2.2',
|
||||
});
|
||||
});
|
||||
|
||||
it('returns 400 when limited token misses remainQuota', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/account-tokens',
|
||||
payload: {
|
||||
accountId: account.id,
|
||||
name: 'bad-token',
|
||||
unlimitedQuota: false,
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: false,
|
||||
message: '有限额度令牌必须填写 remainQuota',
|
||||
});
|
||||
expect(createApiTokenMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('returns 502 when upstream token creation fails', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
createApiTokenMock.mockResolvedValue(false);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/account-tokens',
|
||||
payload: {
|
||||
accountId: account.id,
|
||||
name: 'created-from-upstream',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(502);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: false,
|
||||
message: '站点创建令牌失败',
|
||||
});
|
||||
});
|
||||
|
||||
it('fetches account token groups from upstream', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
getUserGroupsMock.mockResolvedValue(['default', 'vip']);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: `/api/account-tokens/groups/${account.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: true,
|
||||
groups: ['default', 'vip'],
|
||||
});
|
||||
expect(getUserGroupsMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('deletes upstream token before removing local token', async () => {
|
||||
const { account, site } = seedAccount({ siteStatus: 'active' });
|
||||
const token = db.insert(schema.accountTokens).values({
|
||||
accountId: account.id,
|
||||
name: 'upstream-token',
|
||||
token: 'sk-upstream-token',
|
||||
source: 'sync',
|
||||
enabled: true,
|
||||
isDefault: false,
|
||||
}).returning().get();
|
||||
deleteApiTokenMock.mockResolvedValue(true);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/account-tokens/${token.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(deleteApiTokenMock).toHaveBeenCalledTimes(1);
|
||||
expect(deleteApiTokenMock.mock.calls[0][0]).toBe(site.url);
|
||||
expect(deleteApiTokenMock.mock.calls[0][1]).toBe(account.accessToken);
|
||||
expect(deleteApiTokenMock.mock.calls[0][2]).toBe('sk-upstream-token');
|
||||
|
||||
const removed = db.select().from(schema.accountTokens).where(eq(schema.accountTokens.id, token.id)).get();
|
||||
expect(removed).toBeUndefined();
|
||||
});
|
||||
|
||||
it('keeps local token when upstream deletion fails', async () => {
|
||||
const { account } = seedAccount({ siteStatus: 'active' });
|
||||
const token = db.insert(schema.accountTokens).values({
|
||||
accountId: account.id,
|
||||
name: 'upstream-token',
|
||||
token: 'sk-upstream-token',
|
||||
source: 'sync',
|
||||
enabled: true,
|
||||
isDefault: false,
|
||||
}).returning().get();
|
||||
deleteApiTokenMock.mockResolvedValue(false);
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'DELETE',
|
||||
url: `/api/account-tokens/${token.id}`,
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(502);
|
||||
expect(response.json()).toMatchObject({
|
||||
success: false,
|
||||
message: '站点删除令牌失败,本地未删除',
|
||||
});
|
||||
|
||||
const existing = db.select().from(schema.accountTokens).where(eq(schema.accountTokens.id, token.id)).get();
|
||||
expect(existing).toBeDefined();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,726 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { and, eq } from 'drizzle-orm';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import {
|
||||
ensureDefaultTokenForAccount,
|
||||
listTokensWithRelations,
|
||||
normalizeTokenForDisplay,
|
||||
maskToken,
|
||||
repairDefaultToken,
|
||||
setDefaultToken,
|
||||
syncTokensFromUpstream,
|
||||
} from '../../services/accountTokenService.js';
|
||||
import { getAdapter } from '../../services/platforms/index.js';
|
||||
import { resolvePlatformUserId } from '../../services/accountExtraConfig.js';
|
||||
import { startBackgroundTask } from '../../services/backgroundTaskService.js';
|
||||
|
||||
type AccountWithSiteRow = {
|
||||
accounts: typeof schema.accounts.$inferSelect;
|
||||
sites: typeof schema.sites.$inferSelect;
|
||||
};
|
||||
|
||||
type SyncExecutionResult = {
|
||||
accountId: number;
|
||||
accountName: string;
|
||||
accountStatus: string | null;
|
||||
siteId: number;
|
||||
siteName: string;
|
||||
siteStatus: string | null;
|
||||
status: 'synced' | 'skipped' | 'failed';
|
||||
reason?: string;
|
||||
message?: string;
|
||||
synced: boolean;
|
||||
created: number;
|
||||
updated: number;
|
||||
total: number;
|
||||
defaultTokenId?: number | null;
|
||||
};
|
||||
|
||||
const TOKEN_SYNC_TIMEOUT_MS = 15_000;
|
||||
const SYNC_ALL_BATCH_SIZE = 3;
|
||||
|
||||
function buildSyncAccountLabel(item: SyncExecutionResult): string {
|
||||
const account = (item.accountName || `#${item.accountId}`).trim();
|
||||
const site = (item.siteName || 'unknown-site').trim();
|
||||
return `${account} @ ${site}`;
|
||||
}
|
||||
|
||||
function buildSyncReason(item: SyncExecutionResult): string {
|
||||
const message = String(item.message || item.reason || '').trim();
|
||||
if (!message) return '';
|
||||
if (message.length <= 32) return message;
|
||||
return `${message.slice(0, 32)}...`;
|
||||
}
|
||||
|
||||
function buildTokenSyncTaskDetailMessage(results: SyncExecutionResult[]): string {
|
||||
if (!Array.isArray(results) || results.length === 0) return '';
|
||||
|
||||
const synced = results.filter((item) => item.status === 'synced');
|
||||
const skipped = results.filter((item) => item.status === 'skipped');
|
||||
const failed = results.filter((item) => item.status === 'failed');
|
||||
|
||||
const renderRows = (rows: SyncExecutionResult[], withReason = false) => {
|
||||
const sliced = rows.slice(0, 12).map((item) => {
|
||||
const base = buildSyncAccountLabel(item);
|
||||
if (!withReason) return base;
|
||||
const reason = buildSyncReason(item);
|
||||
return reason ? `${base}(${reason})` : base;
|
||||
});
|
||||
if (rows.length > 12) sliced.push(`...等${rows.length}个`);
|
||||
return sliced.join('、');
|
||||
};
|
||||
|
||||
const segments: string[] = [
|
||||
`成功(${synced.length}): ${synced.length > 0 ? renderRows(synced) : '-'}`,
|
||||
`跳过(${skipped.length}): ${skipped.length > 0 ? renderRows(skipped, true) : '-'}`,
|
||||
`失败(${failed.length}): ${failed.length > 0 ? renderRows(failed, true) : '-'}`,
|
||||
];
|
||||
return segments.join('\n');
|
||||
}
|
||||
|
||||
function isSiteDisabled(status?: string | null): boolean {
|
||||
return (status || 'active') === 'disabled';
|
||||
}
|
||||
|
||||
function asTrimmedString(value: unknown): string | undefined {
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const trimmed = value.trim();
|
||||
return trimmed || undefined;
|
||||
}
|
||||
|
||||
function parseOptionalBoolean(value: unknown): boolean | undefined {
|
||||
if (typeof value === 'boolean') return value;
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (normalized === 'true' || normalized === '1') return true;
|
||||
if (normalized === 'false' || normalized === '0') return false;
|
||||
return undefined;
|
||||
}
|
||||
|
||||
function parsePositiveInteger(value: unknown): number | undefined {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
const normalized = Math.trunc(value);
|
||||
return normalized > 0 ? normalized : undefined;
|
||||
}
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return undefined;
|
||||
const normalized = Number.parseInt(trimmed, 10);
|
||||
if (Number.isNaN(normalized) || normalized <= 0) return undefined;
|
||||
return normalized;
|
||||
}
|
||||
|
||||
function parseExpiredTime(value: unknown): number | undefined {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
const normalized = Math.trunc(value);
|
||||
return normalized > 0 ? normalized : undefined;
|
||||
}
|
||||
if (typeof value !== 'string') return undefined;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return undefined;
|
||||
|
||||
if (/^\d+$/.test(trimmed)) {
|
||||
const numericValue = Number.parseInt(trimmed, 10);
|
||||
if (!Number.isNaN(numericValue) && numericValue > 0) return numericValue;
|
||||
}
|
||||
|
||||
const parsedMs = Date.parse(trimmed);
|
||||
if (!Number.isFinite(parsedMs)) return undefined;
|
||||
const seconds = Math.trunc(parsedMs / 1000);
|
||||
return seconds > 0 ? seconds : undefined;
|
||||
}
|
||||
|
||||
async function withTimeout<T>(fn: () => Promise<T>, timeoutMs: number, timeoutMessage: string): Promise<T> {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
try {
|
||||
return await Promise.race([
|
||||
fn(),
|
||||
new Promise<T>((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
async function executeAccountTokenSync(row: AccountWithSiteRow): Promise<SyncExecutionResult> {
|
||||
const accountId = row.accounts.id;
|
||||
const base = {
|
||||
accountId,
|
||||
accountName: row.accounts.username || `account-${accountId}`,
|
||||
accountStatus: row.accounts.status,
|
||||
siteId: row.sites.id,
|
||||
siteName: row.sites.name,
|
||||
siteStatus: row.sites.status,
|
||||
};
|
||||
|
||||
if (isSiteDisabled(row.sites.status)) {
|
||||
return {
|
||||
...base,
|
||||
status: 'skipped',
|
||||
reason: 'site_disabled',
|
||||
message: 'site disabled',
|
||||
synced: false,
|
||||
created: 0,
|
||||
updated: 0,
|
||||
total: 0,
|
||||
defaultTokenId: null,
|
||||
};
|
||||
}
|
||||
|
||||
if (!row.accounts.accessToken) {
|
||||
if (row.accounts.apiToken) {
|
||||
ensureDefaultTokenForAccount(accountId, row.accounts.apiToken, { name: 'default', source: 'legacy' });
|
||||
}
|
||||
return {
|
||||
...base,
|
||||
status: 'skipped',
|
||||
reason: 'missing_access_token',
|
||||
synced: false,
|
||||
created: 0,
|
||||
updated: 0,
|
||||
total: 0,
|
||||
defaultTokenId: null,
|
||||
};
|
||||
}
|
||||
|
||||
const adapter = getAdapter(row.sites.platform);
|
||||
if (!adapter) {
|
||||
return {
|
||||
...base,
|
||||
status: 'failed',
|
||||
reason: 'unsupported_platform',
|
||||
message: `不支持的平台: ${row.sites.platform}`,
|
||||
synced: false,
|
||||
created: 0,
|
||||
updated: 0,
|
||||
total: 0,
|
||||
defaultTokenId: null,
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
const platformUserId = resolvePlatformUserId(row.accounts.extraConfig, row.accounts.username);
|
||||
let tokens = await withTimeout(
|
||||
() => adapter.getApiTokens(row.sites.url, row.accounts.accessToken, platformUserId),
|
||||
TOKEN_SYNC_TIMEOUT_MS,
|
||||
`token sync timeout (${Math.round(TOKEN_SYNC_TIMEOUT_MS / 1000)}s)`,
|
||||
);
|
||||
|
||||
if (tokens.length === 0) {
|
||||
const fallback = await withTimeout(
|
||||
() => adapter.getApiToken(row.sites.url, row.accounts.accessToken, platformUserId),
|
||||
TOKEN_SYNC_TIMEOUT_MS,
|
||||
`token sync timeout (${Math.round(TOKEN_SYNC_TIMEOUT_MS / 1000)}s)`,
|
||||
);
|
||||
if (fallback) {
|
||||
tokens = [{ name: 'default', key: fallback, enabled: true }];
|
||||
}
|
||||
}
|
||||
|
||||
if (tokens.length === 0) {
|
||||
return {
|
||||
...base,
|
||||
status: 'skipped',
|
||||
reason: 'no_upstream_tokens',
|
||||
message: 'upstream returned no api tokens',
|
||||
synced: false,
|
||||
created: 0,
|
||||
updated: 0,
|
||||
total: 0,
|
||||
defaultTokenId: null,
|
||||
};
|
||||
}
|
||||
|
||||
const synced = syncTokensFromUpstream(accountId, tokens);
|
||||
return {
|
||||
...base,
|
||||
status: 'synced',
|
||||
synced: true,
|
||||
...synced,
|
||||
};
|
||||
} catch (error: any) {
|
||||
return {
|
||||
...base,
|
||||
status: 'failed',
|
||||
reason: 'sync_error',
|
||||
message: error?.message || 'sync failed',
|
||||
synced: false,
|
||||
created: 0,
|
||||
updated: 0,
|
||||
total: 0,
|
||||
defaultTokenId: null,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
function appendTokenSyncEvent(result: SyncExecutionResult) {
|
||||
const title = result.status === 'synced'
|
||||
? '令牌同步成功'
|
||||
: (result.status === 'skipped' ? '令牌同步跳过' : '令牌同步失败');
|
||||
const level = result.status === 'synced'
|
||||
? 'info'
|
||||
: (result.status === 'skipped' ? 'warning' : 'error');
|
||||
const detail = result.status === 'synced'
|
||||
? `新增 ${result.created},更新 ${result.updated},总数 ${result.total}`
|
||||
: (result.message || result.reason || 'sync skipped');
|
||||
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: 'token',
|
||||
title,
|
||||
message: `${result.accountName} @ ${result.siteName}: ${detail}`,
|
||||
level,
|
||||
relatedId: result.accountId,
|
||||
relatedType: 'account',
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
async function executeSyncAllAccountTokens() {
|
||||
const rows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.status, 'active'))
|
||||
.all();
|
||||
|
||||
const results: SyncExecutionResult[] = [];
|
||||
for (let offset = 0; offset < rows.length; offset += SYNC_ALL_BATCH_SIZE) {
|
||||
const batch = rows.slice(offset, offset + SYNC_ALL_BATCH_SIZE);
|
||||
const batchResults = await Promise.all(
|
||||
batch.map(async (row) => {
|
||||
const result = await executeAccountTokenSync(row);
|
||||
appendTokenSyncEvent(result);
|
||||
return result;
|
||||
}),
|
||||
);
|
||||
results.push(...batchResults);
|
||||
}
|
||||
|
||||
const summary = {
|
||||
total: results.length,
|
||||
synced: results.filter((item) => item.status === 'synced').length,
|
||||
skipped: results.filter((item) => item.status === 'skipped').length,
|
||||
failed: results.filter((item) => item.status === 'failed').length,
|
||||
created: results.reduce((acc, item) => acc + item.created, 0),
|
||||
updated: results.reduce((acc, item) => acc + item.updated, 0),
|
||||
};
|
||||
|
||||
return { summary, results };
|
||||
}
|
||||
|
||||
export async function accountTokensRoutes(app: FastifyInstance) {
|
||||
app.get<{ Querystring: { accountId?: string } }>('/api/account-tokens', async (request) => {
|
||||
const accountId = request.query.accountId ? Number.parseInt(request.query.accountId, 10) : undefined;
|
||||
return listTokensWithRelations(Number.isFinite(accountId as number) ? accountId : undefined);
|
||||
});
|
||||
|
||||
app.post<{ Body: {
|
||||
accountId: number;
|
||||
name?: string;
|
||||
token?: string;
|
||||
enabled?: boolean;
|
||||
isDefault?: boolean;
|
||||
source?: string;
|
||||
group?: string;
|
||||
unlimitedQuota?: boolean | string;
|
||||
remainQuota?: number | string;
|
||||
expiredTime?: number | string;
|
||||
allowIps?: string;
|
||||
modelLimitsEnabled?: boolean | string;
|
||||
modelLimits?: string;
|
||||
} }>('/api/account-tokens', async (request, reply) => {
|
||||
const body = request.body;
|
||||
const row = db.select()
|
||||
.from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, body.accountId))
|
||||
.get();
|
||||
if (!row) {
|
||||
return reply.code(404).send({ success: false, message: '账号不存在' });
|
||||
}
|
||||
|
||||
const tokenValue = (body.token || '').trim();
|
||||
if (tokenValue) {
|
||||
const now = new Date().toISOString();
|
||||
const existing = db.select().from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, body.accountId))
|
||||
.all();
|
||||
|
||||
const created = db.insert(schema.accountTokens).values({
|
||||
accountId: body.accountId,
|
||||
name: (body.name || '').trim() || (existing.length === 0 ? 'default' : `token-${existing.length + 1}`),
|
||||
token: tokenValue,
|
||||
source: body.source || 'manual',
|
||||
enabled: body.enabled ?? true,
|
||||
isDefault: body.isDefault ?? false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
}).returning().get();
|
||||
|
||||
if (body.isDefault || (existing.length === 0 && (body.enabled ?? true))) {
|
||||
setDefaultToken(created.id);
|
||||
} else if (existing.every((token) => !token.isDefault) && (body.enabled ?? true)) {
|
||||
setDefaultToken(created.id);
|
||||
}
|
||||
|
||||
return { success: true, token: created };
|
||||
}
|
||||
|
||||
const account = row.accounts;
|
||||
const site = row.sites;
|
||||
|
||||
if (isSiteDisabled(site.status)) {
|
||||
return reply.code(400).send({ success: false, message: '站点已禁用,无法创建令牌' });
|
||||
}
|
||||
|
||||
if (!account.accessToken?.trim()) {
|
||||
return reply.code(400).send({ success: false, message: '账号缺少访问令牌,无法创建站点令牌' });
|
||||
}
|
||||
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) {
|
||||
return reply.code(400).send({ success: false, message: `不支持的平台: ${site.platform}` });
|
||||
}
|
||||
|
||||
const unlimitedQuota = body.unlimitedQuota === undefined
|
||||
? undefined
|
||||
: parseOptionalBoolean(body.unlimitedQuota);
|
||||
if (body.unlimitedQuota !== undefined && unlimitedQuota === undefined) {
|
||||
return reply.code(400).send({ success: false, message: 'unlimitedQuota 参数无效' });
|
||||
}
|
||||
|
||||
const remainQuota = body.remainQuota === undefined
|
||||
? undefined
|
||||
: parsePositiveInteger(body.remainQuota);
|
||||
if (body.remainQuota !== undefined && remainQuota === undefined) {
|
||||
return reply.code(400).send({ success: false, message: 'remainQuota 必须是正整数' });
|
||||
}
|
||||
if (unlimitedQuota === false && remainQuota === undefined) {
|
||||
return reply.code(400).send({ success: false, message: '有限额度令牌必须填写 remainQuota' });
|
||||
}
|
||||
|
||||
const expiredTime = body.expiredTime === undefined
|
||||
? undefined
|
||||
: parseExpiredTime(body.expiredTime);
|
||||
if (body.expiredTime !== undefined && expiredTime === undefined) {
|
||||
return reply.code(400).send({ success: false, message: 'expiredTime 参数无效' });
|
||||
}
|
||||
|
||||
const modelLimitsEnabled = body.modelLimitsEnabled === undefined
|
||||
? undefined
|
||||
: parseOptionalBoolean(body.modelLimitsEnabled);
|
||||
if (body.modelLimitsEnabled !== undefined && modelLimitsEnabled === undefined) {
|
||||
return reply.code(400).send({ success: false, message: 'modelLimitsEnabled 参数无效' });
|
||||
}
|
||||
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
const createdViaUpstream = await adapter.createApiToken(
|
||||
site.url,
|
||||
account.accessToken,
|
||||
platformUserId,
|
||||
{
|
||||
name: asTrimmedString(body.name),
|
||||
group: asTrimmedString(body.group),
|
||||
unlimitedQuota,
|
||||
remainQuota,
|
||||
expiredTime,
|
||||
allowIps: asTrimmedString(body.allowIps),
|
||||
modelLimitsEnabled,
|
||||
modelLimits: asTrimmedString(body.modelLimits),
|
||||
},
|
||||
);
|
||||
if (!createdViaUpstream) {
|
||||
return reply.code(502).send({ success: false, message: '站点创建令牌失败' });
|
||||
}
|
||||
|
||||
const syncResult = await executeAccountTokenSync(row);
|
||||
appendTokenSyncEvent(syncResult);
|
||||
|
||||
if (syncResult.status === 'failed') {
|
||||
return reply.code(502).send({ success: false, message: syncResult.message || '同步站点令牌失败' });
|
||||
}
|
||||
if (syncResult.status === 'skipped') {
|
||||
return reply.code(502).send({ success: false, message: syncResult.message || '站点未返回可用令牌' });
|
||||
}
|
||||
|
||||
const preferred = db.select().from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, account.id), eq(schema.accountTokens.isDefault, true)))
|
||||
.get();
|
||||
const token = preferred || db.select().from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, account.id))
|
||||
.all()
|
||||
.slice(-1)[0] || null;
|
||||
|
||||
return {
|
||||
success: true,
|
||||
createdViaUpstream: true,
|
||||
...syncResult,
|
||||
token,
|
||||
};
|
||||
});
|
||||
|
||||
app.put<{ Params: { id: string }; Body: { name?: string; token?: string; enabled?: boolean; isDefault?: boolean; source?: string } }>('/api/account-tokens/:id', async (request, reply) => {
|
||||
const tokenId = Number.parseInt(request.params.id, 10);
|
||||
if (Number.isNaN(tokenId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌 ID 无效' });
|
||||
}
|
||||
|
||||
const existing = db.select().from(schema.accountTokens).where(eq(schema.accountTokens.id, tokenId)).get();
|
||||
if (!existing) {
|
||||
return reply.code(404).send({ success: false, message: '令牌不存在' });
|
||||
}
|
||||
|
||||
const body = request.body;
|
||||
const updates: Record<string, unknown> = { updatedAt: new Date().toISOString() };
|
||||
|
||||
if (body.name !== undefined) {
|
||||
updates.name = (body.name || '').trim() || existing.name;
|
||||
}
|
||||
|
||||
if (body.token !== undefined) {
|
||||
const tokenValue = body.token.trim();
|
||||
if (!tokenValue) {
|
||||
return reply.code(400).send({ success: false, message: '令牌不能为空' });
|
||||
}
|
||||
updates.token = tokenValue;
|
||||
}
|
||||
|
||||
if (body.enabled !== undefined) updates.enabled = body.enabled;
|
||||
if (body.source !== undefined) updates.source = body.source;
|
||||
if (body.isDefault !== undefined) updates.isDefault = body.isDefault;
|
||||
|
||||
db.update(schema.accountTokens).set(updates).where(eq(schema.accountTokens.id, tokenId)).run();
|
||||
|
||||
const latest = db.select().from(schema.accountTokens).where(eq(schema.accountTokens.id, tokenId)).get();
|
||||
if (!latest) {
|
||||
return reply.code(500).send({ success: false, message: '更新失败' });
|
||||
}
|
||||
|
||||
if (body.isDefault === true) {
|
||||
setDefaultToken(tokenId);
|
||||
} else if (latest.isDefault && latest.enabled) {
|
||||
setDefaultToken(tokenId);
|
||||
} else if (existing.isDefault && !latest.enabled) {
|
||||
repairDefaultToken(existing.accountId);
|
||||
} else if (body.isDefault === false && existing.isDefault) {
|
||||
repairDefaultToken(existing.accountId);
|
||||
}
|
||||
|
||||
return { success: true, token: latest };
|
||||
});
|
||||
|
||||
app.post<{ Params: { id: string } }>('/api/account-tokens/:id/default', async (request, reply) => {
|
||||
const tokenId = Number.parseInt(request.params.id, 10);
|
||||
if (Number.isNaN(tokenId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌 ID 无效' });
|
||||
}
|
||||
const success = setDefaultToken(tokenId);
|
||||
if (!success) {
|
||||
return reply.code(404).send({ success: false, message: '令牌不存在' });
|
||||
}
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
app.get<{ Params: { id: string } }>('/api/account-tokens/:id/value', async (request, reply) => {
|
||||
const tokenId = Number.parseInt(request.params.id, 10);
|
||||
if (Number.isNaN(tokenId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌 ID 无效' });
|
||||
}
|
||||
|
||||
const row = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accountTokens.id, tokenId))
|
||||
.get();
|
||||
if (!row) {
|
||||
return reply.code(404).send({ success: false, message: '令牌不存在' });
|
||||
}
|
||||
|
||||
const tokenValue = normalizeTokenForDisplay(row.account_tokens.token, row.sites.platform);
|
||||
return {
|
||||
success: true,
|
||||
id: row.account_tokens.id,
|
||||
name: row.account_tokens.name,
|
||||
token: tokenValue,
|
||||
tokenMasked: maskToken(row.account_tokens.token, row.sites.platform),
|
||||
};
|
||||
});
|
||||
|
||||
app.get<{ Params: { accountId: string } }>('/api/account-tokens/groups/:accountId', async (request, reply) => {
|
||||
const accountId = Number.parseInt(request.params.accountId, 10);
|
||||
if (Number.isNaN(accountId)) {
|
||||
return reply.code(400).send({ success: false, message: '账号 ID 无效' });
|
||||
}
|
||||
|
||||
const row = db.select()
|
||||
.from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.get();
|
||||
if (!row) {
|
||||
return reply.code(404).send({ success: false, message: '账号不存在' });
|
||||
}
|
||||
|
||||
const account = row.accounts;
|
||||
const site = row.sites;
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) {
|
||||
return reply.code(400).send({ success: false, message: `不支持的平台: ${site.platform}` });
|
||||
}
|
||||
if (!account.accessToken?.trim()) {
|
||||
return reply.code(400).send({ success: false, message: '账号缺少访问令牌,无法拉取分组' });
|
||||
}
|
||||
|
||||
try {
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
const groups = await adapter.getUserGroups(site.url, account.accessToken, platformUserId);
|
||||
const normalized = Array.from(new Set((groups || []).map((item) => String(item || '').trim()).filter(Boolean)));
|
||||
return { success: true, groups: normalized.length > 0 ? normalized : ['default'] };
|
||||
} catch (error: any) {
|
||||
return reply.code(502).send({
|
||||
success: false,
|
||||
message: error?.message || '拉取分组失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.delete<{ Params: { id: string } }>('/api/account-tokens/:id', async (request, reply) => {
|
||||
const tokenId = Number.parseInt(request.params.id, 10);
|
||||
if (Number.isNaN(tokenId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌 ID 无效' });
|
||||
}
|
||||
|
||||
const row = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accountTokens.id, tokenId))
|
||||
.get();
|
||||
if (!row) {
|
||||
return reply.code(404).send({ success: false, message: '令牌不存在' });
|
||||
}
|
||||
|
||||
const existing = row.account_tokens;
|
||||
const account = row.accounts;
|
||||
const site = row.sites;
|
||||
const adapter = getAdapter(site.platform);
|
||||
const shouldDeleteUpstream = !isSiteDisabled(site.status) && !!account.accessToken?.trim() && !!adapter;
|
||||
|
||||
if (shouldDeleteUpstream) {
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
const upstreamDeleted = await adapter!.deleteApiToken(
|
||||
site.url,
|
||||
account.accessToken,
|
||||
existing.token,
|
||||
platformUserId,
|
||||
);
|
||||
if (!upstreamDeleted) {
|
||||
return reply.code(502).send({ success: false, message: '站点删除令牌失败,本地未删除' });
|
||||
}
|
||||
}
|
||||
|
||||
db.delete(schema.accountTokens).where(eq(schema.accountTokens.id, tokenId)).run();
|
||||
|
||||
if (existing.isDefault) {
|
||||
repairDefaultToken(existing.accountId);
|
||||
}
|
||||
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
app.post<{ Params: { accountId: string } }>('/api/account-tokens/sync/:accountId', async (request, reply) => {
|
||||
const accountId = Number.parseInt(request.params.accountId, 10);
|
||||
if (Number.isNaN(accountId)) {
|
||||
return reply.code(400).send({ success: false, message: '账号 ID 无效' });
|
||||
}
|
||||
|
||||
const row = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.get();
|
||||
|
||||
if (!row) {
|
||||
return reply.code(404).send({ success: false, message: '账号不存在' });
|
||||
}
|
||||
|
||||
const result = await executeAccountTokenSync(row);
|
||||
appendTokenSyncEvent(result);
|
||||
if (result.status === 'failed' && result.reason === 'unsupported_platform') {
|
||||
return reply.code(400).send({ success: false, message: result.message });
|
||||
}
|
||||
if (result.status === 'failed') {
|
||||
return reply.code(502).send({ success: false, message: result.message || '同步失败' });
|
||||
}
|
||||
return { success: true, ...result };
|
||||
});
|
||||
|
||||
app.post<{ Body?: { wait?: boolean } }>('/api/account-tokens/sync-all', async (request, reply) => {
|
||||
if (request.body?.wait) {
|
||||
const syncResult = await executeSyncAllAccountTokens();
|
||||
return { success: true, ...syncResult };
|
||||
}
|
||||
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'token',
|
||||
title: '同步全部账号令牌',
|
||||
dedupeKey: 'sync-all-account-tokens',
|
||||
notifyOnFailure: true,
|
||||
successTitle: (currentTask) => {
|
||||
const summary = (currentTask.result as any)?.summary;
|
||||
if (!summary) return '同步全部账号令牌已完成';
|
||||
return `同步全部账号令牌已完成(成功${summary.synced}/跳过${summary.skipped}/失败${summary.failed})`;
|
||||
},
|
||||
failureTitle: () => '同步全部账号令牌失败',
|
||||
successMessage: (currentTask) => {
|
||||
const summary = (currentTask.result as any)?.summary;
|
||||
const results = (currentTask.result as any)?.results as SyncExecutionResult[] | undefined;
|
||||
if (!summary) return '全部账号令牌同步任务已完成';
|
||||
const detail = buildTokenSyncTaskDetailMessage(Array.isArray(results) ? results : []);
|
||||
return detail
|
||||
? `全部账号令牌同步完成:成功 ${summary.synced},跳过 ${summary.skipped},失败 ${summary.failed}\n${detail}`
|
||||
: `全部账号令牌同步完成:成功 ${summary.synced},跳过 ${summary.skipped},失败 ${summary.failed}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `全部账号令牌同步失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => executeSyncAllAccountTokens(),
|
||||
);
|
||||
|
||||
return reply.code(202).send({
|
||||
success: true,
|
||||
queued: true,
|
||||
reused,
|
||||
jobId: task.id,
|
||||
status: task.status,
|
||||
message: reused
|
||||
? '令牌同步任务执行中,请稍后查看程序日志'
|
||||
: '已开始全部账号令牌同步,请稍后查看程序日志',
|
||||
});
|
||||
});
|
||||
|
||||
app.get<{ Params: { accountId: string } }>('/api/account-tokens/account/:accountId/default', async (request, reply) => {
|
||||
const accountId = Number.parseInt(request.params.accountId, 10);
|
||||
if (Number.isNaN(accountId)) {
|
||||
return reply.code(400).send({ success: false, message: '账号 ID 无效' });
|
||||
}
|
||||
|
||||
const row = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(eq(schema.accountTokens.accountId, accountId), eq(schema.accountTokens.isDefault, true)))
|
||||
.get();
|
||||
|
||||
return {
|
||||
success: true,
|
||||
token: row
|
||||
? (() => {
|
||||
const { token: rawToken, ...meta } = row.account_tokens;
|
||||
return { ...meta, tokenMasked: maskToken(rawToken, row.sites.platform) };
|
||||
})()
|
||||
: null,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,104 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
const refreshBalanceMock = vi.fn();
|
||||
|
||||
vi.mock('../../services/balanceService.js', () => ({
|
||||
refreshBalance: (...args: unknown[]) => refreshBalanceMock(...args),
|
||||
}));
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('accounts health refresh runtime state', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-accounts-health-refresh-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./accounts.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.accountsRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
refreshBalanceMock.mockReset();
|
||||
db.delete(schema.proxyLogs).run();
|
||||
db.delete(schema.checkinLogs).run();
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('keeps degraded runtime state for unsupported checkin after health refresh', async () => {
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'Wind Hub',
|
||||
url: 'https://windhub.cc',
|
||||
platform: 'done-hub',
|
||||
}).returning().get();
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'ld6jl3djexjf',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
runtimeHealth: {
|
||||
state: 'degraded',
|
||||
reason: '站点不支持签到接口',
|
||||
source: 'checkin',
|
||||
checkedAt: '2026-02-25T18:00:00.000Z',
|
||||
},
|
||||
}),
|
||||
}).returning().get();
|
||||
|
||||
refreshBalanceMock.mockResolvedValueOnce({ balance: 100, used: 0, quota: 100 });
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/accounts/health/refresh',
|
||||
payload: { accountId: account.id, wait: true },
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as {
|
||||
success: boolean;
|
||||
summary: {
|
||||
healthy: number;
|
||||
degraded: number;
|
||||
failed: number;
|
||||
};
|
||||
results: Array<{ state: string; status: string; message: string }>;
|
||||
};
|
||||
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.summary.degraded).toBe(1);
|
||||
expect(body.summary.healthy).toBe(0);
|
||||
expect(body.summary.failed).toBe(0);
|
||||
expect(body.results[0]).toMatchObject({
|
||||
state: 'degraded',
|
||||
status: 'success',
|
||||
message: '站点不支持签到接口',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,185 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import {
|
||||
formatLocalDate,
|
||||
formatUtcSqlDateTime,
|
||||
getLocalDayRangeUtc,
|
||||
parseStoredUtcDateTime,
|
||||
} from '../../services/localTimeService.js';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('accounts api today reward fallback', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-accounts-reward-fallback-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./accounts.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.accountsRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.proxyLogs).run();
|
||||
db.delete(schema.checkinLogs).run();
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('uses today income value when checkin reward is missing', async () => {
|
||||
const today = formatLocalDate(new Date());
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'reward-site',
|
||||
url: 'https://reward-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'reward-user',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
todayIncomeSnapshot: {
|
||||
day: today,
|
||||
baseline: 12.5,
|
||||
latest: 12.5,
|
||||
updatedAt: `${today}T10:00:00.000Z`,
|
||||
},
|
||||
}),
|
||||
}).returning().get();
|
||||
|
||||
db.insert(schema.checkinLogs).values({
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
message: 'checked in',
|
||||
reward: '',
|
||||
createdAt: `${today} 10:01:00`,
|
||||
}).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/accounts',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const rows = response.json() as Array<{ id: number; todayReward: number }>;
|
||||
const target = rows.find((row) => row.id === account.id);
|
||||
expect(target?.todayReward).toBe(12.5);
|
||||
});
|
||||
|
||||
it('prefers parsed checkin reward when available', async () => {
|
||||
const today = formatLocalDate(new Date());
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'reward-site',
|
||||
url: 'https://reward-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'reward-user',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
todayIncomeSnapshot: {
|
||||
day: today,
|
||||
baseline: 10,
|
||||
latest: 14,
|
||||
updatedAt: `${today}T10:00:00.000Z`,
|
||||
},
|
||||
}),
|
||||
}).returning().get();
|
||||
|
||||
db.insert(schema.checkinLogs).values({
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
message: 'checkin success',
|
||||
reward: '1.2',
|
||||
createdAt: `${today} 10:01:00`,
|
||||
}).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/accounts',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const rows = response.json() as Array<{ id: number; todayReward: number }>;
|
||||
const target = rows.find((row) => row.id === account.id);
|
||||
expect(target?.todayReward).toBe(1.2);
|
||||
});
|
||||
|
||||
it('counts today spend only inside local-day range', async () => {
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'spend-site',
|
||||
url: 'https://spend-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'spend-user',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
const { startUtc, endUtc } = getLocalDayRangeUtc();
|
||||
const startDate = parseStoredUtcDateTime(startUtc)!;
|
||||
const endDate = parseStoredUtcDateTime(endUtc)!;
|
||||
const beforeStart = formatUtcSqlDateTime(new Date(startDate.getTime() - 60_000));
|
||||
const inRange = formatUtcSqlDateTime(new Date(startDate.getTime() + 60_000));
|
||||
const afterEnd = formatUtcSqlDateTime(new Date(endDate.getTime() + 60_000));
|
||||
|
||||
db.insert(schema.proxyLogs).values([
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 1,
|
||||
createdAt: beforeStart,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 2,
|
||||
createdAt: inRange,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 4,
|
||||
createdAt: afterEnd,
|
||||
},
|
||||
]).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/accounts',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const rows = response.json() as Array<{ id: number; todaySpend: number }>;
|
||||
const target = rows.find((row) => row.id === account.id);
|
||||
expect(target?.todaySpend).toBe(2);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,575 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, eq, gte, lt } from 'drizzle-orm';
|
||||
import { refreshBalance } from '../../services/balanceService.js';
|
||||
import { getAdapter } from '../../services/platforms/index.js';
|
||||
import { refreshModelsForAccount, rebuildTokenRoutesFromAvailability } from '../../services/modelService.js';
|
||||
import { ensureDefaultTokenForAccount, syncTokensFromUpstream } from '../../services/accountTokenService.js';
|
||||
import { guessPlatformUserIdFromUsername, mergeAccountExtraConfig } from '../../services/accountExtraConfig.js';
|
||||
import { encryptAccountPassword } from '../../services/accountCredentialService.js';
|
||||
import { startBackgroundTask } from '../../services/backgroundTaskService.js';
|
||||
import { parseCheckinRewardAmount } from '../../services/checkinRewardParser.js';
|
||||
import { estimateRewardWithTodayIncomeFallback } from '../../services/todayIncomeRewardService.js';
|
||||
import { getLocalDayRangeUtc } from '../../services/localTimeService.js';
|
||||
import {
|
||||
buildRuntimeHealthForAccount,
|
||||
setAccountRuntimeHealth,
|
||||
type RuntimeHealthState,
|
||||
} from '../../services/accountHealthService.js';
|
||||
|
||||
type AccountWithSiteRow = {
|
||||
accounts: typeof schema.accounts.$inferSelect;
|
||||
sites: typeof schema.sites.$inferSelect;
|
||||
};
|
||||
|
||||
type AccountHealthRefreshResult = {
|
||||
accountId: number;
|
||||
username: string | null;
|
||||
siteName: string;
|
||||
status: 'success' | 'failed' | 'skipped';
|
||||
state: RuntimeHealthState;
|
||||
message: string;
|
||||
};
|
||||
|
||||
function summarizeAccountHealthRefresh(results: AccountHealthRefreshResult[]) {
|
||||
return {
|
||||
total: results.length,
|
||||
healthy: results.filter((item) => item.state === 'healthy').length,
|
||||
unhealthy: results.filter((item) => item.state === 'unhealthy').length,
|
||||
degraded: results.filter((item) => item.state === 'degraded').length,
|
||||
disabled: results.filter((item) => item.state === 'disabled').length,
|
||||
unknown: results.filter((item) => item.state === 'unknown').length,
|
||||
success: results.filter((item) => item.status === 'success').length,
|
||||
failed: results.filter((item) => item.status === 'failed').length,
|
||||
skipped: results.filter((item) => item.status === 'skipped').length,
|
||||
};
|
||||
}
|
||||
|
||||
async function refreshRuntimeHealthForRow(row: AccountWithSiteRow): Promise<AccountHealthRefreshResult> {
|
||||
const accountId = row.accounts.id;
|
||||
const username = row.accounts.username;
|
||||
const siteName = row.sites.name;
|
||||
|
||||
if ((row.accounts.status || 'active') === 'disabled' || (row.sites.status || 'active') === 'disabled') {
|
||||
setAccountRuntimeHealth(accountId, {
|
||||
state: 'disabled',
|
||||
reason: '账号或站点已禁用',
|
||||
source: 'health-refresh',
|
||||
});
|
||||
return {
|
||||
accountId,
|
||||
username,
|
||||
siteName,
|
||||
status: 'skipped',
|
||||
state: 'disabled',
|
||||
message: '账号或站点已禁用',
|
||||
};
|
||||
}
|
||||
|
||||
try {
|
||||
await refreshBalance(accountId);
|
||||
const refreshedAccount = db.select().from(schema.accounts)
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.get();
|
||||
const runtimeHealth = buildRuntimeHealthForAccount({
|
||||
accountStatus: refreshedAccount?.status || row.accounts.status,
|
||||
siteStatus: row.sites.status,
|
||||
extraConfig: refreshedAccount?.extraConfig ?? row.accounts.extraConfig,
|
||||
});
|
||||
|
||||
return {
|
||||
accountId,
|
||||
username,
|
||||
siteName,
|
||||
status: runtimeHealth.state === 'unhealthy' ? 'failed' : 'success',
|
||||
state: runtimeHealth.state,
|
||||
message: runtimeHealth.reason,
|
||||
};
|
||||
} catch (error: any) {
|
||||
const message = String(error?.message || '健康检查失败');
|
||||
setAccountRuntimeHealth(accountId, {
|
||||
state: 'unhealthy',
|
||||
reason: message,
|
||||
source: 'health-refresh',
|
||||
});
|
||||
return {
|
||||
accountId,
|
||||
username,
|
||||
siteName,
|
||||
status: 'failed',
|
||||
state: 'unhealthy',
|
||||
message,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
async function executeRefreshAccountRuntimeHealth(accountId?: number) {
|
||||
const rows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.all();
|
||||
|
||||
const targetRows = Number.isFinite(accountId as number)
|
||||
? rows.filter((row) => row.accounts.id === accountId)
|
||||
: rows;
|
||||
|
||||
const results: AccountHealthRefreshResult[] = [];
|
||||
for (const row of targetRows) {
|
||||
results.push(await refreshRuntimeHealthForRow(row));
|
||||
}
|
||||
|
||||
return {
|
||||
summary: summarizeAccountHealthRefresh(results),
|
||||
results,
|
||||
};
|
||||
}
|
||||
|
||||
export async function accountsRoutes(app: FastifyInstance) {
|
||||
// List all accounts (with site info)
|
||||
app.get('/api/accounts', async () => {
|
||||
const rows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id)).all();
|
||||
|
||||
const { localDay, startUtc, endUtc } = getLocalDayRangeUtc();
|
||||
|
||||
// Aggregate today's spend per account from proxy logs
|
||||
const todayLogs = db.select().from(schema.proxyLogs)
|
||||
.where(and(gte(schema.proxyLogs.createdAt, startUtc), lt(schema.proxyLogs.createdAt, endUtc)))
|
||||
.all();
|
||||
const spendByAccount: Record<number, number> = {};
|
||||
for (const log of todayLogs) {
|
||||
if (log.accountId == null) continue;
|
||||
const cost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
spendByAccount[log.accountId] = (spendByAccount[log.accountId] || 0) + cost;
|
||||
}
|
||||
|
||||
// Aggregate today's checkin rewards per account
|
||||
const todayCheckins = db.select().from(schema.checkinLogs)
|
||||
.where(and(
|
||||
gte(schema.checkinLogs.createdAt, startUtc),
|
||||
lt(schema.checkinLogs.createdAt, endUtc),
|
||||
eq(schema.checkinLogs.status, 'success'),
|
||||
))
|
||||
.all();
|
||||
const rewardByAccount: Record<number, number> = {};
|
||||
const successCountByAccount: Record<number, number> = {};
|
||||
const parsedRewardCountByAccount: Record<number, number> = {};
|
||||
for (const log of todayCheckins) {
|
||||
successCountByAccount[log.accountId] = (successCountByAccount[log.accountId] || 0) + 1;
|
||||
const rewardNum = parseCheckinRewardAmount(log.reward) || parseCheckinRewardAmount(log.message);
|
||||
if (rewardNum <= 0) continue;
|
||||
rewardByAccount[log.accountId] = (rewardByAccount[log.accountId] || 0) + rewardNum;
|
||||
parsedRewardCountByAccount[log.accountId] = (parsedRewardCountByAccount[log.accountId] || 0) + 1;
|
||||
}
|
||||
|
||||
return rows.map((r) => ({
|
||||
...r.accounts,
|
||||
site: r.sites,
|
||||
todaySpend: Math.round((spendByAccount[r.accounts.id] || 0) * 1_000_000) / 1_000_000,
|
||||
todayReward: Math.round(estimateRewardWithTodayIncomeFallback({
|
||||
day: localDay,
|
||||
successCount: successCountByAccount[r.accounts.id] || 0,
|
||||
parsedRewardCount: parsedRewardCountByAccount[r.accounts.id] || 0,
|
||||
rewardSum: rewardByAccount[r.accounts.id] || 0,
|
||||
extraConfig: r.accounts.extraConfig,
|
||||
}) * 1_000_000) / 1_000_000,
|
||||
runtimeHealth: buildRuntimeHealthForAccount({
|
||||
accountStatus: r.accounts.status,
|
||||
siteStatus: r.sites.status,
|
||||
extraConfig: r.accounts.extraConfig,
|
||||
}),
|
||||
}));
|
||||
});
|
||||
|
||||
// Login to a site and auto-create account
|
||||
app.post<{ Body: { siteId: number; username: string; password: string } }>('/api/accounts/login', async (request) => {
|
||||
const { siteId, username, password } = request.body;
|
||||
|
||||
// Get site info
|
||||
const site = db.select().from(schema.sites).where(eq(schema.sites.id, siteId)).get();
|
||||
if (!site) return { success: false, message: 'site not found' };
|
||||
|
||||
// Get platform adapter
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return { success: false, message: `婵炴垶鎸哥粔鐢稿极椤曗偓楠炴劖鎷呴悜妯兼殸濡ょ姷鍋涢崯鑳亹? ${site.platform}` };
|
||||
|
||||
// Login to the target site
|
||||
const loginResult = await adapter.login(site.url, username, password);
|
||||
if (!loginResult.success || !loginResult.accessToken) {
|
||||
return { success: false, message: loginResult.message || 'login failed' };
|
||||
}
|
||||
|
||||
const guessedPlatformUserId = guessPlatformUserIdFromUsername(username);
|
||||
|
||||
// Auto-fetch API token(s)
|
||||
let apiToken: string | null = null;
|
||||
let apiTokens: Array<{ name?: string | null; key?: string | null; enabled?: boolean | null }> = [];
|
||||
try {
|
||||
apiToken = await adapter.getApiToken(site.url, loginResult.accessToken, guessedPlatformUserId);
|
||||
} catch { }
|
||||
try {
|
||||
apiTokens = await adapter.getApiTokens(site.url, loginResult.accessToken, guessedPlatformUserId);
|
||||
} catch { }
|
||||
|
||||
const preferredApiToken = apiTokens.find((token) => token.enabled !== false && token.key)?.key || apiToken || null;
|
||||
const existing = db.select().from(schema.accounts)
|
||||
.where(and(eq(schema.accounts.siteId, siteId), eq(schema.accounts.username, username)))
|
||||
.get();
|
||||
|
||||
const extraConfigPatch: Record<string, unknown> = {
|
||||
autoRelogin: {
|
||||
username,
|
||||
passwordCipher: encryptAccountPassword(password),
|
||||
updatedAt: new Date().toISOString(),
|
||||
},
|
||||
};
|
||||
if (guessedPlatformUserId) {
|
||||
extraConfigPatch.platformUserId = guessedPlatformUserId;
|
||||
}
|
||||
const extraConfig = mergeAccountExtraConfig(existing?.extraConfig, extraConfigPatch);
|
||||
|
||||
// Create or update account
|
||||
let accountId = existing?.id;
|
||||
if (existing) {
|
||||
db.update(schema.accounts).set({
|
||||
accessToken: loginResult.accessToken,
|
||||
apiToken: preferredApiToken || undefined,
|
||||
checkinEnabled: true,
|
||||
status: 'active',
|
||||
extraConfig,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}).where(eq(schema.accounts.id, existing.id)).run();
|
||||
} else {
|
||||
const created = db.insert(schema.accounts).values({
|
||||
siteId,
|
||||
username,
|
||||
accessToken: loginResult.accessToken,
|
||||
apiToken: preferredApiToken || undefined,
|
||||
checkinEnabled: true,
|
||||
extraConfig,
|
||||
}).returning().get();
|
||||
accountId = created.id;
|
||||
}
|
||||
|
||||
const result = db.select().from(schema.accounts).where(eq(schema.accounts.id, accountId!)).get();
|
||||
if (!result) {
|
||||
return { success: false, message: 'account create failed' };
|
||||
}
|
||||
|
||||
if (apiTokens.length > 0) {
|
||||
try {
|
||||
syncTokensFromUpstream(result.id, apiTokens);
|
||||
} catch { }
|
||||
} else if (preferredApiToken) {
|
||||
try {
|
||||
ensureDefaultTokenForAccount(result.id, preferredApiToken, { name: 'default', source: 'sync' });
|
||||
} catch { }
|
||||
}
|
||||
|
||||
// Auto-refresh balance
|
||||
try { await refreshBalance(result.id); } catch { }
|
||||
try {
|
||||
await refreshModelsForAccount(result.id);
|
||||
rebuildTokenRoutesFromAvailability();
|
||||
} catch { }
|
||||
|
||||
const account = db.select().from(schema.accounts).where(eq(schema.accounts.id, result.id)).get();
|
||||
return {
|
||||
success: true,
|
||||
account,
|
||||
apiTokenFound: !!preferredApiToken,
|
||||
tokenCount: apiTokens.length,
|
||||
reusedAccount: !!existing,
|
||||
};
|
||||
});
|
||||
|
||||
// Verify a token against a site - auto-detects token type (session vs API key)
|
||||
app.post<{ Body: { siteId: number; accessToken: string; platformUserId?: number } }>('/api/accounts/verify-token', async (request) => {
|
||||
const { siteId, accessToken, platformUserId } = request.body;
|
||||
const site = db.select().from(schema.sites).where(eq(schema.sites.id, siteId)).get();
|
||||
if (!site) return { success: false, message: 'site not found' };
|
||||
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return { success: false, message: `婵炴垶鎸哥粔鐢稿极椤曗偓楠炴劖鎷呴悜妯兼殸濡ょ姷鍋涢崯鑳亹? ${site.platform}` };
|
||||
|
||||
const result = await adapter.verifyToken(site.url, accessToken, platformUserId);
|
||||
|
||||
if (result.tokenType === 'session') {
|
||||
return {
|
||||
success: true,
|
||||
tokenType: 'session',
|
||||
userInfo: result.userInfo,
|
||||
balance: result.balance,
|
||||
apiToken: result.apiToken,
|
||||
};
|
||||
}
|
||||
|
||||
if (result.tokenType === 'apikey') {
|
||||
return {
|
||||
success: true,
|
||||
tokenType: 'apikey',
|
||||
modelCount: result.models?.length || 0,
|
||||
models: result.models?.slice(0, 10),
|
||||
};
|
||||
}
|
||||
|
||||
// Try to explain unknown failures: missing user id vs anti-bot challenge page.
|
||||
type VerifyFailureReason = 'needs-user-id' | 'shield-blocked' | null;
|
||||
const detectVerifyFailureReason = async (): Promise<VerifyFailureReason> => {
|
||||
const parseFailureReason = (bodyText: string, contentType: string): VerifyFailureReason => {
|
||||
const text = bodyText || '';
|
||||
const ct = (contentType || '').toLowerCase();
|
||||
if (ct.includes('text/html') && /var\s+arg1\s*=|acw_sc__v2|cdn_sec_tc|<script/i.test(text)) {
|
||||
return 'shield-blocked';
|
||||
}
|
||||
|
||||
try {
|
||||
const body = JSON.parse(text) as any;
|
||||
const message = typeof body?.message === 'string' ? body.message : '';
|
||||
if (/mismatch|new-api-user|user id/i.test(message)) return 'needs-user-id';
|
||||
if (/shield|challenge|captcha|acw_sc__v2|arg1/i.test(message)) return 'shield-blocked';
|
||||
} catch { }
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
try {
|
||||
const { fetch } = await import('undici');
|
||||
const candidates = new Set<string>();
|
||||
const trimmed = (accessToken || '').trim();
|
||||
const raw = trimmed.startsWith('Bearer ') ? trimmed.slice(7).trim() : trimmed;
|
||||
if (raw) {
|
||||
if (raw.includes('=')) candidates.add(raw);
|
||||
candidates.add(`session=${raw}`);
|
||||
candidates.add(`token=${raw}`);
|
||||
}
|
||||
|
||||
const headerVariants: Record<string, string>[] = [
|
||||
{ Authorization: `Bearer ${accessToken}`, 'Content-Type': 'application/json', 'New-Api-User': '0' },
|
||||
];
|
||||
|
||||
for (const cookie of candidates) {
|
||||
headerVariants.push({
|
||||
Cookie: cookie,
|
||||
'Content-Type': 'application/json',
|
||||
'X-Requested-With': 'XMLHttpRequest',
|
||||
});
|
||||
}
|
||||
|
||||
for (const headers of headerVariants) {
|
||||
try {
|
||||
const testRes = await fetch(`${site.url}/api/user/self`, { headers });
|
||||
const bodyText = await testRes.text();
|
||||
const contentType = testRes.headers.get('content-type') || '';
|
||||
const reason = parseFailureReason(bodyText, contentType);
|
||||
if (reason) return reason;
|
||||
} catch { }
|
||||
}
|
||||
} catch { }
|
||||
|
||||
return null;
|
||||
};
|
||||
|
||||
const failureReason = await detectVerifyFailureReason();
|
||||
if (failureReason === 'needs-user-id') {
|
||||
return {
|
||||
success: false,
|
||||
needsUserId: true,
|
||||
message: 'This site requires a user ID. Please fill in your site user ID.',
|
||||
};
|
||||
}
|
||||
|
||||
if (failureReason === 'shield-blocked') {
|
||||
return {
|
||||
success: false,
|
||||
shieldBlocked: true,
|
||||
message: 'This site is shielded by anti-bot challenge. Create an API key on the target site and import that key.',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
success: false,
|
||||
message: 'Token invalid: cannot use it as session token or API key',
|
||||
};
|
||||
});
|
||||
|
||||
// Add an account (manual token input) - auto-detects token type and fetches info
|
||||
app.post<{ Body: { siteId: number; username?: string; accessToken: string; apiToken?: string; platformUserId?: number; checkinEnabled?: boolean } }>('/api/accounts', async (request) => {
|
||||
const body = request.body;
|
||||
const site = db.select().from(schema.sites).where(eq(schema.sites.id, body.siteId)).get();
|
||||
|
||||
let username = body.username;
|
||||
let accessToken = body.accessToken;
|
||||
let apiToken = body.apiToken;
|
||||
let tokenType = 'unknown';
|
||||
|
||||
if (site) {
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (adapter) {
|
||||
const verifyResult = await adapter.verifyToken(site.url, body.accessToken, body.platformUserId);
|
||||
tokenType = verifyResult.tokenType;
|
||||
|
||||
if (verifyResult.tokenType === 'session') {
|
||||
// Token is a session token - can do management ops
|
||||
if (!username && verifyResult.userInfo?.username) username = verifyResult.userInfo.username;
|
||||
if (!apiToken && verifyResult.apiToken) apiToken = verifyResult.apiToken;
|
||||
} else if (verifyResult.tokenType === 'apikey') {
|
||||
// Token is an API key - store as apiToken, not accessToken
|
||||
apiToken = body.accessToken;
|
||||
accessToken = ''; // no session token available
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Store platformUserId in extraConfig for NewAPI sites that need it
|
||||
const resolvedPlatformUserId =
|
||||
body.platformUserId || guessPlatformUserIdFromUsername(username) || undefined;
|
||||
let extraConfig: string | undefined;
|
||||
if (resolvedPlatformUserId) {
|
||||
extraConfig = mergeAccountExtraConfig(undefined, { platformUserId: resolvedPlatformUserId });
|
||||
}
|
||||
|
||||
const result = db.insert(schema.accounts).values({
|
||||
siteId: body.siteId,
|
||||
username: username || undefined,
|
||||
accessToken,
|
||||
apiToken: apiToken || undefined,
|
||||
checkinEnabled: tokenType === 'session' ? (body.checkinEnabled ?? true) : false,
|
||||
extraConfig,
|
||||
}).returning().get();
|
||||
|
||||
if (apiToken) {
|
||||
try {
|
||||
ensureDefaultTokenForAccount(result.id, apiToken, { name: 'default', source: 'manual' });
|
||||
} catch { }
|
||||
}
|
||||
|
||||
if (tokenType === 'session' && accessToken && site) {
|
||||
try {
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (adapter) {
|
||||
const syncedTokens = await adapter.getApiTokens(site.url, accessToken, resolvedPlatformUserId);
|
||||
if (syncedTokens.length > 0) {
|
||||
syncTokensFromUpstream(result.id, syncedTokens);
|
||||
}
|
||||
}
|
||||
} catch { }
|
||||
}
|
||||
|
||||
// Try to refresh balance
|
||||
if (tokenType === 'session') {
|
||||
try { await refreshBalance(result.id); } catch { }
|
||||
}
|
||||
try {
|
||||
await refreshModelsForAccount(result.id);
|
||||
rebuildTokenRoutesFromAvailability();
|
||||
} catch { }
|
||||
|
||||
const account = db.select().from(schema.accounts).where(eq(schema.accounts.id, result.id)).get();
|
||||
return { ...account, tokenType, apiTokenFound: !!apiToken, usernameDetected: !!(!body.username && username) };
|
||||
});
|
||||
|
||||
// Update an account
|
||||
app.put<{ Params: { id: string }; Body: any }>('/api/accounts/:id', async (request) => {
|
||||
const id = parseInt(request.params.id);
|
||||
const body = request.body as Record<string, unknown>;
|
||||
const updates: any = {};
|
||||
for (const key of ['username', 'accessToken', 'apiToken', 'status', 'checkinEnabled', 'unitCost', 'extraConfig']) {
|
||||
if (body[key] !== undefined) updates[key] = body[key];
|
||||
}
|
||||
updates.updatedAt = new Date().toISOString();
|
||||
db.update(schema.accounts).set(updates).where(eq(schema.accounts.id, id)).run();
|
||||
|
||||
if (typeof updates.apiToken === 'string' && updates.apiToken.trim()) {
|
||||
try {
|
||||
ensureDefaultTokenForAccount(id, updates.apiToken, { name: 'default', source: 'manual' });
|
||||
} catch { }
|
||||
}
|
||||
|
||||
try {
|
||||
await refreshModelsForAccount(id);
|
||||
rebuildTokenRoutesFromAvailability();
|
||||
} catch { }
|
||||
|
||||
return db.select().from(schema.accounts).where(eq(schema.accounts.id, id)).get();
|
||||
});
|
||||
|
||||
// Delete an account
|
||||
app.delete<{ Params: { id: string } }>('/api/accounts/:id', async (request) => {
|
||||
const id = parseInt(request.params.id);
|
||||
db.delete(schema.accounts).where(eq(schema.accounts.id, id)).run();
|
||||
try {
|
||||
rebuildTokenRoutesFromAvailability();
|
||||
} catch { }
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
app.post<{ Body?: { accountId?: number; wait?: boolean } }>('/api/accounts/health/refresh', async (request, reply) => {
|
||||
const rawAccountId = request.body?.accountId as unknown;
|
||||
const hasAccountId = rawAccountId !== undefined && rawAccountId !== null && String(rawAccountId).trim() !== '';
|
||||
const accountId = hasAccountId ? Number.parseInt(String(rawAccountId), 10) : undefined;
|
||||
const wait = request.body?.wait === true;
|
||||
|
||||
if (hasAccountId && (!Number.isFinite(accountId) || (accountId as number) <= 0)) {
|
||||
return reply.code(400).send({ success: false, message: '账号 ID 无效' });
|
||||
}
|
||||
|
||||
if (wait) {
|
||||
const result = await executeRefreshAccountRuntimeHealth(accountId);
|
||||
if (accountId && result.summary.total === 0) {
|
||||
return reply.code(404).send({ success: false, message: '账号不存在' });
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
...result,
|
||||
};
|
||||
}
|
||||
|
||||
const taskTitle = accountId ? `刷新账号运行健康状态 #${accountId}` : '刷新全部账号运行健康状态';
|
||||
const dedupeKey = accountId ? `refresh-account-runtime-health-${accountId}` : 'refresh-all-account-runtime-health';
|
||||
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'status',
|
||||
title: taskTitle,
|
||||
dedupeKey,
|
||||
notifyOnFailure: true,
|
||||
successMessage: (currentTask) => {
|
||||
const summary = (currentTask.result as { summary?: ReturnType<typeof summarizeAccountHealthRefresh> })?.summary;
|
||||
if (!summary) return `${taskTitle}已完成`;
|
||||
return `${taskTitle}完成:健康 ${summary.healthy},异常 ${summary.unhealthy},禁用 ${summary.disabled}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `${taskTitle}失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => executeRefreshAccountRuntimeHealth(accountId),
|
||||
);
|
||||
|
||||
return reply.code(202).send({
|
||||
success: true,
|
||||
queued: true,
|
||||
reused,
|
||||
jobId: task.id,
|
||||
status: task.status,
|
||||
message: reused
|
||||
? '账号运行健康状态刷新进行中,请稍后查看账号列表'
|
||||
: '已开始刷新账号运行健康状态,请稍后查看账号列表',
|
||||
});
|
||||
});
|
||||
|
||||
// Refresh balance for an account
|
||||
app.post<{ Params: { id: string } }>('/api/accounts/:id/balance', async (request, reply) => {
|
||||
const id = parseInt(request.params.id);
|
||||
try {
|
||||
const result = await refreshBalance(id);
|
||||
if (!result) {
|
||||
reply.code(404);
|
||||
return { message: 'account not found or platform not supported' };
|
||||
}
|
||||
return result;
|
||||
} catch (err: any) {
|
||||
reply.code(400);
|
||||
return { message: err?.message || 'failed to fetch balance' };
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
|
||||
@@ -0,0 +1,56 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { config } from '../../config.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
export async function authRoutes(app: FastifyInstance) {
|
||||
// Change admin auth token (requires old token verification)
|
||||
app.post<{ Body: { oldToken: string; newToken: string } }>('/api/settings/auth/change', async (request, reply) => {
|
||||
const { oldToken, newToken } = request.body;
|
||||
|
||||
if (!oldToken || !newToken) {
|
||||
return reply.code(400).send({ success: false, message: '请填写所有字段' });
|
||||
}
|
||||
|
||||
if (newToken.length < 6) {
|
||||
return reply.code(400).send({ success: false, message: '新 Token 至少 6 个字符' });
|
||||
}
|
||||
|
||||
if (oldToken !== config.authToken) {
|
||||
return reply.code(403).send({ success: false, message: '旧 Token 验证失败' });
|
||||
}
|
||||
|
||||
// Save to settings table
|
||||
const existing = db.select().from(schema.settings).where(eq(schema.settings.key, 'auth_token')).get();
|
||||
if (existing) {
|
||||
db.update(schema.settings).set({ value: JSON.stringify(newToken) }).where(eq(schema.settings.key, 'auth_token')).run();
|
||||
} else {
|
||||
db.insert(schema.settings).values({ key: 'auth_token', value: JSON.stringify(newToken) }).run();
|
||||
}
|
||||
|
||||
// Update runtime config
|
||||
config.authToken = newToken;
|
||||
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: 'token',
|
||||
title: '管理员登录令牌已更新',
|
||||
message: '管理员登录 Token 已被修改,请使用新 Token 登录。',
|
||||
level: 'warning',
|
||||
relatedType: 'settings',
|
||||
createdAt: new Date().toISOString(),
|
||||
}).run();
|
||||
} catch {}
|
||||
|
||||
return { success: true, message: 'Token 已更新' };
|
||||
});
|
||||
|
||||
// Get masked current token (for display)
|
||||
app.get('/api/settings/auth/info', async () => {
|
||||
const token = config.authToken;
|
||||
const masked = token.length > 8
|
||||
? token.slice(0, 4) + '****' + token.slice(-4)
|
||||
: '****';
|
||||
return { masked };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,90 @@
|
||||
import Fastify from 'fastify';
|
||||
import { afterEach, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const checkinAllMock = vi.fn();
|
||||
const checkinAccountMock = vi.fn();
|
||||
|
||||
vi.mock('../../services/checkinService.js', () => ({
|
||||
checkinAll: (...args: unknown[]) => checkinAllMock(...args),
|
||||
checkinAccount: (...args: unknown[]) => checkinAccountMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/checkinScheduler.js', () => ({
|
||||
updateCheckinCron: vi.fn(),
|
||||
}));
|
||||
|
||||
vi.mock('../../db/index.js', () => {
|
||||
const insertChain = {
|
||||
values: () => insertChain,
|
||||
onConflictDoUpdate: () => insertChain,
|
||||
run: () => ({ changes: 1 }),
|
||||
};
|
||||
|
||||
const queryChain = {
|
||||
where: () => queryChain,
|
||||
all: () => [],
|
||||
limit: () => queryChain,
|
||||
offset: () => queryChain,
|
||||
orderBy: () => queryChain,
|
||||
innerJoin: () => queryChain,
|
||||
from: () => queryChain,
|
||||
};
|
||||
|
||||
return {
|
||||
db: {
|
||||
insert: () => insertChain,
|
||||
select: () => queryChain,
|
||||
},
|
||||
schema: {
|
||||
settings: { key: 'key' },
|
||||
checkinLogs: { accountId: 'accountId', createdAt: 'createdAt' },
|
||||
accounts: { id: 'id' },
|
||||
events: { id: 'id' },
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
describe('POST /api/checkin/trigger background task dedupe', () => {
|
||||
beforeEach(async () => {
|
||||
checkinAllMock.mockReset();
|
||||
checkinAccountMock.mockReset();
|
||||
checkinAccountMock.mockResolvedValue({ success: true, message: 'ok' });
|
||||
const { __resetBackgroundTasksForTests } = await import('../../services/backgroundTaskService.js');
|
||||
__resetBackgroundTasksForTests();
|
||||
});
|
||||
|
||||
afterEach(() => {
|
||||
vi.clearAllMocks();
|
||||
});
|
||||
|
||||
it('reuses the same background task while checkin-all is already running', async () => {
|
||||
let resolveFirst: (value: Array<unknown>) => void = () => {};
|
||||
const firstRun = new Promise<Array<unknown>>((resolve) => {
|
||||
resolveFirst = resolve;
|
||||
});
|
||||
checkinAllMock.mockImplementation(() => firstRun);
|
||||
|
||||
const { checkinRoutes } = await import('./checkin.js');
|
||||
const app = Fastify();
|
||||
await app.register(checkinRoutes);
|
||||
|
||||
const firstResponse = await app.inject({ method: 'POST', url: '/api/checkin/trigger' });
|
||||
expect(firstResponse.statusCode).toBe(202);
|
||||
const firstBody = firstResponse.json() as { success: boolean; queued: boolean; jobId: string };
|
||||
expect(firstBody.success).toBe(true);
|
||||
expect(firstBody.queued).toBe(true);
|
||||
expect(typeof firstBody.jobId).toBe('string');
|
||||
expect(firstBody.jobId.length).toBeGreaterThan(10);
|
||||
|
||||
const secondResponse = await app.inject({ method: 'POST', url: '/api/checkin/trigger' });
|
||||
expect(secondResponse.statusCode).toBe(202);
|
||||
const secondBody = secondResponse.json() as { reused: boolean; jobId: string };
|
||||
expect(secondBody.reused).toBe(true);
|
||||
expect(secondBody.jobId).toBe(firstBody.jobId);
|
||||
expect(checkinAllMock).toHaveBeenCalledTimes(1);
|
||||
|
||||
resolveFirst([]);
|
||||
await new Promise((resolve) => setTimeout(resolve, 20));
|
||||
await app.close();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,154 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { eq, desc } from 'drizzle-orm';
|
||||
import { checkinAccount, checkinAll } from '../../services/checkinService.js';
|
||||
import { updateCheckinCron } from '../../services/checkinScheduler.js';
|
||||
import { startBackgroundTask, summarizeCheckinResults } from '../../services/backgroundTaskService.js';
|
||||
import { classifyFailureReason } from '../../services/failureReasonService.js';
|
||||
|
||||
function buildCheckinAccountLabel(item: any): string {
|
||||
const username = item?.username || (item?.accountId ? `#${item.accountId}` : 'unknown');
|
||||
const site = item?.site || 'unknown-site';
|
||||
return `${username} @ ${site}`;
|
||||
}
|
||||
|
||||
function buildCheckinReason(item: any): string {
|
||||
const message = String(item?.result?.message || '').trim();
|
||||
if (!message) return '';
|
||||
if (message.length <= 32) return message;
|
||||
return `${message.slice(0, 32)}...`;
|
||||
}
|
||||
|
||||
function buildCheckinTaskDetailMessage(results: any[]): string {
|
||||
if (!Array.isArray(results) || results.length === 0) return '';
|
||||
|
||||
const successRows = results.filter((item) => {
|
||||
const status = item?.result?.status;
|
||||
if (status === 'skipped' || item?.result?.skipped) return false;
|
||||
return !!item?.result?.success;
|
||||
});
|
||||
const skippedRows = results.filter((item) => {
|
||||
const status = item?.result?.status;
|
||||
return status === 'skipped' || !!item?.result?.skipped;
|
||||
});
|
||||
const failedRows = results.filter((item) => {
|
||||
const status = item?.result?.status;
|
||||
if (status === 'skipped' || item?.result?.skipped) return false;
|
||||
return !item?.result?.success;
|
||||
});
|
||||
|
||||
const renderRows = (rows: any[], withReason = false) => {
|
||||
const sliced = rows.slice(0, 12).map((item) => {
|
||||
const base = buildCheckinAccountLabel(item);
|
||||
if (!withReason) return base;
|
||||
const reason = buildCheckinReason(item);
|
||||
return reason ? `${base}(${reason})` : base;
|
||||
});
|
||||
if (rows.length > 12) sliced.push(`...等${rows.length}个`);
|
||||
return sliced.join('、');
|
||||
};
|
||||
|
||||
const segments: string[] = [
|
||||
`成功(${successRows.length}): ${successRows.length > 0 ? renderRows(successRows) : '-'}`,
|
||||
`跳过(${skippedRows.length}): ${skippedRows.length > 0 ? renderRows(skippedRows, true) : '-'}`,
|
||||
`失败(${failedRows.length}): ${failedRows.length > 0 ? renderRows(failedRows, true) : '-'}`,
|
||||
];
|
||||
return segments.join('\n');
|
||||
}
|
||||
|
||||
export async function checkinRoutes(app: FastifyInstance) {
|
||||
// Trigger check-in for all accounts
|
||||
app.post('/api/checkin/trigger', async (_, reply) => {
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'checkin',
|
||||
title: '全部账号签到',
|
||||
dedupeKey: 'checkin-all',
|
||||
notifyOnFailure: true,
|
||||
successTitle: (currentTask) => {
|
||||
const summary = (currentTask.result as any)?.summary;
|
||||
if (!summary) return '全部账号签到已完成';
|
||||
return `全部账号签到已完成(成功${summary.success}/跳过${summary.skipped}/失败${summary.failed})`;
|
||||
},
|
||||
failureTitle: () => '全部账号签到失败',
|
||||
successMessage: (currentTask) => {
|
||||
const summary = (currentTask.result as any)?.summary;
|
||||
const results = (currentTask.result as any)?.results;
|
||||
if (!summary) return '全部账号签到任务已完成';
|
||||
const detail = buildCheckinTaskDetailMessage(Array.isArray(results) ? results : []);
|
||||
return detail
|
||||
? `全部账号签到完成:成功 ${summary.success},跳过 ${summary.skipped},失败 ${summary.failed}\n${detail}`
|
||||
: `全部账号签到完成:成功 ${summary.success},跳过 ${summary.skipped},失败 ${summary.failed}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `全部账号签到任务失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => {
|
||||
const results = await checkinAll();
|
||||
return {
|
||||
summary: summarizeCheckinResults(results),
|
||||
total: results.length,
|
||||
results,
|
||||
};
|
||||
},
|
||||
);
|
||||
|
||||
return reply.code(202).send({
|
||||
success: true,
|
||||
queued: true,
|
||||
reused,
|
||||
jobId: task.id,
|
||||
status: task.status,
|
||||
message: reused
|
||||
? '签到任务执行中,请稍后查看签到日志'
|
||||
: '已开始全部签到,请稍后查看签到日志',
|
||||
});
|
||||
});
|
||||
|
||||
// Trigger check-in for a specific account
|
||||
app.post<{ Params: { id: string } }>('/api/checkin/trigger/:id', async (request) => {
|
||||
const id = parseInt(request.params.id, 10);
|
||||
const result = await checkinAccount(id);
|
||||
return result;
|
||||
});
|
||||
|
||||
// Get check-in logs
|
||||
app.get<{ Querystring: { limit?: string; offset?: string; accountId?: string } }>('/api/checkin/logs', async (request) => {
|
||||
const limit = parseInt(request.query.limit || '50', 10);
|
||||
const offset = parseInt(request.query.offset || '0', 10);
|
||||
let query = db.select().from(schema.checkinLogs)
|
||||
.innerJoin(schema.accounts, eq(schema.checkinLogs.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.orderBy(desc(schema.checkinLogs.createdAt))
|
||||
.limit(limit)
|
||||
.offset(offset);
|
||||
|
||||
if (request.query.accountId) {
|
||||
query = query.where(eq(schema.checkinLogs.accountId, parseInt(request.query.accountId, 10))) as any;
|
||||
}
|
||||
|
||||
const rows = query.all();
|
||||
return rows.map((row: any) => {
|
||||
const source = row?.checkin_logs || row;
|
||||
const failureReason = classifyFailureReason({
|
||||
message: source?.message,
|
||||
status: source?.status,
|
||||
});
|
||||
return {
|
||||
...row,
|
||||
failureReason,
|
||||
};
|
||||
});
|
||||
});
|
||||
|
||||
// Update check-in schedule
|
||||
app.put<{ Body: { cron: string } }>('/api/checkin/schedule', async (request) => {
|
||||
try {
|
||||
updateCheckinCron(request.body.cron);
|
||||
db.insert(schema.settings).values({ key: 'checkin_cron', value: JSON.stringify(request.body.cron) })
|
||||
.onConflictDoUpdate({ target: schema.settings.key, set: { value: JSON.stringify(request.body.cron) } }).run();
|
||||
return { success: true, cron: request.body.cron };
|
||||
} catch (err: any) {
|
||||
return { error: err.message };
|
||||
}
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,60 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, desc, eq, sql } from 'drizzle-orm';
|
||||
|
||||
export async function eventsRoutes(app: FastifyInstance) {
|
||||
// List events
|
||||
app.get<{ Querystring: { limit?: string; offset?: string; type?: string; read?: string } }>('/api/events', async (request) => {
|
||||
const limit = Math.max(1, Math.min(500, parseInt(request.query.limit || '30', 10)));
|
||||
const offset = Math.max(0, parseInt(request.query.offset || '0', 10));
|
||||
const type = request.query.type;
|
||||
const readQuery = request.query.read;
|
||||
|
||||
const filters = [];
|
||||
if (type) filters.push(eq(schema.events.type, type));
|
||||
if (readQuery === 'true') filters.push(eq(schema.events.read, true));
|
||||
if (readQuery === 'false') filters.push(eq(schema.events.read, false));
|
||||
|
||||
const base = db.select().from(schema.events);
|
||||
if (filters.length > 0) {
|
||||
return base
|
||||
.where(and(...filters))
|
||||
.orderBy(desc(schema.events.createdAt))
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.all();
|
||||
}
|
||||
|
||||
return base
|
||||
.orderBy(desc(schema.events.createdAt))
|
||||
.limit(limit)
|
||||
.offset(offset)
|
||||
.all();
|
||||
});
|
||||
|
||||
// Unread count
|
||||
app.get('/api/events/count', async () => {
|
||||
const result = db.select({ count: sql<number>`count(*)` }).from(schema.events)
|
||||
.where(eq(schema.events.read, false)).get();
|
||||
return { count: result?.count || 0 };
|
||||
});
|
||||
|
||||
// Mark one as read
|
||||
app.post<{ Params: { id: string } }>('/api/events/:id/read', async (request) => {
|
||||
const id = parseInt(request.params.id);
|
||||
db.update(schema.events).set({ read: true }).where(eq(schema.events.id, id)).run();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// Mark all as read
|
||||
app.post('/api/events/read-all', async () => {
|
||||
db.update(schema.events).set({ read: true }).where(eq(schema.events.read, false)).run();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// Clear all events
|
||||
app.delete('/api/events', async () => {
|
||||
db.delete(schema.events).run();
|
||||
return { success: true };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,215 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { config } from '../../config.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
const MONITOR_AUTH_COOKIE = 'meta_monitor_auth';
|
||||
const LDOH_BASE_URL = 'https://ldoh.105117.xyz';
|
||||
const LDOH_COOKIE_SETTING_KEY = 'monitor_ldoh_cookie';
|
||||
|
||||
function upsertSetting(key: string, value: unknown) {
|
||||
db.insert(schema.settings)
|
||||
.values({ key, value: JSON.stringify(value) })
|
||||
.onConflictDoUpdate({
|
||||
target: schema.settings.key,
|
||||
set: { value: JSON.stringify(value) },
|
||||
})
|
||||
.run();
|
||||
}
|
||||
|
||||
function getSettingString(key: string): string {
|
||||
const row = db.select().from(schema.settings).where(eq(schema.settings.key, key)).get();
|
||||
if (!row?.value) return '';
|
||||
try {
|
||||
const parsed = JSON.parse(row.value);
|
||||
return typeof parsed === 'string' ? parsed : '';
|
||||
} catch {
|
||||
return '';
|
||||
}
|
||||
}
|
||||
|
||||
function parseCookies(raw: string | undefined): Record<string, string> {
|
||||
const result: Record<string, string> = {};
|
||||
if (!raw) return result;
|
||||
for (const part of raw.split(';')) {
|
||||
const entry = part.trim();
|
||||
if (!entry) continue;
|
||||
const idx = entry.indexOf('=');
|
||||
if (idx <= 0) continue;
|
||||
const key = entry.slice(0, idx).trim();
|
||||
const value = entry.slice(idx + 1).trim();
|
||||
if (!key) continue;
|
||||
result[key] = value;
|
||||
}
|
||||
return result;
|
||||
}
|
||||
|
||||
function maskCookieValue(cookieText: string): string {
|
||||
const value = cookieText.trim();
|
||||
if (!value) return '';
|
||||
const idx = value.indexOf('=');
|
||||
const raw = idx >= 0 ? value.slice(idx + 1) : value;
|
||||
if (raw.length <= 10) return `${raw.slice(0, 2)}****`;
|
||||
return `${raw.slice(0, 6)}****${raw.slice(-4)}`;
|
||||
}
|
||||
|
||||
function normalizeLdohCookie(raw: string): string {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return '';
|
||||
if (trimmed.includes('ld_auth_session=')) {
|
||||
const firstPair = trimmed.split(';')[0].trim();
|
||||
if (firstPair.startsWith('ld_auth_session=')) return firstPair;
|
||||
}
|
||||
return `ld_auth_session=${trimmed}`;
|
||||
}
|
||||
|
||||
function rewriteProxyText(text: string): string {
|
||||
return text
|
||||
.replaceAll('https://ldoh.105117.xyz/', '/monitor-proxy/ldoh/')
|
||||
.replaceAll('https:\\/\\/ldoh.105117.xyz\\/', '\\/monitor-proxy\\/ldoh\\/')
|
||||
.replaceAll('src="/', 'src="/monitor-proxy/ldoh/')
|
||||
.replaceAll("src='/", "src='/monitor-proxy/ldoh/")
|
||||
.replaceAll('href="/', 'href="/monitor-proxy/ldoh/')
|
||||
.replaceAll("href='/", "href='/monitor-proxy/ldoh/")
|
||||
.replaceAll('action="/', 'action="/monitor-proxy/ldoh/')
|
||||
.replaceAll("action='/", "action='/monitor-proxy/ldoh/")
|
||||
.replaceAll('"/_next/', '"/monitor-proxy/ldoh/_next/')
|
||||
.replaceAll("'/_next/", "'/monitor-proxy/ldoh/_next/")
|
||||
.replaceAll('"\\/api/', '"\\/monitor-proxy\\/ldoh\\/api/')
|
||||
.replaceAll("'/api/", "'/monitor-proxy/ldoh/api/")
|
||||
.replaceAll('"/api/', '"/monitor-proxy/ldoh/api/');
|
||||
}
|
||||
|
||||
function rewriteLocationHeader(location: string | null): string | null {
|
||||
if (!location) return null;
|
||||
if (location.startsWith(`${LDOH_BASE_URL}/`)) {
|
||||
return `/monitor-proxy/ldoh/${location.slice(LDOH_BASE_URL.length + 1)}`;
|
||||
}
|
||||
if (location.startsWith('/')) {
|
||||
return `/monitor-proxy/ldoh${location}`;
|
||||
}
|
||||
return location;
|
||||
}
|
||||
|
||||
function ensureMonitorAuth(request: FastifyRequest, reply: FastifyReply): boolean {
|
||||
const cookies = parseCookies(request.headers.cookie);
|
||||
if (cookies[MONITOR_AUTH_COOKIE] !== config.authToken) {
|
||||
reply.code(401).send({ error: 'Missing or invalid monitor session' });
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
function resolveLdohProxyPath(request: FastifyRequest): string {
|
||||
const rawUrl = String(request.url || '');
|
||||
const cleanPath = rawUrl.split('?')[0] || '';
|
||||
const prefix = '/monitor-proxy/ldoh';
|
||||
if (cleanPath === prefix || cleanPath === `${prefix}/`) return '';
|
||||
if (cleanPath.startsWith(`${prefix}/`)) return cleanPath.slice(prefix.length + 1);
|
||||
return String((request.params as Record<string, unknown>)['*'] || '');
|
||||
}
|
||||
|
||||
export async function monitorRoutes(app: FastifyInstance) {
|
||||
app.get('/api/monitor/config', async () => {
|
||||
const ldohCookie = getSettingString(LDOH_COOKIE_SETTING_KEY);
|
||||
return {
|
||||
ldohCookieConfigured: !!ldohCookie,
|
||||
ldohCookieMasked: ldohCookie ? maskCookieValue(ldohCookie) : '',
|
||||
};
|
||||
});
|
||||
|
||||
app.put<{ Body: { ldohCookie?: string | null } }>('/api/monitor/config', async (request, reply) => {
|
||||
const raw = String(request.body?.ldohCookie || '').trim();
|
||||
if (!raw) {
|
||||
upsertSetting(LDOH_COOKIE_SETTING_KEY, '');
|
||||
return { success: true, message: 'LDOH Cookie 已清空', ldohCookieConfigured: false };
|
||||
}
|
||||
|
||||
const normalized = normalizeLdohCookie(raw);
|
||||
if (!normalized.startsWith('ld_auth_session=') || normalized.length < 24) {
|
||||
return reply.code(400).send({ success: false, message: 'Cookie 格式无效,请填写 ld_auth_session 或其值' });
|
||||
}
|
||||
|
||||
upsertSetting(LDOH_COOKIE_SETTING_KEY, normalized);
|
||||
return {
|
||||
success: true,
|
||||
message: 'LDOH Cookie 已保存',
|
||||
ldohCookieConfigured: true,
|
||||
ldohCookieMasked: maskCookieValue(normalized),
|
||||
};
|
||||
});
|
||||
|
||||
app.post('/api/monitor/session', async (_, reply) => {
|
||||
// HttpOnly cookie for iframe proxy auth within current origin.
|
||||
reply.header(
|
||||
'Set-Cookie',
|
||||
`${MONITOR_AUTH_COOKIE}=${config.authToken}; Path=/; HttpOnly; SameSite=Lax; Max-Age=7200`,
|
||||
);
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
const handleLdohProxy = async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
if (!ensureMonitorAuth(request, reply)) return;
|
||||
|
||||
const storedCookie = getSettingString(LDOH_COOKIE_SETTING_KEY);
|
||||
if (!storedCookie) {
|
||||
return reply.code(400).send('LDOH cookie not configured');
|
||||
}
|
||||
|
||||
const wildcardPath = resolveLdohProxyPath(request);
|
||||
const targetUrl = new URL(`${LDOH_BASE_URL}/${wildcardPath}`);
|
||||
for (const [key, value] of Object.entries(request.query as Record<string, unknown>)) {
|
||||
if (value == null) continue;
|
||||
targetUrl.searchParams.set(key, String(value));
|
||||
}
|
||||
|
||||
const upstreamHeaders: Record<string, string> = {
|
||||
cookie: storedCookie,
|
||||
accept: String(request.headers.accept || '*/*'),
|
||||
'accept-language': String(request.headers['accept-language'] || 'zh-CN,zh;q=0.9,en;q=0.8'),
|
||||
'user-agent': String(request.headers['user-agent'] || 'metapiMonitorProxy/1.0'),
|
||||
};
|
||||
if (request.headers['content-type']) {
|
||||
upstreamHeaders['content-type'] = String(request.headers['content-type']);
|
||||
}
|
||||
if (request.headers.referer) {
|
||||
upstreamHeaders.referer = String(request.headers.referer).replace('/monitor-proxy/ldoh', '');
|
||||
}
|
||||
|
||||
const method = request.method.toUpperCase();
|
||||
const bodyAllowed = !['GET', 'HEAD'].includes(method);
|
||||
const upstreamResponse = await fetch(targetUrl, {
|
||||
method,
|
||||
headers: upstreamHeaders,
|
||||
body: bodyAllowed ? (request.body as BodyInit | null | undefined) : undefined,
|
||||
redirect: 'manual',
|
||||
});
|
||||
|
||||
const contentType = upstreamResponse.headers.get('content-type') || '';
|
||||
const location = rewriteLocationHeader(upstreamResponse.headers.get('location'));
|
||||
if (location) reply.header('location', location);
|
||||
if (contentType) reply.header('content-type', contentType);
|
||||
const cacheControl = upstreamResponse.headers.get('cache-control');
|
||||
if (cacheControl) reply.header('cache-control', cacheControl);
|
||||
|
||||
reply.code(upstreamResponse.status);
|
||||
|
||||
if (
|
||||
contentType.includes('text/html')
|
||||
|| contentType.includes('application/javascript')
|
||||
|| contentType.includes('text/javascript')
|
||||
|| contentType.includes('text/css')
|
||||
|| contentType.includes('application/json')
|
||||
) {
|
||||
const text = await upstreamResponse.text();
|
||||
return reply.send(rewriteProxyText(text));
|
||||
}
|
||||
|
||||
const buffer = Buffer.from(await upstreamResponse.arrayBuffer());
|
||||
return reply.send(buffer);
|
||||
};
|
||||
|
||||
app.all('/monitor-proxy/ldoh', handleLdohProxy);
|
||||
app.all('/monitor-proxy/ldoh/', handleLdohProxy);
|
||||
app.all('/monitor-proxy/ldoh/*', handleLdohProxy);
|
||||
}
|
||||
@@ -0,0 +1,98 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, like, desc, eq } from 'drizzle-orm';
|
||||
|
||||
export async function searchRoutes(app: FastifyInstance) {
|
||||
app.post<{ Body: { query: string; limit?: number } }>('/api/search', async (request) => {
|
||||
const { query, limit = 20 } = request.body;
|
||||
if (!query || query.trim().length === 0) {
|
||||
return { accounts: [], sites: [], checkinLogs: [], proxyLogs: [], models: [] };
|
||||
}
|
||||
|
||||
const q = `%${query.trim()}%`;
|
||||
const perCategory = Math.min(Math.ceil(limit / 5), 10);
|
||||
|
||||
// Search sites
|
||||
const sites = db.select().from(schema.sites)
|
||||
.where(like(schema.sites.name, q))
|
||||
.limit(perCategory).all()
|
||||
.concat(
|
||||
db.select().from(schema.sites)
|
||||
.where(like(schema.sites.url, q))
|
||||
.limit(perCategory).all()
|
||||
);
|
||||
// Deduplicate by id
|
||||
const uniqueSites = [...new Map(sites.map(s => [s.id, s])).values()].slice(0, perCategory);
|
||||
|
||||
// Search accounts (join with sites for site name)
|
||||
const accountResults = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(like(schema.accounts.username, q))
|
||||
.limit(perCategory).all();
|
||||
const accounts = accountResults.map(r => ({ ...r.accounts, site: r.sites }));
|
||||
|
||||
// Search checkin logs (by message)
|
||||
const checkinLogs = db.select().from(schema.checkinLogs)
|
||||
.innerJoin(schema.accounts, eq(schema.checkinLogs.accountId, schema.accounts.id))
|
||||
.where(like(schema.checkinLogs.message, q))
|
||||
.orderBy(desc(schema.checkinLogs.createdAt))
|
||||
.limit(perCategory).all()
|
||||
.map(r => ({ ...r.checkin_logs, account: r.accounts }));
|
||||
|
||||
// Search proxy logs (by model name)
|
||||
const proxyLogs = db.select().from(schema.proxyLogs)
|
||||
.where(like(schema.proxyLogs.modelRequested, q))
|
||||
.orderBy(desc(schema.proxyLogs.createdAt))
|
||||
.limit(perCategory).all();
|
||||
|
||||
// Search models (only keep routable items)
|
||||
const modelRows = db.select({
|
||||
modelName: schema.tokenModelAvailability.modelName,
|
||||
tokenId: schema.accountTokens.id,
|
||||
accountId: schema.accounts.id,
|
||||
siteId: schema.sites.id,
|
||||
})
|
||||
.from(schema.tokenModelAvailability)
|
||||
.innerJoin(schema.accountTokens, eq(schema.tokenModelAvailability.tokenId, schema.accountTokens.id))
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(
|
||||
and(
|
||||
like(schema.tokenModelAvailability.modelName, q),
|
||||
eq(schema.tokenModelAvailability.available, true),
|
||||
eq(schema.accountTokens.enabled, true),
|
||||
eq(schema.accounts.status, 'active'),
|
||||
),
|
||||
)
|
||||
.limit(perCategory * 20)
|
||||
.all();
|
||||
|
||||
const modelAgg = new Map<string, { tokenIds: Set<number>; accountIds: Set<number>; siteIds: Set<number> }>();
|
||||
for (const row of modelRows) {
|
||||
const key = row.modelName;
|
||||
if (!modelAgg.has(key)) {
|
||||
modelAgg.set(key, { tokenIds: new Set(), accountIds: new Set(), siteIds: new Set() });
|
||||
}
|
||||
const agg = modelAgg.get(key)!;
|
||||
agg.tokenIds.add(row.tokenId);
|
||||
agg.accountIds.add(row.accountId);
|
||||
agg.siteIds.add(row.siteId);
|
||||
}
|
||||
|
||||
const models = Array.from(modelAgg.entries())
|
||||
.map(([name, agg]) => ({
|
||||
name,
|
||||
accountCount: agg.accountIds.size,
|
||||
tokenCount: agg.tokenIds.size,
|
||||
siteCount: agg.siteIds.size,
|
||||
}))
|
||||
.sort((a, b) => {
|
||||
if (b.accountCount !== a.accountCount) return b.accountCount - a.accountCount;
|
||||
if (b.tokenCount !== a.tokenCount) return b.tokenCount - a.tokenCount;
|
||||
return a.name.localeCompare(b.name);
|
||||
})
|
||||
.slice(0, perCategory);
|
||||
|
||||
return { accounts, sites: uniqueSites, checkinLogs, proxyLogs, models };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,137 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { join } from 'node:path';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
type ConfigModule = typeof import('../../config.js');
|
||||
|
||||
describe('settings and auth events', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let config: ConfigModule['config'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-settings-events-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const configModule = await import('../../config.js');
|
||||
const settingsRoutesModule = await import('./settings.js');
|
||||
const authRoutesModule = await import('./auth.js');
|
||||
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
config = configModule.config;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(settingsRoutesModule.settingsRoutes);
|
||||
await app.register(authRoutesModule.authRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.events).run();
|
||||
db.delete(schema.settings).run();
|
||||
|
||||
config.authToken = 'old-admin-token-123';
|
||||
config.proxyToken = 'sk-old-proxy-token-123';
|
||||
config.checkinCron = '0 8 * * *';
|
||||
config.balanceRefreshCron = '0 * * * *';
|
||||
config.routingFallbackUnitCost = 1;
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('appends event when runtime settings are updated', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/settings/runtime',
|
||||
payload: {
|
||||
proxyToken: 'sk-new-proxy-token-456',
|
||||
checkinCron: '5 9 * * *',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const events = db.select().from(schema.events).all();
|
||||
expect(events.length).toBe(1);
|
||||
expect(events[0]).toMatchObject({
|
||||
type: 'status',
|
||||
title: '运行时设置已更新',
|
||||
relatedType: 'settings',
|
||||
});
|
||||
expect(events[0].message || '').toContain('代理访问 Token');
|
||||
expect(events[0].message || '').toContain('签到 Cron');
|
||||
});
|
||||
|
||||
it('rejects proxy token that does not start with sk-', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/settings/runtime',
|
||||
payload: {
|
||||
proxyToken: 'new-proxy-token-456',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(400);
|
||||
const body = response.json() as { message?: string };
|
||||
expect(body.message).toContain('sk-');
|
||||
});
|
||||
|
||||
it('persists and returns routing fallback unit cost from runtime settings', async () => {
|
||||
const updateResponse = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/settings/runtime',
|
||||
payload: {
|
||||
routingFallbackUnitCost: 0.25,
|
||||
},
|
||||
});
|
||||
|
||||
expect(updateResponse.statusCode).toBe(200);
|
||||
const updated = updateResponse.json() as { routingFallbackUnitCost?: number };
|
||||
expect(updated.routingFallbackUnitCost).toBe(0.25);
|
||||
expect(config.routingFallbackUnitCost).toBe(0.25);
|
||||
|
||||
const saved = db.select().from(schema.settings).where(eq(schema.settings.key, 'routing_fallback_unit_cost')).get();
|
||||
expect(saved).toBeTruthy();
|
||||
expect(saved?.value).toBe(JSON.stringify(0.25));
|
||||
|
||||
const getResponse = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/settings/runtime',
|
||||
});
|
||||
expect(getResponse.statusCode).toBe(200);
|
||||
const runtime = getResponse.json() as { routingFallbackUnitCost?: number };
|
||||
expect(runtime.routingFallbackUnitCost).toBe(0.25);
|
||||
});
|
||||
|
||||
it('appends event when admin auth token changes', async () => {
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/settings/auth/change',
|
||||
payload: {
|
||||
oldToken: 'old-admin-token-123',
|
||||
newToken: 'new-admin-token-456',
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
|
||||
const events = db.select().from(schema.events).all();
|
||||
expect(events.length).toBe(1);
|
||||
expect(events[0]).toMatchObject({
|
||||
type: 'token',
|
||||
title: '管理员登录令牌已更新',
|
||||
relatedType: 'settings',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,598 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import cron from 'node-cron';
|
||||
import { config } from '../../config.js';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { updateBalanceRefreshCron, updateCheckinCron } from '../../services/checkinScheduler.js';
|
||||
import { sendNotification } from '../../services/notifyService.js';
|
||||
import { exportBackup, importBackup, type BackupExportType } from '../../services/backupService.js';
|
||||
import { startBackgroundTask } from '../../services/backgroundTaskService.js';
|
||||
|
||||
type RoutingWeights = typeof config.routingWeights;
|
||||
|
||||
interface RuntimeSettingsBody {
|
||||
proxyToken?: string;
|
||||
checkinCron?: string;
|
||||
balanceRefreshCron?: string;
|
||||
webhookUrl?: string;
|
||||
barkUrl?: string;
|
||||
webhookEnabled?: boolean;
|
||||
barkEnabled?: boolean;
|
||||
serverChanEnabled?: boolean;
|
||||
serverChanKey?: string;
|
||||
smtpEnabled?: boolean;
|
||||
smtpHost?: string;
|
||||
smtpPort?: number;
|
||||
smtpSecure?: boolean;
|
||||
smtpUser?: string;
|
||||
smtpPass?: string;
|
||||
smtpFrom?: string;
|
||||
smtpTo?: string;
|
||||
notifyCooldownSec?: number;
|
||||
adminIpAllowlist?: string[] | string;
|
||||
routingFallbackUnitCost?: number;
|
||||
routingWeights?: Partial<RoutingWeights>;
|
||||
}
|
||||
|
||||
const PROXY_TOKEN_PREFIX = 'sk-';
|
||||
|
||||
function isValidProxyToken(value: string): boolean {
|
||||
return value.startsWith(PROXY_TOKEN_PREFIX) && value.length >= 6;
|
||||
}
|
||||
|
||||
function maskSecret(value: string): string {
|
||||
if (!value) return '';
|
||||
if (value.length <= 8) return '****';
|
||||
return `${value.slice(0, 4)}****${value.slice(-4)}`;
|
||||
}
|
||||
|
||||
function upsertSetting(key: string, value: unknown) {
|
||||
db.insert(schema.settings)
|
||||
.values({ key, value: JSON.stringify(value) })
|
||||
.onConflictDoUpdate({
|
||||
target: schema.settings.key,
|
||||
set: { value: JSON.stringify(value) },
|
||||
})
|
||||
.run();
|
||||
}
|
||||
|
||||
function appendSettingsEvent(input: {
|
||||
type: 'checkin' | 'balance' | 'proxy' | 'status' | 'token';
|
||||
title: string;
|
||||
message: string;
|
||||
level?: 'info' | 'warning' | 'error';
|
||||
}) {
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: input.type,
|
||||
title: input.title,
|
||||
message: input.message,
|
||||
level: input.level || 'info',
|
||||
relatedType: 'settings',
|
||||
createdAt: new Date().toISOString(),
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
function toPositiveNumberOrFallback(value: unknown, fallback: number) {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n < 0) return fallback;
|
||||
return n;
|
||||
}
|
||||
|
||||
function toStringList(value: unknown): string[] {
|
||||
if (Array.isArray(value)) {
|
||||
return value
|
||||
.map((item) => (typeof item === 'string' ? item.trim() : ''))
|
||||
.filter((item) => item.length > 0);
|
||||
}
|
||||
if (typeof value === 'string') {
|
||||
return value
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.filter((item) => item.length > 0);
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
function applyImportedSettingToRuntime(key: string, value: unknown) {
|
||||
switch (key) {
|
||||
case 'checkin_cron': {
|
||||
if (typeof value !== 'string' || !value || !cron.validate(value)) return;
|
||||
config.checkinCron = value;
|
||||
updateCheckinCron(value);
|
||||
return;
|
||||
}
|
||||
case 'balance_refresh_cron': {
|
||||
if (typeof value !== 'string' || !value || !cron.validate(value)) return;
|
||||
config.balanceRefreshCron = value;
|
||||
updateBalanceRefreshCron(value);
|
||||
return;
|
||||
}
|
||||
case 'proxy_token': {
|
||||
if (typeof value !== 'string') return;
|
||||
const nextToken = value.trim();
|
||||
if (!isValidProxyToken(nextToken)) return;
|
||||
config.proxyToken = nextToken;
|
||||
return;
|
||||
}
|
||||
case 'webhook_url': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.webhookUrl = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'webhook_enabled': {
|
||||
config.webhookEnabled = !!value;
|
||||
return;
|
||||
}
|
||||
case 'bark_url': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.barkUrl = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'bark_enabled': {
|
||||
config.barkEnabled = !!value;
|
||||
return;
|
||||
}
|
||||
case 'serverchan_enabled': {
|
||||
config.serverChanEnabled = !!value;
|
||||
return;
|
||||
}
|
||||
case 'serverchan_key': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.serverChanKey = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'smtp_enabled': {
|
||||
config.smtpEnabled = !!value;
|
||||
return;
|
||||
}
|
||||
case 'smtp_host': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.smtpHost = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'smtp_port': {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n <= 0) return;
|
||||
config.smtpPort = Math.trunc(n);
|
||||
return;
|
||||
}
|
||||
case 'smtp_secure': {
|
||||
config.smtpSecure = !!value;
|
||||
return;
|
||||
}
|
||||
case 'smtp_user': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.smtpUser = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'smtp_pass': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.smtpPass = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'smtp_from': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.smtpFrom = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'smtp_to': {
|
||||
if (typeof value !== 'string') return;
|
||||
config.smtpTo = value.trim();
|
||||
return;
|
||||
}
|
||||
case 'notify_cooldown_sec': {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n < 0) return;
|
||||
config.notifyCooldownSec = Math.trunc(n);
|
||||
return;
|
||||
}
|
||||
case 'admin_ip_allowlist': {
|
||||
config.adminIpAllowlist = toStringList(value);
|
||||
return;
|
||||
}
|
||||
case 'routing_weights': {
|
||||
if (!value || typeof value !== 'object') return;
|
||||
const rw = value as Partial<RoutingWeights>;
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: toPositiveNumberOrFallback(rw.baseWeightFactor, config.routingWeights.baseWeightFactor),
|
||||
valueScoreFactor: toPositiveNumberOrFallback(rw.valueScoreFactor, config.routingWeights.valueScoreFactor),
|
||||
costWeight: toPositiveNumberOrFallback(rw.costWeight, config.routingWeights.costWeight),
|
||||
balanceWeight: toPositiveNumberOrFallback(rw.balanceWeight, config.routingWeights.balanceWeight),
|
||||
usageWeight: toPositiveNumberOrFallback(rw.usageWeight, config.routingWeights.usageWeight),
|
||||
};
|
||||
return;
|
||||
}
|
||||
case 'routing_fallback_unit_cost': {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n <= 0) return;
|
||||
config.routingFallbackUnitCost = Math.max(1e-6, n);
|
||||
return;
|
||||
}
|
||||
default:
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
function getRuntimeSettingsResponse() {
|
||||
return {
|
||||
checkinCron: config.checkinCron,
|
||||
balanceRefreshCron: config.balanceRefreshCron,
|
||||
routingFallbackUnitCost: config.routingFallbackUnitCost,
|
||||
routingWeights: config.routingWeights,
|
||||
webhookUrl: config.webhookUrl,
|
||||
barkUrl: config.barkUrl,
|
||||
webhookEnabled: config.webhookEnabled,
|
||||
barkEnabled: config.barkEnabled,
|
||||
serverChanEnabled: config.serverChanEnabled,
|
||||
serverChanKeyMasked: maskSecret(config.serverChanKey),
|
||||
smtpEnabled: config.smtpEnabled,
|
||||
smtpHost: config.smtpHost,
|
||||
smtpPort: config.smtpPort,
|
||||
smtpSecure: config.smtpSecure,
|
||||
smtpUser: config.smtpUser,
|
||||
smtpPassMasked: maskSecret(config.smtpPass),
|
||||
smtpFrom: config.smtpFrom,
|
||||
smtpTo: config.smtpTo,
|
||||
notifyCooldownSec: config.notifyCooldownSec,
|
||||
adminIpAllowlist: config.adminIpAllowlist,
|
||||
proxyTokenMasked: maskSecret(config.proxyToken),
|
||||
};
|
||||
}
|
||||
|
||||
export async function settingsRoutes(app: FastifyInstance) {
|
||||
app.get('/api/settings/runtime', async () => {
|
||||
return getRuntimeSettingsResponse();
|
||||
});
|
||||
|
||||
app.put<{ Body: RuntimeSettingsBody }>('/api/settings/runtime', async (request, reply) => {
|
||||
const body = request.body || {};
|
||||
const changedLabels: string[] = [];
|
||||
|
||||
if (body.checkinCron !== undefined) {
|
||||
if (!cron.validate(body.checkinCron)) {
|
||||
return reply.code(400).send({ success: false, message: '签到 Cron 表达式无效' });
|
||||
}
|
||||
if (body.checkinCron !== config.checkinCron) {
|
||||
changedLabels.push(`签到 Cron(${config.checkinCron} -> ${body.checkinCron})`);
|
||||
}
|
||||
updateCheckinCron(body.checkinCron);
|
||||
upsertSetting('checkin_cron', body.checkinCron);
|
||||
}
|
||||
|
||||
if (body.balanceRefreshCron !== undefined) {
|
||||
if (!cron.validate(body.balanceRefreshCron)) {
|
||||
return reply.code(400).send({ success: false, message: '余额刷新 Cron 表达式无效' });
|
||||
}
|
||||
if (body.balanceRefreshCron !== config.balanceRefreshCron) {
|
||||
changedLabels.push(`余额刷新 Cron(${config.balanceRefreshCron} -> ${body.balanceRefreshCron})`);
|
||||
}
|
||||
updateBalanceRefreshCron(body.balanceRefreshCron);
|
||||
upsertSetting('balance_refresh_cron', body.balanceRefreshCron);
|
||||
}
|
||||
|
||||
if (body.proxyToken !== undefined) {
|
||||
const proxyToken = String(body.proxyToken).trim();
|
||||
if (!proxyToken.startsWith(PROXY_TOKEN_PREFIX)) {
|
||||
return reply.code(400).send({ success: false, message: '下游访问令牌必须以 sk- 开头' });
|
||||
}
|
||||
if (proxyToken.length < 6) {
|
||||
return reply.code(400).send({ success: false, message: '下游访问令牌至少 6 位(含 sk-)' });
|
||||
}
|
||||
if (proxyToken !== config.proxyToken) {
|
||||
changedLabels.push('代理访问 Token');
|
||||
}
|
||||
config.proxyToken = proxyToken;
|
||||
upsertSetting('proxy_token', proxyToken);
|
||||
}
|
||||
|
||||
if (body.webhookUrl !== undefined) {
|
||||
if (String(body.webhookUrl || '').trim() !== config.webhookUrl) {
|
||||
changedLabels.push('Webhook 地址');
|
||||
}
|
||||
config.webhookUrl = String(body.webhookUrl || '').trim();
|
||||
upsertSetting('webhook_url', config.webhookUrl);
|
||||
}
|
||||
|
||||
if (body.webhookEnabled !== undefined) {
|
||||
if (!!body.webhookEnabled !== config.webhookEnabled) {
|
||||
changedLabels.push('Webhook 开关');
|
||||
}
|
||||
config.webhookEnabled = !!body.webhookEnabled;
|
||||
upsertSetting('webhook_enabled', config.webhookEnabled);
|
||||
}
|
||||
|
||||
if (body.barkUrl !== undefined) {
|
||||
if (String(body.barkUrl || '').trim() !== config.barkUrl) {
|
||||
changedLabels.push('Bark 地址');
|
||||
}
|
||||
config.barkUrl = String(body.barkUrl || '').trim();
|
||||
upsertSetting('bark_url', config.barkUrl);
|
||||
}
|
||||
|
||||
if (body.barkEnabled !== undefined) {
|
||||
if (!!body.barkEnabled !== config.barkEnabled) {
|
||||
changedLabels.push('Bark 开关');
|
||||
}
|
||||
config.barkEnabled = !!body.barkEnabled;
|
||||
upsertSetting('bark_enabled', config.barkEnabled);
|
||||
}
|
||||
|
||||
if (body.serverChanEnabled !== undefined) {
|
||||
if (!!body.serverChanEnabled !== config.serverChanEnabled) {
|
||||
changedLabels.push('Server 酱开关');
|
||||
}
|
||||
config.serverChanEnabled = !!body.serverChanEnabled;
|
||||
upsertSetting('serverchan_enabled', config.serverChanEnabled);
|
||||
}
|
||||
|
||||
if (body.serverChanKey !== undefined) {
|
||||
if (String(body.serverChanKey || '').trim() !== config.serverChanKey) {
|
||||
changedLabels.push('Server 酱密钥');
|
||||
}
|
||||
config.serverChanKey = String(body.serverChanKey || '').trim();
|
||||
upsertSetting('serverchan_key', config.serverChanKey);
|
||||
}
|
||||
|
||||
if (body.smtpEnabled !== undefined) {
|
||||
if (!!body.smtpEnabled !== config.smtpEnabled) {
|
||||
changedLabels.push('SMTP 开关');
|
||||
}
|
||||
config.smtpEnabled = !!body.smtpEnabled;
|
||||
upsertSetting('smtp_enabled', config.smtpEnabled);
|
||||
}
|
||||
|
||||
if (body.smtpHost !== undefined) {
|
||||
if (String(body.smtpHost || '').trim() !== config.smtpHost) {
|
||||
changedLabels.push('SMTP 主机');
|
||||
}
|
||||
config.smtpHost = String(body.smtpHost || '').trim();
|
||||
upsertSetting('smtp_host', config.smtpHost);
|
||||
}
|
||||
|
||||
if (body.smtpPort !== undefined) {
|
||||
const smtpPort = Number(body.smtpPort);
|
||||
if (!Number.isFinite(smtpPort) || smtpPort <= 0) {
|
||||
return reply.code(400).send({ success: false, message: 'SMTP 端口无效' });
|
||||
}
|
||||
if (Math.trunc(smtpPort) !== config.smtpPort) {
|
||||
changedLabels.push(`SMTP 端口(${config.smtpPort} -> ${Math.trunc(smtpPort)})`);
|
||||
}
|
||||
config.smtpPort = Math.trunc(smtpPort);
|
||||
upsertSetting('smtp_port', config.smtpPort);
|
||||
}
|
||||
|
||||
if (body.smtpSecure !== undefined) {
|
||||
if (!!body.smtpSecure !== config.smtpSecure) {
|
||||
changedLabels.push('SMTP 安全连接');
|
||||
}
|
||||
config.smtpSecure = !!body.smtpSecure;
|
||||
upsertSetting('smtp_secure', config.smtpSecure);
|
||||
}
|
||||
|
||||
if (body.smtpUser !== undefined) {
|
||||
if (String(body.smtpUser || '').trim() !== config.smtpUser) {
|
||||
changedLabels.push('SMTP 用户');
|
||||
}
|
||||
config.smtpUser = String(body.smtpUser || '').trim();
|
||||
upsertSetting('smtp_user', config.smtpUser);
|
||||
}
|
||||
|
||||
if (body.smtpPass !== undefined) {
|
||||
if (String(body.smtpPass || '').trim() !== config.smtpPass) {
|
||||
changedLabels.push('SMTP 密码');
|
||||
}
|
||||
config.smtpPass = String(body.smtpPass || '').trim();
|
||||
upsertSetting('smtp_pass', config.smtpPass);
|
||||
}
|
||||
|
||||
if (body.smtpFrom !== undefined) {
|
||||
if (String(body.smtpFrom || '').trim() !== config.smtpFrom) {
|
||||
changedLabels.push('发件人地址');
|
||||
}
|
||||
config.smtpFrom = String(body.smtpFrom || '').trim();
|
||||
upsertSetting('smtp_from', config.smtpFrom);
|
||||
}
|
||||
|
||||
if (body.smtpTo !== undefined) {
|
||||
if (String(body.smtpTo || '').trim() !== config.smtpTo) {
|
||||
changedLabels.push('收件人地址');
|
||||
}
|
||||
config.smtpTo = String(body.smtpTo || '').trim();
|
||||
upsertSetting('smtp_to', config.smtpTo);
|
||||
}
|
||||
|
||||
if (body.notifyCooldownSec !== undefined) {
|
||||
const notifyCooldownSec = Number(body.notifyCooldownSec);
|
||||
if (!Number.isFinite(notifyCooldownSec) || notifyCooldownSec < 0) {
|
||||
return reply.code(400).send({ success: false, message: '告警冷静期必须是大于等于 0 的数字(秒)' });
|
||||
}
|
||||
const nextCooldown = Math.trunc(notifyCooldownSec);
|
||||
if (nextCooldown !== config.notifyCooldownSec) {
|
||||
changedLabels.push(`告警冷静期(${config.notifyCooldownSec}s -> ${nextCooldown}s)`);
|
||||
}
|
||||
config.notifyCooldownSec = nextCooldown;
|
||||
upsertSetting('notify_cooldown_sec', config.notifyCooldownSec);
|
||||
}
|
||||
|
||||
if (body.adminIpAllowlist !== undefined) {
|
||||
const nextAllowlist = toStringList(body.adminIpAllowlist);
|
||||
if (JSON.stringify(nextAllowlist) !== JSON.stringify(config.adminIpAllowlist)) {
|
||||
changedLabels.push('管理端 IP 白名单');
|
||||
}
|
||||
config.adminIpAllowlist = nextAllowlist;
|
||||
upsertSetting('admin_ip_allowlist', nextAllowlist);
|
||||
}
|
||||
|
||||
if (body.routingWeights !== undefined) {
|
||||
const nextWeights: RoutingWeights = {
|
||||
baseWeightFactor: toPositiveNumberOrFallback(body.routingWeights.baseWeightFactor, config.routingWeights.baseWeightFactor),
|
||||
valueScoreFactor: toPositiveNumberOrFallback(body.routingWeights.valueScoreFactor, config.routingWeights.valueScoreFactor),
|
||||
costWeight: toPositiveNumberOrFallback(body.routingWeights.costWeight, config.routingWeights.costWeight),
|
||||
balanceWeight: toPositiveNumberOrFallback(body.routingWeights.balanceWeight, config.routingWeights.balanceWeight),
|
||||
usageWeight: toPositiveNumberOrFallback(body.routingWeights.usageWeight, config.routingWeights.usageWeight),
|
||||
};
|
||||
if (JSON.stringify(nextWeights) !== JSON.stringify(config.routingWeights)) {
|
||||
changedLabels.push('路由权重');
|
||||
}
|
||||
config.routingWeights = nextWeights;
|
||||
upsertSetting('routing_weights', nextWeights);
|
||||
}
|
||||
|
||||
if (body.routingFallbackUnitCost !== undefined) {
|
||||
const nextRoutingFallbackUnitCost = Number(body.routingFallbackUnitCost);
|
||||
if (!Number.isFinite(nextRoutingFallbackUnitCost) || nextRoutingFallbackUnitCost <= 0) {
|
||||
return reply.code(400).send({ success: false, message: '无价模型默认单价必须是大于 0 的数字' });
|
||||
}
|
||||
const normalized = Math.max(1e-6, nextRoutingFallbackUnitCost);
|
||||
if (Math.abs(normalized - config.routingFallbackUnitCost) > 1e-12) {
|
||||
changedLabels.push(`无价模型默认单价(${config.routingFallbackUnitCost} -> ${normalized})`);
|
||||
}
|
||||
config.routingFallbackUnitCost = normalized;
|
||||
upsertSetting('routing_fallback_unit_cost', normalized);
|
||||
}
|
||||
|
||||
if (changedLabels.length > 0) {
|
||||
let eventType: 'checkin' | 'balance' | 'proxy' | 'status' | 'token' = 'status';
|
||||
if (changedLabels.length === 1) {
|
||||
if (changedLabels[0].startsWith('签到 Cron')) eventType = 'checkin';
|
||||
else if (changedLabels[0].startsWith('余额刷新 Cron')) eventType = 'balance';
|
||||
else if (changedLabels[0] === '代理访问 Token') eventType = 'proxy';
|
||||
}
|
||||
appendSettingsEvent({
|
||||
type: eventType,
|
||||
title: '运行时设置已更新',
|
||||
message: `已更新:${changedLabels.join('、')}`,
|
||||
});
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: '运行时设置已更新',
|
||||
...getRuntimeSettingsResponse(),
|
||||
};
|
||||
});
|
||||
|
||||
app.get<{ Querystring: { type?: string } }>('/api/settings/backup/export', async (request, reply) => {
|
||||
const rawType = String(request.query.type || 'all').trim().toLowerCase();
|
||||
const type: BackupExportType = rawType === 'accounts' || rawType === 'preferences' ? rawType : 'all';
|
||||
if (rawType && !['all', 'accounts', 'preferences'].includes(rawType)) {
|
||||
return reply.code(400).send({ success: false, message: '导出类型无效,仅支持 all/accounts/preferences' });
|
||||
}
|
||||
return exportBackup(type);
|
||||
});
|
||||
|
||||
app.post<{ Body: { data?: Record<string, unknown> } }>('/api/settings/backup/import', async (request, reply) => {
|
||||
const payload = request.body?.data;
|
||||
if (!payload || typeof payload !== 'object' || Array.isArray(payload)) {
|
||||
return reply.code(400).send({ success: false, message: '导入数据格式错误:需要 JSON 对象' });
|
||||
}
|
||||
|
||||
try {
|
||||
const result = importBackup(payload);
|
||||
for (const item of result.appliedSettings) {
|
||||
applyImportedSettingToRuntime(item.key, item.value);
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
message: '导入完成',
|
||||
...result,
|
||||
};
|
||||
} catch (err: any) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
message: err?.message || '导入失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/settings/notify/test', async (_, reply) => {
|
||||
try {
|
||||
const result = await sendNotification(
|
||||
'测试通知',
|
||||
'您好,这是一条来自系统设置的连通性测试通知,您的通知相关配置目前工作正常!',
|
||||
'info',
|
||||
{
|
||||
bypassThrottle: true,
|
||||
requireChannel: true,
|
||||
throwOnFailure: true,
|
||||
},
|
||||
);
|
||||
return {
|
||||
success: true,
|
||||
message: `测试通知已发送(成功 ${result.succeeded}/${result.attempted})`,
|
||||
};
|
||||
} catch (err: any) {
|
||||
return reply.code(400).send({
|
||||
success: false,
|
||||
message: err?.message || '测试通知发送失败',
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
app.post('/api/settings/maintenance/clear-cache', async (_, reply) => {
|
||||
const deletedModelAvailability = db.delete(schema.modelAvailability).run().changes;
|
||||
const deletedRouteChannels = db.delete(schema.routeChannels).run().changes;
|
||||
const deletedTokenRoutes = db.delete(schema.tokenRoutes).run().changes;
|
||||
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'maintenance',
|
||||
title: '清理缓存并重建路由',
|
||||
dedupeKey: 'refresh-models-and-rebuild-routes',
|
||||
notifyOnFailure: true,
|
||||
successMessage: (currentTask) => {
|
||||
const rebuild = (currentTask.result as any)?.rebuild;
|
||||
if (!rebuild) return '缓存清理后重建路由已完成';
|
||||
return `缓存清理后重建完成:新增路由 ${rebuild.createdRoutes},新增通道 ${rebuild.createdChannels},移除通道 ${rebuild.removedChannels}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `缓存清理后重建失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => refreshModelsAndRebuildRoutes(),
|
||||
);
|
||||
|
||||
return reply.code(202).send({
|
||||
success: true,
|
||||
queued: true,
|
||||
reused,
|
||||
jobId: task.id,
|
||||
message: '缓存已清理,重建路由已开始执行',
|
||||
deletedModelAvailability,
|
||||
deletedRouteChannels,
|
||||
deletedTokenRoutes,
|
||||
});
|
||||
});
|
||||
|
||||
app.post('/api/settings/maintenance/clear-usage', async () => {
|
||||
const deletedProxyLogs = db.delete(schema.proxyLogs).run().changes;
|
||||
|
||||
db.update(schema.routeChannels).set({
|
||||
successCount: 0,
|
||||
failCount: 0,
|
||||
totalLatencyMs: 0,
|
||||
totalCost: 0,
|
||||
lastUsedAt: null,
|
||||
lastFailAt: null,
|
||||
cooldownUntil: null,
|
||||
}).run();
|
||||
|
||||
db.update(schema.accounts).set({
|
||||
balanceUsed: 0,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}).run();
|
||||
|
||||
appendSettingsEvent({
|
||||
type: 'status',
|
||||
title: '占用统计与使用日志已清理',
|
||||
message: `已清理使用日志 ${deletedProxyLogs} 条,并重置路由与账号占用统计`,
|
||||
level: 'warning',
|
||||
});
|
||||
|
||||
return {
|
||||
success: true,
|
||||
message: '占用统计已清理',
|
||||
deletedProxyLogs,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,74 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('sites status cascade', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-sites-status-cascade-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./sites.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.sitesRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('disables and re-enables related accounts with site status', async () => {
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'status-site',
|
||||
url: 'https://status-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'status-user',
|
||||
accessToken: 'access-token',
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
const disableResp = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/sites/${site.id}`,
|
||||
payload: { status: 'disabled' },
|
||||
});
|
||||
expect(disableResp.statusCode).toBe(200);
|
||||
|
||||
const disabledAccount = db.select().from(schema.accounts).where(eq(schema.accounts.id, account.id)).get();
|
||||
expect(disabledAccount?.status).toBe('disabled');
|
||||
|
||||
const enableResp = await app.inject({
|
||||
method: 'PUT',
|
||||
url: `/api/sites/${site.id}`,
|
||||
payload: { status: 'active' },
|
||||
});
|
||||
expect(enableResp.statusCode).toBe(200);
|
||||
|
||||
const enabledAccount = db.select().from(schema.accounts).where(eq(schema.accounts.id, account.id)).get();
|
||||
expect(enabledAccount?.status).toBe('active');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,125 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, eq } from 'drizzle-orm';
|
||||
import { detectSite } from '../../services/siteDetector.js';
|
||||
|
||||
function normalizeSiteStatus(input: unknown): 'active' | 'disabled' | null {
|
||||
if (input === undefined || input === null) return null;
|
||||
if (typeof input !== 'string') return null;
|
||||
const status = input.trim().toLowerCase();
|
||||
if (status === 'active' || status === 'disabled') return status;
|
||||
return null;
|
||||
}
|
||||
|
||||
export async function sitesRoutes(app: FastifyInstance) {
|
||||
// List all sites
|
||||
app.get('/api/sites', async () => {
|
||||
return db.select().from(schema.sites).all();
|
||||
});
|
||||
|
||||
// Add a site
|
||||
app.post<{ Body: { name: string; url: string; platform?: string; apiKey?: string; status?: string } }>('/api/sites', async (request, reply) => {
|
||||
const { name, url, platform, apiKey, status } = request.body;
|
||||
const normalizedStatus = normalizeSiteStatus(status);
|
||||
if (status !== undefined && !normalizedStatus) {
|
||||
return reply.code(400).send({ error: 'Invalid site status. Expected active or disabled.' });
|
||||
}
|
||||
|
||||
let detectedPlatform = platform;
|
||||
if (!detectedPlatform) {
|
||||
const detected = await detectSite(url);
|
||||
detectedPlatform = detected?.platform;
|
||||
}
|
||||
if (!detectedPlatform) {
|
||||
return { error: 'Could not detect platform. Please specify manually.' };
|
||||
}
|
||||
const result = db.insert(schema.sites).values({
|
||||
name,
|
||||
url: url.replace(/\/+$/, ''),
|
||||
platform: detectedPlatform,
|
||||
apiKey,
|
||||
status: normalizedStatus ?? 'active',
|
||||
}).returning().get();
|
||||
return result;
|
||||
});
|
||||
|
||||
// Update a site
|
||||
app.put<{ Params: { id: string }; Body: { name?: string; url?: string; platform?: string; apiKey?: string; status?: string } }>('/api/sites/:id', async (request, reply) => {
|
||||
const id = parseInt(request.params.id);
|
||||
if (Number.isNaN(id)) {
|
||||
return reply.code(400).send({ error: 'Invalid site id' });
|
||||
}
|
||||
|
||||
const existingSite = db.select().from(schema.sites).where(eq(schema.sites.id, id)).get();
|
||||
if (!existingSite) {
|
||||
return reply.code(404).send({ error: 'Site not found' });
|
||||
}
|
||||
|
||||
const updates: any = {};
|
||||
const body = request.body;
|
||||
const normalizedStatus = normalizeSiteStatus(body.status);
|
||||
if (body.status !== undefined && !normalizedStatus) {
|
||||
return reply.code(400).send({ error: 'Invalid site status. Expected active or disabled.' });
|
||||
}
|
||||
|
||||
if (body.name !== undefined) updates.name = body.name;
|
||||
if (body.url !== undefined) updates.url = body.url.replace(/\/+$/, '');
|
||||
if (body.platform !== undefined) updates.platform = body.platform;
|
||||
if (body.apiKey !== undefined) updates.apiKey = body.apiKey;
|
||||
if (body.status !== undefined) updates.status = normalizedStatus;
|
||||
updates.updatedAt = new Date().toISOString();
|
||||
db.update(schema.sites).set(updates).where(eq(schema.sites.id, id)).run();
|
||||
|
||||
if (body.status !== undefined && normalizedStatus) {
|
||||
const now = new Date().toISOString();
|
||||
if (normalizedStatus === 'disabled') {
|
||||
db.update(schema.accounts)
|
||||
.set({ status: 'disabled', updatedAt: now })
|
||||
.where(eq(schema.accounts.siteId, id))
|
||||
.run();
|
||||
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: 'status',
|
||||
title: '站点已禁用',
|
||||
message: `${existingSite.name} 已禁用,关联账号已全部置为禁用`,
|
||||
level: 'warning',
|
||||
relatedId: id,
|
||||
relatedType: 'site',
|
||||
}).run();
|
||||
} catch {}
|
||||
} else {
|
||||
db.update(schema.accounts)
|
||||
.set({ status: 'active', updatedAt: now })
|
||||
.where(and(eq(schema.accounts.siteId, id), eq(schema.accounts.status, 'disabled')))
|
||||
.run();
|
||||
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: 'status',
|
||||
title: '站点已启用',
|
||||
message: `${existingSite.name} 已启用,关联禁用账号已恢复为活跃`,
|
||||
level: 'info',
|
||||
relatedId: id,
|
||||
relatedType: 'site',
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
|
||||
return db.select().from(schema.sites).where(eq(schema.sites.id, id)).get();
|
||||
});
|
||||
|
||||
// Delete a site
|
||||
app.delete<{ Params: { id: string } }>('/api/sites/:id', async (request) => {
|
||||
const id = parseInt(request.params.id);
|
||||
db.delete(schema.sites).where(eq(schema.sites.id, id)).run();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// Detect platform for a URL
|
||||
app.post<{ Body: { url: string } }>('/api/sites/detect', async (request) => {
|
||||
const result = await detectSite(request.body.url);
|
||||
return result || { error: 'Could not detect platform' };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,156 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { sql } from 'drizzle-orm';
|
||||
import { formatLocalDate } from '../../services/localTimeService.js';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('stats dashboard filters disabled sites', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-stats-site-status-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./stats.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.statsRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.proxyLogs).run();
|
||||
db.delete(schema.checkinLogs).run();
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('excludes disabled-site balances from dashboard totals', async () => {
|
||||
const activeSite = db.insert(schema.sites).values({
|
||||
name: 'active-site',
|
||||
url: 'https://active-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
|
||||
const disabledSite = db.insert(schema.sites).values({
|
||||
name: 'disabled-site',
|
||||
url: 'https://disabled-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
|
||||
db.run(sql`update sites set status = 'disabled' where id = ${disabledSite.id}`);
|
||||
|
||||
db.insert(schema.accounts).values({
|
||||
siteId: activeSite.id,
|
||||
username: 'active-user',
|
||||
accessToken: 'active-token',
|
||||
balance: 100,
|
||||
status: 'active',
|
||||
}).run();
|
||||
|
||||
db.insert(schema.accounts).values({
|
||||
siteId: disabledSite.id,
|
||||
username: 'disabled-user',
|
||||
accessToken: 'disabled-token',
|
||||
balance: 900,
|
||||
status: 'active',
|
||||
}).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/stats/dashboard',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as {
|
||||
totalBalance: number;
|
||||
activeAccounts: number;
|
||||
totalAccounts: number;
|
||||
};
|
||||
|
||||
expect(body.totalBalance).toBe(100);
|
||||
expect(body.activeAccounts).toBe(1);
|
||||
expect(body.totalAccounts).toBe(1);
|
||||
});
|
||||
|
||||
it('treats skipped checkins as successful in dashboard stats', async () => {
|
||||
const today = formatLocalDate(new Date());
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'checkin-site',
|
||||
url: 'https://checkin-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'checkin-user',
|
||||
accessToken: 'token',
|
||||
balance: 10,
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
db.insert(schema.checkinLogs).values([
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
message: 'checked in',
|
||||
reward: '1',
|
||||
createdAt: `${today} 09:00:00`,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'skipped',
|
||||
message: 'today already checked in',
|
||||
reward: '',
|
||||
createdAt: `${today} 09:10:00`,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'failed',
|
||||
message: 'checkin failed',
|
||||
reward: '',
|
||||
createdAt: `${today} 09:20:00`,
|
||||
},
|
||||
]).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/stats/dashboard',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as {
|
||||
todayCheckin: {
|
||||
success: number;
|
||||
failed: number;
|
||||
total: number;
|
||||
};
|
||||
};
|
||||
|
||||
expect(body.todayCheckin).toEqual({
|
||||
success: 2,
|
||||
failed: 1,
|
||||
total: 3,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import {
|
||||
formatLocalDate,
|
||||
formatUtcSqlDateTime,
|
||||
getLocalDayRangeUtc,
|
||||
parseStoredUtcDateTime,
|
||||
} from '../../services/localTimeService.js';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('stats dashboard today reward fallback', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-stats-reward-fallback-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./stats.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.statsRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.proxyLogs).run();
|
||||
db.delete(schema.checkinLogs).run();
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('uses today income value for dashboard todayReward when reward text is empty', async () => {
|
||||
const today = formatLocalDate(new Date());
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'stats-site',
|
||||
url: 'https://stats-site.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'stats-user',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
todayIncomeSnapshot: {
|
||||
day: today,
|
||||
baseline: 4.8,
|
||||
latest: 4.8,
|
||||
updatedAt: `${today}T10:00:00.000Z`,
|
||||
},
|
||||
}),
|
||||
}).returning().get();
|
||||
|
||||
db.insert(schema.checkinLogs).values({
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
message: 'checked in',
|
||||
reward: '',
|
||||
createdAt: `${today} 10:01:00`,
|
||||
}).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/stats/dashboard',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as { todayReward: number };
|
||||
expect(body.todayReward).toBe(4.8);
|
||||
});
|
||||
|
||||
it('counts dashboard today spend only inside local-day range', async () => {
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'stats-spend-site',
|
||||
url: 'https://stats-spend.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'stats-spend-user',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
const { startUtc, endUtc } = getLocalDayRangeUtc();
|
||||
const startDate = parseStoredUtcDateTime(startUtc)!;
|
||||
const endDate = parseStoredUtcDateTime(endUtc)!;
|
||||
const beforeStart = formatUtcSqlDateTime(new Date(startDate.getTime() - 60_000));
|
||||
const inRange = formatUtcSqlDateTime(new Date(startDate.getTime() + 60_000));
|
||||
const afterEnd = formatUtcSqlDateTime(new Date(endDate.getTime() + 60_000));
|
||||
|
||||
db.insert(schema.proxyLogs).values([
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 1,
|
||||
createdAt: beforeStart,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 3,
|
||||
createdAt: inRange,
|
||||
},
|
||||
{
|
||||
accountId: account.id,
|
||||
status: 'success',
|
||||
estimatedCost: 5,
|
||||
createdAt: afterEnd,
|
||||
},
|
||||
]).run();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'GET',
|
||||
url: '/api/stats/dashboard',
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as { todaySpend: number };
|
||||
expect(body.todaySpend).toBe(3);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,637 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, desc, gte, eq, lt } from 'drizzle-orm';
|
||||
import {
|
||||
refreshModelsForAccount,
|
||||
refreshModelsAndRebuildRoutes,
|
||||
rebuildTokenRoutesFromAvailability,
|
||||
} from '../../services/modelService.js';
|
||||
import { buildModelAnalysis } from '../../services/modelAnalysisService.js';
|
||||
import { fallbackTokenCost, fetchModelPricingCatalog } from '../../services/modelPricingService.js';
|
||||
import { getUpstreamModelDescriptionsCached } from '../../services/upstreamModelDescriptionService.js';
|
||||
import { getRunningTaskByDedupeKey, startBackgroundTask } from '../../services/backgroundTaskService.js';
|
||||
import { parseCheckinRewardAmount } from '../../services/checkinRewardParser.js';
|
||||
import { estimateRewardWithTodayIncomeFallback } from '../../services/todayIncomeRewardService.js';
|
||||
import {
|
||||
getLocalDayRangeUtc,
|
||||
getLocalRangeStartUtc,
|
||||
parseStoredUtcDateTime,
|
||||
toLocalDayKeyFromStoredUtc,
|
||||
} from '../../services/localTimeService.js';
|
||||
|
||||
function parseBooleanFlag(raw?: string): boolean {
|
||||
if (!raw) return false;
|
||||
const normalized = raw.trim().toLowerCase();
|
||||
return normalized === '1' || normalized === 'true' || normalized === 'yes';
|
||||
}
|
||||
|
||||
const MODELS_MARKETPLACE_BASE_TTL_MS = 15_000;
|
||||
const MODELS_MARKETPLACE_PRICING_TTL_MS = 90_000;
|
||||
|
||||
type ModelsMarketplaceCacheEntry = {
|
||||
expiresAt: number;
|
||||
models: any[];
|
||||
};
|
||||
|
||||
const modelsMarketplaceCache = new Map<'base' | 'pricing', ModelsMarketplaceCacheEntry>();
|
||||
|
||||
function readModelsMarketplaceCache(includePricing: boolean): any[] | null {
|
||||
const key = includePricing ? 'pricing' : 'base';
|
||||
const cached = modelsMarketplaceCache.get(key);
|
||||
if (!cached) return null;
|
||||
if (Date.now() >= cached.expiresAt) {
|
||||
modelsMarketplaceCache.delete(key);
|
||||
return null;
|
||||
}
|
||||
return cached.models;
|
||||
}
|
||||
|
||||
function writeModelsMarketplaceCache(includePricing: boolean, models: any[]): void {
|
||||
const ttl = includePricing ? MODELS_MARKETPLACE_PRICING_TTL_MS : MODELS_MARKETPLACE_BASE_TTL_MS;
|
||||
const key = includePricing ? 'pricing' : 'base';
|
||||
modelsMarketplaceCache.set(key, {
|
||||
expiresAt: Date.now() + ttl,
|
||||
models,
|
||||
});
|
||||
}
|
||||
|
||||
export async function statsRoutes(app: FastifyInstance) {
|
||||
// Dashboard summary
|
||||
app.get('/api/stats/dashboard', async () => {
|
||||
const accountRows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.sites.status, 'active'))
|
||||
.all();
|
||||
const accounts = accountRows.map((row) => row.accounts);
|
||||
const totalBalance = accounts.reduce((sum, a) => sum + (a.balance || 0), 0);
|
||||
const activeCount = accounts.filter((a) => a.status === 'active').length;
|
||||
|
||||
const { localDay: today, startUtc: todayStartUtc, endUtc: todayEndUtc } = getLocalDayRangeUtc();
|
||||
const todayCheckinRows = db.select().from(schema.checkinLogs)
|
||||
.innerJoin(schema.accounts, eq(schema.checkinLogs.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(
|
||||
gte(schema.checkinLogs.createdAt, todayStartUtc),
|
||||
lt(schema.checkinLogs.createdAt, todayEndUtc),
|
||||
eq(schema.sites.status, 'active'),
|
||||
))
|
||||
.all();
|
||||
const todayCheckins = todayCheckinRows.map((row) => row.checkin_logs);
|
||||
const checkinFailed = todayCheckins.filter((c) => c.status === 'failed').length;
|
||||
const checkinSuccess = todayCheckins.length - checkinFailed;
|
||||
const rewardByAccount: Record<number, number> = {};
|
||||
const successCountByAccount: Record<number, number> = {};
|
||||
const parsedRewardCountByAccount: Record<number, number> = {};
|
||||
for (const row of todayCheckinRows) {
|
||||
const checkin = row.checkin_logs;
|
||||
if (checkin.status !== 'success') continue;
|
||||
const accountId = row.accounts.id;
|
||||
successCountByAccount[accountId] = (successCountByAccount[accountId] || 0) + 1;
|
||||
const rewardValue = parseCheckinRewardAmount(checkin.reward) || parseCheckinRewardAmount(checkin.message);
|
||||
if (rewardValue <= 0) continue;
|
||||
rewardByAccount[accountId] = (rewardByAccount[accountId] || 0) + rewardValue;
|
||||
parsedRewardCountByAccount[accountId] = (parsedRewardCountByAccount[accountId] || 0) + 1;
|
||||
}
|
||||
|
||||
const nowTs = Date.now();
|
||||
const last24hTs = nowTs - 86400000;
|
||||
const last7dDate = getLocalRangeStartUtc(7);
|
||||
const recentProxyLogs = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(gte(schema.proxyLogs.createdAt, last7dDate), eq(schema.sites.status, 'active')))
|
||||
.all()
|
||||
.map((row) => row.proxy_logs);
|
||||
const allProxyLogs = db.select()
|
||||
.from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.sites.status, 'active'))
|
||||
.all();
|
||||
|
||||
const proxy24hLogs = recentProxyLogs.filter((log) => {
|
||||
if (!log.createdAt) return false;
|
||||
const ts = parseStoredUtcDateTime(log.createdAt)?.getTime() ?? Number.NaN;
|
||||
return Number.isFinite(ts) && ts >= last24hTs;
|
||||
});
|
||||
const proxySuccess = proxy24hLogs.filter((l) => l.status === 'success').length;
|
||||
const proxyFailed = proxy24hLogs.filter((l) => l.status === 'failed').length;
|
||||
const totalTokens = proxy24hLogs.reduce((sum, l) => sum + (l.totalTokens || 0), 0);
|
||||
const totalUsed = allProxyLogs.reduce((sum, row) => {
|
||||
const log = row.proxy_logs;
|
||||
const platform = row.sites?.platform || 'new-api';
|
||||
const explicitCost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
if (explicitCost > 0) return sum + explicitCost;
|
||||
return sum + fallbackTokenCost(log.totalTokens || 0, platform);
|
||||
}, 0);
|
||||
const todayProxyLogs = recentProxyLogs.filter((log) => {
|
||||
if (!log.createdAt) return false;
|
||||
return log.createdAt >= todayStartUtc && log.createdAt < todayEndUtc;
|
||||
});
|
||||
const todaySpend = todayProxyLogs.reduce((sum, log) => {
|
||||
const cost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
return sum + cost;
|
||||
}, 0);
|
||||
const todayReward = accounts.reduce((sum, account) => sum + estimateRewardWithTodayIncomeFallback({
|
||||
day: today,
|
||||
successCount: successCountByAccount[account.id] || 0,
|
||||
parsedRewardCount: parsedRewardCountByAccount[account.id] || 0,
|
||||
rewardSum: rewardByAccount[account.id] || 0,
|
||||
extraConfig: account.extraConfig,
|
||||
}), 0);
|
||||
const modelAnalysis = buildModelAnalysis(recentProxyLogs, { days: 7 });
|
||||
|
||||
return {
|
||||
totalBalance,
|
||||
totalUsed: Math.round(totalUsed * 1_000_000) / 1_000_000,
|
||||
todaySpend: Math.round(todaySpend * 1_000_000) / 1_000_000,
|
||||
todayReward: Math.round(todayReward * 1_000_000) / 1_000_000,
|
||||
activeAccounts: activeCount,
|
||||
totalAccounts: accounts.length,
|
||||
todayCheckin: { success: checkinSuccess, failed: checkinFailed, total: todayCheckins.length },
|
||||
proxy24h: { success: proxySuccess, failed: proxyFailed, total: proxy24hLogs.length, totalTokens },
|
||||
modelAnalysis,
|
||||
};
|
||||
});
|
||||
|
||||
// Proxy logs
|
||||
app.get<{ Querystring: { limit?: string; offset?: string } }>('/api/stats/proxy-logs', async (request) => {
|
||||
const limit = parseInt(request.query.limit || '50', 10);
|
||||
const offset = parseInt(request.query.offset || '0', 10);
|
||||
const rows = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.orderBy(desc(schema.proxyLogs.createdAt))
|
||||
.limit(limit).offset(offset).all();
|
||||
|
||||
return rows.map((row) => ({
|
||||
...row.proxy_logs,
|
||||
username: row.accounts?.username || null,
|
||||
siteName: row.sites?.name || null,
|
||||
siteUrl: row.sites?.url || null,
|
||||
}));
|
||||
});
|
||||
|
||||
// Models marketplace - refresh upstream models and aggregate.
|
||||
app.get<{ Querystring: { refresh?: string; includePricing?: string } }>('/api/models/marketplace', async (request) => {
|
||||
const refreshRequested = parseBooleanFlag(request.query.refresh);
|
||||
const includePricing = parseBooleanFlag(request.query.includePricing);
|
||||
|
||||
let refreshQueued = false;
|
||||
let refreshReused = false;
|
||||
let refreshJobId: string | null = null;
|
||||
|
||||
if (refreshRequested) {
|
||||
modelsMarketplaceCache.clear();
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'model',
|
||||
title: '刷新模型广场数据',
|
||||
dedupeKey: 'refresh-models-and-rebuild-routes',
|
||||
notifyOnFailure: true,
|
||||
successMessage: (currentTask) => {
|
||||
const rebuild = (currentTask.result as any)?.rebuild;
|
||||
if (!rebuild) return '模型广场刷新已完成';
|
||||
return `模型广场刷新完成:新增路由 ${rebuild.createdRoutes},新增通道 ${rebuild.createdChannels},移除通道 ${rebuild.removedChannels}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `模型广场刷新失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => refreshModelsAndRebuildRoutes(),
|
||||
);
|
||||
refreshQueued = !reused;
|
||||
refreshReused = reused;
|
||||
refreshJobId = task.id;
|
||||
}
|
||||
const runningRefreshTask = getRunningTaskByDedupeKey('refresh-models-and-rebuild-routes');
|
||||
if (!refreshJobId && runningRefreshTask) refreshJobId = runningRefreshTask.id;
|
||||
|
||||
if (!refreshRequested) {
|
||||
const cachedModels = readModelsMarketplaceCache(includePricing);
|
||||
if (cachedModels) {
|
||||
return {
|
||||
models: cachedModels,
|
||||
meta: {
|
||||
refreshRequested,
|
||||
refreshQueued,
|
||||
refreshReused,
|
||||
refreshRunning: !!runningRefreshTask,
|
||||
refreshJobId,
|
||||
includePricing,
|
||||
cacheHit: true,
|
||||
},
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
const availability = db.select().from(schema.tokenModelAvailability)
|
||||
.innerJoin(schema.accountTokens, eq(schema.tokenModelAvailability.tokenId, schema.accountTokens.id))
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.all();
|
||||
|
||||
const last7d = getLocalRangeStartUtc(7);
|
||||
const recentLogs = db.select().from(schema.proxyLogs)
|
||||
.where(gte(schema.proxyLogs.createdAt, last7d))
|
||||
.all();
|
||||
|
||||
const modelLogStats: Record<string, { success: number; total: number; totalLatency: number }> = {};
|
||||
for (const log of recentLogs) {
|
||||
const model = log.modelActual || log.modelRequested || '';
|
||||
if (!modelLogStats[model]) modelLogStats[model] = { success: 0, total: 0, totalLatency: 0 };
|
||||
modelLogStats[model].total++;
|
||||
if (log.status === 'success') modelLogStats[model].success++;
|
||||
modelLogStats[model].totalLatency += log.latencyMs || 0;
|
||||
}
|
||||
|
||||
type ModelMetadataAggregate = {
|
||||
description: string | null;
|
||||
tags: Set<string>;
|
||||
supportedEndpointTypes: Set<string>;
|
||||
pricingSources: Array<{
|
||||
siteId: number;
|
||||
siteName: string;
|
||||
accountId: number;
|
||||
username: string | null;
|
||||
ownerBy: string | null;
|
||||
enableGroups: string[];
|
||||
groupPricing: Record<string, {
|
||||
quotaType: number;
|
||||
inputPerMillion?: number;
|
||||
outputPerMillion?: number;
|
||||
perCallInput?: number;
|
||||
perCallOutput?: number;
|
||||
perCallTotal?: number;
|
||||
}>;
|
||||
}>;
|
||||
};
|
||||
|
||||
const modelMetadataMap = new Map<string, ModelMetadataAggregate>();
|
||||
if (includePricing) {
|
||||
const activeAccountRows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(eq(schema.accounts.status, 'active'), eq(schema.sites.status, 'active')))
|
||||
.all();
|
||||
|
||||
const metadataResults = await Promise.all(activeAccountRows.map(async (row) => {
|
||||
const catalog = await fetchModelPricingCatalog({
|
||||
site: {
|
||||
id: row.sites.id,
|
||||
url: row.sites.url,
|
||||
platform: row.sites.platform,
|
||||
apiKey: row.sites.apiKey,
|
||||
},
|
||||
account: {
|
||||
id: row.accounts.id,
|
||||
accessToken: row.accounts.accessToken,
|
||||
apiToken: row.accounts.apiToken,
|
||||
},
|
||||
modelName: '__metadata__',
|
||||
totalTokens: 0,
|
||||
});
|
||||
|
||||
return {
|
||||
account: row.accounts,
|
||||
site: row.sites,
|
||||
catalog,
|
||||
};
|
||||
}));
|
||||
|
||||
for (const result of metadataResults) {
|
||||
if (!result.catalog) continue;
|
||||
|
||||
for (const model of result.catalog.models) {
|
||||
const key = model.modelName.toLowerCase();
|
||||
if (!modelMetadataMap.has(key)) {
|
||||
modelMetadataMap.set(key, {
|
||||
description: null,
|
||||
tags: new Set<string>(),
|
||||
supportedEndpointTypes: new Set<string>(),
|
||||
pricingSources: [],
|
||||
});
|
||||
}
|
||||
|
||||
const aggregate = modelMetadataMap.get(key)!;
|
||||
if (!aggregate.description && model.modelDescription) {
|
||||
aggregate.description = model.modelDescription;
|
||||
}
|
||||
|
||||
for (const tag of model.tags) aggregate.tags.add(tag);
|
||||
for (const endpointType of model.supportedEndpointTypes) {
|
||||
aggregate.supportedEndpointTypes.add(endpointType);
|
||||
}
|
||||
|
||||
aggregate.pricingSources.push({
|
||||
siteId: result.site.id,
|
||||
siteName: result.site.name,
|
||||
accountId: result.account.id,
|
||||
username: result.account.username,
|
||||
ownerBy: model.ownerBy,
|
||||
enableGroups: model.enableGroups,
|
||||
groupPricing: model.groupPricing,
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const modelMap: Record<string, {
|
||||
name: string;
|
||||
accountsById: Map<number, {
|
||||
id: number;
|
||||
site: string;
|
||||
username: string | null;
|
||||
latency: number | null;
|
||||
unitCost: number | null;
|
||||
balance: number;
|
||||
tokens: Array<{ id: number; name: string; isDefault: boolean }>;
|
||||
}>;
|
||||
}> = {};
|
||||
|
||||
for (const row of availability) {
|
||||
const m = row.token_model_availability;
|
||||
const t = row.account_tokens;
|
||||
const a = row.accounts;
|
||||
const s = row.sites;
|
||||
if (!m.available || !t.enabled || a.status !== 'active' || s.status !== 'active') continue;
|
||||
|
||||
if (!modelMap[m.modelName]) {
|
||||
modelMap[m.modelName] = { name: m.modelName, accountsById: new Map() };
|
||||
}
|
||||
|
||||
const existingAccount = modelMap[m.modelName].accountsById.get(a.id);
|
||||
if (!existingAccount) {
|
||||
modelMap[m.modelName].accountsById.set(a.id, {
|
||||
id: a.id,
|
||||
site: s.name,
|
||||
username: a.username,
|
||||
latency: m.latencyMs,
|
||||
unitCost: a.unitCost,
|
||||
balance: a.balance || 0,
|
||||
tokens: [{ id: t.id, name: t.name, isDefault: !!t.isDefault }],
|
||||
});
|
||||
} else {
|
||||
const nextLatency = (() => {
|
||||
if (existingAccount.latency == null) return m.latencyMs;
|
||||
if (m.latencyMs == null) return existingAccount.latency;
|
||||
return Math.min(existingAccount.latency, m.latencyMs);
|
||||
})();
|
||||
existingAccount.latency = nextLatency;
|
||||
if (!existingAccount.tokens.some((token) => token.id === t.id)) {
|
||||
existingAccount.tokens.push({ id: t.id, name: t.name, isDefault: !!t.isDefault });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
let upstreamDescriptionMap = new Map<string, string>();
|
||||
if (includePricing) {
|
||||
const hasMissingDescription = Object.keys(modelMap).some((modelName) => {
|
||||
const metadata = modelMetadataMap.get(modelName.toLowerCase());
|
||||
return !metadata?.description;
|
||||
});
|
||||
if (hasMissingDescription) {
|
||||
upstreamDescriptionMap = await getUpstreamModelDescriptionsCached();
|
||||
}
|
||||
}
|
||||
|
||||
const models = Object.values(modelMap).map((m) => {
|
||||
const logStats = modelLogStats[m.name];
|
||||
const accounts = Array.from(m.accountsById.values());
|
||||
const avgLatency = accounts.reduce((sum, a) => sum + (a.latency || 0), 0) / (accounts.length || 1);
|
||||
const metadata = modelMetadataMap.get(m.name.toLowerCase());
|
||||
const fallbackDescription = metadata?.description ? null : upstreamDescriptionMap.get(m.name.toLowerCase()) || null;
|
||||
return {
|
||||
name: m.name,
|
||||
accountCount: accounts.length,
|
||||
tokenCount: accounts.reduce((sum, account) => sum + account.tokens.length, 0),
|
||||
avgLatency: Math.round(avgLatency),
|
||||
successRate: logStats ? Math.round((logStats.success / logStats.total) * 1000) / 10 : null,
|
||||
description: metadata?.description || fallbackDescription,
|
||||
tags: metadata ? Array.from(metadata.tags).sort((a, b) => a.localeCompare(b)) : [],
|
||||
supportedEndpointTypes: metadata ? Array.from(metadata.supportedEndpointTypes).sort((a, b) => a.localeCompare(b)) : [],
|
||||
pricingSources: metadata?.pricingSources || [],
|
||||
accounts,
|
||||
};
|
||||
});
|
||||
|
||||
models.sort((a, b) => b.accountCount - a.accountCount);
|
||||
writeModelsMarketplaceCache(includePricing, models);
|
||||
return {
|
||||
models,
|
||||
meta: {
|
||||
refreshRequested,
|
||||
refreshQueued,
|
||||
refreshReused,
|
||||
refreshRunning: !!runningRefreshTask,
|
||||
refreshJobId,
|
||||
includePricing,
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
app.get('/api/models/token-candidates', async () => {
|
||||
const rows = db.select().from(schema.tokenModelAvailability)
|
||||
.innerJoin(schema.accountTokens, eq(schema.tokenModelAvailability.tokenId, schema.accountTokens.id))
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(
|
||||
and(
|
||||
eq(schema.tokenModelAvailability.available, true),
|
||||
eq(schema.accountTokens.enabled, true),
|
||||
eq(schema.accounts.status, 'active'),
|
||||
eq(schema.sites.status, 'active'),
|
||||
),
|
||||
)
|
||||
.all();
|
||||
|
||||
const result: Record<string, Array<{
|
||||
accountId: number;
|
||||
tokenId: number;
|
||||
tokenName: string;
|
||||
isDefault: boolean;
|
||||
username: string | null;
|
||||
siteId: number;
|
||||
siteName: string;
|
||||
}>> = {};
|
||||
|
||||
for (const row of rows) {
|
||||
const modelName = row.token_model_availability.modelName;
|
||||
if (!result[modelName]) result[modelName] = [];
|
||||
if (result[modelName].some((item) => item.tokenId === row.account_tokens.id)) continue;
|
||||
result[modelName].push({
|
||||
accountId: row.accounts.id,
|
||||
tokenId: row.account_tokens.id,
|
||||
tokenName: row.account_tokens.name,
|
||||
isDefault: !!row.account_tokens.isDefault,
|
||||
username: row.accounts.username,
|
||||
siteId: row.sites.id,
|
||||
siteName: row.sites.name,
|
||||
});
|
||||
}
|
||||
|
||||
return { models: result };
|
||||
});
|
||||
|
||||
// Refresh models for one account and rebuild routes.
|
||||
app.post<{ Params: { accountId: string } }>('/api/models/check/:accountId', async (request) => {
|
||||
const accountId = Number.parseInt(request.params.accountId, 10);
|
||||
if (Number.isNaN(accountId)) {
|
||||
return { success: false, error: 'Invalid account id' };
|
||||
}
|
||||
|
||||
const refresh = await refreshModelsForAccount(accountId);
|
||||
const rebuild = rebuildTokenRoutesFromAvailability();
|
||||
return { success: true, refresh, rebuild };
|
||||
});
|
||||
|
||||
// Site distribution – per-site aggregate data
|
||||
app.get('/api/stats/site-distribution', async () => {
|
||||
const accounts = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.sites.status, 'active'))
|
||||
.all();
|
||||
|
||||
const proxyLogs = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.sites.status, 'active'))
|
||||
.all();
|
||||
|
||||
// Build spend per site from proxy logs
|
||||
const spendBySiteId: Record<number, number> = {};
|
||||
for (const row of proxyLogs) {
|
||||
const siteId = row.sites?.id;
|
||||
if (siteId == null) continue;
|
||||
const log = row.proxy_logs;
|
||||
const platform = row.sites?.platform || 'new-api';
|
||||
const explicitCost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
const cost = explicitCost > 0 ? explicitCost : fallbackTokenCost(log.totalTokens || 0, platform);
|
||||
spendBySiteId[siteId] = (spendBySiteId[siteId] || 0) + cost;
|
||||
}
|
||||
|
||||
// Aggregate accounts by site
|
||||
const siteMap: Record<number, {
|
||||
siteName: string;
|
||||
platform: string;
|
||||
totalBalance: number;
|
||||
accountCount: number;
|
||||
}> = {};
|
||||
|
||||
for (const row of accounts) {
|
||||
const site = row.sites;
|
||||
const acct = row.accounts;
|
||||
if (!siteMap[site.id]) {
|
||||
siteMap[site.id] = { siteName: site.name, platform: site.platform, totalBalance: 0, accountCount: 0 };
|
||||
}
|
||||
siteMap[site.id].totalBalance += acct.balance || 0;
|
||||
siteMap[site.id].accountCount++;
|
||||
}
|
||||
|
||||
const distribution = Object.entries(siteMap).map(([id, info]) => ({
|
||||
siteId: Number(id),
|
||||
siteName: info.siteName,
|
||||
platform: info.platform,
|
||||
totalBalance: Math.round(info.totalBalance * 1_000_000) / 1_000_000,
|
||||
totalSpend: Math.round((spendBySiteId[Number(id)] || 0) * 1_000_000) / 1_000_000,
|
||||
accountCount: info.accountCount,
|
||||
}));
|
||||
|
||||
return { distribution };
|
||||
});
|
||||
|
||||
// Site trend – daily spend/calls broken down by site
|
||||
app.get<{ Querystring: { days?: string } }>('/api/stats/site-trend', async (request) => {
|
||||
const days = Math.max(1, parseInt(request.query.days || '7', 10));
|
||||
const sinceDate = getLocalRangeStartUtc(days);
|
||||
|
||||
const rows = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(gte(schema.proxyLogs.createdAt, sinceDate), eq(schema.sites.status, 'active')))
|
||||
.all();
|
||||
|
||||
// Group by date + site name
|
||||
const dayMap: Record<string, Record<string, { spend: number; calls: number }>> = {};
|
||||
|
||||
for (const row of rows) {
|
||||
const log = row.proxy_logs;
|
||||
const siteName = row.sites?.name || 'unknown';
|
||||
const platform = row.sites?.platform || 'new-api';
|
||||
const date = toLocalDayKeyFromStoredUtc(log.createdAt);
|
||||
if (!date) continue;
|
||||
|
||||
if (!dayMap[date]) dayMap[date] = {};
|
||||
if (!dayMap[date][siteName]) dayMap[date][siteName] = { spend: 0, calls: 0 };
|
||||
|
||||
const explicitCost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
const cost = explicitCost > 0 ? explicitCost : fallbackTokenCost(log.totalTokens || 0, platform);
|
||||
dayMap[date][siteName].spend += cost;
|
||||
dayMap[date][siteName].calls++;
|
||||
}
|
||||
|
||||
// Round spend values and sort by date
|
||||
const trend = Object.entries(dayMap)
|
||||
.sort(([a], [b]) => a.localeCompare(b))
|
||||
.map(([date, sites]) => {
|
||||
const rounded: Record<string, { spend: number; calls: number }> = {};
|
||||
for (const [name, stats] of Object.entries(sites)) {
|
||||
rounded[name] = {
|
||||
spend: Math.round(stats.spend * 1_000_000) / 1_000_000,
|
||||
calls: stats.calls,
|
||||
};
|
||||
}
|
||||
return { date, sites: rounded };
|
||||
});
|
||||
|
||||
return { trend };
|
||||
});
|
||||
|
||||
// Model stats by site
|
||||
app.get<{ Querystring: { siteId?: string; days?: string } }>('/api/stats/model-by-site', async (request) => {
|
||||
const siteId = request.query.siteId ? parseInt(request.query.siteId, 10) : null;
|
||||
const days = Math.max(1, parseInt(request.query.days || '7', 10));
|
||||
const sinceDate = getLocalRangeStartUtc(days);
|
||||
|
||||
// Get account IDs belonging to the site (if filtered)
|
||||
let accountIds: Set<number> | null = null;
|
||||
if (siteId != null && !Number.isNaN(siteId)) {
|
||||
const siteAccounts = db.select().from(schema.accounts)
|
||||
.where(eq(schema.accounts.siteId, siteId)).all();
|
||||
accountIds = new Set(siteAccounts.map((a) => a.id));
|
||||
}
|
||||
|
||||
const rows = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(gte(schema.proxyLogs.createdAt, sinceDate), eq(schema.sites.status, 'active')))
|
||||
.all();
|
||||
|
||||
const modelMap: Record<string, { calls: number; spend: number; tokens: number }> = {};
|
||||
|
||||
for (const row of rows) {
|
||||
const log = row.proxy_logs;
|
||||
// Filter by site if siteId is specified
|
||||
if (accountIds != null && (log.accountId == null || !accountIds.has(log.accountId))) continue;
|
||||
|
||||
const model = log.modelActual || log.modelRequested || 'unknown';
|
||||
const platform = row.sites?.platform || 'new-api';
|
||||
|
||||
if (!modelMap[model]) modelMap[model] = { calls: 0, spend: 0, tokens: 0 };
|
||||
modelMap[model].calls++;
|
||||
modelMap[model].tokens += log.totalTokens || 0;
|
||||
|
||||
const explicitCost = typeof log.estimatedCost === 'number' ? log.estimatedCost : 0;
|
||||
const cost = explicitCost > 0 ? explicitCost : fallbackTokenCost(log.totalTokens || 0, platform);
|
||||
modelMap[model].spend += cost;
|
||||
}
|
||||
|
||||
const models = Object.entries(modelMap)
|
||||
.map(([model, stats]) => ({
|
||||
model,
|
||||
calls: stats.calls,
|
||||
spend: Math.round(stats.spend * 1_000_000) / 1_000_000,
|
||||
tokens: stats.tokens,
|
||||
}))
|
||||
.sort((a, b) => b.calls - a.calls);
|
||||
|
||||
return { models };
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,22 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { getBackgroundTask, listBackgroundTasks } from '../../services/backgroundTaskService.js';
|
||||
|
||||
export async function taskRoutes(app: FastifyInstance) {
|
||||
app.get<{ Querystring: { limit?: string } }>('/api/tasks', async (request) => {
|
||||
const limit = Number.parseInt(request.query.limit || '50', 10);
|
||||
return {
|
||||
tasks: listBackgroundTasks(limit),
|
||||
};
|
||||
});
|
||||
|
||||
app.get<{ Params: { id: string } }>('/api/tasks/:id', async (request, reply) => {
|
||||
const task = getBackgroundTask(request.params.id);
|
||||
if (!task) {
|
||||
return reply.code(404).send({ success: false, message: 'task not found' });
|
||||
}
|
||||
return {
|
||||
success: true,
|
||||
task,
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,451 @@
|
||||
import { FastifyInstance, FastifyReply } from 'fastify';
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { fetch } from 'undici';
|
||||
import { config } from '../../config.js';
|
||||
|
||||
type TestChatMessage = { role: string; content: string };
|
||||
type TestTargetFormat = 'openai' | 'claude';
|
||||
|
||||
type TestChatRequestBody = {
|
||||
model?: string;
|
||||
messages?: TestChatMessage[];
|
||||
targetFormat?: TestTargetFormat;
|
||||
stream?: boolean;
|
||||
temperature?: number;
|
||||
top_p?: number;
|
||||
max_tokens?: number;
|
||||
frequency_penalty?: number;
|
||||
presence_penalty?: number;
|
||||
seed?: number;
|
||||
};
|
||||
|
||||
type ValidatedTestChatPayload = {
|
||||
model: string;
|
||||
messages: TestChatMessage[];
|
||||
targetFormat: TestTargetFormat;
|
||||
stream?: boolean;
|
||||
temperature?: number;
|
||||
top_p?: number;
|
||||
max_tokens?: number;
|
||||
frequency_penalty?: number;
|
||||
presence_penalty?: number;
|
||||
seed?: number;
|
||||
};
|
||||
|
||||
type TestChatJobStatus = 'pending' | 'succeeded' | 'failed' | 'cancelled';
|
||||
|
||||
type TestChatJob = {
|
||||
id: string;
|
||||
status: TestChatJobStatus;
|
||||
payload: ValidatedTestChatPayload;
|
||||
result?: unknown;
|
||||
error?: unknown;
|
||||
controller?: AbortController | null;
|
||||
createdAt: number;
|
||||
updatedAt: number;
|
||||
expiresAt: number;
|
||||
};
|
||||
|
||||
const JOB_TTL_MS = 10 * 60 * 1000;
|
||||
const JOB_CLEANUP_INTERVAL_MS = 60 * 1000;
|
||||
const jobs = new Map<string, TestChatJob>();
|
||||
|
||||
class UpstreamProxyError extends Error {
|
||||
statusCode: number;
|
||||
responsePayload: unknown;
|
||||
|
||||
constructor(statusCode: number, responsePayload: unknown) {
|
||||
super(`Upstream request failed with status ${statusCode}`);
|
||||
this.name = 'UpstreamProxyError';
|
||||
this.statusCode = statusCode;
|
||||
this.responsePayload = responsePayload;
|
||||
}
|
||||
}
|
||||
|
||||
const normalizeErrorPayload = (text: string): unknown => {
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return { error: { message: text, type: 'upstream_error' } };
|
||||
}
|
||||
};
|
||||
|
||||
const validatePayload = (
|
||||
body: TestChatRequestBody,
|
||||
reply: FastifyReply,
|
||||
): ValidatedTestChatPayload | null => {
|
||||
if (!body.model || body.model.trim().length === 0) {
|
||||
reply.code(400).send({ error: 'model is required' });
|
||||
return null;
|
||||
}
|
||||
|
||||
if (!Array.isArray(body.messages) || body.messages.length === 0) {
|
||||
reply.code(400).send({ error: 'messages is required' });
|
||||
return null;
|
||||
}
|
||||
|
||||
const targetFormat: TestTargetFormat = body.targetFormat === 'claude' ? 'claude' : 'openai';
|
||||
|
||||
return {
|
||||
model: body.model,
|
||||
messages: body.messages,
|
||||
targetFormat,
|
||||
stream: body.stream,
|
||||
temperature: body.temperature,
|
||||
top_p: body.top_p,
|
||||
max_tokens: body.max_tokens,
|
||||
frequency_penalty: body.frequency_penalty,
|
||||
presence_penalty: body.presence_penalty,
|
||||
seed: body.seed,
|
||||
};
|
||||
};
|
||||
|
||||
const convertOpenAiPayloadToClaudeBody = (
|
||||
payload: ValidatedTestChatPayload,
|
||||
forceStream: boolean,
|
||||
): Record<string, unknown> => {
|
||||
const systemContents: string[] = [];
|
||||
const messages: Array<{ role: 'user' | 'assistant'; content: string }> = [];
|
||||
|
||||
for (const message of payload.messages) {
|
||||
const role = typeof message.role === 'string' ? message.role : 'user';
|
||||
const content = typeof message.content === 'string' ? message.content : '';
|
||||
if (!content.trim()) continue;
|
||||
|
||||
if (role === 'system') {
|
||||
systemContents.push(content);
|
||||
continue;
|
||||
}
|
||||
|
||||
messages.push({
|
||||
role: role === 'assistant' ? 'assistant' : 'user',
|
||||
content,
|
||||
});
|
||||
}
|
||||
|
||||
const body: Record<string, unknown> = {
|
||||
model: payload.model,
|
||||
stream: forceStream,
|
||||
max_tokens: typeof payload.max_tokens === 'number' && Number.isFinite(payload.max_tokens)
|
||||
? payload.max_tokens
|
||||
: 4096,
|
||||
messages,
|
||||
};
|
||||
|
||||
if (systemContents.length > 0) {
|
||||
body.system = systemContents.join('\n\n');
|
||||
}
|
||||
|
||||
if (typeof payload.temperature === 'number' && Number.isFinite(payload.temperature)) {
|
||||
body.temperature = payload.temperature;
|
||||
}
|
||||
if (typeof payload.top_p === 'number' && Number.isFinite(payload.top_p)) {
|
||||
body.top_p = payload.top_p;
|
||||
}
|
||||
|
||||
return body;
|
||||
};
|
||||
|
||||
const buildUpstreamRequest = (
|
||||
payload: ValidatedTestChatPayload,
|
||||
forceStream: boolean,
|
||||
): { url: string; headers: Record<string, string>; body: Record<string, unknown> } => {
|
||||
if (payload.targetFormat === 'claude') {
|
||||
return {
|
||||
url: `http://127.0.0.1:${config.port}/v1/messages`,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'x-api-key': config.proxyToken,
|
||||
'anthropic-version': '2023-06-01',
|
||||
},
|
||||
body: convertOpenAiPayloadToClaudeBody(payload, forceStream),
|
||||
};
|
||||
}
|
||||
|
||||
const { targetFormat: _targetFormat, ...openAiPayload } = payload;
|
||||
return {
|
||||
url: `http://127.0.0.1:${config.port}/v1/chat/completions`,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${config.proxyToken}`,
|
||||
},
|
||||
body: { ...openAiPayload, stream: forceStream },
|
||||
};
|
||||
};
|
||||
|
||||
const requestUpstreamChat = async (
|
||||
payload: ValidatedTestChatPayload,
|
||||
signal?: AbortSignal,
|
||||
forceStream = false,
|
||||
): Promise<unknown> => {
|
||||
const upstreamRequest = buildUpstreamRequest(payload, forceStream);
|
||||
const upstream = await fetch(upstreamRequest.url, {
|
||||
method: 'POST',
|
||||
headers: upstreamRequest.headers,
|
||||
body: JSON.stringify(upstreamRequest.body),
|
||||
signal,
|
||||
});
|
||||
|
||||
const text = await upstream.text();
|
||||
if (!upstream.ok) {
|
||||
throw new UpstreamProxyError(upstream.status, normalizeErrorPayload(text));
|
||||
}
|
||||
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return { raw: text };
|
||||
}
|
||||
};
|
||||
|
||||
const cleanupExpiredJobs = () => {
|
||||
const now = Date.now();
|
||||
for (const [jobId, job] of jobs.entries()) {
|
||||
if (job.expiresAt <= now) {
|
||||
jobs.delete(jobId);
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
const runJob = async (jobId: string) => {
|
||||
const job = jobs.get(jobId);
|
||||
if (!job || job.status !== 'pending') return;
|
||||
|
||||
const controller = new AbortController();
|
||||
job.controller = controller;
|
||||
|
||||
try {
|
||||
const result = await requestUpstreamChat(job.payload, controller.signal);
|
||||
const current = jobs.get(jobId);
|
||||
if (!current) return;
|
||||
current.controller = null;
|
||||
current.status = 'succeeded';
|
||||
current.result = result;
|
||||
current.updatedAt = Date.now();
|
||||
current.expiresAt = current.updatedAt + JOB_TTL_MS;
|
||||
} catch (error) {
|
||||
const current = jobs.get(jobId);
|
||||
if (!current) return;
|
||||
current.controller = null;
|
||||
|
||||
if ((error as any)?.name === 'AbortError') {
|
||||
current.status = 'cancelled';
|
||||
current.error = { error: { message: 'job cancelled', type: 'cancelled' } };
|
||||
current.updatedAt = Date.now();
|
||||
current.expiresAt = current.updatedAt + 30_000;
|
||||
return;
|
||||
}
|
||||
|
||||
current.status = 'failed';
|
||||
current.error = error instanceof UpstreamProxyError
|
||||
? error.responsePayload
|
||||
: { error: { message: (error as any)?.message || 'proxy request failed', type: 'server_error' } };
|
||||
current.updatedAt = Date.now();
|
||||
current.expiresAt = current.updatedAt + JOB_TTL_MS;
|
||||
}
|
||||
};
|
||||
|
||||
export async function testRoutes(app: FastifyInstance) {
|
||||
const cleanupTimer = setInterval(cleanupExpiredJobs, JOB_CLEANUP_INTERVAL_MS);
|
||||
cleanupTimer.unref?.();
|
||||
|
||||
app.addHook('onClose', async () => {
|
||||
clearInterval(cleanupTimer);
|
||||
});
|
||||
|
||||
app.post<{ Body: TestChatRequestBody }>(
|
||||
'/api/test/chat',
|
||||
async (request, reply) => {
|
||||
const body = request.body || {};
|
||||
const payload = validatePayload(body, reply);
|
||||
if (!payload) return;
|
||||
|
||||
try {
|
||||
const data = await requestUpstreamChat(payload, undefined, false);
|
||||
return reply.send(data);
|
||||
} catch (error) {
|
||||
if (error instanceof UpstreamProxyError) {
|
||||
return reply.code(error.statusCode).send(error.responsePayload);
|
||||
}
|
||||
return reply.code(502).send({
|
||||
error: {
|
||||
message: (error as any)?.message || 'proxy request failed',
|
||||
type: 'server_error',
|
||||
},
|
||||
});
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
app.post<{ Body: TestChatRequestBody }>(
|
||||
'/api/test/chat/stream',
|
||||
async (request, reply) => {
|
||||
const body = request.body || {};
|
||||
const payload = validatePayload(body, reply);
|
||||
if (!payload) return;
|
||||
|
||||
const controller = new AbortController();
|
||||
const abortUpstream = () => {
|
||||
try {
|
||||
if (!controller.signal.aborted) {
|
||||
controller.abort();
|
||||
}
|
||||
} catch {
|
||||
// no-op
|
||||
}
|
||||
};
|
||||
const onClientAborted = () => {
|
||||
abortUpstream();
|
||||
};
|
||||
const onClientClosed = () => {
|
||||
if (!reply.raw.writableEnded) {
|
||||
abortUpstream();
|
||||
}
|
||||
};
|
||||
const cleanupClientListeners = () => {
|
||||
request.raw.off?.('aborted', onClientAborted);
|
||||
reply.raw.off?.('close', onClientClosed);
|
||||
};
|
||||
request.raw.on('aborted', onClientAborted);
|
||||
reply.raw.on('close', onClientClosed);
|
||||
|
||||
let upstream;
|
||||
try {
|
||||
const upstreamRequest = buildUpstreamRequest(payload, true);
|
||||
upstream = await fetch(upstreamRequest.url, {
|
||||
method: 'POST',
|
||||
headers: upstreamRequest.headers,
|
||||
body: JSON.stringify(upstreamRequest.body),
|
||||
signal: controller.signal,
|
||||
});
|
||||
} catch (error) {
|
||||
cleanupClientListeners();
|
||||
return reply.code(502).send({
|
||||
error: {
|
||||
message: (error as any)?.message || 'proxy request failed',
|
||||
type: 'server_error',
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (!upstream.ok) {
|
||||
const text = await upstream.text();
|
||||
cleanupClientListeners();
|
||||
return reply.code(upstream.status).send(normalizeErrorPayload(text));
|
||||
}
|
||||
|
||||
reply.hijack();
|
||||
reply.raw.statusCode = 200;
|
||||
reply.raw.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
||||
reply.raw.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
reply.raw.setHeader('Connection', 'keep-alive');
|
||||
reply.raw.setHeader('X-Accel-Buffering', 'no');
|
||||
|
||||
const reader = upstream.body?.getReader();
|
||||
if (!reader) {
|
||||
cleanupClientListeners();
|
||||
reply.raw.end();
|
||||
return;
|
||||
}
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (value) {
|
||||
reply.raw.write(Buffer.from(value));
|
||||
}
|
||||
}
|
||||
} catch (error) {
|
||||
if (!reply.raw.writableEnded) {
|
||||
const message = JSON.stringify({
|
||||
error: { message: (error as any)?.message || 'stream interrupted', type: 'stream_error' },
|
||||
});
|
||||
reply.raw.write(`event: error\ndata: ${message}\n\n`);
|
||||
}
|
||||
} finally {
|
||||
try {
|
||||
await reader.cancel();
|
||||
} catch {
|
||||
// no-op
|
||||
}
|
||||
cleanupClientListeners();
|
||||
if (!reply.raw.writableEnded) {
|
||||
reply.raw.end();
|
||||
}
|
||||
}
|
||||
},
|
||||
);
|
||||
|
||||
app.post<{ Body: TestChatRequestBody }>(
|
||||
'/api/test/chat/jobs',
|
||||
async (request, reply) => {
|
||||
const body = request.body || {};
|
||||
const payload = validatePayload(body, reply);
|
||||
if (!payload) return;
|
||||
|
||||
const now = Date.now();
|
||||
const jobId = randomUUID();
|
||||
const job: TestChatJob = {
|
||||
id: jobId,
|
||||
status: 'pending',
|
||||
payload,
|
||||
controller: null,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
expiresAt: now + JOB_TTL_MS,
|
||||
};
|
||||
|
||||
jobs.set(jobId, job);
|
||||
void runJob(jobId);
|
||||
|
||||
return reply.code(202).send({
|
||||
jobId,
|
||||
status: job.status,
|
||||
createdAt: new Date(job.createdAt).toISOString(),
|
||||
expiresAt: new Date(job.expiresAt).toISOString(),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.get<{ Params: { jobId: string } }>(
|
||||
'/api/test/chat/jobs/:jobId',
|
||||
async (request, reply) => {
|
||||
const job = jobs.get(request.params.jobId);
|
||||
if (!job) {
|
||||
return reply.code(404).send({ error: { message: 'job not found', type: 'not_found' } });
|
||||
}
|
||||
|
||||
return reply.send({
|
||||
jobId: job.id,
|
||||
status: job.status,
|
||||
result: job.result,
|
||||
error: job.error,
|
||||
createdAt: new Date(job.createdAt).toISOString(),
|
||||
updatedAt: new Date(job.updatedAt).toISOString(),
|
||||
expiresAt: new Date(job.expiresAt).toISOString(),
|
||||
});
|
||||
},
|
||||
);
|
||||
|
||||
app.delete<{ Params: { jobId: string } }>(
|
||||
'/api/test/chat/jobs/:jobId',
|
||||
async (request, reply) => {
|
||||
const job = jobs.get(request.params.jobId);
|
||||
if (!job) {
|
||||
return reply.code(404).send({ error: { message: 'job not found', type: 'not_found' } });
|
||||
}
|
||||
|
||||
if (job.status === 'pending' && job.controller) {
|
||||
try {
|
||||
job.controller.abort();
|
||||
} catch {
|
||||
// no-op
|
||||
}
|
||||
}
|
||||
|
||||
jobs.delete(request.params.jobId);
|
||||
return reply.send({ success: true });
|
||||
},
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,157 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { eq } from 'drizzle-orm';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('PUT /api/channels/batch', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
let seedId = 0;
|
||||
|
||||
const nextId = () => {
|
||||
seedId += 1;
|
||||
return seedId;
|
||||
};
|
||||
|
||||
const seedChannel = (options: { priority: number; weight: number; manualOverride?: boolean }) => {
|
||||
const id = nextId();
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: `site-${id}`,
|
||||
url: `https://example.com/${id}`,
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
accessToken: `access-token-${id}`,
|
||||
apiToken: `api-token-${id}`,
|
||||
}).returning().get();
|
||||
const route = db.insert(schema.tokenRoutes).values({
|
||||
modelPattern: `gpt-4o-${id}`,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
return db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: account.id,
|
||||
priority: options.priority,
|
||||
weight: options.weight,
|
||||
manualOverride: options.manualOverride ?? false,
|
||||
}).returning().get();
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-tokens-batch-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./tokens.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.tokensRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('returns 400 when updates is missing or empty', async () => {
|
||||
const missingRes = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/channels/batch',
|
||||
payload: {},
|
||||
});
|
||||
expect(missingRes.statusCode).toBe(400);
|
||||
expect(missingRes.json()).toMatchObject({ success: false });
|
||||
|
||||
const emptyRes = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/channels/batch',
|
||||
payload: { updates: [] },
|
||||
});
|
||||
expect(emptyRes.statusCode).toBe(400);
|
||||
expect(emptyRes.json()).toMatchObject({ success: false });
|
||||
});
|
||||
|
||||
it('returns 400 when an update item is invalid', async () => {
|
||||
const invalidIdRes = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/channels/batch',
|
||||
payload: {
|
||||
updates: [{ id: '1', priority: 1 }],
|
||||
},
|
||||
});
|
||||
expect(invalidIdRes.statusCode).toBe(400);
|
||||
expect(invalidIdRes.json()).toMatchObject({ success: false });
|
||||
|
||||
const invalidPriorityRes = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/channels/batch',
|
||||
payload: {
|
||||
updates: [{ id: 1, priority: null }],
|
||||
},
|
||||
});
|
||||
expect(invalidPriorityRes.statusCode).toBe(400);
|
||||
expect(invalidPriorityRes.json()).toMatchObject({ success: false });
|
||||
});
|
||||
|
||||
it('updates priorities in batch, sets manualOverride, and keeps weight unchanged', async () => {
|
||||
const channelA = seedChannel({ priority: 9, weight: 17, manualOverride: false });
|
||||
const channelB = seedChannel({ priority: 8, weight: 23, manualOverride: false });
|
||||
|
||||
const res = await app.inject({
|
||||
method: 'PUT',
|
||||
url: '/api/channels/batch',
|
||||
payload: {
|
||||
updates: [
|
||||
{ id: channelA.id, priority: 3.8 },
|
||||
{ id: channelB.id, priority: -7.2 },
|
||||
],
|
||||
},
|
||||
});
|
||||
|
||||
expect(res.statusCode).toBe(200);
|
||||
const body = res.json() as {
|
||||
success: boolean;
|
||||
channels: Array<{ id: number; priority: number; weight: number; manualOverride: boolean }>;
|
||||
};
|
||||
expect(body.success).toBe(true);
|
||||
expect(body.channels).toHaveLength(2);
|
||||
|
||||
const returnedA = body.channels.find((channel) => channel.id === channelA.id);
|
||||
const returnedB = body.channels.find((channel) => channel.id === channelB.id);
|
||||
expect(returnedA).toBeDefined();
|
||||
expect(returnedB).toBeDefined();
|
||||
expect(returnedA?.priority).toBe(3);
|
||||
expect(returnedB?.priority).toBe(0);
|
||||
expect(returnedA?.weight).toBe(17);
|
||||
expect(returnedB?.weight).toBe(23);
|
||||
expect(returnedA?.manualOverride).toBe(true);
|
||||
expect(returnedB?.manualOverride).toBe(true);
|
||||
|
||||
const dbA = db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelA.id)).get();
|
||||
const dbB = db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelB.id)).get();
|
||||
expect(dbA?.priority).toBe(3);
|
||||
expect(dbB?.priority).toBe(0);
|
||||
expect(dbA?.weight).toBe(17);
|
||||
expect(dbB?.weight).toBe(23);
|
||||
expect(dbA?.manualOverride).toBe(true);
|
||||
expect(dbB?.manualOverride).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,102 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { describe, expect, it, beforeAll, beforeEach, afterAll } from 'vitest';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
|
||||
type DbModule = typeof import('../../db/index.js');
|
||||
|
||||
describe('POST /api/routes/decision/batch', () => {
|
||||
let app: FastifyInstance;
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let dataDir = '';
|
||||
let seedId = 0;
|
||||
|
||||
const nextId = () => {
|
||||
seedId += 1;
|
||||
return seedId;
|
||||
};
|
||||
|
||||
const seedRoutableChannel = () => {
|
||||
const id = nextId();
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: `site-${id}`,
|
||||
url: `https://site-${id}.example.com`,
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: `user-${id}`,
|
||||
accessToken: `access-token-${id}`,
|
||||
apiToken: `sk-api-token-${id}`,
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
const route = db.insert(schema.tokenRoutes).values({
|
||||
modelPattern: 'gpt-4o-mini',
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: account.id,
|
||||
tokenId: null,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
}).run();
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-route-decision-batch-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../../db/migrate.js');
|
||||
const dbModule = await import('../../db/index.js');
|
||||
const routesModule = await import('./tokens.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
|
||||
app = Fastify();
|
||||
await app.register(routesModule.tokensRoutes);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
seedId = 0;
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('returns decisions for multiple requested models in one call', async () => {
|
||||
seedRoutableChannel();
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/api/routes/decision/batch',
|
||||
payload: {
|
||||
models: ['gpt-4o-mini', 'gpt-4o-mini', 'unknown-model', ''],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json() as {
|
||||
success: boolean;
|
||||
decisions: Record<string, { matched: boolean; candidates: Array<unknown> }>;
|
||||
};
|
||||
expect(body.success).toBe(true);
|
||||
expect(Object.keys(body.decisions).sort()).toEqual(['gpt-4o-mini', 'unknown-model']);
|
||||
expect(body.decisions['gpt-4o-mini']?.matched).toBe(true);
|
||||
expect(Array.isArray(body.decisions['gpt-4o-mini']?.candidates)).toBe(true);
|
||||
expect(body.decisions['gpt-4o-mini']?.candidates.length).toBeGreaterThan(0);
|
||||
expect(body.decisions['unknown-model']?.matched).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,358 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { and, eq, inArray } from 'drizzle-orm';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { rebuildTokenRoutesFromAvailability, refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { tokenRouter } from '../../services/tokenRouter.js';
|
||||
import { startBackgroundTask } from '../../services/backgroundTaskService.js';
|
||||
|
||||
function isExactModelPattern(modelPattern: string): boolean {
|
||||
return !/[\*\?\[]/.test(modelPattern);
|
||||
}
|
||||
|
||||
function getDefaultTokenId(accountId: number): number | null {
|
||||
const token = db.select().from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, accountId), eq(schema.accountTokens.enabled, true), eq(schema.accountTokens.isDefault, true)))
|
||||
.get();
|
||||
return token?.id ?? null;
|
||||
}
|
||||
|
||||
function tokenSupportsModel(tokenId: number, modelName: string): boolean {
|
||||
const row = db.select().from(schema.tokenModelAvailability)
|
||||
.where(
|
||||
and(
|
||||
eq(schema.tokenModelAvailability.tokenId, tokenId),
|
||||
eq(schema.tokenModelAvailability.modelName, modelName),
|
||||
eq(schema.tokenModelAvailability.available, true),
|
||||
),
|
||||
)
|
||||
.get();
|
||||
return !!row;
|
||||
}
|
||||
|
||||
function checkTokenBelongsToAccount(tokenId: number, accountId: number): boolean {
|
||||
const row = db.select().from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.id, tokenId), eq(schema.accountTokens.accountId, accountId)))
|
||||
.get();
|
||||
return !!row;
|
||||
}
|
||||
|
||||
type BatchChannelPriorityUpdate = {
|
||||
id: number;
|
||||
priority: number;
|
||||
};
|
||||
|
||||
type BatchRouteDecisionModels = {
|
||||
models: string[];
|
||||
};
|
||||
|
||||
function parseBatchChannelUpdates(input: unknown): { ok: true; updates: BatchChannelPriorityUpdate[] } | { ok: false; message: string } {
|
||||
if (!input || typeof input !== 'object') {
|
||||
return { ok: false, message: '请求体必须是对象' };
|
||||
}
|
||||
|
||||
const updates = (input as { updates?: unknown }).updates;
|
||||
if (!Array.isArray(updates) || updates.length === 0) {
|
||||
return { ok: false, message: 'updates 必须是非空数组' };
|
||||
}
|
||||
|
||||
const normalized: BatchChannelPriorityUpdate[] = [];
|
||||
for (let index = 0; index < updates.length; index += 1) {
|
||||
const item = updates[index];
|
||||
if (!item || typeof item !== 'object') {
|
||||
return { ok: false, message: `updates[${index}] 必须是对象` };
|
||||
}
|
||||
|
||||
const { id, priority } = item as { id?: unknown; priority?: unknown };
|
||||
if (typeof id !== 'number' || !Number.isFinite(id)) {
|
||||
return { ok: false, message: `updates[${index}].id 必须是有限数字` };
|
||||
}
|
||||
if (typeof priority !== 'number' || !Number.isFinite(priority)) {
|
||||
return { ok: false, message: `updates[${index}].priority 必须是有限数字` };
|
||||
}
|
||||
|
||||
const normalizedId = Math.trunc(id);
|
||||
if (normalizedId <= 0) {
|
||||
return { ok: false, message: `updates[${index}].id 必须大于 0` };
|
||||
}
|
||||
|
||||
normalized.push({
|
||||
id: normalizedId,
|
||||
priority: Math.max(0, Math.trunc(priority)),
|
||||
});
|
||||
}
|
||||
|
||||
return { ok: true, updates: normalized };
|
||||
}
|
||||
|
||||
function parseBatchRouteDecisionModels(input: unknown): { ok: true; models: string[] } | { ok: false; message: string } {
|
||||
if (!input || typeof input !== 'object') {
|
||||
return { ok: false, message: '请求体必须是对象' };
|
||||
}
|
||||
|
||||
const models = (input as BatchRouteDecisionModels).models;
|
||||
if (!Array.isArray(models) || models.length === 0) {
|
||||
return { ok: false, message: 'models 必须是非空数组' };
|
||||
}
|
||||
|
||||
const dedupe = new Set<string>();
|
||||
const normalized: string[] = [];
|
||||
for (const raw of models) {
|
||||
if (typeof raw !== 'string') continue;
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed || dedupe.has(trimmed)) continue;
|
||||
dedupe.add(trimmed);
|
||||
normalized.push(trimmed);
|
||||
if (normalized.length >= 500) break;
|
||||
}
|
||||
|
||||
if (normalized.length === 0) {
|
||||
return { ok: false, message: 'models 中没有有效模型名称' };
|
||||
}
|
||||
|
||||
return { ok: true, models: normalized };
|
||||
}
|
||||
|
||||
export async function tokensRoutes(app: FastifyInstance) {
|
||||
// List all routes
|
||||
app.get('/api/routes', async () => {
|
||||
const routes = db.select().from(schema.tokenRoutes).all();
|
||||
if (routes.length === 0) return [];
|
||||
|
||||
const routeIds = routes.map((route) => route.id);
|
||||
const channelRows = db.select().from(schema.routeChannels)
|
||||
.innerJoin(schema.accounts, eq(schema.routeChannels.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.leftJoin(schema.accountTokens, eq(schema.routeChannels.tokenId, schema.accountTokens.id))
|
||||
.where(inArray(schema.routeChannels.routeId, routeIds))
|
||||
.all();
|
||||
|
||||
const channelsByRoute = new Map<number, any[]>();
|
||||
|
||||
for (const row of channelRows) {
|
||||
const routeId = row.route_channels.routeId;
|
||||
if (!channelsByRoute.has(routeId)) channelsByRoute.set(routeId, []);
|
||||
channelsByRoute.get(routeId)!.push({
|
||||
...row.route_channels,
|
||||
account: row.accounts,
|
||||
site: row.sites,
|
||||
token: row.account_tokens
|
||||
? {
|
||||
id: row.account_tokens.id,
|
||||
name: row.account_tokens.name,
|
||||
accountId: row.account_tokens.accountId,
|
||||
enabled: row.account_tokens.enabled,
|
||||
isDefault: row.account_tokens.isDefault,
|
||||
}
|
||||
: null,
|
||||
});
|
||||
}
|
||||
|
||||
return routes.map((route) => ({
|
||||
...route,
|
||||
channels: channelsByRoute.get(route.id) || [],
|
||||
}));
|
||||
});
|
||||
|
||||
app.get<{ Querystring: { model?: string } }>('/api/routes/decision', async (request, reply) => {
|
||||
const model = (request.query.model || '').trim();
|
||||
if (!model) {
|
||||
return reply.code(400).send({ success: false, message: 'model 不能为空' });
|
||||
}
|
||||
|
||||
const decision = tokenRouter.explainSelection(model);
|
||||
return { success: true, decision };
|
||||
});
|
||||
|
||||
app.post<{ Body: BatchRouteDecisionModels }>('/api/routes/decision/batch', async (request, reply) => {
|
||||
const parsed = parseBatchRouteDecisionModels(request.body);
|
||||
if (!parsed.ok) {
|
||||
return reply.code(400).send({ success: false, message: parsed.message });
|
||||
}
|
||||
|
||||
const decisions: Record<string, ReturnType<typeof tokenRouter.explainSelection>> = {};
|
||||
for (const model of parsed.models) {
|
||||
decisions[model] = tokenRouter.explainSelection(model);
|
||||
}
|
||||
|
||||
return { success: true, decisions };
|
||||
});
|
||||
|
||||
// Create a route
|
||||
app.post<{ Body: { modelPattern: string; modelMapping?: string; enabled?: boolean } }>('/api/routes', async (request) => {
|
||||
const body = request.body;
|
||||
return db.insert(schema.tokenRoutes).values({
|
||||
modelPattern: body.modelPattern,
|
||||
modelMapping: body.modelMapping,
|
||||
enabled: body.enabled ?? true,
|
||||
}).returning().get();
|
||||
});
|
||||
|
||||
// Update a route
|
||||
app.put<{ Params: { id: string }; Body: any }>('/api/routes/:id', async (request) => {
|
||||
const id = parseInt(request.params.id, 10);
|
||||
const body = request.body as Record<string, unknown>;
|
||||
const updates: Record<string, unknown> = {};
|
||||
|
||||
if (body.modelPattern !== undefined) updates.modelPattern = body.modelPattern;
|
||||
if (body.modelMapping !== undefined) updates.modelMapping = body.modelMapping;
|
||||
if (body.enabled !== undefined) updates.enabled = body.enabled;
|
||||
updates.updatedAt = new Date().toISOString();
|
||||
|
||||
db.update(schema.tokenRoutes).set(updates).where(eq(schema.tokenRoutes.id, id)).run();
|
||||
return db.select().from(schema.tokenRoutes).where(eq(schema.tokenRoutes.id, id)).get();
|
||||
});
|
||||
|
||||
// Delete a route
|
||||
app.delete<{ Params: { id: string } }>('/api/routes/:id', async (request) => {
|
||||
const id = parseInt(request.params.id, 10);
|
||||
db.delete(schema.tokenRoutes).where(eq(schema.tokenRoutes.id, id)).run();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// Add a channel to a route
|
||||
app.post<{ Params: { id: string }; Body: { accountId: number; tokenId?: number; priority?: number; weight?: number } }>('/api/routes/:id/channels', async (request, reply) => {
|
||||
const routeId = parseInt(request.params.id, 10);
|
||||
const body = request.body;
|
||||
|
||||
const route = db.select().from(schema.tokenRoutes).where(eq(schema.tokenRoutes.id, routeId)).get();
|
||||
if (!route) {
|
||||
return reply.code(404).send({ success: false, message: '路由不存在' });
|
||||
}
|
||||
|
||||
const effectiveTokenId = body.tokenId ?? getDefaultTokenId(body.accountId);
|
||||
|
||||
if (body.tokenId && !checkTokenBelongsToAccount(body.tokenId, body.accountId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌不存在或不属于当前账号' });
|
||||
}
|
||||
|
||||
if (isExactModelPattern(route.modelPattern) && effectiveTokenId && !tokenSupportsModel(effectiveTokenId, route.modelPattern)) {
|
||||
return reply.code(400).send({ success: false, message: '该令牌不支持当前模型' });
|
||||
}
|
||||
|
||||
return db.insert(schema.routeChannels).values({
|
||||
routeId,
|
||||
accountId: body.accountId,
|
||||
tokenId: body.tokenId,
|
||||
priority: body.priority ?? 0,
|
||||
weight: body.weight ?? 10,
|
||||
}).returning().get();
|
||||
});
|
||||
|
||||
// Batch update channel priorities
|
||||
app.put<{ Body: { updates: Array<{ id: number; priority: number }> } }>('/api/channels/batch', async (request, reply) => {
|
||||
const parsed = parseBatchChannelUpdates(request.body);
|
||||
if (!parsed.ok) {
|
||||
return reply.code(400).send({ success: false, message: parsed.message });
|
||||
}
|
||||
|
||||
const channelIds = Array.from(new Set(parsed.updates.map((update) => update.id)));
|
||||
const existingChannels = db.select().from(schema.routeChannels)
|
||||
.where(inArray(schema.routeChannels.id, channelIds))
|
||||
.all();
|
||||
if (existingChannels.length !== channelIds.length) {
|
||||
const existingIds = new Set(existingChannels.map((channel) => channel.id));
|
||||
const missingId = channelIds.find((id) => !existingIds.has(id));
|
||||
return reply.code(404).send({ success: false, message: `通道不存在: ${missingId}` });
|
||||
}
|
||||
|
||||
for (const update of parsed.updates) {
|
||||
db.update(schema.routeChannels).set({
|
||||
priority: update.priority,
|
||||
manualOverride: true,
|
||||
}).where(eq(schema.routeChannels.id, update.id)).run();
|
||||
}
|
||||
|
||||
const updatedChannels = db.select().from(schema.routeChannels)
|
||||
.where(inArray(schema.routeChannels.id, channelIds))
|
||||
.all();
|
||||
return { success: true, channels: updatedChannels };
|
||||
});
|
||||
|
||||
// Update a channel
|
||||
app.put<{ Params: { channelId: string }; Body: any }>('/api/channels/:channelId', async (request, reply) => {
|
||||
const channelId = parseInt(request.params.channelId, 10);
|
||||
const body = request.body as Record<string, unknown>;
|
||||
|
||||
const channel = db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelId)).get();
|
||||
if (!channel) {
|
||||
return reply.code(404).send({ success: false, message: '通道不存在' });
|
||||
}
|
||||
|
||||
const route = db.select().from(schema.tokenRoutes).where(eq(schema.tokenRoutes.id, channel.routeId)).get();
|
||||
if (!route) {
|
||||
return reply.code(404).send({ success: false, message: '路由不存在' });
|
||||
}
|
||||
|
||||
if (body.tokenId !== undefined && body.tokenId !== null) {
|
||||
const tokenId = Number(body.tokenId);
|
||||
if (!Number.isFinite(tokenId) || !checkTokenBelongsToAccount(tokenId, channel.accountId)) {
|
||||
return reply.code(400).send({ success: false, message: '令牌不存在或不属于通道账号' });
|
||||
}
|
||||
}
|
||||
|
||||
const nextTokenId = body.tokenId === undefined
|
||||
? (channel.tokenId ?? getDefaultTokenId(channel.accountId))
|
||||
: (body.tokenId === null ? getDefaultTokenId(channel.accountId) : Number(body.tokenId));
|
||||
|
||||
if (isExactModelPattern(route.modelPattern) && nextTokenId && !tokenSupportsModel(nextTokenId, route.modelPattern)) {
|
||||
return reply.code(400).send({ success: false, message: '该令牌不支持当前模型' });
|
||||
}
|
||||
|
||||
const updates: Record<string, unknown> = { manualOverride: true };
|
||||
for (const key of ['priority', 'weight', 'enabled', 'tokenId']) {
|
||||
if (body[key] !== undefined) updates[key] = body[key];
|
||||
}
|
||||
|
||||
db.update(schema.routeChannels).set(updates).where(eq(schema.routeChannels.id, channelId)).run();
|
||||
return db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelId)).get();
|
||||
});
|
||||
|
||||
// Delete a channel
|
||||
app.delete<{ Params: { channelId: string } }>('/api/channels/:channelId', async (request) => {
|
||||
const channelId = parseInt(request.params.channelId, 10);
|
||||
db.delete(schema.routeChannels).where(eq(schema.routeChannels.id, channelId)).run();
|
||||
return { success: true };
|
||||
});
|
||||
|
||||
// Rebuild routes/channels from model availability.
|
||||
app.post<{ Body?: { refreshModels?: boolean; wait?: boolean } }>('/api/routes/rebuild', async (request, reply) => {
|
||||
const body = (request.body || {}) as { refreshModels?: boolean };
|
||||
if (body.refreshModels === false) {
|
||||
const rebuild = rebuildTokenRoutesFromAvailability();
|
||||
return { success: true, rebuild };
|
||||
}
|
||||
|
||||
if ((request.body as { wait?: boolean } | undefined)?.wait) {
|
||||
const result = await refreshModelsAndRebuildRoutes();
|
||||
return { success: true, ...result };
|
||||
}
|
||||
|
||||
const { task, reused } = startBackgroundTask(
|
||||
{
|
||||
type: 'route',
|
||||
title: '刷新模型并重建路由',
|
||||
dedupeKey: 'refresh-models-and-rebuild-routes',
|
||||
notifyOnFailure: true,
|
||||
successMessage: (currentTask) => {
|
||||
const rebuild = (currentTask.result as any)?.rebuild;
|
||||
if (!rebuild) return '刷新模型并重建路由已完成';
|
||||
return `刷新模型并重建路由完成:新增路由 ${rebuild.createdRoutes},新增通道 ${rebuild.createdChannels},移除通道 ${rebuild.removedChannels}`;
|
||||
},
|
||||
failureMessage: (currentTask) => `刷新模型并重建路由失败:${currentTask.error || 'unknown error'}`,
|
||||
},
|
||||
async () => refreshModelsAndRebuildRoutes(),
|
||||
);
|
||||
|
||||
return reply.code(202).send({
|
||||
success: true,
|
||||
queued: true,
|
||||
reused,
|
||||
jobId: task.id,
|
||||
status: task.status,
|
||||
message: reused
|
||||
? '路由重建任务执行中,请稍后查看程序日志'
|
||||
: '已开始路由重建,请稍后查看程序日志',
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,317 @@
|
||||
import Fastify, { type FastifyInstance } from 'fastify';
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const fetchMock = vi.fn();
|
||||
const selectChannelMock = vi.fn();
|
||||
const selectNextChannelMock = vi.fn();
|
||||
const recordSuccessMock = vi.fn();
|
||||
const recordFailureMock = vi.fn();
|
||||
const refreshModelsAndRebuildRoutesMock = vi.fn();
|
||||
const reportProxyAllFailedMock = vi.fn();
|
||||
const reportTokenExpiredMock = vi.fn();
|
||||
const estimateProxyCostMock = vi.fn(async (_arg?: any) => 0);
|
||||
const resolveProxyUsageWithSelfLogFallbackMock = vi.fn(async ({ usage }: any) => ({
|
||||
...usage,
|
||||
estimatedCostFromQuota: 0,
|
||||
recoveredFromSelfLog: false,
|
||||
}));
|
||||
const dbInsertMock = vi.fn((_arg?: any) => ({
|
||||
values: () => ({
|
||||
run: () => undefined,
|
||||
}),
|
||||
}));
|
||||
|
||||
vi.mock('undici', () => ({
|
||||
fetch: (...args: unknown[]) => fetchMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/tokenRouter.js', () => ({
|
||||
tokenRouter: {
|
||||
selectChannel: (...args: unknown[]) => selectChannelMock(...args),
|
||||
selectNextChannel: (...args: unknown[]) => selectNextChannelMock(...args),
|
||||
recordSuccess: (...args: unknown[]) => recordSuccessMock(...args),
|
||||
recordFailure: (...args: unknown[]) => recordFailureMock(...args),
|
||||
},
|
||||
}));
|
||||
|
||||
vi.mock('../../services/modelService.js', () => ({
|
||||
refreshModelsAndRebuildRoutes: (...args: unknown[]) => refreshModelsAndRebuildRoutesMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/alertService.js', () => ({
|
||||
reportProxyAllFailed: (...args: unknown[]) => reportProxyAllFailedMock(...args),
|
||||
reportTokenExpired: (...args: unknown[]) => reportTokenExpiredMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/alertRules.js', () => ({
|
||||
isTokenExpiredError: () => false,
|
||||
}));
|
||||
|
||||
vi.mock('../../services/modelPricingService.js', () => ({
|
||||
estimateProxyCost: (arg: any) => estimateProxyCostMock(arg),
|
||||
}));
|
||||
|
||||
vi.mock('../../services/proxyRetryPolicy.js', () => ({
|
||||
shouldRetryProxyRequest: () => false,
|
||||
}));
|
||||
|
||||
vi.mock('../../services/proxyUsageFallbackService.js', () => ({
|
||||
resolveProxyUsageWithSelfLogFallback: (arg: any) => resolveProxyUsageWithSelfLogFallbackMock(arg),
|
||||
}));
|
||||
|
||||
vi.mock('../../db/index.js', () => ({
|
||||
db: {
|
||||
insert: (arg: any) => dbInsertMock(arg),
|
||||
},
|
||||
schema: {
|
||||
proxyLogs: {},
|
||||
},
|
||||
}));
|
||||
|
||||
describe('chat proxy stream behavior', () => {
|
||||
let app: FastifyInstance;
|
||||
|
||||
beforeAll(async () => {
|
||||
const { chatProxyRoute, claudeMessagesProxyRoute } = await import('./chat.js');
|
||||
app = Fastify();
|
||||
await app.register(chatProxyRoute);
|
||||
await app.register(claudeMessagesProxyRoute);
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
fetchMock.mockReset();
|
||||
selectChannelMock.mockReset();
|
||||
selectNextChannelMock.mockReset();
|
||||
recordSuccessMock.mockReset();
|
||||
recordFailureMock.mockReset();
|
||||
refreshModelsAndRebuildRoutesMock.mockReset();
|
||||
reportProxyAllFailedMock.mockReset();
|
||||
reportTokenExpiredMock.mockReset();
|
||||
estimateProxyCostMock.mockClear();
|
||||
resolveProxyUsageWithSelfLogFallbackMock.mockClear();
|
||||
dbInsertMock.mockClear();
|
||||
|
||||
selectChannelMock.mockReturnValue({
|
||||
channel: { id: 11, routeId: 22 },
|
||||
site: { name: 'demo-site', url: 'https://upstream.example.com' },
|
||||
account: { id: 33, username: 'demo-user' },
|
||||
tokenName: 'default',
|
||||
tokenValue: 'sk-demo',
|
||||
actualModel: 'upstream-gpt',
|
||||
});
|
||||
selectNextChannelMock.mockReturnValue(null);
|
||||
});
|
||||
|
||||
afterAll(async () => {
|
||||
await app.close();
|
||||
});
|
||||
|
||||
it('converts non-SSE upstream streaming responses into SSE events', async () => {
|
||||
fetchMock.mockResolvedValue(new Response(JSON.stringify({
|
||||
id: 'chatcmpl-demo',
|
||||
object: 'chat.completion',
|
||||
created: 1_706_000_000,
|
||||
model: 'upstream-gpt',
|
||||
choices: [{
|
||||
index: 0,
|
||||
message: { role: 'assistant', content: 'hello from upstream' },
|
||||
finish_reason: 'stop',
|
||||
}],
|
||||
usage: { prompt_tokens: 11, completion_tokens: 7, total_tokens: 18 },
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
payload: {
|
||||
model: 'gpt-4o-mini',
|
||||
stream: true,
|
||||
messages: [{ role: 'user', content: 'hi' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers['content-type']).toContain('text/event-stream');
|
||||
expect(response.body).toContain('data: ');
|
||||
expect(response.body).toContain('"chat.completion.chunk"');
|
||||
expect(response.body).toContain('hello from upstream');
|
||||
expect(response.body).toContain('data: [DONE]');
|
||||
});
|
||||
|
||||
it('sets anti-buffering SSE headers for streamed chat responses', async () => {
|
||||
const encoder = new TextEncoder();
|
||||
const upstreamBody = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode('data: {"choices":[{"delta":{"content":"hello"}}]}\n\n'));
|
||||
controller.enqueue(encoder.encode('data: [DONE]\n\n'));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
fetchMock.mockResolvedValue(new Response(upstreamBody, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/event-stream; charset=utf-8' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
payload: {
|
||||
model: 'gpt-4o-mini',
|
||||
stream: true,
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers['content-type']).toContain('text/event-stream');
|
||||
expect(response.headers['cache-control']).toContain('no-transform');
|
||||
expect(response.headers['x-accel-buffering']).toBe('no');
|
||||
expect(response.body).toContain('"chat.completion.chunk"');
|
||||
expect(response.body).toContain('"delta":{"role":"assistant","content":"hello"}');
|
||||
expect(response.body).toContain('data: [DONE]');
|
||||
});
|
||||
|
||||
it('normalizes anthropic-style SSE events into OpenAI chunks for clients like OpenWebUI', async () => {
|
||||
const encoder = new TextEncoder();
|
||||
const upstreamBody = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode('event: message_start\ndata: {"type":"message_start","message":{"id":"msg_123","model":"claude-opus-4-6"}}\n\n'));
|
||||
controller.enqueue(encoder.encode('event: content_block_delta\ndata: {"type":"content_block_delta","delta":{"type":"text_delta","text":"hello"}}\n\n'));
|
||||
controller.enqueue(encoder.encode('event: message_delta\ndata: {"type":"message_delta","delta":{"stop_reason":"end_turn"}}\n\n'));
|
||||
controller.enqueue(encoder.encode('event: message_stop\ndata: {"type":"message_stop"}\n\n'));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
fetchMock.mockResolvedValue(new Response(upstreamBody, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/event-stream; charset=utf-8' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
payload: {
|
||||
model: 'claude-opus-4-6',
|
||||
stream: true,
|
||||
messages: [{ role: 'user', content: 'who are you' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers['content-type']).toContain('text/event-stream');
|
||||
expect(response.body).toContain('"chat.completion.chunk"');
|
||||
expect(response.body).toContain('"delta":{"content":"hello"}');
|
||||
expect(response.body).toContain('"finish_reason":"stop"');
|
||||
expect(response.body).toContain('data: [DONE]');
|
||||
});
|
||||
|
||||
it('emits OpenAI-compatible assistant starter chunk for anthropic message_start events', async () => {
|
||||
const encoder = new TextEncoder();
|
||||
const upstreamBody = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode('event: message_start\ndata: {"type":"message_start","message":{"id":"msg_compat","model":"claude-opus-4-6"}}\n\n'));
|
||||
controller.enqueue(encoder.encode('event: content_block_delta\ndata: {"type":"content_block_delta","delta":{"type":"text_delta","text":"compat"}}\n\n'));
|
||||
controller.enqueue(encoder.encode('event: message_stop\ndata: {"type":"message_stop"}\n\n'));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
fetchMock.mockResolvedValue(new Response(upstreamBody, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/event-stream; charset=utf-8' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/chat/completions',
|
||||
payload: {
|
||||
model: 'claude-opus-4-6',
|
||||
stream: true,
|
||||
messages: [{ role: 'user', content: 'compat test' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.body).toContain('"delta":{"role":"assistant","content":""}');
|
||||
expect(response.body).toContain('"delta":{"content":"compat"}');
|
||||
expect(response.body).toContain('data: [DONE]');
|
||||
});
|
||||
|
||||
it('converts OpenAI non-stream responses into Claude message format on /v1/messages', async () => {
|
||||
fetchMock.mockResolvedValue(new Response(JSON.stringify({
|
||||
id: 'chatcmpl-upstream',
|
||||
object: 'chat.completion',
|
||||
created: 1_706_000_001,
|
||||
model: 'claude-opus-4-6',
|
||||
choices: [{
|
||||
index: 0,
|
||||
message: { role: 'assistant', content: 'hello from claude format' },
|
||||
finish_reason: 'stop',
|
||||
}],
|
||||
usage: { prompt_tokens: 120, completion_tokens: 16, total_tokens: 136 },
|
||||
}), {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'application/json' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/messages',
|
||||
payload: {
|
||||
model: 'claude-opus-4-6',
|
||||
max_tokens: 256,
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
const body = response.json();
|
||||
expect(body.type).toBe('message');
|
||||
expect(body.role).toBe('assistant');
|
||||
expect(body.model).toBe('claude-opus-4-6');
|
||||
expect(body.content?.[0]?.type).toBe('text');
|
||||
expect(body.content?.[0]?.text).toContain('hello from claude format');
|
||||
expect(body.stop_reason).toBe('end_turn');
|
||||
});
|
||||
|
||||
it('converts OpenAI SSE chunks into Claude stream events on /v1/messages', async () => {
|
||||
const encoder = new TextEncoder();
|
||||
const upstreamBody = new ReadableStream<Uint8Array>({
|
||||
start(controller) {
|
||||
controller.enqueue(encoder.encode('data: {"id":"chatcmpl-1","model":"claude-opus-4-6","choices":[{"delta":{"role":"assistant"},"finish_reason":null}]}\n\n'));
|
||||
controller.enqueue(encoder.encode('data: {"id":"chatcmpl-1","model":"claude-opus-4-6","choices":[{"delta":{"content":"hello"},"finish_reason":null}]}\n\n'));
|
||||
controller.enqueue(encoder.encode('data: {"id":"chatcmpl-1","model":"claude-opus-4-6","choices":[{"delta":{},"finish_reason":"stop"}]}\n\n'));
|
||||
controller.enqueue(encoder.encode('data: [DONE]\n\n'));
|
||||
controller.close();
|
||||
},
|
||||
});
|
||||
|
||||
fetchMock.mockResolvedValue(new Response(upstreamBody, {
|
||||
status: 200,
|
||||
headers: { 'content-type': 'text/event-stream; charset=utf-8' },
|
||||
}));
|
||||
|
||||
const response = await app.inject({
|
||||
method: 'POST',
|
||||
url: '/v1/messages',
|
||||
payload: {
|
||||
model: 'claude-opus-4-6',
|
||||
stream: true,
|
||||
max_tokens: 256,
|
||||
messages: [{ role: 'user', content: 'hello' }],
|
||||
},
|
||||
});
|
||||
|
||||
expect(response.statusCode).toBe(200);
|
||||
expect(response.headers['content-type']).toContain('text/event-stream');
|
||||
expect(response.body).toContain('event: message_start');
|
||||
expect(response.body).toContain('event: content_block_delta');
|
||||
expect(response.body).toContain('\"text\":\"hello\"');
|
||||
expect(response.body).toContain('event: message_stop');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,468 @@
|
||||
import { FastifyInstance, FastifyRequest, FastifyReply } from 'fastify';
|
||||
import { tokenRouter } from '../../services/tokenRouter.js';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { fetch } from 'undici';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { reportProxyAllFailed, reportTokenExpired } from '../../services/alertService.js';
|
||||
import { isTokenExpiredError } from '../../services/alertRules.js';
|
||||
import { estimateProxyCost } from '../../services/modelPricingService.js';
|
||||
import { shouldRetryProxyRequest } from '../../services/proxyRetryPolicy.js';
|
||||
import { resolveProxyUsageWithSelfLogFallback } from '../../services/proxyUsageFallbackService.js';
|
||||
import { mergeProxyUsage, parseProxyUsage } from '../../services/proxyUsageParser.js';
|
||||
import {
|
||||
type DownstreamFormat,
|
||||
createStreamTransformContext,
|
||||
createClaudeDownstreamContext,
|
||||
parseDownstreamChatRequest,
|
||||
pullSseEventsWithDone,
|
||||
normalizeUpstreamStreamEvent,
|
||||
serializeNormalizedStreamEvent,
|
||||
serializeStreamDone,
|
||||
normalizeUpstreamFinalResponse,
|
||||
serializeFinalResponse,
|
||||
buildSyntheticOpenAiChunks,
|
||||
} from './chatFormats.js';
|
||||
|
||||
const MAX_RETRIES = 2;
|
||||
|
||||
export async function chatProxyRoute(app: FastifyInstance) {
|
||||
app.post('/v1/chat/completions', async (request: FastifyRequest, reply: FastifyReply) =>
|
||||
handleChatProxyRequest(request, reply, 'openai'));
|
||||
}
|
||||
|
||||
export async function claudeMessagesProxyRoute(app: FastifyInstance) {
|
||||
app.post('/v1/messages', async (request: FastifyRequest, reply: FastifyReply) =>
|
||||
handleChatProxyRequest(request, reply, 'claude'));
|
||||
}
|
||||
|
||||
async function handleChatProxyRequest(
|
||||
request: FastifyRequest,
|
||||
reply: FastifyReply,
|
||||
downstreamFormat: DownstreamFormat,
|
||||
) {
|
||||
const parsedRequest = parseDownstreamChatRequest(request.body, downstreamFormat);
|
||||
if (parsedRequest.error) {
|
||||
return reply.code(parsedRequest.error.statusCode).send(parsedRequest.error.payload);
|
||||
}
|
||||
|
||||
const { requestedModel, isStream, upstreamBody } = parsedRequest.value!;
|
||||
|
||||
const excludeChannelIds: number[] = [];
|
||||
let retryCount = 0;
|
||||
|
||||
while (retryCount <= MAX_RETRIES) {
|
||||
let selected = retryCount === 0
|
||||
? tokenRouter.selectChannel(requestedModel)
|
||||
: tokenRouter.selectNextChannel(requestedModel, excludeChannelIds);
|
||||
|
||||
if (!selected && retryCount === 0) {
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
selected = tokenRouter.selectChannel(requestedModel);
|
||||
}
|
||||
|
||||
if (!selected) {
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: 'No available channels after retries',
|
||||
});
|
||||
return reply.code(503).send({
|
||||
error: { message: 'No available channels for this model', type: 'server_error' },
|
||||
});
|
||||
}
|
||||
|
||||
excludeChannelIds.push(selected.channel.id);
|
||||
|
||||
const targetUrl = `${selected.site.url}/v1/chat/completions`;
|
||||
const forwardBody = {
|
||||
...upstreamBody,
|
||||
model: selected.actualModel,
|
||||
stream: isStream,
|
||||
};
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
Authorization: `Bearer ${selected.tokenValue}`,
|
||||
},
|
||||
body: JSON.stringify(forwardBody),
|
||||
});
|
||||
|
||||
if (!upstream.ok) {
|
||||
const errText = await upstream.text().catch(() => 'unknown error');
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', upstream.status, Date.now() - startTime, errText, retryCount);
|
||||
|
||||
if (isTokenExpiredError({ status: upstream.status, message: errText })) {
|
||||
await reportTokenExpired({
|
||||
accountId: selected.account.id,
|
||||
username: selected.account.username,
|
||||
siteName: selected.site.name,
|
||||
detail: `HTTP ${upstream.status}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (shouldRetryProxyRequest(upstream.status, errText) && retryCount < MAX_RETRIES) {
|
||||
retryCount += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: `upstream returned HTTP ${upstream.status}`,
|
||||
});
|
||||
|
||||
return reply.code(upstream.status).send({
|
||||
error: { message: errText, type: 'upstream_error' },
|
||||
});
|
||||
}
|
||||
|
||||
const modelName = selected.actualModel || requestedModel;
|
||||
|
||||
if (isStream) {
|
||||
reply.hijack();
|
||||
reply.raw.statusCode = 200;
|
||||
reply.raw.setHeader('Content-Type', 'text/event-stream; charset=utf-8');
|
||||
reply.raw.setHeader('Cache-Control', 'no-cache, no-transform');
|
||||
reply.raw.setHeader('Connection', 'keep-alive');
|
||||
reply.raw.setHeader('X-Accel-Buffering', 'no');
|
||||
|
||||
const streamContext = createStreamTransformContext(modelName);
|
||||
const claudeContext = createClaudeDownstreamContext();
|
||||
let parsedUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
||||
|
||||
const writeLines = (lines: string[]) => {
|
||||
for (const line of lines) {
|
||||
reply.raw.write(line);
|
||||
}
|
||||
};
|
||||
|
||||
const writeDone = () => {
|
||||
writeLines(serializeStreamDone(downstreamFormat, streamContext, claudeContext));
|
||||
};
|
||||
|
||||
const emitNormalizedFinalAsStream = (upstreamData: unknown, fallbackText = '') => {
|
||||
const normalizedFinal = normalizeUpstreamFinalResponse(upstreamData, modelName, fallbackText);
|
||||
streamContext.id = normalizedFinal.id;
|
||||
streamContext.model = normalizedFinal.model;
|
||||
streamContext.created = normalizedFinal.created;
|
||||
|
||||
if (downstreamFormat === 'openai') {
|
||||
const syntheticChunks = buildSyntheticOpenAiChunks(normalizedFinal);
|
||||
for (const chunk of syntheticChunks) {
|
||||
reply.raw.write(`data: ${JSON.stringify(chunk)}\n\n`);
|
||||
}
|
||||
return;
|
||||
}
|
||||
|
||||
writeLines(serializeNormalizedStreamEvent('claude', { role: 'assistant' }, streamContext, claudeContext));
|
||||
|
||||
const combinedText = [normalizedFinal.reasoningContent, normalizedFinal.content]
|
||||
.filter((item) => typeof item === 'string' && item.trim().length > 0)
|
||||
.join('\n\n');
|
||||
|
||||
if (combinedText) {
|
||||
writeLines(serializeNormalizedStreamEvent('claude', {
|
||||
contentDelta: combinedText,
|
||||
}, streamContext, claudeContext));
|
||||
}
|
||||
|
||||
writeLines(serializeNormalizedStreamEvent('claude', {
|
||||
finishReason: normalizedFinal.finishReason,
|
||||
}, streamContext, claudeContext));
|
||||
};
|
||||
|
||||
const upstreamContentType = (upstream.headers.get('content-type') || '').toLowerCase();
|
||||
if (!upstreamContentType.includes('text/event-stream')) {
|
||||
const fallbackText = await upstream.text();
|
||||
let fallbackData: unknown = null;
|
||||
try {
|
||||
fallbackData = JSON.parse(fallbackText);
|
||||
} catch {
|
||||
fallbackData = fallbackText;
|
||||
}
|
||||
|
||||
parsedUsage = mergeProxyUsage(parsedUsage, parseProxyUsage(fallbackData));
|
||||
emitNormalizedFinalAsStream(fallbackData, fallbackText);
|
||||
writeDone();
|
||||
reply.raw.end();
|
||||
|
||||
const latency = Date.now() - startTime;
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected,
|
||||
requestedModel,
|
||||
'success',
|
||||
200,
|
||||
latency,
|
||||
null,
|
||||
retryCount,
|
||||
resolvedUsage.promptTokens,
|
||||
resolvedUsage.completionTokens,
|
||||
resolvedUsage.totalTokens,
|
||||
estimatedCost,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const reader = upstream.body?.getReader();
|
||||
if (!reader) {
|
||||
writeDone();
|
||||
reply.raw.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let sseBuffer = '';
|
||||
|
||||
const consumeSseBuffer = (incoming: string): string => {
|
||||
const pulled = pullSseEventsWithDone(incoming);
|
||||
for (const eventBlock of pulled.events) {
|
||||
if (eventBlock.data === '[DONE]') {
|
||||
writeDone();
|
||||
continue;
|
||||
}
|
||||
|
||||
let parsedPayload: unknown = null;
|
||||
try {
|
||||
parsedPayload = JSON.parse(eventBlock.data);
|
||||
} catch {
|
||||
parsedPayload = null;
|
||||
}
|
||||
|
||||
if (parsedPayload && typeof parsedPayload === 'object') {
|
||||
parsedUsage = mergeProxyUsage(parsedUsage, parseProxyUsage(parsedPayload));
|
||||
const normalizedEvent = normalizeUpstreamStreamEvent(parsedPayload, streamContext, modelName);
|
||||
writeLines(serializeNormalizedStreamEvent(
|
||||
downstreamFormat,
|
||||
normalizedEvent,
|
||||
streamContext,
|
||||
claudeContext,
|
||||
));
|
||||
continue;
|
||||
}
|
||||
|
||||
if (downstreamFormat === 'openai') {
|
||||
reply.raw.write(`data: ${eventBlock.data}\n\n`);
|
||||
} else {
|
||||
writeLines(serializeNormalizedStreamEvent('claude', {
|
||||
contentDelta: eventBlock.data,
|
||||
}, streamContext, claudeContext));
|
||||
}
|
||||
}
|
||||
|
||||
return pulled.rest;
|
||||
};
|
||||
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
if (!value) continue;
|
||||
|
||||
sseBuffer += decoder.decode(value, { stream: true });
|
||||
sseBuffer = consumeSseBuffer(sseBuffer);
|
||||
}
|
||||
|
||||
sseBuffer += decoder.decode();
|
||||
if (sseBuffer.trim().length > 0) {
|
||||
sseBuffer = consumeSseBuffer(`${sseBuffer}\n\n`);
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
writeDone();
|
||||
reply.raw.end();
|
||||
}
|
||||
|
||||
const latency = Date.now() - startTime;
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected,
|
||||
requestedModel,
|
||||
'success',
|
||||
200,
|
||||
latency,
|
||||
null,
|
||||
retryCount,
|
||||
resolvedUsage.promptTokens,
|
||||
resolvedUsage.completionTokens,
|
||||
resolvedUsage.totalTokens,
|
||||
estimatedCost,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const rawText = await upstream.text();
|
||||
let upstreamData: unknown = rawText;
|
||||
try {
|
||||
upstreamData = JSON.parse(rawText);
|
||||
} catch {
|
||||
upstreamData = rawText;
|
||||
}
|
||||
|
||||
const latency = Date.now() - startTime;
|
||||
const parsedUsage = parseProxyUsage(upstreamData);
|
||||
const normalizedFinal = normalizeUpstreamFinalResponse(upstreamData, modelName, rawText);
|
||||
const downstreamResponse = serializeFinalResponse(downstreamFormat, normalizedFinal, parsedUsage);
|
||||
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected,
|
||||
requestedModel,
|
||||
'success',
|
||||
200,
|
||||
latency,
|
||||
null,
|
||||
retryCount,
|
||||
resolvedUsage.promptTokens,
|
||||
resolvedUsage.completionTokens,
|
||||
resolvedUsage.totalTokens,
|
||||
estimatedCost,
|
||||
);
|
||||
|
||||
return reply.send(downstreamResponse);
|
||||
} catch (err: any) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', 0, Date.now() - startTime, err?.message || 'network error', retryCount);
|
||||
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
retryCount += 1;
|
||||
continue;
|
||||
}
|
||||
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: err?.message || 'network failure',
|
||||
});
|
||||
|
||||
return reply.code(502).send({
|
||||
error: {
|
||||
message: `Upstream error: ${err?.message || 'network failure'}`,
|
||||
type: 'upstream_error',
|
||||
},
|
||||
});
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function logProxy(
|
||||
selected: any,
|
||||
modelRequested: string,
|
||||
status: string,
|
||||
httpStatus: number,
|
||||
latencyMs: number,
|
||||
errorMessage: string | null,
|
||||
retryCount: number,
|
||||
promptTokens = 0,
|
||||
completionTokens = 0,
|
||||
totalTokens = 0,
|
||||
estimatedCost = 0,
|
||||
) {
|
||||
try {
|
||||
db.insert(schema.proxyLogs).values({
|
||||
routeId: selected.channel.routeId,
|
||||
channelId: selected.channel.id,
|
||||
accountId: selected.account.id,
|
||||
modelRequested,
|
||||
modelActual: selected.actualModel,
|
||||
status,
|
||||
httpStatus,
|
||||
latencyMs,
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens,
|
||||
estimatedCost,
|
||||
errorMessage,
|
||||
retryCount,
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
@@ -0,0 +1,846 @@
|
||||
export type DownstreamFormat = 'openai' | 'claude';
|
||||
|
||||
export type ParsedSseEvent = {
|
||||
event: string;
|
||||
data: string;
|
||||
};
|
||||
|
||||
export type StreamTransformContext = {
|
||||
id: string;
|
||||
model: string;
|
||||
created: number;
|
||||
roleSent: boolean;
|
||||
doneSent: boolean;
|
||||
};
|
||||
|
||||
export type ClaudeDownstreamContext = {
|
||||
messageStarted: boolean;
|
||||
contentBlockStarted: boolean;
|
||||
doneSent: boolean;
|
||||
};
|
||||
|
||||
export type NormalizedStreamEvent = {
|
||||
role?: 'assistant';
|
||||
contentDelta?: string;
|
||||
reasoningDelta?: string;
|
||||
finishReason?: string | null;
|
||||
done?: boolean;
|
||||
};
|
||||
|
||||
export type NormalizedFinalResponse = {
|
||||
id: string;
|
||||
model: string;
|
||||
created: number;
|
||||
content: string;
|
||||
reasoningContent: string;
|
||||
finishReason: string;
|
||||
};
|
||||
|
||||
export type ParsedDownstreamChatRequest = {
|
||||
requestedModel: string;
|
||||
isStream: boolean;
|
||||
upstreamBody: Record<string, unknown>;
|
||||
};
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === 'object';
|
||||
}
|
||||
|
||||
function isNonEmptyString(value: unknown): value is string {
|
||||
return typeof value === 'string' && value.trim().length > 0;
|
||||
}
|
||||
|
||||
function pickFiniteNumber(value: unknown): number | undefined {
|
||||
return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
|
||||
}
|
||||
|
||||
function ensureIntegerTimestamp(value: unknown, fallback: number): number {
|
||||
const n = Number(value);
|
||||
if (!Number.isFinite(n) || n <= 0) return fallback;
|
||||
return Math.trunc(n);
|
||||
}
|
||||
|
||||
function joinNonEmpty(parts: string[]): string {
|
||||
return parts.map((item) => item.trim()).filter((item) => item.length > 0).join('\n\n');
|
||||
}
|
||||
|
||||
function textFromPart(part: unknown): string {
|
||||
if (typeof part === 'string') return part;
|
||||
if (!isRecord(part)) return '';
|
||||
|
||||
if (typeof part.text === 'string') return part.text;
|
||||
if (typeof part.content === 'string') return part.content;
|
||||
if (typeof part.output_text === 'string') return part.output_text;
|
||||
if (typeof part.completion === 'string') return part.completion;
|
||||
if (typeof part.partial_json === 'string') return part.partial_json;
|
||||
if (typeof part.reasoning_content === 'string') return part.reasoning_content;
|
||||
if (typeof part.reasoning === 'string') return part.reasoning;
|
||||
|
||||
if (Array.isArray(part.content)) {
|
||||
return part.content.map((item) => textFromPart(item)).join('');
|
||||
}
|
||||
|
||||
if (isRecord(part.delta)) {
|
||||
const fromDelta = textFromPart(part.delta);
|
||||
if (fromDelta) return fromDelta;
|
||||
}
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function extractTextAndReasoning(value: unknown): { content: string; reasoning: string } {
|
||||
if (typeof value === 'string') return { content: value, reasoning: '' };
|
||||
if (Array.isArray(value)) {
|
||||
const contentParts: string[] = [];
|
||||
const reasoningParts: string[] = [];
|
||||
for (const item of value) {
|
||||
if (typeof item === 'string') {
|
||||
contentParts.push(item);
|
||||
continue;
|
||||
}
|
||||
if (!isRecord(item)) continue;
|
||||
const type = typeof item.type === 'string' ? item.type : '';
|
||||
|
||||
if (type === 'thinking' && typeof item.thinking === 'string') {
|
||||
reasoningParts.push(item.thinking);
|
||||
continue;
|
||||
}
|
||||
if (type === 'thinking_delta' && typeof item.text === 'string') {
|
||||
reasoningParts.push(item.text);
|
||||
continue;
|
||||
}
|
||||
if (typeof item.thought === 'boolean' && item.thought && typeof item.text === 'string') {
|
||||
reasoningParts.push(item.text);
|
||||
continue;
|
||||
}
|
||||
|
||||
const text = textFromPart(item);
|
||||
if (text) contentParts.push(text);
|
||||
}
|
||||
|
||||
return {
|
||||
content: contentParts.join(''),
|
||||
reasoning: reasoningParts.join(''),
|
||||
};
|
||||
}
|
||||
|
||||
if (!isRecord(value)) return { content: '', reasoning: '' };
|
||||
|
||||
if (Array.isArray(value.parts)) {
|
||||
return extractTextAndReasoning(value.parts);
|
||||
}
|
||||
|
||||
return {
|
||||
content: textFromPart(value),
|
||||
reasoning: '',
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeStopReason(raw: unknown): string | null {
|
||||
const value = typeof raw === 'string' ? raw.trim().toLowerCase() : '';
|
||||
if (!value) return null;
|
||||
|
||||
if (
|
||||
value === 'end_turn'
|
||||
|| value === 'stop'
|
||||
|| value === 'end'
|
||||
|| value === 'eos'
|
||||
|| value === 'finished'
|
||||
|| value === 'stop_sequence'
|
||||
) {
|
||||
return 'stop';
|
||||
}
|
||||
|
||||
if (
|
||||
value === 'max_tokens'
|
||||
|| value === 'length'
|
||||
|| value === 'max_output_tokens'
|
||||
|| value === 'max_tokens_exceeded'
|
||||
|| value.includes('max')
|
||||
) {
|
||||
return 'length';
|
||||
}
|
||||
|
||||
if (value === 'tool_use' || value === 'tool_calls' || value.includes('tool')) {
|
||||
return 'tool_calls';
|
||||
}
|
||||
|
||||
return 'stop';
|
||||
}
|
||||
|
||||
export function toClaudeStopReason(finishReason: string | null | undefined): string {
|
||||
const value = normalizeStopReason(finishReason);
|
||||
if (value === 'length') return 'max_tokens';
|
||||
if (value === 'tool_calls') return 'tool_use';
|
||||
return 'end_turn';
|
||||
}
|
||||
|
||||
export function createStreamTransformContext(modelName: string): StreamTransformContext {
|
||||
return {
|
||||
id: `chatcmpl-meta-${Date.now()}`,
|
||||
model: modelName,
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
roleSent: false,
|
||||
doneSent: false,
|
||||
};
|
||||
}
|
||||
|
||||
export function createClaudeDownstreamContext(): ClaudeDownstreamContext {
|
||||
return {
|
||||
messageStarted: false,
|
||||
contentBlockStarted: false,
|
||||
doneSent: false,
|
||||
};
|
||||
}
|
||||
|
||||
function buildClaudeMessageId(sourceId: string): string {
|
||||
if (sourceId.startsWith('msg_')) return sourceId;
|
||||
const sanitized = sourceId.replace(/[^A-Za-z0-9_-]/g, '_');
|
||||
return `msg_${sanitized || Date.now()}`;
|
||||
}
|
||||
|
||||
function serializeSse(event: string, data: unknown): string {
|
||||
const payload = typeof data === 'string' ? data : JSON.stringify(data);
|
||||
if (event) {
|
||||
return `event: ${event}\ndata: ${payload}\n\n`;
|
||||
}
|
||||
return `data: ${payload}\n\n`;
|
||||
}
|
||||
|
||||
function extractAssistantContent(choice: any): string {
|
||||
const messageContent = choice?.message?.content;
|
||||
const parsedMessage = extractTextAndReasoning(messageContent).content;
|
||||
if (parsedMessage) return parsedMessage;
|
||||
|
||||
const content = extractTextAndReasoning(choice?.content).content;
|
||||
if (content) return content;
|
||||
|
||||
if (typeof choice?.text === 'string' && choice.text.length > 0) return choice.text;
|
||||
if (typeof choice?.completion === 'string' && choice.completion.length > 0) return choice.completion;
|
||||
if (typeof choice?.output_text === 'string' && choice.output_text.length > 0) return choice.output_text;
|
||||
if (typeof choice?.delta?.content === 'string' && choice.delta.content.length > 0) return choice.delta.content;
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function extractAssistantReasoning(choice: any): string {
|
||||
const message = choice?.message || {};
|
||||
const direct = [
|
||||
message.reasoning_content,
|
||||
message.reasoning,
|
||||
choice?.reasoning_content,
|
||||
choice?.reasoning,
|
||||
].find((item) => typeof item === 'string' && item.length > 0);
|
||||
|
||||
if (typeof direct === 'string') return direct;
|
||||
|
||||
const parsed = extractTextAndReasoning(message.content);
|
||||
if (parsed.reasoning) return parsed.reasoning;
|
||||
|
||||
const nested = extractTextAndReasoning(choice?.content).reasoning;
|
||||
if (nested) return nested;
|
||||
|
||||
return '';
|
||||
}
|
||||
|
||||
function parseClaudeMessageContent(content: unknown): string {
|
||||
return extractTextAndReasoning(content).content;
|
||||
}
|
||||
|
||||
function convertClaudeRequestToOpenAiBody(body: Record<string, unknown>): {
|
||||
model: string;
|
||||
stream: boolean;
|
||||
messages: Array<{ role: string; content: string }>;
|
||||
payload: Record<string, unknown>;
|
||||
} {
|
||||
const model = typeof body.model === 'string' ? body.model.trim() : '';
|
||||
const stream = body.stream === true;
|
||||
|
||||
const messages: Array<{ role: string; content: string }> = [];
|
||||
|
||||
const appendMessage = (role: string, content: unknown) => {
|
||||
const text = parseClaudeMessageContent(content);
|
||||
if (!text) return;
|
||||
messages.push({ role, content: text });
|
||||
};
|
||||
|
||||
const system = body.system;
|
||||
if (typeof system === 'string') {
|
||||
appendMessage('system', system);
|
||||
} else if (Array.isArray(system)) {
|
||||
const merged = system.map((item) => parseClaudeMessageContent(item)).filter((item) => item.length > 0).join('\n\n');
|
||||
if (merged) appendMessage('system', merged);
|
||||
}
|
||||
|
||||
const rawMessages = Array.isArray(body.messages) ? body.messages : [];
|
||||
for (const message of rawMessages) {
|
||||
if (!isRecord(message)) continue;
|
||||
const role = typeof message.role === 'string' ? message.role : 'user';
|
||||
const mappedRole = role === 'assistant' || role === 'system' ? role : 'user';
|
||||
appendMessage(mappedRole, message.content);
|
||||
}
|
||||
|
||||
const payload: Record<string, unknown> = {
|
||||
model,
|
||||
stream,
|
||||
messages,
|
||||
};
|
||||
|
||||
const temperature = pickFiniteNumber(body.temperature);
|
||||
if (temperature !== undefined) payload.temperature = temperature;
|
||||
|
||||
const topP = pickFiniteNumber(body.top_p);
|
||||
if (topP !== undefined) payload.top_p = topP;
|
||||
|
||||
const maxTokens = pickFiniteNumber(body.max_tokens);
|
||||
if (maxTokens !== undefined) {
|
||||
payload.max_tokens = maxTokens;
|
||||
} else {
|
||||
payload.max_tokens = 4096;
|
||||
}
|
||||
|
||||
if (Array.isArray(body.stop_sequences) && body.stop_sequences.length > 0) {
|
||||
payload.stop = body.stop_sequences;
|
||||
}
|
||||
|
||||
if (body.tools !== undefined) payload.tools = body.tools;
|
||||
if (body.tool_choice !== undefined) payload.tool_choice = body.tool_choice;
|
||||
|
||||
return { model, stream, messages, payload };
|
||||
}
|
||||
|
||||
export function parseDownstreamChatRequest(
|
||||
body: unknown,
|
||||
format: DownstreamFormat,
|
||||
): { value?: ParsedDownstreamChatRequest; error?: { statusCode: number; payload: unknown } } {
|
||||
const raw = isRecord(body) ? body : {};
|
||||
|
||||
if (format === 'claude') {
|
||||
const converted = convertClaudeRequestToOpenAiBody(raw);
|
||||
if (!converted.model) {
|
||||
return {
|
||||
error: {
|
||||
statusCode: 400,
|
||||
payload: { error: { message: 'model is required', type: 'invalid_request_error' } },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
if (converted.messages.length <= 0) {
|
||||
return {
|
||||
error: {
|
||||
statusCode: 400,
|
||||
payload: { error: { message: 'messages is required', type: 'invalid_request_error' } },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
value: {
|
||||
requestedModel: converted.model,
|
||||
isStream: converted.stream,
|
||||
upstreamBody: converted.payload,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const model = typeof raw.model === 'string' ? raw.model.trim() : '';
|
||||
if (!model) {
|
||||
return {
|
||||
error: {
|
||||
statusCode: 400,
|
||||
payload: { error: { message: 'model is required', type: 'invalid_request_error' } },
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
value: {
|
||||
requestedModel: model,
|
||||
isStream: raw.stream === true,
|
||||
upstreamBody: raw,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeUpstreamFinalResponse(
|
||||
payload: unknown,
|
||||
fallbackModel: string,
|
||||
fallbackText = '',
|
||||
): NormalizedFinalResponse {
|
||||
const now = Math.floor(Date.now() / 1000);
|
||||
const fallbackId = `chatcmpl-meta-${Date.now()}`;
|
||||
|
||||
if (isRecord(payload) && Array.isArray(payload.choices)) {
|
||||
const choice = payload.choices[0] ?? {};
|
||||
const content = extractAssistantContent(choice) || extractAssistantContent(payload);
|
||||
const reasoning = extractAssistantReasoning(choice) || extractAssistantReasoning(payload);
|
||||
return {
|
||||
id: isNonEmptyString(payload.id) ? payload.id : fallbackId,
|
||||
model: isNonEmptyString(payload.model) ? payload.model : fallbackModel,
|
||||
created: ensureIntegerTimestamp(payload.created, now),
|
||||
content: content || fallbackText,
|
||||
reasoningContent: reasoning,
|
||||
finishReason: normalizeStopReason(choice?.finish_reason ?? payload.stop_reason) || 'stop',
|
||||
};
|
||||
}
|
||||
|
||||
if (isRecord(payload) && typeof payload.type === 'string' && payload.type === 'message') {
|
||||
return {
|
||||
id: isNonEmptyString(payload.id) ? payload.id : fallbackId,
|
||||
model: isNonEmptyString(payload.model) ? payload.model : fallbackModel,
|
||||
created: now,
|
||||
content: parseClaudeMessageContent(payload.content) || fallbackText,
|
||||
reasoningContent: extractTextAndReasoning(payload.content).reasoning,
|
||||
finishReason: normalizeStopReason(payload.stop_reason) || 'stop',
|
||||
};
|
||||
}
|
||||
|
||||
if (isRecord(payload) && Array.isArray(payload.candidates)) {
|
||||
const candidate = payload.candidates[0] || {};
|
||||
const parsedCandidate = extractTextAndReasoning(candidate?.content?.parts || candidate?.content);
|
||||
return {
|
||||
id: isNonEmptyString((payload as any).responseId) ? (payload as any).responseId : fallbackId,
|
||||
model: isNonEmptyString((payload as any).modelVersion)
|
||||
? (payload as any).modelVersion
|
||||
: fallbackModel,
|
||||
created: now,
|
||||
content: parsedCandidate.content || fallbackText,
|
||||
reasoningContent: parsedCandidate.reasoning,
|
||||
finishReason: normalizeStopReason(candidate?.finishReason || (payload as any).finishReason) || 'stop',
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof payload === 'string' && payload.trim()) {
|
||||
return {
|
||||
id: fallbackId,
|
||||
model: fallbackModel,
|
||||
created: now,
|
||||
content: payload,
|
||||
reasoningContent: '',
|
||||
finishReason: 'stop',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
id: fallbackId,
|
||||
model: fallbackModel,
|
||||
created: now,
|
||||
content: fallbackText,
|
||||
reasoningContent: '',
|
||||
finishReason: 'stop',
|
||||
};
|
||||
}
|
||||
|
||||
export function normalizeUpstreamStreamEvent(
|
||||
payload: unknown,
|
||||
context: StreamTransformContext,
|
||||
fallbackModel: string,
|
||||
): NormalizedStreamEvent {
|
||||
if (!isRecord(payload)) return {};
|
||||
|
||||
if (Array.isArray(payload.choices)) {
|
||||
if (isNonEmptyString(payload.id)) context.id = payload.id;
|
||||
if (isNonEmptyString(payload.model)) context.model = payload.model;
|
||||
context.created = ensureIntegerTimestamp(payload.created, context.created);
|
||||
|
||||
const choice = payload.choices[0] ?? {};
|
||||
const delta = isRecord(choice?.delta) ? choice.delta : {};
|
||||
const deltaParsed = extractTextAndReasoning(delta.content ?? delta);
|
||||
|
||||
const contentDelta =
|
||||
deltaParsed.content
|
||||
|| (typeof choice?.message?.content === 'string' ? choice.message.content : '')
|
||||
|| '';
|
||||
|
||||
const reasoningDelta =
|
||||
(typeof (delta as any).reasoning_content === 'string' ? (delta as any).reasoning_content : '')
|
||||
|| (typeof (delta as any).reasoning === 'string' ? (delta as any).reasoning : '')
|
||||
|| deltaParsed.reasoning
|
||||
|| '';
|
||||
|
||||
return {
|
||||
role: (delta as any).role === 'assistant' ? 'assistant' : undefined,
|
||||
contentDelta: contentDelta || undefined,
|
||||
reasoningDelta: reasoningDelta || undefined,
|
||||
finishReason: normalizeStopReason(choice?.finish_reason),
|
||||
};
|
||||
}
|
||||
|
||||
const type = typeof payload.type === 'string' ? payload.type : '';
|
||||
const message = isRecord(payload.message) ? payload.message : null;
|
||||
|
||||
if (message) {
|
||||
if (isNonEmptyString(message.id)) context.id = message.id;
|
||||
if (isNonEmptyString(message.model)) context.model = message.model;
|
||||
}
|
||||
if (!context.model) context.model = fallbackModel;
|
||||
|
||||
if (type === 'message_start') {
|
||||
return { role: 'assistant' };
|
||||
}
|
||||
|
||||
if (type === 'content_block_start') {
|
||||
const parsed = extractTextAndReasoning(payload.content_block);
|
||||
return {
|
||||
contentDelta: parsed.content || undefined,
|
||||
reasoningDelta: parsed.reasoning || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'content_block_delta') {
|
||||
const delta = isRecord(payload.delta) ? payload.delta : {};
|
||||
const deltaType = typeof delta.type === 'string' ? delta.type : '';
|
||||
const parsed = extractTextAndReasoning(delta);
|
||||
|
||||
if (deltaType === 'thinking_delta') {
|
||||
return {
|
||||
reasoningDelta: parsed.content || parsed.reasoning || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
contentDelta: parsed.content || undefined,
|
||||
reasoningDelta: parsed.reasoning || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'message_delta') {
|
||||
const delta = isRecord(payload.delta) ? payload.delta : {};
|
||||
return {
|
||||
finishReason: normalizeStopReason(delta.stop_reason ?? payload.stop_reason),
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'message_stop') {
|
||||
return { done: true };
|
||||
}
|
||||
|
||||
if (Array.isArray(payload.candidates)) {
|
||||
const candidate = payload.candidates[0] || {};
|
||||
const parsed = extractTextAndReasoning((candidate as any).content?.parts || (candidate as any).content);
|
||||
|
||||
if (isNonEmptyString((payload as any).modelVersion)) {
|
||||
context.model = (payload as any).modelVersion;
|
||||
} else if (!context.model) {
|
||||
context.model = fallbackModel;
|
||||
}
|
||||
|
||||
return {
|
||||
contentDelta: parsed.content || undefined,
|
||||
reasoningDelta: parsed.reasoning || undefined,
|
||||
finishReason: normalizeStopReason((candidate as any).finishReason || (payload as any).finishReason),
|
||||
};
|
||||
}
|
||||
|
||||
const fallback = extractTextAndReasoning(payload);
|
||||
return {
|
||||
contentDelta: fallback.content || undefined,
|
||||
reasoningDelta: fallback.reasoning || undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function buildOpenAiStreamChunk(
|
||||
context: StreamTransformContext,
|
||||
event: NormalizedStreamEvent,
|
||||
): Record<string, unknown> | null {
|
||||
const delta: Record<string, unknown> = {};
|
||||
const isInitialAssistantRoleOnlyEvent = (
|
||||
!context.roleSent
|
||||
&& event.role === 'assistant'
|
||||
&& !event.contentDelta
|
||||
&& !event.reasoningDelta
|
||||
);
|
||||
|
||||
if (!context.roleSent && (event.role === 'assistant' || event.contentDelta || event.reasoningDelta)) {
|
||||
delta.role = 'assistant';
|
||||
context.roleSent = true;
|
||||
} else if (event.role === 'assistant') {
|
||||
delta.role = 'assistant';
|
||||
context.roleSent = true;
|
||||
}
|
||||
|
||||
if (event.contentDelta) {
|
||||
delta.content = event.contentDelta;
|
||||
}
|
||||
|
||||
if (event.reasoningDelta) {
|
||||
delta.reasoning_content = event.reasoningDelta;
|
||||
}
|
||||
|
||||
// Some OpenAI-compatible clients (e.g. OpenWebUI) expect starter chunk to include empty content.
|
||||
if (isInitialAssistantRoleOnlyEvent) {
|
||||
delta.content = '';
|
||||
}
|
||||
|
||||
const finishReason = event.finishReason || null;
|
||||
const hasDelta = Object.keys(delta).length > 0;
|
||||
if (!hasDelta && !finishReason) return null;
|
||||
|
||||
return {
|
||||
id: context.id,
|
||||
object: 'chat.completion.chunk',
|
||||
created: context.created,
|
||||
model: context.model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta,
|
||||
finish_reason: finishReason,
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
function ensureClaudeStartEvents(
|
||||
context: StreamTransformContext,
|
||||
claudeContext: ClaudeDownstreamContext,
|
||||
): string[] {
|
||||
if (claudeContext.messageStarted) return [];
|
||||
|
||||
claudeContext.messageStarted = true;
|
||||
const payload = {
|
||||
type: 'message_start',
|
||||
message: {
|
||||
id: buildClaudeMessageId(context.id),
|
||||
type: 'message',
|
||||
role: 'assistant',
|
||||
model: context.model,
|
||||
content: [],
|
||||
stop_reason: null,
|
||||
stop_sequence: null,
|
||||
usage: {
|
||||
input_tokens: 0,
|
||||
output_tokens: 0,
|
||||
},
|
||||
},
|
||||
};
|
||||
|
||||
return [serializeSse('message_start', payload)];
|
||||
}
|
||||
|
||||
function ensureClaudeContentBlockStart(
|
||||
claudeContext: ClaudeDownstreamContext,
|
||||
): string[] {
|
||||
if (claudeContext.contentBlockStarted) return [];
|
||||
claudeContext.contentBlockStarted = true;
|
||||
|
||||
return [serializeSse('content_block_start', {
|
||||
type: 'content_block_start',
|
||||
index: 0,
|
||||
content_block: {
|
||||
type: 'text',
|
||||
text: '',
|
||||
},
|
||||
})];
|
||||
}
|
||||
|
||||
function buildClaudeDoneEvents(
|
||||
context: StreamTransformContext,
|
||||
claudeContext: ClaudeDownstreamContext,
|
||||
finishReason?: string | null,
|
||||
): string[] {
|
||||
if (claudeContext.doneSent) return [];
|
||||
|
||||
const events: string[] = [];
|
||||
events.push(...ensureClaudeStartEvents(context, claudeContext));
|
||||
|
||||
if (claudeContext.contentBlockStarted) {
|
||||
events.push(serializeSse('content_block_stop', {
|
||||
type: 'content_block_stop',
|
||||
index: 0,
|
||||
}));
|
||||
claudeContext.contentBlockStarted = false;
|
||||
}
|
||||
|
||||
events.push(serializeSse('message_delta', {
|
||||
type: 'message_delta',
|
||||
delta: {
|
||||
stop_reason: toClaudeStopReason(finishReason),
|
||||
stop_sequence: null,
|
||||
},
|
||||
usage: {
|
||||
output_tokens: 0,
|
||||
},
|
||||
}));
|
||||
events.push(serializeSse('message_stop', { type: 'message_stop' }));
|
||||
|
||||
claudeContext.doneSent = true;
|
||||
return events;
|
||||
}
|
||||
|
||||
export function serializeNormalizedStreamEvent(
|
||||
downstreamFormat: DownstreamFormat,
|
||||
event: NormalizedStreamEvent,
|
||||
context: StreamTransformContext,
|
||||
claudeContext: ClaudeDownstreamContext,
|
||||
): string[] {
|
||||
if (downstreamFormat === 'openai') {
|
||||
const chunk = buildOpenAiStreamChunk(context, event);
|
||||
return chunk ? [serializeSse('', chunk)] : [];
|
||||
}
|
||||
|
||||
if (event.done || event.finishReason) {
|
||||
return buildClaudeDoneEvents(context, claudeContext, event.finishReason);
|
||||
}
|
||||
|
||||
const events: string[] = [];
|
||||
if (event.role === 'assistant' || event.contentDelta || event.reasoningDelta) {
|
||||
events.push(...ensureClaudeStartEvents(context, claudeContext));
|
||||
}
|
||||
|
||||
const mergedText = joinNonEmpty([
|
||||
event.reasoningDelta || '',
|
||||
event.contentDelta || '',
|
||||
]);
|
||||
|
||||
if (mergedText) {
|
||||
events.push(...ensureClaudeContentBlockStart(claudeContext));
|
||||
events.push(serializeSse('content_block_delta', {
|
||||
type: 'content_block_delta',
|
||||
index: 0,
|
||||
delta: {
|
||||
type: 'text_delta',
|
||||
text: mergedText,
|
||||
},
|
||||
}));
|
||||
}
|
||||
|
||||
return events;
|
||||
}
|
||||
|
||||
export function serializeStreamDone(
|
||||
downstreamFormat: DownstreamFormat,
|
||||
context: StreamTransformContext,
|
||||
claudeContext: ClaudeDownstreamContext,
|
||||
): string[] {
|
||||
if (context.doneSent) return [];
|
||||
context.doneSent = true;
|
||||
|
||||
if (downstreamFormat === 'openai') {
|
||||
return [serializeSse('', '[DONE]')];
|
||||
}
|
||||
|
||||
return buildClaudeDoneEvents(context, claudeContext, 'stop');
|
||||
}
|
||||
|
||||
export function serializeFinalResponse(
|
||||
downstreamFormat: DownstreamFormat,
|
||||
normalized: NormalizedFinalResponse,
|
||||
usage: { promptTokens: number; completionTokens: number; totalTokens: number },
|
||||
): Record<string, unknown> {
|
||||
if (downstreamFormat === 'claude') {
|
||||
return {
|
||||
id: buildClaudeMessageId(normalized.id),
|
||||
type: 'message',
|
||||
role: 'assistant',
|
||||
model: normalized.model,
|
||||
content: [{
|
||||
type: 'text',
|
||||
text: normalized.content,
|
||||
}],
|
||||
stop_reason: toClaudeStopReason(normalized.finishReason),
|
||||
stop_sequence: null,
|
||||
usage: {
|
||||
input_tokens: usage.promptTokens,
|
||||
output_tokens: usage.completionTokens,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
const message: Record<string, unknown> = {
|
||||
role: 'assistant',
|
||||
content: normalized.content,
|
||||
};
|
||||
if (normalized.reasoningContent) {
|
||||
message.reasoning_content = normalized.reasoningContent;
|
||||
}
|
||||
|
||||
return {
|
||||
id: normalized.id,
|
||||
object: 'chat.completion',
|
||||
created: normalized.created,
|
||||
model: normalized.model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
message,
|
||||
finish_reason: normalizeStopReason(normalized.finishReason) || 'stop',
|
||||
}],
|
||||
usage: {
|
||||
prompt_tokens: usage.promptTokens,
|
||||
completion_tokens: usage.completionTokens,
|
||||
total_tokens: usage.totalTokens,
|
||||
},
|
||||
};
|
||||
}
|
||||
|
||||
export function buildSyntheticOpenAiChunks(normalized: NormalizedFinalResponse): Array<Record<string, unknown>> {
|
||||
const startChunk: Record<string, unknown> = {
|
||||
id: normalized.id,
|
||||
object: 'chat.completion.chunk',
|
||||
created: normalized.created,
|
||||
model: normalized.model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: normalized.content
|
||||
? { role: 'assistant', content: normalized.content }
|
||||
: { role: 'assistant' },
|
||||
finish_reason: null,
|
||||
}],
|
||||
};
|
||||
|
||||
if (normalized.reasoningContent) {
|
||||
(startChunk.choices as any[])[0].delta.reasoning_content = normalized.reasoningContent;
|
||||
}
|
||||
|
||||
const endChunk = {
|
||||
id: normalized.id,
|
||||
object: 'chat.completion.chunk',
|
||||
created: normalized.created,
|
||||
model: normalized.model,
|
||||
choices: [{
|
||||
index: 0,
|
||||
delta: {},
|
||||
finish_reason: normalizeStopReason(normalized.finishReason) || 'stop',
|
||||
}],
|
||||
};
|
||||
|
||||
return [startChunk, endChunk];
|
||||
}
|
||||
|
||||
export function pullSseEventsWithDone(buffer: string): { events: ParsedSseEvent[]; rest: string } {
|
||||
const normalized = buffer.replace(/\r\n/g, '\n');
|
||||
const events: ParsedSseEvent[] = [];
|
||||
let rest = normalized;
|
||||
|
||||
while (true) {
|
||||
const boundary = rest.indexOf('\n\n');
|
||||
if (boundary < 0) break;
|
||||
|
||||
const block = rest.slice(0, boundary);
|
||||
rest = rest.slice(boundary + 2);
|
||||
|
||||
if (!block.trim()) continue;
|
||||
|
||||
const lines = block.split('\n');
|
||||
let eventName = '';
|
||||
const dataLines: string[] = [];
|
||||
|
||||
for (const line of lines) {
|
||||
if (line.startsWith('event:')) {
|
||||
eventName = line.slice(6).trim();
|
||||
continue;
|
||||
}
|
||||
if (line.startsWith('data:')) {
|
||||
dataLines.push(line.slice(5).trimStart());
|
||||
}
|
||||
}
|
||||
|
||||
if (dataLines.length <= 0) continue;
|
||||
|
||||
events.push({
|
||||
event: eventName,
|
||||
data: dataLines.join('\n').trim(),
|
||||
});
|
||||
}
|
||||
|
||||
return { events, rest };
|
||||
}
|
||||
@@ -0,0 +1,256 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { fetch } from 'undici';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { tokenRouter } from '../../services/tokenRouter.js';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { reportProxyAllFailed, reportTokenExpired } from '../../services/alertService.js';
|
||||
import { isTokenExpiredError } from '../../services/alertRules.js';
|
||||
import { estimateProxyCost } from '../../services/modelPricingService.js';
|
||||
import { shouldRetryProxyRequest } from '../../services/proxyRetryPolicy.js';
|
||||
import { resolveProxyUsageWithSelfLogFallback } from '../../services/proxyUsageFallbackService.js';
|
||||
import { mergeProxyUsage, parseProxyUsage, pullSseDataEvents } from '../../services/proxyUsageParser.js';
|
||||
|
||||
const MAX_RETRIES = 2;
|
||||
|
||||
export async function completionsProxyRoute(app: FastifyInstance) {
|
||||
app.post('/v1/completions', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const body = request.body as any;
|
||||
const requestedModel = body?.model;
|
||||
if (!requestedModel) {
|
||||
return reply.code(400).send({ error: { message: 'model is required', type: 'invalid_request_error' } });
|
||||
}
|
||||
|
||||
const isStream = body.stream === true;
|
||||
const excludeChannelIds: number[] = [];
|
||||
let retryCount = 0;
|
||||
|
||||
while (retryCount <= MAX_RETRIES) {
|
||||
let selected = retryCount === 0
|
||||
? tokenRouter.selectChannel(requestedModel)
|
||||
: tokenRouter.selectNextChannel(requestedModel, excludeChannelIds);
|
||||
|
||||
if (!selected && retryCount === 0) {
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
selected = tokenRouter.selectChannel(requestedModel);
|
||||
}
|
||||
|
||||
if (!selected) {
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: 'No available channels after retries',
|
||||
});
|
||||
return reply.code(503).send({
|
||||
error: { message: 'No available channels for this model', type: 'server_error' },
|
||||
});
|
||||
}
|
||||
|
||||
excludeChannelIds.push(selected.channel.id);
|
||||
|
||||
const targetUrl = `${selected.site.url}/v1/completions`;
|
||||
const forwardBody = { ...body, model: selected.actualModel };
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${selected.tokenValue}`,
|
||||
},
|
||||
body: JSON.stringify(forwardBody),
|
||||
});
|
||||
|
||||
if (!upstream.ok) {
|
||||
const errText = await upstream.text().catch(() => 'unknown error');
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', upstream.status, Date.now() - startTime, errText, retryCount);
|
||||
|
||||
if (isTokenExpiredError({ status: upstream.status, message: errText })) {
|
||||
await reportTokenExpired({
|
||||
accountId: selected.account.id,
|
||||
username: selected.account.username,
|
||||
siteName: selected.site.name,
|
||||
detail: `HTTP ${upstream.status}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (shouldRetryProxyRequest(upstream.status, errText) && retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: `upstream returned HTTP ${upstream.status}`,
|
||||
});
|
||||
return reply.code(upstream.status).send({ error: { message: errText, type: 'upstream_error' } });
|
||||
}
|
||||
|
||||
if (isStream) {
|
||||
reply.raw.writeHead(200, {
|
||||
'Content-Type': 'text/event-stream',
|
||||
'Cache-Control': 'no-cache',
|
||||
'Connection': 'keep-alive',
|
||||
});
|
||||
|
||||
const reader = upstream.body?.getReader();
|
||||
if (!reader) {
|
||||
reply.raw.end();
|
||||
return;
|
||||
}
|
||||
|
||||
const decoder = new TextDecoder();
|
||||
let parsedUsage = { promptTokens: 0, completionTokens: 0, totalTokens: 0 };
|
||||
let sseBuffer = '';
|
||||
try {
|
||||
while (true) {
|
||||
const { done, value } = await reader.read();
|
||||
if (done) break;
|
||||
const chunk = decoder.decode(value, { stream: true });
|
||||
reply.raw.write(chunk);
|
||||
|
||||
sseBuffer += chunk;
|
||||
const pulled = pullSseDataEvents(sseBuffer);
|
||||
sseBuffer = pulled.rest;
|
||||
for (const eventPayload of pulled.events) {
|
||||
try {
|
||||
parsedUsage = mergeProxyUsage(parsedUsage, parseProxyUsage(JSON.parse(eventPayload)));
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
if (sseBuffer.trim().length > 0) {
|
||||
const pulled = pullSseDataEvents(`${sseBuffer}\n\n`);
|
||||
for (const eventPayload of pulled.events) {
|
||||
try {
|
||||
parsedUsage = mergeProxyUsage(parsedUsage, parseProxyUsage(JSON.parse(eventPayload)));
|
||||
} catch {}
|
||||
}
|
||||
}
|
||||
} finally {
|
||||
reader.releaseLock();
|
||||
reply.raw.end();
|
||||
}
|
||||
|
||||
const latency = Date.now() - startTime;
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected, requestedModel, 'success', 200, latency, null, retryCount,
|
||||
resolvedUsage.promptTokens, resolvedUsage.completionTokens, resolvedUsage.totalTokens, estimatedCost,
|
||||
);
|
||||
return;
|
||||
}
|
||||
|
||||
const data = await upstream.json() as any;
|
||||
const latency = Date.now() - startTime;
|
||||
const parsedUsage = parseProxyUsage(data);
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected, requestedModel, 'success', 200, latency, null, retryCount,
|
||||
resolvedUsage.promptTokens, resolvedUsage.completionTokens, resolvedUsage.totalTokens, estimatedCost,
|
||||
);
|
||||
return reply.send(data);
|
||||
} catch (err: any) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', 0, Date.now() - startTime, err.message, retryCount);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: err.message || 'network failure',
|
||||
});
|
||||
return reply.code(502).send({
|
||||
error: { message: `Upstream error: ${err.message}`, type: 'upstream_error' },
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logProxy(
|
||||
selected: any,
|
||||
modelRequested: string,
|
||||
status: string,
|
||||
httpStatus: number,
|
||||
latencyMs: number,
|
||||
errorMessage: string | null,
|
||||
retryCount: number,
|
||||
promptTokens = 0,
|
||||
completionTokens = 0,
|
||||
totalTokens = 0,
|
||||
estimatedCost = 0,
|
||||
) {
|
||||
try {
|
||||
db.insert(schema.proxyLogs).values({
|
||||
routeId: selected.channel.routeId,
|
||||
channelId: selected.channel.id,
|
||||
accountId: selected.account.id,
|
||||
modelRequested,
|
||||
modelActual: selected.actualModel,
|
||||
status,
|
||||
httpStatus,
|
||||
latencyMs,
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens,
|
||||
estimatedCost,
|
||||
errorMessage,
|
||||
retryCount,
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,172 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { fetch } from 'undici';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { tokenRouter } from '../../services/tokenRouter.js';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { reportProxyAllFailed, reportTokenExpired } from '../../services/alertService.js';
|
||||
import { isTokenExpiredError } from '../../services/alertRules.js';
|
||||
import { estimateProxyCost } from '../../services/modelPricingService.js';
|
||||
import { shouldRetryProxyRequest } from '../../services/proxyRetryPolicy.js';
|
||||
import { resolveProxyUsageWithSelfLogFallback } from '../../services/proxyUsageFallbackService.js';
|
||||
import { parseProxyUsage } from '../../services/proxyUsageParser.js';
|
||||
|
||||
const MAX_RETRIES = 2;
|
||||
|
||||
export async function embeddingsProxyRoute(app: FastifyInstance) {
|
||||
app.post('/v1/embeddings', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const body = request.body as any;
|
||||
const requestedModel = body?.model;
|
||||
if (!requestedModel) {
|
||||
return reply.code(400).send({ error: { message: 'model is required', type: 'invalid_request_error' } });
|
||||
}
|
||||
|
||||
const excludeChannelIds: number[] = [];
|
||||
let retryCount = 0;
|
||||
|
||||
while (retryCount <= MAX_RETRIES) {
|
||||
let selected = retryCount === 0
|
||||
? tokenRouter.selectChannel(requestedModel)
|
||||
: tokenRouter.selectNextChannel(requestedModel, excludeChannelIds);
|
||||
|
||||
if (!selected && retryCount === 0) {
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
selected = tokenRouter.selectChannel(requestedModel);
|
||||
}
|
||||
|
||||
if (!selected) {
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: 'No available channels after retries',
|
||||
});
|
||||
return reply.code(503).send({ error: { message: 'No available channels', type: 'server_error' } });
|
||||
}
|
||||
|
||||
excludeChannelIds.push(selected.channel.id);
|
||||
|
||||
const targetUrl = `${selected.site.url}/v1/embeddings`;
|
||||
const forwardBody = { ...body, model: selected.actualModel };
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${selected.tokenValue}`,
|
||||
},
|
||||
body: JSON.stringify(forwardBody),
|
||||
});
|
||||
|
||||
const text = await upstream.text();
|
||||
if (!upstream.ok) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', upstream.status, Date.now() - startTime, text, retryCount);
|
||||
|
||||
if (isTokenExpiredError({ status: upstream.status, message: text })) {
|
||||
await reportTokenExpired({
|
||||
accountId: selected.account.id,
|
||||
username: selected.account.username,
|
||||
siteName: selected.site.name,
|
||||
detail: `HTTP ${upstream.status}`,
|
||||
});
|
||||
}
|
||||
|
||||
if (shouldRetryProxyRequest(upstream.status, text) && retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: `upstream returned HTTP ${upstream.status}`,
|
||||
});
|
||||
return reply.code(upstream.status).send({ error: { message: text, type: 'upstream_error' } });
|
||||
}
|
||||
|
||||
let data: any = {};
|
||||
try { data = JSON.parse(text); } catch { data = {}; }
|
||||
const latency = Date.now() - startTime;
|
||||
const parsedUsage = parseProxyUsage(data);
|
||||
const resolvedUsage = await resolveProxyUsageWithSelfLogFallback({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
tokenValue: selected.tokenValue,
|
||||
tokenName: selected.tokenName,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
requestStartedAtMs: startTime,
|
||||
requestEndedAtMs: startTime + latency,
|
||||
localLatencyMs: latency,
|
||||
usage: {
|
||||
promptTokens: parsedUsage.promptTokens,
|
||||
completionTokens: parsedUsage.completionTokens,
|
||||
totalTokens: parsedUsage.totalTokens,
|
||||
},
|
||||
});
|
||||
let estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
promptTokens: resolvedUsage.promptTokens,
|
||||
completionTokens: resolvedUsage.completionTokens,
|
||||
totalTokens: resolvedUsage.totalTokens,
|
||||
});
|
||||
if (resolvedUsage.estimatedCostFromQuota > 0 && (resolvedUsage.recoveredFromSelfLog || estimatedCost <= 0)) {
|
||||
estimatedCost = resolvedUsage.estimatedCostFromQuota;
|
||||
}
|
||||
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(
|
||||
selected, requestedModel, 'success', upstream.status, latency, null, retryCount,
|
||||
resolvedUsage.promptTokens, resolvedUsage.completionTokens, resolvedUsage.totalTokens, estimatedCost,
|
||||
);
|
||||
return reply.code(upstream.status).send(data);
|
||||
} catch (err: any) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', 0, Date.now() - startTime, err.message, retryCount);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: err.message || 'network failure',
|
||||
});
|
||||
return reply.code(502).send({ error: { message: err.message, type: 'upstream_error' } });
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logProxy(
|
||||
selected: any,
|
||||
modelRequested: string,
|
||||
status: string,
|
||||
httpStatus: number,
|
||||
latencyMs: number,
|
||||
errorMessage: string | null,
|
||||
retryCount: number,
|
||||
promptTokens = 0,
|
||||
completionTokens = 0,
|
||||
totalTokens = 0,
|
||||
estimatedCost = 0,
|
||||
) {
|
||||
try {
|
||||
db.insert(schema.proxyLogs).values({
|
||||
routeId: selected.channel.routeId,
|
||||
channelId: selected.channel.id,
|
||||
accountId: selected.account.id,
|
||||
modelRequested,
|
||||
modelActual: selected.actualModel,
|
||||
status,
|
||||
httpStatus,
|
||||
latencyMs,
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens,
|
||||
estimatedCost,
|
||||
errorMessage,
|
||||
retryCount,
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,142 @@
|
||||
import { FastifyInstance, FastifyReply, FastifyRequest } from 'fastify';
|
||||
import { fetch } from 'undici';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { tokenRouter } from '../../services/tokenRouter.js';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
import { reportProxyAllFailed, reportTokenExpired } from '../../services/alertService.js';
|
||||
import { isTokenExpiredError } from '../../services/alertRules.js';
|
||||
import { estimateProxyCost } from '../../services/modelPricingService.js';
|
||||
import { shouldRetryProxyRequest } from '../../services/proxyRetryPolicy.js';
|
||||
|
||||
const MAX_RETRIES = 2;
|
||||
|
||||
export async function imagesProxyRoute(app: FastifyInstance) {
|
||||
app.post('/v1/images/generations', async (request: FastifyRequest, reply: FastifyReply) => {
|
||||
const body = request.body as any;
|
||||
const requestedModel = body?.model || 'gpt-image-1';
|
||||
const excludeChannelIds: number[] = [];
|
||||
let retryCount = 0;
|
||||
|
||||
while (retryCount <= MAX_RETRIES) {
|
||||
let selected = retryCount === 0
|
||||
? tokenRouter.selectChannel(requestedModel)
|
||||
: tokenRouter.selectNextChannel(requestedModel, excludeChannelIds);
|
||||
|
||||
if (!selected && retryCount === 0) {
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
selected = tokenRouter.selectChannel(requestedModel);
|
||||
}
|
||||
|
||||
if (!selected) {
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: 'No available channels after retries',
|
||||
});
|
||||
return reply.code(503).send({
|
||||
error: { message: 'No available channels for this model', type: 'server_error' },
|
||||
});
|
||||
}
|
||||
|
||||
excludeChannelIds.push(selected.channel.id);
|
||||
|
||||
const targetUrl = `${selected.site.url}/v1/images/generations`;
|
||||
const forwardBody = { ...body, model: selected.actualModel };
|
||||
const startTime = Date.now();
|
||||
|
||||
try {
|
||||
const upstream = await fetch(targetUrl, {
|
||||
method: 'POST',
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
'Authorization': `Bearer ${selected.tokenValue}`,
|
||||
},
|
||||
body: JSON.stringify(forwardBody),
|
||||
});
|
||||
|
||||
const text = await upstream.text();
|
||||
if (!upstream.ok) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', upstream.status, Date.now() - startTime, text, retryCount);
|
||||
if (isTokenExpiredError({ status: upstream.status, message: text })) {
|
||||
await reportTokenExpired({
|
||||
accountId: selected.account.id,
|
||||
username: selected.account.username,
|
||||
siteName: selected.site.name,
|
||||
detail: `HTTP ${upstream.status}`,
|
||||
});
|
||||
}
|
||||
if (shouldRetryProxyRequest(upstream.status, text) && retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: `upstream returned HTTP ${upstream.status}`,
|
||||
});
|
||||
return reply.code(upstream.status).send({ error: { message: text, type: 'upstream_error' } });
|
||||
}
|
||||
|
||||
let data: any = {};
|
||||
try { data = JSON.parse(text); } catch { data = { data: [] }; }
|
||||
|
||||
const latency = Date.now() - startTime;
|
||||
const estimatedCost = await estimateProxyCost({
|
||||
site: selected.site,
|
||||
account: selected.account,
|
||||
modelName: selected.actualModel || requestedModel,
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0,
|
||||
});
|
||||
tokenRouter.recordSuccess(selected.channel.id, latency, estimatedCost);
|
||||
logProxy(selected, requestedModel, 'success', upstream.status, latency, null, retryCount, estimatedCost);
|
||||
return reply.code(upstream.status).send(data);
|
||||
} catch (err: any) {
|
||||
tokenRouter.recordFailure(selected.channel.id);
|
||||
logProxy(selected, requestedModel, 'failed', 0, Date.now() - startTime, err.message, retryCount);
|
||||
if (retryCount < MAX_RETRIES) {
|
||||
retryCount++;
|
||||
continue;
|
||||
}
|
||||
await reportProxyAllFailed({
|
||||
model: requestedModel,
|
||||
reason: err.message || 'network failure',
|
||||
});
|
||||
return reply.code(502).send({
|
||||
error: { message: `Upstream error: ${err.message}`, type: 'upstream_error' },
|
||||
});
|
||||
}
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function logProxy(
|
||||
selected: any,
|
||||
modelRequested: string,
|
||||
status: string,
|
||||
httpStatus: number,
|
||||
latencyMs: number,
|
||||
errorMessage: string | null,
|
||||
retryCount: number,
|
||||
estimatedCost = 0,
|
||||
) {
|
||||
try {
|
||||
db.insert(schema.proxyLogs).values({
|
||||
routeId: selected.channel.routeId,
|
||||
channelId: selected.channel.id,
|
||||
accountId: selected.account.id,
|
||||
modelRequested,
|
||||
modelActual: selected.actualModel,
|
||||
status,
|
||||
httpStatus,
|
||||
latencyMs,
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0,
|
||||
estimatedCost,
|
||||
errorMessage,
|
||||
retryCount,
|
||||
}).run();
|
||||
} catch {}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,57 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { db, schema } from '../../db/index.js';
|
||||
import { and, eq } from 'drizzle-orm';
|
||||
import { refreshModelsAndRebuildRoutes } from '../../services/modelService.js';
|
||||
|
||||
export async function modelsProxyRoute(app: FastifyInstance) {
|
||||
app.get('/v1/models', async (request) => {
|
||||
const readModels = () => {
|
||||
const rows = db.select({ modelName: schema.modelAvailability.modelName })
|
||||
.from(schema.modelAvailability)
|
||||
.innerJoin(schema.accounts, eq(schema.modelAvailability.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(
|
||||
and(
|
||||
eq(schema.modelAvailability.available, true),
|
||||
eq(schema.accounts.status, 'active'),
|
||||
eq(schema.sites.status, 'active'),
|
||||
),
|
||||
)
|
||||
.all();
|
||||
return Array.from(new Set(rows.map((r) => r.modelName))).sort();
|
||||
};
|
||||
|
||||
let models = readModels();
|
||||
if (models.length === 0) {
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
models = readModels();
|
||||
}
|
||||
|
||||
const wantsClaudeFormat = typeof request.headers['anthropic-version'] === 'string'
|
||||
|| typeof request.headers['x-api-key'] === 'string';
|
||||
if (wantsClaudeFormat) {
|
||||
const data = models.map((id) => ({
|
||||
id,
|
||||
type: 'model',
|
||||
display_name: id,
|
||||
created_at: new Date().toISOString(),
|
||||
}));
|
||||
return {
|
||||
data,
|
||||
first_id: data[0]?.id || null,
|
||||
last_id: data[data.length - 1]?.id || null,
|
||||
has_more: false,
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
object: 'list',
|
||||
data: models.map(id => ({
|
||||
id,
|
||||
object: 'model',
|
||||
created: Math.floor(Date.now() / 1000),
|
||||
owned_by: 'metapi',
|
||||
})),
|
||||
};
|
||||
});
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { FastifyInstance } from 'fastify';
|
||||
import { proxyAuthMiddleware } from '../../middleware/auth.js';
|
||||
import { chatProxyRoute, claudeMessagesProxyRoute } from './chat.js';
|
||||
import { modelsProxyRoute } from './models.js';
|
||||
import { embeddingsProxyRoute } from './embeddings.js';
|
||||
import { completionsProxyRoute } from './completions.js';
|
||||
import { imagesProxyRoute } from './images.js';
|
||||
|
||||
export async function proxyRoutes(app: FastifyInstance) {
|
||||
// Auth middleware for all /v1 routes
|
||||
app.addHook('onRequest', async (request, reply) => {
|
||||
await proxyAuthMiddleware(request, reply);
|
||||
});
|
||||
|
||||
await app.register(chatProxyRoute);
|
||||
await app.register(claudeMessagesProxyRoute);
|
||||
await app.register(completionsProxyRoute);
|
||||
await app.register(modelsProxyRoute);
|
||||
await app.register(embeddingsProxyRoute);
|
||||
await app.register(imagesProxyRoute);
|
||||
}
|
||||
@@ -0,0 +1,18 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
decryptAccountPassword,
|
||||
encryptAccountPassword,
|
||||
} from './accountCredentialService.js';
|
||||
|
||||
describe('accountCredentialService', () => {
|
||||
it('encrypts and decrypts password values', () => {
|
||||
const cipher = encryptAccountPassword('P@ssw0rd-123');
|
||||
expect(cipher).toBeTruthy();
|
||||
expect(cipher).not.toContain('P@ssw0rd-123');
|
||||
expect(decryptAccountPassword(cipher)).toBe('P@ssw0rd-123');
|
||||
});
|
||||
|
||||
it('returns null for malformed cipher text', () => {
|
||||
expect(decryptAccountPassword('invalid-cipher')).toBeNull();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,44 @@
|
||||
import { createCipheriv, createDecipheriv, createHash, randomBytes } from 'node:crypto';
|
||||
import { config } from '../config.js';
|
||||
|
||||
const VERSION = 'v1';
|
||||
const ALGORITHM = 'aes-256-gcm';
|
||||
|
||||
function buildKey(): Buffer {
|
||||
const secret = (config.accountCredentialSecret || '').trim() || config.authToken || 'change-me';
|
||||
return createHash('sha256').update(secret).digest();
|
||||
}
|
||||
|
||||
export function encryptAccountPassword(password: string): string {
|
||||
const key = buildKey();
|
||||
const iv = randomBytes(12);
|
||||
const cipher = createCipheriv(ALGORITHM, key, iv);
|
||||
const encrypted = Buffer.concat([cipher.update(password, 'utf8'), cipher.final()]);
|
||||
const tag = cipher.getAuthTag();
|
||||
return [
|
||||
VERSION,
|
||||
iv.toString('base64url'),
|
||||
tag.toString('base64url'),
|
||||
encrypted.toString('base64url'),
|
||||
].join(':');
|
||||
}
|
||||
|
||||
export function decryptAccountPassword(cipherText: string): string | null {
|
||||
const parts = (cipherText || '').split(':');
|
||||
if (parts.length !== 4 || parts[0] !== VERSION) return null;
|
||||
|
||||
try {
|
||||
const [, ivRaw, tagRaw, dataRaw] = parts;
|
||||
const key = buildKey();
|
||||
const iv = Buffer.from(ivRaw, 'base64url');
|
||||
const tag = Buffer.from(tagRaw, 'base64url');
|
||||
const data = Buffer.from(dataRaw, 'base64url');
|
||||
|
||||
const decipher = createDecipheriv(ALGORITHM, key, iv);
|
||||
decipher.setAuthTag(tag);
|
||||
const plain = Buffer.concat([decipher.update(data), decipher.final()]);
|
||||
return plain.toString('utf8');
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,41 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
getPlatformUserIdFromExtraConfig,
|
||||
guessPlatformUserIdFromUsername,
|
||||
mergeAccountExtraConfig,
|
||||
resolvePlatformUserId,
|
||||
} from './accountExtraConfig.js';
|
||||
|
||||
describe('accountExtraConfig', () => {
|
||||
it('reads platformUserId from extra config when present', () => {
|
||||
expect(getPlatformUserIdFromExtraConfig(JSON.stringify({ platformUserId: 11494 }))).toBe(11494);
|
||||
expect(getPlatformUserIdFromExtraConfig(JSON.stringify({ platformUserId: '7659' }))).toBe(7659);
|
||||
});
|
||||
|
||||
it('guesses platformUserId from username suffix digits', () => {
|
||||
expect(guessPlatformUserIdFromUsername('linuxdo_7659')).toBe(7659);
|
||||
expect(guessPlatformUserIdFromUsername('user11494')).toBe(11494);
|
||||
expect(guessPlatformUserIdFromUsername('abc')).toBeUndefined();
|
||||
expect(guessPlatformUserIdFromUsername('id_12')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('prefers configured user id over guessed user id', () => {
|
||||
expect(resolvePlatformUserId(JSON.stringify({ platformUserId: 5001 }), 'linuxdo_7659')).toBe(5001);
|
||||
});
|
||||
|
||||
it('merges platformUserId into existing config without dropping keys', () => {
|
||||
const merged = mergeAccountExtraConfig(
|
||||
JSON.stringify({
|
||||
foo: 'bar',
|
||||
autoRelogin: { username: 'demo', passwordCipher: 'cipher' },
|
||||
}),
|
||||
{ platformUserId: 7659 },
|
||||
);
|
||||
|
||||
expect(merged).toBeTruthy();
|
||||
const parsed = JSON.parse(merged!);
|
||||
expect(parsed.foo).toBe('bar');
|
||||
expect(parsed.autoRelogin?.username).toBe('demo');
|
||||
expect(parsed.platformUserId).toBe(7659);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,74 @@
|
||||
type AutoReloginConfig = {
|
||||
username?: unknown;
|
||||
passwordCipher?: unknown;
|
||||
updatedAt?: unknown;
|
||||
};
|
||||
|
||||
type AccountExtraConfig = {
|
||||
platformUserId?: unknown;
|
||||
autoRelogin?: AutoReloginConfig;
|
||||
[key: string]: unknown;
|
||||
};
|
||||
|
||||
function parseExtraConfig(extraConfig?: string | null): AccountExtraConfig {
|
||||
if (!extraConfig) return {};
|
||||
try {
|
||||
const parsed = JSON.parse(extraConfig) as unknown;
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
||||
return parsed as AccountExtraConfig;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeUserId(raw: unknown): number | undefined {
|
||||
if (typeof raw === 'number' && Number.isFinite(raw) && raw > 0) return Math.trunc(raw);
|
||||
if (typeof raw === 'string') {
|
||||
const n = Number.parseInt(raw.trim(), 10);
|
||||
if (!Number.isNaN(n) && n > 0) return n;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
|
||||
export function getPlatformUserIdFromExtraConfig(extraConfig?: string | null): number | undefined {
|
||||
const parsed = parseExtraConfig(extraConfig);
|
||||
return normalizeUserId(parsed.platformUserId);
|
||||
}
|
||||
|
||||
export function guessPlatformUserIdFromUsername(username?: string | null): number | undefined {
|
||||
const text = (username || '').trim();
|
||||
if (!text) return undefined;
|
||||
const match = text.match(/(\d{3,8})$/);
|
||||
if (!match?.[1]) return undefined;
|
||||
return normalizeUserId(match[1]);
|
||||
}
|
||||
|
||||
export function resolvePlatformUserId(extraConfig?: string | null, username?: string | null): number | undefined {
|
||||
return getPlatformUserIdFromExtraConfig(extraConfig) || guessPlatformUserIdFromUsername(username);
|
||||
}
|
||||
|
||||
export function mergeAccountExtraConfig(
|
||||
extraConfig: string | null | undefined,
|
||||
patch: Record<string, unknown>,
|
||||
): string {
|
||||
const merged: Record<string, unknown> = {
|
||||
...parseExtraConfig(extraConfig),
|
||||
...patch,
|
||||
};
|
||||
return JSON.stringify(merged);
|
||||
}
|
||||
|
||||
export function getAutoReloginConfig(extraConfig?: string | null): {
|
||||
username: string;
|
||||
passwordCipher: string;
|
||||
} | null {
|
||||
const parsed = parseExtraConfig(extraConfig);
|
||||
const relogin = parsed.autoRelogin;
|
||||
if (!relogin || typeof relogin !== 'object' || Array.isArray(relogin)) return null;
|
||||
|
||||
const username = typeof relogin.username === 'string' ? relogin.username.trim() : '';
|
||||
const passwordCipher = typeof relogin.passwordCipher === 'string' ? relogin.passwordCipher.trim() : '';
|
||||
if (!username || !passwordCipher) return null;
|
||||
|
||||
return { username, passwordCipher };
|
||||
}
|
||||
@@ -0,0 +1,56 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildRuntimeHealthForAccount } from './accountHealthService.js';
|
||||
|
||||
describe('accountHealthService', () => {
|
||||
it('marks disabled when site or account is disabled', () => {
|
||||
expect(
|
||||
buildRuntimeHealthForAccount({ accountStatus: 'active', siteStatus: 'disabled', extraConfig: null }).state,
|
||||
).toBe('disabled');
|
||||
expect(
|
||||
buildRuntimeHealthForAccount({ accountStatus: 'disabled', siteStatus: 'active', extraConfig: null }).state,
|
||||
).toBe('disabled');
|
||||
});
|
||||
|
||||
it('marks unhealthy when account is expired', () => {
|
||||
const health = buildRuntimeHealthForAccount({
|
||||
accountStatus: 'expired',
|
||||
siteStatus: 'active',
|
||||
extraConfig: null,
|
||||
});
|
||||
expect(health.state).toBe('unhealthy');
|
||||
});
|
||||
|
||||
it('returns stored runtime health from extra config when available', () => {
|
||||
const health = buildRuntimeHealthForAccount({
|
||||
accountStatus: 'active',
|
||||
siteStatus: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
runtimeHealth: {
|
||||
state: 'healthy',
|
||||
reason: '余额刷新成功',
|
||||
source: 'balance',
|
||||
checkedAt: '2026-02-25T12:00:00.000Z',
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
expect(health).toMatchObject({
|
||||
state: 'healthy',
|
||||
reason: '余额刷新成功',
|
||||
source: 'balance',
|
||||
checkedAt: '2026-02-25T12:00:00.000Z',
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to unknown when no runtime health info exists', () => {
|
||||
const health = buildRuntimeHealthForAccount({
|
||||
accountStatus: 'active',
|
||||
siteStatus: 'active',
|
||||
extraConfig: null,
|
||||
});
|
||||
expect(health).toMatchObject({
|
||||
state: 'unknown',
|
||||
source: 'none',
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,176 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { mergeAccountExtraConfig } from './accountExtraConfig.js';
|
||||
|
||||
export type RuntimeHealthState = 'healthy' | 'unhealthy' | 'degraded' | 'unknown' | 'disabled';
|
||||
|
||||
type RuntimeHealthInfo = {
|
||||
state: RuntimeHealthState;
|
||||
reason: string;
|
||||
source: string;
|
||||
checkedAt: string | null;
|
||||
};
|
||||
|
||||
const VALID_RUNTIME_HEALTH_STATES = new Set<RuntimeHealthState>([
|
||||
'healthy',
|
||||
'unhealthy',
|
||||
'degraded',
|
||||
'unknown',
|
||||
'disabled',
|
||||
]);
|
||||
|
||||
function parseObject(value: string | null | undefined): Record<string, unknown> {
|
||||
if (!value) return {};
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
||||
return parsed as Record<string, unknown>;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeRuntimeHealthState(value: unknown): RuntimeHealthState | null {
|
||||
if (typeof value !== 'string') return null;
|
||||
const normalized = value.trim().toLowerCase();
|
||||
if (!VALID_RUNTIME_HEALTH_STATES.has(normalized as RuntimeHealthState)) return null;
|
||||
return normalized as RuntimeHealthState;
|
||||
}
|
||||
|
||||
function normalizeRuntimeHealthRecord(raw: unknown): RuntimeHealthInfo | null {
|
||||
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
|
||||
const source = raw as Record<string, unknown>;
|
||||
const state = normalizeRuntimeHealthState(source.state);
|
||||
if (!state) return null;
|
||||
|
||||
const reason = typeof source.reason === 'string'
|
||||
? source.reason.trim().slice(0, 500)
|
||||
: '';
|
||||
const checkedAt = typeof source.checkedAt === 'string' && source.checkedAt.trim()
|
||||
? source.checkedAt.trim()
|
||||
: null;
|
||||
const eventSource = typeof source.source === 'string' && source.source.trim()
|
||||
? source.source.trim().slice(0, 64)
|
||||
: 'unknown';
|
||||
|
||||
return {
|
||||
state,
|
||||
reason: reason || defaultHealthReason(state),
|
||||
source: eventSource,
|
||||
checkedAt,
|
||||
};
|
||||
}
|
||||
|
||||
function defaultHealthReason(state: RuntimeHealthState): string {
|
||||
switch (state) {
|
||||
case 'healthy':
|
||||
return '运行状态正常';
|
||||
case 'unhealthy':
|
||||
return '最近一次检查失败';
|
||||
case 'degraded':
|
||||
return '运行状态波动';
|
||||
case 'disabled':
|
||||
return '账号或站点已禁用';
|
||||
case 'unknown':
|
||||
default:
|
||||
return '尚未检测';
|
||||
}
|
||||
}
|
||||
|
||||
export function extractRuntimeHealth(extraConfig?: string | null): RuntimeHealthInfo | null {
|
||||
const parsed = parseObject(extraConfig);
|
||||
return normalizeRuntimeHealthRecord(parsed.runtimeHealth);
|
||||
}
|
||||
|
||||
export function buildRuntimeHealthForAccount(input: {
|
||||
accountStatus?: string | null;
|
||||
siteStatus?: string | null;
|
||||
extraConfig?: string | null;
|
||||
}): RuntimeHealthInfo {
|
||||
const accountStatus = (input.accountStatus || 'active').toLowerCase();
|
||||
const siteStatus = (input.siteStatus || 'active').toLowerCase();
|
||||
|
||||
if (accountStatus === 'disabled' || siteStatus === 'disabled') {
|
||||
return {
|
||||
state: 'disabled',
|
||||
reason: defaultHealthReason('disabled'),
|
||||
source: 'system',
|
||||
checkedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
if (accountStatus === 'expired') {
|
||||
return {
|
||||
state: 'unhealthy',
|
||||
reason: '访问令牌已过期',
|
||||
source: 'auth',
|
||||
checkedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
const stored = extractRuntimeHealth(input.extraConfig);
|
||||
if (stored) return stored;
|
||||
|
||||
return {
|
||||
state: 'unknown',
|
||||
reason: defaultHealthReason('unknown'),
|
||||
source: 'none',
|
||||
checkedAt: null,
|
||||
};
|
||||
}
|
||||
|
||||
function buildRuntimeHealthPatch(input: {
|
||||
state: RuntimeHealthState;
|
||||
reason?: string | null;
|
||||
source?: string | null;
|
||||
checkedAt?: string | null;
|
||||
}): RuntimeHealthInfo {
|
||||
const state = normalizeRuntimeHealthState(input.state) || 'unknown';
|
||||
const reason = (input.reason || '').trim().slice(0, 500) || defaultHealthReason(state);
|
||||
const source = (input.source || '').trim().slice(0, 64) || 'manual';
|
||||
const checkedAt = (input.checkedAt || '').trim() || new Date().toISOString();
|
||||
|
||||
return {
|
||||
state,
|
||||
reason,
|
||||
source,
|
||||
checkedAt,
|
||||
};
|
||||
}
|
||||
|
||||
function applyRuntimeHealthToExtraConfig(extraConfig: string | null | undefined, health: RuntimeHealthInfo): string {
|
||||
return mergeAccountExtraConfig(extraConfig, {
|
||||
runtimeHealth: health,
|
||||
});
|
||||
}
|
||||
|
||||
export function setAccountRuntimeHealth(
|
||||
accountId: number,
|
||||
input: {
|
||||
state: RuntimeHealthState;
|
||||
reason?: string | null;
|
||||
source?: string | null;
|
||||
checkedAt?: string | null;
|
||||
},
|
||||
): RuntimeHealthInfo | null {
|
||||
try {
|
||||
const query = db.select().from(schema.accounts).where(eq(schema.accounts.id, accountId)) as any;
|
||||
const account = typeof query?.get === 'function' ? query.get() : null;
|
||||
if (!account) return null;
|
||||
|
||||
const health = buildRuntimeHealthPatch(input);
|
||||
const nextExtraConfig = applyRuntimeHealthToExtraConfig(account.extraConfig, health);
|
||||
|
||||
db.update(schema.accounts)
|
||||
.set({
|
||||
extraConfig: nextExtraConfig,
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.run();
|
||||
|
||||
return health;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { maskToken, normalizeTokenForDisplay } from './accountTokenService.js';
|
||||
|
||||
describe('maskToken', () => {
|
||||
it('keeps sk- prefix for short tokens', () => {
|
||||
const masked = maskToken('sk-abcde');
|
||||
expect(masked.startsWith('sk-')).toBe(true);
|
||||
expect(masked.includes('***')).toBe(true);
|
||||
});
|
||||
|
||||
it('keeps sk- prefix for long tokens', () => {
|
||||
const masked = maskToken('sk-proj-abcdefghijklmnopqrstuvwxyz123456');
|
||||
expect(masked.startsWith('sk-')).toBe(true);
|
||||
expect(masked.includes('***')).toBe(true);
|
||||
});
|
||||
|
||||
it('adds sk- prefix for new-api display when upstream key misses prefix', () => {
|
||||
const masked = maskToken('5Uh6KjAgVOqQxNv97MAS7abkBf7Fg5GphgBXUpAOuvq2IvL4', 'new-api');
|
||||
expect(masked.startsWith('sk-')).toBe(true);
|
||||
});
|
||||
});
|
||||
|
||||
describe('normalizeTokenForDisplay', () => {
|
||||
it('adds sk- for any platform when token misses prefix', () => {
|
||||
expect(normalizeTokenForDisplay('abc123', 'new-api')).toBe('sk-abc123');
|
||||
expect(normalizeTokenForDisplay('xyz789', 'one-api')).toBe('sk-xyz789');
|
||||
expect(normalizeTokenForDisplay('pqr456', 'anyrouter')).toBe('sk-pqr456');
|
||||
expect(normalizeTokenForDisplay('uvw000', 'veloera')).toBe('sk-uvw000');
|
||||
});
|
||||
|
||||
it('keeps existing sk- token unchanged', () => {
|
||||
expect(normalizeTokenForDisplay('sk-abc123', 'veloera')).toBe('sk-abc123');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,260 @@
|
||||
import { and, eq, ne } from 'drizzle-orm';
|
||||
import { db, schema } from '../db/index.js';
|
||||
|
||||
type UpstreamApiToken = {
|
||||
name?: string | null;
|
||||
key?: string | null;
|
||||
enabled?: boolean | null;
|
||||
};
|
||||
|
||||
export function normalizeTokenForDisplay(token?: string | null, platform?: string | null): string {
|
||||
if (!token) return '';
|
||||
const value = token.trim();
|
||||
if (!value) return '';
|
||||
if (platform !== undefined) {
|
||||
// Keep the parameter for route-level compatibility; display rule is now global.
|
||||
}
|
||||
if (!value.toLowerCase().startsWith('sk-')) {
|
||||
return `sk-${value}`;
|
||||
}
|
||||
return value;
|
||||
}
|
||||
|
||||
export function maskToken(token?: string | null, platform?: string | null): string {
|
||||
const value = normalizeTokenForDisplay(token, platform);
|
||||
if (!value) return '';
|
||||
if (value.toLowerCase().startsWith('sk-')) {
|
||||
if (value.length <= 7) return 'sk-***';
|
||||
const visibleMiddle = value.slice(3, Math.min(6, value.length));
|
||||
if (value.length <= 12) return `sk-${visibleMiddle}***${value.slice(-2)}`;
|
||||
return `sk-${visibleMiddle}***${value.slice(-4)}`;
|
||||
}
|
||||
if (value.length <= 10) return `${value.slice(0, 2)}***${value.slice(-2)}`;
|
||||
return `${value.slice(0, 4)}***${value.slice(-4)}`;
|
||||
}
|
||||
|
||||
function normalizeTokenName(name: string | null | undefined, fallbackIndex = 1): string {
|
||||
const trimmed = (name || '').trim();
|
||||
if (trimmed) return trimmed;
|
||||
return fallbackIndex === 1 ? 'default' : `token-${fallbackIndex}`;
|
||||
}
|
||||
|
||||
function normalizeTokenValue(token: string | null | undefined): string | null {
|
||||
const trimmed = (token || '').trim();
|
||||
return trimmed.length > 0 ? trimmed : null;
|
||||
}
|
||||
|
||||
function updateAccountApiToken(accountId: number, tokenValue: string | null) {
|
||||
db.update(schema.accounts)
|
||||
.set({ apiToken: tokenValue || null, updatedAt: new Date().toISOString() })
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.run();
|
||||
}
|
||||
|
||||
export function getPreferredAccountToken(accountId: number) {
|
||||
const tokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, accountId), eq(schema.accountTokens.enabled, true)))
|
||||
.all();
|
||||
|
||||
if (tokens.length === 0) return null;
|
||||
|
||||
const preferred = tokens.find((t) => t.isDefault) || tokens[0];
|
||||
return preferred;
|
||||
}
|
||||
|
||||
export function ensureDefaultTokenForAccount(
|
||||
accountId: number,
|
||||
tokenValue: string,
|
||||
options?: { name?: string; source?: string; enabled?: boolean },
|
||||
): number | null {
|
||||
const normalizedToken = normalizeTokenValue(tokenValue);
|
||||
if (!normalizedToken) return null;
|
||||
|
||||
const now = new Date().toISOString();
|
||||
const tokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, accountId))
|
||||
.all();
|
||||
|
||||
let target = tokens.find((t) => t.token === normalizedToken) || null;
|
||||
if (!target) {
|
||||
target = db.insert(schema.accountTokens)
|
||||
.values({
|
||||
accountId,
|
||||
name: normalizeTokenName(options?.name, tokens.length + 1),
|
||||
token: normalizedToken,
|
||||
source: options?.source || 'manual',
|
||||
enabled: options?.enabled ?? true,
|
||||
isDefault: true,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
} else {
|
||||
db.update(schema.accountTokens)
|
||||
.set({
|
||||
name: options?.name ? normalizeTokenName(options.name) : target.name,
|
||||
source: options?.source || target.source || 'manual',
|
||||
enabled: options?.enabled ?? target.enabled,
|
||||
isDefault: true,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(schema.accountTokens.id, target.id))
|
||||
.run();
|
||||
}
|
||||
|
||||
db.update(schema.accountTokens)
|
||||
.set({ isDefault: false, updatedAt: now })
|
||||
.where(and(eq(schema.accountTokens.accountId, accountId), ne(schema.accountTokens.id, target.id)))
|
||||
.run();
|
||||
|
||||
updateAccountApiToken(accountId, normalizedToken);
|
||||
return target.id;
|
||||
}
|
||||
|
||||
export function setDefaultToken(tokenId: number): boolean {
|
||||
const target = db.select().from(schema.accountTokens).where(eq(schema.accountTokens.id, tokenId)).get();
|
||||
if (!target) return false;
|
||||
|
||||
const now = new Date().toISOString();
|
||||
db.update(schema.accountTokens)
|
||||
.set({ isDefault: false, updatedAt: now })
|
||||
.where(eq(schema.accountTokens.accountId, target.accountId))
|
||||
.run();
|
||||
|
||||
db.update(schema.accountTokens)
|
||||
.set({ isDefault: true, enabled: true, updatedAt: now })
|
||||
.where(eq(schema.accountTokens.id, tokenId))
|
||||
.run();
|
||||
|
||||
updateAccountApiToken(target.accountId, target.token);
|
||||
return true;
|
||||
}
|
||||
|
||||
export function repairDefaultToken(accountId: number) {
|
||||
const tokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, accountId))
|
||||
.all();
|
||||
|
||||
const enabled = tokens.filter((t) => t.enabled);
|
||||
if (enabled.length === 0) {
|
||||
updateAccountApiToken(accountId, null);
|
||||
return null;
|
||||
}
|
||||
|
||||
const currentDefault = enabled.find((t) => t.isDefault) || enabled[0];
|
||||
const now = new Date().toISOString();
|
||||
|
||||
db.update(schema.accountTokens)
|
||||
.set({ isDefault: false, updatedAt: now })
|
||||
.where(eq(schema.accountTokens.accountId, accountId))
|
||||
.run();
|
||||
|
||||
db.update(schema.accountTokens)
|
||||
.set({ isDefault: true, enabled: true, updatedAt: now })
|
||||
.where(eq(schema.accountTokens.id, currentDefault.id))
|
||||
.run();
|
||||
|
||||
updateAccountApiToken(accountId, currentDefault.token);
|
||||
return currentDefault;
|
||||
}
|
||||
|
||||
export function syncTokensFromUpstream(accountId: number, upstreamTokens: UpstreamApiToken[]) {
|
||||
const now = new Date().toISOString();
|
||||
const existing = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, accountId))
|
||||
.all();
|
||||
|
||||
let created = 0;
|
||||
let updated = 0;
|
||||
let index = existing.length + 1;
|
||||
|
||||
for (const upstream of upstreamTokens) {
|
||||
const tokenValue = normalizeTokenValue(upstream.key);
|
||||
if (!tokenValue) continue;
|
||||
|
||||
const tokenName = normalizeTokenName(upstream.name, index);
|
||||
const enabled = upstream.enabled ?? true;
|
||||
|
||||
const byToken = existing.find((row) => row.token === tokenValue);
|
||||
if (byToken) {
|
||||
db.update(schema.accountTokens)
|
||||
.set({
|
||||
name: tokenName,
|
||||
source: 'sync',
|
||||
enabled,
|
||||
updatedAt: now,
|
||||
})
|
||||
.where(eq(schema.accountTokens.id, byToken.id))
|
||||
.run();
|
||||
byToken.name = tokenName;
|
||||
byToken.enabled = enabled;
|
||||
byToken.source = 'sync';
|
||||
byToken.updatedAt = now;
|
||||
updated++;
|
||||
continue;
|
||||
}
|
||||
|
||||
const createdRow = db.insert(schema.accountTokens)
|
||||
.values({
|
||||
accountId,
|
||||
name: tokenName,
|
||||
token: tokenValue,
|
||||
source: 'sync',
|
||||
enabled,
|
||||
isDefault: false,
|
||||
createdAt: now,
|
||||
updatedAt: now,
|
||||
})
|
||||
.returning()
|
||||
.get();
|
||||
|
||||
existing.push(createdRow);
|
||||
created++;
|
||||
index++;
|
||||
}
|
||||
|
||||
const repaired = repairDefaultToken(accountId);
|
||||
|
||||
return {
|
||||
created,
|
||||
updated,
|
||||
total: existing.length,
|
||||
defaultTokenId: repaired?.id || null,
|
||||
};
|
||||
}
|
||||
|
||||
export function listTokensWithRelations(accountId?: number) {
|
||||
const base = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id));
|
||||
|
||||
const rows = accountId
|
||||
? base.where(eq(schema.accountTokens.accountId, accountId)).all()
|
||||
: base.all();
|
||||
|
||||
return rows.map((row) => {
|
||||
const { token, ...tokenMeta } = row.account_tokens;
|
||||
return {
|
||||
...tokenMeta,
|
||||
tokenMasked: maskToken(token, row.sites.platform),
|
||||
account: {
|
||||
id: row.accounts.id,
|
||||
username: row.accounts.username,
|
||||
status: row.accounts.status,
|
||||
},
|
||||
site: {
|
||||
id: row.sites.id,
|
||||
name: row.sites.name,
|
||||
url: row.sites.url,
|
||||
platform: row.sites.platform,
|
||||
},
|
||||
};
|
||||
});
|
||||
}
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { isCloudflareChallenge, isTokenExpiredError } from './alertRules.js';
|
||||
|
||||
describe('alertRules', () => {
|
||||
it('detects cloudflare challenge messages', () => {
|
||||
expect(isCloudflareChallenge('Cloudflare challenge detected')).toBe(true);
|
||||
expect(isCloudflareChallenge('cf challenge required')).toBe(true);
|
||||
expect(isCloudflareChallenge('invalid token')).toBe(false);
|
||||
});
|
||||
|
||||
it('detects token expiration by status or message', () => {
|
||||
expect(isTokenExpiredError({ status: 401, message: 'Unauthorized' })).toBe(true);
|
||||
expect(isTokenExpiredError({ status: 403, message: 'Forbidden' })).toBe(true);
|
||||
expect(isTokenExpiredError({ message: 'jwt expired' })).toBe(true);
|
||||
expect(isTokenExpiredError({ message: 'token invalid' })).toBe(true);
|
||||
expect(isTokenExpiredError({ message: 'invalid access token' })).toBe(true);
|
||||
expect(isTokenExpiredError({ message: 'Token 无效' })).toBe(true);
|
||||
expect(isTokenExpiredError({ message: '无权进行此操作,未登录且未提供 access token' })).toBe(false);
|
||||
expect(isTokenExpiredError({ status: 500, message: 'upstream error' })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,29 @@
|
||||
export function isCloudflareChallenge(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
const text = message.toLowerCase();
|
||||
return text.includes('cloudflare') || text.includes('cf challenge') || text.includes('challenge required');
|
||||
}
|
||||
|
||||
export function isTokenExpiredError(input: { status?: number; message?: string | null }): boolean {
|
||||
if (input.status === 401 || input.status === 403) return true;
|
||||
const text = (input.message || '').toLowerCase();
|
||||
if (!text) return false;
|
||||
|
||||
// NewAPI-like sites may return this when session context is missing for an action,
|
||||
// which does not always mean the account token is expired.
|
||||
if (text.includes('未登录且未提供 access token')) return false;
|
||||
|
||||
const tokenPhrase = text.includes('token') || text.includes('令牌') || text.includes('访问令牌');
|
||||
const hasInvalid = text.includes('invalid') || text.includes('无效');
|
||||
const hasExpired = text.includes('expired') || text.includes('过期');
|
||||
|
||||
return (
|
||||
text.includes('jwt expired') ||
|
||||
text.includes('token expired') ||
|
||||
(tokenPhrase && (hasInvalid || hasExpired)) ||
|
||||
/invalid\s+access\s+token/.test(text) ||
|
||||
/access\s+token\s+is\s+invalid/.test(text) ||
|
||||
text.includes('unauthorized') ||
|
||||
text.includes('forbidden')
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,57 @@
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { sendNotification } from './notifyService.js';
|
||||
import { setAccountRuntimeHealth } from './accountHealthService.js';
|
||||
|
||||
export async function reportTokenExpired(params: {
|
||||
accountId: number;
|
||||
username?: string | null;
|
||||
siteName?: string | null;
|
||||
detail?: string;
|
||||
}) {
|
||||
const accountLabel = params.username || `ID:${params.accountId}`;
|
||||
const siteLabel = params.siteName || 'unknown-site';
|
||||
const detail = params.detail ? ` (${params.detail})` : '';
|
||||
|
||||
db.insert(schema.events).values({
|
||||
type: 'token',
|
||||
title: 'Token 已失效',
|
||||
message: `${accountLabel} @ ${siteLabel} 的 Token 无效或已过期${detail}`,
|
||||
level: 'error',
|
||||
relatedId: params.accountId,
|
||||
relatedType: 'account',
|
||||
}).run();
|
||||
|
||||
db.update(schema.accounts).set({
|
||||
status: 'expired',
|
||||
updatedAt: new Date().toISOString(),
|
||||
}).where(eq(schema.accounts.id, params.accountId)).run();
|
||||
|
||||
setAccountRuntimeHealth(params.accountId, {
|
||||
state: 'unhealthy',
|
||||
reason: params.detail ? `访问令牌失效:${params.detail}` : '访问令牌失效',
|
||||
source: 'auth',
|
||||
});
|
||||
|
||||
await sendNotification(
|
||||
'Token 已失效',
|
||||
`${accountLabel} @ ${siteLabel} 的 Token 无效或已过期${detail}`,
|
||||
'error',
|
||||
);
|
||||
}
|
||||
|
||||
export async function reportProxyAllFailed(params: { model: string; reason: string }) {
|
||||
db.insert(schema.events).values({
|
||||
type: 'proxy',
|
||||
title: '代理全部失败',
|
||||
message: `模型=${params.model}, 原因=${params.reason}`,
|
||||
level: 'error',
|
||||
relatedType: 'route',
|
||||
}).run();
|
||||
|
||||
await sendNotification(
|
||||
'代理全部失败',
|
||||
`模型=${params.model}, 原因=${params.reason}`,
|
||||
'error',
|
||||
);
|
||||
}
|
||||
@@ -0,0 +1,244 @@
|
||||
import { randomUUID } from 'node:crypto';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { sendNotification } from './notifyService.js';
|
||||
|
||||
export type BackgroundTaskStatus = 'pending' | 'running' | 'succeeded' | 'failed';
|
||||
|
||||
export type BackgroundTask = {
|
||||
id: string;
|
||||
type: string;
|
||||
title: string;
|
||||
status: BackgroundTaskStatus;
|
||||
message: string;
|
||||
error: string | null;
|
||||
result: unknown;
|
||||
dedupeKey: string | null;
|
||||
createdAt: string;
|
||||
updatedAt: string;
|
||||
startedAt: string | null;
|
||||
finishedAt: string | null;
|
||||
expiresAtMs: number;
|
||||
};
|
||||
|
||||
type TaskMessageTemplate = string | ((task: BackgroundTask) => string);
|
||||
|
||||
type BackgroundTaskStartOptions = {
|
||||
type: string;
|
||||
title: string;
|
||||
dedupeKey?: string;
|
||||
keepMs?: number;
|
||||
notifyOnSuccess?: boolean;
|
||||
notifyOnFailure?: boolean;
|
||||
successTitle?: TaskMessageTemplate;
|
||||
failureTitle?: TaskMessageTemplate;
|
||||
successMessage?: TaskMessageTemplate;
|
||||
failureMessage?: TaskMessageTemplate;
|
||||
};
|
||||
|
||||
const TASK_TTL_MS = 6 * 60 * 60 * 1000;
|
||||
const TASK_CLEANUP_INTERVAL_MS = 60 * 1000;
|
||||
|
||||
const tasks = new Map<string, BackgroundTask>();
|
||||
const dedupeTaskIds = new Map<string, string>();
|
||||
|
||||
function nowIso() {
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function summarizeError(error: unknown): string {
|
||||
if (error instanceof Error && error.message) return error.message;
|
||||
if (typeof error === 'string' && error.trim()) return error.trim();
|
||||
if (error && typeof error === 'object') {
|
||||
try {
|
||||
return JSON.stringify(error);
|
||||
} catch {
|
||||
return 'unknown error';
|
||||
}
|
||||
}
|
||||
return 'unknown error';
|
||||
}
|
||||
|
||||
function resolveTaskMessage(template: TaskMessageTemplate | undefined, task: BackgroundTask, fallback: string): string {
|
||||
if (typeof template === 'function') {
|
||||
try {
|
||||
const value = template(task);
|
||||
if (typeof value === 'string' && value.trim()) return value.trim();
|
||||
} catch {}
|
||||
return fallback;
|
||||
}
|
||||
if (typeof template === 'string' && template.trim()) return template.trim();
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function setTaskStatus(task: BackgroundTask, patch: Partial<BackgroundTask>) {
|
||||
const next: BackgroundTask = {
|
||||
...task,
|
||||
...patch,
|
||||
updatedAt: nowIso(),
|
||||
};
|
||||
tasks.set(task.id, next);
|
||||
return next;
|
||||
}
|
||||
|
||||
function appendTaskEvent(level: 'info' | 'warning' | 'error', title: string, message: string, taskId: string) {
|
||||
try {
|
||||
db.insert(schema.events).values({
|
||||
type: 'status',
|
||||
title,
|
||||
message,
|
||||
level,
|
||||
relatedType: 'task',
|
||||
createdAt: nowIso(),
|
||||
}).run();
|
||||
} catch {}
|
||||
void taskId;
|
||||
}
|
||||
|
||||
async function runTask(taskId: string, options: BackgroundTaskStartOptions, runner: () => Promise<unknown>) {
|
||||
const initialTask = tasks.get(taskId);
|
||||
if (!initialTask) return;
|
||||
|
||||
let task = setTaskStatus(initialTask, {
|
||||
status: 'running',
|
||||
startedAt: nowIso(),
|
||||
message: `${initialTask.title} 正在执行`,
|
||||
});
|
||||
|
||||
try {
|
||||
const result = await runner();
|
||||
task = setTaskStatus(task, {
|
||||
status: 'succeeded',
|
||||
finishedAt: nowIso(),
|
||||
result,
|
||||
error: null,
|
||||
});
|
||||
|
||||
const eventTitle = resolveTaskMessage(options.successTitle, task, `${task.title} 已完成`);
|
||||
const eventMessage = resolveTaskMessage(options.successMessage, task, `${task.title} 已完成`);
|
||||
task = setTaskStatus(task, { message: eventMessage });
|
||||
appendTaskEvent('info', eventTitle, eventMessage, task.id);
|
||||
|
||||
if (options.notifyOnSuccess) {
|
||||
await sendNotification(eventTitle, eventMessage, 'info');
|
||||
}
|
||||
} catch (error) {
|
||||
const errorText = summarizeError(error);
|
||||
task = setTaskStatus(task, {
|
||||
status: 'failed',
|
||||
finishedAt: nowIso(),
|
||||
error: errorText,
|
||||
message: `${task.title} 失败:${errorText}`,
|
||||
});
|
||||
|
||||
const eventTitle = resolveTaskMessage(options.failureTitle, task, `${task.title} 失败`);
|
||||
const eventMessage = resolveTaskMessage(options.failureMessage, task, task.message);
|
||||
task = setTaskStatus(task, { message: eventMessage });
|
||||
appendTaskEvent('error', eventTitle, eventMessage, task.id);
|
||||
|
||||
if (options.notifyOnFailure ?? true) {
|
||||
await sendNotification(eventTitle, eventMessage, 'error');
|
||||
}
|
||||
} finally {
|
||||
if (task.dedupeKey && dedupeTaskIds.get(task.dedupeKey) === task.id) {
|
||||
dedupeTaskIds.delete(task.dedupeKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function cleanupExpiredTasks() {
|
||||
const now = Date.now();
|
||||
for (const [taskId, task] of tasks.entries()) {
|
||||
if (task.expiresAtMs <= now) {
|
||||
tasks.delete(taskId);
|
||||
if (task.dedupeKey && dedupeTaskIds.get(task.dedupeKey) === taskId) {
|
||||
dedupeTaskIds.delete(task.dedupeKey);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
const cleanupTimer = setInterval(cleanupExpiredTasks, TASK_CLEANUP_INTERVAL_MS);
|
||||
cleanupTimer.unref?.();
|
||||
|
||||
export function startBackgroundTask(
|
||||
options: BackgroundTaskStartOptions,
|
||||
runner: () => Promise<unknown>,
|
||||
): { task: BackgroundTask; reused: boolean } {
|
||||
const dedupeKey = options.dedupeKey?.trim() || '';
|
||||
if (dedupeKey) {
|
||||
const existingTaskId = dedupeTaskIds.get(dedupeKey);
|
||||
if (existingTaskId) {
|
||||
const existing = tasks.get(existingTaskId);
|
||||
if (existing && (existing.status === 'pending' || existing.status === 'running')) {
|
||||
return { task: existing, reused: true };
|
||||
}
|
||||
dedupeTaskIds.delete(dedupeKey);
|
||||
}
|
||||
}
|
||||
|
||||
const createdAt = nowIso();
|
||||
const task: BackgroundTask = {
|
||||
id: randomUUID(),
|
||||
type: options.type,
|
||||
title: options.title,
|
||||
status: 'pending',
|
||||
message: `${options.title} 已开始执行`,
|
||||
error: null,
|
||||
result: null,
|
||||
dedupeKey: dedupeKey || null,
|
||||
createdAt,
|
||||
updatedAt: createdAt,
|
||||
startedAt: null,
|
||||
finishedAt: null,
|
||||
expiresAtMs: Date.now() + Math.max(60_000, options.keepMs ?? TASK_TTL_MS),
|
||||
};
|
||||
|
||||
tasks.set(task.id, task);
|
||||
if (dedupeKey) dedupeTaskIds.set(dedupeKey, task.id);
|
||||
|
||||
appendTaskEvent('info', `${task.title}已开始`, `${task.title} 已开始执行`, task.id);
|
||||
void runTask(task.id, options, runner);
|
||||
return { task, reused: false };
|
||||
}
|
||||
|
||||
export function getBackgroundTask(taskId: string): BackgroundTask | null {
|
||||
return tasks.get(taskId) || null;
|
||||
}
|
||||
|
||||
export function listBackgroundTasks(limit = 50): BackgroundTask[] {
|
||||
const safeLimit = Number.isFinite(limit) ? Math.max(1, Math.min(200, Math.trunc(limit))) : 50;
|
||||
return Array.from(tasks.values())
|
||||
.sort((a, b) => Date.parse(b.createdAt) - Date.parse(a.createdAt))
|
||||
.slice(0, safeLimit);
|
||||
}
|
||||
|
||||
export function getRunningTaskByDedupeKey(key: string): BackgroundTask | null {
|
||||
const taskId = dedupeTaskIds.get(key.trim());
|
||||
if (!taskId) return null;
|
||||
const task = tasks.get(taskId);
|
||||
if (!task) return null;
|
||||
if (task.status !== 'pending' && task.status !== 'running') return null;
|
||||
return task;
|
||||
}
|
||||
|
||||
export function summarizeCheckinResults(results: Array<{ result?: any }>): { total: number; success: number; skipped: number; failed: number } {
|
||||
const summary = { total: results.length, success: 0, skipped: 0, failed: 0 };
|
||||
for (const item of results) {
|
||||
const status = item?.result?.status;
|
||||
if (status === 'skipped' || item?.result?.skipped) {
|
||||
summary.skipped += 1;
|
||||
continue;
|
||||
}
|
||||
if (item?.result?.success) {
|
||||
summary.success += 1;
|
||||
continue;
|
||||
}
|
||||
summary.failed += 1;
|
||||
}
|
||||
return summary;
|
||||
}
|
||||
|
||||
export function __resetBackgroundTasksForTests() {
|
||||
tasks.clear();
|
||||
dedupeTaskIds.clear();
|
||||
}
|
||||
@@ -0,0 +1,604 @@
|
||||
import { asc } from 'drizzle-orm';
|
||||
import cron from 'node-cron';
|
||||
import { db, schema } from '../db/index.js';
|
||||
|
||||
const BACKUP_VERSION = '2.0';
|
||||
|
||||
export type BackupExportType = 'all' | 'accounts' | 'preferences';
|
||||
|
||||
type SiteRow = typeof schema.sites.$inferSelect;
|
||||
type AccountRow = typeof schema.accounts.$inferSelect;
|
||||
type AccountTokenRow = typeof schema.accountTokens.$inferSelect;
|
||||
type TokenRouteRow = typeof schema.tokenRoutes.$inferSelect;
|
||||
type RouteChannelRow = typeof schema.routeChannels.$inferSelect;
|
||||
|
||||
interface AccountsBackupSection {
|
||||
sites: SiteRow[];
|
||||
accounts: AccountRow[];
|
||||
accountTokens: AccountTokenRow[];
|
||||
tokenRoutes: TokenRouteRow[];
|
||||
routeChannels: RouteChannelRow[];
|
||||
}
|
||||
|
||||
interface PreferencesBackupSection {
|
||||
settings: Array<{ key: string; value: unknown }>;
|
||||
}
|
||||
|
||||
interface BackupFullV2 {
|
||||
version: string;
|
||||
timestamp: number;
|
||||
accounts: AccountsBackupSection;
|
||||
preferences: PreferencesBackupSection;
|
||||
}
|
||||
|
||||
interface BackupAccountsPartialV2 {
|
||||
version: string;
|
||||
timestamp: number;
|
||||
type: 'accounts';
|
||||
accounts: AccountsBackupSection;
|
||||
}
|
||||
|
||||
interface BackupPreferencesPartialV2 {
|
||||
version: string;
|
||||
timestamp: number;
|
||||
type: 'preferences';
|
||||
preferences: PreferencesBackupSection;
|
||||
}
|
||||
|
||||
type BackupV2 = BackupFullV2 | BackupAccountsPartialV2 | BackupPreferencesPartialV2;
|
||||
|
||||
type RawBackupData = Record<string, unknown>;
|
||||
|
||||
interface BackupImportResult {
|
||||
allImported: boolean;
|
||||
sections: {
|
||||
accounts: boolean;
|
||||
preferences: boolean;
|
||||
};
|
||||
appliedSettings: Array<{ key: string; value: unknown }>;
|
||||
}
|
||||
|
||||
const EXCLUDED_SETTING_KEYS = new Set<string>([
|
||||
// Keep current admin login credential unchanged to avoid accidental lock-out.
|
||||
'auth_token',
|
||||
]);
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === 'object' && !Array.isArray(value);
|
||||
}
|
||||
|
||||
function asString(value: unknown): string {
|
||||
if (typeof value !== 'string') return '';
|
||||
return value.trim();
|
||||
}
|
||||
|
||||
function asBoolean(value: unknown, fallback = false): boolean {
|
||||
if (typeof value === 'boolean') return value;
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function asNumber(value: unknown, fallback = 0): number {
|
||||
const n = Number(value);
|
||||
return Number.isFinite(n) ? n : fallback;
|
||||
}
|
||||
|
||||
function toIsoString(value: unknown): string {
|
||||
if (typeof value === 'string' && value.trim()) {
|
||||
const d = new Date(value);
|
||||
if (!Number.isNaN(d.getTime())) return d.toISOString();
|
||||
}
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
const d = new Date(value);
|
||||
if (!Number.isNaN(d.getTime())) return d.toISOString();
|
||||
}
|
||||
return new Date().toISOString();
|
||||
}
|
||||
|
||||
function normalizeLegacyQuota(raw: unknown): number {
|
||||
const value = asNumber(raw, 0);
|
||||
if (!Number.isFinite(value) || value <= 0) return 0;
|
||||
// ref-all-api-hub stores quota in raw units for NewAPI-like sites.
|
||||
// Convert obvious raw values to display currency units.
|
||||
if (value >= 10_000) return value / 500_000;
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeLegacyPlatform(raw: string): string {
|
||||
const value = raw.trim().toLowerCase();
|
||||
if (!value) return 'new-api';
|
||||
|
||||
const supported = new Set([
|
||||
'new-api',
|
||||
'one-api',
|
||||
'anyrouter',
|
||||
'one-hub',
|
||||
'done-hub',
|
||||
'sub2api',
|
||||
'veloera',
|
||||
]);
|
||||
if (supported.has(value)) return value;
|
||||
|
||||
if (value.includes('wong')) return 'new-api';
|
||||
if (value.includes('anyrouter')) return 'anyrouter';
|
||||
if (value.includes('done')) return 'done-hub';
|
||||
|
||||
return 'new-api';
|
||||
}
|
||||
|
||||
function buildAccountsSectionFromRefBackup(data: RawBackupData): AccountsBackupSection | null {
|
||||
const accountsContainer = isRecord(data.accounts) ? data.accounts : null;
|
||||
const rows = Array.isArray(accountsContainer?.accounts) ? accountsContainer.accounts : null;
|
||||
if (!rows) return null;
|
||||
|
||||
const sites: SiteRow[] = [];
|
||||
const accounts: AccountRow[] = [];
|
||||
const accountTokens: AccountTokenRow[] = [];
|
||||
const tokenRoutes: TokenRouteRow[] = [];
|
||||
const routeChannels: RouteChannelRow[] = [];
|
||||
|
||||
const siteIdByKey = new Map<string, number>();
|
||||
let nextSiteId = 1;
|
||||
let nextAccountId = 1;
|
||||
let nextTokenId = 1;
|
||||
|
||||
for (const item of rows) {
|
||||
if (!isRecord(item)) continue;
|
||||
|
||||
const siteUrl = asString(item.site_url);
|
||||
if (!siteUrl) continue;
|
||||
|
||||
const platform = normalizeLegacyPlatform(asString(item.site_type));
|
||||
const siteName = asString(item.site_name) || siteUrl;
|
||||
const siteKey = `${platform}::${siteUrl}`;
|
||||
|
||||
let siteId = siteIdByKey.get(siteKey) || 0;
|
||||
if (!siteId) {
|
||||
siteId = nextSiteId++;
|
||||
siteIdByKey.set(siteKey, siteId);
|
||||
sites.push({
|
||||
id: siteId,
|
||||
name: siteName,
|
||||
url: siteUrl,
|
||||
platform,
|
||||
status: 'active',
|
||||
apiKey: null,
|
||||
createdAt: toIsoString(item.created_at),
|
||||
updatedAt: toIsoString(item.updated_at),
|
||||
});
|
||||
}
|
||||
|
||||
const accountInfo = isRecord(item.account_info) ? item.account_info : {};
|
||||
const cookieAuth = isRecord(item.cookieAuth) ? item.cookieAuth : {};
|
||||
const authType = asString(item.authType);
|
||||
|
||||
const accountAccessToken =
|
||||
asString(accountInfo.access_token)
|
||||
|| asString(cookieAuth.sessionCookie)
|
||||
|| asString((item as Record<string, unknown>).access_token);
|
||||
if (!accountAccessToken) continue;
|
||||
|
||||
const platformUserId = asNumber(accountInfo.id, 0);
|
||||
const username = asString(accountInfo.username)
|
||||
|| asString(item.username)
|
||||
|| (platformUserId > 0 ? `user-${platformUserId}` : `account-${nextAccountId}`);
|
||||
|
||||
let apiToken: string | null = null;
|
||||
if (authType === 'api_key') {
|
||||
apiToken = accountAccessToken;
|
||||
}
|
||||
|
||||
const createdAt = toIsoString(item.created_at);
|
||||
const updatedAt = toIsoString(item.updated_at);
|
||||
const checkin = isRecord(item.checkIn) ? item.checkIn : {};
|
||||
const extraConfigPayload = {
|
||||
platformUserId: platformUserId > 0 ? platformUserId : undefined,
|
||||
authType: authType || undefined,
|
||||
source: 'ref-all-api-hub',
|
||||
};
|
||||
|
||||
const accountId = nextAccountId++;
|
||||
const importedBalance = normalizeLegacyQuota(accountInfo.quota);
|
||||
const importedUsed = normalizeLegacyQuota(accountInfo.today_quota_consumption);
|
||||
const importedQuota = importedBalance + importedUsed;
|
||||
|
||||
accounts.push({
|
||||
id: accountId,
|
||||
siteId,
|
||||
username,
|
||||
accessToken: accountAccessToken,
|
||||
apiToken,
|
||||
balance: importedBalance,
|
||||
balanceUsed: importedUsed,
|
||||
quota: importedQuota > 0 ? importedQuota : importedBalance,
|
||||
unitCost: null,
|
||||
valueScore: 0,
|
||||
status: asBoolean(item.disabled, false) ? 'disabled' : 'active',
|
||||
checkinEnabled: asBoolean(checkin.autoCheckInEnabled, true),
|
||||
lastCheckinAt: null,
|
||||
lastBalanceRefresh: null,
|
||||
extraConfig: JSON.stringify(extraConfigPayload),
|
||||
createdAt,
|
||||
updatedAt,
|
||||
});
|
||||
|
||||
if (apiToken) {
|
||||
accountTokens.push({
|
||||
id: nextTokenId++,
|
||||
accountId,
|
||||
name: 'default',
|
||||
token: apiToken,
|
||||
source: 'legacy',
|
||||
enabled: true,
|
||||
isDefault: true,
|
||||
createdAt,
|
||||
updatedAt,
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
sites,
|
||||
accounts,
|
||||
accountTokens,
|
||||
tokenRoutes,
|
||||
routeChannels,
|
||||
};
|
||||
}
|
||||
|
||||
function buildPreferencesSectionFromRefBackup(data: RawBackupData): PreferencesBackupSection | null {
|
||||
const settings: Array<{ key: string; value: unknown }> = [];
|
||||
|
||||
if (isRecord(data.preferences)) {
|
||||
settings.push({ key: 'legacy_preferences_ref_v2', value: data.preferences });
|
||||
}
|
||||
if (isRecord(data.channelConfigs)) {
|
||||
settings.push({ key: 'legacy_channel_configs_ref_v2', value: data.channelConfigs });
|
||||
}
|
||||
if (isRecord(data.apiCredentialProfiles)) {
|
||||
settings.push({ key: 'legacy_api_credential_profiles_ref_v2', value: data.apiCredentialProfiles });
|
||||
}
|
||||
if (isRecord(data.tagStore)) {
|
||||
settings.push({ key: 'legacy_tag_store_ref_v2', value: data.tagStore });
|
||||
}
|
||||
|
||||
if (settings.length === 0) return null;
|
||||
return { settings };
|
||||
}
|
||||
|
||||
function parseSettingValue(raw: string | null): unknown {
|
||||
if (raw === null || raw === undefined) return null;
|
||||
try {
|
||||
return JSON.parse(raw);
|
||||
} catch {
|
||||
return raw;
|
||||
}
|
||||
}
|
||||
|
||||
function stringifySettingValue(value: unknown): string {
|
||||
return JSON.stringify(value);
|
||||
}
|
||||
|
||||
function isFiniteNumber(value: unknown): value is number {
|
||||
return typeof value === 'number' && Number.isFinite(value);
|
||||
}
|
||||
|
||||
function isSettingValueAcceptable(key: string, value: unknown): boolean {
|
||||
if (key === 'checkin_cron' || key === 'balance_refresh_cron') {
|
||||
return typeof value === 'string' && cron.validate(value);
|
||||
}
|
||||
|
||||
if (key === 'proxy_token') {
|
||||
return typeof value === 'string'
|
||||
&& value.trim().length >= 6
|
||||
&& value.trim().startsWith('sk-');
|
||||
}
|
||||
|
||||
if (key === 'smtp_port') {
|
||||
return isFiniteNumber(value) && value > 0;
|
||||
}
|
||||
|
||||
if (key === 'routing_weights') {
|
||||
if (!isRecord(value)) return false;
|
||||
const keys = ['baseWeightFactor', 'valueScoreFactor', 'costWeight', 'balanceWeight', 'usageWeight'] as const;
|
||||
return keys.every((weightKey) => value[weightKey] === undefined || isFiniteNumber(value[weightKey]));
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
function exportAccountsSection(): AccountsBackupSection {
|
||||
const sites = db.select().from(schema.sites).orderBy(asc(schema.sites.id)).all();
|
||||
const accounts = db.select().from(schema.accounts).orderBy(asc(schema.accounts.id)).all();
|
||||
const accountTokens = db.select().from(schema.accountTokens).orderBy(asc(schema.accountTokens.id)).all();
|
||||
const tokenRoutes = db.select().from(schema.tokenRoutes).orderBy(asc(schema.tokenRoutes.id)).all();
|
||||
const routeChannels = db.select().from(schema.routeChannels).orderBy(asc(schema.routeChannels.id)).all();
|
||||
|
||||
return {
|
||||
sites,
|
||||
accounts,
|
||||
accountTokens,
|
||||
tokenRoutes,
|
||||
routeChannels,
|
||||
};
|
||||
}
|
||||
|
||||
function exportPreferencesSection(): PreferencesBackupSection {
|
||||
const settings = db.select().from(schema.settings).all()
|
||||
.filter((row) => !EXCLUDED_SETTING_KEYS.has(row.key))
|
||||
.map((row) => ({
|
||||
key: row.key,
|
||||
value: parseSettingValue(row.value),
|
||||
}));
|
||||
|
||||
return { settings };
|
||||
}
|
||||
|
||||
export function exportBackup(type: BackupExportType): BackupV2 {
|
||||
const now = Date.now();
|
||||
if (type === 'accounts') {
|
||||
return {
|
||||
version: BACKUP_VERSION,
|
||||
timestamp: now,
|
||||
type: 'accounts',
|
||||
accounts: exportAccountsSection(),
|
||||
};
|
||||
}
|
||||
|
||||
if (type === 'preferences') {
|
||||
return {
|
||||
version: BACKUP_VERSION,
|
||||
timestamp: now,
|
||||
type: 'preferences',
|
||||
preferences: exportPreferencesSection(),
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
version: BACKUP_VERSION,
|
||||
timestamp: now,
|
||||
accounts: exportAccountsSection(),
|
||||
preferences: exportPreferencesSection(),
|
||||
};
|
||||
}
|
||||
|
||||
function coerceAccountsSection(input: unknown): AccountsBackupSection | null {
|
||||
if (!isRecord(input)) return null;
|
||||
|
||||
const sites = Array.isArray(input.sites) ? input.sites as SiteRow[] : null;
|
||||
const accounts = Array.isArray(input.accounts) ? input.accounts as AccountRow[] : null;
|
||||
const accountTokens = Array.isArray(input.accountTokens) ? input.accountTokens as AccountTokenRow[] : null;
|
||||
const tokenRoutes = Array.isArray(input.tokenRoutes) ? input.tokenRoutes as TokenRouteRow[] : null;
|
||||
const routeChannels = Array.isArray(input.routeChannels) ? input.routeChannels as RouteChannelRow[] : null;
|
||||
|
||||
if (!sites || !accounts || !accountTokens || !tokenRoutes || !routeChannels) return null;
|
||||
|
||||
return {
|
||||
sites,
|
||||
accounts,
|
||||
accountTokens,
|
||||
tokenRoutes,
|
||||
routeChannels,
|
||||
};
|
||||
}
|
||||
|
||||
function coercePreferencesSection(input: unknown): PreferencesBackupSection | null {
|
||||
if (!isRecord(input)) return null;
|
||||
const settingsRaw = input.settings;
|
||||
if (!Array.isArray(settingsRaw)) return null;
|
||||
|
||||
const settings = settingsRaw
|
||||
.map((row) => {
|
||||
if (!isRecord(row)) return null;
|
||||
const key = typeof row.key === 'string' ? row.key.trim() : '';
|
||||
if (!key || EXCLUDED_SETTING_KEYS.has(key)) return null;
|
||||
return { key, value: row.value };
|
||||
})
|
||||
.filter((row): row is { key: string; value: unknown } => !!row);
|
||||
|
||||
return { settings };
|
||||
}
|
||||
|
||||
function detectAccountsSection(data: RawBackupData): AccountsBackupSection | null {
|
||||
const rootMatch = coerceAccountsSection(data);
|
||||
if (rootMatch) return rootMatch;
|
||||
|
||||
if ('accounts' in data) {
|
||||
const nested = coerceAccountsSection(data.accounts);
|
||||
if (nested) return nested;
|
||||
}
|
||||
|
||||
if (isRecord(data.data) && 'accounts' in data.data) {
|
||||
const legacyNested = coerceAccountsSection((data.data as Record<string, unknown>).accounts);
|
||||
if (legacyNested) return legacyNested;
|
||||
}
|
||||
|
||||
const refFormat = buildAccountsSectionFromRefBackup(data);
|
||||
if (refFormat) return refFormat;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function detectPreferencesSection(data: RawBackupData): PreferencesBackupSection | null {
|
||||
const rootMatch = coercePreferencesSection(data);
|
||||
if (rootMatch) return rootMatch;
|
||||
|
||||
if ('preferences' in data) {
|
||||
const nested = coercePreferencesSection(data.preferences);
|
||||
if (nested) return nested;
|
||||
}
|
||||
|
||||
if (isRecord(data.data) && 'preferences' in data.data) {
|
||||
const legacyNested = coercePreferencesSection((data.data as Record<string, unknown>).preferences);
|
||||
if (legacyNested) return legacyNested;
|
||||
}
|
||||
|
||||
const refFormat = buildPreferencesSectionFromRefBackup(data);
|
||||
if (refFormat) return refFormat;
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function importAccountsSection(section: AccountsBackupSection) {
|
||||
db.transaction((tx) => {
|
||||
tx.delete(schema.routeChannels).run();
|
||||
tx.delete(schema.tokenRoutes).run();
|
||||
tx.delete(schema.tokenModelAvailability).run();
|
||||
tx.delete(schema.modelAvailability).run();
|
||||
tx.delete(schema.proxyLogs).run();
|
||||
tx.delete(schema.checkinLogs).run();
|
||||
tx.delete(schema.accountTokens).run();
|
||||
tx.delete(schema.accounts).run();
|
||||
tx.delete(schema.sites).run();
|
||||
|
||||
for (const row of section.sites) {
|
||||
tx.insert(schema.sites).values({
|
||||
id: row.id,
|
||||
name: row.name,
|
||||
url: row.url,
|
||||
platform: row.platform,
|
||||
status: row.status || 'active',
|
||||
apiKey: row.apiKey,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
}).run();
|
||||
}
|
||||
|
||||
for (const row of section.accounts) {
|
||||
tx.insert(schema.accounts).values({
|
||||
id: row.id,
|
||||
siteId: row.siteId,
|
||||
username: row.username,
|
||||
accessToken: row.accessToken,
|
||||
apiToken: row.apiToken,
|
||||
balance: row.balance,
|
||||
balanceUsed: row.balanceUsed,
|
||||
quota: row.quota,
|
||||
unitCost: row.unitCost,
|
||||
valueScore: row.valueScore,
|
||||
status: row.status,
|
||||
checkinEnabled: row.checkinEnabled,
|
||||
lastCheckinAt: row.lastCheckinAt,
|
||||
lastBalanceRefresh: row.lastBalanceRefresh,
|
||||
extraConfig: row.extraConfig,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
}).run();
|
||||
}
|
||||
|
||||
for (const row of section.accountTokens) {
|
||||
tx.insert(schema.accountTokens).values({
|
||||
id: row.id,
|
||||
accountId: row.accountId,
|
||||
name: row.name,
|
||||
token: row.token,
|
||||
source: row.source,
|
||||
enabled: row.enabled,
|
||||
isDefault: row.isDefault,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
}).run();
|
||||
}
|
||||
|
||||
for (const row of section.tokenRoutes) {
|
||||
tx.insert(schema.tokenRoutes).values({
|
||||
id: row.id,
|
||||
modelPattern: row.modelPattern,
|
||||
modelMapping: row.modelMapping,
|
||||
enabled: row.enabled,
|
||||
createdAt: row.createdAt,
|
||||
updatedAt: row.updatedAt,
|
||||
}).run();
|
||||
}
|
||||
|
||||
for (const row of section.routeChannels) {
|
||||
tx.insert(schema.routeChannels).values({
|
||||
id: row.id,
|
||||
routeId: row.routeId,
|
||||
accountId: row.accountId,
|
||||
tokenId: row.tokenId,
|
||||
priority: row.priority,
|
||||
weight: row.weight,
|
||||
enabled: row.enabled,
|
||||
manualOverride: row.manualOverride,
|
||||
successCount: row.successCount,
|
||||
failCount: row.failCount,
|
||||
totalLatencyMs: row.totalLatencyMs,
|
||||
totalCost: row.totalCost,
|
||||
lastUsedAt: row.lastUsedAt,
|
||||
lastFailAt: row.lastFailAt,
|
||||
cooldownUntil: row.cooldownUntil,
|
||||
}).run();
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function importPreferencesSection(section: PreferencesBackupSection): Array<{ key: string; value: unknown }> {
|
||||
const applied: Array<{ key: string; value: unknown }> = [];
|
||||
|
||||
db.transaction((tx) => {
|
||||
for (const row of section.settings) {
|
||||
if (!isSettingValueAcceptable(row.key, row.value)) continue;
|
||||
|
||||
tx.insert(schema.settings).values({
|
||||
key: row.key,
|
||||
value: stringifySettingValue(row.value),
|
||||
}).onConflictDoUpdate({
|
||||
target: schema.settings.key,
|
||||
set: { value: stringifySettingValue(row.value) },
|
||||
}).run();
|
||||
applied.push({ key: row.key, value: row.value });
|
||||
}
|
||||
});
|
||||
|
||||
return applied;
|
||||
}
|
||||
|
||||
export function importBackup(data: RawBackupData): BackupImportResult {
|
||||
if (!isRecord(data)) {
|
||||
throw new Error('导入数据格式错误:必须为 JSON 对象');
|
||||
}
|
||||
|
||||
if (!('timestamp' in data) || data.timestamp === null || data.timestamp === undefined) {
|
||||
throw new Error('导入数据格式错误:缺少 timestamp');
|
||||
}
|
||||
|
||||
const accountsSection = detectAccountsSection(data);
|
||||
const preferencesSection = detectPreferencesSection(data);
|
||||
|
||||
const type = typeof data.type === 'string' ? data.type : '';
|
||||
const accountsRequested = type === 'accounts' || !!accountsSection;
|
||||
const preferencesRequested = type === 'preferences' || !!preferencesSection;
|
||||
|
||||
if (!accountsRequested && !preferencesRequested) {
|
||||
throw new Error('导入数据中没有可识别的账号或设置数据');
|
||||
}
|
||||
|
||||
let accountsImported = false;
|
||||
let preferencesImported = false;
|
||||
let appliedSettings: Array<{ key: string; value: unknown }> = [];
|
||||
|
||||
if (accountsRequested) {
|
||||
if (!accountsSection) {
|
||||
throw new Error('导入数据格式错误:账号数据结构不正确');
|
||||
}
|
||||
importAccountsSection(accountsSection);
|
||||
accountsImported = true;
|
||||
}
|
||||
|
||||
if (preferencesRequested) {
|
||||
if (!preferencesSection) {
|
||||
throw new Error('导入数据格式错误:设置数据结构不正确');
|
||||
}
|
||||
appliedSettings = importPreferencesSection(preferencesSection);
|
||||
preferencesImported = true;
|
||||
}
|
||||
|
||||
return {
|
||||
allImported: (!accountsRequested || accountsImported) && (!preferencesRequested || preferencesImported),
|
||||
sections: {
|
||||
accounts: accountsImported,
|
||||
preferences: preferencesImported,
|
||||
},
|
||||
appliedSettings,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,267 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const adapterMock = {
|
||||
getBalance: vi.fn(),
|
||||
login: vi.fn(),
|
||||
};
|
||||
|
||||
const selectAllMock = vi.fn();
|
||||
const updateSetMock = vi.fn();
|
||||
const insertValuesMock = vi.fn();
|
||||
const reportTokenExpiredMock = vi.fn();
|
||||
const sendNotificationMock = vi.fn();
|
||||
const decryptPasswordMock = vi.fn();
|
||||
const setAccountRuntimeHealthMock = vi.fn();
|
||||
const extractRuntimeHealthMock = vi.fn();
|
||||
const undiciFetchMock = vi.fn();
|
||||
|
||||
vi.mock('../db/index.js', () => {
|
||||
const selectChain = {
|
||||
all: () => selectAllMock(),
|
||||
where: () => selectChain,
|
||||
innerJoin: () => selectChain,
|
||||
from: () => selectChain,
|
||||
};
|
||||
|
||||
const updateWhereChain = {
|
||||
run: () => ({}),
|
||||
};
|
||||
|
||||
const updateSetChain = {
|
||||
where: () => updateWhereChain,
|
||||
};
|
||||
|
||||
const insertChain = {
|
||||
run: () => ({}),
|
||||
values: (...args: unknown[]) => {
|
||||
insertValuesMock(...args);
|
||||
return insertChain;
|
||||
},
|
||||
};
|
||||
|
||||
return {
|
||||
db: {
|
||||
select: () => selectChain,
|
||||
update: () => ({
|
||||
set: (updates: Record<string, unknown>) => {
|
||||
updateSetMock(updates);
|
||||
return updateSetChain;
|
||||
},
|
||||
}),
|
||||
insert: () => insertChain,
|
||||
},
|
||||
schema: {
|
||||
accounts: { id: 'id', siteId: 'siteId', status: 'status' },
|
||||
sites: { id: 'id' },
|
||||
events: {},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('./platforms/index.js', () => ({
|
||||
getAdapter: () => adapterMock,
|
||||
}));
|
||||
|
||||
vi.mock('./alertService.js', () => ({
|
||||
reportTokenExpired: (...args: unknown[]) => reportTokenExpiredMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./notifyService.js', () => ({
|
||||
sendNotification: (...args: unknown[]) => sendNotificationMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./accountCredentialService.js', () => ({
|
||||
decryptAccountPassword: (...args: unknown[]) => decryptPasswordMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./accountHealthService.js', () => ({
|
||||
setAccountRuntimeHealth: (...args: unknown[]) => setAccountRuntimeHealthMock(...args),
|
||||
extractRuntimeHealth: (...args: unknown[]) => extractRuntimeHealthMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('undici', () => ({
|
||||
fetch: (...args: unknown[]) => undiciFetchMock(...args),
|
||||
}));
|
||||
|
||||
describe('balanceService auto relogin', () => {
|
||||
beforeEach(() => {
|
||||
adapterMock.getBalance.mockReset();
|
||||
adapterMock.login.mockReset();
|
||||
selectAllMock.mockReset();
|
||||
updateSetMock.mockReset();
|
||||
insertValuesMock.mockReset();
|
||||
reportTokenExpiredMock.mockReset();
|
||||
sendNotificationMock.mockReset();
|
||||
decryptPasswordMock.mockReset();
|
||||
setAccountRuntimeHealthMock.mockReset();
|
||||
extractRuntimeHealthMock.mockReset();
|
||||
undiciFetchMock.mockReset();
|
||||
|
||||
extractRuntimeHealthMock.mockReturnValue(null);
|
||||
undiciFetchMock.mockResolvedValue({
|
||||
ok: false,
|
||||
json: async () => ({}),
|
||||
});
|
||||
});
|
||||
|
||||
it('retries balance fetch once after successful auto relogin', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 1,
|
||||
username: 'linuxdo_11494',
|
||||
accessToken: 'stale-token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
platformUserId: 11494,
|
||||
autoRelogin: { username: 'linuxdo_11494', passwordCipher: 'cipher' },
|
||||
}),
|
||||
},
|
||||
sites: {
|
||||
id: 3,
|
||||
name: 'wong',
|
||||
url: 'https://wzw.pp.ua',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.getBalance
|
||||
.mockRejectedValueOnce(new Error('HTTP 401: access token required'))
|
||||
.mockResolvedValueOnce({ balance: 12, used: 1, quota: 13 });
|
||||
decryptPasswordMock.mockReturnValue('plain-password');
|
||||
adapterMock.login.mockResolvedValue({ success: true, accessToken: 'fresh-token' });
|
||||
|
||||
const { refreshBalance } = await import('./balanceService.js');
|
||||
const result = await refreshBalance(1);
|
||||
|
||||
expect(result).toEqual({ balance: 12, used: 1, quota: 13 });
|
||||
expect(adapterMock.login).toHaveBeenCalledTimes(1);
|
||||
expect(adapterMock.getBalance).toHaveBeenCalledTimes(2);
|
||||
expect(adapterMock.getBalance.mock.calls[0][1]).toBe('stale-token');
|
||||
expect(adapterMock.getBalance.mock.calls[1][1]).toBe('fresh-token');
|
||||
expect(updateSetMock.mock.calls.some((call) => call[0]?.accessToken === 'fresh-token')).toBe(true);
|
||||
expect(reportTokenExpiredMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('reports token expired when relogin is unavailable', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 2,
|
||||
username: 'linuxdo_7659',
|
||||
accessToken: 'stale-token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 4,
|
||||
name: 'kfc',
|
||||
url: 'https://kfc-api.sxxe.net',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.getBalance.mockRejectedValueOnce(new Error('HTTP 401: access token required'));
|
||||
|
||||
const { refreshBalance } = await import('./balanceService.js');
|
||||
await expect(refreshBalance(2)).rejects.toThrow('access token');
|
||||
|
||||
expect(adapterMock.login).not.toHaveBeenCalled();
|
||||
expect(reportTokenExpiredMock).toHaveBeenCalledTimes(1);
|
||||
});
|
||||
|
||||
it('keeps degraded health when checkin is unsupported but balance refresh succeeds', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 3,
|
||||
username: 'ld6jl3djexjf',
|
||||
accessToken: 'active-token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
runtimeHealth: {
|
||||
state: 'degraded',
|
||||
reason: 'checkin endpoint not found',
|
||||
source: 'checkin',
|
||||
},
|
||||
}),
|
||||
},
|
||||
sites: {
|
||||
id: 5,
|
||||
name: 'Wind Hub',
|
||||
url: 'https://windhub.cc',
|
||||
platform: 'done-hub',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.getBalance.mockResolvedValueOnce({ balance: 100, used: 2, quota: 102 });
|
||||
extractRuntimeHealthMock.mockReturnValue({
|
||||
state: 'degraded',
|
||||
reason: 'checkin endpoint not found',
|
||||
source: 'checkin',
|
||||
checkedAt: '2026-02-25T12:00:00.000Z',
|
||||
});
|
||||
|
||||
const { refreshBalance } = await import('./balanceService.js');
|
||||
const result = await refreshBalance(3);
|
||||
|
||||
expect(result).toEqual({ balance: 100, used: 2, quota: 102 });
|
||||
expect(setAccountRuntimeHealthMock).toHaveBeenCalledWith(
|
||||
3,
|
||||
expect.objectContaining({
|
||||
state: 'degraded',
|
||||
reason: 'checkin endpoint not found',
|
||||
}),
|
||||
);
|
||||
});
|
||||
|
||||
it('fills today income snapshot from log endpoint when balance api lacks today_income', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 4,
|
||||
username: 'linuxdo_7659',
|
||||
accessToken: 'active-token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 6,
|
||||
name: 'kfc',
|
||||
url: 'https://kfc-api.sxxe.net',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.getBalance.mockResolvedValueOnce({ balance: 12, used: 1, quota: 13 });
|
||||
undiciFetchMock
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({ data: { items: [], total: 0 } }),
|
||||
})
|
||||
.mockResolvedValueOnce({
|
||||
ok: true,
|
||||
json: async () => ({
|
||||
data: {
|
||||
items: [{ quota: 0, content: '签到奖励 2.083650 额度' }],
|
||||
total: 1,
|
||||
},
|
||||
}),
|
||||
});
|
||||
|
||||
const { refreshBalance } = await import('./balanceService.js');
|
||||
const result = await refreshBalance(4);
|
||||
|
||||
expect(result).toEqual({ balance: 12, used: 1, quota: 13, todayIncome: 2.08365 });
|
||||
const updateWithSnapshot = updateSetMock.mock.calls
|
||||
.map((call) => call[0] as Record<string, unknown>)
|
||||
.find((payload) => typeof payload.extraConfig === 'string');
|
||||
expect(updateWithSnapshot).toBeDefined();
|
||||
const parsedExtra = JSON.parse(String(updateWithSnapshot?.extraConfig));
|
||||
expect(parsedExtra.todayIncomeSnapshot?.latest).toBeCloseTo(2.08365, 6);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,391 @@
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { getAdapter } from './platforms/index.js';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { sendNotification } from './notifyService.js';
|
||||
import { isTokenExpiredError } from './alertRules.js';
|
||||
import { reportTokenExpired } from './alertService.js';
|
||||
import { getAutoReloginConfig, resolvePlatformUserId } from './accountExtraConfig.js';
|
||||
import { decryptAccountPassword } from './accountCredentialService.js';
|
||||
import { extractRuntimeHealth, setAccountRuntimeHealth } from './accountHealthService.js';
|
||||
import { updateTodayIncomeSnapshot } from './todayIncomeRewardService.js';
|
||||
import type { BalanceInfo } from './platforms/base.js';
|
||||
|
||||
function isSiteDisabled(status?: string | null): boolean {
|
||||
return (status || 'active') === 'disabled';
|
||||
}
|
||||
|
||||
function shouldAttemptAutoRelogin(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
if (isTokenExpiredError({ message })) return true;
|
||||
|
||||
const text = message.toLowerCase();
|
||||
return (
|
||||
text.includes('access token') ||
|
||||
text.includes('new-api-user') ||
|
||||
text.includes('unauthorized') ||
|
||||
text.includes('forbidden') ||
|
||||
text.includes('not login') ||
|
||||
text.includes('not logged')
|
||||
);
|
||||
}
|
||||
|
||||
function shouldReportExpired(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
if (isTokenExpiredError({ message })) return true;
|
||||
|
||||
const text = message.toLowerCase();
|
||||
return (
|
||||
text.includes('access token') ||
|
||||
text.includes('new-api-user') ||
|
||||
text.includes('unauthorized') ||
|
||||
text.includes('forbidden')
|
||||
);
|
||||
}
|
||||
|
||||
function isUnsupportedCheckinRuntimeHealth(health: ReturnType<typeof extractRuntimeHealth>): boolean {
|
||||
if (!health || health.state !== 'degraded') return false;
|
||||
if ((health.source || '').toLowerCase() === 'checkin') return true;
|
||||
const reason = (health.reason || '').toLowerCase();
|
||||
return (
|
||||
reason.includes('checkin endpoint not found') ||
|
||||
reason.includes('invalid url (post /api/user/checkin)') ||
|
||||
(reason.includes('http 404') && reason.includes('/api/user/checkin')) ||
|
||||
reason.includes('unsupported checkin endpoint')
|
||||
);
|
||||
}
|
||||
|
||||
const INCOME_LOG_TYPES = [1, 4] as const;
|
||||
const LOG_PAGE_SIZE = 100;
|
||||
const LOG_MAX_PAGES = 6;
|
||||
|
||||
function supportsTodayIncomeLogFallback(platform?: string | null): boolean {
|
||||
const normalized = (platform || '').toLowerCase();
|
||||
return (
|
||||
normalized === 'new-api' ||
|
||||
normalized === 'anyrouter' ||
|
||||
normalized === 'one-api' ||
|
||||
normalized === 'veloera'
|
||||
);
|
||||
}
|
||||
|
||||
function getTodayUnixSecondsRange(now = new Date()): { start: number; end: number } {
|
||||
const startDate = new Date(now);
|
||||
startDate.setHours(0, 0, 0, 0);
|
||||
|
||||
const endDate = new Date(now);
|
||||
endDate.setHours(23, 59, 59, 999);
|
||||
|
||||
return {
|
||||
start: Math.floor(startDate.getTime() / 1000),
|
||||
end: Math.floor(endDate.getTime() / 1000),
|
||||
};
|
||||
}
|
||||
|
||||
function parsePositiveNumber(value: unknown): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value) && value > 0) return value;
|
||||
if (typeof value === 'string') {
|
||||
const parsed = Number(value.trim());
|
||||
if (Number.isFinite(parsed) && parsed > 0) return parsed;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function parseIncomeFromContent(content: unknown): number {
|
||||
if (typeof content !== 'string') return 0;
|
||||
const normalized = content.replace(/,/g, '');
|
||||
const match = normalized.match(/[-+]?\d+(?:\.\d+)?/);
|
||||
if (!match) return 0;
|
||||
const parsed = Number(match[0]);
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) return 0;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
function extractLogItems(payload: any): Array<{ quota?: unknown; content?: unknown }> {
|
||||
if (Array.isArray(payload?.data?.items)) return payload.data.items;
|
||||
if (Array.isArray(payload?.items)) return payload.items;
|
||||
if (Array.isArray(payload?.data)) return payload.data;
|
||||
return [];
|
||||
}
|
||||
|
||||
function extractLogTotal(payload: any): number | null {
|
||||
const candidates = [payload?.data?.total, payload?.total];
|
||||
for (const value of candidates) {
|
||||
if (typeof value === 'number' && Number.isFinite(value) && value >= 0) return value;
|
||||
if (typeof value === 'string') {
|
||||
const parsed = Number.parseInt(value.trim(), 10);
|
||||
if (!Number.isNaN(parsed) && parsed >= 0) return parsed;
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveQuotaConversionFactor(platform?: string | null): number {
|
||||
return (platform || '').toLowerCase() === 'veloera' ? 1_000_000 : 500_000;
|
||||
}
|
||||
|
||||
async function fetchTodayIncomeFromLogs(params: {
|
||||
baseUrl: string;
|
||||
accessToken: string;
|
||||
platform?: string | null;
|
||||
platformUserId?: number;
|
||||
}): Promise<number | null> {
|
||||
const baseUrl = params.baseUrl.trim();
|
||||
const accessToken = params.accessToken.trim();
|
||||
if (!baseUrl || !accessToken) return null;
|
||||
|
||||
const { fetch } = await import('undici');
|
||||
const { start, end } = getTodayUnixSecondsRange();
|
||||
const conversionFactor = resolveQuotaConversionFactor(params.platform);
|
||||
const headers: Record<string, string> = {
|
||||
Authorization: `Bearer ${accessToken}`,
|
||||
'Content-Type': 'application/json',
|
||||
};
|
||||
if (typeof params.platformUserId === 'number' && Number.isFinite(params.platformUserId)) {
|
||||
headers['New-Api-User'] = String(Math.trunc(params.platformUserId));
|
||||
}
|
||||
|
||||
let hasAnyLogResponse = false;
|
||||
let totalIncome = 0;
|
||||
|
||||
for (const logType of INCOME_LOG_TYPES) {
|
||||
let page = 1;
|
||||
while (page <= LOG_MAX_PAGES) {
|
||||
const query = new URLSearchParams({
|
||||
p: String(page),
|
||||
page_size: String(LOG_PAGE_SIZE),
|
||||
type: String(logType),
|
||||
token_name: '',
|
||||
model_name: '',
|
||||
start_timestamp: String(start),
|
||||
end_timestamp: String(end),
|
||||
group: '',
|
||||
});
|
||||
|
||||
try {
|
||||
const response = await fetch(`${baseUrl}/api/log/self?${query.toString()}`, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
});
|
||||
if (!response.ok) break;
|
||||
|
||||
const payload = await response.json().catch(() => null);
|
||||
if (!payload || typeof payload !== 'object') break;
|
||||
hasAnyLogResponse = true;
|
||||
|
||||
const items = extractLogItems(payload);
|
||||
for (const item of items) {
|
||||
const quotaRaw = parsePositiveNumber(item?.quota);
|
||||
if (quotaRaw > 0) {
|
||||
totalIncome += quotaRaw / conversionFactor;
|
||||
continue;
|
||||
}
|
||||
totalIncome += parseIncomeFromContent(item?.content);
|
||||
}
|
||||
|
||||
const total = extractLogTotal(payload);
|
||||
if (items.length === 0) break;
|
||||
if (total != null && page * LOG_PAGE_SIZE >= total) break;
|
||||
page += 1;
|
||||
} catch {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (!hasAnyLogResponse) return null;
|
||||
return Math.round(totalIncome * 1_000_000) / 1_000_000;
|
||||
}
|
||||
|
||||
async function tryAutoRelogin(account: any, site: any): Promise<string | null> {
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return null;
|
||||
|
||||
const relogin = getAutoReloginConfig(account.extraConfig);
|
||||
if (!relogin) return null;
|
||||
|
||||
const password = decryptAccountPassword(relogin.passwordCipher);
|
||||
if (!password) return null;
|
||||
|
||||
const loginResult = await adapter.login(site.url, relogin.username, password);
|
||||
if (!loginResult.success || !loginResult.accessToken) return null;
|
||||
|
||||
db.update(schema.accounts)
|
||||
.set({
|
||||
accessToken: loginResult.accessToken,
|
||||
status: account.status === 'expired' ? 'active' : account.status,
|
||||
updatedAt: new Date().toISOString(),
|
||||
})
|
||||
.where(eq(schema.accounts.id, account.id))
|
||||
.run();
|
||||
|
||||
return loginResult.accessToken;
|
||||
}
|
||||
|
||||
export async function refreshBalance(accountId: number) {
|
||||
const rows = db
|
||||
.select()
|
||||
.from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.all();
|
||||
|
||||
if (rows.length === 0) return null;
|
||||
|
||||
const account = rows[0].accounts;
|
||||
const site = rows[0].sites;
|
||||
|
||||
if (isSiteDisabled(site.status)) {
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: 'disabled',
|
||||
reason: '站点已禁用',
|
||||
source: 'balance',
|
||||
});
|
||||
return {
|
||||
balance: account.balance ?? 0,
|
||||
used: account.balanceUsed ?? 0,
|
||||
quota: account.quota ?? 0,
|
||||
skipped: true,
|
||||
reason: 'site_disabled',
|
||||
};
|
||||
}
|
||||
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return null;
|
||||
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
let activeAccessToken = account.accessToken;
|
||||
let balanceInfo: BalanceInfo | null = null;
|
||||
|
||||
const readBalance = async (token: string) => adapter.getBalance(site.url, token, platformUserId);
|
||||
const handleBalanceError = async (err: any) => {
|
||||
const message = err?.message || 'unknown error';
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: 'unhealthy',
|
||||
reason: message,
|
||||
source: 'balance',
|
||||
});
|
||||
if (shouldReportExpired(message)) {
|
||||
await reportTokenExpired({
|
||||
accountId: account.id,
|
||||
username: account.username,
|
||||
siteName: site.name,
|
||||
detail: message,
|
||||
});
|
||||
}
|
||||
throw err;
|
||||
};
|
||||
|
||||
try {
|
||||
balanceInfo = await readBalance(activeAccessToken);
|
||||
} catch (err: any) {
|
||||
const message = err?.message || 'unknown error';
|
||||
if (shouldAttemptAutoRelogin(message)) {
|
||||
const refreshedAccessToken = await tryAutoRelogin(account, site);
|
||||
if (refreshedAccessToken) {
|
||||
activeAccessToken = refreshedAccessToken;
|
||||
try {
|
||||
balanceInfo = await readBalance(activeAccessToken);
|
||||
} catch (retryErr: any) {
|
||||
await handleBalanceError(retryErr);
|
||||
}
|
||||
} else {
|
||||
await handleBalanceError(err);
|
||||
}
|
||||
} else {
|
||||
await handleBalanceError(err);
|
||||
}
|
||||
}
|
||||
|
||||
if (!balanceInfo) {
|
||||
throw new Error('failed to fetch balance');
|
||||
}
|
||||
|
||||
if (
|
||||
!(typeof balanceInfo.todayIncome === 'number' && Number.isFinite(balanceInfo.todayIncome)) &&
|
||||
supportsTodayIncomeLogFallback(site.platform)
|
||||
) {
|
||||
try {
|
||||
const fallbackIncome = await fetchTodayIncomeFromLogs({
|
||||
baseUrl: site.url,
|
||||
accessToken: activeAccessToken,
|
||||
platform: site.platform,
|
||||
platformUserId,
|
||||
});
|
||||
if (typeof fallbackIncome === 'number' && Number.isFinite(fallbackIncome)) {
|
||||
balanceInfo.todayIncome = fallbackIncome;
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const existingRuntimeHealth = extractRuntimeHealth(account.extraConfig);
|
||||
const keepUnsupportedCheckinDegraded = isUnsupportedCheckinRuntimeHealth(existingRuntimeHealth);
|
||||
|
||||
const updates: Record<string, unknown> = {
|
||||
balance: balanceInfo.balance,
|
||||
balanceUsed: balanceInfo.used,
|
||||
quota: balanceInfo.quota,
|
||||
status: account.status === 'expired' ? 'active' : account.status,
|
||||
lastBalanceRefresh: new Date().toISOString(),
|
||||
updatedAt: new Date().toISOString(),
|
||||
};
|
||||
if (typeof balanceInfo.todayIncome === 'number' && Number.isFinite(balanceInfo.todayIncome)) {
|
||||
updates.extraConfig = updateTodayIncomeSnapshot(account.extraConfig, balanceInfo.todayIncome);
|
||||
}
|
||||
|
||||
db.update(schema.accounts)
|
||||
.set(updates)
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.run();
|
||||
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: keepUnsupportedCheckinDegraded ? 'degraded' : 'healthy',
|
||||
reason: keepUnsupportedCheckinDegraded
|
||||
? (existingRuntimeHealth?.reason || '\u7ad9\u70b9\u4e0d\u652f\u6301\u7b7e\u5230\u63a5\u53e3')
|
||||
: '\u4f59\u989d\u5237\u65b0\u6210\u529f',
|
||||
source: keepUnsupportedCheckinDegraded
|
||||
? (existingRuntimeHealth?.source || 'checkin')
|
||||
: 'balance',
|
||||
});
|
||||
|
||||
if (balanceInfo.balance < 1) {
|
||||
db.insert(schema.events).values({
|
||||
type: 'balance',
|
||||
title: '余额不足',
|
||||
message: `${account.username || 'ID:' + accountId} 余额不足: $${balanceInfo.balance.toFixed(2)}`,
|
||||
level: 'warning',
|
||||
relatedId: accountId,
|
||||
relatedType: 'account',
|
||||
}).run();
|
||||
|
||||
await sendNotification(
|
||||
'余额不足提醒',
|
||||
`${account.username || 'ID:' + accountId} 余额不足: $${balanceInfo.balance.toFixed(2)}`,
|
||||
'warning',
|
||||
);
|
||||
}
|
||||
|
||||
return balanceInfo;
|
||||
}
|
||||
|
||||
export async function refreshAllBalances() {
|
||||
const rows = db
|
||||
.select()
|
||||
.from(schema.accounts)
|
||||
.where(eq(schema.accounts.status, 'active'))
|
||||
.all();
|
||||
|
||||
const results: Array<{ accountId: number; balance: number | null }> = [];
|
||||
|
||||
await Promise.all(
|
||||
rows.map(async (account) => {
|
||||
try {
|
||||
const info = await refreshBalance(account.id);
|
||||
results.push({ accountId: account.id, balance: info?.balance ?? null });
|
||||
} catch {
|
||||
results.push({ accountId: account.id, balance: null });
|
||||
}
|
||||
}),
|
||||
);
|
||||
|
||||
return results;
|
||||
}
|
||||
@@ -0,0 +1,21 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { parseCheckinRewardAmount } from './checkinRewardParser.js';
|
||||
|
||||
describe('checkinRewardParser', () => {
|
||||
it('parses numeric reward values', () => {
|
||||
expect(parseCheckinRewardAmount(3.5)).toBe(3.5);
|
||||
expect(parseCheckinRewardAmount('12')).toBe(12);
|
||||
});
|
||||
|
||||
it('parses reward strings with text wrappers', () => {
|
||||
expect(parseCheckinRewardAmount('奖励 +2.75')).toBe(2.75);
|
||||
expect(parseCheckinRewardAmount('checkin success, reward=5')).toBe(5);
|
||||
});
|
||||
|
||||
it('returns zero for missing or non-positive values', () => {
|
||||
expect(parseCheckinRewardAmount('')).toBe(0);
|
||||
expect(parseCheckinRewardAmount(null)).toBe(0);
|
||||
expect(parseCheckinRewardAmount('checked in')).toBe(0);
|
||||
expect(parseCheckinRewardAmount('-1')).toBe(0);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,28 @@
|
||||
function toFiniteNumber(value: unknown): number | null {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
||||
if (typeof value !== 'string') return null;
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return null;
|
||||
const parsed = Number(trimmed);
|
||||
if (Number.isFinite(parsed)) return parsed;
|
||||
return null;
|
||||
}
|
||||
|
||||
export function parseCheckinRewardAmount(value: unknown): number {
|
||||
const numeric = toFiniteNumber(value);
|
||||
if (numeric != null) {
|
||||
return numeric > 0 ? numeric : 0;
|
||||
}
|
||||
|
||||
if (typeof value !== 'string') return 0;
|
||||
const text = value.trim();
|
||||
if (!text) return 0;
|
||||
|
||||
const normalized = text.replace(/,/g, '');
|
||||
const match = normalized.match(/[-+]?\d+(?:\.\d+)?/);
|
||||
if (!match) return 0;
|
||||
|
||||
const parsed = Number(match[0]);
|
||||
if (!Number.isFinite(parsed) || parsed <= 0) return 0;
|
||||
return parsed;
|
||||
}
|
||||
@@ -0,0 +1,103 @@
|
||||
import cron from 'node-cron';
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { config } from '../config.js';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { refreshAllBalances } from './balanceService.js';
|
||||
import { checkinAll } from './checkinService.js';
|
||||
import { refreshModelsAndRebuildRoutes } from './modelService.js';
|
||||
import { sendNotification } from './notifyService.js';
|
||||
import { buildDailySummaryNotification, collectDailySummaryMetrics } from './dailySummaryService.js';
|
||||
|
||||
let checkinTask: cron.ScheduledTask | null = null;
|
||||
let balanceTask: cron.ScheduledTask | null = null;
|
||||
let dailySummaryTask: cron.ScheduledTask | null = null;
|
||||
|
||||
const DAILY_SUMMARY_DEFAULT_CRON = '58 23 * * *';
|
||||
|
||||
function resolveCronSetting(settingKey: string, fallback: string): string {
|
||||
try {
|
||||
const row = db.select().from(schema.settings).where(eq(schema.settings.key, settingKey)).get();
|
||||
if (row?.value) {
|
||||
const parsed = JSON.parse(row.value);
|
||||
if (typeof parsed === 'string' && cron.validate(parsed)) {
|
||||
return parsed;
|
||||
}
|
||||
}
|
||||
} catch {}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
function createCheckinTask(cronExpr: string) {
|
||||
return cron.schedule(cronExpr, async () => {
|
||||
console.log(`[Scheduler] Running check-in at ${new Date().toISOString()}`);
|
||||
try {
|
||||
const results = await checkinAll();
|
||||
const success = results.filter((r) => r.result.success).length;
|
||||
const failed = results.length - success;
|
||||
console.log(`[Scheduler] Check-in complete: ${success} success, ${failed} failed`);
|
||||
} catch (err) {
|
||||
console.error('[Scheduler] Check-in error:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createBalanceTask(cronExpr: string) {
|
||||
return cron.schedule(cronExpr, async () => {
|
||||
console.log(`[Scheduler] Refreshing balances at ${new Date().toISOString()}`);
|
||||
try {
|
||||
await refreshAllBalances();
|
||||
await refreshModelsAndRebuildRoutes();
|
||||
console.log('[Scheduler] Balance refresh complete');
|
||||
} catch (err) {
|
||||
console.error('[Scheduler] Balance refresh error:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
function createDailySummaryTask(cronExpr: string) {
|
||||
return cron.schedule(cronExpr, async () => {
|
||||
console.log(`[Scheduler] Sending daily summary at ${new Date().toISOString()}`);
|
||||
try {
|
||||
const metrics = collectDailySummaryMetrics();
|
||||
const { title, message } = buildDailySummaryNotification(metrics);
|
||||
await sendNotification(title, message, 'info', {
|
||||
bypassThrottle: true,
|
||||
requireChannel: true,
|
||||
throwOnFailure: true,
|
||||
});
|
||||
console.log(`[Scheduler] Daily summary sent: ${title}`);
|
||||
} catch (err) {
|
||||
console.error('[Scheduler] Daily summary error:', err);
|
||||
}
|
||||
});
|
||||
}
|
||||
|
||||
export function startScheduler() {
|
||||
const activeCheckinCron = resolveCronSetting('checkin_cron', config.checkinCron);
|
||||
const activeBalanceCron = resolveCronSetting('balance_refresh_cron', config.balanceRefreshCron);
|
||||
const activeDailySummaryCron = resolveCronSetting('daily_summary_cron', DAILY_SUMMARY_DEFAULT_CRON);
|
||||
config.checkinCron = activeCheckinCron;
|
||||
config.balanceRefreshCron = activeBalanceCron;
|
||||
|
||||
checkinTask = createCheckinTask(activeCheckinCron);
|
||||
balanceTask = createBalanceTask(activeBalanceCron);
|
||||
dailySummaryTask = createDailySummaryTask(activeDailySummaryCron);
|
||||
|
||||
console.log(`[Scheduler] Check-in cron: ${activeCheckinCron}`);
|
||||
console.log(`[Scheduler] Balance refresh cron: ${activeBalanceCron}`);
|
||||
console.log(`[Scheduler] Daily summary cron: ${activeDailySummaryCron}`);
|
||||
}
|
||||
|
||||
export function updateCheckinCron(cronExpr: string) {
|
||||
if (!cron.validate(cronExpr)) throw new Error(`Invalid cron: ${cronExpr}`);
|
||||
config.checkinCron = cronExpr;
|
||||
checkinTask?.stop();
|
||||
checkinTask = createCheckinTask(cronExpr);
|
||||
}
|
||||
|
||||
export function updateBalanceRefreshCron(cronExpr: string) {
|
||||
if (!cron.validate(cronExpr)) throw new Error(`Invalid cron: ${cronExpr}`);
|
||||
config.balanceRefreshCron = cronExpr;
|
||||
balanceTask?.stop();
|
||||
balanceTask = createBalanceTask(cronExpr);
|
||||
}
|
||||
@@ -0,0 +1,321 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const adapterMock = {
|
||||
checkin: vi.fn(),
|
||||
login: vi.fn(),
|
||||
};
|
||||
|
||||
const notifyMock = vi.fn();
|
||||
const reportTokenExpiredMock = vi.fn();
|
||||
const refreshBalanceMock = vi.fn();
|
||||
const decryptPasswordMock = vi.fn();
|
||||
|
||||
const selectAllMock = vi.fn();
|
||||
const insertValuesMock = vi.fn();
|
||||
const updateSetMock = vi.fn();
|
||||
|
||||
vi.mock('../db/index.js', () => {
|
||||
const selectChain = {
|
||||
all: () => selectAllMock(),
|
||||
where: () => selectChain,
|
||||
innerJoin: () => selectChain,
|
||||
from: () => selectChain,
|
||||
};
|
||||
|
||||
const insertChain = {
|
||||
run: () => ({}),
|
||||
values: (...args: unknown[]) => {
|
||||
insertValuesMock(...args);
|
||||
return insertChain;
|
||||
},
|
||||
};
|
||||
|
||||
const updateWhereChain = {
|
||||
run: () => ({}),
|
||||
};
|
||||
|
||||
const updateSetChain = {
|
||||
where: () => updateWhereChain,
|
||||
};
|
||||
|
||||
return {
|
||||
db: {
|
||||
select: () => selectChain,
|
||||
insert: () => insertChain,
|
||||
update: () => ({
|
||||
set: (updates: Record<string, unknown>) => {
|
||||
updateSetMock(updates);
|
||||
return updateSetChain;
|
||||
},
|
||||
}),
|
||||
},
|
||||
schema: {
|
||||
accounts: { id: 'id', siteId: 'siteId', checkinEnabled: 'checkinEnabled', status: 'status' },
|
||||
sites: { id: 'id' },
|
||||
checkinLogs: {},
|
||||
events: {},
|
||||
},
|
||||
};
|
||||
});
|
||||
|
||||
vi.mock('./platforms/index.js', () => ({
|
||||
getAdapter: () => adapterMock,
|
||||
}));
|
||||
|
||||
vi.mock('./notifyService.js', () => ({
|
||||
sendNotification: (...args: unknown[]) => notifyMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./alertService.js', () => ({
|
||||
reportTokenExpired: (...args: unknown[]) => reportTokenExpiredMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./balanceService.js', () => ({
|
||||
refreshBalance: (...args: unknown[]) => refreshBalanceMock(...args),
|
||||
}));
|
||||
|
||||
vi.mock('./accountCredentialService.js', () => ({
|
||||
decryptAccountPassword: (...args: unknown[]) => decryptPasswordMock(...args),
|
||||
}));
|
||||
|
||||
describe('checkinService auto relogin', () => {
|
||||
beforeEach(() => {
|
||||
adapterMock.checkin.mockReset();
|
||||
adapterMock.login.mockReset();
|
||||
notifyMock.mockReset();
|
||||
reportTokenExpiredMock.mockReset();
|
||||
refreshBalanceMock.mockReset();
|
||||
decryptPasswordMock.mockReset();
|
||||
selectAllMock.mockReset();
|
||||
insertValuesMock.mockReset();
|
||||
updateSetMock.mockReset();
|
||||
});
|
||||
|
||||
it('retries checkin once after auto relogin when access token is missing', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 1,
|
||||
username: 'linuxdo_7659',
|
||||
accessToken: 'expired-token',
|
||||
status: 'active',
|
||||
extraConfig: JSON.stringify({
|
||||
autoRelogin: { username: 'linuxdo_7659', passwordCipher: 'cipher' },
|
||||
}),
|
||||
},
|
||||
sites: {
|
||||
id: 3,
|
||||
name: 'kfc',
|
||||
url: 'https://kfc-api.sxxe.net',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin
|
||||
.mockResolvedValueOnce({ success: false, message: '无权进行此操作,未登录且未提供 access token' })
|
||||
.mockResolvedValueOnce({ success: true, message: 'checked in' });
|
||||
decryptPasswordMock.mockReturnValue('plain-password');
|
||||
adapterMock.login.mockResolvedValue({ success: true, accessToken: 'fresh-token' });
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
const result = await checkinAccount(1);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(adapterMock.login).toHaveBeenCalledTimes(1);
|
||||
expect(adapterMock.checkin).toHaveBeenCalledTimes(2);
|
||||
expect(adapterMock.checkin.mock.calls[0][1]).toBe('expired-token');
|
||||
expect(adapterMock.checkin.mock.calls[1][1]).toBe('fresh-token');
|
||||
expect(adapterMock.checkin.mock.calls[0][2]).toBe(7659);
|
||||
expect(updateSetMock).toHaveBeenCalledWith(expect.objectContaining({ accessToken: 'fresh-token' }));
|
||||
});
|
||||
|
||||
it('passes guessed platform user id when config does not include it', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 2,
|
||||
username: 'linuxdo_11494',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 4,
|
||||
name: 'wong',
|
||||
url: 'https://wzw.pp.ua',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({ success: true, message: 'checked in' });
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
await checkinAccount(2);
|
||||
|
||||
expect(adapterMock.checkin).toHaveBeenCalledTimes(1);
|
||||
expect(adapterMock.checkin.mock.calls[0][2]).toBe(11494);
|
||||
});
|
||||
|
||||
it('keeps successful checkin as success when message is 签到成功', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 12,
|
||||
username: 'linuxdo_5566',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 12,
|
||||
name: 'demo',
|
||||
url: 'https://example.com',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({ success: true, message: '签到成功' });
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
const result = await checkinAccount(12);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
const firstInsertPayload = insertValuesMock.mock.calls[0]?.[0] as Record<string, unknown>;
|
||||
expect(firstInsertPayload?.status).toBe('success');
|
||||
});
|
||||
|
||||
it('infers reward from balance delta when checkin reward text is empty', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 13,
|
||||
username: 'linuxdo_7788',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
balance: 10,
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 13,
|
||||
name: 'demo',
|
||||
url: 'https://example.com',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({ success: true, message: 'checkin success' });
|
||||
refreshBalanceMock.mockResolvedValue({ balance: 12.5, used: 0, quota: 12.5 });
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
await checkinAccount(13);
|
||||
|
||||
const firstInsertPayload = insertValuesMock.mock.calls[0]?.[0] as Record<string, unknown>;
|
||||
expect(Number(firstInsertPayload?.reward)).toBeCloseTo(2.5, 6);
|
||||
});
|
||||
|
||||
it('treats already checked in responses as successful checkins', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 9,
|
||||
username: 'linuxdo_9999',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 9,
|
||||
name: 'demo',
|
||||
url: 'https://example.com',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({ success: false, message: '今天已经签到过啦' });
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
const result = await checkinAccount(9);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.status).toBe('success');
|
||||
const firstInsertPayload = insertValuesMock.mock.calls[0]?.[0] as Record<string, unknown>;
|
||||
expect(firstInsertPayload?.status).toBe('success');
|
||||
expect(notifyMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('treats unsupported checkin endpoint responses as skipped', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 10,
|
||||
username: 'linuxdo_131936',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 10,
|
||||
name: 'anyrouter',
|
||||
url: 'https://anyrouter.top',
|
||||
platform: 'anyrouter',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({
|
||||
success: false,
|
||||
message: 'HTTP 404: {"error":{"message":"Invalid URL (POST /api/user/checkin)"}}',
|
||||
});
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
const result = await checkinAccount(10);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.status).toBe('skipped');
|
||||
const firstInsertPayload = insertValuesMock.mock.calls[0]?.[0] as Record<string, unknown>;
|
||||
expect(firstInsertPayload?.status).toBe('skipped');
|
||||
expect(refreshBalanceMock).not.toHaveBeenCalled();
|
||||
expect(notifyMock).not.toHaveBeenCalled();
|
||||
});
|
||||
|
||||
it('treats turnstile-required responses as skipped', async () => {
|
||||
selectAllMock.mockReturnValue([
|
||||
{
|
||||
accounts: {
|
||||
id: 14,
|
||||
username: 'linuxdo_10277',
|
||||
accessToken: 'token',
|
||||
status: 'active',
|
||||
extraConfig: null,
|
||||
},
|
||||
sites: {
|
||||
id: 14,
|
||||
name: 'run-anytime',
|
||||
url: 'https://runanytime.hxi.me',
|
||||
platform: 'new-api',
|
||||
},
|
||||
},
|
||||
]);
|
||||
|
||||
adapterMock.checkin.mockResolvedValue({
|
||||
success: false,
|
||||
message: 'Turnstile token 为空',
|
||||
});
|
||||
|
||||
const { checkinAccount } = await import('./checkinService.js');
|
||||
const result = await checkinAccount(14);
|
||||
|
||||
expect(result.success).toBe(true);
|
||||
expect(result.status).toBe('skipped');
|
||||
const firstInsertPayload = insertValuesMock.mock.calls[0]?.[0] as Record<string, unknown>;
|
||||
expect(firstInsertPayload?.status).toBe('skipped');
|
||||
expect(firstInsertPayload?.message).toBe('站点开启了 Turnstile 校验,需要人工签到');
|
||||
expect(refreshBalanceMock).not.toHaveBeenCalled();
|
||||
expect(notifyMock).not.toHaveBeenCalled();
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,336 @@
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { getAdapter } from './platforms/index.js';
|
||||
import { eq, and } from 'drizzle-orm';
|
||||
import { sendNotification } from './notifyService.js';
|
||||
import { isCloudflareChallenge, isTokenExpiredError } from './alertRules.js';
|
||||
import { reportTokenExpired } from './alertService.js';
|
||||
import { refreshBalance } from './balanceService.js';
|
||||
import { parseCheckinRewardAmount } from './checkinRewardParser.js';
|
||||
import {
|
||||
getAutoReloginConfig,
|
||||
getPlatformUserIdFromExtraConfig,
|
||||
guessPlatformUserIdFromUsername,
|
||||
mergeAccountExtraConfig,
|
||||
resolvePlatformUserId,
|
||||
} from './accountExtraConfig.js';
|
||||
import { decryptAccountPassword } from './accountCredentialService.js';
|
||||
import { setAccountRuntimeHealth } from './accountHealthService.js';
|
||||
|
||||
type CheckinExecutionStatus = 'success' | 'failed' | 'skipped';
|
||||
|
||||
function isSiteDisabled(status?: string | null): boolean {
|
||||
return (status || 'active') === 'disabled';
|
||||
}
|
||||
|
||||
function isAlreadyCheckedInMessage(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
const text = message.trim();
|
||||
if (!text) return false;
|
||||
const normalized = text.toLowerCase();
|
||||
return (
|
||||
normalized.includes('already checked in') ||
|
||||
normalized.includes('already signed') ||
|
||||
normalized.includes('already sign in') ||
|
||||
text.includes('\u4eca\u65e5\u5df2\u7b7e\u5230') ||
|
||||
text.includes('\u4eca\u5929\u5df2\u7b7e\u5230') ||
|
||||
text.includes('\u4eca\u5929\u5df2\u7ecf\u7b7e\u5230') ||
|
||||
text.includes('\u4eca\u65e5\u5df2\u7ecf\u7b7e\u5230') ||
|
||||
text.includes('\u5df2\u7ecf\u7b7e\u5230') ||
|
||||
text.includes('\u5df2\u7b7e\u5230') ||
|
||||
text.includes('\u91cd\u590d\u7b7e\u5230') ||
|
||||
text.includes('\u7b7e\u5230\u8fc7')
|
||||
);
|
||||
}
|
||||
|
||||
function isUnsupportedCheckinMessage(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
const text = message.toLowerCase();
|
||||
return (
|
||||
text.includes('invalid url (post /api/user/checkin)') ||
|
||||
(text.includes('http 404') && text.includes('/api/user/checkin')) ||
|
||||
text.includes('checkin endpoint not found')
|
||||
);
|
||||
}
|
||||
|
||||
function isManualVerificationRequiredMessage(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
const text = message.toLowerCase();
|
||||
return (
|
||||
text.includes('turnstile token \u4e3a\u7a7a') ||
|
||||
(text.includes('turnstile') && (text.includes('token') || text.includes('\u6821\u9a8c') || text.includes('\u9a8c\u8bc1')))
|
||||
);
|
||||
}
|
||||
|
||||
function shouldAttemptAutoRelogin(message?: string | null): boolean {
|
||||
if (!message) return false;
|
||||
if (isTokenExpiredError({ message })) return true;
|
||||
|
||||
const text = message.toLowerCase();
|
||||
if (text.includes('new-api-user')) return true;
|
||||
if (text.includes('access token')) return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
function inferRewardFromBalanceDelta(previousBalance: unknown, latestBalance: unknown): number {
|
||||
const before = typeof previousBalance === 'number' && Number.isFinite(previousBalance)
|
||||
? previousBalance
|
||||
: null;
|
||||
const after = typeof latestBalance === 'number' && Number.isFinite(latestBalance)
|
||||
? latestBalance
|
||||
: null;
|
||||
if (before == null || after == null) return 0;
|
||||
|
||||
const delta = after - before;
|
||||
if (!Number.isFinite(delta) || delta <= 0) return 0;
|
||||
return Math.round(delta * 1_000_000) / 1_000_000;
|
||||
}
|
||||
|
||||
async function tryAutoRelogin(account: any, site: any): Promise<string | null> {
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return null;
|
||||
|
||||
const relogin = getAutoReloginConfig(account.extraConfig);
|
||||
if (!relogin) return null;
|
||||
|
||||
const password = decryptAccountPassword(relogin.passwordCipher);
|
||||
if (!password) return null;
|
||||
|
||||
const result = await adapter.login(site.url, relogin.username, password);
|
||||
if (!result.success || !result.accessToken) return null;
|
||||
|
||||
db.update(schema.accounts)
|
||||
.set({
|
||||
accessToken: result.accessToken,
|
||||
updatedAt: new Date().toISOString(),
|
||||
status: account.status === 'expired' ? 'active' : account.status,
|
||||
})
|
||||
.where(eq(schema.accounts.id, account.id))
|
||||
.run();
|
||||
|
||||
return result.accessToken;
|
||||
}
|
||||
|
||||
export async function checkinAccount(accountId: number, options?: { skipEvent?: boolean }) {
|
||||
const rows = db
|
||||
.select()
|
||||
.from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.all();
|
||||
|
||||
if (rows.length === 0) return { success: false, message: 'account not found' };
|
||||
|
||||
const account = rows[0].accounts;
|
||||
const site = rows[0].sites;
|
||||
|
||||
if (isSiteDisabled(site.status)) {
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: 'disabled',
|
||||
reason: '\u7ad9\u70b9\u5df2\u7981\u7528',
|
||||
source: 'checkin',
|
||||
});
|
||||
db.insert(schema.checkinLogs).values({
|
||||
accountId: account.id,
|
||||
status: 'skipped',
|
||||
message: 'site disabled',
|
||||
}).run();
|
||||
|
||||
if (!options?.skipEvent) {
|
||||
db.insert(schema.events).values({
|
||||
type: 'checkin',
|
||||
title: 'checkin skipped',
|
||||
message: `${account.username || 'ID:' + accountId} @ ${site.name}: site disabled`,
|
||||
level: 'info',
|
||||
relatedId: accountId,
|
||||
relatedType: 'account',
|
||||
}).run();
|
||||
}
|
||||
|
||||
return {
|
||||
success: true,
|
||||
status: 'skipped' as const,
|
||||
skipped: true,
|
||||
reason: 'site_disabled',
|
||||
message: 'site disabled',
|
||||
};
|
||||
}
|
||||
|
||||
const adapter = getAdapter(site.platform);
|
||||
if (!adapter) return { success: false, status: 'failed' as const, message: `unsupported platform: ${site.platform}` };
|
||||
|
||||
const storedPlatformUserId = getPlatformUserIdFromExtraConfig(account.extraConfig);
|
||||
const guessedPlatformUserId = storedPlatformUserId
|
||||
? undefined
|
||||
: guessPlatformUserIdFromUsername(account.username);
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
|
||||
let activeAccessToken = account.accessToken;
|
||||
let result = await adapter.checkin(site.url, activeAccessToken, platformUserId);
|
||||
|
||||
if (!result.success && shouldAttemptAutoRelogin(result.message)) {
|
||||
const refreshedAccessToken = await tryAutoRelogin(account, site);
|
||||
if (refreshedAccessToken) {
|
||||
activeAccessToken = refreshedAccessToken;
|
||||
result = await adapter.checkin(site.url, activeAccessToken, platformUserId);
|
||||
}
|
||||
}
|
||||
|
||||
const isCloudflare = isCloudflareChallenge(result.message);
|
||||
const alreadyCheckedIn = isAlreadyCheckedInMessage(result.message);
|
||||
const unsupportedCheckin = isUnsupportedCheckinMessage(result.message);
|
||||
const manualVerificationRequired = isManualVerificationRequiredMessage(result.message);
|
||||
const manualVerificationMessage = '\u7ad9\u70b9\u5f00\u542f\u4e86 Turnstile \u6821\u9a8c\uff0c\u9700\u8981\u4eba\u5de5\u7b7e\u5230';
|
||||
const logMessage = manualVerificationRequired ? manualVerificationMessage : result.message;
|
||||
const effectiveSuccess = result.success || alreadyCheckedIn || unsupportedCheckin || manualVerificationRequired;
|
||||
const shouldRefreshBalance = result.success || alreadyCheckedIn;
|
||||
const directCheckinSuccess = result.success && !alreadyCheckedIn && !unsupportedCheckin;
|
||||
const normalizedStatus: CheckinExecutionStatus = effectiveSuccess
|
||||
? ((unsupportedCheckin || manualVerificationRequired) ? 'skipped' : 'success')
|
||||
: 'failed';
|
||||
let logReward = result.reward;
|
||||
let refreshedBalanceInfo: Awaited<ReturnType<typeof refreshBalance>> | null = null;
|
||||
|
||||
if (effectiveSuccess) {
|
||||
const healthState = (unsupportedCheckin || manualVerificationRequired) ? 'degraded' : 'healthy';
|
||||
const healthReason = unsupportedCheckin
|
||||
? '\u7ad9\u70b9\u4e0d\u652f\u6301\u7b7e\u5230\u63a5\u53e3'
|
||||
: manualVerificationRequired
|
||||
? manualVerificationMessage
|
||||
: (alreadyCheckedIn ? '\u4eca\u65e5\u5df2\u7b7e\u5230' : (result.message || '\u7b7e\u5230\u6210\u529f'));
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: healthState,
|
||||
reason: healthReason,
|
||||
source: 'checkin',
|
||||
});
|
||||
|
||||
const updates: Record<string, unknown> = {
|
||||
lastCheckinAt: new Date().toISOString(),
|
||||
};
|
||||
if (!storedPlatformUserId && guessedPlatformUserId) {
|
||||
updates.extraConfig = mergeAccountExtraConfig(account.extraConfig, {
|
||||
platformUserId: guessedPlatformUserId,
|
||||
});
|
||||
}
|
||||
if (account.status === 'expired') {
|
||||
updates.status = 'active';
|
||||
updates.updatedAt = new Date().toISOString();
|
||||
}
|
||||
|
||||
db.update(schema.accounts)
|
||||
.set(updates)
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.run();
|
||||
|
||||
if (shouldRefreshBalance) {
|
||||
try {
|
||||
refreshedBalanceInfo = await refreshBalance(account.id);
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const parsedReward = parseCheckinRewardAmount(logReward) || parseCheckinRewardAmount(result.message);
|
||||
if (directCheckinSuccess && parsedReward <= 0) {
|
||||
const inferredReward = inferRewardFromBalanceDelta(account.balance, refreshedBalanceInfo?.balance);
|
||||
if (inferredReward > 0) {
|
||||
logReward = inferredReward.toString();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
db.insert(schema.checkinLogs).values({
|
||||
accountId: account.id,
|
||||
status: normalizedStatus,
|
||||
message: logMessage,
|
||||
reward: logReward,
|
||||
}).run();
|
||||
|
||||
if (!options?.skipEvent) {
|
||||
db.insert(schema.events).values({
|
||||
type: 'checkin',
|
||||
title: effectiveSuccess
|
||||
? (normalizedStatus === 'skipped' ? 'checkin skipped' : 'checkin success')
|
||||
: (isCloudflare ? 'checkin failed (cloudflare challenge)' : 'checkin failed'),
|
||||
message: `${account.username || 'ID:' + accountId} @ ${site.name}: ${logMessage}`,
|
||||
level: effectiveSuccess ? 'info' : 'error',
|
||||
relatedId: accountId,
|
||||
relatedType: 'account',
|
||||
}).run();
|
||||
}
|
||||
|
||||
if (!effectiveSuccess) {
|
||||
setAccountRuntimeHealth(account.id, {
|
||||
state: 'unhealthy',
|
||||
reason: result.message || '\u7b7e\u5230\u5931\u8d25',
|
||||
source: 'checkin',
|
||||
});
|
||||
if (isTokenExpiredError({ message: result.message })) {
|
||||
await reportTokenExpired({
|
||||
accountId: account.id,
|
||||
username: account.username,
|
||||
siteName: site.name,
|
||||
detail: result.message,
|
||||
});
|
||||
}
|
||||
|
||||
if (isCloudflare) {
|
||||
await sendNotification(
|
||||
'Cloudflare challenge',
|
||||
`${account.username || 'ID:' + accountId} @ ${site.name}: ${result.message}`,
|
||||
'warning',
|
||||
);
|
||||
}
|
||||
|
||||
if (!unsupportedCheckin && !manualVerificationRequired) {
|
||||
await sendNotification(
|
||||
'checkin failed',
|
||||
`${account.username || 'ID:' + accountId} @ ${site.name}: ${result.message}`,
|
||||
'error',
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
return {
|
||||
...result,
|
||||
success: effectiveSuccess,
|
||||
status: normalizedStatus,
|
||||
...(normalizedStatus === 'skipped' ? { skipped: true } : {}),
|
||||
};
|
||||
}
|
||||
|
||||
export async function checkinAll() {
|
||||
const rows = db
|
||||
.select()
|
||||
.from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(
|
||||
and(
|
||||
eq(schema.accounts.checkinEnabled, true),
|
||||
eq(schema.accounts.status, 'active'),
|
||||
),
|
||||
)
|
||||
.all();
|
||||
|
||||
const results: Array<{ accountId: number; username: string | null; site: string; result: any }> = [];
|
||||
|
||||
const grouped = new Map<number, typeof rows>();
|
||||
for (const row of rows) {
|
||||
const siteId = row.sites.id;
|
||||
if (!grouped.has(siteId)) grouped.set(siteId, []);
|
||||
grouped.get(siteId)!.push(row);
|
||||
}
|
||||
|
||||
const promises = Array.from(grouped.entries()).map(async ([_, siteRows]) => {
|
||||
for (const row of siteRows) {
|
||||
const r = await checkinAccount(row.accounts.id, { skipEvent: true });
|
||||
results.push({
|
||||
accountId: row.accounts.id,
|
||||
username: row.accounts.username,
|
||||
site: row.sites.name,
|
||||
result: r,
|
||||
});
|
||||
}
|
||||
});
|
||||
|
||||
await Promise.all(promises);
|
||||
return results;
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildDailySummaryNotification, type DailySummaryMetrics } from './dailySummaryService.js';
|
||||
|
||||
describe('dailySummaryService', () => {
|
||||
it('builds readable daily summary notification text', () => {
|
||||
const metrics: DailySummaryMetrics = {
|
||||
localDay: '2026-02-27',
|
||||
generatedAtLocal: '2026-02-27 23:58:00',
|
||||
timeZone: 'Asia/Shanghai',
|
||||
totalAccounts: 10,
|
||||
activeAccounts: 8,
|
||||
lowBalanceAccounts: 2,
|
||||
checkinTotal: 7,
|
||||
checkinSuccess: 5,
|
||||
checkinSkipped: 1,
|
||||
checkinFailed: 1,
|
||||
proxyTotal: 120,
|
||||
proxySuccess: 114,
|
||||
proxyFailed: 6,
|
||||
proxyTotalTokens: 987654,
|
||||
todaySpend: 12.345678,
|
||||
todayReward: 3.210987,
|
||||
};
|
||||
|
||||
const { title, message } = buildDailySummaryNotification(metrics);
|
||||
expect(title).toBe('每日总结 2026-02-27');
|
||||
expect(message).toContain('生成时间: 2026-02-27 23:58:00 (Asia/Shanghai)');
|
||||
expect(message).toContain('签到统计: 总计 7 | 成功 5 | 跳过 1 | 失败 1');
|
||||
expect(message).toContain('代理统计: 总计 120 | 成功 114 | 失败 6');
|
||||
expect(message).toContain('费用统计: 支出 $12.345678 | 奖励 $3.210987 | 净值 $-9.134691');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,126 @@
|
||||
import { and, eq, gte, lt } from 'drizzle-orm';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { getLocalDayRangeUtc, formatLocalDateTime, getResolvedTimeZone } from './localTimeService.js';
|
||||
import { parseCheckinRewardAmount } from './checkinRewardParser.js';
|
||||
import { estimateRewardWithTodayIncomeFallback } from './todayIncomeRewardService.js';
|
||||
|
||||
export type DailySummaryMetrics = {
|
||||
localDay: string;
|
||||
generatedAtLocal: string;
|
||||
timeZone: string;
|
||||
totalAccounts: number;
|
||||
activeAccounts: number;
|
||||
lowBalanceAccounts: number;
|
||||
checkinTotal: number;
|
||||
checkinSuccess: number;
|
||||
checkinSkipped: number;
|
||||
checkinFailed: number;
|
||||
proxyTotal: number;
|
||||
proxySuccess: number;
|
||||
proxyFailed: number;
|
||||
proxyTotalTokens: number;
|
||||
todaySpend: number;
|
||||
todayReward: number;
|
||||
};
|
||||
|
||||
function round6(value: number): number {
|
||||
return Math.round(value * 1_000_000) / 1_000_000;
|
||||
}
|
||||
|
||||
export function collectDailySummaryMetrics(now = new Date()): DailySummaryMetrics {
|
||||
const { localDay, startUtc, endUtc } = getLocalDayRangeUtc(now);
|
||||
|
||||
const accountRows = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.sites.status, 'active'))
|
||||
.all();
|
||||
const accounts = accountRows.map((row) => row.accounts);
|
||||
|
||||
const activeAccounts = accounts.filter((account) => account.status === 'active').length;
|
||||
const lowBalanceAccounts = accounts.filter((account) => (account.balance || 0) < 1).length;
|
||||
|
||||
const todayCheckinRows = db.select().from(schema.checkinLogs)
|
||||
.innerJoin(schema.accounts, eq(schema.checkinLogs.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(
|
||||
gte(schema.checkinLogs.createdAt, startUtc),
|
||||
lt(schema.checkinLogs.createdAt, endUtc),
|
||||
eq(schema.sites.status, 'active'),
|
||||
))
|
||||
.all();
|
||||
const todayCheckins = todayCheckinRows.map((row) => row.checkin_logs);
|
||||
const checkinSkipped = todayCheckins.filter((checkin) => checkin.status === 'skipped').length;
|
||||
const checkinFailed = todayCheckins.filter((checkin) => checkin.status === 'failed').length;
|
||||
const checkinSuccess = todayCheckins.length - checkinSkipped - checkinFailed;
|
||||
|
||||
const rewardByAccount: Record<number, number> = {};
|
||||
const successCountByAccount: Record<number, number> = {};
|
||||
const parsedRewardCountByAccount: Record<number, number> = {};
|
||||
for (const row of todayCheckinRows) {
|
||||
const checkin = row.checkin_logs;
|
||||
if (checkin.status !== 'success') continue;
|
||||
const accountId = row.accounts.id;
|
||||
successCountByAccount[accountId] = (successCountByAccount[accountId] || 0) + 1;
|
||||
const rewardValue = parseCheckinRewardAmount(checkin.reward) || parseCheckinRewardAmount(checkin.message);
|
||||
if (rewardValue <= 0) continue;
|
||||
rewardByAccount[accountId] = (rewardByAccount[accountId] || 0) + rewardValue;
|
||||
parsedRewardCountByAccount[accountId] = (parsedRewardCountByAccount[accountId] || 0) + 1;
|
||||
}
|
||||
|
||||
const todayProxyRows = db.select().from(schema.proxyLogs)
|
||||
.leftJoin(schema.accounts, eq(schema.proxyLogs.accountId, schema.accounts.id))
|
||||
.leftJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(and(
|
||||
gte(schema.proxyLogs.createdAt, startUtc),
|
||||
lt(schema.proxyLogs.createdAt, endUtc),
|
||||
eq(schema.sites.status, 'active'),
|
||||
))
|
||||
.all();
|
||||
const todayProxyLogs = todayProxyRows.map((row) => row.proxy_logs);
|
||||
const proxySuccess = todayProxyLogs.filter((log) => log.status === 'success').length;
|
||||
const proxyFailed = todayProxyLogs.filter((log) => log.status === 'failed').length;
|
||||
const proxyTotalTokens = todayProxyLogs.reduce((sum, log) => sum + (log.totalTokens || 0), 0);
|
||||
const todaySpend = todayProxyLogs.reduce((sum, log) => sum + (typeof log.estimatedCost === 'number' ? log.estimatedCost : 0), 0);
|
||||
|
||||
const todayReward = accounts.reduce((sum, account) => sum + estimateRewardWithTodayIncomeFallback({
|
||||
day: localDay,
|
||||
successCount: successCountByAccount[account.id] || 0,
|
||||
parsedRewardCount: parsedRewardCountByAccount[account.id] || 0,
|
||||
rewardSum: rewardByAccount[account.id] || 0,
|
||||
extraConfig: account.extraConfig,
|
||||
}), 0);
|
||||
|
||||
return {
|
||||
localDay,
|
||||
generatedAtLocal: formatLocalDateTime(now),
|
||||
timeZone: getResolvedTimeZone(),
|
||||
totalAccounts: accounts.length,
|
||||
activeAccounts,
|
||||
lowBalanceAccounts,
|
||||
checkinTotal: todayCheckins.length,
|
||||
checkinSuccess: Math.max(0, checkinSuccess),
|
||||
checkinSkipped,
|
||||
checkinFailed,
|
||||
proxyTotal: todayProxyLogs.length,
|
||||
proxySuccess,
|
||||
proxyFailed,
|
||||
proxyTotalTokens,
|
||||
todaySpend: round6(todaySpend),
|
||||
todayReward: round6(todayReward),
|
||||
};
|
||||
}
|
||||
|
||||
export function buildDailySummaryNotification(metrics: DailySummaryMetrics): { title: string; message: string } {
|
||||
const net = round6(metrics.todayReward - metrics.todaySpend);
|
||||
const title = `每日总结 ${metrics.localDay}`;
|
||||
const message = [
|
||||
`日期: ${metrics.localDay}`,
|
||||
`生成时间: ${metrics.generatedAtLocal} (${metrics.timeZone})`,
|
||||
'',
|
||||
`账号概览: 总计 ${metrics.totalAccounts} | 活跃 ${metrics.activeAccounts} | 低余额(<$1) ${metrics.lowBalanceAccounts}`,
|
||||
`签到统计: 总计 ${metrics.checkinTotal} | 成功 ${metrics.checkinSuccess} | 跳过 ${metrics.checkinSkipped} | 失败 ${metrics.checkinFailed}`,
|
||||
`代理统计: 总计 ${metrics.proxyTotal} | 成功 ${metrics.proxySuccess} | 失败 ${metrics.proxyFailed} | Tokens ${metrics.proxyTotalTokens.toLocaleString()}`,
|
||||
`费用统计: 支出 $${metrics.todaySpend.toFixed(6)} | 奖励 $${metrics.todayReward.toFixed(6)} | 净值 $${net.toFixed(6)}`,
|
||||
].join('\n');
|
||||
return { title, message };
|
||||
}
|
||||
@@ -0,0 +1,52 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { classifyFailureReason } from './failureReasonService.js';
|
||||
|
||||
describe('failureReasonService', () => {
|
||||
it('classifies turnstile requirement as manual verification', () => {
|
||||
const result = classifyFailureReason({
|
||||
message: 'Turnstile token 为空',
|
||||
status: 'failed',
|
||||
});
|
||||
expect(result.code).toBe('manual_turnstile_required');
|
||||
expect(result.category).toBe('verification');
|
||||
});
|
||||
|
||||
it('classifies cloudflare tunnel outage', () => {
|
||||
const result = classifyFailureReason({
|
||||
message: 'HTTP 530 Cloudflare Tunnel error | Error 1033',
|
||||
status: 'failed',
|
||||
httpStatus: 530,
|
||||
});
|
||||
expect(result.code).toBe('cloudflare_tunnel_unavailable');
|
||||
expect(result.category).toBe('network');
|
||||
});
|
||||
|
||||
it('classifies token errors using status and message', () => {
|
||||
const result = classifyFailureReason({
|
||||
message: 'invalid access token',
|
||||
status: 'failed',
|
||||
httpStatus: 401,
|
||||
});
|
||||
expect(result.code).toBe('token_expired');
|
||||
expect(result.category).toBe('auth');
|
||||
});
|
||||
|
||||
it('classifies already checked in as state info', () => {
|
||||
const result = classifyFailureReason({
|
||||
message: '今天已经签到过啦',
|
||||
status: 'success',
|
||||
});
|
||||
expect(result.code).toBe('already_checked_in');
|
||||
expect(result.category).toBe('state');
|
||||
});
|
||||
|
||||
it('classifies missing checkin endpoint as site capability issue', () => {
|
||||
const result = classifyFailureReason({
|
||||
message: 'checkin endpoint not found',
|
||||
status: 'skipped',
|
||||
});
|
||||
expect(result.code).toBe('checkin_not_supported');
|
||||
expect(result.category).toBe('site');
|
||||
expect(result.title).toBe('站点未开启签到');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,142 @@
|
||||
import { isCloudflareChallenge, isTokenExpiredError } from './alertRules.js';
|
||||
|
||||
type FailureReasonCategory =
|
||||
| 'verification'
|
||||
| 'auth'
|
||||
| 'network'
|
||||
| 'site'
|
||||
| 'state'
|
||||
| 'unknown';
|
||||
|
||||
type FailureReasonCode =
|
||||
| 'site_disabled'
|
||||
| 'checkin_not_supported'
|
||||
| 'manual_turnstile_required'
|
||||
| 'cloudflare_tunnel_unavailable'
|
||||
| 'cloudflare_challenge'
|
||||
| 'token_expired'
|
||||
| 'already_checked_in'
|
||||
| 'network_timeout'
|
||||
| 'upstream_error'
|
||||
| 'unknown_error';
|
||||
|
||||
type FailureReason = {
|
||||
code: FailureReasonCode;
|
||||
category: FailureReasonCategory;
|
||||
title: string;
|
||||
actionHint: string;
|
||||
detailHint: string;
|
||||
};
|
||||
|
||||
function includesAny(text: string, keywords: string[]): boolean {
|
||||
return keywords.some((keyword) => text.includes(keyword));
|
||||
}
|
||||
|
||||
export function classifyFailureReason(
|
||||
input: { message?: string | null; status?: string | null; httpStatus?: number | null },
|
||||
): FailureReason {
|
||||
const rawMessage = String(input.message || '').trim();
|
||||
const text = rawMessage.toLowerCase();
|
||||
const status = (input.status || '').toLowerCase();
|
||||
const httpStatus = typeof input.httpStatus === 'number' ? input.httpStatus : 0;
|
||||
|
||||
if (status === 'skipped' && includesAny(text, ['site disabled'])) {
|
||||
return {
|
||||
code: 'site_disabled',
|
||||
category: 'site',
|
||||
title: '站点已禁用',
|
||||
actionHint: '启用站点后再试',
|
||||
detailHint: '该账号所属站点处于禁用状态,任务会自动跳过。',
|
||||
};
|
||||
}
|
||||
|
||||
if (includesAny(text, ['checkin endpoint not found', '签到端点不存在', '站点不支持签到', 'not support checkin'])) {
|
||||
return {
|
||||
code: 'checkin_not_supported',
|
||||
category: 'site',
|
||||
title: '站点未开启签到',
|
||||
actionHint: '无需重试(非故障)',
|
||||
detailHint: '该站点未提供签到端点,账号会被自动跳过。',
|
||||
};
|
||||
}
|
||||
|
||||
if (includesAny(text, ['turnstile token 为空', 'turnstile']) && includesAny(text, ['校验', 'token', '验证', 'manual'])) {
|
||||
return {
|
||||
code: 'manual_turnstile_required',
|
||||
category: 'verification',
|
||||
title: '需要人工验证',
|
||||
actionHint: '浏览器先人工签到一次',
|
||||
detailHint: '站点开启了 Turnstile 人机验证,自动签到无法直接通过。',
|
||||
};
|
||||
}
|
||||
|
||||
if (includesAny(text, ['cloudflare tunnel error', 'error 1033', 'unable to resolve it'])) {
|
||||
return {
|
||||
code: 'cloudflare_tunnel_unavailable',
|
||||
category: 'network',
|
||||
title: '站点隧道不可用',
|
||||
actionHint: '稍后重试或联系站点方',
|
||||
detailHint: 'Cloudflare Tunnel 当前不可达,通常是站点侧网络或隧道进程问题。',
|
||||
};
|
||||
}
|
||||
|
||||
if (isCloudflareChallenge(rawMessage)) {
|
||||
return {
|
||||
code: 'cloudflare_challenge',
|
||||
category: 'verification',
|
||||
title: '触发 Cloudflare 验证',
|
||||
actionHint: '降低频率并稍后重试',
|
||||
detailHint: '请求触发了防护挑战,建议稍后再试或更换稳定站点。',
|
||||
};
|
||||
}
|
||||
|
||||
if (isTokenExpiredError({ status: httpStatus > 0 ? httpStatus : undefined, message: rawMessage })) {
|
||||
return {
|
||||
code: 'token_expired',
|
||||
category: 'auth',
|
||||
title: '令牌失效',
|
||||
actionHint: '重新登录或同步新令牌',
|
||||
detailHint: '账号访问令牌可能过期或无效,需更新认证信息。',
|
||||
};
|
||||
}
|
||||
|
||||
if (includesAny(text, ['already checked in', 'already signed', '今天已经签到', '今日已签到', '已经签到'])) {
|
||||
return {
|
||||
code: 'already_checked_in',
|
||||
category: 'state',
|
||||
title: '今日已签到',
|
||||
actionHint: '无需重复执行',
|
||||
detailHint: '该账号当天签到已完成,重复请求会被站点拒绝或跳过。',
|
||||
};
|
||||
}
|
||||
|
||||
if (includesAny(text, ['timeout', 'timed out', 'etimedout', '请求超时'])) {
|
||||
return {
|
||||
code: 'network_timeout',
|
||||
category: 'network',
|
||||
title: '请求超时',
|
||||
actionHint: '稍后重试并检查网络',
|
||||
detailHint: '请求在超时时间内未完成,可能是网络波动或站点响应慢。',
|
||||
};
|
||||
}
|
||||
|
||||
if (httpStatus >= 500 || includesAny(text, ['http 5', 'upstream', 'internal server error'])) {
|
||||
return {
|
||||
code: 'upstream_error',
|
||||
category: 'site',
|
||||
title: '上游站点错误',
|
||||
actionHint: '稍后重试',
|
||||
detailHint: '站点返回服务端错误,通常需要站点恢复后才可成功。',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
code: 'unknown_error',
|
||||
category: 'unknown',
|
||||
title: status === 'success' ? '执行成功' : '未知错误',
|
||||
actionHint: status === 'success' ? '无需操作' : '查看详细日志后重试',
|
||||
detailHint: status === 'success'
|
||||
? '任务已成功完成。'
|
||||
: '暂未识别到明确错误类型,可根据原始信息进一步排查。',
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,64 @@
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
function pad2(value: number): string {
|
||||
return String(value).padStart(2, '0');
|
||||
}
|
||||
|
||||
export function formatLocalDate(value: Date): string {
|
||||
return `${value.getFullYear()}-${pad2(value.getMonth() + 1)}-${pad2(value.getDate())}`;
|
||||
}
|
||||
|
||||
export function formatLocalDateTime(value: Date): string {
|
||||
return `${formatLocalDate(value)} ${pad2(value.getHours())}:${pad2(value.getMinutes())}:${pad2(value.getSeconds())}`;
|
||||
}
|
||||
|
||||
export function getResolvedTimeZone(): string {
|
||||
return Intl.DateTimeFormat().resolvedOptions().timeZone || 'Local';
|
||||
}
|
||||
|
||||
export function formatUtcSqlDateTime(value: Date): string {
|
||||
return `${value.getUTCFullYear()}-${pad2(value.getUTCMonth() + 1)}-${pad2(value.getUTCDate())} ${pad2(value.getUTCHours())}:${pad2(value.getUTCMinutes())}:${pad2(value.getUTCSeconds())}`;
|
||||
}
|
||||
|
||||
export function parseStoredUtcDateTime(raw: string | null | undefined): Date | null {
|
||||
if (!raw) return null;
|
||||
const text = raw.trim();
|
||||
if (!text) return null;
|
||||
|
||||
let parsed: Date;
|
||||
if (/^\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}$/.test(text)) {
|
||||
parsed = new Date(`${text.replace(' ', 'T')}Z`);
|
||||
} else {
|
||||
parsed = new Date(text);
|
||||
}
|
||||
|
||||
if (Number.isNaN(parsed.getTime())) return null;
|
||||
return parsed;
|
||||
}
|
||||
|
||||
export function toLocalDayKeyFromStoredUtc(raw: string | null | undefined): string | null {
|
||||
const parsed = parseStoredUtcDateTime(raw);
|
||||
if (!parsed) return null;
|
||||
return formatLocalDate(parsed);
|
||||
}
|
||||
|
||||
export function getLocalDayRangeUtc(now = new Date()): {
|
||||
localDay: string;
|
||||
startUtc: string;
|
||||
endUtc: string;
|
||||
} {
|
||||
const localStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
|
||||
const nextLocalStart = new Date(localStart.getTime() + DAY_MS);
|
||||
return {
|
||||
localDay: formatLocalDate(now),
|
||||
startUtc: formatUtcSqlDateTime(localStart),
|
||||
endUtc: formatUtcSqlDateTime(nextLocalStart),
|
||||
};
|
||||
}
|
||||
|
||||
export function getLocalRangeStartUtc(days: number, now = new Date()): string {
|
||||
const normalizedDays = Math.max(1, Math.floor(days || 1));
|
||||
const localStart = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
|
||||
const start = new Date(localStart.getTime() - (normalizedDays - 1) * DAY_MS);
|
||||
return formatUtcSqlDateTime(start);
|
||||
}
|
||||
@@ -0,0 +1,151 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildModelAnalysis } from './modelAnalysisService.js';
|
||||
import { formatLocalDate } from './localTimeService.js';
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
describe('buildModelAnalysis', () => {
|
||||
it('aggregates spend/calls data in a rolling day window', () => {
|
||||
const logs = [
|
||||
{
|
||||
createdAt: '2026-02-24T02:00:00.000Z',
|
||||
modelActual: 'gpt-4o',
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: 800,
|
||||
totalTokens: 1000,
|
||||
estimatedCost: 1.25,
|
||||
},
|
||||
{
|
||||
createdAt: '2026-02-24T06:00:00.000Z',
|
||||
modelActual: null,
|
||||
modelRequested: 'gpt-4o',
|
||||
status: 'failed',
|
||||
latencyMs: 1200,
|
||||
totalTokens: 300,
|
||||
estimatedCost: 0.25,
|
||||
},
|
||||
{
|
||||
createdAt: '2026-02-23T08:00:00.000Z',
|
||||
modelActual: 'claude-3-5-sonnet',
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: 500,
|
||||
totalTokens: 2000,
|
||||
estimatedCost: 2,
|
||||
},
|
||||
{
|
||||
createdAt: '2026-02-19T10:00:00.000Z',
|
||||
modelActual: 'gpt-4.1-mini',
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: 200,
|
||||
totalTokens: 120,
|
||||
estimatedCost: 0.08,
|
||||
},
|
||||
];
|
||||
|
||||
const now = new Date('2026-02-24T12:00:00.000Z');
|
||||
const result = buildModelAnalysis(logs, {
|
||||
now,
|
||||
days: 3,
|
||||
});
|
||||
|
||||
const endDay = new Date(now.getFullYear(), now.getMonth(), now.getDate(), 0, 0, 0, 0);
|
||||
const startDay = new Date(endDay.getTime() - 2 * DAY_MS);
|
||||
const dayKeys = Array.from({ length: 3 }, (_, index) =>
|
||||
formatLocalDate(new Date(startDay.getTime() + index * DAY_MS)),
|
||||
);
|
||||
const daySet = new Set(dayKeys);
|
||||
const spendByDay = new Map<string, number>(dayKeys.map((key) => [key, 0]));
|
||||
for (const log of logs) {
|
||||
const createdAt = new Date(log.createdAt);
|
||||
const dayKey = formatLocalDate(createdAt);
|
||||
if (!daySet.has(dayKey)) continue;
|
||||
spendByDay.set(dayKey, (spendByDay.get(dayKey) ?? 0) + (log.estimatedCost ?? 0));
|
||||
}
|
||||
|
||||
expect(result.window).toEqual({
|
||||
start: dayKeys[0],
|
||||
end: dayKeys[dayKeys.length - 1],
|
||||
days: 3,
|
||||
});
|
||||
expect(result.totals.calls).toBe(3);
|
||||
expect(result.totals.tokens).toBe(3300);
|
||||
expect(result.totals.spend).toBe(3.5);
|
||||
expect(result.spendTrend).toEqual(dayKeys.map((day) => ({
|
||||
day,
|
||||
spend: spendByDay.get(day) ?? 0,
|
||||
})));
|
||||
expect(result.callRanking[0]).toMatchObject({
|
||||
model: 'gpt-4o',
|
||||
calls: 2,
|
||||
successRate: 50,
|
||||
});
|
||||
});
|
||||
|
||||
it('normalizes unknown model names and invalid numbers', () => {
|
||||
const logs = [
|
||||
{
|
||||
createdAt: '2026-02-24T12:00:00.000Z',
|
||||
modelActual: null,
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: null,
|
||||
totalTokens: null,
|
||||
estimatedCost: null,
|
||||
},
|
||||
{
|
||||
createdAt: 'invalid-date',
|
||||
modelActual: 'gpt-4o',
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: 10,
|
||||
totalTokens: 10,
|
||||
estimatedCost: 1,
|
||||
},
|
||||
];
|
||||
|
||||
const result = buildModelAnalysis(logs, {
|
||||
now: new Date('2026-02-24T12:00:00.000Z'),
|
||||
days: 1,
|
||||
});
|
||||
|
||||
expect(result.totals).toEqual({
|
||||
calls: 1,
|
||||
tokens: 0,
|
||||
spend: 0,
|
||||
});
|
||||
expect(result.callsDistribution[0]).toMatchObject({
|
||||
model: 'unknown',
|
||||
calls: 1,
|
||||
share: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to token-based spend estimation when estimatedCost is missing', () => {
|
||||
const logs = [
|
||||
{
|
||||
createdAt: '2026-02-24T12:30:00.000Z',
|
||||
modelActual: 'gpt-4o-mini',
|
||||
modelRequested: null,
|
||||
status: 'success',
|
||||
latencyMs: 120,
|
||||
totalTokens: 1000,
|
||||
estimatedCost: null,
|
||||
},
|
||||
];
|
||||
|
||||
const result = buildModelAnalysis(logs, {
|
||||
now: new Date('2026-02-24T12:00:00.000Z'),
|
||||
days: 1,
|
||||
});
|
||||
|
||||
expect(result.totals.spend).toBe(0.002);
|
||||
expect(result.spendDistribution[0]).toMatchObject({
|
||||
model: 'gpt-4o-mini',
|
||||
spend: 0.002,
|
||||
calls: 1,
|
||||
});
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,214 @@
|
||||
import { formatLocalDate, parseStoredUtcDateTime } from './localTimeService.js';
|
||||
|
||||
const DAY_MS = 24 * 60 * 60 * 1000;
|
||||
|
||||
interface ProxyLogLike {
|
||||
createdAt: string | null;
|
||||
modelActual: string | null;
|
||||
modelRequested: string | null;
|
||||
status: string | null;
|
||||
latencyMs: number | null;
|
||||
totalTokens: number | null;
|
||||
estimatedCost: number | null;
|
||||
}
|
||||
|
||||
interface ModelAnalysisResult {
|
||||
window: {
|
||||
start: string;
|
||||
end: string;
|
||||
days: number;
|
||||
};
|
||||
totals: {
|
||||
calls: number;
|
||||
tokens: number;
|
||||
spend: number;
|
||||
};
|
||||
spendDistribution: Array<{
|
||||
model: string;
|
||||
spend: number;
|
||||
calls: number;
|
||||
}>;
|
||||
spendTrend: Array<{
|
||||
day: string;
|
||||
spend: number;
|
||||
}>;
|
||||
callsDistribution: Array<{
|
||||
model: string;
|
||||
calls: number;
|
||||
share: number;
|
||||
}>;
|
||||
callRanking: Array<{
|
||||
model: string;
|
||||
calls: number;
|
||||
successRate: number;
|
||||
avgLatencyMs: number;
|
||||
spend: number;
|
||||
tokens: number;
|
||||
}>;
|
||||
}
|
||||
|
||||
interface BuildOptions {
|
||||
now?: Date;
|
||||
days?: number;
|
||||
maxModels?: number;
|
||||
}
|
||||
|
||||
interface MutableModelStats {
|
||||
model: string;
|
||||
calls: number;
|
||||
success: number;
|
||||
latencyTotal: number;
|
||||
tokens: number;
|
||||
spend: number;
|
||||
}
|
||||
|
||||
function startOfLocalDay(value: Date): Date {
|
||||
return new Date(
|
||||
value.getFullYear(),
|
||||
value.getMonth(),
|
||||
value.getDate(),
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
0,
|
||||
);
|
||||
}
|
||||
|
||||
function dayKey(value: Date): string {
|
||||
return formatLocalDate(value);
|
||||
}
|
||||
|
||||
function toSafeNumber(value: unknown): number {
|
||||
if (typeof value !== 'number' || Number.isNaN(value) || !Number.isFinite(value)) return 0;
|
||||
return value;
|
||||
}
|
||||
|
||||
function toPositiveInt(value: unknown): number {
|
||||
return Math.max(0, Math.round(toSafeNumber(value)));
|
||||
}
|
||||
|
||||
function round(value: number, digits = 2): number {
|
||||
const factor = 10 ** digits;
|
||||
return Math.round(value * factor) / factor;
|
||||
}
|
||||
|
||||
function resolveModelName(log: ProxyLogLike): string {
|
||||
const raw = (log.modelActual || log.modelRequested || '').trim();
|
||||
return raw.length > 0 ? raw : 'unknown';
|
||||
}
|
||||
|
||||
function resolveSpend(log: ProxyLogLike, tokens: number): number {
|
||||
const explicit = toSafeNumber(log.estimatedCost);
|
||||
if (explicit > 0) return explicit;
|
||||
if (tokens <= 0) return 0;
|
||||
// Fallback to OneAPI/NewAPI quota conversion when explicit cost is absent.
|
||||
return tokens / 500000;
|
||||
}
|
||||
|
||||
export function buildModelAnalysis(logs: ProxyLogLike[], options: BuildOptions = {}): ModelAnalysisResult {
|
||||
const now = options.now ?? new Date();
|
||||
const days = Math.max(1, options.days ?? 7);
|
||||
const maxModels = Math.max(1, options.maxModels ?? 10);
|
||||
|
||||
const endDay = startOfLocalDay(now);
|
||||
const startDay = new Date(endDay.getTime() - (days - 1) * DAY_MS);
|
||||
|
||||
const dayKeys: string[] = [];
|
||||
for (let i = 0; i < days; i++) {
|
||||
dayKeys.push(dayKey(new Date(startDay.getTime() + i * DAY_MS)));
|
||||
}
|
||||
|
||||
const daySet = new Set(dayKeys);
|
||||
const spendTrendMap = new Map<string, number>(dayKeys.map((key) => [key, 0]));
|
||||
const modelMap = new Map<string, MutableModelStats>();
|
||||
|
||||
for (const log of logs) {
|
||||
if (!log?.createdAt) continue;
|
||||
const createdAt = parseStoredUtcDateTime(log.createdAt);
|
||||
if (!createdAt) continue;
|
||||
|
||||
const createdAtDay = dayKey(createdAt);
|
||||
if (!daySet.has(createdAtDay)) continue;
|
||||
|
||||
const model = resolveModelName(log);
|
||||
const tokens = toPositiveInt(log.totalTokens);
|
||||
const spend = Math.max(0, resolveSpend(log, tokens));
|
||||
const latency = toPositiveInt(log.latencyMs);
|
||||
const isSuccess = (log.status || '').toLowerCase() === 'success';
|
||||
|
||||
const stat = modelMap.get(model) ?? {
|
||||
model,
|
||||
calls: 0,
|
||||
success: 0,
|
||||
latencyTotal: 0,
|
||||
tokens: 0,
|
||||
spend: 0,
|
||||
};
|
||||
|
||||
stat.calls += 1;
|
||||
stat.success += isSuccess ? 1 : 0;
|
||||
stat.latencyTotal += latency;
|
||||
stat.tokens += tokens;
|
||||
stat.spend += spend;
|
||||
modelMap.set(model, stat);
|
||||
|
||||
spendTrendMap.set(createdAtDay, (spendTrendMap.get(createdAtDay) ?? 0) + spend);
|
||||
}
|
||||
|
||||
const stats = Array.from(modelMap.values());
|
||||
const totalCalls = stats.reduce((sum, item) => sum + item.calls, 0);
|
||||
const totalTokens = stats.reduce((sum, item) => sum + item.tokens, 0);
|
||||
const totalSpend = round(stats.reduce((sum, item) => sum + item.spend, 0), 6);
|
||||
|
||||
const spendDistribution = [...stats]
|
||||
.sort((a, b) => b.spend - a.spend)
|
||||
.slice(0, maxModels)
|
||||
.map((item) => ({
|
||||
model: item.model,
|
||||
spend: round(item.spend, 6),
|
||||
calls: item.calls,
|
||||
}));
|
||||
|
||||
const callsDistribution = [...stats]
|
||||
.sort((a, b) => b.calls - a.calls)
|
||||
.slice(0, maxModels)
|
||||
.map((item) => ({
|
||||
model: item.model,
|
||||
calls: item.calls,
|
||||
share: totalCalls > 0 ? round((item.calls / totalCalls) * 100, 2) : 0,
|
||||
}));
|
||||
|
||||
const callRanking = [...stats]
|
||||
.sort((a, b) => b.calls - a.calls)
|
||||
.slice(0, maxModels)
|
||||
.map((item) => ({
|
||||
model: item.model,
|
||||
calls: item.calls,
|
||||
successRate: item.calls > 0 ? round((item.success / item.calls) * 100, 2) : 0,
|
||||
avgLatencyMs: item.calls > 0 ? Math.round(item.latencyTotal / item.calls) : 0,
|
||||
spend: round(item.spend, 6),
|
||||
tokens: item.tokens,
|
||||
}));
|
||||
|
||||
const spendTrend = dayKeys.map((day) => ({
|
||||
day,
|
||||
spend: round(spendTrendMap.get(day) ?? 0, 6),
|
||||
}));
|
||||
|
||||
return {
|
||||
window: {
|
||||
start: dayKeys[0],
|
||||
end: dayKeys[dayKeys.length - 1],
|
||||
days,
|
||||
},
|
||||
totals: {
|
||||
calls: totalCalls,
|
||||
tokens: totalTokens,
|
||||
spend: totalSpend,
|
||||
},
|
||||
spendDistribution,
|
||||
spendTrend,
|
||||
callsDistribution,
|
||||
callRanking,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1,101 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { calculateModelUsageCost, fallbackTokenCost, type PricingModel } from './modelPricingService.js';
|
||||
|
||||
describe('modelPricingService', () => {
|
||||
it('calculates token-based cost from model ratio and completion ratio', () => {
|
||||
const model: PricingModel = {
|
||||
modelName: 'gpt-4o',
|
||||
quotaType: 0,
|
||||
modelRatio: 2,
|
||||
completionRatio: 1.5,
|
||||
modelPrice: null,
|
||||
enableGroups: ['vip'],
|
||||
};
|
||||
|
||||
const cost = calculateModelUsageCost(
|
||||
model,
|
||||
{
|
||||
promptTokens: 1000,
|
||||
completionTokens: 500,
|
||||
totalTokens: 1500,
|
||||
},
|
||||
{ default: 1, vip: 2 },
|
||||
);
|
||||
|
||||
expect(cost).toBe(0.014);
|
||||
});
|
||||
|
||||
it('falls back to total tokens when split token usage is missing', () => {
|
||||
const model: PricingModel = {
|
||||
modelName: 'claude-sonnet',
|
||||
quotaType: 0,
|
||||
modelRatio: 1,
|
||||
completionRatio: 2,
|
||||
modelPrice: null,
|
||||
enableGroups: ['default'],
|
||||
};
|
||||
|
||||
const cost = calculateModelUsageCost(
|
||||
model,
|
||||
{
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 2000,
|
||||
},
|
||||
{ default: 1 },
|
||||
);
|
||||
|
||||
expect(cost).toBe(0.004);
|
||||
});
|
||||
|
||||
it('calculates per-call cost when quota type is call-based', () => {
|
||||
const model: PricingModel = {
|
||||
modelName: 'gpt-image-1',
|
||||
quotaType: 1,
|
||||
modelRatio: 1,
|
||||
completionRatio: 1,
|
||||
modelPrice: 0.3,
|
||||
enableGroups: ['vip'],
|
||||
};
|
||||
|
||||
const cost = calculateModelUsageCost(
|
||||
model,
|
||||
{
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0,
|
||||
},
|
||||
{ default: 1, vip: 1.5 },
|
||||
);
|
||||
|
||||
expect(cost).toBe(0.45);
|
||||
});
|
||||
|
||||
it('calculates times-based per-call cost from input ratio only', () => {
|
||||
const model: PricingModel = {
|
||||
modelName: 'flux-kontext-pro',
|
||||
quotaType: 1,
|
||||
modelRatio: 1,
|
||||
completionRatio: 1,
|
||||
modelPrice: { input: 1, output: 3 },
|
||||
enableGroups: ['vip'],
|
||||
};
|
||||
|
||||
const cost = calculateModelUsageCost(
|
||||
model,
|
||||
{
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0,
|
||||
},
|
||||
{ default: 1, vip: 2 },
|
||||
);
|
||||
|
||||
expect(cost).toBe(0.004);
|
||||
});
|
||||
|
||||
it('uses platform-specific fallback token divisor', () => {
|
||||
expect(fallbackTokenCost(1500, 'new-api')).toBe(0.003);
|
||||
expect(fallbackTokenCost(1500, 'veloera')).toBe(0.0015);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,598 @@
|
||||
import type { RequestInit as UndiciRequestInit } from 'undici';
|
||||
|
||||
const PRICE_CACHE_TTL_MS = 10 * 60 * 1000;
|
||||
const PRICE_CACHE_FAILURE_TTL_MS = 60 * 1000;
|
||||
const PRICING_FETCH_TIMEOUT_MS = 8_000;
|
||||
const DEFAULT_GROUP = 'default';
|
||||
const ONE_HUB_PER_CALL_RATIO = 0.002;
|
||||
const MIN_ROUTING_REFERENCE_COST = 1e-6;
|
||||
const ROUTING_REFERENCE_USAGE = {
|
||||
promptTokens: 500_000,
|
||||
completionTokens: 500_000,
|
||||
totalTokens: 1_000_000,
|
||||
};
|
||||
|
||||
export interface PricingModel {
|
||||
modelName: string;
|
||||
quotaType: number;
|
||||
modelRatio: number;
|
||||
completionRatio: number;
|
||||
modelPrice: number | { input: number; output: number } | null;
|
||||
enableGroups: string[];
|
||||
modelDescription?: string | null;
|
||||
tags?: string[];
|
||||
supportedEndpointTypes?: string[];
|
||||
ownerBy?: string | null;
|
||||
}
|
||||
|
||||
interface PricingData {
|
||||
models: Map<string, PricingModel>;
|
||||
groupRatio: Record<string, number>;
|
||||
}
|
||||
|
||||
interface PricingCacheEntry {
|
||||
fetchedAt: number;
|
||||
ttlMs: number;
|
||||
data: PricingData | null;
|
||||
}
|
||||
|
||||
interface RoutingReferenceCostCacheEntry {
|
||||
fetchedAt: number;
|
||||
ttlMs: number;
|
||||
costs: Map<string, number>;
|
||||
}
|
||||
|
||||
interface EstimateProxyCostInput {
|
||||
site: {
|
||||
id: number;
|
||||
url: string;
|
||||
platform: string;
|
||||
apiKey?: string | null;
|
||||
};
|
||||
account: {
|
||||
id: number;
|
||||
accessToken?: string | null;
|
||||
apiToken?: string | null;
|
||||
};
|
||||
modelName: string;
|
||||
promptTokens?: number;
|
||||
completionTokens?: number;
|
||||
totalTokens?: number;
|
||||
}
|
||||
|
||||
interface ModelGroupPricing {
|
||||
quotaType: number;
|
||||
inputPerMillion?: number;
|
||||
outputPerMillion?: number;
|
||||
perCallInput?: number;
|
||||
perCallOutput?: number;
|
||||
perCallTotal?: number;
|
||||
}
|
||||
|
||||
interface ModelPricingCatalogEntry {
|
||||
modelName: string;
|
||||
quotaType: number;
|
||||
modelDescription: string | null;
|
||||
tags: string[];
|
||||
supportedEndpointTypes: string[];
|
||||
ownerBy: string | null;
|
||||
enableGroups: string[];
|
||||
groupPricing: Record<string, ModelGroupPricing>;
|
||||
}
|
||||
|
||||
interface ModelPricingCatalog {
|
||||
models: ModelPricingCatalogEntry[];
|
||||
groupRatio: Record<string, number>;
|
||||
}
|
||||
|
||||
const pricingCache = new Map<string, PricingCacheEntry>();
|
||||
const routingReferenceCostCache = new Map<string, RoutingReferenceCostCacheEntry>();
|
||||
|
||||
function toNumber(value: unknown, fallback = 0): number {
|
||||
const n = typeof value === 'number' ? value : Number(value);
|
||||
if (!Number.isFinite(n)) return fallback;
|
||||
return n;
|
||||
}
|
||||
|
||||
function toPositiveInt(value: unknown): number {
|
||||
return Math.max(0, Math.round(toNumber(value, 0)));
|
||||
}
|
||||
|
||||
function roundCost(value: number): number {
|
||||
return Math.round(Math.max(0, value) * 1_000_000) / 1_000_000;
|
||||
}
|
||||
|
||||
function normalizeModelPrice(value: unknown): number | { input: number; output: number } | null {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) return value;
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
|
||||
const input = toNumber((value as any).input, Number.NaN);
|
||||
const output = toNumber((value as any).output, Number.NaN);
|
||||
if (Number.isNaN(input) && Number.isNaN(output)) return null;
|
||||
|
||||
return {
|
||||
input: Number.isNaN(input) ? 0 : input,
|
||||
output: Number.isNaN(output) ? 0 : output,
|
||||
};
|
||||
}
|
||||
|
||||
function normalizeGroupRatio(raw: unknown): Record<string, number> {
|
||||
const result: Record<string, number> = {};
|
||||
if (raw && typeof raw === 'object') {
|
||||
for (const [key, value] of Object.entries(raw as Record<string, unknown>)) {
|
||||
const ratio = toNumber(value, 1);
|
||||
if (ratio > 0) result[key] = ratio;
|
||||
}
|
||||
}
|
||||
|
||||
if (Object.keys(result).length === 0) {
|
||||
result[DEFAULT_GROUP] = 1;
|
||||
} else if (!(DEFAULT_GROUP in result)) {
|
||||
result[DEFAULT_GROUP] = 1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
function normalizeStringArray(raw: unknown): string[] {
|
||||
if (Array.isArray(raw)) {
|
||||
return raw.map((item) => String(item || '').trim()).filter(Boolean);
|
||||
}
|
||||
|
||||
if (typeof raw === 'string') {
|
||||
return raw
|
||||
.split(',')
|
||||
.map((item) => item.trim())
|
||||
.filter(Boolean);
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
function normalizePricingModels(rawModels: unknown[]): Map<string, PricingModel> {
|
||||
const models = new Map<string, PricingModel>();
|
||||
|
||||
for (const raw of rawModels) {
|
||||
if (!raw || typeof raw !== 'object') continue;
|
||||
|
||||
const modelName = String((raw as any).model_name || '').trim();
|
||||
if (!modelName) continue;
|
||||
|
||||
const quotaType = toPositiveInt((raw as any).quota_type);
|
||||
const modelRatio = toNumber((raw as any).model_ratio, 1);
|
||||
const completionRatio = toNumber((raw as any).completion_ratio, 1);
|
||||
const enableGroupsRaw = (raw as any).enable_groups;
|
||||
const enableGroups = Array.isArray(enableGroupsRaw)
|
||||
? enableGroupsRaw.map((item: unknown) => String(item || '').trim()).filter(Boolean)
|
||||
: [DEFAULT_GROUP];
|
||||
const modelDescriptionRaw = (raw as any).model_description;
|
||||
const modelDescription = typeof modelDescriptionRaw === 'string'
|
||||
? (modelDescriptionRaw.trim() || null)
|
||||
: null;
|
||||
const tags = normalizeStringArray((raw as any).tags);
|
||||
const supportedEndpointTypes = normalizeStringArray((raw as any).supported_endpoint_types);
|
||||
const ownerByRaw = (raw as any).owner_by;
|
||||
const ownerBy = typeof ownerByRaw === 'string' ? (ownerByRaw.trim() || null) : null;
|
||||
|
||||
models.set(modelName, {
|
||||
modelName,
|
||||
quotaType,
|
||||
modelRatio: modelRatio > 0 ? modelRatio : 1,
|
||||
completionRatio: completionRatio > 0 ? completionRatio : 1,
|
||||
modelPrice: normalizeModelPrice((raw as any).model_price),
|
||||
enableGroups: enableGroups.length > 0 ? enableGroups : [DEFAULT_GROUP],
|
||||
modelDescription,
|
||||
tags,
|
||||
supportedEndpointTypes,
|
||||
ownerBy,
|
||||
});
|
||||
}
|
||||
|
||||
return models;
|
||||
}
|
||||
|
||||
function unwrapPayload(payload: unknown): unknown {
|
||||
if (!payload || typeof payload !== 'object') return payload;
|
||||
if ('data' in (payload as any)) return (payload as any).data;
|
||||
return payload;
|
||||
}
|
||||
|
||||
function normalizeCommonPricingPayload(payload: unknown): PricingData | null {
|
||||
const maybeData = unwrapPayload(payload);
|
||||
if (!Array.isArray(maybeData)) return null;
|
||||
|
||||
const models = normalizePricingModels(maybeData);
|
||||
if (models.size === 0) return null;
|
||||
|
||||
const groupRatio = normalizeGroupRatio((payload as any)?.group_ratio);
|
||||
return { models, groupRatio };
|
||||
}
|
||||
|
||||
function normalizeOneHubPricingPayload(availablePayload: unknown, groupPayload: unknown): PricingData | null {
|
||||
const available = unwrapPayload(availablePayload);
|
||||
if (!available || typeof available !== 'object') return null;
|
||||
|
||||
const transformed: unknown[] = [];
|
||||
for (const [modelName, rawValue] of Object.entries(available as Record<string, unknown>)) {
|
||||
const item = rawValue as any;
|
||||
const price = item?.price || {};
|
||||
const input = toNumber(price.input, 0);
|
||||
const output = toNumber(price.output, input);
|
||||
const isTokenType = String(price.type || '').toLowerCase() === 'tokens';
|
||||
|
||||
transformed.push({
|
||||
model_name: modelName,
|
||||
model_description: item?.description || item?.desc || '',
|
||||
quota_type: isTokenType ? 0 : 1,
|
||||
model_ratio: 1,
|
||||
completion_ratio: input > 0 ? output / input : 1,
|
||||
model_price: { input, output },
|
||||
enable_groups: Array.isArray(item?.groups) && item.groups.length > 0 ? item.groups : [DEFAULT_GROUP],
|
||||
supported_endpoint_types: Array.isArray(item?.supported_endpoint_types) ? item.supported_endpoint_types : [],
|
||||
tags: Array.isArray(item?.tags) ? item.tags : [],
|
||||
owner_by: item?.owned_by || item?.provider || null,
|
||||
});
|
||||
}
|
||||
|
||||
const models = normalizePricingModels(transformed);
|
||||
if (models.size === 0) return null;
|
||||
|
||||
const groupMap = unwrapPayload(groupPayload);
|
||||
const groupRatioSource: Record<string, number> = {};
|
||||
if (groupMap && typeof groupMap === 'object') {
|
||||
for (const [key, group] of Object.entries(groupMap as Record<string, any>)) {
|
||||
groupRatioSource[key] = toNumber(group?.ratio, 1);
|
||||
}
|
||||
}
|
||||
|
||||
const groupRatio = normalizeGroupRatio(groupRatioSource);
|
||||
return { models, groupRatio };
|
||||
}
|
||||
|
||||
async function fetchJson(url: string, options?: UndiciRequestInit): Promise<unknown> {
|
||||
const { fetch } = await import('undici');
|
||||
const controller = new AbortController();
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, PRICING_FETCH_TIMEOUT_MS);
|
||||
|
||||
try {
|
||||
const response = await fetch(url, {
|
||||
...options,
|
||||
signal: controller.signal,
|
||||
body: options?.body ?? undefined,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options?.headers,
|
||||
},
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
} catch (error: any) {
|
||||
if (error?.name === 'AbortError') {
|
||||
throw new Error(`pricing fetch timeout (${Math.round(PRICING_FETCH_TIMEOUT_MS / 1000)}s)`);
|
||||
}
|
||||
throw error;
|
||||
} finally {
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle);
|
||||
timeoutHandle = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
function normalizeUrl(baseUrl: string): string {
|
||||
return baseUrl.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function buildTokenCandidates(input: EstimateProxyCostInput): string[] {
|
||||
const candidates = [
|
||||
input.account.accessToken,
|
||||
input.account.apiToken,
|
||||
input.site.apiKey,
|
||||
].filter((value): value is string => typeof value === 'string' && value.trim().length > 0);
|
||||
|
||||
return Array.from(new Set(candidates));
|
||||
}
|
||||
|
||||
async function fetchCommonPricing(baseUrl: string, token?: string): Promise<PricingData | null> {
|
||||
const headers: Record<string, string> = {};
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
const payload = await fetchJson(`${baseUrl}/api/pricing`, { headers });
|
||||
return normalizeCommonPricingPayload(payload);
|
||||
}
|
||||
|
||||
async function fetchOneHubPricing(baseUrl: string, token?: string): Promise<PricingData | null> {
|
||||
const headers: Record<string, string> = {};
|
||||
if (token) headers.Authorization = `Bearer ${token}`;
|
||||
|
||||
const [availablePayload, groupPayload] = await Promise.all([
|
||||
fetchJson(`${baseUrl}/api/available_model`, { headers }),
|
||||
fetchJson(`${baseUrl}/api/user_group_map`, { headers }),
|
||||
]);
|
||||
|
||||
return normalizeOneHubPricingPayload(availablePayload, groupPayload);
|
||||
}
|
||||
|
||||
function getCacheKey(input: EstimateProxyCostInput): string {
|
||||
return `${input.site.id}:${input.account.id}`;
|
||||
}
|
||||
|
||||
function normalizeModelKey(modelName: string): string {
|
||||
return modelName.trim().toLowerCase();
|
||||
}
|
||||
|
||||
function buildRoutingReferenceCostMap(data: PricingData): Map<string, number> {
|
||||
const costs = new Map<string, number>();
|
||||
for (const model of data.models.values()) {
|
||||
const cost = calculateModelUsageCost(model, ROUTING_REFERENCE_USAGE, data.groupRatio);
|
||||
if (!Number.isFinite(cost)) continue;
|
||||
costs.set(normalizeModelKey(model.modelName), Math.max(cost, MIN_ROUTING_REFERENCE_COST));
|
||||
}
|
||||
return costs;
|
||||
}
|
||||
|
||||
function syncRoutingReferenceCostCache(
|
||||
key: string,
|
||||
fetchedAt: number,
|
||||
ttlMs: number,
|
||||
data: PricingData | null,
|
||||
): void {
|
||||
if (!data) {
|
||||
routingReferenceCostCache.delete(key);
|
||||
return;
|
||||
}
|
||||
|
||||
routingReferenceCostCache.set(key, {
|
||||
fetchedAt,
|
||||
ttlMs,
|
||||
costs: buildRoutingReferenceCostMap(data),
|
||||
});
|
||||
}
|
||||
|
||||
async function fetchPricingData(input: EstimateProxyCostInput): Promise<PricingData | null> {
|
||||
const baseUrl = normalizeUrl(input.site.url);
|
||||
const tokenCandidates = buildTokenCandidates(input);
|
||||
|
||||
const fetcher = input.site.platform === 'one-hub' || input.site.platform === 'done-hub'
|
||||
? fetchOneHubPricing
|
||||
: fetchCommonPricing;
|
||||
|
||||
for (const token of tokenCandidates) {
|
||||
try {
|
||||
const data = await fetcher(baseUrl, token);
|
||||
if (data && data.models.size > 0) return data;
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Some sites expose pricing publicly.
|
||||
try {
|
||||
const data = await fetcher(baseUrl, undefined);
|
||||
if (data && data.models.size > 0) return data;
|
||||
} catch {}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
async function getPricingDataCached(input: EstimateProxyCostInput): Promise<PricingData | null> {
|
||||
const key = getCacheKey(input);
|
||||
const now = Date.now();
|
||||
const cached = pricingCache.get(key);
|
||||
if (cached && now - cached.fetchedAt < cached.ttlMs) {
|
||||
if (cached.data && !routingReferenceCostCache.has(key)) {
|
||||
syncRoutingReferenceCostCache(key, cached.fetchedAt, cached.ttlMs, cached.data);
|
||||
}
|
||||
return cached.data;
|
||||
}
|
||||
|
||||
const data = await fetchPricingData(input);
|
||||
const ttlMs = data ? PRICE_CACHE_TTL_MS : PRICE_CACHE_FAILURE_TTL_MS;
|
||||
pricingCache.set(key, {
|
||||
fetchedAt: now,
|
||||
ttlMs,
|
||||
data,
|
||||
});
|
||||
syncRoutingReferenceCostCache(key, now, ttlMs, data);
|
||||
return data;
|
||||
}
|
||||
|
||||
export function getCachedModelRoutingReferenceCost(input: {
|
||||
siteId: number;
|
||||
accountId: number;
|
||||
modelName: string;
|
||||
}): number | null {
|
||||
const key = `${input.siteId}:${input.accountId}`;
|
||||
const cached = routingReferenceCostCache.get(key);
|
||||
if (!cached) return null;
|
||||
|
||||
if (Date.now() - cached.fetchedAt >= cached.ttlMs) {
|
||||
return null;
|
||||
}
|
||||
|
||||
const cost = cached.costs.get(normalizeModelKey(input.modelName));
|
||||
if (typeof cost !== 'number' || !Number.isFinite(cost) || cost <= 0) {
|
||||
return null;
|
||||
}
|
||||
|
||||
return cost;
|
||||
}
|
||||
|
||||
function resolveModel(modelName: string, data: PricingData): PricingModel | null {
|
||||
const exact = data.models.get(modelName);
|
||||
if (exact) return exact;
|
||||
|
||||
const lower = modelName.toLowerCase();
|
||||
for (const [name, model] of data.models.entries()) {
|
||||
if (name.toLowerCase() === lower) return model;
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
function resolveGroupMultiplier(model: PricingModel, groupRatio: Record<string, number>): number {
|
||||
if (model.enableGroups.includes(DEFAULT_GROUP) && groupRatio[DEFAULT_GROUP]) {
|
||||
return groupRatio[DEFAULT_GROUP];
|
||||
}
|
||||
|
||||
for (const group of model.enableGroups) {
|
||||
if (groupRatio[group]) return groupRatio[group];
|
||||
}
|
||||
|
||||
const first = Object.values(groupRatio).find((ratio) => ratio > 0);
|
||||
return first || 1;
|
||||
}
|
||||
|
||||
function calculatePerCallCost(
|
||||
modelPrice: number | { input: number; output: number } | null,
|
||||
multiplier: number,
|
||||
): number {
|
||||
if (typeof modelPrice === 'number') {
|
||||
return modelPrice * multiplier;
|
||||
}
|
||||
|
||||
if (modelPrice && typeof modelPrice === 'object') {
|
||||
// done-hub/one-hub times pricing follows input ratio only.
|
||||
return toNumber(modelPrice.input, 0) * multiplier * ONE_HUB_PER_CALL_RATIO;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function calculatePerCallPricing(
|
||||
modelPrice: number | { input: number; output: number } | null,
|
||||
multiplier: number,
|
||||
): { input?: number; output?: number; total: number } {
|
||||
if (typeof modelPrice === 'number') {
|
||||
const total = roundCost(modelPrice * multiplier);
|
||||
return { total };
|
||||
}
|
||||
|
||||
if (modelPrice && typeof modelPrice === 'object') {
|
||||
const input = roundCost(toNumber(modelPrice.input, 0) * multiplier * ONE_HUB_PER_CALL_RATIO);
|
||||
const output = roundCost(toNumber(modelPrice.output, 0) * multiplier * ONE_HUB_PER_CALL_RATIO);
|
||||
return {
|
||||
input,
|
||||
output,
|
||||
total: input,
|
||||
};
|
||||
}
|
||||
|
||||
return { total: 0 };
|
||||
}
|
||||
|
||||
export function calculateModelUsageCost(
|
||||
model: PricingModel,
|
||||
usage: { promptTokens: number; completionTokens: number; totalTokens: number },
|
||||
groupRatio: Record<string, number>,
|
||||
): number {
|
||||
const multiplier = resolveGroupMultiplier(model, groupRatio);
|
||||
|
||||
if (model.quotaType === 1) {
|
||||
return roundCost(calculatePerCallCost(model.modelPrice, multiplier));
|
||||
}
|
||||
|
||||
const totalTokens = toPositiveInt(usage.totalTokens);
|
||||
const promptTokens = toPositiveInt(usage.promptTokens);
|
||||
const completionTokens = toPositiveInt(usage.completionTokens);
|
||||
const hasSplit = promptTokens > 0 || completionTokens > 0;
|
||||
const effectivePrompt = hasSplit ? promptTokens : totalTokens;
|
||||
const effectiveCompletion = hasSplit ? completionTokens : 0;
|
||||
|
||||
const inputPerMillion = model.modelRatio * 2 * multiplier;
|
||||
const outputPerMillion = model.modelRatio * model.completionRatio * 2 * multiplier;
|
||||
|
||||
const cost = (effectivePrompt / 1_000_000) * inputPerMillion
|
||||
+ (effectiveCompletion / 1_000_000) * outputPerMillion;
|
||||
return roundCost(cost);
|
||||
}
|
||||
|
||||
export async function fetchModelPricingCatalog(input: EstimateProxyCostInput): Promise<ModelPricingCatalog | null> {
|
||||
const pricingData = await getPricingDataCached(input);
|
||||
if (!pricingData) return null;
|
||||
|
||||
const groups = Array.from(new Set([DEFAULT_GROUP, ...Object.keys(pricingData.groupRatio)]));
|
||||
const defaultMultiplier = pricingData.groupRatio[DEFAULT_GROUP] || 1;
|
||||
|
||||
const models: ModelPricingCatalogEntry[] = Array.from(pricingData.models.values())
|
||||
.map((model) => {
|
||||
const allowedGroups = Array.from(new Set([...(model.enableGroups || []), DEFAULT_GROUP]));
|
||||
const modelGroups = groups.filter((group) => allowedGroups.includes(group));
|
||||
const effectiveGroups = modelGroups.length > 0 ? modelGroups : [DEFAULT_GROUP];
|
||||
|
||||
const groupPricing = effectiveGroups.reduce<Record<string, ModelGroupPricing>>((acc, group) => {
|
||||
const multiplier = pricingData.groupRatio[group] || defaultMultiplier;
|
||||
if (model.quotaType === 1) {
|
||||
const perCall = calculatePerCallPricing(model.modelPrice, multiplier);
|
||||
acc[group] = {
|
||||
quotaType: 1,
|
||||
perCallInput: perCall.input,
|
||||
perCallOutput: perCall.output,
|
||||
perCallTotal: perCall.total,
|
||||
};
|
||||
return acc;
|
||||
}
|
||||
|
||||
acc[group] = {
|
||||
quotaType: 0,
|
||||
inputPerMillion: roundCost(model.modelRatio * 2 * multiplier),
|
||||
outputPerMillion: roundCost(model.modelRatio * model.completionRatio * 2 * multiplier),
|
||||
};
|
||||
return acc;
|
||||
}, {});
|
||||
|
||||
return {
|
||||
modelName: model.modelName,
|
||||
quotaType: model.quotaType,
|
||||
modelDescription: model.modelDescription || null,
|
||||
tags: model.tags || [],
|
||||
supportedEndpointTypes: model.supportedEndpointTypes || [],
|
||||
ownerBy: model.ownerBy || null,
|
||||
enableGroups: model.enableGroups || [DEFAULT_GROUP],
|
||||
groupPricing,
|
||||
};
|
||||
})
|
||||
.sort((a, b) => a.modelName.localeCompare(b.modelName));
|
||||
|
||||
return {
|
||||
models,
|
||||
groupRatio: pricingData.groupRatio,
|
||||
};
|
||||
}
|
||||
|
||||
export function fallbackTokenCost(totalTokens: number, platform: string): number {
|
||||
const divisor = platform === 'veloera' ? 1_000_000 : 500_000;
|
||||
return roundCost(toPositiveInt(totalTokens) / divisor);
|
||||
}
|
||||
|
||||
export async function estimateProxyCost(input: EstimateProxyCostInput): Promise<number> {
|
||||
const promptTokens = toPositiveInt(input.promptTokens);
|
||||
const completionTokens = toPositiveInt(input.completionTokens);
|
||||
const totalTokens = toPositiveInt(input.totalTokens || (promptTokens + completionTokens));
|
||||
|
||||
try {
|
||||
const pricingData = await getPricingDataCached(input);
|
||||
if (!pricingData) {
|
||||
return fallbackTokenCost(totalTokens, input.site.platform);
|
||||
}
|
||||
|
||||
const model = resolveModel(input.modelName, pricingData);
|
||||
if (!model) {
|
||||
return fallbackTokenCost(totalTokens, input.site.platform);
|
||||
}
|
||||
|
||||
return calculateModelUsageCost(model, { promptTokens, completionTokens, totalTokens }, pricingData.groupRatio);
|
||||
} catch {
|
||||
return fallbackTokenCost(totalTokens, input.site.platform);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,284 @@
|
||||
import { and, eq } from 'drizzle-orm';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { getAdapter } from './platforms/index.js';
|
||||
import { ensureDefaultTokenForAccount, getPreferredAccountToken } from './accountTokenService.js';
|
||||
import { resolvePlatformUserId } from './accountExtraConfig.js';
|
||||
|
||||
const API_TOKEN_DISCOVERY_TIMEOUT_MS = 8_000;
|
||||
const MODEL_DISCOVERY_TIMEOUT_MS = 12_000;
|
||||
const MODEL_REFRESH_BATCH_SIZE = 3;
|
||||
|
||||
function isSiteDisabled(status?: string | null): boolean {
|
||||
return (status || 'active') === 'disabled';
|
||||
}
|
||||
|
||||
function normalizeModels(models: string[]): string[] {
|
||||
return Array.from(new Set(models.filter((model) => typeof model === 'string' && model.trim().length > 0)));
|
||||
}
|
||||
|
||||
async function withTimeout<T>(fn: () => Promise<T>, timeoutMs: number, timeoutMessage: string): Promise<T> {
|
||||
let timer: ReturnType<typeof setTimeout> | null = null;
|
||||
try {
|
||||
return await Promise.race([
|
||||
fn(),
|
||||
new Promise<T>((_, reject) => {
|
||||
timer = setTimeout(() => reject(new Error(timeoutMessage)), timeoutMs);
|
||||
}),
|
||||
]);
|
||||
} finally {
|
||||
if (timer) clearTimeout(timer);
|
||||
}
|
||||
}
|
||||
|
||||
export async function refreshModelsForAccount(accountId: number) {
|
||||
const row = db.select().from(schema.accounts)
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(eq(schema.accounts.id, accountId))
|
||||
.get();
|
||||
|
||||
if (!row) {
|
||||
return { accountId, refreshed: false, modelCount: 0, reason: 'account_not_found' };
|
||||
}
|
||||
|
||||
const account = row.accounts;
|
||||
const site = row.sites;
|
||||
const adapter = getAdapter(site.platform);
|
||||
|
||||
const accountTokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(eq(schema.accountTokens.accountId, accountId))
|
||||
.all();
|
||||
|
||||
db.delete(schema.modelAvailability)
|
||||
.where(eq(schema.modelAvailability.accountId, accountId))
|
||||
.run();
|
||||
|
||||
for (const token of accountTokens) {
|
||||
db.delete(schema.tokenModelAvailability)
|
||||
.where(eq(schema.tokenModelAvailability.tokenId, token.id))
|
||||
.run();
|
||||
}
|
||||
|
||||
if (isSiteDisabled(site.status)) {
|
||||
return { accountId, refreshed: false, modelCount: 0, reason: 'site_disabled' };
|
||||
}
|
||||
|
||||
if (!adapter || account.status !== 'active') {
|
||||
return { accountId, refreshed: false, modelCount: 0, reason: 'adapter_or_status' };
|
||||
}
|
||||
|
||||
const platformUserId = resolvePlatformUserId(account.extraConfig, account.username);
|
||||
let discoveredApiToken: string | null = null;
|
||||
|
||||
if (!account.apiToken && account.accessToken) {
|
||||
try {
|
||||
discoveredApiToken = await withTimeout(
|
||||
() => adapter.getApiToken(site.url, account.accessToken, platformUserId),
|
||||
API_TOKEN_DISCOVERY_TIMEOUT_MS,
|
||||
`api token discovery timeout (${Math.round(API_TOKEN_DISCOVERY_TIMEOUT_MS / 1000)}s)`,
|
||||
);
|
||||
if (discoveredApiToken) {
|
||||
ensureDefaultTokenForAccount(account.id, discoveredApiToken, { name: 'default', source: 'sync' });
|
||||
db.update(schema.accounts).set({
|
||||
apiToken: discoveredApiToken,
|
||||
updatedAt: new Date().toISOString(),
|
||||
}).where(eq(schema.accounts.id, account.id)).run();
|
||||
}
|
||||
} catch {}
|
||||
}
|
||||
|
||||
let enabledTokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, account.id), eq(schema.accountTokens.enabled, true)))
|
||||
.all();
|
||||
|
||||
// Last fallback: if still no managed token but account has a legacy apiToken, mirror it into token table.
|
||||
if (enabledTokens.length === 0) {
|
||||
const fallback = discoveredApiToken || account.apiToken || null;
|
||||
if (fallback) {
|
||||
ensureDefaultTokenForAccount(account.id, fallback, { name: 'default', source: 'legacy' });
|
||||
enabledTokens = db.select()
|
||||
.from(schema.accountTokens)
|
||||
.where(and(eq(schema.accountTokens.accountId, account.id), eq(schema.accountTokens.enabled, true)))
|
||||
.all();
|
||||
}
|
||||
}
|
||||
|
||||
const accountModels = new Set<string>();
|
||||
const modelLatency = new Map<string, number | null>();
|
||||
let scannedTokenCount = 0;
|
||||
|
||||
for (const token of enabledTokens) {
|
||||
const startedAt = Date.now();
|
||||
let models: string[] = [];
|
||||
|
||||
try {
|
||||
models = normalizeModels(
|
||||
await withTimeout(
|
||||
() => adapter.getModels(site.url, token.token, platformUserId),
|
||||
MODEL_DISCOVERY_TIMEOUT_MS,
|
||||
`model discovery timeout (${Math.round(MODEL_DISCOVERY_TIMEOUT_MS / 1000)}s)`,
|
||||
),
|
||||
);
|
||||
} catch {
|
||||
models = [];
|
||||
}
|
||||
|
||||
if (models.length === 0) continue;
|
||||
|
||||
const latencyMs = Date.now() - startedAt;
|
||||
const checkedAt = new Date().toISOString();
|
||||
|
||||
db.insert(schema.tokenModelAvailability).values(
|
||||
models.map((modelName) => ({
|
||||
tokenId: token.id,
|
||||
modelName,
|
||||
available: true,
|
||||
latencyMs,
|
||||
checkedAt,
|
||||
})),
|
||||
).run();
|
||||
|
||||
scannedTokenCount++;
|
||||
|
||||
for (const modelName of models) {
|
||||
accountModels.add(modelName);
|
||||
const prev = modelLatency.get(modelName);
|
||||
if (prev === undefined || prev === null || latencyMs < prev) {
|
||||
modelLatency.set(modelName, latencyMs);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (accountModels.size > 0) {
|
||||
const checkedAt = new Date().toISOString();
|
||||
db.insert(schema.modelAvailability).values(
|
||||
Array.from(accountModels).map((modelName) => ({
|
||||
accountId: account.id,
|
||||
modelName,
|
||||
available: true,
|
||||
latencyMs: modelLatency.get(modelName) ?? null,
|
||||
checkedAt,
|
||||
})),
|
||||
).run();
|
||||
}
|
||||
|
||||
return {
|
||||
accountId,
|
||||
refreshed: true,
|
||||
modelCount: accountModels.size,
|
||||
tokenScanned: scannedTokenCount,
|
||||
discoveredApiToken: !!discoveredApiToken,
|
||||
};
|
||||
}
|
||||
|
||||
async function refreshModelsForAllActiveAccounts() {
|
||||
const accounts = db.select({ id: schema.accounts.id }).from(schema.accounts)
|
||||
.where(eq(schema.accounts.status, 'active'))
|
||||
.all();
|
||||
|
||||
const results = [];
|
||||
for (let offset = 0; offset < accounts.length; offset += MODEL_REFRESH_BATCH_SIZE) {
|
||||
const batch = accounts.slice(offset, offset + MODEL_REFRESH_BATCH_SIZE);
|
||||
const batchResults = await Promise.all(batch.map(async (account) => refreshModelsForAccount(account.id)));
|
||||
results.push(...batchResults);
|
||||
}
|
||||
return results;
|
||||
}
|
||||
|
||||
export function rebuildTokenRoutesFromAvailability() {
|
||||
const tokenRows = db.select().from(schema.tokenModelAvailability)
|
||||
.innerJoin(schema.accountTokens, eq(schema.tokenModelAvailability.tokenId, schema.accountTokens.id))
|
||||
.innerJoin(schema.accounts, eq(schema.accountTokens.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.where(
|
||||
and(
|
||||
eq(schema.tokenModelAvailability.available, true),
|
||||
eq(schema.accountTokens.enabled, true),
|
||||
eq(schema.accounts.status, 'active'),
|
||||
eq(schema.sites.status, 'active'),
|
||||
),
|
||||
)
|
||||
.all();
|
||||
|
||||
const modelTokens = new Map<string, Map<number, number>>();
|
||||
for (const row of tokenRows) {
|
||||
const modelName = row.token_model_availability.modelName;
|
||||
if (!modelTokens.has(modelName)) modelTokens.set(modelName, new Map<number, number>());
|
||||
modelTokens.get(modelName)!.set(row.account_tokens.id, row.accounts.id);
|
||||
}
|
||||
|
||||
const routes = db.select().from(schema.tokenRoutes).all();
|
||||
const channels = db.select().from(schema.routeChannels).all();
|
||||
|
||||
let createdRoutes = 0;
|
||||
let createdChannels = 0;
|
||||
let removedChannels = 0;
|
||||
|
||||
for (const [modelName, tokenAccountMap] of modelTokens.entries()) {
|
||||
let route = routes.find((r) => r.modelPattern === modelName);
|
||||
if (!route) {
|
||||
route = db.insert(schema.tokenRoutes).values({
|
||||
modelPattern: modelName,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
routes.push(route);
|
||||
createdRoutes++;
|
||||
}
|
||||
|
||||
const routeChannels = channels.filter((channel) => channel.routeId === route.id);
|
||||
const desiredTokenIds = new Set<number>(Array.from(tokenAccountMap.keys()));
|
||||
|
||||
for (const [tokenId, accountId] of tokenAccountMap.entries()) {
|
||||
const exists = routeChannels.some((channel) => channel.tokenId === tokenId);
|
||||
if (exists) continue;
|
||||
|
||||
const created = db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId,
|
||||
tokenId,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
manualOverride: false,
|
||||
}).returning().get();
|
||||
channels.push(created);
|
||||
createdChannels++;
|
||||
}
|
||||
|
||||
for (const channel of routeChannels) {
|
||||
if (channel.tokenId && desiredTokenIds.has(channel.tokenId)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!channel.tokenId) {
|
||||
const preferred = getPreferredAccountToken(channel.accountId);
|
||||
if (preferred && desiredTokenIds.has(preferred.id)) {
|
||||
db.update(schema.routeChannels)
|
||||
.set({ tokenId: preferred.id })
|
||||
.where(eq(schema.routeChannels.id, channel.id))
|
||||
.run();
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
if (!channel.manualOverride) {
|
||||
db.delete(schema.routeChannels).where(eq(schema.routeChannels.id, channel.id)).run();
|
||||
removedChannels++;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
return {
|
||||
models: modelTokens.size,
|
||||
createdRoutes,
|
||||
createdChannels,
|
||||
removedChannels,
|
||||
};
|
||||
}
|
||||
|
||||
export async function refreshModelsAndRebuildRoutes() {
|
||||
const refresh = await refreshModelsForAllActiveAccounts();
|
||||
const rebuild = rebuildTokenRoutesFromAvailability();
|
||||
return { refresh, rebuild };
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
createNotificationSignature,
|
||||
evaluateNotificationThrottle,
|
||||
pruneNotificationThrottleState,
|
||||
type NotificationThrottleState,
|
||||
} from './notificationThrottle.js';
|
||||
|
||||
describe('notificationThrottle', () => {
|
||||
it('suppresses duplicate notifications during cooldown and merges suppressed count after cooldown', () => {
|
||||
const state = new Map<string, NotificationThrottleState>();
|
||||
const signature = createNotificationSignature('代理全部失败', '模型=gpt-4o', 'error');
|
||||
|
||||
const first = evaluateNotificationThrottle(state, signature, 1_000, 300_000);
|
||||
expect(first.shouldSend).toBe(true);
|
||||
expect(first.mergedCount).toBe(0);
|
||||
|
||||
const second = evaluateNotificationThrottle(state, signature, 1_020, 300_000);
|
||||
expect(second.shouldSend).toBe(false);
|
||||
expect(second.mergedCount).toBe(0);
|
||||
|
||||
const third = evaluateNotificationThrottle(state, signature, 301_100, 300_000);
|
||||
expect(third.shouldSend).toBe(true);
|
||||
expect(third.mergedCount).toBe(1);
|
||||
});
|
||||
|
||||
it('prunes stale throttle state entries', () => {
|
||||
const state = new Map<string, NotificationThrottleState>();
|
||||
state.set('recent', { lastSentAtMs: 950_000, suppressedCount: 0 });
|
||||
state.set('stale', { lastSentAtMs: 1, suppressedCount: 3 });
|
||||
|
||||
pruneNotificationThrottleState(state, 1_000_000, 60_000);
|
||||
|
||||
expect(state.has('recent')).toBe(true);
|
||||
expect(state.has('stale')).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,53 @@
|
||||
export type NotificationThrottleState = {
|
||||
lastSentAtMs: number;
|
||||
suppressedCount: number;
|
||||
};
|
||||
|
||||
export function createNotificationSignature(title: string, message: string, level: string): string {
|
||||
return [
|
||||
(level || '').trim().toLowerCase(),
|
||||
(title || '').trim(),
|
||||
(message || '').trim(),
|
||||
].join('||');
|
||||
}
|
||||
|
||||
export function evaluateNotificationThrottle(
|
||||
state: Map<string, NotificationThrottleState>,
|
||||
signature: string,
|
||||
nowMs: number,
|
||||
cooldownMs: number,
|
||||
): { shouldSend: boolean; mergedCount: number } {
|
||||
if (cooldownMs <= 0) {
|
||||
return { shouldSend: true, mergedCount: 0 };
|
||||
}
|
||||
|
||||
const current = state.get(signature);
|
||||
if (!current) {
|
||||
state.set(signature, { lastSentAtMs: nowMs, suppressedCount: 0 });
|
||||
return { shouldSend: true, mergedCount: 0 };
|
||||
}
|
||||
|
||||
if (nowMs - current.lastSentAtMs < cooldownMs) {
|
||||
current.suppressedCount += 1;
|
||||
state.set(signature, current);
|
||||
return { shouldSend: false, mergedCount: 0 };
|
||||
}
|
||||
|
||||
const mergedCount = current.suppressedCount;
|
||||
state.set(signature, { lastSentAtMs: nowMs, suppressedCount: 0 });
|
||||
return { shouldSend: true, mergedCount };
|
||||
}
|
||||
|
||||
export function pruneNotificationThrottleState(
|
||||
state: Map<string, NotificationThrottleState>,
|
||||
nowMs: number,
|
||||
staleMs: number,
|
||||
): void {
|
||||
if (staleMs <= 0) return;
|
||||
|
||||
for (const [key, value] of state.entries()) {
|
||||
if (nowMs - value.lastSentAtMs > staleMs) {
|
||||
state.delete(key);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,91 @@
|
||||
import { beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
|
||||
const sendMailMock = vi.fn();
|
||||
const createTransportMock = vi.fn(() => ({
|
||||
sendMail: (...args: unknown[]) => sendMailMock(...args),
|
||||
}));
|
||||
const fetchMock = vi.fn();
|
||||
|
||||
vi.mock('nodemailer', () => ({
|
||||
default: {
|
||||
createTransport: (...args: unknown[]) => (createTransportMock as any)(...args),
|
||||
},
|
||||
createTransport: (...args: unknown[]) => (createTransportMock as any)(...args),
|
||||
}));
|
||||
|
||||
vi.mock('undici', () => ({
|
||||
fetch: (...args: unknown[]) => fetchMock(...args),
|
||||
}));
|
||||
|
||||
describe('notifyService', () => {
|
||||
beforeEach(async () => {
|
||||
vi.resetModules();
|
||||
sendMailMock.mockReset();
|
||||
createTransportMock.mockClear();
|
||||
fetchMock.mockReset();
|
||||
|
||||
const { config } = await import('../config.js');
|
||||
config.notifyCooldownSec = 300;
|
||||
config.webhookEnabled = false;
|
||||
config.webhookUrl = '';
|
||||
config.barkEnabled = false;
|
||||
config.barkUrl = '';
|
||||
config.serverChanEnabled = false;
|
||||
config.serverChanKey = '';
|
||||
config.smtpEnabled = true;
|
||||
config.smtpHost = 'smtp.example.com';
|
||||
config.smtpPort = 465;
|
||||
config.smtpSecure = true;
|
||||
config.smtpUser = 'demo-user';
|
||||
config.smtpPass = 'demo-pass';
|
||||
config.smtpFrom = 'sender@example.com';
|
||||
config.smtpTo = 'receiver@example.com';
|
||||
});
|
||||
|
||||
it('bypasses cooldown when bypassThrottle is enabled', async () => {
|
||||
sendMailMock.mockResolvedValue({ accepted: ['receiver@example.com'] });
|
||||
const { sendNotification } = await import('./notifyService.js');
|
||||
|
||||
await (sendNotification as any)('测试通知', 'same-message', 'info', { bypassThrottle: true });
|
||||
await (sendNotification as any)('测试通知', 'same-message', 'info', { bypassThrottle: true });
|
||||
|
||||
expect(sendMailMock).toHaveBeenCalledTimes(2);
|
||||
});
|
||||
|
||||
it('throws when strict delivery is required and no channels are enabled', async () => {
|
||||
const { config } = await import('../config.js');
|
||||
config.smtpEnabled = false;
|
||||
|
||||
const { sendNotification } = await import('./notifyService.js');
|
||||
await expect(
|
||||
(sendNotification as any)('测试通知', 'message', 'info', {
|
||||
requireChannel: true,
|
||||
throwOnFailure: true,
|
||||
}),
|
||||
).rejects.toThrow('未启用任何通知渠道');
|
||||
});
|
||||
|
||||
it('throws when strict delivery is required and all channel sends fail', async () => {
|
||||
sendMailMock.mockRejectedValue(new Error('smtp auth failed'));
|
||||
const { sendNotification } = await import('./notifyService.js');
|
||||
|
||||
await expect(
|
||||
(sendNotification as any)('测试通知', 'message', 'info', {
|
||||
bypassThrottle: true,
|
||||
throwOnFailure: true,
|
||||
}),
|
||||
).rejects.toThrow(/smtp auth failed|通知发送失败/);
|
||||
});
|
||||
|
||||
it('includes local time and utc time labels in smtp payload', async () => {
|
||||
sendMailMock.mockResolvedValue({ accepted: ['receiver@example.com'] });
|
||||
const { sendNotification } = await import('./notifyService.js');
|
||||
|
||||
await sendNotification('测试通知', 'message', 'info', { bypassThrottle: true });
|
||||
|
||||
expect(sendMailMock).toHaveBeenCalledTimes(1);
|
||||
const payload = sendMailMock.mock.calls[0]?.[0] as { text?: string };
|
||||
expect(payload?.text || '').toContain('Local Time:');
|
||||
expect(payload?.text || '').toContain('UTC Time:');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,228 @@
|
||||
import { fetch } from 'undici';
|
||||
import { config } from '../config.js';
|
||||
import nodemailer, { type Transporter } from 'nodemailer';
|
||||
import {
|
||||
createNotificationSignature,
|
||||
evaluateNotificationThrottle,
|
||||
pruneNotificationThrottleState,
|
||||
type NotificationThrottleState,
|
||||
} from './notificationThrottle.js';
|
||||
import { formatLocalDateTime, getResolvedTimeZone } from './localTimeService.js';
|
||||
|
||||
type NotificationChannel = 'webhook' | 'bark' | 'serverchan' | 'smtp';
|
||||
|
||||
export type SendNotificationOptions = {
|
||||
bypassThrottle?: boolean;
|
||||
requireChannel?: boolean;
|
||||
throwOnFailure?: boolean;
|
||||
};
|
||||
|
||||
export type NotificationDispatchResult = {
|
||||
throttled: boolean;
|
||||
attempted: number;
|
||||
succeeded: number;
|
||||
failed: number;
|
||||
failedChannels: NotificationChannel[];
|
||||
};
|
||||
|
||||
let cachedSmtpFingerprint = '';
|
||||
let cachedTransporter: Transporter | null = null;
|
||||
const notificationThrottleState = new Map<string, NotificationThrottleState>();
|
||||
|
||||
function getSmtpFingerprint() {
|
||||
return [
|
||||
config.smtpHost,
|
||||
config.smtpPort,
|
||||
config.smtpSecure ? '1' : '0',
|
||||
config.smtpUser,
|
||||
config.smtpPass,
|
||||
config.smtpFrom,
|
||||
config.smtpTo,
|
||||
].join('|');
|
||||
}
|
||||
|
||||
function getSmtpTransporter() {
|
||||
const fingerprint = getSmtpFingerprint();
|
||||
if (cachedTransporter && cachedSmtpFingerprint === fingerprint) {
|
||||
return cachedTransporter;
|
||||
}
|
||||
|
||||
cachedTransporter = nodemailer.createTransport({
|
||||
host: config.smtpHost,
|
||||
port: config.smtpPort,
|
||||
secure: config.smtpSecure,
|
||||
auth: config.smtpUser
|
||||
? {
|
||||
user: config.smtpUser,
|
||||
pass: config.smtpPass,
|
||||
}
|
||||
: undefined,
|
||||
});
|
||||
cachedSmtpFingerprint = fingerprint;
|
||||
return cachedTransporter;
|
||||
}
|
||||
|
||||
function buildTimeFootnote(now: Date): string {
|
||||
const timeZone = getResolvedTimeZone();
|
||||
return [
|
||||
`Local Time: ${formatLocalDateTime(now)} (${timeZone})`,
|
||||
`UTC Time: ${now.toISOString()}`,
|
||||
].join('\n');
|
||||
}
|
||||
|
||||
export async function sendNotification(
|
||||
title: string,
|
||||
message: string,
|
||||
level: 'info' | 'warning' | 'error' = 'info',
|
||||
options: SendNotificationOptions = {},
|
||||
): Promise<NotificationDispatchResult> {
|
||||
const now = new Date();
|
||||
const timeFootnote = buildTimeFootnote(now);
|
||||
const { bypassThrottle = false, requireChannel = false, throwOnFailure = false } = options;
|
||||
const cooldownMs = Math.max(0, Math.trunc(config.notifyCooldownSec)) * 1000;
|
||||
let resolvedMessage = message;
|
||||
if (!bypassThrottle && cooldownMs > 0) {
|
||||
const nowMs = Date.now();
|
||||
pruneNotificationThrottleState(notificationThrottleState, nowMs, Math.max(cooldownMs * 6, 600_000));
|
||||
const signature = createNotificationSignature(title, message, level);
|
||||
const decision = evaluateNotificationThrottle(notificationThrottleState, signature, nowMs, cooldownMs);
|
||||
if (!decision.shouldSend) {
|
||||
return {
|
||||
throttled: true,
|
||||
attempted: 0,
|
||||
succeeded: 0,
|
||||
failed: 0,
|
||||
failedChannels: [],
|
||||
};
|
||||
}
|
||||
if (decision.mergedCount > 0) {
|
||||
resolvedMessage = `${message}\n\n[通知合并] 冷静期内已合并 ${decision.mergedCount} 条重复告警`;
|
||||
}
|
||||
}
|
||||
|
||||
const tasks: Array<{ channel: NotificationChannel; run: () => Promise<unknown> }> = [];
|
||||
|
||||
if (config.webhookEnabled && config.webhookUrl) {
|
||||
tasks.push(
|
||||
{
|
||||
channel: 'webhook',
|
||||
run: async () => {
|
||||
const response = await fetch(config.webhookUrl, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/json' },
|
||||
body: JSON.stringify({
|
||||
title,
|
||||
message: resolvedMessage,
|
||||
level,
|
||||
timestamp: now.toISOString(),
|
||||
localTime: formatLocalDateTime(now),
|
||||
timeZone: getResolvedTimeZone(),
|
||||
}),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Webhook 响应状态 ${response.status}`);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (config.barkEnabled && config.barkUrl) {
|
||||
const barkBase = config.barkUrl.replace(/\/+$/, '');
|
||||
const url = `${barkBase}/${encodeURIComponent(title)}/${encodeURIComponent(resolvedMessage)}?group=AllApiHub&level=${encodeURIComponent(level)}`;
|
||||
tasks.push({
|
||||
channel: 'bark',
|
||||
run: async () => {
|
||||
const response = await fetch(url, { method: 'GET' });
|
||||
if (!response.ok) {
|
||||
throw new Error(`Bark 响应状态 ${response.status}`);
|
||||
}
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
if (config.serverChanEnabled && config.serverChanKey) {
|
||||
const form = new URLSearchParams({
|
||||
title,
|
||||
desp: `${resolvedMessage}\n\nLevel: ${level}\n${timeFootnote}`,
|
||||
});
|
||||
tasks.push(
|
||||
{
|
||||
channel: 'serverchan',
|
||||
run: async () => {
|
||||
const response = await fetch(`https://sctapi.ftqq.com/${config.serverChanKey}.send`, {
|
||||
method: 'POST',
|
||||
headers: { 'Content-Type': 'application/x-www-form-urlencoded' },
|
||||
body: form.toString(),
|
||||
});
|
||||
if (!response.ok) {
|
||||
throw new Error(`Server酱响应状态 ${response.status}`);
|
||||
}
|
||||
},
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (
|
||||
config.smtpEnabled &&
|
||||
config.smtpHost &&
|
||||
config.smtpPort > 0 &&
|
||||
config.smtpFrom &&
|
||||
config.smtpTo
|
||||
) {
|
||||
const transporter = getSmtpTransporter();
|
||||
tasks.push(
|
||||
{
|
||||
channel: 'smtp',
|
||||
run: () => transporter.sendMail({
|
||||
from: config.smtpFrom,
|
||||
to: config.smtpTo,
|
||||
subject: `[metapi][${level.toUpperCase()}] ${title}`,
|
||||
text: `${resolvedMessage}\n\nLevel: ${level}\n${timeFootnote}`,
|
||||
}),
|
||||
},
|
||||
);
|
||||
}
|
||||
|
||||
if (tasks.length === 0) {
|
||||
if (requireChannel || throwOnFailure) {
|
||||
throw new Error('未启用任何通知渠道,请先开启并保存至少一种通知方式');
|
||||
}
|
||||
return {
|
||||
throttled: false,
|
||||
attempted: 0,
|
||||
succeeded: 0,
|
||||
failed: 0,
|
||||
failedChannels: [],
|
||||
};
|
||||
}
|
||||
|
||||
const results = await Promise.all(tasks.map(async (task) => {
|
||||
try {
|
||||
await task.run();
|
||||
return { channel: task.channel, ok: true as const, error: '' };
|
||||
} catch (error: any) {
|
||||
return {
|
||||
channel: task.channel,
|
||||
ok: false as const,
|
||||
error: error?.message || String(error) || 'unknown error',
|
||||
};
|
||||
}
|
||||
}));
|
||||
|
||||
const failedResults = results.filter((item) => !item.ok);
|
||||
const succeeded = results.length - failedResults.length;
|
||||
const failedChannels = failedResults.map((item) => item.channel);
|
||||
|
||||
if (throwOnFailure && succeeded === 0 && failedResults.length > 0) {
|
||||
throw new Error(`通知发送失败:${failedResults[0].error}`);
|
||||
}
|
||||
|
||||
return {
|
||||
throttled: false,
|
||||
attempted: results.length,
|
||||
succeeded,
|
||||
failed: failedResults.length,
|
||||
failedChannels,
|
||||
};
|
||||
}
|
||||
@@ -0,0 +1 @@
|
||||
<html><script>var arg1='B8B78DDA1D5CD2598AAEE9DD6975913AA490BAE3';(function(a,c){var G=a0j,d=a();while(!![]){try{var e=-parseInt(G(0x117))/0x1*(parseInt(G(0x111))/0x2)+-parseInt(G(0xfb))/0x3*(parseInt(G(0x10e))/0x4)+-parseInt(G(0x101))/0x5*(-parseInt(G(0xfd))/0x6)+-parseInt(G(0x102))/0x7*(parseInt(G(0x122))/0x8)+parseInt(G(0x112))/0x9+parseInt(G(0x11d))/0xa*(parseInt(G(0x11c))/0xb)+parseInt(G(0x114))/0xc;if(e===c)break;else d['push'](d['shift']());}catch(f){d['push'](d['shift']());}}}(a0i,0x760bf),!(function(){var L=a0j,j=(function(){var B=!![];return function(C,D){var E=B?function(){var H=a0j;if(D){var F=D[H(0x10d)](C,arguments);return D=null,F;}}:function(){};return B=![],E;};}()),k=j(this,function(){var I=a0j;return k[I(0xff)]()[I(0x123)](I(0x10f))[I(0xff)]()[I(0x107)](k)[I(0x123)](I(0x10f));});k();var l=(function(){var B=!![];return function(C,D){var E=B?function(){var J=a0j;if(D){var F=D[J(0x10d)](C,arguments);return D=null,F;}}:function(){};return B=![],E;};}());(function(){l(this,function(){var K=a0j,B=new RegExp(K(0x118)),C=new RegExp(K(0x106),'i'),D=b(K(0x100));!B[K(0x104)](D+K(0x105))||!C[K(0x104)](D+K(0x11b))?D('0'):b();})();}());for(var m=[0xf,0x23,0x1d,0x18,0x21,0x10,0x1,0x26,0xa,0x9,0x13,0x1f,0x28,0x1b,0x16,0x17,0x19,0xd,0x6,0xb,0x27,0x12,0x14,0x8,0xe,0x15,0x20,0x1a,0x2,0x1e,0x7,0x4,0x11,0x5,0x3,0x1c,0x22,0x25,0xc,0x24],p=L(0x115),q=[],u='',v='',w=L(0x116),x=0x0;x<arg1[w];x++)for(var y=arg1[x],z=0x0;z<m[w];z++)m[z]==x+0x1&&(q[z]=y);for(u=q[L(0xfc)](''),x=0x0;x<u[w]&&x<p[w];x+=0x2){var A=(parseInt(u[L(0x11a)](x,x+0x2),0x10)^parseInt(p[L(0x11a)](x,x+0x2),0x10))[L(0xff)](0x10);0x1==A[w]&&(A='0'+A),v+=A;}document[L(0x121)]='acw_sc__v2='+v+L(0x120)+new Date(Date[L(0x119)]()+0x36ee80)[L(0x10c)]()+L(0x109),document[L(0xfe)][L(0x103)]();}()));function a0i(){var N=['mJKZmgTStNvVyq','C3rYAw5N','y2fSBa','o2v4CgLYzxm9','y29VA2LL','mteZmZy3mNLbu2PszW','C2vHCMnO','D2HPBguGkhrYDwuPihT9','mJq1ndi0rKHuthnj','AM9PBG','nNH1rKHOuq','Bg9JyxrPB24','Dg9tDhjPBMC','Aw5PDa','mJi2odi1nwnMre1IyG','n0HxChPJva','CMvSB2fK','DgvZDa','y2HHAw4','xcTCkYaQkd86w2eTEKeTwL8KxvSWltLHlxPblvPFjf0Qkq','y29UC3rYDwn0B3i','y291BNrLCG','o21HEc1Hz2u9mZyWmdTWyxrOps87','zgvIDq','ywn0Aw9U','Dg9htvrtDhjPBMC','yxbWBhK','mJbiru1MChi','kcGOlISPkYKRksSK','z2DLCG','nKHJq01Aqq','nJe5nZu5ogH1twPUDa','C3rHDgvpyMPLy3q','mZu5mdu5mNbcB2Pxyq','mZaWmde3nJaWmdG1nJaWnJa2mtuWmtuZmZaWmZy5mdaYnZGWmdm3nq','BgvUz3rO','mtqWnti2shvUBNDv','zNvUy3rPB24GkLWOicPCkq','BM93','C2XPy2u','Aw5WDxq','ntm5BwrLuMXi'];a0i=function(){return N;};return a0i();}function a0j(a,b){var c=a0i();return a0j=function(d,e){d=d-0xfb;var f=c[d];if(a0j['tGHEKR']===undefined){var g=function(l){var m='abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789+/=';var n='',o='',p=n+g;for(var q=0x0,r,s,t=0x0;s=l['charAt'](t++);~s&&(r=q%0x4?r*0x40+s:s,q++%0x4)?n+=p['charCodeAt'](t+0xa)-0xa!==0x0?String['fromCharCode'](0xff&r>>(-0x2*q&0x6)):q:0x0){s=m['indexOf'](s);}for(var u=0x0,v=n['length'];u<v;u++){o+='%'+('00'+n['charCodeAt'](u)['toString'](0x10))['slice'](-0x2);}return decodeURIComponent(o);};a0j['CrMtTV']=g,a=arguments,a0j['tGHEKR']=!![];}var h=c[0x0],i=d+h,j=a[i];if(!j){var k=function(l){this['jyamLv']=l,this['WxwaRR']=[0x1,0x0,0x0],this['GuwnVk']=function(){return'newState';},this['CxWuMi']='\x5cw+\x20*\x5c(\x5c)\x20*{\x5cw+\x20*',this['FxAgTK']='[\x27|\x22].+[\x27|\x22];?\x20*}';};k['prototype']['dQvDam']=function(){var l=new RegExp(this['CxWuMi']+this['FxAgTK']),m=l['test'](this['GuwnVk']['toString']())?--this['WxwaRR'][0x1]:--this['WxwaRR'][0x0];return this['fQYikn'](m);},k['prototype']['fQYikn']=function(l){if(!Boolean(~l))return l;return this['Hbwxmd'](this['jyamLv']);},k['prototype']['Hbwxmd']=function(l){for(var m=0x0,n=this['WxwaRR']['length'];m<n;m++){this['WxwaRR']['push'](Math['round'](Math['random']())),n=this['WxwaRR']['length'];}return l(this['WxwaRR'][0x0]);},new k(a0j)['dQvDam'](),f=a0j['CrMtTV'](f),a[i]=f;}else f=j;return f;},a0j(a,b);}function b(a){function c(d){var M=a0j;if(typeof d===M(0x11e))return function(e){}[M(0x107)](M(0x124))[M(0x10d)](M(0x108));else(''+d/d)[M(0x116)]!==0x1||d%0x14===0x0?function(){return!![];}[M(0x107)](M(0x10a)+M(0x110))[M(0x11f)](M(0x10b)):function(){return![];}[M(0x107)](M(0x10a)+M(0x110))[M(0x10d)](M(0x113));c(++d);}try{if(a)return c;else c(0x0);}catch(d){}}</script></html>
|
||||
@@ -0,0 +1,10 @@
|
||||
import { NewApiAdapter } from './newApi.js';
|
||||
|
||||
export class AnyRouterAdapter extends NewApiAdapter {
|
||||
readonly platformName = 'anyrouter';
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
const normalized = (url || '').toLowerCase();
|
||||
return normalized.includes('anyrouter');
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,192 @@
|
||||
import type { RequestInit as UndiciRequestInit } from 'undici';
|
||||
|
||||
export interface CheckinResult {
|
||||
success: boolean;
|
||||
message: string;
|
||||
reward?: string;
|
||||
}
|
||||
|
||||
export interface BalanceInfo {
|
||||
balance: number;
|
||||
used: number;
|
||||
quota: number;
|
||||
todayIncome?: number;
|
||||
todayQuotaConsumption?: number;
|
||||
}
|
||||
|
||||
interface LoginResult {
|
||||
success: boolean;
|
||||
accessToken?: string;
|
||||
username?: string;
|
||||
message?: string;
|
||||
}
|
||||
|
||||
export interface UserInfo {
|
||||
username: string;
|
||||
displayName?: string;
|
||||
email?: string;
|
||||
role?: number;
|
||||
}
|
||||
|
||||
export interface TokenVerifyResult {
|
||||
tokenType: 'session' | 'apikey' | 'unknown';
|
||||
userInfo?: UserInfo | null;
|
||||
balance?: BalanceInfo | null;
|
||||
apiToken?: string | null;
|
||||
models?: string[];
|
||||
}
|
||||
|
||||
export interface ApiTokenInfo {
|
||||
name: string;
|
||||
key: string;
|
||||
enabled?: boolean;
|
||||
}
|
||||
|
||||
export interface CreateApiTokenOptions {
|
||||
name?: string;
|
||||
group?: string;
|
||||
unlimitedQuota?: boolean;
|
||||
remainQuota?: number;
|
||||
expiredTime?: number;
|
||||
allowIps?: string;
|
||||
modelLimitsEnabled?: boolean;
|
||||
modelLimits?: string;
|
||||
}
|
||||
|
||||
export interface PlatformAdapter {
|
||||
readonly platformName: string;
|
||||
detect(url: string): Promise<boolean>;
|
||||
login(baseUrl: string, username: string, password: string): Promise<LoginResult>;
|
||||
getUserInfo(baseUrl: string, accessToken: string, platformUserId?: number): Promise<UserInfo | null>;
|
||||
verifyToken(baseUrl: string, token: string, platformUserId?: number): Promise<TokenVerifyResult>;
|
||||
checkin(baseUrl: string, accessToken: string, platformUserId?: number): Promise<CheckinResult>;
|
||||
getBalance(baseUrl: string, accessToken: string, platformUserId?: number): Promise<BalanceInfo>;
|
||||
getModels(baseUrl: string, token: string, platformUserId?: number): Promise<string[]>;
|
||||
getApiToken(baseUrl: string, accessToken: string, platformUserId?: number): Promise<string | null>;
|
||||
getApiTokens(baseUrl: string, accessToken: string, platformUserId?: number): Promise<ApiTokenInfo[]>;
|
||||
getUserGroups(baseUrl: string, accessToken: string, platformUserId?: number): Promise<string[]>;
|
||||
createApiToken(baseUrl: string, accessToken: string, platformUserId?: number, options?: CreateApiTokenOptions): Promise<boolean>;
|
||||
deleteApiToken(baseUrl: string, accessToken: string, tokenKey: string, platformUserId?: number): Promise<boolean>;
|
||||
}
|
||||
|
||||
export abstract class BasePlatformAdapter implements PlatformAdapter {
|
||||
abstract readonly platformName: string;
|
||||
|
||||
abstract detect(url: string): Promise<boolean>;
|
||||
abstract checkin(baseUrl: string, accessToken: string): Promise<CheckinResult>;
|
||||
abstract getBalance(baseUrl: string, accessToken: string): Promise<BalanceInfo>;
|
||||
abstract getModels(baseUrl: string, token: string, platformUserId?: number): Promise<string[]>;
|
||||
|
||||
async verifyToken(baseUrl: string, token: string, _platformUserId?: number): Promise<TokenVerifyResult> {
|
||||
// 1. Try as session/access token first (for management APIs)
|
||||
const userInfo = await this.getUserInfo(baseUrl, token);
|
||||
if (userInfo) {
|
||||
let balance: BalanceInfo | null = null;
|
||||
try { balance = await this.getBalance(baseUrl, token); } catch {}
|
||||
let apiToken: string | null = null;
|
||||
try { apiToken = await this.getApiToken(baseUrl, token); } catch {}
|
||||
return { tokenType: 'session', userInfo, balance, apiToken };
|
||||
}
|
||||
|
||||
// 2. Try as API key (for /v1/models)
|
||||
try {
|
||||
const models = await this.getModels(baseUrl, token);
|
||||
if (models && models.length > 0) {
|
||||
return { tokenType: 'apikey', models };
|
||||
}
|
||||
} catch {}
|
||||
|
||||
return { tokenType: 'unknown' };
|
||||
}
|
||||
|
||||
async getUserInfo(baseUrl: string, accessToken: string): Promise<UserInfo | null> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/self`, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
if (res?.success && res?.data) {
|
||||
return {
|
||||
username: res.data.username || res.data.display_name || '',
|
||||
displayName: res.data.display_name,
|
||||
email: res.data.email,
|
||||
role: res.data.role,
|
||||
};
|
||||
}
|
||||
return null;
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
async login(baseUrl: string, username: string, password: string): Promise<LoginResult> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/login`, {
|
||||
method: 'POST',
|
||||
body: JSON.stringify({ username, password }),
|
||||
});
|
||||
if (res?.success && res?.data) {
|
||||
return {
|
||||
success: true,
|
||||
accessToken: typeof res.data === 'string' ? res.data : res.data.token || res.data.access_token,
|
||||
username,
|
||||
};
|
||||
}
|
||||
return { success: false, message: res?.message || '登录失败' };
|
||||
} catch (err: any) {
|
||||
return { success: false, message: err.message || '登录请求失败' };
|
||||
}
|
||||
}
|
||||
|
||||
async getApiToken(_baseUrl: string, _accessToken: string, _platformUserId?: number): Promise<string | null> {
|
||||
return null;
|
||||
}
|
||||
|
||||
async getApiTokens(baseUrl: string, accessToken: string, platformUserId?: number): Promise<ApiTokenInfo[]> {
|
||||
const token = await this.getApiToken(baseUrl, accessToken, platformUserId);
|
||||
if (!token) return [];
|
||||
return [{ name: 'default', key: token, enabled: true }];
|
||||
}
|
||||
|
||||
async createApiToken(
|
||||
_baseUrl: string,
|
||||
_accessToken: string,
|
||||
_platformUserId?: number,
|
||||
_options?: CreateApiTokenOptions,
|
||||
): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
|
||||
async getUserGroups(
|
||||
_baseUrl: string,
|
||||
_accessToken: string,
|
||||
_platformUserId?: number,
|
||||
): Promise<string[]> {
|
||||
return ['default'];
|
||||
}
|
||||
|
||||
async deleteApiToken(
|
||||
_baseUrl: string,
|
||||
_accessToken: string,
|
||||
_tokenKey: string,
|
||||
_platformUserId?: number,
|
||||
): Promise<boolean> {
|
||||
return false;
|
||||
}
|
||||
|
||||
protected async fetchJson<T>(url: string, options?: UndiciRequestInit): Promise<T> {
|
||||
const { fetch } = await import('undici');
|
||||
const requestOptions: UndiciRequestInit = {
|
||||
...options,
|
||||
body: options?.body ?? undefined,
|
||||
headers: {
|
||||
'Content-Type': 'application/json',
|
||||
...options?.headers,
|
||||
},
|
||||
};
|
||||
const res = await fetch(url, requestOptions);
|
||||
if (!res.ok) {
|
||||
throw new Error(`HTTP ${res.status}: ${await res.text()}`);
|
||||
}
|
||||
return res.json() as Promise<T>;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,76 @@
|
||||
import { afterEach, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
||||
import { AddressInfo } from 'node:net';
|
||||
import { DoneHubAdapter } from './doneHub.js';
|
||||
|
||||
describe('DoneHubAdapter', () => {
|
||||
let server: ReturnType<typeof createServer>;
|
||||
let baseUrl: string;
|
||||
|
||||
beforeEach(async () => {
|
||||
server = createServer((req: IncomingMessage, res: ServerResponse) => {
|
||||
if (req.url === '/v1/models') {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: { message: 'unauthorized' } }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url === '/api/available_model') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
'gpt-4o': { price: { input: 0.5, output: 1.5 } },
|
||||
'deepseek-chat': { price: { input: 0.1, output: 0.2 } },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url?.startsWith('/api/token/')) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: {
|
||||
data: [
|
||||
{ key: 'donehub-token-key', name: 'sys_playground', status: 1 },
|
||||
],
|
||||
},
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'not found' }));
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => resolve());
|
||||
});
|
||||
const addr = server.address() as AddressInfo;
|
||||
baseUrl = `http://127.0.0.1:${addr.port}`;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((err?: Error) => (err ? reject(err) : resolve()));
|
||||
});
|
||||
});
|
||||
|
||||
it('marks checkin as unsupported', async () => {
|
||||
const adapter = new DoneHubAdapter();
|
||||
const result = await adapter.checkin(baseUrl, 'token');
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toBe('checkin endpoint not found');
|
||||
});
|
||||
|
||||
it('falls back to /api/available_model when /v1/models is unavailable', async () => {
|
||||
const adapter = new DoneHubAdapter();
|
||||
const models = await adapter.getModels(baseUrl, 'token');
|
||||
expect(models).toEqual(['gpt-4o', 'deepseek-chat']);
|
||||
});
|
||||
|
||||
it('parses token list from nested data.data shape', async () => {
|
||||
const adapter = new DoneHubAdapter();
|
||||
const tokens = await adapter.getApiTokens(baseUrl, 'token');
|
||||
expect(tokens).toEqual([
|
||||
{ key: 'donehub-token-key', name: 'sys_playground', enabled: true },
|
||||
]);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,39 @@
|
||||
import { OneApiAdapter } from './oneApi.js';
|
||||
import type { CheckinResult } from './base.js';
|
||||
|
||||
export class DoneHubAdapter extends OneApiAdapter {
|
||||
readonly platformName = 'done-hub';
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
const normalized = url.toLowerCase();
|
||||
return normalized.includes('donehub') || normalized.includes('done-hub');
|
||||
}
|
||||
|
||||
// DoneHub deployments generally do not expose /api/user/checkin.
|
||||
// Mark as unsupported so higher-level logic records it as skipped instead of failed.
|
||||
override async checkin(_baseUrl: string, _accessToken: string): Promise<CheckinResult> {
|
||||
return { success: false, message: 'checkin endpoint not found' };
|
||||
}
|
||||
|
||||
override async getModels(baseUrl: string, apiToken: string, _platformUserId?: number): Promise<string[]> {
|
||||
let openAiModels: string[] = [];
|
||||
try {
|
||||
openAiModels = await super.getModels(baseUrl, apiToken, _platformUserId);
|
||||
} catch {}
|
||||
if (openAiModels.length > 0) return openAiModels;
|
||||
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/available_model`, {
|
||||
headers: { Authorization: `Bearer ${apiToken}` },
|
||||
});
|
||||
|
||||
const payload = (res?.data && typeof res.data === 'object') ? res.data : res;
|
||||
if (payload && typeof payload === 'object' && !Array.isArray(payload)) {
|
||||
const models = Object.keys(payload).filter(Boolean);
|
||||
if (models.length > 0) return models;
|
||||
}
|
||||
} catch {}
|
||||
|
||||
return [];
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,28 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { detectPlatform, getAdapter } from './index.js';
|
||||
|
||||
describe('getAdapter platform aliases', () => {
|
||||
it('returns dedicated anyrouter adapter for anyrouter alias', () => {
|
||||
const adapter = getAdapter('anyrouter');
|
||||
expect(adapter?.platformName).toBe('anyrouter');
|
||||
});
|
||||
|
||||
it('handles case-insensitive platform strings', () => {
|
||||
const adapter = getAdapter('Veloera');
|
||||
expect(adapter?.platformName).toBe('veloera');
|
||||
});
|
||||
|
||||
it('returns undefined for unknown platforms', () => {
|
||||
expect(getAdapter('unknown-platform')).toBeUndefined();
|
||||
});
|
||||
|
||||
it('detects anyrouter URL before generic new-api adapter', async () => {
|
||||
const adapter = await detectPlatform('https://anyrouter.top');
|
||||
expect(adapter?.platformName).toBe('anyrouter');
|
||||
});
|
||||
|
||||
it('detects done-hub URL before generic adapters', async () => {
|
||||
const adapter = await detectPlatform('https://demo.donehub.example');
|
||||
expect(adapter?.platformName).toBe('done-hub');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,58 @@
|
||||
import type { PlatformAdapter } from './base.js';
|
||||
import { AnyRouterAdapter } from './anyrouter.js';
|
||||
import { NewApiAdapter } from './newApi.js';
|
||||
import { OneApiAdapter } from './oneApi.js';
|
||||
import { VeloeraAdapter } from './veloera.js';
|
||||
import { OneHubAdapter } from './oneHub.js';
|
||||
import { DoneHubAdapter } from './doneHub.js';
|
||||
import { Sub2ApiAdapter } from './sub2api.js';
|
||||
|
||||
const adapters: PlatformAdapter[] = [
|
||||
// Specific forks before generic adapters for better auto-detection.
|
||||
new AnyRouterAdapter(),
|
||||
new DoneHubAdapter(),
|
||||
new OneHubAdapter(),
|
||||
new NewApiAdapter(),
|
||||
new Sub2ApiAdapter(),
|
||||
new OneApiAdapter(),
|
||||
new VeloeraAdapter(),
|
||||
];
|
||||
|
||||
const platformAliases: Record<string, string> = {
|
||||
// NewAPI family aliases
|
||||
anyrouter: 'anyrouter',
|
||||
'wong-gongyi': 'new-api',
|
||||
'vo-api': 'new-api',
|
||||
'super-api': 'new-api',
|
||||
'rix-api': 'new-api',
|
||||
'neo-api': 'new-api',
|
||||
newapi: 'new-api',
|
||||
'new api': 'new-api',
|
||||
// OneAPI family aliases
|
||||
oneapi: 'one-api',
|
||||
'one api': 'one-api',
|
||||
// Keep canonical forms explicit for clarity
|
||||
'new-api': 'new-api',
|
||||
'one-api': 'one-api',
|
||||
veloera: 'veloera',
|
||||
'one-hub': 'one-hub',
|
||||
'done-hub': 'done-hub',
|
||||
sub2api: 'sub2api',
|
||||
};
|
||||
|
||||
function normalizePlatform(platform: string): string {
|
||||
const raw = (platform || '').trim().toLowerCase();
|
||||
return platformAliases[raw] ?? raw;
|
||||
}
|
||||
|
||||
export function getAdapter(platform: string): PlatformAdapter | undefined {
|
||||
const normalized = normalizePlatform(platform);
|
||||
return adapters.find((a) => a.platformName === normalized);
|
||||
}
|
||||
|
||||
export async function detectPlatform(url: string): Promise<PlatformAdapter | undefined> {
|
||||
for (const adapter of adapters) {
|
||||
if (await adapter.detect(url)) return adapter;
|
||||
}
|
||||
return undefined;
|
||||
}
|
||||
@@ -0,0 +1,414 @@
|
||||
import { describe, it, expect, beforeEach, afterEach } from 'vitest';
|
||||
import { createServer, type IncomingMessage, type ServerResponse } from 'node:http';
|
||||
import { AddressInfo } from 'node:net';
|
||||
import { readFileSync } from 'node:fs';
|
||||
import { NewApiAdapter } from './newApi.js';
|
||||
|
||||
interface RequestSnapshot {
|
||||
method: string;
|
||||
url: string;
|
||||
headers: IncomingMessage['headers'];
|
||||
}
|
||||
|
||||
const COOKIE_SESSION_TOKEN = 'cookie-session-token';
|
||||
const COOKIE_REQUIRES_USER_TOKEN = 'cookie-requires-user';
|
||||
const CHECKIN_ALREADY_TOKEN = 'checkin-already-token';
|
||||
const CHECKIN_INVALID_URL_TOKEN = 'checkin-invalid-url-token';
|
||||
const CHECKIN_CLOUDFLARE_530_TOKEN = 'checkin-cloudflare-530-token';
|
||||
const BALANCE_FAIL_TOKEN = 'balance-fail-token';
|
||||
const COOKIE_SHIELDED_TOKEN = Buffer.from(
|
||||
`1771864970|${Buffer.from('username=linuxdo_131936').toString('base64')}|sig`,
|
||||
).toString('base64');
|
||||
const ANYROUTER_CHALLENGE_HTML = readFileSync(
|
||||
new URL('./__fixtures__/anyrouter-challenge.html', import.meta.url),
|
||||
'utf8',
|
||||
);
|
||||
const ANYROUTER_CHALLENGE_ACW = '699dbedad126579b6bc0ebb91eaae8d7af3548b5';
|
||||
const CLOUDFLARE_530_HTML = `
|
||||
<!doctype html>
|
||||
<html lang="en-US">
|
||||
<head>
|
||||
<title>Cloudflare Tunnel error | newapi.tanmw.top | Cloudflare</title>
|
||||
</head>
|
||||
<body>
|
||||
<h1><span>Error</span><span>1033</span></h1>
|
||||
<h2>Cloudflare Tunnel error</h2>
|
||||
</body>
|
||||
</html>
|
||||
`;
|
||||
|
||||
describe('NewApiAdapter', () => {
|
||||
let server: ReturnType<typeof createServer>;
|
||||
let baseUrl: string;
|
||||
let requests: RequestSnapshot[] = [];
|
||||
|
||||
beforeEach(async () => {
|
||||
requests = [];
|
||||
server = createServer((req: IncomingMessage, res: ServerResponse) => {
|
||||
requests.push({
|
||||
method: req.method || 'GET',
|
||||
url: req.url || '/',
|
||||
headers: req.headers,
|
||||
});
|
||||
|
||||
if (req.url === '/v1/models') {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: { message: 'invalid token' } }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url === '/api/user/models') {
|
||||
if (req.headers['new-api-user'] !== '11494') {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ data: ['gpt-4o', 'gpt-4.1'] }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url?.startsWith('/api/token/')) {
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_SHIELDED_TOKEN}`) {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_SHIELDED_TOKEN}`)) {
|
||||
if (!req.headers.cookie.includes(`acw_sc__v2=${ANYROUTER_CHALLENGE_ACW}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.end(ANYROUTER_CHALLENGE_HTML);
|
||||
return;
|
||||
}
|
||||
if (req.headers['new-api-user'] !== '131936') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: {
|
||||
items: [{ key: 'shielded-cookie-key' }],
|
||||
},
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_SESSION_TOKEN}`) {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_REQUIRES_USER_TOKEN}`) {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_SESSION_TOKEN}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: {
|
||||
items: [{ key: 'cookie-api-key' }],
|
||||
},
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_REQUIRES_USER_TOKEN}`)) {
|
||||
if (req.headers['new-api-user'] !== '8899') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: {
|
||||
items: [{ key: 'cookie-user-key' }],
|
||||
},
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
data: {
|
||||
items: [{ key: 'api-key-from-token-list' }],
|
||||
},
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url === '/api/user/self') {
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${BALANCE_FAIL_TOKEN}`) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: '閺冪姵娼堟潻娑滎攽濮濄倖鎼锋担婊愮礉閺堫亞娅ヨぐ鏇氱瑬閺堫亝褰佹笟?access token' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (
|
||||
typeof req.headers.cookie === 'string' &&
|
||||
(
|
||||
req.headers.cookie.includes(`session=${BALANCE_FAIL_TOKEN}`) ||
|
||||
req.headers.cookie.includes(`token=${BALANCE_FAIL_TOKEN}`)
|
||||
)
|
||||
) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: '閺冪姵娼堟潻娑滎攽濮濄倖鎼锋担婊愮礉閺堫亞娅ヨぐ鏇氱瑬閺堫亝褰佹笟?access token' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_SHIELDED_TOKEN}`)) {
|
||||
if (!req.headers.cookie.includes(`acw_sc__v2=${ANYROUTER_CHALLENGE_ACW}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.end(ANYROUTER_CHALLENGE_HTML);
|
||||
return;
|
||||
}
|
||||
if (req.headers['new-api-user'] !== '131936') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: { id: 131936, username: 'linuxdo_131936', quota: 3000000, used_quota: 1200000 },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === 'Bearer session-token') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: { id: 11494, username: 'demo-user', quota: 1000000, used_quota: 1000 },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_SESSION_TOKEN}`) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'invalid token' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_REQUIRES_USER_TOKEN}`) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'invalid token' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'invalid token' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_SESSION_TOKEN}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: { id: 7788, username: 'cookie-user', quota: 2000000, used_quota: 500000 },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_REQUIRES_USER_TOKEN}`)) {
|
||||
if (req.headers['new-api-user'] !== '8899') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({
|
||||
success: true,
|
||||
data: { id: 8899, username: 'cookie-user-id-required', quota: 1500000, used_quota: 100000 },
|
||||
}));
|
||||
return;
|
||||
}
|
||||
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
|
||||
if (req.url === '/api/user/checkin') {
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${CHECKIN_CLOUDFLARE_530_TOKEN}`) {
|
||||
res.writeHead(530, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.end(CLOUDFLARE_530_HTML);
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${CHECKIN_INVALID_URL_TOKEN}`) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: { message: 'Invalid URL (POST /api/user/checkin)' } }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${CHECKIN_INVALID_URL_TOKEN}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: { message: 'Invalid URL (POST /api/user/checkin)' } }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${CHECKIN_ALREADY_TOKEN}`) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: '浠婂ぉ宸茬粡绛惧埌杩囧暒' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${CHECKIN_ALREADY_TOKEN}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: '鏃犳潈杩涜姝ゆ搷浣滐紝鏈櫥褰曚笖鏈彁渚?access token' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.authorization === 'string' && req.headers.authorization === `Bearer ${COOKIE_SHIELDED_TOKEN}`) {
|
||||
res.writeHead(401, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'unauthorized' }));
|
||||
return;
|
||||
}
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${COOKIE_SHIELDED_TOKEN}`)) {
|
||||
if (!req.headers.cookie.includes(`acw_sc__v2=${ANYROUTER_CHALLENGE_ACW}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'text/html; charset=utf-8' });
|
||||
res.end(ANYROUTER_CHALLENGE_HTML);
|
||||
return;
|
||||
}
|
||||
if (req.headers['new-api-user'] !== '131936') {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: 'missing New-Api-User' }));
|
||||
return;
|
||||
}
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: true, message: 'checked-in-ok' }));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
if (req.url === '/api/user/sign_in') {
|
||||
if (typeof req.headers.cookie === 'string' && req.headers.cookie.includes(`session=${CHECKIN_ALREADY_TOKEN}`)) {
|
||||
res.writeHead(200, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ success: false, message: '鏃犳潈杩涜姝ゆ搷浣滐紝鏈櫥褰曚笖鏈彁渚?access token' }));
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
res.writeHead(404, { 'Content-Type': 'application/json' });
|
||||
res.end(JSON.stringify({ error: 'not found' }));
|
||||
});
|
||||
|
||||
await new Promise<void>((resolve) => {
|
||||
server.listen(0, '127.0.0.1', () => resolve());
|
||||
});
|
||||
const addr = server.address() as AddressInfo;
|
||||
baseUrl = `http://127.0.0.1:${addr.port}`;
|
||||
});
|
||||
|
||||
afterEach(async () => {
|
||||
await new Promise<void>((resolve, reject) => {
|
||||
server.close((err?: Error) => (err ? reject(err) : resolve()));
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to session model endpoint when /v1/models rejects token', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const models = await adapter.getModels(baseUrl, 'session-token', 11494);
|
||||
|
||||
expect(models).toEqual(['gpt-4o', 'gpt-4.1']);
|
||||
expect(requests.some((r) => r.url === '/v1/models')).toBe(true);
|
||||
expect(
|
||||
requests.some((r) => r.url === '/api/user/models' && r.headers['new-api-user'] === '11494'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('parses token list response with data.items[] shape', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const token = await adapter.getApiToken(baseUrl, 'session-token', 11494);
|
||||
|
||||
expect(token).toBe('api-key-from-token-list');
|
||||
});
|
||||
|
||||
it('detects cookie session values as session tokens for anyrouter-like deployments', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.verifyToken(baseUrl, COOKIE_SESSION_TOKEN);
|
||||
|
||||
expect(result.tokenType).toBe('session');
|
||||
expect(result.userInfo?.username).toBe('cookie-user');
|
||||
expect(result.apiToken).toBe('cookie-api-key');
|
||||
expect(
|
||||
requests.some((r) => r.url === '/api/user/self' && typeof r.headers.cookie === 'string' && r.headers.cookie.includes(`session=${COOKIE_SESSION_TOKEN}`)),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('auto-probes New-Api-User for cookie sessions when header is required', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.verifyToken(baseUrl, COOKIE_REQUIRES_USER_TOKEN);
|
||||
|
||||
expect(result.tokenType).toBe('session');
|
||||
expect(result.userInfo?.username).toBe('cookie-user-id-required');
|
||||
expect(result.apiToken).toBe('cookie-user-key');
|
||||
expect(
|
||||
requests.some((r) => r.url === '/api/user/self' && r.headers['new-api-user'] === '8899'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('solves anyrouter acw challenge and probes user id from session payload', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.verifyToken(baseUrl, COOKIE_SHIELDED_TOKEN);
|
||||
|
||||
expect(result.tokenType).toBe('session');
|
||||
expect(result.userInfo?.username).toBe('linuxdo_131936');
|
||||
expect(typeof result.apiToken === 'string' && result.apiToken.length > 0).toBe(true);
|
||||
expect(
|
||||
requests.some(
|
||||
(r) =>
|
||||
r.url === '/api/user/self' &&
|
||||
typeof r.headers.cookie === 'string' &&
|
||||
r.headers.cookie.includes(`acw_sc__v2=${ANYROUTER_CHALLENGE_ACW}`),
|
||||
),
|
||||
).toBe(true);
|
||||
expect(
|
||||
requests.some((r) => r.url === '/api/user/self' && r.headers['new-api-user'] === '131936'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('uses shielded cookie flow for balance and checkin', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const balance = await adapter.getBalance(baseUrl, COOKIE_SHIELDED_TOKEN);
|
||||
const checkin = await adapter.checkin(baseUrl, COOKIE_SHIELDED_TOKEN);
|
||||
|
||||
expect(balance).toEqual({
|
||||
quota: 8.4,
|
||||
used: 2.4,
|
||||
balance: 6,
|
||||
});
|
||||
expect(checkin.success).toBe(true);
|
||||
expect(
|
||||
requests.some((r) => r.url === '/api/user/checkin' && r.headers['new-api-user'] === '131936'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('preserves upstream balance failure message for UI feedback', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
|
||||
await expect(adapter.getBalance(baseUrl, BALANCE_FAIL_TOKEN)).rejects.toThrow('access token');
|
||||
});
|
||||
|
||||
it('preserves nested checkin error message instead of generic fallback', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.checkin(baseUrl, CHECKIN_INVALID_URL_TOKEN, 11494);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toContain('Invalid URL');
|
||||
});
|
||||
|
||||
it('summarizes cloudflare tunnel HTML failures to concise checkin error', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.checkin(baseUrl, CHECKIN_CLOUDFLARE_530_TOKEN, 11494);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toBe('HTTP 530: Cloudflare Tunnel error (Error 1033)');
|
||||
});
|
||||
|
||||
it('preserves already-checked-in message instead of overriding with cookie fallback error', async () => {
|
||||
const adapter = new NewApiAdapter();
|
||||
const result = await adapter.checkin(baseUrl, CHECKIN_ALREADY_TOKEN, 11494);
|
||||
|
||||
expect(result.success).toBe(false);
|
||||
expect(result.message).toBe('浠婂ぉ宸茬粡绛惧埌杩囧暒');
|
||||
});
|
||||
});
|
||||
File diff suppressed because it is too large
Load Diff
@@ -0,0 +1,216 @@
|
||||
import { ApiTokenInfo, BasePlatformAdapter, CheckinResult, BalanceInfo, CreateApiTokenOptions } from './base.js';
|
||||
|
||||
type CreateApiTokenPayload = {
|
||||
name: string;
|
||||
unlimited_quota: boolean;
|
||||
expired_time: number;
|
||||
remain_quota: number;
|
||||
allow_ips: string;
|
||||
model_limits_enabled: boolean;
|
||||
model_limits: string;
|
||||
group: string;
|
||||
};
|
||||
|
||||
export class OneApiAdapter extends BasePlatformAdapter {
|
||||
readonly platformName: string = 'one-api';
|
||||
|
||||
private normalizeTokenKeyForCompare(value?: string | null): string {
|
||||
const trimmed = (value || '').trim();
|
||||
return trimmed.startsWith('Bearer ') ? trimmed.slice(7).trim() : trimmed;
|
||||
}
|
||||
|
||||
private buildDefaultTokenPayload(options?: CreateApiTokenOptions): CreateApiTokenPayload {
|
||||
const normalizedName = (options?.name || '').trim() || 'metapi';
|
||||
const unlimitedQuota = options?.unlimitedQuota ?? true;
|
||||
const remainQuota = Number.isFinite(options?.remainQuota)
|
||||
? Math.max(0, Math.trunc(options?.remainQuota as number))
|
||||
: 0;
|
||||
const expiredTime = Number.isFinite(options?.expiredTime)
|
||||
? Math.trunc(options?.expiredTime as number)
|
||||
: -1;
|
||||
return {
|
||||
name: normalizedName,
|
||||
unlimited_quota: unlimitedQuota,
|
||||
expired_time: expiredTime,
|
||||
remain_quota: remainQuota,
|
||||
allow_ips: (options?.allowIps || '').trim(),
|
||||
model_limits_enabled: options?.modelLimitsEnabled ?? false,
|
||||
model_limits: (options?.modelLimits || '').trim(),
|
||||
group: (options?.group || '').trim(),
|
||||
};
|
||||
}
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${url}/api/status`);
|
||||
return res?.success === true && !res?.data?.system_name;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async checkin(baseUrl: string, accessToken: string): Promise<CheckinResult> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/checkin`, {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
if (res?.success) {
|
||||
return { success: true, message: res.message || 'Check-in successful', reward: res.data?.reward?.toString() };
|
||||
}
|
||||
return { success: false, message: res?.message || 'Check-in failed' };
|
||||
} catch (err: any) {
|
||||
return { success: false, message: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
async getBalance(baseUrl: string, accessToken: string): Promise<BalanceInfo> {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/self`, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
const data = res?.data;
|
||||
const quota = (data?.quota || 0) / 500000;
|
||||
const used = (data?.used_quota || 0) / 500000;
|
||||
const todayIncome = Number.isFinite(data?.today_income) ? (data.today_income / 500000) : undefined;
|
||||
const todayQuotaConsumption = Number.isFinite(data?.today_quota_consumption) ? (data.today_quota_consumption / 500000) : undefined;
|
||||
return { balance: quota - used, used, quota, todayIncome, todayQuotaConsumption };
|
||||
}
|
||||
|
||||
async getModels(baseUrl: string, apiToken: string, _platformUserId?: number): Promise<string[]> {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/v1/models`, {
|
||||
headers: { Authorization: `Bearer ${apiToken}` },
|
||||
});
|
||||
return (res?.data || []).map((m: any) => m.id).filter(Boolean);
|
||||
}
|
||||
|
||||
async getApiTokens(baseUrl: string, accessToken: string): Promise<ApiTokenInfo[]> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/token/?p=0&size=100`, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
|
||||
const items = (() => {
|
||||
if (Array.isArray(res?.data)) return res.data;
|
||||
if (Array.isArray(res?.data?.items)) return res.data.items;
|
||||
if (Array.isArray(res?.data?.data)) return res.data.data;
|
||||
if (Array.isArray(res?.items)) return res.items;
|
||||
if (Array.isArray(res?.list)) return res.list;
|
||||
if (Array.isArray(res?.data?.list)) return res.data.list;
|
||||
return [];
|
||||
})();
|
||||
|
||||
return items
|
||||
.map((item: any, index: number) => {
|
||||
const key = typeof item?.key === 'string' ? item.key.trim() : '';
|
||||
if (!key) return null;
|
||||
const rawName = typeof item?.name === 'string' ? item.name.trim() : '';
|
||||
const status = typeof item?.status === 'number' ? item.status : undefined;
|
||||
return {
|
||||
name: rawName || (index === 0 ? 'default' : `token-${index + 1}`),
|
||||
key,
|
||||
enabled: status === undefined ? true : status === 1,
|
||||
} satisfies ApiTokenInfo;
|
||||
})
|
||||
.filter((item: ApiTokenInfo | null): item is ApiTokenInfo => !!item);
|
||||
} catch {
|
||||
return [];
|
||||
}
|
||||
}
|
||||
|
||||
async getApiToken(baseUrl: string, accessToken: string): Promise<string | null> {
|
||||
const tokens = await this.getApiTokens(baseUrl, accessToken);
|
||||
return tokens.find((token) => token.enabled !== false)?.key || tokens[0]?.key || null;
|
||||
}
|
||||
|
||||
async getUserGroups(baseUrl: string, accessToken: string): Promise<string[]> {
|
||||
const headers = { Authorization: `Bearer ${accessToken}` };
|
||||
const extractGroupKeys = (payload: any): string[] => {
|
||||
const source = payload?.data || payload;
|
||||
if (!source || typeof source !== 'object') return [];
|
||||
return Object.keys(source).map((key) => key.trim()).filter(Boolean);
|
||||
};
|
||||
|
||||
try {
|
||||
const groupMap = await this.fetchJson<any>(`${baseUrl}/api/user_group_map`, { headers });
|
||||
const keys = extractGroupKeys(groupMap);
|
||||
if (keys.length > 0) return Array.from(new Set(keys));
|
||||
} catch {}
|
||||
|
||||
try {
|
||||
const groups = await this.fetchJson<any>(`${baseUrl}/api/user/self/groups`, { headers });
|
||||
const keys = extractGroupKeys(groups);
|
||||
if (keys.length > 0) return Array.from(new Set(keys));
|
||||
} catch {}
|
||||
|
||||
return ['default'];
|
||||
}
|
||||
|
||||
async createApiToken(
|
||||
baseUrl: string,
|
||||
accessToken: string,
|
||||
_platformUserId?: number,
|
||||
options?: CreateApiTokenOptions,
|
||||
): Promise<boolean> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/token/`, {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
body: JSON.stringify(this.buildDefaultTokenPayload(options)),
|
||||
});
|
||||
return !!res?.success;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async deleteApiToken(
|
||||
baseUrl: string,
|
||||
accessToken: string,
|
||||
tokenKey: string,
|
||||
): Promise<boolean> {
|
||||
const targetKey = this.normalizeTokenKeyForCompare(tokenKey);
|
||||
if (!targetKey) return false;
|
||||
|
||||
const headers = { Authorization: `Bearer ${accessToken}` };
|
||||
let tokenId: number | null = null;
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/token/?p=0&size=100`, { headers });
|
||||
const items = (() => {
|
||||
if (Array.isArray(res?.data)) return res.data;
|
||||
if (Array.isArray(res?.data?.items)) return res.data.items;
|
||||
if (Array.isArray(res?.items)) return res.items;
|
||||
return [];
|
||||
})();
|
||||
for (const item of items) {
|
||||
const key = this.normalizeTokenKeyForCompare(item?.key);
|
||||
const id = Number.parseInt(String(item?.id), 10);
|
||||
if (key && key === targetKey && Number.isFinite(id)) {
|
||||
tokenId = id;
|
||||
break;
|
||||
}
|
||||
}
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
|
||||
if (!tokenId) return true;
|
||||
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/token/${tokenId}`, {
|
||||
method: 'DELETE',
|
||||
headers,
|
||||
});
|
||||
if (res?.success) return true;
|
||||
} catch {}
|
||||
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/token/${tokenId}/`, {
|
||||
method: 'DELETE',
|
||||
headers,
|
||||
});
|
||||
return !!res?.success;
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,11 @@
|
||||
import { OneApiAdapter } from './oneApi.js';
|
||||
|
||||
export class OneHubAdapter extends OneApiAdapter {
|
||||
readonly platformName = 'one-hub';
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
const normalized = url.toLowerCase();
|
||||
return normalized.includes('onehub') || normalized.includes('one-hub');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,10 @@
|
||||
import { OneApiAdapter } from './oneApi.js';
|
||||
|
||||
export class Sub2ApiAdapter extends OneApiAdapter {
|
||||
readonly platformName = 'sub2api';
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
return url.toLowerCase().includes('sub2api');
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,51 @@
|
||||
import { BasePlatformAdapter, CheckinResult, BalanceInfo } from './base.js';
|
||||
|
||||
export class VeloeraAdapter extends BasePlatformAdapter {
|
||||
readonly platformName = 'veloera';
|
||||
|
||||
async detect(url: string): Promise<boolean> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${url}/api/status`);
|
||||
return res?.success === true && (
|
||||
res?.data?.system_name?.toLowerCase().includes('veloera') ||
|
||||
res?.data?.version?.includes('veloera')
|
||||
);
|
||||
} catch {
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
async checkin(baseUrl: string, accessToken: string): Promise<CheckinResult> {
|
||||
try {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/checkin`, {
|
||||
method: 'POST',
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
if (res?.success) {
|
||||
return { success: true, message: res.message || 'Check-in successful', reward: res.data?.reward?.toString() };
|
||||
}
|
||||
return { success: false, message: res?.message || 'Check-in failed' };
|
||||
} catch (err: any) {
|
||||
return { success: false, message: err.message };
|
||||
}
|
||||
}
|
||||
|
||||
async getBalance(baseUrl: string, accessToken: string): Promise<BalanceInfo> {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/api/user/self`, {
|
||||
headers: { Authorization: `Bearer ${accessToken}` },
|
||||
});
|
||||
const data = res?.data;
|
||||
const quota = (data?.quota || 0) / 1000000;
|
||||
const used = (data?.used_quota || 0) / 1000000;
|
||||
const todayIncome = Number.isFinite(data?.today_income) ? (data.today_income / 1000000) : undefined;
|
||||
const todayQuotaConsumption = Number.isFinite(data?.today_quota_consumption) ? (data.today_quota_consumption / 1000000) : undefined;
|
||||
return { balance: quota - used, used, quota, todayIncome, todayQuotaConsumption };
|
||||
}
|
||||
|
||||
async getModels(baseUrl: string, apiToken: string, _platformUserId?: number): Promise<string[]> {
|
||||
const res = await this.fetchJson<any>(`${baseUrl}/v1/models`, {
|
||||
headers: { Authorization: `Bearer ${apiToken}` },
|
||||
});
|
||||
return (res?.data || []).map((m: any) => m.id).filter(Boolean);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,34 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { shouldRetryProxyRequest } from './proxyRetryPolicy.js';
|
||||
|
||||
describe('proxyRetryPolicy', () => {
|
||||
it('retries on rate limit and server errors', () => {
|
||||
expect(shouldRetryProxyRequest(429, 'rate limit')).toBe(true);
|
||||
expect(shouldRetryProxyRequest(500, 'internal error')).toBe(true);
|
||||
expect(shouldRetryProxyRequest(503, 'service unavailable')).toBe(true);
|
||||
});
|
||||
|
||||
it('retries on model unsupported messages from upstream', () => {
|
||||
expect(
|
||||
shouldRetryProxyRequest(400, '{"error":"当前 API 不支持所选模型 claude-sonnet-4-5-20250929","type":"error"}'),
|
||||
).toBe(true);
|
||||
expect(
|
||||
shouldRetryProxyRequest(400, '{"error":{"message":"unsupported model: claude-3"}}'),
|
||||
).toBe(true);
|
||||
expect(
|
||||
shouldRetryProxyRequest(404, '{"error":{"message":"The model `gpt-4.1` does not exist"}}'),
|
||||
).toBe(true);
|
||||
});
|
||||
|
||||
it('does not retry on generic client request errors', () => {
|
||||
expect(
|
||||
shouldRetryProxyRequest(400, '{"error":{"message":"invalid request body"}}'),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldRetryProxyRequest(401, '{"error":{"message":"invalid access token"}}'),
|
||||
).toBe(false);
|
||||
expect(
|
||||
shouldRetryProxyRequest(403, '{"error":{"message":"forbidden"}}'),
|
||||
).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,27 @@
|
||||
const MODEL_UNSUPPORTED_PATTERNS: RegExp[] = [
|
||||
/当前\s*api\s*不支持所选模型/i,
|
||||
/不支持所选模型/i,
|
||||
/不支持.*模型/i,
|
||||
/模型.*不支持/i,
|
||||
/unsupported\s+model/i,
|
||||
/model\s+not\s+supported/i,
|
||||
/does\s+not\s+support(?:\s+the)?\s+model/i,
|
||||
/model.*does\s+not\s+exist/i,
|
||||
/no\s+such\s+model/i,
|
||||
/unknown\s+model/i,
|
||||
/invalid\s+model/i,
|
||||
/model[_\s-]?not[_\s-]?found/i,
|
||||
/you\s+do\s+not\s+have\s+access\s+to\s+the\s+model/i,
|
||||
];
|
||||
|
||||
function isModelUnsupportedErrorMessage(rawMessage?: string | null): boolean {
|
||||
const text = (rawMessage || '').trim();
|
||||
if (!text) return false;
|
||||
return MODEL_UNSUPPORTED_PATTERNS.some((pattern) => pattern.test(text));
|
||||
}
|
||||
|
||||
export function shouldRetryProxyRequest(status: number, upstreamErrorText?: string | null): boolean {
|
||||
if (status === 429 || status >= 500) return true;
|
||||
if (status !== 400 && status !== 404 && status !== 422) return false;
|
||||
return isModelUnsupportedErrorMessage(upstreamErrorText);
|
||||
}
|
||||
@@ -0,0 +1,147 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
extractSelfLogItems,
|
||||
findBestSelfLogMatch,
|
||||
shouldLookupSelfLog,
|
||||
type SelfLogItem,
|
||||
} from './proxyUsageFallbackService.js';
|
||||
|
||||
describe('proxyUsageFallbackService', () => {
|
||||
it('extracts logs from nested /api/log/self payload', () => {
|
||||
const payload = {
|
||||
success: true,
|
||||
data: {
|
||||
data: [
|
||||
{
|
||||
model_name: 'gpt-4o',
|
||||
prompt_tokens: 120,
|
||||
completion_tokens: 80,
|
||||
quota: 3300,
|
||||
created_at: 1_700_000_123,
|
||||
request_time: 1450,
|
||||
},
|
||||
],
|
||||
},
|
||||
};
|
||||
|
||||
const logs = extractSelfLogItems(payload);
|
||||
expect(logs).toEqual([
|
||||
{
|
||||
modelName: 'gpt-4o',
|
||||
tokenName: '',
|
||||
promptTokens: 120,
|
||||
completionTokens: 80,
|
||||
totalTokens: 200,
|
||||
quota: 3300,
|
||||
createdAtMs: 1_700_000_123_000,
|
||||
requestTimeMs: 1450,
|
||||
},
|
||||
]);
|
||||
});
|
||||
|
||||
it('matches best log by model + time window + request time', () => {
|
||||
const logs: SelfLogItem[] = [
|
||||
{
|
||||
modelName: 'gpt-4o',
|
||||
tokenName: 'default',
|
||||
promptTokens: 10,
|
||||
completionTokens: 5,
|
||||
totalTokens: 15,
|
||||
quota: 1000,
|
||||
createdAtMs: 1_700_000_000_000,
|
||||
requestTimeMs: 800,
|
||||
},
|
||||
{
|
||||
modelName: 'gpt-4o',
|
||||
tokenName: 'default',
|
||||
promptTokens: 200,
|
||||
completionTokens: 100,
|
||||
totalTokens: 300,
|
||||
quota: 4500,
|
||||
createdAtMs: 1_700_000_007_000,
|
||||
requestTimeMs: 3100,
|
||||
},
|
||||
{
|
||||
modelName: 'gpt-4o-mini',
|
||||
tokenName: 'default',
|
||||
promptTokens: 999,
|
||||
completionTokens: 1,
|
||||
totalTokens: 1000,
|
||||
quota: 9999,
|
||||
createdAtMs: 1_700_000_007_000,
|
||||
requestTimeMs: 3100,
|
||||
},
|
||||
];
|
||||
|
||||
const matched = findBestSelfLogMatch(logs, {
|
||||
modelName: 'gpt-4o',
|
||||
requestStartedAtMs: 1_700_000_004_000,
|
||||
requestEndedAtMs: 1_700_000_008_200,
|
||||
localLatencyMs: 3200,
|
||||
});
|
||||
|
||||
expect(matched).toEqual(logs[1]);
|
||||
});
|
||||
|
||||
it('returns null when no safe match exists', () => {
|
||||
const logs: SelfLogItem[] = [
|
||||
{
|
||||
modelName: 'gpt-4o',
|
||||
tokenName: 'default',
|
||||
promptTokens: 120,
|
||||
completionTokens: 80,
|
||||
totalTokens: 200,
|
||||
quota: 3300,
|
||||
createdAtMs: 1_700_000_123_000,
|
||||
requestTimeMs: 1450,
|
||||
},
|
||||
];
|
||||
|
||||
const matched = findBestSelfLogMatch(logs, {
|
||||
modelName: 'gpt-4o',
|
||||
requestStartedAtMs: 1_700_010_000_000,
|
||||
requestEndedAtMs: 1_700_010_004_000,
|
||||
localLatencyMs: 4000,
|
||||
});
|
||||
|
||||
expect(matched).toBeNull();
|
||||
});
|
||||
|
||||
it('matches provider-prefixed model names', () => {
|
||||
const logs: SelfLogItem[] = [
|
||||
{
|
||||
modelName: 'z-ai/glm4.7',
|
||||
tokenName: 'sys_playground',
|
||||
promptTokens: 1095,
|
||||
completionTokens: 2179,
|
||||
totalTokens: 3274,
|
||||
quota: 1890000,
|
||||
createdAtMs: 1_772_024_884_000,
|
||||
requestTimeMs: 59_000,
|
||||
},
|
||||
];
|
||||
|
||||
const matched = findBestSelfLogMatch(logs, {
|
||||
modelName: 'glm4.7',
|
||||
tokenName: 'windhub',
|
||||
requestStartedAtMs: 1_772_024_825_000,
|
||||
requestEndedAtMs: 1_772_024_884_500,
|
||||
localLatencyMs: 59_000,
|
||||
});
|
||||
|
||||
expect(matched).toEqual(logs[0]);
|
||||
});
|
||||
|
||||
it('always enables self-log lookup for done-hub/one-hub', () => {
|
||||
expect(shouldLookupSelfLog('done-hub', { promptTokens: 12, completionTokens: 8, totalTokens: 20 })).toBe(true);
|
||||
expect(shouldLookupSelfLog('one-hub', { promptTokens: 1, completionTokens: 1, totalTokens: 2 })).toBe(true);
|
||||
});
|
||||
|
||||
it('only enables self-log lookup for new-api/anyrouter when usage is missing', () => {
|
||||
expect(shouldLookupSelfLog('new-api', { promptTokens: 0, completionTokens: 0, totalTokens: 0 })).toBe(true);
|
||||
expect(shouldLookupSelfLog('anyrouter', { promptTokens: 0, completionTokens: 0, totalTokens: 0 })).toBe(true);
|
||||
|
||||
expect(shouldLookupSelfLog('new-api', { promptTokens: 12, completionTokens: 3, totalTokens: 15 })).toBe(false);
|
||||
expect(shouldLookupSelfLog('anyrouter', { promptTokens: 12, completionTokens: 3, totalTokens: 15 })).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,388 @@
|
||||
import { fetch } from 'undici';
|
||||
import { resolvePlatformUserId } from './accountExtraConfig.js';
|
||||
|
||||
const SELF_LOG_FETCH_TIMEOUT_MS = 8_000;
|
||||
const SELF_LOG_PAGE_SIZE = 20;
|
||||
const MATCH_LOOKBACK_MS = 25_000;
|
||||
const MATCH_LOOKAHEAD_MS = 120_000;
|
||||
const MATCH_MAX_CREATED_DELTA_MS = 90_000;
|
||||
const MATCH_MAX_LATENCY_DELTA_MS = 12_000;
|
||||
const QUOTA_PER_UNIT = 500_000;
|
||||
const SUPPORTED_USAGE_FALLBACK_PLATFORMS = new Set(['done-hub', 'one-hub', 'new-api', 'anyrouter']);
|
||||
const ALWAYS_LOOKUP_SELF_LOG_PLATFORMS = new Set(['done-hub', 'one-hub']);
|
||||
const PLATFORM_REQUIRES_USER_HEADER = new Set(['new-api', 'anyrouter']);
|
||||
|
||||
interface ProxyUsage {
|
||||
promptTokens: number;
|
||||
completionTokens: number;
|
||||
totalTokens: number;
|
||||
}
|
||||
|
||||
interface ProxyUsageFallbackInput {
|
||||
site: {
|
||||
url: string;
|
||||
platform: string;
|
||||
apiKey?: string | null;
|
||||
};
|
||||
account: {
|
||||
accessToken?: string | null;
|
||||
apiToken?: string | null;
|
||||
username?: string | null;
|
||||
extraConfig?: string | null;
|
||||
platformUserId?: number | null;
|
||||
};
|
||||
tokenValue?: string | null;
|
||||
tokenName?: string | null;
|
||||
modelName: string;
|
||||
requestStartedAtMs: number;
|
||||
requestEndedAtMs: number;
|
||||
localLatencyMs: number;
|
||||
usage: ProxyUsage;
|
||||
}
|
||||
|
||||
interface ProxyUsageFallbackResult extends ProxyUsage {
|
||||
recoveredFromSelfLog: boolean;
|
||||
estimatedCostFromQuota: number;
|
||||
}
|
||||
|
||||
export interface SelfLogItem {
|
||||
modelName: string;
|
||||
tokenName: string;
|
||||
promptTokens: number;
|
||||
completionTokens: number;
|
||||
totalTokens: number;
|
||||
quota: number;
|
||||
createdAtMs: number;
|
||||
requestTimeMs: number;
|
||||
}
|
||||
|
||||
interface SelfLogMatchInput {
|
||||
modelName: string;
|
||||
tokenName?: string | null;
|
||||
requestStartedAtMs: number;
|
||||
requestEndedAtMs: number;
|
||||
localLatencyMs: number;
|
||||
}
|
||||
|
||||
function toNumber(value: unknown, fallback = 0): number {
|
||||
const n = typeof value === 'number' ? value : Number(value);
|
||||
if (!Number.isFinite(n)) return fallback;
|
||||
return n;
|
||||
}
|
||||
|
||||
function toPositiveInt(value: unknown): number {
|
||||
return Math.max(0, Math.round(toNumber(value, 0)));
|
||||
}
|
||||
|
||||
function roundCost(value: number): number {
|
||||
return Math.round(Math.max(0, value) * 1_000_000) / 1_000_000;
|
||||
}
|
||||
|
||||
function toTimestampMs(value: unknown): number {
|
||||
if (typeof value === 'number' && Number.isFinite(value)) {
|
||||
if (value > 1_000_000_000_000) return Math.round(value);
|
||||
if (value > 1_000_000_000) return Math.round(value * 1000);
|
||||
return 0;
|
||||
}
|
||||
|
||||
if (typeof value === 'string') {
|
||||
const trimmed = value.trim();
|
||||
if (!trimmed) return 0;
|
||||
|
||||
const numeric = Number(trimmed);
|
||||
if (Number.isFinite(numeric)) {
|
||||
return toTimestampMs(numeric);
|
||||
}
|
||||
|
||||
const parsed = Date.parse(trimmed);
|
||||
return Number.isNaN(parsed) ? 0 : parsed;
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
function normalizeUsage(usage: ProxyUsage): ProxyUsage {
|
||||
const promptTokens = toPositiveInt(usage.promptTokens);
|
||||
const completionTokens = toPositiveInt(usage.completionTokens);
|
||||
const totalTokensRaw = toPositiveInt(usage.totalTokens);
|
||||
const totalTokens = totalTokensRaw > 0 ? totalTokensRaw : (promptTokens + completionTokens);
|
||||
return {
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens,
|
||||
};
|
||||
}
|
||||
|
||||
function isUsageMissing(usage: ProxyUsage): boolean {
|
||||
return usage.promptTokens <= 0 && usage.completionTokens <= 0 && usage.totalTokens <= 0;
|
||||
}
|
||||
|
||||
export function shouldLookupSelfLog(
|
||||
platform: string,
|
||||
usage: ProxyUsage,
|
||||
): boolean {
|
||||
const normalizedPlatform = String(platform || '').toLowerCase();
|
||||
if (!SUPPORTED_USAGE_FALLBACK_PLATFORMS.has(normalizedPlatform)) return false;
|
||||
if (ALWAYS_LOOKUP_SELF_LOG_PLATFORMS.has(normalizedPlatform)) return true;
|
||||
return isUsageMissing(usage);
|
||||
}
|
||||
|
||||
function normalizeUrl(url: string): string {
|
||||
return url.replace(/\/+$/, '');
|
||||
}
|
||||
|
||||
function getArrayNode(value: unknown): unknown[] | null {
|
||||
if (Array.isArray(value)) return value;
|
||||
if (!value || typeof value !== 'object') return null;
|
||||
|
||||
const record = value as Record<string, unknown>;
|
||||
if (Array.isArray(record.data)) return record.data;
|
||||
if (Array.isArray(record.items)) return record.items;
|
||||
return null;
|
||||
}
|
||||
|
||||
function modelNameMatches(left: string, right: string): boolean {
|
||||
const normalizedLeft = left.trim().toLowerCase();
|
||||
const normalizedRight = right.trim().toLowerCase();
|
||||
if (!normalizedLeft || !normalizedRight) return false;
|
||||
if (normalizedLeft === normalizedRight) return true;
|
||||
if (normalizedLeft.endsWith(`/${normalizedRight}`)) return true;
|
||||
if (normalizedRight.endsWith(`/${normalizedLeft}`)) return true;
|
||||
|
||||
const leftTail = normalizedLeft.split('/').pop() || '';
|
||||
const rightTail = normalizedRight.split('/').pop() || '';
|
||||
return !!leftTail && leftTail === rightTail;
|
||||
}
|
||||
|
||||
function getPayloadList(payload: unknown): unknown[] {
|
||||
const candidates = [
|
||||
payload,
|
||||
(payload as any)?.data,
|
||||
(payload as any)?.data?.data,
|
||||
(payload as any)?.data?.items,
|
||||
(payload as any)?.items,
|
||||
];
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const list = getArrayNode(candidate);
|
||||
if (list) return list;
|
||||
}
|
||||
|
||||
return [];
|
||||
}
|
||||
|
||||
function mapSelfLogItem(raw: unknown): SelfLogItem | null {
|
||||
if (!raw || typeof raw !== 'object') return null;
|
||||
const row = raw as Record<string, unknown>;
|
||||
|
||||
const modelName = String(row.model_name ?? row.modelName ?? '').trim();
|
||||
if (!modelName) return null;
|
||||
|
||||
const promptTokens = toPositiveInt(row.prompt_tokens ?? row.promptTokens);
|
||||
const completionTokens = toPositiveInt(row.completion_tokens ?? row.completionTokens);
|
||||
const totalTokensRaw = toPositiveInt(row.total_tokens ?? row.totalTokens);
|
||||
const totalTokens = totalTokensRaw > 0 ? totalTokensRaw : (promptTokens + completionTokens);
|
||||
const createdAtMs = toTimestampMs(row.created_at ?? row.createdAt);
|
||||
if (createdAtMs <= 0) return null;
|
||||
|
||||
return {
|
||||
modelName,
|
||||
tokenName: String(row.token_name ?? row.tokenName ?? '').trim(),
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens,
|
||||
quota: toPositiveInt(row.quota),
|
||||
createdAtMs,
|
||||
requestTimeMs: toPositiveInt(row.request_time ?? row.requestTime),
|
||||
};
|
||||
}
|
||||
|
||||
function buildTokenCandidates(input: ProxyUsageFallbackInput): string[] {
|
||||
const candidates = [
|
||||
input.account.accessToken,
|
||||
input.tokenValue,
|
||||
input.account.apiToken,
|
||||
input.site.apiKey,
|
||||
]
|
||||
.map((value) => (typeof value === 'string' ? value.trim() : ''))
|
||||
.filter(Boolean);
|
||||
return Array.from(new Set(candidates));
|
||||
}
|
||||
|
||||
function resolveSelfLogUserId(input: ProxyUsageFallbackInput): number | undefined {
|
||||
const direct = toPositiveInt(input.account.platformUserId);
|
||||
if (direct > 0) return direct;
|
||||
return resolvePlatformUserId(input.account.extraConfig, input.account.username);
|
||||
}
|
||||
|
||||
async function fetchSelfLogPayload(baseUrl: string, token: string, input: ProxyUsageFallbackInput): Promise<unknown> {
|
||||
const controller = new AbortController();
|
||||
let timeoutHandle: ReturnType<typeof setTimeout> | null = setTimeout(() => {
|
||||
controller.abort();
|
||||
}, SELF_LOG_FETCH_TIMEOUT_MS);
|
||||
|
||||
try {
|
||||
const query = `p=0&page=1&size=${SELF_LOG_PAGE_SIZE}&order=-created_at`;
|
||||
const headers: Record<string, string> = {
|
||||
Authorization: `Bearer ${token}`,
|
||||
};
|
||||
const platform = String(input.site.platform || '').toLowerCase();
|
||||
if (PLATFORM_REQUIRES_USER_HEADER.has(platform)) {
|
||||
const userId = resolveSelfLogUserId(input);
|
||||
if (userId) {
|
||||
headers['New-Api-User'] = String(userId);
|
||||
}
|
||||
}
|
||||
const response = await fetch(`${baseUrl}/api/log/self?${query}`, {
|
||||
method: 'GET',
|
||||
headers,
|
||||
signal: controller.signal,
|
||||
});
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(`HTTP ${response.status}`);
|
||||
}
|
||||
|
||||
const text = await response.text();
|
||||
if (!text.trim()) return null;
|
||||
|
||||
try {
|
||||
return JSON.parse(text);
|
||||
} catch {
|
||||
return null;
|
||||
}
|
||||
} finally {
|
||||
if (timeoutHandle) {
|
||||
clearTimeout(timeoutHandle);
|
||||
timeoutHandle = null;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
async function fetchRecentSelfLogItems(input: ProxyUsageFallbackInput): Promise<SelfLogItem[]> {
|
||||
const baseUrl = normalizeUrl(input.site.url);
|
||||
const tokens = buildTokenCandidates(input);
|
||||
for (const token of tokens) {
|
||||
try {
|
||||
const payload = await fetchSelfLogPayload(baseUrl, token, input);
|
||||
const items = extractSelfLogItems(payload);
|
||||
if (items.length > 0) return items;
|
||||
} catch {}
|
||||
}
|
||||
return [];
|
||||
}
|
||||
|
||||
export function extractSelfLogItems(payload: unknown): SelfLogItem[] {
|
||||
const rows = getPayloadList(payload);
|
||||
const items = rows
|
||||
.map((row) => mapSelfLogItem(row))
|
||||
.filter((item): item is SelfLogItem => item !== null)
|
||||
.sort((a, b) => b.createdAtMs - a.createdAtMs);
|
||||
|
||||
return items;
|
||||
}
|
||||
|
||||
export function findBestSelfLogMatch(items: SelfLogItem[], input: SelfLogMatchInput): SelfLogItem | null {
|
||||
const requestedModel = input.modelName.trim();
|
||||
if (!requestedModel || items.length === 0) return null;
|
||||
|
||||
const windowStart = input.requestStartedAtMs - MATCH_LOOKBACK_MS;
|
||||
const windowEnd = input.requestEndedAtMs + MATCH_LOOKAHEAD_MS;
|
||||
let candidates = items.filter((item) => (
|
||||
modelNameMatches(item.modelName, requestedModel)
|
||||
&& item.createdAtMs >= windowStart
|
||||
&& item.createdAtMs <= windowEnd
|
||||
&& (item.totalTokens > 0 || item.quota > 0 || item.requestTimeMs > 0)
|
||||
));
|
||||
if (candidates.length === 0) return null;
|
||||
|
||||
const tokenName = String(input.tokenName || '').trim().toLowerCase();
|
||||
if (tokenName) {
|
||||
const tokenMatched = candidates.filter((item) => item.tokenName.trim().toLowerCase() === tokenName);
|
||||
if (tokenMatched.length > 0) {
|
||||
candidates = tokenMatched;
|
||||
}
|
||||
}
|
||||
|
||||
let best: SelfLogItem | null = null;
|
||||
let bestScore = Number.POSITIVE_INFINITY;
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const createdAtDelta = Math.abs(candidate.createdAtMs - input.requestEndedAtMs);
|
||||
const latencyDelta = (input.localLatencyMs > 0 && candidate.requestTimeMs > 0)
|
||||
? Math.abs(candidate.requestTimeMs - input.localLatencyMs)
|
||||
: 0;
|
||||
const score = createdAtDelta + (latencyDelta * 2);
|
||||
|
||||
if (score < bestScore) {
|
||||
best = candidate;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
if (!best) return null;
|
||||
|
||||
const createdDelta = Math.abs(best.createdAtMs - input.requestEndedAtMs);
|
||||
if (createdDelta > MATCH_MAX_CREATED_DELTA_MS) return null;
|
||||
|
||||
if (input.localLatencyMs > 0 && best.requestTimeMs > 0) {
|
||||
const latencyDelta = Math.abs(best.requestTimeMs - input.localLatencyMs);
|
||||
if (latencyDelta > MATCH_MAX_LATENCY_DELTA_MS) return null;
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
function toQuotaCost(quota: number): number {
|
||||
return roundCost(toPositiveInt(quota) / QUOTA_PER_UNIT);
|
||||
}
|
||||
|
||||
export async function resolveProxyUsageWithSelfLogFallback(
|
||||
input: ProxyUsageFallbackInput,
|
||||
): Promise<ProxyUsageFallbackResult> {
|
||||
const normalizedUsage = normalizeUsage(input.usage);
|
||||
const fallback: ProxyUsageFallbackResult = {
|
||||
...normalizedUsage,
|
||||
recoveredFromSelfLog: false,
|
||||
estimatedCostFromQuota: 0,
|
||||
};
|
||||
|
||||
const platform = String(input.site.platform || '').toLowerCase();
|
||||
if (!shouldLookupSelfLog(platform, normalizedUsage)) {
|
||||
return fallback;
|
||||
}
|
||||
|
||||
try {
|
||||
const items = await fetchRecentSelfLogItems(input);
|
||||
const matched = findBestSelfLogMatch(items, {
|
||||
modelName: input.modelName,
|
||||
tokenName: input.tokenName,
|
||||
requestStartedAtMs: input.requestStartedAtMs,
|
||||
requestEndedAtMs: input.requestEndedAtMs,
|
||||
localLatencyMs: input.localLatencyMs,
|
||||
});
|
||||
|
||||
if (!matched) return fallback;
|
||||
|
||||
const matchedUsage: ProxyUsage = {
|
||||
promptTokens: matched.promptTokens,
|
||||
completionTokens: matched.completionTokens,
|
||||
totalTokens: matched.totalTokens,
|
||||
};
|
||||
const normalizedMatched = normalizeUsage(matchedUsage);
|
||||
const useMatchedTokens = normalizedMatched.totalTokens > 0
|
||||
|| normalizedMatched.promptTokens > 0
|
||||
|| normalizedMatched.completionTokens > 0;
|
||||
const resolvedTokens = useMatchedTokens ? normalizedMatched : normalizedUsage;
|
||||
|
||||
return {
|
||||
promptTokens: resolvedTokens.promptTokens,
|
||||
completionTokens: resolvedTokens.completionTokens,
|
||||
totalTokens: resolvedTokens.totalTokens,
|
||||
recoveredFromSelfLog: true,
|
||||
estimatedCostFromQuota: toQuotaCost(matched.quota),
|
||||
};
|
||||
} catch {
|
||||
return fallback;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,120 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
mergeProxyUsage,
|
||||
parseProxyUsage,
|
||||
pullSseDataEvents,
|
||||
} from './proxyUsageParser.js';
|
||||
|
||||
describe('proxyUsageParser', () => {
|
||||
it('parses standard OpenAI usage fields', () => {
|
||||
const usage = parseProxyUsage({
|
||||
usage: {
|
||||
prompt_tokens: 123,
|
||||
completion_tokens: 45,
|
||||
total_tokens: 168,
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage).toEqual({
|
||||
promptTokens: 123,
|
||||
completionTokens: 45,
|
||||
totalTokens: 168,
|
||||
});
|
||||
});
|
||||
|
||||
it('parses input/output token style usage fields', () => {
|
||||
const usage = parseProxyUsage({
|
||||
usage: {
|
||||
input_tokens: 80,
|
||||
output_tokens: 20,
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage).toEqual({
|
||||
promptTokens: 80,
|
||||
completionTokens: 20,
|
||||
totalTokens: 100,
|
||||
});
|
||||
});
|
||||
|
||||
it('parses Gemini usageMetadata shape', () => {
|
||||
const usage = parseProxyUsage({
|
||||
usageMetadata: {
|
||||
promptTokenCount: 12,
|
||||
candidatesTokenCount: 34,
|
||||
totalTokenCount: 46,
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage).toEqual({
|
||||
promptTokens: 12,
|
||||
completionTokens: 34,
|
||||
totalTokens: 46,
|
||||
});
|
||||
});
|
||||
|
||||
it('parses deeply nested usage payloads', () => {
|
||||
const usage = parseProxyUsage({
|
||||
data: {
|
||||
result: {
|
||||
response: {
|
||||
usage: {
|
||||
prompt_tokens: 210,
|
||||
completion_tokens: 40,
|
||||
total_tokens: 250,
|
||||
},
|
||||
},
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage).toEqual({
|
||||
promptTokens: 210,
|
||||
completionTokens: 40,
|
||||
totalTokens: 250,
|
||||
});
|
||||
});
|
||||
|
||||
it('falls back to usage detail objects when aggregate fields are absent', () => {
|
||||
const usage = parseProxyUsage({
|
||||
usage: {
|
||||
prompt_tokens_details: {
|
||||
text_tokens: 7,
|
||||
cached_tokens: 3,
|
||||
},
|
||||
completion_tokens_details: {
|
||||
reasoning_tokens: 20,
|
||||
},
|
||||
},
|
||||
});
|
||||
|
||||
expect(usage).toEqual({
|
||||
promptTokens: 10,
|
||||
completionTokens: 20,
|
||||
totalTokens: 30,
|
||||
});
|
||||
});
|
||||
|
||||
it('merges usage snapshots by keeping richer values', () => {
|
||||
const merged = mergeProxyUsage(
|
||||
{ promptTokens: 0, completionTokens: 0, totalTokens: 0 },
|
||||
{ promptTokens: 90, completionTokens: 30, totalTokens: 120 },
|
||||
);
|
||||
|
||||
expect(merged).toEqual({
|
||||
promptTokens: 90,
|
||||
completionTokens: 30,
|
||||
totalTokens: 120,
|
||||
});
|
||||
});
|
||||
|
||||
it('pulls SSE data events across chunk boundaries', () => {
|
||||
const first = pullSseDataEvents('data: {"a":1}\n\ndata: {"b":');
|
||||
expect(first.events).toEqual(['{"a":1}']);
|
||||
expect(first.rest).toBe('data: {"b":');
|
||||
|
||||
const second = pullSseDataEvents(`${first.rest}2}\n\ndata: [DONE]\n\n`);
|
||||
expect(second.events).toEqual(['{"b":2}']);
|
||||
expect(second.rest).toBe('');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,206 @@
|
||||
interface ParsedProxyUsage {
|
||||
promptTokens: number;
|
||||
completionTokens: number;
|
||||
totalTokens: number;
|
||||
}
|
||||
|
||||
const ZERO_USAGE: ParsedProxyUsage = {
|
||||
promptTokens: 0,
|
||||
completionTokens: 0,
|
||||
totalTokens: 0,
|
||||
};
|
||||
|
||||
function toPositiveInt(value: unknown): number {
|
||||
const n = typeof value === 'number' ? value : Number(value);
|
||||
if (!Number.isFinite(n)) return 0;
|
||||
return Math.max(0, Math.round(n));
|
||||
}
|
||||
|
||||
function isRecord(value: unknown): value is Record<string, unknown> {
|
||||
return !!value && typeof value === 'object';
|
||||
}
|
||||
|
||||
function collectUsageCandidates(payload: unknown): Array<Record<string, unknown>> {
|
||||
const candidates: Array<Record<string, unknown>> = [];
|
||||
const visited = new Set<object>();
|
||||
const queue: unknown[] = [];
|
||||
|
||||
const enqueue = (value: unknown) => {
|
||||
if (!value || typeof value !== 'object') return;
|
||||
queue.push(value);
|
||||
};
|
||||
|
||||
enqueue(payload);
|
||||
enqueue((payload as any)?.usage);
|
||||
enqueue((payload as any)?.usageMetadata);
|
||||
enqueue((payload as any)?.usage_metadata);
|
||||
enqueue((payload as any)?.token_usage);
|
||||
enqueue((payload as any)?.tokenUsage);
|
||||
|
||||
// Guard against unexpectedly deep/large payloads.
|
||||
let inspected = 0;
|
||||
const MAX_INSPECT = 200;
|
||||
|
||||
while (queue.length > 0 && inspected < MAX_INSPECT) {
|
||||
const current = queue.shift();
|
||||
inspected += 1;
|
||||
|
||||
if (Array.isArray(current)) {
|
||||
for (const item of current) enqueue(item);
|
||||
continue;
|
||||
}
|
||||
|
||||
if (!isRecord(current)) continue;
|
||||
if (visited.has(current)) continue;
|
||||
visited.add(current);
|
||||
candidates.push(current);
|
||||
|
||||
for (const value of Object.values(current)) {
|
||||
enqueue(value);
|
||||
}
|
||||
}
|
||||
|
||||
return candidates;
|
||||
}
|
||||
|
||||
function firstPositiveInt(record: Record<string, unknown>, keys: string[]): number {
|
||||
for (const key of keys) {
|
||||
const value = toPositiveInt(record[key]);
|
||||
if (value > 0) return value;
|
||||
}
|
||||
return 0;
|
||||
}
|
||||
|
||||
function sumNumericFields(value: unknown): number {
|
||||
if (!isRecord(value)) return 0;
|
||||
return Object.values(value).reduce<number>((sum, item) => sum + toPositiveInt(item), 0);
|
||||
}
|
||||
|
||||
function parseUsageRecord(record: Record<string, unknown>): ParsedProxyUsage {
|
||||
let promptTokens = firstPositiveInt(record, [
|
||||
'prompt_tokens',
|
||||
'promptTokens',
|
||||
'prompt_token_count',
|
||||
'promptTokenCount',
|
||||
'input_tokens',
|
||||
'inputTokens',
|
||||
'input_token_count',
|
||||
'inputTokenCount',
|
||||
]);
|
||||
let completionTokens = firstPositiveInt(record, [
|
||||
'completion_tokens',
|
||||
'completionTokens',
|
||||
'completion_token_count',
|
||||
'completionTokenCount',
|
||||
'candidates_token_count',
|
||||
'candidatesTokenCount',
|
||||
'output_tokens',
|
||||
'outputTokens',
|
||||
'output_token_count',
|
||||
'outputTokenCount',
|
||||
]);
|
||||
let totalTokens = firstPositiveInt(record, [
|
||||
'total_tokens',
|
||||
'totalTokens',
|
||||
'total_token_count',
|
||||
'totalTokenCount',
|
||||
]);
|
||||
|
||||
if (promptTokens <= 0) {
|
||||
promptTokens = Math.max(
|
||||
sumNumericFields(record.prompt_tokens_details),
|
||||
sumNumericFields(record.promptTokensDetails),
|
||||
sumNumericFields(record.input_tokens_details),
|
||||
sumNumericFields(record.inputTokensDetails),
|
||||
);
|
||||
}
|
||||
|
||||
if (completionTokens <= 0) {
|
||||
completionTokens = Math.max(
|
||||
sumNumericFields(record.completion_tokens_details),
|
||||
sumNumericFields(record.completionTokensDetails),
|
||||
sumNumericFields(record.output_tokens_details),
|
||||
sumNumericFields(record.outputTokensDetails),
|
||||
);
|
||||
}
|
||||
|
||||
if (totalTokens <= 0) {
|
||||
totalTokens = promptTokens + completionTokens;
|
||||
}
|
||||
|
||||
if (promptTokens <= 0 && totalTokens > completionTokens) {
|
||||
promptTokens = totalTokens - completionTokens;
|
||||
}
|
||||
if (completionTokens <= 0 && totalTokens > promptTokens) {
|
||||
completionTokens = totalTokens - promptTokens;
|
||||
}
|
||||
|
||||
return {
|
||||
promptTokens,
|
||||
completionTokens,
|
||||
totalTokens: Math.max(totalTokens, promptTokens + completionTokens),
|
||||
};
|
||||
}
|
||||
|
||||
export function parseProxyUsage(payload: unknown): ParsedProxyUsage {
|
||||
if (!payload || typeof payload !== 'object') return { ...ZERO_USAGE };
|
||||
const candidates = collectUsageCandidates(payload);
|
||||
|
||||
let best = { ...ZERO_USAGE };
|
||||
let bestScore = -1;
|
||||
|
||||
for (const candidate of candidates) {
|
||||
const parsed = parseUsageRecord(candidate);
|
||||
const score = parsed.totalTokens > 0
|
||||
? (parsed.totalTokens * 10_000 + parsed.promptTokens + parsed.completionTokens)
|
||||
: (parsed.promptTokens + parsed.completionTokens);
|
||||
if (score > bestScore) {
|
||||
best = parsed;
|
||||
bestScore = score;
|
||||
}
|
||||
}
|
||||
|
||||
return best;
|
||||
}
|
||||
|
||||
export function mergeProxyUsage(base: ParsedProxyUsage, incoming: ParsedProxyUsage): ParsedProxyUsage {
|
||||
const baseScore = base.totalTokens > 0 ? base.totalTokens * 10_000 + base.promptTokens + base.completionTokens : (base.promptTokens + base.completionTokens);
|
||||
const incomingScore = incoming.totalTokens > 0
|
||||
? incoming.totalTokens * 10_000 + incoming.promptTokens + incoming.completionTokens
|
||||
: (incoming.promptTokens + incoming.completionTokens);
|
||||
|
||||
if (incomingScore > baseScore) return incoming;
|
||||
|
||||
const promptTokens = Math.max(base.promptTokens, incoming.promptTokens);
|
||||
const completionTokens = Math.max(base.completionTokens, incoming.completionTokens);
|
||||
const totalTokens = Math.max(base.totalTokens, incoming.totalTokens, promptTokens + completionTokens);
|
||||
|
||||
return { promptTokens, completionTokens, totalTokens };
|
||||
}
|
||||
|
||||
export function pullSseDataEvents(buffer: string): { events: string[]; rest: string } {
|
||||
const normalized = buffer.replace(/\r\n/g, '\n');
|
||||
const events: string[] = [];
|
||||
let rest = normalized;
|
||||
|
||||
while (true) {
|
||||
const boundary = rest.indexOf('\n\n');
|
||||
if (boundary < 0) break;
|
||||
const block = rest.slice(0, boundary);
|
||||
rest = rest.slice(boundary + 2);
|
||||
|
||||
if (!block.trim()) continue;
|
||||
|
||||
const dataLines = block
|
||||
.split('\n')
|
||||
.filter((line) => line.startsWith('data:'))
|
||||
.map((line) => line.slice(5).trimStart());
|
||||
|
||||
if (dataLines.length <= 0) continue;
|
||||
const payload = dataLines.join('\n').trim();
|
||||
if (!payload || payload === '[DONE]') continue;
|
||||
events.push(payload);
|
||||
}
|
||||
|
||||
return { events, rest };
|
||||
}
|
||||
@@ -0,0 +1,8 @@
|
||||
import { detectPlatform } from './platforms/index.js';
|
||||
|
||||
export async function detectSite(url: string) {
|
||||
const normalizedUrl = url.replace(/\/+$/, '');
|
||||
const adapter = await detectPlatform(normalizedUrl);
|
||||
if (!adapter) return null;
|
||||
return { url: normalizedUrl, platform: adapter.platformName };
|
||||
}
|
||||
@@ -0,0 +1,32 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { buildStartupEndpoints, buildStartupSummaryLines } from './startupInfo.js';
|
||||
|
||||
describe('startupInfo', () => {
|
||||
it('builds single-port endpoint summary for admin and proxy APIs', () => {
|
||||
const endpoints = buildStartupEndpoints({
|
||||
port: 4000,
|
||||
host: '0.0.0.0',
|
||||
authToken: 'admin-token',
|
||||
proxyToken: 'proxy-token',
|
||||
});
|
||||
|
||||
expect(endpoints.baseUrl).toBe('http://127.0.0.1:4000');
|
||||
expect(endpoints.adminDashboardUrl).toBe('http://127.0.0.1:4000');
|
||||
expect(endpoints.adminApiExample).toBe('http://127.0.0.1:4000/api/stats/dashboard');
|
||||
expect(endpoints.proxyApiExample).toBe('http://127.0.0.1:4000/v1/chat/completions');
|
||||
});
|
||||
|
||||
it('renders copy-ready startup summary lines', () => {
|
||||
const lines = buildStartupSummaryLines({
|
||||
port: 4000,
|
||||
host: '0.0.0.0',
|
||||
authToken: 'admin-token',
|
||||
proxyToken: 'proxy-token',
|
||||
});
|
||||
|
||||
expect(lines.some((line) => line.includes('metapi running'))).toBe(true);
|
||||
expect(lines.some((line) => line.includes('Dashboard: http://127.0.0.1:4000'))).toBe(true);
|
||||
expect(lines.some((line) => line.includes('/api/stats/dashboard'))).toBe(true);
|
||||
expect(lines.some((line) => line.includes('/v1/chat/completions'))).toBe(true);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,51 @@
|
||||
type StartupSummaryInput = {
|
||||
port: number;
|
||||
host: string;
|
||||
authToken: string;
|
||||
proxyToken: string;
|
||||
};
|
||||
|
||||
type StartupEndpoints = {
|
||||
baseUrl: string;
|
||||
adminDashboardUrl: string;
|
||||
adminApiExample: string;
|
||||
proxyApiExample: string;
|
||||
adminApiCurl: string;
|
||||
proxyApiCurl: string;
|
||||
};
|
||||
|
||||
function resolveDisplayHost(host: string): string {
|
||||
const trimmed = (host || '').trim();
|
||||
if (!trimmed || trimmed === '0.0.0.0' || trimmed === '::') return '127.0.0.1';
|
||||
return trimmed;
|
||||
}
|
||||
|
||||
export function buildStartupEndpoints(input: StartupSummaryInput): StartupEndpoints {
|
||||
const displayHost = resolveDisplayHost(input.host);
|
||||
const baseUrl = `http://${displayHost}:${input.port}`;
|
||||
|
||||
const adminApiExample = `${baseUrl}/api/stats/dashboard`;
|
||||
const proxyApiExample = `${baseUrl}/v1/chat/completions`;
|
||||
|
||||
return {
|
||||
baseUrl,
|
||||
adminDashboardUrl: baseUrl,
|
||||
adminApiExample,
|
||||
proxyApiExample,
|
||||
adminApiCurl: `curl '${adminApiExample}' -H 'Authorization: Bearer ${input.authToken}'`,
|
||||
proxyApiCurl: `curl '${proxyApiExample}' -H 'Authorization: Bearer ${input.proxyToken}' -H 'Content-Type: application/json' -d '{"model":"gpt-4o-mini","messages":[{"role":"user","content":"ping"}]}'`,
|
||||
};
|
||||
}
|
||||
|
||||
export function buildStartupSummaryLines(input: StartupSummaryInput): string[] {
|
||||
const endpoints = buildStartupEndpoints(input);
|
||||
|
||||
return [
|
||||
`metapi running on ${input.host}:${input.port}`,
|
||||
`Dashboard: ${endpoints.adminDashboardUrl}`,
|
||||
`Admin API: ${endpoints.adminApiExample}`,
|
||||
`Proxy API: ${endpoints.proxyApiExample}`,
|
||||
`Admin curl: ${endpoints.adminApiCurl}`,
|
||||
`Proxy curl: ${endpoints.proxyApiCurl}`,
|
||||
];
|
||||
}
|
||||
@@ -0,0 +1,65 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import {
|
||||
estimateRewardWithTodayIncomeFallback,
|
||||
getTodayIncomeDelta,
|
||||
getTodayIncomeValue,
|
||||
updateTodayIncomeSnapshot,
|
||||
} from './todayIncomeRewardService.js';
|
||||
import { formatLocalDate } from './localTimeService.js';
|
||||
|
||||
describe('today income reward service', () => {
|
||||
it('records latest income value for same-day snapshots', () => {
|
||||
const now = new Date('2026-02-25T08:00:00.000Z');
|
||||
const later = new Date('2026-02-25T10:00:00.000Z');
|
||||
const day = formatLocalDate(now);
|
||||
|
||||
const first = updateTodayIncomeSnapshot(null, 12, now);
|
||||
expect(getTodayIncomeValue(first, day)).toBe(12);
|
||||
expect(getTodayIncomeDelta(first, day)).toBe(0);
|
||||
|
||||
const second = updateTodayIncomeSnapshot(first, 15.5, later);
|
||||
expect(getTodayIncomeValue(second, day)).toBe(15.5);
|
||||
expect(getTodayIncomeDelta(second, day)).toBe(3.5);
|
||||
});
|
||||
|
||||
it('resets baseline on day change', () => {
|
||||
const firstAt = new Date('2026-02-25T09:00:00.000Z');
|
||||
const secondAt = new Date('2026-02-26T09:00:00.000Z');
|
||||
const firstDay = formatLocalDate(firstAt);
|
||||
const secondDay = formatLocalDate(secondAt);
|
||||
const day1 = updateTodayIncomeSnapshot(null, 10, firstAt);
|
||||
const day2 = updateTodayIncomeSnapshot(day1, 4, secondAt);
|
||||
|
||||
expect(getTodayIncomeValue(day2, firstDay)).toBe(0);
|
||||
expect(getTodayIncomeValue(day2, secondDay)).toBe(4);
|
||||
expect(getTodayIncomeDelta(day2, firstDay)).toBe(0);
|
||||
expect(getTodayIncomeDelta(day2, secondDay)).toBe(0);
|
||||
});
|
||||
|
||||
it('falls back to today income value only when parsed rewards are missing', () => {
|
||||
const day = formatLocalDate(new Date('2026-02-25T08:00:00.000Z'));
|
||||
const extraConfig = updateTodayIncomeSnapshot(
|
||||
updateTodayIncomeSnapshot(null, 8, new Date('2026-02-25T08:00:00.000Z')),
|
||||
10.2,
|
||||
new Date('2026-02-25T12:00:00.000Z'),
|
||||
);
|
||||
|
||||
const fallbackReward = estimateRewardWithTodayIncomeFallback({
|
||||
day,
|
||||
successCount: 1,
|
||||
parsedRewardCount: 0,
|
||||
rewardSum: 0,
|
||||
extraConfig,
|
||||
});
|
||||
expect(fallbackReward).toBeCloseTo(10.2, 6);
|
||||
|
||||
const preferParsed = estimateRewardWithTodayIncomeFallback({
|
||||
day,
|
||||
successCount: 1,
|
||||
parsedRewardCount: 1,
|
||||
rewardSum: 1.5,
|
||||
extraConfig,
|
||||
});
|
||||
expect(preferParsed).toBe(1.5);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,116 @@
|
||||
import { mergeAccountExtraConfig } from './accountExtraConfig.js';
|
||||
import { formatLocalDate } from './localTimeService.js';
|
||||
|
||||
type TodayIncomeSnapshot = {
|
||||
day: string;
|
||||
baseline?: number;
|
||||
latest: number;
|
||||
updatedAt?: string;
|
||||
};
|
||||
|
||||
type EstimateRewardInput = {
|
||||
day: string;
|
||||
successCount: number;
|
||||
parsedRewardCount: number;
|
||||
rewardSum: number;
|
||||
extraConfig?: string | null;
|
||||
};
|
||||
|
||||
function parseObject(value: string | null | undefined): Record<string, unknown> {
|
||||
if (!value) return {};
|
||||
try {
|
||||
const parsed = JSON.parse(value);
|
||||
if (!parsed || typeof parsed !== 'object' || Array.isArray(parsed)) return {};
|
||||
return parsed as Record<string, unknown>;
|
||||
} catch {
|
||||
return {};
|
||||
}
|
||||
}
|
||||
|
||||
function toNonNegativeNumber(value: unknown): number | null {
|
||||
if (typeof value !== 'number' || !Number.isFinite(value)) return null;
|
||||
if (value < 0) return null;
|
||||
return value;
|
||||
}
|
||||
|
||||
function normalizeSnapshot(raw: unknown): TodayIncomeSnapshot | null {
|
||||
if (!raw || typeof raw !== 'object' || Array.isArray(raw)) return null;
|
||||
const record = raw as Record<string, unknown>;
|
||||
const day = typeof record.day === 'string' ? record.day.trim() : '';
|
||||
if (!day) return null;
|
||||
|
||||
const latest = toNonNegativeNumber(record.latest);
|
||||
if (latest == null) return null;
|
||||
|
||||
const baseline = toNonNegativeNumber(record.baseline);
|
||||
|
||||
return {
|
||||
day,
|
||||
baseline: baseline ?? latest,
|
||||
latest,
|
||||
updatedAt: typeof record.updatedAt === 'string' ? record.updatedAt.trim() : undefined,
|
||||
};
|
||||
}
|
||||
|
||||
function extractTodayIncomeSnapshot(extraConfig?: string | null): TodayIncomeSnapshot | null {
|
||||
const parsed = parseObject(extraConfig);
|
||||
return normalizeSnapshot(parsed.todayIncomeSnapshot);
|
||||
}
|
||||
|
||||
export function getTodayIncomeDelta(extraConfig: string | null | undefined, day: string): number {
|
||||
if (!day) return 0;
|
||||
const snapshot = extractTodayIncomeSnapshot(extraConfig);
|
||||
if (!snapshot || snapshot.day !== day) return 0;
|
||||
|
||||
const baseline = typeof snapshot.baseline === 'number' ? snapshot.baseline : snapshot.latest;
|
||||
const delta = snapshot.latest - baseline;
|
||||
if (!Number.isFinite(delta) || delta <= 0) return 0;
|
||||
return delta;
|
||||
}
|
||||
|
||||
export function getTodayIncomeValue(extraConfig: string | null | undefined, day: string): number {
|
||||
if (!day) return 0;
|
||||
const snapshot = extractTodayIncomeSnapshot(extraConfig);
|
||||
if (!snapshot || snapshot.day !== day) return 0;
|
||||
return snapshot.latest;
|
||||
}
|
||||
|
||||
export function updateTodayIncomeSnapshot(
|
||||
extraConfig: string | null | undefined,
|
||||
todayIncome: number,
|
||||
now = new Date(),
|
||||
): string {
|
||||
const income = toNonNegativeNumber(todayIncome);
|
||||
if (income == null) return extraConfig || '{}';
|
||||
|
||||
const day = formatLocalDate(now);
|
||||
const existing = extractTodayIncomeSnapshot(extraConfig);
|
||||
let baseline = income;
|
||||
let latest = income;
|
||||
|
||||
if (existing && existing.day === day) {
|
||||
baseline = typeof existing.baseline === 'number' ? existing.baseline : existing.latest;
|
||||
if (income < baseline) baseline = income;
|
||||
latest = income;
|
||||
}
|
||||
|
||||
return mergeAccountExtraConfig(extraConfig, {
|
||||
todayIncomeSnapshot: {
|
||||
day,
|
||||
baseline,
|
||||
latest,
|
||||
updatedAt: now.toISOString(),
|
||||
},
|
||||
});
|
||||
}
|
||||
|
||||
export function estimateRewardWithTodayIncomeFallback(input: EstimateRewardInput): number {
|
||||
const rewardSum = Number.isFinite(input.rewardSum) && input.rewardSum > 0 ? input.rewardSum : 0;
|
||||
if (!Number.isFinite(input.successCount) || input.successCount <= 0) return rewardSum;
|
||||
|
||||
const hasMissingReward = input.parsedRewardCount < input.successCount;
|
||||
if (!hasMissingReward && rewardSum > 0) return rewardSum;
|
||||
|
||||
const incomeValue = getTodayIncomeValue(input.extraConfig, input.day);
|
||||
return incomeValue > rewardSum ? incomeValue : rewardSum;
|
||||
}
|
||||
@@ -0,0 +1,397 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it, vi } from 'vitest';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
|
||||
type DbModule = typeof import('../db/index.js');
|
||||
type TokenRouterModule = typeof import('./tokenRouter.js');
|
||||
type ConfigModule = typeof import('../config.js');
|
||||
|
||||
const mockedCatalogRoutingCost = vi.fn<(
|
||||
input: { siteId: number; accountId: number; modelName: string }
|
||||
) => number | null>(() => null);
|
||||
|
||||
vi.mock('./modelPricingService.js', async () => {
|
||||
const actual = await vi.importActual<typeof import('./modelPricingService.js')>('./modelPricingService.js');
|
||||
return {
|
||||
...actual,
|
||||
getCachedModelRoutingReferenceCost: mockedCatalogRoutingCost,
|
||||
};
|
||||
});
|
||||
|
||||
describe('TokenRouter selection scoring', () => {
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let TokenRouter: TokenRouterModule['TokenRouter'];
|
||||
let config: ConfigModule['config'];
|
||||
let dataDir = '';
|
||||
let idSeed = 0;
|
||||
let originalRoutingWeights: typeof config.routingWeights;
|
||||
let originalRoutingFallbackUnitCost: number;
|
||||
|
||||
const nextId = () => {
|
||||
idSeed += 1;
|
||||
return idSeed;
|
||||
};
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-token-router-selection-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../db/migrate.js');
|
||||
const dbModule = await import('../db/index.js');
|
||||
const tokenRouterModule = await import('./tokenRouter.js');
|
||||
const configModule = await import('../config.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
TokenRouter = tokenRouterModule.TokenRouter;
|
||||
config = configModule.config;
|
||||
originalRoutingWeights = { ...config.routingWeights };
|
||||
originalRoutingFallbackUnitCost = config.routingFallbackUnitCost;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
idSeed = 0;
|
||||
mockedCatalogRoutingCost.mockReset();
|
||||
mockedCatalogRoutingCost.mockReturnValue(null);
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
config.routingWeights = { ...originalRoutingWeights };
|
||||
config.routingFallbackUnitCost = originalRoutingFallbackUnitCost;
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
function createRoute(modelPattern: string) {
|
||||
return db.insert(schema.tokenRoutes).values({
|
||||
modelPattern,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
}
|
||||
|
||||
function createSite(namePrefix: string) {
|
||||
const id = nextId();
|
||||
return db.insert(schema.sites).values({
|
||||
name: `${namePrefix}-${id}`,
|
||||
url: `https://${namePrefix}-${id}.example.com`,
|
||||
platform: 'new-api',
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
}
|
||||
|
||||
function createAccount(siteId: number, usernamePrefix: string) {
|
||||
const id = nextId();
|
||||
return db.insert(schema.accounts).values({
|
||||
siteId,
|
||||
username: `${usernamePrefix}-${id}`,
|
||||
accessToken: `access-${id}`,
|
||||
apiToken: `sk-${id}`,
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
}
|
||||
|
||||
function createToken(accountId: number, name: string) {
|
||||
return db.insert(schema.accountTokens).values({
|
||||
accountId,
|
||||
name,
|
||||
token: `token-${name}-${nextId()}`,
|
||||
enabled: true,
|
||||
isDefault: false,
|
||||
}).returning().get();
|
||||
}
|
||||
|
||||
it('normalizes probability across channels on the same site', () => {
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: 1,
|
||||
valueScoreFactor: 0,
|
||||
costWeight: 0,
|
||||
balanceWeight: 0,
|
||||
usageWeight: 0,
|
||||
};
|
||||
|
||||
const route = createRoute('claude-haiku-4-5-20251001');
|
||||
|
||||
const siteA = createSite('site-a');
|
||||
const accountA = createAccount(siteA.id, 'user-a');
|
||||
const tokenA1 = createToken(accountA.id, 'a-1');
|
||||
const tokenA2 = createToken(accountA.id, 'a-2');
|
||||
|
||||
const siteB = createSite('site-b');
|
||||
const accountB = createAccount(siteB.id, 'user-b');
|
||||
const tokenB = createToken(accountB.id, 'b-1');
|
||||
|
||||
const channelA1 = db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountA.id,
|
||||
tokenId: tokenA1.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
const channelA2 = db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountA.id,
|
||||
tokenId: tokenA2.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
const channelB = db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountB.id,
|
||||
tokenId: tokenB.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
const decision = new TokenRouter().explainSelection('claude-haiku-4-5-20251001');
|
||||
const probMap = new Map(decision.candidates.map((candidate) => [candidate.channelId, candidate.probability]));
|
||||
|
||||
const probA1 = probMap.get(channelA1.id) ?? 0;
|
||||
const probA2 = probMap.get(channelA2.id) ?? 0;
|
||||
const probB = probMap.get(channelB.id) ?? 0;
|
||||
|
||||
expect(probA1).toBeCloseTo(25, 1);
|
||||
expect(probA2).toBeCloseTo(25, 1);
|
||||
expect(probB).toBeCloseTo(50, 1);
|
||||
expect(probA1 + probA2).toBeCloseTo(probB, 1);
|
||||
});
|
||||
|
||||
it('uses observed channel cost from real routing results when scoring cost priority', () => {
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: 0.35,
|
||||
valueScoreFactor: 0.65,
|
||||
costWeight: 1,
|
||||
balanceWeight: 0,
|
||||
usageWeight: 0,
|
||||
};
|
||||
|
||||
const route = createRoute('claude-opus-4-6');
|
||||
|
||||
const siteCheap = createSite('cheap-site');
|
||||
const accountCheap = createAccount(siteCheap.id, 'cheap-user');
|
||||
const tokenCheap = createToken(accountCheap.id, 'cheap-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountCheap.id,
|
||||
tokenId: tokenCheap.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 10,
|
||||
failCount: 0,
|
||||
totalCost: 0.01,
|
||||
}).run();
|
||||
|
||||
const siteExpensive = createSite('expensive-site');
|
||||
const accountExpensive = createAccount(siteExpensive.id, 'expensive-user');
|
||||
const tokenExpensive = createToken(accountExpensive.id, 'exp-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountExpensive.id,
|
||||
tokenId: tokenExpensive.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 10,
|
||||
failCount: 0,
|
||||
totalCost: 0.1,
|
||||
}).run();
|
||||
|
||||
const decision = new TokenRouter().explainSelection('claude-opus-4-6');
|
||||
const cheapCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('cheap-site'));
|
||||
const expensiveCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('expensive-site'));
|
||||
|
||||
expect(cheapCandidate).toBeTruthy();
|
||||
expect(expensiveCandidate).toBeTruthy();
|
||||
expect((cheapCandidate?.probability || 0)).toBeGreaterThan(expensiveCandidate?.probability || 0);
|
||||
expect(cheapCandidate?.reason || '').toContain('成本=实测');
|
||||
expect(expensiveCandidate?.reason || '').toContain('成本=实测');
|
||||
});
|
||||
|
||||
it('uses runtime-configured fallback unit cost when observed and configured costs are missing', () => {
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: 0.35,
|
||||
valueScoreFactor: 0.65,
|
||||
costWeight: 1,
|
||||
balanceWeight: 0,
|
||||
usageWeight: 0,
|
||||
};
|
||||
config.routingFallbackUnitCost = 0.02;
|
||||
|
||||
const route = createRoute('claude-sonnet-4-6');
|
||||
|
||||
const siteFallback = createSite('fallback-site');
|
||||
const accountFallback = createAccount(siteFallback.id, 'fallback-user');
|
||||
const tokenFallback = createToken(accountFallback.id, 'fallback-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountFallback.id,
|
||||
tokenId: tokenFallback.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 0,
|
||||
failCount: 0,
|
||||
totalCost: 0,
|
||||
}).run();
|
||||
|
||||
const siteObserved = createSite('observed-site');
|
||||
const accountObserved = createAccount(siteObserved.id, 'observed-user');
|
||||
const tokenObserved = createToken(accountObserved.id, 'observed-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountObserved.id,
|
||||
tokenId: tokenObserved.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 10,
|
||||
failCount: 0,
|
||||
totalCost: 2, // unit cost 0.2
|
||||
}).run();
|
||||
|
||||
const decision = new TokenRouter().explainSelection('claude-sonnet-4-6');
|
||||
const fallbackCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('fallback-site'));
|
||||
const observedCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('observed-site'));
|
||||
|
||||
expect(fallbackCandidate).toBeTruthy();
|
||||
expect(observedCandidate).toBeTruthy();
|
||||
expect((fallbackCandidate?.probability || 0)).toBeGreaterThan(observedCandidate?.probability || 0);
|
||||
expect(fallbackCandidate?.reason || '').toContain('成本=默认:0.020000');
|
||||
});
|
||||
|
||||
it('penalizes fallback-cost channels when fallback unit cost is set very high', () => {
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: 0.35,
|
||||
valueScoreFactor: 0.65,
|
||||
costWeight: 0.75,
|
||||
balanceWeight: 0.15,
|
||||
usageWeight: 0.1,
|
||||
};
|
||||
config.routingFallbackUnitCost = 1000;
|
||||
|
||||
const route = createRoute('gpt-5-nano');
|
||||
|
||||
const siteFallback = createSite('fallback-high-balance');
|
||||
const accountFallback = db.insert(schema.accounts).values({
|
||||
siteId: siteFallback.id,
|
||||
username: `fallback-high-balance-${nextId()}`,
|
||||
accessToken: `access-${nextId()}`,
|
||||
apiToken: `sk-${nextId()}`,
|
||||
status: 'active',
|
||||
balance: 10_000,
|
||||
}).returning().get();
|
||||
const tokenFallback = createToken(accountFallback.id, 'fallback-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountFallback.id,
|
||||
tokenId: tokenFallback.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 0,
|
||||
failCount: 0,
|
||||
totalCost: 0,
|
||||
}).run();
|
||||
|
||||
const siteObserved = createSite('observed-low-balance');
|
||||
const accountObserved = db.insert(schema.accounts).values({
|
||||
siteId: siteObserved.id,
|
||||
username: `observed-low-balance-${nextId()}`,
|
||||
accessToken: `access-${nextId()}`,
|
||||
apiToken: `sk-${nextId()}`,
|
||||
status: 'active',
|
||||
balance: 0,
|
||||
}).returning().get();
|
||||
const tokenObserved = createToken(accountObserved.id, 'observed-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountObserved.id,
|
||||
tokenId: tokenObserved.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 10,
|
||||
failCount: 0,
|
||||
totalCost: 10, // observed unit cost = 1
|
||||
}).run();
|
||||
|
||||
const decision = new TokenRouter().explainSelection('gpt-5-nano');
|
||||
const fallbackCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('fallback-high-balance'));
|
||||
const observedCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('observed-low-balance'));
|
||||
|
||||
expect(fallbackCandidate).toBeTruthy();
|
||||
expect(observedCandidate).toBeTruthy();
|
||||
expect((fallbackCandidate?.probability || 0)).toBeLessThan(1);
|
||||
expect((observedCandidate?.probability || 0)).toBeGreaterThan(99);
|
||||
expect(fallbackCandidate?.reason || '').toContain('成本=默认:1000.000000');
|
||||
});
|
||||
|
||||
it('uses cached catalog routing cost when observed and configured costs are missing', () => {
|
||||
config.routingWeights = {
|
||||
baseWeightFactor: 0.35,
|
||||
valueScoreFactor: 0.65,
|
||||
costWeight: 1,
|
||||
balanceWeight: 0,
|
||||
usageWeight: 0,
|
||||
};
|
||||
config.routingFallbackUnitCost = 100;
|
||||
|
||||
const route = createRoute('claude-sonnet-4-5-20250929');
|
||||
|
||||
const siteCatalog = createSite('catalog-site');
|
||||
const accountCatalog = createAccount(siteCatalog.id, 'catalog-user');
|
||||
const tokenCatalog = createToken(accountCatalog.id, 'catalog-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountCatalog.id,
|
||||
tokenId: tokenCatalog.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 0,
|
||||
failCount: 0,
|
||||
totalCost: 0,
|
||||
}).run();
|
||||
|
||||
const siteFallback = createSite('fallback-site');
|
||||
const accountFallback = createAccount(siteFallback.id, 'fallback-user');
|
||||
const tokenFallback = createToken(accountFallback.id, 'fallback-token');
|
||||
db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: accountFallback.id,
|
||||
tokenId: tokenFallback.id,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
successCount: 0,
|
||||
failCount: 0,
|
||||
totalCost: 0,
|
||||
}).run();
|
||||
|
||||
mockedCatalogRoutingCost.mockImplementation(({ accountId, modelName }) => {
|
||||
if (accountId !== accountCatalog.id) return null;
|
||||
if (modelName !== 'claude-sonnet-4-5-20250929') return null;
|
||||
return 0.2;
|
||||
});
|
||||
|
||||
const decision = new TokenRouter().explainSelection('claude-sonnet-4-5-20250929');
|
||||
const catalogCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('catalog-site'));
|
||||
const fallbackCandidate = decision.candidates.find((candidate) => candidate.siteName.startsWith('fallback-site'));
|
||||
|
||||
expect(catalogCandidate).toBeTruthy();
|
||||
expect(fallbackCandidate).toBeTruthy();
|
||||
expect((catalogCandidate?.probability || 0)).toBeGreaterThan(fallbackCandidate?.probability || 0);
|
||||
expect(catalogCandidate?.reason || '').toContain('成本=目录:0.200000');
|
||||
expect(fallbackCandidate?.reason || '').toContain('成本=默认:100.000000');
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,82 @@
|
||||
import { afterAll, beforeAll, beforeEach, describe, expect, it } from 'vitest';
|
||||
import { mkdtempSync } from 'node:fs';
|
||||
import { tmpdir } from 'node:os';
|
||||
import { join } from 'node:path';
|
||||
import { sql } from 'drizzle-orm';
|
||||
|
||||
type DbModule = typeof import('../db/index.js');
|
||||
type TokenRouterModule = typeof import('./tokenRouter.js');
|
||||
|
||||
describe('TokenRouter site status guard', () => {
|
||||
let db: DbModule['db'];
|
||||
let schema: DbModule['schema'];
|
||||
let TokenRouter: TokenRouterModule['TokenRouter'];
|
||||
let dataDir = '';
|
||||
|
||||
beforeAll(async () => {
|
||||
dataDir = mkdtempSync(join(tmpdir(), 'metapi-token-router-site-status-'));
|
||||
process.env.DATA_DIR = dataDir;
|
||||
|
||||
await import('../db/migrate.js');
|
||||
const dbModule = await import('../db/index.js');
|
||||
const tokenRouterModule = await import('./tokenRouter.js');
|
||||
db = dbModule.db;
|
||||
schema = dbModule.schema;
|
||||
TokenRouter = tokenRouterModule.TokenRouter;
|
||||
});
|
||||
|
||||
beforeEach(() => {
|
||||
db.delete(schema.routeChannels).run();
|
||||
db.delete(schema.tokenRoutes).run();
|
||||
db.delete(schema.tokenModelAvailability).run();
|
||||
db.delete(schema.modelAvailability).run();
|
||||
db.delete(schema.accountTokens).run();
|
||||
db.delete(schema.accounts).run();
|
||||
db.delete(schema.sites).run();
|
||||
});
|
||||
|
||||
afterAll(() => {
|
||||
delete process.env.DATA_DIR;
|
||||
});
|
||||
|
||||
it('does not select channels from disabled sites', () => {
|
||||
const site = db.insert(schema.sites).values({
|
||||
name: 'disabled-site',
|
||||
url: 'https://disabled.example.com',
|
||||
platform: 'new-api',
|
||||
}).returning().get();
|
||||
db.run(sql`update sites set status = 'disabled' where id = ${site.id}`);
|
||||
|
||||
const account = db.insert(schema.accounts).values({
|
||||
siteId: site.id,
|
||||
username: 'disabled-user',
|
||||
accessToken: 'access-disabled',
|
||||
apiToken: 'sk-disabled',
|
||||
status: 'active',
|
||||
}).returning().get();
|
||||
|
||||
const route = db.insert(schema.tokenRoutes).values({
|
||||
modelPattern: 'gpt-4o-mini',
|
||||
enabled: true,
|
||||
}).returning().get();
|
||||
|
||||
const channel = db.insert(schema.routeChannels).values({
|
||||
routeId: route.id,
|
||||
accountId: account.id,
|
||||
tokenId: null,
|
||||
priority: 0,
|
||||
weight: 10,
|
||||
enabled: true,
|
||||
manualOverride: false,
|
||||
}).returning().get();
|
||||
|
||||
const router = new TokenRouter();
|
||||
const selected = router.selectChannel('gpt-4o-mini');
|
||||
expect(selected).toBeNull();
|
||||
|
||||
const decision = router.explainSelection('gpt-4o-mini');
|
||||
expect(decision.matched).toBe(true);
|
||||
const candidate = decision.candidates.find((item) => item.channelId === channel.id);
|
||||
expect(candidate?.eligible).toBe(false);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,81 @@
|
||||
import { describe, expect, it } from 'vitest';
|
||||
import { filterRecentlyFailedCandidates } from './tokenRouter.js';
|
||||
|
||||
type Candidate = {
|
||||
channel: {
|
||||
failCount?: number | null;
|
||||
lastFailAt?: string | null;
|
||||
};
|
||||
id: string;
|
||||
};
|
||||
|
||||
describe('filterRecentlyFailedCandidates', () => {
|
||||
it('prefers healthy channels when at least one healthy channel exists', () => {
|
||||
const nowMs = Date.now();
|
||||
const candidates: Candidate[] = [
|
||||
{
|
||||
id: 'failed',
|
||||
channel: {
|
||||
failCount: 2,
|
||||
lastFailAt: new Date(nowMs - 30 * 1000).toISOString(),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'healthy',
|
||||
channel: {
|
||||
failCount: 0,
|
||||
lastFailAt: null,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = filterRecentlyFailedCandidates(candidates, nowMs, 600);
|
||||
expect(result.map((c) => c.id)).toEqual(['healthy']);
|
||||
});
|
||||
|
||||
it('keeps all channels when all channels failed recently', () => {
|
||||
const nowMs = Date.now();
|
||||
const candidates: Candidate[] = [
|
||||
{
|
||||
id: 'a',
|
||||
channel: {
|
||||
failCount: 1,
|
||||
lastFailAt: new Date(nowMs - 20 * 1000).toISOString(),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'b',
|
||||
channel: {
|
||||
failCount: 3,
|
||||
lastFailAt: new Date(nowMs - 40 * 1000).toISOString(),
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = filterRecentlyFailedCandidates(candidates, nowMs, 600);
|
||||
expect(result.map((c) => c.id).sort()).toEqual(['a', 'b']);
|
||||
});
|
||||
|
||||
it('does not penalize stale failures outside the avoidance window', () => {
|
||||
const nowMs = Date.now();
|
||||
const candidates: Candidate[] = [
|
||||
{
|
||||
id: 'stale-failure',
|
||||
channel: {
|
||||
failCount: 5,
|
||||
lastFailAt: new Date(nowMs - 20 * 60 * 1000).toISOString(),
|
||||
},
|
||||
},
|
||||
{
|
||||
id: 'healthy',
|
||||
channel: {
|
||||
failCount: 0,
|
||||
lastFailAt: null,
|
||||
},
|
||||
},
|
||||
];
|
||||
|
||||
const result = filterRecentlyFailedCandidates(candidates, nowMs, 600);
|
||||
expect(result.map((c) => c.id).sort()).toEqual(['healthy', 'stale-failure']);
|
||||
});
|
||||
});
|
||||
@@ -0,0 +1,603 @@
|
||||
import { eq } from 'drizzle-orm';
|
||||
import { minimatch } from 'minimatch';
|
||||
import { db, schema } from '../db/index.js';
|
||||
import { config } from '../config.js';
|
||||
import { getCachedModelRoutingReferenceCost } from './modelPricingService.js';
|
||||
|
||||
interface RouteMatch {
|
||||
route: typeof schema.tokenRoutes.$inferSelect;
|
||||
channels: Array<{
|
||||
channel: typeof schema.routeChannels.$inferSelect;
|
||||
account: typeof schema.accounts.$inferSelect;
|
||||
site: typeof schema.sites.$inferSelect;
|
||||
token: typeof schema.accountTokens.$inferSelect | null;
|
||||
}>;
|
||||
}
|
||||
|
||||
type RouteChannelCandidate = RouteMatch['channels'][number];
|
||||
|
||||
interface SelectedChannel {
|
||||
channel: typeof schema.routeChannels.$inferSelect;
|
||||
account: typeof schema.accounts.$inferSelect;
|
||||
site: typeof schema.sites.$inferSelect;
|
||||
token: typeof schema.accountTokens.$inferSelect | null;
|
||||
tokenValue: string;
|
||||
tokenName: string;
|
||||
actualModel: string;
|
||||
}
|
||||
|
||||
type FailureAwareChannel = {
|
||||
failCount?: number | null;
|
||||
lastFailAt?: string | null;
|
||||
};
|
||||
|
||||
const RECENT_FAILURE_AVOID_SEC = 10 * 60;
|
||||
const MIN_EFFECTIVE_UNIT_COST = 1e-6;
|
||||
|
||||
function isSiteDisabled(status?: string | null): boolean {
|
||||
return (status || 'active') === 'disabled';
|
||||
}
|
||||
|
||||
export function isChannelRecentlyFailed(
|
||||
channel: FailureAwareChannel,
|
||||
nowMs = Date.now(),
|
||||
avoidSec = RECENT_FAILURE_AVOID_SEC,
|
||||
): boolean {
|
||||
if (avoidSec <= 0) return false;
|
||||
if ((channel.failCount ?? 0) <= 0) return false;
|
||||
if (!channel.lastFailAt) return false;
|
||||
|
||||
const failTs = Date.parse(channel.lastFailAt);
|
||||
if (Number.isNaN(failTs)) return false;
|
||||
|
||||
return nowMs - failTs < avoidSec * 1000;
|
||||
}
|
||||
|
||||
export function filterRecentlyFailedCandidates<T extends { channel: FailureAwareChannel }>(
|
||||
candidates: T[],
|
||||
nowMs = Date.now(),
|
||||
avoidSec = RECENT_FAILURE_AVOID_SEC,
|
||||
): T[] {
|
||||
if (candidates.length <= 1) return candidates;
|
||||
if (avoidSec <= 0) return candidates;
|
||||
|
||||
const healthy = candidates.filter((candidate) => !isChannelRecentlyFailed(candidate.channel, nowMs, avoidSec));
|
||||
// If all channels failed recently, keep them all and let weight/random decide.
|
||||
return healthy.length > 0 ? healthy : candidates;
|
||||
}
|
||||
|
||||
export interface RouteDecisionCandidate {
|
||||
channelId: number;
|
||||
accountId: number;
|
||||
username: string;
|
||||
siteName: string;
|
||||
tokenName: string;
|
||||
priority: number;
|
||||
weight: number;
|
||||
eligible: boolean;
|
||||
recentlyFailed: boolean;
|
||||
avoidedByRecentFailure: boolean;
|
||||
probability: number;
|
||||
reason: string;
|
||||
}
|
||||
|
||||
export interface RouteDecisionExplanation {
|
||||
requestedModel: string;
|
||||
actualModel: string;
|
||||
matched: boolean;
|
||||
routeId?: number;
|
||||
modelPattern?: string;
|
||||
selectedChannelId?: number;
|
||||
selectedAccountId?: number;
|
||||
selectedLabel?: string;
|
||||
summary: string[];
|
||||
candidates: RouteDecisionCandidate[];
|
||||
}
|
||||
|
||||
type CostSignal = {
|
||||
unitCost: number;
|
||||
source: 'observed' | 'configured' | 'catalog' | 'fallback';
|
||||
};
|
||||
|
||||
function resolveEffectiveUnitCost(candidate: RouteChannelCandidate, modelName: string): CostSignal {
|
||||
const successCount = Math.max(0, candidate.channel.successCount ?? 0);
|
||||
const totalCost = Math.max(0, candidate.channel.totalCost ?? 0);
|
||||
const configured = candidate.account.unitCost ?? null;
|
||||
|
||||
if (successCount > 0 && totalCost > 0) {
|
||||
return {
|
||||
unitCost: Math.max(totalCost / successCount, MIN_EFFECTIVE_UNIT_COST),
|
||||
source: 'observed',
|
||||
};
|
||||
}
|
||||
|
||||
if (typeof configured === 'number' && Number.isFinite(configured) && configured > 0) {
|
||||
return {
|
||||
unitCost: Math.max(configured, MIN_EFFECTIVE_UNIT_COST),
|
||||
source: 'configured',
|
||||
};
|
||||
}
|
||||
|
||||
const catalogCost = getCachedModelRoutingReferenceCost({
|
||||
siteId: candidate.site.id,
|
||||
accountId: candidate.account.id,
|
||||
modelName,
|
||||
});
|
||||
if (typeof catalogCost === 'number' && Number.isFinite(catalogCost) && catalogCost > 0) {
|
||||
return {
|
||||
unitCost: Math.max(catalogCost, MIN_EFFECTIVE_UNIT_COST),
|
||||
source: 'catalog',
|
||||
};
|
||||
}
|
||||
|
||||
return {
|
||||
unitCost: Math.max(config.routingFallbackUnitCost || 1, MIN_EFFECTIVE_UNIT_COST),
|
||||
source: 'fallback',
|
||||
};
|
||||
}
|
||||
|
||||
export class TokenRouter {
|
||||
/**
|
||||
* Find matching route and select a channel for the given model.
|
||||
* Returns null if no route/channel available.
|
||||
*/
|
||||
selectChannel(requestedModel: string): SelectedChannel | null {
|
||||
const match = this.findRoute(requestedModel);
|
||||
if (!match) return null;
|
||||
|
||||
// Apply model mapping if configured
|
||||
let actualModel = requestedModel;
|
||||
if (match.route.modelMapping) {
|
||||
try {
|
||||
const mapping = JSON.parse(match.route.modelMapping);
|
||||
if (mapping[requestedModel]) actualModel = mapping[requestedModel];
|
||||
} catch {}
|
||||
}
|
||||
|
||||
// Filter channels: enabled, not in cooldown, account/site active with token
|
||||
const nowIso = new Date().toISOString();
|
||||
const nowMs = Date.now();
|
||||
const available = match.channels.filter((c) =>
|
||||
c.channel.enabled &&
|
||||
c.account.status === 'active' &&
|
||||
!isSiteDisabled(c.site.status) &&
|
||||
!!this.resolveChannelTokenValue(c) &&
|
||||
(!c.channel.cooldownUntil || c.channel.cooldownUntil <= nowIso),
|
||||
);
|
||||
|
||||
if (available.length === 0) return null;
|
||||
|
||||
// Group by priority
|
||||
const layers = new Map<number, typeof available>();
|
||||
for (const c of available) {
|
||||
const p = c.channel.priority ?? 0;
|
||||
if (!layers.has(p)) layers.set(p, []);
|
||||
layers.get(p)!.push(c);
|
||||
}
|
||||
|
||||
// Sort layers by priority (ascending = higher priority first)
|
||||
const sortedPriorities = Array.from(layers.keys()).sort((a, b) => a - b);
|
||||
|
||||
// Try each priority layer
|
||||
for (const priority of sortedPriorities) {
|
||||
const candidates = filterRecentlyFailedCandidates(
|
||||
layers.get(priority)!,
|
||||
nowMs,
|
||||
RECENT_FAILURE_AVOID_SEC,
|
||||
);
|
||||
const selected = this.weightedRandomSelect(candidates, actualModel);
|
||||
if (selected) {
|
||||
const tokenValue = this.resolveChannelTokenValue(selected);
|
||||
if (!tokenValue) continue;
|
||||
|
||||
return {
|
||||
...selected,
|
||||
tokenValue,
|
||||
tokenName: selected.token?.name || 'default',
|
||||
actualModel,
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
/**
|
||||
* Select next channel for failover (exclude already-tried channels).
|
||||
*/
|
||||
selectNextChannel(requestedModel: string, excludeChannelIds: number[]): SelectedChannel | null {
|
||||
const match = this.findRoute(requestedModel);
|
||||
if (!match) return null;
|
||||
|
||||
let actualModel = requestedModel;
|
||||
if (match.route.modelMapping) {
|
||||
try {
|
||||
const mapping = JSON.parse(match.route.modelMapping);
|
||||
if (mapping[requestedModel]) actualModel = mapping[requestedModel];
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const nowIso = new Date().toISOString();
|
||||
const nowMs = Date.now();
|
||||
const available = match.channels.filter((c) =>
|
||||
c.channel.enabled &&
|
||||
c.account.status === 'active' &&
|
||||
!isSiteDisabled(c.site.status) &&
|
||||
!!this.resolveChannelTokenValue(c) &&
|
||||
(!c.channel.cooldownUntil || c.channel.cooldownUntil <= nowIso) &&
|
||||
!excludeChannelIds.includes(c.channel.id),
|
||||
);
|
||||
|
||||
if (available.length === 0) return null;
|
||||
|
||||
const layers = new Map<number, typeof available>();
|
||||
for (const c of available) {
|
||||
const p = c.channel.priority ?? 0;
|
||||
if (!layers.has(p)) layers.set(p, []);
|
||||
layers.get(p)!.push(c);
|
||||
}
|
||||
|
||||
const sortedPriorities = Array.from(layers.keys()).sort((a, b) => a - b);
|
||||
for (const priority of sortedPriorities) {
|
||||
const candidates = filterRecentlyFailedCandidates(
|
||||
layers.get(priority)!,
|
||||
nowMs,
|
||||
RECENT_FAILURE_AVOID_SEC,
|
||||
);
|
||||
const selected = this.weightedRandomSelect(candidates, actualModel);
|
||||
if (!selected) continue;
|
||||
|
||||
const tokenValue = this.resolveChannelTokenValue(selected);
|
||||
if (!tokenValue) continue;
|
||||
|
||||
return {
|
||||
...selected,
|
||||
tokenValue,
|
||||
tokenName: selected.token?.name || 'default',
|
||||
actualModel,
|
||||
};
|
||||
}
|
||||
|
||||
return null;
|
||||
}
|
||||
|
||||
explainSelection(requestedModel: string, excludeChannelIds: number[] = []): RouteDecisionExplanation {
|
||||
const match = this.findRoute(requestedModel);
|
||||
if (!match) {
|
||||
return {
|
||||
requestedModel,
|
||||
actualModel: requestedModel,
|
||||
matched: false,
|
||||
summary: ['未匹配到启用的路由'],
|
||||
candidates: [],
|
||||
};
|
||||
}
|
||||
|
||||
let actualModel = requestedModel;
|
||||
if (match.route.modelMapping) {
|
||||
try {
|
||||
const mapping = JSON.parse(match.route.modelMapping) as Record<string, string>;
|
||||
if (mapping[requestedModel]) actualModel = mapping[requestedModel];
|
||||
} catch {}
|
||||
}
|
||||
|
||||
const nowIso = new Date().toISOString();
|
||||
const nowMs = Date.now();
|
||||
const summary: string[] = [`命中路由:${match.route.modelPattern}`];
|
||||
const availableByPriority = new Map<number, RouteChannelCandidate[]>();
|
||||
const candidates: RouteDecisionCandidate[] = [];
|
||||
const candidateMap = new Map<number, RouteDecisionCandidate>();
|
||||
|
||||
for (const row of match.channels) {
|
||||
const reasonParts: string[] = [];
|
||||
if (!row.channel.enabled) reasonParts.push('通道禁用');
|
||||
if (row.account.status !== 'active') reasonParts.push(`账号状态=${row.account.status}`);
|
||||
if (isSiteDisabled(row.site.status)) reasonParts.push(`站点状态=${row.site.status || 'disabled'}`);
|
||||
if (excludeChannelIds.includes(row.channel.id)) reasonParts.push('当前请求已尝试');
|
||||
const tokenValue = this.resolveChannelTokenValue(row);
|
||||
if (!tokenValue) reasonParts.push('令牌不可用');
|
||||
if (row.channel.cooldownUntil && row.channel.cooldownUntil > nowIso) reasonParts.push('冷却中');
|
||||
|
||||
const recentlyFailed = isChannelRecentlyFailed(row.channel, nowMs, RECENT_FAILURE_AVOID_SEC);
|
||||
const eligible = reasonParts.length === 0;
|
||||
const candidate: RouteDecisionCandidate = {
|
||||
channelId: row.channel.id,
|
||||
accountId: row.account.id,
|
||||
username: row.account.username || `account-${row.account.id}`,
|
||||
siteName: row.site.name || 'unknown',
|
||||
tokenName: row.token?.name || 'default',
|
||||
priority: row.channel.priority ?? 0,
|
||||
weight: row.channel.weight ?? 10,
|
||||
eligible,
|
||||
recentlyFailed,
|
||||
avoidedByRecentFailure: false,
|
||||
probability: 0,
|
||||
reason: eligible ? '可用' : reasonParts.join('、'),
|
||||
};
|
||||
candidates.push(candidate);
|
||||
candidateMap.set(candidate.channelId, candidate);
|
||||
|
||||
if (eligible) {
|
||||
const priority = row.channel.priority ?? 0;
|
||||
if (!availableByPriority.has(priority)) availableByPriority.set(priority, []);
|
||||
availableByPriority.get(priority)!.push(row);
|
||||
}
|
||||
}
|
||||
|
||||
if (availableByPriority.size === 0) {
|
||||
summary.push('没有可用通道(全部被禁用、站点不可用、冷却或令牌不可用)');
|
||||
return {
|
||||
requestedModel,
|
||||
actualModel,
|
||||
matched: true,
|
||||
routeId: match.route.id,
|
||||
modelPattern: match.route.modelPattern,
|
||||
summary,
|
||||
candidates,
|
||||
};
|
||||
}
|
||||
|
||||
const sortedPriorities = Array.from(availableByPriority.keys()).sort((a, b) => a - b);
|
||||
let selected: RouteChannelCandidate | null = null;
|
||||
let selectedPriority = 0;
|
||||
|
||||
for (const priority of sortedPriorities) {
|
||||
const rawLayer = availableByPriority.get(priority) ?? [];
|
||||
if (rawLayer.length === 0) continue;
|
||||
|
||||
const filteredLayer = filterRecentlyFailedCandidates(rawLayer, nowMs, RECENT_FAILURE_AVOID_SEC);
|
||||
const avoided = rawLayer.filter((row) => !filteredLayer.some((item) => item.channel.id === row.channel.id));
|
||||
if (avoided.length > 0) {
|
||||
for (const row of avoided) {
|
||||
const target = candidateMap.get(row.channel.id);
|
||||
if (!target) continue;
|
||||
target.avoidedByRecentFailure = true;
|
||||
target.reason = `最近失败,优先避让(${RECENT_FAILURE_AVOID_SEC / 60} 分钟窗口)`;
|
||||
}
|
||||
}
|
||||
|
||||
const weighted = this.calculateWeightedSelection(filteredLayer, actualModel);
|
||||
for (const detail of weighted.details) {
|
||||
const target = candidateMap.get(detail.candidate.channel.id);
|
||||
if (!target) continue;
|
||||
target.probability = Number((detail.probability * 100).toFixed(2));
|
||||
if (target.eligible && !target.avoidedByRecentFailure) {
|
||||
target.reason = detail.reason;
|
||||
}
|
||||
}
|
||||
|
||||
if (!weighted.selected) continue;
|
||||
selected = weighted.selected;
|
||||
selectedPriority = priority;
|
||||
summary.push(
|
||||
avoided.length > 0
|
||||
? `优先级 P${priority}:可用 ${rawLayer.length},因最近失败避让 ${avoided.length}`
|
||||
: `优先级 P${priority}:可用 ${rawLayer.length}`,
|
||||
);
|
||||
break;
|
||||
}
|
||||
|
||||
if (!selected) {
|
||||
summary.push('本次未选出通道');
|
||||
return {
|
||||
requestedModel,
|
||||
actualModel,
|
||||
matched: true,
|
||||
routeId: match.route.id,
|
||||
modelPattern: match.route.modelPattern,
|
||||
summary,
|
||||
candidates,
|
||||
};
|
||||
}
|
||||
|
||||
const selectedChannel = candidateMap.get(selected.channel.id);
|
||||
const selectedLabel = selectedChannel
|
||||
? `${selectedChannel.username} @ ${selectedChannel.siteName} / ${selectedChannel.tokenName}`
|
||||
: `channel-${selected.channel.id}`;
|
||||
summary.push(`最终选择:${selectedLabel}(P${selectedPriority})`);
|
||||
|
||||
return {
|
||||
requestedModel,
|
||||
actualModel,
|
||||
matched: true,
|
||||
routeId: match.route.id,
|
||||
modelPattern: match.route.modelPattern,
|
||||
selectedChannelId: selected.channel.id,
|
||||
selectedAccountId: selected.account.id,
|
||||
selectedLabel,
|
||||
summary,
|
||||
candidates,
|
||||
};
|
||||
}
|
||||
|
||||
/**
|
||||
* Record success for a channel.
|
||||
*/
|
||||
recordSuccess(channelId: number, latencyMs: number, cost: number) {
|
||||
const ch = db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelId)).get();
|
||||
if (!ch) return;
|
||||
db.update(schema.routeChannels).set({
|
||||
successCount: (ch.successCount ?? 0) + 1,
|
||||
totalLatencyMs: (ch.totalLatencyMs ?? 0) + latencyMs,
|
||||
totalCost: (ch.totalCost ?? 0) + cost,
|
||||
lastUsedAt: new Date().toISOString(),
|
||||
cooldownUntil: null,
|
||||
lastFailAt: null,
|
||||
}).where(eq(schema.routeChannels.id, channelId)).run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Record failure and set cooldown.
|
||||
*/
|
||||
recordFailure(channelId: number) {
|
||||
const ch = db.select().from(schema.routeChannels).where(eq(schema.routeChannels.id, channelId)).get();
|
||||
if (!ch) return;
|
||||
const failCount = (ch.failCount ?? 0) + 1;
|
||||
// Exponential backoff cooldown: 30s, 60s, 120s, 240s, max 5min
|
||||
const cooldownSec = Math.min(30 * Math.pow(2, failCount - 1), 300);
|
||||
const cooldownUntil = new Date(Date.now() + cooldownSec * 1000).toISOString();
|
||||
db.update(schema.routeChannels).set({
|
||||
failCount,
|
||||
lastFailAt: new Date().toISOString(),
|
||||
cooldownUntil,
|
||||
}).where(eq(schema.routeChannels.id, channelId)).run();
|
||||
}
|
||||
|
||||
/**
|
||||
* Get all available models (aggregated from all routes).
|
||||
*/
|
||||
getAvailableModels(): string[] {
|
||||
const routes = db.select().from(schema.tokenRoutes)
|
||||
.where(eq(schema.tokenRoutes.enabled, true)).all();
|
||||
return routes.map((r) => r.modelPattern);
|
||||
}
|
||||
|
||||
// --- Private methods ---
|
||||
|
||||
private findRoute(model: string): RouteMatch | null {
|
||||
const routes = db.select().from(schema.tokenRoutes)
|
||||
.where(eq(schema.tokenRoutes.enabled, true)).all();
|
||||
|
||||
// Find matching route by pattern
|
||||
const matchedRoute = routes.find((r) => {
|
||||
if (r.modelPattern === model) return true; // exact match
|
||||
return minimatch(model, r.modelPattern); // glob match
|
||||
});
|
||||
|
||||
if (!matchedRoute) return null;
|
||||
|
||||
// Get channels with account, site and token info
|
||||
const channels = db
|
||||
.select()
|
||||
.from(schema.routeChannels)
|
||||
.innerJoin(schema.accounts, eq(schema.routeChannels.accountId, schema.accounts.id))
|
||||
.innerJoin(schema.sites, eq(schema.accounts.siteId, schema.sites.id))
|
||||
.leftJoin(schema.accountTokens, eq(schema.routeChannels.tokenId, schema.accountTokens.id))
|
||||
.where(eq(schema.routeChannels.routeId, matchedRoute.id))
|
||||
.all();
|
||||
|
||||
const mapped = channels.map((row) => ({
|
||||
channel: row.route_channels,
|
||||
account: row.accounts,
|
||||
site: row.sites,
|
||||
token: row.account_tokens,
|
||||
}));
|
||||
|
||||
return { route: matchedRoute, channels: mapped };
|
||||
}
|
||||
|
||||
private resolveChannelTokenValue(candidate: {
|
||||
channel: typeof schema.routeChannels.$inferSelect;
|
||||
account: typeof schema.accounts.$inferSelect;
|
||||
token: typeof schema.accountTokens.$inferSelect | null;
|
||||
}): string | null {
|
||||
if (candidate.channel.tokenId) {
|
||||
if (!candidate.token) return null;
|
||||
if (!candidate.token.enabled) return null;
|
||||
const token = candidate.token.token?.trim();
|
||||
return token ? token : null;
|
||||
}
|
||||
|
||||
const fallback = candidate.account.apiToken?.trim();
|
||||
return fallback ? fallback : null;
|
||||
}
|
||||
|
||||
private weightedRandomSelect(
|
||||
candidates: RouteChannelCandidate[],
|
||||
modelName: string,
|
||||
) {
|
||||
return this.calculateWeightedSelection(candidates, modelName).selected;
|
||||
}
|
||||
|
||||
private calculateWeightedSelection(candidates: RouteChannelCandidate[], modelName: string) {
|
||||
if (candidates.length === 0) {
|
||||
return {
|
||||
selected: null as RouteChannelCandidate | null,
|
||||
details: [] as Array<{ candidate: RouteChannelCandidate; probability: number; reason: string }>,
|
||||
};
|
||||
}
|
||||
|
||||
if (candidates.length === 1) {
|
||||
return {
|
||||
selected: candidates[0],
|
||||
details: [{
|
||||
candidate: candidates[0],
|
||||
probability: 1,
|
||||
reason: '唯一可用候选',
|
||||
}],
|
||||
};
|
||||
}
|
||||
|
||||
const { baseWeightFactor, valueScoreFactor, costWeight, balanceWeight, usageWeight } = config.routingWeights;
|
||||
const effectiveCosts = candidates.map((candidate) => resolveEffectiveUnitCost(candidate, modelName));
|
||||
|
||||
const valueScores = candidates.map((c, i) => {
|
||||
const unitCost = effectiveCosts[i]?.unitCost || 1;
|
||||
const balance = c.account.balance || 0;
|
||||
const totalUsed = (c.channel.successCount ?? 0) + (c.channel.failCount ?? 0);
|
||||
const recentUsage = Math.max(totalUsed, 1);
|
||||
return costWeight * (1 / unitCost) + balanceWeight * balance + usageWeight * (1 / recentUsage);
|
||||
});
|
||||
|
||||
const maxVS = Math.max(...valueScores, 0.001);
|
||||
const minVS = Math.min(...valueScores, 0);
|
||||
const range = maxVS - minVS || 1;
|
||||
const normalizedVS = valueScores.map((v) => (v - minVS) / range);
|
||||
|
||||
const baseContributions = candidates.map((c, i) => {
|
||||
const weight = c.channel.weight ?? 10;
|
||||
return (weight + 10) * (baseWeightFactor + normalizedVS[i] * valueScoreFactor);
|
||||
});
|
||||
|
||||
// Avoid over-favoring a site that has many tokens/channels for the same route.
|
||||
// Site-level total contribution remains comparable, then split across its channels.
|
||||
const siteChannelCounts = new Map<number, number>();
|
||||
for (const candidate of candidates) {
|
||||
siteChannelCounts.set(candidate.site.id, (siteChannelCounts.get(candidate.site.id) || 0) + 1);
|
||||
}
|
||||
|
||||
const contributions = candidates.map((candidate, i) => {
|
||||
const siteChannels = Math.max(1, siteChannelCounts.get(candidate.site.id) || 1);
|
||||
let contribution = baseContributions[i] / siteChannels;
|
||||
|
||||
// If upstream price is unknown and we are using fallback unit cost,
|
||||
// apply an explicit penalty so raising fallback cost meaningfully lowers probability.
|
||||
if (effectiveCosts[i]?.source === 'fallback') {
|
||||
contribution *= 1 / Math.max(1, effectiveCosts[i]?.unitCost || 1);
|
||||
}
|
||||
|
||||
return contribution;
|
||||
});
|
||||
|
||||
const totalContribution = contributions.reduce((a, b) => a + b, 0);
|
||||
const details = candidates.map((candidate, i) => {
|
||||
const probability = totalContribution > 0 ? contributions[i] / totalContribution : 0;
|
||||
const weight = candidate.channel.weight ?? 10;
|
||||
const cost = effectiveCosts[i];
|
||||
const costSourceText = cost?.source === 'observed'
|
||||
? '实测'
|
||||
: (cost?.source === 'configured' ? '配置' : (cost?.source === 'catalog' ? '目录' : '默认'));
|
||||
const siteChannels = Math.max(1, siteChannelCounts.get(candidate.site.id) || 1);
|
||||
return {
|
||||
candidate,
|
||||
probability,
|
||||
reason: `按权重随机(W=${weight},成本=${costSourceText}:${(cost?.unitCost || 1).toFixed(6)},同站点通道=${siteChannels},概率≈${(probability * 100).toFixed(1)}%)`,
|
||||
};
|
||||
});
|
||||
|
||||
let rand = Math.random() * totalContribution;
|
||||
let selected = candidates[candidates.length - 1];
|
||||
for (let i = 0; i < candidates.length; i++) {
|
||||
rand -= contributions[i];
|
||||
if (rand <= 0) {
|
||||
selected = candidates[i];
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
return { selected, details };
|
||||
}
|
||||
}
|
||||
|
||||
export const tokenRouter = new TokenRouter();
|
||||
|
||||
Some files were not shown because too many files have changed in this diff Show More
Reference in New Issue
Block a user