Jargon SDK for Google Assistant Actions (nodejs)
The Jargon SDK makes it easy for skill developers to manage their runtime content, and to support
multiple languages from within their skill.
Need help localizing your skills to new languages and locales? Contact Jargon at localization@jargon.com.
Requirements
This version of the SDK works with Google Assistant actions that are built using the Actions on Gooogle support library.
The Jargon SDK makes use of Javascript features supported by Node version 8 and later. When deploying on Google / Firebase cloud functions make sure you're selecting the appropriate engine in your package.json:
"engines": {
"node": "8"
}
Like the Actions on Goolge support library, the Jargon SDK is built using TypeScript,
and includes typing information in the distribution package.
Core concepts
Content resources and resource files
Content resources define the text that your skill outputs to users, via Alexa's voice, card content,
or screen content. It's important that these resources live outside of your skill's source code to
make it possible to localize them into other languages.
The Jargon SDK expects resource files to live in the "resources" subdirectory within your lambda
code (i.e., skill_root/lambda/custom/resources). Each locale has a single resouce file, named for
that locale (e.g., "en-US.json").
Resource files are JSON, with a single top-level object (similar to package.json). The keys within that
object are the identifiers you'll use to refer to specific resources within your source code. Nested objects
are supported to help you organize your resources.
{
"key1":"Text for key 1",
"key2":"Text for key 2",
"nestedObjects":{
"are":{
"supported":"Use the key 'nestedObjects.are.supported' to refer to this resource"
}
}
}
Resource value format
Resource values are in ICU MessageFormat. This
format supports constructing text at runtime based on parameters passed in from your code, and selecting
alternative forms to handle things like pluralization and gender.
Named parameters
{
"sayHello":"Hello {name}"
}
Plural forms
{
"itemCount":"{count, plural, =0 {You have zero items} =1 {You have one item} other {You have # items}}"
}
Gendered forms
{
"pronounSelection":"{gender, select, female {She did it!} male {He did it!} other {It did it!}"
}
Variations
It's important for Alexa skills to vary the words they use in response to users, lest they sound robotic. The Jargon SDK
makes ths simple with built-in variation support. Variations are defined using nested objects:
{
"resourceWithVariations":{
"v1":"First variation",
"v2":"Second variation",
"v3":"Third variation"
}
}
When rendering the key resourceWithVariations
the Jargon SDK will choose a variation at random (with other more complex
methods coming in future versions). If you render the same resource multiple times within a single request (e.g., for spoken
content and for card or screen content) the SDK will by default consistently choose the same variation.
Note that you can always select a specific variation using its fully-qualified key (e.g., resourceWithVariations.v1
)
You can determine which variation the SDK chose via the ResourceManager's selectedVariation(s) routines.
Runtime interface
RenderItem
A RenderItem specifies a resource key, optional parameters, and options to control details of the rendering (which
are themselves optional).
interface RenderItem {
key: string
params?: RenderParams
options?: RenderOptions
}
RenderParams
are a map from parameter name to a string, number, or RenderItem
instance.
interface RenderParams {
[param: string]: string | number | RenderItem
}
The use of a RenderItem
instance as a parameter value makes it easy to compose multiple
resource together at runtime. This is useful when a parameter value varies across locales,
or when you want the SDK to select across multiple variations for a parameter value, and reduces
the need to chain together multiple calls into the ResourceManager
.
The ri
helper function simplifies constructing a RenderItem
:
function ri (key: string, params?: RenderParams, options?: RenderOptions): RenderItem
handlerInput.jrb.speak(ri('sayHello', { 'name': 'World' }))
RenderOptions
allows fine-grained control of rendering behavior for a specific call, overriding
the configuration set at the ResourceManager
level.
interface RenderOptions {
readonly forceNewRandom?: boolean
}
JargonDialogflowApp and JargonActionsSdkApp
- Use the version that corresponds with the API your action is written against
- Installs middleware to create the Jargon per-request objects
- That middleware adds the following to the conversation object passed to your intent handlers
export interface Conversation<TUserStorage> {
jargonResourceManager: ResourceManager
jrm: ResourceManager
jargonResponseFactory: ResponseFactory
jrf: ResponseFactory
}
Intializing the Jargon SDK is simple:
const { JargonDialogflowApp } = require('@jargon/actions-on-google')
const app = dialogflow()
new JargonDialogflowApp().installOnto(app)
ResponseFactory
- Simplifies the creation of response objects
- Each response type (simple, basic card, etc.) has a corresponding method that takes a Jargon variant of the options
- The main difference with the Jargon variants is replacing user-visisble
string
fields with RenderItem
s
export interface JBasicCardOptions {
title?: RenderItem
subtitle?: RenderItem
text?: RenderItem
image?: GoogleActionsV2UiElementsImage
buttons?: GoogleActionsV2UiElementsButton | GoogleActionsV2UiElementsButton[]
display?: GoogleActionsV2UiElementsBasicCardImageDisplayOptions
}
export interface JSimpleResponseOptions {
speech: RenderItem
text?: RenderItem
}
export interface ResponseFactory {
basicCard (options: JBasicCardOptions): Promise<BasicCard>
simple (options: JSimpleResponseOptions | RenderItem): Promise<SimpleResponse>
}
ResourceManager
Internally ResponseFactory
uses a ResourceManager
to render strings and objects. You
can directly access the resource manager if desired, for use cases such as:
- obtaining locale-specific values that are used as parameters for later rendering operations
- incrementally or conditionally constructing complex content
- response directives that internally have locale-specific content (such as an upsell directive)
- batch rendering of multiple resources
- determining which variation the ResourceManager chose
export interface ResourceManager {
render (item: RenderItem): Promise<string>
renderBatch (items: RenderItem[]): Promise<string[]>
renderObject<T> (item: RenderItem): Promise<T>
selectedVariation (item: RenderItem): Promise<SelectedVariation>
selectedVariations (): Promise<SelectedVariation[]>
readonly locale: string
}
Note that the render routines return Promise
s to the rendered content, not the content directly.
ResourceManager
is part of the package @jargon/sdk-core,
and can be used directly from code that isn't based on the Actions on Google support library.
Built-in Resources
This SDK includes default responses for some common scenarios. These responses are available using the following RenderItem
keys:
Jargon.unhandledResponse
-- provides a response for when you can't otherwise process an intentJargon.defaultReprompt
-- provides a generic reprompt
You can render these resources as you would any of your own. You can also define your own version for these keys in your resource file to override the Jargon-provided responses.
Currently the SDK includes variants of these resources for English, with other languages coming soon.
Adding to an existing action
Installation
Externalize resources