Commit 6a0a201c by zhangxingmin

push

parent 15b5f869
...@@ -12,14 +12,12 @@ ...@@ -12,14 +12,12 @@
"preview": "vite preview", "preview": "vite preview",
"report": "npm run build --report" "report": "npm run build --report"
}, },
"repository": {
"type": "git",
"url": "https://gitee.com/y_project/RuoYi-Vue.git"
},
"dependencies": { "dependencies": {
"@ai-sdk/alibaba": "^1.0.17",
"@element-plus/icons-vue": "2.3.1", "@element-plus/icons-vue": "2.3.1",
"@vueup/vue-quill": "1.2.0", "@vueup/vue-quill": "1.2.0",
"@vueuse/core": "13.3.0", "@vueuse/core": "13.3.0",
"ai": "^6.0.168",
"ali-oss": "^6.23.0", "ali-oss": "^6.23.0",
"axios": "1.9.0", "axios": "1.9.0",
"clipboard": "2.0.11", "clipboard": "2.0.11",
...@@ -34,7 +32,7 @@ ...@@ -34,7 +32,7 @@
"jsencrypt": "3.3.2", "jsencrypt": "3.3.2",
"jszip": "^3.10.1", "jszip": "^3.10.1",
"lodash-es": "^4.17.21", "lodash-es": "^4.17.21",
"marked": "^4.3.0", "marked": "^16.4.0",
"nprogress": "0.2.0", "nprogress": "0.2.0",
"p-limit": "^7.3.0", "p-limit": "^7.3.0",
"pinia": "3.0.2", "pinia": "3.0.2",
......
...@@ -13,6 +13,51 @@ export function randList(data) { ...@@ -13,6 +13,51 @@ export function randList(data) {
} }
/** /**
* 获取完整回答(非流式)
*/
export async function getFullAnswer(question, timeout = 300000) {
const userStore = useUserStore();
const token = getToken();
const tenantId = userStore.currentTenant?.apiLoginTenantInfoResponse?.tenantBizId;
const headers = {
'Authorization': `Bearer ${token}`
};
if (tenantId) {
headers['X-Tenant-ID'] = tenantId;
}
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const url = `${baseUrl}/ai/api/api/ai/stream?question=${encodeURIComponent(question)}`;
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), timeout);
try {
const response = await fetch(url, {
method: 'GET',
headers,
signal: controller.signal
});
clearTimeout(timeoutId);
if (!response.ok) {
let errorMessage = response.statusText;
try {
const errorBody = await response.json();
errorMessage = errorBody.msg || errorBody.message || errorMessage;
} catch (e) {}
throw new Error(errorMessage);
}
// 直接返回纯文本
return await response.text();
} finally {
clearTimeout(timeoutId);
}
}
/**
* 查询输出流信息(流式响应) * 查询输出流信息(流式响应)
* 增强错误处理:当HTTP状态非2xx时,解析错误响应体并抛出包含code属性的错误 * 增强错误处理:当HTTP状态非2xx时,解析错误响应体并抛出包含code属性的错误
*/ */
...@@ -30,7 +75,7 @@ export function getStream(question, timeout = 300000) { ...@@ -30,7 +75,7 @@ export function getStream(question, timeout = 300000) {
} }
const baseUrl = import.meta.env.VITE_APP_BASE_API const baseUrl = import.meta.env.VITE_APP_BASE_API
const url = `${baseUrl}/ai/api/api/ai/stream?question=${encodeURIComponent(question)}` const url = `${baseUrl}/ai/api/api/ai/stream-sse?question=${encodeURIComponent(question)}`;
const controller = new AbortController() const controller = new AbortController()
const timeoutId = setTimeout(() => controller.abort(), timeout) const timeoutId = setTimeout(() => controller.abort(), timeout)
...@@ -40,6 +85,7 @@ export function getStream(question, timeout = 300000) { ...@@ -40,6 +85,7 @@ export function getStream(question, timeout = 300000) {
headers, headers,
signal: controller.signal signal: controller.signal
}).then(async (response) => { }).then(async (response) => {
console.log(response)
clearTimeout(timeoutId) clearTimeout(timeoutId)
if (!response.ok) { if (!response.ok) {
// 尝试解析错误响应体 // 尝试解析错误响应体
...@@ -107,3 +153,71 @@ export async function getServiceCardList() { ...@@ -107,3 +153,71 @@ export async function getServiceCardList() {
throw error throw error
} }
} }
/**
* 启动流式生成,返回 sessionId
*/
export async function startStream(question) {
const userStore = useUserStore();
const token = getToken();
const tenantId = userStore.currentTenant?.apiLoginTenantInfoResponse?.tenantBizId;
const headers = {
'Authorization': `Bearer ${token}`
};
if (tenantId) {
headers['X-Tenant-ID'] = tenantId;
}
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const url = `${baseUrl}/ai/api/api/ai/start-stream`;
const formData = new FormData();
formData.append('question', question);
const controller = new AbortController();
const timeoutId = setTimeout(() => controller.abort(), 30000); // 30秒超时
try {
const response = await fetch(url, {
method: 'POST',
headers,
body: formData,
signal: controller.signal
});
clearTimeout(timeoutId);
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
const data = await response.json();
return data.sessionId;
} catch (error) {
clearTimeout(timeoutId);
throw error;
}
}
/**
* 轮询获取流式内容
*/
export async function pollContent(sessionId) {
const userStore = useUserStore();
const token = getToken();
const tenantId = userStore.currentTenant?.apiLoginTenantInfoResponse?.tenantBizId;
const headers = {
'Authorization': `Bearer ${token}`
};
if (tenantId) {
headers['X-Tenant-ID'] = tenantId;
}
const baseUrl = import.meta.env.VITE_APP_BASE_API;
const url = `${baseUrl}/ai/api/api/ai/stream-content?sessionId=${encodeURIComponent(sessionId)}`;
const response = await fetch(url, { headers });
if (!response.ok) {
throw new Error(`HTTP ${response.status}`);
}
return await response.json(); // { content: "...", finished: true/false }
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment