How to use the thingtalk.Ast function in thingtalk

To help you get started, we’ve selected a few thingtalk examples, based on popular ways it is used in public projects.

Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.

github stanford-oval / thingpedia-common-devices / test / mock.js View on Github external
async getDeviceManifest(deviceKind) {
        if (this._cachedManifests.has(deviceKind))
            return this._cachedManifests.get(deviceKind);

        const manifestPath = path.resolve(path.dirname(module.filename), '../' + deviceKind + '/manifest.tt');
        const ourMetadata = (await util.promisify(fs.readFile)(manifestPath)).toString();
        const ourParsed = ThingTalk.Grammar.parse(ourMetadata);
        ourParsed.classes[0].annotations.version = new ThingTalk.Ast.Value.Number(-1);

        if (!ourParsed.classes[0].is_abstract) {
            try {
                // ourMetadata might lack some of the fields that are in the
                // real metadata, such as api keys and OAuth secrets
                // for that reason we fetch the metadata for thingpedia as well,
                // and fill in any missing parameter
                const officialMetadata = await super.getDeviceCode(deviceKind);
                const officialParsed = ThingTalk.Grammar.parse(officialMetadata);

                const ourConfig = ourParsed.classes[0].config;

                ourConfig.in_params = ourConfig.in_params.filter((ip) => !ip.value.isUndefined);
                const ourConfigParams = new Set(ourConfig.in_params.map((ip) => ip.name));
                const officialConfig = officialParsed.classes[0].config;
github stanford-oval / genie-toolkit / lib / sampler.js View on Github external
const choices = this._allContexts.get(this._context);
            for (let token of uniform(choices, this._rng).split(' ')) {
                // replace entities and undo penn tree bank tokenization
                if (/^[A-Z]/.test(token)) { // entity
                    if (!this._assignedEntities[token]) {
                        console.log(this._context, this._targetCode, this._assignedEntities);
                        throw new Error(`Missing entity ${token} (present in the context sentence, not in the context code)`);
                    }
                    token = this._assignedEntities[token].display;
                }
                context_utterance = this._detokenizer(context_utterance, prevtoken, token);
                prevtoken = token;
            }

            for (let [, slot] of context.iterateSlots()) {
                if (slot instanceof ThingTalk.Ast.Selector)
                    continue;
                if (slot.value.isUndefined) {
                    assistant_action = 'slot-fill:' + slot.name;
                    break;
                }
            }
            if (assistant_action === null) {
                if (context.isProgram && context.rules.every((r) => !r.stream && r.actions.every((a) => a.isInvocation && a.invocation.selector.isBuiltin)))
                    assistant_action = 'result';
                else
                    assistant_action = 'confirm';
            }
        }

        // remove flags
        const id = this._id.replace(/^R?P?S/, '');
github stanford-oval / almond-dialog-agent / lib / stack-frames / make-dialog.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016-2019 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//         Silei Xu 
//
// See COPYING for details
"use strict";

const assert = require('assert');

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;

const { ValueCategory, Intent } = require('../semantic');
const Helpers = require('../helpers');

const { slotFillSingle, concretizeValue } = require('../dialogs/slot_filling');
const { makeContact } = require('../dialogs/contact_search');

const ProgramStackFrame = require('./program');
const CustomStackFrame = require('./custom');
const DialogStackFrame = require('./base');

const CATEGORIES = ['media', 'social-network', 'home', 'communication', 'service', 'data-management'];
const PRIM_TYPES = ['stream', 'query', 'action'];

async function getCategoryList(dlg, primType) {
github stanford-oval / almond-cloud / util / manifest_to_schema.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2016 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//
// See COPYING for details
"use strict";

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Type = ThingTalk.Type;

const { clean } = require('./tokenize');

function makeSchemaFunctionDef(functionType, functionName, schema, isMeta) {
    const args = [];
    // compat with Thingpedia API quirks
    const types = schema.types || schema.schema;

    types.forEach((type, i) => {
        type = Type.fromString(type);
        const argname = schema.args[i];
        const argrequired = !!schema.required[i];
        const arginput = !!schema.is_input[i];

        let direction;
github stanford-oval / almond-dialog-agent / lib / dialogs / slot_filling.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of Almond
//
// Copyright 2016-2017 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//
// See COPYING for details
"use strict";

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;
const Describe = ThingTalk.Describe;

const ValueCategory = require('../semantic').ValueCategory;
const Helpers = require('../helpers');

const { contactSearch } = require('./contact_search');
const resolveUserContext = require('./user_context');
const { chooseDevice } = require('./device_choice');
const { lookupEntity, lookupLocation } = require('./entity_lookup');

const MESSAGING_ACCOUNT_REGEX = /^[A-Za-z.0-9]+-account:/;

async function addDisplayToContact(dlg, contact) {
    const principal = contact.value;

    if (dlg.platformData.contacts) {
github stanford-oval / almond-cloud / training / sentence-generator / grammar_lib.js View on Github external
// -*- mode: js; indent-tabs-mode: nil; js-basic-offset: 4 -*-
//
// This file is part of ThingEngine
//
// Copyright 2017-2018 The Board of Trustees of the Leland Stanford Junior University
//
// Author: Giovanni Campagna 
//
// See COPYING for details
"use strict";

const ThingTalk = require('thingtalk');
const Ast = ThingTalk.Ast;

const { split } = require('../../util/tokenize');
const { coin, uniform } = require('../../util/random');

const NON_TERM_REGEX = /\${(?:choice\(([^)]+)\)|([a-zA-Z0-9._:(),]+))}/;

// A numbered constant, eg. QUOTED_STRING_0 or NUMBER_1 or HASHTAG_3
// During generation, this constant is put in the program as a VarRef
// with an unique variable name.
class Constant {
    constructor(symbol, number, type) {
        this.symbol = symbol;
        this.number = number;
        this.type = type;
        this.value = new Ast.Value.VarRef(`__const_${symbol.replace(/[:._]/g, (match) => {
            if (match === '_')
github stanford-oval / thingengine-core / lib / apps / trigger_runner.js View on Github external
return state.open().then(() => {
                if (this._stopped)
                    return;

                // TODO filters
                let filter = ThingTalk.Ast.BooleanExpression.True;

                let stream = device[this._fn](this._params, state, filter);
                this._streams.set(device, stream);

                stream.uniqueId = uniqueId; // for debugging only
                stream.device = device;

                stream.on('error', this._errorListener);
                stream.on('end', this._endListener);
                stream.on('data', this._dataListener);
            });
        }).catch((e) => {
github stanford-oval / almond-dialog-agent / lib / semantic.js View on Github external
what = 'command';
    else if (expected !== null)
        what = 'generic';
    else
        what = null;
    return what;
};

const Intent = adt.data({
    // internally generated intents that have no thingtalk representation
    Unsupported: { platformData: adt.any },
    Example: { utterance: adt.only(String), targetCode: adt.only(String), platformData: adt.any },
    Failed: { command: adt.only(String, null), platformData: adt.any },

    // bookkeeping intents that require special handling in the dialogues and the dispatcher
    Train: { command: adt.only(Object, null), fallbacks: adt.only(Array, null), thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    Back: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    More: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    Empty: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    Debug: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    Maybe: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    CommandList: { device: adt.only(String, null), category: adt.only(String), thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },
    NeverMind: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any }, // cancel the current task
    Stop: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any }, // cancel the current task, quietly
    Help: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any }, // ask for contextual help, or start a new task
    Make: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any }, // reset and start a new task
    WakeUp: { thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any }, // do nothing and wake up the screen
    Answer: { category: adt.only(ValueCategory), value: adt.only(Ast.Value, Number), thingtalk: adt.only(ThingTalk.Ast.Input), platformData: adt.any },

    // thingtalk
    Program: {
        program: adt.only(Ast.Program),
github stanford-oval / thingengine-core / test / functional / test_builtins.js View on Github external
async function testGetCommands(engine) {
    const device = engine.devices.getDevice('thingengine-own-global');

    const result = await device.get_get_commands({ device: new Tp.Value.Entity('com.xkcd', 'tt:device', 'XKCD') });

    for (let ex of result)
        assert(ex.program instanceof ThingTalk.Ast.Example);
}
github stanford-oval / thingengine-core / lib / db / memory.js View on Github external
_genCommand(table, version, cols, filters, aggregation) {
        let where = '';
        if (version) {
            let v = ThingTalk.Ast.Value.Number(version);
            let vfilter = new ThingTalk.Ast.BooleanExpression.Atom('_id', '<=', v);
            filters = new ThingTalk.Ast.BooleanExpression.And([filters, vfilter]);
        }
        let parsed = this._filterParser(filters);
        if (parsed.length > 0)
            where += `where ${parsed} `;
        if (!aggregation)
            return this._queryCommand(table, cols, where);
        else if (aggregation.type.startsWith('argm'))
            return this._queryArgm(table, cols, aggregation, where);
        else
            return this._queryAggregation(table, aggregation, where);
    }