@kodelyth/msteams 2026.5.39 → 2026.5.42
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/dist/api.js +3 -0
- package/dist/channel-BvTXHuGs.js +1161 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-NssGKZm5.js +650 -0
- package/dist/config-schema-Btk-XCOd.js +43 -0
- package/dist/contract-api.js +2 -0
- package/dist/graph-users-D-gKCguI.js +1411 -0
- package/dist/index.js +22 -0
- package/dist/oauth-BUxlphX3.js +114 -0
- package/dist/oauth.token-ebId9946.js +116 -0
- package/dist/probe-Cj2KsAGF.js +2190 -0
- package/dist/runtime-api-BL4DOWXD.js +28 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-Bo7kdUrT.js +35 -0
- package/dist/secret-contract-api.js +2 -0
- package/dist/setup-entry.js +15 -0
- package/dist/setup-plugin-api.js +64 -0
- package/dist/setup-surface-COTQDcTQ.js +531 -0
- package/dist/src-tvpsGYPV.js +4226 -0
- package/dist/test-api.js +2 -0
- package/index.ts +20 -0
- package/klaw.plugin.json +2 -726
- package/package.json +4 -4
- package/runtime-api.ts +66 -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 +506 -0
- package/src/attachments/bot-framework.ts +348 -0
- package/src/attachments/download.ts +328 -0
- package/src/attachments/graph.test.ts +441 -0
- package/src/attachments/graph.ts +489 -0
- package/src/attachments/html.ts +122 -0
- package/src/attachments/payload.ts +14 -0
- package/src/attachments/remote-media.test.ts +187 -0
- package/src/attachments/remote-media.ts +86 -0
- package/src/attachments/shared.test.ts +547 -0
- package/src/attachments/shared.ts +655 -0
- package/src/attachments/types.ts +47 -0
- package/src/attachments.graph.test.ts +414 -0
- package/src/attachments.helpers.test.ts +245 -0
- package/src/attachments.test-helpers.ts +17 -0
- package/src/attachments.test.ts +754 -0
- package/src/attachments.ts +18 -0
- package/src/block-streaming-config.test.ts +61 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.actions.test.ts +797 -0
- package/src/channel.directory.test.ts +176 -0
- package/src/channel.message-adapter.test.ts +227 -0
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -0
- package/src/channel.test.ts +136 -0
- package/src/channel.ts +1176 -0
- package/src/config-schema.ts +6 -0
- package/src/config-ui-hints.ts +40 -0
- package/src/conversation-store-fs.test.ts +81 -0
- package/src/conversation-store-fs.ts +149 -0
- package/src/conversation-store-helpers.test.ts +202 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +51 -0
- package/src/conversation-store.shared.test.ts +260 -0
- package/src/conversation-store.ts +71 -0
- package/src/directory-live.test.ts +156 -0
- package/src/directory-live.ts +111 -0
- package/src/doctor.ts +27 -0
- package/src/errors.test.ts +154 -0
- package/src/errors.ts +270 -0
- package/src/feedback-reflection-prompt.ts +117 -0
- package/src/feedback-reflection-store.ts +113 -0
- package/src/feedback-reflection.test.ts +237 -0
- package/src/feedback-reflection.ts +268 -0
- package/src/file-consent-helpers.test.ts +328 -0
- package/src/file-consent-helpers.ts +115 -0
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.test.ts +378 -0
- package/src/file-consent.ts +223 -0
- package/src/graph-chat.ts +36 -0
- package/src/graph-group-management.test.ts +332 -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 +253 -0
- package/src/graph-messages.read.test.ts +391 -0
- package/src/graph-messages.search.test.ts +227 -0
- package/src/graph-messages.test-helpers.ts +50 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.test.ts +222 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.test.ts +252 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.test.ts +253 -0
- package/src/graph-upload.ts +531 -0
- package/src/graph-users.ts +29 -0
- package/src/graph.test.ts +540 -0
- package/src/graph.ts +308 -0
- package/src/inbound.test.ts +221 -0
- package/src/inbound.ts +148 -0
- package/src/index.ts +4 -0
- package/src/media-helpers.test.ts +220 -0
- package/src/media-helpers.ts +105 -0
- package/src/mentions.test.ts +254 -0
- package/src/mentions.ts +114 -0
- package/src/messenger.test.ts +961 -0
- package/src/messenger.ts +608 -0
- package/src/monitor-handler/access.ts +136 -0
- package/src/monitor-handler/inbound-media.test.ts +314 -0
- package/src/monitor-handler/inbound-media.ts +180 -0
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +28 -0
- package/src/monitor-handler/message-handler.authz.test.ts +739 -0
- package/src/monitor-handler/message-handler.dm-media.test.ts +54 -0
- package/src/monitor-handler/message-handler.test-support.ts +99 -0
- package/src/monitor-handler/message-handler.thread-parent.test.ts +225 -0
- package/src/monitor-handler/message-handler.thread-session.test.ts +132 -0
- package/src/monitor-handler/message-handler.ts +1003 -0
- package/src/monitor-handler/reaction-handler.test.ts +325 -0
- package/src/monitor-handler/reaction-handler.ts +122 -0
- package/src/monitor-handler/thread-session.ts +30 -0
- package/src/monitor-handler.adaptive-card.test.ts +158 -0
- package/src/monitor-handler.feedback-authz.test.ts +357 -0
- package/src/monitor-handler.file-consent.test.ts +443 -0
- package/src/monitor-handler.sso.test.ts +576 -0
- package/src/monitor-handler.test-helpers.ts +181 -0
- package/src/monitor-handler.ts +538 -0
- package/src/monitor-handler.types.ts +27 -0
- package/src/monitor-types.ts +6 -0
- package/src/monitor.lifecycle.test.ts +457 -0
- package/src/monitor.test.ts +119 -0
- package/src/monitor.ts +476 -0
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.test.ts +350 -0
- package/src/oauth.token.ts +162 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.test.ts +400 -0
- package/src/outbound.ts +198 -0
- package/src/pending-uploads-fs.test.ts +261 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.test.ts +186 -0
- package/src/pending-uploads.ts +121 -0
- package/src/policy.test.ts +156 -0
- package/src/policy.ts +245 -0
- package/src/polls-store-memory.ts +32 -0
- package/src/polls.test.ts +169 -0
- package/src/polls.ts +312 -0
- package/src/presentation.ts +93 -0
- package/src/probe.test.ts +79 -0
- package/src/probe.ts +132 -0
- package/src/reply-dispatcher.test.ts +543 -0
- package/src/reply-dispatcher.ts +523 -0
- package/src/reply-stream-controller.test.ts +424 -0
- package/src/reply-stream-controller.ts +334 -0
- package/src/resolve-allowlist.test.ts +253 -0
- package/src/resolve-allowlist.ts +309 -0
- package/src/revoked-context.ts +17 -0
- package/src/runtime.ts +12 -0
- package/src/sdk-types.ts +59 -0
- package/src/sdk.test.ts +727 -0
- package/src/sdk.ts +916 -0
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +7 -0
- package/src/send-context.test.ts +93 -0
- package/src/send-context.ts +269 -0
- package/src/send.test.ts +588 -0
- package/src/send.ts +697 -0
- package/src/sent-message-cache.test.ts +106 -0
- package/src/sent-message-cache.ts +174 -0
- package/src/session-route.ts +40 -0
- package/src/setup-core.ts +162 -0
- package/src/setup-surface.test.ts +175 -0
- package/src/setup-surface.ts +319 -0
- package/src/sso-token-store.test.ts +74 -0
- package/src/sso-token-store.ts +166 -0
- package/src/sso.ts +300 -0
- package/src/storage.ts +25 -0
- package/src/store-fs.ts +42 -0
- package/src/streaming-message.test.ts +323 -0
- package/src/streaming-message.ts +327 -0
- package/src/test-runtime.ts +16 -0
- package/src/thread-parent-context.test.ts +224 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token-response.ts +11 -0
- package/src/token.test.ts +268 -0
- package/src/token.ts +194 -0
- package/src/user-agent.test.ts +121 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.test.ts +104 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
- package/api.js +0 -7
- package/channel-config-api.js +0 -7
- package/channel-plugin-api.js +0 -7
- package/contract-api.js +0 -7
- package/index.js +0 -7
- package/runtime-api.js +0 -7
- package/secret-contract-api.js +0 -7
- package/setup-entry.js +0 -7
- package/setup-plugin-api.js +0 -7
- package/test-api.js +0 -7
|
@@ -0,0 +1,261 @@
|
|
|
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, vi } 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(), "klaw-msteams-pending-"));
|
|
21
|
+
createdTempDirs.push(dir);
|
|
22
|
+
return dir;
|
|
23
|
+
}
|
|
24
|
+
|
|
25
|
+
function makeEnv(stateDir: string): NodeJS.ProcessEnv {
|
|
26
|
+
return { ...process.env, KLAW_STATE_DIR: stateDir };
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
async function requirePendingUpload(id: string, env: NodeJS.ProcessEnv) {
|
|
30
|
+
const upload = await getPendingUploadFs(id, { env });
|
|
31
|
+
if (!upload) {
|
|
32
|
+
throw new Error(`expected pending upload ${id}`);
|
|
33
|
+
}
|
|
34
|
+
return upload;
|
|
35
|
+
}
|
|
36
|
+
|
|
37
|
+
async function cleanupTempDirs(): Promise<void> {
|
|
38
|
+
while (createdTempDirs.length > 0) {
|
|
39
|
+
const dir = createdTempDirs.pop();
|
|
40
|
+
if (!dir) {
|
|
41
|
+
continue;
|
|
42
|
+
}
|
|
43
|
+
try {
|
|
44
|
+
await fs.promises.rm(dir, { recursive: true, force: true });
|
|
45
|
+
} catch {
|
|
46
|
+
// tmp dir may already be gone
|
|
47
|
+
}
|
|
48
|
+
}
|
|
49
|
+
}
|
|
50
|
+
|
|
51
|
+
describe("msteams pending uploads (fs-backed)", () => {
|
|
52
|
+
beforeEach(() => {
|
|
53
|
+
setMSTeamsRuntime(msteamsRuntimeStub);
|
|
54
|
+
clearPendingUploads();
|
|
55
|
+
});
|
|
56
|
+
|
|
57
|
+
afterEach(async () => {
|
|
58
|
+
await cleanupTempDirs();
|
|
59
|
+
vi.useRealTimers();
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
it("stores and retrieves a pending upload by id", async () => {
|
|
63
|
+
const stateDir = await makeTempStateDir();
|
|
64
|
+
const env = makeEnv(stateDir);
|
|
65
|
+
|
|
66
|
+
await storePendingUploadFs(
|
|
67
|
+
{
|
|
68
|
+
id: "upload-1",
|
|
69
|
+
buffer: Buffer.from("hello world"),
|
|
70
|
+
filename: "greeting.txt",
|
|
71
|
+
contentType: "text/plain",
|
|
72
|
+
conversationId: "19:conv@thread.v2",
|
|
73
|
+
},
|
|
74
|
+
{ env },
|
|
75
|
+
);
|
|
76
|
+
|
|
77
|
+
const loaded = await requirePendingUpload("upload-1", env);
|
|
78
|
+
expect(loaded.id).toBe("upload-1");
|
|
79
|
+
expect(loaded.filename).toBe("greeting.txt");
|
|
80
|
+
expect(loaded.contentType).toBe("text/plain");
|
|
81
|
+
expect(loaded.conversationId).toBe("19:conv@thread.v2");
|
|
82
|
+
expect(loaded.buffer.toString("utf8")).toBe("hello world");
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
it("returns undefined for missing and undefined ids", async () => {
|
|
86
|
+
const stateDir = await makeTempStateDir();
|
|
87
|
+
const env = makeEnv(stateDir);
|
|
88
|
+
|
|
89
|
+
expect(await getPendingUploadFs(undefined, { env })).toBeUndefined();
|
|
90
|
+
expect(await getPendingUploadFs("does-not-exist", { env })).toBeUndefined();
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
it("persists so another reader finds the entry (simulates cross-process)", async () => {
|
|
94
|
+
const stateDir = await makeTempStateDir();
|
|
95
|
+
const env = makeEnv(stateDir);
|
|
96
|
+
|
|
97
|
+
// First "process": writer
|
|
98
|
+
await storePendingUploadFs(
|
|
99
|
+
{
|
|
100
|
+
id: "upload-x",
|
|
101
|
+
buffer: Buffer.from("top secret"),
|
|
102
|
+
filename: "secret.bin",
|
|
103
|
+
conversationId: "19:conv@thread.v2",
|
|
104
|
+
},
|
|
105
|
+
{ env },
|
|
106
|
+
);
|
|
107
|
+
|
|
108
|
+
// Confirm the backing file actually exists on disk with expected shape
|
|
109
|
+
const storePath = path.join(stateDir, "msteams-pending-uploads.json");
|
|
110
|
+
const raw = await fs.promises.readFile(storePath, "utf-8");
|
|
111
|
+
const parsed = JSON.parse(raw) as {
|
|
112
|
+
version: number;
|
|
113
|
+
uploads: Record<string, { bufferBase64: string; filename: string }>;
|
|
114
|
+
};
|
|
115
|
+
expect(parsed.version).toBe(1);
|
|
116
|
+
expect(parsed.uploads["upload-x"]?.filename).toBe("secret.bin");
|
|
117
|
+
expect(Buffer.from(parsed.uploads["upload-x"].bufferBase64, "base64").toString("utf8")).toBe(
|
|
118
|
+
"top secret",
|
|
119
|
+
);
|
|
120
|
+
|
|
121
|
+
// Second "process": reader using the same state dir
|
|
122
|
+
const reader = await getPendingUploadFs("upload-x", { env });
|
|
123
|
+
expect(reader?.buffer.toString("utf8")).toBe("top secret");
|
|
124
|
+
expect(reader?.filename).toBe("secret.bin");
|
|
125
|
+
});
|
|
126
|
+
|
|
127
|
+
it("removes persisted entries", async () => {
|
|
128
|
+
const stateDir = await makeTempStateDir();
|
|
129
|
+
const env = makeEnv(stateDir);
|
|
130
|
+
|
|
131
|
+
await storePendingUploadFs(
|
|
132
|
+
{
|
|
133
|
+
id: "upload-rm",
|
|
134
|
+
buffer: Buffer.from("x"),
|
|
135
|
+
filename: "rm.bin",
|
|
136
|
+
conversationId: "19:conv@thread.v2",
|
|
137
|
+
},
|
|
138
|
+
{ env },
|
|
139
|
+
);
|
|
140
|
+
const loaded = await requirePendingUpload("upload-rm", env);
|
|
141
|
+
expect(loaded.id).toBe("upload-rm");
|
|
142
|
+
expect(loaded.filename).toBe("rm.bin");
|
|
143
|
+
expect(loaded.contentType).toBeUndefined();
|
|
144
|
+
expect(loaded.conversationId).toBe("19:conv@thread.v2");
|
|
145
|
+
expect(loaded.consentCardActivityId).toBeUndefined();
|
|
146
|
+
expect(loaded.buffer.toString("utf8")).toBe("x");
|
|
147
|
+
expect(Number.isFinite(loaded.createdAt)).toBe(true);
|
|
148
|
+
|
|
149
|
+
await removePendingUploadFs("upload-rm", { env });
|
|
150
|
+
expect(await getPendingUploadFs("upload-rm", { env })).toBeUndefined();
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
it("remove is a no-op for unknown ids", async () => {
|
|
154
|
+
const stateDir = await makeTempStateDir();
|
|
155
|
+
const env = makeEnv(stateDir);
|
|
156
|
+
|
|
157
|
+
await expect(removePendingUploadFs("never-existed", { env })).resolves.toBeUndefined();
|
|
158
|
+
await expect(removePendingUploadFs(undefined, { env })).resolves.toBeUndefined();
|
|
159
|
+
});
|
|
160
|
+
|
|
161
|
+
it("expires entries past their ttl on read", async () => {
|
|
162
|
+
const stateDir = await makeTempStateDir();
|
|
163
|
+
const env = makeEnv(stateDir);
|
|
164
|
+
const now = new Date("2026-05-08T00:00:00.000Z");
|
|
165
|
+
vi.useFakeTimers({ now });
|
|
166
|
+
|
|
167
|
+
await storePendingUploadFs(
|
|
168
|
+
{
|
|
169
|
+
id: "upload-old",
|
|
170
|
+
buffer: Buffer.from("stale"),
|
|
171
|
+
filename: "stale.txt",
|
|
172
|
+
conversationId: "19:conv@thread.v2",
|
|
173
|
+
},
|
|
174
|
+
{ env, ttlMs: 1 },
|
|
175
|
+
);
|
|
176
|
+
vi.setSystemTime(now.getTime() + 2);
|
|
177
|
+
expect(await getPendingUploadFs("upload-old", { env, ttlMs: 1 })).toBeUndefined();
|
|
178
|
+
});
|
|
179
|
+
|
|
180
|
+
it("updates consent card activity id on an existing entry", async () => {
|
|
181
|
+
const stateDir = await makeTempStateDir();
|
|
182
|
+
const env = makeEnv(stateDir);
|
|
183
|
+
|
|
184
|
+
await storePendingUploadFs(
|
|
185
|
+
{
|
|
186
|
+
id: "upload-a",
|
|
187
|
+
buffer: Buffer.from("payload"),
|
|
188
|
+
filename: "f.txt",
|
|
189
|
+
conversationId: "19:conv@thread.v2",
|
|
190
|
+
},
|
|
191
|
+
{ env },
|
|
192
|
+
);
|
|
193
|
+
|
|
194
|
+
await setPendingUploadActivityIdFs("upload-a", "activity-xyz", { env });
|
|
195
|
+
const loaded = await getPendingUploadFs("upload-a", { env });
|
|
196
|
+
expect(loaded?.consentCardActivityId).toBe("activity-xyz");
|
|
197
|
+
});
|
|
198
|
+
|
|
199
|
+
it("ignores malformed or empty store files and returns undefined", async () => {
|
|
200
|
+
const stateDir = await makeTempStateDir();
|
|
201
|
+
const env = makeEnv(stateDir);
|
|
202
|
+
const storePath = path.join(stateDir, "msteams-pending-uploads.json");
|
|
203
|
+
await fs.promises.writeFile(storePath, "not valid json", "utf-8");
|
|
204
|
+
|
|
205
|
+
// Should not throw and should treat as empty
|
|
206
|
+
expect(await getPendingUploadFs("anything", { env })).toBeUndefined();
|
|
207
|
+
|
|
208
|
+
await fs.promises.writeFile(storePath, JSON.stringify({ version: 2, uploads: {} }), "utf-8");
|
|
209
|
+
expect(await getPendingUploadFs("anything", { env })).toBeUndefined();
|
|
210
|
+
});
|
|
211
|
+
});
|
|
212
|
+
|
|
213
|
+
describe("prepareFileConsentActivityFs end-to-end", () => {
|
|
214
|
+
beforeEach(() => {
|
|
215
|
+
setMSTeamsRuntime(msteamsRuntimeStub);
|
|
216
|
+
clearPendingUploads();
|
|
217
|
+
});
|
|
218
|
+
|
|
219
|
+
afterEach(async () => {
|
|
220
|
+
await cleanupTempDirs();
|
|
221
|
+
});
|
|
222
|
+
|
|
223
|
+
it("writes the pending upload to the fs store with the same id as the card", async () => {
|
|
224
|
+
const stateDir = await makeTempStateDir();
|
|
225
|
+
const env = makeEnv(stateDir);
|
|
226
|
+
// Redirect state dir via env so the helper's FS writes land under our tmp
|
|
227
|
+
const originalEnv = process.env.KLAW_STATE_DIR;
|
|
228
|
+
process.env.KLAW_STATE_DIR = stateDir;
|
|
229
|
+
|
|
230
|
+
try {
|
|
231
|
+
const result = await prepareFileConsentActivityFs({
|
|
232
|
+
media: {
|
|
233
|
+
buffer: Buffer.from("cli file"),
|
|
234
|
+
filename: "cli.bin",
|
|
235
|
+
contentType: "application/octet-stream",
|
|
236
|
+
},
|
|
237
|
+
conversationId: "19:victim@thread.v2",
|
|
238
|
+
description: "Sent via CLI",
|
|
239
|
+
});
|
|
240
|
+
|
|
241
|
+
expect(result.uploadId).toMatch(/[0-9a-f-]/);
|
|
242
|
+
const attachments = result.activity.attachments as Array<Record<string, unknown>>;
|
|
243
|
+
expect(attachments).toHaveLength(1);
|
|
244
|
+
const content = attachments[0]?.content as { acceptContext: { uploadId: string } };
|
|
245
|
+
expect(content.acceptContext.uploadId).toBe(result.uploadId);
|
|
246
|
+
|
|
247
|
+
// Reader in (simulated) other process finds the entry under the same key
|
|
248
|
+
const loaded = await requirePendingUpload(result.uploadId, env);
|
|
249
|
+
expect(loaded.filename).toBe("cli.bin");
|
|
250
|
+
expect(loaded.contentType).toBe("application/octet-stream");
|
|
251
|
+
expect(loaded.conversationId).toBe("19:victim@thread.v2");
|
|
252
|
+
expect(loaded.buffer.toString("utf8")).toBe("cli file");
|
|
253
|
+
} finally {
|
|
254
|
+
if (originalEnv === undefined) {
|
|
255
|
+
delete process.env.KLAW_STATE_DIR;
|
|
256
|
+
} else {
|
|
257
|
+
process.env.KLAW_STATE_DIR = originalEnv;
|
|
258
|
+
}
|
|
259
|
+
}
|
|
260
|
+
});
|
|
261
|
+
});
|
|
@@ -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 Klaw 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
|
+
}
|
|
@@ -0,0 +1,186 @@
|
|
|
1
|
+
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
+
import {
|
|
3
|
+
clearPendingUploads,
|
|
4
|
+
getPendingUpload,
|
|
5
|
+
getPendingUploadCount,
|
|
6
|
+
removePendingUpload,
|
|
7
|
+
setPendingUploadActivityId,
|
|
8
|
+
storePendingUpload,
|
|
9
|
+
} from "./pending-uploads.js";
|
|
10
|
+
|
|
11
|
+
function requirePendingUpload(id: string) {
|
|
12
|
+
const upload = getPendingUpload(id);
|
|
13
|
+
if (!upload) {
|
|
14
|
+
throw new Error(`expected pending upload ${id}`);
|
|
15
|
+
}
|
|
16
|
+
return upload;
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
describe("pending-uploads", () => {
|
|
20
|
+
beforeEach(() => {
|
|
21
|
+
vi.useFakeTimers();
|
|
22
|
+
clearPendingUploads();
|
|
23
|
+
});
|
|
24
|
+
|
|
25
|
+
afterEach(() => {
|
|
26
|
+
clearPendingUploads();
|
|
27
|
+
vi.useRealTimers();
|
|
28
|
+
});
|
|
29
|
+
|
|
30
|
+
describe("storePendingUpload", () => {
|
|
31
|
+
it("stores and retrieves a pending upload", () => {
|
|
32
|
+
const id = storePendingUpload({
|
|
33
|
+
buffer: Buffer.from("data"),
|
|
34
|
+
filename: "file.txt",
|
|
35
|
+
contentType: "text/plain",
|
|
36
|
+
conversationId: "conv-1",
|
|
37
|
+
});
|
|
38
|
+
|
|
39
|
+
const upload = requirePendingUpload(id);
|
|
40
|
+
expect(upload.id).toBe(id);
|
|
41
|
+
expect(upload.buffer.toString()).toBe("data");
|
|
42
|
+
expect(upload.filename).toBe("file.txt");
|
|
43
|
+
expect(upload.contentType).toBe("text/plain");
|
|
44
|
+
expect(upload.conversationId).toBe("conv-1");
|
|
45
|
+
expect(upload.createdAt).toBe(Date.now());
|
|
46
|
+
});
|
|
47
|
+
|
|
48
|
+
it("stores consentCardActivityId when provided", () => {
|
|
49
|
+
const id = storePendingUpload({
|
|
50
|
+
buffer: Buffer.from("data"),
|
|
51
|
+
filename: "file.txt",
|
|
52
|
+
conversationId: "conv-1",
|
|
53
|
+
consentCardActivityId: "activity-abc",
|
|
54
|
+
});
|
|
55
|
+
|
|
56
|
+
const upload = getPendingUpload(id);
|
|
57
|
+
expect(upload?.consentCardActivityId).toBe("activity-abc");
|
|
58
|
+
});
|
|
59
|
+
|
|
60
|
+
it("stores without consentCardActivityId when not provided", () => {
|
|
61
|
+
const id = storePendingUpload({
|
|
62
|
+
buffer: Buffer.from("data"),
|
|
63
|
+
filename: "file.txt",
|
|
64
|
+
conversationId: "conv-1",
|
|
65
|
+
});
|
|
66
|
+
|
|
67
|
+
const upload = getPendingUpload(id);
|
|
68
|
+
expect(upload?.consentCardActivityId).toBeUndefined();
|
|
69
|
+
});
|
|
70
|
+
|
|
71
|
+
it("auto-removes entry after TTL expires", () => {
|
|
72
|
+
const id = storePendingUpload({
|
|
73
|
+
buffer: Buffer.from("data"),
|
|
74
|
+
filename: "file.txt",
|
|
75
|
+
conversationId: "conv-1",
|
|
76
|
+
});
|
|
77
|
+
|
|
78
|
+
expect(requirePendingUpload(id).filename).toBe("file.txt");
|
|
79
|
+
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
|
|
80
|
+
// After TTL the in-memory check also gates access
|
|
81
|
+
expect(getPendingUpload(id)).toBeUndefined();
|
|
82
|
+
});
|
|
83
|
+
});
|
|
84
|
+
|
|
85
|
+
describe("removePendingUpload", () => {
|
|
86
|
+
it("removes the entry immediately", () => {
|
|
87
|
+
const id = storePendingUpload({
|
|
88
|
+
buffer: Buffer.from("data"),
|
|
89
|
+
filename: "file.txt",
|
|
90
|
+
conversationId: "conv-1",
|
|
91
|
+
});
|
|
92
|
+
|
|
93
|
+
removePendingUpload(id);
|
|
94
|
+
expect(getPendingUpload(id)).toBeUndefined();
|
|
95
|
+
});
|
|
96
|
+
|
|
97
|
+
it("clears the TTL timer so it does not fire after explicit removal", () => {
|
|
98
|
+
const id = storePendingUpload({
|
|
99
|
+
buffer: Buffer.from("data"),
|
|
100
|
+
filename: "file.txt",
|
|
101
|
+
conversationId: "conv-1",
|
|
102
|
+
});
|
|
103
|
+
|
|
104
|
+
expect(getPendingUploadCount()).toBe(1);
|
|
105
|
+
removePendingUpload(id);
|
|
106
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
107
|
+
|
|
108
|
+
// Advance past TTL — timer should have been cleared and count stays 0
|
|
109
|
+
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
|
|
110
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
111
|
+
});
|
|
112
|
+
|
|
113
|
+
it("leaves existing uploads untouched for undefined id", () => {
|
|
114
|
+
storePendingUpload({
|
|
115
|
+
buffer: Buffer.from("data"),
|
|
116
|
+
filename: "file.txt",
|
|
117
|
+
conversationId: "conv-1",
|
|
118
|
+
});
|
|
119
|
+
|
|
120
|
+
removePendingUpload(undefined);
|
|
121
|
+
expect(getPendingUploadCount()).toBe(1);
|
|
122
|
+
});
|
|
123
|
+
|
|
124
|
+
it("leaves the store empty for unknown ids", () => {
|
|
125
|
+
removePendingUpload("non-existent-id");
|
|
126
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
127
|
+
});
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
describe("clearPendingUploads", () => {
|
|
131
|
+
it("removes all entries and cancels timers", () => {
|
|
132
|
+
storePendingUpload({ buffer: Buffer.from("a"), filename: "a.txt", conversationId: "c1" });
|
|
133
|
+
storePendingUpload({ buffer: Buffer.from("b"), filename: "b.txt", conversationId: "c2" });
|
|
134
|
+
expect(getPendingUploadCount()).toBe(2);
|
|
135
|
+
|
|
136
|
+
clearPendingUploads();
|
|
137
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
138
|
+
|
|
139
|
+
// TTL timers should have been cleared — no side-effects after advance
|
|
140
|
+
vi.advanceTimersByTime(5 * 60 * 1000 + 1);
|
|
141
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
142
|
+
});
|
|
143
|
+
});
|
|
144
|
+
|
|
145
|
+
describe("setPendingUploadActivityId", () => {
|
|
146
|
+
it("sets the consentCardActivityId on an existing upload", () => {
|
|
147
|
+
const id = storePendingUpload({
|
|
148
|
+
buffer: Buffer.from("data"),
|
|
149
|
+
filename: "file.txt",
|
|
150
|
+
conversationId: "conv-1",
|
|
151
|
+
});
|
|
152
|
+
|
|
153
|
+
expect(getPendingUpload(id)?.consentCardActivityId).toBeUndefined();
|
|
154
|
+
|
|
155
|
+
setPendingUploadActivityId(id, "activity-xyz");
|
|
156
|
+
expect(getPendingUpload(id)?.consentCardActivityId).toBe("activity-xyz");
|
|
157
|
+
});
|
|
158
|
+
|
|
159
|
+
it("leaves the store empty for unknown upload ids", () => {
|
|
160
|
+
setPendingUploadActivityId("non-existent", "activity-xyz");
|
|
161
|
+
expect(getPendingUploadCount()).toBe(0);
|
|
162
|
+
});
|
|
163
|
+
});
|
|
164
|
+
|
|
165
|
+
describe("getPendingUpload", () => {
|
|
166
|
+
it("returns undefined for undefined id", () => {
|
|
167
|
+
expect(getPendingUpload(undefined)).toBeUndefined();
|
|
168
|
+
});
|
|
169
|
+
|
|
170
|
+
it("returns undefined for unknown id", () => {
|
|
171
|
+
expect(getPendingUpload("no-such-id")).toBeUndefined();
|
|
172
|
+
});
|
|
173
|
+
|
|
174
|
+
it("returns undefined when entry is past TTL but timer has not yet fired", () => {
|
|
175
|
+
const id = storePendingUpload({
|
|
176
|
+
buffer: Buffer.from("data"),
|
|
177
|
+
filename: "file.txt",
|
|
178
|
+
conversationId: "conv-1",
|
|
179
|
+
});
|
|
180
|
+
|
|
181
|
+
// Manually advance time without firing timers to simulate stale entry
|
|
182
|
+
vi.setSystemTime(Date.now() + 5 * 60 * 1000 + 1);
|
|
183
|
+
expect(getPendingUpload(id)).toBeUndefined();
|
|
184
|
+
});
|
|
185
|
+
});
|
|
186
|
+
});
|