Skip to content

Enhanced ChatGPT Clone: Features Agents, MCP, Skills, DeepSeek, Anthropic, AWS, OpenAI, Responses API, Azure, Groq, o1, GPT-5, Mistral, OpenRouter, Vertex AI, Gemini, Artifacts, AI model switching, message search, Code Interpreter, langchain, DALL-E-3, OpenAPI Actions, Functions, Secure Multi-User Auth, Presets, open-source for self-hosting. Active

🛠️ refactor: Improve Logging and Error Handling in `ToolService` and `useSSE` (#2922)

Danny Avila08d6bea35997
diff --git a/api/app/clients/ChatGPTClient.js b/api/app/clients/ChatGPTClient.js
index d218849513..0a7f6fc7d8 100644
--- a/api/app/clients/ChatGPTClient.js
+++ b/api/app/clients/ChatGPTClient.js
@@ -438,9 +438,17 @@ class ChatGPTClient extends BaseClient {
if (message.eventType === 'text-generation' && message.text) {
onTokenProgress(message.text);
- } else if (message.eventType === 'stream-end' && message.response) {
+ reply += message.text;
+ }
+ /*
+ Cohere API Chinese Unicode character replacement hotfix.
+ Should be un-commented when the following issue is resolved:
+ https://github.com/cohere-ai/cohere-typescript/issues/151
+
+ else if (message.eventType === 'stream-end' && message.response) {
reply = message.response.text;
}
+ */
}
return reply;
diff --git a/api/config/parsers.js b/api/config/parsers.js
index 16c85cba4f..7c04a17cae 100644
--- a/api/config/parsers.js
+++ b/api/config/parsers.js
@@ -27,26 +27,25 @@ function getMatchingSensitivePatterns(valueStr) {
}
/**
- * Redacts sensitive information from a console message.
- *
+ * Redacts sensitive information from a console message and trims it to a specified length if provided.
* @param {string} str - The console message to be redacted.
- * @returns {string} - The redacted console message.
+ * @param {number} [trimLength] - The optional length at which to trim the redacted message.
+ * @returns {string} - The redacted and optionally trimmed console message.
*/
-function redactMessage(str) {
+function redactMessage(str, trimLength) {
if (!str) {
return '';
}
const patterns = getMatchingSensitivePatterns(str);
-
- if (patterns.length === 0) {
- return str;
- }
-
patterns.forEach((pattern) => {
str = str.replace(pattern, '$1[REDACTED]');
});
+ if (trimLength !== undefined && str.length > trimLength) {
+ return `${str.substring(0, trimLength)}...`;
+ }
+
return str;
}
diff --git a/api/server/services/ToolService.js b/api/server/services/ToolService.js
index 149afc5df0..524797d062 100644
--- a/api/server/services/ToolService.js
+++ b/api/server/services/ToolService.js
@@ -340,29 +340,26 @@ async function processRequiredActions(client, requiredActions) {
currentAction.toolInput = currentAction.toolInput.input;
}
+ const handleToolError = (error) => {
+ logger.error(
+ `tool_call_id: ${currentAction.toolCallId} | Error processing tool ${currentAction.tool}`,
+ error,
+ );
+ return {
+ tool_call_id: currentAction.toolCallId,
+ output: `Error processing tool ${currentAction.tool}: ${redactMessage(error.message, 256)}`,
+ };
+ };
+
try {
const promise = tool
._call(currentAction.toolInput)
.then(handleToolOutput)
- .catch((error) => {
- logger.error(`Error processing tool ${currentAction.tool}`, error);
- return {
- tool_call_id: currentAction.toolCallId,
- output: `Error processing tool ${currentAction.tool}: ${redactMessage(error.message)}`,
- };
- });
+ .catch(handleToolError);
promises.push(promise);
} catch (error) {
- logger.error(
- `tool_call_id: ${currentAction.toolCallId} | Error processing tool ${currentAction.tool}`,
- error,
- );
- promises.push(
- Promise.resolve({
- tool_call_id: currentAction.toolCallId,
- error: error.message,
- }),
- );
+ const toolOutputError = handleToolError(error);
+ promises.push(Promise.resolve(toolOutputError));
}
}
diff --git a/client/src/hooks/SSE/useSSE.ts b/client/src/hooks/SSE/useSSE.ts
index 4b821ba403..02cc63020d 100644
--- a/client/src/hooks/SSE/useSSE.ts
+++ b/client/src/hooks/SSE/useSSE.ts
@@ -367,7 +367,7 @@ export default function useSSE(submission: TSubmission | null, index = 0) {
if (!data) {
const convoId = conversationId ?? v4();
const errorResponse = parseErrorResponse({
- text: 'Error connecting to server',
+ text: 'Error connecting to server, try refreshing the page.',
...submission,
conversationId: convoId,
});