@resolveio/server-lib 22.3.124 → 22.3.125

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 (34) hide show
  1. package/collections/ai-run.collection.d.ts +3 -0
  2. package/collections/ai-run.collection.js +170 -0
  3. package/collections/ai-run.collection.js.map +1 -0
  4. package/managers/ai-run-evidence.manager.d.ts +36 -0
  5. package/managers/ai-run-evidence.manager.js +377 -0
  6. package/managers/ai-run-evidence.manager.js.map +1 -0
  7. package/managers/openai-usage-ledger.manager.d.ts +1 -0
  8. package/managers/openai-usage-ledger.manager.js +5 -56
  9. package/managers/openai-usage-ledger.manager.js.map +1 -1
  10. package/models/ai-run.model.d.ts +19 -0
  11. package/models/ai-run.model.js +4 -0
  12. package/models/ai-run.model.js.map +1 -0
  13. package/package.json +3 -1
  14. package/public_api.d.ts +7 -0
  15. package/public_api.js +7 -0
  16. package/public_api.js.map +1 -1
  17. package/util/ai-run-evidence-adapters.d.ts +33 -0
  18. package/util/ai-run-evidence-adapters.js +660 -0
  19. package/util/ai-run-evidence-adapters.js.map +1 -0
  20. package/util/ai-run-evidence-dashboard.d.ts +67 -0
  21. package/util/ai-run-evidence-dashboard.js +309 -0
  22. package/util/ai-run-evidence-dashboard.js.map +1 -0
  23. package/util/ai-run-evidence-eval.d.ts +86 -0
  24. package/util/ai-run-evidence-eval.js +854 -0
  25. package/util/ai-run-evidence-eval.js.map +1 -0
  26. package/util/ai-run-evidence.d.ts +212 -0
  27. package/util/ai-run-evidence.js +649 -0
  28. package/util/ai-run-evidence.js.map +1 -0
  29. package/util/ai-runner-qa-tools.d.ts +1 -0
  30. package/util/ai-runner-qa-tools.js +129 -10
  31. package/util/ai-runner-qa-tools.js.map +1 -1
  32. package/util/openai-usage-cost.d.ts +6 -0
  33. package/util/openai-usage-cost.js +92 -0
  34. package/util/openai-usage-cost.js.map +1 -0
