UNPKG

@maximai/maxim-js

Version:

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

69 lines (68 loc) 3.88 kB
import { LanguageModelV1Prompt, LanguageModelV1StreamPart } from "ai-sdk-provider-v1"; import { ChatCompletionMessage, ChatCompletionResult, CompletionRequest, Generation, Span, Trace } from "index"; import type { MaximLogger } from "../../logger"; import type { ToolCallError } from "../../components/toolCall"; import { DoGenerateResultLike } from "../types"; import { MaximVercelProviderMetadata } from "../types"; /** Canonical shape for a normalized tool part (supports both output and result formats). */ export interface NormalizedToolPart { toolCallId: string; isError: boolean; content: string; errorInfo?: ToolCallError; } /** * Normalizes a V1 prompt tool part (which may have output or result) into a canonical shape. * Used by both logging (processToolResultsFromPromptV1) and transcript generation (parsePromptMessages). */ export declare function normalizeToolPart(part: { toolCallId: string; output?: { type: string; value: unknown; }; result?: unknown; }): NormalizedToolPart; /** * Converts a LanguageModelV1Prompt into an array of CompletionRequest or ChatCompletionMessage objects. * * This function transforms the structured prompt format used by the Vercel AI SDK into the message format expected by downstream consumers, handling system, user, assistant, and tool roles. * * @param prompt - The prompt to be parsed, consisting of structured message parts. * @returns An array of parsed messages suitable for completion requests or chat completions. * @throws If an unsupported user message type is encountered. */ export declare function parsePromptMessages(prompt: LanguageModelV1Prompt): Array<CompletionRequest | ChatCompletionMessage>; /** * 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. * Supports both output-based format (V2/V3 style) and result-based format (V1 style). * * @param prompt - The raw LanguageModelV1 prompt containing tool results * @param logger - The MaximLogger instance for logging tool results/errors */ export declare function processToolResultsFromPromptV1(prompt: LanguageModelV1Prompt, logger: MaximLogger): void; /** * Converts a doGenerate result object into a ChatCompletionResult format. * * This function adapts the result of a language model generation (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 rawResponse fields. * @returns The formatted chat completion result, including id, model, choices, and token usage. */ export declare function convertDoGenerateResultToChatCompletionResult(result: DoGenerateResultLike & { [key: string]: any; }): 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 processStream(chunks: LanguageModelV1StreamPart[], span: Span, trace: Trace, generation: Generation, model: string, maximMetadata: MaximVercelProviderMetadata | undefined): void;