@glodon-aiot/chat-app-sdk 0.0.29 → 0.0.30

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
@@ -426760,6 +426760,10 @@ try {
426760
426760
  error;
426761
426761
  }
426762
426762
  /** 按 fileId 存储轮询的 AbortController,删除文件时用于停止轮询 */ const knowledgePollingControllers = new Map();
426763
+ /**
426764
+ * 空会话时并发上传多个文件共用的「创建会话」Promise。
426765
+ * 保证同一时刻只有一次创建会话请求,后续上传复用同一会话,避免多文件一次选择时创建多个会话。
426766
+ */ let pendingCreateConversationPromise = null;
426763
426767
  /**
426764
426768
  * 取消指定 fileId 的知识库文件状态轮询(会话中删除正在上传的文件时调用)
426765
426769
  */ function cancelKnowledgePolling(fileId) {
@@ -426962,7 +426966,7 @@ const useUploadToKnowledge = (config)=>{
426962
426966
  // 1. Get or create conversation
426963
426967
  let currentConversationId = conversationId || "";
426964
426968
  let currentSectionId = sectionId || "";
426965
- // If no conversation, create one
426969
+ // If no conversation, create one (or reuse in-flight creation when multiple files are uploaded at once)
426966
426970
  if (!currentConversationId) {
426967
426971
  var _chatConfig_appInfo5, _chatConfig_appInfo6, _chatConfig_appInfo7, _chatConfig_appInfo8, _chatConfig_auth3, _chatConfig_auth4;
426968
426972
  const finalAppId = appId || ((_chatConfig_appInfo5 = chatConfig.appInfo) === null || _chatConfig_appInfo5 === void 0 ? void 0 : _chatConfig_appInfo5.appId) || chatConfig.bot_id || "";
@@ -426976,100 +426980,95 @@ const useUploadToKnowledge = (config)=>{
426976
426980
  const finalConversationName = conversationName || ((_chatConfig_appInfo8 = chatConfig.appInfo) === null || _chatConfig_appInfo8 === void 0 ? void 0 : _chatConfig_appInfo8.conversationName);
426977
426981
  const finalConnectorId = connectorId || ((_chatConfig_auth3 = chatConfig.auth) === null || _chatConfig_auth3 === void 0 ? void 0 : _chatConfig_auth3.connectorId);
426978
426982
  const finalToken = token || ((_chatConfig_auth4 = chatConfig.auth) === null || _chatConfig_auth4 === void 0 ? void 0 : _chatConfig_auth4.token) || "";
426979
- // Try to use createConversationForMessage if available
426980
- if (use_upload_to_knowledge_createConversationForMessage && cozeApiSdk) try {
426981
- const conversationResult = await use_upload_to_knowledge_createConversationForMessage(cozeApiSdk, {
426982
- projectType,
426983
- projectId: finalAppId,
426984
- workflowId: finalWorkflowId,
426985
- mode: finalDraftMode ? "draft" : "release",
426986
- conversationName: finalConversationName,
426987
- connectorId: finalConnectorId
426988
- });
426989
- currentConversationId = conversationResult.conversationId;
426990
- currentSectionId = conversationResult.sectionId;
426991
- } catch (error) {
426992
- console.warn("Failed to create conversation using createConversationForMessage:", error);
426993
- // Fall through to direct API call
426994
- }
426995
- // If createConversationForMessage failed or is not available, use direct API call
426996
- // Note: finalApiUrl is defined later in the code, so we use apiUrl here
426997
- if (!currentConversationId && apiUrl && finalToken) try {
426998
- let createUrl = "";
426999
- let createPayload = {};
427000
- if (projectType === "bot") {
427001
- createUrl = `${apiUrl}/v1/conversations/create`;
427002
- createPayload = {
427003
- connector_id: finalConnectorId
427004
- };
427005
- } else {
427006
- createUrl = `${apiUrl}/v1/workflow/conversation/create`;
427007
- createPayload = {
427008
- app_id: finalAppId,
427009
- draft_mode: finalDraftMode || false,
427010
- workflow_id: finalWorkflowId || "",
427011
- connector_id: finalConnectorId
427012
- };
427013
- // Only add get_or_create if conversationName is provided
427014
- if (finalConversationName && finalConversationName.trim() !== "") {
427015
- createPayload.get_or_create = true;
427016
- createPayload.conversation_name = finalConversationName;
427017
- }
427018
- }
427019
- const createResponse = await fetch(createUrl, {
427020
- method: "POST",
427021
- headers: {
427022
- "Content-Type": "application/json",
427023
- Authorization: `Bearer ${finalToken}`,
427024
- "Accept-Language": "en"
427025
- },
427026
- body: JSON.stringify(createPayload)
427027
- });
427028
- const createResult = await createResponse.json();
427029
- if (createResult.code === 0 && createResult.data) {
427030
- currentConversationId = createResult.data.id || "";
427031
- currentSectionId = createResult.data.last_section_id || "";
427032
- } else {
427033
- console.error("Failed to create conversation:", createResult.msg || "Unknown error");
427034
- return null;
427035
- }
427036
- } catch (error1) {
427037
- console.error("Error creating conversation:", error1);
427038
- return null;
427039
- }
427040
- // Update conversation ID in chat area
427041
- if (currentConversationId) {
427042
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- useGlobalInitStore type may vary by context
427043
- const globalStore = useGlobalInitStore;
427044
- if (globalStore && typeof globalStore.getState === "function") {
427045
- const { setConversationId } = globalStore.getState();
427046
- setConversationId(currentConversationId);
427047
- }
427048
- if (currentSectionId && setLatestSectionId) setLatestSectionId(currentSectionId);
427049
- // Also update currentConversationInfo in store so text messages can use it
427050
- // First try to use the callback from config (preferred method)
427051
- if (config === null || config === void 0 ? void 0 : config.updateCurrentConversationInfo) try {
427052
- // Delay updating currentConversationInfo to ensure file message is displayed first
427053
- // This prevents chatarea from refreshing before the file message is added to the store
427054
- // Use requestAnimationFrame + setTimeout to ensure file message is fully rendered and saved
427055
- const DELAY_MS = 200; // Increased delay to ensure file message is fully saved to store
427056
- requestAnimationFrame(()=>{
427057
- setTimeout(()=>{
427058
- if (config.updateCurrentConversationInfo) config.updateCurrentConversationInfo({
427059
- id: currentConversationId,
427060
- last_section_id: currentSectionId
427061
- });
427062
- }, DELAY_MS);
426983
+ const runCreateConversation = async ()=>{
426984
+ let newConversationId = "";
426985
+ let newSectionId = "";
426986
+ if (use_upload_to_knowledge_createConversationForMessage && cozeApiSdk) try {
426987
+ const conversationResult = await use_upload_to_knowledge_createConversationForMessage(cozeApiSdk, {
426988
+ projectType,
426989
+ projectId: finalAppId,
426990
+ workflowId: finalWorkflowId,
426991
+ mode: finalDraftMode ? "draft" : "release",
426992
+ conversationName: finalConversationName,
426993
+ connectorId: finalConnectorId
427063
426994
  });
426995
+ newConversationId = conversationResult.conversationId;
426996
+ newSectionId = conversationResult.sectionId;
427064
426997
  } catch (error) {
427065
- console.warn("Failed to schedule updateCurrentConversationInfo:", error);
426998
+ console.warn("Failed to create conversation using createConversationForMessage:", error);
427066
426999
  }
427067
- else {
427068
- // Fallback: try to use dynamically imported hook (may not work in callbacks)
427069
- // Note: This is a fallback that may not work in callbacks
427070
- // eslint-disable-next-line @typescript-eslint/no-explicit-any -- fallback hook may not be available
427071
- const storeHook = use_upload_to_knowledge_useChatAppStore;
427000
+ if (!newConversationId && apiUrl && finalToken) try {
427001
+ let createUrl = "";
427002
+ let createPayload = {};
427003
+ if (projectType === "bot") {
427004
+ createUrl = `${apiUrl}/v1/conversations/create`;
427005
+ createPayload = {
427006
+ connector_id: finalConnectorId
427007
+ };
427008
+ } else {
427009
+ createUrl = `${apiUrl}/v1/workflow/conversation/create`;
427010
+ createPayload = {
427011
+ app_id: finalAppId,
427012
+ draft_mode: finalDraftMode || false,
427013
+ workflow_id: finalWorkflowId || "",
427014
+ connector_id: finalConnectorId
427015
+ };
427016
+ if (finalConversationName && finalConversationName.trim() !== "") {
427017
+ createPayload.get_or_create = true;
427018
+ createPayload.conversation_name = finalConversationName;
427019
+ }
427020
+ }
427021
+ const createResponse = await fetch(createUrl, {
427022
+ method: "POST",
427023
+ headers: {
427024
+ "Content-Type": "application/json",
427025
+ Authorization: `Bearer ${finalToken}`,
427026
+ "Accept-Language": "en"
427027
+ },
427028
+ body: JSON.stringify(createPayload)
427029
+ });
427030
+ const createResult = await createResponse.json();
427031
+ if (createResult.code === 0 && createResult.data) {
427032
+ newConversationId = createResult.data.id || "";
427033
+ newSectionId = createResult.data.last_section_id || "";
427034
+ } else console.error("Failed to create conversation:", createResult.msg || "Unknown error");
427035
+ } catch (error1) {
427036
+ console.error("Error creating conversation:", error1);
427072
427037
  }
427038
+ if (newConversationId) {
427039
+ if (useGlobalInitStore && typeof useGlobalInitStore.getState === "function") {
427040
+ const { setConversationId } = useGlobalInitStore.getState();
427041
+ setConversationId(newConversationId);
427042
+ }
427043
+ if (newSectionId && setLatestSectionId) setLatestSectionId(newSectionId);
427044
+ if (config === null || config === void 0 ? void 0 : config.updateCurrentConversationInfo) try {
427045
+ const DELAY_MS = 200;
427046
+ requestAnimationFrame(()=>{
427047
+ setTimeout(()=>{
427048
+ if (config.updateCurrentConversationInfo) config.updateCurrentConversationInfo({
427049
+ id: newConversationId,
427050
+ last_section_id: newSectionId
427051
+ });
427052
+ }, DELAY_MS);
427053
+ });
427054
+ } catch (err) {
427055
+ console.warn("Failed to schedule updateCurrentConversationInfo:", err);
427056
+ }
427057
+ }
427058
+ return {
427059
+ conversationId: newConversationId,
427060
+ sectionId: newSectionId
427061
+ };
427062
+ };
427063
+ if (!pendingCreateConversationPromise) pendingCreateConversationPromise = runCreateConversation().finally(()=>{
427064
+ pendingCreateConversationPromise = null;
427065
+ });
427066
+ try {
427067
+ const result = await pendingCreateConversationPromise;
427068
+ currentConversationId = result.conversationId;
427069
+ currentSectionId = result.sectionId;
427070
+ } catch (error) {
427071
+ console.warn("Create conversation promise rejected:", error);
427073
427072
  }
427074
427073
  }
427075
427074
  if (!currentConversationId || !currentSectionId) {