diff --git a/src/cli/commands.js b/src/cli/commands.js index 0f48837..e8593e6 100644 --- a/src/cli/commands.js +++ b/src/cli/commands.js @@ -10,6 +10,7 @@ const { renderBox } = require('../ui/banner'); const { createIndentedWriter } = require('../ui/indented_writer'); const { cyan, green } = require('../utils/colors'); const { Spinner } = require('../ui/spinner'); +const { normalizeTokenUsage } = require('../utils/token_usage'); function printHelp() { console.log('/new 创建新对话'); @@ -23,8 +24,14 @@ function printHelp() { console.log('/help 显示指令列表'); } +function printNotice(message) { + console.log(''); + console.log(message); + console.log(''); +} + async function handleCommand(input, ctx) { - const { rl, state, config, workspace } = ctx; + const { rl, state, config, workspace, statusBar } = ctx; const persist = () => { if (!state.conversation) return; state.conversation = updateConversation(workspace, state.conversation, state.messages || [], { @@ -50,6 +57,7 @@ async function handleCommand(input, ctx) { } if (cmd === '/new') { + state.tokenUsage = normalizeTokenUsage({ prompt: 0, completion: 0, total: 0 }); const conv = createConversation(workspace, { model_key: state.modelKey, model_id: state.modelId, @@ -60,8 +68,9 @@ async function handleCommand(input, ctx) { }); state.conversation = conv; state.messages = []; - console.log(`已创建新对话: ${conv.id}`); + printNotice(`已创建新对话: ${conv.id}`); persist(); + if (statusBar) statusBar.render(); return { exit: false }; } diff --git a/src/cli/index.js b/src/cli/index.js index 915cb42..3f4759c 100644 --- a/src/cli/index.js +++ b/src/cli/index.js @@ -62,6 +62,7 @@ let menuLastSearchTerm = ''; let menuJustClosedAt = 0; let menuInjectedCommand = null; let menuAbortController = null; +let menuJustClosedInjected = false; readline.emitKeypressEvents(process.stdin); if (process.stdin.isTTY) process.stdin.setRawMode(true); @@ -124,6 +125,7 @@ process.stdin.on('keypress', (str, key) => { commandMenuActive = false; menuAbortController = null; menuJustClosedAt = menuInjectedCommand ? Date.now() : 0; + menuJustClosedInjected = !!menuInjectedCommand; menuLastSearchTerm = menuSearchTerm; drainStdin(); rl.line = ''; @@ -179,22 +181,25 @@ function initReadline() { } const input = line.trim(); if (menuJustClosedAt) { - const tooOld = Date.now() - menuJustClosedAt > 800; - const normalizedMenu = String(menuLastSearchTerm).trim().replace(/^\/+/, ''); - const normalizedInput = input.replace(/^\/+/, ''); - if (!tooOld && (!normalizedMenu ? input === '' : normalizedInput === normalizedMenu)) { - menuJustClosedAt = 0; - menuLastSearchTerm = ''; - if (process.stdout.isTTY) { - readline.moveCursor(process.stdout, 0, -1); - readline.clearLine(process.stdout, 0); - readline.cursorTo(process.stdout, 0); + if (!menuJustClosedInjected) { + const tooOld = Date.now() - menuJustClosedAt > 800; + const normalizedMenu = String(menuLastSearchTerm).trim().replace(/^\/+/, ''); + const normalizedInput = input.replace(/^\/+/, ''); + if (!tooOld && (!normalizedMenu ? input === '' : normalizedInput === normalizedMenu)) { + menuJustClosedAt = 0; + menuLastSearchTerm = ''; + if (process.stdout.isTTY) { + readline.moveCursor(process.stdout, 0, -1); + readline.clearLine(process.stdout, 0); + readline.cursorTo(process.stdout, 0); + } + promptWithStatus(); + return; } - rl.prompt(); - return; } menuJustClosedAt = 0; menuLastSearchTerm = ''; + menuJustClosedInjected = false; } if (!input) { promptWithStatus(); @@ -202,7 +207,7 @@ function initReadline() { } if (input.startsWith('/')) { - const result = await handleCommand(input, { rl, state, config, workspace: WORKSPACE }); + const result = await handleCommand(input, { rl, state, config, workspace: WORKSPACE, statusBar }); if (result && result.exit) return; promptWithStatus(); return;