UNPKG

@maximai/maxim-js

Version:

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

50 lines (49 loc) 3.21 kB
import { LanguageModelV3GenerateResult, LanguageModelV3Prompt, LanguageModelV3StreamPart } from "ai-sdk-provider-v3"; import { ChatCompletionMessage, ChatCompletionResult, CompletionRequest, Generation, Span, Trace } from "index"; import type { MaximLogger } from "../../logger"; import { MaximVercelProviderMetadata } from "../types"; import type { Attachment } from "../../../types"; /** * Converts a LanguageModelV3Prompt into an array of CompletionRequest or ChatCompletionMessage objects. * * This function transforms the structured prompt format used by the Vercel AI SDK v3 into the message format expected by downstream consumers, handling system, user, assistant, and tool roles. * It also extracts file attachments from file messages. * * @param prompt - The prompt to be parsed, consisting of structured message parts. * @returns An object containing parsed messages and extracted file attachments. * @throws If an unsupported user message type is encountered. */ export declare function parsePromptMessagesV3(prompt: LanguageModelV3Prompt): { messages: Array<CompletionRequest | ChatCompletionMessage>; attachments: Attachment[]; }; /** * Processes tool results from the raw prompt and logs them to Maxim. * Calls toolCallError for error-type results (error-text, error-json) and toolCallResult for successes. * * @param prompt - The raw LanguageModelV3 prompt containing tool results * @param logger - The MaximLogger instance for logging tool results/errors */ export declare function processToolResultsFromPromptV3(prompt: LanguageModelV3Prompt, logger: MaximLogger): void; /** * Converts a doGenerate result object into a ChatCompletionResult format. * * This function adapts the result of a language model generation v3 (including token usage, model info, and choices) into the standardized ChatCompletionResult structure expected by downstream consumers. * * @param result - The result object from a generation call, including usage, response, and content fields. * @returns The formatted chat completion result, including id, model, choices, and token usage. */ export declare function convertDoGenerateResultToChatCompletionResultV3(result: LanguageModelV3GenerateResult): ChatCompletionResult; /** * Processes a stream of language model output chunks and logs the result to Maxim tracing. * * This function aggregates streamed output parts, constructs a chat completion result, and finalizes the generation, span, and trace as appropriate. It also handles errors and ensures proper cleanup of tracing resources. * * @param chunks - The array of streamed output parts from the language model. * @param span - The Maxim tracing span associated with this generation. * @param trace - The Maxim tracing trace associated with this generation. * @param generation - The Maxim generation object to log the result to. * @param model - The model identifier used for this generation. * @param maximMetadata - Optional Maxim metadata for advanced tracing. */ export declare function processStreamV3(chunks: LanguageModelV3StreamPart[], span: Span, trace: Trace, generation: Generation, model: string, maximMetadata: MaximVercelProviderMetadata | undefined): void;