Skip to content
Open
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
7 changes: 7 additions & 0 deletions .changeset/d1-message-order.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
---
"@voltagent/cloudflare-d1": patch
---

fix(cloudflare-d1): keep insertion order for messages that share a timestamp

`addMessages` stores one `created_at` for the whole batch, and `getMessages` ordered by `created_at` alone. With `limit`, the newest messages of the last batch could be dropped in favour of older ones from the same batch. Ties are now broken by insertion order (`rowid`).
1 change: 1 addition & 0 deletions packages/cloudflare-d1/package.json
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
"@voltagent/internal": "^1.0.2"
},
"devDependencies": {
"@libsql/client": "^0.15.0",
"@vitest/coverage-v8": "^3.2.4",
"@voltagent/core": "^2.4.4",
"@voltagent/logger": "^2.0.2",
Expand Down
100 changes: 100 additions & 0 deletions packages/cloudflare-d1/src/memory-adapter.sqlite.spec.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,100 @@
import type { D1Database } from "@cloudflare/workers-types";
import { type InStatement, createClient } from "@libsql/client";
import type { UIMessage } from "ai";
import { describe, expect, it } from "vitest";
import { D1MemoryAdapter } from "./memory-adapter";

/**
* The part of a D1 binding the adapter uses, backed by a real in-memory SQLite
* database (D1 is SQLite), so the adapter's SQL is actually run.
*/
function createSqliteBinding(): D1Database {
const db = createClient({ url: ":memory:" });

const rowsOf = async (stmt: InStatement) => {
const { columns, rows } = await db.execute(stmt);
return rows.map((row) => Object.fromEntries(columns.map((c, i) => [c, row[i]])));
};

const statement = (sql: string, params: unknown[] = []) => {
const stmt = { sql, args: params } as InStatement;
return {
stmt,
bind: (...args: unknown[]) => statement(sql, args),
async run() {
await db.execute(stmt);
return { success: true, meta: {} };
},
async all() {
return { results: await rowsOf(stmt), success: true, meta: {} };
},
async first() {
return (await rowsOf(stmt))[0] ?? null;
},
};
};

return {
prepare: (sql: string) => statement(sql),
async batch(statements: Array<ReturnType<typeof statement>>) {
await db.batch(
statements.map((s) => s.stmt),
"write",
);
return [];
},
} as unknown as D1Database;
}

const text = (id: string, role: UIMessage["role"], value: string): UIMessage => ({
id,
role,
parts: [{ type: "text", text: value }],
});

const texts = (messages: UIMessage[]) => messages.map((m) => (m.parts[0] as { text: string }).text);

describe("D1MemoryAdapter getMessages order", () => {
async function adapterWithConversation() {
const adapter = new D1MemoryAdapter({ binding: createSqliteBinding(), tablePrefix: "t" });
await adapter.createConversation({
id: "c1",
resourceId: "agent",
userId: "u1",
title: "t",
metadata: {},
});
return adapter;
}

// One turn saved with addMessages shares a single created_at. The ids are
// deliberately not in alphabetical order.
const turn = [
text("q7", "user", "1 what is the weather?"),
text("c2", "assistant", "2 calling the weather tool"),
text("x9", "assistant", "3 tool result: sunny"),
text("a1", "assistant", "4 it is sunny"),
];

it("keeps insertion order for messages saved together", async () => {
const adapter = await adapterWithConversation();
await adapter.addMessages(turn, "u1", "c1");

expect(texts(await adapter.getMessages("u1", "c1"))).toEqual([
"1 what is the weather?",
"2 calling the weather tool",
"3 tool result: sunny",
"4 it is sunny",
]);
});

it("returns the newest messages when limit cuts into one batch", async () => {
const adapter = await adapterWithConversation();
await adapter.addMessages(turn, "u1", "c1");

expect(texts(await adapter.getMessages("u1", "c1", { limit: 2 }))).toEqual([
"3 tool result: sunny",
"4 it is sunny",
]);
});
});
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 @@ -646,8 +646,11 @@ export class D1MemoryAdapter implements StorageAdapter {
const messagesTable = `${this.tablePrefix}_messages`;
const { limit, before, after, roles } = options || {};

// Messages saved together share one created_at, so rowid (insertion order)
// breaks ties: without it, `limit` can keep older messages of the newest
// batch and drop the latest ones.
let sql = `SELECT * FROM (
SELECT * FROM ${messagesTable}
SELECT *, rowid AS _insert_order FROM ${messagesTable}
WHERE conversation_id = ? AND user_id = ?`;
const args: unknown[] = [conversationId, userId];

Expand All @@ -667,13 +670,13 @@ export class D1MemoryAdapter implements StorageAdapter {
args.push(after.toISOString());
}

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

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

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

Expand Down
3 changes: 3 additions & 0 deletions pnpm-lock.yaml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.