import { ObjectId } from "../index"; import { ObjectId as MongoObjectId } from "mongodb"; import { MIS_SystemInstruction, MIS_TOOLS, QUERY_TYPES, } from "./instructions.types"; import { getSystemInstructionsCollection } from "./instructions.getters"; /** * Validate system instruction data * @param data partial system instruction data * @throws Error if tool or queryType is invalid */ const validateInstructionData = (data: Partial) => { if ( data.tool && !(Object.values(MIS_TOOLS) as string[]).includes(data.tool) ) { throw new Error( `Invalid tool value: ${data.tool}. Allowed values are: ${Object.values(MIS_TOOLS).join(", ")}`, ); } if ( data.queryType && !(Object.values(QUERY_TYPES) as string[]).includes(data.queryType) ) { throw new Error( `Invalid queryType value: ${data.queryType}. Allowed values are: ${Object.values(QUERY_TYPES).join(", ")}`, ); } }; /** * Create a new system instruction * @param instructionData system instruction data * @returns inserted system instruction id */ export const createSystemInstruction = async ( instructionData: Omit< MIS_SystemInstruction, "_id" | "createdAt" | "updatedAt" >, ): Promise => { validateInstructionData(instructionData as Partial); const instruction: Omit = { //append doc metadata to user provided data ...instructionData, createdAt: new Date(), updatedAt: new Date(), }; const { insertedId } = await getSystemInstructionsCollection().insertOne( instruction as MIS_SystemInstruction, ); return insertedId; }; /** * Update a system instruction by id * @param id system instruction id * @param data partial system instruction data * @returns updated system instruction or null if not found */ export const updateSystemInstruction = async ( id: string, data: Partial>, ): Promise => { validateInstructionData(data as Partial); const result = await getSystemInstructionsCollection().findOneAndUpdate( { _id: new ObjectId(id) }, { $set: { ...data, updatedAt: new Date() } }, { returnDocument: "after" }, ); return result || null; }; /** * Delete a system instruction by id * @param id system instruction id * @returns true if deleted, false otherwise */ export const deleteSystemInstruction = async (id: string): Promise => { const result = await getSystemInstructionsCollection().deleteOne({ _id: new ObjectId(id), }); return result.deletedCount > 0; }; /** * Activate a system instruction by id, sets isActive to true. * Does not check the current instruction state, calling this on an active instruction will do nothing. * @param id system instruction id * @returns true on success, false otherwise */ export const activateSystemInstruction = async ( id: string, ): Promise => { const result = await getSystemInstructionsCollection().findOneAndUpdate( { _id: new ObjectId(id) }, { $set: { isActive: true } }, { returnDocument: "after" }, ); return Boolean(result); }; /** * Deactivate a system instruction by id, sets isActive to false. * Does not check the current instruction state, calling this on a deactivated instruction will do nothing. * @param id system instruction id * @returns true on success, false otherwise */ export const deactivateSystemInstruction = async ( id: string, ): Promise => { const result = await getSystemInstructionsCollection().findOneAndUpdate( { _id: new ObjectId(id) }, { $set: { isActive: false } }, { returnDocument: "after" }, ); return Boolean(result); };