@sendbird/actionbook-core 0.9.1 → 0.9.7

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
@@ -4612,6 +4612,17 @@ function findDuplicateJumpPoints(doc2) {
4612
4612
 
4613
4613
  // src/tree/documentTree.ts
4614
4614
  var MAX_DEPTH6 = 128;
4615
+ var END_ACTION_RESOURCE_IDS = /* @__PURE__ */ new Set([
4616
+ "close-happy-tiger"
4617
+ // PredefinedToolKey.CloseConversation
4618
+ ]);
4619
+ var END_ACTION_TEXTS = /* @__PURE__ */ new Set([
4620
+ "end conversation",
4621
+ "end_conversation"
4622
+ ]);
4623
+ function isEndActionTag(tagType, resourceId, text2) {
4624
+ return tagType === "handoff" || END_ACTION_RESOURCE_IDS.has(resourceId) || END_ACTION_TEXTS.has(text2.toLowerCase());
4625
+ }
4615
4626
  function extractInlineItems(content, blockIndex) {
4616
4627
  const items = [];
4617
4628
  for (const node of content) {
@@ -4623,10 +4634,10 @@ function extractInlineItems(content, blockIndex) {
4623
4634
  children: []
4624
4635
  });
4625
4636
  } else if (node.type === "resourceTag") {
4626
- const isEndAction = node.tagType === "handoff";
4637
+ const endAction = isEndActionTag(node.tagType, node.resourceId, node.text);
4627
4638
  items.push({
4628
- type: isEndAction ? "endAction" : "resourceTag",
4629
- label: isEndAction ? `End ${node.text}` : node.text,
4639
+ type: endAction ? "endAction" : "resourceTag",
4640
+ label: endAction ? `End ${node.text}` : node.text,
4630
4641
  blockIndex,
4631
4642
  children: [],
4632
4643
  meta: { tagType: node.tagType, resourceId: node.resourceId }
@@ -4660,14 +4671,15 @@ function extractBlockItems(blocks, startIndex, depth) {
4660
4671
  break;
4661
4672
  }
4662
4673
  case "jinjaIfBlock": {
4663
- const branches = block.branches.map((branch) => {
4674
+ const branches = block.branches.map((branch, branchIdx) => {
4664
4675
  const branchLabel = branch.branchType === "else" ? "ELSE" : `${branch.branchType.toUpperCase()} ${branch.condition || ""}`.trim();
4665
4676
  const branchChildren = extractBlockItems(branch.content, blockIndex, depth + 1);
4666
4677
  return {
4667
4678
  type: "jinjaBranch",
4668
4679
  label: branchLabel,
4669
4680
  blockIndex,
4670
- children: branchChildren
4681
+ children: branchChildren,
4682
+ meta: { branchIndex: branchIdx }
4671
4683
  };
4672
4684
  });
4673
4685
  items.push({
@@ -4715,11 +4727,12 @@ function buildDocumentTree(doc2) {
4715
4727
  const hasStructuredJinja = items.some((n) => n.type === "jinjaIf");
4716
4728
  if (!hasStructuredJinja) {
4717
4729
  for (const s of structures) {
4718
- const branches = s.branches.map((b) => ({
4730
+ const branches = s.branches.map((b, branchIdx) => ({
4719
4731
  type: "jinjaBranch",
4720
4732
  label: b.type === "else" ? "ELSE" : `${b.type.toUpperCase()} ${b.condition || ""}`.trim(),
4721
4733
  blockIndex: b.tagBlockIndex,
4722
- children: []
4734
+ children: [],
4735
+ meta: { branchIndex: branchIdx }
4723
4736
  }));
4724
4737
  const jinjaNode = {
4725
4738
  type: "jinjaIf",
@@ -5640,6 +5653,20 @@ function findMatchingJinjaBranch(tree, containerNode, context) {
5640
5653
 
5641
5654
  // src/actionbookToAST.ts
5642
5655
  var EMPTY_DOC = { type: "doc", content: [{ type: "paragraph", content: [] }] };
5656
+ var JINJA_BLOCK_TAG_RE = /^\s*\{%\s*(if|elif|else|endif)\s/m;
5657
+ function needsJinjaReparse(doc2, instruction) {
5658
+ if (!instruction || !JINJA_BLOCK_TAG_RE.test(instruction)) return false;
5659
+ function hasJinjaBlock(nodes) {
5660
+ for (const node of nodes) {
5661
+ if (node.type === "jinjaIfBlock") return true;
5662
+ if ("content" in node && Array.isArray(node.content)) {
5663
+ if (hasJinjaBlock(node.content)) return true;
5664
+ }
5665
+ }
5666
+ return false;
5667
+ }
5668
+ return !hasJinjaBlock(doc2.content);
5669
+ }
5643
5670
  var ActionbookConversionError = class extends Error {
5644
5671
  constructor(message) {
5645
5672
  super(message);
@@ -5669,6 +5696,9 @@ function actionbookToAST(manual) {
5669
5696
  if (errors2.length > 0) {
5670
5697
  throw new ActionbookConversionError(`Schema validation failed: ${errors2[0].message}`);
5671
5698
  }
5699
+ if (v2.instruction && needsJinjaReparse(doc3, v2.instruction)) {
5700
+ return parseMarkdown(v2.instruction, { jinjaNodes: true });
5701
+ }
5672
5702
  return doc3;
5673
5703
  }
5674
5704
  const doc2 = fromProseMirrorJSON(obj);
@@ -5676,10 +5706,13 @@ function actionbookToAST(manual) {
5676
5706
  if (errors.length > 0) {
5677
5707
  throw new ActionbookConversionError(`Schema validation after PM conversion failed: ${errors[0].message}`);
5678
5708
  }
5709
+ if (v2.instruction && needsJinjaReparse(doc2, v2.instruction)) {
5710
+ return parseMarkdown(v2.instruction, { jinjaNodes: true });
5711
+ }
5679
5712
  return doc2;
5680
5713
  }
5681
5714
  if (v2.instruction) {
5682
- return parseMarkdown(v2.instruction);
5715
+ return parseMarkdown(v2.instruction, { jinjaNodes: true });
5683
5716
  }
5684
5717
  return EMPTY_DOC;
5685
5718
  }