@gakr-gakr/msteams 0.1.0
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/autobot.plugin.json +15 -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 +20 -0
- package/package.json +72 -0
- 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.ts +348 -0
- package/src/attachments/download.ts +328 -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.ts +86 -0
- package/src/attachments/shared.ts +655 -0
- package/src/attachments/types.ts +47 -0
- package/src/attachments.ts +18 -0
- package/src/channel-api.ts +1 -0
- package/src/channel.runtime.ts +56 -0
- package/src/channel.setup.ts +77 -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.ts +149 -0
- package/src/conversation-store-helpers.ts +105 -0
- package/src/conversation-store-memory.ts +51 -0
- package/src/conversation-store.ts +71 -0
- package/src/directory-live.ts +111 -0
- package/src/doctor.ts +27 -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.ts +271 -0
- package/src/file-consent-helpers.ts +115 -0
- package/src/file-consent-invoke.ts +150 -0
- package/src/file-consent.ts +223 -0
- package/src/graph-chat.ts +36 -0
- package/src/graph-group-management.ts +168 -0
- package/src/graph-members.ts +48 -0
- package/src/graph-messages.ts +534 -0
- package/src/graph-teams.ts +114 -0
- package/src/graph-thread.ts +146 -0
- package/src/graph-upload.ts +531 -0
- package/src/graph-users.ts +29 -0
- package/src/graph.ts +308 -0
- package/src/inbound.ts +148 -0
- package/src/index.ts +4 -0
- package/src/media-helpers.ts +105 -0
- package/src/mentions.ts +114 -0
- package/src/messenger.ts +608 -0
- package/src/monitor-handler/access.ts +136 -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.test-support.ts +102 -0
- package/src/monitor-handler/message-handler.ts +1015 -0
- package/src/monitor-handler/reaction-handler.ts +124 -0
- package/src/monitor-handler/thread-session.ts +30 -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.ts +476 -0
- package/src/oauth.flow.ts +77 -0
- package/src/oauth.shared.ts +37 -0
- package/src/oauth.token.ts +162 -0
- package/src/oauth.ts +130 -0
- package/src/outbound.ts +198 -0
- package/src/pending-uploads-fs.ts +235 -0
- package/src/pending-uploads.ts +121 -0
- package/src/policy.ts +245 -0
- package/src/polls-store-memory.ts +32 -0
- package/src/polls.ts +312 -0
- package/src/presentation.ts +93 -0
- package/src/probe.ts +132 -0
- package/src/reply-dispatcher.ts +523 -0
- package/src/reply-stream-controller.ts +334 -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.ts +916 -0
- package/src/secret-contract.ts +49 -0
- package/src/secret-input.ts +7 -0
- package/src/send-context.ts +269 -0
- package/src/send.ts +697 -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.ts +319 -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.ts +327 -0
- package/src/thread-parent-context.ts +159 -0
- package/src/token-response.ts +11 -0
- package/src/token.ts +194 -0
- package/src/user-agent.ts +53 -0
- package/src/webhook-timeouts.ts +27 -0
- package/src/welcome-card.ts +57 -0
- package/test-api.ts +1 -0
- package/tsconfig.json +16 -0
|
@@ -0,0 +1,6 @@
|
|
|
1
|
+
import { buildChannelConfigSchema, MSTeamsConfigSchema } from "../config-api.js";
|
|
2
|
+
import { msTeamsChannelConfigUiHints } from "./config-ui-hints.js";
|
|
3
|
+
|
|
4
|
+
export const MSTeamsChannelConfigSchema = buildChannelConfigSchema(MSTeamsConfigSchema, {
|
|
5
|
+
uiHints: msTeamsChannelConfigUiHints,
|
|
6
|
+
});
|
|
@@ -0,0 +1,40 @@
|
|
|
1
|
+
import type { ChannelConfigUiHint } from "autobot/plugin-sdk/channel-core";
|
|
2
|
+
|
|
3
|
+
export const msTeamsChannelConfigUiHints = {
|
|
4
|
+
"": {
|
|
5
|
+
label: "MS Teams",
|
|
6
|
+
help: "Microsoft Teams channel provider configuration and provider-specific policy toggles. Use this section to isolate Teams behavior from other enterprise chat providers.",
|
|
7
|
+
},
|
|
8
|
+
configWrites: {
|
|
9
|
+
label: "MS Teams Config Writes",
|
|
10
|
+
help: "Allow Microsoft Teams to write config in response to channel events/commands (default: true).",
|
|
11
|
+
},
|
|
12
|
+
streaming: {
|
|
13
|
+
label: "MS Teams Streaming",
|
|
14
|
+
help: 'Microsoft Teams preview/progress streaming mode: "off" | "partial" | "block" | "progress". Personal chats use Teams native streaminfo progress when available.',
|
|
15
|
+
},
|
|
16
|
+
"streaming.progress.label": {
|
|
17
|
+
label: "MS Teams Progress Label",
|
|
18
|
+
help: 'Initial progress title. Use "auto" for built-in single-word labels, a custom string, or false to hide the title.',
|
|
19
|
+
},
|
|
20
|
+
"streaming.progress.labels": {
|
|
21
|
+
label: "MS Teams Progress Label Pool",
|
|
22
|
+
help: 'Candidate labels for streaming.progress.label="auto". Leave unset to use AutoBot built-in progress labels.',
|
|
23
|
+
},
|
|
24
|
+
"streaming.progress.maxLines": {
|
|
25
|
+
label: "MS Teams Progress Max Lines",
|
|
26
|
+
help: "Maximum number of compact progress lines to keep below the progress title (default: 8).",
|
|
27
|
+
},
|
|
28
|
+
"streaming.progress.maxLineChars": {
|
|
29
|
+
label: "MS Teams Progress Max Line Chars",
|
|
30
|
+
help: "Maximum characters per compact progress line before truncation (default: 120). Prose cuts at word boundaries; commands and paths keep useful suffixes.",
|
|
31
|
+
},
|
|
32
|
+
"streaming.progress.toolProgress": {
|
|
33
|
+
label: "MS Teams Progress Tool Lines",
|
|
34
|
+
help: "Show compact tool/progress lines in progress mode (default: true). Set false to keep only the title until final delivery.",
|
|
35
|
+
},
|
|
36
|
+
"streaming.progress.commandText": {
|
|
37
|
+
label: "MS Teams Progress Command Text",
|
|
38
|
+
help: 'Command/exec detail in progress lines: "raw" preserves released behavior; "status" shows only the tool label.',
|
|
39
|
+
},
|
|
40
|
+
} satisfies Record<string, ChannelConfigUiHint>;
|
|
@@ -0,0 +1,149 @@
|
|
|
1
|
+
import {
|
|
2
|
+
findPreferredDmConversationByUserId,
|
|
3
|
+
mergeStoredConversationReference,
|
|
4
|
+
normalizeStoredConversationId,
|
|
5
|
+
parseStoredConversationTimestamp,
|
|
6
|
+
toConversationStoreEntries,
|
|
7
|
+
} from "./conversation-store-helpers.js";
|
|
8
|
+
import type {
|
|
9
|
+
MSTeamsConversationStore,
|
|
10
|
+
MSTeamsConversationStoreEntry,
|
|
11
|
+
StoredConversationReference,
|
|
12
|
+
} from "./conversation-store.js";
|
|
13
|
+
import { resolveMSTeamsStorePath } from "./storage.js";
|
|
14
|
+
import { readJsonFile, withFileLock, writeJsonFile } from "./store-fs.js";
|
|
15
|
+
|
|
16
|
+
type ConversationStoreData = {
|
|
17
|
+
version: 1;
|
|
18
|
+
conversations: Record<string, StoredConversationReference>;
|
|
19
|
+
};
|
|
20
|
+
|
|
21
|
+
const STORE_FILENAME = "msteams-conversations.json";
|
|
22
|
+
const MAX_CONVERSATIONS = 1000;
|
|
23
|
+
const CONVERSATION_TTL_MS = 365 * 24 * 60 * 60 * 1000;
|
|
24
|
+
|
|
25
|
+
function pruneToLimit(conversations: Record<string, StoredConversationReference>) {
|
|
26
|
+
const entries = Object.entries(conversations);
|
|
27
|
+
if (entries.length <= MAX_CONVERSATIONS) {
|
|
28
|
+
return conversations;
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
entries.sort((a, b) => {
|
|
32
|
+
const aTs = parseStoredConversationTimestamp(a[1].lastSeenAt) ?? 0;
|
|
33
|
+
const bTs = parseStoredConversationTimestamp(b[1].lastSeenAt) ?? 0;
|
|
34
|
+
return aTs - bTs;
|
|
35
|
+
});
|
|
36
|
+
|
|
37
|
+
const keep = entries.slice(entries.length - MAX_CONVERSATIONS);
|
|
38
|
+
return Object.fromEntries(keep);
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
function pruneExpired(
|
|
42
|
+
conversations: Record<string, StoredConversationReference>,
|
|
43
|
+
nowMs: number,
|
|
44
|
+
ttlMs: number,
|
|
45
|
+
) {
|
|
46
|
+
let removed = false;
|
|
47
|
+
const kept: typeof conversations = {};
|
|
48
|
+
for (const [conversationId, reference] of Object.entries(conversations)) {
|
|
49
|
+
const lastSeenAt = parseStoredConversationTimestamp(reference.lastSeenAt);
|
|
50
|
+
// Preserve legacy entries that have no lastSeenAt until they're seen again.
|
|
51
|
+
if (lastSeenAt != null && nowMs - lastSeenAt > ttlMs) {
|
|
52
|
+
removed = true;
|
|
53
|
+
continue;
|
|
54
|
+
}
|
|
55
|
+
kept[conversationId] = reference;
|
|
56
|
+
}
|
|
57
|
+
return { conversations: kept, removed };
|
|
58
|
+
}
|
|
59
|
+
|
|
60
|
+
export function createMSTeamsConversationStoreFs(params?: {
|
|
61
|
+
env?: NodeJS.ProcessEnv;
|
|
62
|
+
homedir?: () => string;
|
|
63
|
+
ttlMs?: number;
|
|
64
|
+
stateDir?: string;
|
|
65
|
+
storePath?: string;
|
|
66
|
+
}): MSTeamsConversationStore {
|
|
67
|
+
const ttlMs = params?.ttlMs ?? CONVERSATION_TTL_MS;
|
|
68
|
+
const filePath = resolveMSTeamsStorePath({
|
|
69
|
+
filename: STORE_FILENAME,
|
|
70
|
+
env: params?.env,
|
|
71
|
+
homedir: params?.homedir,
|
|
72
|
+
stateDir: params?.stateDir,
|
|
73
|
+
storePath: params?.storePath,
|
|
74
|
+
});
|
|
75
|
+
|
|
76
|
+
const empty: ConversationStoreData = { version: 1, conversations: {} };
|
|
77
|
+
|
|
78
|
+
const readStore = async (): Promise<ConversationStoreData> => {
|
|
79
|
+
const { value } = await readJsonFile(filePath, empty);
|
|
80
|
+
if (
|
|
81
|
+
value.version !== 1 ||
|
|
82
|
+
!value.conversations ||
|
|
83
|
+
typeof value.conversations !== "object" ||
|
|
84
|
+
Array.isArray(value.conversations)
|
|
85
|
+
) {
|
|
86
|
+
return empty;
|
|
87
|
+
}
|
|
88
|
+
const nowMs = Date.now();
|
|
89
|
+
const pruned = pruneExpired(value.conversations, nowMs, ttlMs).conversations;
|
|
90
|
+
return { version: 1, conversations: pruneToLimit(pruned) };
|
|
91
|
+
};
|
|
92
|
+
|
|
93
|
+
const list = async (): Promise<MSTeamsConversationStoreEntry[]> => {
|
|
94
|
+
const store = await readStore();
|
|
95
|
+
return toConversationStoreEntries(Object.entries(store.conversations));
|
|
96
|
+
};
|
|
97
|
+
|
|
98
|
+
const get = async (conversationId: string): Promise<StoredConversationReference | null> => {
|
|
99
|
+
const store = await readStore();
|
|
100
|
+
return store.conversations[normalizeStoredConversationId(conversationId)] ?? null;
|
|
101
|
+
};
|
|
102
|
+
|
|
103
|
+
const findPreferredDmByUserId = async (
|
|
104
|
+
id: string,
|
|
105
|
+
): Promise<MSTeamsConversationStoreEntry | null> => {
|
|
106
|
+
return findPreferredDmConversationByUserId(await list(), id);
|
|
107
|
+
};
|
|
108
|
+
|
|
109
|
+
const upsert = async (
|
|
110
|
+
conversationId: string,
|
|
111
|
+
reference: StoredConversationReference,
|
|
112
|
+
): Promise<void> => {
|
|
113
|
+
const normalizedId = normalizeStoredConversationId(conversationId);
|
|
114
|
+
await withFileLock(filePath, empty, async () => {
|
|
115
|
+
const store = await readStore();
|
|
116
|
+
store.conversations[normalizedId] = mergeStoredConversationReference(
|
|
117
|
+
store.conversations[normalizedId],
|
|
118
|
+
reference,
|
|
119
|
+
new Date().toISOString(),
|
|
120
|
+
);
|
|
121
|
+
const nowMs = Date.now();
|
|
122
|
+
store.conversations = pruneExpired(store.conversations, nowMs, ttlMs).conversations;
|
|
123
|
+
store.conversations = pruneToLimit(store.conversations);
|
|
124
|
+
await writeJsonFile(filePath, store);
|
|
125
|
+
});
|
|
126
|
+
};
|
|
127
|
+
|
|
128
|
+
const remove = async (conversationId: string): Promise<boolean> => {
|
|
129
|
+
const normalizedId = normalizeStoredConversationId(conversationId);
|
|
130
|
+
return await withFileLock(filePath, empty, async () => {
|
|
131
|
+
const store = await readStore();
|
|
132
|
+
if (!(normalizedId in store.conversations)) {
|
|
133
|
+
return false;
|
|
134
|
+
}
|
|
135
|
+
delete store.conversations[normalizedId];
|
|
136
|
+
await writeJsonFile(filePath, store);
|
|
137
|
+
return true;
|
|
138
|
+
});
|
|
139
|
+
};
|
|
140
|
+
|
|
141
|
+
return {
|
|
142
|
+
upsert,
|
|
143
|
+
get,
|
|
144
|
+
list,
|
|
145
|
+
remove,
|
|
146
|
+
findPreferredDmByUserId,
|
|
147
|
+
findByUserId: findPreferredDmByUserId,
|
|
148
|
+
};
|
|
149
|
+
}
|
|
@@ -0,0 +1,105 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import type {
|
|
3
|
+
MSTeamsConversationStoreEntry,
|
|
4
|
+
StoredConversationReference,
|
|
5
|
+
} from "./conversation-store.js";
|
|
6
|
+
|
|
7
|
+
export function normalizeStoredConversationId(raw: string): string {
|
|
8
|
+
return raw.split(";")[0] ?? raw;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export function parseStoredConversationTimestamp(value: string | undefined): number | null {
|
|
12
|
+
if (!value) {
|
|
13
|
+
return null;
|
|
14
|
+
}
|
|
15
|
+
const parsed = Date.parse(value);
|
|
16
|
+
if (!Number.isFinite(parsed)) {
|
|
17
|
+
return null;
|
|
18
|
+
}
|
|
19
|
+
return parsed;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function toConversationStoreEntries(
|
|
23
|
+
entries: Iterable<[string, StoredConversationReference]>,
|
|
24
|
+
): MSTeamsConversationStoreEntry[] {
|
|
25
|
+
return Array.from(entries, ([conversationId, reference]) => ({
|
|
26
|
+
conversationId,
|
|
27
|
+
reference,
|
|
28
|
+
}));
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
export function mergeStoredConversationReference(
|
|
32
|
+
existing: StoredConversationReference | undefined,
|
|
33
|
+
incoming: StoredConversationReference,
|
|
34
|
+
nowIso: string,
|
|
35
|
+
): StoredConversationReference {
|
|
36
|
+
return {
|
|
37
|
+
// Preserve fields from the previous entry that may not be present on every
|
|
38
|
+
// inbound activity. Without this, sparse activities (e.g. conversationUpdate,
|
|
39
|
+
// reactions) would clear previously captured values. Some fields are only
|
|
40
|
+
// populated opportunistically, such as timezone from clientInfo entities and
|
|
41
|
+
// graphChatId from Graph lookups used for DM media downloads.
|
|
42
|
+
...(existing?.timezone && !incoming.timezone ? { timezone: existing.timezone } : {}),
|
|
43
|
+
...(existing?.graphChatId && !incoming.graphChatId
|
|
44
|
+
? { graphChatId: existing.graphChatId }
|
|
45
|
+
: {}),
|
|
46
|
+
...(existing?.tenantId && !incoming.tenantId ? { tenantId: existing.tenantId } : {}),
|
|
47
|
+
...(existing?.aadObjectId && !incoming.aadObjectId
|
|
48
|
+
? { aadObjectId: existing.aadObjectId }
|
|
49
|
+
: {}),
|
|
50
|
+
...incoming,
|
|
51
|
+
lastSeenAt: nowIso,
|
|
52
|
+
};
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
export function findPreferredDmConversationByUserId(
|
|
56
|
+
entries: Iterable<MSTeamsConversationStoreEntry>,
|
|
57
|
+
id: string,
|
|
58
|
+
): MSTeamsConversationStoreEntry | null {
|
|
59
|
+
const target = id.trim();
|
|
60
|
+
if (!target) {
|
|
61
|
+
return null;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
// Partition user matches into DM-safe and non-DM buckets.
|
|
65
|
+
// Channel and group conversations also carry the sender's aadObjectId, but
|
|
66
|
+
// returning one of those when the caller asked for a user-targeted DM would
|
|
67
|
+
// leak the reply into a shared channel -- the root cause of #54520.
|
|
68
|
+
const personalMatches: MSTeamsConversationStoreEntry[] = [];
|
|
69
|
+
const unknownTypeMatches: MSTeamsConversationStoreEntry[] = [];
|
|
70
|
+
for (const entry of entries) {
|
|
71
|
+
if (entry.reference.user?.aadObjectId !== target && entry.reference.user?.id !== target) {
|
|
72
|
+
continue;
|
|
73
|
+
}
|
|
74
|
+
const convType = normalizeLowercaseStringOrEmpty(
|
|
75
|
+
entry.reference.conversation?.conversationType ?? "",
|
|
76
|
+
);
|
|
77
|
+
if (convType === "personal") {
|
|
78
|
+
personalMatches.push(entry);
|
|
79
|
+
} else if (convType === "channel" || convType === "groupchat") {
|
|
80
|
+
// Explicitly skip channel/group conversations -- these must never be
|
|
81
|
+
// returned for a user-targeted DM lookup.
|
|
82
|
+
} else {
|
|
83
|
+
// Legacy entries without conversationType are ambiguous. Include them
|
|
84
|
+
// as a fallback but rank below confirmed personal conversations.
|
|
85
|
+
unknownTypeMatches.push(entry);
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
// Prefer confirmed personal DMs, fall back to unknown-type entries.
|
|
90
|
+
const candidates = personalMatches.length > 0 ? personalMatches : unknownTypeMatches;
|
|
91
|
+
if (candidates.length === 0) {
|
|
92
|
+
return null;
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// When multiple candidates exist, prefer the most recently seen one.
|
|
96
|
+
if (candidates.length > 1) {
|
|
97
|
+
candidates.sort(
|
|
98
|
+
(a, b) =>
|
|
99
|
+
(parseStoredConversationTimestamp(b.reference.lastSeenAt) ?? 0) -
|
|
100
|
+
(parseStoredConversationTimestamp(a.reference.lastSeenAt) ?? 0),
|
|
101
|
+
);
|
|
102
|
+
}
|
|
103
|
+
|
|
104
|
+
return candidates[0] ?? null;
|
|
105
|
+
}
|
|
@@ -0,0 +1,51 @@
|
|
|
1
|
+
import {
|
|
2
|
+
findPreferredDmConversationByUserId,
|
|
3
|
+
mergeStoredConversationReference,
|
|
4
|
+
normalizeStoredConversationId,
|
|
5
|
+
toConversationStoreEntries,
|
|
6
|
+
} from "./conversation-store-helpers.js";
|
|
7
|
+
import type {
|
|
8
|
+
MSTeamsConversationStore,
|
|
9
|
+
MSTeamsConversationStoreEntry,
|
|
10
|
+
StoredConversationReference,
|
|
11
|
+
} from "./conversation-store.js";
|
|
12
|
+
|
|
13
|
+
export function createMSTeamsConversationStoreMemory(
|
|
14
|
+
initial: MSTeamsConversationStoreEntry[] = [],
|
|
15
|
+
): MSTeamsConversationStore {
|
|
16
|
+
const map = new Map<string, StoredConversationReference>();
|
|
17
|
+
for (const { conversationId, reference } of initial) {
|
|
18
|
+
map.set(normalizeStoredConversationId(conversationId), reference);
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const findPreferredDmByUserId = async (
|
|
22
|
+
id: string,
|
|
23
|
+
): Promise<MSTeamsConversationStoreEntry | null> => {
|
|
24
|
+
return findPreferredDmConversationByUserId(toConversationStoreEntries(map.entries()), id);
|
|
25
|
+
};
|
|
26
|
+
|
|
27
|
+
return {
|
|
28
|
+
upsert: async (conversationId, reference) => {
|
|
29
|
+
const normalizedId = normalizeStoredConversationId(conversationId);
|
|
30
|
+
map.set(
|
|
31
|
+
normalizedId,
|
|
32
|
+
mergeStoredConversationReference(
|
|
33
|
+
map.get(normalizedId),
|
|
34
|
+
reference,
|
|
35
|
+
new Date().toISOString(),
|
|
36
|
+
),
|
|
37
|
+
);
|
|
38
|
+
},
|
|
39
|
+
get: async (conversationId) => {
|
|
40
|
+
return map.get(normalizeStoredConversationId(conversationId)) ?? null;
|
|
41
|
+
},
|
|
42
|
+
list: async () => {
|
|
43
|
+
return toConversationStoreEntries(map.entries());
|
|
44
|
+
},
|
|
45
|
+
remove: async (conversationId) => {
|
|
46
|
+
return map.delete(normalizeStoredConversationId(conversationId));
|
|
47
|
+
},
|
|
48
|
+
findPreferredDmByUserId,
|
|
49
|
+
findByUserId: findPreferredDmByUserId,
|
|
50
|
+
};
|
|
51
|
+
}
|
|
@@ -0,0 +1,71 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Conversation store for MS Teams proactive messaging.
|
|
3
|
+
*
|
|
4
|
+
* Stores ConversationReference-like objects keyed by conversation ID so we can
|
|
5
|
+
* send proactive messages later (after the webhook turn has completed).
|
|
6
|
+
*/
|
|
7
|
+
|
|
8
|
+
/** Minimal ConversationReference shape for proactive messaging */
|
|
9
|
+
export type StoredConversationReference = {
|
|
10
|
+
/** Timestamp when this reference was last seen/updated. */
|
|
11
|
+
lastSeenAt?: string;
|
|
12
|
+
/** Activity ID from the last message */
|
|
13
|
+
activityId?: string;
|
|
14
|
+
/** Channel thread root activity ID for threaded replies. */
|
|
15
|
+
threadId?: string;
|
|
16
|
+
/** User who sent the message */
|
|
17
|
+
user?: { id?: string; name?: string; aadObjectId?: string };
|
|
18
|
+
/** Agent/bot that received the message */
|
|
19
|
+
agent?: { id?: string; name?: string; aadObjectId?: string } | null;
|
|
20
|
+
/** @deprecated legacy field (pre-Agents SDK). Prefer `agent`. */
|
|
21
|
+
bot?: { id?: string; name?: string };
|
|
22
|
+
/** Conversation details */
|
|
23
|
+
conversation?: { id?: string; conversationType?: string; tenantId?: string };
|
|
24
|
+
/**
|
|
25
|
+
* Tenant ID sourced from `activity.channelData.tenant.id` at inbound time.
|
|
26
|
+
* Bot Framework requires this on outbound proactive messages so the connector
|
|
27
|
+
* can route them to the correct Azure AD tenant; without it, the connector
|
|
28
|
+
* rejects the request with HTTP 403. For channel activities, `conversation.tenantId`
|
|
29
|
+
* is often unset, making `channelData.tenant.id` the reliable source.
|
|
30
|
+
*/
|
|
31
|
+
tenantId?: string;
|
|
32
|
+
/**
|
|
33
|
+
* Azure AD object ID of the user who sent the last inbound activity,
|
|
34
|
+
* mirrored from `activity.from.aadObjectId` so outbound proactive sends
|
|
35
|
+
* can include it on the connector request (required for personal DMs).
|
|
36
|
+
*/
|
|
37
|
+
aadObjectId?: string;
|
|
38
|
+
/** Team ID for channel messages (when available). */
|
|
39
|
+
teamId?: string;
|
|
40
|
+
/** Channel ID (usually "msteams") */
|
|
41
|
+
channelId?: string;
|
|
42
|
+
/** Service URL for sending messages back */
|
|
43
|
+
serviceUrl?: string;
|
|
44
|
+
/** Locale */
|
|
45
|
+
locale?: string;
|
|
46
|
+
/**
|
|
47
|
+
* Cached Graph API chat ID (format: `19:xxx@thread.tacv2` or `19:xxx@unq.gbl.spaces`).
|
|
48
|
+
* Bot Framework conversation IDs for personal DMs use a different format (`a:1xxx` or
|
|
49
|
+
* `8:orgid:xxx`) that the Graph API does not accept. This field caches the resolved
|
|
50
|
+
* Graph-native chat ID so we don't need to re-query the API on every send.
|
|
51
|
+
*/
|
|
52
|
+
graphChatId?: string;
|
|
53
|
+
/** IANA timezone from Teams clientInfo entity (e.g. "America/New_York") */
|
|
54
|
+
timezone?: string;
|
|
55
|
+
};
|
|
56
|
+
|
|
57
|
+
export type MSTeamsConversationStoreEntry = {
|
|
58
|
+
conversationId: string;
|
|
59
|
+
reference: StoredConversationReference;
|
|
60
|
+
};
|
|
61
|
+
|
|
62
|
+
export type MSTeamsConversationStore = {
|
|
63
|
+
upsert: (conversationId: string, reference: StoredConversationReference) => Promise<void>;
|
|
64
|
+
get: (conversationId: string) => Promise<StoredConversationReference | null>;
|
|
65
|
+
list: () => Promise<MSTeamsConversationStoreEntry[]>;
|
|
66
|
+
remove: (conversationId: string) => Promise<boolean>;
|
|
67
|
+
/** Person-targeted proactive lookup: prefer the freshest personal DM reference. */
|
|
68
|
+
findPreferredDmByUserId: (id: string) => Promise<MSTeamsConversationStoreEntry | null>;
|
|
69
|
+
/** @deprecated Use `findPreferredDmByUserId` for proactive user-targeted sends. */
|
|
70
|
+
findByUserId: (id: string) => Promise<MSTeamsConversationStoreEntry | null>;
|
|
71
|
+
};
|
|
@@ -0,0 +1,111 @@
|
|
|
1
|
+
import { normalizeLowercaseStringOrEmpty } from "autobot/plugin-sdk/string-coerce-runtime";
|
|
2
|
+
import type { ChannelDirectoryEntry } from "../runtime-api.js";
|
|
3
|
+
import { searchGraphUsers } from "./graph-users.js";
|
|
4
|
+
import {
|
|
5
|
+
listChannelsForTeam,
|
|
6
|
+
listTeamsByName,
|
|
7
|
+
normalizeQuery,
|
|
8
|
+
resolveGraphToken,
|
|
9
|
+
} from "./graph.js";
|
|
10
|
+
|
|
11
|
+
export async function listMSTeamsDirectoryPeersLive(params: {
|
|
12
|
+
cfg: unknown;
|
|
13
|
+
query?: string | null;
|
|
14
|
+
limit?: number | null;
|
|
15
|
+
}): Promise<ChannelDirectoryEntry[]> {
|
|
16
|
+
const query = normalizeQuery(params.query);
|
|
17
|
+
if (!query) {
|
|
18
|
+
return [];
|
|
19
|
+
}
|
|
20
|
+
const token = await resolveGraphToken(params.cfg);
|
|
21
|
+
const limit = typeof params.limit === "number" && params.limit > 0 ? params.limit : 20;
|
|
22
|
+
|
|
23
|
+
const users = await searchGraphUsers({ token, query, top: limit });
|
|
24
|
+
|
|
25
|
+
return users
|
|
26
|
+
.map((user) => {
|
|
27
|
+
const id = user.id?.trim();
|
|
28
|
+
if (!id) {
|
|
29
|
+
return null;
|
|
30
|
+
}
|
|
31
|
+
const name = user.displayName?.trim();
|
|
32
|
+
const handle = user.userPrincipalName?.trim() || user.mail?.trim();
|
|
33
|
+
return {
|
|
34
|
+
kind: "user",
|
|
35
|
+
id: `user:${id}`,
|
|
36
|
+
name: name || undefined,
|
|
37
|
+
handle: handle ? `@${handle}` : undefined,
|
|
38
|
+
raw: user,
|
|
39
|
+
} satisfies ChannelDirectoryEntry;
|
|
40
|
+
})
|
|
41
|
+
.filter(Boolean) as ChannelDirectoryEntry[];
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export async function listMSTeamsDirectoryGroupsLive(params: {
|
|
45
|
+
cfg: unknown;
|
|
46
|
+
query?: string | null;
|
|
47
|
+
limit?: number | null;
|
|
48
|
+
}): Promise<ChannelDirectoryEntry[]> {
|
|
49
|
+
const rawQuery = normalizeQuery(params.query);
|
|
50
|
+
if (!rawQuery) {
|
|
51
|
+
return [];
|
|
52
|
+
}
|
|
53
|
+
const token = await resolveGraphToken(params.cfg);
|
|
54
|
+
const limit = typeof params.limit === "number" && params.limit > 0 ? params.limit : 20;
|
|
55
|
+
const [teamQuery, channelQuery] = rawQuery.includes("/")
|
|
56
|
+
? rawQuery
|
|
57
|
+
.split("/", 2)
|
|
58
|
+
.map((part) => part.trim())
|
|
59
|
+
.filter(Boolean)
|
|
60
|
+
: [rawQuery, null];
|
|
61
|
+
|
|
62
|
+
const teams = await listTeamsByName(token, teamQuery);
|
|
63
|
+
const results: ChannelDirectoryEntry[] = [];
|
|
64
|
+
|
|
65
|
+
for (const team of teams) {
|
|
66
|
+
const teamId = team.id?.trim();
|
|
67
|
+
if (!teamId) {
|
|
68
|
+
continue;
|
|
69
|
+
}
|
|
70
|
+
const teamName = team.displayName?.trim() || teamQuery;
|
|
71
|
+
if (!channelQuery) {
|
|
72
|
+
results.push({
|
|
73
|
+
kind: "group",
|
|
74
|
+
id: `team:${teamId}`,
|
|
75
|
+
name: teamName,
|
|
76
|
+
handle: teamName ? `#${teamName}` : undefined,
|
|
77
|
+
raw: team,
|
|
78
|
+
});
|
|
79
|
+
if (results.length >= limit) {
|
|
80
|
+
return results;
|
|
81
|
+
}
|
|
82
|
+
continue;
|
|
83
|
+
}
|
|
84
|
+
const channels = await listChannelsForTeam(token, teamId);
|
|
85
|
+
for (const channel of channels) {
|
|
86
|
+
const name = channel.displayName?.trim();
|
|
87
|
+
if (!name) {
|
|
88
|
+
continue;
|
|
89
|
+
}
|
|
90
|
+
if (
|
|
91
|
+
!normalizeLowercaseStringOrEmpty(name).includes(
|
|
92
|
+
normalizeLowercaseStringOrEmpty(channelQuery),
|
|
93
|
+
)
|
|
94
|
+
) {
|
|
95
|
+
continue;
|
|
96
|
+
}
|
|
97
|
+
results.push({
|
|
98
|
+
kind: "group",
|
|
99
|
+
id: `conversation:${channel.id}`,
|
|
100
|
+
name: `${teamName}/${name}`,
|
|
101
|
+
handle: `#${name}`,
|
|
102
|
+
raw: channel,
|
|
103
|
+
});
|
|
104
|
+
if (results.length >= limit) {
|
|
105
|
+
return results;
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
|
|
110
|
+
return results;
|
|
111
|
+
}
|
package/src/doctor.ts
ADDED
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
import { createDangerousNameMatchingMutableAllowlistWarningCollector } from "autobot/plugin-sdk/channel-policy";
|
|
2
|
+
|
|
3
|
+
function isMSTeamsMutableAllowEntry(raw: string): boolean {
|
|
4
|
+
const text = raw.trim();
|
|
5
|
+
if (!text || text === "*") {
|
|
6
|
+
return false;
|
|
7
|
+
}
|
|
8
|
+
|
|
9
|
+
const withoutPrefix = text.replace(/^(msteams|user):/i, "").trim();
|
|
10
|
+
return /\s/.test(withoutPrefix) || withoutPrefix.includes("@");
|
|
11
|
+
}
|
|
12
|
+
|
|
13
|
+
export const collectMSTeamsMutableAllowlistWarnings =
|
|
14
|
+
createDangerousNameMatchingMutableAllowlistWarningCollector({
|
|
15
|
+
channel: "msteams",
|
|
16
|
+
detector: isMSTeamsMutableAllowEntry,
|
|
17
|
+
collectLists: (scope) => [
|
|
18
|
+
{
|
|
19
|
+
pathLabel: `${scope.prefix}.allowFrom`,
|
|
20
|
+
list: scope.account.allowFrom,
|
|
21
|
+
},
|
|
22
|
+
{
|
|
23
|
+
pathLabel: `${scope.prefix}.groupAllowFrom`,
|
|
24
|
+
list: scope.account.groupAllowFrom,
|
|
25
|
+
},
|
|
26
|
+
],
|
|
27
|
+
});
|