|
| 1 | +import { CommandKind, SlashCommand } from './types.js'; |
| 2 | +import { t } from '../utils/i18n.js'; |
| 3 | +import { HistoryItem } from '../types.js'; |
| 4 | + |
| 5 | +type HistoryOptions = { |
| 6 | + limit: number; |
| 7 | + type: 'user' | 'assistant' | 'error' | 'all'; |
| 8 | + query: string; |
| 9 | +}; |
| 10 | + |
| 11 | +const parseHistoryArgs = (args: string): HistoryOptions => { |
| 12 | + const tokens = args.split(/\s+/).filter(Boolean); |
| 13 | + let limit = 20; |
| 14 | + let type: HistoryOptions['type'] = 'user'; |
| 15 | + const queryParts: string[] = []; |
| 16 | + |
| 17 | + for (let i = 0; i < tokens.length; i += 1) { |
| 18 | + const token = tokens[i]; |
| 19 | + if (token === '--limit' && tokens[i + 1]) { |
| 20 | + limit = Number(tokens[i + 1]) || limit; |
| 21 | + i += 1; |
| 22 | + continue; |
| 23 | + } |
| 24 | + if (token.startsWith('--limit=')) { |
| 25 | + const value = token.split('=')[1]; |
| 26 | + limit = Number(value) || limit; |
| 27 | + continue; |
| 28 | + } |
| 29 | + if (token === '--type' && tokens[i + 1]) { |
| 30 | + const value = tokens[i + 1] as HistoryOptions['type']; |
| 31 | + type = value; |
| 32 | + i += 1; |
| 33 | + continue; |
| 34 | + } |
| 35 | + if (token.startsWith('--type=')) { |
| 36 | + const value = token.split('=')[1] as HistoryOptions['type']; |
| 37 | + type = value; |
| 38 | + continue; |
| 39 | + } |
| 40 | + queryParts.push(token); |
| 41 | + } |
| 42 | + |
| 43 | + return { |
| 44 | + limit: Math.max(1, Math.min(200, limit)), |
| 45 | + type, |
| 46 | + query: queryParts.join(' ').trim(), |
| 47 | + }; |
| 48 | +}; |
| 49 | + |
| 50 | +const matchesType = ( |
| 51 | + item: HistoryItem, |
| 52 | + type: HistoryOptions['type'], |
| 53 | +): boolean => { |
| 54 | + if (type === 'all') { |
| 55 | + return true; |
| 56 | + } |
| 57 | + if (type === 'user') { |
| 58 | + return item.type === 'user' || item.type === 'user_shell'; |
| 59 | + } |
| 60 | + if (type === 'assistant') { |
| 61 | + return item.type === 'gemini' || item.type === 'gemini_content'; |
| 62 | + } |
| 63 | + return item.type === 'error'; |
| 64 | +}; |
| 65 | + |
| 66 | +const formatHistoryItem = (item: HistoryItem): string => { |
| 67 | + if (item.text && item.text.trim()) { |
| 68 | + return `[${item.type}] ${item.text}`; |
| 69 | + } |
| 70 | + return `[${item.type}] (no text)`; |
| 71 | +}; |
| 72 | + |
| 73 | +export const historyCommand: SlashCommand = { |
| 74 | + name: 'history', |
| 75 | + description: t('command.history.description'), |
| 76 | + kind: CommandKind.BUILT_IN, |
| 77 | + action: async (context, args) => { |
| 78 | + const options = parseHistoryArgs(args); |
| 79 | + const history = context.ui.history ?? []; |
| 80 | + |
| 81 | + if (history.length === 0) { |
| 82 | + return { |
| 83 | + type: 'message', |
| 84 | + messageType: 'info', |
| 85 | + content: t('command.history.empty'), |
| 86 | + }; |
| 87 | + } |
| 88 | + |
| 89 | + const query = options.query.toLowerCase(); |
| 90 | + const filtered = history.filter((item) => { |
| 91 | + if (!matchesType(item, options.type)) { |
| 92 | + return false; |
| 93 | + } |
| 94 | + if (!query) { |
| 95 | + return true; |
| 96 | + } |
| 97 | + return (item.text ?? '').toLowerCase().includes(query); |
| 98 | + }); |
| 99 | + |
| 100 | + const latest = filtered.slice(-options.limit).reverse(); |
| 101 | + const lines = latest.map((item, index) => |
| 102 | + `${index + 1}. ${formatHistoryItem(item)}`.trim(), |
| 103 | + ); |
| 104 | + |
| 105 | + if (lines.length === 0) { |
| 106 | + return { |
| 107 | + type: 'message', |
| 108 | + messageType: 'info', |
| 109 | + content: t('command.history.empty'), |
| 110 | + }; |
| 111 | + } |
| 112 | + |
| 113 | + return { |
| 114 | + type: 'message', |
| 115 | + messageType: 'info', |
| 116 | + content: [t('command.history.header'), ...lines].join('\n'), |
| 117 | + }; |
| 118 | + }, |
| 119 | +}; |
0 commit comments