cc-haha/src/server/router.ts
程序员阿江(Relakkes) 2a6c7dc67a Merge token usage view into local main
Integrate the Token usage settings page on top of the newer local main branch, preserving the existing H5 access router surface while adding the activity stats API route.

Constraint: Local main already contained H5 access router changes after the Token usage worktree base.
Rejected: Prefer either router branch during conflict resolution | both API surfaces are independent and must remain registered.
Confidence: high
Scope-risk: narrow
Directive: Keep h5-access and activity-stats routes registered together in src/server/router.ts.
Tested: Conflict marker scan for src/server/router.ts
Not-tested: Post-merge desktop/server checks pending
2026-05-10 11:38:33 +08:00

120 lines
3.7 KiB
TypeScript

/**
* API Router — 将请求路由到对应的 API handler
*/
import { handleSessionsApi } from './api/sessions.js'
import { handleSettingsApi } from './api/settings.js'
import { handleModelsApi } from './api/models.js'
import { handleScheduledTasksApi } from './api/scheduled-tasks.js'
import { handleSearchApi } from './api/search.js'
import { handleAgentsApi } from './api/agents.js'
import { handleStatusApi } from './api/status.js'
import { handleConversationsApi } from './api/conversations.js'
import { handleTeamsApi } from './api/teams.js'
import { handleFilesystemRoute } from './api/filesystem.js'
import { handleProvidersApi } from './api/providers.js'
import { handleAdaptersApi } from './api/adapters.js'
import { handlePluginsApi } from './api/plugins.js'
import { handleSkillsApi } from './api/skills.js'
import { handleComputerUseApi } from './api/computer-use.js'
import { handleHahaOAuthApi } from './api/haha-oauth.js'
import { handleHahaOpenAIOAuthApi } from './api/haha-openai-oauth.js'
import { handleMcpApi } from './api/mcp.js'
import { handleDiagnosticsApi } from './api/diagnostics.js'
import { handleDoctorApi } from './api/doctor.js'
import { handleH5AccessApi } from './api/h5-access.js'
import { handleActivityStatsApi } from './api/activityStats.js'
export async function handleApiRequest(req: Request, url: URL): Promise<Response> {
const path = url.pathname
const segments = path.split('/').filter(Boolean) // ['api', 'sessions', ...]
// Route to appropriate handler based on the second segment
const resource = segments[1]
switch (resource) {
case 'sessions': {
// Route /api/sessions/:id/chat/* to conversations handler
const subResource = segments[3]
if (subResource === 'chat') {
return handleConversationsApi(req, url, segments)
}
return handleSessionsApi(req, url, segments)
}
case 'conversations':
return handleConversationsApi(req, url, segments)
case 'settings':
return handleSettingsApi(req, url, segments)
case 'models':
case 'effort':
return handleModelsApi(req, url, segments)
case 'permissions':
return handleSettingsApi(req, url, segments) // permissions under settings
case 'scheduled-tasks':
return handleScheduledTasksApi(req, url, segments)
case 'search':
return handleSearchApi(req, url, segments)
case 'agents':
case 'tasks':
return handleAgentsApi(req, url, segments)
case 'status':
return handleStatusApi(req, url, segments)
case 'teams':
return handleTeamsApi(req, url, segments)
case 'providers':
return handleProvidersApi(req, url, segments)
case 'haha-oauth':
return handleHahaOAuthApi(req, url, segments)
case 'haha-openai-oauth':
return handleHahaOpenAIOAuthApi(req, url, segments)
case 'adapters':
return handleAdaptersApi(req, url, segments)
case 'skills':
return handleSkillsApi(req, url, segments)
case 'mcp':
return handleMcpApi(req, url, segments)
case 'plugins':
return handlePluginsApi(req, url, segments)
case 'computer-use':
return handleComputerUseApi(req, url, segments)
case 'diagnostics':
return handleDiagnosticsApi(req, url, segments)
case 'doctor':
return handleDoctorApi(req, url, segments)
case 'h5-access':
return handleH5AccessApi(req, url, segments)
case 'activity-stats':
return handleActivityStatsApi(req, url, segments)
case 'filesystem':
return handleFilesystemRoute(url.pathname, url)
default:
return Response.json(
{ error: 'Not Found', message: `Unknown API resource: ${resource}` },
{ status: 404 }
)
}
}