@ai-matrx/content-ir 0.10.3 → 0.11.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.cjs CHANGED
@@ -4631,6 +4631,354 @@ function emitPayloadFence(kind, value) {
4631
4631
  return "```json\n" + emitPayloadJson(kind, value) + "\n```";
4632
4632
  }
4633
4633
 
4634
+ // directives/grammar.ts
4635
+ var RESERVED_PREFIX = "directive_v";
4636
+ var DIRECTIVE_VERSION = 1;
4637
+ var SLUG_PREFIX = `${RESERVED_PREFIX}${DIRECTIVE_VERSION}_`;
4638
+ var CLASSES = [
4639
+ "reference",
4640
+ "view",
4641
+ "create",
4642
+ "update",
4643
+ "delete",
4644
+ "action",
4645
+ "validation",
4646
+ "secret"
4647
+ ];
4648
+ var CAPABILITY_BY_CLASS = {
4649
+ reference: "pure",
4650
+ view: "pure",
4651
+ validation: "pure",
4652
+ secret: "sensitive",
4653
+ create: "side_effect",
4654
+ update: "side_effect",
4655
+ delete: "side_effect",
4656
+ action: "side_effect"
4657
+ };
4658
+ var SIDE_EFFECT_CLASSES = new Set(
4659
+ CLASSES.filter((c) => CAPABILITY_BY_CLASS[c] === "side_effect")
4660
+ );
4661
+ var IN_CONTENT_CLASSES = /* @__PURE__ */ new Set([
4662
+ "reference",
4663
+ "secret"
4664
+ ]);
4665
+ function isDirectiveClass(value) {
4666
+ return typeof value === "string" && Object.prototype.hasOwnProperty.call(CAPABILITY_BY_CLASS, value);
4667
+ }
4668
+ function isReservedDirectiveSlug(slug) {
4669
+ return typeof slug === "string" && slug.startsWith(RESERVED_PREFIX);
4670
+ }
4671
+ function isToken(value) {
4672
+ return /^[a-z][a-z0-9_]*$/.test(value);
4673
+ }
4674
+ function buildDirectiveSlug(directiveClass, noun, version = DIRECTIVE_VERSION) {
4675
+ if (!isDirectiveClass(directiveClass)) {
4676
+ throw new Error(
4677
+ `unknown directive class ${JSON.stringify(directiveClass)}; the vocabulary is CLOSED: ${CLASSES.join(", ")}.`
4678
+ );
4679
+ }
4680
+ if (typeof noun !== "string" || !isToken(noun)) {
4681
+ throw new Error(
4682
+ `invalid directive noun ${JSON.stringify(noun)} for class ${JSON.stringify(directiveClass)}: a noun is lowercase [a-z0-9_], starts with a letter, and is non-empty.`
4683
+ );
4684
+ }
4685
+ return `${RESERVED_PREFIX}${version}_${directiveClass}_${noun}`;
4686
+ }
4687
+ function parseDirectiveSlug(slug) {
4688
+ if (!isReservedDirectiveSlug(slug)) return null;
4689
+ const rest = slug.slice(RESERVED_PREFIX.length);
4690
+ const firstSep = rest.indexOf("_");
4691
+ if (firstSep <= 0) return null;
4692
+ const versionDigits = rest.slice(0, firstSep);
4693
+ if (!/^[0-9]+$/.test(versionDigits)) return null;
4694
+ const remainder = rest.slice(firstSep + 1);
4695
+ const classSep = remainder.indexOf("_");
4696
+ if (classSep <= 0) return null;
4697
+ const directiveClass = remainder.slice(0, classSep);
4698
+ const noun = remainder.slice(classSep + 1);
4699
+ if (!isDirectiveClass(directiveClass) || !isToken(noun)) return null;
4700
+ return {
4701
+ slug,
4702
+ version: Number.parseInt(versionDigits, 10),
4703
+ directiveClass,
4704
+ noun,
4705
+ capability: CAPABILITY_BY_CLASS[directiveClass],
4706
+ executes: executesAtOutputRoot(directiveClass),
4707
+ inContent: resolvesInContent(directiveClass)
4708
+ };
4709
+ }
4710
+ function capabilityOf(directiveClass) {
4711
+ return CAPABILITY_BY_CLASS[directiveClass];
4712
+ }
4713
+ function executesAtOutputRoot(directiveClass) {
4714
+ return isDirectiveClass(directiveClass) && SIDE_EFFECT_CLASSES.has(directiveClass);
4715
+ }
4716
+ function resolvesInContent(directiveClass) {
4717
+ return isDirectiveClass(directiveClass) && IN_CONTENT_CLASSES.has(directiveClass);
4718
+ }
4719
+ function directiveSlugOf(obj) {
4720
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return null;
4721
+ const slug = obj[KIND_KEY];
4722
+ return isReservedDirectiveSlug(slug) ? slug : null;
4723
+ }
4724
+ function isKindDirective(obj) {
4725
+ return directiveSlugOf(obj) !== null;
4726
+ }
4727
+ function buildKindDirective(slug, items) {
4728
+ return { [KIND_KEY]: slug, items };
4729
+ }
4730
+ function looksLikeDirectiveHead(content) {
4731
+ const match = content.trimStart().match(/^\{\s*"__kind"\s*:\s*"([^"]*)/);
4732
+ return !!match && isReservedDirectiveSlug(match[1]);
4733
+ }
4734
+
4735
+ // directives/legacy-shell.ts
4736
+ var LEGACY_SENTINEL = "matrx_version";
4737
+ var CLASS_BY_LEGACY_KIND = {
4738
+ reference: "reference",
4739
+ secret: "secret",
4740
+ validation: "validation"
4741
+ };
4742
+ var LEGACY_SIDE_EFFECT_KINDS = /* @__PURE__ */ new Set(["output_directive", "function"]);
4743
+ var VERB_NOUN_RE = /^(create|update|delete):([a-z][a-z0-9_]*)$/;
4744
+ var USES_SLOT = /* @__PURE__ */ Symbol.for("ai-matrx.content-ir.legacy-shell-uses");
4745
+ var uses = globalThis[USES_SLOT] ??= { count: 0 };
4746
+ function legacyShellUses() {
4747
+ return uses.count;
4748
+ }
4749
+ function resetLegacyShellUses() {
4750
+ uses.count = 0;
4751
+ }
4752
+ function isLegacyShell(obj) {
4753
+ if (typeof obj !== "object" || obj === null || Array.isArray(obj)) return false;
4754
+ const record = obj;
4755
+ if (!(LEGACY_SENTINEL in record)) return false;
4756
+ const kind = record.kind;
4757
+ return typeof kind === "string" && (kind in CLASS_BY_LEGACY_KIND || LEGACY_SIDE_EFFECT_KINDS.has(kind));
4758
+ }
4759
+ function slugForLegacy(kind, type) {
4760
+ let directiveClass;
4761
+ let noun;
4762
+ if (LEGACY_SIDE_EFFECT_KINDS.has(kind)) {
4763
+ const match = VERB_NOUN_RE.exec(type);
4764
+ if (match) {
4765
+ directiveClass = match[1];
4766
+ noun = match[2];
4767
+ } else {
4768
+ directiveClass = "action";
4769
+ noun = type;
4770
+ }
4771
+ } else {
4772
+ const mapped = CLASS_BY_LEGACY_KIND[kind];
4773
+ if (!mapped) return null;
4774
+ directiveClass = mapped;
4775
+ noun = type;
4776
+ }
4777
+ try {
4778
+ return buildDirectiveSlug(directiveClass, noun);
4779
+ } catch {
4780
+ return null;
4781
+ }
4782
+ }
4783
+ function translateLegacyShell(obj) {
4784
+ const kind = obj.kind;
4785
+ const type = obj.type;
4786
+ if (typeof kind !== "string" || typeof type !== "string") return null;
4787
+ const slug = slugForLegacy(kind, type);
4788
+ if (slug === null) return null;
4789
+ const items = obj.items;
4790
+ uses.count += 1;
4791
+ return { [KIND_KEY]: slug, items: Array.isArray(items) ? [...items] : [] };
4792
+ }
4793
+
4794
+ // directives/decode.ts
4795
+ var DirectiveDecodeError = class extends Error {
4796
+ constructor(message) {
4797
+ super(message);
4798
+ this.name = "DirectiveDecodeError";
4799
+ }
4800
+ };
4801
+ function asObject(value) {
4802
+ if (typeof value !== "object" || value === null || Array.isArray(value)) return null;
4803
+ return value;
4804
+ }
4805
+ function decodeDirective(value) {
4806
+ let obj = asObject(value);
4807
+ if (obj === null) return null;
4808
+ let legacy = false;
4809
+ if (isLegacyShell(obj)) {
4810
+ const translated = translateLegacyShell(obj);
4811
+ if (translated === null) {
4812
+ throw new DirectiveDecodeError(
4813
+ `a retired 4-key shell with kind=${JSON.stringify(obj.kind)} type=${JSON.stringify(obj.type)} does not map onto the Kind Directives grammar. Emit the current shell: {"${KIND_KEY}": "directive_v1_<class>_<noun>", "items": [...]}.`
4814
+ );
4815
+ }
4816
+ obj = translated;
4817
+ legacy = true;
4818
+ }
4819
+ const rawSlug = obj[KIND_KEY];
4820
+ if (!isReservedDirectiveSlug(rawSlug)) return null;
4821
+ const parsed = parseDirectiveSlug(rawSlug);
4822
+ if (parsed === null) {
4823
+ throw new DirectiveDecodeError(
4824
+ `malformed directive slug ${JSON.stringify(rawSlug)} \u2014 it claims the reserved "directive_v" namespace but does not parse as directive_v<version>_<class>_<noun>.`
4825
+ );
4826
+ }
4827
+ const rawItems = obj.items;
4828
+ const items = Array.isArray(rawItems) ? rawItems.filter((i) => asObject(i) !== null) : [];
4829
+ return {
4830
+ parsed,
4831
+ slug: parsed.slug,
4832
+ directiveClass: parsed.directiveClass,
4833
+ noun: parsed.noun,
4834
+ items,
4835
+ shell: { [KIND_KEY]: parsed.slug, items },
4836
+ legacyShell: legacy
4837
+ };
4838
+ }
4839
+ function tryDecodeDirective(value, onError) {
4840
+ try {
4841
+ return decodeDirective(value);
4842
+ } catch (error) {
4843
+ onError?.(error instanceof Error ? error.message : "directive decode failed");
4844
+ return null;
4845
+ }
4846
+ }
4847
+ function tryDecodeDirectiveContent(content, onError) {
4848
+ if (typeof content !== "string") return tryDecodeDirective(content, onError);
4849
+ let parsed;
4850
+ try {
4851
+ parsed = JSON.parse(content);
4852
+ } catch {
4853
+ return null;
4854
+ }
4855
+ return tryDecodeDirective(parsed, onError);
4856
+ }
4857
+
4858
+ // directives/display.ts
4859
+ function titleCaseToken(token) {
4860
+ const words = token.replace(/_/g, " ").trim();
4861
+ return words ? words.charAt(0).toUpperCase() + words.slice(1) : token;
4862
+ }
4863
+ var ACTION_BY_CLASS = {
4864
+ reference: "Reference",
4865
+ view: "View",
4866
+ create: "Create",
4867
+ update: "Update",
4868
+ delete: "Delete",
4869
+ action: "Run",
4870
+ validation: "Validate",
4871
+ secret: "Secret"
4872
+ };
4873
+ function nounLabel(noun, catalog) {
4874
+ const label = catalog?.(noun)?.label;
4875
+ return label && label.length > 0 ? label : titleCaseToken(noun);
4876
+ }
4877
+ function nounFamily(noun, catalog) {
4878
+ return catalog?.(noun)?.family ?? "";
4879
+ }
4880
+ function nounTitleColumn(noun, catalog) {
4881
+ return catalog?.(noun)?.titleColumn ?? null;
4882
+ }
4883
+ function directiveDisplay(directiveClass, noun, catalog) {
4884
+ const label = nounLabel(noun, catalog);
4885
+ const action = ACTION_BY_CLASS[directiveClass];
4886
+ return {
4887
+ noun: label,
4888
+ family: nounFamily(noun, catalog),
4889
+ action,
4890
+ title: `${action} ${label}`
4891
+ };
4892
+ }
4893
+
4894
+ // directives/item-summary.ts
4895
+ var NAME_KEYS = ["name", "title", "label", "heading", "slug", "key", "question", "summary"];
4896
+ var FACT_EXCLUDE = /* @__PURE__ */ new Set([
4897
+ "__kind",
4898
+ ...NAME_KEYS,
4899
+ "id",
4900
+ "description",
4901
+ "about",
4902
+ "notes",
4903
+ "content",
4904
+ "text",
4905
+ "body",
4906
+ "organization_id",
4907
+ "user_id",
4908
+ "created_by",
4909
+ "resource_type"
4910
+ ]);
4911
+ function firstString(item, keys) {
4912
+ for (const key of keys) {
4913
+ const value = item[key];
4914
+ if (typeof value === "string" && value.trim()) return value.trim();
4915
+ }
4916
+ return null;
4917
+ }
4918
+ function itemTitle(item, titleColumn, index, total) {
4919
+ const fromCatalog = titleColumn ? firstString(item, [titleColumn]) : null;
4920
+ const name = fromCatalog ?? firstString(item, NAME_KEYS);
4921
+ if (name) return name;
4922
+ return total > 1 ? `Item ${index + 1} of ${total}` : "Item";
4923
+ }
4924
+ function itemSubtitle(item) {
4925
+ return firstString(item, ["description", "about", "summary", "doc"]);
4926
+ }
4927
+ var UUID_RE = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
4928
+ var FACT_LABEL_OVERRIDES = {
4929
+ variable_definitions: "variables",
4930
+ context_policies: "context",
4931
+ custom_tools: "custom tools"
4932
+ };
4933
+ function factLabel(key) {
4934
+ return FACT_LABEL_OVERRIDES[key] ?? key.replace(/_/g, " ");
4935
+ }
4936
+ function itemFacts(item, limit = 4) {
4937
+ const facts = [];
4938
+ for (const [key, value] of Object.entries(item)) {
4939
+ if (facts.length >= limit) break;
4940
+ if (FACT_EXCLUDE.has(key)) continue;
4941
+ if (Array.isArray(value)) {
4942
+ if (value.length === 0) continue;
4943
+ facts.push({ key, label: factLabel(key), value: String(value.length) });
4944
+ continue;
4945
+ }
4946
+ if (typeof value === "string") {
4947
+ const trimmed = value.trim();
4948
+ if (!trimmed || trimmed.length > 40) continue;
4949
+ if (UUID_RE.test(trimmed)) continue;
4950
+ facts.push({ key, label: factLabel(key), value: trimmed });
4951
+ continue;
4952
+ }
4953
+ if (typeof value === "number" || typeof value === "boolean") {
4954
+ facts.push({ key, label: factLabel(key), value: String(value) });
4955
+ }
4956
+ }
4957
+ return facts;
4958
+ }
4959
+
4960
+ // directives/item-kind.ts
4961
+ function directiveItemKindFromEdges(edges) {
4962
+ for (const edge of edges ?? []) {
4963
+ const field = edge.field_name ?? edge.fieldPath;
4964
+ const child = edge.child_kind ?? edge.childKind;
4965
+ if (field === "items" && typeof child === "string" && child) return child;
4966
+ }
4967
+ return null;
4968
+ }
4969
+ function asKindInstance(item, kind) {
4970
+ const existing = item[KIND_KEY];
4971
+ const resolved = typeof existing === "string" && existing ? existing : kind;
4972
+ if (!resolved) return null;
4973
+ return { [KIND_KEY]: resolved, ...item };
4974
+ }
4975
+
4976
+ exports.ACTION_BY_CLASS = ACTION_BY_CLASS;
4977
+ exports.CAPABILITY_BY_CLASS = CAPABILITY_BY_CLASS;
4978
+ exports.CLASSES = CLASSES;
4979
+ exports.DIRECTIVE_VERSION = DIRECTIVE_VERSION;
4980
+ exports.DirectiveDecodeError = DirectiveDecodeError;
4981
+ exports.IN_CONTENT_CLASSES = IN_CONTENT_CLASSES;
4634
4982
  exports.IR_ENVELOPE_CACHE_VERSION = IR_ENVELOPE_CACHE_VERSION;
