@ai-matrx/content-ir 0.5.0 → 0.6.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/index.js CHANGED
@@ -3171,6 +3171,29 @@ function buildAgentSchemaWithRenderBlockSupport(input, rootKindSlug, arrayBindin
3171
3171
  }
3172
3172
  return updatedRoot;
3173
3173
  }
3174
+ function resolveRef(ref, ctx) {
3175
+ if (ref === "#" || ref === "#/") {
3176
+ return { slug: ctx.schemaName, node: ctx.rootSchema };
3177
+ }
3178
+ const match = /^#\/(?:\$defs|definitions)\/(.+)$/.exec(ref);
3179
+ if (!match) return null;
3180
+ const name = decodeURIComponent(
3181
+ (match[1] ?? "").replace(/~1/g, "/").replace(/~0/g, "~")
3182
+ );
3183
+ const node = ctx.defs[name];
3184
+ if (!isRecord4(node)) return null;
3185
+ const declared = isRecord4(node.properties) ? readBlockKindFromProperties(node.properties) : null;
3186
+ return { slug: declared ?? name, node };
3187
+ }
3188
+ function refToObjectField(resolved, path, ctx) {
3189
+ if (ctx.refsInProgress.has(resolved.slug)) return;
3190
+ ctx.refsInProgress.add(resolved.slug);
3191
+ try {
3192
+ registerDeclaredKindDraft(resolved.slug, resolved.node, path, ctx);
3193
+ } finally {
3194
+ ctx.refsInProgress.delete(resolved.slug);
3195
+ }
3196
+ }
3174
3197
  function isAnyValueSchema(node) {
3175
3198
  return node.type === void 0 && node.enum === void 0 && node.const === void 0 && node.properties === void 0 && node.items === void 0 && node.additionalProperties === void 0 && node.anyOf === void 0 && node.oneOf === void 0 && node.allOf === void 0 && node.$ref === void 0;
3176
3199
  }
@@ -3207,12 +3230,21 @@ function convertProperty(fieldName, node, required, path, ctx) {
3207
3230
  }
3208
3231
  function convertPropertyCore(fieldName, node, required, path, ctx) {
3209
3232
  if (typeof node.$ref === "string") {
3210
- ctx.problems.push({
3211
- severity: "error",
3212
- path,
3213
- message: `$ref is not supported ("${node.$ref}"). Inline the schema manually.`
3214
- });
3215
- return null;
3233
+ const resolved = resolveRef(node.$ref, ctx);
3234
+ if (!resolved) {
3235
+ ctx.problems.push({
3236
+ severity: "error",
3237
+ path,
3238
+ message: `Unresolvable $ref ("${node.$ref}") \u2014 only "#", "#/$defs/<name>" and "#/definitions/<name>" are supported.`
3239
+ });
3240
+ return null;
3241
+ }
3242
+ refToObjectField(resolved, path, ctx);
3243
+ return {
3244
+ ...requiredNullableFlags(required),
3245
+ type: "object",
3246
+ kind: resolved.slug
3247
+ };
3216
3248
  }
3217
3249
  if (Array.isArray(node.anyOf) || Array.isArray(node.oneOf)) {
3218
3250
  const variants = node.anyOf ?? node.oneOf;
@@ -3228,12 +3260,18 @@ function convertPropertyCore(fieldName, node, required, path, ctx) {
3228
3260
  continue;
3229
3261
  }
3230
3262
  if (typeof variant.$ref === "string") {
3231
- ctx.problems.push({
3232
- severity: "error",
3233
- path,
3234
- message: `$ref inside anyOf/oneOf is not supported ("${variant.$ref}"). Inline the schema manually.`
3235
- });
3236
- unsupported = true;
3263
+ const resolvedVariant = resolveRef(variant.$ref, ctx);
3264
+ if (!resolvedVariant) {
3265
+ ctx.problems.push({
3266
+ severity: "error",
3267
+ path,
3268
+ message: `Unresolvable $ref inside anyOf/oneOf ("${variant.$ref}").`
3269
+ });
3270
+ unsupported = true;
3271
+ continue;
3272
+ }
3273
+ refToObjectField(resolvedVariant, path, ctx);
3274
+ memberKinds.push(resolvedVariant.slug);
3237
3275
  continue;
3238
3276
  }
3239
3277
  const { type: type2, nullable: nullable2 } = resolvePrimaryType(variant);
@@ -3436,6 +3474,27 @@ function convertPropertyCore(fieldName, node, required, path, ctx) {
3436
3474
  });
3437
3475
  return null;
3438
3476
  }
