(server: FastifyInstance, ctx: HttpRouteContext)
| 3 | import { sessionService, ownerProfileService, PreferencesManager } from '@openchatlab/node-runtime' |
| 4 | |
| 5 | export function registerSessionRoutes(server: FastifyInstance, ctx: HttpRouteContext): void { |
| 6 | const { sessionAdapter: adapter } = ctx |
| 7 | |
| 8 | // Lazy: only owner-profile routes need preferences.json access |
| 9 | let preferencesInstance: PreferencesManager | null = null |
| 10 | const preferences = () => { |
| 11 | preferencesInstance ??= ctx.preferencesManager ?? new PreferencesManager(ctx.pathProvider.getSystemDir()) |
| 12 | return preferencesInstance |
| 13 | } |
| 14 | |
| 15 | server.get('/_web/sessions', async () => { |
| 16 | const aiChatCounts = ctx.aiChatManager?.getAIChatCountsBySession() |
| 17 | return sessionService.listAnalysisSessions(adapter, { |
| 18 | enrichSession: aiChatCounts?.size |
| 19 | ? (dto) => ({ ...dto, aiConversationCount: aiChatCounts.get(dto.id) ?? 0 }) |
| 20 | : undefined, |
| 21 | }) |
| 22 | }) |
| 23 | |
| 24 | server.get<{ Params: { id: string } }>('/_web/sessions/:id', async (request) => { |
| 25 | const session = sessionService.getAnalysisSession(adapter, request.params.id) |
| 26 | if (!session) { |
| 27 | throw Object.assign(new Error(`Session not found: ${request.params.id}`), { statusCode: 404 }) |
| 28 | } |
| 29 | if (ctx.aiChatManager) { |
| 30 | session.aiConversationCount = ctx.aiChatManager.getAIChats(request.params.id).length |
| 31 | } |
| 32 | return session |
| 33 | }) |
| 34 | |
| 35 | server.delete<{ Params: { id: string } }>('/_web/sessions/:id', async (request, reply) => { |
| 36 | const { id } = request.params |
| 37 | try { |
| 38 | const deleted = sessionService.deleteSession(adapter, id) |
| 39 | if (!deleted) { |
| 40 | return reply.code(404).send({ success: false, error: 'File not found' }) |
| 41 | } |
| 42 | return { success: true } |
| 43 | } catch (err) { |
| 44 | return reply.code(500).send({ success: false, error: String(err) }) |
| 45 | } |
| 46 | }) |
| 47 | |
| 48 | server.patch<{ Params: { id: string }; Body: { name: string } }>('/_web/sessions/:id/name', async (request) => { |
| 49 | sessionService.renameSession(adapter, request.params.id, request.body.name) |
| 50 | return { success: true } |
| 51 | }) |
| 52 | |
| 53 | server.patch<{ Params: { id: string }; Body: { ownerId: string | null } }>( |
| 54 | '/_web/sessions/:id/owner', |
| 55 | async (request) => { |
| 56 | sessionService.updateSessionOwnerId(adapter, request.params.id, request.body.ownerId ?? null) |
| 57 | return { success: true } |
| 58 | } |
| 59 | ) |
| 60 | |
| 61 | // Try to auto-apply the stored platform owner profile to this session. |
| 62 | server.post<{ Params: { id: string } }>('/_web/sessions/:id/owner/apply-profile', async (request) => { |
no test coverage detected