@agentropy/runtime 0.0.0-bootstrap.0 → 0.1.0
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 +202 -0
- package/README.md +38 -1
- package/dist/a2a-client.d.ts +111 -0
- package/dist/a2a-client.js +386 -0
- package/dist/a2a-history.d.ts +27 -0
- package/dist/a2a-history.js +67 -0
- package/dist/a2a-http.d.ts +46 -0
- package/dist/a2a-http.js +183 -0
- package/dist/a2a-protocol.d.ts +77 -0
- package/dist/a2a-protocol.js +148 -0
- package/dist/a2a-push-receiver.d.ts +24 -0
- package/dist/a2a-push-receiver.js +161 -0
- package/dist/a2a-push.d.ts +85 -0
- package/dist/a2a-push.js +385 -0
- package/dist/a2a-server.d.ts +82 -0
- package/dist/a2a-server.js +523 -0
- package/dist/a2a-sse.d.ts +5 -0
- package/dist/a2a-sse.js +36 -0
- package/dist/a2a.d.ts +41 -0
- package/dist/a2a.js +40 -0
- package/dist/app.d.ts +256 -0
- package/dist/app.js +541 -0
- package/dist/audit.d.ts +229 -0
- package/dist/audit.js +718 -0
- package/dist/bounded-body.d.ts +32 -0
- package/dist/bounded-body.js +124 -0
- package/dist/communication.d.ts +62 -0
- package/dist/communication.js +142 -0
- package/dist/compaction.d.ts +24 -0
- package/dist/compaction.js +202 -0
- package/dist/federation.d.ts +108 -0
- package/dist/federation.js +241 -0
- package/dist/http-path.d.ts +7 -0
- package/dist/http-path.js +14 -0
- package/dist/human-actions.d.ts +62 -0
- package/dist/human-actions.js +359 -0
- package/dist/implementation.d.ts +47 -0
- package/dist/implementation.js +138 -0
- package/dist/inbox.d.ts +101 -0
- package/dist/inbox.js +389 -0
- package/dist/index.d.ts +32 -0
- package/dist/index.js +32 -0
- package/dist/ingress.d.ts +35 -0
- package/dist/ingress.js +86 -0
- package/dist/lifecycle.d.ts +45 -0
- package/dist/lifecycle.js +57 -0
- package/dist/limits.d.ts +43 -0
- package/dist/limits.js +71 -0
- package/dist/mcp.d.ts +111 -0
- package/dist/mcp.js +308 -0
- package/dist/memory.d.ts +25 -0
- package/dist/memory.js +134 -0
- package/dist/model-routing.d.ts +66 -0
- package/dist/model-routing.js +80 -0
- package/dist/otel.d.ts +60 -0
- package/dist/otel.js +72 -0
- package/dist/outbox.d.ts +116 -0
- package/dist/outbox.js +327 -0
- package/dist/policy-cache.d.ts +69 -0
- package/dist/policy-cache.js +178 -0
- package/dist/priority.d.ts +27 -0
- package/dist/priority.js +28 -0
- package/dist/quotas.d.ts +19 -0
- package/dist/quotas.js +47 -0
- package/dist/reasoning.d.ts +54 -0
- package/dist/reasoning.js +307 -0
- package/dist/registry.d.ts +101 -0
- package/dist/registry.js +369 -0
- package/dist/remote.d.ts +130 -0
- package/dist/remote.js +473 -0
- package/dist/runtime.d.ts +167 -0
- package/dist/runtime.js +1035 -0
- package/dist/skills.d.ts +86 -0
- package/dist/skills.js +376 -0
- package/dist/task-service.d.ts +239 -0
- package/dist/task-service.js +790 -0
- package/dist/tools.d.ts +140 -0
- package/dist/tools.js +476 -0
- package/dist/wake-index.d.ts +9 -0
- package/dist/wake-index.js +11 -0
- package/dist/wakeups.d.ts +142 -0
- package/dist/wakeups.js +501 -0
- package/dist/workflow.d.ts +36 -0
- package/dist/workflow.js +101 -0
- package/package.json +41 -5
package/dist/inbox.js
ADDED
|
@@ -0,0 +1,389 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* User inbox as a projection of TaskEvents (docs/human-actions.md "User inbox as projection", R-12, R-25, R-36, AC-02).
|
|
3
|
+
*
|
|
4
|
+
* - `notification.notify` writes its journal entry and a `notificationCreated` outbox event in one transaction,
|
|
5
|
+
* so a replayed step returns the original `entryId` and never creates a second entry (AC-07 internal half).
|
|
6
|
+
* - `InboxProjector` turns published events into entries; it is idempotent per event ID and never changes task state.
|
|
7
|
+
* - `conversation.briefing` returns entries not yet presented at their current revision and marks them presented
|
|
8
|
+
* in the same transaction, so a later turn does not repeat an unchanged entry.
|
|
9
|
+
*/
|
|
10
|
+
import { AgentropyError, } from '@agentropy/core';
|
|
11
|
+
import { toTaskEvent, } from './outbox.js';
|
|
12
|
+
import { SEVERITY_ORDER, resolveDelivery, } from './priority.js';
|
|
13
|
+
import { hashKey, } from './task-service.js';
|
|
14
|
+
/** The entry with `pending` recomputed from its markers and revision. */
|
|
15
|
+
const settle = (e) => ({
|
|
16
|
+
...e,
|
|
17
|
+
pending: !e.markers.acknowledgedAt && e.markers.presentedRevision !== e.revision
|
|
18
|
+
? 1
|
|
19
|
+
: 0,
|
|
20
|
+
});
|
|
21
|
+
const SEVERITIES = ['critical', 'high', 'normal', 'low'];
|
|
22
|
+
const APPLIED_KEPT = 50;
|
|
23
|
+
/** Public shape: storage bookkeeping removed. */
|
|
24
|
+
export function publicEntry(e) {
|
|
25
|
+
const { ownerId: _o, status: _s, applied: _a, pending: _p, ...entry } = e;
|
|
26
|
+
void _o;
|
|
27
|
+
void _s;
|
|
28
|
+
void _a;
|
|
29
|
+
void _p;
|
|
30
|
+
return entry;
|
|
31
|
+
}
|
|
32
|
+
function authenticated(caller) {
|
|
33
|
+
if (!caller?.id || !caller.tenantId)
|
|
34
|
+
throw new AgentropyError('Unauthenticated', 'No trusted principal');
|
|
35
|
+
return caller;
|
|
36
|
+
}
|
|
37
|
+
export class InboxService {
|
|
38
|
+
tasks;
|
|
39
|
+
options;
|
|
40
|
+
constructor(tasks, options = {}) {
|
|
41
|
+
this.tasks = tasks;
|
|
42
|
+
this.options = options;
|
|
43
|
+
}
|
|
44
|
+
get store() {
|
|
45
|
+
return this.tasks.o.store;
|
|
46
|
+
}
|
|
47
|
+
async list(query, caller) {
|
|
48
|
+
const p = authenticated(caller);
|
|
49
|
+
const after = query.cursor ? Number(query.cursor) : 0;
|
|
50
|
+
if (!Number.isSafeInteger(after) || after < 0)
|
|
51
|
+
throw new AgentropyError('SchemaViolation', 'Invalid inbox cursor');
|
|
52
|
+
const docs = await this.store.transaction((tx) => tx.find('inbox', {
|
|
53
|
+
where: { tenantId: p.tenantId, ownerId: p.id },
|
|
54
|
+
orderBy: 'seq',
|
|
55
|
+
afterSeq: after,
|
|
56
|
+
limit: Math.min(Math.max(query.limit ?? 50, 1), 200),
|
|
57
|
+
}));
|
|
58
|
+
return {
|
|
59
|
+
entries: docs.map((d) => publicEntry(d.value)),
|
|
60
|
+
cursor: String(docs.at(-1)?.seq ?? after),
|
|
61
|
+
};
|
|
62
|
+
}
|
|
63
|
+
async mark(entryId, marker, caller, presentedRevision) {
|
|
64
|
+
const p = authenticated(caller);
|
|
65
|
+
await this.store.transaction(async (tx) => {
|
|
66
|
+
const doc = await tx.get('inbox', entryId);
|
|
67
|
+
// Another user's or tenant's entry is invisible, not forbidden.
|
|
68
|
+
if (!doc ||
|
|
69
|
+
doc.value.ownerId !== p.id ||
|
|
70
|
+
doc.value.tenantId !== p.tenantId)
|
|
71
|
+
throw new AgentropyError('NotFound', 'Inbox entry not found');
|
|
72
|
+
const e = doc.value;
|
|
73
|
+
const now = this.tasks.o.now();
|
|
74
|
+
const markers = { ...e.markers };
|
|
75
|
+
if (marker === 'presented') {
|
|
76
|
+
markers.presentedAt = now;
|
|
77
|
+
markers.presentedRevision = presentedRevision ?? e.revision;
|
|
78
|
+
}
|
|
79
|
+
else if (marker === 'read')
|
|
80
|
+
markers.readAt ??= now;
|
|
81
|
+
else
|
|
82
|
+
markers.acknowledgedAt ??= now;
|
|
83
|
+
await tx.update('inbox', doc.id, doc.revision, settle({ ...e, markers }));
|
|
84
|
+
});
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Turn-start briefing (R-36, R-26): entries not yet presented at their current revision, filtered and ordered by
|
|
88
|
+
* the delivery policy (interrupts first, then severity, then age), marked presented in the same transaction.
|
|
89
|
+
* On-demand entries are left unpresented and only counted.
|
|
90
|
+
*/
|
|
91
|
+
async briefing(user, limit = 10) {
|
|
92
|
+
const p = authenticated(user);
|
|
93
|
+
const prefs = (await this.options.preferences?.(p)) ?? {};
|
|
94
|
+
return this.store.transaction(async (tx) => {
|
|
95
|
+
// Only entries the briefing may show: `pending` 1, plus entries written before the field existed (null).
|
|
96
|
+
const docs = (await Promise.all([1, null].map((pending) => tx.find('inbox', {
|
|
97
|
+
where: { tenantId: p.tenantId, ownerId: p.id, pending },
|
|
98
|
+
orderBy: 'seq',
|
|
99
|
+
}))))
|
|
100
|
+
.flat()
|
|
101
|
+
.sort((x, y) => x.seq - y.seq);
|
|
102
|
+
const due = docs
|
|
103
|
+
.filter((d) => !d.value.markers.acknowledgedAt &&
|
|
104
|
+
d.value.markers.presentedRevision !== d.value.revision)
|
|
105
|
+
.map((d) => ({
|
|
106
|
+
d,
|
|
107
|
+
decision: resolveDelivery(publicEntry(d.value), prefs, this.options.policy),
|
|
108
|
+
}));
|
|
109
|
+
const onDemand = due.filter((x) => x.decision.delivery === 'on-demand');
|
|
110
|
+
const shown = due
|
|
111
|
+
.filter((x) => x.decision.delivery !== 'on-demand')
|
|
112
|
+
.sort((a, b) => Number(b.decision.delivery === 'interrupt') -
|
|
113
|
+
Number(a.decision.delivery === 'interrupt') ||
|
|
114
|
+
SEVERITY_ORDER.indexOf(a.decision.severity) -
|
|
115
|
+
SEVERITY_ORDER.indexOf(b.decision.severity) ||
|
|
116
|
+
a.d.seq - b.d.seq)
|
|
117
|
+
.slice(0, Math.min(Math.max(limit, 1), 50));
|
|
118
|
+
const now = this.tasks.o.now();
|
|
119
|
+
const entries = [];
|
|
120
|
+
for (const { d, decision } of shown) {
|
|
121
|
+
const next = {
|
|
122
|
+
...d.value,
|
|
123
|
+
markers: {
|
|
124
|
+
...d.value.markers,
|
|
125
|
+
presentedAt: now,
|
|
126
|
+
presentedRevision: d.value.revision,
|
|
127
|
+
},
|
|
128
|
+
};
|
|
129
|
+
await tx.update('inbox', d.id, d.revision, settle(next));
|
|
130
|
+
entries.push({ ...publicEntry(next), severity: decision.severity });
|
|
131
|
+
}
|
|
132
|
+
return { entries, onDemand: onDemand.length };
|
|
133
|
+
});
|
|
134
|
+
}
|
|
135
|
+
}
|
|
136
|
+
/** Projects user-relevant TaskEvents into inbox entries. */
|
|
137
|
+
export class InboxProjector {
|
|
138
|
+
tasks;
|
|
139
|
+
id = 'inbox';
|
|
140
|
+
projectTerminal;
|
|
141
|
+
touched = [];
|
|
142
|
+
constructor(tasks, options = {}) {
|
|
143
|
+
this.tasks = tasks;
|
|
144
|
+
this.projectTerminal =
|
|
145
|
+
options.projectTerminal ??
|
|
146
|
+
((task, type) => type === 'taskFailed' && !!task.parentTaskId);
|
|
147
|
+
this.push = options.push;
|
|
148
|
+
this.policy = options.policy;
|
|
149
|
+
}
|
|
150
|
+
push;
|
|
151
|
+
policy;
|
|
152
|
+
/**
|
|
153
|
+
* Reconciliation (M2-03): re-apply every committed TaskEvent in commit order. Projection is idempotent per event,
|
|
154
|
+
* so existing entries and their markers are kept; missing entries are recreated. Task state is never changed.
|
|
155
|
+
* Returns how many entries were created.
|
|
156
|
+
*/
|
|
157
|
+
async rebuild() {
|
|
158
|
+
const store = this.tasks.o.store;
|
|
159
|
+
const before = (await store.transaction((tx) => tx.find('inbox', {})))
|
|
160
|
+
.length;
|
|
161
|
+
const rows = await store.transaction((tx) => tx.find('outbox', { orderBy: 'seq' }));
|
|
162
|
+
for (const r of rows)
|
|
163
|
+
await this.handle(toTaskEvent(r.value), false);
|
|
164
|
+
return ((await store.transaction((tx) => tx.find('inbox', {}))).length - before);
|
|
165
|
+
}
|
|
166
|
+
async handle(event, push = true) {
|
|
167
|
+
this.touched = [];
|
|
168
|
+
await this.project(event);
|
|
169
|
+
if (push && this.push)
|
|
170
|
+
await this.pushUrgent(this.touched);
|
|
171
|
+
}
|
|
172
|
+
async pushUrgent(ids) {
|
|
173
|
+
for (const id of ids) {
|
|
174
|
+
const doc = await this.tasks.o.store.transaction((tx) => tx.get('inbox', id));
|
|
175
|
+
if (!doc)
|
|
176
|
+
continue;
|
|
177
|
+
const entry = publicEntry(doc.value);
|
|
178
|
+
if (resolveDelivery(entry, {}, this.policy).delivery !== 'interrupt')
|
|
179
|
+
continue;
|
|
180
|
+
let delivered;
|
|
181
|
+
try {
|
|
182
|
+
delivered = await this.push(entry);
|
|
183
|
+
}
|
|
184
|
+
catch {
|
|
185
|
+
delivered = false; // push is best effort; the briefing still presents it
|
|
186
|
+
}
|
|
187
|
+
if (delivered)
|
|
188
|
+
await this.tasks.o.store.transaction(async (tx) => {
|
|
189
|
+
const cur = await tx.get('inbox', id);
|
|
190
|
+
if (cur)
|
|
191
|
+
await tx.update('inbox', id, cur.revision, {
|
|
192
|
+
...cur.value,
|
|
193
|
+
markers: {
|
|
194
|
+
...cur.value.markers,
|
|
195
|
+
deliveredAt: this.tasks.o.now(),
|
|
196
|
+
},
|
|
197
|
+
});
|
|
198
|
+
});
|
|
199
|
+
}
|
|
200
|
+
}
|
|
201
|
+
async project(event) {
|
|
202
|
+
if (event.type !== 'notificationCreated' &&
|
|
203
|
+
event.type !== 'humanActionRequested' &&
|
|
204
|
+
event.type !== 'humanActionResolved' &&
|
|
205
|
+
event.type !== 'taskCompleted' &&
|
|
206
|
+
event.type !== 'taskFailed')
|
|
207
|
+
return;
|
|
208
|
+
await this.tasks.o.store.transaction(async (tx) => {
|
|
209
|
+
const task = (await tx.get('tasks', event.taskId))?.value;
|
|
210
|
+
if (!task)
|
|
211
|
+
return;
|
|
212
|
+
switch (event.type) {
|
|
213
|
+
case 'notificationCreated':
|
|
214
|
+
return this.notification(tx, event, task);
|
|
215
|
+
case 'humanActionRequested':
|
|
216
|
+
return this.actionRequested(tx, event, task);
|
|
217
|
+
case 'humanActionResolved':
|
|
218
|
+
return this.actionResolved(tx, event);
|
|
219
|
+
case 'taskCompleted':
|
|
220
|
+
case 'taskFailed':
|
|
221
|
+
if (this.projectTerminal(task, event.type))
|
|
222
|
+
return this.upsert(tx, event, `entry:task:${task.taskId}`, {
|
|
223
|
+
ownerId: task.ownerId,
|
|
224
|
+
task,
|
|
225
|
+
severity: 'high',
|
|
226
|
+
title: event.type === 'taskFailed' ? 'Task failed' : 'Task completed',
|
|
227
|
+
...(task.outcome ? { body: task.outcome.message } : {}),
|
|
228
|
+
sourceType: event.type,
|
|
229
|
+
});
|
|
230
|
+
}
|
|
231
|
+
});
|
|
232
|
+
}
|
|
233
|
+
async notification(tx, event, task) {
|
|
234
|
+
const d = event.data;
|
|
235
|
+
await this.upsert(tx, event, d.entryId, {
|
|
236
|
+
ownerId: task.ownerId,
|
|
237
|
+
task,
|
|
238
|
+
severity: d.severity,
|
|
239
|
+
title: d.title,
|
|
240
|
+
body: d.body,
|
|
241
|
+
sourceType: 'notificationCreated',
|
|
242
|
+
});
|
|
243
|
+
}
|
|
244
|
+
async actionRequested(tx, event, task) {
|
|
245
|
+
const d = event.data;
|
|
246
|
+
const users = d.assignees.principalIds?.length
|
|
247
|
+
? d.assignees.principalIds
|
|
248
|
+
: [task.ownerId];
|
|
249
|
+
for (const user of users)
|
|
250
|
+
await this.upsert(tx, event, actionEntryId(d.requestId, user), {
|
|
251
|
+
ownerId: user,
|
|
252
|
+
task,
|
|
253
|
+
severity: 'high',
|
|
254
|
+
title: d.prompt,
|
|
255
|
+
actionRequestId: d.requestId,
|
|
256
|
+
sourceType: 'humanActionRequested',
|
|
257
|
+
});
|
|
258
|
+
}
|
|
259
|
+
async actionResolved(tx, event) {
|
|
260
|
+
const d = event.data;
|
|
261
|
+
for (const user of d.assignees) {
|
|
262
|
+
const doc = await tx.get('inbox', actionEntryId(d.requestId, user));
|
|
263
|
+
if (!doc || doc.value.applied.includes(event.eventId))
|
|
264
|
+
continue;
|
|
265
|
+
await tx.update('inbox', doc.id, doc.revision, settle({
|
|
266
|
+
...doc.value,
|
|
267
|
+
status: 'resolved',
|
|
268
|
+
revision: doc.value.revision + 1,
|
|
269
|
+
markers: { ...doc.value.markers, resolvedAt: event.occurredAt },
|
|
270
|
+
applied: [...doc.value.applied, event.eventId].slice(-APPLIED_KEPT),
|
|
271
|
+
}));
|
|
272
|
+
}
|
|
273
|
+
}
|
|
274
|
+
async upsert(tx, event, entryId, e) {
|
|
275
|
+
const cur = await tx.get('inbox', entryId);
|
|
276
|
+
if (cur?.value.applied.includes(event.eventId))
|
|
277
|
+
return;
|
|
278
|
+
this.touched.push(entryId);
|
|
279
|
+
const content = {
|
|
280
|
+
source: {
|
|
281
|
+
eventId: event.eventId,
|
|
282
|
+
taskId: event.taskId,
|
|
283
|
+
type: e.sourceType,
|
|
284
|
+
},
|
|
285
|
+
severity: e.severity,
|
|
286
|
+
title: e.title,
|
|
287
|
+
...(e.body !== undefined ? { body: e.body } : {}),
|
|
288
|
+
...(e.actionRequestId ? { actionRequestId: e.actionRequestId } : {}),
|
|
289
|
+
};
|
|
290
|
+
if (!cur) {
|
|
291
|
+
await tx.insert('inbox', entryId, settle({
|
|
292
|
+
entryId,
|
|
293
|
+
userId: e.ownerId,
|
|
294
|
+
ownerId: e.ownerId,
|
|
295
|
+
tenantId: e.task.tenantId,
|
|
296
|
+
agentInstanceId: e.task.instanceId,
|
|
297
|
+
...content,
|
|
298
|
+
revision: 1,
|
|
299
|
+
markers: {},
|
|
300
|
+
status: 'open',
|
|
301
|
+
applied: [event.eventId],
|
|
302
|
+
}));
|
|
303
|
+
return;
|
|
304
|
+
}
|
|
305
|
+
// A later event for the same entry (deduplicated notification) revises it, so it is presented again.
|
|
306
|
+
const { body: _b, ...previous } = cur.value;
|
|
307
|
+
void _b;
|
|
308
|
+
await tx.update('inbox', cur.id, cur.revision, settle({
|
|
309
|
+
...previous,
|
|
310
|
+
...content,
|
|
311
|
+
revision: cur.value.revision + 1,
|
|
312
|
+
applied: [...cur.value.applied, event.eventId].slice(-APPLIED_KEPT),
|
|
313
|
+
}));
|
|
314
|
+
}
|
|
315
|
+
}
|
|
316
|
+
export const actionEntryId = (requestId, user) => `entry:action:${hashKey(requestId, user)}`;
|
|
317
|
+
function validateNotify(input) {
|
|
318
|
+
const n = input;
|
|
319
|
+
if (!n ||
|
|
320
|
+
!SEVERITIES.includes(n.severity) ||
|
|
321
|
+
typeof n.title !== 'string' ||
|
|
322
|
+
!n.title ||
|
|
323
|
+
typeof n.body !== 'string' ||
|
|
324
|
+
(n.dedupeKey !== undefined && typeof n.dedupeKey !== 'string'))
|
|
325
|
+
throw new AgentropyError('SchemaViolation', 'notification.notify needs severity, title and body');
|
|
326
|
+
return {
|
|
327
|
+
severity: n.severity,
|
|
328
|
+
title: n.title,
|
|
329
|
+
body: n.body,
|
|
330
|
+
...(n.dedupeKey ? { dedupeKey: n.dedupeKey } : {}),
|
|
331
|
+
};
|
|
332
|
+
}
|
|
333
|
+
/**
|
|
334
|
+
* Platform-internal effect journal: runs `effect` at most once per idempotency key, in the same transaction
|
|
335
|
+
* as the journal write, and returns the original result on replay.
|
|
336
|
+
*/
|
|
337
|
+
export async function journaled(tasks, ctx, operation, effect) {
|
|
338
|
+
if (!ctx.idempotencyKey)
|
|
339
|
+
throw new AgentropyError('SchemaViolation', `${operation} requires a stepKey`, {
|
|
340
|
+
details: { reason: 'missingStepKey' },
|
|
341
|
+
});
|
|
342
|
+
const effectId = `effect:${ctx.idempotencyKey}`;
|
|
343
|
+
return tasks.o.store.transaction(async (tx) => {
|
|
344
|
+
const done = await tx.get('effects', effectId);
|
|
345
|
+
if (done)
|
|
346
|
+
return done.value.result;
|
|
347
|
+
const task = await tx.get('tasks', ctx.taskId);
|
|
348
|
+
if (!task || task.value.tenantId !== ctx.tenantId)
|
|
349
|
+
throw new AgentropyError('NotFound', 'Task not found');
|
|
350
|
+
const result = await effect(tx, task);
|
|
351
|
+
await tx.insert('effects', effectId, {
|
|
352
|
+
taskId: ctx.taskId,
|
|
353
|
+
operation,
|
|
354
|
+
...(ctx.stepKey ? { stepKey: ctx.stepKey } : {}),
|
|
355
|
+
result,
|
|
356
|
+
at: tasks.o.now(),
|
|
357
|
+
});
|
|
358
|
+
return result;
|
|
359
|
+
});
|
|
360
|
+
}
|
|
361
|
+
/** Built-in `notification.notify` and `conversation.briefing` handlers. */
|
|
362
|
+
export function inboxOperationHandlers(tasks, inbox) {
|
|
363
|
+
return {
|
|
364
|
+
notify: async (input, ctx) => {
|
|
365
|
+
const n = validateNotify(input);
|
|
366
|
+
return journaled(tasks, ctx, 'notification.notify', async (tx, task) => {
|
|
367
|
+
const entryId = n.dedupeKey
|
|
368
|
+
? `entry:${hashKey(task.value.tenantId, task.value.ownerId, 'dedupe', n.dedupeKey)}`
|
|
369
|
+
: `entry:${hashKey(ctx.idempotencyKey)}`;
|
|
370
|
+
await tasks.emit(tx, task.value, task.revision, 'notificationCreated', {
|
|
371
|
+
...n,
|
|
372
|
+
entryId,
|
|
373
|
+
...(ctx.stepKey ? { stepKey: ctx.stepKey } : {}),
|
|
374
|
+
});
|
|
375
|
+
return { entryId };
|
|
376
|
+
});
|
|
377
|
+
},
|
|
378
|
+
briefing: async (input, ctx) => {
|
|
379
|
+
const { limit } = (input ?? {});
|
|
380
|
+
const task = await tasks.o.store.transaction((tx) => tx.get('tasks', ctx.taskId));
|
|
381
|
+
if (!task || task.value.tenantId !== ctx.tenantId)
|
|
382
|
+
throw new AgentropyError('NotFound', 'Task not found');
|
|
383
|
+
// Only a turn the owner submitted may read the owner's inbox; a delegated turn from someone else may not.
|
|
384
|
+
if (task.value.callerId !== task.value.ownerId)
|
|
385
|
+
throw new AgentropyError('PermissionDenied', 'Briefing is only available to turns submitted by the inbox owner');
|
|
386
|
+
return inbox.briefing({ id: task.value.ownerId, type: 'user', tenantId: task.value.tenantId }, limit);
|
|
387
|
+
},
|
|
388
|
+
};
|
|
389
|
+
}
|
package/dist/index.d.ts
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @agentropy/runtime — registries, providers and services. */
|
|
2
|
+
export declare const packageName: "@agentropy/runtime";
|
|
3
|
+
export * from './implementation.js';
|
|
4
|
+
export * from './registry.js';
|
|
5
|
+
export * from './tools.js';
|
|
6
|
+
export * from './skills.js';
|
|
7
|
+
export * from './communication.js';
|
|
8
|
+
export * from './runtime.js';
|
|
9
|
+
export * from './task-service.js';
|
|
10
|
+
export * from './wakeups.js';
|
|
11
|
+
export * from './outbox.js';
|
|
12
|
+
export * from './inbox.js';
|
|
13
|
+
export * from './human-actions.js';
|
|
14
|
+
export * from './audit.js';
|
|
15
|
+
export * from './app.js';
|
|
16
|
+
export * from './priority.js';
|
|
17
|
+
export * from './otel.js';
|
|
18
|
+
export * from './ingress.js';
|
|
19
|
+
export * from './limits.js';
|
|
20
|
+
export * from './policy-cache.js';
|
|
21
|
+
export * from './reasoning.js';
|
|
22
|
+
export * from './workflow.js';
|
|
23
|
+
export * from './quotas.js';
|
|
24
|
+
export * from './a2a.js';
|
|
25
|
+
export * from './a2a-history.js';
|
|
26
|
+
export * from './a2a-push-receiver.js';
|
|
27
|
+
export * from './remote.js';
|
|
28
|
+
export * from './federation.js';
|
|
29
|
+
export * from './compaction.js';
|
|
30
|
+
export * from './mcp.js';
|
|
31
|
+
export * from './memory.js';
|
|
32
|
+
export * from './model-routing.js';
|
package/dist/index.js
ADDED
|
@@ -0,0 +1,32 @@
|
|
|
1
|
+
/** @agentropy/runtime — registries, providers and services. */
|
|
2
|
+
export const packageName = '@agentropy/runtime';
|
|
3
|
+
export * from './implementation.js';
|
|
4
|
+
export * from './registry.js';
|
|
5
|
+
export * from './tools.js';
|
|
6
|
+
export * from './skills.js';
|
|
7
|
+
export * from './communication.js';
|
|
8
|
+
export * from './runtime.js';
|
|
9
|
+
export * from './task-service.js';
|
|
10
|
+
export * from './wakeups.js';
|
|
11
|
+
export * from './outbox.js';
|
|
12
|
+
export * from './inbox.js';
|
|
13
|
+
export * from './human-actions.js';
|
|
14
|
+
export * from './audit.js';
|
|
15
|
+
export * from './app.js';
|
|
16
|
+
export * from './priority.js';
|
|
17
|
+
export * from './otel.js';
|
|
18
|
+
export * from './ingress.js';
|
|
19
|
+
export * from './limits.js';
|
|
20
|
+
export * from './policy-cache.js';
|
|
21
|
+
export * from './reasoning.js';
|
|
22
|
+
export * from './workflow.js';
|
|
23
|
+
export * from './quotas.js';
|
|
24
|
+
export * from './a2a.js';
|
|
25
|
+
export * from './a2a-history.js';
|
|
26
|
+
export * from './a2a-push-receiver.js';
|
|
27
|
+
export * from './remote.js';
|
|
28
|
+
export * from './federation.js';
|
|
29
|
+
export * from './compaction.js';
|
|
30
|
+
export * from './mcp.js';
|
|
31
|
+
export * from './memory.js';
|
|
32
|
+
export * from './model-routing.js';
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Out-of-process activation ingress (M2-02, FEAT-027.3): a transport-neutral handler that HTTP endpoints, queue
|
|
3
|
+
* consumers or webhooks call with an `AgentMessage` envelope and the principal *their* authentication produced.
|
|
4
|
+
*
|
|
5
|
+
* - The envelope is validated (`validateAgentMessage`); actor and tenant claims in it are ignored — the trusted
|
|
6
|
+
* principal from the transport is the only identity (AC-09).
|
|
7
|
+
* - `agent.delegate` (command) accepts a task: the envelope `idempotencyKey` deduplicates redeliveries.
|
|
8
|
+
* - `agent.event` and `agent.callback` (event/command) publish an inbound event: the envelope `messageId` is the
|
|
9
|
+
* event ID, so a redelivered message is recorded once and wakes nothing twice.
|
|
10
|
+
* - Errors come back as the public Errors v1 shape; nothing internal leaks.
|
|
11
|
+
*/
|
|
12
|
+
import { type AgentropyErrorShape, type TrustedPrincipal } from '@agentropy/core';
|
|
13
|
+
import type { LocalApp } from './app.js';
|
|
14
|
+
export type IngressResult = {
|
|
15
|
+
status: 'accepted';
|
|
16
|
+
taskId: string;
|
|
17
|
+
durability: string;
|
|
18
|
+
} | {
|
|
19
|
+
status: 'recorded';
|
|
20
|
+
eventId: string;
|
|
21
|
+
} | {
|
|
22
|
+
status: 'duplicate';
|
|
23
|
+
eventId: string;
|
|
24
|
+
} | {
|
|
25
|
+
status: 'rejected';
|
|
26
|
+
error: AgentropyErrorShape;
|
|
27
|
+
};
|
|
28
|
+
/** Inbound callback events use this source; handlers subscribe with `{ source: CALLBACK_SOURCE, filter: { callbackId } }`. */
|
|
29
|
+
export declare const CALLBACK_SOURCE = "callback";
|
|
30
|
+
export interface ActivationIngress {
|
|
31
|
+
receive(message: unknown, principal: TrustedPrincipal, transport?: {
|
|
32
|
+
traceparent?: string;
|
|
33
|
+
}): Promise<IngressResult>;
|
|
34
|
+
}
|
|
35
|
+
export declare function createActivationIngress(app: LocalApp): ActivationIngress;
|
package/dist/ingress.js
ADDED
|
@@ -0,0 +1,86 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Out-of-process activation ingress (M2-02, FEAT-027.3): a transport-neutral handler that HTTP endpoints, queue
|
|
3
|
+
* consumers or webhooks call with an `AgentMessage` envelope and the principal *their* authentication produced.
|
|
4
|
+
*
|
|
5
|
+
* - The envelope is validated (`validateAgentMessage`); actor and tenant claims in it are ignored — the trusted
|
|
6
|
+
* principal from the transport is the only identity (AC-09).
|
|
7
|
+
* - `agent.delegate` (command) accepts a task: the envelope `idempotencyKey` deduplicates redeliveries.
|
|
8
|
+
* - `agent.event` and `agent.callback` (event/command) publish an inbound event: the envelope `messageId` is the
|
|
9
|
+
* event ID, so a redelivered message is recorded once and wakes nothing twice.
|
|
10
|
+
* - Errors come back as the public Errors v1 shape; nothing internal leaks.
|
|
11
|
+
*/
|
|
12
|
+
import { AgentropyError, enforcePolicy, sanitizeTraceparent, toAgentropyError, validateAgentMessage, } from '@agentropy/core';
|
|
13
|
+
/** Inbound callback events use this source; handlers subscribe with `{ source: CALLBACK_SOURCE, filter: { callbackId } }`. */
|
|
14
|
+
export const CALLBACK_SOURCE = 'callback';
|
|
15
|
+
export function createActivationIngress(app) {
|
|
16
|
+
return {
|
|
17
|
+
async receive(message, principal, transport = {}) {
|
|
18
|
+
try {
|
|
19
|
+
if (!principal?.id || !principal.tenantId)
|
|
20
|
+
throw new AgentropyError('Unauthenticated', 'No trusted principal');
|
|
21
|
+
const m = validateAgentMessage(message);
|
|
22
|
+
const traceparent = sanitizeTraceparent(transport.traceparent);
|
|
23
|
+
switch (m.name) {
|
|
24
|
+
case 'agent.delegate': {
|
|
25
|
+
if (m.kind !== 'command')
|
|
26
|
+
throw new AgentropyError('SchemaViolation', 'agent.delegate must be a command');
|
|
27
|
+
const p = m.payload;
|
|
28
|
+
if (!p?.target?.instanceId)
|
|
29
|
+
throw new AgentropyError('SchemaViolation', 'agent.delegate needs payload.target.instanceId');
|
|
30
|
+
const handle = await app.delegate({
|
|
31
|
+
target: { instanceId: p.target.instanceId },
|
|
32
|
+
input: p.input,
|
|
33
|
+
idempotencyKey: m.idempotencyKey,
|
|
34
|
+
correlationId: m.correlationId,
|
|
35
|
+
...(p.deadline ? { deadline: p.deadline } : {}),
|
|
36
|
+
}, principal, traceparent ? { traceparent } : {});
|
|
37
|
+
return {
|
|
38
|
+
status: 'accepted',
|
|
39
|
+
taskId: handle.taskId,
|
|
40
|
+
durability: handle.durability,
|
|
41
|
+
};
|
|
42
|
+
}
|
|
43
|
+
case 'agent.event':
|
|
44
|
+
case 'agent.callback': {
|
|
45
|
+
const p = m.payload;
|
|
46
|
+
const source = m.name === 'agent.callback' ? CALLBACK_SOURCE : p?.source;
|
|
47
|
+
if (!source || (m.name === 'agent.callback' && !p.callbackId))
|
|
48
|
+
throw new AgentropyError('SchemaViolation', m.name === 'agent.callback'
|
|
49
|
+
? 'agent.callback needs payload.callbackId'
|
|
50
|
+
: 'agent.event needs payload.source');
|
|
51
|
+
await enforcePolicy(app.tasks.o.pdp, {
|
|
52
|
+
principal,
|
|
53
|
+
action: 'event:publish',
|
|
54
|
+
resource: {
|
|
55
|
+
type: 'event-source',
|
|
56
|
+
id: source,
|
|
57
|
+
tenantId: principal.tenantId,
|
|
58
|
+
},
|
|
59
|
+
});
|
|
60
|
+
const fresh = await app.publishEvent({
|
|
61
|
+
eventId: m.messageId,
|
|
62
|
+
source,
|
|
63
|
+
// The transport's principal decides the tenant, never the envelope.
|
|
64
|
+
tenantId: principal.tenantId,
|
|
65
|
+
payload: m.name === 'agent.callback'
|
|
66
|
+
? { callbackId: p.callbackId, data: p.data }
|
|
67
|
+
: p.data,
|
|
68
|
+
...(traceparent ? { traceparent } : {}),
|
|
69
|
+
});
|
|
70
|
+
return fresh
|
|
71
|
+
? { status: 'recorded', eventId: m.messageId }
|
|
72
|
+
: { status: 'duplicate', eventId: m.messageId };
|
|
73
|
+
}
|
|
74
|
+
default:
|
|
75
|
+
throw new AgentropyError('UnsupportedOperation', `Unknown message ${m.name}`);
|
|
76
|
+
}
|
|
77
|
+
}
|
|
78
|
+
catch (err) {
|
|
79
|
+
return {
|
|
80
|
+
status: 'rejected',
|
|
81
|
+
error: toAgentropyError(err).toJSON(),
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
},
|
|
85
|
+
};
|
|
86
|
+
}
|
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle status of tools and skills kept in the store (FEAT-074; Tool v1 revision 6, Skill v1). The registries hold
|
|
3
|
+
* descriptors and files that the application registers at every start; what must outlive a process and reach every
|
|
4
|
+
* worker is the status an operator set: `active`, `suspended`, `revoked`. It is one row per tool or skill version in the
|
|
5
|
+
* `lifecycle` collection. A row is only ever tightened by a restart: `register` adopts it, and `revoked` is final.
|
|
6
|
+
*/
|
|
7
|
+
import { type Store, type VersionedRef } from '@agentropy/core';
|
|
8
|
+
export type LifecycleStatus = 'registered' | 'active' | 'suspended' | 'revoked';
|
|
9
|
+
export type LifecycleKind = 'tool' | 'skill';
|
|
10
|
+
export interface LifecycleRow {
|
|
11
|
+
kind: LifecycleKind;
|
|
12
|
+
id: string;
|
|
13
|
+
version: string;
|
|
14
|
+
status: LifecycleStatus;
|
|
15
|
+
/** Content digest of what the status was set for: a row never applies to different content. */
|
|
16
|
+
digest?: string;
|
|
17
|
+
by?: string;
|
|
18
|
+
at?: string;
|
|
19
|
+
}
|
|
20
|
+
export declare const lifecycleKey: (kind: LifecycleKind, ref: VersionedRef) => string;
|
|
21
|
+
/** The status row of one version, if an operator ever set one. */
|
|
22
|
+
export declare const readLifecycle: (store: Store, kind: LifecycleKind, ref: VersionedRef) => Promise<import("@agentropy/core").StoredDocument<LifecycleRow> | undefined>;
|
|
23
|
+
/** Every status row of one kind, by version key. */
|
|
24
|
+
export declare function readLifecycles(store: Store, kind: LifecycleKind): Promise<Map<string, LifecycleRow>>;
|
|
25
|
+
/**
|
|
26
|
+
* The status to act on: the stored row when it applies to this content, else what this process knows. An `active` row
|
|
27
|
+
* does not make unverified content usable, and a row for other content (a digest that differs) is not adopted: only
|
|
28
|
+
* a stricter status is ever taken from a row that cannot be tied to the content.
|
|
29
|
+
*/
|
|
30
|
+
export declare function effectiveStatus(local: LifecycleStatus, row: LifecycleRow | undefined, digest: string, verified: boolean): LifecycleStatus;
|
|
31
|
+
/** A row that names other content than the one being registered: the version is already taken. */
|
|
32
|
+
export declare function conflicts(row: LifecycleRow | undefined, digest: string): boolean;
|
|
33
|
+
/**
|
|
34
|
+
* Moves the stored status in one transaction: it re-reads the row, refuses a move the current status does not allow
|
|
35
|
+
* (`revoked` is final) and writes the new one, so two workers cannot undo each other's operator action.
|
|
36
|
+
*/
|
|
37
|
+
export declare function moveLifecycle(store: Store, kind: LifecycleKind, ref: VersionedRef, change: {
|
|
38
|
+
from: LifecycleStatus[];
|
|
39
|
+
to: LifecycleStatus;
|
|
40
|
+
/** The status this process knows, when no row exists yet. */
|
|
41
|
+
local: LifecycleStatus;
|
|
42
|
+
digest: string;
|
|
43
|
+
by: string;
|
|
44
|
+
at: string;
|
|
45
|
+
}): Promise<void>;
|
|
@@ -0,0 +1,57 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Lifecycle status of tools and skills kept in the store (FEAT-074; Tool v1 revision 6, Skill v1). The registries hold
|
|
3
|
+
* descriptors and files that the application registers at every start; what must outlive a process and reach every
|
|
4
|
+
* worker is the status an operator set: `active`, `suspended`, `revoked`. It is one row per tool or skill version in the
|
|
5
|
+
* `lifecycle` collection. A row is only ever tightened by a restart: `register` adopts it, and `revoked` is final.
|
|
6
|
+
*/
|
|
7
|
+
import { AgentropyError, } from '@agentropy/core';
|
|
8
|
+
export const lifecycleKey = (kind, ref) => `${kind}:${ref.id}@${ref.version}`;
|
|
9
|
+
/** The status row of one version, if an operator ever set one. */
|
|
10
|
+
export const readLifecycle = (store, kind, ref) => store.transaction((tx) => tx.get('lifecycle', lifecycleKey(kind, ref)));
|
|
11
|
+
/** Every status row of one kind, by version key. */
|
|
12
|
+
export async function readLifecycles(store, kind) {
|
|
13
|
+
const rows = await store.transaction((tx) => tx.find('lifecycle', { where: { kind } }));
|
|
14
|
+
return new Map(rows.map((r) => [lifecycleKey(kind, r.value), r.value]));
|
|
15
|
+
}
|
|
16
|
+
/**
|
|
17
|
+
* The status to act on: the stored row when it applies to this content, else what this process knows. An `active` row
|
|
18
|
+
* does not make unverified content usable, and a row for other content (a digest that differs) is not adopted: only
|
|
19
|
+
* a stricter status is ever taken from a row that cannot be tied to the content.
|
|
20
|
+
*/
|
|
21
|
+
export function effectiveStatus(local, row, digest, verified) {
|
|
22
|
+
if (!row)
|
|
23
|
+
return local;
|
|
24
|
+
const sameContent = row.digest === digest;
|
|
25
|
+
if (row.status === 'active')
|
|
26
|
+
return sameContent && verified ? 'active' : local;
|
|
27
|
+
return row.status;
|
|
28
|
+
}
|
|
29
|
+
/** A row that names other content than the one being registered: the version is already taken. */
|
|
30
|
+
export function conflicts(row, digest) {
|
|
31
|
+
return row?.digest !== undefined && row.digest !== digest;
|
|
32
|
+
}
|
|
33
|
+
/**
|
|
34
|
+
* Moves the stored status in one transaction: it re-reads the row, refuses a move the current status does not allow
|
|
35
|
+
* (`revoked` is final) and writes the new one, so two workers cannot undo each other's operator action.
|
|
36
|
+
*/
|
|
37
|
+
export async function moveLifecycle(store, kind, ref, change) {
|
|
38
|
+
await store.transaction(async (tx) => {
|
|
39
|
+
const cur = await tx.get('lifecycle', lifecycleKey(kind, ref));
|
|
40
|
+
const status = cur?.value.status ?? change.local;
|
|
41
|
+
if (!change.from.includes(status))
|
|
42
|
+
throw new AgentropyError('InvalidState', `Cannot move a ${status} ${kind}`, { details: { status } });
|
|
43
|
+
const value = {
|
|
44
|
+
kind,
|
|
45
|
+
id: ref.id,
|
|
46
|
+
version: ref.version,
|
|
47
|
+
status: change.to,
|
|
48
|
+
digest: change.digest,
|
|
49
|
+
by: change.by,
|
|
50
|
+
at: change.at,
|
|
51
|
+
};
|
|
52
|
+
if (cur)
|
|
53
|
+
await tx.update('lifecycle', cur.id, cur.revision, value);
|
|
54
|
+
else
|
|
55
|
+
await tx.insert('lifecycle', lifecycleKey(kind, ref), value);
|
|
56
|
+
});
|
|
57
|
+
}
|