Skip to content

Commit

Permalink
Send STREAMING_SET_PERSONA to parent (#9)
Browse files Browse the repository at this point in the history
* add basic tests (to be run locally)
* refactor types
* switch to `setup()` in `speechstate.ts`
* add CI for publishing PRs as npm packages with tag @pr<PR number>
  • Loading branch information
vladmaraev authored Aug 28, 2024
1 parent be7eed4 commit e5de8c6
Show file tree
Hide file tree
Showing 14 changed files with 3,667 additions and 500 deletions.
31 changes: 31 additions & 0 deletions .github/workflows/test.js.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
name: Compile and package

on:
pull_request:
branches: [ "master" ]
jobs:
build:

runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v4
- uses: cachix/install-nix-action@v27
with:
nix_path: nixpkgs=channel:nixos-24.05

- run: nix develop

- run: yarn --immutable
- run: yarn exec tsc
- run: npm version prerelease --no-git-tag-version --preid=${{ format('pr{0}-{1}-{2}', github.event.number, github.run_attempt, github.run_id) }}

- uses: actions/setup-node@v4
with:
node-version: '14.x'
registry-url: 'https://registry.npmjs.org'
- name: Publish to NPM registry
run: npm publish --tag ${{ format('pr{0}', github.event.number) }}
env:
NODE_AUTH_TOKEN: ${{ secrets.NPM_TOKEN }}

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,4 @@ yarn-error.log*
# pycharm
.idea
/.yarn/cache
/src/credentials.ts
Binary file added bun.lockb
Binary file not shown.
41 changes: 41 additions & 0 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

36 changes: 36 additions & 0 deletions flake.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
{
inputs = {
# nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable";
systems.url = "github:nix-systems/default";
};

outputs =
{ systems, nixpkgs, ... }@inputs:
let
eachSystem = f: nixpkgs.lib.genAttrs (import systems) (system: f nixpkgs.legacyPackages.${system});
in
{
devShells = eachSystem (pkgs: {
default = pkgs.mkShell {
buildInputs = [
pkgs.corepack
pkgs.nodejs

# You can set the major version of Node.js to a specific one instead
# of the default version
# pkgs.nodejs-22_x

# It is possible to use bun instead of node.
pkgs.bun

# Optionally, you can add yarn or pnpm for package management for node.
# pkgs.nodePackages.pnpm
pkgs.yarn

# pkgs.nodePackages.typescript
# pkgs.nodePackages.typescript-language-server
];
};
});
};
}
31 changes: 25 additions & 6 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,40 +5,59 @@
"homepage": "http://localhost/speechstate",
"main": "./dist/index.js",
"types": "./dist/index.d.ts",
"files": ["/dist"],
"files": [
"/dist"
],
"dependencies": {
"microsoft-cognitiveservices-speech-sdk": "^1.31.0",
"web-speech-cognitive-services": "^7.1.3",
"xstate": "^5.9.1"
"xstate": "^5.17.4"
},
"scripts": {
"dev": "vite",
"compile": "tsc",
"test": "jest --silent=false"
"test": "vitest",
"test:browser": "vitest"
},
"eslintConfig": {
"extends": ["react-app", "react-app/jest"]
"extends": [
"react-app",
"react-app/jest"
]
},
"browserslist": {
"production": [">0.2%", "not dead", "not op_mini all"],
"production": [
">0.2%",
"not dead",
"not op_mini all"
],
"development": [
"last 1 chrome version",
"last 1 firefox version",
"last 1 safari version"
]
},
"devDependencies": {
"@statelyai/inspect": "^0.2.5",
"@statelyai/inspect": "^0.4.0",
"@testing-library/dom": "^10.4.0",
"@types/jest": "^29.5.2",
"@types/webspeechapi": "^0.0.29",
"@vitest/browser": "^2.0.5",
"buffer": "^5.5.0||^6.0.0",
"jest": "^29.5.0",
"jest-environment-jsdom": "^29.5.0",
"ts-jest": "^29.1.0",
"typescript": "^5.0.4",
"vite": "^5.0.11",
"vitest": "^2.0.5",
"webdriverio": "^9.0.7",
"ws": "^8.16.0"
},
"trustedDependencies": [
"p-defer-es5",
"edgedriver",
"core-js-pure"
],
"packageManager": "[email protected]",
"description": "* SDK",
"directories": {
Expand Down
64 changes: 6 additions & 58 deletions src/asr.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,71 +7,19 @@ import {
raise,
cancel,
} from "xstate";
import { getToken } from "./getToken";

import createSpeechRecognitionPonyfill from "web-speech-cognitive-services/lib/SpeechServices/SpeechToText";

import {
RecogniseParameters,
ASRContext,
ASREvent,
ASRInit,
ASRPonyfillInput,
Hypothesis,
AzureSpeechCredentials,
AzureLanguageCredentials,
} from "./types";

interface MySpeechRecognition extends SpeechRecognition {
new ();
}
interface MySpeechGrammarList extends SpeechGrammarList {
new ();
}

type ASREvent =
| {
type: "READY";
value: {
wsaASR: MySpeechRecognition;
wsaGrammarList: MySpeechGrammarList;
};
}
| { type: "ERROR" }
| { type: "NOINPUT" }
| { type: "CONTROL" }
| {
type: "START";
value?: RecogniseParameters;
}
| { type: "STARTED"; value: { wsaASRinstance: MySpeechRecognition } }
| { type: "STARTSPEECH" }
| { type: "RECOGNISED" }
| { type: "RESULT"; value: Hypothesis[] };

interface ASRContext extends ASRInit {
azureAuthorizationToken?: string;
wsaASR?: MySpeechRecognition;
wsaASRinstance?: MySpeechRecognition;
wsaGrammarList?: MySpeechGrammarList;
result?: Hypothesis[];
nluResult?: any; // TODO
params?: RecogniseParameters;
}

interface ASRInit {
asrDefaultCompleteTimeout: number;
asrDefaultNoInputTimeout: number;
locale: string;
audioContext: AudioContext;
azureCredentials: string | AzureSpeechCredentials;
azureRegion: string;
speechRecognitionEndpointId?: string;
azureLanguageCredentials?: AzureLanguageCredentials;
}
import { getToken } from "./getToken";

interface ASRPonyfillInput {
audioContext: AudioContext;
azureAuthorizationToken: string;
azureRegion: string;
speechRecognitionEndpointId?: string;
}
import createSpeechRecognitionPonyfill from "web-speech-cognitive-services/lib/SpeechServices/SpeechToText";

export const asrMachine = setup({
types: {
Expand Down
23 changes: 13 additions & 10 deletions src/dev.ts
Original file line number Diff line number Diff line change
@@ -1,19 +1,19 @@
import { createActor, createMachine, assign } from "xstate";
import { createActor, createMachine } from "xstate";

import { speechstate } from "./speechstate";
import {
AzureLanguageCredentials,
AzureSpeechCredentials,
Settings,
} from "./types";
import { createSkyInspector } from "@statelyai/inspect";
import { createBrowserInspector } from "@statelyai/inspect";

const { inspect } = createSkyInspector();
const inspector = createBrowserInspector();

const azureSpeechCredentials: AzureSpeechCredentials = {
endpoint:
"https://northeurope.api.cognitive.microsoft.com/sts/v1.0/issuetoken",
key: "",
key: "2e15e033f605414bbbfe26cb631ab755",
};

const azureLanguageCredentials: AzureLanguageCredentials = {
Expand All @@ -26,7 +26,7 @@ const azureLanguageCredentials: AzureLanguageCredentials = {

const settings: Settings = {
azureCredentials: azureSpeechCredentials,
azureRegion: "swedencentral",
azureRegion: "northeurope",
azureLanguageCredentials: azureLanguageCredentials,
asrDefaultCompleteTimeout: 0,
asrDefaultNoInputTimeout: 5000,
Expand All @@ -36,15 +36,18 @@ const settings: Settings = {
};

const speechMachine = createMachine({
entry: assign({
ssRef: ({ spawn }) => spawn(speechstate, { input: settings }),
}),
context: ({ spawn }) => {
return { ssRef: spawn(speechstate, { input: settings }) };
},
initial: "Main",
states: { Main: {} },
});

export const speechState = createActor(speechMachine, { inspect });
export const speechState = createActor(speechMachine, {
inspect: inspector.inspect,
});

speechState.start();
speechState.getSnapshot().context.ssRef.send({ type: "PREPARE" });


(window as any).speechService = speechState;
Loading

0 comments on commit e5de8c6

Please sign in to comment.