@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
255 lines • 11.9 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.normalizeToolPart = normalizeToolPart;
exports.parsePromptMessages = parsePromptMessages;
exports.processToolResultsFromPromptV1 = processToolResultsFromPromptV1;
exports.convertDoGenerateResultToChatCompletionResult = convertDoGenerateResultToChatCompletionResult;
exports.processStream = processStream;
const utils_1 = require("../utils");
const uuid_1 = require("uuid");
/**
* 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).
*/
function normalizeToolPart(part) {
const toolCallId = part.toolCallId;
const output = part.output;
const result = part.result;
if (output && typeof output === "object" && "type" in output) {
const isError = output.type === "error-text" || output.type === "error-json";
if (isError) {
const errorInfo = (0, utils_1.extractErrorInfo)(output.value);
return {
toolCallId,
isError: true,
content: errorInfo.message,
errorInfo,
};
}
const content = (0, utils_1.parseToolResultOutput)(output);
return { toolCallId, isError: false, content };
}
const content = typeof result === "string" ? result : JSON.stringify(result !== null && result !== void 0 ? result : "");
return { toolCallId, isError: false, content };
}
/**
* 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.
*/
function parsePromptMessages(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) => {
switch (msg.type) {
case "text":
return {
type: "text",
text: msg.text,
};
case "image":
return {
type: "image_url",
image_url: {
url: msg.image.toString(),
},
};
default:
throw new Error(`Unsupported user message type: ${msg.type}`);
}
}),
},
];
}
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.args),
},
})),
},
];
}
case "tool": {
return promptMsg.content.map((part) => {
const normalized = normalizeToolPart(part);
return {
role: "tool",
tool_call_id: normalized.toolCallId,
content: normalized.content,
};
});
}
}
})
.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.
* 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
*/
function processToolResultsFromPromptV1(prompt, logger) {
for (const promptMsg of prompt) {
if (promptMsg.role !== "tool")
continue;
for (const part of promptMsg.content) {
const normalized = normalizeToolPart(part);
if (normalized.isError && normalized.errorInfo) {
logger.toolCallError(normalized.toolCallId, normalized.errorInfo);
}
else {
logger.toolCallResult(normalized.toolCallId, normalized.content);
}
}
}
}
/**
* 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.
*/
function convertDoGenerateResultToChatCompletionResult(result) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j, _k, _l, _m;
return {
id: (0, uuid_1.v4)(),
object: "chat_completion",
created: Math.floor(Date.now() / 1000),
model: (_d = (_b = (_a = result.response) === null || _a === void 0 ? void 0 : _a.model_id) !== null && _b !== void 0 ? _b : (_c = result.response) === null || _c === void 0 ? void 0 : _c.modelId) !== null && _d !== void 0 ? _d : "unknown",
choices: Array.isArray((_f = (_e = result.rawResponse) === null || _e === void 0 ? void 0 : _e.body) === null || _f === void 0 ? void 0 : _f.choices)
? (_h = (_g = result.rawResponse) === null || _g === void 0 ? void 0 : _g.body) === null || _h === void 0 ? void 0 : _h.choices
: Array.isArray((_k = (_j = result.rawResponse) === null || _j === void 0 ? void 0 : _j.body) === null || _k === void 0 ? void 0 : _k.content)
? (_m = (_l = result.rawResponse) === null || _l === void 0 ? void 0 : _l.body) === null || _m === void 0 ? void 0 : _m.content
: [],
usage: {
prompt_tokens: result.usage.promptTokens,
completion_tokens: result.usage.completionTokens,
total_tokens: result.usage.promptTokens + result.usage.completionTokens,
},
};
}
/**
* 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 processStream(chunks, span, trace, generation, model, maximMetadata) {
var _a, _b, _c, _d, _e, _f, _g, _h, _j;
try {
const result = processChunks(chunks);
generation.result({
id: (0, uuid_1.v4)(),
object: "chat_completion",
created: Math.floor(Date.now() / 1000),
model: model,
choices: [
{
index: 0,
text: result.text,
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
// Only end trace here if user explicitly provided traceId (they manage it)
// Otherwise, the wrapper will handle trace ending based on tool-call detection
}
}
/**
* 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 processChunks(chunks) {
let text = "";
const toolCalls = {};
let usage = undefined;
let finishReason = undefined;
for (const chunk of chunks) {
switch (chunk.type) {
case "text-delta":
text += chunk.textDelta;
break;
case "tool-call":
toolCalls[chunk.toolCallId] = chunk;
break;
case "tool-call-delta":
if (!toolCalls[chunk.toolCallId]) {
toolCalls[chunk.toolCallId] = {
toolCallType: chunk.toolCallType,
toolCallId: chunk.toolCallId,
toolName: chunk.toolName,
args: "",
};
}
toolCalls[chunk.toolCallId].args += chunk.argsTextDelta;
break;
case "finish":
usage = chunk.usage;
finishReason = chunk.finishReason;
break;
}
}
return { text, toolCalls: Object.values(toolCalls), usage, finishReason };
}
//# sourceMappingURL=utils.js.map