@kodelyth/msteams 2026.5.42 → 2026.6.2
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/klaw.plugin.json +726 -2
- package/package.json +18 -6
- package/api.ts +0 -3
- package/channel-config-api.ts +0 -1
- package/channel-plugin-api.ts +0 -2
- package/config-api.ts +0 -4
- package/contract-api.ts +0 -4
- package/index.ts +0 -20
- package/runtime-api.ts +0 -66
- package/secret-contract-api.ts +0 -5
- package/setup-entry.ts +0 -13
- package/setup-plugin-api.ts +0 -3
- package/src/ai-entity.ts +0 -7
- package/src/approval-auth.ts +0 -44
- package/src/attachments/bot-framework.test.ts +0 -506
- package/src/attachments/bot-framework.ts +0 -348
- package/src/attachments/download.ts +0 -328
- package/src/attachments/graph.test.ts +0 -441
- package/src/attachments/graph.ts +0 -489
- package/src/attachments/html.ts +0 -122
- package/src/attachments/payload.ts +0 -14
- package/src/attachments/remote-media.test.ts +0 -187
- package/src/attachments/remote-media.ts +0 -86
- package/src/attachments/shared.test.ts +0 -547
- package/src/attachments/shared.ts +0 -655
- package/src/attachments/types.ts +0 -47
- package/src/attachments.graph.test.ts +0 -414
- package/src/attachments.helpers.test.ts +0 -245
- package/src/attachments.test-helpers.ts +0 -17
- package/src/attachments.test.ts +0 -754
- package/src/attachments.ts +0 -18
- package/src/block-streaming-config.test.ts +0 -61
- package/src/channel-api.ts +0 -1
- package/src/channel.actions.test.ts +0 -797
- package/src/channel.directory.test.ts +0 -176
- package/src/channel.message-adapter.test.ts +0 -227
- package/src/channel.runtime.ts +0 -56
- package/src/channel.setup.ts +0 -77
- package/src/channel.test.ts +0 -136
- package/src/channel.ts +0 -1176
- package/src/config-schema.ts +0 -6
- package/src/config-ui-hints.ts +0 -40
- package/src/conversation-store-fs.test.ts +0 -81
- package/src/conversation-store-fs.ts +0 -149
- package/src/conversation-store-helpers.test.ts +0 -202
- package/src/conversation-store-helpers.ts +0 -105
- package/src/conversation-store-memory.ts +0 -51
- package/src/conversation-store.shared.test.ts +0 -260
- package/src/conversation-store.ts +0 -71
- package/src/directory-live.test.ts +0 -156
- package/src/directory-live.ts +0 -111
- package/src/doctor.ts +0 -27
- package/src/errors.test.ts +0 -154
- package/src/errors.ts +0 -270
- package/src/feedback-reflection-prompt.ts +0 -117
- package/src/feedback-reflection-store.ts +0 -113
- package/src/feedback-reflection.test.ts +0 -237
- package/src/feedback-reflection.ts +0 -268
- package/src/file-consent-helpers.test.ts +0 -328
- package/src/file-consent-helpers.ts +0 -115
- package/src/file-consent-invoke.ts +0 -150
- package/src/file-consent.test.ts +0 -378
- package/src/file-consent.ts +0 -223
- package/src/graph-chat.ts +0 -36
- package/src/graph-group-management.test.ts +0 -332
- package/src/graph-group-management.ts +0 -168
- package/src/graph-members.test.ts +0 -89
- package/src/graph-members.ts +0 -48
- package/src/graph-messages.actions.test.ts +0 -253
- package/src/graph-messages.read.test.ts +0 -391
- package/src/graph-messages.search.test.ts +0 -227
- package/src/graph-messages.test-helpers.ts +0 -50
- package/src/graph-messages.ts +0 -534
- package/src/graph-teams.test.ts +0 -222
- package/src/graph-teams.ts +0 -114
- package/src/graph-thread.test.ts +0 -252
- package/src/graph-thread.ts +0 -146
- package/src/graph-upload.test.ts +0 -253
- package/src/graph-upload.ts +0 -531
- package/src/graph-users.ts +0 -29
- package/src/graph.test.ts +0 -540
- package/src/graph.ts +0 -308
- package/src/inbound.test.ts +0 -221
- package/src/inbound.ts +0 -148
- package/src/index.ts +0 -4
- package/src/media-helpers.test.ts +0 -220
- package/src/media-helpers.ts +0 -105
- package/src/mentions.test.ts +0 -254
- package/src/mentions.ts +0 -114
- package/src/messenger.test.ts +0 -961
- package/src/messenger.ts +0 -608
- package/src/monitor-handler/access.ts +0 -136
- package/src/monitor-handler/inbound-media.test.ts +0 -314
- package/src/monitor-handler/inbound-media.ts +0 -180
- package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
- package/src/monitor-handler/message-handler.authz.test.ts +0 -739
- package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
- package/src/monitor-handler/message-handler.test-support.ts +0 -99
- package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
- package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
- package/src/monitor-handler/message-handler.ts +0 -1003
- package/src/monitor-handler/reaction-handler.test.ts +0 -325
- package/src/monitor-handler/reaction-handler.ts +0 -122
- package/src/monitor-handler/thread-session.ts +0 -30
- package/src/monitor-handler.adaptive-card.test.ts +0 -158
- package/src/monitor-handler.feedback-authz.test.ts +0 -357
- package/src/monitor-handler.file-consent.test.ts +0 -443
- package/src/monitor-handler.sso.test.ts +0 -576
- package/src/monitor-handler.test-helpers.ts +0 -181
- package/src/monitor-handler.ts +0 -538
- package/src/monitor-handler.types.ts +0 -27
- package/src/monitor-types.ts +0 -6
- package/src/monitor.lifecycle.test.ts +0 -457
- package/src/monitor.test.ts +0 -119
- package/src/monitor.ts +0 -476
- package/src/oauth.flow.ts +0 -77
- package/src/oauth.shared.ts +0 -37
- package/src/oauth.test.ts +0 -350
- package/src/oauth.token.ts +0 -162
- package/src/oauth.ts +0 -130
- package/src/outbound.test.ts +0 -400
- package/src/outbound.ts +0 -198
- package/src/pending-uploads-fs.test.ts +0 -261
- package/src/pending-uploads-fs.ts +0 -235
- package/src/pending-uploads.test.ts +0 -186
- package/src/pending-uploads.ts +0 -121
- package/src/policy.test.ts +0 -156
- package/src/policy.ts +0 -245
- package/src/polls-store-memory.ts +0 -32
- package/src/polls.test.ts +0 -169
- package/src/polls.ts +0 -312
- package/src/presentation.ts +0 -93
- package/src/probe.test.ts +0 -79
- package/src/probe.ts +0 -132
- package/src/reply-dispatcher.test.ts +0 -543
- package/src/reply-dispatcher.ts +0 -523
- package/src/reply-stream-controller.test.ts +0 -424
- package/src/reply-stream-controller.ts +0 -334
- package/src/resolve-allowlist.test.ts +0 -253
- package/src/resolve-allowlist.ts +0 -309
- package/src/revoked-context.ts +0 -17
- package/src/runtime.ts +0 -12
- package/src/sdk-types.ts +0 -59
- package/src/sdk.test.ts +0 -727
- package/src/sdk.ts +0 -916
- package/src/secret-contract.ts +0 -49
- package/src/secret-input.ts +0 -7
- package/src/send-context.test.ts +0 -93
- package/src/send-context.ts +0 -269
- package/src/send.test.ts +0 -588
- package/src/send.ts +0 -697
- package/src/sent-message-cache.test.ts +0 -106
- package/src/sent-message-cache.ts +0 -174
- package/src/session-route.ts +0 -40
- package/src/setup-core.ts +0 -162
- package/src/setup-surface.test.ts +0 -175
- package/src/setup-surface.ts +0 -319
- package/src/sso-token-store.test.ts +0 -74
- package/src/sso-token-store.ts +0 -166
- package/src/sso.ts +0 -300
- package/src/storage.ts +0 -25
- package/src/store-fs.ts +0 -42
- package/src/streaming-message.test.ts +0 -323
- package/src/streaming-message.ts +0 -327
- package/src/test-runtime.ts +0 -16
- package/src/thread-parent-context.test.ts +0 -224
- package/src/thread-parent-context.ts +0 -159
- package/src/token-response.ts +0 -11
- package/src/token.test.ts +0 -268
- package/src/token.ts +0 -194
- package/src/user-agent.test.ts +0 -121
- package/src/user-agent.ts +0 -53
- package/src/webhook-timeouts.ts +0 -27
- package/src/welcome-card.test.ts +0 -104
- package/src/welcome-card.ts +0 -57
- package/test-api.ts +0 -1
- package/tsconfig.json +0 -16
package/src/polls.test.ts
DELETED
|
@@ -1,169 +0,0 @@
|
|
|
1
|
-
import fs from "node:fs";
|
|
2
|
-
import os from "node:os";
|
|
3
|
-
import path from "node:path";
|
|
4
|
-
import { beforeEach, describe, expect, it } from "vitest";
|
|
5
|
-
import { createMSTeamsPollStoreMemory } from "./polls-store-memory.js";
|
|
6
|
-
import { buildMSTeamsPollCard, createMSTeamsPollStoreFs, extractMSTeamsPollVote } from "./polls.js";
|
|
7
|
-
import { setMSTeamsRuntime } from "./runtime.js";
|
|
8
|
-
import { msteamsRuntimeStub } from "./test-runtime.js";
|
|
9
|
-
|
|
10
|
-
describe("msteams polls", () => {
|
|
11
|
-
beforeEach(() => {
|
|
12
|
-
setMSTeamsRuntime(msteamsRuntimeStub);
|
|
13
|
-
});
|
|
14
|
-
|
|
15
|
-
it("builds poll cards with fallback text", () => {
|
|
16
|
-
const card = buildMSTeamsPollCard({
|
|
17
|
-
question: "Lunch?",
|
|
18
|
-
options: ["Pizza", "Sushi"],
|
|
19
|
-
});
|
|
20
|
-
|
|
21
|
-
expect(card.pollId).toMatch(
|
|
22
|
-
/^[0-9a-f]{8}-[0-9a-f]{4}-[1-8][0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$/i,
|
|
23
|
-
);
|
|
24
|
-
expect(card.fallbackText).toBe("Poll: Lunch?\n1. Pizza\n2. Sushi");
|
|
25
|
-
});
|
|
26
|
-
|
|
27
|
-
it("extracts poll votes from activity values", () => {
|
|
28
|
-
const vote = extractMSTeamsPollVote({
|
|
29
|
-
value: {
|
|
30
|
-
klawPollId: "poll-1",
|
|
31
|
-
choices: "0,1",
|
|
32
|
-
},
|
|
33
|
-
});
|
|
34
|
-
|
|
35
|
-
expect(vote).toEqual({
|
|
36
|
-
pollId: "poll-1",
|
|
37
|
-
selections: ["0", "1"],
|
|
38
|
-
});
|
|
39
|
-
});
|
|
40
|
-
|
|
41
|
-
it("stores and records poll votes", async () => {
|
|
42
|
-
const home = await fs.promises.mkdtemp(path.join(os.tmpdir(), "klaw-msteams-polls-"));
|
|
43
|
-
const store = createMSTeamsPollStoreFs({ homedir: () => home });
|
|
44
|
-
await store.createPoll({
|
|
45
|
-
id: "poll-2",
|
|
46
|
-
question: "Pick one",
|
|
47
|
-
options: ["A", "B"],
|
|
48
|
-
maxSelections: 1,
|
|
49
|
-
createdAt: new Date().toISOString(),
|
|
50
|
-
votes: {},
|
|
51
|
-
});
|
|
52
|
-
await store.recordVote({
|
|
53
|
-
pollId: "poll-2",
|
|
54
|
-
voterId: "user-1",
|
|
55
|
-
selections: ["0", "1"],
|
|
56
|
-
});
|
|
57
|
-
const stored = await store.getPoll("poll-2");
|
|
58
|
-
if (!stored) {
|
|
59
|
-
throw new Error("expected stored poll after recordVote");
|
|
60
|
-
}
|
|
61
|
-
expect(stored.votes["user-1"]).toEqual(["0"]);
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
const createFsStore = async () => {
|
|
66
|
-
const stateDir = await fs.promises.mkdtemp(path.join(os.tmpdir(), "klaw-msteams-polls-"));
|
|
67
|
-
return createMSTeamsPollStoreFs({ stateDir });
|
|
68
|
-
};
|
|
69
|
-
|
|
70
|
-
const createMemoryStore = () => createMSTeamsPollStoreMemory();
|
|
71
|
-
|
|
72
|
-
describe.each([
|
|
73
|
-
{ name: "memory", createStore: createMemoryStore },
|
|
74
|
-
{ name: "fs", createStore: createFsStore },
|
|
75
|
-
])("$name poll store", ({ createStore }) => {
|
|
76
|
-
it("stores polls and records normalized votes", async () => {
|
|
77
|
-
const store = await createStore();
|
|
78
|
-
await store.createPoll({
|
|
79
|
-
id: "poll-1",
|
|
80
|
-
question: "Lunch?",
|
|
81
|
-
options: ["Pizza", "Sushi"],
|
|
82
|
-
maxSelections: 1,
|
|
83
|
-
createdAt: new Date().toISOString(),
|
|
84
|
-
votes: {},
|
|
85
|
-
});
|
|
86
|
-
|
|
87
|
-
const poll = await store.recordVote({
|
|
88
|
-
pollId: "poll-1",
|
|
89
|
-
voterId: "user-1",
|
|
90
|
-
selections: ["0", "1"],
|
|
91
|
-
});
|
|
92
|
-
|
|
93
|
-
if (!poll) {
|
|
94
|
-
throw new Error("poll store did not return the updated poll");
|
|
95
|
-
}
|
|
96
|
-
expect(poll.votes["user-1"]).toEqual(["0"]);
|
|
97
|
-
});
|
|
98
|
-
});
|
|
99
|
-
|
|
100
|
-
describe("memory poll store", () => {
|
|
101
|
-
it("reads seeded polls back, updates timestamps, and returns null for missing polls", async () => {
|
|
102
|
-
const store = createMSTeamsPollStoreMemory([
|
|
103
|
-
{
|
|
104
|
-
id: "poll-1",
|
|
105
|
-
question: "Pick one",
|
|
106
|
-
options: ["A", "B"],
|
|
107
|
-
maxSelections: 1,
|
|
108
|
-
votes: {},
|
|
109
|
-
createdAt: "2026-03-22T00:00:00.000Z",
|
|
110
|
-
updatedAt: "2026-03-22T00:00:00.000Z",
|
|
111
|
-
},
|
|
112
|
-
]);
|
|
113
|
-
|
|
114
|
-
await expect(store.getPoll("poll-1")).resolves.toEqual({
|
|
115
|
-
id: "poll-1",
|
|
116
|
-
question: "Pick one",
|
|
117
|
-
options: ["A", "B"],
|
|
118
|
-
maxSelections: 1,
|
|
119
|
-
votes: {},
|
|
120
|
-
createdAt: "2026-03-22T00:00:00.000Z",
|
|
121
|
-
updatedAt: "2026-03-22T00:00:00.000Z",
|
|
122
|
-
});
|
|
123
|
-
|
|
124
|
-
const originalUpdatedAt = "2026-03-22T00:00:00.000Z";
|
|
125
|
-
const result = await store.recordVote({
|
|
126
|
-
pollId: "poll-1",
|
|
127
|
-
voterId: "user-1",
|
|
128
|
-
selections: ["1", "0", "missing"],
|
|
129
|
-
});
|
|
130
|
-
|
|
131
|
-
expect(result?.votes["user-1"]).toEqual(["1"]);
|
|
132
|
-
expect(result?.updatedAt).not.toBe(originalUpdatedAt);
|
|
133
|
-
|
|
134
|
-
await store.createPoll({
|
|
135
|
-
id: "poll-2",
|
|
136
|
-
question: "Pick many",
|
|
137
|
-
options: ["X", "Y"],
|
|
138
|
-
maxSelections: 2,
|
|
139
|
-
votes: {},
|
|
140
|
-
createdAt: "2026-03-22T00:00:00.000Z",
|
|
141
|
-
updatedAt: "2026-03-22T00:00:00.000Z",
|
|
142
|
-
});
|
|
143
|
-
|
|
144
|
-
const updatedPoll = await store.recordVote({
|
|
145
|
-
pollId: "poll-2",
|
|
146
|
-
voterId: "user-2",
|
|
147
|
-
selections: ["1", "0", "1"],
|
|
148
|
-
});
|
|
149
|
-
if (!updatedPoll?.updatedAt) {
|
|
150
|
-
throw new Error("expected updated poll timestamp after recordVote");
|
|
151
|
-
}
|
|
152
|
-
const { updatedAt, ...stableUpdatedPoll } = updatedPoll;
|
|
153
|
-
expect(typeof updatedAt).toBe("string");
|
|
154
|
-
expect(stableUpdatedPoll).toEqual({
|
|
155
|
-
id: "poll-2",
|
|
156
|
-
question: "Pick many",
|
|
157
|
-
options: ["X", "Y"],
|
|
158
|
-
maxSelections: 2,
|
|
159
|
-
votes: {
|
|
160
|
-
"user-2": ["1", "0"],
|
|
161
|
-
},
|
|
162
|
-
createdAt: "2026-03-22T00:00:00.000Z",
|
|
163
|
-
});
|
|
164
|
-
|
|
165
|
-
await expect(
|
|
166
|
-
store.recordVote({ pollId: "missing", voterId: "nobody", selections: ["x"] }),
|
|
167
|
-
).resolves.toBeNull();
|
|
168
|
-
});
|
|
169
|
-
});
|
package/src/polls.ts
DELETED
|
@@ -1,312 +0,0 @@
|
|
|
1
|
-
import crypto from "node:crypto";
|
|
2
|
-
import { isRecord, normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
3
|
-
import { resolveMSTeamsStorePath } from "./storage.js";
|
|
4
|
-
import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
|
|
5
|
-
|
|
6
|
-
type MSTeamsPollVote = {
|
|
7
|
-
pollId: string;
|
|
8
|
-
selections: string[];
|
|
9
|
-
};
|
|
10
|
-
|
|
11
|
-
export type MSTeamsPoll = {
|
|
12
|
-
id: string;
|
|
13
|
-
question: string;
|
|
14
|
-
options: string[];
|
|
15
|
-
maxSelections: number;
|
|
16
|
-
createdAt: string;
|
|
17
|
-
updatedAt?: string;
|
|
18
|
-
conversationId?: string;
|
|
19
|
-
messageId?: string;
|
|
20
|
-
votes: Record<string, string[]>;
|
|
21
|
-
};
|
|
22
|
-
|
|
23
|
-
export type MSTeamsPollStore = {
|
|
24
|
-
createPoll: (poll: MSTeamsPoll) => Promise<void>;
|
|
25
|
-
getPoll: (pollId: string) => Promise<MSTeamsPoll | null>;
|
|
26
|
-
recordVote: (params: {
|
|
27
|
-
pollId: string;
|
|
28
|
-
voterId: string;
|
|
29
|
-
selections: string[];
|
|
30
|
-
}) => Promise<MSTeamsPoll | null>;
|
|
31
|
-
};
|
|
32
|
-
|
|
33
|
-
type MSTeamsPollCard = {
|
|
34
|
-
pollId: string;
|
|
35
|
-
question: string;
|
|
36
|
-
options: string[];
|
|
37
|
-
maxSelections: number;
|
|
38
|
-
card: Record<string, unknown>;
|
|
39
|
-
fallbackText: string;
|
|
40
|
-
};
|
|
41
|
-
|
|
42
|
-
type PollStoreData = {
|
|
43
|
-
version: 1;
|
|
44
|
-
polls: Record<string, MSTeamsPoll>;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
const STORE_FILENAME = "msteams-polls.json";
|
|
48
|
-
const MAX_POLLS = 1000;
|
|
49
|
-
const POLL_TTL_MS = 30 * 24 * 60 * 60 * 1000;
|
|
50
|
-
|
|
51
|
-
function normalizeChoiceValue(value: unknown): string | null {
|
|
52
|
-
if (typeof value === "string") {
|
|
53
|
-
const trimmed = value.trim();
|
|
54
|
-
return trimmed ? trimmed : null;
|
|
55
|
-
}
|
|
56
|
-
if (typeof value === "number" && Number.isFinite(value)) {
|
|
57
|
-
return String(value);
|
|
58
|
-
}
|
|
59
|
-
return null;
|
|
60
|
-
}
|
|
61
|
-
|
|
62
|
-
function extractSelections(value: unknown): string[] {
|
|
63
|
-
if (Array.isArray(value)) {
|
|
64
|
-
return value.map(normalizeChoiceValue).filter((entry): entry is string => Boolean(entry));
|
|
65
|
-
}
|
|
66
|
-
const normalized = normalizeChoiceValue(value);
|
|
67
|
-
if (!normalized) {
|
|
68
|
-
return [];
|
|
69
|
-
}
|
|
70
|
-
if (normalized.includes(",")) {
|
|
71
|
-
return normalized
|
|
72
|
-
.split(",")
|
|
73
|
-
.map((entry) => entry.trim())
|
|
74
|
-
.filter(Boolean);
|
|
75
|
-
}
|
|
76
|
-
return [normalized];
|
|
77
|
-
}
|
|
78
|
-
|
|
79
|
-
function readNestedValue(value: unknown, keys: Array<string | number>): unknown {
|
|
80
|
-
let current: unknown = value;
|
|
81
|
-
for (const key of keys) {
|
|
82
|
-
if (!isRecord(current)) {
|
|
83
|
-
return undefined;
|
|
84
|
-
}
|
|
85
|
-
current = current[key as keyof typeof current];
|
|
86
|
-
}
|
|
87
|
-
return current;
|
|
88
|
-
}
|
|
89
|
-
|
|
90
|
-
function readNestedString(value: unknown, keys: Array<string | number>): string | undefined {
|
|
91
|
-
return normalizeOptionalString(readNestedValue(value, keys));
|
|
92
|
-
}
|
|
93
|
-
|
|
94
|
-
export function extractMSTeamsPollVote(
|
|
95
|
-
activity: { value?: unknown } | undefined,
|
|
96
|
-
): MSTeamsPollVote | null {
|
|
97
|
-
const value = activity?.value;
|
|
98
|
-
if (!value || !isRecord(value)) {
|
|
99
|
-
return null;
|
|
100
|
-
}
|
|
101
|
-
const pollId =
|
|
102
|
-
readNestedString(value, ["klawPollId"]) ??
|
|
103
|
-
readNestedString(value, ["pollId"]) ??
|
|
104
|
-
readNestedString(value, ["klaw", "pollId"]) ??
|
|
105
|
-
readNestedString(value, ["klaw", "poll", "id"]) ??
|
|
106
|
-
readNestedString(value, ["data", "klawPollId"]) ??
|
|
107
|
-
readNestedString(value, ["data", "pollId"]) ??
|
|
108
|
-
readNestedString(value, ["data", "klaw", "pollId"]);
|
|
109
|
-
if (!pollId) {
|
|
110
|
-
return null;
|
|
111
|
-
}
|
|
112
|
-
|
|
113
|
-
const directSelections = extractSelections(value.choices);
|
|
114
|
-
const nestedSelections = extractSelections(readNestedValue(value, ["choices"]));
|
|
115
|
-
const dataSelections = extractSelections(readNestedValue(value, ["data", "choices"]));
|
|
116
|
-
const selections =
|
|
117
|
-
directSelections.length > 0
|
|
118
|
-
? directSelections
|
|
119
|
-
: nestedSelections.length > 0
|
|
120
|
-
? nestedSelections
|
|
121
|
-
: dataSelections;
|
|
122
|
-
|
|
123
|
-
if (selections.length === 0) {
|
|
124
|
-
return null;
|
|
125
|
-
}
|
|
126
|
-
|
|
127
|
-
return {
|
|
128
|
-
pollId,
|
|
129
|
-
selections,
|
|
130
|
-
};
|
|
131
|
-
}
|
|
132
|
-
|
|
133
|
-
export function buildMSTeamsPollCard(params: {
|
|
134
|
-
question: string;
|
|
135
|
-
options: string[];
|
|
136
|
-
maxSelections?: number;
|
|
137
|
-
pollId?: string;
|
|
138
|
-
}): MSTeamsPollCard {
|
|
139
|
-
const pollId = params.pollId ?? crypto.randomUUID();
|
|
140
|
-
const maxSelections =
|
|
141
|
-
typeof params.maxSelections === "number" && params.maxSelections > 1
|
|
142
|
-
? Math.floor(params.maxSelections)
|
|
143
|
-
: 1;
|
|
144
|
-
const cappedMaxSelections = Math.min(Math.max(1, maxSelections), params.options.length);
|
|
145
|
-
const choices = params.options.map((option, index) => ({
|
|
146
|
-
title: option,
|
|
147
|
-
value: String(index),
|
|
148
|
-
}));
|
|
149
|
-
const hint =
|
|
150
|
-
cappedMaxSelections > 1
|
|
151
|
-
? `Select up to ${cappedMaxSelections} option${cappedMaxSelections === 1 ? "" : "s"}.`
|
|
152
|
-
: "Select one option.";
|
|
153
|
-
|
|
154
|
-
const card = {
|
|
155
|
-
type: "AdaptiveCard",
|
|
156
|
-
version: "1.5",
|
|
157
|
-
body: [
|
|
158
|
-
{
|
|
159
|
-
type: "TextBlock",
|
|
160
|
-
text: params.question,
|
|
161
|
-
wrap: true,
|
|
162
|
-
weight: "Bolder",
|
|
163
|
-
size: "Medium",
|
|
164
|
-
},
|
|
165
|
-
{
|
|
166
|
-
type: "Input.ChoiceSet",
|
|
167
|
-
id: "choices",
|
|
168
|
-
isMultiSelect: cappedMaxSelections > 1,
|
|
169
|
-
style: "expanded",
|
|
170
|
-
choices,
|
|
171
|
-
},
|
|
172
|
-
{
|
|
173
|
-
type: "TextBlock",
|
|
174
|
-
text: hint,
|
|
175
|
-
wrap: true,
|
|
176
|
-
isSubtle: true,
|
|
177
|
-
spacing: "Small",
|
|
178
|
-
},
|
|
179
|
-
],
|
|
180
|
-
actions: [
|
|
181
|
-
{
|
|
182
|
-
type: "Action.Submit",
|
|
183
|
-
title: "Vote",
|
|
184
|
-
data: {
|
|
185
|
-
klawPollId: pollId,
|
|
186
|
-
pollId,
|
|
187
|
-
},
|
|
188
|
-
msteams: {
|
|
189
|
-
type: "messageBack",
|
|
190
|
-
text: "klaw poll vote",
|
|
191
|
-
displayText: "Vote recorded",
|
|
192
|
-
value: { klawPollId: pollId, pollId },
|
|
193
|
-
},
|
|
194
|
-
},
|
|
195
|
-
],
|
|
196
|
-
};
|
|
197
|
-
|
|
198
|
-
const fallbackLines = [
|
|
199
|
-
`Poll: ${params.question}`,
|
|
200
|
-
...params.options.map((option, index) => `${index + 1}. ${option}`),
|
|
201
|
-
];
|
|
202
|
-
|
|
203
|
-
return {
|
|
204
|
-
pollId,
|
|
205
|
-
question: params.question,
|
|
206
|
-
options: params.options,
|
|
207
|
-
maxSelections: cappedMaxSelections,
|
|
208
|
-
card,
|
|
209
|
-
fallbackText: fallbackLines.join("\n"),
|
|
210
|
-
};
|
|
211
|
-
}
|
|
212
|
-
|
|
213
|
-
type MSTeamsPollStoreFsOptions = {
|
|
214
|
-
env?: NodeJS.ProcessEnv;
|
|
215
|
-
homedir?: () => string;
|
|
216
|
-
stateDir?: string;
|
|
217
|
-
storePath?: string;
|
|
218
|
-
};
|
|
219
|
-
|
|
220
|
-
function parseTimestamp(value?: string): number | null {
|
|
221
|
-
if (!value) {
|
|
222
|
-
return null;
|
|
223
|
-
}
|
|
224
|
-
const parsed = Date.parse(value);
|
|
225
|
-
return Number.isFinite(parsed) ? parsed : null;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
function pruneExpired(polls: Record<string, MSTeamsPoll>) {
|
|
229
|
-
const cutoff = Date.now() - POLL_TTL_MS;
|
|
230
|
-
const entries = Object.entries(polls).filter(([, poll]) => {
|
|
231
|
-
const ts = parseTimestamp(poll.updatedAt ?? poll.createdAt) ?? 0;
|
|
232
|
-
return ts >= cutoff;
|
|
233
|
-
});
|
|
234
|
-
return Object.fromEntries(entries);
|
|
235
|
-
}
|
|
236
|
-
|
|
237
|
-
function pruneToLimit(polls: Record<string, MSTeamsPoll>) {
|
|
238
|
-
const entries = Object.entries(polls);
|
|
239
|
-
if (entries.length <= MAX_POLLS) {
|
|
240
|
-
return polls;
|
|
241
|
-
}
|
|
242
|
-
entries.sort((a, b) => {
|
|
243
|
-
const aTs = parseTimestamp(a[1].updatedAt ?? a[1].createdAt) ?? 0;
|
|
244
|
-
const bTs = parseTimestamp(b[1].updatedAt ?? b[1].createdAt) ?? 0;
|
|
245
|
-
return aTs - bTs;
|
|
246
|
-
});
|
|
247
|
-
const keep = entries.slice(entries.length - MAX_POLLS);
|
|
248
|
-
return Object.fromEntries(keep);
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
export function normalizeMSTeamsPollSelections(poll: MSTeamsPoll, selections: string[]) {
|
|
252
|
-
const maxSelections = Math.max(1, poll.maxSelections);
|
|
253
|
-
const mapped = selections
|
|
254
|
-
.map((entry) => Number.parseInt(entry, 10))
|
|
255
|
-
.filter((value) => Number.isFinite(value))
|
|
256
|
-
.filter((value) => value >= 0 && value < poll.options.length)
|
|
257
|
-
.map((value) => String(value));
|
|
258
|
-
const limited = maxSelections > 1 ? mapped.slice(0, maxSelections) : mapped.slice(0, 1);
|
|
259
|
-
return Array.from(new Set(limited));
|
|
260
|
-
}
|
|
261
|
-
|
|
262
|
-
export function createMSTeamsPollStoreFs(params?: MSTeamsPollStoreFsOptions): MSTeamsPollStore {
|
|
263
|
-
const filePath = resolveMSTeamsStorePath({
|
|
264
|
-
filename: STORE_FILENAME,
|
|
265
|
-
env: params?.env,
|
|
266
|
-
homedir: params?.homedir,
|
|
267
|
-
stateDir: params?.stateDir,
|
|
268
|
-
storePath: params?.storePath,
|
|
269
|
-
});
|
|
270
|
-
const empty: PollStoreData = { version: 1, polls: {} };
|
|
271
|
-
|
|
272
|
-
const readStore = async (): Promise<PollStoreData> => {
|
|
273
|
-
const { value } = await readJsonFile(filePath, empty);
|
|
274
|
-
const pruned = pruneToLimit(pruneExpired(value.polls ?? {}));
|
|
275
|
-
return { version: 1, polls: pruned };
|
|
276
|
-
};
|
|
277
|
-
|
|
278
|
-
const writeStore = async (data: PollStoreData) => {
|
|
279
|
-
await writeJsonFile(filePath, data);
|
|
280
|
-
};
|
|
281
|
-
|
|
282
|
-
const createPoll = async (poll: MSTeamsPoll) => {
|
|
283
|
-
await withFileLock(filePath, empty, async () => {
|
|
284
|
-
const data = await readStore();
|
|
285
|
-
data.polls[poll.id] = poll;
|
|
286
|
-
await writeStore({ version: 1, polls: pruneToLimit(data.polls) });
|
|
287
|
-
});
|
|
288
|
-
};
|
|
289
|
-
|
|
290
|
-
const getPoll = async (pollId: string) =>
|
|
291
|
-
await withFileLock(filePath, empty, async () => {
|
|
292
|
-
const data = await readStore();
|
|
293
|
-
return data.polls[pollId] ?? null;
|
|
294
|
-
});
|
|
295
|
-
|
|
296
|
-
const recordVote = async (params: { pollId: string; voterId: string; selections: string[] }) =>
|
|
297
|
-
await withFileLock(filePath, empty, async () => {
|
|
298
|
-
const data = await readStore();
|
|
299
|
-
const poll = data.polls[params.pollId];
|
|
300
|
-
if (!poll) {
|
|
301
|
-
return null;
|
|
302
|
-
}
|
|
303
|
-
const normalized = normalizeMSTeamsPollSelections(poll, params.selections);
|
|
304
|
-
poll.votes[params.voterId] = normalized;
|
|
305
|
-
poll.updatedAt = new Date().toISOString();
|
|
306
|
-
data.polls[poll.id] = poll;
|
|
307
|
-
await writeStore({ version: 1, polls: pruneToLimit(data.polls) });
|
|
308
|
-
return poll;
|
|
309
|
-
});
|
|
310
|
-
|
|
311
|
-
return { createPoll, getPoll, recordVote };
|
|
312
|
-
}
|
package/src/presentation.ts
DELETED
|
@@ -1,93 +0,0 @@
|
|
|
1
|
-
import {
|
|
2
|
-
adaptMessagePresentationForChannel,
|
|
3
|
-
type MessagePresentation,
|
|
4
|
-
} from "klaw/plugin-sdk/interactive-runtime";
|
|
5
|
-
import { normalizeOptionalString } from "klaw/plugin-sdk/string-coerce-runtime";
|
|
6
|
-
import type { ChannelOutboundAdapter } from "../runtime-api.js";
|
|
7
|
-
|
|
8
|
-
export const MSTEAMS_PRESENTATION_CAPABILITIES = {
|
|
9
|
-
supported: true,
|
|
10
|
-
buttons: true,
|
|
11
|
-
selects: false,
|
|
12
|
-
context: true,
|
|
13
|
-
divider: true,
|
|
14
|
-
limits: {
|
|
15
|
-
actions: {
|
|
16
|
-
supportsStyles: false,
|
|
17
|
-
supportsDisabled: false,
|
|
18
|
-
},
|
|
19
|
-
text: {
|
|
20
|
-
markdownDialect: "markdown",
|
|
21
|
-
},
|
|
22
|
-
},
|
|
23
|
-
} satisfies ChannelOutboundAdapter["presentationCapabilities"];
|
|
24
|
-
|
|
25
|
-
export function buildMSTeamsPresentationCard(params: {
|
|
26
|
-
presentation: MessagePresentation;
|
|
27
|
-
text?: string | null;
|
|
28
|
-
}) {
|
|
29
|
-
const body: Record<string, unknown>[] = [];
|
|
30
|
-
const text = normalizeOptionalString(params.text);
|
|
31
|
-
if (text) {
|
|
32
|
-
body.push({
|
|
33
|
-
type: "TextBlock",
|
|
34
|
-
text,
|
|
35
|
-
wrap: true,
|
|
36
|
-
});
|
|
37
|
-
}
|
|
38
|
-
const presentation = adaptMessagePresentationForChannel({
|
|
39
|
-
presentation: params.presentation,
|
|
40
|
-
capabilities: MSTEAMS_PRESENTATION_CAPABILITIES,
|
|
41
|
-
});
|
|
42
|
-
if (presentation.title) {
|
|
43
|
-
body.push({
|
|
44
|
-
type: "TextBlock",
|
|
45
|
-
text: presentation.title,
|
|
46
|
-
weight: "Bolder",
|
|
47
|
-
size: "Medium",
|
|
48
|
-
wrap: true,
|
|
49
|
-
});
|
|
50
|
-
}
|
|
51
|
-
const actions: Record<string, unknown>[] = [];
|
|
52
|
-
for (const block of presentation.blocks) {
|
|
53
|
-
if (block.type === "text" || block.type === "context") {
|
|
54
|
-
body.push({
|
|
55
|
-
type: "TextBlock",
|
|
56
|
-
text: block.text,
|
|
57
|
-
wrap: true,
|
|
58
|
-
...(block.type === "context" ? { isSubtle: true, size: "Small" } : {}),
|
|
59
|
-
});
|
|
60
|
-
continue;
|
|
61
|
-
}
|
|
62
|
-
if (block.type === "divider") {
|
|
63
|
-
body.push({ type: "TextBlock", text: "---", wrap: true, isSubtle: true });
|
|
64
|
-
continue;
|
|
65
|
-
}
|
|
66
|
-
if (block.type === "buttons") {
|
|
67
|
-
for (const button of block.buttons) {
|
|
68
|
-
const targetUrl = button.url ?? button.webApp?.url ?? button.web_app?.url;
|
|
69
|
-
if (targetUrl) {
|
|
70
|
-
actions.push({
|
|
71
|
-
type: "Action.OpenUrl",
|
|
72
|
-
title: button.label,
|
|
73
|
-
url: targetUrl,
|
|
74
|
-
});
|
|
75
|
-
continue;
|
|
76
|
-
}
|
|
77
|
-
if (button.value) {
|
|
78
|
-
actions.push({
|
|
79
|
-
type: "Action.Submit",
|
|
80
|
-
title: button.label,
|
|
81
|
-
data: { value: button.value, label: button.label },
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
|
-
}
|
|
85
|
-
}
|
|
86
|
-
}
|
|
87
|
-
return {
|
|
88
|
-
type: "AdaptiveCard",
|
|
89
|
-
version: "1.4",
|
|
90
|
-
body,
|
|
91
|
-
...(actions.length ? { actions } : {}),
|
|
92
|
-
};
|
|
93
|
-
}
|
package/src/probe.test.ts
DELETED
|
@@ -1,79 +0,0 @@
|
|
|
1
|
-
import { afterEach, beforeEach, describe, expect, it, vi } from "vitest";
|
|
2
|
-
import type { MSTeamsConfig } from "../runtime-api.js";
|
|
3
|
-
|
|
4
|
-
const hostMockState = vi.hoisted(() => ({
|
|
5
|
-
tokenError: null as Error | null,
|
|
6
|
-
}));
|
|
7
|
-
|
|
8
|
-
vi.mock("@microsoft/teams.apps", () => ({
|
|
9
|
-
App: class {
|
|
10
|
-
protected async getBotToken() {
|
|
11
|
-
if (hostMockState.tokenError) {
|
|
12
|
-
throw hostMockState.tokenError;
|
|
13
|
-
}
|
|
14
|
-
return { value: "token" };
|
|
15
|
-
}
|
|
16
|
-
protected async getAppGraphToken() {
|
|
17
|
-
if (hostMockState.tokenError) {
|
|
18
|
-
throw hostMockState.tokenError;
|
|
19
|
-
}
|
|
20
|
-
return { value: "token" };
|
|
21
|
-
}
|
|
22
|
-
},
|
|
23
|
-
}));
|
|
24
|
-
|
|
25
|
-
vi.mock("@microsoft/teams.api", () => ({
|
|
26
|
-
Client: function Client() {},
|
|
27
|
-
}));
|
|
28
|
-
|
|
29
|
-
import { probeMSTeams } from "./probe.js";
|
|
30
|
-
|
|
31
|
-
describe("msteams probe", () => {
|
|
32
|
-
beforeEach(() => {
|
|
33
|
-
hostMockState.tokenError = null;
|
|
34
|
-
vi.stubEnv("MSTEAMS_APP_ID", "");
|
|
35
|
-
vi.stubEnv("MSTEAMS_APP_PASSWORD", "");
|
|
36
|
-
vi.stubEnv("MSTEAMS_TENANT_ID", "");
|
|
37
|
-
});
|
|
38
|
-
|
|
39
|
-
afterEach(() => {
|
|
40
|
-
vi.unstubAllEnvs();
|
|
41
|
-
});
|
|
42
|
-
|
|
43
|
-
it("returns an error when credentials are missing", async () => {
|
|
44
|
-
const cfg = { enabled: true } as unknown as MSTeamsConfig;
|
|
45
|
-
await expect(probeMSTeams(cfg)).resolves.toEqual({
|
|
46
|
-
ok: false,
|
|
47
|
-
error: "missing credentials (appId, appPassword, tenantId)",
|
|
48
|
-
});
|
|
49
|
-
});
|
|
50
|
-
|
|
51
|
-
it("validates credentials by acquiring a token", async () => {
|
|
52
|
-
const cfg = {
|
|
53
|
-
enabled: true,
|
|
54
|
-
appId: "app",
|
|
55
|
-
appPassword: "pw",
|
|
56
|
-
tenantId: "tenant",
|
|
57
|
-
} as unknown as MSTeamsConfig;
|
|
58
|
-
await expect(probeMSTeams(cfg)).resolves.toEqual({
|
|
59
|
-
ok: true,
|
|
60
|
-
appId: "app",
|
|
61
|
-
graph: { ok: true, roles: undefined, scopes: undefined },
|
|
62
|
-
});
|
|
63
|
-
});
|
|
64
|
-
|
|
65
|
-
it("returns a helpful error when token acquisition fails", async () => {
|
|
66
|
-
hostMockState.tokenError = new Error("bad creds");
|
|
67
|
-
const cfg = {
|
|
68
|
-
enabled: true,
|
|
69
|
-
appId: "app",
|
|
70
|
-
appPassword: "pw",
|
|
71
|
-
tenantId: "tenant",
|
|
72
|
-
} as unknown as MSTeamsConfig;
|
|
73
|
-
await expect(probeMSTeams(cfg)).resolves.toEqual({
|
|
74
|
-
ok: false,
|
|
75
|
-
appId: "app",
|
|
76
|
-
error: "bad creds",
|
|
77
|
-
});
|
|
78
|
-
});
|
|
79
|
-
});
|