mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-04 07:40:23 +00:00
refactor(reply): extract subagent text helper
This commit is contained in:
22
src/auto-reply/reply/commands-subagents-shared.test.ts
Normal file
22
src/auto-reply/reply/commands-subagents-shared.test.ts
Normal file
@@ -0,0 +1,22 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { extractMessageText } from "./commands-subagents-text.js";
|
||||
|
||||
describe("extractMessageText", () => {
|
||||
it("preserves user markers and sanitizes assistant markers", () => {
|
||||
const cases = [
|
||||
{
|
||||
message: { role: "user", content: "Here [Tool Call: foo (ID: 1)] ok" },
|
||||
expectedText: "Here [Tool Call: foo (ID: 1)] ok",
|
||||
},
|
||||
{
|
||||
message: { role: "assistant", content: "Here [Tool Call: foo (ID: 1)] ok" },
|
||||
expectedText: "Here ok",
|
||||
},
|
||||
] as const;
|
||||
|
||||
for (const testCase of cases) {
|
||||
const result = extractMessageText(testCase.message);
|
||||
expect(result?.text).toBe(testCase.expectedText);
|
||||
}
|
||||
});
|
||||
});
|
||||
16
src/auto-reply/reply/commands-subagents-text.ts
Normal file
16
src/auto-reply/reply/commands-subagents-text.ts
Normal file
@@ -0,0 +1,16 @@
|
||||
import { sanitizeTextContent } from "../../agents/tools/chat-history-text.js";
|
||||
import { extractTextFromChatContent } from "../../shared/chat-content.js";
|
||||
|
||||
export type ChatMessage = {
|
||||
role?: unknown;
|
||||
content?: unknown;
|
||||
};
|
||||
|
||||
export function extractMessageText(message: ChatMessage): { role: string; text: string } | null {
|
||||
const role = typeof message.role === "string" ? message.role : "";
|
||||
const shouldSanitize = role === "assistant";
|
||||
const text = extractTextFromChatContent(message.content, {
|
||||
sanitizeText: shouldSanitize ? sanitizeTextContent : undefined,
|
||||
});
|
||||
return text ? { role, text } : null;
|
||||
}
|
||||
@@ -12,7 +12,6 @@ import { handleSubagentsSpawnAction } from "./commands-subagents/action-spawn.js
|
||||
import { handleSubagentsUnfocusAction } from "./commands-subagents/action-unfocus.js";
|
||||
import {
|
||||
type SubagentsCommandContext,
|
||||
extractMessageText,
|
||||
resolveHandledPrefix,
|
||||
resolveRequesterSessionKey,
|
||||
resolveSubagentsAction,
|
||||
@@ -20,7 +19,7 @@ import {
|
||||
} from "./commands-subagents/shared.js";
|
||||
import type { CommandHandler } from "./commands-types.js";
|
||||
|
||||
export { extractMessageText };
|
||||
export { extractMessageText } from "./commands-subagents-text.js";
|
||||
|
||||
export const handleSubagentsCommand: CommandHandler = async (params, allowTextCommands) => {
|
||||
if (!allowTextCommands) {
|
||||
|
||||
@@ -9,7 +9,6 @@ import {
|
||||
extractAssistantText,
|
||||
resolveInternalSessionKey,
|
||||
resolveMainSessionAlias,
|
||||
sanitizeTextContent,
|
||||
stripToolMessages,
|
||||
} from "../../../agents/tools/sessions-helpers.js";
|
||||
import type {
|
||||
@@ -22,13 +21,13 @@ import { formatTimeAgo } from "../../../infra/format-time/format-relative.ts";
|
||||
import { parseAgentSessionKey } from "../../../routing/session-key.js";
|
||||
import { isSubagentSessionKey } from "../../../routing/session-key.js";
|
||||
import { looksLikeSessionId } from "../../../sessions/session-id.js";
|
||||
import { extractTextFromChatContent } from "../../../shared/chat-content.js";
|
||||
import {
|
||||
formatDurationCompact,
|
||||
formatTokenUsageDisplay,
|
||||
truncateLine,
|
||||
} from "../../../shared/subagents-format.js";
|
||||
import { resolveCommandSurfaceChannel, resolveChannelAccountId } from "../channel-context.js";
|
||||
import { extractMessageText, type ChatMessage } from "../commands-subagents-text.js";
|
||||
import type { CommandHandler, CommandHandlerResult } from "../commands-types.js";
|
||||
import {
|
||||
formatRunLabel,
|
||||
@@ -367,20 +366,6 @@ export function buildSubagentsHelp() {
|
||||
].join("\n");
|
||||
}
|
||||
|
||||
export type ChatMessage = {
|
||||
role?: unknown;
|
||||
content?: unknown;
|
||||
};
|
||||
|
||||
export function extractMessageText(message: ChatMessage): { role: string; text: string } | null {
|
||||
const role = typeof message.role === "string" ? message.role : "";
|
||||
const shouldSanitize = role === "assistant";
|
||||
const text = extractTextFromChatContent(message.content, {
|
||||
sanitizeText: shouldSanitize ? sanitizeTextContent : undefined,
|
||||
});
|
||||
return text ? { role, text } : null;
|
||||
}
|
||||
|
||||
export function formatLogLines(messages: ChatMessage[]) {
|
||||
const lines: string[] = [];
|
||||
for (const msg of messages) {
|
||||
|
||||
Reference in New Issue
Block a user