mirror of
https://github.com/kamranahmedse/developer-roadmap.git
synced 2026-11-29 02:17:42 +08:00
wip
This commit is contained in:
1 parent
697488b0b4
commit
bdc9dffcd2
4 files changed
+177
-171
No files matched your search
@@ -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('');
|
||||
|
||||
@@ -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';
|
||||
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user