Files
heicode-win/cc-haha/src/server/router.ts
T
chenchenandClaude Opus 4.7 c0363be850 feat(resources): slice 3 — persist mcp JWT, proxy, list bindings
Backend:

  1. Extend SavedProvider schema with optional mcpAuth field
     (accessToken / refreshToken / accessExpiresAt / refreshExpiresAt /
     managerLoginUrl / userId / channelId). Wired through
     CreateProviderInput and UpdateProviderInput so providerService
     persists tokens to providers.json.

  2. handleLoginWithCredentials (Path A) now decodes the JWT exp claim
     of both tokens (no signature verification — issuer just authed us)
     and stores the resulting mcpAuth object on the saved provider.
     Documented TTL (24h access / 7d refresh) used as fallback if exp
     claim missing.

  3. New handler api/heicode-resources.ts — proxy for the local server
     route /api/heicode-resources/{*path}. It:
       - Reads mcpAuth from the active provider (401 if missing)
       - Refreshes the access token if < 60s from expiry by calling
         <managerLoginUrl>/api/auth/refresh; persists the new pair
         back to providers.json before forwarding
       - Returns 401 if refresh token is also expired (re-login needed)
       - Forwards request to <managerLoginUrl>/api/resources or
         /api/resource-grants with Authorization: Bearer <accessToken>
       - Passes status + body through

  4. router.ts: register case 'heicode-resources'.

  5. errorHandler: add ApiError.unauthorized(401) and badGateway(502)
     factories used by the proxy.

Desktop:

  6. New api/heicodeResources.ts client + types (ResourceBinding,
     ResourceGrant, etc. mirroring mcp-server contract). Slice 3 only
     exposes listBindings + getBinding.

  7. New stores/resourceStore.ts (zustand) with bindings, isLoading,
     hasFetched, error + fetchBindings action.

  8. pages/ResourceBindings.tsx upgraded from shell to a real list:
       - Auto-fetches on mount
       - Shows loading skeleton, error banner with dismiss, empty state,
         or a 5-column table (Name / Type / External ref / Status /
         Permission scope)
       - Refresh button in the header
       - Footer note about CRUD coming in slice 4

  9. i18n: 14 new keys (common.dismiss + resources.refresh / refreshing
     / col.* / error.title / footer.cruComingSoon) in both zh + en.

E2E behaviour after install: log in via 55@55.com / By@123456., open
Resources tab — local server proxies to apimtaiji and lists whatever
ResourceBindings the user has on mcp-server. New test account 55@55.com
has 0 bindings, so empty state shows up.

Slice 4 next: Create / Edit / Delete binding modals + Grants UI.

Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
2026-05-07 18:04:59 +08:00

108 lines
3.3 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 { handleHeicodeAuthApi } from './api/heicode-auth.js'
import { handleHeicodeResourcesApi } from './api/heicode-resources.js'
import { handleMcpApi } from './api/mcp.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 'heicode-auth':
return handleHeicodeAuthApi(req, url, segments)
case 'heicode-resources':
return handleHeicodeResourcesApi(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 'filesystem':
return handleFilesystemRoute(url.pathname, url)
default:
return Response.json(
{ error: 'Not Found', message: `Unknown API resource: ${resource}` },
{ status: 404 }
)
}
}