plain-design 1.0.0-beta.157 → 1.0.0-beta.159
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
|
@@ -70,7 +70,7 @@ export const $ai = (() => {
|
|
|
70
70
|
* @date 2025/5/20 19:26
|
|
71
71
|
*/
|
|
72
72
|
const chatSystem = async (userContent: string, systemContent?: string, _aiConfig?: iAiConfiguration) => {
|
|
73
|
-
const messages = [{ "role": "user", "content": userContent }];
|
|
73
|
+
const messages: iAiHistory[] = [{ "role": "user", "content": userContent }];
|
|
74
74
|
if (!!systemContent) {
|
|
75
75
|
messages.unshift({ "role": "system", "content": systemContent });
|
|
76
76
|
}
|
|
@@ -100,15 +100,7 @@ export const $ai = (() => {
|
|
|
100
100
|
onThinking,
|
|
101
101
|
onThinkEnd,
|
|
102
102
|
onThinkStart,
|
|
103
|
-
}:
|
|
104
|
-
aiConfig?: iAiConfiguration,
|
|
105
|
-
messages: iAiHistory[],
|
|
106
|
-
onReceiving: (data: { fullText: string, chunkText: string }) => void,
|
|
107
|
-
onFinish?: (fullText: string) => void,
|
|
108
|
-
onThinking?: (thinkText: string) => void,
|
|
109
|
-
onThinkStart?: () => void,
|
|
110
|
-
onThinkEnd?: () => void,
|
|
111
|
-
}
|
|
103
|
+
}: iAiChatStreamParameter
|
|
112
104
|
) => {
|
|
113
105
|
const aiConfig = _aiConfig ?? getConfig();
|
|
114
106
|
// 发送请求
|
|
@@ -279,8 +271,8 @@ export interface iAiConfiguration {
|
|
|
279
271
|
}
|
|
280
272
|
|
|
281
273
|
export interface iAiHistory {
|
|
282
|
-
|
|
283
|
-
|
|
274
|
+
role: 'user' | 'assistant' | 'system';
|
|
275
|
+
content?: string;
|
|
284
276
|
}
|
|
285
277
|
|
|
286
278
|
export interface iAiChoice {
|
|
@@ -352,3 +344,13 @@ export interface iAiChatResponse {
|
|
|
352
344
|
total_tokens: number,
|
|
353
345
|
}
|
|
354
346
|
}
|
|
347
|
+
|
|
348
|
+
export interface iAiChatStreamParameter {
|
|
349
|
+
aiConfig?: iAiConfiguration,
|
|
350
|
+
messages: iAiHistory[],
|
|
351
|
+
onReceiving: (data: { fullText: string, chunkText: string }) => void,
|
|
352
|
+
onFinish?: (fullText: string) => void,
|
|
353
|
+
onThinking?: (thinkText: string) => void,
|
|
354
|
+
onThinkStart?: () => void,
|
|
355
|
+
onThinkEnd?: () => void,
|
|
356
|
+
}
|
|
@@ -7,7 +7,7 @@ import Button from "../Button";
|
|
|
7
7
|
import './ai-chat-box.scss';
|
|
8
8
|
import $message from "../$message";
|
|
9
9
|
import {uuid} from "@peryl/utils/uuid";
|
|
10
|
-
import {$ai, iAiChatResponse, iAiConfiguration, iAiHistory} from "../$ai";
|
|
10
|
+
import {$ai, iAiChatResponse, iAiChatStreamParameter, iAiConfiguration, iAiHistory} from "../$ai";
|
|
11
11
|
import $configuration from "../$configuration";
|
|
12
12
|
import Scroll from "../Scroll";
|
|
13
13
|
import {delay} from "@peryl/utils/delay";
|
|
@@ -28,6 +28,9 @@ export const AiChatBox = designComponent({
|
|
|
28
28
|
systemContent: { type: String },
|
|
29
29
|
/*手动处理大模型返回的message*/
|
|
30
30
|
handleMessage: { type: Function as PropType<(param: { message: string, userContent: string, chatData?: iAiChatResponse, addHistory: (history: iChatBoxHistory) => void }) => void | iChatBoxHistory | iChatBoxHistory[] | Promise<void | iChatBoxHistory | iChatBoxHistory[]>> },
|
|
31
|
+
handleReceive: { type: Function as PropType<(data: { fullText?: string, chunkText?: string }) => void> },
|
|
32
|
+
beforeSendUserContent: { type: Function as PropType<(data: { userContent: string, messages: iAiHistory[] }) => Promise<void>> },
|
|
33
|
+
handleChatStream: { type: Function as PropType<(parameter: iAiChatStreamParameter & { userContent: string }) => Promise<any>> },
|
|
31
34
|
inputMinHeight: { type: String, default: '80px' },
|
|
32
35
|
inputMaxHeight: { type: String, default: '225px' },
|
|
33
36
|
},
|
|
@@ -62,27 +65,29 @@ export const AiChatBox = designComponent({
|
|
|
62
65
|
refs.scroll.methods.scrollEnd();
|
|
63
66
|
}
|
|
64
67
|
},
|
|
65
|
-
addHistory: async (history: iChatBoxHistory) => {
|
|
66
|
-
state.histories.push(history);
|
|
68
|
+
addHistory: async (...history: iChatBoxHistory[]) => {
|
|
69
|
+
state.histories.push(...history);
|
|
67
70
|
await methods.scrollEnd();
|
|
68
71
|
},
|
|
69
72
|
send: async (userContent: string) => {
|
|
70
73
|
await delay(23);
|
|
71
74
|
state.userContent = '';
|
|
72
|
-
|
|
73
|
-
await methods.scrollEnd();
|
|
75
|
+
await methods.addHistory({ role: 'user', content: userContent, id: uuid() });
|
|
74
76
|
|
|
75
77
|
/*const { message, chatData } = await $ai.chatSystem(userContent, props.systemContent, aiConfig);*/
|
|
76
78
|
const messages: iAiHistory[] = [{ role: 'user', content: userContent }];
|
|
79
|
+
await props.beforeSendUserContent?.({ userContent, messages });
|
|
77
80
|
if (!!props.systemContent?.trim().length) {messages.unshift({ role: 'system', content: props.systemContent });}
|
|
78
81
|
let chatBoxHistory: iChatBoxHistory | undefined = undefined;
|
|
79
82
|
let message: string | undefined = undefined;
|
|
80
|
-
|
|
81
|
-
|
|
83
|
+
|
|
84
|
+
const aiChatStreamParameter: iAiChatStreamParameter & { userContent: string } = {
|
|
82
85
|
aiConfig,
|
|
83
86
|
messages,
|
|
84
|
-
|
|
87
|
+
userContent,
|
|
88
|
+
onReceiving: ({ fullText, chunkText }) => {
|
|
85
89
|
// console.log('receiving', fullText);
|
|
90
|
+
props.handleReceive?.({ fullText, chunkText });
|
|
86
91
|
if (!chatBoxHistory) {
|
|
87
92
|
state.histories.push({ role: 'assistant', content: '', id: uuid() });
|
|
88
93
|
chatBoxHistory = lastItem(state.histories);
|
|
@@ -94,7 +99,14 @@ export const AiChatBox = designComponent({
|
|
|
94
99
|
chatBoxHistory!.content = fullText;
|
|
95
100
|
message = fullText;
|
|
96
101
|
},
|
|
97
|
-
}
|
|
102
|
+
};
|
|
103
|
+
|
|
104
|
+
if (!!props.handleChatStream) {
|
|
105
|
+
await props.handleChatStream(aiChatStreamParameter);
|
|
106
|
+
} else {
|
|
107
|
+
await $ai.chatStream(aiChatStreamParameter);
|
|
108
|
+
}
|
|
109
|
+
|
|
98
110
|
if (!message) {
|
|
99
111
|
throw new Error('message is empty!');
|
|
100
112
|
}
|
|
@@ -108,9 +120,8 @@ export const AiChatBox = designComponent({
|
|
|
108
120
|
});
|
|
109
121
|
|
|
110
122
|
if (newHistory != null) {
|
|
111
|
-
|
|
123
|
+
await methods.addHistory(...toArray(newHistory));
|
|
112
124
|
}
|
|
113
|
-
await methods.scrollEnd();
|
|
114
125
|
|
|
115
126
|
return { message, /*chatData*/ };
|
|
116
127
|
},
|
|
@@ -133,7 +144,12 @@ export const AiChatBox = designComponent({
|
|
|
133
144
|
};
|
|
134
145
|
|
|
135
146
|
return {
|
|
136
|
-
refer: {
|
|
147
|
+
refer: {
|
|
148
|
+
refs,
|
|
149
|
+
aiConfig,
|
|
150
|
+
state,
|
|
151
|
+
methods,
|
|
152
|
+
},
|
|
137
153
|
render: () => (
|
|
138
154
|
<div className={classes.value}>
|
|
139
155
|
{slots.prepend.isExist() && (
|
|
@@ -177,10 +193,8 @@ export const AiChatBox = designComponent({
|
|
|
177
193
|
},
|
|
178
194
|
});
|
|
179
195
|
|
|
180
|
-
export interface iChatBoxHistory {
|
|
196
|
+
export interface iChatBoxHistory extends iAiHistory {
|
|
181
197
|
id: string,
|
|
182
|
-
role: 'user' | 'assistant';
|
|
183
|
-
content?: string,
|
|
184
198
|
render?: () => any,
|
|
185
199
|
}
|
|
186
200
|
|