u
This commit is contained in:
@@ -2,7 +2,9 @@ import * as db from '../../db/modelconversation.js';
|
||||
import env from '../../config/env.js';
|
||||
import { RETIRED_MODELS, resolveDefaultModel } from '../../tools/llm-key.js';
|
||||
import { emit } from '../sse.js';
|
||||
import { getOrCreateManager, removeManager } from '../../services/conversationManager.js';
|
||||
import { getOrCreateManager, removeManager, cleanUserMessageContent } from '../../services/conversationManager.js';
|
||||
|
||||
const FIRST_MESSAGE_MAX_CHARS = 200;
|
||||
|
||||
// #region Helpers
|
||||
|
||||
@@ -22,6 +24,24 @@ function parsePositiveInt(value, field) {
|
||||
return { id };
|
||||
}
|
||||
|
||||
/**
|
||||
* Turns the stored first user message payload (`{"content": ...}`) into short plain text
|
||||
* for conversation lists.
|
||||
* @param {string|null} storedMessage
|
||||
* @returns {string|null}
|
||||
*/
|
||||
function buildFirstMessagePreview(storedMessage) {
|
||||
if (!storedMessage) return null;
|
||||
let rawContent;
|
||||
try {
|
||||
rawContent = JSON.parse(storedMessage).content;
|
||||
} catch {
|
||||
rawContent = storedMessage;
|
||||
}
|
||||
const text = cleanUserMessageContent(rawContent).trim();
|
||||
return text.length > FIRST_MESSAGE_MAX_CHARS ? `${text.slice(0, FIRST_MESSAGE_MAX_CHARS)}…` : text;
|
||||
}
|
||||
|
||||
function modelConversationErrorStatus(error) {
|
||||
if (error.message === 'No company selected') return 400;
|
||||
if (error.message === 'Not authorized') return 403;
|
||||
@@ -92,7 +112,11 @@ export function register(app, basePath = '') {
|
||||
}
|
||||
|
||||
try {
|
||||
const conversations = await db.list(req.user.id);
|
||||
const rows = await db.list(req.user.id);
|
||||
const conversations = rows.map(({ first_user_message: storedFirstMessage, ...conversation }) => ({
|
||||
...conversation,
|
||||
first_message: buildFirstMessagePreview(storedFirstMessage),
|
||||
}));
|
||||
res.status(200).json({ conversations });
|
||||
} catch (error) {
|
||||
const status = modelConversationErrorStatus(error);
|
||||
|
||||
Reference in New Issue
Block a user