st-comp 0.0.249 → 0.0.251

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/package.json CHANGED
@@ -1,7 +1,7 @@
1
1
  {
2
2
  "name": "st-comp",
3
3
  "public": true,
4
- "version": "0.0.249",
4
+ "version": "0.0.251",
5
5
  "type": "module",
6
6
  "scripts": {
7
7
  "dev": "vite",
@@ -1,26 +1,31 @@
1
1
  <script setup>
2
2
  import dayjs from "dayjs";
3
3
  import { ElMessage } from "element-plus";
4
- import { ref, nextTick, watch } from "vue";
5
- import { sendToBaiLianAppNonStreaming } from "../../public/aiTools";
4
+ import { ref, nextTick, watch, onMounted } from "vue";
5
+ import { sendToBaiLianAppStreaming } from "../../public/aiTools";
6
6
  import { UserFilled, Service, Promotion } from "@element-plus/icons-vue";
7
7
 
8
8
  const emit = defineEmits(["callBack"]);
9
+ const props = defineProps({
10
+ defaultMessage: {
11
+ type: String,
12
+ default: "你好呀!我是你的品种池AI助手,会根据您的自然语言去进行品种的条件查询\n\n示例: \n帮我查A股科创板下品种转价差上证50并且总市值大于1千万, 按照总市值升序进行排序",
13
+ },
14
+ });
9
15
 
10
16
  const visible = ref(false);
11
- const isSending = ref(false);
12
- const isThinking = ref(false);
17
+ const isSending = ref(false); // 发送按钮
18
+ const isThinking = ref(false); // AI思考状态
13
19
 
20
+ // 消息列表
14
21
  const messageListRef = ref(null);
15
- const messages = ref([
16
- {
17
- role: "assistant",
18
- time: dayjs().format("HH:mm"),
19
- content: "你好呀!我是你的品种池AI助手,会根据您的自然语言去进行品种的条件查询\n\n示例: \n帮我查找A股,总市值1000亿,流通股本大于等于1000万",
20
- },
21
- ]);
22
+ const messages = ref([]);
22
23
  const inputMessage = ref("");
23
24
 
25
+ // 当前正在接收的AI消息(用于流式更新)
26
+ const currentAssistantMessage = ref(null);
27
+ const currentAssistantIndex = ref(-1);
28
+
24
29
  // 发送消息
25
30
  const sendMessage = async () => {
26
31
  // 校验输入内容是否为空
@@ -38,29 +43,85 @@ const sendMessage = async () => {
38
43
  inputMessage.value = "";
39
44
  await scrollToBottom();
40
45
 
41
- // 发送请求至百炼应用AI
46
+ // 创建一个临时的AI消息占位符
47
+ const assistantMessage = {
48
+ role: "assistant",
49
+ time: dayjs().format("HH:mm"),
50
+ content: "",
51
+ };
52
+ messages.value.push(assistantMessage);
53
+ currentAssistantIndex.value = messages.value.length - 1;
54
+ currentAssistantMessage.value = assistantMessage;
55
+ await scrollToBottom();
56
+
57
+ // 发送请求至百炼应用AI(流式)
42
58
  isSending.value = true;
43
59
  isThinking.value = true;
44
- let apiRes = "{}";
60
+
61
+ let fullResponse = "";
62
+
45
63
  try {
46
64
  const appId = "9e54d112acfe4531bd1fc4fee8827fef";
47
65
  const apiKey = "sk-d995eb26a4334bdeb2ccb4cbfaf51de8";
48
- apiRes = await sendToBaiLianAppNonStreaming({ appId, apiKey, value: content });
66
+
67
+ await sendToBaiLianAppStreaming({
68
+ appId,
69
+ apiKey,
70
+ value: content,
71
+ callback: (type, data) => {
72
+ if (type === "message") {
73
+ // 实时更新消息内容
74
+ fullResponse += data;
75
+ if (currentAssistantMessage.value) {
76
+ currentAssistantMessage.value.content = fullResponse;
77
+ // 实时滚动到底部
78
+ scrollToBottom();
79
+ }
80
+ } else if (type === "finish") {
81
+ // 流式传输完成
82
+ isThinking.value = false;
83
+ isSending.value = false;
84
+ console.log(fullResponse)
85
+ // 触发回调
86
+ try {
87
+ // 尝试解析完整的响应为JSON
88
+ const jsonResponse = JSON.parse(fullResponse);
89
+ emit("callBack", jsonResponse);
90
+ } catch (error) {
91
+ // 如果不是JSON格式,直接返回文本
92
+ emit("callBack", fullResponse);
93
+ }
94
+
95
+ // 清空当前消息引用
96
+ currentAssistantMessage.value = null;
97
+ currentAssistantIndex.value = -1;
98
+ }
99
+ }
100
+ });
101
+ } catch (error) {
102
+ ElMessage.error(`AI响应异常: ${error}`);
103
+ console.error("AI响应异常:", error);
104
+
105
+ // 如果出错,移除占位消息并显示错误
106
+ if (currentAssistantIndex.value !== -1) {
107
+ messages.value.splice(currentAssistantIndex.value, 1);
108
+ currentAssistantMessage.value = null;
109
+ currentAssistantIndex.value = -1;
110
+ }
111
+
112
+ // 添加错误提示消息
49
113
  messages.value.push({
50
114
  role: "assistant",
51
115
  time: dayjs().format("HH:mm"),
52
- content: apiRes,
116
+ content: "抱歉,AI服务响应异常,请稍后重试。",
53
117
  });
54
118
  await scrollToBottom();
55
- } catch (error) {
56
- ElMessage.error(`AI响应异常: ${error}`);
57
- console.error("AI响应异常:", error);
58
- } finally {
119
+
59
120
  isSending.value = false;
60
121
  isThinking.value = false;
61
122
  }
62
- emit("callBack", JSON.parse(apiRes));
63
123
  };
124
+
64
125
  // 滚动到底部
65
126
  const scrollToBottom = async () => {
66
127
  await nextTick();
@@ -68,6 +129,15 @@ const scrollToBottom = async () => {
68
129
  messageListRef.value.scrollTop = messageListRef.value.scrollHeight;
69
130
  }
70
131
  };
132
+
133
+ onMounted(() => {
134
+ messages.value.push({
135
+ role: "assistant",
136
+ time: dayjs().format("HH:mm"),
137
+ content: props.defaultMessage,
138
+ });
139
+ });
140
+
71
141
  watch(
72
142
  () => messages.value,
73
143
  () => {
@@ -75,6 +145,7 @@ watch(
75
145
  },
76
146
  { deep: true },
77
147
  );
148
+
78
149
  defineExpose({
79
150
  open: (data) => {
80
151
  visible.value = true;
@@ -110,21 +181,23 @@ defineExpose({
110
181
  class="message-item"
111
182
  :class="message.role"
112
183
  >
113
- <div class="avatar">
114
- <el-avatar
115
- :size="32"
116
- :icon="message.role === 'user' ? UserFilled : Service"
117
- />
118
- </div>
119
- <div class="message-content">
120
- <div class="message-text">{{ message.content }}</div>
121
- <div class="message-time">{{ message.time }}</div>
122
- </div>
184
+ <template v-if="message.content">
185
+ <div class="avatar">
186
+ <el-avatar
187
+ :size="32"
188
+ :icon="message.role === 'user' ? UserFilled : Service"
189
+ />
190
+ </div>
191
+ <div class="message-content">
192
+ <div class="message-text">{{ message.content }}</div>
193
+ <div class="message-time">{{ message.time }}</div>
194
+ </div>
195
+ </template>
123
196
  </div>
124
197
 
125
- <!-- AI 思考状态 -->
198
+ <!-- AI 思考状态(仅在未开始接收流式消息时显示) -->
126
199
  <div
127
- v-if="isThinking"
200
+ v-if="isThinking && !currentAssistantMessage?.content"
128
201
  class="message-item assistant"
129
202
  >
130
203
  <div class="avatar">
@@ -427,4 +500,4 @@ defineExpose({
427
500
  opacity: 1;
428
501
  }
429
502
  }
430
- </style>
503
+ </style>
package/public/aiTools.js CHANGED
@@ -1,11 +1,4 @@
1
- /**
2
- * 发送非流式请求到百炼 AI 应用
3
- * @param {Object} options
4
- * @param {string} options.appId - 应用ID
5
- * @param {string} options.apiKey - API Key
6
- * @param {string} options.value - 用户输入
7
- * @returns {Promise<string>} 返回 AI 输出的完整文本(JSON 字符串)
8
- */
1
+ // 非流式返回
9
2
  export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) => {
10
3
  try {
11
4
  const response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
@@ -34,3 +27,43 @@ export const sendToBaiLianAppNonStreaming = async ({ appId, apiKey, value }) =>
34
27
  throw error;
35
28
  }
36
29
  };
30
+ // 流式返回
31
+ export const sendToBaiLianAppStreaming = async ({ appId, apiKey, value, callback }) => {
32
+ try {
33
+ const res = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
34
+ method: "POST",
35
+ body: JSON.stringify({
36
+ input: { prompt: value },
37
+ parameters: {
38
+ incremental_output: "true", // 流式返回
39
+ },
40
+ debug: {},
41
+ }),
42
+ headers: {
43
+ Authorization: `Bearer ${apiKey}`,
44
+ "Content-Type": "application/json",
45
+ "X-DashScope-SSE": "enable", // 流式输出
46
+ },
47
+ });
48
+ if (!res.ok || !res.body) throw new Error("请求失败");
49
+
50
+ const reader = res.body.getReader();
51
+ const decoder = new TextDecoder();
52
+
53
+ while (true) {
54
+ const { done, value } = await reader.read();
55
+ if (done && callback) {
56
+ callback("finish", "");
57
+ break;
58
+ }
59
+ try {
60
+ const data = decoder.decode(value, { stream: true });
61
+ const resData = JSON.parse(data.split("\n")[3].substr(5));
62
+ const resText = resData?.output?.text;
63
+ if (resText && callback) callback("message", resText);
64
+ } catch (error) {}
65
+ }
66
+ } catch (error) {
67
+ console.error(error);
68
+ }
69
+ };