@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.
Files changed (128) hide show
  1. package/README.md +17 -1
  2. package/dist/src/activityHandler.d.ts +304 -46
  3. package/dist/src/activityHandler.js +298 -45
  4. package/dist/src/activityHandler.js.map +1 -1
  5. package/dist/src/agent-client/agentClient.d.ts +50 -0
  6. package/dist/src/agent-client/agentClient.js +28 -0
  7. package/dist/src/agent-client/agentClient.js.map +1 -1
  8. package/dist/src/app/agentApplication.d.ts +272 -7
  9. package/dist/src/app/agentApplication.js +278 -12
  10. package/dist/src/app/agentApplication.js.map +1 -1
  11. package/dist/src/app/agentApplicationBuilder.d.ts +34 -3
  12. package/dist/src/app/agentApplicationBuilder.js +38 -6
  13. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  14. package/dist/src/app/agentApplicationOptions.d.ts +26 -2
  15. package/dist/src/app/appMemory.d.ts +34 -0
  16. package/dist/src/app/{memory.js → appMemory.js} +1 -1
  17. package/dist/src/app/appMemory.js.map +1 -0
  18. package/dist/src/app/appRoute.d.ts +6 -0
  19. package/dist/src/app/attachmentDownloader.d.ts +18 -0
  20. package/dist/src/app/attachmentDownloader.js +18 -0
  21. package/dist/src/app/attachmentDownloader.js.map +1 -1
  22. package/dist/src/app/conversationUpdateEvents.d.ts +6 -0
  23. package/dist/src/app/index.d.ts +4 -1
  24. package/dist/src/app/index.js +4 -1
  25. package/dist/src/app/index.js.map +1 -1
  26. package/dist/src/app/inputFileDownloader.d.ts +22 -0
  27. package/dist/src/app/oauth/authorization.d.ts +87 -0
  28. package/dist/src/app/oauth/authorization.js +135 -0
  29. package/dist/src/app/oauth/authorization.js.map +1 -0
  30. package/dist/src/app/routeHandler.d.ts +8 -0
  31. package/dist/src/app/routeSelector.d.ts +9 -0
  32. package/dist/src/app/turnEvents.d.ts +6 -0
  33. package/dist/src/app/turnState.d.ts +152 -2
  34. package/dist/src/app/turnState.js +125 -0
  35. package/dist/src/app/turnState.js.map +1 -1
  36. package/dist/src/app/turnStateEntry.d.ts +32 -0
  37. package/dist/src/app/turnStateEntry.js +32 -0
  38. package/dist/src/app/turnStateEntry.js.map +1 -1
  39. package/dist/src/auth/authConfiguration.d.ts +24 -0
  40. package/dist/src/auth/authConfiguration.js.map +1 -1
  41. package/dist/src/auth/request.d.ts +12 -0
  42. package/dist/src/baseAdapter.d.ts +17 -0
  43. package/dist/src/baseAdapter.js +17 -0
  44. package/dist/src/baseAdapter.js.map +1 -1
  45. package/dist/src/cards/cardFactory.d.ts +3 -0
  46. package/dist/src/cards/cardFactory.js +3 -0
  47. package/dist/src/cards/cardFactory.js.map +1 -1
  48. package/dist/src/cards/index.d.ts +1 -0
  49. package/dist/src/cards/index.js +1 -0
  50. package/dist/src/cards/index.js.map +1 -1
  51. package/dist/src/cards/o365ConnectorCardActionBase.d.ts +8 -0
  52. package/dist/src/cloudAdapter.d.ts +25 -3
  53. package/dist/src/cloudAdapter.js +25 -3
  54. package/dist/src/cloudAdapter.js.map +1 -1
  55. package/dist/src/getProductInfo.d.ts +6 -0
  56. package/dist/src/getProductInfo.js +6 -0
  57. package/dist/src/getProductInfo.js.map +1 -1
  58. package/dist/src/logger.d.ts +34 -2
  59. package/dist/src/logger.js +35 -0
  60. package/dist/src/logger.js.map +1 -1
  61. package/dist/src/oauth/oAuthFlow.d.ts +32 -3
  62. package/dist/src/oauth/oAuthFlow.js +38 -14
  63. package/dist/src/oauth/oAuthFlow.js.map +1 -1
  64. package/dist/src/oauth/userTokenClient.d.ts +2 -2
  65. package/dist/src/oauth/userTokenClient.js +3 -3
  66. package/dist/src/oauth/userTokenClient.js.map +1 -1
  67. package/dist/src/state/agentState.d.ts +79 -27
  68. package/dist/src/state/agentState.js +58 -27
  69. package/dist/src/state/agentState.js.map +1 -1
  70. package/dist/src/state/agentStatePropertyAccesor.d.ts +67 -11
  71. package/dist/src/state/agentStatePropertyAccesor.js +58 -11
  72. package/dist/src/state/agentStatePropertyAccesor.js.map +1 -1
  73. package/dist/src/statusCodes.d.ts +39 -0
  74. package/dist/src/statusCodes.js +39 -0
  75. package/dist/src/statusCodes.js.map +1 -1
  76. package/dist/src/storage/memoryStorage.d.ts +48 -14
  77. package/dist/src/storage/memoryStorage.js +48 -14
  78. package/dist/src/storage/memoryStorage.js.map +1 -1
  79. package/dist/src/storage/storage.d.ts +43 -13
  80. package/dist/src/tokenResponseEventName.d.ts +3 -0
  81. package/dist/src/tokenResponseEventName.js +3 -0
  82. package/dist/src/tokenResponseEventName.js.map +1 -1
  83. package/dist/src/turnContext.d.ts +142 -56
  84. package/dist/src/turnContext.js +123 -53
  85. package/dist/src/turnContext.js.map +1 -1
  86. package/package.json +6 -6
  87. package/src/activityHandler.ts +304 -46
  88. package/src/agent-client/agentClient.ts +55 -5
  89. package/src/app/agentApplication.ts +283 -15
  90. package/src/app/agentApplicationBuilder.ts +39 -7
  91. package/src/app/agentApplicationOptions.ts +33 -2
  92. package/src/app/appMemory.ts +38 -0
  93. package/src/app/appRoute.ts +7 -0
  94. package/src/app/attachmentDownloader.ts +18 -0
  95. package/src/app/conversationUpdateEvents.ts +6 -0
  96. package/src/app/index.ts +4 -1
  97. package/src/app/inputFileDownloader.ts +24 -0
  98. package/src/app/oauth/authorization.ts +162 -0
  99. package/src/app/routeHandler.ts +8 -0
  100. package/src/app/routeSelector.ts +9 -0
  101. package/src/app/turnEvents.ts +6 -0
  102. package/src/app/turnState.ts +153 -3
  103. package/src/app/turnStateEntry.ts +32 -0
  104. package/src/auth/authConfiguration.ts +32 -1
  105. package/src/auth/request.ts +15 -0
  106. package/src/baseAdapter.ts +18 -0
  107. package/src/cards/cardFactory.ts +3 -0
  108. package/src/cards/index.ts +1 -0
  109. package/src/cards/o365ConnectorCardActionBase.ts +8 -0
  110. package/src/cloudAdapter.ts +28 -3
  111. package/src/getProductInfo.ts +7 -0
  112. package/src/logger.ts +34 -1
  113. package/src/oauth/oAuthFlow.ts +59 -18
  114. package/src/oauth/userTokenClient.ts +4 -4
  115. package/src/state/agentState.ts +81 -29
  116. package/src/state/agentStatePropertyAccesor.ts +67 -11
  117. package/src/statusCodes.ts +51 -0
  118. package/src/storage/memoryStorage.ts +48 -14
  119. package/src/storage/storage.ts +51 -18
  120. package/src/tokenResponseEventName.ts +3 -0
  121. package/src/turnContext.ts +142 -56
  122. package/dist/src/app/memory.d.ts +0 -10
  123. package/dist/src/app/memory.js.map +0 -1
  124. package/dist/src/app/oauth/userIdentity.d.ts +0 -43
  125. package/dist/src/app/oauth/userIdentity.js +0 -54
  126. package/dist/src/app/oauth/userIdentity.js.map +0 -1
  127. package/src/app/memory.ts +0 -14
  128. package/src/app/oauth/userIdentity.ts +0 -78
