mirror of
https://github.com/docmost/docmost.git
synced 2026-05-07 14:43:06 +08:00
b76f5adaad
* feat(ee): AI menu * - Add insert below and copy option * prebuild @editor-ext * sanitize output * clear existing output * switch to menu component * refactor directory * separator * refactor directory * support more languages * pass markdown to model * fix: close AI menu on page change * enhance text input and preview styling * fix: Use absolute positioning for the AI menu * make preview scrollable * activation controls * enhance bubble menu * sync * set width * fix line break * switch terminologies * cloud * buffer --------- Co-authored-by: Philipinho <16838612+Philipinho@users.noreply.github.com>
47 lines
1.3 KiB
TypeScript
47 lines
1.3 KiB
TypeScript
import { useMutation, UseMutationResult } from "@tanstack/react-query";
|
|
import { useState, useCallback } from "react";
|
|
import { aiAnswers, IAiSearchResponse } from "@/ee/ai/services/ai-search-service.ts";
|
|
import { IPageSearchParams } from "@/features/search/types/search.types.ts";
|
|
|
|
// @ts-ignore
|
|
interface UseAiSearchResult extends UseMutationResult<IAiSearchResponse, Error, IPageSearchParams> {
|
|
streamingAnswer: string;
|
|
streamingSources: any[];
|
|
clearStreaming: () => void;
|
|
}
|
|
|
|
export function useAiSearch(): UseAiSearchResult {
|
|
const [streamingAnswer, setStreamingAnswer] = useState("");
|
|
const [streamingSources, setStreamingSources] = useState<any[]>([]);
|
|
|
|
const clearStreaming = useCallback(() => {
|
|
setStreamingAnswer("");
|
|
setStreamingSources([]);
|
|
}, []);
|
|
|
|
const mutation = useMutation({
|
|
mutationFn: async (params: IPageSearchParams & { contentType?: string }) => {
|
|
setStreamingAnswer("");
|
|
setStreamingSources([]);
|
|
|
|
const { contentType, ...apiParams } = params;
|
|
|
|
return await aiAnswers(apiParams, (chunk) => {
|
|
if (chunk.content) {
|
|
setStreamingAnswer((prev) => prev + chunk.content);
|
|
}
|
|
if (chunk.sources) {
|
|
setStreamingSources(chunk.sources);
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
return {
|
|
...mutation,
|
|
streamingAnswer,
|
|
streamingSources,
|
|
clearStreaming,
|
|
};
|
|
}
|