@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
package/src/outbound.ts
CHANGED
|
@@ -1,53 +1,71 @@
|
|
|
1
|
-
import
|
|
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";
|
|
2
4
|
import { createMSTeamsPollStoreFs } from "./polls.js";
|
|
3
|
-
import { getMSTeamsRuntime } from "./runtime.js";
|
|
4
5
|
import { sendMessageMSTeams, sendPollMSTeams } from "./send.js";
|
|
5
6
|
|
|
6
7
|
export const msteamsOutbound: ChannelOutboundAdapter = {
|
|
7
8
|
deliveryMode: "direct",
|
|
8
|
-
chunker:
|
|
9
|
+
chunker: chunkTextForOutbound,
|
|
9
10
|
chunkerMode: "markdown",
|
|
10
11
|
textChunkLimit: 4000,
|
|
11
12
|
pollMaxOptions: 12,
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
sendMessageMSTeams({
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
38
|
-
|
|
39
|
-
|
|
40
|
-
|
|
41
|
-
|
|
42
|
-
|
|
43
|
-
|
|
44
|
-
|
|
45
|
-
|
|
46
|
-
|
|
47
|
-
|
|
48
|
-
|
|
49
|
-
|
|
50
|
-
|
|
51
|
-
|
|
52
|
-
|
|
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
|
+
}),
|
|
53
71
|
};
|
|
@@ -0,0 +1,246 @@
|
|
|
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
|
+
});
|
|
@@ -0,0 +1,235 @@
|
|
|
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
|
+
}
|