From bdc9dffcd2c21aae89ffc115aa50fc770e09c8b3 Mon Sep 17 00:00:00 2001 From: Arik Chakma Date: Fri, 13 Jun 2025 13:28:52 +0600 Subject: [PATCH] wip --- .../GenerateGuide/GenerateAIGuide.tsx | 6 +- src/components/GenerateGuide/GetAIGuide.tsx | 2 +- src/helper/generate-ai-document.ts | 167 ----------------- src/helper/generate-ai-guide.ts | 173 ++++++++++++++++++ 4 files changed, 177 insertions(+), 171 deletions(-) delete mode 100644 src/helper/generate-ai-document.ts create mode 100644 src/helper/generate-ai-guide.ts diff --git a/src/components/GenerateGuide/GenerateAIGuide.tsx b/src/components/GenerateGuide/GenerateAIGuide.tsx index 78de4f8ef..d3eced2ed 100644 --- a/src/components/GenerateGuide/GenerateAIGuide.tsx +++ b/src/components/GenerateGuide/GenerateAIGuide.tsx @@ -1,6 +1,6 @@ import { useQuery } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; -import { generateDocument } from '../../helper/generate-ai-document'; +import { generateGuide } from '../../helper/generate-ai-guide'; import { getCourseFineTuneData } from '../../lib/ai'; import { getUrlParams } from '../../lib/browser'; import { isLoggedIn } from '../../lib/jwt'; @@ -8,9 +8,9 @@ import { getAiCourseOptions } from '../../queries/ai-course'; import { queryClient } from '../../stores/query-client'; import { AIDocumentContent } from './AIGuideContent'; -type GenerateAIDocumentProps = {}; +type GenerateAIGuideProps = {}; -export function GenerateAIDocument(props: GenerateAIDocumentProps) { +export function GenerateAIGuide(props: GenerateAIGuideProps) { const [term, setTerm] = useState(''); const [difficulty, setDifficulty] = useState(''); const [sessionId, setSessionId] = useState(''); diff --git a/src/components/GenerateGuide/GetAIGuide.tsx b/src/components/GenerateGuide/GetAIGuide.tsx index 248314c89..8ad82eaf4 100644 --- a/src/components/GenerateGuide/GetAIGuide.tsx +++ b/src/components/GenerateGuide/GetAIGuide.tsx @@ -1,6 +1,6 @@ import { useQuery } from '@tanstack/react-query'; import { useEffect, useState } from 'react'; -import { generateDocument } from '../../helper/generate-ai-document'; +import { generateDocument } from '../../helper/generate-ai-guide'; import { queryClient } from '../../stores/query-client'; import { getAiDocumentOptions } from '../../queries/ai-document'; import { AIDocumentContent } from './AIGuideContent'; diff --git a/src/helper/generate-ai-document.ts b/src/helper/generate-ai-document.ts deleted file mode 100644 index e9c28753c..000000000 --- a/src/helper/generate-ai-document.ts +++ /dev/null @@ -1,167 +0,0 @@ -import { - readStream -} from '../lib/ai'; -import { queryClient } from '../stores/query-client'; -import { getAiCourseLimitOptions } from '../queries/ai-course'; - -type GenerateDocumentOptions = { - term: string; - difficulty: string; - slug?: string; - isForce?: boolean; - prompt?: string; - instructions?: string; - goal?: string; - about?: string; - onDocumentIdChange?: (documentId: string) => void; - onDocumentSlugChange?: (documentSlug: string) => void; - onDocumentChange?: (document: string) => void; - onLoadingChange?: (isLoading: boolean) => void; - onCreatorIdChange?: (creatorId: string) => void; - onError?: (error: string) => void; - src?: string; -}; - -export async function generateDocument(options: GenerateDocumentOptions) { - const { - term, - slug, - difficulty, - onDocumentIdChange, - onDocumentSlugChange, - onDocumentChange, - onLoadingChange, - onError, - onCreatorIdChange, - isForce = false, - prompt, - instructions, - goal, - about, - src = 'search', - } = options; - - onLoadingChange?.(true); - onDocumentChange?.(''); - onError?.(''); - - try { - let response = null; - - if (slug && isForce) { - response = await fetch( - `${import.meta.env.PUBLIC_API_URL}/v1-regenerate-ai-document/${slug}`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - credentials: 'include', - body: JSON.stringify({ - isForce, - customPrompt: prompt, - }), - }, - ); - } else { - response = await fetch( - `${import.meta.env.PUBLIC_API_URL}/v1-generate-ai-document`, - { - method: 'POST', - headers: { - 'Content-Type': 'application/json', - }, - body: JSON.stringify({ - keyword: term, - difficulty, - isForce, - customPrompt: prompt, - instructions, - goal, - about, - src, - }), - credentials: 'include', - }, - ); - } - - if (!response.ok) { - const data = await response.json(); - console.error( - 'Error generating course:', - data?.message || 'Something went wrong', - ); - onLoadingChange?.(false); - onError?.(data?.message || 'Something went wrong'); - return; - } - - const reader = response.body?.getReader(); - - if (!reader) { - console.error('Failed to get reader from response'); - onError?.('Something went wrong'); - onLoadingChange?.(false); - return; - } - - const DOCUMENT_ID_REGEX = new RegExp('@DOCID:(\\w+)@'); - const DOCUMENT_SLUG_REGEX = new RegExp(/@DOCSLUG:([\w-]+)@/); - const CREATOR_ID_REGEX = new RegExp('@CREATORID:(\\w+)@'); - - await readStream(reader, { - onStream: async (result) => { - if (result.includes('@DOCID') || result.includes('@DOCSLUG')) { - const documentIdMatch = result.match(DOCUMENT_ID_REGEX); - const documentSlugMatch = result.match(DOCUMENT_SLUG_REGEX); - const creatorIdMatch = result.match(CREATOR_ID_REGEX); - const extractedDocumentId = documentIdMatch?.[1] || ''; - const extractedDocumentSlug = documentSlugMatch?.[1] || ''; - const extractedCreatorId = creatorIdMatch?.[1] || ''; - - if (extractedDocumentSlug) { - window.history.replaceState( - { - documentId: extractedDocumentId, - documentSlug: extractedDocumentSlug, - term, - difficulty, - }, - '', - `${origin}/ai/document/${extractedDocumentSlug}`, - ); - } - - result = result - .replace(DOCUMENT_ID_REGEX, '') - .replace(DOCUMENT_SLUG_REGEX, '') - .replace(CREATOR_ID_REGEX, ''); - - onDocumentIdChange?.(extractedDocumentId); - onDocumentSlugChange?.(extractedDocumentSlug); - onCreatorIdChange?.(extractedCreatorId); - } - - try { - onDocumentChange?.(result); - } catch (e) { - console.error('Error parsing streamed course content:', e); - } - }, - onStreamEnd: async (result) => { - result = result - .replace(DOCUMENT_ID_REGEX, '') - .replace(DOCUMENT_SLUG_REGEX, '') - .replace(CREATOR_ID_REGEX, ''); - - onLoadingChange?.(false); - queryClient.invalidateQueries(getAiCourseLimitOptions()); - }, - }); - } catch (error: any) { - onError?.(error?.message || 'Something went wrong'); - console.error('Error in course generation:', error); - onLoadingChange?.(false); - } -} diff --git a/src/helper/generate-ai-guide.ts b/src/helper/generate-ai-guide.ts new file mode 100644 index 000000000..eaae37069 --- /dev/null +++ b/src/helper/generate-ai-guide.ts @@ -0,0 +1,173 @@ +import { readStream } from '../lib/ai'; +import { queryClient } from '../stores/query-client'; +import { getAiCourseLimitOptions } from '../queries/ai-course'; + +type GenerateGuideOptions = { + term: string; + difficulty: string; + slug?: string; + isForce?: boolean; + prompt?: string; + instructions?: string; + goal?: string; + about?: string; + onDocumentIdChange?: (documentId: string) => void; + onDocumentSlugChange?: (documentSlug: string) => void; + onDocumentChange?: (document: string) => void; + onLoadingChange?: (isLoading: boolean) => void; + onCreatorIdChange?: (creatorId: string) => void; + onError?: (error: string) => void; + src?: string; +}; + +export async function generateGuide(options: GenerateGuideOptions) { + const { + term, + slug, + difficulty, + onDocumentIdChange, + onDocumentSlugChange, + onDocumentChange, + onLoadingChange, + onError, + onCreatorIdChange, + isForce = false, + prompt, + instructions, + goal, + about, + src = 'search', + } = options; + + onLoadingChange?.(true); + onDocumentChange?.(''); + onError?.(''); + + try { + let response = null; + + if (slug && isForce) { + response = await fetch( + `${import.meta.env.PUBLIC_API_URL}/v1-regenerate-ai-document/${slug}`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + credentials: 'include', + body: JSON.stringify({ + isForce, + customPrompt: prompt, + }), + }, + ); + } else { + response = await fetch( + `${import.meta.env.PUBLIC_API_URL}/v1-generate-ai-document`, + { + method: 'POST', + headers: { + 'Content-Type': 'application/json', + }, + body: JSON.stringify({ + keyword: term, + difficulty, + isForce, + customPrompt: prompt, + instructions, + goal, + about, + src, + }), + credentials: 'include', + }, + ); + } + + if (!response.ok) { + const data = await response.json(); + console.error( + 'Error generating course:', + data?.message || 'Something went wrong', + ); + onLoadingChange?.(false); + onError?.(data?.message || 'Something went wrong'); + return; + } + + // const reader = response.body?.getReader(); + + // if (!reader) { + // console.error('Failed to get reader from response'); + // onError?.('Something went wrong'); + // onLoadingChange?.(false); + // return; + // } + + // const DOCUMENT_ID_REGEX = new RegExp('@DOCID:(\\w+)@'); + // const DOCUMENT_SLUG_REGEX = new RegExp(/@DOCSLUG:([\w-]+)@/); + // const CREATOR_ID_REGEX = new RegExp('@CREATORID:(\\w+)@'); + + const stream = response.body; + if (!stream) { + console.error('Failed to get stream from response'); + onError?.('Something went wrong'); + onLoadingChange?.(false); + return; + } + + // await readStream(reader, { + // onStream: async (result) => { + // if (result.includes('@DOCID') || result.includes('@DOCSLUG')) { + // const documentIdMatch = result.match(DOCUMENT_ID_REGEX); + // const documentSlugMatch = result.match(DOCUMENT_SLUG_REGEX); + // const creatorIdMatch = result.match(CREATOR_ID_REGEX); + // const extractedDocumentId = documentIdMatch?.[1] || ''; + // const extractedDocumentSlug = documentSlugMatch?.[1] || ''; + // const extractedCreatorId = creatorIdMatch?.[1] || ''; + + // if (extractedDocumentSlug) { + // window.history.replaceState( + // { + // documentId: extractedDocumentId, + // documentSlug: extractedDocumentSlug, + // term, + // difficulty, + // }, + // '', + // `${origin}/ai/document/${extractedDocumentSlug}`, + // ); + // } + + // result = result + // .replace(DOCUMENT_ID_REGEX, '') + // .replace(DOCUMENT_SLUG_REGEX, '') + // .replace(CREATOR_ID_REGEX, ''); + + // onDocumentIdChange?.(extractedDocumentId); + // onDocumentSlugChange?.(extractedDocumentSlug); + // onCreatorIdChange?.(extractedCreatorId); + // } + + // try { + // onDocumentChange?.(result); + // } catch (e) { + // console.error('Error parsing streamed course content:', e); + // } + // }, + // onStreamEnd: async (result) => { + // result = result + // .replace(DOCUMENT_ID_REGEX, '') + // .replace(DOCUMENT_SLUG_REGEX, '') + // .replace(CREATOR_ID_REGEX, ''); + + // onLoadingChange?.(false); + // queryClient.invalidateQueries(getAiCourseLimitOptions()); + // }, + // }); + } catch (error: any) { + onError?.(error?.message || 'Something went wrong'); + console.error('Error in course generation:', error); + onLoadingChange?.(false); + } +}