* fix(gateway): avoid premature agent.wait completion on transient errors * fix(agent): preemptively guard tool results against context overflow * fix: harden tool-result context guard and add message_id metadata * fix: use importOriginal in session-key mock to include DEFAULT_ACCOUNT_ID The run.skill-filter test was mocking ../../routing/session-key.js with only buildAgentMainSessionKey and normalizeAgentId, but the module also exports DEFAULT_ACCOUNT_ID which is required transitively by src/web/auth-store.ts. Switch to importOriginal pattern so all real exports are preserved alongside the mocked functions. * pi-runner: guard accumulated tool-result overflow in transformContext * PI runner: compact overflowing tool-result context * Subagent: harden tool-result context recovery * Enhance tool-result context handling by adding support for legacy tool outputs and improving character estimation for message truncation. This includes a new function to create legacy tool results and updates to existing functions to better manage context overflow scenarios. * Enhance iMessage handling by adding reply tag support in send functions and tests. This includes modifications to prepend or rewrite reply tags based on provided replyToId, ensuring proper message formatting for replies. * Enhance message delivery across multiple channels by implementing sticky reply context for chunked messages. This includes preserving reply references in Discord, Telegram, and iMessage, ensuring that follow-up messages maintain their intended reply targets. Additionally, improve handling of reply tags in system prompts and tests to support consistent reply behavior. * Enhance read tool functionality by implementing auto-paging across chunks when no explicit limit is provided, scaling output budget based on model context window. Additionally, add tests for adaptive reading behavior and capped continuation guidance for large outputs. Update related functions to support these features. * Refine tool-result context management by stripping oversized read-tool details payloads during compaction, ensuring repeated read calls do not bypass context limits. Introduce new utility functions for handling truncation content and enhance character estimation for tool results. Add tests to validate the removal of excessive details in context overflow scenarios. * Refine message delivery logic in Matrix and Telegram by introducing a flag to track if a text chunk was sent. This ensures that replies are only marked as delivered when a text chunk has been successfully sent, improving the accuracy of reply handling in both channels. * fix: tighten reply threading coverage and prep fixes (#19508) (thanks @tyler6204)
169 lines
5.5 KiB
TypeScript
169 lines
5.5 KiB
TypeScript
import { describe, expect, it } from "vitest";
|
|
import type { TemplateContext } from "../templating.js";
|
|
import { buildInboundMetaSystemPrompt, buildInboundUserContextPrefix } from "./inbound-meta.js";
|
|
|
|
function parseInboundMetaPayload(text: string): Record<string, unknown> {
|
|
const match = text.match(/```json\n([\s\S]*?)\n```/);
|
|
if (!match?.[1]) {
|
|
throw new Error("missing inbound meta json block");
|
|
}
|
|
return JSON.parse(match[1]) as Record<string, unknown>;
|
|
}
|
|
|
|
function parseConversationInfoPayload(text: string): Record<string, unknown> {
|
|
const match = text.match(/Conversation info \(untrusted metadata\):\n```json\n([\s\S]*?)\n```/);
|
|
if (!match?.[1]) {
|
|
throw new Error("missing conversation info json block");
|
|
}
|
|
return JSON.parse(match[1]) as Record<string, unknown>;
|
|
}
|
|
|
|
describe("buildInboundMetaSystemPrompt", () => {
|
|
it("includes trusted message and routing ids for tool actions", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "123",
|
|
MessageSidFull: "123",
|
|
ReplyToId: "99",
|
|
OriginatingTo: "telegram:5494292670",
|
|
OriginatingChannel: "telegram",
|
|
Provider: "telegram",
|
|
Surface: "telegram",
|
|
ChatType: "direct",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["schema"]).toBe("openclaw.inbound_meta.v1");
|
|
expect(payload["message_id"]).toBe("123");
|
|
expect(payload["message_id_full"]).toBeUndefined();
|
|
expect(payload["reply_to_id"]).toBe("99");
|
|
expect(payload["chat_id"]).toBe("telegram:5494292670");
|
|
expect(payload["channel"]).toBe("telegram");
|
|
});
|
|
|
|
it("includes sender_id when provided", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "456",
|
|
SenderId: "289522496",
|
|
OriginatingTo: "telegram:-1001249586642",
|
|
OriginatingChannel: "telegram",
|
|
Provider: "telegram",
|
|
Surface: "telegram",
|
|
ChatType: "group",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["sender_id"]).toBe("289522496");
|
|
});
|
|
|
|
it("trims sender_id before storing", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "457",
|
|
SenderId: " 289522496 ",
|
|
OriginatingTo: "telegram:-1001249586642",
|
|
OriginatingChannel: "telegram",
|
|
Provider: "telegram",
|
|
Surface: "telegram",
|
|
ChatType: "group",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["sender_id"]).toBe("289522496");
|
|
});
|
|
|
|
it("omits sender_id when blank", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "458",
|
|
SenderId: " ",
|
|
OriginatingTo: "telegram:-1001249586642",
|
|
OriginatingChannel: "telegram",
|
|
Provider: "telegram",
|
|
Surface: "telegram",
|
|
ChatType: "group",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["sender_id"]).toBeUndefined();
|
|
});
|
|
|
|
it("omits sender_id when not provided", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "789",
|
|
OriginatingTo: "telegram:5494292670",
|
|
OriginatingChannel: "telegram",
|
|
Provider: "telegram",
|
|
Surface: "telegram",
|
|
ChatType: "direct",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["sender_id"]).toBeUndefined();
|
|
});
|
|
|
|
it("keeps message_id_full only when it differs from message_id", () => {
|
|
const prompt = buildInboundMetaSystemPrompt({
|
|
MessageSid: "short-id",
|
|
MessageSidFull: "full-provider-message-id",
|
|
OriginatingTo: "channel:C1",
|
|
OriginatingChannel: "slack",
|
|
Provider: "slack",
|
|
Surface: "slack",
|
|
ChatType: "group",
|
|
} as TemplateContext);
|
|
|
|
const payload = parseInboundMetaPayload(prompt);
|
|
expect(payload["message_id"]).toBe("short-id");
|
|
expect(payload["message_id_full"]).toBe("full-provider-message-id");
|
|
});
|
|
});
|
|
|
|
describe("buildInboundUserContextPrefix", () => {
|
|
it("omits conversation label block for direct chats", () => {
|
|
const text = buildInboundUserContextPrefix({
|
|
ChatType: "direct",
|
|
ConversationLabel: "openclaw-tui",
|
|
} as TemplateContext);
|
|
|
|
expect(text).toBe("");
|
|
});
|
|
|
|
it("keeps conversation label for group chats", () => {
|
|
const text = buildInboundUserContextPrefix({
|
|
ChatType: "group",
|
|
ConversationLabel: "ops-room",
|
|
} as TemplateContext);
|
|
|
|
expect(text).toContain("Conversation info (untrusted metadata):");
|
|
expect(text).toContain('"conversation_label": "ops-room"');
|
|
});
|
|
|
|
it("includes sender identifier in conversation info", () => {
|
|
const text = buildInboundUserContextPrefix({
|
|
ChatType: "direct",
|
|
SenderE164: " +15551234567 ",
|
|
} as TemplateContext);
|
|
|
|
const conversationInfo = parseConversationInfoPayload(text);
|
|
expect(conversationInfo["sender"]).toBe("+15551234567");
|
|
});
|
|
|
|
it("includes message_id in conversation info", () => {
|
|
const text = buildInboundUserContextPrefix({
|
|
ChatType: "direct",
|
|
MessageSid: " msg-123 ",
|
|
} as TemplateContext);
|
|
|
|
const conversationInfo = parseConversationInfoPayload(text);
|
|
expect(conversationInfo["message_id"]).toBe("msg-123");
|
|
});
|
|
|
|
it("falls back to SenderId when sender phone is missing", () => {
|
|
const text = buildInboundUserContextPrefix({
|
|
ChatType: "direct",
|
|
SenderId: " user@example.com ",
|
|
} as TemplateContext);
|
|
|
|
const conversationInfo = parseConversationInfoPayload(text);
|
|
expect(conversationInfo["sender"]).toBe("user@example.com");
|
|
});
|
|
});
|