4635
4983
  exports.IR_ENVELOPE_KEY = IR_ENVELOPE_KEY;
4636
4984
  exports.IR_PARTIAL_KEY = IR_PARTIAL_KEY;
@@ -4643,11 +4991,18 @@ exports.KindStorageError = KindStorageError;
4643
4991
  exports.KindStreamParser = KindStreamParser;
4644
4992
  exports.NODE_OUTCOME_KIND = NODE_OUTCOME_KIND;
4645
4993
  exports.ParseSession = ParseSession;
4994
+ exports.RESERVED_PREFIX = RESERVED_PREFIX;
4646
4995
  exports.ROOT_STORAGE_NAME = ROOT_STORAGE_NAME;
4647
4996
  exports.RUN_RESULT_KIND = RUN_RESULT_KIND;
4997
+ exports.SIDE_EFFECT_CLASSES = SIDE_EFFECT_CLASSES;
4998
+ exports.SLUG_PREFIX = SLUG_PREFIX;
4648
4999
  exports.advancePartialKind = advancePartialKind;
5000
+ exports.asKindInstance = asKindInstance;
4649
5001
  exports.buildAgentSchemaWithRenderBlockSupport = buildAgentSchemaWithRenderBlockSupport;
4650
5002
  exports.buildCompliantKindSnapshot = buildCompliantKindSnapshot;
