@leitwerk-dev/protocol 0.1.0
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/LICENSE +13 -0
- package/dist/chunk-6L4W7WPV.js +1 -0
- package/dist/chunk-6L4W7WPV.js.map +1 -0
- package/dist/chunk-7PCFBAV4.js +740 -0
- package/dist/chunk-7PCFBAV4.js.map +1 -0
- package/dist/chunk-AAV3AAOF.js +1 -0
- package/dist/chunk-AAV3AAOF.js.map +1 -0
- package/dist/chunk-IEMT3JG4.js +1 -0
- package/dist/chunk-IEMT3JG4.js.map +1 -0
- package/dist/chunk-IWVU76M5.js +26 -0
- package/dist/chunk-IWVU76M5.js.map +1 -0
- package/dist/config-snapshot.d.ts +211 -0
- package/dist/config-snapshot.d.ts.map +1 -0
- package/dist/config-snapshot.js +2 -0
- package/dist/config-snapshot.js.map +1 -0
- package/dist/external-review.d.ts +8 -0
- package/dist/external-review.d.ts.map +1 -0
- package/dist/external-review.js +2 -0
- package/dist/external-review.js.map +1 -0
- package/dist/form-contract.d.ts +41 -0
- package/dist/form-contract.d.ts.map +1 -0
- package/dist/form-contract.js +2 -0
- package/dist/form-contract.js.map +1 -0
- package/dist/http-contracts-C8IXe8MU.d.ts +1483 -0
- package/dist/http-contracts.d.ts +959 -0
- package/dist/http-contracts.d.ts.map +1 -0
- package/dist/http-contracts.js +434 -0
- package/dist/http-contracts.js.map +1 -0
- package/dist/index.d.ts +21 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -0
- package/dist/index.js.map +1 -0
- package/dist/initial-prompt.d.ts +8 -0
- package/dist/initial-prompt.d.ts.map +1 -0
- package/dist/initial-prompt.js +103 -0
- package/dist/initial-prompt.js.map +1 -0
- package/dist/launcher-contract.d.ts +34 -0
- package/dist/launcher-contract.d.ts.map +1 -0
- package/dist/launcher-contract.js +2 -0
- package/dist/launcher-contract.js.map +1 -0
- package/dist/live-turn-projection.d.ts +39 -0
- package/dist/live-turn-projection.d.ts.map +1 -0
- package/dist/live-turn-projection.js +395 -0
- package/dist/live-turn-projection.js.map +1 -0
- package/dist/pi-session-message.d.ts +25 -0
- package/dist/pi-session-message.d.ts.map +1 -0
- package/dist/pi-session-message.js +27 -0
- package/dist/pi-session-message.js.map +1 -0
- package/dist/primary-path-snapshot.d.ts +77 -0
- package/dist/primary-path-snapshot.d.ts.map +1 -0
- package/dist/primary-path-snapshot.js +2 -0
- package/dist/primary-path-snapshot.js.map +1 -0
- package/dist/protocol.d.ts +428 -0
- package/dist/protocol.d.ts.map +1 -0
- package/dist/protocol.js +261 -0
- package/dist/protocol.js.map +1 -0
- package/dist/session-entry-tree.d.ts +15 -0
- package/dist/session-entry-tree.d.ts.map +1 -0
- package/dist/session-entry-tree.js +42 -0
- package/dist/session-entry-tree.js.map +1 -0
- package/dist/streamable-events.d.ts +4 -0
- package/dist/streamable-events.d.ts.map +1 -0
- package/dist/streamable-events.js +17 -0
- package/dist/streamable-events.js.map +1 -0
- package/dist/text-preview.d.ts +7 -0
- package/dist/text-preview.d.ts.map +1 -0
- package/dist/text-preview.js +23 -0
- package/dist/text-preview.js.map +1 -0
- package/dist/timeline-projection.d.ts +9 -0
- package/dist/timeline-projection.d.ts.map +1 -0
- package/dist/timeline-projection.js +30 -0
- package/dist/timeline-projection.js.map +1 -0
- package/dist/timestamp-ordering.d.ts +3 -0
- package/dist/timestamp-ordering.d.ts.map +1 -0
- package/dist/timestamp-ordering.js +15 -0
- package/dist/timestamp-ordering.js.map +1 -0
- package/dist/tool-renderer-contract.d.ts +18 -0
- package/dist/tool-renderer-contract.d.ts.map +1 -0
- package/dist/tool-renderer-contract.js +22 -0
- package/dist/tool-renderer-contract.js.map +1 -0
- package/dist/turn-continuation.d.ts +36 -0
- package/dist/turn-continuation.d.ts.map +1 -0
- package/dist/turn-continuation.js +179 -0
- package/dist/turn-continuation.js.map +1 -0
- package/dist/usage-by-turn-record.d.ts +5 -0
- package/dist/usage-by-turn-record.d.ts.map +1 -0
- package/dist/usage-by-turn-record.js +25 -0
- package/dist/usage-by-turn-record.js.map +1 -0
- package/dist/usage-snapshot.d.ts +25 -0
- package/dist/usage-snapshot.d.ts.map +1 -0
- package/dist/usage-snapshot.js +120 -0
- package/dist/usage-snapshot.js.map +1 -0
- package/dist/ws-event-payloads.d.ts +18 -0
- package/dist/ws-event-payloads.d.ts.map +1 -0
- package/dist/ws-event-payloads.js +47 -0
- package/dist/ws-event-payloads.js.map +1 -0
- package/package.json +65 -0
- package/src/config-snapshot.ts +244 -0
- package/src/external-review.ts +7 -0
- package/src/form-contract.ts +50 -0
- package/src/http-contracts.ts +1497 -0
- package/src/index.ts +20 -0
- package/src/initial-prompt.ts +127 -0
- package/src/launcher-contract.ts +43 -0
- package/src/live-turn-projection.ts +501 -0
- package/src/pi-session-message.ts +48 -0
- package/src/primary-path-snapshot.ts +98 -0
- package/src/protocol.ts +503 -0
- package/src/session-entry-tree.ts +63 -0
- package/src/streamable-events.ts +20 -0
- package/src/text-preview.ts +32 -0
- package/src/timeline-projection.ts +43 -0
- package/src/timestamp-ordering.ts +15 -0
- package/src/tool-renderer-contract.ts +41 -0
- package/src/turn-continuation.ts +298 -0
- package/src/usage-by-turn-record.ts +34 -0
- package/src/usage-snapshot.ts +174 -0
- package/src/ws-event-payloads.ts +73 -0
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
export interface PiSessionMessageLike {
|
|
2
|
+
role?: unknown;
|
|
3
|
+
content?: unknown;
|
|
4
|
+
details?: unknown;
|
|
5
|
+
toolCallId?: unknown;
|
|
6
|
+
toolName?: unknown;
|
|
7
|
+
isError?: unknown;
|
|
8
|
+
usage?: unknown;
|
|
9
|
+
}
|
|
10
|
+
|
|
11
|
+
export interface PiSessionMessageEntryLike {
|
|
12
|
+
type?: unknown;
|
|
13
|
+
message?: unknown;
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function isPiSessionMessageEntryType<TEntry extends { type?: unknown }>(
|
|
17
|
+
entry: TEntry | undefined,
|
|
18
|
+
): entry is TEntry & { type: "message" } {
|
|
19
|
+
return entry?.type === "message";
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export function isPiSessionMessageRecord(value: unknown): value is PiSessionMessageLike {
|
|
23
|
+
return typeof value === "object" && value !== null && !Array.isArray(value);
|
|
24
|
+
}
|
|
25
|
+
|
|
26
|
+
export function isPiSessionMessageEntryWithRecord<TEntry extends PiSessionMessageEntryLike>(
|
|
27
|
+
entry: TEntry | undefined,
|
|
28
|
+
): entry is TEntry & { type: "message"; message: NonNullable<TEntry["message"]> } {
|
|
29
|
+
return isPiSessionMessageEntryType(entry) && isPiSessionMessageRecord(entry.message);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
export function extractPiSessionMessageText(content: unknown): string {
|
|
33
|
+
if (typeof content === "string") {
|
|
34
|
+
return content;
|
|
35
|
+
}
|
|
36
|
+
if (!Array.isArray(content)) {
|
|
37
|
+
return "";
|
|
38
|
+
}
|
|
39
|
+
return content
|
|
40
|
+
.map((block) => {
|
|
41
|
+
if (typeof block !== "object" || block === null || Array.isArray(block)) {
|
|
42
|
+
return "";
|
|
43
|
+
}
|
|
44
|
+
const record = block as { type?: unknown; text?: unknown };
|
|
45
|
+
return record.type === "text" && typeof record.text === "string" ? record.text : "";
|
|
46
|
+
})
|
|
47
|
+
.join("");
|
|
48
|
+
}
|
|
@@ -0,0 +1,98 @@
|
|
|
1
|
+
import type {
|
|
2
|
+
ProcessSemanticEntryRefs,
|
|
3
|
+
ProcessTurnAnnotation,
|
|
4
|
+
ProcessTurnRecord,
|
|
5
|
+
SemanticEntryRef,
|
|
6
|
+
WorkerLease,
|
|
7
|
+
} from "@leitwerk-dev/domain";
|
|
8
|
+
import type { UsageSnapshot } from "./usage-snapshot.js";
|
|
9
|
+
|
|
10
|
+
export interface PrimaryPathEntrySnapshot extends Record<string, unknown> {
|
|
11
|
+
id: string;
|
|
12
|
+
parentId: string | null;
|
|
13
|
+
type: string;
|
|
14
|
+
timestamp: string;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
export interface PrimaryPathDetailRailSnapshot {
|
|
18
|
+
keyPoints: [];
|
|
19
|
+
futureTurns: [];
|
|
20
|
+
currentPosition: null;
|
|
21
|
+
}
|
|
22
|
+
|
|
23
|
+
export interface PrimaryPathStreamingAssistantSnapshot {
|
|
24
|
+
text: string;
|
|
25
|
+
thinking: string;
|
|
26
|
+
lastUpdatedAt: string | null;
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
export interface PrimaryPathThinkingTraceItemSnapshot {
|
|
30
|
+
kind: "thinking";
|
|
31
|
+
text: string;
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
export interface PrimaryPathToolCallTraceItemSnapshot {
|
|
35
|
+
kind: "tool_call";
|
|
36
|
+
toolCallId: string;
|
|
37
|
+
}
|
|
38
|
+
|
|
39
|
+
export type PrimaryPathOperationalTraceSeverity = "info" | "success" | "warning" | "error";
|
|
40
|
+
|
|
41
|
+
export interface PrimaryPathOperationalTraceItemSnapshot {
|
|
42
|
+
kind: "operational_event";
|
|
43
|
+
eventType: string;
|
|
44
|
+
severity: PrimaryPathOperationalTraceSeverity;
|
|
45
|
+
title: string;
|
|
46
|
+
message: string;
|
|
47
|
+
timestamp: string;
|
|
48
|
+
}
|
|
49
|
+
|
|
50
|
+
export type PrimaryPathTraceItemSnapshot =
|
|
51
|
+
| PrimaryPathThinkingTraceItemSnapshot
|
|
52
|
+
| PrimaryPathToolCallTraceItemSnapshot
|
|
53
|
+
| PrimaryPathOperationalTraceItemSnapshot;
|
|
54
|
+
|
|
55
|
+
export interface PrimaryPathToolCallSnapshot {
|
|
56
|
+
toolCallId: string;
|
|
57
|
+
toolName: string;
|
|
58
|
+
status: "running" | "completed";
|
|
59
|
+
startedAt: string;
|
|
60
|
+
completedAt: string | null;
|
|
61
|
+
arguments: Record<string, unknown> | null;
|
|
62
|
+
result: unknown;
|
|
63
|
+
isError: boolean;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type TurnUsageSnapshot = UsageSnapshot;
|
|
67
|
+
|
|
68
|
+
export interface PrimaryPathActiveTurnSnapshot {
|
|
69
|
+
turnRecordId: string;
|
|
70
|
+
turnId: string;
|
|
71
|
+
turnType: ProcessTurnRecord["turnType"];
|
|
72
|
+
pathType: ProcessTurnRecord["pathType"];
|
|
73
|
+
startedAt: string;
|
|
74
|
+
assistant: PrimaryPathStreamingAssistantSnapshot;
|
|
75
|
+
toolCalls: PrimaryPathToolCallSnapshot[];
|
|
76
|
+
traceItems: PrimaryPathTraceItemSnapshot[];
|
|
77
|
+
usage: TurnUsageSnapshot | null;
|
|
78
|
+
eventWindowTruncated: boolean;
|
|
79
|
+
}
|
|
80
|
+
|
|
81
|
+
export interface PrimaryPathTurnStateSnapshot {
|
|
82
|
+
currentTurnRecordId: string | null;
|
|
83
|
+
workerState: WorkerLease["state"] | null;
|
|
84
|
+
isStreaming: boolean;
|
|
85
|
+
activeTurn: PrimaryPathActiveTurnSnapshot | null;
|
|
86
|
+
}
|
|
87
|
+
|
|
88
|
+
export interface PrimaryPathSnapshot {
|
|
89
|
+
instanceId: string;
|
|
90
|
+
rebuiltAt: string;
|
|
91
|
+
primaryPathEntries: PrimaryPathEntrySnapshot[];
|
|
92
|
+
currentLeaf: SemanticEntryRef | null;
|
|
93
|
+
semanticEntryRefs: ProcessSemanticEntryRefs;
|
|
94
|
+
labels: Record<string, string>;
|
|
95
|
+
turnAnnotations: ProcessTurnAnnotation[];
|
|
96
|
+
detailRail: PrimaryPathDetailRailSnapshot;
|
|
97
|
+
turnState: PrimaryPathTurnStateSnapshot;
|
|
98
|
+
}
|
package/src/protocol.ts
ADDED
|
@@ -0,0 +1,503 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type FutureExecutionKind,
|
|
3
|
+
PROCESS_SEMANTIC_ENTRY_REF_KEYS,
|
|
4
|
+
PROCESS_TURN_RECORD_PATH_TYPES,
|
|
5
|
+
PROCESS_TURN_TYPES,
|
|
6
|
+
type ProcessInput,
|
|
7
|
+
type ProcessInstance,
|
|
8
|
+
type ProcessProject,
|
|
9
|
+
type WorkerLease,
|
|
10
|
+
} from "@leitwerk-dev/domain";
|
|
11
|
+
import * as v from "valibot";
|
|
12
|
+
import type { StreamableWorkerEventType } from "./streamable-events.js";
|
|
13
|
+
|
|
14
|
+
export type ParseResult<T> = { ok: true; value: T } | { ok: false; error: string };
|
|
15
|
+
|
|
16
|
+
export const ok = <T>(value: T): ParseResult<T> => ({ ok: true, value });
|
|
17
|
+
export const err = (error: string): ParseResult<never> => ({ ok: false, error });
|
|
18
|
+
export const unknownRecordSchema = v.custom<Record<string, unknown>>(
|
|
19
|
+
(value): value is Record<string, unknown> =>
|
|
20
|
+
typeof value === "object" && value !== null && !Array.isArray(value),
|
|
21
|
+
"must be an object",
|
|
22
|
+
);
|
|
23
|
+
export const nullableStringSchema = v.nullable(v.string());
|
|
24
|
+
export const optionalNullableStringSchema = v.optional(nullableStringSchema);
|
|
25
|
+
|
|
26
|
+
export function parseSchema<TSchema extends v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>>(
|
|
27
|
+
schema: TSchema,
|
|
28
|
+
value: unknown,
|
|
29
|
+
context: string,
|
|
30
|
+
): ParseResult<v.InferOutput<TSchema>> {
|
|
31
|
+
const parsed = v.safeParse(schema, value);
|
|
32
|
+
if (parsed.success) {
|
|
33
|
+
return ok(parsed.output);
|
|
34
|
+
}
|
|
35
|
+
const issue = parsed.issues?.[0];
|
|
36
|
+
const target = issue ? [context, v.getDotPath(issue)].filter(Boolean).join(".") : context;
|
|
37
|
+
return err(
|
|
38
|
+
issue?.message === "must be an object"
|
|
39
|
+
? `${target} must be an object`
|
|
40
|
+
: `${target} ${issue?.message ?? "is invalid"}`,
|
|
41
|
+
);
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
export function parseLiteral<T extends string>(
|
|
45
|
+
value: unknown,
|
|
46
|
+
allowed: readonly T[],
|
|
47
|
+
context: string,
|
|
48
|
+
): ParseResult<T> {
|
|
49
|
+
return v.is(v.picklist(allowed), value)
|
|
50
|
+
? ok(value as T)
|
|
51
|
+
: err(`${context} must be one of: ${allowed.join(", ")}`);
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
export const WS_PROTOCOL_VERSION = "leitwerk/ws/v1";
|
|
55
|
+
|
|
56
|
+
export const WS_PROCESS_TYPES = {
|
|
57
|
+
TOAST: "process.toast",
|
|
58
|
+
FUTURE_UPDATED: "future.updated",
|
|
59
|
+
} as const;
|
|
60
|
+
|
|
61
|
+
export const WS_PRIMARY_PATH_TYPES = {
|
|
62
|
+
TURN_STARTED: "primary_path.turn_started",
|
|
63
|
+
ASSISTANT_PARTIAL: "primary_path.assistant_partial",
|
|
64
|
+
USAGE_UPDATED: "primary_path.usage_updated",
|
|
65
|
+
ASSISTANT_COMMITTED: "primary_path.assistant_committed",
|
|
66
|
+
TOOL_CALL_STARTED: "primary_path.tool_call_started",
|
|
67
|
+
TOOL_CALL_COMPLETED: "primary_path.tool_call_completed",
|
|
68
|
+
TURN_ANNOTATION_CHANGED: "primary_path.turn_annotation_changed",
|
|
69
|
+
LABEL_CHANGED: "primary_path.label_changed",
|
|
70
|
+
CHANGED: "primary_path.changed",
|
|
71
|
+
} as const;
|
|
72
|
+
|
|
73
|
+
export const WS_PI_STREAM_TYPES = {
|
|
74
|
+
STARTED: "pi.stream.started",
|
|
75
|
+
DELTA: "pi.stream.delta",
|
|
76
|
+
COMPLETED: "pi.stream.completed",
|
|
77
|
+
TOOL_STARTED: "pi.tool.started",
|
|
78
|
+
TOOL_COMPLETED: "pi.tool.completed",
|
|
79
|
+
LABEL_CHANGED: "pi.label.changed",
|
|
80
|
+
ERROR: "pi.error",
|
|
81
|
+
RETRY_START: "pi.retry.start",
|
|
82
|
+
RETRY_END: "pi.retry.end",
|
|
83
|
+
USAGE: "pi.usage",
|
|
84
|
+
} as const;
|
|
85
|
+
|
|
86
|
+
type PiPayload<T = Record<string, never>> = {
|
|
87
|
+
turnId?: string | null;
|
|
88
|
+
turnRecordId?: string | null;
|
|
89
|
+
timestamp?: string;
|
|
90
|
+
} & T;
|
|
91
|
+
|
|
92
|
+
const finiteNumberSchema = v.pipe(v.number(), v.finite());
|
|
93
|
+
const semanticEntryRefSchema = v.object({
|
|
94
|
+
entryId: v.string(),
|
|
95
|
+
turnRecordId: nullableStringSchema,
|
|
96
|
+
});
|
|
97
|
+
const primaryPathBasePayloadEntries = {
|
|
98
|
+
turnRecordId: nullableStringSchema,
|
|
99
|
+
piTurnId: nullableStringSchema,
|
|
100
|
+
timestamp: v.string(),
|
|
101
|
+
};
|
|
102
|
+
const primaryPathTreeEntries = {
|
|
103
|
+
rootEntry: v.nullable(semanticEntryRefSchema),
|
|
104
|
+
currentLeaf: v.nullable(semanticEntryRefSchema),
|
|
105
|
+
};
|
|
106
|
+
const primaryPathTreeSchema = v.object(primaryPathTreeEntries);
|
|
107
|
+
const processTurnRecordSummarySchema = v.object({
|
|
108
|
+
id: v.string(),
|
|
109
|
+
turnId: v.string(),
|
|
110
|
+
turnType: v.picklist(PROCESS_TURN_TYPES),
|
|
111
|
+
pathType: v.picklist(PROCESS_TURN_RECORD_PATH_TYPES),
|
|
112
|
+
startedAt: v.string(),
|
|
113
|
+
});
|
|
114
|
+
const usageCostSnapshotSchema = v.object({
|
|
115
|
+
input: finiteNumberSchema,
|
|
116
|
+
output: finiteNumberSchema,
|
|
117
|
+
reasoning: v.optional(finiteNumberSchema),
|
|
118
|
+
cacheRead: finiteNumberSchema,
|
|
119
|
+
cacheWrite: finiteNumberSchema,
|
|
120
|
+
total: finiteNumberSchema,
|
|
121
|
+
});
|
|
122
|
+
const usageSnapshotSchema = v.object({
|
|
123
|
+
input: finiteNumberSchema,
|
|
124
|
+
output: finiteNumberSchema,
|
|
125
|
+
cacheRead: finiteNumberSchema,
|
|
126
|
+
cacheWrite: finiteNumberSchema,
|
|
127
|
+
totalTokens: finiteNumberSchema,
|
|
128
|
+
cost: v.nullable(usageCostSnapshotSchema),
|
|
129
|
+
});
|
|
130
|
+
const turnAnnotationReferenceSchema = v.variant("kind", [
|
|
131
|
+
v.object({
|
|
132
|
+
kind: v.literal("turn_record"),
|
|
133
|
+
turnRecordId: v.string(),
|
|
134
|
+
role: nullableStringSchema,
|
|
135
|
+
}),
|
|
136
|
+
v.object({
|
|
137
|
+
kind: v.literal("entry"),
|
|
138
|
+
entryId: v.string(),
|
|
139
|
+
role: nullableStringSchema,
|
|
140
|
+
}),
|
|
141
|
+
v.object({
|
|
142
|
+
kind: v.literal("semantic_entry_ref"),
|
|
143
|
+
ref: v.picklist(PROCESS_SEMANTIC_ENTRY_REF_KEYS),
|
|
144
|
+
role: nullableStringSchema,
|
|
145
|
+
}),
|
|
146
|
+
]);
|
|
147
|
+
const processTurnAnnotationSchema = v.object({
|
|
148
|
+
id: v.string(),
|
|
149
|
+
instanceId: v.string(),
|
|
150
|
+
annotationType: v.string(),
|
|
151
|
+
annotationKey: nullableStringSchema,
|
|
152
|
+
references: v.array(turnAnnotationReferenceSchema),
|
|
153
|
+
payload: unknownRecordSchema,
|
|
154
|
+
createdAt: v.string(),
|
|
155
|
+
updatedAt: v.string(),
|
|
156
|
+
});
|
|
157
|
+
const PRIMARY_PATH_PAYLOAD_SCHEMAS = {
|
|
158
|
+
[WS_PRIMARY_PATH_TYPES.TURN_STARTED]: v.object({
|
|
159
|
+
turnRecord: processTurnRecordSummarySchema,
|
|
160
|
+
}),
|
|
161
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_PARTIAL]: v.object({
|
|
162
|
+
...primaryPathBasePayloadEntries,
|
|
163
|
+
text: v.string(),
|
|
164
|
+
streamType: v.string(),
|
|
165
|
+
}),
|
|
166
|
+
[WS_PRIMARY_PATH_TYPES.USAGE_UPDATED]: v.object({
|
|
167
|
+
...primaryPathBasePayloadEntries,
|
|
168
|
+
usage: usageSnapshotSchema,
|
|
169
|
+
}),
|
|
170
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_COMMITTED]: v.object({
|
|
171
|
+
turnRecord: processTurnRecordSummarySchema,
|
|
172
|
+
...primaryPathTreeEntries,
|
|
173
|
+
}),
|
|
174
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_STARTED]: v.object({
|
|
175
|
+
...primaryPathBasePayloadEntries,
|
|
176
|
+
toolCallId: v.string(),
|
|
177
|
+
toolName: v.string(),
|
|
178
|
+
arguments: v.nullable(unknownRecordSchema),
|
|
179
|
+
}),
|
|
180
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_COMPLETED]: v.object({
|
|
181
|
+
...primaryPathBasePayloadEntries,
|
|
182
|
+
toolCallId: v.string(),
|
|
183
|
+
toolName: v.string(),
|
|
184
|
+
result: v.unknown(),
|
|
185
|
+
isError: v.boolean(),
|
|
186
|
+
}),
|
|
187
|
+
[WS_PRIMARY_PATH_TYPES.TURN_ANNOTATION_CHANGED]: v.object({
|
|
188
|
+
change: v.picklist(["created", "updated"] as const),
|
|
189
|
+
annotation: processTurnAnnotationSchema,
|
|
190
|
+
}),
|
|
191
|
+
[WS_PRIMARY_PATH_TYPES.LABEL_CHANGED]: v.object({
|
|
192
|
+
...primaryPathBasePayloadEntries,
|
|
193
|
+
targetId: nullableStringSchema,
|
|
194
|
+
label: nullableStringSchema,
|
|
195
|
+
}),
|
|
196
|
+
[WS_PRIMARY_PATH_TYPES.CHANGED]: primaryPathTreeSchema,
|
|
197
|
+
} as const satisfies Record<
|
|
198
|
+
(typeof WS_PRIMARY_PATH_TYPES)[keyof typeof WS_PRIMARY_PATH_TYPES],
|
|
199
|
+
v.BaseSchema<unknown, unknown, v.BaseIssue<unknown>>
|
|
200
|
+
>;
|
|
201
|
+
type PrimaryPathPayloadByType = {
|
|
202
|
+
[K in keyof typeof PRIMARY_PATH_PAYLOAD_SCHEMAS]: v.InferOutput<
|
|
203
|
+
(typeof PRIMARY_PATH_PAYLOAD_SCHEMAS)[K]
|
|
204
|
+
>;
|
|
205
|
+
};
|
|
206
|
+
type PrimaryPathTree = v.InferOutput<typeof primaryPathTreeSchema>;
|
|
207
|
+
type PiUsagePayload = PiPayload<{
|
|
208
|
+
input?: number;
|
|
209
|
+
output?: number;
|
|
210
|
+
cacheRead?: number;
|
|
211
|
+
cacheWrite?: number;
|
|
212
|
+
totalTokens?: number;
|
|
213
|
+
cost?: {
|
|
214
|
+
input?: number;
|
|
215
|
+
output?: number;
|
|
216
|
+
cacheRead?: number;
|
|
217
|
+
cacheWrite?: number;
|
|
218
|
+
total?: number;
|
|
219
|
+
};
|
|
220
|
+
cacheHitRate?: number;
|
|
221
|
+
}>;
|
|
222
|
+
|
|
223
|
+
export type WsDurability = "durable" | "ephemeral";
|
|
224
|
+
|
|
225
|
+
export type ProcessAttentionTarget =
|
|
226
|
+
| { kind: "action_required" }
|
|
227
|
+
| { kind: "turn_failed" }
|
|
228
|
+
| { kind: "worker_failed" }
|
|
229
|
+
| { kind: "question_request"; requestId: string };
|
|
230
|
+
|
|
231
|
+
export type WsPayloadByType = {
|
|
232
|
+
hello: { serverVersion: string };
|
|
233
|
+
pong: Record<string, never>;
|
|
234
|
+
"process.created": { process: ProcessInstance; processId: string; launcherId?: string };
|
|
235
|
+
"process.updated": { process: Partial<ProcessInstance>; changedFields?: readonly string[] };
|
|
236
|
+
"process.deleted": { instanceId: string };
|
|
237
|
+
"project.updated": {
|
|
238
|
+
projectId: string;
|
|
239
|
+
project: {
|
|
240
|
+
key: string;
|
|
241
|
+
repoLocator: string;
|
|
242
|
+
repoLocatorKind: ProcessProject["repoLocatorKind"];
|
|
243
|
+
branch: string;
|
|
244
|
+
baseBranch: string;
|
|
245
|
+
workBranch: string | null;
|
|
246
|
+
externalId: string | null;
|
|
247
|
+
externalUrl: string | null;
|
|
248
|
+
pipelineStatus: string | null;
|
|
249
|
+
};
|
|
250
|
+
};
|
|
251
|
+
"worker.state": {
|
|
252
|
+
worker: WorkerLease;
|
|
253
|
+
workerId: string;
|
|
254
|
+
state: WorkerLease["state"];
|
|
255
|
+
previousState: WorkerLease["state"];
|
|
256
|
+
reason: string;
|
|
257
|
+
};
|
|
258
|
+
"process.input.queued": { instanceId: string; input: ProcessInput };
|
|
259
|
+
"process.input.acknowledged": { instanceId: string; inputId: string; sequence: number };
|
|
260
|
+
"process.event": { eventType: string; level: string; message: string };
|
|
261
|
+
[WS_PROCESS_TYPES.TOAST]: {
|
|
262
|
+
instanceId: string;
|
|
263
|
+
level: "warn" | "error";
|
|
264
|
+
message: string;
|
|
265
|
+
eventType: string;
|
|
266
|
+
dedupeKey: string;
|
|
267
|
+
ttlMs: number;
|
|
268
|
+
/** Optional typed Chronicle target applied only when the operator opens the toast. */
|
|
269
|
+
focusTarget?: ProcessAttentionTarget;
|
|
270
|
+
};
|
|
271
|
+
[WS_PROCESS_TYPES.FUTURE_UPDATED]: {
|
|
272
|
+
futureExecutionId: string;
|
|
273
|
+
operation: "created" | "updated" | "deleted";
|
|
274
|
+
kind: FutureExecutionKind;
|
|
275
|
+
};
|
|
276
|
+
"plan.updated": { planRevision: number; reviewState: string; approved: boolean; summary: string };
|
|
277
|
+
"review.updated": {
|
|
278
|
+
hasIssues: boolean;
|
|
279
|
+
issueCount: number;
|
|
280
|
+
reviewMarkdown?: string;
|
|
281
|
+
nextTurnId: string | null;
|
|
282
|
+
};
|
|
283
|
+
[WS_PI_STREAM_TYPES.STARTED]: PiPayload;
|
|
284
|
+
[WS_PI_STREAM_TYPES.DELTA]: PiPayload<{
|
|
285
|
+
text?: string;
|
|
286
|
+
delta?: string;
|
|
287
|
+
chunk?: string;
|
|
288
|
+
streamType?: string;
|
|
289
|
+
}>;
|
|
290
|
+
[WS_PI_STREAM_TYPES.COMPLETED]: PiPayload;
|
|
291
|
+
[WS_PI_STREAM_TYPES.TOOL_STARTED]: PiPayload<{
|
|
292
|
+
toolCallId?: string;
|
|
293
|
+
toolName?: string;
|
|
294
|
+
name?: string;
|
|
295
|
+
arguments?: Record<string, unknown>;
|
|
296
|
+
args?: Record<string, unknown>;
|
|
297
|
+
}>;
|
|
298
|
+
[WS_PI_STREAM_TYPES.TOOL_COMPLETED]: PiPayload<{
|
|
299
|
+
toolCallId?: string;
|
|
300
|
+
toolName?: string;
|
|
301
|
+
name?: string;
|
|
302
|
+
result?: unknown;
|
|
303
|
+
isError?: boolean;
|
|
304
|
+
}>;
|
|
305
|
+
[WS_PI_STREAM_TYPES.LABEL_CHANGED]: PiPayload<{
|
|
306
|
+
targetId?: string | null;
|
|
307
|
+
label?: string | null;
|
|
308
|
+
}>;
|
|
309
|
+
[WS_PI_STREAM_TYPES.ERROR]: PiPayload<{
|
|
310
|
+
message?: string;
|
|
311
|
+
errorMessage?: string;
|
|
312
|
+
source?: string;
|
|
313
|
+
toolName?: string;
|
|
314
|
+
stopReason?: string;
|
|
315
|
+
provider?: string;
|
|
316
|
+
model?: string;
|
|
317
|
+
}>;
|
|
318
|
+
[WS_PI_STREAM_TYPES.RETRY_START]: PiPayload<{
|
|
319
|
+
attempt?: number;
|
|
320
|
+
maxAttempts?: number;
|
|
321
|
+
delayMs?: number;
|
|
322
|
+
errorMessage?: string;
|
|
323
|
+
message?: string;
|
|
324
|
+
}>;
|
|
325
|
+
[WS_PI_STREAM_TYPES.RETRY_END]: PiPayload<{
|
|
326
|
+
success?: boolean;
|
|
327
|
+
attempt?: number;
|
|
328
|
+
finalError?: string;
|
|
329
|
+
message?: string;
|
|
330
|
+
}>;
|
|
331
|
+
[WS_PI_STREAM_TYPES.USAGE]: PiUsagePayload;
|
|
332
|
+
[WS_PRIMARY_PATH_TYPES.TURN_STARTED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.TURN_STARTED];
|
|
333
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_PARTIAL]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.ASSISTANT_PARTIAL];
|
|
334
|
+
[WS_PRIMARY_PATH_TYPES.USAGE_UPDATED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.USAGE_UPDATED];
|
|
335
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_COMMITTED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.ASSISTANT_COMMITTED];
|
|
336
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_STARTED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.TOOL_CALL_STARTED];
|
|
337
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_COMPLETED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.TOOL_CALL_COMPLETED];
|
|
338
|
+
[WS_PRIMARY_PATH_TYPES.TURN_ANNOTATION_CHANGED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.TURN_ANNOTATION_CHANGED];
|
|
339
|
+
[WS_PRIMARY_PATH_TYPES.LABEL_CHANGED]: PrimaryPathPayloadByType[typeof WS_PRIMARY_PATH_TYPES.LABEL_CHANGED];
|
|
340
|
+
[WS_PRIMARY_PATH_TYPES.CHANGED]: PrimaryPathTree;
|
|
341
|
+
};
|
|
342
|
+
|
|
343
|
+
export const WS_FRAME_DURABILITY = {
|
|
344
|
+
hello: "ephemeral",
|
|
345
|
+
pong: "ephemeral",
|
|
346
|
+
"process.created": "durable",
|
|
347
|
+
"process.updated": "durable",
|
|
348
|
+
"process.deleted": "durable",
|
|
349
|
+
"project.updated": "durable",
|
|
350
|
+
"worker.state": "durable",
|
|
351
|
+
"process.input.queued": "durable",
|
|
352
|
+
"process.input.acknowledged": "durable",
|
|
353
|
+
"process.event": "durable",
|
|
354
|
+
[WS_PROCESS_TYPES.TOAST]: "ephemeral",
|
|
355
|
+
[WS_PROCESS_TYPES.FUTURE_UPDATED]: "durable",
|
|
356
|
+
"plan.updated": "durable",
|
|
357
|
+
"review.updated": "durable",
|
|
358
|
+
[WS_PI_STREAM_TYPES.STARTED]: "ephemeral",
|
|
359
|
+
[WS_PI_STREAM_TYPES.DELTA]: "ephemeral",
|
|
360
|
+
[WS_PI_STREAM_TYPES.COMPLETED]: "ephemeral",
|
|
361
|
+
[WS_PI_STREAM_TYPES.TOOL_STARTED]: "ephemeral",
|
|
362
|
+
[WS_PI_STREAM_TYPES.TOOL_COMPLETED]: "ephemeral",
|
|
363
|
+
[WS_PI_STREAM_TYPES.LABEL_CHANGED]: "ephemeral",
|
|
364
|
+
[WS_PI_STREAM_TYPES.ERROR]: "ephemeral",
|
|
365
|
+
[WS_PI_STREAM_TYPES.RETRY_START]: "ephemeral",
|
|
366
|
+
[WS_PI_STREAM_TYPES.RETRY_END]: "ephemeral",
|
|
367
|
+
[WS_PI_STREAM_TYPES.USAGE]: "ephemeral",
|
|
368
|
+
[WS_PRIMARY_PATH_TYPES.TURN_STARTED]: "durable",
|
|
369
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_PARTIAL]: "ephemeral",
|
|
370
|
+
[WS_PRIMARY_PATH_TYPES.USAGE_UPDATED]: "ephemeral",
|
|
371
|
+
[WS_PRIMARY_PATH_TYPES.ASSISTANT_COMMITTED]: "durable",
|
|
372
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_STARTED]: "ephemeral",
|
|
373
|
+
[WS_PRIMARY_PATH_TYPES.TOOL_CALL_COMPLETED]: "ephemeral",
|
|
374
|
+
[WS_PRIMARY_PATH_TYPES.TURN_ANNOTATION_CHANGED]: "durable",
|
|
375
|
+
[WS_PRIMARY_PATH_TYPES.LABEL_CHANGED]: "durable",
|
|
376
|
+
[WS_PRIMARY_PATH_TYPES.CHANGED]: "durable",
|
|
377
|
+
} as const satisfies Record<keyof WsPayloadByType, WsDurability>;
|
|
378
|
+
|
|
379
|
+
export type WsDurabilityByType = typeof WS_FRAME_DURABILITY;
|
|
380
|
+
export type KnownWsFrameType = keyof WsPayloadByType;
|
|
381
|
+
export type KnownDurableWsFrameType = {
|
|
382
|
+
[K in KnownWsFrameType]: WsDurabilityByType[K] extends "durable" ? K : never;
|
|
383
|
+
}[KnownWsFrameType];
|
|
384
|
+
export type KnownEphemeralWsFrameType = {
|
|
385
|
+
[K in KnownWsFrameType]: WsDurabilityByType[K] extends "ephemeral" ? K : never;
|
|
386
|
+
}[KnownWsFrameType];
|
|
387
|
+
export type WsFrameOfType<T extends KnownWsFrameType> = {
|
|
388
|
+
protocol: typeof WS_PROTOCOL_VERSION;
|
|
389
|
+
type: T;
|
|
390
|
+
durability: WsDurabilityByType[T];
|
|
391
|
+
sentAt: string;
|
|
392
|
+
instanceId?: string;
|
|
393
|
+
payload: WsPayloadByType[T];
|
|
394
|
+
};
|
|
395
|
+
export type DurableWsFrameInput = {
|
|
396
|
+
[K in KnownDurableWsFrameType]: { type: K; instanceId?: string; payload: WsPayloadByType[K] };
|
|
397
|
+
}[KnownDurableWsFrameType];
|
|
398
|
+
export type EphemeralWsFrameInput = {
|
|
399
|
+
[K in KnownEphemeralWsFrameType]: { type: K; instanceId?: string; payload: WsPayloadByType[K] };
|
|
400
|
+
}[KnownEphemeralWsFrameType];
|
|
401
|
+
export type WsFrame = { [K in KnownWsFrameType]: WsFrameOfType<K> }[KnownWsFrameType];
|
|
402
|
+
export type PrimaryPathWsFrameType =
|
|
403
|
+
(typeof WS_PRIMARY_PATH_TYPES)[keyof typeof WS_PRIMARY_PATH_TYPES];
|
|
404
|
+
export type PrimaryPathWsFrame = Extract<WsFrame, { type: PrimaryPathWsFrameType }>;
|
|
405
|
+
|
|
406
|
+
const KNOWN_WS_FRAME_TYPES = Object.keys(WS_FRAME_DURABILITY) as KnownWsFrameType[];
|
|
407
|
+
const PRIMARY_PATH_WS_FRAME_TYPES = Object.values(
|
|
408
|
+
WS_PRIMARY_PATH_TYPES,
|
|
409
|
+
) as PrimaryPathWsFrameType[];
|
|
410
|
+
const WS_FRAME_SCHEMA = v.looseObject({
|
|
411
|
+
protocol: v.literal(WS_PROTOCOL_VERSION),
|
|
412
|
+
type: v.picklist(KNOWN_WS_FRAME_TYPES),
|
|
413
|
+
durability: v.picklist(["durable", "ephemeral"] as const),
|
|
414
|
+
sentAt: v.string(),
|
|
415
|
+
instanceId: v.optional(v.string()),
|
|
416
|
+
payload: unknownRecordSchema,
|
|
417
|
+
});
|
|
418
|
+
|
|
419
|
+
export function parsePrimaryPathWsFrameInput(value: unknown): ParseResult<PrimaryPathWsFrame> {
|
|
420
|
+
const frame = parseWsFrame(value);
|
|
421
|
+
if (!frame.ok) {
|
|
422
|
+
return frame;
|
|
423
|
+
}
|
|
424
|
+
const type = parseLiteral(
|
|
425
|
+
frame.value.type,
|
|
426
|
+
PRIMARY_PATH_WS_FRAME_TYPES,
|
|
427
|
+
"primary_path ws frame.type",
|
|
428
|
+
);
|
|
429
|
+
if (!type.ok) {
|
|
430
|
+
return type;
|
|
431
|
+
}
|
|
432
|
+
const payload = parseSchema(
|
|
433
|
+
PRIMARY_PATH_PAYLOAD_SCHEMAS[type.value],
|
|
434
|
+
frame.value.payload,
|
|
435
|
+
`primary_path ws frame.payload (${type.value})`,
|
|
436
|
+
);
|
|
437
|
+
return payload.ok
|
|
438
|
+
? ok({ ...frame.value, type: type.value, payload: payload.value } as PrimaryPathWsFrame)
|
|
439
|
+
: payload;
|
|
440
|
+
}
|
|
441
|
+
|
|
442
|
+
export function parseWsFrame(value: unknown): ParseResult<WsFrame> {
|
|
443
|
+
const frame = parseSchema(WS_FRAME_SCHEMA, value, "ws frame");
|
|
444
|
+
if (!frame.ok) {
|
|
445
|
+
return frame;
|
|
446
|
+
}
|
|
447
|
+
return frame.value.durability !== WS_FRAME_DURABILITY[frame.value.type]
|
|
448
|
+
? err(
|
|
449
|
+
`ws frame.durability must be '${WS_FRAME_DURABILITY[frame.value.type]}' for type '${frame.value.type}'`,
|
|
450
|
+
)
|
|
451
|
+
: ok(frame.value as WsFrame);
|
|
452
|
+
}
|
|
453
|
+
|
|
454
|
+
export function createDurableWsFrame(input: DurableWsFrameInput & { sentAt?: string }): WsFrame {
|
|
455
|
+
return {
|
|
456
|
+
protocol: WS_PROTOCOL_VERSION,
|
|
457
|
+
type: input.type,
|
|
458
|
+
durability: "durable",
|
|
459
|
+
sentAt: input.sentAt ?? new Date().toISOString(),
|
|
460
|
+
...(input.instanceId ? { instanceId: input.instanceId } : {}),
|
|
461
|
+
payload: input.payload,
|
|
462
|
+
} as WsFrame;
|
|
463
|
+
}
|
|
464
|
+
|
|
465
|
+
export function createEphemeralWsFrame(input: {
|
|
466
|
+
type: KnownEphemeralWsFrameType;
|
|
467
|
+
instanceId?: string;
|
|
468
|
+
payload: WsPayloadByType[KnownEphemeralWsFrameType];
|
|
469
|
+
sentAt?: string;
|
|
470
|
+
}): WsFrame {
|
|
471
|
+
return {
|
|
472
|
+
protocol: WS_PROTOCOL_VERSION,
|
|
473
|
+
type: input.type,
|
|
474
|
+
durability: "ephemeral",
|
|
475
|
+
sentAt: input.sentAt ?? new Date().toISOString(),
|
|
476
|
+
...(input.instanceId ? { instanceId: input.instanceId } : {}),
|
|
477
|
+
payload: input.payload,
|
|
478
|
+
} as WsFrame;
|
|
479
|
+
}
|
|
480
|
+
|
|
481
|
+
const WORKER_TO_WS_TYPE: Record<StreamableWorkerEventType, string> = {
|
|
482
|
+
"pi.stream.delta": WS_PI_STREAM_TYPES.DELTA,
|
|
483
|
+
"pi.turn.start": WS_PI_STREAM_TYPES.STARTED,
|
|
484
|
+
"pi.turn.end": WS_PI_STREAM_TYPES.COMPLETED,
|
|
485
|
+
"pi.tool.call": WS_PI_STREAM_TYPES.TOOL_STARTED,
|
|
486
|
+
"pi.tool.result": WS_PI_STREAM_TYPES.TOOL_COMPLETED,
|
|
487
|
+
"pi.label.changed": WS_PI_STREAM_TYPES.LABEL_CHANGED,
|
|
488
|
+
"pi.error": WS_PI_STREAM_TYPES.ERROR,
|
|
489
|
+
"pi.retry.start": WS_PI_STREAM_TYPES.RETRY_START,
|
|
490
|
+
"pi.retry.end": WS_PI_STREAM_TYPES.RETRY_END,
|
|
491
|
+
"pi.usage": WS_PI_STREAM_TYPES.USAGE,
|
|
492
|
+
};
|
|
493
|
+
|
|
494
|
+
export type RawPiDiagnosticWsFrameType =
|
|
495
|
+
(typeof WS_PI_STREAM_TYPES)[keyof typeof WS_PI_STREAM_TYPES];
|
|
496
|
+
|
|
497
|
+
export function mapWorkerEventToWsType(workerEventType: string): RawPiDiagnosticWsFrameType | null {
|
|
498
|
+
return (
|
|
499
|
+
((WORKER_TO_WS_TYPE as Partial<Record<string, string>>)[workerEventType] as
|
|
500
|
+
| RawPiDiagnosticWsFrameType
|
|
501
|
+
| undefined) ?? null
|
|
502
|
+
);
|
|
503
|
+
}
|
|
@@ -0,0 +1,63 @@
|
|
|
1
|
+
export interface SessionTreeEntryLike {
|
|
2
|
+
id: string;
|
|
3
|
+
parentId: string | null;
|
|
4
|
+
timestamp: string;
|
|
5
|
+
type: string;
|
|
6
|
+
}
|
|
7
|
+
|
|
8
|
+
export interface ReadonlyEntryTree<TEntry extends SessionTreeEntryLike> {
|
|
9
|
+
entries: TEntry[];
|
|
10
|
+
leafId: string | null;
|
|
11
|
+
getEntry(id: string): TEntry | undefined;
|
|
12
|
+
getChildren(parentId: string): TEntry[];
|
|
13
|
+
getBranch(fromId?: string | null): TEntry[];
|
|
14
|
+
}
|
|
15
|
+
|
|
16
|
+
export function createReadonlyEntryTree<TEntry extends SessionTreeEntryLike>(
|
|
17
|
+
entries: readonly TEntry[],
|
|
18
|
+
): ReadonlyEntryTree<TEntry> {
|
|
19
|
+
const orderedEntries = [...entries];
|
|
20
|
+
const entriesById = new Map(orderedEntries.map((entry) => [entry.id, entry]));
|
|
21
|
+
const childrenByParentId = new Map<string, TEntry[]>();
|
|
22
|
+
|
|
23
|
+
for (const entry of orderedEntries) {
|
|
24
|
+
if (typeof entry.parentId !== "string" || entry.parentId.trim() === "") {
|
|
25
|
+
continue;
|
|
26
|
+
}
|
|
27
|
+
const siblings = childrenByParentId.get(entry.parentId) ?? [];
|
|
28
|
+
siblings.push(entry);
|
|
29
|
+
childrenByParentId.set(entry.parentId, siblings);
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
const leafId = orderedEntries.at(-1)?.id ?? null;
|
|
33
|
+
|
|
34
|
+
const getEntry = (id: string): TEntry | undefined => entriesById.get(id);
|
|
35
|
+
const getChildren = (parentId: string): TEntry[] =>
|
|
36
|
+
(childrenByParentId.get(parentId) ?? []).slice();
|
|
37
|
+
const getBranch = (fromId?: string | null): TEntry[] => {
|
|
38
|
+
const resolvedFromId = fromId ?? leafId ?? undefined;
|
|
39
|
+
if (!resolvedFromId) {
|
|
40
|
+
return [];
|
|
41
|
+
}
|
|
42
|
+
const branch: TEntry[] = [];
|
|
43
|
+
const visited = new Set<string>();
|
|
44
|
+
let currentEntry = entriesById.get(resolvedFromId);
|
|
45
|
+
while (currentEntry) {
|
|
46
|
+
if (visited.has(currentEntry.id)) {
|
|
47
|
+
break;
|
|
48
|
+
}
|
|
49
|
+
visited.add(currentEntry.id);
|
|
50
|
+
branch.unshift(currentEntry);
|
|
51
|
+
currentEntry = currentEntry.parentId ? entriesById.get(currentEntry.parentId) : undefined;
|
|
52
|
+
}
|
|
53
|
+
return branch;
|
|
54
|
+
};
|
|
55
|
+
|
|
56
|
+
return {
|
|
57
|
+
entries: orderedEntries,
|
|
58
|
+
leafId,
|
|
59
|
+
getEntry,
|
|
60
|
+
getChildren,
|
|
61
|
+
getBranch,
|
|
62
|
+
};
|
|
63
|
+
}
|