@leitwerk-dev/protocol 0.3.1 → 0.4.1

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 (52) hide show
  1. package/dist/{chunk-C7IMTOT4.js → chunk-6VYYJ4GN.js} +19 -2
  2. package/dist/chunk-6VYYJ4GN.js.map +1 -0
  3. package/dist/{chunk-LTIDCJFK.js → chunk-GOYGXRLL.js} +5 -1
  4. package/dist/chunk-GOYGXRLL.js.map +1 -0
  5. package/dist/config-snapshot.d.ts +4 -0
  6. package/dist/config-snapshot.d.ts.map +1 -1
  7. package/dist/dashboard-contracts.d.ts +111 -0
  8. package/dist/dashboard-contracts.d.ts.map +1 -0
  9. package/dist/execution-inspection.d.ts +178 -0
  10. package/dist/execution-inspection.d.ts.map +1 -0
  11. package/dist/form-contract.d.ts +10 -0
  12. package/dist/form-contract.d.ts.map +1 -1
  13. package/dist/{http-contracts-public-BiX2Og68.d.ts → http-contracts-DrTIVrNI.d.ts} +434 -7
  14. package/dist/http-contracts-public.d.ts +4 -1
  15. package/dist/http-contracts-public.d.ts.map +1 -1
  16. package/dist/http-contracts-public.js +6 -4
  17. package/dist/http-contracts.d.ts +82 -5
  18. package/dist/http-contracts.d.ts.map +1 -1
  19. package/dist/http-contracts.js +4 -2
  20. package/dist/index.d.ts +5 -3
  21. package/dist/index.d.ts.map +1 -1
  22. package/dist/index.js +267 -249
  23. package/dist/index.js.map +1 -1
  24. package/dist/live-turn-projection.d.ts +30 -37
  25. package/dist/live-turn-projection.d.ts.map +1 -1
  26. package/dist/protocol.d.ts +13 -0
  27. package/dist/protocol.d.ts.map +1 -1
  28. package/dist/protocol.js +1 -1
  29. package/dist/scoped-settings.d.ts +116 -0
  30. package/dist/scoped-settings.d.ts.map +1 -0
  31. package/dist/skill-pack.d.ts +66 -0
  32. package/dist/skill-pack.d.ts.map +1 -0
  33. package/dist/ticket-creation-contracts.d.ts +6 -0
  34. package/dist/ticket-creation-contracts.d.ts.map +1 -1
  35. package/package.json +2 -2
  36. package/src/config-snapshot.ts +4 -0
  37. package/src/dashboard-contracts.ts +111 -0
  38. package/src/execution-inspection.ts +204 -0
  39. package/src/form-contract.ts +10 -0
  40. package/src/http-contracts-public.ts +17 -0
  41. package/src/http-contracts.ts +98 -2
  42. package/src/index.ts +10 -6
  43. package/src/live-turn-projection.ts +177 -173
  44. package/src/protocol.ts +13 -0
  45. package/src/scoped-settings.ts +148 -0
  46. package/src/skill-pack.ts +93 -0
  47. package/src/ticket-creation-contracts.ts +6 -0
  48. package/dist/chunk-C7IMTOT4.js.map +0 -1
  49. package/dist/chunk-LTIDCJFK.js.map +0 -1
  50. package/dist/turn-trace-projection.d.ts +0 -5
  51. package/dist/turn-trace-projection.d.ts.map +0 -1
  52. package/src/turn-trace-projection.ts +0 -42
package/dist/index.js CHANGED
@@ -11,7 +11,7 @@ import {
11
11
  serializeFutureActionPayload,
12
12
  serializeFutureLaunchPayload,
13
13
  validateScheduleRequestInput
14
- } from "./chunk-C7IMTOT4.js";
14
+ } from "./chunk-6VYYJ4GN.js";
15
15
  import {
16
16
  WS_PRIMARY_PATH_TYPES,
17
17
  WS_PROCESS_TYPES,
@@ -22,7 +22,7 @@ import {
22
22
  parsePrimaryPathWsFrameInput,
23
23
  parseSchema,
24
24
  parseWsFrame
25
- } from "./chunk-LTIDCJFK.js";
25
+ } from "./chunk-GOYGXRLL.js";
26
26
 
27
27
  // src/usage-snapshot.ts
28
28
  import { copiedUnknownRecordSchema as unknownRecordSchema } from "@leitwerk-dev/domain";
