@microsoft/agents-hosting 0.2.10-g3ac88ff25e → 0.3.5
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.
- package/README.md +17 -1
- package/dist/src/activityHandler.d.ts +304 -46
- package/dist/src/activityHandler.js +298 -45
- package/dist/src/activityHandler.js.map +1 -1
- package/dist/src/agent-client/agentClient.d.ts +50 -0
- package/dist/src/agent-client/agentClient.js +28 -0
- package/dist/src/agent-client/agentClient.js.map +1 -1
- package/dist/src/app/agentApplication.d.ts +272 -7
- package/dist/src/app/agentApplication.js +278 -12
- package/dist/src/app/agentApplication.js.map +1 -1
- package/dist/src/app/agentApplicationBuilder.d.ts +34 -3
- package/dist/src/app/agentApplicationBuilder.js +38 -6
- package/dist/src/app/agentApplicationBuilder.js.map +1 -1
- package/dist/src/app/agentApplicationOptions.d.ts +26 -2
- package/dist/src/app/appMemory.d.ts +34 -0
- package/dist/src/app/{memory.js → appMemory.js} +1 -1
- package/dist/src/app/appMemory.js.map +1 -0
- package/dist/src/app/appRoute.d.ts +6 -0
- package/dist/src/app/attachmentDownloader.d.ts +18 -0
- package/dist/src/app/attachmentDownloader.js +18 -0
- package/dist/src/app/attachmentDownloader.js.map +1 -1
- package/dist/src/app/conversationUpdateEvents.d.ts +6 -0
- package/dist/src/app/index.d.ts +4 -1
- package/dist/src/app/index.js +4 -1
- package/dist/src/app/index.js.map +1 -1
- package/dist/src/app/inputFileDownloader.d.ts +22 -0
- package/dist/src/app/oauth/authorization.d.ts +87 -0
- package/dist/src/app/oauth/authorization.js +135 -0
- package/dist/src/app/oauth/authorization.js.map +1 -0
- package/dist/src/app/routeHandler.d.ts +8 -0
- package/dist/src/app/routeSelector.d.ts +9 -0
- package/dist/src/app/turnEvents.d.ts +6 -0
- package/dist/src/app/turnState.d.ts +152 -2
- package/dist/src/app/turnState.js +125 -0
- package/dist/src/app/turnState.js.map +1 -1
- package/dist/src/app/turnStateEntry.d.ts +32 -0
- package/dist/src/app/turnStateEntry.js +32 -0
- package/dist/src/app/turnStateEntry.js.map +1 -1
- package/dist/src/auth/authConfiguration.d.ts +24 -0
- package/dist/src/auth/authConfiguration.js.map +1 -1
- package/dist/src/auth/request.d.ts +12 -0
- package/dist/src/baseAdapter.d.ts +17 -0
- package/dist/src/baseAdapter.js +17 -0
- package/dist/src/baseAdapter.js.map +1 -1
- package/dist/src/cards/cardFactory.d.ts +3 -0
- package/dist/src/cards/cardFactory.js +3 -0
- package/dist/src/cards/cardFactory.js.map +1 -1
- package/dist/src/cards/index.d.ts +1 -0
- package/dist/src/cards/index.js +1 -0
- package/dist/src/cards/index.js.map +1 -1
- package/dist/src/cards/o365ConnectorCardActionBase.d.ts +8 -0
- package/dist/src/cloudAdapter.d.ts +25 -3
- package/dist/src/cloudAdapter.js +25 -3
- package/dist/src/cloudAdapter.js.map +1 -1
- package/dist/src/getProductInfo.d.ts +6 -0
- package/dist/src/getProductInfo.js +6 -0
- package/dist/src/getProductInfo.js.map +1 -1
- package/dist/src/logger.d.ts +34 -2
- package/dist/src/logger.js +35 -0
- package/dist/src/logger.js.map +1 -1
- package/dist/src/oauth/oAuthFlow.d.ts +32 -3
- package/dist/src/oauth/oAuthFlow.js +38 -14
- package/dist/src/oauth/oAuthFlow.js.map +1 -1
- package/dist/src/oauth/userTokenClient.d.ts +2 -2
- package/dist/src/oauth/userTokenClient.js +3 -3
- package/dist/src/oauth/userTokenClient.js.map +1 -1
- package/dist/src/state/agentState.d.ts +79 -27
- package/dist/src/state/agentState.js +58 -27
- package/dist/src/state/agentState.js.map +1 -1
- package/dist/src/state/agentStatePropertyAccesor.d.ts +67 -11
- package/dist/src/state/agentStatePropertyAccesor.js +58 -11
- package/dist/src/state/agentStatePropertyAccesor.js.map +1 -1
- package/dist/src/statusCodes.d.ts +39 -0
- package/dist/src/statusCodes.js +39 -0
- package/dist/src/statusCodes.js.map +1 -1
- package/dist/src/storage/memoryStorage.d.ts +48 -14
- package/dist/src/storage/memoryStorage.js +48 -14
- package/dist/src/storage/memoryStorage.js.map +1 -1
- package/dist/src/storage/storage.d.ts +43 -13
- package/dist/src/tokenResponseEventName.d.ts +3 -0
- package/dist/src/tokenResponseEventName.js +3 -0
- package/dist/src/tokenResponseEventName.js.map +1 -1
- package/dist/src/turnContext.d.ts +142 -56
- package/dist/src/turnContext.js +123 -53
- package/dist/src/turnContext.js.map +1 -1
- package/package.json +6 -6
- package/src/activityHandler.ts +304 -46
- package/src/agent-client/agentClient.ts +55 -5
- package/src/app/agentApplication.ts +283 -15
- package/src/app/agentApplicationBuilder.ts +39 -7
- package/src/app/agentApplicationOptions.ts +33 -2
- package/src/app/appMemory.ts +38 -0
- package/src/app/appRoute.ts +7 -0
- package/src/app/attachmentDownloader.ts +18 -0
- package/src/app/conversationUpdateEvents.ts +6 -0
- package/src/app/index.ts +4 -1
- package/src/app/inputFileDownloader.ts +24 -0
- package/src/app/oauth/authorization.ts +162 -0
- package/src/app/routeHandler.ts +8 -0
- package/src/app/routeSelector.ts +9 -0
- package/src/app/turnEvents.ts +6 -0
- package/src/app/turnState.ts +153 -3
- package/src/app/turnStateEntry.ts +32 -0
- package/src/auth/authConfiguration.ts +32 -1
- package/src/auth/request.ts +15 -0
- package/src/baseAdapter.ts +18 -0
- package/src/cards/cardFactory.ts +3 -0
- package/src/cards/index.ts +1 -0
- package/src/cards/o365ConnectorCardActionBase.ts +8 -0
- package/src/cloudAdapter.ts +28 -3
- package/src/getProductInfo.ts +7 -0
- package/src/logger.ts +34 -1
- package/src/oauth/oAuthFlow.ts +59 -18
- package/src/oauth/userTokenClient.ts +4 -4
- package/src/state/agentState.ts +81 -29
- package/src/state/agentStatePropertyAccesor.ts +67 -11
- package/src/statusCodes.ts +51 -0
- package/src/storage/memoryStorage.ts +48 -14
- package/src/storage/storage.ts +51 -18
- package/src/tokenResponseEventName.ts +3 -0
- package/src/turnContext.ts +142 -56
- package/dist/src/app/memory.d.ts +0 -10
- package/dist/src/app/memory.js.map +0 -1
- package/dist/src/app/oauth/userIdentity.d.ts +0 -43
- package/dist/src/app/oauth/userIdentity.js +0 -54
- package/dist/src/app/oauth/userIdentity.js.map +0 -1
- package/src/app/memory.ts +0 -14
- package/src/app/oauth/userIdentity.ts +0 -78
|
@@ -6,12 +6,36 @@
|
|
|
6
6
|
import { TurnContext } from '../turnContext'
|
|
7
7
|
import { TurnState } from './turnState'
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* Represents a file input with its content, type, and optional URL.
|
|
11
|
+
*/
|
|
9
12
|
export interface InputFile {
|
|
13
|
+
/**
|
|
14
|
+
* The content of the file as a Buffer.
|
|
15
|
+
*/
|
|
10
16
|
content: Buffer;
|
|
17
|
+
|
|
18
|
+
/**
|
|
19
|
+
* The MIME type of the file content.
|
|
20
|
+
*/
|
|
11
21
|
contentType: string;
|
|
22
|
+
|
|
23
|
+
/**
|
|
24
|
+
* An optional URL pointing to the file content.
|
|
25
|
+
*/
|
|
12
26
|
contentUrl?: string;
|
|
13
27
|
}
|
|
14
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Interface for downloading input files in a specific turn context and state.
|
|
31
|
+
*/
|
|
15
32
|
export interface InputFileDownloader<TState extends TurnState = TurnState> {
|
|
33
|
+
/**
|
|
34
|
+
* Downloads files based on the provided turn context and state.
|
|
35
|
+
*
|
|
36
|
+
* @param context - The turn context for the current operation.
|
|
37
|
+
* @param state - The state associated with the current turn.
|
|
38
|
+
* @returns A promise that resolves to an array of input files.
|
|
39
|
+
*/
|
|
16
40
|
downloadFiles(context: TurnContext, state: TState): Promise<InputFile[]>;
|
|
17
41
|
}
|
|
@@ -0,0 +1,162 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Copyright (c) Microsoft Corporation. All rights reserved.
|
|
3
|
+
* Licensed under the MIT License.
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
import { TurnContext } from '../../turnContext'
|
|
7
|
+
import { debug } from '../../logger'
|
|
8
|
+
import { TurnState } from '../turnState'
|
|
9
|
+
import { Storage } from '../../storage'
|
|
10
|
+
import { OAuthFlow, TokenRequestStatus, TokenResponse } from '../../oauth'
|
|
11
|
+
import { UserState } from '../../state'
|
|
12
|
+
|
|
13
|
+
const logger = debug('agents:authorization')
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* Interface defining an authorization handler for OAuth flows
|
|
17
|
+
* @interface AuthHandler
|
|
18
|
+
*/
|
|
19
|
+
export interface AuthHandler {
|
|
20
|
+
/** Connection name for the auth provider */
|
|
21
|
+
name?: string,
|
|
22
|
+
/** Whether authorization should be triggered automatically */
|
|
23
|
+
auto?: boolean,
|
|
24
|
+
/** The OAuth flow implementation */
|
|
25
|
+
flow?: OAuthFlow,
|
|
26
|
+
/** Title to display on auth cards/UI */
|
|
27
|
+
title?: string,
|
|
28
|
+
/** Text to display on auth cards/UI */
|
|
29
|
+
text?: string,
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
/**
|
|
33
|
+
* Options for configuring user authorization.
|
|
34
|
+
* Contains settings to configure OAuth connections.
|
|
35
|
+
*/
|
|
36
|
+
export interface AuthorizationHandlers extends Record<string, AuthHandler> {}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* Class responsible for managing authorization and OAuth flows
|
|
40
|
+
* @class Authorization
|
|
41
|
+
*/
|
|
42
|
+
export class Authorization {
|
|
43
|
+
_authHandlers: AuthorizationHandlers
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* Creates a new instance of UserAuthorization.
|
|
47
|
+
* @param {Storage} storage - The storage system to use for state management.
|
|
48
|
+
* @param {AuthorizationHandlers} authHandlers - Configuration for OAuth providers
|
|
49
|
+
* @throws {Error} If storage is null/undefined or no auth handlers are provided
|
|
50
|
+
*/
|
|
51
|
+
constructor (storage: Storage, authHandlers: AuthorizationHandlers) {
|
|
52
|
+
if (storage === undefined || storage === null) {
|
|
53
|
+
throw new Error('Storage is required for UserAuthorization')
|
|
54
|
+
}
|
|
55
|
+
const userState = new UserState(storage)
|
|
56
|
+
if (authHandlers === undefined || Object.keys(authHandlers).length === 0) {
|
|
57
|
+
throw new Error('The authorization does not have any auth handlers')
|
|
58
|
+
}
|
|
59
|
+
this._authHandlers = authHandlers
|
|
60
|
+
for (const ah in this._authHandlers) {
|
|
61
|
+
if (this._authHandlers![ah].name === undefined && process.env[ah + '_connectionName'] === undefined) {
|
|
62
|
+
throw new Error(`AuthHandler name ${ah}_connectionName not set in autorization and not found in env vars.`)
|
|
63
|
+
}
|
|
64
|
+
const currentAuthHandler = this._authHandlers![ah]
|
|
65
|
+
currentAuthHandler.name = currentAuthHandler.name ?? process.env[ah + '_connectionName'] as string
|
|
66
|
+
currentAuthHandler.title = currentAuthHandler.title ?? process.env[ah + '_connectionTitle'] as string
|
|
67
|
+
currentAuthHandler.text = currentAuthHandler.text ?? process.env[ah + '_connectionText'] as string
|
|
68
|
+
currentAuthHandler.auto = currentAuthHandler.auto ?? process.env[ah + '_connectionAuto'] === 'true'
|
|
69
|
+
currentAuthHandler.flow = new OAuthFlow(userState, currentAuthHandler.name, null!, currentAuthHandler.title, currentAuthHandler.text)
|
|
70
|
+
}
|
|
71
|
+
logger.info('Authorization handlers configured with', this._authHandlers.length, 'handlers')
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
/**
|
|
75
|
+
* Gets the token for a specific auth handler
|
|
76
|
+
* @param {TurnContext} context - The context object for the current turn
|
|
77
|
+
* @param {string} [authHandlerId] - Optional ID of the auth handler to use, defaults to first handler
|
|
78
|
+
* @returns {Promise<TokenResponse>} The token response from the OAuth provider
|
|
79
|
+
*/
|
|
80
|
+
public async getToken (context: TurnContext, authHandlerId?: string): Promise<TokenResponse> {
|
|
81
|
+
logger.info('getToken from user token service for authHandlerId:', authHandlerId)
|
|
82
|
+
const authHandler = this.resolverHandler(authHandlerId)
|
|
83
|
+
return await authHandler.flow?.getUserToken(context)!
|
|
84
|
+
}
|
|
85
|
+
|
|
86
|
+
/**
|
|
87
|
+
* Begins or continues an OAuth flow
|
|
88
|
+
* @param {TurnContext} context - The context object for the current turn
|
|
89
|
+
* @param {TurnState} state - The state object for the current turn
|
|
90
|
+
* @param {string} [authHandlerId] - Optional ID of the auth handler to use, defaults to first handler
|
|
91
|
+
* @returns {Promise<TokenResponse>} The token response from the OAuth provider
|
|
92
|
+
*/
|
|
93
|
+
public async beginOrContinueFlow (context: TurnContext, state: TurnState, authHandlerId?: string) : Promise<TokenResponse> {
|
|
94
|
+
logger.info('beginOrContinueFlow for authHandlerId:', authHandlerId)
|
|
95
|
+
const flow = this.resolverHandler(authHandlerId).flow!
|
|
96
|
+
let tokenResponse: TokenResponse
|
|
97
|
+
if (flow.state!.flowStarted === false) {
|
|
98
|
+
tokenResponse = await flow.beginFlow(context)
|
|
99
|
+
} else {
|
|
100
|
+
tokenResponse = await flow.continueFlow(context)
|
|
101
|
+
if (tokenResponse.status === TokenRequestStatus.Success) {
|
|
102
|
+
if (this._signInHandler) {
|
|
103
|
+
await this._signInHandler(context, state, authHandlerId)
|
|
104
|
+
}
|
|
105
|
+
}
|
|
106
|
+
}
|
|
107
|
+
return tokenResponse
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
/**
|
|
111
|
+
* Gets the current state of the OAuth flow
|
|
112
|
+
* @param {string} [authHandlerId] - Optional ID of the auth handler to check, defaults to first handler
|
|
113
|
+
* @returns {boolean} Whether the flow has started
|
|
114
|
+
*/
|
|
115
|
+
public getFlowState (authHandlerId?: string) : boolean {
|
|
116
|
+
const flow = this.resolverHandler(authHandlerId).flow!
|
|
117
|
+
return flow.state?.flowStarted!
|
|
118
|
+
}
|
|
119
|
+
|
|
120
|
+
/**
|
|
121
|
+
* Resolves the auth handler to use based on the provided ID
|
|
122
|
+
* @param {string} [authHandlerId] - Optional ID of the auth handler to resolve, defaults to first handler
|
|
123
|
+
* @returns {AuthHandler} The resolved auth handler
|
|
124
|
+
*/
|
|
125
|
+
resolverHandler = (authHandlerId?: string) : AuthHandler => {
|
|
126
|
+
if (authHandlerId) {
|
|
127
|
+
return this._authHandlers![authHandlerId]
|
|
128
|
+
}
|
|
129
|
+
return this._authHandlers![Object.keys(this._authHandlers)[0]]
|
|
130
|
+
}
|
|
131
|
+
|
|
132
|
+
/**
|
|
133
|
+
* Signs out the current user.
|
|
134
|
+
* This method clears the user's token and resets the SSO state.
|
|
135
|
+
*
|
|
136
|
+
* @param {TurnContext} context - The context object for the current turn.
|
|
137
|
+
* @param {TurnState} state - The state object for the current turn.
|
|
138
|
+
* @param {string} [authHandlerId] - Optional ID of the auth handler to use for sign out
|
|
139
|
+
* @returns {Promise<void>}
|
|
140
|
+
*/
|
|
141
|
+
async signOut (context: TurnContext, state: TurnState, authHandlerId?: string) : Promise<void> {
|
|
142
|
+
logger.info('signOut for authHandlerId:', authHandlerId)
|
|
143
|
+
if (authHandlerId === undefined) { // aw
|
|
144
|
+
for (const ah in this._authHandlers) {
|
|
145
|
+
const flow = this._authHandlers[ah].flow
|
|
146
|
+
await flow?.signOut(context)
|
|
147
|
+
}
|
|
148
|
+
} else {
|
|
149
|
+
await this.resolverHandler(authHandlerId).flow?.signOut(context)
|
|
150
|
+
}
|
|
151
|
+
}
|
|
152
|
+
|
|
153
|
+
_signInHandler: ((context: TurnContext, state: TurnState, authHandlerId?: string) => void) | null = null
|
|
154
|
+
|
|
155
|
+
/**
|
|
156
|
+
* Sets a handler to be called when sign-in is successfully completed
|
|
157
|
+
* @param {Function} handler - The handler function to call on successful sign-in
|
|
158
|
+
*/
|
|
159
|
+
public onSignInSuccess (handler: (context: TurnContext, state: TurnState, authHandlerId?: string) => void) {
|
|
160
|
+
this._signInHandler = handler
|
|
161
|
+
}
|
|
162
|
+
}
|
package/src/app/routeHandler.ts
CHANGED
|
@@ -6,4 +6,12 @@
|
|
|
6
6
|
import { TurnContext } from '../turnContext'
|
|
7
7
|
import { TurnState } from './turnState'
|
|
8
8
|
|
|
9
|
+
/**
|
|
10
|
+
* A handler function for routing operations in a specific turn context and state.
|
|
11
|
+
*
|
|
12
|
+
* @typeParam TState - The type of the turn state.
|
|
13
|
+
* @param context - The turn context for the current operation.
|
|
14
|
+
* @param state - The state associated with the current turn.
|
|
15
|
+
* @returns A promise that resolves when the routing operation is complete.
|
|
16
|
+
*/
|
|
9
17
|
export type RouteHandler<TState extends TurnState> = (context: TurnContext, state: TState) => Promise<void>
|
package/src/app/routeSelector.ts
CHANGED
|
@@ -5,6 +5,15 @@
|
|
|
5
5
|
|
|
6
6
|
import { TurnContext } from '../turnContext'
|
|
7
7
|
|
|
8
|
+
/**
|
|
9
|
+
* A function that determines whether a specific condition is met in the given turn context.
|
|
10
|
+
*
|
|
11
|
+
* @param context - The turn context for the current operation.
|
|
12
|
+
* @returns A promise that resolves to a boolean indicating whether the condition is met.
|
|
13
|
+
*/
|
|
8
14
|
export type Selector = (context: TurnContext) => Promise<boolean>
|
|
9
15
|
|
|
16
|
+
/**
|
|
17
|
+
* A specialized selector for routing operations.
|
|
18
|
+
*/
|
|
10
19
|
export type RouteSelector = Selector
|
package/src/app/turnEvents.ts
CHANGED
|
@@ -3,4 +3,10 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents the types of events that can occur during a turn in the application.
|
|
8
|
+
*
|
|
9
|
+
* - `beforeTurn`: Triggered before the turn starts.
|
|
10
|
+
* - `afterTurn`: Triggered after the turn ends.
|
|
11
|
+
*/
|
|
6
12
|
export type TurnEvents = 'beforeTurn' | 'afterTurn'
|
package/src/app/turnState.ts
CHANGED
|
@@ -4,7 +4,7 @@
|
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
6
|
import { Storage, StoreItems } from '../storage'
|
|
7
|
-
import {
|
|
7
|
+
import { AppMemory } from './appMemory'
|
|
8
8
|
import { InputFile } from './inputFileDownloader'
|
|
9
9
|
import { TurnStateEntry } from './turnStateEntry'
|
|
10
10
|
import { TurnContext } from '../turnContext'
|
|
@@ -20,21 +20,47 @@ const TEMP_SCOPE = 'temp'
|
|
|
20
20
|
|
|
21
21
|
const SSO_SCOPE = 'sso'
|
|
22
22
|
|
|
23
|
+
/**
|
|
24
|
+
* Default interface for conversation state.
|
|
25
|
+
* Extend this interface to define custom conversation state properties.
|
|
26
|
+
*/
|
|
23
27
|
export interface DefaultConversationState {}
|
|
24
28
|
|
|
29
|
+
/**
|
|
30
|
+
* Default interface for user state.
|
|
31
|
+
* Extend this interface to define custom user state properties.
|
|
32
|
+
*/
|
|
25
33
|
export interface DefaultUserState {}
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* Default interface for temporary state that persists only during the current turn.
|
|
37
|
+
* Contains properties used for handling user input, file attachments, and OAuth flows.
|
|
38
|
+
*/
|
|
26
39
|
export interface DefaultTempState {
|
|
40
|
+
/** Current user input text */
|
|
27
41
|
input: string;
|
|
42
|
+
/** Collection of files attached to the current message */
|
|
28
43
|
inputFiles: InputFile[];
|
|
44
|
+
/** Last output text generated by the agent */
|
|
29
45
|
lastOutput: string;
|
|
46
|
+
/** Results from actions performed by the agent */
|
|
30
47
|
actionOutputs: Record<string, string>;
|
|
48
|
+
/** OAuth tokens keyed by provider ID */
|
|
31
49
|
authTokens: { [key: string]: string };
|
|
50
|
+
/** Flag to prevent duplicate token exchanges */
|
|
32
51
|
duplicateTokenExchange?: boolean;
|
|
33
52
|
}
|
|
34
53
|
|
|
54
|
+
/**
|
|
55
|
+
* Default interface for Single Sign-On (SSO) state.
|
|
56
|
+
* Contains properties used for managing OAuth authentication flows.
|
|
57
|
+
*/
|
|
35
58
|
export interface DefaultSSOState {
|
|
59
|
+
/** Whether an OAuth flow has been started */
|
|
36
60
|
flowStarted: boolean;
|
|
61
|
+
/** The user's access token */
|
|
37
62
|
userToken: string;
|
|
63
|
+
/** Expiration timestamp for the current flow */
|
|
38
64
|
flowExpires: number;
|
|
39
65
|
}
|
|
40
66
|
|
|
@@ -67,19 +93,28 @@ export interface DefaultSSOState {
|
|
|
67
93
|
* }
|
|
68
94
|
* }
|
|
69
95
|
* ```
|
|
96
|
+
* @template TConversationState - Type for conversation-scoped state
|
|
97
|
+
* @template TUserState - Type for user-scoped state
|
|
98
|
+
* @template TTempState - Type for temporary state that exists only for the current turn
|
|
99
|
+
* @template TSSOState - Type for Single Sign-On (SSO) state
|
|
70
100
|
*/
|
|
71
|
-
|
|
72
101
|
export class TurnState<
|
|
73
102
|
TConversationState = DefaultConversationState,
|
|
74
103
|
TUserState = DefaultUserState,
|
|
75
104
|
TTempState = DefaultTempState,
|
|
76
105
|
TSSOState = DefaultSSOState
|
|
77
|
-
> implements
|
|
106
|
+
> implements AppMemory {
|
|
78
107
|
private _scopes: Record<string, TurnStateEntry> = {}
|
|
79
108
|
private _isLoaded = false
|
|
80
109
|
private _loadingPromise?: Promise<boolean>
|
|
81
110
|
private _stateNotLoadedString = 'TurnState hasn\'t been loaded. Call load() first.'
|
|
82
111
|
|
|
112
|
+
/**
|
|
113
|
+
* Gets the conversation-scoped state.
|
|
114
|
+
* This state is shared by all users in the same conversation.
|
|
115
|
+
* @returns The conversation state object
|
|
116
|
+
* @throws Error if state hasn't been loaded
|
|
117
|
+
*/
|
|
83
118
|
public get conversation (): TConversationState {
|
|
84
119
|
const scope = this.getScope(CONVERSATION_SCOPE)
|
|
85
120
|
if (!scope) {
|
|
@@ -88,6 +123,11 @@ export class TurnState<
|
|
|
88
123
|
return scope.value as TConversationState
|
|
89
124
|
}
|
|
90
125
|
|
|
126
|
+
/**
|
|
127
|
+
* Sets the conversation-scoped state.
|
|
128
|
+
* @param value - The new conversation state object
|
|
129
|
+
* @throws Error if state hasn't been loaded
|
|
130
|
+
*/
|
|
91
131
|
public set conversation (value: TConversationState) {
|
|
92
132
|
const scope = this.getScope(CONVERSATION_SCOPE)
|
|
93
133
|
if (!scope) {
|
|
@@ -96,10 +136,20 @@ export class TurnState<
|
|
|
96
136
|
scope.replace(value as Record<string, unknown>)
|
|
97
137
|
}
|
|
98
138
|
|
|
139
|
+
/**
|
|
140
|
+
* Gets whether the state has been loaded from storage
|
|
141
|
+
* @returns True if the state has been loaded, false otherwise
|
|
142
|
+
*/
|
|
99
143
|
public get isLoaded (): boolean {
|
|
100
144
|
return this._isLoaded
|
|
101
145
|
}
|
|
102
146
|
|
|
147
|
+
/**
|
|
148
|
+
* Gets the temporary state for the current turn.
|
|
149
|
+
* This state is not persisted between turns.
|
|
150
|
+
* @returns The temporary state object
|
|
151
|
+
* @throws Error if state hasn't been loaded
|
|
152
|
+
*/
|
|
103
153
|
public get temp (): TTempState {
|
|
104
154
|
const scope = this.getScope(TEMP_SCOPE)
|
|
105
155
|
if (!scope) {
|
|
@@ -108,6 +158,11 @@ export class TurnState<
|
|
|
108
158
|
return scope.value as TTempState
|
|
109
159
|
}
|
|
110
160
|
|
|
161
|
+
/**
|
|
162
|
+
* Sets the temporary state for the current turn.
|
|
163
|
+
* @param value - The new temporary state object
|
|
164
|
+
* @throws Error if state hasn't been loaded
|
|
165
|
+
*/
|
|
111
166
|
public set temp (value: TTempState) {
|
|
112
167
|
const scope = this.getScope(TEMP_SCOPE)
|
|
113
168
|
if (!scope) {
|
|
@@ -116,6 +171,12 @@ export class TurnState<
|
|
|
116
171
|
scope.replace(value as Record<string, unknown>)
|
|
117
172
|
}
|
|
118
173
|
|
|
174
|
+
/**
|
|
175
|
+
* Gets the user-scoped state.
|
|
176
|
+
* This state is unique to each user and persists across conversations.
|
|
177
|
+
* @returns The user state object
|
|
178
|
+
* @throws Error if state hasn't been loaded
|
|
179
|
+
*/
|
|
119
180
|
public get user (): TUserState {
|
|
120
181
|
const scope = this.getScope(USER_SCOPE)
|
|
121
182
|
if (!scope) {
|
|
@@ -124,6 +185,11 @@ export class TurnState<
|
|
|
124
185
|
return scope.value as TUserState
|
|
125
186
|
}
|
|
126
187
|
|
|
188
|
+
/**
|
|
189
|
+
* Sets the user-scoped state.
|
|
190
|
+
* @param value - The new user state object
|
|
191
|
+
* @throws Error if state hasn't been loaded
|
|
192
|
+
*/
|
|
127
193
|
public set user (value: TUserState) {
|
|
128
194
|
const scope = this.getScope(USER_SCOPE)
|
|
129
195
|
if (!scope) {
|
|
@@ -132,6 +198,12 @@ export class TurnState<
|
|
|
132
198
|
scope.replace(value as Record<string, unknown>)
|
|
133
199
|
}
|
|
134
200
|
|
|
201
|
+
/**
|
|
202
|
+
* Gets the Single Sign-On (SSO) state.
|
|
203
|
+
* This state is used to manage OAuth authentication flows.
|
|
204
|
+
* @returns The SSO state object
|
|
205
|
+
* @throws Error if state hasn't been loaded
|
|
206
|
+
*/
|
|
135
207
|
public get sso (): TSSOState {
|
|
136
208
|
const scope = this.getScope(SSO_SCOPE)
|
|
137
209
|
if (!scope) {
|
|
@@ -140,6 +212,11 @@ export class TurnState<
|
|
|
140
212
|
return scope.value as TSSOState
|
|
141
213
|
}
|
|
142
214
|
|
|
215
|
+
/**
|
|
216
|
+
* Sets the Single Sign-On (SSO) state.
|
|
217
|
+
* @param value - The new SSO state object
|
|
218
|
+
* @throws Error if state hasn't been loaded
|
|
219
|
+
*/
|
|
143
220
|
public set sso (value: TSSOState) {
|
|
144
221
|
const scope = this.getScope(SSO_SCOPE)
|
|
145
222
|
if (!scope) {
|
|
@@ -148,6 +225,11 @@ export class TurnState<
|
|
|
148
225
|
scope.replace(value as Record<string, unknown>)
|
|
149
226
|
}
|
|
150
227
|
|
|
228
|
+
/**
|
|
229
|
+
* Marks the conversation state for deletion.
|
|
230
|
+
* The state will be deleted from storage on the next call to save().
|
|
231
|
+
* @throws Error if state hasn't been loaded
|
|
232
|
+
*/
|
|
151
233
|
public deleteConversationState (): void {
|
|
152
234
|
const scope = this.getScope(CONVERSATION_SCOPE)
|
|
153
235
|
if (!scope) {
|
|
@@ -156,6 +238,11 @@ export class TurnState<
|
|
|
156
238
|
scope.delete()
|
|
157
239
|
}
|
|
158
240
|
|
|
241
|
+
/**
|
|
242
|
+
* Marks the temporary state for deletion.
|
|
243
|
+
* Since temporary state is not persisted, this just clears the in-memory object.
|
|
244
|
+
* @throws Error if state hasn't been loaded
|
|
245
|
+
*/
|
|
159
246
|
public deleteTempState (): void {
|
|
160
247
|
const scope = this.getScope(TEMP_SCOPE)
|
|
161
248
|
if (!scope) {
|
|
@@ -164,6 +251,11 @@ export class TurnState<
|
|
|
164
251
|
scope.delete()
|
|
165
252
|
}
|
|
166
253
|
|
|
254
|
+
/**
|
|
255
|
+
* Marks the user state for deletion.
|
|
256
|
+
* The state will be deleted from storage on the next call to save().
|
|
257
|
+
* @throws Error if state hasn't been loaded
|
|
258
|
+
*/
|
|
167
259
|
public deleteUserState (): void {
|
|
168
260
|
const scope = this.getScope(USER_SCOPE)
|
|
169
261
|
if (!scope) {
|
|
@@ -172,10 +264,20 @@ export class TurnState<
|
|
|
172
264
|
scope.delete()
|
|
173
265
|
}
|
|
174
266
|
|
|
267
|
+
/**
|
|
268
|
+
* Gets a specific state scope by name.
|
|
269
|
+
* @param scope - The name of the scope to retrieve
|
|
270
|
+
* @returns The state entry for the scope, or undefined if not found
|
|
271
|
+
*/
|
|
175
272
|
public getScope (scope: string): TurnStateEntry | undefined {
|
|
176
273
|
return this._scopes[scope]
|
|
177
274
|
}
|
|
178
275
|
|
|
276
|
+
/**
|
|
277
|
+
* Deletes a value from state by dot-notation path.
|
|
278
|
+
* Format: "scope.property" or just "property" (defaults to temp scope)
|
|
279
|
+
* @param path - The path to the value to delete
|
|
280
|
+
*/
|
|
179
281
|
public deleteValue (path: string): void {
|
|
180
282
|
const { scope, name } = this.getScopeAndName(path)
|
|
181
283
|
if (Object.prototype.hasOwnProperty.call(scope.value, name)) {
|
|
@@ -183,21 +285,47 @@ export class TurnState<
|
|
|
183
285
|
}
|
|
184
286
|
}
|
|
185
287
|
|
|
288
|
+
/**
|
|
289
|
+
* Checks if a value exists in state by dot-notation path.
|
|
290
|
+
* Format: "scope.property" or just "property" (defaults to temp scope)
|
|
291
|
+
* @param path - The path to check
|
|
292
|
+
* @returns True if the value exists, false otherwise
|
|
293
|
+
*/
|
|
186
294
|
public hasValue (path: string): boolean {
|
|
187
295
|
const { scope, name } = this.getScopeAndName(path)
|
|
188
296
|
return Object.prototype.hasOwnProperty.call(scope.value, name)
|
|
189
297
|
}
|
|
190
298
|
|
|
299
|
+
/**
|
|
300
|
+
* Gets a value from state by dot-notation path.
|
|
301
|
+
* Format: "scope.property" or just "property" (defaults to temp scope)
|
|
302
|
+
* @template TValue - The type of the value to retrieve
|
|
303
|
+
* @param path - The path to the value
|
|
304
|
+
* @returns The value at the specified path
|
|
305
|
+
*/
|
|
191
306
|
public getValue<TValue = unknown>(path: string): TValue {
|
|
192
307
|
const { scope, name } = this.getScopeAndName(path)
|
|
193
308
|
return scope.value[name] as TValue
|
|
194
309
|
}
|
|
195
310
|
|
|
311
|
+
/**
|
|
312
|
+
* Sets a value in state by dot-notation path.
|
|
313
|
+
* Format: "scope.property" or just "property" (defaults to temp scope)
|
|
314
|
+
* @param path - The path to set
|
|
315
|
+
* @param value - The value to set
|
|
316
|
+
*/
|
|
196
317
|
public setValue (path: string, value: unknown): void {
|
|
197
318
|
const { scope, name } = this.getScopeAndName(path)
|
|
198
319
|
scope.value[name] = value
|
|
199
320
|
}
|
|
200
321
|
|
|
322
|
+
/**
|
|
323
|
+
* Loads state from storage into memory.
|
|
324
|
+
* @param context - The turn context
|
|
325
|
+
* @param storage - Optional storage provider (if not provided, state will be in-memory only)
|
|
326
|
+
* @param force - If true, forces a reload from storage even if state is already loaded
|
|
327
|
+
* @returns Promise that resolves to true if state was loaded, false if it was already loaded
|
|
328
|
+
*/
|
|
201
329
|
public load (context: TurnContext, storage?: Storage, force: boolean = false): Promise<boolean> {
|
|
202
330
|
if (this._isLoaded && !force) {
|
|
203
331
|
return Promise.resolve(false)
|
|
@@ -242,6 +370,14 @@ export class TurnState<
|
|
|
242
370
|
return this._loadingPromise
|
|
243
371
|
}
|
|
244
372
|
|
|
373
|
+
/**
|
|
374
|
+
* Saves state changes to storage.
|
|
375
|
+
* Only changed scopes will be persisted.
|
|
376
|
+
* @param context - The turn context
|
|
377
|
+
* @param storage - Optional storage provider (if not provided, state changes won't be persisted)
|
|
378
|
+
* @returns Promise that resolves when the save operation is complete
|
|
379
|
+
* @throws Error if state hasn't been loaded
|
|
380
|
+
*/
|
|
245
381
|
public async save (context: TurnContext, storage?: Storage): Promise<void> {
|
|
246
382
|
if (!this._isLoaded && this._loadingPromise) {
|
|
247
383
|
await this._loadingPromise
|
|
@@ -291,6 +427,13 @@ export class TurnState<
|
|
|
291
427
|
}
|
|
292
428
|
}
|
|
293
429
|
|
|
430
|
+
/**
|
|
431
|
+
* Computes the storage keys for each scope based on the turn context.
|
|
432
|
+
* Override this method in derived classes to add or modify storage keys.
|
|
433
|
+
* @param context - The turn context
|
|
434
|
+
* @returns Promise that resolves to a dictionary of scope names to storage keys
|
|
435
|
+
* @protected
|
|
436
|
+
*/
|
|
294
437
|
protected onComputeStorageKeys (context: TurnContext): Promise<Record<string, string>> {
|
|
295
438
|
const activity = context.activity
|
|
296
439
|
const channelId = activity?.channelId
|
|
@@ -321,6 +464,13 @@ export class TurnState<
|
|
|
321
464
|
return Promise.resolve(keys)
|
|
322
465
|
}
|
|
323
466
|
|
|
467
|
+
/**
|
|
468
|
+
* Parses a dot-notation path into scope and property name.
|
|
469
|
+
* If no scope is specified, defaults to the temp scope.
|
|
470
|
+
* @param path - The path to parse (format: "scope.property" or just "property")
|
|
471
|
+
* @returns Object containing the scope entry and property name
|
|
472
|
+
* @private
|
|
473
|
+
*/
|
|
324
474
|
private getScopeAndName (path: string): { scope: TurnStateEntry; name: string } {
|
|
325
475
|
const parts = path.split('.')
|
|
326
476
|
if (parts.length > 2) {
|
|
@@ -3,26 +3,47 @@
|
|
|
3
3
|
* Licensed under the MIT License.
|
|
4
4
|
*/
|
|
5
5
|
|
|
6
|
+
/**
|
|
7
|
+
* Represents an entry in the turn state that can be tracked for changes and stored.
|
|
8
|
+
*/
|
|
6
9
|
export class TurnStateEntry {
|
|
7
10
|
private _value: Record<string, unknown>
|
|
8
11
|
private _storageKey?: string
|
|
9
12
|
private _deleted = false
|
|
10
13
|
private _hash: string
|
|
11
14
|
|
|
15
|
+
/**
|
|
16
|
+
* Creates a new instance of TurnStateEntry.
|
|
17
|
+
* @param value - The initial value for the entry.
|
|
18
|
+
* @param storageKey - Optional storage key used to persist the entry.
|
|
19
|
+
*/
|
|
12
20
|
public constructor (value?: Record<string, unknown>, storageKey?: string) {
|
|
13
21
|
this._value = value || {}
|
|
14
22
|
this._storageKey = storageKey
|
|
15
23
|
this._hash = JSON.stringify(this._value)
|
|
16
24
|
}
|
|
17
25
|
|
|
26
|
+
/**
|
|
27
|
+
* Gets whether the entry value has changed since its creation or last update.
|
|
28
|
+
* @returns True if the value has changed, otherwise false.
|
|
29
|
+
*/
|
|
18
30
|
public get hasChanged (): boolean {
|
|
19
31
|
return JSON.stringify(this._value) !== this._hash
|
|
20
32
|
}
|
|
21
33
|
|
|
34
|
+
/**
|
|
35
|
+
* Gets whether the entry has been marked for deletion.
|
|
36
|
+
* @returns True if the entry is marked for deletion, otherwise false.
|
|
37
|
+
*/
|
|
22
38
|
public get isDeleted (): boolean {
|
|
23
39
|
return this._deleted
|
|
24
40
|
}
|
|
25
41
|
|
|
42
|
+
/**
|
|
43
|
+
* Gets the current value of the entry. If the entry was marked for deletion,
|
|
44
|
+
* it will be reset to an empty object and marked as not deleted.
|
|
45
|
+
* @returns The current value of the entry.
|
|
46
|
+
*/
|
|
26
47
|
public get value (): Record<string, unknown> {
|
|
27
48
|
if (this.isDeleted) {
|
|
28
49
|
this._value = {}
|
|
@@ -32,14 +53,25 @@ export class TurnStateEntry {
|
|
|
32
53
|
return this._value
|
|
33
54
|
}
|
|
34
55
|
|
|
56
|
+
/**
|
|
57
|
+
* Gets the storage key associated with this entry.
|
|
58
|
+
* @returns The storage key, or undefined if no storage key was specified.
|
|
59
|
+
*/
|
|
35
60
|
public get storageKey (): string | undefined {
|
|
36
61
|
return this._storageKey
|
|
37
62
|
}
|
|
38
63
|
|
|
64
|
+
/**
|
|
65
|
+
* Marks the entry for deletion.
|
|
66
|
+
*/
|
|
39
67
|
public delete (): void {
|
|
40
68
|
this._deleted = true
|
|
41
69
|
}
|
|
42
70
|
|
|
71
|
+
/**
|
|
72
|
+
* Replaces the current value with a new value.
|
|
73
|
+
* @param value - The new value to set. If undefined, an empty object will be used.
|
|
74
|
+
*/
|
|
43
75
|
public replace (value?: Record<string, unknown>): void {
|
|
44
76
|
this._value = value || {}
|
|
45
77
|
}
|
|
@@ -7,13 +7,44 @@
|
|
|
7
7
|
* Represents the authentication configuration.
|
|
8
8
|
*/
|
|
9
9
|
export interface AuthConfiguration {
|
|
10
|
+
/**
|
|
11
|
+
* The tenant ID for the authentication configuration.
|
|
12
|
+
*/
|
|
10
13
|
tenantId?: string
|
|
14
|
+
|
|
15
|
+
/**
|
|
16
|
+
* The client ID for the authentication configuration. Required in production.
|
|
17
|
+
*/
|
|
11
18
|
clientId?: string
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The client secret for the authentication configuration.
|
|
22
|
+
*/
|
|
12
23
|
clientSecret?: string
|
|
24
|
+
|
|
25
|
+
/**
|
|
26
|
+
* The path to the certificate PEM file.
|
|
27
|
+
*/
|
|
13
28
|
certPemFile?: string
|
|
29
|
+
|
|
30
|
+
/**
|
|
31
|
+
* The path to the certificate key file.
|
|
32
|
+
*/
|
|
14
33
|
certKeyFile?: string
|
|
34
|
+
|
|
35
|
+
/**
|
|
36
|
+
* A list of valid issuers for the authentication configuration.
|
|
37
|
+
*/
|
|
15
38
|
issuers: string[]
|
|
16
|
-
|
|
39
|
+
|
|
40
|
+
/**
|
|
41
|
+
* The connection name for the authentication configuration.
|
|
42
|
+
*/
|
|
43
|
+
connectionName?: string
|
|
44
|
+
|
|
45
|
+
/**
|
|
46
|
+
* The FIC (First-Party Integration Channel) client ID.
|
|
47
|
+
*/
|
|
17
48
|
FICClientId?: string
|
|
18
49
|
}
|
|
19
50
|
|