Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/controllers/history.controller.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,6 +54,7 @@ const getRecentThreads = async (req, res, next) => {
// Extract search filters (supports both search and regular listing)
const filters = {
keyword: req.query.keyword,
filter_by: req.query.filter_by,
time_range:
req.query.start_date || req.query.end_date
? {
Expand Down
46 changes: 33 additions & 13 deletions src/db_services/history.service.js
Original file line number Diff line number Diff line change
Expand Up @@ -102,19 +102,39 @@ async function findRecentThreadsByBridgeId(org_id, bridge_id, filters, user_feed
}

// Add keyword search across recommended columns
if (filters?.keyword?.length > 0 && filters?.keyword !== "") {
const keywordConditions = {
[Sequelize.Op.or]: [
{ message_id: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ thread_id: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ sub_thread_id: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ llm_message: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ user: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ chatbot_message: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } },
{ updated_llm_message: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } }
]
};
whereConditions[Sequelize.Op.and] = [keywordConditions];
const searchableColumns = ["message_id", "thread_id", "sub_thread_id", "llm_message", "user", "chatbot_message", "updated_llm_message"];
const filterBy = filters?.filter_by;

if (filterBy && typeof filterBy === "object" && Object.keys(filterBy).length > 0) {
const orConditions = [];
for (const [col, keyword] of Object.entries(filterBy)) {
if (!keyword || keyword === "") continue;
const escapedKw = keyword.replace(/'/g, "''");
if (col === "variables") {
orConditions.push(
Sequelize.literal(
`EXISTS (SELECT 1 FROM jsonb_each_text(COALESCE("conversation_logs"."variables", '{}'::jsonb)) AS kv WHERE jsonb_typeof(COALESCE("conversation_logs"."variables", 'null'::jsonb)) = 'object' AND kv.value ILIKE '%${escapedKw}%')`
)
);
} else if (searchableColumns.includes(col)) {
orConditions.push({ [col]: { [Sequelize.Op.iLike]: `%${keyword}%` } });
}
}
if (orConditions.length > 0) {
whereConditions[Sequelize.Op.and] = [{ [Sequelize.Op.or]: orConditions }];
}
} else if (filters?.keyword?.length > 0 && filters?.keyword !== "") {
const escapedKeyword = filters.keyword.replace(/'/g, "''");
whereConditions[Sequelize.Op.and] = [
{
[Sequelize.Op.or]: [
...searchableColumns.map((col) => ({ [col]: { [Sequelize.Op.iLike]: `%${filters.keyword}%` } })),
Sequelize.literal(
`EXISTS (SELECT 1 FROM jsonb_each_text(COALESCE("conversation_logs"."variables", '{}'::jsonb)) AS kv WHERE jsonb_typeof(COALESCE("conversation_logs"."variables", 'null'::jsonb)) = 'object' AND kv.value ILIKE '%${escapedKeyword}%')`
)
]
}
];
}

// Get recent threads with distinct thread_id, ordered by updated_at
Expand Down