@openclaw/msteams 2026.5.2 → 2026.5.3-beta.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 (197) hide show
  1. package/dist/api.js +3 -0
  2. package/dist/channel-D7hdreTh.js +984 -0
  3. package/dist/channel-config-api.js +2 -0
  4. package/dist/channel-plugin-api.js +2 -0
  5. package/dist/channel.runtime-BC1ruIfN.js +573 -0
  6. package/dist/config-schema-B8QezH6t.js +15 -0
  7. package/dist/contract-api.js +2 -0
  8. package/dist/graph-users-9uQJepqr.js +1354 -0
  9. package/dist/index.js +22 -0
  10. package/dist/oauth-BWJyilR1.js +114 -0
  11. package/dist/oauth.token-xxpoLWy5.js +115 -0
  12. package/dist/policy-DTnU2GR7.js +142 -0
  13. package/dist/probe-D_H8yFps.js +2194 -0
  14. package/dist/resolve-allowlist-D41JSziq.js +219 -0
  15. package/dist/runtime-api-DV1iVMn1.js +28 -0
  16. package/dist/runtime-api.js +2 -0
  17. package/dist/secret-contract-BuoEXmPS.js +35 -0
  18. package/dist/secret-contract-api.js +2 -0
  19. package/dist/setup-entry.js +15 -0
  20. package/dist/setup-plugin-api.js +64 -0
  21. package/dist/setup-surface-BLkFQYIQ.js +313 -0
  22. package/dist/src-CFp1QpFd.js +4064 -0
  23. package/dist/test-api.js +2 -0
  24. package/package.json +14 -6
  25. package/api.ts +0 -3
  26. package/channel-config-api.ts +0 -1
  27. package/channel-plugin-api.ts +0 -2
  28. package/config-api.ts +0 -4
  29. package/contract-api.ts +0 -4
  30. package/index.ts +0 -20
  31. package/runtime-api.ts +0 -73
  32. package/secret-contract-api.ts +0 -5
  33. package/setup-entry.ts +0 -13
  34. package/setup-plugin-api.ts +0 -3
  35. package/src/ai-entity.ts +0 -7
  36. package/src/approval-auth.ts +0 -44
  37. package/src/attachments/bot-framework.test.ts +0 -461
  38. package/src/attachments/bot-framework.ts +0 -362
  39. package/src/attachments/download.ts +0 -311
  40. package/src/attachments/graph.test.ts +0 -416
  41. package/src/attachments/graph.ts +0 -484
  42. package/src/attachments/html.ts +0 -122
  43. package/src/attachments/payload.ts +0 -14
  44. package/src/attachments/remote-media.test.ts +0 -137
  45. package/src/attachments/remote-media.ts +0 -112
  46. package/src/attachments/shared.test.ts +0 -530
  47. package/src/attachments/shared.ts +0 -626
  48. package/src/attachments/types.ts +0 -47
  49. package/src/attachments.graph.test.ts +0 -342
  50. package/src/attachments.helpers.test.ts +0 -246
  51. package/src/attachments.test-helpers.ts +0 -17
  52. package/src/attachments.test.ts +0 -687
  53. package/src/attachments.ts +0 -18
  54. package/src/block-streaming-config.test.ts +0 -61
  55. package/src/channel-api.ts +0 -1
  56. package/src/channel.actions.test.ts +0 -742
  57. package/src/channel.directory.test.ts +0 -200
  58. package/src/channel.runtime.ts +0 -56
  59. package/src/channel.setup.ts +0 -77
  60. package/src/channel.test.ts +0 -128
  61. package/src/channel.ts +0 -1136
  62. package/src/config-schema.ts +0 -6
  63. package/src/config-ui-hints.ts +0 -12
  64. package/src/conversation-store-fs.test.ts +0 -74
  65. package/src/conversation-store-fs.ts +0 -149
  66. package/src/conversation-store-helpers.test.ts +0 -202
  67. package/src/conversation-store-helpers.ts +0 -105
  68. package/src/conversation-store-memory.ts +0 -51
  69. package/src/conversation-store.shared.test.ts +0 -225
  70. package/src/conversation-store.ts +0 -71
  71. package/src/directory-live.test.ts +0 -156
  72. package/src/directory-live.ts +0 -111
  73. package/src/doctor.ts +0 -27
  74. package/src/errors.test.ts +0 -133
  75. package/src/errors.ts +0 -246
  76. package/src/feedback-reflection-prompt.ts +0 -117
  77. package/src/feedback-reflection-store.ts +0 -114
  78. package/src/feedback-reflection.test.ts +0 -237
  79. package/src/feedback-reflection.ts +0 -283
  80. package/src/file-consent-helpers.test.ts +0 -326
  81. package/src/file-consent-helpers.ts +0 -126
  82. package/src/file-consent-invoke.ts +0 -150
  83. package/src/file-consent.test.ts +0 -363
  84. package/src/file-consent.ts +0 -287
  85. package/src/graph-chat.ts +0 -55
  86. package/src/graph-group-management.test.ts +0 -318
  87. package/src/graph-group-management.ts +0 -168
  88. package/src/graph-members.test.ts +0 -89
  89. package/src/graph-members.ts +0 -48
  90. package/src/graph-messages.actions.test.ts +0 -243
  91. package/src/graph-messages.read.test.ts +0 -391
  92. package/src/graph-messages.search.test.ts +0 -213
  93. package/src/graph-messages.test-helpers.ts +0 -50
  94. package/src/graph-messages.ts +0 -534
  95. package/src/graph-teams.test.ts +0 -215
  96. package/src/graph-teams.ts +0 -114
  97. package/src/graph-thread.test.ts +0 -246
  98. package/src/graph-thread.ts +0 -146
  99. package/src/graph-upload.test.ts +0 -258
  100. package/src/graph-upload.ts +0 -531
  101. package/src/graph-users.ts +0 -29
  102. package/src/graph.test.ts +0 -516
  103. package/src/graph.ts +0 -293
  104. package/src/inbound.test.ts +0 -221
  105. package/src/inbound.ts +0 -148
  106. package/src/index.ts +0 -4
  107. package/src/media-helpers.test.ts +0 -202
  108. package/src/media-helpers.ts +0 -105
  109. package/src/mentions.test.ts +0 -244
  110. package/src/mentions.ts +0 -114
  111. package/src/messenger.test.ts +0 -865
  112. package/src/messenger.ts +0 -605
  113. package/src/monitor-handler/access.ts +0 -125
  114. package/src/monitor-handler/inbound-media.test.ts +0 -289
  115. package/src/monitor-handler/inbound-media.ts +0 -180
  116. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  117. package/src/monitor-handler/message-handler.authz.test.ts +0 -669
  118. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  119. package/src/monitor-handler/message-handler.test-support.ts +0 -100
  120. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
  121. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
  122. package/src/monitor-handler/message-handler.ts +0 -1000
  123. package/src/monitor-handler/reaction-handler.test.ts +0 -267
  124. package/src/monitor-handler/reaction-handler.ts +0 -210
  125. package/src/monitor-handler/thread-session.ts +0 -17
  126. package/src/monitor-handler.adaptive-card.test.ts +0 -162
  127. package/src/monitor-handler.feedback-authz.test.ts +0 -314
  128. package/src/monitor-handler.file-consent.test.ts +0 -423
  129. package/src/monitor-handler.sso.test.ts +0 -563
  130. package/src/monitor-handler.test-helpers.ts +0 -180
  131. package/src/monitor-handler.ts +0 -534
  132. package/src/monitor-handler.types.ts +0 -27
  133. package/src/monitor-types.ts +0 -6
  134. package/src/monitor.lifecycle.test.ts +0 -278
  135. package/src/monitor.test.ts +0 -119
  136. package/src/monitor.ts +0 -442
  137. package/src/oauth.flow.ts +0 -77
  138. package/src/oauth.shared.ts +0 -37
  139. package/src/oauth.test.ts +0 -305
  140. package/src/oauth.token.ts +0 -158
  141. package/src/oauth.ts +0 -130
  142. package/src/outbound.test.ts +0 -130
  143. package/src/outbound.ts +0 -71
  144. package/src/pending-uploads-fs.test.ts +0 -246
  145. package/src/pending-uploads-fs.ts +0 -235
  146. package/src/pending-uploads.test.ts +0 -173
  147. package/src/pending-uploads.ts +0 -121
  148. package/src/policy.test.ts +0 -240
  149. package/src/policy.ts +0 -262
  150. package/src/polls-store-memory.ts +0 -32
  151. package/src/polls.test.ts +0 -160
  152. package/src/polls.ts +0 -323
  153. package/src/presentation.ts +0 -68
  154. package/src/probe.test.ts +0 -77
  155. package/src/probe.ts +0 -132
  156. package/src/reply-dispatcher.test.ts +0 -437
  157. package/src/reply-dispatcher.ts +0 -346
  158. package/src/reply-stream-controller.test.ts +0 -235
  159. package/src/reply-stream-controller.ts +0 -147
  160. package/src/resolve-allowlist.test.ts +0 -250
  161. package/src/resolve-allowlist.ts +0 -309
  162. package/src/revoked-context.ts +0 -17
  163. package/src/runtime.ts +0 -9
  164. package/src/sdk-types.ts +0 -59
  165. package/src/sdk.test.ts +0 -666
  166. package/src/sdk.ts +0 -884
  167. package/src/secret-contract.ts +0 -49
  168. package/src/secret-input.ts +0 -7
  169. package/src/send-context.ts +0 -231
  170. package/src/send.test.ts +0 -493
  171. package/src/send.ts +0 -637
  172. package/src/sent-message-cache.test.ts +0 -15
  173. package/src/sent-message-cache.ts +0 -56
  174. package/src/session-route.ts +0 -40
  175. package/src/setup-core.ts +0 -160
  176. package/src/setup-surface.test.ts +0 -202
  177. package/src/setup-surface.ts +0 -320
  178. package/src/sso-token-store.test.ts +0 -72
  179. package/src/sso-token-store.ts +0 -166
  180. package/src/sso.ts +0 -300
  181. package/src/storage.ts +0 -25
  182. package/src/store-fs.ts +0 -44
  183. package/src/streaming-message.test.ts +0 -262
  184. package/src/streaming-message.ts +0 -297
  185. package/src/test-runtime.ts +0 -16
  186. package/src/thread-parent-context.test.ts +0 -224
  187. package/src/thread-parent-context.ts +0 -159
  188. package/src/token-response.ts +0 -11
  189. package/src/token.test.ts +0 -259
  190. package/src/token.ts +0 -195
  191. package/src/user-agent.test.ts +0 -86
  192. package/src/user-agent.ts +0 -53
  193. package/src/webhook-timeouts.ts +0 -27
  194. package/src/welcome-card.test.ts +0 -81
  195. package/src/welcome-card.ts +0 -57
  196. package/test-api.ts +0 -1
  197. package/tsconfig.json +0 -16
