-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
10 changed files
with
227 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,29 @@ | ||
import type { VpsConfiguration } from '../../typings'; | ||
import { createNavigatorAudioProvider } from '../voice/listener/navigatorAudioProvider'; | ||
import { createVoiceListener } from '../voice/listener/voiceListener'; | ||
|
||
export type InifinteListenParams = Omit<VpsConfiguration, 'fakeVps' | 'logger' | 'getToken' | 'vpsToken'> & { | ||
token: string; | ||
voiceMeta: Record<string, unknown>; | ||
}; | ||
|
||
const worker = new Worker(new URL('./vps.worker.js', import.meta.url), { type: 'module' }); | ||
|
||
export function createInifiniteListen(config: InifinteListenParams, restApiUrl: string) { | ||
const listener = createVoiceListener( | ||
(cb, ...args) => createNavigatorAudioProvider(cb, ...args), | ||
(port: MessagePort) => { | ||
worker.postMessage({ type: 'start' }, [port]); | ||
}, | ||
); | ||
worker.postMessage({ type: 'init', params: [config, restApiUrl] }); | ||
|
||
return { | ||
start: () => { | ||
listener.listen(); | ||
}, | ||
stop: () => { | ||
listener.stop(); | ||
}, | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
import { v4 } from 'uuid'; | ||
|
||
import { createClient } from '../client/client'; | ||
import { createProtocol } from '../client/protocol'; | ||
import { createTransport } from '../client/transport'; | ||
|
||
function convertFieldValuesToString< | ||
Obj extends Record<string, unknown>, | ||
ObjStringified = { [key in keyof Obj]: string }, | ||
>(object: Obj): ObjStringified { | ||
return Object.keys(object).reduce((acc: Record<string, string>, key: string) => { | ||
if (object[key]) { | ||
acc[key] = | ||
typeof object[key] === 'string' && (object[key] as string).startsWith('{') | ||
? (object[key] as string) | ||
: JSON.stringify(object[key]); | ||
} | ||
return acc; | ||
}, {}) as ObjStringified; | ||
} | ||
|
||
let _client: ReturnType<typeof createClient>; | ||
let _stream: (chunk: Uint8Array, last: boolean) => void; | ||
let _push: (text: string) => void; | ||
|
||
function init({ token, voiceMeta, ...config }, restApiUrl: string) { | ||
const sessionId = v4(); | ||
const convertedVoiceMeta = convertFieldValuesToString(voiceMeta); | ||
const transport = createTransport({}); | ||
const protocol = createProtocol(transport, { ...config, getToken: () => token }); | ||
_client = createClient(protocol, undefined, { | ||
getVoiceMeta: () => convertedVoiceMeta, | ||
}); | ||
_push = (message: string) => | ||
fetch(restApiUrl, { | ||
method: 'POST', | ||
headers: { | ||
'Content-Type': 'application/json;charset=utf-8', | ||
}, | ||
body: JSON.stringify({ | ||
sessionId, | ||
message, | ||
timestamp: new Date().toISOString(), | ||
}), | ||
}); | ||
|
||
_client.on('stt', ({ text, response }) => { | ||
if (text?.data) { | ||
_push(text.data); | ||
return; | ||
} | ||
|
||
if (response) { | ||
const { decoderResultField } = response; | ||
|
||
if (decoderResultField?.hypothesis?.length) { | ||
_push(decoderResultField.hypothesis[0].normalizedText || ''); | ||
} | ||
} | ||
}); | ||
} | ||
|
||
function start(port: MessagePort) { | ||
_client | ||
.init() | ||
.then(() => { | ||
_client.createVoiceStream(({ sendVoice }) => { | ||
_stream = sendVoice; | ||
|
||
return Promise.resolve(); | ||
}, {}); | ||
}) | ||
.catch((err) => { | ||
console.error(err); | ||
}); | ||
|
||
port.onmessage = (event) => { | ||
_stream?.(event.data, false); | ||
}; | ||
} | ||
|
||
function handleChunk(chunk: Uint8Array, last: boolean) { | ||
_stream?.(chunk, last); | ||
} | ||
|
||
self.addEventListener('message', function (e) { | ||
switch (e.data.type) { | ||
case 'init': | ||
init(e.data.params[0], e.data.params[1]); | ||
break; | ||
case 'start': | ||
start(e.ports[0]); | ||
break; | ||
case 'chunk': | ||
handleChunk(e.data.params[0], e.data.params[1]); | ||
break; | ||
case 'stop': | ||
break; | ||
} | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.