Skip to content

@codesoul-co/hypha-models / router

Using this module

Use the Router module for using the public contracts and operations for this capability boundary. It exports 3 classes, 2 functions, 4 interfaces, 1 type.

Import from the package entrypoint

ts
import {
  ModelAliasRegistry,
  ModelProviderError,
  ModelRouter,
  normalizeModelProviderError,
  parseModelTarget,
} from '@codesoul-co/hypha-models';

import type {
  ModelProviderErrorInit,
  ModelRouterOptions,
  NormalizedProviderErrorContext,
  ResolvedModelRoute,
  ModelProviderErrorCode,
} from '@codesoul-co/hypha-models';

Usage patterns

  • Use the 5 type/interface exports as static contracts in application code, adapters, or tests. Import them with import type; they do not exist at runtime.
  • The module exposes 3 classes as constructable runtime implementations. Each symbol entry lists its constructor and public methods.
  • The module exposes 2 functions as direct operation entrypoints. Every overload, required/optional parameter, and return type is documented below.

Public exports

SymbolKindSignatureDescription
ModelAliasRegistryclassnew ModelAliasRegistry(aliases?: ModelAliasSpec[]): ModelAliasRegistryModel Alias Registry class with 5 public constructor or member entries; its exact declarations are listed below.
ModelProviderErrorclassnew ModelProviderError(init: ModelProviderErrorInit): ModelProviderErrorModel Provider Error class with 15 public constructor or member entries; its exact declarations are listed below.
ModelRouterclassnew ModelRouter(options: ModelRouterOptions): ModelRouterModel Router class with 7 public constructor or member entries; its exact declarations are listed below.
normalizeModelProviderErrorfunctionnormalizeModelProviderError(error: unknown, context?: NormalizedProviderErrorContext): ModelProviderErrorNormalize Model Provider Error function with 1 public call signature; parameters and return types are listed below.
parseModelTargetfunctionparseModelTarget(target: string): Pick<ResolvedModelRoute, "providerId" | "providerModel"> | nullParse Model Target function with 1 public call signature; parameters and return types are listed below.
ModelProviderErrorInitinterfaceinterface ModelProviderErrorInitModel Provider Error Init interface with 8 public fields or methods.
ModelRouterOptionsinterfaceinterface ModelRouterOptionsModel Router Options interface with 5 public fields or methods.
NormalizedProviderErrorContextinterfaceinterface NormalizedProviderErrorContextNormalized Provider Error Context interface with 3 public fields or methods.
ResolvedModelRouteinterfaceinterface ResolvedModelRouteResolved Model Route interface with 4 public fields or methods.
ModelProviderErrorCodetypetype ModelProviderErrorCode = 'MODEL_PROVIDER_ERROR' | 'MODEL_PROVIDER_HTTP_ERROR' | 'MODEL_PROVIDER_TIMEOUT' | 'MODEL_PROVIDER_RATE_LIMITED' | 'MODEL_PROVIDER_AUTH_FAILED' | 'MODEL_PROVIDER_BAD_REQUEST' | 'MODEL_PROVIDER_STREAM_ERROR' | 'MODEL_PROVIDER_NOT_FOUND' | 'MODEL_ALIAS_NOT_FOUND' | 'MODEL_ROUTING_FAILED'Public type alias for Model Provider Error Code; the declaration contains its complete type expression.

ModelAliasRegistry

Model Alias Registry class with 5 public constructor or member entries; its exact declarations are listed below.

  • Kind: class
  • Import: import { ModelAliasRegistry } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export declare class ModelAliasRegistry {
    constructor(aliases?: ModelAliasSpec[]);
    register(alias: ModelAliasSpec): void;
    registerTarget(alias: string, target: string, version?: string): ModelAliasSpec;
    resolve(aliasOrTarget: string): ResolvedModelRoute;
    list(): ModelAliasSpec[];
}

Public members

MemberKindSignatureDescription
constructorconstructor(aliases?: ModelAliasSpec[]): ModelAliasRegistryCreates an instance of this class.
listmethodlist(): ModelAliasSpec[]Public method; parameters and return type are shown in the signature.
registermethodregister(alias: ModelAliasSpec): voidPublic method; parameters and return type are shown in the signature.
registerTargetmethodregisterTarget(alias: string, target: string, version?: string): ModelAliasSpecPublic method; parameters and return type are shown in the signature.
resolvemethodresolve(aliasOrTarget: string): ResolvedModelRoutePublic method; parameters and return type are shown in the signature.

ModelProviderError

Model Provider Error class with 15 public constructor or member entries; its exact declarations are listed below.

  • Kind: class
  • Import: import { ModelProviderError } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export declare class ModelProviderError extends FrameworkError {
    readonly code: ModelProviderErrorCode;
    readonly providerId?: string;
    readonly modelAlias?: string;
    readonly status?: number;
    readonly retryable: boolean;
    readonly raw?: unknown;
    constructor(init: ModelProviderErrorInit);
}

