@peasant-labs/schema 0.53.0 → 0.57.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.
@@ -0,0 +1,3 @@
1
+ export declare function isTranscriptDirectoryPositionsValid(directory: unknown): boolean;
2
+ /** Project a positioned context and refs into the shared conversation-position rule. */
3
+ export declare function isPositionedDirectoryPositionsValid(directory: unknown): boolean;
@@ -0,0 +1,89 @@
1
+ import { zConversationTurnSpan } from "./internal/generated/contract/zod.gen.js";
2
+ // Where a decoded segment-directory response places each segment among the
3
+ // session's conversation turns. This mirrors Go's
4
+ // validateDirectoryConversationTurns, which applies four rules: both counts of
5
+ // a stated span are non-negative, which the generated span shape already
6
+ // enforces; the total is a count; a span ends at or before it; and immediate
7
+ // neighbours that both state a span must meet exactly (an omitted span breaks
8
+ // the chain).
9
+ //
10
+ // When the shared position rule changes, both implementations change in the
11
+ // same PR, and the shared fixtures detect disagreement only for the cases they
12
+ // cover.
13
+ //
14
+ // The browser is deliberately stricter than Go's struct validation in three
15
+ // places, following the generated wire shape: an explicit null, a number
16
+ // outside the safe-integer domain, and a span missing before or count are all
17
+ // unusable here where Go's non-pointer int fields would read zero. The helper
18
+ // also sees JavaScript numbers after JSON parsing, so it cannot detect a number
19
+ // lexeme that already rounded to a safe integer; this is not raw-token parity.
20
+ //
21
+ // false means the optional positions are unusable and a consumer should drop
22
+ // them. It is not a rejection of the directory or its turns. true is not an
23
+ // attestation of required fields, authorization, other pages, or a future
24
+ // negotiated representation.
25
+ function isRecord(value) {
26
+ return typeof value === "object" && value !== null && !Array.isArray(value);
27
+ }
28
+ function isNonNegativeSafeInteger(value) {
29
+ return typeof value === "number" && Number.isSafeInteger(value) && value >= 0;
30
+ }
31
+ // The generated ConversationTurnSpan shape is the only accepted span: both
32
+ // counts present, integral, and inside 0..9007199254740991.
33
+ function parseSpan(value) {
34
+ const parsed = zConversationTurnSpan.safeParse(value);
35
+ return parsed.success ? parsed.data : undefined;
36
+ }
37
+ export function isTranscriptDirectoryPositionsValid(directory) {
38
+ if (!isRecord(directory))
39
+ return false;
40
+ const segments = directory.segments;
41
+ if (!Array.isArray(segments))
42
+ return false;
43
+ const statedTotal = directory.totalConversationTurns;
44
+ if (statedTotal !== undefined && !isNonNegativeSafeInteger(statedTotal))
45
+ return false;
46
+ // Undefined means the previous ref stated no span, so the chain of
47
+ // comparisons stops there instead of bridging to the next stated span.
48
+ let previous;
49
+ for (const ref of segments) {
50
+ if (!isRecord(ref))
51
+ return false;
52
+ const stated = ref.conversationTurns;
53
+ let span;
54
+ if (stated !== undefined) {
55
+ span = parseSpan(stated);
56
+ if (span === undefined)
57
+ return false;
58
+ if (statedTotal !== undefined && !endsAtOrBefore(span, statedTotal))
59
+ return false;
60
+ }
61
+ if (span !== undefined && previous !== undefined && !continues(previous, span))
62
+ return false;
63
+ previous = span;
64
+ }
65
+ return true;
66
+ }
67
+ /** Project a positioned context and refs into the shared conversation-position rule. */
68
+ export function isPositionedDirectoryPositionsValid(directory) {
69
+ if (!isRecord(directory))
70
+ return false;
71
+ const context = directory.context;
72
+ if (!isRecord(context))
73
+ return false;
74
+ return isTranscriptDirectoryPositionsValid({
75
+ totalConversationTurns: context.totalConversationTurns,
76
+ segments: directory.segments,
77
+ });
78
+ }
79
+ // before + count can pass Number.MAX_SAFE_INTEGER and round, so compare by an
80
+ // exact difference instead of the sum. Both operands are safe integers, so
81
+ // total - count is exact.
82
+ function endsAtOrBefore(span, total) {
83
+ return span.count <= total && span.before <= total - span.count;
84
+ }
85
+ // The exact difference between two safe integers is exact, so this is the
86
+ // strict next.before === previous.before + previous.count without the sum.
87
+ function continues(previous, next) {
88
+ return next.before - previous.before === previous.count;
89
+ }
@@ -0,0 +1,17 @@
1
+ import type { TurnDetail } from "./internal/generated/contract/zod.gen.js";
2
+ /**
3
+ * The strict body's typed view. A served legacy body may omit any member, and
4
+ * index is never an address: use the row's recordedIndex and
5
+ * recordedIndexState instead. Read every other member defensively.
6
+ *
7
+ * This view preserves values, not JSON bytes. Re-encoding may change whitespace,
8
+ * number lexemes, or escapes; the wire's byte guarantee belongs to the server.
9
+ */
10
+ export type TranscriptServedTurn = Omit<TurnDetail, "index"> & {
11
+ readonly index?: unknown;
12
+ };
13
+ /**
14
+ * Read a server-validated served turn. This checks object-ness only, not the
15
+ * members: legacy turns need not have the strict shape. Non-objects are refused.
16
+ */
17
+ export declare function readTranscriptServedTurn(value: unknown): TranscriptServedTurn | undefined;
@@ -0,0 +1,11 @@
1
+ /**
2
+ * Read a server-validated served turn. This checks object-ness only, not the
3
+ * members: legacy turns need not have the strict shape. Non-objects are refused.
4
+ */
5
+ export function readTranscriptServedTurn(value) {
6
+ if (typeof value !== "object" || value === null || Array.isArray(value))
7
+ return undefined;
8
+ // The strict view describes the contract, not a guarantee about legacy
9
+ // members. Consumers must inspect every member before using it.
10
+ return value;
11
+ }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@peasant-labs/schema",
3
- "version": "0.53.0",
3
+ "version": "0.57.0",
4
4
  "description": "Generated TypeScript contract bindings for the peasant-labs schema module",
5
5
  "license": "Apache-2.0",
6
6
  "repository": {