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 /resume 支持标题子串查找
jingjinduob98957c190dd
diff --git a/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/resume.ts b/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/resume.tsindex 0a83bef4aa..353910af15 100644--- a/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/resume.ts+++ b/jiuwenclaw/channels/tui/frontend/src/core/commands/builtins/resume.ts@@ -36,15 +36,17 @@ export function createResumeCommand(): SlashCommand {action: async (ctx, args) => {const value = args.trim();try {+ // 获取 session 列表用于匹配+ const listPayload = await ctx.request<SessionListPayload>("session.list", {});+ const allSessions = listPayload.sessions ?? [];+if (value === "" || value === "list") {- const payload = await ctx.request<SessionListPayload>("session.list", {});- const sessions = payload.sessions ?? [];- const total = payload.total ?? sessions.length;- if (sessions.length === 0) {+ const total = listPayload.total ?? allSessions.length;+ if (allSessions.length === 0) {ctx.addItem(addInfo(ctx.sessionId, "No sessions found", "r"));return;}- const items = sessions.map((s, i) => {+ const items = allSessions.map((s, i) => {const lastActive = s.last_message_at? new Date(s.last_message_at * 1000).toLocaleString(): "-";@@ -64,19 +66,42 @@ export function createResumeCommand(): SlashCommand {return;}- const payload = await ctx.request<ResumeResumePayload>(- "command.resume",- value ? { query: value } : {},+ // 1. 先检查是否完全匹配 session_id+ const sessionIdMatch = allSessions.find(+ (s) => s.session_id === value || s.session_id.startsWith(value) && value.length >= 8,);+ if (sessionIdMatch) {+ const nextSessionId = sessionIdMatch.session_id;+ ctx.updateSession(nextSessionId);+ ctx.clearEntries();+ ctx.addItem(addInfo(nextSessionId, `Resumed session ${nextSessionId}`, "r"));+ void ctx.restoreHistory(nextSessionId);+ void (async () => {+ try {+ const meta = await ctx.request<{ session_id: string; title: string }>(+ "session.rename",+ { session_id: nextSessionId },+ );+ ctx.setSessionTitle(meta.title || "");+ } catch {+ ctx.setSessionTitle("");+ }+ })();+ return;+ }++ // 2. 检查是否完全匹配 title(显示内容,title 或 fallback 的 session_id)+ const titleMatches = allSessions.filter((s) => {+ const displayLabel = s.title?.trim() || s.session_id;+ return displayLabel === value;+ });- const nextSessionId = payload.session_id?.trim();- if (payload.resumed && nextSessionId) {+ if (titleMatches.length === 1) {+ const nextSessionId = titleMatches[0]!.session_id;ctx.updateSession(nextSessionId);ctx.clearEntries();ctx.addItem(addInfo(nextSessionId, `Resumed session ${nextSessionId}`, "r"));void ctx.restoreHistory(nextSessionId);- // 异步拉取被恢复会话的标题;在拿到结果前保留上一会话的 title 显示,- // 避免 "清空 -> 回填" 造成状态栏闪烁。成功后覆盖,失败不影响核心功能。void (async () => {try {const meta = await ctx.request<{ session_id: string; title: string }>(@@ -90,25 +115,22 @@ export function createResumeCommand(): SlashCommand {})();return;}++ // 3. 多个 title 匹配+ if (titleMatches.length > 1) {+ ctx.addItem(+ addInfo(+ ctx.sessionId,+ `Found ${titleMatches.length} sessions matching "${value}". Please use /resume to pick a specific session.`,+ "r",+ ),+ );+ return;+ }++ // 4. 没有匹配ctx.addItem(- addInfo(- ctx.sessionId,- nextSessionId- ? `Resume candidate: ${nextSessionId}`- : value- ? `No resume match for ${value}`- : "No resumable session returned",- "r",- {- view: "kv",- title: "Resume",- items: [- ...(nextSessionId ? [{ label: "session", value: nextSessionId }] : []),- ...(payload.query ? [{ label: "query", value: payload.query }] : []),- ...(payload.preview ? [{ label: "preview", value: payload.preview }] : []),- ],- },- ),+ addInfo(ctx.sessionId, `Session "${value}" was not found. Use /resume to see available sessions.`, "r"),);} catch (error) {const message = error instanceof Error ? error.message : String(error);diff --git a/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts b/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.tsindex 7d656ba997..0fb17e38f0 100644--- a/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts+++ b/jiuwenclaw/channels/tui/frontend/src/ui/app-screen.ts@@ -10,6 +10,7 @@ import {type SlashCommand as TuiSlashCommand,TUI,matchesKey,+ decodeKittyPrintable,truncateToWidth,} from "@mariozechner/pi-tui";import { spawnSync } from "node:child_process";@@ -91,6 +92,7 @@ type ResumeSessionListState = {list: SelectList;sessions: SessionMeta[];total: number;+ searchQuery: string;};type ModelListState = {@@ -445,12 +447,27 @@ function formatSessionTime(timestamp: number | undefined): string {return new Date(timestamp * 1000).toLocaleString();}+function getDisplayLabel(s: SessionMeta): string {+ return s.title?.trim() || s.session_id;+}++function sessionToSelectItem(s: SessionMeta): SelectItem {+ return {+ value: s.session_id,+ label: getDisplayLabel(s),+ description: `${s.session_id} · msgs ${s.message_count ?? 0} · ${formatSessionTime(s.last_message_at)}`,+ };+}+function buildResumeSessionItems(sessions: SessionMeta[]): SelectItem[] {- return sessions.map((session) => ({- value: session.session_id,- label: session.title?.trim() || session.session_id,- description: `${session.session_id} · msgs ${session.message_count ?? 0} · ${formatSessionTime(session.last_message_at)}`,- }));+ return sessions.map(sessionToSelectItem);+}++function filterResumeSessions(sessions: SessionMeta[], query: string): SelectItem[] {+ const normalizedQuery = query.toLowerCase();+ return sessions+ .filter((s) => getDisplayLabel(s).toLowerCase().includes(normalizedQuery))+ .map(sessionToSelectItem);}export class AppScreen implements Component, Focusable {@@ -831,7 +848,23 @@ export class AppScreen implements Component, Focusable {}if (!snapshot.pendingQuestion && this.resumeSessionList !== null) {- this.resumeSessionList.list.handleInput(data);+ const printableChar = this.getPrintableChar(data);+ if (printableChar !== undefined) {+ const newQuery = this.resumeSessionList.searchQuery + printableChar;+ this.updateResumeSearchQuery(newQuery);+ } else if (matchesKey(data, "backspace")) {+ const newQuery = this.resumeSessionList.searchQuery.slice(0, -1);+ this.updateResumeSearchQuery(newQuery);+ } else if (matchesKey(data, "escape")) {+ if (this.resumeSessionList.searchQuery) {+ this.updateResumeSearchQuery("");+ } else {+ this.resumeSessionList = null;+ this.tui.requestRender();+ }+ } else {+ this.resumeSessionList.list.handleInput(data);+ }this.tui.requestRender();return;}@@ -1418,7 +1451,7 @@ export class AppScreen implements Component, Focusable {this.resumeSessionList = null;this.tui.requestRender();};- this.resumeSessionList = { list, sessions, total };+ this.resumeSessionList = { list, sessions, total, searchQuery: "" };this.tui.requestRender();} catch (error) {const message = error instanceof Error ? error.message : String(error);@@ -1439,6 +1472,52 @@ export class AppScreen implements Component, Focusable {this.tui.requestRender();}+ private getPrintableChar(data: string): string | undefined {+ // Kitty protocol printable character+ const kittyChar = decodeKittyPrintable(data);+ if (kittyChar) return kittyChar;++ // Check for printable Unicode character (not control sequences)+ if (data.length === 1) {+ const code = data.charCodeAt(0);+ // Control characters (0-31, 127) and DEL (127) are not printable+ // Extended ASCII (128-255) and Unicode (>255) printable chars are accepted+ if (code >= 32 && code !== 127) return data;+ }++ // UTF-8 multi-byte characters (Chinese, etc.)+ // Check if data looks like a valid UTF-8 printable string (not an escape sequence)+ if (data.length > 1 && !data.startsWith("\x1b")) {+ try {+ // Verify it's a valid printable string+ const firstChar = data[0];+ if (firstChar && firstChar.charCodeAt(0) >= 32) {+ return data;+ }+ } catch {+ // Invalid UTF-8, ignore+ }+ }++ return undefined;+ }++ private updateResumeSearchQuery(query: string): void {+ if (!this.resumeSessionList) return;+ const filteredItems = filterResumeSessions(this.resumeSessionList.sessions, query);+ const list = new SelectList(filteredItems, Math.min(Math.max(filteredItems.length, 1), 8), selectListTheme, {+ minPrimaryColumnWidth: 24,+ maxPrimaryColumnWidth: 42,+ });+ list.onSelect = (item) => void this.handleResumeSessionSelection(item.value);+ list.onCancel = () => {+ this.resumeSessionList = null;+ this.tui.requestRender();+ };+ this.resumeSessionList = { ...this.resumeSessionList, list, searchQuery: query };+ this.tui.requestRender();+ }+private buildStartupPromptLines(width: number): string[] {if (!this.startupPromptList) {return [];@@ -1463,13 +1542,24 @@ export class AppScreen implements Component, Focusable {if (!this.resumeSessionList) {return [];}+ const searchBox = this.resumeSessionList.searchQuery+ ? padToWidth(palette.text.primary(`Search: ${this.resumeSessionList.searchQuery}${END_CURSOR}`), width)+ : padToWidth(palette.text.dim("Type to search · ↑/↓ choose · Enter resume · Esc cancel"), width);return [padToWidth(palette.status.warning(`Resume session (${this.resumeSessionList.total} total)`),width,),+ searchBox,...this.resumeSessionList.list.render(width),- padToWidth(palette.text.dim("↑/↓ choose · Enter resume · Esc cancel"), width),+ padToWidth(+ palette.text.dim(+ this.resumeSessionList.searchQuery+ ? "Backspace delete · Enter resume · Esc clear"+ : "↑/↓ choose · Enter resume · Esc cancel"+ ),+ width,+ ),];}