52 lines
1.5 KiB
TypeScript
52 lines
1.5 KiB
TypeScript
import { protectRoute } from '~~/server/utils/auth';
|
|
import { getPendingGeneration, getActiveGeneration, isGenerationActive } from '~~/server/utils/generation';
|
|
import type { GenerationStatus } from '~~/server/types/chat';
|
|
|
|
export default defineEventHandler(async (event) => {
|
|
await protectRoute(event);
|
|
|
|
const generationId = getRouterParam(event, 'id');
|
|
|
|
if (!generationId) {
|
|
throw createError({
|
|
statusCode: 400,
|
|
statusMessage: 'Missing generation ID'
|
|
});
|
|
}
|
|
|
|
const pendingGeneration = getPendingGeneration(generationId);
|
|
const isActive = isGenerationActive(generationId);
|
|
const activeGeneration = getActiveGeneration(generationId);
|
|
|
|
if (!pendingGeneration && !activeGeneration) {
|
|
throw createError({
|
|
statusCode: 404,
|
|
statusMessage: 'Generation not found'
|
|
});
|
|
}
|
|
|
|
if (pendingGeneration) {
|
|
const status: GenerationStatus = {
|
|
generationId,
|
|
status: 'pending',
|
|
topicId: pendingGeneration.topicId
|
|
};
|
|
return status;
|
|
}
|
|
|
|
if (isActive && activeGeneration) {
|
|
const status: GenerationStatus = {
|
|
generationId,
|
|
status: 'active',
|
|
content: activeGeneration.content,
|
|
topicId: activeGeneration.topicId
|
|
};
|
|
return status;
|
|
}
|
|
|
|
const status: GenerationStatus = {
|
|
generationId,
|
|
status: 'completed'
|
|
};
|
|
return status;
|
|
}); |