test: reduce subagent announce import overhead

This commit is contained in:
Peter Steinberger
2026-04-03 15:59:26 +01:00
parent 25a187568f
commit a6816cb59c
11 changed files with 231 additions and 143 deletions

View File

@@ -0,0 +1,50 @@
import { extractTextFromChatContent } from "../../shared/chat-content.js";
import { sanitizeUserFacingText } from "../pi-embedded-helpers.js";
import {
stripDowngradedToolCallText,
stripMinimaxToolCallXml,
stripModelSpecialTokens,
stripThinkingTagsFromText,
} from "../pi-embedded-utils.js";
export function stripToolMessages(messages: unknown[]): unknown[] {
return messages.filter((msg) => {
if (!msg || typeof msg !== "object") {
return true;
}
const role = (msg as { role?: unknown }).role;
return role !== "toolResult" && role !== "tool";
});
}
export function sanitizeTextContent(text: string): string {
if (!text) {
return text;
}
return stripThinkingTagsFromText(
stripDowngradedToolCallText(stripModelSpecialTokens(stripMinimaxToolCallXml(text))),
);
}
export function extractAssistantText(message: unknown): string | undefined {
if (!message || typeof message !== "object") {
return undefined;
}
if ((message as { role?: unknown }).role !== "assistant") {
return undefined;
}
const content = (message as { content?: unknown }).content;
if (!Array.isArray(content)) {
return undefined;
}
const joined =
extractTextFromChatContent(content, {
sanitizeText: sanitizeTextContent,
joinWith: "",
normalizeText: (text) => text.trim(),
}) ?? "";
const stopReason = (message as { stopReason?: unknown }).stopReason;
const errorContext = stopReason === "error";
return joined ? sanitizeUserFacingText(joined, { errorContext }) : undefined;
}

View File

@@ -5,9 +5,14 @@ import {
import { resolveSessionConversationRef } from "../../channels/plugins/session-conversation.js";
import { normalizeChannelId as normalizeChatChannelId } from "../../channels/registry.js";
import type { OpenClawConfig } from "../../config/config.js";
import { ANNOUNCE_SKIP_TOKEN, REPLY_SKIP_TOKEN } from "./sessions-send-tokens.js";
export {
ANNOUNCE_SKIP_TOKEN,
REPLY_SKIP_TOKEN,
isAnnounceSkip,
isReplySkip,
} from "./sessions-send-tokens.js";
const ANNOUNCE_SKIP_TOKEN = "ANNOUNCE_SKIP";
const REPLY_SKIP_TOKEN = "REPLY_SKIP";
const DEFAULT_PING_PONG_TURNS = 5;
const MAX_PING_PONG_TURNS = 5;
@@ -115,14 +120,6 @@ export function buildAgentToAgentAnnounceContext(params: {
return lines.join("\n");
}
export function isAnnounceSkip(text?: string) {
return (text ?? "").trim() === ANNOUNCE_SKIP_TOKEN;
}
export function isReplySkip(text?: string) {
return (text ?? "").trim() === REPLY_SKIP_TOKEN;
}
export function resolvePingPongTurns(cfg?: OpenClawConfig) {
const raw = cfg?.session?.agentToAgent?.maxPingPongTurns;
const fallback = DEFAULT_PING_PONG_TURNS;

View File

@@ -0,0 +1,10 @@
export const ANNOUNCE_SKIP_TOKEN = "ANNOUNCE_SKIP";
export const REPLY_SKIP_TOKEN = "REPLY_SKIP";
export function isAnnounceSkip(text?: string) {
return (text ?? "").trim() === ANNOUNCE_SKIP_TOKEN;
}
export function isReplySkip(text?: string) {
return (text ?? "").trim() === REPLY_SKIP_TOKEN;
}