Make Hatchet enqueue optional in dev

This commit is contained in:
Ruslan Bakiev
2026-05-05 12:05:55 +07:00
parent 2357897530
commit f570b17dfb

View File

@@ -1,12 +1,40 @@
import { hatchet } from './hatchet-client.js';
import { workflowNames } from './workflow-names.js';
export type ProcessVoiceExperienceInput = {
experienceId: string;
};
type HatchetClientLike = {
run: (workflowName: string, input: Record<string, unknown>) => Promise<unknown>;
runNoWait?: (workflowName: string, input: Record<string, unknown>) => Promise<unknown>;
};
let hatchet: HatchetClientLike | null = null;
let initDone = false;
async function ensureHatchetClient(): Promise<HatchetClientLike | null> {
if (initDone) return hatchet;
initDone = true;
if (!process.env.HATCHET_CLIENT_TOKEN) {
console.warn('[hatchet] HATCHET_CLIENT_TOKEN is not set, workflow trigger skipped');
return null;
}
const sdk = await import('@hatchet-dev/typescript-sdk');
hatchet = sdk.HatchetClient.init() as unknown as HatchetClientLike;
return hatchet;
}
export async function enqueueVoiceExperience(
input: ProcessVoiceExperienceInput,
) {
await hatchet.admin.runWorkflow(workflowNames.processVoiceExperience, input);
const client = await ensureHatchetClient();
if (!client) return { triggered: false };
const runRef = client.runNoWait
? await client.runNoWait(workflowNames.processVoiceExperience, input)
: await client.run(workflowNames.processVoiceExperience, input);
return { triggered: true, runRef };
}