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
8 changes: 8 additions & 0 deletions .changeset/icy-heads-invent.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
---
"@voltagent/cloudflare-d1": minor
"@voltagent/postgres": minor
"@voltagent/supabase": minor
"@voltagent/libsql": minor
---

The SQL statement has been modified. Previously, the query returned the earliest messages instead of the most recent ones.
9 changes: 6 additions & 3 deletions packages/cloudflare-d1/src/memory-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -615,8 +615,9 @@ export class D1MemoryAdapter implements StorageAdapter {
const messagesTable = `${this.tablePrefix}_messages`;
const { limit, before, after, roles } = options || {};

let sql = `SELECT * FROM ${messagesTable}
WHERE conversation_id = ? AND user_id = ?`;
let sql = `SELECT * FROM (
SELECT * FROM ${messagesTable}
WHERE conversation_id = ? AND user_id = ?`;
const args: unknown[] = [conversationId, userId];

if (roles && roles.length > 0) {
Expand All @@ -635,12 +636,14 @@ export class D1MemoryAdapter implements StorageAdapter {
args.push(after.toISOString());
}

sql += " ORDER BY created_at ASC";
sql += " ORDER BY created_at DESC";
if (limit && limit > 0) {
sql += " LIMIT ?";
args.push(limit);
}

sql += " ) AS subq ORDER BY created_at ASC";

const rows = await this.all<D1Row>(sql, args);

return rows.map((row) => {
Expand Down
12 changes: 9 additions & 3 deletions packages/libsql/src/memory-core.ts
Original file line number Diff line number Diff line change
Expand Up @@ -573,8 +573,12 @@ export class LibSQLMemoryCore implements StorageAdapter {
const messagesTable = `${this.tablePrefix}_messages`;
const { limit, before, after, roles } = options || {};

let sql = `SELECT * FROM ${messagesTable}
WHERE conversation_id = ? AND user_id = ?`;
let sql = `
SELECT * FROM (
SELECT *
FROM ${messagesTable}
WHERE conversation_id = ? AND user_id = ?
`;
const args: any[] = [conversationId, userId];

if (roles && roles.length > 0) {
Expand All @@ -593,12 +597,14 @@ export class LibSQLMemoryCore implements StorageAdapter {
args.push(after.toISOString());
}

sql += " ORDER BY created_at ASC";
sql += " ORDER BY created_at DESC";
if (limit && limit > 0) {
sql += " LIMIT ?";
args.push(limit);
}

sql += " ) AS subq ORDER BY created_at ASC";

const result = await this.client.execute({ sql, args });

return result.rows.map((row) => {
Expand Down
9 changes: 6 additions & 3 deletions packages/postgres/src/memory-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -515,8 +515,9 @@ export class PostgreSQLMemoryAdapter implements StorageAdapter {
});

// Build query with filters - use SELECT * to handle both old and new schemas safely
let sql = `SELECT * FROM ${messagesTable}
WHERE conversation_id = $1 AND user_id = $2`;
let sql = `SELECT * FROM (
SELECT * FROM ${messagesTable}
WHERE conversation_id = $1 AND user_id = $2`;
const params: any[] = [conversationId, userId];
let paramCount = 3;

Expand Down Expand Up @@ -551,12 +552,14 @@ export class PostgreSQLMemoryAdapter implements StorageAdapter {
}

// Order by creation time and apply limit
sql += " ORDER BY created_at ASC";
sql += " ORDER BY created_at DESC";
if (limit && limit > 0) {
sql += ` LIMIT $${paramCount}`;
params.push(limit);
}

sql += " ) AS subq ORDER BY created_at ASC";

// Debug: Final SQL and parameters
this.log("Final SQL query:", sql);
this.log("Query parameters:", params);
Expand Down
4 changes: 2 additions & 2 deletions packages/supabase/src/memory-adapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -619,7 +619,7 @@ END OF MIGRATION SQL
}

// Order by creation time and apply limit
query = query.order("created_at", { ascending: true });
query = query.order("created_at", { ascending: false });
if (limit && limit > 0) {
query = query.limit(limit);
}
Expand All @@ -631,7 +631,7 @@ END OF MIGRATION SQL
}

// Convert to UIMessages with on-the-fly migration for old format
return (data || []).map((row) => {
return (data || []).reverse().map((row) => {
// Determine parts based on whether we have new format (parts) or old format (content)
let parts: any;

Expand Down
Loading