ai-utils.js
Version:
Build AI applications, chatbots, and agents with JavaScript and TypeScript.
37 lines (36 loc) • 1.64 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.validateChatPrompt = exports.ChatPromptValidationError = void 0;
class ChatPromptValidationError extends Error {
constructor(message) {
super(message);
this.name = "ChatPromptValidationError";
}
}
exports.ChatPromptValidationError = ChatPromptValidationError;
/**
* Checks if a chat prompt is valid. Throws a `ChatPromptValidationError` if it's not.
*/
function validateChatPrompt(chatPrompt) {
if (chatPrompt.length < 1) {
throw new ChatPromptValidationError("ChatPrompt should have at least one message.");
}
const initialType = "system" in chatPrompt[0] ? "system" : "user";
if (initialType === "system" && chatPrompt.length === 1) {
throw new ChatPromptValidationError("A system message should be followed by a user message.");
}
let expectedType = initialType === "system" ? "user" : "ai";
for (let i = 1; i < chatPrompt.length; i++) {
const messageType = "user" in chatPrompt[i] ? "user" : "ai";
if (messageType !== expectedType) {
throw new ChatPromptValidationError(`Message at index ${i} should be a ${expectedType} message, but it's a ${messageType} message.`);
}
// Flip the expected type for the next iteration.
expectedType = expectedType === "user" ? "ai" : "user";
}
// If the last message is not a user message, throw an error.
if (expectedType !== "ai") {
throw new ChatPromptValidationError("The last message should be a user message.");
}
}
exports.validateChatPrompt = validateChatPrompt;