Skip to content

Commit 9dd3de8

Browse files
authored
Merge pull request #395 from miurla/refactor/improve-ui
Refactor/improve UI
2 parents 20d37e0 + af17278 commit 9dd3de8

File tree

6 files changed

+68
-18
lines changed

6 files changed

+68
-18
lines changed

app/api/chat/route.ts

+9-9
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,16 @@
1-
import {
2-
streamText,
3-
createDataStreamResponse,
4-
convertToCoreMessages,
5-
JSONValue
6-
} from 'ai'
7-
import { researcher } from '@/lib/agents/researcher'
8-
import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
9-
import { cookies } from 'next/headers'
101
import { getChat, saveChat } from '@/lib/actions/chat'
2+
import { generateRelatedQuestions } from '@/lib/agents/generate-related-questions'
3+
import { researcher } from '@/lib/agents/researcher'
114
import { ExtendedCoreMessage } from '@/lib/types'
125
import { convertToExtendedCoreMessages } from '@/lib/utils'
136
import { isProviderEnabled } from '@/lib/utils/registry'
7+
import {
8+
convertToCoreMessages,
9+
createDataStreamResponse,
10+
JSONValue,
11+
streamText
12+
} from 'ai'
13+
import { cookies } from 'next/headers'
1414

1515
export const maxDuration = 30
1616

components/chat-share.tsx

+1-1
Original file line numberDiff line numberDiff line change
@@ -73,7 +73,7 @@ export function ChatShare({ chatId, className }: ChatShareProps) {
7373
>
7474
<DialogTrigger asChild>
7575
<Button
76-
className={cn('rounded-full', isLoading && 'hidden')}
76+
className={cn('rounded-full', isLoading && 'invisible')}
7777
size="icon"
7878
variant={'ghost'}
7979
onClick={() => setOpen(true)}

components/chat.tsx

+6-1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import { CHAT_ID } from '@/lib/constants'
44
import { Message, useChat } from 'ai/react'
5+
import { useEffect } from 'react'
56
import { toast } from 'sonner'
67
import { ChatMessages } from './chat-messages'
78
import { ChatPanel } from './chat-panel'
@@ -38,6 +39,10 @@ export function Chat({
3839
}
3940
})
4041

42+
useEffect(() => {
43+
setMessages(savedMessages)
44+
}, [id])
45+
4146
const onQuerySelect = (query: string) => {
4247
append({
4348
role: 'user',
@@ -46,7 +51,7 @@ export function Chat({
4651
}
4752

4853
return (
49-
<div className="flex flex-col w-full max-w-3xl pt-10 pb-16 mx-auto stretch">
54+
<div className="flex flex-col w-full max-w-3xl pt-10 pb-20 mx-auto stretch">
5055
<ChatMessages
5156
messages={messages}
5257
onQuerySelect={onQuerySelect}

components/custom-link.tsx

+37
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import { cn } from '@/lib/utils'
2+
import { AnchorHTMLAttributes, DetailedHTMLProps, ReactNode } from 'react'
3+
4+
type CustomLinkProps = Omit<
5+
DetailedHTMLProps<AnchorHTMLAttributes<HTMLAnchorElement>, HTMLAnchorElement>,
6+
'ref'
7+
> & {
8+
children: ReactNode
9+
}
10+
11+
export function Citing({
12+
href,
13+
children,
14+
className,
15+
...props
16+
}: CustomLinkProps) {
17+
const childrenText = children?.toString() || ''
18+
const isNumber = /^\d+$/.test(childrenText)
19+
const linkClasses = cn(
20+
isNumber
21+
? 'text-[10px] bg-muted text-muted-froreground rounded-full w-4 h-4 px-0.5 inline-flex items-center justify-center hover:bg-muted/50 duration-200 no-underline -translate-y-0.5'
22+
: 'hover:underline inline-flex items-center gap-1.5',
23+
className
24+
)
25+
26+
return (
27+
<a
28+
href={href}
29+
target="_blank"
30+
rel="noopener noreferrer"
31+
className={linkClasses}
32+
{...props}
33+
>
34+
{children}
35+
</a>
36+
)
37+
}

components/message.tsx

+6-4
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,13 @@
11
'use client'
22

3-
import { MemoizedReactMarkdown } from './ui/markdown'
3+
import 'katex/dist/katex.min.css'
44
import rehypeExternalLinks from 'rehype-external-links'
5+
import rehypeKatex from 'rehype-katex'
56
import remarkGfm from 'remark-gfm'
67
import remarkMath from 'remark-math'
7-
import rehypeKatex from 'rehype-katex'
8-
import 'katex/dist/katex.min.css'
8+
import { Citing } from './custom-link'
99
import { CodeBlock } from './ui/codeblock'
10+
import { MemoizedReactMarkdown } from './ui/markdown'
1011

1112
export function BotMessage({ message }: { message: string }) {
1213
// Check if the content contains LaTeX patterns
@@ -67,7 +68,8 @@ export function BotMessage({ message }: { message: string }) {
6768
{...props}
6869
/>
6970
)
70-
}
71+
},
72+
a: Citing
7173
}}
7274
>
7375
{message}

lib/agents/researcher.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
import { CoreMessage, streamText } from 'ai'
2-
import { getModel } from '../utils/registry'
3-
import { searchTool } from '../tools/search'
42
import { retrieveTool } from '../tools/retrieve'
3+
import { searchTool } from '../tools/search'
54
import { videoSearchTool } from '../tools/video-search'
5+
import { getModel } from '../utils/registry'
6+
7+
const SYSTEM_PROMPT = `
8+
Instructions:
69
7-
const SYSTEM_PROMPT = `You are a helpful AI assistant with access to real-time web search, content retrieval, and video search capabilities.
10+
You are a helpful AI assistant with access to real-time web search, content retrieval, and video search capabilities.
811
When asked a question, you should:
912
1. Search for relevant information using the search tool when needed
1013
2. Use the retrieve tool to get detailed content from specific URLs
@@ -16,6 +19,9 @@ When asked a question, you should:
1619
8. Use markdown to structure your responses. Use headings to break up the content into sections.
1720
9. Include relevant images that support your explanations, but avoid using images frequently. Use images only when they actively aid the user's understanding.
1821
10. **Use the retrieve tool only with user-provided URLs.**
22+
23+
Citation Format:
24+
<cite_format>[number](url)</cite_format>
1925
`
2026

2127
type ResearcherReturn = Parameters<typeof streamText>[0]

0 commit comments

Comments
 (0)