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
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
import type {
  AutomaticSpeechRecognitionPipeline,
  CausalLMOutputWithPast,
  GPT2Tokenizer,
  LlamaForCausalLM,
  PreTrainedModel,
  StoppingCriteriaList,
} from '@huggingface/transformers'
import type { Device, DType } from '@xsai-transformers/shared/types'
import type { GenerateOptions } from 'kokoro-js'
import type {
  WorkerMessageEventError,
  WorkerMessageEventInfo,
  WorkerMessageEventOutput,
  WorkerMessageEventProgress,
  WorkerMessageEventSetVoiceResponse,
  WorkerMessageEventStatus,
} from '../types/worker'

import {
  // VAD
  AutoModel,

  AutoModelForCausalLM,
  // LLM
  AutoTokenizer,
  InterruptableStoppingCriteria,
  pipeline,

  // Speech recognition
  Tensor,
  TextStreamer,
} from '@huggingface/transformers'
import { isWebGPUSupported } from 'gpuu/webgpu'
import { KokoroTTS, TextSplitterStream } from 'kokoro-js'

import {
  EXIT_THRESHOLD,
  INPUT_SAMPLE_RATE,
  MAX_BUFFER_DURATION,
  MAX_NUM_PREV_BUFFERS,
  MIN_SILENCE_DURATION_SAMPLES,
  MIN_SPEECH_DURATION_SAMPLES,
  SPEECH_PAD_SAMPLES,
  SPEECH_THRESHOLD,
} from '../constants'

interface Message {
  role: 'system' | 'user' | 'assistant'
  content: string
}

type Voices = GenerateOptions['voice']
export type PretrainedConfig = NonNullable<Parameters<typeof AutoModel.from_pretrained>[1]>['config']

const whisperDtypeMap: Record<Device, DType> = {
  webgpu: {
    encoder_model: 'fp32',
    decoder_model_merged: 'fp32',
  },
  wasm: {
    encoder_model: 'fp32',
    decoder_model_merged: 'q8',
  },
}

const model_id = 'onnx-community/Kokoro-82M-v1.0-ONNX'
let voice: Voices | undefined
let silero_vad: PreTrainedModel
let transcriber: AutomaticSpeechRecognitionPipeline
let tts: KokoroTTS

const SYSTEM_MESSAGE: Message = {
  role: 'system',
  content:
    'You\'re a helpful and conversational voice assistant. Keep your responses short, clear, and casual.',
}
let messages: Message[] = [SYSTEM_MESSAGE]
let past_key_values_cache: any = null
let stopping_criteria: InterruptableStoppingCriteria | null = null

// Global audio buffer to store incoming audio
const BUFFER = new Float32Array(MAX_BUFFER_DURATION * INPUT_SAMPLE_RATE)
let bufferPointer = 0

// Initial state for VAD
const sr = new Tensor('int64', [INPUT_SAMPLE_RATE], [])
let state = new Tensor('float32', new Float32Array(2 * 1 * 128), [2, 1, 128])

// Whether we are in the process of adding audio to the buffer
let isRecording = false
let isPlaying = false // new flag

let tokenizer: GPT2Tokenizer
let llm: LlamaForCausalLM

const prevBuffers: Float32Array[] = []

export async function loadModels() {
  tts = await KokoroTTS.from_pretrained(model_id, {
    dtype: 'fp32',
    device: 'webgpu',
  })

  const device = 'webgpu'
  globalThis.postMessage({ type: 'info', data: { message: `Using device: "${device}"` } } satisfies WorkerMessageEventInfo)
  globalThis.postMessage({ type: 'info', data: { message: 'Loading models...', duration: 'until_next' } } satisfies WorkerMessageEventInfo)

  // Load models
  silero_vad = await AutoModel.from_pretrained(
    'onnx-community/silero-vad',
    {
      config: { model_type: 'custom' } as PretrainedConfig,
      dtype: 'fp32', // Full-precision
      progress_callback: progress => globalThis.postMessage({ type: 'progress', data: { message: progress } } satisfies WorkerMessageEventProgress),
    },
  ).catch((error: Error) => {
    globalThis.postMessage({ type: 'error', data: { error, message: error.message } } satisfies WorkerMessageEventError<Error>)
    throw error
  })

  transcriber = await pipeline(
    'automatic-speech-recognition',
    'onnx-community/whisper-base', // or "onnx-community/moonshine-base-ONNX",
    {
      device,
      dtype: whisperDtypeMap[device as keyof typeof whisperDtypeMap],
      progress_callback: progress => globalThis.postMessage({ type: 'progress', data: { message: progress } } satisfies WorkerMessageEventProgress),
    },
  ).catch((error: Error) => {
    globalThis.postMessage({ type: 'error', data: { error, message: error.message } } satisfies WorkerMessageEventError<Error>)
    throw error
  })

  await transcriber(new Float32Array(INPUT_SAMPLE_RATE)) // Compile shaders

  llm = await AutoModelForCausalLM.from_pretrained(
    'HuggingFaceTB/SmolLM2-1.7B-Instruct',
    {
      dtype: await isWebGPUSupported() ? 'q4f16' : 'int8',
      device: await isWebGPUSupported() ? 'webgpu' : 'wasm',
      progress_callback: progress => globalThis.postMessage({ type: 'progress', data: { message: progress } } satisfies WorkerMessageEventProgress),
    },
  ).catch((error: Error) => {
    globalThis.postMessage({ type: 'error', data: { error, message: error.message } } satisfies WorkerMessageEventError<Error>)
    throw error
  })

  tokenizer = await AutoTokenizer.from_pretrained(
    'HuggingFaceTB/SmolLM2-1.7B-Instruct',
  ).catch((error: Error) => {
    globalThis.postMessage({ type: 'error', data: { error, message: error.message } } satisfies WorkerMessageEventError<Error>)
    throw error
  })

  await llm.generate({ ...tokenizer('x'), max_new_tokens: 1 }) // Compile shaders

  globalThis.postMessage({
    type: 'status',
    data: {
      status: 'ready',
      message: 'Ready!',
      voices: tts.voices,
    },
  } as WorkerMessageEventStatus)
}

