Plugin Helpers

View as Markdown

Generated from crates/node/plugin.d.ts.

Import from nemo-relay-node/plugin.

Plugin configuration, validation, activation, and registration helpers.

Interfaces

ConfigPolicy

Plugin-level policy for unknown or unsupported plugin configuration.

1export interface ConfigPolicy {
2 unknown_component?: UnsupportedBehavior;
3 unknown_field?: UnsupportedBehavior;
4 unsupported_value?: UnsupportedBehavior;
5}

ConfigDiagnostic

One validation or compatibility diagnostic produced by the plugin system.

1export interface ConfigDiagnostic {
2 level: 'warning' | 'error';
3 code: string;
4 component?: string;
5 field?: string;
6 message: string;
7}

ConfigReport

Validation or activation report for a plugin configuration.

1export interface ConfigReport {
2 diagnostics: ConfigDiagnostic[];
3}

ComponentSpec interface

One top-level plugin component.

1export interface ComponentSpec {
2 kind: string;
3 enabled?: boolean;
4 config?: Record<string, Json>;
5}

PluginConfig

Canonical plugin configuration document.

1export interface PluginConfig {
2 version?: number;
3 components?: Array<{
4 kind: string;
5 enabled?: boolean;
6 config?: Record<string, Json>;
7 }>;
8 policy?: ConfigPolicy;
9}

DynamicPluginActivationSpec

Explicitly resolved dynamic plugin load and component configuration.

1export interface DynamicPluginActivationSpec {
2 pluginId: string;
3 kind: DynamicPluginKind;
4 manifestRef: string;
5 environmentRef?: string | null;
6 config?: Record<string, Json>;
7}

DynamicPluginActivation

Owns one process-wide dynamic plugin host activation.

1export interface DynamicPluginActivation extends AsyncDisposable {
2 /** Validation report produced by the successful activation. */
3 readonly report: ConfigReport;
4 /**
5 * Whether this activation handle has not begun teardown. `false` does not
6 * guarantee another process-wide activation can start after failed teardown.
7 */
8 readonly active: boolean;
9 /** Clear callbacks before unloading libraries and workers. Idempotent. */
10 close(): Promise<void>;
11 /** Delegate structured `await using` cleanup to `close()`. */
12 [Symbol.asyncDispose](): Promise<void>;
13}

PendingMarkSpec

A mark Relay materializes under a managed lifecycle.

1export interface PendingMarkSpec {
2 name: string;
3 category?: string | null;
4 categoryProfile?: Json;
5 data?: Json;
6 metadata?: Json;
7}

LlmOptimizationDataSchema

Schema tag attached to an opaque optimization contribution payload.

1export interface LlmOptimizationDataSchema {
2 name: string;
3 version: string;
4}

LlmOptimizationModel

Model identity retained for counterfactual pricing and downstream repricing.

1export interface LlmOptimizationModel {
2 model: string;
3 provider?: string;
4}

LlmOptimizationModelTransition

Baseline and effective model identities for a routing optimization.

1export interface LlmOptimizationModelTransition {
2 baseline?: LlmOptimizationModel;
3 effective?: LlmOptimizationModel;
4}

LlmOptimizationTokens

Explicit token evidence, independent from a pricing catalog.

1export interface LlmOptimizationTokens {
2 /** Token counts must be non-negative JavaScript safe integers. */
3 prompt_tokens?: number;
4 /** Token counts must be non-negative JavaScript safe integers. */
5 completion_tokens?: number;
6 /** Token counts must be non-negative JavaScript safe integers. */
7 cache_read_tokens?: number;
8 /** Token counts must be non-negative JavaScript safe integers. */
9 cache_write_tokens?: number;
10 /** Token counts must be non-negative JavaScript safe integers. */
11 total_tokens?: number;
12}

LlmOptimizationTokenImpact

Baseline, effective, and saved token evidence for one optimization.

1export interface LlmOptimizationTokenImpact {
2 baseline?: LlmOptimizationTokens;
3 effective?: LlmOptimizationTokens;
4 saved?: LlmOptimizationTokens;
5 quality?: 'observed' | 'estimated';
6 estimation_method?: string;
7}

LlmOptimizationContribution

One plugin’s optimization evidence.

kind is deliberately an open string so new optimizer categories round-trip without a Relay release. Unknown top-level fields are retained by the wire contract and represented by this interface’s JSON extension surface.

1export interface LlmOptimizationContribution {
2 id?: string;
3 /** Relay ordering must remain within JavaScript's safe-integer range. */
4 sequence?: number;
5 producer: string;
6 kind: 'input_compression' | 'model_routing' | (string & {});
7 applied: boolean;
8 model_transition?: LlmOptimizationModelTransition;
9 token_impact?: LlmOptimizationTokenImpact;
10 payload_schema?: LlmOptimizationDataSchema;
11 payload?: Json;
12 [key: string]: Json | undefined;
13}

