@leitwerk-dev/protocol 0.1.9 → 0.2.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 (162) hide show
  1. package/dist/api-token-contracts.d.ts +45 -0
  2. package/dist/api-token-contracts.d.ts.map +1 -0
  3. package/dist/{chunk-SKQG5SE3.js → chunk-4AKWRRFS.js} +113 -28
  4. package/dist/chunk-4AKWRRFS.js.map +1 -0
  5. package/dist/{chunk-J7LXDQAN.js → chunk-LTIDCJFK.js} +185 -6
  6. package/dist/chunk-LTIDCJFK.js.map +1 -0
  7. package/dist/{chunk-IWVU76M5.js → chunk-RUPDB3WZ.js} +1 -1
  8. package/dist/chunk-RUPDB3WZ.js.map +1 -0
  9. package/dist/compact-turn-summary.d.ts +49 -0
  10. package/dist/compact-turn-summary.d.ts.map +1 -0
  11. package/dist/config-snapshot-public.d.ts +2 -0
  12. package/dist/config-snapshot-public.d.ts.map +1 -0
  13. package/dist/config-snapshot-public.js +1 -0
  14. package/dist/config-snapshot.d.ts +110 -15
  15. package/dist/config-snapshot.d.ts.map +1 -1
  16. package/dist/config-snapshot.js +0 -1
  17. package/dist/config-snapshot.js.map +1 -1
  18. package/dist/extension-ui-contracts.d.ts +49 -0
  19. package/dist/extension-ui-contracts.d.ts.map +1 -0
  20. package/dist/external-review.d.ts +5 -1
  21. package/dist/external-review.d.ts.map +1 -1
  22. package/dist/form-contract-public.d.ts +2 -0
  23. package/dist/form-contract-public.d.ts.map +1 -0
  24. package/dist/form-contract-public.js +1 -0
  25. package/dist/form-contract.d.ts +29 -5
  26. package/dist/form-contract.d.ts.map +1 -1
  27. package/dist/form-contract.js +0 -1
  28. package/dist/form-contract.js.map +1 -1
  29. package/dist/{http-contracts-DP9P_2YM.d.ts → http-contracts-public-rL4HFcxz.d.ts} +1099 -47
  30. package/dist/http-contracts-public.d.ts +4 -0
  31. package/dist/http-contracts-public.d.ts.map +1 -0
  32. package/dist/http-contracts-public.js +10 -0
  33. package/dist/http-contracts.d.ts +923 -45
  34. package/dist/http-contracts.d.ts.map +1 -1
  35. package/dist/http-contracts.js +32 -432
  36. package/dist/http-contracts.js.map +1 -1
  37. package/dist/index.d.ts +30 -20
  38. package/dist/index.d.ts.map +1 -1
  39. package/dist/index.js +1146 -20
  40. package/dist/index.js.map +1 -1
  41. package/dist/initial-prompt.d.ts +6 -0
  42. package/dist/initial-prompt.d.ts.map +1 -1
  43. package/dist/launcher-contract-public.d.ts +2 -0
  44. package/dist/launcher-contract-public.d.ts.map +1 -0
  45. package/dist/launcher-contract-public.js +1 -0
  46. package/dist/launcher-contract-public.js.map +1 -0
  47. package/dist/launcher-contract.d.ts +26 -1
  48. package/dist/launcher-contract.d.ts.map +1 -1
  49. package/dist/launcher-contract.js +0 -1
  50. package/dist/launcher-contract.js.map +1 -1
  51. package/dist/live-turn-projection.d.ts +30 -1
  52. package/dist/live-turn-projection.d.ts.map +1 -1
  53. package/dist/pi-session-message.d.ts +16 -0
  54. package/dist/pi-session-message.d.ts.map +1 -1
  55. package/dist/primary-path-snapshot.d.ts +64 -0
  56. package/dist/primary-path-snapshot.d.ts.map +1 -1
  57. package/dist/protocol.d.ts +450 -17
  58. package/dist/protocol.d.ts.map +1 -1
  59. package/dist/protocol.js +37 -259
  60. package/dist/protocol.js.map +1 -1
  61. package/dist/session-entry-tree.d.ts +12 -0
  62. package/dist/session-entry-tree.d.ts.map +1 -1
  63. package/dist/streamable-events.d.ts +4 -1
  64. package/dist/streamable-events.d.ts.map +1 -1
  65. package/dist/text-preview.d.ts +5 -0
  66. package/dist/text-preview.d.ts.map +1 -1
  67. package/dist/ticket-creation-contracts.d.ts +55 -0
  68. package/dist/ticket-creation-contracts.d.ts.map +1 -0
  69. package/dist/timeline-projection.d.ts +6 -1
  70. package/dist/timeline-projection.d.ts.map +1 -1
  71. package/dist/timestamp-ordering.d.ts +2 -0
  72. package/dist/timestamp-ordering.d.ts.map +1 -1
  73. package/dist/tool-renderer-contract-public.d.ts +3 -0
  74. package/dist/tool-renderer-contract-public.d.ts.map +1 -0
  75. package/dist/tool-renderer-contract-public.js +9 -0
  76. package/dist/tool-renderer-contract-public.js.map +1 -0
  77. package/dist/tool-renderer-contract.d.ts +15 -0
  78. package/dist/tool-renderer-contract.d.ts.map +1 -1
  79. package/dist/tool-renderer-contract.js +10 -21
  80. package/dist/tool-renderer-contract.js.map +1 -1
  81. package/dist/tool-result-truncation.d.ts +10 -0
  82. package/dist/tool-result-truncation.d.ts.map +1 -0
  83. package/dist/turn-continuation.d.ts +29 -1
  84. package/dist/turn-continuation.d.ts.map +1 -1
  85. package/dist/turn-trace-projection.d.ts +5 -0
  86. package/dist/turn-trace-projection.d.ts.map +1 -0
  87. package/dist/usage-by-turn-record.d.ts +2 -0
  88. package/dist/usage-by-turn-record.d.ts.map +1 -1
  89. package/dist/usage-snapshot.d.ts +23 -4
  90. package/dist/usage-snapshot.d.ts.map +1 -1
  91. package/dist/ws-event-payloads.d.ts +16 -1
  92. package/dist/ws-event-payloads.d.ts.map +1 -1
  93. package/package.json +18 -18
  94. package/src/api-token-contracts.ts +44 -0
  95. package/src/compact-turn-summary.ts +141 -0
  96. package/src/config-snapshot-public.ts +12 -0
  97. package/src/config-snapshot.ts +110 -15
  98. package/src/extension-ui-contracts.ts +67 -0
  99. package/src/external-review.ts +5 -1
  100. package/src/form-contract-public.ts +6 -0
  101. package/src/form-contract.ts +29 -5
  102. package/src/http-contracts-public.ts +86 -0
  103. package/src/http-contracts.ts +1059 -102
  104. package/src/index.ts +184 -20
  105. package/src/initial-prompt.ts +9 -6
  106. package/src/launcher-contract-public.ts +8 -0
  107. package/src/launcher-contract.ts +26 -1
  108. package/src/live-turn-projection.ts +70 -113
  109. package/src/pi-session-message.ts +21 -3
  110. package/src/primary-path-snapshot.ts +64 -0
  111. package/src/protocol.ts +461 -26
  112. package/src/session-entry-tree.ts +12 -0
  113. package/src/streamable-events.ts +5 -0
  114. package/src/text-preview.ts +5 -0
  115. package/src/ticket-creation-contracts.ts +62 -0
  116. package/src/timeline-projection.ts +10 -4
  117. package/src/timestamp-ordering.ts +2 -0
  118. package/src/tool-renderer-contract-public.ts +10 -0
  119. package/src/tool-renderer-contract.ts +15 -0
  120. package/src/tool-result-truncation.ts +98 -0
  121. package/src/turn-continuation.ts +50 -46
  122. package/src/turn-trace-projection.ts +42 -0
  123. package/src/usage-by-turn-record.ts +2 -0
  124. package/src/usage-snapshot.ts +45 -62
  125. package/src/ws-event-payloads.ts +19 -14
  126. package/dist/chunk-6L4W7WPV.js +0 -1
  127. package/dist/chunk-AAV3AAOF.js +0 -1
  128. package/dist/chunk-IEMT3JG4.js +0 -1
  129. package/dist/chunk-IWVU76M5.js.map +0 -1
  130. package/dist/chunk-J7LXDQAN.js.map +0 -1
  131. package/dist/chunk-SKQG5SE3.js.map +0 -1
  132. package/dist/external-review.js +0 -2
  133. package/dist/external-review.js.map +0 -1
  134. package/dist/initial-prompt.js +0 -103
  135. package/dist/initial-prompt.js.map +0 -1
  136. package/dist/live-turn-projection.js +0 -395
  137. package/dist/live-turn-projection.js.map +0 -1
  138. package/dist/pi-session-message.js +0 -27
  139. package/dist/pi-session-message.js.map +0 -1
  140. package/dist/primary-path-snapshot.js +0 -2
  141. package/dist/primary-path-snapshot.js.map +0 -1
  142. package/dist/session-entry-tree.js +0 -42
  143. package/dist/session-entry-tree.js.map +0 -1
  144. package/dist/streamable-events.js +0 -17
  145. package/dist/streamable-events.js.map +0 -1
  146. package/dist/text-preview.js +0 -23
  147. package/dist/text-preview.js.map +0 -1
  148. package/dist/timeline-projection.js +0 -33
  149. package/dist/timeline-projection.js.map +0 -1
  150. package/dist/timestamp-ordering.js +0 -15
  151. package/dist/timestamp-ordering.js.map +0 -1
  152. package/dist/turn-continuation.js +0 -179
  153. package/dist/turn-continuation.js.map +0 -1
  154. package/dist/usage-by-turn-record.js +0 -25
  155. package/dist/usage-by-turn-record.js.map +0 -1
  156. package/dist/usage-snapshot.js +0 -120
  157. package/dist/usage-snapshot.js.map +0 -1
  158. package/dist/ws-event-payloads.js +0 -47
  159. package/dist/ws-event-payloads.js.map +0 -1
  160. /package/dist/{chunk-6L4W7WPV.js.map → config-snapshot-public.js.map} +0 -0
  161. /package/dist/{chunk-AAV3AAOF.js.map → form-contract-public.js.map} +0 -0
  162. /package/dist/{chunk-IEMT3JG4.js.map → http-contracts-public.js.map} +0 -0