loadModels()

/**
 * Perform Voice Activity Detection (VAD)
 * @param buffer The new audio buffer
 * @returns `true` if the buffer is speech, `false` otherwise.
 */
async function vad(buffer?: Float32Array): Promise<boolean> {
  if (!buffer) {
    // Possibly closed or interrupted
    return false
  }

  const input = new Tensor('float32', buffer, [1, buffer.length])

  const { stateN, output } = await silero_vad({ input, sr, state })
  state = stateN // Update state

  const isSpeech = output.data[0]

  // Use heuristics to determine if the buffer is speech or not
  return (
    // Case 1: We are above the threshold (definitely speech)
    isSpeech > SPEECH_THRESHOLD
    // Case 2: We are in the process of recording, and the probability is above the negative (exit) threshold
    || (isRecording && isSpeech >= EXIT_THRESHOLD)
  )
}

interface SpeechData {
  start: number
  end: number
  duration: number
}

type BatchEncodingItem = number[] | number[][] | Tensor
/**
 * Holds the output of the tokenizer's call function.
 */
interface BatchEncoding {
  /**
   * List of token ids to be fed to a model.
   */
  input_ids: BatchEncodingItem
  /**
   * List of indices specifying which tokens should be attended to by the model.
   */
  attention_mask: BatchEncodingItem
  /**
   * List of token type ids to be fed to a model.
   */
  token_type_ids?: BatchEncodingItem
}

/**
 * Transcribe the audio buffer
 * @param buffer The audio buffer
 * @param _data Additional data
 */
async function speechToSpeech(buffer: Float32Array, _data: SpeechData): Promise<void> {
  isPlaying = true

  // 1. Transcribe the audio from the user
  const result = await transcriber(buffer)
  const text = (result as { text: string }).text.trim()

  if (['', '[BLANK_AUDIO]'].includes(text)) {
    // If the transcription is empty or a blank audio, we skip the rest of the processing
    return
  }

  messages.push({ role: 'user', content: text })

  // Set up text-to-speech streaming
  const splitter = new TextSplitterStream()
  const stream = tts!.stream(splitter, { voice });
  (async () => {
    for await (const { text, audio } of stream) {
      globalThis.postMessage({ type: 'output', data: { text, result: audio } } satisfies WorkerMessageEventOutput)
    }
  })()

  // 2. Generate a response using the LLM
  const inputs = tokenizer.apply_chat_template(messages, {
    add_generation_prompt: true,
    return_dict: true,
  }) as BatchEncoding

  const streamer = new TextStreamer(tokenizer, {
    skip_prompt: true,
    skip_special_tokens: true,
    callback_function: (text: string) => {
      splitter.push(text)
    },
    token_callback_function: () => {},
  })

  stopping_criteria = new InterruptableStoppingCriteria()
  type GenerationFunctionParameters = Parameters<typeof llm.generate>[0] & Record<string, any>

  const generatedRes = await llm.generate({
    ...inputs,
    past_key_values: past_key_values_cache,
    do_sample: false, // TODO: do_sample: true is bugged (invalid data location on top-k sample)
    max_new_tokens: 1024,
    streamer,
    stopping_criteria: stopping_criteria as unknown as StoppingCriteriaList,
    return_dict_in_generate: true,
  } as GenerationFunctionParameters)

  const { past_key_values, sequences } = generatedRes as CausalLMOutputWithPast & { sequences: Tensor }
  past_key_values_cache = past_key_values

  // Finally, close the stream to signal that no more text will be added.
  splitter.close()

  const decoded = tokenizer.batch_decode(
    // TODO: fix null as any
    sequences.slice(null, [(inputs.input_ids as Tensor).dims[1], null as any]),
    { skip_special_tokens: true },
  )

  messages.push({ role: 'assistant', content: decoded[0] })
}

// Track the number of samples after the last speech chunk
let postSpeechSamples = 0
function resetAfterRecording(offset = 0): void {
  globalThis.postMessage({
    type: 'status',
    data: {
      status: 'recording_end',
      message: 'Transcribing...',
      duration: 'until_next',
    },
  } satisfies WorkerMessageEventStatus)

  BUFFER.fill(0, offset)
  bufferPointer = offset
  isRecording = false
  postSpeechSamples = 0
}

