Extract dashboard settings routes
This commit is contained in:
@@ -47,20 +47,7 @@ import {
|
||||
sanitizeScheduledTask,
|
||||
} from './web-dashboard-data.js';
|
||||
import { handleSimpleGetRoute } from './web-dashboard-routes.js';
|
||||
import {
|
||||
addClaudeAccountFromToken,
|
||||
getActiveCodexSettingsIndex,
|
||||
getFastMode,
|
||||
getModelConfig,
|
||||
listClaudeAccounts,
|
||||
listCodexAccounts,
|
||||
refreshAllCodexAccounts,
|
||||
refreshCodexAccount,
|
||||
removeAccountDirectory,
|
||||
setActiveCodexSettingsIndex,
|
||||
updateFastMode,
|
||||
updateModelConfig,
|
||||
} from './settings-store.js';
|
||||
import { handleSettingsRoute } from './web-dashboard-settings-routes.js';
|
||||
|
||||
const DEFAULT_STATUS_MAX_AGE_MS = 10 * 60 * 1000;
|
||||
const ROOM_MESSAGE_ID_CACHE_LIMIT = 500;
|
||||
@@ -1258,155 +1245,12 @@ export function createWebDashboardHandler(
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname === '/api/settings/models' &&
|
||||
(request.method === 'PUT' || request.method === 'PATCH')
|
||||
) {
|
||||
let body: unknown = null;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return jsonResponse({ error: 'Invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
if (!body || typeof body !== 'object') {
|
||||
return jsonResponse(
|
||||
{ error: 'Body must be a JSON object' },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
try {
|
||||
const next = updateModelConfig(body as Record<string, unknown>);
|
||||
return jsonResponse(next);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname === '/api/settings/fast-mode' &&
|
||||
(request.method === 'PUT' || request.method === 'PATCH')
|
||||
) {
|
||||
let body: unknown = null;
|
||||
try {
|
||||
body = await request.json();
|
||||
} catch {
|
||||
return jsonResponse({ error: 'Invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
if (!body || typeof body !== 'object') {
|
||||
return jsonResponse(
|
||||
{ error: 'Body must be a JSON object' },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
try {
|
||||
const next = updateFastMode(body as Record<string, unknown>);
|
||||
return jsonResponse(next);
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const accountAddMatch = url.pathname.match(
|
||||
/^\/api\/settings\/accounts\/(claude)$/,
|
||||
);
|
||||
if (accountAddMatch && request.method === 'POST') {
|
||||
let body: { token?: unknown } | null = null;
|
||||
try {
|
||||
body = (await request.json()) as { token?: unknown };
|
||||
} catch {
|
||||
return jsonResponse({ error: 'Invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
const token = typeof body?.token === 'string' ? body.token.trim() : '';
|
||||
if (!token) {
|
||||
return jsonResponse({ error: 'token is required' }, { status: 400 });
|
||||
}
|
||||
try {
|
||||
const result = addClaudeAccountFromToken(token);
|
||||
return jsonResponse({ ok: true, ...result });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const accountDelMatch = url.pathname.match(
|
||||
/^\/api\/settings\/accounts\/(claude|codex)\/(\d+)$/,
|
||||
);
|
||||
if (accountDelMatch && request.method === 'DELETE') {
|
||||
const provider = accountDelMatch[1] as 'claude' | 'codex';
|
||||
const index = Number.parseInt(accountDelMatch[2], 10);
|
||||
try {
|
||||
removeAccountDirectory(provider, index);
|
||||
return jsonResponse({ ok: true, provider, index });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
{
|
||||
const refreshMatch = url.pathname.match(
|
||||
/^\/api\/settings\/accounts\/codex\/(\d+)\/refresh$/,
|
||||
);
|
||||
if (refreshMatch && request.method === 'POST') {
|
||||
const index = Number.parseInt(refreshMatch[1], 10);
|
||||
try {
|
||||
const updated = await refreshCodexAccount(index);
|
||||
return jsonResponse({ ok: true, account: updated });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname === '/api/settings/accounts/codex/refresh-all' &&
|
||||
request.method === 'POST'
|
||||
) {
|
||||
try {
|
||||
const result = await refreshAllCodexAccounts();
|
||||
return jsonResponse({ ok: true, ...result });
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
if (
|
||||
url.pathname === '/api/settings/accounts/codex/current' &&
|
||||
request.method === 'PUT'
|
||||
) {
|
||||
let body: { index?: unknown } | null = null;
|
||||
try {
|
||||
body = (await request.json()) as { index?: unknown };
|
||||
} catch {
|
||||
return jsonResponse({ error: 'Invalid JSON body' }, { status: 400 });
|
||||
}
|
||||
const idx = typeof body?.index === 'number' ? body.index : Number.NaN;
|
||||
if (!Number.isInteger(idx)) {
|
||||
return jsonResponse(
|
||||
{ error: 'index must be an integer' },
|
||||
{ status: 400 },
|
||||
);
|
||||
}
|
||||
try {
|
||||
setActiveCodexSettingsIndex(idx);
|
||||
return jsonResponse({
|
||||
ok: true,
|
||||
codexCurrentIndex: getActiveCodexSettingsIndex(),
|
||||
});
|
||||
} catch (err) {
|
||||
const message = err instanceof Error ? err.message : String(err);
|
||||
return jsonResponse({ error: message }, { status: 400 });
|
||||
}
|
||||
}
|
||||
const settingsRoute = await handleSettingsRoute({
|
||||
url,
|
||||
request,
|
||||
jsonResponse,
|
||||
});
|
||||
if (settingsRoute) return settingsRoute;
|
||||
|
||||
if (url.pathname === '/api/tasks' && request.method === 'POST') {
|
||||
if (!loadRoomBindings) {
|
||||
@@ -1599,22 +1443,6 @@ export function createWebDashboardHandler(
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/settings/accounts') {
|
||||
return jsonResponse({
|
||||
claude: listClaudeAccounts(),
|
||||
codex: listCodexAccounts(),
|
||||
codexCurrentIndex: getActiveCodexSettingsIndex(),
|
||||
});
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/settings/models') {
|
||||
return jsonResponse(getModelConfig());
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/settings/fast-mode') {
|
||||
return jsonResponse(getFastMode());
|
||||
}
|
||||
|
||||
if (url.pathname === '/api/stream') {
|
||||
const encoder = new TextEncoder();
|
||||
const stream = new ReadableStream({
|
||||
|
||||
Reference in New Issue
Block a user