@glodon-aiot/chat-app-sdk 0.0.14 → 0.0.15

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/index.esm.js CHANGED
@@ -408020,6 +408020,48 @@ const useGroupedConversations = (conversations)=>{
408020
408020
  return groupedConversations;
408021
408021
  };
408022
408022
 
408023
+ ;// CONCATENATED MODULE: ../open-chat/src/util/conversation-display-name.ts
408024
+ /*
408025
+ * Copyright 2025 coze-dev Authors
408026
+ *
408027
+ * Licensed under the Apache License, Version 2.0 (the "License");
408028
+ * you may not use this file except in compliance with the License.
408029
+ * You may obtain a copy of the License at
408030
+ *
408031
+ * http://www.apache.org/licenses/LICENSE-2.0
408032
+ *
408033
+ * Unless required by applicable law or agreed to in writing, software
408034
+ * distributed under the License is distributed on an "AS IS" BASIS,
408035
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
408036
+ * See the License for the specific language governing permissions and
408037
+ * limitations under the License.
408038
+ */
408039
+ // UUID格式判断:xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx
408040
+ const UUID_REGEX = /^[0-9a-f]{8}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{4}-[0-9a-f]{12}$/i;
408041
+ const isUUID = (str)=>{
408042
+ if (!str) {
408043
+ return false;
408044
+ }
408045
+ return UUID_REGEX.test(str);
408046
+ };
408047
+ /**
408048
+ * 获取会话显示名称
408049
+ * 优先级:title > name(如果不是UUID)> 新创建的会话
408050
+ * @param item 会话项
408051
+ * @returns 显示名称
408052
+ */ const getConversationDisplayName = (item)=>{
408053
+ // 优先使用title
408054
+ if (item.title) {
408055
+ return item.title;
408056
+ }
408057
+ // 如果name存在且不是UUID格式,使用name
408058
+ if (item.name && !isUUID(item.name)) {
408059
+ return item.name;
408060
+ }
408061
+ // 否则显示"新创建的会话"
408062
+ return intl_i18n.t('web_sdk_conversation_default_name');
408063
+ };
408064
+
408023
408065
  ;// CONCATENATED MODULE: ../open-chat/src/components/conversation-list-sider/conversation-item/mobile/operate/index.tsx
408024
408066
  /*
408025
408067
  * Copyright 2025 coze-dev Authors
@@ -408220,7 +408262,7 @@ const MobileConversationItem = (param)=>{
408220
408262
  ellipsis: {
408221
408263
  showTooltip: {
408222
408264
  opts: {
408223
- content: item.title || intl_i18n.t('web_sdk_conversation_default_name'),
408265
+ content: getConversationDisplayName(item),
408224
408266
  style: {
408225
408267
  wordBreak: 'break-all'
408226
408268
  },
@@ -408229,7 +408271,7 @@ const MobileConversationItem = (param)=>{
408229
408271
  }
408230
408272
  }
408231
408273
  },
408232
- children: item.title || intl_i18n.t('web_sdk_conversation_default_name')
408274
+ children: getConversationDisplayName(item)
408233
408275
  })
408234
408276
  })
408235
408277
  }),
@@ -408388,7 +408430,7 @@ const PcConversationItem = (param)=>{
408388
408430
  ellipsis: {
408389
408431
  showTooltip: {
408390
408432
  opts: {
408391
- content: item.title,
408433
+ content: getConversationDisplayName(item),
408392
408434
  style: {
408393
408435
  wordBreak: 'break-all'
408394
408436
  },
@@ -408397,7 +408439,7 @@ const PcConversationItem = (param)=>{
408397
408439
  }
408398
408440
  }
408399
408441
  },
408400
- children: item.title || intl_i18n.t('web_sdk_conversation_default_name')
408442
+ children: getConversationDisplayName(item)
408401
408443
  }),
408402
408444
  /*#__PURE__*/ (0,jsx_runtime.jsx)("div", {
408403
408445
  className: conversation_item_pc_index_module["conversation-operate-wrapper"],
@@ -409314,9 +409356,20 @@ const createChatStore = (chatConfig, userInfo)=>{
409314
409356
  }));
