@gakr-gakr/msteams 0.1.0

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 (107) hide show
  1. package/api.ts +3 -0
  2. package/autobot.plugin.json +15 -0
  3. package/channel-config-api.ts +1 -0
  4. package/channel-plugin-api.ts +2 -0
  5. package/config-api.ts +4 -0
  6. package/contract-api.ts +4 -0
  7. package/index.ts +20 -0
  8. package/package.json +72 -0
  9. package/runtime-api.ts +66 -0
  10. package/secret-contract-api.ts +5 -0
  11. package/setup-entry.ts +13 -0
  12. package/setup-plugin-api.ts +3 -0
  13. package/src/ai-entity.ts +7 -0
  14. package/src/approval-auth.ts +44 -0
  15. package/src/attachments/bot-framework.ts +348 -0
  16. package/src/attachments/download.ts +328 -0
  17. package/src/attachments/graph.ts +489 -0
  18. package/src/attachments/html.ts +122 -0
  19. package/src/attachments/payload.ts +14 -0
  20. package/src/attachments/remote-media.ts +86 -0
  21. package/src/attachments/shared.ts +655 -0
  22. package/src/attachments/types.ts +47 -0
  23. package/src/attachments.ts +18 -0
  24. package/src/channel-api.ts +1 -0
  25. package/src/channel.runtime.ts +56 -0
  26. package/src/channel.setup.ts +77 -0
  27. package/src/channel.ts +1176 -0
  28. package/src/config-schema.ts +6 -0
  29. package/src/config-ui-hints.ts +40 -0
  30. package/src/conversation-store-fs.ts +149 -0
  31. package/src/conversation-store-helpers.ts +105 -0
  32. package/src/conversation-store-memory.ts +51 -0
  33. package/src/conversation-store.ts +71 -0
  34. package/src/directory-live.ts +111 -0
  35. package/src/doctor.ts +27 -0
  36. package/src/errors.ts +270 -0
  37. package/src/feedback-reflection-prompt.ts +117 -0
  38. package/src/feedback-reflection-store.ts +113 -0
  39. package/src/feedback-reflection.ts +271 -0
  40. package/src/file-consent-helpers.ts +115 -0
  41. package/src/file-consent-invoke.ts +150 -0
  42. package/src/file-consent.ts +223 -0
  43. package/src/graph-chat.ts +36 -0
  44. package/src/graph-group-management.ts +168 -0
  45. package/src/graph-members.ts +48 -0
  46. package/src/graph-messages.ts +534 -0
  47. package/src/graph-teams.ts +114 -0
  48. package/src/graph-thread.ts +146 -0
  49. package/src/graph-upload.ts +531 -0
  50. package/src/graph-users.ts +29 -0
  51. package/src/graph.ts +308 -0
  52. package/src/inbound.ts +148 -0
  53. package/src/index.ts +4 -0
  54. package/src/media-helpers.ts +105 -0
  55. package/src/mentions.ts +114 -0
  56. package/src/messenger.ts +608 -0
  57. package/src/monitor-handler/access.ts +136 -0
  58. package/src/monitor-handler/inbound-media.ts +180 -0
  59. package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
  60. package/src/monitor-handler/message-handler.test-support.ts +102 -0
  61. package/src/monitor-handler/message-handler.ts +1015 -0
  62. package/src/monitor-handler/reaction-handler.ts +124 -0
  63. package/src/monitor-handler/thread-session.ts +30 -0
  64. package/src/monitor-handler.ts +538 -0
  65. package/src/monitor-handler.types.ts +27 -0
  66. package/src/monitor-types.ts +6 -0
  67. package/src/monitor.ts +476 -0
  68. package/src/oauth.flow.ts +77 -0
  69. package/src/oauth.shared.ts +37 -0
  70. package/src/oauth.token.ts +162 -0
  71. package/src/oauth.ts +130 -0
  72. package/src/outbound.ts +198 -0
  73. package/src/pending-uploads-fs.ts +235 -0
  74. package/src/pending-uploads.ts +121 -0
  75. package/src/policy.ts +245 -0
  76. package/src/polls-store-memory.ts +32 -0
  77. package/src/polls.ts +312 -0
  78. package/src/presentation.ts +93 -0
  79. package/src/probe.ts +132 -0
  80. package/src/reply-dispatcher.ts +523 -0
  81. package/src/reply-stream-controller.ts +334 -0
  82. package/src/resolve-allowlist.ts +309 -0
  83. package/src/revoked-context.ts +17 -0
  84. package/src/runtime.ts +12 -0
  85. package/src/sdk-types.ts +59 -0
  86. package/src/sdk.ts +916 -0
  87. package/src/secret-contract.ts +49 -0
  88. package/src/secret-input.ts +7 -0
  89. package/src/send-context.ts +269 -0
  90. package/src/send.ts +697 -0
  91. package/src/sent-message-cache.ts +174 -0
  92. package/src/session-route.ts +40 -0
  93. package/src/setup-core.ts +162 -0
  94. package/src/setup-surface.ts +319 -0
  95. package/src/sso-token-store.ts +166 -0
  96. package/src/sso.ts +300 -0
  97. package/src/storage.ts +25 -0
  98. package/src/store-fs.ts +42 -0
  99. package/src/streaming-message.ts +327 -0
  100. package/src/thread-parent-context.ts +159 -0
  101. package/src/token-response.ts +11 -0
  102. package/src/token.ts +194 -0
  103. package/src/user-agent.ts +53 -0
  104. package/src/webhook-timeouts.ts +27 -0
  105. package/src/welcome-card.ts +57 -0
  106. package/test-api.ts +1 -0
  107. package/tsconfig.json +16 -0
