|
1 | 1 | import { describe, expect, it } from "vitest"; |
2 | | -import { buildInboundUserContextPrefix } from "../../../src/auto-reply/reply/inbound-meta.js"; |
3 | | -import { buildReplyPromptEnvelopeBase } from "../../../src/auto-reply/reply/prompt-prelude.js"; |
4 | 2 | import { buildTelegramMessageContextForTest } from "./bot-message-context.test-harness.js"; |
5 | 3 | import type { TelegramPromptContextEntry } from "./bot-message-context.types.js"; |
6 | 4 |
|
7 | | -type RoomEventPromptContext = Parameters<typeof buildInboundUserContextPrefix>[0] & |
8 | | - Parameters<typeof buildReplyPromptEnvelopeBase>[0]["ctx"]; |
9 | | - |
10 | | -function renderRoomEventPromptText(ctx: RoomEventPromptContext): string { |
11 | | - const inboundUserContext = buildInboundUserContextPrefix(ctx); |
12 | | - return ( |
13 | | - buildReplyPromptEnvelopeBase({ |
14 | | - ctx, |
15 | | - sessionCtx: ctx, |
16 | | - baseBody: ctx.BodyForAgent ?? ctx.Body ?? ctx.RawBody ?? "", |
17 | | - hasUserBody: true, |
18 | | - inboundUserContext, |
19 | | - isBareSessionReset: false, |
20 | | - startupAction: "new", |
21 | | - inboundEventKind: "room_event", |
22 | | - sourceReplyDeliveryMode: "message_tool_only", |
23 | | - }).currentInboundContext?.text ?? "" |
24 | | - ); |
25 | | -} |
26 | | - |
27 | 5 | const telegramChatWindowContext: TelegramPromptContextEntry = { |
28 | 6 | label: "Conversation context", |
29 | 7 | source: "telegram", |
@@ -228,6 +206,49 @@ describe("buildTelegramMessageContext prompt context", () => { |
228 | 206 | ); |
229 | 207 | }); |
230 | 208 |
|
| 209 | + it("applies the ambient watermark before truncating the history window", async () => { |
| 210 | + const ctx = await buildTelegramMessageContextForTest({ |
| 211 | + message: { |
| 212 | + message_id: 13, |
| 213 | + chat: { id: -1001234567890, type: "supergroup", title: "Forum" }, |
| 214 | + from: { id: 1234, first_name: "Pat" }, |
| 215 | + text: "@bot what happened?", |
| 216 | + entities: [{ type: "mention", offset: 0, length: 4 }], |
| 217 | + }, |
| 218 | + historyLimit: 1, |
| 219 | + groupHistories: new Map([ |
| 220 | + [ |
| 221 | + "-1001234567890", |
| 222 | + [ |
| 223 | + { |
| 224 | + messageId: "12", |
| 225 | + sender: "Mira", |
| 226 | + timestamp: 1_700_000_002_000, |
| 227 | + body: "unpersisted gap", |
| 228 | + }, |
| 229 | + { |
| 230 | + messageId: "11", |
| 231 | + sender: "Lee", |
| 232 | + timestamp: 1_700_000_001_000, |
| 233 | + body: "late persisted ambient", |
| 234 | + }, |
| 235 | + ], |
| 236 | + ], |
| 237 | + ]), |
| 238 | + sessionRuntime: { |
| 239 | + readAmbientTranscriptWatermark: () => ({ |
| 240 | + messageId: "11", |
| 241 | + timestampMs: 1_700_000_001_000, |
| 242 | + updatedAt: 1_700_000_003_000, |
| 243 | + }), |
| 244 | + }, |
| 245 | + }); |
| 246 | + |
| 247 | + expect(ctx?.ctxPayload.InboundHistory).toEqual([ |
| 248 | + expect.objectContaining({ messageId: "12", body: "unpersisted gap" }), |
| 249 | + ]); |
| 250 | + }); |
| 251 | + |
231 | 252 | it("omits transcript-owned ambient rows from steady-state room-event prompt text", async () => { |
232 | 253 | const ctx = await buildTelegramMessageContextForTest({ |
233 | 254 | message: { |
@@ -276,11 +297,13 @@ describe("buildTelegramMessageContext prompt context", () => { |
276 | 297 | if (!ctx) { |
277 | 298 | throw new Error("Expected room-event context"); |
278 | 299 | } |
279 | | - const promptText = renderRoomEventPromptText(ctx.ctxPayload as RoomEventPromptContext); |
280 | | - expect(promptText).toContain("[OpenClaw room event]"); |
281 | | - expect(promptText).toContain("Current event:\n#12 Pat: current ambient"); |
282 | | - expect(promptText).not.toContain("persisted ambient"); |
283 | | - expect(promptText).not.toContain("Chat history since last reply"); |
| 300 | + expect(ctx.ctxPayload).toMatchObject({ |
| 301 | + BodyForAgent: "current ambient", |
| 302 | + InboundEventKind: "room_event", |
| 303 | + MessageSid: "12", |
| 304 | + SenderName: "Pat", |
| 305 | + }); |
284 | 306 | expect(ctx.ctxPayload.InboundHistory).toBeUndefined(); |
| 307 | + expect(ctx.ctxPayload.UntrustedStructuredContext).toBeUndefined(); |
285 | 308 | }); |
286 | 309 | }); |
0 commit comments