@paymanai/payman-ask-sdk 4.0.22 → 4.0.23

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.mjs CHANGED
@@ -1,3 +1,4 @@
1
+ import * as React from 'react';
1
2
  import { createContext, forwardRef, useCallback, useRef, useState, useMemo, useEffect, useImperativeHandle, useLayoutEffect, useContext } from 'react';
2
3
  import { AnimatePresence, motion } from 'framer-motion';
3
4
  import { clsx } from 'clsx';
@@ -3378,9 +3379,88 @@ function MarkdownImageV2({
3378
3379
  }
3379
3380
  ) });
3380
3381
  }
3381
- function buildComponents(onImageClick, isResolvingRef) {
3382
+ var RAG_SOURCE_CLASS = "payman-v2-rag-source";
3383
+ var RAG_FOLLOW_UP_CLASS = "payman-v2-rag-follow-up";
3384
+ var SOURCE_PATTERN = /^Sources?:\s*\S/i;
3385
+ var MAX_FOLLOW_UP_LENGTH = 320;
3386
+ var HR_PATTERN = /^-{3,}$|^_{3,}$|^\*{3,}$/;
3387
+ function normalizeRagParagraphText(text) {
3388
+ return text.replace(/\s+/g, " ").trim();
3389
+ }
3390
+ function isRagSourceParagraph(text) {
3391
+ return SOURCE_PATTERN.test(normalizeRagParagraphText(text));
3392
+ }
3393
+ function isRagFollowUpCandidate(text) {
3394
+ const normalized = normalizeRagParagraphText(text);
3395
+ return normalized.length > 0 && normalized.length <= MAX_FOLLOW_UP_LENGTH && normalized.endsWith("?");
3396
+ }
3397
+ function extractRagParagraphTexts(content) {
3398
+ const paragraphs = [];
3399
+ const currentParagraph = [];
3400
+ const flushParagraph = () => {
3401
+ const paragraph = normalizeRagParagraphText(currentParagraph.join(" "));
3402
+ currentParagraph.length = 0;
3403
+ if (paragraph) paragraphs.push(paragraph);
3404
+ };
3405
+ content.replace(/\\n/g, "\n").split(/\r?\n/).forEach((line) => {
3406
+ const trimmedLine = line.trim();
3407
+ if (!trimmedLine) {
3408
+ flushParagraph();
3409
+ return;
3410
+ }
3411
+ if (HR_PATTERN.test(trimmedLine)) {
3412
+ flushParagraph();
3413
+ return;
3414
+ }
3415
+ currentParagraph.push(trimmedLine);
3416
+ });
3417
+ flushParagraph();
3418
+ return paragraphs;
3419
+ }
3420
+ function getRagAnswerParagraphRoles(paragraphs) {
3421
+ const normalized = paragraphs.map(normalizeRagParagraphText);
3422
+ const roles = normalized.map(() => null);
3423
+ const sourceIndexes = normalized.map((paragraph, index) => isRagSourceParagraph(paragraph) ? index : -1).filter((index) => index >= 0);
3424
+ for (const sourceIndex of sourceIndexes) {
3425
+ roles[sourceIndex] = "source";
3426
+ for (const candidateIndex of [sourceIndex - 1, sourceIndex + 1]) {
3427
+ if (candidateIndex >= 0 && candidateIndex < normalized.length && roles[candidateIndex] == null && isRagFollowUpCandidate(normalized[candidateIndex] ?? "")) {
3428
+ roles[candidateIndex] = "follow-up";
3429
+ }
3430
+ }
3431
+ }
3432
+ return roles;
3433
+ }
3434
+ function getRagAnswerParagraphRole(paragraph, paragraphs) {
3435
+ const normalizedParagraph = normalizeRagParagraphText(paragraph);
3436
+ if (isRagSourceParagraph(normalizedParagraph)) return "source";
3437
+ const roles = getRagAnswerParagraphRoles(paragraphs);
3438
+ return paragraphs.map(normalizeRagParagraphText).some((candidate, index) => {
3439
+ return candidate === normalizedParagraph && roles[index] === "follow-up";
3440
+ }) ? "follow-up" : null;
3441
+ }
3442
+ function ragAnswerRoleClassName(role) {
3443
+ if (role === "source") return RAG_SOURCE_CLASS;
3444
+ if (role === "follow-up") return RAG_FOLLOW_UP_CLASS;
3445
+ return void 0;
3446
+ }
3447
+ function reactNodeText(node) {
3448
+ if (typeof node === "string" || typeof node === "number") return String(node);
3449
+ if (Array.isArray(node)) return node.map(reactNodeText).join("");
3450
+ if (React.isValidElement(node)) {
3451
+ return reactNodeText(node.props.children);
3452
+ }
3453
+ return "";
3454
+ }
3455
+ function buildComponents(onImageClick, isResolvingRef, ragParagraphsRef) {
3382
3456
  return {
3383
- p: ({ children }) => /* @__PURE__ */ jsx("p", { children }),
3457
+ p: ({ children }) => {
3458
+ const role = getRagAnswerParagraphRole(
3459
+ reactNodeText(children),
3460
+ ragParagraphsRef?.current ?? []
3461
+ );
3462
+ return /* @__PURE__ */ jsx("p", { className: ragAnswerRoleClassName(role), children });
3463
+ },
3384
3464
  code: ({ children }) => /* @__PURE__ */ jsx("code", { children }),
3385
3465
  pre: ({ children }) => /* @__PURE__ */ jsx("div", { className: "payman-v2-markdown-pre", children: /* @__PURE__ */ jsx("pre", { children }) }),
3386
3466
  ul: ({ children }) => /* @__PURE__ */ jsx("ul", { children }),
@@ -3418,8 +3498,13 @@ function MarkdownRendererV2({
3418
3498
  }) {
3419
3499
  const isResolvingRef = useRef(isResolvingImages);
3420
3500
  isResolvingRef.current = isResolvingImages;
3501
+ const ragParagraphsRef = useRef([]);
3502
+ ragParagraphsRef.current = useMemo(
3503
+ () => extractRagParagraphTexts(content),
3504
+ [content]
3505
+ );
3421
3506
  const components = useMemo(
3422
- () => buildComponents(onImageClick, isResolvingRef),
3507
+ () => buildComponents(onImageClick, isResolvingRef, ragParagraphsRef),
3423
3508
  [onImageClick]
3424
3509
  );
3425
3510
  return /* @__PURE__ */ jsx(