$ show miurla/morphic
Morphic is a self-hostable AI answer engine and chat application. In production it streams LLM research results into a rich chat UI: it runs an agentic researcher loop that calls search tools (SearXNG, Tavily, Brave, Exa, Firecrawl), fetches and extracts page content, allocates numbered citations, and emits a JSONL-based 'spec' stream that a generative UI renderer turns into structured cards, grids, headings, buttons, and inline images. Alongside the core chat it ships usage-budget metering, guest/anonymous chat, file uploads to S3-compatible storage, a notes/library feature, feedback collection, model discovery and selection, chat sharing, admin invitations, and an analytics relay that forwards scrubbed PostHog events without exposing authentication tokens.
The LLM identified 5 files worth highlighting for reuse:
A self-contained SSRF-hardened fetch wrapper that validates URLs before connect, blocks private/reserved IPv4 and IPv6 ranges, re-checks every manual redirect target, and provides an assertion-based public-URL guard. This solves a universal server-side problem (fetching user-supplied URLs safely) and can be dropped into any Node or edge fetch pipeline with minimal adaptation.
Deterministic classification of upstream AI/provider errors into stable, user-safe public error payloads (billing, rate-limit, context-length, model-unavailable) while strictly preventing internal exception details from reaching clients. The redaction and provenance-guard approach is directly reusable in any API gateway or LLM proxy that must sanitize errors without losing operational signal.
An atomic Redis-backed consumption/refund gate using hand-written Lua scripts, with attempt deduplication, monthly/hourly TTL windows, shadow-mode enforcement, refund handling, and fail-open behavior when Redis is unavailable. It demonstrates a production-grade metering pattern that is essentially provider-agnostic and could be extracted as the backend for any pay-per-use or quota system.
Abstract base class for web-search providers with a uniform result schema, HTTP-error creation, and recoverable-error classification for retry/fallback decisions. Adding a new search backend reduces to implementing one well-defined interface; the same seam pattern applies to any multi-vendor API integration that needs graceful fallbacks.
A streaming parser for JSONL 'spec' blocks emitted by LLMs, with an idempotent migration chain, partial-spec pruning, and forward-compatibility guarantees that decouple model output from renderer capabilities. This is a non-obvious design for anyone building generative UI or structured streaming output on top of an LLM, and the parser itself is independent of the React render layer.