@openclaw/msteams 2026.5.2 → 2026.5.3-beta.1
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/dist/api.js +3 -0
- package/dist/channel-D7hdreTh.js +984 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-BC1ruIfN.js +573 -0
- package/dist/config-schema-B8QezH6t.js +15 -0
- package/dist/contract-api.js +2 -0
- package/dist/graph-users-9uQJepqr.js +1354 -0
- package/dist/index.js +22 -0
- package/dist/oauth-BWJyilR1.js +114 -0
- package/dist/oauth.token-xxpoLWy5.js +115 -0
- package/dist/policy-DTnU2GR7.js +142 -0
- package/dist/probe-D_H8yFps.js +2194 -0
- package/dist/resolve-allowlist-D41JSziq.js +219 -0
- package/dist/runtime-api-DV1iVMn1.js +28 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-BuoEXmPS.js +35 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-plugin-api.js +64 -0
- package/dist/setup-surface-BLkFQYIQ.js +313 -0
- package/dist/src-CFp1QpFd.js +4064 -0
- package/dist/test-api.js +2 -0
- package/package.json +14 -6
- package/api.ts +0 -3
- package/channel-config-api.ts +0 -1
- package/channel-plugin-api.ts +0 -2
- package/config-api.ts +0 -4
- package/contract-api.ts +0 -4
- package/index.ts +0 -20
- package/runtime-api.ts +0 -73
- package/secret-contract-api.ts +0 -5
- package/setup-entry.ts +0 -13
- package/setup-plugin-api.ts +0 -3
- package/src/ai-entity.ts +0 -7
- package/src/approval-auth.ts +0 -44
- package/src/attachments/bot-framework.test.ts +0 -461
- package/src/attachments/bot-framework.ts +0 -362
- package/src/attachments/download.ts +0 -311
- package/src/attachments/graph.test.ts +0 -416
- package/src/attachments/graph.ts +0 -484
- package/src/attachments/html.ts +0 -122
- package/src/attachments/payload.ts +0 -14
- package/src/attachments/remote-media.test.ts +0 -137
- package/src/attachments/remote-media.ts +0 -112
- package/src/attachments/shared.test.ts +0 -530
- package/src/attachments/shared.ts +0 -626
- package/src/attachments/types.ts +0 -47
- package/src/attachments.graph.test.ts +0 -342
- package/src/attachments.helpers.test.ts +0 -246
- package/src/attachments.test-helpers.ts +0 -17
- package/src/attachments.test.ts +0 -687
- package/src/attachments.ts +0 -18
- package/src/block-streaming-config.test.ts +0 -61
- package/src/channel-api.ts +0 -1
- package/src/channel.actions.test.ts +0 -742
- package/src/channel.directory.test.ts +0 -200
- package/src/channel.runtime.ts +0 -56
- package/src/channel.setup.ts +0 -77
- package/src/channel.test.ts +0 -128
- package/src/channel.ts +0 -1136
- package/src/config-schema.ts +0 -6
- package/src/config-ui-hints.ts +0 -12
- package/src/conversation-store-fs.test.ts +0 -74
- package/src/conversation-store-fs.ts +0 -149
- package/src/conversation-store-helpers.test.ts +0 -202
- package/src/conversation-store-helpers.ts +0 -105
- package/src/conversation-store-memory.ts +0 -51
- package/src/conversation-store.shared.test.ts +0 -225
- package/src/conversation-store.ts +0 -71
- package/src/directory-live.test.ts +0 -156
- package/src/directory-live.ts +0 -111
- package/src/doctor.ts +0 -27
- package/src/errors.test.ts +0 -133
- package/src/errors.ts +0 -246
- package/src/feedback-reflection-prompt.ts +0 -117
- package/src/feedback-reflection-store.ts +0 -114
- package/src/feedback-reflection.test.ts +0 -237
- package/src/feedback-reflection.ts +0 -283
- package/src/file-consent-helpers.test.ts +0 -326
- package/src/file-consent-helpers.ts +0 -126
- package/src/file-consent-invoke.ts +0 -150
- package/src/file-consent.test.ts +0 -363
- package/src/file-consent.ts +0 -287
- package/src/graph-chat.ts +0 -55
- package/src/graph-group-management.test.ts +0 -318
- package/src/graph-group-management.ts +0 -168
- package/src/graph-members.test.ts +0 -89
- package/src/graph-members.ts +0 -48
- package/src/graph-messages.actions.test.ts +0 -243
- package/src/graph-messages.read.test.ts +0 -391
- package/src/graph-messages.search.test.ts +0 -213
- package/src/graph-messages.test-helpers.ts +0 -50
- package/src/graph-messages.ts +0 -534
- package/src/graph-teams.test.ts +0 -215
- package/src/graph-teams.ts +0 -114
- package/src/graph-thread.test.ts +0 -246
- package/src/graph-thread.ts +0 -146
- package/src/graph-upload.test.ts +0 -258
- package/src/graph-upload.ts +0 -531
- package/src/graph-users.ts +0 -29
- package/src/graph.test.ts +0 -516
- package/src/graph.ts +0 -293
- package/src/inbound.test.ts +0 -221
- package/src/inbound.ts +0 -148
- package/src/index.ts +0 -4
- package/src/media-helpers.test.ts +0 -202
- package/src/media-helpers.ts +0 -105
- package/src/mentions.test.ts +0 -244
- package/src/mentions.ts +0 -114
- package/src/messenger.test.ts +0 -865
- package/src/messenger.ts +0 -605
- package/src/monitor-handler/access.ts +0 -125
- package/src/monitor-handler/inbound-media.test.ts +0 -289
- package/src/monitor-handler/inbound-media.ts +0 -180
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
- package/src/monitor-handler/message-handler.authz.test.ts +0 -669
- package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
- package/src/monitor-handler/message-handler.test-support.ts +0 -100
- package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
- package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
- package/src/monitor-handler/message-handler.ts +0 -1000
- package/src/monitor-handler/reaction-handler.test.ts +0 -267
- package/src/monitor-handler/reaction-handler.ts +0 -210
- package/src/monitor-handler/thread-session.ts +0 -17
- package/src/monitor-handler.adaptive-card.test.ts +0 -162
- package/src/monitor-handler.feedback-authz.test.ts +0 -314
- package/src/monitor-handler.file-consent.test.ts +0 -423
- package/src/monitor-handler.sso.test.ts +0 -563
- package/src/monitor-handler.test-helpers.ts +0 -180
- package/src/monitor-handler.ts +0 -534
- package/src/monitor-handler.types.ts +0 -27
- package/src/monitor-types.ts +0 -6
- package/src/monitor.lifecycle.test.ts +0 -278
- package/src/monitor.test.ts +0 -119
- package/src/monitor.ts +0 -442
- package/src/oauth.flow.ts +0 -77
- package/src/oauth.shared.ts +0 -37
- package/src/oauth.test.ts +0 -305
- package/src/oauth.token.ts +0 -158
- package/src/oauth.ts +0 -130
- package/src/outbound.test.ts +0 -130
- package/src/outbound.ts +0 -71
- package/src/pending-uploads-fs.test.ts +0 -246
- package/src/pending-uploads-fs.ts +0 -235
- package/src/pending-uploads.test.ts +0 -173
- package/src/pending-uploads.ts +0 -121
- package/src/policy.test.ts +0 -240
- package/src/policy.ts +0 -262
- package/src/polls-store-memory.ts +0 -32
- package/src/polls.test.ts +0 -160
- package/src/polls.ts +0 -323
- package/src/presentation.ts +0 -68
- package/src/probe.test.ts +0 -77
- package/src/probe.ts +0 -132
- package/src/reply-dispatcher.test.ts +0 -437
- package/src/reply-dispatcher.ts +0 -346
- package/src/reply-stream-controller.test.ts +0 -235
- package/src/reply-stream-controller.ts +0 -147
- package/src/resolve-allowlist.test.ts +0 -250
- package/src/resolve-allowlist.ts +0 -309
- package/src/revoked-context.ts +0 -17
- package/src/runtime.ts +0 -9
- package/src/sdk-types.ts +0 -59
- package/src/sdk.test.ts +0 -666
- package/src/sdk.ts +0 -884
- package/src/secret-contract.ts +0 -49
- package/src/secret-input.ts +0 -7
- package/src/send-context.ts +0 -231
- package/src/send.test.ts +0 -493
- package/src/send.ts +0 -637
- package/src/sent-message-cache.test.ts +0 -15
- package/src/sent-message-cache.ts +0 -56
- package/src/session-route.ts +0 -40
- package/src/setup-core.ts +0 -160
- package/src/setup-surface.test.ts +0 -202
- package/src/setup-surface.ts +0 -320
- package/src/sso-token-store.test.ts +0 -72
- package/src/sso-token-store.ts +0 -166
- package/src/sso.ts +0 -300
- package/src/storage.ts +0 -25
- package/src/store-fs.ts +0 -44
- package/src/streaming-message.test.ts +0 -262
- package/src/streaming-message.ts +0 -297
- package/src/test-runtime.ts +0 -16
- package/src/thread-parent-context.test.ts +0 -224
- package/src/thread-parent-context.ts +0 -159
- package/src/token-response.ts +0 -11
- package/src/token.test.ts +0 -259
- package/src/token.ts +0 -195
- package/src/user-agent.test.ts +0 -86
- package/src/user-agent.ts +0 -53
- package/src/webhook-timeouts.ts +0 -27
- package/src/welcome-card.test.ts +0 -81
- package/src/welcome-card.ts +0 -57
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/src/sso.ts
DELETED
|
@@ -1,300 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Bot Framework OAuth SSO invoke handlers for Microsoft Teams.
|
|
3
|
-
*
|
|
4
|
-
* Handles two invoke activities Teams sends when the bot has presented
|
|
5
|
-
* an `oauthCard` or when the user completes an interactive sign-in:
|
|
6
|
-
*
|
|
7
|
-
* 1. `signin/tokenExchange`
|
|
8
|
-
* The Teams client obtained an exchangeable token from the bot's
|
|
9
|
-
* AAD app and forwards it to the bot. The bot exchanges that token
|
|
10
|
-
* with the Bot Framework User Token service, which returns the real
|
|
11
|
-
* delegated user token (for example, a Microsoft Graph access token
|
|
12
|
-
* if the OAuth connection is set up for Graph).
|
|
13
|
-
*
|
|
14
|
-
* 2. `signin/verifyState`
|
|
15
|
-
* Fallback for the magic-code flow: the user finishes sign-in in a
|
|
16
|
-
* browser tab, receives a 6-digit code, and pastes it back into the
|
|
17
|
-
* chat. The bot then asks the User Token service for the token
|
|
18
|
-
* corresponding to that code.
|
|
19
|
-
*
|
|
20
|
-
* In both cases the bot must reply with an `invokeResponse` (HTTP 200)
|
|
21
|
-
* immediately or the Teams UI shows "Something went wrong". Callers of
|
|
22
|
-
* {@link handleSigninTokenExchangeInvoke} and
|
|
23
|
-
* {@link handleSigninVerifyStateInvoke} are responsible for sending
|
|
24
|
-
* that ack; these helpers encapsulate token exchange and persistence.
|
|
25
|
-
*/
|
|
26
|
-
|
|
27
|
-
import type { MSTeamsAccessTokenProvider } from "./attachments/types.js";
|
|
28
|
-
import type { MSTeamsSsoTokenStore } from "./sso-token-store.js";
|
|
29
|
-
import { buildUserAgent } from "./user-agent.js";
|
|
30
|
-
|
|
31
|
-
/** Scope used to obtain a Bot Framework service token. */
|
|
32
|
-
const BOT_FRAMEWORK_TOKEN_SCOPE = "https://api.botframework.com/.default";
|
|
33
|
-
|
|
34
|
-
/** Bot Framework User Token service base URL. */
|
|
35
|
-
const BOT_FRAMEWORK_USER_TOKEN_BASE_URL = "https://token.botframework.com";
|
|
36
|
-
|
|
37
|
-
/**
|
|
38
|
-
* Response shape returned by the Bot Framework User Token service for
|
|
39
|
-
* `GetUserToken` and `ExchangeToken`.
|
|
40
|
-
*
|
|
41
|
-
* @see https://learn.microsoft.com/azure/bot-service/rest-api/bot-framework-rest-connector-user-token-service
|
|
42
|
-
*/
|
|
43
|
-
type BotFrameworkUserTokenResponse = {
|
|
44
|
-
channelId?: string;
|
|
45
|
-
connectionName: string;
|
|
46
|
-
token: string;
|
|
47
|
-
expiration?: string;
|
|
48
|
-
};
|
|
49
|
-
|
|
50
|
-
export type MSTeamsSsoFetch = (
|
|
51
|
-
input: string,
|
|
52
|
-
init?: {
|
|
53
|
-
method?: string;
|
|
54
|
-
headers?: Record<string, string>;
|
|
55
|
-
body?: string;
|
|
56
|
-
},
|
|
57
|
-
) => Promise<{
|
|
58
|
-
ok: boolean;
|
|
59
|
-
status: number;
|
|
60
|
-
json(): Promise<unknown>;
|
|
61
|
-
text(): Promise<string>;
|
|
62
|
-
}>;
|
|
63
|
-
|
|
64
|
-
export type MSTeamsSsoDeps = {
|
|
65
|
-
tokenProvider: MSTeamsAccessTokenProvider;
|
|
66
|
-
tokenStore: MSTeamsSsoTokenStore;
|
|
67
|
-
connectionName: string;
|
|
68
|
-
/** Override `fetch` for testing. */
|
|
69
|
-
fetchImpl?: MSTeamsSsoFetch;
|
|
70
|
-
/** Override the User Token service base URL (testing / sovereign clouds). */
|
|
71
|
-
userTokenBaseUrl?: string;
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
type MSTeamsSsoUser = {
|
|
75
|
-
/** Stable user identifier — AAD object ID when available. */
|
|
76
|
-
userId: string;
|
|
77
|
-
/** Bot Framework channel ID (default: "msteams"). */
|
|
78
|
-
channelId?: string;
|
|
79
|
-
};
|
|
80
|
-
|
|
81
|
-
type MSTeamsSsoResult =
|
|
82
|
-
| {
|
|
83
|
-
ok: true;
|
|
84
|
-
token: string;
|
|
85
|
-
expiresAt?: string;
|
|
86
|
-
}
|
|
87
|
-
| {
|
|
88
|
-
ok: false;
|
|
89
|
-
code:
|
|
90
|
-
| "missing_user"
|
|
91
|
-
| "missing_connection"
|
|
92
|
-
| "missing_token"
|
|
93
|
-
| "missing_state"
|
|
94
|
-
| "service_error"
|
|
95
|
-
| "unexpected_response";
|
|
96
|
-
message: string;
|
|
97
|
-
status?: number;
|
|
98
|
-
};
|
|
99
|
-
|
|
100
|
-
type SigninTokenExchangeValue = {
|
|
101
|
-
id?: string;
|
|
102
|
-
connectionName?: string;
|
|
103
|
-
token?: string;
|
|
104
|
-
};
|
|
105
|
-
|
|
106
|
-
type SigninVerifyStateValue = {
|
|
107
|
-
state?: string;
|
|
108
|
-
};
|
|
109
|
-
|
|
110
|
-
/**
|
|
111
|
-
* Extract and validate the `signin/tokenExchange` activity value. Teams
|
|
112
|
-
* delivers `{ id, connectionName, token }`; any field may be missing on
|
|
113
|
-
* malformed invocations, so callers should check the parsed result.
|
|
114
|
-
*/
|
|
115
|
-
export function parseSigninTokenExchangeValue(value: unknown): SigninTokenExchangeValue | null {
|
|
116
|
-
if (!value || typeof value !== "object") {
|
|
117
|
-
return null;
|
|
118
|
-
}
|
|
119
|
-
const obj = value as Record<string, unknown>;
|
|
120
|
-
const id = typeof obj.id === "string" ? obj.id : undefined;
|
|
121
|
-
const connectionName = typeof obj.connectionName === "string" ? obj.connectionName : undefined;
|
|
122
|
-
const token = typeof obj.token === "string" ? obj.token : undefined;
|
|
123
|
-
return { id, connectionName, token };
|
|
124
|
-
}
|
|
125
|
-
|
|
126
|
-
/** Extract the `signin/verifyState` activity value `{ state }`. */
|
|
127
|
-
export function parseSigninVerifyStateValue(value: unknown): SigninVerifyStateValue | null {
|
|
128
|
-
if (!value || typeof value !== "object") {
|
|
129
|
-
return null;
|
|
130
|
-
}
|
|
131
|
-
const obj = value as Record<string, unknown>;
|
|
132
|
-
const state = typeof obj.state === "string" ? obj.state : undefined;
|
|
133
|
-
return { state };
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
type UserTokenServiceCallParams = {
|
|
137
|
-
baseUrl: string;
|
|
138
|
-
path: string;
|
|
139
|
-
query: Record<string, string>;
|
|
140
|
-
method: "GET" | "POST";
|
|
141
|
-
body?: unknown;
|
|
142
|
-
bearerToken: string;
|
|
143
|
-
fetchImpl: MSTeamsSsoFetch;
|
|
144
|
-
};
|
|
145
|
-
|
|
146
|
-
async function callUserTokenService(
|
|
147
|
-
params: UserTokenServiceCallParams,
|
|
148
|
-
): Promise<BotFrameworkUserTokenResponse | { error: string; status: number }> {
|
|
149
|
-
const qs = new URLSearchParams(params.query).toString();
|
|
150
|
-
const url = `${params.baseUrl.replace(/\/+$/, "")}${params.path}?${qs}`;
|
|
151
|
-
const headers: Record<string, string> = {
|
|
152
|
-
Accept: "application/json",
|
|
153
|
-
Authorization: `Bearer ${params.bearerToken}`,
|
|
154
|
-
"User-Agent": buildUserAgent(),
|
|
155
|
-
};
|
|
156
|
-
if (params.body !== undefined) {
|
|
157
|
-
headers["Content-Type"] = "application/json";
|
|
158
|
-
}
|
|
159
|
-
const response = await params.fetchImpl(url, {
|
|
160
|
-
method: params.method,
|
|
161
|
-
headers,
|
|
162
|
-
body: params.body === undefined ? undefined : JSON.stringify(params.body),
|
|
163
|
-
});
|
|
164
|
-
if (!response.ok) {
|
|
165
|
-
const text = await response.text().catch(() => "");
|
|
166
|
-
return { error: text || `HTTP ${response.status}`, status: response.status };
|
|
167
|
-
}
|
|
168
|
-
let parsed: unknown;
|
|
169
|
-
try {
|
|
170
|
-
parsed = await response.json();
|
|
171
|
-
} catch {
|
|
172
|
-
return { error: "invalid JSON from User Token service", status: response.status };
|
|
173
|
-
}
|
|
174
|
-
if (!parsed || typeof parsed !== "object") {
|
|
175
|
-
return { error: "empty response from User Token service", status: response.status };
|
|
176
|
-
}
|
|
177
|
-
const obj = parsed as Record<string, unknown>;
|
|
178
|
-
const token = typeof obj.token === "string" ? obj.token : undefined;
|
|
179
|
-
const connectionName = typeof obj.connectionName === "string" ? obj.connectionName : undefined;
|
|
180
|
-
const channelId = typeof obj.channelId === "string" ? obj.channelId : undefined;
|
|
181
|
-
const expiration = typeof obj.expiration === "string" ? obj.expiration : undefined;
|
|
182
|
-
if (!token || !connectionName) {
|
|
183
|
-
return { error: "User Token service response missing token/connectionName", status: 502 };
|
|
184
|
-
}
|
|
185
|
-
return { channelId, connectionName, token, expiration };
|
|
186
|
-
}
|
|
187
|
-
|
|
188
|
-
/**
|
|
189
|
-
* Exchange a Teams SSO token for a delegated user token via Bot
|
|
190
|
-
* Framework's User Token service, then persist the result.
|
|
191
|
-
*/
|
|
192
|
-
export async function handleSigninTokenExchangeInvoke(params: {
|
|
193
|
-
value: SigninTokenExchangeValue;
|
|
194
|
-
user: MSTeamsSsoUser;
|
|
195
|
-
deps: MSTeamsSsoDeps;
|
|
196
|
-
}): Promise<MSTeamsSsoResult> {
|
|
197
|
-
const { value, user, deps } = params;
|
|
198
|
-
if (!user.userId) {
|
|
199
|
-
return { ok: false, code: "missing_user", message: "no user id on invoke activity" };
|
|
200
|
-
}
|
|
201
|
-
const connectionName = value.connectionName?.trim() || deps.connectionName;
|
|
202
|
-
if (!connectionName) {
|
|
203
|
-
return { ok: false, code: "missing_connection", message: "no OAuth connection name" };
|
|
204
|
-
}
|
|
205
|
-
if (!value.token) {
|
|
206
|
-
return { ok: false, code: "missing_token", message: "no exchangeable token on invoke" };
|
|
207
|
-
}
|
|
208
|
-
|
|
209
|
-
const bearer = await deps.tokenProvider.getAccessToken(BOT_FRAMEWORK_TOKEN_SCOPE);
|
|
210
|
-
const fetchImpl = deps.fetchImpl ?? (globalThis.fetch as unknown as MSTeamsSsoFetch);
|
|
211
|
-
const result = await callUserTokenService({
|
|
212
|
-
baseUrl: deps.userTokenBaseUrl ?? BOT_FRAMEWORK_USER_TOKEN_BASE_URL,
|
|
213
|
-
path: "/api/usertoken/exchange",
|
|
214
|
-
query: {
|
|
215
|
-
userId: user.userId,
|
|
216
|
-
connectionName,
|
|
217
|
-
channelId: user.channelId ?? "msteams",
|
|
218
|
-
},
|
|
219
|
-
method: "POST",
|
|
220
|
-
body: { token: value.token },
|
|
221
|
-
bearerToken: bearer,
|
|
222
|
-
fetchImpl,
|
|
223
|
-
});
|
|
224
|
-
|
|
225
|
-
if ("error" in result) {
|
|
226
|
-
return {
|
|
227
|
-
ok: false,
|
|
228
|
-
code: result.status >= 500 ? "service_error" : "unexpected_response",
|
|
229
|
-
message: result.error,
|
|
230
|
-
status: result.status,
|
|
231
|
-
};
|
|
232
|
-
}
|
|
233
|
-
|
|
234
|
-
await deps.tokenStore.save({
|
|
235
|
-
connectionName,
|
|
236
|
-
userId: user.userId,
|
|
237
|
-
token: result.token,
|
|
238
|
-
expiresAt: result.expiration,
|
|
239
|
-
updatedAt: new Date().toISOString(),
|
|
240
|
-
});
|
|
241
|
-
|
|
242
|
-
return { ok: true, token: result.token, expiresAt: result.expiration };
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
/**
|
|
246
|
-
* Finish a magic-code sign-in: look up the user token for the state
|
|
247
|
-
* code via Bot Framework's User Token service, then persist it.
|
|
248
|
-
*/
|
|
249
|
-
export async function handleSigninVerifyStateInvoke(params: {
|
|
250
|
-
value: SigninVerifyStateValue;
|
|
251
|
-
user: MSTeamsSsoUser;
|
|
252
|
-
deps: MSTeamsSsoDeps;
|
|
253
|
-
}): Promise<MSTeamsSsoResult> {
|
|
254
|
-
const { value, user, deps } = params;
|
|
255
|
-
if (!user.userId) {
|
|
256
|
-
return { ok: false, code: "missing_user", message: "no user id on invoke activity" };
|
|
257
|
-
}
|
|
258
|
-
if (!deps.connectionName) {
|
|
259
|
-
return { ok: false, code: "missing_connection", message: "no OAuth connection name" };
|
|
260
|
-
}
|
|
261
|
-
const state = value.state?.trim();
|
|
262
|
-
if (!state) {
|
|
263
|
-
return { ok: false, code: "missing_state", message: "no state code on invoke" };
|
|
264
|
-
}
|
|
265
|
-
|
|
266
|
-
const bearer = await deps.tokenProvider.getAccessToken(BOT_FRAMEWORK_TOKEN_SCOPE);
|
|
267
|
-
const fetchImpl = deps.fetchImpl ?? (globalThis.fetch as unknown as MSTeamsSsoFetch);
|
|
268
|
-
const result = await callUserTokenService({
|
|
269
|
-
baseUrl: deps.userTokenBaseUrl ?? BOT_FRAMEWORK_USER_TOKEN_BASE_URL,
|
|
270
|
-
path: "/api/usertoken/GetToken",
|
|
271
|
-
query: {
|
|
272
|
-
userId: user.userId,
|
|
273
|
-
connectionName: deps.connectionName,
|
|
274
|
-
channelId: user.channelId ?? "msteams",
|
|
275
|
-
code: state,
|
|
276
|
-
},
|
|
277
|
-
method: "GET",
|
|
278
|
-
bearerToken: bearer,
|
|
279
|
-
fetchImpl,
|
|
280
|
-
});
|
|
281
|
-
|
|
282
|
-
if ("error" in result) {
|
|
283
|
-
return {
|
|
284
|
-
ok: false,
|
|
285
|
-
code: result.status >= 500 ? "service_error" : "unexpected_response",
|
|
286
|
-
message: result.error,
|
|
287
|
-
status: result.status,
|
|
288
|
-
};
|
|
289
|
-
}
|
|
290
|
-
|
|
291
|
-
await deps.tokenStore.save({
|
|
292
|
-
connectionName: deps.connectionName,
|
|
293
|
-
userId: user.userId,
|
|
294
|
-
token: result.token,
|
|
295
|
-
expiresAt: result.expiration,
|
|
296
|
-
updatedAt: new Date().toISOString(),
|
|
297
|
-
});
|
|
298
|
-
|
|
299
|
-
return { ok: true, token: result.token, expiresAt: result.expiration };
|
|
300
|
-
}
|
package/src/storage.ts
DELETED
|
@@ -1,25 +0,0 @@
|
|
|
1
|
-
import path from "node:path";
|
|
2
|
-
import { getMSTeamsRuntime } from "./runtime.js";
|
|
3
|
-
|
|
4
|
-
type MSTeamsStorePathOptions = {
|
|
5
|
-
env?: NodeJS.ProcessEnv;
|
|
6
|
-
homedir?: () => string;
|
|
7
|
-
stateDir?: string;
|
|
8
|
-
storePath?: string;
|
|
9
|
-
filename: string;
|
|
10
|
-
};
|
|
11
|
-
|
|
12
|
-
export function resolveMSTeamsStorePath(params: MSTeamsStorePathOptions): string {
|
|
13
|
-
if (params.storePath) {
|
|
14
|
-
return params.storePath;
|
|
15
|
-
}
|
|
16
|
-
if (params.stateDir) {
|
|
17
|
-
return path.join(params.stateDir, params.filename);
|
|
18
|
-
}
|
|
19
|
-
|
|
20
|
-
const env = params.env ?? process.env;
|
|
21
|
-
const stateDir = params.homedir
|
|
22
|
-
? getMSTeamsRuntime().state.resolveStateDir(env, params.homedir)
|
|
23
|
-
: getMSTeamsRuntime().state.resolveStateDir(env);
|
|
24
|
-
return path.join(stateDir, params.filename);
|
|
25
|
-
}
|
package/src/store-fs.ts
DELETED
|
@@ -1,44 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import { withFileLock as withPathLock } from "openclaw/plugin-sdk/file-lock";
|
|
3
|
-
import { readJsonFileWithFallback, writeJsonFileAtomically } from "openclaw/plugin-sdk/json-store";
|
|
4
|
-
|
|
5
|
-
const STORE_LOCK_OPTIONS = {
|
|
6
|
-
retries: {
|
|
7
|
-
retries: 10,
|
|
8
|
-
factor: 2,
|
|
9
|
-
minTimeout: 100,
|
|
10
|
-
maxTimeout: 10_000,
|
|
11
|
-
randomize: true,
|
|
12
|
-
},
|
|
13
|
-
stale: 30_000,
|
|
14
|
-
} as const;
|
|
15
|
-
|
|
16
|
-
export async function readJsonFile<T>(
|
|
17
|
-
filePath: string,
|
|
18
|
-
fallback: T,
|
|
19
|
-
): Promise<{ value: T; exists: boolean }> {
|
|
20
|
-
return await readJsonFileWithFallback(filePath, fallback);
|
|
21
|
-
}
|
|
22
|
-
|
|
23
|
-
export async function writeJsonFile(filePath: string, value: unknown): Promise<void> {
|
|
24
|
-
await writeJsonFileAtomically(filePath, value);
|
|
25
|
-
}
|
|
26
|
-
|
|
27
|
-
async function ensureJsonFile(filePath: string, fallback: unknown) {
|
|
28
|
-
try {
|
|
29
|
-
await fs.promises.access(filePath);
|
|
30
|
-
} catch {
|
|
31
|
-
await writeJsonFile(filePath, fallback);
|
|
32
|
-
}
|
|
33
|
-
}
|
|
34
|
-
|
|
35
|
-
export async function withFileLock<T>(
|
|
36
|
-
filePath: string,
|
|
37
|
-
fallback: unknown,
|
|
38
|
-
fn: () => Promise<T>,
|
|
39
|
-
): Promise<T> {
|
|
40
|
-
await ensureJsonFile(filePath, fallback);
|
|
41
|
-
return await withPathLock(filePath, STORE_LOCK_OPTIONS, async () => {
|
|
42
|
-
return await fn();
|
|
43
|
-
});
|
|
44
|
-
}
|
|
@@ -1,262 +0,0 @@
|
|
|
1
|
-
import { afterEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import { TeamsHttpStream } from "./streaming-message.js";
|
|
3
|
-
|
|
4
|
-
async function flushStreamTimer(): Promise<void> {
|
|
5
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
6
|
-
}
|
|
7
|
-
|
|
8
|
-
describe("TeamsHttpStream", () => {
|
|
9
|
-
afterEach(() => {
|
|
10
|
-
vi.useRealTimers();
|
|
11
|
-
});
|
|
12
|
-
|
|
13
|
-
it("sends first chunk as typing activity with streaminfo", async () => {
|
|
14
|
-
vi.useFakeTimers();
|
|
15
|
-
|
|
16
|
-
const sent: unknown[] = [];
|
|
17
|
-
const stream = new TeamsHttpStream({
|
|
18
|
-
sendActivity: vi.fn(async (activity) => {
|
|
19
|
-
sent.push(activity);
|
|
20
|
-
return { id: "stream-1" };
|
|
21
|
-
}),
|
|
22
|
-
throttleMs: 1,
|
|
23
|
-
});
|
|
24
|
-
|
|
25
|
-
// Enough text to pass MIN_INITIAL_CHARS threshold
|
|
26
|
-
stream.update("Hello, this is a test response that is long enough.");
|
|
27
|
-
await flushStreamTimer();
|
|
28
|
-
|
|
29
|
-
expect(sent.length).toBeGreaterThanOrEqual(1);
|
|
30
|
-
const firstActivity = sent[0] as Record<string, unknown>;
|
|
31
|
-
expect(firstActivity.type).toBe("typing");
|
|
32
|
-
expect(typeof firstActivity.text).toBe("string");
|
|
33
|
-
expect(firstActivity.text as string).toContain("Hello");
|
|
34
|
-
// Should have streaminfo entity
|
|
35
|
-
const entities = firstActivity.entities as Array<Record<string, unknown>>;
|
|
36
|
-
expect(entities).toEqual(
|
|
37
|
-
expect.arrayContaining([
|
|
38
|
-
expect.objectContaining({ type: "streaminfo", streamType: "streaming" }),
|
|
39
|
-
]),
|
|
40
|
-
);
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("sends final message activity on finalize", async () => {
|
|
44
|
-
vi.useFakeTimers();
|
|
45
|
-
|
|
46
|
-
const sent: unknown[] = [];
|
|
47
|
-
const stream = new TeamsHttpStream({
|
|
48
|
-
sendActivity: vi.fn(async (activity) => {
|
|
49
|
-
sent.push(activity);
|
|
50
|
-
return { id: "stream-1" };
|
|
51
|
-
}),
|
|
52
|
-
throttleMs: 1,
|
|
53
|
-
});
|
|
54
|
-
|
|
55
|
-
stream.update("Hello, this is a complete response for finalization testing.");
|
|
56
|
-
await flushStreamTimer();
|
|
57
|
-
|
|
58
|
-
await stream.finalize();
|
|
59
|
-
|
|
60
|
-
// Find the final message activity
|
|
61
|
-
const finalActivity = sent.find((a) => (a as Record<string, unknown>).type === "message") as
|
|
62
|
-
| Record<string, unknown>
|
|
63
|
-
| undefined;
|
|
64
|
-
|
|
65
|
-
expect(finalActivity).toBeDefined();
|
|
66
|
-
expect(finalActivity!.text).toBe(
|
|
67
|
-
"Hello, this is a complete response for finalization testing.",
|
|
68
|
-
);
|
|
69
|
-
// No cursor in final
|
|
70
|
-
expect(finalActivity!.text as string).not.toContain("\u258D");
|
|
71
|
-
|
|
72
|
-
// Should have AI-generated entity
|
|
73
|
-
const entities = finalActivity!.entities as Array<Record<string, unknown>>;
|
|
74
|
-
expect(entities).toEqual(
|
|
75
|
-
expect.arrayContaining([expect.objectContaining({ additionalType: ["AIGeneratedContent"] })]),
|
|
76
|
-
);
|
|
77
|
-
|
|
78
|
-
// Should have streaminfo with final type
|
|
79
|
-
expect(entities).toEqual(
|
|
80
|
-
expect.arrayContaining([
|
|
81
|
-
expect.objectContaining({ type: "streaminfo", streamType: "final" }),
|
|
82
|
-
]),
|
|
83
|
-
);
|
|
84
|
-
});
|
|
85
|
-
|
|
86
|
-
it("does not send below MIN_INITIAL_CHARS", async () => {
|
|
87
|
-
vi.useFakeTimers();
|
|
88
|
-
|
|
89
|
-
const sendActivity = vi.fn(async () => ({ id: "x" }));
|
|
90
|
-
const stream = new TeamsHttpStream({ sendActivity, throttleMs: 1 });
|
|
91
|
-
|
|
92
|
-
stream.update("Hi");
|
|
93
|
-
await flushStreamTimer();
|
|
94
|
-
|
|
95
|
-
expect(sendActivity).not.toHaveBeenCalled();
|
|
96
|
-
});
|
|
97
|
-
|
|
98
|
-
it("finalize with no content does nothing", async () => {
|
|
99
|
-
const sendActivity = vi.fn(async () => ({ id: "x" }));
|
|
100
|
-
const stream = new TeamsHttpStream({ sendActivity });
|
|
101
|
-
|
|
102
|
-
await stream.finalize();
|
|
103
|
-
expect(sendActivity).not.toHaveBeenCalled();
|
|
104
|
-
});
|
|
105
|
-
|
|
106
|
-
it("finalize sends content even if no chunks were streamed", async () => {
|
|
107
|
-
const sent: unknown[] = [];
|
|
108
|
-
const stream = new TeamsHttpStream({
|
|
109
|
-
sendActivity: vi.fn(async (activity) => {
|
|
110
|
-
sent.push(activity);
|
|
111
|
-
return { id: "msg-1" };
|
|
112
|
-
}),
|
|
113
|
-
});
|
|
114
|
-
|
|
115
|
-
// Short text — below MIN_INITIAL_CHARS, so no streaming chunk sent
|
|
116
|
-
stream.update("Short");
|
|
117
|
-
await stream.finalize();
|
|
118
|
-
|
|
119
|
-
// Should send final message even though no chunks were streamed
|
|
120
|
-
expect(sent.length).toBe(1);
|
|
121
|
-
const activity = sent[0] as Record<string, unknown>;
|
|
122
|
-
expect(activity.type).toBe("message");
|
|
123
|
-
expect(activity.text).toBe("Short");
|
|
124
|
-
});
|
|
125
|
-
|
|
126
|
-
it("sets feedbackLoopEnabled on final message", async () => {
|
|
127
|
-
vi.useFakeTimers();
|
|
128
|
-
|
|
129
|
-
const sent: unknown[] = [];
|
|
130
|
-
const stream = new TeamsHttpStream({
|
|
131
|
-
sendActivity: vi.fn(async (activity) => {
|
|
132
|
-
sent.push(activity);
|
|
133
|
-
return { id: "stream-1" };
|
|
134
|
-
}),
|
|
135
|
-
feedbackLoopEnabled: true,
|
|
136
|
-
throttleMs: 1,
|
|
137
|
-
});
|
|
138
|
-
|
|
139
|
-
stream.update("A response long enough to pass the minimum character threshold for streaming.");
|
|
140
|
-
await flushStreamTimer();
|
|
141
|
-
await stream.finalize();
|
|
142
|
-
|
|
143
|
-
const finalActivity = sent.find(
|
|
144
|
-
(a) => (a as Record<string, unknown>).type === "message",
|
|
145
|
-
) as Record<string, unknown>;
|
|
146
|
-
|
|
147
|
-
const channelData = finalActivity.channelData as Record<string, unknown>;
|
|
148
|
-
expect(channelData.feedbackLoopEnabled).toBe(true);
|
|
149
|
-
});
|
|
150
|
-
|
|
151
|
-
it("sends informative update with streamType informative", async () => {
|
|
152
|
-
const sent: unknown[] = [];
|
|
153
|
-
const stream = new TeamsHttpStream({
|
|
154
|
-
sendActivity: vi.fn(async (activity) => {
|
|
155
|
-
sent.push(activity);
|
|
156
|
-
return { id: "stream-1" };
|
|
157
|
-
}),
|
|
158
|
-
});
|
|
159
|
-
|
|
160
|
-
await stream.sendInformativeUpdate("Thinking...");
|
|
161
|
-
|
|
162
|
-
expect(sent.length).toBe(1);
|
|
163
|
-
const activity = sent[0] as Record<string, unknown>;
|
|
164
|
-
expect(activity.type).toBe("typing");
|
|
165
|
-
expect(activity.text).toBe("Thinking...");
|
|
166
|
-
const entities = activity.entities as Array<Record<string, unknown>>;
|
|
167
|
-
expect(entities).toEqual(
|
|
168
|
-
expect.arrayContaining([
|
|
169
|
-
expect.objectContaining({
|
|
170
|
-
type: "streaminfo",
|
|
171
|
-
streamType: "informative",
|
|
172
|
-
streamSequence: 1,
|
|
173
|
-
}),
|
|
174
|
-
]),
|
|
175
|
-
);
|
|
176
|
-
});
|
|
177
|
-
|
|
178
|
-
it("informative update establishes streamId for subsequent chunks", async () => {
|
|
179
|
-
vi.useFakeTimers();
|
|
180
|
-
|
|
181
|
-
const sent: unknown[] = [];
|
|
182
|
-
const stream = new TeamsHttpStream({
|
|
183
|
-
sendActivity: vi.fn(async (activity) => {
|
|
184
|
-
sent.push(activity);
|
|
185
|
-
return { id: "stream-1" };
|
|
186
|
-
}),
|
|
187
|
-
throttleMs: 1,
|
|
188
|
-
});
|
|
189
|
-
|
|
190
|
-
await stream.sendInformativeUpdate("Working...");
|
|
191
|
-
stream.update("Hello, this is a long enough response for streaming to begin.");
|
|
192
|
-
await flushStreamTimer();
|
|
193
|
-
|
|
194
|
-
// Second activity (streaming chunk) should have the streamId from the informative update
|
|
195
|
-
expect(sent.length).toBeGreaterThanOrEqual(2);
|
|
196
|
-
const chunk = sent[1] as Record<string, unknown>;
|
|
197
|
-
const entities = chunk.entities as Array<Record<string, unknown>>;
|
|
198
|
-
expect(entities).toEqual(
|
|
199
|
-
expect.arrayContaining([
|
|
200
|
-
expect.objectContaining({ type: "streaminfo", streamId: "stream-1" }),
|
|
201
|
-
]),
|
|
202
|
-
);
|
|
203
|
-
});
|
|
204
|
-
|
|
205
|
-
it("hasContent is true after update", () => {
|
|
206
|
-
const stream = new TeamsHttpStream({
|
|
207
|
-
sendActivity: vi.fn(async () => ({ id: "x" })),
|
|
208
|
-
});
|
|
209
|
-
|
|
210
|
-
expect(stream.hasContent).toBe(false);
|
|
211
|
-
stream.update("some text");
|
|
212
|
-
expect(stream.hasContent).toBe(true);
|
|
213
|
-
});
|
|
214
|
-
|
|
215
|
-
it("double finalize is a no-op", async () => {
|
|
216
|
-
const sendActivity = vi.fn(async () => ({ id: "x" }));
|
|
217
|
-
const stream = new TeamsHttpStream({ sendActivity });
|
|
218
|
-
|
|
219
|
-
stream.update("A response long enough to pass the minimum character threshold.");
|
|
220
|
-
await stream.finalize();
|
|
221
|
-
const callCount = sendActivity.mock.calls.length;
|
|
222
|
-
|
|
223
|
-
await stream.finalize();
|
|
224
|
-
expect(sendActivity.mock.calls.length).toBe(callCount);
|
|
225
|
-
});
|
|
226
|
-
|
|
227
|
-
it("stops streaming before stream age timeout and finalizes with last good text", async () => {
|
|
228
|
-
vi.useFakeTimers();
|
|
229
|
-
|
|
230
|
-
const sent: unknown[] = [];
|
|
231
|
-
const sendActivity = vi.fn(async (activity) => {
|
|
232
|
-
sent.push(activity);
|
|
233
|
-
return { id: "stream-1" };
|
|
234
|
-
});
|
|
235
|
-
const stream = new TeamsHttpStream({ sendActivity, throttleMs: 1 });
|
|
236
|
-
|
|
237
|
-
stream.update("Hello, this is a long enough response for streaming to begin.");
|
|
238
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
239
|
-
|
|
240
|
-
stream.update(
|
|
241
|
-
"Hello, this is a long enough response for streaming to begin. More text before timeout.",
|
|
242
|
-
);
|
|
243
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
244
|
-
|
|
245
|
-
vi.setSystemTime(new Date(Date.now() + 45_001));
|
|
246
|
-
stream.update(
|
|
247
|
-
"Hello, this is a long enough response for streaming to begin. More text before timeout. Even more text after timeout.",
|
|
248
|
-
);
|
|
249
|
-
await vi.advanceTimersByTimeAsync(1);
|
|
250
|
-
|
|
251
|
-
expect(stream.isFailed).toBe(true);
|
|
252
|
-
|
|
253
|
-
const finalActivity = sent.find((a) => (a as Record<string, unknown>).type === "message") as
|
|
254
|
-
| Record<string, unknown>
|
|
255
|
-
| undefined;
|
|
256
|
-
|
|
257
|
-
expect(finalActivity).toBeDefined();
|
|
258
|
-
expect(finalActivity!.text).toBe(
|
|
259
|
-
"Hello, this is a long enough response for streaming to begin. More text before timeout.",
|
|
260
|
-
);
|
|
261
|
-
});
|
|
262
|
-
});
|