@theoremjs/web 0.3.5-next.1025.g5523d8f → 0.3.5-next.1027.g2143bf2

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/README.md CHANGED
@@ -668,6 +668,18 @@ The save that `intake` calls in this example is your function. In one write, it
668
668
 
669
669
  The handler checks the message before it calls `intake`, with the core's `checkMessage`. A message with no `id`, with content the profile does not take, or to a conversation the session cannot read is refused, and `intake` is not called.
670
670
 
671
+ To refuse a message yourself, throw from `intake`. Name the reason, and give its words in the profile's `labels`:
672
+
673
+ ```ts
674
+ throw new TheoremError('request', 'ref names a document this person cannot use', {
675
+ copy: { key: 'reason.not_allowed', params: { name: 'Q3' } },
676
+ });
677
+
678
+ labels: { 'reason.not_allowed': 'You cannot use {name}.' }
679
+ ```
680
+
681
+ The page reads those words, on `/turn` and on `/steer`. A reason with no words reads as `request.refused`. The second argument of `TheoremError` is for your logs; the person never reads it.
682
+
671
683
  On `/turn`, the stream opens with a `conversation` line that has `posted` and `landsAs`. With `opened`, the reply follows on the same stream. With `saved`, the stream ends there.
672
684
 
673
685
  On `/steer`, the handler does not put the message into the running turn. The reply that holds the conversation reads the message from your inbox: in the same reply when your `prepare` takes it with `run.take`, or in a follow-up. The response is `{ ok: true, saved: { conversation, landsAs } }`. When the steer's save took the conversation, no reply was running. The handler then asks `onContinue`. For `'taken'`, your app runs the reply with `followUp`. Otherwise the handler lets the conversation go, and the page's wake runs the reply. Your `policy` must tell the message to a reply at `opening: 'wake'`.
