@twentyhq/meet 0.0.0-stage → 1.0.0

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.
Files changed (172) hide show
  1. package/README.md +48 -2
  2. package/dependencies/yarn.lock +1863 -0
  3. package/manifest.json +1901 -0
  4. package/package.json +44 -4
  5. package/public/gallery/meet-cover-image-1.png +0 -0
  6. package/public/gallery/meet-cover-image-2.png +0 -0
  7. package/public/gallery/meet-cover-image-3.png +0 -0
  8. package/public/logo.svg +1 -0
  9. package/src/constants/meet-call-summary-prompt.constant.ts +54 -0
  10. package/src/constants/meet.constant.ts +17 -0
  11. package/src/constants/universal-identifiers.ts +35 -0
  12. package/src/logic-functions/constants/calendar-event-matching.constant.ts +3 -0
  13. package/src/logic-functions/constants/google-docs-api.constant.ts +10 -0
  14. package/src/logic-functions/constants/google-drive-api.constant.ts +1 -0
  15. package/src/logic-functions/constants/google-pubsub-push.constant.ts +7 -0
  16. package/src/logic-functions/constants/google-user-info.constant.ts +2 -0
  17. package/src/logic-functions/constants/google-workspace-events.constant.ts +17 -0
  18. package/src/logic-functions/constants/meet-api.constant.ts +9 -0
  19. package/src/logic-functions/constants/meet-call-summary.constant.ts +3 -0
  20. package/src/logic-functions/constants/meet-import.constant.ts +7 -0
  21. package/src/logic-functions/constants/meet-recording-import.constant.ts +7 -0
  22. package/src/logic-functions/constants/restricted-field-placeholder.constant.ts +2 -0
  23. package/src/logic-functions/meet-events-resolver.mjs +1345 -0
  24. package/src/logic-functions/meet-events-resolver.mjs.map +7 -0
  25. package/src/logic-functions/meet-events-resolver.ts +89 -0
  26. package/src/logic-functions/meet-handle-event.mjs +665 -0
  27. package/src/logic-functions/meet-handle-event.mjs.map +7 -0
  28. package/src/logic-functions/meet-handle-event.ts +86 -0
  29. package/src/logic-functions/meet-identify-account.mjs +369 -0
  30. package/src/logic-functions/meet-identify-account.mjs.map +7 -0
  31. package/src/logic-functions/meet-identify-account.ts +42 -0
  32. package/src/logic-functions/meet-import-conference.mjs +1465 -0
  33. package/src/logic-functions/meet-import-conference.mjs.map +7 -0
  34. package/src/logic-functions/meet-import-conference.ts +101 -0
  35. package/src/logic-functions/meet-import-recording.mjs +794 -0
  36. package/src/logic-functions/meet-import-recording.mjs.map +7 -0
  37. package/src/logic-functions/meet-import-recording.ts +99 -0
  38. package/src/logic-functions/meet-queue-conference-imports.mjs +711 -0
  39. package/src/logic-functions/meet-queue-conference-imports.mjs.map +7 -0
  40. package/src/logic-functions/meet-queue-conference-imports.ts +62 -0
  41. package/src/logic-functions/meet-refresh-connections.mjs +457 -0
  42. package/src/logic-functions/meet-refresh-connections.mjs.map +7 -0
  43. package/src/logic-functions/meet-refresh-connections.ts +64 -0
  44. package/src/logic-functions/meet-register-connection.mjs +837 -0
  45. package/src/logic-functions/meet-register-connection.mjs.map +7 -0
  46. package/src/logic-functions/meet-register-connection.ts +81 -0
  47. package/src/logic-functions/meet-summarize-call-recording.mjs +645 -0
  48. package/src/logic-functions/meet-summarize-call-recording.mjs.map +7 -0
  49. package/src/logic-functions/meet-summarize-call-recording.ts +111 -0
  50. package/src/logic-functions/meet-unregister-connection.mjs +610 -0
  51. package/src/logic-functions/meet-unregister-connection.mjs.map +7 -0
  52. package/src/logic-functions/meet-unregister-connection.ts +57 -0
  53. package/src/logic-functions/types/calendar-event-candidate.type.ts +6 -0
  54. package/src/logic-functions/types/call-recording-for-summary.type.ts +5 -0
  55. package/src/logic-functions/types/call-recording-sync-fields.type.ts +12 -0
  56. package/src/logic-functions/types/call-recording-upsert-outcome.type.ts +4 -0
  57. package/src/logic-functions/types/google-doc-lists.type.ts +4 -0
  58. package/src/logic-functions/types/google-doc-markdown-segment.type.ts +5 -0
  59. package/src/logic-functions/types/google-doc-paragraph-element.type.ts +8 -0
  60. package/src/logic-functions/types/google-doc-paragraph.type.ts +7 -0
  61. package/src/logic-functions/types/google-doc-tab.type.ts +10 -0
  62. package/src/logic-functions/types/google-doc.type.ts +6 -0
  63. package/src/logic-functions/types/google-pubsub-push-body.type.ts +6 -0
  64. package/src/logic-functions/types/google-request-method.type.ts +1 -0
  65. package/src/logic-functions/types/google-user-info.type.ts +4 -0
  66. package/src/logic-functions/types/google-workspace-events-operation.type.ts +8 -0
  67. package/src/logic-functions/types/meet-call-summary-outcome.type.ts +6 -0
  68. package/src/logic-functions/types/meet-collection-page.type.ts +3 -0
  69. package/src/logic-functions/types/meet-conference-record.type.ts +7 -0
  70. package/src/logic-functions/types/meet-conference-sync-outcome.type.ts +5 -0
  71. package/src/logic-functions/types/meet-conference-sync-result.type.ts +7 -0
  72. package/src/logic-functions/types/meet-daily-job-name.type.ts +1 -0
  73. package/src/logic-functions/types/meet-event-data.type.ts +5 -0
  74. package/src/logic-functions/types/meet-event-outcome.type.ts +4 -0
  75. package/src/logic-functions/types/meet-event-subscription.type.ts +8 -0
  76. package/src/logic-functions/types/meet-event.type.ts +5 -0
  77. package/src/logic-functions/types/meet-exception-code.type.ts +12 -0
  78. package/src/logic-functions/types/meet-exception.type.ts +25 -0
  79. package/src/logic-functions/types/meet-participant.type.ts +8 -0
  80. package/src/logic-functions/types/meet-recording-import-outcome.type.ts +6 -0
  81. package/src/logic-functions/types/meet-recording.type.ts +7 -0
  82. package/src/logic-functions/types/meet-smart-note.type.ts +7 -0
  83. package/src/logic-functions/types/meet-space.type.ts +5 -0
  84. package/src/logic-functions/types/meet-transcript-entry.type.ts +8 -0
  85. package/src/logic-functions/types/meet-transcript.type.ts +7 -0
  86. package/src/logic-functions/types/transcript-entry.type.ts +7 -0
  87. package/src/logic-functions/utils/build-google-api-url-or-throw.util.ts +30 -0
  88. package/src/logic-functions/utils/build-google-doc-markdown-segment.util.ts +67 -0
  89. package/src/logic-functions/utils/build-meet-artifact-import-job.util.ts +25 -0
  90. package/src/logic-functions/utils/build-meet-call-recording-sync-fields.util.ts +48 -0
  91. package/src/logic-functions/utils/build-meet-call-summary-job.util.ts +19 -0
  92. package/src/logic-functions/utils/build-meet-call-summary-prompt.util.ts +50 -0
  93. package/src/logic-functions/utils/build-meet-conference-import-job.util.ts +26 -0
  94. package/src/logic-functions/utils/build-meet-conference-import-payload.util.ts +18 -0
  95. package/src/logic-functions/utils/build-meet-conference-record-filter.util.ts +8 -0
  96. package/src/logic-functions/utils/build-meet-daily-job.util.ts +17 -0
  97. package/src/logic-functions/utils/build-meet-recording-import-job.util.ts +20 -0
  98. package/src/logic-functions/utils/build-meet-url-or-throw.util.ts +15 -0
  99. package/src/logic-functions/utils/build-meet-user-target-resource.util.ts +2 -0
  100. package/src/logic-functions/utils/can-renew-meet-event-subscription.util.ts +18 -0
  101. package/src/logic-functions/utils/chunk-into-batches.util.ts +10 -0
  102. package/src/logic-functions/utils/claim-meet-event-subscription.util.ts +10 -0
  103. package/src/logic-functions/utils/compute-call-recording-id-for-meet-conference.util.ts +16 -0
  104. package/src/logic-functions/utils/convert-google-doc-paragraph-to-markdown.util.ts +62 -0
  105. package/src/logic-functions/utils/convert-google-doc-tab-to-markdown.util.ts +49 -0
  106. package/src/logic-functions/utils/create-meet-event-subscription-or-throw.util.ts +41 -0
  107. package/src/logic-functions/utils/delete-meet-event-subscription.util.ts +22 -0
  108. package/src/logic-functions/utils/enqueue-meet-conference-imports-or-throw.util.ts +39 -0
  109. package/src/logic-functions/utils/enqueue-meet-daily-jobs-or-throw.util.ts +35 -0
  110. package/src/logic-functions/utils/ensure-meet-event-subscription-or-throw.util.ts +51 -0
  111. package/src/logic-functions/utils/escape-markdown-text.util.ts +4 -0
  112. package/src/logic-functions/utils/escape-sql-like-pattern.util.ts +2 -0
  113. package/src/logic-functions/utils/extract-bearer-token.util.ts +23 -0
  114. package/src/logic-functions/utils/extract-meet-name-from-doc-title.util.ts +16 -0
  115. package/src/logic-functions/utils/fetch-meet-json.util.ts +107 -0
  116. package/src/logic-functions/utils/find-call-recording-for-summary.util.ts +38 -0
  117. package/src/logic-functions/utils/find-call-recording-video-state.util.ts +25 -0
  118. package/src/logic-functions/utils/find-meet-calendar-event.util.ts +74 -0
  119. package/src/logic-functions/utils/find-meet-conference-title.util.ts +38 -0
  120. package/src/logic-functions/utils/find-meet-event-subscription.util.ts +32 -0
  121. package/src/logic-functions/utils/find-meet-smart-notes-markdown.util.ts +32 -0
  122. package/src/logic-functions/utils/format-seconds-as-clock-timestamp.util.ts +20 -0
  123. package/src/logic-functions/utils/get-conference-record-name-from-artifact-name.util.ts +6 -0
  124. package/src/logic-functions/utils/get-connection-access-token-or-throw.util.ts +18 -0
  125. package/src/logic-functions/utils/get-google-doc-title.util.ts +31 -0
  126. package/src/logic-functions/utils/get-google-doc.util.ts +30 -0
  127. package/src/logic-functions/utils/get-google-drive-file-size-bytes-or-throw.util.ts +31 -0
  128. package/src/logic-functions/utils/get-meet-conference-record.util.ts +18 -0
  129. package/src/logic-functions/utils/get-meet-connection-subscription-key.util.ts +3 -0
  130. package/src/logic-functions/utils/get-meet-event-subscription-key.util.ts +2 -0
  131. package/src/logic-functions/utils/get-meet-participant-display-name.util.ts +20 -0
  132. package/src/logic-functions/utils/get-meet-recording-drive-file-id.util.ts +16 -0
  133. package/src/logic-functions/utils/get-meet-recording.util.ts +18 -0
  134. package/src/logic-functions/utils/get-meet-space.util.ts +15 -0
  135. package/src/logic-functions/utils/get-random-delay-milliseconds.util.ts +4 -0
  136. package/src/logic-functions/utils/is-google-doc-unreadable-error.util.ts +9 -0
  137. package/src/logic-functions/utils/is-google-pubsub-push-authorized.util.ts +31 -0
  138. package/src/logic-functions/utils/is-meet-ai-summary-enabled-for-member.util.ts +22 -0
  139. package/src/logic-functions/utils/is-transcript-entry-list.util.ts +16 -0
  140. package/src/logic-functions/utils/join-google-doc-markdown-segments.util.ts +102 -0
  141. package/src/logic-functions/utils/list-calendar-events-in-window.util.ts +142 -0
  142. package/src/logic-functions/utils/list-meet-collection.util.ts +79 -0
  143. package/src/logic-functions/utils/list-meet-conference-records.util.ts +22 -0
  144. package/src/logic-functions/utils/list-meet-participants.util.ts +15 -0
  145. package/src/logic-functions/utils/list-meet-recordings.util.ts +15 -0
  146. package/src/logic-functions/utils/list-meet-smart-notes.util.ts +15 -0
  147. package/src/logic-functions/utils/list-meet-transcript-entries.util.ts +15 -0
  148. package/src/logic-functions/utils/list-meet-transcripts.util.ts +15 -0
  149. package/src/logic-functions/utils/map-meet-transcript-entries.util.ts +64 -0
  150. package/src/logic-functions/utils/normalize-meeting-url.util.ts +18 -0
  151. package/src/logic-functions/utils/open-google-drive-file-download-or-throw.util.ts +36 -0
  152. package/src/logic-functions/utils/parse-meet-call-summary-response.util.ts +36 -0
  153. package/src/logic-functions/utils/parse-meet-event-data-or-throw.util.ts +36 -0
  154. package/src/logic-functions/utils/parse-meet-event-or-throw.util.ts +35 -0
  155. package/src/logic-functions/utils/parse-retry-after-milliseconds.util.ts +27 -0
  156. package/src/logic-functions/utils/put-stream-to-upload-url-or-throw.util.ts +73 -0
  157. package/src/logic-functions/utils/read-meet-event-subscription-or-throw.util.ts +24 -0
  158. package/src/logic-functions/utils/release-meet-event-subscription.util.ts +29 -0
  159. package/src/logic-functions/utils/renew-meet-event-subscription-or-throw.util.ts +32 -0
  160. package/src/logic-functions/utils/run-meet-call-summarizer-or-throw.util.ts +28 -0
  161. package/src/logic-functions/utils/save-call-recording-summary-if-empty.util.ts +29 -0
  162. package/src/logic-functions/utils/save-call-recording-video.util.ts +23 -0
  163. package/src/logic-functions/utils/should-generate-meet-ai-summary.util.ts +17 -0
  164. package/src/logic-functions/utils/sleep-for-milliseconds.util.ts +2 -0
  165. package/src/logic-functions/utils/split-surrounding-whitespace.util.ts +10 -0
  166. package/src/logic-functions/utils/store-meet-connection-subscription.util.ts +46 -0
  167. package/src/logic-functions/utils/sync-meet-conference-or-throw.util.ts +129 -0
  168. package/src/logic-functions/utils/upload-google-drive-file-as-call-recording-video-or-throw.util.ts +73 -0
  169. package/src/logic-functions/utils/upsert-call-recording-or-throw.util.ts +104 -0
  170. package/src/logic-functions/utils/verify-google-pubsub-push-token-or-throw.util.ts +71 -0
  171. package/src/logic-functions/utils/wait-for-google-workspace-events-operation-or-throw.util.ts +53 -0
  172. package/tsconfig.json +39 -0
