@microsoft/agents-hosting 1.5.0-beta.1.g77c44097fe → 1.5.0-beta.12.ga9a2b23c19

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 (61) hide show
  1. package/dist/package.json +2 -2
  2. package/dist/src/app/agentApplication.d.ts +31 -9
  3. package/dist/src/app/agentApplication.js +125 -82
  4. package/dist/src/app/agentApplication.js.map +1 -1
  5. package/dist/src/app/agentApplicationBuilder.d.ts +7 -0
  6. package/dist/src/app/agentApplicationBuilder.js +9 -0
  7. package/dist/src/app/agentApplicationBuilder.js.map +1 -1
  8. package/dist/src/app/agentApplicationOptions.d.ts +6 -0
  9. package/dist/src/app/auth/authorizationManager.d.ts +4 -0
  10. package/dist/src/app/auth/authorizationManager.js +10 -9
  11. package/dist/src/app/auth/authorizationManager.js.map +1 -1
  12. package/dist/src/app/auth/handlers/azureBotAuthorization.js +3 -3
  13. package/dist/src/app/auth/handlers/azureBotAuthorization.js.map +1 -1
  14. package/dist/src/app/index.d.ts +1 -0
  15. package/dist/src/app/index.js +1 -0
  16. package/dist/src/app/index.js.map +1 -1
  17. package/dist/src/app/proactive/conversation.d.ts +43 -0
  18. package/dist/src/app/proactive/conversation.js +67 -0
  19. package/dist/src/app/proactive/conversation.js.map +1 -0
  20. package/dist/src/app/proactive/conversationBuilder.d.ts +54 -0
  21. package/dist/src/app/proactive/conversationBuilder.js +110 -0
  22. package/dist/src/app/proactive/conversationBuilder.js.map +1 -0
  23. package/dist/src/app/proactive/conversationReferenceBuilder.d.ts +68 -0
  24. package/dist/src/app/proactive/conversationReferenceBuilder.js +125 -0
  25. package/dist/src/app/proactive/conversationReferenceBuilder.js.map +1 -0
  26. package/dist/src/app/proactive/createConversationOptions.d.ts +30 -0
  27. package/dist/src/app/proactive/createConversationOptions.js +10 -0
  28. package/dist/src/app/proactive/createConversationOptions.js.map +1 -0
  29. package/dist/src/app/proactive/createConversationOptionsBuilder.d.ts +69 -0
  30. package/dist/src/app/proactive/createConversationOptionsBuilder.js +141 -0
  31. package/dist/src/app/proactive/createConversationOptionsBuilder.js.map +1 -0
  32. package/dist/src/app/proactive/index.d.ts +7 -0
  33. package/dist/src/app/proactive/index.js +26 -0
  34. package/dist/src/app/proactive/index.js.map +1 -0
  35. package/dist/src/app/proactive/proactive.d.ts +248 -0
  36. package/dist/src/app/proactive/proactive.js +271 -0
  37. package/dist/src/app/proactive/proactive.js.map +1 -0
  38. package/dist/src/app/proactive/proactiveOptions.d.ts +19 -0
  39. package/dist/src/app/proactive/proactiveOptions.js +5 -0
  40. package/dist/src/app/proactive/proactiveOptions.js.map +1 -0
  41. package/dist/src/errorHelper.js +94 -0
  42. package/dist/src/errorHelper.js.map +1 -1
  43. package/dist/src/turnContext.js +7 -1
  44. package/dist/src/turnContext.js.map +1 -1
  45. package/package.json +2 -2
  46. package/src/app/agentApplication.ts +130 -79
  47. package/src/app/agentApplicationBuilder.ts +11 -0
  48. package/src/app/agentApplicationOptions.ts +7 -0
  49. package/src/app/auth/authorizationManager.ts +14 -9
  50. package/src/app/auth/handlers/azureBotAuthorization.ts +3 -3
  51. package/src/app/index.ts +1 -0
  52. package/src/app/proactive/conversation.ts +87 -0
  53. package/src/app/proactive/conversationBuilder.ts +139 -0
  54. package/src/app/proactive/conversationReferenceBuilder.ts +161 -0
  55. package/src/app/proactive/createConversationOptions.ts +35 -0
  56. package/src/app/proactive/createConversationOptionsBuilder.ts +181 -0
  57. package/src/app/proactive/index.ts +10 -0
  58. package/src/app/proactive/proactive.ts +481 -0
  59. package/src/app/proactive/proactiveOptions.ts +24 -0
  60. package/src/errorHelper.ts +108 -0
  61. package/src/turnContext.ts +6 -1
@@ -0,0 +1,161 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ import type { ChannelAccount, ConversationReference } from '@microsoft/agents-activity'
5
+ import { Channels, RoleTypes } from '@microsoft/agents-activity'
6
+ import { debug } from '@microsoft/agents-activity/logger'
7
+
8
+ const logger = debug('agents:conversation-reference-builder')
9
+
10
+ /** Set of all channel IDs defined in the Channels enum, used for validation. */
11
+ const knownChannelIds = new Set(Object.values(Channels) as string[])
12
+
13
+ /**
14
+ * Well-known Teams service URLs for proactive messaging.
15
+ *
16
+ * Use only when the incoming `serviceUrl` from a real conversation is unavailable.
17
+ * Once you have received a `serviceUrl` from a real turn, cache and prefer that value.
18
+ */
19
+ export const TeamsServiceEndpoints = {
20
+ /** Standard public cloud Teams endpoint. */
21
+ publicGlobal: 'https://smba.trafficmanager.net/teams/',
22
+ /** US Government Community Cloud (GCC). */
23
+ gcc: 'https://smba.infra.gcc.teams.microsoft.com/teams',
24
+ /** US Government Community Cloud High (GCC-High). */
25
+ gccHigh: 'https://smba.infra.gov.teams.microsoft.us/teams',
26
+ /** US Department of Defense (DoD). */
27
+ dod: 'https://smba.infra.dod.teams.microsoft.us/teams',
28
+ } as const
29
+
30
+ /**
31
+ * Fluent builder for `ConversationReference`.
32
+ */
33
+ export class ConversationReferenceBuilder {
34
+ private readonly _channelId: string
35
+ private _serviceUrl: string
36
+ private _agent: ChannelAccount
37
+ private _user?: ChannelAccount
38
+ private _conversationId?: string
39
+ private _activityId?: string
40
+ private _locale?: string
41
+
42
+ private constructor (channelId: string, serviceUrl: string, agent: ChannelAccount) {
43
+ this._channelId = channelId
44
+ this._serviceUrl = serviceUrl
45
+ this._agent = agent
46
+ }
47
+
48
+ /**
49
+ * Creates a new builder seeded with the agent identity and channel.
50
+ * On Teams, the agent id is prefixed with `28:` automatically.
51
+ * @param agentClientId The agent's client (app) ID.
52
+ * @param channelId The target channel (e.g. `'msteams'`, `'webchat'`).
53
+ * @param serviceUrl Optional override. If omitted, `build()` fills in the
54
+ * channel default via `serviceUrlForChannel()`.
55
+ */
56
+ static create (
57
+ agentClientId: string,
58
+ channelId: string,
59
+ serviceUrl?: string
60
+ ): ConversationReferenceBuilder {
61
+ return new ConversationReferenceBuilder(
62
+ channelId,
63
+ serviceUrl ?? '',
64
+ ConversationReferenceBuilder.agentForChannel(channelId, agentClientId)
65
+ )
66
+ }
67
+
68
+ /**
69
+ * Returns the default service URL for a channel.
70
+ * Teams returns the public global endpoint; all other channels use the
71
+ * `https://{channelId}.botframework.com/` pattern (matching C# behavior).
72
+ */
73
+ static serviceUrlForChannel (channelId: string): string {
74
+ if (!channelId) return ''
75
+ if (channelId === Channels.Msteams) return TeamsServiceEndpoints.publicGlobal
76
+ if (!knownChannelIds.has(channelId)) {
77
+ logger.warn(
78
+ `serviceUrlForChannel: unrecognized channelId '${channelId}' — constructing fallback URL ` +
79
+ 'https://<channelId>.botframework.com/. Provide an explicit serviceUrl to suppress this warning.'
80
+ )
81
+ }
82
+ return `https://${channelId}.botframework.com/`
83
+ }
84
+
85
+ /** Sets `reference.user` from an id + optional name. Role defaults to `RoleTypes.User`. */
86
+ withUser (userId: string, userName?: string): this
87
+ /** Sets `reference.user` from a full `ChannelAccount`. */
88
+ withUser (account: ChannelAccount): this
89
+ withUser (userIdOrAccount: string | ChannelAccount, userName?: string): this {
90
+ this._user = typeof userIdOrAccount === 'string'
91
+ ? { id: userIdOrAccount, name: userName, role: RoleTypes.User }
92
+ : userIdOrAccount
93
+ return this
94
+ }
95
+
96
+ /** Sets `reference.agent` from an id + optional name. Role defaults to `RoleTypes.Agent`. On Teams, id is prefixed with `28:`. */
97
+ withAgent (agentClientId: string, agentName?: string): this
98
+ /** Sets `reference.agent` from a full `ChannelAccount`. */
99
+ withAgent (account: ChannelAccount): this
100
+ withAgent (agentIdOrAccount: string | ChannelAccount, agentName?: string): this {
101
+ this._agent = typeof agentIdOrAccount === 'string'
102
+ ? ConversationReferenceBuilder.agentForChannel(this._channelId, agentIdOrAccount, agentName)
103
+ : agentIdOrAccount
104
+ return this
105
+ }
106
+
107
+ /** Sets `reference.serviceUrl`. */
108
+ withServiceUrl (serviceUrl: string): this {
109
+ this._serviceUrl = serviceUrl
110
+ return this
111
+ }
112
+
113
+ /** Sets `reference.conversation.id`. */
114
+ withConversationId (id: string): this {
115
+ this._conversationId = id
116
+ return this
117
+ }
118
+
119
+ /** Sets `reference.activityId`. */
120
+ withActivityId (activityId: string): this {
121
+ this._activityId = activityId
122
+ return this
123
+ }
124
+
125
+ /** Sets `reference.locale`. */
126
+ withLocale (locale: string): this {
127
+ this._locale = locale
128
+ return this
129
+ }
130
+
131
+ /** Builds and returns the `ConversationReference`. */
132
+ build (): ConversationReference {
133
+ const serviceUrl =
134
+ this._serviceUrl || ConversationReferenceBuilder.serviceUrlForChannel(this._channelId)
135
+
136
+ const ref: ConversationReference = {
137
+ channelId: this._channelId,
138
+ serviceUrl,
139
+ conversation: { id: this._conversationId ?? '', isGroup: false },
140
+ agent: this._agent,
141
+ user: this._user ?? { role: RoleTypes.User },
142
+ }
143
+
144
+ if (this._activityId !== undefined) ref.activityId = this._activityId
145
+ if (this._locale !== undefined) ref.locale = this._locale
146
+
147
+ return ref
148
+ }
149
+
150
+ /**
151
+ * Builds a `ChannelAccount` for the agent, applying the Teams `28:` id prefix
152
+ * when the channel is `msteams`.
153
+ */
154
+ private static agentForChannel (channelId: string, agentClientId: string, agentName?: string): ChannelAccount {
155
+ return {
156
+ id: channelId === Channels.Msteams ? `28:${agentClientId}` : agentClientId,
157
+ name: agentName,
158
+ role: RoleTypes.Agent,
159
+ }
160
+ }
161
+ }
@@ -0,0 +1,35 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ import type { ConversationParameters } from '@microsoft/agents-activity'
5
+ import type { ConversationClaims } from './conversation'
6
+
7
+ /**
8
+ * Default OAuth scope for Azure Bot Service authentication.
9
+ */
10
+ export const AzureBotScope = 'https://api.botframework.com'
11
+
12
+ /**
13
+ * Options passed to `Proactive.createConversation()`.
14
+ * Flattened — no nested Conversation wrapper.
15
+ */
16
+ export interface CreateConversationOptions {
17
+ /** JWT claims for the agent identity. `aud` must be the agent's client ID. */
18
+ identity: ConversationClaims
19
+ /** The target channel (e.g. `'msteams'`). */
20
+ channelId: string
21
+ /** The service URL for the channel. */
22
+ serviceUrl: string
23
+ /**
24
+ * OAuth scope for token acquisition.
25
+ * Defaults to `AzureBotScope` when not set by the builder.
26
+ */
27
+ scope: string
28
+ /**
29
+ * When `true`, the resulting `Conversation` is stored automatically after
30
+ * creation. Defaults to `false`.
31
+ */
32
+ storeConversation?: boolean
33
+ /** Conversation configuration passed to `adapter.createConversationAsync()`. */
34
+ parameters: Partial<ConversationParameters>
35
+ }
@@ -0,0 +1,181 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ import type { Activity, ChannelAccount, ConversationParameters } from '@microsoft/agents-activity'
5
+ import { Channels, ExceptionHelper, RoleTypes } from '@microsoft/agents-activity'
6
+ import { Errors } from '../../errorHelper'
7
+ import { AzureBotScope, type CreateConversationOptions } from './createConversationOptions'
8
+ import type { ConversationClaims } from './conversation'
9
+ import { ConversationReferenceBuilder } from './conversationReferenceBuilder'
10
+
11
+ /**
12
+ * Fluent builder for `CreateConversationOptions`.
13
+ *
14
+ * @example
15
+ * ```typescript
16
+ * const opts = CreateConversationOptionsBuilder
17
+ * .create('my-client-id', 'msteams')
18
+ * .withUser('user-aad-id')
19
+ * .withTenantId('tenant-id')
20
+ * .build()
21
+ * ```
22
+ */
23
+ export class CreateConversationOptionsBuilder {
24
+ private readonly _claims: ConversationClaims
25
+ private readonly _channelId: string
26
+ private readonly _serviceUrl: string
27
+ private _scope: string = AzureBotScope
28
+ private _storeConversation: boolean = false
29
+ private _parameters: Partial<ConversationParameters> = {
30
+ channelData: {},
31
+ }
32
+
33
+ private _activity: Partial<Activity> | undefined
34
+
35
+ private constructor (claims: ConversationClaims, channelId: string, serviceUrl?: string) {
36
+ this._claims = claims
37
+ this._channelId = channelId
38
+ this._serviceUrl =
39
+ serviceUrl ?? ConversationReferenceBuilder.serviceUrlForChannel(channelId)
40
+ }
41
+
42
+ /**
43
+ * Creates a new builder from an agent client ID string.
44
+ * @param agentClientId The agent's client (app) ID.
45
+ * @param channelId The target channel (e.g. `'msteams'`).
46
+ * @param serviceUrl Optional service URL override.
47
+ */
48
+ static create (agentClientId: string, channelId: string, serviceUrl?: string, parameters?: Partial<ConversationParameters>): CreateConversationOptionsBuilder
49
+ /**
50
+ * Creates a new builder from an existing claims object (e.g. from a stored `Conversation`).
51
+ * @param claims JWT claims — `aud` must be the agent's client ID.
52
+ * @param channelId The target channel (e.g. `'msteams'`).
53
+ * @param serviceUrl Optional service URL override.
54
+ */
55
+ static create (claims: ConversationClaims, channelId: string, serviceUrl?: string, parameters?: Partial<ConversationParameters>): CreateConversationOptionsBuilder
56
+ static create (
57
+ agentClientIdOrClaims: string | ConversationClaims,
58
+ channelId: string,
59
+ serviceUrl?: string,
60
+ parameters?: Partial<ConversationParameters>
61
+ ): CreateConversationOptionsBuilder {
62
+ const claims: ConversationClaims = typeof agentClientIdOrClaims === 'string'
63
+ ? { aud: agentClientIdOrClaims }
64
+ : agentClientIdOrClaims
65
+
66
+ const builder = new CreateConversationOptionsBuilder(claims, channelId, serviceUrl)
67
+ if (parameters) {
68
+ builder._parameters = { ...builder._parameters, ...parameters }
69
+ }
70
+
71
+ // Set parameters.agent if not already provided — matches C# behavior
72
+ if (!builder._parameters.agent) {
73
+ builder._parameters.agent = { id: claims.aud, role: RoleTypes.Agent }
74
+ }
75
+
76
+ return builder
77
+ }
78
+
79
+ /** Adds a member (the target user) to `parameters.members`. */
80
+ withUser (userId: string, userName?: string): this
81
+ withUser (account: ChannelAccount): this
82
+ withUser (userIdOrAccount: string | ChannelAccount, userName?: string): this {
83
+ const account: ChannelAccount =
84
+ typeof userIdOrAccount === 'string'
85
+ ? { id: userIdOrAccount, name: userName }
86
+ : userIdOrAccount
87
+ const members = this._parameters.members ?? []
88
+ members.push(account)
89
+ this._parameters = { ...this._parameters, members }
90
+ return this
91
+ }
92
+
93
+ /** Sets `parameters.activity`. Defaults `activity.type` to `'message'` if not provided. */
94
+ withActivity (activity: Partial<Activity>): this {
95
+ this._activity = activity
96
+ return this
97
+ }
98
+
99
+ /** Merges additional channel-specific data into `parameters.channelData`. */
100
+ withChannelData (data: object): this {
101
+ this._parameters = {
102
+ ...this._parameters,
103
+ channelData: { ...(this._parameters.channelData as object ?? {}), ...data },
104
+ }
105
+ return this
106
+ }
107
+
108
+ /**
109
+ * Sets `parameters.tenantId`.
110
+ * On `msteams` channels, also sets `channelData.tenant.id`.
111
+ */
112
+ withTenantId (tenantId: string): this {
113
+ this._parameters = { ...this._parameters, tenantId }
114
+ if (this._channelId === Channels.Msteams) {
115
+ this.withChannelData({ tenant: { id: tenantId } })
116
+ }
117
+ return this
118
+ }
119
+
120
+ /**
121
+ * Sets `parameters.isGroup = true` and `channelData.channel.id`.
122
+ * Only has effect on `msteams` channels.
123
+ */
124
+ withTeamsChannelId (teamsChannelId: string): this {
125
+ if (this._channelId !== Channels.Msteams) return this
126
+ this._parameters = { ...this._parameters, isGroup: true }
127
+ this.withChannelData({ channel: { id: teamsChannelId } })
128
+ return this
129
+ }
130
+
131
+ /** Sets `parameters.topicName`. */
132
+ withTopicName (name: string): this {
133
+ this._parameters = { ...this._parameters, topicName: name }
134
+ return this
135
+ }
136
+
137
+ /** Sets `parameters.isGroup`. */
138
+ isGroup (value: boolean): this {
139
+ this._parameters = { ...this._parameters, isGroup: value }
140
+ return this
141
+ }
142
+
143
+ /** Overrides the default `AzureBotScope` OAuth scope. */
144
+ withScope (scope: string): this {
145
+ this._scope = scope
146
+ return this
147
+ }
148
+
149
+ /** Controls whether the resulting conversation is stored after creation. */
150
+ storeConversation (value: boolean): this {
151
+ this._storeConversation = value
152
+ return this
153
+ }
154
+
155
+ /**
156
+ * Builds and returns `CreateConversationOptions`.
157
+ * @throws if no members were added via `withUser()`.
158
+ */
159
+ build (): CreateConversationOptions {
160
+ if (!this._parameters.members?.length) {
161
+ throw ExceptionHelper.generateException(Error, Errors.CreateConversationBuilderMembersRequired)
162
+ }
163
+
164
+ const activity: Partial<Activity> = {
165
+ type: 'message',
166
+ ...this._activity,
167
+ }
168
+
169
+ return {
170
+ identity: this._claims,
171
+ channelId: this._channelId,
172
+ serviceUrl: this._serviceUrl,
173
+ scope: this._scope,
174
+ storeConversation: this._storeConversation,
175
+ parameters: {
176
+ ...this._parameters,
177
+ activity: activity as Activity,
178
+ },
179
+ }
180
+ }
181
+ }
@@ -0,0 +1,10 @@
1
+ // Copyright (c) Microsoft Corporation. All rights reserved.
2
+ // Licensed under the MIT License.
3
+
4
+ export * from './conversation'
5
+ export * from './conversationBuilder'
6
+ export * from './conversationReferenceBuilder'
7
+ export * from './createConversationOptions'
8
+ export * from './createConversationOptionsBuilder'
9
+ export * from './proactiveOptions'
10
+ export * from './proactive'