plain-design 1.0.0-beta.156 → 1.0.0-beta.158

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,6 +1,6 @@
1
1
  {
2
2
  "name": "plain-design",
3
- "version": "1.0.0-beta.156",
3
+ "version": "1.0.0-beta.158",
4
4
  "description": "",
5
5
  "main": "dist/plain-design.min.js",
6
6
  "module": "dist/plain-design.commonjs.min.js",
@@ -7,11 +7,12 @@ 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} from "../$ai";
10
+ import {$ai, iAiChatResponse, iAiConfiguration, iAiHistory} from "../$ai";
11
11
  import $configuration from "../$configuration";
12
12
  import Scroll from "../Scroll";
13
13
  import {delay} from "@peryl/utils/delay";
14
14
  import {toArray} from "@peryl/utils/toArray";
15
+ import {lastItem} from "@peryl/utils/lastItem";
15
16
 
16
17
  export const AiChatBox = designComponent({
17
18
  props: {
@@ -26,7 +27,8 @@ export const AiChatBox = designComponent({
26
27
  /*系统提示词*/
27
28
  systemContent: { type: String },
28
29
  /*手动处理大模型返回的message*/
29
- handleMessage: { type: Function as PropType<(param: { message: string, userContent: string, chatData: iAiChatResponse, addHistory: (history: iChatBoxHistory) => void }) => void | iChatBoxHistory | iChatBoxHistory[] | Promise<void | iChatBoxHistory | iChatBoxHistory[]>> },
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> },
30
32
  inputMinHeight: { type: String, default: '80px' },
31
33
  inputMaxHeight: { type: String, default: '225px' },
32
34
  },
@@ -61,25 +63,56 @@ export const AiChatBox = designComponent({
61
63
  refs.scroll.methods.scrollEnd();
62
64
  }
63
65
  },
64
- addHistory: async (history: iChatBoxHistory) => {
65
- state.histories.push(history);
66
+ addHistory: async (...history: iChatBoxHistory[]) => {
67
+ state.histories.push(...history);
66
68
  await methods.scrollEnd();
67
69
  },
68
70
  send: async (userContent: string) => {
69
71
  await delay(23);
70
72
  state.userContent = '';
71
- state.histories.push({ role: 'user', content: userContent, id: uuid() });
72
- await methods.scrollEnd();
73
+ await methods.addHistory({ role: 'user', content: userContent, id: uuid() });
73
74
 
74
- const { message, chatData } = await $ai.chatSystem(userContent, props.systemContent, aiConfig);
75
+ /*const { message, chatData } = await $ai.chatSystem(userContent, props.systemContent, aiConfig);*/
76
+ const messages: iAiHistory[] = [{ role: 'user', content: userContent }];
77
+ if (!!props.systemContent?.trim().length) {messages.unshift({ role: 'system', content: props.systemContent });}
78
+ let chatBoxHistory: iChatBoxHistory | undefined = undefined;
79
+ let message: string | undefined = undefined;
80
+ // console.log('start chat stream');
81
+ await $ai.chatStream({
82
+ aiConfig,
83
+ messages,
84
+ onReceiving: ({ fullText, chunkText }) => {
85
+ // console.log('receiving', fullText);
86
+ props.handleReceive?.({ fullText, chunkText });
87
+ if (!chatBoxHistory) {
88
+ state.histories.push({ role: 'assistant', content: '', id: uuid() });
89
+ chatBoxHistory = lastItem(state.histories);
90
+ }
91
+ chatBoxHistory!.content = fullText;
92
+ methods.scrollEnd();
93
+ },
94
+ onFinish: (fullText) => {
95
+ chatBoxHistory!.content = fullText;
96
+ message = fullText;
97
+ },
98
+ });
99
+ if (!message) {
100
+ throw new Error('message is empty!');
101
+ }
75
102
 
76
103
  /*可以手动处理返回的消息,自定义往对话框插入内容*/
77
- const newHistory: iChatBoxHistory | iChatBoxHistory[] = await props.handleMessage?.({ message, userContent, chatData, addHistory: methods.addHistory }) || ({ role: 'assistant', content: message, id: uuid() });
104
+ const newHistory: void | iChatBoxHistory | iChatBoxHistory[] = await props.handleMessage?.({
105
+ message,
106
+ userContent,
107
+ // chatData,
108
+ addHistory: methods.addHistory
109
+ });
78
110
 
79
- state.histories.push(...toArray(newHistory));
80
- await methods.scrollEnd();
111
+ if (newHistory != null) {
112
+ await methods.addHistory(...toArray(newHistory));
113
+ }
81
114
 
82
- return { message, chatData };
115
+ return { message, /*chatData*/ };
83
116
  },
84
117
  };
85
118
 
@@ -180,7 +180,7 @@ export const useTableOptionAi = AutoModule.createRegistration((option) => {
180
180
  await methods.apply(operateItems);
181
181
  const workTitleMap = { search: '查询', sort: '排序', config: '配置', error: '' };
182
182
  return [
183
- { id: uuid(), role: 'assistant', content: message },
183
+ // { id: uuid(), role: 'assistant', content: message },
184
184
  { id: uuid(), role: 'assistant', content: `已经为您执行 ${operateItems.map(i => workTitleMap[i.type]).join(',')} 工作` }
185
185
  ];
186
186
  } catch (e) {