Skip to content

Commit 9a0fef2

Browse files
committed
fixing bad bug
1 parent 5e9ff60 commit 9a0fef2

File tree

4 files changed

+53
-15
lines changed

4 files changed

+53
-15
lines changed

src/app/api/assistant/templates.ts

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,10 @@ Each item describes a different property (or facet) of the scene, based on its c
5858
- Remember, if the director is asking to edit the video project data structure, you MUST only return the item object, in JSON format.
5959
- If you don't understand how to modify, it's okay to say you don't understand and politely ask for clarification.
6060
- When you edit a JSON list, sure to recopy the id for each field exactly like it is in the original, otherwise it breaks everything.
61-
- The director might give a query in English, French, Spanish.. but the movie scene is in English.
61+
- The director might give a query in French, English, Spanish.. but the movie scene is in English.
6262
- ALWAYS write the output in English: if the query is in another language, translate it to English.
63+
- When updating a scene (with UPDATE_STORY_AND_SCENE) never forget to update the updatedSceneSegments array!
64+
- Also when updating a scene segments, NEVER, EVER FORGET ABOUT THE CAMERA SEGMENTS! BEcause this is how we actually split our scene into separate shots!
6365
- Important: if the director is asking a QUESTION ("who is.. what is.. please analyze etc..") then DO NOT return JSON, but raw text instead`
6466

6567
export const examples = `

src/services/assistant/useAssistant.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -37,8 +37,6 @@ import { parseRawInputToAction } from './parseRawInputToAction'
3737
import { useAudio } from '../audio/useAudio'
3838
import { updateStoryAndScene } from './updateStoryAndScene'
3939

40-
const enableTextToSpeech = false
41-
4240
export const useAssistant = create<AssistantStore>((set, get) => ({
4341
...getDefaultAssistantState(),
4442

@@ -182,6 +180,8 @@ export const useAssistant = create<AssistantStore>((set, get) => ({
182180
return
183181
}
184182

183+
console.log('processUserMessage():', input)
184+
185185
const { addEventToHistory, processActionOrMessage } = get()
186186
const {
187187
bufferedSegments: { activeSegments },

src/services/assistant/useVoiceAssistant.ts

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,11 +14,9 @@ export function useVoiceAssistant() {
1414
const stop = useMic((s) => s.stop)
1515
const clear = useMic((s) => s.clear)
1616

17-
const debouncedTranscript = useDebounce(transcript, 1200)
18-
1917
useEffect(() => {
20-
processUserMessage(debouncedTranscript)
21-
}, [debouncedTranscript, processUserMessage])
18+
processUserMessage(transcript)
19+
}, [transcript, processUserMessage])
2220

2321
return {
2422
isSupported,

src/services/mic/useMic.ts

Lines changed: 46 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@ import { MicStore } from '@aitube/clapper-services'
55

66
import { getDefaultMicState } from './getDefaultMicState'
77

8+
const cutoffTimeInMs = 1200
9+
810
export const useMic = create<MicStore>((set, get) => ({
911
...getDefaultMicState(),
1012

@@ -28,20 +30,50 @@ export const useMic = create<MicStore>((set, get) => ({
2830
})
2931
}
3032

31-
recognition.interimResults = interimResults
33+
recognition.interimResults = true
3234
recognition.lang = lang
33-
recognition.continuous = continuous
35+
recognition.continuous = true
3436

3537
const speechRecognitionList = new window.webkitSpeechGrammarList()
3638
speechRecognitionList.addFromString(grammar, grammarWeight)
3739
recognition.grammars = speechRecognitionList
3840

41+
let debounceTimer: NodeJS.Timeout | null = null
42+
let lastCompleteTranscript = ''
43+
let currentTranscript = ''
44+
let lastSpeechTime = Date.now()
45+
3946
const handleResult = (event: SpeechRecognitionEvent) => {
40-
let transcript = ''
41-
for (let i = 0; i < event.results.length; i++) {
42-
transcript += event.results?.[i]?.[0]?.transcript || ''
47+
const currentTime = Date.now()
48+
49+
// Check if it's been more than $cutoffTimeInMs since the last speech
50+
if (currentTime - lastSpeechTime > cutoffTimeInMs) {
51+
lastCompleteTranscript = ''
52+
currentTranscript = ''
53+
}
54+
55+
lastSpeechTime = currentTime
56+
57+
// Get the most recent result
58+
const latestResult = event.results[event.results.length - 1]
59+
currentTranscript = latestResult[0].transcript.trim()
60+
61+
// If it's a final result, update lastCompleteTranscript
62+
if (latestResult.isFinal) {
63+
lastCompleteTranscript = currentTranscript
4364
}
44-
set({ transcript })
65+
66+
const fullTranscript = lastCompleteTranscript +
67+
(currentTranscript !== lastCompleteTranscript ? ' ' + currentTranscript : '')
68+
69+
if (debounceTimer) {
70+
clearTimeout(debounceTimer)
71+
}
72+
73+
debounceTimer = setTimeout(() => {
74+
set({ transcript: fullTranscript.trim() })
75+
debounceTimer = null
76+
}, cutoffTimeInMs)
4577
}
4678

4779
const handleError = (event: SpeechRecognitionErrorEvent) => {
@@ -54,7 +86,13 @@ export const useMic = create<MicStore>((set, get) => ({
5486
}
5587

5688
const handleEnd = () => {
57-
set({ isListening: false, transcript: '' })
89+
if (debounceTimer) {
90+
clearTimeout(debounceTimer)
91+
const fullTranscript = lastCompleteTranscript +
92+
(currentTranscript !== lastCompleteTranscript ? ' ' + currentTranscript : '')
93+
set({ transcript: fullTranscript.trim() })
94+
}
95+
set({ isListening: false })
5896
}
5997

6098
recognition.addEventListener('result', handleResult)
@@ -86,4 +124,4 @@ export const useMic = create<MicStore>((set, get) => ({
86124

87125
if (typeof window !== 'undefined') {
88126
useMic.getState().init()
89-
}
127+
}

0 commit comments

Comments
 (0)