Skip to content
This repository has been archived by the owner on Aug 16, 2023. It is now read-only.

Commit

Permalink
Add support for Collection Browse
Browse files Browse the repository at this point in the history
Change-Id: I9f5c0a81a5712b3627e6f6e744f9e5cd14feb73a
  • Loading branch information
taycaldwell committed Oct 7, 2020
1 parent 43e697c commit c0472b9
Show file tree
Hide file tree
Showing 8 changed files with 215 additions and 1 deletion.
56 changes: 56 additions & 0 deletions src/api/schema.ts
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,8 @@ export interface EntryDisplay {
*
* A hero image for the card. The height is fixed to 192dp. Optional.
*
* Image for the collection item.
*
* An image.
*
* A small image icon displayed on the right from the title. It's resized to 36x36 dp.
Expand Down Expand Up @@ -444,6 +446,8 @@ export interface Image {
* URL of document associated with browsing carousel item. Required for browsing carousel.
*
* What happens when a user opens the link
*
* Required. URI to open if the item selected.
*/
export interface OpenURL {
/**
Expand Down Expand Up @@ -734,6 +738,8 @@ export interface Prompt {
* conjunction with the "first_simple" field in the containing prompt to speak to the user
* in addition to displaying a interactive canvas response. The maximum size of the response
* is 50k bytes.
*
* A response to be used for interactive canvas experience.
*/
export interface Canvas {
/**
Expand Down Expand Up @@ -765,6 +771,10 @@ export interface Content {
* A card presenting a collection of options to select from.
*/
collection?: Collection
/**
* A card presenting a collection of web pages to open.
*/
collectionBrowse?: CollectionBrowse
/**
* An image.
*/
Expand Down Expand Up @@ -839,6 +849,8 @@ export interface Link {
* How the image background will be filled. Optional.
*
* How the image backgrounds of collection items will be filled. Optional.
*
* Type of image display option.
*/
export enum ImageFill {
Cropped = 'CROPPED',
Expand Down Expand Up @@ -881,6 +893,50 @@ export interface CollectionItem {
key?: string
}

/**
* A card presenting a collection of web pages to open.
*
* Presents a set of web documents as a collection of large-tile items. Items may be
* selected to launch their associated web document in a web viewer.
*/
export interface CollectionBrowse {
/**
* Type of image display option.
*/
imageFill?: ImageFill
/**
* Min: 2. Max: 10.
*/
items?: CollectionBrowseItem[]
}

/**
* Item in the collection.
*/
export interface CollectionBrowseItem {
/**
* Description of the collection item.
*/
description?: string
/**
* Footer text for the collection item, displayed below the description. Single line of
* text, truncated with an ellipsis.
*/
footer?: string
/**
* Image for the collection item.
*/
image?: Image
/**
* Required. URI to open if the item selected.
*/
openUriAction?: OpenURL
/**
* Required. Title of the collection item.
*/
title?: string
}

/**
* A card presenting a list of options to select from.
*
Expand Down
53 changes: 53 additions & 0 deletions src/conversation/_test/conversation.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import {
Image,
Link,
Canvas,
CollectionBrowse,
} from '../..'
import * as Schema from '../../api/schema'
import { clone } from '../../common'
Expand Down Expand Up @@ -350,6 +351,58 @@ test('Canvas response sent', async t => {
})
})

test('CollectionBrowse response sent', async t => {
const handlerName = 'handlerName'
const app = conversation()
const collectionBrowseParams = {
imageFill: Schema.ImageFill.White,
items:
[
{
title: 'Item #1',
description: 'Description of Item #1',
footer: 'Footer of Item #1',
image: {
url: 'https://developers.google.com/assistant/assistant_96.png',
},
openUriAction: {
url: 'https://www.example.com',
},
},
{
title: 'Item #2',
description: 'Description of Item #2',
footer: 'Footer of Item #2',
image: {
url: 'https://developers.google.com/assistant/assistant_96.png',
},
openUriAction: {
url: 'https://www.example.com',
},
},
],
}
app.handle(handlerName, conv => {
conv.add(new CollectionBrowse(collectionBrowseParams))
})
const res = await app.handler(
requestBuilder(handlerName, {}) as Schema.HandlerRequest, {})
t.is(res.status, 200)
t.log(JSON.stringify(clone(res.body)))
t.deepEqual(clone(res.body), {
session: {
id: '7250447207398852357',
params: {},
},
prompt: {
override: false,
content: {
collectionBrowse: collectionBrowseParams,
},
},
})
})

test('app instance contains health check handler', async t => {
const handlerName = 'actions.handler.HEALTH_CHECK'
const app = conversation()
Expand Down
41 changes: 40 additions & 1 deletion src/conversation/prompt/_test/prompt.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import test from 'ava'
import { Prompt, OrderUpdate } from '..'
import { Prompt, OrderUpdate, CollectionBrowse } from '..'
import * as common from '../../../common'
import * as Schema from '../../../api/schema'

Expand All @@ -29,3 +29,42 @@ test('orderUpdate is added to prompt', t => {
},
})
})

test('collectionBrowse is added to prompt', t => {
const prompt = new Prompt()
const collectionBrowseParams = {
imageFill: Schema.ImageFill.White,
items:
[
{
title: 'Item #1',
description: 'Description of Item #1',
footer: 'Footer of Item #1',
image: {
url: 'https://developers.google.com/assistant/assistant_96.png',
},
openUriAction: {
url: 'https://www.example.com',
},
},
{
title: 'Item #2',
description: 'Description of Item #2',
footer: 'Footer of Item #2',
image: {
url: 'https://developers.google.com/assistant/assistant_96.png',
},
openUriAction: {
url: 'https://www.example.com',
},
},
],
}
prompt.add(new CollectionBrowse(collectionBrowseParams))
t.deepEqual(common.clone(prompt) as Schema.Prompt, {
override: false,
content: {
collectionBrowse: collectionBrowseParams,
},
})
})
38 changes: 38 additions & 0 deletions src/conversation/prompt/content/collectionBrowse.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
/**
* Copyright 2020 Google LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

import * as Schema from '../../../api/schema'

export class CollectionBrowse implements Schema.CollectionBrowse {
/**
* How the image backgrounds of collection items will be filled. Optional.
*/
imageFill: Schema.ImageFill
/**
* min: 2 max: 10
*/
items: Schema.CollectionBrowseItem[]
/** @hidden */
constructor(input: Schema.CollectionBrowse = {}) {
const {
imageFill = Schema.ImageFill.Unspecified,
items = [],
} = input
this.imageFill = imageFill
this.items = items
}

}
10 changes: 10 additions & 0 deletions src/conversation/prompt/content/content.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@
import * as Schema from '../../../api/schema'
import { Card } from './card'
import { Collection } from './collection'
import { CollectionBrowse } from './collectionBrowse'
import { Image } from './image'
import { List } from './list'
import { Media } from './media'
Expand All @@ -25,6 +26,7 @@ import { Table } from './table'
export type PromptContent =
Card |
Collection |
CollectionBrowse |
Image |
List |
Media |
Expand All @@ -39,6 +41,10 @@ export class Content {
* A collection.
*/
collection?: Schema.Collection | undefined
/**
* A card presenting a collection of web pages to open.
*/
collectionBrowse?: Schema.CollectionBrowse | undefined
/**
* An image.
*/
Expand All @@ -60,6 +66,7 @@ export class Content {
const {
card = null,
collection = null,
collectionBrowse = null,
image = null,
list = null,
media = null,
Expand All @@ -72,6 +79,9 @@ export class Content {
if (collection) {
this.collection = collection
}
if (collectionBrowse) {
this.collectionBrowse = collectionBrowse
}
if (image) {
this.image = image
}
Expand Down
1 change: 1 addition & 0 deletions src/conversation/prompt/content/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ export * from './content'

export * from './card'
export * from './collection'
export * from './collectionBrowse'
export * from './image'
export * from './link'
export * from './list'
Expand Down
16 changes: 16 additions & 0 deletions src/conversation/prompt/prompt.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import { Link } from './content/link'
import { Content } from './content/content'
import { Card } from './content/card'
import { Collection } from './content/collection'
import { CollectionBrowse } from './content/collectionBrowse'
import { Image } from './content/image'
import { List } from './content/list'
import { Media } from './content/media'
Expand All @@ -33,6 +34,14 @@ export type PromptItem =
string |
Simple |
Content |
Card |
Collection |
CollectionBrowse |
Canvas |
Image |
List |
Media |
Table |
Link |
Suggestion |
Canvas |
Expand Down Expand Up @@ -155,6 +164,13 @@ export class Prompt implements Schema.Prompt {
this.content.collection = item
continue
}
if (item instanceof CollectionBrowse) {
if (!this.content) {
this.content = new Content({})
}
this.content.collectionBrowse = item
continue
}
if (item instanceof Canvas) {
this.canvas = item
continue
Expand Down
1 change: 1 addition & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,7 @@ export {
Suggestion,
Card,
Collection,
CollectionBrowse,
Image,
Link,
List,
Expand Down

0 comments on commit c0472b9

Please sign in to comment.