import Itzam from "itzam";
const itzam = new Itzam(process.env.ITZAM_API_KEY);
const workflowSlug = "turtle-chat";
// 1. Create a thread
export const createThread = async (userId: string) => {
const thread = await itzam.threads.create({
workflowSlug,
// You can add more lookup keys to find the thread later
lookupKeys: [userId],
});
return thread;
};
// 2. Send a message using the thread ID
export const sendMessage = async (message: string, threadId: string) => {
const response = await itzam.streamText({
input: message,
threadId, // This will automatically add the older messages
});
return response;
};
// 3. Fetch user threads by the user ID
export const getUserThreads = async (userId: string) => {
const threads = await itzam.threads.list(workflowSlug, {
lookupKeys: [userId],
});
return threads;
};
// 4. Fetch older messages by the thread ID
export const getThreadRuns = async (threadId: string) => {
const { runs } = await itzam.threads.getRuns(threadId);
return runs;
};