-
Notifications
You must be signed in to change notification settings - Fork 565
/
Copy pathwebviewProvider.ts
99 lines (89 loc) · 3.25 KB
/
webviewProvider.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
/*!
* Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
* SPDX-License-Identifier: Apache-2.0
*/
import {
EventEmitter,
CancellationToken,
Webview,
WebviewView,
WebviewViewProvider,
WebviewViewResolveContext,
Uri,
} from 'vscode'
import { QuickActionCommandGroup } from '@aws/mynah-ui'
import * as path from 'path'
import { LanguageServerResolver } from 'aws-core-vscode/shared'
import { disclaimer } from 'aws-core-vscode/amazonq'
export class AmazonQChatViewProvider implements WebviewViewProvider {
public static readonly viewType = 'aws.amazonq.AmazonQChatView'
private readonly onDidResolveWebviewEmitter = new EventEmitter<void>()
public readonly onDidResolveWebview = this.onDidResolveWebviewEmitter.event
webview: Webview | undefined
private readonly quickActionCommands: QuickActionCommandGroup[] = [
{
groupName: 'Quick Actions',
commands: [
{
command: '/help',
icon: 'help',
description: 'Learn more about Amazon Q',
},
{
command: '/clear',
icon: 'trash',
description: 'Clear this session',
},
],
},
]
constructor(private readonly mynahUIPath: string) {}
public async resolveWebviewView(
webviewView: WebviewView,
context: WebviewViewResolveContext,
_token: CancellationToken
) {
this.webview = webviewView.webview
const lspDir = Uri.parse(LanguageServerResolver.defaultDir)
webviewView.webview.options = {
enableScripts: true,
enableCommandUris: true,
localResourceRoots: [lspDir, Uri.parse(path.dirname(this.mynahUIPath))],
}
const uiPath = webviewView.webview.asWebviewUri(Uri.parse(this.mynahUIPath)).toString()
webviewView.webview.html = await this.getWebviewContent(uiPath)
this.onDidResolveWebviewEmitter.fire()
}
private async getWebviewContent(mynahUIPath: string) {
const disclaimerAcknowledged = await disclaimer.disclaimerAcknowledged()
return `
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<title>Chat</title>
<style>
body,
html {
background-color: var(--mynah-color-bg);
color: var(--mynah-color-text-default);
height: 100%;
width: 100%;
overflow: hidden;
margin: 0;
padding: 0;
}
</style>
</head>
<body>
<script type="text/javascript" src="${mynahUIPath.toString()}" defer onload="init()"></script>
<script type="text/javascript">
const init = () => {
amazonQChat.createChat(acquireVsCodeApi(), { disclaimerAcknowledged: ${disclaimerAcknowledged}, quickActionCommands: ${JSON.stringify(this.quickActionCommands)}});
}
</script>
</body>
</html>`
}
}