feat(collections): add AI-generated use cases

Add "Example Use Cases" section to collection pages that generates
practical workflow examples showing how tools can work together.

- Add useCases and useCasesGeneratedAt fields to Collection model
- Create use-cases-generator.ts using Vercel AI SDK with gpt-4.1-mini
- Add POST /api/collections/[id]/use-cases/generate endpoint
- Add AI_GENERATION_RATE_LIMIT (5 req/hour per IP)
- Create UseCasesSection component with generate/regenerate UI
- Generate 6 use cases: 3 simple (1-2 tools) + 3 complex (3-5 tools)
- Include useCases in public collection API response
This commit is contained in:
Ajax Davis 2026-01-16 02:33:57 +10:00
parent c21ed5b41e
commit 3456b26c9d
8 changed files with 480 additions and 0 deletions

View file

@ -96,6 +96,38 @@ export const CloneCollectionSchema = z.object({
.optional(), // If not provided, will use original name or append "(copy)"
});
// ============================================================================
// Use Case Types (AI-generated workflows)
// ============================================================================
export const UseCaseToolStepSchema = z.object({
toolName: z.string().describe('Name of the tool being invoked'),
packageName: z.string().describe('NPM package name containing the tool'),
purpose: z.string().max(100).describe('Why this tool is called at this step'),
order: z.number().int().min(1).describe('Execution order (1-based)'),
});
export const UseCaseSchema = z.object({
id: z.string().describe('Unique identifier for this use case'),
userPrompt: z
.string()
.min(20)
.max(200)
.describe('Example user prompt that triggers this workflow'),
description: z.string().min(30).max(150).describe('Brief description of what this accomplishes'),
toolSequence: z
.array(UseCaseToolStepSchema)
.min(1)
.max(10)
.describe('Ordered sequence of tool calls'),
});
export const CollectionUseCasesSchema = z.array(UseCaseSchema).max(6);
export type UseCaseToolStep = z.infer<typeof UseCaseToolStepSchema>;
export type UseCase = z.infer<typeof UseCaseSchema>;
export type CollectionUseCases = z.infer<typeof CollectionUseCasesSchema>;
// ============================================================================
// Response Types (for API responses)
// ============================================================================
@ -109,6 +141,8 @@ export const CollectionSchema = z.object({
toolCount: z.number(),
forkCount: z.number().default(0),
forkedFromId: z.string().nullable().optional(),
useCases: CollectionUseCasesSchema.nullable().optional(),
useCasesGeneratedAt: z.date().nullable().optional(),
createdAt: z.date(),
updatedAt: z.date(),
});