package/client/index.d.ts CHANGED
@@ -25,7 +25,7 @@ export { type PageDispatchResult, withPageToolDispatch } from './page-dispatch.t
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
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
- 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';
28
+ export { fieldLabel, fieldReading, humanize, isPart, isPlain, isProse, isRow, json, type KeyNamer, 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';
31
31
  export { DOT_FLOATS, FRAME_SLOT, queryGeometry, SEGMENT_FLOATS, type SpaceGeometry, spaceGeometry, } from './space-geometry.ts';
@@ -5,6 +5,7 @@
5
5
  *
6
6
  * @module
7
7
  */
8
+ import { type KeyNamer } from './shaped-data.ts';
8
9
  export type JsonSchema = Record<string, unknown>;
9
10
  /** The input a field gets. */
10
11
  export type SchemaControl = {
@@ -73,13 +74,13 @@ export declare function branchValue(branch: SchemaBranch, value: unknown): unkno
73
74
  /** The value in `branch` with `body` as its body. */
74
75
  export declare function inBranch(branch: SchemaBranch, body: unknown): Record<string, unknown>;
75
76
  /** A shape's body as a field of its own, labelled as the shape. */
76
- export declare function branchField(branch: SchemaBranch): SchemaField;
77
+ export declare function branchField(branch: SchemaBranch, nameOf?: KeyNamer): SchemaField;
77
78
  /** A value in `branch`, its body at its sample. */
78
79
  export declare function branchSample(branch: SchemaBranch): Record<string, unknown>;
79
80
  /** The input a field's schema asks for. */
80
- export declare function schemaControl(field: JsonSchema): SchemaControl;
81
+ export declare function schemaControl(field: JsonSchema, nameOf?: KeyNamer): SchemaControl;
81
82
  /** An object schema's fields in the order it declares them, each with its input. */
82
- export declare function schemaFields(schema: JsonSchema): SchemaField[];
83
+ export declare function schemaFields(schema: JsonSchema, nameOf?: KeyNamer): SchemaField[];
83
84
  /** 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. */
84
85
  export declare function sampleFromSchema(schema: JsonSchema): Record<string, unknown>;
85
86
  /** A new row for a list of objects: its required fields, each at its sample. */
@@ -87,4 +88,4 @@ export declare function blankRow(item: JsonSchema): Record<string, unknown>;
87
88
  /** The object with `key` set, or dropped when the value is empty. An optional field left empty isn't sent. */
88
89
  export declare function withField(row: Record<string, unknown>, key: string, value: unknown): Record<string, unknown>;
89
90
  /** The labels of required fields the request is still missing, nested ones by their path. */
90
- export declare function missingFields(schema: JsonSchema, value: unknown, prefix?: string): string[];
91
+ export declare function missingFields(schema: JsonSchema, value: unknown, prefix?: string, nameOf?: KeyNamer): string[];
@@ -6,6 +6,7 @@
6
6
  * @module
7
7
  */
8
8
  import { humanize, isRow } from "./shaped-data.js";
9
+ const asWorded = (_, words) => words;
9
10
  /** Choices this few sit side by side; more open a menu. */
10
11
  const MAX_SEGMENTS = 5;
11
12
  /** Whole numbers bounded this closely slide; wider ranges are typed. */
@@ -79,7 +80,7 @@ function titleOf(prop) {
79
80
  }
80
81
  const isDistinct = (values) => new Set(values).size === values.length;
81
82
  /** Shapes told apart by a property each holds a different constant in. */
82
- function taggedBranches(options) {
83
+ function taggedBranches(options, nameOf) {
83
84
  const shapes = options.map((option) => option.properties);
84
85
  for (const key of Object.keys(shapes[0] ?? {})) {
85
86
  const tags = shapes.map((shape) => constOf(shape[key]));
@@ -94,7 +95,7 @@ function taggedBranches(options) {
94
95
  ? { required: option.required.filter((name) => name !== key) }
95
96
  : {};
96
97
  return {
97
- label: titleOf(option) ?? humanize(String(is)),
98
+ label: titleOf(option) ?? nameOf(String(is), humanize(String(is))),
98
99
  key,
99
100
  is,
100
101
  body: { ...option, properties: rest, ...required },
@@ -106,26 +107,26 @@ function taggedBranches(options) {
106
107
  return undefined;
107
108
  }
108
109
  /** Shapes that each hold one key of their own: the key says which. */
109
- function keyedBranches(options) {
110
+ function keyedBranches(options, nameOf) {
110
111
  const branches = options.map((option) => {
111
112
  const entries = Object.entries(option.properties);
112
113
  const [entry] = entries;
113
114
  if (entries.length !== 1 || !entry || !isRow(entry[1]))
114
115
  return undefined;
115
116
  const [key, body] = entry;
116
- return { label: titleOf(option) ?? humanize(key), key, body };
117
+ return { label: titleOf(option) ?? nameOf(key, humanize(key)), key, body };
117
118
  });
118
119
  if (!branches.every((branch) => branch !== undefined))
119
120
  return undefined;
120
121
  return isDistinct(branches.map((branch) => branch.key)) ? branches : undefined;
121
122
  }
122
123
  /** The shapes of a field of several objects, when its value shows which one it is in. */
123
- function branchesOf(prop) {
124
+ function branchesOf(prop, nameOf = asWorded) {
124
125
  const options = optionsOf(prop);
125
126
  if (!options || options.length < 2 || !options.every((option) => isRow(option.properties))) {
126
127
  return undefined;
127
128
  }
128
- return taggedBranches(options) ?? keyedBranches(options);
129
+ return taggedBranches(options, nameOf) ?? keyedBranches(options, nameOf);
129
130
  }
130
131
  /** The shape `value` is in, if it is in one. */
131
132
  export function chosenBranch(branches, value) {
@@ -149,12 +150,14 @@ export function inBranch(branch, body) {
149
150
  return { ...(isRow(body) ? body : {}), [branch.key]: branch.is };
150
151
  }
151
152
  /** A shape's body as a field of its own, labelled as the shape. */
152
- export function branchField(branch) {
153
+ export function branchField(branch, nameOf = asWorded) {
153
154
  return {
154
155
  key: branch.key,
155
156
  label: branch.label,
156
157
  isRequired: true,
157
- control: branch.is === undefined ? schemaControl(branch.body) : { kind: 'group', schema: branch.body },
158
+ control: branch.is === undefined
159
+ ? schemaControl(branch.body, nameOf)
160
+ : { kind: 'group', schema: branch.body },
158
161
  };
159
162
  }
160
163
  /** A value in `branch`, its body at its sample. */
@@ -204,12 +207,12 @@ function listControl(prop) {
204
207
  return { kind: 'json' };
205
208
  }
206
209
  /** The input a field's schema asks for. */
207
- export function schemaControl(field) {
210
+ export function schemaControl(field, nameOf = asWorded) {
208
211
  const prop = withoutNull(field).schema;
209
212
  const options = enumOf(prop);
210
213
  if (options)
211
214
  return { kind: 'choice', options, isMenu: options.length > MAX_SEGMENTS };
212
- const branches = branchesOf(prop);
215
+ const branches = branchesOf(prop, nameOf);
213
216
  if (branches)
214
217
  return { kind: 'branches', branches, isMenu: branches.length > MAX_SEGMENTS };
215
218
  switch (kindOf(prop)) {
@@ -235,7 +238,7 @@ function requiredOf(schema) {
235
238
  : []);
236
239
  }
237
240
  /** An object schema's fields in the order it declares them, each with its input. */
238
- export function schemaFields(schema) {
241
+ export function schemaFields(schema, nameOf = asWorded) {
239
242
  const props = isRow(schema.properties) ? schema.properties : {};
240
243
  const required = requiredOf(schema);
241
244
  return Object.entries(props).flatMap(([key, prop]) => {
@@ -244,13 +247,13 @@ export function schemaFields(schema) {
244
247
  return [
245
248
  {
246
249
  key,
247
- label: typeof prop.title === 'string' && prop.title ? prop.title : humanize(key),
250
+ label: typeof prop.title === 'string' && prop.title ? prop.title : nameOf(key, humanize(key)),
248
251
  ...(typeof prop.description === 'string' && prop.description
249
252
  ? { description: prop.description }
250
253
  : {}),
251
254
  isRequired: required.has(key),
252
255
  ...(withoutNull(prop).isNullable ? { isNullable: true } : {}),
253
- control: schemaControl(prop),
256
+ control: schemaControl(prop, nameOf),
254
257
  },
255
258
  ];
256
259
  });
@@ -327,32 +330,34 @@ export function withField(row, key, value) {
327
330
  return next;
328
331
  }
329
332
  /** The labels of required fields the request is still missing, nested ones by their path. */
330
- export function missingFields(schema, value, prefix = '') {
331
- const branches = branchesOf(schema);
333
+ export function missingFields(schema, value, prefix = '', nameOf = asWorded) {
334
+ const branches = branchesOf(schema, nameOf);
332
335
  if (branches) {
333
336
  const branch = chosenBranch(branches, value);
334
337
  if (!branch)
335
338
  return [];
336
339
  // why: A keyed shape's one key is the field a top-level request lacks when it is empty.
337
340
  const label = prefix || (branch.is === undefined ? branch.label : '');
338
- return missingIn(branchField(branch), branchValue(branch, value), label);
341
+ return missingIn(branchField(branch, nameOf), branchValue(branch, value), label, nameOf);
339
342
  }
340
343
  if (!isRow(value))
341
344
  return [];
342
- return schemaFields(schema).flatMap((field) => missingIn(field, value[field.key], prefix ? `${prefix} › ${field.label}` : field.label));
345
+ return schemaFields(schema, nameOf).flatMap((field) => missingIn(field, value[field.key], prefix ? `${prefix} › ${field.label}` : field.label, nameOf));
343
346
  }
344
347
  /** What `entry`, the value of `field`, still lacks: the field itself, or what its shape needs. */
345
- function missingIn(field, entry, label) {
348
+ function missingIn(field, entry, label, nameOf) {
346
349
  if (entry === null && field.isNullable)
347
350
  return [];
348
351
  if (entry === undefined || entry === null || entry === '')
349
352
  return field.isRequired ? [label] : [];
350
353
  const { control } = field;
351
354
  if (control.kind === 'group')
352
- return missingFields(control.schema, entry, label);
355
+ return missingFields(control.schema, entry, label, nameOf);
353
356
  if (control.kind === 'branches') {
354
357
  const branch = chosenBranch(control.branches, entry);
355
- return branch ? missingIn(branchField(branch), branchValue(branch, entry), label) : [label];
358
+ return branch
359
+ ? missingIn(branchField(branch, nameOf), branchValue(branch, entry), label, nameOf)
360
+ : [label];
356
361
  }
357
362
  return [];
358
363
  }
@@ -23,15 +23,17 @@ export declare function layoutChars(value: unknown): number;
23
23
  export declare function unpacked(value: unknown): unknown;
24
24
  /** `country_code`, `countryCode` → "Country code". */
25
25
  export declare function humanize(key: string): string;
26
+ /** Words a data key for the person; `words` is the key as Theorem would say it. */
27
+ export type KeyNamer = (key: string, words: string) => string;
26
28
  /** A unit worth printing: a symbol or abbreviation, not a format name like `iso8601` or `wmo code`. */
27
29
  export declare function printableUnit(unit: unknown): string | undefined;
28
30
  /** A column's label and unit: from its `<key>_units` entry, else its key's suffix. */
29
- export declare function fieldLabel(key: string, units?: Row): {
31
+ export declare function fieldLabel(key: string, units?: Row, nameOf?: KeyNamer): {
30
32
  label: string;
31
33
  unit?: string;
32
34
  };
33
35
  /** A field's label and the unit its value reads with. A unit only reads onto a number: `country_code` stays "Country code". */
34
- export declare function fieldReading(key: string, value: unknown, units?: Row): {
36
+ export declare function fieldReading(key: string, value: unknown, units?: Row, nameOf?: KeyNamer): {
35
37
  label: string;
36
38
  unit?: string;
37
39
  };
@@ -123,7 +125,7 @@ export type Section = {
123
125
  units?: Row;
124
126
  };
125
127
  /** An object's fields split: plain ones for the field list, nested ones as sections. */
126
- export declare function splitFields(row: Row): {
128
+ export declare function splitFields(row: Row, nameOf?: KeyNamer): {
127
129
  plain: [string, unknown][];
128
130
  sections: Section[];
129
131
  };
@@ -142,6 +142,7 @@ export function humanize(key) {
142
142
  .toLowerCase();
143
143
  return words.charAt(0).toUpperCase() + words.slice(1);
144
144
  }
145
+ const asWorded = (_, words) => words;
145
146
  /** A unit worth printing: a symbol or abbreviation, not a format name like `iso8601` or `wmo code`. */
146
147
  export function printableUnit(unit) {
147
148
  if (typeof unit !== 'string' || unit === '' || /\s/.test(unit) || unit === 'iso8601')
@@ -170,23 +171,23 @@ const SUFFIX_UNITS = {
170
171
  jpy: 'JPY',
171
172
  };
172
173
  /** The unit a key's suffix names, and the key without it: `distance_m` → Distance, m. */
173
- function suffixUnit(key) {
174
+ function suffixUnit(key, nameOf) {
174
175
  const match = /^(.+?)[_-]([a-z]+)$/i.exec(key);
175
176
  const unit = match ? SUFFIX_UNITS[(match[2] ?? '').toLowerCase()] : undefined;
176
- return match?.[1] && unit ? { label: humanize(match[1]), unit } : undefined;
177
+ return match?.[1] && unit ? { label: nameOf(key, humanize(match[1])), unit } : undefined;
177
178
  }
178
179
  /** A column's label and unit: from its `<key>_units` entry, else its key's suffix. */
179
- export function fieldLabel(key, units) {
180
+ export function fieldLabel(key, units, nameOf = asWorded) {
180
181
  const listed = printableUnit(units?.[key]);
181
182
  if (listed)
182
- return { label: humanize(key), unit: listed };
183
- return suffixUnit(key) ?? { label: humanize(key) };
183
+ return { label: nameOf(key, humanize(key)), unit: listed };
184
+ return suffixUnit(key, nameOf) ?? { label: nameOf(key, humanize(key)) };
184
185
  }
185
186
  /** A field's label and the unit its value reads with. A unit only reads onto a number: `country_code` stays "Country code". */
186
- export function fieldReading(key, value, units) {
187
+ export function fieldReading(key, value, units, nameOf = asWorded) {
187
188
  if (typeof value !== 'number')
188
- return { label: humanize(key) };
189
- return fieldLabel(key, units);
189
+ return { label: nameOf(key, humanize(key)) };
190
+ return fieldLabel(key, units, nameOf);
190
191
  }
191
192
  /** The `<key>_units` object beside `key`, if there is one. */
192
193
  export function unitsOf(row, key) {
@@ -344,7 +345,7 @@ function isInline(value) {
344
345
  inner.length <= MAX_INLINE_TOKENS &&
345
346
  inner.every(isPlain));
346
347
  }
347
- function section(row, key) {
348
+ function section(row, key, nameOf) {
348
349
  const path = [key];
349
350
  let inner = unpacked(row[key]);
350
351
  let units = unitsOf(row, key);
@@ -358,13 +359,16 @@ function section(row, key) {
358
359
  units = unitsOf(inner, only[0]);
359
360
  inner = unpacked(only[1]);
360
361
  }
361
- return { key, title: path.map(humanize).join(' › '), value: inner, units };
362
+ const title = path.map((part) => nameOf(part, humanize(part))).join(' › ');
363
+ return { key, title, value: inner, units };
362
364
  }
363
365
  /** An object's fields split: plain ones for the field list, nested ones as sections. */
364
- export function splitFields(row) {
366
+ export function splitFields(row, nameOf = asWorded) {
365
367
  const entries = shownEntries(row);
366
368
  return {
367
369
  plain: entries.filter(([, value]) => isInline(value)),
368
- sections: entries.filter(([, value]) => !isInline(value)).map(([key]) => section(row, key)),
370
+ sections: entries
371
+ .filter(([, value]) => !isInline(value))
372
+ .map(([key]) => section(row, key, nameOf)),
369
373
  };
370
374
  }
@@ -7,7 +7,7 @@
7
7
  * @module
8
8
  */
9
9
  import { type ToolOutputRefPart } from '@theoremjs/agents';
10
- import { type Row } from './shaped-data.ts';
10
+ import { type KeyNamer, type Row } from './shaped-data.ts';
11
11
  /** One number that answers the call, as a stat card shows it. */
12
12
  export type ResultFigure = {
13
13
  key: string;
@@ -64,7 +64,7 @@ export type ReturnedMedia = {
64
64
  }[];
65
65
  };
66
66
  /** What a tool's output leads with. Empty when nothing stands out: the data alone reads best. */
67
- export declare function layoutResult(output: unknown): ResultLayout;
67
+ export declare function layoutResult(output: unknown, nameOf?: KeyNamer): ResultLayout;
68
68
  /** The images and audio a call returned, each with its bytes; parts without them (references) are left out. */
69
69
  export declare function returnedMedia(parts: readonly ResultMedia[], imageAlt: string): ReturnedMedia;
70
70
  /** The references a call returned, which only the app's resolver turns into something to show. */
@@ -24,24 +24,25 @@ function isMeasure(key) {
24
24
  return !NOT_A_MEASURE.test(key);
25
25
  }
26
26
  /** A code-like key (`USD`, `EUR`) keeps its case; others read as words. */
27
- function keyLabel(key) {
28
- return /^[A-Z][A-Z0-9]{1,5}$/.test(key) ? key : humanize(key);
27
+ const CODE_KEY = /^[A-Z][A-Z0-9]{1,5}$/;
28
+ function keyLabel(key, nameOf) {
29
+ return nameOf(key, CODE_KEY.test(key) ? key : humanize(key));
29
30
  }
30
31
  /** A field's reading, unless its units say it's a code (`wmo code`, `iso8601`, a flag with no unit). */
31
- function measured(key, units) {
32
+ function measured(key, units, nameOf) {
32
33
  if (!isMeasure(key))
33
34
  return undefined;
34
35
  if (units && key in units && printableUnit(units[key]) === undefined)
35
36
  return undefined;
36
- const { label, unit } = fieldLabel(key, units);
37
- return { label: keyLabel(key) === key ? key : label, unit };
37
+ const { label, unit } = fieldLabel(key, units, nameOf);
38
+ return { label: CODE_KEY.test(key) ? keyLabel(key, nameOf) : label, unit };
38
39
  }
39
- function figuresOf(row, units) {
40
+ function figuresOf(row, units, nameOf) {
40
41
  const figures = [];
41
42
  for (const [key, value] of shownEntries(row)) {
42
43
  if (typeof value !== 'number' || !Number.isFinite(value))
43
44
  continue;
44
- const reading = measured(key, units);
45
+ const reading = measured(key, units, nameOf);
45
46
  if (reading)
46
47
  figures.push({ key, value, ...reading });
47
48
  }
@@ -56,7 +57,7 @@ function figuresOf(row, units) {
56
57
  * value's own top-level numbers. Beside a nested reading, top-level numbers
57
58
  * are usually the request echoed back.
58
59
  */
59
- function leadFigures(value) {
60
+ function leadFigures(value, nameOf) {
60
61
  if (!isRow(value))
61
62
  return [];
62
63
  for (const [key, entry] of shownEntries(value)) {
@@ -66,22 +67,22 @@ function leadFigures(value) {
66
67
  const plain = Object.values(inner);
67
68
  if (plain.length === 0 || !plain.every(isPlain))
68
69
  continue;
69
- const figures = figuresOf(inner, unitsOf(value, key));
70
+ const figures = figuresOf(inner, unitsOf(value, key), nameOf);
70
71
  if (figures.length > 0)
71
72
  return figures;
72
73
  }
73
- return figuresOf(value, undefined);
74
+ return figuresOf(value, undefined, nameOf);
74
75
  }
75
76
  function distinct(rows, key) {
76
77
  return new Set(rows.map((row) => row[key])).size;
77
78
  }
78
79
  /** Fields every row holds as a number, that measure something and vary. */
79
- function numericSeries(rows, skip, units) {
80
+ function numericSeries(rows, skip, units, nameOf) {
80
81
  const keys = [...new Set(rows.flatMap((row) => Object.keys(row)))].filter((key) => key !== skip &&
81
82
  rows.every((row) => typeof row[key] === 'number' || row[key] === null) &&
82
83
  distinct(rows, key) > 1);
83
84
  return keys.flatMap((key) => {
84
- const reading = measured(key, units);
85
+ const reading = measured(key, units, nameOf);
85
86
  return reading ? [{ key, ...reading }] : [];
86
87
  });
87
88
  }
@@ -111,9 +112,9 @@ function sharedWords(series) {
111
112
  };
112
113
  }
113
114
  /** Series over time, one chart per unit they share, each overlaying up to MAX_SERIES. */
114
- function timeCharts(path, title, rows, x, units) {
115
+ function timeCharts(path, title, rows, x, units, nameOf) {
115
116
  const byUnit = new Map();
116
- for (const series of numericSeries(rows, x, units)) {
117
+ for (const series of numericSeries(rows, x, units, nameOf)) {
117
118
  const group = byUnit.get(series.unit ?? '') ?? [];
118
119
  group.push(series);
119
120
  byUnit.set(series.unit ?? '', group);
@@ -150,11 +151,11 @@ function labelKey(rows) {
150
151
  return unique ?? title;
151
152
  }
152
153
  /** Rows named by a title field, ranked by each series that varies, most varied first. */
153
- function categoryCharts(path, title, rows, units) {
154
+ function categoryCharts(path, title, rows, units, nameOf) {
154
155
  const label = labelKey(rows);
155
156
  if (!label)
156
157
  return [];
157
- return numericSeries(rows, label, units)
158
+ return numericSeries(rows, label, units, nameOf)
158
159
  .sort((a, b) => distinct(rows, b.key) - distinct(rows, a.key))
159
160
  .map((series) => ({
160
161
  kind: 'category',
@@ -168,29 +169,34 @@ function categoryCharts(path, title, rows, units) {
168
169
  .slice(0, MAX_BARS),
169
170
  }));
170
171
  }
171
- function rowsCharts(path, title, rows, units) {
172
+ function rowsCharts(path, title, rows, units, nameOf) {
172
173
  if (rows.length < MIN_POINTS)
173
174
  return [];
174
175
  const x = dateKey(rows);
175
- return x ? timeCharts(path, title, rows, x, units) : categoryCharts(path, title, rows, units);
176
+ return x
177
+ ? timeCharts(path, title, rows, x, units, nameOf)
178
+ : categoryCharts(path, title, rows, units, nameOf);
176
179
  }
177
180
  /** Charts anywhere in the value, to MAX_SEARCH_DEPTH: lists of rows, column stores, and objects of numbers. */
178
- function chartsIn(value, path, title, units, depth) {
181
+ function chartsIn(value, path, title, units, depth, nameOf) {
179
182
  if (depth > MAX_SEARCH_DEPTH)
180
183
  return [];
181
184
  if (Array.isArray(value)) {
182
185
  const rows = value.map(unpacked);
183
- return rows.every(isRow) ? rowsCharts(path, title, rows, units) : [];
186
+ return rows.every(isRow) ? rowsCharts(path, title, rows, units, nameOf) : [];
184
187
  }
185
188
  if (!isRow(value))
186
189
  return [];
187
190
  const store = columnStore(value);
188
191
  if (store)
189
- return rowsCharts(path, title, storeRows(value, store), units);
192
+ return rowsCharts(path, title, storeRows(value, store), units, nameOf);
190
193
  const entries = shownEntries(value);
191
194
  const numbers = entries.filter(([key, entry]) => typeof entry === 'number' && isMeasure(key));
192
195
  if (depth > 0 && numbers.length >= MIN_POINTS && numbers.length === entries.length) {
193
- const rows = numbers.map(([key, entry]) => ({ label: keyLabel(key), value: entry }));
196
+ const rows = numbers.map(([key, entry]) => ({
197
+ label: keyLabel(key, nameOf),
198
+ value: entry,
199
+ }));
194
200
  return [
195
201
  {
196
202
  kind: 'category',
@@ -201,14 +207,14 @@ function chartsIn(value, path, title, units, depth) {
201
207
  },
202
208
  ];
203
209
  }
204
- return entries.flatMap(([key, entry]) => chartsIn(unpacked(entry), path ? `${path}.${key}` : key, depth === 0 ? humanize(key) : `${title} › ${humanize(key)}`, unitsOf(value, key), depth + 1));
210
+ return entries.flatMap(([key, entry]) => chartsIn(unpacked(entry), path ? `${path}.${key}` : key, depth === 0 ? nameOf(key, humanize(key)) : `${title} › ${nameOf(key, humanize(key))}`, unitsOf(value, key), depth + 1, nameOf));
205
211
  }
206
212
  /** What a tool's output leads with. Empty when nothing stands out: the data alone reads best. */
207
- export function layoutResult(output) {
213
+ export function layoutResult(output, nameOf = (_, words) => words) {
208
214
  const value = unpacked(output);
209
215
  return {
210
- figures: leadFigures(value),
211
- charts: chartsIn(value, '', '', undefined, 0).slice(0, MAX_CHARTS),
216
+ figures: leadFigures(value, nameOf),
217
+ charts: chartsIn(value, '', '', undefined, 0, nameOf).slice(0, MAX_CHARTS),
212
218
  };
213
219
  }
214
220
  function mediaSrc(part) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@theoremjs/web",
3
- "version": "0.3.5-next.1025.g5523d8f",
3
+ "version": "0.3.5-next.1027.g2143bf2",
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.1025.g5523d8f"
28
+ "@theoremjs/agents": "^0.3.5-next.1027.g2143bf2"
29
29
  }
30
30
  }