@chat-lab/ui 0.1.0-beta.41 → 0.1.0-beta.42

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.
@@ -3,8 +3,10 @@ import mitt from '../../../node_modules/.pnpm/mitt@3.0.1/node_modules/mitt/dist/
3
3
  import { proxy } from '../../../node_modules/.pnpm/valtio@2.1.8_patch_hash_boxi3qpsg2dztwe4vz7tsjpr3m_@types_react@17.0.2_react@17.0.2/node_modules/valtio/esm/vanilla.js';
4
4
  import v4 from '../../../node_modules/.pnpm/uuid@13.0.0/node_modules/uuid/dist/v4.js';
5
5
  import last from '../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/last.js';
6
+ import throttle from '../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/throttle.js';
6
7
  import findIndex from '../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/findIndex.js';
7
8
  import compact from '../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/compact.js';
9
+ import findLastIndex from '../../../node_modules/.pnpm/lodash-es@4.17.21/node_modules/lodash-es/findLastIndex.js';
8
10
 
9
11
  var __defProp = Object.defineProperty;
10
12
  var __export = (target, all) => {
@@ -307,6 +309,7 @@ var BetterEventSource = class {
307
309
  },
308
310
  () => this.isNormalFinished = true
309
311
  );
312
+ const decoder = new TextDecoder();
310
313
  try {
311
314
  while (true) {
312
315
  const { done, value } = await reader.read();
@@ -324,7 +327,7 @@ var BetterEventSource = class {
324
327
  }
325
328
  break;
326
329
  }
327
- const text = new TextDecoder().decode(value);
330
+ const text = decoder.decode(value, { stream: true });
328
331
  let response = null;
329
332
  if (text.startsWith("data:")) {
330
333
  response = text.slice(5);
@@ -768,7 +771,7 @@ var A2AProtocol = class extends BaseProtocols_default {
768
771
  });
769
772
  }
770
773
  }
771
- return { ...message, content: message.content.concat(contents) };
774
+ return { ...message, content: contents };
772
775
  }
773
776
  return message;
774
777
  }
@@ -811,8 +814,6 @@ var A2AProtocol = class extends BaseProtocols_default {
811
814
  }
812
815
  };
813
816
  var a2a_default = A2AProtocol;
