@microsoft/agents-copilotstudio-client 1.1.0-alpha.12.gecd5464d28 → 1.1.0-alpha.14.g5a79897a92
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
|
@@ -6,6 +6,7 @@
|
|
|
6
6
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
7
|
exports.CopilotStudioWebChat = void 0;
|
|
8
8
|
const uuid_1 = require("uuid");
|
|
9
|
+
const agents_activity_1 = require("@microsoft/agents-activity");
|
|
9
10
|
const rxjs_1 = require("rxjs");
|
|
10
11
|
const logger_1 = require("@microsoft/agents-activity/logger");
|
|
11
12
|
const logger = (0, logger_1.debug)('copilot-studio:webchat');
|
|
@@ -135,6 +136,8 @@ class CopilotStudioWebChat {
|
|
|
135
136
|
logger.debug('--> Connection established.');
|
|
136
137
|
notifyTyping();
|
|
137
138
|
const activity = await client.startConversationAsync();
|
|
139
|
+
// Remove replyToId to avoid timeout issues with WebChat on first activity.
|
|
140
|
+
delete activity.replyToId;
|
|
138
141
|
conversation = activity.conversation;
|
|
139
142
|
sequence = 0;
|
|
140
143
|
notifyActivity(activity);
|
|
@@ -173,17 +176,21 @@ class CopilotStudioWebChat {
|
|
|
173
176
|
}
|
|
174
177
|
return createObservable(async (subscriber) => {
|
|
175
178
|
try {
|
|
176
|
-
const id = (0, uuid_1.v4)();
|
|
177
179
|
logger.info('--> Sending activity to Copilot Studio ...');
|
|
178
|
-
|
|
180
|
+
const newActivity = agents_activity_1.Activity.fromObject({
|
|
181
|
+
...activity,
|
|
182
|
+
id: (0, uuid_1.v4)(),
|
|
183
|
+
attachments: await processAttachments(activity)
|
|
184
|
+
});
|
|
185
|
+
notifyActivity(newActivity);
|
|
179
186
|
notifyTyping();
|
|
180
|
-
const activities = await client.sendActivity(
|
|
187
|
+
const activities = await client.sendActivity(newActivity);
|
|
181
188
|
for (const responseActivity of activities) {
|
|
182
189
|
notifyActivity(responseActivity);
|
|
183
190
|
}
|
|
184
|
-
subscriber.next(id);
|
|
191
|
+
subscriber.next(newActivity.id);
|
|
185
192
|
subscriber.complete();
|
|
186
|
-
logger.info('
|
|
193
|
+
logger.info('<-- Activity received correctly from Copilot Studio.');
|
|
187
194
|
}
|
|
188
195
|
catch (error) {
|
|
189
196
|
logger.error('Error sending Activity to Copilot Studio:', error);
|
|
@@ -203,6 +210,67 @@ class CopilotStudioWebChat {
|
|
|
203
210
|
}
|
|
204
211
|
}
|
|
205
212
|
exports.CopilotStudioWebChat = CopilotStudioWebChat;
|
|
213
|
+
/**
|
|
214
|
+
* Processes activity attachments.
|
|
215
|
+
* @param activity The activity to process for attachments.
|
|
216
|
+
* @returns A promise that resolves to the activity with all attachments converted.
|
|
217
|
+
*/
|
|
218
|
+
async function processAttachments(activity) {
|
|
219
|
+
var _a;
|
|
220
|
+
if (activity.type !== 'message' || !((_a = activity.attachments) === null || _a === void 0 ? void 0 : _a.length)) {
|
|
221
|
+
return activity.attachments || [];
|
|
222
|
+
}
|
|
223
|
+
const attachments = [];
|
|
224
|
+
for (const attachment of activity.attachments) {
|
|
225
|
+
const processed = await processBlobAttachment(attachment);
|
|
226
|
+
attachments.push(processed);
|
|
227
|
+
}
|
|
228
|
+
return attachments;
|
|
229
|
+
}
|
|
230
|
+
/**
|
|
231
|
+
* Processes a blob attachment to convert its content URL to a data URL.
|
|
232
|
+
* @param attachment The attachment to process.
|
|
233
|
+
* @returns A promise that resolves to the processed attachment.
|
|
234
|
+
*/
|
|
235
|
+
async function processBlobAttachment(attachment) {
|
|
236
|
+
let newContentUrl = attachment.contentUrl;
|
|
237
|
+
if (!(newContentUrl === null || newContentUrl === void 0 ? void 0 : newContentUrl.startsWith('blob:'))) {
|
|
238
|
+
return attachment;
|
|
239
|
+
}
|
|
240
|
+
try {
|
|
241
|
+
const response = await fetch(newContentUrl);
|
|
242
|
+
if (!response.ok) {
|
|
243
|
+
throw new Error(`Failed to fetch blob URL: ${response.status} ${response.statusText}`);
|
|
244
|
+
}
|
|
245
|
+
const blob = await response.blob();
|
|
246
|
+
const arrayBuffer = await blob.arrayBuffer();
|
|
247
|
+
const base64 = arrayBufferToBase64(arrayBuffer);
|
|
248
|
+
newContentUrl = `data:${blob.type};base64,${base64}`;
|
|
249
|
+
}
|
|
250
|
+
catch (error) {
|
|
251
|
+
newContentUrl = attachment.contentUrl;
|
|
252
|
+
logger.error('Error processing blob attachment:', newContentUrl, error);
|
|
253
|
+
}
|
|
254
|
+
return { ...attachment, contentUrl: newContentUrl };
|
|
255
|
+
}
|
|
256
|
+
/**
|
|
257
|
+
* Converts an ArrayBuffer to a base64 string.
|
|
258
|
+
* @param buffer The ArrayBuffer to convert.
|
|
259
|
+
* @returns The base64 encoded string.
|
|
260
|
+
*/
|
|
261
|
+
function arrayBufferToBase64(buffer) {
|
|
262
|
+
// Node.js environment
|
|
263
|
+
const BufferClass = typeof globalThis.Buffer === 'function' ? globalThis.Buffer : undefined;
|
|
264
|
+
if (BufferClass && typeof BufferClass.from === 'function') {
|
|
265
|
+
return BufferClass.from(buffer).toString('base64');
|
|
266
|
+
}
|
|
267
|
+
// Browser environment
|
|
268
|
+
let binary = '';
|
|
269
|
+
for (const byte of new Uint8Array(buffer)) {
|
|
270
|
+
binary += String.fromCharCode(byte);
|
|
271
|
+
}
|
|
272
|
+
return btoa(binary);
|
|
273
|
+
}
|
|
206
274
|
/**
|
|
207
275
|
* Creates an RxJS Observable that wraps an asynchronous function execution.
|
|
208
276
|
*
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"copilotStudioWebChat.js","sourceRoot":"","sources":["../../src/copilotStudioWebChat.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+BAAiC;
|
|
1
|
+
{"version":3,"file":"copilotStudioWebChat.js","sourceRoot":"","sources":["../../src/copilotStudioWebChat.ts"],"names":[],"mappings":";AAAA;;;GAGG;;;AAEH,+BAAiC;AAEjC,gEAAsF;AACtF,+BAAmE;AAGnE,8DAAyD;AAEzD,MAA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;AACH,SAAS,mBAAmB,CAAE,MAAmB;IAC/C,sBAAsB;IACtB,MAAM,WAAW,GAAG,OAAO,UAAU,CAAC,MAAM,KAAK,UAAU,CAAC,CAAC,CAAC,UAAU,CAAC,MAAM,CAAC,CAAC,CAAC,SAAS,CAAA;IAC3F,IAAI,WAAW,IAAI,OAAO,WAAW,CAAC,IAAI,KAAK,UAAU,EAAE,CAAC;QAC1D,OAAO,WAAW,CAAC,IAAI,CAAC,MAAM,CAAC,CAAC,QAAQ,CAAC,QAAQ,CAAC,CAAA;IACpD,CAAC;IAED,sBAAsB;IACtB,IAAI,MAAM,GAAG,EAAE,CAAA;IACf,KAAK,MAAM,IAAI,IAAI,IAAI,UAAU,CAAC,MAAM,CAAC,EAAE,CAAC;QAC1C,MAAM,IAAI,MAAM,CAAC,YAAY,CAAC,IAAI,CAAC,CAAA;IACrC,CAAC;IACD,OAAO,IAAI,CAAC,MAAM,CAAC,CAAA;AACrB,CAAC;AAED;;;;;;;;;;;;;;;;;;;;;;;;;;;;;GA6BG;AACH,SAAS,gBAAgB,CAAK,EAAuC;IACnE,OAAO,IAAI,iBAAU,CAAI,CAAC,UAAyB,EAAE,EAAE;QACrD,OAAO,CAAC,OAAO,CAAC,EAAE,CAAC,UAAU,CAAC,CAAC,CAAC,KAAK,CAAC,CAAC,KAAK,EAAE,EAAE,CAAC,UAAU,CAAC,KAAK,CAAC,KAAK,CAAC,CAAC,CAAA;IAC3E,CAAC,CAAC,CAAA;AACJ,CAAC"}
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@microsoft/agents-copilotstudio-client",
|
|
3
|
-
"version": "1.1.0-alpha.
|
|
3
|
+
"version": "1.1.0-alpha.14.g5a79897a92",
|
|
4
4
|
"homepage": "https://github.com/microsoft/Agents-for-js",
|
|
5
5
|
"repository": {
|
|
6
6
|
"type": "git",
|
|
@@ -27,7 +27,7 @@
|
|
|
27
27
|
"build:browser": "esbuild --platform=browser --target=es2019 --format=esm --bundle --sourcemap --minify --outfile=dist/src/browser.mjs src/index.ts"
|
|
28
28
|
},
|
|
29
29
|
"dependencies": {
|
|
30
|
-
"@microsoft/agents-activity": "1.1.0-alpha.
|
|
30
|
+
"@microsoft/agents-activity": "1.1.0-alpha.14.g5a79897a92",
|
|
31
31
|
"axios": "^1.9.0",
|
|
32
32
|
"rxjs": "7.8.2",
|
|
33
33
|
"uuid": "^11.1.0"
|
|
@@ -5,7 +5,7 @@
|
|
|
5
5
|
|
|
6
6
|
import { v4 as uuid } from 'uuid'
|
|
7
7
|
|
|
8
|
-
import { Activity, ConversationAccount } from '@microsoft/agents-activity'
|
|
8
|
+
import { Activity, Attachment, ConversationAccount } from '@microsoft/agents-activity'
|
|
9
9
|
import { Observable, BehaviorSubject, type Subscriber } from 'rxjs'
|
|
10
10
|
|
|
11
11
|
import { CopilotStudioClient } from './copilotStudioClient'
|
|
@@ -219,6 +219,8 @@ export class CopilotStudioWebChat {
|
|
|
219
219
|
logger.debug('--> Connection established.')
|
|
220
220
|
notifyTyping()
|
|
221
221
|
const activity = await client.startConversationAsync()
|
|
222
|
+
// Remove replyToId to avoid timeout issues with WebChat on first activity.
|
|
223
|
+
delete activity.replyToId
|
|
222
224
|
conversation = activity.conversation
|
|
223
225
|
sequence = 0
|
|
224
226
|
notifyActivity(activity)
|
|
@@ -264,22 +266,25 @@ export class CopilotStudioWebChat {
|
|
|
264
266
|
|
|
265
267
|
return createObservable<string>(async (subscriber) => {
|
|
266
268
|
try {
|
|
267
|
-
const id = uuid()
|
|
268
|
-
|
|
269
269
|
logger.info('--> Sending activity to Copilot Studio ...')
|
|
270
|
+
const newActivity = Activity.fromObject({
|
|
271
|
+
...activity,
|
|
272
|
+
id: uuid(),
|
|
273
|
+
attachments: await processAttachments(activity)
|
|
274
|
+
})
|
|
270
275
|
|
|
271
|
-
notifyActivity(
|
|
276
|
+
notifyActivity(newActivity)
|
|
272
277
|
notifyTyping()
|
|
273
278
|
|
|
274
|
-
const activities = await client.sendActivity(
|
|
279
|
+
const activities = await client.sendActivity(newActivity)
|
|
275
280
|
|
|
276
281
|
for (const responseActivity of activities) {
|
|
277
282
|
notifyActivity(responseActivity)
|
|
278
283
|
}
|
|
279
284
|
|
|
280
|
-
subscriber.next(id)
|
|
285
|
+
subscriber.next(newActivity.id!)
|
|
281
286
|
subscriber.complete()
|
|
282
|
-
logger.info('
|
|
287
|
+
logger.info('<-- Activity received correctly from Copilot Studio.')
|
|
283
288
|
} catch (error) {
|
|
284
289
|
logger.error('Error sending Activity to Copilot Studio:', error)
|
|
285
290
|
subscriber.error(error)
|
|
@@ -299,6 +304,74 @@ export class CopilotStudioWebChat {
|
|
|
299
304
|
}
|
|
300
305
|
}
|
|
301
306
|
|
|
307
|
+
/**
|
|
308
|
+
* Processes activity attachments.
|
|
309
|
+
* @param activity The activity to process for attachments.
|
|
310
|
+
* @returns A promise that resolves to the activity with all attachments converted.
|
|
311
|
+
*/
|
|
312
|
+
async function processAttachments (activity: Activity): Promise<Attachment[]> {
|
|
313
|
+
if (activity.type !== 'message' || !activity.attachments?.length) {
|
|
314
|
+
return activity.attachments || []
|
|
315
|
+
}
|
|
316
|
+
|
|
317
|
+
const attachments: Attachment[] = []
|
|
318
|
+
for (const attachment of activity.attachments) {
|
|
319
|
+
const processed = await processBlobAttachment(attachment)
|
|
320
|
+
attachments.push(processed)
|
|
321
|
+
}
|
|
322
|
+
|
|
323
|
+
return attachments
|
|
324
|
+
}
|
|
325
|
+
|
|
326
|
+
/**
|
|
327
|
+
* Processes a blob attachment to convert its content URL to a data URL.
|
|
328
|
+
* @param attachment The attachment to process.
|
|
329
|
+
* @returns A promise that resolves to the processed attachment.
|
|
330
|
+
*/
|
|
331
|
+
async function processBlobAttachment (attachment: Attachment): Promise<Attachment> {
|
|
332
|
+
let newContentUrl = attachment.contentUrl
|
|
333
|
+
if (!newContentUrl?.startsWith('blob:')) {
|
|
334
|
+
return attachment
|
|
335
|
+
}
|
|
336
|
+
|
|
337
|
+
try {
|
|
338
|
+
const response = await fetch(newContentUrl)
|
|
339
|
+
if (!response.ok) {
|
|
340
|
+
throw new Error(`Failed to fetch blob URL: ${response.status} ${response.statusText}`)
|
|
341
|
+
}
|
|
342
|
+
|
|
343
|
+
const blob = await response.blob()
|
|
344
|
+
const arrayBuffer = await blob.arrayBuffer()
|
|
345
|
+
const base64 = arrayBufferToBase64(arrayBuffer)
|
|
346
|
+
newContentUrl = `data:${blob.type};base64,${base64}`
|
|
347
|
+
} catch (error) {
|
|
348
|
+
newContentUrl = attachment.contentUrl
|
|
349
|
+
logger.error('Error processing blob attachment:', newContentUrl, error)
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
return { ...attachment, contentUrl: newContentUrl }
|
|
353
|
+
}
|
|
354
|
+
|
|
355
|
+
/**
|
|
356
|
+
* Converts an ArrayBuffer to a base64 string.
|
|
357
|
+
* @param buffer The ArrayBuffer to convert.
|
|
358
|
+
* @returns The base64 encoded string.
|
|
359
|
+
*/
|
|
360
|
+
function arrayBufferToBase64 (buffer: ArrayBuffer): string {
|
|
361
|
+
// Node.js environment
|
|
362
|
+
const BufferClass = typeof globalThis.Buffer === 'function' ? globalThis.Buffer : undefined
|
|
363
|
+
if (BufferClass && typeof BufferClass.from === 'function') {
|
|
364
|
+
return BufferClass.from(buffer).toString('base64')
|
|
365
|
+
}
|
|
366
|
+
|
|
367
|
+
// Browser environment
|
|
368
|
+
let binary = ''
|
|
369
|
+
for (const byte of new Uint8Array(buffer)) {
|
|
370
|
+
binary += String.fromCharCode(byte)
|
|
371
|
+
}
|
|
372
|
+
return btoa(binary)
|
|
373
|
+
}
|
|
374
|
+
|
|
302
375
|
/**
|
|
303
376
|
* Creates an RxJS Observable that wraps an asynchronous function execution.
|
|
304
377
|
*
|