refac
This commit is contained in:
@@ -166,6 +166,11 @@ async def generate_title(request: Request, form_data: dict, user=Depends(get_ver
|
||||
models = request.app.state.MODELS
|
||||
|
||||
model_id = form_data['model']
|
||||
if not model_id:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_400_BAD_REQUEST,
|
||||
detail='No model specified for title generation. Please ensure a model is selected for this chat.',
|
||||
)
|
||||
if model_id not in models:
|
||||
raise HTTPException(
|
||||
status_code=status.HTTP_404_NOT_FOUND,
|
||||
|
||||
@@ -51,6 +51,7 @@
|
||||
import Sparkles from '$lib/components/icons/Sparkles.svelte';
|
||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||
import { generateTitle } from '$lib/apis';
|
||||
import { createMessagesList } from '$lib/utils';
|
||||
|
||||
export let className = '';
|
||||
|
||||
@@ -343,14 +344,46 @@
|
||||
chat = await getChatById(localStorage.token, id);
|
||||
}
|
||||
|
||||
const messages = (chat.chat?.messages ?? []).map((message) => {
|
||||
return {
|
||||
const chatContent = chat.chat;
|
||||
|
||||
// Build the active branch message list from the history tree.
|
||||
// Fallback to the legacy flat messages array for older chats
|
||||
// that haven't been migrated to the tree structure yet.
|
||||
const history = chatContent?.history;
|
||||
let messages = [];
|
||||
if (history?.messages && history?.currentId) {
|
||||
messages = createMessagesList(history, history.currentId).map((message) => ({
|
||||
role: message.role,
|
||||
content: message.content
|
||||
};
|
||||
});
|
||||
}));
|
||||
} else {
|
||||
messages = (chatContent?.messages ?? []).map((message) => ({
|
||||
role: message.role,
|
||||
content: message.content
|
||||
}));
|
||||
}
|
||||
|
||||
const model = chat.chat.models.at(0) ?? chat.models.at(0) ?? '';
|
||||
// Resolve the model from the most recent assistant message in the
|
||||
// active branch. This avoids using the stale top-level `models`
|
||||
// array which may reference a model from an older edit.
|
||||
let model = '';
|
||||
if (history?.messages && history?.currentId) {
|
||||
let currentId = history.currentId;
|
||||
while (currentId) {
|
||||
const msg = history.messages[currentId];
|
||||
if (!msg) break;
|
||||
if (msg.role === 'assistant' && msg.model) {
|
||||
model = msg.model;
|
||||
break;
|
||||
}
|
||||
currentId = msg.parentId;
|
||||
}
|
||||
}
|
||||
|
||||
// Fallback to top-level models if no model was found in the history
|
||||
if (!model) {
|
||||
model = chatContent?.models?.at(0) ?? '';
|
||||
}
|
||||
|
||||
chatTitle = '';
|
||||
|
||||
|
||||
Reference in New Issue
Block a user