jorel
Version:
A unified wrapper for working with LLMs from multiple providers, including streams, images, documents & automatic tool use.
59 lines (58 loc) • 2.4 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.generateAssistantMessage = exports.generateSystemMessage = exports.generateUserMessage = void 0;
const shared_1 = require("../shared");
const generateUserMessage = async (taskInput) => {
const baseMessage = {
id: (0, shared_1.generateUniqueId)(),
role: "user",
createdAt: Date.now(),
};
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 {
id: (0, shared_1.generateUniqueId)(),
role: "user",
content,
createdAt: Date.now(),
};
};
exports.generateUserMessage = generateUserMessage;
const generateSystemMessage = (systemMessage, documentSystemMessage, documents) => {
const id = (0, shared_1.generateUniqueId)();
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, messageId) => {
const id = messageId ?? (0, shared_1.generateUniqueId)();
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;