814
-
815
- // src/protocols/ark.ts
816
817
  var ArkProtocol = class extends BaseProtocols_default {
817
818
  // stream: boolean;
818
819
  constructor(options) {
@@ -825,19 +826,43 @@ var ArkProtocol = class extends BaseProtocols_default {
825
826
  const arkResponse = chunk.response;
826
827
  const text = arkResponse.choices?.[0]?.message?.content || "";
827
828
  const reasoningText = arkResponse?.choices?.[0]?.message?.reasoningContent || "";
828
- return {
829
- ...chunk.message,
830
- content: [
831
- ...chunk.message.content,
832
- {
829
+ const newContent = [...chunk.message.content];
830
+ if (reasoningText) {
831
+ const lastReasoningIndex = findLastIndex(
832
+ newContent,
833
+ (c) => c.type === "reasoning"
834
+ );
835
+ if (lastReasoningIndex !== -1) {
836
+ const prev = newContent[lastReasoningIndex];
837
+ newContent[lastReasoningIndex] = {
838
+ ...prev,
839
+ text: prev.text + reasoningText
840
+ };
841
+ } else {
842
+ newContent.push({
833
843
  type: "reasoning",
834
844
  text: reasoningText
835
- },
836
- {
845
+ });
846
+ }
847
+ }
848
+ if (text) {
849
+ const lastTextIndex = findLastIndex(newContent, (c) => c.type === "text");
850
+ if (lastTextIndex !== -1) {
851
+ const prev = newContent[lastTextIndex];
852
+ newContent[lastTextIndex] = {
853
+ ...prev,
854
+ text: prev.text + text
855
+ };
856
+ } else {
857
+ newContent.push({
837
858
  type: "text",
838
859
  text
839
- }
840
- ]
860
+ });
861
+ }
862
+ }
863
+ return {
864
+ ...chunk.message,
865
+ content: newContent
841
866
  };
842
867
  }
843
868
  // 修改generateRequestBody方法,确保包含defaultParams参数
@@ -1327,7 +1352,7 @@ var AdkPlugin = class extends ChatkitPlugin {
1327
1352
  // response: chunk,
1328
1353
  // }),
1329
1354
  // ),
1330
- messages: data.events.reduce((prev, cur) => {
1355
+ messages: data?.events?.reduce((prev, cur) => {
1331
1356
  if (prev.length === 0 || cur.author === "user" || last(prev)?.role !== (cur.author === "user" ? "user" : "assistant")) {
1332
1357
  prev.push(
1333
1358
  this.adkProtocol.parseChunk({
@@ -1443,36 +1468,41 @@ async function fetchChatCompletion({
1443
1468
  storeManager
1444
1469
  }) {
1445
1470
  try {
1471
+ let currentMessage = storeManager.getMessage(
1472
+ message.threadId,
1473
+ message.id
1474
+ ) || message;
1475
+ const throttledOnResponse = throttle(
1476
+ (msg) => onResponse(msg),
1477
+ 500,
1478
+ { leading: true, trailing: true }
1479
+ );
1446
1480
  const onChunk = ({
1447
1481
  response
1448
1482
  }) => {
1449
- const _message = storeManager.getMessage(
1450
- message.threadId,
1451
- message.id
1452
- );
1453
1483
  const updatedMessage = protocolAdaptor.parseChunk({
1454
- message: _message,
1484
+ message: currentMessage,
1455
1485
  response
1456
1486
  });
1487
+ currentMessage = updatedMessage;
1457
1488
  if (updatedMessage.status === "error" /* ERROR */) {
1489
+ throttledOnResponse.cancel();
1458
1490
  onResponse({ ...updatedMessage, status: "error" /* ERROR */ });
1459
1491
  return;
1460
1492
  }
1461
- onResponse({ ...updatedMessage, status: "pending" /* PENDING */ });
1493
+ throttledOnResponse({ ...updatedMessage, status: "pending" /* PENDING */ });
1462
1494
  };
1463
1495
  try {
1464
1496
  if (abortController?.signal) {
1465
1497
  abortController.signal.addEventListener("abort", () => {
1466
- const _message = storeManager.getMessage(
1467
- message.threadId,
1468
- message.id
1469
- );
1470
- onResponse({
1471
- ..._message,
1498
+ throttledOnResponse.cancel();
1499
+ currentMessage = {
1500
+ ...currentMessage,
1472
1501
  sending: false,
1473
1502
  status: "aborted" /* ABORTED */,
1474
1503
  endedAt: dayjs().unix()
1475
- });
1504
+ };
1505
+ onResponse(currentMessage);
1476
1506
  });
1477
1507
  }
1478
1508
  await completions({
@@ -1481,25 +1511,27 @@ async function fetchChatCompletion({
1481
1511
  onChunk
1482
1512
  });
1483
1513
  } catch (error) {
1514
+ throttledOnResponse.cancel();
1484
1515
  console.log(error);
1485
- const _message = storeManager.getMessage(
1486
- message.threadId,
1487
- message.id
1488
- );
1489
- _message.status = "error" /* ERROR */;
1490
- _message.sending = false;
1491
- _message.endedAt = Date.now();
1492
- _message.error = {
1493
- message: error.message || "\u672A\u77E5\u9519\u8BEF",
1494
- extra: error.stack
1516
+ currentMessage = {
1517
+ ...currentMessage,
1518
+ status: "error" /* ERROR */,
1519
+ sending: false,
1520
+ endedAt: Date.now(),
1521
+ error: {
1522
+ message: error.message || "\u672A\u77E5\u9519\u8BEF",
1523
+ extra: error.stack
1524
+ }
1495
1525
  };
1496
- onResponse(_message);
1526
+ onResponse(currentMessage);
1497
1527
  return;
1498
1528
  }
1529
+ throttledOnResponse.cancel();
1499
1530
  onResponse({
1500
- id: message.id,
1501
- threadId: message.threadId,
1502
- status: message.status !== "error" /* ERROR */ ? "success" /* SUCCESS */ : "error" /* ERROR */,
1531
+ ...currentMessage,
1532
+ id: currentMessage.id,
1533
+ threadId: currentMessage.threadId,
1534
+ status: currentMessage.status !== "error" /* ERROR */ ? "success" /* SUCCESS */ : "error" /* ERROR */,
1503
1535
  endedAt: Date.now()
1504
1536
  });
1505
1537
  } catch (error) {
@@ -1717,6 +1749,7 @@ var ChatController = class {
1717
1749
  dynamicParams: hookContext.dynamicParams,
1718
1750
  requestOptions: hookContext.extraRequestOptions
1719
1751
  };
1752
+ const storeManager = this.store;
1720
1753
  try {
1721
1754
  const protocolAdaptor = getProtocolAdaptor(this.protocolOptions);
1722
1755
  const requestBody = protocolAdaptor.generateRequestBody({
@@ -1726,7 +1759,7 @@ var ChatController = class {
1726
1759
  thread,
1727
1760
  contextMessages: allMessages
1728
1761
  });
1729
- this.store.addMessage(threadId, userMessage);
1762
+ storeManager.addMessage(threadId, userMessage);
1730
1763
  assistantMessage = generateModelMessage({
1731
1764
  threadId
1732
1765
  });
@@ -1745,13 +1778,13 @@ var ChatController = class {
1745
1778
  pluginManager: this.pluginManager,
1746
1779
  completions: this.requestOptions.completions,
1747
1780
  protocolAdaptor,
1748
- storeManager: this.store,
1781
+ storeManager,
1749
1782
  onResponse: async (updatedMessage) => {
1750
1783
  if (updatedMessage.status === "pending" /* PENDING */ && !updatedMessage.createdAt) {
1751
1784
  updatedMessage.createdAt = Date.now();
1752
1785
  }
1753
1786
  this.pluginManager.hooks.message.received.callAsync(updatedMessage);
1754
- this.store.updateMessage(threadId, updatedMessage.id, {
1787
+ storeManager.updateMessage(threadId, updatedMessage.id, {
1755
1788
  updatedMessage,
1756
1789
  partial: true
1757
1790
  });
@@ -1760,8 +1793,8 @@ var ChatController = class {
1760
1793
  } catch (error) {
1761
1794
  onErrorFromProps?.(error);
1762
1795
  } finally {
1763
- this.store.setSending(threadId, false);
1764
- this.store.setLoading(threadId, false);
1796
+ storeManager.setSending(threadId, false);
1797
+ storeManager.setLoading(threadId, false);
1765
1798
  return;
1766
1799
  }
1767
1800
  }