@antv/dumi-theme-antv 0.8.0-beta.2 → 0.8.0-beta.4

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.
Files changed (70) hide show
  1. package/dist/builtins/Playground/index.module.less +0 -1
  2. package/dist/components/AI/HomeDialog/AntVBanner/index.module.less +2 -2
  3. package/dist/components/AI/HomeDialog/ModeSelector/index.module.less +39 -22
  4. package/dist/components/AI/HomeDialog/PromptTextarea/ChooseLib/index.module.less +0 -1
  5. package/dist/components/AI/HomeDialog/PromptTextarea/DatasourceCard/index.module.less +3 -3
  6. package/dist/components/AI/HomeDialog/PromptTextarea/index.js +10 -23
  7. package/dist/components/AI/HomeDialog/PromptTextarea/index.module.less +8 -8
  8. package/dist/components/AI/HomeDialog/RecommendCase/card.module.less +1 -1
  9. package/dist/components/AI/HomeDialog/RecommendCase/index.module.less +2 -2
  10. package/dist/components/Login/Captcha/index.js +199 -0
  11. package/dist/components/Login/Captcha/index.less +91 -0
  12. package/dist/components/Login/CheckCode/index.js +262 -0
  13. package/dist/components/Login/CheckCode/index.less +137 -0
  14. package/dist/components/Login/CountDownButton/index.js +115 -0
  15. package/dist/components/Login/CountDownButton/index.less +8 -0
  16. package/dist/components/Login/LoginForm.js +245 -0
  17. package/dist/components/Login/LoginForm.less +409 -0
  18. package/dist/components/Login/index.js +20 -0
  19. package/dist/components/Login/openAuthWindow.js +54 -0
  20. package/dist/components/Login/types.js +5 -0
  21. package/dist/components/Login/utils.js +47 -0
  22. package/dist/hooks/useProducts.js +3 -2
  23. package/dist/layouts/DocLayout.js +2 -1
  24. package/dist/layouts/GlobalLayout/index.js +8 -4
  25. package/dist/locales/en.json +52 -1
  26. package/dist/locales/zh.json +52 -1
  27. package/dist/model/AIChat.js +2 -2
  28. package/dist/model/auth.js +89 -0
  29. package/dist/pages/AIPlayground/components/ConversationsMenu/index.module.less +2 -0
  30. package/dist/pages/AIPlayground/components/MarkdownComponent/MarkdownCodeBlock.js +7 -5
  31. package/dist/pages/AIPlayground/components/MarkdownComponent/MarkdownCodeBlock.module.less +1 -2
  32. package/dist/pages/AIPlayground/components/MsgBox/index.js +179 -140
  33. package/dist/pages/AIPlayground/components/MsgBox/index.module.less +3 -2
  34. package/dist/pages/AIPlayground/components/MsgBox/useAutoScroll.js +46 -0
  35. package/dist/pages/AIPlayground/components/SessionLayout/index.module.less +1 -2
  36. package/dist/pages/AIPlayground/components/TaskBox/generateCode.js +7 -1
  37. package/dist/pages/AIPlayground/components/TaskBox/index.js +78 -55
  38. package/dist/pages/AIPlayground/components/TaskBox/index.module.less +1 -0
  39. package/dist/pages/Examples/components/Accouncement/index.module.less +1 -1
  40. package/dist/pages/Examples/index.module.less +11 -11
  41. package/dist/pages/Index/components/Cases/index.module.less +8 -8
  42. package/dist/pages/Index/components/Companies/index.module.less +4 -3
  43. package/dist/pages/Index/components/Features/FeatureCard.module.less +4 -5
  44. package/dist/pages/Index/components/Features/index.module.less +5 -5
  45. package/dist/pages/Index/components/_.less +9 -9
  46. package/dist/slots/Banner/Notification.module.less +8 -8
  47. package/dist/slots/Banner/index.module.less +10 -9
  48. package/dist/slots/CodeEditor/Toolbar.js +5 -3
  49. package/dist/slots/CodeEditor/index.js +10 -7
  50. package/dist/slots/CodeEditor/index.module.less +3 -0
  51. package/dist/slots/CodePreview/index.module.less +0 -3
  52. package/dist/slots/CodeRunner/index.js +5 -2
  53. package/dist/slots/ContentTable/index.module.less +2 -1
  54. package/dist/slots/Detail/News.module.less +9 -9
  55. package/dist/slots/Detail/index.module.less +11 -14
  56. package/dist/slots/ExampleSider/index.module.less +3 -4
  57. package/dist/slots/Footer/index.module.less +2 -2
  58. package/dist/slots/Header/Products/Product.module.less +2 -2
  59. package/dist/slots/Header/Search/SearchResult.js +7 -0
  60. package/dist/slots/Header/Search/SearchResult.module.less +1 -0
  61. package/dist/slots/Header/index.module.less +5 -5
  62. package/dist/slots/LiveExample/index.module.less +1 -1
  63. package/dist/slots/Loading/index.module.less +30 -28
  64. package/dist/slots/ManualContent/index.module.less +14 -17
  65. package/dist/slots/_.less +9 -9
  66. package/dist/utils/auth.js +10 -0
  67. package/dist/utils/env.js +24 -0
  68. package/dist/utils/request.js +104 -0
  69. package/package.json +7 -2
  70. package/dist/static/SearchAiIcon.svg +0 -14
