@deepagents/experimental 7.8.2 → 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/README.md +27 -10
- package/dist/index.js +601 -54
- package/dist/index.js.map +4 -4
- package/dist/zukhruf/index.d.ts +4 -0
- package/dist/zukhruf/index.d.ts.map +1 -1
- package/dist/zukhruf/index.js +601 -54
- package/dist/zukhruf/index.js.map +4 -4
- package/dist/zukhruf/mailbox/postgres.store.d.ts +9 -1
- package/dist/zukhruf/mailbox/postgres.store.d.ts.map +1 -1
- package/dist/zukhruf/mailbox/sqlite.store.d.ts +3 -1
- package/dist/zukhruf/mailbox/sqlite.store.d.ts.map +1 -1
- package/dist/zukhruf/mailbox/store.d.ts +4 -0
- package/dist/zukhruf/mailbox/store.d.ts.map +1 -1
- package/dist/zukhruf/plugins/conversation-scheduling/index.js.map +1 -1
- package/dist/zukhruf/plugins/schedules/index.js.map +1 -1
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts +14 -1
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts.map +1 -1
- package/dist/zukhruf/queue/turn-queue.d.ts +2 -0
- package/dist/zukhruf/queue/turn-queue.d.ts.map +1 -1
- package/dist/zukhruf/runtime/agent-runtime.d.ts +6 -7
- package/dist/zukhruf/runtime/agent-runtime.d.ts.map +1 -1
- package/dist/zukhruf/storage/agent-storage.d.ts +41 -0
- package/dist/zukhruf/storage/agent-storage.d.ts.map +1 -0
- package/dist/zukhruf/storage/embedded-agent-storage.d.ts +63 -0
- package/dist/zukhruf/storage/embedded-agent-storage.d.ts.map +1 -0
- package/dist/zukhruf/storage/postgres-agent-storage.d.ts +56 -0
- package/dist/zukhruf/storage/postgres-agent-storage.d.ts.map +1 -0
- package/dist/zukhruf/storage/sqlite-admission-gate.d.ts +29 -0
- package/dist/zukhruf/storage/sqlite-admission-gate.d.ts.map +1 -0
- package/dist/zukhruf/storage/turn-outbox.d.ts +65 -0
- package/dist/zukhruf/storage/turn-outbox.d.ts.map +1 -0
- package/package.json +6 -6
package/dist/index.js
CHANGED
|
@@ -320,19 +320,25 @@ var toolOutputMatches = (pattern) => toolCall({
|
|
|
320
320
|
// packages/experimental/src/zukhruf/index.ts
|
|
321
321
|
var zukhruf_exports = {};
|
|
322
322
|
__export(zukhruf_exports, {
|
|
323
|
+
AdmissionScopeError: () => AdmissionScopeError,
|
|
323
324
|
AgentObservation: () => AgentObservation,
|
|
324
325
|
AgentPath: () => AgentPath,
|
|
325
326
|
AgentPluginCapability: () => AgentPluginCapability,
|
|
326
327
|
AgentRuntime: () => AgentRuntime,
|
|
328
|
+
AgentStorage: () => AgentStorage,
|
|
329
|
+
AgentStorageDatabaseMismatchError: () => AgentStorageDatabaseMismatchError,
|
|
327
330
|
AgentThread: () => AgentThread,
|
|
328
331
|
AgentThreadLimitError: () => AgentThreadLimitError,
|
|
329
332
|
ConversationNotFoundError: () => ConversationNotFoundError,
|
|
330
333
|
DEFAULT_CONVERSATION_STATUS_CHANNEL: () => DEFAULT_CONVERSATION_STATUS_CHANNEL,
|
|
334
|
+
EmbeddedAgentStorage: () => EmbeddedAgentStorage,
|
|
335
|
+
EmbeddedAgentStorageInUseError: () => EmbeddedAgentStorageInUseError,
|
|
331
336
|
InterAgentCommunicationTypes: () => InterAgentCommunicationTypes,
|
|
332
337
|
MailboxStore: () => MailboxStore,
|
|
333
338
|
MessageDeliveryModes: () => MessageDeliveryModes,
|
|
334
339
|
PgBossConversationStatusChangeSource: () => PgBossConversationStatusChangeSource,
|
|
335
340
|
PgBossTurnQueue: () => PgBossTurnQueue,
|
|
341
|
+
PostgresAgentStorage: () => PostgresAgentStorage,
|
|
336
342
|
PostgresMailboxStore: () => PostgresMailboxStore,
|
|
337
343
|
SqliteMailboxStore: () => SqliteMailboxStore,
|
|
338
344
|
StaleTurnError: () => StaleTurnError,
|
|
@@ -581,7 +587,10 @@ function defineInstructions(...fragments) {
|
|
|
581
587
|
|
|
582
588
|
// packages/experimental/src/zukhruf/mailbox/postgres.store.ts
|
|
583
589
|
import { z as z2 } from "zod";
|
|
584
|
-
import {
|
|
590
|
+
import {
|
|
591
|
+
PostgresStoreDatabase,
|
|
592
|
+
lockSchemaDDL
|
|
593
|
+
} from "@deepagents/context";
|
|
585
594
|
|
|
586
595
|
// packages/experimental/src/zukhruf/mailbox/communication-schema.ts
|
|
587
596
|
import { z } from "zod";
|
|
@@ -690,21 +699,26 @@ var mailboxItemRowsSchema = z2.array(
|
|
|
690
699
|
);
|
|
691
700
|
var existsRowsSchema = z2.tuple([z2.object({ exists: z2.boolean() })]);
|
|
692
701
|
var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
693
|
-
#
|
|
694
|
-
|
|
695
|
-
constructor(pool, schema) {
|
|
702
|
+
#database;
|
|
703
|
+
constructor(database) {
|
|
696
704
|
super();
|
|
697
|
-
this.#
|
|
698
|
-
this.#schema = schema;
|
|
705
|
+
this.#database = database;
|
|
699
706
|
}
|
|
700
707
|
/** Opens the store: creates its schema objects before it returns. */
|
|
701
708
|
static async create(options) {
|
|
702
|
-
const
|
|
703
|
-
await
|
|
704
|
-
|
|
709
|
+
const database = new PostgresStoreDatabase(options.pool, options.schema);
|
|
710
|
+
await database.query(
|
|
711
|
+
lockSchemaDDL(mailboxDDL(database.schema), database.schema)
|
|
712
|
+
);
|
|
713
|
+
return new _PostgresMailboxStore(database);
|
|
705
714
|
}
|
|
706
|
-
|
|
707
|
-
|
|
715
|
+
/**
|
|
716
|
+
* A view of this store whose reads and writes run inside `tx`. Each of its
|
|
717
|
+
* writes takes the recipient's lock in a savepoint of `tx`, and the lock is
|
|
718
|
+
* held until `tx` ends.
|
|
719
|
+
*/
|
|
720
|
+
bind(tx) {
|
|
721
|
+
return new _PostgresMailboxStore(this.#database.bind(tx));
|
|
708
722
|
}
|
|
709
723
|
/**
|
|
710
724
|
* Runs `fn` while holding the recipient's lock, so enqueue, drain and turn
|
|
@@ -712,21 +726,18 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
712
726
|
* immediate transactions do.
|
|
713
727
|
*/
|
|
714
728
|
#forRecipient(recipient, fn) {
|
|
715
|
-
return
|
|
716
|
-
await
|
|
717
|
-
|
|
718
|
-
|
|
719
|
-
|
|
720
|
-
]
|
|
721
|
-
);
|
|
722
|
-
return fn(client);
|
|
729
|
+
return this.#database.transaction(async (tx) => {
|
|
730
|
+
await tx.query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
|
|
731
|
+
`zukhruf:mailbox:${JSON.stringify([this.#database.schema, recipient.chatId, recipient.userId])}`
|
|
732
|
+
]);
|
|
733
|
+
return fn(tx);
|
|
723
734
|
});
|
|
724
735
|
}
|
|
725
736
|
async beginTurn(recipient, turnId) {
|
|
726
737
|
await this.#forRecipient(
|
|
727
738
|
recipient,
|
|
728
739
|
(client) => client.query(
|
|
729
|
-
`INSERT INTO ${this.#
|
|
740
|
+
`INSERT INTO ${this.#database.table("zukhruf_mailbox_activity")}
|
|
730
741
|
(recipient_chat_id, recipient_user_id, turn_id)
|
|
731
742
|
VALUES ($1, $2, $3)
|
|
732
743
|
ON CONFLICT (recipient_chat_id, recipient_user_id)
|
|
@@ -741,13 +752,13 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
741
752
|
const consumedTerminal = communication.type === InterAgentCommunicationTypes.FinalAnswer && await this.#exists(
|
|
742
753
|
client,
|
|
743
754
|
`SELECT EXISTS (
|
|
744
|
-
SELECT 1 FROM ${this.#
|
|
755
|
+
SELECT 1 FROM ${this.#database.table("zukhruf_mailbox_consumed_terminal_ids")}
|
|
745
756
|
WHERE communication_id = $1)`,
|
|
746
757
|
[communication.id]
|
|
747
758
|
);
|
|
748
759
|
if (!consumedTerminal) {
|
|
749
760
|
await client.query(
|
|
750
|
-
`INSERT INTO ${this.#
|
|
761
|
+
`INSERT INTO ${this.#database.table("zukhruf_mailbox_items")}
|
|
751
762
|
(communication_id, recipient_chat_id, recipient_user_id, communication)
|
|
752
763
|
VALUES ($1, $2, $3, $4::jsonb)
|
|
753
764
|
ON CONFLICT (communication_id) DO NOTHING`,
|
|
@@ -765,7 +776,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
765
776
|
endTurn(recipient, turnId) {
|
|
766
777
|
return this.#forRecipient(recipient, async (client) => {
|
|
767
778
|
const ended = await client.query(
|
|
768
|
-
`DELETE FROM ${this.#
|
|
779
|
+
`DELETE FROM ${this.#database.table("zukhruf_mailbox_activity")}
|
|
769
780
|
WHERE recipient_chat_id = $1
|
|
770
781
|
AND recipient_user_id = $2
|
|
771
782
|
AND turn_id = $3`,
|
|
@@ -778,7 +789,18 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
778
789
|
});
|
|
779
790
|
}
|
|
780
791
|
hasPending(recipient) {
|
|
781
|
-
return this.#hasPending(this.#
|
|
792
|
+
return this.#hasPending(this.#database, recipient);
|
|
793
|
+
}
|
|
794
|
+
hasPendingTriggerTurn(recipient) {
|
|
795
|
+
return this.#exists(
|
|
796
|
+
this.#database,
|
|
797
|
+
`SELECT EXISTS (
|
|
798
|
+
SELECT 1 FROM ${this.#database.table("zukhruf_mailbox_items")}
|
|
799
|
+
WHERE recipient_chat_id = $1
|
|
800
|
+
AND recipient_user_id = $2
|
|
801
|
+
AND communication @> '{"triggerTurn": true}')`,
|
|
802
|
+
[recipient.chatId, recipient.userId]
|
|
803
|
+
);
|
|
782
804
|
}
|
|
783
805
|
drain(recipient) {
|
|
784
806
|
return this.#drain(recipient, false);
|
|
@@ -791,7 +813,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
791
813
|
const items = mailboxItemRowsSchema.parse(
|
|
792
814
|
(await client.query(
|
|
793
815
|
`SELECT sequence, communication
|
|
794
|
-
FROM ${this.#
|
|
816
|
+
FROM ${this.#database.table("zukhruf_mailbox_items")}
|
|
795
817
|
WHERE recipient_chat_id = $1
|
|
796
818
|
AND recipient_user_id = $2
|
|
797
819
|
ORDER BY sequence`,
|
|
@@ -802,7 +824,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
802
824
|
const consumed = firstTrigger === -1 ? items : items.slice(0, firstTrigger);
|
|
803
825
|
if (consumed.length === 0) return [];
|
|
804
826
|
await client.query(
|
|
805
|
-
`INSERT INTO ${this.#
|
|
827
|
+
`INSERT INTO ${this.#database.table("zukhruf_mailbox_consumed_terminal_ids")} (communication_id)
|
|
806
828
|
SELECT unnest($1::text[])
|
|
807
829
|
ON CONFLICT (communication_id) DO NOTHING`,
|
|
808
830
|
[
|
|
@@ -812,7 +834,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
812
834
|
]
|
|
813
835
|
);
|
|
814
836
|
await client.query(
|
|
815
|
-
`DELETE FROM ${this.#
|
|
837
|
+
`DELETE FROM ${this.#database.table("zukhruf_mailbox_items")}
|
|
816
838
|
WHERE sequence = ANY($1::bigint[])`,
|
|
817
839
|
[consumed.map(({ sequence }) => sequence)]
|
|
818
840
|
);
|
|
@@ -823,7 +845,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
823
845
|
return this.#exists(
|
|
824
846
|
queryable,
|
|
825
847
|
`SELECT EXISTS (
|
|
826
|
-
SELECT 1 FROM ${this.#
|
|
848
|
+
SELECT 1 FROM ${this.#database.table("zukhruf_mailbox_items")}
|
|
827
849
|
WHERE recipient_chat_id = $1 AND recipient_user_id = $2)`,
|
|
828
850
|
[recipient.chatId, recipient.userId]
|
|
829
851
|
);
|
|
@@ -832,7 +854,7 @@ var PostgresMailboxStore = class _PostgresMailboxStore extends MailboxStore {
|
|
|
832
854
|
return this.#exists(
|
|
833
855
|
client,
|
|
834
856
|
`SELECT EXISTS (
|
|
835
|
-
SELECT 1 FROM ${this.#
|
|
857
|
+
SELECT 1 FROM ${this.#database.table("zukhruf_mailbox_activity")}
|
|
836
858
|
WHERE recipient_chat_id = $1 AND recipient_user_id = $2)`,
|
|
837
859
|
[recipient.chatId, recipient.userId]
|
|
838
860
|
);
|
|
@@ -894,9 +916,9 @@ var mailboxItemRowsSchema2 = z3.array(
|
|
|
894
916
|
);
|
|
895
917
|
var SqliteMailboxStore = class extends MailboxStore {
|
|
896
918
|
#database;
|
|
897
|
-
constructor(
|
|
919
|
+
constructor(pathOrDb) {
|
|
898
920
|
super();
|
|
899
|
-
this.#database = open(
|
|
921
|
+
this.#database = typeof pathOrDb === "string" ? open(pathOrDb) : pathOrDb;
|
|
900
922
|
this.#database.exec(SETUP);
|
|
901
923
|
tables.apply(this.#database);
|
|
902
924
|
}
|
|
@@ -963,6 +985,17 @@ var SqliteMailboxStore = class extends MailboxStore {
|
|
|
963
985
|
async hasPending(recipient) {
|
|
964
986
|
return this.#hasPending(recipient);
|
|
965
987
|
}
|
|
988
|
+
async hasPendingTriggerTurn(recipient) {
|
|
989
|
+
const row = this.#database.prepare(
|
|
990
|
+
`SELECT 1
|
|
991
|
+
FROM zukhruf_mailbox_items
|
|
992
|
+
WHERE recipient_chat_id = ?
|
|
993
|
+
AND recipient_user_id = ?
|
|
994
|
+
AND json_extract(communication, '$.triggerTurn') = 1
|
|
995
|
+
LIMIT 1`
|
|
996
|
+
).get(recipient.chatId, recipient.userId);
|
|
997
|
+
return row !== void 0;
|
|
998
|
+
}
|
|
966
999
|
#hasPending(recipient) {
|
|
967
1000
|
const row = this.#database.prepare(
|
|
968
1001
|
`SELECT 1
|
|
@@ -1086,6 +1119,7 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
1086
1119
|
static async create(boss, options = {}) {
|
|
1087
1120
|
const settings = _PgBossTurnQueue.#settings(boss, options);
|
|
1088
1121
|
const opened = await _PgBossTurnQueue.#openQueue(boss, settings);
|
|
1122
|
+
await boss.findJobs(settings.queue, { id: uuidv7() });
|
|
1089
1123
|
return new _PgBossTurnQueue(boss, settings, opened);
|
|
1090
1124
|
}
|
|
1091
1125
|
static #settings(boss, options) {
|
|
@@ -1160,19 +1194,53 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
1160
1194
|
get deadLetterQueue() {
|
|
1161
1195
|
return deadLetterQueueOf(this.#queue);
|
|
1162
1196
|
}
|
|
1163
|
-
|
|
1197
|
+
/**
|
|
1198
|
+
* A view whose `push` and `getTurnActivity` run inside `tx`: a pushed turn
|
|
1199
|
+
* reaches workers only if `tx` commits, and activity counts what `tx` has
|
|
1200
|
+
* pushed so far. `tx` must be on the database PgBoss uses.
|
|
1201
|
+
*/
|
|
1202
|
+
bind(tx) {
|
|
1203
|
+
const db = {
|
|
1204
|
+
executeSql: (text, values) => tx.query(text, values)
|
|
1205
|
+
};
|
|
1206
|
+
return {
|
|
1207
|
+
push: (turn) => this.#push(turn, db),
|
|
1208
|
+
getTurnActivity: (conversation) => this.#getTurnActivity(conversation, db)
|
|
1209
|
+
};
|
|
1210
|
+
}
|
|
1211
|
+
push(turn) {
|
|
1212
|
+
return this.#push(turn, void 0);
|
|
1213
|
+
}
|
|
1214
|
+
getTurnActivity(conversation) {
|
|
1215
|
+
return this.#getTurnActivity(conversation, void 0);
|
|
1216
|
+
}
|
|
1217
|
+
/**
|
|
1218
|
+
* Pushes `turn` as the job `id`, a UUID. While pg-boss holds a job with that
|
|
1219
|
+
* id, a second push of it adds nothing. Ids that grow with push order (UUIDv7)
|
|
1220
|
+
* keep FIFO across timestamp ties.
|
|
1221
|
+
*/
|
|
1222
|
+
async pushAs(id, turn) {
|
|
1223
|
+
await this.#send(id, turn, void 0);
|
|
1224
|
+
}
|
|
1225
|
+
async #push(turn, db) {
|
|
1164
1226
|
const jobId = uuidv7();
|
|
1165
|
-
|
|
1166
|
-
id: jobId,
|
|
1167
|
-
singletonKey: turn.chatId
|
|
1168
|
-
});
|
|
1169
|
-
if (inserted === null) {
|
|
1227
|
+
if (!await this.#send(jobId, turn, db)) {
|
|
1170
1228
|
throw new Error(`PgBossTurnQueue job id collision: ${jobId}`);
|
|
1171
1229
|
}
|
|
1172
1230
|
}
|
|
1173
|
-
|
|
1231
|
+
/** Whether pg-boss inserted the job: it skips an id it already holds. */
|
|
1232
|
+
async #send(id, turn, db) {
|
|
1233
|
+
const inserted = await this.#boss.send(this.#queue, turn, {
|
|
1234
|
+
id,
|
|
1235
|
+
singletonKey: turn.chatId,
|
|
1236
|
+
db
|
|
1237
|
+
});
|
|
1238
|
+
return inserted !== null;
|
|
1239
|
+
}
|
|
1240
|
+
async #getTurnActivity(conversation, db) {
|
|
1174
1241
|
const jobs = await this.#boss.findJobs(this.#queue, {
|
|
1175
|
-
key: conversation.chatId
|
|
1242
|
+
key: conversation.chatId,
|
|
1243
|
+
db
|
|
1176
1244
|
});
|
|
1177
1245
|
const conversationJobs = jobs.filter(
|
|
1178
1246
|
(job) => job.data.userId === conversation.userId && (job.state === "created" || job.state === "retry" || job.state === "active")
|
|
@@ -2641,12 +2709,12 @@ function replacementRange(messages, keepLastMessages) {
|
|
|
2641
2709
|
while (messages[start]?.role === "system") start++;
|
|
2642
2710
|
let limit = messages.length - keepLastMessages;
|
|
2643
2711
|
const links = /* @__PURE__ */ new Map();
|
|
2644
|
-
const link = (key, index,
|
|
2712
|
+
const link = (key, index, open3) => {
|
|
2645
2713
|
const previous = links.get(key);
|
|
2646
2714
|
links.set(key, {
|
|
2647
2715
|
first: previous?.first ?? index,
|
|
2648
2716
|
last: index,
|
|
2649
|
-
open:
|
|
2717
|
+
open: open3 ?? previous?.open ?? false
|
|
2650
2718
|
});
|
|
2651
2719
|
};
|
|
2652
2720
|
for (const [index, message] of messages.entries()) {
|
|
@@ -2675,8 +2743,8 @@ function replacementRange(messages, keepLastMessages) {
|
|
|
2675
2743
|
}
|
|
2676
2744
|
}
|
|
2677
2745
|
const crossings = new Int32Array(messages.length + 1);
|
|
2678
|
-
for (const { first, last, open:
|
|
2679
|
-
if (
|
|
2746
|
+
for (const { first, last, open: open3 } of links.values()) {
|
|
2747
|
+
if (open3) limit = Math.min(limit, first);
|
|
2680
2748
|
crossings[first + 1]++;
|
|
2681
2749
|
crossings[last + 1]--;
|
|
2682
2750
|
}
|
|
@@ -4965,37 +5033,38 @@ var RuntimeHost = class {
|
|
|
4965
5033
|
new PluginManager(root, options.bindings, multiAgent)
|
|
4966
5034
|
);
|
|
4967
5035
|
const declarations = plugins.declarations;
|
|
4968
|
-
const
|
|
4969
|
-
const
|
|
5036
|
+
const { storage } = options;
|
|
5037
|
+
const directory = new AgentDirectory(storage.store);
|
|
5038
|
+
const streams = storage.streams;
|
|
4970
5039
|
const conversationStatus = new ConversationStatusProjector({
|
|
4971
|
-
store:
|
|
5040
|
+
store: storage.store,
|
|
4972
5041
|
streams,
|
|
4973
|
-
queue:
|
|
5042
|
+
queue: storage.queue,
|
|
4974
5043
|
directory,
|
|
4975
5044
|
changeSource: options.conversationStatusChanges
|
|
4976
5045
|
});
|
|
4977
5046
|
const queue = new StatusPublishingTurnQueue(
|
|
4978
|
-
|
|
5047
|
+
storage.queue,
|
|
4979
5048
|
(conversation) => conversationStatus.publish(conversation)
|
|
4980
5049
|
);
|
|
4981
5050
|
const mailbox = new MailboxCoordinator({
|
|
4982
|
-
store:
|
|
5051
|
+
store: storage.mailboxStore,
|
|
4983
5052
|
queue,
|
|
4984
5053
|
streams
|
|
4985
5054
|
});
|
|
4986
5055
|
const approvals = new ApprovalController({
|
|
4987
|
-
store:
|
|
5056
|
+
store: storage.store,
|
|
4988
5057
|
queue
|
|
4989
5058
|
});
|
|
4990
5059
|
const statusProjector = new AgentStatusProjector({
|
|
4991
|
-
store:
|
|
5060
|
+
store: storage.store,
|
|
4992
5061
|
streams,
|
|
4993
5062
|
queue,
|
|
4994
5063
|
mailbox,
|
|
4995
5064
|
directory,
|
|
4996
5065
|
approvals
|
|
4997
5066
|
});
|
|
4998
|
-
const historyForker = new AgentHistoryForker(
|
|
5067
|
+
const historyForker = new AgentHistoryForker(storage.store);
|
|
4999
5068
|
const controlPlane = new AgentControlPlane({
|
|
5000
5069
|
root: declarations.root,
|
|
5001
5070
|
streams,
|
|
@@ -5008,7 +5077,7 @@ var RuntimeHost = class {
|
|
|
5008
5077
|
recordActivity: (thread, activity) => conversationStatus.recordActivity(thread, activity),
|
|
5009
5078
|
maxConcurrentThreadsPerSession: multiAgent.maxConcurrentThreadsPerSession
|
|
5010
5079
|
});
|
|
5011
|
-
this.#store =
|
|
5080
|
+
this.#store = storage.store;
|
|
5012
5081
|
this.#queue = queue;
|
|
5013
5082
|
this.#streams = streams;
|
|
5014
5083
|
this.#mailbox = mailbox;
|
|
@@ -5034,7 +5103,7 @@ var RuntimeHost = class {
|
|
|
5034
5103
|
};
|
|
5035
5104
|
const sandboxes = resources.use(new ConversationSandboxes());
|
|
5036
5105
|
this.#executor = new AgentTurnExecutor({
|
|
5037
|
-
store:
|
|
5106
|
+
store: storage.store,
|
|
5038
5107
|
streams,
|
|
5039
5108
|
controlPlane,
|
|
5040
5109
|
mailbox,
|
|
@@ -5372,6 +5441,484 @@ function defineSandbox(createBackend, options) {
|
|
|
5372
5441
|
};
|
|
5373
5442
|
}
|
|
5374
5443
|
|
|
5444
|
+
// packages/experimental/src/zukhruf/storage/agent-storage.ts
|
|
5445
|
+
var AgentStorage = class {
|
|
5446
|
+
};
|
|
5447
|
+
|
|
5448
|
+
// packages/experimental/src/zukhruf/storage/embedded-agent-storage.ts
|
|
5449
|
+
import { PGlite } from "@electric-sql/pglite";
|
|
5450
|
+
import { FileLock } from "@zukhruf/fs";
|
|
5451
|
+
import { open as open2 } from "@zukhruf/sqlite-open";
|
|
5452
|
+
import { mkdir } from "node:fs/promises";
|
|
5453
|
+
import { join as join2 } from "node:path";
|
|
5454
|
+
import { PgBoss, fromPglite } from "pg-boss";
|
|
5455
|
+
import {
|
|
5456
|
+
PollingChangeSource,
|
|
5457
|
+
SqliteContextStore,
|
|
5458
|
+
SqliteStreamStore,
|
|
5459
|
+
StreamManager
|
|
5460
|
+
} from "@deepagents/context";
|
|
5461
|
+
|
|
5462
|
+
// packages/experimental/src/zukhruf/storage/sqlite-admission-gate.ts
|
|
5463
|
+
import { Lock } from "@zukhruf/async";
|
|
5464
|
+
import { AsyncLocalStorage } from "node:async_hooks";
|
|
5465
|
+
var AdmissionScopeError = class extends Error {
|
|
5466
|
+
name = "AdmissionScopeError";
|
|
5467
|
+
constructor() {
|
|
5468
|
+
super(
|
|
5469
|
+
"EmbeddedAgentStorage: an admission's work called the storage itself; inside admit, read and write through the AdmissionScope it was given"
|
|
5470
|
+
);
|
|
5471
|
+
}
|
|
5472
|
+
};
|
|
5473
|
+
var SqliteAdmissionGate = class {
|
|
5474
|
+
#database;
|
|
5475
|
+
#connection = new Lock();
|
|
5476
|
+
#admission = new AsyncLocalStorage();
|
|
5477
|
+
constructor(database) {
|
|
5478
|
+
this.#database = database;
|
|
5479
|
+
}
|
|
5480
|
+
/** `part` with every method run through `call`. */
|
|
5481
|
+
view(part) {
|
|
5482
|
+
return new Proxy(part, {
|
|
5483
|
+
get: (target, property) => {
|
|
5484
|
+
const value = Reflect.get(target, property, target);
|
|
5485
|
+
if (typeof value !== "function") return value;
|
|
5486
|
+
return (...args) => this.call(async () => Reflect.apply(value, target, args));
|
|
5487
|
+
}
|
|
5488
|
+
});
|
|
5489
|
+
}
|
|
5490
|
+
/** Runs `operation` while it holds the connection alone. */
|
|
5491
|
+
async call(operation) {
|
|
5492
|
+
if (this.#insideAdmission()) throw new AdmissionScopeError();
|
|
5493
|
+
return this.#connection.run(operation, { signal: void 0 });
|
|
5494
|
+
}
|
|
5495
|
+
/** Runs `work` in one `BEGIN IMMEDIATE` transaction: commits when it resolves, rolls back when it throws. */
|
|
5496
|
+
async admit(work) {
|
|
5497
|
+
if (this.#insideAdmission()) throw new AdmissionScopeError();
|
|
5498
|
+
return this.#connection.run(
|
|
5499
|
+
async () => {
|
|
5500
|
+
const admission = { open: true };
|
|
5501
|
+
this.#database.exec("BEGIN IMMEDIATE");
|
|
5502
|
+
try {
|
|
5503
|
+
const result = await this.#admission.run(admission, work);
|
|
5504
|
+
this.#database.exec("COMMIT");
|
|
5505
|
+
return result;
|
|
5506
|
+
} catch (error) {
|
|
5507
|
+
if (this.#database.isTransaction) this.#database.exec("ROLLBACK");
|
|
5508
|
+
throw error;
|
|
5509
|
+
} finally {
|
|
5510
|
+
admission.open = false;
|
|
5511
|
+
}
|
|
5512
|
+
},
|
|
5513
|
+
{ signal: void 0 }
|
|
5514
|
+
);
|
|
5515
|
+
}
|
|
5516
|
+
/**
|
|
5517
|
+
* Inside its own admission a call would wait for that admission to end,
|
|
5518
|
+
* which waits for the call. Work the admission left running after it
|
|
5519
|
+
* ended is no longer inside it.
|
|
5520
|
+
*/
|
|
5521
|
+
#insideAdmission() {
|
|
5522
|
+
return this.#admission.getStore()?.open === true;
|
|
5523
|
+
}
|
|
5524
|
+
};
|
|
5525
|
+
|
|
5526
|
+
// packages/experimental/src/zukhruf/storage/turn-outbox.ts
|
|
5527
|
+
import { Lock as Lock2 } from "@zukhruf/async";
|
|
5528
|
+
import { Declaration as Declaration2 } from "@zukhruf/sqlite-drift";
|
|
5529
|
+
import { v7 as uuidv72 } from "uuid";
|
|
5530
|
+
import { z as z14 } from "zod";
|
|
5531
|
+
var tables2 = new Declaration2(
|
|
5532
|
+
[
|
|
5533
|
+
`CREATE TABLE zukhruf_turn_outbox (
|
|
5534
|
+
sequence INTEGER PRIMARY KEY AUTOINCREMENT,
|
|
5535
|
+
job_id TEXT NOT NULL UNIQUE,
|
|
5536
|
+
chat_id TEXT NOT NULL,
|
|
5537
|
+
user_id TEXT NOT NULL,
|
|
5538
|
+
stream_id TEXT NOT NULL,
|
|
5539
|
+
turn TEXT NOT NULL
|
|
5540
|
+
)`,
|
|
5541
|
+
`CREATE INDEX zukhruf_turn_outbox_conversation
|
|
5542
|
+
ON zukhruf_turn_outbox (chat_id, user_id, sequence)`
|
|
5543
|
+
],
|
|
5544
|
+
{ rows: "keep" }
|
|
5545
|
+
);
|
|
5546
|
+
var outboxRowSchema = z14.object({ jobId: z14.string(), turn: z14.string() });
|
|
5547
|
+
function parseTurn(text) {
|
|
5548
|
+
const turn = JSON.parse(text);
|
|
5549
|
+
return turn;
|
|
5550
|
+
}
|
|
5551
|
+
var TurnOutbox = class {
|
|
5552
|
+
#database;
|
|
5553
|
+
constructor(database) {
|
|
5554
|
+
this.#database = database;
|
|
5555
|
+
tables2.apply(database);
|
|
5556
|
+
}
|
|
5557
|
+
add(turn) {
|
|
5558
|
+
this.#database.prepare(
|
|
5559
|
+
`INSERT INTO zukhruf_turn_outbox (job_id, chat_id, user_id, stream_id, turn)
|
|
5560
|
+
VALUES (?, ?, ?, ?, ?)`
|
|
5561
|
+
).run(
|
|
5562
|
+
uuidv72(),
|
|
5563
|
+
turn.chatId,
|
|
5564
|
+
turn.userId,
|
|
5565
|
+
turn.streamId,
|
|
5566
|
+
JSON.stringify(turn)
|
|
5567
|
+
);
|
|
5568
|
+
}
|
|
5569
|
+
oldest() {
|
|
5570
|
+
return this.#outboxed(
|
|
5571
|
+
this.#database.prepare(
|
|
5572
|
+
`SELECT job_id AS jobId, turn FROM zukhruf_turn_outbox
|
|
5573
|
+
ORDER BY sequence LIMIT 1`
|
|
5574
|
+
).get()
|
|
5575
|
+
);
|
|
5576
|
+
}
|
|
5577
|
+
oldestOf(conversation) {
|
|
5578
|
+
return this.#outboxed(
|
|
5579
|
+
this.#database.prepare(
|
|
5580
|
+
`SELECT job_id AS jobId, turn FROM zukhruf_turn_outbox
|
|
5581
|
+
WHERE chat_id = ? AND user_id = ?
|
|
5582
|
+
ORDER BY sequence LIMIT 1`
|
|
5583
|
+
).get(conversation.chatId, conversation.userId)
|
|
5584
|
+
)?.turn;
|
|
5585
|
+
}
|
|
5586
|
+
holds(conversation) {
|
|
5587
|
+
return this.#database.prepare(
|
|
5588
|
+
`SELECT 1 FROM zukhruf_turn_outbox
|
|
5589
|
+
WHERE chat_id = ? AND user_id = ? LIMIT 1`
|
|
5590
|
+
).get(conversation.chatId, conversation.userId) !== void 0;
|
|
5591
|
+
}
|
|
5592
|
+
remove(jobId) {
|
|
5593
|
+
this.#database.prepare("DELETE FROM zukhruf_turn_outbox WHERE job_id = ?").run(jobId);
|
|
5594
|
+
}
|
|
5595
|
+
removeStream(streamId) {
|
|
5596
|
+
this.#database.prepare("DELETE FROM zukhruf_turn_outbox WHERE stream_id = ?").run(streamId);
|
|
5597
|
+
}
|
|
5598
|
+
#outboxed(row) {
|
|
5599
|
+
if (row === void 0) return void 0;
|
|
5600
|
+
const { jobId, turn } = outboxRowSchema.parse(row);
|
|
5601
|
+
return { jobId, turn: parseTurn(turn) };
|
|
5602
|
+
}
|
|
5603
|
+
};
|
|
5604
|
+
function withOutboxed(activity, outboxed) {
|
|
5605
|
+
if (activity === "running") return activity;
|
|
5606
|
+
return outboxed ? "queued" : activity;
|
|
5607
|
+
}
|
|
5608
|
+
var OutboxRelay = class {
|
|
5609
|
+
#gate;
|
|
5610
|
+
#outbox;
|
|
5611
|
+
#queue;
|
|
5612
|
+
/** Held by a drain, and by work that must not run while a row is between its push and its delete. */
|
|
5613
|
+
#rows = new Lock2();
|
|
5614
|
+
constructor(gate, outbox, queue) {
|
|
5615
|
+
this.#gate = gate;
|
|
5616
|
+
this.#outbox = outbox;
|
|
5617
|
+
this.#queue = queue;
|
|
5618
|
+
}
|
|
5619
|
+
/** Resolves once every row added before the call has reached pg-boss. */
|
|
5620
|
+
drain() {
|
|
5621
|
+
return this.between(async () => {
|
|
5622
|
+
for (; ; ) {
|
|
5623
|
+
const next = await this.#gate.call(async () => this.#outbox.oldest());
|
|
5624
|
+
if (!next) return;
|
|
5625
|
+
await this.#queue.pushAs(next.jobId, next.turn);
|
|
5626
|
+
await this.#gate.call(async () => this.#outbox.remove(next.jobId));
|
|
5627
|
+
}
|
|
5628
|
+
});
|
|
5629
|
+
}
|
|
5630
|
+
/** Runs `operation` while no drain runs. */
|
|
5631
|
+
between(operation) {
|
|
5632
|
+
return this.#rows.run(operation, { signal: void 0 });
|
|
5633
|
+
}
|
|
5634
|
+
};
|
|
5635
|
+
var OutboxTurnQueue = class extends TurnQueue {
|
|
5636
|
+
#gate;
|
|
5637
|
+
#outbox;
|
|
5638
|
+
#relay;
|
|
5639
|
+
#queue;
|
|
5640
|
+
constructor(parts) {
|
|
5641
|
+
super();
|
|
5642
|
+
this.#gate = parts.gate;
|
|
5643
|
+
this.#outbox = parts.outbox;
|
|
5644
|
+
this.#relay = parts.relay;
|
|
5645
|
+
this.#queue = parts.queue;
|
|
5646
|
+
}
|
|
5647
|
+
/** The view an admission pushes and counts through: its pushes are outbox rows of its transaction. */
|
|
5648
|
+
scope() {
|
|
5649
|
+
return {
|
|
5650
|
+
push: async (turn) => this.#outbox.add(turn),
|
|
5651
|
+
getTurnActivity: async (conversation) => {
|
|
5652
|
+
const outboxed = this.#outbox.holds(conversation);
|
|
5653
|
+
return withOutboxed(
|
|
5654
|
+
await this.#queue.getTurnActivity(conversation),
|
|
5655
|
+
outboxed
|
|
5656
|
+
);
|
|
5657
|
+
}
|
|
5658
|
+
};
|
|
5659
|
+
}
|
|
5660
|
+
/** Resolves once the turn is in pg-boss. */
|
|
5661
|
+
async push(turn) {
|
|
5662
|
+
await this.#gate.call(async () => this.#outbox.add(turn));
|
|
5663
|
+
await this.#relay.drain();
|
|
5664
|
+
}
|
|
5665
|
+
async getTurnActivity(conversation) {
|
|
5666
|
+
const outboxed = await this.#gate.call(
|
|
5667
|
+
async () => this.#outbox.holds(conversation)
|
|
5668
|
+
);
|
|
5669
|
+
return withOutboxed(
|
|
5670
|
+
await this.#queue.getTurnActivity(conversation),
|
|
5671
|
+
outboxed
|
|
5672
|
+
);
|
|
5673
|
+
}
|
|
5674
|
+
async getCurrentTurn(conversation) {
|
|
5675
|
+
const outboxed = await this.#gate.call(
|
|
5676
|
+
async () => this.#outbox.oldestOf(conversation)
|
|
5677
|
+
);
|
|
5678
|
+
return await this.#queue.getCurrentTurn(conversation) ?? outboxed;
|
|
5679
|
+
}
|
|
5680
|
+
cancel(streamId) {
|
|
5681
|
+
return this.#relay.between(async () => {
|
|
5682
|
+
await this.#gate.call(async () => this.#outbox.removeStream(streamId));
|
|
5683
|
+
await this.#queue.cancel(streamId);
|
|
5684
|
+
});
|
|
5685
|
+
}
|
|
5686
|
+
consume(handler, options) {
|
|
5687
|
+
return this.#queue.consume(handler, options);
|
|
5688
|
+
}
|
|
5689
|
+
resumeParked(chatId) {
|
|
5690
|
+
return this.#queue.resumeParked(chatId);
|
|
5691
|
+
}
|
|
5692
|
+
};
|
|
5693
|
+
|
|
5694
|
+
// packages/experimental/src/zukhruf/storage/embedded-agent-storage.ts
|
|
5695
|
+
var EmbeddedAgentStorageInUseError = class extends Error {
|
|
5696
|
+
name = "EmbeddedAgentStorageInUseError";
|
|
5697
|
+
directory;
|
|
5698
|
+
constructor(directory) {
|
|
5699
|
+
super(
|
|
5700
|
+
`EmbeddedAgentStorage: ${JSON.stringify(directory)} is open in another EmbeddedAgentStorage; one storage at a time may open a directory`
|
|
5701
|
+
);
|
|
5702
|
+
this.directory = directory;
|
|
5703
|
+
}
|
|
5704
|
+
};
|
|
5705
|
+
function emitWarning(error) {
|
|
5706
|
+
process.emitWarning(
|
|
5707
|
+
Error.isError(error) ? error : new Error(String(error), { cause: error })
|
|
5708
|
+
);
|
|
5709
|
+
}
|
|
5710
|
+
var EmbeddedAgentStorage = class _EmbeddedAgentStorage extends AgentStorage {
|
|
5711
|
+
store;
|
|
5712
|
+
streams;
|
|
5713
|
+
queue;
|
|
5714
|
+
mailboxStore;
|
|
5715
|
+
/** Started and owned; a host may bind it to plugins that keep jobs beside the turns. */
|
|
5716
|
+
boss;
|
|
5717
|
+
/** The PGlite database pg-boss runs on, for transactions of such plugins. */
|
|
5718
|
+
queueDatabase;
|
|
5719
|
+
#gate;
|
|
5720
|
+
#scope;
|
|
5721
|
+
#relay;
|
|
5722
|
+
#resources;
|
|
5723
|
+
constructor(parts) {
|
|
5724
|
+
super();
|
|
5725
|
+
this.store = parts.store;
|
|
5726
|
+
this.streams = parts.streams;
|
|
5727
|
+
this.queue = parts.queue;
|
|
5728
|
+
this.mailboxStore = parts.mailboxStore;
|
|
5729
|
+
this.boss = parts.boss;
|
|
5730
|
+
this.queueDatabase = parts.queueDatabase;
|
|
5731
|
+
this.#gate = parts.gate;
|
|
5732
|
+
this.#scope = parts.scope;
|
|
5733
|
+
this.#relay = parts.relay;
|
|
5734
|
+
this.#resources = parts.resources;
|
|
5735
|
+
}
|
|
5736
|
+
/**
|
|
5737
|
+
* Opens the storage in `directory` and pushes the turns its outbox still
|
|
5738
|
+
* holds. Rejects with `EmbeddedAgentStorageInUseError` while another storage
|
|
5739
|
+
* has the directory open.
|
|
5740
|
+
*/
|
|
5741
|
+
static async open(options) {
|
|
5742
|
+
await mkdir(options.directory, { recursive: true });
|
|
5743
|
+
await using resources = new AsyncDisposableStack();
|
|
5744
|
+
const lock = resources.use(
|
|
5745
|
+
FileLock.open(join2(options.directory, "zukhruf.lock"))
|
|
5746
|
+
);
|
|
5747
|
+
if (!lock.tryLock()) {
|
|
5748
|
+
throw new EmbeddedAgentStorageInUseError(options.directory);
|
|
5749
|
+
}
|
|
5750
|
+
const database = resources.adopt(
|
|
5751
|
+
open2(join2(options.directory, "zukhruf.sqlite")),
|
|
5752
|
+
(database2) => database2.close()
|
|
5753
|
+
);
|
|
5754
|
+
const queueDatabase = resources.adopt(
|
|
5755
|
+
new PGlite(join2(options.directory, "zukhruf.queue")),
|
|
5756
|
+
(queueDatabase2) => queueDatabase2.close()
|
|
5757
|
+
);
|
|
5758
|
+
const boss = new PgBoss({
|
|
5759
|
+
db: fromPglite(queueDatabase),
|
|
5760
|
+
backend: "pglite"
|
|
5761
|
+
});
|
|
5762
|
+
boss.on("error", emitWarning);
|
|
5763
|
+
await boss.start();
|
|
5764
|
+
resources.defer(() => boss.stop({ graceful: false }));
|
|
5765
|
+
const turns = await PgBossTurnQueue.create(boss, {
|
|
5766
|
+
pollingIntervalSeconds: 0.5,
|
|
5767
|
+
...options.queue,
|
|
5768
|
+
schema: "pgboss"
|
|
5769
|
+
});
|
|
5770
|
+
const gate = new SqliteAdmissionGate(database);
|
|
5771
|
+
const store = new SqliteContextStore(database);
|
|
5772
|
+
const streamStore = new SqliteStreamStore(database);
|
|
5773
|
+
const mailboxStore = new SqliteMailboxStore(database);
|
|
5774
|
+
const outbox = new TurnOutbox(database);
|
|
5775
|
+
const relay = new OutboxRelay(gate, outbox, turns);
|
|
5776
|
+
resources.defer(() => relay.between(async () => void 0));
|
|
5777
|
+
const queue = new OutboxTurnQueue({ gate, outbox, relay, queue: turns });
|
|
5778
|
+
const streams = gate.view(streamStore);
|
|
5779
|
+
await relay.drain();
|
|
5780
|
+
return new _EmbeddedAgentStorage({
|
|
5781
|
+
store: gate.view(store),
|
|
5782
|
+
streams: new StreamManager({
|
|
5783
|
+
store: streams,
|
|
5784
|
+
changeSource: new PollingChangeSource({ reads: streams })
|
|
5785
|
+
}),
|
|
5786
|
+
queue,
|
|
5787
|
+
mailboxStore: gate.view(mailboxStore),
|
|
5788
|
+
boss,
|
|
5789
|
+
queueDatabase,
|
|
5790
|
+
gate,
|
|
5791
|
+
scope: {
|
|
5792
|
+
store,
|
|
5793
|
+
streams: streamStore,
|
|
5794
|
+
mailboxStore,
|
|
5795
|
+
queue: queue.scope()
|
|
5796
|
+
},
|
|
5797
|
+
relay,
|
|
5798
|
+
resources: resources.move()
|
|
5799
|
+
});
|
|
5800
|
+
}
|
|
5801
|
+
/**
|
|
5802
|
+
* Runs `work` in one SQLite transaction while no other admission, to any
|
|
5803
|
+
* tree, runs. Resolves once it has committed; the relay then pushes the
|
|
5804
|
+
* turns it outboxed.
|
|
5805
|
+
*/
|
|
5806
|
+
async admit(_tree, work) {
|
|
5807
|
+
const result = await this.#gate.admit(() => work(this.#scope));
|
|
5808
|
+
this.#relay.drain().catch(emitWarning);
|
|
5809
|
+
return result;
|
|
5810
|
+
}
|
|
5811
|
+
/** Waits for the relay, stops pg-boss, and closes both databases. */
|
|
5812
|
+
async [Symbol.asyncDispose]() {
|
|
5813
|
+
await this.#resources.disposeAsync();
|
|
5814
|
+
}
|
|
5815
|
+
};
|
|
5816
|
+
|
|
5817
|
+
// packages/experimental/src/zukhruf/storage/postgres-agent-storage.ts
|
|
5818
|
+
import { z as z15 } from "zod";
|
|
5819
|
+
import {
|
|
5820
|
+
PostgresContextStore,
|
|
5821
|
+
PostgresNotifyChangeSource,
|
|
5822
|
+
PostgresStreamStore,
|
|
5823
|
+
StreamManager as StreamManager2,
|
|
5824
|
+
postgresTransaction
|
|
5825
|
+
} from "@deepagents/context";
|
|
5826
|
+
var DATABASE_IDENTITY_SQL = `SELECT current_database() AS database,
|
|
5827
|
+
(pg_control_system()).system_identifier::text AS "systemIdentifier"`;
|
|
5828
|
+
var databaseIdentityRowsSchema = z15.tuple([
|
|
5829
|
+
z15.object({ database: z15.string(), systemIdentifier: z15.string() })
|
|
5830
|
+
]);
|
|
5831
|
+
async function databaseIdentity(result) {
|
|
5832
|
+
return databaseIdentityRowsSchema.parse((await result).rows)[0];
|
|
5833
|
+
}
|
|
5834
|
+
var AgentStorageDatabaseMismatchError = class extends Error {
|
|
5835
|
+
name = "AgentStorageDatabaseMismatchError";
|
|
5836
|
+
pool;
|
|
5837
|
+
boss;
|
|
5838
|
+
constructor(pool, boss) {
|
|
5839
|
+
super(
|
|
5840
|
+
`PostgresAgentStorage needs the pool and PgBoss on one database; the pool reaches "${pool.database}" (system ${pool.systemIdentifier}) and PgBoss reaches "${boss.database}" (system ${boss.systemIdentifier})`
|
|
5841
|
+
);
|
|
5842
|
+
this.pool = pool;
|
|
5843
|
+
this.boss = boss;
|
|
5844
|
+
}
|
|
5845
|
+
};
|
|
5846
|
+
var PostgresAgentStorage = class _PostgresAgentStorage extends AgentStorage {
|
|
5847
|
+
store;
|
|
5848
|
+
streams;
|
|
5849
|
+
queue;
|
|
5850
|
+
mailboxStore;
|
|
5851
|
+
#pool;
|
|
5852
|
+
#schema;
|
|
5853
|
+
#streamStore;
|
|
5854
|
+
#ownedChangeSource;
|
|
5855
|
+
constructor(parts) {
|
|
5856
|
+
super();
|
|
5857
|
+
this.#pool = parts.pool;
|
|
5858
|
+
this.#schema = parts.schema;
|
|
5859
|
+
this.store = parts.store;
|
|
5860
|
+
this.#streamStore = parts.streamStore;
|
|
5861
|
+
this.streams = parts.streams;
|
|
5862
|
+
this.queue = parts.queue;
|
|
5863
|
+
this.mailboxStore = parts.mailboxStore;
|
|
5864
|
+
this.#ownedChangeSource = parts.ownedChangeSource;
|
|
5865
|
+
}
|
|
5866
|
+
/**
|
|
5867
|
+
* Opens the storage: checks that the pool and PgBoss reach one database,
|
|
5868
|
+
* then creates the tables and the turn queue.
|
|
5869
|
+
*/
|
|
5870
|
+
static async create(options) {
|
|
5871
|
+
const { pool, boss } = options;
|
|
5872
|
+
const schema = options.schema ?? "public";
|
|
5873
|
+
const [poolIdentity, bossIdentity] = await Promise.all([
|
|
5874
|
+
databaseIdentity(pool.query(DATABASE_IDENTITY_SQL)),
|
|
5875
|
+
databaseIdentity(boss.getDb().executeSql(DATABASE_IDENTITY_SQL))
|
|
5876
|
+
]);
|
|
5877
|
+
if (poolIdentity.database !== bossIdentity.database || poolIdentity.systemIdentifier !== bossIdentity.systemIdentifier) {
|
|
5878
|
+
throw new AgentStorageDatabaseMismatchError(poolIdentity, bossIdentity);
|
|
5879
|
+
}
|
|
5880
|
+
const store = await PostgresContextStore.create({ pool, schema });
|
|
5881
|
+
const streamStore = await PostgresStreamStore.create({ pool, schema });
|
|
5882
|
+
const mailboxStore = await PostgresMailboxStore.create({ pool, schema });
|
|
5883
|
+
const queue = await PgBossTurnQueue.create(boss, options.queue);
|
|
5884
|
+
let changeSource = options.changeSource;
|
|
5885
|
+
let ownedChangeSource;
|
|
5886
|
+
if (changeSource === void 0) {
|
|
5887
|
+
ownedChangeSource = await PostgresNotifyChangeSource.create({
|
|
5888
|
+
pool,
|
|
5889
|
+
schema
|
|
5890
|
+
});
|
|
5891
|
+
changeSource = ownedChangeSource;
|
|
5892
|
+
}
|
|
5893
|
+
return new _PostgresAgentStorage({
|
|
5894
|
+
pool,
|
|
5895
|
+
schema,
|
|
5896
|
+
store,
|
|
5897
|
+
streamStore,
|
|
5898
|
+
streams: new StreamManager2({ store: streamStore, changeSource }),
|
|
5899
|
+
queue,
|
|
5900
|
+
mailboxStore,
|
|
5901
|
+
ownedChangeSource
|
|
5902
|
+
});
|
|
5903
|
+
}
|
|
5904
|
+
admit(tree, work) {
|
|
5905
|
+
return postgresTransaction(this.#pool, async (tx) => {
|
|
5906
|
+
await tx.query("SELECT pg_advisory_xact_lock(hashtextextended($1, 0))", [
|
|
5907
|
+
`zukhruf:agent-tree:${JSON.stringify([this.#schema, tree.userId, tree.treeId])}`
|
|
5908
|
+
]);
|
|
5909
|
+
return work({
|
|
5910
|
+
store: this.store.bind(tx),
|
|
5911
|
+
streams: this.#streamStore.bind(tx),
|
|
5912
|
+
mailboxStore: this.mailboxStore.bind(tx),
|
|
5913
|
+
queue: this.queue.bind(tx)
|
|
5914
|
+
});
|
|
5915
|
+
});
|
|
5916
|
+
}
|
|
5917
|
+
async [Symbol.asyncDispose]() {
|
|
5918
|
+
await this.#ownedChangeSource?.close();
|
|
5919
|
+
}
|
|
5920
|
+
};
|
|
5921
|
+
|
|
5375
5922
|
// packages/experimental/src/zukhruf/tool.ts
|
|
5376
5923
|
function defineTool(definition) {
|
|
5377
5924
|
return definition;
|