-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
Expand file tree
/
Copy pathchat_title.ts
More file actions
44 lines (37 loc) · 1.39 KB
/
Copy pathchat_title.ts
File metadata and controls
44 lines (37 loc) · 1.39 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
import becca from "../../becca/becca.js";
import { getProvider } from "./index.js";
import { OllamaProvider } from "./providers/ollama.js";
import log from "../log.js";
import { t } from "i18next";
/** Default title prefixes that indicate the note hasn't been manually renamed. */
function hasDefaultTitle(title: string): boolean {
// "Chat: <timestamp>" from sidebar/API-created chats
const chatPrefix = t("special_notes.llm_chat_prefix");
// "New note" from manually created chats
const newNoteTitle = t("notes.new-note");
return title.startsWith(chatPrefix) || title === newNoteTitle;
}
/**
* Generate a short descriptive title for a chat note based on the first user message,
* then rename the note. Only renames if the note still has a default title.
*/
export async function generateChatTitle(chatNoteId: string, firstMessage: string): Promise<void> {
const note = becca.getNote(chatNoteId);
if (!note) {
return;
}
if (!hasDefaultTitle(note.title)) {
return;
}
const provider = getProvider();
// Ensure Ollama models are loaded so titleModel is set
if (provider instanceof OllamaProvider) {
await provider.loadModels();
}
const title = await provider.generateTitle(firstMessage);
if (title) {
note.title = title;
note.save();
log.info(`Auto-renamed chat note ${chatNoteId} to "${title}"`);
}
}