@openclaw/msteams 2026.3.13 → 2026.5.2-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/api.ts +3 -0
- package/channel-config-api.ts +1 -0
- package/channel-plugin-api.ts +2 -0
- package/config-api.ts +4 -0
- package/contract-api.ts +4 -0
- package/index.ts +15 -12
- package/openclaw.plugin.json +553 -1
- package/package.json +46 -12
- package/runtime-api.ts +73 -0
- package/secret-contract-api.ts +5 -0
- package/setup-entry.ts +13 -0
- package/setup-plugin-api.ts +3 -0
- package/src/ai-entity.ts +7 -0
- package/src/approval-auth.ts +44 -0
- package/src/attachments/bot-framework.test.ts +461 -0
- package/src/attachments/bot-framework.ts +362 -0
- package/src/attachments/download.ts +63 -19
- package/src/attachments/graph.test.ts +416 -0
- package/src/attachments/graph.ts +163 -72
- package/src/attachments/html.ts +33 -1
- package/src/attachments/payload.ts +1 -1
- package/src/attachments/remote-media.test.ts +137 -0
- package/src/attachments/remote-media.ts +75 -8
- package/src/attachments/shared.test.ts +138 -1
- package/src/attachments/shared.ts +193 -26
- package/src/attachments/types.ts +10 -0
- package/src/attachments.graph.test.ts +342 -0
- package/src/attachments.helpers.test.ts +246 -0
- package/src/attachments.test-helpers.ts +17 -0
- package/src/attachments.test.ts +163 -418
- package/src/attachments.ts +5 -5
- package/src/block-streaming-config.test.ts +61 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.actions.test.ts +742 -0
- package/src/channel.directory.test.ts +145 -4
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -0
- package/src/channel.test.ts +128 -0
- package/src/channel.ts +1077 -395
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +12 -0
- package/src/conversation-store-fs.test.ts +4 -5
- package/src/conversation-store-fs.ts +35 -51
- package/src/conversation-store-helpers.test.ts +202 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +27 -23
- package/src/conversation-store.shared.test.ts +225 -0
- package/src/conversation-store.ts +30 -0
- package/src/directory-live.test.ts +156 -0
- package/src/directory-live.ts +7 -4
- package/src/doctor.ts +27 -0
- package/src/errors.test.ts +64 -1
- package/src/errors.ts +50 -9
- package/src/feedback-reflection-prompt.ts +117 -0
- package/src/feedback-reflection-store.ts +114 -0
- package/src/feedback-reflection.test.ts +237 -0
- package/src/feedback-reflection.ts +283 -0
- package/src/file-consent-helpers.test.ts +83 -0
- package/src/file-consent-helpers.ts +64 -11
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.test.ts +363 -0
- package/src/file-consent.ts +165 -4
- package/src/graph-chat.ts +5 -3
- package/src/graph-group-management.test.ts +318 -0
- package/src/graph-group-management.ts +168 -0
- package/src/graph-members.test.ts +89 -0
- package/src/graph-members.ts +48 -0
- package/src/graph-messages.actions.test.ts +243 -0
- package/src/graph-messages.read.test.ts +391 -0
- package/src/graph-messages.search.test.ts +213 -0
- package/src/graph-messages.test-helpers.ts +50 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.test.ts +215 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.test.ts +246 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.test.ts +161 -4
- package/src/graph-upload.ts +147 -56
- package/src/graph.test.ts +516 -0
- package/src/graph.ts +233 -21
- package/src/inbound.test.ts +156 -1
- package/src/inbound.ts +101 -1
- package/src/media-helpers.ts +1 -1
- package/src/mentions.test.ts +27 -18
- package/src/mentions.ts +2 -2
- package/src/messenger.test.ts +504 -23
- package/src/messenger.ts +133 -52
- package/src/monitor-handler/access.ts +125 -0
- package/src/monitor-handler/inbound-media.test.ts +289 -0
- package/src/monitor-handler/inbound-media.ts +57 -5
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
- package/src/monitor-handler/message-handler.authz.test.ts +588 -74
- package/src/monitor-handler/message-handler.dm-media.test.ts +54 -0
- package/src/monitor-handler/message-handler.test-support.ts +100 -0
- package/src/monitor-handler/message-handler.thread-parent.test.ts +223 -0
- package/src/monitor-handler/message-handler.thread-session.test.ts +77 -0
- package/src/monitor-handler/message-handler.ts +470 -164
- package/src/monitor-handler/reaction-handler.test.ts +267 -0
- package/src/monitor-handler/reaction-handler.ts +210 -0
- package/src/monitor-handler/thread-session.ts +17 -0
- package/src/monitor-handler.adaptive-card.test.ts +162 -0
- package/src/monitor-handler.feedback-authz.test.ts +314 -0
- package/src/monitor-handler.file-consent.test.ts +281 -79
- package/src/monitor-handler.sso.test.ts +563 -0
- package/src/monitor-handler.test-helpers.ts +180 -0
- package/src/monitor-handler.ts +459 -115
- package/src/monitor-handler.types.ts +27 -0
- package/src/monitor-types.ts +1 -0
- package/src/monitor.lifecycle.test.ts +74 -10
- package/src/monitor.test.ts +35 -1
- package/src/monitor.ts +143 -46
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.test.ts +305 -0
- package/src/oauth.token.ts +158 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.test.ts +10 -11
- package/src/outbound.ts +62 -44
- package/src/pending-uploads-fs.test.ts +246 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.test.ts +173 -0
- package/src/pending-uploads.ts +34 -2
- package/src/policy.test.ts +11 -5
- package/src/policy.ts +5 -5
- package/src/polls.test.ts +106 -5
- package/src/polls.ts +15 -7
- package/src/presentation.ts +68 -0
- package/src/probe.test.ts +27 -8
- package/src/probe.ts +43 -9
- package/src/reply-dispatcher.test.ts +437 -0
- package/src/reply-dispatcher.ts +259 -73
- package/src/reply-stream-controller.test.ts +235 -0
- package/src/reply-stream-controller.ts +147 -0
- package/src/resolve-allowlist.test.ts +105 -1
- package/src/resolve-allowlist.ts +112 -7
- package/src/runtime.ts +6 -3
- package/src/sdk-types.ts +43 -3
- package/src/sdk.test.ts +666 -0
- package/src/sdk.ts +867 -16
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +1 -1
- package/src/send-context.ts +76 -9
- package/src/send.test.ts +389 -5
- package/src/send.ts +140 -32
- package/src/sent-message-cache.ts +30 -18
- package/src/session-route.ts +40 -0
- package/src/setup-core.ts +160 -0
- package/src/setup-surface.test.ts +202 -0
- package/src/setup-surface.ts +320 -0
- package/src/sso-token-store.test.ts +72 -0
- package/src/sso-token-store.ts +166 -0
- package/src/sso.ts +300 -0
- package/src/storage.ts +1 -1
- package/src/store-fs.ts +2 -2
- package/src/streaming-message.test.ts +262 -0
- package/src/streaming-message.ts +297 -0
- package/src/test-runtime.ts +1 -1
- package/src/thread-parent-context.test.ts +224 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token.test.ts +237 -50
- package/src/token.ts +162 -7
- package/src/user-agent.test.ts +86 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.test.ts +81 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
- package/CHANGELOG.md +0 -107
- package/src/file-lock.ts +0 -1
- package/src/graph-users.test.ts +0 -66
- package/src/onboarding.ts +0 -381
- package/src/polls-store.test.ts +0 -38
- package/src/revoked-context.test.ts +0 -39
- package/src/token-response.test.ts +0 -23
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { translateMSTeamsDmConversationIdForGraph } from "../inbound.js";
|
|
3
|
+
|
|
4
|
+
describe("translateMSTeamsDmConversationIdForGraph", () => {
|
|
5
|
+
it("translates a: conversation ID to Graph format for DMs", () => {
|
|
6
|
+
const result = translateMSTeamsDmConversationIdForGraph({
|
|
7
|
+
isDirectMessage: true,
|
|
8
|
+
conversationId: "a:1abc2def3",
|
|
9
|
+
aadObjectId: "user-aad-id",
|
|
10
|
+
appId: "bot-app-id",
|
|
11
|
+
});
|
|
12
|
+
expect(result).toBe("19:user-aad-id_bot-app-id@unq.gbl.spaces");
|
|
13
|
+
});
|
|
14
|
+
|
|
15
|
+
it("passes through non-a: conversation IDs unchanged", () => {
|
|
16
|
+
const result = translateMSTeamsDmConversationIdForGraph({
|
|
17
|
+
isDirectMessage: true,
|
|
18
|
+
conversationId: "19:existing@unq.gbl.spaces",
|
|
19
|
+
aadObjectId: "user-aad-id",
|
|
20
|
+
appId: "bot-app-id",
|
|
21
|
+
});
|
|
22
|
+
expect(result).toBe("19:existing@unq.gbl.spaces");
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
it("passes through when aadObjectId is missing", () => {
|
|
26
|
+
const result = translateMSTeamsDmConversationIdForGraph({
|
|
27
|
+
isDirectMessage: true,
|
|
28
|
+
conversationId: "a:1abc2def3",
|
|
29
|
+
aadObjectId: null,
|
|
30
|
+
appId: "bot-app-id",
|
|
31
|
+
});
|
|
32
|
+
expect(result).toBe("a:1abc2def3");
|
|
33
|
+
});
|
|
34
|
+
|
|
35
|
+
it("passes through when appId is missing", () => {
|
|
36
|
+
const result = translateMSTeamsDmConversationIdForGraph({
|
|
37
|
+
isDirectMessage: true,
|
|
38
|
+
conversationId: "a:1abc2def3",
|
|
39
|
+
aadObjectId: "user-aad-id",
|
|
40
|
+
appId: null,
|
|
41
|
+
});
|
|
42
|
+
expect(result).toBe("a:1abc2def3");
|
|
43
|
+
});
|
|
44
|
+
|
|
45
|
+
it("passes through for non-DM conversations even with a: prefix", () => {
|
|
46
|
+
const result = translateMSTeamsDmConversationIdForGraph({
|
|
47
|
+
isDirectMessage: false,
|
|
48
|
+
conversationId: "a:1abc2def3",
|
|
49
|
+
aadObjectId: "user-aad-id",
|
|
50
|
+
appId: "bot-app-id",
|
|
51
|
+
});
|
|
52
|
+
expect(result).toBe("a:1abc2def3");
|
|
53
|
+
});
|
|
54
|
+
});
|
|
@@ -0,0 +1,100 @@
|
|
|
1
|
+
import { vi } from "vitest";
|
|
2
|
+
import type { OpenClawConfig, RuntimeEnv } from "../../runtime-api.js";
|
|
3
|
+
import type { MSTeamsMessageHandlerDeps } from "../monitor-handler.js";
|
|
4
|
+
import { installMSTeamsTestRuntime } from "../monitor-handler.test-helpers.js";
|
|
5
|
+
|
|
6
|
+
export const channelConversationId = "19:general@thread.tacv2";
|
|
7
|
+
|
|
8
|
+
type MessageHandlerDepsOptions = {
|
|
9
|
+
enqueueSystemEvent?: ReturnType<typeof vi.fn>;
|
|
10
|
+
readAllowFromStore?: ReturnType<typeof vi.fn>;
|
|
11
|
+
upsertPairingRequest?: ReturnType<typeof vi.fn>;
|
|
12
|
+
recordInboundSession?: ReturnType<typeof vi.fn>;
|
|
13
|
+
resolveAgentRoute?: (params: { peer: { kind: string; id: string } }) => unknown;
|
|
14
|
+
};
|
|
15
|
+
|
|
16
|
+
export function createMessageHandlerDeps(
|
|
17
|
+
cfg: OpenClawConfig,
|
|
18
|
+
options: MessageHandlerDepsOptions = {},
|
|
19
|
+
) {
|
|
20
|
+
const enqueueSystemEvent = options.enqueueSystemEvent ?? vi.fn();
|
|
21
|
+
const readAllowFromStore = options.readAllowFromStore ?? vi.fn(async () => []);
|
|
22
|
+
const upsertPairingRequest = options.upsertPairingRequest ?? vi.fn(async () => null);
|
|
23
|
+
const recordInboundSession =
|
|
24
|
+
options.recordInboundSession ?? vi.fn(async (_params: { sessionKey: string }) => undefined);
|
|
25
|
+
const resolveAgentRoute =
|
|
26
|
+
options.resolveAgentRoute ??
|
|
27
|
+
vi.fn(({ peer }: { peer: { kind: string; id: string } }) => ({
|
|
28
|
+
sessionKey: `agent:main:msteams:${peer.kind}:${peer.id}`,
|
|
29
|
+
agentId: "main",
|
|
30
|
+
accountId: "default",
|
|
31
|
+
mainSessionKey: "agent:main:main",
|
|
32
|
+
lastRoutePolicy: "session" as const,
|
|
33
|
+
matchedBy: "default" as const,
|
|
34
|
+
}));
|
|
35
|
+
|
|
36
|
+
installMSTeamsTestRuntime({
|
|
37
|
+
enqueueSystemEvent,
|
|
38
|
+
readAllowFromStore,
|
|
39
|
+
upsertPairingRequest,
|
|
40
|
+
recordInboundSession,
|
|
41
|
+
resolveAgentRoute,
|
|
42
|
+
resolveTextChunkLimit: () => 4000,
|
|
43
|
+
resolveStorePath: () => "/tmp/test-store",
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
const conversationStore = {
|
|
47
|
+
get: vi.fn(async () => null),
|
|
48
|
+
upsert: vi.fn(async () => undefined),
|
|
49
|
+
list: vi.fn(async () => []),
|
|
50
|
+
remove: vi.fn(async () => false),
|
|
51
|
+
findPreferredDmByUserId: vi.fn(async () => null),
|
|
52
|
+
findByUserId: vi.fn(async () => null),
|
|
53
|
+
} satisfies MSTeamsMessageHandlerDeps["conversationStore"];
|
|
54
|
+
|
|
55
|
+
const deps: MSTeamsMessageHandlerDeps = {
|
|
56
|
+
cfg,
|
|
57
|
+
runtime: { error: vi.fn() } as unknown as RuntimeEnv,
|
|
58
|
+
appId: "test-app",
|
|
59
|
+
adapter: {} as MSTeamsMessageHandlerDeps["adapter"],
|
|
60
|
+
tokenProvider: {
|
|
61
|
+
getAccessToken: vi.fn(async () => "token"),
|
|
62
|
+
},
|
|
63
|
+
textLimit: 4000,
|
|
64
|
+
mediaMaxBytes: 1024 * 1024,
|
|
65
|
+
conversationStore,
|
|
66
|
+
pollStore: {
|
|
67
|
+
recordVote: vi.fn(async () => null),
|
|
68
|
+
} as unknown as MSTeamsMessageHandlerDeps["pollStore"],
|
|
69
|
+
log: {
|
|
70
|
+
info: vi.fn(),
|
|
71
|
+
debug: vi.fn(),
|
|
72
|
+
error: vi.fn(),
|
|
73
|
+
} as unknown as MSTeamsMessageHandlerDeps["log"],
|
|
74
|
+
};
|
|
75
|
+
|
|
76
|
+
return {
|
|
77
|
+
conversationStore,
|
|
78
|
+
deps,
|
|
79
|
+
enqueueSystemEvent,
|
|
80
|
+
readAllowFromStore,
|
|
81
|
+
upsertPairingRequest,
|
|
82
|
+
recordInboundSession,
|
|
83
|
+
resolveAgentRoute,
|
|
84
|
+
};
|
|
85
|
+
}
|
|
86
|
+
|
|
87
|
+
export function buildChannelActivity(overrides: Record<string, unknown> = {}) {
|
|
88
|
+
return {
|
|
89
|
+
id: "msg-1",
|
|
90
|
+
type: "message",
|
|
91
|
+
text: "hello",
|
|
92
|
+
from: { id: "user-id", aadObjectId: "user-aad", name: "Test User" },
|
|
93
|
+
recipient: { id: "bot-id", name: "Bot" },
|
|
94
|
+
conversation: { id: channelConversationId, conversationType: "channel" },
|
|
95
|
+
channelData: { team: { id: "team-1" } },
|
|
96
|
+
attachments: [],
|
|
97
|
+
entities: [{ type: "mention", mentioned: { id: "bot-id" } }],
|
|
98
|
+
...overrides,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
@@ -0,0 +1,223 @@
|
|
|
1
|
+
import { beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import type { OpenClawConfig } from "../../runtime-api.js";
|
|
3
|
+
import { _resetThreadParentContextCachesForTest } from "../thread-parent-context.js";
|
|
4
|
+
import "./message-handler-mock-support.test-support.js";
|
|
5
|
+
import { getRuntimeApiMockState } from "./message-handler-mock-support.test-support.js";
|
|
6
|
+
import { createMSTeamsMessageHandler } from "./message-handler.js";
|
|
7
|
+
import {
|
|
8
|
+
buildChannelActivity,
|
|
9
|
+
channelConversationId,
|
|
10
|
+
createMessageHandlerDeps,
|
|
11
|
+
} from "./message-handler.test-support.js";
|
|
12
|
+
|
|
13
|
+
const runtimeApiMockState = getRuntimeApiMockState();
|
|
14
|
+
const fetchChannelMessageMock = vi.hoisted(() => vi.fn());
|
|
15
|
+
const fetchThreadRepliesMock = vi.hoisted(() => vi.fn(async () => []));
|
|
16
|
+
const resolveTeamGroupIdMock = vi.hoisted(() => vi.fn(async () => "group-1"));
|
|
17
|
+
|
|
18
|
+
vi.mock("../graph-thread.js", () => {
|
|
19
|
+
const stripHtmlFromTeamsMessage = (html: string) =>
|
|
20
|
+
html
|
|
21
|
+
.replace(/<at[^>]*>(.*?)<\/at>/gi, "@$1")
|
|
22
|
+
.replace(/<[^>]*>/g, " ")
|
|
23
|
+
.replace(/&/g, "&")
|
|
24
|
+
.replace(/</g, "<")
|
|
25
|
+
.replace(/>/g, ">")
|
|
26
|
+
.replace(/"/g, '"')
|
|
27
|
+
.replace(/'/g, "'")
|
|
28
|
+
.replace(/ /g, " ")
|
|
29
|
+
.replace(/\s+/g, " ")
|
|
30
|
+
.trim();
|
|
31
|
+
return {
|
|
32
|
+
stripHtmlFromTeamsMessage,
|
|
33
|
+
resolveTeamGroupId: resolveTeamGroupIdMock,
|
|
34
|
+
fetchChannelMessage: fetchChannelMessageMock,
|
|
35
|
+
fetchThreadReplies: fetchThreadRepliesMock,
|
|
36
|
+
};
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
describe("msteams thread parent context injection", () => {
|
|
40
|
+
type MessageHandler = ReturnType<typeof createMSTeamsMessageHandler>;
|
|
41
|
+
|
|
42
|
+
function findParentSystemEventCall(
|
|
43
|
+
mock: ReturnType<typeof vi.fn>,
|
|
44
|
+
): [string, { sessionKey: string; contextKey?: string }] | undefined {
|
|
45
|
+
const calls = mock.mock.calls as Array<[string, { sessionKey: string; contextKey?: string }]>;
|
|
46
|
+
return calls.find(([text]) => text.startsWith("Replying to @"));
|
|
47
|
+
}
|
|
48
|
+
|
|
49
|
+
async function dispatchThreadReply(handler: MessageHandler, id: string) {
|
|
50
|
+
await handler({
|
|
51
|
+
activity: buildChannelActivity({ id, replyToId: "thread-root-123" }),
|
|
52
|
+
sendActivity: vi.fn(async () => undefined),
|
|
53
|
+
} as unknown as Parameters<MessageHandler>[0]);
|
|
54
|
+
}
|
|
55
|
+
|
|
56
|
+
async function dispatchTwoThreadReplies(handler: MessageHandler) {
|
|
57
|
+
await dispatchThreadReply(handler, "msg-reply-1");
|
|
58
|
+
await dispatchThreadReply(handler, "msg-reply-2");
|
|
59
|
+
}
|
|
60
|
+
|
|
61
|
+
beforeEach(() => {
|
|
62
|
+
_resetThreadParentContextCachesForTest();
|
|
63
|
+
fetchChannelMessageMock.mockReset();
|
|
64
|
+
fetchThreadRepliesMock.mockReset();
|
|
65
|
+
fetchThreadRepliesMock.mockImplementation(async () => []);
|
|
66
|
+
resolveTeamGroupIdMock.mockReset();
|
|
67
|
+
resolveTeamGroupIdMock.mockImplementation(async () => "group-1");
|
|
68
|
+
runtimeApiMockState.dispatchReplyFromConfigWithSettledDispatcher.mockClear();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
const cfg: OpenClawConfig = {
|
|
72
|
+
channels: { msteams: { groupPolicy: "open" } },
|
|
73
|
+
} as OpenClawConfig;
|
|
74
|
+
|
|
75
|
+
it("enqueues a Replying to @sender system event on the first thread reply", async () => {
|
|
76
|
+
fetchChannelMessageMock.mockResolvedValueOnce({
|
|
77
|
+
id: "thread-root-123",
|
|
78
|
+
from: { user: { displayName: "Alice", id: "alice-id" } },
|
|
79
|
+
body: { content: "Can someone investigate the latency spike?", contentType: "text" },
|
|
80
|
+
});
|
|
81
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps(cfg);
|
|
82
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
83
|
+
|
|
84
|
+
await handler({
|
|
85
|
+
activity: buildChannelActivity({ id: "msg-reply-1", replyToId: "thread-root-123" }),
|
|
86
|
+
sendActivity: vi.fn(async () => undefined),
|
|
87
|
+
} as unknown as Parameters<typeof handler>[0]);
|
|
88
|
+
|
|
89
|
+
const parentCall = findParentSystemEventCall(enqueueSystemEvent);
|
|
90
|
+
expect(parentCall).toBeDefined();
|
|
91
|
+
expect(parentCall?.[0]).toBe("Replying to @Alice: Can someone investigate the latency spike?");
|
|
92
|
+
expect(parentCall?.[1]?.contextKey).toContain("msteams:thread-parent:");
|
|
93
|
+
expect(parentCall?.[1]?.contextKey).toContain("thread-root-123");
|
|
94
|
+
});
|
|
95
|
+
|
|
96
|
+
it("caches parent fetches across thread replies in the same session", async () => {
|
|
97
|
+
fetchChannelMessageMock.mockResolvedValue({
|
|
98
|
+
id: "thread-root-123",
|
|
99
|
+
from: { user: { displayName: "Alice" } },
|
|
100
|
+
body: { content: "Original question", contentType: "text" },
|
|
101
|
+
});
|
|
102
|
+
const { deps } = createMessageHandlerDeps(cfg);
|
|
103
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
104
|
+
|
|
105
|
+
await dispatchTwoThreadReplies(handler);
|
|
106
|
+
|
|
107
|
+
// Parent message fetched exactly once across two replies thanks to LRU cache.
|
|
108
|
+
expect(fetchChannelMessageMock).toHaveBeenCalledTimes(1);
|
|
109
|
+
});
|
|
110
|
+
|
|
111
|
+
it("does not re-enqueue the same parent context within the same session", async () => {
|
|
112
|
+
fetchChannelMessageMock.mockResolvedValue({
|
|
113
|
+
id: "thread-root-123",
|
|
114
|
+
from: { user: { displayName: "Alice" } },
|
|
115
|
+
body: { content: "Original question", contentType: "text" },
|
|
116
|
+
});
|
|
117
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps(cfg);
|
|
118
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
119
|
+
|
|
120
|
+
await dispatchTwoThreadReplies(handler);
|
|
121
|
+
|
|
122
|
+
const parentCalls = enqueueSystemEvent.mock.calls.filter(
|
|
123
|
+
([text]) => typeof text === "string" && text.startsWith("Replying to @"),
|
|
124
|
+
);
|
|
125
|
+
expect(parentCalls).toHaveLength(1);
|
|
126
|
+
});
|
|
127
|
+
|
|
128
|
+
it("does not enqueue parent context when allowlist visibility blocks the parent sender", async () => {
|
|
129
|
+
fetchChannelMessageMock.mockResolvedValue({
|
|
130
|
+
id: "thread-root-123",
|
|
131
|
+
from: { user: { displayName: "Mallory", id: "mallory-aad" } },
|
|
132
|
+
body: { content: "Blocked context", contentType: "text" },
|
|
133
|
+
});
|
|
134
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps({
|
|
135
|
+
channels: {
|
|
136
|
+
msteams: {
|
|
137
|
+
groupPolicy: "allowlist",
|
|
138
|
+
groupAllowFrom: ["alice-aad"],
|
|
139
|
+
contextVisibility: "allowlist",
|
|
140
|
+
teams: {
|
|
141
|
+
"team-1": {
|
|
142
|
+
channels: {
|
|
143
|
+
[channelConversationId]: { requireMention: false },
|
|
144
|
+
},
|
|
145
|
+
},
|
|
146
|
+
},
|
|
147
|
+
},
|
|
148
|
+
},
|
|
149
|
+
} as OpenClawConfig);
|
|
150
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
151
|
+
|
|
152
|
+
await handler({
|
|
153
|
+
activity: buildChannelActivity({
|
|
154
|
+
id: "msg-reply-1",
|
|
155
|
+
replyToId: "thread-root-123",
|
|
156
|
+
from: { id: "alice-id", aadObjectId: "alice-aad", name: "Alice" },
|
|
157
|
+
}),
|
|
158
|
+
sendActivity: vi.fn(async () => undefined),
|
|
159
|
+
} as unknown as Parameters<typeof handler>[0]);
|
|
160
|
+
|
|
161
|
+
expect(findParentSystemEventCall(enqueueSystemEvent)).toBeUndefined();
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
it("handles Graph failure gracefully without throwing or emitting a parent event", async () => {
|
|
165
|
+
fetchChannelMessageMock.mockRejectedValueOnce(new Error("graph down"));
|
|
166
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps(cfg);
|
|
167
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
168
|
+
|
|
169
|
+
await handler({
|
|
170
|
+
activity: buildChannelActivity({ id: "msg-reply-1", replyToId: "thread-root-123" }),
|
|
171
|
+
sendActivity: vi.fn(async () => undefined),
|
|
172
|
+
} as unknown as Parameters<typeof handler>[0]);
|
|
173
|
+
|
|
174
|
+
const parentCall = findParentSystemEventCall(enqueueSystemEvent);
|
|
175
|
+
expect(parentCall).toBeUndefined();
|
|
176
|
+
// Original inbound system event still fires (best-effort parent fetch does not block).
|
|
177
|
+
expect(enqueueSystemEvent).toHaveBeenCalled();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("does not fetch parent for DM replyToId", async () => {
|
|
181
|
+
fetchChannelMessageMock.mockResolvedValue({
|
|
182
|
+
id: "x",
|
|
183
|
+
from: { user: { displayName: "Alice" } },
|
|
184
|
+
body: { content: "should-not-happen", contentType: "text" },
|
|
185
|
+
});
|
|
186
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps({
|
|
187
|
+
channels: { msteams: { allowFrom: ["*"] } },
|
|
188
|
+
} as OpenClawConfig);
|
|
189
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
190
|
+
|
|
191
|
+
await handler({
|
|
192
|
+
activity: {
|
|
193
|
+
...buildChannelActivity(),
|
|
194
|
+
conversation: { id: "a:dm-conversation", conversationType: "personal" },
|
|
195
|
+
channelData: {},
|
|
196
|
+
replyToId: "dm-parent",
|
|
197
|
+
entities: [],
|
|
198
|
+
},
|
|
199
|
+
sendActivity: vi.fn(async () => undefined),
|
|
200
|
+
} as unknown as Parameters<typeof handler>[0]);
|
|
201
|
+
|
|
202
|
+
expect(fetchChannelMessageMock).not.toHaveBeenCalled();
|
|
203
|
+
expect(findParentSystemEventCall(enqueueSystemEvent)).toBeUndefined();
|
|
204
|
+
});
|
|
205
|
+
|
|
206
|
+
it("does not fetch parent for top-level channel messages without replyToId", async () => {
|
|
207
|
+
fetchChannelMessageMock.mockResolvedValue({
|
|
208
|
+
id: "x",
|
|
209
|
+
from: { user: { displayName: "Alice" } },
|
|
210
|
+
body: { content: "should-not-happen", contentType: "text" },
|
|
211
|
+
});
|
|
212
|
+
const { deps, enqueueSystemEvent } = createMessageHandlerDeps(cfg);
|
|
213
|
+
const handler = createMSTeamsMessageHandler(deps);
|
|
214
|
+
|
|
215
|
+
await handler({
|
|
216
|
+
activity: buildChannelActivity({ id: "msg-root-1", replyToId: undefined }),
|
|
217
|
+
sendActivity: vi.fn(async () => undefined),
|
|
218
|
+
} as unknown as Parameters<typeof handler>[0]);
|
|
219
|
+
|
|
220
|
+
expect(fetchChannelMessageMock).not.toHaveBeenCalled();
|
|
221
|
+
expect(findParentSystemEventCall(enqueueSystemEvent)).toBeUndefined();
|
|
222
|
+
});
|
|
223
|
+
});
|
|
@@ -0,0 +1,77 @@
|
|
|
1
|
+
import { describe, expect, it } from "vitest";
|
|
2
|
+
import { resolveMSTeamsRouteSessionKey } from "./thread-session.js";
|
|
3
|
+
|
|
4
|
+
const channelConversationSessionKey = "agent:main:msteams:channel:19:channel@thread.tacv2";
|
|
5
|
+
|
|
6
|
+
describe("msteams thread session isolation", () => {
|
|
7
|
+
it("appends thread suffix to session key for channel thread replies", async () => {
|
|
8
|
+
const sessionKey = resolveMSTeamsRouteSessionKey({
|
|
9
|
+
baseSessionKey: channelConversationSessionKey,
|
|
10
|
+
isChannel: true,
|
|
11
|
+
replyToId: "thread-root-123",
|
|
12
|
+
});
|
|
13
|
+
|
|
14
|
+
expect(sessionKey).toContain("thread:");
|
|
15
|
+
expect(sessionKey).toContain("thread-root-123");
|
|
16
|
+
});
|
|
17
|
+
|
|
18
|
+
it("does not append thread suffix for top-level channel messages", async () => {
|
|
19
|
+
const sessionKey = resolveMSTeamsRouteSessionKey({
|
|
20
|
+
baseSessionKey: channelConversationSessionKey,
|
|
21
|
+
isChannel: true,
|
|
22
|
+
replyToId: undefined,
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
expect(sessionKey).not.toContain("thread:");
|
|
26
|
+
expect(sessionKey).toBe(channelConversationSessionKey);
|
|
27
|
+
});
|
|
28
|
+
|
|
29
|
+
it("produces different session keys for different threads in the same channel", async () => {
|
|
30
|
+
const sessionKeyA = resolveMSTeamsRouteSessionKey({
|
|
31
|
+
baseSessionKey: channelConversationSessionKey,
|
|
32
|
+
isChannel: true,
|
|
33
|
+
replyToId: "thread-A",
|
|
34
|
+
});
|
|
35
|
+
const sessionKeyB = resolveMSTeamsRouteSessionKey({
|
|
36
|
+
baseSessionKey: channelConversationSessionKey,
|
|
37
|
+
isChannel: true,
|
|
38
|
+
replyToId: "thread-B",
|
|
39
|
+
});
|
|
40
|
+
|
|
41
|
+
expect(sessionKeyA).not.toBe(sessionKeyB);
|
|
42
|
+
expect(sessionKeyA).toContain("thread-a"); // normalized lowercase
|
|
43
|
+
expect(sessionKeyB).toContain("thread-b");
|
|
44
|
+
});
|
|
45
|
+
|
|
46
|
+
it("does not affect DM session keys", async () => {
|
|
47
|
+
const sessionKey = resolveMSTeamsRouteSessionKey({
|
|
48
|
+
baseSessionKey: "agent:main:msteams:dm:user-1",
|
|
49
|
+
isChannel: false,
|
|
50
|
+
replyToId: "some-reply-id",
|
|
51
|
+
});
|
|
52
|
+
|
|
53
|
+
expect(sessionKey).not.toContain("thread:");
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
it("does not affect group chat session keys", async () => {
|
|
57
|
+
const sessionKey = resolveMSTeamsRouteSessionKey({
|
|
58
|
+
baseSessionKey: "agent:main:msteams:group:19:group-chat-id@unq.gbl.spaces",
|
|
59
|
+
isChannel: false,
|
|
60
|
+
replyToId: "some-reply-id",
|
|
61
|
+
});
|
|
62
|
+
|
|
63
|
+
expect(sessionKey).not.toContain("thread:");
|
|
64
|
+
});
|
|
65
|
+
|
|
66
|
+
it("prefers conversation message id over replyToId for deep channel replies", async () => {
|
|
67
|
+
const sessionKey = resolveMSTeamsRouteSessionKey({
|
|
68
|
+
baseSessionKey: channelConversationSessionKey,
|
|
69
|
+
isChannel: true,
|
|
70
|
+
conversationMessageId: "thread-root",
|
|
71
|
+
replyToId: "nested-reply",
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
expect(sessionKey).toContain("thread-root");
|
|
75
|
+
expect(sessionKey).not.toContain("nested-reply");
|
|
76
|
+
});
|
|
77
|
+
});
|