|
1 | 1 | import React, { createContext, useState, useContext, ReactNode, useEffect, useRef, useCallback } from 'react'; |
| 2 | +import { message } from 'antd'; |
2 | 3 | import { storageService } from '@/storage'; |
3 | 4 | import { useXAgent, useXChat } from '@ant-design/x'; |
4 | 5 | import type { MessageInfo } from '@ant-design/x/es/use-x-chat'; |
@@ -422,20 +423,26 @@ export const ChatProvider: React.FC<{ children: ReactNode }> = ({ children }) => |
422 | 423 |
|
423 | 424 | // Setup XAgent |
424 | 425 | const [agent] = useXAgent<any, { message: InputData }, ChatMessage>({ |
425 | | - request: async ({ message }, { onSuccess, onUpdate, onStream }) => { |
| 426 | + request: async ({ message: reqMessage }, { onSuccess, onUpdate, onStream }) => { |
426 | 427 | // Create AbortController for cancellation: create a new one for each request |
427 | 428 | if (onStream) { |
428 | 429 | onStream(new AbortController()); |
429 | 430 | } |
430 | | - const selectedModel = await storageService.getSelectedModelConfig(); |
431 | | - if (!selectedModel) { |
432 | | - throw new Error(t('context.model.noModelConfigured')); |
| 431 | + let selectedModel: ModelItem | null = null; |
| 432 | + try { |
| 433 | + selectedModel = await storageService.getSelectedModelConfig(); |
| 434 | + } catch (error) { |
| 435 | + const errorMsg = t('context.model.noModelConfigured'); |
| 436 | + message.error(errorMsg); |
| 437 | + handleApiError(error, onUpdate, onSuccess); |
| 438 | + return; |
433 | 439 | } |
| 440 | + |
434 | 441 | try { |
435 | 442 | // 0. First add an assistant message: can have loading state, but currently auto-scroll only works when second-to-last message is visible |
436 | 443 | onUpdate({ role: 'assistant', content: '', messages: [], status: 'loading' }); |
437 | 444 | // 1. Prepare context content |
438 | | - const { processResult, historyList, contextItems } = await prepareContextItem(message); |
| 445 | + const { processResult, historyList, contextItems } = await prepareContextItem(reqMessage); |
439 | 446 |
|
440 | 447 |
|
441 | 448 | // 2. Update last user message |
|
0 commit comments