AI import
Turn pasted text into a typed record with generateStructured from @civility/services/ai. You pass a prompt and a Zod schema and get back a validated object.
This example uses the user's own API key, kept in a settings document.
// import.ts
import { z } from '@zod/zod'
import { generateStructured } from '@civility/services/ai'
import { recipes, settings } from './store.ts'
const Recipe = z.object({
title: z.string(),
ingredients: z.array(z.string()),
steps: z.array(z.string()),
servings: z.number().optional(),
})
export async function importRecipe(pastedText: string) {
const { aiApiKey } = await settings.get()
if (!aiApiKey) throw new Error('Add an AI key in Settings to use import.')
const recipe = await generateStructured({
prompt: 'Extract a structured recipe from the text the user pasted.',
userDescription: pastedText,
schema: Recipe,
config: { apiKey: aiApiKey, provider: 'claude' },
})
await recipes.set(crypto.randomUUID(), recipe)
return recipe
}
store.ts here exports a settings document with an aiApiKey field and a recipes collection, set up as in Store.
The imported recipe is an ordinary record. It syncs and exports like one the user typed.
To use a key provided by the server instead, or to store keys as credentials, see the Services object.
Next: Share links