Skip to content

JiuwenSwarm is an intelligent AI Agent built on openJiuwen. It extends the powerful capabilities of large language models directly to your fingertips through various communication apps you use daily.

Python

fix:修复TUI输入长文断连

openJiuwen-bot422d8893fe0f
diff --git a/jiuwenswarm/channels/tui/frontend/src/app-state.ts b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
index cce3cbe82f..8b802dfc54 100644
--- a/jiuwenswarm/channels/tui/frontend/src/app-state.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/app-state.ts
@@ -371,6 +371,9 @@ export class CliPiAppState {
if (status === "connected") {
await this.fetchModelInfo();
}
+ if (status === "message_too_big") {
+ this.addItem(addError(this.sessionId, "消息过大,服务器拒绝了连接。请缩短输入内容后重新发送。"));
+ }
});
this.unlistenFrames = this.wsClient.onFrame((frame) => {
@@ -863,6 +866,14 @@ export class CliPiAppState {
): string | null => {
if (this.connectionStatus !== "connected") return null;
const mode = modeOverride ?? this.mode;
+ // Pre-check: reject messages whose serialized frame exceeds 7 MB (gateway
+ // server max_size is 8 MB; leave 1 MB margin for JSON overhead).
+ const estimatedSize = JSON.stringify({ type: "req", method: "chat.send", params: { content, query: content, mode, ...(attachments?.length ? { attachments } : {}) } }).length;
+ if (estimatedSize > 7 * 1024 * 1024) {
+ this.addItem(addError(this.sessionId, `消息过大(约 ${Math.round(estimatedSize / 1024 / 1024)} MB),请缩短输入内容。`));
+ this.emitChange();
+ return null;
+ }
if (this.streamingState !== StreamingState.Idle) {
this.sendEventOnly("chat.interrupt", { intent: "cancel" });
}
@@ -899,6 +910,13 @@ export class CliPiAppState {
if (this.connectionStatus !== "connected") return null;
const trimmed = content.trim();
if (!trimmed) return null;
+ // Same pre-check as sendMessage: reject oversized frames.
+ const estimatedSize = JSON.stringify({ type: "req", method: "chat.interrupt", params: { intent: "supplement", new_input: trimmed, ...(attachments?.length ? { attachments } : {}) } }).length;
+ if (estimatedSize > 7 * 1024 * 1024) {
+ this.addItem(addError(this.sessionId, `消息过大(约 ${Math.round(estimatedSize / 1024 / 1024)} MB),请缩短输入内容。`));
+ this.emitChange();
+ return null;
+ }
const requestId = this.sendEventOnly("chat.interrupt", {
intent: "supplement",
new_input: trimmed,
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
index 722cc1d470..1c7eb9d5ee 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/commands/types.ts
@@ -6,7 +6,7 @@ import type { ConfigItemSchema } from "./builtins/config.js";
import type { ClientMode } from "../modes.js";
import type { SessionUsageSummary } from "../../app-state.js";
-export type ConnectionStatus = "idle" | "connecting" | "connected" | "reconnecting" | "auth_failed";
+export type ConnectionStatus = "idle" | "connecting" | "connected" | "reconnecting" | "auth_failed" | "message_too_big";
export type StatusViewTab = "status" | "usage" | "config";
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/ws-client.ts b/jiuwenswarm/channels/tui/frontend/src/core/ws-client.ts
index 6ba9e4b6aa..6c51dc3b98 100644
--- a/jiuwenswarm/channels/tui/frontend/src/core/ws-client.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/core/ws-client.ts
@@ -3,7 +3,7 @@ import type { Frame, ReqFrame, ResFrame } from "./protocol.js";
import { isResFrame } from "./protocol.js";
export type FrameHandler = (frame: Frame) => void;
-export type ConnectionStatus = "idle" | "connecting" | "connected" | "reconnecting" | "auth_failed";
+export type ConnectionStatus = "idle" | "connecting" | "connected" | "reconnecting" | "auth_failed" | "message_too_big";
interface PendingRequest {
resolve: (frame: ResFrame) => void;
@@ -144,6 +144,12 @@ export class WsClient {
return;
}
+ if (code === 1009) {
+ this.setStatus("message_too_big");
+ this.rejectAllPending(new Error("消息过大,服务器拒绝了连接。请缩短输入内容后重试。"));
+ return;
+ }
+
if (code === 1013) {
this.setStatus("idle");
this.rejectAllPending(new Error("cli channel busy"));
diff --git a/jiuwenswarm/channels/tui/frontend/src/index.ts b/jiuwenswarm/channels/tui/frontend/src/index.ts
index 5ef7477882..55ba98cf8c 100644
--- a/jiuwenswarm/channels/tui/frontend/src/index.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/index.ts
@@ -30,7 +30,8 @@ Options:
process.exit(0);
}
-if (!process.stdin.isTTY || !process.stdout.isTTY) {
+// 允许通过环境变量跳过 TTY 检查(用于自动化测试)
+if (!process.env.JIUWENSWARM_TUI_HEADLESS && (!process.stdin.isTTY || !process.stdout.isTTY)) {
console.error("jiuwenswarm-tui requires an interactive TTY");
process.exit(1);
}
@@ -107,7 +108,7 @@ process.on("SIGTERM", () => {
let lastInterruptTime = 0;
process.on("SIGINT", () => {
const now = Date.now();
- if (now - lastInterruptTime < 1000) {
+ if (now - lastInterruptTime < 3000) {
void closeUi(0);
return;
}
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index 548ce93a5b..058ec28fd0 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -801,9 +801,7 @@ export class AppScreen implements Component, Focusable {
return;
}
- // Ctrl+C double-press for pending questions (aligns with Claude Code behavior):
- // First press: show "Press Ctrl+C again to exit" notice, don't cancel yet.
- // Second press within 1s: actually cancel the question.
+ // Ctrl+C during pending question: first press shows hint, second press within 1s cancels
if (pendingQuestion && matchesKey(data, "ctrl+c")) {
if (this.ctrlCPendingForQuestion) {
this.clearCtrlCPendingForQuestion();
@@ -818,7 +816,7 @@ export class AppScreen implements Component, Focusable {
this.ctrlCPendingForQuestionTimer = null;
this.transientNotice = null;
this.tui.requestRender();
- }, 1000);
+ }, 3000);
this.tui.requestRender();
return;
}
@@ -868,6 +866,18 @@ export class AppScreen implements Component, Focusable {
requestLocalInterrupt: () => {
this.state.requestLocalInterrupt();
},
+ showCtrlCExitHint: () => {
+ if (this.transientNoticeTimer) {
+ clearTimeout(this.transientNoticeTimer);
+ }
+ this.transientNotice = "Press Ctrl+C again to exit";
+ this.transientNoticeTimer = setTimeout(() => {
+ this.transientNotice = null;
+ this.transientNoticeTimer = null;
+ this.tui.requestRender();
+ }, 3000);
+ this.tui.requestRender();
+ },
});
if (handled) {
return;
@@ -3181,6 +3191,9 @@ export class AppScreen implements Component, Focusable {
),
);
}
+ if (this.ctrlCPendingForQuestion) {
+ lines.push(padToWidth(palette.status.warning("Press Ctrl+C again to exit"), width));
+ }
return lines;
}
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/keymap.ts b/jiuwenswarm/channels/tui/frontend/src/ui/keymap.ts
index b9e3aebd3a..bfa4af6116 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/keymap.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/keymap.ts
@@ -15,6 +15,8 @@ export interface AppScreenKeymapDelegate {
interruptTask(): void;
/** Set local interrupt flag only (for long-running local commands like log streaming) */
requestLocalInterrupt(): void;
+ /** Show a brief hint that pressing Ctrl+C again will exit */
+ showCtrlCExitHint(): void;
exitApp(): void;
toggleTodos(): void;
toggleTeamPanel(): void;
@@ -40,7 +42,7 @@ export const APP_SCREEN_KEY_BINDINGS: readonly KeyBinding[] = [
description: "中断任务;连按两次退出",
run: (delegate) => {
const now = Date.now();
- if (now - lastInterruptTime < 1000) {
+ if (now - lastInterruptTime < 3000) {
delegate.exitApp();
return;
}
@@ -58,6 +60,9 @@ export const APP_SCREEN_KEY_BINDINGS: readonly KeyBinding[] = [
delegate.clearInput();
}
+ // Show hint that pressing Ctrl+C again within 1s will exit
+ delegate.showCtrlCExitHint();
+
lastInterruptTime = now;
},
},
@@ -67,12 +72,13 @@ export const APP_SCREEN_KEY_BINDINGS: readonly KeyBinding[] = [
description: "中断任务;连按两次退出",
run: (delegate) => {
const now = Date.now();
- if (now - lastInterruptTime < 1000) {
+ if (now - lastInterruptTime < 3000) {
delegate.exitApp();
return;
}
lastInterruptTime = now;
delegate.interruptTask();
+ delegate.showCtrlCExitHint();
},
},
{
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
index e11a480481..437feaed4f 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/screen-layout.ts
@@ -87,6 +87,8 @@ function connectionStatusLabel(status: AppSnapshot["connectionStatus"]): string
return "backend unavailable · retrying";
case "auth_failed":
return "auth failed";
+ case "message_too_big":
+ return "消息过大 · 连接被断开";
case "idle":
return "backend unavailable";
case "connected":
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/welcome.ts b/jiuwenswarm/channels/tui/frontend/src/ui/welcome.ts
index 629391355c..1ccfa24074 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/welcome.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/welcome.ts
@@ -53,6 +53,8 @@ function connectionHint(status: ConnectionStatus): string | null {
return "Backend unavailable · start jiuwenswarm-gateway or check --url";
case "auth_failed":
return "Authentication failed · check --token";
+ case "message_too_big":
+ return "消息过大,连接被断开 · 请缩短输入后重试";
case "connected":
default:
return null;
diff --git a/jiuwenswarm/gateway/app_gateway.py b/jiuwenswarm/gateway/app_gateway.py
index 671f78733b..a228761728 100644
--- a/jiuwenswarm/gateway/app_gateway.py
+++ b/jiuwenswarm/gateway/app_gateway.py
@@ -376,12 +376,15 @@ class GatewayServer:
except Exception: # pragma: no cover
from websockets import serve as ws_serve
+ ws_max_size = 8 * 2**20 # 8 MB — matches AgentServer link
+
self._server = await ws_serve(
self._connection_handler,
self.config.host,
self.config.port,
ping_interval=20,
ping_timeout=600,
+ max_size=ws_max_size,
)
self._running = True
paths = ", ".join(self.config.routes.keys())
diff --git a/jiuwenswarm/gateway/channel_manager/web/web_connect.py b/jiuwenswarm/gateway/channel_manager/web/web_connect.py
index a4855ddf1b..838849a770 100644
--- a/jiuwenswarm/gateway/channel_manager/web/web_connect.py
+++ b/jiuwenswarm/gateway/channel_manager/web/web_connect.py
@@ -237,6 +237,8 @@ class WebChannel(BaseChannel):
ws_serve = websockets.serve
+ ws_max_size = 8 * 2**20 # 8 MB — matches AgentServer link
+
self._server = await ws_serve(
self._connection_handler,
self.config.host,
@@ -244,6 +246,7 @@ class WebChannel(BaseChannel):
process_request=self._process_request,
ping_interval=20,
ping_timeout=20,
+ max_size=ws_max_size,
)
self._running = True
logger.info(
diff --git a/jiuwenswarm/server/agent_ws_server.py b/jiuwenswarm/server/agent_ws_server.py
index f1ca229b03..ab73988069 100644
--- a/jiuwenswarm/server/agent_ws_server.py
+++ b/jiuwenswarm/server/agent_ws_server.py
@@ -388,6 +388,8 @@ class AgentWebSocketServer:
await ensure_persistent_checkpointer()
+ ws_max_size = 8 * 2**20 # 8 MB — matches Gateway → AgentServer link
+
try:
from websockets.legacy.server import serve as legacy_serve
self._server = await legacy_serve(
@@ -397,6 +399,7 @@ class AgentWebSocketServer:
process_request=self._process_request,
ping_interval=self._ping_interval,
ping_timeout=self._ping_timeout,
+ max_size=ws_max_size,
)
except ImportError:
import websockets
@@ -407,6 +410,7 @@ class AgentWebSocketServer:
process_request=self._process_request,
ping_interval=self._ping_interval,
ping_timeout=self._ping_timeout,
+ max_size=ws_max_size,
)
logger.info(
"[AgentWebSocketServer] 已启动: ws://%s:%s", self._host, self._port