2026-01-15 06:01:34 +08:00
|
|
|
import { reactive } from 'vue'
|
2026-05-19 18:37:29 +08:00
|
|
|
import { normalizeQuestions } from '@/utils/questionUtils'
|
2026-03-07 23:28:25 +08:00
|
|
|
|
|
|
|
|
const extractQuestionPayload = (chunk) => {
|
|
|
|
|
const interruptInfo = chunk?.interrupt_info || {}
|
2026-03-17 03:39:48 +08:00
|
|
|
const rawQuestions = chunk?.questions || interruptInfo?.questions || []
|
2026-03-10 00:16:41 +08:00
|
|
|
const source = chunk?.source || interruptInfo?.source || 'interrupt'
|
2026-05-19 18:37:29 +08:00
|
|
|
const questions = normalizeQuestions(rawQuestions)
|
2026-03-07 23:28:25 +08:00
|
|
|
|
|
|
|
|
return {
|
2026-03-17 03:39:48 +08:00
|
|
|
questions,
|
|
|
|
|
source
|
2026-03-07 23:28:25 +08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2026-05-28 12:53:29 +08:00
|
|
|
export function useApproval({ getThreadState, fetchThreadMessages }) {
|
2025-11-01 21:34:16 +08:00
|
|
|
const approvalState = reactive({
|
|
|
|
|
showModal: false,
|
2026-03-17 03:39:48 +08:00
|
|
|
questions: [],
|
|
|
|
|
status: '',
|
2026-05-28 12:53:29 +08:00
|
|
|
threadId: null,
|
|
|
|
|
parentRunId: null
|
2026-01-15 06:01:34 +08:00
|
|
|
})
|
2025-11-01 21:34:16 +08:00
|
|
|
|
|
|
|
|
const processApprovalInStream = (chunk, threadId, currentAgentId) => {
|
2026-03-16 21:40:19 +08:00
|
|
|
if (
|
|
|
|
|
chunk.status !== 'ask_user_question_required' &&
|
|
|
|
|
chunk.status !== 'human_approval_required'
|
|
|
|
|
) {
|
2026-01-15 06:01:34 +08:00
|
|
|
return false
|
2025-11-01 21:34:16 +08:00
|
|
|
}
|
|
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
const threadState = getThreadState(threadId)
|
|
|
|
|
if (!threadState) return false
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2026-03-07 23:28:25 +08:00
|
|
|
const payload = extractQuestionPayload(chunk)
|
2026-03-17 03:39:48 +08:00
|
|
|
if (!payload.questions.length) return false
|
2026-03-07 23:28:25 +08:00
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
threadState.isStreaming = false
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2026-01-15 06:01:34 +08:00
|
|
|
approvalState.showModal = true
|
2026-03-17 03:39:48 +08:00
|
|
|
approvalState.questions = payload.questions
|
|
|
|
|
approvalState.status = chunk.status || ''
|
2026-01-15 06:01:34 +08:00
|
|
|
approvalState.threadId = chunk.thread_id || threadId
|
2026-05-28 12:53:29 +08:00
|
|
|
approvalState.parentRunId = chunk.run_id || null
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2026-03-07 23:28:25 +08:00
|
|
|
fetchThreadMessages({ agentId: currentAgentId, threadId })
|
2025-11-01 21:34:16 +08:00
|
|
|
|
2026-03-07 23:28:25 +08:00
|
|
|
return true
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-11-01 21:34:16 +08:00
|
|
|
|
|
|
|
|
const resetApprovalState = () => {
|
2026-01-15 06:01:34 +08:00
|
|
|
approvalState.showModal = false
|
2026-03-17 03:39:48 +08:00
|
|
|
approvalState.questions = []
|
|
|
|
|
approvalState.status = ''
|
2026-01-15 06:01:34 +08:00
|
|
|
approvalState.threadId = null
|
2026-05-28 12:53:29 +08:00
|
|
|
approvalState.parentRunId = null
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
2025-11-01 21:34:16 +08:00
|
|
|
|
|
|
|
|
return {
|
|
|
|
|
approvalState,
|
|
|
|
|
processApprovalInStream,
|
|
|
|
|
resetApprovalState
|
2026-01-15 06:01:34 +08:00
|
|
|
}
|
|
|
|
|
}
|