mirror of
https://github.com/hanzo-docs/docs.git
synced 2026-07-27 04:31:57 +00:00
Upstream changes include: - UI: improved TOC detection, clerk variant, slot support - Base UI: redesigned home layout navigation menu - CLI: slot support in customise command - OpenAPI: unsetting auth fields in playground - Multiple version package releases All fumadocs-* imports renamed to @hanzo/docs-* namespace.
456 lines
13 KiB
TypeScript
456 lines
13 KiB
TypeScript
'use client';
|
|
import {
|
|
type ComponentProps,
|
|
createContext,
|
|
type ReactNode,
|
|
type SyntheticEvent,
|
|
use,
|
|
useEffect,
|
|
useEffectEvent,
|
|
useMemo,
|
|
useRef,
|
|
useState,
|
|
} from 'react';
|
|
import { Loader2, MessageCircleIcon, RefreshCw, Send, X } from 'lucide-react';
|
|
import { cn } from '@/lib/cn';
|
|
import { buttonVariants } from '@hanzo/docs-base-ui/components/ui/button';
|
|
import Link from '@hanzo/docs-core/link';
|
|
import { type UIMessage, useChat, type UseChatHelpers } from '@ai-sdk/react';
|
|
import type { ProvideLinksToolSchema } from '@/lib/chat/inkeep-qa-schema';
|
|
import type { z } from 'zod';
|
|
import { DefaultChatTransport } from 'ai';
|
|
import { chatEndpoint, publishableKey } from '@/lib/hanzo/client';
|
|
import { Markdown } from './markdown';
|
|
import { Presence } from '@radix-ui/react-presence';
|
|
|
|
const Context = createContext<{
|
|
open: boolean;
|
|
setOpen: (open: boolean) => void;
|
|
chat: UseChatHelpers<UIMessage>;
|
|
} | null>(null);
|
|
|
|
export function AISearchPanelHeader({ className, ...props }: ComponentProps<'div'>) {
|
|
const { setOpen } = useAISearchContext();
|
|
|
|
return (
|
|
<div
|
|
className={cn(
|
|
'sticky top-0 flex items-start gap-2 border rounded-xl bg-fd-secondary text-fd-secondary-foreground shadow-sm',
|
|
className,
|
|
)}
|
|
{...props}
|
|
>
|
|
<div className="px-3 py-2 flex-1">
|
|
<p className="text-sm font-medium mb-2">AI Chat</p>
|
|
<p className="text-xs text-fd-muted-foreground">
|
|
Powered by{' '}
|
|
<a href="https://hanzo.ai" target="_blank" rel="noreferrer noopener">
|
|
Hanzo AI
|
|
</a>
|
|
</p>
|
|
</div>
|
|
|
|
<button
|
|
aria-label="Close"
|
|
tabIndex={-1}
|
|
className={cn(
|
|
buttonVariants({
|
|
size: 'icon-sm',
|
|
color: 'ghost',
|
|
className: 'text-fd-muted-foreground rounded-full',
|
|
}),
|
|
)}
|
|
onClick={() => setOpen(false)}
|
|
>
|
|
<X />
|
|
</button>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
export function AISearchInputActions() {
|
|
const { messages, status, setMessages, regenerate } = useChatContext();
|
|
const isLoading = status === 'streaming';
|
|
|
|
if (messages.length === 0) return null;
|
|
|
|
return (
|
|
<>
|
|
{!isLoading && messages.at(-1)?.role === 'assistant' && (
|
|
<button
|
|
type="button"
|
|
className={cn(
|
|
buttonVariants({
|
|
color: 'secondary',
|
|
size: 'sm',
|
|
className: 'rounded-full gap-1.5',
|
|
}),
|
|
)}
|
|
onClick={() => regenerate()}
|
|
>
|
|
<RefreshCw className="size-4" />
|
|
Retry
|
|
</button>
|
|
)}
|
|
<button
|
|
type="button"
|
|
className={cn(
|
|
buttonVariants({
|
|
color: 'secondary',
|
|
size: 'sm',
|
|
className: 'rounded-full',
|
|
}),
|
|
)}
|
|
onClick={() => setMessages([])}
|
|
>
|
|
Clear Chat
|
|
</button>
|
|
</>
|
|
);
|
|
}
|
|
|
|
const StorageKeyInput = '__ai_search_input';
|
|
export function AISearchInput(props: ComponentProps<'form'>) {
|
|
const { status, sendMessage, stop } = useChatContext();
|
|
const [input, setInput] = useState(() => localStorage.getItem(StorageKeyInput) ?? '');
|
|
const isLoading = status === 'streaming' || status === 'submitted';
|
|
const onStart = (e?: SyntheticEvent) => {
|
|
e?.preventDefault();
|
|
void sendMessage({ text: input });
|
|
setInput('');
|
|
};
|
|
|
|
localStorage.setItem(StorageKeyInput, input);
|
|
|
|
useEffect(() => {
|
|
if (isLoading) document.getElementById('nd-ai-input')?.focus();
|
|
}, [isLoading]);
|
|
|
|
return (
|
|
<form {...props} className={cn('flex items-start pe-2', props.className)} onSubmit={onStart}>
|
|
<Input
|
|
value={input}
|
|
placeholder={isLoading ? 'AI is answering...' : 'Ask a question'}
|
|
autoFocus
|
|
className="p-3"
|
|
disabled={status === 'streaming' || status === 'submitted'}
|
|
onChange={(e) => {
|
|
setInput(e.target.value);
|
|
}}
|
|
onKeyDown={(event) => {
|
|
if (!event.shiftKey && event.key === 'Enter') {
|
|
onStart(event);
|
|
}
|
|
}}
|
|
/>
|
|
{isLoading ? (
|
|
<button
|
|
key="bn"
|
|
type="button"
|
|
className={cn(
|
|
buttonVariants({
|
|
color: 'secondary',
|
|
className: 'transition-all rounded-full mt-2 gap-2',
|
|
}),
|
|
)}
|
|
onClick={stop}
|
|
>
|
|
<Loader2 className="size-4 animate-spin text-fd-muted-foreground" />
|
|
Abort Answer
|
|
</button>
|
|
) : (
|
|
<button
|
|
key="bn"
|
|
type="submit"
|
|
className={cn(
|
|
buttonVariants({
|
|
color: 'primary',
|
|
className: 'transition-all rounded-full mt-2',
|
|
}),
|
|
)}
|
|
disabled={input.length === 0}
|
|
>
|
|
<Send className="size-4" />
|
|
</button>
|
|
)}
|
|
</form>
|
|
);
|
|
}
|
|
|
|
function List(props: Omit<ComponentProps<'div'>, 'dir'>) {
|
|
const containerRef = useRef<HTMLDivElement>(null);
|
|
|
|
useEffect(() => {
|
|
if (!containerRef.current) return;
|
|
function callback() {
|
|
const container = containerRef.current;
|
|
if (!container) return;
|
|
|
|
container.scrollTo({
|
|
top: container.scrollHeight,
|
|
behavior: 'instant',
|
|
});
|
|
}
|
|
|
|
const observer = new ResizeObserver(callback);
|
|
callback();
|
|
|
|
const element = containerRef.current?.firstElementChild;
|
|
|
|
if (element) {
|
|
observer.observe(element);
|
|
}
|
|
|
|
return () => {
|
|
observer.disconnect();
|
|
};
|
|
}, []);
|
|
|
|
return (
|
|
<div
|
|
ref={containerRef}
|
|
{...props}
|
|
className={cn('fd-scroll-container overflow-y-auto min-w-0 flex flex-col', props.className)}
|
|
>
|
|
{props.children}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
function Input(props: ComponentProps<'textarea'>) {
|
|
const ref = useRef<HTMLDivElement>(null);
|
|
const shared = cn('col-start-1 row-start-1', props.className);
|
|
|
|
return (
|
|
<div className="grid flex-1">
|
|
<textarea
|
|
id="nd-ai-input"
|
|
{...props}
|
|
className={cn(
|
|
'resize-none bg-transparent placeholder:text-fd-muted-foreground focus-visible:outline-none',
|
|
shared,
|
|
)}
|
|
/>
|
|
<div ref={ref} className={cn(shared, 'break-all invisible')}>
|
|
{`${props.value?.toString() ?? ''}\n`}
|
|
</div>
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const roleName: Record<string, string> = {
|
|
user: 'you',
|
|
assistant: 'hanzo-bot',
|
|
};
|
|
|
|
function Message({ message, ...props }: { message: UIMessage } & ComponentProps<'div'>) {
|
|
let markdown = '';
|
|
let links: z.infer<typeof ProvideLinksToolSchema>['links'] = [];
|
|
|
|
for (const part of message.parts ?? []) {
|
|
if (part.type === 'text') {
|
|
markdown += part.text;
|
|
continue;
|
|
}
|
|
|
|
if (part.type === 'tool-provideLinks' && part.input) {
|
|
links = (part.input as z.infer<typeof ProvideLinksToolSchema>).links;
|
|
}
|
|
}
|
|
|
|
return (
|
|
<div onClick={(e) => e.stopPropagation()} {...props}>
|
|
<p
|
|
className={cn(
|
|
'mb-1 text-sm font-medium text-fd-muted-foreground',
|
|
message.role === 'assistant' && 'text-fd-primary',
|
|
)}
|
|
>
|
|
{roleName[message.role] ?? 'unknown'}
|
|
</p>
|
|
<div className="prose text-sm">
|
|
<Markdown text={markdown} />
|
|
</div>
|
|
{links && links.length > 0 && (
|
|
<div className="mt-2 flex flex-row flex-wrap items-center gap-1">
|
|
{links.map((item, i) => (
|
|
<Link
|
|
key={i}
|
|
href={item.url}
|
|
className="block text-xs rounded-lg border p-3 hover:bg-fd-accent hover:text-fd-accent-foreground"
|
|
>
|
|
<p className="font-medium">{item.title}</p>
|
|
<p className="text-fd-muted-foreground">Reference {item.label}</p>
|
|
</Link>
|
|
))}
|
|
</div>
|
|
)}
|
|
</div>
|
|
);
|
|
}
|
|
|
|
const systemPrompt =
|
|
'You are the documentation assistant for Hanzo Bot, a personal AI assistant by Hanzo AI (a Techstars-backed applied AI lab). ' +
|
|
'Hanzo Bot runs on your own devices and integrates with messaging platforms you already use. ' +
|
|
'It can run skills, automate tasks, browse the web, write code, and manage files — all locally. ' +
|
|
'Answer confidently and directly. Be concise. Use markdown. Include doc links when relevant.';
|
|
|
|
export function AISearch({ children }: { children: ReactNode }) {
|
|
const [open, setOpen] = useState(false);
|
|
const chat = useChat({
|
|
id: 'search',
|
|
initialMessages: [{ id: 'system', role: 'system' as const, content: systemPrompt, parts: [] }],
|
|
transport: new DefaultChatTransport({
|
|
api: chatEndpoint,
|
|
headers: { Authorization: `Bearer ${publishableKey}` },
|
|
}),
|
|
});
|
|
|
|
return (
|
|
<Context value={useMemo(() => ({ chat, open, setOpen }), [chat, open])}>{children}</Context>
|
|
);
|
|
}
|
|
|
|
export function AISearchTrigger({
|
|
position = 'default',
|
|
className,
|
|
...props
|
|
}: ComponentProps<'button'> & { position?: 'default' | 'float' }) {
|
|
const { open, setOpen } = useAISearchContext();
|
|
|
|
return (
|
|
<button
|
|
data-state={open ? 'open' : 'closed'}
|
|
className={cn(
|
|
position === 'float' && [
|
|
'fixed bottom-4 gap-3 w-24 end-[calc(--spacing(4)+var(--removed-body-scroll-bar-size,0px))] shadow-lg z-20 transition-[translate,opacity]',
|
|
open && 'translate-y-10 opacity-0',
|
|
],
|
|
className,
|
|
)}
|
|
onClick={() => setOpen(!open)}
|
|
{...props}
|
|
>
|
|
{props.children}
|
|
</button>
|
|
);
|
|
}
|
|
|
|
export function AISearchPanel() {
|
|
const { open, setOpen } = useAISearchContext();
|
|
useHotKey();
|
|
|
|
return (
|
|
<>
|
|
<style>
|
|
{`
|
|
@keyframes ask-ai-open {
|
|
from {
|
|
translate: 100% 0;
|
|
}
|
|
to {
|
|
translate: 0 0;
|
|
}
|
|
}
|
|
@keyframes ask-ai-close {
|
|
from {
|
|
width: var(--ai-chat-width);
|
|
}
|
|
to {
|
|
width: 0px;
|
|
}
|
|
}`}
|
|
</style>
|
|
<Presence present={open}>
|
|
<div
|
|
data-state={open ? 'open' : 'closed'}
|
|
className="fixed inset-0 z-30 backdrop-blur-xs bg-fd-overlay data-[state=open]:animate-fd-fade-in data-[state=closed]:animate-fd-fade-out lg:hidden"
|
|
onClick={() => setOpen(false)}
|
|
/>
|
|
</Presence>
|
|
<Presence present={open}>
|
|
<div
|
|
className={cn(
|
|
'overflow-hidden z-30 bg-fd-card text-fd-card-foreground [--ai-chat-width:400px] 2xl:[--ai-chat-width:460px]',
|
|
'max-lg:fixed max-lg:inset-x-2 max-lg:inset-y-4 max-lg:border max-lg:rounded-2xl max-lg:shadow-xl',
|
|
'lg:sticky lg:top-0 lg:h-dvh lg:border-s lg:ms-auto lg:in-[#nd-docs-layout]:[grid-area:toc] lg:in-[#nd-notebook-layout]:row-span-full lg:in-[#nd-notebook-layout]:col-start-5',
|
|
open
|
|
? 'animate-fd-dialog-in lg:animate-[ask-ai-open_200ms]'
|
|
: 'animate-fd-dialog-out lg:animate-[ask-ai-close_200ms]',
|
|
)}
|
|
>
|
|
<div className="flex flex-col size-full p-2 lg:p-3 lg:w-(--ai-chat-width)">
|
|
<AISearchPanelHeader />
|
|
<AISearchPanelList className="flex-1" />
|
|
<div className="rounded-xl border bg-fd-secondary text-fd-secondary-foreground shadow-sm has-focus-visible:shadow-md">
|
|
<AISearchInput />
|
|
<div className="flex items-center gap-1.5 p-1 empty:hidden">
|
|
<AISearchInputActions />
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</div>
|
|
</Presence>
|
|
</>
|
|
);
|
|
}
|
|
|
|
export function AISearchPanelList({ className, style, ...props }: ComponentProps<'div'>) {
|
|
const chat = useChatContext();
|
|
const messages = chat.messages.filter((msg) => msg.role !== 'system');
|
|
|
|
return (
|
|
<List
|
|
className={cn('py-4 overscroll-contain', className)}
|
|
style={{
|
|
maskImage:
|
|
'linear-gradient(to bottom, transparent, white 1rem, white calc(100% - 1rem), transparent 100%)',
|
|
...style,
|
|
}}
|
|
{...props}
|
|
>
|
|
{messages.length === 0 ? (
|
|
<div className="text-sm text-fd-muted-foreground/80 size-full flex flex-col items-center justify-center text-center gap-2">
|
|
<MessageCircleIcon fill="currentColor" stroke="none" />
|
|
<p onClick={(e) => e.stopPropagation()}>Start a new chat below.</p>
|
|
</div>
|
|
) : (
|
|
<div className="flex flex-col px-3 gap-4">
|
|
{messages.map((item) => (
|
|
<Message key={item.id} message={item} />
|
|
))}
|
|
</div>
|
|
)}
|
|
</List>
|
|
);
|
|
}
|
|
|
|
export function useHotKey() {
|
|
const { open, setOpen } = useAISearchContext();
|
|
|
|
const onKeyPress = useEffectEvent((e: KeyboardEvent) => {
|
|
if (e.key === 'Escape' && open) {
|
|
setOpen(false);
|
|
e.preventDefault();
|
|
}
|
|
|
|
if (e.key === '/' && (e.metaKey || e.ctrlKey) && !open) {
|
|
setOpen(true);
|
|
e.preventDefault();
|
|
}
|
|
});
|
|
|
|
useEffect(() => {
|
|
window.addEventListener('keydown', onKeyPress);
|
|
return () => window.removeEventListener('keydown', onKeyPress);
|
|
}, []);
|
|
}
|
|
|
|
export function useAISearchContext() {
|
|
return use(Context)!;
|
|
}
|
|
|
|
function useChatContext() {
|
|
return use(Context)!.chat;
|
|
}
|