This repository has been archived by the owner on Aug 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
prompt.ts
234 lines (224 loc) · 6.31 KB
/
prompt.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
/**
* 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'
import { Simple } from './simple'
import { Suggestion } from './suggestion'
import { Canvas } from './canvas'
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'
import { Table } from './content/table'
import { OrderUpdate } from './content/order'
/**
* Prompts define how your Action renders responses to users and how your
* Action prompts them to continue. As you build your Action, you can add
* prompts to invocations and to various places within scenes. Prompts can be
* as simple as a text or speech response, or can be more complex and contain
* rich content like cards, images, and tables.
*
* @see {@link https://developers.google.com/assistant/conversational/prompts | Developer Documentation}
* @see {@link https://github.com/actions-on-google/actions-builder-conversation-components-nodejs/ | Conversation Components code sample}
*/
export type PromptItem =
string |
Simple |
Content |
Card |
Collection |
CollectionBrowse |
Canvas |
Image |
List |
Media |
Table |
Link |
Suggestion |
OrderUpdate
const addSimple = (prompt: Prompt, simple: Simple) => {
if(prompt.firstSimple && prompt.lastSimple) {
throw new Error('Error adding simple response: Two simple responses already defined')
}
if(prompt.firstSimple && !prompt.lastSimple){
prompt.lastSimple = new Simple(simple)
}
if(!prompt.firstSimple && !prompt.lastSimple){
prompt.firstSimple = new Simple(simple)
}
if(!prompt.firstSimple && prompt.lastSimple){
prompt.firstSimple = new Simple(simple)
}
}
const appendFirstSimple = (prompt: Prompt, speech: string) => {
if(!prompt.firstSimple){
prompt.firstSimple = new Simple(speech)
} else {
prompt.firstSimple.speech += speech
}
}
const addSuggestion = (prompt: Prompt, suggestion: Suggestion) => {
if (!prompt.suggestions) {
prompt.suggestions = []
}
// Maximum of 8 suggestions
// https://developers.google.com/assistant/conversational/prompts#suggestions
if (prompt.suggestions.length >= 8) {
throw new Error('Error adding suggestion: Max number of suggestion (8) already added.')
} else {
prompt.suggestions.push(suggestion)
}
}
export class Prompt implements Schema.Prompt {
content: Content | undefined
firstSimple: Simple | undefined
lastSimple: Simple | undefined
link: Link | undefined
suggestions: Suggestion[] | undefined
canvas: Canvas | undefined
orderUpdate: OrderUpdate | undefined
override: boolean | undefined
/** @hidden */
constructor(input: Schema.Prompt = {}) {
const {
override = false,
content = null,
firstSimple = null,
lastSimple = null,
link = null,
suggestions = null,
canvas = null,
orderUpdate = null,
} = input
this.override = override
if (content) {
this.content = new Content(content)
}
if (firstSimple) {
this.firstSimple = new Simple(firstSimple)
}
if (lastSimple) {
this.lastSimple = new Simple(lastSimple)
}
if (link) {
this.link = new Link(link)
}
if (suggestions){
for (const suggestion of suggestions) {
addSuggestion(this, new Suggestion(suggestion))
}
}
if (canvas){
this.canvas = new Canvas(canvas)
}
if (orderUpdate) {
this.orderUpdate = new OrderUpdate(orderUpdate)
}
}
append(item: string) {
if (typeof item === 'string') {
appendFirstSimple(this, item)
} else {
throw new Error('append only supports strings')
}
return this
}
add(...items: PromptItem[]) {
for (const item of items) {
if (typeof item === 'string') {
addSimple(this, new Simple(item))
continue
}
if (item instanceof Content) {
this.content = item
continue
}
if (item instanceof Card) {
if (!this.content) {
this.content = new Content({})
}
this.content.card = item
continue
}
if (item instanceof Collection) {
if (!this.content) {
this.content = new Content({})
}
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
}
if (item instanceof Image) {
if (!this.content) {
this.content = new Content({})
}
this.content.image = item
continue
}
if (item instanceof List) {
if (!this.content) {
this.content = new Content({})
}
this.content.list = item
continue
}
if (item instanceof Media) {
if (!this.content) {
this.content = new Content({})
}
this.content.media = item
continue
}
if (item instanceof Table) {
if (!this.content) {
this.content = new Content({})
}
this.content.table = item
continue
}
if (item instanceof Simple) {
addSimple(this, item)
continue
}
if (item instanceof Link) {
this.link = item
continue
}
if (item instanceof OrderUpdate) {
this.orderUpdate = item
continue
}
if (item instanceof Suggestion) {
addSuggestion(this, item)
continue
}
}
return this
}
}