@cydm/happy-elves 0.1.0-beta.298 → 0.1.0-beta.299
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.
|
@@ -315,7 +315,8 @@ function foldAliasedRuntimeEvents(events) {
|
|
|
315
315
|
const profiles = new Map();
|
|
316
316
|
const runtimeToRequest = new Map();
|
|
317
317
|
for (const event of events) {
|
|
318
|
-
const profile = profiles.get(event.turnId) ?? { output: "" };
|
|
318
|
+
const profile = profiles.get(event.turnId) ?? { output: "", hasDone: false, firstEventId: event.id };
|
|
319
|
+
profile.firstEventId = Math.min(profile.firstEventId, event.id);
|
|
319
320
|
const payload = event.decoded;
|
|
320
321
|
if (payload?.type === "user_prompt" && profile.prompt === undefined) {
|
|
321
322
|
profile.prompt = payload.text.trim();
|
|
@@ -324,6 +325,7 @@ function foldAliasedRuntimeEvents(events) {
|
|
|
324
325
|
profile.output += payload.text;
|
|
325
326
|
}
|
|
326
327
|
else if (payload?.type === "done") {
|
|
328
|
+
profile.hasDone = true;
|
|
327
329
|
const runtimeTurnId = payload.lastTurnId ?? payload.currentHead;
|
|
328
330
|
if (runtimeTurnId && runtimeTurnId !== event.turnId && !runtimeTurnId.startsWith("event:")) {
|
|
329
331
|
runtimeToRequest.set(runtimeTurnId, event.turnId);
|
|
@@ -345,11 +347,83 @@ function foldAliasedRuntimeEvents(events) {
|
|
|
345
347
|
duplicateRuntimeToRequest.set(runtimeTurnId, requestTurnId);
|
|
346
348
|
}
|
|
347
349
|
}
|
|
350
|
+
const inferredRuntimeToRequest = inferRuntimeToRequestAliases(profiles, runtimeToRequest);
|
|
351
|
+
const inferredRequestToRuntime = new Map();
|
|
352
|
+
for (const [runtimeTurnId, requestTurnId] of inferredRuntimeToRequest) {
|
|
353
|
+
runtimeToRequest.set(runtimeTurnId, requestTurnId);
|
|
354
|
+
inferredRequestToRuntime.set(requestTurnId, runtimeTurnId);
|
|
355
|
+
}
|
|
356
|
+
const foldedEvents = [];
|
|
357
|
+
for (const event of events) {
|
|
358
|
+
if (duplicateRuntimeTurns.has(event.turnId))
|
|
359
|
+
continue;
|
|
360
|
+
const inferredRequestTurnId = inferredRuntimeToRequest.get(event.turnId);
|
|
361
|
+
if (inferredRequestTurnId) {
|
|
362
|
+
foldedEvents.push({ ...event, turnId: inferredRequestTurnId });
|
|
363
|
+
continue;
|
|
364
|
+
}
|
|
365
|
+
const inferredRuntimeTurnId = inferredRequestToRuntime.get(event.turnId);
|
|
366
|
+
if (inferredRuntimeTurnId && shouldDropRequestSideDuplicateEvent(event, profiles.get(inferredRuntimeTurnId))) {
|
|
367
|
+
continue;
|
|
368
|
+
}
|
|
369
|
+
foldedEvents.push(event);
|
|
370
|
+
}
|
|
348
371
|
return {
|
|
349
|
-
events:
|
|
350
|
-
runtimeToRequest: duplicateRuntimeToRequest,
|
|
372
|
+
events: foldedEvents,
|
|
373
|
+
runtimeToRequest: new Map([...duplicateRuntimeToRequest, ...inferredRuntimeToRequest]),
|
|
351
374
|
};
|
|
352
375
|
}
|
|
376
|
+
function inferRuntimeToRequestAliases(profiles, explicitRuntimeToRequest) {
|
|
377
|
+
const requestsByPrompt = new Map();
|
|
378
|
+
const runtimesByPrompt = new Map();
|
|
379
|
+
for (const [turnId, profile] of profiles) {
|
|
380
|
+
const promptKey = comparableOutput(profile.prompt ?? "");
|
|
381
|
+
if (!promptKey)
|
|
382
|
+
continue;
|
|
383
|
+
if (turnId.startsWith("turn_") && !profile.hasDone) {
|
|
384
|
+
requestsByPrompt.set(promptKey, [...(requestsByPrompt.get(promptKey) ?? []), { turnId, output: profile.output, firstEventId: profile.firstEventId }]);
|
|
385
|
+
}
|
|
386
|
+
else if (!turnId.startsWith("turn_") && profile.hasDone && !explicitRuntimeToRequest.has(turnId)) {
|
|
387
|
+
runtimesByPrompt.set(promptKey, [...(runtimesByPrompt.get(promptKey) ?? []), { turnId, output: profile.output, firstEventId: profile.firstEventId }]);
|
|
388
|
+
}
|
|
389
|
+
}
|
|
390
|
+
const inferred = new Map();
|
|
391
|
+
for (const [promptKey, requests] of requestsByPrompt) {
|
|
392
|
+
const runtimes = runtimesByPrompt.get(promptKey) ?? [];
|
|
393
|
+
if (requests.length !== 1 || runtimes.length !== 1)
|
|
394
|
+
continue;
|
|
395
|
+
const request = requests[0];
|
|
396
|
+
const runtime = runtimes[0];
|
|
397
|
+
if (!request || !runtime)
|
|
398
|
+
continue;
|
|
399
|
+
if (runtime.firstEventId <= request.firstEventId)
|
|
400
|
+
continue;
|
|
401
|
+
if (!outputsAreCompatible(request.output, runtime.output))
|
|
402
|
+
continue;
|
|
403
|
+
inferred.set(runtime.turnId, request.turnId);
|
|
404
|
+
}
|
|
405
|
+
return inferred;
|
|
406
|
+
}
|
|
407
|
+
function outputsAreCompatible(requestOutput, runtimeOutput) {
|
|
408
|
+
const request = comparableOutput(requestOutput);
|
|
409
|
+
const runtime = comparableOutput(runtimeOutput);
|
|
410
|
+
if (!request || !runtime)
|
|
411
|
+
return true;
|
|
412
|
+
return request.includes(runtime) || runtime.includes(request);
|
|
413
|
+
}
|
|
414
|
+
function shouldDropRequestSideDuplicateEvent(event, runtimeProfile) {
|
|
415
|
+
if (!runtimeProfile)
|
|
416
|
+
return false;
|
|
417
|
+
const payload = event.decoded;
|
|
418
|
+
if (payload?.type === "user_prompt") {
|
|
419
|
+
return comparableOutput(payload.text) === comparableOutput(runtimeProfile.prompt ?? "");
|
|
420
|
+
}
|
|
421
|
+
if (payload?.type === "text_delta" && payload.stream !== "thought" && payload.text) {
|
|
422
|
+
const chunk = comparableOutput(payload.text);
|
|
423
|
+
return Boolean(chunk) && comparableOutput(runtimeProfile.output).includes(chunk);
|
|
424
|
+
}
|
|
425
|
+
return false;
|
|
426
|
+
}
|
|
353
427
|
function dedupeSyntheticHistoricalEvents(events) {
|
|
354
428
|
const groups = new Map();
|
|
355
429
|
for (const event of events) {
|
package/apps/cli/package.json
CHANGED
package/apps/daemon/package.json
CHANGED