This repository has been archived by the owner on Jun 30, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 528
/
skillDialog.ts
294 lines (249 loc) · 12.6 KB
/
skillDialog.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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
/**
* Copyright(c) Microsoft Corporation.All rights reserved.
* Licensed under the MIT License.
*/
import {
Activity,
ActivityTypes,
BotTelemetryClient,
Entity,
SemanticActionStateTypes,
StatePropertyAccessor,
TurnContext } from 'botbuilder';
import { ComponentDialog, DialogContext, DialogInstance, DialogReason, DialogTurnResult,
DialogTurnStatus } from 'botbuilder-dialogs';
import { ActivityExtensions, isProviderTokenResponse, MultiProviderAuthDialog, TokenEvents } from 'botbuilder-solutions';
import { IServiceClientCredentials } from './auth';
import { SkillHttpTransport } from './http';
import { IAction, ISkillManifest, ISlot } from './models';
import { SkillContext } from './skillContext';
import { ISkillTransport, TokenRequestHandler } from './skillTransport';
/**
* The SkillDialog class provides the ability for a Bot to send/receive messages to a remote Skill (itself a Bot).
* The dialog name is that of the underlying Skill it's wrapping.
*/
export class SkillDialog extends ComponentDialog {
private readonly authDialog?: MultiProviderAuthDialog;
private readonly serviceClientCredentials: IServiceClientCredentials;
private readonly skillContextAccessor: StatePropertyAccessor<SkillContext>;
private readonly skillManifest: ISkillManifest;
private readonly skillTransport: ISkillTransport;
private readonly queuedResponses: Partial<Activity>[];
public constructor(
skillManifest: ISkillManifest,
serviceClientCredentials: IServiceClientCredentials,
telemetryClient: BotTelemetryClient,
skillContextAccessor: StatePropertyAccessor<SkillContext>,
authDialog?: MultiProviderAuthDialog,
skillTransport?: ISkillTransport
) {
super(skillManifest.id);
if (skillManifest === undefined) { throw new Error('skillManifest has no value'); }
this.skillManifest = skillManifest;
if (serviceClientCredentials === undefined) { throw new Error('serviceClientCredentials has no value'); }
this.serviceClientCredentials = serviceClientCredentials;
if (telemetryClient === undefined) { throw new Error('telemetryClient has no value'); }
this.telemetryClient = telemetryClient;
this.skillTransport = skillTransport || new SkillHttpTransport(skillManifest, this.serviceClientCredentials);
this.queuedResponses = [];
this.skillContextAccessor = skillContextAccessor;
if (authDialog !== undefined) {
this.authDialog = authDialog;
this.addDialog(this.authDialog);
}
}
public async endDialog(context: TurnContext, instance: DialogInstance, reason: DialogReason): Promise<void> {
if (reason === DialogReason.cancelCalled) {
// when dialog is being ended/cancelled, send an activity to skill
// to cancel all dialogs on the skill side
if (this.skillTransport !== undefined) {
await this.skillTransport.cancelRemoteDialogs(context);
}
}
await super.endDialog(context, instance, reason);
}
/**
* When a SkillDialog is started, a skillBegin event is sent which firstly indicates the Skill is being invoked in Skill mode,
* also slots are also provided where the information exists in the parent Bot.
* @param innerDC inner dialog context.
* @param options options
* @returns dialog turn result.
*/
protected async onBeginDialog(innerDC: DialogContext, options?: object): Promise<DialogTurnResult> {
let slots: SkillContext = new SkillContext();
// Retrieve the SkillContext state object to identify slots (parameters) that can be used to slot-fill when invoking the skill
const sc: SkillContext = await this.skillContextAccessor.get(innerDC.context, new SkillContext());
const skillContext: SkillContext = Object.assign(new SkillContext(), sc);
// In instances where the caller is able to identify/specify the action we process the Action specific slots
// In other scenarios (aggregated skill dispatch) we evaluate all possible slots against context and pass across
// enabling the Skill to perform it's own action identification.
// eslint-disable-next-line @typescript-eslint/tslint/config, @typescript-eslint/no-explicit-any
const actionName: string|undefined = <any> options;
if (actionName !== undefined) {
// Find the specified within the selected Skill for slot filling evaluation
const action: IAction|undefined = this.skillManifest.actions.find((item: IAction): boolean => item.id === actionName);
if (action !== undefined) {
// If the action doesn't define any Slots or SkillContext is empty then we skip slot evaluation
if (action.definition.slots !== undefined && action.definition.slots.length > 0) {
// Match Slots to Skill Context
slots = await this.matchSkillContextToSlots(innerDC, action.definition.slots, skillContext);
}
} else {
const message: string = `Passed Action (${
actionName
}) could not be found within the ${
this.skillManifest.id
} skill manifest action definition.`;
throw new Error(message);
}
} else {
// The caller hasn't got the capability of identifying the action as well as the Skill so we enumerate
// actions and slot data to pass what we have
// Retrieve a distinct list of all slots,
// some actions may use the same slot so we use distinct to ensure we only get 1 instance.
const skillSlots: ISlot[] = this.skillManifest.actions.reduce(
(acc: ISlot[], curr: IAction): ISlot[] => {
const currDistinct: ISlot[] = curr.definition.slots.filter(
(slot: ISlot): boolean => !acc.find((item: ISlot): boolean => item.name === slot.name)
);
return acc.concat(currDistinct);
},
[]);
if (skillSlots !== undefined) {
// Match Slots to Skill Context
slots = await this.matchSkillContextToSlots(innerDC, skillSlots, skillContext);
}
}
const traceMessage: string = `-->Handing off to the ${this.skillManifest.name} skill.`;
await innerDC.context.sendActivity({
type: ActivityTypes.Trace,
text: traceMessage
});
const activity: Activity = innerDC.context.activity;
const entities: { [key: string]: Entity } = {};
// PENDING: Review Entity values
// PENDING: Entity class does not have the prop 'Properties'
slots.forEachObj((value: Object, key: string): void => {
// eslint-disable-next-line @typescript-eslint/tslint/config, @typescript-eslint/no-explicit-any
entities[key] = <any> {
type: '',
properties: value
};
});
activity.semanticAction = {
id: '',
state: SemanticActionStateTypes.Continue,
entities: entities
};
// Send event to Skill/Bot
return this.forwardToSkill(innerDC, activity);
}
/**
* All subsequent messages are forwarded on to the skill.
* @param innerDC Inner Dialog Context.
* @returns DialogTurnResult.
*/
protected async onContinueDialog(innerDC: DialogContext): Promise<DialogTurnResult> {
const activity: Activity = innerDC.context.activity;
if (this.authDialog && innerDC.activeDialog && innerDC.activeDialog.id === this.authDialog.id) {
// Handle magic code auth
const result: DialogTurnResult<Object> = await innerDC.continueDialog();
// forward the token response to the skill
if (result.status === DialogTurnStatus.complete && isProviderTokenResponse(result.result)) {
activity.type = ActivityTypes.Event;
activity.name = TokenEvents.tokenResponseEventName;
activity.value = result.result;
} else {
return result;
}
}
const dialogResult: DialogTurnResult = await this.forwardToSkill(innerDC, activity);
this.skillTransport.disconnect();
return dialogResult;
}
public async matchSkillContextToSlots(innerDc: DialogContext, actionSlots: ISlot[], skillContext: SkillContext): Promise<SkillContext> {
const slots: SkillContext = new SkillContext();
if (actionSlots !== undefined && actionSlots.length > 0) {
actionSlots.forEach(async (slot: ISlot): Promise<void> => {
// For each slot we check to see if there is an exact match, if so we pass this slot across to the skill
const value: Object|undefined = skillContext.getObj(slot.name);
if (skillContext !== undefined && value !== undefined) {
slots.setObj(slot.name, value);
// Send trace to emulator
const traceMessage: string = `-->Matched the ${
slot.name
} slot within SkillContext and passing to the Skill.`;
await innerDc.context.sendActivity({
type: ActivityTypes.Trace,
text: traceMessage
});
}
});
}
return slots;
}
/**
* Forward an inbound activity on to the Skill.
* This is a synchronous operation whereby all response activities are aggregated and returned in one batch.
* @param innerDc Inner DialogContext.
* @param activity Activity.
* @returns DialogTurnResult.
*/
private async forwardToSkill(innerDc: DialogContext, activity: Partial<Activity>): Promise<DialogTurnResult> {
try {
const handoffActivity: boolean = await this.skillTransport.forwardToSkill(
innerDc.context,
activity,
this.getTokenRequestCallback(innerDc));
if (handoffActivity) {
const traceMessage: string = `<--Ending the skill conversation with the ${
this.skillManifest.name
} Skill and handing off to Parent Bot.`;
await innerDc.context.sendActivity({
type: ActivityTypes.Trace,
text: traceMessage
});
return await innerDc.endDialog();
} else {
let dialogResult: DialogTurnResult = {
status: DialogTurnStatus.waiting
};
// if there's any response we need to send to the skill queued
// forward to skill and start a new turn
while (this.queuedResponses.length > 0 &&
dialogResult.status !== DialogTurnStatus.complete &&
dialogResult.status !== DialogTurnStatus.cancelled) {
const lastActivity: Partial<Activity> | undefined = this.queuedResponses.pop();
if (lastActivity !== undefined) {
dialogResult = await this.forwardToSkill(innerDc, lastActivity);
}
}
return dialogResult;
}
} catch (error) {
// something went wrong forwarding to the skill, so end dialog cleanly and throw so the error is logged.
// NOTE: errors within the skill itself are handled by the OnTurnError handler on the adapter.
await innerDc.endDialog();
throw error;
}
}
private getTokenRequestCallback(dialogContext: DialogContext): TokenRequestHandler {
return async (activity: Activity): Promise<void> => {
// Send trace to emulator
await dialogContext.context.sendActivity({
type: ActivityTypes.Trace,
text: '<--Received a Token Request from a skill'
});
if (this.authDialog) {
const authResult: DialogTurnResult<Object> = await dialogContext.beginDialog(this.authDialog.id);
if (isProviderTokenResponse(authResult.result)) {
const tokenEvent: Activity = ActivityExtensions.createReply(activity);
tokenEvent.type = ActivityTypes.Event;
tokenEvent.name = TokenEvents.tokenResponseEventName;
tokenEvent.value = authResult.result;
this.queuedResponses.push(tokenEvent);
}
}
};
}
}