adds the ability to rename chats
This commit is contained in:
@@ -60,6 +60,14 @@ export class SybilApiClient {
|
||||
return data.chat;
|
||||
}
|
||||
|
||||
async updateChatTitle(chatId: string, title: string) {
|
||||
const data = await this.request<{ chat: ChatSummary }>(`/v1/chats/${chatId}`, {
|
||||
method: "PATCH",
|
||||
body: { title },
|
||||
});
|
||||
return data.chat;
|
||||
}
|
||||
|
||||
async suggestChatTitle(body: { chatId: string; content: string }) {
|
||||
const data = await this.request<{ chat: ChatSummary }>("/v1/chats/title/suggest", {
|
||||
method: "POST",
|
||||
|
||||
100
tui/src/index.ts
100
tui/src/index.ts
@@ -254,6 +254,7 @@ async function main() {
|
||||
let renderedSidebarItems: SidebarItem[] = [];
|
||||
let renderedSidebarLines: string[] = [];
|
||||
let suppressedSidebarSelectEvents = 0;
|
||||
let isRenamePromptOpen = false;
|
||||
|
||||
const screen = blessed.screen({
|
||||
smartCSR: true,
|
||||
@@ -361,6 +362,26 @@ async function main() {
|
||||
},
|
||||
});
|
||||
|
||||
const renamePrompt = (blessed as any).prompt({
|
||||
parent: screen,
|
||||
label: " Rename chat ",
|
||||
border: "line",
|
||||
tags: true,
|
||||
keys: true,
|
||||
vi: true,
|
||||
mouse: true,
|
||||
top: "center",
|
||||
left: "center",
|
||||
width: "50%",
|
||||
height: "shrink",
|
||||
hidden: true,
|
||||
style: {
|
||||
border: { fg: "cyan" },
|
||||
label: { fg: "cyan" },
|
||||
fg: "white",
|
||||
},
|
||||
});
|
||||
|
||||
const focusables = [sidebar, transcript, composer] as const;
|
||||
|
||||
function getTranscriptViewportHeight() {
|
||||
@@ -680,7 +701,7 @@ async function main() {
|
||||
const top = `{bold}${escapeTags(getSelectedTitle())}{/bold} {gray-fg}- Sybil TUI${modeLabel}${isSearchMode ? " • Exa Search" : ""}{/gray-fg}`;
|
||||
|
||||
let controls =
|
||||
"{gray-fg}Controls:{/gray-fg} [tab] focus [esc] command mode [↑/↓] highlight [enter] send/select [n] new chat [/] new search [d] delete [q] quit";
|
||||
"{gray-fg}Controls:{/gray-fg} [tab] focus [esc] command mode [↑/↓] highlight [enter] send/select [n] new chat [/] new search [r] rename [d] delete [C-r] refresh [q] quit";
|
||||
if (!isSearchMode) {
|
||||
controls += `\n{gray-fg}Model:{/gray-fg} provider {cyan-fg}${provider}{/cyan-fg} [p] model {cyan-fg}${escapeTags(model)}{/cyan-fg} [m]`;
|
||||
controls += providerModelOptions.length === 0 ? " {red-fg}(no models){/red-fg}" : "";
|
||||
@@ -842,6 +863,27 @@ async function main() {
|
||||
composer.readInput();
|
||||
}
|
||||
|
||||
function shouldIgnoreGlobalShortcut() {
|
||||
return isRenamePromptOpen || isTextInputFocused(screen, composer);
|
||||
}
|
||||
|
||||
function promptForChatTitle(currentTitle: string) {
|
||||
isRenamePromptOpen = true;
|
||||
updateUI();
|
||||
return new Promise<string | null>((resolve) => {
|
||||
renamePrompt.input("Title:", currentTitle, (err: Error | null, value: string | null) => {
|
||||
isRenamePromptOpen = false;
|
||||
renamePrompt.hide();
|
||||
screen.render();
|
||||
if (err || value === null || value === undefined) {
|
||||
resolve(null);
|
||||
return;
|
||||
}
|
||||
resolve(value);
|
||||
});
|
||||
});
|
||||
}
|
||||
|
||||
function cycleFocus(step: 1 | -1) {
|
||||
const focused = screen.focused;
|
||||
const currentIndex = focusables.findIndex((node) => node === focused);
|
||||
@@ -1302,6 +1344,37 @@ async function main() {
|
||||
await refreshCollections({ loadSelection: true, scrollToBottomOnLoad: true });
|
||||
}
|
||||
|
||||
async function handleRenameSelection() {
|
||||
if (!selectedItem || selectedItem.kind !== "chat") return;
|
||||
|
||||
const chatId = selectedItem.id;
|
||||
const summary = chats.find((chat) => chat.id === chatId);
|
||||
const currentTitle = selectedChat?.id === chatId ? getChatTitle(selectedChat, selectedChat.messages) : summary ? getChatTitle(summary) : "New chat";
|
||||
const value = await promptForChatTitle(currentTitle);
|
||||
const title = value?.trim();
|
||||
if (!title) {
|
||||
updateUI();
|
||||
return;
|
||||
}
|
||||
|
||||
setError(null);
|
||||
const updated = await api.updateChatTitle(chatId, title);
|
||||
chats = [updated, ...chats.filter((chat) => chat.id !== updated.id)];
|
||||
workspaceItems = upsertWorkspaceItem(workspaceItems, chatWorkspaceItem(updated));
|
||||
if (selectedChat?.id === updated.id) {
|
||||
selectedChat = {
|
||||
...selectedChat,
|
||||
title: updated.title,
|
||||
updatedAt: updated.updatedAt,
|
||||
initiatedProvider: updated.initiatedProvider,
|
||||
initiatedModel: updated.initiatedModel,
|
||||
lastUsedProvider: updated.lastUsedProvider,
|
||||
lastUsedModel: updated.lastUsedModel,
|
||||
};
|
||||
}
|
||||
updateUI();
|
||||
}
|
||||
|
||||
function cycleProvider() {
|
||||
const visibleProviders = getVisibleProviders(modelCatalog);
|
||||
const cycleProviders = visibleProviders.length ? visibleProviders : BASE_PROVIDERS;
|
||||
@@ -1387,18 +1460,18 @@ async function main() {
|
||||
});
|
||||
|
||||
screen.key(["q"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
screen.destroy();
|
||||
process.exit(0);
|
||||
});
|
||||
|
||||
screen.key(["tab"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
cycleFocus(1);
|
||||
});
|
||||
|
||||
screen.key(["S-tab", "backtab"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
cycleFocus(-1);
|
||||
});
|
||||
|
||||
@@ -1415,36 +1488,43 @@ async function main() {
|
||||
});
|
||||
|
||||
screen.key(["n"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
handleCreateChat();
|
||||
});
|
||||
|
||||
screen.key(["/"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
handleCreateSearch();
|
||||
});
|
||||
|
||||
screen.key(["d"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
void runAction(async () => {
|
||||
await handleDeleteSelection();
|
||||
});
|
||||
});
|
||||
|
||||
screen.key(["p"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
if (getIsSearchMode() || isSending) return;
|
||||
cycleProvider();
|
||||
});
|
||||
|
||||
screen.key(["m"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
if (getIsSearchMode() || isSending) return;
|
||||
cycleModel();
|
||||
});
|
||||
|
||||
screen.key(["r"], () => {
|
||||
if (isTextInputFocused(screen, composer)) return;
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
void runAction(async () => {
|
||||
await handleRenameSelection();
|
||||
});
|
||||
});
|
||||
|
||||
screen.key(["C-r"], () => {
|
||||
if (shouldIgnoreGlobalShortcut()) return;
|
||||
void runAction(async () => {
|
||||
await refreshCollections({ loadSelection: true });
|
||||
await refreshModels();
|
||||
|
||||
Reference in New Issue
Block a user