@@ -6,6 +6,15 @@
6
6
  import { TurnContext } from '../turnContext'
7
7
  import { AgentState, CustomKey } from './agentState'
8
8
 
9
+ /**
10
+ * Interface for accessing a property in state storage with type safety.
11
+ *
12
+ * The interface defines standard methods for working with persisted state properties,
13
+ * allowing property access with strong typing to reduce errors when working with
14
+ * complex state objects.
15
+ *
16
+ * @typeParam T The type of the property being accessed
17
+ */
9
18
  export interface StatePropertyAccessor<T = any> {
10
19
  /**
11
20
  * Deletes the persisted property from its backing storage object.
@@ -64,20 +73,56 @@ export interface StatePropertyAccessor<T = any> {
64
73
  }
65
74
 
66
75
  /**
67
- * Provides access to an Agent state property.
76
+ * Provides typed access to an Agent state property with automatic state loading.
77
+ *
78
+ * The AgentStatePropertyAccessor simplifies working with state by abstracting
79
+ * the details of loading state from storage and manipulating specific properties.
80
+ * It automatically handles:
81
+ *
82
+ * - Loading state when needed
83
+ * - Deep cloning of default values to prevent reference issues
84
+ * - Type checking for properties (when using TypeScript)
85
+ * - Ensuring properties exist before access
86
+ *
87
+ * Property accessors are created through the {@link AgentState.createProperty} method:
88
+ *
89
+ * ```typescript
90
+ * // Create a property accessor for a user profile
91
+ * const userProfile = userState.createProperty<UserProfile>("userProfile");
92
+ *
93
+ * // Get the profile with a default if not exists
94
+ * const profile = await userProfile.get(context, { name: "", preferences: {} });
95
+ *
96
+ * // Update a value
97
+ * profile.preferences.theme = "dark";
98
+ *
99
+ * // Save the change
100
+ * await userProfile.set(context, profile);
101
+ *
102
+ * // Later, call userState.saveChanges(context) to persist to storage
103
+ * ```
104
+ *
105
+ * @typeParam T The type of the property being accessed
68
106
  */
69
107
  export class AgentStatePropertyAccessor<T = any> implements StatePropertyAccessor<T> {
70
108
  /**
71
109
  * Creates a new instance of AgentStatePropertyAccessor.
72
- * @param state The agent state.
73
- * @param name The name of the property.
110
+ *
111
+ * @param state The agent state object that will contain this property
112
+ * @param name The name of the property in the state object
74
113
  */
75
114
  constructor (protected readonly state: AgentState, public readonly name: string) { }
76
115
 
77
116
  /**
78
117
  * Deletes the property from the state.
79
- * @param context The turn context.
80
- * @returns A promise that resolves when the delete operation is complete.
118
+ *
119
+ * This removes the property from the state object but does not automatically
120
+ * persist the change to storage. Call state.saveChanges() afterwards to
121
+ * persist changes.
122
+ *
123
+ * @param context The turn context
124
+ * @param customKey Optional custom key for storing the state in a specific location
125
+ * @returns A promise that resolves when the delete operation is complete
81
126
  */
82
127
  async delete (context: TurnContext, customKey?: CustomKey): Promise<void> {
83
128
  const obj: any = await this.state.load(context, false, customKey)
@@ -88,9 +133,15 @@ export class AgentStatePropertyAccessor<T = any> implements StatePropertyAccesso
88
133
 
89
134
  /**
90
135
  * Gets the value of the property from the state.
91
- * @param context The turn context.
92
- * @param defaultValue The default value to return if the property is not found.
93
- * @returns A promise that resolves to the value of the property.
136
+ *
137
+ * If the property doesn't exist and a default value is provided, a deep clone
138
+ * of the default value will be stored in state and returned. This ensures that
139
+ * modifications to the returned object will be properly tracked.
140
+ *
141
+ * @param context The turn context
142
+ * @param defaultValue Optional default value to use if the property doesn't exist
143
+ * @param customKey Optional custom key for storing the state in a specific location
144
+ * @returns A promise that resolves to the value of the property or undefined
94
145
  */
95
146
  async get (context: TurnContext, defaultValue?: T, customKey?: CustomKey): Promise<T> {
96
147
  const obj: any = await this.state.load(context, false, customKey)
@@ -107,9 +158,14 @@ export class AgentStatePropertyAccessor<T = any> implements StatePropertyAccesso
107
158
 
108
159
  /**
109
160
  * Sets the value of the property in the state.
110
- * @param context The turn context.
111
- * @param value The value to set.
112
- * @returns A promise that resolves when the set operation is complete.
161
+ *
162
+ * This updates the property in the in-memory state object but does not automatically
163
+ * persist the change to storage. Call state.saveChanges() afterwards to persist changes.
164
+ *
165
+ * @param context The turn context
166
+ * @param value The value to set
167
+ * @param customKey Optional custom key for storing the state in a specific location
168
+ * @returns A promise that resolves when the set operation is complete
113
169
  */
114
170
  async set (context: TurnContext, value: T, customKey?: CustomKey): Promise<void> {
115
171
  const obj: any = await this.state.load(context, false, customKey)
@@ -4,17 +4,68 @@
4
4
  */
5
5
 
6
6
  export enum StatusCodes {
7
+ /**
8
+ * The request has succeeded.
9
+ */
7
10
  OK = 200,
11
+
12
+ /**
13
+ * The request has been fulfilled and resulted in a new resource being created.
14
+ */
8
15
  CREATED = 201,
16
+
17
+ /**
18
+ * Indicates multiple options for the resource that the client may follow.
19
+ */
9
20
  MULTIPLE_CHOICES = 300,
21
+
22
+ /**
23
+ * The server cannot or will not process the request due to a client error.
24
+ */
10
25
  BAD_REQUEST = 400,
26
+
27
+ /**
28
+ * The request requires user authentication.
29
+ */
11
30
  UNAUTHORIZED = 401,
31
+
32
+ /**
33
+ * The requested resource could not be found.
34
+ */
12
35
  NOT_FOUND = 404,
36
+
37
+ /**
38
+ * The request method is not allowed for the requested resource.
39
+ */
13
40
  METHOD_NOT_ALLOWED = 405,
41
+
42
+ /**
43
+ * The request could not be completed due to a conflict with the current state of the resource.
44
+ */
14
45
  CONFLICT = 409,
46
+
47
+ /**
48
+ * The server does not meet one of the preconditions specified by the client.
49
+ */
15
50
  PRECONDITION_FAILED = 412,
51
+
52
+ /**
53
+ * The client should switch to a different protocol.
54
+ */
16
55
  UPGRADE_REQUIRED = 426,
56
+
57
+ /**
58
+ * The server encountered an unexpected condition that prevented it from fulfilling the request.
59
+ */
17
60
  INTERNAL_SERVER_ERROR = 500,
61
+
62
+ /**
63
+ * The server does not support the functionality required to fulfill the request.
64
+ */
18
65
  NOT_IMPLEMENTED = 501,
66
+
67
+ /**
68
+ * The server received an invalid response from the upstream server.
69
+ */
19
70
  BAD_GATEWAY = 502,
20
71
  }
@@ -9,20 +9,39 @@ import { debug } from '../logger'
9
9
  const logger = debug('agents:memory-storage')
10
10
 
11
11
  /**
12
- * A simple in-memory storage provider.
12
+ * A simple in-memory storage provider that implements the Storage interface.
13
+ *
14
+ * This class provides a volatile storage solution that keeps data in memory,
15
+ * which means data is lost when the process terminates. It's primarily useful for:
16
+ * - Development and testing scenarios
17
+ * - Simple applications that don't require data persistence across restarts
18
+ * - Stateless environments where external storage isn't available
19
+ *
20
+ * MemoryStorage supports optimistic concurrency control through eTags and
21
+ * can be used as a singleton through the getSingleInstance() method to
22
+ * share state across different parts of an application.
13
23
  */
14
24
  export class MemoryStorage implements Storage {
15
25
  private static instance: MemoryStorage
26
+ /**
27
+ * Counter used to generate unique eTags for stored items
28
+ */
16
29
  private etag: number = 1
17
30
 
18
31
  /**
19
32
  * Creates a new instance of the MemoryStorage class.
20
- * @param memory An optional initial memory store.
33
+ *
34
+ * @param memory An optional initial memory store to seed the storage with data
21
35
  */
22
36
  constructor (private memory: { [k: string]: string } = {}) { }
23
37
 
24
38
  /**
25
- * Gets a single instance of the MemoryStorage class.
39
+ * Gets a single shared instance of the MemoryStorage class.
40
+ *
41
+ * Using this method ensures that the same storage instance is used across
42
+ * the application, allowing for shared state without passing references.
43
+ *
44
+ * @returns The singleton instance of MemoryStorage
26
45
  */
27
46
  static getSingleInstance (): MemoryStorage {
28
47
  if (!MemoryStorage.instance) {
@@ -33,9 +52,10 @@ export class MemoryStorage implements Storage {
33
52
 
34
53
  /**
35
54
  * Reads storage items from memory.
36
- * @param keys The keys of the items to read.
37
- * @returns A promise that resolves to the read items.
38
- * @throws Will throw an error if keys are not provided.
55
+ *
56
+ * @param keys The keys of the items to read
57
+ * @returns A promise that resolves to the read items
58
+ * @throws Will throw an error if keys are not provided or the array is empty
39
59
  */
40
60
  async read (keys: string[]): Promise<StoreItem> {
41
61
  if (!keys || keys.length === 0) {
@@ -56,9 +76,15 @@ export class MemoryStorage implements Storage {
56
76
 
57
77
  /**
58
78
  * Writes storage items to memory.
59
- * @param changes The items to write.
60
- * @returns A promise that resolves when the write operation is complete.
61
- * @throws Will throw an error if changes are not provided.
79
+ *
80
+ * This method supports optimistic concurrency control through eTags.
81
+ * If an item has an eTag, it will only be updated if the existing item
82
+ * has the same eTag. If an item has an eTag of '*' or no eTag, it will
83
+ * always be written regardless of the current state.
84
+ *
85
+ * @param changes The items to write, indexed by key
86
+ * @returns A promise that resolves when the write operation is complete
87
+ * @throws Will throw an error if changes are not provided or if there's an eTag conflict
62
88
  */
63
89
  async write (changes: StoreItem): Promise<void> {
64
90
  if (!changes || changes.length === 0) {
@@ -83,8 +109,9 @@ export class MemoryStorage implements Storage {
83
109
 
84
110
  /**
85
111
  * Deletes storage items from memory.
86
- * @param keys The keys of the items to delete.
87
- * @returns A promise that resolves when the delete operation is complete.
112
+ *
113
+ * @param keys The keys of the items to delete
114
+ * @returns A promise that resolves when the delete operation is complete
88
115
  */
89
116
  async delete (keys: string[]): Promise<void> {
90
117
  logger.info(`Deleting keys: ${keys.join(', ')}`)
@@ -94,9 +121,16 @@ export class MemoryStorage implements Storage {
94
121
  }
95
122
 
96
123
  /**
97
- * Saves an item to memory.
98
- * @param key The key of the item to save.
99
- * @param item The item to save.
124
+ * Saves an item to memory with a new eTag.
125
+ *
126
+ * This private method handles the details of:
127
+ * - Creating a clone of the item to prevent modification of the original
128
+ * - Generating a new eTag for optimistic concurrency control
129
+ * - Converting the item to a JSON string for storage
130
+ *
131
+ * @param key The key of the item to save
132
+ * @param item The item to save
133
+ * @private
100
134
  */
101
135
  private saveItem (key: string, item: unknown): void {
102
136
  const clone = Object.assign({}, item, { eTag: (this.etag++).toString() })
@@ -6,47 +6,80 @@
6
6
  import { TurnContext } from '../turnContext'
7
7
 
8
8
  /**
9
- * Represents an item to be stored.
9
+ * Represents an item to be stored in a storage provider.
10
+ * Each item can contain arbitrary data along with an optional eTag for optimistic concurrency control.
10
11
  */
11
12
  export interface StoreItem {
12
- eTag?: string
13
- [key: string]: any
13
+ /**
14
+ * Optional eTag used for optimistic concurrency control.
15
+ * When set to '*', it indicates that the write should proceed regardless of existing data.
16
+ * When comparing eTags, exact string matching is used to determine if data has changed.
17
+ */
18
+ eTag?: string;
19
+
20
+ /**
21
+ * Additional properties can be stored in the item.
22
+ * Each storage provider may have specific requirements or limitations on property names and values.
23
+ */
24
+ [key: string]: any;
14
25
  }
15
26
 
16
27
  /**
17
- * Represents a collection of store items.
28
+ * Represents a collection of store items indexed by key.
29
+ * Used as the return type for storage read operations.
18
30
  */
19
31
  export interface StoreItems {
32
+ /**
33
+ * Keys are the storage item identifiers, and values are the stored items.
34
+ * If a requested key is not found during a read operation, it will not appear in this collection.
35
+ */
20
36
  [key: string]: any;
21
37
  }
22
38
 
23
39
  /**
24
- * A factory function to generate storage keys based on the context.
25
- * @param context The TurnContext for the current turn of conversation.
26
- * @returns A string key for storage.
40
+ * A factory function to generate storage keys based on the conversation context.
41
+ * Allows different storage strategies based on the conversation state.
42
+ *
43
+ * @param context The TurnContext for the current turn of conversation
44
+ * @returns A string key for storage that uniquely identifies where to store the data
27
45
  */
28
- export type StorageKeyFactory = (context: TurnContext) => string
46
+ export type StorageKeyFactory = (context: TurnContext) => string | Promise<string>
29
47
 
30
48
  /**
31
- * Defines the interface for storage operations.
49
+ * Defines the interface for storage operations in the Agents platform.
50
+ *
51
+ * Storage providers persist state data across conversation turns, enabling
52
+ * agents to maintain context over time. Different implementations may store
53
+ * data in memory, databases, blob storage, or other persistence mechanisms.
54
+ *
55
+ * The interface is designed to be simple with just three core operations:
56
+ * read, write, and delete. All operations are asynchronous to support both
57
+ * in-memory and remote storage providers.
32
58
  */
33
59
  export interface Storage {
34
60
  /**
35
61
  * Reads store items from storage.
36
- * @param keys The keys of the items to read.
37
- * @returns A promise that resolves to the store items.
62
+ *
63
+ * @param keys The keys of the items to read
64
+ * @returns A promise that resolves to the store items. Items that don't exist in storage will not be included in the result.
65
+ * @throws If the keys array is empty or undefined
38
66
  */
39
- read: (keys: string[]) => Promise<StoreItem>
67
+ read: (keys: string[]) => Promise<StoreItem>;
68
+
40
69
  /**
41
70
  * Writes store items to storage.
42
- * @param changes The items to write to storage.
43
- * @returns A promise that resolves when the write operation is complete.
71
+ *
72
+ * @param changes The items to write to storage, indexed by key
73
+ * @returns A promise that resolves when the write operation is complete
74
+ * @throws If the changes object is empty or undefined, or if an eTag conflict occurs and optimistic concurrency is enabled
44
75
  */
45
- write: (changes: StoreItem) => Promise<void>
76
+ write: (changes: StoreItem) => Promise<void>;
77
+
46
78
  /**
47
79
  * Deletes store items from storage.
48
- * @param keys The keys of the items to delete.
49
- * @returns A promise that resolves when the delete operation is complete.
80
+ *
81
+ * @param keys The keys of the items to delete
82
+ * @returns A promise that resolves when the delete operation is complete
50
83
  */
51
- delete: (keys: string[]) => Promise<void>
84
+ delete: (keys: string[]) => Promise<void>;
52
85
  }
@@ -3,4 +3,7 @@
3
3
  * Licensed under the MIT License.
4
4
  */
5
5
 
6
+ /**
7
+ * The event name used for token response activities.
8
+ */
6
9
  export const tokenResponseEventName = 'tokens/response'