@kernl-sdk/libsql 0.1.38 → 0.1.39
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/.turbo/turbo-build.log +5 -4
- package/CHANGELOG.md +8 -0
- package/README.md +225 -0
- package/dist/__tests__/constraints.test.d.ts +2 -0
- package/dist/__tests__/constraints.test.d.ts.map +1 -0
- package/dist/__tests__/constraints.test.js +97 -0
- package/dist/__tests__/helpers.d.ts +36 -0
- package/dist/__tests__/helpers.d.ts.map +1 -0
- package/dist/__tests__/helpers.js +80 -0
- package/dist/__tests__/memory.create-get.test.d.ts +2 -0
- package/dist/__tests__/memory.create-get.test.d.ts.map +1 -0
- package/dist/__tests__/memory.create-get.test.js +8 -0
- package/dist/__tests__/memory.delete.test.d.ts +2 -0
- package/dist/__tests__/memory.delete.test.d.ts.map +1 -0
- package/dist/__tests__/memory.delete.test.js +6 -0
- package/dist/__tests__/memory.list.test.d.ts +2 -0
- package/dist/__tests__/memory.list.test.d.ts.map +1 -0
- package/dist/__tests__/memory.list.test.js +8 -0
- package/dist/__tests__/memory.update.test.d.ts +2 -0
- package/dist/__tests__/memory.update.test.d.ts.map +1 -0
- package/dist/__tests__/memory.update.test.js +8 -0
- package/dist/__tests__/migrations.test.d.ts +2 -0
- package/dist/__tests__/migrations.test.d.ts.map +1 -0
- package/dist/__tests__/migrations.test.js +68 -0
- package/dist/__tests__/row-codecs.test.d.ts +2 -0
- package/dist/__tests__/row-codecs.test.d.ts.map +1 -0
- package/dist/__tests__/row-codecs.test.js +175 -0
- package/dist/__tests__/sql-utils.test.d.ts +2 -0
- package/dist/__tests__/sql-utils.test.d.ts.map +1 -0
- package/dist/__tests__/sql-utils.test.js +45 -0
- package/dist/__tests__/storage.init.test.d.ts +2 -0
- package/dist/__tests__/storage.init.test.d.ts.map +1 -0
- package/dist/__tests__/storage.init.test.js +63 -0
- package/dist/__tests__/thread.lifecycle.test.d.ts +2 -0
- package/dist/__tests__/thread.lifecycle.test.d.ts.map +1 -0
- package/dist/__tests__/thread.lifecycle.test.js +172 -0
- package/dist/__tests__/transaction.test.d.ts +2 -0
- package/dist/__tests__/transaction.test.d.ts.map +1 -0
- package/dist/__tests__/transaction.test.js +16 -0
- package/dist/__tests__/utils.test.d.ts +2 -0
- package/dist/__tests__/utils.test.d.ts.map +1 -0
- package/dist/__tests__/utils.test.js +31 -0
- package/dist/client.d.ts +46 -0
- package/dist/client.d.ts.map +1 -0
- package/dist/client.js +46 -0
- package/dist/index.d.ts +5 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +5 -1
- package/dist/memory/__tests__/create-get.test.d.ts +2 -0
- package/dist/memory/__tests__/create-get.test.d.ts.map +1 -0
- package/dist/memory/__tests__/create-get.test.js +126 -0
- package/dist/memory/__tests__/delete.test.d.ts +2 -0
- package/dist/memory/__tests__/delete.test.d.ts.map +1 -0
- package/dist/memory/__tests__/delete.test.js +96 -0
- package/dist/memory/__tests__/list.test.d.ts +2 -0
- package/dist/memory/__tests__/list.test.d.ts.map +1 -0
- package/dist/memory/__tests__/list.test.js +168 -0
- package/dist/memory/__tests__/sql.test.d.ts +2 -0
- package/dist/memory/__tests__/sql.test.d.ts.map +1 -0
- package/dist/memory/__tests__/sql.test.js +159 -0
- package/dist/memory/__tests__/update.test.d.ts +2 -0
- package/dist/memory/__tests__/update.test.d.ts.map +1 -0
- package/dist/memory/__tests__/update.test.js +113 -0
- package/dist/memory/row.d.ts +11 -0
- package/dist/memory/row.d.ts.map +1 -0
- package/dist/memory/row.js +29 -0
- package/dist/memory/sql.d.ts +34 -0
- package/dist/memory/sql.d.ts.map +1 -0
- package/dist/memory/sql.js +109 -0
- package/dist/memory/store.d.ts +41 -0
- package/dist/memory/store.d.ts.map +1 -0
- package/dist/memory/store.js +132 -0
- package/dist/migrations.d.ts +32 -0
- package/dist/migrations.d.ts.map +1 -0
- package/dist/migrations.js +157 -0
- package/dist/sql.d.ts +28 -0
- package/dist/sql.d.ts.map +1 -0
- package/dist/sql.js +22 -0
- package/dist/storage.d.ts +75 -0
- package/dist/storage.d.ts.map +1 -0
- package/dist/storage.js +123 -0
- package/dist/thread/__tests__/append.test.d.ts +2 -0
- package/dist/thread/__tests__/append.test.d.ts.map +1 -0
- package/dist/thread/__tests__/append.test.js +141 -0
- package/dist/thread/__tests__/history.test.d.ts +2 -0
- package/dist/thread/__tests__/history.test.d.ts.map +1 -0
- package/dist/thread/__tests__/history.test.js +146 -0
- package/dist/thread/__tests__/sql.test.d.ts +2 -0
- package/dist/thread/__tests__/sql.test.d.ts.map +1 -0
- package/dist/thread/__tests__/sql.test.js +129 -0
- package/dist/thread/__tests__/store.test.d.ts +2 -0
- package/dist/thread/__tests__/store.test.d.ts.map +1 -0
- package/dist/thread/__tests__/store.test.js +170 -0
- package/dist/thread/row.d.ts +19 -0
- package/dist/thread/row.d.ts.map +1 -0
- package/dist/thread/row.js +65 -0
- package/dist/thread/sql.d.ts +33 -0
- package/dist/thread/sql.d.ts.map +1 -0
- package/dist/thread/sql.js +112 -0
- package/dist/thread/store.d.ts +67 -0
- package/dist/thread/store.d.ts.map +1 -0
- package/dist/thread/store.js +282 -0
- package/dist/utils.d.ts +10 -0
- package/dist/utils.d.ts.map +1 -0
- package/dist/utils.js +21 -0
- package/package.json +15 -11
- package/src/__tests__/constraints.test.ts +123 -0
- package/src/__tests__/helpers.ts +98 -0
- package/src/__tests__/migrations.test.ts +114 -0
- package/src/__tests__/row-codecs.test.ts +201 -0
- package/src/__tests__/sql-utils.test.ts +52 -0
- package/src/__tests__/storage.init.test.ts +92 -0
- package/src/__tests__/thread.lifecycle.test.ts +234 -0
- package/src/__tests__/transaction.test.ts +25 -0
- package/src/__tests__/utils.test.ts +38 -0
- package/src/client.ts +71 -0
- package/src/index.ts +10 -0
- package/src/memory/__tests__/create-get.test.ts +161 -0
- package/src/memory/__tests__/delete.test.ts +124 -0
- package/src/memory/__tests__/list.test.ts +198 -0
- package/src/memory/__tests__/sql.test.ts +186 -0
- package/src/memory/__tests__/update.test.ts +148 -0
- package/src/memory/row.ts +36 -0
- package/src/memory/sql.ts +142 -0
- package/src/memory/store.ts +173 -0
- package/src/migrations.ts +206 -0
- package/src/sql.ts +35 -0
- package/src/storage.ts +170 -0
- package/src/thread/__tests__/append.test.ts +201 -0
- package/src/thread/__tests__/history.test.ts +198 -0
- package/src/thread/__tests__/sql.test.ts +154 -0
- package/src/thread/__tests__/store.test.ts +219 -0
- package/src/thread/row.ts +77 -0
- package/src/thread/sql.ts +153 -0
- package/src/thread/store.ts +381 -0
- package/src/utils.ts +20 -0
- package/LICENSE +0 -201
package/.turbo/turbo-build.log
CHANGED
|
@@ -1,4 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
>
|
|
4
|
-
|
|
1
|
+
|
|
2
|
+
|
|
3
|
+
> @kernl-sdk/libsql@0.1.39 build /Users/andjones/Documents/projects/kernl/packages/storage/libsql
|
|
4
|
+
> tsc && tsc-alias --resolve-full-paths
|
|
5
|
+
|
package/CHANGELOG.md
CHANGED
package/README.md
ADDED
|
@@ -0,0 +1,225 @@
|
|
|
1
|
+
# @kernl-sdk/libsql
|
|
2
|
+
|
|
3
|
+
LibSQL/SQLite storage adapter for Kernl. Supports local SQLite files, in-memory databases, and remote [Turso](https://turso.tech) databases.
|
|
4
|
+
|
|
5
|
+
## Installation
|
|
6
|
+
|
|
7
|
+
```bash
|
|
8
|
+
pnpm add @kernl-sdk/libsql
|
|
9
|
+
```
|
|
10
|
+
|
|
11
|
+
## Usage
|
|
12
|
+
|
|
13
|
+
### Local SQLite File
|
|
14
|
+
|
|
15
|
+
```typescript
|
|
16
|
+
import { Kernl } from "kernl";
|
|
17
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
18
|
+
|
|
19
|
+
const kernl = new Kernl({
|
|
20
|
+
storage: libsql({ url: "file:./kernl.db" }),
|
|
21
|
+
});
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
### In-Memory (Testing)
|
|
25
|
+
|
|
26
|
+
```typescript
|
|
27
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
28
|
+
|
|
29
|
+
const storage = libsql({ url: ":memory:" });
|
|
30
|
+
```
|
|
31
|
+
|
|
32
|
+
### Remote Turso
|
|
33
|
+
|
|
34
|
+
```typescript
|
|
35
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
36
|
+
|
|
37
|
+
const storage = libsql({
|
|
38
|
+
url: "libsql://your-database.turso.io",
|
|
39
|
+
authToken: process.env.TURSO_AUTH_TOKEN,
|
|
40
|
+
});
|
|
41
|
+
```
|
|
42
|
+
|
|
43
|
+
### Using an Existing Client
|
|
44
|
+
|
|
45
|
+
```typescript
|
|
46
|
+
import { createClient } from "@libsql/client";
|
|
47
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
48
|
+
|
|
49
|
+
const client = createClient({ url: "file:./kernl.db" });
|
|
50
|
+
const storage = libsql({ client });
|
|
51
|
+
```
|
|
52
|
+
|
|
53
|
+
## Direct Usage
|
|
54
|
+
|
|
55
|
+
You can use the thread and memory stores directly for lower-level operations.
|
|
56
|
+
|
|
57
|
+
### Threads
|
|
58
|
+
|
|
59
|
+
```typescript
|
|
60
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
61
|
+
|
|
62
|
+
const storage = libsql({ url: "file:./kernl.db" });
|
|
63
|
+
|
|
64
|
+
// Create a thread
|
|
65
|
+
const thread = await storage.threads.insert({
|
|
66
|
+
id: "thread-123",
|
|
67
|
+
namespace: "default",
|
|
68
|
+
agentId: "my-agent",
|
|
69
|
+
model: "openai/gpt-4",
|
|
70
|
+
context: { userId: "user-456" },
|
|
71
|
+
metadata: { title: "Support Chat" },
|
|
72
|
+
});
|
|
73
|
+
|
|
74
|
+
// Get a thread by ID
|
|
75
|
+
const found = await storage.threads.get("thread-123");
|
|
76
|
+
|
|
77
|
+
// Get thread with event history
|
|
78
|
+
const withHistory = await storage.threads.get("thread-123", {
|
|
79
|
+
history: true,
|
|
80
|
+
});
|
|
81
|
+
|
|
82
|
+
// Get thread with filtered history
|
|
83
|
+
const withFilteredHistory = await storage.threads.get("thread-123", {
|
|
84
|
+
history: { after: 5, kinds: ["message"], limit: 10, order: "desc" },
|
|
85
|
+
});
|
|
86
|
+
|
|
87
|
+
// List threads with filters
|
|
88
|
+
const threads = await storage.threads.list({
|
|
89
|
+
filter: { namespace: "default", agentId: "my-agent", state: "idle" },
|
|
90
|
+
order: { createdAt: "desc" },
|
|
91
|
+
limit: 20,
|
|
92
|
+
});
|
|
93
|
+
|
|
94
|
+
// Update a thread
|
|
95
|
+
const updated = await storage.threads.update("thread-123", {
|
|
96
|
+
state: "running",
|
|
97
|
+
metadata: { title: "Updated Title" },
|
|
98
|
+
});
|
|
99
|
+
|
|
100
|
+
// Get event history separately
|
|
101
|
+
const events = await storage.threads.history("thread-123", {
|
|
102
|
+
after: 0,
|
|
103
|
+
kinds: ["message", "tool_call"],
|
|
104
|
+
limit: 50,
|
|
105
|
+
order: "asc",
|
|
106
|
+
});
|
|
107
|
+
|
|
108
|
+
// Delete a thread (cascades to events)
|
|
109
|
+
await storage.threads.delete("thread-123");
|
|
110
|
+
```
|
|
111
|
+
|
|
112
|
+
### Memories
|
|
113
|
+
|
|
114
|
+
```typescript
|
|
115
|
+
import { libsql } from "@kernl-sdk/libsql";
|
|
116
|
+
|
|
117
|
+
const storage = libsql({ url: "file:./kernl.db" });
|
|
118
|
+
|
|
119
|
+
// Create a memory
|
|
120
|
+
const memory = await storage.memories.create({
|
|
121
|
+
id: "mem-789",
|
|
122
|
+
scope: { namespace: "default", entityId: "user-456", agentId: "my-agent" },
|
|
123
|
+
kind: "semantic",
|
|
124
|
+
collection: "facts",
|
|
125
|
+
content: { text: "User prefers dark mode" },
|
|
126
|
+
wmem: true,
|
|
127
|
+
metadata: { source: "preference" },
|
|
128
|
+
});
|
|
129
|
+
|
|
130
|
+
// Get a memory by ID
|
|
131
|
+
const found = await storage.memories.get("mem-789");
|
|
132
|
+
|
|
133
|
+
// List memories with filters
|
|
134
|
+
const memories = await storage.memories.list({
|
|
135
|
+
filter: {
|
|
136
|
+
scope: { namespace: "default", entityId: "user-456" },
|
|
137
|
+
collections: ["facts", "preferences"],
|
|
138
|
+
wmem: true,
|
|
139
|
+
},
|
|
140
|
+
order: "desc",
|
|
141
|
+
limit: 100,
|
|
142
|
+
});
|
|
143
|
+
|
|
144
|
+
// Update a memory
|
|
145
|
+
const updated = await storage.memories.update("mem-789", {
|
|
146
|
+
content: { text: "User prefers light mode" },
|
|
147
|
+
metadata: { source: "updated-preference" },
|
|
148
|
+
});
|
|
149
|
+
|
|
150
|
+
// Delete a memory
|
|
151
|
+
await storage.memories.delete("mem-789");
|
|
152
|
+
|
|
153
|
+
// Delete multiple memories
|
|
154
|
+
await storage.memories.mdelete(["mem-789", "mem-790", "mem-791"]);
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
## API
|
|
158
|
+
|
|
159
|
+
### `libsql(config)`
|
|
160
|
+
|
|
161
|
+
Creates a LibSQL storage adapter.
|
|
162
|
+
|
|
163
|
+
**Config options:**
|
|
164
|
+
|
|
165
|
+
| Option | Type | Description |
|
|
166
|
+
|--------|------|-------------|
|
|
167
|
+
| `url` | `string` | Database URL. Supports `file:`, `:memory:`, or `libsql://` |
|
|
168
|
+
| `authToken` | `string` | Auth token for Turso (optional for local) |
|
|
169
|
+
| `client` | `Client` | Existing `@libsql/client` instance |
|
|
170
|
+
|
|
171
|
+
## Store Methods
|
|
172
|
+
|
|
173
|
+
### ThreadStore
|
|
174
|
+
|
|
175
|
+
| Method | Description |
|
|
176
|
+
|--------|-------------|
|
|
177
|
+
| `get(tid, include?)` | Get a thread by ID, optionally with event history |
|
|
178
|
+
| `list(options?)` | List threads with filtering, ordering, and pagination |
|
|
179
|
+
| `insert(thread)` | Create a new thread |
|
|
180
|
+
| `update(tid, patch)` | Update thread state, context, or metadata |
|
|
181
|
+
| `delete(tid)` | Delete a thread and its events |
|
|
182
|
+
| `history(tid, options?)` | Get event history for a thread |
|
|
183
|
+
| `append(events)` | Append events to thread history (idempotent) |
|
|
184
|
+
|
|
185
|
+
### MemoryStore
|
|
186
|
+
|
|
187
|
+
| Method | Description |
|
|
188
|
+
|--------|-------------|
|
|
189
|
+
| `get(id)` | Get a memory by ID |
|
|
190
|
+
| `list(options?)` | List memories with filtering and pagination |
|
|
191
|
+
| `create(memory)` | Create a new memory record |
|
|
192
|
+
| `update(id, patch)` | Update a memory record |
|
|
193
|
+
| `delete(id)` | Delete a memory by ID |
|
|
194
|
+
| `mdelete(ids)` | Delete multiple memories by ID |
|
|
195
|
+
|
|
196
|
+
## Differences from PostgreSQL
|
|
197
|
+
|
|
198
|
+
This adapter provides feature parity with `@kernl-sdk/pg` with the following SQLite-specific adaptations:
|
|
199
|
+
|
|
200
|
+
- **JSON storage**: Uses `TEXT` columns instead of `JSONB`
|
|
201
|
+
- **Booleans**: Stored as `INTEGER` (0/1)
|
|
202
|
+
- **Arrays**: `IN (?, ?, ?)` instead of PostgreSQL's `= ANY($1)`
|
|
203
|
+
- **Schema**: Table names prefixed with `kernl_` (SQLite has no schema support)
|
|
204
|
+
|
|
205
|
+
## SQLite Configuration
|
|
206
|
+
|
|
207
|
+
For local file databases (`file:` URLs), the adapter automatically sets:
|
|
208
|
+
|
|
209
|
+
- `PRAGMA journal_mode = WAL` - Write-Ahead Logging for better concurrent read/write performance
|
|
210
|
+
- `PRAGMA busy_timeout = 5000` - Wait up to 5 seconds when the database is locked
|
|
211
|
+
|
|
212
|
+
These are not set for remote Turso databases, which manage these settings automatically.
|
|
213
|
+
|
|
214
|
+
## Tables
|
|
215
|
+
|
|
216
|
+
The adapter creates the following tables:
|
|
217
|
+
|
|
218
|
+
- `kernl_migrations` - Migration tracking
|
|
219
|
+
- `kernl_threads` - Thread records
|
|
220
|
+
- `kernl_thread_events` - Thread event history
|
|
221
|
+
- `kernl_memories` - Memory records
|
|
222
|
+
|
|
223
|
+
## License
|
|
224
|
+
|
|
225
|
+
MIT
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"constraints.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/constraints.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,97 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { create_client, create_storage, enable_foreign_keys, THREADS_TABLE, THREAD_EVENTS_TABLE, testid, } from "./helpers.js";
|
|
3
|
+
describe("LibSQL constraints", () => {
|
|
4
|
+
let client;
|
|
5
|
+
let storage;
|
|
6
|
+
beforeEach(async () => {
|
|
7
|
+
client = create_client();
|
|
8
|
+
await enable_foreign_keys(client);
|
|
9
|
+
storage = create_storage(client);
|
|
10
|
+
// Initialize tables
|
|
11
|
+
await storage.memories.list();
|
|
12
|
+
});
|
|
13
|
+
afterEach(() => {
|
|
14
|
+
client.close();
|
|
15
|
+
});
|
|
16
|
+
it("enforces thread_events foreign key to threads", async () => {
|
|
17
|
+
// Try to insert event for non-existent thread
|
|
18
|
+
const result = client.execute({
|
|
19
|
+
sql: `INSERT INTO "${THREAD_EVENTS_TABLE}" (id, tid, seq, kind, timestamp) VALUES (?, ?, ?, ?, ?)`,
|
|
20
|
+
args: ["evt-1", "nonexistent-thread", 1, "message", Date.now()],
|
|
21
|
+
});
|
|
22
|
+
await expect(result).rejects.toThrow(/FOREIGN KEY/i);
|
|
23
|
+
});
|
|
24
|
+
it("cascades thread deletion to events", async () => {
|
|
25
|
+
const tid = testid("thread");
|
|
26
|
+
// Insert a thread directly
|
|
27
|
+
await client.execute({
|
|
28
|
+
sql: `INSERT INTO "${THREADS_TABLE}" (id, namespace, agent_id, model, context, tick, state, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
29
|
+
args: [
|
|
30
|
+
tid,
|
|
31
|
+
"default",
|
|
32
|
+
"agent-1",
|
|
33
|
+
"test/model",
|
|
34
|
+
"{}",
|
|
35
|
+
0,
|
|
36
|
+
"idle",
|
|
37
|
+
Date.now(),
|
|
38
|
+
Date.now(),
|
|
39
|
+
],
|
|
40
|
+
});
|
|
41
|
+
// Insert events for the thread
|
|
42
|
+
await client.execute({
|
|
43
|
+
sql: `INSERT INTO "${THREAD_EVENTS_TABLE}" (id, tid, seq, kind, timestamp) VALUES (?, ?, ?, ?, ?)`,
|
|
44
|
+
args: ["evt-1", tid, 1, "message", Date.now()],
|
|
45
|
+
});
|
|
46
|
+
await client.execute({
|
|
47
|
+
sql: `INSERT INTO "${THREAD_EVENTS_TABLE}" (id, tid, seq, kind, timestamp) VALUES (?, ?, ?, ?, ?)`,
|
|
48
|
+
args: ["evt-2", tid, 2, "message", Date.now()],
|
|
49
|
+
});
|
|
50
|
+
// Verify events exist
|
|
51
|
+
const beforeDelete = await client.execute({
|
|
52
|
+
sql: `SELECT COUNT(*) as count FROM "${THREAD_EVENTS_TABLE}" WHERE tid = ?`,
|
|
53
|
+
args: [tid],
|
|
54
|
+
});
|
|
55
|
+
expect(beforeDelete.rows[0].count).toBe(2);
|
|
56
|
+
// Delete thread
|
|
57
|
+
await client.execute({
|
|
58
|
+
sql: `DELETE FROM "${THREADS_TABLE}" WHERE id = ?`,
|
|
59
|
+
args: [tid],
|
|
60
|
+
});
|
|
61
|
+
// Events should be cascaded
|
|
62
|
+
const afterDelete = await client.execute({
|
|
63
|
+
sql: `SELECT COUNT(*) as count FROM "${THREAD_EVENTS_TABLE}" WHERE tid = ?`,
|
|
64
|
+
args: [tid],
|
|
65
|
+
});
|
|
66
|
+
expect(afterDelete.rows[0].count).toBe(0);
|
|
67
|
+
});
|
|
68
|
+
it("enforces unique (tid, id) on thread_events", async () => {
|
|
69
|
+
const tid = testid("thread");
|
|
70
|
+
// Insert a thread
|
|
71
|
+
await client.execute({
|
|
72
|
+
sql: `INSERT INTO "${THREADS_TABLE}" (id, namespace, agent_id, model, context, tick, state, created_at, updated_at) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?)`,
|
|
73
|
+
args: [
|
|
74
|
+
tid,
|
|
75
|
+
"default",
|
|
76
|
+
"agent-1",
|
|
77
|
+
"test/model",
|
|
78
|
+
"{}",
|
|
79
|
+
0,
|
|
80
|
+
"idle",
|
|
81
|
+
Date.now(),
|
|
82
|
+
Date.now(),
|
|
83
|
+
],
|
|
84
|
+
});
|
|
85
|
+
// Insert an event
|
|
86
|
+
await client.execute({
|
|
87
|
+
sql: `INSERT INTO "${THREAD_EVENTS_TABLE}" (id, tid, seq, kind, timestamp) VALUES (?, ?, ?, ?, ?)`,
|
|
88
|
+
args: ["evt-1", tid, 1, "message", Date.now()],
|
|
89
|
+
});
|
|
90
|
+
// Try to insert duplicate (tid, id) - should fail
|
|
91
|
+
const duplicate = client.execute({
|
|
92
|
+
sql: `INSERT INTO "${THREAD_EVENTS_TABLE}" (id, tid, seq, kind, timestamp) VALUES (?, ?, ?, ?, ?)`,
|
|
93
|
+
args: ["evt-1", tid, 2, "message", Date.now()],
|
|
94
|
+
});
|
|
95
|
+
await expect(duplicate).rejects.toThrow(/UNIQUE/i);
|
|
96
|
+
});
|
|
97
|
+
});
|
|
@@ -0,0 +1,36 @@
|
|
|
1
|
+
import { type Client } from "@libsql/client";
|
|
2
|
+
import type { IAgentRegistry, IModelRegistry } from "kernl";
|
|
3
|
+
import { LibSQLStorage } from "../storage.js";
|
|
4
|
+
/**
|
|
5
|
+
* Generate a unique database URL for each test.
|
|
6
|
+
* Uses temp file instead of :memory: due to libsql bug with in-memory transactions.
|
|
7
|
+
*/
|
|
8
|
+
export declare function test_db_url(): string;
|
|
9
|
+
export declare const THREADS_TABLE = "kernl_threads";
|
|
10
|
+
export declare const THREAD_EVENTS_TABLE = "kernl_thread_events";
|
|
11
|
+
export declare const MEMORIES_TABLE = "kernl_memories";
|
|
12
|
+
export declare const MIGRATIONS_TABLE = "kernl_migrations";
|
|
13
|
+
/**
|
|
14
|
+
* Create a test database setup with client and storage sharing the same URL.
|
|
15
|
+
*/
|
|
16
|
+
export declare function create_test_db(): {
|
|
17
|
+
client: Client;
|
|
18
|
+
storage: LibSQLStorage;
|
|
19
|
+
url: string;
|
|
20
|
+
};
|
|
21
|
+
export declare function create_client(url?: string): Client;
|
|
22
|
+
export declare function enable_foreign_keys(client: Client): Promise<void>;
|
|
23
|
+
export declare function create_storage(client: Client, url?: string): LibSQLStorage;
|
|
24
|
+
export declare function reset_tables(client: Client): Promise<void>;
|
|
25
|
+
/**
|
|
26
|
+
* Create mock registries for thread hydration tests.
|
|
27
|
+
*/
|
|
28
|
+
export declare function create_mock_registries(): {
|
|
29
|
+
agents: IAgentRegistry;
|
|
30
|
+
models: IModelRegistry;
|
|
31
|
+
};
|
|
32
|
+
/**
|
|
33
|
+
* Generate a unique ID for tests.
|
|
34
|
+
*/
|
|
35
|
+
export declare function testid(prefix?: string): string;
|
|
36
|
+
//# sourceMappingURL=helpers.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"helpers.d.ts","sourceRoot":"","sources":["../../src/__tests__/helpers.ts"],"names":[],"mappings":"AAAA,OAAO,EAAgB,KAAK,MAAM,EAAE,MAAM,gBAAgB,CAAC;AAE3D,OAAO,KAAK,EAAE,cAAc,EAAE,cAAc,EAAE,MAAM,OAAO,CAAC;AAE5D,OAAO,EAAE,aAAa,EAAE,MAAM,YAAY,CAAC;AAK3C;;;GAGG;AACH,wBAAgB,WAAW,IAAI,MAAM,CAEpC;AAED,eAAO,MAAM,aAAa,kBAAiC,CAAC;AAC5D,eAAO,MAAM,mBAAmB,wBAAuC,CAAC;AACxE,eAAO,MAAM,cAAc,mBAAkC,CAAC;AAC9D,eAAO,MAAM,gBAAgB,qBAAoC,CAAC;AAElE;;GAEG;AACH,wBAAgB,cAAc,IAAI;IAAE,MAAM,EAAE,MAAM,CAAC;IAAC,OAAO,EAAE,aAAa,CAAC;IAAC,GAAG,EAAE,MAAM,CAAA;CAAE,CAKxF;AAED,wBAAgB,aAAa,CAAC,GAAG,CAAC,EAAE,MAAM,GAAG,MAAM,CAGlD;AAED,wBAAsB,mBAAmB,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAEvE;AAED,wBAAgB,cAAc,CAAC,MAAM,EAAE,MAAM,EAAE,GAAG,CAAC,EAAE,MAAM,GAAG,aAAa,CAG1E;AAED,wBAAsB,YAAY,CAAC,MAAM,EAAE,MAAM,GAAG,OAAO,CAAC,IAAI,CAAC,CAIhE;AAED;;GAEG;AACH,wBAAgB,sBAAsB,IAAI;IACxC,MAAM,EAAE,cAAc,CAAC;IACvB,MAAM,EAAE,cAAc,CAAC;CACxB,CAgCA;AAED;;GAEG;AACH,wBAAgB,MAAM,CAAC,MAAM,GAAE,MAAe,GAAG,MAAM,CAEtD"}
|
|
@@ -0,0 +1,80 @@
|
|
|
1
|
+
import { createClient } from "@libsql/client";
|
|
2
|
+
import { KERNL_SCHEMA_NAME } from "@kernl-sdk/storage";
|
|
3
|
+
import { LibSQLStorage } from "../storage.js";
|
|
4
|
+
// Counter to ensure unique database URLs within the same process
|
|
5
|
+
let dbCounter = 0;
|
|
6
|
+
/**
|
|
7
|
+
* Generate a unique database URL for each test.
|
|
8
|
+
* Uses temp file instead of :memory: due to libsql bug with in-memory transactions.
|
|
9
|
+
*/
|
|
10
|
+
export function test_db_url() {
|
|
11
|
+
return `file:/tmp/kernl-test-${process.pid}-${Date.now()}-${++dbCounter}.db`;
|
|
12
|
+
}
|
|
13
|
+
export const THREADS_TABLE = `${KERNL_SCHEMA_NAME}_threads`;
|
|
14
|
+
export const THREAD_EVENTS_TABLE = `${KERNL_SCHEMA_NAME}_thread_events`;
|
|
15
|
+
export const MEMORIES_TABLE = `${KERNL_SCHEMA_NAME}_memories`;
|
|
16
|
+
export const MIGRATIONS_TABLE = `${KERNL_SCHEMA_NAME}_migrations`;
|
|
17
|
+
/**
|
|
18
|
+
* Create a test database setup with client and storage sharing the same URL.
|
|
19
|
+
*/
|
|
20
|
+
export function create_test_db() {
|
|
21
|
+
const url = test_db_url();
|
|
22
|
+
const client = createClient({ url });
|
|
23
|
+
const storage = new LibSQLStorage({ client, url });
|
|
24
|
+
return { client, storage, url };
|
|
25
|
+
}
|
|
26
|
+
export function create_client(url) {
|
|
27
|
+
const dbUrl = url ?? test_db_url();
|
|
28
|
+
return createClient({ url: dbUrl });
|
|
29
|
+
}
|
|
30
|
+
export async function enable_foreign_keys(client) {
|
|
31
|
+
await client.execute("PRAGMA foreign_keys = ON");
|
|
32
|
+
}
|
|
33
|
+
export function create_storage(client, url) {
|
|
34
|
+
const dbUrl = url ?? test_db_url();
|
|
35
|
+
return new LibSQLStorage({ client, url: dbUrl });
|
|
36
|
+
}
|
|
37
|
+
export async function reset_tables(client) {
|
|
38
|
+
await client.execute(`DELETE FROM "${THREAD_EVENTS_TABLE}"`);
|
|
39
|
+
await client.execute(`DELETE FROM "${THREADS_TABLE}"`);
|
|
40
|
+
await client.execute(`DELETE FROM "${MEMORIES_TABLE}"`);
|
|
41
|
+
}
|
|
42
|
+
/**
|
|
43
|
+
* Create mock registries for thread hydration tests.
|
|
44
|
+
*/
|
|
45
|
+
export function create_mock_registries() {
|
|
46
|
+
const agentMap = new Map();
|
|
47
|
+
const modelMap = new Map();
|
|
48
|
+
// Add a test agent
|
|
49
|
+
agentMap.set("test-agent", {
|
|
50
|
+
id: "test-agent",
|
|
51
|
+
kind: "llm",
|
|
52
|
+
name: "Test Agent",
|
|
53
|
+
});
|
|
54
|
+
// Add a test model
|
|
55
|
+
modelMap.set("test/model", {
|
|
56
|
+
id: "test/model",
|
|
57
|
+
provider: "test",
|
|
58
|
+
modelId: "model",
|
|
59
|
+
});
|
|
60
|
+
return {
|
|
61
|
+
agents: {
|
|
62
|
+
get: (id) => agentMap.get(id),
|
|
63
|
+
set: (id, agent) => agentMap.set(id, agent),
|
|
64
|
+
has: (id) => agentMap.has(id),
|
|
65
|
+
list: () => Array.from(agentMap.values()),
|
|
66
|
+
},
|
|
67
|
+
models: {
|
|
68
|
+
get: (id) => modelMap.get(id),
|
|
69
|
+
set: (id, model) => modelMap.set(id, model),
|
|
70
|
+
has: (id) => modelMap.has(id),
|
|
71
|
+
list: () => Array.from(modelMap.values()),
|
|
72
|
+
},
|
|
73
|
+
};
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Generate a unique ID for tests.
|
|
77
|
+
*/
|
|
78
|
+
export function testid(prefix = "test") {
|
|
79
|
+
return `${prefix}-${Date.now()}-${Math.random().toString(36).slice(2, 8)}`;
|
|
80
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.create-get.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/memory.create-get.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
describe("LibSQL memory create/get", () => {
|
|
3
|
+
it.todo("creates memory with text content");
|
|
4
|
+
it.todo("creates memory with object content");
|
|
5
|
+
it.todo("creates working memory (wmem=true)");
|
|
6
|
+
it.todo("creates short-term memory with expiration");
|
|
7
|
+
it.todo("gets by id and returns null when missing");
|
|
8
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.delete.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/memory.delete.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.list.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/memory.list.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
describe("LibSQL memory list", () => {
|
|
3
|
+
it.todo("filters by namespace/entity/agent/collection");
|
|
4
|
+
it.todo("filters by wmem and smem");
|
|
5
|
+
it.todo("filters by timestamp range");
|
|
6
|
+
it.todo("orders by timestamp desc/asc");
|
|
7
|
+
it.todo("applies limit and offset");
|
|
8
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"memory.update.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/memory.update.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
import { describe, it } from "vitest";
|
|
2
|
+
describe("LibSQL memory update", () => {
|
|
3
|
+
it.todo("updates content and bumps updated_at");
|
|
4
|
+
it.todo("updates wmem flag");
|
|
5
|
+
it.todo("updates smem expiration");
|
|
6
|
+
it.todo("updates metadata");
|
|
7
|
+
it.todo("throws when memory does not exist");
|
|
8
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"migrations.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/migrations.test.ts"],"names":[],"mappings":""}
|
|
@@ -0,0 +1,68 @@
|
|
|
1
|
+
import { describe, it, expect, beforeEach, afterEach } from "vitest";
|
|
2
|
+
import { create_client, create_storage, THREADS_TABLE, MEMORIES_TABLE, } from "./helpers.js";
|
|
3
|
+
describe("LibSQL migrations", () => {
|
|
4
|
+
let client;
|
|
5
|
+
beforeEach(() => {
|
|
6
|
+
client = create_client();
|
|
7
|
+
});
|
|
8
|
+
afterEach(() => {
|
|
9
|
+
client.close();
|
|
10
|
+
});
|
|
11
|
+
it("creates tables with mapped SQLite types", async () => {
|
|
12
|
+
const storage = create_storage(client);
|
|
13
|
+
await storage.memories.list();
|
|
14
|
+
// Check threads table schema
|
|
15
|
+
const threadInfo = await client.execute(`PRAGMA table_info("${THREADS_TABLE}")`);
|
|
16
|
+
const threadCols = Object.fromEntries(threadInfo.rows.map((r) => [r.name, r.type]));
|
|
17
|
+
// JSON fields should be TEXT (not JSONB)
|
|
18
|
+
expect(threadCols.context).toBe("TEXT");
|
|
19
|
+
expect(threadCols.metadata).toBe("TEXT");
|
|
20
|
+
expect(threadCols.state).toBe("TEXT");
|
|
21
|
+
// Check memories table schema
|
|
22
|
+
const memoryInfo = await client.execute(`PRAGMA table_info("${MEMORIES_TABLE}")`);
|
|
23
|
+
const memoryCols = Object.fromEntries(memoryInfo.rows.map((r) => [r.name, r.type]));
|
|
24
|
+
// Boolean fields should be INTEGER
|
|
25
|
+
expect(memoryCols.wmem).toBe("INTEGER");
|
|
26
|
+
// JSON fields should be TEXT
|
|
27
|
+
expect(memoryCols.content).toBe("TEXT");
|
|
28
|
+
expect(memoryCols.metadata).toBe("TEXT");
|
|
29
|
+
});
|
|
30
|
+
it("applies column defaults correctly", async () => {
|
|
31
|
+
const storage = create_storage(client);
|
|
32
|
+
await storage.memories.list();
|
|
33
|
+
// Check threads table defaults
|
|
34
|
+
const threadInfo = await client.execute(`PRAGMA table_info("${THREADS_TABLE}")`);
|
|
35
|
+
const tickCol = threadInfo.rows.find((r) => r.name === "tick");
|
|
36
|
+
const namespaceCol = threadInfo.rows.find((r) => r.name === "namespace");
|
|
37
|
+
expect(tickCol?.dflt_value).toBe("0");
|
|
38
|
+
expect(namespaceCol?.dflt_value).toBe("'kernl'");
|
|
39
|
+
// Check memories table defaults
|
|
40
|
+
const memoryInfo = await client.execute(`PRAGMA table_info("${MEMORIES_TABLE}")`);
|
|
41
|
+
const wmemCol = memoryInfo.rows.find((r) => r.name === "wmem");
|
|
42
|
+
expect(wmemCol?.dflt_value).toBe("0");
|
|
43
|
+
});
|
|
44
|
+
it("creates indexes from table definitions", async () => {
|
|
45
|
+
const storage = create_storage(client);
|
|
46
|
+
await storage.memories.list();
|
|
47
|
+
// Check for indexes on threads table
|
|
48
|
+
const threadIndexes = await client.execute(`PRAGMA index_list("${THREADS_TABLE}")`);
|
|
49
|
+
const threadIndexNames = threadIndexes.rows.map((r) => r.name);
|
|
50
|
+
// Should have index on namespace
|
|
51
|
+
expect(threadIndexNames.some((n) => n.includes("namespace"))).toBe(true);
|
|
52
|
+
// Check for indexes on memories table
|
|
53
|
+
const memoryIndexes = await client.execute(`PRAGMA index_list("${MEMORIES_TABLE}")`);
|
|
54
|
+
const memoryIndexNames = memoryIndexes.rows.map((r) => r.name);
|
|
55
|
+
// Should have indexes for common queries
|
|
56
|
+
expect(memoryIndexNames.some((n) => n.includes("namespace"))).toBe(true);
|
|
57
|
+
});
|
|
58
|
+
it("creates foreign key constraint on thread_events", async () => {
|
|
59
|
+
const storage = create_storage(client);
|
|
60
|
+
await storage.memories.list();
|
|
61
|
+
const fkInfo = await client.execute('PRAGMA foreign_key_list("kernl_thread_events")');
|
|
62
|
+
expect(fkInfo.rows.length).toBeGreaterThan(0);
|
|
63
|
+
const threadFk = fkInfo.rows.find((r) => r.table === THREADS_TABLE);
|
|
64
|
+
expect(threadFk).toBeDefined();
|
|
65
|
+
expect(threadFk?.from).toBe("tid");
|
|
66
|
+
expect(threadFk?.to).toBe("id");
|
|
67
|
+
});
|
|
68
|
+
});
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"row-codecs.test.d.ts","sourceRoot":"","sources":["../../src/__tests__/row-codecs.test.ts"],"names":[],"mappings":""}
|