Fix /new handling and status refresh
This commit is contained in:
parent
858298c070
commit
25dade1fb3
@ -10,6 +10,7 @@ const { renderBox } = require('../ui/banner');
|
|||||||
const { createIndentedWriter } = require('../ui/indented_writer');
|
const { createIndentedWriter } = require('../ui/indented_writer');
|
||||||
const { cyan, green } = require('../utils/colors');
|
const { cyan, green } = require('../utils/colors');
|
||||||
const { Spinner } = require('../ui/spinner');
|
const { Spinner } = require('../ui/spinner');
|
||||||
|
const { normalizeTokenUsage } = require('../utils/token_usage');
|
||||||
|
|
||||||
function printHelp() {
|
function printHelp() {
|
||||||
console.log('/new 创建新对话');
|
console.log('/new 创建新对话');
|
||||||
@ -23,8 +24,14 @@ function printHelp() {
|
|||||||
console.log('/help 显示指令列表');
|
console.log('/help 显示指令列表');
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function printNotice(message) {
|
||||||
|
console.log('');
|
||||||
|
console.log(message);
|
||||||
|
console.log('');
|
||||||
|
}
|
||||||
|
|
||||||
async function handleCommand(input, ctx) {
|
async function handleCommand(input, ctx) {
|
||||||
const { rl, state, config, workspace } = ctx;
|
const { rl, state, config, workspace, statusBar } = ctx;
|
||||||
const persist = () => {
|
const persist = () => {
|
||||||
if (!state.conversation) return;
|
if (!state.conversation) return;
|
||||||
state.conversation = updateConversation(workspace, state.conversation, state.messages || [], {
|
state.conversation = updateConversation(workspace, state.conversation, state.messages || [], {
|
||||||
@ -50,6 +57,7 @@ async function handleCommand(input, ctx) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (cmd === '/new') {
|
if (cmd === '/new') {
|
||||||
|
state.tokenUsage = normalizeTokenUsage({ prompt: 0, completion: 0, total: 0 });
|
||||||
const conv = createConversation(workspace, {
|
const conv = createConversation(workspace, {
|
||||||
model_key: state.modelKey,
|
model_key: state.modelKey,
|
||||||
model_id: state.modelId,
|
model_id: state.modelId,
|
||||||
@ -60,8 +68,9 @@ async function handleCommand(input, ctx) {
|
|||||||
});
|
});
|
||||||
state.conversation = conv;
|
state.conversation = conv;
|
||||||
state.messages = [];
|
state.messages = [];
|
||||||
console.log(`已创建新对话: ${conv.id}`);
|
printNotice(`已创建新对话: ${conv.id}`);
|
||||||
persist();
|
persist();
|
||||||
|
if (statusBar) statusBar.render();
|
||||||
return { exit: false };
|
return { exit: false };
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -62,6 +62,7 @@ let menuLastSearchTerm = '';
|
|||||||
let menuJustClosedAt = 0;
|
let menuJustClosedAt = 0;
|
||||||
let menuInjectedCommand = null;
|
let menuInjectedCommand = null;
|
||||||
let menuAbortController = null;
|
let menuAbortController = null;
|
||||||
|
let menuJustClosedInjected = false;
|
||||||
|
|
||||||
readline.emitKeypressEvents(process.stdin);
|
readline.emitKeypressEvents(process.stdin);
|
||||||
if (process.stdin.isTTY) process.stdin.setRawMode(true);
|
if (process.stdin.isTTY) process.stdin.setRawMode(true);
|
||||||
@ -124,6 +125,7 @@ process.stdin.on('keypress', (str, key) => {
|
|||||||
commandMenuActive = false;
|
commandMenuActive = false;
|
||||||
menuAbortController = null;
|
menuAbortController = null;
|
||||||
menuJustClosedAt = menuInjectedCommand ? Date.now() : 0;
|
menuJustClosedAt = menuInjectedCommand ? Date.now() : 0;
|
||||||
|
menuJustClosedInjected = !!menuInjectedCommand;
|
||||||
menuLastSearchTerm = menuSearchTerm;
|
menuLastSearchTerm = menuSearchTerm;
|
||||||
drainStdin();
|
drainStdin();
|
||||||
rl.line = '';
|
rl.line = '';
|
||||||
@ -179,22 +181,25 @@ function initReadline() {
|
|||||||
}
|
}
|
||||||
const input = line.trim();
|
const input = line.trim();
|
||||||
if (menuJustClosedAt) {
|
if (menuJustClosedAt) {
|
||||||
const tooOld = Date.now() - menuJustClosedAt > 800;
|
if (!menuJustClosedInjected) {
|
||||||
const normalizedMenu = String(menuLastSearchTerm).trim().replace(/^\/+/, '');
|
const tooOld = Date.now() - menuJustClosedAt > 800;
|
||||||
const normalizedInput = input.replace(/^\/+/, '');
|
const normalizedMenu = String(menuLastSearchTerm).trim().replace(/^\/+/, '');
|
||||||
if (!tooOld && (!normalizedMenu ? input === '' : normalizedInput === normalizedMenu)) {
|
const normalizedInput = input.replace(/^\/+/, '');
|
||||||
menuJustClosedAt = 0;
|
if (!tooOld && (!normalizedMenu ? input === '' : normalizedInput === normalizedMenu)) {
|
||||||
menuLastSearchTerm = '';
|
menuJustClosedAt = 0;
|
||||||
if (process.stdout.isTTY) {
|
menuLastSearchTerm = '';
|
||||||
readline.moveCursor(process.stdout, 0, -1);
|
if (process.stdout.isTTY) {
|
||||||
readline.clearLine(process.stdout, 0);
|
readline.moveCursor(process.stdout, 0, -1);
|
||||||
readline.cursorTo(process.stdout, 0);
|
readline.clearLine(process.stdout, 0);
|
||||||
|
readline.cursorTo(process.stdout, 0);
|
||||||
|
}
|
||||||
|
promptWithStatus();
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
rl.prompt();
|
|
||||||
return;
|
|
||||||
}
|
}
|
||||||
menuJustClosedAt = 0;
|
menuJustClosedAt = 0;
|
||||||
menuLastSearchTerm = '';
|
menuLastSearchTerm = '';
|
||||||
|
menuJustClosedInjected = false;
|
||||||
}
|
}
|
||||||
if (!input) {
|
if (!input) {
|
||||||
promptWithStatus();
|
promptWithStatus();
|
||||||
@ -202,7 +207,7 @@ function initReadline() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (input.startsWith('/')) {
|
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;
|
if (result && result.exit) return;
|
||||||
promptWithStatus();
|
promptWithStatus();
|
||||||
return;
|
return;
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user