@atolis-hq/wake 0.3.34 → 0.3.35
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.
|
@@ -37,7 +37,10 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
|
|
|
37
37
|
const resources = createResourceService(journal, lookup);
|
|
38
38
|
const pullRequests = createPullRequestService(journal, work, resources);
|
|
39
39
|
const activities = options.activities ??
|
|
40
|
-
createBuiltInActivityRegistry(journal, pullRequests, resources, wakeRoot, createGitHubAgentContextReader(journal, resources
|
|
40
|
+
createBuiltInActivityRegistry(journal, pullRequests, resources, wakeRoot, createGitHubAgentContextReader(journal, resources, {
|
|
41
|
+
publicUiUrl: config.surfaces.web.publicUrl,
|
|
42
|
+
githubAdapters: githubAdapters(config),
|
|
43
|
+
}));
|
|
41
44
|
const definitions = Object.fromEntries(Object.entries(config.orchestration.workflows).map(([name, definition]) => [
|
|
42
45
|
name,
|
|
43
46
|
compileWorkflow(name, definition, activities, Object.keys(config.orchestration.workflows)),
|
|
@@ -156,3 +159,6 @@ export async function createCompositionRoot(wakeRoot, options = {}) {
|
|
|
156
159
|
...runtime,
|
|
157
160
|
};
|
|
158
161
|
}
|
|
162
|
+
function githubAdapters(config) {
|
|
163
|
+
return Object.entries(config.integrations).flatMap(([adapter, integration]) => integration.enabled && (integration.provider ?? adapter) === 'github' ? [adapter] : []);
|
|
164
|
+
}
|
|
@@ -4,9 +4,9 @@ import { adapterId } from '../../contracts/identifiers.js';
|
|
|
4
4
|
import { integrationStream } from '../../contracts/streams.js';
|
|
5
5
|
import { boundedDiagnosticEvidence } from '../contracts/check-evidence.js';
|
|
6
6
|
import { GitHubEventType, selectGitHubAdapterEvent } from '../contracts/events.js';
|
|
7
|
-
import { createCommentHistoryReader } from './comment-history-reader.js';
|
|
8
|
-
export function createGitHubAgentContextReader(journal, resources) {
|
|
9
|
-
const commentHistory = createCommentHistoryReader(journal, resources);
|
|
7
|
+
import { createCommentHistoryReader, } from './comment-history-reader.js';
|
|
8
|
+
export function createGitHubAgentContextReader(journal, resources, options = {}) {
|
|
9
|
+
const commentHistory = createCommentHistoryReader(journal, resources, options);
|
|
10
10
|
return {
|
|
11
11
|
async forWorkItem(workItemId, options) {
|
|
12
12
|
const comments = await commentHistory.forWorkItem(workItemId, options);
|
|
@@ -1,40 +1,149 @@
|
|
|
1
1
|
import { ResourceCorrelationRole } from '../../../resources/index.js';
|
|
2
2
|
import { adapterId } from '../../contracts/identifiers.js';
|
|
3
|
+
import { DeliveryEventType, selectDeliveryEvent } from '../../delivery/contracts/events.js';
|
|
4
|
+
import { DeliveryIntentEventType, selectDeliveryIntentEvent, } from '../../delivery/contracts/intents.js';
|
|
5
|
+
import { DeliveryResultKind } from '../../delivery/contracts/vocabulary.js';
|
|
3
6
|
import { GitHubEventType, selectGitHubAdapterEvent } from '../contracts/events.js';
|
|
4
|
-
|
|
7
|
+
import { GitHubAdapter, UnknownGitHubIdentity } from '../contracts/vocabulary.js';
|
|
8
|
+
import { formatAgentRunComment } from './agent-run-comment.js';
|
|
9
|
+
export function createCommentHistoryReader(journal, resources, readerOptions = {}) {
|
|
5
10
|
return {
|
|
6
11
|
async forWorkItem(workItemId, options) {
|
|
7
|
-
const
|
|
12
|
+
const resourcesById = new Map((await Promise.all((await resources.correlationsForWork(workItemId))
|
|
8
13
|
.filter((correlation) => correlation.role === ResourceCorrelationRole.Primary)
|
|
9
|
-
.map((correlation) => resources.get(correlation.resourceId)))).flatMap((resource) =>
|
|
10
|
-
|
|
14
|
+
.map((correlation) => resources.get(correlation.resourceId)))).flatMap((resource) => resource === null ? [] : [[resource.resourceId, resource]]));
|
|
15
|
+
const keys = new Set([...resourcesById.values()].flatMap((resource) => {
|
|
16
|
+
if (parseAdapterId(resource.externalKey.adapter) === null)
|
|
11
17
|
return [];
|
|
12
18
|
return [`${resource.externalKey.adapter}:${resource.externalKey.key}`];
|
|
13
19
|
}));
|
|
14
20
|
if (keys.size === 0)
|
|
15
21
|
return [];
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
22
|
+
const githubAdapters = new Set(readerOptions.githubAdapters ?? [GitHubAdapter]);
|
|
23
|
+
const githubResourceIds = new Set([...resourcesById.entries()].flatMap(([resourceId, resource]) => githubAdapters.has(resource.externalKey.adapter) ? [resourceId] : []));
|
|
24
|
+
return readCommentHistory(await journal.readAll(0), resourcesById, keys, githubResourceIds, options?.observedSince, readerOptions.publicUiUrl);
|
|
25
|
+
},
|
|
26
|
+
};
|
|
27
|
+
}
|
|
28
|
+
function readCommentHistory(events, resourcesById, keys, githubResourceIds, observedSince, publicUiUrl) {
|
|
29
|
+
const intents = commentIntents(events, githubResourceIds);
|
|
30
|
+
const observed = providerComments(events, keys);
|
|
31
|
+
const confirmed = confirmations(events, intents);
|
|
32
|
+
return reconcileCommentHistory(observed, confirmed, resourcesById, observedSince, publicUiUrl);
|
|
33
|
+
}
|
|
34
|
+
function commentIntents(events, githubResourceIds) {
|
|
35
|
+
const intents = new Map();
|
|
36
|
+
for (const event of events) {
|
|
37
|
+
const intent = selectDeliveryIntentEvent(event);
|
|
38
|
+
if (intent !== null &&
|
|
39
|
+
isCommentIntent(intent) &&
|
|
40
|
+
githubResourceIds.has(intent.payload.resourceId))
|
|
41
|
+
intents.set(intent.eventId, intent);
|
|
42
|
+
}
|
|
43
|
+
return intents;
|
|
44
|
+
}
|
|
45
|
+
function providerComments(events, keys) {
|
|
46
|
+
const observed = new Map();
|
|
47
|
+
for (const event of events) {
|
|
48
|
+
const comment = providerComment(event, keys);
|
|
49
|
+
if (comment !== null)
|
|
50
|
+
observed.set(event.eventId, comment);
|
|
51
|
+
}
|
|
52
|
+
return observed;
|
|
53
|
+
}
|
|
54
|
+
function confirmations(events, intents) {
|
|
55
|
+
const confirmed = new Map();
|
|
56
|
+
for (const event of events) {
|
|
57
|
+
const delivery = selectDeliveryEvent(event);
|
|
58
|
+
if (delivery === null || !isConfirmed(delivery))
|
|
59
|
+
continue;
|
|
60
|
+
const intent = intents.get(delivery.payload.intentEventId);
|
|
61
|
+
if (intent === undefined)
|
|
62
|
+
continue;
|
|
63
|
+
const candidate = { event, intent };
|
|
64
|
+
const current = confirmed.get(delivery.payload.intentEventId);
|
|
65
|
+
if (current === undefined || candidate.event.globalPosition < current.event.globalPosition)
|
|
66
|
+
confirmed.set(delivery.payload.intentEventId, candidate);
|
|
67
|
+
}
|
|
68
|
+
return confirmed;
|
|
69
|
+
}
|
|
70
|
+
function reconcileCommentHistory(observed, confirmed, resourcesById, observedSince, publicUiUrl) {
|
|
71
|
+
const history = new Map();
|
|
72
|
+
for (const entry of observed.values())
|
|
73
|
+
history.set(entry.event.eventId, {
|
|
74
|
+
entry: entry.value,
|
|
75
|
+
occurredAt: entry.event.occurredAt,
|
|
76
|
+
globalPosition: entry.event.globalPosition,
|
|
77
|
+
});
|
|
78
|
+
for (const [intentEventId, confirmation] of confirmed) {
|
|
79
|
+
const resource = resourcesById.get(confirmation.intent.payload.resourceId);
|
|
80
|
+
if (resource === undefined)
|
|
81
|
+
continue;
|
|
82
|
+
const resourceKey = `${resource.externalKey.adapter}:${resource.externalKey.key}`;
|
|
83
|
+
const matchingProvider = [...observed.values()].find((entry) => entry.resourceKey === resourceKey && deliveryMarker(entry.value.body) === intentEventId);
|
|
84
|
+
history.set(intentEventId, {
|
|
85
|
+
entry: matchingProvider?.value ??
|
|
86
|
+
syntheticComment(confirmation.intent, confirmation.event.occurredAt, publicUiUrl),
|
|
87
|
+
occurredAt: confirmation.event.occurredAt,
|
|
88
|
+
globalPosition: confirmation.event.globalPosition,
|
|
89
|
+
});
|
|
90
|
+
if (matchingProvider !== undefined)
|
|
91
|
+
history.delete(matchingProvider.event.eventId);
|
|
92
|
+
}
|
|
93
|
+
return [...history.values()]
|
|
94
|
+
.filter((entry) => observedSince === undefined || entry.occurredAt > observedSince)
|
|
95
|
+
.sort((left, right) => left.globalPosition - right.globalPosition)
|
|
96
|
+
.map((entry) => entry.entry);
|
|
97
|
+
}
|
|
98
|
+
function isCommentIntent(value) {
|
|
99
|
+
return (value.eventType === DeliveryIntentEventType.StatusPublishRequested ||
|
|
100
|
+
value.eventType === DeliveryIntentEventType.ReplyPublishRequested ||
|
|
101
|
+
value.eventType === DeliveryIntentEventType.AgentRunPublishRequested);
|
|
102
|
+
}
|
|
103
|
+
function isConfirmed(value) {
|
|
104
|
+
return (value.eventType === DeliveryEventType.Confirmed ||
|
|
105
|
+
(value.eventType === DeliveryEventType.Reconciled &&
|
|
106
|
+
value.payload.result === DeliveryResultKind.Confirmed));
|
|
107
|
+
}
|
|
108
|
+
function providerComment(event, keys) {
|
|
109
|
+
const observed = selectGitHubAdapterEvent(event);
|
|
110
|
+
if (observed?.eventType !== GitHubEventType.CommentObserved ||
|
|
111
|
+
!keys.has(`${observed.stream.id}:${observed.payload.externalKey}`))
|
|
112
|
+
return null;
|
|
113
|
+
return {
|
|
114
|
+
event,
|
|
115
|
+
resourceKey: `${observed.stream.id}:${observed.payload.externalKey}`,
|
|
116
|
+
value: {
|
|
117
|
+
author: observed.payload.actor.id,
|
|
118
|
+
occurredAt: observed.occurredAt,
|
|
119
|
+
body: observed.payload.body,
|
|
120
|
+
...(observed.payload.reviewKind !== 'issue' || observed.payload.location === undefined
|
|
121
|
+
? {}
|
|
122
|
+
: { location: observed.payload.location }),
|
|
35
123
|
},
|
|
36
124
|
};
|
|
37
125
|
}
|
|
126
|
+
function syntheticComment(intent, occurredAt, publicUiUrl) {
|
|
127
|
+
return {
|
|
128
|
+
author: UnknownGitHubIdentity,
|
|
129
|
+
occurredAt,
|
|
130
|
+
body: deliveredCommentBody(intent, publicUiUrl),
|
|
131
|
+
};
|
|
132
|
+
}
|
|
133
|
+
function deliveredCommentBody(intent, publicUiUrl) {
|
|
134
|
+
const marker = `<!-- wake:delivery:${intent.eventId} -->`;
|
|
135
|
+
const body = intent.eventType === DeliveryIntentEventType.AgentRunPublishRequested
|
|
136
|
+
? formatAgentRunComment({
|
|
137
|
+
idempotencyKey: intent.eventId,
|
|
138
|
+
...intent.payload.report,
|
|
139
|
+
publicUiUrl,
|
|
140
|
+
})
|
|
141
|
+
: intent.payload.body;
|
|
142
|
+
return `${body}\n${marker}`.trim();
|
|
143
|
+
}
|
|
144
|
+
function deliveryMarker(body) {
|
|
145
|
+
return /<!--\s*wake:delivery:([^\s>]+)\s*-->/.exec(body)?.[1];
|
|
146
|
+
}
|
|
38
147
|
function parseAdapterId(value) {
|
|
39
148
|
try {
|
|
40
149
|
return adapterId(value);
|