@kodelyth/msteams 2026.5.42 → 2026.6.2

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 (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,6 +0,0 @@
1
- import { buildChannelConfigSchema, MSTeamsConfigSchema } from "../config-api.js";
2
- import { msTeamsChannelConfigUiHints } from "./config-ui-hints.js";
3
-
4
- export const MSTeamsChannelConfigSchema = buildChannelConfigSchema(MSTeamsConfigSchema, {
5
- uiHints: msTeamsChannelConfigUiHints,
6
- });
@@ -1,40 +0,0 @@
1
- import type { ChannelConfigUiHint } from "klaw/plugin-sdk/channel-core";
2
-
3
- export const msTeamsChannelConfigUiHints = {
4
- "": {
5
- label: "MS Teams",
6
- help: "Microsoft Teams channel provider configuration and provider-specific policy toggles. Use this section to isolate Teams behavior from other enterprise chat providers.",
7
- },
8
- configWrites: {
9
- label: "MS Teams Config Writes",
10
- help: "Allow Microsoft Teams to write config in response to channel events/commands (default: true).",
11
- },
12
- streaming: {
13
- label: "MS Teams Streaming",
14
- help: 'Microsoft Teams preview/progress streaming mode: "off" | "partial" | "block" | "progress". Personal chats use Teams native streaminfo progress when available.',
15
- },
16
- "streaming.progress.label": {
17
- label: "MS Teams Progress Label",
18
- help: 'Initial progress title. Use "auto" for built-in single-word labels, a custom string, or false to hide the title.',
19
- },
20
- "streaming.progress.labels": {
21
- label: "MS Teams Progress Label Pool",
22
- help: 'Candidate labels for streaming.progress.label="auto". Leave unset to use Klaw built-in progress labels.',
23
- },
24
- "streaming.progress.maxLines": {
25
- label: "MS Teams Progress Max Lines",
26
- help: "Maximum number of compact progress lines to keep below the progress title (default: 8).",
27
- },
28
- "streaming.progress.maxLineChars": {
29
- label: "MS Teams Progress Max Line Chars",
30
- help: "Maximum characters per compact progress line before truncation (default: 120). Prose cuts at word boundaries; commands and paths keep useful suffixes.",
31
- },
32
- "streaming.progress.toolProgress": {
33
- label: "MS Teams Progress Tool Lines",
34
- help: "Show compact tool/progress lines in progress mode (default: true). Set false to keep only the title until final delivery.",
35
- },
36
- "streaming.progress.commandText": {
37
- label: "MS Teams Progress Command Text",
38
- help: 'Command/exec detail in progress lines: "raw" preserves released behavior; "status" shows only the tool label.',
39
- },
40
- } satisfies Record<string, ChannelConfigUiHint>;
@@ -1,81 +0,0 @@
1
- import fs from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { beforeEach, describe, expect, it } from "vitest";
5
- import { createMSTeamsConversationStoreFs } from "./conversation-store-fs.js";
6
- import type { StoredConversationReference } from "./conversation-store.js";
7
- import { setMSTeamsRuntime } from "./runtime.js";
8
- import { msteamsRuntimeStub } from "./test-runtime.js";
9
-
10
- describe("msteams conversation store (fs-only)", () => {
11
- beforeEach(() => {
12
- setMSTeamsRuntime(msteamsRuntimeStub);
13
- });
14
-
15
- it("filters and prunes expired entries while preserving legacy entries without lastSeenAt", async () => {
16
- const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "klaw-msteams-store-"));
17
-
18
- const env: NodeJS.ProcessEnv = {
19
- ...process.env,
20
- KLAW_STATE_DIR: stateDir,
21
- };
22
-
23
- const store = createMSTeamsConversationStoreFs({ env, ttlMs: 1_000 });
24
-
25
- const ref: StoredConversationReference = {
26
- conversation: { id: "19:active@thread.tacv2" },
27
- channelId: "msteams",
28
- serviceUrl: "https://service.example.com",
29
- user: { id: "u1", aadObjectId: "aad1" },
30
- };
31
-
32
- await store.upsert("19:active@thread.tacv2", ref);
33
-
34
- const filePath = path.join(stateDir, "msteams-conversations.json");
35
- const raw = await fs.promises.readFile(filePath, "utf-8");
36
- const json = JSON.parse(raw) as {
37
- version: number;
38
- conversations: Record<string, StoredConversationReference>;
39
- };
40
-
41
- json.conversations["19:old@thread.tacv2"] = {
42
- ...ref,
43
- conversation: { id: "19:old@thread.tacv2" },
44
- lastSeenAt: new Date(Date.now() - 60_000).toISOString(),
45
- };
46
-
47
- json.conversations["19:legacy@thread.tacv2"] = {
48
- ...ref,
49
- conversation: { id: "19:legacy@thread.tacv2" },
50
- };
51
-
52
- await fs.promises.writeFile(filePath, `${JSON.stringify(json, null, 2)}\n`);
53
-
54
- const list = await store.list();
55
- const ids = list.map((entry) => entry.conversationId).toSorted();
56
- expect(ids).toEqual(["19:active@thread.tacv2", "19:legacy@thread.tacv2"]);
57
-
58
- expect(await store.get("19:old@thread.tacv2")).toBeNull();
59
- const legacyConversation = await store.get("19:legacy@thread.tacv2");
60
- if (!legacyConversation) {
61
- throw new Error("expected migrated legacy Teams conversation");
62
- }
63
- if (!legacyConversation.conversation) {
64
- throw new Error("expected migrated legacy Teams conversation payload");
65
- }
66
- expect(legacyConversation.conversation.id).toBe("19:legacy@thread.tacv2");
67
-
68
- await store.upsert("19:new@thread.tacv2", {
69
- ...ref,
70
- conversation: { id: "19:new@thread.tacv2" },
71
- });
72
-
73
- const rawAfter = await fs.promises.readFile(filePath, "utf-8");
74
- const jsonAfter = JSON.parse(rawAfter) as typeof json;
75
- expect(Object.keys(jsonAfter.conversations).toSorted()).toEqual([
76
- "19:active@thread.tacv2",
77
- "19:legacy@thread.tacv2",
78
- "19:new@thread.tacv2",
79
- ]);
80
- });
81
- });
@@ -1,149 +0,0 @@
1
- import {
2
- findPreferredDmConversationByUserId,
3
- mergeStoredConversationReference,
4
- normalizeStoredConversationId,
5
- parseStoredConversationTimestamp,
6
- toConversationStoreEntries,
7
- } from "./conversation-store-helpers.js";
8
- import type {
9
- MSTeamsConversationStore,
10
- MSTeamsConversationStoreEntry,
11
- StoredConversationReference,
12
- } from "./conversation-store.js";
13
- import { resolveMSTeamsStorePath } from "./storage.js";
14
- import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
15
-
16
- type ConversationStoreData = {
17
- version: 1;
18
- conversations: Record<string, StoredConversationReference>;
19
- };
20
-
21
- const STORE_FILENAME = "msteams-conversations.json";
22
- const MAX_CONVERSATIONS = 1000;
23
- const CONVERSATION_TTL_MS = 365 * 24 * 60 * 60 * 1000;
24
-
25
- function pruneToLimit(conversations: Record<string, StoredConversationReference>) {
26
- const entries = Object.entries(conversations);
27
- if (entries.length <= MAX_CONVERSATIONS) {
28
- return conversations;
29
- }
30
-
31
- entries.sort((a, b) => {
32
- const aTs = parseStoredConversationTimestamp(a[1].lastSeenAt) ?? 0;
33
- const bTs = parseStoredConversationTimestamp(b[1].lastSeenAt) ?? 0;
34
- return aTs - bTs;
35
- });
36
-
37
- const keep = entries.slice(entries.length - MAX_CONVERSATIONS);
38
- return Object.fromEntries(keep);
39
- }
40
-
41
- function pruneExpired(
42
- conversations: Record<string, StoredConversationReference>,
43
- nowMs: number,
44
- ttlMs: number,
45
- ) {
46
- let removed = false;
47
- const kept: typeof conversations = {};
48
- for (const [conversationId, reference] of Object.entries(conversations)) {
49
- const lastSeenAt = parseStoredConversationTimestamp(reference.lastSeenAt);
50
- // Preserve legacy entries that have no lastSeenAt until they're seen again.
51
- if (lastSeenAt != null && nowMs - lastSeenAt > ttlMs) {
52
- removed = true;
53
- continue;
54
- }
55
- kept[conversationId] = reference;
56
- }
57
- return { conversations: kept, removed };
58
- }
59
-
60
- export function createMSTeamsConversationStoreFs(params?: {
61
- env?: NodeJS.ProcessEnv;
62
- homedir?: () => string;
63
- ttlMs?: number;
64
- stateDir?: string;
65
- storePath?: string;
66
- }): MSTeamsConversationStore {
67
- const ttlMs = params?.ttlMs ?? CONVERSATION_TTL_MS;
68
- const filePath = resolveMSTeamsStorePath({
69
- filename: STORE_FILENAME,
70
- env: params?.env,
71
- homedir: params?.homedir,
72
- stateDir: params?.stateDir,
73
- storePath: params?.storePath,
74
- });
75
-
76
- const empty: ConversationStoreData = { version: 1, conversations: {} };
77
-
78
- const readStore = async (): Promise<ConversationStoreData> => {
79
- const { value } = await readJsonFile(filePath, empty);
80
- if (
81
- value.version !== 1 ||
82
- !value.conversations ||
83
- typeof value.conversations !== "object" ||
84
- Array.isArray(value.conversations)
85
- ) {
86
- return empty;
87
- }
88
- const nowMs = Date.now();
89
- const pruned = pruneExpired(value.conversations, nowMs, ttlMs).conversations;
90
- return { version: 1, conversations: pruneToLimit(pruned) };
91
- };
92
-
93
- const list = async (): Promise<MSTeamsConversationStoreEntry[]> => {
94
- const store = await readStore();
95
- return toConversationStoreEntries(Object.entries(store.conversations));
96
- };
97
-
98
- const get = async (conversationId: string): Promise<StoredConversationReference | null> => {
99
- const store = await readStore();
100
- return store.conversations[normalizeStoredConversationId(conversationId)] ?? null;
101
- };
102
-
103
- const findPreferredDmByUserId = async (
104
- id: string,
105
- ): Promise<MSTeamsConversationStoreEntry | null> => {
106
- return findPreferredDmConversationByUserId(await list(), id);
107
- };
108
-
109
- const upsert = async (
110
- conversationId: string,
111
- reference: StoredConversationReference,
112
- ): Promise<void> => {
113
- const normalizedId = normalizeStoredConversationId(conversationId);
114
- await withFileLock(filePath, empty, async () => {
115
- const store = await readStore();
116
- store.conversations[normalizedId] = mergeStoredConversationReference(
117
- store.conversations[normalizedId],
118
- reference,
119
- new Date().toISOString(),
120
- );
121
- const nowMs = Date.now();
122
- store.conversations = pruneExpired(store.conversations, nowMs, ttlMs).conversations;
123
- store.conversations = pruneToLimit(store.conversations);
124
- await writeJsonFile(filePath, store);
125
- });
126
- };
127
-
128
- const remove = async (conversationId: string): Promise<boolean> => {
129
- const normalizedId = normalizeStoredConversationId(conversationId);
130
- return await withFileLock(filePath, empty, async () => {
131
- const store = await readStore();
132
- if (!(normalizedId in store.conversations)) {
133
- return false;
134
- }
135
- delete store.conversations[normalizedId];
136
- await writeJsonFile(filePath, store);
137
- return true;
138
- });
139
- };
140
-
141
- return {
142
- upsert,
143
- get,
144
- list,
145
- remove,
146
- findPreferredDmByUserId,
147
- findByUserId: findPreferredDmByUserId,
148
- };
149
- }
@@ -1,202 +0,0 @@
1
- import { describe, expect, it } from "vitest";
2
- import { findPreferredDmConversationByUserId } from "./conversation-store-helpers.js";
3
- import type { MSTeamsConversationStoreEntry } from "./conversation-store.js";
4
-
5
- function entry(params: {
6
- conversationId: string;
7
- userId?: string;
8
- aadObjectId?: string;
9
- conversationType?: string;
10
- lastSeenAt?: string;
11
- }): MSTeamsConversationStoreEntry {
12
- return {
13
- conversationId: params.conversationId,
14
- reference: {
15
- user: {
16
- id: params.userId ?? "user-1",
17
- aadObjectId: params.aadObjectId ?? "aad-1",
18
- },
19
- conversation: {
20
- id: params.conversationId,
21
- conversationType: params.conversationType,
22
- },
23
- lastSeenAt: params.lastSeenAt,
24
- },
25
- };
26
- }
27
-
28
- describe("findPreferredDmConversationByUserId", () => {
29
- it("returns null for empty id", () => {
30
- expect(findPreferredDmConversationByUserId([], " ")).toBeNull();
31
- });
32
-
33
- it("returns null when no entries match", () => {
34
- const entries = [entry({ conversationId: "conv-1", aadObjectId: "other-user" })];
35
- expect(findPreferredDmConversationByUserId(entries, "aad-1")).toBeNull();
36
- });
37
-
38
- it("returns a personal DM conversation by aadObjectId", () => {
39
- const entries = [
40
- entry({
41
- conversationId: "dm-conv",
42
- aadObjectId: "aad-target",
43
- conversationType: "personal",
44
- }),
45
- ];
46
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
47
- expect(result?.conversationId).toBe("dm-conv");
48
- });
49
-
50
- it("returns a personal DM conversation by user.id", () => {
51
- const entries = [
52
- entry({
53
- conversationId: "dm-conv",
54
- userId: "user-target",
55
- aadObjectId: "other",
56
- conversationType: "personal",
57
- }),
58
- ];
59
- const result = findPreferredDmConversationByUserId(entries, "user-target");
60
- expect(result?.conversationId).toBe("dm-conv");
61
- });
62
-
63
- it("does NOT return a channel conversation for a user lookup (#54520)", () => {
64
- // This is the core bug: user sends messages in both a DM and a channel.
65
- // The channel conversation also carries the user's aadObjectId.
66
- // findPreferredDmByUserId must NOT return the channel conversation.
67
- const entries = [
68
- entry({
69
- conversationId: "19:channel@thread.tacv2",
70
- aadObjectId: "aad-target",
71
- conversationType: "channel",
72
- lastSeenAt: "2026-03-25T21:00:00.000Z",
73
- }),
74
- ];
75
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
76
- expect(result).toBeNull();
77
- });
78
-
79
- it("does NOT return a groupChat conversation for a user lookup (#54520)", () => {
80
- const entries = [
81
- entry({
82
- conversationId: "19:group@thread.tacv2",
83
- aadObjectId: "aad-target",
84
- conversationType: "groupChat",
85
- lastSeenAt: "2026-03-25T21:00:00.000Z",
86
- }),
87
- ];
88
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
89
- expect(result).toBeNull();
90
- });
91
-
92
- it("prefers personal DM over channel even when channel is more recent (#54520)", () => {
93
- // Reproduces the exact race: channel message arrives after DM, but the
94
- // DM conversation should still be returned.
95
- const entries = [
96
- entry({
97
- conversationId: "dm-conv",
98
- aadObjectId: "aad-target",
99
- conversationType: "personal",
100
- lastSeenAt: "2026-03-25T20:00:00.000Z",
101
- }),
102
- entry({
103
- conversationId: "19:channel@thread.tacv2",
104
- aadObjectId: "aad-target",
105
- conversationType: "channel",
106
- lastSeenAt: "2026-03-25T21:00:00.000Z",
107
- }),
108
- ];
109
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
110
- expect(result?.conversationId).toBe("dm-conv");
111
- });
112
-
113
- it("prefers personal DM over groupChat even when groupChat is more recent", () => {
114
- const entries = [
115
- entry({
116
- conversationId: "dm-conv",
117
- aadObjectId: "aad-target",
118
- conversationType: "personal",
119
- lastSeenAt: "2026-03-25T20:00:00.000Z",
120
- }),
121
- entry({
122
- conversationId: "19:group@thread.tacv2",
123
- aadObjectId: "aad-target",
124
- conversationType: "groupChat",
125
- lastSeenAt: "2026-03-25T21:00:00.000Z",
126
- }),
127
- ];
128
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
129
- expect(result?.conversationId).toBe("dm-conv");
130
- });
131
-
132
- it("prefers the freshest personal DM when multiple exist", () => {
133
- const entries = [
134
- entry({
135
- conversationId: "dm-old",
136
- aadObjectId: "aad-target",
137
- conversationType: "personal",
138
- lastSeenAt: "2026-03-25T20:00:00.000Z",
139
- }),
140
- entry({
141
- conversationId: "dm-new",
142
- aadObjectId: "aad-target",
143
- conversationType: "personal",
144
- lastSeenAt: "2026-03-25T21:00:00.000Z",
145
- }),
146
- ];
147
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
148
- expect(result?.conversationId).toBe("dm-new");
149
- });
150
-
151
- it("falls back to unknown-type entries when no personal conversations exist", () => {
152
- // Legacy entries without conversationType should still be usable as a
153
- // fallback to avoid breaking existing deployments.
154
- const entries = [
155
- entry({
156
- conversationId: "legacy-conv",
157
- aadObjectId: "aad-target",
158
- // No conversationType set (legacy entry)
159
- }),
160
- ];
161
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
162
- expect(result?.conversationId).toBe("legacy-conv");
163
- });
164
-
165
- it("prefers personal over unknown-type entries", () => {
166
- const entries = [
167
- entry({
168
- conversationId: "legacy-conv",
169
- aadObjectId: "aad-target",
170
- lastSeenAt: "2026-03-25T21:00:00.000Z",
171
- // No conversationType
172
- }),
173
- entry({
174
- conversationId: "dm-conv",
175
- aadObjectId: "aad-target",
176
- conversationType: "personal",
177
- lastSeenAt: "2026-03-25T20:00:00.000Z",
178
- }),
179
- ];
180
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
181
- expect(result?.conversationId).toBe("dm-conv");
182
- });
183
-
184
- it("does NOT fall back to channel/group when no personal or unknown entries exist", () => {
185
- const entries = [
186
- entry({
187
- conversationId: "19:channel@thread.tacv2",
188
- aadObjectId: "aad-target",
189
- conversationType: "channel",
190
- lastSeenAt: "2026-03-25T21:00:00.000Z",
191
- }),
192
- entry({
193
- conversationId: "19:group@thread.tacv2",
194
- aadObjectId: "aad-target",
195
- conversationType: "groupChat",
196
- lastSeenAt: "2026-03-25T20:00:00.000Z",
197
- }),
198
- ];
199
- const result = findPreferredDmConversationByUserId(entries, "aad-target");
200
- expect(result).toBeNull();
201
- });
202
- });
@@ -1,105 +0,0 @@
1
- import { normalizeLowercaseStringOrEmpty } from "klaw/plugin-sdk/string-coerce-runtime";
2
- import type {
3
- MSTeamsConversationStoreEntry,
4
- StoredConversationReference,
5
- } from "./conversation-store.js";
6
-
7
- export function normalizeStoredConversationId(raw: string): string {
8
- return raw.split(";")[0] ?? raw;
9
- }
10
-
11
- export function parseStoredConversationTimestamp(value: string | undefined): number | null {
12
- if (!value) {
13
- return null;
14
- }
15
- const parsed = Date.parse(value);
16
- if (!Number.isFinite(parsed)) {
17
- return null;
18
- }
19
- return parsed;
20
- }
21
-
22
- export function toConversationStoreEntries(
23
- entries: Iterable<[string, StoredConversationReference]>,
24
- ): MSTeamsConversationStoreEntry[] {
25
- return Array.from(entries, ([conversationId, reference]) => ({
26
- conversationId,
27
- reference,
28
- }));
29
- }
30
-
31
- export function mergeStoredConversationReference(
32
- existing: StoredConversationReference | undefined,
33
- incoming: StoredConversationReference,
34
- nowIso: string,
35
- ): StoredConversationReference {
36
- return {
37
- // Preserve fields from the previous entry that may not be present on every
38
- // inbound activity. Without this, sparse activities (e.g. conversationUpdate,
39
- // reactions) would clear previously captured values. Some fields are only
40
- // populated opportunistically, such as timezone from clientInfo entities and
41
- // graphChatId from Graph lookups used for DM media downloads.
42
- ...(existing?.timezone && !incoming.timezone ? { timezone: existing.timezone } : {}),
43
- ...(existing?.graphChatId && !incoming.graphChatId
44
- ? { graphChatId: existing.graphChatId }
45
- : {}),
46
- ...(existing?.tenantId && !incoming.tenantId ? { tenantId: existing.tenantId } : {}),
47
- ...(existing?.aadObjectId && !incoming.aadObjectId
48
- ? { aadObjectId: existing.aadObjectId }
49
- : {}),
50
- ...incoming,
51
- lastSeenAt: nowIso,
52
- };
53
- }
54
-
55
- export function findPreferredDmConversationByUserId(
56
- entries: Iterable<MSTeamsConversationStoreEntry>,
57
- id: string,
58
- ): MSTeamsConversationStoreEntry | null {
59
- const target = id.trim();
60
- if (!target) {
61
- return null;
62
- }
63
-
64
- // Partition user matches into DM-safe and non-DM buckets.
65
- // Channel and group conversations also carry the sender's aadObjectId, but
66
- // returning one of those when the caller asked for a user-targeted DM would
67
- // leak the reply into a shared channel -- the root cause of #54520.
68
- const personalMatches: MSTeamsConversationStoreEntry[] = [];
69
- const unknownTypeMatches: MSTeamsConversationStoreEntry[] = [];
70
- for (const entry of entries) {
71
- if (entry.reference.user?.aadObjectId !== target && entry.reference.user?.id !== target) {
72
- continue;
73
- }
74
- const convType = normalizeLowercaseStringOrEmpty(
75
- entry.reference.conversation?.conversationType ?? "",
76
- );
77
- if (convType === "personal") {
78
- personalMatches.push(entry);
79
- } else if (convType === "channel" || convType === "groupchat") {
80
- // Explicitly skip channel/group conversations -- these must never be
81
- // returned for a user-targeted DM lookup.
82
- } else {
83
- // Legacy entries without conversationType are ambiguous. Include them
84
- // as a fallback but rank below confirmed personal conversations.
85
- unknownTypeMatches.push(entry);
86
- }
87
- }
88
-
89
- // Prefer confirmed personal DMs, fall back to unknown-type entries.
90
- const candidates = personalMatches.length > 0 ? personalMatches : unknownTypeMatches;
91
- if (candidates.length === 0) {
92
- return null;
93
- }
94
-
95
- // When multiple candidates exist, prefer the most recently seen one.
96
- if (candidates.length > 1) {
97
- candidates.sort(
98
- (a, b) =>
99
- (parseStoredConversationTimestamp(b.reference.lastSeenAt) ?? 0) -
100
- (parseStoredConversationTimestamp(a.reference.lastSeenAt) ?? 0),
101
- );
102
- }
103
-
104
- return candidates[0] ?? null;
105
- }
@@ -1,51 +0,0 @@
1
- import {
2
- findPreferredDmConversationByUserId,
3
- mergeStoredConversationReference,
4
- normalizeStoredConversationId,
5
- toConversationStoreEntries,
6
- } from "./conversation-store-helpers.js";
7
- import type {
8
- MSTeamsConversationStore,
9
- MSTeamsConversationStoreEntry,
10
- StoredConversationReference,
11
- } from "./conversation-store.js";
12
-
13
- export function createMSTeamsConversationStoreMemory(
14
- initial: MSTeamsConversationStoreEntry[] = [],
15
- ): MSTeamsConversationStore {
16
- const map = new Map<string, StoredConversationReference>();
17
- for (const { conversationId, reference } of initial) {
18
- map.set(normalizeStoredConversationId(conversationId), reference);
19
- }
20
-
21
- const findPreferredDmByUserId = async (
22
- id: string,
23
- ): Promise<MSTeamsConversationStoreEntry | null> => {
24
- return findPreferredDmConversationByUserId(toConversationStoreEntries(map.entries()), id);
25
- };
26
-
27
- return {
28
- upsert: async (conversationId, reference) => {
29
- const normalizedId = normalizeStoredConversationId(conversationId);
30
- map.set(
31
- normalizedId,
32
- mergeStoredConversationReference(
33
- map.get(normalizedId),
34
- reference,
35
- new Date().toISOString(),
36
- ),
37
- );
38
- },
39
- get: async (conversationId) => {
40
- return map.get(normalizeStoredConversationId(conversationId)) ?? null;
41
- },
42
- list: async () => {
43
- return toConversationStoreEntries(map.entries());
44
- },
45
- remove: async (conversationId) => {
46
- return map.delete(normalizeStoredConversationId(conversationId));
47
- },
48
- findPreferredDmByUserId,
49
- findByUserId: findPreferredDmByUserId,
50
- };
51
- }