@effect-agent/storage-memory 0.1.0-beta.36 → 0.1.0-beta.38
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/index.d.mts +6 -2
- package/dist/index.mjs +161 -3
- package/dist/index.mjs.map +1 -1
- package/dist/testing.d.mts +2 -2
- package/dist/testing.mjs +2 -2
- package/package.json +3 -3
- package/src/index.ts +1 -0
- package/src/memory-schedule-store.ts +284 -0
- package/src/testing.ts +6 -0
package/dist/index.d.mts
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
import { ConversationStore, SubmissionLedger } from "@effect-agent/session";
|
|
1
|
+
import { ConversationStore, ScheduleStore, SubmissionLedger } from "@effect-agent/session";
|
|
2
2
|
import { Crypto, Effect, Layer, Option } from "effect";
|
|
3
3
|
//#region src/memory-ledger.d.ts
|
|
4
4
|
/** Construction options for the in-memory reference SubmissionLedger. */
|
|
@@ -18,6 +18,10 @@ interface MemorySubmissionLedgerOptions {
|
|
|
18
18
|
declare const memorySubmissionLedgerLayer: (options?: MemorySubmissionLedgerOptions) => Layer.Layer<SubmissionLedger>;
|
|
19
19
|
declare const MemorySubmissionLedgerLive: Layer.Layer<SubmissionLedger>;
|
|
20
20
|
//#endregion
|
|
21
|
+
//#region src/memory-schedule-store.d.ts
|
|
22
|
+
declare const memoryScheduleStoreLayer: () => Layer.Layer<ScheduleStore>;
|
|
23
|
+
declare const MemoryScheduleStoreLive: Layer.Layer<ScheduleStore>;
|
|
24
|
+
//#endregion
|
|
21
25
|
//#region src/memory-storage.d.ts
|
|
22
26
|
declare const MemoryConversationStoreLive: Layer.Layer<ConversationStore, never, Crypto.Crypto>;
|
|
23
27
|
/**
|
|
@@ -26,5 +30,5 @@ declare const MemoryConversationStoreLive: Layer.Layer<ConversationStore, never,
|
|
|
26
30
|
*/
|
|
27
31
|
declare const MemoryStorageLive: Layer.Layer<ConversationStore, never, Crypto.Crypto>;
|
|
28
32
|
//#endregion
|
|
29
|
-
export { MemoryConversationStoreLive, MemoryStorageLive, MemorySubmissionLedgerLive, MemorySubmissionLedgerOptions, memorySubmissionLedgerLayer };
|
|
33
|
+
export { MemoryConversationStoreLive, MemoryScheduleStoreLive, MemoryStorageLive, MemorySubmissionLedgerLive, MemorySubmissionLedgerOptions, memoryScheduleStoreLayer, memorySubmissionLedgerLayer };
|
|
30
34
|
//# sourceMappingURL=index.d.mts.map
|
package/dist/index.mjs
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import { AttemptId, ConversationId, ReceiptId, SubmissionId } from "@effect-agent/core";
|
|
2
|
-
import { AbortCommand, AbortIntent, AdmissionAdmitted, AdmissionConflict, AdmissionIndeterminate, AdmissionNotAdmitted, AdmissionRequest, AdmissionResult, AppendConflict, AppendResult, ApprovalConflict, ApprovalDecisionCommand, ApprovalDecisionIntent, AttachChildToReservationRequest, BeginChildBudgetReleaseRequest, CanonicalRecordEnvelope, CanonicalSequence, CheckpointRejected, ChildAttachmentSnapshot, ChildBudgetReservationRequest, ChildBudgetReservationSnapshot, ChildReservationConflict, ChildSettledNotification, Claim, ClaimJoiningRequest, ClaimRequest, ConversationExport, ConversationExportRequest, ConversationMaterialization, ConversationNotMaterialized, ConversationObservation, ConversationRead, ConversationStore, ConversationStoreError, ConversationTail, ConversationTailRequest, DEFAULT_OWNERSHIP_LEASE_DURATION, EMPTY_TAIL_DIGEST, FenceRejected, FencedAppendRequest, InputAppliedMarker, JoinSnapshot, JoinedToHost, JoiningClaim, LedgerCapabilities, LedgerError, LoadCheckpointRequest, MarkInputAppliedRequest, MarkJoinedRequest, MarkReadyRequest, MarkUnknownRequest, ObservationOffset, OwnershipLost, OwnershipRenewal, OwnershipSnapshot, OwnershipToken, PersistedJson, ProducerEpoch, QueueSequence, RecoverySnapshot, RecoverySnapshotRequest, ReleaseChildBudgetRequest, ReleaseOwnershipRequest, RenewOwnershipRequest, ReservedChildBudget, ReservedSettlement, RevertJoiningRequest, SaveCheckpointRequest, Settlement, SettlementConflict, SettlementFinalization, SettlementReservation, SettlementReservationSnapshot, SubmissionLedger, SubmissionLookup, SubmissionLookupByKey, SubmissionSnapshot, SuspendRequest, SuspensionSnapshot, UnknownResolution, UnknownResolutionCommand, UnknownResolutionConflict, UnknownResolutionIntent, digestCanonicalBatch, settlementFailureFromRecord } from "@effect-agent/session";
|
|
3
|
-
import { Clock, Crypto, DateTime, Duration, Effect, Encoding, Layer, Option, PubSub, Ref, Schema, Stream } from "effect";
|
|
2
|
+
import { AbortCommand, AbortIntent, AdmissionAdmitted, AdmissionConflict, AdmissionIndeterminate, AdmissionNotAdmitted, AdmissionRequest, AdmissionResult, AppendConflict, AppendResult, ApprovalConflict, ApprovalDecisionCommand, ApprovalDecisionIntent, AttachChildToReservationRequest, BeginChildBudgetReleaseRequest, CanonicalRecordEnvelope, CanonicalSequence, CheckpointRejected, ChildAttachmentSnapshot, ChildBudgetReservationRequest, ChildBudgetReservationSnapshot, ChildReservationConflict, ChildSettledNotification, Claim, ClaimJoiningRequest, ClaimRequest, ConversationExport, ConversationExportRequest, ConversationMaterialization, ConversationNotMaterialized, ConversationObservation, ConversationRead, ConversationStore, ConversationStoreError, ConversationTail, ConversationTailRequest, DEFAULT_OWNERSHIP_LEASE_DURATION, EMPTY_TAIL_DIGEST, FenceRejected, FencedAppendRequest, InputAppliedMarker, JoinSnapshot, JoinedToHost, JoiningClaim, LedgerCapabilities, LedgerError, LoadCheckpointRequest, MarkInputAppliedRequest, MarkJoinedRequest, MarkReadyRequest, MarkUnknownRequest, ObservationOffset, OwnershipLost, OwnershipRenewal, OwnershipSnapshot, OwnershipToken, PersistedJson, ProducerEpoch, QueueSequence, RecoverySnapshot, RecoverySnapshotRequest, ReleaseChildBudgetRequest, ReleaseOwnershipRequest, RenewOwnershipRequest, ReservedChildBudget, ReservedSettlement, RevertJoiningRequest, SaveCheckpointRequest, ScheduleCapacityError, ScheduleChange, ScheduleConflict, ScheduleDueCursor, ScheduleFailpoint, ScheduleKey, ScheduleNotFound, ScheduleOwner, SchedulePageRequest, ScheduleRecord, ScheduleStorageError, ScheduleStore, Settlement, SettlementConflict, SettlementFinalization, SettlementReservation, SettlementReservationSnapshot, SubmissionLedger, SubmissionLookup, SubmissionLookupByKey, SubmissionSnapshot, SuspendRequest, SuspensionSnapshot, UnknownResolution, UnknownResolutionCommand, UnknownResolutionConflict, UnknownResolutionIntent, applyScheduleChange, compareScheduleKeys, compareScheduleNames, defaultSchedulingLimits, digestCanonicalBatch, scheduleDeadline, scheduleKeyOf, scheduleKeyString, scheduleOwnerKey, scheduleUsesCapacity, settlementFailureFromRecord } from "@effect-agent/session";
|
|
3
|
+
import { Clock, Crypto, DateTime, Duration, Effect, Encoding, Layer, Option, PubSub, Ref, Result, Schema, Stream } from "effect";
|
|
4
4
|
//#region src/memory-ledger.ts
|
|
5
5
|
const MAX_SUBMISSIONS = 65536;
|
|
6
6
|
/**
|
|
@@ -992,6 +992,164 @@ const makeSubmissionLedger = (options = {}) => Effect.gen(function* () {
|
|
|
992
992
|
const memorySubmissionLedgerLayer = (options = {}) => Layer.effect(SubmissionLedger, makeSubmissionLedger(options));
|
|
993
993
|
const MemorySubmissionLedgerLive = memorySubmissionLedgerLayer();
|
|
994
994
|
//#endregion
|
|
995
|
+
//#region src/memory-schedule-store.ts
|
|
996
|
+
const storageError = (operation, reason) => ScheduleStorageError.make({
|
|
997
|
+
operation,
|
|
998
|
+
reason
|
|
999
|
+
});
|
|
1000
|
+
const encodeRecord = (operation, record) => Effect.try({
|
|
1001
|
+
try: () => Schema.encodeSync(Schema.fromJsonString(ScheduleRecord))(record),
|
|
1002
|
+
catch: () => storageError(operation, "corrupt")
|
|
1003
|
+
});
|
|
1004
|
+
const decodeRecord = (operation, encoded) => Effect.try({
|
|
1005
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(encoded),
|
|
1006
|
+
catch: () => storageError(operation, "corrupt")
|
|
1007
|
+
});
|
|
1008
|
+
const decodeInput = (operation, schema, value) => Effect.try({
|
|
1009
|
+
try: () => Schema.decodeUnknownSync(schema)(value),
|
|
1010
|
+
catch: () => storageError(operation, "corrupt")
|
|
1011
|
+
});
|
|
1012
|
+
const sameOwner = (record, owner) => scheduleOwnerKey(record.owner) === scheduleOwnerKey(owner);
|
|
1013
|
+
const makeScheduleStore = Effect.gen(function* () {
|
|
1014
|
+
const state = yield* Ref.make({ records: /* @__PURE__ */ new Map() });
|
|
1015
|
+
const failpoint = yield* ScheduleFailpoint;
|
|
1016
|
+
const insert = Effect.fn("MemoryScheduleStore.insert")((record, ownerLimit) => Effect.gen(function* () {
|
|
1017
|
+
const encoded = yield* encodeRecord("insert", record);
|
|
1018
|
+
yield* failpoint.hit("schedule:insert:before");
|
|
1019
|
+
const decision = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
|
|
1020
|
+
const key = scheduleKeyString(record);
|
|
1021
|
+
const existingText = current.records.get(key);
|
|
1022
|
+
if (existingText !== void 0) {
|
|
1023
|
+
const decoded = Result.try({
|
|
1024
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(existingText),
|
|
1025
|
+
catch: () => storageError("insert", "corrupt")
|
|
1026
|
+
});
|
|
1027
|
+
if (Result.isFailure(decoded)) return [Result.fail(decoded.failure), current];
|
|
1028
|
+
if (decoded.success.creationFingerprint === record.creationFingerprint) return [Result.succeed(decoded.success), current];
|
|
1029
|
+
return [Result.fail(ScheduleConflict.make({
|
|
1030
|
+
reason: "creation",
|
|
1031
|
+
key: scheduleKeyOf(record)
|
|
1032
|
+
})), current];
|
|
1033
|
+
}
|
|
1034
|
+
let ownerCount = 0;
|
|
1035
|
+
for (const text of current.records.values()) {
|
|
1036
|
+
const decoded = Result.try({
|
|
1037
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(text),
|
|
1038
|
+
catch: () => storageError("insert", "corrupt")
|
|
1039
|
+
});
|
|
1040
|
+
if (Result.isFailure(decoded)) return [Result.fail(decoded.failure), current];
|
|
1041
|
+
if (sameOwner(decoded.success, record.owner) && scheduleUsesCapacity(decoded.success)) ownerCount += 1;
|
|
1042
|
+
}
|
|
1043
|
+
if (ownerCount >= ownerLimit) return [Result.fail(ScheduleCapacityError.make({ limit: ownerLimit })), current];
|
|
1044
|
+
const records = new Map(current.records);
|
|
1045
|
+
records.set(key, encoded);
|
|
1046
|
+
const next = { records };
|
|
1047
|
+
return [Result.succeed(record), next];
|
|
1048
|
+
}));
|
|
1049
|
+
const inserted = yield* Effect.fromResult(decision);
|
|
1050
|
+
yield* failpoint.hit("schedule:insert:after");
|
|
1051
|
+
return yield* decodeRecord("insert", yield* encodeRecord("insert", inserted));
|
|
1052
|
+
}));
|
|
1053
|
+
const get = Effect.fn("MemoryScheduleStore.get")(function* (key) {
|
|
1054
|
+
const decodedKey = yield* decodeInput("get", ScheduleKey, key);
|
|
1055
|
+
const text = (yield* Ref.get(state)).records.get(scheduleKeyString(decodedKey));
|
|
1056
|
+
return text === void 0 ? null : yield* decodeRecord("get", text);
|
|
1057
|
+
});
|
|
1058
|
+
const list = Effect.fn("MemoryScheduleStore.list")(function* (request) {
|
|
1059
|
+
const decodedRequest = yield* decodeInput("list", SchedulePageRequest, request);
|
|
1060
|
+
const records = [];
|
|
1061
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
1062
|
+
const record = yield* decodeRecord("list", text);
|
|
1063
|
+
if (sameOwner(record, decodedRequest.owner) && (decodedRequest.after === void 0 || compareScheduleNames(record.scheduleId, decodedRequest.after) > 0)) records.push(record);
|
|
1064
|
+
}
|
|
1065
|
+
records.sort((left, right) => compareScheduleNames(left.scheduleId, right.scheduleId));
|
|
1066
|
+
const hasNext = records.length > decodedRequest.limit;
|
|
1067
|
+
const items = records.slice(0, decodedRequest.limit);
|
|
1068
|
+
return {
|
|
1069
|
+
items,
|
|
1070
|
+
next: hasNext ? items.at(-1)?.scheduleId ?? null : null
|
|
1071
|
+
};
|
|
1072
|
+
});
|
|
1073
|
+
const change = Effect.fn("MemoryScheduleStore.change")((key, command, ownerLimit = defaultSchedulingLimits.maxSchedulesPerOwner) => Effect.gen(function* () {
|
|
1074
|
+
const decodedKey = yield* decodeInput("change", ScheduleKey, key);
|
|
1075
|
+
const decodedCommand = yield* decodeInput("change", ScheduleChange, command);
|
|
1076
|
+
yield* failpoint.hit(`schedule:${decodedCommand._tag.toLowerCase()}:before`);
|
|
1077
|
+
const decision = yield* Effect.uninterruptible(Ref.modify(state, (current) => {
|
|
1078
|
+
const storageKey = scheduleKeyString(decodedKey);
|
|
1079
|
+
const text = current.records.get(storageKey);
|
|
1080
|
+
if (text === void 0) return [Result.fail(ScheduleNotFound.make({ key: decodedKey })), current];
|
|
1081
|
+
const decoded = Result.try({
|
|
1082
|
+
try: () => Schema.decodeSync(Schema.fromJsonString(ScheduleRecord))(text),
|
|
1083
|
+
catch: () => storageError("change", "corrupt")
|
|
1084
|
+
});
|
|
1085
|
+
if (Result.isFailure(decoded)) return [Result.fail(decoded.failure), current];
|
|
1086
|
+
const applied = applyScheduleChange(decoded.success, decodedCommand);
|
|
1087
|
+
if (Result.isFailure(applied)) return [Result.fail(applied.failure), current];
|
|
1088
|
+
if (applied.success === decoded.success) return [Result.succeed(decoded.success), current];
|
|
1089
|
+
if (!scheduleUsesCapacity(decoded.success) && scheduleUsesCapacity(applied.success)) {
|
|
1090
|
+
let count = 0;
|
|
1091
|
+
for (const text of current.records.values()) {
|
|
1092
|
+
const candidate = Schema.decodeUnknownResult(Schema.fromJsonString(ScheduleRecord))(text);
|
|
1093
|
+
if (Result.isFailure(candidate)) return [Result.fail(storageError("change", "corrupt")), current];
|
|
1094
|
+
if (sameOwner(candidate.success, decodedKey.owner) && scheduleUsesCapacity(candidate.success)) count += 1;
|
|
1095
|
+
}
|
|
1096
|
+
if (count >= ownerLimit) return [Result.fail(ScheduleCapacityError.make({ limit: ownerLimit })), current];
|
|
1097
|
+
}
|
|
1098
|
+
const encoded = Result.try({
|
|
1099
|
+
try: () => Schema.encodeSync(Schema.fromJsonString(ScheduleRecord))(applied.success),
|
|
1100
|
+
catch: () => storageError("change", "corrupt")
|
|
1101
|
+
});
|
|
1102
|
+
if (Result.isFailure(encoded)) return [Result.fail(encoded.failure), current];
|
|
1103
|
+
const records = new Map(current.records);
|
|
1104
|
+
records.set(storageKey, encoded.success);
|
|
1105
|
+
const next = { records };
|
|
1106
|
+
return [Result.succeed(applied.success), next];
|
|
1107
|
+
}));
|
|
1108
|
+
const changed = yield* Effect.fromResult(decision);
|
|
1109
|
+
yield* failpoint.hit(`schedule:${decodedCommand._tag.toLowerCase()}:after`);
|
|
1110
|
+
return yield* decodeRecord("change", yield* encodeRecord("change", changed));
|
|
1111
|
+
}));
|
|
1112
|
+
const due = Effect.fn("MemoryScheduleStore.due")(function* (nowMillis, limit, owner, after) {
|
|
1113
|
+
const decodedOwner = owner === void 0 ? void 0 : yield* decodeInput("due", ScheduleOwner, owner);
|
|
1114
|
+
const cursor = after === void 0 ? void 0 : yield* decodeInput("due", ScheduleDueCursor, after);
|
|
1115
|
+
const records = [];
|
|
1116
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
1117
|
+
const record = yield* decodeRecord("due", text);
|
|
1118
|
+
const deadline = scheduleDeadline(record);
|
|
1119
|
+
if (deadline !== null && deadline <= nowMillis && (decodedOwner === void 0 || sameOwner(record, decodedOwner)) && (cursor === void 0 || deadline > cursor.deadlineAtMillis || deadline === cursor.deadlineAtMillis && compareScheduleKeys(record, cursor) > 0)) records.push({
|
|
1120
|
+
...scheduleKeyOf(record),
|
|
1121
|
+
deadlineAtMillis: deadline
|
|
1122
|
+
});
|
|
1123
|
+
}
|
|
1124
|
+
records.sort((left, right) => {
|
|
1125
|
+
const byDeadline = left.deadlineAtMillis - right.deadlineAtMillis;
|
|
1126
|
+
return byDeadline !== 0 ? byDeadline : compareScheduleKeys(left, right);
|
|
1127
|
+
});
|
|
1128
|
+
return records.slice(0, limit);
|
|
1129
|
+
});
|
|
1130
|
+
const nextDeadline = Effect.fn("MemoryScheduleStore.nextDeadline")(function* (owner) {
|
|
1131
|
+
const decodedOwner = owner === void 0 ? void 0 : yield* decodeInput("nextDeadline", ScheduleOwner, owner);
|
|
1132
|
+
let earliest = null;
|
|
1133
|
+
for (const text of (yield* Ref.get(state)).records.values()) {
|
|
1134
|
+
const record = yield* decodeRecord("nextDeadline", text);
|
|
1135
|
+
if (decodedOwner !== void 0 && !sameOwner(record, decodedOwner)) continue;
|
|
1136
|
+
const deadline = scheduleDeadline(record);
|
|
1137
|
+
if (deadline !== null && (earliest === null || deadline < earliest)) earliest = deadline;
|
|
1138
|
+
}
|
|
1139
|
+
return earliest;
|
|
1140
|
+
});
|
|
1141
|
+
return ScheduleStore.of({
|
|
1142
|
+
insert,
|
|
1143
|
+
get,
|
|
1144
|
+
list,
|
|
1145
|
+
change,
|
|
1146
|
+
due,
|
|
1147
|
+
nextDeadline
|
|
1148
|
+
});
|
|
1149
|
+
});
|
|
1150
|
+
const memoryScheduleStoreLayer = () => Layer.effect(ScheduleStore, makeScheduleStore);
|
|
1151
|
+
const MemoryScheduleStoreLive = memoryScheduleStoreLayer();
|
|
1152
|
+
//#endregion
|
|
995
1153
|
//#region src/memory-storage.ts
|
|
996
1154
|
const MAX_CONVERSATIONS = 256;
|
|
997
1155
|
const MAX_RECORDS_PER_CONVERSATION = 65536;
|
|
@@ -1292,6 +1450,6 @@ const MemoryConversationStoreLive = Layer.effect(ConversationStore, makeConversa
|
|
|
1292
1450
|
*/
|
|
1293
1451
|
const MemoryStorageLive = MemoryConversationStoreLive;
|
|
1294
1452
|
//#endregion
|
|
1295
|
-
export { MemoryConversationStoreLive, MemoryStorageLive, MemorySubmissionLedgerLive, memorySubmissionLedgerLayer };
|
|
1453
|
+
export { MemoryConversationStoreLive, MemoryScheduleStoreLive, MemoryStorageLive, MemorySubmissionLedgerLive, memoryScheduleStoreLayer, memorySubmissionLedgerLayer };
|
|
1296
1454
|
|
|
1297
1455
|
//# sourceMappingURL=index.mjs.map
|