
import {defer, once} from "@e280/stz"
import {Comrade, Host} from "@e280/comrade"
import {AutomaticSpeechRecognitionPipeline, Pipeline} from "@huggingface/transformers"

import {loadPipe} from "../../parts/load-pipe.js"
import {transcribe} from "./parts/transcribe.js"
import {exposeErrors} from "../../parts/expose-errors.js"
import {TranscriberSchematic, TranscriberSpec} from "./types.js"

const deferred = defer<{pipe: Pipeline, spec: TranscriberSpec}>()

const makePrepare = (host: Host<TranscriberSchematic>) => once(async(spec: TranscriberSpec) => {
	deferred.resolve({
		spec,
		pipe: await loadPipe({
			spec,
			task: "automatic-speech-recognition",
			onLoading: loading => host.loading(loading),
		}) as AutomaticSpeechRecognitionPipeline
	})
})

await Comrade.worker<TranscriberSchematic>(shell => {
	const prepare = makePrepare(shell.host)
	return {
		prepare: exposeErrors(prepare),
		transcribe: exposeErrors(async(request) => {
			const {pipe, spec} = await deferred.promise
			return transcribe({
				pipe,
				spec,
				request,
				callbacks: {
					onReport: report => shell.host.deliverReport(report),
					onTranscription: transcription => shell.host.deliverTranscription(transcription),
				},
			})
		})
	}
})

