feat: 联调对话功能
This commit is contained in:
+57
-3
@@ -32,7 +32,17 @@ export type TrainingSession = {
|
||||
patient_config: SessionPatientConfig
|
||||
}
|
||||
|
||||
type ApiEnvelope<T> = {
|
||||
export type CompleteInquiryResult = {
|
||||
session_id: number
|
||||
status: string
|
||||
}
|
||||
|
||||
export type StoredTrainingScenario = {
|
||||
session?: TrainingSession
|
||||
[key: string]: unknown
|
||||
}
|
||||
|
||||
export type ApiEnvelope<T> = {
|
||||
code: string
|
||||
message: string
|
||||
data: T
|
||||
@@ -56,7 +66,7 @@ function readAccessToken() {
|
||||
return token
|
||||
}
|
||||
|
||||
function authHeaders(accept = 'application/json') {
|
||||
export function authHeaders(accept = 'application/json') {
|
||||
return {
|
||||
'Content-Type': 'application/json',
|
||||
Accept: accept,
|
||||
@@ -65,7 +75,7 @@ function authHeaders(accept = 'application/json') {
|
||||
}
|
||||
}
|
||||
|
||||
async function readError(response: Response) {
|
||||
export async function readError(response: Response) {
|
||||
const text = await response.text().catch(() => '')
|
||||
if (!text) return `请求失败(${response.status})`
|
||||
try {
|
||||
@@ -76,6 +86,32 @@ async function readError(response: Response) {
|
||||
return text
|
||||
}
|
||||
|
||||
export function readStoredTrainingScenario() {
|
||||
const value = uni.getStorageSync('clinical-thinking-scenario')
|
||||
if (value && typeof value === 'object') return value as StoredTrainingScenario
|
||||
return null
|
||||
}
|
||||
|
||||
export function readActiveSessionId() {
|
||||
const sessionId = readStoredTrainingScenario()?.session?.session_id
|
||||
if (typeof sessionId === 'number' && Number.isInteger(sessionId) && sessionId > 0) {
|
||||
return sessionId
|
||||
}
|
||||
throw new Error('未找到当前会话,请先生成模拟场景')
|
||||
}
|
||||
|
||||
export function updateStoredSessionStatus(status: string) {
|
||||
const scenario = readStoredTrainingScenario()
|
||||
if (!scenario?.session) return
|
||||
uni.setStorageSync('clinical-thinking-scenario', {
|
||||
...scenario,
|
||||
session: {
|
||||
...scenario.session,
|
||||
status
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
export async function createTrainingSession(payload: CreateSessionPayload) {
|
||||
const response = await fetch(`${FASTAPI_BASE_URL}/sessions`, {
|
||||
method: 'POST',
|
||||
@@ -94,6 +130,24 @@ export async function createTrainingSession(payload: CreateSessionPayload) {
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function completeInquiry(sessionId: number) {
|
||||
const response = await fetch(`${FASTAPI_BASE_URL}/sessions/${sessionId}/complete-inquiry`, {
|
||||
method: 'POST',
|
||||
headers: authHeaders()
|
||||
})
|
||||
|
||||
if (!response.ok) {
|
||||
throw new Error(await readError(response))
|
||||
}
|
||||
|
||||
const result = (await response.json()) as ApiEnvelope<CompleteInquiryResult>
|
||||
if (result.code !== 'OK' || !result.data?.session_id) {
|
||||
throw new Error(result.message || '完成采集失败')
|
||||
}
|
||||
|
||||
return result.data
|
||||
}
|
||||
|
||||
export async function streamSessionChat(
|
||||
sessionId: number,
|
||||
message: string,
|
||||
|
||||
Reference in New Issue
Block a user