@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.
Files changed (177) hide show
  1. package/klaw.plugin.json +726 -2
  2. package/package.json +18 -6
  3. package/api.ts +0 -3
  4. package/channel-config-api.ts +0 -1
  5. package/channel-plugin-api.ts +0 -2
  6. package/config-api.ts +0 -4
  7. package/contract-api.ts +0 -4
  8. package/index.ts +0 -20
  9. package/runtime-api.ts +0 -66
  10. package/secret-contract-api.ts +0 -5
  11. package/setup-entry.ts +0 -13
  12. package/setup-plugin-api.ts +0 -3
  13. package/src/ai-entity.ts +0 -7
  14. package/src/approval-auth.ts +0 -44
  15. package/src/attachments/bot-framework.test.ts +0 -506
  16. package/src/attachments/bot-framework.ts +0 -348
  17. package/src/attachments/download.ts +0 -328
  18. package/src/attachments/graph.test.ts +0 -441
  19. package/src/attachments/graph.ts +0 -489
  20. package/src/attachments/html.ts +0 -122
  21. package/src/attachments/payload.ts +0 -14
  22. package/src/attachments/remote-media.test.ts +0 -187
  23. package/src/attachments/remote-media.ts +0 -86
  24. package/src/attachments/shared.test.ts +0 -547
  25. package/src/attachments/shared.ts +0 -655
  26. package/src/attachments/types.ts +0 -47
  27. package/src/attachments.graph.test.ts +0 -414
  28. package/src/attachments.helpers.test.ts +0 -245
  29. package/src/attachments.test-helpers.ts +0 -17
  30. package/src/attachments.test.ts +0 -754
  31. package/src/attachments.ts +0 -18
  32. package/src/block-streaming-config.test.ts +0 -61
  33. package/src/channel-api.ts +0 -1
  34. package/src/channel.actions.test.ts +0 -797
  35. package/src/channel.directory.test.ts +0 -176
  36. package/src/channel.message-adapter.test.ts +0 -227
  37. package/src/channel.runtime.ts +0 -56
  38. package/src/channel.setup.ts +0 -77
  39. package/src/channel.test.ts +0 -136
  40. package/src/channel.ts +0 -1176
  41. package/src/config-schema.ts +0 -6
  42. package/src/config-ui-hints.ts +0 -40
  43. package/src/conversation-store-fs.test.ts +0 -81
  44. package/src/conversation-store-fs.ts +0 -149
  45. package/src/conversation-store-helpers.test.ts +0 -202
  46. package/src/conversation-store-helpers.ts +0 -105
  47. package/src/conversation-store-memory.ts +0 -51
  48. package/src/conversation-store.shared.test.ts +0 -260
  49. package/src/conversation-store.ts +0 -71
  50. package/src/directory-live.test.ts +0 -156
  51. package/src/directory-live.ts +0 -111
  52. package/src/doctor.ts +0 -27
  53. package/src/errors.test.ts +0 -154
  54. package/src/errors.ts +0 -270
  55. package/src/feedback-reflection-prompt.ts +0 -117
  56. package/src/feedback-reflection-store.ts +0 -113
  57. package/src/feedback-reflection.test.ts +0 -237
  58. package/src/feedback-reflection.ts +0 -268
  59. package/src/file-consent-helpers.test.ts +0 -328
  60. package/src/file-consent-helpers.ts +0 -115
  61. package/src/file-consent-invoke.ts +0 -150
  62. package/src/file-consent.test.ts +0 -378
  63. package/src/file-consent.ts +0 -223
  64. package/src/graph-chat.ts +0 -36
  65. package/src/graph-group-management.test.ts +0 -332
  66. package/src/graph-group-management.ts +0 -168
  67. package/src/graph-members.test.ts +0 -89
  68. package/src/graph-members.ts +0 -48
  69. package/src/graph-messages.actions.test.ts +0 -253
  70. package/src/graph-messages.read.test.ts +0 -391
  71. package/src/graph-messages.search.test.ts +0 -227
  72. package/src/graph-messages.test-helpers.ts +0 -50
  73. package/src/graph-messages.ts +0 -534
  74. package/src/graph-teams.test.ts +0 -222
  75. package/src/graph-teams.ts +0 -114
  76. package/src/graph-thread.test.ts +0 -252
  77. package/src/graph-thread.ts +0 -146
  78. package/src/graph-upload.test.ts +0 -253
  79. package/src/graph-upload.ts +0 -531
  80. package/src/graph-users.ts +0 -29
  81. package/src/graph.test.ts +0 -540
  82. package/src/graph.ts +0 -308
  83. package/src/inbound.test.ts +0 -221
  84. package/src/inbound.ts +0 -148
  85. package/src/index.ts +0 -4
  86. package/src/media-helpers.test.ts +0 -220
  87. package/src/media-helpers.ts +0 -105
  88. package/src/mentions.test.ts +0 -254
  89. package/src/mentions.ts +0 -114
  90. package/src/messenger.test.ts +0 -961
  91. package/src/messenger.ts +0 -608
  92. package/src/monitor-handler/access.ts +0 -136
  93. package/src/monitor-handler/inbound-media.test.ts +0 -314
  94. package/src/monitor-handler/inbound-media.ts +0 -180
  95. package/src/monitor-handler/message-handler-mock-support.test-support.ts +0 -28
  96. package/src/monitor-handler/message-handler.authz.test.ts +0 -739
  97. package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
  98. package/src/monitor-handler/message-handler.test-support.ts +0 -99
  99. package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -225
  100. package/src/monitor-handler/message-handler.thread-session.test.ts +0 -132
  101. package/src/monitor-handler/message-handler.ts +0 -1003
  102. package/src/monitor-handler/reaction-handler.test.ts +0 -325
  103. package/src/monitor-handler/reaction-handler.ts +0 -122
  104. package/src/monitor-handler/thread-session.ts +0 -30
  105. package/src/monitor-handler.adaptive-card.test.ts +0 -158
  106. package/src/monitor-handler.feedback-authz.test.ts +0 -357
  107. package/src/monitor-handler.file-consent.test.ts +0 -443
  108. package/src/monitor-handler.sso.test.ts +0 -576
  109. package/src/monitor-handler.test-helpers.ts +0 -181
  110. package/src/monitor-handler.ts +0 -538
  111. package/src/monitor-handler.types.ts +0 -27
  112. package/src/monitor-types.ts +0 -6
  113. package/src/monitor.lifecycle.test.ts +0 -457
  114. package/src/monitor.test.ts +0 -119
  115. package/src/monitor.ts +0 -476
  116. package/src/oauth.flow.ts +0 -77
  117. package/src/oauth.shared.ts +0 -37
  118. package/src/oauth.test.ts +0 -350
  119. package/src/oauth.token.ts +0 -162
  120. package/src/oauth.ts +0 -130
  121. package/src/outbound.test.ts +0 -400
  122. package/src/outbound.ts +0 -198
  123. package/src/pending-uploads-fs.test.ts +0 -261
  124. package/src/pending-uploads-fs.ts +0 -235
  125. package/src/pending-uploads.test.ts +0 -186
  126. package/src/pending-uploads.ts +0 -121
  127. package/src/policy.test.ts +0 -156
  128. package/src/policy.ts +0 -245
  129. package/src/polls-store-memory.ts +0 -32
  130. package/src/polls.test.ts +0 -169
  131. package/src/polls.ts +0 -312
  132. package/src/presentation.ts +0 -93
  133. package/src/probe.test.ts +0 -79
  134. package/src/probe.ts +0 -132
  135. package/src/reply-dispatcher.test.ts +0 -543
  136. package/src/reply-dispatcher.ts +0 -523
  137. package/src/reply-stream-controller.test.ts +0 -424
  138. package/src/reply-stream-controller.ts +0 -334
  139. package/src/resolve-allowlist.test.ts +0 -253
  140. package/src/resolve-allowlist.ts +0 -309
  141. package/src/revoked-context.ts +0 -17
  142. package/src/runtime.ts +0 -12
  143. package/src/sdk-types.ts +0 -59
  144. package/src/sdk.test.ts +0 -727
  145. package/src/sdk.ts +0 -916
  146. package/src/secret-contract.ts +0 -49
  147. package/src/secret-input.ts +0 -7
  148. package/src/send-context.test.ts +0 -93
  149. package/src/send-context.ts +0 -269
  150. package/src/send.test.ts +0 -588
  151. package/src/send.ts +0 -697
  152. package/src/sent-message-cache.test.ts +0 -106
  153. package/src/sent-message-cache.ts +0 -174
  154. package/src/session-route.ts +0 -40
  155. package/src/setup-core.ts +0 -162
  156. package/src/setup-surface.test.ts +0 -175
  157. package/src/setup-surface.ts +0 -319
  158. package/src/sso-token-store.test.ts +0 -74
  159. package/src/sso-token-store.ts +0 -166
  160. package/src/sso.ts +0 -300
  161. package/src/storage.ts +0 -25
  162. package/src/store-fs.ts +0 -42
  163. package/src/streaming-message.test.ts +0 -323
  164. package/src/streaming-message.ts +0 -327
  165. package/src/test-runtime.ts +0 -16
  166. package/src/thread-parent-context.test.ts +0 -224
  167. package/src/thread-parent-context.ts +0 -159
  168. package/src/token-response.ts +0 -11
  169. package/src/token.test.ts +0 -268
  170. package/src/token.ts +0 -194
  171. package/src/user-agent.test.ts +0 -121
  172. package/src/user-agent.ts +0 -53
  173. package/src/webhook-timeouts.ts +0 -27
  174. package/src/welcome-card.test.ts +0 -104
  175. package/src/welcome-card.ts +0 -57
  176. package/test-api.ts +0 -1
  177. package/tsconfig.json +0 -16
