UNPKG

@maximai/maxim-js

Version:

Maxim AI JS SDK. Visit https://getmaxim.ai for more info.

158 lines (157 loc) 5.8 kB
import type { LanguageModelV1, LanguageModelV1CallOptions, LanguageModelV1StreamPart } from "ai-sdk-provider-v1"; import { MaximLogger } from "../../logger"; /** * A wrapper class that adds Maxim logging and tracing to a Vercel AI SDK language model. * * This class decorates a LanguageModelV1 instance, intercepting calls to provide * advanced observability, tracing, and logging via the MaximLogger. It is intended * for internal use by the wrapMaximAISDKModel function. * * @class * @template T - The type of the language model (must extend LanguageModelV1). * @param model - The Vercel AI SDK language model instance to wrap. * @param logger - The MaximLogger instance to use for tracing and logging. */ export declare class MaximAISDKWrapper implements LanguageModelV1 { private model; private logger; private currentTraceId; private currentTrace; private currentSession; private isInToolCallSequence; /** * @constructor * Creates a new MaximAISDKWrapper instance. * * @param model - The Vercel AI SDK language model instance to wrap. * @param logger - The MaximLogger instance to use for tracing and logging. */ constructor(model: LanguageModelV1, logger: MaximLogger); /** * Sets up Maxim logging and tracing for a model call. * * Extracts Maxim metadata, parses prompt messages, and initializes session, trace, and span objects. * Also logs user input to the trace if appropriate. * * @private * @param options - The call options for the model invocation. * @param promptMessages - The parsed prompt messages (used to detect tool calls). * @returns An object containing maximMetadata, trace, session, span, and promptMessages. */ private setupLogging; /** * Executes a text or object generation call with Maxim tracing and logging. * * This method is called internally by generateText and generateObject, and logs the generation * result, errors, and relevant metadata to Maxim. * * @param options - The call options for the model invocation. * @returns The result of the underlying model's doGenerate call. */ doGenerate(options: LanguageModelV1CallOptions): Promise<{ text?: string; reasoning?: string | Array<{ type: "text"; text: string; signature?: string; } | { type: "redacted"; data: string; }>; files?: Array<{ data: string | Uint8Array; mimeType: string; }>; toolCalls?: Array<import("ai-sdk-provider-v1").LanguageModelV1FunctionToolCall>; finishReason: import("ai-sdk-provider-v1").LanguageModelV1FinishReason; usage: { promptTokens: number; completionTokens: number; }; rawCall: { rawPrompt: unknown; rawSettings: Record<string, unknown>; }; rawResponse?: { headers?: Record<string, string>; body?: unknown; }; request?: { body?: string; }; response?: { id?: string; timestamp?: Date; modelId?: string; }; warnings?: import("ai-sdk-provider-v1").LanguageModelV1CallWarning[]; providerMetadata?: import("ai-sdk-provider-v1").LanguageModelV1ProviderMetadata; sources?: import("ai-sdk-provider-v1").LanguageModelV1Source[]; logprobs?: import("ai-sdk-provider-v1").LanguageModelV1LogProbs; }>; /** * Executes a streaming generation call with Maxim tracing and logging. * * This method is called internally by streamText and streamObject, and logs the streaming * result, errors, and relevant metadata to Maxim. * * @param options - The call options for the model invocation. * @returns The result of the underlying model's doStream call, with a wrapped stream. */ doStream(options: LanguageModelV1CallOptions): Promise<{ stream: import("stream/web").ReadableStream<LanguageModelV1StreamPart>; rawCall: { rawPrompt: unknown; rawSettings: Record<string, unknown>; }; rawResponse?: { headers?: Record<string, string>; }; request?: { body?: string; }; warnings?: Array<import("ai-sdk-provider-v1").LanguageModelV1CallWarning>; }>; /** * Returns the default object generation mode of the wrapped model. * * @returns The default object generation mode. */ get defaultObjectGenerationMode(): import("ai-sdk-provider-v1").LanguageModelV1ObjectGenerationMode; /** * Returns the model ID of the wrapped model. * * @returns The model ID. */ get modelId(): string; /** * Returns the provider name of the wrapped model. * * @returns The provider name. */ get provider(): string; /** * Returns the specification version of the wrapped model. * * @returns The specification version. */ get specificationVersion(): "v1"; /** * Indicates whether the wrapped model supports image URLs. * * @returns True if image URLs are supported, false otherwise. */ get supportsImageUrls(): boolean | undefined; /** * Indicates whether the wrapped model supports structured outputs. * * @returns True if structured outputs are supported, false otherwise. */ get supportsStructuredOutputs(): boolean | undefined; /** * Indicates whether the wrapped model supports URL input. * * @returns True if URL input is supported, false otherwise. */ get supportsUrl(): ((url: URL) => boolean) | undefined; }