From f570b17dfb76dfbe06a5f6ab9ef5e63fd0c3e760 Mon Sep 17 00:00:00 2001 From: Ruslan Bakiev <572431+veikab@users.noreply.github.com> Date: Tue, 5 May 2026 12:05:55 +0700 Subject: [PATCH] Make Hatchet enqueue optional in dev --- src/hatchet/enqueue-voice-experience.ts | 32 +++++++++++++++++++++++-- 1 file changed, 30 insertions(+), 2 deletions(-) diff --git a/src/hatchet/enqueue-voice-experience.ts b/src/hatchet/enqueue-voice-experience.ts index 93b1228..11e9b5b 100644 --- a/src/hatchet/enqueue-voice-experience.ts +++ b/src/hatchet/enqueue-voice-experience.ts @@ -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) => Promise; + runNoWait?: (workflowName: string, input: Record) => Promise; +}; + +let hatchet: HatchetClientLike | null = null; +let initDone = false; + +async function ensureHatchetClient(): Promise { + 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 }; }