n8n-nodes-openai-analytics
Version:
n8n node for OpenAI Analytics
50 lines • 2.03 kB
JavaScript
;
Object.defineProperty(exports, "__esModule", { value: true });
exports.runThread = void 0;
async function runThread(context) {
const { openai, functionThis, i } = context;
const threadId = functionThis.getNodeParameter('threadId', i);
const assistantSelection = functionThis.getNodeParameter('assistantSelection', i);
let assistantId = '';
if (assistantSelection === 'byId') {
assistantId = functionThis.getNodeParameter('assistantId', i);
}
else {
assistantId = functionThis.getNodeParameter('assistantId', i);
}
const waitForCompletion = functionThis.getNodeParameter('waitForCompletion', i);
const instructions = functionThis.getNodeParameter('instructions', i, '');
// Run the thread
const runParams = {
assistant_id: assistantId
};
if (instructions)
runParams.instructions = instructions;
const run = await openai.beta.threads.runs.create(threadId, runParams);
if (!waitForCompletion) {
return {
json: { ...run }
};
}
else {
// Poll for completion
const maxPollTime = functionThis.getNodeParameter('maxPollTime', i, 60);
const maxEndTime = Date.now() + maxPollTime * 1000;
let runStatus = run;
while (['in_progress', 'queued', 'requires_action'].includes(runStatus.status) &&
Date.now() < maxEndTime) {
// Wait for 1 second between each check
await new Promise((resolve) => setTimeout(resolve, 1000));
runStatus = await openai.beta.threads.runs.retrieve(threadId, run.id);
}
const responseData = { ...runStatus };
// If requested, append messages to the response
if (runStatus.status === 'completed') {
const messages = await openai.beta.threads.messages.list(threadId);
responseData.messages = messages.data;
}
return { json: responseData };
}
}
exports.runThread = runThread;
//# sourceMappingURL=runThread.js.map