import { z } from "zod"; import type { ProviderApiKeys } from "./keys"; import { generateStructuredObject } from "./generate-structured"; import { getModel } from "./provider"; import { buildSystemPrompt, buildUserPrompt } from "./prompts"; import { THREADS_MAX_CHARS } from "@/lib/utils"; const draftSchema = z.object({ drafts: z.array( z.object({ text: z.string().max(THREADS_MAX_CHARS), angle: z.string(), rationale: z.string(), sourcePermalinks: z.array(z.string()), }) ), }); export interface GenerateInput { topicLabel: string; persona?: string | null; aiProvider: string; aiModel: string; apiKeys?: ProviderApiKeys; count: number; posts: Array<{ text: string; authorName?: string | null; permalink?: string | null; likeCount?: number | null; replyCount?: number | null; replies?: Array<{ text: string; authorName?: string | null; likeCount?: number | null }>; }>; } export async function generatePostDrafts(input: GenerateInput) { const model = getModel(input.aiProvider, input.aiModel, input.apiKeys ?? {}); const object = await generateStructuredObject({ model, provider: input.aiProvider, modelId: input.aiModel, schema: draftSchema, system: buildSystemPrompt(input.persona), prompt: buildUserPrompt({ topicLabel: input.topicLabel, posts: input.posts, count: input.count, }), }); return object.drafts.map((draft) => ({ ...draft, text: draft.text.slice(0, THREADS_MAX_CHARS), })); }