@theoremjs/web 0.3.5-next.949.gac4d2f6 → 0.3.5-next.951.gc40a24e

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/client/index.d.ts CHANGED
@@ -24,7 +24,7 @@ export { isOAuthComplete, notifyOAuthComplete } from './oauth-popup.ts';
24
24
  export { type PageDispatchResult, withPageToolDispatch } from './page-dispatch.ts';
25
25
  export { sessionAfter, stampWorked } from './run-commit.ts';
26
26
  export { keepsConversation, type RunOutcome, resumeInterfaceTool, type StreamView, streamInterfaceDraftTurn, streamInterfaceTurn, } from './run-session.ts';
27
- export { blankRow, type JsonSchema, missingFields, type SchemaControl, type SchemaField, sampleFromSchema, schemaControl, schemaFields, withField, } from './schema-fields.ts';
27
+ export { blankRow, branchField, branchSample, branchValue, chosenBranch, inBranch, type JsonSchema, missingFields, type SchemaBranch, type SchemaControl, type SchemaField, sampleFromSchema, schemaControl, schemaFields, withField, } from './schema-fields.ts';
28
28
  export { fieldLabel, fieldReading, humanize, isPart, isPlain, isProse, isRow, json, type ListRow, layoutChars, MAX_ROWS, type PlainReading, plainReading, type Row, rowHeading, type Section, type Shape, shapeOf, splitFields, unpacked, withUnit, } from './shaped-data.ts';
29
29
  export { citationsFromBlock, type SourceCitation, type SourceCitationBlock, } from './source-citations.ts';
30
30
  export type { QueryMatch } from './space-geometry.ts';
package/client/index.js CHANGED
@@ -20,7 +20,7 @@ export { isOAuthComplete, notifyOAuthComplete } from "./oauth-popup.js";
20
20
  export { withPageToolDispatch } from "./page-dispatch.js";
21
21
  export { sessionAfter, stampWorked } from "./run-commit.js";
22
22
  export { keepsConversation, resumeInterfaceTool, streamInterfaceDraftTurn, streamInterfaceTurn, } from "./run-session.js";
23
- export { blankRow, missingFields, sampleFromSchema, schemaControl, schemaFields, withField, } from "./schema-fields.js";
23
+ export { blankRow, branchField, branchSample, branchValue, chosenBranch, inBranch, missingFields, sampleFromSchema, schemaControl, schemaFields, withField, } from "./schema-fields.js";
24
24
  export { fieldLabel, fieldReading, humanize, isPart, isPlain, isProse, isRow, json, layoutChars, MAX_ROWS, plainReading, rowHeading, shapeOf, splitFields, unpacked, withUnit, } from "./shaped-data.js";
25
25
  export { citationsFromBlock, } from "./source-citations.js";
26
26
  export { DOT_FLOATS, FRAME_SLOT, queryGeometry, SEGMENT_FLOATS, spaceGeometry, } from "./space-geometry.js";
@@ -39,18 +39,45 @@ export type SchemaControl = {
39
39
  } | {
40
40
  kind: 'group';
41
41
  schema: JsonSchema;
42
+ } | {
43
+ kind: 'branches';
44
+ branches: SchemaBranch[];
45
+ isMenu: boolean;
42
46
  } | {
43
47
  kind: 'json';
44
48
  };
49
+ /**
50
+ * One of the shapes a field of several (`anyOf` or `oneOf`) can take, and how its value shows
51
+ * which: `key` holds the constant `is`, or, with no `is`, the object holds `key` alone.
52
+ */
53
+ export type SchemaBranch = {
54
+ label: string;
55
+ key: string;
56
+ is?: string | number | boolean;
57
+ /** What the form asks for in this shape: the rest of the object, or the value under `key`. */
58
+ body: JsonSchema;
59
+ };
45
60
  export type SchemaField = {
46
61
  key: string;
47
62
  label: string;
48
63
  description?: string;
49
64
  isRequired: boolean;
65
+ /** The field also takes `null`, which the form sends only when asked: left out stays left out. */
66
+ isNullable?: true;
50
67
  control: SchemaControl;
51
68
  };
