@cesarus777/opencode-cgp 0.4.1 → 0.4.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/core.js CHANGED
@@ -173,6 +173,11 @@ function reconcile(a, b) {
173
173
  };
174
174
  }
175
175
  function analyzeModelRequests(records, options) {
176
+ const accumulator = createModelAccumulator(options);
177
+ accumulator.append(records);
178
+ return accumulator.result();
179
+ }
180
+ function createModelAccumulator(options) {
176
181
  const maxRequests = options.maxRequests ?? 1e5;
177
182
  if (!Number.isSafeInteger(maxRequests) || maxRequests < 1 || maxRequests > 1e6)
178
183
  throw new Error("maxRequests must be an integer from 1 to 1000000");
@@ -183,7 +188,7 @@ function analyzeModelRequests(records, options) {
183
188
  throw new Error("Invalid analytics timestamp");
184
189
  if (options.from !== undefined && options.to !== undefined && options.from > options.to)
185
190
  throw new Error("--from must precede --to");
186
- const coverage = {
191
+ const counters = {
187
192
  source: "retained-request-records",
188
193
  scannedRecords: 0,
189
194
  unscannedRecords: 0,
@@ -197,74 +202,98 @@ function analyzeModelRequests(records, options) {
197
202
  partial: false
198
203
  };
199
204
  const requests = new Map;
200
- for (const record of records) {
201
- coverage.scannedRecords++;
202
- if (record.type === "gap") {
203
- coverage.gapRecords++;
204
- continue;
205
- }
206
- if (record.type !== "request")
207
- continue;
208
- const fact = requestFact(record);
209
- if (!fact) {
210
- coverage.missingIdentityRecords++;
211
- continue;
212
- }
213
- fact.location = options.location;
214
- const key = JSON.stringify([
215
- fact.sessionId,
216
- fact.requestId,
217
- fact.startedAt
218
- ]);
219
- const previous = requests.get(key);
220
- if (previous) {
221
- coverage.duplicateRecords++;
222
- const merged = reconcile(previous.fact, fact);
223
- if (!merged)
224
- previous.conflict = true;
225
- else
226
- previous.fact = merged;
227
- } else {
228
- if (requests.size === maxRequests) {
229
- coverage.scannedRecords--;
230
- coverage.unscannedRecords = records.length - coverage.scannedRecords;
205
+ let totalRecords = 0, stopped = false;
206
+ function append(records) {
207
+ totalRecords += records.length;
208
+ for (const record of records) {
209
+ if (stopped)
231
210
  break;
211
+ counters.scannedRecords++;
212
+ if (record.type === "gap") {
213
+ counters.gapRecords++;
214
+ continue;
215
+ }
216
+ if (record.type !== "request")
217
+ continue;
218
+ const fact = requestFact(record);
219
+ if (!fact) {
220
+ counters.missingIdentityRecords++;
221
+ continue;
222
+ }
223
+ fact.location = options.location;
224
+ const key = JSON.stringify([
225
+ fact.sessionId,
226
+ fact.requestId,
227
+ fact.startedAt
228
+ ]);
229
+ const previous = requests.get(key);
230
+ if (previous) {
231
+ counters.duplicateRecords++;
232
+ const merged = reconcile(previous.fact, fact);
233
+ if (!merged)
234
+ previous.conflict = true;
235
+ else
236
+ previous.fact = merged;
237
+ } else {
238
+ if (requests.size === maxRequests) {
239
+ counters.scannedRecords--;
240
+ stopped = true;
241
+ break;
242
+ }
243
+ requests.set(key, { fact, conflict: false });
232
244
  }
233
- requests.set(key, { fact, conflict: false });
234
245
  }
235
246
  }
236
- const accepted = [];
237
- for (const { fact: r, conflict } of requests.values()) {
238
- if (conflict) {
239
- coverage.conflictingRequests++;
240
- continue;
241
- }
242
- if (r.completedAt === undefined) {
243
- coverage.incompleteRequests++;
244
- continue;
245
- }
246
- const duration = r.completedAt - r.startedAt;
247
- if (!nonnegative(r.completedAt) || !Number.isFinite(duration) || duration <= 0) {
248
- coverage.invalidTimingRequests++;
249
- continue;
247
+ function* steps() {
248
+ const coverage = {
249
+ ...counters,
250
+ unscannedRecords: totalRecords - counters.scannedRecords
251
+ };
252
+ const accepted = [];
253
+ for (const { fact: r, conflict } of requests.values()) {
254
+ yield;
255
+ if (conflict) {
256
+ coverage.conflictingRequests++;
257
+ continue;
258
+ }
259
+ if (r.completedAt === undefined) {
260
+ coverage.incompleteRequests++;
261
+ continue;
262
+ }
263
+ const duration = r.completedAt - r.startedAt;
264
+ if (!nonnegative(r.completedAt) || !Number.isFinite(duration) || duration <= 0) {
265
+ coverage.invalidTimingRequests++;
266
+ continue;
267
+ }
268
+ if (options.from !== undefined && r.completedAt < options.from || options.to !== undefined && r.completedAt >= options.to || options.model !== undefined && r.model !== options.model || options.agent !== undefined && r.agent !== options.agent)
269
+ continue;
270
+ accepted.push({ ...r, completedAt: r.completedAt });
250
271
  }
251
- if (options.from !== undefined && r.completedAt < options.from || options.to !== undefined && r.completedAt >= options.to || options.model !== undefined && r.model !== options.model || options.agent !== undefined && r.agent !== options.agent)
252
- continue;
253
- accepted.push({ ...r, completedAt: r.completedAt });
272
+ const models = yield* summarizeModelRequestSteps(accepted);
273
+ coverage.partial = coverage.unscannedRecords > 0 || coverage.conflictingRequests > 0 || coverage.missingIdentityRecords > 0 || coverage.incompleteRequests > 0 || coverage.invalidTimingRequests > 0 || coverage.gapRecords > 0 || models.some((s) => s.partialRequests > 0 || s.legacyAttributionRequests > 0 || s.excludedTpsRequests > 0 || s.excludedTtftRequests > 0 || !s.model);
274
+ return {
275
+ schemaVersion: 1,
276
+ location: options.location,
277
+ models,
278
+ coverage,
279
+ requests: accepted
280
+ };
254
281
  }
255
- const models = summarizeModelRequests(accepted);
256
- coverage.partial = coverage.unscannedRecords > 0 || coverage.conflictingRequests > 0 || coverage.missingIdentityRecords > 0 || coverage.incompleteRequests > 0 || coverage.invalidTimingRequests > 0 || coverage.gapRecords > 0 || models.some((s) => s.partialRequests > 0 || s.legacyAttributionRequests > 0 || s.excludedTpsRequests > 0 || s.excludedTtftRequests > 0 || !s.model);
257
- return {
258
- schemaVersion: 1,
259
- location: options.location,
260
- models,
261
- coverage,
262
- requests: accepted
263
- };
282
+ return { append, steps, result: () => finishSteps(steps()) };
283
+ }
284
+ function finishSteps(steps) {
285
+ let step = steps.next();
286
+ while (!step.done)
287
+ step = steps.next();
288
+ return step.value;
264
289
  }
265
290
  function summarizeModelRequests(requests) {
291
+ return finishSteps(summarizeModelRequestSteps(requests));
292
+ }
293
+ function* summarizeModelRequestSteps(requests) {
266
294
  const groups = new Map;
267
295
  for (const r of requests) {
296
+ yield;
268
297
  const duration = r.completedAt - r.startedAt;
269
298
  let group = groups.get(r.model);
270
299
  if (!group) {