@@ -1,18 +1,30 @@
1
+ /** @internal */
1
2
  export interface SessionTreeEntryLike {
3
+ /** @internal */
2
4
  id: string;
5
+ /** @internal */
3
6
  parentId: string | null;
7
+ /** @internal */
4
8
  timestamp: string;
9
+ /** @internal */
5
10
  type: string;
6
11
  }
7
12
 
13
+ /** @internal */
8
14
  export interface ReadonlyEntryTree<TEntry extends SessionTreeEntryLike> {
15
+ /** @internal */
9
16
  entries: TEntry[];
17
+ /** @internal */
10
18
  leafId: string | null;
19
+ /** @internal */
11
20
  getEntry(id: string): TEntry | undefined;
21
+ /** @internal */
12
22
  getChildren(parentId: string): TEntry[];
23
+ /** @internal */
13
24
  getBranch(fromId?: string | null): TEntry[];
14
25
  }
15
26
 
27
+ /** @internal */
16
28
  export function createReadonlyEntryTree<TEntry extends SessionTreeEntryLike>(
17
29
  entries: readonly TEntry[],
18
30
  ): ReadonlyEntryTree<TEntry> {
@@ -1,3 +1,4 @@
1
+ /** @internal */
1
2
  export const STREAMABLE_WORKER_EVENT_TYPES = [
2
3
  "pi.stream.delta",
3
4
  "pi.turn.start",
@@ -9,12 +10,16 @@ export const STREAMABLE_WORKER_EVENT_TYPES = [
9
10
  "pi.retry.start",
10
11
  "pi.retry.end",
11
12
  "pi.usage",
13
+ "pi.compaction.start",
14
+ "pi.compaction.end",
12
15
  ] as const;
13
16
 
17
+ /** @internal */
14
18
  export type StreamableWorkerEventType = (typeof STREAMABLE_WORKER_EVENT_TYPES)[number];
15
19
 
16
20
  const STREAMABLE_EVENT_TYPES = new Set<string>(STREAMABLE_WORKER_EVENT_TYPES);
17
21
 
22
+ /** @internal */
18
23
  export function isStreamableEvent(eventType: string): eventType is StreamableWorkerEventType {
19
24
  return STREAMABLE_EVENT_TYPES.has(eventType);
20
25
  }
@@ -1,14 +1,19 @@
1
+ /** @internal */
1
2
  export interface TextPreview {
3
+ /** @internal */
2
4
  text: string;
5
+ /** @internal */
3
6
  truncated: boolean;
4
7
  }
5
8
 
9
+ /** @internal */
6
10
  export function truncateTextPreview(value: string, maxLength: number): TextPreview {
7
11
  return value.length <= maxLength
8
12
  ? { text: value, truncated: false }
9
13
  : { text: value.slice(0, maxLength).trimEnd(), truncated: true };
10
14
  }
11
15
 
16
+ /** @internal */
12
17
  export function buildTrailingLinePreview(
13
18
  value: string,
14
19
  lineCount: number,
@@ -0,0 +1,62 @@
1
+ import type { ProcessRelation } from "@leitwerk-dev/domain";
2
+
3
+ /** @internal */
4
+ export interface TicketCreationToolSummary {
5
+ /** @internal */
6
+ name: string;
7
+ /** @internal */
8
+ displayName: string;
9
+ }
10
+
11
+ /** @internal */
12
+ export type TicketResultArtifactLocator =
13
+ | {
14
+ /** @internal */
15
+ kind: "turn_result";
16
+ /** @internal */
17
+ turnRecordId: string;
18
+ }
19
+ | {
20
+ /** @internal */
21
+ kind: "leaf_outcome";
22
+ /** @internal */
23
+ leafEntryId: string;
24
+ };
25
+
26
+ /** @internal */
27
+ export interface TicketCreationFocus {
28
+ /** @internal */
29
+ kind: "whole_result" | "excerpt";
30
+ /** Browser-selected normalized text. The server still validates the artifact. @internal */
31
+ excerpt?: string;
32
+ }
33
+
34
+ /** @internal */
35
+ export interface LaunchTicketCreationRequestBody {
36
+ /** @internal */
37
+ artifact: TicketResultArtifactLocator;
38
+ /** @internal */
39
+ focus: TicketCreationFocus;
40
+ /** @internal */
41
+ additionalInstructions?: string;
42
+ /** @internal */
43
+ toolName: string;
44
+ /** @internal */
45
+ modelProfileId?: string;
46
+ }
47
+
48
+ /** @internal */
49
+ export interface LaunchTicketCreationResponseBody {
50
+ /** @internal */
51
+ childInstanceId: string;
52
+ /** @internal */
53
+ relation: ProcessRelation;
54
+ }
55
+
56
+ /** @internal */
57
+ export interface ResolveToolApprovalRequestBody {
58
+ /** @internal */
59
+ action: "accept" | "feedback" | "decline";
60
+ /** @internal */
61
+ feedback?: string;
62
+ }
@@ -4,21 +4,27 @@ import type {
4
4
  } from "./http-contracts.js";
5
5
  import type { PrimaryPathActiveTurnSnapshot } from "./primary-path-snapshot.js";
6
6
 
7
+ /** @internal */
7
8
  export function timelinePresentationForTurnType(
8
9
  turnType: ProcessTimelineTurnSummary["turnType"],
9
10
  ): ProcessTimelineTurnPresentation {
10
11
  if (turnType === "external") return "external_trigger";
11
12
  if (turnType === "human") return "operator_decision";
12
- return turnType === "automatic" || turnType === "server_automatic"
13
- ? "automatic_turn"
14
- : "llm_turn";
13
+ return turnType === "automatic" ? "automatic_turn" : "llm_turn";
15
14
  }
16
15
 
16
+ /** @internal */
17
17
  export function buildActiveTimelineTurnSummary(
18
- activeTurn: PrimaryPathActiveTurnSnapshot,
18
+ activeTurn: Pick<
19
+ PrimaryPathActiveTurnSnapshot,
20
+ "turnRecordId" | "turnId" | "turnType" | "pathType" | "startedAt" | "assistant"
21
+ >,
19
22
  input: {
23
+ /** @internal */
20
24
  summary: string;
25
+ /** @internal */
21
26
  output: string;
27
+ /** @internal */
22
28
  actionSource?: ProcessTimelineTurnSummary["actionSource"];
23
29
  } = { summary: `Current step: ${activeTurn.turnId}`, output: activeTurn.assistant.text.trim() },
24
30
  ): ProcessTimelineTurnSummary {
@@ -1,3 +1,4 @@
1
+ /** @internal */
1
2
  export function compareTimestampStrings(left: string, right: string): number {
2
3
  const leftMs = Date.parse(left);
3
4
  const rightMs = Date.parse(right);
@@ -7,6 +8,7 @@ export function compareTimestampStrings(left: string, right: string): number {
7
8
  return left.localeCompare(right);
8
9
  }
9
10
 
11
+ /** @internal */
10
12
  export function happenedOnOrAfterStart(entryTimestamp: string, startedAt: string | null): boolean {
11
13
  if (!startedAt) {
12
14
  return true;
@@ -0,0 +1,10 @@
1
+ export type {
2
+ ToolCallRendererDefinition,
3
+ ToolCallRendererFieldDefinition,
4
+ ToolCallRendererValueKind,
5
+ ToolCallRendererValueSource,
6
+ } from "./tool-renderer-contract.js";
7
+ export {
8
+ TOOL_CALL_RENDERER_VALUE_KINDS,
9
+ TOOL_CALL_RENDERER_VALUE_SOURCES,
10
+ } from "./tool-renderer-contract.js";
@@ -1,20 +1,34 @@
1
+ /** @internal */
1
2
  export const TOOL_CALL_RENDERER_VALUE_KINDS = ["markdown", "plaintext", "json"] as const;
3
+ /** @internal */
2
4
  export type ToolCallRendererValueKind = (typeof TOOL_CALL_RENDERER_VALUE_KINDS)[number];
3
5
 
6
+ /** @internal */
4
7
  export const TOOL_CALL_RENDERER_VALUE_SOURCES = ["arguments", "result"] as const;
8
+ /** @internal */
5
9
  export type ToolCallRendererValueSource = (typeof TOOL_CALL_RENDERER_VALUE_SOURCES)[number];
6
10
 
11
+ /** @internal */
7
12
  export interface ToolCallRendererFieldDefinition {
13
+ /** @internal */
8
14
  id: string;
15
+ /** @internal */
9
16
  label: string;
17
+ /** @internal */
10
18
  kind: ToolCallRendererValueKind;
19
+ /** @internal */
11
20
  source: ToolCallRendererValueSource;
21
+ /** @internal */
12
22
  path?: string;
13
23
  }
14
24
 
25
+ /** @internal */
15
26
  export interface ToolCallRendererDefinition {
27
+ /** @internal */
16
28
  toolName: string;
29
+ /** @internal */
17
30
  title?: string;
31
+ /** @internal */
18
32
  fields: readonly ToolCallRendererFieldDefinition[];
19
33
  }
20
34
 
@@ -22,6 +36,7 @@ import * as v from "valibot";
22
36
 
23
37
  const unknownRecordSchema = v.record(v.string(), v.unknown());
24
38
 
39
+ /** @internal */
25
40
  export function readValueAtPath(value: unknown, path: string | undefined): unknown {
26
41
  if (!path || path.trim() === "") {
27
42
  return value;
@@ -0,0 +1,98 @@
1
+ import { asUnknownRecord } from "@leitwerk-dev/domain";
2
+
3
+ const TOOL_TRUNCATION_BOOLEAN_KEYS = [
4
+ "truncated",
5
+ "isTruncated",
6
+ "wasTruncated",
7
+ "outputTruncated",
8
+ "resultTruncated",
9
+ ] as const;
10
+ const TOOL_TRUNCATION_TEXT_MARKERS = [
11
+ "output truncated",
12
+ "result truncated",
13
+ "response was too big",
14
+ "too large to display",
15
+ "truncated after",
16
+ "truncated to last",
17
+ "truncated since",
18
+ ] as const;
19
+
20
+ function readTruncationPayload(value: unknown): unknown {
21
+ const record = asUnknownRecord(value);
22
+ if (!record) {
23
+ return null;
24
+ }
25
+ if ("truncation" in record) {
26
+ return record.truncation;
27
+ }
28
+ for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
29
+ if (key in record) {
30
+ return record[key];
31
+ }
32
+ }
33
+ return null;
34
+ }
35
+
36
+ function isExplicitFalseTruncationString(value: string): boolean {
37
+ const normalized = value.trim().toLowerCase();
38
+ return (
39
+ normalized === "" ||
40
+ normalized === "false" ||
41
+ normalized === "none" ||
42
+ normalized === "null" ||
43
+ normalized === "no" ||
44
+ normalized === "0"
45
+ );
46
+ }
47
+
48
+ function isTruthyTruncationValue(value: unknown): boolean {
49
+ if (value === null || value === undefined) {
50
+ return false;
51
+ }
52
+ if (typeof value === "boolean") {
53
+ return value;
54
+ }
55
+ if (typeof value === "number") {
56
+ return Number.isFinite(value) && value > 0;
57
+ }
58
+ if (typeof value === "string") {
59
+ return !isExplicitFalseTruncationString(value);
60
+ }
61
+ if (Array.isArray(value)) {
62
+ return value.length > 0;
63
+ }
64
+ const record = asUnknownRecord(value);
65
+ if (!record) {
66
+ return true;
67
+ }
68
+ for (const key of TOOL_TRUNCATION_BOOLEAN_KEYS) {
69
+ if (record[key] === true) {
70
+ return true;
71
+ }
72
+ }
73
+ return Object.keys(record).some((key) => {
74
+ if (
75
+ TOOL_TRUNCATION_BOOLEAN_KEYS.includes(key as (typeof TOOL_TRUNCATION_BOOLEAN_KEYS)[number])
76
+ ) {
77
+ return false;
78
+ }
79
+ return isTruthyTruncationValue(record[key]);
80
+ });
81
+ }
82
+
83
+ /** @internal */
84
+ export function isToolResultTruncated(input: {
85
+ /** @internal */
86
+ resultText: string | null;
87
+ /** @internal */
88
+ resultDetails: unknown;
89
+ /** @internal */
90
+ resultValue: unknown;
91
+ }): boolean {
92
+ const normalizedText = input.resultText?.toLowerCase() ?? "";
93
+ return (
94
+ TOOL_TRUNCATION_TEXT_MARKERS.some((marker) => normalizedText.includes(marker)) ||
95
+ isTruthyTruncationValue(readTruncationPayload(input.resultDetails)) ||
96
+ isTruthyTruncationValue(readTruncationPayload(input.resultValue))
97
+ );
98
+ }
@@ -4,45 +4,57 @@ import {
4
4
  isPiSessionMessageEntryType,
5
5
  isPiSessionMessageEntryWithRecord,
6
6
  } from "./pi-session-message.js";
7
- import { createReadonlyEntryTree, type ReadonlyEntryTree } from "./session-entry-tree.js";
7
+ import type { ReadonlyEntryTree } from "./session-entry-tree.js";
8
8
  import { compareTimestampStrings, happenedOnOrAfterStart } from "./timestamp-ordering.js";
9
9
 
10
+ /** @internal */
10
11
  export interface ContinuationTreeEntry {
12
+ /** @internal */
11
13
  id: string;
14
+ /** @internal */
12
15
  parentId: string | null;
16
+ /** @internal */
13
17
  timestamp: string;
18
+ /** @internal */
14
19
  type: string;
20
+ /** @internal */
15
21
  message?: {
22
+ /** @internal */
16
23
  role?: string;
24
+ /** @internal */
17
25
  content?: unknown;
18
26
  };
19
27
  }
20
28
 
29
+ /** @internal */
21
30
  export interface ContinuationTurnRecordLike {
31
+ /** @internal */
22
32
  forkPiEntryId?: string | null;
33
+ /** @internal */
23
34
  resultPiEntryId?: string | null;
35
+ /** @internal */
24
36
  startedAt?: string | null;
37
+ /** @internal */
25
38
  status?: string | null;
26
39
  }
27
40
 
41
+ /** @internal */
28
42
  export interface ContinuationSliceBounds {
43
+ /** @internal */
29
44
  endedAt?: string | null;
30
45
  }
31
46
 
47
+ /** @internal */
32
48
  export interface TurnContinuationIndex<TEntry extends ContinuationTreeEntry> {
49
+ /** @internal */
33
50
  resolveLeafEntryId(
34
51
  turnRecord: ContinuationTurnRecordLike,
35
52
  bounds?: ContinuationSliceBounds,
36
53
  ): string | null;
54
+ /** @internal */
37
55
  buildSlice(turnRecord: ContinuationTurnRecordLike, bounds?: ContinuationSliceBounds): TEntry[];
38
56
  }
39
57
 
40
- function createEntriesById(
41
- entries: readonly ContinuationTreeEntry[],
42
- ): Map<string, ContinuationTreeEntry> {
43
- return new Map(entries.map((entry) => [entry.id, entry]));
44
- }
45
-
46
58
  function branchContainsAncestor(
47
59
  entriesById: ReadonlyMap<string, ContinuationTreeEntry>,
48
60
  entryId: string,
@@ -70,10 +82,16 @@ function happenedOnOrBeforeEnd(entryTimestamp: string, endedAt: string | null):
70
82
  return compareTimestampStrings(entryTimestamp, endedAt) <= 0;
71
83
  }
72
84
 
73
- function isContinuableEntryType(
85
+ function isContinuableEntryWithinBounds(
74
86
  entry: ContinuationTreeEntry | undefined,
87
+ startedAt: string | null,
88
+ endedAt: string | null,
75
89
  ): entry is ContinuationTreeEntry {
76
- return isPiSessionMessageEntryType(entry) || entry?.type === "compaction";
90
+ return (
91
+ (isPiSessionMessageEntryType(entry) || entry?.type === "compaction") &&
92
+ happenedOnOrAfterStart(entry.timestamp, startedAt) &&
93
+ happenedOnOrBeforeEnd(entry.timestamp, endedAt)
94
+ );
77
95
  }
78
96
 
79
97
  function isExplicitLeafUsable(
@@ -84,19 +102,10 @@ function isExplicitLeafUsable(
84
102
  endedAt: string | null,
85
103
  ): boolean {
86
104
  const leafEntry = entriesById.get(leafId);
87
- if (!isContinuableEntryType(leafEntry)) {
105
+ if (!isContinuableEntryWithinBounds(leafEntry, startedAt, endedAt)) {
88
106
  return false;
89
107
  }
90
- if (!happenedOnOrAfterStart(leafEntry.timestamp, startedAt)) {
91
- return false;
92
- }
93
- if (!happenedOnOrBeforeEnd(leafEntry.timestamp, endedAt)) {
94
- return false;
95
- }
96
- if (!forkPiEntryId) {
97
- return true;
98
- }
99
- return branchContainsAncestor(entriesById, leafId, forkPiEntryId);
108
+ return !forkPiEntryId || branchContainsAncestor(entriesById, leafId, forkPiEntryId);
100
109
  }
101
110
 
102
111
  function findLatestContinuableEntryIdOnBranch(
@@ -106,16 +115,7 @@ function findLatestContinuableEntryIdOnBranch(
106
115
  ): string | null {
107
116
  for (let index = entries.length - 1; index >= 0; index -= 1) {
108
117
  const entry = entries[index];
109
- if (!entry) {
110
- continue;
111
- }
112
- if (!isContinuableEntryType(entry)) {
113
- continue;
114
- }
115
- if (!happenedOnOrAfterStart(entry.timestamp, options.startedAt)) {
116
- continue;
117
- }
118
- if (!happenedOnOrBeforeEnd(entry.timestamp, options.endedAt)) {
118
+ if (!isContinuableEntryWithinBounds(entry, options.startedAt, options.endedAt)) {
119
119
  continue;
120
120
  }
121
121
  if (options.ancestorId && !branchContainsAncestor(entriesById, entry.id, options.ancestorId)) {
@@ -126,11 +126,6 @@ function findLatestContinuableEntryIdOnBranch(
126
126
  return null;
127
127
  }
128
128
 
129
- function readMessageRole(entry: ContinuationTreeEntry | undefined): string | null {
130
- const role = entry?.message?.role;
131
- return typeof role === "string" && role.trim() !== "" ? role : null;
132
- }
133
-
134
129
  function resolveTurnContinuationLeafEntryIdFromIndex(
135
130
  entries: readonly ContinuationTreeEntry[],
136
131
  entriesById: ReadonlyMap<string, ContinuationTreeEntry>,
@@ -162,17 +157,17 @@ function resolveTurnContinuationLeafEntryIdFromIndex(
162
157
  });
163
158
  }
164
159
 
160
+ /** @internal */
165
161
  export function createTurnContinuationIndex<TEntry extends ContinuationTreeEntry>(
166
162
  entries: readonly TEntry[],
167
163
  ): TurnContinuationIndex<TEntry> {
168
- const orderedEntries = entries;
169
- const entriesById = createEntriesById(orderedEntries);
164
+ const entriesById = new Map(entries.map((entry) => [entry.id, entry]));
170
165
 
171
166
  const resolveLeafEntryId = (
172
167
  turnRecord: ContinuationTurnRecordLike,
173
168
  bounds: ContinuationSliceBounds = {},
174
169
  ): string | null =>
175
- resolveTurnContinuationLeafEntryIdFromIndex(orderedEntries, entriesById, turnRecord, bounds);
170
+ resolveTurnContinuationLeafEntryIdFromIndex(entries, entriesById, turnRecord, bounds);
176
171
 
177
172
  const buildSlice = (
178
173
  turnRecord: ContinuationTurnRecordLike,
@@ -185,7 +180,7 @@ export function createTurnContinuationIndex<TEntry extends ContinuationTreeEntry
185
180
  const forkPiEntryId = trimToNull(turnRecord.forkPiEntryId);
186
181
  const reversedSlice: TEntry[] = [];
187
182
  const visited = new Set<string>();
188
- let current = entriesById.get(continuationLeafId) as TEntry | undefined;
183
+ let current = entriesById.get(continuationLeafId);
189
184
  let foundFork = forkPiEntryId === null;
190
185
  while (current && !visited.has(current.id)) {
191
186
  visited.add(current.id);
@@ -194,9 +189,7 @@ export function createTurnContinuationIndex<TEntry extends ContinuationTreeEntry
194
189
  break;
195
190
  }
196
191
  reversedSlice.push(current);
197
- current = current.parentId
198
- ? (entriesById.get(current.parentId) as TEntry | undefined)
199
- : undefined;
192
+ current = current.parentId ? entriesById.get(current.parentId) : undefined;
200
193
  }
201
194
  if (!foundFork) {
202
195
  return [];
@@ -215,6 +208,7 @@ export function createTurnContinuationIndex<TEntry extends ContinuationTreeEntry
215
208
  return { resolveLeafEntryId, buildSlice };
216
209
  }
217
210
 
211
+ /** @internal */
218
212
  export function resolveTurnContinuationLeafEntryId(
219
213
  entries: readonly ContinuationTreeEntry[],
220
214
  turnRecord: ContinuationTurnRecordLike,
@@ -223,6 +217,7 @@ export function resolveTurnContinuationLeafEntryId(
223
217
  return createTurnContinuationIndex(entries).resolveLeafEntryId(turnRecord, bounds);
224
218
  }
225
219
 
220
+ /** @internal */
226
221
  export function buildTurnContinuationSliceFromTree<TEntry extends ContinuationTreeEntry>(
227
222
  tree: ReadonlyEntryTree<TEntry>,
228
223
  turnRecord: ContinuationTurnRecordLike,
@@ -231,19 +226,24 @@ export function buildTurnContinuationSliceFromTree<TEntry extends ContinuationTr
231
226
  return createTurnContinuationIndex(tree.entries).buildSlice(turnRecord, bounds);
232
227
  }
233
228
 
229
+ /** @internal */
234
230
  export function buildTurnContinuationSlice<TEntry extends ContinuationTreeEntry>(
235
231
  entries: readonly TEntry[],
236
232
  turnRecord: ContinuationTurnRecordLike,
237
233
  bounds: ContinuationSliceBounds = {},
238
234
  ): TEntry[] {
239
- return buildTurnContinuationSliceFromTree(createReadonlyEntryTree(entries), turnRecord, bounds);
235
+ return createTurnContinuationIndex(entries).buildSlice(turnRecord, bounds);
240
236
  }
241
237
 
238
+ /** @internal */
242
239
  export interface BranchUserPromptSnapshot {
240
+ /** @internal */
243
241
  text: string | null;
242
+ /** @internal */
244
243
  createdAt: string | null;
245
244
  }
246
245
 
246
+ /** @internal */
247
247
  export function extractFirstUserPromptOnBranch<TEntry extends ContinuationTreeEntry>(
248
248
  tree: ReadonlyEntryTree<TEntry>,
249
249
  leafId: string | null | undefined,
@@ -254,7 +254,7 @@ export function extractFirstUserPromptOnBranch<TEntry extends ContinuationTreeEn
254
254
  }
255
255
  const branch = tree.getBranch(resolvedLeafId);
256
256
  for (const entry of branch) {
257
- if (!isPiSessionMessageEntryWithRecord(entry) || readMessageRole(entry) !== "user") {
257
+ if (!isPiSessionMessageEntryWithRecord(entry) || entry.message.role !== "user") {
258
258
  continue;
259
259
  }
260
260
  const text = extractPiSessionMessageText(entry.message.content).trim();
@@ -269,6 +269,7 @@ export function extractFirstUserPromptOnBranch<TEntry extends ContinuationTreeEn
269
269
  return { text: null, createdAt: null };
270
270
  }
271
271
 
272
+ /** @internal */
272
273
  export function resolveTurnContinuationUserPrompt(
273
274
  entries: readonly ContinuationTreeEntry[],
274
275
  turnRecord: ContinuationTurnRecordLike,
@@ -278,10 +279,12 @@ export function resolveTurnContinuationUserPrompt(
278
279
  if (!continuationLeafId) {
279
280
  return null;
280
281
  }
281
- const continuationLeaf = createEntriesById(entries).get(continuationLeafId);
282
+ const continuationLeaf = new Map(entries.map((entry) => [entry.id, entry])).get(
283
+ continuationLeafId,
284
+ );
282
285
  if (
283
286
  !isPiSessionMessageEntryWithRecord(continuationLeaf) ||
284
- readMessageRole(continuationLeaf) !== "user"
287
+ continuationLeaf.message.role !== "user"
285
288
  ) {
286
289
  return null;
287
290
  }
@@ -289,6 +292,7 @@ export function resolveTurnContinuationUserPrompt(
289
292
  return promptText !== "" ? promptText : null;
290
293
  }
291
294
 
295
+ /** @internal */
292
296
  export function hasTurnContinuationProgress(
293
297
  entries: readonly ContinuationTreeEntry[],
294
298
  turnRecord: ContinuationTurnRecordLike,
@@ -0,0 +1,42 @@
1
+ import type { TurnTraceSnapshot } from "./http-contracts.js";
2
+ import {
3
+ type MutableLiveTurnProjection,
4
+ snapshotLiveTurnProjection,
5
+ } from "./live-turn-projection.js";
6
+ import { extractPiSessionMessageText } from "./pi-session-message.js";
7
+ import { isToolResultTruncated } from "./tool-result-truncation.js";
8
+
9
+ /** Share the recorded tool content presentation between live HTTP history and live frames. @internal */
10
+ export function snapshotTurnTrace(
11
+ projection: MutableLiveTurnProjection,
12
+ piInput: TurnTraceSnapshot["piInput"] = null,
13
+ ): TurnTraceSnapshot {
14
+ const snapshot = snapshotLiveTurnProjection(projection);
15
+ return {
16
+ ...snapshot,
17
+ piInput,
18
+ toolCalls: snapshot.toolCalls.map(({ result, ...tool }) => {
19
+ const record =
20
+ result && typeof result === "object" ? (result as Record<string, unknown>) : null;
21
+ const resultText =
22
+ result == null
23
+ ? null
24
+ : typeof result === "string"
25
+ ? result
26
+ : record && "content" in record
27
+ ? extractPiSessionMessageText(record.content)
28
+ : JSON.stringify(result, null, 2);
29
+ return {
30
+ ...tool,
31
+ resultText,
32
+ truncated:
33
+ ("truncated" in tool && tool.truncated === true) ||
34
+ isToolResultTruncated({
35
+ resultText,
36
+ resultDetails: record?.details,
37
+ resultValue: result,
38
+ }),
39
+ };
40
+ }),
41
+ };
42
+ }
@@ -6,8 +6,10 @@ import {
6
6
  } from "./usage-snapshot.js";
7
7
  import { asWsEventPayloadRecord, readWsEventTurnRecordId } from "./ws-event-payloads.js";
8
8
 
9
+ /** @internal */
9
10
  export type UsageSnapshotByTurnRecordId = Record<string, UsageSnapshot>;
10
11
 
12
+ /** @internal */
11
13
  export function buildUsageSnapshotsByTurnRecordId(
12
14
  events: readonly ProcessEvent[],
13
15
  ): UsageSnapshotByTurnRecordId {