69
+ /** The shape `value` is in, if it is in one. */
70
+ export declare function chosenBranch(branches: readonly SchemaBranch[], value: unknown): SchemaBranch | undefined;
71
+ /** The part of `value` a shape's body edits. */
72
+ export declare function branchValue(branch: SchemaBranch, value: unknown): unknown;
73
+ /** The value in `branch` with `body` as its body. */
74
+ export declare function inBranch(branch: SchemaBranch, body: unknown): Record<string, unknown>;
75
+ /** A shape's body as a field of its own, labelled as the shape. */
76
+ export declare function branchField(branch: SchemaBranch): SchemaField;
77
+ /** A value in `branch`, its body at its sample. */
78
+ export declare function branchSample(branch: SchemaBranch): Record<string, unknown>;
52
79
  /** The input a field's schema asks for. */
53
- export declare function schemaControl(prop: JsonSchema): SchemaControl;
80
+ export declare function schemaControl(field: JsonSchema): SchemaControl;
54
81
  /** An object schema's fields in the order it declares them, each with its input. */
55
82
  export declare function schemaFields(schema: JsonSchema): SchemaField[];
56
83
  /** Required fields, and optional ones the schema gives an example for; a plain value of the field's type when the schema declares no sample. */
@@ -37,6 +37,130 @@ function enumOf(prop) {
37
37
  ? options
38
38
  : undefined;
39
39
  }