5003
+ exports.buildDirectiveSlug = buildDirectiveSlug;
5004
+ exports.buildKindDirective = buildKindDirective;
5005
+ exports.capabilityOf = capabilityOf;
4651
5006
  exports.classifyInboundEnvelopeMetadata = classifyInboundEnvelopeMetadata;
4652
5007
  exports.collectReferencedKinds = collectReferencedKinds;
4653
5008
  exports.collectSchemaReferencedKinds = collectSchemaReferencedKinds;
@@ -4655,13 +5010,18 @@ exports.compareWithExistingKindSchema = compareWithExistingKindSchema;
4655
5010
  exports.convertAiSchemaToBlockFields = convertAiSchemaToBlockFields;
4656
5011
  exports.createFingerprinter = createFingerprinter;
4657
5012
  exports.createKindStreamParser = createKindStreamParser;
5013
+ exports.decodeDirective = decodeDirective;
4658
5014
  exports.describeDualGateFailure = describeDualGateFailure;
5015
+ exports.directiveDisplay = directiveDisplay;
5016
+ exports.directiveItemKindFromEdges = directiveItemKindFromEdges;
5017
+ exports.directiveSlugOf = directiveSlugOf;
4659
5018
  exports.disposeParseSession = disposeParseSession;
4660
5019
  exports.emitPayloadFence = emitPayloadFence;
