refac
This commit is contained in:
1 parent
9c14740ffb
commit
e5c8f8110a
2 files changed
+43
-5
No files matched your search
@@ -166,6 +166,11 @@ async def generate_title(request: Request, form_data: dict, user=Depends(get_ver
|
|||||||
models = request.app.state.MODELS
|
models = request.app.state.MODELS
|
||||||
|
|
||||||
model_id = form_data['model']
|
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:
|
if model_id not in models:
|
||||||
raise HTTPException(
|
raise HTTPException(
|
||||||
status_code=status.HTTP_404_NOT_FOUND,
|
status_code=status.HTTP_404_NOT_FOUND,
|
||||||
|
|||||||
@@ -51,6 +51,7 @@
|
|||||||
import Sparkles from '$lib/components/icons/Sparkles.svelte';
|
import Sparkles from '$lib/components/icons/Sparkles.svelte';
|
||||||
import Spinner from '$lib/components/common/Spinner.svelte';
|
import Spinner from '$lib/components/common/Spinner.svelte';
|
||||||
import { generateTitle } from '$lib/apis';
|
import { generateTitle } from '$lib/apis';
|
||||||
|
import { createMessagesList } from '$lib/utils';
|
||||||
|
|
||||||
export let className = '';
|
export let className = '';
|
||||||
|
|
||||||
@@ -343,14 +344,46 @@
|
|||||||
chat = await getChatById(localStorage.token, id);
|
chat = await getChatById(localStorage.token, id);
|
||||||
}
|
}
|
||||||
|
|
||||||
const messages = (chat.chat?.messages ?? []).map((message) => {
|
const chatContent = chat.chat;
|
||||||
return {
|
|
||||||
|
// 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,
|
role: message.role,
|
||||||
content: message.content
|
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 = '';
|
chatTitle = '';
|
||||||
|
|
||||||
|
|||||||
Reference in new issue
Block a user