初始化模版工程

This commit is contained in:
Cloud Bot
2026-03-20 07:33:46 +00:00
commit 23717e0ecd
386 changed files with 51675 additions and 0 deletions

View 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>
)
}

View 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,
})

View 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
}