UNPKG

@microsoft/agents-copilotstudio-client

Version:

Microsoft Copilot Studio Client for JavaScript. Copilot Studio Client.

143 lines (142 loc) 7.17 kB
/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import { ConnectionSettings } from './connectionSettings'; import { Activity } from '@microsoft/agents-activity'; import { StartRequest } from './startRequest'; import { StartResponse, ExecuteTurnResponse } from './responses'; import { SubscribeEvent } from './subscribeEvent'; /** * Client for interacting with Microsoft Copilot Studio services. * Provides functionality to start conversations and send messages to Copilot Studio bots. */ export declare class CopilotStudioClient { /** Header key for conversation ID. */ private static readonly conversationIdHeaderKey; /** Island Header key */ private static readonly islandExperimentalUrlHeaderKey; /** The ID of the current conversation. */ private conversationId; /** The connection settings for the client. */ private readonly settings; /** The authenticaton token. */ private readonly token; /** * Returns the scope URL needed to connect to Copilot Studio from the connection settings. * This is used for authentication token audience configuration. * @param settings Copilot Studio connection settings. * @returns The scope URL for token audience. * @deprecated Use ScopeHelper.getScopeFromSettings instead. */ static scopeFromSettings: (settings: ConnectionSettings) => string; /** * Creates an instance of CopilotStudioClient. * @param settings The connection settings. * @param token The authentication token. */ constructor(settings: ConnectionSettings, token: string); /** * Logs a diagnostic message if diagnostics are enabled. * @param message The message to log. * @param args Additional arguments to log. */ private logDiagnostic; /** * Streams activities from the Copilot Studio service using eventsource-client. * @param url The connection URL for Copilot Studio. * @param body Optional. The request body (for POST). * @param method Optional. The HTTP method (default: POST). * @returns An async generator yielding the Agent's Activities. */ private postRequestAsync; private processResponseHeaders; /** * Starts a new conversation with the Copilot Studio service using a StartRequest. * @param request The request parameters for starting the conversation. * @returns An async generator yielding the Agent's Activities. */ startConversationStreaming(request: StartRequest): AsyncGenerator<Activity>; /** * Starts a new conversation with the Copilot Studio service. * @param emitStartConversationEvent Whether to emit a start conversation event. Defaults to true. * @returns An async generator yielding the Agent's Activities. */ startConversationStreaming(emitStartConversationEvent?: boolean): AsyncGenerator<Activity>; /** * Sends an activity to the Copilot Studio service and retrieves the response activities. * @param activity The activity to send. * @param conversationId The ID of the conversation. Defaults to the current conversation ID. * @returns An async generator yielding the Agent's Activities. */ sendActivityStreaming(activity: Activity, conversationId?: string): AsyncGenerator<Activity>; /** * Executes a turn in an existing conversation by sending an activity. * This method provides explicit control over the conversation ID. * @param activity The activity to send. * @param conversationId The ID of the conversation. Required. * @returns An async generator yielding the Agent's Activities. * @throws Error if conversationId is not provided. */ executeStreaming(activity: Activity, conversationId: string): AsyncGenerator<Activity>; /** * Executes a turn in an existing conversation by sending an activity. * @param activity The activity to send. * @param conversationId The ID of the conversation. Required. * @returns A promise yielding an array of activities. * @throws Error if conversationId is not provided. * @deprecated Use executeStreaming instead. */ execute(activity: Activity, conversationId: string): Promise<Activity[]>; /** * Starts a new conversation with the Copilot Studio service using a StartRequest. * @param request The request parameters for starting the conversation. * @returns A promise yielding an array of activities. * @deprecated Use startConversationStreaming instead. */ startConversationAsync(request: StartRequest): Promise<Activity[]>; /** * Starts a new conversation with the Copilot Studio service. * @param emitStartConversationEvent Whether to emit a start conversation event. Defaults to true. * @returns A promise yielding an array of activities. * @deprecated Use startConversationStreaming instead. */ startConversationAsync(emitStartConversationEvent?: boolean): Promise<Activity[]>; /** * Sends a question to the Copilot Studio service and retrieves the response activities. * @param question The question to ask. * @param conversationId The ID of the conversation. Defaults to the current conversation ID. * @returns A promise yielding an array of activities. * @deprecated Use sendActivityStreaming instead. */ askQuestionAsync(question: string, conversationId?: string): Promise<Activity[]>; /** * Sends an activity to the Copilot Studio service and retrieves the response activities. * @param activity The activity to send. * @param conversationId The ID of the conversation. Defaults to the current conversation ID. * @returns A promise yielding an array of activities. * @deprecated Use sendActivityStreaming instead. */ sendActivity(activity: Activity, conversationId?: string): Promise<Activity[]>; /** * Starts a new conversation and returns a typed response. * @param request The request parameters for starting the conversation. * @returns A promise yielding a StartResponse with activities and conversation metadata. */ startConversationWithResponse(request?: StartRequest | boolean): Promise<StartResponse>; /** * Executes a turn and returns a typed response. * @param activity The activity to send. * @param conversationId The conversation ID. * @returns A promise yielding an ExecuteTurnResponse with activities and metadata. */ executeWithResponse(activity: Activity, conversationId: string): Promise<ExecuteTurnResponse>; /** * Subscribes to a conversation to receive events via Server-Sent Events (SSE). * This method allows resumption from a specific event ID. * @param conversationId The ID of the conversation to subscribe to. * @param lastReceivedEventId Optional. The last received event ID for resumption. * @returns An async generator yielding SubscribeEvent objects containing activities and event IDs. */ subscribeAsync(conversationId: string, lastReceivedEventId?: string): AsyncGenerator<SubscribeEvent>; }