Public members

MemberKindSignatureDescription
causepropertyreadonly cause?: unknownPublic property; its type, readonly modifier and optionality are shown in the signature.
codepropertyreadonly code: ModelProviderErrorCodePublic property; its type, readonly modifier and optionality are shown in the signature.
constructorconstructor(init: ModelProviderErrorInit): ModelProviderErrorCreates an instance of this class.
contextpropertyreadonly context?: Record<string, unknown>Public property; its type, readonly modifier and optionality are shown in the signature.
messagepropertymessage: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
modelAliaspropertyreadonly modelAlias?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
namepropertyname: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
providerIdpropertyreadonly providerId?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
rawpropertyreadonly raw?: unknownPublic property; its type, readonly modifier and optionality are shown in the signature.
retryablepropertyreadonly retryable: booleanPublic property; its type, readonly modifier and optionality are shown in the signature.
stackpropertystack?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
static captureStackTracemethodstatic captureStackTrace(targetObject: object, constructorOpt?: Function): voidCreates a .stack property on targetObject, which when accessed returns a string representing the location in the code at which Error.captureStackTrace() was called. js const myObject = {}; Error.captureStackTrace(myObject); myObject.stack; // Similar to `new Error().stack` The first line of the trace will be prefixed with ${myObject.name}: ${myObject.message}. The optional constructorOpt argument accepts a function. If given, all frames above constructorOpt, including constructorOpt, will be omitted from the generated stack trace. The constructorOpt argument is useful for hiding implementation details of error generation from the user. For instance: js function a() { b(); } function b() { c(); } function c() { // Create an error without stack trace to avoid calculating the stack trace twice. const { stackTraceLimit } = Error; Error.stackTraceLimit = 0; const error = new Error(); Error.stackTraceLimit = stackTraceLimit; // Capture the stack trace above function b Error.captureStackTrace(error, b); // Neither function c, nor b is included in the stack trace throw error; } a();
static prepareStackTracemethodstatic prepareStackTrace(err: Error, stackTraces: NodeJS.CallSite[]): anyPublic method; parameters and return type are shown in the signature.
static stackTraceLimitpropertystatic stackTraceLimit: numberThe Error.stackTraceLimit property specifies the number of stack frames collected by a stack trace (whether generated by new Error().stack or Error.captureStackTrace(obj)). The default value is 10 but may be set to any valid JavaScript number. Changes will affect any stack trace captured after the value has been changed. If set to a non-number value, or set to a negative number, stack traces will not capture any frames.
statuspropertyreadonly status?: numberPublic property; its type, readonly modifier and optionality are shown in the signature.

ModelRouter

Model Router class with 7 public constructor or member entries; its exact declarations are listed below.

  • Kind: class
  • Import: import { ModelRouter } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export declare class ModelRouter implements ModelProvider<ModelRequest, ModelResponse> {
    readonly id: string;
    constructor(options: ModelRouterOptions);
    capabilities(): ModelCapabilities;
    generate(request: ModelRequest): Promise<ModelResponse>;
    stream(request: ModelRequest): AsyncIterable<ModelStreamEvent>;
    resolve(aliasOrTarget: string): ResolvedModelRoute;
    listAliases(): ModelAliasSpec[];
}

Public members

MemberKindSignatureDescription
capabilitiesmethodcapabilities(): ModelCapabilitiesPublic method; parameters and return type are shown in the signature.
constructorconstructor(options: ModelRouterOptions): ModelRouterCreates an instance of this class.
generatemethodgenerate(request: ModelRequest): Promise<ModelResponse>Public method; parameters and return type are shown in the signature.
idpropertyreadonly id: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
listAliasesmethodlistAliases(): ModelAliasSpec[]Public method; parameters and return type are shown in the signature.
resolvemethodresolve(aliasOrTarget: string): ResolvedModelRoutePublic method; parameters and return type are shown in the signature.
streammethodstream(request: ModelRequest): AsyncIterable<ModelStreamEvent>Public method; parameters and return type are shown in the signature.

normalizeModelProviderError

Normalize Model Provider Error function with 1 public call signature; parameters and return types are listed below.

  • Kind: function
  • Import: import { normalizeModelProviderError } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export declare function normalizeModelProviderError(error: unknown, context?: NormalizedProviderErrorContext): ModelProviderError;

Call signature

text
normalizeModelProviderError(error: unknown, context?: NormalizedProviderErrorContext): ModelProviderError

Parameters

ParameterTypeRequiredDescription
errorunknownYesRequired parameter; accepted values are defined by the type column.
contextNormalizedProviderErrorContextNoOptional parameter; accepted values are defined by the type column.

Returns

  • Type: ModelProviderError
  • Description: The return contract is defined by the type shown above.

parseModelTarget

