How I built "highlight → rewrite with AI" for Teachi, an AI lesson planner used by 600+ teachers.
Teachers wanted Google-Docs-style editing: type, bold, and rewrite any passage with AI, without wrecking the formatting they'd applied by hand.
What made it work:
• Tiptap editor with a floating toolbar on selection. Bold/italic apply instantly and never call an API.
• The selected range is flattened to plain text before it goes to the model. The AI reasons about words, not markup.
• The reply is inserted back at exactly that range, so every other paragraph keeps its formatting.
• Select fewer than two words and it asks for a full sentence: one word gives the AI no context.
• Plans stay in the same string field, now as HTML, so nothing needed a migration and old plans open as plain text.
• Debounced autosave (1.5s idle) persists edits once they settle.
Stack: Next.js, Supabase, OpenAI, Tiptap.
Building AI features into a Next.js app? Happy to swap notes.