@segmentstream-cloud/ontology 0.22.0 → 0.23.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/render.js CHANGED
@@ -1,4 +1,4 @@
1
- import { shortestRoutesFrom } from "./aggregate-routes.js";
1
+ import { inputEntities, shortestRoutesFrom } from "./aggregate-routes.js";
2
2
  import { isAllowed, permissionOf, READ_VERB, rolesGranting } from "./permissions.js";
3
3
  import { REFERENCE_ANNOTATION } from "./property.js";
4
4
  /** What an action of the manifest requires; version 1 manifests leave it implicit. */
@@ -389,6 +389,51 @@ export function measuresFor(manifest, permissions) {
389
389
  measure,
390
390
  })));
391
391
  }
392
+ /**
393
+ * The measures of the model whose every input entity the reader may read, in declaration order.
394
+ * Reading a model measure is reading its inputs. Without permissions, every one.
395
+ */
396
+ export function modelMeasuresFor(manifest, permissions) {
397
+ return (manifest.measures ?? []).filter((measure) => inputEntities(measure).every((entity) => readable(entity, permissions)));
398
+ }
399
+ /** `a`, `a and b`, `a, b and c`. */
400
+ function listed(values) {
401
+ return values.length < 2
402
+ ? values.join("")
403
+ : `${values.slice(0, -1).join(", ")} and ${values[values.length - 1]}`;
404
+ }
405
+ /**
406
+ * How one alignment group reads: `sales.order_date aligns with plans.month`, and for an entity
407
+ * without a counterpart `listings has no counterpart for sales.order_date`. Members follow the
408
+ * measure's input entities, whatever order the document keeps them in.
409
+ */
410
+ function alignmentClauses(measure, group) {
411
+ const order = inputEntities(measure);
412
+ const members = Object.entries(group).sort(([left], [right]) => order.indexOf(left) - order.indexOf(right));
413
+ const present = members.flatMap(([entity, property]) => property === null ? [] : [`${entity}.${property}`]);
414
+ const absent = members.flatMap(([entity, property]) => (property === null ? [entity] : []));
415
+ const [first, ...rest] = present;
416
+ if (first === undefined) {
417
+ return [];
418
+ }
419
+ return [
420
+ ...(rest.length > 0 ? [`${first} aligns with ${listed(rest)}`] : []),
421
+ ...absent.map((entity) => `${entity} has no counterpart for ${first}`),
422
+ ];
423
+ }
424
+ /**
425
+ * A measure of the model as `describe`, the MCP tool and the instructions show it: its description,
426
+ * the entity measures it reads and how their properties align. `Ordered over planned units. Reads
427
+ * sales.ordered and plans.planned_units; sales.order_date aligns with plans.month.`
428
+ */
429
+ export function modelMeasureDescription(measure) {
430
+ const clauses = [
431
+ `Reads ${listed(measure.inputs)}`,
432
+ ...measure.align.flatMap((group) => alignmentClauses(measure, group)),
433
+ ];
434
+ const described = measure.description === "" ? [] : [sentence(measure.description)];
435
+ return [...described, `${clauses.join("; ")}.`].join(" ");
436
+ }
392
437
  /**
393
438
  * The entities one entity reaches, with the shortest routes to each: exactly the routes aggregate
394
439
  * resolution weighs ({@link shortestRoutesFrom}). Only then is the reader applied: a target they may
@@ -416,8 +461,9 @@ function reachLine(entity, reaches) {
416
461
  return `${entity} reaches ${targets.join(", ")}.`;
417
462
  }
418
463
  /**
419
- * The aggregate part of the catalog's prose: every measure the reader may read, model-wide, and
420
- * per measure entity the entities it reaches. Empty when the reader may read no measure.
464
+ * The aggregate part of the catalog's prose: every measure the reader may read, model-wide, the
465
+ * measures of the model whose every input they may read, and per measure entity the entities it
466
+ * reaches. Empty when the reader may read no measure.
421
467
  */
422
468
  export function renderAggregates(manifest, permissions) {
423
469
  const measures = measuresFor(manifest, permissions);
@@ -430,10 +476,17 @@ export function renderAggregates(manifest, permissions) {
430
476
  return reaches.length === 0 ? [] : [reachLine(owner, reaches)];
431
477
  });
432
478
  const measureLines = measures.map(({ name, entity, measure }) => `${name} (${measure.unit}): ${sentence(measureDescription(entity, measure))}`);
