← back to Designer Portfolio Pages

src/aiIntro.ts

46 lines

import Anthropic from "@anthropic-ai/sdk";
import type { PatternSnapshot } from "./schemas.js";

export async function maybeGenerateAiIntro(args: {
  anthropicApiKey?: string;
  anthropicModel?: string;
  designerName: string;
  businessName?: string;
  title?: string;
  patterns: PatternSnapshot[];
}): Promise<string | null> {
  if (!args.anthropicApiKey) return null;

  const client = new Anthropic({ apiKey: args.anthropicApiKey });
  const model = args.anthropicModel?.trim() || "claude-opus-4-8";

  const patternList = args.patterns
    .slice(0, 12)
    .map(p => `- ${p.name} (${p.tags.slice(0, 4).join(", ")})`)
    .join("\n");

  const prompt = [
    "Write a concise intro paragraph (60-110 words) for a designer portfolio page.",
    "Audience: a homeowner + contractor + spouse viewing together.",
    "Tone: polished, warm, confident, non-salesy, interior-design oriented.",
    "Avoid buzzwords and avoid mentioning 'AI'.",
    "",
    `Designer: ${args.designerName}${args.businessName ? ` (${args.businessName})` : ""}`,
    `Portfolio title: ${args.title || "(untitled)"}`,
    "",
    "Selected patterns:",
    patternList
  ].join("\n");

  const msg = await client.messages.create({
    model,
    max_tokens: 220,
    messages: [{ role: "user", content: prompt }]
  });

  const first = msg.content?.[0];
  if (!first || first.type !== "text") return null;

  return first.text.trim() || null;
}