feat: 热点改进

This commit is contained in:
2026-02-25 01:24:57 +08:00
parent cd5a2f0c7d
commit 7285534405
8 changed files with 537 additions and 122 deletions

View File

@@ -0,0 +1,75 @@
/**
* Forecast 文案改写 API
*/
import { fetchEventSource } from '@microsoft/fetch-event-source'
import tokenManager from '@gold/utils/token-manager'
import { API_BASE } from '@gold/config/api'
const BASE_URL = `${API_BASE.APP_TIK}`
/**
* 流式文案改写SSE
* @param {Object} options - 请求配置
* @param {number} options.agentId - 智能体ID
* @param {string} options.userText - 用户输入文案
* @param {number} [options.level] - 改写级别/强度
* @param {string} [options.modelType] - 模型类型forecast_standard/forecast_meiju
* @param {AbortController} [options.ctrl] - 取消控制器
* @param {Function} options.onMessage - 消息回调
* @param {Function} [options.onError] - 错误回调
* @param {Function} [options.onClose] - 关闭回调
*/
export async function rewriteStream(options) {
const {
agentId,
userText,
level = 50,
modelType = 'forecast_standard',
ctrl,
onMessage,
onError,
onClose
} = options || {}
const token = tokenManager.getAccessToken()
return fetchEventSource(`${BASE_URL}/dify/forecast/rewrite`, {
method: 'post',
headers: {
'Content-Type': 'application/json',
'Authorization': `Bearer ${token}`,
'tenant-id': import.meta.env?.VITE_TENANT_ID
},
openWhenHidden: true,
body: JSON.stringify({
agentId,
userText,
level,
modelType
}),
onmessage: (event) => {
if (typeof onMessage === 'function') {
try {
const data = JSON.parse(event.data)
// 解析 CommonResult 包装
const result = data.code === 0 ? data.data : data
onMessage(result)
} catch (e) {
console.error('解析 SSE 数据失败:', e)
}
}
},
onerror: (err) => {
if (typeof onError === 'function') {
onError(err)
}
throw err // 不重试
},
onclose: () => {
if (typeof onClose === 'function') {
onClose()
}
},
signal: ctrl ? ctrl.signal : undefined
})
}