@@ -1,130 +0,0 @@
1
- import { beforeEach, describe, expect, it, vi } from "vitest";
2
- import type { OpenClawConfig } from "../runtime-api.js";
3
-
4
- const mocks = vi.hoisted(() => ({
5
- sendMessageMSTeams: vi.fn(),
6
- sendPollMSTeams: vi.fn(),
7
- createPoll: vi.fn(),
8
- }));
9
-
10
- vi.mock("./send.js", () => ({
11
- sendMessageMSTeams: mocks.sendMessageMSTeams,
12
- sendPollMSTeams: mocks.sendPollMSTeams,
13
- }));
14
-
15
- vi.mock("./polls.js", () => ({
16
- createMSTeamsPollStoreFs: () => ({
17
- createPoll: mocks.createPoll,
18
- }),
19
- }));
20
-
21
- import { msteamsOutbound } from "./outbound.js";
22
-
23
- describe("msteamsOutbound cfg threading", () => {
24
- beforeEach(() => {
25
- mocks.sendMessageMSTeams.mockReset();
26
- mocks.sendPollMSTeams.mockReset();
27
- mocks.createPoll.mockReset();
28
- mocks.sendMessageMSTeams.mockResolvedValue({
29
- messageId: "msg-1",
30
- conversationId: "conv-1",
31
- });
32
- mocks.sendPollMSTeams.mockResolvedValue({
33
- pollId: "poll-1",
34
- messageId: "msg-poll-1",
35
- conversationId: "conv-1",
36
- });
37
- mocks.createPoll.mockResolvedValue(undefined);
38
- });
39
-
40
- it("passes resolved cfg to sendMessageMSTeams for text sends", async () => {
41
- const cfg = {
42
- channels: {
43
- msteams: {
44
- appId: "resolved-app-id",
45
- },
46
- },
47
- } as OpenClawConfig;
48
-
49
- await msteamsOutbound.sendText!({
50
- cfg,
51
- to: "conversation:abc",
52
- text: "hello",
53
- });
54
-
55
- expect(mocks.sendMessageMSTeams).toHaveBeenCalledWith({
56
- cfg,
57
- to: "conversation:abc",
58
- text: "hello",
59
- });
60
- });
61
-
62
- it("passes resolved cfg and media roots for media sends", async () => {
63
- const cfg = {
64
- channels: {
65
- msteams: {
66
- appId: "resolved-app-id",
67
- },
68
- },
69
- } as OpenClawConfig;
70
-
71
- await msteamsOutbound.sendMedia!({
72
- cfg,
73
- to: "conversation:abc",
74
- text: "photo",
75
- mediaUrl: "file:///tmp/photo.png",
76
- mediaLocalRoots: ["/tmp"],
77
- });
78
-
79
- expect(mocks.sendMessageMSTeams).toHaveBeenCalledWith({
80
- cfg,
81
- to: "conversation:abc",
82
- text: "photo",
83
- mediaUrl: "file:///tmp/photo.png",
84
- mediaLocalRoots: ["/tmp"],
85
- });
86
- });
87
-
88
- it("passes resolved cfg to sendPollMSTeams and stores poll metadata", async () => {
89
- const cfg = {
90
- channels: {
91
- msteams: {
92
- appId: "resolved-app-id",
93
- },
94
- },
95
- } as OpenClawConfig;
96
-
97
- await msteamsOutbound.sendPoll!({
98
- cfg,
99
- to: "conversation:abc",
100
- poll: {
101
- question: "Snack?",
102
- options: ["Pizza", "Sushi"],
103
- },
104
- });
105
-
106
- expect(mocks.sendPollMSTeams).toHaveBeenCalledWith({
107
- cfg,
108
- to: "conversation:abc",
109
- question: "Snack?",
110
- options: ["Pizza", "Sushi"],
111
- maxSelections: 1,
112
- });
113
- expect(mocks.createPoll).toHaveBeenCalledWith(
114
- expect.objectContaining({
115
- id: "poll-1",
116
- question: "Snack?",
117
- options: ["Pizza", "Sushi"],
118
- }),
119
- );
120
- });
121
-
122
- it("chunks outbound text without requiring MSTeams runtime initialization", () => {
123
- const chunker = msteamsOutbound.chunker;
124
- if (!chunker) {
125
- throw new Error("msteams outbound.chunker unavailable");
126
- }
127
-
128
- expect(chunker("alpha beta", 5)).toEqual(["alpha", "beta"]);
129
- });
130
- });
package/src/outbound.ts DELETED
@@ -1,71 +0,0 @@
1
- import { createAttachedChannelResultAdapter } from "openclaw/plugin-sdk/channel-send-result";
2
- import { resolveOutboundSendDep } from "openclaw/plugin-sdk/outbound-send-deps";
3
- import { chunkTextForOutbound, type ChannelOutboundAdapter } from "../runtime-api.js";
4
- import { createMSTeamsPollStoreFs } from "./polls.js";
5
- import { sendMessageMSTeams, sendPollMSTeams } from "./send.js";
6
-
7
- export const msteamsOutbound: ChannelOutboundAdapter = {
8
- deliveryMode: "direct",
9
- chunker: chunkTextForOutbound,
10
- chunkerMode: "markdown",
11
- textChunkLimit: 4000,
12
- pollMaxOptions: 12,
13
- ...createAttachedChannelResultAdapter({
14
- channel: "msteams",
15
- sendText: async ({ cfg, to, text, deps }) => {
16
- type SendFn = (
17
- to: string,
18
- text: string,
19
- ) => Promise<{ messageId: string; conversationId: string }>;
20
- const send =
21
- resolveOutboundSendDep<SendFn>(deps, "msteams") ??
22
- ((to, text) => sendMessageMSTeams({ cfg, to, text }));
23
- return await send(to, text);
24
- },
25
- sendMedia: async ({ cfg, to, text, mediaUrl, mediaLocalRoots, mediaReadFile, deps }) => {
26
- type SendFn = (
27
- to: string,
28
- text: string,
29
- opts?: {
30
- mediaUrl?: string;
31
- mediaLocalRoots?: readonly string[];
32
- mediaReadFile?: (filePath: string) => Promise<Buffer>;
33
- },
34
- ) => Promise<{ messageId: string; conversationId: string }>;
35
- const send =
36
- resolveOutboundSendDep<SendFn>(deps, "msteams") ??
37
- ((to, text, opts) =>
38
- sendMessageMSTeams({
39
- cfg,
40
- to,
41
- text,
42
- mediaUrl: opts?.mediaUrl,
43
- mediaLocalRoots: opts?.mediaLocalRoots,
44
- mediaReadFile: opts?.mediaReadFile,
45
- }));
46
- return await send(to, text, { mediaUrl, mediaLocalRoots, mediaReadFile });
47
- },
48
- sendPoll: async ({ cfg, to, poll }) => {
49
- const maxSelections = poll.maxSelections ?? 1;
50
- const result = await sendPollMSTeams({
51
- cfg,
52
- to,
53
- question: poll.question,
54
- options: poll.options,
55
- maxSelections,
56
- });
57
- const pollStore = createMSTeamsPollStoreFs();
58
- await pollStore.createPoll({
59
- id: result.pollId,
60
- question: poll.question,
61
- options: poll.options,
62
- maxSelections,
63
- createdAt: new Date().toISOString(),
64
- conversationId: result.conversationId,
65
- messageId: result.messageId,
66
- votes: {},
67
- });
68
- return result;
69
- },
70
- }),
71
- };
@@ -1,246 +0,0 @@
1
- import fs from "node:fs";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { afterEach, beforeEach, describe, expect, it } from "vitest";
5
- import { prepareFileConsentActivityFs } from "./file-consent-helpers.js";
6
- import {
7
- getPendingUploadFs,
8
- removePendingUploadFs,
9
- setPendingUploadActivityIdFs,
10
- storePendingUploadFs,
11
- } from "./pending-uploads-fs.js";
12
- import { clearPendingUploads } from "./pending-uploads.js";
13
- import { setMSTeamsRuntime } from "./runtime.js";
14
- import { msteamsRuntimeStub } from "./test-runtime.js";
15
-
16
- // Track temp dirs created by each test so afterEach can clean them up.
17
- const createdTempDirs: string[] = [];
18
-
19
- async function makeTempStateDir(): Promise<string> {
20
- const dir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "openclaw-msteams-pending-"));
21
- createdTempDirs.push(dir);
22
- return dir;
23
- }
24
-
25
- function makeEnv(stateDir: string): NodeJS.ProcessEnv {
26
- return { ...process.env, OPENCLAW_STATE_DIR: stateDir };
27
- }
28
-
29
- async function cleanupTempDirs(): Promise<void> {
30
- while (createdTempDirs.length > 0) {
31
- const dir = createdTempDirs.pop();
32
- if (!dir) {
33
- continue;
34
- }
35
- try {
36
- await fs.promises.rm(dir, { recursive: true, force: true });
37
- } catch {
38
- // tmp dir may already be gone
39
- }
40
- }
41
- }
42
-
43
- describe("msteams pending uploads (fs-backed)", () => {
44
- beforeEach(() => {
45
- setMSTeamsRuntime(msteamsRuntimeStub);
46
- clearPendingUploads();
47
- });
48
-
49
- afterEach(async () => {
50
- await cleanupTempDirs();
51
- });
52
-
53
- it("stores and retrieves a pending upload by id", async () => {
54
- const stateDir = await makeTempStateDir();
55
- const env = makeEnv(stateDir);
56
-
57
- await storePendingUploadFs(
58
- {
59
- id: "upload-1",
60
- buffer: Buffer.from("hello world"),
61
- filename: "greeting.txt",
62
- contentType: "text/plain",
63
- conversationId: "19:conv@thread.v2",
64
- },
65
- { env },
66
- );
67
-
68
- const loaded = await getPendingUploadFs("upload-1", { env });
69
- expect(loaded).toBeDefined();
70
- expect(loaded?.id).toBe("upload-1");
71
- expect(loaded?.filename).toBe("greeting.txt");
72
- expect(loaded?.contentType).toBe("text/plain");
73
- expect(loaded?.conversationId).toBe("19:conv@thread.v2");
74
- expect(loaded?.buffer.toString("utf8")).toBe("hello world");
75
- });
76
-
77
- it("returns undefined for missing and undefined ids", async () => {
78
- const stateDir = await makeTempStateDir();
79
- const env = makeEnv(stateDir);
80
-
81
- expect(await getPendingUploadFs(undefined, { env })).toBeUndefined();
82
- expect(await getPendingUploadFs("does-not-exist", { env })).toBeUndefined();
83
- });
84
-
85
- it("persists so another reader finds the entry (simulates cross-process)", async () => {
86
- const stateDir = await makeTempStateDir();
87
- const env = makeEnv(stateDir);
88
-
89
- // First "process": writer
90
- await storePendingUploadFs(
91
- {
92
- id: "upload-x",
93
- buffer: Buffer.from("top secret"),
94
- filename: "secret.bin",
95
- conversationId: "19:conv@thread.v2",
96
- },
97
- { env },
98
- );
99
-
100
- // Confirm the backing file actually exists on disk with expected shape
101
- const storePath = path.join(stateDir, "msteams-pending-uploads.json");
102
- const raw = await fs.promises.readFile(storePath, "utf-8");
103
- const parsed = JSON.parse(raw) as {
104
- version: number;
105
- uploads: Record<string, { bufferBase64: string; filename: string }>;
106
- };
107
- expect(parsed.version).toBe(1);
108
- expect(parsed.uploads["upload-x"]?.filename).toBe("secret.bin");
109
- expect(Buffer.from(parsed.uploads["upload-x"].bufferBase64, "base64").toString("utf8")).toBe(
110
- "top secret",
111
- );
112
-
113
- // Second "process": reader using the same state dir
114
- const reader = await getPendingUploadFs("upload-x", { env });
115
- expect(reader?.buffer.toString("utf8")).toBe("top secret");
116
- expect(reader?.filename).toBe("secret.bin");
117
- });
118
-
119
- it("removes persisted entries", async () => {
120
- const stateDir = await makeTempStateDir();
121
- const env = makeEnv(stateDir);
122
-
123
- await storePendingUploadFs(
124
- {
125
- id: "upload-rm",
126
- buffer: Buffer.from("x"),
127
- filename: "rm.bin",
128
- conversationId: "19:conv@thread.v2",
129
- },
130
- { env },
131
- );
132
- expect(await getPendingUploadFs("upload-rm", { env })).toBeDefined();
133
-
134
- await removePendingUploadFs("upload-rm", { env });
135
- expect(await getPendingUploadFs("upload-rm", { env })).toBeUndefined();
136
- });
137
-
138
- it("remove is a no-op for unknown ids", async () => {
139
- const stateDir = await makeTempStateDir();
140
- const env = makeEnv(stateDir);
141
-
142
- await expect(removePendingUploadFs("never-existed", { env })).resolves.toBeUndefined();
143
- await expect(removePendingUploadFs(undefined, { env })).resolves.toBeUndefined();
144
- });
145
-
146
- it("expires entries past their ttl on read", async () => {
147
- const stateDir = await makeTempStateDir();
148
- const env = makeEnv(stateDir);
149
-
150
- await storePendingUploadFs(
151
- {
152
- id: "upload-old",
153
- buffer: Buffer.from("stale"),
154
- filename: "stale.txt",
155
- conversationId: "19:conv@thread.v2",
156
- },
157
- { env, ttlMs: 1 },
158
- );
159
- // Wait past ttl
160
- await new Promise((resolve) => setTimeout(resolve, 10));
161
- expect(await getPendingUploadFs("upload-old", { env, ttlMs: 1 })).toBeUndefined();
162
- });
163
-
164
- it("updates consent card activity id on an existing entry", async () => {
165
- const stateDir = await makeTempStateDir();
166
- const env = makeEnv(stateDir);
167
-
168
- await storePendingUploadFs(
169
- {
170
- id: "upload-a",
171
- buffer: Buffer.from("payload"),
172
- filename: "f.txt",
173
- conversationId: "19:conv@thread.v2",
174
- },
175
- { env },
176
- );
177
-
178
- await setPendingUploadActivityIdFs("upload-a", "activity-xyz", { env });
179
- const loaded = await getPendingUploadFs("upload-a", { env });
180
- expect(loaded?.consentCardActivityId).toBe("activity-xyz");
181
- });
182
-
183
- it("ignores malformed or empty store files and returns undefined", async () => {
184
- const stateDir = await makeTempStateDir();
185
- const env = makeEnv(stateDir);
186
- const storePath = path.join(stateDir, "msteams-pending-uploads.json");
187
- await fs.promises.writeFile(storePath, "not valid json", "utf-8");
188
-
189
- // Should not throw and should treat as empty
190
- expect(await getPendingUploadFs("anything", { env })).toBeUndefined();
191
-
192
- await fs.promises.writeFile(storePath, JSON.stringify({ version: 2, uploads: {} }), "utf-8");
193
- expect(await getPendingUploadFs("anything", { env })).toBeUndefined();
194
- });
195
- });
196
-
197
- describe("prepareFileConsentActivityFs end-to-end", () => {
198
- beforeEach(() => {
199
- setMSTeamsRuntime(msteamsRuntimeStub);
200
- clearPendingUploads();
201
- });
202
-
203
- afterEach(async () => {
204
- await cleanupTempDirs();
205
- });
206
-
207
- it("writes the pending upload to the fs store with the same id as the card", async () => {
208
- const stateDir = await makeTempStateDir();
209
- const env = makeEnv(stateDir);
210
- // Redirect state dir via env so the helper's FS writes land under our tmp
211
- const originalEnv = process.env.OPENCLAW_STATE_DIR;
212
- process.env.OPENCLAW_STATE_DIR = stateDir;
213
-
214
- try {
215
- const result = await prepareFileConsentActivityFs({
216
- media: {
217
- buffer: Buffer.from("cli file"),
218
- filename: "cli.bin",
219
- contentType: "application/octet-stream",
220
- },
221
- conversationId: "19:victim@thread.v2",
222
- description: "Sent via CLI",
223
- });
224
-
225
- expect(result.uploadId).toMatch(/[0-9a-f-]/);
226
- const attachments = result.activity.attachments as Array<Record<string, unknown>>;
227
- expect(attachments).toHaveLength(1);
228
- const content = attachments[0]?.content as { acceptContext: { uploadId: string } };
229
- expect(content.acceptContext.uploadId).toBe(result.uploadId);
230
-
231
- // Reader in (simulated) other process finds the entry under the same key
232
- const loaded = await getPendingUploadFs(result.uploadId, { env });
233
- expect(loaded).toBeDefined();
234
- expect(loaded?.filename).toBe("cli.bin");
235
- expect(loaded?.contentType).toBe("application/octet-stream");
236
- expect(loaded?.conversationId).toBe("19:victim@thread.v2");
237
- expect(loaded?.buffer.toString("utf8")).toBe("cli file");
238
- } finally {
239
- if (originalEnv === undefined) {
240
- delete process.env.OPENCLAW_STATE_DIR;
241
- } else {
242
- process.env.OPENCLAW_STATE_DIR = originalEnv;
243
- }
244
- }
245
- });
246
- });
@@ -1,235 +0,0 @@
1
- /**
2
- * Filesystem-backed pending upload store for the FileConsentCard flow.
3
- *
4
- * The CLI `message send --media` path runs in a different process from the
5
- * gateway's bot monitor that receives the `fileConsent/invoke` callback.
6
- * An in-memory `pending-uploads.ts` store cannot bridge those processes, so
7
- * when the user clicks "Allow" the monitor handler's lookup misses and the
8
- * user sees "card action not supported".
9
- *
10
- * This FS store persists pending uploads to a JSON file (with the file buffer
11
- * base64-encoded) so any process that shares the OpenClaw state dir can read
12
- * them back. The in-memory store in `pending-uploads.ts` is still the fast
13
- * path for same-process flows (for example the messenger reply path); this FS
14
- * store is a cross-process fallback.
15
- */
16
-
17
- import { resolveMSTeamsStorePath } from "./storage.js";
18
- import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
19
-
20
- /** TTL for persisted pending uploads (matches in-memory store). */
21
- const PENDING_UPLOAD_TTL_MS = 5 * 60 * 1000;
22
-
23
- /** Cap to avoid unbounded growth if a process crashes mid-flow. */
24
- const MAX_PENDING_UPLOADS = 100;
25
-
26
- const STORE_FILENAME = "msteams-pending-uploads.json";
27
-
28
- type PendingUploadFsRecord = {
29
- id: string;
30
- bufferBase64: string;
31
- filename: string;
32
- contentType?: string;
33
- conversationId: string;
34
- /** Activity ID of the original FileConsentCard, used to replace it after upload */
35
- consentCardActivityId?: string;
36
- createdAt: number;
37
- };
38
-
39
- type PendingUploadFs = {
40
- id: string;
41
- buffer: Buffer;
42
- filename: string;
43
- contentType?: string;
44
- conversationId: string;
45
- consentCardActivityId?: string;
46
- createdAt: number;
47
- };
48
-
49
- type PendingUploadStoreData = {
50
- version: 1;
51
- uploads: Record<string, PendingUploadFsRecord>;
52
- };
53
-
54
- const empty: PendingUploadStoreData = { version: 1, uploads: {} };
55
-
56
- type PendingUploadsFsOptions = {
57
- env?: NodeJS.ProcessEnv;
58
- homedir?: () => string;
59
- stateDir?: string;
60
- storePath?: string;
61
- ttlMs?: number;
62
- };
63
-
64
- function resolveFilePath(options: PendingUploadsFsOptions | undefined): string {
65
- return resolveMSTeamsStorePath({
66
- filename: STORE_FILENAME,
67
- env: options?.env,
68
- homedir: options?.homedir,
69
- stateDir: options?.stateDir,
70
- storePath: options?.storePath,
71
- });
72
- }
73
-
74
- function pruneExpired(
75
- uploads: Record<string, PendingUploadFsRecord>,
76
- nowMs: number,
77
- ttlMs: number,
78
- ): Record<string, PendingUploadFsRecord> {
79
- const kept: Record<string, PendingUploadFsRecord> = {};
80
- for (const [id, record] of Object.entries(uploads)) {
81
- if (nowMs - record.createdAt <= ttlMs) {
82
- kept[id] = record;
83
- }
84
- }
85
- return kept;
86
- }
87
-
88
- function pruneToLimit(
89
- uploads: Record<string, PendingUploadFsRecord>,
90
- ): Record<string, PendingUploadFsRecord> {
91
- const entries = Object.entries(uploads);
92
- if (entries.length <= MAX_PENDING_UPLOADS) {
93
- return uploads;
94
- }
95
- // Oldest createdAt first; drop the oldest until we fit.
96
- entries.sort((a, b) => a[1].createdAt - b[1].createdAt);
97
- const keep = entries.slice(entries.length - MAX_PENDING_UPLOADS);
98
- return Object.fromEntries(keep);
99
- }
100
-
101
- function recordToUpload(record: PendingUploadFsRecord): PendingUploadFs {
102
- return {
103
- id: record.id,
104
- buffer: Buffer.from(record.bufferBase64, "base64"),
105
- filename: record.filename,
106
- contentType: record.contentType,
107
- conversationId: record.conversationId,
108
- consentCardActivityId: record.consentCardActivityId,
109
- createdAt: record.createdAt,
110
- };
111
- }
112
-
113
- function isValidStore(value: unknown): value is PendingUploadStoreData {
114
- if (!value || typeof value !== "object") {
115
- return false;
116
- }
117
- const candidate = value as Partial<PendingUploadStoreData>;
118
- return (
119
- candidate.version === 1 &&
120
- typeof candidate.uploads === "object" &&
121
- candidate.uploads !== null &&
122
- !Array.isArray(candidate.uploads)
123
- );
124
- }
125
-
126
- async function readStore(filePath: string, ttlMs: number): Promise<PendingUploadStoreData> {
127
- const { value } = await readJsonFile<unknown>(filePath, empty);
128
- if (!isValidStore(value)) {
129
- return { version: 1, uploads: {} };
130
- }
131
- const uploads = pruneToLimit(pruneExpired(value.uploads, Date.now(), ttlMs));
132
- return { version: 1, uploads };
133
- }
134
-
135
- /**
136
- * Persist a pending upload record so another process can read it back.
137
- * Pass in the pre-generated id (same as the one placed in the consent card
138
- * context) so the in-memory and FS stores share the same key.
139
- */
140
- export async function storePendingUploadFs(
141
- upload: {
142
- id: string;
143
- buffer: Buffer;
144
- filename: string;
145
- contentType?: string;
146
- conversationId: string;
147
- consentCardActivityId?: string;
148
- },
149
- options?: PendingUploadsFsOptions,
150
- ): Promise<void> {
151
- const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
152
- const filePath = resolveFilePath(options);
153
- await withFileLock(filePath, empty, async () => {
154
- const store = await readStore(filePath, ttlMs);
155
- store.uploads[upload.id] = {
156
- id: upload.id,
157
- bufferBase64: upload.buffer.toString("base64"),
158
- filename: upload.filename,
159
- contentType: upload.contentType,
160
- conversationId: upload.conversationId,
161
- consentCardActivityId: upload.consentCardActivityId,
162
- createdAt: Date.now(),
163
- };
164
- store.uploads = pruneToLimit(pruneExpired(store.uploads, Date.now(), ttlMs));
165
- await writeJsonFile(filePath, store);
166
- });
167
- }
168
-
169
- /**
170
- * Retrieve a persisted pending upload. Expired entries are treated as absent.
171
- */
172
- export async function getPendingUploadFs(
173
- id: string | undefined,
174
- options?: PendingUploadsFsOptions,
175
- ): Promise<PendingUploadFs | undefined> {
176
- if (!id) {
177
- return undefined;
178
- }
179
- const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
180
- const filePath = resolveFilePath(options);
181
- const store = await readStore(filePath, ttlMs);
182
- const record = store.uploads[id];
183
- if (!record) {
184
- return undefined;
185
- }
186
- if (Date.now() - record.createdAt > ttlMs) {
187
- return undefined;
188
- }
189
- return recordToUpload(record);
190
- }
191
-
192
- /**
193
- * Remove a persisted pending upload (after successful upload or decline).
194
- * No-op if the entry is already gone.
195
- */
196
- export async function removePendingUploadFs(
197
- id: string | undefined,
198
- options?: PendingUploadsFsOptions,
199
- ): Promise<void> {
200
- if (!id) {
201
- return;
202
- }
203
- const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
204
- const filePath = resolveFilePath(options);
205
- await withFileLock(filePath, empty, async () => {
206
- const store = await readStore(filePath, ttlMs);
207
- if (!(id in store.uploads)) {
208
- return;
209
- }
210
- delete store.uploads[id];
211
- await writeJsonFile(filePath, store);
212
- });
213
- }
214
-
215
- /**
216
- * Set the consent card activity ID on a persisted entry. Called after the
217
- * FileConsentCard activity is sent and we know its message id.
218
- */
219
- export async function setPendingUploadActivityIdFs(
220
- id: string,
221
- activityId: string,
222
- options?: PendingUploadsFsOptions,
223
- ): Promise<void> {
224
- const ttlMs = options?.ttlMs ?? PENDING_UPLOAD_TTL_MS;
225
- const filePath = resolveFilePath(options);
226
- await withFileLock(filePath, empty, async () => {
227
- const store = await readStore(filePath, ttlMs);
228
- const record = store.uploads[id];
229
- if (!record) {
230
- return;
231
- }
232
- record.consentCardActivityId = activityId;
233
- await writeJsonFile(filePath, store);
234
- });
235
- }