mirror of
https://github.com/docmost/docmost.git
synced 2026-05-17 23:14:07 +08:00
9fb16bc842
* WIP * AI module - init * WIP * sync * WIP * refactor naming * new columns * sync * sync * fix search bug * stream response * WIP * feat embeddings sync * refine * Add workspaceId to page events * refine * WIP * add translation string * sync * reset ai answer on query change * hide AI search in cloud * capture streaming error * sync
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 { askAi, 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 askAi(apiParams, (chunk) => {
|
|
if (chunk.content) {
|
|
setStreamingAnswer((prev) => prev + chunk.content);
|
|
}
|
|
if (chunk.sources) {
|
|
setStreamingSources(chunk.sources);
|
|
}
|
|
});
|
|
},
|
|
});
|
|
|
|
return {
|
|
...mutation,
|
|
streamingAnswer,
|
|
streamingSources,
|
|
clearStreaming,
|
|
};
|
|
}
|