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