Build and test a provider
Start a provider from the starter package, run the shared conformance suites, and pack it into a RelKit app. Runtime, deploy-target and agent adapters follow the same steps.
This guide is for people writing a provider or adapter outside the RelKit repository. A provider
built this way is checked by the same tests as the first-party ones: the conformance suites in
@relkit/testing run the cases that Redis, Postgres, Cloudflare and the local providers pass.
Author a provider covers the descriptor APIs this
guide uses.
The provider contract is Core: the provider APIs in relkit/provider, the cache, bucket, event and
jobs provider interfaces, the runtime adapter and deploy-target interfaces, and the conformance
suites. An incompatible change to them ships only in a major release with a migration, as the
versioning policy describes. The realtime provider and agent adapter interfaces
belong to Labs features and can change in a minor release; see Maturity.
Start from the starter
Copy templates/provider-starter
from the RelKit repository and rename the package. It is a working cache provider with an in-memory
store; replace the store with a client of your backend. It contains:
| File | Role |
|---|---|
src/index.ts |
Authoring entry: starterCache() returns the descriptor an app puts in its config. |
src/runtime.ts |
Runtime entry: the provider and the runtimeIntegration registration that creates it. |
package.json |
Both entries under relkit.integration, so RelKit finds them without running them. |
tests/conformance.test.ts |
The shared cache suite, one test per case. |
The authoring entry is data only and loads no client library. The runtime entry is loaded by the
server for each profile that selects your adapter id, and its create function returns the
provider with ready and release hooks.
Name the package relkit-<name> or @<scope>/relkit-<name>. relkit check reads integration
metadata only from imports of relkit, @relkit/* and packages named this way, so a provider with
another name is not found and the check fails with RELKIT_RUNTIME_INTEGRATION_PACKAGE_MISSING.
Keep relkit.integration.id equal to the id you pass to defineIntegrationReference and to
runtimeIntegration.integrationId.
/**
* Authoring entry: the descriptor an application puts in `relkit.config.ts`. It is data only; it
* opens no connection and loads no runtime code.
*/
import {
defineConnectionContract,
defineIntegrationReference,
defineProviderAdapter,
defineProviderBehavior,
defineProviderCapability,
type ProviderAdapter,
type ProviderBehavior,
} from "relkit/provider";
const cache = defineProviderCapability("cache");
/** The descriptor `starterCache()` returns. */
export type StarterCacheAdapter = ProviderAdapter<
typeof cache,
"starter-memory-cache",
Readonly<Record<never, never>>,
ProviderBehavior<Readonly<{ maxEntries: number }>>
>;
/** The integration id, equal to `relkit.integration.id` in package.json. */
export const starterIntegration = defineIntegrationReference("starter");
/** Settings of the in-memory cache. */
export interface StarterCacheOptions {
/** Largest number of entries kept; the oldest entry is dropped first. Defaults to 1000. */
readonly maxEntries?: number;
}
/**
* Defines a cache profile backed by the starter's in-memory cache.
* @param options - Cache settings.
* @returns The provider descriptor.
* @example
* ```ts
* import { defineApp } from "relkit/config";
* import { starterCache } from "relkit-provider-starter";
* export default defineApp({ cache: { memory: starterCache() }, defaults: { cache: "memory" } });
* ```
*/
export function starterCache(options: StarterCacheOptions = {}): StarterCacheAdapter {
const maxEntries = options.maxEntries ?? 1000;
if (!Number.isSafeInteger(maxEntries) || maxEntries < 1)
throw new TypeError("starterCache maxEntries must be a positive integer");
return defineProviderAdapter({
integration: starterIntegration,
capability: cache,
adapterId: "starter-memory-cache",
connectionContract: defineConnectionContract({}),
connection: {},
behavior: defineProviderBehavior({ maxEntries }),
});
}
Run the conformance suites
Each provider kind has a suite. A suite exports its cases and a harness; run each case as a test,
or call runConformance from @relkit/testing/conformance for a report with pass, fail or skip per
case.
| Provider kind | Suite | Interface | First-party providers that run it |
|---|---|---|---|
| Cache | @relkit/testing/cache-conformance |
CacheProvider from relkit/cache |
local, Redis, Redis over HTTP, KV |
| Buckets | @relkit/testing/buckets-conformance |
BucketProvider from relkit/buckets |
local, R2 |
| Realtime | @relkit/testing/realtime-conformance |
RealtimeProvider from relkit/realtime/provider |
local, Redis, Durable Objects |
| Events | @relkit/testing/events-conformance |
EventProvider from relkit/events |
local, Postgres |
| Jobs | @relkit/testing/jobs-conformance |
JobsAdapterRuntime from relkit/jobs/adapter |
deterministic test adapter, Postgres |
/**
* Runs the shared cache conformance suite against the starter provider: the same cases every
* first-party cache provider passes, one test per case. Failure injection uses a store whose
* backend is down.
*/
import { describe, expect, test } from "bun:test";
import { createCacheConformanceHarness } from "@relkit/testing/cache-conformance";
import { runConformance } from "@relkit/testing/conformance";
import { createStarterCacheProvider, runtimeIntegration } from "../src/runtime.js";
import { starterCache } from "../src/index.js";
const down = async (): Promise<never> => {
throw new Error("backend unavailable");
};
const harness = createCacheConformanceHarness({
create: (cacheId) => createStarterCacheProvider({ cacheId }),
failing: () =>
createStarterCacheProvider({ cacheId: "down", store: { get: down, set: down, delete: down } }),
});
describe("starter cache conformance", () => {
for (const item of harness.cases) test(item.description, () => harness.run(item.id));
test("every case passes", async () => {
const report = await runConformance(harness);
expect(report.results.map((result) => result.outcome)).toEqual(harness.cases.map(() => "pass"));
});
});
test("the descriptor and the runtime registration name the same adapter", () => {
const descriptor = starterCache({ maxEntries: 10 });
const registration = runtimeIntegration.registrations[0]!;
expect(descriptor.adapterId).toBe(registration.adapterId);
expect(descriptor.integration.integrationId).toBe(runtimeIntegration.integrationId);
expect(() => starterCache({ maxEntries: 0 })).toThrow("positive integer");
});
A case is skipped only when the provider declares it lacks the feature: no increment capability,
no signed URLs, no close method, or no failing provider given. A failed case names the first
expectation that broke.
Inject failures
The suites cannot break your backend for you, so each one takes a failing factory: a provider
pointed at a backend that does not answer, such as an unreachable URL, a missing binding or, in the
starter, a store whose methods reject. The backend-failure case checks that operations reject
instead of reporting a miss. The jobs suite takes faulty instead: an adapter that commits a
submission and loses the response, so the unknown-acknowledgement case can check recovery.
Pack it and install it into an app
Build and pack the provider, then add the packed file to an app:
bun run build
bun pm pack
bun add ../relkit-provider-starter/relkit-provider-starter-0.1.0.tgz
Select it in the app’s config and run relkit check, which reads the integration metadata from the
installed package:
import { defineApp } from "relkit/config";
import { starterCache } from "relkit-provider-starter";
export default defineApp({
cache: { memory: starterCache({ maxEntries: 100 }) },
defaults: { cache: "memory" },
});
Write an adapter
Runtime, deploy-target and agent adapters work the same way: a public interface and a suite.
| Adapter | Interface | Suite | First-party adapters |
|---|---|---|---|
| Runtime | RuntimeAdapter from relkit/runtime |
@relkit/testing/runtime-conformance |
Bun, Node, Cloudflare Workers |
| Deploy target | DeployTarget from @relkit/cli/deploy-targets |
@relkit/testing/deploy-target-conformance |
Railway, Vercel, Cloudflare |
| Agent | AgentAdapterRuntime and defineAgentRuntime from relkit/agent-adapters |
@relkit/testing/agents-conformance |
LangChain, AI SDK |
- Runtime adapters run the suite inside the runtime they adapt. Pass
kind: "process"for a runtime with a listener, files and child processes, andkind: "isolate"for one without, whose adapter must refuse those operations. - Deploy targets render native configuration and a CI workflow and change no cloud resource.
The suite checks every declared runtime, deterministic output, paths inside the project, a GitHub
Actions workflow and, for targets that read the compiled graph, a clear error without it.
relkit deploy initaccepts the first-party targets only; a new target is written and tested against the interface. - Agent adapters are described in Agent adapters.
Reference: Testing API, Provider API,
CLI automation API for DeployTarget.