初始化模版工程
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>
|
||||
)
|
||||
}
|
||||
|
||||
|
||||
25
components/nova-sdk/context/context.ts
Normal file
25
components/nova-sdk/context/context.ts
Normal file
@@ -0,0 +1,25 @@
|
||||
import { createContext } from 'react'
|
||||
import type { NovaKitContextValue, NovaAPI } from '../types'
|
||||
import { TaskStatus } from '../types'
|
||||
|
||||
const defaultApiStub: NovaAPI = {
|
||||
apiClient: null,
|
||||
getArtifactUrl: async () => ({ data: '' }),
|
||||
stopChat: async () => {},
|
||||
getConversationInfoList: async () => ({ data: [] }),
|
||||
}
|
||||
|
||||
export const NovaKitContext = createContext<NovaKitContextValue>({
|
||||
agentId: '',
|
||||
agentName: 'Autonomous Agent',
|
||||
messageList: [],
|
||||
taskStatus: TaskStatus.PENDING,
|
||||
conversationId: null,
|
||||
isLoading: false,
|
||||
artifacts: [],
|
||||
api: defaultApiStub,
|
||||
setLoading: () => {},
|
||||
loading: false,
|
||||
})
|
||||
|
||||
|
||||
15
components/nova-sdk/context/useNovaKit.ts
Normal file
15
components/nova-sdk/context/useNovaKit.ts
Normal file
@@ -0,0 +1,15 @@
|
||||
import { useContext } from 'react'
|
||||
import { NovaKitContext } from './context'
|
||||
import type { NovaKitContextValue } from '../types'
|
||||
|
||||
/**
|
||||
* Hook 获取 NovaKit Context
|
||||
*/
|
||||
export function useNovaKit(): NovaKitContextValue {
|
||||
const context = useContext(NovaKitContext)
|
||||
if (!context) {
|
||||
throw new Error('useNovaKit must be used within a NovaKitProvider')
|
||||
}
|
||||
return context
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user