mirror of
https://github.com/openclaw/openclaw.git
synced 2026-05-05 06:20:31 +00:00
fix: handle gateway slash command replies in TUI
This commit is contained in:
@@ -1,6 +1,6 @@
|
||||
import type { TUI } from "@mariozechner/pi-tui";
|
||||
import type { ChatLog } from "./components/chat-log.js";
|
||||
import { asString } from "./tui-formatters.js";
|
||||
import { asString, extractTextFromMessage, isCommandMessage } from "./tui-formatters.js";
|
||||
import { TuiStreamAssembler } from "./tui-stream-assembler.js";
|
||||
import type { AgentEvent, ChatEvent, TuiStateAccess } from "./tui-types.js";
|
||||
|
||||
@@ -49,6 +49,17 @@ export function createEventHandlers(context: EventHandlerContext) {
|
||||
setActivityStatus("streaming");
|
||||
}
|
||||
if (evt.state === "final") {
|
||||
if (isCommandMessage(evt.message)) {
|
||||
const text = extractTextFromMessage(evt.message);
|
||||
if (text) chatLog.addSystem(text);
|
||||
streamAssembler.drop(evt.runId);
|
||||
noteFinalizedRun(evt.runId);
|
||||
state.activeChatRunId = null;
|
||||
setActivityStatus("idle");
|
||||
void refreshSessionInfo?.();
|
||||
tui.requestRender();
|
||||
return;
|
||||
}
|
||||
const stopReason =
|
||||
evt.message && typeof evt.message === "object" && !Array.isArray(evt.message)
|
||||
? typeof (evt.message as Record<string, unknown>).stopReason === "string"
|
||||
|
||||
@@ -4,6 +4,7 @@ import {
|
||||
extractContentFromMessage,
|
||||
extractTextFromMessage,
|
||||
extractThinkingFromMessage,
|
||||
isCommandMessage,
|
||||
} from "./tui-formatters.js";
|
||||
|
||||
describe("extractTextFromMessage", () => {
|
||||
@@ -98,3 +99,11 @@ describe("extractContentFromMessage", () => {
|
||||
expect(text).toContain("HTTP 429");
|
||||
});
|
||||
});
|
||||
|
||||
describe("isCommandMessage", () => {
|
||||
it("detects command-marked messages", () => {
|
||||
expect(isCommandMessage({ command: true })).toBe(true);
|
||||
expect(isCommandMessage({ command: false })).toBe(false);
|
||||
expect(isCommandMessage({})).toBe(false);
|
||||
});
|
||||
});
|
||||
|
||||
@@ -140,6 +140,11 @@ export function extractTextFromMessage(
|
||||
return formatRawAssistantErrorForUi(errorMessage);
|
||||
}
|
||||
|
||||
export function isCommandMessage(message: unknown): boolean {
|
||||
if (!message || typeof message !== "object") return false;
|
||||
return (message as Record<string, unknown>).command === true;
|
||||
}
|
||||
|
||||
export function formatTokens(total?: number | null, context?: number | null) {
|
||||
if (total == null && context == null) return "tokens ?";
|
||||
const totalLabel = total == null ? "?" : formatTokenCount(total);
|
||||
|
||||
@@ -6,7 +6,7 @@ import {
|
||||
} from "../routing/session-key.js";
|
||||
import type { ChatLog } from "./components/chat-log.js";
|
||||
import type { GatewayAgentsList, GatewayChatClient } from "./gateway-chat.js";
|
||||
import { asString, extractTextFromMessage } from "./tui-formatters.js";
|
||||
import { asString, extractTextFromMessage, isCommandMessage } from "./tui-formatters.js";
|
||||
import type { TuiOptions, TuiStateAccess } from "./tui-types.js";
|
||||
|
||||
type SessionActionContext = {
|
||||
@@ -161,6 +161,11 @@ export function createSessionActions(context: SessionActionContext) {
|
||||
for (const entry of record.messages ?? []) {
|
||||
if (!entry || typeof entry !== "object") continue;
|
||||
const message = entry as Record<string, unknown>;
|
||||
if (isCommandMessage(message)) {
|
||||
const text = extractTextFromMessage(message);
|
||||
if (text) chatLog.addSystem(text);
|
||||
continue;
|
||||
}
|
||||
if (message.role === "user") {
|
||||
const text = extractTextFromMessage(message);
|
||||
if (text) chatLog.addUser(text);
|
||||
|
||||
Reference in New Issue
Block a user