3477
+ if (typeof itemNode.$ref === "string") {
3478
+ const resolvedItem = resolveRef(itemNode.$ref, ctx);
3479
+ if (!resolvedItem) {
3480
+ ctx.problems.push({
3481
+ severity: "error",
3482
+ path: `${path}[]`,
3483
+ message: `Unresolvable $ref in array items ("${itemNode.$ref}").`
3484
+ });
3485
+ return null;
3486
+ }
3487
+ refToObjectField(resolvedItem, `${path}[]`, ctx);
3488
+ ctx.arrayBindings.push({
3489
+ arrayField: fieldName,
3490
+ itemKindSlug: resolvedItem.slug
3491
+ });
3492
+ return {
3493
+ ...requiredNullableFlags(required, nullable),
3494
+ type: "array",
3495
+ itemKinds: [resolvedItem.slug]
3496
+ };
3497
+ }
3439
3498
  if (isAnyValueSchema(itemNode)) {
3440
3499
  return {
3441
3500
  ...requiredNullableFlags(required, nullable),
@@ -3470,11 +3529,29 @@ function convertPropertyCore(fieldName, node, required, path, ctx) {
3470
3529
  }
3471
3530
  const itemKinds = [];
3472
3531
  for (const variant of itemVariants) {
3532
+ if (isRecord4(variant) && typeof variant.$ref === "string") {
3533
+ const resolvedVariant = resolveRef(variant.$ref, ctx);
3534
+ if (!resolvedVariant) {
3535
+ ctx.problems.push({
3536
+ severity: "error",
3537
+ path: `${path}[]`,
3538
+ message: `Unresolvable $ref in array items anyOf ("${variant.$ref}").`
3539
+ });
3540
+ return null;
3541
+ }
3542
+ itemKinds.push(resolvedVariant.slug);
3543
+ refToObjectField(resolvedVariant, `${path}[]<${resolvedVariant.slug}>`, ctx);
3544
+ ctx.arrayBindings.push({
3545
+ arrayField: fieldName,
3546
+ itemKindSlug: resolvedVariant.slug
3547
+ });
3548
+ continue;
3549
+ }
3473
3550
  if (!isRecord4(variant) || !isRecord4(variant.properties)) {
3474
3551
  ctx.problems.push({
3475
3552
  severity: "error",
3476
3553
  path: `${path}[]`,
3477
- message: "Array items anyOf/oneOf variants must be inline objects declaring __kind."
3554
+ message: "Array items anyOf/oneOf variants must be inline objects declaring __kind, or $refs to defs."
3478
3555
  });
3479
3556
  return null;
3480
3557
  }
@@ -3608,6 +3685,14 @@ function convertPropertyCore(fieldName, node, required, path, ctx) {
3608
3685
  kind: referencedKind
3609
3686
  };
3610
3687
  }
3688
+ if (Object.keys(nestedFields).length === 0 && !apIsOpen && isRecord4(ap) && KIND_KEY in node.properties) {
3689
+ const apType = resolvePrimaryType(ap).type;
3690
+ return {
3691
+ ...requiredNullableFlags(required, nullable),
3692
+ type: "record",
3693
+ values: apType === "string" || apType === "number" || apType === "boolean" ? apType : "json"
3694
+ };
3695
+ }
3611
3696
  let open = apIsOpen;
3612
3697
  if (!apIsOpen && isRecord4(ap)) {
3613
3698
  ctx.problems.push({
@@ -3724,12 +3809,21 @@ function normalizeAiSchemaInput(input) {
3724
3809
  return { name: null, strict: null, rootSchema: null, parseErrors };
3725
3810
  }
3726
3811
  function convertAiSchemaToBlockFields(schemaName, rootSchema, strict) {
3812
+ const rawDefs = isRecord4(rootSchema.$defs) ? rootSchema.$defs : isRecord4(rootSchema.definitions) ? rootSchema.definitions : {};
3813
+ const defs = {};
3814
+ for (const [name, node] of Object.entries(rawDefs)) {
3815
+ if (isRecord4(node)) defs[name] = node;
3816
+ }
3727
3817
  const ctx = {
3728
3818
  schemaName,
3819
+ strict,
3729
3820
  problems: [],
3730
3821
  droppedMetadata: [],
3731
3822
  blockSchemas: [],
3732
- arrayBindings: []
3823
+ arrayBindings: [],
3824
+ defs,
3825
+ rootSchema,
3826
+ refsInProgress: /* @__PURE__ */ new Set()
3733
3827
  };
3734
3828
  ctx.droppedMetadata.push(...collectDropped(rootSchema, ""));
3735
3829
  const rootAp = rootSchema.additionalProperties;