4661
5020
  exports.emitPayloadJson = emitPayloadJson;
4662
5021
  exports.emptyValueForFieldSchema = emptyValueForFieldSchema;
4663
5022
  exports.envelopeCacheFromEnvelopes = envelopeCacheFromEnvelopes;
4664
5023
  exports.envelopeFromCompleteValue = envelopeFromCompleteValue;
5024
+ exports.executesAtOutputRoot = executesAtOutputRoot;
4665
5025
  exports.fenceDiscriminator = fenceDiscriminator;
4666
5026
  exports.fieldsToDbPayload = fieldsToDbPayload;
4667
5027
  exports.fingerprintText = fingerprintText;
@@ -4673,22 +5033,35 @@ exports.irPathKey = irPathKey;
4673
5033
  exports.irPathLabel = irPathLabel;
4674
5034
  exports.irPathsEqual = irPathsEqual;
4675
5035
  exports.isCanonicalBlockIR = isCanonicalBlockIR;
5036
+ exports.isDirectiveClass = isDirectiveClass;
4676
5037
  exports.isDuplicateBlockSlug = isDuplicateBlockSlug;
4677
5038
  exports.isEmptyResidue = isEmptyResidue;
4678
5039
  exports.isIrEnvelopeCache = isIrEnvelopeCache;
