jorel
Version:
A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.
65 lines (64 loc) • 2.58 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAssistantMessage = exports.generateSystemMessage = exports.generateUserMessage = void 0;
const shared_1 = require("../shared");
const resolveMessageId = (option, timestamp) => {
if (option && typeof option === "object" && "messageId" in option) {
return option.messageId;
}
return (0, shared_1.generateMessageId)(option, timestamp);
};
const generateUserMessage = async (taskInput, idOption) => {
const id = resolveMessageId(idOption);
const createdAt = Date.now();
const baseMessage = {
id,
role: "user",
createdAt,
};
if (typeof taskInput === "string") {
return { ...baseMessage, content: [{ type: "text", text: taskInput }] };
}
const content = [];
for (const input of taskInput) {
if (typeof input === "string") {
content.push({ type: "text", text: input });
}
else {
content.push(await input.toMessageContent());
}
}
return {
...baseMessage,
content,
};
};
exports.generateUserMessage = generateUserMessage;
const generateSystemMessage = (systemMessage, documentSystemMessage, documents, idOption) => {
const id = resolveMessageId(idOption);
const createdAt = Date.now();
if (documents && documents.length > 0) {
if (!documentSystemMessage)
throw new Error("Document system message must be provided when documents are provided.");
if (!documentSystemMessage.includes("{{documents}}")) {
throw new Error("System message must include '{{documents}}' placeholder when documents are provided.");
}
return {
id,
role: "system",
content: systemMessage + "\n" + documentSystemMessage.replace("{{documents}}", documents.systemMessageRepresentation),
createdAt,
};
}
return { id, role: "system", content: systemMessage, createdAt };
};
exports.generateSystemMessage = generateSystemMessage;
const generateAssistantMessage = (content, reasoningContent = null, toolCalls, idOption) => {
const id = resolveMessageId(idOption);
const createdAt = Date.now();
if (!toolCalls || toolCalls.length === 0) {
return { id, role: "assistant", content: content || "", reasoningContent, createdAt };
}
return { id, role: "assistant_with_tools", content: content || null, toolCalls, reasoningContent, createdAt };
};
exports.generateAssistantMessage = generateAssistantMessage;