@relayfx/sdk 0.0.24 → 0.0.26
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.js
CHANGED
|
@@ -13,6 +13,7 @@ var __export = (target, all) => {
|
|
|
13
13
|
set: __exportSetter.bind(all, name)
|
|
14
14
|
});
|
|
15
15
|
};
|
|
16
|
+
var __require = import.meta.require;
|
|
16
17
|
|
|
17
18
|
// src/adapter-outbox.ts
|
|
18
19
|
var exports_adapter_outbox = {};
|
|
@@ -893,7 +894,6 @@ __export(exports_database_service, {
|
|
|
893
894
|
});
|
|
894
895
|
import * as MysqlClient from "@effect/sql-mysql2/MysqlClient";
|
|
895
896
|
import * as PgClient from "@effect/sql-pg/PgClient";
|
|
896
|
-
import * as SqliteClient from "@effect/sql-sqlite-bun/SqliteClient";
|
|
897
897
|
import * as PgDrizzle from "drizzle-orm/effect-postgres";
|
|
898
898
|
import { sql } from "drizzle-orm";
|
|
899
899
|
import { Config, Context, DateTime, Effect, Layer, Redacted, Schema as Schema12 } from "effect";
|
|
@@ -941,9 +941,9 @@ var MysqlClientLive = MysqlClient.layerConfig({
|
|
|
941
941
|
url: Config.redacted("DATABASE_URL").pipe(Config.withDefault(Redacted.make("mysql://relay:relay@localhost:5546/relay"))),
|
|
942
942
|
maxConnections: Config.int("RELAY_SQL_MAX_CONNECTIONS").pipe(Config.withDefault(8))
|
|
943
943
|
});
|
|
944
|
-
var SqliteClientLive = SqliteClient.layerConfig({
|
|
944
|
+
var SqliteClientLive = Layer.unwrap(Effect.promise(() => import("@effect/sql-sqlite-bun/SqliteClient")).pipe(Effect.map((SqliteClient) => SqliteClient.layerConfig({
|
|
945
945
|
filename: Config.string("RELAY_SQLITE_FILE").pipe(Config.withDefault("relay.sqlite"))
|
|
946
|
-
});
|
|
946
|
+
}))));
|
|
947
947
|
var timestampParam = (client, millis) => client.onDialectOrElse({
|
|
948
948
|
sqlite: () => millis,
|
|
949
949
|
orElse: () => toPgDate(millis)
|
|
@@ -1539,10 +1539,11 @@ __export(exports_agent_chat_repository, {
|
|
|
1539
1539
|
layer: () => layer4,
|
|
1540
1540
|
get: () => get,
|
|
1541
1541
|
Service: () => Service4,
|
|
1542
|
+
AgentChatRow: () => AgentChatRow,
|
|
1542
1543
|
AgentChatRepositoryError: () => AgentChatRepositoryError
|
|
1543
1544
|
});
|
|
1544
|
-
import { eq as eq3 } from "drizzle-orm";
|
|
1545
1545
|
import { Context as Context4, Effect as Effect4, Layer as Layer4, Schema as Schema14 } from "effect";
|
|
1546
|
+
import * as SqlClient3 from "effect/unstable/sql/SqlClient";
|
|
1546
1547
|
class AgentChatRepositoryError extends Schema14.TaggedErrorClass()("AgentChatRepositoryError", {
|
|
1547
1548
|
message: Schema14.String
|
|
1548
1549
|
}) {
|
|
@@ -1550,35 +1551,58 @@ class AgentChatRepositoryError extends Schema14.TaggedErrorClass()("AgentChatRep
|
|
|
1550
1551
|
|
|
1551
1552
|
class Service4 extends Context4.Service()("@relayfx/store-sql/AgentChatRepository") {
|
|
1552
1553
|
}
|
|
1554
|
+
var AgentChatRow = Schema14.Struct({
|
|
1555
|
+
execution_id: Schema14.String,
|
|
1556
|
+
export_json: Schema14.Unknown,
|
|
1557
|
+
updated_at: Schema14.Union([Schema14.Date, Schema14.String, Schema14.Number])
|
|
1558
|
+
}).annotate({ identifier: "Relay.AgentChat.Row" });
|
|
1553
1559
|
var toRecord = (row) => ({
|
|
1554
|
-
executionId: row.
|
|
1555
|
-
exportData: row.
|
|
1556
|
-
updatedAt:
|
|
1560
|
+
executionId: exports_ids_schema.ExecutionId.make(row.execution_id),
|
|
1561
|
+
exportData: decodeJson(row.export_json),
|
|
1562
|
+
updatedAt: fromDbTimestamp(row.updated_at)
|
|
1557
1563
|
});
|
|
1558
|
-
var
|
|
1564
|
+
var toRepositoryError = (error) => new AgentChatRepositoryError({ message: String(error) });
|
|
1565
|
+
var decodeRow = (row) => Schema14.decodeUnknownEffect(AgentChatRow)(row).pipe(Effect4.mapError(toRepositoryError));
|
|
1559
1566
|
var layer4 = Layer4.effect(Service4, Effect4.gen(function* () {
|
|
1560
|
-
const
|
|
1567
|
+
const sql4 = yield* SqlClient3.SqlClient;
|
|
1568
|
+
const selectByKey = (tenantId2, executionId) => sql4`
|
|
1569
|
+
SELECT execution_id, export_json, updated_at
|
|
1570
|
+
FROM relay_agent_chats
|
|
1571
|
+
WHERE tenant_id = ${tenantId2} AND execution_id = ${executionId}
|
|
1572
|
+
`;
|
|
1573
|
+
const upsert2 = sql4.onDialectOrElse({
|
|
1574
|
+
mysql: () => (tenantId2, input) => sql4.withTransaction(Effect4.gen(function* () {
|
|
1575
|
+
yield* sql4`
|
|
1576
|
+
INSERT INTO relay_agent_chats (tenant_id, execution_id, export_json, updated_at)
|
|
1577
|
+
VALUES (${tenantId2}, ${input.executionId}, ${encodeJson(input.exportData)}, ${timestampParam(sql4, input.updatedAt)})
|
|
1578
|
+
ON DUPLICATE KEY UPDATE
|
|
1579
|
+
export_json = VALUES(export_json),
|
|
1580
|
+
updated_at = VALUES(updated_at)
|
|
1581
|
+
`;
|
|
1582
|
+
return yield* selectByKey(tenantId2, input.executionId);
|
|
1583
|
+
})),
|
|
1584
|
+
orElse: () => (tenantId2, input) => sql4`
|
|
1585
|
+
INSERT INTO relay_agent_chats (tenant_id, execution_id, export_json, updated_at)
|
|
1586
|
+
VALUES (${tenantId2}, ${input.executionId}, ${encodeJson(input.exportData)}, ${timestampParam(sql4, input.updatedAt)})
|
|
1587
|
+
ON CONFLICT (tenant_id, execution_id) DO UPDATE SET
|
|
1588
|
+
export_json = excluded.export_json,
|
|
1589
|
+
updated_at = excluded.updated_at
|
|
1590
|
+
RETURNING execution_id, export_json, updated_at
|
|
1591
|
+
`
|
|
1592
|
+
});
|
|
1561
1593
|
const save = Effect4.fn("AgentChatRepository.save")(function* (input) {
|
|
1562
1594
|
const tenantId2 = yield* current;
|
|
1563
|
-
const rows = yield*
|
|
1564
|
-
tenantId: tenantId2,
|
|
1565
|
-
executionId: input.executionId,
|
|
1566
|
-
exportJson: input.exportData,
|
|
1567
|
-
updatedAt: toPgDate(input.updatedAt)
|
|
1568
|
-
}).onConflictDoUpdate({
|
|
1569
|
-
target: [relayAgentChats.tenantId, relayAgentChats.executionId],
|
|
1570
|
-
set: { exportJson: input.exportData, updatedAt: toPgDate(input.updatedAt) }
|
|
1571
|
-
}).returning());
|
|
1595
|
+
const rows = yield* upsert2(tenantId2, input).pipe(Effect4.mapError(toRepositoryError));
|
|
1572
1596
|
const row = rows[0];
|
|
1573
1597
|
if (row === undefined) {
|
|
1574
1598
|
return yield* Effect4.fail(new AgentChatRepositoryError({ message: "Agent chat upsert returned no row" }));
|
|
1575
1599
|
}
|
|
1576
|
-
return toRecord(row);
|
|
1600
|
+
return toRecord(yield* decodeRow(row));
|
|
1577
1601
|
});
|
|
1578
1602
|
const get = Effect4.fn("AgentChatRepository.get")(function* (executionId) {
|
|
1579
1603
|
const tenantId2 = yield* current;
|
|
1580
|
-
const rows = yield*
|
|
1581
|
-
return rows[0] === undefined ? undefined : toRecord(rows[0]);
|
|
1604
|
+
const rows = yield* selectByKey(tenantId2, executionId).pipe(Effect4.mapError(toRepositoryError));
|
|
1605
|
+
return rows[0] === undefined ? undefined : toRecord(yield* decodeRow(rows[0]));
|
|
1582
1606
|
});
|
|
1583
1607
|
return Service4.of({ save, get });
|
|
1584
1608
|
}));
|
|
@@ -1622,7 +1646,7 @@ __export(exports_agent_definition_repository, {
|
|
|
1622
1646
|
Service: () => Service5,
|
|
1623
1647
|
AgentDefinitionRepositoryError: () => AgentDefinitionRepositoryError
|
|
1624
1648
|
});
|
|
1625
|
-
import { asc as asc2, desc, eq as
|
|
1649
|
+
import { asc as asc2, desc, eq as eq3, sql as sql4 } from "drizzle-orm";
|
|
1626
1650
|
import { Context as Context5, Effect as Effect5, Layer as Layer5, Schema as Schema15 } from "effect";
|
|
1627
1651
|
class AgentDefinitionRepositoryError extends Schema15.TaggedErrorClass()("AgentDefinitionRepositoryError", {
|
|
1628
1652
|
message: Schema15.String
|
|
@@ -1669,14 +1693,14 @@ var compareByCreatedAtDesc = (left, right) => {
|
|
|
1669
1693
|
const createdAt = right.createdAt - left.createdAt;
|
|
1670
1694
|
return createdAt === 0 ? left.id.localeCompare(right.id) : createdAt;
|
|
1671
1695
|
};
|
|
1672
|
-
var
|
|
1696
|
+
var mapDatabaseError2 = (effect) => effect.pipe(Effect5.mapError((error) => new AgentDefinitionRepositoryError({ message: String(error) })));
|
|
1673
1697
|
var mapPutError = (effect) => effect.pipe(Effect5.mapError((error) => error instanceof AgentDefinitionRepositoryError ? error : new AgentDefinitionRepositoryError({ message: String(error) })));
|
|
1674
1698
|
var layer5 = Layer5.effect(Service5, Effect5.gen(function* () {
|
|
1675
1699
|
const db = yield* Service;
|
|
1676
1700
|
const put = Effect5.fn("AgentDefinitionRepository.put")(function* (input) {
|
|
1677
1701
|
const tenantId2 = yield* current;
|
|
1678
1702
|
return yield* mapPutError(db.transaction((tx) => Effect5.gen(function* () {
|
|
1679
|
-
const existingRows = yield* tx.select().from(relayAgentDefinitions).where(predicate(tenantId2, relayAgentDefinitions.tenantId,
|
|
1703
|
+
const existingRows = yield* tx.select().from(relayAgentDefinitions).where(predicate(tenantId2, relayAgentDefinitions.tenantId, eq3(relayAgentDefinitions.id, input.id))).for("update").limit(1);
|
|
1680
1704
|
const existing = existingRows[0];
|
|
1681
1705
|
if (existing !== undefined && sameDefinitionContent(existing.definitionJson, input.definition)) {
|
|
1682
1706
|
return toRecord2(existing);
|
|
@@ -1696,7 +1720,7 @@ var layer5 = Layer5.effect(Service5, Effect5.gen(function* () {
|
|
|
1696
1720
|
definitionJson: input.definition,
|
|
1697
1721
|
toolInputSchemaDigestsJson: input.toolInputSchemaDigests ?? {},
|
|
1698
1722
|
updatedAt: toPgDate(input.now)
|
|
1699
|
-
}).where(predicate(tenantId2, relayAgentDefinitions.tenantId,
|
|
1723
|
+
}).where(predicate(tenantId2, relayAgentDefinitions.tenantId, eq3(relayAgentDefinitions.id, input.id))).returning();
|
|
1700
1724
|
const row = rows[0];
|
|
1701
1725
|
if (row === undefined) {
|
|
1702
1726
|
return yield* Effect5.fail(new AgentDefinitionRepositoryError({ message: "Agent definition put returned no row" }));
|
|
@@ -1718,22 +1742,22 @@ var layer5 = Layer5.effect(Service5, Effect5.gen(function* () {
|
|
|
1718
1742
|
});
|
|
1719
1743
|
const get2 = Effect5.fn("AgentDefinitionRepository.get")(function* (id2) {
|
|
1720
1744
|
const tenantId2 = yield* current;
|
|
1721
|
-
const rows = yield*
|
|
1745
|
+
const rows = yield* mapDatabaseError2(db.select().from(relayAgentDefinitions).where(predicate(tenantId2, relayAgentDefinitions.tenantId, eq3(relayAgentDefinitions.id, id2))).limit(1));
|
|
1722
1746
|
return rows[0] === undefined ? undefined : toRecord2(rows[0]);
|
|
1723
1747
|
});
|
|
1724
1748
|
const getRevision = Effect5.fn("AgentDefinitionRepository.getRevision")(function* (input) {
|
|
1725
1749
|
const tenantId2 = yield* current;
|
|
1726
|
-
const rows = yield*
|
|
1750
|
+
const rows = yield* mapDatabaseError2(db.select().from(relayAgentDefinitionRevisions).where(predicate(tenantId2, relayAgentDefinitionRevisions.tenantId, eq3(relayAgentDefinitionRevisions.agentDefinitionId, input.id), eq3(relayAgentDefinitionRevisions.revision, input.revision))).limit(1));
|
|
1727
1751
|
return rows[0] === undefined ? undefined : toRevisionRecord(rows[0]);
|
|
1728
1752
|
});
|
|
1729
1753
|
const list2 = Effect5.fn("AgentDefinitionRepository.list")(function* () {
|
|
1730
1754
|
const tenantId2 = yield* current;
|
|
1731
|
-
const rows = yield*
|
|
1755
|
+
const rows = yield* mapDatabaseError2(db.select().from(relayAgentDefinitions).where(predicate(tenantId2, relayAgentDefinitions.tenantId)).orderBy(desc(relayAgentDefinitions.createdAt), asc2(relayAgentDefinitions.id)));
|
|
1732
1756
|
return rows.map(toRecord2);
|
|
1733
1757
|
});
|
|
1734
1758
|
const listRevisions = Effect5.fn("AgentDefinitionRepository.listRevisions")(function* (id2) {
|
|
1735
1759
|
const tenantId2 = yield* current;
|
|
1736
|
-
const rows = yield*
|
|
1760
|
+
const rows = yield* mapDatabaseError2(db.select().from(relayAgentDefinitionRevisions).where(predicate(tenantId2, relayAgentDefinitionRevisions.tenantId, eq3(relayAgentDefinitionRevisions.agentDefinitionId, id2))).orderBy(desc(relayAgentDefinitionRevisions.revision)));
|
|
1737
1761
|
return rows.map(toRevisionRecord);
|
|
1738
1762
|
});
|
|
1739
1763
|
return Service5.of({ put, get: get2, getRevision, list: list2, listRevisions });
|
|
@@ -1824,7 +1848,7 @@ __export(exports_child_execution_repository, {
|
|
|
1824
1848
|
ChildExecutionRepositoryError: () => ChildExecutionRepositoryError,
|
|
1825
1849
|
ChildExecutionNotFound: () => ChildExecutionNotFound
|
|
1826
1850
|
});
|
|
1827
|
-
import { asc as asc3, eq as
|
|
1851
|
+
import { asc as asc3, eq as eq4 } from "drizzle-orm";
|
|
1828
1852
|
import { Context as Context6, Effect as Effect6, Layer as Layer6, Schema as Schema16 } from "effect";
|
|
1829
1853
|
class ChildExecutionRepositoryError extends Schema16.TaggedErrorClass()("ChildExecutionRepositoryError", {
|
|
1830
1854
|
message: Schema16.String
|
|
@@ -1863,15 +1887,15 @@ var toRecord3 = (row) => ({
|
|
|
1863
1887
|
createdAt: fromPgDate(row.createdAt),
|
|
1864
1888
|
updatedAt: fromPgDate(row.updatedAt)
|
|
1865
1889
|
});
|
|
1866
|
-
var
|
|
1890
|
+
var mapDatabaseError3 = (effect) => effect.pipe(Effect6.mapError((error) => new ChildExecutionRepositoryError({ message: String(error) })));
|
|
1867
1891
|
var layer6 = Layer6.effect(Service6, Effect6.gen(function* () {
|
|
1868
1892
|
const db = yield* Service;
|
|
1869
1893
|
const create = Effect6.fn("ChildExecutionRepository.create")(function* (input) {
|
|
1870
1894
|
const tenantId2 = yield* current;
|
|
1871
|
-
const existing = yield*
|
|
1895
|
+
const existing = yield* mapDatabaseError3(db.select().from(relayChildExecutions).where(predicate(tenantId2, relayChildExecutions.tenantId, eq4(relayChildExecutions.id, input.id))).limit(1));
|
|
1872
1896
|
if (existing[0] !== undefined)
|
|
1873
1897
|
return yield* Effect6.fail(new DuplicateChildExecution({ id: input.id }));
|
|
1874
|
-
const rows = yield*
|
|
1898
|
+
const rows = yield* mapDatabaseError3(db.insert(relayChildExecutions).values(toInsert(tenantId2, input)).returning());
|
|
1875
1899
|
const row = rows[0];
|
|
1876
1900
|
if (row === undefined) {
|
|
1877
1901
|
return yield* Effect6.fail(new ChildExecutionRepositoryError({ message: "Child execution insert returned no row" }));
|
|
@@ -1880,21 +1904,21 @@ var layer6 = Layer6.effect(Service6, Effect6.gen(function* () {
|
|
|
1880
1904
|
});
|
|
1881
1905
|
const get3 = Effect6.fn("ChildExecutionRepository.get")(function* (id2) {
|
|
1882
1906
|
const tenantId2 = yield* current;
|
|
1883
|
-
const rows = yield*
|
|
1907
|
+
const rows = yield* mapDatabaseError3(db.select().from(relayChildExecutions).where(predicate(tenantId2, relayChildExecutions.tenantId, eq4(relayChildExecutions.id, id2))).limit(1));
|
|
1884
1908
|
return rows[0] === undefined ? undefined : toRecord3(rows[0]);
|
|
1885
1909
|
});
|
|
1886
1910
|
const listByExecution = Effect6.fn("ChildExecutionRepository.listByExecution")(function* (executionId) {
|
|
1887
1911
|
const tenantId2 = yield* current;
|
|
1888
|
-
const rows = yield*
|
|
1912
|
+
const rows = yield* mapDatabaseError3(db.select().from(relayChildExecutions).where(predicate(tenantId2, relayChildExecutions.tenantId, eq4(relayChildExecutions.executionId, executionId))).orderBy(asc3(relayChildExecutions.createdAt)));
|
|
1889
1913
|
return rows.map(toRecord3);
|
|
1890
1914
|
});
|
|
1891
1915
|
const updateStatus = Effect6.fn("ChildExecutionRepository.updateStatus")(function* (input) {
|
|
1892
1916
|
const tenantId2 = yield* current;
|
|
1893
|
-
const rows = yield*
|
|
1917
|
+
const rows = yield* mapDatabaseError3(db.update(relayChildExecutions).set({
|
|
1894
1918
|
status: input.status,
|
|
1895
1919
|
updatedAt: toPgDate(input.updatedAt),
|
|
1896
1920
|
...input.metadata === undefined ? {} : { metadataJson: input.metadata }
|
|
1897
|
-
}).where(predicate(tenantId2, relayChildExecutions.tenantId,
|
|
1921
|
+
}).where(predicate(tenantId2, relayChildExecutions.tenantId, eq4(relayChildExecutions.id, input.id))).returning());
|
|
1898
1922
|
const row = rows[0];
|
|
1899
1923
|
if (row === undefined)
|
|
1900
1924
|
return yield* Effect6.fail(new ChildExecutionNotFound({ id: input.id }));
|
|
@@ -1972,7 +1996,7 @@ __export(exports_cluster_registry_repository, {
|
|
|
1972
1996
|
ClusterRegistryError: () => ClusterRegistryError
|
|
1973
1997
|
});
|
|
1974
1998
|
import { Context as Context7, Effect as Effect7, Layer as Layer7, Schema as Schema17 } from "effect";
|
|
1975
|
-
import { SqlClient as
|
|
1999
|
+
import { SqlClient as SqlClient5, SqlSchema } from "effect/unstable/sql";
|
|
1976
2000
|
|
|
1977
2001
|
class ClusterRegistryError extends Schema17.TaggedErrorClass()("ClusterRegistryError", {
|
|
1978
2002
|
message: Schema17.String
|
|
@@ -1998,7 +2022,7 @@ var toRunnerRecord = (row) => ({
|
|
|
1998
2022
|
});
|
|
1999
2023
|
var toRegistryError = (error) => new ClusterRegistryError({ message: String(error) });
|
|
2000
2024
|
var layer7 = Layer7.effect(Service7, Effect7.gen(function* () {
|
|
2001
|
-
const sql5 = yield*
|
|
2025
|
+
const sql5 = yield* SqlClient5.SqlClient;
|
|
2002
2026
|
const runnersQuery = SqlSchema.findAll({
|
|
2003
2027
|
Request: Schema17.Void,
|
|
2004
2028
|
Result: RunnerRow,
|
|
@@ -2051,7 +2075,7 @@ __export(exports_compaction_repository, {
|
|
|
2051
2075
|
Service: () => Service8,
|
|
2052
2076
|
CompactionRepositoryError: () => CompactionRepositoryError
|
|
2053
2077
|
});
|
|
2054
|
-
import { asc as asc4, eq as
|
|
2078
|
+
import { asc as asc4, eq as eq5 } from "drizzle-orm";
|
|
2055
2079
|
import { Context as Context8, Effect as Effect8, Layer as Layer8, Schema as Schema18 } from "effect";
|
|
2056
2080
|
class CompactionRepositoryError extends Schema18.TaggedErrorClass()("CompactionRepositoryError", {
|
|
2057
2081
|
message: Schema18.String
|
|
@@ -2068,7 +2092,7 @@ var toRecord4 = (row) => ({
|
|
|
2068
2092
|
turn: row.turn,
|
|
2069
2093
|
createdAt: fromPgDate(row.createdAt)
|
|
2070
2094
|
});
|
|
2071
|
-
var
|
|
2095
|
+
var mapDatabaseError4 = (effect) => effect.pipe(Effect8.mapError((error) => new CompactionRepositoryError({ message: String(error) })));
|
|
2072
2096
|
var key = (input) => `${input.executionId}:${input.checkpointId}`;
|
|
2073
2097
|
var sameCheckpoint = (record, input) => record.summary === input.summary && record.firstKeptEntryId === input.firstKeptEntryId && record.turn === input.turn;
|
|
2074
2098
|
var conflict = (input) => new CompactionRepositoryError({
|
|
@@ -2079,7 +2103,7 @@ var layer8 = Layer8.effect(Service8, Effect8.gen(function* () {
|
|
|
2079
2103
|
const db = yield* Service;
|
|
2080
2104
|
const get4 = Effect8.fn("CompactionRepository.get")(function* (input) {
|
|
2081
2105
|
const tenantId2 = yield* current;
|
|
2082
|
-
const rows = yield*
|
|
2106
|
+
const rows = yield* mapDatabaseError4(db.select().from(relayAgentCompactions).where(predicate(tenantId2, relayAgentCompactions.tenantId, eq5(relayAgentCompactions.executionId, input.executionId), eq5(relayAgentCompactions.checkpointId, input.checkpointId))).limit(1));
|
|
2083
2107
|
return rows[0] === undefined ? undefined : toRecord4(rows[0]);
|
|
2084
2108
|
});
|
|
2085
2109
|
const record = Effect8.fn("CompactionRepository.record")(function* (input) {
|
|
@@ -2090,7 +2114,7 @@ var layer8 = Layer8.effect(Service8, Effect8.gen(function* () {
|
|
|
2090
2114
|
return yield* Effect8.fail(conflict(input));
|
|
2091
2115
|
return existing;
|
|
2092
2116
|
}
|
|
2093
|
-
const rows = yield*
|
|
2117
|
+
const rows = yield* mapDatabaseError4(db.insert(relayAgentCompactions).values({
|
|
2094
2118
|
tenantId: tenantId2,
|
|
2095
2119
|
executionId: input.executionId,
|
|
2096
2120
|
checkpointId: input.checkpointId,
|
|
@@ -2115,7 +2139,7 @@ var layer8 = Layer8.effect(Service8, Effect8.gen(function* () {
|
|
|
2115
2139
|
});
|
|
2116
2140
|
const list3 = Effect8.fn("CompactionRepository.list")(function* (executionId) {
|
|
2117
2141
|
const tenantId2 = yield* current;
|
|
2118
|
-
const rows = yield*
|
|
2142
|
+
const rows = yield* mapDatabaseError4(db.select().from(relayAgentCompactions).where(predicate(tenantId2, relayAgentCompactions.tenantId, eq5(relayAgentCompactions.executionId, executionId))).orderBy(asc4(relayAgentCompactions.turn), asc4(relayAgentCompactions.checkpointId)));
|
|
2119
2143
|
return rows.map(toRecord4);
|
|
2120
2144
|
});
|
|
2121
2145
|
return Service8.of({ get: get4, record, list: list3 });
|
|
@@ -2180,7 +2204,7 @@ __export(exports_envelope_repository, {
|
|
|
2180
2204
|
EnvelopeReadyClaimMismatch: () => EnvelopeReadyClaimMismatch,
|
|
2181
2205
|
EnvelopeNotFound: () => EnvelopeNotFound
|
|
2182
2206
|
});
|
|
2183
|
-
import { and as and2, asc as asc5, desc as desc2, eq as
|
|
2207
|
+
import { and as and2, asc as asc5, desc as desc2, eq as eq6, inArray, lte, or, sql as sql5 } from "drizzle-orm";
|
|
2184
2208
|
import { Context as Context9, Effect as Effect9, Layer as Layer9, Schema as Schema19 } from "effect";
|
|
2185
2209
|
var WaitState = Schema19.Literals(["open", "resolved", "timed_out", "cancelled"]).annotate({
|
|
2186
2210
|
identifier: "Relay.Store.WaitState"
|
|
@@ -2311,17 +2335,17 @@ var maxListWaitsLimit = 500;
|
|
|
2311
2335
|
var defaultListWaitsLimit = 50;
|
|
2312
2336
|
var listWaitsLimit = (input) => Math.min(Math.max(input.limit ?? defaultListWaitsLimit, 1), maxListWaitsLimit);
|
|
2313
2337
|
var listWaitsConditions = (input) => [
|
|
2314
|
-
input.executionId === undefined ? undefined :
|
|
2315
|
-
input.state === undefined ? undefined :
|
|
2338
|
+
input.executionId === undefined ? undefined : eq6(relayWaits.executionId, input.executionId),
|
|
2339
|
+
input.state === undefined ? undefined : eq6(relayWaits.state, input.state)
|
|
2316
2340
|
].filter((condition) => condition !== undefined);
|
|
2317
2341
|
var compareWaitsDesc = (left, right) => right.createdAt - left.createdAt || (left.id < right.id ? 1 : left.id > right.id ? -1 : 0);
|
|
2318
|
-
var
|
|
2342
|
+
var mapDatabaseError5 = (effect) => effect.pipe(Effect9.mapError((error) => new EnvelopeRepositoryError({ message: String(error) })));
|
|
2319
2343
|
var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
2320
2344
|
const db = yield* Service;
|
|
2321
2345
|
const acceptEnvelope = Effect9.fn("EnvelopeRepository.acceptEnvelope")(function* (input) {
|
|
2322
2346
|
const tenantId2 = yield* current;
|
|
2323
2347
|
const wait = toWaitInsert(tenantId2, input);
|
|
2324
|
-
yield*
|
|
2348
|
+
yield* mapDatabaseError5(db.transaction((tx) => Effect9.gen(function* () {
|
|
2325
2349
|
yield* tx.insert(relayEnvelopes).values(toEnvelopeRow(tenantId2, input.envelope));
|
|
2326
2350
|
if (wait !== undefined) {
|
|
2327
2351
|
yield* tx.insert(relayWaits).values(wait);
|
|
@@ -2334,17 +2358,17 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2334
2358
|
});
|
|
2335
2359
|
const getEnvelope = Effect9.fn("EnvelopeRepository.getEnvelope")(function* (id2) {
|
|
2336
2360
|
const tenantId2 = yield* current;
|
|
2337
|
-
const rows = yield*
|
|
2361
|
+
const rows = yield* mapDatabaseError5(db.select().from(relayEnvelopes).where(predicate(tenantId2, relayEnvelopes.tenantId, eq6(relayEnvelopes.id, id2))).limit(1));
|
|
2338
2362
|
return rows[0] === undefined ? undefined : toEnvelope(rows[0]);
|
|
2339
2363
|
});
|
|
2340
2364
|
const getWait = Effect9.fn("EnvelopeRepository.getWait")(function* (id2) {
|
|
2341
2365
|
const tenantId2 = yield* current;
|
|
2342
|
-
const rows = yield*
|
|
2366
|
+
const rows = yield* mapDatabaseError5(db.select().from(relayWaits).where(predicate(tenantId2, relayWaits.tenantId, eq6(relayWaits.id, id2))).limit(1));
|
|
2343
2367
|
return rows[0] === undefined ? undefined : toWaitRecord(rows[0]);
|
|
2344
2368
|
});
|
|
2345
2369
|
const createWait = Effect9.fn("EnvelopeRepository.createWait")(function* (input) {
|
|
2346
2370
|
const tenantId2 = yield* current;
|
|
2347
|
-
const rows = yield*
|
|
2371
|
+
const rows = yield* mapDatabaseError5(db.insert(relayWaits).values({
|
|
2348
2372
|
tenantId: tenantId2,
|
|
2349
2373
|
id: input.waitId,
|
|
2350
2374
|
executionId: input.executionId,
|
|
@@ -2361,13 +2385,13 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2361
2385
|
});
|
|
2362
2386
|
const listWaits = Effect9.fn("EnvelopeRepository.listWaits")(function* (input) {
|
|
2363
2387
|
const tenantId2 = yield* current;
|
|
2364
|
-
const rows = yield*
|
|
2388
|
+
const rows = yield* mapDatabaseError5(db.select().from(relayWaits).where(predicate(tenantId2, relayWaits.tenantId, ...listWaitsConditions(input))).orderBy(desc2(relayWaits.createdAt), desc2(relayWaits.id)).limit(listWaitsLimit(input)));
|
|
2365
2389
|
return rows.map(toWaitRecord);
|
|
2366
2390
|
});
|
|
2367
2391
|
const claimReady = Effect9.fn("EnvelopeRepository.claimReady")(function* (input) {
|
|
2368
2392
|
const tenantId2 = yield* current;
|
|
2369
|
-
const candidate = db.select({ id: relayEnvelopeReady.id }).from(relayEnvelopeReady).where(predicate(tenantId2, relayEnvelopeReady.tenantId,
|
|
2370
|
-
const rows = yield*
|
|
2393
|
+
const candidate = db.select({ id: relayEnvelopeReady.id }).from(relayEnvelopeReady).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq6(relayEnvelopeReady.routeType, input.routeType), input.routeKey === undefined ? undefined : eq6(relayEnvelopeReady.routeKey, input.routeKey), or(and2(eq6(relayEnvelopeReady.state, "ready"), lte(relayEnvelopeReady.availableAt, toPgDate(input.now))), and2(eq6(relayEnvelopeReady.state, "claimed"), lte(relayEnvelopeReady.claimExpiresAt, toPgDate(input.now)))))).orderBy(asc5(relayEnvelopeReady.availableAt), asc5(relayEnvelopeReady.createdAt)).limit(1).for("update", { skipLocked: true });
|
|
2394
|
+
const rows = yield* mapDatabaseError5(db.update(relayEnvelopeReady).set({
|
|
2371
2395
|
state: "claimed",
|
|
2372
2396
|
claimOwner: input.workerId,
|
|
2373
2397
|
claimExpiresAt: toPgDate(input.claimExpiresAt),
|
|
@@ -2379,14 +2403,14 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2379
2403
|
});
|
|
2380
2404
|
const releaseReady = Effect9.fn("EnvelopeRepository.releaseReady")(function* (input) {
|
|
2381
2405
|
const tenantId2 = yield* current;
|
|
2382
|
-
const rows = yield*
|
|
2406
|
+
const rows = yield* mapDatabaseError5(db.update(relayEnvelopeReady).set({
|
|
2383
2407
|
state: "ready",
|
|
2384
2408
|
availableAt: toPgDate(input.nextAvailableAt),
|
|
2385
2409
|
claimOwner: null,
|
|
2386
2410
|
claimExpiresAt: null,
|
|
2387
2411
|
lastError: input.error ?? null,
|
|
2388
2412
|
updatedAt: toPgDate(input.nextAvailableAt)
|
|
2389
|
-
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId,
|
|
2413
|
+
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq6(relayEnvelopeReady.id, input.id), eq6(relayEnvelopeReady.claimOwner, input.workerId), eq6(relayEnvelopeReady.state, "claimed"))).returning());
|
|
2390
2414
|
const row = rows[0];
|
|
2391
2415
|
if (row !== undefined)
|
|
2392
2416
|
return toReadyRecord(row);
|
|
@@ -2399,11 +2423,11 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2399
2423
|
});
|
|
2400
2424
|
const ackReady = Effect9.fn("EnvelopeRepository.ackReady")(function* (input) {
|
|
2401
2425
|
const tenantId2 = yield* current;
|
|
2402
|
-
const rows = yield*
|
|
2426
|
+
const rows = yield* mapDatabaseError5(db.update(relayEnvelopeReady).set({
|
|
2403
2427
|
state: "acknowledged",
|
|
2404
2428
|
acknowledgedAt: toPgDate(input.now),
|
|
2405
2429
|
updatedAt: toPgDate(input.now)
|
|
2406
|
-
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId,
|
|
2430
|
+
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq6(relayEnvelopeReady.id, input.id), eq6(relayEnvelopeReady.claimOwner, input.workerId), eq6(relayEnvelopeReady.state, "claimed"))).returning());
|
|
2407
2431
|
const row = rows[0];
|
|
2408
2432
|
if (row !== undefined)
|
|
2409
2433
|
return toReadyRecord(row);
|
|
@@ -2421,11 +2445,11 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2421
2445
|
return yield* Effect9.fail(new WaitNotFound({ id: input.waitId }));
|
|
2422
2446
|
if (current2.state !== "open")
|
|
2423
2447
|
return { wait: toWaitRecord(current2), transitioned: false };
|
|
2424
|
-
const rows = yield*
|
|
2448
|
+
const rows = yield* mapDatabaseError5(db.update(relayWaits).set({
|
|
2425
2449
|
state: input.state,
|
|
2426
2450
|
resolvedAt: toPgDate(input.completedAt),
|
|
2427
2451
|
metadataJson: { ...current2.metadataJson, ...metadata3(input.metadata) }
|
|
2428
|
-
}).where(predicate(tenantId2, relayWaits.tenantId,
|
|
2452
|
+
}).where(predicate(tenantId2, relayWaits.tenantId, eq6(relayWaits.id, input.waitId), eq6(relayWaits.state, "open"))).returning());
|
|
2429
2453
|
const row = rows[0];
|
|
2430
2454
|
if (row === undefined) {
|
|
2431
2455
|
const latest = yield* getWaitRow(db, tenantId2, input.waitId);
|
|
@@ -2457,8 +2481,8 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2457
2481
|
resolveWait
|
|
2458
2482
|
});
|
|
2459
2483
|
}));
|
|
2460
|
-
var getReadyRow = (db, tenantId2, id2) =>
|
|
2461
|
-
var getWaitRow = (db, tenantId2, id2) =>
|
|
2484
|
+
var getReadyRow = (db, tenantId2, id2) => mapDatabaseError5(db.select().from(relayEnvelopeReady).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq6(relayEnvelopeReady.id, id2))).limit(1)).pipe(Effect9.map((rows) => rows[0]));
|
|
2485
|
+
var getWaitRow = (db, tenantId2, id2) => mapDatabaseError5(db.select().from(relayWaits).where(predicate(tenantId2, relayWaits.tenantId, eq6(relayWaits.id, id2))).limit(1)).pipe(Effect9.map((rows) => rows[0]));
|
|
2462
2486
|
var memoryLayer7 = Layer9.sync(Service9, () => {
|
|
2463
2487
|
const envelopes = new Map;
|
|
2464
2488
|
const waits = new Map;
|
|
@@ -2668,7 +2692,7 @@ __export(exports_execution_event_repository, {
|
|
|
2668
2692
|
ExecutionEventCursorNotFound: () => ExecutionEventCursorNotFound,
|
|
2669
2693
|
DuplicateExecutionEvent: () => DuplicateExecutionEvent
|
|
2670
2694
|
});
|
|
2671
|
-
import { asc as asc6, desc as desc3, eq as
|
|
2695
|
+
import { asc as asc6, desc as desc3, eq as eq7, gt, max, or as or2, sql as sql6 } from "drizzle-orm";
|
|
2672
2696
|
import { Context as Context10, Effect as Effect10, Filter, Layer as Layer10, Option as Option2, Schema as Schema20, Stream } from "effect";
|
|
2673
2697
|
var executionEventsChannel = "relay_execution_events";
|
|
2674
2698
|
|
|
@@ -2732,7 +2756,7 @@ var toEvent = (row) => ({
|
|
|
2732
2756
|
data: row.dataJson,
|
|
2733
2757
|
created_at: fromPgDate(row.createdAt)
|
|
2734
2758
|
});
|
|
2735
|
-
var
|
|
2759
|
+
var mapDatabaseError6 = (effect) => effect.pipe(Effect10.mapError((error) => new ExecutionEventRepositoryError({ message: String(error) })));
|
|
2736
2760
|
var mapSqlError = (error) => new ExecutionEventRepositoryError({ message: String(error) });
|
|
2737
2761
|
var appendedSignalPayload = (tenantId2, event) => JSON.stringify({ tenantId: tenantId2, executionId: event.execution_id, sequence: event.sequence });
|
|
2738
2762
|
var parseAppendedSignal = (tenantId2, executionId, payload) => {
|
|
@@ -2778,7 +2802,7 @@ var layer10 = Layer10.effect(Service10, Effect10.gen(function* () {
|
|
|
2778
2802
|
const db = yield* Service;
|
|
2779
2803
|
const append = Effect10.fn("ExecutionEventRepository.append")(function* (input) {
|
|
2780
2804
|
const tenantId2 = yield* current;
|
|
2781
|
-
const previousRows = yield*
|
|
2805
|
+
const previousRows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.execution_id))).orderBy(desc3(relayExecutionEvents.sequence)).limit(1));
|
|
2782
2806
|
const previous = previousRows[0];
|
|
2783
2807
|
if (previous !== undefined && input.sequence === previous.sequence) {
|
|
2784
2808
|
return yield* Effect10.fail(duplicateError(input));
|
|
@@ -2790,11 +2814,11 @@ var layer10 = Layer10.effect(Service10, Effect10.gen(function* () {
|
|
|
2790
2814
|
previous_sequence: previous.sequence
|
|
2791
2815
|
}));
|
|
2792
2816
|
}
|
|
2793
|
-
const duplicateCursorRows = yield*
|
|
2817
|
+
const duplicateCursorRows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.cursor, input.cursor))).limit(1));
|
|
2794
2818
|
if (duplicateCursorRows[0] !== undefined) {
|
|
2795
2819
|
return yield* Effect10.fail(duplicateError(input));
|
|
2796
2820
|
}
|
|
2797
|
-
const rows = yield*
|
|
2821
|
+
const rows = yield* mapDatabaseError6(db.insert(relayExecutionEvents).values(toRow(tenantId2, input)).returning());
|
|
2798
2822
|
const row = rows[0];
|
|
2799
2823
|
if (row === undefined) {
|
|
2800
2824
|
return yield* Effect10.fail(new ExecutionEventRepositoryError({ message: "Execution event append returned no row" }));
|
|
@@ -2804,49 +2828,49 @@ var layer10 = Layer10.effect(Service10, Effect10.gen(function* () {
|
|
|
2804
2828
|
const list4 = Effect10.fn("ExecutionEventRepository.list")(function* (input) {
|
|
2805
2829
|
const tenantId2 = yield* current;
|
|
2806
2830
|
if (input.afterCursor === undefined) {
|
|
2807
|
-
const rows2 = yield*
|
|
2831
|
+
const rows2 = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId))).orderBy(asc6(relayExecutionEvents.sequence)).limit(listLimit(input)));
|
|
2808
2832
|
return rows2.map(toEvent);
|
|
2809
2833
|
}
|
|
2810
|
-
const cursorRows = yield*
|
|
2834
|
+
const cursorRows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), eq7(relayExecutionEvents.cursor, input.afterCursor))).limit(1));
|
|
2811
2835
|
const cursorRow = cursorRows[0];
|
|
2812
2836
|
if (cursorRow === undefined) {
|
|
2813
2837
|
return yield* Effect10.fail(new ExecutionEventCursorNotFound({ execution_id: input.executionId, cursor: input.afterCursor }));
|
|
2814
2838
|
}
|
|
2815
|
-
const rows = yield*
|
|
2839
|
+
const rows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), gt(relayExecutionEvents.sequence, cursorRow.sequence))).orderBy(asc6(relayExecutionEvents.sequence)).limit(listLimit(input)));
|
|
2816
2840
|
return rows.map(toEvent);
|
|
2817
2841
|
});
|
|
2818
2842
|
const findBySequenceOrCursor = Effect10.fn("ExecutionEventRepository.findBySequenceOrCursor")(function* (input) {
|
|
2819
2843
|
const tenantId2 = yield* current;
|
|
2820
|
-
const rows = yield*
|
|
2844
|
+
const rows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), or2(eq7(relayExecutionEvents.sequence, input.sequence), eq7(relayExecutionEvents.cursor, input.cursor)))).orderBy(asc6(relayExecutionEvents.sequence)).limit(1));
|
|
2821
2845
|
const row = rows[0];
|
|
2822
2846
|
return row === undefined ? Option2.none() : Option2.some(toEvent(row));
|
|
2823
2847
|
});
|
|
2824
2848
|
const findByCursor = Effect10.fn("ExecutionEventRepository.findByCursor")(function* (input) {
|
|
2825
2849
|
const tenantId2 = yield* current;
|
|
2826
|
-
const rows = yield*
|
|
2850
|
+
const rows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), eq7(relayExecutionEvents.cursor, input.cursor))).limit(1));
|
|
2827
2851
|
const row = rows[0];
|
|
2828
2852
|
return row === undefined ? Option2.none() : Option2.some(toEvent(row));
|
|
2829
2853
|
});
|
|
2830
2854
|
const findFirstByTypeAfterSequence = Effect10.fn("ExecutionEventRepository.findFirstByTypeAfterSequence")(function* (input) {
|
|
2831
2855
|
const tenantId2 = yield* current;
|
|
2832
|
-
const rows = yield*
|
|
2856
|
+
const rows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), eq7(relayExecutionEvents.type, input.type), gt(relayExecutionEvents.sequence, input.afterSequence))).orderBy(asc6(relayExecutionEvents.sequence)).limit(1));
|
|
2833
2857
|
const row = rows[0];
|
|
2834
2858
|
return row === undefined ? Option2.none() : Option2.some(toEvent(row));
|
|
2835
2859
|
});
|
|
2836
2860
|
const maxSequence = Effect10.fn("ExecutionEventRepository.maxSequence")(function* (executionId) {
|
|
2837
2861
|
const tenantId2 = yield* current;
|
|
2838
|
-
const rows = yield*
|
|
2862
|
+
const rows = yield* mapDatabaseError6(db.select({ value: max(relayExecutionEvents.sequence) }).from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, executionId))));
|
|
2839
2863
|
const value = rows[0]?.value;
|
|
2840
2864
|
return value === null || value === undefined ? undefined : value;
|
|
2841
2865
|
});
|
|
2842
2866
|
const sumUsage = Effect10.fn("ExecutionEventRepository.sumUsage")(function* (executionId) {
|
|
2843
2867
|
const tenantId2 = yield* current;
|
|
2844
|
-
const rows = yield*
|
|
2868
|
+
const rows = yield* mapDatabaseError6(db.select({
|
|
2845
2869
|
value: sql6`sum(
|
|
2846
2870
|
coalesce((${relayExecutionEvents.dataJson} ->> 'input_tokens')::bigint, 0)
|
|
2847
2871
|
+ coalesce((${relayExecutionEvents.dataJson} ->> 'output_tokens')::bigint, 0)
|
|
2848
2872
|
)`
|
|
2849
|
-
}).from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId,
|
|
2873
|
+
}).from(relayExecutionEvents).where(predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, executionId), eq7(relayExecutionEvents.type, usageReportedEventType))));
|
|
2850
2874
|
const value = rows[0]?.value;
|
|
2851
2875
|
return value === null || value === undefined ? 0 : Number(value);
|
|
2852
2876
|
});
|
|
@@ -2857,7 +2881,7 @@ var layer10 = Layer10.effect(Service10, Effect10.gen(function* () {
|
|
|
2857
2881
|
const appendedSignals = (executionId) => Stream.unwrap(current.pipe(Effect10.map((tenantId2) => db.$client.listen(executionEventsChannel).pipe(Stream.mapError(mapSqlError), Stream.filterMap(Filter.fromPredicateOption((payload) => parseAppendedSignal(tenantId2, executionId, payload)))))));
|
|
2858
2882
|
const listAfterSequence = Effect10.fn("ExecutionEventRepository.listAfterSequence")(function* (input) {
|
|
2859
2883
|
const tenantId2 = yield* current;
|
|
2860
|
-
const rows = yield*
|
|
2884
|
+
const rows = yield* mapDatabaseError6(db.select().from(relayExecutionEvents).where(input.afterSequence === undefined ? predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId)) : predicate(tenantId2, relayExecutionEvents.tenantId, eq7(relayExecutionEvents.executionId, input.executionId), gt(relayExecutionEvents.sequence, input.afterSequence))).orderBy(asc6(relayExecutionEvents.sequence)).limit(input.limit));
|
|
2861
2885
|
return rows.map(toEvent);
|
|
2862
2886
|
});
|
|
2863
2887
|
return Service10.of({
|
|
@@ -2977,7 +3001,7 @@ __export(exports_execution_repository, {
|
|
|
2977
3001
|
ExecutionNotFound: () => ExecutionNotFound,
|
|
2978
3002
|
DuplicateExecution: () => DuplicateExecution
|
|
2979
3003
|
});
|
|
2980
|
-
import { and as and3, desc as desc4, eq as
|
|
3004
|
+
import { and as and3, desc as desc4, eq as eq8, lt, or as or3 } from "drizzle-orm";
|
|
2981
3005
|
import { Context as Context11, Effect as Effect11, Layer as Layer11, Schema as Schema21 } from "effect";
|
|
2982
3006
|
class ExecutionRepositoryError extends Schema21.TaggedErrorClass()("ExecutionRepositoryError", {
|
|
2983
3007
|
message: Schema21.String
|
|
@@ -3058,7 +3082,7 @@ var toRecord5 = (row) => {
|
|
|
3058
3082
|
updatedAt: fromPgDate(row.updatedAt)
|
|
3059
3083
|
};
|
|
3060
3084
|
};
|
|
3061
|
-
var
|
|
3085
|
+
var mapDatabaseError7 = (effect) => effect.pipe(Effect11.mapError((error) => new ExecutionRepositoryError({ message: String(error) })));
|
|
3062
3086
|
var maxListLimit2 = 100;
|
|
3063
3087
|
var defaultListLimit = 50;
|
|
3064
3088
|
var listLimit2 = (input) => Math.min(Math.max(input.limit ?? defaultListLimit, 1), maxListLimit2);
|
|
@@ -3069,20 +3093,20 @@ var nextCursorOf = (records, fetched, limit) => {
|
|
|
3069
3093
|
var compareDesc = (left, right) => right.updatedAt - left.updatedAt || (left.id < right.id ? 1 : left.id > right.id ? -1 : 0);
|
|
3070
3094
|
var afterCursor = (record2, cursor) => cursor === undefined || record2.updatedAt < cursor.updatedAt || record2.updatedAt === cursor.updatedAt && record2.id < cursor.id;
|
|
3071
3095
|
var listConditions = (input) => [
|
|
3072
|
-
input.rootAddressId === undefined ? undefined :
|
|
3073
|
-
input.sessionId === undefined ? undefined :
|
|
3074
|
-
input.status === undefined ? undefined :
|
|
3075
|
-
input.cursor === undefined ? undefined : or3(lt(relayExecutions.updatedAt, toPgDate(input.cursor.updatedAt)), and3(
|
|
3096
|
+
input.rootAddressId === undefined ? undefined : eq8(relayExecutions.rootAddressId, input.rootAddressId),
|
|
3097
|
+
input.sessionId === undefined ? undefined : eq8(relayExecutions.sessionId, input.sessionId),
|
|
3098
|
+
input.status === undefined ? undefined : eq8(relayExecutions.status, input.status),
|
|
3099
|
+
input.cursor === undefined ? undefined : or3(lt(relayExecutions.updatedAt, toPgDate(input.cursor.updatedAt)), and3(eq8(relayExecutions.updatedAt, toPgDate(input.cursor.updatedAt)), lt(relayExecutions.id, input.cursor.id)))
|
|
3076
3100
|
].filter((condition) => condition !== undefined);
|
|
3077
3101
|
var layer11 = Layer11.effect(Service11, Effect11.gen(function* () {
|
|
3078
3102
|
const db = yield* Service;
|
|
3079
3103
|
const create2 = Effect11.fn("ExecutionRepository.create")(function* (input) {
|
|
3080
3104
|
const tenantId2 = yield* current;
|
|
3081
3105
|
yield* validateCreateInput(input);
|
|
3082
|
-
const existing = yield*
|
|
3106
|
+
const existing = yield* mapDatabaseError7(db.select().from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId, eq8(relayExecutions.id, input.id))).limit(1));
|
|
3083
3107
|
if (existing[0] !== undefined)
|
|
3084
3108
|
return yield* Effect11.fail(new DuplicateExecution({ id: input.id }));
|
|
3085
|
-
const rows = yield*
|
|
3109
|
+
const rows = yield* mapDatabaseError7(db.insert(relayExecutions).values(toInsert2(tenantId2, input)).returning());
|
|
3086
3110
|
const row = rows[0];
|
|
3087
3111
|
if (row === undefined) {
|
|
3088
3112
|
return yield* Effect11.fail(new ExecutionRepositoryError({ message: "Execution insert returned no row" }));
|
|
@@ -3091,24 +3115,24 @@ var layer11 = Layer11.effect(Service11, Effect11.gen(function* () {
|
|
|
3091
3115
|
});
|
|
3092
3116
|
const get5 = Effect11.fn("ExecutionRepository.get")(function* (id2) {
|
|
3093
3117
|
const tenantId2 = yield* current;
|
|
3094
|
-
const rows = yield*
|
|
3118
|
+
const rows = yield* mapDatabaseError7(db.select().from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId, eq8(relayExecutions.id, id2))).limit(1));
|
|
3095
3119
|
return rows[0] === undefined ? undefined : toRecord5(rows[0]);
|
|
3096
3120
|
});
|
|
3097
3121
|
const list5 = Effect11.fn("ExecutionRepository.list")(function* (input) {
|
|
3098
3122
|
const tenantId2 = yield* current;
|
|
3099
3123
|
const limit = listLimit2(input);
|
|
3100
|
-
const rows = yield*
|
|
3124
|
+
const rows = yield* mapDatabaseError7(db.select().from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId, ...listConditions(input))).orderBy(desc4(relayExecutions.updatedAt), desc4(relayExecutions.id)).limit(limit + 1));
|
|
3101
3125
|
const records = rows.slice(0, limit).map(toRecord5);
|
|
3102
3126
|
const nextCursor = nextCursorOf(records, rows.length, limit);
|
|
3103
3127
|
return nextCursor === undefined ? { records } : { records, nextCursor };
|
|
3104
3128
|
});
|
|
3105
3129
|
const transition = Effect11.fn("ExecutionRepository.transition")(function* (input) {
|
|
3106
3130
|
const tenantId2 = yield* current;
|
|
3107
|
-
const rows = yield*
|
|
3131
|
+
const rows = yield* mapDatabaseError7(db.update(relayExecutions).set({
|
|
3108
3132
|
status: input.status,
|
|
3109
3133
|
metadataJson: metadata4(input.metadata),
|
|
3110
3134
|
updatedAt: toPgDate(input.updatedAt)
|
|
3111
|
-
}).where(predicate(tenantId2, relayExecutions.tenantId,
|
|
3135
|
+
}).where(predicate(tenantId2, relayExecutions.tenantId, eq8(relayExecutions.id, input.id))).returning());
|
|
3112
3136
|
const row = rows[0];
|
|
3113
3137
|
if (row === undefined)
|
|
3114
3138
|
return yield* Effect11.fail(new ExecutionNotFound({ id: input.id }));
|
|
@@ -3187,7 +3211,7 @@ var transition = Effect11.fn("ExecutionRepository.transition.call")(function* (i
|
|
|
3187
3211
|
return yield* repository.transition(input);
|
|
3188
3212
|
});
|
|
3189
3213
|
// ../store-sql/src/idempotency/idempotency-repository.ts
|
|
3190
|
-
import { eq as
|
|
3214
|
+
import { eq as eq9, sql as sql7 } from "drizzle-orm";
|
|
3191
3215
|
import { Context as Context12, Effect as Effect12, HashMap, Layer as Layer12, Option as Option3, Ref, Schema as Schema22, Semaphore } from "effect";
|
|
3192
3216
|
class IdempotencyRepositoryError extends Schema22.TaggedErrorClass()("IdempotencyRepositoryError", {
|
|
3193
3217
|
message: Schema22.String
|
|
@@ -3228,7 +3252,7 @@ var layer12 = Layer12.effect(Service12, Effect12.gen(function* () {
|
|
|
3228
3252
|
const tenantId2 = yield* current;
|
|
3229
3253
|
return yield* db.transaction((tx) => Effect12.gen(function* () {
|
|
3230
3254
|
yield* tx.execute(sql7`select pg_advisory_xact_lock(hashtextextended(${`${tenantId2}:${operationId(input)}`}, 0))`);
|
|
3231
|
-
const existingRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId,
|
|
3255
|
+
const existingRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId, eq9(relayIdempotencyKeys.scope, input.scope), eq9(relayIdempotencyKeys.operation, input.operation), eq9(relayIdempotencyKeys.key, input.key))).limit(1);
|
|
3232
3256
|
const existing = existingRows[0];
|
|
3233
3257
|
if (existing !== undefined)
|
|
3234
3258
|
return { replayed: true, record: toRecord6(existing) };
|
|
@@ -3237,7 +3261,7 @@ var layer12 = Layer12.effect(Service12, Effect12.gen(function* () {
|
|
|
3237
3261
|
const row = rows[0];
|
|
3238
3262
|
if (row !== undefined)
|
|
3239
3263
|
return { replayed: false, record: toRecord6(row) };
|
|
3240
|
-
const winnerRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId,
|
|
3264
|
+
const winnerRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId, eq9(relayIdempotencyKeys.scope, input.scope), eq9(relayIdempotencyKeys.operation, input.operation), eq9(relayIdempotencyKeys.key, input.key))).limit(1);
|
|
3241
3265
|
const winner = winnerRows[0];
|
|
3242
3266
|
if (winner === undefined) {
|
|
3243
3267
|
return yield* Effect12.fail(new IdempotencyRepositoryError({ message: "Idempotency insert returned no row" }));
|
|
@@ -3314,7 +3338,7 @@ var queryRowToRecord = (row) => ({
|
|
|
3314
3338
|
metadata: row.metadataJson,
|
|
3315
3339
|
createdAt: fromPgDate(row.createdAt)
|
|
3316
3340
|
});
|
|
3317
|
-
var
|
|
3341
|
+
var mapDatabaseError8 = (effect) => effect.pipe(Effect13.mapError((error) => new MemoryRepositoryError({ message: String(error) })));
|
|
3318
3342
|
var parsePgFloatArray = (value) => {
|
|
3319
3343
|
const body = value.startsWith("{") && value.endsWith("}") ? value.slice(1, -1) : value;
|
|
3320
3344
|
if (body.length === 0)
|
|
@@ -3358,7 +3382,7 @@ var layer13 = Layer13.effect(Service13, Effect13.gen(function* () {
|
|
|
3358
3382
|
const upsert2 = Effect13.fn("MemoryRepository.upsert")(function* (input) {
|
|
3359
3383
|
const tenantId2 = yield* current;
|
|
3360
3384
|
yield* validateVector("embedding", input.embedding);
|
|
3361
|
-
const rows = yield*
|
|
3385
|
+
const rows = yield* mapDatabaseError8(db.insert(relayMemoryRecords).values({
|
|
3362
3386
|
tenantId: tenantId2,
|
|
3363
3387
|
id: input.id,
|
|
3364
3388
|
agent: input.agent,
|
|
@@ -3389,7 +3413,7 @@ var layer13 = Layer13.effect(Service13, Effect13.gen(function* () {
|
|
|
3389
3413
|
if (input.topK <= 0)
|
|
3390
3414
|
return [];
|
|
3391
3415
|
const queryEmbeddingSql = sql8`array[${sql8.join(input.embedding.map((value) => sql8`${value}`), sql8`, `)}]::double precision[]`;
|
|
3392
|
-
const rows = yield*
|
|
3416
|
+
const rows = yield* mapDatabaseError8(db.execute(sql8`
|
|
3393
3417
|
with query_embedding as (
|
|
3394
3418
|
select ${queryEmbeddingSql} as embedding
|
|
3395
3419
|
),
|
|
@@ -3529,7 +3553,7 @@ __export(exports_permission_rule_repository, {
|
|
|
3529
3553
|
Service: () => Service14,
|
|
3530
3554
|
PermissionRuleRepositoryError: () => PermissionRuleRepositoryError
|
|
3531
3555
|
});
|
|
3532
|
-
import { asc as asc7, eq as
|
|
3556
|
+
import { asc as asc7, eq as eq10 } from "drizzle-orm";
|
|
3533
3557
|
import { Context as Context14, Effect as Effect14, Layer as Layer14, Schema as Schema24 } from "effect";
|
|
3534
3558
|
class PermissionRuleRepositoryError extends Schema24.TaggedErrorClass()("PermissionRuleRepositoryError", {
|
|
3535
3559
|
message: Schema24.String
|
|
@@ -3544,13 +3568,13 @@ var toRecord8 = (row) => ({
|
|
|
3544
3568
|
rule: row.ruleJson,
|
|
3545
3569
|
createdAt: fromPgDate(row.createdAt)
|
|
3546
3570
|
});
|
|
3547
|
-
var
|
|
3571
|
+
var mapDatabaseError9 = (effect) => effect.pipe(Effect14.mapError((error) => new PermissionRuleRepositoryError({ message: String(error) })));
|
|
3548
3572
|
var memoryKey = (agent, scope, pattern) => `${agent}\x00${scope}\x00${pattern}`;
|
|
3549
3573
|
var layer14 = Layer14.effect(Service14, Effect14.gen(function* () {
|
|
3550
3574
|
const db = yield* Service;
|
|
3551
3575
|
const remember = Effect14.fn("PermissionRuleRepository.remember")(function* (input) {
|
|
3552
3576
|
const tenantId2 = yield* current;
|
|
3553
|
-
const rows = yield*
|
|
3577
|
+
const rows = yield* mapDatabaseError9(db.insert(relayPermissionRules).values({
|
|
3554
3578
|
tenantId: tenantId2,
|
|
3555
3579
|
agent: input.agent,
|
|
3556
3580
|
scope: input.scope,
|
|
@@ -3577,7 +3601,7 @@ var layer14 = Layer14.effect(Service14, Effect14.gen(function* () {
|
|
|
3577
3601
|
});
|
|
3578
3602
|
const list6 = Effect14.fn("PermissionRuleRepository.list")(function* (input) {
|
|
3579
3603
|
const tenantId2 = yield* current;
|
|
3580
|
-
const rows = yield*
|
|
3604
|
+
const rows = yield* mapDatabaseError9(db.select().from(relayPermissionRules).where(predicate(tenantId2, relayPermissionRules.tenantId, eq10(relayPermissionRules.agent, input.agent), eq10(relayPermissionRules.scope, input.scope))).orderBy(asc7(relayPermissionRules.createdAt), asc7(relayPermissionRules.pattern)));
|
|
3581
3605
|
return rows.map(toRecord8);
|
|
3582
3606
|
});
|
|
3583
3607
|
return Service14.of({ remember, list: list6 });
|
|
@@ -3620,7 +3644,7 @@ __export(exports_context_epoch_repository, {
|
|
|
3620
3644
|
Service: () => Service15,
|
|
3621
3645
|
ContextEpochRepositoryError: () => ContextEpochRepositoryError
|
|
3622
3646
|
});
|
|
3623
|
-
import { eq as
|
|
3647
|
+
import { eq as eq11 } from "drizzle-orm";
|
|
3624
3648
|
import { Context as Context15, Effect as Effect15, Layer as Layer15, Schema as Schema25 } from "effect";
|
|
3625
3649
|
class ContextEpochRepositoryError extends Schema25.TaggedErrorClass()("ContextEpochRepositoryError", {
|
|
3626
3650
|
message: Schema25.String
|
|
@@ -3635,7 +3659,7 @@ var toRecord9 = (row) => ({
|
|
|
3635
3659
|
dynamicSourceIds: row.dynamicSourceIdsJson,
|
|
3636
3660
|
openedAt: fromPgDate(row.openedAt)
|
|
3637
3661
|
});
|
|
3638
|
-
var
|
|
3662
|
+
var mapDatabaseError10 = (effect) => effect.pipe(Effect15.mapError((error) => new ContextEpochRepositoryError({ message: String(error) })));
|
|
3639
3663
|
var sameEpoch = (record2, input) => record2.baseline === input.baseline && JSON.stringify(record2.dynamicSourceIds) === JSON.stringify(input.dynamicSourceIds);
|
|
3640
3664
|
var conflict2 = (executionId) => new ContextEpochRepositoryError({ message: `Context epoch already exists with different payload: ${executionId}` });
|
|
3641
3665
|
var cloneRecord5 = (record2) => structuredClone(record2);
|
|
@@ -3643,12 +3667,12 @@ var layer15 = Layer15.effect(Service15, Effect15.gen(function* () {
|
|
|
3643
3667
|
const db = yield* Service;
|
|
3644
3668
|
const get6 = Effect15.fn("ContextEpochRepository.get")(function* (executionId) {
|
|
3645
3669
|
const tenantId2 = yield* current;
|
|
3646
|
-
const rows = yield*
|
|
3670
|
+
const rows = yield* mapDatabaseError10(db.select().from(relayExecutionContextEpochs).where(predicate(tenantId2, relayExecutionContextEpochs.tenantId, eq11(relayExecutionContextEpochs.executionId, executionId))).limit(1));
|
|
3647
3671
|
return rows[0] === undefined ? undefined : toRecord9(rows[0]);
|
|
3648
3672
|
});
|
|
3649
3673
|
const save2 = Effect15.fn("ContextEpochRepository.save")(function* (input) {
|
|
3650
3674
|
const tenantId2 = yield* current;
|
|
3651
|
-
const rows = yield*
|
|
3675
|
+
const rows = yield* mapDatabaseError10(db.insert(relayExecutionContextEpochs).values({
|
|
3652
3676
|
tenantId: tenantId2,
|
|
3653
3677
|
executionId: input.executionId,
|
|
3654
3678
|
baseline: input.baseline,
|
|
@@ -3722,7 +3746,7 @@ __export(exports_schedule_repository, {
|
|
|
3722
3746
|
ScheduleNotFound: () => ScheduleNotFound,
|
|
3723
3747
|
ScheduleClaimMismatch: () => ScheduleClaimMismatch
|
|
3724
3748
|
});
|
|
3725
|
-
import { and as and4, asc as asc8, eq as
|
|
3749
|
+
import { and as and4, asc as asc8, eq as eq12, inArray as inArray2, lte as lte2, or as or4, sql as sql9 } from "drizzle-orm";
|
|
3726
3750
|
import { Context as Context16, Effect as Effect16, Layer as Layer16, Schema as Schema26 } from "effect";
|
|
3727
3751
|
class ScheduleRepositoryError extends Schema26.TaggedErrorClass()("ScheduleRepositoryError", {
|
|
3728
3752
|
message: Schema26.String
|
|
@@ -3789,12 +3813,12 @@ var toRecord10 = (row) => ({
|
|
|
3789
3813
|
created_at: fromPgDate(row.createdAt),
|
|
3790
3814
|
updated_at: fromPgDate(row.updatedAt)
|
|
3791
3815
|
});
|
|
3792
|
-
var
|
|
3816
|
+
var mapDatabaseError11 = (effect) => effect.pipe(Effect16.mapError((error) => new ScheduleRepositoryError({ message: String(error) })));
|
|
3793
3817
|
var cancellableStates = new Set(["active", "claimed"]);
|
|
3794
3818
|
var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
3795
3819
|
const db = yield* Service;
|
|
3796
|
-
const getRow = (tenantId2, id2) =>
|
|
3797
|
-
const getByIdempotencyKey = (tenantId2, key2) =>
|
|
3820
|
+
const getRow = (tenantId2, id2) => mapDatabaseError11(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, eq12(relaySchedules.id, id2))).limit(1)).pipe(Effect16.map((rows) => rows[0]));
|
|
3821
|
+
const getByIdempotencyKey = (tenantId2, key2) => mapDatabaseError11(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, eq12(relaySchedules.idempotencyKey, key2))).limit(1)).pipe(Effect16.map((rows) => rows[0]));
|
|
3798
3822
|
const create3 = Effect16.fn("ScheduleRepository.create")(function* (input) {
|
|
3799
3823
|
const tenantId2 = yield* current;
|
|
3800
3824
|
const invalid = validationError(input);
|
|
@@ -3805,7 +3829,7 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3805
3829
|
if (existing !== undefined)
|
|
3806
3830
|
return toRecord10(existing);
|
|
3807
3831
|
}
|
|
3808
|
-
const rows = yield*
|
|
3832
|
+
const rows = yield* mapDatabaseError11(db.insert(relaySchedules).values(toInsert4(tenantId2, input)).returning());
|
|
3809
3833
|
const row = rows[0];
|
|
3810
3834
|
if (row === undefined) {
|
|
3811
3835
|
return yield* Effect16.fail(new ScheduleRepositoryError({ message: `Schedule ${input.id} was not persisted` }));
|
|
@@ -3819,17 +3843,17 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3819
3843
|
});
|
|
3820
3844
|
const list7 = Effect16.fn("ScheduleRepository.list")(function* (input) {
|
|
3821
3845
|
const tenantId2 = yield* current;
|
|
3822
|
-
const rows = yield*
|
|
3846
|
+
const rows = yield* mapDatabaseError11(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, input.state === undefined ? undefined : eq12(relaySchedules.state, input.state))).orderBy(asc8(relaySchedules.nextRunAt), asc8(relaySchedules.createdAt)));
|
|
3823
3847
|
return rows.map(toRecord10);
|
|
3824
3848
|
});
|
|
3825
3849
|
const cancel = Effect16.fn("ScheduleRepository.cancel")(function* (input) {
|
|
3826
3850
|
const tenantId2 = yield* current;
|
|
3827
|
-
const rows = yield*
|
|
3851
|
+
const rows = yield* mapDatabaseError11(db.update(relaySchedules).set({
|
|
3828
3852
|
state: "cancelled",
|
|
3829
3853
|
claimOwner: null,
|
|
3830
3854
|
claimExpiresAt: null,
|
|
3831
3855
|
updatedAt: toPgDate(input.cancelledAt)
|
|
3832
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId,
|
|
3856
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq12(relaySchedules.id, input.id), inArray2(relaySchedules.state, [...cancellableStates]))).returning());
|
|
3833
3857
|
const row = rows[0];
|
|
3834
3858
|
if (row !== undefined)
|
|
3835
3859
|
return toRecord10(row);
|
|
@@ -3842,8 +3866,8 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3842
3866
|
});
|
|
3843
3867
|
const claimDue = Effect16.fn("ScheduleRepository.claimDue")(function* (input) {
|
|
3844
3868
|
const tenantId2 = yield* current;
|
|
3845
|
-
const candidate = db.select({ id: relaySchedules.id }).from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, or4(and4(
|
|
3846
|
-
const rows = yield*
|
|
3869
|
+
const candidate = db.select({ id: relaySchedules.id }).from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, or4(and4(eq12(relaySchedules.state, "active"), lte2(relaySchedules.nextRunAt, toPgDate(input.now))), and4(eq12(relaySchedules.state, "claimed"), lte2(relaySchedules.claimExpiresAt, toPgDate(input.now)))))).orderBy(asc8(relaySchedules.nextRunAt), asc8(relaySchedules.createdAt)).limit(1).for("update", { skipLocked: true });
|
|
3870
|
+
const rows = yield* mapDatabaseError11(db.update(relaySchedules).set({
|
|
3847
3871
|
state: "claimed",
|
|
3848
3872
|
claimOwner: input.workerId,
|
|
3849
3873
|
claimExpiresAt: toPgDate(input.claimExpiresAt),
|
|
@@ -3854,11 +3878,11 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3854
3878
|
});
|
|
3855
3879
|
const ack = Effect16.fn("ScheduleRepository.ack")(function* (input) {
|
|
3856
3880
|
const tenantId2 = yield* current;
|
|
3857
|
-
const rows = yield*
|
|
3881
|
+
const rows = yield* mapDatabaseError11(db.update(relaySchedules).set({
|
|
3858
3882
|
...input.nextRunAt === undefined ? { state: "completed" } : { state: "active", nextRunAt: toPgDate(input.nextRunAt), claimOwner: null },
|
|
3859
3883
|
claimExpiresAt: null,
|
|
3860
3884
|
updatedAt: toPgDate(input.now)
|
|
3861
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId,
|
|
3885
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq12(relaySchedules.id, input.id), eq12(relaySchedules.claimOwner, input.workerId), eq12(relaySchedules.state, "claimed"))).returning());
|
|
3862
3886
|
const row = rows[0];
|
|
3863
3887
|
if (row !== undefined)
|
|
3864
3888
|
return toRecord10(row);
|
|
@@ -3871,14 +3895,14 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3871
3895
|
});
|
|
3872
3896
|
const release = Effect16.fn("ScheduleRepository.release")(function* (input) {
|
|
3873
3897
|
const tenantId2 = yield* current;
|
|
3874
|
-
const rows = yield*
|
|
3898
|
+
const rows = yield* mapDatabaseError11(db.update(relaySchedules).set({
|
|
3875
3899
|
state: "active",
|
|
3876
3900
|
nextRunAt: toPgDate(input.nextAvailableAt),
|
|
3877
3901
|
claimOwner: null,
|
|
3878
3902
|
claimExpiresAt: null,
|
|
3879
3903
|
lastError: input.error,
|
|
3880
3904
|
updatedAt: toPgDate(input.nextAvailableAt)
|
|
3881
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId,
|
|
3905
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq12(relaySchedules.id, input.id), eq12(relaySchedules.claimOwner, input.workerId), eq12(relaySchedules.state, "claimed"))).returning());
|
|
3882
3906
|
const row = rows[0];
|
|
3883
3907
|
if (row !== undefined)
|
|
3884
3908
|
return toRecord10(row);
|
|
@@ -4059,7 +4083,7 @@ __export(exports_session_repository, {
|
|
|
4059
4083
|
Service: () => Service17,
|
|
4060
4084
|
DuplicateSession: () => DuplicateSession
|
|
4061
4085
|
});
|
|
4062
|
-
import { and as and5, count, desc as desc5, eq as
|
|
4086
|
+
import { and as and5, count, desc as desc5, eq as eq13, lt as lt2, or as or5 } from "drizzle-orm";
|
|
4063
4087
|
import { Context as Context17, Effect as Effect17, Layer as Layer17, Schema as Schema27 } from "effect";
|
|
4064
4088
|
class SessionRepositoryError extends Schema27.TaggedErrorClass()("SessionRepositoryError", {
|
|
4065
4089
|
message: Schema27.String
|
|
@@ -4135,7 +4159,7 @@ var toEntryRecord = (row) => {
|
|
|
4135
4159
|
return Effect17.fail(new SessionRepositoryError({ message: `Unknown session entry tag: ${row.tag}` }));
|
|
4136
4160
|
}
|
|
4137
4161
|
};
|
|
4138
|
-
var
|
|
4162
|
+
var mapDatabaseError12 = (effect) => effect.pipe(Effect17.mapError((error) => new SessionRepositoryError({ message: String(error) })));
|
|
4139
4163
|
var mapWriteError = (effect) => effect.pipe(Effect17.mapError((error) => error instanceof SessionRepositoryError ? error : new SessionRepositoryError({ message: String(error) })));
|
|
4140
4164
|
var maxListLimit3 = 100;
|
|
4141
4165
|
var defaultListLimit2 = 50;
|
|
@@ -4147,8 +4171,8 @@ var nextCursorOf2 = (records, fetched, limit) => {
|
|
|
4147
4171
|
var compareDesc2 = (left, right) => right.updatedAt - left.updatedAt || (left.id < right.id ? 1 : left.id > right.id ? -1 : 0);
|
|
4148
4172
|
var afterCursor2 = (record2, cursor) => cursor === undefined || record2.updatedAt < cursor.updatedAt || record2.updatedAt === cursor.updatedAt && record2.id < cursor.id;
|
|
4149
4173
|
var listConditions2 = (input) => [
|
|
4150
|
-
input.rootAddressId === undefined ? undefined :
|
|
4151
|
-
input.cursor === undefined ? undefined : or5(lt2(relaySessions.updatedAt, toPgDate(input.cursor.updatedAt)), and5(
|
|
4174
|
+
input.rootAddressId === undefined ? undefined : eq13(relaySessions.rootAddressId, input.rootAddressId),
|
|
4175
|
+
input.cursor === undefined ? undefined : or5(lt2(relaySessions.updatedAt, toPgDate(input.cursor.updatedAt)), and5(eq13(relaySessions.updatedAt, toPgDate(input.cursor.updatedAt)), lt2(relaySessions.id, input.cursor.id)))
|
|
4152
4176
|
].filter((condition) => condition !== undefined);
|
|
4153
4177
|
var comparableEntry = (entry) => {
|
|
4154
4178
|
const { createdAt: _createdAt, ...value } = entry;
|
|
@@ -4192,11 +4216,11 @@ var validateCompaction = (currentPath, input) => {
|
|
|
4192
4216
|
var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
4193
4217
|
const db = yield* Service;
|
|
4194
4218
|
const getForTenant = Effect17.fn("SessionRepository.getForTenant")(function* (tenantId2, id2) {
|
|
4195
|
-
const rows = yield*
|
|
4219
|
+
const rows = yield* mapDatabaseError12(db.select().from(relaySessions).where(predicate(tenantId2, relaySessions.tenantId, eq13(relaySessions.id, id2))).limit(1));
|
|
4196
4220
|
return rows[0] === undefined ? undefined : toSessionRecord(rows[0]);
|
|
4197
4221
|
});
|
|
4198
4222
|
const entryById = Effect17.fn("SessionRepository.entryById")(function* (tenantId2, sessionId, id2) {
|
|
4199
|
-
const rows = yield*
|
|
4223
|
+
const rows = yield* mapDatabaseError12(db.select().from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId, eq13(relaySessionEntries.id, id2), eq13(relaySessionEntries.sessionId, sessionId))).limit(1));
|
|
4200
4224
|
return rows[0] === undefined ? undefined : yield* toEntryRecord(rows[0]);
|
|
4201
4225
|
});
|
|
4202
4226
|
const pathForTenant = Effect17.fn("SessionRepository.pathForTenant")(function* (tenantId2, input) {
|
|
@@ -4231,7 +4255,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4231
4255
|
const existing = yield* getForTenant(tenantId2, input.id);
|
|
4232
4256
|
if (existing !== undefined)
|
|
4233
4257
|
return yield* Effect17.fail(new DuplicateSession({ id: input.id }));
|
|
4234
|
-
const rows = yield*
|
|
4258
|
+
const rows = yield* mapDatabaseError12(db.insert(relaySessions).values({
|
|
4235
4259
|
tenantId: tenantId2,
|
|
4236
4260
|
id: input.id,
|
|
4237
4261
|
rootAddressId: input.rootAddressId,
|
|
@@ -4250,13 +4274,13 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4250
4274
|
if (existing !== undefined) {
|
|
4251
4275
|
if (existing.rootAddressId !== input.rootAddressId)
|
|
4252
4276
|
return yield* Effect17.fail(rootMismatch(input.id));
|
|
4253
|
-
const rows2 = yield*
|
|
4277
|
+
const rows2 = yield* mapDatabaseError12(db.update(relaySessions).set({ updatedAt: toPgDate(input.now) }).where(predicate(tenantId2, relaySessions.tenantId, eq13(relaySessions.id, input.id))).returning());
|
|
4254
4278
|
const row2 = rows2[0];
|
|
4255
4279
|
if (row2 === undefined)
|
|
4256
4280
|
return yield* Effect17.fail(missingSession(input.id));
|
|
4257
4281
|
return toSessionRecord(row2);
|
|
4258
4282
|
}
|
|
4259
|
-
const rows = yield*
|
|
4283
|
+
const rows = yield* mapDatabaseError12(db.insert(relaySessions).values({
|
|
4260
4284
|
tenantId: tenantId2,
|
|
4261
4285
|
id: input.id,
|
|
4262
4286
|
rootAddressId: input.rootAddressId,
|
|
@@ -4276,14 +4300,14 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4276
4300
|
const list8 = Effect17.fn("SessionRepository.list")(function* (input) {
|
|
4277
4301
|
const tenantId2 = yield* current;
|
|
4278
4302
|
const limit = listLimit3(input);
|
|
4279
|
-
const rows = yield*
|
|
4303
|
+
const rows = yield* mapDatabaseError12(db.select().from(relaySessions).where(predicate(tenantId2, relaySessions.tenantId, ...listConditions2(input))).orderBy(desc5(relaySessions.updatedAt), desc5(relaySessions.id)).limit(limit + 1));
|
|
4280
4304
|
const records = rows.slice(0, limit).map(toSessionRecord);
|
|
4281
4305
|
const nextCursor = nextCursorOf2(records, rows.length, limit);
|
|
4282
4306
|
return nextCursor === undefined ? { records } : { records, nextCursor };
|
|
4283
4307
|
});
|
|
4284
4308
|
const touch = Effect17.fn("SessionRepository.touch")(function* (input) {
|
|
4285
4309
|
const tenantId2 = yield* current;
|
|
4286
|
-
const rows = yield*
|
|
4310
|
+
const rows = yield* mapDatabaseError12(db.update(relaySessions).set({ updatedAt: toPgDate(input.updatedAt) }).where(predicate(tenantId2, relaySessions.tenantId, eq13(relaySessions.id, input.id))).returning());
|
|
4287
4311
|
const row = rows[0];
|
|
4288
4312
|
if (row === undefined)
|
|
4289
4313
|
return yield* Effect17.fail(missingSession(input.id));
|
|
@@ -4305,7 +4329,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4305
4329
|
if (entry === undefined)
|
|
4306
4330
|
return yield* Effect17.fail(missingEntry(input.id));
|
|
4307
4331
|
}
|
|
4308
|
-
yield*
|
|
4332
|
+
yield* mapDatabaseError12(db.update(relaySessions).set({ leafEntryId: input.id, updatedAt: toPgDate(input.updatedAt) }).where(predicate(tenantId2, relaySessions.tenantId, eq13(relaySessions.id, input.sessionId))));
|
|
4309
4333
|
});
|
|
4310
4334
|
const setLeaf = Effect17.fn("SessionRepository.setLeaf")(function* (input) {
|
|
4311
4335
|
const tenantId2 = yield* current;
|
|
@@ -4342,7 +4366,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4342
4366
|
createdAt: toPgDate(input.createdAt)
|
|
4343
4367
|
}).onConflictDoNothing({ target: [relaySessionEntries.tenantId, relaySessionEntries.id] }).returning();
|
|
4344
4368
|
const row = rows[0];
|
|
4345
|
-
const record2 = row === undefined ? yield* tx.select().from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId,
|
|
4369
|
+
const record2 = row === undefined ? yield* tx.select().from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId, eq13(relaySessionEntries.id, input.id), eq13(relaySessionEntries.sessionId, input.sessionId))).limit(1).pipe(Effect17.flatMap((existingRows) => {
|
|
4346
4370
|
const existingRow = existingRows[0];
|
|
4347
4371
|
if (existingRow === undefined) {
|
|
4348
4372
|
return Effect17.fail(new SessionRepositoryError({ message: "Session entry insert returned no row" }));
|
|
@@ -4351,7 +4375,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4351
4375
|
})) : yield* toEntryRecord(row);
|
|
4352
4376
|
if (!sameEntry(record2, desired))
|
|
4353
4377
|
return yield* Effect17.fail(conflictingEntry(input.id));
|
|
4354
|
-
const sessionRows = yield* tx.update(relaySessions).set({ leafEntryId: input.id, updatedAt: toPgDate(input.createdAt) }).where(predicate(tenantId2, relaySessions.tenantId,
|
|
4378
|
+
const sessionRows = yield* tx.update(relaySessions).set({ leafEntryId: input.id, updatedAt: toPgDate(input.createdAt) }).where(predicate(tenantId2, relaySessions.tenantId, eq13(relaySessions.id, input.sessionId))).returning();
|
|
4355
4379
|
if (sessionRows[0] === undefined)
|
|
4356
4380
|
return yield* Effect17.fail(missingSession(input.sessionId));
|
|
4357
4381
|
return record2;
|
|
@@ -4359,7 +4383,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4359
4383
|
});
|
|
4360
4384
|
const countEntries = Effect17.fn("SessionRepository.countEntries")(function* (sessionId) {
|
|
4361
4385
|
const tenantId2 = yield* current;
|
|
4362
|
-
const rows = yield*
|
|
4386
|
+
const rows = yield* mapDatabaseError12(db.select({ value: count() }).from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId, eq13(relaySessionEntries.sessionId, sessionId))));
|
|
4363
4387
|
return Number(rows[0]?.value ?? 0);
|
|
4364
4388
|
});
|
|
4365
4389
|
return Service17.of({ create: create4, ensure, get: get8, list: list8, touch, appendEntry, countEntries, path, setLeaf, leaf });
|
|
@@ -4548,7 +4572,7 @@ __export(exports_skill_definition_repository, {
|
|
|
4548
4572
|
SkillDefinitionRepositoryError: () => SkillDefinitionRepositoryError,
|
|
4549
4573
|
Service: () => Service18
|
|
4550
4574
|
});
|
|
4551
|
-
import { asc as asc9, desc as desc6, eq as
|
|
4575
|
+
import { asc as asc9, desc as desc6, eq as eq14, inArray as inArray3, sql as sql10 } from "drizzle-orm";
|
|
4552
4576
|
import { Context as Context18, Effect as Effect18, HashSet, Layer as Layer18, Schema as Schema28, Semaphore as Semaphore2 } from "effect";
|
|
4553
4577
|
class SkillDefinitionRepositoryError extends Schema28.TaggedErrorClass()("SkillDefinitionRepositoryError", {
|
|
4554
4578
|
message: Schema28.String
|
|
@@ -4611,10 +4635,10 @@ var missingSkillMessage = (requested, records) => {
|
|
|
4611
4635
|
const found = HashSet.fromIterable(records.map((record2) => record2.id));
|
|
4612
4636
|
return `Skill definition not found: ${requested.find((id2) => !HashSet.has(found, id2)) ?? requested[0]}`;
|
|
4613
4637
|
};
|
|
4614
|
-
var
|
|
4638
|
+
var mapDatabaseError13 = (effect) => effect.pipe(Effect18.mapError((error) => new SkillDefinitionRepositoryError({ message: String(error) })));
|
|
4615
4639
|
var mapPutError2 = (effect) => effect.pipe(Effect18.mapError((error) => error instanceof SkillDefinitionRepositoryError ? error : new SkillDefinitionRepositoryError({ message: String(error) })));
|
|
4616
4640
|
var skillPinLockKey = (tenantId2, executionId) => `skill-pins:${tenantId2}:${executionId}`;
|
|
4617
|
-
var lockExecutionPins = (db, tenantId2, executionId) =>
|
|
4641
|
+
var lockExecutionPins = (db, tenantId2, executionId) => mapDatabaseError13(db.execute(sql10`select pg_advisory_xact_lock(hashtext(${skillPinLockKey(tenantId2, executionId)}))`)).pipe(Effect18.asVoid);
|
|
4618
4642
|
var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
4619
4643
|
const db = yield* Service;
|
|
4620
4644
|
const put2 = Effect18.fn("SkillDefinitionRepository.put")(function* (input) {
|
|
@@ -4657,32 +4681,32 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4657
4681
|
});
|
|
4658
4682
|
const get9 = Effect18.fn("SkillDefinitionRepository.get")(function* (id2) {
|
|
4659
4683
|
const tenantId2 = yield* current;
|
|
4660
|
-
const rows = yield*
|
|
4684
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId, eq14(relaySkillDefinitions.id, id2))).limit(1));
|
|
4661
4685
|
return rows[0] === undefined ? undefined : toRecord11(rows[0]);
|
|
4662
4686
|
});
|
|
4663
4687
|
const getRevision2 = Effect18.fn("SkillDefinitionRepository.getRevision")(function* (input) {
|
|
4664
4688
|
const tenantId2 = yield* current;
|
|
4665
|
-
const rows = yield*
|
|
4689
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySkillDefinitionRevisions).where(predicate(tenantId2, relaySkillDefinitionRevisions.tenantId, eq14(relaySkillDefinitionRevisions.skillDefinitionId, input.id), eq14(relaySkillDefinitionRevisions.revision, input.revision))).limit(1));
|
|
4666
4690
|
return rows[0] === undefined ? undefined : toRevisionRecord2(rows[0]);
|
|
4667
4691
|
});
|
|
4668
4692
|
const list9 = Effect18.fn("SkillDefinitionRepository.list")(function* () {
|
|
4669
4693
|
const tenantId2 = yield* current;
|
|
4670
|
-
const rows = yield*
|
|
4694
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId)).orderBy(desc6(relaySkillDefinitions.createdAt), asc9(relaySkillDefinitions.id)));
|
|
4671
4695
|
return rows.map(toRecord11);
|
|
4672
4696
|
});
|
|
4673
4697
|
const listRevisions2 = Effect18.fn("SkillDefinitionRepository.listRevisions")(function* (id2) {
|
|
4674
4698
|
const tenantId2 = yield* current;
|
|
4675
|
-
const rows = yield*
|
|
4699
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySkillDefinitionRevisions).where(predicate(tenantId2, relaySkillDefinitionRevisions.tenantId, eq14(relaySkillDefinitionRevisions.skillDefinitionId, id2))).orderBy(desc6(relaySkillDefinitionRevisions.revision)));
|
|
4676
4700
|
return rows.map(toRevisionRecord2);
|
|
4677
4701
|
});
|
|
4678
4702
|
const listPins = Effect18.fn("SkillDefinitionRepository.listPins")(function* (executionId) {
|
|
4679
4703
|
const tenantId2 = yield* current;
|
|
4680
|
-
const rows = yield*
|
|
4704
|
+
const rows = yield* mapDatabaseError13(db.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId, eq14(relayExecutionSkillPins.executionId, executionId))).orderBy(asc9(relayExecutionSkillPins.skillDefinitionId)));
|
|
4681
4705
|
return rows.map(toPinRecord);
|
|
4682
4706
|
});
|
|
4683
4707
|
const getPinned = Effect18.fn("SkillDefinitionRepository.getPinned")(function* (input) {
|
|
4684
4708
|
const tenantId2 = yield* current;
|
|
4685
|
-
const rows = yield*
|
|
4709
|
+
const rows = yield* mapDatabaseError13(db.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId, eq14(relayExecutionSkillPins.executionId, input.executionId), eq14(relayExecutionSkillPins.skillDefinitionId, input.skillDefinitionId))).limit(1));
|
|
4686
4710
|
return rows[0] === undefined ? undefined : toPinRecord(rows[0]);
|
|
4687
4711
|
});
|
|
4688
4712
|
const pinExecution = Effect18.fn("SkillDefinitionRepository.pinExecution")(function* (input) {
|
|
@@ -4692,7 +4716,7 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4692
4716
|
return [];
|
|
4693
4717
|
return yield* mapPutError2(db.transaction((tx) => Effect18.gen(function* () {
|
|
4694
4718
|
yield* lockExecutionPins(tx, tenantId2, input.executionId);
|
|
4695
|
-
const existingRows = yield* tx.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId,
|
|
4719
|
+
const existingRows = yield* tx.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId, eq14(relayExecutionSkillPins.executionId, input.executionId))).orderBy(asc9(relayExecutionSkillPins.skillDefinitionId));
|
|
4696
4720
|
if (existingRows.length > 0) {
|
|
4697
4721
|
const existing = existingRows.map(toPinRecord);
|
|
4698
4722
|
if (!sameIdSet(existing.map((record2) => record2.skillDefinitionId), requestedIds)) {
|
|
@@ -4851,7 +4875,7 @@ __export(exports_steering_repository, {
|
|
|
4851
4875
|
SteeringRepositoryError: () => SteeringRepositoryError,
|
|
4852
4876
|
Service: () => Service19
|
|
4853
4877
|
});
|
|
4854
|
-
import { asc as asc10, eq as
|
|
4878
|
+
import { asc as asc10, eq as eq15, inArray as inArray4, isNull, max as max2, sql as sql11 } from "drizzle-orm";
|
|
4855
4879
|
import { Context as Context19, Effect as Effect19, Layer as Layer19, Ref as Ref3, Schema as Schema29 } from "effect";
|
|
4856
4880
|
class SteeringRepositoryError extends Schema29.TaggedErrorClass()("SteeringRepositoryError", {
|
|
4857
4881
|
message: Schema29.String
|
|
@@ -4861,7 +4885,7 @@ class SteeringRepositoryError extends Schema29.TaggedErrorClass()("SteeringRepos
|
|
|
4861
4885
|
class Service19 extends Context19.Service()("@relayfx/store-sql/SteeringRepository") {
|
|
4862
4886
|
}
|
|
4863
4887
|
var SteeringKindSchema = Schema29.Literals(["steering", "follow_up"]);
|
|
4864
|
-
var
|
|
4888
|
+
var mapDatabaseError14 = (effect) => effect.pipe(Effect19.mapError((error) => new SteeringRepositoryError({ message: String(error) })));
|
|
4865
4889
|
var mapTransactionError = (effect) => effect.pipe(Effect19.mapError((error) => error instanceof SteeringRepositoryError ? error : new SteeringRepositoryError({ message: String(error) })));
|
|
4866
4890
|
var toMessageRecord = (row) => {
|
|
4867
4891
|
const consumedAt = fromNullablePgDate(row.consumedAt);
|
|
@@ -4902,21 +4926,21 @@ var consumedMessage = (message, input) => ({
|
|
|
4902
4926
|
var messagesForDrain = (db, tenantId2, drain) => Effect19.gen(function* () {
|
|
4903
4927
|
if (drain.messageSequences.length === 0)
|
|
4904
4928
|
return { drain, messages: [] };
|
|
4905
|
-
const rows = yield*
|
|
4929
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq15(relaySteeringMessages.executionId, drain.executionId), eq15(relaySteeringMessages.kind, drain.kind), inArray4(relaySteeringMessages.sequence, [...drain.messageSequences]))).orderBy(asc10(relaySteeringMessages.sequence)));
|
|
4906
4930
|
return { drain, messages: rows.map(toMessageRecord) };
|
|
4907
4931
|
});
|
|
4908
4932
|
var findDrain = (db, tenantId2, input) => Effect19.gen(function* () {
|
|
4909
|
-
const rows = yield*
|
|
4933
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySteeringDrains).where(predicate(tenantId2, relaySteeringDrains.tenantId, eq15(relaySteeringDrains.executionId, input.executionId), eq15(relaySteeringDrains.kind, input.kind), eq15(relaySteeringDrains.drainId, input.drainId))).limit(1));
|
|
4910
4934
|
const row = rows[0];
|
|
4911
4935
|
return row === undefined ? undefined : toDrainRecord(row);
|
|
4912
4936
|
});
|
|
4913
|
-
var lockQueue = (db, tenantId2, executionId, kind) =>
|
|
4914
|
-
var runningExecutionStatus = (db, tenantId2, executionId) => db.select({ status: relayExecutions.status }).from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId,
|
|
4937
|
+
var lockQueue = (db, tenantId2, executionId, kind) => mapDatabaseError14(db.execute(sql11`select pg_advisory_xact_lock(hashtext(${queueKey(tenantId2, executionId, kind)}))`)).pipe(Effect19.asVoid);
|
|
4938
|
+
var runningExecutionStatus = (db, tenantId2, executionId) => db.select({ status: relayExecutions.status }).from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId, eq15(relayExecutions.id, executionId))).for("update").limit(1).pipe(Effect19.map((rows) => rows[0]?.status));
|
|
4915
4939
|
var layer19 = Layer19.effect(Service19, Effect19.gen(function* () {
|
|
4916
4940
|
const db = yield* Service;
|
|
4917
4941
|
const appendInTransaction = Effect19.fn("SteeringRepository.appendInTransaction")(function* (tx, tenantId2, input) {
|
|
4918
4942
|
yield* lockQueue(tx, tenantId2, input.executionId, input.kind);
|
|
4919
|
-
const sequenceRows = yield* tx.select({ value: max2(relaySteeringMessages.sequence) }).from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId,
|
|
4943
|
+
const sequenceRows = yield* tx.select({ value: max2(relaySteeringMessages.sequence) }).from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq15(relaySteeringMessages.executionId, input.executionId), eq15(relaySteeringMessages.kind, input.kind)));
|
|
4920
4944
|
const sequence = Number(sequenceRows[0]?.value ?? -1) + 1;
|
|
4921
4945
|
const rows = yield* tx.insert(relaySteeringMessages).values({
|
|
4922
4946
|
tenantId: tenantId2,
|
|
@@ -4961,13 +4985,13 @@ var layer19 = Layer19.effect(Service19, Effect19.gen(function* () {
|
|
|
4961
4985
|
const existingAfterLock = yield* findDrain(scopedTx, tenantId2, input);
|
|
4962
4986
|
if (existingAfterLock !== undefined)
|
|
4963
4987
|
return yield* messagesForDrain(scopedTx, tenantId2, existingAfterLock);
|
|
4964
|
-
const query2 = tx.select().from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId,
|
|
4988
|
+
const query2 = tx.select().from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq15(relaySteeringMessages.executionId, input.executionId), eq15(relaySteeringMessages.kind, input.kind), isNull(relaySteeringMessages.consumedAt))).orderBy(asc10(relaySteeringMessages.sequence));
|
|
4965
4989
|
const rows = input.mode === "one-at-a-time" ? yield* query2.limit(1) : yield* query2;
|
|
4966
4990
|
const sequences = rows.map((row) => row.sequence);
|
|
4967
4991
|
const messages = sequences.length === 0 ? [] : yield* tx.update(relaySteeringMessages).set({
|
|
4968
4992
|
drainId: input.drainId,
|
|
4969
4993
|
consumedAt: toPgDate(input.consumedAt)
|
|
4970
|
-
}).where(predicate(tenantId2, relaySteeringMessages.tenantId,
|
|
4994
|
+
}).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq15(relaySteeringMessages.executionId, input.executionId), eq15(relaySteeringMessages.kind, input.kind), inArray4(relaySteeringMessages.sequence, sequences))).returning();
|
|
4971
4995
|
const messageBySequence = new Map(messages.map((message) => [message.sequence, message]));
|
|
4972
4996
|
const orderedMessages = sequences.flatMap((sequence) => {
|
|
4973
4997
|
const message = messageBySequence.get(sequence);
|
|
@@ -5087,7 +5111,7 @@ __export(exports_tool_call_repository, {
|
|
|
5087
5111
|
DuplicateToolResult: () => DuplicateToolResult,
|
|
5088
5112
|
DuplicateToolCall: () => DuplicateToolCall
|
|
5089
5113
|
});
|
|
5090
|
-
import { asc as asc11, eq as
|
|
5114
|
+
import { asc as asc11, eq as eq16 } from "drizzle-orm";
|
|
5091
5115
|
import { Context as Context20, Effect as Effect20, Layer as Layer20, Schema as Schema30 } from "effect";
|
|
5092
5116
|
class ToolCallRepositoryError extends Schema30.TaggedErrorClass()("ToolCallRepositoryError", {
|
|
5093
5117
|
message: Schema30.String
|
|
@@ -5148,15 +5172,15 @@ var toResultRecord = (row) => ({
|
|
|
5148
5172
|
},
|
|
5149
5173
|
createdAt: fromPgDate(row.createdAt)
|
|
5150
5174
|
});
|
|
5151
|
-
var
|
|
5175
|
+
var mapDatabaseError15 = (effect) => effect.pipe(Effect20.mapError((error) => new ToolCallRepositoryError({ message: String(error) })));
|
|
5152
5176
|
var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
5153
5177
|
const db = yield* Service;
|
|
5154
5178
|
const recordCall = Effect20.fn("ToolCallRepository.recordCall")(function* (input) {
|
|
5155
5179
|
const tenantId2 = yield* current;
|
|
5156
|
-
const existingRows = yield*
|
|
5180
|
+
const existingRows = yield* mapDatabaseError15(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq16(relayToolCalls.id, input.call.id))).limit(1));
|
|
5157
5181
|
if (existingRows[0] !== undefined)
|
|
5158
5182
|
return yield* Effect20.fail(new DuplicateToolCall({ id: input.call.id }));
|
|
5159
|
-
const rows = yield*
|
|
5183
|
+
const rows = yield* mapDatabaseError15(db.insert(relayToolCalls).values(toCallInsert(tenantId2, input)).returning());
|
|
5160
5184
|
const row = rows[0];
|
|
5161
5185
|
if (row === undefined) {
|
|
5162
5186
|
return yield* Effect20.fail(new ToolCallRepositoryError({ message: "Tool call insert returned no row" }));
|
|
@@ -5165,24 +5189,24 @@ var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
|
5165
5189
|
});
|
|
5166
5190
|
const getCall = Effect20.fn("ToolCallRepository.getCall")(function* (id2) {
|
|
5167
5191
|
const tenantId2 = yield* current;
|
|
5168
|
-
const rows = yield*
|
|
5192
|
+
const rows = yield* mapDatabaseError15(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq16(relayToolCalls.id, id2))).limit(1));
|
|
5169
5193
|
return rows[0] === undefined ? undefined : toCallRecord(rows[0]);
|
|
5170
5194
|
});
|
|
5171
5195
|
const listCalls = Effect20.fn("ToolCallRepository.listCalls")(function* (executionId) {
|
|
5172
5196
|
const tenantId2 = yield* current;
|
|
5173
|
-
const rows = yield*
|
|
5197
|
+
const rows = yield* mapDatabaseError15(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq16(relayToolCalls.executionId, executionId))).orderBy(asc11(relayToolCalls.createdAt)));
|
|
5174
5198
|
return rows.map(toCallRecord);
|
|
5175
5199
|
});
|
|
5176
5200
|
const recordResult = Effect20.fn("ToolCallRepository.recordResult")(function* (input) {
|
|
5177
5201
|
const tenantId2 = yield* current;
|
|
5178
|
-
const callRows = yield*
|
|
5202
|
+
const callRows = yield* mapDatabaseError15(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq16(relayToolCalls.id, input.result.call_id))).limit(1));
|
|
5179
5203
|
if (callRows[0] === undefined)
|
|
5180
5204
|
return yield* Effect20.fail(new ToolCallNotFound({ id: input.result.call_id }));
|
|
5181
|
-
const existingRows = yield*
|
|
5205
|
+
const existingRows = yield* mapDatabaseError15(db.select().from(relayToolResults).where(predicate(tenantId2, relayToolResults.tenantId, eq16(relayToolResults.toolCallId, input.result.call_id))).limit(1));
|
|
5182
5206
|
if (existingRows[0] !== undefined) {
|
|
5183
5207
|
return yield* Effect20.fail(new DuplicateToolResult({ call_id: input.result.call_id }));
|
|
5184
5208
|
}
|
|
5185
|
-
const rows = yield*
|
|
5209
|
+
const rows = yield* mapDatabaseError15(db.insert(relayToolResults).values(toResultInsert(tenantId2, input)).returning());
|
|
5186
5210
|
const row = rows[0];
|
|
5187
5211
|
if (row === undefined) {
|
|
5188
5212
|
return yield* Effect20.fail(new ToolCallRepositoryError({ message: "Tool result insert returned no row" }));
|
|
@@ -5191,7 +5215,7 @@ var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
|
5191
5215
|
});
|
|
5192
5216
|
const getResult = Effect20.fn("ToolCallRepository.getResult")(function* (callId) {
|
|
5193
5217
|
const tenantId2 = yield* current;
|
|
5194
|
-
const rows = yield*
|
|
5218
|
+
const rows = yield* mapDatabaseError15(db.select().from(relayToolResults).where(predicate(tenantId2, relayToolResults.tenantId, eq16(relayToolResults.toolCallId, callId))).limit(1));
|
|
5195
5219
|
return rows[0] === undefined ? undefined : toResultRecord(rows[0]);
|
|
5196
5220
|
});
|
|
5197
5221
|
return Service20.of({ recordCall, getCall, listCalls, recordResult, getResult });
|
|
@@ -5272,7 +5296,7 @@ __export(exports_workspace_lease_repository, {
|
|
|
5272
5296
|
Service: () => Service21,
|
|
5273
5297
|
DuplicateWorkspaceLease: () => DuplicateWorkspaceLease
|
|
5274
5298
|
});
|
|
5275
|
-
import { eq as
|
|
5299
|
+
import { eq as eq17, notInArray } from "drizzle-orm";
|
|
5276
5300
|
import { Context as Context21, Effect as Effect21, Layer as Layer21, Schema as Schema31 } from "effect";
|
|
5277
5301
|
class WorkspaceLeaseRepositoryError extends Schema31.TaggedErrorClass()("WorkspaceLeaseRepositoryError", {
|
|
5278
5302
|
message: Schema31.String
|
|
@@ -5342,8 +5366,8 @@ var toSnapshotRecord = (row) => ({
|
|
|
5342
5366
|
metadata: row.metadataJson,
|
|
5343
5367
|
created_at: fromPgDate(row.createdAt)
|
|
5344
5368
|
});
|
|
5345
|
-
var
|
|
5346
|
-
var updateLease = (db, tenantId2, executionId, values) =>
|
|
5369
|
+
var mapDatabaseError16 = (effect) => effect.pipe(Effect21.mapError((error) => new WorkspaceLeaseRepositoryError({ message: String(error) })));
|
|
5370
|
+
var updateLease = (db, tenantId2, executionId, values) => mapDatabaseError16(db.update(relayWorkspaceLeases).set(values).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq17(relayWorkspaceLeases.executionId, executionId))).returning()).pipe(Effect21.flatMap((rows) => {
|
|
5347
5371
|
const row = rows[0];
|
|
5348
5372
|
if (row === undefined)
|
|
5349
5373
|
return Effect21.fail(new WorkspaceLeaseNotFound({ execution_id: executionId }));
|
|
@@ -5353,16 +5377,16 @@ var layer21 = Layer21.effect(Service21, Effect21.gen(function* () {
|
|
|
5353
5377
|
const db = yield* Service;
|
|
5354
5378
|
const getByExecution = Effect21.fn("WorkspaceLeaseRepository.getByExecution")(function* (id2) {
|
|
5355
5379
|
const tenantId2 = yield* current;
|
|
5356
|
-
const rows = yield*
|
|
5380
|
+
const rows = yield* mapDatabaseError16(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq17(relayWorkspaceLeases.executionId, id2))).limit(1));
|
|
5357
5381
|
return rows[0] === undefined ? undefined : toLeaseRecord(rows[0]);
|
|
5358
5382
|
});
|
|
5359
5383
|
const plan = Effect21.fn("WorkspaceLeaseRepository.plan")(function* (input) {
|
|
5360
5384
|
const tenantId2 = yield* current;
|
|
5361
|
-
const existing = yield*
|
|
5385
|
+
const existing = yield* mapDatabaseError16(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq17(relayWorkspaceLeases.executionId, input.executionId), notInArray(relayWorkspaceLeases.status, [...terminalStatuses]))).limit(1));
|
|
5362
5386
|
if (existing[0] !== undefined) {
|
|
5363
5387
|
return yield* Effect21.fail(new DuplicateWorkspaceLease({ execution_id: input.executionId }));
|
|
5364
5388
|
}
|
|
5365
|
-
const rows = yield*
|
|
5389
|
+
const rows = yield* mapDatabaseError16(db.insert(relayWorkspaceLeases).values(toPlanInsert(tenantId2, input)).returning());
|
|
5366
5390
|
const row = rows[0];
|
|
5367
5391
|
if (row === undefined) {
|
|
5368
5392
|
return yield* Effect21.fail(new WorkspaceLeaseRepositoryError({ message: "Workspace lease insert returned no row" }));
|
|
@@ -5394,11 +5418,11 @@ var layer21 = Layer21.effect(Service21, Effect21.gen(function* () {
|
|
|
5394
5418
|
});
|
|
5395
5419
|
const recordSnapshot = Effect21.fn("WorkspaceLeaseRepository.recordSnapshot")(function* (input) {
|
|
5396
5420
|
const tenantId2 = yield* current;
|
|
5397
|
-
const leaseRows = yield*
|
|
5421
|
+
const leaseRows = yield* mapDatabaseError16(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq17(relayWorkspaceLeases.executionId, input.executionId))).limit(1));
|
|
5398
5422
|
if (leaseRows[0] === undefined) {
|
|
5399
5423
|
return yield* Effect21.fail(new WorkspaceLeaseNotFound({ execution_id: input.executionId }));
|
|
5400
5424
|
}
|
|
5401
|
-
const rows = yield*
|
|
5425
|
+
const rows = yield* mapDatabaseError16(db.insert(relayWorkspaceSnapshots).values(toSnapshotInsert(tenantId2, input)).returning());
|
|
5402
5426
|
const row = rows[0];
|
|
5403
5427
|
if (row === undefined) {
|
|
5404
5428
|
return yield* Effect21.fail(new WorkspaceLeaseRepositoryError({ message: "Workspace snapshot insert returned no row" }));
|