Skip to main content

API Reference

Every public symbol exported from @nest-native/ai-sdk.

Module

AiModule.forRoot(options?)

Registers the module with synchronous configuration. Returns a global DynamicModule by default.

AiModule.forRoot({
isGlobal: true,
defaultHeaders: { 'x-powered-by': 'nest-native-ai-sdk' },
onError: (error) => 'An error occurred.',
});

AiModule.forRootAsync(options?)

Registers the module with configuration resolved through a factory.

AiModule.forRootAsync({
imports: [ConfigModule],
inject: [ConfigService],
useFactory: (config: ConfigService) => ({
defaultHeaders: { 'x-app': config.getOrThrow('APP_NAME') },
}),
});

AI_MODULE_OPTIONS

The injection token for the resolved AiModuleOptions. Inject it to read the global configuration directly.

Decorators

@AiStream(options?)

Method decorator. Turns a Nest HTTP handler into an AI SDK streaming endpoint. See @AiStream.

@AiAbortSignal()

Parameter decorator. Injects an AbortSignal that fires on client disconnect. See @AiAbortSignal.

@AiContext()

Parameter decorator. Injects a request-scoped AiExecutionContext ({ request, response, signal }) so an AI SDK tool's execute closure can reach the current request mid-stream. See @AiContext.

Interfaces and Types

AiModuleOptions

FieldTypeDefaultDescription
isGlobalbooleantrueRegister the module globally.
defaultHeadersRecord<string, string>Headers applied to every @AiStream response. Method headers override matching keys.
onErrorAiStreamErrorMapperAI SDK defaultDefault in-stream error mapper. Method-level onError overrides it. ui-message only.

AiModuleAsyncOptions

FieldTypeDefaultDescription
isGlobalbooleantrueRegister the module globally.
importsModuleMetadata['imports'][]Modules to import for the factory.
injectany[][]Providers injected into useFactory.
extraProvidersProvider[][]Extra providers registered alongside the options.
useFactory(...args) => AiModuleOptions | Promise<AiModuleOptions>requiredResolves the options.

AiStreamOptions

FieldTypeDefaultDescription
formatAiStreamFormat'ui-message'Wire format.
headersRecord<string, string>Headers merged over module defaults; method keys win.
statusnumber200HTTP status for the streaming response.
onErrorAiStreamErrorMappermodule defaultIn-stream error mapper. ui-message only.

AiStreamFormat

'ui-message' | 'text'. See Stream Formats.

AiStreamErrorMapper

(error: unknown) => string. Maps an error thrown during stream production to the message carried by the AI SDK's in-stream error frame. Return only vetted, non-sensitive messages. See Error Mapping.

AiStreamResult

The structural shape @AiStream knows how to serialize. Both streamText and streamObject results satisfy it, so the package does not import concrete AI SDK classes (keeping ai a peer dependency).

interface AiStreamResult {
pipeUIMessageStreamToResponse?: (response: ServerResponse, init?: AiStreamResponseInit) => void;
pipeTextStreamToResponse?: (response: ServerResponse, init?: AiStreamResponseInit) => void;
}

AiStreamResponseInit

interface AiStreamResponseInit {
status?: number;
headers?: Record<string, string>;
onError?: AiStreamErrorMapper;
}

The init forwarded to the AI SDK's pipe*ToResponse helpers. onError is forwarded only for the ui-message format.

AiPlatformResponse and AiHttpServerResponse

Structural views of the platform response the package normalizes across adapters. AiPlatformResponse.raw is Fastify's underlying Node response; AiPlatformResponse.hijack is Fastify's reply.hijack(). Express omits both. See Adapters.

AiExecutionContext

The request-scoped context injected by @AiContext(). See @AiContext.

FieldTypeDescription
requestunknownThe adapter's request object (Express Request / Fastify FastifyRequest). Cast it to read request.user, headers, or params.
responseAiPlatformResponse | ServerResponseThe active platform response.
signalAbortSignalThe client-disconnect signal — the same one @AiAbortSignal() resolves.

What Is Not Exported

The package does not export model providers, prompt templates, tool registries, or agent state — those live in your application or another package. See the Roadmap for the scope boundary.