mirror of
https://github.com/openclaw/openclaw.git
synced 2026-04-03 03:03:24 -04:00
fix (agents): suppress NO_REPLY final text when message tool already sent text
This commit is contained in:
31
src/agents/pi-embedded-subscribe.handlers.messages.test.ts
Normal file
31
src/agents/pi-embedded-subscribe.handlers.messages.test.ts
Normal file
@@ -0,0 +1,31 @@
|
||||
import { describe, expect, it } from "vitest";
|
||||
import { resolveSilentReplyFallbackText } from "./pi-embedded-subscribe.handlers.messages.js";
|
||||
|
||||
describe("resolveSilentReplyFallbackText", () => {
|
||||
it("replaces NO_REPLY with latest messaging tool text when available", () => {
|
||||
expect(
|
||||
resolveSilentReplyFallbackText({
|
||||
text: "NO_REPLY",
|
||||
messagingToolSentTexts: ["first", "final delivered text"],
|
||||
}),
|
||||
).toBe("final delivered text");
|
||||
});
|
||||
|
||||
it("keeps original text when response is not NO_REPLY", () => {
|
||||
expect(
|
||||
resolveSilentReplyFallbackText({
|
||||
text: "normal assistant reply",
|
||||
messagingToolSentTexts: ["final delivered text"],
|
||||
}),
|
||||
).toBe("normal assistant reply");
|
||||
});
|
||||
|
||||
it("keeps NO_REPLY when there is no messaging tool text to mirror", () => {
|
||||
expect(
|
||||
resolveSilentReplyFallbackText({
|
||||
text: "NO_REPLY",
|
||||
messagingToolSentTexts: [],
|
||||
}),
|
||||
).toBe("NO_REPLY");
|
||||
});
|
||||
});
|
||||
@@ -1,6 +1,7 @@
|
||||
import type { AgentEvent, AgentMessage } from "@mariozechner/pi-agent-core";
|
||||
import type { EmbeddedPiSubscribeContext } from "./pi-embedded-subscribe.handlers.types.js";
|
||||
import { parseReplyDirectives } from "../auto-reply/reply/reply-directives.js";
|
||||
import { SILENT_REPLY_TOKEN } from "../auto-reply/tokens.js";
|
||||
import { emitAgentEvent } from "../infra/agent-events.js";
|
||||
import { createInlineCodeState } from "../markdown/code-spans.js";
|
||||
import {
|
||||
@@ -29,6 +30,21 @@ const stripTrailingDirective = (text: string): string => {
|
||||
return text.slice(0, openIndex);
|
||||
};
|
||||
|
||||
export function resolveSilentReplyFallbackText(params: {
|
||||
text: string;
|
||||
messagingToolSentTexts: string[];
|
||||
}): string {
|
||||
const trimmed = params.text.trim();
|
||||
if (trimmed !== SILENT_REPLY_TOKEN) {
|
||||
return params.text;
|
||||
}
|
||||
const fallback = params.messagingToolSentTexts.at(-1)?.trim();
|
||||
if (!fallback) {
|
||||
return params.text;
|
||||
}
|
||||
return fallback;
|
||||
}
|
||||
|
||||
export function handleMessageStart(
|
||||
ctx: EmbeddedPiSubscribeContext,
|
||||
evt: AgentEvent & { message: AgentMessage },
|
||||
@@ -214,7 +230,10 @@ export function handleMessageEnd(
|
||||
rawThinking: extractAssistantThinking(assistantMessage),
|
||||
});
|
||||
|
||||
const text = ctx.stripBlockTags(rawText, { thinking: false, final: false });
|
||||
const text = resolveSilentReplyFallbackText({
|
||||
text: ctx.stripBlockTags(rawText, { thinking: false, final: false }),
|
||||
messagingToolSentTexts: ctx.state.messagingToolSentTexts,
|
||||
});
|
||||
const rawThinking =
|
||||
ctx.state.includeReasoning || ctx.state.streamReasoning
|
||||
? extractAssistantThinking(assistantMessage) || extractThinkingFromTaggedText(rawText)
|
||||
|
||||
Reference in New Issue
Block a user