UNPKG

@maximai/maxim-js

Version:

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

314 lines 14.1 kB
"use strict"; Object.defineProperty(exports, "__esModule", { value: true }); exports.parsePromptMessagesV2 = parsePromptMessagesV2; exports.processToolResultsFromPromptV2 = processToolResultsFromPromptV2; exports.convertDoGenerateResultToChatCompletionResultV2 = convertDoGenerateResultToChatCompletionResultV2; exports.processStreamV2 = processStreamV2; const utils_1 = require("../utils"); const uuid_1 = require("uuid"); /** * Converts a LanguageModelV2Prompt into an array of CompletionRequest or ChatCompletionMessage objects. * * This function transforms the structured prompt format used by the Vercel AI SDK v2 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. */ function parsePromptMessagesV2(prompt) { const promptMessages = prompt .map((promptMsg) => { var _a; switch (promptMsg.role) { case "system": { return [ { role: "system", content: promptMsg.content, }, ]; } case "user": { return [ { role: "user", content: promptMsg.content.map((msg) => { var _a, _b; switch (msg.type) { case "text": return { type: "text", text: msg.text, }; case "file": return { type: "image_url", image_url: { url: (_a = msg.filename) !== null && _a !== void 0 ? _a : "", }, }; default: { // Try to extract type or serialize the message for better error reporting const msgType = (_b = msg === null || msg === void 0 ? void 0 : msg.type) !== null && _b !== void 0 ? _b : "unknown"; let msgString; try { msgString = JSON.stringify(msg); } catch { msgString = `[object with type: ${msgType}]`; } throw new Error(`Unsupported user message type: ${msgType} (${msgString})`); } } }), }, ]; } case "assistant": { const assistantText = promptMsg.content.find((msg) => msg.type === "text"); const assistantToolCalls = promptMsg.content.filter((msg) => msg.type === "tool-call"); return [ { role: "assistant", content: (_a = assistantText === null || assistantText === void 0 ? void 0 : assistantText.text) !== null && _a !== void 0 ? _a : null, tool_calls: assistantToolCalls.map((tool) => ({ id: tool.toolCallId, type: "function", function: { name: tool.toolName, arguments: JSON.stringify(tool.input), }, })), }, ]; } case "tool": { return promptMsg.content.map((part) => ({ role: "tool", tool_call_id: part.toolCallId, content: (0, utils_1.parseToolResultOutput)(part.output), })); } } }) .flat(); return promptMessages; } /** * 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 LanguageModelV2 prompt containing tool results * @param logger - The MaximLogger instance for logging tool results/errors */ function processToolResultsFromPromptV2(prompt, logger) { for (const promptMsg of prompt) { if (promptMsg.role !== "tool") continue; for (const part of promptMsg.content) { if (part.type !== "tool-result") continue; const toolCallId = part.toolCallId; const output = part.output; const isError = output.type === "error-text" || output.type === "error-json"; if (isError) { const errorInfo = (0, utils_1.extractErrorInfo)(output.value); logger.toolCallError(toolCallId, errorInfo); } else { const content = (0, utils_1.parseToolResultOutput)(output); logger.toolCallResult(toolCallId, content); } } } } /** * Converts a doGenerate result object into a ChatCompletionResult format. * * This function adapts the result of a language model generation v2 (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. */ function convertDoGenerateResultToChatCompletionResultV2(result) { var _a, _b, _c, _d, _e, _f; return { id: (0, uuid_1.v4)(), object: "chat_completion", created: Math.floor(Date.now() / 1000), model: (_b = (_a = result.response) === null || _a === void 0 ? void 0 : _a.modelId) !== null && _b !== void 0 ? _b : "unknown", choices: result.content.map((content, index) => { switch (content.type) { case "text": return { index, message: { content: content.text, role: "assistant", }, finish_reason: result.finishReason, }; case "file": return { index, message: { content: content.data, role: "assistant", }, finish_reason: result.finishReason, }; case "tool-call": return { index, logprobs: null, message: { content: null, role: "assistant", tool_calls: [ { id: content.toolCallId, type: "function", function: { name: content.toolName, arguments: content.input, }, }, ], }, finish_reason: result.finishReason, }; case "tool-result": return { index, logprobs: null, message: { content: typeof content.result === "string" ? content.result : JSON.stringify(content.result), role: "assistant", }, finish_reason: result.finishReason, }; case "source": return { index, logprobs: null, message: { content: content.sourceType === "url" ? content.url : content.title, role: "assistant", }, finish_reason: result.finishReason, }; default: return { index, logprobs: null, message: { content: JSON.stringify(content), role: "assistant", }, finish_reason: result.finishReason, }; } }), usage: { prompt_tokens: (_c = result.usage.inputTokens) !== null && _c !== void 0 ? _c : 0, completion_tokens: (_d = result.usage.outputTokens) !== null && _d !== void 0 ? _d : 0, total_tokens: ((_e = result.usage.inputTokens) !== null && _e !== void 0 ? _e : 0) + ((_f = result.usage.outputTokens) !== null && _f !== void 0 ? _f : 0), }, }; } /** * 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. */ function processStreamV2(chunks, span, trace, generation, model, maximMetadata) { var _a, _b, _c, _d, _e, _f, _g, _h, _j; try { const result = processChunksV2(chunks); generation.result({ id: (0, uuid_1.v4)(), object: "chat_completion", created: Math.floor(Date.now() / 1000), model: model, choices: [ { index: 0, message: { tool_calls: result.toolCalls.map((toolCall) => ({ id: toolCall.toolCallId, type: toolCall.type, function: { name: toolCall.toolName, arguments: toolCall.input, }, })), content: result.text, role: "assistant", }, finish_reason: (_a = result.finishReason) !== null && _a !== void 0 ? _a : "stop", logprobs: null, }, ], usage: { prompt_tokens: (_c = (_b = result.usage) === null || _b === void 0 ? void 0 : _b.promptTokens) !== null && _c !== void 0 ? _c : 0, completion_tokens: (_e = (_d = result.usage) === null || _d === void 0 ? void 0 : _d.completionTokens) !== null && _e !== void 0 ? _e : 0, total_tokens: ((_g = (_f = result.usage) === null || _f === void 0 ? void 0 : _f.promptTokens) !== null && _g !== void 0 ? _g : 0) + ((_j = (_h = result.usage) === null || _h === void 0 ? void 0 : _h.completionTokens) !== null && _j !== void 0 ? _j : 0), }, }); generation.end(); } catch (error) { generation.error({ message: error.message, }); console.error("[Maxim SDK] Logging failed:", error); } finally { span.end(); // Note: Trace ending is now handled by the wrapper to support tool-call sequences } } /** * Processes an array of streamed language model output chunks into a structured result. * * This function aggregates text, tool calls, token usage, and finish reason from the provided stream parts, returning a single object summarizing the output of the language model stream. * * @param chunks - The array of streamed output parts from the language model. * @returns An object containing the aggregated text, tool calls, token usage, and finish reason. */ function processChunksV2(chunks) { var _a, _b; let text = ""; const toolCalls = {}; let usage = undefined; let finishReason = undefined; for (const chunk of chunks) { switch (chunk.type) { case "text-delta": text += chunk.delta; break; case "tool-call": toolCalls[chunk.toolCallId] = chunk; break; case "tool-result": text += typeof chunk.result === "string" ? chunk.result : JSON.stringify(chunk.result); break; case "finish": usage = { promptTokens: (_a = chunk.usage.inputTokens) !== null && _a !== void 0 ? _a : 0, completionTokens: (_b = chunk.usage.outputTokens) !== null && _b !== void 0 ? _b : 0, }; finishReason = chunk.finishReason; break; } } return { text, toolCalls: Object.values(toolCalls), usage, finishReason }; } //# sourceMappingURL=utils.js.map