Elffuss BraidHub

kikocis/continue public

8d9577e85559 · 6.8 KiBRawPermalink
1import { Editor, ReactRenderer } from "@tiptap/react";
2import {
3 ContextProviderDescription,
4 ContextSubmenuItem,
5 ContextSubmenuItemWithProvider,
6} from "core";
7import { MutableRefObject } from "react";
8import tippy from "tippy.js";
9import { IIdeMessenger } from "../../../../context/IdeMessenger";
10import { AppDispatch } from "../../../../redux/store";
11import AtMentionDropdown from "../../AtMentionDropdown";
12import { ComboBoxItem, ComboBoxItemType, ComboBoxSubAction } from "../../types";
13import { TIPPY_DIV_ID } from "../TipTapEditor";
14import { SlashCommand } from "../extensions";
15
16function getSuggestion(
17 items: (props: { query: string }) => Promise<ComboBoxItem[]>,
18 enterSubmenu: (editor: Editor, providerId: string) => void = (editor) => {},
19 onClose: () => void = () => {},
20 onOpen: () => void = () => {},
21) {
22 return {
23 items,
24 allowSpaces: true,
25 render: () => {
26 let component: any;
27 let popup: any;
28
29 const onExit = () => {
30 popup?.[0]?.destroy();
31 component?.destroy();
32 onClose();
33 };
34
35 return {
36 onStart: (props: any) => {
37 component = new ReactRenderer(AtMentionDropdown, {
38 props: { ...props, enterSubmenu, onClose: onExit },
39 editor: props.editor,
40 });
41
42 if (!props.clientRect) {
43 console.log("no client rect");
44 return;
45 }
46
47 const container = document.getElementById(TIPPY_DIV_ID);
48
49 if (!container) {
50 console.log("no container");
51 return;
52 }
53
54 popup = tippy("body", {
55 getReferenceClientRect: props.clientRect,
56 appendTo: () => container,
57 content: component.element,
58 showOnCreate: true,
59 interactive: true,
60 trigger: "manual",
61 placement: "bottom-start",
62 maxWidth: `${window.innerWidth - 24}px`,
63 });
64
65 onOpen();
66 },
67
68 onUpdate(props: any) {
69 component.updateProps({ ...props, enterSubmenu });
70
71 if (!props.clientRect) {
72 return;
73 }
74
75 popup[0].setProps({
76 getReferenceClientRect: props.clientRect,
77 });
78 },
79
80 onKeyDown(props: any) {
81 if (props.event.key === "Escape") {
82 popup[0].hide();
83
84 return true;
85 }
86
87 return component.ref?.onKeyDown(props);
88 },
89
90 onExit,
91 };
92 },
93 };
94}
95
96function getSubActionsForSubmenuItem(
97 item: ContextSubmenuItem & { providerTitle: string },
98 ideMessenger: IIdeMessenger,
99): ComboBoxSubAction[] | undefined {
100 if (item.providerTitle === "docs") {
101 return [
102 {
103 label: "Open in new tab",
104 icon: "trash",
105 action: () => {
106 ideMessenger.post("context/removeDocs", { startUrl: item.id });
107 },
108 },
109 ];
110 }
111
112 return undefined;
113}
114
115export function getContextProviderDropdownOptions(
116 availableContextProvidersRef: MutableRefObject<ContextProviderDescription[]>,
117 getSubmenuContextItemsRef: MutableRefObject<
118 (
119 providerTitle: string | undefined,
120 query: string,
121 ) => ContextSubmenuItemWithProvider[]
122 >,
123 enterSubmenu: (editor: Editor, providerId: string) => void,
124 onClose: () => void,
125 onOpen: () => void,
126 inSubmenu: MutableRefObject<string | undefined>,
127 ideMessenger: IIdeMessenger,
128) {
129 const items = async ({ query }: { query: string }) => {
130 if (inSubmenu.current) {
131 const results = getSubmenuContextItemsRef.current(
132 inSubmenu.current,
133 query,
134 );
135 return results.map((result) => {
136 return {
137 ...result,
138 label: result.title,
139 type: inSubmenu.current as ComboBoxItemType,
140 query: result.id,
141 subActions: getSubActionsForSubmenuItem(result, ideMessenger),
142 };
143 });
144 }
145
146 const contextProviderMatches: ComboBoxItem[] =
147 availableContextProvidersRef.current
148 ?.filter(
149 (provider) =>
150 provider.title.toLowerCase().startsWith(query.toLowerCase()) ||
151 provider.displayTitle.toLowerCase().startsWith(query.toLowerCase()),
152 )
153 .map((provider) => ({
154 name: provider.displayTitle,
155 description: provider.description,
156 id: provider.title,
157 title: provider.displayTitle,
158 label: provider.displayTitle,
159 renderInlineAs: provider.renderInlineAs,
160 type: "contextProvider" as ComboBoxItemType,
161 contextProvider: provider,
162 }))
163 .sort((c, _) => (c.id === "file" ? -1 : 1)) || [];
164
165 if (contextProviderMatches.length) {
166 contextProviderMatches.push({
167 title: "Add more context providers",
168 type: "action",
169 action: () => {
170 ideMessenger.post(
171 "openUrl",
172 "https://docs.continue.dev/customization/context-providers#built-in-context-providers",
173 );
174 },
175 description: "",
176 });
177 return contextProviderMatches;
178 }
179
180 // No provider matches -> search all providers
181 const results = getSubmenuContextItemsRef.current(undefined, query);
182 return results.map((result) => {
183 return {
184 ...result,
185 label: result.title,
186 type: result.providerTitle as ComboBoxItemType,
187 query: result.id,
188 icon: result.icon,
189 };
190 });
191 };
192
193 return getSuggestion(items, enterSubmenu, onClose, onOpen);
194}
195
196export function getSlashCommandDropdownOptions(
197 availableSlashCommandsRef: MutableRefObject<ComboBoxItem[]>,
198 onClose: () => void,
199 onOpen: () => void,
200 ideMessenger: IIdeMessenger,
201 dispatch: AppDispatch,
202 inputId: string,
203) {
204 const items = async ({ query }: { query: string }) => {
205 const options = [...availableSlashCommandsRef.current];
206
207 const filteredCommands =
208 query.length > 0
209 ? options.filter((slashCommand) => {
210 const sc = slashCommand.title.toLowerCase();
211 const iv = query.toLowerCase();
212 return sc.startsWith(iv);
213 })
214 : options;
215
216 const commandItems = (filteredCommands || []).map((provider) => ({
217 name: provider.title,
218 description: provider.description,
219 id: provider.title,
220 title: provider.title,
221 label: provider.title,
222 type: (provider.type ?? SlashCommand.name) as ComboBoxItemType,
223 content: provider.content,
224 action: provider.action,
225 }));
226
227 if (query.length === 0 && commandItems.length === 0) {
228 commandItems.push({
229 title: "Explore prompts",
230 type: "action",
231 action: () =>
232 ideMessenger.post(
233 "openUrl",
234 "https://hub.continue.dev/explore/prompts",
235 ),
236 description: "",
237 name: "",
238 id: "",
239 label: "",
240 content: "",
241 });
242 }
243
244 return commandItems;
245 };
246 return getSuggestion(items, undefined, onClose, onOpen);
247}