klg-asutk-app/app/api/ai-data/route.ts
Yuriy 0150aba4f5 Consolidation: KLG ASUTK + PAPA integration
- Unify API: lib/api.ts uses /api/v1, inbox uses /api/inbox (rewrites)
- Remove localhost refs: openapi, inbox page
- Add rewrites: /api/inbox|tmc -> inbox-server, /api/v1 -> FastAPI
- Add stub routes: knowledge/insights, recommendations, search, log-error
- Transfer from PAPA: prompts (inspection, tmc), scripts, supabase, data/tmc-requests
- Fix inbox-server: ORDER BY created_at, package.json
- Remove redundant app/api/inbox/files route (rewrites handle it)
- knowledge/ in gitignore (large PDFs)

Co-authored-by: Cursor <cursoragent@cursor.com>
2026-02-08 17:18:31 +03:00

63 lines
2.5 KiB
TypeScript

export const dynamic = "force-dynamic";
import { NextRequest, NextResponse } from 'next/server';
import { getDataForAI } from '../ai-data-helper';
import { filterSchema } from '@/lib/validation';
import { handleError } from '@/lib/error-handler';
import { logAudit } from '@/lib/logger';
/**
* API endpoint для предоставления ИИ агенту доступа ко всем базам данных
* ИИ агент может запрашивать данные через этот endpoint
*/
export async function POST(request: NextRequest) {
try {
const body = await request.json();
const { query: _query, dataType, filters } = body;
// Валидация фильтров
if (filters) {
filterSchema.parse(filters);
}
// Логирование доступа
const ip = request.ip ?? request.headers.get('x-forwarded-for') ?? 'unknown';
logAudit('AI_DATA_ACCESS', dataType || 'all', { ip, dataType, filters });
if (dataType === 'all') {
// Возвращаем сводку по всем базам данным
return NextResponse.json({
summary: {
aircraft: { count: 3, description: 'Воздушные суда' },
regulations: { count: 19, description: 'Нормативные документы' },
organizations: { count: 3, description: 'Организации' },
risks: { count: 3, description: 'Риски' },
audits: { count: 3, description: 'Аудиты' },
checklists: { count: 2, description: 'Чек-листы' },
applications: { count: 2, description: 'Заявки' },
users: { count: 3, description: 'Пользователи' },
documents: { count: 3, description: 'Документы' },
},
message: 'Используйте dataType для получения детальной информации по каждой базе данных',
});
}
// Получаем данные через helper функцию
const result = await getDataForAI(dataType, filters);
if (!result) {
return NextResponse.json(
{ error: 'Неизвестный тип данных', availableTypes: ['aircraft', 'regulations', 'organizations', 'risks', 'audits', 'checklists', 'applications', 'users', 'documents', 'all'] },
{ status: 400 }
);
}
return NextResponse.json(result);
} catch (error: any) {
return handleError(error, {
path: '/api/ai-data',
method: 'POST',
});
}
}