LlmRequestInterceptOutcome

Canonical result returned by an LLM request intercept.

1export interface LlmRequestInterceptOutcome {
2 request: Json;
3 annotated?: Json | null;
4 pendingMarks?: PendingMarkSpec[];
5 optimizationContributions?: LlmOptimizationContribution[];
6}

ToolExecutionInterceptOutcome

Canonical result returned by a tool execution intercept.

result is passed to the remaining middleware and application. pendingMarks are Relay-owned lifecycle metadata emitted after the tool-end event and are not included in the application-visible result.

1export interface ToolExecutionInterceptOutcome {
2 result: Json;
3 pendingMarks?: PendingMarkSpec[];
4}

PluginContext

Component-scoped registration context passed to plugin handlers.

1export interface PluginContext {
2 /** Register an infallible event subscriber for this component. */
3 registerSubscriber(name: string, callback: (event: Json) => void): void;
4 /** Register a mark event sanitizer for this component. */
5 registerMarkSanitizeGuardrail(
6 name: string,
7 priority: number,
8 callback: (event: Json, fields: EventSanitizeFields) => EventSanitizeFields,
9 ): void;
10 /** Register a scope-start event sanitizer for this component. */
11 registerScopeSanitizeStartGuardrail(
12 name: string,
13 priority: number,
14 callback: (event: Json, fields: EventSanitizeFields) => EventSanitizeFields,
15 ): void;
16 /** Register a scope-end event sanitizer for this component. */
17 registerScopeSanitizeEndGuardrail(
18 name: string,
19 priority: number,
20 callback: (event: Json, fields: EventSanitizeFields) => EventSanitizeFields,
21 ): void;
22 /** Register a tool sanitize-request guardrail for this component. */
23 registerToolSanitizeRequestGuardrail(
24 name: string,
25 priority: number,
26 callback: (name: string, args: Json) => Json,
27 ): void;
28 /** Register a tool sanitize-response guardrail for this component. */
29 registerToolSanitizeResponseGuardrail(
30 name: string,
31 priority: number,
32 callback: (name: string, result: Json) => Json,
33 ): void;
34 /** Register a tool conditional-execution guardrail for this component. */
35 registerToolConditionalExecutionGuardrail(
36 name: string,
37 priority: number,
38 callback: (name: string, args: Json) => string | null,
39 ): void;
40 /** Register an LLM sanitize-request guardrail for this component. */
41 registerLlmSanitizeRequestGuardrail(name: string, priority: number, callback: (request: Json) => Json): void;
42 /** Register an LLM sanitize-response guardrail for this component. */
43 registerLlmSanitizeResponseGuardrail(name: string, priority: number, callback: (response: Json) => Json): void;
44 /** Register an LLM conditional-execution guardrail for this component. */
45 registerLlmConditionalExecutionGuardrail(
46 name: string,
47 priority: number,
48 callback: (request: Json) => string | null,
49 ): void;
50 /** Register an LLM request intercept for this component. */
51 registerLlmRequestIntercept(
52 name: string,
53 priority: number,
54 breakChain: boolean,
55 callback: (args: { name: string; request: Json; annotated: Json | null }) => LlmRequestInterceptOutcome,
56 ): void;
57 /** Register an LLM execution intercept for this component. */
58 registerLlmExecutionIntercept(
59 name: string,
60 priority: number,
61 callback: (request: Json, next: (request: Json) => Json | Promise<Json>) => Json | Promise<Json>,
62 ): void;
63 /** Register an LLM streaming execution intercept for this component. */
64 registerLlmStreamExecutionIntercept(
65 name: string,
66 priority: number,
67 callback: (
68 request: Json,
69 next: (request: Json) => AsyncIterable<Json> | Promise<AsyncIterable<Json>>,
70 ) => AsyncIterable<Json> | Promise<AsyncIterable<Json>>,
71 ): void;
72 /** Register a tool request intercept for this component. */
73 registerToolRequestIntercept(
74 name: string,
75 priority: number,
76 breakChain: boolean,
77 callback: (name: string, args: Json) => Json,
78 ): void;
79 /**
80 * Register tool execution middleware that returns a canonical outcome.
81 * The `next` callback resolves to the raw downstream result.
82 */
83 registerToolExecutionIntercept(
84 name: string,
85 priority: number,
86 callback: (
87 args: Json,
88 next: (args: Json) => Json | Promise<Json>,
89 ) => ToolExecutionInterceptOutcome | Promise<ToolExecutionInterceptOutcome>,
90 ): void;
91}

Plugin

Plugin callback contract.

