st-comp 0.0.250 → 0.0.252
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/es/MonacoEditor.cjs +1 -1
- package/es/MonacoEditor.js +1 -1
- package/es/VarietyAiHelper.cjs +4 -2
- package/es/VarietyAiHelper.js +234 -171
- package/es/VarietySearch.cjs +1 -1
- package/es/VarietySearch.js +1 -1
- package/es/aiTools.js +81 -8
- package/es/{index-5befc414.cjs → index-40f05e2c.cjs} +2 -2
- package/es/{index-8f9d0d17.js → index-8a54ceeb.js} +2 -2
- package/es/{python-27ece6fa.cjs → python-02c3937a.cjs} +1 -1
- package/es/{python-656db04f.js → python-99011a53.js} +1 -1
- package/es/style.css +1 -1
- package/lib/aiTools.js +81 -8
- package/lib/bundle.js +1 -1
- package/lib/bundle.umd.cjs +50 -48
- package/lib/{index-b51777a3.js → index-d57eff69.js} +755 -692
- package/lib/{python-ac4949ec.js → python-fe790c5a.js} +1 -1
- package/lib/style.css +1 -1
- package/package.json +1 -1
- package/packages/VarietyAiHelper/index.vue +91 -26
- package/public/aiTools.js +81 -8
- package/src/pages/VarietySearch/index.vue +0 -1
package/package.json
CHANGED
|
@@ -2,26 +2,30 @@
|
|
|
2
2
|
import dayjs from "dayjs";
|
|
3
3
|
import { ElMessage } from "element-plus";
|
|
4
4
|
import { ref, nextTick, watch, onMounted } from "vue";
|
|
5
|
-
import {
|
|
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
9
|
const props = defineProps({
|
|
10
10
|
defaultMessage: {
|
|
11
11
|
type: String,
|
|
12
|
-
default: "你好呀!我是你的品种池AI助手,会根据您的自然语言去进行品种的条件查询\n\n示例: \n
|
|
12
|
+
default: "你好呀!我是你的品种池AI助手,会根据您的自然语言去进行品种的条件查询\n\n示例: \n帮我查A股科创板下品种转价差上证50并且总市值大于1千万, 按照总市值升序进行排序",
|
|
13
13
|
},
|
|
14
14
|
});
|
|
15
15
|
|
|
16
16
|
const visible = ref(false);
|
|
17
|
-
const isSending = ref(false);
|
|
18
|
-
const isThinking = ref(false);
|
|
17
|
+
const isSending = ref(false); // 发送按钮
|
|
18
|
+
const isThinking = ref(false); // AI思考状态
|
|
19
19
|
|
|
20
20
|
// 消息列表
|
|
21
21
|
const messageListRef = ref(null);
|
|
22
22
|
const messages = ref([]);
|
|
23
23
|
const inputMessage = ref("");
|
|
24
24
|
|
|
25
|
+
// 当前正在接收的AI消息(用于流式更新)
|
|
26
|
+
const currentAssistantMessage = ref(null);
|
|
27
|
+
const currentAssistantIndex = ref(-1);
|
|
28
|
+
|
|
25
29
|
// 发送消息
|
|
26
30
|
const sendMessage = async () => {
|
|
27
31
|
// 校验输入内容是否为空
|
|
@@ -39,29 +43,85 @@ const sendMessage = async () => {
|
|
|
39
43
|
inputMessage.value = "";
|
|
40
44
|
await scrollToBottom();
|
|
41
45
|
|
|
42
|
-
//
|
|
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(流式)
|
|
43
58
|
isSending.value = true;
|
|
44
59
|
isThinking.value = true;
|
|
45
|
-
|
|
60
|
+
|
|
61
|
+
let fullResponse = "";
|
|
62
|
+
|
|
46
63
|
try {
|
|
47
64
|
const appId = "9e54d112acfe4531bd1fc4fee8827fef";
|
|
48
65
|
const apiKey = "sk-d995eb26a4334bdeb2ccb4cbfaf51de8";
|
|
49
|
-
|
|
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
|
+
// 添加错误提示消息
|
|
50
113
|
messages.value.push({
|
|
51
114
|
role: "assistant",
|
|
52
115
|
time: dayjs().format("HH:mm"),
|
|
53
|
-
content:
|
|
116
|
+
content: "抱歉,AI服务响应异常,请稍后重试。",
|
|
54
117
|
});
|
|
55
118
|
await scrollToBottom();
|
|
56
|
-
|
|
57
|
-
ElMessage.error(`AI响应异常: ${error}`);
|
|
58
|
-
console.error("AI响应异常:", error);
|
|
59
|
-
} finally {
|
|
119
|
+
|
|
60
120
|
isSending.value = false;
|
|
61
121
|
isThinking.value = false;
|
|
62
122
|
}
|
|
63
|
-
emit("callBack", JSON.parse(apiRes));
|
|
64
123
|
};
|
|
124
|
+
|
|
65
125
|
// 滚动到底部
|
|
66
126
|
const scrollToBottom = async () => {
|
|
67
127
|
await nextTick();
|
|
@@ -69,6 +129,7 @@ const scrollToBottom = async () => {
|
|
|
69
129
|
messageListRef.value.scrollTop = messageListRef.value.scrollHeight;
|
|
70
130
|
}
|
|
71
131
|
};
|
|
132
|
+
|
|
72
133
|
onMounted(() => {
|
|
73
134
|
messages.value.push({
|
|
74
135
|
role: "assistant",
|
|
@@ -76,6 +137,7 @@ onMounted(() => {
|
|
|
76
137
|
content: props.defaultMessage,
|
|
77
138
|
});
|
|
78
139
|
});
|
|
140
|
+
|
|
79
141
|
watch(
|
|
80
142
|
() => messages.value,
|
|
81
143
|
() => {
|
|
@@ -83,6 +145,7 @@ watch(
|
|
|
83
145
|
},
|
|
84
146
|
{ deep: true },
|
|
85
147
|
);
|
|
148
|
+
|
|
86
149
|
defineExpose({
|
|
87
150
|
open: (data) => {
|
|
88
151
|
visible.value = true;
|
|
@@ -118,21 +181,23 @@ defineExpose({
|
|
|
118
181
|
class="message-item"
|
|
119
182
|
:class="message.role"
|
|
120
183
|
>
|
|
121
|
-
<
|
|
122
|
-
<
|
|
123
|
-
|
|
124
|
-
|
|
125
|
-
|
|
126
|
-
|
|
127
|
-
|
|
128
|
-
<div class="message-
|
|
129
|
-
|
|
130
|
-
|
|
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>
|
|
131
196
|
</div>
|
|
132
197
|
|
|
133
|
-
<!-- AI
|
|
198
|
+
<!-- AI 思考状态(仅在未开始接收流式消息时显示) -->
|
|
134
199
|
<div
|
|
135
|
-
v-if="isThinking"
|
|
200
|
+
v-if="isThinking && !currentAssistantMessage?.content"
|
|
136
201
|
class="message-item assistant"
|
|
137
202
|
>
|
|
138
203
|
<div class="avatar">
|
|
@@ -435,4 +500,4 @@ defineExpose({
|
|
|
435
500
|
opacity: 1;
|
|
436
501
|
}
|
|
437
502
|
}
|
|
438
|
-
</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,83 @@ 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 response = await fetch(`https://dashscope.aliyuncs.com/api/v1/apps/${appId}/completion`, {
|
|
34
|
+
method: "POST",
|
|
35
|
+
body: JSON.stringify({
|
|
36
|
+
input: { prompt: value },
|
|
37
|
+
parameters: { incremental_output: "true" },
|
|
38
|
+
debug: {},
|
|
39
|
+
}),
|
|
40
|
+
headers: {
|
|
41
|
+
Authorization: `Bearer ${apiKey}`,
|
|
42
|
+
"Content-Type": "application/json",
|
|
43
|
+
"X-DashScope-SSE": "enable",
|
|
44
|
+
},
|
|
45
|
+
});
|
|
46
|
+
|
|
47
|
+
if (!response.ok) throw new Error(`HTTP ${response.status}`);
|
|
48
|
+
|
|
49
|
+
const reader = response.body.getReader();
|
|
50
|
+
const decoder = new TextDecoder();
|
|
51
|
+
let buffer = "";
|
|
52
|
+
|
|
53
|
+
while (true) {
|
|
54
|
+
const { done, value } = await reader.read();
|
|
55
|
+
if (done) {
|
|
56
|
+
// 处理最后可能遗留的数据
|
|
57
|
+
if (buffer.trim()) {
|
|
58
|
+
tryProcessBuffer(buffer, callback);
|
|
59
|
+
}
|
|
60
|
+
callback("finish", "");
|
|
61
|
+
break;
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
buffer += decoder.decode(value, { stream: true });
|
|
65
|
+
|
|
66
|
+
let newlineIndex;
|
|
67
|
+
while ((newlineIndex = buffer.indexOf("\n")) !== -1) {
|
|
68
|
+
const line = buffer.substring(0, newlineIndex).trim();
|
|
69
|
+
buffer = buffer.substring(newlineIndex + 1);
|
|
70
|
+
|
|
71
|
+
if (line.startsWith("data:")) {
|
|
72
|
+
const data = line.substring(5).trim();
|
|
73
|
+
if (data && data !== "[DONE]") {
|
|
74
|
+
try {
|
|
75
|
+
const parsed = JSON.parse(data);
|
|
76
|
+
const text = parsed?.output?.text;
|
|
77
|
+
if (text && callback) {
|
|
78
|
+
callback("message", text);
|
|
79
|
+
}
|
|
80
|
+
} catch (e) {
|
|
81
|
+
// 可能是部分数据,继续等待
|
|
82
|
+
console.debug("等待完整数据...");
|
|
83
|
+
}
|
|
84
|
+
}
|
|
85
|
+
}
|
|
86
|
+
}
|
|
87
|
+
}
|
|
88
|
+
} catch (error) {
|
|
89
|
+
console.error("流式请求失败:", error);
|
|
90
|
+
callback("error", error.message);
|
|
91
|
+
}
|
|
92
|
+
};
|
|
93
|
+
|
|
94
|
+
// 辅助函数:处理缓冲区剩余数据
|
|
95
|
+
function tryProcessBuffer(buffer, callback) {
|
|
96
|
+
const lines = buffer.split("\n");
|
|
97
|
+
for (const line of lines) {
|
|
98
|
+
if (line.startsWith("data:")) {
|
|
99
|
+
const data = line.substring(5).trim();
|
|
100
|
+
if (data && data !== "[DONE]") {
|
|
101
|
+
try {
|
|
102
|
+
const parsed = JSON.parse(data);
|
|
103
|
+
const text = parsed?.output?.text;
|
|
104
|
+
if (text && callback) callback("message", text);
|
|
105
|
+
} catch (e) {}
|
|
106
|
+
}
|
|
107
|
+
}
|
|
108
|
+
}
|
|
109
|
+
}
|