UNPKG

@microsoft/agents-hosting

Version:

Microsoft 365 Agents SDK for JavaScript

132 lines (131 loc) 5.78 kB
/** * Copyright (c) Microsoft Corporation. All rights reserved. * Licensed under the MIT License. */ import { AuthConfiguration } from './auth/authConfiguration'; import { AuthProvider } from './auth/authProvider'; import { Middleware, MiddlewareHandler, MiddlewareSet } from './middlewareSet'; import { TurnContext } from './turnContext'; import { Activity, ConversationReference } from '@microsoft/agents-activity'; import { ResourceResponse } from './connector-client/resourceResponse'; import { AttachmentData } from './connector-client/attachmentData'; import { AttachmentInfo } from './connector-client/attachmentInfo'; import { UserTokenClient } from './oauth'; /** * Abstract base class for all adapters in the Agents framework. * * @remarks * This class provides core functionality for handling conversations, managing middleware, * authentication, and error handling. Adapters are responsible for translating between * the Agents framework and specific communication channels (like Teams, Web Chat, etc.). * * Key features: * - Middleware pipeline for processing incoming and outgoing activities * - Error handling and recovery mechanisms * - Authentication provider integration * - Abstract methods for channel-specific operations * - Context management with revocable proxies for security */ export declare abstract class BaseAdapter { /** * The middleware set used to process the pipeline of middleware handlers. */ protected middleware: MiddlewareSet; private turnError; /** * Symbol key used to store agent identity information in the TurnContext. */ readonly AgentIdentityKey: symbol; /** * Symbol key used to store connector client instances in the TurnContext. */ readonly ConnectorClientKey: symbol; /** * Symbol key used to store OAuth scope information in the TurnContext. */ readonly OAuthScopeKey: symbol; /** * The authentication provider used for token management. */ authProvider: AuthProvider; /** * The user token client used for managing user tokens. */ userTokenClient: UserTokenClient | null; /** * The authentication configuration for the adapter. */ abstract authConfig: AuthConfiguration; /** * Sends a set of activities to the conversation. * @param context - The TurnContext for the current turn. * @param activities - The activities to send. * @returns A promise representing the array of ResourceResponses for the sent activities. */ abstract sendActivities(context: TurnContext, activities: Activity[]): Promise<ResourceResponse[]>; /** * Updates an existing activity. * @param context - The TurnContext for the current turn. * @param activity - The activity to update. * @returns A promise representing the ResourceResponse for the updated activity. */ abstract updateActivity(context: TurnContext, activity: Activity): Promise<ResourceResponse | void>; /** * Deletes an existing activity. * @param context - The TurnContext for the current turn. * @param reference - The conversation reference of the activity to delete. * @returns A promise representing the completion of the delete operation. */ abstract deleteActivity(context: TurnContext, reference: Partial<ConversationReference>): Promise<void>; /** * Continues a conversation. * @param reference - The conversation reference to continue. * @param logic - The logic to execute. * @returns A promise representing the completion of the continue operation. */ abstract continueConversation(reference: Partial<ConversationReference>, logic: (revocableContext: TurnContext) => Promise<void>): Promise<void>; /** * Uploads an attachment. * @param conversationId - The conversation ID. * @param attachmentData - The attachment data. * @returns A promise representing the ResourceResponse for the uploaded attachment. */ abstract uploadAttachment(conversationId: string, attachmentData: AttachmentData): Promise<ResourceResponse>; /** * Gets attachment information. * @param attachmentId - The attachment ID. * @returns A promise representing the AttachmentInfo for the requested attachment. */ abstract getAttachmentInfo(attachmentId: string): Promise<AttachmentInfo>; /** * Gets an attachment. * @param attachmentId - The attachment ID. * @param viewId - The view ID. * @returns A promise representing the NodeJS.ReadableStream for the requested attachment. */ abstract getAttachment(attachmentId: string, viewId: string): Promise<NodeJS.ReadableStream>; /** * Gets the error handler for the adapter. * @returns The current error handler function. */ get onTurnError(): (context: TurnContext, error: Error) => Promise<void>; /** * Sets the error handler for the adapter. * @param value - The error handler function to set. */ set onTurnError(value: (context: TurnContext, error: Error) => Promise<void>); /** * Adds middleware to the adapter's middleware pipeline. * @param middlewares - The middleware to add. * @returns The adapter instance. */ use(...middlewares: Array<MiddlewareHandler | Middleware>): this; private makeRevocable; /** * Runs the middleware pipeline in sequence. * @param context - The TurnContext for the current turn. * @param next - The next function to call in the pipeline. * @returns A promise representing the completion of the middleware pipeline. */ protected runMiddleware(context: TurnContext, next: (revocableContext: TurnContext) => Promise<void>): Promise<void>; }