Open-source agentic AI Search Execution Platform. Track AI visibility, find opportunities, and let agents turn signals into prioritized actions and drafted fixes across ChatGPT, Claude, Gemini, Google AI Overviews, Google AI Mode, Perplexity, Grok, and Microsoft Copilot.
TypeScript aeoai-agentai-searchanswer-engine-optimizationbrand-monitoringchatgptclaudecopilotdockergeminigeogoogle-ai-overviewsgrokmcpnextjsopen-sourceperplexityseosupabasetypescript
import { createClient } from '@/lib/supabase/server';
import type { ChatStep } from './events';
import { CHAT_RUN_STEP_SELECT, toClientStep, type ChatRunStepMemberRow } from './steps';
/**
* A saved chat as its page opens it (CHAT-01, CHAT-03 item 21): its
* messages, and each answer's run with the steps as kept (toClientStep: no
* tool, no debug record). Read as the person, so the chat's policy holds: a
* chat is its author's only (CHAT-20 §16c). A run still under way is rebuilt
* by the page from its events (`…/events?after=0`), and keeps streaming.
*/
export interface ThreadRun {
/** The run's handle for its routes (approve, stop, events): the person's own. */
runId: string;
status: string;
steps: ChatStep[];
}
export interface ThreadMessage {
role: 'user' | 'assistant';
text: string;
generatedAt: string | null;
dataAsOf: string | null;
timezone: string | null;
run: ThreadRun | null;
}
export interface ChatThread {
conversationId: string;
title: string | null;
brandId: string | null;
messages: ThreadMessage[];
}
/** The person's chat with its messages and runs; null when it isn't theirs or isn't there. */
export async function loadChatThread(conversationId: string): Promise<ChatThread | null> {
const supabase = await createClient();
const { data: chat, error } = await supabase
.from('chat_conversations')
.select('id, title, brand_id')
.eq('id', conversationId)
.maybeSingle();
if (error) throw new Error(`Could not read the chat: ${error.message}`);
if (!chat) return null;
const [messages, runs] = await Promise.all([
supabase
.from('chat_messages')
.select('id, role, content_md, generated_at, data_as_of, timezone, created_at')
.eq('conversation_id', conversationId)
.order('created_at', { ascending: true }),
supabase
.from('chat_runs')
.select('id, message_id, status')
.eq('conversation_id', conversationId),
]);
if (messages.error) throw new Error(`Could not read the chat: ${messages.error.message}`);
if (runs.error) throw new Error(`Could not read the chat's runs: ${runs.error.message}`);
const runIds = (runs.data ?? []).map((run) => run.id);
const steps = runIds.length
? await supabase
.from('chat_run_steps')
.select(CHAT_RUN_STEP_SELECT)
.in('run_id', runIds)
.order('position', { ascending: true })
: { data: [], error: null };
if (steps.error) throw new Error(`Could not read the chat's steps: ${steps.error.message}`);
const stepsByRun = new Map<string, ChatStep[]>();
for (const row of (steps.data ?? []) as unknown as ChatRunStepMemberRow[]) {
const step = toClientStep(row);
if (step) stepsByRun.set(row.run_id, [...(stepsByRun.get(row.run_id) ?? []), step]);
}
const runByMessage = new Map(
(runs.data ?? []).map((run) => [
run.message_id,
{ runId: run.id, status: run.status, steps: stepsByRun.get(run.id) ?? [] },
]),
);
return {
conversationId: chat.id,
title: chat.title,
brandId: chat.brand_id,
messages: (messages.data ?? []).map((message) => ({
role: message.role as 'user' | 'assistant',
text: message.content_md,
generatedAt: message.generated_at,
dataAsOf: message.data_as_of,
timezone: message.timezone,
run: runByMessage.get(message.id) ?? null,
})),
};
}