@maximai/maxim-js
Version:
Maxim AI JS SDK. Visit https://getmaxim.ai for more info.
147 lines • 5.44 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.determineProvider = determineProvider;
exports.extractModelParameters = extractModelParameters;
exports.extractMaximMetadataFromOptions = extractMaximMetadataFromOptions;
exports.extractErrorInfo = extractErrorInfo;
exports.parseToolResultOutput = parseToolResultOutput;
const uuid_1 = require("uuid");
/**
* Determines the provider type from a given model string.
*
* This function inspects the model identifier and returns a type-safe provider name (such as 'openai', 'bedrock', 'anthropic', etc.) based on known substrings in the model name.
* If no known provider is found, it defaults to 'openai'.
*
* @param model - The model identifier string to inspect.
* @returns The detected provider name.
*/
function determineProvider(model) {
const mapper = (param) => {
if (param.includes("azure"))
return "azure";
if (param.includes("azure_openai"))
return "azure";
if (param.includes("amazon_bedrock"))
return "bedrock";
if (param.includes("bedrock"))
return "bedrock";
if (param.includes("huggingface"))
return "huggingface";
if (param.includes("together"))
return "together";
if (param.includes("openai"))
return "openai";
if (param.includes("anthropic"))
return "anthropic";
if (param.includes("google"))
return "google";
if (param.includes("groq"))
return "groq";
if (param.includes("elevenlabs"))
return "elevenlabs";
return null;
};
const provider = mapper(model);
if (provider !== null) {
return provider;
}
return "openai";
}
/**
* Extracts supported model parameters from the given language model call options.
*
* This function pulls out relevant generation parameters (such as temperature, maxTokens, penalties, etc.) from the provided LanguageModelV1CallOptions object, returning them in a plain object for downstream use.
*
* @param options - The call options containing model parameters.
* @returns An object containing the extracted model parameters, including temperature, maxTokens, topP, topK, frequencyPenalty, stopSequences, seed, headers, presencePenalty, abortSignal, and responseFormat.
*/
function extractModelParameters(options) {
const params = {
temperature: options.temperature,
topP: options.topP,
topK: options.topK,
frequencyPenalty: options.frequencyPenalty,
stopSequences: options.stopSequences,
seed: options.seed,
headers: options.headers,
presencePenalty: options.presencePenalty,
abortSignal: options.abortSignal,
responseFormat: options.responseFormat,
};
return params;
}
/**
* Extracts Maxim-specific provider metadata from the given language model call options.
*
* This function retrieves the `maxim` metadata object from the `providerMetadata` field of the options, for advanced tracing and logging in Maxim's observability system.
*
* @param options - The call options containing provider metadata.
* @returns The extracted Maxim metadata with a guaranteed `spanId`, or undefined if not present.
*/
function extractMaximMetadataFromOptions(metadata) {
var _a;
if (!metadata || !metadata["maxim"])
return undefined;
const maximMetadata = metadata["maxim"];
return {
...maximMetadata,
spanId: (_a = maximMetadata.spanId) !== null && _a !== void 0 ? _a : (0, uuid_1.v4)(),
};
}
/**
* Extracts structured error information from any thrown value.
*
* Handles standard Error objects, API error objects (with code/type fields),
* plain strings, and unknown values — so generation.error() always receives
* a meaningful message instead of an empty object.
*
* @param error - The caught value from a catch block.
* @returns An object with message, and optionally code and type.
*/
function extractErrorInfo(error) {
if (error instanceof Error) {
return {
message: error.message,
type: error.name !== "Error" ? error.name : undefined,
code: error["code"],
};
}
if (typeof error === "string") {
return { message: error };
}
if (typeof error === "object" && error !== null) {
const err = error;
let message;
if (typeof err["message"] === "string") {
message = err["message"];
}
else {
try {
message = JSON.stringify(error);
}
catch {
message = err["message"] !== undefined ? String(err["message"]) : String(error);
}
}
return {
message,
code: typeof err["code"] === "string" ? err["code"] : undefined,
type: typeof err["type"] === "string" ? err["type"] : undefined,
};
}
return { message: String(error) };
}
function parseToolResultOutput(content) {
switch (content.type) {
case "text":
case "error-text":
return content.value;
case "json":
case "error-json":
case "content":
return JSON.stringify(content.value);
default:
throw new Error(`Unknown tool result type: ${content}`);
}
}
//# sourceMappingURL=utils.js.map