Parse Model Target function with 1 public call signature; parameters and return types are listed below.

  • Kind: function
  • Import: import { parseModelTarget } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export declare function parseModelTarget(target: string): Pick<ResolvedModelRoute, 'providerId' | 'providerModel'> | null;

Call signature

text
parseModelTarget(target: string): Pick<ResolvedModelRoute, "providerId" | "providerModel"> | null

Parameters

ParameterTypeRequiredDescription
targetstringYesRequired parameter; accepted values are defined by the type column.

Returns

  • Type: Pick<ResolvedModelRoute, "providerId" | "providerModel">
  • Description: The return contract is defined by the type shown above.

ModelProviderErrorInit

Model Provider Error Init interface with 8 public fields or methods.

  • Kind: interface
  • Import: import type { ModelProviderErrorInit } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export interface ModelProviderErrorInit {
    code: ModelProviderErrorCode;
    message: string;
    providerId?: string;
    modelAlias?: string;
    status?: number;
    retryable?: boolean;
    raw?: unknown;
    cause?: unknown;
}

Contract members

MemberKindSignatureDescription
causepropertycause?: unknownPublic property; its type, readonly modifier and optionality are shown in the signature.
codepropertycode: ModelProviderErrorCodePublic property; its type, readonly modifier and optionality are shown in the signature.
messagepropertymessage: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
modelAliaspropertymodelAlias?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
providerIdpropertyproviderId?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
rawpropertyraw?: unknownPublic property; its type, readonly modifier and optionality are shown in the signature.
retryablepropertyretryable?: booleanPublic property; its type, readonly modifier and optionality are shown in the signature.
statuspropertystatus?: numberPublic property; its type, readonly modifier and optionality are shown in the signature.

ModelRouterOptions

Model Router Options interface with 5 public fields or methods.

  • Kind: interface
  • Import: import type { ModelRouterOptions } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export interface ModelRouterOptions {
    id?: string;
    registry: Pick<ModelRegistry, 'get' | 'list'>;
    aliases?: ModelAliasSpec[];
    routing?: ModelRoutingSpec;
    fallbackAliases?: string[];
}

Contract members

MemberKindSignatureDescription
aliasespropertyaliases?: ModelAliasSpec[]Public property; its type, readonly modifier and optionality are shown in the signature.
fallbackAliasespropertyfallbackAliases?: string[]Public property; its type, readonly modifier and optionality are shown in the signature.
idpropertyid?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
registrypropertyregistry: Pick<ModelRegistry, "list" | "get">Public property; its type, readonly modifier and optionality are shown in the signature.
routingpropertyrouting?: ModelRoutingSpecPublic property; its type, readonly modifier and optionality are shown in the signature.

NormalizedProviderErrorContext

Normalized Provider Error Context interface with 3 public fields or methods.

  • Kind: interface
  • Import: import type { NormalizedProviderErrorContext } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export interface NormalizedProviderErrorContext {
    providerId?: string;
    modelAlias?: string;
    operation?: 'generate' | 'stream' | 'count_tokens' | 'health';
}

Contract members

MemberKindSignatureDescription
modelAliaspropertymodelAlias?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
operationpropertyoperation?: "generate" | "stream" | "count_tokens" | "health"Public property; its type, readonly modifier and optionality are shown in the signature.
providerIdpropertyproviderId?: stringPublic property; its type, readonly modifier and optionality are shown in the signature.

ResolvedModelRoute

Resolved Model Route interface with 4 public fields or methods.

  • Kind: interface
  • Import: import type { ResolvedModelRoute } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export interface ResolvedModelRoute {
    alias: string;
    providerId: string;
    providerModel: string;
    spec?: ModelAliasSpec;
}

Contract members

MemberKindSignatureDescription
aliaspropertyalias: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
providerIdpropertyproviderId: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
providerModelpropertyproviderModel: stringPublic property; its type, readonly modifier and optionality are shown in the signature.
specpropertyspec?: ModelAliasSpecPublic property; its type, readonly modifier and optionality are shown in the signature.

ModelProviderErrorCode

Public type alias for Model Provider Error Code; the declaration contains its complete type expression.

  • Kind: type
  • Import: import type { ModelProviderErrorCode } from '@codesoul-co/hypha-models';
  • Source module: router

Declaration

text
export type ModelProviderErrorCode = 'MODEL_PROVIDER_ERROR' | 'MODEL_PROVIDER_HTTP_ERROR' | 'MODEL_PROVIDER_TIMEOUT' | 'MODEL_PROVIDER_RATE_LIMITED' | 'MODEL_PROVIDER_AUTH_FAILED' | 'MODEL_PROVIDER_BAD_REQUEST' | 'MODEL_PROVIDER_STREAM_ERROR' | 'MODEL_PROVIDER_NOT_FOUND' | 'MODEL_ALIAS_NOT_FOUND' | 'MODEL_ROUTING_FAILED';