@spine-event-engine/server 2.0.0-snapshot.22 → 2.0.0-snapshot.23

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.
Files changed (126) hide show
  1. package/README.md +104 -7
  2. package/REFERENCE.md +194 -19
  3. package/dist/agent/agent-admission.d.ts +80 -0
  4. package/dist/agent/agent-admission.d.ts.map +1 -0
  5. package/dist/agent/agent-admission.js +127 -0
  6. package/dist/agent/agent-admission.js.map +1 -0
  7. package/dist/agent/agent-ai-binding.d.ts +24 -0
  8. package/dist/agent/agent-ai-binding.d.ts.map +1 -0
  9. package/dist/agent/agent-ai-binding.js +48 -0
  10. package/dist/agent/agent-ai-binding.js.map +1 -0
  11. package/dist/agent/agent-ai-runtime.d.ts +76 -0
  12. package/dist/agent/agent-ai-runtime.d.ts.map +1 -0
  13. package/dist/agent/agent-ai-runtime.js +1269 -0
  14. package/dist/agent/agent-ai-runtime.js.map +1 -0
  15. package/dist/agent/agent-execution-capacity.d.ts +43 -0
  16. package/dist/agent/agent-execution-capacity.d.ts.map +1 -0
  17. package/dist/agent/agent-execution-capacity.js +147 -0
  18. package/dist/agent/agent-execution-capacity.js.map +1 -0
  19. package/dist/agent/agent-execution-fault.d.ts +18 -0
  20. package/dist/agent/agent-execution-fault.d.ts.map +1 -0
  21. package/dist/agent/agent-execution-fault.js +31 -0
  22. package/dist/agent/agent-execution-fault.js.map +1 -0
  23. package/dist/agent/agent-execution-session.d.ts +103 -0
  24. package/dist/agent/agent-execution-session.d.ts.map +1 -0
  25. package/dist/agent/agent-execution-session.js +238 -0
  26. package/dist/agent/agent-execution-session.js.map +1 -0
  27. package/dist/agent/agent-history.d.ts +148 -0
  28. package/dist/agent/agent-history.d.ts.map +1 -0
  29. package/dist/agent/agent-history.js +340 -0
  30. package/dist/agent/agent-history.js.map +1 -0
  31. package/dist/agent/agent-interaction-audit.d.ts +58 -0
  32. package/dist/agent/agent-interaction-audit.d.ts.map +1 -0
  33. package/dist/agent/agent-interaction-audit.js +114 -0
  34. package/dist/agent/agent-interaction-audit.js.map +1 -0
  35. package/dist/agent/agent-interaction-events.d.ts +40 -0
  36. package/dist/agent/agent-interaction-events.d.ts.map +1 -0
  37. package/dist/agent/agent-interaction-events.js +129 -0
  38. package/dist/agent/agent-interaction-events.js.map +1 -0
  39. package/dist/agent/agent-mcp-budget.d.ts +125 -0
  40. package/dist/agent/agent-mcp-budget.d.ts.map +1 -0
  41. package/dist/agent/agent-mcp-budget.js +566 -0
  42. package/dist/agent/agent-mcp-budget.js.map +1 -0
  43. package/dist/agent/agent-mcp-host.d.ts +125 -0
  44. package/dist/agent/agent-mcp-host.d.ts.map +1 -0
  45. package/dist/agent/agent-mcp-host.js +15 -0
  46. package/dist/agent/agent-mcp-host.js.map +1 -0
  47. package/dist/agent/agent-mcp-runtime.d.ts +199 -0
  48. package/dist/agent/agent-mcp-runtime.d.ts.map +1 -0
  49. package/dist/agent/agent-mcp-runtime.js +621 -0
  50. package/dist/agent/agent-mcp-runtime.js.map +1 -0
  51. package/dist/agent/agent-model-selection.d.ts +129 -0
  52. package/dist/agent/agent-model-selection.d.ts.map +1 -0
  53. package/dist/agent/agent-model-selection.js +342 -0
  54. package/dist/agent/agent-model-selection.js.map +1 -0
  55. package/dist/agent/agent-read-runtime.d.ts +70 -0
  56. package/dist/agent/agent-read-runtime.d.ts.map +1 -0
  57. package/dist/agent/agent-read-runtime.js +320 -0
  58. package/dist/agent/agent-read-runtime.js.map +1 -0
  59. package/dist/agent/agent-revisions.d.ts +35 -0
  60. package/dist/agent/agent-revisions.d.ts.map +1 -0
  61. package/dist/agent/agent-revisions.js +133 -0
  62. package/dist/agent/agent-revisions.js.map +1 -0
  63. package/dist/agent/agent-scheduler.d.ts +99 -0
  64. package/dist/agent/agent-scheduler.d.ts.map +1 -0
  65. package/dist/agent/agent-scheduler.js +398 -0
  66. package/dist/agent/agent-scheduler.js.map +1 -0
  67. package/dist/bus/command-bus.d.ts +62 -0
  68. package/dist/bus/command-bus.d.ts.map +1 -1
  69. package/dist/bus/command-bus.js +51 -2
  70. package/dist/bus/command-bus.js.map +1 -1
  71. package/dist/bus/event-bus.d.ts +137 -3
  72. package/dist/bus/event-bus.d.ts.map +1 -1
  73. package/dist/bus/event-bus.js +133 -2
  74. package/dist/bus/event-bus.js.map +1 -1
  75. package/dist/bus/saved-dispatcher-binding.d.ts +96 -0
  76. package/dist/bus/saved-dispatcher-binding.d.ts.map +1 -0
  77. package/dist/bus/saved-dispatcher-binding.js +35 -0
  78. package/dist/bus/saved-dispatcher-binding.js.map +1 -0
  79. package/dist/context/bounded-context.d.ts +36 -2
  80. package/dist/context/bounded-context.d.ts.map +1 -1
  81. package/dist/context/bounded-context.js +253 -54
  82. package/dist/context/bounded-context.js.map +1 -1
  83. package/dist/context/tenant-index.d.ts +25 -0
  84. package/dist/context/tenant-index.d.ts.map +1 -1
  85. package/dist/context/tenant-index.js +45 -1
  86. package/dist/context/tenant-index.js.map +1 -1
  87. package/dist/entity/entity.d.ts +82 -3
  88. package/dist/entity/entity.d.ts.map +1 -1
  89. package/dist/entity/entity.js +121 -22
  90. package/dist/entity/entity.js.map +1 -1
  91. package/dist/handler/generated-handler-registry.js +8 -3
  92. package/dist/handler/generated-handler-registry.js.map +1 -1
  93. package/dist/handler/handler-metadata.d.ts +17 -1
  94. package/dist/handler/handler-metadata.d.ts.map +1 -1
  95. package/dist/handler/handler-metadata.js +10 -4
  96. package/dist/handler/handler-metadata.js.map +1 -1
  97. package/dist/index.d.ts +2 -2
  98. package/dist/index.d.ts.map +1 -1
  99. package/dist/index.js +1 -1
  100. package/dist/index.js.map +1 -1
  101. package/dist/repository/commit-fence.d.ts +11 -0
  102. package/dist/repository/commit-fence.d.ts.map +1 -1
  103. package/dist/repository/commit-fence.js +13 -0
  104. package/dist/repository/commit-fence.js.map +1 -1
  105. package/dist/repository/repository.d.ts +222 -13
  106. package/dist/repository/repository.d.ts.map +1 -1
  107. package/dist/repository/repository.js +2323 -123
  108. package/dist/repository/repository.js.map +1 -1
  109. package/dist/runtime/signal-publisher.d.ts +6 -0
  110. package/dist/runtime/signal-publisher.d.ts.map +1 -1
  111. package/dist/runtime/signal-publisher.js +13 -0
  112. package/dist/runtime/signal-publisher.js.map +1 -1
  113. package/dist/runtime/standalone-handler-runtime.d.ts +7 -0
  114. package/dist/runtime/standalone-handler-runtime.d.ts.map +1 -1
  115. package/dist/runtime/standalone-handler-runtime.js +148 -11
  116. package/dist/runtime/standalone-handler-runtime.js.map +1 -1
  117. package/dist/server/server.d.ts +9 -0
  118. package/dist/server/server.d.ts.map +1 -1
  119. package/dist/server/server.js +13 -1
  120. package/dist/server/server.js.map +1 -1
  121. package/dist/testing/index.d.ts +57 -1
  122. package/dist/testing/index.d.ts.map +1 -1
  123. package/dist/testing/index.js +87 -0
  124. package/dist/testing/index.js.map +1 -1
  125. package/dist/tsconfig.tsbuildinfo +1 -1
  126. package/package.json +14 -12
