Commit 307f751e authored by 高如斌's avatar 高如斌

发送聊天send函数添加displayTextOrEvent参数,支持自定义发送content显示值

parent bbc9fd63
...@@ -732,19 +732,36 @@ const processSSELine = async ( ...@@ -732,19 +732,36 @@ const processSSELine = async (
} }
return false; return false;
}; };
/**
// 发送消息 * 发送消息到 AI 智能体
const sendMessage = async () => { * @param displayTextOrEvent 可选参数,可以是:
* - string: 在聊天框中显示的自定义文本
* - Event: 来自按钮点击或键盘事件(会被忽略)
* - undefined: 显示 inputMessage 的原始内容
*
* 使用场景:
* 1. 普通文本消息:sendMessage() - 显示和发送的内容一致
* 2. 表单提交:sendMessage("填写内容已提交") - 显示友好提示,实际发送表单 JSON 数据
* 3. 事件触发:@click="sendMessage" - 自动过滤事件对象
*
*/
const sendMessage = async (displayTextOrEvent?: string | Event) => {
if (!selectedAgent.value || !inputMessage.value.trim()) { if (!selectedAgent.value || !inputMessage.value.trim()) {
return; return;
} }
const userMessage = inputMessage.value.trim(); const userMessage = inputMessage.value.trim(); // 实际发送给后端的内容
// 判断参数类型,如果是事件对象则忽略,否则使用自定义文本
const displayText =
typeof displayTextOrEvent === "string" ? displayTextOrEvent : undefined;
const displayMessage = displayText || userMessage; // 在聊天框中显示的内容
inputMessage.value = ""; inputMessage.value = "";
// 添加用户消息 // 添加用户消息到聊天记录(显示自定义文本)
messages.value.push({ messages.value.push({
content: userMessage, content: displayMessage,
isUser: true, isUser: true,
agentId: selectedAgent.value, agentId: selectedAgent.value,
timestamp: Date.now(), timestamp: Date.now(),
...@@ -753,7 +770,7 @@ const sendMessage = async () => { ...@@ -753,7 +770,7 @@ const sendMessage = async () => {
await scrollToBottom(); await scrollToBottom();
// 添加AI消息容器(流式接收 // 添加AI消息容器(用于流式接收响应
const aiMessageIndex = messages.value.length; const aiMessageIndex = messages.value.length;
messages.value.push({ messages.value.push({
content: "", content: "",
...@@ -762,7 +779,7 @@ const sendMessage = async () => { ...@@ -762,7 +779,7 @@ const sendMessage = async () => {
timestamp: Date.now(), timestamp: Date.now(),
isStreaming: true, isStreaming: true,
hasError: false, hasError: false,
originalMessage: userMessage, originalMessage: userMessage, // 保存原始消息用于重试
}); });
isLoading.value = true; isLoading.value = true;
...@@ -898,9 +915,9 @@ const sendMessage = async () => { ...@@ -898,9 +915,9 @@ const sendMessage = async () => {
// 设置表单提交回调函数 // 设置表单提交回调函数
formStore.setSubmitCallback((formData: any) => { formStore.setSubmitCallback((formData: any) => {
console.log("表单提交数据:", formData); console.log("表单提交数据:", formData);
// 将表单数据作为消息发送 // 将表单数据作为消息发送,但在聊天框中显示 "填写内容已提交"
inputMessage.value = JSON.stringify(formData); inputMessage.value = JSON.stringify(formData);
sendMessage(); sendMessage("填写内容已提交");
}); });
// 打开表单 // 打开表单
......
...@@ -27,19 +27,19 @@ const submit = () => { ...@@ -27,19 +27,19 @@ const submit = () => {
templateRef.value?.ctx.validate(1, (isValid: boolean, formData: any) => { templateRef.value?.ctx.validate(1, (isValid: boolean, formData: any) => {
if (isValid) { if (isValid) {
// 追加隐藏字段到表单数据 // 追加隐藏字段到表单数据
const hiddenFields = formStore.hiddenFields; // const hiddenFields = formStore.hiddenFields;
hiddenFields.forEach((field) => { // hiddenFields.forEach((field) => {
if (field.name) { // if (field.name) {
// 根据字段 title 设置不同的值 // // 根据字段 title 设置不同的值
if (field.title === "接访员工姓名") { // if (field.title === "接访员工姓名") {
formData[field.name] = "张益达"; // formData[field.name] = "张益达";
} else if (field.title === "接访员工手机号") { // } else if (field.title === "接访员工手机号") {
formData[field.name] = "13611111111"; // formData[field.name] = "13611111111";
} else { // } else {
formData[field.name] = "none"; // formData[field.name] = "none";
} // }
} // }
}); // });
console.log("表单验证通过,提交数据:", formData); console.log("表单验证通过,提交数据:", formData);
// 调用 store 的 submitForm 方法,触发回调 // 调用 store 的 submitForm 方法,触发回调
......
...@@ -3,10 +3,9 @@ ...@@ -3,10 +3,9 @@
* @Author: gaorubin * @Author: gaorubin
* @Date: 2025-12-24 16:12:58 * @Date: 2025-12-24 16:12:58
* @LastEditors: gaorubin * @LastEditors: gaorubin
* @LastEditTime: 2025-12-24 17:26:54 * @LastEditTime: 2025-12-26 10:45:18
*/ */
import { defineStore } from "pinia"; import { defineStore } from "pinia";
import { cloneDeep } from "lodash-es";
/** /**
* 获取初始表单 JSON 配置 * 获取初始表单 JSON 配置
......
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