Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 5 additions & 3 deletions crates/node/model_pricing.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
// SPDX-License-Identifier: Apache-2.0

import type { Json, JsonObject } from './index';
import type { ComponentSpec as PluginComponentSpec, ConfigReport } from './plugin';
import type { ComponentSpecFor, ConfigReport } from './plugin';

/** Plugin kind used by the model pricing component. */
export declare const PRICING_PLUGIN_KIND = 'pricing';
Expand Down Expand Up @@ -111,12 +111,14 @@ export declare function fileSource(path: string): FileSource;
export declare function pricingConfig(sources?: PricingSource[]): PricingConfig;
/** Wrap model pricing config as a top-level plugin component. */
export declare function ComponentSpec(
config: PricingConfig | JsonObject,
config: PricingConfig,
Comment thread
willkill07 marked this conversation as resolved.
Resolved
options?: {
enabled?: boolean;
},
): PluginComponentSpec;
): ComponentSpecFor<'pricing'>;
/** Validate a model pricing config document without activating it. */
export declare function validateConfig(config: PricingConfig | JsonObject): ConfigReport;
/** Validate an untyped model pricing document and return it as a config; throws on error diagnostics. */
export declare function parseConfig(document: JsonObject): PricingConfig;

export type { Json };
17 changes: 17 additions & 0 deletions crates/node/model_pricing.js
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,22 @@ function validateConfig(config) {
}).config;
}

/**
* Validate an untyped model pricing document and return it as a config.
*
* @param {object} document - Model pricing component configuration document.
* @returns {object} The same document once validation reports no errors.
*/
function parseConfig(document) {
const errors = validateConfig(document).diagnostics.filter((diagnostic) => diagnostic.level === 'error');
if (errors.length > 0) {
throw new Error(`invalid model pricing config: ${errors.map((diagnostic) => diagnostic.message).join('; ')}`, {
cause: errors,
});
}
return document;
}

module.exports = {
PRICING_PLUGIN_KIND,
defaultConfig,
Expand All @@ -185,4 +201,5 @@ module.exports = {
pricingConfig,
ComponentSpec,
validateConfig,
parseConfig,
};
2 changes: 1 addition & 1 deletion crates/node/pii_redaction.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -69,4 +69,4 @@ export declare function ComponentSpec(
options?: {
enabled?: boolean;
},
): import('./plugin.js').ComponentSpec;
): ComponentSpec;
40 changes: 32 additions & 8 deletions crates/node/plugin.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,14 @@
/// <reference lib="esnext.disposable" />

import type { AdaptivePluginConfig } from './adaptive';
import type { ConfigDiagnostic, Json, JsonObject, JsonObjectCompatible, PluginContext, ResourceMetricsConfig } from './index';
import type {
ConfigDiagnostic,
Json,
JsonObject,
JsonObjectCompatible,
PluginContext,
ResourceMetricsConfig,
} from './index';
import type { PricingConfig } from './model_pricing';
import type { Config as ObservabilityConfig } from './observability';
import type { Config as PiiRedactionConfig } from './pii_redaction';
Expand Down Expand Up @@ -71,23 +78,33 @@ export type KnownComponentSpec = {
[K in ComponentKind]: { kind: K; enabled?: boolean; config?: ComponentConfigs[K] };
}[ComponentKind];