1export interface Plugin {
2 /** Validate one component-local config object. */
3 validate?(pluginConfig: Record<string, Json>): ConfigDiagnostic[] | null | undefined;
4 /**
5 * Install middleware and subscribers for one component instance.
6 *
7 * Throwing aborts the current initialization and triggers rollback.
8 */
9 register(pluginConfig: Record<string, Json>, context: PluginContext): void;
10}

Functions

defaultConfig

Create an empty plugin configuration.

Returns the canonical top-level config shape with version = 1 and no configured components so callers can build a document incrementally before validating or activating it.

Returns

A new PluginConfig object ready for mutation or validation.

Remarks

Mutating the returned object does not affect runtime state until it is passed to initialize.

1export declare function defaultConfig(): PluginConfig;

ComponentSpec function

Create a plugin component entry for a plugin config document.

Packages a plugin kind, component-local config, and enablement flag into the object shape expected by PluginConfig.components.

Parameters

  • kind: Registered plugin kind to reference.
  • config: Component-local config passed to plugin hooks.
  • options: Optional component-level flags.

Returns

A ComponentSpec ready to insert into a plugin config.

Remarks

Setting options.enabled = false preserves the component for validation while skipping runtime registration during initialize.

1export declare function ComponentSpec(
2 kind: string,
3 config?: Record<string, Json>,
4 options?: {
5 enabled?: boolean;

validate

Validate a plugin configuration without activating it.

Runs the same config validation pipeline used by initialization while leaving the active plugin registry and runtime configuration unchanged.

Parameters

  • config: Candidate plugin configuration document.

Returns

A structured validation report with diagnostics.

Remarks

Use this to surface warnings or incompatibilities before replacing the active plugin configuration.

1export declare function validate(config: PluginConfig): ConfigReport;

initialize

Validate and activate a plugin configuration.

Replaces the current active config, invokes each enabled component’s registration hooks, and resolves with the final activation report.

Parameters

  • config: Plugin configuration document to activate.

Returns

A promise resolving to the activation report.

Remarks

Partial plugin registration is rolled back if activation fails, and the promise rejects with the underlying validation or setup error.

1export declare function initialize(config: PluginConfig): Promise<ConfigReport>;

initializeWithDynamicPlugins

Initialize with explicitly resolved dynamic plugins.

The returned object owns loaded libraries and worker processes. Keep it alive while plugin callbacks may run and call close() for deterministic teardown. Garbage collection is a defensive fallback only.

Parameters

  • config: Base configuration layered over discovered plugins.toml files.
  • specs: Non-empty explicit manifest and component configuration for each plugin.

Returns

The owned activation and its validation report.

Remarks

File-configured static components initialize before dynamic components. Use initialize() for a static-only configuration.

1export declare function initializeWithDynamicPlugins(
2 config: PluginConfig,
3 specs: DynamicPluginActivationSpec[],
4): Promise<DynamicPluginActivation>;

clear

Clear the active plugin configuration.

Removes the currently active component registrations while leaving plugin kinds in the registry so they can be reused by a later initialization call.

Returns

Nothing.

Remarks

Registered plugin kinds remain available after the active config is cleared.

1export declare function clear(): void;

report

Return the last successfully activated plugin report.

Exposes the most recent activation report emitted by the native plugin system without triggering validation or registration work.

Returns

The last activation report, if one exists.

Remarks

This returns null until initialize succeeds at least once in the current process.

1export declare function report(): ConfigReport | null;

listKinds

List registered plugin kinds.

Returns the plugin kind identifiers currently known to the global registry so callers can inspect what can be referenced from plugin configs.

Returns

The registered plugin kind names.

Remarks

The list reflects registry state only; it does not indicate whether a plugin kind is currently active in the runtime configuration.

1export declare function listKinds(): string[];

register

Register a plugin kind with JavaScript validation and registration hooks.

Adapts the higher-level Plugin object contract to the native callback shape expected by the Node binding.

Parameters

  • pluginKind: Unique plugin kind identifier to register.
  • plugin: Plugin implementation with validate and register hooks.

Returns

Nothing.

Remarks

Omitting plugin.validate makes the plugin permissive during validation; plugin.register still runs later during initialize.

1export declare function register(pluginKind: string, plugin: Plugin): void;

deregister

Remove a previously registered plugin kind.

Deletes the plugin kind from the registry so future config validation and initialization calls can no longer reference it.

Parameters

  • pluginKind: Registered plugin kind identifier to remove.

Returns

true when a plugin kind was removed, otherwise false.

Remarks

Active runtime registrations remain until clear() or the next successful initialize(...).

1export declare function deregister(pluginKind: string): boolean;

Type Aliases

UnsupportedBehavior

Policy behavior for unsupported configuration.

1export type UnsupportedBehavior = 'ignore' | 'warn' | 'error';

DynamicPluginKind

Execution lane for a dynamically loaded Relay plugin.

1export type DynamicPluginKind = 'rust_dynamic' | 'worker';