export function convertLuParseResultToLuFile()

in Composer/packages/lib/indexers/src/utils/luUtil.ts [75:198]


export function convertLuParseResultToLuFile(
  id: string,
  resource: LuParseResource,
  luFeatures: ILUFeaturesConfig,
  importResolver?: LUImportResolverDelegate
): LuFile {
  // filter structured-object from LUParser result.
  const { Sections, Errors, Content } = resource;
  const intents: LuIntentSection[] = [];
  const fileId = id;
  Sections.forEach((section) => {
    const { Name, Body, SectionType } = section;
    const range = new Range(
      new Position(get(section, 'Range.Start.Line', 0), get(section, 'Range.Start.Character', 0)),
      new Position(get(section, 'Range.End.Line', 0), get(section, 'Range.End.Character', 0))
    );
    if (SectionType === LuSectionTypes.SIMPLEINTENTSECTION) {
      const Entities = section.Entities.map(({ Name, Type }) => ({ Name, Type }));
      intents.push({ Name, Body, Entities, range, fileId });
    } else if (SectionType === LuSectionTypes.NESTEDINTENTSECTION) {
      const Children = section.SimpleIntentSections.map((subSection) => {
        const { Name, Body } = subSection;
        const range = new Range(
          new Position(get(section, 'Range.Start.Line', 0), get(subSection, 'Range.Start.Character', 0)),
          new Position(get(section, 'Range.End.Line', 0), get(subSection, 'Range.End.Character', 0))
        );
        const Entities = subSection.Entities.map(({ Name, Type }) => ({ Name, Type }));
        return { Name, Body, Entities, range, fileId };
      });
      intents.push({ Name, Body, Children, range, fileId });
      intents.push(
        ...Children.map((subSection) => {
          return {
            ...subSection,
            Name: `${section.Name}/${subSection.Name}`,
          };
        })
      );
    }
  });

  const appliedluFeatures = merge(defaultLUFeatures, luFeatures || {});
  if (luFeatures.isOrchestartor) {
    appliedluFeatures.enableCompositeEntities = false;
    appliedluFeatures.enableListEntities = false;
    appliedluFeatures.enableMLEntities = false;
    appliedluFeatures.enablePrebuiltEntities = false;
    appliedluFeatures.enableRegexEntities = false;
  }

  const syntaxDiagnostics = Errors.map((e) => convertLuDiagnostic(e, id)) as Diagnostic[];
  for (const item of syntaxDiagnostics) {
    const reseveredPrebuiltEntittyError = /.*The model name .* is reserved./g;
    if (reseveredPrebuiltEntittyError.test(item.message)) {
      item.severity = DiagnosticSeverity.Warning;
    }
  }

  const semanticDiagnostics = validateResource(resource, appliedluFeatures).map((e) =>
    convertLuDiagnostic(e, id)
  ) as Diagnostic[];

  const imports = Sections.filter(({ SectionType }) => SectionType === SectionTypes.ImportSection).map(
    ({ Path, Description }) => {
      return {
        id: getFileName(Path),
        description: Description,
        path: Path,
      };
    }
  );

  // find all reference and parse them.
  const allIntents: LuIntentSection[] = clone(intents);
  const referenceDiagnostics: Diagnostic[] = [];
  if (importResolver) {
    Sections.filter(({ SectionType }) => SectionType === SectionTypes.ImportSection).forEach((item) => {
      try {
        const targetFile = importResolver(id, item.Path);
        if (targetFile) {
          const targetFileParsed = luIndexer.parse(targetFile.content, targetFile.id, luFeatures, importResolver);
          allIntents.push(...targetFileParsed.allIntents);
        }
      } catch (_error) {
        const res = new Diagnostic(_error.message, item.Path, DiagnosticSeverity.Error, item.Path);
        const start: Position = item.Range
          ? new Position(item.Range.Start.Line, item.Range.Start.Character)
          : new Position(0, 0);
        const end: Position = item.Range
          ? new Position(item.Range.End.Line, item.Range.End.Character)
          : new Position(0, 0);
        res.range = new Range(start, end);
        referenceDiagnostics.push(res);
      }
    });
  }

  let diagnostics = syntaxDiagnostics.concat(semanticDiagnostics).concat(referenceDiagnostics);

  // if is orchestrator, report entity error
  if (luFeatures.isOrchestartor) {
    const orchestatorSemanticDiagnostics: Diagnostic[] = semanticDiagnostics.map((item) => {
      const matchResult = item.message.match(OrchestratorEntityPattern);
      if (matchResult) {
        item.message = `Orchestrator does not support ${matchResult[1]} entities.`;
        item.severity = DiagnosticSeverity.Warning;
      }
      return item;
    });
    diagnostics = syntaxDiagnostics.concat(orchestatorSemanticDiagnostics).concat(referenceDiagnostics);
  }

  return {
    id,
    content: Content,
    empty: !Sections.length,
    intents,
    allIntents,
    diagnostics,
    imports,
    resource: { Sections, Errors, Content },
    isContentUnparsed: false,
  };
}