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(pasted-text): implement functions for handling pasted text and integrate into AppScreen

zhengkailan6e220c6dfe69
diff --git a/jiuwenswarm/channels/tui/frontend/src/core/pasted-text.ts b/jiuwenswarm/channels/tui/frontend/src/core/pasted-text.ts
new file mode 100644
index 0000000000..68c90eb8ce
--- /dev/null
+++ b/jiuwenswarm/channels/tui/frontend/src/core/pasted-text.ts
@@ -0,0 +1,37 @@
+const PASTED_TEXT_MARKER_RE = /\[Pasted text #(\d+) \+\d+ lines\]/g;
+
+export const PASTED_TEXT_LINE_THRESHOLD = 4;
+export const PASTED_TEXT_CHAR_THRESHOLD = 1000;
+
+export function stripBracketedPasteMarkers(text: string): string {
+ return text.split("\x1b[200~").join("").split("\x1b[201~").join("");
+}
+
+export function normalizePastedText(text: string): string {
+ return text.replace(/\r\n/g, "\n").replace(/\r/g, "\n").replace(/\t/g, " ");
+}
+
+export function countPastedTextLines(text: string): number {
+ return text.split("\n").length;
+}
+
+export function shouldCollapsePastedText(text: string): boolean {
+ return (
+ countPastedTextLines(text) > PASTED_TEXT_LINE_THRESHOLD ||
+ text.length > PASTED_TEXT_CHAR_THRESHOLD
+ );
+}
+
+export function formatPastedTextMarker(id: number, text: string): string {
+ return `[Pasted text #${id} +${countPastedTextLines(text)} lines]`;
+}
+
+export function expandPastedTextMarkers(
+ text: string,
+ pastedTextById: ReadonlyMap<number, string>,
+): string {
+ return text.replace(PASTED_TEXT_MARKER_RE, (marker, idText: string) => {
+ const pastedText = pastedTextById.get(Number.parseInt(idText, 10));
+ return pastedText ?? marker;
+ });
+}
diff --git a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
index a31525c6d3..1f4af8d346 100644
--- a/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
+++ b/jiuwenswarm/channels/tui/frontend/src/ui/app-screen.ts
@@ -72,6 +72,13 @@ import {
setCurrentProjectDir,
} from "../core/tui-trusted-dirs-store.js";
import { consumeParseError } from "../core/tui-config-store.js";
+import {
+ expandPastedTextMarkers,
+ formatPastedTextMarker,
+ normalizePastedText,
+ shouldCollapsePastedText,
+ stripBracketedPasteMarkers,
+} from "../core/pasted-text.js";
import { handleAppScreenKeyInput } from "./keymap.js";
import { buildAppScreenLines } from "./screen-layout.js";
import { buildTranscriptLines } from "./transcript-renderer.js";
@@ -1138,6 +1145,10 @@ export class AppScreen implements Component, Focusable {
private lastTranscriptLineWidth = 0;
/** Image attachments keyed by composer `@path` tokens (e.g. cached base64 for terminal preview). */
private composerAttachments: FileAttachment[] = [];
+ private pastedTextById = new Map<number, string>();
+ private pastedTextIdByContent = new Map<string, number>();
+ private nextPastedTextId = 1;
+ private pastedTextClearTimer: ReturnType<typeof setTimeout> | null = null;
/** FileViewer state for viewing large content (e.g., formatted logs) */
private fileViewerState: FileViewerState | null = null;
/** DiffViewer state for interactive diff browsing */
@@ -1163,6 +1174,11 @@ export class AppScreen implements Component, Focusable {
this.editor.setAutocompleteProvider(this.composerAutocompleteProvider);
};
this.editor.onChange = () => {
+ if (!this.editor.getText()) {
+ this.schedulePastedTextStateClear();
+ } else {
+ this.cancelPastedTextStateClear();
+ }
this.tui.requestRender();
};
this.editor.onSubmit = async (value) => {
@@ -1181,6 +1197,11 @@ export class AppScreen implements Component, Focusable {
// check input emptiness and populate the input field after auto-restore.
this.state.setInputRef((text: string) => {
this.editor.setText(text);
+ if (!text) {
+ this.schedulePastedTextStateClear();
+ } else {
+ this.cancelPastedTextStateClear();
+ }
});
this.state.getInputValueRef(() => this.editor.getText());
// Initialize project scope from the user's actual cwd
@@ -2164,7 +2185,13 @@ export class AppScreen implements Component, Focusable {
// Only intercept when the paste is *pure* file paths (drag-and-drop).
// If there's command text interleaved, treat it as a normal paste.
if (!snapshot.pendingQuestion && data.length > 4) {
- const pastedContent = data.replace(/\x1b\[200~/, "").replace(/\x1b\[201~/, "");
+ const hasPasteStart = data.includes("\x1b[200~");
+ const hasPasteEnd = data.includes("\x1b[201~");
+ if (hasPasteStart !== hasPasteEnd) {
+ this.editor.handleInput(data);
+ return;
+ }
+ const pastedContent = stripBracketedPasteMarkers(data);
const filePaths = extractFilePathsFromPaste(pastedContent);
if (filePaths.length > 0 && isPurePathPaste(pastedContent)) {
// 若解析出路径但无一通过附件校验(扩展名不在白名单等),须把原文交给编辑器,避免粘贴被吞掉
@@ -2172,6 +2199,9 @@ export class AppScreen implements Component, Focusable {
return;
}
}
+ if (this.handlePastedTextCollapse(pastedContent)) {
+ return;
+ }
}
this.editor.handleInput(data);
@@ -2286,7 +2316,8 @@ export class AppScreen implements Component, Focusable {
}
private async handleSubmit(raw: string): Promise<void> {
- const text = raw.trim();
+ const editorText = raw.trim();
+ const text = this.expandPastedText(editorText).trim();
if (!text) return;
// 更新用户活动时间戳(用于 auto-recap 空闲检测)
@@ -2307,7 +2338,7 @@ export class AppScreen implements Component, Focusable {
return;
}
- const { content, attachments } = this.buildOutgoingMessage(text);
+ const { content, attachments } = this.buildOutgoingMessage(editorText);
const snapshot = this.state.getSnapshot();
if (!content && !(snapshot.pendingQuestion && this.otherInputMode)) return;
@@ -4480,9 +4511,10 @@ export class AppScreen implements Component, Focusable {
}
private buildOutgoingMessage(text: string): { content: string; attachments: FileAttachment[] } {
+ const expandedText = this.expandPastedText(text);
return {
- content: text.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim(),
- attachments: this.collectComposerAttachments(text),
+ content: this.expandPastedText(text.replace(/[ \t]{2,}/g, " ").replace(/[ \t]+\n/g, "\n").trim()),
+ attachments: this.collectComposerAttachments(expandedText),
};
}
@@ -5292,6 +5324,35 @@ export class AppScreen implements Component, Focusable {
this.tui.requestRender();
}
+ private clearPastedTextState(): void {
+ this.cancelPastedTextStateClear();
+ this.pastedTextById.clear();
+ this.pastedTextIdByContent.clear();
+ this.nextPastedTextId = 1;
+ }
+
+ private cancelPastedTextStateClear(): void {
+ if (!this.pastedTextClearTimer) {
+ return;
+ }
+ clearTimeout(this.pastedTextClearTimer);
+ this.pastedTextClearTimer = null;
+ }
+
+ private schedulePastedTextStateClear(): void {
+ if (this.pastedTextClearTimer) {
+ clearTimeout(this.pastedTextClearTimer);
+ }
+ // Editor clears text and emits onChange("") before onSubmit(value), so keep paste data for that submit.
+ this.pastedTextClearTimer = setTimeout(() => {
+ this.clearPastedTextState();
+ }, 0);
+ }
+
+ private expandPastedText(text: string): string {
+ return expandPastedTextMarkers(text, this.pastedTextById);
+ }
+
private syncComposerAttachmentsFromEditor(): void {
if (this.syncingComposerInput) {
return;
@@ -5374,6 +5435,25 @@ export class AppScreen implements Component, Focusable {
return this.isAcceptedAttachment(path) && isImageAttachment(path);
}
+ private handlePastedTextCollapse(text: string): boolean {
+ const normalizedText = normalizePastedText(text);
+ if (!shouldCollapsePastedText(normalizedText)) {
+ return false;
+ }
+
+ let pasteId = this.pastedTextIdByContent.get(normalizedText);
+ if (!pasteId) {
+ pasteId = this.nextPastedTextId;
+ this.nextPastedTextId += 1;
+ this.pastedTextIdByContent.set(normalizedText, pasteId);
+ this.pastedTextById.set(pasteId, normalizedText);
+ }
+
+ this.editor.insertTextAtCursor(formatPastedTextMarker(pasteId, normalizedText));
+ this.tui.requestRender();
+ return true;
+ }
+
/** Handle pasted/dragged content - detects file paths and converts to @path references. */
private handleDroppedFiles(filePaths: string[]): boolean {
const insertText = filePaths