function dispatchForTranscriptionAndResetAudioBuffer(overflow?: Float32Array): void {
  // Get start and end time of the speech segment, minus the padding
  const now = Date.now()
  const end
    = now - ((postSpeechSamples + SPEECH_PAD_SAMPLES) / INPUT_SAMPLE_RATE) * 1000
  const start = end - (bufferPointer / INPUT_SAMPLE_RATE) * 1000
  const duration = end - start
  const overflowLength = overflow?.length ?? 0

  // Send the audio buffer to the worker
  const buffer = BUFFER.slice(0, bufferPointer + SPEECH_PAD_SAMPLES)

  const prevLength = prevBuffers.reduce((acc, b) => acc + b.length, 0)
  const paddedBuffer = new Float32Array(prevLength + buffer.length)
  let offset = 0
  for (const prev of prevBuffers) {
    paddedBuffer.set(prev, offset)
    offset += prev.length
  }
  paddedBuffer.set(buffer, offset)
  speechToSpeech(paddedBuffer, { start, end, duration })

  // Set overflow (if present) and reset the rest of the audio buffer
  if (overflow) {
    BUFFER.set(overflow, 0)
  }
  resetAfterRecording(overflowLength)
}

globalThis.onmessage = async (event: MessageEvent) => {
  const { type, buffer } = event.data

  // refuse new audio while playing back
  if (type === 'audio' && isPlaying)
    return

  switch (type) {
    case 'start_call': {
      const name = tts!.voices[voice ?? 'af_heart']?.name ?? 'Heart'
      greet(`Hey there, my name is ${name}! How can I help you today?`)
      return
    }
    case 'end_call':
      messages = [SYSTEM_MESSAGE]
      past_key_values_cache = null
      break
    case 'interrupt':
      stopping_criteria?.interrupt()
      return
    case 'set_voice':
      voice = event.data.voice

      globalThis.postMessage({
        type: 'set_voice_response',
        data: {
          ok: true,
        },
      } satisfies WorkerMessageEventSetVoiceResponse)

      return
    case 'playback_ended':
      isPlaying = false
      return
  }

  const wasRecording = isRecording // Save current state
  const isSpeech = await vad(buffer)

  if (!wasRecording && !isSpeech) {
    // We are not recording, and the buffer is not speech,
    // so we will probably discard the buffer. So, we insert
    // into a FIFO queue with maximum size of PREV_BUFFER_SIZE
    if (prevBuffers.length >= MAX_NUM_PREV_BUFFERS) {
      // If the queue is full, we discard the oldest buffer
      prevBuffers.shift()
    }
    prevBuffers.push(buffer)
    return
  }

  const remaining = BUFFER.length - bufferPointer
  if (buffer.length >= remaining) {
    // The buffer is larger than (or equal to) the remaining space in the global buffer,
    // so we perform transcription and copy the overflow to the global buffer
    BUFFER.set(buffer.subarray(0, remaining), bufferPointer)
    bufferPointer += remaining

    // Dispatch the audio buffer
    const overflow = buffer.subarray(remaining)
    dispatchForTranscriptionAndResetAudioBuffer(overflow)
    return
  }
  else {
    // The buffer is smaller than the remaining space in the global buffer,
    // so we copy it to the global buffer
    BUFFER.set(buffer, bufferPointer)
    bufferPointer += buffer.length
  }

  if (isSpeech) {
    if (!isRecording) {
      // Indicate start of recording
      globalThis.postMessage({
        type: 'status',
        data: {
          status: 'recording_start',
          message: 'Listening...',
          duration: 'until_next',
        },
      } satisfies WorkerMessageEventStatus)
    }

    // Start or continue recording
    isRecording = true
    postSpeechSamples = 0 // Reset the post-speech samples

    return
  }

  postSpeechSamples += buffer.length

  // At this point we're confident that we were recording (wasRecording === true), but the latest buffer is not speech.
  // So, we check whether we have reached the end of the current audio chunk.
  if (postSpeechSamples < MIN_SILENCE_DURATION_SAMPLES) {
    // There was a short pause, but not long enough to consider the end of a speech chunk
    // (e.g., the speaker took a breath), so we continue recording
    return
  }

  if (bufferPointer < MIN_SPEECH_DURATION_SAMPLES) {
    // The entire buffer (including the new chunk) is smaller than the minimum
    // duration of a speech chunk, so we can safely discard the buffer.
    resetAfterRecording()
    return
  }

  dispatchForTranscriptionAndResetAudioBuffer()
}

function greet(text: string): void {
  isPlaying = true

  const splitter = new TextSplitterStream()
  const stream = tts!.stream(splitter, { voice });

  (async () => {
    for await (const { text: chunkText, audio } of stream) {
      globalThis.postMessage({ type: 'output', data: { text: chunkText, result: audio } } satisfies WorkerMessageEventOutput)
    }
  })()

  splitter.push(text)
  splitter.close()
  messages.push({ role: 'assistant', content: text })
}