/
emulator.tsx
388 lines (346 loc) · 14.7 KB
/
emulator.tsx
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
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
//
// Copyright (c) Microsoft. All rights reserved.
// Licensed under the MIT license.
//
// Microsoft Bot Framework: http://botframework.com
//
// Bot Framework Emulator Github:
// https://github.com/Microsoft/BotFramwork-Emulator
//
// Copyright (c) Microsoft Corporation
// All rights reserved.
//
// MIT License:
// Permission is hereby granted, free of charge, to any person obtaining
// a copy of this software and associated documentation files (the
// "Software"), to deal in the Software without restriction, including
// without limitation the rights to use, copy, modify, merge, publish,
// distribute, sublicense, and/or sell copies of the Software, and to
// permit persons to whom the Software is furnished to do so, subject to
// the following conditions:
//
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED ""AS IS"", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
import * as BotChat from 'botframework-webchat';
import { uniqueId, uniqueIdv4 } from '@bfemulator/sdk-shared';
import { Splitter, SplitButton } from '@bfemulator/ui-react';
import base64Url from 'base64url';
import { IEndpointService } from 'botframework-config/lib/schema';
import * as React from 'react';
import { connect } from 'react-redux';
import { BehaviorSubject } from 'rxjs';
import * as ChatActions from '../../../data/action/chatActions';
import { updateDocument } from '../../../data/action/editorActions';
import * as PresentationActions from '../../../data/action/presentationActions';
import { Document } from '../../../data/reducer/editor';
import { RootState } from '../../../data/store';
import { CommandServiceImpl } from '../../../platform/commands/commandServiceImpl';
import ChatPanel from './chatPanel/chatPanel';
import LogPanel from './logPanel/logPanel';
import PlaybackBar from './playbackBar/playbackBar';
import { debounce } from '../../../utils';
import { newNotification, Notification, SharedConstants } from '@bfemulator/app-shared';
import * as styles from './emulator.scss';
import { beginAdd } from '../../../data/action/notificationActions';
import { InspectorContainer } from './parts';
import { ToolBar } from './toolbar/toolbar';
const { encode } = base64Url;
const RestartConversationOptions = {
NewUserId: 'Restart with new user ID',
SameUserId: 'Restart with same user ID'
};
export type EmulatorMode = 'transcript' | 'livechat';
interface EmulatorProps {
activeDocumentId?: string;
clearLog?: (documentId: string) => void;
conversationId?: string;
createErrorNotification?: (notification: Notification) => void;
dirty?: boolean;
document?: any;
documentId?: string;
enablePresentationMode?: (enabled: boolean) => void;
endpointId?: string;
endpointService?: IEndpointService;
mode?: EmulatorMode;
newConversation?: (documentId: string, options: any) => void;
presentationModeEnabled?: boolean;
setInspectorObjects?: (documentId: string, objects: any) => void;
updateChat?: (documentId: string, updatedValues: any) => void;
updateDocument?: (documentId: string, updatedValues: Partial<Document>) => void;
url?: string;
}
class EmulatorComponent extends React.Component<EmulatorProps, {}> {
private readonly onVerticalSizeChange = debounce((sizes) => {
this.props.document.ui = {
...this.props.document.ui,
verticalSplitter: sizes
};
}, 500);
private readonly onHorizontalSizeChange = debounce((sizes) => {
this.props.document.ui = {
...this.props.document.ui,
horizontalSplitter: sizes
};
}, 500);
constructor(props: EmulatorProps) {
super(props);
}
shouldStartNewConversation(props: EmulatorProps = this.props): boolean {
return !props.document.directLine ||
(props.document.conversationId !== props.document.directLine.conversationId);
}
componentWillMount() {
window.addEventListener('keydown', this.keyboardEventListener);
if (this.shouldStartNewConversation()) {
this.startNewConversation();
}
}
componentWillUnmount() {
window.removeEventListener('keydown', this.keyboardEventListener);
}
componentWillReceiveProps(nextProps: EmulatorProps) {
const { props, keyboardEventListener, startNewConversation } = this;
const { document = {} } = props;
const { document: nextDocument = {} } = nextProps;
const documentOrUserIdChanged = (!nextDocument.directLine && document.documentId !== nextDocument.documentId)
|| (document.userId !== nextDocument.userId);
if (documentOrUserIdChanged) {
startNewConversation(nextProps).catch();
}
const switchedDocuments = props.activeDocumentId !== nextProps.activeDocumentId;
const switchedToThisDocument = (nextProps.activeDocumentId === props.documentId);
if (switchedDocuments) {
if (switchedToThisDocument) {
window.removeEventListener('keydown', keyboardEventListener);
window.addEventListener('keydown', keyboardEventListener);
} else {
window.removeEventListener('keydown', keyboardEventListener);
}
}
}
startNewConversation = async (props: EmulatorProps = this.props): Promise<any> => {
if (props.document.subscription) {
props.document.subscription.unsubscribe();
}
const selectedActivity$ = new BehaviorSubject({});
const subscription = selectedActivity$.subscribe((obj: any) => {
if (obj && obj.activity) {
this.props.setInspectorObjects(props.document.documentId, obj.activity);
}
});
// TODO: Don't append mode
const conversationId = `${ uniqueId() }|${ props.mode }`;
const options = {
conversationId,
conversationMode: props.mode,
endpointId: props.endpointId
};
if (props.document.directLine) {
props.document.directLine.end();
}
this.initConversation(props, options, selectedActivity$, subscription);
if (props.mode === 'transcript') {
try {
const conversation = await CommandServiceImpl.remoteCall(
SharedConstants.Commands.Emulator.NewTranscript,
conversationId
);
if (props.document && props.document.inMemory && props.document.activities) {
try {
// transcript was deep linked via protocol or is generated in-memory via chatdown,
// and should just be fed its own activities attached to the document
await CommandServiceImpl.remoteCall(
SharedConstants.Commands.Emulator.FeedTranscriptFromMemory,
conversation.conversationId,
props.document.botId,
props.document.userId,
props.document.activities
);
} catch (err) {
throw new Error(`Error while feeding deep-linked transcript to conversation: ${err}`);
}
} else {
try {
// the transcript is on disk, so its activities need to be read on the main side and fed in
const fileInfo: { fileName: string, filePath: string } = await CommandServiceImpl.remoteCall(
SharedConstants.Commands.Emulator.FeedTranscriptFromDisk,
conversation.conversationId,
props.document.botId,
props.document.userId,
props.document.documentId
);
this.props.updateDocument(this.props.documentId, fileInfo);
} catch (err) {
throw new Error(`Error while feeding transcript on disk to conversation: ${err}`);
}
}
} catch (err) {
const errMsg = `Error creating a new conversation in transcript mode: ${err}`;
const notification = newNotification(errMsg);
this.props.createErrorNotification(notification);
}
}
}
initConversation(props: EmulatorProps, options: any, selectedActivity$: any, subscription: any): void {
const encodedOptions = encode(JSON.stringify(options));
// TODO: We need to use encoded token because we need to pass both endpoint ID and conversation ID
// We should think about a better model to pass conversation ID from Web Chat to emulator core
const directLine = new BotChat.DirectLine({
secret: encodedOptions,
domain: `${ this.props.url }/v3/directline`,
webSocket: false
});
this.props.newConversation(props.documentId, {
conversationId: options.conversationId,
// webChatStore,
directLine,
selectedActivity$,
subscription
});
}
render(): JSX.Element {
return this.props.presentationModeEnabled ? this.renderPresentationView() : this.renderDefaultView();
}
renderPresentationView(): JSX.Element {
const transcriptMode = this.props.mode === 'transcript';
const chatPanelChild = transcriptMode ? (
<div className={ styles.presentationPlaybackDock }>
<PlaybackBar/>
</div>) : null;
return (
<div className={ styles.presentation }>
<div className={ styles.presentationContent }>
<ChatPanel mode={ this.props.mode } document={ this.props.document }
onStartConversation={ this.onStartOverClick }/>
{ chatPanelChild }
</div>
<span
className={ styles.closePresentationIcon }
onClick={ () => this.onPresentationClick(false) }
/>
</div>
);
}
renderDefaultView(): JSX.Element {
const { NewUserId, SameUserId } = RestartConversationOptions;
return (
<div className={ styles.emulator }>
{
this.props.mode === 'livechat' &&
<div className={ styles.header }>
<ToolBar>
<SplitButton
defaultLabel="Restart conversation"
buttonClass={ styles.restartIcon }
options={ [NewUserId, SameUserId] }
onClick={ this.onStartOverClick }/>
<button
className={ `${ styles.saveTranscriptIcon } ${ styles.toolbarIcon || '' }` }
onClick={ this.onExportClick }
>
Save transcript
</button>
</ToolBar>
</div>
}
<div className={ `${styles.content} ${styles.vertical}` }>
<Splitter orientation="vertical" primaryPaneIndex={ 0 }
minSizes={ { 0: 80, 1: 80 } }
initialSizes={ this.getVerticalSplitterSizes }
onSizeChange={ this.onVerticalSizeChange }>
<div className={ styles.content }>
<ChatPanel mode={ this.props.mode }
className={ styles.chatPanel }
document={ this.props.document }
onStartConversation={ this.onStartOverClick }/>
</div>
<div className={ styles.content }>
<Splitter orientation="horizontal" primaryPaneIndex={ 0 }
minSizes={ { 0: 80, 1: 80 } }
initialSizes={ this.getHorizontalSplitterSizes }
onSizeChange={ this.onHorizontalSizeChange }>
<InspectorContainer document={ this.props.document }/>
<LogPanel document={ this.props.document }/>
</Splitter>
</div>
</Splitter>
</div>
</div>
);
}
private getVerticalSplitterSizes = (): { [0]: string } => {
return {
0: `${this.props.document.ui.verticalSplitter[0].percentage}`
};
}
private getHorizontalSplitterSizes = (): { [0]: string } => {
return {
0: `${this.props.document.ui.horizontalSplitter[0].percentage}`
};
}
private onPresentationClick = (enabled: boolean): void => {
this.props.enablePresentationMode(enabled);
}
private onStartOverClick = async (option: string = RestartConversationOptions.NewUserId): Promise<void> => {
const { NewUserId, SameUserId } = RestartConversationOptions;
this.props.clearLog(this.props.document.documentId);
this.props.setInspectorObjects(this.props.document.documentId, []);
switch (option) {
case NewUserId:
const newUserId = uniqueIdv4();
// set new user as current on emulator facilities side
await CommandServiceImpl.remoteCall(SharedConstants.Commands.Emulator.SetCurrentUser, newUserId);
this.props.updateChat(this.props.documentId, { userId: newUserId });
break;
case SameUserId:
this.startNewConversation();
break;
default:
break;
}
}
private onExportClick = (): void => {
if (this.props.document.directLine) {
CommandServiceImpl.remoteCall(
SharedConstants.Commands.Emulator.SaveTranscriptToFile,
this.props.document.directLine.conversationId
);
}
}
private readonly keyboardEventListener: EventListener = (event: KeyboardEvent): void => {
// Meta corresponds to 'Command' on Mac
const ctrlOrCmdPressed = event.getModifierState('Control') || event.getModifierState('Meta');
const shiftPressed = ctrlOrCmdPressed && event.getModifierState('Shift');
const key = event.key.toLowerCase();
if (ctrlOrCmdPressed && shiftPressed && key === 'r') {
this.onStartOverClick();
}
}
}
const mapStateToProps = (state: RootState, { documentId }: { documentId: string }): EmulatorProps => ({
activeDocumentId: state.editor.editors[state.editor.activeEditor].activeDocumentId,
conversationId: state.chat.chats[documentId].conversationId,
document: state.chat.chats[documentId],
endpointId: state.chat.chats[documentId].endpointId,
presentationModeEnabled: state.presentation.enabled
});
const mapDispatchToProps = (dispatch): EmulatorProps => ({
enablePresentationMode: enable => enable ? dispatch(PresentationActions.enable())
: dispatch(PresentationActions.disable()),
setInspectorObjects: (documentId, objects) => dispatch(ChatActions.setInspectorObjects(documentId, objects)),
clearLog: documentId => dispatch(ChatActions.clearLog(documentId)),
newConversation: (documentId, options) => dispatch(ChatActions.newConversation(documentId, options)),
updateChat: (documentId: string, updatedValues: any) => dispatch(ChatActions.updateChat(documentId, updatedValues)),
updateDocument: (documentId, updatedValues: Partial<Document>) => dispatch(updateDocument(documentId, updatedValues)),
createErrorNotification: (notification: Notification) => dispatch(beginAdd(notification))
});
export const Emulator = connect(mapStateToProps, mapDispatchToProps)(EmulatorComponent as any) as any;