IntelliClass_FE/api/aifn.ts
Timothy Yin 92fc748a57
Some checks failed
CI / lint (push) Failing after 46s
CI / test (push) Failing after 1m20s
feat: 教学设计部分 UI
2025-04-26 19:29:50 +08:00

98 lines
2.4 KiB
TypeScript

import type { IResponse } from '.'
import type { AIGeneratedContent } from '~/components/ai'
export type AIGeneratedContentResponse = IResponse<{
data: AIGeneratedContent & {
raw: string
}
}>
export const AGCStream = async <ReqT>(
endpoint: string,
params: ReqT,
events: {
onTextChunk?: (message: string) => void
onComplete?: () => void
}) => {
const { onTextChunk: onMessage, onComplete } = events
const loginState = useLoginState()
const runtimeConfig = useRuntimeConfig()
const baseURL = runtimeConfig.public.baseURL as string
const response = await fetch(new URL(endpoint, baseURL), {
method: 'POST',
headers: {
'Content-Type': 'application/json',
'Accept': 'text/event-stream',
'Authorization': `Bearer ${loginState.token}`,
},
body: JSON.stringify(params),
})
if (!response) {
throw new Error('Network response was not ok')
}
const reader = response.body?.getReader()
const decoder = new TextDecoder('utf-8')
let buffer = ''
while (true) {
const { done, value } = await reader?.read() || {}
if (done) break
buffer += decoder.decode(value, { stream: true })
const parts = buffer.split('\n\n')
buffer = parts.pop()!
for (const part of parts) {
if (!part.trim().startsWith('data:')) continue
const payload = part.replace(/^data:\s*/, '').trim()
try {
const obj = JSON.parse(payload)
if (obj?.event && obj.event === 'workflow_finished') {
onComplete?.()
return
}
if (obj?.event && obj.event === 'text_chunk') {
let text_chunk = obj.data?.text as string
if (text_chunk) {
if (text_chunk.startsWith('<') && text_chunk.endsWith('>')) {
const endTag = text_chunk.match(/<\/[^>]*>/)
if (endTag) {
text_chunk = text_chunk.replace(endTag[0], '\n' + endTag[0])
}
}
onMessage?.(text_chunk)
}
}
} catch {
// ignore
}
}
}
onComplete?.()
}
export const generateLessonPlan = async (params: {
query: string
}) => {
return await http<AIGeneratedContentResponse>(`/ai/lesson-plan-design/text-block`, {
method: 'POST',
body: params,
})
}
export const generateCase = async (params: {
query: string
}) => {
return await http<AIGeneratedContentResponse>(`/ai/case-design/text-block`, {
method: 'POST',
body: params,
})
}