11import { Brain } from "lucide-react" ;
2- import { useCallback , useEffect , useRef , useState } from "react" ;
2+ import { useCallback , useEffect , useRef , useState , useMemo } from "react" ;
33import { useTranslation } from "react-i18next" ;
44import clsx from "clsx" ;
55import { useKeyPress } from "ahooks" ;
@@ -23,8 +23,11 @@ import ConnectionError from "./ConnectionError";
2323import SearchIcons from "./SearchIcons" ;
2424import ChatIcons from "./ChatIcons" ;
2525// import AiSummaryIcon from "../Common/Icons/AiSummaryIcon";
26+ import { Post } from "@/api/axiosRequest" ;
27+ import platformAdapter from "@/utils/platformAdapter" ;
2628
2729interface ChatInputProps {
30+ isTauri : boolean ;
2831 onSend : ( message : string ) => void ;
2932 disabled : boolean ;
3033 disabledChange : ( ) => void ;
@@ -40,22 +43,6 @@ interface ChatInputProps {
4043 isMCPActive : boolean ;
4144 setIsMCPActive : ( ) => void ;
4245 isChatPage ?: boolean ;
43- getDataSourcesByServer : (
44- serverId : string ,
45- options ?: {
46- from ?: number ;
47- size ?: number ;
48- query ?: string ;
49- }
50- ) => Promise < DataSource [ ] > ;
51- getMCPByServer : (
52- serverId : string ,
53- options ?: {
54- from ?: number ;
55- size ?: number ;
56- query ?: string ;
57- }
58- ) => Promise < DataSource [ ] > ;
5946 setupWindowFocusListener : ( callback : ( ) => void ) => Promise < ( ) => void > ;
6047 checkScreenPermission : ( ) => Promise < boolean > ;
6148 requestScreenPermission : ( ) => void ;
@@ -75,6 +62,7 @@ interface ChatInputProps {
7562}
7663
7764export default function ChatInput ( {
65+ isTauri,
7866 onSend,
7967 disabled,
8068 changeMode,
@@ -90,8 +78,6 @@ export default function ChatInput({
9078 isMCPActive,
9179 setIsMCPActive,
9280 isChatPage = false ,
93- getDataSourcesByServer,
94- getMCPByServer,
9581 setupWindowFocusListener,
9682 hasModules = [ ] ,
9783 searchPlaceholder,
@@ -254,6 +240,156 @@ export default function ChatInput({
254240
255241 const source = currentAssistant ?. _source ;
256242
243+ const assistantConfig = useMemo ( ( ) => {
244+ return {
245+ datasourceEnabled : source ?. datasource ?. enabled ,
246+ datasourceVisible : source ?. datasource ?. visible ,
247+ datasourceIds : source ?. datasource ?. ids ,
248+ mcpEnabled : source ?. mcp_servers ?. enabled ,
249+ mcpVisible : source ?. mcp_servers ?. visible ,
250+ mcpIds : source ?. mcp_servers ?. ids ,
251+ } ;
252+ } , [ currentAssistant ] ) ;
253+
254+ const getDataSourcesByServer = useCallback (
255+ async (
256+ serverId : string ,
257+ options ?: {
258+ from ?: number ;
259+ size ?: number ;
260+ query ?: string ;
261+ }
262+ ) : Promise < DataSource [ ] > => {
263+ if (
264+ ! (
265+ assistantConfig . datasourceEnabled && assistantConfig . datasourceVisible
266+ )
267+ ) {
268+ return [ ] ;
269+ }
270+
271+ const body : Record < string , any > = {
272+ id : serverId ,
273+ from : options ?. from || 0 ,
274+ size : options ?. size || 1000 ,
275+ } ;
276+
277+ body . query = {
278+ bool : {
279+ must : [ { term : { enabled : true } } ] ,
280+ } ,
281+ } ;
282+
283+ if ( options ?. query ) {
284+ body . query . bool . must . push ( {
285+ query_string : {
286+ fields : [ "combined_fulltext" ] ,
287+ query : options ?. query ,
288+ fuzziness : "AUTO" ,
289+ fuzzy_prefix_length : 2 ,
290+ fuzzy_max_expansions : 10 ,
291+ fuzzy_transpositions : true ,
292+ allow_leading_wildcard : false ,
293+ } ,
294+ } ) ;
295+ }
296+
297+ let response : any ;
298+ if ( isTauri ) {
299+ response = await platformAdapter . invokeBackend ( "datasource_search" , {
300+ id : serverId ,
301+ options : body ,
302+ } ) ;
303+ } else {
304+ const [ error , res ] : any = await Post ( "/datasource/_search" , body ) ;
305+ if ( error ) {
306+ console . error ( "_search" , error ) ;
307+ return [ ] ;
308+ }
309+ response = res ?. hits ?. hits ?. map ( ( item : any ) => {
310+ return {
311+ ...item ,
312+ id : item . _source . id ,
313+ name : item . _source . name ,
314+ } ;
315+ } ) ;
316+ }
317+ let ids = assistantConfig . datasourceIds ;
318+ if ( Array . isArray ( ids ) && ids . length > 0 && ! ids . includes ( "*" ) ) {
319+ response = response ?. filter ( ( item : any ) => ids . includes ( item . id ) ) ;
320+ }
321+ return response || [ ] ;
322+ } ,
323+ [ assistantConfig ]
324+ ) ;
325+
326+ const getMCPByServer = useCallback (
327+ async (
328+ serverId : string ,
329+ options ?: {
330+ from ?: number ;
331+ size ?: number ;
332+ query ?: string ;
333+ }
334+ ) : Promise < DataSource [ ] > => {
335+ if ( ! ( assistantConfig . mcpEnabled && assistantConfig . mcpVisible ) ) {
336+ return [ ] ;
337+ }
338+ const body : Record < string , any > = {
339+ id : serverId ,
340+ from : options ?. from || 0 ,
341+ size : options ?. size || 1000 ,
342+ } ;
343+ body . query = {
344+ bool : {
345+ must : [ { term : { enabled : true } } ] ,
346+ } ,
347+ } ;
348+
349+ if ( options ?. query ) {
350+ body . query . bool . must . push ( {
351+ query_string : {
352+ fields : [ "combined_fulltext" ] ,
353+ query : options ?. query ,
354+ fuzziness : "AUTO" ,
355+ fuzzy_prefix_length : 2 ,
356+ fuzzy_max_expansions : 10 ,
357+ fuzzy_transpositions : true ,
358+ allow_leading_wildcard : false ,
359+ } ,
360+ } ) ;
361+ }
362+
363+ let response : any ;
364+ if ( isTauri ) {
365+ response = await platformAdapter . invokeBackend (
366+ "mcp_server_search" ,
367+ body
368+ ) ;
369+ } else {
370+ const [ error , res ] : any = await Post ( "/mcp_server/_search" , body ) ;
371+ if ( error ) {
372+ console . error ( "_search" , error ) ;
373+ return [ ] ;
374+ }
375+ response = res ?. hits ?. hits ?. map ( ( item : any ) => {
376+ return {
377+ ...item ,
378+ id : item . _source . id ,
379+ name : item . _source . name ,
380+ } ;
381+ } ) ;
382+ }
383+ let ids = assistantConfig . mcpIds ;
384+ if ( Array . isArray ( ids ) && ids . length > 0 && ! ids . includes ( "*" ) ) {
385+ response = response ?. filter ( ( item : any ) => ids . includes ( item . id ) ) ;
386+ }
387+ return response || [ ] ;
388+ } ,
389+ [ assistantConfig ]
390+ ) ;
391+
392+
257393 return (
258394 < div className = { `w-full relative` } >
259395 < div
0 commit comments