Files
open-gsio/workers/site/workflows/preprocessing/executePreprocessingWorkflow.ts
geoffsee 33679583af init
2025-05-22 23:14:01 -04:00

55 lines
1.6 KiB
TypeScript

import { createPreprocessingWorkflow } from "./createPreprocessingWorkflow";
export async function executePreprocessingWorkflow({
latestUserMessage,
latestAiMessage,
eventHost,
streamId,
chat: { messages, openai },
}) {
console.log(`Executing executePreprocessingWorkflow`);
const initialState = { latestUserMessage, latestAiMessage };
// Add execution tracking flag to prevent duplicate runs
const executionKey = `preprocessing-${crypto.randomUUID()}`;
if (globalThis[executionKey]) {
console.log("Preventing duplicate preprocessing workflow execution");
return globalThis[executionKey];
}
const workflows = {
preprocessing: createPreprocessingWorkflow({
eventHost,
initialState,
streamId,
chat: { messages, openai },
}),
results: new Map(),
};
try {
// Store the promise to prevent parallel executions
globalThis[executionKey] = (async () => {
await workflows.preprocessing.navigate(latestUserMessage);
await workflows.preprocessing.executeWorkflow(latestUserMessage);
console.log(
`executePreprocessingWorkflow::workflow::preprocessing::results`,
{ state: JSON.stringify(workflows.preprocessing.state, null, 2) },
);
workflows.results.set("preprocessed", workflows.preprocessing.state);
// Cleanup after execution
setTimeout(() => {
delete globalThis[executionKey];
}, 1000);
return workflows;
})();
return await globalThis[executionKey];
} catch (error) {
delete globalThis[executionKey];
throw new Error("Workflow execution failed");
}
}