初始化模版工程
This commit is contained in:
80
components/nova-sdk/context/NovaKitProvider.tsx
Normal file
80
components/nova-sdk/context/NovaKitProvider.tsx
Normal file
@@ -0,0 +1,80 @@
|
||||
import React, { useState, useEffect, useRef } from 'react'
|
||||
import type {
|
||||
NovaKitContextValue,
|
||||
ExtendedEvent,
|
||||
TaskStatus,
|
||||
NovaAPI,
|
||||
TaskArtifact,
|
||||
} from '../types'
|
||||
import { TaskStatus as Status } from '../types'
|
||||
import { NovaKitContext } from './context'
|
||||
|
||||
export interface NovaKitProviderProps {
|
||||
children: React.ReactNode
|
||||
/** 统一的 API 命名空间 */
|
||||
api: NovaAPI
|
||||
agentId: string
|
||||
agentName?: string
|
||||
conversationId?: string
|
||||
onMessage?: (event: ExtendedEvent) => void
|
||||
setLoading: (loading: boolean) => void
|
||||
loading: boolean
|
||||
mode?: 'chat' | 'share'
|
||||
panelMode?: 'sidebar' | 'dialog'
|
||||
}
|
||||
|
||||
/**
|
||||
* NovaKit Provider - 提供消息列表和状态管理
|
||||
*/
|
||||
export function NovaKitProvider({
|
||||
mode = 'chat',
|
||||
panelMode = 'sidebar',
|
||||
children,
|
||||
api,
|
||||
agentId,
|
||||
agentName,
|
||||
conversationId: propConversationId,
|
||||
setLoading,
|
||||
loading,
|
||||
}: NovaKitProviderProps) {
|
||||
const [messageList, setMessageList] = useState<ExtendedEvent[][]>([])
|
||||
const [taskStatus] = useState<TaskStatus>(Status.PENDING)
|
||||
const [isLoading ] = useState(false)
|
||||
const [artifacts, setArtifacts] = useState<TaskArtifact[]>([])
|
||||
const prevConversationIdRef = useRef<string | undefined>(propConversationId)
|
||||
|
||||
// 监听 conversationId 变化,重置相关状态
|
||||
useEffect(() => {
|
||||
if (propConversationId !== prevConversationIdRef.current) {
|
||||
prevConversationIdRef.current = propConversationId
|
||||
setMessageList([])
|
||||
setArtifacts([])
|
||||
}
|
||||
}, [propConversationId])
|
||||
|
||||
// 直接使用 prop,不需要内部 state
|
||||
const conversationId = propConversationId || null
|
||||
|
||||
const value: NovaKitContextValue = {
|
||||
api,
|
||||
agentId,
|
||||
agentName,
|
||||
panelMode,
|
||||
messageList: messageList,
|
||||
taskStatus,
|
||||
conversationId,
|
||||
isLoading,
|
||||
artifacts,
|
||||
loading,
|
||||
setLoading,
|
||||
mode,
|
||||
}
|
||||
|
||||
return (
|
||||
<NovaKitContext.Provider value={value}>
|
||||
{children}
|
||||
</NovaKitContext.Provider>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user