@untemps/react-vocal 2.0.0-beta.2 → 2.0.0-beta.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +7 -0
- package/dist/index.es.js +12 -1
- package/dist/index.es.js.map +1 -1
- package/dist/index.js +1 -1
- package/dist/index.js.map +1 -1
- package/dist/index.umd.js +1 -1
- package/dist/index.umd.js.map +1 -1
- package/package.json +1 -1
- package/src/components/Vocal.jsx +14 -3
- package/src/components/__tests__/Vocal.test.jsx +46 -0
- package/vitest.setup.js +3 -3
package/package.json
CHANGED
package/src/components/Vocal.jsx
CHANGED
|
@@ -82,11 +82,22 @@ const Vocal = ({
|
|
|
82
82
|
)
|
|
83
83
|
|
|
84
84
|
const _onResult = useCallback(
|
|
85
|
-
(event
|
|
85
|
+
(event) => {
|
|
86
|
+
const transcript = Array.from(event?.results ?? [], (segment) => {
|
|
87
|
+
let best = { confidence: -Infinity, transcript: '' }
|
|
88
|
+
for (let j = 0; j < segment.length; j++) {
|
|
89
|
+
const alt = segment[j]
|
|
90
|
+
if (alt.confidence === undefined || alt.confidence > best.confidence) {
|
|
91
|
+
best = alt
|
|
92
|
+
}
|
|
93
|
+
}
|
|
94
|
+
return best.transcript ?? ''
|
|
95
|
+
}).join('')
|
|
96
|
+
|
|
86
97
|
stopTimer()
|
|
87
98
|
stopRecognition()
|
|
88
|
-
triggerCommandRef.current(
|
|
89
|
-
propsRef.current.onResult?.(
|
|
99
|
+
triggerCommandRef.current(transcript)
|
|
100
|
+
propsRef.current.onResult?.(transcript, event)
|
|
90
101
|
},
|
|
91
102
|
[stopTimer, stopRecognition]
|
|
92
103
|
)
|
|
@@ -427,4 +427,50 @@ describe('Vocal', () => {
|
|
|
427
427
|
|
|
428
428
|
expect(onErrorV1).not.toHaveBeenCalled()
|
|
429
429
|
})
|
|
430
|
+
|
|
431
|
+
it('returns the most confident alternative when multiple alternatives are provided', async () => {
|
|
432
|
+
const onResult = vi.fn()
|
|
433
|
+
const recognition = new SpeechRecognitionWrapper()
|
|
434
|
+
const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
|
|
435
|
+
|
|
436
|
+
await act(async () => {
|
|
437
|
+
fireEvent.click(getByTestId('__vocal-root__'))
|
|
438
|
+
recognition.instance.say([[
|
|
439
|
+
{ transcript: 'bar', confidence: 0.4 },
|
|
440
|
+
{ transcript: 'foo', confidence: 0.9 },
|
|
441
|
+
{ transcript: 'baz', confidence: 0.1 },
|
|
442
|
+
]])
|
|
443
|
+
await waitFor(() => expect(onResult).toHaveBeenCalledWith('foo', expect.anything()))
|
|
444
|
+
})
|
|
445
|
+
})
|
|
446
|
+
|
|
447
|
+
it('joins all segments when multiple result segments are provided', async () => {
|
|
448
|
+
const onResult = vi.fn()
|
|
449
|
+
const recognition = new SpeechRecognitionWrapper()
|
|
450
|
+
const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
|
|
451
|
+
|
|
452
|
+
await act(async () => {
|
|
453
|
+
fireEvent.click(getByTestId('__vocal-root__'))
|
|
454
|
+
recognition.instance.say([
|
|
455
|
+
[{ transcript: 'hello ', confidence: 0.9 }],
|
|
456
|
+
[{ transcript: 'world', confidence: 0.8 }],
|
|
457
|
+
])
|
|
458
|
+
await waitFor(() => expect(onResult).toHaveBeenCalledWith('hello world', expect.anything()))
|
|
459
|
+
})
|
|
460
|
+
})
|
|
461
|
+
|
|
462
|
+
it('picks highest-confidence alternative per segment when multi-segment with multi-alternative', async () => {
|
|
463
|
+
const onResult = vi.fn()
|
|
464
|
+
const recognition = new SpeechRecognitionWrapper()
|
|
465
|
+
const { getByTestId } = render(getInstance({ __rsInstance: recognition, onResult }))
|
|
466
|
+
|
|
467
|
+
await act(async () => {
|
|
468
|
+
fireEvent.click(getByTestId('__vocal-root__'))
|
|
469
|
+
recognition.instance.say([
|
|
470
|
+
[{ transcript: 'good ', confidence: 0.8 }, { transcript: 'bad ', confidence: 0.2 }],
|
|
471
|
+
[{ transcript: 'day', confidence: 0.95 }, { transcript: 'dey', confidence: 0.3 }],
|
|
472
|
+
])
|
|
473
|
+
await waitFor(() => expect(onResult).toHaveBeenCalledWith('good day', expect.anything()))
|
|
474
|
+
})
|
|
475
|
+
})
|
|
430
476
|
})
|
package/vitest.setup.js
CHANGED
|
@@ -54,14 +54,14 @@ global.SpeechRecognition = vi.fn(function () {
|
|
|
54
54
|
abort: vi.fn(function () {
|
|
55
55
|
handlers.end?.()
|
|
56
56
|
}),
|
|
57
|
-
say: vi.fn(function (
|
|
57
|
+
say: vi.fn(function (input) {
|
|
58
58
|
handlers.speechstart?.()
|
|
59
59
|
|
|
60
60
|
const resultEvent = new Event('result')
|
|
61
61
|
resultEvent.resultIndex = 0
|
|
62
|
-
resultEvent.results = [[{ transcript:
|
|
62
|
+
resultEvent.results = Array.isArray(input) ? input : input ? [[{ transcript: input }]] : []
|
|
63
63
|
handlers.speechend?.()
|
|
64
|
-
if (
|
|
64
|
+
if (input) {
|
|
65
65
|
handlers.result?.(resultEvent)
|
|
66
66
|
} else {
|
|
67
67
|
handlers.nomatch?.()
|