@@ -367,6 +367,32 @@ import {
367
367
  readNonBlankString as readString
368
368
  } from "@leitwerk-dev/domain";
369
369
 
370
+ // src/pi-session-message.ts
371
+ function isPiSessionMessageEntryType(entry) {
372
+ return entry?.type === "message";
373
+ }
374
+ function isPiSessionMessageRecord(value) {
375
+ return typeof value === "object" && value !== null && !Array.isArray(value);
376
+ }
377
+ function isPiSessionMessageEntryWithRecord(entry) {
378
+ return isPiSessionMessageEntryType(entry) && isPiSessionMessageRecord(entry.message);
379
+ }
380
+ function extractPiSessionMessageText(content) {
381
+ if (typeof content === "string") {
382
+ return content;
383
+ }
384
+ if (!Array.isArray(content)) {
385
+ return "";
386
+ }
387
+ return content.map((block) => {
388
+ if (typeof block !== "object" || block === null || Array.isArray(block)) {
389
+ return "";
390
+ }
391
+ const record = block;
392
+ return record.type === "text" && typeof record.text === "string" ? record.text : "";
393
+ }).join("");
394
+ }
395
+
370
396
  // src/timestamp-ordering.ts
371
397
  function compareTimestampStrings(left, right) {
372
398
  const leftMs = Date.parse(left);
@@ -383,6 +409,80 @@ function happenedOnOrAfterStart(entryTimestamp, startedAt) {
383
409
  return compareTimestampStrings(entryTimestamp, startedAt) >= 0;
384
410
  }
385
411
 
412
+ // src/tool-result-truncation.ts
413
+ import { asUnknownRecord as asUnknownRecord2 } from "@leitwerk-dev/domain";
414
+ var TOOL_TRUNCATION_BOOLEAN_KEYS = [
415
+ "truncated",
416
+ "isTruncated",
417
+ "wasTruncated",
418
+ "outputTruncated",
419
+ "resultTruncated"
420
+ ];
421
+ var TOOL_TRUNCATION_TEXT_MARKERS = [
422
+ "output truncated",
423
+ "result truncated",
424
+ "response was too big",
425
+ "too large to display",
426
+ "truncated after",
427
+ "truncated to last",
428
+ "truncated since"
429
+ ];
430
+ function readTruncationPayload(value) {
431
+ const record = asUnknownRecord2(value);
432
+ if (!record) {
433
+ return null;
434
+ }
435
+ if ("truncation" in record) {
436
+ return record.truncation;
437
+ }
438
+ for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
439
+ if (key in record) {
440
+ return record[key];
441
+ }
442
+ }
443
+ return null;
444
+ }
445
+ function isExplicitFalseTruncationString(value) {
446
+ const normalized = value.trim().toLowerCase();
447
+ return normalized === "" || normalized === "false" || normalized === "none" || normalized === "null" || normalized === "no" || normalized === "0";
448
+ }
449
+ function isTruthyTruncationValue(value) {
450
+ if (value === null || value === void 0) {
451
+ return false;
452
+ }
453
+ if (typeof value === "boolean") {
454
+ return value;
455
+ }
456
+ if (typeof value === "number") {
457
+ return Number.isFinite(value) && value > 0;
458
+ }
459
+ if (typeof value === "string") {
460
+ return !isExplicitFalseTruncationString(value);
461
+ }
462
+ if (Array.isArray(value)) {
463
+ return value.length > 0;
464
+ }
465
+ const record = asUnknownRecord2(value);
466
+ if (!record) {
467
+ return true;
468
+ }
469
+ for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
470
+ if (record[key] === true) {
471
+ return true;
472
+ }
473
+ }
474
+ return Object.keys(record).some((key) => {
475
+ if (TOOL_TRUNCATION_BOOLEAN_KEYS.includes(key)) {
476
+ return false;
477
+ }
478
+ return isTruthyTruncationValue(record[key]);
479
+ });
480
+ }
481
+ function isToolResultTruncated(input) {
482
+ const normalizedText = input.resultText?.toLowerCase() ?? "";
483
+ return TOOL_TRUNCATION_TEXT_MARKERS.some((marker) => normalizedText.includes(marker)) || isTruthyTruncationValue(readTruncationPayload(input.resultDetails)) || isTruthyTruncationValue(readTruncationPayload(input.resultValue));
484
+ }
485
+
386
486
  // src/live-turn-projection.ts
