Q9: chat wakes carry every new message and the latest history
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
2a7b17a47a
commit
abe9534350
2 files changed
+48
-26
No files matched your search
@@ -1,6 +1,16 @@
|
|||||||
import type { DatabaseSync } from "node:sqlite";
|
import type { DatabaseSync } from "node:sqlite";
|
||||||
import type { ChatContextBlock } from "./task-prompts";
|
import type { ChatContextBlock } from "./task-prompts";
|
||||||
|
|
||||||
|
const HISTORY_LIMIT = 20;
|
||||||
|
|
||||||
|
const MESSAGE_SELECT = `
|
||||||
|
select m.body_md, m.created_at, m.edited_at, m.sender_type, m.sender_id,
|
||||||
|
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
|
||||||
|
coalesce(a.role, e.role) as sender_role
|
||||||
|
from chat_messages m
|
||||||
|
left join agents a on a.id = m.sender_id and m.sender_type = 'agent'
|
||||||
|
left join employees e on e.id = m.sender_id and m.sender_type = 'employee'`;
|
||||||
|
|
||||||
interface ThreadMessageRow {
|
interface ThreadMessageRow {
|
||||||
body_md: string;
|
body_md: string;
|
||||||
created_at: string;
|
created_at: string;
|
||||||
@@ -18,28 +28,25 @@ export function loadChatWakeContext(db: DatabaseSync, orgId: string, agentId: st
|
|||||||
const thread = db.prepare("select space_id from chat_threads where id = ? and org_id = ?")
|
const thread = db.prepare("select space_id from chat_threads where id = ? and org_id = ?")
|
||||||
.get(scope.sourceThreadId, orgId) as { space_id: string } | undefined;
|
.get(scope.sourceThreadId, orgId) as { space_id: string } | undefined;
|
||||||
if (!thread) throw new Error(`Chat wake thread ${scope.sourceThreadId} no longer exists.`);
|
if (!thread) throw new Error(`Chat wake thread ${scope.sourceThreadId} no longer exists.`);
|
||||||
const messages = db.prepare(`
|
const lastReplyAt = (db.prepare(`
|
||||||
select m.body_md, m.created_at, m.edited_at, m.sender_type, m.sender_id,
|
select coalesce(max(created_at), '') as at from chat_messages
|
||||||
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
|
where thread_id = ? and org_id = ? and sender_type = 'agent' and sender_id = ?
|
||||||
coalesce(a.role, e.role) as sender_role
|
`).get(scope.sourceThreadId, orgId, agentId) as { at: string }).at;
|
||||||
from chat_messages m
|
const isFresh = `not (m.sender_type = 'agent' and m.sender_id = ?) and (m.created_at > ? or coalesce(m.edited_at, '') > ?)`;
|
||||||
left join agents a on a.id = m.sender_id and m.sender_type = 'agent'
|
const args = [scope.sourceThreadId, orgId, agentId, lastReplyAt, lastReplyAt];
|
||||||
left join employees e on e.id = m.sender_id and m.sender_type = 'employee'
|
const fresh = db.prepare(`${MESSAGE_SELECT} where m.thread_id = ? and m.org_id = ? and ${isFresh} order by m.created_at asc`)
|
||||||
where m.thread_id = ? and m.org_id = ?
|
.all(...args) as unknown as ThreadMessageRow[];
|
||||||
order by m.created_at asc
|
const history = (db.prepare(`${MESSAGE_SELECT} where m.thread_id = ? and m.org_id = ? and not (${isFresh}) order by m.created_at desc limit ?`)
|
||||||
limit 20
|
.all(...args, HISTORY_LIMIT) as unknown as ThreadMessageRow[]).reverse();
|
||||||
`).all(scope.sourceThreadId, orgId) as unknown as ThreadMessageRow[];
|
const historyTotal = (db.prepare(`select count(*) as n from chat_messages m where m.thread_id = ? and m.org_id = ? and not (${isFresh})`)
|
||||||
if (messages.length === 0) return null;
|
.get(...args) as { n: number }).n;
|
||||||
const isOwn = (m: ThreadMessageRow) => m.sender_type === "agent" && m.sender_id === agentId;
|
if (fresh.length === 0 && history.length === 0) return null;
|
||||||
const ownReplies = messages.filter(isOwn);
|
const omitted = historyTotal - history.length;
|
||||||
const lastReplyAt = ownReplies.length > 0 ? ownReplies[ownReplies.length - 1].created_at : "";
|
|
||||||
const isFresh = (m: ThreadMessageRow) =>
|
|
||||||
!isOwn(m) && (m.created_at > lastReplyAt || (m.edited_at !== null && m.edited_at > lastReplyAt));
|
|
||||||
return {
|
return {
|
||||||
threadId: scope.sourceThreadId,
|
threadId: scope.sourceThreadId,
|
||||||
spaceId: thread.space_id,
|
spaceId: thread.space_id,
|
||||||
history: messages.filter((m) => !isFresh(m)).map(formatMessage).join("\n"),
|
history: [...(omitted > 0 ? [`(${omitted} earlier messages in this thread are not shown.)`] : []), ...history.map(formatMessage)].join("\n"),
|
||||||
fresh: messages.filter(isFresh).map(formatMessage).join("\n"),
|
fresh: fresh.map(formatMessage).join("\n"),
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -60,13 +67,7 @@ export function createRunMessagePoller(
|
|||||||
): () => string | null {
|
): () => string | null {
|
||||||
let watermark = input.runStartedAt;
|
let watermark = input.runStartedAt;
|
||||||
return () => {
|
return () => {
|
||||||
const rows = db.prepare(`
|
const rows = db.prepare(`${MESSAGE_SELECT}
|
||||||
select m.body_md, m.created_at, m.edited_at, m.sender_type, m.sender_id,
|
|
||||||
coalesce(a.name, e.display_name, m.sender_type) as sender_name,
|
|
||||||
coalesce(a.role, e.role) as sender_role
|
|
||||||
from chat_messages m
|
|
||||||
left join agents a on a.id = m.sender_id and m.sender_type = 'agent'
|
|
||||||
left join employees e on e.id = m.sender_id and m.sender_type = 'employee'
|
|
||||||
where m.org_id = ? and m.created_at > ? and m.deleted_at is null
|
where m.org_id = ? and m.created_at > ? and m.deleted_at is null
|
||||||
and not (m.sender_type = 'agent' and m.sender_id = ?)
|
and not (m.sender_type = 'agent' and m.sender_id = ?)
|
||||||
and (m.thread_id = ? or m.thread_id in (
|
and (m.thread_id = ? or m.thread_id in (
|
||||||
|
|||||||
@@ -173,4 +173,25 @@ describe("chat wake context", () => {
|
|||||||
expect(last.role === "user" && last.text).toContain("Stop — use Postgres instead.");
|
expect(last.role === "user" && last.text).toContain("Stop — use Postgres instead.");
|
||||||
h.connection.close();
|
h.connection.close();
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test("a long thread keeps every new message and the most recent history", async () => {
|
||||||
|
const h = await founderLine();
|
||||||
|
const threadId = h.say("Message 0", null);
|
||||||
|
await h.runWake();
|
||||||
|
for (let index = 1; index <= 30; index += 1) h.say(`Message ${index}`, threadId);
|
||||||
|
await h.runWake();
|
||||||
|
for (let index = 31; index <= 55; index += 1) h.say(`Message ${index}`, threadId);
|
||||||
|
await h.runWake();
|
||||||
|
|
||||||
|
const prompt = h.wakePrompts[2];
|
||||||
|
const earlier = prompt.slice(prompt.indexOf("Earlier in this thread"), prompt.indexOf("New since your last reply"));
|
||||||
|
const fresh = prompt.slice(prompt.indexOf("New since your last reply"));
|
||||||
|
const lineEnds = (block: string, text: string) => block.split("\n").some((line) => line.endsWith(text));
|
||||||
|
for (let index = 31; index <= 55; index += 1) expect(lineEnds(fresh, `: Message ${index}`)).toBe(true);
|
||||||
|
expect(lineEnds(earlier, ": Reply 2.")).toBe(true);
|
||||||
|
expect(lineEnds(earlier, ": Message 12")).toBe(true);
|
||||||
|
expect(lineEnds(earlier, ": Message 11")).toBe(false);
|
||||||
|
expect(earlier).toContain("13 earlier messages in this thread are not shown.");
|
||||||
|
h.connection.close();
|
||||||
|
});
|
||||||
});
|
});
|
||||||
Reference in new issue
Block a user