UNPKG

@juspay/neurolink

Version:

The pipe layer of an AI nervous system: one interface connecting provider neurons to your application, across three inference types — generate, stream and decide. `decide` returns typed, calibrated judgements from a non-generative model (~400ms, ~$0.00002

123 lines (122 loc) • 5.64 kB
import { BedrockRuntimeClient } from "@aws-sdk/client-bedrock-runtime"; import type { AIProviderName } from "../../constants/enums.js"; import { BaseProvider } from "../../core/baseProvider.js"; import type { NeuroLink } from "../../neurolink.js"; import type { EmbedInput, StreamOptions, StreamResult, EnhancedGenerateResult, TextGenerationOptions } from "../../types/index.js"; export declare class AmazonBedrockProvider extends BaseProvider { private bedrockClient; private conversationHistory; private region; /** * Parse the region segment from a Bedrock ARN. * Returns null when the input is not an ARN. * * Supports all AWS partitions: * - `arn:aws:bedrock:…` (commercial) * - `arn:aws-cn:bedrock:…` (China) * - `arn:aws-us-gov:bedrock:…` (GovCloud) */ private static extractRegionFromArn; constructor(modelName?: string, neurolink?: NeuroLink, region?: string, credentials?: { accessKeyId?: string; secretAccessKey?: string; sessionToken?: string; region?: string; }); /** * Perform initial health check to catch credential/connectivity issues early * This prevents the health check failure we saw in production logs */ private performInitialHealthCheck; getAISDKModel(): never; getProviderName(): AIProviderName; getDefaultModel(): string; /** * Get the default embedding model for Amazon Bedrock * @returns The default Bedrock embedding model name */ protected getDefaultEmbeddingModel(): string; generate(optionsOrPrompt: TextGenerationOptions | string): Promise<EnhancedGenerateResult | null>; private conversationLoop; private convertToAWSMessages; /** * `tools` is passed in rather than re-resolved from `getAllTools()`. That * call returns only the provider's own registry, so resolving here meant a * tool the caller passed to generate/stream could never execute — the * streaming path advertised it to the model and then failed every call to * it with "Tool not found", and the generate path never advertised it at * all. The turn's full merged tool set is resolved once by the caller and * handed down. */ private executeSingleTool; /** * Resolve the turn's tools once: whatever the caller passed, else the * provider's own registry. `BaseProvider.stream()` has already merged base * tools into `options.tools` by the time it reaches the streaming path; * the generate path has no such pre-merge, so it falls back here. */ private resolveTurnTools; /** * Present the resolved tools in the shape `runAgenticLoop` dispatches * through. Execution still goes through `executeSingleTool`, so the tool * span, the parameter defaults and the ToolResult unwrapping are unchanged * — only which tools are reachable changes. */ private toEngineTools; private convertAISDKToolsToToolDefinitions; private formatToolsForBedrock; private convertToBedrockMessages; getBedrockClient(): BedrockRuntimeClient; protected executeStream(options: StreamOptions): Promise<StreamResult>; /** * A multimodal turn skips model middleware (see modelBridge.ts's header). * WARN when middleware was configured, per request or on the provider, * since it silently did not run; multimodal turns alone are unremarkable, * so stay at debug otherwise. */ private logMultimodalMiddlewareSkip; /** * `inferenceConfig` carries `maxTokens` and `temperature` only, so any other * sampling option a middleware sets would be dropped without a trace. * `tools` is reported where it is compared, at each call site. */ private warnOnUnsentMiddlewareOptions; /** * Runs the turn behind `streamingConversationLoop`: the model-middleware * path for a text-only starting turn, or the direct fallback for a * multimodal one. Extracted only to keep `streamingConversationLoop` * readable — the branching logic itself is unchanged from what it * replaced. */ private buildBedrockStreamTurn; /** * Turns a settled loop outcome's usage into the shape both the analytics * resolution and the stream's own `finally` block report. Shared so the * two call sites in `streamingConversationLoop` can't drift. */ private summarizeBedrockStreamUsage; private streamingConversationLoop; /** * Health check for Amazon Bedrock service * Uses ListFoundationModels API to validate connectivity and permissions */ checkBedrockHealth(): Promise<void>; protected formatProviderError(error: unknown): Error; /** * Generate embeddings for text or multi-modal input using Amazon Bedrock. * Supports both text-only models (Titan Embed Text) and multi-modal models * (Titan Embed Image, Nova Multimodal Embeddings). * * @param input - Text string or EmbedInput with text/image/mimeType * @param modelName - The embedding model to use (default: amazon.titan-embed-text-v2:0) * @returns Promise resolving to the embedding vector */ embed(input: string | EmbedInput, modelName?: string): Promise<number[]>; /** * Generate embeddings for multiple texts in a single batch * @param texts - The texts to embed * @param modelName - The embedding model to use (default: amazon.titan-embed-text-v2:0) * @returns Promise resolving to an array of embedding vectors */ embedMany(texts: string[], modelName?: string): Promise<number[][]>; }