@@ -1,27 +1,25 @@
1
- function _toConsumableArray(arr) { return _arrayWithoutHoles(arr) || _iterableToArray(arr) || _unsupportedIterableToArray(arr) || _nonIterableSpread(); }
2
- function _nonIterableSpread() { throw new TypeError("Invalid attempt to spread non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
- function _iterableToArray(iter) { if (typeof Symbol !== "undefined" && iter[Symbol.iterator] != null || iter["@@iterator"] != null) return Array.from(iter); }
4
- function _arrayWithoutHoles(arr) { if (Array.isArray(arr)) return _arrayLikeToArray(arr); }
5
1
  function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
6
2
  function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
7
3
  function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
8
4
  function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
9
5
  function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
10
6
  function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
11
- import { CheckOutlined, CopyOutlined, DislikeOutlined, LikeOutlined, PlusSquareOutlined, SyncOutlined } from '@ant-design/icons';
7
+ import { CheckOutlined, CopyOutlined, PlusSquareOutlined, SyncOutlined } from '@ant-design/icons';
12
8
  import { Bubble } from '@ant-design/x';
13
9
  import { Button, Flex, Space, Tooltip } from 'antd';
14
- import { history, useSiteData, useIntl } from 'dumi';
15
- import { findLast } from 'lodash-es';
16
- import React, { useEffect, useState } from 'react';
10
+ import { useChat } from '@ai-sdk/react';
11
+ import { TextStreamChatTransport } from 'ai';
12
+ import { history, useIntl, useSiteData } from 'dumi';
13
+ import React, { useEffect, useState, useMemo, useRef } from 'react';
17
14
  import { useCopyToClipboard } from 'react-use';
18
15
  import { useSnapshot } from 'valtio';
19
16
  import { PromptTextarea } from "../../../../components/AI/HomeDialog/PromptTextarea";
20
- import { useStreamingText } from "../../../../hooks/useStreamingText";
21
17
  import { AIChatStore, clearEmptySession, createPureNewSession, derivedState } from "../../../../model/AIChat";
22
18
  import { getCodeFromMarkdown, isPreviewable } from "../../../../utils/code";
23
19
  import { MarkdownComponent } from "../MarkdownComponent";
24
20
  import styles from "./index.module.less";
21
+ import { useAutoScroll } from "./useAutoScroll";
22
+ import { getBaseURL } from "../../../../utils/env";
25
23
  var avatar = {
26
24
  icon: /*#__PURE__*/React.createElement("img", {
27
25
  draggable: false,
@@ -35,16 +33,38 @@ var avatar = {
35
33
  };
36
34
  var chatScrollIntoView = function chatScrollIntoView() {
37
35
  setTimeout(function () {
38
- var nodeList = document.querySelectorAll('.ant-bubble');
39
- nodeList[nodeList.length - 1].scrollIntoView({
40
- behavior: 'smooth'
36
+ document.getElementById('msgBoxAnchor').scrollIntoView({
37
+ block: 'end'
41
38
  });
39
+ }, 100);
40
+ };
41
+
42
+ // 辅助函数:将旧的 Message 格式转换为新的 UIMessage 格式
43
+ var convertToUIMessages = function convertToUIMessages(messages) {
44
+ return messages.map(function (msg) {
45
+ return {
46
+ id: msg.id,
47
+ role: msg.role,
48
+ parts: [{
49
+ type: 'text',
50
+ text: msg.content
51
+ }]
52
+ };
42
53
  });
43
54
  };
55
+
56
+ // 辅助函数:从 UIMessage 提取文本内容
57
+ var getTextContent = function getTextContent(message) {
58
+ return message.parts.filter(function (part) {
59
+ return part.type === 'text';
60
+ }).map(function (part) {
61
+ return part.text;
62
+ }).join('');
63
+ };
44
64
  function MsgBox(props) {
45
- var _derivedSnap$activeSe, _derivedSnap$activeSe2, _derivedSnap$activeSe3;
65
+ var _derivedSnap$activeSe, _derivedSnap$activeSe3, _derivedSnap$activeSe5, _derivedSnap$activeSe6, _messages;
46
66
  var _props$messages = props.messages,
47
- messages = _props$messages === void 0 ? [] : _props$messages,
67
+ initialMessages = _props$messages === void 0 ? [] : _props$messages,
48
68
  _props$simple = props.simple,
49
69
  simple = _props$simple === void 0 ? false : _props$simple,
50
70
  _props$context = props.context,
@@ -63,98 +83,113 @@ function MsgBox(props) {
63
83
  _useState4 = _slicedToArray(_useState3, 2),
64
84
  promptText = _useState4[0],
65
85
  setPromptText = _useState4[1];
66
- var _useState5 = useState(false),
86
+ var _useState5 = useState(''),
67
87
  _useState6 = _slicedToArray(_useState5, 2),
68
- isStreaming = _useState6[0],
69
- setIsStreaming = _useState6[1]; // trigger
70
- var _useState7 = useState(false),
71
- _useState8 = _slicedToArray(_useState7, 2),
72
- loading = _useState8[0],
73
- setLoading = _useState8[1];
88
+ fileSummary = _useState6[0],
89
+ setFileSummary = _useState6[1];
74
90
  var snap = useSnapshot(AIChatStore);
75
91
  var derivedSnap = useSnapshot(derivedState);
76
92
  var _useCopyToClipboard = useCopyToClipboard(),
77
93
  _useCopyToClipboard2 = _slicedToArray(_useCopyToClipboard, 2),
78
94
  copyState = _useCopyToClipboard2[0],
79
95
  copyToClipboard = _useCopyToClipboard2[1];
80
- var latestUserMessage = findLast((_derivedSnap$activeSe = derivedSnap.activeSession) === null || _derivedSnap$activeSe === void 0 ? void 0 : _derivedSnap$activeSe.messages, function (msg) {
81
- return msg.role === 'user';
82
- });
83
- var _useState9 = useState(''),
84
- _useState10 = _slicedToArray(_useState9, 2),
85
- fileSummary = _useState10[0],
86
- setFileSummary = _useState10[1];
87
- var streamingText = useStreamingText({
88
- url: 'https://webgw-pre.alipay.com/visqaservice/external/chat',
89
- method: 'POST',
90
- body: {
91
- gptConversationId: (_derivedSnap$activeSe2 = derivedSnap.activeSession) === null || _derivedSnap$activeSe2 === void 0 ? void 0 : _derivedSnap$activeSe2.id,
92
- query: latestUserMessage === null || latestUserMessage === void 0 ? void 0 : latestUserMessage.content,
93
- library: (latestUserMessage === null || latestUserMessage === void 0 ? void 0 : latestUserMessage.lib) || lib,
94
- mode: latestUserMessage === null || latestUserMessage === void 0 ? void 0 : latestUserMessage.mode,
95
- anonymousUserId: snap.anonymousUserId,
96
- context: (latestUserMessage === null || latestUserMessage === void 0 ? void 0 : latestUserMessage.context) || context,
97
- mountId: "container"
98
- },
99
- trigger: isStreaming,
100
- // 将 isStreaming 状态作为 trigger
101
- headers: {
102
- 'Content-Type': 'application/json',
103
- 'x-webgw-version': '2.0',
104
- 'x-webgw-appid': '180020010001210065'
105
- },
106
- beforeStart: function beforeStart() {
107
- setLoading(true);
108
- },
109
- onFinish: function onFinish(finalText) {
110
- try {
111
- var _derivedState$activeS;
112
- var finalJSON = JSON.parse(finalText);
113
- (_derivedState$activeS = derivedState.activeSession) === null || _derivedState$activeS === void 0 || (_derivedState$activeS = _derivedState$activeS.messages) === null || _derivedState$activeS === void 0 || _derivedState$activeS.push({
96
+
97
+ // 使用 ref 存储动态值,避免重新创建 transport
98
+ var anonymousUserIdRef = useRef(snap.anonymousUserId);
99
+ var contextRef = useRef(props.context);
100
+ var activeSessionIdRef = useRef((_derivedSnap$activeSe = derivedSnap.activeSession) === null || _derivedSnap$activeSe === void 0 ? void 0 : _derivedSnap$activeSe.id);
101
+ useEffect(function () {
102
+ var _derivedSnap$activeSe2;
103
+ anonymousUserIdRef.current = snap.anonymousUserId;
104
+ activeSessionIdRef.current = (_derivedSnap$activeSe2 = derivedSnap.activeSession) === null || _derivedSnap$activeSe2 === void 0 ? void 0 : _derivedSnap$activeSe2.id;
105
+ }, [snap.anonymousUserId, (_derivedSnap$activeSe3 = derivedSnap.activeSession) === null || _derivedSnap$activeSe3 === void 0 ? void 0 : _derivedSnap$activeSe3.id]);
106
+
107
+ // 转换初始消息为新格式
108
+ var convertedInitialMessages = useMemo(function () {
109
+ return convertToUIMessages(initialMessages);
110
+ }, [initialMessages]);
111
+
112
+ // 核心:使用 useChat hook,配置 DefaultChatTransport
113
+ var _useChat = useChat({
114
+ transport: new TextStreamChatTransport({
115
+ api: getBaseURL() + '/api/modules/antv/ai/chat',
116
+ credentials: 'include',
117
+ headers: {
118
+ // xxx
119
+ },
120
+ // body 可以是函数,用于获取最新的动态值
121
+ body: function body() {
122
+ return {
123
+ gptConversationId: activeSessionIdRef.current,
124
+ anonymousUserId: anonymousUserIdRef.current,
125
+ mountId: 'container',
126
+ antvContext: props.context,
127
+ library: lib
128
+ };
129
+ }
130
+ }),
131
+ messages: convertedInitialMessages,
132
+ // 当AI响应结束时触发
133
+ onFinish: function onFinish(_ref) {
134
+ var message = _ref.message,
135
+ isAbort = _ref.isAbort,
136
+ isDisconnect = _ref.isDisconnect,
137
+ isError = _ref.isError;
138
+ if (!isAbort && !isDisconnect && !isError) {
139
+ var _derivedState$activeS;
140
+ var messageContent = getTextContent(message);
141
+ if (isPreviewable(messageContent)) {
142
+ var codeBlock = getCodeFromMarkdown(messageContent).code;
143
+ AIChatStore.codeBlock = codeBlock;
144
+ onCodegen === null || onCodegen === void 0 || onCodegen(codeBlock);
145
+ }
146
+ (_derivedState$activeS = derivedState.activeSession) === null || _derivedState$activeS === void 0 || (_derivedState$activeS = _derivedState$activeS.messages) === null || _derivedState$activeS === void 0 || _derivedState$activeS.push({
147
+ id: crypto.randomUUID(),
148
+ role: 'assistant',
149
+ content: messageContent,
150
+ createdAt: Date.now()
151
+ });
152
+ }
153
+ },
154
+ onError: function onError(error) {
155
+ var _derivedState$activeS2;
156
+ console.error('回答失败', error);
157
+ // 同步错误消息到 valtio
158
+ (_derivedState$activeS2 = derivedState.activeSession) === null || _derivedState$activeS2 === void 0 || (_derivedState$activeS2 = _derivedState$activeS2.messages) === null || _derivedState$activeS2 === void 0 || _derivedState$activeS2.push({
114
159
  id: crypto.randomUUID(),
115
160
  role: 'assistant',
116
- content: finalJSON.content,
161
+ content: formatMessage({
162
+ id: 'ai.msgbox.error.response'
163
+ }),
117
164
  createdAt: Date.now()
118
165
  // mode,
119
166
  // lib,
120
167
  });
121
- if (isPreviewable(finalJSON.content)) {
122
- var codeBlock = getCodeFromMarkdown(finalJSON.content).code;
123
- AIChatStore.codeBlock = codeBlock;
124
- onCodegen === null || onCodegen === void 0 || onCodegen(codeBlock);
125
- }
126
- } catch (e) {
127
- // 说明不是JSON格式
128
- } finally {
129
- // 当流结束时,更新最后一条消息并重置 trigger
130
- setIsStreaming(false); // **关键:流结束后,关闭 trigger**
131
- setLoading(false);
132
- chatScrollIntoView();
133
168
  }
134
- },
135
- onError: function onError(error) {
136
- var _derivedState$activeS2;
137
- console.log('回答失败', error);
138
- // 处理错误
139
- (_derivedState$activeS2 = derivedState.activeSession) === null || _derivedState$activeS2 === void 0 || (_derivedState$activeS2 = _derivedState$activeS2.messages) === null || _derivedState$activeS2 === void 0 || _derivedState$activeS2.push({
140
- id: crypto.randomUUID(),
141
- role: 'assistant',
142
- content: formatMessage({
143
- id: 'ai.msgbox.error.response'
144
- }),
145
- createdAt: Date.now()
146
- // mode,
147
- // lib,
148
- });
149
- setIsStreaming(false);
150
- setLoading(false);
151
- }
152
- });
169
+ }),
170
+ messages = _useChat.messages,
171
+ setMessages = _useChat.setMessages,
172
+ sendMessage = _useChat.sendMessage,
173
+ regenerate = _useChat.regenerate,
174
+ status = _useChat.status,
175
+ stop = _useChat.stop;
153
176
 
154
- // 3. 处理用户提交
177
+ // 处理用户提交
155
178
  var handleSubmit = function handleSubmit() {
156
179
  var _derivedState$activeS3;
157
- if (!promptText.trim() || isStreaming) return; // 如果正在流式输出,则不允许发送
180
+ if (!promptText.trim() || status === 'streaming' || status === 'submitted') return;
181
+
182
+ // 使用 sendMessage 函数发送新消息
183
+ // 第二个参数传递每次请求特定的额外数据
184
+ sendMessage({
185
+ text: promptText
186
+ }, {
187
+ body: {
188
+ context: fileSummary,
189
+ lib: lib,
190
+ mode: 'implement'
191
+ }
192
+ });
158
193
  setPromptText('');
159
194
  (_derivedState$activeS3 = derivedState.activeSession) === null || _derivedState$activeS3 === void 0 || (_derivedState$activeS3 = _derivedState$activeS3.messages) === null || _derivedState$activeS3 === void 0 || _derivedState$activeS3.push({
160
195
  id: crypto.randomUUID(),
@@ -164,21 +199,40 @@ function MsgBox(props) {
164
199
  context: fileSummary,
165
200
  lib: lib
166
201
  });
167
- // **关键:开启 trigger,开始请求**
168
- setIsStreaming(true);
169
202
  chatScrollIntoView();
170
203
  };
204
+
205
+ // 同步 Valtio store -> useChat state
171
206
  useEffect(function () {
172
- // 检查是否存在临时消息
173
- if (snap.tempMessage) {
174
- var activeSession = derivedState.activeSession;
175
- if (activeSession) {
176
- // 1. 将临时消息正式添加到当前会话中
177
- activeSession.messages.push(snap.tempMessage);
178
- setIsStreaming(true);
179
- AIChatStore.tempMessage = null;
207
+ if (simple) {
208
+ return;
209
+ }
210
+ setTimeout(function () {
211
+ var _derivedSnap$activeSe4;
212
+ var sessionMessages = (_derivedSnap$activeSe4 = derivedSnap.activeSession) === null || _derivedSnap$activeSe4 === void 0 ? void 0 : _derivedSnap$activeSe4.messages;
213
+ if ((sessionMessages === null || sessionMessages === void 0 ? void 0 : sessionMessages.length) > 0) {
214
+ var converted = convertToUIMessages(sessionMessages);
215
+ // 避免无限循环,仅当消息数量或内容不同时更新
216
+ if (messages.length !== converted.length || JSON.stringify(messages) !== JSON.stringify(converted)) {
217
+ setMessages(converted);
218
+ }
180
219
  }
220
+ });
221
+ }, [(_derivedSnap$activeSe5 = derivedSnap.activeSession) === null || _derivedSnap$activeSe5 === void 0 ? void 0 : _derivedSnap$activeSe5.id, (_derivedSnap$activeSe6 = derivedSnap.activeSession) === null || _derivedSnap$activeSe6 === void 0 || (_derivedSnap$activeSe6 = _derivedSnap$activeSe6.messages) === null || _derivedSnap$activeSe6 === void 0 ? void 0 : _derivedSnap$activeSe6.length]);
222
+
223
+ // 处理从外部(如demo页)发起的对话
224
+ useEffect(function () {
225
+ if (snap.tempMessage) {
226
+ var _derivedState$activeS4, _derivedState$activeS5;
227
+ sendMessage({
228
+ text: snap.tempMessage.content
229
+ });
230
+ (_derivedState$activeS4 = derivedState.activeSession) === null || _derivedState$activeS4 === void 0 || (_derivedState$activeS4 = _derivedState$activeS4.messages) === null || _derivedState$activeS4 === void 0 || (_derivedState$activeS5 = _derivedState$activeS4.push) === null || _derivedState$activeS5 === void 0 || _derivedState$activeS5.call(_derivedState$activeS4, snap.tempMessage);
231
+ AIChatStore.tempMessage = null;
181
232
  }
233
+ }, [snap.tempMessage]);
234
+ useEffect(function () {
235
+ chatScrollIntoView();
182
236
  if (simple) {
183
237
  createPureNewSession(title);
184
238
  }
@@ -187,51 +241,36 @@ function MsgBox(props) {
187
241
  };
188
242
  }, []);
189
243
  useEffect(function () {
190
- if (derivedState.activeSession && Date.now() - derivedState.activeSession.createdAt > 5000) {
191
- setLoading(false);
192
- setIsStreaming(false);
193
- }
244
+ chatScrollIntoView();
194
245
  }, [snap.activeSessionId]);
195
- var showMessages = [].concat(_toConsumableArray(messages), _toConsumableArray(((_derivedSnap$activeSe3 = derivedSnap.activeSession) === null || _derivedSnap$activeSe3 === void 0 ? void 0 : _derivedSnap$activeSe3.messages) || []));
246
+
247
+ // 将 messages 数组作为依赖项。当它变化时,Hook 会运行。
248
+ var _useAutoScroll = useAutoScroll(messages),
249
+ containerRef = _useAutoScroll.containerRef,
250
+ anchorRef = _useAutoScroll.anchorRef,
251
+ showScrollDownButton = _useAutoScroll.showScrollDownButton;
196
252
  return /*#__PURE__*/React.createElement(React.Fragment, null, /*#__PURE__*/React.createElement(Flex, {
197
253
  gap: "middle",
198
254
  vertical: true,
199
- className: styles.chatContainer
200
- }, showMessages === null || showMessages === void 0 ? void 0 : showMessages.map(function (msg, index) {
255
+ className: styles.chatContainer,
256
+ ref: containerRef
257
+ }, messages.map(function (msg, index) {
258
+ var textContent = getTextContent(msg);
201
259
  return /*#__PURE__*/React.createElement(Bubble, {
202
- key: index,
260
+ key: msg.id || index,
203
261
  content: /*#__PURE__*/React.createElement(MarkdownComponent, {
204
- content: msg.content
262
+ content: textContent
205
263
  }),
206
264
  avatar: msg.role === 'assistant' ? avatar : null,
207
- footer: msg.role === 'assistant' && index > 0 ? /*#__PURE__*/React.createElement(Space, {
265
+ footer: msg.role === 'assistant' && index > 0 && index === messages.length - 1 && status === 'ready' ? /*#__PURE__*/React.createElement(Space, {
208
266
  size: "small"
209
267
  }, /*#__PURE__*/React.createElement(Tooltip, {
210
- title: formatMessage({
211
- id: 'ai.msgbox.like'
212
- })
213
- }, /*#__PURE__*/React.createElement(Button, {
214
- color: "default",
215
- variant: "text",
216
- size: "small",
217
- icon: /*#__PURE__*/React.createElement(LikeOutlined, null)
218
- })), /*#__PURE__*/React.createElement(Tooltip, {
219
- title: formatMessage({
220
- id: 'ai.msgbox.dislike'
221
- })
222
- }, /*#__PURE__*/React.createElement(Button, {
223
- color: "default",
224
- variant: "text",
225
- size: "small",
226
- icon: /*#__PURE__*/React.createElement(DislikeOutlined, null)
227
- })), index === showMessages.length - 1 && /*#__PURE__*/React.createElement(Tooltip, {
228
268
  title: formatMessage({
229
269
  id: 'ai.msgbox.retry'
230
270
  })
231
271
  }, /*#__PURE__*/React.createElement(Button, {
232
272
  onClick: function onClick() {
233
- derivedState.activeSession.messages.pop();
234
- setIsStreaming(true);
273
+ return regenerate();
235
274
  },
236
275
  color: "default",
237
276
  variant: "text",
@@ -246,19 +285,22 @@ function MsgBox(props) {
246
285
  variant: "text",
247
286
  size: "small",
248
287
  onClick: function onClick() {
249
- return copyToClipboard(msg.content);
288
+ return copyToClipboard(textContent);
250
289
  },
251
- icon: copyState.value === msg.content ? /*#__PURE__*/React.createElement(CheckOutlined, null) : /*#__PURE__*/React.createElement(CopyOutlined, null)
290
+ icon: copyState.value === textContent ? /*#__PURE__*/React.createElement(CheckOutlined, null) : /*#__PURE__*/React.createElement(CopyOutlined, null)
252
291
  }))) : null,
253
292
  placement: msg.role === 'user' ? 'end' : 'start'
254
293
  });
255
- }), loading && /*#__PURE__*/React.createElement(Bubble, {
294
+ }), (status === 'streaming' || status === 'submitted') && ((_messages = messages[messages.length - 1]) === null || _messages === void 0 ? void 0 : _messages.role) === 'user' && /*#__PURE__*/React.createElement(Bubble, {
256
295
  placement: "start",
257
296
  avatar: avatar,
258
- loading: loading
297
+ loading: true
298
+ }), /*#__PURE__*/React.createElement("div", {
299
+ ref: anchorRef,
300
+ id: "msgBoxAnchor"
259
301
  })), /*#__PURE__*/React.createElement("div", null, !props.simple && /*#__PURE__*/React.createElement("div", {
260
302
  className: styles.newButtonContainer
261
- }, /*#__PURE__*/React.createElement("button", {
303
+ }, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement("button", {
262
304
  type: "button",
263
305
  onClick: function onClick() {
264
306
  return history.push('/');
@@ -266,16 +308,13 @@ function MsgBox(props) {
266
308
  className: styles.newButton
267
309
  }, /*#__PURE__*/React.createElement(Space, null, /*#__PURE__*/React.createElement(PlusSquareOutlined, null), formatMessage({
268
310
  id: 'ai.msgbox.start.new.chat'
269
- })))), /*#__PURE__*/React.createElement(PromptTextarea, {
311
+ }))))), /*#__PURE__*/React.createElement(PromptTextarea, {
270
312
  size: "compact",
271
313
  mode: "implement",
272
314
  value: promptText,
273
315
  onChange: setPromptText,
274
- loading: loading,
275
- onCancel: function onCancel() {
276
- setIsStreaming(false);
277
- setLoading(false);
278
- },
316
+ loading: status === 'streaming' || status === 'submitted',
317
+ onCancel: stop,
279
318
  showAction: !props.simple,
280
319
  style: {
281
320
  marginBottom: 0
@@ -5,7 +5,7 @@
5
5
 
6
6
  .query {
7
7
  background: #c9dfff;
8
- box-shadow: 0 4px 10px rgba(51, 113, 242, .1);
8
+ box-shadow: 0 4px 10px rgba(51, 113, 242, 10%);
9
9
  border-radius: 8px;
10
10
  padding: 8px 16px;
11
11
  max-width: calc(100% - 100px);
@@ -26,7 +26,7 @@
26
26
  padding: 0 8px;
27
27
  color: var(--primary-color);
28
28
  font-size: 14px;
29
- background: rgba(135, 59, 244, .145);
29
+ background: rgba(135, 59, 244, 14.5%);
30
30
  border-radius: 6px;
31
31
  line-height: 22px;
32
32
  margin: 12px auto;
@@ -34,6 +34,7 @@
34
34
 
35
35
  .newButtonContainer {
36
36
  display: flex;
37
+ justify-content: center;
37
38
  }
38
39
 
39
40
  .chatContainer {
@@ -0,0 +1,46 @@
1
+ function _slicedToArray(arr, i) { return _arrayWithHoles(arr) || _iterableToArrayLimit(arr, i) || _unsupportedIterableToArray(arr, i) || _nonIterableRest(); }
2
+ function _nonIterableRest() { throw new TypeError("Invalid attempt to destructure non-iterable instance.\nIn order to be iterable, non-array objects must have a [Symbol.iterator]() method."); }
3
+ function _unsupportedIterableToArray(o, minLen) { if (!o) return; if (typeof o === "string") return _arrayLikeToArray(o, minLen); var n = Object.prototype.toString.call(o).slice(8, -1); if (n === "Object" && o.constructor) n = o.constructor.name; if (n === "Map" || n === "Set") return Array.from(o); if (n === "Arguments" || /^(?:Ui|I)nt(?:8|16|32)(?:Clamped)?Array$/.test(n)) return _arrayLikeToArray(o, minLen); }
4
+ function _arrayLikeToArray(arr, len) { if (len == null || len > arr.length) len = arr.length; for (var i = 0, arr2 = new Array(len); i < len; i++) arr2[i] = arr[i]; return arr2; }
5
+ function _iterableToArrayLimit(r, l) { var t = null == r ? null : "undefined" != typeof Symbol && r[Symbol.iterator] || r["@@iterator"]; if (null != t) { var e, n, i, u, a = [], f = !0, o = !1; try { if (i = (t = t.call(r)).next, 0 === l) { if (Object(t) !== t) return; f = !1; } else for (; !(f = (e = i.call(t)).done) && (a.push(e.value), a.length !== l); f = !0); } catch (r) { o = !0, n = r; } finally { try { if (!f && null != t.return && (u = t.return(), Object(u) !== u)) return; } finally { if (o) throw n; } } return a; } }
6
+ function _arrayWithHoles(arr) { if (Array.isArray(arr)) return arr; }
7
+ import { useRef, useLayoutEffect, useState } from 'react';
8
+
9
+ // threshold: 容差值,表示离底部多远以内都算“在底部”
10
+ export var useAutoScroll = function useAutoScroll(dependencies) {
11
+ var threshold = arguments.length > 1 && arguments[1] !== undefined ? arguments[1] : 100;
12
+ // 指向滚动容器
13
+ var containerRef = useRef(null);
14
+ // 指向一个永远在列表末尾的空div,作为滚动的目标
15
+ var anchorRef = useRef(null);
16
+ var _useState = useState(false),
17
+ _useState2 = _slicedToArray(_useState, 2),
18
+ showScrollDownButton = _useState2[0],
19
+ setShowScrollDownButton = _useState2[1];
20
+ useLayoutEffect(function () {
21
+ var container = containerRef.current;
22
+ if (!container) return;
23
+
24
+ // scrollTop: 滚动条距离顶部的距离
25
+ // scrollHeight: 整个可滚动内容的总高度
26
+ // clientHeight: 容器可视区域的高度
27
+ var isAtBottom = container.scrollHeight - container.scrollTop <= container.clientHeight + threshold + 200;
28
+
29
+ // 只有当用户已经在底部时,才执行自动滚动
30
+ if (isAtBottom) {
31
+ var _anchorRef$current;
32
+ (_anchorRef$current = anchorRef.current) === null || _anchorRef$current === void 0 || _anchorRef$current.scrollIntoView({
33
+ block: 'end' // 滚动到元素的末尾
34
+ });
35
+ setShowScrollDownButton(false);
36
+ } else {
37
+ setShowScrollDownButton(true);
38
+ }
39
+ }, [dependencies, threshold]); // 依赖项改变时(比如新消息来了),触发此 effect
40
+
41
+ return {
42
+ containerRef: containerRef,
43
+ anchorRef: anchorRef,
44
+ showScrollDownButton: showScrollDownButton
45
+ };
46
+ };
@@ -31,7 +31,6 @@
31
31
  .taskBox {
32
32
  height: calc(100% - 32px);
33
33
  margin: 16px;
34
- padding: 16px;
35
34
  flex-grow: 1;
36
- background: #ffffff;
35
+ background: #fff;
37
36
  }
@@ -77,14 +77,20 @@ export function generateDependencies(codeString) {
77
77
  }
78
78
  export function wrap2VisionSnap() {
79
79
  var codeBlock = arguments.length > 0 && arguments[0] !== undefined ? arguments[0] : '';
80
+ var dependencies = generateDependencies(codeBlock);
81
+ var rootElementType = dependencies['@antv/f2'] ? 'canvas' : 'div';
80
82
  return {
81
83
  modules: {
82
84
  '/package.json': {
83
85
  fpath: '/package.json',
84
- code: "{\n \"name\": \"AntV-master\",\n \"version\": \"1.0.0\",\n \"main\": \"/src/index.jsx\",\n \"dependencies\": ".concat(JSON.stringify(generateDependencies(codeBlock), null, 2), "\n}")
86
+ code: "{\n \"name\": \"AntV-adapted-project\",\n \"version\": \"1.0.0\",\n \"main\": \"/src/index.jsx\",\n \"dependencies\": ".concat(JSON.stringify(dependencies, null, 2), "\n}")
85
87
  },
86
88
  '/src/index.jsx': {
87
89
  fpath: '/src/index.jsx',
90
+ code: "\n// --- Adapter Script ---\n\n// 1. \u627E\u5230\u7F16\u8F91\u5668\u73AF\u5883\u63D0\u4F9B\u7684\u6839\u8282\u70B9 #root\nconst rootElement = document.getElementById('root');\n\nif (rootElement) {\n // 2. \u5728 #root \u5185\u90E8\u521B\u5EFA\u4E00\u4E2A ".concat(rootElementType, "\n const containerElement = document.createElement('").concat(rootElementType, "');\n\n // 3. \u5C06\u8FD9\u4E2A div \u7684 id \u8BBE\u7F6E\u4E3A 'container'\uFF0C\u4EE5\u6EE1\u8DB3\u7528\u6237\u4EE3\u7801\u7684\u9700\u6C42\n containerElement.id = 'container';\n\n // 4. \u5C06\u5B83\u6DFB\u52A0\u5230 #root \u4E2D\n rootElement.appendChild(containerElement);\n\n // 5. \u73B0\u5728 DOM \u4E2D\u5DF2\u7ECF\u5B58\u5728 #container\uFF0C\u5B89\u5168\u5730\u5BFC\u5165\u5E76\u6267\u884C\u7528\u6237\u7684\u4EE3\u7801\n import('./App.jsx');\n\n}\n ")
91
+ },
92
+ '/src/App.jsx': {
93
+ fpath: '/src/App.jsx',
88
94
  code: codeBlock
89
95
  }
90
96
  }