Skip to content
项目
群组
代码片段
帮助
当前项目
正在载入...
登录 / 注册
切换导航面板
R
risk-monitor
项目
项目
详情
活动
周期分析
仓库
仓库
文件
提交
分支
标签
贡献者
图表
比较
统计图
议题
0
议题
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
CI / CD
CI / CD
流水线
作业
日程
统计图
Wiki
Wiki
代码片段
代码片段
成员
成员
折叠边栏
关闭边栏
活动
图像
聊天
创建新问题
作业
提交
问题看板
Open sidebar
蔡建
risk-monitor
Commits
2117fce3
提交
2117fce3
authored
11月 13, 2025
作者:
coderBryanFu
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
update
上级
377f30c9
隐藏空白字符变更
内嵌
并排
正在显示
2 个修改的文件
包含
84 行增加
和
83 行删除
+84
-83
AiBox.vue
src/components/AiBox.vue
+73
-73
index.vue
src/views/writtingAsstaint/index.vue
+11
-10
没有找到文件。
src/components/AiBox.vue
浏览文件 @
2117fce3
...
...
@@ -58,7 +58,7 @@
</div>
<div
class=
"text"
>
概括页面内容
</div>
</div>
<div
class=
"submit"
>
<div
class=
"submit"
@
click=
"sendMessage"
>
<img
src=
"@/assets/icons/aiBox/submit.png"
alt=
""
/>
</div>
</div>
...
...
@@ -126,78 +126,78 @@ const updateLastAIMessage = content => {
};
// 使用 fetchEventSource 连接
//
const connectSSE = async question => {
//
// 添加用户消息
//
addMessage("user", question);
//
// 添加空的 AI 消息用于流式更新
//
addMessage("ai", "");
//
isLoading.value = true;
//
// 创建 AbortController 用于取消请求
//
abortController.value = new AbortController();
//
try {
//
await fetchEventSource("/api/chat-stream", {
//
method: "POST",
//
headers: {
//
"Content-Type": "application/json"
//
},
//
body: JSON.stringify({
//
question: question
//
// 其他参数...
//
}),
//
signal: abortController.value.signal,
//
onopen: async response => {
//
console.log("SSE 连接已建立", response.status);
//
if (response.status !== 200) {
//
throw new Error(`请求失败: ${response.status}`);
//
}
//
},
//
onmessage: event => {
//
try {
//
if (event.data === "[DONE]") {
//
// 流式输出结束
//
isLoading.value = false;
//
return;
//
}
//
const data = JSON.parse(event.data);
//
if (data.type === "content" && data.content) {
//
// 流式更新内容
//
updateLastAIMessage(prev => prev + data.content);
//
} else if (data.type === "error") {
//
throw new Error(data.message || "请求失败");
//
}
//
} catch (error) {
//
console.error("解析 SSE 数据错误:", error);
//
}
//
},
//
onclose: () => {
//
console.log("SSE 连接已关闭");
//
isLoading.value = false;
//
},
//
onerror: error => {
//
console.error("SSE 连接错误:", error);
//
ElMessage.error("连接失败,请重试");
//
isLoading.value = false;
//
// 不要抛出错误,否则会重试
//
}
//
});
//
} catch (error) {
//
console.error("SSE 请求失败:", error);
//
if (error.name !== "AbortError") {
//
ElMessage.error(error.message || "请求失败");
//
}
//
isLoading.value = false;
//
}
//
};
const
connectSSE
=
async
question
=>
{
// 添加用户消息
addMessage
(
"user"
,
question
);
// 添加空的 AI 消息用于流式更新
addMessage
(
"ai"
,
""
);
isLoading
.
value
=
true
;
// 创建 AbortController 用于取消请求
abortController
.
value
=
new
AbortController
();
try
{
await
fetchEventSource
(
"/api/chat-stream"
,
{
method
:
"POST"
,
headers
:
{
"Content-Type"
:
"application/json"
},
body
:
JSON
.
stringify
({
question
:
question
// 其他参数...
}),
signal
:
abortController
.
value
.
signal
,
onopen
:
async
response
=>
{
console
.
log
(
"SSE 连接已建立"
,
response
.
status
);
if
(
response
.
status
!==
200
)
{
throw
new
Error
(
`请求失败:
${
response
.
status
}
`
);
}
},
onmessage
:
event
=>
{
try
{
if
(
event
.
data
===
"[DONE]"
)
{
// 流式输出结束
isLoading
.
value
=
false
;
return
;
}
const
data
=
JSON
.
parse
(
event
.
data
);
if
(
data
.
type
===
"content"
&&
data
.
content
)
{
// 流式更新内容
updateLastAIMessage
(
prev
=>
prev
+
data
.
content
);
}
else
if
(
data
.
type
===
"error"
)
{
throw
new
Error
(
data
.
message
||
"请求失败"
);
}
}
catch
(
error
)
{
console
.
error
(
"解析 SSE 数据错误:"
,
error
);
}
},
onclose
:
()
=>
{
console
.
log
(
"SSE 连接已关闭"
);
isLoading
.
value
=
false
;
},
onerror
:
error
=>
{
console
.
error
(
"SSE 连接错误:"
,
error
);
ElMessage
.
error
(
"连接失败,请重试"
);
isLoading
.
value
=
false
;
// 不要抛出错误,否则会重试
}
});
}
catch
(
error
)
{
console
.
error
(
"SSE 请求失败:"
,
error
);
if
(
error
.
name
!==
"AbortError"
)
{
ElMessage
.
error
(
error
.
message
||
"请求失败"
);
}
isLoading
.
value
=
false
;
}
};
// 发送消息
const
sendMessage
=
async
()
=>
{
...
...
src/views/writtingAsstaint/index.vue
浏览文件 @
2117fce3
...
...
@@ -182,28 +182,29 @@ const getStreamChat = async (search, inputValue) => {
},
async
onmessage
(
res
)
{
let
msgData
=
JSON
.
parse
(
res
.
data
);
console
.
log
(
"ress"
,
msgData
.
content
);
console
.
log
(
"resss"
,
msgData
.
content
);
let
str
=
msgData
.
content
;
reportContent
.
value
+=
str
;
updateContent
(
reportContent
.
value
,
scrollContainer
.
value
);
if
(
str
!==
"[DONE]"
)
{
reportContent
.
value
+=
str
;
updateContent
(
reportContent
.
value
,
scrollContainer
.
value
);
}
else
{
ElMessage
.
success
(
"报文生成结束"
);
abortController
.
value
.
abort
();
abortController
.
value
=
new
AbortController
();
}
},
onerror
(
error
)
{
ElMessage
({
message
:
"
问题回答失败请重试
"
,
message
:
"
写报生成报错!
"
,
type
:
"warning"
});
abortController
.
value
.
abort
();
abortController
.
value
=
new
AbortController
();
throw
new
Error
(
error
);
}
}).
catch
(
error
=>
{
ElMessage
({
message
:
"
问题回答失败请重试
"
,
message
:
"
写报生成报错!
"
,
type
:
"warning"
});
abortController
.
value
.
abort
();
...
...
编写
预览
Markdown
格式
0%
重试
或
添加新文件
添加附件
取消
您添加了
0
人
到此讨论。请谨慎行事。
请先完成此评论的编辑!
取消
请
注册
或者
登录
后发表评论