@eka-care/medassist-core 1.0.66 → 1.0.68
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/Synapse.d.ts +0 -1
- package/dist/connection/ConnectionFactory.d.ts +0 -1
- package/dist/connection/SSE.d.ts +0 -1
- package/dist/connection/Websocket.d.ts +0 -1
- package/dist/constants/index.d.ts +0 -1
- package/dist/constants/types.d.ts +0 -1
- package/dist/conversation.d.ts +0 -1
- package/dist/esm/Synapse.js +612 -0
- package/dist/esm/connection/ConnectionFactory.js +27 -0
- package/dist/esm/connection/SSE.js +212 -0
- package/dist/esm/connection/Websocket.js +178 -0
- package/dist/esm/constants/index.js +25 -0
- package/dist/esm/constants/types.js +1 -0
- package/dist/esm/conversation.js +7 -0
- package/dist/esm/events/Events.js +41 -0
- package/dist/esm/events/Incoming.js +1 -0
- package/dist/esm/events/Outgoing.js +1 -0
- package/dist/esm/events/index.js +2 -0
- package/dist/esm/events/types.js +5 -0
- package/dist/esm/index.js +34 -0
- package/dist/esm/internal/Api/BaseResource.js +50 -0
- package/dist/esm/internal/Api/HttpClient.js +131 -0
- package/dist/esm/internal/Api/types.js +1 -0
- package/dist/esm/internal/Error/Error.js +229 -0
- package/dist/esm/internal/Error/types.js +9 -0
- package/dist/esm/internal/connection/BaseConnection.js +134 -0
- package/dist/esm/internal/connection/types.js +17 -0
- package/dist/esm/internal/events/EventEmitter.js +26 -0
- package/dist/esm/internal/store/index.js +5 -0
- package/dist/esm/media/audio/Audio.copy.js +363 -0
- package/dist/esm/media/audio/Audio.js +310 -0
- package/dist/esm/media/audio/types.js +13 -0
- package/dist/esm/media/file/File.js +159 -0
- package/dist/esm/messages/MessageManager.js +476 -0
- package/dist/esm/messages/types.js +35 -0
- package/dist/esm/resources/config/Config.js +11 -0
- package/dist/esm/resources/feedback/Feedback.js +9 -0
- package/dist/esm/resources/feedback/types.js +7 -0
- package/dist/esm/resources/index.js +152 -0
- package/dist/esm/resources/session/Session.js +44 -0
- package/dist/esm/resources/session/types.js +5 -0
- package/dist/esm/resources/toolCall/ToolCall.js +12 -0
- package/dist/esm/resources/toolCall/types.js +34 -0
- package/dist/esm/resources/types.js +4 -0
- package/dist/esm/resources/voice/VoiceResource.js +14 -0
- package/dist/esm/resources/voice/types.js +1 -0
- package/dist/esm/types/index.js +8 -0
- package/dist/esm/utils/Error.js +110 -0
- package/dist/esm/voice/VoiceAgent.js +305 -0
- package/dist/esm/voice/VoiceAudioAnalyser.js +32 -0
- package/dist/esm/voice/index.js +1 -0
- package/dist/esm/voice/types.js +15 -0
- package/dist/events/Events.d.ts +0 -1
- package/dist/events/Incoming.d.ts +0 -1
- package/dist/events/Outgoing.d.ts +0 -1
- package/dist/events/index.d.ts +0 -1
- package/dist/events/types.d.ts +0 -1
- package/dist/index.d.ts +0 -1
- package/dist/internal/Api/BaseResource.d.ts +0 -1
- package/dist/internal/Api/HttpClient.d.ts +0 -1
- package/dist/internal/Api/types.d.ts +0 -1
- package/dist/internal/Error/Error.d.ts +0 -1
- package/dist/internal/Error/types.d.ts +0 -1
- package/dist/internal/connection/BaseConnection.d.ts +0 -1
- package/dist/internal/connection/types.d.ts +0 -1
- package/dist/internal/events/EventEmitter.d.ts +0 -1
- package/dist/internal/store/index.d.ts +0 -1
- package/dist/media/audio/Audio.copy.d.ts +0 -1
- package/dist/media/audio/Audio.d.ts +0 -1
- package/dist/media/audio/types.d.ts +0 -1
- package/dist/media/file/File.d.ts +0 -1
- package/dist/messages/MessageManager.d.ts +0 -1
- package/dist/messages/types.d.ts +0 -1
- package/dist/resources/config/Config.d.ts +0 -1
- package/dist/resources/feedback/Feedback.d.ts +0 -1
- package/dist/resources/feedback/types.d.ts +0 -1
- package/dist/resources/index.d.ts +0 -1
- package/dist/resources/session/Session.d.ts +0 -1
- package/dist/resources/session/types.d.ts +0 -1
- package/dist/resources/toolCall/ToolCall.d.ts +0 -1
- package/dist/resources/toolCall/types.d.ts +0 -1
- package/dist/resources/types.d.ts +0 -1
- package/dist/resources/voice/VoiceResource.d.ts +0 -1
- package/dist/resources/voice/types.d.ts +0 -1
- package/dist/types/index.d.ts +0 -1
- package/dist/utils/Error.d.ts +0 -1
- package/dist/voice/VoiceAgent.d.ts +0 -1
- package/dist/voice/VoiceAudioAnalyser.d.ts +0 -1
- package/dist/voice/index.d.ts +0 -1
- package/dist/voice/types.d.ts +0 -1
- package/package.json +1 -1
- package/dist/Synapse.d.ts.map +0 -1
- package/dist/auth/constants.d.ts +0 -12
- package/dist/auth/constants.d.ts.map +0 -1
- package/dist/auth/constants.js +0 -10
- package/dist/auth/index.d.ts +0 -3
- package/dist/auth/index.d.ts.map +0 -1
- package/dist/auth/index.js +0 -18
- package/dist/auth/session.d.ts +0 -5
- package/dist/auth/session.d.ts.map +0 -1
- package/dist/auth/session.js +0 -36
- package/dist/connection/ConnectionFactory.d.ts.map +0 -1
- package/dist/connection/SSE.d.ts.map +0 -1
- package/dist/connection/Websocket.d.ts.map +0 -1
- package/dist/constants/index.d.ts.map +0 -1
- package/dist/constants/types.d.ts.map +0 -1
- package/dist/conversation.d.ts.map +0 -1
- package/dist/events/Events.d.ts.map +0 -1
- package/dist/events/Incoming.d.ts.map +0 -1
- package/dist/events/Outgoing.d.ts.map +0 -1
- package/dist/events/index.d.ts.map +0 -1
- package/dist/events/types.d.ts.map +0 -1
- package/dist/index.d.ts.map +0 -1
- package/dist/internal/Api/BaseResource.d.ts.map +0 -1
- package/dist/internal/Api/HttpClient.d.ts.map +0 -1
- package/dist/internal/Api/types.d.ts.map +0 -1
- package/dist/internal/Error/Error.d.ts.map +0 -1
- package/dist/internal/Error/types.d.ts.map +0 -1
- package/dist/internal/connection/BaseConnection.d.ts.map +0 -1
- package/dist/internal/connection/types.d.ts.map +0 -1
- package/dist/internal/events/EventEmitter.d.ts.map +0 -1
- package/dist/internal/store/index.d.ts.map +0 -1
- package/dist/media/audio/Audio.copy.d.ts.map +0 -1
- package/dist/media/audio/Audio.d.ts.map +0 -1
- package/dist/media/audio/types.d.ts.map +0 -1
- package/dist/media/file/File.d.ts.map +0 -1
- package/dist/messages/MessageManager.d.ts.map +0 -1
- package/dist/messages/types.d.ts.map +0 -1
- package/dist/resources/config/Config.d.ts.map +0 -1
- package/dist/resources/feedback/Feedback.d.ts.map +0 -1
- package/dist/resources/feedback/types.d.ts.map +0 -1
- package/dist/resources/index.d.ts.map +0 -1
- package/dist/resources/session/Session.d.ts.map +0 -1
- package/dist/resources/session/types.d.ts.map +0 -1
- package/dist/resources/toolCall/ToolCall.d.ts.map +0 -1
- package/dist/resources/toolCall/types.d.ts.map +0 -1
- package/dist/resources/types.d.ts.map +0 -1
- package/dist/resources/voice/VoiceResource.d.ts.map +0 -1
- package/dist/resources/voice/types.d.ts.map +0 -1
- package/dist/types/index.d.ts.map +0 -1
- package/dist/utils/Error.d.ts.map +0 -1
- package/dist/voice/VoiceAgent.d.ts.map +0 -1
- package/dist/voice/VoiceAudioAnalyser.d.ts.map +0 -1
- package/dist/voice/index.d.ts.map +0 -1
- package/dist/voice/types.d.ts.map +0 -1
|
@@ -0,0 +1,152 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Init class to fetch agent configuration from the platform
|
|
3
|
+
*/
|
|
4
|
+
import { HttpClient } from "../internal/Api/HttpClient";
|
|
5
|
+
import { Session } from "./session/Session";
|
|
6
|
+
import { APIError } from "../internal/Error/Error";
|
|
7
|
+
import { Config } from "./config/Config";
|
|
8
|
+
import { VoiceResource } from "./voice/VoiceResource";
|
|
9
|
+
export class ResourceManager {
|
|
10
|
+
resourceConfig;
|
|
11
|
+
httpClient;
|
|
12
|
+
session;
|
|
13
|
+
config;
|
|
14
|
+
voiceResource;
|
|
15
|
+
constructor(config) {
|
|
16
|
+
this.resourceConfig = config;
|
|
17
|
+
// Set up HTTP client with agentId header for all requests
|
|
18
|
+
this.httpClient = new HttpClient({
|
|
19
|
+
baseUrl: this.resourceConfig.serverUrl,
|
|
20
|
+
headers: {
|
|
21
|
+
"X-agent-id": this.resourceConfig.agentId, // This will be included in ALL requests
|
|
22
|
+
"ngrok-skip-browser-warning": "69420",
|
|
23
|
+
...(this.resourceConfig.authorization && { Authorization: this.resourceConfig.authorization }),
|
|
24
|
+
},
|
|
25
|
+
// authorization: config.authorization,
|
|
26
|
+
});
|
|
27
|
+
this.config = new Config(this.httpClient);
|
|
28
|
+
this.session = new Session(this.httpClient);
|
|
29
|
+
this.voiceResource = new VoiceResource(this.httpClient);
|
|
30
|
+
}
|
|
31
|
+
/**
|
|
32
|
+
* Fetch agent configuration and create session
|
|
33
|
+
*/
|
|
34
|
+
async createSession(userId, context) {
|
|
35
|
+
try {
|
|
36
|
+
// Use SessionsResource to start a session
|
|
37
|
+
// The X-agent-id header is automatically included from httpClient config
|
|
38
|
+
const { profile, intent, user_location, referer, ...extraContext } = context || {};
|
|
39
|
+
const sessionRequest = {
|
|
40
|
+
...(userId && { user_id: userId }),
|
|
41
|
+
...(profile && { user_context: profile }),
|
|
42
|
+
...((intent || Object.keys(extraContext).length > 0) && {
|
|
43
|
+
session_context: {
|
|
44
|
+
...(intent && { intent }),
|
|
45
|
+
...(user_location && { user_location }),
|
|
46
|
+
...extraContext, // Include any extra properties from context
|
|
47
|
+
}
|
|
48
|
+
}),
|
|
49
|
+
...(referer && { referer }),
|
|
50
|
+
};
|
|
51
|
+
const sessionData = await this.session.create(sessionRequest, this.resourceConfig.authToken);
|
|
52
|
+
if (!sessionData?.session_id || !sessionData?.session_token) {
|
|
53
|
+
throw new APIError("Failed to start session.", 500, undefined, undefined);
|
|
54
|
+
}
|
|
55
|
+
return sessionData;
|
|
56
|
+
}
|
|
57
|
+
catch (error) {
|
|
58
|
+
if (error instanceof APIError) {
|
|
59
|
+
throw error;
|
|
60
|
+
}
|
|
61
|
+
throw new APIError("Failed to start session.", 500, undefined, undefined);
|
|
62
|
+
}
|
|
63
|
+
}
|
|
64
|
+
async getAgentConfig() {
|
|
65
|
+
if (!this.config) {
|
|
66
|
+
this.config = new Config(this.httpClient);
|
|
67
|
+
}
|
|
68
|
+
try {
|
|
69
|
+
return await this.config.retrieve(this.resourceConfig.agentId);
|
|
70
|
+
}
|
|
71
|
+
catch (error) {
|
|
72
|
+
throw new APIError("Failed to get agent config.", 500, undefined, undefined);
|
|
73
|
+
}
|
|
74
|
+
}
|
|
75
|
+
/**
|
|
76
|
+
* Validate a session if it's still active or not
|
|
77
|
+
* GET /med-assist/session/:sessionId
|
|
78
|
+
*/
|
|
79
|
+
async validateSession(sessionId) {
|
|
80
|
+
try {
|
|
81
|
+
const sessionData = await this.session.retrieve(sessionId);
|
|
82
|
+
if (sessionData.msg && sessionData.msg === "Session Active") {
|
|
83
|
+
return {
|
|
84
|
+
expired: false,
|
|
85
|
+
active: true,
|
|
86
|
+
};
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
expired: true,
|
|
90
|
+
active: false,
|
|
91
|
+
};
|
|
92
|
+
}
|
|
93
|
+
catch (error) {
|
|
94
|
+
if (error instanceof APIError) {
|
|
95
|
+
switch (error.status) {
|
|
96
|
+
// case 404:
|
|
97
|
+
// return {
|
|
98
|
+
// expired: false,
|
|
99
|
+
// active: false,
|
|
100
|
+
// };
|
|
101
|
+
// case 500:
|
|
102
|
+
// return {
|
|
103
|
+
// expired: false,
|
|
104
|
+
// active: false,
|
|
105
|
+
// };
|
|
106
|
+
case 401:
|
|
107
|
+
// if (!!error.error?.msg) {
|
|
108
|
+
// return {
|
|
109
|
+
// expired: true,
|
|
110
|
+
// active: false,
|
|
111
|
+
// };
|
|
112
|
+
// }
|
|
113
|
+
// throw new APIError(error.error?.msg || "Failed to authenticate user", 401, undefined, undefined);
|
|
114
|
+
return {
|
|
115
|
+
expired: true,
|
|
116
|
+
active: false,
|
|
117
|
+
};
|
|
118
|
+
default:
|
|
119
|
+
throw error;
|
|
120
|
+
}
|
|
121
|
+
}
|
|
122
|
+
throw error;
|
|
123
|
+
}
|
|
124
|
+
}
|
|
125
|
+
/**
|
|
126
|
+
* Refresh a session
|
|
127
|
+
* POST /med-assist/session/:sessionId/refresh
|
|
128
|
+
*/
|
|
129
|
+
async refreshSession(sessionId, sessionToken) {
|
|
130
|
+
return await this.session.refresh(sessionId, sessionToken);
|
|
131
|
+
}
|
|
132
|
+
/**
|
|
133
|
+
* Feedback on a message
|
|
134
|
+
* PATCH /med-assist/session/:sessionId/:messageId
|
|
135
|
+
*/
|
|
136
|
+
async sendFeedback(sessionId, messageId, feedback, feedbackReason) {
|
|
137
|
+
return await this.session.feedback(sessionId, messageId, feedback, feedbackReason);
|
|
138
|
+
}
|
|
139
|
+
/**
|
|
140
|
+
* Call a tool
|
|
141
|
+
* POST /med-assist/api-call-tool
|
|
142
|
+
*/
|
|
143
|
+
async callTool(sessionId, toolId, message_id, session_token, toolParams) {
|
|
144
|
+
return await this.session.callTool(sessionId, toolId, message_id, session_token, toolParams);
|
|
145
|
+
}
|
|
146
|
+
async getVoiceIceServers(sessionId, sessionToken) {
|
|
147
|
+
return await this.voiceResource.getIceServers(sessionId, sessionToken);
|
|
148
|
+
}
|
|
149
|
+
async sendVoiceOffer(body) {
|
|
150
|
+
return await this.voiceResource.sendOffer(body);
|
|
151
|
+
}
|
|
152
|
+
}
|
|
@@ -0,0 +1,44 @@
|
|
|
1
|
+
import { BaseResource } from "../../internal/Api/BaseResource";
|
|
2
|
+
/**
|
|
3
|
+
* SessionsResource class for managing sessions-related api calls
|
|
4
|
+
*/
|
|
5
|
+
export class Session extends BaseResource {
|
|
6
|
+
//Ts implicitly calls super() here
|
|
7
|
+
/**
|
|
8
|
+
* create a new session
|
|
9
|
+
* POST /med-assist/session
|
|
10
|
+
*/
|
|
11
|
+
basePath = "/med-assist/session";
|
|
12
|
+
async create(sessionRequest, authToken) {
|
|
13
|
+
return this.post(this.basePath, sessionRequest, authToken
|
|
14
|
+
? { headers: { "x-auth-token": authToken } }
|
|
15
|
+
: undefined);
|
|
16
|
+
}
|
|
17
|
+
async retrieve(sessionId) {
|
|
18
|
+
return this.get(`${this.basePath}/${sessionId}`);
|
|
19
|
+
}
|
|
20
|
+
/**
|
|
21
|
+
* refresh a session
|
|
22
|
+
* POST /med-assist/session/refresh
|
|
23
|
+
*/
|
|
24
|
+
async refresh(sessionId, sessionToken) {
|
|
25
|
+
return this.get(`${this.basePath}/${sessionId}/refresh`, undefined, {
|
|
26
|
+
headers: {
|
|
27
|
+
"x-sess-token": sessionToken,
|
|
28
|
+
},
|
|
29
|
+
});
|
|
30
|
+
}
|
|
31
|
+
async feedback(sessionId, messageId, feedback, feedback_reason) {
|
|
32
|
+
return this.put(`${this.basePath}/${sessionId}/messages/${messageId}/feedback`, {
|
|
33
|
+
feedback,
|
|
34
|
+
feedback_reason,
|
|
35
|
+
});
|
|
36
|
+
}
|
|
37
|
+
async callTool(sessionId, toolId, messageId, sessionToken, toolParams) {
|
|
38
|
+
return this.post(`${this.basePath}/${sessionId}/messages/${messageId}/tools/${toolId}`, { tool_params: toolParams }, {
|
|
39
|
+
headers: {
|
|
40
|
+
"x-sess-token": sessionToken,
|
|
41
|
+
},
|
|
42
|
+
});
|
|
43
|
+
}
|
|
44
|
+
}
|
|
@@ -0,0 +1,12 @@
|
|
|
1
|
+
import { BaseResource } from "../../internal/Api/BaseResource";
|
|
2
|
+
export class ToolCall extends BaseResource {
|
|
3
|
+
basePath = "/med-assist/api-call-tool";
|
|
4
|
+
async callTool(sessionId, toolName, toolParams) {
|
|
5
|
+
// if (toolName === SYNAPSE_TOOL_NAME.GET_LOCATION) {
|
|
6
|
+
// return this.requestLocation() as Promise<R>;
|
|
7
|
+
// }
|
|
8
|
+
return this.post(`${this.basePath}?session_id=${sessionId}&tool_name=${toolName}`, {
|
|
9
|
+
tool_params: toolParams,
|
|
10
|
+
});
|
|
11
|
+
}
|
|
12
|
+
}
|
|
@@ -0,0 +1,34 @@
|
|
|
1
|
+
export var SYNAPSE_TOOL_NAME;
|
|
2
|
+
(function (SYNAPSE_TOOL_NAME) {
|
|
3
|
+
SYNAPSE_TOOL_NAME["MOBILE_VERIFICATION"] = "mobile_verification";
|
|
4
|
+
SYNAPSE_TOOL_NAME["AVAILABILITY_DATES"] = "availability_dates";
|
|
5
|
+
SYNAPSE_TOOL_NAME["AVAILABILITY_SLOTS"] = "availability_slots";
|
|
6
|
+
SYNAPSE_TOOL_NAME["DOCTOR_AVAILABILITY"] = "doctor_availability";
|
|
7
|
+
SYNAPSE_TOOL_NAME["DOCTOR_DETAILS"] = "doctor_details";
|
|
8
|
+
SYNAPSE_TOOL_NAME["GET_LOCATION"] = "get_location";
|
|
9
|
+
})(SYNAPSE_TOOL_NAME || (SYNAPSE_TOOL_NAME = {}));
|
|
10
|
+
export var SYNAPSE_COMPONENTS;
|
|
11
|
+
(function (SYNAPSE_COMPONENTS) {
|
|
12
|
+
SYNAPSE_COMPONENTS["MOBILE"] = "mobile_number";
|
|
13
|
+
SYNAPSE_COMPONENTS["OTP"] = "otp";
|
|
14
|
+
SYNAPSE_COMPONENTS["EMAIL"] = "email_address";
|
|
15
|
+
SYNAPSE_COMPONENTS["PILL"] = "pills";
|
|
16
|
+
SYNAPSE_COMPONENTS["MULTI"] = "multi";
|
|
17
|
+
SYNAPSE_COMPONENTS["DOCTOR_CARD"] = "doctor_card";
|
|
18
|
+
SYNAPSE_COMPONENTS["CARD"] = "card";
|
|
19
|
+
SYNAPSE_COMPONENTS["MEDIA_CARD"] = "media_card";
|
|
20
|
+
})(SYNAPSE_COMPONENTS || (SYNAPSE_COMPONENTS = {}));
|
|
21
|
+
export var SYNAPSE_TOOL_CALLBACK_NAME;
|
|
22
|
+
(function (SYNAPSE_TOOL_CALLBACK_NAME) {
|
|
23
|
+
SYNAPSE_TOOL_CALLBACK_NAME["DOCTOR_AVAILABILITY"] = "get_doctor_availability";
|
|
24
|
+
SYNAPSE_TOOL_CALLBACK_NAME["AVAILABILITY_DATES"] = "get_available_dates";
|
|
25
|
+
SYNAPSE_TOOL_CALLBACK_NAME["AVAILABILITY_SLOTS"] = "get_available_slots";
|
|
26
|
+
SYNAPSE_TOOL_CALLBACK_NAME["MOBILE_VERIFICATION"] = "verify_mobile_number";
|
|
27
|
+
SYNAPSE_TOOL_CALLBACK_NAME["DOCTOR_DETAILS"] = "get_doctor_details";
|
|
28
|
+
})(SYNAPSE_TOOL_CALLBACK_NAME || (SYNAPSE_TOOL_CALLBACK_NAME = {}));
|
|
29
|
+
export var SYNAPSE_EICITATION_STATUS;
|
|
30
|
+
(function (SYNAPSE_EICITATION_STATUS) {
|
|
31
|
+
SYNAPSE_EICITATION_STATUS["PROGRESS"] = "progress";
|
|
32
|
+
SYNAPSE_EICITATION_STATUS["SUCCESS"] = "success";
|
|
33
|
+
SYNAPSE_EICITATION_STATUS["FAILURE"] = "failure";
|
|
34
|
+
})(SYNAPSE_EICITATION_STATUS || (SYNAPSE_EICITATION_STATUS = {}));
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
import { BaseResource } from "../../internal/Api/BaseResource";
|
|
2
|
+
export class VoiceResource extends BaseResource {
|
|
3
|
+
static ICE_SERVERS_PATH = "/med-assist/voice/ice-servers";
|
|
4
|
+
static OFFER_PATH = "/med-assist/voice/offer";
|
|
5
|
+
async getIceServers(sessionId, sessionToken) {
|
|
6
|
+
return this.get(VoiceResource.ICE_SERVERS_PATH, {
|
|
7
|
+
session_id: sessionId,
|
|
8
|
+
token: sessionToken,
|
|
9
|
+
});
|
|
10
|
+
}
|
|
11
|
+
async sendOffer(body) {
|
|
12
|
+
return this.post(VoiceResource.OFFER_PATH, body);
|
|
13
|
+
}
|
|
14
|
+
}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,110 @@
|
|
|
1
|
+
import { APIConnectionTimeoutError, APIError, PermissionDeniedError, SynapseError, UnauthorizedError, } from "../internal/Error/Error";
|
|
2
|
+
// Utility functions for error handling
|
|
3
|
+
export class ErrorUtils {
|
|
4
|
+
/**
|
|
5
|
+
* Check if an error is a specific API error type
|
|
6
|
+
*/
|
|
7
|
+
static isAPIError(error) {
|
|
8
|
+
return error instanceof APIError;
|
|
9
|
+
}
|
|
10
|
+
/**
|
|
11
|
+
* Check if an error is a network-related error
|
|
12
|
+
*/
|
|
13
|
+
static isNetworkError(error) {
|
|
14
|
+
return (error instanceof APIError &&
|
|
15
|
+
(error.status === 0 || error.status === undefined));
|
|
16
|
+
}
|
|
17
|
+
/**
|
|
18
|
+
* Check if an error is a timeout error
|
|
19
|
+
*/
|
|
20
|
+
static isTimeoutError(error) {
|
|
21
|
+
return error instanceof APIConnectionTimeoutError;
|
|
22
|
+
}
|
|
23
|
+
/**
|
|
24
|
+
* Check if an error is an authentication error
|
|
25
|
+
*/
|
|
26
|
+
static isAuthError(error) {
|
|
27
|
+
return (error instanceof UnauthorizedError ||
|
|
28
|
+
error instanceof PermissionDeniedError);
|
|
29
|
+
}
|
|
30
|
+
/**
|
|
31
|
+
* Check if an error is a client error (4xx)
|
|
32
|
+
*/
|
|
33
|
+
static isClientError(error) {
|
|
34
|
+
return (error instanceof APIError &&
|
|
35
|
+
error.status !== undefined &&
|
|
36
|
+
error.status >= 400 &&
|
|
37
|
+
error.status < 500);
|
|
38
|
+
}
|
|
39
|
+
/**
|
|
40
|
+
* Check if an error is a server error (5xx)
|
|
41
|
+
*/
|
|
42
|
+
static isServerError(error) {
|
|
43
|
+
return (error instanceof APIError &&
|
|
44
|
+
error.status !== undefined &&
|
|
45
|
+
error.status >= 500);
|
|
46
|
+
}
|
|
47
|
+
/**
|
|
48
|
+
* Get user-friendly error message
|
|
49
|
+
*/
|
|
50
|
+
static getUserFriendlyMessage(error) {
|
|
51
|
+
if (ErrorUtils.isNetworkError(error)) {
|
|
52
|
+
return "Unable to connect to the server. Please check your internet connection.";
|
|
53
|
+
}
|
|
54
|
+
if (ErrorUtils.isTimeoutError(error)) {
|
|
55
|
+
return "Request timed out. Please try again.";
|
|
56
|
+
}
|
|
57
|
+
if (ErrorUtils.isAuthError(error)) {
|
|
58
|
+
return "Authentication failed. Please log in again.";
|
|
59
|
+
}
|
|
60
|
+
if (ErrorUtils.isServerError(error)) {
|
|
61
|
+
return "Server error occurred. Please try again later.";
|
|
62
|
+
}
|
|
63
|
+
if (ErrorUtils.isAPIError(error)) {
|
|
64
|
+
return error.message;
|
|
65
|
+
}
|
|
66
|
+
if (error instanceof Error) {
|
|
67
|
+
return error.message;
|
|
68
|
+
}
|
|
69
|
+
return "An unexpected error occurred.";
|
|
70
|
+
}
|
|
71
|
+
/**
|
|
72
|
+
* Extract error details for logging
|
|
73
|
+
*/
|
|
74
|
+
static getErrorDetails(error) {
|
|
75
|
+
if (ErrorUtils.isAPIError(error)) {
|
|
76
|
+
return {
|
|
77
|
+
type: error.name,
|
|
78
|
+
message: error.message,
|
|
79
|
+
code: error.code,
|
|
80
|
+
status: error.status,
|
|
81
|
+
timestamp: error.timestamp,
|
|
82
|
+
stack: error.stack,
|
|
83
|
+
context: error.context,
|
|
84
|
+
hint: error.hint,
|
|
85
|
+
};
|
|
86
|
+
}
|
|
87
|
+
if (error instanceof SynapseError) {
|
|
88
|
+
return {
|
|
89
|
+
type: error.name,
|
|
90
|
+
message: error.message,
|
|
91
|
+
code: error.code,
|
|
92
|
+
timestamp: error.timestamp,
|
|
93
|
+
stack: error.stack,
|
|
94
|
+
context: error.context,
|
|
95
|
+
hint: error.hint,
|
|
96
|
+
};
|
|
97
|
+
}
|
|
98
|
+
if (error instanceof Error) {
|
|
99
|
+
return {
|
|
100
|
+
type: error.name,
|
|
101
|
+
message: error.message,
|
|
102
|
+
stack: error.stack,
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
return {
|
|
106
|
+
type: "Unknown",
|
|
107
|
+
message: String(error),
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
}
|