Fix /chat list not write terminal escape codes directly (#10415)

This commit is contained in:
Adam Weidman
2025-10-03 00:52:16 +02:00
committed by GitHub
parent fcdfa8609a
commit 16d4701822
7 changed files with 171 additions and 99 deletions
+15 -35
View File
@@ -17,15 +17,14 @@ import type {
import { CommandKind } from './types.js';
import { decodeTagName } from '@google/gemini-cli-core';
import path from 'node:path';
import type { HistoryItemWithoutId } from '../types.js';
import type {
HistoryItemWithoutId,
HistoryItemChatList,
ChatDetail,
} from '../types.js';
import { MessageType } from '../types.js';
import type { Content } from '@google/genai';
interface ChatDetail {
name: string;
mtime: Date;
}
const getSavedChatTags = async (
context: CommandContext,
mtSortDesc: boolean,
@@ -39,7 +38,7 @@ const getSavedChatTags = async (
const file_head = 'checkpoint-';
const file_tail = '.json';
const files = await fsPromises.readdir(geminiDir);
const chatDetails: Array<{ name: string; mtime: Date }> = [];
const chatDetails: ChatDetail[] = [];
for (const file of files) {
if (file.startsWith(file_head) && file.endsWith(file_tail)) {
@@ -48,15 +47,15 @@ const getSavedChatTags = async (
const tagName = file.slice(file_head.length, -file_tail.length);
chatDetails.push({
name: decodeTagName(tagName),
mtime: stats.mtime,
mtime: stats.mtime.toISOString(),
});
}
}
chatDetails.sort((a, b) =>
mtSortDesc
? b.mtime.getTime() - a.mtime.getTime()
: a.mtime.getTime() - b.mtime.getTime(),
? b.mtime.localeCompare(a.mtime)
: a.mtime.localeCompare(b.mtime),
);
return chatDetails;
@@ -69,34 +68,15 @@ const listCommand: SlashCommand = {
name: 'list',
description: 'List saved conversation checkpoints',
kind: CommandKind.BUILT_IN,
action: async (context): Promise<MessageActionReturn> => {
action: async (context): Promise<void> => {
const chatDetails = await getSavedChatTags(context, false);
if (chatDetails.length === 0) {
return {
type: 'message',
messageType: 'info',
content: 'No saved conversation checkpoints found.',
};
}
const maxNameLength = Math.max(
...chatDetails.map((chat) => chat.name.length),
);
let message = 'List of saved conversations:\n\n';
for (const chat of chatDetails) {
const paddedName = chat.name.padEnd(maxNameLength, ' ');
const isoString = chat.mtime.toISOString();
const match = isoString.match(/(\d{4}-\d{2}-\d{2})T(\d{2}:\d{2}:\d{2})/);
const formattedDate = match ? `${match[1]} ${match[2]}` : 'Invalid Date';
message += ` - \u001b[36m${paddedName}\u001b[0m \u001b[90m(saved on ${formattedDate})\u001b[0m\n`;
}
message += `\n\u001b[90mNote: Newest last, oldest first\u001b[0m`;
return {
type: 'message',
messageType: 'info',
content: message,
const item: HistoryItemChatList = {
type: MessageType.CHAT_LIST,
chats: chatDetails,
};
context.ui.addItem(item, Date.now());
},
};