@ai-matrx/content-ir 0.19.21 → 0.19.25

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/index.cjs CHANGED
@@ -3188,13 +3188,13 @@ function makeKindJsonSchemaProperty(kindSlug, strict) {
3188
3188
  return { ...base2, enum: [kindSlug] };
3189
3189
  }
3190
3190
  function injectKindIntoObjectSchema(objectSchema, kindSlug, strict) {
3191
- const clone = deepClone(objectSchema);
3192
- const properties = isRecord4(clone.properties) ? { ...clone.properties } : {};
3191
+ const clone2 = deepClone(objectSchema);
3192
+ const properties = isRecord4(clone2.properties) ? { ...clone2.properties } : {};
3193
3193
  properties[KIND_KEY] = makeKindJsonSchemaProperty(kindSlug, strict);
3194
- const required = Array.isArray(clone.required) ? clone.required.filter((k) => typeof k === "string") : [];
3194
+ const required = Array.isArray(clone2.required) ? clone2.required.filter((k) => typeof k === "string") : [];
3195
3195
  const nextRequired = required.includes(KIND_KEY) ? required : [KIND_KEY, ...required];
3196
3196
  return {
3197
- ...clone,
3197
+ ...clone2,
3198
3198
  properties,
3199
3199
  required: nextRequired,
3200
3200
  ...strict ? { additionalProperties: false } : {}
@@ -4391,8 +4391,132 @@ function kindSchemaToJsonSchema(kind, resolve, options = {}) {
4391
4391
  return { name: kind, schema: { ...rootNode, $defs: defs }, strict, unresolved };
4392
4392
  }
4393
4393
 
4394
+ // convert/control-keys.ts
4395
+ var CONTROL_KEYS = ["_replaces", "_new", "_record_id", "_records"];
4396
+ var RECORD_DISPOSITION = "record";
4397
+ var KIND_KEY2 = "__kind";
4398
+ var DOCUMENT_KEYS = /* @__PURE__ */ new Set(["$schema", "$id", "$defs", "definitions", "title", "description"]);
4399
+ var CONTROL = new Set(CONTROL_KEYS);
4400
+ function controlKeysApplyTo(disposition) {
4401
+ return disposition === RECORD_DISPOSITION;
4402
+ }
4403
+ function clone(value) {
4404
+ return value === void 0 ? value : JSON.parse(JSON.stringify(value));
4405
+ }
4406
+ function isObjectSchema(schema) {
4407
+ if (!schema || typeof schema !== "object" || Array.isArray(schema)) return false;
4408
+ const s = schema;
4409
+ return s.type === "object" && !!s.properties && typeof s.properties === "object" && !Array.isArray(s.properties);
4410
+ }
4411
+ function requiredOf(schema) {
4412
+ return Array.isArray(schema.required) ? schema.required : [];
4413
+ }
4414
+ function recordObject(schema) {
4415
+ const item = {};
4416
+ for (const [k, v] of Object.entries(schema)) {
4417
+ if (!DOCUMENT_KEYS.has(k)) item[k] = clone(v);
4418
+ }
4419
+ const props = {};
4420
+ for (const [k, v] of Object.entries(item.properties)) {
4421
+ if (k !== KIND_KEY2 && !CONTROL.has(k)) props[k] = v;
4422
+ }
4423
+ item.properties = props;
4424
+ const required = requiredOf(item).filter((k) => k !== KIND_KEY2 && !CONTROL.has(k));
4425
+ if (required.length > 0) item.required = required;
4426
+ else delete item.required;
4427
+ return item;
4428
+ }
4429
+ function stored(schema) {
4430
+ const out = clone(schema);
4431
+ const item = recordObject(schema);
4432
+ const props = out.properties;
4433
+ props._replaces = { type: ["string", "null"], format: "uuid" };
4434
+ props._new = { type: ["boolean", "null"] };
4435
+ props._record_id = { type: ["string", "null"], format: "uuid" };
4436
+ props._records = { type: ["array", "null"], items: item };
4437
+ const rootRequired = requiredOf(out).filter((k) => !CONTROL.has(k));
4438
+ const lifted = rootRequired.filter((k) => k !== KIND_KEY2);
4439
+ if (lifted.length > 0) {
4440
+ const kept = rootRequired.filter((k) => k === KIND_KEY2);
4441
+ if (kept.length > 0) out.required = kept;
4442
+ else delete out.required;
4443
+ const branch = {
4444
+ if: {
4445
+ anyOf: [
4446
+ { required: ["_record_id"], properties: { _record_id: { type: "string" } } },
4447
+ { required: ["_records"], properties: { _records: { type: "array" } } }
4448
+ ]
4449
+ },
4450
+ else: { required: lifted }
4451
+ };
4452
+ out.allOf = Array.isArray(out.allOf) ? [...out.allOf, branch] : [branch];
4453
+ }
4454
+ return out;
4455
+ }
4456
+ function authored(schema, many, allowEdit) {
4457
+ const signals = {
4458
+ _replaces: { type: "string", format: "uuid" },
4459
+ _new: { type: "boolean" }
4460
+ };
4461
+ if (allowEdit) signals._record_id = { type: "string", format: "uuid" };
4462
+ if (!many) {
4463
+ const out2 = clone(schema);
4464
+ Object.assign(out2.properties, clone(signals));
4465
+ const required = requiredOf(out2).filter((k) => !CONTROL.has(k));
4466
+ if (required.length > 0 || "required" in out2) out2.required = required;
4467
+ return out2;
4468
+ }
4469
+ const out = {};
4470
+ for (const [k, v] of Object.entries(schema)) {
4471
+ if (DOCUMENT_KEYS.has(k)) out[k] = clone(v);
4472
+ }
4473
+ out.type = "object";
4474
+ const kindProp = schema.properties[KIND_KEY2];
4475
+ const properties = {};
4476
+ if (kindProp !== void 0) properties[KIND_KEY2] = clone(kindProp);
4477
+ properties._records = { type: "array", items: recordObject(schema) };
4478
+ Object.assign(properties, clone(signals));
4479
+ out.properties = properties;
4480
+ out.required = [KIND_KEY2, "_records"].filter((k) => k in properties);
4481
+ out.additionalProperties = false;
4482
+ return out;
4483
+ }
4484
+ function withControlKeys(schema, options) {
4485
+ const { mode, many = false, allowEdit = false } = options;
4486
+ if (mode !== "stored" && mode !== "authored") {
4487
+ throw new Error(`withControlKeys: mode must be "stored" or "authored", got ${String(mode)}`);
4488
+ }
4489
+ if (!isObjectSchema(schema)) return schema;
4490
+ return mode === "stored" ? stored(schema) : authored(schema, many, allowEdit);
4491
+ }
4492
+ var CONTROL_KEY_FIELDS = Object.freeze({
4493
+ _replaces: { type: "string", nullable: true },
4494
+ _new: { type: "boolean", nullable: true },
4495
+ _record_id: { type: "string", nullable: true },
4496
+ // A batch's items are records of this kind WITHOUT `__kind`; the render layer reads them
4497
+ // as opaque JSON (never stricter than the store, which validates each item).
4498
+ _records: { type: "json[]", nullable: true }
4499
+ });
4500
+ function withControlKeyFields(schema) {
4501
+ if (schema.root) return schema;
4502
+ const fields = { ...schema.fields };
4503
+ for (const key of CONTROL_KEYS) {
4504
+ if (!(key in fields)) fields[key] = { ...CONTROL_KEY_FIELDS[key] };
4505
+ }
4506
+ return { ...schema, fields };
4507
+ }
4508
+ function stripControlKeys(value) {
4509
+ const values = {};
4510
+ const signals = {};
4511
+ for (const [k, v] of Object.entries(value)) {
4512
+ if (CONTROL.has(k)) signals[k] = v;
4513
+ else values[k] = v;
4514
+ }
4515
+ return { values, signals };
4516
+ }
4517
+
4394
4518
  // convert/json-schema-to-kind.ts
4395
- function kindSchemaFromJsonSchema(kind, jsonSchema) {
4519
+ function kindSchemaFromJsonSchema(kind, jsonSchema, options = {}) {
4396
4520
  if (jsonSchema === null || jsonSchema === void 0) {
4397
4521
  return { schema: null, children: {}, problems: [] };
4398
4522
  }
@@ -4423,6 +4547,9 @@ function kindSchemaFromJsonSchema(kind, jsonSchema) {
4423
4547
  if (draft.slug === kind) schema = asKindSchema;
4424
4548
  else children[draft.slug] = asKindSchema;
4425
4549
  }
4550
+ if (schema && controlKeysApplyTo(options.disposition)) {
4551
+ schema = withControlKeyFields(schema);
4552
+ }
4426
4553
  return { schema, children, problems: converted.problems };
4427
4554
  }
4428
4555
 
@@ -7933,6 +8060,8 @@ exports.BacktickRuns = BacktickRuns;
7933
8060
  exports.CALLOUT_NAMES = CALLOUT_NAMES;
7934
8061
  exports.CAPABILITY_BY_CLASS = CAPABILITY_BY_CLASS;
7935
8062
  exports.CLASSES = CLASSES;
8063
+ exports.CONTROL_KEYS = CONTROL_KEYS;
8064
+ exports.CONTROL_KEY_FIELDS = CONTROL_KEY_FIELDS;
7936
8065
  exports.DIRECTIVE_VERSION = DIRECTIVE_VERSION;
7937
8066
  exports.DirectiveDecodeError = DirectiveDecodeError;
7938
8067
  exports.FENCE_OPENER_WHITESPACE = FENCE_OPENER_WHITESPACE;
@@ -7952,6 +8081,7 @@ exports.KindStreamParser = KindStreamParser;
7952
8081
  exports.NESTING_FENCE_LANGUAGES = NESTING_FENCE_LANGUAGES;
7953
8082
  exports.NODE_OUTCOME_KIND = NODE_OUTCOME_KIND;
7954
8083
  exports.ParseSession = ParseSession;
8084
+ exports.RECORD_DISPOSITION = RECORD_DISPOSITION;
7955
8085
  exports.RESERVED_PREFIX = RESERVED_PREFIX;
7956
8086
  exports.ROOT_STORAGE_NAME = ROOT_STORAGE_NAME;
7957
8087
  exports.RUN_RESULT_KIND = RUN_RESULT_KIND;
@@ -7979,6 +8109,7 @@ exports.collectReferencedKinds = collectReferencedKinds;
7979
8109
  exports.collectSchemaReferencedKinds = collectSchemaReferencedKinds;
7980
8110
  exports.compareWithExistingKindSchema = compareWithExistingKindSchema;
7981
8111
  exports.continuesTable = continuesTable;
8112
+ exports.controlKeysApplyTo = controlKeysApplyTo;
7982
8113
  exports.convertAiSchemaToBlockFields = convertAiSchemaToBlockFields;
7983
8114
  exports.createFingerprinter = createFingerprinter;
7984
8115
  exports.createKindStreamParser = createKindStreamParser;
@@ -8065,6 +8196,7 @@ exports.opensTable = opensTable;
8065
8196
  exports.pairDisplayMath = pairDisplayMath;
8066
8197
  exports.parseDirectiveSlug = parseDirectiveSlug;
8067
8198
  exports.parseFenceOpener = parseFenceOpener;
8199
+ exports.pendingCodeSpanStart = pendingCodeSpanStart;
8068
8200
  exports.pendingDisplayMathOpener = pendingDisplayMathOpener;
8069
8201
  exports.readEnvelope = readEnvelope;
8070
8202
  exports.readNodeOutcomeValue = readNodeOutcomeValue;
@@ -8097,6 +8229,7 @@ exports.splitsSurrogatePair = splitsSurrogatePair;
8097
8229
  exports.startsHtmlBlock = startsHtmlBlock;
8098
8230
  exports.startsPipelessTable = startsPipelessTable;
8099
8231
  exports.storageToKindSchema = storageToKindSchema;
8232
+ exports.stripControlKeys = stripControlKeys;
8100
8233
  exports.stripKindDeep = stripKindDeep;
8101
8234
  exports.tableContainerIndent = tableContainerIndent;
8102
8235
  exports.tableStartsAt = tableStartsAt;
@@ -8110,6 +8243,8 @@ exports.tryDecodeDirectiveContent = tryDecodeDirectiveContent;
8110
8243
  exports.unescapeCellPipes = unescapeCellPipes;
8111
8244
  exports.validateBlockSchemaSavePlan = validateBlockSchemaSavePlan;
8112
8245
  exports.validateStructuralLeg = validateStructuralLeg;
8246
+ exports.withControlKeyFields = withControlKeyFields;
8247
+ exports.withControlKeys = withControlKeys;
8113
8248
  exports.withRootKind = withRootKind;
8114
8249
  exports.xmlDiscriminator = xmlDiscriminator;
8115
8250
  //# sourceMappingURL=index.cjs.map