@novaproto/contracts
v0.2.0
Published
Canonical AI-provider contracts (chat, embeddings, models, health) shared by llamactl, sirius-gateway, and embersynth.
Readme
@novaproto/contracts
One vocabulary for every AI provider. The Zod-validated contract layer every gateway, agent harness, and operator surface otherwise re-derives.
Schemas for chat, embeddings, models, streaming, health, usage, and pricing —
plus the AiProvider interface and an OpenAI-compatible adapter factory.
This is the SDK core of Nova. It carries
zero runtime logic of its own beyond zod (dependencies: { zod: ^4.3.6 }):
schemas for the types that cross the wire, TypeScript interfaces for the runtime
abstractions, and one concrete adapter factory for the OpenAI REST dialect.
Implement AiProvider once; route, bill, observe, and proxy everywhere.
Install
npm install @novaproto/contracts
# or: bun add @novaproto/contractsRange in package.json: "@novaproto/contracts": "^0.1.0".
What it gives you
Each capability below is backed by a real export from
src/ — no invented surface.
- Canonical chat wire types —
UnifiedAiRequestSchema/UnifiedAiResponseSchema(+ inferredUnifiedAiRequest/UnifiedAiResponse),ChatMessageSchema,ContentBlockSchema(discriminated union of text /image_url/ input-audio blocks),ToolSchema,ToolCallSchema,ToolChoiceSchema,ResponseFormatSchema(text/json_object/json_schema),RoleSchema,FinishReasonSchema,UsageSchema. - Streaming events —
UnifiedStreamEventSchema, a discriminated union ofchunk/tool_call/error/done, with tool-call delta preservation (UnifiedStreamChunkSchema,StreamChoiceSchema,StreamDeltaSchema). - Execution events —
ExecutionEventV1Schema, one accepted execution attempt asaccepted→content→ exactly oneterminal, with request and attempt identity and a gap-freesequenceon every event. Content wraps the canonicalchunk/tool_callstream events; the terminal iscompleted(requirescompletion: "upstream"),cancelled,failed(typedExecutionErrorCodeSchemacode) orunknown-outcome, and always carries aUsageObservationV1.validateExecutionEventSequence, orstepExecutionSequence+finishExecutionSequencefor a live stream, returns typed violations. Golden fixtures:test/fixtures/execution-event-v1/. - Embeddings —
UnifiedEmbeddingRequestSchema/UnifiedEmbeddingResponseSchema,EmbeddingRowSchema; string / array / token-array input union,encoding_format,dimensions. - Models catalog —
ModelInfoSchema,ModelListResponseSchema,ModelCapabilitySchema,ModelCostSchema. - Health —
ProviderHealthSchema,ProviderHealthStateSchema(healthy/degraded/unhealthy/unknown). - Usage record —
UsageRecordSchemaandUsageKindSchema. Privacy lock by construction: it records token counts, not content. - Pricing —
ModelPricingSchema,ProviderPricingSchema, and thePricingCatalogtype (Map<string, ProviderPricing>). - Retrieval (RAG) —
SearchRequest/SearchResponse,StoreRequest/StoreResponse,DeleteRequest/DeleteResponse,ListCollectionsResponse,DocumentSchema,SearchResultSchema,CollectionInfoSchema. Score is cosine similarity normalized0..1. - Runtime abstractions (TypeScript interfaces, not Zod) —
AiProvider(createResponse, optionalstreamResponse/createEmbeddings/listModels/healthCheck),RetrievalProvider,ProviderFactory/ProviderFactoryInput,ProviderRegistry. - OpenAI-compat adapter factory —
createOpenAICompatProvider(opts): AiProvider. Covers non-streaming and SSE streaming (content + tool-call deltas), embeddings,listModels, andhealthCheck; fires anonUsagehook after each successful call; strips Nova-onlycapabilities/providerOptionsbefore going on the wire. Also exportsmergeRequestHeaders,OpenAICompatUsageSnapshot,OpenAICompatOnUsage.
Example
Build a provider for any OpenAI-compatible upstream — OpenAI itself, Together,
groq, Mistral, or a local llama-server:
import { createOpenAICompatProvider } from "@novaproto/contracts";
const provider = createOpenAICompatProvider({
name: "together",
baseUrl: "https://api.together.xyz/v1",
apiKey: process.env.TOGETHER_API_KEY!,
});
const res = await provider.createResponse({
model: "meta-llama/Llama-3.3-70B-Instruct",
messages: [{ role: "user", content: "hello" }],
});
console.log(res.choices[0].message.content, res.latencyMs, res.provider);OpenAICompatOptions fields: name, displayName?, baseUrl, apiKey,
fetch?, extraHeaders?, healthPath? (default /models), onUsage?.
The adapter is kept deliberately thin — no retry loop, no failover, no logging.
Those belong in the orchestrator that composes providers. To persist usage,
wire the onUsage hook to the JSONL sink in
@novaproto/mcp-shared.
Node-portable
The package has no bun:* or Bun.* dependencies — it runs on plain Node.js
and on Bun alike. It builds to dist/src/*.js + .d.ts via tsc --build, and
main / types point at the compiled JavaScript. Bun is the monorepo's dev and
CI runtime only; consumers are not required to adopt it.
License
MIT — part of the Nova monorepo. See the
root README for the sibling
packages @novaproto/mcp-shared and @novaproto/mcp, and the architecture
overview.