package/src/polls.ts ADDED
@@ -0,0 +1,312 @@
1
+ import crypto from "node:crypto";
2
+ import { isRecord, normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
3
+ import { resolveMSTeamsStorePath } from "./storage.js";
4
+ import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
5
+
6
+ type MSTeamsPollVote = {
7
+ pollId: string;
8
+ selections: string[];
9
+ };
10
+
11
+ export type MSTeamsPoll = {
12
+ id: string;
13
+ question: string;
14
+ options: string[];
15
+ maxSelections: number;
16
+ createdAt: string;
17
+ updatedAt?: string;
18
+ conversationId?: string;
19
+ messageId?: string;
20
+ votes: Record<string, string[]>;
21
+ };
22
+
23
+ export type MSTeamsPollStore = {
24
+ createPoll: (poll: MSTeamsPoll) => Promise<void>;
25
+ getPoll: (pollId: string) => Promise<MSTeamsPoll | null>;
26
+ recordVote: (params: {
27
+ pollId: string;
28
+ voterId: string;
29
+ selections: string[];
30
+ }) => Promise<MSTeamsPoll | null>;
31
+ };
32
+
33
+ type MSTeamsPollCard = {
34
+ pollId: string;
35
+ question: string;
36
+ options: string[];
37
+ maxSelections: number;
38
+ card: Record<string, unknown>;
39
+ fallbackText: string;
40
+ };
41
+
42
+ type PollStoreData = {
43
+ version: 1;
44
+ polls: Record<string, MSTeamsPoll>;
45
+ };
46
+
47
+ const STORE_FILENAME = "msteams-polls.json";
48
+ const MAX_POLLS = 1000;
49
+ const POLL_TTL_MS = 30 * 24 * 60 * 60 * 1000;
50
+
51
+ function normalizeChoiceValue(value: unknown): string | null {
52
+ if (typeof value === "string") {
53
+ const trimmed = value.trim();
54
+ return trimmed ? trimmed : null;
55
+ }
56
+ if (typeof value === "number" && Number.isFinite(value)) {
57
+ return String(value);
58
+ }
59
+ return null;
60
+ }
61
+
62
+ function extractSelections(value: unknown): string[] {
63
+ if (Array.isArray(value)) {
64
+ return value.map(normalizeChoiceValue).filter((entry): entry is string => Boolean(entry));
65
+ }
66
+ const normalized = normalizeChoiceValue(value);
67
+ if (!normalized) {
68
+ return [];
69
+ }
70
+ if (normalized.includes(",")) {
71
+ return normalized
72
+ .split(",")
73
+ .map((entry) => entry.trim())
74
+ .filter(Boolean);
75
+ }
76
+ return [normalized];
77
+ }
78
+
79
+ function readNestedValue(value: unknown, keys: Array<string | number>): unknown {
80
+ let current: unknown = value;
81
+ for (const key of keys) {
82
+ if (!isRecord(current)) {
83
+ return undefined;
84
+ }
85
+ current = current[key as keyof typeof current];
86
+ }
87
+ return current;
88
+ }
89
+
90
+ function readNestedString(value: unknown, keys: Array<string | number>): string | undefined {
91
+ return normalizeOptionalString(readNestedValue(value, keys));
92
+ }
93
+
94
+ export function extractMSTeamsPollVote(
95
+ activity: { value?: unknown } | undefined,
96
+ ): MSTeamsPollVote | null {
97
+ const value = activity?.value;
98
+ if (!value || !isRecord(value)) {
99
+ return null;
100
+ }
101
+ const pollId =
102
+ readNestedString(value, ["autobotPollId"]) ??
103
+ readNestedString(value, ["pollId"]) ??
104
+ readNestedString(value, ["autobot", "pollId"]) ??
105
+ readNestedString(value, ["autobot", "poll", "id"]) ??
106
+ readNestedString(value, ["data", "autobotPollId"]) ??
107
+ readNestedString(value, ["data", "pollId"]) ??
108
+ readNestedString(value, ["data", "autobot", "pollId"]);
109
+ if (!pollId) {
110
+ return null;
111
+ }
112
+
113
+ const directSelections = extractSelections(value.choices);
114
+ const nestedSelections = extractSelections(readNestedValue(value, ["choices"]));
115
+ const dataSelections = extractSelections(readNestedValue(value, ["data", "choices"]));
116
+ const selections =
117
+ directSelections.length > 0
118
+ ? directSelections
119
+ : nestedSelections.length > 0
120
+ ? nestedSelections
121
+ : dataSelections;
122
+
123
+ if (selections.length === 0) {
124
+ return null;
125
+ }
126
+
127
+ return {
128
+ pollId,
129
+ selections,
130
+ };
131
+ }
132
+
133
+ export function buildMSTeamsPollCard(params: {
134
+ question: string;
135
+ options: string[];
136
+ maxSelections?: number;
137
+ pollId?: string;
138
+ }): MSTeamsPollCard {
139
+ const pollId = params.pollId ?? crypto.randomUUID();
140
+ const maxSelections =
141
+ typeof params.maxSelections === "number" && params.maxSelections > 1
142
+ ? Math.floor(params.maxSelections)
143
+ : 1;
144
+ const cappedMaxSelections = Math.min(Math.max(1, maxSelections), params.options.length);
145
+ const choices = params.options.map((option, index) => ({
146
+ title: option,
147
+ value: String(index),
148
+ }));
149
+ const hint =
150
+ cappedMaxSelections > 1
151
+ ? `Select up to ${cappedMaxSelections} option${cappedMaxSelections === 1 ? "" : "s"}.`
152
+ : "Select one option.";
153
+
154
+ const card = {
155
+ type: "AdaptiveCard",
156
+ version: "1.5",
157
+ body: [
158
+ {
159
+ type: "TextBlock",
160
+ text: params.question,
161
+ wrap: true,
162
+ weight: "Bolder",
163
+ size: "Medium",
164
+ },
165
+ {
166
+ type: "Input.ChoiceSet",
167
+ id: "choices",
168
+ isMultiSelect: cappedMaxSelections > 1,
169
+ style: "expanded",
170
+ choices,
171
+ },
172
+ {
173
+ type: "TextBlock",
174
+ text: hint,
175
+ wrap: true,
176
+ isSubtle: true,
177
+ spacing: "Small",
178
+ },
179
+ ],
180
+ actions: [
181
+ {
182
+ type: "Action.Submit",
183
+ title: "Vote",
184
+ data: {
185
+ autobotPollId: pollId,
186
+ pollId,
187
+ },
188
+ msteams: {
189
+ type: "messageBack",
190
+ text: "autobot poll vote",
191
+ displayText: "Vote recorded",
192
+ value: { autobotPollId: pollId, pollId },
193
+ },
194
+ },
195
+ ],
196
+ };
197
+
198
+ const fallbackLines = [
199
+ `Poll: ${params.question}`,
200
+ ...params.options.map((option, index) => `${index + 1}. ${option}`),
201
+ ];
202
+
203
+ return {
204
+ pollId,
205
+ question: params.question,
206
+ options: params.options,
207
+ maxSelections: cappedMaxSelections,
208
+ card,
209
+ fallbackText: fallbackLines.join("\n"),
210
+ };
211
+ }
212
+
213
+ type MSTeamsPollStoreFsOptions = {
214
+ env?: NodeJS.ProcessEnv;
215
+ homedir?: () => string;
216
+ stateDir?: string;
217
+ storePath?: string;
218
+ };
219
+
220
+ function parseTimestamp(value?: string): number | null {
221
+ if (!value) {
222
+ return null;
223
+ }
224
+ const parsed = Date.parse(value);
225
+ return Number.isFinite(parsed) ? parsed : null;
226
+ }
227
+
228
+ function pruneExpired(polls: Record<string, MSTeamsPoll>) {
229
+ const cutoff = Date.now() - POLL_TTL_MS;
230
+ const entries = Object.entries(polls).filter(([, poll]) => {
231
+ const ts = parseTimestamp(poll.updatedAt ?? poll.createdAt) ?? 0;
232
+ return ts >= cutoff;
233
+ });
234
+ return Object.fromEntries(entries);
235
+ }
236
+
237
+ function pruneToLimit(polls: Record<string, MSTeamsPoll>) {
238
+ const entries = Object.entries(polls);
239
+ if (entries.length <= MAX_POLLS) {
240
+ return polls;
241
+ }
242
+ entries.sort((a, b) => {
243
+ const aTs = parseTimestamp(a[1].updatedAt ?? a[1].createdAt) ?? 0;
244
+ const bTs = parseTimestamp(b[1].updatedAt ?? b[1].createdAt) ?? 0;
245
+ return aTs - bTs;
246
+ });
247
+ const keep = entries.slice(entries.length - MAX_POLLS);
248
+ return Object.fromEntries(keep);
249
+ }
250
+
251
+ export function normalizeMSTeamsPollSelections(poll: MSTeamsPoll, selections: string[]) {
252
+ const maxSelections = Math.max(1, poll.maxSelections);
253
+ const mapped = selections
254
+ .map((entry) => Number.parseInt(entry, 10))
255
+ .filter((value) => Number.isFinite(value))
256
+ .filter((value) => value >= 0 && value < poll.options.length)
257
+ .map((value) => String(value));
258
+ const limited = maxSelections > 1 ? mapped.slice(0, maxSelections) : mapped.slice(0, 1);
259
+ return Array.from(new Set(limited));
260
+ }
261
+
262
+ export function createMSTeamsPollStoreFs(params?: MSTeamsPollStoreFsOptions): MSTeamsPollStore {
263
+ const filePath = resolveMSTeamsStorePath({
264
+ filename: STORE_FILENAME,
265
+ env: params?.env,
266
+ homedir: params?.homedir,
267
+ stateDir: params?.stateDir,
268
+ storePath: params?.storePath,
269
+ });
270
+ const empty: PollStoreData = { version: 1, polls: {} };
271
+
272
+ const readStore = async (): Promise<PollStoreData> => {
273
+ const { value } = await readJsonFile(filePath, empty);
274
+ const pruned = pruneToLimit(pruneExpired(value.polls ?? {}));
275
+ return { version: 1, polls: pruned };
276
+ };
277
+
278
+ const writeStore = async (data: PollStoreData) => {
279
+ await writeJsonFile(filePath, data);
280
+ };
281
+
282
+ const createPoll = async (poll: MSTeamsPoll) => {
283
+ await withFileLock(filePath, empty, async () => {
284
+ const data = await readStore();
285
+ data.polls[poll.id] = poll;
286
+ await writeStore({ version: 1, polls: pruneToLimit(data.polls) });
287
+ });
288
+ };
289
+
290
+ const getPoll = async (pollId: string) =>
291
+ await withFileLock(filePath, empty, async () => {
292
+ const data = await readStore();
293
+ return data.polls[pollId] ?? null;
294
+ });
295
+
296
+ const recordVote = async (params: { pollId: string; voterId: string; selections: string[] }) =>
297
+ await withFileLock(filePath, empty, async () => {
298
+ const data = await readStore();
299
+ const poll = data.polls[params.pollId];
300
+ if (!poll) {
301
+ return null;
302
+ }
303
+ const normalized = normalizeMSTeamsPollSelections(poll, params.selections);
304
+ poll.votes[params.voterId] = normalized;
305
+ poll.updatedAt = new Date().toISOString();
306
+ data.polls[poll.id] = poll;
307
+ await writeStore({ version: 1, polls: pruneToLimit(data.polls) });
308
+ return poll;
309
+ });
310
+
311
+ return { createPoll, getPoll, recordVote };
312
+ }
@@ -0,0 +1,93 @@
1
+ import {
2
+ adaptMessagePresentationForChannel,
3
+ type MessagePresentation,
4
+ } from "autobot/plugin-sdk/interactive-runtime";
5
+ import { normalizeOptionalString } from "autobot/plugin-sdk/string-coerce-runtime";
6
+ import type { ChannelOutboundAdapter } from "../runtime-api.js";
7
+
8
+ export const MSTEAMS_PRESENTATION_CAPABILITIES = {
9
+ supported: true,
10
+ buttons: true,
11
+ selects: false,
12
+ context: true,
13
+ divider: true,
14
+ limits: {
15
+ actions: {
16
+ supportsStyles: false,
17
+ supportsDisabled: false,
18
+ },
19
+ text: {
20
+ markdownDialect: "markdown",
21
+ },
22
+ },
23
+ } satisfies ChannelOutboundAdapter["presentationCapabilities"];
24
+
25
+ export function buildMSTeamsPresentationCard(params: {
26
+ presentation: MessagePresentation;
27
+ text?: string | null;
28
+ }) {
29
+ const body: Record<string, unknown>[] = [];
30
+ const text = normalizeOptionalString(params.text);
31
+ if (text) {
32
+ body.push({
33
+ type: "TextBlock",
34
+ text,
35
+ wrap: true,
36
+ });
37
+ }
38
+ const presentation = adaptMessagePresentationForChannel({
39
+ presentation: params.presentation,
40
+ capabilities: MSTEAMS_PRESENTATION_CAPABILITIES,
41
+ });
42
+ if (presentation.title) {
43
+ body.push({
44
+ type: "TextBlock",
45
+ text: presentation.title,
46
+ weight: "Bolder",
47
+ size: "Medium",
48
+ wrap: true,
49
+ });
50
+ }
51
+ const actions: Record<string, unknown>[] = [];
52
+ for (const block of presentation.blocks) {
53
+ if (block.type === "text" || block.type === "context") {
54
+ body.push({
55
+ type: "TextBlock",
56
+ text: block.text,
57
+ wrap: true,
58
+ ...(block.type === "context" ? { isSubtle: true, size: "Small" } : {}),
59
+ });
60
+ continue;
61
+ }
62
+ if (block.type === "divider") {
63
+ body.push({ type: "TextBlock", text: "---", wrap: true, isSubtle: true });
64
+ continue;
65
+ }
66
+ if (block.type === "buttons") {
67
+ for (const button of block.buttons) {
68
+ const targetUrl = button.url ?? button.webApp?.url ?? button.web_app?.url;
69
+ if (targetUrl) {
70
+ actions.push({
71
+ type: "Action.OpenUrl",
72
+ title: button.label,
73
+ url: targetUrl,
74
+ });
75
+ continue;
76
+ }
77
+ if (button.value) {
78
+ actions.push({
79
+ type: "Action.Submit",
80
+ title: button.label,
81
+ data: { value: button.value, label: button.label },
82
+ });
83
+ }
84
+ }
85
+ }
86
+ }
87
+ return {
88
+ type: "AdaptiveCard",
89
+ version: "1.4",
90
+ body,
91
+ ...(actions.length ? { actions } : {}),
92
+ };
93
+ }
package/src/probe.ts ADDED
@@ -0,0 +1,132 @@
1
+ import {
2
+ normalizeStringEntries,
3
+ type BaseProbeResult,
4
+ type MSTeamsConfig,
5
+ } from "../runtime-api.js";
6
+ import { formatUnknownError } from "./errors.js";
7
+ import { createMSTeamsTokenProvider, loadMSTeamsSdkWithAuth } from "./sdk.js";
8
+ import { readAccessToken } from "./token-response.js";
9
+ import { loadDelegatedTokens, resolveMSTeamsCredentials } from "./token.js";
10
+
11
+ export type ProbeMSTeamsResult = BaseProbeResult<string> & {
12
+ appId?: string;
13
+ graph?: {
14
+ ok: boolean;
15
+ error?: string;
16
+ roles?: string[];
17
+ scopes?: string[];
18
+ };
19
+ delegatedAuth?: {
20
+ ok: boolean;
21
+ error?: string;
22
+ scopes?: string[];
23
+ userPrincipalName?: string;
24
+ };
25
+ };
26
+
27
+ function decodeJwtPayload(token: string): Record<string, unknown> | null {
28
+ const parts = token.split(".");
29
+ if (parts.length < 2) {
30
+ return null;
31
+ }
32
+ const payload = parts[1] ?? "";
33
+ const padded = payload.padEnd(payload.length + ((4 - (payload.length % 4)) % 4), "=");
34
+ const normalized = padded.replace(/-/g, "+").replace(/_/g, "/");
35
+ try {
36
+ const decoded = Buffer.from(normalized, "base64").toString("utf8");
37
+ const parsed = JSON.parse(decoded) as Record<string, unknown>;
38
+ return parsed && typeof parsed === "object" ? parsed : null;
39
+ } catch {
40
+ return null;
41
+ }
42
+ }
43
+
44
+ function readStringArray(value: unknown): string[] | undefined {
45
+ if (!Array.isArray(value)) {
46
+ return undefined;
47
+ }
48
+ const out = normalizeStringEntries(value);
49
+ return out.length > 0 ? out : undefined;
50
+ }
51
+
52
+ function readScopes(value: unknown): string[] | undefined {
53
+ if (typeof value !== "string") {
54
+ return undefined;
55
+ }
56
+ const out = value
57
+ .split(/\s+/)
58
+ .map((entry) => entry.trim())
59
+ .filter(Boolean);
60
+ return out.length > 0 ? out : undefined;
61
+ }
62
+
63
+ export async function probeMSTeams(cfg?: MSTeamsConfig): Promise<ProbeMSTeamsResult> {
64
+ const creds = resolveMSTeamsCredentials(cfg);
65
+ if (!creds) {
66
+ return {
67
+ ok: false,
68
+ error: "missing credentials (appId, appPassword, tenantId)",
69
+ };
70
+ }
71
+
72
+ try {
73
+ const { app } = await loadMSTeamsSdkWithAuth(creds);
74
+ const tokenProvider = createMSTeamsTokenProvider(app);
75
+ const botTokenValue = await tokenProvider.getAccessToken("https://api.botframework.com");
76
+ if (!botTokenValue) {
77
+ throw new Error("Failed to acquire bot token");
78
+ }
79
+
80
+ let graph:
81
+ | {
82
+ ok: boolean;
83
+ error?: string;
84
+ roles?: string[];
85
+ scopes?: string[];
86
+ }
87
+ | undefined;
88
+ try {
89
+ const graphTokenValue = await tokenProvider.getAccessToken("https://graph.microsoft.com");
90
+ const accessToken = readAccessToken(graphTokenValue);
91
+ const payload = accessToken ? decodeJwtPayload(accessToken) : null;
92
+ graph = {
93
+ ok: true,
94
+ roles: readStringArray(payload?.roles),
95
+ scopes: readScopes(payload?.scp),
96
+ };
97
+ } catch (err) {
98
+ graph = { ok: false, error: formatUnknownError(err) };
99
+ }
100
+ let delegatedAuth: ProbeMSTeamsResult["delegatedAuth"];
101
+ if (cfg?.delegatedAuth?.enabled) {
102
+ try {
103
+ const tokens = loadDelegatedTokens();
104
+ if (tokens) {
105
+ const isExpired = tokens.expiresAt <= Date.now();
106
+ delegatedAuth = {
107
+ ok: !isExpired,
108
+ scopes: tokens.scopes,
109
+ userPrincipalName: tokens.userPrincipalName,
110
+ ...(isExpired ? { error: "token expired (will auto-refresh on next use)" } : {}),
111
+ };
112
+ } else {
113
+ delegatedAuth = { ok: false, error: "no delegated tokens found (run setup wizard)" };
114
+ }
115
+ } catch {
116
+ delegatedAuth = { ok: false, error: "failed to load delegated tokens" };
117
+ }
118
+ }
119
+ return {
120
+ ok: true,
121
+ appId: creds.appId,
122
+ ...(graph ? { graph } : {}),
123
+ ...(delegatedAuth ? { delegatedAuth } : {}),
124
+ };
125
+ } catch (err) {
126
+ return {
127
+ ok: false,
128
+ appId: creds.appId,
129
+ error: formatUnknownError(err),
130
+ };
131
+ }
132
+ }