@@ -0,0 +1,64 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+ import { isDefined } from 'twenty-sdk/utils';
3
+
4
+ import { type MeetConferenceRecord } from 'src/logic-functions/types/meet-conference-record.type';
5
+ import { type MeetParticipant } from 'src/logic-functions/types/meet-participant.type';
6
+ import { type MeetTranscriptEntry } from 'src/logic-functions/types/meet-transcript-entry.type';
7
+ import { type TranscriptEntry } from 'src/logic-functions/types/transcript-entry.type';
8
+ import { getMeetParticipantDisplayName } from 'src/logic-functions/utils/get-meet-participant-display-name.util';
9
+
10
+ export const mapMeetTranscriptEntries = ({
11
+ conferenceRecord,
12
+ participants,
13
+ transcriptEntries,
14
+ }: {
15
+ conferenceRecord: Pick<MeetConferenceRecord, 'startTime'>;
16
+ participants: MeetParticipant[];
17
+ transcriptEntries: Pick<
18
+ MeetTranscriptEntry,
19
+ 'participant' | 'text' | 'startTime'
20
+ >[];
21
+ }): TranscriptEntry[] => {
22
+ const conferenceStartMilliseconds = Date.parse(conferenceRecord.startTime);
23
+ const displayNameByParticipantName = new Map(
24
+ participants.map((participant) => [
25
+ participant.name,
26
+ getMeetParticipantDisplayName(participant),
27
+ ]),
28
+ );
29
+
30
+ return transcriptEntries
31
+ .flatMap(({ participant, text, startTime }) => {
32
+ const trimmedText = text?.trim();
33
+ const startMilliseconds = Date.parse(startTime ?? '');
34
+
35
+ if (!isNonEmptyString(trimmedText) || Number.isNaN(startMilliseconds)) {
36
+ return [];
37
+ }
38
+
39
+ return [
40
+ { participantName: participant, text: trimmedText, startMilliseconds },
41
+ ];
42
+ })
43
+ .sort(
44
+ (firstEntry, secondEntry) =>
45
+ firstEntry.startMilliseconds - secondEntry.startMilliseconds,
46
+ )
47
+ .map(({ participantName, text, startMilliseconds }) => ({
48
+ participant: {
49
+ name: isDefined(participantName)
50
+ ? (displayNameByParticipantName.get(participantName) ?? null)
51
+ : null,
52
+ },
53
+ words: [
54
+ {
55
+ text,
56
+ start_timestamp: {
57
+ relative:
58
+ Math.max(startMilliseconds - conferenceStartMilliseconds, 0) /
59
+ 1_000,
60
+ },
61
+ },
62
+ ],
63
+ }));
64
+ };
@@ -0,0 +1,18 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+
3
+ export const normalizeMeetingUrl = (
4
+ meetingUrl: string | null | undefined,
5
+ ): string | undefined => {
6
+ if (!isNonEmptyString(meetingUrl) || !URL.canParse(meetingUrl)) {
7
+ return undefined;
8
+ }
9
+
10
+ const parsedUrl = new URL(meetingUrl);
11
+
12
+ parsedUrl.hash = '';
13
+ parsedUrl.search = '';
14
+ parsedUrl.hostname = parsedUrl.hostname.toLowerCase().replace(/^www\./, '');
15
+ parsedUrl.pathname = parsedUrl.pathname.replace(/\/+$/, '');
16
+
17
+ return parsedUrl.toString();
18
+ };
@@ -0,0 +1,36 @@
1
+ import { isDefined } from 'twenty-sdk/utils';
2
+
3
+ import { GOOGLE_DRIVE_API_BASE_URL } from 'src/logic-functions/constants/google-drive-api.constant';
4
+ import { MeetException } from 'src/logic-functions/types/meet-exception.type';
5
+ import { buildGoogleApiUrlOrThrow } from 'src/logic-functions/utils/build-google-api-url-or-throw.util';
6
+
7
+ export const openGoogleDriveFileDownloadOrThrow = async ({
8
+ accessToken,
9
+ driveFileId,
10
+ signal,
11
+ }: {
12
+ accessToken: string;
13
+ driveFileId: string;
14
+ signal: AbortSignal;
15
+ }): Promise<ReadableStream<Uint8Array>> => {
16
+ const response = await fetch(
17
+ buildGoogleApiUrlOrThrow({
18
+ baseUrl: GOOGLE_DRIVE_API_BASE_URL,
19
+ path: `files/${driveFileId}`,
20
+ queryParameters: { alt: 'media' },
21
+ }),
22
+ { headers: { Authorization: `Bearer ${accessToken}` }, signal },
23
+ );
24
+
25
+ if (!response.ok || !isDefined(response.body)) {
26
+ await response.body?.cancel();
27
+
28
+ throw new MeetException({
29
+ message: `Google Drive download of file ${driveFileId} failed (${response.status})`,
30
+ code: 'REQUEST_FAILED',
31
+ httpStatus: response.status,
32
+ });
33
+ }
34
+
35
+ return response.body;
36
+ };
@@ -0,0 +1,36 @@
1
+ import { isNonEmptyString, isObject } from '@sniptt/guards';
2
+
3
+ import { MEET_CALL_SUMMARY_UNAVAILABLE_PREFIX } from 'src/constants/meet-call-summary-prompt.constant';
4
+ import { MEET_SUMMARY_UNAVAILABLE_HEADING } from 'src/logic-functions/constants/meet-call-summary.constant';
5
+
6
+ export const parseMeetCallSummaryResponse = (agentResult: {
7
+ success: boolean;
8
+ result: object | null;
9
+ }): string | undefined => {
10
+ const { success, result } = agentResult;
11
+
12
+ if (
13
+ !success ||
14
+ !isObject(result) ||
15
+ !('response' in result) ||
16
+ !isNonEmptyString(result.response)
17
+ ) {
18
+ return undefined;
19
+ }
20
+
21
+ const response = result.response.trim();
22
+
23
+ if (!response.startsWith(MEET_CALL_SUMMARY_UNAVAILABLE_PREFIX)) {
24
+ return response;
25
+ }
26
+
27
+ const reason = response
28
+ .slice(MEET_CALL_SUMMARY_UNAVAILABLE_PREFIX.length)
29
+ .trim();
30
+
31
+ if (!isNonEmptyString(reason)) {
32
+ return undefined;
33
+ }
34
+
35
+ return `${MEET_SUMMARY_UNAVAILABLE_HEADING}\n\n${reason}`;
36
+ };
@@ -0,0 +1,36 @@
1
+ import { isNonEmptyString, isObject } from '@sniptt/guards';
2
+
3
+ import { type MeetEventData } from 'src/logic-functions/types/meet-event-data.type';
4
+ import { MeetException } from 'src/logic-functions/types/meet-exception.type';
5
+
6
+ const decodeMeetEventData = (
7
+ encodedData: string,
8
+ ): MeetEventData | null => {
9
+ try {
10
+ return JSON.parse(Buffer.from(encodedData, 'base64').toString('utf8'));
11
+ } catch (error) {
12
+ throw new MeetException({
13
+ message: `The Google Meet event data is not valid JSON: ${error instanceof Error ? error.message : String(error)}`,
14
+ code: 'EVENT_DATA_UNREADABLE',
15
+ });
16
+ }
17
+ };
18
+
19
+ export const parseMeetEventDataOrThrow = (
20
+ encodedData: string | undefined,
21
+ ): MeetEventData => {
22
+ if (!isNonEmptyString(encodedData)) {
23
+ return {};
24
+ }
25
+
26
+ const eventData = decodeMeetEventData(encodedData);
27
+
28
+ if (!isObject(eventData)) {
29
+ throw new MeetException({
30
+ message: 'The Google Meet event data is not an object',
31
+ code: 'EVENT_DATA_UNREADABLE',
32
+ });
33
+ }
34
+
35
+ return eventData;
36
+ };
@@ -0,0 +1,35 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+
3
+ import { GOOGLE_WORKSPACE_EVENTS_SOURCE_PREFIX } from 'src/logic-functions/constants/google-workspace-events.constant';
4
+ import { type MeetEvent } from 'src/logic-functions/types/meet-event.type';
5
+ import { type GooglePubsubPushBody } from 'src/logic-functions/types/google-pubsub-push-body.type';
6
+ import { parseMeetEventDataOrThrow } from 'src/logic-functions/utils/parse-meet-event-data-or-throw.util';
7
+
8
+ export const parseMeetEventOrThrow = (
9
+ pushBody: GooglePubsubPushBody | null,
10
+ ): MeetEvent | undefined => {
11
+ const source = pushBody?.message?.attributes?.['ce-source'];
12
+ const eventType = pushBody?.message?.attributes?.['ce-type'];
13
+
14
+ if (
15
+ !isNonEmptyString(source) ||
16
+ !source.startsWith(GOOGLE_WORKSPACE_EVENTS_SOURCE_PREFIX) ||
17
+ !isNonEmptyString(eventType)
18
+ ) {
19
+ return undefined;
20
+ }
21
+
22
+ const eventData = parseMeetEventDataOrThrow(pushBody?.message?.data);
23
+ const artifactName =
24
+ eventData.transcript?.name ??
25
+ eventData.smartNote?.name ??
26
+ eventData.recording?.name;
27
+
28
+ return {
29
+ subscriptionName: source.slice(
30
+ GOOGLE_WORKSPACE_EVENTS_SOURCE_PREFIX.length,
31
+ ),
32
+ eventType,
33
+ ...(isNonEmptyString(artifactName) ? { artifactName } : {}),
34
+ };
35
+ };
@@ -0,0 +1,27 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+
3
+ export const parseRetryAfterMilliseconds = ({
4
+ retryAfter,
5
+ now,
6
+ }: {
7
+ retryAfter: string | null;
8
+ now: Date;
9
+ }): number | undefined => {
10
+ if (!isNonEmptyString(retryAfter)) {
11
+ return undefined;
12
+ }
13
+
14
+ const seconds = Number(retryAfter);
15
+
16
+ if (Number.isFinite(seconds)) {
17
+ return seconds > 0 ? seconds * 1_000 : undefined;
18
+ }
19
+
20
+ const retryAt = Date.parse(retryAfter);
21
+
22
+ if (Number.isNaN(retryAt) || retryAt <= now.getTime()) {
23
+ return undefined;
24
+ }
25
+
26
+ return retryAt - now.getTime();
27
+ };
@@ -0,0 +1,73 @@
1
+ import { request as requestOverHttp, type IncomingMessage } from 'node:http';
2
+ import { request as requestOverHttps } from 'node:https';
3
+ import { Readable } from 'node:stream';
4
+ import { finished, pipeline } from 'node:stream/promises';
5
+
6
+ import { MeetException } from 'src/logic-functions/types/meet-exception.type';
7
+
8
+ async function* readStreamChunks(
9
+ stream: ReadableStream<Uint8Array>,
10
+ ): AsyncGenerator<Uint8Array> {
11
+ const reader = stream.getReader();
12
+
13
+ try {
14
+ while (true) {
15
+ const { done, value } = await reader.read();
16
+
17
+ if (done) {
18
+ return;
19
+ }
20
+
21
+ yield value;
22
+ }
23
+ } finally {
24
+ await reader.cancel();
25
+ }
26
+ }
27
+
28
+ // node:http instead of fetch: fetch can buffer a streamed request body in memory.
29
+ export const putStreamToUploadUrlOrThrow = async ({
30
+ stream,
31
+ sizeBytes,
32
+ uploadUrl,
33
+ contentType,
34
+ signal,
35
+ }: {
36
+ stream: ReadableStream<Uint8Array>;
37
+ sizeBytes: number;
38
+ uploadUrl: string;
39
+ contentType: string;
40
+ signal: AbortSignal;
41
+ }): Promise<void> => {
42
+ const url = new URL(uploadUrl);
43
+ const requestUpload =
44
+ url.protocol === 'http:' ? requestOverHttp : requestOverHttps;
45
+ const uploadRequest = requestUpload(url, {
46
+ method: 'PUT',
47
+ headers: { 'Content-Type': contentType, 'Content-Length': sizeBytes },
48
+ signal,
49
+ });
50
+ const uploadResponsePromise = new Promise<IncomingMessage>(
51
+ (resolve, reject) => {
52
+ uploadRequest.once('response', resolve);
53
+ uploadRequest.once('error', reject);
54
+ },
55
+ );
56
+
57
+ const [, uploadResponse] = await Promise.all([
58
+ pipeline(Readable.from(readStreamChunks(stream)), uploadRequest),
59
+ uploadResponsePromise,
60
+ ]);
61
+
62
+ uploadResponse.resume();
63
+ await finished(uploadResponse);
64
+
65
+ const statusCode = uploadResponse.statusCode ?? 0;
66
+
67
+ if (statusCode < 200 || statusCode >= 300) {
68
+ throw new MeetException({
69
+ message: `Video upload failed with status ${statusCode}`,
70
+ code: 'FILE_UPLOAD_FAILED',
71
+ });
72
+ }
73
+ };
@@ -0,0 +1,24 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+ import { isDefined } from 'twenty-sdk/utils';
3
+
4
+ import { type MeetEventSubscription } from 'src/logic-functions/types/meet-event-subscription.type';
5
+ import { MeetException } from 'src/logic-functions/types/meet-exception.type';
6
+ import { type GoogleWorkspaceEventsOperation } from 'src/logic-functions/types/google-workspace-events-operation.type';
7
+
8
+ export const readMeetEventSubscriptionOrThrow = ({
9
+ response,
10
+ error,
11
+ }: Pick<
12
+ GoogleWorkspaceEventsOperation,
13
+ 'response' | 'error'
14
+ >): MeetEventSubscription => {
15
+ if (isDefined(error) || !isNonEmptyString(response?.name)) {
16
+ throw new MeetException({
17
+ message: `Google did not return the event subscription: ${error?.message ?? 'the operation has no result'}`,
18
+ code: 'EVENT_SUBSCRIPTION_FAILED',
19
+ googleStatus: error?.status,
20
+ });
21
+ }
22
+
23
+ return response;
24
+ };
@@ -0,0 +1,29 @@
1
+ import { getConnection, kv } from 'twenty-sdk/logic-function';
2
+
3
+ import { deleteMeetEventSubscription } from 'src/logic-functions/utils/delete-meet-event-subscription.util';
4
+ import { getConnectionAccessTokenOrThrow } from 'src/logic-functions/utils/get-connection-access-token-or-throw.util';
5
+ import { getMeetEventSubscriptionKey } from 'src/logic-functions/utils/get-meet-event-subscription-key.util';
6
+
7
+ export const releaseMeetEventSubscription = async ({
8
+ connectedAccountId,
9
+ subscriptionName,
10
+ }: {
11
+ connectedAccountId: string;
12
+ subscriptionName: string;
13
+ }): Promise<void> => {
14
+ const eventSubscriptionKey =
15
+ getMeetEventSubscriptionKey(subscriptionName);
16
+
17
+ try {
18
+ await deleteMeetEventSubscription({
19
+ accessToken: getConnectionAccessTokenOrThrow(
20
+ await getConnection(connectedAccountId),
21
+ ),
22
+ subscriptionName,
23
+ });
24
+ } finally {
25
+ // The claim outlives the Google call, so no other workspace takes a subscription about to be deleted.
26
+ await kv.delete(eventSubscriptionKey);
27
+ await kv.delete(eventSubscriptionKey, { scope: 'SERVER' });
28
+ }
29
+ };
@@ -0,0 +1,32 @@
1
+ import { GOOGLE_WORKSPACE_EVENTS_API_BASE_URL } from 'src/logic-functions/constants/google-workspace-events.constant';
2
+ import { type MeetEventSubscription } from 'src/logic-functions/types/meet-event-subscription.type';
3
+ import { type GoogleWorkspaceEventsOperation } from 'src/logic-functions/types/google-workspace-events-operation.type';
4
+ import { buildGoogleApiUrlOrThrow } from 'src/logic-functions/utils/build-google-api-url-or-throw.util';
5
+ import { fetchMeetJson } from 'src/logic-functions/utils/fetch-meet-json.util';
6
+ import { readMeetEventSubscriptionOrThrow } from 'src/logic-functions/utils/read-meet-event-subscription-or-throw.util';
7
+ import { waitForGoogleWorkspaceEventsOperationOrThrow } from 'src/logic-functions/utils/wait-for-google-workspace-events-operation-or-throw.util';
8
+
9
+ export const renewMeetEventSubscriptionOrThrow = async ({
10
+ accessToken,
11
+ subscriptionName,
12
+ }: {
13
+ accessToken: string;
14
+ subscriptionName: string;
15
+ }): Promise<MeetEventSubscription> =>
16
+ readMeetEventSubscriptionOrThrow(
17
+ await waitForGoogleWorkspaceEventsOperationOrThrow({
18
+ accessToken,
19
+ pollCount: 0,
20
+ operation: await fetchMeetJson<GoogleWorkspaceEventsOperation>({
21
+ accessToken,
22
+ url: buildGoogleApiUrlOrThrow({
23
+ baseUrl: GOOGLE_WORKSPACE_EVENTS_API_BASE_URL,
24
+ path: subscriptionName,
25
+ queryParameters: { updateMask: 'ttl' },
26
+ }),
27
+ method: 'PATCH',
28
+ // Google reads a zero duration as the longest lifetime it allows.
29
+ body: { ttl: '0s' },
30
+ }),
31
+ }),
32
+ );
@@ -0,0 +1,28 @@
1
+ import {
2
+ RetryableLogicFunctionError,
3
+ runAgent,
4
+ } from 'twenty-sdk/logic-function';
5
+
6
+ import { MEET_CALL_SUMMARIZER_AGENT_UNIVERSAL_IDENTIFIER } from 'src/constants/universal-identifiers';
7
+
8
+ export const runMeetCallSummarizerOrThrow = async (
9
+ prompt: string,
10
+ ): Promise<{ success: boolean; result: object | null }> => {
11
+ const agentResult = await runAgent({
12
+ agentUniversalIdentifier:
13
+ MEET_CALL_SUMMARIZER_AGENT_UNIVERSAL_IDENTIFIER,
14
+ input: prompt,
15
+ }).catch((error: unknown) => {
16
+ throw new RetryableLogicFunctionError(
17
+ `The Google Meet summarizer agent could not run: ${error instanceof Error ? error.message : String(error)}`,
18
+ );
19
+ });
20
+
21
+ if (!agentResult.success) {
22
+ throw new RetryableLogicFunctionError(
23
+ `The Google Meet summarizer agent failed: ${agentResult.error ?? agentResult.status}`,
24
+ );
25
+ }
26
+
27
+ return agentResult;
28
+ };
@@ -0,0 +1,29 @@
1
+ import { type CoreApiClient } from 'twenty-client-sdk/core';
2
+
3
+ export const saveCallRecordingSummaryIfEmpty = async ({
4
+ coreApiClient,
5
+ callRecordingId,
6
+ summaryMarkdown,
7
+ }: {
8
+ coreApiClient: Pick<CoreApiClient, 'mutation'>;
9
+ callRecordingId: string;
10
+ summaryMarkdown: string;
11
+ }): Promise<boolean> => {
12
+ const mutationResult = await coreApiClient.mutation({
13
+ updateCallRecordings: {
14
+ __args: {
15
+ filter: {
16
+ id: { eq: callRecordingId },
17
+ or: [
18
+ { summary: { markdown: { is: 'NULL' as const } } },
19
+ { summary: { markdown: { eq: '' } } },
20
+ ],
21
+ },
22
+ data: { summary: { blocknote: null, markdown: summaryMarkdown } },
23
+ },
24
+ id: true,
25
+ },
26
+ });
27
+
28
+ return (mutationResult.updateCallRecordings ?? []).length > 0;
29
+ };
@@ -0,0 +1,23 @@
1
+ import { type CoreApiClient } from 'twenty-client-sdk/core';
2
+
3
+ import { MEET_RECORDING_FILE_NAME } from 'src/logic-functions/constants/meet-recording-import.constant';
4
+
5
+ export const saveCallRecordingVideo = async ({
6
+ coreApiClient,
7
+ callRecordingId,
8
+ fileId,
9
+ }: {
10
+ coreApiClient: Pick<CoreApiClient, 'mutation'>;
11
+ callRecordingId: string;
12
+ fileId: string;
13
+ }): Promise<void> => {
14
+ await coreApiClient.mutation({
15
+ updateCallRecording: {
16
+ __args: {
17
+ id: callRecordingId,
18
+ data: { video: [{ fileId, label: MEET_RECORDING_FILE_NAME }] },
19
+ },
20
+ id: true,
21
+ },
22
+ });
23
+ };
@@ -0,0 +1,17 @@
1
+ import { isNonEmptyArray, isNonEmptyString } from '@sniptt/guards';
2
+
3
+ import { type MeetSmartNote } from 'src/logic-functions/types/meet-smart-note.type';
4
+ import { type TranscriptEntry } from 'src/logic-functions/types/transcript-entry.type';
5
+
6
+ export const shouldGenerateMeetAiSummary = ({
7
+ transcriptEntries,
8
+ summaryMarkdown,
9
+ smartNotes,
10
+ }: {
11
+ transcriptEntries: TranscriptEntry[];
12
+ summaryMarkdown: string | undefined;
13
+ smartNotes: Pick<MeetSmartNote, 'state'>[];
14
+ }): boolean =>
15
+ isNonEmptyArray(transcriptEntries) &&
16
+ !isNonEmptyString(summaryMarkdown) &&
17
+ smartNotes.every(({ state }) => state === 'FILE_GENERATED');
@@ -0,0 +1,2 @@
1
+ export const sleepForMilliseconds = (milliseconds: number): Promise<void> =>
2
+ new Promise((resolve) => setTimeout(resolve, milliseconds));
@@ -0,0 +1,10 @@
1
+ const SURROUNDING_WHITESPACE_PATTERN = /^(\s*)([\s\S]*?)(\s*)$/;
2
+
3
+ export const splitSurroundingWhitespace = (
4
+ text: string,
5
+ ): { leading: string; content: string; trailing: string } => {
6
+ const [, leading = '', content = '', trailing = ''] =
7
+ SURROUNDING_WHITESPACE_PATTERN.exec(text) ?? [];
8
+
9
+ return { leading, content, trailing };
10
+ };
@@ -0,0 +1,46 @@
1
+ import { isNonEmptyString } from '@sniptt/guards';
2
+ import { kv } from 'twenty-sdk/logic-function';
3
+
4
+ import { claimMeetEventSubscription } from 'src/logic-functions/utils/claim-meet-event-subscription.util';
5
+ import { getMeetConnectionSubscriptionKey } from 'src/logic-functions/utils/get-meet-connection-subscription-key.util';
6
+ import { getMeetEventSubscriptionKey } from 'src/logic-functions/utils/get-meet-event-subscription-key.util';
7
+ import { releaseMeetEventSubscription } from 'src/logic-functions/utils/release-meet-event-subscription.util';
8
+
9
+ export const storeMeetConnectionSubscription = async ({
10
+ connectedAccountId,
11
+ subscriptionName,
12
+ replacedSubscriptionName,
13
+ }: {
14
+ connectedAccountId: string;
15
+ subscriptionName: string;
16
+ replacedSubscriptionName: string | undefined;
17
+ }): Promise<void> => {
18
+ const connectionSubscriptionKey =
19
+ getMeetConnectionSubscriptionKey(connectedAccountId);
20
+ const previousSubscriptionName = await kv.get<string>(
21
+ connectionSubscriptionKey,
22
+ );
23
+ const staleSubscriptionNames = new Set(
24
+ [previousSubscriptionName, replacedSubscriptionName]
25
+ .filter(isNonEmptyString)
26
+ .filter(
27
+ (staleSubscriptionName) => staleSubscriptionName !== subscriptionName,
28
+ ),
29
+ );
30
+
31
+ await claimMeetEventSubscription(subscriptionName);
32
+ await kv.set(
33
+ getMeetEventSubscriptionKey(subscriptionName),
34
+ connectedAccountId,
35
+ );
36
+
37
+ for (const staleSubscriptionName of staleSubscriptionNames) {
38
+ await releaseMeetEventSubscription({
39
+ connectedAccountId,
40
+ subscriptionName: staleSubscriptionName,
41
+ });
42
+ }
43
+
44
+ // Written last: a retry after a failed release still finds the stale name here.
45
+ await kv.set(connectionSubscriptionKey, subscriptionName);
46
+ };