@defai.digital/memory-domain 13.0.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/LICENSE ADDED
@@ -0,0 +1,214 @@
1
+ AutomatosX Open Source Edition - Apache License 2.0
2
+
3
+ This license applies ONLY to the AutomatosX Open Source Edition code
4
+ published on GitHub at https://github.com/defai-digital/automatosx
5
+
6
+ This license does NOT apply to:
7
+ - AutomatosX Pro Edition (proprietary software - separate EULA)
8
+ - AutomatosX Enterprise Edition (proprietary software - separate agreement)
9
+
10
+ For commercial editions, see COMMERCIAL-LICENSE.md
11
+
12
+ ===============================================================================
13
+
14
+ Apache License
15
+ Version 2.0, January 2004
16
+ http://www.apache.org/licenses/
17
+
18
+ TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
19
+
20
+ 1. Definitions.
21
+
22
+ "License" shall mean the terms and conditions for use, reproduction,
23
+ and distribution as defined by Sections 1 through 9 of this document.
24
+
25
+ "Licensor" shall mean the copyright owner or entity authorized by
26
+ the copyright owner that is granting the License.
27
+
28
+ "Legal Entity" shall mean the union of the acting entity and all
29
+ other entities that control, are controlled by, or are under common
30
+ control with that entity. For the purposes of this definition,
31
+ "control" means (i) the power, direct or indirect, to cause the
32
+ direction or management of such entity, whether by contract or
33
+ otherwise, or (ii) ownership of fifty percent (50%) or more of the
34
+ outstanding shares, or (iii) beneficial ownership of such entity.
35
+
36
+ "You" (or "Your") shall mean an individual or Legal Entity
37
+ exercising permissions granted by this License.
38
+
39
+ "Source" form shall mean the preferred form for making modifications,
40
+ including but not limited to software source code, documentation
41
+ source, and configuration files.
42
+
43
+ "Object" form shall mean any form resulting from mechanical
44
+ transformation or translation of a Source form, including but
45
+ not limited to compiled object code, generated documentation,
46
+ and conversions to other media types.
47
+
48
+ "Work" shall mean the work of authorship, whether in Source or
49
+ Object form, made available under the License, as indicated by a
50
+ copyright notice that is included in or attached to the work
51
+ (an example is provided in the Appendix below).
52
+
53
+ "Derivative Works" shall mean any work, whether in Source or Object
54
+ form, that is based on (or derived from) the Work and for which the
55
+ editorial revisions, annotations, elaborations, or other modifications
56
+ represent, as a whole, an original work of authorship. For the purposes
57
+ of this License, Derivative Works shall not include works that remain
58
+ separable from, or merely link (or bind by name) to the interfaces of,
59
+ the Work and Derivative Works thereof.
60
+
61
+ "Contribution" shall mean any work of authorship, including
62
+ the original version of the Work and any modifications or additions
63
+ to that Work or Derivative Works thereof, that is intentionally
64
+ submitted to Licensor for inclusion in the Work by the copyright owner
65
+ or by an individual or Legal Entity authorized to submit on behalf of
66
+ the copyright owner. For the purposes of this definition, "submitted"
67
+ means any form of electronic, verbal, or written communication sent
68
+ to the Licensor or its representatives, including but not limited to
69
+ communication on electronic mailing lists, source code control systems,
70
+ and issue tracking systems that are managed by, or on behalf of, the
71
+ Licensor for the purpose of discussing and improving the Work, but
72
+ excluding communication that is conspicuously marked or otherwise
73
+ designated in writing by the copyright owner as "Not a Contribution."
74
+
75
+ "Contributor" shall mean Licensor and any individual or Legal Entity
76
+ on behalf of whom a Contribution has been received by Licensor and
77
+ subsequently incorporated within the Work.
78
+
79
+ 2. Grant of Copyright License. Subject to the terms and conditions of
80
+ this License, each Contributor hereby grants to You a perpetual,
81
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
82
+ copyright license to reproduce, prepare Derivative Works of,
83
+ publicly display, publicly perform, sublicense, and distribute the
84
+ Work and such Derivative Works in Source or Object form.
85
+
86
+ 3. Grant of Patent License. Subject to the terms and conditions of
87
+ this License, each Contributor hereby grants to You a perpetual,
88
+ worldwide, non-exclusive, no-charge, royalty-free, irrevocable
89
+ (except as stated in this section) patent license to make, have made,
90
+ use, offer to sell, sell, import, and otherwise transfer the Work,
91
+ where such license applies only to those patent claims licensable
92
+ by such Contributor that are necessarily infringed by their
93
+ Contribution(s) alone or by combination of their Contribution(s)
94
+ with the Work to which such Contribution(s) was submitted. If You
95
+ institute patent litigation against any entity (including a
96
+ cross-claim or counterclaim in a lawsuit) alleging that the Work
97
+ or a Contribution incorporated within the Work constitutes direct
98
+ or contributory patent infringement, then any patent licenses
99
+ granted to You under this License for that Work shall terminate
100
+ as of the date such litigation is filed.
101
+
102
+ 4. Redistribution. You may reproduce and distribute copies of the
103
+ Work or Derivative Works thereof in any medium, with or without
104
+ modifications, and in Source or Object form, provided that You
105
+ meet the following conditions:
106
+
107
+ (a) You must give any other recipients of the Work or
108
+ Derivative Works a copy of this License; and
109
+
110
+ (b) You must cause any modified files to carry prominent notices
111
+ stating that You changed the files; and
112
+
113
+ (c) You must retain, in the Source form of any Derivative Works
114
+ that You distribute, all copyright, patent, trademark, and
115
+ attribution notices from the Source form of the Work,
116
+ excluding those notices that do not pertain to any part of
117
+ the Derivative Works; and
118
+
119
+ (d) If the Work includes a "NOTICE" text file as part of its
120
+ distribution, then any Derivative Works that You distribute must
121
+ include a readable copy of the attribution notices contained
122
+ within such NOTICE file, excluding those notices that do not
123
+ pertain to any part of the Derivative Works, in at least one
124
+ of the following places: within a NOTICE text file distributed
125
+ as part of the Derivative Works; within the Source form or
126
+ documentation, if provided along with the Derivative Works; or,
127
+ within a display generated by the Derivative Works, if and
128
+ wherever such third-party notices normally appear. The contents
129
+ of the NOTICE file are for informational purposes only and
130
+ do not modify the License. You may add Your own attribution
131
+ notices within Derivative Works that You distribute, alongside
132
+ or as an addendum to the NOTICE text from the Work, provided
133
+ that such additional attribution notices cannot be construed
134
+ as modifying the License.
135
+
136
+ You may add Your own copyright statement to Your modifications and
137
+ may provide additional or different license terms and conditions
138
+ for use, reproduction, or distribution of Your modifications, or
139
+ for any such Derivative Works as a whole, provided Your use,
140
+ reproduction, and distribution of the Work otherwise complies with
141
+ the conditions stated in this License.
142
+
143
+ 5. Submission of Contributions. Unless You explicitly state otherwise,
144
+ any Contribution intentionally submitted for inclusion in the Work
145
+ by You to the Licensor shall be under the terms and conditions of
146
+ this License, without any additional terms or conditions.
147
+ Notwithstanding the above, nothing herein shall supersede or modify
148
+ the terms of any separate license agreement you may have executed
149
+ with Licensor regarding such Contributions.
150
+
151
+ 6. Trademarks. This License does not grant permission to use the trade
152
+ names, trademarks, service marks, or product names of the Licensor,
153
+ except as required for reasonable and customary use in describing the
154
+ origin of the Work and reproducing the content of the NOTICE file.
155
+
156
+ 7. Disclaimer of Warranty. Unless required by applicable law or
157
+ agreed to in writing, Licensor provides the Work (and each
158
+ Contributor provides its Contributions) on an "AS IS" BASIS,
159
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
160
+ implied, including, without limitation, any warranties or conditions
161
+ of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
162
+ PARTICULAR PURPOSE. You are solely responsible for determining the
163
+ appropriateness of using or redistributing the Work and assume any
164
+ risks associated with Your exercise of permissions under this License.
165
+
166
+ 8. Limitation of Liability. In no event and under no legal theory,
167
+ whether in tort (including negligence), contract, or otherwise,
168
+ unless required by applicable law (such as deliberate and grossly
169
+ negligent acts) or agreed to in writing, shall any Contributor be
170
+ liable to You for damages, including any direct, indirect, special,
171
+ incidental, or consequential damages of any character arising as a
172
+ result of this License or out of the use or inability to use the
173
+ Work (including but not limited to damages for loss of goodwill,
174
+ work stoppage, computer failure or malfunction, or any and all
175
+ other commercial damages or losses), even if such Contributor
176
+ has been advised of the possibility of such damages.
177
+
178
+ 9. Accepting Warranty or Additional Liability. While redistributing
179
+ the Work or Derivative Works thereof, You may choose to offer,
180
+ and charge a fee for, acceptance of support, warranty, indemnity,
181
+ or other liability obligations and/or rights consistent with this
182
+ License. However, in accepting such obligations, You may act only
183
+ on Your own behalf and on Your sole responsibility, not on behalf
184
+ of any other Contributor, and only if You agree to indemnify,
185
+ defend, and hold each Contributor harmless for any liability
186
+ incurred by, or claims asserted against, such Contributor by reason
187
+ of your accepting any such warranty or additional liability.
188
+
189
+ END OF TERMS AND CONDITIONS
190
+
191
+ APPENDIX: How to apply the Apache License to your work.
192
+
193
+ To apply the Apache License to your work, attach the following
194
+ boilerplate notice, with the fields enclosed by brackets "[]"
195
+ replaced with your own identifying information. (Don't include
196
+ the brackets!) The text should be enclosed in the appropriate
197
+ comment syntax for the file format. We also recommend that a
198
+ file or class name and description of purpose be included on the
199
+ same "printed page" as the copyright notice for easier
200
+ identification within third-party archives.
201
+
202
+ Copyright 2024-2025 DEFAI Private Limited
203
+
204
+ Licensed under the Apache License, Version 2.0 (the "License");
205
+ you may not use this file except in compliance with the License.
206
+ You may obtain a copy of the License at
207
+
208
+ http://www.apache.org/licenses/LICENSE-2.0
209
+
210
+ Unless required by applicable law or agreed to in writing, software
211
+ distributed under the License is distributed on an "AS IS" BASIS,
212
+ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
213
+ See the License for the specific language governing permissions and
214
+ limitations under the License.
@@ -0,0 +1,31 @@
1
+ import type { MemoryEvent } from '@defai.digital/contracts';
2
+ import type { EventStore, AggregateState, EventHandler } from './types.js';
3
+ /**
4
+ * Aggregate repository for event-sourced aggregates
5
+ * INV-MEM-002: Replaying events produces identical results
6
+ */
7
+ export declare class AggregateRepository<T> {
8
+ private readonly eventStore;
9
+ private readonly initialState;
10
+ private readonly eventHandler;
11
+ constructor(eventStore: EventStore, initialState: () => T, eventHandler: EventHandler<T>);
12
+ /**
13
+ * Loads an aggregate by replaying its events
14
+ * INV-MEM-002: Replay consistency - same events produce same state
15
+ */
16
+ load(aggregateId: string): Promise<AggregateState<T>>;
17
+ /**
18
+ * Saves an event to the aggregate
19
+ * INV-MEM-004: Event ordering enforced via version
20
+ */
21
+ save(aggregateId: string, event: MemoryEvent, expectedVersion?: number): Promise<void>;
22
+ /**
23
+ * Gets an aggregate at a specific version (point-in-time query)
24
+ */
25
+ loadAtVersion(aggregateId: string, version: number): Promise<AggregateState<T>>;
26
+ }
27
+ /**
28
+ * Creates an aggregate repository
29
+ */
30
+ export declare function createAggregateRepository<T>(eventStore: EventStore, initialState: () => T, eventHandler: EventHandler<T>): AggregateRepository<T>;
31
+ //# sourceMappingURL=aggregate.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aggregate.d.ts","sourceRoot":"","sources":["../src/aggregate.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,MAAM,0BAA0B,CAAC;AAC5D,OAAO,KAAK,EACV,UAAU,EACV,cAAc,EACd,YAAY,EACb,MAAM,YAAY,CAAC;AAIpB;;;GAGG;AACH,qBAAa,mBAAmB,CAAC,CAAC;IAE9B,OAAO,CAAC,QAAQ,CAAC,UAAU;IAC3B,OAAO,CAAC,QAAQ,CAAC,YAAY;IAC7B,OAAO,CAAC,QAAQ,CAAC,YAAY;gBAFZ,UAAU,EAAE,UAAU,EACtB,YAAY,EAAE,MAAM,CAAC,EACrB,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC;IAGhD;;;OAGG;IACG,IAAI,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;IA6B3D;;;OAGG;IACG,IAAI,CACR,WAAW,EAAE,MAAM,EACnB,KAAK,EAAE,WAAW,EAClB,eAAe,CAAC,EAAE,MAAM,GACvB,OAAO,CAAC,IAAI,CAAC;IAgBhB;;OAEG;IACG,aAAa,CACjB,WAAW,EAAE,MAAM,EACnB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,cAAc,CAAC,CAAC,CAAC,CAAC;CAiC9B;AAED;;GAEG;AACH,wBAAgB,yBAAyB,CAAC,CAAC,EACzC,UAAU,EAAE,UAAU,EACtB,YAAY,EAAE,MAAM,CAAC,EACrB,YAAY,EAAE,YAAY,CAAC,CAAC,CAAC,GAC5B,mBAAmB,CAAC,CAAC,CAAC,CAExB"}
@@ -0,0 +1,94 @@
1
+ import { MemoryErrorCodes } from './types.js';
2
+ import { EventStoreError } from './event-store.js';
3
+ /**
4
+ * Aggregate repository for event-sourced aggregates
5
+ * INV-MEM-002: Replaying events produces identical results
6
+ */
7
+ export class AggregateRepository {
8
+ eventStore;
9
+ initialState;
10
+ eventHandler;
11
+ constructor(eventStore, initialState, eventHandler) {
12
+ this.eventStore = eventStore;
13
+ this.initialState = initialState;
14
+ this.eventHandler = eventHandler;
15
+ }
16
+ /**
17
+ * Loads an aggregate by replaying its events
18
+ * INV-MEM-002: Replay consistency - same events produce same state
19
+ */
20
+ async load(aggregateId) {
21
+ const events = await this.eventStore.getEvents(aggregateId);
22
+ if (events.length === 0) {
23
+ return {
24
+ aggregateId,
25
+ version: 0,
26
+ state: this.initialState(),
27
+ };
28
+ }
29
+ // Replay events to reconstruct state (INV-MEM-002)
30
+ let state = this.initialState();
31
+ let lastEvent;
32
+ for (const event of events) {
33
+ state = this.eventHandler(state, event);
34
+ lastEvent = event;
35
+ }
36
+ return {
37
+ aggregateId,
38
+ version: lastEvent?.version ?? events.length,
39
+ state,
40
+ lastEventId: lastEvent?.eventId,
41
+ lastTimestamp: lastEvent?.timestamp,
42
+ };
43
+ }
44
+ /**
45
+ * Saves an event to the aggregate
46
+ * INV-MEM-004: Event ordering enforced via version
47
+ */
48
+ async save(aggregateId, event, expectedVersion) {
49
+ // Optimistic concurrency check
50
+ if (expectedVersion !== undefined) {
51
+ const currentVersion = await this.eventStore.getVersion(aggregateId);
52
+ if (currentVersion !== expectedVersion) {
53
+ throw new EventStoreError(MemoryErrorCodes.VERSION_CONFLICT, `Optimistic concurrency failure: expected version ${String(expectedVersion)}, current is ${String(currentVersion)}`, { expected: expectedVersion, current: currentVersion });
54
+ }
55
+ }
56
+ await this.eventStore.append(event);
57
+ }
58
+ /**
59
+ * Gets an aggregate at a specific version (point-in-time query)
60
+ */
61
+ async loadAtVersion(aggregateId, version) {
62
+ const events = await this.eventStore.getEvents(aggregateId);
63
+ // Take only events up to the requested version
64
+ const eventsToReplay = events.filter((e) => e.version !== undefined && e.version <= version);
65
+ if (eventsToReplay.length === 0) {
66
+ return {
67
+ aggregateId,
68
+ version: 0,
69
+ state: this.initialState(),
70
+ };
71
+ }
72
+ // Replay events (INV-MEM-002)
73
+ let state = this.initialState();
74
+ let lastEvent;
75
+ for (const event of eventsToReplay) {
76
+ state = this.eventHandler(state, event);
77
+ lastEvent = event;
78
+ }
79
+ return {
80
+ aggregateId,
81
+ version: lastEvent?.version ?? eventsToReplay.length,
82
+ state,
83
+ lastEventId: lastEvent?.eventId,
84
+ lastTimestamp: lastEvent?.timestamp,
85
+ };
86
+ }
87
+ }
88
+ /**
89
+ * Creates an aggregate repository
90
+ */
91
+ export function createAggregateRepository(eventStore, initialState, eventHandler) {
92
+ return new AggregateRepository(eventStore, initialState, eventHandler);
93
+ }
94
+ //# sourceMappingURL=aggregate.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"aggregate.js","sourceRoot":"","sources":["../src/aggregate.ts"],"names":[],"mappings":"AAMA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAC9C,OAAO,EAAE,eAAe,EAAE,MAAM,kBAAkB,CAAC;AAEnD;;;GAGG;AACH,MAAM,OAAO,mBAAmB;IAEX;IACA;IACA;IAHnB,YACmB,UAAsB,EACtB,YAAqB,EACrB,YAA6B;QAF7B,eAAU,GAAV,UAAU,CAAY;QACtB,iBAAY,GAAZ,YAAY,CAAS;QACrB,iBAAY,GAAZ,YAAY,CAAiB;IAC7C,CAAC;IAEJ;;;OAGG;IACH,KAAK,CAAC,IAAI,CAAC,WAAmB;QAC5B,MAAM,MAAM,GAAG,MAAM,IAAI,CAAC,UAAU,CAAC,SAAS,CAAC,WAAW,CAAC,CAAC;QAE5D,IAAI,MAAM,CAAC,MAAM,KAAK,CAAC,EAAE,CAAC;YACxB,OAAO;gBACL,WAAW;gBACX,OAAO,EAAE,CAAC;gBACV,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE;aAC3B,CAAC;QACJ,CAAC;QAED,mDAAmD;QACnD,IAAI,KAAK,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,IAAI,SAAkC,CAAC;QAEvC,KAAK,MAAM,KAAK,IAAI,MAAM,EAAE,CAAC;YAC3B,KA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AChC,OAAO;gBACL,WAAW;gBACX,OAAO,EAAE,CAAC;gBACV,KAAK,EAAE,IAAI,CAAC,YAAY,EAAE;aAC3B,CAAC;QACJ,CAAC;QAED,8BAA8B;QAC9B,IAAI,KAAK,GAAG,IAAI,CAAC,YAAY,EAAE,CAAC;QAChC,IAAI,SAAkC,CAAC;QAEvC,KAAK,MAAM,KAAK,IAAI,cAAc,EAAE,CAAC;YACnC,KAAK,GAAG,IAAI,CAAC,YAAY,CAAC,KAAK,EAAE,KAAK,CAAC,CAAC;YACxC,SAAS,GAAG,KAAK,CAAC;QACpB,CAAC;QAED,OAAO;YACL,WAAW;YACX,OAAO,EAAE,SAAS,EAAE,OAAO,IAAI,cAAc,CAAC,MAAM;YACpD,KAAK;YACL,WAAW,EAAE,SAAS,EAAE,OAAO;YAC/B,aAAa,EAAE,SAAS,EAAE,SAAS;SACpC,CAAC;IACJ,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,yBAAyB,CACvC,UAAsB,EACtB,YAAqB,EACrB,YAA6B;IAE7B,OAAO,IAAI,mBAAmB,CAAC,UAAU,EAAE,YAAY,EAAE,YAAY,CAAC,CAAC;AACzE,CAAC"}
@@ -0,0 +1,39 @@
1
+ import type { EventStore, ConversationState, Message } from './types.js';
2
+ /**
3
+ * Conversation aggregate for managing conversation state
4
+ */
5
+ export declare class ConversationAggregate {
6
+ private readonly repository;
7
+ constructor(eventStore: EventStore);
8
+ /**
9
+ * Creates a new conversation
10
+ */
11
+ create(conversationId: string, options?: {
12
+ title?: string;
13
+ initialContext?: Record<string, unknown>;
14
+ }): Promise<ConversationState>;
15
+ /**
16
+ * Adds a message to a conversation
17
+ */
18
+ addMessage(conversationId: string, message: Omit<Message, 'timestamp'>): Promise<ConversationState>;
19
+ /**
20
+ * Updates conversation metadata
21
+ */
22
+ update(conversationId: string, updates: {
23
+ title?: string;
24
+ context?: Record<string, unknown>;
25
+ }): Promise<ConversationState>;
26
+ /**
27
+ * Gets a conversation by ID
28
+ */
29
+ get(conversationId: string): Promise<ConversationState>;
30
+ /**
31
+ * Gets a conversation at a specific version
32
+ */
33
+ getAtVersion(conversationId: string, version: number): Promise<ConversationState>;
34
+ }
35
+ /**
36
+ * Creates a conversation aggregate
37
+ */
38
+ export declare function createConversationAggregate(eventStore: EventStore): ConversationAggregate;
39
+ //# sourceMappingURL=conversation.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.d.ts","sourceRoot":"","sources":["../src/conversation.ts"],"names":[],"mappings":"AACA,OAAO,KAAK,EACV,UAAU,EACV,iBAAiB,EACjB,OAAO,EAER,MAAM,YAAY,CAAC;AA+EpB;;GAEG;AACH,qBAAa,qBAAqB;IAChC,OAAO,CAAC,QAAQ,CAAC,UAAU,CAAyC;gBAExD,UAAU,EAAE,UAAU;IAQlC;;OAEG;IACG,MAAM,CACV,cAAc,EAAE,MAAM,EACtB,OAAO,CAAC,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,cAAc,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GACrE,OAAO,CAAC,iBAAiB,CAAC;IAkB7B;;OAEG;IACG,UAAU,CACd,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,IAAI,CAAC,OAAO,EAAE,WAAW,CAAC,GAClC,OAAO,CAAC,iBAAiB,CAAC;IAqB7B;;OAEG;IACG,MAAM,CACV,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE;QAAE,KAAK,CAAC,EAAE,MAAM,CAAC;QAAC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,CAAA;KAAE,GAC7D,OAAO,CAAC,iBAAiB,CAAC;IAgB7B;;OAEG;IACG,GAAG,CAAC,cAAc,EAAE,MAAM,GAAG,OAAO,CAAC,iBAAiB,CAAC;IAI7D;;OAEG;IACG,YAAY,CAChB,cAAc,EAAE,MAAM,EACtB,OAAO,EAAE,MAAM,GACd,OAAO,CAAC,iBAAiB,CAAC;CAG9B;AAED;;GAEG;AACH,wBAAgB,2BAA2B,CACzC,UAAU,EAAE,UAAU,GACrB,qBAAqB,CAEvB"}
@@ -0,0 +1,130 @@
1
+ import { createMemoryEvent } from '@defai.digital/contracts';
2
+ import { AggregateRepository } from './aggregate.js';
3
+ /**
4
+ * Initial state for a conversation
5
+ */
6
+ function initialConversationState() {
7
+ return {
8
+ conversationId: '',
9
+ messages: [],
10
+ context: {},
11
+ };
12
+ }
13
+ /**
14
+ * Event handler for conversation events
15
+ * INV-MEM-002: Pure function - no external state reads
16
+ */
17
+ const conversationEventHandler = (state, event) => {
18
+ switch (event.type) {
19
+ case 'conversation.created': {
20
+ const payload = event.payload;
21
+ return {
22
+ ...state,
23
+ conversationId: payload.conversationId,
24
+ title: payload.title,
25
+ context: payload.initialContext ?? {},
26
+ createdAt: event.timestamp,
27
+ updatedAt: event.timestamp,
28
+ };
29
+ }
30
+ case 'conversation.updated': {
31
+ const payload = event.payload;
32
+ return {
33
+ ...state,
34
+ title: payload.title ?? state.title,
35
+ context: { ...state.context, ...payload.context },
36
+ updatedAt: event.timestamp,
37
+ };
38
+ }
39
+ case 'message.added': {
40
+ const payload = event.payload;
41
+ const message = {
42
+ messageId: payload.messageId,
43
+ role: payload.role,
44
+ content: payload.content,
45
+ tokenCount: payload.tokenCount,
46
+ timestamp: event.timestamp,
47
+ };
48
+ return {
49
+ ...state,
50
+ messages: [...state.messages, message],
51
+ updatedAt: event.timestamp,
52
+ };
53
+ }
54
+ default:
55
+ // Unknown event types are ignored (forward compatibility)
56
+ return state;
57
+ }
58
+ };
59
+ /**
60
+ * Conversation aggregate for managing conversation state
61
+ */
62
+ export class ConversationAggregate {
63
+ repository;
64
+ constructor(eventStore) {
65
+ this.repository = new AggregateRepository(eventStore, initialConversationState, conversationEventHandler);
66
+ }
67
+ /**
68
+ * Creates a new conversation
69
+ */
70
+ async create(conversationId, options) {
71
+ const event = createMemoryEvent('conversation.created', {
72
+ conversationId,
73
+ title: options?.title,
74
+ initialContext: options?.initialContext,
75
+ }, {
76
+ aggregateId: conversationId,
77
+ version: 1,
78
+ });
79
+ await this.repository.save(conversationId, event, 0);
80
+ return (await this.repository.load(conversationId)).state;
81
+ }
82
+ /**
83
+ * Adds a message to a conversation
84
+ */
85
+ async addMessage(conversationId, message) {
86
+ const current = await this.repository.load(conversationId);
87
+ const event = createMemoryEvent('message.added', {
88
+ messageId: message.messageId,
89
+ role: message.role,
90
+ content: message.content,
91
+ tokenCount: message.tokenCount,
92
+ }, {
93
+ aggregateId: conversationId,
94
+ version: current.version + 1,
95
+ });
96
+ await this.repository.save(conversationId, event, current.version);
97
+ return (await this.repository.load(conversationId)).state;
98
+ }
99
+ /**
100
+ * Updates conversation metadata
101
+ */
102
+ async update(conversationId, updates) {
103
+ const current = await this.repository.load(conversationId);
104
+ const event = createMemoryEvent('conversation.updated', updates, {
105
+ aggregateId: conversationId,
106
+ version: current.version + 1,
107
+ });
108
+ await this.repository.save(conversationId, event, current.version);
109
+ return (await this.repository.load(conversationId)).state;
110
+ }
111
+ /**
112
+ * Gets a conversation by ID
113
+ */
114
+ async get(conversationId) {
115
+ return (await this.repository.load(conversationId)).state;
116
+ }
117
+ /**
118
+ * Gets a conversation at a specific version
119
+ */
120
+ async getAtVersion(conversationId, version) {
121
+ return (await this.repository.loadAtVersion(conversationId, version)).state;
122
+ }
123
+ }
124
+ /**
125
+ * Creates a conversation aggregate
126
+ */
127
+ export function createConversationAggregate(eventStore) {
128
+ return new ConversationAggregate(eventStore);
129
+ }
130
+ //# sourceMappingURL=conversation.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"conversation.js","sourceRoot":"","sources":["../src/conversation.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,iBAAiB,EAAE,MAAM,0BAA0B,CAAC;AAO7D,OAAO,EAAE,mBAAmB,EAAE,MAAM,gBAAgB,CAAC;AAErD;;GAEG;AACH,SAAS,wBAAwB;IAC/B,OAAO;QACL,cAAc,EAAE,EA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0,0 +1,53 @@
1
+ import type { MemoryEvent, MemoryEventType } from '@defai.digital/contracts';
2
+ import type { EventStore } from './types.js';
3
+ /**
4
+ * Error thrown by event store operations
5
+ */
6
+ export declare class EventStoreError extends Error {
7
+ readonly code: string;
8
+ readonly details?: Record<string, unknown> | undefined;
9
+ constructor(code: string, message: string, details?: Record<string, unknown> | undefined);
10
+ }
11
+ /**
12
+ * In-memory event store implementation
13
+ * INV-MEM-001: Events are immutable - stored as frozen objects
14
+ * INV-MEM-004: Events are ordered by version within aggregate
15
+ */
16
+ export declare class InMemoryEventStore implements EventStore {
17
+ private readonly events;
18
+ private readonly allEvents;
19
+ private readonly correlationIndex;
20
+ /**
21
+ * Appends an event to the store
22
+ * INV-MEM-001: Events are immutable
23
+ * INV-MEM-004: Version ordering enforced
24
+ */
25
+ append(event: MemoryEvent): Promise<void>;
26
+ /**
27
+ * Gets all events for an aggregate
28
+ * INV-MEM-004: Events returned in version order
29
+ */
30
+ getEvents(aggregateId: string): Promise<MemoryEvent[]>;
31
+ /**
32
+ * Gets events by type
33
+ */
34
+ getEventsByType(type: MemoryEventType): Promise<MemoryEvent[]>;
35
+ /**
36
+ * Gets events by correlation ID
37
+ * INV-MEM-005: Support correlation tracing
38
+ */
39
+ getEventsByCorrelation(correlationId: string): Promise<MemoryEvent[]>;
40
+ /**
41
+ * Gets the current version for an aggregate
42
+ */
43
+ getVersion(aggregateId: string): Promise<number>;
44
+ /**
45
+ * Clears all events (for testing)
46
+ */
47
+ clear(): void;
48
+ }
49
+ /**
50
+ * Creates an in-memory event store
51
+ */
52
+ export declare function createInMemoryEventStore(): InMemoryEventStore;
53
+ //# sourceMappingURL=event-store.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-store.d.ts","sourceRoot":"","sources":["../src/event-store.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAC7E,OAAO,KAAK,EAAE,UAAU,EAAE,MAAM,YAAY,CAAC;AAG7C;;GAEG;AACH,qBAAa,eAAgB,SAAQ,KAAK;aAEtB,IAAI,EAAE,MAAM;aAEZ,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC;gBAFjC,IAAI,EAAE,MAAM,EAC5B,OAAO,EAAE,MAAM,EACC,OAAO,CAAC,EAAE,MAAM,CAAC,MAAM,EAAE,OAAO,CAAC,YAAA;CAKpD;AAED;;;;GAIG;AACH,qBAAa,kBAAmB,YAAW,UAAU;IACnD,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAoC;IAC3D,OAAO,CAAC,QAAQ,CAAC,SAAS,CAAqB;IAC/C,OAAO,CAAC,QAAQ,CAAC,gBAAgB,CAAoC;IAErE;;;;OAIG;IACG,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC;IA2C/C;;;OAGG;IACH,SAAS,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAMtD;;OAEG;IACH,eAAe,CAAC,IAAI,EAAE,eAAe,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAI9D;;;OAGG;IACH,sBAAsB,CAAC,aAAa,EAAE,MAAM,GAAG,OAAO,CAAC,WAAW,EAAE,CAAC;IAIrE;;OAEG;IACH,UAAU,CAAC,WAAW,EAAE,MAAM,GAAG,OAAO,CAAC,MAAM,CAAC;IAShD;;OAEG;IACH,KAAK,IAAI,IAAI;CAKd;AAED;;GAEG;AACH,wBAAgB,wBAAwB,IAAI,kBAAkB,CAE7D"}
@@ -0,0 +1,105 @@
1
+ import { MemoryErrorCodes } from './types.js';
2
+ /**
3
+ * Error thrown by event store operations
4
+ */
5
+ export class EventStoreError extends Error {
6
+ code;
7
+ details;
8
+ constructor(code, message, details) {
9
+ super(message);
10
+ this.code = code;
11
+ this.details = details;
12
+ this.name = 'EventStoreError';
13
+ }
14
+ }
15
+ /**
16
+ * In-memory event store implementation
17
+ * INV-MEM-001: Events are immutable - stored as frozen objects
18
+ * INV-MEM-004: Events are ordered by version within aggregate
19
+ */
20
+ export class InMemoryEventStore {
21
+ events = new Map();
22
+ allEvents = [];
23
+ correlationIndex = new Map();
24
+ /**
25
+ * Appends an event to the store
26
+ * INV-MEM-001: Events are immutable
27
+ * INV-MEM-004: Version ordering enforced
28
+ */
29
+ async append(event) {
30
+ // Validate event
31
+ if (event.eventId === '') {
32
+ throw new EventStoreError(MemoryErrorCodes.INVALID_EVENT, 'Event must have an eventId');
33
+ }
34
+ const aggregateId = event.aggregateId ?? 'global';
35
+ // Check version ordering (INV-MEM-004)
36
+ if (event.version !== undefined) {
37
+ const currentVersion = await this.getVersion(aggregateId);
38
+ if (event.version !== currentVersion + 1) {
39
+ throw new EventStoreError(MemoryErrorCodes.VERSION_CONFLICT, `Version conflict: expected ${String(currentVersion + 1)}, got ${String(event.version)}`, { expected: currentVersion + 1, actual: event.version });
40
+ }
41
+ }
42
+ // Freeze the event to make it immutable (INV-MEM-001)
43
+ const frozenEvent = Object.freeze(structuredClone(event));
44
+ // Store in aggregate bucket
45
+ const aggregateEvents = this.events.get(aggregateId) ?? [];
46
+ aggregateEvents.push(frozenEvent);
47
+ this.events.set(aggregateId, aggregateEvents);
48
+ // Store in global list
49
+ this.allEvents.push(frozenEvent);
50
+ // Index by correlation ID (INV-MEM-005)
51
+ if (event.metadata?.correlationId !== undefined) {
52
+ const correlatedEvents = this.correlationIndex.get(event.metadata.correlationId) ?? [];
53
+ correlatedEvents.push(frozenEvent);
54
+ this.correlationIndex.set(event.metadata.correlationId, correlatedEvents);
55
+ }
56
+ }
57
+ /**
58
+ * Gets all events for an aggregate
59
+ * INV-MEM-004: Events returned in version order
60
+ */
61
+ getEvents(aggregateId) {
62
+ const events = this.events.get(aggregateId) ?? [];
63
+ // Events are already in append order, which should be version order
64
+ return Promise.resolve([...events]);
65
+ }
66
+ /**
67
+ * Gets events by type
68
+ */
69
+ getEventsByType(type) {
70
+ return Promise.resolve(this.allEvents.filter((e) => e.type === type));
71
+ }
72
+ /**
73
+ * Gets events by correlation ID
74
+ * INV-MEM-005: Support correlation tracing
75
+ */
76
+ getEventsByCorrelation(correlationId) {
77
+ return Promise.resolve(this.correlationIndex.get(correlationId) ?? []);
78
+ }
79
+ /**
80
+ * Gets the current version for an aggregate
81
+ */
82
+ getVersion(aggregateId) {
83
+ const events = this.events.get(aggregateId) ?? [];
84
+ if (events.length === 0) {
85
+ return Promise.resolve(0);
86
+ }
87
+ const lastEvent = events[events.length - 1];
88
+ return Promise.resolve(lastEvent?.version ?? events.length);
89
+ }
90
+ /**
91
+ * Clears all events (for testing)
92
+ */
93
+ clear() {
94
+ this.events.clear();
95
+ this.allEvents.length = 0;
96
+ this.correlationIndex.clear();
97
+ }
98
+ }
99
+ /**
100
+ * Creates an in-memory event store
101
+ */
102
+ export function createInMemoryEventStore() {
103
+ return new InMemoryEventStore();
104
+ }
105
+ //# sourceMappingURL=event-store.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"event-store.js","sourceRoot":"","sources":["../src/event-store.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,gBAAgB,EAAE,MAAM,YAAY,CAAC;AAE9C;;GAEG;AACH,MAAM,OAAO,eAAgB,SAAQ,KAAK;IAEtB;IAEA;IAHlB,YACkB,IAAY,EAC5B,OAAe,EACC,OAAiC;QAEjD,KAAK,CAAC,OAAO,CAAC,CAAC;QAJC,SAAI,GAAJ,IAAI,CAAQ;QAEZ,YAAO,GAAP,OAAO,CAA0B;QAGjD,IAAI,CAAC,IAAI,GAAG,iBAAiB,CAAC;IAChC,CAAC;CACF;AAED;;;;GAIG;AACH,MAAM,OAAO,kBAAkB;IACZ,MAAM,GAAG,IAAI,GAAG,EAAyB,CAAC;IAC1C,SAAS,GAAkB,EAAE,CAAC;IAC9B,gBAAgB,GAAG,IAAI,GAAG,EAAyB,CAAC;IAErE;;;;OAIG;IACH,KAAK,CAAC,MAAM,CAAC,KAAkB;QAC7B,iB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MAAM,CAAC,CAAC;IAC9D,CAAC;IAED;;OAEG;IACH,KAAK;QACH,IAAI,CAAC,MAAM,CAAC,KAAK,EAAE,CAAC;QACpB,IAAI,CAAC,SAAS,CAAC,MAAM,GAAG,CAAC,CAAC;QAC1B,IAAI,CAAC,gBAAgB,CAAC,KAAK,EAAE,CAAC;IAChC,CAAC;CACF;AAED;;GAEG;AACH,MAAM,UAAU,wBAAwB;IACtC,OAAO,IAAI,kBAAkB,EAAE,CAAC;AAClC,CAAC"}
@@ -0,0 +1,5 @@
1
+ export { InMemoryEventStore, EventStoreError, createInMemoryEventStore, } from './event-store.js';
2
+ export { AggregateRepository, createAggregateRepository, } from './aggregate.js';
3
+ export { ConversationAggregate, createConversationAggregate, } from './conversation.js';
4
+ export { MemoryErrorCodes, type MemoryErrorCode, type EventStore, type AggregateState, type EventHandler, type ConversationState, type Message, type MemoryItem, type MemoryStoreState, } from './types.js';
5
+ //# sourceMappingURL=index.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.d.ts","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAGA,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,wBAAwB,GACzB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,mBAAmB,EACnB,yBAAyB,GAC1B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,qBAAqB,EACrB,2BAA2B,GAC5B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,gBAAgB,EAChB,KAAK,eAAe,EACpB,KAAK,UAAU,EACf,KAAK,cAAc,EACnB,KAAK,YAAY,EACjB,KAAK,iBAAiB,EACtB,KAAK,OAAO,EACZ,KAAK,UAAU,EACf,KAAK,gBAAgB,GACtB,MAAM,YAAY,CAAC"}
package/dist/index.js ADDED
@@ -0,0 +1,7 @@
1
+ // Memory Domain
2
+ // Event sourcing following contract invariants
3
+ export { InMemoryEventStore, EventStoreError, createInMemoryEventStore, } from './event-store.js';
4
+ export { AggregateRepository, createAggregateRepository, } from './aggregate.js';
5
+ export { ConversationAggregate, createConversationAggregate, } from './conversation.js';
6
+ export { MemoryErrorCodes, } from './types.js';
7
+ //# sourceMappingURL=index.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"index.js","sourceRoot":"","sources":["../src/index.ts"],"names":[],"mappings":"AAAA,gBAAgB;AAChB,+CAA+C;AAE/C,OAAO,EACL,kBAAkB,EAClB,eAAe,EACf,wBAAwB,GACzB,MAAM,kBAAkB,CAAC;AAC1B,OAAO,EACL,mBAAmB,EACnB,yBAAyB,GAC1B,MAAM,gBAAgB,CAAC;AACxB,OAAO,EACL,qBAAqB,EACrB,2BAA2B,GAC5B,MAAM,mBAAmB,CAAC;AAC3B,OAAO,EACL,gBAAgB,GASjB,MAAM,YAAY,CAAC"}
@@ -0,0 +1,93 @@
1
+ import type { MemoryEvent, MemoryEventType } from '@defai.digital/contracts';
2
+ /**
3
+ * Event store interface for persistence
4
+ * INV-MEM-003: Adapters must not accept domain objects directly
5
+ */
6
+ export interface EventStore {
7
+ /**
8
+ * Appends an event to the store
9
+ * INV-MEM-001: Events are immutable once stored
10
+ */
11
+ append(event: MemoryEvent): Promise<void>;
12
+ /**
13
+ * Gets all events for an aggregate
14
+ * INV-MEM-004: Events returned in version order
15
+ */
16
+ getEvents(aggregateId: string): Promise<MemoryEvent[]>;
17
+ /**
18
+ * Gets events by type
19
+ */
20
+ getEventsByType(type: MemoryEventType): Promise<MemoryEvent[]>;
21
+ /**
22
+ * Gets events by correlation ID
23
+ * INV-MEM-005: Support correlation tracing
24
+ */
25
+ getEventsByCorrelation(correlationId: string): Promise<MemoryEvent[]>;
26
+ /**
27
+ * Gets the current version for an aggregate
28
+ */
29
+ getVersion(aggregateId: string): Promise<number>;
30
+ }
31
+ /**
32
+ * Aggregate state reconstructed from events
33
+ */
34
+ export interface AggregateState<T> {
35
+ aggregateId: string;
36
+ version: number;
37
+ state: T;
38
+ lastEventId?: string | undefined;
39
+ lastTimestamp?: string | undefined;
40
+ }
41
+ /**
42
+ * Event handler for applying events to state
43
+ */
44
+ export type EventHandler<T> = (state: T, event: MemoryEvent) => T;
45
+ /**
46
+ * Conversation state
47
+ */
48
+ export interface ConversationState {
49
+ conversationId: string;
50
+ title?: string | undefined;
51
+ messages: Message[];
52
+ context: Record<string, unknown>;
53
+ createdAt?: string | undefined;
54
+ updatedAt?: string | undefined;
55
+ }
56
+ /**
57
+ * Message in a conversation
58
+ */
59
+ export interface Message {
60
+ messageId: string;
61
+ role: 'user' | 'assistant' | 'system';
62
+ content: string;
63
+ tokenCount?: number | undefined;
64
+ timestamp?: string | undefined;
65
+ }
66
+ /**
67
+ * Memory item stored in the system
68
+ */
69
+ export interface MemoryItem {
70
+ key: string;
71
+ value: unknown;
72
+ ttlMs?: number;
73
+ tags?: string[];
74
+ createdAt: string;
75
+ expiresAt?: string;
76
+ }
77
+ /**
78
+ * Memory store state
79
+ */
80
+ export interface MemoryStoreState {
81
+ items: Map<string, MemoryItem>;
82
+ }
83
+ /**
84
+ * Error codes for memory domain
85
+ */
86
+ export declare const MemoryErrorCodes: {
87
+ readonly VERSION_CONFLICT: "MEMORY_VERSION_CONFLICT";
88
+ readonly AGGREGATE_NOT_FOUND: "MEMORY_AGGREGATE_NOT_FOUND";
89
+ readonly INVALID_EVENT: "MEMORY_INVALID_EVENT";
90
+ readonly STORE_ERROR: "MEMORY_STORE_ERROR";
91
+ };
92
+ export type MemoryErrorCode = (typeof MemoryErrorCodes)[keyof typeof MemoryErrorCodes];
93
+ //# sourceMappingURL=types.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.d.ts","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,WAAW,EAAE,eAAe,EAAE,MAAM,0BAA0B,CAAC;AAE7E;;;GAGG;AACH,MAAM,WAAW,UAAU;IACzB;;;OAGG;IACH,MAAM,CAAC,KAAK,EAAE,WAAW,GAAG,OAAO,CAAC,IAAI,CAAC,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M,GAAG,SAAS,CAAC;IAChC,SAAS,CAAC,EAAE,MAAM,GAAG,SAAS,CAAC;CAChC;AAED;;GAEG;AACH,MAAM,WAAW,UAAU;IACzB,GAAG,EAAE,MAAM,CAAC;IACZ,KAAK,EAAE,OAAO,CAAC;IACf,KAAK,CAAC,EAAE,MAAM,CAAC;IACf,IAAI,CAAC,EAAE,MAAM,EAAE,CAAC;IAChB,SAAS,EAAE,MAAM,CAAC;IAClB,SAAS,CAAC,EAAE,MAAM,CAAC;CACpB;AAED;;GAEG;AACH,MAAM,WAAW,gBAAgB;IAC/B,KAAK,EAAE,GAAG,CAAC,MAAM,EAAE,UAAU,CAAC,CAAC;CAChC;AAED;;GAEG;AACH,eAAO,MAAM,gBAAgB;;;;;CAKnB,CAAC;AAEX,MAAM,MAAM,eAAe,GACzB,CAAC,OAAO,gBAAgB,CAAC,CAAC,MAAM,OAAO,gBAAgB,CAAC,CAAC"}
package/dist/types.js ADDED
@@ -0,0 +1,10 @@
1
+ /**
2
+ * Error codes for memory domain
3
+ */
4
+ export const MemoryErrorCodes = {
5
+ VERSION_CONFLICT: 'MEMORY_VERSION_CONFLICT',
6
+ AGGREGATE_NOT_FOUND: 'MEMORY_AGGREGATE_NOT_FOUND',
7
+ INVALID_EVENT: 'MEMORY_INVALID_EVENT',
8
+ STORE_ERROR: 'MEMORY_STORE_ERROR',
9
+ };
10
+ //# sourceMappingURL=types.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"types.js","sourceRoot":"","sources":["../src/types.ts"],"names":[],"mappings":"AA8FA;;GAEG;AACH,MAAM,CAAC,MAAM,gBAAgB,GAAG;IAC9B,gBAAgB,EAAE,yBAAyB;IAC3C,mBAAmB,EAAE,4BAA4B;IACjD,aAAa,EAAE,sBAAsB;IACrC,WAAW,EAAE,oBAAoB;CACzB,CAAC"}
package/package.json ADDED
@@ -0,0 +1,41 @@
1
+ {
2
+ "name": "@defai.digital/memory-domain",
3
+ "version": "13.0.3",
4
+ "type": "module",
5
+ "description": "Memory domain - event sourcing following contract invariants",
6
+ "license": "Apache-2.0",
7
+ "author": "DEFAI Private Limited",
8
+ "repository": {
9
+ "type": "git",
10
+ "url": "https://github.com/defai-digital/automatosx.git",
11
+ "directory": "packages/core/memory-domain"
12
+ },
13
+ "homepage": "https://github.com/defai-digital/automatosx#readme",
14
+ "bugs": {
15
+ "url": "https://github.com/defai-digital/automatosx/issues"
16
+ },
17
+ "main": "./dist/index.js",
18
+ "types": "./dist/index.d.ts",
19
+ "exports": {
20
+ ".": {
21
+ "types": "./dist/index.d.ts",
22
+ "import": "./dist/index.js"
23
+ }
24
+ },
25
+ "files": [
26
+ "dist"
27
+ ],
28
+ "engines": {
29
+ "node": ">=20.0.0"
30
+ },
31
+ "publishConfig": {
32
+ "access": "public"
33
+ },
34
+ "dependencies": {
35
+ "@defai.digital/contracts": "13.0.3"
36
+ },
37
+ "scripts": {
38
+ "build": "tsc --build",
39
+ "clean": "rm -rf dist *.tsbuildinfo"
40
+ }
41
+ }