Skip to content

Commit

Permalink
Merge pull request #66 from seemorg/dev
Browse files Browse the repository at this point in the history
Production deployment
  • Loading branch information
ahmedriad1 authored Feb 2, 2025
2 parents 5226590 + 64657a5 commit 48c5e7f
Show file tree
Hide file tree
Showing 3 changed files with 46 additions and 7 deletions.
19 changes: 16 additions & 3 deletions src/app/[locale]/t/[bookId]/_components/ai-tab/useChat.tsx
Original file line number Diff line number Diff line change
@@ -1,8 +1,9 @@
import { chatWithBook } from "@/server/services/chat";
import type { SemanticSearchBookNode } from "@/types/SemanticSearchBookNode";
import type { ChatResponse } from "@/types/chat";
import { useCallback } from "react";
import { useCallback, useMemo } from "react";
import { useChatStore } from "../../_stores/chat";
import { useBookDetails } from "../../_contexts/book-details.context";

type ChatMessage = {
id?: string;
Expand Down Expand Up @@ -79,6 +80,16 @@ export default function useChat({
bookId: string;
versionId: string;
}): UseChatResult {
const { bookResponse } = useBookDetails();
const aiVersion = useMemo(() => {
const currentVersion = bookResponse.book.versions.find(
(v) => v.id === versionId,
);

if (currentVersion?.aiSupported) return currentVersion.id;
return bookResponse.book.aiVersion;
}, [bookId, versionId, bookResponse]);

const messages = useChatStore((s) => s.messages);
const setMessages = useChatStore((s) => s.setMessages);
const question = useChatStore((s) => s.question);
Expand All @@ -102,6 +113,7 @@ export default function useChat({
try {
const { eventSource, messageId } = await chatWithBook({
bookId,
versionId: aiVersion,
question: q,
messages,
});
Expand All @@ -121,7 +133,7 @@ export default function useChat({

setIsPending(false);
// eslint-disable-next-line react-hooks/exhaustive-deps
}, [bookId, question]);
}, [bookId, question, aiVersion]);

const regenerateResponse = useCallback(
async (messageIndex?: number) => {
Expand Down Expand Up @@ -152,6 +164,7 @@ export default function useChat({
try {
const { eventSource, messageId } = await chatWithBook({
bookId,
versionId: aiVersion,
question: question.text,
messages: previousMessages,
});
Expand All @@ -170,7 +183,7 @@ export default function useChat({

setIsPending(false);
},
[bookId, messages],
[bookId, messages, aiVersion],
);

const clearChat = useCallback(() => {
Expand Down
29 changes: 26 additions & 3 deletions src/app/[locale]/t/[bookId]/_components/search-tab/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ import { VersionAlert } from "../version-alert";
import { Badge } from "@/components/ui/badge";
import { ChevronLeftIcon, ChevronRightIcon } from "lucide-react";
import { useSearchStore } from "../../_stores/search";
import { useState } from "react";
import { useMemo, useState } from "react";

import { useBookDetails } from "../../_contexts/book-details.context";
import { AzureSearchFilter, buildQuery } from "./search-filters";
Expand Down Expand Up @@ -45,6 +45,21 @@ export default function SearchTab() {
} = useSearchStore();
const [inputValue, setInputValue] = useState(value);

const searchVersion = useMemo(() => {
const currentVersion = bookResponse.book.versions.find(
(v) => v.id === bookResponse.content.id,
);

if (type === "semantic") {
// check ai version
if (currentVersion?.aiSupported) return currentVersion.id;
return bookResponse.book.aiVersion;
}

if (currentVersion?.keywordSupported) return currentVersion.id;
return bookResponse.book.keywordVersion;
}, [type, bookResponse]);

const isVersionMismatch =
type === "semantic"
? bookResponse.book.aiVersion !== bookResponse.content.id
Expand All @@ -60,13 +75,21 @@ export default function SearchTab() {
isLoading,
error,
} = useQuery({
queryKey: ["search", bookResponse.book.id, value, type, page] as const,
queryKey: [
"search",
bookResponse.book.id,
searchVersion,
value,
type,
page,
] as const,
queryFn: async ({ queryKey }) => {
const [, _bookId, _q, _type, _page] = queryKey;
const [, _bookId, _versionId, _q, _type, _page] = queryKey;
if (!_q) return null;

return await searchBook(
_bookId,
_versionId,
_q,
_type === "simple" || _type === "advanced" ? "keyword" : "semantic",
_page,
Expand Down
5 changes: 4 additions & 1 deletion src/server/services/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,12 +31,13 @@ const baseRequest = async <T>(

export const chatWithBook = async (body: {
bookId: string;
versionId: string;
question: string;
messages: any[];
}) => {
const response = await baseRequest<{ chatId: string }>(
"POST",
`/chat/${body.bookId}`,
`/chat/${body.bookId}/${body.versionId}`,
{
question: body.question,
messages: body.messages,
Expand Down Expand Up @@ -78,13 +79,15 @@ export type SearchBookResponse = {

export const searchBook = async (
bookId: string,
versionId: string,
query: string,
type: "semantic" | "keyword" = "semantic",
page: number = 1,
) => {
const queryParams = new URLSearchParams();
queryParams.set("q", query);
queryParams.set("bookId", bookId);
queryParams.set("versionId", versionId);
queryParams.set("type", type);
queryParams.set("page", page.toString());

Expand Down

0 comments on commit 48c5e7f

Please sign in to comment.