Skip to content

Commit

Permalink
misc: fetch notes until finished (#951)
Browse files Browse the repository at this point in the history
  • Loading branch information
embbnux authored Dec 27, 2024
1 parent f856e74 commit 42641b1
Show file tree
Hide file tree
Showing 2 changed files with 42 additions and 4 deletions.
39 changes: 36 additions & 3 deletions src/modules/SmartNotes/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import {
} from '@ringcentral-integration/core';
import { dynamicLoad } from '@ringcentral/mfe-react';
import callDirections from '@ringcentral-integration/commons/enums/callDirections';
import { sleep } from '@ringcentral-integration/commons/utils';

interface SmartNoteSession {
id: string;
Expand Down Expand Up @@ -101,6 +102,9 @@ export class SmartNotes extends RcModuleV2 {
return;
}
if (this.session?.id === webphoneSession.partyData.sessionId) {
if (this._smartNoteClient && this._smartNoteClient.transcriptions.length > 0) {
this.addRecentNotedCall(this.session.id);
}
this.setSession({
id: webphoneSession.partyData.sessionId,
status: 'Disconnected',
Expand Down Expand Up @@ -245,6 +249,20 @@ export class SmartNotes extends RcModuleV2 {
return this._smartNoteClient;
}

@state
recentNotedCalls = [];

@action
addRecentNotedCall(telephonySessionId) {
let newRecentNotedCalls = [telephonySessionId].concat(
this.recentNotedCalls.filter((id) => id !== telephonySessionId),
);
if (newRecentNotedCalls.length > 5) {
newRecentNotedCalls = newRecentNotedCalls.slice(0, 5);
}
this.recentNotedCalls = newRecentNotedCalls;
}

@state
callsQueryResults = [];

Expand Down Expand Up @@ -277,7 +295,10 @@ export class SmartNotes extends RcModuleV2 {
const queryResult = await this.SmartNoteClient.querySmartNotes(sdk, noQueryIds);
const notedResult = [];
noQueryIds.forEach((id) => {
const noted = !!queryResult.records.find((record) => record.telephonySessionId === id);
let noted = !!queryResult.records.find((record) => record.telephonySessionId === id);
if (!noted && this.recentNotedCalls.indexOf(id) > -1) {
noted = true; // if it's in recentNotedCalls, it should be noted
}
notedResult.push({
id,
noted,
Expand Down Expand Up @@ -312,6 +333,19 @@ export class SmartNotes extends RcModuleV2 {
this.smartNoteTextStore = newStore;
}

async _fetchNotesUntilFinished(telephonySessionId, retryCount = 0) {
const sdk = this._deps.client.service;
const result = await this.SmartNoteClient.getNotes(sdk, telephonySessionId);
if (result.status !== 'InProgress') {
return result;
}
if (retryCount > 3) {
return result;
}
await sleep(5000);
return this._fetchNotesUntilFinished(telephonySessionId, retryCount + 1);
}

async fetchSmartNoteText(telephonySessionId) {
if (!this.SmartNoteClient || !this.hasPermission) {
return null;
Expand All @@ -327,9 +361,8 @@ export class SmartNotes extends RcModuleV2 {
if (!noted || !noted.noted) {
return null;
}
const sdk = this._deps.client.service;
try {
const note = await this.SmartNoteClient.getNotes(sdk, telephonySessionId);
const note = await this._fetchNotesUntilFinished(telephonySessionId);
let noteHTMLString = note.data || '';
noteHTMLString = noteHTMLString.replaceAll('<strong>', '**').replaceAll('</strong>', '**');
const doc = new DOMParser().parseFromString(noteHTMLString, 'text/html');
Expand Down
7 changes: 6 additions & 1 deletion src/modules/ThirdPartyService/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -779,7 +779,12 @@ export default class ThirdPartyService extends RcModuleV2 {
if (!this._callLoggerPath) {
return;
}
if (this._deps.smartNotes.hasPermission) {
if (
this._deps.smartNotes.hasPermission && (
!!call.internalType || // call log from history
call.result === 'Disconnected' // active call after call end
)
) {
options.aiNote = await this._deps.smartNotes.fetchSmartNoteText(call.telephonySessionId);
const transcript = await this._deps.smartNotes.fetchTranscript(call.telephonySessionId);
if (transcript) {
Expand Down

0 comments on commit 42641b1

Please sign in to comment.