Skip to content

Commit bd589f0

Browse files
authored
Merge pull request #729 from prayas17/fix-ai-issues
fix ai issues and implemented sentry at more places
2 parents 4753dcd + 6372118 commit bd589f0

File tree

1 file changed

+53
-14
lines changed

1 file changed

+53
-14
lines changed

src/modules/workspace/services/ai-assistant.service.ts

Lines changed: 53 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,11 @@ export class AiAssistantService {
649649
}
650650

651651
if (!this.deepseekClient) {
652+
Sentry.withScope((scope) => {
653+
scope.setTag("emailId", emailId);
654+
scope.setExtra("emailId", emailId);
655+
Sentry.captureException("DeepSeek Initialization Failed.");
656+
});
652657
throw new InternalServerErrorException(
653658
"DeepSeek AI client is not initialized.",
654659
);
@@ -665,12 +670,17 @@ export class AiAssistantService {
665670
} else {
666671
console.warn("Invalid conversation format: Not a string or array");
667672
}
668-
} catch (e) {
669-
console.warn("Failed to parse conversation:", e);
673+
} catch (error) {
674+
console.warn("Failed to parse conversation:", error);
675+
Sentry.withScope((scope) => {
676+
scope.setTag("emailId", emailId);
677+
scope.setExtra("emailId", emailId);
678+
Sentry.captureException(error);
679+
});
670680
}
671681
}
672682

673-
const userInput = `{Text: ${text}, API data: ${apiData}}`;
683+
const userInput = JSON.stringify({ Text: text, "API data": apiData });
674684

675685
const messageHistory: ChatMessage[] = [
676686
{ role: Roles.system, content: instructions },
@@ -744,12 +754,21 @@ export class AiAssistantService {
744754
model: model,
745755
};
746756

747-
await this.producerService.produce(
748-
TOPIC.AI_RESPONSE_GENERATED_TOPIC,
749-
{
750-
value: JSON.stringify(eventMessage),
751-
},
752-
);
757+
try {
758+
await this.producerService.produce(
759+
TOPIC.AI_RESPONSE_GENERATED_TOPIC,
760+
{
761+
value: JSON.stringify(eventMessage),
762+
},
763+
);
764+
} catch (e) {
765+
console.warn("Kafka logging failed", e);
766+
Sentry.withScope((scope) => {
767+
scope.setTag("emailId", emailId);
768+
scope.setExtra("emailId", emailId);
769+
Sentry.captureException(e);
770+
});
771+
}
753772

754773
const activityLog = {
755774
userId: user._id.toString(),
@@ -759,23 +778,38 @@ export class AiAssistantService {
759778
threadId: "null",
760779
};
761780

781+
try {
782+
await this.producerService.produce(TOPIC.AI_ACTIVITY_LOG_TOPIC, {
783+
value: JSON.stringify(activityLog),
784+
});
785+
} catch (e) {
786+
console.warn("Kafka logging failed", e);
787+
Sentry.withScope((scope) => {
788+
scope.setTag("emailId", emailId);
789+
scope.setExtra("emailId", emailId);
790+
Sentry.captureException(e);
791+
});
792+
}
793+
762794
// Send activity log to Kafka topic
763-
await this.producerService.produce(TOPIC.AI_ACTIVITY_LOG_TOPIC, {
764-
value: JSON.stringify(activityLog),
765-
});
766795
} else {
767796
console.warn("Run usage not yet available.");
768797
}
769798
} catch (e) {
770799
console.error("Invalid JSON in event data:", event.data, e);
800+
Sentry.withScope((scope) => {
801+
scope.setTag("emailId", emailId);
802+
scope.setExtra("emailId", emailId);
803+
Sentry.captureException(e);
804+
});
771805
}
772806
}
773807
} catch (error) {
774808
console.error("DeepSeek error:", error);
775809
Sentry.withScope((scope) => {
776810
scope.setTag("emailId", emailId);
777811
scope.setExtra("emailId", emailId);
778-
Sentry.captureException(error.message);
812+
Sentry.captureException(error);
779813
});
780814
client.send(
781815
JSON.stringify({
@@ -1794,6 +1828,11 @@ export class AiAssistantService {
17941828
try {
17951829
parsedData = JSON.parse(message);
17961830
} catch (err) {
1831+
Sentry.withScope((scope) => {
1832+
scope.setTag("emailId", parsedData.emailId);
1833+
scope.setExtra("emailId", parsedData.emailId);
1834+
Sentry.captureException(err);
1835+
});
17971836
client.send(
17981837
JSON.stringify({ event: "error", message: "Invalid JSON format." }),
17991838
);
@@ -2004,7 +2043,7 @@ export class AiAssistantService {
20042043
} catch (error) {
20052044
console.error("Error in WebSocket loop:", error);
20062045
Sentry.withScope((scope) => {
2007-
Sentry.captureException(error.message);
2046+
Sentry.captureException(error);
20082047
});
20092048
if (client.readyState === WebSocket.OPEN) {
20102049
client.send(

0 commit comments

Comments
 (0)