Skip to content

Commit

Permalink
Add default assistant support in global model and AIChat components
Browse files Browse the repository at this point in the history
  • Loading branch information
trheyi committed Feb 9, 2025
1 parent 05d4a8f commit cf29dc1
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 15 deletions.
11 changes: 10 additions & 1 deletion packages/xgen/context/app/model.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ export default class GlobalModel {
layout: App.Layout = 'Admin'
theme: App.Theme = 'light'
avatar = {} as AvatarFullConfig
default_assistant = {} as App.AssistantSummary
locale_messages = {} as LocaleMessages
app_info = {} as App.Info
user = (local.user || {}) as App.User
Expand Down Expand Up @@ -67,6 +68,9 @@ export default class GlobalModel {
const layout = local.xgen_layout || res.optional?.layout || 'Admin'
this.setLayout(layout)

// Default Assistant
this.setDefaultAssistant(res.agent?.default || {})

return Promise.resolve()
}

Expand Down Expand Up @@ -141,6 +145,11 @@ export default class GlobalModel {
local.current_nav = current_nav
}

setDefaultAssistant(assistant: App.AssistantSummary) {
this.default_assistant = assistant
local.default_assistant = assistant
}

setInSetting(in_setting: boolean) {
this.in_setting = in_setting
local.in_setting = in_setting
Expand Down Expand Up @@ -177,7 +186,7 @@ export default class GlobalModel {
this.neo = neo
return
}
this.neo = { assistant_id: undefined, chat_id: undefined, placeholder: undefined }
this.neo = { chat_id: undefined, placeholder: undefined }
}

setNeoChatId(chat_id: string) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import Icon from '@/widgets/Icon'
import styles from './index.less'
import clsx from 'clsx'
import { App } from '@/types'
import { useGlobal } from '@/context/app'

interface AssistantProps {
assistant: App.AssistantSummary | undefined
Expand All @@ -10,8 +11,8 @@ interface AssistantProps {
}

const Assistant = ({ assistant, loading, onDelete }: AssistantProps) => {
const { assistant_id, assistant_name, assistant_avatar, assistant_deleteable } = assistant || {}
if (!assistant_id || assistant_id == '' || loading) {
const { assistant_name, assistant_avatar, assistant_deleteable } = assistant || {}
if (loading) {
return (
<div className={styles.assistantInfo}>
<div className={styles.avatarWrapper}>
Expand Down
14 changes: 4 additions & 10 deletions packages/xgen/layouts/components/Neo/components/AIChat/index.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ const AIChat = (props: AIChatProps) => {

const {
assistant,
loadingChat,
messages,
loading,
title,
Expand All @@ -65,6 +66,7 @@ const AIChat = (props: AIChatProps) => {
cancel,
uploadFile,
attachments,
resetAssistant,
removeAttachment,
addAttachment,
formatFileName,
Expand Down Expand Up @@ -481,14 +483,6 @@ const AIChat = (props: AIChatProps) => {
}
}

// Add mock data for assistant
const mockAssistant = {
assistant_id: assistant?.assistant_id || 'default-neo',
assistant_name: assistant?.assistant_name || 'Neo Assistant',
assistant_avatar: assistant?.assistant_avatar || 'https://api.dicebear.com/7.x/bottts/svg?seed=Neo'
// canDelete: false
}

return (
<div className={clsx(styles.aiChat, className)}>
{header || (
Expand Down Expand Up @@ -572,8 +566,8 @@ const AIChat = (props: AIChatProps) => {
<div className={styles.leftSection}>
<Assistant
assistant={assistant || {}}
loading={loading}
onDelete={() => {}}
loading={loadingChat}
onDelete={resetAssistant}
/>
{/* Current Page Info */}
{showCurrentPage && currentPage && (
Expand Down
35 changes: 33 additions & 2 deletions packages/xgen/layouts/components/Neo/hooks/useAIChat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -155,10 +155,11 @@ export default ({ chat_id, upload_options = {} }: Args) => {
const event_source = useRef<EventSource>()
const global = useGlobal()
const [messages, setMessages] = useState<Array<App.ChatInfo>>([])
const [assistant, setAssistant] = useState<App.AssistantSummary | undefined>(undefined)
const [assistant, setAssistant] = useState<App.AssistantSummary | undefined>(global.default_assistant)

const [title, setTitle] = useState<string>('')
const [loading, setLoading] = useState(false)
const [loadingChat, setLoadingChat] = useState(false)
const [attachments, setAttachments] = useState<App.ChatAttachment[]>([])
const [pendingCleanup, setPendingCleanup] = useState<App.ChatAttachment[]>([])
const uploadControllers = useRef<Map<string, AbortController>>(new Map())
Expand Down Expand Up @@ -642,6 +643,19 @@ export default ({ chat_id, upload_options = {} }: Args) => {
current_answer.assistant_avatar = last_assistant.assistant_avatar || undefined
current_answer.type = type || last_type || 'text'

// Update assistant info
if (
last_assistant.assistant_id &&
last_assistant.assistant_name &&
last_assistant.assistant_avatar
) {
updateAssistant({
...last_assistant,
assistant_deleteable:
last_assistant.assistant_id !== global.default_assistant.assistant_id
} as App.AssistantSummary)
}

if (done) {
if (text) {
current_answer.text = text
Expand Down Expand Up @@ -960,6 +974,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {

/** Get Single Chat **/
const getChat = useMemoizedFn(async (id?: string) => {
setLoadingChat(true)
if (!neo_api) return

const chatId = id || chat_id
Expand All @@ -970,10 +985,14 @@ export default ({ chat_id, upload_options = {} }: Args) => {
const [err, res] = await to<{ data: App.ChatDetail }>(axios.get(endpoint))
if (err) {
message_.error('Failed to fetch chat details')
setLoadingChat(false)
return
}

if (!res?.data) return null
if (!res?.data) {
setLoadingChat(false)
return null
}

const chatInfo = res.data
const formattedMessages: App.ChatInfo[] = []
Expand All @@ -989,6 +1008,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {

// Update assistant
updateAssistant(res.data.chat as App.AssistantSummary)
setLoadingChat(false)

return {
messages: formattedMessages,
Expand All @@ -1001,6 +1021,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {
assistant_id?: string
): Promise<{ chat_id: string; placeholder?: App.ChatPlaceholder; exist?: boolean } | null> {
if (!neo_api) return { chat_id: makeChatID(), placeholder: undefined, exist: false }
setLoadingChat(true)

const endpoint = `${neo_api}/chats/latest?token=${encodeURIComponent(getToken())}&assistant_id=${
assistant_id || ''
Expand All @@ -1011,6 +1032,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {
)
if (err) {
message_.error('Failed to fetch the latest chat')
setLoadingChat(false)
return null
}

Expand All @@ -1020,6 +1042,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {
if (typeof res.data === 'object' && 'placeholder' in res.data) {
// Update assistant
updateAssistant(res.data as App.AssistantSummary)
setLoadingChat(false)
return { chat_id: makeChatID(), placeholder: res.data.placeholder }
}

Expand All @@ -1038,12 +1061,18 @@ export default ({ chat_id, upload_options = {} }: Args) => {

// Update assistant
updateAssistant(chatInfo.chat)
setLoadingChat(false)

// Set chat_id
global.setNeoChatId(chatInfo.chat.chat_id)
return { chat_id: chatInfo.chat.chat_id, exist: true }
})

/** Reset assistant **/
const resetAssistant = useMemoizedFn(() => {
updateAssistant(global.default_assistant)
})

/** Update Chat **/
const updateChatByContent = useMemoizedFn(async (id: string, content: string) => {
if (!neo_api) return
Expand Down Expand Up @@ -1342,6 +1371,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {
return {
messages,
loading,
loadingChat,
assistant,
setMessages,
cancel,
Expand Down Expand Up @@ -1369,6 +1399,7 @@ export default ({ chat_id, upload_options = {} }: Args) => {
generateTitle,
generatePrompts,
titleGenerating,
resetAssistant,
setTitleGenerating,
getAssistants,
findAssistant,
Expand Down
5 changes: 5 additions & 0 deletions packages/xgen/types/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,11 @@ export declare namespace App {
/** Application mode */
mode?: 'development' | 'production' | 'test'

/** default assistant */
agent?: {
default?: AssistantSummary
}

optional?: {
/** default layout */
layout?: Layout
Expand Down

0 comments on commit cf29dc1

Please sign in to comment.