4679
5040
  exports.isJsonAnyField = isJsonAnyField;
5041
+ exports.isKindDirective = isKindDirective;
5042
+ exports.isLegacyShell = isLegacyShell;
4680
5043
  exports.isProvisionalKind = isProvisionalKind;
5044
+ exports.isReservedDirectiveSlug = isReservedDirectiveSlug;
4681
5045
  exports.isScalarArrayType = isScalarArrayType;
4682
5046
  exports.isTerminalKindEvent = isTerminalKindEvent;
5047
+ exports.itemFacts = itemFacts;
5048
+ exports.itemSubtitle = itemSubtitle;
5049
+ exports.itemTitle = itemTitle;
4683
5050
  exports.kindSchemaFromJsonSchema = kindSchemaFromJsonSchema;
4684
5051
  exports.kindSchemaToJsonSchema = kindSchemaToJsonSchema;
4685
5052
  exports.kindSchemaToStorage = kindSchemaToStorage;
4686
5053
  exports.kindVerdictOf = kindVerdictOf;
5054
+ exports.legacyShellUses = legacyShellUses;
5055
+ exports.looksLikeDirectiveHead = looksLikeDirectiveHead;
4687
5056
  exports.makePartialKindStalenessGate = makePartialKindStalenessGate;
4688
5057
  exports.mergeResidueIntoValue = mergeResidueIntoValue;
