Files
test1/components/nova-sdk/context/NovaKitProvider.tsx
2026-03-20 07:33:46 +00:00

81 lines
1.9 KiB
TypeScript
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
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>
)
}