479
+ const modelLines = modelMeasuresFor(manifest, permissions).map((measure) => `${measure.name} (${measure.unit}): ${modelMeasureDescription(measure)}`);
433
480
  return [
434
481
  `Aggregates: ${RECORDS_OR_AGGREGATES} (POST /api/ontology/aggregate, or the aggregate tool).`,
435
482
  `${INDENT}Measures, as <entity>.<measure>:`,
436
483
  ...measureLines.map((line) => `${INDENT}${INDENT}${line}`),
484
+ ...(modelLines.length === 0
485
+ ? []
486
+ : [
487
+ `${INDENT}Measures of the model, by name:`,
488
+ ...modelLines.map((line) => `${INDENT}${INDENT}${line}`),
489
+ ]),
437
490
  ...(reachLines.length === 0
438
491
  ? []
439
492
  : [
@@ -1,7 +1,8 @@
1
1
  import { type OntologyManifest } from "../manifest.js";
2
2
  /**
3
3
  * The catalog for a reader: the manifest, with each entity's actions narrowed to the ones the given
4
- * permissions may run. Without permissions it is the manifest itself.
4
+ * permissions may run, and only the measures of the model whose every input entity they may read.
5
+ * Without permissions it is the manifest itself.
5
6
  */
6
7
  export declare function catalogFor(manifest: OntologyManifest, permissions?: ReadonlyArray<string>): OntologyManifest;
7
8
  export interface GraphRelationship {
@@ -1,16 +1,20 @@
1
1
  import { isValueMatched } from "../manifest.js";
2
2
  import { isAllowed } from "../permissions.js";
3
- import { actionPermission } from "../render.js";
3
+ import { actionPermission, modelMeasuresFor } from "../render.js";
4
4
  /**
5
5
  * The catalog for a reader: the manifest, with each entity's actions narrowed to the ones the given
6
- * permissions may run. Without permissions it is the manifest itself.
6
+ * permissions may run, and only the measures of the model whose every input entity they may read.
7
+ * Without permissions it is the manifest itself.
7
8
  */
8
9
  export function catalogFor(manifest, permissions) {
9
10
  if (permissions === undefined) {
10
11
  return manifest;
11
12
  }
13
+ const { measures: declared, ...rest } = manifest;
14
+ const measures = declared === undefined ? [] : modelMeasuresFor(manifest, permissions);
12
15
  return {
13
- ...manifest,
16
+ ...rest,
17
+ ...(measures.length === 0 ? {} : { measures }),
14
18
  entities: manifest.entities
15
19
  .filter((entity) => entity.source === undefined ||
16
20
  isAllowed(entity.permissions?.read ?? `${entity.name}:read`, { permissions }))
@@ -7,7 +7,7 @@ import { SCOPE_MISMATCH } from "../aggregate-platform.js";
7
7
  import { CurrentPrincipal, withPrincipal } from "../authentication.js";
8
8
  import { catalogFor } from "../catalog.js";
9
9
  import { ConversationSearch } from "../agent/conversations.js";
10
- import { describeCatalog, measureDescription, measuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
10
+ import { describeCatalog, measureDescription, measuresFor, modelMeasureDescription, modelMeasuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
11
11
  import { RequestId, errorBodyForWire, expectedFailure, internalError, schemaIssues, } from "../errors.js";
12
12
  import { publicOrigin } from "../oauth/protocol.js";
13
13
  import { mcpChallenge, mcpResource } from "../oauth/server.js";
@@ -313,7 +313,10 @@ function instructions({ built }, permissions) {
313
313
  return (definition.source === undefined && isAllowed(permissionOf(name, READ_VERB), { permissions }));
314
314
  })
315
315
  .map(({ name, resolved }) => `- ${name}: ${resolved.entity.definition.description}`);
316
- const measures = measuresFor(manifest, permissions).map(({ name, entity, measure }) => `- ${name}: ${measureDescription(entity, measure)}`);
316
+ const measures = [
317
+ ...measuresFor(manifest, permissions).map(({ name, entity, measure }) => `- ${name}: ${measureDescription(entity, measure)}`),
318
+ ...modelMeasuresFor(manifest, permissions).map((measure) => `- ${measure.name}: ${modelMeasureDescription(measure)}`),
319
+ ];
317
320
  return [
318
321
  "This server is the app's ontology: the records the company works with and the actions " +
319
322
  "that change them. Every change goes through an action, is checked against your " +
@@ -1,11 +1,11 @@
1
1
  import { Schema } from "effect";
2
2
  import { AGGREGATE_NAME_PATTERN } from "../../aggregate-routes.js";
3
3
  import { InvalidOntology } from "../../diagnostics.js";
4
- import { isValueMatched } from "../../manifest.js";
4
+ import { isValueMatched, } from "../../manifest.js";
5
5
  import { GENERATED_UPSERT_PREFIX, humanize, mcpToolName } from "../../names.js";
6
6
  import { isAllowed, permissionOf, READ_VERB } from "../../permissions.js";
7
7
  import { queryJsonSchema } from "../../query.js";
8
- import { measureDescription, measuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
8
+ import { measureDescription, measuresFor, modelMeasureDescription, modelMeasuresFor, RECORDS_OR_AGGREGATES, } from "../../render.js";
9
9
  import { actionInputJsonSchema } from "../../runtime/generated-actions.js";
10
10
  import { uploadFields } from "../../storage/attachments.js";
11
11
  import { actionDescription, requires } from "../descriptions.js";
@@ -281,8 +281,15 @@ function measureChoice({ name, entity, measure }) {
281
281
  measureDescription(entity, measure),
282
282
  };
283
283
  }
284
+ /** A measure of the model, by its bare name, read only by a caller who may read every input. */
285
+ function modelMeasureChoice(measure) {
286
+ return {
287
+ const: measure.name,
288
+ description: `Model · ${measure.label} (${measure.unit}): ` + modelMeasureDescription(measure),
289
+ };
290
+ }
284
291
  /** The aggregate tool's input for a caller: the request of `POST /api/ontology/aggregate`. */
285
- function aggregateInput(measures) {
292
+ function aggregateInput({ measures, model }) {
286
293
  return {
287
294
  type: "object",
288
295
  properties: {
@@ -291,9 +298,11 @@ function aggregateInput(measures) {
291
298
  minItems: 1,
292
299
  maxItems: AGGREGATE_LIMITS.measures,
293
300
  uniqueItems: true,
294
- items: { anyOf: measures.map(measureChoice) },
295
- description: "What to compute, as <entity>.<measure>. Measures of several entities are each " +
296
- "computed over their own entity's rows, then combined on the dimensions.",
301
+ items: { anyOf: [...measures.map(measureChoice), ...model.map(modelMeasureChoice)] },
302
+ description: "What to compute, as <entity>.<measure>, or a measure of the model by its name. " +
303
+ "Measures of several entities are each computed over their own entity's rows, then " +
304
+ "combined on the dimensions; a measure of the model is computed on the combined rows " +
305
+ "from the entity measures it reads.",
297
306
  },
298
307
  dimensions: {
299
308
  type: "array",
@@ -385,17 +394,23 @@ const AGGREGATE_DESCRIPTION = "Numbers about the model: measures from one or sev
385
394
  "which dates were used. " +
386
395
  "Answers come from the warehouse, and stored records from its replica: execution.sources says " +
387
396
  "how fresh each is, and execution.warnings says when a filter did not apply to a measure. " +
388
- "Requires reading every entity the request reads, path intermediates included. Call describe " +
389
- "for the model's measures and relationships.";
397
+ "Requires reading every entity the request reads, path intermediates included, and every " +
398
+ "entity a measure of the model reads. A measure of the model is named without an entity; a " +
399
+ "filter must reach every entity it reads or none of them, unless its alignment gives an entity " +
400
+ "no counterpart. Call describe for the model's measures and relationships.";
390
401
  function aggregateTool(manifest) {
391
402
  return {
392
403
  name: "aggregate",
393
404
  title: "Aggregate measures",
394
405
  description: AGGREGATE_DESCRIPTION,
395
- inputSchema: aggregateInput(measuresFor(manifest)),
406
+ inputSchema: aggregateInput({
407
+ measures: measuresFor(manifest),
408
+ model: modelMeasuresFor(manifest),
409
+ }),
396
410
  inputSchemaFor: (caller) => {
397
411
  const measures = measuresFor(manifest, caller.permissions);
398
- return measures.length === 0 ? undefined : aggregateInput(measures);
412
+ const model = modelMeasuresFor(manifest, caller.permissions);
413
+ return measures.length === 0 ? undefined : aggregateInput({ measures, model });
399
414
  },
400
415
  annotations: READ,
401
416
  permissions: [],
@@ -268,6 +268,16 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
268
268
  readonly key: Schema.String;
269
269
  readonly description: Schema.String;
270
270
  }>>>;
271
+ readonly measures: Schema.optionalKey<Schema.$Array<Schema.Struct<{
272
+ readonly name: Schema.String;
273
+ readonly label: Schema.String;
274
+ readonly description: Schema.String;
275
+ readonly unit: Schema.Literals<readonly ["number", "currency", "ratio", "percent"]>;
276
+ readonly format: Schema.Literals<readonly ["decimal", "currency", "percent"]>;
277
+ readonly expression: Schema.Codec<Schema.Json, Schema.Json, never, never>;
278
+ readonly inputs: Schema.$Array<Schema.String>;
279
+ readonly align: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.String>>>;
280
+ }>>>;
271
281
  }>, Schema.Struct<{
272
282
  readonly entities: Schema.toEncoded<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
273
283
  readonly measures: Schema.optionalKey<Schema.$Array<Schema.encodeKeys<Schema.Struct<{
@@ -387,6 +397,16 @@ export declare const Catalog: Schema.decodeTo<Schema.Struct<{
387
397
  readonly key: Schema.String;
388
398
  readonly description: Schema.String;
389
399
  }>>>>;
400
+ readonly measures: Schema.toEncoded<Schema.optionalKey<Schema.$Array<Schema.Struct<{
401
+ readonly name: Schema.String;
402
+ readonly label: Schema.String;
403
+ readonly description: Schema.String;
404
+ readonly unit: Schema.Literals<readonly ["number", "currency", "ratio", "percent"]>;
405
+ readonly format: Schema.Literals<readonly ["decimal", "currency", "percent"]>;
406
+ readonly expression: Schema.Codec<Schema.Json, Schema.Json, never, never>;
407
+ readonly inputs: Schema.$Array<Schema.String>;
408
+ readonly align: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.String>>>;
409
+ }>>>>;
390
410
  }>, never, never>;
391
411
  export declare const Graph: Schema.Struct<{
392
412
  readonly hash: Schema.String;
@@ -505,6 +525,7 @@ export declare const AggregateResult: Schema.Struct<{
505
525
  readonly additive: Schema.optionalKey<Schema.Boolean>;
506
526
  readonly filters: Schema.optionalKey<Schema.$Array<Schema.String>>;
507
527
  readonly balanceOver: Schema.optionalKey<Schema.String>;
528
+ readonly inputs: Schema.optionalKey<Schema.$Array<Schema.String>>;
508
529
  }>>;
509
530
  readonly rows: Schema.$Array<Schema.$Record<Schema.String, Schema.NullOr<Schema.Union<readonly [Schema.String, Schema.Boolean]>>>>;
510
531
  readonly truncated: Schema.Boolean;
@@ -366,7 +366,10 @@ const aggregateFields = {
366
366
  export const AggregateRequest = closedStruct({
367
367
  measures: Schema.Array(Schema.String)
368
368
  .check(Schema.isMaxLength(AGGREGATE_LIMITS.measures))
369
- .annotate({ description: "<entity>.<measure>, at least one." }),
369
+ .annotate({
370
+ description: "<entity>.<measure>, or a measure of the model by its name, such as plan_attainment; " +
371
+ "at least one, at most 30 of both together.",
372
+ }),
370
373
  dimensions: Schema.optionalKey(Schema.Array(Schema.String).check(Schema.isMaxLength(AGGREGATE_LIMITS.dimensions))).annotate({
371
374
  description: "<entity>.<property>, or a route from a measure's entity: " +
372
375
  "<entity>.<relationship>[.<relationship>].<property>.",
@@ -408,7 +411,9 @@ export const EntityAggregateRequest = closedStruct({
408
411
  /** SegmentStream's answer to an aggregate, which the service forwards unchanged. */
409
412
  export const AggregateResult = Schema.Struct({
410
413
  fields: Schema.Array(Schema.Struct({
411
- name: Schema.String.annotate({ description: "A dimension identity or <entity>.<measure>." }),
414
+ name: Schema.String.annotate({
415
+ description: "A dimension identity, <entity>.<measure>, or the name of a measure of the model.",
416
+ }),
412
417
  label: Schema.String,
413
418
  type: Schema.String,
414
419
  unit: Schema.optionalKey(Schema.String),
@@ -425,6 +430,10 @@ export const AggregateResult = Schema.Struct({
425
430
  description: "The date or timestamp dimension a balance measure was grouped by or filtered to one " +
426
431
  "day of.",
427
432
  }),
433
+ inputs: Schema.optionalKey(Schema.Array(Schema.String)).annotate({
434
+ description: "The entity measures a measure of the model reads, <entity>.<measure>, in declaration " +
435
+ "order; present only on such a measure.",
436
+ }),
428
437
  })),
429
438
  rows: Schema.Array(Schema.Record(Schema.String, Schema.NullOr(Schema.Union([Schema.String, Schema.Boolean])))),
430
439
  truncated: Schema.Boolean,
@@ -253,6 +253,10 @@ function checkMeasures(input) {
253
253
  !["decimal", "currency", "percent"].includes(measure.format)) {
254
254
  report("invalid-measure", field, "Invalid measure format.");
255
255
  }
256
+ if (measure.align !== undefined) {
257
+ report("invalid-measure", field, "Entity measures take no align; a measure that aligns the properties of several " +
258
+ "entities belongs in defineOntology({ measures }).");
259
+ }
256
260
  let remaining = 10_000;
257
261
  /** The balance properties of the measure's aggregates and of the measures it references. */
258
262
  const balances = new Set();
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@segmentstream-cloud/ontology",
3
- "version": "0.22.0",
3
+ "version": "0.23.0",
4
4
  "description": "Define a SegmentStream workspace app's ontology: entities, relationships, roles and actions, written with Effect.",
5
5
  "license": "Apache-2.0",
6
6
  "author": "SegmentStream",