$ show supermemoryai/llm-bridge
llm-bridge is a TypeScript library that provides a universal translation layer between major LLM provider APIs: OpenAI Chat Completions, OpenAI Responses API, Anthropic Claude, and Google Gemini. It defines a canonical 'universal' intermediate format that captures all provider-specific fields (including an `_original` payload for perfect round-trip reconstruction), and offers bidirectional converters (`toUniversal`/`fromUniversal`) for each provider. The library also includes streaming support with provider-specific SSE parsers and emitters, error normalization with classification helpers, tool calling lifecycle utilities, and a request handler that can inject context via an `editFunction` callbackβmaking it ideal for building proxies, middleware, load balancers, and cost optimizers. Architecturally, the codebase is well-organized into modules for models (format conversion), streaming (parsing/emitting), errors (normalization), helpers (type guards, reconstruction quality), and tools (continuation, extraction). The core pattern is a superset universal type that holds all possible fields, with converters that first attempt to use the original provider payload for lossless reconstruction before falling back to field-by-field translation. This design ensures high fidelity even when translating between providers with different capabilities (e.g., Anthropic's extended thinking blocks preserved through OpenAI conversion). The library is published as an npm package (MIT license) with dual CJS/ESM builds, includes over 300 unit tests covering round-trip conversion, streaming, error handling, and critical edge cases, and is used in production at supermemory.ai. While the code demonstrates strong engineering craftsmanship, it has some risks: heavy use of `any` type assertions in converters, tight coupling to provider SDK types (which may drift), an unpinned remote URL for token pricing, and no end-to-end integration tests with real API keys. Overall, it is a solid, reusable library for any project needing cross-provider LLM interoperability.
The LLM identified 4 files worth highlighting for reuse:
Implements `canPerfectlyReconstruct` and `getReconstructionQuality` to determine whether a universal message can be losslessly converted back to its original provider format. Uses deep equality checks against the stored `_original` payload. This pattern is reusable in any system that needs to preserve original data through transformations (e.g., API proxies, data pipelines) and demonstrates non-obvious attention to data fidelity.
Defines a `UniversalError` type with classification helpers (`isRetryableError`, `isUserError`, `isQuotaError`, `isAuthError`, `isRateLimitError`). This normalizes provider-specific error responses into a consistent structure, making it easy for consumers to handle errors uniformly. The pattern is self-contained and can be extracted for any API wrapper or proxy that needs to standardize error handling across multiple backends.
Contains a generic `parseSSEStream` helper that parses Server-Sent Events from a readable stream, plus provider-specific parsers (`parseOpenAIStream`, `parseAnthropicStream`, `parseGoogleStream`) that convert raw SSE data into `UniversalStreamEvent` objects. The generic SSE parser is reusable for any SSE-based API, and the provider-specific parsers demonstrate a clean pattern for normalizing streaming responses into a common format.
Exports `detectProvider` which automatically identifies the LLM provider from a request URL and body. It checks URL patterns (e.g., `/v1/chat/completions` for OpenAI, `/v1/messages` for Anthropic) and body fields (e.g., `model` prefix). This is a self-contained utility that can be reused in any proxy or middleware that needs to route or transform requests based on the target provider without explicit configuration.