react-ai-renderer 0.1.4 → 0.1.6

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.
@@ -1,1398 +0,0 @@
1
- import * as React from 'react';
2
- import React__default, { useState, useEffect } from 'react';
3
- import * as runtime from 'react/jsx-runtime';
4
- import { jsx, jsxs } from 'react/jsx-runtime';
5
- import RemarkMath from 'remark-math';
6
- import RemarkBreaks from 'remark-breaks';
7
- import RemarkGfm from 'remark-gfm';
8
- import { run, compile } from '@mdx-js/mdx';
9
- import RehypeKatex from 'rehype-katex';
10
- import RehypeHighlight from 'rehype-highlight';
11
-
12
- var MDXStreamingParser = /** @class */function () {
13
- function MDXStreamingParser(components) {
14
- // 将组件名称存储在Set中,而不是整个组件对象
15
- this.registeredComponents = new Set(components.map(function (c) {
16
- return c.name;
17
- }));
18
- this.messages = new Map();
19
- }
20
- // 检查组件是否已注册
21
- MDXStreamingParser.prototype.isComponentRegistered = function (name) {
22
- return this.registeredComponents.has(name);
23
- };
24
- MDXStreamingParser.prototype.parse = function (messageId, input) {
25
- var state = {
26
- position: 0,
27
- stack: [],
28
- buffer: ''
29
- };
30
- var result = [];
31
- var i = state.position;
32
- while (i < input.length) {
33
- var char = input[i];
34
- if (char === '<') {
35
- // Handle closing tags
36
- if (input[i + 1] === '/') {
37
- var closeTagEnd = input.indexOf('>', i);
38
- if (closeTagEnd === -1) {
39
- state.buffer += input.slice(i);
40
- break;
41
- }
42
- var tagName = input.slice(i + 2, closeTagEnd);
43
- // 检查组件是否已注册
44
- var isRegistered = this.isComponentRegistered(tagName);
45
- // 如果组件未注册,则作为文本处理
46
- if (!isRegistered) {
47
- state.buffer += input.slice(i, closeTagEnd + 1);
48
- i = closeTagEnd + 1;
49
- continue;
50
- }
51
- if (/^[A-Z][A-Za-z0-9]*$/.test(tagName)) {
52
- // Handle text content in the buffer
53
- var trimmedContent = state.buffer.trim();
54
- if (trimmedContent && state.stack.length > 0) {
55
- var currentComponent = state.stack[state.stack.length - 1];
56
- if (!currentComponent.children) {
57
- currentComponent.children = [];
58
- }
59
- if (typeof currentComponent.children === 'string') {
60
- currentComponent.children = [{
61
- type: "text",
62
- value: currentComponent.children
63
- }];
64
- }
65
- currentComponent.children.push({
66
- type: "text",
67
- value: trimmedContent
68
- });
69
- }
70
- state.buffer = '';
71
- state.stack.pop();
72
- i = closeTagEnd + 1;
73
- continue;
74
- }
75
- }
76
- // Handle opening tags
77
- var tagMatch = input.slice(i).match(/^<([A-Z][A-Za-z0-9]*)/);
78
- if (tagMatch) {
79
- // Handle accumulated text before the tag
80
- var componentName = tagMatch[1];
81
- // 检查组件是否已注册
82
- var isRegistered = this.isComponentRegistered(componentName);
83
- // 如果组件未注册,将整个标签作为文本处理
84
- if (!isRegistered) {
85
- state.buffer += input[i];
86
- i++;
87
- continue;
88
- }
89
- var trimmedText = state.buffer.trim();
90
- if (trimmedText && state.stack.length === 0) {
91
- result.push({
92
- type: "text",
93
- value: trimmedText
94
- });
95
- } else if (trimmedText && state.stack.length > 0) {
96
- var currentComponent = state.stack[state.stack.length - 1];
97
- if (!currentComponent.children) {
98
- currentComponent.children = [];
99
- }
100
- if (typeof currentComponent.children === 'string') {
101
- currentComponent.children = [{
102
- type: "text",
103
- value: currentComponent.children
104
- }];
105
- }
106
- currentComponent.children.push({
107
- type: "text",
108
- value: trimmedText
109
- });
110
- }
111
- state.buffer = '';
112
- // 动态解析标签形式,不再依赖预先注册的 selfClosing 属性
113
- var _a = this.parseComponentTag(input.slice(i)),
114
- props = _a.props,
115
- endIndex = _a.endIndex,
116
- selfClosing = _a.selfClosing;
117
- var component = {
118
- type: "component",
119
- value: componentName,
120
- props: props,
121
- selfClosing: selfClosing // 使用实际解析出的标签形式
122
- };
123
- if (!selfClosing) {
124
- state.stack.push(component);
125
- if (state.stack.length === 1) {
126
- result.push(component);
127
- } else {
128
- var parent_1 = state.stack[state.stack.length - 2];
129
- if (!parent_1.children || typeof parent_1.children === 'string') {
130
- parent_1.children = [];
131
- }
132
- parent_1.children.push(component);
133
- }
134
- } else {
135
- if (state.stack.length === 0) {
136
- result.push(component);
137
- } else {
138
- var parent_2 = state.stack[state.stack.length - 1];
139
- if (!parent_2.children || typeof parent_2.children === 'string') {
140
- parent_2.children = [];
141
- }
142
- parent_2.children.push(component);
143
- }
144
- }
145
- i += endIndex;
146
- char = input[i];
147
- continue;
148
- }
149
- }
150
- state.buffer += char;
151
- i++;
152
- }
153
- // Handle any remaining text in the buffer
154
- var finalText = state.buffer.trim();
155
- if (finalText && state.stack.length === 0) {
156
- if (finalText === '<') {
157
- result.push({
158
- type: "text",
159
- value: ''
160
- });
161
- } else {
162
- result.push({
163
- type: "text",
164
- value: finalText
165
- });
166
- }
167
- } else if (finalText && state.stack.length > 0) {
168
- var currentComponent = state.stack[state.stack.length - 1];
169
- if (!currentComponent.children) {
170
- currentComponent.children = [];
171
- }
172
- if (typeof currentComponent.children === 'string') {
173
- currentComponent.children = [{
174
- type: "text",
175
- value: currentComponent.children
176
- }];
177
- }
178
- currentComponent.children.push({
179
- type: "text",
180
- value: finalText
181
- });
182
- }
183
- state.position = i;
184
- return result;
185
- };
186
- MDXStreamingParser.prototype.findComponentClose = function (input, startIndex) {
187
- var depth = 1;
188
- var i = startIndex;
189
- var openclose = false; // 内嵌开闭标签要成对
190
- while (i < input.length) {
191
- if (input[i] === '/' && input[i + 1] === '>') {
192
- depth--;
193
- if (depth === 0) return i + 2;
194
- i += 2;
195
- } else if (input[i] === '<') {
196
- openclose = true;
197
- depth++;
198
- i++;
199
- } else if (input[i] === '>') {
200
- if (openclose) {
201
- openclose = false;
202
- depth--;
203
- }
204
- if (depth === 0) return i + 1;
205
- i++;
206
- } else {
207
- i++;
208
- }
209
- }
210
- return input.length;
211
- };
212
- MDXStreamingParser.prototype.parseComponentTag = function (input) {
213
- var props = {};
214
- var i = 0;
215
- // 跳过组件名
216
- while (i < input.length && input[i] !== ' ' && input[i] !== '>') i++;
217
- // 使用 parsePropsCommon 方法解析属性,该方法会返回标签形式信息
218
- var _a = this.parsePropsCommon(input),
219
- parsedProps = _a.props,
220
- endIndex = _a.endIndex,
221
- selfClosing = _a.selfClosing;
222
- Object.assign(props, parsedProps);
223
- return {
224
- props: props,
225
- endIndex: endIndex,
226
- selfClosing: selfClosing
227
- };
228
- };
229
- MDXStreamingParser.prototype.parsePropsCommon = function (input) {
230
- var props = {};
231
- var i = 0;
232
- var inQuote = false;
233
- var quoteChar = '';
234
- var bracketCount = 0;
235
- var selfClosing = false;
236
- // 跳过组件名
237
- while (i < input.length && input[i] !== ' ' && input[i] !== '>') i++;
238
- while (i < input.length) {
239
- var char = input[i];
240
- if (char === '"' || char === "'") {
241
- if (!inQuote) {
242
- inQuote = true;
243
- quoteChar = char;
244
- } else if (char === quoteChar && bracketCount === 0) {
245
- inQuote = false;
246
- }
247
- } else if (char === '{' && !inQuote) {
248
- bracketCount++;
249
- } else if (char === '}' && !inQuote) {
250
- bracketCount--;
251
- } else if (char === '/' && input[i + 1] === '>' && !inQuote && bracketCount === 0) {
252
- selfClosing = true;
253
- return {
254
- props: props,
255
- endIndex: i + 2,
256
- selfClosing: selfClosing
257
- };
258
- } else if (char === '>' && !inQuote && bracketCount === 0) {
259
- return {
260
- props: props,
261
- endIndex: i + 1,
262
- selfClosing: selfClosing
263
- };
264
- }
265
- if (char === '=' && !inQuote && bracketCount === 0) {
266
- var propName = '';
267
- var j = i - 1;
268
- while (j >= 0 && /[\w-]/.test(input[j])) {
269
- propName = input[j] + propName;
270
- j--;
271
- }
272
- var value = '';
273
- j = i + 1;
274
- if (input[j] === '{') {
275
- // 处理 JSON 值
276
- var startBracket = j;
277
- bracketCount = 1;
278
- j++;
279
- while (j < input.length && bracketCount > 0) {
280
- if (input[j] === '{') bracketCount++;
281
- if (input[j] === '}') bracketCount--;
282
- j++;
283
- }
284
- value = input.slice(startBracket + 1, j - 1);
285
- try {
286
- // 处理属性值中的模板字符串
287
- value = value.replace(/`([^`]*)`/g, function (match, p1) {
288
- return JSON.stringify(p1);
289
- });
290
- // 处理可能的对象字面量
291
- if (value.trim().startsWith('{')) {
292
- props[propName] = JSON.parse(value);
293
- } else {
294
- // 处理其他 JSON 值
295
- try {
296
- props[propName] = JSON.parse(value);
297
- } catch (_a) {
298
- // 如果不是有效的 JSON,保持原值
299
- props[propName] = value;
300
- }
301
- }
302
- } catch (e) {
303
- console.warn("Failed to parse prop value for ".concat(propName, ":"), value);
304
- props[propName] = value;
305
- }
306
- i = j;
307
- continue;
308
- } else if (input[j] === '"' || input[j] === "'") {
309
- // 处理字符串值
310
- var quote = input[j];
311
- j++;
312
- while (j < input.length && input[j] !== quote) {
313
- value += input[j];
314
- j++;
315
- }
316
- props[propName] = value;
317
- i = j + 1;
318
- continue;
319
- }
320
- }
321
- i++;
322
- }
323
- return {
324
- props: props,
325
- endIndex: i,
326
- selfClosing: selfClosing
327
- };
328
- };
329
- MDXStreamingParser.prototype.parsePropsSelfClosing = function (content) {
330
- var props = {};
331
- // 先找到当前标签的结束位置
332
- var endIndex = content.indexOf('/>');
333
- if (endIndex === -1) return props;
334
- // 只处理当前标签内的内容
335
- var tagContent = content.slice(0, endIndex);
336
- // 使用更精确的正则匹配属性
337
- var propRegex = /(\w+)=(?:"([^"]*)"|'([^']*)'|{([^}]*)})/g;
338
- var match;
339
- while ((match = propRegex.exec(tagContent)) !== null) {
340
- var propName = match[1];
341
- var value = void 0;
342
- if (match[2] !== undefined) {
343
- // 处理双引号值
344
- value = match[2];
345
- } else if (match[3] !== undefined) {
346
- // 处理单引号值
347
- value = match[3];
348
- } else if (match[4] !== undefined) {
349
- // 处理花括号值
350
- try {
351
- value = JSON.parse(match[4]);
352
- } catch (e) {
353
- value = match[4]; // 解析失败保留原始值
354
- }
355
- }
356
- props[propName] = value;
357
- }
358
- return props;
359
- };
360
- MDXStreamingParser.prototype.reset = function () {
361
- this.messages.clear();
362
- };
363
- return MDXStreamingParser;
364
- }();
365
-
366
- /******************************************************************************
367
- Copyright (c) Microsoft Corporation.
368
-
369
- Permission to use, copy, modify, and/or distribute this software for any
370
- purpose with or without fee is hereby granted.
371
-
372
- THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES WITH
373
- REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
374
- AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY SPECIAL, DIRECT,
375
- INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
376
- LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR
377
- OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
378
- PERFORMANCE OF THIS SOFTWARE.
379
- ***************************************************************************** */
380
- /* global Reflect, Promise, SuppressedError, Symbol, Iterator */
381
-
382
- var extendStatics = function(d, b) {
383
- extendStatics = Object.setPrototypeOf ||
384
- ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
385
- function (d, b) { for (var p in b) if (Object.prototype.hasOwnProperty.call(b, p)) d[p] = b[p]; };
386
- return extendStatics(d, b);
387
- };
388
-
389
- function __extends(d, b) {
390
- if (typeof b !== "function" && b !== null)
391
- throw new TypeError("Class extends value " + String(b) + " is not a constructor or null");
392
- extendStatics(d, b);
393
- function __() { this.constructor = d; }
394
- d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
395
- }
396
-
397
- var __assign = function() {
398
- __assign = Object.assign || function __assign(t) {
399
- for (var s, i = 1, n = arguments.length; i < n; i++) {
400
- s = arguments[i];
401
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p)) t[p] = s[p];
402
- }
403
- return t;
404
- };
405
- return __assign.apply(this, arguments);
406
- };
407
-
408
- function __rest(s, e) {
409
- var t = {};
410
- for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p) && e.indexOf(p) < 0)
411
- t[p] = s[p];
412
- if (s != null && typeof Object.getOwnPropertySymbols === "function")
413
- for (var i = 0, p = Object.getOwnPropertySymbols(s); i < p.length; i++) {
414
- if (e.indexOf(p[i]) < 0 && Object.prototype.propertyIsEnumerable.call(s, p[i]))
415
- t[p[i]] = s[p[i]];
416
- }
417
- return t;
418
- }
419
-
420
- function __awaiter(thisArg, _arguments, P, generator) {
421
- function adopt(value) { return value instanceof P ? value : new P(function (resolve) { resolve(value); }); }
422
- return new (P || (P = Promise))(function (resolve, reject) {
423
- function fulfilled(value) { try { step(generator.next(value)); } catch (e) { reject(e); } }
424
- function rejected(value) { try { step(generator["throw"](value)); } catch (e) { reject(e); } }
425
- function step(result) { result.done ? resolve(result.value) : adopt(result.value).then(fulfilled, rejected); }
426
- step((generator = generator.apply(thisArg, _arguments || [])).next());
427
- });
428
- }
429
-
430
- function __generator(thisArg, body) {
431
- var _ = { label: 0, sent: function() { if (t[0] & 1) throw t[1]; return t[1]; }, trys: [], ops: [] }, f, y, t, g = Object.create((typeof Iterator === "function" ? Iterator : Object).prototype);
432
- return g.next = verb(0), g["throw"] = verb(1), g["return"] = verb(2), typeof Symbol === "function" && (g[Symbol.iterator] = function() { return this; }), g;
433
- function verb(n) { return function (v) { return step([n, v]); }; }
434
- function step(op) {
435
- if (f) throw new TypeError("Generator is already executing.");
436
- while (g && (g = 0, op[0] && (_ = 0)), _) try {
437
- if (f = 1, y && (t = op[0] & 2 ? y["return"] : op[0] ? y["throw"] || ((t = y["return"]) && t.call(y), 0) : y.next) && !(t = t.call(y, op[1])).done) return t;
438
- if (y = 0, t) op = [op[0] & 2, t.value];
439
- switch (op[0]) {
440
- case 0: case 1: t = op; break;
441
- case 4: _.label++; return { value: op[1], done: false };
442
- case 5: _.label++; y = op[1]; op = [0]; continue;
443
- case 7: op = _.ops.pop(); _.trys.pop(); continue;
444
- default:
445
- if (!(t = _.trys, t = t.length > 0 && t[t.length - 1]) && (op[0] === 6 || op[0] === 2)) { _ = 0; continue; }
446
- if (op[0] === 3 && (!t || (op[1] > t[0] && op[1] < t[3]))) { _.label = op[1]; break; }
447
- if (op[0] === 6 && _.label < t[1]) { _.label = t[1]; t = op; break; }
448
- if (t && _.label < t[2]) { _.label = t[2]; _.ops.push(op); break; }
449
- if (t[2]) _.ops.pop();
450
- _.trys.pop(); continue;
451
- }
452
- op = body.call(thisArg, _);
453
- } catch (e) { op = [6, e]; y = 0; } finally { f = t = 0; }
454
- if (op[0] & 5) throw op[1]; return { value: op[0] ? op[1] : void 0, done: true };
455
- }
456
- }
457
-
458
- function __read(o, n) {
459
- var m = typeof Symbol === "function" && o[Symbol.iterator];
460
- if (!m) return o;
461
- var i = m.call(o), r, ar = [], e;
462
- try {
463
- while ((n === void 0 || n-- > 0) && !(r = i.next()).done) ar.push(r.value);
464
- }
465
- catch (error) { e = { error: error }; }
466
- finally {
467
- try {
468
- if (r && !r.done && (m = i["return"])) m.call(i);
469
- }
470
- finally { if (e) throw e.error; }
471
- }
472
- return ar;
473
- }
474
-
475
- function __spreadArray(to, from, pack) {
476
- if (pack || arguments.length === 2) for (var i = 0, l = from.length, ar; i < l; i++) {
477
- if (ar || !(i in from)) {
478
- if (!ar) ar = Array.prototype.slice.call(from, 0, i);
479
- ar[i] = from[i];
480
- }
481
- }
482
- return to.concat(ar || Array.prototype.slice.call(from));
483
- }
484
-
485
- typeof SuppressedError === "function" ? SuppressedError : function (error, suppressed, message) {
486
- var e = new Error(message);
487
- return e.name = "SuppressedError", e.error = error, e.suppressed = suppressed, e;
488
- };
489
-
490
- var CodeHighlight = function (_a) {
491
- var textContent = _a.textContent,
492
- _b = _a.language,
493
- language = _b === void 0 ? "txt" : _b,
494
- darkMode = _a.darkMode;
495
- var _c = __read(useState(false), 2),
496
- showCopy = _c[0],
497
- setShowCopy = _c[1];
498
- var copyToClipboard = function (text) {
499
- return __awaiter(void 0, void 0, void 0, function () {
500
- var textarea;
501
- return __generator(this, function (_a) {
502
- switch (_a.label) {
503
- case 0:
504
- _a.trys.push([0, 2,, 3]);
505
- return [4 /*yield*/, navigator.clipboard.writeText(text)];
506
- case 1:
507
- _a.sent();
508
- // 简单的复制成功提示
509
- alert('代码已复制到剪贴板');
510
- return [3 /*break*/, 3];
511
- case 2:
512
- _a.sent();
513
- textarea = document.createElement("textarea");
514
- textarea.value = text;
515
- document.body.appendChild(textarea);
516
- textarea.select();
517
- try {
518
- document.execCommand("copy");
519
- alert('代码已复制到剪贴板');
520
- } catch (error) {
521
- console.error("Fallback copy failed", error);
522
- }
523
- document.body.removeChild(textarea);
524
- return [3 /*break*/, 3];
525
- case 3:
526
- return [2 /*return*/];
527
- }
528
- });
529
- });
530
- };
531
- // 标准化语言名称,增强对 mermaid 语言的识别
532
- var normalizedLanguage = language.toLowerCase();
533
- // 检查是否为 mermaid 语言的变体
534
- if (normalizedLanguage.includes('mermaid')) {
535
- normalizedLanguage = "mermaid";
536
- }
537
- if (language === "mermaid") {
538
- // 动态导入 MermaidRenderer 组件
539
- var MermaidRenderer = /*#__PURE__*/React__default.lazy(function () {
540
- return import('./MermaidRenderer-680adac3.js').catch(function () {
541
- return {
542
- default: function () {
543
- return /*#__PURE__*/jsx("div", {});
544
- }
545
- };
546
- });
547
- });
548
- return /*#__PURE__*/jsx(React__default.Suspense, {
549
- fallback: /*#__PURE__*/jsx("div", {}),
550
- children: /*#__PURE__*/jsx(MermaidRenderer, {
551
- chart: String(textContent)
552
- }, Date.now())
553
- });
554
- }
555
- // 简单的代码高亮样式
556
- var codeStyle = {
557
- backgroundColor: darkMode ? '#2d2d2d' : '#f5f5f5',
558
- color: darkMode ? '#f8f8f2' : '#333',
559
- padding: '12px',
560
- borderRadius: '4px',
561
- fontFamily: 'monospace',
562
- fontSize: '14px',
563
- lineHeight: '1.4',
564
- overflowX: 'auto',
565
- border: "1px solid ".concat(darkMode ? '#555' : '#ddd')
566
- };
567
- return /*#__PURE__*/jsxs("div", {
568
- style: {
569
- position: "relative"
570
- },
571
- children: [/*#__PURE__*/jsx("button", {
572
- style: {
573
- position: "absolute",
574
- right: "10px",
575
- top: "5px",
576
- zIndex: 1,
577
- background: darkMode ? "#555" : "#333",
578
- color: "#fff",
579
- border: "none",
580
- padding: "4px 8px",
581
- fontSize: "12px",
582
- cursor: "pointer",
583
- borderRadius: "4px"
584
- },
585
- onClick: function (e) {
586
- e.preventDefault();
587
- copyToClipboard(String(textContent));
588
- },
589
- onMouseEnter: function () {
590
- return setShowCopy(true);
591
- },
592
- onMouseLeave: function () {
593
- return setShowCopy(false);
594
- },
595
- children: showCopy ? "点击复制" : "复制"
596
- }), /*#__PURE__*/jsx("pre", {
597
- style: codeStyle,
598
- children: /*#__PURE__*/jsx("code", {
599
- children: String(textContent).replace(/\n$/, "")
600
- })
601
- })]
602
- });
603
- };
604
-
605
- var Think = function (_a) {
606
- var content = _a.content,
607
- children = _a.children;
608
- var _b = __read(useState(true), 2),
609
- isExpanded = _b[0],
610
- setIsExpanded = _b[1];
611
- var thinkContent = content || children;
612
- if (!thinkContent) {
613
- return /*#__PURE__*/jsx("div", {});
614
- }
615
- return /*#__PURE__*/jsxs("div", {
616
- className: "funq-think border rounded-lg p-3 mb-3 bg-blue-50 border-blue-200",
617
- children: [/*#__PURE__*/jsxs("div", {
618
- className: "flex items-center justify-between cursor-pointer",
619
- onClick: function () {
620
- return setIsExpanded(!isExpanded);
621
- },
622
- children: [/*#__PURE__*/jsxs("div", {
623
- className: "flex items-center gap-2",
624
- children: [/*#__PURE__*/jsx("div", {
625
- className: "w-4 h-4 rounded-full bg-blue-500 flex items-center justify-center",
626
- children: /*#__PURE__*/jsx("svg", {
627
- className: "w-3 h-3 text-white",
628
- fill: "none",
629
- stroke: "currentColor",
630
- viewBox: "0 0 24 24",
631
- children: /*#__PURE__*/jsx("path", {
632
- strokeLinecap: "round",
633
- strokeLinejoin: "round",
634
- strokeWidth: "2",
635
- d: "M9.663 17h4.673M12 3v1m6.364 1.636l-.707.707M21 12h-1M4 12H3m3.343-5.657l-.707-.707m2.828 9.9a5 5 0 117.072 0l-.548.547A3.374 3.374 0 0014 18.469V19a2 2 0 11-4 0v-.531c0-.895-.356-1.754-.988-2.386l-.548-.547z"
636
- })
637
- })
638
- }), /*#__PURE__*/jsx("span", {
639
- className: "font-medium text-sm",
640
- children: "AI \u601D\u8003\u8FC7\u7A0B"
641
- })]
642
- }), /*#__PURE__*/jsx("div", {
643
- className: "w-4 h-4 text-muted-foreground",
644
- children: isExpanded ? /*#__PURE__*/jsx("svg", {
645
- fill: "none",
646
- stroke: "currentColor",
647
- viewBox: "0 0 24 24",
648
- children: /*#__PURE__*/jsx("path", {
649
- strokeLinecap: "round",
650
- strokeLinejoin: "round",
651
- strokeWidth: "2",
652
- d: "M5 15l7-7 7 7"
653
- })
654
- }) : /*#__PURE__*/jsx("svg", {
655
- fill: "none",
656
- stroke: "currentColor",
657
- viewBox: "0 0 24 24",
658
- children: /*#__PURE__*/jsx("path", {
659
- strokeLinecap: "round",
660
- strokeLinejoin: "round",
661
- strokeWidth: "2",
662
- d: "M19 9l-7 7-7-7"
663
- })
664
- })
665
- })]
666
- }), isExpanded && /*#__PURE__*/jsx("div", {
667
- className: "mt-3 pt-3 border-t border-blue-200",
668
- children: /*#__PURE__*/jsx("blockquote", {
669
- className: "text-sm text-blue-800 bg-blue-100 p-2 rounded",
670
- children: typeof thinkContent === 'string' ? decodeURIComponent(thinkContent) : thinkContent
671
- })
672
- })]
673
- });
674
- };
675
-
676
- var VideoComponent = function (_a) {
677
- _a.node;
678
- var props = __rest(_a, ["node"]);
679
- var videoNode = {
680
- props: {}
681
- };
682
- try {
683
- props.children.map(function (item) {
684
- if (item.props && item.type === 'source') {
685
- videoNode = item;
686
- }
687
- });
688
- } catch (e) {}
689
- return /*#__PURE__*/jsxs("video", {
690
- controls: true,
691
- ...props,
692
- children: [/*#__PURE__*/jsx("source", {
693
- src: videoNode.props.src || '',
694
- type: videoNode.props.type || 'video/mp4'
695
- }), "\u4F60\u7684\u6D4F\u89C8\u5668\u4E0D\u652F\u6301video\u6807\u7B7E"]
696
- });
697
- };
698
- // 音频组件
699
- var AudioComponent = function (_a) {
700
- _a.node;
701
- var props = __rest(_a, ["node"]);
702
- var audioNode = {
703
- props: {}
704
- };
705
- try {
706
- props.children.map(function (item) {
707
- if (item.props && item.type === 'source') {
708
- audioNode = item;
709
- }
710
- });
711
- } catch (e) {
712
- console.error('Error processing audio children:', e);
713
- }
714
- return /*#__PURE__*/jsxs("audio", {
715
- controls: true,
716
- ...props,
717
- children: [/*#__PURE__*/jsx("source", {
718
- src: audioNode.props.src || '',
719
- type: audioNode.props.type || 'audio/mpeg'
720
- }), "\u4F60\u7684\u6D4F\u89C8\u5668\u4E0D\u652F\u6301 audio \u6807\u7B7E"]
721
- });
722
- };
723
- // 代码组件
724
- var CodeComponent = function (props) {
725
- var children = props.children,
726
- className = props.className;
727
- props.node;
728
- var rest = __rest(props, ["children", "className", "node"]);
729
- var match = /language-(\w+)/.exec(className || "");
730
- String(children).replace(/\n$/, '');
731
- var extractText = function (children) {
732
- return React__default.Children.toArray(children).reduce(function (acc, child) {
733
- if (typeof child === 'string') {
734
- return acc + child;
735
- } else if (/*#__PURE__*/React__default.isValidElement(child) && child.props && child.props.children) {
736
- return acc + extractText(child.props.children);
737
- }
738
- return acc;
739
- }, '');
740
- };
741
- var content = extractText(children);
742
- // 增强语言检测逻辑,检查内容中是否包含mermaid特征
743
- var language = match ? match[1] : "txt";
744
- // 如果内容包含mermaid特征且语言未正确识别,则强制设置为mermaid
745
- if (!match || match[1] !== "mermaid") {
746
- var trimmedContent = content.trim();
747
- if (trimmedContent.startsWith('graph') || trimmedContent.startsWith('flowchart') || trimmedContent.startsWith('sequenceDiagram') || trimmedContent.startsWith('gantt') || trimmedContent.startsWith('classDiagram') || trimmedContent.startsWith('stateDiagram') || trimmedContent.startsWith('pie') || trimmedContent.startsWith('erDiagram') || trimmedContent.startsWith('journey') || trimmedContent.startsWith('requirementDiagram') || trimmedContent.startsWith('gitGraph')) {
748
- language = "mermaid";
749
- }
750
- }
751
- return language !== "txt" ? /*#__PURE__*/jsx(CodeHighlight, {
752
- ...rest,
753
- language: language,
754
- textContent: content,
755
- children: content
756
- }) : /*#__PURE__*/jsx("code", {
757
- className: className,
758
- ...props,
759
- children: content
760
- });
761
- };
762
- // 段落组件
763
- var PComponent = function (pProps) {
764
- return /*#__PURE__*/jsx("p", {
765
- ...pProps,
766
- dir: "auto"
767
- });
768
- };
769
- // 链接组件
770
- var AComponent = function (aProps) {
771
- var _a;
772
- var href = aProps.href || "";
773
- var isInternal = /^\/#/i.test(href);
774
- var target = isInternal ? "_self" : (_a = aProps.target) !== null && _a !== void 0 ? _a : "_blank";
775
- // 增强链接样式,使其更加明显
776
- var linkStyle = {
777
- color: '#1a73e8',
778
- fontWeight: '600',
779
- borderBottom: '2px solid #1a73e8',
780
- paddingBottom: '2px',
781
- transition: 'all 0.2s ease'
782
- };
783
- // 鼠标悬停效果
784
- var hoverStyle = {
785
- color: '#0d47a1',
786
- borderBottom: '2px solid #0d47a1',
787
- backgroundColor: 'rgba(26, 115, 232, 0.1)'
788
- };
789
- // 合并原有属性和新样式
790
- return /*#__PURE__*/jsx("a", {
791
- ...aProps,
792
- target: target,
793
- style: __assign(__assign({}, linkStyle), aProps.style),
794
- onMouseEnter: function (e) {
795
- var target = e.target;
796
- Object.assign(target.style, hoverStyle);
797
- aProps.onMouseEnter && aProps.onMouseEnter(e);
798
- },
799
- onMouseLeave: function (e) {
800
- var target = e.target;
801
- Object.assign(target.style, linkStyle);
802
- aProps.onMouseLeave && aProps.onMouseLeave(e);
803
- }
804
- });
805
- };
806
- // Pre 组件
807
- var PreComponent = function (props) {
808
- return /*#__PURE__*/jsx("div", {
809
- ...props
810
- });
811
- };
812
- var BuiltInComponents = {
813
- _THINK: Think,
814
- // _TOOL_CALL: ToolCall,
815
- pre: PreComponent,
816
- code: CodeComponent,
817
- p: PComponent,
818
- a: AComponent,
819
- video: VideoComponent,
820
- audio: AudioComponent
821
- };
822
-
823
- var ErrorBoundary = /** @class */function (_super) {
824
- __extends(ErrorBoundary, _super);
825
- function ErrorBoundary(props) {
826
- var _this = _super.call(this, props) || this;
827
- _this.state = {
828
- hasError: false
829
- };
830
- return _this;
831
- }
832
- ErrorBoundary.getDerivedStateFromError = function () {
833
- return {
834
- hasError: true
835
- };
836
- };
837
- ErrorBoundary.prototype.componentDidCatch = function (error, errorInfo) {
838
- // 可以在这里记录错误信息
839
- };
840
- ErrorBoundary.prototype.render = function () {
841
- if (this.state.hasError) {
842
- return /*#__PURE__*/jsx("div", {
843
- children: this.props.content
844
- });
845
- }
846
- return this.props.children;
847
- };
848
- return ErrorBoundary;
849
- }(React__default.Component);
850
-
851
- function MdxLayout(_a) {
852
- var children = _a.children;
853
- return /*#__PURE__*/jsx("div", {
854
- children: children
855
- });
856
- }
857
-
858
- var THINK_BEGIN_TAG = '<think>';
859
- var THINK_END_TAG = '</think>';
860
- var CUSTOMER_THINK_COMPONENT_DISPLAY = '_THINK';
861
- function fixMDXContent(content) {
862
- // 替换中文括号为英文括号
863
- return content.replace(/\\\[/g, '$$').replace(/\\\]/g, '$$')
864
- // 确保LaTeX公式正确转义
865
- .replace(/\\\(/g, '$').replace(/\\\)/g, '$');
866
- }
867
- function generateRandomName(length) {
868
- if (length === void 0) {
869
- length = 6;
870
- }
871
- var characters = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789';
872
- var result = '';
873
- for (var i = 0; i < length; i++) {
874
- result += characters.charAt(Math.floor(Math.random() * characters.length));
875
- }
876
- return result;
877
- }
878
- function remarkThinkUpdate(content) {
879
- var thinkContent = '';
880
- var resultContent = '';
881
- var thinkBeginIndex = content.indexOf(THINK_BEGIN_TAG);
882
- var thinkEndIndex = content.lastIndexOf(THINK_END_TAG);
883
- if (thinkBeginIndex !== -1 && thinkEndIndex === -1) {
884
- // 有开始,无结束
885
- thinkContent = content.slice(THINK_BEGIN_TAG.length);
886
- thinkContent = "<".concat(CUSTOMER_THINK_COMPONENT_DISPLAY, " content=\"").concat(encodeURIComponent(thinkContent), "\" />");
887
- } else if (thinkBeginIndex !== -1 && thinkEndIndex !== -1) {
888
- // 有开始,有结束
889
- var anotherEndIndex = content.indexOf(THINK_END_TAG);
890
- if (anotherEndIndex !== thinkEndIndex) {
891
- thinkContent = content.slice(THINK_BEGIN_TAG.length, anotherEndIndex);
892
- resultContent = content.slice(thinkEndIndex + THINK_END_TAG.length);
893
- thinkContent = "<".concat(CUSTOMER_THINK_COMPONENT_DISPLAY, " content=\"").concat(encodeURIComponent(thinkContent), "\" />");
894
- } else {
895
- thinkContent = content.slice(THINK_BEGIN_TAG.length, thinkEndIndex);
896
- thinkContent = "<".concat(CUSTOMER_THINK_COMPONENT_DISPLAY, " content=\"").concat(encodeURIComponent(thinkContent), "\" />");
897
- resultContent = content.slice(thinkEndIndex + THINK_END_TAG.length);
898
- }
899
- } else if (thinkBeginIndex == -1) {
900
- resultContent = content;
901
- }
902
- return {
903
- thinkContent: thinkContent,
904
- resultContent: resultContent
905
- };
906
- }
907
- /**
908
- * 转义流式内容中的特殊字符,防止MDX解析错误
909
- * @param content 需要转义的内容
910
- * @returns 转义后的内容
911
- */
912
- function escapeContentForStream(content) {
913
- if (typeof content !== 'string') return content;
914
- return content.replace(/&/g, '&amp;').replace(/</g, '&lt;').replace(/>/g, '&gt;').replace(/"/g, '&quot;').replace(/'/g, '&#x27;').replace(/{/g, '&#123;').replace(/}/g, '&#125;');
915
- }
916
-
917
- var renderTimer = null;
918
- // 辅助函数:格式化props
919
- function formatProps(props) {
920
- return Object.entries(props).map(function (_a) {
921
- var _b = __read(_a, 2),
922
- key = _b[0],
923
- value = _b[1];
924
- if (typeof value === 'string') {
925
- return "".concat(key, "=\"").concat(value, "\"");
926
- }
927
- try {
928
- value = JSON.stringify(value);
929
- } catch (e) {
930
- console.error('Error stringifying prop value:', e);
931
- }
932
- return "".concat(key, "={").concat(value, "}");
933
- }).join(' ');
934
- }
935
- function renderMdx(mdxContent, scope, registeredComponents) {
936
- return __awaiter(this, void 0, void 0, function () {
937
- var componentTagRegex, usedComponents, match, vf, XComponent;
938
- return __generator(this, function (_a) {
939
- switch (_a.label) {
940
- case 0:
941
- _a.trys.push([0, 3,, 4]);
942
- renderTimer && clearTimeout(renderTimer);
943
- componentTagRegex = /<([A-Z][a-zA-Z]*)\b/g;
944
- usedComponents = new Set();
945
- match = void 0;
946
- while ((match = componentTagRegex.exec(mdxContent)) !== null) {
947
- usedComponents.add(match[1]);
948
- }
949
- return [4 /*yield*/, compile(mdxContent, {
950
- outputFormat: "function-body",
951
- remarkPlugins: [[RemarkMath, {
952
- strict: true
953
- }], RemarkGfm, RemarkBreaks],
954
- rehypePlugins: [[RehypeKatex], [RehypeHighlight, {
955
- detect: false,
956
- ignoreMissing: true,
957
- plainText: ['mermaid'] // 将mermaid作为纯文本处理,避免被错误高亮
958
- }]]
959
- })];
960
- case 1:
961
- vf = _a.sent();
962
- return [4 /*yield*/, run(vf, __assign(__assign(__assign({}, runtime), scope), {
963
- Fragment: MdxLayout
964
- }))];
965
- case 2:
966
- XComponent = _a.sent().default;
967
- return [2 /*return*/, /*#__PURE__*/React.createElement(XComponent, {
968
- components: registeredComponents,
969
- scope: scope
970
- })];
971
- case 3:
972
- _a.sent();
973
- return [2 /*return*/, new Promise(function (resolve) {
974
- renderTimer = setTimeout(function () {
975
- resolve(/*#__PURE__*/React.createElement('div', {
976
- style: {
977
- backgroundColor: 'transparent'
978
- }
979
- }, mdxContent));
980
- clearTimeout(renderTimer); // 清理定时器
981
- }, 2000); // 1秒延迟
982
- })];
983
- case 4:
984
- return [2 /*return*/];
985
- }
986
- });
987
- });
988
- }
989
- // 递归解析组件的函数
990
- function parseComponentRecursively(allComponentHandlers, item, scope) {
991
- return __awaiter(this, void 0, void 0, function () {
992
- var componentHandler, componentString_1, parsedChildren, props, propsString, childrenContent_1, parsedChildren_1, componentString;
993
- return __generator(this, function (_a) {
994
- switch (_a.label) {
995
- case 0:
996
- if (item.type === 'text') {
997
- return [2 /*return*/, item.value];
998
- }
999
- if (!(item.type === 'component')) return [3 /*break*/, 8];
1000
- componentHandler = allComponentHandlers.find(function (c) {
1001
- return c.name === item.value;
1002
- });
1003
- if (!!componentHandler) return [3 /*break*/, 5];
1004
- componentString_1 = "<".concat(item.value);
1005
- if (item.props) {
1006
- componentString_1 += ' ' + Object.entries(item.props).map(function (_a) {
1007
- var _b = __read(_a, 2),
1008
- key = _b[0],
1009
- value = _b[1];
1010
- if (typeof value === 'string') {
1011
- return "".concat(key, "=\"").concat(value, "\"");
1012
- }
1013
- try {
1014
- return "".concat(key, "={").concat(JSON.stringify(value), "}");
1015
- } catch (e) {
1016
- return "".concat(key, "=\"[object Object]\"");
1017
- }
1018
- }).join(' ');
1019
- }
1020
- if (!item.selfClosing) return [3 /*break*/, 1];
1021
- componentString_1 += ' />';
1022
- return [3 /*break*/, 4];
1023
- case 1:
1024
- componentString_1 += '>';
1025
- if (!(item.children && Array.isArray(item.children))) return [3 /*break*/, 3];
1026
- return [4 /*yield*/, Promise.all(item.children.map(function (child) {
1027
- return parseComponentRecursively(allComponentHandlers, child, scope);
1028
- }))];
1029
- case 2:
1030
- parsedChildren = _a.sent();
1031
- componentString_1 += parsedChildren.join('');
1032
- _a.label = 3;
1033
- case 3:
1034
- componentString_1 += "</".concat(item.value, ">");
1035
- _a.label = 4;
1036
- case 4:
1037
- return [2 /*return*/, componentString_1];
1038
- case 5:
1039
- props = __assign(__assign({}, item.props), {
1040
- key: generateRandomName()
1041
- });
1042
- propsString = formatProps(props);
1043
- propsString += " scope={props.scope}";
1044
- childrenContent_1 = '';
1045
- if (!(item.children && Array.isArray(item.children))) return [3 /*break*/, 7];
1046
- return [4 /*yield*/, Promise.all(item.children.map(function (child) {
1047
- return parseComponentRecursively(allComponentHandlers, child, scope);
1048
- }))];
1049
- case 6:
1050
- parsedChildren_1 = _a.sent();
1051
- item.children.map(function (item, index) {
1052
- if (item.type === 'text') {
1053
- childrenContent_1 += escapeContentForStream(parsedChildren_1[index]); // encodeURIComponent(parsedChildren[index]);
1054
- } else {
1055
- childrenContent_1 += parsedChildren_1[index];
1056
- }
1057
- });
1058
- _a.label = 7;
1059
- case 7:
1060
- // 调用组件的渲染钩子
1061
- if (componentHandler.onRender) {
1062
- componentHandler.onRender(item, scope);
1063
- }
1064
- componentString = componentHandler.selfClosing ? "<".concat(item.value, " ").concat(propsString, " />") : "\t\n<".concat(item.value, " ").concat(propsString, ">\n").concat(childrenContent_1, "\n</").concat(item.value, ">\t\n");
1065
- return [2 /*return*/, componentString];
1066
- case 8:
1067
- return [2 /*return*/, null];
1068
- }
1069
- });
1070
- });
1071
- }
1072
-
1073
- // 使用 WeakMap 作为缓存,避免重复检测相同组件类型
1074
- var componentDetectionCache = new WeakMap();
1075
- /**
1076
- * 检测组件是否为自闭合组件
1077
- * 通过检查组件是否接受 children 属性来判断
1078
- * @param component - 要检测的 React 组件
1079
- * @returns boolean - true 表示自闭合组件(不支持 children),false 表示非自闭合组件(支持 children)
1080
- */
1081
- function isSelfClosingComponent(component) {
1082
- // 首先检查缓存
1083
- if (componentDetectionCache.has(component)) {
1084
- return componentDetectionCache.get(component);
1085
- }
1086
- try {
1087
- // 对于函数组件,使用更可靠的方法检测
1088
- if (typeof component === 'function') {
1089
- // 优先检查组件的 propTypes(如果存在)
1090
- if ('propTypes' in component && component.propTypes) {
1091
- var propTypes = component.propTypes;
1092
- var hasChildrenPropType = 'children' in propTypes;
1093
- var result_1 = !hasChildrenPropType;
1094
- componentDetectionCache.set(component, result_1);
1095
- return result_1;
1096
- }
1097
- // 检查组件的 defaultProps(如果存在)
1098
- if ('defaultProps' in component && component.defaultProps) {
1099
- var defaultProps = component.defaultProps;
1100
- // 如果 defaultProps 中包含 children,则组件支持 children
1101
- if ('children' in defaultProps) {
1102
- componentDetectionCache.set(component, false);
1103
- return false;
1104
- }
1105
- }
1106
- // 检查函数签名(如果可用)
1107
- var componentString = component.toString();
1108
- // 移除注释和多余空格
1109
- var cleanString = componentString.replace(/\/\*[\s\S]*?\*\//g, '').replace(/\/\/.*$/gm, '').trim();
1110
- // 检查是否显式接受 children 参数
1111
- var hasChildrenDestructuring = /\(\s*\{[^}]*\bchildren\b[^}]*\}/.test(cleanString);
1112
- // 如果组件参数中明确包含 children,则认为是非自闭合组件
1113
- if (hasChildrenDestructuring) {
1114
- componentDetectionCache.set(component, false);
1115
- return false;
1116
- } else {
1117
- componentDetectionCache.set(component, true);
1118
- return true;
1119
- }
1120
- }
1121
- // 对于类组件,默认假设是非自闭合的
1122
- var result = false;
1123
- componentDetectionCache.set(component, result);
1124
- return result;
1125
- } catch (error) {
1126
- // 如果检测过程中出现错误,默认返回 false(非自闭合)
1127
- var result = false;
1128
- componentDetectionCache.set(component, result);
1129
- return result;
1130
- }
1131
- }
1132
-
1133
- function ReactAIRenderer(_a) {
1134
- var _this = this;
1135
- var content = _a.content,
1136
- _b = _a.scope,
1137
- scope = _b === void 0 ? {} : _b,
1138
- _c = _a.components,
1139
- components = _c === void 0 ? {} : _c,
1140
- children = _a.children,
1141
- _d = _a.componentHandlers,
1142
- componentHandlers = _d === void 0 ? [] : _d;
1143
- content = content || children || '';
1144
- var allComponents = __assign(__assign({}, BuiltInComponents), components);
1145
- // 修复:确保 components 是一个对象后再使用 Object.entries
1146
- var allComponentHandlers = __spreadArray(__spreadArray([], __read(components ? Object.entries(components).filter(function (_a) {
1147
- var _b = __read(_a, 1),
1148
- name = _b[0];
1149
- return !componentHandlers.some(function (handler) {
1150
- return handler.name === name;
1151
- });
1152
- }).map(function (_a) {
1153
- var _b = __read(_a, 2),
1154
- name = _b[0],
1155
- component = _b[1];
1156
- var selfClosing = isSelfClosingComponent(component);
1157
- return {
1158
- component: component,
1159
- name: name,
1160
- selfClosing: selfClosing
1161
- };
1162
- }) : []), false), __read(componentHandlers), false);
1163
- var _e = __read(useState(null), 2),
1164
- component = _e[0],
1165
- setComponent = _e[1];
1166
- var parser = new MDXStreamingParser(allComponentHandlers);
1167
- var mdxContent = content || '';
1168
- useEffect(function () {
1169
- var parseMDX = function () {
1170
- return __awaiter(_this, void 0, void 0, function () {
1171
- var ThinkComponent, ResultComponent, _a, thinkContent, resultContent, parsedData, parsedComponents, finalComponent, i, _result, MDXComponent;
1172
- return __generator(this, function (_b) {
1173
- switch (_b.label) {
1174
- case 0:
1175
- ThinkComponent = null;
1176
- ResultComponent = null;
1177
- _a = remarkThinkUpdate(mdxContent), thinkContent = _a.thinkContent, resultContent = _a.resultContent;
1178
- if (!thinkContent) return [3 /*break*/, 2];
1179
- return [4 /*yield*/, renderMdx(thinkContent, scope, allComponents)];
1180
- case 1:
1181
- ThinkComponent = _b.sent();
1182
- _b.label = 2;
1183
- case 2:
1184
- if (!resultContent) return [3 /*break*/, 8];
1185
- resultContent = fixMDXContent(resultContent);
1186
- parsedData = parser.parse('magic', resultContent);
1187
- return [4 /*yield*/, Promise.all(parsedData.map(function (item) {
1188
- return parseComponentRecursively(allComponentHandlers, item, scope);
1189
- }))];
1190
- case 3:
1191
- parsedComponents = _b.sent();
1192
- finalComponent = [];
1193
- i = 0;
1194
- _b.label = 4;
1195
- case 4:
1196
- if (!(i < parsedComponents.length)) return [3 /*break*/, 7];
1197
- return [4 /*yield*/, renderMdx(parsedComponents[i], scope, allComponents)];
1198
- case 5:
1199
- _result = _b.sent();
1200
- finalComponent.push(_result);
1201
- _b.label = 6;
1202
- case 6:
1203
- i++;
1204
- return [3 /*break*/, 4];
1205
- case 7:
1206
- ResultComponent = /*#__PURE__*/jsx("div", {
1207
- children: finalComponent
1208
- });
1209
- _b.label = 8;
1210
- case 8:
1211
- MDXComponent = /*#__PURE__*/jsxs(React__default.Fragment, {
1212
- children: [ThinkComponent, ResultComponent]
1213
- });
1214
- setComponent(MDXComponent);
1215
- return [2 /*return*/];
1216
- }
1217
- });
1218
- });
1219
- };
1220
- parseMDX();
1221
- }, [content]);
1222
- return /*#__PURE__*/jsx(ErrorBoundary, {
1223
- content: content,
1224
- children: component
1225
- });
1226
- }
1227
-
1228
- var ToolCall = function (_a) {
1229
- var toolCall = _a.toolCall,
1230
- children = _a.children;
1231
- var _b = __read(useState(false), 2),
1232
- isExpanded = _b[0],
1233
- setIsExpanded = _b[1];
1234
- var getStateIcon = function () {
1235
- switch (toolCall.state) {
1236
- case 'loading':
1237
- return /*#__PURE__*/jsx("div", {
1238
- className: "w-4 h-4 animate-spin border-2 border-blue-500 border-t-transparent rounded-full"
1239
- });
1240
- case 'done':
1241
- return /*#__PURE__*/jsx("div", {
1242
- className: "w-4 h-4 rounded-full bg-green-500 flex items-center justify-center",
1243
- children: /*#__PURE__*/jsx("svg", {
1244
- className: "w-3 h-3 text-white",
1245
- fill: "none",
1246
- stroke: "currentColor",
1247
- viewBox: "0 0 24 24",
1248
- children: /*#__PURE__*/jsx("path", {
1249
- strokeLinecap: "round",
1250
- strokeLinejoin: "round",
1251
- strokeWidth: "2",
1252
- d: "M5 13l4 4L19 7"
1253
- })
1254
- })
1255
- });
1256
- case 'error':
1257
- return /*#__PURE__*/jsx("div", {
1258
- className: "w-4 h-4 rounded-full bg-red-500 flex items-center justify-center",
1259
- children: /*#__PURE__*/jsx("svg", {
1260
- className: "w-3 h-3 text-white",
1261
- fill: "none",
1262
- stroke: "currentColor",
1263
- viewBox: "0 0 24 24",
1264
- children: /*#__PURE__*/jsx("path", {
1265
- strokeLinecap: "round",
1266
- strokeLinejoin: "round",
1267
- strokeWidth: "2",
1268
- d: "M6 18L18 6M6 6l12 12"
1269
- })
1270
- })
1271
- });
1272
- default:
1273
- return /*#__PURE__*/jsx("div", {
1274
- className: "w-4 h-4 animate-spin border-2 border-blue-500 border-t-transparent rounded-full"
1275
- });
1276
- }
1277
- };
1278
- var getStateText = function () {
1279
- switch (toolCall.state) {
1280
- case 'loading':
1281
- return '执行中...';
1282
- case 'done':
1283
- return '执行完成';
1284
- case 'error':
1285
- return '执行出错';
1286
- default:
1287
- return '执行中...';
1288
- }
1289
- };
1290
- var getStateClass = function () {
1291
- switch (toolCall.state) {
1292
- case 'loading':
1293
- return 'bg-blue-50 border-blue-200';
1294
- case 'done':
1295
- return 'bg-green-50 border-green-200';
1296
- case 'error':
1297
- return 'bg-red-50 border-red-200';
1298
- default:
1299
- return 'bg-blue-50 border-blue-200';
1300
- }
1301
- };
1302
- // 解析工具调用结果,如果是JSON字符串则格式化显示
1303
- var formatResult = function (result) {
1304
- if (!result) return result;
1305
- try {
1306
- // 尝试解析为JSON
1307
- var parsed = JSON.parse(result);
1308
- return JSON.stringify(parsed, null, 2);
1309
- } catch (e) {
1310
- // 如果不是有效的JSON,直接返回原始字符串
1311
- return result;
1312
- }
1313
- };
1314
- return /*#__PURE__*/jsxs("div", {
1315
- className: "funq-tool-call border rounded-lg p-3 mb-3 ".concat(getStateClass()),
1316
- children: [/*#__PURE__*/jsxs("div", {
1317
- className: "flex items-center justify-between cursor-pointer",
1318
- onClick: function () {
1319
- return setIsExpanded(!isExpanded);
1320
- },
1321
- children: [/*#__PURE__*/jsxs("div", {
1322
- className: "flex items-center gap-2",
1323
- children: [getStateIcon(), /*#__PURE__*/jsxs("span", {
1324
- className: "font-medium text-sm",
1325
- children: ["\u5DE5\u5177\u8C03\u7528: ", toolCall.toolCallName]
1326
- }), /*#__PURE__*/jsx("span", {
1327
- className: "text-xs text-muted-foreground",
1328
- children: getStateText()
1329
- })]
1330
- }), /*#__PURE__*/jsxs("div", {
1331
- className: "flex items-center gap-2",
1332
- children: [toolCall.startTime && /*#__PURE__*/jsxs("span", {
1333
- className: "text-xs text-muted-foreground",
1334
- children: ["\u5F00\u59CB: ", new Date(toolCall.startTime).toLocaleTimeString()]
1335
- }), toolCall.endTime && toolCall.executionTime && /*#__PURE__*/jsxs("span", {
1336
- className: "text-xs text-muted-foreground",
1337
- children: ["\u8017\u65F6: ", toolCall.executionTime, "ms"]
1338
- }), /*#__PURE__*/jsx("div", {
1339
- className: "w-4 h-4 text-muted-foreground",
1340
- children: isExpanded ? /*#__PURE__*/jsx("svg", {
1341
- fill: "none",
1342
- stroke: "currentColor",
1343
- viewBox: "0 0 24 24",
1344
- children: /*#__PURE__*/jsx("path", {
1345
- strokeLinecap: "round",
1346
- strokeLinejoin: "round",
1347
- strokeWidth: "2",
1348
- d: "M5 15l7-7 7 7"
1349
- })
1350
- }) : /*#__PURE__*/jsx("svg", {
1351
- fill: "none",
1352
- stroke: "currentColor",
1353
- viewBox: "0 0 24 24",
1354
- children: /*#__PURE__*/jsx("path", {
1355
- strokeLinecap: "round",
1356
- strokeLinejoin: "round",
1357
- strokeWidth: "2",
1358
- d: "M19 9l-7 7-7-7"
1359
- })
1360
- })
1361
- })]
1362
- })]
1363
- }), isExpanded && /*#__PURE__*/jsxs("div", {
1364
- className: "mt-3 pt-3 border-t border-border",
1365
- children: [toolCall.toolCallArgs && /*#__PURE__*/jsxs("div", {
1366
- className: "mb-3",
1367
- children: [/*#__PURE__*/jsx("h4", {
1368
- className: "text-sm font-medium mb-1",
1369
- children: "\u53C2\u6570:"
1370
- }), /*#__PURE__*/jsx("pre", {
1371
- className: "text-xs bg-muted p-2 rounded overflow-x-auto break-words whitespace-pre-wrap",
1372
- children: toolCall.toolCallArgs
1373
- })]
1374
- }), (toolCall.delta || toolCall.state === 'error') && /*#__PURE__*/jsxs("div", {
1375
- children: [/*#__PURE__*/jsx("h4", {
1376
- className: "text-sm font-medium mb-1",
1377
- children: "\u7ED3\u679C:"
1378
- }), /*#__PURE__*/jsx("div", {
1379
- className: "text-sm p-2 rounded ".concat(toolCall.state === 'error' ? 'bg-red-100 text-red-800' : 'bg-muted'),
1380
- children: toolCall.state === 'error' ? /*#__PURE__*/jsx("pre", {
1381
- className: "whitespace-pre-wrap break-words overflow-x-auto",
1382
- children: formatResult(toolCall.delta) || '工具执行出错'
1383
- }) : /*#__PURE__*/jsx("pre", {
1384
- className: "whitespace-pre-wrap break-words overflow-x-auto",
1385
- children: formatResult(toolCall.delta)
1386
- })
1387
- })]
1388
- }), children && /*#__PURE__*/jsx("div", {
1389
- className: "mt-2",
1390
- children: children
1391
- })]
1392
- })]
1393
- });
1394
- };
1395
-
1396
- // 导出 MDXStreamingParser 类
1397
-
1398
- export { MDXStreamingParser as M, ReactAIRenderer as R, Think as T, __read as _, __awaiter as a, __generator as b, ToolCall as c };