forked from microsoft/CopilotStudioSamples
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathindex.html
More file actions
217 lines (196 loc) · 6.82 KB
/
Copy pathindex.html
File metadata and controls
217 lines (196 loc) · 6.82 KB
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8" />
<title>Copilot Chat with Sendbox Pinned</title>
<script src="https://unpkg.com/react@16.8.6/umd/react.production.min.js"></script>
<script src="https://unpkg.com/react-dom@16.8.6/umd/react-dom.production.min.js"></script>
<script src="https://unpkg.com/botframework-webchat@4.18.1-main.20241015.4744d69/dist/webchat.js"></script>
<script src="https://unpkg.com/botframework-webchat-fluent-theme@4.18.1-main.20241015.4744d69/dist/botframework-webchat-fluent-theme.production.min.js"></script>
<script src="https://unpkg.com/copilot-studio-direct-to-engine-chat-adapter@0.0.0-main.20241022-173702.3e37c28/dist/copilot-studio-direct-to-engine-chat-adapter.production.global.js"></script>
<script src="https://alcdn.msauth.net/browser/2.35.0/js/msal-browser.min.js"></script>
<style>
html,
body {
height: 100%;
margin: 0;
background-color: #f0f0f0;
font-family: "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
}
#webchat-container {
width: 1000px;
height: 600px;
background-color: white;
box-shadow: 0 4px 8px rgba(0, 0, 0, 0.1);
border-radius: 8px;
padding: 10px;
position: absolute;
top: 50%;
left: 50%;
transform: translate(-50%, -50%);
display: flex;
flex-direction: column;
justify-content: flex-end;
overflow: hidden;
}
#webchat {
display: flex;
flex-direction: column;
height: 100%;
width: 100%;
}
.typing-indicator {
min-height: 24px;
transition: padding 0.2s ease;
padding: 0;
display: flex;
align-items: flex-end;
}
.typing-indicator.active {
padding: 8px 0;
}
.typing-bubble {
background-color: #fff;
border-radius: 8px;
box-shadow: 0 1px 4px rgba(0, 0, 0, 0.1);
padding: 8px 12px;
margin-left: 16px;
font-size: 14px;
color: #242424;
font-style: italic;
border: 1px solid #e6e6e6;
max-width: 75%;
animation: blinkBubble 1.4s infinite;
}
@keyframes blinkBubble {
0%,
100% {
opacity: 1;
}
50% {
opacity: 0.5;
}
}
</style>
</head>
<body>
<div id="webchat-container">
<div id="webchat" role="main"></div>
</div>
<script>
const msalInstance = new msal.PublicClientApplication({
auth: {
clientId: "YOUR_CLIENT_ID", // Replace with your Microsoft Entra application client ID
authority: "https://login.microsoftonline.com/YOUR_TENANT_ID", // Replace with your tenant ID
redirectUri: window.location.origin
},
cache: {
cacheLocation: "localStorage",
storeAuthStateInCookie: true
}
});
const loginRequest = {
scopes: ["https://api.powerplatform.com/CopilotStudio.Copilots.Invoke"]
};
function TypingIndicator({ isTyping }) {
return React.createElement(
'div',
{ className: `typing-indicator${isTyping ? ' active' : ''}` },
isTyping
? React.createElement('div', { className: 'typing-bubble' }, 'Retrieving...')
: ''
);
}
async function getToken() {
const accounts = msalInstance.getAllAccounts();
if (accounts.length > 0) {
return (await msalInstance.acquireTokenSilent({ ...loginRequest, account: accounts[0] })).accessToken;
} else {
return (await msalInstance.loginPopup({ ...loginRequest, prompt: "consent" })).accessToken;
}
}
async function startChat(token) {
const strategy = new window.CopilotStudioDirectToEngineChatAdapter.ThirdPartyPublishedBotStrategy({
botSchema: 'YOUR_AGENT_SCHEMA', // Replace with your Copilot Studio Agent schema name
environmentEndpointURL: new URL('https://YOUR_ENVIRONMENT_ENDPOINT.environment.api.powerplatform.com/'), // Replace with your Power Platform environment endpoint
getToken: () => token,
transport: 'auto'
});
const directLine = window.CopilotStudioDirectToEngineChatAdapter.toDirectLineJS(
window.CopilotStudioDirectToEngineChatAdapter.createHalfDuplexChatAdapter(strategy, { emitStartConversationEvent: true })
);
const { Components } = window.WebChat;
const { useState, useEffect } = window.React;
const App = () => {
const [isTyping, setIsTyping] = useState(false);
useEffect(() => {
const sub = directLine.activity$.subscribe(activity => {
if (
activity.type === 'event' &&
activity.name === 'DynamicPlanReceived' &&
Array.isArray(activity?.value?.steps) &&
activity.value.steps.some(step => step.includes('UniversalSearchTool'))
) {
setIsTyping(true);
} else if (
activity.type === 'event' &&
activity.name === 'DynamicPlanStepFinished' &&
activity?.value?.taskDialogId === 'P:UniversalSearchTool'
) {
setIsTyping(false);
}
});
return () => sub.unsubscribe();
}, []);
return React.createElement(
Components.AccessKeySinkSurface,
{
style: {
display: 'flex',
flexDirection: 'column',
justifyContent: 'flex-end',
height: '100%'
}
},
React.createElement(Components.BasicToaster),
React.createElement(Components.BasicTranscript),
React.createElement(TypingIndicator, { isTyping }),
React.createElement(Components.BasicSendBox)
);
};
window.ReactDOM.render(
React.createElement(
window.WebChat.FluentThemeProvider,
null,
React.createElement(
Components.Composer,
{
directLine,
styleOptions: {
backgroundColor: 'var(--colorNeutralBackground1)',
bubbleBackground: 'var(--colorNeutralBackground2)',
bubbleTextColor: 'var(--colorNeutralForeground1)',
bubbleFromUserBackground: 'var(--colorBrandStroke2)',
bubbleFromUserTextColor: 'var(--colorNeutralForeground1)',
timestampColor: 'var(--colorNeutralForeground3)',
bubbleBorderRadius: 10,
bubbleFromUserBorderRadius: 10,
}
},
React.createElement(App)
)
),
document.getElementById('webchat')
);
}
document.addEventListener('DOMContentLoaded', async () => {
try {
const token = await getToken();
startChat(token);
} catch (e) {
document.body.innerHTML = `<div style="color: red; font-family: sans-serif; padding: 1em;">Authentication failed: ${e.message}</div>`;
}
});
</script>
</body>
</html>