This commit is contained in:
parent
0b9998528f
commit
710b6ebdfc
|
|
@ -11,12 +11,7 @@ import { Section } from '@/components/mpv2/section'
|
|||
import { FollowupPanel } from '@/components/mpv2/followup-panel'
|
||||
import { inquire, researcher, taskManager, querySuggestor } from '@/lib/agents'
|
||||
|
||||
const initialAIState: {
|
||||
role: 'user' | 'assistant' | 'system' | 'function' | 'tool'
|
||||
content: string
|
||||
id?: string
|
||||
name?: string
|
||||
}[] = []
|
||||
const initialAIState: ExperimentalMessage[] = []
|
||||
|
||||
const initialUIState: {
|
||||
id: number
|
||||
|
|
@ -24,24 +19,13 @@ const initialUIState: {
|
|||
component: React.ReactNode
|
||||
}[] = []
|
||||
|
||||
// ① 先声明 submit(不引用 AI)
|
||||
let submit: any
|
||||
|
||||
// ② 先生成 AI,再写 submit
|
||||
export const {
|
||||
AIProvider,
|
||||
useAIState,
|
||||
useUIState,
|
||||
useActions
|
||||
} = createAI({
|
||||
const AIProvider = createAI({
|
||||
actions: {
|
||||
submit: async function(formData?: FormData, skip?: boolean) {
|
||||
'use server'
|
||||
|
||||
const aiState = getMutableAIState<typeof AIProvider>() // 这里用 AIProvider
|
||||
const aiState = getMutableAIState<typeof AIProvider>()
|
||||
const uiStream = createStreamableUI()
|
||||
const isGenerating = createStreamableValue(true)
|
||||
|
||||
const messages: ExperimentalMessage[] = aiState.get() as any
|
||||
const userInput = skip
|
||||
? `{"action": "skip"}`
|
||||
|
|
@ -60,10 +44,8 @@ export const {
|
|||
|
||||
async function processEvents() {
|
||||
uiStream.update(<Spinner />)
|
||||
|
||||
let action: any = { object: { next: 'proceed' } }
|
||||
if (!skip) action = await taskManager(messages)
|
||||
|
||||
if (action.object.next === 'inquire') {
|
||||
const inquiry = await inquire(uiStream, messages)
|
||||
uiStream.done()
|
||||
|
|
@ -74,7 +56,6 @@ export const {
|
|||
])
|
||||
return
|
||||
}
|
||||
|
||||
let answer = ''
|
||||
let errorOccurred = false
|
||||
const streamText = createStreamableValue<string>()
|
||||
|
|
@ -88,7 +69,6 @@ export const {
|
|||
errorOccurred = hasError
|
||||
}
|
||||
streamText.done()
|
||||
|
||||
if (!errorOccurred) {
|
||||
await querySuggestor(uiStream, messages)
|
||||
uiStream.append(
|
||||
|
|
@ -97,14 +77,11 @@ export const {
|
|||
</Section>
|
||||
)
|
||||
}
|
||||
|
||||
isGenerating.done(false)
|
||||
uiStream.done()
|
||||
aiState.done([...aiState.get(), { role: 'assistant', content: answer }])
|
||||
}
|
||||
|
||||
processEvents()
|
||||
|
||||
return {
|
||||
id: Date.now(),
|
||||
isGenerating: isGenerating.value,
|
||||
|
|
@ -115,3 +92,5 @@ export const {
|
|||
initialUIState,
|
||||
initialAIState
|
||||
})
|
||||
|
||||
export default AIProvider
|
||||
|
|
|
|||
Loading…
Reference in New Issue