File size: 4,040 Bytes
05c5ed5
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
import { Edge, useReactFlow } from "@xyflow/react";
import {
  OutputSchemaSourceKey,
  UINode,
} from "lib/ai/workflow/workflow.interface";
import { memo, useCallback, useMemo, useRef } from "react";

import { VariableSelectContent } from "./variable-select";
import { TipTapMentionJsonContent } from "app-types/util";
import MentionInput from "../mention-input";

import { generateUUID } from "lib/utils";
import { findAvailableSchemaBySource } from "lib/ai/workflow/shared.workflow";
import { VariableMentionItem } from "./variable-mention-item";
import { useToRef } from "@/hooks/use-latest";
import {
  DropdownMenu,
  DropdownMenuContent,
  DropdownMenuTrigger,
} from "ui/dropdown-menu";

interface OutputSchemaMentionInputProps {
  currentNodeId: string;
  nodes: UINode[];
  edges: Edge[];
  content?: TipTapMentionJsonContent;
  onChange: (content: TipTapMentionJsonContent) => void;
  placeholder?: string;
  editable?: boolean;
  className?: string;
}

export function OutputSchemaMentionInput({
  currentNodeId,
  content,
  onChange,
  editable,
  className,
}: OutputSchemaMentionInputProps) {
  const { getNodes, getEdges } = useReactFlow<UINode>();
  const latestContent = useToRef<TipTapMentionJsonContent>(content!);
  const handleChange = useCallback(
    ({ json }: { json: TipTapMentionJsonContent }) => {
      onChange(json);
    },
    [],
  );

  const onRemove = useCallback((id: string) => {
    const newContent = structuredClone(latestContent.current);
    newContent.content.some((item) => {
      if (item?.content?.length) {
        const targetIndex = item.content.findIndex(
          (item) => item.type == "mention" && item.attrs.id === id,
        );
        if (targetIndex !== -1) {
          item.content.splice(targetIndex, 1);
          return true;
        }
        return false;
      }
    });
    onChange(newContent);
  }, []);

  const MentionItem = useCallback(
    ({ id, label }: { id: string; label: string }) => {
      const item = JSON.parse(label) as OutputSchemaSourceKey;

      const nodes = getNodes();
      const edges = getEdges();

      const labelData = findAvailableSchemaBySource({
        nodeId: currentNodeId,
        source: item,
        nodes: nodes.map((node) => node.data),
        edges,
      });

      const handleRemove = () => onRemove(id);

      return (
        <VariableMentionItem
          className="max-w-60"
          {...labelData}
          onRemove={handleRemove}
        />
      );
    },
    [],
  );

  const Suggestion = useMemo(
    () => outputSchemaMentionInputSuggestionBuilder(currentNodeId),
    [currentNodeId],
  );

  return (
    <MentionInput
      className={className}
      suggestionChar="/"
      disabled={!editable}
      content={content}
      onChange={handleChange}
      MentionItem={MentionItem}
      Suggestion={Suggestion}
    />
  );
}

const outputSchemaMentionInputSuggestionBuilder = (
  nodeId: string,
): React.FC<{
  onClose: () => void;
  onSelectMention: (item: { label: string; id: string }) => void;
  top: number;
  left: number;
}> =>
  memo(function OutputSchemaMentionInputSuggestion({
    onSelectMention,
    top,
    left,
    onClose,
  }) {
    const mentionRef = useRef<HTMLDivElement>(null);

    return (
      <div
        className="fixed z-50"
        style={{
          top,
          left,
        }}
      >
        <DropdownMenu open={true} onOpenChange={onClose}>
          <DropdownMenuTrigger className="sr-only" />
          <DropdownMenuContent ref={mentionRef} align="start" side="top">
            <VariableSelectContent
              onClose={onClose}
              currentNodeId={nodeId}
              onChange={(item) => {
                onSelectMention({
                  id: generateUUID(),
                  label: JSON.stringify({
                    nodeId: item.nodeId,
                    path: item.path,
                  }),
                });
              }}
            />
          </DropdownMenuContent>
        </DropdownMenu>
      </div>
    );
  });