File size: 1,179 Bytes
aa01edd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
import { usePubSub } from "create-pubsub/react";
import { useCallback } from "react";
import { settingsPubSub } from "../../../modules/pubSub";

export function useReasoningContent(text: string) {
  const [settings] = usePubSub(settingsPubSub);

  const extractReasoningAndMainContent = useCallback(
    (text: string, startMarker: string, endMarker: string) => {
      if (!text)
        return { reasoningContent: "", mainContent: "", isGenerating: false };

      if (!text.trim().startsWith(startMarker))
        return { reasoningContent: "", mainContent: text, isGenerating: false };

      const endIndex = text.indexOf(endMarker);

      if (endIndex === -1) {
        return {
          reasoningContent: text.slice(startMarker.length),
          mainContent: "",
          isGenerating: true,
        };
      }

      return {
        reasoningContent: text.slice(startMarker.length, endIndex),
        mainContent: text.slice(endIndex + endMarker.length),
        isGenerating: false,
      };
    },
    [],
  );

  const result = extractReasoningAndMainContent(
    text,
    settings.reasoningStartMarker,
    settings.reasoningEndMarker,
  );

  return result;
}