@openclaw/msteams 2026.5.2 → 2026.5.3-beta.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/dist/api.js +3 -0
- package/dist/channel-D7hdreTh.js +984 -0
- package/dist/channel-config-api.js +2 -0
- package/dist/channel-plugin-api.js +2 -0
- package/dist/channel.runtime-BC1ruIfN.js +573 -0
- package/dist/config-schema-B8QezH6t.js +15 -0
- package/dist/contract-api.js +2 -0
- package/dist/graph-users-9uQJepqr.js +1354 -0
- package/dist/index.js +22 -0
- package/dist/oauth-BWJyilR1.js +114 -0
- package/dist/oauth.token-xxpoLWy5.js +115 -0
- package/dist/policy-DTnU2GR7.js +142 -0
- package/dist/probe-D_H8yFps.js +2194 -0
- package/dist/resolve-allowlist-D41JSziq.js +219 -0
- package/dist/runtime-api-DV1iVMn1.js +28 -0
- package/dist/runtime-api.js +2 -0
- package/dist/secret-contract-BuoEXmPS.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-BLkFQYIQ.js +313 -0
- package/dist/src-CFp1QpFd.js +4064 -0
- package/dist/test-api.js +2 -0
- package/package.json +14 -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 -73
- 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 -461
- package/src/attachments/bot-framework.ts +0 -362
- package/src/attachments/download.ts +0 -311
- package/src/attachments/graph.test.ts +0 -416
- package/src/attachments/graph.ts +0 -484
- package/src/attachments/html.ts +0 -122
- package/src/attachments/payload.ts +0 -14
- package/src/attachments/remote-media.test.ts +0 -137
- package/src/attachments/remote-media.ts +0 -112
- package/src/attachments/shared.test.ts +0 -530
- package/src/attachments/shared.ts +0 -626
- package/src/attachments/types.ts +0 -47
- package/src/attachments.graph.test.ts +0 -342
- package/src/attachments.helpers.test.ts +0 -246
- package/src/attachments.test-helpers.ts +0 -17
- package/src/attachments.test.ts +0 -687
- 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 -742
- package/src/channel.directory.test.ts +0 -200
- package/src/channel.runtime.ts +0 -56
- package/src/channel.setup.ts +0 -77
- package/src/channel.test.ts +0 -128
- package/src/channel.ts +0 -1136
- package/src/config-schema.ts +0 -6
- package/src/config-ui-hints.ts +0 -12
- package/src/conversation-store-fs.test.ts +0 -74
- 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 -225
- 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 -133
- package/src/errors.ts +0 -246
- package/src/feedback-reflection-prompt.ts +0 -117
- package/src/feedback-reflection-store.ts +0 -114
- package/src/feedback-reflection.test.ts +0 -237
- package/src/feedback-reflection.ts +0 -283
- package/src/file-consent-helpers.test.ts +0 -326
- package/src/file-consent-helpers.ts +0 -126
- package/src/file-consent-invoke.ts +0 -150
- package/src/file-consent.test.ts +0 -363
- package/src/file-consent.ts +0 -287
- package/src/graph-chat.ts +0 -55
- package/src/graph-group-management.test.ts +0 -318
- 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 -243
- package/src/graph-messages.read.test.ts +0 -391
- package/src/graph-messages.search.test.ts +0 -213
- package/src/graph-messages.test-helpers.ts +0 -50
- package/src/graph-messages.ts +0 -534
- package/src/graph-teams.test.ts +0 -215
- package/src/graph-teams.ts +0 -114
- package/src/graph-thread.test.ts +0 -246
- package/src/graph-thread.ts +0 -146
- package/src/graph-upload.test.ts +0 -258
- package/src/graph-upload.ts +0 -531
- package/src/graph-users.ts +0 -29
- package/src/graph.test.ts +0 -516
- package/src/graph.ts +0 -293
- 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 -202
- package/src/media-helpers.ts +0 -105
- package/src/mentions.test.ts +0 -244
- package/src/mentions.ts +0 -114
- package/src/messenger.test.ts +0 -865
- package/src/messenger.ts +0 -605
- package/src/monitor-handler/access.ts +0 -125
- package/src/monitor-handler/inbound-media.test.ts +0 -289
- 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 -669
- package/src/monitor-handler/message-handler.dm-media.test.ts +0 -54
- package/src/monitor-handler/message-handler.test-support.ts +0 -100
- package/src/monitor-handler/message-handler.thread-parent.test.ts +0 -223
- package/src/monitor-handler/message-handler.thread-session.test.ts +0 -77
- package/src/monitor-handler/message-handler.ts +0 -1000
- package/src/monitor-handler/reaction-handler.test.ts +0 -267
- package/src/monitor-handler/reaction-handler.ts +0 -210
- package/src/monitor-handler/thread-session.ts +0 -17
- package/src/monitor-handler.adaptive-card.test.ts +0 -162
- package/src/monitor-handler.feedback-authz.test.ts +0 -314
- package/src/monitor-handler.file-consent.test.ts +0 -423
- package/src/monitor-handler.sso.test.ts +0 -563
- package/src/monitor-handler.test-helpers.ts +0 -180
- package/src/monitor-handler.ts +0 -534
- package/src/monitor-handler.types.ts +0 -27
- package/src/monitor-types.ts +0 -6
- package/src/monitor.lifecycle.test.ts +0 -278
- package/src/monitor.test.ts +0 -119
- package/src/monitor.ts +0 -442
- package/src/oauth.flow.ts +0 -77
- package/src/oauth.shared.ts +0 -37
- package/src/oauth.test.ts +0 -305
- package/src/oauth.token.ts +0 -158
- package/src/oauth.ts +0 -130
- package/src/outbound.test.ts +0 -130
- package/src/outbound.ts +0 -71
- package/src/pending-uploads-fs.test.ts +0 -246
- package/src/pending-uploads-fs.ts +0 -235
- package/src/pending-uploads.test.ts +0 -173
- package/src/pending-uploads.ts +0 -121
- package/src/policy.test.ts +0 -240
- package/src/policy.ts +0 -262
- package/src/polls-store-memory.ts +0 -32
- package/src/polls.test.ts +0 -160
- package/src/polls.ts +0 -323
- package/src/presentation.ts +0 -68
- package/src/probe.test.ts +0 -77
- package/src/probe.ts +0 -132
- package/src/reply-dispatcher.test.ts +0 -437
- package/src/reply-dispatcher.ts +0 -346
- package/src/reply-stream-controller.test.ts +0 -235
- package/src/reply-stream-controller.ts +0 -147
- package/src/resolve-allowlist.test.ts +0 -250
- package/src/resolve-allowlist.ts +0 -309
- package/src/revoked-context.ts +0 -17
- package/src/runtime.ts +0 -9
- package/src/sdk-types.ts +0 -59
- package/src/sdk.test.ts +0 -666
- package/src/sdk.ts +0 -884
- package/src/secret-contract.ts +0 -49
- package/src/secret-input.ts +0 -7
- package/src/send-context.ts +0 -231
- package/src/send.test.ts +0 -493
- package/src/send.ts +0 -637
- package/src/sent-message-cache.test.ts +0 -15
- package/src/sent-message-cache.ts +0 -56
- package/src/session-route.ts +0 -40
- package/src/setup-core.ts +0 -160
- package/src/setup-surface.test.ts +0 -202
- package/src/setup-surface.ts +0 -320
- package/src/sso-token-store.test.ts +0 -72
- 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 -44
- package/src/streaming-message.test.ts +0 -262
- package/src/streaming-message.ts +0 -297
- 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 -259
- package/src/token.ts +0 -195
- package/src/user-agent.test.ts +0 -86
- package/src/user-agent.ts +0 -53
- package/src/webhook-timeouts.ts +0 -27
- package/src/welcome-card.test.ts +0 -81
- package/src/welcome-card.ts +0 -57
- package/test-api.ts +0 -1
- 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).toEqual([]);
|
|
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.toMatchObject({ 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.toEqual([]);
|
|
233
|
-
await expect(
|
|
234
|
-
import("node:fs/promises").then((fs) => fs.readFile(migratedFile, "utf-8")),
|
|
235
|
-
).resolves.toContain("Legacy learning");
|
|
236
|
-
});
|
|
237
|
-
});
|
|
@@ -1,283 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* Background reflection triggered by negative user feedback (thumbs-down).
|
|
3
|
-
*
|
|
4
|
-
* Flow:
|
|
5
|
-
* 1. User thumbs-down -> invoke handler acks immediately
|
|
6
|
-
* 2. This module runs in the background (fire-and-forget)
|
|
7
|
-
* 3. Reads recent session context
|
|
8
|
-
* 4. Sends a synthetic reflection prompt to the agent
|
|
9
|
-
* 5. Stores the derived learning in session
|
|
10
|
-
* 6. Optionally sends a proactive follow-up to the user
|
|
11
|
-
*/
|
|
12
|
-
|
|
13
|
-
import { normalizeOptionalLowercaseString } from "openclaw/plugin-sdk/text-runtime";
|
|
14
|
-
import {
|
|
15
|
-
dispatchReplyFromConfigWithSettledDispatcher,
|
|
16
|
-
type OpenClawConfig,
|
|
17
|
-
} from "../runtime-api.js";
|
|
18
|
-
import type { StoredConversationReference } from "./conversation-store.js";
|
|
19
|
-
import { formatUnknownError } from "./errors.js";
|
|
20
|
-
import { buildReflectionPrompt, parseReflectionResponse } from "./feedback-reflection-prompt.js";
|
|
21
|
-
import {
|
|
22
|
-
DEFAULT_COOLDOWN_MS,
|
|
23
|
-
clearReflectionCooldowns,
|
|
24
|
-
isReflectionAllowed,
|
|
25
|
-
loadSessionLearnings,
|
|
26
|
-
recordReflectionTime,
|
|
27
|
-
storeSessionLearning,
|
|
28
|
-
} from "./feedback-reflection-store.js";
|
|
29
|
-
import type { MSTeamsAdapter } from "./messenger.js";
|
|
30
|
-
import { buildConversationReference } from "./messenger.js";
|
|
31
|
-
import type { MSTeamsMonitorLogger } from "./monitor-types.js";
|
|
32
|
-
import { getMSTeamsRuntime } from "./runtime.js";
|
|
33
|
-
|
|
34
|
-
type FeedbackEvent = {
|
|
35
|
-
type: "custom";
|
|
36
|
-
event: "feedback";
|
|
37
|
-
ts: number;
|
|
38
|
-
messageId: string;
|
|
39
|
-
value: "positive" | "negative";
|
|
40
|
-
comment?: string;
|
|
41
|
-
sessionKey: string;
|
|
42
|
-
agentId: string;
|
|
43
|
-
conversationId: string;
|
|
44
|
-
reflectionLearning?: string;
|
|
45
|
-
};
|
|
46
|
-
|
|
47
|
-
export function buildFeedbackEvent(params: {
|
|
48
|
-
messageId: string;
|
|
49
|
-
value: "positive" | "negative";
|
|
50
|
-
comment?: string;
|
|
51
|
-
sessionKey: string;
|
|
52
|
-
agentId: string;
|
|
53
|
-
conversationId: string;
|
|
54
|
-
}): FeedbackEvent {
|
|
55
|
-
return {
|
|
56
|
-
type: "custom",
|
|
57
|
-
event: "feedback",
|
|
58
|
-
ts: Date.now(),
|
|
59
|
-
messageId: params.messageId,
|
|
60
|
-
value: params.value,
|
|
61
|
-
comment: params.comment,
|
|
62
|
-
sessionKey: params.sessionKey,
|
|
63
|
-
agentId: params.agentId,
|
|
64
|
-
conversationId: params.conversationId,
|
|
65
|
-
};
|
|
66
|
-
}
|
|
67
|
-
|
|
68
|
-
type RunFeedbackReflectionParams = {
|
|
69
|
-
cfg: OpenClawConfig;
|
|
70
|
-
adapter: MSTeamsAdapter;
|
|
71
|
-
appId: string;
|
|
72
|
-
conversationRef: StoredConversationReference;
|
|
73
|
-
sessionKey: string;
|
|
74
|
-
agentId: string;
|
|
75
|
-
conversationId: string;
|
|
76
|
-
feedbackMessageId: string;
|
|
77
|
-
thumbedDownResponse?: string;
|
|
78
|
-
userComment?: string;
|
|
79
|
-
log: MSTeamsMonitorLogger;
|
|
80
|
-
};
|
|
81
|
-
|
|
82
|
-
function buildReflectionContext(params: {
|
|
83
|
-
cfg: OpenClawConfig;
|
|
84
|
-
conversationId: string;
|
|
85
|
-
sessionKey: string;
|
|
86
|
-
reflectionPrompt: string;
|
|
87
|
-
}) {
|
|
88
|
-
const core = getMSTeamsRuntime();
|
|
89
|
-
const envelopeOptions = core.channel.reply.resolveEnvelopeFormatOptions(params.cfg);
|
|
90
|
-
const body = core.channel.reply.formatAgentEnvelope({
|
|
91
|
-
channel: "Teams",
|
|
92
|
-
from: "system",
|
|
93
|
-
body: params.reflectionPrompt,
|
|
94
|
-
envelope: envelopeOptions,
|
|
95
|
-
});
|
|
96
|
-
|
|
97
|
-
return {
|
|
98
|
-
ctxPayload: core.channel.reply.finalizeInboundContext({
|
|
99
|
-
Body: body,
|
|
100
|
-
BodyForAgent: params.reflectionPrompt,
|
|
101
|
-
RawBody: params.reflectionPrompt,
|
|
102
|
-
CommandBody: params.reflectionPrompt,
|
|
103
|
-
From: `msteams:system:${params.conversationId}`,
|
|
104
|
-
To: `conversation:${params.conversationId}`,
|
|
105
|
-
SessionKey: params.sessionKey,
|
|
106
|
-
ChatType: "direct" as const,
|
|
107
|
-
SenderName: "system",
|
|
108
|
-
SenderId: "system",
|
|
109
|
-
Provider: "msteams" as const,
|
|
110
|
-
Surface: "msteams" as const,
|
|
111
|
-
Timestamp: Date.now(),
|
|
112
|
-
WasMentioned: true,
|
|
113
|
-
CommandAuthorized: false,
|
|
114
|
-
OriginatingChannel: "msteams" as const,
|
|
115
|
-
OriginatingTo: `conversation:${params.conversationId}`,
|
|
116
|
-
}),
|
|
117
|
-
};
|
|
118
|
-
}
|
|
119
|
-
|
|
120
|
-
function createReflectionCaptureDispatcher(params: {
|
|
121
|
-
cfg: OpenClawConfig;
|
|
122
|
-
agentId: string;
|
|
123
|
-
log: MSTeamsMonitorLogger;
|
|
124
|
-
}) {
|
|
125
|
-
const core = getMSTeamsRuntime();
|
|
126
|
-
let response = "";
|
|
127
|
-
const noopTypingCallbacks = {
|
|
128
|
-
onReplyStart: async () => {},
|
|
129
|
-
onIdle: () => {},
|
|
130
|
-
onCleanup: () => {},
|
|
131
|
-
};
|
|
132
|
-
|
|
133
|
-
const { dispatcher, replyOptions } = core.channel.reply.createReplyDispatcherWithTyping({
|
|
134
|
-
deliver: async (payload) => {
|
|
135
|
-
if (payload.text) {
|
|
136
|
-
response += (response ? "\n" : "") + payload.text;
|
|
137
|
-
}
|
|
138
|
-
},
|
|
139
|
-
typingCallbacks: noopTypingCallbacks,
|
|
140
|
-
humanDelay: core.channel.reply.resolveHumanDelayConfig(params.cfg, params.agentId),
|
|
141
|
-
onError: (err) => {
|
|
142
|
-
params.log.debug?.("reflection reply error", { error: formatUnknownError(err) });
|
|
143
|
-
},
|
|
144
|
-
});
|
|
145
|
-
|
|
146
|
-
return {
|
|
147
|
-
dispatcher,
|
|
148
|
-
replyOptions,
|
|
149
|
-
readResponse: () => response,
|
|
150
|
-
};
|
|
151
|
-
}
|
|
152
|
-
|
|
153
|
-
async function sendReflectionFollowUp(params: {
|
|
154
|
-
adapter: MSTeamsAdapter;
|
|
155
|
-
appId: string;
|
|
156
|
-
conversationRef: StoredConversationReference;
|
|
157
|
-
userMessage: string;
|
|
158
|
-
}): Promise<void> {
|
|
159
|
-
const baseRef = buildConversationReference(params.conversationRef);
|
|
160
|
-
const proactiveRef = { ...baseRef, activityId: undefined };
|
|
161
|
-
|
|
162
|
-
await params.adapter.continueConversation(params.appId, proactiveRef, async (ctx) => {
|
|
163
|
-
await ctx.sendActivity({
|
|
164
|
-
type: "message",
|
|
165
|
-
text: params.userMessage,
|
|
166
|
-
});
|
|
167
|
-
});
|
|
168
|
-
}
|
|
169
|
-
|
|
170
|
-
/**
|
|
171
|
-
* Run a background reflection after negative feedback.
|
|
172
|
-
* This is designed to be called fire-and-forget (don't await in the invoke handler).
|
|
173
|
-
*/
|
|
174
|
-
export async function runFeedbackReflection(params: RunFeedbackReflectionParams): Promise<void> {
|
|
175
|
-
const { cfg, log, sessionKey } = params;
|
|
176
|
-
const cooldownMs = cfg.channels?.msteams?.feedbackReflectionCooldownMs ?? DEFAULT_COOLDOWN_MS;
|
|
177
|
-
if (!isReflectionAllowed(sessionKey, cooldownMs)) {
|
|
178
|
-
log.debug?.("skipping reflection (cooldown active)", { sessionKey });
|
|
179
|
-
return;
|
|
180
|
-
}
|
|
181
|
-
|
|
182
|
-
const reflectionPrompt = buildReflectionPrompt({
|
|
183
|
-
thumbedDownResponse: params.thumbedDownResponse,
|
|
184
|
-
userComment: params.userComment,
|
|
185
|
-
});
|
|
186
|
-
const runtime = getMSTeamsRuntime();
|
|
187
|
-
const storePath = runtime.channel.session.resolveStorePath(cfg.session?.store, {
|
|
188
|
-
agentId: params.agentId,
|
|
189
|
-
});
|
|
190
|
-
const { ctxPayload } = buildReflectionContext({
|
|
191
|
-
cfg,
|
|
192
|
-
conversationId: params.conversationId,
|
|
193
|
-
sessionKey: params.sessionKey,
|
|
194
|
-
reflectionPrompt,
|
|
195
|
-
});
|
|
196
|
-
|
|
197
|
-
const capture = createReflectionCaptureDispatcher({
|
|
198
|
-
cfg,
|
|
199
|
-
agentId: params.agentId,
|
|
200
|
-
log,
|
|
201
|
-
});
|
|
202
|
-
|
|
203
|
-
try {
|
|
204
|
-
await dispatchReplyFromConfigWithSettledDispatcher({
|
|
205
|
-
ctxPayload,
|
|
206
|
-
cfg,
|
|
207
|
-
dispatcher: capture.dispatcher,
|
|
208
|
-
onSettled: () => {},
|
|
209
|
-
replyOptions: capture.replyOptions,
|
|
210
|
-
});
|
|
211
|
-
} catch (err) {
|
|
212
|
-
log.error("reflection dispatch failed", { error: formatUnknownError(err) });
|
|
213
|
-
return;
|
|
214
|
-
}
|
|
215
|
-
|
|
216
|
-
const reflectionResponse = capture.readResponse().trim();
|
|
217
|
-
if (!reflectionResponse) {
|
|
218
|
-
log.debug?.("reflection produced no output");
|
|
219
|
-
return;
|
|
220
|
-
}
|
|
221
|
-
|
|
222
|
-
const parsedReflection = parseReflectionResponse(reflectionResponse);
|
|
223
|
-
if (!parsedReflection) {
|
|
224
|
-
log.debug?.("reflection produced no structured output");
|
|
225
|
-
return;
|
|
226
|
-
}
|
|
227
|
-
|
|
228
|
-
recordReflectionTime(sessionKey, cooldownMs);
|
|
229
|
-
log.info("reflection complete", {
|
|
230
|
-
sessionKey,
|
|
231
|
-
responseLength: reflectionResponse.length,
|
|
232
|
-
followUp: parsedReflection.followUp,
|
|
233
|
-
});
|
|
234
|
-
|
|
235
|
-
try {
|
|
236
|
-
await storeSessionLearning({
|
|
237
|
-
storePath,
|
|
238
|
-
sessionKey: params.sessionKey,
|
|
239
|
-
learning: parsedReflection.learning,
|
|
240
|
-
});
|
|
241
|
-
} catch (err) {
|
|
242
|
-
log.debug?.("failed to store reflection learning", { error: formatUnknownError(err) });
|
|
243
|
-
}
|
|
244
|
-
|
|
245
|
-
const conversationType = normalizeOptionalLowercaseString(
|
|
246
|
-
params.conversationRef.conversation?.conversationType,
|
|
247
|
-
);
|
|
248
|
-
const shouldNotify =
|
|
249
|
-
conversationType === "personal" &&
|
|
250
|
-
parsedReflection.followUp &&
|
|
251
|
-
Boolean(parsedReflection.userMessage);
|
|
252
|
-
|
|
253
|
-
if (!shouldNotify) {
|
|
254
|
-
if (parsedReflection.followUp && conversationType !== "personal") {
|
|
255
|
-
log.debug?.("skipping reflection follow-up outside direct message", {
|
|
256
|
-
sessionKey,
|
|
257
|
-
conversationType,
|
|
258
|
-
});
|
|
259
|
-
}
|
|
260
|
-
return;
|
|
261
|
-
}
|
|
262
|
-
|
|
263
|
-
try {
|
|
264
|
-
await sendReflectionFollowUp({
|
|
265
|
-
adapter: params.adapter,
|
|
266
|
-
appId: params.appId,
|
|
267
|
-
conversationRef: params.conversationRef,
|
|
268
|
-
userMessage: parsedReflection.userMessage!,
|
|
269
|
-
});
|
|
270
|
-
log.info("sent reflection follow-up", { sessionKey });
|
|
271
|
-
} catch (err) {
|
|
272
|
-
log.debug?.("failed to send reflection follow-up", { error: formatUnknownError(err) });
|
|
273
|
-
}
|
|
274
|
-
}
|
|
275
|
-
|
|
276
|
-
export {
|
|
277
|
-
buildReflectionPrompt,
|
|
278
|
-
clearReflectionCooldowns,
|
|
279
|
-
isReflectionAllowed,
|
|
280
|
-
loadSessionLearnings,
|
|
281
|
-
parseReflectionResponse,
|
|
282
|
-
recordReflectionTime,
|
|
283
|
-
};
|