4689
5058
  exports.normalizeAiSchemaInput = normalizeAiSchemaInput;
4690
5059
  exports.normalizeJsonRegion = normalizeJsonRegion;
5060
+ exports.nounFamily = nounFamily;
5061
+ exports.nounLabel = nounLabel;
5062
+ exports.nounTitleColumn = nounTitleColumn;
4691
5063
  exports.openParseSession = openParseSession;
5064
+ exports.parseDirectiveSlug = parseDirectiveSlug;
4692
5065
  exports.readEnvelope = readEnvelope;
4693
5066
  exports.readNodeOutcomeValue = readNodeOutcomeValue;
4694
5067
  exports.readObjectKind = readObjectKind;
@@ -4698,6 +5071,8 @@ exports.readRunResultValue = readRunResultValue;
4698
5071
  exports.reconstructRegionValue = reconstructRegionValue;
4699
5072
  exports.rehydrateNodeOutcome = rehydrateNodeOutcome;
4700
5073
  exports.rehydrateRunResult = rehydrateRunResult;
5074
+ exports.resetLegacyShellUses = resetLegacyShellUses;
5075
+ exports.resolvesInContent = resolvesInContent;
4701
5076
  exports.reuseEnvelopeIfCurrent = reuseEnvelopeIfCurrent;
4702
5077
  exports.runKindDualGate = runKindDualGate;
4703
5078
  exports.runSchemaConversion = runSchemaConversion;
@@ -4709,6 +5084,9 @@ exports.schemaStructureDepth = schemaStructureDepth;
4709
5084
  exports.setJsonRootKeyLookup = setJsonRootKeyLookup;
4710
5085
  exports.storageToKindSchema = storageToKindSchema;
4711
5086
  exports.stripKindDeep = stripKindDeep;
5087
+ exports.titleCaseToken = titleCaseToken;
5088
+ exports.tryDecodeDirective = tryDecodeDirective;
5089
+ exports.tryDecodeDirectiveContent = tryDecodeDirectiveContent;
4712
5090
  exports.validateBlockSchemaSavePlan = validateBlockSchemaSavePlan;
4713
5091
  exports.validateStructuralLeg = validateStructuralLeg;
4714
5092
  exports.withRootKind = withRootKind;