@@ -0,0 +1,238 @@
1
+ /*
2
+ * Copyright 2026, CodeMatters. All rights reserved.
3
+ *
4
+ * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5
+ * in compliance with the License. You may obtain a copy of the License at
6
+ *
7
+ * https://www.apache.org/licenses/LICENSE-2.0
8
+ *
9
+ * Unless required by applicable law or agreed to in writing, software distributed under the License
10
+ * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11
+ * or implied. See the License for the specific language governing permissions and limitations under
12
+ * the License.
13
+ */
14
+ import { clone, create, toBinary } from "@bufbuild/protobuf";
15
+ import { EmptySchema } from "@bufbuild/protobuf/wkt";
16
+ import { AiModelKind, ModelPreferenceSchema, ModelRefSchema, } from "@spine-event-engine/proto/agent";
17
+ import { AgentExecutionRecordSchema, } from "@spine-event-engine/proto/generated/spine/server/agent/execution_record_pb.js";
18
+ import { AgentExecutionFault } from "./agent-execution-fault.js";
19
+ /**
20
+ * Serializes one claimed Agent record's journal and lease images.
21
+ */
22
+ export class AgentExecutionSession {
23
+ storage;
24
+ token;
25
+ #controller = new AbortController();
26
+ #record;
27
+ #initialPreferences;
28
+ #preferences;
29
+ #queue = Promise.resolve();
30
+ /**
31
+ * Creates a serialized mutation boundary for one exact provider claim.
32
+ *
33
+ * @param storage Provider methods for this Agent repository.
34
+ * @param record Record returned by the successful claim.
35
+ * @param token Exclusive token required by later provider writes.
36
+ * @param preferences Per-instance selections observed at claim time.
37
+ */
38
+ constructor(storage, record, token, preferences = []) {
39
+ this.storage = storage;
40
+ this.token = token;
41
+ if (record.accepted?.key === undefined || record.claimToken !== token)
42
+ throw new Error("Agent execution session requires its exact claimed record.");
43
+ this.#record = record;
44
+ this.#initialPreferences = preferences;
45
+ this.#preferences = [...this.#initialPreferences];
46
+ }
47
+ /**
48
+ * Cancels callbacks after provider authority is lost.
49
+ *
50
+ * @returns Cancellation signal shared with scoped work.
51
+ */
52
+ get signal() {
53
+ return this.#controller.signal;
54
+ }
55
+ /**
56
+ * Returns a defensive copy of the last provider-confirmed image.
57
+ *
58
+ * @returns Independent execution record image.
59
+ */
60
+ record() {
61
+ return clone(AgentExecutionRecordSchema, this.#record);
62
+ }
63
+ /**
64
+ * Returns per-instance selections observed at the claim boundary.
65
+ *
66
+ * @returns Current staged selections in a separate array.
67
+ */
68
+ preferences() {
69
+ return [...this.#preferences];
70
+ }
71
+ /**
72
+ * Returns selections fixed when this invocation claimed its Agent.
73
+ *
74
+ * @returns Claim-time selections in a separate array.
75
+ */
76
+ initialPreferences() {
77
+ return [...this.#initialPreferences];
78
+ }
79
+ /**
80
+ * Sets one per-kind selection for conditional completion of this signal.
81
+ *
82
+ * @param kind Generation or decision kind being selected.
83
+ * @param model Chosen deployment, or undefined to inherit the repository default.
84
+ */
85
+ stagePreference(kind, model) {
86
+ if (this.signal.aborted)
87
+ throw new Error("Agent execution claim is no longer active.");
88
+ if (kind !== AiModelKind.GENERATION && kind !== AiModelKind.DECISION)
89
+ throw new TypeError("Agent model preference requires a supported kind.");
90
+ const selection = model === undefined
91
+ ? { case: "inheritRepositoryDefault", value: create(EmptySchema) }
92
+ : { case: "model", value: clone(ModelRefSchema, model) };
93
+ const next = create(ModelPreferenceSchema, { kind, selection });
94
+ const index = this.#preferences.findIndex((entry) => entry.kind === kind);
95
+ if (index < 0)
96
+ this.#preferences.push(next);
97
+ else
98
+ this.#preferences[index] = next;
99
+ }
100
+ /**
101
+ * Restores claim-time selections after a draft is rejected before completion.
102
+ */
103
+ discardPreferenceChanges() {
104
+ this.#preferences.splice(0, this.#preferences.length, ...this.#initialPreferences);
105
+ }
106
+ /**
107
+ * Returns fixed provider payload bounds for pre-dispatch reservations.
108
+ *
109
+ * @returns Provider capacity for the current Agent repository.
110
+ */
111
+ capacity() {
112
+ return this.storage.capacity;
113
+ }
114
+ /**
115
+ * Updates the journal against the latest provider-confirmed image.
116
+ *
117
+ * @param makeNext Pure transformation of the last confirmed record.
118
+ * @param historyEntries Conversation and System records saved with this update.
119
+ * @returns Independent image confirmed by the provider write.
120
+ */
121
+ update(makeNext, historyEntries = []) {
122
+ return this.#enqueue(async () => {
123
+ const current = this.record();
124
+ const expectedRecordBytes = toBinary(AgentExecutionRecordSchema, current);
125
+ const next = makeNext(current);
126
+ const key = current.accepted?.key;
127
+ if (key === undefined)
128
+ throw new Error("Claimed Agent record lost its accepted key.");
129
+ await this.storage.update({
130
+ key,
131
+ token: this.token,
132
+ expectedRecordBytes,
133
+ next,
134
+ historyEntries,
135
+ });
136
+ this.#record = next;
137
+ return this.record();
138
+ });
139
+ }
140
+ /**
141
+ * Updates the claim expiry and reloads the provider image before later fenced writes.
142
+ *
143
+ * @param expiresAt Requested later claim expiry.
144
+ * @returns Reloaded provider-confirmed record image.
145
+ */
146
+ renew(expiresAt) {
147
+ return this.#enqueue(async () => {
148
+ const key = this.#record.accepted?.key;
149
+ if (key === undefined)
150
+ throw new Error("Claimed Agent record lost its accepted key.");
151
+ if (!(await this.storage.renew(key, this.token, expiresAt)))
152
+ throw new Error("Agent execution lost its provider claim during renewal.");
153
+ const refreshed = await this.storage.read(key);
154
+ if (refreshed?.claimToken !== this.token)
155
+ throw new Error("Agent execution lost its provider claim after renewal.");
156
+ this.#record = refreshed;
157
+ return this.record();
158
+ });
159
+ }
160
+ /**
161
+ * Commits Entity and execution results under the same serialized fence.
162
+ *
163
+ * @param makeNext Pure transformation of the last confirmed record.
164
+ * @param historyEntries History records committed with the Entity transition.
165
+ * @param entityCommit Conditional state and output commit, when supplied.
166
+ * @returns Independent image confirmed by the atomic completion.
167
+ */
168
+ complete(makeNext, historyEntries, entityCommit) {
169
+ return this.#enqueue(async () => {
170
+ const current = this.record();
171
+ const expectedRecordBytes = toBinary(AgentExecutionRecordSchema, current);
172
+ const key = current.accepted?.key;
173
+ if (key === undefined)
174
+ throw new Error("Claimed Agent record lost its accepted key.");
175
+ const next = makeNext(current);
176
+ await this.storage.complete({
177
+ key,
178
+ token: this.token,
179
+ expectedRecordBytes,
180
+ next,
181
+ historyEntries,
182
+ ...(entityCommit === undefined ? {} : { entityCommit }),
183
+ });
184
+ this.#record = next;
185
+ return this.record();
186
+ });
187
+ }
188
+ /**
189
+ * Marks saved output IDs delivered and reloads the provider image.
190
+ *
191
+ * @param signals Original output IDs accepted by normal delivery.
192
+ * @returns Reloaded provider-confirmed record image.
193
+ */
194
+ markDelivered(signals) {
195
+ return this.#enqueue(async () => {
196
+ const current = this.record();
197
+ const key = current.accepted?.key;
198
+ if (key === undefined)
199
+ throw new Error("Claimed Agent record lost its accepted key.");
200
+ await this.storage.markDelivered(key, this.token, toBinary(AgentExecutionRecordSchema, current), signals);
201
+ const refreshed = await this.storage.read(key);
202
+ if (refreshed === undefined)
203
+ throw new Error("Agent output record disappeared after delivery.");
204
+ this.#record = refreshed;
205
+ return this.record();
206
+ });
207
+ }
208
+ /**
209
+ * Stops later queued mutations from using this claim.
210
+ */
211
+ stop() {
212
+ this.#controller.abort();
213
+ }
214
+ /**
215
+ * Serializes one fenced provider operation after preceding writes settle.
216
+ *
217
+ * @typeParam Result Result returned by the provider operation.
218
+ * @param work Provider mutation to run while the session remains active.
219
+ * @returns Mutation result or a claim-loss rejection.
220
+ */
221
+ #enqueue(work) {
222
+ const task = this.#queue.then(async () => {
223
+ if (this.signal.aborted)
224
+ throw new Error("Agent execution claim is no longer active.");
225
+ try {
226
+ return await work();
227
+ }
228
+ catch (error) {
229
+ if (!(error instanceof AgentExecutionFault))
230
+ this.stop();
231
+ throw error;
232
+ }
233
+ });
234
+ this.#queue = task.then(() => undefined, () => undefined);
235
+ return task;
236
+ }
237
+ }
238
+ //# sourceMappingURL=agent-execution-session.js.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-execution-session.js","sourceRoot":"","sources":["../../src/agent/agent-execution-session.ts"],"names":[],"mappings":"AAAA;;;;;;;;;;;;GAYG;AAEH,OAAO,EAAE,KAAK,EAAE,MAAM,EAAE,QAAQ,EAAgB,MAAM,oBAAoB,CAAC;AAC3E,OAAO,EAAE,WAAW,EAAkB,MAAM,wBAAwB,CAAC;AACrE,OAAO,EACL,WAAW,EACX,qBAAqB,EACrB,cAAc,GAIf,MAAM,iCAAiC,CAAC;AACzC,OAAO,EACL,0BAA0B,GAE3B,MAAM,+EAA+E,CAAC;AAKvF,OAAO,EAAE,mBAAmB,EAAE,MAAM,4BAA4B,CAAC;AASjE;;GAEG;AACH,MAAM,OAAO,qBAAqB;IAoBb;IAEA;IArBV,WAAW,GAAG,IAAI,eAAe,EAAE,CAAC;IAE7C,OAAO,CAAuB;IAErB,mBAAmB,CAA6B;IAEhD,YAAY,CAAoB;IAEzC,MAAM,GAAkB,OAAO,CAAC,OAAO,EAAE,CAAC;IAE1C;;;;;;;OAOG;IACH,YACmB,OAAuB,EACxC,MAA4B,EACX,KAAa,EAC9B,cAA0C,EAAE;QAH3B,YAAO,GAAP,OAAO,CAAgB;QAEvB,UAAK,GAAL,KAAK,CAAQ;QAG9B,IAAI,MAAM,CAAC,QAAQ,EAAE,GAAG,KAAK,SAAS,IAAI,MAAM,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,CAAS,IAA2B;QAC1C,MAAM,IAAI,GAAG,IAAI,CAAC,MAAM,CAAC,IAAI,CAAC,KAAK,IAAI,EAAE;YACvC,IAAI,IAAI,CAAC,MAAM,CAAC,OAAO;gBAAE,MAAM,IAAI,KAAK,CAAC,4CAA4C,CAAC,CAAC;YACvF,IAAI,CAAC;gBACH,OAAO,MAAM,IAAI,EAAE,CAAC;YACtB,CAAC;YAAC,OAAO,KAAK,EAAE,CAAC;gBACf,IAAI,CAAC,CAAC,KAAK,YAAY,mBAAmB,CAAC;oBAAE,IAAI,CAAC,IAAI,EAAE,CAAC;gBACzD,MAAM,KAAK,CAAC;YACd,CAAC;QACH,CAAC,CAAC,CAAC;QACH,IAAI,CAAC,MAAM,GAAG,IAAI,CAAC,IAAI,CACrB,GAAG,EAAE,CAAC,SAAS,EACf,GAAG,EAAE,CAAC,SAAS,CAChB,CAAC;QACF,OAAO,IAAI,CAAC;IACd,CAAC;CACF"}
@@ -0,0 +1,148 @@
1
+ import type { ConversationHistoryRead, HistoryPage, HistoryRead } from "@spine-event-engine/ai";
2
+ import type { Event } from "@spine-event-engine/proto";
3
+ import { type AgentHistoryEntry, type ConversationRecord } from "@spine-event-engine/proto/agent";
4
+ import { type AgentHistoryPage, type AgentHistoryStorage, type AgentHistoryView } from "@spine-event-engine/storage/provider";
5
+ /**
6
+ * Identifies the exact repository and Agent instance for a continuation.
7
+ */
8
+ export interface AgentHistoryScope {
9
+ /**
10
+ * Bounded Context name in the active repository.
11
+ */
12
+ readonly context: string;
13
+ /**
14
+ * Complete tenant identity encoding.
15
+ */
16
+ readonly tenant: string;
17
+ /**
18
+ * Registered Entity state type.
19
+ */
20
+ readonly repository: string;
21
+ /**
22
+ * Canonical typed Agent ID key.
23
+ */
24
+ readonly entity: string;
25
+ }
26
+ /**
27
+ * Returns a saved complete page or delegates to a live provider read.
28
+ *
29
+ * @param scope Exact repository and Agent scope.
30
+ * @param view Indexed history view.
31
+ * @param request Public read request.
32
+ * @param live Loads the current provider page only when needed.
33
+ * @param maxBytes Optional response-byte ceiling for this read.
34
+ * @returns The saved or newly loaded complete provider page.
35
+ */
36
+ export type AgentHistoryJournal = (scope: AgentHistoryScope, view: AgentHistoryView, request: HistoryRead, live: () => Promise<AgentHistoryPage>, maxBytes: number | undefined) => Promise<AgentHistoryPage>;
37
+ /**
38
+ * Repository binding for an Agent's mandatory indexed history.
39
+ *
40
+ * @typeParam Id Typed Agent identifier.
41
+ */
42
+ export interface AgentHistoryBinding<Id> {
43
+ /**
44
+ * Mandatory provider handle.
45
+ */
46
+ readonly storage: AgentHistoryStorage<Id>;
47
+ /**
48
+ * Typed Agent ID.
49
+ */
50
+ readonly entityId: Id;
51
+ /**
52
+ * Full cursor and journal scope.
53
+ */
54
+ readonly scope: AgentHistoryScope;
55
+ /**
56
+ * Optional durable read replay interceptor.
57
+ */
58
+ readonly journal?: AgentHistoryJournal;
59
+ /**
60
+ * Optional maximum serialized entry bytes in one response.
61
+ */
62
+ readonly maxBytes?: number;
63
+ }
64
+ /**
65
+ * Internal accessors shared by the Agent base and repository runtime.
66
+ */
67
+ export interface AgentHistoryReadAccess {
68
+ /**
69
+ * Attaches one mandatory storage handle to a restored Agent.
70
+ *
71
+ * @typeParam Id Typed Agent identifier.
72
+ * @param entity Restored Agent.
73
+ * @param binding Repository history binding.
74
+ */
75
+ bind<Id>(entity: object, binding: AgentHistoryBinding<Id>): void;
76
+ /**
77
+ * Binds a journal that records and replays this handler's history reads.
78
+ *
79
+ * @param entity Agent with an existing repository history binding.
80
+ * @param journal Intercepts history reads during the handler.
81
+ * @returns Callback that restores the original history binding after the handler finishes.
82
+ */
83
+ withJournal(entity: object, journal: AgentHistoryJournal): () => void;
84
+ /**
85
+ * Reads all categories.
86
+ *
87
+ * @param entity Agent instance.
88
+ * @param request Page request.
89
+ * @returns Complete entries and continuation.
90
+ */
91
+ full(entity: object, request: HistoryRead): Promise<HistoryPage<AgentHistoryEntry>>;
92
+ /**
93
+ * Reads one conversation.
94
+ *
95
+ * @param entity Agent instance.
96
+ * @param request Conversation and page request.
97
+ * @returns Conversation records and continuation.
98
+ */
99
+ conversation(entity: object, request: ConversationHistoryRead): Promise<HistoryPage<ConversationRecord>>;
100
+ /**
101
+ * Reads System Events.
102
+ *
103
+ * @param entity Agent instance.
104
+ * @param request Page request.
105
+ * @returns Original Event envelopes and continuation.
106
+ */
107
+ system(entity: object, request: HistoryRead): Promise<HistoryPage<Event>>;
108
+ /**
109
+ * Reads emitted domain Events.
110
+ *
111
+ * @param entity Agent instance.
112
+ * @param request Page request.
113
+ * @returns Original Event envelopes and continuation.
114
+ */
115
+ domain(entity: object, request: HistoryRead): Promise<HistoryPage<Event>>;
116
+ /**
117
+ * Validates a cursor and reads one provider page.
118
+ *
119
+ * @param entity Agent instance.
120
+ * @param view Indexed history view.
121
+ * @param request Page request.
122
+ * @returns Complete entries and continuation.
123
+ */
124
+ read(entity: object, view: AgentHistoryView, request: HistoryRead): Promise<HistoryPage<AgentHistoryEntry>>;
125
+ /**
126
+ * Applies the same opaque cursor contract to a testing repository binding.
127
+ *
128
+ * @typeParam Id Typed Agent identifier supplied by the repository binding.
129
+ * @param binding Exact Agent repository and tenant storage scope.
130
+ * @param view Indexed history view.
131
+ * @param request Public page request.
132
+ * @returns Retained entries and opaque continuation.
133
+ */
134
+ readBound<Id>(binding: AgentHistoryBinding<Id>, view: AgentHistoryView, request: HistoryRead): Promise<HistoryPage<AgentHistoryEntry>>;
135
+ /**
136
+ * Persists a genuine framework System Event.
137
+ *
138
+ * @param entity Agent instance.
139
+ * @param event Original System Event envelope.
140
+ * @returns Completion after append.
141
+ */
142
+ appendSystem(entity: object, event: Event): Promise<void>;
143
+ }
144
+ /**
145
+ * Binds protected Agent history reads to repository storage.
146
+ */
147
+ export declare const AgentHistoryReads: AgentHistoryReadAccess;
148
+ //# sourceMappingURL=agent-history.d.ts.map
@@ -0,0 +1 @@
1
+ {"version":3,"file":"agent-history.d.ts","sourceRoot":"","sources":["../../src/agent/agent-history.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