Skip to content
Source
Sign inSign up
samwillis/neonbricks
Open History to see the last commit

ai-conversation.ts

45 lines1.4 KB

1import type { AIConversationRow, AIGeneration } from "../shared/types";
2​
3export const activeAIStates = ["queued", "designing", "checking", "repairing"];
4​
5// The live query is authoritative for the entire conversation, including new
6// prompts from other devices and rows removed by a membership withdrawal.
7export function conversationRows(
8 rows: readonly AIConversationRow[],
9 previous: readonly AIGeneration[] = [],
10): AIGeneration[] {
11 const prior = new Map(previous.map((r) => [r.id, r]));
12 return [...rows]
13 .sort(
14 (a, b) =>
15 (a.created_at || "").localeCompare(b.created_at || "") ||
16 a.id.localeCompare(b.id),
17 )
18 .map((row) => {
19 const next = {
20 ...row,
21 blueprint:
22 row.blueprint ??
23 (row.title ? { title: row.title, summary: row.summary || "" } : null),
24 parts: ["ready", "accepted"].includes(row.status)
25 ? row.preview?.parts || []
26 : [],
27 };
28 const old = prior.get(row.id);
29 return old && JSON.stringify(old) === JSON.stringify(next) ? old : next;
30 });
32​
33export function conversationState(jobs: AIGeneration[]) {
34 const job =
35 jobs.filter((r) => activeAIStates.includes(r.status)).at(-1) ||
36 jobs.at(-1) ||
37 null;
38 const lastApplied = jobs.map((r) => r.status).lastIndexOf("accepted");
39 const preview =
40 jobs
41 .slice(lastApplied + 1)
42 .filter((r) => r.status === "ready")
43 .at(-1) || null;
44 return { job, preview };