@deepagents/context 7.8.1 → 7.9.0
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/dist/index.d.ts +1 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +402 -410
- package/dist/index.js.map +4 -4
- package/dist/lib/store/postgres-store-database.d.ts +25 -0
- package/dist/lib/store/postgres-store-database.d.ts.map +1 -0
- package/dist/lib/store/postgres-transaction.d.ts +19 -5
- package/dist/lib/store/postgres-transaction.d.ts.map +1 -1
- package/dist/lib/store/postgres.store.d.ts +8 -18
- package/dist/lib/store/postgres.store.d.ts.map +1 -1
- package/dist/lib/stream/postgres-notify-change-source.d.ts +3 -2
- package/dist/lib/stream/postgres-notify-change-source.d.ts.map +1 -1
- package/dist/lib/stream/postgres.stream-store.d.ts +6 -3
- package/dist/lib/stream/postgres.stream-store.d.ts.map +1 -1
- package/package.json +2 -2
package/dist/index.js
CHANGED
|
@@ -11120,16 +11120,79 @@ function lockSchemaDDL(ddl, schema) {
|
|
|
11120
11120
|
${ddl}`;
|
|
11121
11121
|
}
|
|
11122
11122
|
|
|
11123
|
+
// packages/context/src/lib/store/postgres-store-database.ts
|
|
11124
|
+
import { z as z14 } from "zod";
|
|
11125
|
+
|
|
11123
11126
|
// packages/context/src/lib/store/postgres-transaction.ts
|
|
11124
11127
|
function surfacedThroughQueries() {
|
|
11125
11128
|
}
|
|
11126
|
-
|
|
11127
|
-
|
|
11129
|
+
var openTransaction;
|
|
11130
|
+
var finishTransaction;
|
|
11131
|
+
var openSavepoint;
|
|
11132
|
+
var closeSavepoint;
|
|
11133
|
+
var PostgresTransaction = class _PostgresTransaction {
|
|
11134
|
+
static {
|
|
11135
|
+
openTransaction = (client) => new _PostgresTransaction(client, void 0);
|
|
11136
|
+
finishTransaction = (tx) => {
|
|
11137
|
+
tx.#finished = true;
|
|
11138
|
+
};
|
|
11139
|
+
openSavepoint = (parent) => {
|
|
11140
|
+
parent.#assertUsable();
|
|
11141
|
+
const child = new _PostgresTransaction(parent.#client, parent);
|
|
11142
|
+
parent.#child = child;
|
|
11143
|
+
return child;
|
|
11144
|
+
};
|
|
11145
|
+
closeSavepoint = (parent, child) => {
|
|
11146
|
+
child.#finished = true;
|
|
11147
|
+
parent.#child = void 0;
|
|
11148
|
+
};
|
|
11149
|
+
}
|
|
11150
|
+
#client;
|
|
11151
|
+
#parent;
|
|
11152
|
+
#child;
|
|
11153
|
+
#finished = false;
|
|
11154
|
+
constructor(client, parent) {
|
|
11155
|
+
this.#client = client;
|
|
11156
|
+
this.#parent = parent;
|
|
11157
|
+
}
|
|
11158
|
+
async query(text3, values) {
|
|
11159
|
+
this.#assertUsable();
|
|
11160
|
+
return this.#client.query(text3, values);
|
|
11161
|
+
}
|
|
11162
|
+
#assertUsable() {
|
|
11163
|
+
if (this.#isFinished()) {
|
|
11164
|
+
throw new Error(
|
|
11165
|
+
"PostgresTransaction: the transaction is already finished"
|
|
11166
|
+
);
|
|
11167
|
+
}
|
|
11168
|
+
if (this.#child) {
|
|
11169
|
+
throw new Error(
|
|
11170
|
+
"PostgresTransaction: a savepoint opened on this transaction is still running; run its work in sequence"
|
|
11171
|
+
);
|
|
11172
|
+
}
|
|
11173
|
+
}
|
|
11174
|
+
#isFinished() {
|
|
11175
|
+
const parent = this.#parent;
|
|
11176
|
+
return this.#finished || parent !== void 0 && parent.#isFinished();
|
|
11177
|
+
}
|
|
11178
|
+
};
|
|
11179
|
+
var savepoints = 0;
|
|
11180
|
+
async function postgresTransaction(db, fn) {
|
|
11181
|
+
if (db instanceof PostgresTransaction) {
|
|
11182
|
+
return inSavepoint(db, fn);
|
|
11183
|
+
}
|
|
11184
|
+
const client = await db.connect();
|
|
11128
11185
|
client.on("error", surfacedThroughQueries);
|
|
11186
|
+
const tx = openTransaction(client);
|
|
11129
11187
|
let broken = false;
|
|
11130
11188
|
try {
|
|
11131
11189
|
await client.query("BEGIN");
|
|
11132
|
-
|
|
11190
|
+
let result;
|
|
11191
|
+
try {
|
|
11192
|
+
result = await fn(tx);
|
|
11193
|
+
} finally {
|
|
11194
|
+
finishTransaction(tx);
|
|
11195
|
+
}
|
|
11133
11196
|
await client.query("COMMIT");
|
|
11134
11197
|
return result;
|
|
11135
11198
|
} catch (error) {
|
|
@@ -11144,10 +11207,60 @@ async function postgresTransaction(pool, fn) {
|
|
|
11144
11207
|
client.release(broken);
|
|
11145
11208
|
}
|
|
11146
11209
|
}
|
|
11210
|
+
async function inSavepoint(parent, fn) {
|
|
11211
|
+
const savepoint = `deepagents_savepoint_${++savepoints}`;
|
|
11212
|
+
const child = openSavepoint(parent);
|
|
11213
|
+
let result;
|
|
11214
|
+
try {
|
|
11215
|
+
await child.query(`SAVEPOINT ${savepoint}`);
|
|
11216
|
+
result = await fn(child);
|
|
11217
|
+
} catch (error) {
|
|
11218
|
+
closeSavepoint(parent, child);
|
|
11219
|
+
await parent.query(`ROLLBACK TO SAVEPOINT ${savepoint}`);
|
|
11220
|
+
throw error;
|
|
11221
|
+
}
|
|
11222
|
+
closeSavepoint(parent, child);
|
|
11223
|
+
await parent.query(`RELEASE SAVEPOINT ${savepoint}`);
|
|
11224
|
+
return result;
|
|
11225
|
+
}
|
|
11226
|
+
|
|
11227
|
+
// packages/context/src/lib/store/postgres-store-database.ts
|
|
11228
|
+
var PostgresStoreDatabase = class _PostgresStoreDatabase {
|
|
11229
|
+
schema;
|
|
11230
|
+
#db;
|
|
11231
|
+
constructor(db, schema = "public") {
|
|
11232
|
+
if (!/^[a-zA-Z_]\w*$/.test(schema)) {
|
|
11233
|
+
throw new Error(`Invalid schema name: "${schema}"`);
|
|
11234
|
+
}
|
|
11235
|
+
this.schema = schema;
|
|
11236
|
+
this.#db = db;
|
|
11237
|
+
}
|
|
11238
|
+
/** The same schema, read and written inside `tx`. */
|
|
11239
|
+
bind(tx) {
|
|
11240
|
+
return new _PostgresStoreDatabase(tx, this.schema);
|
|
11241
|
+
}
|
|
11242
|
+
/** `name` qualified by the schema, quoted for SQL. */
|
|
11243
|
+
table(name) {
|
|
11244
|
+
return `"${this.schema}"."${name}"`;
|
|
11245
|
+
}
|
|
11246
|
+
query(text3, values) {
|
|
11247
|
+
const db = this.#db;
|
|
11248
|
+
return db.query(text3, values);
|
|
11249
|
+
}
|
|
11250
|
+
async rows(row, text3, values) {
|
|
11251
|
+
return z14.array(row).parse((await this.query(text3, values)).rows);
|
|
11252
|
+
}
|
|
11253
|
+
/**
|
|
11254
|
+
* Runs `fn` in a transaction: its own on the pool, or a savepoint of the
|
|
11255
|
+
* transaction this database is bound to.
|
|
11256
|
+
*/
|
|
11257
|
+
transaction(fn) {
|
|
11258
|
+
return postgresTransaction(this.#db, fn);
|
|
11259
|
+
}
|
|
11260
|
+
};
|
|
11147
11261
|
|
|
11148
11262
|
// packages/context/src/lib/store/postgres.store.ts
|
|
11149
|
-
import {
|
|
11150
|
-
import { z as z14 } from "zod";
|
|
11263
|
+
import { z as z15 } from "zod";
|
|
11151
11264
|
|
|
11152
11265
|
// packages/context/src/lib/store/ddl.postgres.ts
|
|
11153
11266
|
function storeDDL(schema) {
|
|
@@ -11238,10 +11351,10 @@ CREATE TRIGGER "${schema}_messages_fts_vector_update"
|
|
|
11238
11351
|
}
|
|
11239
11352
|
|
|
11240
11353
|
// packages/context/src/lib/store/postgres.store.ts
|
|
11241
|
-
var chatColumns2 =
|
|
11242
|
-
id:
|
|
11243
|
-
userid:
|
|
11244
|
-
title:
|
|
11354
|
+
var chatColumns2 = z15.object({
|
|
11355
|
+
id: z15.string(),
|
|
11356
|
+
userid: z15.string(),
|
|
11357
|
+
title: z15.string().nullable(),
|
|
11245
11358
|
metadata: jsonObject.nullable(),
|
|
11246
11359
|
createdat: bigintColumn,
|
|
11247
11360
|
updatedat: bigintColumn
|
|
@@ -11252,27 +11365,27 @@ var chatInfo2 = chatColumns2.extend({ messagecount: bigintColumn, branchcount: b
|
|
|
11252
11365
|
messageCount: row.messagecount,
|
|
11253
11366
|
branchCount: row.branchcount
|
|
11254
11367
|
}));
|
|
11255
|
-
var messageColumns2 =
|
|
11256
|
-
id:
|
|
11257
|
-
chatid:
|
|
11258
|
-
parentid:
|
|
11259
|
-
name:
|
|
11260
|
-
type:
|
|
11261
|
-
data:
|
|
11368
|
+
var messageColumns2 = z15.object({
|
|
11369
|
+
id: z15.string(),
|
|
11370
|
+
chatid: z15.string(),
|
|
11371
|
+
parentid: z15.string().nullable(),
|
|
11372
|
+
name: z15.string(),
|
|
11373
|
+
type: z15.string().nullable(),
|
|
11374
|
+
data: z15.unknown(),
|
|
11262
11375
|
createdat: bigintColumn
|
|
11263
11376
|
});
|
|
11264
11377
|
var storedMessage2 = messageColumns2.transform(toMessage2);
|
|
11265
|
-
var searchResult2 = messageColumns2.extend({ rank:
|
|
11378
|
+
var searchResult2 = messageColumns2.extend({ rank: z15.number(), snippet: z15.string() }).transform((row) => ({
|
|
11266
11379
|
message: toMessage2(row),
|
|
11267
11380
|
rank: row.rank,
|
|
11268
11381
|
snippet: row.snippet
|
|
11269
11382
|
}));
|
|
11270
|
-
var storedBranch2 =
|
|
11271
|
-
id:
|
|
11272
|
-
chatid:
|
|
11273
|
-
name:
|
|
11274
|
-
headmessageid:
|
|
11275
|
-
isactive:
|
|
11383
|
+
var storedBranch2 = z15.object({
|
|
11384
|
+
id: z15.string(),
|
|
11385
|
+
chatid: z15.string(),
|
|
11386
|
+
name: z15.string(),
|
|
11387
|
+
headmessageid: z15.string().nullable(),
|
|
11388
|
+
isactive: z15.boolean(),
|
|
11276
11389
|
createdat: bigintColumn
|
|
11277
11390
|
}).transform((row) => ({
|
|
11278
11391
|
id: row.id,
|
|
@@ -11282,18 +11395,18 @@ var storedBranch2 = z14.object({
|
|
|
11282
11395
|
isActive: row.isactive,
|
|
11283
11396
|
createdAt: row.createdat
|
|
11284
11397
|
}));
|
|
11285
|
-
var branchRow =
|
|
11286
|
-
id:
|
|
11287
|
-
name:
|
|
11288
|
-
headmessageid:
|
|
11289
|
-
isactive:
|
|
11398
|
+
var branchRow = z15.object({
|
|
11399
|
+
id: z15.string(),
|
|
11400
|
+
name: z15.string(),
|
|
11401
|
+
headmessageid: z15.string().nullable(),
|
|
11402
|
+
isactive: z15.boolean(),
|
|
11290
11403
|
createdat: bigintColumn
|
|
11291
11404
|
});
|
|
11292
|
-
var storedCheckpoint2 =
|
|
11293
|
-
id:
|
|
11294
|
-
chatid:
|
|
11295
|
-
name:
|
|
11296
|
-
messageid:
|
|
11405
|
+
var storedCheckpoint2 = z15.object({
|
|
11406
|
+
id: z15.string(),
|
|
11407
|
+
chatid: z15.string(),
|
|
11408
|
+
name: z15.string(),
|
|
11409
|
+
messageid: z15.string(),
|
|
11297
11410
|
createdat: bigintColumn
|
|
11298
11411
|
}).transform((row) => ({
|
|
11299
11412
|
id: row.id,
|
|
@@ -11302,10 +11415,10 @@ var storedCheckpoint2 = z14.object({
|
|
|
11302
11415
|
messageId: row.messageid,
|
|
11303
11416
|
createdAt: row.createdat
|
|
11304
11417
|
}));
|
|
11305
|
-
var checkpointInfo2 =
|
|
11306
|
-
id:
|
|
11307
|
-
name:
|
|
11308
|
-
messageid:
|
|
11418
|
+
var checkpointInfo2 = z15.object({
|
|
11419
|
+
id: z15.string(),
|
|
11420
|
+
name: z15.string(),
|
|
11421
|
+
messageid: z15.string(),
|
|
11309
11422
|
createdat: bigintColumn
|
|
11310
11423
|
}).transform((row) => ({
|
|
11311
11424
|
id: row.id,
|
|
@@ -11313,26 +11426,26 @@ var checkpointInfo2 = z14.object({
|
|
|
11313
11426
|
messageId: row.messageid,
|
|
11314
11427
|
createdAt: row.createdat
|
|
11315
11428
|
}));
|
|
11316
|
-
var idRow2 =
|
|
11317
|
-
var existsRow =
|
|
11318
|
-
var countRow =
|
|
11319
|
-
var graphMessageRow2 =
|
|
11320
|
-
id:
|
|
11321
|
-
parentid:
|
|
11322
|
-
name:
|
|
11323
|
-
data:
|
|
11429
|
+
var idRow2 = z15.object({ id: z15.string() });
|
|
11430
|
+
var existsRow = z15.object({ exists: z15.boolean() });
|
|
11431
|
+
var countRow = z15.object({ count: bigintColumn });
|
|
11432
|
+
var graphMessageRow2 = z15.object({
|
|
11433
|
+
id: z15.string(),
|
|
11434
|
+
parentid: z15.string().nullable(),
|
|
11435
|
+
name: z15.string(),
|
|
11436
|
+
data: z15.unknown(),
|
|
11324
11437
|
createdat: bigintColumn
|
|
11325
11438
|
});
|
|
11326
|
-
var graphBranch2 =
|
|
11327
|
-
name:
|
|
11328
|
-
headmessageid:
|
|
11329
|
-
isactive:
|
|
11439
|
+
var graphBranch2 = z15.object({
|
|
11440
|
+
name: z15.string(),
|
|
11441
|
+
headmessageid: z15.string().nullable(),
|
|
11442
|
+
isactive: z15.boolean()
|
|
11330
11443
|
}).transform((row) => ({
|
|
11331
11444
|
name: row.name,
|
|
11332
11445
|
headMessageId: row.headmessageid,
|
|
11333
11446
|
isActive: row.isactive
|
|
11334
11447
|
}));
|
|
11335
|
-
var graphCheckpoint2 =
|
|
11448
|
+
var graphCheckpoint2 = z15.object({ name: z15.string(), messageid: z15.string() }).transform((row) => ({
|
|
11336
11449
|
name: row.name,
|
|
11337
11450
|
messageId: row.messageid
|
|
11338
11451
|
}));
|
|
@@ -11358,81 +11471,30 @@ function toMessage2(row) {
|
|
|
11358
11471
|
};
|
|
11359
11472
|
}
|
|
11360
11473
|
var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
11361
|
-
#
|
|
11362
|
-
|
|
11363
|
-
#ownsPool;
|
|
11364
|
-
constructor(options) {
|
|
11474
|
+
#database;
|
|
11475
|
+
constructor(database) {
|
|
11365
11476
|
super();
|
|
11366
|
-
|
|
11367
|
-
if (!/^[a-zA-Z_]\w*$/.test(schema)) {
|
|
11368
|
-
throw new Error(`Invalid schema name: "${schema}"`);
|
|
11369
|
-
}
|
|
11370
|
-
this.#schema = schema;
|
|
11371
|
-
const pg = _PostgresContextStore.#requirePg();
|
|
11372
|
-
if (options.pool instanceof pg.Pool) {
|
|
11373
|
-
this.#pool = options.pool;
|
|
11374
|
-
this.#ownsPool = false;
|
|
11375
|
-
} else {
|
|
11376
|
-
this.#pool = typeof options.pool === "string" ? new pg.Pool({ connectionString: options.pool }) : new pg.Pool(options.pool);
|
|
11377
|
-
this.#ownsPool = true;
|
|
11378
|
-
}
|
|
11379
|
-
}
|
|
11380
|
-
static #requirePg() {
|
|
11381
|
-
try {
|
|
11382
|
-
const require2 = createRequire(import.meta.url);
|
|
11383
|
-
return require2("pg");
|
|
11384
|
-
} catch {
|
|
11385
|
-
throw new Error(
|
|
11386
|
-
'PostgresContextStore requires the "pg" package. Install it with: npm install pg'
|
|
11387
|
-
);
|
|
11388
|
-
}
|
|
11389
|
-
}
|
|
11390
|
-
#t(name) {
|
|
11391
|
-
return `"${this.#schema}"."${name}"`;
|
|
11477
|
+
this.#database = database;
|
|
11392
11478
|
}
|
|
11393
11479
|
/** Opens the store: creates its schema objects before it returns. */
|
|
11394
11480
|
static async create(options) {
|
|
11395
|
-
const
|
|
11396
|
-
|
|
11397
|
-
|
|
11398
|
-
|
|
11399
|
-
|
|
11400
|
-
throw error;
|
|
11401
|
-
}
|
|
11402
|
-
return store;
|
|
11403
|
-
}
|
|
11404
|
-
async #initialize() {
|
|
11405
|
-
await this.#pool.query(lockSchemaDDL(storeDDL(this.#schema), this.#schema));
|
|
11406
|
-
}
|
|
11407
|
-
/**
|
|
11408
|
-
* Execute a query using the pool (no transaction).
|
|
11409
|
-
*/
|
|
11410
|
-
async #query(sql, params) {
|
|
11411
|
-
const result = await this.#pool.query(sql, params);
|
|
11412
|
-
return result.rows;
|
|
11413
|
-
}
|
|
11414
|
-
/**
|
|
11415
|
-
* Execute a query using the pool and parse every returned row.
|
|
11416
|
-
*/
|
|
11417
|
-
async #rows(row, sql, params) {
|
|
11418
|
-
return z14.array(row).parse(await this.#query(sql, params));
|
|
11481
|
+
const database = new PostgresStoreDatabase(options.pool, options.schema);
|
|
11482
|
+
await database.query(
|
|
11483
|
+
lockSchemaDDL(storeDDL(database.schema), database.schema)
|
|
11484
|
+
);
|
|
11485
|
+
return new _PostgresContextStore(database);
|
|
11419
11486
|
}
|
|
11420
|
-
/**
|
|
11421
|
-
|
|
11422
|
-
|
|
11423
|
-
*/
|
|
11424
|
-
async close() {
|
|
11425
|
-
if (this.#ownsPool) {
|
|
11426
|
-
await this.#pool.end();
|
|
11427
|
-
}
|
|
11487
|
+
/** A view of this store whose reads and writes run inside `tx`. */
|
|
11488
|
+
bind(tx) {
|
|
11489
|
+
return new _PostgresContextStore(this.#database.bind(tx));
|
|
11428
11490
|
}
|
|
11429
11491
|
// ==========================================================================
|
|
11430
11492
|
// Chat Operations
|
|
11431
11493
|
// ==========================================================================
|
|
11432
11494
|
async createChat(chat2) {
|
|
11433
|
-
return
|
|
11434
|
-
const result = await
|
|
11435
|
-
`INSERT INTO ${this.#
|
|
11495
|
+
return this.#database.transaction(async (tx) => {
|
|
11496
|
+
const result = await tx.query(
|
|
11497
|
+
`INSERT INTO ${this.#database.table("chats")} (id, userId, title, metadata)
|
|
11436
11498
|
VALUES ($1, $2, $3, $4)
|
|
11437
11499
|
RETURNING *`,
|
|
11438
11500
|
[
|
|
@@ -11443,8 +11505,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11443
11505
|
]
|
|
11444
11506
|
);
|
|
11445
11507
|
const created = storedChat2.parse(result.rows[0]);
|
|
11446
|
-
await
|
|
11447
|
-
`INSERT INTO ${this.#
|
|
11508
|
+
await tx.query(
|
|
11509
|
+
`INSERT INTO ${this.#database.table("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
11448
11510
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)`,
|
|
11449
11511
|
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
11450
11512
|
);
|
|
@@ -11452,9 +11514,9 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11452
11514
|
});
|
|
11453
11515
|
}
|
|
11454
11516
|
async upsertChat(chat2) {
|
|
11455
|
-
return
|
|
11456
|
-
const result = await
|
|
11457
|
-
`INSERT INTO ${this.#
|
|
11517
|
+
return this.#database.transaction(async (tx) => {
|
|
11518
|
+
const result = await tx.query(
|
|
11519
|
+
`INSERT INTO ${this.#database.table("chats")} (id, userId, title, metadata)
|
|
11458
11520
|
VALUES ($1, $2, $3, $4)
|
|
11459
11521
|
ON CONFLICT(id) DO UPDATE SET id = EXCLUDED.id
|
|
11460
11522
|
RETURNING *`,
|
|
@@ -11466,8 +11528,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11466
11528
|
]
|
|
11467
11529
|
);
|
|
11468
11530
|
const upserted = storedChat2.parse(result.rows[0]);
|
|
11469
|
-
await
|
|
11470
|
-
`INSERT INTO ${this.#
|
|
11531
|
+
await tx.query(
|
|
11532
|
+
`INSERT INTO ${this.#database.table("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
11471
11533
|
VALUES ($1, $2, 'main', NULL, TRUE, $3)
|
|
11472
11534
|
ON CONFLICT(chatId, name) DO NOTHING`,
|
|
11473
11535
|
[crypto.randomUUID(), chat2.id, Date.now()]
|
|
@@ -11476,17 +11538,17 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11476
11538
|
});
|
|
11477
11539
|
}
|
|
11478
11540
|
async getChat(chatId) {
|
|
11479
|
-
const [chat2] = await this.#rows(
|
|
11541
|
+
const [chat2] = await this.#database.rows(
|
|
11480
11542
|
storedChat2,
|
|
11481
|
-
`SELECT * FROM ${this.#
|
|
11543
|
+
`SELECT * FROM ${this.#database.table("chats")} WHERE id = $1`,
|
|
11482
11544
|
[chatId]
|
|
11483
11545
|
);
|
|
11484
11546
|
return chat2;
|
|
11485
11547
|
}
|
|
11486
11548
|
async updateChat(chatId, update) {
|
|
11487
|
-
return
|
|
11488
|
-
const result = await
|
|
11489
|
-
`SELECT * FROM ${this.#
|
|
11549
|
+
return this.#database.transaction(async (tx) => {
|
|
11550
|
+
const result = await tx.query(
|
|
11551
|
+
`SELECT * FROM ${this.#database.table("chats")} WHERE id = $1 FOR UPDATE`,
|
|
11490
11552
|
[chatId]
|
|
11491
11553
|
);
|
|
11492
11554
|
const current = storedChat2.optional().parse(result.rows[0]);
|
|
@@ -11509,8 +11571,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11509
11571
|
params.push(JSON.stringify(updates.metadata));
|
|
11510
11572
|
}
|
|
11511
11573
|
params.push(chatId);
|
|
11512
|
-
const updated = await
|
|
11513
|
-
`UPDATE ${this.#
|
|
11574
|
+
const updated = await tx.query(
|
|
11575
|
+
`UPDATE ${this.#database.table("chats")} SET ${setClauses.join(", ")} WHERE id = $${paramIndex} RETURNING *`,
|
|
11514
11576
|
params
|
|
11515
11577
|
);
|
|
11516
11578
|
return storedChat2.parse(updated.rows[0]);
|
|
@@ -11541,7 +11603,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11541
11603
|
params.push(options.offset);
|
|
11542
11604
|
}
|
|
11543
11605
|
}
|
|
11544
|
-
return this.#rows(
|
|
11606
|
+
return this.#database.rows(
|
|
11545
11607
|
chatInfo2,
|
|
11546
11608
|
`SELECT
|
|
11547
11609
|
c.id,
|
|
@@ -11552,9 +11614,9 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11552
11614
|
c.updatedAt,
|
|
11553
11615
|
COUNT(DISTINCT m.id) as messageCount,
|
|
11554
11616
|
COUNT(DISTINCT b.id) as branchCount
|
|
11555
|
-
FROM ${this.#
|
|
11556
|
-
LEFT JOIN ${this.#
|
|
11557
|
-
LEFT JOIN ${this.#
|
|
11617
|
+
FROM ${this.#database.table("chats")} c
|
|
11618
|
+
LEFT JOIN ${this.#database.table("messages")} m ON m.chatId = c.id
|
|
11619
|
+
LEFT JOIN ${this.#database.table("branches")} b ON b.chatId = c.id
|
|
11558
11620
|
${whereClause}
|
|
11559
11621
|
GROUP BY c.id
|
|
11560
11622
|
ORDER BY c.updatedAt DESC${limitClause}`,
|
|
@@ -11562,14 +11624,14 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11562
11624
|
);
|
|
11563
11625
|
}
|
|
11564
11626
|
async deleteChat(chatId, options) {
|
|
11565
|
-
return
|
|
11566
|
-
let sql = `DELETE FROM ${this.#
|
|
11627
|
+
return this.#database.transaction(async (tx) => {
|
|
11628
|
+
let sql = `DELETE FROM ${this.#database.table("chats")} WHERE id = $1`;
|
|
11567
11629
|
const params = [chatId];
|
|
11568
11630
|
if (options?.userId !== void 0) {
|
|
11569
11631
|
sql += " AND userId = $2";
|
|
11570
11632
|
params.push(options.userId);
|
|
11571
11633
|
}
|
|
11572
|
-
const result = await
|
|
11634
|
+
const result = await tx.query(sql, params);
|
|
11573
11635
|
return (result.rowCount ?? 0) > 0;
|
|
11574
11636
|
});
|
|
11575
11637
|
}
|
|
@@ -11580,9 +11642,9 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11580
11642
|
if (message2.parentId === message2.id) {
|
|
11581
11643
|
throw new Error(`Message ${message2.id} cannot be its own parent`);
|
|
11582
11644
|
}
|
|
11583
|
-
await
|
|
11584
|
-
await
|
|
11585
|
-
`INSERT INTO ${this.#
|
|
11645
|
+
await this.#database.transaction(async (tx) => {
|
|
11646
|
+
await tx.query(
|
|
11647
|
+
`INSERT INTO ${this.#database.table("messages")} (id, chatId, parentId, name, type, data, createdAt)
|
|
11586
11648
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
11587
11649
|
ON CONFLICT(id) DO UPDATE SET
|
|
11588
11650
|
name = EXCLUDED.name,
|
|
@@ -11599,8 +11661,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11599
11661
|
]
|
|
11600
11662
|
);
|
|
11601
11663
|
const content = typeof message2.data === "string" ? message2.data : JSON.stringify(message2.data);
|
|
11602
|
-
await
|
|
11603
|
-
`INSERT INTO ${this.#
|
|
11664
|
+
await tx.query(
|
|
11665
|
+
`INSERT INTO ${this.#database.table("messages_fts")} (messageId, chatId, name, content)
|
|
11604
11666
|
VALUES ($1, $2, $3, $4)
|
|
11605
11667
|
ON CONFLICT(messageId) DO UPDATE SET
|
|
11606
11668
|
chatId = EXCLUDED.chatId,
|
|
@@ -11614,9 +11676,9 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11614
11676
|
if (parentId === messageId) {
|
|
11615
11677
|
throw new Error(`Message ${messageId} cannot be its own parent`);
|
|
11616
11678
|
}
|
|
11617
|
-
const rows = await this.#rows(
|
|
11679
|
+
const rows = await this.#database.rows(
|
|
11618
11680
|
idRow2,
|
|
11619
|
-
`UPDATE ${this.#
|
|
11681
|
+
`UPDATE ${this.#database.table("messages")} SET parentId = $1 WHERE id = $2 RETURNING id`,
|
|
11620
11682
|
[parentId, messageId]
|
|
11621
11683
|
);
|
|
11622
11684
|
if (rows.length === 0) {
|
|
@@ -11624,20 +11686,20 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11624
11686
|
}
|
|
11625
11687
|
}
|
|
11626
11688
|
async getMessage(messageId) {
|
|
11627
|
-
const [message2] = await this.#rows(
|
|
11689
|
+
const [message2] = await this.#database.rows(
|
|
11628
11690
|
storedMessage2,
|
|
11629
|
-
`SELECT * FROM ${this.#
|
|
11691
|
+
`SELECT * FROM ${this.#database.table("messages")} WHERE id = $1`,
|
|
11630
11692
|
[messageId]
|
|
11631
11693
|
);
|
|
11632
11694
|
return message2;
|
|
11633
11695
|
}
|
|
11634
11696
|
async getMessageChain(headId) {
|
|
11635
|
-
return this.#rows(
|
|
11697
|
+
return this.#database.rows(
|
|
11636
11698
|
storedMessage2,
|
|
11637
11699
|
`WITH RECURSIVE chain AS (
|
|
11638
|
-
SELECT *, 0 as depth FROM ${this.#
|
|
11700
|
+
SELECT *, 0 as depth FROM ${this.#database.table("messages")} WHERE id = $1
|
|
11639
11701
|
UNION ALL
|
|
11640
|
-
SELECT m.*, c.depth + 1 FROM ${this.#
|
|
11702
|
+
SELECT m.*, c.depth + 1 FROM ${this.#database.table("messages")} m
|
|
11641
11703
|
INNER JOIN chain c ON m.id = c.parentId
|
|
11642
11704
|
WHERE c.depth < 10000
|
|
11643
11705
|
)
|
|
@@ -11647,9 +11709,9 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11647
11709
|
);
|
|
11648
11710
|
}
|
|
11649
11711
|
async hasChildren(messageId) {
|
|
11650
|
-
const [row] = await this.#rows(
|
|
11712
|
+
const [row] = await this.#database.rows(
|
|
11651
11713
|
existsRow,
|
|
11652
|
-
`SELECT EXISTS(SELECT 1 FROM ${this.#
|
|
11714
|
+
`SELECT EXISTS(SELECT 1 FROM ${this.#database.table("messages")} WHERE parentId = $1) as exists`,
|
|
11653
11715
|
[messageId]
|
|
11654
11716
|
);
|
|
11655
11717
|
return row.exists;
|
|
@@ -11669,8 +11731,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11669
11731
|
// Branch Operations
|
|
11670
11732
|
// ==========================================================================
|
|
11671
11733
|
async createBranch(branch) {
|
|
11672
|
-
await this.#query(
|
|
11673
|
-
`INSERT INTO ${this.#
|
|
11734
|
+
await this.#database.query(
|
|
11735
|
+
`INSERT INTO ${this.#database.table("branches")} (id, chatId, name, headMessageId, isActive, createdAt)
|
|
11674
11736
|
VALUES ($1, $2, $3, $4, $5, $6)`,
|
|
11675
11737
|
[
|
|
11676
11738
|
branch.id,
|
|
@@ -11683,37 +11745,37 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11683
11745
|
);
|
|
11684
11746
|
}
|
|
11685
11747
|
async getBranch(chatId, name) {
|
|
11686
|
-
const [branch] = await this.#rows(
|
|
11748
|
+
const [branch] = await this.#database.rows(
|
|
11687
11749
|
storedBranch2,
|
|
11688
|
-
`SELECT * FROM ${this.#
|
|
11750
|
+
`SELECT * FROM ${this.#database.table("branches")} WHERE chatId = $1 AND name = $2`,
|
|
11689
11751
|
[chatId, name]
|
|
11690
11752
|
);
|
|
11691
11753
|
return branch;
|
|
11692
11754
|
}
|
|
11693
11755
|
async getActiveBranch(chatId) {
|
|
11694
|
-
const [branch] = await this.#rows(
|
|
11756
|
+
const [branch] = await this.#database.rows(
|
|
11695
11757
|
storedBranch2,
|
|
11696
|
-
`SELECT * FROM ${this.#
|
|
11758
|
+
`SELECT * FROM ${this.#database.table("branches")} WHERE chatId = $1 AND isActive = TRUE`,
|
|
11697
11759
|
[chatId]
|
|
11698
11760
|
);
|
|
11699
11761
|
return branch;
|
|
11700
11762
|
}
|
|
11701
11763
|
async setActiveBranch(chatId, branchId) {
|
|
11702
|
-
await
|
|
11703
|
-
await
|
|
11704
|
-
`UPDATE ${this.#
|
|
11764
|
+
await this.#database.transaction(async (tx) => {
|
|
11765
|
+
await tx.query(
|
|
11766
|
+
`UPDATE ${this.#database.table("branches")} SET isActive = FALSE WHERE chatId = $1`,
|
|
11705
11767
|
[chatId]
|
|
11706
11768
|
);
|
|
11707
|
-
await
|
|
11708
|
-
`UPDATE ${this.#
|
|
11769
|
+
await tx.query(
|
|
11770
|
+
`UPDATE ${this.#database.table("branches")} SET isActive = TRUE WHERE id = $1`,
|
|
11709
11771
|
[branchId]
|
|
11710
11772
|
);
|
|
11711
11773
|
});
|
|
11712
11774
|
}
|
|
11713
11775
|
async updateBranchHead(branchId, messageId, expectedHeadMessageId) {
|
|
11714
|
-
const rows = await this.#rows(
|
|
11776
|
+
const rows = await this.#database.rows(
|
|
11715
11777
|
idRow2,
|
|
11716
|
-
`UPDATE ${this.#
|
|
11778
|
+
`UPDATE ${this.#database.table("branches")} SET headMessageId = $1
|
|
11717
11779
|
WHERE id = $2 AND headMessageId IS NOT DISTINCT FROM $3
|
|
11718
11780
|
RETURNING id`,
|
|
11719
11781
|
[messageId, branchId, expectedHeadMessageId]
|
|
@@ -11721,7 +11783,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11721
11783
|
return rows.length > 0;
|
|
11722
11784
|
}
|
|
11723
11785
|
async listBranches(chatId) {
|
|
11724
|
-
const branches = await this.#rows(
|
|
11786
|
+
const branches = await this.#database.rows(
|
|
11725
11787
|
branchRow,
|
|
11726
11788
|
`SELECT
|
|
11727
11789
|
id,
|
|
@@ -11729,7 +11791,7 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11729
11791
|
headMessageId,
|
|
11730
11792
|
isActive,
|
|
11731
11793
|
createdAt
|
|
11732
|
-
FROM ${this.#
|
|
11794
|
+
FROM ${this.#database.table("branches")}
|
|
11733
11795
|
WHERE chatId = $1
|
|
11734
11796
|
ORDER BY createdAt ASC`,
|
|
11735
11797
|
[chatId]
|
|
@@ -11748,12 +11810,12 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11748
11810
|
return result;
|
|
11749
11811
|
}
|
|
11750
11812
|
async #countChain(headMessageId) {
|
|
11751
|
-
const [row] = await this.#rows(
|
|
11813
|
+
const [row] = await this.#database.rows(
|
|
11752
11814
|
countRow,
|
|
11753
11815
|
`WITH RECURSIVE chain AS (
|
|
11754
|
-
SELECT id, parentId FROM ${this.#
|
|
11816
|
+
SELECT id, parentId FROM ${this.#database.table("messages")} WHERE id = $1
|
|
11755
11817
|
UNION ALL
|
|
11756
|
-
SELECT m.id, m.parentId FROM ${this.#
|
|
11818
|
+
SELECT m.id, m.parentId FROM ${this.#database.table("messages")} m
|
|
11757
11819
|
INNER JOIN chain c ON m.id = c.parentId
|
|
11758
11820
|
)
|
|
11759
11821
|
SELECT COUNT(*) as count FROM chain`,
|
|
@@ -11765,8 +11827,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11765
11827
|
// Checkpoint Operations
|
|
11766
11828
|
// ==========================================================================
|
|
11767
11829
|
async createCheckpoint(checkpoint) {
|
|
11768
|
-
await this.#query(
|
|
11769
|
-
`INSERT INTO ${this.#
|
|
11830
|
+
await this.#database.query(
|
|
11831
|
+
`INSERT INTO ${this.#database.table("checkpoints")} (id, chatId, name, messageId, createdAt)
|
|
11770
11832
|
VALUES ($1, $2, $3, $4, $5)
|
|
11771
11833
|
ON CONFLICT(chatId, name) DO UPDATE SET
|
|
11772
11834
|
messageId = EXCLUDED.messageId,
|
|
@@ -11781,26 +11843,26 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11781
11843
|
);
|
|
11782
11844
|
}
|
|
11783
11845
|
async getCheckpoint(chatId, name) {
|
|
11784
|
-
const [checkpoint] = await this.#rows(
|
|
11846
|
+
const [checkpoint] = await this.#database.rows(
|
|
11785
11847
|
storedCheckpoint2,
|
|
11786
|
-
`SELECT * FROM ${this.#
|
|
11848
|
+
`SELECT * FROM ${this.#database.table("checkpoints")} WHERE chatId = $1 AND name = $2`,
|
|
11787
11849
|
[chatId, name]
|
|
11788
11850
|
);
|
|
11789
11851
|
return checkpoint;
|
|
11790
11852
|
}
|
|
11791
11853
|
async listCheckpoints(chatId) {
|
|
11792
|
-
return this.#rows(
|
|
11854
|
+
return this.#database.rows(
|
|
11793
11855
|
checkpointInfo2,
|
|
11794
11856
|
`SELECT id, name, messageId, createdAt
|
|
11795
|
-
FROM ${this.#
|
|
11857
|
+
FROM ${this.#database.table("checkpoints")}
|
|
11796
11858
|
WHERE chatId = $1
|
|
11797
11859
|
ORDER BY createdAt DESC`,
|
|
11798
11860
|
[chatId]
|
|
11799
11861
|
);
|
|
11800
11862
|
}
|
|
11801
11863
|
async deleteCheckpoint(chatId, name) {
|
|
11802
|
-
await this.#query(
|
|
11803
|
-
`DELETE FROM ${this.#
|
|
11864
|
+
await this.#database.query(
|
|
11865
|
+
`DELETE FROM ${this.#database.table("checkpoints")} WHERE chatId = $1 AND name = $2`,
|
|
11804
11866
|
[chatId, name]
|
|
11805
11867
|
);
|
|
11806
11868
|
}
|
|
@@ -11822,8 +11884,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11822
11884
|
ts_rank(fts.content_vector, plainto_tsquery('english', $2)) as rank,
|
|
11823
11885
|
ts_headline('english', fts.content, plainto_tsquery('english', $2),
|
|
11824
11886
|
'StartSel=<mark>, StopSel=</mark>, MaxWords=32, MinWords=5, MaxFragments=1') as snippet
|
|
11825
|
-
FROM ${this.#
|
|
11826
|
-
JOIN ${this.#
|
|
11887
|
+
FROM ${this.#database.table("messages_fts")} fts
|
|
11888
|
+
JOIN ${this.#database.table("messages")} m ON m.id = fts.messageId
|
|
11827
11889
|
WHERE fts.content_vector @@ plainto_tsquery('english', $2)
|
|
11828
11890
|
AND fts.chatId = $1
|
|
11829
11891
|
`;
|
|
@@ -11836,16 +11898,16 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11836
11898
|
}
|
|
11837
11899
|
sql += ` ORDER BY rank DESC LIMIT $${paramIndex}`;
|
|
11838
11900
|
params.push(limit);
|
|
11839
|
-
return this.#rows(searchResult2, sql, params);
|
|
11901
|
+
return this.#database.rows(searchResult2, sql, params);
|
|
11840
11902
|
}
|
|
11841
11903
|
// ==========================================================================
|
|
11842
11904
|
// Visualization Operations
|
|
11843
11905
|
// ==========================================================================
|
|
11844
11906
|
async getGraph(chatId) {
|
|
11845
|
-
const messageRows = await this.#rows(
|
|
11907
|
+
const messageRows = await this.#database.rows(
|
|
11846
11908
|
graphMessageRow2,
|
|
11847
11909
|
`SELECT id, parentId, name, data, createdAt
|
|
11848
|
-
FROM ${this.#
|
|
11910
|
+
FROM ${this.#database.table("messages")}
|
|
11849
11911
|
WHERE chatId = $1
|
|
11850
11912
|
ORDER BY createdAt ASC`,
|
|
11851
11913
|
[chatId]
|
|
@@ -11861,18 +11923,18 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11861
11923
|
createdAt: row.createdat
|
|
11862
11924
|
};
|
|
11863
11925
|
});
|
|
11864
|
-
const branches = await this.#rows(
|
|
11926
|
+
const branches = await this.#database.rows(
|
|
11865
11927
|
graphBranch2,
|
|
11866
11928
|
`SELECT name, headMessageId, isActive
|
|
11867
|
-
FROM ${this.#
|
|
11929
|
+
FROM ${this.#database.table("branches")}
|
|
11868
11930
|
WHERE chatId = $1
|
|
11869
11931
|
ORDER BY createdAt ASC`,
|
|
11870
11932
|
[chatId]
|
|
11871
11933
|
);
|
|
11872
|
-
const checkpoints = await this.#rows(
|
|
11934
|
+
const checkpoints = await this.#database.rows(
|
|
11873
11935
|
graphCheckpoint2,
|
|
11874
11936
|
`SELECT name, messageId
|
|
11875
|
-
FROM ${this.#
|
|
11937
|
+
FROM ${this.#database.table("checkpoints")}
|
|
11876
11938
|
WHERE chatId = $1
|
|
11877
11939
|
ORDER BY createdAt ASC`,
|
|
11878
11940
|
[chatId]
|
|
@@ -11887,8 +11949,8 @@ var PostgresContextStore = class _PostgresContextStore extends ContextStore {
|
|
|
11887
11949
|
};
|
|
11888
11950
|
|
|
11889
11951
|
// packages/context/src/lib/store/sqlserver.store.ts
|
|
11890
|
-
import { createRequire
|
|
11891
|
-
import { z as
|
|
11952
|
+
import { createRequire } from "node:module";
|
|
11953
|
+
import { z as z16 } from "zod";
|
|
11892
11954
|
|
|
11893
11955
|
// packages/context/src/lib/store/ddl.sqlserver.ts
|
|
11894
11956
|
function storeDDL2(schema) {
|
|
@@ -12048,82 +12110,82 @@ async function withSqlServerSchemaLock(mssql, pool, schema, run) {
|
|
|
12048
12110
|
}
|
|
12049
12111
|
|
|
12050
12112
|
// packages/context/src/lib/store/sqlserver.store.ts
|
|
12051
|
-
var chatColumns3 =
|
|
12052
|
-
id:
|
|
12053
|
-
userId:
|
|
12054
|
-
title:
|
|
12055
|
-
metadata:
|
|
12113
|
+
var chatColumns3 = z16.object({
|
|
12114
|
+
id: z16.string(),
|
|
12115
|
+
userId: z16.string(),
|
|
12116
|
+
title: z16.string().nullable(),
|
|
12117
|
+
metadata: z16.string().nullable(),
|
|
12056
12118
|
createdAt: bigintColumn,
|
|
12057
12119
|
updatedAt: bigintColumn
|
|
12058
12120
|
});
|
|
12059
12121
|
var storedChat3 = chatColumns3.transform(toStoredChat3);
|
|
12060
|
-
var chatInfo3 = chatColumns3.extend({ messageCount:
|
|
12122
|
+
var chatInfo3 = chatColumns3.extend({ messageCount: z16.number(), branchCount: z16.number() }).transform((row) => ({
|
|
12061
12123
|
...toStoredChat3(row),
|
|
12062
12124
|
messageCount: row.messageCount,
|
|
12063
12125
|
branchCount: row.branchCount
|
|
12064
12126
|
}));
|
|
12065
|
-
var messageColumns3 =
|
|
12066
|
-
id:
|
|
12067
|
-
chatId:
|
|
12068
|
-
parentId:
|
|
12069
|
-
name:
|
|
12070
|
-
type:
|
|
12071
|
-
data:
|
|
12127
|
+
var messageColumns3 = z16.object({
|
|
12128
|
+
id: z16.string(),
|
|
12129
|
+
chatId: z16.string(),
|
|
12130
|
+
parentId: z16.string().nullable(),
|
|
12131
|
+
name: z16.string(),
|
|
12132
|
+
type: z16.string().nullable(),
|
|
12133
|
+
data: z16.string(),
|
|
12072
12134
|
createdAt: bigintColumn
|
|
12073
12135
|
});
|
|
12074
12136
|
var storedMessage3 = messageColumns3.transform(toMessage3);
|
|
12075
|
-
var searchResult3 = messageColumns3.extend({ rank:
|
|
12137
|
+
var searchResult3 = messageColumns3.extend({ rank: z16.number(), snippet: z16.string() }).transform((row) => ({
|
|
12076
12138
|
message: toMessage3(row),
|
|
12077
12139
|
rank: row.rank,
|
|
12078
12140
|
snippet: row.snippet
|
|
12079
12141
|
}));
|
|
12080
|
-
var storedBranch3 =
|
|
12081
|
-
id:
|
|
12082
|
-
chatId:
|
|
12083
|
-
name:
|
|
12084
|
-
headMessageId:
|
|
12085
|
-
isActive:
|
|
12142
|
+
var storedBranch3 = z16.object({
|
|
12143
|
+
id: z16.string(),
|
|
12144
|
+
chatId: z16.string(),
|
|
12145
|
+
name: z16.string(),
|
|
12146
|
+
headMessageId: z16.string().nullable(),
|
|
12147
|
+
isActive: z16.boolean(),
|
|
12086
12148
|
createdAt: bigintColumn
|
|
12087
12149
|
});
|
|
12088
|
-
var branchRow2 =
|
|
12089
|
-
id:
|
|
12090
|
-
name:
|
|
12091
|
-
headMessageId:
|
|
12092
|
-
isActive:
|
|
12150
|
+
var branchRow2 = z16.object({
|
|
12151
|
+
id: z16.string(),
|
|
12152
|
+
name: z16.string(),
|
|
12153
|
+
headMessageId: z16.string().nullable(),
|
|
12154
|
+
isActive: z16.boolean(),
|
|
12093
12155
|
createdAt: bigintColumn
|
|
12094
12156
|
});
|
|
12095
|
-
var storedCheckpoint3 =
|
|
12096
|
-
id:
|
|
12097
|
-
chatId:
|
|
12098
|
-
name:
|
|
12099
|
-
messageId:
|
|
12157
|
+
var storedCheckpoint3 = z16.object({
|
|
12158
|
+
id: z16.string(),
|
|
12159
|
+
chatId: z16.string(),
|
|
12160
|
+
name: z16.string(),
|
|
12161
|
+
messageId: z16.string(),
|
|
12100
12162
|
createdAt: bigintColumn
|
|
12101
12163
|
});
|
|
12102
|
-
var checkpointInfo3 =
|
|
12103
|
-
id:
|
|
12104
|
-
name:
|
|
12105
|
-
messageId:
|
|
12164
|
+
var checkpointInfo3 = z16.object({
|
|
12165
|
+
id: z16.string(),
|
|
12166
|
+
name: z16.string(),
|
|
12167
|
+
messageId: z16.string(),
|
|
12106
12168
|
createdAt: bigintColumn
|
|
12107
12169
|
});
|
|
12108
|
-
var idRow3 =
|
|
12109
|
-
var hasChildrenRow2 =
|
|
12110
|
-
var countRow2 =
|
|
12111
|
-
var ftsInstalledRow =
|
|
12112
|
-
var graphMessageRow3 =
|
|
12113
|
-
id:
|
|
12114
|
-
parentId:
|
|
12115
|
-
name:
|
|
12116
|
-
data:
|
|
12170
|
+
var idRow3 = z16.object({ id: z16.string() });
|
|
12171
|
+
var hasChildrenRow2 = z16.object({ hasChildren: z16.number() });
|
|
12172
|
+
var countRow2 = z16.object({ count: z16.number() });
|
|
12173
|
+
var ftsInstalledRow = z16.object({ ftsInstalled: z16.number().nullable() });
|
|
12174
|
+
var graphMessageRow3 = z16.object({
|
|
12175
|
+
id: z16.string(),
|
|
12176
|
+
parentId: z16.string().nullable(),
|
|
12177
|
+
name: z16.string(),
|
|
12178
|
+
data: z16.string(),
|
|
12117
12179
|
createdAt: bigintColumn
|
|
12118
12180
|
});
|
|
12119
|
-
var graphBranch3 =
|
|
12120
|
-
name:
|
|
12121
|
-
headMessageId:
|
|
12122
|
-
isActive:
|
|
12181
|
+
var graphBranch3 = z16.object({
|
|
12182
|
+
name: z16.string(),
|
|
12183
|
+
headMessageId: z16.string().nullable(),
|
|
12184
|
+
isActive: z16.boolean()
|
|
12123
12185
|
});
|
|
12124
|
-
var graphCheckpoint3 =
|
|
12125
|
-
name:
|
|
12126
|
-
messageId:
|
|
12186
|
+
var graphCheckpoint3 = z16.object({
|
|
12187
|
+
name: z16.string(),
|
|
12188
|
+
messageId: z16.string()
|
|
12127
12189
|
});
|
|
12128
12190
|
function toStoredChat3(row) {
|
|
12129
12191
|
return {
|
|
@@ -12168,7 +12230,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
12168
12230
|
}
|
|
12169
12231
|
static #requireMssql() {
|
|
12170
12232
|
try {
|
|
12171
|
-
const require2 =
|
|
12233
|
+
const require2 = createRequire(import.meta.url);
|
|
12172
12234
|
return require2("mssql");
|
|
12173
12235
|
} catch {
|
|
12174
12236
|
throw new Error(
|
|
@@ -12251,7 +12313,7 @@ var SqlServerContextStore = class _SqlServerContextStore extends ContextStore {
|
|
|
12251
12313
|
* Execute a query using the pool and parse every returned row.
|
|
12252
12314
|
*/
|
|
12253
12315
|
async #rows(row, sql, params) {
|
|
12254
|
-
return
|
|
12316
|
+
return z16.array(row).parse(await this.#query(sql, params));
|
|
12255
12317
|
}
|
|
12256
12318
|
/**
|
|
12257
12319
|
* Close the pool connection.
|
|
@@ -13108,9 +13170,8 @@ function staticPartText(part, options) {
|
|
|
13108
13170
|
|
|
13109
13171
|
// packages/context/src/lib/stream/postgres-notify-change-source.ts
|
|
13110
13172
|
import { EventEmitter, on } from "node:events";
|
|
13111
|
-
import { createRequire as createRequire3 } from "node:module";
|
|
13112
13173
|
import pMemoize3 from "p-memoize";
|
|
13113
|
-
import { z as
|
|
13174
|
+
import { z as z17 } from "zod";
|
|
13114
13175
|
|
|
13115
13176
|
// packages/context/src/lib/stream/ddl.stream.postgres-notify.ts
|
|
13116
13177
|
var DEFAULT_POSTGRES_STREAM_CHANGES_CHANNEL = "deepagents_stream_changes";
|
|
@@ -13215,7 +13276,6 @@ var PostgresNotifyChangeSource = class _PostgresNotifyChangeSource {
|
|
|
13215
13276
|
#pool;
|
|
13216
13277
|
#schema;
|
|
13217
13278
|
#channel;
|
|
13218
|
-
#ownsPool;
|
|
13219
13279
|
/** Aborts when close() starts: every subscription ends, and no LISTEN connection opens. */
|
|
13220
13280
|
#closed = new AbortController();
|
|
13221
13281
|
/** Emits `stream:<id>` with a ChangeDelivery; `error` fails every subscription. */
|
|
@@ -13246,39 +13306,17 @@ var PostgresNotifyChangeSource = class _PostgresNotifyChangeSource {
|
|
|
13246
13306
|
assertIdentifier(channel, "channel");
|
|
13247
13307
|
this.#schema = schema;
|
|
13248
13308
|
this.#channel = channel;
|
|
13249
|
-
|
|
13250
|
-
if (options.pool instanceof pg.Pool) {
|
|
13251
|
-
this.#pool = options.pool;
|
|
13252
|
-
this.#ownsPool = false;
|
|
13253
|
-
} else {
|
|
13254
|
-
this.#pool = typeof options.pool === "string" ? new pg.Pool({ connectionString: options.pool }) : new pg.Pool(options.pool);
|
|
13255
|
-
this.#ownsPool = true;
|
|
13256
|
-
}
|
|
13257
|
-
}
|
|
13258
|
-
static #requirePg() {
|
|
13259
|
-
try {
|
|
13260
|
-
const require2 = createRequire3(import.meta.url);
|
|
13261
|
-
return require2("pg");
|
|
13262
|
-
} catch {
|
|
13263
|
-
throw new Error(
|
|
13264
|
-
'PostgresNotifyChangeSource requires the "pg" package. Install it with: npm install pg'
|
|
13265
|
-
);
|
|
13266
|
-
}
|
|
13309
|
+
this.#pool = options.pool;
|
|
13267
13310
|
}
|
|
13268
13311
|
/** Opens the source: installs the notify triggers before it returns. */
|
|
13269
13312
|
static async create(options) {
|
|
13270
13313
|
const source = new _PostgresNotifyChangeSource(options);
|
|
13271
|
-
|
|
13272
|
-
|
|
13273
|
-
|
|
13274
|
-
|
|
13275
|
-
|
|
13276
|
-
|
|
13277
|
-
);
|
|
13278
|
-
} catch (error) {
|
|
13279
|
-
await source.close();
|
|
13280
|
-
throw error;
|
|
13281
|
-
}
|
|
13314
|
+
await source.#pool.query(
|
|
13315
|
+
lockSchemaDDL(
|
|
13316
|
+
postgresStreamNotifyDDL(source.#schema, source.#channel),
|
|
13317
|
+
source.#schema
|
|
13318
|
+
)
|
|
13319
|
+
);
|
|
13282
13320
|
return source;
|
|
13283
13321
|
}
|
|
13284
13322
|
async *subscribe(streamId, signal) {
|
|
@@ -13314,9 +13352,6 @@ var PostgresNotifyChangeSource = class _PostgresNotifyChangeSource {
|
|
|
13314
13352
|
} catch {
|
|
13315
13353
|
}
|
|
13316
13354
|
await this.#releaseListener();
|
|
13317
|
-
if (this.#ownsPool) {
|
|
13318
|
-
await this.#pool.end();
|
|
13319
|
-
}
|
|
13320
13355
|
}
|
|
13321
13356
|
async #ensureListening() {
|
|
13322
13357
|
if (this.#listener instanceof Listening) return;
|
|
@@ -13368,10 +13403,10 @@ var PostgresNotifyChangeSource = class _PostgresNotifyChangeSource {
|
|
|
13368
13403
|
await listener.release(this.#channel);
|
|
13369
13404
|
}
|
|
13370
13405
|
};
|
|
13371
|
-
var notificationPayload =
|
|
13372
|
-
schema:
|
|
13373
|
-
streamId:
|
|
13374
|
-
kind:
|
|
13406
|
+
var notificationPayload = z17.object({
|
|
13407
|
+
schema: z17.string(),
|
|
13408
|
+
streamId: z17.string(),
|
|
13409
|
+
kind: z17.string()
|
|
13375
13410
|
});
|
|
13376
13411
|
function parsePayload(payload) {
|
|
13377
13412
|
try {
|
|
@@ -13391,8 +13426,7 @@ function assertIdentifier(value, label) {
|
|
|
13391
13426
|
}
|
|
13392
13427
|
|
|
13393
13428
|
// packages/context/src/lib/stream/postgres.stream-store.ts
|
|
13394
|
-
import {
|
|
13395
|
-
import { z as z18 } from "zod";
|
|
13429
|
+
import { z as z19 } from "zod";
|
|
13396
13430
|
|
|
13397
13431
|
// packages/context/src/lib/stream/ddl.stream.postgres.ts
|
|
13398
13432
|
function postgresStreamDDL(schema) {
|
|
@@ -13430,8 +13464,8 @@ CREATE INDEX IF NOT EXISTS "idx_${schema}_streams_status_created_at_id"
|
|
|
13430
13464
|
// packages/context/src/lib/stream/rows.ts
|
|
13431
13465
|
import { validateTypes } from "@ai-sdk/provider-utils";
|
|
13432
13466
|
import { uiMessageChunkSchema } from "ai";
|
|
13433
|
-
import { z as
|
|
13434
|
-
var streamStatus =
|
|
13467
|
+
import { z as z18 } from "zod";
|
|
13468
|
+
var streamStatus = z18.enum([
|
|
13435
13469
|
"queued",
|
|
13436
13470
|
"running",
|
|
13437
13471
|
"completed",
|
|
@@ -13443,14 +13477,14 @@ function toStreamPart(value) {
|
|
|
13443
13477
|
}
|
|
13444
13478
|
|
|
13445
13479
|
// packages/context/src/lib/stream/postgres.stream-store.ts
|
|
13446
|
-
var storedStream =
|
|
13447
|
-
id:
|
|
13480
|
+
var storedStream = z19.object({
|
|
13481
|
+
id: z19.string(),
|
|
13448
13482
|
status: streamStatus,
|
|
13449
13483
|
created_at: bigintColumn,
|
|
13450
13484
|
started_at: bigintColumn.nullable(),
|
|
13451
13485
|
finished_at: bigintColumn.nullable(),
|
|
13452
13486
|
cancel_requested_at: bigintColumn.nullable(),
|
|
13453
|
-
error:
|
|
13487
|
+
error: z19.string().nullable()
|
|
13454
13488
|
}).transform((row) => ({
|
|
13455
13489
|
id: row.id,
|
|
13456
13490
|
status: row.status,
|
|
@@ -13460,85 +13494,44 @@ var storedStream = z18.object({
|
|
|
13460
13494
|
cancelRequestedAt: row.cancel_requested_at,
|
|
13461
13495
|
error: row.error
|
|
13462
13496
|
}));
|
|
13463
|
-
var statusRow =
|
|
13464
|
-
var idRow4 =
|
|
13465
|
-
var chunkRow =
|
|
13466
|
-
stream_id:
|
|
13467
|
-
seq:
|
|
13468
|
-
data:
|
|
13497
|
+
var statusRow = z19.object({ status: streamStatus });
|
|
13498
|
+
var idRow4 = z19.object({ id: z19.string() });
|
|
13499
|
+
var chunkRow = z19.object({
|
|
13500
|
+
stream_id: z19.string(),
|
|
13501
|
+
seq: z19.number(),
|
|
13502
|
+
data: z19.unknown(),
|
|
13469
13503
|
created_at: bigintColumn
|
|
13470
13504
|
});
|
|
13471
13505
|
var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
13472
|
-
#
|
|
13473
|
-
|
|
13474
|
-
#ownsPool;
|
|
13475
|
-
constructor(options) {
|
|
13506
|
+
#database;
|
|
13507
|
+
constructor(database) {
|
|
13476
13508
|
super();
|
|
13477
|
-
|
|
13478
|
-
assertIdentifier2(schema, "schema");
|
|
13479
|
-
this.#schema = schema;
|
|
13480
|
-
const pg = _PostgresStreamStore.#requirePg();
|
|
13481
|
-
if (options.pool instanceof pg.Pool) {
|
|
13482
|
-
this.#pool = options.pool;
|
|
13483
|
-
this.#ownsPool = false;
|
|
13484
|
-
} else {
|
|
13485
|
-
this.#pool = typeof options.pool === "string" ? new pg.Pool({ connectionString: options.pool }) : new pg.Pool(options.pool);
|
|
13486
|
-
this.#ownsPool = true;
|
|
13487
|
-
}
|
|
13488
|
-
}
|
|
13489
|
-
static #requirePg() {
|
|
13490
|
-
try {
|
|
13491
|
-
const require2 = createRequire4(import.meta.url);
|
|
13492
|
-
return require2("pg");
|
|
13493
|
-
} catch {
|
|
13494
|
-
throw new Error(
|
|
13495
|
-
'PostgresStreamStore requires the "pg" package. Install it with: npm install pg'
|
|
13496
|
-
);
|
|
13497
|
-
}
|
|
13498
|
-
}
|
|
13499
|
-
#t(name) {
|
|
13500
|
-
return `"${this.#schema}"."${name}"`;
|
|
13509
|
+
this.#database = database;
|
|
13501
13510
|
}
|
|
13502
13511
|
/** Opens the store: creates its tables before it returns. */
|
|
13503
13512
|
static async create(options) {
|
|
13504
|
-
const
|
|
13505
|
-
|
|
13506
|
-
|
|
13507
|
-
} catch (error) {
|
|
13508
|
-
await store.close();
|
|
13509
|
-
throw error;
|
|
13510
|
-
}
|
|
13511
|
-
return store;
|
|
13512
|
-
}
|
|
13513
|
-
async #initialize() {
|
|
13514
|
-
await this.#pool.query(
|
|
13515
|
-
lockSchemaDDL(postgresStreamDDL(this.#schema), this.#schema)
|
|
13513
|
+
const database = new PostgresStoreDatabase(options.pool, options.schema);
|
|
13514
|
+
await database.query(
|
|
13515
|
+
lockSchemaDDL(postgresStreamDDL(database.schema), database.schema)
|
|
13516
13516
|
);
|
|
13517
|
+
return new _PostgresStreamStore(database);
|
|
13517
13518
|
}
|
|
13518
|
-
|
|
13519
|
-
|
|
13520
|
-
|
|
13521
|
-
}
|
|
13522
|
-
}
|
|
13523
|
-
async #query(sql, params) {
|
|
13524
|
-
const result = await this.#pool.query(sql, params);
|
|
13525
|
-
return result.rows;
|
|
13526
|
-
}
|
|
13527
|
-
async #rows(row, sql, params) {
|
|
13528
|
-
return z18.array(row).parse(await this.#query(sql, params));
|
|
13519
|
+
/** A view of this store whose reads and writes run inside `tx`. */
|
|
13520
|
+
bind(tx) {
|
|
13521
|
+
return new _PostgresStreamStore(this.#database.bind(tx));
|
|
13529
13522
|
}
|
|
13530
13523
|
async createStream(stream) {
|
|
13531
|
-
await this.#query(
|
|
13532
|
-
`INSERT INTO ${this.#
|
|
13524
|
+
await this.#database.query(
|
|
13525
|
+
`INSERT INTO ${this.#database.table("streams")}
|
|
13533
13526
|
(id, status, created_at, started_at, finished_at, cancel_requested_at, error)
|
|
13534
13527
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
|
13535
13528
|
streamParams(stream)
|
|
13536
13529
|
);
|
|
13537
13530
|
}
|
|
13538
13531
|
async upsertStream(stream) {
|
|
13539
|
-
const [created] = await this.#rows(
|
|
13532
|
+
const [created] = await this.#database.rows(
|
|
13540
13533
|
storedStream,
|
|
13541
|
-
`INSERT INTO ${this.#
|
|
13534
|
+
`INSERT INTO ${this.#database.table("streams")}
|
|
13542
13535
|
(id, status, created_at, started_at, finished_at, cancel_requested_at, error)
|
|
13543
13536
|
VALUES ($1, $2, $3, $4, $5, $6, $7)
|
|
13544
13537
|
ON CONFLICT(id) DO NOTHING
|
|
@@ -13557,36 +13550,36 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13557
13550
|
return { stream: existing, created: false };
|
|
13558
13551
|
}
|
|
13559
13552
|
async getStream(streamId) {
|
|
13560
|
-
const [found] = await this.#rows(
|
|
13553
|
+
const [found] = await this.#database.rows(
|
|
13561
13554
|
storedStream,
|
|
13562
|
-
`SELECT * FROM ${this.#
|
|
13555
|
+
`SELECT * FROM ${this.#database.table("streams")} WHERE id = $1`,
|
|
13563
13556
|
[streamId]
|
|
13564
13557
|
);
|
|
13565
13558
|
return found;
|
|
13566
13559
|
}
|
|
13567
13560
|
async getStreamStatus(streamId) {
|
|
13568
|
-
const rows = await this.#rows(
|
|
13561
|
+
const rows = await this.#database.rows(
|
|
13569
13562
|
statusRow,
|
|
13570
|
-
`SELECT status FROM ${this.#
|
|
13563
|
+
`SELECT status FROM ${this.#database.table("streams")} WHERE id = $1`,
|
|
13571
13564
|
[streamId]
|
|
13572
13565
|
);
|
|
13573
13566
|
return rows[0]?.status;
|
|
13574
13567
|
}
|
|
13575
13568
|
async listStreamIds(options) {
|
|
13576
13569
|
const params = [];
|
|
13577
|
-
let sql = `SELECT id FROM ${this.#
|
|
13570
|
+
let sql = `SELECT id FROM ${this.#database.table("streams")}`;
|
|
13578
13571
|
if (options?.status) {
|
|
13579
13572
|
params.push(options.status);
|
|
13580
13573
|
sql += ` WHERE status = $${params.length}`;
|
|
13581
13574
|
}
|
|
13582
13575
|
sql += " ORDER BY created_at ASC, id ASC";
|
|
13583
|
-
const rows = await this.#rows(idRow4, sql, params);
|
|
13576
|
+
const rows = await this.#database.rows(idRow4, sql, params);
|
|
13584
13577
|
return rows.map((row) => row.id);
|
|
13585
13578
|
}
|
|
13586
13579
|
async updateStream(streamId, update) {
|
|
13587
|
-
return
|
|
13588
|
-
const selected = await
|
|
13589
|
-
`SELECT * FROM ${this.#
|
|
13580
|
+
return this.#database.transaction(async (tx) => {
|
|
13581
|
+
const selected = await tx.query(
|
|
13582
|
+
`SELECT * FROM ${this.#database.table("streams")} WHERE id = $1 FOR UPDATE`,
|
|
13590
13583
|
[streamId]
|
|
13591
13584
|
);
|
|
13592
13585
|
const stream = storedStream.optional().parse(selected.rows[0]);
|
|
@@ -13612,8 +13605,8 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13612
13605
|
if (updates.error !== void 0) set("error", updates.error);
|
|
13613
13606
|
if (setClauses.length === 0) return { stream, updated: false };
|
|
13614
13607
|
params.push(streamId);
|
|
13615
|
-
const updated = await
|
|
13616
|
-
`UPDATE ${this.#
|
|
13608
|
+
const updated = await tx.query(
|
|
13609
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13617
13610
|
SET ${setClauses.join(", ")}
|
|
13618
13611
|
WHERE id = $${params.length}
|
|
13619
13612
|
RETURNING *`,
|
|
@@ -13626,40 +13619,40 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13626
13619
|
const now = Date.now();
|
|
13627
13620
|
switch (status) {
|
|
13628
13621
|
case "running":
|
|
13629
|
-
await this.#query(
|
|
13630
|
-
`UPDATE ${this.#
|
|
13622
|
+
await this.#database.query(
|
|
13623
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13631
13624
|
SET status = $1, started_at = $2
|
|
13632
13625
|
WHERE id = $3 AND status = 'queued'`,
|
|
13633
13626
|
[status, now, streamId]
|
|
13634
13627
|
);
|
|
13635
13628
|
break;
|
|
13636
13629
|
case "completed":
|
|
13637
|
-
await this.#query(
|
|
13638
|
-
`UPDATE ${this.#
|
|
13630
|
+
await this.#database.query(
|
|
13631
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13639
13632
|
SET status = $1, finished_at = $2
|
|
13640
13633
|
WHERE id = $3 AND status IN ('queued', 'running')`,
|
|
13641
13634
|
[status, now, streamId]
|
|
13642
13635
|
);
|
|
13643
13636
|
break;
|
|
13644
13637
|
case "failed":
|
|
13645
|
-
await this.#query(
|
|
13646
|
-
`UPDATE ${this.#
|
|
13638
|
+
await this.#database.query(
|
|
13639
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13647
13640
|
SET status = $1, finished_at = $2, error = $3
|
|
13648
13641
|
WHERE id = $4 AND status IN ('queued', 'running')`,
|
|
13649
13642
|
[status, now, options?.error ?? null, streamId]
|
|
13650
13643
|
);
|
|
13651
13644
|
break;
|
|
13652
13645
|
case "cancelled":
|
|
13653
|
-
await this.#query(
|
|
13654
|
-
`UPDATE ${this.#
|
|
13646
|
+
await this.#database.query(
|
|
13647
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13655
13648
|
SET status = $1, cancel_requested_at = $2, finished_at = $3
|
|
13656
13649
|
WHERE id = $4 AND status IN ('queued', 'running')`,
|
|
13657
13650
|
[status, now, now, streamId]
|
|
13658
13651
|
);
|
|
13659
13652
|
break;
|
|
13660
13653
|
default:
|
|
13661
|
-
await this.#query(
|
|
13662
|
-
`UPDATE ${this.#
|
|
13654
|
+
await this.#database.query(
|
|
13655
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13663
13656
|
SET status = $1
|
|
13664
13657
|
WHERE id = $2 AND status = 'queued'`,
|
|
13665
13658
|
[status, streamId]
|
|
@@ -13675,28 +13668,28 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13675
13668
|
created_at: chunk.createdAt
|
|
13676
13669
|
}));
|
|
13677
13670
|
const params = [JSON.stringify(rows)];
|
|
13678
|
-
const insertSql = `INSERT INTO ${this.#
|
|
13671
|
+
const insertSql = `INSERT INTO ${this.#database.table("stream_chunks")} (stream_id, seq, data, created_at)
|
|
13679
13672
|
SELECT stream_id, seq, data, created_at
|
|
13680
13673
|
FROM jsonb_to_recordset($1::jsonb)
|
|
13681
13674
|
AS rows(stream_id TEXT, seq INTEGER, data JSONB, created_at BIGINT)`;
|
|
13682
13675
|
const failures = collectStreamFailures(chunks);
|
|
13683
13676
|
if (failures.length === 0) {
|
|
13684
|
-
await this.#query(insertSql, params);
|
|
13677
|
+
await this.#database.query(insertSql, params);
|
|
13685
13678
|
return;
|
|
13686
13679
|
}
|
|
13687
|
-
await
|
|
13688
|
-
await
|
|
13680
|
+
await this.#database.transaction(async (tx) => {
|
|
13681
|
+
await tx.query(insertSql, params);
|
|
13689
13682
|
const failedAt = Date.now();
|
|
13690
13683
|
for (const failure of failures) {
|
|
13691
|
-
const result = await
|
|
13692
|
-
`UPDATE ${this.#
|
|
13684
|
+
const result = await tx.query(
|
|
13685
|
+
`UPDATE ${this.#database.table("streams")}
|
|
13693
13686
|
SET status = $1, finished_at = $2, error = $3
|
|
13694
13687
|
WHERE id = $4 AND status IN ('queued', 'running')`,
|
|
13695
13688
|
["failed", failedAt, failure.error, failure.streamId]
|
|
13696
13689
|
);
|
|
13697
13690
|
if (result.rowCount !== 1) {
|
|
13698
|
-
const existing = await
|
|
13699
|
-
`SELECT id FROM ${this.#
|
|
13691
|
+
const existing = await tx.query(
|
|
13692
|
+
`SELECT id FROM ${this.#database.table("streams")} WHERE id = $1`,
|
|
13700
13693
|
[failure.streamId]
|
|
13701
13694
|
);
|
|
13702
13695
|
if (existing.rowCount !== 1) {
|
|
@@ -13708,7 +13701,7 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13708
13701
|
}
|
|
13709
13702
|
async getChunks(streamId, fromSeq, limit) {
|
|
13710
13703
|
const params = [streamId];
|
|
13711
|
-
let sql = `SELECT * FROM ${this.#
|
|
13704
|
+
let sql = `SELECT * FROM ${this.#database.table("stream_chunks")} WHERE stream_id = $1`;
|
|
13712
13705
|
if (fromSeq !== void 0) {
|
|
13713
13706
|
params.push(fromSeq);
|
|
13714
13707
|
sql += ` AND seq >= $${params.length}`;
|
|
@@ -13718,7 +13711,7 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13718
13711
|
params.push(limit);
|
|
13719
13712
|
sql += ` LIMIT $${params.length}`;
|
|
13720
13713
|
}
|
|
13721
|
-
const rows = await this.#rows(chunkRow, sql, params);
|
|
13714
|
+
const rows = await this.#database.rows(chunkRow, sql, params);
|
|
13722
13715
|
return Promise.all(
|
|
13723
13716
|
rows.map(async (row) => ({
|
|
13724
13717
|
streamId: row.stream_id,
|
|
@@ -13729,14 +13722,15 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13729
13722
|
);
|
|
13730
13723
|
}
|
|
13731
13724
|
async deleteStream(streamId) {
|
|
13732
|
-
await this.#query(
|
|
13733
|
-
|
|
13734
|
-
|
|
13725
|
+
await this.#database.query(
|
|
13726
|
+
`DELETE FROM ${this.#database.table("streams")} WHERE id = $1`,
|
|
13727
|
+
[streamId]
|
|
13728
|
+
);
|
|
13735
13729
|
}
|
|
13736
13730
|
async reopenStream(streamId) {
|
|
13737
|
-
return
|
|
13738
|
-
const result = await
|
|
13739
|
-
`SELECT * FROM ${this.#
|
|
13731
|
+
return this.#database.transaction(async (tx) => {
|
|
13732
|
+
const result = await tx.query(
|
|
13733
|
+
`SELECT * FROM ${this.#database.table("streams")} WHERE id = $1 FOR UPDATE`,
|
|
13740
13734
|
[streamId]
|
|
13741
13735
|
);
|
|
13742
13736
|
const row = statusRow.optional().parse(result.rows[0]);
|
|
@@ -13748,9 +13742,10 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13748
13742
|
`Cannot reopen stream "${streamId}" with status "${row.status}". Only terminal streams can be reopened.`
|
|
13749
13743
|
);
|
|
13750
13744
|
}
|
|
13751
|
-
await
|
|
13752
|
-
|
|
13753
|
-
|
|
13745
|
+
await tx.query(
|
|
13746
|
+
`DELETE FROM ${this.#database.table("streams")} WHERE id = $1`,
|
|
13747
|
+
[streamId]
|
|
13748
|
+
);
|
|
13754
13749
|
const now = Date.now();
|
|
13755
13750
|
const stream = {
|
|
13756
13751
|
id: streamId,
|
|
@@ -13761,8 +13756,8 @@ var PostgresStreamStore = class _PostgresStreamStore extends StreamStore {
|
|
|
13761
13756
|
cancelRequestedAt: null,
|
|
13762
13757
|
error: null
|
|
13763
13758
|
};
|
|
13764
|
-
await
|
|
13765
|
-
`INSERT INTO ${this.#
|
|
13759
|
+
await tx.query(
|
|
13760
|
+
`INSERT INTO ${this.#database.table("streams")}
|
|
13766
13761
|
(id, status, created_at, started_at, finished_at, cancel_requested_at, error)
|
|
13767
13762
|
VALUES ($1, $2, $3, $4, $5, $6, $7)`,
|
|
13768
13763
|
streamParams(stream)
|
|
@@ -13785,17 +13780,12 @@ function streamParams(stream) {
|
|
|
13785
13780
|
function isTerminal2(status) {
|
|
13786
13781
|
return status !== "queued" && status !== "running";
|
|
13787
13782
|
}
|
|
13788
|
-
function assertIdentifier2(value, label) {
|
|
13789
|
-
if (!/^[a-zA-Z_]\w*$/.test(value)) {
|
|
13790
|
-
throw new Error(`Invalid ${label} name: "${value}"`);
|
|
13791
|
-
}
|
|
13792
|
-
}
|
|
13793
13783
|
|
|
13794
13784
|
// packages/context/src/lib/stream/sqlite.stream-store.ts
|
|
13795
13785
|
import { Declaration as Declaration2 } from "@zukhruf/sqlite-drift";
|
|
13796
13786
|
import { open as open2 } from "@zukhruf/sqlite-open";
|
|
13797
13787
|
import { transaction as transaction2 } from "@zukhruf/sqlite-transaction";
|
|
13798
|
-
import { z as
|
|
13788
|
+
import { z as z20 } from "zod";
|
|
13799
13789
|
|
|
13800
13790
|
// packages/context/src/lib/stream/ddl.stream.sqlite.ts
|
|
13801
13791
|
var SETUP2 = `
|
|
@@ -13830,22 +13820,22 @@ var statements2 = [
|
|
|
13830
13820
|
|
|
13831
13821
|
// packages/context/src/lib/stream/sqlite.stream-store.ts
|
|
13832
13822
|
var tables2 = new Declaration2(statements2, { rows: "keep" });
|
|
13833
|
-
var streamRow =
|
|
13834
|
-
id:
|
|
13823
|
+
var streamRow = z20.object({
|
|
13824
|
+
id: z20.string(),
|
|
13835
13825
|
status: streamStatus,
|
|
13836
|
-
createdAt:
|
|
13837
|
-
startedAt:
|
|
13838
|
-
finishedAt:
|
|
13839
|
-
cancelRequestedAt:
|
|
13840
|
-
error:
|
|
13826
|
+
createdAt: z20.number(),
|
|
13827
|
+
startedAt: z20.number().nullable(),
|
|
13828
|
+
finishedAt: z20.number().nullable(),
|
|
13829
|
+
cancelRequestedAt: z20.number().nullable(),
|
|
13830
|
+
error: z20.string().nullable()
|
|
13841
13831
|
});
|
|
13842
|
-
var statusRow2 =
|
|
13843
|
-
var idRow5 =
|
|
13844
|
-
var chunkRow2 =
|
|
13845
|
-
streamId:
|
|
13846
|
-
seq:
|
|
13847
|
-
data:
|
|
13848
|
-
createdAt:
|
|
13832
|
+
var statusRow2 = z20.object({ status: streamStatus });
|
|
13833
|
+
var idRow5 = z20.object({ id: z20.string() });
|
|
13834
|
+
var chunkRow2 = z20.object({
|
|
13835
|
+
streamId: z20.string(),
|
|
13836
|
+
seq: z20.number(),
|
|
13837
|
+
data: z20.string(),
|
|
13838
|
+
createdAt: z20.number()
|
|
13849
13839
|
});
|
|
13850
13840
|
var SqliteStreamStore = class extends StreamStore {
|
|
13851
13841
|
#db;
|
|
@@ -13891,7 +13881,7 @@ var SqliteStreamStore = class extends StreamStore {
|
|
|
13891
13881
|
return row?.status;
|
|
13892
13882
|
}
|
|
13893
13883
|
async listStreamIds(options) {
|
|
13894
|
-
const rows =
|
|
13884
|
+
const rows = z20.array(idRow5).parse(
|
|
13895
13885
|
options?.status ? this.#sql.all`SELECT id FROM streams WHERE status = ${options.status} ORDER BY createdAt ASC, id ASC` : this.#sql.all`SELECT id FROM streams ORDER BY createdAt ASC, id ASC`
|
|
13896
13886
|
);
|
|
13897
13887
|
return rows.map((row) => row.id);
|
|
@@ -13984,7 +13974,7 @@ var SqliteStreamStore = class extends StreamStore {
|
|
|
13984
13974
|
);
|
|
13985
13975
|
}
|
|
13986
13976
|
async getChunks(streamId, fromSeq, limit) {
|
|
13987
|
-
const rows =
|
|
13977
|
+
const rows = z20.array(chunkRow2).parse(this.#chunkRows(streamId, fromSeq, limit));
|
|
13988
13978
|
return Promise.all(
|
|
13989
13979
|
rows.map(async (row) => ({
|
|
13990
13980
|
streamId: row.streamId,
|
|
@@ -14499,7 +14489,9 @@ export {
|
|
|
14499
14489
|
PollingChangeSource,
|
|
14500
14490
|
PostgresContextStore,
|
|
14501
14491
|
PostgresNotifyChangeSource,
|
|
14492
|
+
PostgresStoreDatabase,
|
|
14502
14493
|
PostgresStreamStore,
|
|
14494
|
+
PostgresTransaction,
|
|
14503
14495
|
RuntimeStrategy,
|
|
14504
14496
|
SearchQuerySyntaxError,
|
|
14505
14497
|
SqlServerContextStore,
|