codicent-app-sdk 0.3.71 → 0.3.72
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/dist/cjs/hooks/useChat.d.ts +1 -0
- package/dist/cjs/hooks/useChat.d.ts.map +1 -1
- package/dist/cjs/hooks/useChat.js +1 -1
- package/dist/esm/hooks/useChat.d.ts +1 -0
- package/dist/esm/hooks/useChat.d.ts.map +1 -1
- package/dist/esm/hooks/useChat.js +1 -1
- package/dist/index.d.ts +1 -0
- package/package.json +1 -1
@@ -11,5 +11,6 @@ export declare function useChat(state: AppStateMachine): {
|
|
11
11
|
handleSend: (text: string, retryCount?: number) => void;
|
12
12
|
newChat: () => void;
|
13
13
|
openChat: (id: string) => void;
|
14
|
+
reloadChatHistory: () => Promise<boolean>;
|
14
15
|
};
|
15
16
|
//# sourceMappingURL=useChat.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useChat.d.ts","sourceRoot":"","sources":["../../../src/hooks/useChat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAO,MAAM,UAAU,CAAC;AAOhD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,eAAe;;;
|
1
|
+
{"version":3,"file":"useChat.d.ts","sourceRoot":"","sources":["../../../src/hooks/useChat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAO,MAAM,UAAU,CAAC;AAOhD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,eAAe;;;uBAmDnC,MAAM;;mBA2IR,MAAM;;EA2Bd"}
|
@@ -1 +1 @@
|
|
1
|
-
"use strict";var e=require("react"),t=require("../stores/chatStore.js");require("../utils/MessageContent.js"),require("../node_modules/tinycolor2/esm/tinycolor.js");var
|
1
|
+
"use strict";var e=require("react"),t=require("../stores/chatStore.js");require("../utils/MessageContent.js"),require("../node_modules/tinycolor2/esm/tinycolor.js");var s=require("../utils/logging.js");require("react/jsx-runtime"),require("../_virtual/index.js"),require("../config/index.js"),require("../lib/wavtools/lib/wav_packer.js"),require("../lib/wavtools/lib/analysis/audio_analysis.js"),require("../lib/wavtools/lib/wav_stream_player.js"),require("../lib/wavtools/lib/wav_recorder.js"),exports.useChat=function(r){const[a,o]=e.useState(t.chatStore.getState()),{service:n}=r;e.useEffect((()=>t.chatStore.subscribe(o)),[n]);const i=e.useCallback((async()=>{const e=t.chatStore.getConversationId();if(e){console.log("Reloading chat history to check for missed messages...");try{const r=await n.getChatHistory(e),a=t.chatStore.getMessages();return r.length>a.length?(console.log(`Found ${r.length-a.length} missed messages, updating chat...`),t.chatStore.reset(),r.forEach(((e,s)=>{const r={id:e.id,sender:s%2==0?"user":"bot",content:e.content};t.chatStore.addMessage(r)})),t.chatStore.setConversationId(e),s.default("chat-history-reloaded"),!0):(console.log("No missed messages found"),!1)}catch(e){return console.error("Failed to reload chat history:",e),!1}}return!1}),[n]),c=e.useCallback(((e,r=0)=>{if(""===e.trim())return;const o=`$user-msg-${Date.now()}`,l={id:o,sender:"user",content:e},d=a.messages.length>0?a.messages[a.messages.length-1].id:void 0;t.chatStore.addMessage(l),t.chatStore.setBotTyping(!0),s.default("chat-send");const u=Promise.race([n.chat(e,d),new Promise(((e,t)=>{setTimeout((()=>t(new Error("Request timeout - please try again"))),3e4)}))]).then((e=>{const{id:r,content:a}=e,o={id:r,sender:"bot",content:a,isNew:!0};t.chatStore.setConversationId(r),t.chatStore.addMessage(o),t.chatStore.setBotTyping(!1),s.default("chat-receive"),a.startsWith("Error code: ")&&s.default("chat-error: "+a)})).catch((async a=>{console.error("Chat error details:",{error:a,message:a?.message,name:a?.name,stack:a?.stack,tempId:o,lastMessageId:d,retryCount:r,timestamp:(new Date).toISOString()});if(0===r&&("AbortError"===a?.name||a?.message?.includes("timeout")||a?.message?.includes("Failed to fetch")||a?.message?.includes("NetworkError")))return console.log("Auto-retrying chat request..."),t.chatStore.removeMessage(o),t.chatStore.setBotTyping(!1),void setTimeout((()=>c(e,r+1)),2e3);function l(){let e="An error occurred while sending the message. Please try again.";if(n.onUnauthorized&&a?.toString().includes("401"))return alert("Session expired. Please log in again."),void n.onUnauthorized();"AbortError"===a?.name||a?.message?.includes("timeout")?(e="Request timed out. Please check your connection and try again.",s.default("chat-timeout")):a?.message?.includes("HTTP error: 500")?(e="Server error. Please try again in a moment.",s.default("chat-server-error")):(a?.message?.includes("Failed to fetch")||a?.message?.includes("NetworkError"))&&(e="Network error. Please check your connection and try again.",s.default("chat-network-error")),alert(e),t.chatStore.removeMessage(o),t.chatStore.setBotTyping(!1),s.default("chat-error")}"AbortError"===a?.name||a?.message?.includes("timeout")||a?.message?.includes("Failed to fetch")||a?.message?.includes("NetworkError")||a?.message?.includes("WebSocket")||a?.message?.includes("SignalR")||a?.message?.includes("Connection")?(console.log("Potential SignalR/connection issue detected, checking for missed messages..."),setTimeout((async()=>{if(await i())return t.chatStore.removeMessage(o),t.chatStore.setBotTyping(!1),console.log("Recovered missed message(s) from server"),void s.default("chat-recovered");l()}),2e3)):l()}));t.chatStore.addPendingRequest(o,u)}),[n,a.messages,i]),l=e.useCallback((()=>{t.chatStore.reset(),t.chatStore.clearMessages(),s.default("new-chat")}),[]),d=e.useCallback((e=>{t.chatStore.reset(),t.chatStore.clearMessages(),s.default("open-chat"),n.getChatHistory(e).then((e=>{e.forEach(((e,s)=>{const r={id:e.id,sender:s%2==0?"user":"bot",content:e.content};t.chatStore.addMessage(r)}))}))}),[n]);return{messages:a.messages,isBotTyping:a.isBotTyping,handleSend:c,newChat:l,openChat:d,reloadChatHistory:i}};
|
@@ -11,5 +11,6 @@ export declare function useChat(state: AppStateMachine): {
|
|
11
11
|
handleSend: (text: string, retryCount?: number) => void;
|
12
12
|
newChat: () => void;
|
13
13
|
openChat: (id: string) => void;
|
14
|
+
reloadChatHistory: () => Promise<boolean>;
|
14
15
|
};
|
15
16
|
//# sourceMappingURL=useChat.d.ts.map
|
@@ -1 +1 @@
|
|
1
|
-
{"version":3,"file":"useChat.d.ts","sourceRoot":"","sources":["../../../src/hooks/useChat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAO,MAAM,UAAU,CAAC;AAOhD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,eAAe;;;
|
1
|
+
{"version":3,"file":"useChat.d.ts","sourceRoot":"","sources":["../../../src/hooks/useChat.ts"],"names":[],"mappings":"AAEA,OAAO,EAAE,eAAe,EAAO,MAAM,UAAU,CAAC;AAOhD,MAAM,WAAW,OAAO;IACtB,EAAE,EAAE,MAAM,CAAC;IACX,MAAM,EAAE,MAAM,GAAG,KAAK,CAAC;IACvB,OAAO,EAAE,MAAM,CAAC;IAChB,KAAK,CAAC,EAAE,OAAO,CAAC;CACjB;AAED,wBAAgB,OAAO,CAAC,KAAK,EAAE,eAAe;;;uBAmDnC,MAAM;;mBA2IR,MAAM;;EA2Bd"}
|
@@ -1 +1 @@
|
|
1
|
-
import{useState as e,useEffect as
|
1
|
+
import{useState as e,useEffect as s,useCallback as t}from"react";import{chatStore as o}from"../stores/chatStore.js";import"../utils/MessageContent.js";import"../node_modules/tinycolor2/esm/tinycolor.js";import r from"../utils/logging.js";import"react/jsx-runtime";import"../_virtual/index.js";import"../config/index.js";import"../lib/wavtools/lib/wav_packer.js";import"../lib/wavtools/lib/analysis/audio_analysis.js";import"../lib/wavtools/lib/wav_stream_player.js";import"../lib/wavtools/lib/wav_recorder.js";function n(n){const[a,i]=e(o.getState()),{service:c}=n;s((()=>o.subscribe(i)),[c]);const d=t((async()=>{const e=o.getConversationId();if(e){console.log("Reloading chat history to check for missed messages...");try{const s=await c.getChatHistory(e),t=o.getMessages();return s.length>t.length?(console.log(`Found ${s.length-t.length} missed messages, updating chat...`),o.reset(),s.forEach(((e,s)=>{const t={id:e.id,sender:s%2==0?"user":"bot",content:e.content};o.addMessage(t)})),o.setConversationId(e),r("chat-history-reloaded"),!0):(console.log("No missed messages found"),!1)}catch(e){return console.error("Failed to reload chat history:",e),!1}}return!1}),[c]),g=t(((e,s=0)=>{if(""===e.trim())return;const t=`$user-msg-${Date.now()}`,n={id:t,sender:"user",content:e},i=a.messages.length>0?a.messages[a.messages.length-1].id:void 0;o.addMessage(n),o.setBotTyping(!0),r("chat-send");const l=Promise.race([c.chat(e,i),new Promise(((e,s)=>{setTimeout((()=>s(new Error("Request timeout - please try again"))),3e4)}))]).then((e=>{const{id:s,content:t}=e,n={id:s,sender:"bot",content:t,isNew:!0};o.setConversationId(s),o.addMessage(n),o.setBotTyping(!1),r("chat-receive"),t.startsWith("Error code: ")&&r("chat-error: "+t)})).catch((async n=>{console.error("Chat error details:",{error:n,message:n?.message,name:n?.name,stack:n?.stack,tempId:t,lastMessageId:i,retryCount:s,timestamp:(new Date).toISOString()});if(0===s&&("AbortError"===n?.name||n?.message?.includes("timeout")||n?.message?.includes("Failed to fetch")||n?.message?.includes("NetworkError")))return console.log("Auto-retrying chat request..."),o.removeMessage(t),o.setBotTyping(!1),void setTimeout((()=>g(e,s+1)),2e3);function a(){let e="An error occurred while sending the message. Please try again.";if(c.onUnauthorized&&n?.toString().includes("401"))return alert("Session expired. Please log in again."),void c.onUnauthorized();"AbortError"===n?.name||n?.message?.includes("timeout")?(e="Request timed out. Please check your connection and try again.",r("chat-timeout")):n?.message?.includes("HTTP error: 500")?(e="Server error. Please try again in a moment.",r("chat-server-error")):(n?.message?.includes("Failed to fetch")||n?.message?.includes("NetworkError"))&&(e="Network error. Please check your connection and try again.",r("chat-network-error")),alert(e),o.removeMessage(t),o.setBotTyping(!1),r("chat-error")}"AbortError"===n?.name||n?.message?.includes("timeout")||n?.message?.includes("Failed to fetch")||n?.message?.includes("NetworkError")||n?.message?.includes("WebSocket")||n?.message?.includes("SignalR")||n?.message?.includes("Connection")?(console.log("Potential SignalR/connection issue detected, checking for missed messages..."),setTimeout((async()=>{if(await d())return o.removeMessage(t),o.setBotTyping(!1),console.log("Recovered missed message(s) from server"),void r("chat-recovered");a()}),2e3)):a()}));o.addPendingRequest(t,l)}),[c,a.messages,d]),l=t((()=>{o.reset(),o.clearMessages(),r("new-chat")}),[]),m=t((e=>{o.reset(),o.clearMessages(),r("open-chat"),c.getChatHistory(e).then((e=>{e.forEach(((e,s)=>{const t={id:e.id,sender:s%2==0?"user":"bot",content:e.content};o.addMessage(t)}))}))}),[c]);return{messages:a.messages,isBotTyping:a.isBotTyping,handleSend:g,newChat:l,openChat:m,reloadChatHistory:d}}export{n as useChat};
|
package/dist/index.d.ts
CHANGED
@@ -379,6 +379,7 @@ declare function useChat(state: AppStateMachine): {
|
|
379
379
|
handleSend: (text: string, retryCount?: number) => void;
|
380
380
|
newChat: () => void;
|
381
381
|
openChat: (id: string) => void;
|
382
|
+
reloadChatHistory: () => Promise<boolean>;
|
382
383
|
};
|
383
384
|
|
384
385
|
declare const useCodicentState: (state: AppStateMachine) => {
|