409315
409357
  },
409316
409358
  updateCurrentConversationNameByMessage: async (name)=>{
409359
+ var _chatConfigRef_current;
409317
409360
  const { currentConversationInfo, updateCurrentConversationInfo, updateConversations, cozeApi } = get();
409318
- // 如果没有 conversationInfo 或已有名称,直接返回
409319
- if (!currentConversationInfo || currentConversationInfo.name) {
409361
+ // 如果没有 conversationInfo,直接返回
409362
+ if (!currentConversationInfo) {
409363
+ return Promise.resolve(false);
409364
+ }
409365
+ const isAppType = ((_chatConfigRef_current = chatConfigRef.current) === null || _chatConfigRef_current === void 0 ? void 0 : _chatConfigRef_current.type) === client_ChatType.APP;
409366
+ // 对于App模式,title应该只由chat接口的更新事件来设置,不应该主动PUT请求
409367
+ // 完全禁用App模式下的PUT请求,避免死循环
409368
+ if (isAppType) {
409369
+ return Promise.resolve(false);
409370
+ }
409371
+ // 对于Bot模式,如果已有名称,直接返回
409372
+ if (currentConversationInfo.name) {
409320
409373
  return Promise.resolve(false);
409321
409374
  }
409322
409375
  // 如果 conversationId 不存在或为空,说明 conversation 还未创建成功,直接返回
@@ -409325,13 +409378,9 @@ const createChatStore = (chatConfig, userInfo)=>{
409325
409378
  return Promise.resolve(false);
409326
409379
  }
409327
409380
  try {
409328
- var _chatConfigRef_current;
409329
- const isAppType = ((_chatConfigRef_current = chatConfigRef.current) === null || _chatConfigRef_current === void 0 ? void 0 : _chatConfigRef_current.type) === client_ChatType.APP;
409330
- const url = isAppType ? `/v1/workflow/conversations/${currentConversationInfo.id}` : `/v1/conversations/${currentConversationInfo.id}`;
409331
- // App 模式使用 title 字段,Bot 模式使用 name 字段
409332
- const requestBody = isAppType ? {
409333
- title: name
409334
- } : {
409381
+ const url = `/v1/conversations/${currentConversationInfo.id}`;
409382
+ // Bot 模式使用 name 字段
409383
+ const requestBody = {
409335
409384
  name
409336
409385
  };
409337
409386
  console.log('cozeApi?.put', url);
@@ -409355,7 +409404,24 @@ const createChatStore = (chatConfig, userInfo)=>{
409355
409404
  updateConversations: (conversations, operate)=>{
409356
409405
  set(immer_produce((s)=>{
409357
409406
  if (operate === 'replace') {
409358
- s.conversations = conversations;
409407
+ // 修复:在replace时,保留store中已有的title字段
409408
+ // 因为服务器返回的数据可能不包含最新的title(title是通过chat接口更新事件更新的)
409409
+ // 需要将新数据与store中已有的数据合并,保留store中的title
409410
+ const existingConversationsMap = new Map(s.conversations.map((c)=>[
409411
+ c.id,
409412
+ c
409413
+ ]));
409414
+ s.conversations = conversations.map((newConv)=>{
409415
+ const existingConv = existingConversationsMap.get(newConv.id);
409416
+ if (existingConv && existingConv.title) {
409417
+ // 如果store中已有该会话且有title,保留title
409418
+ return {
409419
+ ...newConv,
409420
+ title: existingConv.title
409421
+ };
409422
+ }
409423
+ return newConv;
409424
+ });
409359
409425
  } else if (operate === 'add') {
409360
409426
  s.conversations = [
409361
409427
  ...s.conversations,
@@ -409372,8 +409438,12 @@ const createChatStore = (chatConfig, userInfo)=>{
409372
409438
  conversations.forEach((conversation)=>{
409373
409439
  const index = s.conversations.findIndex((c)=>c.id === conversation.id);
409374
409440
  if (index !== -1) {
409441
+ // 修复:确保只更新传入的字段,保留原有字段
409442
+ // 先展开原有对象,再展开新对象,确保新对象的字段覆盖原有字段
409443
+ // 但不会丢失原有对象中的其他字段(如其他会话的title)
409444
+ const existingConversation = s.conversations[index];
409375
409445
  s.conversations[index] = {
409376
- ...s.conversations[index],
409446
+ ...existingConversation,
409377
409447
  ...conversation
409378
409448
  };
409379
409449
  }
@@ -413704,30 +413774,41 @@ studioOpenClientReporter.init(studio_open_client_reporter_slardarInstance);
413704
413774
 
413705
413775
 
413706
413776
 
413777
+
413707
413778
  const useUpdateConversationNameByMessage = ()=>{
413708
413779
  const currentConversationNameRef = (0,react.useRef)();
413709
413780
  const { updateCurrentConversationNameByMessage, currentConversationInfo } = context_useChatAppStore(shallow_useShallow((s)=>({
413710
413781
  updateCurrentConversationNameByMessage: s.updateCurrentConversationNameByMessage,
413711
413782
  currentConversationInfo: s.currentConversationInfo
413712
413783
  })));
413784
+ const { chatConfig: { type: chatType } } = context_useChatAppProps();
413785
+ const isAppType = chatType === client_ChatType.APP;
413713
413786
  const { useMessagesStore } = use_chat_area_context_useChatAreaStoreSet();
413714
413787
  const messages = useMessagesStore((s)=>s.messages, lodash_es_isEqual);
413715
413788
  (0,react.useEffect)(()=>{
413716
- currentConversationNameRef.current = currentConversationInfo === null || currentConversationInfo === void 0 ? void 0 : currentConversationInfo.name;
413789
+ // 对于App模式,检查title;对于Bot模式,检查name
413790
+ if (isAppType) {
413791
+ currentConversationNameRef.current = currentConversationInfo === null || currentConversationInfo === void 0 ? void 0 : currentConversationInfo.title;
413792
+ } else {
413793
+ currentConversationNameRef.current = currentConversationInfo === null || currentConversationInfo === void 0 ? void 0 : currentConversationInfo.name;
413794
+ }
413717
413795
  }, [
413718
- currentConversationInfo
413796
+ currentConversationInfo,
413797
+ isAppType
413719
413798
  ]);
413720
413799
  (0,react.useEffect)(()=>{
413721
413800
  const message = messages[messages.length - 1];
413722
413801
  const name = message === null || message === void 0 ? void 0 : message.content.slice(0, 100);
413723
413802
  // 确保 conversation 已创建成功(有 id)且还没有名称时才更新
413803
+ // 对于App模式,如果title已经存在(通过chat接口更新事件设置的),不应该再PUT请求
413724
413804
  if (message && !currentConversationNameRef.current && (currentConversationInfo === null || currentConversationInfo === void 0 ? void 0 : currentConversationInfo.id) && currentConversationInfo.id.trim() !== '') {
413725
413805
  updateCurrentConversationNameByMessage(name);
413726
413806
  currentConversationNameRef.current = name;
413727
413807
  }
413728
413808
  }, [
413729
413809
  messages,
413730
- currentConversationInfo
413810
+ currentConversationInfo,
413811
+ updateCurrentConversationNameByMessage
413731
413812
  ]);
413732
413813
  };
413733
413814
 
@@ -420134,35 +420215,43 @@ class MessageParser {
420134
420215
  const currentConversation = refCurrentConversationInfo.current;
420135
420216
  const updateConversationsFn = refUpdateConversations.current;
420136
420217
  const updateCurrentConversationInfoFn = refUpdateCurrentConversationInfo.current;
420137
- const conversationsList = refConversations.current;
420138
420218
  const { title } = updates;
420139
420219
  if (title !== undefined) {
420140
- // 找到要更新的会话,保留其原有字段
420141
- const existingConversation = conversationsList.find((c)=>c.id === conversation_id);
420142
420220
  // 更新会话列表中的会话名称
420143
420221
  // 注意:PC 端和 Mobile 端都优先使用 title,如果没有 title 则使用 name
420144
420222
  // 对于 App 模式,需要同时更新 name 和 title
420145
420223
  // 对于 Bot 模式,也需要更新 name 和 title(如果存在)
420146
- const updatedConversation = {
420224
+ // 修复:使用函数式更新,确保从最新的store状态中获取并保留所有字段
420225
+ // 这样可以避免因为ref未及时更新而导致的数据丢失问题
420226
+ const updatedConversationObj = {
420147
420227
  id: conversation_id,
420148
420228
  name: title,
420149
420229
  title,
420150
- updated_at: Math.floor(updated_at / 1000),
420151
- // 保留原有字段(如果存在),否则使用默认值
420152
- created_at: (existingConversation === null || existingConversation === void 0 ? void 0 : existingConversation.created_at) ?? Math.floor(updated_at / 1000),
420153
- meta_data: (existingConversation === null || existingConversation === void 0 ? void 0 : existingConversation.meta_data) ?? {},
420154
- last_section_id: (existingConversation === null || existingConversation === void 0 ? void 0 : existingConversation.last_section_id) ?? ''
420230
+ updated_at: Math.floor(updated_at / 1000)
420155
420231
  };
420156
420232
  updateConversationsFn([
420157
- updatedConversation
420233
+ updatedConversationObj
420158
420234
  ], 'update');
420159
420235
  // 如果当前会话是被更新的会话,同时更新 currentConversationInfo
420160
- if ((currentConversation === null || currentConversation === void 0 ? void 0 : currentConversation.id) === conversation_id) {
420236
+ // 修复:无论ref是否是最新的,都更新currentConversationInfo,确保header能显示title
420237
+ // updateCurrentConversationInfo函数会合并字段,所以即使ref不是最新的,title也会被正确设置
420238
+ // 从conversations列表中查找对应的会话,如果找到说明这个会话存在,可能是当前会话
420239
+ const conversationsList = refConversations.current;
420240
+ const foundConversation = conversationsList.find((c)=>c.id === conversation_id);
420241
+ // 如果ref中的id匹配,或者找到了对应的会话,就更新title
420242
+ // 这样可以确保即使ref不是最新的,也能正确更新title
420243
+ if ((currentConversation === null || currentConversation === void 0 ? void 0 : currentConversation.id) === conversation_id || foundConversation) {
420244
+ // 优先使用ref中的currentConversationInfo,保留其所有字段
420245
+ // 如果ref中没有,使用conversations列表中的信息
420246
+ const baseInfo = currentConversation || foundConversation;
420161
420247
  updateCurrentConversationInfoFn({
420162
- ...currentConversation,
420248
+ ...baseInfo || {},
420249
+ id: conversation_id,
420163
420250
  name: title,
420164
- // @ts-expect-error: title 字段可能不在类型定义中,但实际需要
420165
- title
420251
+ title,
420252
+ // 保留currentConversationInfo特有的字段
420253
+ conversationListVisible: (currentConversation === null || currentConversation === void 0 ? void 0 : currentConversation.conversationListVisible) ?? false,
420254
+ isLargeWidth: (currentConversation === null || currentConversation === void 0 ? void 0 : currentConversation.isLargeWidth) ?? false
420166
420255
  });
420167
420256
  }
420168
420257
  }