@@ -0,0 +1,660 @@
1
+ "use strict";
2
+ var __assign = (this && this.__assign) || function () {
3
+ __assign = Object.assign || function(t) {
4
+ for (var s, i = 1, n = arguments.length; i < n; i++) {
5
+ s = arguments[i];
6
+ for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
7
+ t[p] = s[p];
8
+ }
9
+ return t;
10
+ };
11
+ return __assign.apply(this, arguments);
12
+ };
13
+ var __values = (this && this.__values) || function(o) {
14
+ var s = typeof Symbol === "function" && Symbol.iterator, m = s && o[s], i = 0;
15
+ if (m) return m.call(o);
16
+ if (o && typeof o.length === "number") return {
17
+ next: function () {
18
+ if (o && i >= o.length) o = void 0;
19
+ return { value: o && o[i++], done: !o };
20
+ }
21
+ };
22
+ throw new TypeError(s ? "Object is not iterable." : "Symbol.iterator is not defined.");
23
+ };
24
+ Object.defineProperty(exports, "__esModule", { value: true });
25
+ exports.buildSupportAIRunFromEvidence = buildSupportAIRunFromEvidence;
26
+ exports.buildAICoderAIRunFromEvidence = buildAICoderAIRunFromEvidence;
27
+ exports.buildAssistantAIRunFromEvidence = buildAssistantAIRunFromEvidence;
28
+ var ai_run_evidence_1 = require("./ai-run-evidence");
29
+ function cleanText(value, max) {
30
+ if (max === void 0) { max = 1000; }
31
+ return String(value || '').replace(/\s+/g, ' ').trim().slice(0, max);
32
+ }
33
+ function idText(value, max) {
34
+ if (max === void 0) { max = 160; }
35
+ var normalized = cleanText(value, max);
36
+ return normalized || undefined;
37
+ }
38
+ function asArray(value) {
39
+ return Array.isArray(value) ? value : [];
40
+ }
41
+ function firstText(source, fields, max) {
42
+ var e_1, _a;
43
+ if (max === void 0) { max = 1000; }
44
+ try {
45
+ for (var fields_1 = __values(fields), fields_1_1 = fields_1.next(); !fields_1_1.done; fields_1_1 = fields_1.next()) {
46
+ var field = fields_1_1.value;
47
+ var value = source === null || source === void 0 ? void 0 : source[field];
48
+ var normalized = idText(value, max);
49
+ if (normalized) {
50
+ return normalized;
51
+ }
52
+ }
53
+ }
54
+ catch (e_1_1) { e_1 = { error: e_1_1 }; }
55
+ finally {
56
+ try {
57
+ if (fields_1_1 && !fields_1_1.done && (_a = fields_1.return)) _a.call(fields_1);
58
+ }
59
+ finally { if (e_1) throw e_1.error; }
60
+ }
61
+ return undefined;
62
+ }
63
+ function dateValue(source, fields) {
64
+ var e_2, _a;
65
+ try {
66
+ for (var fields_2 = __values(fields), fields_2_1 = fields_2.next(); !fields_2_1.done; fields_2_1 = fields_2.next()) {
67
+ var field = fields_2_1.value;
68
+ var value = source === null || source === void 0 ? void 0 : source[field];
69
+ if (value) {
70
+ return value;
71
+ }
72
+ }
73
+ }
74
+ catch (e_2_1) { e_2 = { error: e_2_1 }; }
75
+ finally {
76
+ try {
77
+ if (fields_2_1 && !fields_2_1.done && (_a = fields_2.return)) _a.call(fields_2);
78
+ }
79
+ finally { if (e_2) throw e_2.error; }
80
+ }
81
+ return undefined;
82
+ }
83
+ function addSourceId(sourceIds, key, value) {
84
+ var normalized = idText(value);
85
+ if (normalized) {
86
+ sourceIds[key] = normalized;
87
+ }
88
+ }
89
+ function pushEvent(events, event) {
90
+ var _a;
91
+ var message = cleanText(event.message, 1800);
92
+ var metadata = event.metadata || {};
93
+ if (!message && !Object.keys(metadata).length && !((_a = event.artifactPaths) === null || _a === void 0 ? void 0 : _a.length)) {
94
+ return;
95
+ }
96
+ events.push(__assign(__assign({}, event), { message: message, artifactPaths: asArray(event.artifactPaths).filter(Boolean).slice(0, 20) }));
97
+ }
98
+ function eventDate(record) {
99
+ return dateValue(record, ['recordedAt', 'createdAt', 'updatedAt', 'timestamp', 'time', 'date']);
100
+ }
101
+ function collectUsageEvents(entries, events) {
102
+ var e_3, _a;
103
+ var _b, _c, _d, _e;
104
+ if (!entries.length) {
105
+ return undefined;
106
+ }
107
+ var normalizedEntries = (0, ai_run_evidence_1.normalizeOpenAIUsageRowsForCosting)(entries);
108
+ try {
109
+ for (var normalizedEntries_1 = __values(normalizedEntries), normalizedEntries_1_1 = normalizedEntries_1.next(); !normalizedEntries_1_1.done; normalizedEntries_1_1 = normalizedEntries_1.next()) {
110
+ var entry = normalizedEntries_1_1.value;
111
+ pushEvent(events, {
112
+ type: 'usage',
113
+ category: cleanText(entry.category || entry.kind || entry.phase, 160) || 'usage',
114
+ message: cleanText(entry.summary || entry.description || entry.model || 'Model usage recorded.', 800),
115
+ recordedAt: eventDate(entry),
116
+ metadata: {
117
+ model: entry.model,
118
+ inputTokens: (_b = entry.inputTokens) !== null && _b !== void 0 ? _b : entry.input_tokens,
119
+ outputTokens: (_c = entry.outputTokens) !== null && _c !== void 0 ? _c : entry.output_tokens,
120
+ totalTokens: (_d = entry.totalTokens) !== null && _d !== void 0 ? _d : entry.total_tokens,
121
+ estimatedUsd: (_e = entry.estimatedUsd) !== null && _e !== void 0 ? _e : entry.cost_estimate
122
+ }
123
+ });
124
+ }
125
+ }
126
+ catch (e_3_1) { e_3 = { error: e_3_1 }; }
127
+ finally {
128
+ try {
129
+ if (normalizedEntries_1_1 && !normalizedEntries_1_1.done && (_a = normalizedEntries_1.return)) _a.call(normalizedEntries_1);
130
+ }
131
+ finally { if (e_3) throw e_3.error; }
132
+ }
133
+ return (0, ai_run_evidence_1.buildAIRunCost)(entries);
134
+ }
135
+ function collectCommitEvents(commits, events) {
136
+ var e_4, _a;
137
+ try {
138
+ for (var _b = __values(commits.slice(0, 80)), _c = _b.next(); !_c.done; _c = _b.next()) {
139
+ var commit = _c.value;
140
+ pushEvent(events, {
141
+ type: 'git_commit',
142
+ message: cleanText(commit.message || commit.subject || commit.title || commit.hash || commit.sha, 1200),
143
+ recordedAt: eventDate(commit),
144
+ metadata: {
145
+ sha: commit.sha || commit.hash,
146
+ branch: commit.branch,
147
+ author: commit.author
148
+ }
149
+ });
150
+ }
151
+ }
152
+ catch (e_4_1) { e_4 = { error: e_4_1 }; }
153
+ finally {
154
+ try {
155
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
156
+ }
157
+ finally { if (e_4) throw e_4.error; }
158
+ }
159
+ }
160
+ function statusFromBoolean(value, fallback) {
161
+ if (value === true) {
162
+ return 'pass';
163
+ }
164
+ if (value === false) {
165
+ return 'fail';
166
+ }
167
+ return fallback;
168
+ }
169
+ function collectArtifacts(evidence) {
170
+ var e_5, _a, e_6, _b;
171
+ var artifacts = [];
172
+ try {
173
+ for (var _c = __values(asArray(evidence === null || evidence === void 0 ? void 0 : evidence.artifacts)), _d = _c.next(); !_d.done; _d = _c.next()) {
174
+ var entry = _d.value;
175
+ artifacts.push({
176
+ type: entry.type || 'other',
177
+ path: cleanText(entry.path || entry.file || entry.artifactPath, 500),
178
+ url: cleanText(entry.url, 500),
179
+ title: cleanText(entry.title || entry.name, 300),
180
+ caption: cleanText(entry.caption || entry.message, 800),
181
+ metadata: entry.metadata
182
+ });
183
+ }
184
+ }
185
+ catch (e_5_1) { e_5 = { error: e_5_1 }; }
186
+ finally {
187
+ try {
188
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
189
+ }
190
+ finally { if (e_5) throw e_5.error; }
191
+ }
192
+ var screenshots = asArray((evidence === null || evidence === void 0 ? void 0 : evidence.screenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.qaScreenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserScreenshots));
193
+ try {
194
+ for (var _e = __values(screenshots.slice(0, 40)), _f = _e.next(); !_f.done; _f = _e.next()) {
195
+ var screenshot = _f.value;
196
+ if (typeof screenshot === 'string') {
197
+ artifacts.push({ type: 'screenshot', path: screenshot });
198
+ }
199
+ else {
200
+ artifacts.push({
201
+ type: 'screenshot',
202
+ path: cleanText(screenshot.path || screenshot.file, 500),
203
+ url: cleanText(screenshot.url, 500),
204
+ title: cleanText(screenshot.title || screenshot.route, 300),
205
+ caption: cleanText(screenshot.caption || screenshot.message, 800),
206
+ metadata: screenshot.metadata
207
+ });
208
+ }
209
+ }
210
+ }
211
+ catch (e_6_1) { e_6 = { error: e_6_1 }; }
212
+ finally {
213
+ try {
214
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
215
+ }
216
+ finally { if (e_6) throw e_6.error; }
217
+ }
218
+ return artifacts;
219
+ }
220
+ function collectInfraChecks(evidence) {
221
+ var e_7, _a;
222
+ var _b;
223
+ var checks = [];
224
+ try {
225
+ for (var _c = __values(asArray((evidence === null || evidence === void 0 ? void 0 : evidence.infraChecks) || (evidence === null || evidence === void 0 ? void 0 : evidence.preflightChecks))), _d = _c.next(); !_d.done; _d = _c.next()) {
226
+ var entry = _d.value;
227
+ checks.push({
228
+ name: cleanText(entry.name || entry.check || entry.key, 160) || 'infra_check',
229
+ status: entry.status || statusFromBoolean(entry.passed),
230
+ message: cleanText(entry.message || entry.error || entry.reason, 1000),
231
+ path: cleanText(entry.path, 500),
232
+ durationMs: Number((_b = entry.durationMs) !== null && _b !== void 0 ? _b : entry.duration_ms) || undefined,
233
+ metadata: entry.metadata,
234
+ checkedAt: eventDate(entry)
235
+ });
236
+ }
237
+ }
238
+ catch (e_7_1) { e_7 = { error: e_7_1 }; }
239
+ finally {
240
+ try {
241
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
242
+ }
243
+ finally { if (e_7) throw e_7.error; }
244
+ }
245
+ if (evidence === null || evidence === void 0 ? void 0 : evidence.preflightStatus) {
246
+ checks.push({
247
+ name: 'qa_preflight',
248
+ status: evidence.preflightStatus,
249
+ message: cleanText(evidence.preflightMessage || evidence.preflightReason, 1000),
250
+ path: cleanText(evidence.preflightArtifactPath || evidence.preflightPath, 500)
251
+ });
252
+ }
253
+ return checks;
254
+ }
255
+ function collectCompileResult(evidence) {
256
+ var _a, _b, _c;
257
+ var build = (evidence === null || evidence === void 0 ? void 0 : evidence.compile) || (evidence === null || evidence === void 0 ? void 0 : evidence.build) || (evidence === null || evidence === void 0 ? void 0 : evidence.buildEvidence) || (evidence === null || evidence === void 0 ? void 0 : evidence.buildResult);
258
+ if (!build && (evidence === null || evidence === void 0 ? void 0 : evidence.compileStatus) === undefined && (evidence === null || evidence === void 0 ? void 0 : evidence.buildStatus) === undefined) {
259
+ return undefined;
260
+ }
261
+ var status = (build === null || build === void 0 ? void 0 : build.status)
262
+ || (evidence === null || evidence === void 0 ? void 0 : evidence.compileStatus)
263
+ || (evidence === null || evidence === void 0 ? void 0 : evidence.buildStatus)
264
+ || statusFromBoolean((_b = (_a = build === null || build === void 0 ? void 0 : build.passed) !== null && _a !== void 0 ? _a : build === null || build === void 0 ? void 0 : build.allExpectedPassed) !== null && _b !== void 0 ? _b : evidence === null || evidence === void 0 ? void 0 : evidence.buildPassed);
265
+ return {
266
+ status: status,
267
+ command: cleanText((build === null || build === void 0 ? void 0 : build.command) || (evidence === null || evidence === void 0 ? void 0 : evidence.buildCommand), 500),
268
+ artifactPath: cleanText((build === null || build === void 0 ? void 0 : build.artifactPath) || (build === null || build === void 0 ? void 0 : build.logPath) || (evidence === null || evidence === void 0 ? void 0 : evidence.buildLogPath), 500),
269
+ message: cleanText((build === null || build === void 0 ? void 0 : build.message) || (build === null || build === void 0 ? void 0 : build.error) || (evidence === null || evidence === void 0 ? void 0 : evidence.buildMessage), 1000),
270
+ durationMs: Number((_c = build === null || build === void 0 ? void 0 : build.durationMs) !== null && _c !== void 0 ? _c : build === null || build === void 0 ? void 0 : build.duration_ms) || undefined,
271
+ staleEvidence: (build === null || build === void 0 ? void 0 : build.staleEvidence) === true || (evidence === null || evidence === void 0 ? void 0 : evidence.staleBuildEvidence) === true,
272
+ recordedAt: eventDate(build || evidence || {})
273
+ };
274
+ }
275
+ function collectRouteProbes(evidence) {
276
+ var e_8, _a, e_9, _b;
277
+ var probes = [];
278
+ try {
279
+ for (var _c = __values(asArray((evidence === null || evidence === void 0 ? void 0 : evidence.routeProbes) || (evidence === null || evidence === void 0 ? void 0 : evidence.routes) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserRoutes))), _d = _c.next(); !_d.done; _d = _c.next()) {
280
+ var entry = _d.value;
281
+ probes.push({
282
+ route: cleanText(entry.route || entry.path || entry.url, 500) || '/',
283
+ status: entry.status || statusFromBoolean(entry.passed),
284
+ screenshot: cleanText(entry.screenshot || entry.screenshotPath, 500),
285
+ caption: cleanText(entry.caption || entry.message, 1000),
286
+ shellOnly: entry.shellOnly === true,
287
+ authenticated: entry.authenticated === true,
288
+ consoleErrors: asArray(entry.consoleErrors),
289
+ networkErrors: asArray(entry.networkErrors),
290
+ message: cleanText(entry.message || entry.error, 1000),
291
+ recordedAt: eventDate(entry)
292
+ });
293
+ }
294
+ }
295
+ catch (e_8_1) { e_8 = { error: e_8_1 }; }
296
+ finally {
297
+ try {
298
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
299
+ }
300
+ finally { if (e_8) throw e_8.error; }
301
+ }
302
+ var screenshots = asArray((evidence === null || evidence === void 0 ? void 0 : evidence.screenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.qaScreenshots) || (evidence === null || evidence === void 0 ? void 0 : evidence.browserScreenshots));
303
+ try {
304
+ for (var _e = __values(screenshots.slice(0, 20)), _f = _e.next(); !_f.done; _f = _e.next()) {
305
+ var screenshot = _f.value;
306
+ if (typeof screenshot === 'string') {
307
+ probes.push({ route: '/', status: 'pass', screenshot: screenshot, caption: 'Historical browser screenshot recorded.' });
308
+ }
309
+ else if (screenshot.route || screenshot.path || screenshot.url || screenshot.screenshot || screenshot.file) {
310
+ probes.push({
311
+ route: cleanText(screenshot.route || screenshot.url || screenshot.path, 500) || '/',
312
+ status: screenshot.status || statusFromBoolean(screenshot.passed, 'pass'),
313
+ screenshot: cleanText(screenshot.screenshot || screenshot.screenshotPath || screenshot.file || screenshot.path, 500),
314
+ caption: cleanText(screenshot.caption || screenshot.message, 1000),
315
+ shellOnly: screenshot.shellOnly === true,
316
+ authenticated: screenshot.authenticated === true,
317
+ recordedAt: eventDate(screenshot)
318
+ });
319
+ }
320
+ }
321
+ }
322
+ catch (e_9_1) { e_9 = { error: e_9_1 }; }
323
+ finally {
324
+ try {
325
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
326
+ }
327
+ finally { if (e_9) throw e_9.error; }
328
+ }
329
+ return probes;
330
+ }
331
+ function collectBusinessAssertions(evidence) {
332
+ var e_10, _a;
333
+ var assertions = [];
334
+ try {
335
+ for (var _b = __values(asArray((evidence === null || evidence === void 0 ? void 0 : evidence.businessAssertions) || (evidence === null || evidence === void 0 ? void 0 : evidence.workflowAssertions) || (evidence === null || evidence === void 0 ? void 0 : evidence.assertions))), _c = _b.next(); !_c.done; _c = _b.next()) {
336
+ var entry = _c.value;
337
+ assertions.push({
338
+ assertion: cleanText(entry.assertion || entry.name || entry.workflow || entry.expected, 1000) || 'business assertion',
339
+ status: entry.status || statusFromBoolean(entry.passed),
340
+ workflow: cleanText(entry.workflow, 400),
341
+ route: cleanText(entry.route, 500),
342
+ action: cleanText(entry.action, 500),
343
+ expected: cleanText(entry.expected, 1000),
344
+ observed: cleanText(entry.observed || entry.actual, 1000),
345
+ dataProof: cleanText(entry.dataProof || entry.proof, 1400),
346
+ artifactPaths: asArray(entry.artifactPaths || entry.artifacts),
347
+ message: cleanText(entry.message || entry.error || entry.reason, 1000),
348
+ recordedAt: eventDate(entry),
349
+ metadata: entry.metadata
350
+ });
351
+ }
352
+ }
353
+ catch (e_10_1) { e_10 = { error: e_10_1 }; }
354
+ finally {
355
+ try {
356
+ if (_c && !_c.done && (_a = _b.return)) _a.call(_b);
357
+ }
358
+ finally { if (e_10) throw e_10.error; }
359
+ }
360
+ if (!assertions.length && (evidence === null || evidence === void 0 ? void 0 : evidence.qaRunOutcome) === 'business_assertion_passed') {
361
+ assertions.push({
362
+ assertion: cleanText(evidence.aiRunOutcomeReason || evidence.reason || 'Historical business assertion passed.', 1000),
363
+ status: 'pass',
364
+ dataProof: cleanText(evidence.aiRunOutcomeReason || evidence.reason, 1400)
365
+ });
366
+ }
367
+ if (!assertions.length && (evidence === null || evidence === void 0 ? void 0 : evidence.qaRunOutcome) === 'business_assertion_failed') {
368
+ assertions.push({
369
+ assertion: cleanText(evidence.aiRunOutcomeReason || evidence.reason || 'Historical business assertion failed.', 1000),
370
+ status: 'fail',
371
+ observed: cleanText(evidence.aiRunOutcomeReason || evidence.reason, 1000)
372
+ });
373
+ }
374
+ return assertions;
375
+ }
376
+ function buildQaFromEvidence(evidence, now) {
377
+ return (0, ai_run_evidence_1.buildAIQaRun)({
378
+ infraChecks: collectInfraChecks(evidence),
379
+ compile: collectCompileResult(evidence),
380
+ routeProbes: collectRouteProbes(evidence),
381
+ businessAssertions: collectBusinessAssertions(evidence),
382
+ artifacts: collectArtifacts(evidence),
383
+ now: now
384
+ });
385
+ }
386
+ function evidenceObject() {
387
+ var e_11, _a;
388
+ var values = [];
389
+ for (var _i = 0; _i < arguments.length; _i++) {
390
+ values[_i] = arguments[_i];
391
+ }
392
+ try {
393
+ for (var values_1 = __values(values), values_1_1 = values_1.next(); !values_1_1.done; values_1_1 = values_1.next()) {
394
+ var value = values_1_1.value;
395
+ if (value && typeof value === 'object' && !Array.isArray(value)) {
396
+ return value;
397
+ }
398
+ }
399
+ }
400
+ catch (e_11_1) { e_11 = { error: e_11_1 }; }
401
+ finally {
402
+ try {
403
+ if (values_1_1 && !values_1_1.done && (_a = values_1.return)) _a.call(values_1);
404
+ }
405
+ finally { if (e_11) throw e_11.error; }
406
+ }
407
+ return {};
408
+ }
409
+ function buildSupportAIRunFromEvidence(input) {
410
+ var e_12, _a, e_13, _b;
411
+ var ticket = input.ticket || {};
412
+ var job = input.job || {};
413
+ var evidence = evidenceObject(input.qaEvidence, job.qaEvidence, job.qa_evidence, ticket.qaEvidence, ticket.qa_evidence);
414
+ var events = [];
415
+ var sourceIds = {};
416
+ addSourceId(sourceIds, 'ticketId', ticket._id || ticket.id || job.ticketId || job.supportTicketId);
417
+ addSourceId(sourceIds, 'ticketNumber', ticket.ticketNumber || ticket.number || job.ticketNumber);
418
+ addSourceId(sourceIds, 'jobId', job._id || job.id || job.jobId);
419
+ addSourceId(sourceIds, 'buildPlanId', job.buildPlanId || ticket.buildPlanId);
420
+ try {
421
+ for (var _c = __values(asArray(input.taskEvents)), _d = _c.next(); !_d.done; _d = _c.next()) {
422
+ var event_1 = _d.value;
423
+ pushEvent(events, {
424
+ type: event_1.type === 'human_intervention' ? 'human_intervention' : 'log',
425
+ category: cleanText(event_1.category || event_1.phase || event_1.type, 160),
426
+ message: cleanText(event_1.message || event_1.summary || event_1.text || event_1.title, 1800),
427
+ recordedAt: eventDate(event_1),
428
+ metadata: {
429
+ status: event_1.status,
430
+ phase: event_1.phase,
431
+ taskId: event_1.taskId || event_1._id || event_1.id
432
+ }
433
+ });
434
+ }
435
+ }
436
+ catch (e_12_1) { e_12 = { error: e_12_1 }; }
437
+ finally {
438
+ try {
439
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
440
+ }
441
+ finally { if (e_12) throw e_12.error; }
442
+ }
443
+ try {
444
+ for (var _e = __values(asArray(input.aiJobs)), _f = _e.next(); !_f.done; _f = _e.next()) {
445
+ var aiJob = _f.value;
446
+ pushEvent(events, {
447
+ type: aiJob.model ? 'model_call' : 'log',
448
+ category: cleanText(aiJob.category || aiJob.phase || aiJob.runner, 160),
449
+ message: cleanText(aiJob.summary || aiJob.promptSummary || aiJob.status || aiJob.phase, 1800),
450
+ recordedAt: eventDate(aiJob),
451
+ metadata: {
452
+ id: aiJob._id || aiJob.id,
453
+ model: aiJob.model,
454
+ status: aiJob.status,
455
+ phase: aiJob.phase
456
+ }
457
+ });
458
+ }
459
+ }
460
+ catch (e_13_1) { e_13 = { error: e_13_1 }; }
461
+ finally {
462
+ try {
463
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
464
+ }
465
+ finally { if (e_13) throw e_13.error; }
466
+ }
467
+ collectCommitEvents(asArray(input.commits), events);
468
+ var cost = collectUsageEvents(asArray(input.usageLedger), events);
469
+ var qa = buildQaFromEvidence(evidence, input.now);
470
+ return (0, ai_run_evidence_1.buildAIRun)({
471
+ source: 'support_ticket',
472
+ sourceIds: sourceIds,
473
+ title: firstText(ticket, ['title', 'subject', 'summary']) || firstText(job, ['title', 'summary']),
474
+ status: firstText(job, ['status', 'state']) || firstText(ticket, ['status', 'state']),
475
+ phase: firstText(job, ['phase', 'runnerPhase']),
476
+ startedAt: dateValue(job, ['startedAt', 'createdAt']) || dateValue(ticket, ['createdAt']),
477
+ completedAt: dateValue(job, ['completedAt', 'finishedAt', 'updatedAt']),
478
+ events: events,
479
+ qa: qa,
480
+ cost: cost,
481
+ scorecardPassed: evidence.scorecardPassed === true || evidence.scorecardStatus === 'pass',
482
+ deployStatus: evidence.deployStatus,
483
+ publishStatus: evidence.publishStatus,
484
+ sampleDataStatus: evidence.sampleDataStatus,
485
+ manualHandoff: evidence.manualHandoff === true || job.manualHandoff === true,
486
+ stopped: evidence.stopped === true || job.stopped === true,
487
+ rejected: evidence.rejected === true,
488
+ explicitAccepted: evidence.explicitAccepted === true || ticket.accepted === true,
489
+ terminal: /complete|closed|resolved|done|failed|rejected/i.test("".concat(job.status || '', " ").concat(ticket.status || '')),
490
+ now: input.now,
491
+ metadata: {
492
+ versionCount: asArray(input.versions).length,
493
+ buildPlanCount: asArray(input.buildPlans).length
494
+ }
495
+ });
496
+ }
497
+ function buildAICoderAIRunFromEvidence(input) {
498
+ var e_14, _a;
499
+ var _b, _c, _d, _e, _f, _g, _h;
500
+ var app = input.app || {};
501
+ var job = input.job || {};
502
+ var evidence = evidenceObject(input.qaEvidence, job.qaEvidence, job.qa_evidence, app.qaEvidence, app.qa_evidence);
503
+ var events = [];
504
+ var sourceIds = {};
505
+ addSourceId(sourceIds, 'appId', app._id || app.id || job.appId || job.aicoderAppId);
506
+ addSourceId(sourceIds, 'jobId', job._id || job.id || job.jobId);
507
+ addSourceId(sourceIds, 'domain', app.domain || app.customDomain || job.domain);
508
+ try {
509
+ for (var _j = __values(asArray(input.logs)), _k = _j.next(); !_k.done; _k = _j.next()) {
510
+ var log = _k.value;
511
+ pushEvent(events, {
512
+ type: log.type === 'scorecard' ? 'scorecard' : 'log',
513
+ category: cleanText(log.category || log.phase || log.level, 160),
514
+ message: cleanText(log.message || log.summary || log.text || log.title, 1800),
515
+ artifactPaths: asArray(log.artifactPaths || log.artifacts),
516
+ recordedAt: eventDate(log),
517
+ metadata: {
518
+ status: log.status,
519
+ level: log.level,
520
+ score: log.score
521
+ }
522
+ });
523
+ }
524
+ }
525
+ catch (e_14_1) { e_14 = { error: e_14_1 }; }
526
+ finally {
527
+ try {
528
+ if (_k && !_k.done && (_a = _j.return)) _a.call(_j);
529
+ }
530
+ finally { if (e_14) throw e_14.error; }
531
+ }
532
+ collectCommitEvents(asArray(input.commits), events);
533
+ var cost = collectUsageEvents(asArray(input.usageLedger), events);
534
+ var qa = buildQaFromEvidence(evidence, input.now);
535
+ var scorecardPassed = evidence.scorecardPassed === true
536
+ || evidence.scorecardStatus === 'pass'
537
+ || (Number((_c = (_b = evidence.score) !== null && _b !== void 0 ? _b : evidence.qualityScore) !== null && _c !== void 0 ? _c : 0) >= Number((_e = (_d = evidence.scoreThreshold) !== null && _d !== void 0 ? _d : job.scoreThreshold) !== null && _e !== void 0 ? _e : 90));
538
+ return (0, ai_run_evidence_1.buildAIRun)({
539
+ source: 'aicoder_app',
540
+ sourceIds: sourceIds,
541
+ title: firstText(app, ['name', 'title']) || firstText(job, ['appName', 'title', 'goal']),
542
+ status: firstText(job, ['status', 'state']) || firstText(app, ['status', 'state']),
543
+ phase: firstText(job, ['phase', 'runnerPhase']),
544
+ startedAt: dateValue(job, ['startedAt', 'createdAt']) || dateValue(app, ['createdAt']),
545
+ completedAt: dateValue(job, ['completedAt', 'finishedAt', 'updatedAt']),
546
+ events: events,
547
+ qa: qa,
548
+ cost: cost,
549
+ scorecardPassed: scorecardPassed,
550
+ deployStatus: evidence.deployStatus || statusFromBoolean(evidence.deployReady),
551
+ publishStatus: evidence.publishStatus || evidence.pullRequestStatus,
552
+ sampleDataStatus: evidence.sampleDataStatus || statusFromBoolean((_f = evidence.sampleDataReady) !== null && _f !== void 0 ? _f : evidence.hasSampleData),
553
+ manualHandoff: evidence.manualHandoff === true || job.manualHandoff === true,
554
+ stopped: evidence.stopped === true || job.stopped === true,
555
+ rejected: evidence.rejected === true,
556
+ explicitAccepted: evidence.explicitAccepted === true || app.accepted === true,
557
+ terminal: /complete|closed|resolved|done|failed|rejected/i.test("".concat(job.status || '', " ").concat(job.phase || '')),
558
+ now: input.now,
559
+ metadata: {
560
+ domain: app.domain || app.customDomain || job.domain,
561
+ wowScore: (_g = evidence.wowScore) !== null && _g !== void 0 ? _g : job.wowScore,
562
+ score: (_h = evidence.score) !== null && _h !== void 0 ? _h : evidence.qualityScore
563
+ }
564
+ });
565
+ }
566
+ function buildAssistantAIRunFromEvidence(input) {
567
+ var e_15, _a, e_16, _b;
568
+ var conversation = input.conversation || {};
569
+ var events = [];
570
+ var sourceIds = {};
571
+ addSourceId(sourceIds, 'conversationId', conversation._id || conversation.id || conversation.conversationId);
572
+ addSourceId(sourceIds, 'userId', conversation.userId || conversation.ownerId);
573
+ try {
574
+ for (var _c = __values(asArray(input.messages)), _d = _c.next(); !_d.done; _d = _c.next()) {
575
+ var message = _d.value;
576
+ pushEvent(events, {
577
+ type: 'assistant_message',
578
+ category: cleanText(message.role || message.authorRole || message.type, 160),
579
+ message: cleanText(message.content || message.text || message.message || message.summary, 1800),
580
+ recordedAt: eventDate(message),
581
+ metadata: {
582
+ id: message._id || message.id,
583
+ role: message.role,
584
+ model: message.model
585
+ }
586
+ });
587
+ }
588
+ }
589
+ catch (e_15_1) { e_15 = { error: e_15_1 }; }
590
+ finally {
591
+ try {
592
+ if (_d && !_d.done && (_a = _c.return)) _a.call(_c);
593
+ }
594
+ finally { if (e_15) throw e_15.error; }
595
+ }
596
+ try {
597
+ for (var _e = __values(asArray(input.issueReports)), _f = _e.next(); !_f.done; _f = _e.next()) {
598
+ var report = _f.value;
599
+ pushEvent(events, {
600
+ type: 'human_intervention',
601
+ category: cleanText(report.category || report.status || 'issue_report', 160),
602
+ message: cleanText(report.summary || report.description || report.message || report.title, 1800),
603
+ recordedAt: eventDate(report),
604
+ metadata: {
605
+ id: report._id || report.id,
606
+ status: report.status,
607
+ severity: report.severity
608
+ }
609
+ });
610
+ }
611
+ }
612
+ catch (e_16_1) { e_16 = { error: e_16_1 }; }
613
+ finally {
614
+ try {
615
+ if (_f && !_f.done && (_b = _e.return)) _b.call(_e);
616
+ }
617
+ finally { if (e_16) throw e_16.error; }
618
+ }
619
+ var cost = collectUsageEvents(asArray(input.usageLedger), events);
620
+ var correctnessEvidence = {
621
+ businessAssertions: asArray(input.correctnessChecks).map(function (check) { return ({
622
+ assertion: check.assertion || check.name || check.case || 'assistant correctness check',
623
+ status: check.status || statusFromBoolean(check.passed),
624
+ expected: check.expected,
625
+ observed: check.observed || check.actual,
626
+ dataProof: check.dataProof || check.proof,
627
+ artifactPaths: check.artifactPaths,
628
+ message: check.message || check.reason,
629
+ metadata: check.metadata,
630
+ recordedAt: eventDate(check)
631
+ }); })
632
+ };
633
+ var qa = buildQaFromEvidence(correctnessEvidence, input.now);
634
+ var failedReports = asArray(input.issueReports)
635
+ .filter(function (report) { return /open|new|fail|bug|wrong|incorrect|error/i.test("".concat(report.status || '', " ").concat(report.category || '', " ").concat(report.summary || '')); });
636
+ return (0, ai_run_evidence_1.buildAIRun)({
637
+ source: 'ai_assistant',
638
+ sourceIds: sourceIds,
639
+ title: firstText(conversation, ['title', 'subject', 'summary']) || 'AI assistant conversation',
640
+ status: firstText(conversation, ['status', 'state']),
641
+ phase: firstText(conversation, ['phase']),
642
+ startedAt: dateValue(conversation, ['startedAt', 'createdAt']),
643
+ completedAt: dateValue(conversation, ['completedAt', 'closedAt', 'updatedAt']),
644
+ events: events,
645
+ qa: qa,
646
+ cost: cost,
647
+ rejected: failedReports.length > 0 || conversation.rejected === true,
648
+ manualHandoff: conversation.manualHandoff === true,
649
+ stopped: conversation.stopped === true,
650
+ explicitAccepted: conversation.accepted === true,
651
+ terminal: /complete|closed|resolved|done|failed|rejected/i.test("".concat(conversation.status || '')),
652
+ now: input.now,
653
+ metadata: {
654
+ issueReportCount: asArray(input.issueReports).length,
655
+ correctnessCheckCount: asArray(input.correctnessChecks).length
656
+ }
657
+ });
658
+ }
659
+
660
+ //# sourceMappingURL=ai-run-evidence-adapters.js.map