/** One top-level plugin component. */
export interface ComponentSpec {
declare const openComponent: unique symbol;

/** Config of a component created with `ComponentSpec` for a kind registered through `registerPlugin`. */
export type OpenComponentConfig = {
readonly [openComponent]: true;
};

/** Component for a kind registered through `registerPlugin`; create one with `ComponentSpec`. */
export interface OpenComponentSpec<C extends object = JsonObject> {
kind: string;
enabled?: boolean;
config?: JsonObject;
config?: C & OpenComponentConfig;
}

export type ComponentConfigFor<K extends string> = K extends ComponentKind ? ComponentConfigs[K] : JsonObject;

export type ComponentSpecFor<K extends string> = K extends ComponentKind
? Extract<KnownComponentSpec, { kind: K }>
: ComponentSpec;
: OpenComponentSpec;

/** One top-level plugin component. */
export type ComponentSpec = KnownComponentSpec | OpenComponentSpec<object>;

/** Canonical plugin configuration document. */
export interface PluginConfig {
version?: number;
components?: Array<KnownComponentSpec | ComponentSpec>;
components?: ComponentSpec[];
policy?: ConfigPolicy;
/** Startup-only host service profile; omitted settings use file layers. */
diagnostics?: { enabled?: boolean; limits?: Record<string, number> };
Expand Down Expand Up @@ -181,13 +198,20 @@ export declare function defaultConfig(): PluginConfig;
* @remarks Setting `options.enabled = false` preserves the component for
* validation while skipping runtime registration during `initialize`.
*/
export declare function ComponentSpec<K extends string, C extends JsonObjectCompatible<C> = JsonObject>(
export declare function ComponentSpec<K extends ComponentKind>(
kind: K,
config?: K extends ComponentKind ? ComponentConfigs[K] : C,
config?: ComponentConfigs[K],
options?: {
enabled?: boolean;
},
): ComponentSpecFor<K>;
export declare function ComponentSpec<K extends string, C extends JsonObjectCompatible<C> = JsonObject>(
kind: K extends ComponentKind ? never : K,
config?: C,
options?: {
enabled?: boolean;
},
): OpenComponentSpec<C>;
Comment thread
willkill07 marked this conversation as resolved.
Resolved
/**
* Initialize the core-owned static and dynamic plugin host.
*
Expand Down
65 changes: 27 additions & 38 deletions crates/node/root-types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@ type BareObject<V> = V extends unknown
: never
: never
: never;
type JsonShaped<T> = [Extract<T, NonJsonValue> | BareObject<T>] extends [never] ? T : never;
type Shaped<T, Mapped, Excluded = NonJsonValue> = [Extract<T, Excluded> | BareObject<T>] extends [never]
? Mapped
: never;
type JsonProperty<V> = [unknown] extends [V]
? unknown
: V extends JsonValue
Expand Down Expand Up @@ -84,53 +86,43 @@ interface EventSanitizeFieldsOpen {

export type JsonCompatible<T> =
| JsonValue
| ([T] extends [{} | null]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: JsonProperty<T[K]> }
: never);
| ([T] extends [{} | null] ? Shaped<T, { [K in keyof T]: JsonProperty<T[K]> }> : never);
export type JsonObjectCompatible<T> =
| JsonObject
| ([T] extends [readonly unknown[]]
? never
: [T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: JsonProperty<T[K]> }
: never);
| ([T] extends [object]
? Shaped<T, { [K in keyof T]: JsonProperty<T[K]> }, NonJsonValue | readonly unknown[]>
: never);
export type LlmRequestCompatible<T> =
| LlmRequestOpen
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: K extends 'headers' ? JsonObjectProperty<T[K]> : JsonProperty<T[K]> } & Record<
? Shaped<
T,
{ [K in keyof T]: K extends 'headers' ? JsonObjectProperty<T[K]> : JsonProperty<T[K]> } & Record<
'headers' | 'content',
unknown
>
>
: never);
export type ToolExecutionResultCompatible<T> =
| ToolExecutionResultOpen
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: JsonProperty<T[K]> } & Record<'result', unknown>
: never);
| ([T] extends [object] ? Shaped<T, { [K in keyof T]: JsonProperty<T[K]> } & Record<'result', unknown>> : never);
export type ToolExecutionInterceptOutcomeCompatible<T> =
| ToolExecutionInterceptOutcome
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: K extends 'pendingMarks' ? PendingMarksProperty<T[K]> : JsonProperty<T[K]> } & Record<
? Shaped<
T,
{ [K in keyof T]: K extends 'pendingMarks' ? PendingMarksProperty<T[K]> : JsonProperty<T[K]> } & Record<
'result',
unknown
>
>
: never);
export type LlmRequestInterceptOutcomeCompatible<T> =
| LlmRequestInterceptOutcome
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: {
? Shaped<
T,
{
[K in keyof T]: K extends 'request'
? LlmRequestProperty<T[K]>
: K extends 'pendingMarks'
Expand All @@ -139,21 +131,14 @@ export type LlmRequestInterceptOutcomeCompatible<T> =
? LlmOptimizationContribution[]
: JsonProperty<T[K]>;
} & Record<'request', unknown>
>
: never);
export type EventSanitizeFieldsCompatible<T> =
| EventSanitizeFieldsOpen
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: JsonProperty<T[K]> }
: never);
| ([T] extends [object] ? Shaped<T, { [K in keyof T]: JsonProperty<T[K]> }> : never);
export type EventMetadataCompatible<T> =
| EventMetadata
| ([T] extends [object]
? [JsonShaped<T>] extends [never]
? never
: { [K in keyof T]: EventMetadataProperty<T[K]> }
: never);
| ([T] extends [object] ? Shaped<T, { [K in keyof T]: EventMetadataProperty<T[K]> }> : never);
export type MetricMeasurementsCompatible<T> =
| readonly MetricMeasurement[]
| ([T] extends [readonly unknown[]] ? { [I in keyof T]: MetricMeasurementProperty<T[I]> } : never);
Expand Down Expand Up @@ -618,7 +603,11 @@ export interface PluginContext {
name: string,
priority: number,
breakChain: boolean,
callback: (args: { name: string; request: LlmRequestShape; annotated: Json | null }) => TOutcome | Promise<TOutcome>,
callback: (args: {
name: string;
request: LlmRequestShape;
annotated: Json | null;
}) => TOutcome | Promise<TOutcome>,
): void;
registerLlmExecutionIntercept<TResponse extends JsonCompatible<TResponse>>(
name: string,
Expand Down
4 changes: 2 additions & 2 deletions crates/node/src/api/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ fn cleanup_node_environment() {

/// Emit a structured operational log record through Relay's configured sinks.
#[napi(
ts_generic_types = "TFields extends JsonCompatible<TFields> = Json",
ts_generic_types = "TFields extends JsonObjectCompatible<TFields> = JsonObject",
catch_unwind
)]
pub fn log(
Expand All @@ -144,7 +144,7 @@ macro_rules! log_levels {
($($level:ident: $doc:literal,)*) => {$(
#[doc = $doc]
#[napi(
ts_generic_types = "TFields extends JsonCompatible<TFields> = Json",
ts_generic_types = "TFields extends JsonObjectCompatible<TFields> = JsonObject",
catch_unwind
)]
pub fn $level(
Expand Down
9 changes: 9 additions & 0 deletions crates/node/tests/model_pricing_tests.mjs
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,15 @@ describe('model pricing plugin helpers', () => {
);
});

it('parses untyped documents into configs', () => {
const document = { sources: [pricing.inlineSource(pricing.inlineCatalog([pricedEntry()]))] };
assert.equal(pricing.parseConfig(document), document);
assert.throws(
() => pricing.parseConfig({ sources: 1 }),
(error) => /invalid model pricing config/.test(error.message) && error.cause[0].code === 'pricing.invalid_config',
);
});

it('accepts and validates cache write rates', () => {
const report = (rate) =>
pricing.validateConfig(
Expand Down
Loading
Loading