387
487
  var PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES = [
388
488
  "pi.error",
@@ -391,9 +491,6 @@ var PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES = [
391
491
  "pi.compaction.start",
392
492
  "pi.compaction.end"
393
493
  ];
394
- var PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET = new Set(
395
- PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPES
396
- );
397
494
  function cloneToolCall(toolCall) {
398
495
  return {
399
496
  ...toolCall,
@@ -429,120 +526,68 @@ function retryEndFallbackMessage(data) {
429
526
  return attempt !== null ? `Retry sequence ended after ${attempt} ${attempt === 1 ? "retry" : "retries"}.` : "Retry sequence ended.";
430
527
  }
431
528
  function buildPrimaryPathOperationalTraceItem(input) {
432
- if (!PRIMARY_PATH_OPERATIONAL_PI_EVENT_TYPE_SET.has(input.eventType)) {
433
- return null;
434
- }
435
- const timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
529
+ let presentation;
436
530
  if (input.eventType === "pi.error") {
437
- return {
438
- kind: "operational_event",
439
- eventType: input.eventType,
531
+ presentation = {
440
532
  severity: "error",
441
533
  title: "Pi request failed",
442
- message: firstMessage(input.data, ["message", "errorMessage", "finalError"]) ?? "Pi reported an error.",
443
- timestamp
534
+ message: firstMessage(input.data, ["message", "errorMessage", "finalError"]) ?? "Pi reported an error."
444
535
  };
445
- }
446
- if (input.eventType === "pi.retry.start") {
447
- return {
448
- kind: "operational_event",
449
- eventType: input.eventType,
536
+ } else if (input.eventType === "pi.retry.start") {
537
+ presentation = {
450
538
  severity: "warning",
451
539
  title: "Retry scheduled",
452
- message: firstMessage(input.data, ["message", "errorMessage"]) ?? retryStartFallbackMessage(input.data),
453
- timestamp
540
+ message: firstMessage(input.data, ["message", "errorMessage"]) ?? retryStartFallbackMessage(input.data)
454
541
  };
455
- }
456
- if (input.eventType === "pi.retry.end") {
542
+ } else if (input.eventType === "pi.retry.end") {
457
543
  const success = input.data.success === true;
458
- return {
459
- kind: "operational_event",
460
- eventType: input.eventType,
544
+ presentation = {
461
545
  severity: success ? "success" : "error",
462
546
  title: success ? "Retry succeeded" : "Retry exhausted",
463
- message: firstMessage(input.data, ["message", "finalError", "errorMessage"]) ?? retryEndFallbackMessage(input.data),
464
- timestamp
547
+ message: firstMessage(input.data, ["message", "finalError", "errorMessage"]) ?? retryEndFallbackMessage(input.data)
465
548
  };
466
- }
467
- if (input.eventType === "pi.compaction.start") {
549
+ } else if (input.eventType === "pi.compaction.start") {
468
550
  const reason = formatReason(input.data.reason);
469
- return {
470
- kind: "operational_event",
471
- eventType: input.eventType,
551
+ presentation = {
472
552
  severity: "info",
473
553
  title: "Context compaction started",
474
- message: reason ? `Pi started compacting context (${reason}).` : "Pi started compacting context.",
475
- timestamp
554
+ message: reason ? `Pi started compacting context (${reason}).` : "Pi started compacting context."
476
555
  };
477
- }
478
- if (input.eventType === "pi.compaction.end") {
556
+ } else if (input.eventType === "pi.compaction.end") {
479
557
  const reason = formatReason(input.data.reason);
480
558
  const errorMessage = firstMessage(input.data, ["errorMessage", "message"]);
481
559
  const aborted = input.data.aborted === true;
482
- const willRetry = input.data.willRetry === true;
483
- const statusMessage = errorMessage ? errorMessage : aborted ? "Context compaction was cancelled." : `Pi compacted context${reason ? ` (${reason})` : ""}.${willRetry ? " Pi will continue automatically." : ""}`;
484
- return {
485
- kind: "operational_event",
486
- eventType: input.eventType,
560
+ presentation = {
487
561
  severity: errorMessage ? "error" : aborted ? "warning" : "success",
488
562
  title: errorMessage ? "Context compaction failed" : aborted ? "Context compaction cancelled" : "Context compacted",
489
- message: statusMessage,
490
- timestamp
563
+ message: errorMessage ?? (aborted ? "Context compaction was cancelled." : `Pi compacted context${reason ? ` (${reason})` : ""}.${input.data.willRetry === true ? " Pi will continue automatically." : ""}`)
491
564
  };
492
- }
493
- return null;
494
- }
495
- function addOpenToolCallId(projection, toolName, toolCallId) {
496
- const ids = projection.openToolCallIdsByName.get(toolName) ?? [];
497
- ids.push(toolCallId);
498
- projection.openToolCallIdsByName.set(toolName, ids);
499
- }
500
- function consumeLatestOpenToolCallId(projection, toolName) {
501
- const ids = projection.openToolCallIdsByName.get(toolName);
502
- if (!ids || ids.length === 0) {
503
- return null;
504
- }
505
- const toolCallId = ids.pop() ?? null;
506
- if (ids.length === 0) {
507
- projection.openToolCallIdsByName.delete(toolName);
508
565
  } else {
509
- projection.openToolCallIdsByName.set(toolName, ids);
510
- }
511
- return toolCallId;
512
- }
513
- function removeOpenToolCallId(projection, toolCallId) {
514
- for (const [toolName, ids] of projection.openToolCallIdsByName.entries()) {
515
- const nextIds = ids.filter((candidate) => candidate !== toolCallId);
516
- if (nextIds.length === ids.length) {
517
- continue;
518
- }
519
- if (nextIds.length === 0) {
520
- projection.openToolCallIdsByName.delete(toolName);
521
- } else {
522
- projection.openToolCallIdsByName.set(toolName, nextIds);
523
- }
524
- return;
566
+ return null;
525
567
  }
568
+ return {
569
+ kind: "operational_event",
570
+ eventType: input.eventType,
571
+ timestamp: readWsEventTimestamp(input.data, input.fallbackTimestamp),
572
+ ...presentation
573
+ };
526
574
  }
527
575
  function canonicalizeToolCallId(input) {
528
576
  const explicitToolCallId = readWsEventToolCallId(input.data);
529
- if (explicitToolCallId) {
530
- if (input.preferOpenCall) {
531
- removeOpenToolCallId(input.projection, explicitToolCallId);
532
- }
533
- return explicitToolCallId;
534
- }
577
+ if (explicitToolCallId) return explicitToolCallId;
535
578
  if (input.preferOpenCall) {
536
- const openToolCallId = consumeLatestOpenToolCallId(input.projection, input.toolName);
537
- if (openToolCallId) {
538
- return openToolCallId;
539
- }
579
+ const openToolCall = [...input.projection.toolCallsById.values()].reverse().find((tool) => tool.status === "running" && tool.toolName === input.toolName);
580
+ if (openToolCall) return openToolCall.toolCallId;
540
581
  }
541
- return resolveWsEventToolCallId(input.data, {
542
- timestamp: input.timestamp,
543
- toolName: input.toolName,
544
- ordinal: input.projection.nextFallbackOrdinal++
545
- });
582
+ let toolCallId;
583
+ do {
584
+ toolCallId = resolveWsEventToolCallId(input.data, {
585
+ timestamp: input.timestamp,
586
+ toolName: input.toolName,
587
+ ordinal: input.projection.nextFallbackOrdinal++
588
+ });
589
+ } while (input.projection.toolCallsById.has(toolCallId));
590
+ return toolCallId;
546
591
  }
547
592
  function appendThinkingTraceText(projection, text) {
548
593
  if (text.length === 0) {
@@ -569,31 +614,35 @@ function ensureToolTraceItem(projection, toolCallId) {
569
614
  toolCallId
570
615
  });
571
616
  }
572
- function createMutableLiveTurnProjection() {
617
+ function createProjectionState() {
573
618
  return {
574
619
  assistant: {
575
620
  text: "",
576
621
  thinking: "",
577
622
  lastUpdatedAt: null
578
623
  },
579
- toolCalls: [],
580
624
  traceItems: [],
581
625
  usage: null,
582
626
  toolCallsById: /* @__PURE__ */ new Map(),
583
- openToolCallIdsByName: /* @__PURE__ */ new Map(),
584
627
  nextFallbackOrdinal: 1
585
628
  };
586
629
  }
587
- function snapshotLiveTurnProjection(projection) {
630
+ function snapshotProjection(projection, presentTool) {
588
631
  return {
589
632
  assistant: { ...projection.assistant },
590
- toolCalls: projection.toolCalls.map(cloneToolCall),
591
633
  traceItems: projection.traceItems.map((item) => ({ ...item })),
592
- usage: cloneUsageSnapshot(projection.usage)
634
+ usage: cloneUsageSnapshot(projection.usage),
635
+ toolCalls: Array.from(
636
+ projection.toolCallsById.values(),
637
+ (tool) => presentTool(cloneToolCall(tool))
638
+ )
593
639
  };
594
640
  }
595
641
  function applyPiEventToLiveTurnProjection(projection, input) {
596
642
  let timestamp = readWsEventTimestamp(input.data, input.fallbackTimestamp);
643
+ let canonicalData = input.data;
644
+ let toolCallId = null;
645
+ let toolName = null;
597
646
  if (input.eventType === "pi.stream.delta") {
598
647
  const text = readWsEventStreamText(input.data);
599
648
  if (text) {
@@ -607,15 +656,15 @@ function applyPiEventToLiveTurnProjection(projection, input) {
607
656
  }
608
657
  } else if (input.eventType === "pi.tool.call" || input.eventType === "pi.tool.result") {
609
658
  const isResult = input.eventType === "pi.tool.result";
610
- const toolName = readWsEventToolName(input.data);
611
- const toolCallId = canonicalizeToolCallId({
659
+ toolName = readWsEventToolName(input.data);
660
+ toolCallId = canonicalizeToolCallId({
612
661
  projection,
613
662
  data: input.data,
614
663
  timestamp,
615
664
  toolName,
616
665
  preferOpenCall: isResult
617
666
  });
618
- const canonicalData = readWsEventToolCallId(input.data) === toolCallId ? input.data : { ...input.data, toolCallId };
667
+ canonicalData = readWsEventToolCallId(input.data) === toolCallId ? input.data : { ...input.data, toolCallId };
619
668
  const existingToolCall = projection.toolCallsById.get(toolCallId);
620
669
  const toolCall = existingToolCall ?? {
621
670
  toolCallId,
@@ -627,43 +676,30 @@ function applyPiEventToLiveTurnProjection(projection, input) {
627
676
  result: null,
628
677
  isError: false
629
678
  };
630
- if (!existingToolCall) {
631
- projection.toolCalls.push(toolCall);
632
- projection.toolCallsById.set(toolCallId, toolCall);
633
- }
679
+ if (!existingToolCall) projection.toolCallsById.set(toolCallId, toolCall);
634
680
  if (isResult) {
635
681
  toolCall.status = "completed";
636
682
  toolCall.completedAt = timestamp;
637
683
  toolCall.result = canonicalData.result ?? null;
684
+ delete toolCall.restoredResult;
638
685
  toolCall.isError = canonicalData.isError === true;
639
- if (existingToolCall) removeOpenToolCallId(projection, toolCallId);
640
686
  }
641
687
  ensureToolTraceItem(projection, toolCallId);
642
- if (!isResult) addOpenToolCallId(projection, toolName, toolCallId);
643
- return {
644
- canonicalData,
645
- timestamp,
646
- toolCallId,
647
- toolName
648
- };
649
688
  } else if (input.eventType === "pi.usage") {
650
689
  projection.usage = mergeUsageSnapshots(projection.usage, normalizeUsageSnapshot(input.data));
651
690
  } else {
652
- const operationalTraceItem = buildPrimaryPathOperationalTraceItem({
653
- eventType: input.eventType,
654
- data: input.data,
655
- fallbackTimestamp: input.fallbackTimestamp
656
- });
691
+ const operationalTraceItem = buildPrimaryPathOperationalTraceItem(input);
657
692
  if (operationalTraceItem) {
658
693
  projection.traceItems.push(operationalTraceItem);
659
694
  timestamp = operationalTraceItem.timestamp;
660
695
  }
661
696
  }
662
697
  return {
663
- canonicalData: input.data,
698
+ canonicalData,
664
699
  timestamp,
665
- toolCallId: null,
666
- toolName: null
700
+ toolCallId,
701
+ toolName,
702
+ usage: cloneUsageSnapshot(projection.usage)
667
703
  };
668
704
  }
669
705
  function compareProcessEventsChronologically(left, right) {
@@ -682,42 +718,61 @@ function compareProcessEventsChronologically(left, right) {
682
718
  }
683
719
  return left.id.localeCompare(right.id);
684
720
  }
685
- function buildLiveTurnProjectionFromEvents(events) {
686
- const projection = createMutableLiveTurnProjection();
721
+ function snapshotToolTrace({
722
+ result,
723
+ restoredResult,
724
+ ...tool
725
+ }) {
726
+ if (restoredResult) return { ...tool, ...restoredResult };
727
+ const record = result && typeof result === "object" ? result : null;
728
+ const resultText = result == null ? null : typeof result === "string" ? result : record && "content" in record ? extractPiSessionMessageText(record.content) : JSON.stringify(result, null, 2);
729
+ return {
730
+ ...tool,
731
+ resultText,
732
+ truncated: isToolResultTruncated({
733
+ resultText,
734
+ resultDetails: record?.details,
735
+ resultValue: result
736
+ })
737
+ };
738
+ }
739
+ function traceProjection(state, retainedPiInput = null) {
740
+ return {
741
+ apply: (event) => applyPiEventToLiveTurnProjection(state, event),
742
+ snapshot: (piInput = retainedPiInput) => ({
743
+ ...snapshotProjection(state, snapshotToolTrace),
744
+ piInput
745
+ })
746
+ };
747
+ }
748
+ function createLiveTurnProjection(events = []) {
749
+ const state = createProjectionState();
687
750
  for (const event of [...events].sort(compareProcessEventsChronologically)) {
688
- applyPiEventToLiveTurnProjection(projection, {
751
+ applyPiEventToLiveTurnProjection(state, {
689
752
  eventType: event.eventType,
690
753
  data: asWsEventPayloadRecord(event.data),
691
754
  fallbackTimestamp: event.createdAt
692
755
  });
693
756
  }
694
- return projection;
695
- }
696
-
697
- // src/pi-session-message.ts
698
- function isPiSessionMessageEntryType(entry) {
699
- return entry?.type === "message";
700
- }
701
- function isPiSessionMessageRecord(value) {
702
- return typeof value === "object" && value !== null && !Array.isArray(value);
703
- }
704
- function isPiSessionMessageEntryWithRecord(entry) {
705
- return isPiSessionMessageEntryType(entry) && isPiSessionMessageRecord(entry.message);
757
+ return {
758
+ ...traceProjection(state),
759
+ rawSnapshot: () => snapshotProjection(state, ({ restoredResult: _restoredResult, ...tool }) => tool)
760
+ };
706
761
  }
707
- function extractPiSessionMessageText(content) {
708
- if (typeof content === "string") {
709
- return content;
710
- }
711
- if (!Array.isArray(content)) {
712
- return "";
762
+ function restoreTurnTraceProjection(trace) {
763
+ const state = createProjectionState();
764
+ state.assistant = { ...trace.assistant };
765
+ state.traceItems = trace.traceItems.map((item) => ({ ...item }));
766
+ state.usage = cloneUsageSnapshot(trace.usage);
767
+ for (const { resultText, truncated, ...tool } of trace.toolCalls) {
768
+ const toolCall = cloneToolCall({
769
+ ...tool,
770
+ result: null,
771
+ restoredResult: { resultText, truncated }
772
+ });
773
+ state.toolCallsById.set(toolCall.toolCallId, toolCall);
713
774
  }
714
- return content.map((block) => {
715
- if (typeof block !== "object" || block === null || Array.isArray(block)) {
716
- return "";
717
- }
718
- const record = block;
719
- return record.type === "text" && typeof record.text === "string" ? record.text : "";
720
- }).join("");
775
+ return traceProjection(state, trace.piInput);
721
776
  }
722
777
 
723
778
  // src/session-entry-tree.ts
@@ -763,6 +818,67 @@ function createReadonlyEntryTree(entries) {
763
818
  };
764
819
  }
765
820
 
821
+ // src/skill-pack.ts
822
+ import { orderDependencies } from "@leitwerk-dev/domain";
823
+ import * as v3 from "valibot";
824
+ var nonEmpty = v3.pipe(v3.string(), v3.minLength(1));
825
+ var skillId = v3.pipe(v3.string(), v3.regex(/^[a-z0-9][a-z0-9-]{0,62}$/));
826
+ var relativePath = v3.pipe(
827
+ v3.string(),
828
+ v3.check(
829
+ (value) => value.length > 0 && !value.includes("\\") && !value.includes("\0") && value.split("/").every((part) => part !== "" && part !== "." && part !== ".." && part !== ".git")
830
+ )
831
+ );
832
+ var upstreamSchema = v3.object({
833
+ /** @public */
834
+ url: nonEmpty,
835
+ /** @public */
836
+ commit: v3.pipe(v3.string(), v3.regex(/^(?:[a-f0-9]{40}|[a-f0-9]{64})$/))
837
+ });
838
+ var entrySchema = v3.object({
839
+ /** @public */
840
+ id: skillId,
841
+ /** @public */
842
+ directory: relativePath,
843
+ /** @public */
844
+ label: nonEmpty,
845
+ /** @public */
846
+ description: nonEmpty,
847
+ /** @public */
848
+ sourcePath: relativePath,
849
+ /** @public */
850
+ dependencies: v3.array(skillId)
851
+ });
852
+ var manifestSchema = v3.object({
853
+ /** @public */
854
+ formatVersion: v3.literal(1),
855
+ /** @public */
856
+ upstream: upstreamSchema,
857
+ /** @public */
858
+ patchDigest: v3.pipe(v3.string(), v3.regex(/^[a-f0-9]{64}$/)),
859
+ /** @public */
860
+ skills: v3.pipe(v3.array(entrySchema), v3.minLength(1))
861
+ });
862
+ function parseSkillPackManifest(input) {
863
+ const manifest = v3.parse(manifestSchema, input);
864
+ const byId = new Map(manifest.skills.map((skill) => [skill.id, skill]));
865
+ if (byId.size !== manifest.skills.length) throw new Error("Duplicate skill ID in skill pack");
866
+ const directories = new Set(manifest.skills.map((skill) => skill.directory));
867
+ if (directories.size !== manifest.skills.length) throw new Error("Duplicate skill directory");
868
+ orderDependencies(
869
+ byId.keys(),
870
+ (id) => {
871
+ const entry = byId.get(id);
872
+ if (!entry) throw new Error(`Missing skill pack dependency '${id}'`);
873
+ if (new Set(entry.dependencies).size !== entry.dependencies.length)
874
+ throw new Error(`Duplicate dependencies for skill '${id}'`);
875
+ return entry.dependencies;
876
+ },
877
+ (id) => `Circular skill dependency at '${id}'`
878
+ );
879
+ return manifest;
880
+ }
881
+
766
882
  // src/streamable-events.ts
767
883
  var STREAMABLE_WORKER_EVENT_TYPES = [
768
884
  "pi.stream.delta",
@@ -818,80 +934,6 @@ function buildActiveTimelineTurnSummary(activeTurn, input = { summary: `Current
818
934
  };
819
935
  }
820
936
 
821
- // src/tool-result-truncation.ts
822
- import { asUnknownRecord as asUnknownRecord2 } from "@leitwerk-dev/domain";
823
- var TOOL_TRUNCATION_BOOLEAN_KEYS = [
824
- "truncated",
825
- "isTruncated",
826
- "wasTruncated",
827
- "outputTruncated",
828
- "resultTruncated"
829
- ];
830
- var TOOL_TRUNCATION_TEXT_MARKERS = [
831
- "output truncated",
832
- "result truncated",
833
- "response was too big",
834
- "too large to display",
835
- "truncated after",
836
- "truncated to last",
837
- "truncated since"
838
- ];
839
- function readTruncationPayload(value) {
840
- const record = asUnknownRecord2(value);
841
- if (!record) {
842
- return null;
843
- }
844
- if ("truncation" in record) {
845
- return record.truncation;
846
- }
847
- for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
848
- if (key in record) {
849
- return record[key];
850
- }
851
- }
852
- return null;
853
- }
854
- function isExplicitFalseTruncationString(value) {
855
- const normalized = value.trim().toLowerCase();
856
- return normalized === "" || normalized === "false" || normalized === "none" || normalized === "null" || normalized === "no" || normalized === "0";
857
- }
858
- function isTruthyTruncationValue(value) {
859
- if (value === null || value === void 0) {
860
- return false;
861
- }
862
- if (typeof value === "boolean") {
863
- return value;
864
- }
865
- if (typeof value === "number") {
866
- return Number.isFinite(value) && value > 0;
867
- }
868
- if (typeof value === "string") {
869
- return !isExplicitFalseTruncationString(value);
870
- }
871
- if (Array.isArray(value)) {
872
- return value.length > 0;
873
- }
874
- const record = asUnknownRecord2(value);
875
- if (!record) {
876
- return true;
877
- }
878
- for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
879
- if (record[key] === true) {
880
- return true;
881
- }
882
- }
883
- return Object.keys(record).some((key) => {
884
- if (TOOL_TRUNCATION_BOOLEAN_KEYS.includes(key)) {
885
- return false;
886
- }
887
- return isTruthyTruncationValue(record[key]);
888
- });
889
- }
890
- function isToolResultTruncated(input) {
891
- const normalizedText = input.resultText?.toLowerCase() ?? "";
892
- return TOOL_TRUNCATION_TEXT_MARKERS.some((marker) => normalizedText.includes(marker)) || isTruthyTruncationValue(readTruncationPayload(input.resultDetails)) || isTruthyTruncationValue(readTruncationPayload(input.resultValue));
893
- }
894
-
895
937
  // src/turn-continuation.ts
896
938
  import { trimToNull } from "@leitwerk-dev/domain";
897
939
  function branchContainsAncestor(entriesById, entryId, ancestorId) {
@@ -1034,28 +1076,6 @@ function hasTurnContinuationProgress(entries, turnRecord, bounds = {}) {
1034
1076
  return resolveTurnContinuationLeafEntryId(entries, turnRecord, bounds) !== null;
1035
1077
  }
1036
1078
 
1037
- // src/turn-trace-projection.ts
1038
- function snapshotTurnTrace(projection, piInput = null) {
1039
- const snapshot = snapshotLiveTurnProjection(projection);
1040
- return {
1041
- ...snapshot,
1042
- piInput,
1043
- toolCalls: snapshot.toolCalls.map(({ result, ...tool }) => {
1044
- const record = result && typeof result === "object" ? result : null;
1045
- const resultText = result == null ? null : typeof result === "string" ? result : record && "content" in record ? extractPiSessionMessageText(record.content) : JSON.stringify(result, null, 2);
1046
- return {
1047
- ...tool,
1048
- resultText,
1049
- truncated: "truncated" in tool && tool.truncated === true || isToolResultTruncated({
1050
- resultText,
1051
- resultDetails: record?.details,
1052
- resultValue: result
1053
- })
1054
- };
1055
- })
1056
- };
1057
- }
1058
-
1059
1079
  // src/usage-by-turn-record.ts
1060
1080
  function buildUsageSnapshotsByTurnRecordId(events) {
1061
1081
  const snapshots = {};
@@ -1086,18 +1106,16 @@ export {
1086
1106
  WS_PROCESS_TYPES,
1087
1107
  WS_PROTOCOL_VERSION,
1088
1108
  applyEventToCompactTurnSummary,
1089
- applyPiEventToLiveTurnProjection,
1090
1109
  asWsEventPayloadRecord,
1091
1110
  browserUiExtensionDescriptorSchema,
1092
1111
  buildActiveTimelineTurnSummary,
1093
- buildLiveTurnProjectionFromEvents,
1094
1112
  buildPrimaryPathOperationalTraceItem,
1095
1113
  buildUsageSnapshotsByTurnRecordId,
1096
1114
  cloneUsageSnapshot,
1097
1115
  compareTimestampStrings,
1098
1116
  createDurableWsFrame,
1099
1117
  createEphemeralWsFrame,
1100
- createMutableLiveTurnProjection,
1118
+ createLiveTurnProjection,
1101
1119
  createReadonlyEntryTree,
1102
1120
  createTurnContinuationIndex,
1103
1121
  emptyCompactTurnSummary,
@@ -1123,6 +1141,7 @@ export {
1123
1141
  parseLauncherTurnConfigsJson,
1124
1142
  parsePrimaryPathWsFrameInput,
1125
1143
  parseSchema,
1144
+ parseSkillPackManifest,
1126
1145
  parseWsFrame,
1127
1146
  projectFutureExecutionOverview,
1128
1147
  readValueAtPath,
@@ -1136,10 +1155,9 @@ export {
1136
1155
  reasoningPreviewTail,
1137
1156
  resolveTurnContinuationLeafEntryId,
1138
1157
  resolveTurnContinuationUserPrompt,
1158
+ restoreTurnTraceProjection,
1139
1159
  serializeFutureActionPayload,
1140
1160
  serializeFutureLaunchPayload,
1141
- snapshotLiveTurnProjection,
1142
- snapshotTurnTrace,
1143
1161
  timelinePresentationForTurnType,
1144
1162
  truncateTextPreview,
1145
1163
  validateScheduleRequestInput