mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-02 22:30:19 +00:00
test: reduce subagent announce import overhead
This commit is contained in:
50
src/agents/tools/session-message-text.ts
Normal file
50
src/agents/tools/session-message-text.ts
Normal 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;
|
||||
}
|
||||
@@ -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;
|
||||
|
||||
10
src/agents/tools/sessions-send-tokens.ts
Normal file
10
src/agents/tools/sessions-send-tokens.ts
Normal 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;
|
||||
}
|
||||
Reference in New Issue
Block a user