@relayfx/sdk 0.0.9 → 0.0.11
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 +144 -158
- package/package.json +1 -1
package/dist/index.js
CHANGED
|
@@ -2140,7 +2140,7 @@ __export(exports_envelope_repository, {
|
|
|
2140
2140
|
EnvelopeReadyClaimMismatch: () => EnvelopeReadyClaimMismatch,
|
|
2141
2141
|
EnvelopeNotFound: () => EnvelopeNotFound
|
|
2142
2142
|
});
|
|
2143
|
-
import { and as and2, asc as asc5, desc as desc2, eq as eq7, lte, or, sql as sql5 } from "drizzle-orm";
|
|
2143
|
+
import { and as and2, asc as asc5, desc as desc2, eq as eq7, inArray, lte, or, sql as sql5 } from "drizzle-orm";
|
|
2144
2144
|
import { Context as Context9, Effect as Effect9, Layer as Layer9, Schema as Schema18 } from "effect";
|
|
2145
2145
|
var WaitState = Schema18.Literals(["open", "resolved", "timed_out", "cancelled"]).annotate({
|
|
2146
2146
|
identifier: "Relay.Store.WaitState"
|
|
@@ -2326,10 +2326,7 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2326
2326
|
});
|
|
2327
2327
|
const claimReady = Effect9.fn("EnvelopeRepository.claimReady")(function* (input) {
|
|
2328
2328
|
const tenantId2 = yield* current;
|
|
2329
|
-
const
|
|
2330
|
-
const candidate = candidates[0];
|
|
2331
|
-
if (candidate === undefined)
|
|
2332
|
-
return;
|
|
2329
|
+
const candidate = db.select({ id: relayEnvelopeReady.id }).from(relayEnvelopeReady).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq7(relayEnvelopeReady.routeType, input.routeType), input.routeKey === undefined ? undefined : eq7(relayEnvelopeReady.routeKey, input.routeKey), or(and2(eq7(relayEnvelopeReady.state, "ready"), lte(relayEnvelopeReady.availableAt, toPgDate(input.now))), and2(eq7(relayEnvelopeReady.state, "claimed"), lte(relayEnvelopeReady.claimExpiresAt, toPgDate(input.now)))))).orderBy(asc5(relayEnvelopeReady.availableAt), asc5(relayEnvelopeReady.createdAt)).limit(1).for("update", { skipLocked: true });
|
|
2333
2330
|
const rows = yield* mapDatabaseError6(db.update(relayEnvelopeReady).set({
|
|
2334
2331
|
state: "claimed",
|
|
2335
2332
|
claimOwner: input.workerId,
|
|
@@ -2337,19 +2334,11 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2337
2334
|
claimedAt: toPgDate(input.now),
|
|
2338
2335
|
updatedAt: toPgDate(input.now),
|
|
2339
2336
|
attempt: sql5`${relayEnvelopeReady.attempt} + 1`
|
|
2340
|
-
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId,
|
|
2337
|
+
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, inArray(relayEnvelopeReady.id, candidate))).returning());
|
|
2341
2338
|
return rows[0] === undefined ? undefined : toReadyRecord(rows[0]);
|
|
2342
2339
|
});
|
|
2343
2340
|
const releaseReady = Effect9.fn("EnvelopeRepository.releaseReady")(function* (input) {
|
|
2344
2341
|
const tenantId2 = yield* current;
|
|
2345
|
-
const current2 = yield* getReadyRow(db, tenantId2, input.id);
|
|
2346
|
-
if (current2 === undefined)
|
|
2347
|
-
return yield* Effect9.fail(new EnvelopeReadyNotFound({ id: input.id }));
|
|
2348
|
-
if (current2.state === "acknowledged")
|
|
2349
|
-
return toReadyRecord(current2);
|
|
2350
|
-
if (current2.claimOwner !== input.workerId) {
|
|
2351
|
-
return yield* Effect9.fail(new EnvelopeReadyClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
2352
|
-
}
|
|
2353
2342
|
const rows = yield* mapDatabaseError6(db.update(relayEnvelopeReady).set({
|
|
2354
2343
|
state: "ready",
|
|
2355
2344
|
availableAt: toPgDate(input.nextAvailableAt),
|
|
@@ -2357,31 +2346,33 @@ var layer9 = Layer9.effect(Service9, Effect9.gen(function* () {
|
|
|
2357
2346
|
claimExpiresAt: null,
|
|
2358
2347
|
lastError: input.error ?? null,
|
|
2359
2348
|
updatedAt: toPgDate(input.nextAvailableAt)
|
|
2360
|
-
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq7(relayEnvelopeReady.id, input.id))).returning());
|
|
2349
|
+
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq7(relayEnvelopeReady.id, input.id), eq7(relayEnvelopeReady.claimOwner, input.workerId), eq7(relayEnvelopeReady.state, "claimed"))).returning());
|
|
2361
2350
|
const row = rows[0];
|
|
2362
|
-
if (row
|
|
2363
|
-
return
|
|
2364
|
-
return toReadyRecord(row);
|
|
2365
|
-
});
|
|
2366
|
-
const ackReady = Effect9.fn("EnvelopeRepository.ackReady")(function* (input) {
|
|
2367
|
-
const tenantId2 = yield* current;
|
|
2351
|
+
if (row !== undefined)
|
|
2352
|
+
return toReadyRecord(row);
|
|
2368
2353
|
const current2 = yield* getReadyRow(db, tenantId2, input.id);
|
|
2369
2354
|
if (current2 === undefined)
|
|
2370
2355
|
return yield* Effect9.fail(new EnvelopeReadyNotFound({ id: input.id }));
|
|
2371
|
-
if (current2.state === "acknowledged")
|
|
2356
|
+
if (current2.state === "acknowledged" && current2.claimOwner === input.workerId)
|
|
2372
2357
|
return toReadyRecord(current2);
|
|
2373
|
-
|
|
2374
|
-
|
|
2375
|
-
|
|
2358
|
+
return yield* Effect9.fail(new EnvelopeReadyClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
2359
|
+
});
|
|
2360
|
+
const ackReady = Effect9.fn("EnvelopeRepository.ackReady")(function* (input) {
|
|
2361
|
+
const tenantId2 = yield* current;
|
|
2376
2362
|
const rows = yield* mapDatabaseError6(db.update(relayEnvelopeReady).set({
|
|
2377
2363
|
state: "acknowledged",
|
|
2378
2364
|
acknowledgedAt: toPgDate(input.now),
|
|
2379
2365
|
updatedAt: toPgDate(input.now)
|
|
2380
|
-
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq7(relayEnvelopeReady.id, input.id))).returning());
|
|
2366
|
+
}).where(predicate(tenantId2, relayEnvelopeReady.tenantId, eq7(relayEnvelopeReady.id, input.id), eq7(relayEnvelopeReady.claimOwner, input.workerId), eq7(relayEnvelopeReady.state, "claimed"))).returning());
|
|
2381
2367
|
const row = rows[0];
|
|
2382
|
-
if (row
|
|
2368
|
+
if (row !== undefined)
|
|
2369
|
+
return toReadyRecord(row);
|
|
2370
|
+
const current2 = yield* getReadyRow(db, tenantId2, input.id);
|
|
2371
|
+
if (current2 === undefined)
|
|
2383
2372
|
return yield* Effect9.fail(new EnvelopeReadyNotFound({ id: input.id }));
|
|
2384
|
-
|
|
2373
|
+
if (current2.state === "acknowledged" && current2.claimOwner === input.workerId)
|
|
2374
|
+
return toReadyRecord(current2);
|
|
2375
|
+
return yield* Effect9.fail(new EnvelopeReadyClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
2385
2376
|
});
|
|
2386
2377
|
const completeWait = Effect9.fn("EnvelopeRepository.completeWait")(function* (input) {
|
|
2387
2378
|
const tenantId2 = yield* current;
|
|
@@ -2525,9 +2516,9 @@ var memoryLayer7 = Layer9.sync(Service9, () => {
|
|
|
2525
2516
|
const current2 = getReady(input.id);
|
|
2526
2517
|
if (current2 === undefined)
|
|
2527
2518
|
return yield* Effect9.fail(new EnvelopeReadyNotFound({ id: input.id }));
|
|
2528
|
-
if (current2.state === "acknowledged")
|
|
2519
|
+
if (current2.state === "acknowledged" && current2.claimOwner === input.workerId)
|
|
2529
2520
|
return current2;
|
|
2530
|
-
if (current2.claimOwner !== input.workerId) {
|
|
2521
|
+
if (current2.state !== "claimed" || current2.claimOwner !== input.workerId) {
|
|
2531
2522
|
return yield* Effect9.fail(new EnvelopeReadyClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
2532
2523
|
}
|
|
2533
2524
|
const released = {
|
|
@@ -2547,9 +2538,9 @@ var memoryLayer7 = Layer9.sync(Service9, () => {
|
|
|
2547
2538
|
const current2 = getReady(input.id);
|
|
2548
2539
|
if (current2 === undefined)
|
|
2549
2540
|
return yield* Effect9.fail(new EnvelopeReadyNotFound({ id: input.id }));
|
|
2550
|
-
if (current2.state === "acknowledged")
|
|
2541
|
+
if (current2.state === "acknowledged" && current2.claimOwner === input.workerId)
|
|
2551
2542
|
return current2;
|
|
2552
|
-
if (current2.claimOwner !== input.workerId) {
|
|
2543
|
+
if (current2.state !== "claimed" || current2.claimOwner !== input.workerId) {
|
|
2553
2544
|
return yield* Effect9.fail(new EnvelopeReadyClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
2554
2545
|
}
|
|
2555
2546
|
const acknowledged = {
|
|
@@ -3156,8 +3147,8 @@ var transition = Effect11.fn("ExecutionRepository.transition.call")(function* (i
|
|
|
3156
3147
|
return yield* repository.transition(input);
|
|
3157
3148
|
});
|
|
3158
3149
|
// ../store-sql/src/idempotency/idempotency-repository.ts
|
|
3159
|
-
import { eq as eq10 } from "drizzle-orm";
|
|
3160
|
-
import { Context as Context12, Effect as Effect12, HashMap, Layer as Layer12, Option as Option3, Ref, Schema as Schema21 } from "effect";
|
|
3150
|
+
import { eq as eq10, sql as sql7 } from "drizzle-orm";
|
|
3151
|
+
import { Context as Context12, Effect as Effect12, HashMap, Layer as Layer12, Option as Option3, Ref, Schema as Schema21, Semaphore } from "effect";
|
|
3161
3152
|
class IdempotencyRepositoryError extends Schema21.TaggedErrorClass()("IdempotencyRepositoryError", {
|
|
3162
3153
|
message: Schema21.String
|
|
3163
3154
|
}) {
|
|
@@ -3191,29 +3182,36 @@ var toInsert3 = (tenantId2, input, result) => ({
|
|
|
3191
3182
|
updatedAt: toPgDate(input.createdAt),
|
|
3192
3183
|
...input.expiresAt === undefined ? {} : { expiresAt: toPgDate(input.expiresAt) }
|
|
3193
3184
|
});
|
|
3194
|
-
var mapDatabaseError9 = (effect) => effect.pipe(Effect12.mapError((error) => new IdempotencyRepositoryError({ message: String(error) })));
|
|
3195
3185
|
var layer12 = Layer12.effect(Service12, Effect12.gen(function* () {
|
|
3196
3186
|
const db = yield* Service;
|
|
3197
3187
|
const runOnce = (input, create3) => Effect12.gen(function* () {
|
|
3198
3188
|
const tenantId2 = yield* current;
|
|
3199
|
-
|
|
3200
|
-
|
|
3201
|
-
|
|
3202
|
-
|
|
3203
|
-
|
|
3204
|
-
|
|
3205
|
-
|
|
3206
|
-
|
|
3207
|
-
|
|
3208
|
-
|
|
3209
|
-
|
|
3189
|
+
return yield* db.transaction((tx) => Effect12.gen(function* () {
|
|
3190
|
+
yield* tx.execute(sql7`select pg_advisory_xact_lock(hashtextextended(${`${tenantId2}:${operationId(input)}`}, 0))`);
|
|
3191
|
+
const existingRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId, eq10(relayIdempotencyKeys.scope, input.scope), eq10(relayIdempotencyKeys.operation, input.operation), eq10(relayIdempotencyKeys.key, input.key))).limit(1);
|
|
3192
|
+
const existing = existingRows[0];
|
|
3193
|
+
if (existing !== undefined)
|
|
3194
|
+
return { replayed: true, record: toRecord6(existing) };
|
|
3195
|
+
const result = yield* create3;
|
|
3196
|
+
const rows = yield* tx.insert(relayIdempotencyKeys).values(toInsert3(tenantId2, input, result)).onConflictDoNothing().returning();
|
|
3197
|
+
const row = rows[0];
|
|
3198
|
+
if (row !== undefined)
|
|
3199
|
+
return { replayed: false, record: toRecord6(row) };
|
|
3200
|
+
const winnerRows = yield* tx.select().from(relayIdempotencyKeys).where(predicate(tenantId2, relayIdempotencyKeys.tenantId, eq10(relayIdempotencyKeys.scope, input.scope), eq10(relayIdempotencyKeys.operation, input.operation), eq10(relayIdempotencyKeys.key, input.key))).limit(1);
|
|
3201
|
+
const winner = winnerRows[0];
|
|
3202
|
+
if (winner === undefined) {
|
|
3203
|
+
return yield* Effect12.fail(new IdempotencyRepositoryError({ message: "Idempotency insert returned no row" }));
|
|
3204
|
+
}
|
|
3205
|
+
return { replayed: true, record: toRecord6(winner) };
|
|
3206
|
+
})).pipe(Effect12.mapError((error) => error instanceof IdempotencyRepositoryError ? error : new IdempotencyRepositoryError({ message: String(error) })));
|
|
3210
3207
|
});
|
|
3211
3208
|
const transaction = (effect) => db.transaction((tx) => effect.pipe(Effect12.provideService(Service, tx))).pipe(Effect12.mapError((error) => new IdempotencyRepositoryError({ message: String(error) })));
|
|
3212
3209
|
return Service12.of({ runOnce, transaction });
|
|
3213
3210
|
}));
|
|
3214
3211
|
var memoryLayer10 = Layer12.effect(Service12, Effect12.gen(function* () {
|
|
3215
3212
|
const records = yield* Ref.make(HashMap.empty());
|
|
3216
|
-
const
|
|
3213
|
+
const semaphore = Semaphore.makeUnsafe(1);
|
|
3214
|
+
const runOnce = (input, create3) => semaphore.withPermits(1)(Effect12.gen(function* () {
|
|
3217
3215
|
const id2 = operationId(input);
|
|
3218
3216
|
const store = yield* Ref.get(records);
|
|
3219
3217
|
const existing = HashMap.get(store, id2).pipe(Option3.getOrUndefined);
|
|
@@ -3231,7 +3229,7 @@ var memoryLayer10 = Layer12.effect(Service12, Effect12.gen(function* () {
|
|
|
3231
3229
|
};
|
|
3232
3230
|
yield* Ref.update(records, (current2) => HashMap.set(current2, id2, record2));
|
|
3233
3231
|
return { replayed: false, record: record2 };
|
|
3234
|
-
});
|
|
3232
|
+
}));
|
|
3235
3233
|
const transaction = (effect) => Effect12.gen(function* () {
|
|
3236
3234
|
const snapshot = yield* Ref.get(records);
|
|
3237
3235
|
return yield* effect.pipe(Effect12.catch((error) => Ref.set(records, snapshot).pipe(Effect12.flatMap(() => Effect12.fail(error)))));
|
|
@@ -3249,7 +3247,7 @@ __export(exports_memory_repository, {
|
|
|
3249
3247
|
Service: () => Service13,
|
|
3250
3248
|
MemoryRepositoryError: () => MemoryRepositoryError
|
|
3251
3249
|
});
|
|
3252
|
-
import { sql as
|
|
3250
|
+
import { sql as sql8 } from "drizzle-orm";
|
|
3253
3251
|
import { Context as Context13, Effect as Effect13, Layer as Layer13, Ref as Ref2, Schema as Schema22 } from "effect";
|
|
3254
3252
|
class MemoryRepositoryError extends Schema22.TaggedErrorClass()("MemoryRepositoryError", {
|
|
3255
3253
|
message: Schema22.String
|
|
@@ -3276,7 +3274,7 @@ var queryRowToRecord = (row) => ({
|
|
|
3276
3274
|
metadata: row.metadataJson,
|
|
3277
3275
|
createdAt: fromPgDate(row.createdAt)
|
|
3278
3276
|
});
|
|
3279
|
-
var
|
|
3277
|
+
var mapDatabaseError9 = (effect) => effect.pipe(Effect13.mapError((error) => new MemoryRepositoryError({ message: String(error) })));
|
|
3280
3278
|
var parsePgFloatArray = (value) => {
|
|
3281
3279
|
const body = value.startsWith("{") && value.endsWith("}") ? value.slice(1, -1) : value;
|
|
3282
3280
|
if (body.length === 0)
|
|
@@ -3320,7 +3318,7 @@ var layer13 = Layer13.effect(Service13, Effect13.gen(function* () {
|
|
|
3320
3318
|
const upsert2 = Effect13.fn("MemoryRepository.upsert")(function* (input) {
|
|
3321
3319
|
const tenantId2 = yield* current;
|
|
3322
3320
|
yield* validateVector("embedding", input.embedding);
|
|
3323
|
-
const rows = yield*
|
|
3321
|
+
const rows = yield* mapDatabaseError9(db.insert(relayMemoryRecords).values({
|
|
3324
3322
|
tenantId: tenantId2,
|
|
3325
3323
|
id: input.id,
|
|
3326
3324
|
agent: input.agent,
|
|
@@ -3350,8 +3348,8 @@ var layer13 = Layer13.effect(Service13, Effect13.gen(function* () {
|
|
|
3350
3348
|
yield* validateVector("query embedding", input.embedding);
|
|
3351
3349
|
if (input.topK <= 0)
|
|
3352
3350
|
return [];
|
|
3353
|
-
const queryEmbeddingSql =
|
|
3354
|
-
const rows = yield*
|
|
3351
|
+
const queryEmbeddingSql = sql8`array[${sql8.join(input.embedding.map((value) => sql8`${value}`), sql8`, `)}]::double precision[]`;
|
|
3352
|
+
const rows = yield* mapDatabaseError9(db.execute(sql8`
|
|
3355
3353
|
with query_embedding as (
|
|
3356
3354
|
select ${queryEmbeddingSql} as embedding
|
|
3357
3355
|
),
|
|
@@ -3506,13 +3504,13 @@ var toRecord8 = (row) => ({
|
|
|
3506
3504
|
rule: row.ruleJson,
|
|
3507
3505
|
createdAt: fromPgDate(row.createdAt)
|
|
3508
3506
|
});
|
|
3509
|
-
var
|
|
3507
|
+
var mapDatabaseError10 = (effect) => effect.pipe(Effect14.mapError((error) => new PermissionRuleRepositoryError({ message: String(error) })));
|
|
3510
3508
|
var memoryKey = (agent, scope, pattern) => `${agent}\x00${scope}\x00${pattern}`;
|
|
3511
3509
|
var layer14 = Layer14.effect(Service14, Effect14.gen(function* () {
|
|
3512
3510
|
const db = yield* Service;
|
|
3513
3511
|
const remember = Effect14.fn("PermissionRuleRepository.remember")(function* (input) {
|
|
3514
3512
|
const tenantId2 = yield* current;
|
|
3515
|
-
const rows = yield*
|
|
3513
|
+
const rows = yield* mapDatabaseError10(db.insert(relayPermissionRules).values({
|
|
3516
3514
|
tenantId: tenantId2,
|
|
3517
3515
|
agent: input.agent,
|
|
3518
3516
|
scope: input.scope,
|
|
@@ -3539,7 +3537,7 @@ var layer14 = Layer14.effect(Service14, Effect14.gen(function* () {
|
|
|
3539
3537
|
});
|
|
3540
3538
|
const list6 = Effect14.fn("PermissionRuleRepository.list")(function* (input) {
|
|
3541
3539
|
const tenantId2 = yield* current;
|
|
3542
|
-
const rows = yield*
|
|
3540
|
+
const rows = yield* mapDatabaseError10(db.select().from(relayPermissionRules).where(predicate(tenantId2, relayPermissionRules.tenantId, eq11(relayPermissionRules.agent, input.agent), eq11(relayPermissionRules.scope, input.scope))).orderBy(asc7(relayPermissionRules.createdAt), asc7(relayPermissionRules.pattern)));
|
|
3543
3541
|
return rows.map(toRecord8);
|
|
3544
3542
|
});
|
|
3545
3543
|
return Service14.of({ remember, list: list6 });
|
|
@@ -3597,7 +3595,7 @@ var toRecord9 = (row) => ({
|
|
|
3597
3595
|
dynamicSourceIds: row.dynamicSourceIdsJson,
|
|
3598
3596
|
openedAt: fromPgDate(row.openedAt)
|
|
3599
3597
|
});
|
|
3600
|
-
var
|
|
3598
|
+
var mapDatabaseError11 = (effect) => effect.pipe(Effect15.mapError((error) => new ContextEpochRepositoryError({ message: String(error) })));
|
|
3601
3599
|
var sameEpoch = (record2, input) => record2.baseline === input.baseline && JSON.stringify(record2.dynamicSourceIds) === JSON.stringify(input.dynamicSourceIds);
|
|
3602
3600
|
var conflict2 = (executionId) => new ContextEpochRepositoryError({ message: `Context epoch already exists with different payload: ${executionId}` });
|
|
3603
3601
|
var cloneRecord5 = (record2) => structuredClone(record2);
|
|
@@ -3605,12 +3603,12 @@ var layer15 = Layer15.effect(Service15, Effect15.gen(function* () {
|
|
|
3605
3603
|
const db = yield* Service;
|
|
3606
3604
|
const get6 = Effect15.fn("ContextEpochRepository.get")(function* (executionId) {
|
|
3607
3605
|
const tenantId2 = yield* current;
|
|
3608
|
-
const rows = yield*
|
|
3606
|
+
const rows = yield* mapDatabaseError11(db.select().from(relayExecutionContextEpochs).where(predicate(tenantId2, relayExecutionContextEpochs.tenantId, eq12(relayExecutionContextEpochs.executionId, executionId))).limit(1));
|
|
3609
3607
|
return rows[0] === undefined ? undefined : toRecord9(rows[0]);
|
|
3610
3608
|
});
|
|
3611
3609
|
const save2 = Effect15.fn("ContextEpochRepository.save")(function* (input) {
|
|
3612
3610
|
const tenantId2 = yield* current;
|
|
3613
|
-
const rows = yield*
|
|
3611
|
+
const rows = yield* mapDatabaseError11(db.insert(relayExecutionContextEpochs).values({
|
|
3614
3612
|
tenantId: tenantId2,
|
|
3615
3613
|
executionId: input.executionId,
|
|
3616
3614
|
baseline: input.baseline,
|
|
@@ -3684,7 +3682,7 @@ __export(exports_schedule_repository, {
|
|
|
3684
3682
|
ScheduleNotFound: () => ScheduleNotFound,
|
|
3685
3683
|
ScheduleClaimMismatch: () => ScheduleClaimMismatch
|
|
3686
3684
|
});
|
|
3687
|
-
import { and as and4, asc as asc8, eq as eq13, lte as lte2, or as or4, sql as
|
|
3685
|
+
import { and as and4, asc as asc8, eq as eq13, inArray as inArray2, lte as lte2, or as or4, sql as sql9 } from "drizzle-orm";
|
|
3688
3686
|
import { Context as Context16, Effect as Effect16, Layer as Layer16, Schema as Schema25 } from "effect";
|
|
3689
3687
|
class ScheduleRepositoryError extends Schema25.TaggedErrorClass()("ScheduleRepositoryError", {
|
|
3690
3688
|
message: Schema25.String
|
|
@@ -3751,12 +3749,12 @@ var toRecord10 = (row) => ({
|
|
|
3751
3749
|
created_at: fromPgDate(row.createdAt),
|
|
3752
3750
|
updated_at: fromPgDate(row.updatedAt)
|
|
3753
3751
|
});
|
|
3754
|
-
var
|
|
3752
|
+
var mapDatabaseError12 = (effect) => effect.pipe(Effect16.mapError((error) => new ScheduleRepositoryError({ message: String(error) })));
|
|
3755
3753
|
var cancellableStates = new Set(["active", "claimed"]);
|
|
3756
3754
|
var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
3757
3755
|
const db = yield* Service;
|
|
3758
|
-
const getRow = (tenantId2, id2) =>
|
|
3759
|
-
const getByIdempotencyKey = (tenantId2, key2) =>
|
|
3756
|
+
const getRow = (tenantId2, id2) => mapDatabaseError12(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, id2))).limit(1)).pipe(Effect16.map((rows) => rows[0]));
|
|
3757
|
+
const getByIdempotencyKey = (tenantId2, key2) => mapDatabaseError12(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.idempotencyKey, key2))).limit(1)).pipe(Effect16.map((rows) => rows[0]));
|
|
3760
3758
|
const create3 = Effect16.fn("ScheduleRepository.create")(function* (input) {
|
|
3761
3759
|
const tenantId2 = yield* current;
|
|
3762
3760
|
const invalid = validationError(input);
|
|
@@ -3767,7 +3765,7 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3767
3765
|
if (existing !== undefined)
|
|
3768
3766
|
return toRecord10(existing);
|
|
3769
3767
|
}
|
|
3770
|
-
const rows = yield*
|
|
3768
|
+
const rows = yield* mapDatabaseError12(db.insert(relaySchedules).values(toInsert4(tenantId2, input)).returning());
|
|
3771
3769
|
const row = rows[0];
|
|
3772
3770
|
if (row === undefined) {
|
|
3773
3771
|
return yield* Effect16.fail(new ScheduleRepositoryError({ message: `Schedule ${input.id} was not persisted` }));
|
|
@@ -3781,86 +3779,73 @@ var layer16 = Layer16.effect(Service16, Effect16.gen(function* () {
|
|
|
3781
3779
|
});
|
|
3782
3780
|
const list7 = Effect16.fn("ScheduleRepository.list")(function* (input) {
|
|
3783
3781
|
const tenantId2 = yield* current;
|
|
3784
|
-
const rows = yield*
|
|
3782
|
+
const rows = yield* mapDatabaseError12(db.select().from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, input.state === undefined ? undefined : eq13(relaySchedules.state, input.state))).orderBy(asc8(relaySchedules.nextRunAt), asc8(relaySchedules.createdAt)));
|
|
3785
3783
|
return rows.map(toRecord10);
|
|
3786
3784
|
});
|
|
3787
3785
|
const cancel = Effect16.fn("ScheduleRepository.cancel")(function* (input) {
|
|
3788
3786
|
const tenantId2 = yield* current;
|
|
3789
|
-
const
|
|
3790
|
-
if (current2 === undefined)
|
|
3791
|
-
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3792
|
-
if (current2.state === "cancelled")
|
|
3793
|
-
return toRecord10(current2);
|
|
3794
|
-
if (!cancellableStates.has(current2.state)) {
|
|
3795
|
-
return yield* Effect16.fail(new ScheduleRepositoryError({ message: `Schedule ${input.id} cannot be cancelled from ${current2.state}` }));
|
|
3796
|
-
}
|
|
3797
|
-
const rows = yield* mapDatabaseError13(db.update(relaySchedules).set({
|
|
3787
|
+
const rows = yield* mapDatabaseError12(db.update(relaySchedules).set({
|
|
3798
3788
|
state: "cancelled",
|
|
3799
3789
|
claimOwner: null,
|
|
3800
3790
|
claimExpiresAt: null,
|
|
3801
3791
|
updatedAt: toPgDate(input.cancelledAt)
|
|
3802
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id))).returning());
|
|
3792
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id), inArray2(relaySchedules.state, [...cancellableStates]))).returning());
|
|
3803
3793
|
const row = rows[0];
|
|
3804
|
-
if (row
|
|
3794
|
+
if (row !== undefined)
|
|
3795
|
+
return toRecord10(row);
|
|
3796
|
+
const current2 = yield* getRow(tenantId2, input.id);
|
|
3797
|
+
if (current2 === undefined)
|
|
3805
3798
|
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3806
|
-
|
|
3799
|
+
if (current2.state === "cancelled")
|
|
3800
|
+
return toRecord10(current2);
|
|
3801
|
+
return yield* Effect16.fail(new ScheduleRepositoryError({ message: `Schedule ${input.id} cannot be cancelled from ${current2.state}` }));
|
|
3807
3802
|
});
|
|
3808
3803
|
const claimDue = Effect16.fn("ScheduleRepository.claimDue")(function* (input) {
|
|
3809
3804
|
const tenantId2 = yield* current;
|
|
3810
|
-
const
|
|
3811
|
-
const
|
|
3812
|
-
if (candidate === undefined)
|
|
3813
|
-
return;
|
|
3814
|
-
const rows = yield* mapDatabaseError13(db.update(relaySchedules).set({
|
|
3805
|
+
const candidate = db.select({ id: relaySchedules.id }).from(relaySchedules).where(predicate(tenantId2, relaySchedules.tenantId, or4(and4(eq13(relaySchedules.state, "active"), lte2(relaySchedules.nextRunAt, toPgDate(input.now))), and4(eq13(relaySchedules.state, "claimed"), lte2(relaySchedules.claimExpiresAt, toPgDate(input.now)))))).orderBy(asc8(relaySchedules.nextRunAt), asc8(relaySchedules.createdAt)).limit(1).for("update", { skipLocked: true });
|
|
3806
|
+
const rows = yield* mapDatabaseError12(db.update(relaySchedules).set({
|
|
3815
3807
|
state: "claimed",
|
|
3816
3808
|
claimOwner: input.workerId,
|
|
3817
3809
|
claimExpiresAt: toPgDate(input.claimExpiresAt),
|
|
3818
3810
|
updatedAt: toPgDate(input.now),
|
|
3819
|
-
attempt:
|
|
3820
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId,
|
|
3811
|
+
attempt: sql9`${relaySchedules.attempt} + 1`
|
|
3812
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, inArray2(relaySchedules.id, candidate))).returning());
|
|
3821
3813
|
return rows[0] === undefined ? undefined : toRecord10(rows[0]);
|
|
3822
3814
|
});
|
|
3823
3815
|
const ack = Effect16.fn("ScheduleRepository.ack")(function* (input) {
|
|
3824
3816
|
const tenantId2 = yield* current;
|
|
3825
|
-
const
|
|
3826
|
-
|
|
3827
|
-
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3828
|
-
if (current2.state === "completed")
|
|
3829
|
-
return toRecord10(current2);
|
|
3830
|
-
if (current2.claimOwner !== input.workerId) {
|
|
3831
|
-
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3832
|
-
}
|
|
3833
|
-
const rows = yield* mapDatabaseError13(db.update(relaySchedules).set({
|
|
3834
|
-
...input.nextRunAt === undefined ? { state: "completed" } : { state: "active", nextRunAt: toPgDate(input.nextRunAt) },
|
|
3835
|
-
claimOwner: null,
|
|
3817
|
+
const rows = yield* mapDatabaseError12(db.update(relaySchedules).set({
|
|
3818
|
+
...input.nextRunAt === undefined ? { state: "completed" } : { state: "active", nextRunAt: toPgDate(input.nextRunAt), claimOwner: null },
|
|
3836
3819
|
claimExpiresAt: null,
|
|
3837
3820
|
updatedAt: toPgDate(input.now)
|
|
3838
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id))).returning());
|
|
3821
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id), eq13(relaySchedules.claimOwner, input.workerId), eq13(relaySchedules.state, "claimed"))).returning());
|
|
3839
3822
|
const row = rows[0];
|
|
3840
|
-
if (row
|
|
3823
|
+
if (row !== undefined)
|
|
3824
|
+
return toRecord10(row);
|
|
3825
|
+
const current2 = yield* getRow(tenantId2, input.id);
|
|
3826
|
+
if (current2 === undefined)
|
|
3841
3827
|
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3842
|
-
|
|
3828
|
+
if (current2.state === "completed" && current2.claimOwner === input.workerId)
|
|
3829
|
+
return toRecord10(current2);
|
|
3830
|
+
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3843
3831
|
});
|
|
3844
3832
|
const release = Effect16.fn("ScheduleRepository.release")(function* (input) {
|
|
3845
3833
|
const tenantId2 = yield* current;
|
|
3846
|
-
const
|
|
3847
|
-
if (current2 === undefined)
|
|
3848
|
-
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3849
|
-
if (current2.claimOwner !== input.workerId) {
|
|
3850
|
-
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3851
|
-
}
|
|
3852
|
-
const rows = yield* mapDatabaseError13(db.update(relaySchedules).set({
|
|
3834
|
+
const rows = yield* mapDatabaseError12(db.update(relaySchedules).set({
|
|
3853
3835
|
state: "active",
|
|
3854
3836
|
nextRunAt: toPgDate(input.nextAvailableAt),
|
|
3855
3837
|
claimOwner: null,
|
|
3856
3838
|
claimExpiresAt: null,
|
|
3857
3839
|
lastError: input.error,
|
|
3858
3840
|
updatedAt: toPgDate(input.nextAvailableAt)
|
|
3859
|
-
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id))).returning());
|
|
3841
|
+
}).where(predicate(tenantId2, relaySchedules.tenantId, eq13(relaySchedules.id, input.id), eq13(relaySchedules.claimOwner, input.workerId), eq13(relaySchedules.state, "claimed"))).returning());
|
|
3860
3842
|
const row = rows[0];
|
|
3861
|
-
if (row
|
|
3843
|
+
if (row !== undefined)
|
|
3844
|
+
return toRecord10(row);
|
|
3845
|
+
const current2 = yield* getRow(tenantId2, input.id);
|
|
3846
|
+
if (current2 === undefined)
|
|
3862
3847
|
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3863
|
-
return
|
|
3848
|
+
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3864
3849
|
});
|
|
3865
3850
|
return Service16.of({ create: create3, get: get7, list: list7, cancel, claimDue, ack, release });
|
|
3866
3851
|
}));
|
|
@@ -3949,14 +3934,15 @@ var memoryLayer14 = Layer16.sync(Service16, () => {
|
|
|
3949
3934
|
const current2 = schedules.get(input.id);
|
|
3950
3935
|
if (current2 === undefined)
|
|
3951
3936
|
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3952
|
-
if (current2.record.state === "completed")
|
|
3937
|
+
if (current2.record.state === "completed" && current2.claimOwner === input.workerId)
|
|
3953
3938
|
return current2.record;
|
|
3954
|
-
if (current2.claimOwner !== input.workerId) {
|
|
3939
|
+
if (current2.record.state !== "claimed" || current2.claimOwner !== input.workerId) {
|
|
3955
3940
|
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3956
3941
|
}
|
|
3957
3942
|
const acked = input.nextRunAt === undefined ? { ...current2.record, state: "completed", updated_at: input.now } : { ...current2.record, state: "active", next_run_at: input.nextRunAt, updated_at: input.now };
|
|
3958
3943
|
yield* Effect16.sync(() => schedules.set(input.id, {
|
|
3959
3944
|
record: acked,
|
|
3945
|
+
...input.nextRunAt === undefined ? { claimOwner: input.workerId } : {},
|
|
3960
3946
|
...current2.idempotencyKey === undefined ? {} : { idempotencyKey: current2.idempotencyKey }
|
|
3961
3947
|
}));
|
|
3962
3948
|
return acked;
|
|
@@ -3965,7 +3951,7 @@ var memoryLayer14 = Layer16.sync(Service16, () => {
|
|
|
3965
3951
|
const current2 = schedules.get(input.id);
|
|
3966
3952
|
if (current2 === undefined)
|
|
3967
3953
|
return yield* Effect16.fail(new ScheduleNotFound({ id: input.id }));
|
|
3968
|
-
if (current2.claimOwner !== input.workerId) {
|
|
3954
|
+
if (current2.record.state !== "claimed" || current2.claimOwner !== input.workerId) {
|
|
3969
3955
|
return yield* Effect16.fail(new ScheduleClaimMismatch({ id: input.id, worker_id: input.workerId }));
|
|
3970
3956
|
}
|
|
3971
3957
|
const released = {
|
|
@@ -4109,7 +4095,7 @@ var toEntryRecord = (row) => {
|
|
|
4109
4095
|
return Effect17.fail(new SessionRepositoryError({ message: `Unknown session entry tag: ${row.tag}` }));
|
|
4110
4096
|
}
|
|
4111
4097
|
};
|
|
4112
|
-
var
|
|
4098
|
+
var mapDatabaseError13 = (effect) => effect.pipe(Effect17.mapError((error) => new SessionRepositoryError({ message: String(error) })));
|
|
4113
4099
|
var mapWriteError = (effect) => effect.pipe(Effect17.mapError((error) => error instanceof SessionRepositoryError ? error : new SessionRepositoryError({ message: String(error) })));
|
|
4114
4100
|
var maxListLimit3 = 100;
|
|
4115
4101
|
var defaultListLimit2 = 50;
|
|
@@ -4166,11 +4152,11 @@ var validateCompaction = (currentPath, input) => {
|
|
|
4166
4152
|
var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
4167
4153
|
const db = yield* Service;
|
|
4168
4154
|
const getForTenant = Effect17.fn("SessionRepository.getForTenant")(function* (tenantId2, id2) {
|
|
4169
|
-
const rows = yield*
|
|
4155
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySessions).where(predicate(tenantId2, relaySessions.tenantId, eq14(relaySessions.id, id2))).limit(1));
|
|
4170
4156
|
return rows[0] === undefined ? undefined : toSessionRecord(rows[0]);
|
|
4171
4157
|
});
|
|
4172
4158
|
const entryById = Effect17.fn("SessionRepository.entryById")(function* (tenantId2, sessionId, id2) {
|
|
4173
|
-
const rows = yield*
|
|
4159
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId, eq14(relaySessionEntries.id, id2), eq14(relaySessionEntries.sessionId, sessionId))).limit(1));
|
|
4174
4160
|
return rows[0] === undefined ? undefined : yield* toEntryRecord(rows[0]);
|
|
4175
4161
|
});
|
|
4176
4162
|
const pathForTenant = Effect17.fn("SessionRepository.pathForTenant")(function* (tenantId2, input) {
|
|
@@ -4205,7 +4191,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4205
4191
|
const existing = yield* getForTenant(tenantId2, input.id);
|
|
4206
4192
|
if (existing !== undefined)
|
|
4207
4193
|
return yield* Effect17.fail(new DuplicateSession({ id: input.id }));
|
|
4208
|
-
const rows = yield*
|
|
4194
|
+
const rows = yield* mapDatabaseError13(db.insert(relaySessions).values({
|
|
4209
4195
|
tenantId: tenantId2,
|
|
4210
4196
|
id: input.id,
|
|
4211
4197
|
rootAddressId: input.rootAddressId,
|
|
@@ -4224,13 +4210,13 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4224
4210
|
if (existing !== undefined) {
|
|
4225
4211
|
if (existing.rootAddressId !== input.rootAddressId)
|
|
4226
4212
|
return yield* Effect17.fail(rootMismatch(input.id));
|
|
4227
|
-
const rows2 = yield*
|
|
4213
|
+
const rows2 = yield* mapDatabaseError13(db.update(relaySessions).set({ updatedAt: toPgDate(input.now) }).where(predicate(tenantId2, relaySessions.tenantId, eq14(relaySessions.id, input.id))).returning());
|
|
4228
4214
|
const row2 = rows2[0];
|
|
4229
4215
|
if (row2 === undefined)
|
|
4230
4216
|
return yield* Effect17.fail(missingSession(input.id));
|
|
4231
4217
|
return toSessionRecord(row2);
|
|
4232
4218
|
}
|
|
4233
|
-
const rows = yield*
|
|
4219
|
+
const rows = yield* mapDatabaseError13(db.insert(relaySessions).values({
|
|
4234
4220
|
tenantId: tenantId2,
|
|
4235
4221
|
id: input.id,
|
|
4236
4222
|
rootAddressId: input.rootAddressId,
|
|
@@ -4250,14 +4236,14 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4250
4236
|
const list8 = Effect17.fn("SessionRepository.list")(function* (input) {
|
|
4251
4237
|
const tenantId2 = yield* current;
|
|
4252
4238
|
const limit = listLimit3(input);
|
|
4253
|
-
const rows = yield*
|
|
4239
|
+
const rows = yield* mapDatabaseError13(db.select().from(relaySessions).where(predicate(tenantId2, relaySessions.tenantId, ...listConditions2(input))).orderBy(desc5(relaySessions.updatedAt), desc5(relaySessions.id)).limit(limit + 1));
|
|
4254
4240
|
const records = rows.slice(0, limit).map(toSessionRecord);
|
|
4255
4241
|
const nextCursor = nextCursorOf2(records, rows.length, limit);
|
|
4256
4242
|
return nextCursor === undefined ? { records } : { records, nextCursor };
|
|
4257
4243
|
});
|
|
4258
4244
|
const touch = Effect17.fn("SessionRepository.touch")(function* (input) {
|
|
4259
4245
|
const tenantId2 = yield* current;
|
|
4260
|
-
const rows = yield*
|
|
4246
|
+
const rows = yield* mapDatabaseError13(db.update(relaySessions).set({ updatedAt: toPgDate(input.updatedAt) }).where(predicate(tenantId2, relaySessions.tenantId, eq14(relaySessions.id, input.id))).returning());
|
|
4261
4247
|
const row = rows[0];
|
|
4262
4248
|
if (row === undefined)
|
|
4263
4249
|
return yield* Effect17.fail(missingSession(input.id));
|
|
@@ -4279,7 +4265,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4279
4265
|
if (entry === undefined)
|
|
4280
4266
|
return yield* Effect17.fail(missingEntry(input.id));
|
|
4281
4267
|
}
|
|
4282
|
-
yield*
|
|
4268
|
+
yield* mapDatabaseError13(db.update(relaySessions).set({ leafEntryId: input.id, updatedAt: toPgDate(input.updatedAt) }).where(predicate(tenantId2, relaySessions.tenantId, eq14(relaySessions.id, input.sessionId))));
|
|
4283
4269
|
});
|
|
4284
4270
|
const setLeaf = Effect17.fn("SessionRepository.setLeaf")(function* (input) {
|
|
4285
4271
|
const tenantId2 = yield* current;
|
|
@@ -4333,7 +4319,7 @@ var layer17 = Layer17.effect(Service17, Effect17.gen(function* () {
|
|
|
4333
4319
|
});
|
|
4334
4320
|
const countEntries = Effect17.fn("SessionRepository.countEntries")(function* (sessionId) {
|
|
4335
4321
|
const tenantId2 = yield* current;
|
|
4336
|
-
const rows = yield*
|
|
4322
|
+
const rows = yield* mapDatabaseError13(db.select({ value: count() }).from(relaySessionEntries).where(predicate(tenantId2, relaySessionEntries.tenantId, eq14(relaySessionEntries.sessionId, sessionId))));
|
|
4337
4323
|
return Number(rows[0]?.value ?? 0);
|
|
4338
4324
|
});
|
|
4339
4325
|
return Service17.of({ create: create4, ensure, get: get8, list: list8, touch, appendEntry, countEntries, path, setLeaf, leaf });
|
|
@@ -4522,8 +4508,8 @@ __export(exports_skill_definition_repository, {
|
|
|
4522
4508
|
SkillDefinitionRepositoryError: () => SkillDefinitionRepositoryError,
|
|
4523
4509
|
Service: () => Service18
|
|
4524
4510
|
});
|
|
4525
|
-
import { asc as asc9, desc as desc6, eq as eq15, inArray, sql as
|
|
4526
|
-
import { Context as Context18, Effect as Effect18, HashSet, Layer as Layer18, Schema as Schema27, Semaphore } from "effect";
|
|
4511
|
+
import { asc as asc9, desc as desc6, eq as eq15, inArray as inArray3, sql as sql10 } from "drizzle-orm";
|
|
4512
|
+
import { Context as Context18, Effect as Effect18, HashSet, Layer as Layer18, Schema as Schema27, Semaphore as Semaphore2 } from "effect";
|
|
4527
4513
|
class SkillDefinitionRepositoryError extends Schema27.TaggedErrorClass()("SkillDefinitionRepositoryError", {
|
|
4528
4514
|
message: Schema27.String
|
|
4529
4515
|
}) {
|
|
@@ -4585,10 +4571,10 @@ var missingSkillMessage = (requested, records) => {
|
|
|
4585
4571
|
const found = HashSet.fromIterable(records.map((record2) => record2.id));
|
|
4586
4572
|
return `Skill definition not found: ${requested.find((id2) => !HashSet.has(found, id2)) ?? requested[0]}`;
|
|
4587
4573
|
};
|
|
4588
|
-
var
|
|
4574
|
+
var mapDatabaseError14 = (effect) => effect.pipe(Effect18.mapError((error) => new SkillDefinitionRepositoryError({ message: String(error) })));
|
|
4589
4575
|
var mapPutError2 = (effect) => effect.pipe(Effect18.mapError((error) => error instanceof SkillDefinitionRepositoryError ? error : new SkillDefinitionRepositoryError({ message: String(error) })));
|
|
4590
4576
|
var skillPinLockKey = (tenantId2, executionId) => `skill-pins:${tenantId2}:${executionId}`;
|
|
4591
|
-
var lockExecutionPins = (db, tenantId2, executionId) =>
|
|
4577
|
+
var lockExecutionPins = (db, tenantId2, executionId) => mapDatabaseError14(db.execute(sql10`select pg_advisory_xact_lock(hashtext(${skillPinLockKey(tenantId2, executionId)}))`)).pipe(Effect18.asVoid);
|
|
4592
4578
|
var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
4593
4579
|
const db = yield* Service;
|
|
4594
4580
|
const put2 = Effect18.fn("SkillDefinitionRepository.put")(function* (input) {
|
|
@@ -4606,7 +4592,7 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4606
4592
|
target: [relaySkillDefinitions.tenantId, relaySkillDefinitions.id],
|
|
4607
4593
|
set: {
|
|
4608
4594
|
name: input.definition.frontmatter.name,
|
|
4609
|
-
currentRevision:
|
|
4595
|
+
currentRevision: sql10`${relaySkillDefinitions.currentRevision} + 1`,
|
|
4610
4596
|
definitionJson: input.definition,
|
|
4611
4597
|
updatedAt: toPgDate(input.now)
|
|
4612
4598
|
}
|
|
@@ -4631,32 +4617,32 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4631
4617
|
});
|
|
4632
4618
|
const get9 = Effect18.fn("SkillDefinitionRepository.get")(function* (id2) {
|
|
4633
4619
|
const tenantId2 = yield* current;
|
|
4634
|
-
const rows = yield*
|
|
4620
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId, eq15(relaySkillDefinitions.id, id2))).limit(1));
|
|
4635
4621
|
return rows[0] === undefined ? undefined : toRecord11(rows[0]);
|
|
4636
4622
|
});
|
|
4637
4623
|
const getRevision2 = Effect18.fn("SkillDefinitionRepository.getRevision")(function* (input) {
|
|
4638
4624
|
const tenantId2 = yield* current;
|
|
4639
|
-
const rows = yield*
|
|
4625
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySkillDefinitionRevisions).where(predicate(tenantId2, relaySkillDefinitionRevisions.tenantId, eq15(relaySkillDefinitionRevisions.skillDefinitionId, input.id), eq15(relaySkillDefinitionRevisions.revision, input.revision))).limit(1));
|
|
4640
4626
|
return rows[0] === undefined ? undefined : toRevisionRecord2(rows[0]);
|
|
4641
4627
|
});
|
|
4642
4628
|
const list9 = Effect18.fn("SkillDefinitionRepository.list")(function* () {
|
|
4643
4629
|
const tenantId2 = yield* current;
|
|
4644
|
-
const rows = yield*
|
|
4630
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId)).orderBy(desc6(relaySkillDefinitions.createdAt), asc9(relaySkillDefinitions.id)));
|
|
4645
4631
|
return rows.map(toRecord11);
|
|
4646
4632
|
});
|
|
4647
4633
|
const listRevisions2 = Effect18.fn("SkillDefinitionRepository.listRevisions")(function* (id2) {
|
|
4648
4634
|
const tenantId2 = yield* current;
|
|
4649
|
-
const rows = yield*
|
|
4635
|
+
const rows = yield* mapDatabaseError14(db.select().from(relaySkillDefinitionRevisions).where(predicate(tenantId2, relaySkillDefinitionRevisions.tenantId, eq15(relaySkillDefinitionRevisions.skillDefinitionId, id2))).orderBy(desc6(relaySkillDefinitionRevisions.revision)));
|
|
4650
4636
|
return rows.map(toRevisionRecord2);
|
|
4651
4637
|
});
|
|
4652
4638
|
const listPins = Effect18.fn("SkillDefinitionRepository.listPins")(function* (executionId) {
|
|
4653
4639
|
const tenantId2 = yield* current;
|
|
4654
|
-
const rows = yield*
|
|
4640
|
+
const rows = yield* mapDatabaseError14(db.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId, eq15(relayExecutionSkillPins.executionId, executionId))).orderBy(asc9(relayExecutionSkillPins.skillDefinitionId)));
|
|
4655
4641
|
return rows.map(toPinRecord);
|
|
4656
4642
|
});
|
|
4657
4643
|
const getPinned = Effect18.fn("SkillDefinitionRepository.getPinned")(function* (input) {
|
|
4658
4644
|
const tenantId2 = yield* current;
|
|
4659
|
-
const rows = yield*
|
|
4645
|
+
const rows = yield* mapDatabaseError14(db.select().from(relayExecutionSkillPins).where(predicate(tenantId2, relayExecutionSkillPins.tenantId, eq15(relayExecutionSkillPins.executionId, input.executionId), eq15(relayExecutionSkillPins.skillDefinitionId, input.skillDefinitionId))).limit(1));
|
|
4660
4646
|
return rows[0] === undefined ? undefined : toPinRecord(rows[0]);
|
|
4661
4647
|
});
|
|
4662
4648
|
const pinExecution = Effect18.fn("SkillDefinitionRepository.pinExecution")(function* (input) {
|
|
@@ -4676,7 +4662,7 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4676
4662
|
}
|
|
4677
4663
|
return existing;
|
|
4678
4664
|
}
|
|
4679
|
-
const currentRows = yield* tx.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId,
|
|
4665
|
+
const currentRows = yield* tx.select().from(relaySkillDefinitions).where(predicate(tenantId2, relaySkillDefinitions.tenantId, inArray3(relaySkillDefinitions.id, [...requestedIds])));
|
|
4680
4666
|
const currentRecords = currentRows.map(toRecord11);
|
|
4681
4667
|
if (currentRecords.length !== requestedIds.length) {
|
|
4682
4668
|
return yield* Effect18.fail(new SkillDefinitionRepositoryError({ message: missingSkillMessage(requestedIds, currentRecords) }));
|
|
@@ -4695,7 +4681,7 @@ var layer18 = Layer18.effect(Service18, Effect18.gen(function* () {
|
|
|
4695
4681
|
return Service18.of({ put: put2, get: get9, getRevision: getRevision2, list: list9, listRevisions: listRevisions2, pinExecution, listPins, getPinned });
|
|
4696
4682
|
}));
|
|
4697
4683
|
var memoryLayer16 = Layer18.sync(Service18, () => {
|
|
4698
|
-
const semaphore =
|
|
4684
|
+
const semaphore = Semaphore2.makeUnsafe(1);
|
|
4699
4685
|
const records = new Map;
|
|
4700
4686
|
const revisions = new Map;
|
|
4701
4687
|
const pins = new Map;
|
|
@@ -4825,7 +4811,7 @@ __export(exports_steering_repository, {
|
|
|
4825
4811
|
SteeringRepositoryError: () => SteeringRepositoryError,
|
|
4826
4812
|
Service: () => Service19
|
|
4827
4813
|
});
|
|
4828
|
-
import { asc as asc10, eq as eq16, inArray as
|
|
4814
|
+
import { asc as asc10, eq as eq16, inArray as inArray4, isNull, max as max2, sql as sql11 } from "drizzle-orm";
|
|
4829
4815
|
import { Context as Context19, Effect as Effect19, Layer as Layer19, Ref as Ref3, Schema as Schema28 } from "effect";
|
|
4830
4816
|
class SteeringRepositoryError extends Schema28.TaggedErrorClass()("SteeringRepositoryError", {
|
|
4831
4817
|
message: Schema28.String
|
|
@@ -4835,7 +4821,7 @@ class SteeringRepositoryError extends Schema28.TaggedErrorClass()("SteeringRepos
|
|
|
4835
4821
|
class Service19 extends Context19.Service()("@relayfx/store-sql/SteeringRepository") {
|
|
4836
4822
|
}
|
|
4837
4823
|
var SteeringKindSchema = Schema28.Literals(["steering", "follow_up"]);
|
|
4838
|
-
var
|
|
4824
|
+
var mapDatabaseError15 = (effect) => effect.pipe(Effect19.mapError((error) => new SteeringRepositoryError({ message: String(error) })));
|
|
4839
4825
|
var mapTransactionError = (effect) => effect.pipe(Effect19.mapError((error) => error instanceof SteeringRepositoryError ? error : new SteeringRepositoryError({ message: String(error) })));
|
|
4840
4826
|
var toMessageRecord = (row) => {
|
|
4841
4827
|
const consumedAt = fromNullablePgDate(row.consumedAt);
|
|
@@ -4876,15 +4862,15 @@ var consumedMessage = (message, input) => ({
|
|
|
4876
4862
|
var messagesForDrain = (db, tenantId2, drain) => Effect19.gen(function* () {
|
|
4877
4863
|
if (drain.messageSequences.length === 0)
|
|
4878
4864
|
return { drain, messages: [] };
|
|
4879
|
-
const rows = yield*
|
|
4865
|
+
const rows = yield* mapDatabaseError15(db.select().from(relaySteeringMessages).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq16(relaySteeringMessages.executionId, drain.executionId), eq16(relaySteeringMessages.kind, drain.kind), inArray4(relaySteeringMessages.sequence, [...drain.messageSequences]))).orderBy(asc10(relaySteeringMessages.sequence)));
|
|
4880
4866
|
return { drain, messages: rows.map(toMessageRecord) };
|
|
4881
4867
|
});
|
|
4882
4868
|
var findDrain = (db, tenantId2, input) => Effect19.gen(function* () {
|
|
4883
|
-
const rows = yield*
|
|
4869
|
+
const rows = yield* mapDatabaseError15(db.select().from(relaySteeringDrains).where(predicate(tenantId2, relaySteeringDrains.tenantId, eq16(relaySteeringDrains.executionId, input.executionId), eq16(relaySteeringDrains.kind, input.kind), eq16(relaySteeringDrains.drainId, input.drainId))).limit(1));
|
|
4884
4870
|
const row = rows[0];
|
|
4885
4871
|
return row === undefined ? undefined : toDrainRecord(row);
|
|
4886
4872
|
});
|
|
4887
|
-
var lockQueue = (db, tenantId2, executionId, kind) =>
|
|
4873
|
+
var lockQueue = (db, tenantId2, executionId, kind) => mapDatabaseError15(db.execute(sql11`select pg_advisory_xact_lock(hashtext(${queueKey(tenantId2, executionId, kind)}))`)).pipe(Effect19.asVoid);
|
|
4888
4874
|
var runningExecutionStatus = (db, tenantId2, executionId) => db.select({ status: relayExecutions.status }).from(relayExecutions).where(predicate(tenantId2, relayExecutions.tenantId, eq16(relayExecutions.id, executionId))).for("update").limit(1).pipe(Effect19.map((rows) => rows[0]?.status));
|
|
4889
4875
|
var layer19 = Layer19.effect(Service19, Effect19.gen(function* () {
|
|
4890
4876
|
const db = yield* Service;
|
|
@@ -4941,7 +4927,7 @@ var layer19 = Layer19.effect(Service19, Effect19.gen(function* () {
|
|
|
4941
4927
|
const messages = sequences.length === 0 ? [] : yield* tx.update(relaySteeringMessages).set({
|
|
4942
4928
|
drainId: input.drainId,
|
|
4943
4929
|
consumedAt: toPgDate(input.consumedAt)
|
|
4944
|
-
}).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq16(relaySteeringMessages.executionId, input.executionId), eq16(relaySteeringMessages.kind, input.kind),
|
|
4930
|
+
}).where(predicate(tenantId2, relaySteeringMessages.tenantId, eq16(relaySteeringMessages.executionId, input.executionId), eq16(relaySteeringMessages.kind, input.kind), inArray4(relaySteeringMessages.sequence, sequences))).returning();
|
|
4945
4931
|
const messageBySequence = new Map(messages.map((message) => [message.sequence, message]));
|
|
4946
4932
|
const orderedMessages = sequences.flatMap((sequence) => {
|
|
4947
4933
|
const message = messageBySequence.get(sequence);
|
|
@@ -5122,15 +5108,15 @@ var toResultRecord = (row) => ({
|
|
|
5122
5108
|
},
|
|
5123
5109
|
createdAt: fromPgDate(row.createdAt)
|
|
5124
5110
|
});
|
|
5125
|
-
var
|
|
5111
|
+
var mapDatabaseError16 = (effect) => effect.pipe(Effect20.mapError((error) => new ToolCallRepositoryError({ message: String(error) })));
|
|
5126
5112
|
var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
5127
5113
|
const db = yield* Service;
|
|
5128
5114
|
const recordCall = Effect20.fn("ToolCallRepository.recordCall")(function* (input) {
|
|
5129
5115
|
const tenantId2 = yield* current;
|
|
5130
|
-
const existingRows = yield*
|
|
5116
|
+
const existingRows = yield* mapDatabaseError16(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq17(relayToolCalls.id, input.call.id))).limit(1));
|
|
5131
5117
|
if (existingRows[0] !== undefined)
|
|
5132
5118
|
return yield* Effect20.fail(new DuplicateToolCall({ id: input.call.id }));
|
|
5133
|
-
const rows = yield*
|
|
5119
|
+
const rows = yield* mapDatabaseError16(db.insert(relayToolCalls).values(toCallInsert(tenantId2, input)).returning());
|
|
5134
5120
|
const row = rows[0];
|
|
5135
5121
|
if (row === undefined) {
|
|
5136
5122
|
return yield* Effect20.fail(new ToolCallRepositoryError({ message: "Tool call insert returned no row" }));
|
|
@@ -5139,24 +5125,24 @@ var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
|
5139
5125
|
});
|
|
5140
5126
|
const getCall = Effect20.fn("ToolCallRepository.getCall")(function* (id2) {
|
|
5141
5127
|
const tenantId2 = yield* current;
|
|
5142
|
-
const rows = yield*
|
|
5128
|
+
const rows = yield* mapDatabaseError16(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq17(relayToolCalls.id, id2))).limit(1));
|
|
5143
5129
|
return rows[0] === undefined ? undefined : toCallRecord(rows[0]);
|
|
5144
5130
|
});
|
|
5145
5131
|
const listCalls = Effect20.fn("ToolCallRepository.listCalls")(function* (executionId) {
|
|
5146
5132
|
const tenantId2 = yield* current;
|
|
5147
|
-
const rows = yield*
|
|
5133
|
+
const rows = yield* mapDatabaseError16(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq17(relayToolCalls.executionId, executionId))).orderBy(asc11(relayToolCalls.createdAt)));
|
|
5148
5134
|
return rows.map(toCallRecord);
|
|
5149
5135
|
});
|
|
5150
5136
|
const recordResult = Effect20.fn("ToolCallRepository.recordResult")(function* (input) {
|
|
5151
5137
|
const tenantId2 = yield* current;
|
|
5152
|
-
const callRows = yield*
|
|
5138
|
+
const callRows = yield* mapDatabaseError16(db.select().from(relayToolCalls).where(predicate(tenantId2, relayToolCalls.tenantId, eq17(relayToolCalls.id, input.result.call_id))).limit(1));
|
|
5153
5139
|
if (callRows[0] === undefined)
|
|
5154
5140
|
return yield* Effect20.fail(new ToolCallNotFound({ id: input.result.call_id }));
|
|
5155
|
-
const existingRows = yield*
|
|
5141
|
+
const existingRows = yield* mapDatabaseError16(db.select().from(relayToolResults).where(predicate(tenantId2, relayToolResults.tenantId, eq17(relayToolResults.toolCallId, input.result.call_id))).limit(1));
|
|
5156
5142
|
if (existingRows[0] !== undefined) {
|
|
5157
5143
|
return yield* Effect20.fail(new DuplicateToolResult({ call_id: input.result.call_id }));
|
|
5158
5144
|
}
|
|
5159
|
-
const rows = yield*
|
|
5145
|
+
const rows = yield* mapDatabaseError16(db.insert(relayToolResults).values(toResultInsert(tenantId2, input)).returning());
|
|
5160
5146
|
const row = rows[0];
|
|
5161
5147
|
if (row === undefined) {
|
|
5162
5148
|
return yield* Effect20.fail(new ToolCallRepositoryError({ message: "Tool result insert returned no row" }));
|
|
@@ -5165,7 +5151,7 @@ var layer20 = Layer20.effect(Service20, Effect20.gen(function* () {
|
|
|
5165
5151
|
});
|
|
5166
5152
|
const getResult = Effect20.fn("ToolCallRepository.getResult")(function* (callId) {
|
|
5167
5153
|
const tenantId2 = yield* current;
|
|
5168
|
-
const rows = yield*
|
|
5154
|
+
const rows = yield* mapDatabaseError16(db.select().from(relayToolResults).where(predicate(tenantId2, relayToolResults.tenantId, eq17(relayToolResults.toolCallId, callId))).limit(1));
|
|
5169
5155
|
return rows[0] === undefined ? undefined : toResultRecord(rows[0]);
|
|
5170
5156
|
});
|
|
5171
5157
|
return Service20.of({ recordCall, getCall, listCalls, recordResult, getResult });
|
|
@@ -5316,8 +5302,8 @@ var toSnapshotRecord = (row) => ({
|
|
|
5316
5302
|
metadata: row.metadataJson,
|
|
5317
5303
|
created_at: fromPgDate(row.createdAt)
|
|
5318
5304
|
});
|
|
5319
|
-
var
|
|
5320
|
-
var updateLease = (db, tenantId2, executionId, values) =>
|
|
5305
|
+
var mapDatabaseError17 = (effect) => effect.pipe(Effect21.mapError((error) => new WorkspaceLeaseRepositoryError({ message: String(error) })));
|
|
5306
|
+
var updateLease = (db, tenantId2, executionId, values) => mapDatabaseError17(db.update(relayWorkspaceLeases).set(values).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq18(relayWorkspaceLeases.executionId, executionId))).returning()).pipe(Effect21.flatMap((rows) => {
|
|
5321
5307
|
const row = rows[0];
|
|
5322
5308
|
if (row === undefined)
|
|
5323
5309
|
return Effect21.fail(new WorkspaceLeaseNotFound({ execution_id: executionId }));
|
|
@@ -5327,16 +5313,16 @@ var layer21 = Layer21.effect(Service21, Effect21.gen(function* () {
|
|
|
5327
5313
|
const db = yield* Service;
|
|
5328
5314
|
const getByExecution = Effect21.fn("WorkspaceLeaseRepository.getByExecution")(function* (id2) {
|
|
5329
5315
|
const tenantId2 = yield* current;
|
|
5330
|
-
const rows = yield*
|
|
5316
|
+
const rows = yield* mapDatabaseError17(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq18(relayWorkspaceLeases.executionId, id2))).limit(1));
|
|
5331
5317
|
return rows[0] === undefined ? undefined : toLeaseRecord(rows[0]);
|
|
5332
5318
|
});
|
|
5333
5319
|
const plan = Effect21.fn("WorkspaceLeaseRepository.plan")(function* (input) {
|
|
5334
5320
|
const tenantId2 = yield* current;
|
|
5335
|
-
const existing = yield*
|
|
5321
|
+
const existing = yield* mapDatabaseError17(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq18(relayWorkspaceLeases.executionId, input.executionId), notInArray(relayWorkspaceLeases.status, [...terminalStatuses]))).limit(1));
|
|
5336
5322
|
if (existing[0] !== undefined) {
|
|
5337
5323
|
return yield* Effect21.fail(new DuplicateWorkspaceLease({ execution_id: input.executionId }));
|
|
5338
5324
|
}
|
|
5339
|
-
const rows = yield*
|
|
5325
|
+
const rows = yield* mapDatabaseError17(db.insert(relayWorkspaceLeases).values(toPlanInsert(tenantId2, input)).returning());
|
|
5340
5326
|
const row = rows[0];
|
|
5341
5327
|
if (row === undefined) {
|
|
5342
5328
|
return yield* Effect21.fail(new WorkspaceLeaseRepositoryError({ message: "Workspace lease insert returned no row" }));
|
|
@@ -5368,11 +5354,11 @@ var layer21 = Layer21.effect(Service21, Effect21.gen(function* () {
|
|
|
5368
5354
|
});
|
|
5369
5355
|
const recordSnapshot = Effect21.fn("WorkspaceLeaseRepository.recordSnapshot")(function* (input) {
|
|
5370
5356
|
const tenantId2 = yield* current;
|
|
5371
|
-
const leaseRows = yield*
|
|
5357
|
+
const leaseRows = yield* mapDatabaseError17(db.select().from(relayWorkspaceLeases).where(predicate(tenantId2, relayWorkspaceLeases.tenantId, eq18(relayWorkspaceLeases.executionId, input.executionId))).limit(1));
|
|
5372
5358
|
if (leaseRows[0] === undefined) {
|
|
5373
5359
|
return yield* Effect21.fail(new WorkspaceLeaseNotFound({ execution_id: input.executionId }));
|
|
5374
5360
|
}
|
|
5375
|
-
const rows = yield*
|
|
5361
|
+
const rows = yield* mapDatabaseError17(db.insert(relayWorkspaceSnapshots).values(toSnapshotInsert(tenantId2, input)).returning());
|
|
5376
5362
|
const row = rows[0];
|
|
5377
5363
|
if (row === undefined) {
|
|
5378
5364
|
return yield* Effect21.fail(new WorkspaceLeaseRepositoryError({ message: "Workspace snapshot insert returned no row" }));
|
|
@@ -8347,7 +8333,7 @@ __export(exports_model_registry, {
|
|
|
8347
8333
|
Service: () => Service28,
|
|
8348
8334
|
LanguageModelNotRegistered: () => LanguageModelNotRegistered
|
|
8349
8335
|
});
|
|
8350
|
-
import { Chunk as Chunk2, Context as Context40, Effect as Effect47, Layer as Layer41, Option as Option17, Ref as Ref11, Schema as Schema45, Semaphore as
|
|
8336
|
+
import { Chunk as Chunk2, Context as Context40, Effect as Effect47, Layer as Layer41, Option as Option17, Ref as Ref11, Schema as Schema45, Semaphore as Semaphore3 } from "effect";
|
|
8351
8337
|
import * as Ai10 from "effect/unstable/ai";
|
|
8352
8338
|
|
|
8353
8339
|
class LanguageModelNotRegistered extends Schema45.TaggedErrorClass()("LanguageModelNotRegistered", {
|
|
@@ -8380,7 +8366,7 @@ var registrationFromLayer = (input) => Ai10.Model.make(input.provider, input.mod
|
|
|
8380
8366
|
})));
|
|
8381
8367
|
var layer31 = (initialRegistrations = [], options) => Layer41.effect(Service28, Effect47.gen(function* () {
|
|
8382
8368
|
const registry = yield* Ref11.make(initialRegistrations.reduce(upsertRegistration, Chunk2.empty()));
|
|
8383
|
-
const semaphore = options?.maxConcurrentModelCalls === undefined ? undefined : yield*
|
|
8369
|
+
const semaphore = options?.maxConcurrentModelCalls === undefined ? undefined : yield* Semaphore3.make(options.maxConcurrentModelCalls);
|
|
8384
8370
|
const register4 = Effect47.fn("ModelRegistry.register")(function* (input) {
|
|
8385
8371
|
yield* Ref11.update(registry, (items) => upsertRegistration(items, input.registration));
|
|
8386
8372
|
});
|