Beyond the defaults
Merged options
Set adapter defaults once and share them across your entire context.
What are merged options?
Many adapters accept options at the call site: timezone for cron(), temperature for llm(), and so on. When the same options repeat across dozens of capabilities, duplication becomes a maintenance problem. Merged options solve this by letting you register context-level defaults that every adapter of that type inherits automatically.
The merge hierarchy (last wins):
- Built-in defaults: hardcoded in the adapter (e.g.
temperature: 0forllm()) - Context defaults: registered in
craft.config.ts - Per-adapter options: passed directly at the call site
Per-adapter options always take precedence over context defaults, which in turn take precedence over built-in defaults.
Setting defaults for core adapters
Core adapters (cron, direct) have dedicated fields on CraftConfig. Set them once and every adapter of that type in the context inherits the values:
// craft.config.ts
import { defineProject } from '@routecraft/routecraft'
export default defineProject({
cron: { timezone: 'UTC', maxJitter: 2000 },
})
Now every cron() source inherits timezone: 'UTC' and maxJitter: 2000 unless overridden:
// Inherits timezone: 'UTC' and maxJitter: 2000 from config
.from(cron('@daily'))
// Overrides timezone but keeps maxJitter: 2000
.from(cron('0 9 * * 1-5', { timezone: 'America/New_York' }))
Setting defaults for external adapters
Adapters from other packages (like @routecraft/ai) use the plugin pattern. Register a companion plugin in craft.config.ts:
import { defineProject } from '@routecraft/routecraft'
import { llmPlugin, embeddingPlugin } from '@routecraft/ai'
export default defineProject({
plugins: [
llmPlugin({
providers: { anthropic: { apiKey: process.env.ANTHROPIC_API_KEY! } },
defaultOptions: { temperature: 0.7 },
}),
embeddingPlugin({
providers: { openai: { apiKey: process.env.OPENAI_API_KEY! } },
}),
],
})
Plugins that manage additional concerns (like llmPlugin which also registers provider credentials) wrap defaultOptions inside a larger configuration object. See the Plugins reference for the full options of each plugin.
The direct adapter also supports a context-level channelType to swap all endpoints from in-memory to a distributed implementation. See Configuration.
Supported adapters
How it works
Under the hood, a config key such as cron installs a small plugin that holds the defaults for the application. When an adapter needs its options (in subscribe() or send()), it reads its plugin's defaults for the application it runs in and combines them with its own per-adapter options. Per-adapter values always win.
The shipped cron() source publishes its defaults through a port: a named, typed token one plugin provides and another plugin can look up.
┌──────────────┐ provide() ┌─────────────────┐
│ CraftConfig │─────────────►│ port │
│ cron: { ... }│ │ CRON_DEFAULTS │
└──────────────┘ └───────┬─────────┘
│ context.lookup()
▼
┌─────────────────┐
│ CronAdapter │
│ mergedOptions()│
│ { ...defaults, │
│ ...adapter } │
└─────────────────┘
A port's identity is its name: the token's key is the registered symbol for it, so two copies of the module that declares it in one process (the ESM and CJS builds of one install, a global CLI beside a project's own copy) resolve as one port rather than silently splitting the defaults in two. Two tokens with one name but different keys in one application are RC1103.
Adding merged options to a custom adapter
If you are building a custom adapter and want to support merged options, follow these steps.
1. Define the options type
export interface MyAdapterOptions {
apiKey?: string
baseUrl?: string
timeout?: number
}
2. Keep the defaults per application
Call applicationState() once, at module scope. It holds one value per application, so two applications in one process (two test contexts, say) never read each other's defaults:
import { applicationState } from '@routecraft/routecraft'
const defaults = applicationState<Partial<MyAdapterOptions>>()
3. Merge them in your adapter
A destination or an enricher reads the defaults of the application its exchange runs in with .of(exchange):
class MyAdapter implements Destination<unknown> {
readonly adapterId = 'acme.adapter.my-adapter'
public options: Partial<MyAdapterOptions>
constructor(options?: Partial<MyAdapterOptions>) {
this.options = options ?? {}
}
async send(exchange) {
const opts = {
timeout: 5000, // built-in default
...defaults.of(exchange), // application defaults
...this.options, // per-adapter overrides
}
// use opts.apiKey, opts.baseUrl, opts.timeout ...
}
}
A source has no exchange yet when it subscribes. It reads the same state with defaults.get(sub.context.application).
4. Create a plugin factory
Ship a companion plugin that stores the defaults in bind, so users have a typed, discoverable API:
import { definePlugin } from '@routecraft/routecraft'
export function myAdapterPlugin(defaultOptions: Partial<MyAdapterOptions>) {
return definePlugin({
id: 'acme.my-adapter',
bind(c) {
defaults.set(c.application, defaultOptions)
},
})
}
To offer a config key instead (defineProject({ myAdapter: {...} })), register the same factory with registerConfigApplier('myAdapter', myAdapterPlugin) and augment CraftConfig with the key.
5. Export the plugin
Export the plugin from your package. The state stays private to your module: if another plugin should be able to provide or read the defaults, publish them through a port as well.
export { myAdapterPlugin }
Related
Adapters
How adapters are configured, at the call site and in craft config.
Configuration
Every CraftConfig field, including the cron and direct defaults.
Plugins
The reference catalogue: every built-in plugin and its options.