From 5752a23826b55d2fe52830da2e5f5384ca28298a Mon Sep 17 00:00:00 2001 From: Evgeniy Semin Date: Wed, 4 Oct 2023 17:38:46 +0300 Subject: [PATCH 1/3] =?UTF-8?q?chore:=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BD?= =?UTF-8?q?=D1=8B=20window?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assistantSdk/assistant.ts | 4 ++-- src/assistantSdk/client/protocol.ts | 11 +++++---- src/assistantSdk/client/transport.ts | 23 +++++++++++-------- src/assistantSdk/voice/audioContext.ts | 13 ++++++----- .../voice/listener/navigatorAudioProvider.ts | 2 +- 5 files changed, 31 insertions(+), 22 deletions(-) diff --git a/src/assistantSdk/assistant.ts b/src/assistantSdk/assistant.ts index de3993cce9..efa318c2b6 100644 --- a/src/assistantSdk/assistant.ts +++ b/src/assistantSdk/assistant.ts @@ -70,7 +70,7 @@ const promiseTimeout = (promise: Promise, timeout: number): Promise => return Promise.race([ promise.then((v) => { if (timeoutId) { - window.clearTimeout(timeoutId); + clearTimeout(timeoutId); } return v; }), @@ -405,7 +405,7 @@ export const createAssistant = ({ const { command } = items[i]; if (typeof command !== 'undefined') { - window.setTimeout(() => emit('command', command)); + setTimeout(() => emit('command', command)); if (command.type === 'start_music_recognition') { voice.shazam(); diff --git a/src/assistantSdk/client/protocol.ts b/src/assistantSdk/client/protocol.ts index 0eea4494c5..305d5211fc 100644 --- a/src/assistantSdk/client/protocol.ts +++ b/src/assistantSdk/client/protocol.ts @@ -275,11 +275,11 @@ export const createProtocol = ( status = 'connected'; - window.clearTimeout(clearReadyTimer); + clearTimeout(clearReadyTimer); /// считаем коннект = ready, если по истечении таймаута сокет не был разорван /// т.к бек может разрывать сокет, если с settings что-то не так - clearReadyTimer = window.setTimeout(() => { + clearReadyTimer = setTimeout(() => { if (status !== 'connected') { return; } @@ -294,7 +294,7 @@ export const createProtocol = ( status = 'ready'; emit('ready'); - }, 250); + }, 250) as unknown as number; logger?.({ type: 'init', params: { ...configuration, ...currentSettings } }); }), @@ -343,7 +343,10 @@ export const createProtocol = ( }, init: () => { // в отличии от reconnect не обрывает коннект если он в порядке - if (status === 'ready' && window.navigator.onLine) { + if ( + (status === 'ready' && typeof window !== 'undefined' && window.navigator.onLine) || + typeof window === 'undefined' + ) { return Promise.resolve(); } diff --git a/src/assistantSdk/client/transport.ts b/src/assistantSdk/client/transport.ts index 7d545dcbae..95a626b33b 100644 --- a/src/assistantSdk/client/transport.ts +++ b/src/assistantSdk/client/transport.ts @@ -16,19 +16,24 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C const { on, emit } = createNanoEvents(); let hasCert = !checkCertUrl; - let retryTimeoutId = -1; + let retryTimeoutId: unknown = -1; let retries = 0; let status: 'closed' | 'closing' | 'connecting' | 'open' = 'closed'; let webSocket: WebSocket; let stopped = true; - const checkCert = (checkUrl: string) => - new Promise((resolve) => { + const checkCert = (checkUrl: string) => { + if (typeof window === 'undefined') { + return Promise.resolve(true); + } + + return new Promise((resolve) => { window .fetch(checkUrl) .then(() => resolve(true)) .catch(() => resolve(false)); }); + }; const close = () => { stopped = true; @@ -46,7 +51,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C status = 'connecting'; emit('connecting'); - if (!hasCert && window.navigator.onLine) { + if ((!hasCert && typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined') { const okay = await checkCert(checkCertUrl!); if (!okay) { @@ -70,7 +75,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return; } - window.clearTimeout(retryTimeoutId); + clearTimeout(retryTimeoutId as number); retries = 0; @@ -90,10 +95,10 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C // пробуем переподключаться, если возникла ошибка при коннекте if (!webSocket || (webSocket.readyState === 3 && !stopped)) { - window.clearTimeout(retryTimeoutId); + clearTimeout(retryTimeoutId as number); if (retries < 2) { - retryTimeoutId = window.setTimeout(() => { + retryTimeoutId = setTimeout(() => { // eslint-disable-next-line @typescript-eslint/no-use-before-define open(url); @@ -129,7 +134,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return; } - window.setTimeout(() => reconnect(url)); + setTimeout(() => reconnect(url)); close(); }; @@ -141,7 +146,7 @@ export const createTransport = ({ createWS = defaultWSCreator, checkCertUrl }: C return { close, get isOnline() { - return window.navigator.onLine; + return (typeof window !== 'undefined' && window.navigator.onLine) || typeof window === 'undefined'; }, on, open, diff --git a/src/assistantSdk/voice/audioContext.ts b/src/assistantSdk/voice/audioContext.ts index 0c6fb93c1d..13f326e93c 100644 --- a/src/assistantSdk/voice/audioContext.ts +++ b/src/assistantSdk/voice/audioContext.ts @@ -8,16 +8,17 @@ export const isAudioSupported = typeof window !== 'undefined' && (window.AudioCo * @returns AudioContext */ export const createAudioContext = (options?: AudioContextOptions): AudioContext => { - if (window.AudioContext) { - return new AudioContext(options); + if (!isAudioSupported) { + throw new Error('Audio not supported'); } - if (window.webkitAudioContext) { - // eslint-disable-next-line new-cap - return new window.webkitAudioContext(); + if (window.AudioContext) { + return new AudioContext(options); } - throw new Error('Audio not supported'); + // @ts-ignore + // eslint-disable-next-line new-cap + return new window.webkitAudioContext(); }; interface ContextEvents { diff --git a/src/assistantSdk/voice/listener/navigatorAudioProvider.ts b/src/assistantSdk/voice/listener/navigatorAudioProvider.ts index c2c08303ea..a825fe5ad1 100644 --- a/src/assistantSdk/voice/listener/navigatorAudioProvider.ts +++ b/src/assistantSdk/voice/listener/navigatorAudioProvider.ts @@ -132,7 +132,7 @@ export const createNavigatorAudioProvider = (cb: (buffer: ArrayBuffer, last: boo return createAudioRecorder(stream, cb); }) .catch((err) => { - if (window.location.protocol === 'http:') { + if (typeof window !== 'undefined' && window.location.protocol === 'http:') { throw new Error('Audio is supported only on a secure connection'); } From 242aee370ae508867de10ba25c0f1a4697db00a4 Mon Sep 17 00:00:00 2001 From: Evgeniy Semin Date: Mon, 9 Oct 2023 14:58:35 +0300 Subject: [PATCH 2/3] =?UTF-8?q?chore:=20=D0=A3=D0=B1=D1=80=D0=B0=D0=BD=20L?= =?UTF-8?q?ong?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/assistantSdk/assistant.ts | 3 +-- src/assistantSdk/client/client.ts | 10 ++++------ src/assistantSdk/meta.ts | 6 ++---- src/assistantSdk/voice/voice.ts | 2 +- src/typings.ts | 5 ++--- 5 files changed, 10 insertions(+), 16 deletions(-) diff --git a/src/assistantSdk/assistant.ts b/src/assistantSdk/assistant.ts index efa318c2b6..39b607befe 100644 --- a/src/assistantSdk/assistant.ts +++ b/src/assistantSdk/assistant.ts @@ -1,6 +1,5 @@ /* eslint-disable camelcase */ import { ActionCommand } from '@salutejs/scenario'; -import Long from 'long'; import { createNanoEvents } from '../nanoevents'; import { @@ -314,7 +313,7 @@ export const createAssistant = ({ /** отправляет ответ на запрос доступа к местоположению и пр. меты */ const sendMetaForPermissionRequest = async ( - requestMessageId: number | Long, + requestMessageId: number, appInfo: AppInfo, items: PermissionType[], ) => { diff --git a/src/assistantSdk/client/client.ts b/src/assistantSdk/client/client.ts index db52fafd7d..24f0672e9d 100644 --- a/src/assistantSdk/client/client.ts +++ b/src/assistantSdk/client/client.ts @@ -1,5 +1,3 @@ -import Long from 'long'; - import { createNanoEvents } from '../../nanoevents'; import { SystemMessageDataType, @@ -35,7 +33,7 @@ export const createClient = ( const { on, emit } = createNanoEvents(); /** ждет ответ бека и возвращает данные из этого ответа */ - const waitForAnswer = (messageId: number | Long): Promise => + const waitForAnswer = (messageId: number): Promise => new Promise((resolve) => { const off = on('systemMessage', (systemMessageData, originalMessage) => { if ( @@ -50,7 +48,7 @@ export const createClient = ( }); /** отправляет произвольный systemMessage, не подкладывает мету */ - const sendData = (data: Record, messageName = '', meta?: MetaStringified): number | Long => { + const sendData = (data: Record, messageName = '', meta?: MetaStringified): number => { const messageId = protocol.getMessageId(); protocol.sendSystemMessage( @@ -109,7 +107,7 @@ export const createClient = ( appInfo: AppInfo, messageName = 'SERVER_ACTION', mode?: AssistantServerActionMode, - ): Promise => { + ): Promise => { const messageId = protocol.getMessageId(); // мету и server_action отправляем в одном systemMessage @@ -144,7 +142,7 @@ export const createClient = ( isSsml = false, shouldSendDisableDubbing?: boolean, additionalMeta?: AdditionalMeta, - ): Promise => { + ): Promise => { if (text.trim() === '') { return undefined; } diff --git a/src/assistantSdk/meta.ts b/src/assistantSdk/meta.ts index 462f119fca..d4becad8d9 100644 --- a/src/assistantSdk/meta.ts +++ b/src/assistantSdk/meta.ts @@ -1,6 +1,4 @@ /* eslint-disable camelcase */ -import Long from 'long'; - import { AppInfo, Meta, PermissionStatus, PermissionType, SystemMessageDataType } from '../typings'; export type Permission = Record; @@ -13,7 +11,7 @@ export type CommandResponse = Required> }; server_action: { action_id: 'command_response'; - request_message_id: number | Long; + request_message_id: number; command_response: { request_permissions?: { permissions: Array<{ @@ -56,7 +54,7 @@ export const getTime = (): Meta['time'] => ({ }); export const getAnswerForRequestPermissions = async ( - requestMessageId: number | Long, + requestMessageId: number, appInfo: AppInfo, items: PermissionType[], ): Promise => { diff --git a/src/assistantSdk/voice/voice.ts b/src/assistantSdk/voice/voice.ts index f2339e335c..36b62b96d6 100644 --- a/src/assistantSdk/voice/voice.ts +++ b/src/assistantSdk/voice/voice.ts @@ -197,7 +197,7 @@ export const createVoice = ( // гипотезы распознавания речи subscriptions.push( - speechRecognizer.on('hypotesis', (text: string, isLast: boolean, mid: number | Long) => { + speechRecognizer.on('hypotesis', (text: string, isLast: boolean, mid: number) => { emit({ asr: { text: listener.status === 'listen' && !settings.current.disableListening ? text : '', diff --git a/src/typings.ts b/src/typings.ts index ed278ee9da..8d98ee7213 100644 --- a/src/typings.ts +++ b/src/typings.ts @@ -1,4 +1,3 @@ -import Long from 'long'; import { Action, ActionCommand, @@ -484,7 +483,7 @@ export type SystemMessageDataType = { }; export interface OriginalMessageType { - messageId: number | Long; + messageId: number; last: number; messageName: string; token?: string | null; @@ -503,7 +502,7 @@ export interface OriginalMessageType { systemMessage?: { data?: string | null; } | null; - timestamp?: number | Long | null; + timestamp?: number | null; meta?: { [k: string]: string } | null; } From ad7cc284562da149a61f290bcf31f24382e7a806 Mon Sep 17 00:00:00 2001 From: Evgeniy Semin Date: Mon, 9 Oct 2023 15:19:48 +0300 Subject: [PATCH 3/3] =?UTF-8?q?chore:=20=D0=98=D1=81=D0=BF=D1=80=D0=B0?= =?UTF-8?q?=D0=B2=D0=BB=D0=B5=D0=BD=D0=B0=20=D1=81=D0=B1=D0=BE=D1=80=D0=BA?= =?UTF-8?q?=D0=B0=20d.ts=20=D0=B4=D0=BB=D1=8F=20asr?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- rollup.config.js | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/rollup.config.js b/rollup.config.js index 4688b8da02..14aef476e0 100644 --- a/rollup.config.js +++ b/rollup.config.js @@ -94,6 +94,14 @@ export default [ }), typescript({ outDir: 'esm', declaration: false, declarationMap: false, module: 'esnext' }), ...common.plugins, + copy({ + targets: [ + { + src: 'src/assistantSdk/voice/recognizers/asr/*.d.ts', + dest: 'dist/assistantSdk/voice/recognizers/asr', + }, + ], + }), ], }, {