@lobehub/ui 1.59.0 → 1.60.1
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/ChatItem/index.d.ts +5 -4
- package/es/ChatItem/index.js +11 -6
- package/es/ChatList/Item.d.ts +50 -0
- package/es/ChatList/Item.js +78 -0
- package/es/ChatList/index.d.ts +6 -39
- package/es/ChatList/index.js +17 -75
- package/es/EditableMessageList/index.d.ts +3 -3
- package/es/EditableMessageList/index.js +1 -1
- package/es/{Chat/store → EditableMessageList}/messageReducer.d.ts +5 -4
- package/es/{Chat/store → EditableMessageList}/messageReducer.js +0 -15
- package/es/index.d.ts +2 -2
- package/es/types/chatMessage.d.ts +4 -4
- package/es/types/llm.d.ts +13 -33
- package/es/types/llm.js +1 -10
- package/package.json +1 -1
- package/es/Chat/const.d.ts +0 -1
- package/es/Chat/const.js +0 -1
- package/es/Chat/index.d.ts +0 -2
- package/es/Chat/index.js +0 -2
- package/es/Chat/store/index.d.ts +0 -19
- package/es/Chat/store/index.js +0 -15
- package/es/Chat/store/initialState.d.ts +0 -28
- package/es/Chat/store/initialState.js +0 -9
- package/es/Chat/store/store.d.ts +0 -34
- package/es/Chat/store/store.js +0 -240
- package/es/Chat/types.d.ts +0 -77
- package/es/Chat/types.js +0 -1
- package/es/Chat/utils/fetch.d.ts +0 -11
- package/es/Chat/utils/fetch.js +0 -100
package/es/ChatItem/index.d.ts
CHANGED
|
@@ -3,14 +3,11 @@ import { ReactNode } from 'react';
|
|
|
3
3
|
import { type EditableMessageProps } from "../EditableMessage";
|
|
4
4
|
import { MetaData } from "../types/meta";
|
|
5
5
|
export interface ChatItemProps {
|
|
6
|
+
ErrorMessage?: ReactNode;
|
|
6
7
|
/**
|
|
7
8
|
* @description Actions to be displayed in the chat item
|
|
8
9
|
*/
|
|
9
10
|
actions?: ReactNode;
|
|
10
|
-
/**
|
|
11
|
-
* @description Props for Alert component
|
|
12
|
-
*/
|
|
13
|
-
alert?: AlertProps;
|
|
14
11
|
/**
|
|
15
12
|
* @description Metadata for the avatar
|
|
16
13
|
*/
|
|
@@ -27,6 +24,10 @@ export interface ChatItemProps {
|
|
|
27
24
|
* @description Whether the chat item is in editing mode
|
|
28
25
|
*/
|
|
29
26
|
editing?: boolean;
|
|
27
|
+
/**
|
|
28
|
+
* @description Props for Error render
|
|
29
|
+
*/
|
|
30
|
+
error?: AlertProps;
|
|
30
31
|
/**
|
|
31
32
|
* @description Whether the chat item is in loading state
|
|
32
33
|
*/
|
package/es/ChatItem/index.js
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
2
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
3
|
-
var _excluded = ["actions", "className", "primary", "borderSpacing", "loading", "message", "placement", "type", "avatar", "
|
|
3
|
+
var _excluded = ["actions", "className", "primary", "borderSpacing", "loading", "message", "placement", "type", "avatar", "error", "showTitle", "time", "editing", "onChange", "onEditingChange", "messageExtra", "renderMessage", "text", "ErrorMessage"];
|
|
4
4
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
5
5
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
6
6
|
import { Alert } from 'antd';
|
|
@@ -27,7 +27,7 @@ var ChatItem = /*#__PURE__*/memo(function (_ref) {
|
|
|
27
27
|
_ref$type = _ref.type,
|
|
28
28
|
type = _ref$type === void 0 ? 'block' : _ref$type,
|
|
29
29
|
avatar = _ref.avatar,
|
|
30
|
-
|
|
30
|
+
error = _ref.error,
|
|
31
31
|
showTitle = _ref.showTitle,
|
|
32
32
|
time = _ref.time,
|
|
33
33
|
editing = _ref.editing,
|
|
@@ -36,6 +36,7 @@ var ChatItem = /*#__PURE__*/memo(function (_ref) {
|
|
|
36
36
|
messageExtra = _ref.messageExtra,
|
|
37
37
|
renderMessage = _ref.renderMessage,
|
|
38
38
|
text = _ref.text,
|
|
39
|
+
ErrorMessage = _ref.ErrorMessage,
|
|
39
40
|
properties = _objectWithoutProperties(_ref, _excluded);
|
|
40
41
|
var _useStyles = useStyles({
|
|
41
42
|
avatarSize: AVATAR_SIZE,
|
|
@@ -90,10 +91,14 @@ var ChatItem = /*#__PURE__*/memo(function (_ref) {
|
|
|
90
91
|
})]
|
|
91
92
|
}), /*#__PURE__*/_jsxs("div", {
|
|
92
93
|
className: styles.messageContent,
|
|
93
|
-
children: [
|
|
94
|
-
|
|
95
|
-
|
|
96
|
-
|
|
94
|
+
children: [error ? /*#__PURE__*/_jsxs(Flexbox, {
|
|
95
|
+
gap: 8,
|
|
96
|
+
children: [/*#__PURE__*/_jsx(Alert, _objectSpread({
|
|
97
|
+
className: styles.alert,
|
|
98
|
+
showIcon: true,
|
|
99
|
+
type: 'error'
|
|
100
|
+
}, error)), ErrorMessage]
|
|
101
|
+
}) : /*#__PURE__*/_jsxs(Flexbox, {
|
|
97
102
|
className: cx(styles.message, editing && styles.editingContainer),
|
|
98
103
|
children: [messageContent, messageExtra && !editing ? /*#__PURE__*/_jsx("div", {
|
|
99
104
|
className: styles.messageExtra,
|
|
@@ -0,0 +1,50 @@
|
|
|
1
|
+
import { ReactNode } from 'react';
|
|
2
|
+
import { type ChatItemProps } from "../ChatItem";
|
|
3
|
+
import { ChatMessage, ChatMessageError } from "../types/chatMessage";
|
|
4
|
+
import { type ActionsBarProps } from './ActionsBar';
|
|
5
|
+
export type OnMessageChange = (id: string, content: string) => void;
|
|
6
|
+
export type MessageExtra = (props: ChatMessage) => ReactNode;
|
|
7
|
+
export type OnActionClick = (actionKey: string, messageId: string) => void;
|
|
8
|
+
export type RenderMessage = (content: ReactNode, message: ChatMessage) => ReactNode;
|
|
9
|
+
export type RenderErrorMessage = (error: ChatMessageError, message: ChatMessage) => ReactNode;
|
|
10
|
+
export interface ListItemProps {
|
|
11
|
+
loading?: boolean;
|
|
12
|
+
/**
|
|
13
|
+
* @description 点击操作按钮的回调函数
|
|
14
|
+
*/
|
|
15
|
+
onActionClick?: OnActionClick;
|
|
16
|
+
/**
|
|
17
|
+
* @description 消息变化的回调函数
|
|
18
|
+
*/
|
|
19
|
+
onMessageChange?: OnMessageChange;
|
|
20
|
+
/**
|
|
21
|
+
* @description 渲染错误消息的函数
|
|
22
|
+
*/
|
|
23
|
+
renderErrorMessage?: RenderErrorMessage;
|
|
24
|
+
/**
|
|
25
|
+
* @description 渲染消息的函数
|
|
26
|
+
*/
|
|
27
|
+
renderMessage?: RenderMessage;
|
|
28
|
+
/**
|
|
29
|
+
* @description 渲染消息额外内容的函数
|
|
30
|
+
*/
|
|
31
|
+
renderMessageExtra?: MessageExtra;
|
|
32
|
+
/**
|
|
33
|
+
* @description 是否显示聊天项的名称
|
|
34
|
+
* @default false
|
|
35
|
+
*/
|
|
36
|
+
showTitle?: boolean;
|
|
37
|
+
/**
|
|
38
|
+
* @description 文本内容
|
|
39
|
+
*/
|
|
40
|
+
text?: ChatItemProps['text'] & ActionsBarProps['text'] & {
|
|
41
|
+
copySuccess?: string;
|
|
42
|
+
};
|
|
43
|
+
/**
|
|
44
|
+
* @description 聊天列表的类型
|
|
45
|
+
* @default 'chat'
|
|
46
|
+
*/
|
|
47
|
+
type?: 'docs' | 'chat';
|
|
48
|
+
}
|
|
49
|
+
declare const Item: import("react").NamedExoticComponent<ChatMessage & ListItemProps>;
|
|
50
|
+
export default Item;
|
|
@@ -0,0 +1,78 @@
|
|
|
1
|
+
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
|
+
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
3
|
+
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
+
var _excluded = ["renderMessageExtra", "showTitle", "onActionClick", "onMessageChange", "type", "text", "renderMessage", "renderErrorMessage", "loading"];
|
|
5
|
+
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
6
|
+
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
7
|
+
import { App } from 'antd';
|
|
8
|
+
import copy from 'copy-to-clipboard';
|
|
9
|
+
import { memo, useCallback, useState } from 'react';
|
|
10
|
+
import ChatItem from "../ChatItem";
|
|
11
|
+
import ActionsBar from "./ActionsBar";
|
|
12
|
+
import { jsx as _jsx } from "react/jsx-runtime";
|
|
13
|
+
var Item = /*#__PURE__*/memo(function (_ref) {
|
|
14
|
+
var _item$error;
|
|
15
|
+
var MessageExtra = _ref.renderMessageExtra,
|
|
16
|
+
showTitle = _ref.showTitle,
|
|
17
|
+
_onActionClick = _ref.onActionClick,
|
|
18
|
+
onMessageChange = _ref.onMessageChange,
|
|
19
|
+
type = _ref.type,
|
|
20
|
+
text = _ref.text,
|
|
21
|
+
renderMessage = _ref.renderMessage,
|
|
22
|
+
renderErrorMessage = _ref.renderErrorMessage,
|
|
23
|
+
loading = _ref.loading,
|
|
24
|
+
item = _objectWithoutProperties(_ref, _excluded);
|
|
25
|
+
var renderMessageExtra = MessageExtra ? /*#__PURE__*/_jsx(MessageExtra, _objectSpread({}, item)) : undefined;
|
|
26
|
+
var _useState = useState(false),
|
|
27
|
+
_useState2 = _slicedToArray(_useState, 2),
|
|
28
|
+
editing = _useState2[0],
|
|
29
|
+
setEditing = _useState2[1];
|
|
30
|
+
var _App$useApp = App.useApp(),
|
|
31
|
+
message = _App$useApp.message;
|
|
32
|
+
var innerRenderMessage = useCallback(function (content) {
|
|
33
|
+
return renderMessage === null || renderMessage === void 0 ? void 0 : renderMessage(content, item);
|
|
34
|
+
}, [renderMessage, item]);
|
|
35
|
+
return /*#__PURE__*/_jsx(ChatItem, {
|
|
36
|
+
ErrorMessage: item.error ? renderErrorMessage === null || renderErrorMessage === void 0 ? void 0 : renderErrorMessage(item.error, item) : undefined,
|
|
37
|
+
actions: /*#__PURE__*/_jsx(ActionsBar, {
|
|
38
|
+
onActionClick: function onActionClick(actionKey) {
|
|
39
|
+
switch (actionKey) {
|
|
40
|
+
case 'copy':
|
|
41
|
+
{
|
|
42
|
+
copy(item.content);
|
|
43
|
+
message.success((text === null || text === void 0 ? void 0 : text.copySuccess) || 'Copy Success');
|
|
44
|
+
break;
|
|
45
|
+
}
|
|
46
|
+
case 'edit':
|
|
47
|
+
{
|
|
48
|
+
setEditing(true);
|
|
49
|
+
break;
|
|
50
|
+
}
|
|
51
|
+
}
|
|
52
|
+
_onActionClick === null || _onActionClick === void 0 ? void 0 : _onActionClick(actionKey, item.id);
|
|
53
|
+
},
|
|
54
|
+
primary: item.role === 'user',
|
|
55
|
+
text: text
|
|
56
|
+
}),
|
|
57
|
+
avatar: item.meta,
|
|
58
|
+
editing: editing,
|
|
59
|
+
error: item.error ? {
|
|
60
|
+
message: (_item$error = item.error) === null || _item$error === void 0 ? void 0 : _item$error.message
|
|
61
|
+
} : undefined,
|
|
62
|
+
loading: loading,
|
|
63
|
+
message: item.content,
|
|
64
|
+
messageExtra: renderMessageExtra,
|
|
65
|
+
onChange: function onChange(value) {
|
|
66
|
+
onMessageChange === null || onMessageChange === void 0 ? void 0 : onMessageChange(item.id, value);
|
|
67
|
+
},
|
|
68
|
+
onEditingChange: setEditing,
|
|
69
|
+
placement: type === 'chat' ? item.role === 'user' ? 'right' : 'left' : 'left',
|
|
70
|
+
primary: item.role === 'user',
|
|
71
|
+
renderMessage: renderMessage ? innerRenderMessage : undefined,
|
|
72
|
+
showTitle: showTitle,
|
|
73
|
+
text: text,
|
|
74
|
+
time: item.updateAt || item.createAt,
|
|
75
|
+
type: type === 'chat' ? 'block' : 'pure'
|
|
76
|
+
});
|
|
77
|
+
});
|
|
78
|
+
export default Item;
|
package/es/ChatList/index.d.ts
CHANGED
|
@@ -1,46 +1,13 @@
|
|
|
1
|
-
|
|
2
|
-
import {
|
|
3
|
-
import
|
|
4
|
-
|
|
5
|
-
import { type ActionsBarProps } from './ActionsBar';
|
|
6
|
-
export type MessageExtra = (props: ChatMessage) => ReactNode;
|
|
7
|
-
export type RenderMessage = (content: ReactNode, message: ChatMessage) => ReactNode;
|
|
8
|
-
export interface ItemProps extends ChatMessage {
|
|
9
|
-
MessageExtra?: MessageExtra;
|
|
10
|
-
onActionClick?: (actionKey: string, messageId: string) => void;
|
|
11
|
-
onMessageChange?: (id: string, content: string) => void;
|
|
12
|
-
renderMessage?: RenderMessage;
|
|
13
|
-
showTitle?: boolean;
|
|
14
|
-
text?: ChatItemProps['text'] & ActionsBarProps['text'] & {
|
|
15
|
-
copySuccess?: string;
|
|
16
|
-
};
|
|
17
|
-
type: 'docs' | 'chat';
|
|
18
|
-
}
|
|
19
|
-
export interface ChatListProps extends DivProps {
|
|
1
|
+
/// <reference types="react" />
|
|
2
|
+
import type { ChatMessage, DivProps } from "../types";
|
|
3
|
+
import { ListItemProps } from './Item';
|
|
4
|
+
export interface ChatListProps extends DivProps, ListItemProps {
|
|
20
5
|
/**
|
|
21
6
|
* @description Data of chat messages to be displayed
|
|
22
7
|
*/
|
|
23
8
|
data: ChatMessage[];
|
|
24
|
-
|
|
25
|
-
* @description Callback function triggered when an action is clicked
|
|
26
|
-
* @param {string} actionKey - The key of the action
|
|
27
|
-
* @param {string} messageId - The id of the message
|
|
28
|
-
*/
|
|
29
|
-
onActionClick?: (actionKey: string, messageId: string) => void;
|
|
30
|
-
onMessageChange?: (id: string, content: string) => void;
|
|
31
|
-
renderMessage?: RenderMessage;
|
|
32
|
-
renderMessageExtra?: MessageExtra;
|
|
33
|
-
/**
|
|
34
|
-
* @description Whether to show name of the chat item
|
|
35
|
-
* @default false
|
|
36
|
-
*/
|
|
37
|
-
showTitle?: ChatItemProps['showTitle'];
|
|
38
|
-
text?: ItemProps['text'];
|
|
39
|
-
/**
|
|
40
|
-
* @description Type of chat list
|
|
41
|
-
* @default 'chat'
|
|
42
|
-
*/
|
|
43
|
-
type?: 'docs' | 'chat';
|
|
9
|
+
loadingId?: string;
|
|
44
10
|
}
|
|
11
|
+
export type { OnActionClick, OnMessageChange, RenderErrorMessage, RenderMessage } from './Item';
|
|
45
12
|
declare const ChatList: import("react").NamedExoticComponent<ChatListProps>;
|
|
46
13
|
export default ChatList;
|
package/es/ChatList/index.js
CHANGED
|
@@ -1,86 +1,26 @@
|
|
|
1
|
-
import _slicedToArray from "@babel/runtime/helpers/esm/slicedToArray";
|
|
2
1
|
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
3
2
|
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
-
var _excluded = ["
|
|
5
|
-
_excluded2 = ["onActionClick", "renderMessageExtra", "className", "data", "type", "text", "showTitle", "onMessageChange", "renderMessage"];
|
|
3
|
+
var _excluded = ["onActionClick", "renderMessageExtra", "className", "data", "type", "text", "showTitle", "onMessageChange", "renderMessage", "renderErrorMessage", "loadingId"];
|
|
6
4
|
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
7
5
|
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
8
|
-
import {
|
|
9
|
-
import
|
|
10
|
-
import { memo, useCallback, useState } from 'react';
|
|
11
|
-
import ChatItem from "../ChatItem";
|
|
12
|
-
import ActionsBar from "./ActionsBar";
|
|
6
|
+
import { memo } from 'react';
|
|
7
|
+
import Item from "./Item";
|
|
13
8
|
import { useStyles } from "./style";
|
|
14
9
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
15
|
-
var
|
|
16
|
-
var
|
|
10
|
+
var ChatList = /*#__PURE__*/memo(function (_ref) {
|
|
11
|
+
var onActionClick = _ref.onActionClick,
|
|
12
|
+
MessageExtra = _ref.renderMessageExtra,
|
|
13
|
+
className = _ref.className,
|
|
14
|
+
data = _ref.data,
|
|
15
|
+
_ref$type = _ref.type,
|
|
16
|
+
type = _ref$type === void 0 ? 'chat' : _ref$type,
|
|
17
|
+
text = _ref.text,
|
|
17
18
|
showTitle = _ref.showTitle,
|
|
18
|
-
_onActionClick = _ref.onActionClick,
|
|
19
19
|
onMessageChange = _ref.onMessageChange,
|
|
20
|
-
type = _ref.type,
|
|
21
|
-
text = _ref.text,
|
|
22
20
|
renderMessage = _ref.renderMessage,
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
26
|
-
_useState2 = _slicedToArray(_useState, 2),
|
|
27
|
-
editing = _useState2[0],
|
|
28
|
-
setEditing = _useState2[1];
|
|
29
|
-
var _App$useApp = App.useApp(),
|
|
30
|
-
message = _App$useApp.message;
|
|
31
|
-
var innerRenderMessage = useCallback(function (content) {
|
|
32
|
-
return renderMessage === null || renderMessage === void 0 ? void 0 : renderMessage(content, item);
|
|
33
|
-
}, [renderMessage]);
|
|
34
|
-
return /*#__PURE__*/_jsx(ChatItem, {
|
|
35
|
-
actions: /*#__PURE__*/_jsx(ActionsBar, {
|
|
36
|
-
onActionClick: function onActionClick(actionKey) {
|
|
37
|
-
switch (actionKey) {
|
|
38
|
-
case 'copy':
|
|
39
|
-
{
|
|
40
|
-
copy(item.content);
|
|
41
|
-
message.success((text === null || text === void 0 ? void 0 : text.copySuccess) || 'Copy Success');
|
|
42
|
-
break;
|
|
43
|
-
}
|
|
44
|
-
case 'edit':
|
|
45
|
-
{
|
|
46
|
-
setEditing(true);
|
|
47
|
-
break;
|
|
48
|
-
}
|
|
49
|
-
}
|
|
50
|
-
_onActionClick === null || _onActionClick === void 0 ? void 0 : _onActionClick(actionKey, item.id);
|
|
51
|
-
},
|
|
52
|
-
primary: item.role === 'user',
|
|
53
|
-
text: text
|
|
54
|
-
}),
|
|
55
|
-
avatar: item.meta,
|
|
56
|
-
editing: editing,
|
|
57
|
-
message: item.content,
|
|
58
|
-
messageExtra: renderMessageExtra,
|
|
59
|
-
onChange: function onChange(value) {
|
|
60
|
-
onMessageChange === null || onMessageChange === void 0 ? void 0 : onMessageChange(item.id, value);
|
|
61
|
-
},
|
|
62
|
-
onEditingChange: setEditing,
|
|
63
|
-
placement: type === 'chat' ? item.role === 'user' ? 'right' : 'left' : 'left',
|
|
64
|
-
primary: item.role === 'user',
|
|
65
|
-
renderMessage: renderMessage ? innerRenderMessage : undefined,
|
|
66
|
-
showTitle: showTitle,
|
|
67
|
-
text: text,
|
|
68
|
-
time: item.updateAt || item.createAt,
|
|
69
|
-
type: type === 'chat' ? 'block' : 'pure'
|
|
70
|
-
});
|
|
71
|
-
};
|
|
72
|
-
var ChatList = /*#__PURE__*/memo(function (_ref2) {
|
|
73
|
-
var onActionClick = _ref2.onActionClick,
|
|
74
|
-
MessageExtra = _ref2.renderMessageExtra,
|
|
75
|
-
className = _ref2.className,
|
|
76
|
-
data = _ref2.data,
|
|
77
|
-
_ref2$type = _ref2.type,
|
|
78
|
-
type = _ref2$type === void 0 ? 'chat' : _ref2$type,
|
|
79
|
-
text = _ref2.text,
|
|
80
|
-
showTitle = _ref2.showTitle,
|
|
81
|
-
onMessageChange = _ref2.onMessageChange,
|
|
82
|
-
renderMessage = _ref2.renderMessage,
|
|
83
|
-
props = _objectWithoutProperties(_ref2, _excluded2);
|
|
21
|
+
renderErrorMessage = _ref.renderErrorMessage,
|
|
22
|
+
loadingId = _ref.loadingId,
|
|
23
|
+
props = _objectWithoutProperties(_ref, _excluded);
|
|
84
24
|
var _useStyles = useStyles(),
|
|
85
25
|
cx = _useStyles.cx,
|
|
86
26
|
styles = _useStyles.styles;
|
|
@@ -89,10 +29,12 @@ var ChatList = /*#__PURE__*/memo(function (_ref2) {
|
|
|
89
29
|
}, props), {}, {
|
|
90
30
|
children: data.map(function (item) {
|
|
91
31
|
return /*#__PURE__*/_jsx(Item, _objectSpread({
|
|
92
|
-
|
|
32
|
+
loading: loadingId === item.id,
|
|
93
33
|
onActionClick: onActionClick,
|
|
94
34
|
onMessageChange: onMessageChange,
|
|
35
|
+
renderErrorMessage: renderErrorMessage,
|
|
95
36
|
renderMessage: renderMessage,
|
|
37
|
+
renderMessageExtra: MessageExtra,
|
|
96
38
|
showTitle: showTitle,
|
|
97
39
|
text: text,
|
|
98
40
|
type: type
|
|
@@ -1,10 +1,10 @@
|
|
|
1
1
|
/// <reference types="react" />
|
|
2
|
-
import {
|
|
2
|
+
import { LLMMessage } from "../types/llm";
|
|
3
3
|
export interface EditableMessageListProps {
|
|
4
4
|
/**
|
|
5
5
|
* @description The data sources to be rendered
|
|
6
6
|
*/
|
|
7
|
-
dataSources:
|
|
7
|
+
dataSources: LLMMessage[];
|
|
8
8
|
/**
|
|
9
9
|
* @description Whether the component is disabled or not
|
|
10
10
|
* @default false
|
|
@@ -14,7 +14,7 @@ export interface EditableMessageListProps {
|
|
|
14
14
|
* @description Callback function triggered when the data sources are changed
|
|
15
15
|
* @param chatMessages - the updated data sources
|
|
16
16
|
*/
|
|
17
|
-
onChange?: (chatMessages:
|
|
17
|
+
onChange?: (chatMessages: LLMMessage[]) => void;
|
|
18
18
|
}
|
|
19
19
|
export declare const EditableMessageList: import("react").NamedExoticComponent<EditableMessageListProps>;
|
|
20
20
|
export default EditableMessageList;
|
|
@@ -5,9 +5,9 @@ import { Plus, Trash } from 'lucide-react';
|
|
|
5
5
|
import { memo, useEffect, useReducer } from 'react';
|
|
6
6
|
import { Flexbox } from 'react-layout-kit';
|
|
7
7
|
import ActionIcon from "../ActionIcon";
|
|
8
|
-
import { messagesReducer } from "../Chat";
|
|
9
8
|
import Icon from "../Icon";
|
|
10
9
|
import { ControlInput } from "../components/ControlInput";
|
|
10
|
+
import { messagesReducer } from "./messageReducer";
|
|
11
11
|
import { jsx as _jsx } from "react/jsx-runtime";
|
|
12
12
|
import { jsxs as _jsxs } from "react/jsx-runtime";
|
|
13
13
|
export var EditableMessageList = /*#__PURE__*/memo(function (_ref) {
|
|
@@ -1,10 +1,11 @@
|
|
|
1
|
-
import {
|
|
1
|
+
import { ChatMessageError, MessageRoleType } from "../types";
|
|
2
|
+
import { LLMMessage } from "../types/llm";
|
|
2
3
|
export type MessageDispatch = {
|
|
3
|
-
message:
|
|
4
|
+
message: LLMMessage;
|
|
4
5
|
type: 'addMessage';
|
|
5
6
|
} | {
|
|
6
7
|
index: number;
|
|
7
|
-
message:
|
|
8
|
+
message: LLMMessage;
|
|
8
9
|
type: 'insertMessage';
|
|
9
10
|
} | {
|
|
10
11
|
index: number;
|
|
@@ -34,4 +35,4 @@ export type MessageDispatch = {
|
|
|
34
35
|
index: number;
|
|
35
36
|
type: 'setErrorMessage';
|
|
36
37
|
};
|
|
37
|
-
export declare const messagesReducer: (state:
|
|
38
|
+
export declare const messagesReducer: (state: LLMMessage[], payload: MessageDispatch) => LLMMessage[];
|
|
@@ -66,21 +66,6 @@ export var messagesReducer = function messagesReducer(state, payload) {
|
|
|
66
66
|
draftState[index].error = error;
|
|
67
67
|
});
|
|
68
68
|
}
|
|
69
|
-
case 'updateMessageChoice':
|
|
70
|
-
{
|
|
71
|
-
return produce(state, function (draftState) {
|
|
72
|
-
var index = payload.index,
|
|
73
|
-
message = payload.message;
|
|
74
|
-
var botMessage = draftState[index];
|
|
75
|
-
var previousMessage = botMessage.content;
|
|
76
|
-
botMessage.content = message;
|
|
77
|
-
if (botMessage.choices) {
|
|
78
|
-
botMessage.choices.push(previousMessage);
|
|
79
|
-
} else {
|
|
80
|
-
botMessage.choices = [previousMessage];
|
|
81
|
-
}
|
|
82
|
-
});
|
|
83
|
-
}
|
|
84
69
|
default:
|
|
85
70
|
{
|
|
86
71
|
throw new Error('暂未实现的 type,请检查 reducer');
|
package/es/index.d.ts
CHANGED
|
@@ -2,11 +2,11 @@ export { default as ActionIcon, type ActionIconProps, type ActionIconSize } from
|
|
|
2
2
|
export { default as ActionIconGroup, type ActionIconGroupProps } from './ActionIconGroup';
|
|
3
3
|
export { default as Avatar, type AvatarProps } from './Avatar';
|
|
4
4
|
export { default as Burger, type BurgerProps } from './Burger';
|
|
5
|
-
export type { ChatMessage, MessageRoleType } from './Chat';
|
|
6
5
|
export { default as ChatHeader, type ChatHeaderProps } from './ChatHeader';
|
|
7
6
|
export { default as ChatInputArea, type ChatInputAreaProps } from './ChatInputArea';
|
|
8
7
|
export { default as ChatItem, type ChatItemProps } from './ChatItem';
|
|
9
|
-
export {
|
|
8
|
+
export type { ChatListProps, OnActionClick, OnMessageChange, RenderErrorMessage, RenderMessage, } from './ChatList';
|
|
9
|
+
export { default as ChatList } from './ChatList';
|
|
10
10
|
export { default as CodeEditor, type CodeEditorProps } from './CodeEditor';
|
|
11
11
|
export { default as ColorScales, type ColorScalesProps } from './ColorScales';
|
|
12
12
|
export { default as ContextMenu, type ContextMenuProps } from './ContextMenu';
|
|
@@ -1,5 +1,5 @@
|
|
|
1
|
-
import { LLMRoleType } from './llm';
|
|
2
1
|
import { BaseDataModel } from './meta';
|
|
2
|
+
export type MessageRoleType = 'user' | 'system' | 'assistant' | 'function';
|
|
3
3
|
/**
|
|
4
4
|
* 聊天消息错误对象
|
|
5
5
|
*/
|
|
@@ -8,8 +8,7 @@ export interface ChatMessageError {
|
|
|
8
8
|
* 错误信息
|
|
9
9
|
*/
|
|
10
10
|
message: string;
|
|
11
|
-
|
|
12
|
-
type: 'general' | 'llm';
|
|
11
|
+
type?: string;
|
|
13
12
|
}
|
|
14
13
|
export interface ChatMessage extends BaseDataModel {
|
|
15
14
|
/**
|
|
@@ -17,6 +16,7 @@ export interface ChatMessage extends BaseDataModel {
|
|
|
17
16
|
* @description 消息内容
|
|
18
17
|
*/
|
|
19
18
|
content: string;
|
|
19
|
+
error?: ChatMessageError;
|
|
20
20
|
extra?: {
|
|
21
21
|
translate?: {
|
|
22
22
|
target: string;
|
|
@@ -29,5 +29,5 @@ export interface ChatMessage extends BaseDataModel {
|
|
|
29
29
|
* 角色
|
|
30
30
|
* @description 消息发送者的角色
|
|
31
31
|
*/
|
|
32
|
-
role:
|
|
32
|
+
role: MessageRoleType;
|
|
33
33
|
}
|
package/es/types/llm.d.ts
CHANGED
|
@@ -1,42 +1,22 @@
|
|
|
1
|
+
import { ChatMessageError, MessageRoleType } from "./chatMessage";
|
|
1
2
|
/**
|
|
2
|
-
*
|
|
3
|
+
* @title ChatGPTMessage 聊天消息
|
|
4
|
+
* @category Model
|
|
3
5
|
*/
|
|
4
|
-
export
|
|
5
|
-
/**
|
|
6
|
-
* GPT 3.5 Turbo
|
|
7
|
-
*/
|
|
8
|
-
GPT3_5 = "gpt-3.5-turbo",
|
|
9
|
-
/**
|
|
10
|
-
* GPT 4
|
|
11
|
-
*/
|
|
12
|
-
GPT4 = "gpt-4"
|
|
13
|
-
}
|
|
14
|
-
export interface LMParameters {
|
|
15
|
-
/**
|
|
16
|
-
* 控制生成文本中的惩罚系数,用于减少重复性
|
|
17
|
-
*/
|
|
18
|
-
frequency_penalty?: number;
|
|
19
|
-
/**
|
|
20
|
-
* 生成文本的最大长度
|
|
21
|
-
*/
|
|
22
|
-
max_tokens?: number;
|
|
6
|
+
export interface LLMMessage {
|
|
23
7
|
/**
|
|
24
|
-
*
|
|
8
|
+
* @title 内容
|
|
9
|
+
* @description 消息内容
|
|
25
10
|
*/
|
|
26
|
-
|
|
11
|
+
content: string;
|
|
27
12
|
/**
|
|
28
|
-
*
|
|
29
|
-
* @default 0.8
|
|
13
|
+
* 如果存在错误消息
|
|
30
14
|
*/
|
|
31
|
-
|
|
15
|
+
error?: ChatMessageError;
|
|
32
16
|
/**
|
|
33
|
-
*
|
|
17
|
+
* @title 角色
|
|
18
|
+
* @description 消息发送者的角色
|
|
19
|
+
* @enum {MessageRoleType} ChatGPTAgent
|
|
34
20
|
*/
|
|
35
|
-
|
|
36
|
-
}
|
|
37
|
-
export type LLMRoleType = 'user' | 'system' | 'assistant' | 'function';
|
|
38
|
-
export interface LLMMessage {
|
|
39
|
-
content: string;
|
|
40
|
-
role: LLMRoleType;
|
|
21
|
+
role: MessageRoleType;
|
|
41
22
|
}
|
|
42
|
-
export type LLMExample = LLMMessage[];
|
package/es/types/llm.js
CHANGED
package/package.json
CHANGED
package/es/Chat/const.d.ts
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export declare const LOADING_FLAT = "...";
|
package/es/Chat/const.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export var LOADING_FLAT = '...';
|
package/es/Chat/index.d.ts
DELETED
package/es/Chat/index.js
DELETED
package/es/Chat/store/index.d.ts
DELETED
|
@@ -1,19 +0,0 @@
|
|
|
1
|
-
/// <reference types="react" />
|
|
2
|
-
import { StoreApi } from 'zustand';
|
|
3
|
-
import { ChatStore } from './store';
|
|
4
|
-
export declare const createChatStore: (showDevtools?: boolean) => import("zustand").UseBoundStore<Omit<StoreApi<ChatStore>, "setState"> & {
|
|
5
|
-
setState<A extends string | {
|
|
6
|
-
type: unknown;
|
|
7
|
-
}>(partial: ChatStore | Partial<ChatStore> | ((state: ChatStore) => ChatStore | Partial<ChatStore>), replace?: boolean | undefined, action?: A | undefined): void;
|
|
8
|
-
}>;
|
|
9
|
-
export declare const Provider: ({ createStore, children }: {
|
|
10
|
-
createStore: () => StoreApi<ChatStore>;
|
|
11
|
-
children: import("react").ReactNode;
|
|
12
|
-
}) => import("react").FunctionComponentElement<import("react").ProviderProps<StoreApi<ChatStore> | undefined>>, useStore: import("zustand-utils").UseContextStore<StoreApi<ChatStore>>, useStoreApi: () => {
|
|
13
|
-
setState: (partial: ChatStore | Partial<ChatStore> | ((state: ChatStore) => ChatStore | Partial<ChatStore>), replace?: boolean | undefined) => void;
|
|
14
|
-
getState: () => ChatStore;
|
|
15
|
-
subscribe: (listener: (state: ChatStore, prevState: ChatStore) => void) => () => void;
|
|
16
|
-
destroy: () => void;
|
|
17
|
-
};
|
|
18
|
-
export { type MessageDispatch, messagesReducer } from './messageReducer';
|
|
19
|
-
export type { ChatStore } from './store';
|
package/es/Chat/store/index.js
DELETED
|
@@ -1,15 +0,0 @@
|
|
|
1
|
-
import { create } from 'zustand';
|
|
2
|
-
import { createContext, optionalDevtools } from 'zustand-utils';
|
|
3
|
-
import { createStore } from "./store";
|
|
4
|
-
export var createChatStore = function createChatStore() {
|
|
5
|
-
var showDevtools = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : false;
|
|
6
|
-
return create()(optionalDevtools(showDevtools)(createStore, {
|
|
7
|
-
name: 'CHAT_COMPONENT'
|
|
8
|
-
}));
|
|
9
|
-
};
|
|
10
|
-
var _createContext = createContext(),
|
|
11
|
-
Provider = _createContext.Provider,
|
|
12
|
-
useStore = _createContext.useStore,
|
|
13
|
-
useStoreApi = _createContext.useStoreApi;
|
|
14
|
-
export { Provider, useStore, useStoreApi };
|
|
15
|
-
export { messagesReducer } from "./messageReducer";
|
|
@@ -1,28 +0,0 @@
|
|
|
1
|
-
import { ChatMessage, InternalChatContext, OpenAIRequestParameters } from '../types';
|
|
2
|
-
export interface ChatState extends InternalChatContext {
|
|
3
|
-
/**
|
|
4
|
-
* @title 改变系统角色状态
|
|
5
|
-
* @type {boolean}
|
|
6
|
-
*/
|
|
7
|
-
changingSystemRole: boolean;
|
|
8
|
-
/**
|
|
9
|
-
* 编辑中的消息 id
|
|
10
|
-
*/
|
|
11
|
-
editingMessageId?: number | undefined;
|
|
12
|
-
/**
|
|
13
|
-
* @title 加载状态
|
|
14
|
-
* @type {boolean}
|
|
15
|
-
*/
|
|
16
|
-
loading: boolean;
|
|
17
|
-
/**
|
|
18
|
-
* @title 消息
|
|
19
|
-
* @type {string}
|
|
20
|
-
*/
|
|
21
|
-
message: string;
|
|
22
|
-
onAgentChange?: (agent: any, type: 'update' | 'remove') => void;
|
|
23
|
-
onMessagesChange?: (messages: ChatMessage[]) => void;
|
|
24
|
-
onResponseFinished?: (session: any) => void;
|
|
25
|
-
onResponseStart?: (messages: ChatMessage[]) => Promise<void>;
|
|
26
|
-
request?: (parameters: OpenAIRequestParameters) => Promise<Response>;
|
|
27
|
-
}
|
|
28
|
-
export declare const initialState: ChatState;
|
package/es/Chat/store/store.d.ts
DELETED
|
@@ -1,34 +0,0 @@
|
|
|
1
|
-
import { StateCreator } from 'zustand/vanilla';
|
|
2
|
-
import { ChatMessage } from '../types';
|
|
3
|
-
import { FetchSSEOptions } from '../utils/fetch';
|
|
4
|
-
import { ChatState } from './initialState';
|
|
5
|
-
import { MessageDispatch } from './messageReducer';
|
|
6
|
-
interface ChatAction {
|
|
7
|
-
/**
|
|
8
|
-
* @title 派发消息
|
|
9
|
-
* @param payload - 消息分发
|
|
10
|
-
* @returns void
|
|
11
|
-
*/
|
|
12
|
-
dispatchMessage: (payload: MessageDispatch) => void;
|
|
13
|
-
generateMessage: (message: string, messages: ChatMessage[], options: FetchSSEOptions) => Promise<void>;
|
|
14
|
-
/**
|
|
15
|
-
* @title 处理消息编辑
|
|
16
|
-
* @param index - 消息索引或空
|
|
17
|
-
* @returns void
|
|
18
|
-
*/
|
|
19
|
-
handleMessageEditing: (index?: number | undefined) => void;
|
|
20
|
-
/**
|
|
21
|
-
* @title 重发消息
|
|
22
|
-
* @param index - 消息索引
|
|
23
|
-
* @returns Promise<void>
|
|
24
|
-
*/
|
|
25
|
-
resendMessage: (index: number) => Promise<void>;
|
|
26
|
-
/**
|
|
27
|
-
* @title 发送消息
|
|
28
|
-
* @returns Promise<void>
|
|
29
|
-
*/
|
|
30
|
-
sendMessage: () => Promise<void>;
|
|
31
|
-
}
|
|
32
|
-
export type ChatStore = ChatAction & ChatState;
|
|
33
|
-
export declare const createStore: StateCreator<ChatStore, [['zustand/devtools', never]]>;
|
|
34
|
-
export {};
|
package/es/Chat/store/store.js
DELETED
|
@@ -1,240 +0,0 @@
|
|
|
1
|
-
import _toConsumableArray from "@babel/runtime/helpers/esm/toConsumableArray";
|
|
2
|
-
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
3
|
-
import _objectWithoutProperties from "@babel/runtime/helpers/esm/objectWithoutProperties";
|
|
4
|
-
import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
|
|
5
|
-
var _excluded = ["type"];
|
|
6
|
-
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
7
|
-
function ownKeys(object, enumerableOnly) { var keys = Object.keys(object); if (Object.getOwnPropertySymbols) { var symbols = Object.getOwnPropertySymbols(object); enumerableOnly && (symbols = symbols.filter(function (sym) { return Object.getOwnPropertyDescriptor(object, sym).enumerable; })), keys.push.apply(keys, symbols); } return keys; }
|
|
8
|
-
function _objectSpread(target) { for (var i = 1; i < arguments.length; i++) { var source = null != arguments[i] ? arguments[i] : {}; i % 2 ? ownKeys(Object(source), !0).forEach(function (key) { _defineProperty(target, key, source[key]); }) : Object.getOwnPropertyDescriptors ? Object.defineProperties(target, Object.getOwnPropertyDescriptors(source)) : ownKeys(Object(source)).forEach(function (key) { Object.defineProperty(target, key, Object.getOwnPropertyDescriptor(source, key)); }); } return target; }
|
|
9
|
-
import { LOADING_FLAT } from "../const";
|
|
10
|
-
import { fetchSSE } from "../utils/fetch";
|
|
11
|
-
import { initialState } from "./initialState";
|
|
12
|
-
import { messagesReducer } from "./messageReducer";
|
|
13
|
-
export var createStore = function createStore(set, get) {
|
|
14
|
-
return _objectSpread(_objectSpread({}, initialState), {}, {
|
|
15
|
-
dispatchMessage: function dispatchMessage(payload) {
|
|
16
|
-
var _get$onMessagesChange, _get;
|
|
17
|
-
var type = payload.type,
|
|
18
|
-
res = _objectWithoutProperties(payload, _excluded);
|
|
19
|
-
var messages = messagesReducer(get().messages, payload);
|
|
20
|
-
set({
|
|
21
|
-
messages: messages
|
|
22
|
-
}, false, {
|
|
23
|
-
payload: res,
|
|
24
|
-
type: "dispatchMessage/".concat(type)
|
|
25
|
-
});
|
|
26
|
-
(_get$onMessagesChange = (_get = get()).onMessagesChange) === null || _get$onMessagesChange === void 0 ? void 0 : _get$onMessagesChange.call(_get, messages);
|
|
27
|
-
},
|
|
28
|
-
generateMessage: function () {
|
|
29
|
-
var _generateMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(message, messages, options) {
|
|
30
|
-
var _get2, onResponseStart, request, onResponseFinished, fetcher;
|
|
31
|
-
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
32
|
-
while (1) switch (_context.prev = _context.next) {
|
|
33
|
-
case 0:
|
|
34
|
-
_get2 = get(), onResponseStart = _get2.onResponseStart, request = _get2.request, onResponseFinished = _get2.onResponseFinished;
|
|
35
|
-
if (request) {
|
|
36
|
-
_context.next = 3;
|
|
37
|
-
break;
|
|
38
|
-
}
|
|
39
|
-
return _context.abrupt("return");
|
|
40
|
-
case 3:
|
|
41
|
-
_context.next = 5;
|
|
42
|
-
return onResponseStart === null || onResponseStart === void 0 ? void 0 : onResponseStart(get().messages);
|
|
43
|
-
case 5:
|
|
44
|
-
set({
|
|
45
|
-
loading: true
|
|
46
|
-
});
|
|
47
|
-
fetcher = function fetcher() {
|
|
48
|
-
return request({
|
|
49
|
-
messages: messages
|
|
50
|
-
});
|
|
51
|
-
};
|
|
52
|
-
_context.next = 9;
|
|
53
|
-
return fetchSSE(fetcher, options);
|
|
54
|
-
case 9:
|
|
55
|
-
set({
|
|
56
|
-
loading: false
|
|
57
|
-
});
|
|
58
|
-
onResponseFinished === null || onResponseFinished === void 0 ? void 0 : onResponseFinished({
|
|
59
|
-
messages: get().messages
|
|
60
|
-
});
|
|
61
|
-
case 11:
|
|
62
|
-
case "end":
|
|
63
|
-
return _context.stop();
|
|
64
|
-
}
|
|
65
|
-
}, _callee);
|
|
66
|
-
}));
|
|
67
|
-
function generateMessage(_x, _x2, _x3) {
|
|
68
|
-
return _generateMessage.apply(this, arguments);
|
|
69
|
-
}
|
|
70
|
-
return generateMessage;
|
|
71
|
-
}(),
|
|
72
|
-
handleMessageEditing: function handleMessageEditing(index) {
|
|
73
|
-
set({
|
|
74
|
-
editingMessageId: index
|
|
75
|
-
});
|
|
76
|
-
},
|
|
77
|
-
resendMessage: function () {
|
|
78
|
-
var _resendMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee2(index) {
|
|
79
|
-
var _contextMessages$at;
|
|
80
|
-
var _get3, dispatchMessage, sendMessage, generateMessage, messages, lastMessage, contextMessages, userMessage, targetMessage, botPreviousMessage, currentResponse;
|
|
81
|
-
return _regeneratorRuntime.wrap(function _callee2$(_context2) {
|
|
82
|
-
while (1) switch (_context2.prev = _context2.next) {
|
|
83
|
-
case 0:
|
|
84
|
-
_get3 = get(), dispatchMessage = _get3.dispatchMessage, sendMessage = _get3.sendMessage, generateMessage = _get3.generateMessage, messages = _get3.messages;
|
|
85
|
-
lastMessage = messages.at(-1); // 用户通过手动删除,造成了他的问题是最后一条消息
|
|
86
|
-
// 这种情况下,相当于用户重新发送消息
|
|
87
|
-
if (!(messages.length === index && (lastMessage === null || lastMessage === void 0 ? void 0 : lastMessage.role) === 'user')) {
|
|
88
|
-
_context2.next = 8;
|
|
89
|
-
break;
|
|
90
|
-
}
|
|
91
|
-
dispatchMessage({
|
|
92
|
-
index: index - 1,
|
|
93
|
-
type: 'deleteMessage'
|
|
94
|
-
});
|
|
95
|
-
set({
|
|
96
|
-
message: lastMessage.content
|
|
97
|
-
});
|
|
98
|
-
_context2.next = 7;
|
|
99
|
-
return sendMessage();
|
|
100
|
-
case 7:
|
|
101
|
-
return _context2.abrupt("return");
|
|
102
|
-
case 8:
|
|
103
|
-
// 上下文消息就是当前消息之前的消息
|
|
104
|
-
contextMessages = get().messages.slice(0, index); // 上下文消息中最后一条消息
|
|
105
|
-
userMessage = (_contextMessages$at = contextMessages.at(-1)) === null || _contextMessages$at === void 0 ? void 0 : _contextMessages$at.content;
|
|
106
|
-
if (userMessage) {
|
|
107
|
-
_context2.next = 12;
|
|
108
|
-
break;
|
|
109
|
-
}
|
|
110
|
-
return _context2.abrupt("return");
|
|
111
|
-
case 12:
|
|
112
|
-
targetMessage = messages[index]; // 如果不是 assistant 的消息,那么需要额外插入一条消息
|
|
113
|
-
if (targetMessage.role === 'assistant') {
|
|
114
|
-
botPreviousMessage = targetMessage.content; // 保存之前的消息为历史消息
|
|
115
|
-
dispatchMessage({
|
|
116
|
-
index: index,
|
|
117
|
-
message: botPreviousMessage,
|
|
118
|
-
type: 'updateMessageChoice'
|
|
119
|
-
});
|
|
120
|
-
dispatchMessage({
|
|
121
|
-
index: index,
|
|
122
|
-
message: LOADING_FLAT,
|
|
123
|
-
type: 'updateMessage'
|
|
124
|
-
});
|
|
125
|
-
} else {
|
|
126
|
-
dispatchMessage({
|
|
127
|
-
index: index,
|
|
128
|
-
message: {
|
|
129
|
-
content: LOADING_FLAT,
|
|
130
|
-
role: 'assistant'
|
|
131
|
-
},
|
|
132
|
-
type: 'insertMessage'
|
|
133
|
-
});
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
// 重置错误信息
|
|
137
|
-
dispatchMessage({
|
|
138
|
-
error: undefined,
|
|
139
|
-
index: index,
|
|
140
|
-
type: 'setErrorMessage'
|
|
141
|
-
});
|
|
142
|
-
|
|
143
|
-
// 开始更新消息
|
|
144
|
-
currentResponse = [];
|
|
145
|
-
_context2.next = 18;
|
|
146
|
-
return generateMessage(userMessage, contextMessages, {
|
|
147
|
-
onErrorHandle: function onErrorHandle(error) {
|
|
148
|
-
dispatchMessage({
|
|
149
|
-
error: error,
|
|
150
|
-
index: index,
|
|
151
|
-
type: 'setErrorMessage'
|
|
152
|
-
});
|
|
153
|
-
},
|
|
154
|
-
onMessageHandle: function onMessageHandle(text) {
|
|
155
|
-
currentResponse = [].concat(_toConsumableArray(currentResponse), [text]);
|
|
156
|
-
dispatchMessage({
|
|
157
|
-
index: index,
|
|
158
|
-
message: currentResponse.join(''),
|
|
159
|
-
type: 'updateMessage'
|
|
160
|
-
});
|
|
161
|
-
}
|
|
162
|
-
});
|
|
163
|
-
case 18:
|
|
164
|
-
case "end":
|
|
165
|
-
return _context2.stop();
|
|
166
|
-
}
|
|
167
|
-
}, _callee2);
|
|
168
|
-
}));
|
|
169
|
-
function resendMessage(_x4) {
|
|
170
|
-
return _resendMessage.apply(this, arguments);
|
|
171
|
-
}
|
|
172
|
-
return resendMessage;
|
|
173
|
-
}(),
|
|
174
|
-
sendMessage: function () {
|
|
175
|
-
var _sendMessage = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee3() {
|
|
176
|
-
var _get4, message, dispatchMessage, generateMessage, messages, currentResponse;
|
|
177
|
-
return _regeneratorRuntime.wrap(function _callee3$(_context3) {
|
|
178
|
-
while (1) switch (_context3.prev = _context3.next) {
|
|
179
|
-
case 0:
|
|
180
|
-
_get4 = get(), message = _get4.message, dispatchMessage = _get4.dispatchMessage, generateMessage = _get4.generateMessage, messages = _get4.messages;
|
|
181
|
-
if (message) {
|
|
182
|
-
_context3.next = 3;
|
|
183
|
-
break;
|
|
184
|
-
}
|
|
185
|
-
return _context3.abrupt("return");
|
|
186
|
-
case 3:
|
|
187
|
-
set({
|
|
188
|
-
message: ''
|
|
189
|
-
});
|
|
190
|
-
dispatchMessage({
|
|
191
|
-
message: message,
|
|
192
|
-
type: 'addUserMessage'
|
|
193
|
-
});
|
|
194
|
-
|
|
195
|
-
// 添加一个空的信息用于放置 ai 响应
|
|
196
|
-
dispatchMessage({
|
|
197
|
-
message: {
|
|
198
|
-
content: LOADING_FLAT,
|
|
199
|
-
role: 'assistant'
|
|
200
|
-
},
|
|
201
|
-
type: 'addMessage'
|
|
202
|
-
});
|
|
203
|
-
currentResponse = []; // 生成 messages
|
|
204
|
-
_context3.next = 9;
|
|
205
|
-
return generateMessage(message, messages, {
|
|
206
|
-
onErrorHandle: function onErrorHandle(error) {
|
|
207
|
-
dispatchMessage({
|
|
208
|
-
error: error,
|
|
209
|
-
index: get().messages.length - 1,
|
|
210
|
-
type: 'setErrorMessage'
|
|
211
|
-
});
|
|
212
|
-
},
|
|
213
|
-
onMessageHandle: function onMessageHandle(text) {
|
|
214
|
-
currentResponse = [].concat(_toConsumableArray(currentResponse), [text]);
|
|
215
|
-
dispatchMessage({
|
|
216
|
-
responseStream: currentResponse,
|
|
217
|
-
type: 'updateLatestBotMessage'
|
|
218
|
-
});
|
|
219
|
-
|
|
220
|
-
// 滚动到最后一条消息
|
|
221
|
-
var item = document.querySelector('#for-loading');
|
|
222
|
-
if (!item) return;
|
|
223
|
-
item.scrollIntoView({
|
|
224
|
-
behavior: 'smooth'
|
|
225
|
-
});
|
|
226
|
-
}
|
|
227
|
-
});
|
|
228
|
-
case 9:
|
|
229
|
-
case "end":
|
|
230
|
-
return _context3.stop();
|
|
231
|
-
}
|
|
232
|
-
}, _callee3);
|
|
233
|
-
}));
|
|
234
|
-
function sendMessage() {
|
|
235
|
-
return _sendMessage.apply(this, arguments);
|
|
236
|
-
}
|
|
237
|
-
return sendMessage;
|
|
238
|
-
}()
|
|
239
|
-
});
|
|
240
|
-
};
|
package/es/Chat/types.d.ts
DELETED
|
@@ -1,77 +0,0 @@
|
|
|
1
|
-
export type InternalChatContext = Omit<ChatContext, 'id'>;
|
|
2
|
-
export type MessageRoleType = 'user' | 'system' | 'assistant' | 'function';
|
|
3
|
-
/**
|
|
4
|
-
* 聊天消息错误对象
|
|
5
|
-
*/
|
|
6
|
-
export interface ChatMessageError {
|
|
7
|
-
/**
|
|
8
|
-
* 错误信息
|
|
9
|
-
*/
|
|
10
|
-
message: string;
|
|
11
|
-
/**
|
|
12
|
-
* 错误状态码
|
|
13
|
-
*/
|
|
14
|
-
status: number;
|
|
15
|
-
/**
|
|
16
|
-
* 错误类型
|
|
17
|
-
* @enum ["chatbot", "openai"]
|
|
18
|
-
* @enumNames ["聊天机器人", "开放AI"]
|
|
19
|
-
*/
|
|
20
|
-
type: 'chatbot' | 'openai';
|
|
21
|
-
}
|
|
22
|
-
/**
|
|
23
|
-
* @title ChatGPTMessage 聊天消息
|
|
24
|
-
* @category Model
|
|
25
|
-
*/
|
|
26
|
-
export interface ChatMessage {
|
|
27
|
-
/**
|
|
28
|
-
* 其余生成项
|
|
29
|
-
*/
|
|
30
|
-
choices?: string[];
|
|
31
|
-
/**
|
|
32
|
-
* @title 内容
|
|
33
|
-
* @description 消息内容
|
|
34
|
-
*/
|
|
35
|
-
content: string;
|
|
36
|
-
/**
|
|
37
|
-
* 如果存在错误消息
|
|
38
|
-
*/
|
|
39
|
-
error?: ChatMessageError;
|
|
40
|
-
/**
|
|
41
|
-
* @title 角色
|
|
42
|
-
* @description 消息发送者的角色
|
|
43
|
-
* @enum {MessageRoleType} ChatGPTAgent
|
|
44
|
-
*/
|
|
45
|
-
role: MessageRoleType;
|
|
46
|
-
}
|
|
47
|
-
export interface ChatContext {
|
|
48
|
-
createAt: number;
|
|
49
|
-
/**
|
|
50
|
-
* @title 会话描述
|
|
51
|
-
* @description 用户设置的会话描述
|
|
52
|
-
* @type {string}
|
|
53
|
-
*/
|
|
54
|
-
description?: string;
|
|
55
|
-
id: string;
|
|
56
|
-
/**
|
|
57
|
-
* @title 消息列表
|
|
58
|
-
* @description 聊天室中的所有消息
|
|
59
|
-
* @type {ChatMessage[]}
|
|
60
|
-
*/
|
|
61
|
-
messages: ChatMessage[];
|
|
62
|
-
/**
|
|
63
|
-
* @title 会话标题
|
|
64
|
-
*/
|
|
65
|
-
title?: string;
|
|
66
|
-
updateAt: number;
|
|
67
|
-
}
|
|
68
|
-
export type ChatContextMap = Record<string, Omit<ChatContext, 'systemRole'>>;
|
|
69
|
-
/**
|
|
70
|
-
* 请求数据类型
|
|
71
|
-
*/
|
|
72
|
-
export interface OpenAIRequestParameters {
|
|
73
|
-
/**
|
|
74
|
-
* 中间的聊天记录
|
|
75
|
-
*/
|
|
76
|
-
messages?: ChatMessage[];
|
|
77
|
-
}
|
package/es/Chat/types.js
DELETED
|
@@ -1 +0,0 @@
|
|
|
1
|
-
export {};
|
package/es/Chat/utils/fetch.d.ts
DELETED
|
@@ -1,11 +0,0 @@
|
|
|
1
|
-
import { ChatMessageError } from '../types';
|
|
2
|
-
export interface FetchSSEOptions {
|
|
3
|
-
onErrorHandle?: (error: ChatMessageError) => void;
|
|
4
|
-
onMessageHandle?: (text: string) => void;
|
|
5
|
-
}
|
|
6
|
-
/**
|
|
7
|
-
* 使用流式方法获取数据
|
|
8
|
-
* @param fetchFn
|
|
9
|
-
* @param options
|
|
10
|
-
*/
|
|
11
|
-
export declare const fetchSSE: (fetchFunction: () => Promise<Response>, options?: FetchSSEOptions) => Promise<Response | undefined>;
|
package/es/Chat/utils/fetch.js
DELETED
|
@@ -1,100 +0,0 @@
|
|
|
1
|
-
import _asyncToGenerator from "@babel/runtime/helpers/esm/asyncToGenerator";
|
|
2
|
-
import _regeneratorRuntime from "@babel/runtime/regenerator";
|
|
3
|
-
var codeMessage = {
|
|
4
|
-
200: '成功获取数据,服务已响应',
|
|
5
|
-
201: '操作成功,数据已保存',
|
|
6
|
-
202: '您的请求已进入后台排队,请耐心等待异步任务完成',
|
|
7
|
-
204: '数据已成功删除',
|
|
8
|
-
400: '很抱歉,您的请求出错,服务器未执行任何数据的创建或修改操作',
|
|
9
|
-
401: '很抱歉,您的权限不足。请确认用户名或密码是否正确',
|
|
10
|
-
403: '很抱歉,您无权访问此内容',
|
|
11
|
-
404: '很抱歉,您请求的记录不存在,服务器未能执行任何操作',
|
|
12
|
-
406: '很抱歉,服务器不支持该请求格式',
|
|
13
|
-
410: '很抱歉,你所请求的资源已永久删除',
|
|
14
|
-
422: '很抱歉,在创建对象时遇到验证错误,请稍后再试',
|
|
15
|
-
500: '很抱歉,服务器出现了问题,请稍后再试',
|
|
16
|
-
502: '很抱歉,您遇到了网关错误。这可能是由于网络故障或服务器问题导致的。请稍后再试,或联系管理员以获取更多帮助',
|
|
17
|
-
503: '很抱歉,我们的服务器过载或处在维护中,服务暂时不可用',
|
|
18
|
-
504: '很抱歉,网关请求超时,请稍后再试'
|
|
19
|
-
};
|
|
20
|
-
/**
|
|
21
|
-
* 使用流式方法获取数据
|
|
22
|
-
* @param fetchFn
|
|
23
|
-
* @param options
|
|
24
|
-
*/
|
|
25
|
-
export var fetchSSE = /*#__PURE__*/function () {
|
|
26
|
-
var _ref = _asyncToGenerator( /*#__PURE__*/_regeneratorRuntime.mark(function _callee(fetchFunction) {
|
|
27
|
-
var options,
|
|
28
|
-
response,
|
|
29
|
-
_options$onErrorHandl,
|
|
30
|
-
chatMessageError,
|
|
31
|
-
returnRes,
|
|
32
|
-
data,
|
|
33
|
-
reader,
|
|
34
|
-
decoder,
|
|
35
|
-
done,
|
|
36
|
-
_options$onMessageHan,
|
|
37
|
-
_yield$reader$read,
|
|
38
|
-
value,
|
|
39
|
-
doneReading,
|
|
40
|
-
chunkValue,
|
|
41
|
-
_args = arguments;
|
|
42
|
-
return _regeneratorRuntime.wrap(function _callee$(_context) {
|
|
43
|
-
while (1) switch (_context.prev = _context.next) {
|
|
44
|
-
case 0:
|
|
45
|
-
options = _args.length > 1 && _args[1] !== undefined ? _args[1] : {};
|
|
46
|
-
_context.next = 3;
|
|
47
|
-
return fetchFunction();
|
|
48
|
-
case 3:
|
|
49
|
-
response = _context.sent;
|
|
50
|
-
if (response.ok) {
|
|
51
|
-
_context.next = 8;
|
|
52
|
-
break;
|
|
53
|
-
}
|
|
54
|
-
chatMessageError = {
|
|
55
|
-
message: codeMessage[response.status],
|
|
56
|
-
status: response.status,
|
|
57
|
-
type: 'chatbot'
|
|
58
|
-
};
|
|
59
|
-
(_options$onErrorHandl = options.onErrorHandle) === null || _options$onErrorHandl === void 0 ? void 0 : _options$onErrorHandl.call(options, chatMessageError);
|
|
60
|
-
return _context.abrupt("return");
|
|
61
|
-
case 8:
|
|
62
|
-
returnRes = response.clone();
|
|
63
|
-
data = response.body;
|
|
64
|
-
if (data) {
|
|
65
|
-
_context.next = 12;
|
|
66
|
-
break;
|
|
67
|
-
}
|
|
68
|
-
return _context.abrupt("return");
|
|
69
|
-
case 12:
|
|
70
|
-
reader = data.getReader();
|
|
71
|
-
decoder = new TextDecoder();
|
|
72
|
-
done = false;
|
|
73
|
-
case 15:
|
|
74
|
-
if (done) {
|
|
75
|
-
_context.next = 26;
|
|
76
|
-
break;
|
|
77
|
-
}
|
|
78
|
-
_context.next = 18;
|
|
79
|
-
return reader.read();
|
|
80
|
-
case 18:
|
|
81
|
-
_yield$reader$read = _context.sent;
|
|
82
|
-
value = _yield$reader$read.value;
|
|
83
|
-
doneReading = _yield$reader$read.done;
|
|
84
|
-
done = doneReading;
|
|
85
|
-
chunkValue = decoder.decode(value);
|
|
86
|
-
(_options$onMessageHan = options.onMessageHandle) === null || _options$onMessageHan === void 0 ? void 0 : _options$onMessageHan.call(options, chunkValue);
|
|
87
|
-
_context.next = 15;
|
|
88
|
-
break;
|
|
89
|
-
case 26:
|
|
90
|
-
return _context.abrupt("return", returnRes);
|
|
91
|
-
case 27:
|
|
92
|
-
case "end":
|
|
93
|
-
return _context.stop();
|
|
94
|
-
}
|
|
95
|
-
}, _callee);
|
|
96
|
-
}));
|
|
97
|
-
return function fetchSSE(_x) {
|
|
98
|
-
return _ref.apply(this, arguments);
|
|
99
|
-
};
|
|
100
|
-
}();
|