Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
sendUtterance(utterance, expecting, choices) {
const store = 'no';
const data = {
q: utterance,
store: store,
thingtalk_version: ThingTalk.version,
};
if (expecting)
data.expect = String(expecting);
let url = `${this._baseUrl}/query?${qs.stringify(data)}`;
// we need to do this one by hand because of the peculiar encoding
// of the keys (we must not escape [ and ])
if (choices) {
choices.forEach((c, i) => {
if (c)
url += `&choices[${i}]=${encodeURIComponent(c.title)}`;
});
}
return Tp.Helpers.Http.get(url).then((data) => {
getSchemas(kinds, withMetadata) {
var to = THINGPEDIA_URL + '/api/v3/schema/' + kinds.join(',');
to += '?locale=' + this.locale;
to += '&thingtalk_version=' + ThingTalk.version;
if (withMetadata)
to += '&meta=1';
if (this.developerKey)
to += '&developer_key=' + this.developerKey;
return this._simpleRequest(to, true);
}
onlineLearn(utterance, code, store = 'automatic', user = null) {
if (Array.isArray(code))
code = code.join(' ');
if (typeof code !== 'string')
throw new TypeError('Invalid code parameter to onlineLearn');
return Promise.resolve($.ajax(this._baseUrl + '/learn', {
method: 'POST',
data: {
q: utterance,
target: code,
store,
owner: user,
thingtalk_version: ThingTalk.version,
developer_key: this._developerKey
}
})).catch((e) => {
if ('responseJSON' in e && 'error' in e.responseJSON) {
if (e.responseJSON.error === 'Missing owner for commandpedia command')
throw new Error('You need to log in to add new command.');
throw new Error('Failed to store the new sentence: ' + e.responseJSON.error);
} else {
throw e;
}
});
}
async sendUtterance(utterance, tokenized, contextCode, contextEntities) {
const data = {
q: utterance,
store: 'no',
thingtalk_version: ThingTalk.version,
};
let response;
if (contextCode !== undefined) {
data.context = contextCode.join(' ');
data.entities = contextEntities;
data.tokenized = tokenized;
data.skip_typechecking = true;
response = await Tp.Helpers.Http.post(`${this._baseUrl}/query`, JSON.stringify(data), {
dataContentType: 'application/json'
});
} else {
data.tokenized = tokenized ? '1' : '';
data.skip_typechecking = '1';
"use strict";
const express = require('express');
const ThingTalk = require('thingtalk');
const Tp = require('thingpedia');
const router = express.Router();
const db = require('../util/db');
const iv = require('../util/input_validation');
const I18n = require('../util/i18n');
const userModel = require('../model/user');
const exampleModel = require('../model/example');
const Config = require('../config');
const LATEST_THINGTALK_VERSION = ThingTalk.version;
async function learn(req, res) {
let store = req.body.store;
if (['no', 'automatic', 'online', 'commandpedia'].indexOf(store) < 0) {
res.status(400).json({ error: 'Invalid store parameter' });
return;
}
const owner = req.body.owner;
if (store === 'commandpedia' && !owner) {
res.status(400).json({ error: 'Missing owner for commandpedia command' });
return;
}
if (!I18n.get(req.params.locale, false)) {
res.status(404).json({ error: 'Unsupported language' });
return;
async sendUtterance(utterance, context, expecting, choices) {
const store = this._prefs.get('sabrina-store-log') || 'no';
const data = {
q: utterance,
store: store,
thingtalk_version: ThingTalk.version,
};
if (this._platform)
data.developer_key = this._platform.getDeveloperKey();
if (expecting)
data.expect = String(expecting);
if (choices)
data.choices = choices.map((c) => c.title);
if (this._prefs.get('experimental-contextual-model')) {
const now = new Date;
if (context.timeout > now) {
data.context = context.code;
data.entities = context.entities;
} else {
data.context = 'null';
data.entities = {};