Q141: reply to a specific message in its thread, and see what each reply answers

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
ZacharyZhang-NYandClaude Opus 5.5 committed 2026-10-08 23:01:57 -04:00
1 parent a6ab09d6b5
commit 87c297dd45
17 files changed
+377 -57

No files matched your search

+16
View File
@@ -0,0 +1,16 @@
import type { DatabaseSync } from "node:sqlite";
import type { ThreadHistory } from "../../shared/chat-domain";
import { camel } from "./snapshot-service";
/** Every message of one thread with its attachments, mentions and commands, however far back it started. */
export function threadHistory(db: DatabaseSync, orgId: string, threadId: string): ThreadHistory {
const thread = db.prepare("select id from chat_threads where id = ? and org_id = ?").get(threadId, orgId);
if (!thread) throw new Error("Chat thread was not found.");
const rows = <T>(sql: string) => (db.prepare(sql).all(threadId) as Array<Record<string, unknown>>).map((row) => camel(row) as T);
return {
messages: rows("select * from chat_messages where thread_id = ? order by created_at, rowid"),
attachments: rows("select a.* from chat_attachments a join chat_messages m on m.id = a.message_id where m.thread_id = ?"),
mentions: rows("select x.* from chat_mentions x join chat_messages m on m.id = x.message_id where m.thread_id = ? order by x.offset_start"),
slashInvocations: rows("select s.* from chat_slash_invocations s join chat_messages m on m.id = s.message_id where m.thread_id = ?"),
};
}
+8 -2
View File
@@ -21,7 +21,7 @@ function toBool(value: unknown): boolean {
return value === 1 || value === true;
}
function camel(row: Row): Row {
export function camel(row: Row): Row {
const next: Row = {};
for (const [key, value] of Object.entries(row)) {
next[key.replace(/_([a-z])/g, (_m, ch: string) => ch.toUpperCase())] = value;
@@ -97,7 +97,13 @@ export class SnapshotService {
chatSpaces: list(this.db, "chat_spaces", "created_at asc"),
chatMemberships: list(this.db, "chat_memberships", "space_id asc, principal_type asc, principal_id asc"),
chatMessages: recentList<AppSnapshot["chatMessages"][number]>(this.db, "chat_messages", "created_at desc", 500).reverse(),
chatThreads: recentList(this.db, "chat_threads", "updated_at desc", 200),
// Every thread a loaded message belongs to, plus the most recently active ones, so no shown message loses its thread.
chatThreads: all<Row>(this.db.prepare(`
select * from chat_threads
where id in (select thread_id from (select thread_id from chat_messages order by created_at desc limit 500))
or id in (select id from chat_threads order by updated_at desc limit 200)
order by updated_at desc
`)).map((row) => camel(row) as unknown as AppSnapshot["chatThreads"][number]),
chatAttachments: recentList(this.db, "chat_attachments", "message_id desc", 500),
chatMentions: recentList(this.db, "chat_mentions", "message_id desc, offset_start asc", 500),
chatSlashInvocations: recentList(this.db, "chat_slash_invocations", "created_at desc", 200),