Skip to content

Commit

Permalink
v6.6.0: Composer multi-responses support
Browse files Browse the repository at this point in the history
Summary:
- Composer multi-responses support
- bumped API to 20230215
- bumped SDK to 6.6.0

Reviewed By: nishsinghal20, CleanestMink126

Differential Revision: D43291213

fbshipit-source-id: 03f5d26b0f00a07fa6c9a6789a6d53b0fe9b2b35
  • Loading branch information
Julien Odent authored and facebook-github-bot committed Feb 15, 2023
1 parent fef90a1 commit 948f228
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 13 deletions.
5 changes: 5 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
## v6.6.0 - Multi-responses in Composer

- Multi-responses in Composer: `runComposerAudio()`, `.runComposer()` (and raw `.converse()` + `.event()`) now emit `response` events for intermediate responses, and run intermediate actions as well.
- Bumped API version to `20230215`.

## v6.5.1
- Update uuid to version 9.0.0

Expand Down
5 changes: 4 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,8 @@ Takes the following parameters:
- `body` - the audio `Readable` stream
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object

Emits `partialTranscription` and `fullTranscription` events.
Emits `partialTranscription`, `fullTranscription`, and `response` events. Runs
intermediate `actions` as instructed by the API.

We recommend to use `.runComposerAudio()` instead of this raw API.

Expand All @@ -132,6 +133,8 @@ Takes the following parameters:
- `contextMap` - the [context map](https://wit.ai/docs/recipes#custom-context) object
- `message` - the optional user text query

Emits `response` events, and run intermediate `actions` as instructed by the API.

We recommend to use `.runComposer()` instead of this raw API.

### .message()
Expand Down
2 changes: 1 addition & 1 deletion lib/config.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@
*/

module.exports = {
DEFAULT_API_VERSION: 20220801,
DEFAULT_API_VERSION: 20230215,
DEFAULT_WIT_URL: 'https://api.wit.ai',
};
87 changes: 78 additions & 9 deletions lib/wit.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ const {Readable} = require('stream');
const Url = require('url');

const LIVE_UNDERSTANDING_API_VERSION = 20220608;
const MULTI_RESPONSES_API_VERSION = 20230215;

class Wit extends EventEmitter {
constructor(opts) {
Expand Down Expand Up @@ -45,7 +46,7 @@ class Wit extends EventEmitter {
throw new Error('Please provide an audio stream (Readable).');
}

const {apiVersion, headers, logger, proxy, witURL} = this.config;
const {actions, apiVersion, headers, logger, proxy, witURL} = this.config;

const params = {
session_id: sessionId,
Expand All @@ -60,6 +61,8 @@ class Wit extends EventEmitter {
const fullURL = witURL + '/converse?' + encodeURIParams(params);
logger.debug(method, fullURL);

const multi_responses_enabled = apiVersion >= MULTI_RESPONSES_API_VERSION;

const req = fetch(fullURL, {
body,
method,
Expand All @@ -73,10 +76,10 @@ class Wit extends EventEmitter {

const _partialResponses = req
.then(
response =>
resp =>
new Promise((resolve, reject) => {
logger.debug('status', response.status);
const bodyStream = response.body;
logger.debug('status', resp.status);
const bodyStream = resp.body;

bodyStream.on('readable', () => {
let chunk;
Expand All @@ -86,13 +89,31 @@ class Wit extends EventEmitter {
}

for (const rsp of parseResponse(contents)) {
const {error, is_final, text} = rsp;
const {action, context_map, error, is_final, response, text} =
rsp;

// Live transcription
if (!(error || is_final)) {
logger.debug('[converse] partialTranscription:', text);
this.emit('partialTranscription', text);
}

// Multi-responses
if (
multi_responses_enabled &&
!(error || is_final) &&
(action || response)
) {
if (response) {
logger.debug('[converse] partialResponse:', response);
this.emit('response', response);
}

if (action) {
logger.debug('[converse] got partial action:', action);
runAction(logger, actions, action, context_map);
}
}
}
});
}),
Expand Down Expand Up @@ -121,7 +142,7 @@ class Wit extends EventEmitter {
throw new Error('Please provide a session ID (string).');
}

const {apiVersion, headers, logger, proxy, witURL} = this.config;
const {actions, apiVersion, headers, logger, proxy, witURL} = this.config;

const params = {
session_id: sessionId,
Expand All @@ -142,8 +163,56 @@ class Wit extends EventEmitter {
const fullURL = witURL + '/event?' + encodeURIParams(params);
logger.debug(method, fullURL);

return fetch(fullURL, {body: JSON.stringify(body), method, headers, proxy})
.then(response => Promise.all([response.json(), response.status]))
const req = fetch(fullURL, {
body: JSON.stringify(body),
method,
headers,
proxy,
});

// Multi-responses
if (apiVersion >= MULTI_RESPONSES_API_VERSION) {
const _partialResponses = req
.then(
resp =>
new Promise((resolve, reject) => {
logger.debug('status', resp.status);
const bodyStream = resp.body;

bodyStream.on('readable', () => {
let chunk;
let contents = '';
while (null !== (chunk = bodyStream.read())) {
contents += chunk.toString();
}

for (const rsp of parseResponse(contents)) {
const {action, context_map, error, is_final, response} = rsp;

if (!(error || is_final) && (action || response)) {
if (response) {
logger.debug('[event] partialResponse:', response);
this.emit('response', response);
}

if (action) {
logger.debug('[event] got partial action:', action);
runAction(logger, actions, action, context_map);
}
}
}
});
}),
)
.catch(e =>
logger.error('[event] could not parse partial response', e),
);
}

return req
.then(response => Promise.all([response.text(), response.status]))
.then(([contents, status]) => ([parseResponse(contents).pop(), status]))
.catch(e => e)
.then(makeWitResponseHandler(logger, 'event'));
}

Expand Down Expand Up @@ -364,7 +433,7 @@ class Wit extends EventEmitter {
throw new Error('Please provide a text input (string).');
}
if (typeof voice !== 'string') {
throw new Error('Please provide a voice input. (string)');
throw new Error('Please provide a voice input (string).');
}

const {apiVersion, headers, logger, proxy, witURL} = this.config;
Expand Down
5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "node-wit",
"version": "6.5.1",
"version": "6.6.0",
"description": "Wit.ai Node.js SDK",
"keywords": [
"wit",
Expand All @@ -9,7 +9,8 @@
"botengine",
"bots",
"nlp",
"automation"
"automation",
"composer"
],
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 948f228

Please sign in to comment.