Skip to content

Commit 8f4918f

Browse files
Cherrypick backcompat for Copilot Studio Client into 1.1 release (#812)
* backwards compat for copilot studio client, plus lots of tests for both syntaxs * rationalize some test cases * correct method name * correct method name * correct method name * merge * remove console, fix Activity.fromObject in samples to include type * merged * Fix issue with conversationId in copilotstudio-console sample --------- Co-authored-by: CeciliaAvila <cecilia.avila@southworks.com>
1 parent ab1be1b commit 8f4918f

File tree

5 files changed

+531
-20
lines changed

5 files changed

+531
-20
lines changed

packages/agents-copilotstudio-client/src/copilotStudioClient.ts

Lines changed: 48 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ export class CopilotStudioClient {
160160
* @param emitStartConversationEvent Whether to emit a start conversation event. Defaults to true.
161161
* @returns An async generator yielding the Agent's Activities.
162162
*/
163-
public async * startConversationAsync (emitStartConversationEvent: boolean = true): AsyncGenerator<Activity> {
163+
public async * startConversationStreaming (emitStartConversationEvent: boolean = true): AsyncGenerator<Activity> {
164164
const uriStart: string = getCopilotStudioConnectionUrl(this.settings)
165165
const body = { emitStartConversationEvent }
166166

@@ -169,15 +169,46 @@ export class CopilotStudioClient {
169169
yield * this.postRequestAsync(uriStart, body, 'POST')
170170
}
171171

172+
/**
173+
* Sends an activity to the Copilot Studio service and retrieves the response activities.
174+
* @param activity The activity to send.
175+
* @param conversationId The ID of the conversation. Defaults to the current conversation ID.
176+
* @returns An async generator yielding the Agent's Activities.
177+
*/
178+
public async * sendActivityStreaming (activity: Activity, conversationId: string = this.conversationId) : AsyncGenerator<Activity> {
179+
const localConversationId = activity.conversation?.id ?? conversationId
180+
const uriExecute = getCopilotStudioConnectionUrl(this.settings, localConversationId)
181+
const qbody: ExecuteTurnRequest = new ExecuteTurnRequest(activity)
182+
183+
logger.info('Sending activity...', activity)
184+
yield * this.postRequestAsync(uriExecute, qbody, 'POST')
185+
}
186+
187+
/**
188+
* Starts a new conversation with the Copilot Studio service.
189+
* @param emitStartConversationEvent Whether to emit a start conversation event. Defaults to true.
190+
* @returns A promise yielding an array of activities.
191+
* @deprecated Use startConversationStreaming instead.
192+
*/
193+
public async startConversationAsync (emitStartConversationEvent: boolean = true): Promise<Activity[]> {
194+
const result: Activity[] = []
195+
for await (const value of this.startConversationStreaming(emitStartConversationEvent)) {
196+
result.push(value)
197+
}
198+
return result
199+
}
200+
172201
/**
173202
* Sends a question to the Copilot Studio service and retrieves the response activities.
174203
* @param question The question to ask.
175204
* @param conversationId The ID of the conversation. Defaults to the current conversation ID.
176-
* @returns An async generator yielding the Agent's Activities.
205+
* @returns A promise yielding an array of activities.
206+
* @deprecated Use sendActivityStreaming instead.
177207
*/
178-
public async * askQuestionAsync (question: string, conversationId: string = this.conversationId) : AsyncGenerator<Activity> {
208+
public async askQuestionAsync (question: string, conversationId?: string) : Promise<Activity[]> {
209+
const localConversationId = conversationId?.trim() ? conversationId : this.conversationId
179210
const conversationAccount: ConversationAccount = {
180-
id: conversationId
211+
id: localConversationId
181212
}
182213
const activityObj = {
183214
type: 'message',
@@ -186,21 +217,25 @@ export class CopilotStudioClient {
186217
}
187218
const activity = Activity.fromObject(activityObj)
188219

189-
yield * this.sendActivity(activity)
220+
const result: Activity[] = []
221+
for await (const value of this.sendActivityStreaming(activity, conversationId)) {
222+
result.push(value)
223+
}
224+
return result
190225
}
191226

192227
/**
193228
* Sends an activity to the Copilot Studio service and retrieves the response activities.
194229
* @param activity The activity to send.
195230
* @param conversationId The ID of the conversation. Defaults to the current conversation ID.
196-
* @returns An async generator yielding the Agent's Activities.
231+
* @returns A promise yielding an array of activities.
232+
* @deprecated Use sendActivityStreaming instead.
197233
*/
198-
public async * sendActivity (activity: Activity, conversationId: string = this.conversationId) : AsyncGenerator<Activity> {
199-
const localConversationId = activity.conversation?.id ?? conversationId
200-
const uriExecute = getCopilotStudioConnectionUrl(this.settings, localConversationId)
201-
const qbody: ExecuteTurnRequest = new ExecuteTurnRequest(activity)
202-
203-
logger.info('Sending activity...', activity)
204-
yield * this.postRequestAsync(uriExecute, qbody, 'POST')
234+
public async sendActivity (activity: Activity, conversationId: string = this.conversationId) : Promise<Activity[]> {
235+
const result: Activity[] = []
236+
for await (const value of this.sendActivityStreaming(activity, conversationId)) {
237+
result.push(value)
238+
}
239+
return result
205240
}
206241
}

packages/agents-copilotstudio-client/src/copilotStudioWebChat.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -219,7 +219,7 @@ export class CopilotStudioWebChat {
219219
logger.debug('--> Connection established.')
220220
notifyTyping()
221221

222-
for await (const activity of client.startConversationAsync()) {
222+
for await (const activity of client.startConversationStreaming()) {
223223
delete activity.replyToId
224224
conversation = activity.conversation
225225
notifyActivity(activity)
@@ -281,7 +281,7 @@ export class CopilotStudioWebChat {
281281
subscriber.next(newActivity.id!)
282282

283283
// Stream the agent's response, but don't block the UI
284-
for await (const responseActivity of client.sendActivity(newActivity)) {
284+
for await (const responseActivity of client.sendActivityStreaming(newActivity)) {
285285
notifyActivity(responseActivity)
286286
logger.info('<-- Activity received correctly from Copilot Studio.')
287287
}

0 commit comments

Comments
 (0)