@@ -1,237 +0,0 @@
1
- import { mkdtemp, rm, writeFile } from "node:fs/promises";
2
- import os from "node:os";
3
- import path from "node:path";
4
- import { afterEach, describe, expect, it, vi } from "vitest";
5
- import { storeSessionLearning } from "./feedback-reflection-store.js";
6
- import {
7
- buildFeedbackEvent,
8
- buildReflectionPrompt,
9
- clearReflectionCooldowns,
10
- isReflectionAllowed,
11
- loadSessionLearnings,
12
- parseReflectionResponse,
13
- recordReflectionTime,
14
- } from "./feedback-reflection.js";
15
-
16
- describe("buildFeedbackEvent", () => {
17
- it("builds a well-formed custom event", () => {
18
- const event = buildFeedbackEvent({
19
- messageId: "msg-123",
20
- value: "negative",
21
- comment: "too verbose",
22
- sessionKey: "msteams:user1",
23
- agentId: "default",
24
- conversationId: "19:abc",
25
- });
26
-
27
- expect(event.type).toBe("custom");
28
- expect(event.event).toBe("feedback");
29
- expect(event.value).toBe("negative");
30
- expect(event.comment).toBe("too verbose");
31
- expect(event.messageId).toBe("msg-123");
32
- expect(event.ts).toBeGreaterThan(0);
33
- });
34
-
35
- it("omits comment when not provided", () => {
36
- const event = buildFeedbackEvent({
37
- messageId: "msg-123",
38
- value: "positive",
39
- sessionKey: "msteams:user1",
40
- agentId: "default",
41
- conversationId: "19:abc",
42
- });
43
-
44
- expect(event.comment).toBeUndefined();
45
- expect(event.value).toBe("positive");
46
- });
47
- });
48
-
49
- describe("buildReflectionPrompt", () => {
50
- it("includes the thumbed-down response", () => {
51
- const prompt = buildReflectionPrompt({
52
- thumbedDownResponse: "Here is a long explanation...",
53
- });
54
-
55
- expect(prompt).toContain("previous response wasn't helpful");
56
- expect(prompt).toContain("Here is a long explanation...");
57
- expect(prompt).toContain("reflect");
58
- });
59
-
60
- it("truncates long responses", () => {
61
- const longResponse = "x".repeat(600);
62
- const prompt = buildReflectionPrompt({
63
- thumbedDownResponse: longResponse,
64
- });
65
-
66
- expect(prompt).toContain("...");
67
- expect(prompt.length).toBeLessThan(longResponse.length + 500);
68
- });
69
-
70
- it("includes user comment when provided", () => {
71
- const prompt = buildReflectionPrompt({
72
- thumbedDownResponse: "Some response",
73
- userComment: "Too wordy",
74
- });
75
-
76
- expect(prompt).toContain('User\'s comment: "Too wordy"');
77
- });
78
-
79
- it("works without optional params", () => {
80
- const prompt = buildReflectionPrompt({});
81
- expect(prompt).toContain("previous response wasn't helpful");
82
- expect(prompt).toContain('"followUp":false');
83
- });
84
- });
85
-
86
- describe("parseReflectionResponse", () => {
87
- it("parses strict JSON output", () => {
88
- expect(
89
- parseReflectionResponse(
90
- '{"learning":"Be more direct next time.","followUp":true,"userMessage":"Sorry about that. I will keep it tighter."}',
91
- ),
92
- ).toEqual({
93
- learning: "Be more direct next time.",
94
- followUp: true,
95
- userMessage: "Sorry about that. I will keep it tighter.",
96
- });
97
- });
98
-
99
- it("parses JSON inside markdown fences", () => {
100
- expect(
101
- parseReflectionResponse(
102
- '```json\n{"learning":"Ask a clarifying question first.","followUp":false,"userMessage":""}\n```',
103
- ),
104
- ).toEqual({
105
- learning: "Ask a clarifying question first.",
106
- followUp: false,
107
- userMessage: undefined,
108
- });
109
- });
110
-
111
- it("falls back to internal-only learning when parsing fails", () => {
112
- expect(parseReflectionResponse("Be more concise.\nFollow up: yes.")).toEqual({
113
- learning: "Be more concise.\nFollow up: yes.",
114
- followUp: false,
115
- });
116
- });
117
- });
118
-
119
- describe("reflection cooldown", () => {
120
- afterEach(() => {
121
- clearReflectionCooldowns();
122
- vi.restoreAllMocks();
123
- });
124
-
125
- it("allows first reflection", () => {
126
- expect(isReflectionAllowed("session-1")).toBe(true);
127
- });
128
-
129
- it("blocks reflection within cooldown", () => {
130
- recordReflectionTime("session-1");
131
- expect(isReflectionAllowed("session-1", 60_000)).toBe(false);
132
- });
133
-
134
- it("allows reflection after cooldown expires", () => {
135
- // Manually set a past timestamp
136
- recordReflectionTime("session-1");
137
- // Override the map entry to simulate time passing
138
- clearReflectionCooldowns();
139
- expect(isReflectionAllowed("session-1", 1)).toBe(true);
140
- });
141
-
142
- it("tracks sessions independently", () => {
143
- recordReflectionTime("session-1");
144
- expect(isReflectionAllowed("session-1", 60_000)).toBe(false);
145
- expect(isReflectionAllowed("session-2", 60_000)).toBe(true);
146
- });
147
-
148
- it("keeps longer custom cooldown entries during pruning", () => {
149
- vi.spyOn(Date, "now").mockReturnValue(0);
150
- recordReflectionTime("target", 600_000);
151
-
152
- vi.spyOn(Date, "now").mockReturnValue(301_000);
153
- for (let index = 0; index <= 500; index += 1) {
154
- recordReflectionTime(`session-${index}`, 600_000);
155
- }
156
-
157
- expect(isReflectionAllowed("target", 600_000)).toBe(false);
158
- });
159
- });
160
-
161
- describe("loadSessionLearnings", () => {
162
- let tmpDir: string;
163
-
164
- afterEach(async () => {
165
- if (tmpDir) {
166
- await rm(tmpDir, { recursive: true, force: true });
167
- }
168
- });
169
-
170
- it("returns empty array when file doesn't exist", async () => {
171
- tmpDir = await mkdtemp(path.join(os.tmpdir(), "learnings-test-"));
172
- const learnings = await loadSessionLearnings(tmpDir, "nonexistent");
173
- expect(learnings).toStrictEqual([]);
174
- });
175
-
176
- it("reads existing learnings", async () => {
177
- tmpDir = await mkdtemp(path.join(os.tmpdir(), "learnings-test-"));
178
- const safeKey = Buffer.from("msteams:user1", "utf8").toString("base64url");
179
- const filePath = path.join(tmpDir, `${safeKey}.learnings.json`);
180
- await writeFile(filePath, JSON.stringify(["Be concise", "Use examples"]), "utf-8");
181
-
182
- const learnings = await loadSessionLearnings(tmpDir, "msteams:user1");
183
- expect(learnings).toEqual(["Be concise", "Use examples"]);
184
- });
185
-
186
- it("keeps distinct session keys isolated across the filename persistence boundary", async () => {
187
- tmpDir = await mkdtemp(path.join(os.tmpdir(), "learnings-test-"));
188
-
189
- await storeSessionLearning({
190
- storePath: tmpDir,
191
- sessionKey: "msteams:user1",
192
- learning: "Use bullets",
193
- });
194
- await storeSessionLearning({
195
- storePath: tmpDir,
196
- sessionKey: "msteams/user1",
197
- learning: "Avoid bullets",
198
- });
199
-
200
- await expect(loadSessionLearnings(tmpDir, "msteams:user1")).resolves.toEqual(["Use bullets"]);
201
- await expect(loadSessionLearnings(tmpDir, "msteams/user1")).resolves.toEqual(["Avoid bullets"]);
202
- });
203
-
204
- it("reads and migrates legacy sanitized session learning files", async () => {
205
- tmpDir = await mkdtemp(path.join(os.tmpdir(), "learnings-test-"));
206
- const legacyFile = path.join(tmpDir, "msteams_user1.learnings.json");
207
- await writeFile(legacyFile, JSON.stringify(["Legacy learning"]), "utf-8");
208
-
209
- await expect(loadSessionLearnings(tmpDir, "msteams:user1")).resolves.toEqual([
210
- "Legacy learning",
211
- ]);
212
-
213
- await storeSessionLearning({
214
- storePath: tmpDir,
215
- sessionKey: "msteams:user1",
216
- learning: "New learning",
217
- });
218
-
219
- const migratedFile = path.join(
220
- tmpDir,
221
- `${Buffer.from("msteams:user1", "utf8").toString("base64url")}.learnings.json`,
222
- );
223
- await expect(loadSessionLearnings(tmpDir, "msteams:user1")).resolves.toEqual([
224
- "Legacy learning",
225
- "New learning",
226
- ]);
227
- await expect(rm(legacyFile, { force: false })).rejects.toHaveProperty("code", "ENOENT");
228
- await expect(loadSessionLearnings(tmpDir, "msteams:user1")).resolves.toEqual([
229
- "Legacy learning",
230
- "New learning",
231
- ]);
232
- await expect(loadSessionLearnings(tmpDir, "msteams/user1")).resolves.toStrictEqual([]);
233
- await expect(
234
- import("node:fs/promises").then((fs) => fs.readFile(migratedFile, "utf-8")),
235
- ).resolves.toContain("Legacy learning");
236
- });
237
- });
@@ -1,268 +0,0 @@
1
- import { normalizeOptionalLowercaseString } from "klaw/plugin-sdk/string-coerce-runtime";
2
- import { dispatchReplyFromConfigWithSettledDispatcher, type KlawConfig } from "../runtime-api.js";
3
- import type { StoredConversationReference } from "./conversation-store.js";
4
- import { formatUnknownError } from "./errors.js";
5
- import { buildReflectionPrompt, parseReflectionResponse } from "./feedback-reflection-prompt.js";
6
- import {
7
- DEFAULT_COOLDOWN_MS,
8
- clearReflectionCooldowns,
9
- isReflectionAllowed,
10
- loadSessionLearnings,
11
- recordReflectionTime,
12
- storeSessionLearning,
13
- } from "./feedback-reflection-store.js";
14
- import type { MSTeamsAdapter } from "./messenger.js";
15
- import { buildConversationReference } from "./messenger.js";
16
- import type { MSTeamsMonitorLogger } from "./monitor-types.js";
17
- import { getMSTeamsRuntime } from "./runtime.js";
18
-
19
- type FeedbackEvent = {
20
- type: "custom";
21
- event: "feedback";
22
- ts: number;
23
- messageId: string;
24
- value: "positive" | "negative";
25
- comment?: string;
26
- sessionKey: string;
27
- agentId: string;
28
- conversationId: string;
29
- reflectionLearning?: string;
30
- };
31
-
32
- export function buildFeedbackEvent(params: {
33
- messageId: string;
34
- value: "positive" | "negative";
35
- comment?: string;
36
- sessionKey: string;
37
- agentId: string;
38
- conversationId: string;
39
- }): FeedbackEvent {
40
- return {
41
- type: "custom",
42
- event: "feedback",
43
- ts: Date.now(),
44
- messageId: params.messageId,
45
- value: params.value,
46
- comment: params.comment,
47
- sessionKey: params.sessionKey,
48
- agentId: params.agentId,
49
- conversationId: params.conversationId,
50
- };
51
- }
52
-
53
- type RunFeedbackReflectionParams = {
54
- cfg: KlawConfig;
55
- adapter: MSTeamsAdapter;
56
- appId: string;
57
- conversationRef: StoredConversationReference;
58
- sessionKey: string;
59
- agentId: string;
60
- conversationId: string;
61
- feedbackMessageId: string;
62
- thumbedDownResponse?: string;
63
- userComment?: string;
64
- log: MSTeamsMonitorLogger;
65
- };
66
-
67
- function buildReflectionContext(params: {
68
- cfg: KlawConfig;
69
- conversationId: string;
70
- sessionKey: string;
71
- reflectionPrompt: string;
72
- }) {
73
- const core = getMSTeamsRuntime();
74
- const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(params.cfg);
75
- const body = core.channel.reply.formatAgentEnvelope({
76
- channel: "Teams",
77
- from: "system",
78
- body: params.reflectionPrompt,
79
- envelope: envelopeOptions,
80
- });
81
-
82
- return {
83
- ctxPayload: core.channel.reply.finalizeInboundContext({
84
- Body: body,
85
- BodyForAgent: params.reflectionPrompt,
86
- RawBody: params.reflectionPrompt,
87
- CommandBody: params.reflectionPrompt,
88
- From: `msteams:system:${params.conversationId}`,
89
- To: `conversation:${params.conversationId}`,
90
- SessionKey: params.sessionKey,
91
- ChatType: "direct" as const,
92
- SenderName: "system",
93
- SenderId: "system",
94
- Provider: "msteams" as const,
95
- Surface: "msteams" as const,
96
- Timestamp: Date.now(),
97
- WasMentioned: true,
98
- CommandAuthorized: false,
99
- OriginatingChannel: "msteams" as const,
100
- OriginatingTo: `conversation:${params.conversationId}`,
101
- }),
102
- };
103
- }
104
-
105
- function createReflectionCaptureDispatcher(params: {
106
- cfg: KlawConfig;
107
- agentId: string;
108
- log: MSTeamsMonitorLogger;
109
- }) {
110
- const core = getMSTeamsRuntime();
111
- let response = "";
112
- const noopTypingCallbacks = {
113
- onReplyStart: async () => {},
114
- onIdle: () => {},
115
- onCleanup: () => {},
116
- };
117
-
118
- const { dispatcher, replyOptions } = core.channel.reply.createReplyDispatcherWithTyping({
119
- deliver: async (payload) => {
120
- if (payload.text) {
121
- response += (response ? "\n" : "") + payload.text;
122
- }
123
- },
124
- typingCallbacks: noopTypingCallbacks,
125
- humanDelay: core.channel.reply.resolveHumanDelayConfig(params.cfg, params.agentId),
126
- onError: (err) => {
127
- params.log.debug?.("reflection reply error", { error: formatUnknownError(err) });
128
- },
129
- });
130
-
131
- return {
132
- dispatcher,
133
- replyOptions,
134
- readResponse: () => response,
135
- };
136
- }
137
-
138
- async function sendReflectionFollowUp(params: {
139
- adapter: MSTeamsAdapter;
140
- appId: string;
141
- conversationRef: StoredConversationReference;
142
- userMessage: string;
143
- }): Promise<void> {
144
- const baseRef = buildConversationReference(params.conversationRef);
145
- const proactiveRef = { ...baseRef, activityId: undefined };
146
-
147
- await params.adapter.continueConversation(params.appId, proactiveRef, async (ctx) => {
148
- await ctx.sendActivity({
149
- type: "message",
150
- text: params.userMessage,
151
- });
152
- });
153
- }
154
-
155
- /**
156
- * Run a background reflection after negative feedback.
157
- * This is designed to be called fire-and-forget (don't await in the invoke handler).
158
- */
159
- export async function runFeedbackReflection(params: RunFeedbackReflectionParams): Promise<void> {
160
- const { cfg, log, sessionKey } = params;
161
- const cooldownMs = cfg.channels?.msteams?.feedbackReflectionCooldownMs ?? DEFAULT_COOLDOWN_MS;
162
- if (!isReflectionAllowed(sessionKey, cooldownMs)) {
163
- log.debug?.("skipping reflection (cooldown active)", { sessionKey });
164
- return;
165
- }
166
-
167
- const reflectionPrompt = buildReflectionPrompt({
168
- thumbedDownResponse: params.thumbedDownResponse,
169
- userComment: params.userComment,
170
- });
171
- const runtime = getMSTeamsRuntime();
172
- const storePath = runtime.channel.session.resolveStorePath(cfg.session?.store, {
173
- agentId: params.agentId,
174
- });
175
- const { ctxPayload } = buildReflectionContext({
176
- cfg,
177
- conversationId: params.conversationId,
178
- sessionKey: params.sessionKey,
179
- reflectionPrompt,
180
- });
181
-
182
- const capture = createReflectionCaptureDispatcher({
183
- cfg,
184
- agentId: params.agentId,
185
- log,
186
- });
187
-
188
- try {
189
- await dispatchReplyFromConfigWithSettledDispatcher({
190
- ctxPayload,
191
- cfg,
192
- dispatcher: capture.dispatcher,
193
- onSettled: () => {},
194
- replyOptions: capture.replyOptions,
195
- });
196
- } catch (err) {
197
- log.error("reflection dispatch failed", { error: formatUnknownError(err) });
198
- return;
199
- }
200
-
201
- const reflectionResponse = capture.readResponse().trim();
202
- if (!reflectionResponse) {
203
- log.debug?.("reflection produced no output");
204
- return;
205
- }
206
-
207
- const parsedReflection = parseReflectionResponse(reflectionResponse);
208
- if (!parsedReflection) {
209
- log.debug?.("reflection produced no structured output");
210
- return;
211
- }
212
-
213
- recordReflectionTime(sessionKey, cooldownMs);
214
- log.info("reflection complete", {
215
- sessionKey,
216
- responseLength: reflectionResponse.length,
217
- followUp: parsedReflection.followUp,
218
- });
219
-
220
- try {
221
- await storeSessionLearning({
222
- storePath,
223
- sessionKey: params.sessionKey,
224
- learning: parsedReflection.learning,
225
- });
226
- } catch (err) {
227
- log.debug?.("failed to store reflection learning", { error: formatUnknownError(err) });
228
- }
229
-
230
- const conversationType = normalizeOptionalLowercaseString(
231
- params.conversationRef.conversation?.conversationType,
232
- );
233
- const shouldNotify =
234
- conversationType === "personal" &&
235
- parsedReflection.followUp &&
236
- Boolean(parsedReflection.userMessage);
237
-
238
- if (!shouldNotify) {
239
- if (parsedReflection.followUp && conversationType !== "personal") {
240
- log.debug?.("skipping reflection follow-up outside direct message", {
241
- sessionKey,
242
- conversationType,
243
- });
244
- }
245
- return;
246
- }
247
-
248
- try {
249
- await sendReflectionFollowUp({
250
- adapter: params.adapter,
251
- appId: params.appId,
252
- conversationRef: params.conversationRef,
253
- userMessage: parsedReflection.userMessage!,
254
- });
255
- log.info("sent reflection follow-up", { sessionKey });
256
- } catch (err) {
257
- log.debug?.("failed to send reflection follow-up", { error: formatUnknownError(err) });
258
- }
259
- }
260
-
261
- export {
262
- buildReflectionPrompt,
263
- clearReflectionCooldowns,
264
- isReflectionAllowed,
265
- loadSessionLearnings,
266
- parseReflectionResponse,
267
- recordReflectionTime,
268
- };