Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 3 additions & 12 deletions src/ai/conversation.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { getSaver } from "#chaincraft/ai/memory/checkpoint-memory.js";
import { listThreadIds } from "#chaincraft/ai/memory/checkpoint-memory.js";

// Map of conversation IDs by graph type
const conversationIds = new Map<string, Set<string>>();
Expand Down Expand Up @@ -30,16 +30,7 @@ async function bootstrapConversationIds(graphType: string): Promise<void> {
return;
}

const saver = await getSaver('list-conversations', graphType);
const ids = new Set<string>();

for await (const checkpoint of saver.list({}, {})) {
const threadId = checkpoint.config?.configurable?.thread_id;
if (threadId) {
ids.add(threadId);
}
}

conversationIds.set(graphType, ids);
const threadIds = await listThreadIds(graphType);
conversationIds.set(graphType, new Set(threadIds));
bootstrappedTypes.add(graphType);
}
2 changes: 2 additions & 0 deletions src/ai/design/design-workflow.ts
Original file line number Diff line number Diff line change
Expand Up @@ -262,6 +262,8 @@ export async function getDesignByVersion(
conversationId: string,
version: number
): Promise<(DesignState | undefined)> {
// TODO - Consider paginating through checkpoints if we expect many versions,
// to avoid excessive memory usage.
// Check if conversation exists
if (!(await isActiveConversation(conversationId))) {
throw new Error(`Conversation ${conversationId} not found`);
Expand Down
34 changes: 34 additions & 0 deletions src/ai/memory/checkpoint-memory.ts
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,40 @@ export async function getSaver(
}
}

/**
* List all thread IDs for a given graph type.
* Uses efficient direct SQL query for PostgreSQL, falls back to checkpoint list for SQLite.
*/
export async function listThreadIds(graphType: string): Promise<string[]> {
await initialize();

const db = await getOrCreateDatabase(graphType);
const ids = new Set<string>();

if (db.backend === "postgres" && db.sharedSaver) {
// For PostgreSQL: query distinct thread_ids directly (much more efficient than loading checkpoints)
const postgresaver = db.sharedSaver as PostgresSaver;
const pool = (postgresaver as any).pool as Pool;
const result = await pool.query(
'SELECT DISTINCT thread_id FROM checkpoints ORDER BY thread_id'
);
for (const row of result.rows) {
ids.add(row.thread_id);
}
} else {
// For SQLite: use limited checkpoint list
const saver = await getSaver('list-threads', graphType);
for await (const checkpoint of saver.list({}, { limit: 100 })) {
const threadId = checkpoint.config?.configurable?.thread_id;
if (threadId) {
ids.add(threadId);
}
}
}

return Array.from(ids).sort();
}

export async function deleteThread(
threadId: string,
graphType: string
Expand Down