Compare commits
8 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
| c521e14320 | |||
| f72371e0bb | |||
| 480029439f | |||
| 473c9b1e8e | |||
| dd044148c1 | |||
| 80ebd18099 | |||
| a038e7eaae | |||
| f6436d0c83 |
@@ -35,7 +35,7 @@ const config: Config = {
|
|||||||
markdown: {
|
markdown: {
|
||||||
mermaid: true,
|
mermaid: true,
|
||||||
},
|
},
|
||||||
plugins: [docuservix],
|
plugins: [docuservix()],
|
||||||
themes: ['@docusaurus/theme-mermaid'],
|
themes: ['@docusaurus/theme-mermaid'],
|
||||||
|
|
||||||
// Future flags, see https://docusaurus.io/docs/api/docusaurus-config#future
|
// Future flags, see https://docusaurus.io/docs/api/docusaurus-config#future
|
||||||
|
|||||||
@@ -0,0 +1,88 @@
|
|||||||
|
import { useLocation } from '@docusaurus/router';
|
||||||
|
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||||
|
|
||||||
|
import { useOptions } from '@docuservix/hooks/useOptions';
|
||||||
|
import { IChat, IChatMessage, IChatSource } from '@docuservix/models/chat';
|
||||||
|
|
||||||
|
interface UseChatResult {
|
||||||
|
dialog: IChat;
|
||||||
|
typing: boolean;
|
||||||
|
statusMessage?: string;
|
||||||
|
sendMessage: (text: string) => void;
|
||||||
|
}
|
||||||
|
|
||||||
|
function useQuery(): string {
|
||||||
|
const location = useLocation();
|
||||||
|
const params = new URLSearchParams(location.search);
|
||||||
|
|
||||||
|
return params.get('q') ?? '';
|
||||||
|
}
|
||||||
|
|
||||||
|
export function useChat(): UseChatResult {
|
||||||
|
const chatEndpoint = useOptions().api + '/chat';
|
||||||
|
const urlQuery = useQuery();
|
||||||
|
|
||||||
|
const [messages, setMessages] = useState<IChatMessage[]>([]);
|
||||||
|
const [loading, setLoading] = useState(false);
|
||||||
|
const [error, setError] = useState<string | null>(null);
|
||||||
|
|
||||||
|
const initialSentRef = useRef(false);
|
||||||
|
const messagesEndRef = useRef(messages);
|
||||||
|
|
||||||
|
messagesEndRef.current = messages;
|
||||||
|
|
||||||
|
const sendMessage = useCallback(
|
||||||
|
async (text: string) => {
|
||||||
|
const content = text.trim();
|
||||||
|
|
||||||
|
if (!content) {
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
|
const userMessage: IChatMessage = { role: 'user', content };
|
||||||
|
const newHistory = [...messagesEndRef.current, userMessage];
|
||||||
|
|
||||||
|
setMessages(newHistory);
|
||||||
|
setLoading(true);
|
||||||
|
setError(null);
|
||||||
|
|
||||||
|
try {
|
||||||
|
const res = await fetch(chatEndpoint, {
|
||||||
|
method: 'POST',
|
||||||
|
headers: { 'Content-Type': 'application/json' },
|
||||||
|
body: JSON.stringify({ messages: newHistory }),
|
||||||
|
});
|
||||||
|
|
||||||
|
if (!res.ok) {
|
||||||
|
throw new Error(`HTTP ${res.status}`);
|
||||||
|
}
|
||||||
|
|
||||||
|
const data: { answer: string; sources?: IChatSource[] } = await res.json();
|
||||||
|
|
||||||
|
setMessages((prev) => [
|
||||||
|
...prev,
|
||||||
|
{ role: 'assistant', content: data.answer, sources: data.sources },
|
||||||
|
]);
|
||||||
|
} catch (err) {
|
||||||
|
setError(err instanceof Error ? err.message : 'Ошибка при обращении к серверу');
|
||||||
|
} finally {
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
},
|
||||||
|
[chatEndpoint],
|
||||||
|
);
|
||||||
|
|
||||||
|
useEffect(() => {
|
||||||
|
if (urlQuery && !initialSentRef.current) {
|
||||||
|
initialSentRef.current = true;
|
||||||
|
sendMessage(urlQuery);
|
||||||
|
}
|
||||||
|
}, [urlQuery, sendMessage]);
|
||||||
|
|
||||||
|
return {
|
||||||
|
dialog: { messages },
|
||||||
|
typing: loading,
|
||||||
|
statusMessage: error ?? undefined,
|
||||||
|
sendMessage,
|
||||||
|
};
|
||||||
|
}
|
||||||
@@ -0,0 +1,7 @@
|
|||||||
|
import { usePluginData } from '@docusaurus/useGlobalData';
|
||||||
|
|
||||||
|
import { DocuservixOptions } from '@docuservix/models/docuservix';
|
||||||
|
|
||||||
|
export function useOptions(): DocuservixOptions {
|
||||||
|
return usePluginData('docuservix') as DocuservixOptions;
|
||||||
|
}
|
||||||
@@ -2,7 +2,12 @@ import path from 'path';
|
|||||||
|
|
||||||
import type { LoadContext, Plugin } from '@docusaurus/types';
|
import type { LoadContext, Plugin } from '@docusaurus/types';
|
||||||
|
|
||||||
function pluginDocuservix(_context: LoadContext): Plugin {
|
import { DocuservixOptions } from '@docuservix/models/docuservix';
|
||||||
|
|
||||||
|
export default function docuservix(options: Partial<DocuservixOptions> = {}) {
|
||||||
|
const { api = '/api' } = options;
|
||||||
|
|
||||||
|
return function pluginDocuservix(_context: LoadContext): Plugin {
|
||||||
return {
|
return {
|
||||||
name: 'docuservix',
|
name: 'docuservix',
|
||||||
|
|
||||||
@@ -17,7 +22,11 @@ function pluginDocuservix(_context: LoadContext): Plugin {
|
|||||||
},
|
},
|
||||||
|
|
||||||
async contentLoaded({ actions }) {
|
async contentLoaded({ actions }) {
|
||||||
const { addRoute } = actions;
|
const { addRoute, setGlobalData } = actions;
|
||||||
|
|
||||||
|
setGlobalData({
|
||||||
|
api,
|
||||||
|
});
|
||||||
|
|
||||||
addRoute({
|
addRoute({
|
||||||
path: '/chat',
|
path: '/chat',
|
||||||
@@ -26,6 +35,5 @@ function pluginDocuservix(_context: LoadContext): Plugin {
|
|||||||
});
|
});
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
};
|
||||||
}
|
}
|
||||||
|
|
||||||
export default pluginDocuservix;
|
|
||||||
|
|||||||
@@ -2,7 +2,36 @@ export interface IChat {
|
|||||||
messages: IChatMessage[];
|
messages: IChatMessage[];
|
||||||
}
|
}
|
||||||
|
|
||||||
|
export interface IChatSource {
|
||||||
|
file: string;
|
||||||
|
heading: string;
|
||||||
|
anchor: string;
|
||||||
|
score: number;
|
||||||
|
}
|
||||||
|
|
||||||
export interface IChatMessage {
|
export interface IChatMessage {
|
||||||
role: 'user' | 'assistant';
|
role: 'user' | 'assistant';
|
||||||
content: string;
|
content: string;
|
||||||
|
sources?: IChatSource[];
|
||||||
|
}
|
||||||
|
|
||||||
|
function stripNumericPrefixes(p: string): string {
|
||||||
|
return p
|
||||||
|
.split('/')
|
||||||
|
.map((seg) => seg.replace(/^\d+-/, ''))
|
||||||
|
.join('/');
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sourceToUrl(file: string, anchor: string): string {
|
||||||
|
let p = file.replace(/^docs\//, '').replace(/\.md$/, '');
|
||||||
|
|
||||||
|
p = stripNumericPrefixes(p);
|
||||||
|
|
||||||
|
return `/docs/${p}${anchor ? `#${anchor}` : ''}`;
|
||||||
|
}
|
||||||
|
|
||||||
|
export function sourceToPath(file: string): string {
|
||||||
|
const p = file.replace(/^docs\//, '').replace(/\.md$/, '');
|
||||||
|
|
||||||
|
return stripNumericPrefixes(p);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -0,0 +1,3 @@
|
|||||||
|
export interface DocuservixOptions {
|
||||||
|
api?: string;
|
||||||
|
}
|
||||||
@@ -1,30 +1,20 @@
|
|||||||
import Layout from '@theme/Layout';
|
import Layout from '@theme/Layout';
|
||||||
import { ReactNode } from 'react';
|
import { ReactNode } from 'react';
|
||||||
|
|
||||||
import { IChat } from '@docuservix/models/chat';
|
import { useChat } from '@docuservix/hooks/useChat';
|
||||||
import { Chat } from '@docuservix/widgets/chat';
|
import { Chat } from '@docuservix/widgets/chat';
|
||||||
|
|
||||||
const dialog: IChat = {
|
|
||||||
messages: [
|
|
||||||
{
|
|
||||||
role: 'user',
|
|
||||||
content: 'Can you show me some CSS animations? It can be simple tools like chatbots...',
|
|
||||||
},
|
|
||||||
{
|
|
||||||
role: 'assistant',
|
|
||||||
content: "Hello! I'm your AI assistant. How can I help you today?",
|
|
||||||
},
|
|
||||||
],
|
|
||||||
};
|
|
||||||
|
|
||||||
export function ChatPage(): ReactNode {
|
export function ChatPage(): ReactNode {
|
||||||
|
const { dialog, typing, statusMessage, sendMessage } = useChat();
|
||||||
|
|
||||||
return (
|
return (
|
||||||
<Layout title="Чат">
|
<Layout title="Чат">
|
||||||
<main className="container margin-vert--lg">
|
<main className="container margin-vert--lg">
|
||||||
<Chat
|
<Chat
|
||||||
dialog={dialog}
|
dialog={dialog}
|
||||||
statusMessage="Unable to connect to the server"
|
typing={typing}
|
||||||
typing
|
statusMessage={statusMessage}
|
||||||
|
onSend={sendMessage}
|
||||||
/>
|
/>
|
||||||
</main>
|
</main>
|
||||||
</Layout>
|
</Layout>
|
||||||
|
|||||||
@@ -34,6 +34,35 @@
|
|||||||
border-bottom-right-radius: 0.25rem;
|
border-bottom-right-radius: 0.25rem;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
.Message__sources {
|
||||||
|
margin-top: 8px;
|
||||||
|
padding: 8px 1rem 0;
|
||||||
|
border-top: 1px solid var(--ifm-color-emphasis-200);
|
||||||
|
}
|
||||||
|
|
||||||
|
.Message__sourcesLabel {
|
||||||
|
margin-bottom: 4px;
|
||||||
|
color: var(--ifm-color-emphasis-600);
|
||||||
|
font-weight: var(--ifm-font-weight-semibold);
|
||||||
|
font-size: 0.75rem;
|
||||||
|
letter-spacing: 0.04em;
|
||||||
|
text-transform: uppercase;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Message__sourceLink {
|
||||||
|
display: block;
|
||||||
|
overflow: hidden;
|
||||||
|
color: var(--ifm-color-primary);
|
||||||
|
font-size: 0.8rem;
|
||||||
|
white-space: nowrap;
|
||||||
|
text-decoration: none;
|
||||||
|
text-overflow: ellipsis;
|
||||||
|
}
|
||||||
|
|
||||||
|
.Message__sourceLink:hover {
|
||||||
|
text-decoration: underline;
|
||||||
|
}
|
||||||
|
|
||||||
@media (min-width: 576px) {
|
@media (min-width: 576px) {
|
||||||
.Message {
|
.Message {
|
||||||
max-width: 75%;
|
max-width: 75%;
|
||||||
|
|||||||
@@ -1,6 +1,9 @@
|
|||||||
|
import Link from '@docusaurus/Link';
|
||||||
import block from 'bem-css-modules';
|
import block from 'bem-css-modules';
|
||||||
import React, { ReactNode } from 'react';
|
import React, { ReactNode } from 'react';
|
||||||
|
|
||||||
|
import { IChatSource, sourceToPath, sourceToUrl } from '@docuservix/models/chat';
|
||||||
|
|
||||||
import styles from './Message.module.css';
|
import styles from './Message.module.css';
|
||||||
|
|
||||||
const b = block(styles, 'Message');
|
const b = block(styles, 'Message');
|
||||||
@@ -8,12 +11,28 @@ const b = block(styles, 'Message');
|
|||||||
interface MessageProps {
|
interface MessageProps {
|
||||||
role: 'user' | 'assistant';
|
role: 'user' | 'assistant';
|
||||||
content: string;
|
content: string;
|
||||||
|
sources?: IChatSource[];
|
||||||
}
|
}
|
||||||
|
|
||||||
export function Message({ role, content }: MessageProps): ReactNode {
|
export function Message({ role, content, sources }: MessageProps): ReactNode {
|
||||||
return (
|
return (
|
||||||
<div className={b({ role })}>
|
<div className={b({ role })}>
|
||||||
<div className={b('content')}>{content}</div>
|
<div className={b('content')}>{content}</div>
|
||||||
|
|
||||||
|
{sources && sources.length > 0 && (
|
||||||
|
<div className={b('sources')}>
|
||||||
|
<div className={b('sourcesLabel')}>Источники:</div>
|
||||||
|
{sources.map((src, j) => (
|
||||||
|
<Link
|
||||||
|
key={j}
|
||||||
|
to={sourceToUrl(src.file, src.anchor)}
|
||||||
|
className={b('sourceLink')}
|
||||||
|
>
|
||||||
|
{src.heading || sourceToPath(src.file)}
|
||||||
|
</Link>
|
||||||
|
))}
|
||||||
|
</div>
|
||||||
|
)}
|
||||||
</div>
|
</div>
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -21,6 +21,7 @@ export function Messages({ messages, typing }: MessagesProps): ReactNode {
|
|||||||
key={i}
|
key={i}
|
||||||
role={msg.role}
|
role={msg.role}
|
||||||
content={msg.content}
|
content={msg.content}
|
||||||
|
sources={msg.sources}
|
||||||
/>
|
/>
|
||||||
))}
|
))}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user