@deepagents/experimental 7.1.1 → 7.1.3
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 +0 -1
- package/dist/index.js +252 -290
- package/dist/index.js.map +4 -4
- package/dist/providers/account-tokens.d.ts.map +1 -1
- package/dist/providers/claude/index.js +59 -51
- package/dist/providers/claude/index.js.map +2 -2
- package/dist/providers/claude/local-credentials.d.ts +1 -1
- package/dist/providers/claude/local-credentials.d.ts.map +1 -1
- package/dist/providers/codex/index.js +57 -49
- package/dist/providers/codex/index.js.map +2 -2
- package/dist/providers/codex/local-credentials.d.ts +1 -1
- package/dist/providers/codex/local-credentials.d.ts.map +1 -1
- package/dist/zukhruf/index.d.ts +0 -1
- package/dist/zukhruf/index.d.ts.map +1 -1
- package/dist/zukhruf/index.js +252 -290
- package/dist/zukhruf/index.js.map +4 -4
- package/dist/zukhruf/mailbox/coordinator.d.ts.map +1 -1
- package/dist/zukhruf/mailbox/sqlite.store.d.ts.map +1 -1
- package/dist/zukhruf/plugins/conversation-scheduling/index.js +12 -0
- package/dist/zukhruf/plugins/conversation-scheduling/index.js.map +3 -3
- package/dist/zukhruf/plugins/http/index.d.ts.map +1 -1
- package/dist/zukhruf/plugins/http/index.js +20 -164
- package/dist/zukhruf/plugins/http/index.js.map +4 -4
- package/dist/zukhruf/plugins/schedules/http.d.ts.map +1 -1
- package/dist/zukhruf/plugins/schedules/http.js +37 -180
- package/dist/zukhruf/plugins/schedules/http.js.map +4 -4
- package/dist/zukhruf/plugins/schedules/index.js +14 -1
- package/dist/zukhruf/plugins/schedules/index.js.map +3 -3
- package/dist/zukhruf/plugins/schedules/scheduled-tasks.d.ts.map +1 -1
- package/dist/zukhruf/plugins/uploads/http.d.ts.map +1 -1
- package/dist/zukhruf/plugins/uploads/http.js +30 -173
- package/dist/zukhruf/plugins/uploads/http.js.map +4 -4
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts +6 -2
- package/dist/zukhruf/queue/pg-boss.turn-queue.d.ts.map +1 -1
- package/dist/zukhruf/runtime/conversation-status/projector.d.ts.map +1 -1
- package/package.json +29 -13
- package/dist/zukhruf/plugins/http/parse.d.ts +0 -3
- package/dist/zukhruf/plugins/http/parse.d.ts.map +0 -1
- package/dist/zukhruf/plugins/http/validator.d.ts +0 -34
- package/dist/zukhruf/plugins/http/validator.d.ts.map +0 -1
- package/dist/zukhruf/render-turn.d.ts +0 -9
- package/dist/zukhruf/render-turn.d.ts.map +0 -1
package/dist/index.js
CHANGED
|
@@ -339,8 +339,7 @@ __export(zukhruf_exports, {
|
|
|
339
339
|
defineInstructions: () => defineInstructions,
|
|
340
340
|
defineSandbox: () => defineSandbox,
|
|
341
341
|
defineStack: () => defineStack,
|
|
342
|
-
defineTool: () => defineTool
|
|
343
|
-
renderTurn: () => renderTurn
|
|
342
|
+
defineTool: () => defineTool
|
|
344
343
|
});
|
|
345
344
|
|
|
346
345
|
// packages/experimental/src/zukhruf/agent.ts
|
|
@@ -533,6 +532,7 @@ function defineInstructions(...fragments) {
|
|
|
533
532
|
}
|
|
534
533
|
|
|
535
534
|
// packages/experimental/src/zukhruf/mailbox/sqlite.store.ts
|
|
535
|
+
import { transaction } from "@zukhruf/sqlite-transaction";
|
|
536
536
|
import { DatabaseSync } from "node:sqlite";
|
|
537
537
|
import { z } from "zod";
|
|
538
538
|
|
|
@@ -644,59 +644,70 @@ var mailboxItemRowsSchema = z.array(
|
|
|
644
644
|
);
|
|
645
645
|
var SqliteMailboxStore = class extends MailboxStore {
|
|
646
646
|
#database;
|
|
647
|
-
#closed = false;
|
|
648
647
|
constructor(path3) {
|
|
649
648
|
super();
|
|
650
649
|
this.#database = new DatabaseSync(path3, { timeout: BUSY_TIMEOUT_MS });
|
|
651
650
|
this.#database.exec(DDL);
|
|
652
651
|
}
|
|
653
652
|
async beginTurn(recipient, turnId) {
|
|
654
|
-
|
|
655
|
-
this.#database
|
|
656
|
-
|
|
653
|
+
transaction(
|
|
654
|
+
this.#database,
|
|
655
|
+
() => {
|
|
656
|
+
this.#database.prepare(
|
|
657
|
+
`INSERT INTO zukhruf_mailbox_activity (
|
|
657
658
|
recipient_chat_id,
|
|
658
659
|
recipient_user_id,
|
|
659
660
|
turn_id
|
|
660
661
|
) VALUES (?, ?, ?)
|
|
661
662
|
ON CONFLICT (recipient_chat_id, recipient_user_id)
|
|
662
663
|
DO UPDATE SET turn_id = excluded.turn_id`
|
|
663
|
-
|
|
664
|
-
|
|
664
|
+
).run(recipient.chatId, recipient.userId, turnId);
|
|
665
|
+
},
|
|
666
|
+
{ mode: "immediate" }
|
|
667
|
+
);
|
|
665
668
|
}
|
|
666
669
|
async enqueue(communication) {
|
|
667
|
-
return
|
|
668
|
-
|
|
669
|
-
|
|
670
|
-
this.#
|
|
671
|
-
|
|
670
|
+
return transaction(
|
|
671
|
+
this.#database,
|
|
672
|
+
() => {
|
|
673
|
+
const consumedTerminal = communication.type === InterAgentCommunicationTypes.FinalAnswer && this.#hasConsumedTerminal(communication.id);
|
|
674
|
+
if (!consumedTerminal) {
|
|
675
|
+
this.#database.prepare(
|
|
676
|
+
`INSERT OR IGNORE INTO zukhruf_mailbox_items (
|
|
672
677
|
recipient_chat_id,
|
|
673
678
|
recipient_user_id,
|
|
674
679
|
communication
|
|
675
680
|
) VALUES (?, ?, ?)`
|
|
676
|
-
|
|
677
|
-
|
|
678
|
-
|
|
679
|
-
|
|
680
|
-
|
|
681
|
-
|
|
682
|
-
|
|
683
|
-
|
|
684
|
-
|
|
685
|
-
|
|
681
|
+
).run(
|
|
682
|
+
communication.recipient.chatId,
|
|
683
|
+
communication.recipient.userId,
|
|
684
|
+
JSON.stringify(communication)
|
|
685
|
+
);
|
|
686
|
+
}
|
|
687
|
+
return {
|
|
688
|
+
recipientActive: this.#isTurnActive(communication.recipient)
|
|
689
|
+
};
|
|
690
|
+
},
|
|
691
|
+
{ mode: "immediate" }
|
|
692
|
+
);
|
|
686
693
|
}
|
|
687
694
|
async endTurn(recipient, turnId) {
|
|
688
|
-
return
|
|
689
|
-
|
|
690
|
-
|
|
695
|
+
return transaction(
|
|
696
|
+
this.#database,
|
|
697
|
+
() => {
|
|
698
|
+
const result = this.#database.prepare(
|
|
699
|
+
`DELETE FROM zukhruf_mailbox_activity
|
|
691
700
|
WHERE recipient_chat_id = ?
|
|
692
701
|
AND recipient_user_id = ?
|
|
693
702
|
AND turn_id = ?`
|
|
694
|
-
|
|
695
|
-
|
|
696
|
-
|
|
697
|
-
|
|
698
|
-
|
|
699
|
-
|
|
703
|
+
).run(recipient.chatId, recipient.userId, turnId);
|
|
704
|
+
return {
|
|
705
|
+
hasPending: this.#hasPending(recipient),
|
|
706
|
+
turnEnded: result.changes > 0
|
|
707
|
+
};
|
|
708
|
+
},
|
|
709
|
+
{ mode: "immediate" }
|
|
710
|
+
);
|
|
700
711
|
}
|
|
701
712
|
async hasPending(recipient) {
|
|
702
713
|
return this.#hasPending(recipient);
|
|
@@ -737,57 +748,46 @@ var SqliteMailboxStore = class extends MailboxStore {
|
|
|
737
748
|
return this.#drain(recipient, true);
|
|
738
749
|
}
|
|
739
750
|
#drain(recipient, leadingQueueOnly) {
|
|
740
|
-
return
|
|
741
|
-
|
|
742
|
-
|
|
743
|
-
|
|
751
|
+
return transaction(
|
|
752
|
+
this.#database,
|
|
753
|
+
() => {
|
|
754
|
+
const items = mailboxItemRowsSchema.parse(
|
|
755
|
+
this.#database.prepare(
|
|
756
|
+
`SELECT sequence, communication
|
|
744
757
|
FROM zukhruf_mailbox_items
|
|
745
758
|
WHERE recipient_chat_id = ?
|
|
746
759
|
AND recipient_user_id = ?
|
|
747
760
|
ORDER BY sequence`
|
|
748
|
-
|
|
749
|
-
|
|
750
|
-
|
|
751
|
-
|
|
752
|
-
|
|
753
|
-
|
|
754
|
-
|
|
755
|
-
|
|
761
|
+
).all(recipient.chatId, recipient.userId)
|
|
762
|
+
);
|
|
763
|
+
const firstTrigger = leadingQueueOnly ? items.findIndex(({ communication }) => communication.triggerTurn) : -1;
|
|
764
|
+
const consumed = firstTrigger === -1 ? items : items.slice(0, firstTrigger);
|
|
765
|
+
const lastConsumed = consumed.at(-1);
|
|
766
|
+
if (lastConsumed) {
|
|
767
|
+
const tombstone = this.#database.prepare(
|
|
768
|
+
`INSERT OR IGNORE
|
|
756
769
|
INTO zukhruf_mailbox_consumed_terminal_ids (communication_id)
|
|
757
770
|
VALUES (?)`
|
|
758
|
-
|
|
759
|
-
|
|
760
|
-
|
|
761
|
-
|
|
771
|
+
);
|
|
772
|
+
for (const { communication } of consumed) {
|
|
773
|
+
if (communication.type === InterAgentCommunicationTypes.FinalAnswer) {
|
|
774
|
+
tombstone.run(communication.id);
|
|
775
|
+
}
|
|
762
776
|
}
|
|
763
|
-
|
|
764
|
-
|
|
765
|
-
`DELETE FROM zukhruf_mailbox_items
|
|
777
|
+
this.#database.prepare(
|
|
778
|
+
`DELETE FROM zukhruf_mailbox_items
|
|
766
779
|
WHERE recipient_chat_id = ?
|
|
767
780
|
AND recipient_user_id = ?
|
|
768
781
|
AND sequence <= ?`
|
|
769
|
-
|
|
770
|
-
|
|
771
|
-
|
|
772
|
-
|
|
773
|
-
|
|
774
|
-
|
|
775
|
-
this.#database.exec("BEGIN IMMEDIATE");
|
|
776
|
-
try {
|
|
777
|
-
const result = operation();
|
|
778
|
-
this.#database.exec("COMMIT");
|
|
779
|
-
return result;
|
|
780
|
-
} catch (error) {
|
|
781
|
-
try {
|
|
782
|
-
this.#database.exec("ROLLBACK");
|
|
783
|
-
} catch {
|
|
784
|
-
}
|
|
785
|
-
throw error;
|
|
786
|
-
}
|
|
782
|
+
).run(recipient.chatId, recipient.userId, lastConsumed.sequence);
|
|
783
|
+
}
|
|
784
|
+
return consumed.map(({ communication }) => communication);
|
|
785
|
+
},
|
|
786
|
+
{ mode: "immediate" }
|
|
787
|
+
);
|
|
787
788
|
}
|
|
788
789
|
close() {
|
|
789
|
-
if (this.#
|
|
790
|
-
this.#closed = true;
|
|
790
|
+
if (!this.#database.isOpen) return;
|
|
791
791
|
this.#database.close();
|
|
792
792
|
}
|
|
793
793
|
[Symbol.dispose]() {
|
|
@@ -807,87 +807,115 @@ var TurnQueue = class {
|
|
|
807
807
|
// packages/experimental/src/zukhruf/queue/pg-boss.turn-queue.ts
|
|
808
808
|
var MAX_TIMEOUT_MS = 2147483647;
|
|
809
809
|
var queueCatalogRowSchema = z2.object({ table: z2.string() });
|
|
810
|
+
function deadLetterQueueOf(queue) {
|
|
811
|
+
return `${queue}-dead`;
|
|
812
|
+
}
|
|
810
813
|
var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
811
814
|
#boss;
|
|
812
815
|
#queue;
|
|
813
816
|
#schema;
|
|
814
|
-
#schemaConfigurationError;
|
|
815
817
|
#table;
|
|
816
|
-
#expireInSeconds;
|
|
817
818
|
#cancelIntentTtlMs;
|
|
818
|
-
#heartbeatSeconds;
|
|
819
819
|
#pollingIntervalSeconds;
|
|
820
820
|
#active = /* @__PURE__ */ new Map();
|
|
821
|
+
/** Cancels that arrived before a local worker runs the turn; each expires on its own timer. */
|
|
821
822
|
#cancelIntents = /* @__PURE__ */ new Map();
|
|
822
|
-
|
|
823
|
-
constructor(boss, options = {}) {
|
|
823
|
+
constructor(boss, settings, table) {
|
|
824
824
|
super();
|
|
825
825
|
this.#boss = boss;
|
|
826
|
-
this.#queue =
|
|
826
|
+
this.#queue = settings.queue;
|
|
827
|
+
this.#schema = settings.schema;
|
|
828
|
+
this.#table = table;
|
|
829
|
+
this.#cancelIntentTtlMs = settings.expireInSeconds * 1e3;
|
|
830
|
+
this.#pollingIntervalSeconds = settings.pollingIntervalSeconds;
|
|
831
|
+
}
|
|
832
|
+
/**
|
|
833
|
+
* Opens the queue: creates the turn queue and its dead-letter queue when
|
|
834
|
+
* they are missing, and checks that the schema is the PgBoss schema.
|
|
835
|
+
*/
|
|
836
|
+
static async create(boss, options = {}) {
|
|
837
|
+
const settings = _PgBossTurnQueue.#settings(boss, options);
|
|
838
|
+
const table = await _PgBossTurnQueue.#openQueue(boss, settings);
|
|
839
|
+
return new _PgBossTurnQueue(boss, settings, table);
|
|
840
|
+
}
|
|
841
|
+
static #settings(boss, options) {
|
|
827
842
|
const db = boss.getDb();
|
|
828
843
|
const configuredSchema = "config" in db && isRecord(db.config) ? db.config.schema : void 0;
|
|
829
844
|
const bossSchema = typeof configuredSchema === "string" ? configuredSchema : void 0;
|
|
830
|
-
|
|
845
|
+
const expireInSeconds = options.expireInSeconds ?? 3600;
|
|
846
|
+
const cancelIntentTtlMs = expireInSeconds * 1e3;
|
|
847
|
+
if (!Number.isFinite(expireInSeconds) || expireInSeconds <= 0 || !Number.isSafeInteger(cancelIntentTtlMs) || cancelIntentTtlMs > Number.MAX_SAFE_INTEGER - Date.now()) {
|
|
848
|
+
throw new Error(
|
|
849
|
+
"PgBossTurnQueue expireInSeconds must be a finite positive duration"
|
|
850
|
+
);
|
|
851
|
+
}
|
|
831
852
|
if (options.schema === void 0 && bossSchema === void 0) {
|
|
832
|
-
|
|
833
|
-
|
|
834
|
-
|
|
853
|
+
throw new Error(
|
|
854
|
+
"PgBossTurnQueue cannot read the schema from a custom PgBoss database adapter; pass the PgBoss schema in options.schema"
|
|
855
|
+
);
|
|
835
856
|
}
|
|
836
|
-
|
|
837
|
-
this.#cancelIntentTtlMs = this.#expireInSeconds * 1e3;
|
|
838
|
-
if (!Number.isFinite(this.#expireInSeconds) || this.#expireInSeconds <= 0 || !Number.isSafeInteger(this.#cancelIntentTtlMs) || this.#cancelIntentTtlMs > Number.MAX_SAFE_INTEGER - Date.now()) {
|
|
857
|
+
if (options.schema !== void 0 && bossSchema !== void 0 && options.schema !== bossSchema) {
|
|
839
858
|
throw new Error(
|
|
840
|
-
|
|
859
|
+
`PgBossTurnQueue schema "${options.schema}" does not match the PgBoss schema "${bossSchema}"`
|
|
841
860
|
);
|
|
842
861
|
}
|
|
843
|
-
|
|
844
|
-
|
|
845
|
-
|
|
846
|
-
|
|
847
|
-
|
|
848
|
-
|
|
849
|
-
|
|
850
|
-
return `${this.#queue}-dead`;
|
|
862
|
+
return {
|
|
863
|
+
queue: options.queue ?? "zukhruf-turns",
|
|
864
|
+
schema: options.schema ?? bossSchema ?? "pgboss",
|
|
865
|
+
expireInSeconds,
|
|
866
|
+
heartbeatSeconds: options.heartbeatSeconds ?? 30,
|
|
867
|
+
pollingIntervalSeconds: options.pollingIntervalSeconds ?? 1
|
|
868
|
+
};
|
|
851
869
|
}
|
|
852
|
-
|
|
853
|
-
|
|
854
|
-
|
|
870
|
+
/** Creates the queues when they are missing; returns the turn queue's job table. */
|
|
871
|
+
static async #openQueue(boss, settings) {
|
|
872
|
+
const deadLetterQueue = deadLetterQueueOf(settings.queue);
|
|
873
|
+
if (!await boss.getQueue(deadLetterQueue)) {
|
|
874
|
+
await boss.createQueue(deadLetterQueue);
|
|
855
875
|
}
|
|
856
|
-
|
|
857
|
-
await this.#boss.createQueue(this.deadLetterQueue);
|
|
858
|
-
}
|
|
859
|
-
let queue = await this.#boss.getQueue(this.#queue);
|
|
876
|
+
let queue = await boss.getQueue(settings.queue);
|
|
860
877
|
if (!queue) {
|
|
861
|
-
await
|
|
878
|
+
await boss.createQueue(settings.queue, {
|
|
862
879
|
policy: "key_strict_fifo",
|
|
863
880
|
retryLimit: 0,
|
|
864
|
-
expireInSeconds:
|
|
865
|
-
heartbeatSeconds:
|
|
881
|
+
expireInSeconds: settings.expireInSeconds,
|
|
882
|
+
heartbeatSeconds: settings.heartbeatSeconds,
|
|
866
883
|
deleteAfterSeconds: 0,
|
|
867
|
-
deadLetter:
|
|
884
|
+
deadLetter: deadLetterQueue
|
|
868
885
|
});
|
|
869
|
-
queue = await
|
|
886
|
+
queue = await boss.getQueue(settings.queue);
|
|
887
|
+
}
|
|
888
|
+
if (!queue) {
|
|
889
|
+
throw new Error(`Queue "${settings.queue}" was not initialized`);
|
|
870
890
|
}
|
|
871
|
-
|
|
872
|
-
this.#table = queue.table;
|
|
891
|
+
const { table } = queue;
|
|
873
892
|
try {
|
|
874
|
-
const catalog = await
|
|
893
|
+
const catalog = await boss.getDb().executeSql(
|
|
875
894
|
`SELECT table_name AS "table"
|
|
876
|
-
FROM ${_PgBossTurnQueue.#quoteIdentifier(
|
|
895
|
+
FROM ${_PgBossTurnQueue.#quoteIdentifier(settings.schema)}."queue"
|
|
877
896
|
WHERE name = $1`,
|
|
878
|
-
[
|
|
897
|
+
[settings.queue]
|
|
879
898
|
);
|
|
880
899
|
const catalogRow = queueCatalogRowSchema.safeParse(catalog.rows[0]);
|
|
881
|
-
if (catalog.rows.length !== 1 || !catalogRow.success || catalogRow.data.table !==
|
|
900
|
+
if (catalog.rows.length !== 1 || !catalogRow.success || catalogRow.data.table !== table) {
|
|
882
901
|
throw new Error("queue catalog does not match");
|
|
883
902
|
}
|
|
884
|
-
await
|
|
903
|
+
await boss.getDb().executeSql(
|
|
904
|
+
`SELECT 1 FROM ${_PgBossTurnQueue.#relationOf(settings.schema, table)} WHERE false`
|
|
905
|
+
);
|
|
885
906
|
} catch (cause) {
|
|
886
907
|
throw new Error(
|
|
887
|
-
`PgBossTurnQueue schema "${
|
|
908
|
+
`PgBossTurnQueue schema "${settings.schema}" does not match the PgBoss queue; pass the PgBoss schema in options.schema`,
|
|
888
909
|
{ cause }
|
|
889
910
|
);
|
|
890
911
|
}
|
|
912
|
+
return table;
|
|
913
|
+
}
|
|
914
|
+
get queue() {
|
|
915
|
+
return this.#queue;
|
|
916
|
+
}
|
|
917
|
+
get deadLetterQueue() {
|
|
918
|
+
return deadLetterQueueOf(this.#queue);
|
|
891
919
|
}
|
|
892
920
|
async push(turn) {
|
|
893
921
|
const jobId = uuidv7();
|
|
@@ -936,6 +964,7 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
936
964
|
}
|
|
937
965
|
}
|
|
938
966
|
async consume(handler, options) {
|
|
967
|
+
const disposal = new AbortController();
|
|
939
968
|
const turnWorkerId = await this.#boss.work(
|
|
940
969
|
this.#queue,
|
|
941
970
|
{
|
|
@@ -943,6 +972,10 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
943
972
|
pollingIntervalSeconds: this.#pollingIntervalSeconds
|
|
944
973
|
},
|
|
945
974
|
async ([job]) => {
|
|
975
|
+
if (disposal.signal.aborted) {
|
|
976
|
+
await this.#handBack(this.#queue, job.id);
|
|
977
|
+
return;
|
|
978
|
+
}
|
|
946
979
|
let parked = false;
|
|
947
980
|
const localAbort = new AbortController();
|
|
948
981
|
const active = this.#active.get(job.data.streamId) ?? /* @__PURE__ */ new Set();
|
|
@@ -984,6 +1017,10 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
984
1017
|
pollingIntervalSeconds: this.#pollingIntervalSeconds
|
|
985
1018
|
},
|
|
986
1019
|
async ([job]) => {
|
|
1020
|
+
if (disposal.signal.aborted) {
|
|
1021
|
+
await this.#handBack(this.deadLetterQueue, job.id);
|
|
1022
|
+
return;
|
|
1023
|
+
}
|
|
987
1024
|
try {
|
|
988
1025
|
await options.onOrphaned(
|
|
989
1026
|
job.data,
|
|
@@ -999,6 +1036,7 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
999
1036
|
);
|
|
1000
1037
|
return {
|
|
1001
1038
|
[Symbol.asyncDispose]: async () => {
|
|
1039
|
+
disposal.abort();
|
|
1002
1040
|
await this.#boss.offWork(this.#queue, {
|
|
1003
1041
|
id: turnWorkerId,
|
|
1004
1042
|
wait: options.waitForActive
|
|
@@ -1030,50 +1068,40 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
1030
1068
|
}
|
|
1031
1069
|
}
|
|
1032
1070
|
#rememberCancelIntent(streamId) {
|
|
1033
|
-
this.#
|
|
1034
|
-
this.#
|
|
1035
|
-
this.#scheduleCancelIntentExpiry();
|
|
1036
|
-
}
|
|
1037
|
-
#hasCancelIntent(streamId) {
|
|
1038
|
-
this.#pruneCancelIntents();
|
|
1039
|
-
return this.#cancelIntents.has(streamId);
|
|
1040
|
-
}
|
|
1041
|
-
#pruneCancelIntents() {
|
|
1042
|
-
const now = Date.now();
|
|
1043
|
-
for (const [streamId, expiresAt] of this.#cancelIntents) {
|
|
1044
|
-
if (expiresAt <= now) this.#cancelIntents.delete(streamId);
|
|
1045
|
-
}
|
|
1046
|
-
this.#scheduleCancelIntentExpiry();
|
|
1047
|
-
}
|
|
1048
|
-
#forgetCancelIntent(streamId) {
|
|
1049
|
-
this.#cancelIntents.delete(streamId);
|
|
1050
|
-
this.#scheduleCancelIntentExpiry();
|
|
1071
|
+
this.#forgetCancelIntent(streamId);
|
|
1072
|
+
this.#armCancelIntent(streamId, Date.now() + this.#cancelIntentTtlMs);
|
|
1051
1073
|
}
|
|
1052
|
-
#
|
|
1053
|
-
if (this.#cancelIntentExpiryTimer !== void 0) {
|
|
1054
|
-
globalThis.clearTimeout(this.#cancelIntentExpiryTimer);
|
|
1055
|
-
this.#cancelIntentExpiryTimer = void 0;
|
|
1056
|
-
}
|
|
1057
|
-
let nextExpiry = Number.POSITIVE_INFINITY;
|
|
1058
|
-
for (const expiresAt of this.#cancelIntents.values()) {
|
|
1059
|
-
nextExpiry = Math.min(nextExpiry, expiresAt);
|
|
1060
|
-
}
|
|
1061
|
-
if (!Number.isFinite(nextExpiry)) return;
|
|
1074
|
+
#armCancelIntent(streamId, expiresAt) {
|
|
1062
1075
|
const timer = globalThis.setTimeout(
|
|
1063
1076
|
() => {
|
|
1064
|
-
if (this.#
|
|
1065
|
-
this.#
|
|
1066
|
-
this.#pruneCancelIntents();
|
|
1077
|
+
if (Date.now() < expiresAt) this.#armCancelIntent(streamId, expiresAt);
|
|
1078
|
+
else this.#cancelIntents.delete(streamId);
|
|
1067
1079
|
},
|
|
1068
|
-
Math.min(MAX_TIMEOUT_MS, Math.max(1,
|
|
1080
|
+
Math.min(MAX_TIMEOUT_MS, Math.max(1, expiresAt - Date.now()))
|
|
1069
1081
|
);
|
|
1070
|
-
this.#cancelIntentExpiryTimer = timer;
|
|
1071
1082
|
timer.unref();
|
|
1083
|
+
this.#cancelIntents.set(streamId, { expiresAt, timer });
|
|
1084
|
+
}
|
|
1085
|
+
#hasCancelIntent(streamId) {
|
|
1086
|
+
const intent = this.#cancelIntents.get(streamId);
|
|
1087
|
+
return intent !== void 0 && intent.expiresAt > Date.now();
|
|
1088
|
+
}
|
|
1089
|
+
#forgetCancelIntent(streamId) {
|
|
1090
|
+
globalThis.clearTimeout(this.#cancelIntents.get(streamId)?.timer);
|
|
1091
|
+
this.#cancelIntents.delete(streamId);
|
|
1092
|
+
}
|
|
1093
|
+
/**
|
|
1094
|
+
* Returns a claimed job to its queue unrun, in its original order: cancel
|
|
1095
|
+
* releases its strict-FIFO key, as parking does, and resume makes it
|
|
1096
|
+
* fetchable again with its id, created_on and retry count unchanged.
|
|
1097
|
+
* pg-boss's later completion of the job finds it no longer active and
|
|
1098
|
+
* changes nothing.
|
|
1099
|
+
*/
|
|
1100
|
+
async #handBack(queue, id) {
|
|
1101
|
+
await this.#boss.cancel(queue, id);
|
|
1102
|
+
await this.#boss.resume(queue, id);
|
|
1072
1103
|
}
|
|
1073
1104
|
async #deleteIfStillQueued(ids) {
|
|
1074
|
-
if (!this.#table) {
|
|
1075
|
-
throw new Error("PgBossTurnQueue.initialize() must run before cancel()");
|
|
1076
|
-
}
|
|
1077
1105
|
await this.#boss.getDb().executeSql(
|
|
1078
1106
|
`DELETE FROM ${this.#relation()}
|
|
1079
1107
|
WHERE name = $1
|
|
@@ -1083,12 +1111,10 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
1083
1111
|
);
|
|
1084
1112
|
}
|
|
1085
1113
|
#relation() {
|
|
1086
|
-
|
|
1087
|
-
|
|
1088
|
-
|
|
1089
|
-
|
|
1090
|
-
}
|
|
1091
|
-
return `${_PgBossTurnQueue.#quoteIdentifier(this.#schema)}.${_PgBossTurnQueue.#quoteIdentifier(this.#table)}`;
|
|
1114
|
+
return _PgBossTurnQueue.#relationOf(this.#schema, this.#table);
|
|
1115
|
+
}
|
|
1116
|
+
static #relationOf(schema, table) {
|
|
1117
|
+
return `${_PgBossTurnQueue.#quoteIdentifier(schema)}.${_PgBossTurnQueue.#quoteIdentifier(table)}`;
|
|
1092
1118
|
}
|
|
1093
1119
|
static #quoteIdentifier(identifier) {
|
|
1094
1120
|
return `"${identifier.replaceAll('"', '""')}"`;
|
|
@@ -1118,79 +1144,6 @@ var PgBossTurnQueue = class _PgBossTurnQueue extends TurnQueue {
|
|
|
1118
1144
|
}
|
|
1119
1145
|
};
|
|
1120
1146
|
|
|
1121
|
-
// packages/experimental/src/zukhruf/render-turn.ts
|
|
1122
|
-
import { styleText } from "node:util";
|
|
1123
|
-
import { isRecord as isRecord2 } from "@deepagents/context";
|
|
1124
|
-
var dim = (text) => styleText("dim", text);
|
|
1125
|
-
var red = (text) => styleText("red", text);
|
|
1126
|
-
function oneLine(value, max = 88) {
|
|
1127
|
-
const text = String(value ?? "").replaceAll(/\s+/g, " ").trim();
|
|
1128
|
-
return text.length > max ? `${text.slice(0, max - 1)}\u2026` : text;
|
|
1129
|
-
}
|
|
1130
|
-
function describeToolCall(toolName, input) {
|
|
1131
|
-
const args = isRecord2(input) ? input : {};
|
|
1132
|
-
switch (toolName) {
|
|
1133
|
-
case "spawn_agent":
|
|
1134
|
-
return `\u21C4 spawn ${args.agent_type} "${args.task_name}" \u2014 ${args.message}`;
|
|
1135
|
-
case "send_message":
|
|
1136
|
-
return `\u21C4 mail ${args.target} \u2014 ${args.message}`;
|
|
1137
|
-
case "followup_task":
|
|
1138
|
-
return `\u21C4 follow-up ${args.target} \u2014 ${args.message}`;
|
|
1139
|
-
case "list_agents":
|
|
1140
|
-
return "\u21C4 list agents";
|
|
1141
|
-
default:
|
|
1142
|
-
return `\xB7 ${toolName}`;
|
|
1143
|
-
}
|
|
1144
|
-
}
|
|
1145
|
-
async function renderTurn(stream) {
|
|
1146
|
-
const toolNames = /* @__PURE__ */ new Map();
|
|
1147
|
-
let midText = false;
|
|
1148
|
-
let thinkingShown = false;
|
|
1149
|
-
const breakText = () => {
|
|
1150
|
-
if (midText) process.stdout.write("\n");
|
|
1151
|
-
midText = false;
|
|
1152
|
-
};
|
|
1153
|
-
for await (const chunk of stream) {
|
|
1154
|
-
switch (chunk.type) {
|
|
1155
|
-
case "reasoning-start":
|
|
1156
|
-
if (!thinkingShown) {
|
|
1157
|
-
console.log(dim("\u2026"));
|
|
1158
|
-
thinkingShown = true;
|
|
1159
|
-
}
|
|
1160
|
-
break;
|
|
1161
|
-
case "text-delta":
|
|
1162
|
-
process.stdout.write(chunk.delta);
|
|
1163
|
-
midText = true;
|
|
1164
|
-
break;
|
|
1165
|
-
case "text-end":
|
|
1166
|
-
breakText();
|
|
1167
|
-
break;
|
|
1168
|
-
case "tool-input-available":
|
|
1169
|
-
toolNames.set(chunk.toolCallId, chunk.toolName);
|
|
1170
|
-
breakText();
|
|
1171
|
-
console.log(dim(describeToolCall(chunk.toolName, chunk.input)));
|
|
1172
|
-
break;
|
|
1173
|
-
case "tool-output-error":
|
|
1174
|
-
breakText();
|
|
1175
|
-
console.log(
|
|
1176
|
-
red(
|
|
1177
|
-
`\u2717 ${toolNames.get(chunk.toolCallId) ?? "tool"}: ${oneLine(chunk.errorText)}`
|
|
1178
|
-
)
|
|
1179
|
-
);
|
|
1180
|
-
break;
|
|
1181
|
-
case "error":
|
|
1182
|
-
breakText();
|
|
1183
|
-
console.log(red(`\u2717 ${oneLine(chunk.errorText)}`));
|
|
1184
|
-
break;
|
|
1185
|
-
case "abort":
|
|
1186
|
-
breakText();
|
|
1187
|
-
console.log(dim("\xB7 turn aborted"));
|
|
1188
|
-
break;
|
|
1189
|
-
}
|
|
1190
|
-
}
|
|
1191
|
-
breakText();
|
|
1192
|
-
}
|
|
1193
|
-
|
|
1194
1147
|
// packages/experimental/src/zukhruf/runtime/agent-runtime.ts
|
|
1195
1148
|
import {
|
|
1196
1149
|
ContextEngine as ContextEngine5
|
|
@@ -1498,7 +1451,7 @@ var AgentHistoryForker = class {
|
|
|
1498
1451
|
await this.#store.updateChat(child.conversation.chatId, (chat2) => {
|
|
1499
1452
|
const metadata = chat2.metadata;
|
|
1500
1453
|
const zukhruf = metadata?.zukhruf;
|
|
1501
|
-
if (!
|
|
1454
|
+
if (!isRecord2(zukhruf)) {
|
|
1502
1455
|
throw new Error(
|
|
1503
1456
|
`spawn_agent: child chat "${child.conversation.chatId}" is missing Zukhruf metadata`
|
|
1504
1457
|
);
|
|
@@ -1604,7 +1557,7 @@ var AgentHistoryForker = class {
|
|
|
1604
1557
|
await this.#store.updateChat(child.conversation.chatId, (chat2) => {
|
|
1605
1558
|
const metadata = chat2.metadata;
|
|
1606
1559
|
const zukhruf = metadata?.zukhruf;
|
|
1607
|
-
if (!
|
|
1560
|
+
if (!isRecord2(zukhruf)) {
|
|
1608
1561
|
throw new Error(
|
|
1609
1562
|
`spawn_agent: child chat "${child.conversation.chatId}" is missing Zukhruf metadata`
|
|
1610
1563
|
);
|
|
@@ -1696,12 +1649,12 @@ function isForkTurnBoundary(message) {
|
|
|
1696
1649
|
return interAgentCommunication(message)?.triggerTurn ?? true;
|
|
1697
1650
|
}
|
|
1698
1651
|
function interAgentCommunication(message) {
|
|
1699
|
-
if (!
|
|
1652
|
+
if (!isRecord2(message.metadata)) return void 0;
|
|
1700
1653
|
if (!Object.hasOwn(message.metadata, "interAgentCommunication")) {
|
|
1701
1654
|
return void 0;
|
|
1702
1655
|
}
|
|
1703
1656
|
const communication = message.metadata.interAgentCommunication;
|
|
1704
|
-
if (!
|
|
1657
|
+
if (!isRecord2(communication) || typeof communication.triggerTurn !== "boolean") {
|
|
1705
1658
|
throw new Error(
|
|
1706
1659
|
`spawn_agent: message "${message.id}" has invalid inter-agent metadata`
|
|
1707
1660
|
);
|
|
@@ -1709,7 +1662,7 @@ function interAgentCommunication(message) {
|
|
|
1709
1662
|
return { triggerTurn: communication.triggerTurn };
|
|
1710
1663
|
}
|
|
1711
1664
|
function parseHistoryForkRecord(value) {
|
|
1712
|
-
if (!
|
|
1665
|
+
if (!isRecord2(value)) {
|
|
1713
1666
|
throw new Error("spawn_agent: invalid persisted history-fork metadata");
|
|
1714
1667
|
}
|
|
1715
1668
|
const { forkTurns, parentChatId, parentHeadMessageId, sourceMessageIds } = value;
|
|
@@ -1728,11 +1681,11 @@ function parseHistoryForkRecord(value) {
|
|
|
1728
1681
|
function isForkTurns(value) {
|
|
1729
1682
|
return value === "none" || value === "all" || typeof value === "number" && Number.isSafeInteger(value) && value > 0;
|
|
1730
1683
|
}
|
|
1731
|
-
function
|
|
1684
|
+
function isRecord2(value) {
|
|
1732
1685
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
1733
1686
|
}
|
|
1734
1687
|
function isEmptyAssistantPlaceholder(value) {
|
|
1735
|
-
return
|
|
1688
|
+
return isRecord2(value) && typeof value.id === "string" && value.role === "assistant" && Array.isArray(value.parts) && value.parts.length === 0;
|
|
1736
1689
|
}
|
|
1737
1690
|
function arraysEqual(left, right) {
|
|
1738
1691
|
return left.length === right.length && left.every((value, index) => value === right[index]);
|
|
@@ -2178,12 +2131,12 @@ var AgentControlPlane = class _AgentControlPlane {
|
|
|
2178
2131
|
|
|
2179
2132
|
// packages/experimental/src/zukhruf/mailbox/coordinator.ts
|
|
2180
2133
|
import { setTimeout as delay } from "node:timers/promises";
|
|
2134
|
+
import pMemoize from "p-memoize";
|
|
2181
2135
|
var WAIT_POLL_INTERVAL_MS = 50;
|
|
2182
2136
|
var MailboxCoordinator = class {
|
|
2183
2137
|
#store;
|
|
2184
2138
|
#queue;
|
|
2185
2139
|
#streams;
|
|
2186
|
-
#wakeRequests = /* @__PURE__ */ new Map();
|
|
2187
2140
|
constructor(options) {
|
|
2188
2141
|
this.#store = options.store;
|
|
2189
2142
|
this.#queue = options.queue;
|
|
@@ -2231,11 +2184,12 @@ var MailboxCoordinator = class {
|
|
|
2231
2184
|
});
|
|
2232
2185
|
}
|
|
2233
2186
|
}
|
|
2234
|
-
|
|
2235
|
-
|
|
2236
|
-
|
|
2237
|
-
|
|
2238
|
-
|
|
2187
|
+
/**
|
|
2188
|
+
* Coalesces only concurrent wake requests in this process. A settled request
|
|
2189
|
+
* is not kept, because another process may consume the wake.
|
|
2190
|
+
*/
|
|
2191
|
+
#scheduleWake = pMemoize(
|
|
2192
|
+
async (target) => {
|
|
2239
2193
|
const wakeId = AgentTurnId.random(target).toString();
|
|
2240
2194
|
await this.#streams.register(wakeId);
|
|
2241
2195
|
try {
|
|
@@ -2251,19 +2205,12 @@ var MailboxCoordinator = class {
|
|
|
2251
2205
|
});
|
|
2252
2206
|
throw error;
|
|
2253
2207
|
}
|
|
2254
|
-
}
|
|
2255
|
-
|
|
2256
|
-
|
|
2257
|
-
|
|
2258
|
-
|
|
2259
|
-
|
|
2260
|
-
};
|
|
2261
|
-
void request.then(clearRequest, clearRequest);
|
|
2262
|
-
return request;
|
|
2263
|
-
}
|
|
2264
|
-
#conversationKey(conversation) {
|
|
2265
|
-
return JSON.stringify([conversation.chatId, conversation.userId]);
|
|
2266
|
-
}
|
|
2208
|
+
},
|
|
2209
|
+
{
|
|
2210
|
+
cacheKey: ([target]) => JSON.stringify([target.chatId, target.userId]),
|
|
2211
|
+
cache: false
|
|
2212
|
+
}
|
|
2213
|
+
);
|
|
2267
2214
|
};
|
|
2268
2215
|
|
|
2269
2216
|
// packages/experimental/src/zukhruf/multi-agent-config.ts
|
|
@@ -2493,6 +2440,8 @@ function replacementRange(messages, keepLastMessages) {
|
|
|
2493
2440
|
case "tool-approval-response":
|
|
2494
2441
|
link(`approval:${part.approvalId}`, index, false);
|
|
2495
2442
|
break;
|
|
2443
|
+
default:
|
|
2444
|
+
break;
|
|
2496
2445
|
}
|
|
2497
2446
|
}
|
|
2498
2447
|
}
|
|
@@ -2648,7 +2597,7 @@ var inputUsageSchema = cacheObservationSchema.pick({ scope: true, prefixLength:
|
|
|
2648
2597
|
function hash(value) {
|
|
2649
2598
|
return createHash("sha256").update(JSON.stringify(value)).digest("hex");
|
|
2650
2599
|
}
|
|
2651
|
-
function
|
|
2600
|
+
function isRecord3(value) {
|
|
2652
2601
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
2653
2602
|
}
|
|
2654
2603
|
function cacheRetentionMs(step) {
|
|
@@ -2660,14 +2609,14 @@ function cacheRetentionMs(step) {
|
|
|
2660
2609
|
return void 0;
|
|
2661
2610
|
}
|
|
2662
2611
|
}
|
|
2663
|
-
if (!
|
|
2612
|
+
if (!isRecord3(body)) return void 0;
|
|
2664
2613
|
const provider = step.model.provider;
|
|
2665
2614
|
const routed = provider === "openrouter" || provider === "openrouter.chat";
|
|
2666
2615
|
const upstream = routed ? step.providerMetadata?.openrouter?.provider : void 0;
|
|
2667
2616
|
if (provider === "openai.responses" || provider === "openai.chat" || routed && upstream === "OpenAI") {
|
|
2668
2617
|
const options = body.prompt_cache_options;
|
|
2669
|
-
if (options !== void 0 && !
|
|
2670
|
-
if (
|
|
2618
|
+
if (options !== void 0 && !isRecord3(options)) return void 0;
|
|
2619
|
+
if (isRecord3(options) && options.ttl !== void 0) {
|
|
2671
2620
|
return options.ttl === "30m" ? 30 * 6e4 : void 0;
|
|
2672
2621
|
}
|
|
2673
2622
|
const version = /^(?:openai\/)?gpt-(\d+)(?:\.(\d+))?(?:-|$)/.exec(
|
|
@@ -2687,18 +2636,18 @@ function cacheRetentionMs(step) {
|
|
|
2687
2636
|
}
|
|
2688
2637
|
const windows = [];
|
|
2689
2638
|
for (const block of blocks) {
|
|
2690
|
-
if (!
|
|
2639
|
+
if (!isRecord3(block)) continue;
|
|
2691
2640
|
if (Array.isArray(block.content)) blocks.push(...block.content);
|
|
2692
2641
|
const control = block.cache_control;
|
|
2693
2642
|
if (control === void 0) continue;
|
|
2694
|
-
if (!
|
|
2643
|
+
if (!isRecord3(control) || control.type !== "ephemeral") return void 0;
|
|
2695
2644
|
if (control.ttl === void 0 || control.ttl === "5m")
|
|
2696
2645
|
windows.push(5 * 6e4);
|
|
2697
2646
|
else if (control.ttl === "1h") windows.push(60 * 6e4);
|
|
2698
2647
|
else return void 0;
|
|
2699
2648
|
}
|
|
2700
2649
|
const creation = step.usage.raw?.cache_creation;
|
|
2701
|
-
if (
|
|
2650
|
+
if (isRecord3(creation)) {
|
|
2702
2651
|
if (typeof creation.ephemeral_1h_input_tokens === "number" && creation.ephemeral_1h_input_tokens > 0)
|
|
2703
2652
|
return 60 * 6e4;
|
|
2704
2653
|
if (step.usage.inputTokenDetails.cacheReadTokens === 0 && creation.ephemeral_1h_input_tokens === 0 && typeof creation.ephemeral_5m_input_tokens === "number" && creation.ephemeral_5m_input_tokens > 0)
|
|
@@ -2774,10 +2723,10 @@ function createCompaction(config, engine, store, signal, onEvent) {
|
|
|
2774
2723
|
if (!initialized) {
|
|
2775
2724
|
const chat2 = await store.getChat(engine.chatId);
|
|
2776
2725
|
const metadata = chat2?.metadata?.zukhruf;
|
|
2777
|
-
const saved =
|
|
2778
|
-
const savedCache =
|
|
2726
|
+
const saved = isRecord3(metadata) ? metadata.compaction : void 0;
|
|
2727
|
+
const savedCache = isRecord3(metadata) ? metadata.promptCache : void 0;
|
|
2779
2728
|
baseline = inputUsageSchema.safeParse(
|
|
2780
|
-
|
|
2729
|
+
isRecord3(metadata) ? metadata.inputUsage : void 0
|
|
2781
2730
|
).data ?? null;
|
|
2782
2731
|
if (savedCache !== void 0) {
|
|
2783
2732
|
cache = cacheObservationSchema.nullable().parse(savedCache);
|
|
@@ -2884,7 +2833,7 @@ function createCompaction(config, engine, store, signal, onEvent) {
|
|
|
2884
2833
|
metadata: {
|
|
2885
2834
|
...metadata,
|
|
2886
2835
|
zukhruf: {
|
|
2887
|
-
...
|
|
2836
|
+
...isRecord3(metadata?.zukhruf) ? metadata.zukhruf : {},
|
|
2888
2837
|
compaction: checkpoint,
|
|
2889
2838
|
inputUsage: null
|
|
2890
2839
|
}
|
|
@@ -2919,7 +2868,7 @@ function createCompaction(config, engine, store, signal, onEvent) {
|
|
|
2919
2868
|
const onStepEnd = async (step) => {
|
|
2920
2869
|
if (signal.aborted || step.finishReason === "error") return;
|
|
2921
2870
|
const iterations = step.usage.raw?.iterations;
|
|
2922
|
-
const ambiguous = iterations != null && (!Array.isArray(iterations) || iterations.length !== 1 || !
|
|
2871
|
+
const ambiguous = iterations != null && (!Array.isArray(iterations) || iterations.length !== 1 || !isRecord3(iterations[0]) || iterations[0].type !== "message") || step.content.some(
|
|
2923
2872
|
(part) => part.type === "custom" && part.kind === "openai.compaction" || part.type === "text" && part.providerMetadata?.anthropic?.type === "compaction"
|
|
2924
2873
|
);
|
|
2925
2874
|
const nextBaseline = ambiguous ? null : inputUsageSchema.safeParse({
|
|
@@ -2934,7 +2883,7 @@ function createCompaction(config, engine, store, signal, onEvent) {
|
|
|
2934
2883
|
metadata: {
|
|
2935
2884
|
...metadata,
|
|
2936
2885
|
zukhruf: {
|
|
2937
|
-
...
|
|
2886
|
+
...isRecord3(metadata?.zukhruf) ? metadata.zukhruf : {},
|
|
2938
2887
|
promptCache: next,
|
|
2939
2888
|
inputUsage: nextBaseline
|
|
2940
2889
|
}
|
|
@@ -3385,7 +3334,7 @@ var AgentTurnExecutor = class {
|
|
|
3385
3334
|
if (turn.kind === "message" && turn.elements !== void 0) {
|
|
3386
3335
|
const declared = turn.elements;
|
|
3387
3336
|
await this.#store.updateChat(turn.chatId, ({ metadata }) => {
|
|
3388
|
-
const zukhruf =
|
|
3337
|
+
const zukhruf = isRecord4(metadata?.zukhruf) ? metadata.zukhruf : {};
|
|
3389
3338
|
if (isDeepStrictEqual(zukhruf.elements, declared)) return void 0;
|
|
3390
3339
|
return {
|
|
3391
3340
|
metadata: {
|
|
@@ -3415,7 +3364,7 @@ var AgentTurnExecutor = class {
|
|
|
3415
3364
|
available = persisted;
|
|
3416
3365
|
return void 0;
|
|
3417
3366
|
}
|
|
3418
|
-
const zukhruf =
|
|
3367
|
+
const zukhruf = isRecord4(metadata?.zukhruf) ? metadata.zukhruf : {};
|
|
3419
3368
|
return {
|
|
3420
3369
|
metadata: {
|
|
3421
3370
|
...metadata,
|
|
@@ -3485,7 +3434,7 @@ ${communication.content}`;
|
|
|
3485
3434
|
};
|
|
3486
3435
|
function readElements(metadata) {
|
|
3487
3436
|
const zukhruf = metadata?.zukhruf;
|
|
3488
|
-
if (!
|
|
3437
|
+
if (!isRecord4(zukhruf) || !Object.hasOwn(zukhruf, "elements")) {
|
|
3489
3438
|
return void 0;
|
|
3490
3439
|
}
|
|
3491
3440
|
const parsed = elementsSchema.safeParse(zukhruf.elements);
|
|
@@ -3496,7 +3445,7 @@ function readElements(metadata) {
|
|
|
3496
3445
|
}
|
|
3497
3446
|
function readSkills(metadata) {
|
|
3498
3447
|
const zukhruf = metadata?.zukhruf;
|
|
3499
|
-
if (!
|
|
3448
|
+
if (!isRecord4(zukhruf) || !Object.hasOwn(zukhruf, "skills")) {
|
|
3500
3449
|
return void 0;
|
|
3501
3450
|
}
|
|
3502
3451
|
if (!Array.isArray(zukhruf.skills) || !zukhruf.skills.every(isSkill)) {
|
|
@@ -3505,9 +3454,9 @@ function readSkills(metadata) {
|
|
|
3505
3454
|
return zukhruf.skills;
|
|
3506
3455
|
}
|
|
3507
3456
|
function isSkill(value) {
|
|
3508
|
-
return
|
|
3457
|
+
return isRecord4(value) && typeof value.name === "string" && typeof value.description === "string" && typeof value.path === "string";
|
|
3509
3458
|
}
|
|
3510
|
-
function
|
|
3459
|
+
function isRecord4(value) {
|
|
3511
3460
|
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
3512
3461
|
}
|
|
3513
3462
|
|
|
@@ -3666,6 +3615,7 @@ var ApprovalController = class _ApprovalController {
|
|
|
3666
3615
|
};
|
|
3667
3616
|
|
|
3668
3617
|
// packages/experimental/src/zukhruf/runtime/conversation-status/projector.ts
|
|
3618
|
+
import { Latch } from "@zukhruf/async";
|
|
3669
3619
|
import { MemoryStore, Mutex } from "@zukhruf/mutex";
|
|
3670
3620
|
import { EventEmitter, on } from "node:events";
|
|
3671
3621
|
import { isDeepStrictEqual as isDeepStrictEqual2 } from "node:util";
|
|
@@ -3699,6 +3649,19 @@ var ProjectorEventDelivery = class {
|
|
|
3699
3649
|
this.event = event;
|
|
3700
3650
|
}
|
|
3701
3651
|
};
|
|
3652
|
+
var Detached = class {
|
|
3653
|
+
// A private member keeps the type nominal: an empty class would also
|
|
3654
|
+
// describe RemoteAttachment, and `instanceof` could not narrow the union.
|
|
3655
|
+
#detached = true;
|
|
3656
|
+
};
|
|
3657
|
+
var DETACHED = new Detached();
|
|
3658
|
+
var RemoteAttachment = class {
|
|
3659
|
+
abort = new AbortController();
|
|
3660
|
+
/** Opens when the source subscribes: with nothing, or with the error that stopped it. */
|
|
3661
|
+
connected = new Latch();
|
|
3662
|
+
/** Opens when the consume loop has stopped. */
|
|
3663
|
+
ended = new Latch();
|
|
3664
|
+
};
|
|
3702
3665
|
var ConversationStatusProjector = class _ConversationStatusProjector {
|
|
3703
3666
|
#store;
|
|
3704
3667
|
#streams;
|
|
@@ -3709,7 +3672,7 @@ var ConversationStatusProjector = class _ConversationStatusProjector {
|
|
|
3709
3672
|
#last = /* @__PURE__ */ new Map();
|
|
3710
3673
|
#publishing = new Mutex(new MemoryStore());
|
|
3711
3674
|
#subscribers = 0;
|
|
3712
|
-
#remote;
|
|
3675
|
+
#remote = DETACHED;
|
|
3713
3676
|
constructor(options) {
|
|
3714
3677
|
this.#store = options.store;
|
|
3715
3678
|
this.#streams = options.streams;
|
|
@@ -3873,34 +3836,32 @@ var ConversationStatusProjector = class _ConversationStatusProjector {
|
|
|
3873
3836
|
this.#subscribers += 1;
|
|
3874
3837
|
const source = this.#changeSource;
|
|
3875
3838
|
if (!source) return;
|
|
3876
|
-
|
|
3877
|
-
|
|
3878
|
-
|
|
3879
|
-
const remote = { abort, ready: ready.promise, done: Promise.resolve() };
|
|
3839
|
+
let remote = this.#remote;
|
|
3840
|
+
if (!(remote instanceof RemoteAttachment)) {
|
|
3841
|
+
remote = new RemoteAttachment();
|
|
3880
3842
|
this.#remote = remote;
|
|
3881
|
-
|
|
3843
|
+
void this.#consumeRemote(source, remote);
|
|
3882
3844
|
}
|
|
3883
|
-
|
|
3884
|
-
|
|
3885
|
-
} catch (error) {
|
|
3845
|
+
const failure = await remote.connected.wait();
|
|
3846
|
+
if (failure) {
|
|
3886
3847
|
await this.#detachRemote();
|
|
3887
|
-
throw error;
|
|
3848
|
+
throw failure.error;
|
|
3888
3849
|
}
|
|
3889
3850
|
}
|
|
3890
3851
|
async #detachRemote() {
|
|
3891
3852
|
this.#subscribers -= 1;
|
|
3892
|
-
if (this.#subscribers > 0 || !this.#remote) return;
|
|
3893
3853
|
const remote = this.#remote;
|
|
3854
|
+
if (this.#subscribers > 0 || !(remote instanceof RemoteAttachment)) return;
|
|
3894
3855
|
remote.abort.abort();
|
|
3895
|
-
this.#remote =
|
|
3896
|
-
await remote.
|
|
3856
|
+
this.#remote = DETACHED;
|
|
3857
|
+
await remote.ended.wait();
|
|
3897
3858
|
}
|
|
3898
|
-
async #consumeRemote(source, remote
|
|
3859
|
+
async #consumeRemote(source, remote) {
|
|
3899
3860
|
let connected = false;
|
|
3900
3861
|
try {
|
|
3901
3862
|
const events = await source.subscribe(remote.abort.signal);
|
|
3902
3863
|
connected = true;
|
|
3903
|
-
|
|
3864
|
+
remote.connected.open(void 0);
|
|
3904
3865
|
for await (const event of events) {
|
|
3905
3866
|
if (event.type === "reset") {
|
|
3906
3867
|
this.#emit(event);
|
|
@@ -3915,12 +3876,13 @@ var ConversationStatusProjector = class _ConversationStatusProjector {
|
|
|
3915
3876
|
});
|
|
3916
3877
|
}
|
|
3917
3878
|
} catch (error) {
|
|
3918
|
-
if (!connected)
|
|
3879
|
+
if (!connected) remote.connected.open({ error });
|
|
3919
3880
|
if (connected && !(error instanceof Error && error.name === "AbortError")) {
|
|
3920
3881
|
this.#emit({ type: "error", error });
|
|
3921
3882
|
}
|
|
3922
3883
|
} finally {
|
|
3923
|
-
if (this.#remote === remote) this.#remote =
|
|
3884
|
+
if (this.#remote === remote) this.#remote = DETACHED;
|
|
3885
|
+
remote.ended.open();
|
|
3924
3886
|
}
|
|
3925
3887
|
}
|
|
3926
3888
|
#emit(event) {
|