40
+ function optionsOf(prop) {
41
+ const options = Array.isArray(prop.anyOf)
42
+ ? prop.anyOf
43
+ : Array.isArray(prop.oneOf)
44
+ ? prop.oneOf
45
+ : undefined;
46
+ return options?.filter(isRow);
47
+ }
48
+ const isNullSchema = (prop) => prop.type === 'null';
49
+ /** The field's schema without its `null` option, and whether it has one. */
50
+ function withoutNull(prop) {
51
+ if (Array.isArray(prop.type) && prop.type.includes('null')) {
52
+ return { schema: prop, isNullable: true };
53
+ }
54
+ const options = optionsOf(prop);
55
+ if (!options?.some(isNullSchema))
56
+ return { schema: prop, isNullable: false };
57
+ const rest = options.filter((option) => !isNullSchema(option));
58
+ const { anyOf: _anyOf, oneOf: _oneOf, ...own } = prop;
59
+ const [only] = rest;
60
+ return {
61
+ schema: rest.length === 1 && only ? { ...only, ...own } : { ...own, anyOf: rest },
62
+ isNullable: true,
63
+ };
64
+ }
65
+ function constOf(prop) {
66
+ if (!isRow(prop))
67
+ return undefined;
68
+ const value = 'const' in prop
69
+ ? prop.const
70
+ : Array.isArray(prop.enum) && prop.enum.length === 1
71
+ ? prop.enum[0]
72
+ : undefined;
73
+ return typeof value === 'string' || typeof value === 'number' || typeof value === 'boolean'
74
+ ? value
75
+ : undefined;
76
+ }
77
+ function titleOf(prop) {
78
+ return typeof prop.title === 'string' && prop.title ? prop.title : undefined;
79
+ }
80
+ const isDistinct = (values) => new Set(values).size === values.length;
81
+ /** Shapes told apart by a property each holds a different constant in. */
82
+ function taggedBranches(options) {
83
+ const shapes = options.map((option) => option.properties);
84
+ for (const key of Object.keys(shapes[0] ?? {})) {
85
+ const tags = shapes.map((shape) => constOf(shape[key]));
86
+ if (!isDistinct(tags))
87
+ continue;
88
+ const branches = options.map((option, at) => {
89
+ const is = tags[at];
90
+ if (is === undefined)
91
+ return undefined;
92
+ const { [key]: _tag, ...rest } = shapes[at] ?? {};
93
+ const required = Array.isArray(option.required)
94
+ ? { required: option.required.filter((name) => name !== key) }
95
+ : {};
96
+ return {
97
+ label: titleOf(option) ?? humanize(String(is)),
98
+ key,
99
+ is,
100
+ body: { ...option, properties: rest, ...required },
101
+ };
102
+ });
103
+ if (branches.every((branch) => branch !== undefined))
104
+ return branches;
105
+ }
106
+ return undefined;
107
+ }
108
+ /** Shapes that each hold one key of their own: the key says which. */
109
+ function keyedBranches(options) {
110
+ const branches = options.map((option) => {
111
+ const entries = Object.entries(option.properties);
112
+ const [entry] = entries;
113
+ if (entries.length !== 1 || !entry || !isRow(entry[1]))
114
+ return undefined;
115
+ const [key, body] = entry;
116
+ return { label: titleOf(option) ?? humanize(key), key, body };
117
+ });
118
+ if (!branches.every((branch) => branch !== undefined))
119
+ return undefined;
120
+ return isDistinct(branches.map((branch) => branch.key)) ? branches : undefined;
121
+ }
122
+ /** The shapes of a field of several objects, when its value shows which one it is in. */
123
+ function branchesOf(prop) {
124
+ const options = optionsOf(prop);
125
+ if (!options || options.length < 2 || !options.every((option) => isRow(option.properties))) {
126
+ return undefined;
127
+ }
128
+ return taggedBranches(options) ?? keyedBranches(options);
129
+ }
130
+ /** The shape `value` is in, if it is in one. */
131
+ export function chosenBranch(branches, value) {
132
+ if (!isRow(value))
133
+ return undefined;
134
+ return branches.find((branch) => branch.is === undefined ? Object.hasOwn(value, branch.key) : value[branch.key] === branch.is);
135
+ }
136
+ /** The part of `value` a shape's body edits. */
137
+ export function branchValue(branch, value) {
138
+ if (!isRow(value))
139
+ return undefined;
140
+ if (branch.is === undefined)
141
+ return value[branch.key];
142
+ const { [branch.key]: _tag, ...rest } = value;
143
+ return rest;
144
+ }
145
+ /** The value in `branch` with `body` as its body. */
146
+ export function inBranch(branch, body) {
147
+ if (branch.is === undefined)
148
+ return { [branch.key]: body };
149
+ return { ...(isRow(body) ? body : {}), [branch.key]: branch.is };
150
+ }
151
+ /** A shape's body as a field of its own, labelled as the shape. */
152
+ export function branchField(branch) {
153
+ return {
154
+ key: branch.key,
155
+ label: branch.label,
156
+ isRequired: true,
157
+ control: branch.is === undefined ? schemaControl(branch.body) : { kind: 'group', schema: branch.body },
158
+ };
159
+ }
160
+ /** A value in `branch`, its body at its sample. */
161
+ export function branchSample(branch) {
162
+ return inBranch(branch, sampleValue(branch.body));
163
+ }
40
164
  function numberAt(prop, key) {
41
165
  const value = prop[key];
42
166
  return typeof value === 'number' && Number.isFinite(value) ? value : undefined;
@@ -80,10 +204,14 @@ function listControl(prop) {
80
204
  return { kind: 'json' };
81
205
  }
82
206
  /** The input a field's schema asks for. */
83
- export function schemaControl(prop) {
207
+ export function schemaControl(field) {
208
+ const prop = withoutNull(field).schema;
84
209
  const options = enumOf(prop);
85
210
  if (options)
86
211
  return { kind: 'choice', options, isMenu: options.length > MAX_SEGMENTS };
212
+ const branches = branchesOf(prop);
213
+ if (branches)
214
+ return { kind: 'branches', branches, isMenu: branches.length > MAX_SEGMENTS };
87
215
  switch (kindOf(prop)) {
88
216
  case 'boolean':
89
217
  return { kind: 'switch' };
@@ -121,6 +249,7 @@ export function schemaFields(schema) {
121
249
  ? { description: prop.description }
122
250
  : {}),
123
251
  isRequired: required.has(key),
252
+ ...(withoutNull(prop).isNullable ? { isNullable: true } : {}),
124
253
  control: schemaControl(prop),
125
254
  },
126
255
  ];
@@ -138,10 +267,14 @@ function declaredSample(prop) {
138
267
  return { value: prop.enum[0] };
139
268
  return undefined;
140
269
  }
141
- function sampleValue(prop) {
142
- const declared = declaredSample(prop);
270
+ function sampleValue(field) {
271
+ const declared = declaredSample(field);
143
272
  if (declared)
144
273
  return declared.value;
274
+ const prop = withoutNull(field).schema;
275
+ const branches = branchesOf(prop);
276
+ if (branches?.[0])
277
+ return branchSample(branches[0]);
145
278
  switch (kindOf(prop)) {
146
279
  case 'number':
147
280
  case 'integer':
@@ -153,20 +286,28 @@ function sampleValue(prop) {
153
286
  case 'object':
154
287
  return sampleFromSchema(prop);
155
288
  case 'string':
156
- case 'unknown':
157
289
  return 'example';
290
+ case 'unknown':
291
+ // why: No sample fits a value of no known shape; the field waits for the person.
292
+ return undefined;
158
293
  }
159
294
  }
160
295
  /** Required fields, and optional ones the schema gives an example for; a plain value of the field's type when the schema declares no sample. */
161
296
  export function sampleFromSchema(schema) {
297
+ const branches = branchesOf(schema);
298
+ if (branches?.[0])
299
+ return branchSample(branches[0]);
162
300
  const props = isRow(schema.properties) ? schema.properties : {};
163
301
  const required = new Set(requiredOf(schema));
164
302
  const sample = {};
165
303
  for (const [key, prop] of Object.entries(props)) {
166
304
  if (!isRow(prop))
167
305
  continue;
168
- if (required.has(key))
169
- sample[key] = sampleValue(prop);
306
+ if (required.has(key)) {
307
+ const value = sampleValue(prop);
308
+ if (value !== undefined)
309
+ sample[key] = value;
310
+ }
170
311
  else if (Array.isArray(prop.examples) && prop.examples.length)
171
312
  sample[key] = prop.examples[0];
172
313
  }
@@ -187,13 +328,31 @@ export function withField(row, key, value) {
187
328
  }
188
329
  /** The labels of required fields the request is still missing, nested ones by their path. */
189
330
  export function missingFields(schema, value, prefix = '') {
331
+ const branches = branchesOf(schema);
332
+ if (branches) {
333
+ const branch = chosenBranch(branches, value);
334
+ if (!branch)
335
+ return [];
336
+ // why: A keyed shape's one key is the field a top-level request lacks when it is empty.
337
+ const label = prefix || (branch.is === undefined ? branch.label : '');
338
+ return missingIn(branchField(branch), branchValue(branch, value), label);
339
+ }
190
340
  if (!isRow(value))
191
341
  return [];
192
- return schemaFields(schema).flatMap((field) => {
193
- const entry = value[field.key];
194
- const label = prefix ? `${prefix} › ${field.label}` : field.label;
195
- if (entry === undefined || entry === null || entry === '')
196
- return field.isRequired ? [label] : [];
197
- return field.control.kind === 'group' ? missingFields(field.control.schema, entry, label) : [];
198
- });
342
+ return schemaFields(schema).flatMap((field) => missingIn(field, value[field.key], prefix ? `${prefix} › ${field.label}` : field.label));
343
+ }
344
+ /** What `entry`, the value of `field`, still lacks: the field itself, or what its shape needs. */
345
+ function missingIn(field, entry, label) {
346
+ if (entry === null && field.isNullable)
347
+ return [];
348
+ if (entry === undefined || entry === null || entry === '')
349
+ return field.isRequired ? [label] : [];
350
+ const { control } = field;
351
+ if (control.kind === 'group')
352
+ return missingFields(control.schema, entry, label);
353
+ if (control.kind === 'branches') {
354
+ const branch = chosenBranch(control.branches, entry);
355
+ return branch ? missingIn(branchField(branch), branchValue(branch, entry), label) : [label];
356
+ }
357
+ return [];
199
358
  }
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.949.gac4d2f6",
3
+ "version": "0.3.5-next.951.gc40a24e",
4
4
  "description": "Theorem on the web platform, with no UI framework: Web-standard server handlers that serve a profile, and the browser client that talks to them.",
5
5
  "license": "MIT",
6
6
  "type": "module",
@@ -25,6 +25,6 @@
25
25
  },
26
26
  "peerDependencies": {
27
27
  "zod": "^4.6.5",
28
- "@theoremjs/agents": "^0.3.5-next.949.gac4d2f6"
28
+ "@theoremjs/agents": "^0.3.5-next.951.gc40a24e"
29
29
  }
30
30
  }