feat: add searchMessages function and corresponding API endpoint for message queries
This commit is contained in:
@@ -188,7 +188,9 @@ function scheduleConversationAnalysis(conversationKey: string): void {
|
||||
|
||||
// If we have available slots, process immediately with shorter debounce
|
||||
const debounceTime =
|
||||
activeRequests < MAX_ACTIVE_REQUESTS ? Math.min(DEBOUNCE_MS, 500) : DEBOUNCE_MS;
|
||||
activeRequests < MAX_ACTIVE_REQUESTS
|
||||
? Math.min(DEBOUNCE_MS, 500)
|
||||
: DEBOUNCE_MS;
|
||||
|
||||
// Set new debounced timer
|
||||
const timer = setTimeout(async () => {
|
||||
|
||||
@@ -180,8 +180,20 @@ export function parseModerationResponse(
|
||||
// Check that all target IDs were found
|
||||
const missingIds = targetIds.filter((id) => !foundIds.has(id));
|
||||
if (missingIds.length > 0) {
|
||||
log.warn({ missingIds }, "Some target IDs missing in response");
|
||||
throw new Error(`Missing target IDs: ${missingIds.join(",")}`);
|
||||
log.warn(
|
||||
{ missingIds, foundCount: foundIds.size, totalCount: targetIds.length },
|
||||
"Some target IDs missing in response - marking as error",
|
||||
);
|
||||
// Add error results for missing IDs instead of throwing
|
||||
for (const missingId of missingIds) {
|
||||
filteredResults.push({
|
||||
messageId: missingId,
|
||||
status: "clean",
|
||||
flags: [],
|
||||
score: 0,
|
||||
analysis: "Analysis incomplete - LLM did not process this message",
|
||||
});
|
||||
}
|
||||
}
|
||||
|
||||
return filteredResults;
|
||||
|
||||
@@ -639,3 +639,54 @@ export async function getAttachmentsForMessages(
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
export async function searchMessages(input: {
|
||||
query: string;
|
||||
channelId?: string;
|
||||
limit?: number;
|
||||
}): Promise<MessageRecord[]> {
|
||||
try {
|
||||
const { query, channelId, limit = 20 } = input;
|
||||
const database = db();
|
||||
|
||||
const searchPattern = `%${query}%`;
|
||||
const conditions: (SQL | undefined)[] = [isNull(messagesTable.deleted_at)];
|
||||
|
||||
if (channelId) {
|
||||
conditions.push(
|
||||
or(
|
||||
eq(messagesTable.channel_id, channelId),
|
||||
eq(messagesTable.thread_id, channelId),
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
conditions.push(
|
||||
or(
|
||||
sql`${messagesTable.content} LIKE ${searchPattern}`,
|
||||
sql`${messagesTable.edited_content} LIKE ${searchPattern}`,
|
||||
),
|
||||
);
|
||||
|
||||
const validConditions = conditions.filter((c): c is SQL => c !== undefined);
|
||||
|
||||
const rows = await database
|
||||
.select()
|
||||
.from(messagesTable)
|
||||
.where(and(...validConditions))
|
||||
.orderBy(desc(messagesTable.created_at))
|
||||
.limit(limit);
|
||||
|
||||
return rows as MessageRecord[];
|
||||
} catch (error) {
|
||||
logger.error(
|
||||
{
|
||||
query: input.query,
|
||||
channelId: input.channelId,
|
||||
error: error instanceof Error ? error.message : String(error),
|
||||
},
|
||||
"Failed to search messages",
|
||||
);
|
||||
throw error;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user