ThingTalk
raw JSON →ThingTalk is a declarative, rule-based distributed programming language for virtual assistants, developed by Stanford Open Virtual Assistant Lab. This package (v2.1.1, not semver: minor bumps can break library APIs) provides the grammar, just-in-time compiler, interface for SMT-based program analysis, natural language translation, and runtime libraries. It connects web services and IoT devices via 'when-get-do' statements, leveraging Thingpedia API primitives. Differentiators: unique focus on virtual assistants, crowdsourced API repository, and non-semver versioning where minor versions may introduce breaking API changes. Release cadence: irregular, with major version bumps for language incompatibility, minor for library changes, patch for bug fixes.
Common errors
error SyntaxError: Unexpected token ↓
=> @service.function(params). Refer to ThingTalk language specification. error Error: Cannot find module 'thingtalk' ↓
npm install thingtalk and use import { compile } from 'thingtalk' (ESM) or ensure your bundler supports ESM. For CommonJS, use dynamic import: const { compile } = await import('thingtalk'). error TypeError: compile is not a function ↓
import compile from 'thingtalk' with import { compile } from 'thingtalk'. error ThingTalk version mismatch: expected 2.1.x, got 2.2.0 ↓
{ libraryVersion: '2.1.0' } in the compile options to output syntax compatible with the older client. Warnings
breaking Minor version bumps can break library APIs. Use tilde version ranges. ↓
gotcha No default export; all exports are named. ↓
deprecated The `translate` function has been deprecated in favor of `compileWithTranslation`. ↓
gotcha ThingTalk syntax generated by a newer version may not parse with an older client. Specify library version for compatibility. ↓
Install
npm install thingtalk yarn add thingtalk pnpm add thingtalk Imports
- compile wrong
const compile = require('thingtalk').compile;correctimport { compile } from 'thingtalk' - Grammar wrong
import Grammar from 'thingtalk';correctimport { Grammar } from 'thingtalk' - Ast wrong
import { Ast } from 'thingtalk';correctimport type { Ast } from 'thingtalk'
Quickstart
import { compile } from 'thingtalk';
const code = `
monitor(@com.washingtonpost.get_article(section=enum world))
=> @com.yandex.translate.translate(target_language="zh", text=title)
=> @com.facebook.post(status=$result);
`;
try {
const compiled = compile(code, { locale: 'en-US', timezone: 'America/Los_Angeles' });
console.log('Compiled program:', JSON.stringify(compiled, null, 2));
} catch (err) {
console.error('Compilation failed:', err);
}