@twentyhq/teams 1.1.2 → 1.2.1
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/dependencies/yarn.lock +723 -3
- package/manifest.json +46 -29
- package/package.json +4 -2
- package/src/features/chat/logic-functions/teams-activities-enqueue.mjs +378 -162
- package/src/features/chat/logic-functions/teams-activities-enqueue.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-activities-enqueue.ts +1 -1
- package/src/features/chat/logic-functions/teams-activities-resolver.mjs +500 -40
- package/src/features/chat/logic-functions/teams-activities-resolver.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-activities-resolver.ts +13 -2
- package/src/features/chat/logic-functions/teams-list-unlinked-users.mjs +95 -61
- package/src/features/chat/logic-functions/teams-list-unlinked-users.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-match-user-links.mjs +105 -86
- package/src/features/chat/logic-functions/teams-match-user-links.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-register-connection.mjs +119 -101
- package/src/features/chat/logic-functions/teams-register-connection.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-register-connection.ts +1 -1
- package/src/features/chat/logic-functions/teams-search-users.mjs +96 -62
- package/src/features/chat/logic-functions/teams-search-users.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-set-user-link.mjs +88 -54
- package/src/features/chat/logic-functions/teams-set-user-link.mjs.map +4 -4
- package/src/features/chat/logic-functions/teams-tenant-release.mjs +72 -41
- package/src/features/chat/logic-functions/teams-tenant-release.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-count-transcript-history.mjs +99 -68
- package/src/features/transcripts/logic-functions/teams-count-transcript-history.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-import-transcript-history.mjs +99 -68
- package/src/features/transcripts/logic-functions/teams-import-transcript-history.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-import-transcript.mjs +96 -65
- package/src/features/transcripts/logic-functions/teams-import-transcript.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-list-organizer-transcripts.mjs +89 -58
- package/src/features/transcripts/logic-functions/teams-list-organizer-transcripts.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-sync-transcript.mjs +96 -65
- package/src/features/transcripts/logic-functions/teams-sync-transcript.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-transcript-history-count.mjs +0 -1
- package/src/features/transcripts/logic-functions/teams-transcript-history-count.mjs.map +2 -2
- package/src/features/transcripts/logic-functions/teams-transcript-history-import.mjs +0 -1
- package/src/features/transcripts/logic-functions/teams-transcript-history-import.mjs.map +2 -2
- package/src/features/transcripts/logic-functions/teams-transcript-history-status.mjs +0 -1
- package/src/features/transcripts/logic-functions/teams-transcript-history-status.mjs.map +2 -2
- package/src/features/transcripts/logic-functions/teams-transcripts-webhook.mjs +104 -73
- package/src/features/transcripts/logic-functions/teams-transcripts-webhook.mjs.map +4 -4
- package/src/features/transcripts/logic-functions/teams-uninstall.mjs +72 -41
- package/src/features/transcripts/logic-functions/teams-uninstall.mjs.map +4 -4
- package/src/front-components/settings.front-component.mjs +113 -113
- package/src/front-components/settings.front-component.mjs.map +4 -4
- package/src/logic-functions/teams-health-check.mjs +46 -25
- package/src/logic-functions/teams-health-check.mjs.map +4 -4
- package/src/logic-functions/teams-health-check.ts +21 -6
- package/src/logic-functions/teams-post-install.mjs +1270 -0
- package/src/logic-functions/teams-post-install.mjs.map +7 -0
- package/src/logic-functions/teams-post-install.ts +15 -0
|
@@ -124,10 +124,10 @@ var require_convenience = __commonJS({
|
|
|
124
124
|
return structural_1.isArray(term) && term.length > 0;
|
|
125
125
|
};
|
|
126
126
|
exports.isNonEmptyArray = isNonEmptyArray3;
|
|
127
|
-
var
|
|
127
|
+
var isNonEmptyString20 = (term) => {
|
|
128
128
|
return primitives_1.isString(term) && term.length > 0;
|
|
129
129
|
};
|
|
130
|
-
exports.isNonEmptyString =
|
|
130
|
+
exports.isNonEmptyString = isNonEmptyString20;
|
|
131
131
|
var isNumberOrNaN = (term) => {
|
|
132
132
|
return typeof term === "number";
|
|
133
133
|
};
|
|
@@ -228,7 +228,7 @@ var FAILURE_REPLY_RESERVE_SECONDS = 30;
|
|
|
228
228
|
var TEAMS_ASSISTANT_AGENT_BUDGET_SECONDS = TEAMS_ASSISTANT_REQUEST_TIMEOUT_SECONDS - FAILURE_REPLY_RESERVE_SECONDS;
|
|
229
229
|
|
|
230
230
|
// src/features/chat/logic-functions/utils/answer-teams-assistant-request.ts
|
|
231
|
-
var
|
|
231
|
+
var import_guards17 = __toESM(require_build());
|
|
232
232
|
import { CoreApiClient } from "twenty-client-sdk/core";
|
|
233
233
|
|
|
234
234
|
// src/features/chat/logic-functions/constants/teams-assistant-request-status.ts
|
|
@@ -239,6 +239,9 @@ var TEAMS_ASSISTANT_REQUEST_STATUS = {
|
|
|
239
239
|
FAILED: "FAILED"
|
|
240
240
|
};
|
|
241
241
|
|
|
242
|
+
// src/features/chat/logic-functions/constants/teams-assistant-unmatched-request-name.ts
|
|
243
|
+
var TEAMS_ASSISTANT_UNMATCHED_REQUEST_NAME = "Request from an unlinked Teams account";
|
|
244
|
+
|
|
242
245
|
// src/features/chat/logic-functions/constants/teams-assistant-unmatched-sender-text.ts
|
|
243
246
|
var TEAMS_ASSISTANT_UNMATCHED_SENDER_TEXT = "I could not match your Teams account to a member of this Twenty workspace. Ask an admin to invite you to Twenty with the same email address you use in Teams.";
|
|
244
247
|
|
|
@@ -280,7 +283,7 @@ var claimTeamsAssistantRequest = async ({
|
|
|
280
283
|
var import_guards3 = __toESM(require_build());
|
|
281
284
|
|
|
282
285
|
// src/features/chat/logic-functions/constants/teams-assistant-failure-text.ts
|
|
283
|
-
var TEAMS_ASSISTANT_FAILURE_TEXT = "
|
|
286
|
+
var TEAMS_ASSISTANT_FAILURE_TEXT = "That request failed. Try again, and if it keeps failing, an admin can find the details in Twenty.";
|
|
284
287
|
|
|
285
288
|
// src/features/chat/logic-functions/constants/teams-assistant-history-turn-limit.ts
|
|
286
289
|
var TEAMS_ASSISTANT_HISTORY_TURN_LIMIT = 10;
|
|
@@ -334,7 +337,9 @@ var updateTeamsAssistantRequest = async ({
|
|
|
334
337
|
status,
|
|
335
338
|
responseText,
|
|
336
339
|
errorMessage,
|
|
337
|
-
workspaceMemberId
|
|
340
|
+
workspaceMemberId,
|
|
341
|
+
requestText,
|
|
342
|
+
name
|
|
338
343
|
}) => {
|
|
339
344
|
await client.mutation({
|
|
340
345
|
updateTeamsAssistantRequest: {
|
|
@@ -344,7 +349,9 @@ var updateTeamsAssistantRequest = async ({
|
|
|
344
349
|
...o(status) ? { status } : {},
|
|
345
350
|
...o(responseText) ? { responseText: { blocknote: null, markdown: responseText } } : {},
|
|
346
351
|
...o(errorMessage) ? { errorMessage } : {},
|
|
347
|
-
...o(workspaceMemberId) ? { workspaceMemberId } : {}
|
|
352
|
+
...o(workspaceMemberId) ? { workspaceMemberId } : {},
|
|
353
|
+
...o(requestText) ? { requestText: { blocknote: null, markdown: requestText } } : {},
|
|
354
|
+
...o(name) ? { name } : {}
|
|
348
355
|
}
|
|
349
356
|
},
|
|
350
357
|
id: true
|
|
@@ -418,6 +425,26 @@ ${CODE_FENCE}`;
|
|
|
418
425
|
return `${keptText}${hasOpenCodeFence(keptText) ? closingFence : ""}${notice}`;
|
|
419
426
|
};
|
|
420
427
|
|
|
428
|
+
// src/features/chat/logic-functions/utils/build-teams-assistant-request-name.ts
|
|
429
|
+
var TEAMS_ASSISTANT_REQUEST_NAME_MAX_LENGTH = 60;
|
|
430
|
+
var graphemeSegmenter = new Intl.Segmenter(void 0, {
|
|
431
|
+
granularity: "grapheme"
|
|
432
|
+
});
|
|
433
|
+
var countCodePoints = (text) => [...text].length;
|
|
434
|
+
var buildTeamsAssistantRequestName = (requestText) => {
|
|
435
|
+
if (countCodePoints(requestText) <= TEAMS_ASSISTANT_REQUEST_NAME_MAX_LENGTH) {
|
|
436
|
+
return requestText;
|
|
437
|
+
}
|
|
438
|
+
const { truncatedText } = Array.from(
|
|
439
|
+
graphemeSegmenter.segment(requestText),
|
|
440
|
+
({ segment }) => segment
|
|
441
|
+
).reduce(
|
|
442
|
+
({ truncatedText: truncatedText2, isFull }, grapheme) => isFull || countCodePoints(truncatedText2 + grapheme) >= TEAMS_ASSISTANT_REQUEST_NAME_MAX_LENGTH ? { truncatedText: truncatedText2, isFull: true } : { truncatedText: truncatedText2 + grapheme, isFull: false },
|
|
443
|
+
{ truncatedText: "", isFull: false }
|
|
444
|
+
);
|
|
445
|
+
return `${truncatedText}\u2026`;
|
|
446
|
+
};
|
|
447
|
+
|
|
421
448
|
// src/features/chat/logic-functions/utils/fetch-workspace-base-url.ts
|
|
422
449
|
var import_guards5 = __toESM(require_build());
|
|
423
450
|
import { MetadataApiClient } from "twenty-client-sdk/metadata";
|
|
@@ -750,7 +777,7 @@ var getTeamsConnectorAccessTokenOrThrow = async () => {
|
|
|
750
777
|
};
|
|
751
778
|
|
|
752
779
|
// src/features/chat/logic-functions/utils/resolve-teams-sender-workspace-member-id.ts
|
|
753
|
-
var
|
|
780
|
+
var import_guards14 = __toESM(require_build());
|
|
754
781
|
|
|
755
782
|
// src/features/chat/logic-functions/constants/teams-user-link-source.ts
|
|
756
783
|
var TEAMS_USER_LINK_SOURCE = {
|
|
@@ -853,19 +880,66 @@ var parseTeamsConnectorResponseOrThrow = ({
|
|
|
853
880
|
};
|
|
854
881
|
|
|
855
882
|
// src/features/chat/logic-functions/utils/request-teams-connector.ts
|
|
856
|
-
var
|
|
883
|
+
var import_guards13 = __toESM(require_build());
|
|
884
|
+
|
|
885
|
+
// src/features/chat/logic-functions/constants/teams-connector-idempotent-retryable-statuses.ts
|
|
886
|
+
var TEAMS_CONNECTOR_IDEMPOTENT_RETRYABLE_STATUSES = /* @__PURE__ */ new Set([
|
|
887
|
+
502,
|
|
888
|
+
504
|
|
889
|
+
]);
|
|
890
|
+
|
|
891
|
+
// src/features/chat/logic-functions/constants/teams-connector-request-max-attempts.ts
|
|
892
|
+
var TEAMS_CONNECTOR_REQUEST_MAX_ATTEMPTS = 3;
|
|
857
893
|
|
|
858
894
|
// src/features/chat/logic-functions/constants/teams-connector-request-timeout-ms.ts
|
|
859
895
|
var TEAMS_CONNECTOR_REQUEST_TIMEOUT_MS = 1e4;
|
|
860
896
|
|
|
897
|
+
// src/features/chat/logic-functions/constants/teams-connector-retry-max-total-wait-ms.ts
|
|
898
|
+
var TEAMS_CONNECTOR_RETRY_MAX_TOTAL_WAIT_MS = 4e3;
|
|
899
|
+
|
|
900
|
+
// src/features/chat/logic-functions/constants/teams-connector-retryable-statuses.ts
|
|
901
|
+
var TEAMS_CONNECTOR_RETRYABLE_STATUSES = /* @__PURE__ */ new Set([412, 429, 503]);
|
|
902
|
+
|
|
903
|
+
// src/features/chat/logic-functions/types/teams-connector-request-error.ts
|
|
904
|
+
var TeamsConnectorRequestError = class extends Error {
|
|
905
|
+
constructor({ message, status }) {
|
|
906
|
+
super(message);
|
|
907
|
+
this.name = "TeamsConnectorRequestError";
|
|
908
|
+
this.status = status;
|
|
909
|
+
}
|
|
910
|
+
};
|
|
911
|
+
|
|
912
|
+
// src/features/chat/logic-functions/constants/teams-connector-retry-base-delay-ms.ts
|
|
913
|
+
var TEAMS_CONNECTOR_RETRY_BASE_DELAY_MS = 1e3;
|
|
914
|
+
|
|
915
|
+
// src/features/chat/logic-functions/constants/teams-connector-retry-jitter-ratio.ts
|
|
916
|
+
var TEAMS_CONNECTOR_RETRY_JITTER_RATIO = 0.25;
|
|
917
|
+
|
|
918
|
+
// src/features/chat/logic-functions/utils/get-teams-connector-retry-delay-ms.ts
|
|
919
|
+
var getTeamsConnectorRetryDelayMs = ({
|
|
920
|
+
attempt,
|
|
921
|
+
retryAfterMs,
|
|
922
|
+
maxDelayMs
|
|
923
|
+
}) => {
|
|
924
|
+
if (o(retryAfterMs)) {
|
|
925
|
+
const delayMs = Math.max(retryAfterMs, TEAMS_CONNECTOR_RETRY_BASE_DELAY_MS);
|
|
926
|
+
return delayMs > maxDelayMs ? 0 : delayMs;
|
|
927
|
+
}
|
|
928
|
+
const backoffMs = TEAMS_CONNECTOR_RETRY_BASE_DELAY_MS * 2 ** attempt;
|
|
929
|
+
return Math.min(
|
|
930
|
+
backoffMs * (1 + Math.random() * TEAMS_CONNECTOR_RETRY_JITTER_RATIO),
|
|
931
|
+
maxDelayMs
|
|
932
|
+
);
|
|
933
|
+
};
|
|
934
|
+
|
|
861
935
|
// src/features/chat/logic-functions/constants/teams-connector-service-url-host-suffixes.ts
|
|
862
|
-
var TEAMS_CONNECTOR_SERVICE_URL_HOST_SUFFIXES = [
|
|
863
|
-
".botframework.com",
|
|
864
|
-
".botframework.azure.us"
|
|
865
|
-
];
|
|
936
|
+
var TEAMS_CONNECTOR_SERVICE_URL_HOST_SUFFIXES = [".botframework.com"];
|
|
866
937
|
|
|
867
938
|
// src/features/chat/logic-functions/constants/teams-connector-service-url-hosts.ts
|
|
868
|
-
var TEAMS_CONNECTOR_SERVICE_URL_HOSTS = [
|
|
939
|
+
var TEAMS_CONNECTOR_SERVICE_URL_HOSTS = [
|
|
940
|
+
"smba.trafficmanager.net",
|
|
941
|
+
"smba.infra.gcc.teams.microsoft.com"
|
|
942
|
+
];
|
|
869
943
|
|
|
870
944
|
// src/features/chat/logic-functions/utils/is-teams-connector-service-url.ts
|
|
871
945
|
var isTeamsConnectorServiceUrl = (serviceUrl) => {
|
|
@@ -889,13 +963,65 @@ var normalizeTeamsServiceUrl = (serviceUrl) => {
|
|
|
889
963
|
}
|
|
890
964
|
};
|
|
891
965
|
|
|
966
|
+
// src/utils/parse-retry-after-ms.ts
|
|
967
|
+
var import_guards12 = __toESM(require_build());
|
|
968
|
+
var parseRetryAfterMs = (retryAfter) => {
|
|
969
|
+
if (!(0, import_guards12.isNonEmptyString)(retryAfter)) {
|
|
970
|
+
return void 0;
|
|
971
|
+
}
|
|
972
|
+
const retryAfterSeconds = Number(retryAfter);
|
|
973
|
+
if (Number.isFinite(retryAfterSeconds)) {
|
|
974
|
+
return Math.max(retryAfterSeconds * 1e3, 0);
|
|
975
|
+
}
|
|
976
|
+
const retryAtMs = Date.parse(retryAfter);
|
|
977
|
+
return Number.isNaN(retryAtMs) ? void 0 : Math.max(retryAtMs - Date.now(), 0);
|
|
978
|
+
};
|
|
979
|
+
|
|
980
|
+
// src/utils/sleep-for-milliseconds.ts
|
|
981
|
+
var sleepForMilliseconds = (milliseconds) => new Promise((resolve) => setTimeout(resolve, milliseconds));
|
|
982
|
+
|
|
983
|
+
// src/utils/fetch-with-retries.ts
|
|
984
|
+
var fetchWithRetries = async ({
|
|
985
|
+
sendRequest,
|
|
986
|
+
maxAttempts,
|
|
987
|
+
isRetryableStatus,
|
|
988
|
+
getRetryDelayMs,
|
|
989
|
+
retryAbortSignal
|
|
990
|
+
}) => {
|
|
991
|
+
const sendAttempt = async ({
|
|
992
|
+
attempt,
|
|
993
|
+
waitedMs
|
|
994
|
+
}) => {
|
|
995
|
+
const response = await sendRequest();
|
|
996
|
+
if (response.ok || !isRetryableStatus(response.status) || attempt + 1 >= maxAttempts) {
|
|
997
|
+
return response;
|
|
998
|
+
}
|
|
999
|
+
const delayMs = getRetryDelayMs({
|
|
1000
|
+
attempt,
|
|
1001
|
+
retryAfterMs: parseRetryAfterMs(response.headers.get("retry-after")),
|
|
1002
|
+
waitedMs
|
|
1003
|
+
});
|
|
1004
|
+
if (!o(delayMs)) {
|
|
1005
|
+
return response;
|
|
1006
|
+
}
|
|
1007
|
+
await sleepForMilliseconds(delayMs);
|
|
1008
|
+
if (o(retryAbortSignal) && retryAbortSignal.aborted) {
|
|
1009
|
+
return response;
|
|
1010
|
+
}
|
|
1011
|
+
await response.body?.cancel();
|
|
1012
|
+
return sendAttempt({ attempt: attempt + 1, waitedMs: waitedMs + delayMs });
|
|
1013
|
+
};
|
|
1014
|
+
return sendAttempt({ attempt: 0, waitedMs: 0 });
|
|
1015
|
+
};
|
|
1016
|
+
|
|
892
1017
|
// src/features/chat/logic-functions/utils/request-teams-connector.ts
|
|
893
1018
|
var requestTeamsConnector = async ({
|
|
894
1019
|
serviceUrl,
|
|
895
1020
|
path,
|
|
896
1021
|
method,
|
|
897
1022
|
accessToken,
|
|
898
|
-
body
|
|
1023
|
+
body,
|
|
1024
|
+
retryAbortSignal
|
|
899
1025
|
}) => {
|
|
900
1026
|
const normalizedServiceUrl = normalizeTeamsServiceUrl(serviceUrl);
|
|
901
1027
|
if (!isTeamsConnectorServiceUrl(normalizedServiceUrl)) {
|
|
@@ -903,22 +1029,37 @@ var requestTeamsConnector = async ({
|
|
|
903
1029
|
`Refused to send the bot credentials to ${normalizedServiceUrl}, which is not a Bot Connector host`
|
|
904
1030
|
);
|
|
905
1031
|
}
|
|
906
|
-
const
|
|
907
|
-
|
|
908
|
-
|
|
909
|
-
|
|
910
|
-
|
|
1032
|
+
const deadlineAtMs = Date.now() + TEAMS_CONNECTOR_REQUEST_TIMEOUT_MS;
|
|
1033
|
+
const response = await fetchWithRetries({
|
|
1034
|
+
sendRequest: () => fetch(`${normalizedServiceUrl}${path}`, {
|
|
1035
|
+
method,
|
|
1036
|
+
headers: {
|
|
1037
|
+
authorization: `Bearer ${accessToken}`,
|
|
1038
|
+
...o(body) ? { "content-type": "application/json" } : {}
|
|
1039
|
+
},
|
|
1040
|
+
...o(body) ? { body: JSON.stringify(body) } : {},
|
|
1041
|
+
signal: AbortSignal.timeout(Math.max(deadlineAtMs - Date.now(), 1))
|
|
1042
|
+
}),
|
|
1043
|
+
maxAttempts: TEAMS_CONNECTOR_REQUEST_MAX_ATTEMPTS,
|
|
1044
|
+
isRetryableStatus: (status) => TEAMS_CONNECTOR_RETRYABLE_STATUSES.has(status) || method !== "POST" && TEAMS_CONNECTOR_IDEMPOTENT_RETRYABLE_STATUSES.has(status),
|
|
1045
|
+
getRetryDelayMs: ({ attempt, retryAfterMs, waitedMs }) => {
|
|
1046
|
+
const delayMs = getTeamsConnectorRetryDelayMs({
|
|
1047
|
+
attempt,
|
|
1048
|
+
retryAfterMs,
|
|
1049
|
+
maxDelayMs: TEAMS_CONNECTOR_RETRY_MAX_TOTAL_WAIT_MS - waitedMs
|
|
1050
|
+
});
|
|
1051
|
+
return delayMs <= 0 || Date.now() + delayMs >= deadlineAtMs ? void 0 : delayMs;
|
|
911
1052
|
},
|
|
912
|
-
|
|
913
|
-
signal: AbortSignal.timeout(TEAMS_CONNECTOR_REQUEST_TIMEOUT_MS)
|
|
1053
|
+
retryAbortSignal
|
|
914
1054
|
});
|
|
915
1055
|
const responseBody = await response.text();
|
|
916
|
-
if (
|
|
917
|
-
|
|
918
|
-
`Bot Connector ${method} ${path} failed: ${response.status} ${response.statusText}${(0, import_guards12.isNonEmptyString)(responseBody) ? ` - ${responseBody}` : ""}`
|
|
919
|
-
);
|
|
1056
|
+
if (response.ok) {
|
|
1057
|
+
return responseBody;
|
|
920
1058
|
}
|
|
921
|
-
|
|
1059
|
+
throw new TeamsConnectorRequestError({
|
|
1060
|
+
message: `Bot Connector ${method} ${path} failed: ${response.status} ${response.statusText}${(0, import_guards13.isNonEmptyString)(responseBody) ? ` - ${responseBody}` : ""}`,
|
|
1061
|
+
status: response.status
|
|
1062
|
+
});
|
|
922
1063
|
};
|
|
923
1064
|
|
|
924
1065
|
// src/features/chat/logic-functions/utils/request-teams-connector-json.ts
|
|
@@ -951,20 +1092,11 @@ var resolveTeamsSenderWorkspaceMemberId = async ({
|
|
|
951
1092
|
serviceUrl,
|
|
952
1093
|
conversationId,
|
|
953
1094
|
teamsUserId,
|
|
1095
|
+
teamsUserAadObjectId,
|
|
954
1096
|
teamsTenantId,
|
|
955
1097
|
accessToken
|
|
956
1098
|
}) => {
|
|
957
|
-
|
|
958
|
-
serviceUrl,
|
|
959
|
-
conversationId,
|
|
960
|
-
memberId: teamsUserId,
|
|
961
|
-
accessToken
|
|
962
|
-
});
|
|
963
|
-
if (!isMemberOfClaimedTenant({ member, teamsTenantId })) {
|
|
964
|
-
return void 0;
|
|
965
|
-
}
|
|
966
|
-
const teamsUserAadObjectId = member.objectId;
|
|
967
|
-
if ((0, import_guards13.isNonEmptyString)(teamsUserAadObjectId)) {
|
|
1099
|
+
if ((0, import_guards14.isNonEmptyString)(teamsUserAadObjectId)) {
|
|
968
1100
|
const linkedWorkspaceMemberId = await findTeamsUserLinkWorkspaceMemberId({
|
|
969
1101
|
client,
|
|
970
1102
|
teamsTenantId,
|
|
@@ -974,7 +1106,16 @@ var resolveTeamsSenderWorkspaceMemberId = async ({
|
|
|
974
1106
|
return linkedWorkspaceMemberId;
|
|
975
1107
|
}
|
|
976
1108
|
}
|
|
977
|
-
const
|
|
1109
|
+
const member = await getTeamsConversationMember({
|
|
1110
|
+
serviceUrl,
|
|
1111
|
+
conversationId,
|
|
1112
|
+
memberId: teamsUserId,
|
|
1113
|
+
accessToken
|
|
1114
|
+
});
|
|
1115
|
+
if (!isMemberOfClaimedTenant({ member, teamsTenantId })) {
|
|
1116
|
+
return void 0;
|
|
1117
|
+
}
|
|
1118
|
+
const email = [member.email, member.userPrincipalName].find(import_guards14.isNonEmptyString);
|
|
978
1119
|
if (!o(email)) {
|
|
979
1120
|
return void 0;
|
|
980
1121
|
}
|
|
@@ -982,7 +1123,7 @@ var resolveTeamsSenderWorkspaceMemberId = async ({
|
|
|
982
1123
|
client,
|
|
983
1124
|
email
|
|
984
1125
|
});
|
|
985
|
-
if (o(workspaceMemberId) && (0,
|
|
1126
|
+
if (o(workspaceMemberId) && (0, import_guards14.isNonEmptyString)(teamsUserAadObjectId)) {
|
|
986
1127
|
await createTeamsUserLink({
|
|
987
1128
|
client,
|
|
988
1129
|
name: member.name,
|
|
@@ -1001,12 +1142,12 @@ var resolveTeamsSenderWorkspaceMemberId = async ({
|
|
|
1001
1142
|
};
|
|
1002
1143
|
|
|
1003
1144
|
// src/features/chat/logic-functions/utils/run-teams-assistant-agent-or-throw.ts
|
|
1004
|
-
var
|
|
1145
|
+
var import_guards16 = __toESM(require_build());
|
|
1005
1146
|
|
|
1006
1147
|
// src/features/chat/logic-functions/utils/build-teams-assistant-messages.ts
|
|
1007
|
-
var
|
|
1148
|
+
var import_guards15 = __toESM(require_build());
|
|
1008
1149
|
var buildRecordReferenceSection = (workspaceBaseUrl) => {
|
|
1009
|
-
if (!(0,
|
|
1150
|
+
if (!(0, import_guards15.isNonEmptyString)(workspaceBaseUrl)) {
|
|
1010
1151
|
return "This workspace URL could not be resolved, so record links are unavailable. Name records in plain text and never write a Twenty URL, not even a guessed one.";
|
|
1011
1152
|
}
|
|
1012
1153
|
return [
|
|
@@ -1088,7 +1229,7 @@ var runTeamsAssistantAgentOrThrow = async ({
|
|
|
1088
1229
|
throw new Error(agentResult.error ?? "Agent run failed");
|
|
1089
1230
|
}
|
|
1090
1231
|
const responseText = hasResponseText(agentResult.result) ? agentResult.result.response.trim() : "";
|
|
1091
|
-
if (!(0,
|
|
1232
|
+
if (!(0, import_guards16.isNonEmptyString)(responseText)) {
|
|
1092
1233
|
throw new TeamsAssistantEmptyResponseError(
|
|
1093
1234
|
"Agent returned an empty response"
|
|
1094
1235
|
);
|
|
@@ -1142,6 +1283,7 @@ var startTeamsAssistantStream = async ({
|
|
|
1142
1283
|
text
|
|
1143
1284
|
}) => {
|
|
1144
1285
|
let streamSequence = 1;
|
|
1286
|
+
const streamClosing = new AbortController();
|
|
1145
1287
|
const { id: streamId } = await sendTeamsActivity({
|
|
1146
1288
|
serviceUrl,
|
|
1147
1289
|
conversationId,
|
|
@@ -1154,36 +1296,48 @@ var startTeamsAssistantStream = async ({
|
|
|
1154
1296
|
]
|
|
1155
1297
|
}
|
|
1156
1298
|
});
|
|
1157
|
-
const sendStreamActivity = async (
|
|
1299
|
+
const sendStreamActivity = async ({
|
|
1300
|
+
activity,
|
|
1301
|
+
retryAbortSignal
|
|
1302
|
+
}) => {
|
|
1158
1303
|
await requestTeamsConnector({
|
|
1159
1304
|
serviceUrl,
|
|
1160
1305
|
path: buildTeamsActivityPath({ conversationId }),
|
|
1161
1306
|
method: "POST",
|
|
1162
1307
|
accessToken: await getTeamsConnectorAccessTokenOrThrow(),
|
|
1163
|
-
body: activity
|
|
1308
|
+
body: activity,
|
|
1309
|
+
retryAbortSignal
|
|
1164
1310
|
});
|
|
1165
1311
|
};
|
|
1166
1312
|
return {
|
|
1167
1313
|
update: (text2) => {
|
|
1168
1314
|
streamSequence += 1;
|
|
1169
1315
|
return sendStreamActivity({
|
|
1170
|
-
|
|
1171
|
-
|
|
1172
|
-
|
|
1173
|
-
|
|
1174
|
-
|
|
1175
|
-
|
|
1176
|
-
|
|
1177
|
-
|
|
1178
|
-
|
|
1179
|
-
|
|
1316
|
+
activity: {
|
|
1317
|
+
type: "typing",
|
|
1318
|
+
text: text2,
|
|
1319
|
+
entities: [
|
|
1320
|
+
{
|
|
1321
|
+
type: "streaminfo",
|
|
1322
|
+
streamId,
|
|
1323
|
+
streamType: "informative",
|
|
1324
|
+
streamSequence
|
|
1325
|
+
}
|
|
1326
|
+
]
|
|
1327
|
+
},
|
|
1328
|
+
retryAbortSignal: streamClosing.signal
|
|
1180
1329
|
});
|
|
1181
1330
|
},
|
|
1182
|
-
finish: (text2) =>
|
|
1183
|
-
|
|
1184
|
-
|
|
1185
|
-
|
|
1186
|
-
|
|
1331
|
+
finish: (text2) => {
|
|
1332
|
+
streamClosing.abort();
|
|
1333
|
+
return sendStreamActivity({
|
|
1334
|
+
activity: {
|
|
1335
|
+
type: "message",
|
|
1336
|
+
text: text2,
|
|
1337
|
+
entities: [{ type: "streaminfo", streamId, streamType: "final" }]
|
|
1338
|
+
}
|
|
1339
|
+
});
|
|
1340
|
+
}
|
|
1187
1341
|
};
|
|
1188
1342
|
};
|
|
1189
1343
|
|
|
@@ -1283,10 +1437,11 @@ var answerTeamsAssistantRequest = async ({
|
|
|
1283
1437
|
teamsConversationId,
|
|
1284
1438
|
teamsConversationType,
|
|
1285
1439
|
teamsUserId,
|
|
1440
|
+
teamsUserAadObjectId,
|
|
1286
1441
|
teamsTenantId,
|
|
1287
1442
|
requestText
|
|
1288
1443
|
} = request;
|
|
1289
|
-
if (!(0,
|
|
1444
|
+
if (!(0, import_guards17.isNonEmptyString)(teamsServiceUrl) || !(0, import_guards17.isNonEmptyString)(teamsConversationId) || !(0, import_guards17.isNonEmptyString)(teamsConversationType) || !(0, import_guards17.isNonEmptyString)(teamsUserId) || !(0, import_guards17.isNonEmptyString)(teamsTenantId) || !(0, import_guards17.isNonEmptyString)(requestText)) {
|
|
1290
1445
|
return { status: "skipped", reason: "Request record is missing fields" };
|
|
1291
1446
|
}
|
|
1292
1447
|
const client = new CoreApiClient();
|
|
@@ -1304,6 +1459,7 @@ var answerTeamsAssistantRequest = async ({
|
|
|
1304
1459
|
serviceUrl: teamsServiceUrl,
|
|
1305
1460
|
conversationId: teamsConversationId,
|
|
1306
1461
|
teamsUserId,
|
|
1462
|
+
teamsUserAadObjectId,
|
|
1307
1463
|
teamsTenantId,
|
|
1308
1464
|
accessToken: await getTeamsConnectorAccessTokenOrThrow()
|
|
1309
1465
|
});
|
|
@@ -1313,14 +1469,17 @@ var answerTeamsAssistantRequest = async ({
|
|
|
1313
1469
|
client,
|
|
1314
1470
|
id: request.id,
|
|
1315
1471
|
status: TEAMS_ASSISTANT_REQUEST_STATUS.DONE,
|
|
1316
|
-
responseText: TEAMS_ASSISTANT_UNMATCHED_SENDER_TEXT
|
|
1472
|
+
responseText: TEAMS_ASSISTANT_UNMATCHED_SENDER_TEXT,
|
|
1473
|
+
name: TEAMS_ASSISTANT_UNMATCHED_REQUEST_NAME
|
|
1317
1474
|
}).catch(() => void 0);
|
|
1318
1475
|
return { status: "declined", reason: "Sender is not a workspace member" };
|
|
1319
1476
|
}
|
|
1320
1477
|
await updateTeamsAssistantRequest({
|
|
1321
1478
|
client,
|
|
1322
1479
|
id: request.id,
|
|
1323
|
-
workspaceMemberId
|
|
1480
|
+
workspaceMemberId,
|
|
1481
|
+
requestText,
|
|
1482
|
+
name: buildTeamsAssistantRequestName(requestText)
|
|
1324
1483
|
});
|
|
1325
1484
|
const [history, workspaceBaseUrl] = await Promise.all([
|
|
1326
1485
|
findTeamsAssistantHistory({
|
|
@@ -1370,31 +1529,35 @@ var answerTeamsAssistantRequest = async ({
|
|
|
1370
1529
|
}
|
|
1371
1530
|
};
|
|
1372
1531
|
|
|
1532
|
+
// src/features/chat/logic-functions/constants/teams-assistant-chat-off-text.ts
|
|
1533
|
+
var TEAMS_ASSISTANT_CHAT_OFF_TEXT = "Chat is turned off for your Twenty workspace. Ask a Twenty admin to turn on Enable chat in Settings > Applications > Teams.";
|
|
1534
|
+
|
|
1535
|
+
// src/features/chat/logic-functions/constants/teams-assistant-help-text.ts
|
|
1536
|
+
var TEAMS_ASSISTANT_HELP_TEXT = [
|
|
1537
|
+
"Hi! Ask me anything about your CRM, for example:",
|
|
1538
|
+
[
|
|
1539
|
+
'- "How many open opportunities are in the pipeline?"',
|
|
1540
|
+
'- "What deals are closing this month?"',
|
|
1541
|
+
'- "Create a task to follow up on my newest lead"'
|
|
1542
|
+
].join("\n"),
|
|
1543
|
+
"Mention me with your question, or send it to me in a personal chat.",
|
|
1544
|
+
"My answers are generated by AI. Check anything important in Twenty."
|
|
1545
|
+
].join("\n\n");
|
|
1546
|
+
|
|
1547
|
+
// src/features/chat/logic-functions/constants/teams-assistant-notice-ttl-ms.ts
|
|
1548
|
+
var TEAMS_ASSISTANT_NOTICE_TTL_MS = 60 * 60 * 1e3;
|
|
1549
|
+
|
|
1550
|
+
// src/features/chat/logic-functions/constants/teams-assistant-welcome-ttl-ms.ts
|
|
1551
|
+
var TEAMS_ASSISTANT_WELCOME_TTL_MS = 30 * 24 * 60 * 60 * 1e3;
|
|
1552
|
+
|
|
1373
1553
|
// src/features/chat/logic-functions/utils/enqueue-teams-assistant-request-record.ts
|
|
1374
1554
|
import { CoreApiClient as CoreApiClient2 } from "twenty-client-sdk/core";
|
|
1375
1555
|
|
|
1376
1556
|
// src/features/chat/logic-functions/data/create-teams-assistant-request.ts
|
|
1377
|
-
var
|
|
1557
|
+
var import_guards18 = __toESM(require_build());
|
|
1378
1558
|
|
|
1379
|
-
// src/features/chat/logic-functions/
|
|
1380
|
-
var
|
|
1381
|
-
var graphemeSegmenter = new Intl.Segmenter(void 0, {
|
|
1382
|
-
granularity: "grapheme"
|
|
1383
|
-
});
|
|
1384
|
-
var countCodePoints = (text) => [...text].length;
|
|
1385
|
-
var buildTeamsAssistantRequestName = (requestText) => {
|
|
1386
|
-
if (countCodePoints(requestText) <= TEAMS_ASSISTANT_REQUEST_NAME_MAX_LENGTH) {
|
|
1387
|
-
return requestText;
|
|
1388
|
-
}
|
|
1389
|
-
const { truncatedText } = Array.from(
|
|
1390
|
-
graphemeSegmenter.segment(requestText),
|
|
1391
|
-
({ segment }) => segment
|
|
1392
|
-
).reduce(
|
|
1393
|
-
({ truncatedText: truncatedText2, isFull }, grapheme) => isFull || countCodePoints(truncatedText2 + grapheme) >= TEAMS_ASSISTANT_REQUEST_NAME_MAX_LENGTH ? { truncatedText: truncatedText2, isFull: true } : { truncatedText: truncatedText2 + grapheme, isFull: false },
|
|
1394
|
-
{ truncatedText: "", isFull: false }
|
|
1395
|
-
);
|
|
1396
|
-
return `${truncatedText}\u2026`;
|
|
1397
|
-
};
|
|
1559
|
+
// src/features/chat/logic-functions/constants/teams-assistant-pending-request-name.ts
|
|
1560
|
+
var TEAMS_ASSISTANT_PENDING_REQUEST_NAME = "Teams request";
|
|
1398
1561
|
|
|
1399
1562
|
// src/features/chat/logic-functions/data/create-teams-assistant-request.ts
|
|
1400
1563
|
var createTeamsAssistantRequest = async ({
|
|
@@ -1406,15 +1569,14 @@ var createTeamsAssistantRequest = async ({
|
|
|
1406
1569
|
__args: {
|
|
1407
1570
|
data: {
|
|
1408
1571
|
...draft,
|
|
1409
|
-
name:
|
|
1410
|
-
requestText: { blocknote: null, markdown: draft.requestText }
|
|
1572
|
+
name: TEAMS_ASSISTANT_PENDING_REQUEST_NAME
|
|
1411
1573
|
}
|
|
1412
1574
|
},
|
|
1413
1575
|
id: true
|
|
1414
1576
|
}
|
|
1415
1577
|
});
|
|
1416
1578
|
const requestId = mutationResult.createTeamsAssistantRequest?.id;
|
|
1417
|
-
if (!(0,
|
|
1579
|
+
if (!(0, import_guards18.isNonEmptyString)(requestId)) {
|
|
1418
1580
|
throw new Error("createTeamsAssistantRequest did not return an id");
|
|
1419
1581
|
}
|
|
1420
1582
|
return requestId;
|
|
@@ -1446,17 +1608,12 @@ var findTeamsAssistantRequestByTeamsActivity = async ({
|
|
|
1446
1608
|
teamsTenantId: true,
|
|
1447
1609
|
teamsUserId: true,
|
|
1448
1610
|
teamsUserAadObjectId: true,
|
|
1449
|
-
requestText: { markdown: true },
|
|
1450
1611
|
updatedAt: true
|
|
1451
1612
|
}
|
|
1452
1613
|
}
|
|
1453
1614
|
}
|
|
1454
1615
|
});
|
|
1455
|
-
|
|
1456
|
-
if (!o(node)) {
|
|
1457
|
-
return void 0;
|
|
1458
|
-
}
|
|
1459
|
-
return { ...node, requestText: node.requestText?.markdown ?? void 0 };
|
|
1616
|
+
return queryResult.teamsAssistantRequests?.edges?.[0]?.node ?? void 0;
|
|
1460
1617
|
};
|
|
1461
1618
|
|
|
1462
1619
|
// src/features/chat/logic-functions/utils/is-duplicate-record-error.ts
|
|
@@ -1464,12 +1621,12 @@ var DUPLICATE_RECORD_MESSAGE_PATTERN = /duplicate (entry|key)/i;
|
|
|
1464
1621
|
var isDuplicateRecordError = (error) => error instanceof Error && DUPLICATE_RECORD_MESSAGE_PATTERN.test(error.message);
|
|
1465
1622
|
|
|
1466
1623
|
// src/features/chat/logic-functions/utils/is-teams-assistant-request-resumable.ts
|
|
1467
|
-
var
|
|
1624
|
+
var import_guards19 = __toESM(require_build());
|
|
1468
1625
|
var isTeamsAssistantRequestResumable = (record, { nowMs = Date.now() } = {}) => {
|
|
1469
1626
|
if (record.status === TEAMS_ASSISTANT_REQUEST_STATUS.PENDING) {
|
|
1470
1627
|
return true;
|
|
1471
1628
|
}
|
|
1472
|
-
if (record.status !== TEAMS_ASSISTANT_REQUEST_STATUS.PROCESSING || !(0,
|
|
1629
|
+
if (record.status !== TEAMS_ASSISTANT_REQUEST_STATUS.PROCESSING || !(0, import_guards19.isNonEmptyString)(record.updatedAt)) {
|
|
1473
1630
|
return false;
|
|
1474
1631
|
}
|
|
1475
1632
|
return new Date(record.updatedAt).getTime() < nowMs - TEAMS_ASSISTANT_REQUEST_TIMEOUT_SECONDS * 1e3;
|
|
@@ -1477,7 +1634,10 @@ var isTeamsAssistantRequestResumable = (record, { nowMs = Date.now() } = {}) =>
|
|
|
1477
1634
|
|
|
1478
1635
|
// src/features/chat/logic-functions/utils/enqueue-teams-assistant-request-record.ts
|
|
1479
1636
|
var ALREADY_QUEUED_SKIP_REASON = "Teams activity is already queued";
|
|
1480
|
-
var resolveExistingRequest = (
|
|
1637
|
+
var resolveExistingRequest = ({
|
|
1638
|
+
existingRequest,
|
|
1639
|
+
requestText
|
|
1640
|
+
}) => o(existingRequest) && isTeamsAssistantRequestResumable(existingRequest) ? { ok: true, request: { ...existingRequest, requestText } } : { ok: true, skipped: ALREADY_QUEUED_SKIP_REASON };
|
|
1481
1641
|
var enqueueTeamsAssistantRequestRecord = async (request) => {
|
|
1482
1642
|
const client = new CoreApiClient2();
|
|
1483
1643
|
const teamsActivityLookup = {
|
|
@@ -1486,13 +1646,14 @@ var enqueueTeamsAssistantRequestRecord = async (request) => {
|
|
|
1486
1646
|
teamsActivityId: request.teamsActivityId
|
|
1487
1647
|
};
|
|
1488
1648
|
const existingRequest = await findTeamsAssistantRequestByTeamsActivity(teamsActivityLookup);
|
|
1649
|
+
const { requestText, ...draftWithoutText } = request;
|
|
1489
1650
|
if (o(existingRequest)) {
|
|
1490
|
-
return resolveExistingRequest(existingRequest);
|
|
1651
|
+
return resolveExistingRequest({ existingRequest, requestText });
|
|
1491
1652
|
}
|
|
1492
1653
|
try {
|
|
1493
1654
|
const requestId = await createTeamsAssistantRequest({
|
|
1494
1655
|
client,
|
|
1495
|
-
draft:
|
|
1656
|
+
draft: draftWithoutText
|
|
1496
1657
|
});
|
|
1497
1658
|
return {
|
|
1498
1659
|
ok: true,
|
|
@@ -1504,46 +1665,48 @@ var enqueueTeamsAssistantRequestRecord = async (request) => {
|
|
|
1504
1665
|
};
|
|
1505
1666
|
} catch (error) {
|
|
1506
1667
|
if (isDuplicateRecordError(error)) {
|
|
1507
|
-
return resolveExistingRequest(
|
|
1508
|
-
await findTeamsAssistantRequestByTeamsActivity(teamsActivityLookup)
|
|
1509
|
-
|
|
1668
|
+
return resolveExistingRequest({
|
|
1669
|
+
existingRequest: await findTeamsAssistantRequestByTeamsActivity(teamsActivityLookup),
|
|
1670
|
+
requestText
|
|
1671
|
+
});
|
|
1510
1672
|
}
|
|
1511
1673
|
throw error;
|
|
1512
1674
|
}
|
|
1513
1675
|
};
|
|
1514
1676
|
|
|
1515
|
-
// src/constants/release-flags.ts
|
|
1516
|
-
var RELEASE_FLAGS = {
|
|
1517
|
-
IS_CHAT_ASSISTANT_RELEASED: false,
|
|
1518
|
-
IS_TRANSCRIPT_HISTORY_IMPORT_RELEASED: false
|
|
1519
|
-
};
|
|
1520
|
-
|
|
1521
1677
|
// src/features/chat/constants/chat-enabled-application-variable-key.ts
|
|
1522
1678
|
var CHAT_ENABLED_APPLICATION_VARIABLE_KEY = "TEAMS_CHAT_ENABLED";
|
|
1523
1679
|
|
|
1524
|
-
// src/utils/is-feature-enabled.ts
|
|
1525
|
-
var isFeatureEnabled = ({
|
|
1526
|
-
isAvailable,
|
|
1527
|
-
settingValue
|
|
1528
|
-
}) => isAvailable && settingValue === "true";
|
|
1529
|
-
|
|
1530
1680
|
// src/features/chat/logic-functions/utils/is-teams-chat-enabled.ts
|
|
1531
|
-
var isTeamsChatEnabled = () =>
|
|
1532
|
-
|
|
1533
|
-
|
|
1534
|
-
|
|
1681
|
+
var isTeamsChatEnabled = () => process.env[CHAT_ENABLED_APPLICATION_VARIABLE_KEY] === "true";
|
|
1682
|
+
|
|
1683
|
+
// src/features/chat/logic-functions/constants/teams-assistant-help-requests.ts
|
|
1684
|
+
var TEAMS_ASSISTANT_HELP_REQUESTS = /* @__PURE__ */ new Set([
|
|
1685
|
+
"help",
|
|
1686
|
+
"hi",
|
|
1687
|
+
"hello",
|
|
1688
|
+
"hey"
|
|
1689
|
+
]);
|
|
1690
|
+
|
|
1691
|
+
// src/features/chat/logic-functions/utils/is-teams-help-request.ts
|
|
1692
|
+
var isTeamsHelpRequest = (requestText) => TEAMS_ASSISTANT_HELP_REQUESTS.has(
|
|
1693
|
+
requestText.trim().toLowerCase().replace(/[!.?]+$/, "")
|
|
1694
|
+
);
|
|
1695
|
+
|
|
1696
|
+
// src/features/chat/logic-functions/utils/is-teams-install-activity.ts
|
|
1697
|
+
var isTeamsInstallActivity = (activity) => activity.type === "installationUpdate" && activity.action === "add";
|
|
1535
1698
|
|
|
1536
1699
|
// src/features/chat/logic-functions/utils/parse-teams-assistant-request.ts
|
|
1537
|
-
var
|
|
1700
|
+
var import_guards21 = __toESM(require_build());
|
|
1538
1701
|
|
|
1539
1702
|
// src/features/chat/logic-functions/utils/collect-teams-bot-mention-texts.ts
|
|
1540
|
-
var
|
|
1703
|
+
var import_guards20 = __toESM(require_build());
|
|
1541
1704
|
var collectTeamsBotMentionTexts = ({
|
|
1542
1705
|
entities,
|
|
1543
1706
|
recipient
|
|
1544
1707
|
}) => (entities ?? []).filter(
|
|
1545
|
-
(entity) => entity.type === "mention" && (0,
|
|
1546
|
-
).map((entity) => entity.text).filter(
|
|
1708
|
+
(entity) => entity.type === "mention" && (0, import_guards20.isNonEmptyString)(recipient?.id) && entity.mentioned?.id?.toLowerCase() === recipient.id.toLowerCase()
|
|
1709
|
+
).map((entity) => entity.text).filter(import_guards20.isNonEmptyString);
|
|
1547
1710
|
|
|
1548
1711
|
// src/features/chat/logic-functions/utils/normalize-teams-request-text.ts
|
|
1549
1712
|
var MENTION_TAG_PATTERN = /<at>(.*?)<\/at>/g;
|
|
@@ -1576,15 +1739,15 @@ var parseTeamsAssistantRequest = ({
|
|
|
1576
1739
|
return { request: null, skipReason: "Not a user message" };
|
|
1577
1740
|
}
|
|
1578
1741
|
const conversationId = activity.conversation?.id;
|
|
1579
|
-
if (!(0,
|
|
1742
|
+
if (!(0, import_guards21.isNonEmptyString)(activity.id) || !(0, import_guards21.isNonEmptyString)(conversationId) || !(0, import_guards21.isNonEmptyString)(senderId)) {
|
|
1580
1743
|
return { request: null, skipReason: "Activity is missing required fields" };
|
|
1581
1744
|
}
|
|
1582
1745
|
const conversationType = activity.conversation?.conversationType ?? TEAMS_PERSONAL_CONVERSATION_TYPE;
|
|
1583
|
-
if (conversationType !== TEAMS_PERSONAL_CONVERSATION_TYPE && !(0,
|
|
1746
|
+
if (conversationType !== TEAMS_PERSONAL_CONVERSATION_TYPE && !(0, import_guards21.isNonEmptyArray)(collectTeamsBotMentionTexts(activity))) {
|
|
1584
1747
|
return { request: null, skipReason: "Bot is not mentioned" };
|
|
1585
1748
|
}
|
|
1586
1749
|
const requestText = normalizeTeamsRequestText(activity);
|
|
1587
|
-
if (!(0,
|
|
1750
|
+
if (!(0, import_guards21.isNonEmptyString)(requestText)) {
|
|
1588
1751
|
return {
|
|
1589
1752
|
request: null,
|
|
1590
1753
|
skipReason: "Empty request text",
|
|
@@ -1609,68 +1772,121 @@ var parseTeamsAssistantRequest = ({
|
|
|
1609
1772
|
};
|
|
1610
1773
|
};
|
|
1611
1774
|
|
|
1612
|
-
// src/features/chat/logic-functions/
|
|
1613
|
-
var
|
|
1614
|
-
"Hi! Ask me anything about your CRM, for example:",
|
|
1615
|
-
[
|
|
1616
|
-
'- "How many open opportunities are in the pipeline?"',
|
|
1617
|
-
'- "What deals are closing this month?"',
|
|
1618
|
-
'- "Create a task to follow up on my newest lead"'
|
|
1619
|
-
].join("\n"),
|
|
1620
|
-
"Mention me with your question, or send it to me in a personal chat."
|
|
1621
|
-
].join("\n\n");
|
|
1622
|
-
|
|
1623
|
-
// src/features/chat/logic-functions/utils/build-teams-empty-request-reply-kv-key.ts
|
|
1624
|
-
var buildTeamsEmptyRequestReplyKvKey = ({
|
|
1775
|
+
// src/features/chat/logic-functions/utils/build-teams-help-reply-kv-key.ts
|
|
1776
|
+
var buildTeamsHelpReplyKvKey = ({
|
|
1625
1777
|
teamsConversationId,
|
|
1626
1778
|
teamsActivityId
|
|
1627
|
-
}) => `teams-
|
|
1779
|
+
}) => `teams-help-reply:${teamsConversationId}:${teamsActivityId}`;
|
|
1628
1780
|
|
|
1629
|
-
// src/features/chat/logic-functions/utils/
|
|
1630
|
-
var
|
|
1631
|
-
|
|
1632
|
-
|
|
1633
|
-
|
|
1781
|
+
// src/features/chat/logic-functions/utils/send-teams-reply-once.ts
|
|
1782
|
+
var sendTeamsReplyOnce = async ({
|
|
1783
|
+
kvKey,
|
|
1784
|
+
ttlMs,
|
|
1785
|
+
serviceUrl,
|
|
1786
|
+
conversationId,
|
|
1787
|
+
text
|
|
1788
|
+
}) => {
|
|
1789
|
+
const existingClaim = await ge.get(kvKey);
|
|
1634
1790
|
if (o(existingClaim) && existingClaim.expiresAt > Date.now()) {
|
|
1635
1791
|
return false;
|
|
1636
1792
|
}
|
|
1637
|
-
await ge.set(
|
|
1638
|
-
return true;
|
|
1639
|
-
};
|
|
1640
|
-
|
|
1641
|
-
// src/features/chat/logic-functions/utils/reply-to-empty-teams-assistant-request.ts
|
|
1642
|
-
var replyToEmptyTeamsAssistantRequest = async (emptyRequest) => {
|
|
1643
|
-
if (!await claimTeamsEmptyRequestReply(emptyRequest)) {
|
|
1644
|
-
return { ok: true, skipped: "Empty request was already answered" };
|
|
1645
|
-
}
|
|
1793
|
+
await ge.set(kvKey, { expiresAt: Date.now() + ttlMs });
|
|
1646
1794
|
try {
|
|
1647
1795
|
await sendTeamsActivity({
|
|
1648
|
-
serviceUrl
|
|
1649
|
-
conversationId
|
|
1796
|
+
serviceUrl,
|
|
1797
|
+
conversationId,
|
|
1650
1798
|
accessToken: await getTeamsConnectorAccessTokenOrThrow(),
|
|
1651
|
-
activity: { type: "message", text
|
|
1799
|
+
activity: { type: "message", text }
|
|
1652
1800
|
});
|
|
1653
1801
|
} catch (error) {
|
|
1654
|
-
await ge.delete(
|
|
1802
|
+
await ge.delete(kvKey).catch(
|
|
1655
1803
|
(releaseError) => console.warn(
|
|
1656
|
-
`[teams] could not release the
|
|
1804
|
+
`[teams] could not release the reply claim ${kvKey}`,
|
|
1657
1805
|
releaseError
|
|
1658
1806
|
)
|
|
1659
1807
|
);
|
|
1660
1808
|
throw error;
|
|
1661
1809
|
}
|
|
1662
|
-
return
|
|
1810
|
+
return true;
|
|
1811
|
+
};
|
|
1812
|
+
|
|
1813
|
+
// src/features/chat/logic-functions/utils/reply-with-teams-assistant-help.ts
|
|
1814
|
+
var replyWithTeamsAssistantHelp = async (request) => {
|
|
1815
|
+
const isSent = await sendTeamsReplyOnce({
|
|
1816
|
+
kvKey: buildTeamsHelpReplyKvKey(request),
|
|
1817
|
+
ttlMs: TEAMS_ASSISTANT_NOTICE_TTL_MS,
|
|
1818
|
+
serviceUrl: request.teamsServiceUrl,
|
|
1819
|
+
conversationId: request.teamsConversationId,
|
|
1820
|
+
text: TEAMS_ASSISTANT_HELP_TEXT
|
|
1821
|
+
});
|
|
1822
|
+
return isSent ? { ok: true } : { ok: true, skipped: "Help was already sent for this activity" };
|
|
1823
|
+
};
|
|
1824
|
+
|
|
1825
|
+
// src/features/chat/logic-functions/utils/send-teams-assistant-notice.ts
|
|
1826
|
+
var import_guards22 = __toESM(require_build());
|
|
1827
|
+
|
|
1828
|
+
// src/features/chat/logic-functions/utils/is-teams-activity-addressed-to-bot.ts
|
|
1829
|
+
var isTeamsActivityAddressedToBot = (payload) => {
|
|
1830
|
+
if (isTeamsInstallActivity(payload.activity)) {
|
|
1831
|
+
return true;
|
|
1832
|
+
}
|
|
1833
|
+
const parsed = parseTeamsAssistantRequest(payload);
|
|
1834
|
+
return parsed.request !== null || o(parsed.emptyRequest);
|
|
1835
|
+
};
|
|
1836
|
+
|
|
1837
|
+
// src/features/chat/logic-functions/utils/send-teams-assistant-notice.ts
|
|
1838
|
+
var sendTeamsAssistantNotice = async ({
|
|
1839
|
+
payload,
|
|
1840
|
+
kind,
|
|
1841
|
+
text,
|
|
1842
|
+
ttlMs
|
|
1843
|
+
}) => {
|
|
1844
|
+
const conversationId = payload.activity.conversation?.id;
|
|
1845
|
+
if (!(0, import_guards22.isNonEmptyString)(conversationId) || !isTeamsActivityAddressedToBot(payload)) {
|
|
1846
|
+
return false;
|
|
1847
|
+
}
|
|
1848
|
+
return sendTeamsReplyOnce({
|
|
1849
|
+
kvKey: `teams-notice:${kind}:${conversationId}`,
|
|
1850
|
+
ttlMs,
|
|
1851
|
+
serviceUrl: payload.serviceUrl,
|
|
1852
|
+
conversationId,
|
|
1853
|
+
text
|
|
1854
|
+
}).catch((error) => {
|
|
1855
|
+
console.warn(
|
|
1856
|
+
`[teams] could not post the ${kind} notice in ${conversationId}`,
|
|
1857
|
+
error
|
|
1858
|
+
);
|
|
1859
|
+
return false;
|
|
1860
|
+
});
|
|
1663
1861
|
};
|
|
1664
1862
|
|
|
1665
1863
|
// src/features/chat/logic-functions/utils/enqueue-teams-assistant-request.ts
|
|
1864
|
+
var CHAT_DISABLED_SKIP_REASON = "Chat is disabled";
|
|
1666
1865
|
var enqueueTeamsAssistantRequest = async (payload) => {
|
|
1667
|
-
|
|
1668
|
-
|
|
1669
|
-
|
|
1866
|
+
if (!isTeamsChatEnabled()) {
|
|
1867
|
+
await sendTeamsAssistantNotice({
|
|
1868
|
+
payload,
|
|
1869
|
+
kind: "chat-off",
|
|
1870
|
+
text: TEAMS_ASSISTANT_CHAT_OFF_TEXT,
|
|
1871
|
+
ttlMs: TEAMS_ASSISTANT_NOTICE_TTL_MS
|
|
1872
|
+
});
|
|
1873
|
+
return { ok: true, skipped: CHAT_DISABLED_SKIP_REASON };
|
|
1874
|
+
}
|
|
1875
|
+
if (isTeamsInstallActivity(payload.activity)) {
|
|
1876
|
+
const isWelcomeSent = await sendTeamsAssistantNotice({
|
|
1877
|
+
payload,
|
|
1878
|
+
kind: "welcome",
|
|
1879
|
+
text: TEAMS_ASSISTANT_HELP_TEXT,
|
|
1880
|
+
ttlMs: TEAMS_ASSISTANT_WELCOME_TTL_MS
|
|
1881
|
+
});
|
|
1882
|
+
return isWelcomeSent ? { ok: true } : { ok: true, skipped: "Welcome was not sent" };
|
|
1670
1883
|
}
|
|
1671
1884
|
const parsed = parseTeamsAssistantRequest(payload);
|
|
1672
1885
|
if (parsed.request === null) {
|
|
1673
|
-
return o(parsed.emptyRequest) ? await
|
|
1886
|
+
return o(parsed.emptyRequest) ? await replyWithTeamsAssistantHelp(parsed.emptyRequest) : { ok: true, skipped: parsed.skipReason };
|
|
1887
|
+
}
|
|
1888
|
+
if (isTeamsHelpRequest(parsed.request.requestText)) {
|
|
1889
|
+
return await replyWithTeamsAssistantHelp(parsed.request);
|
|
1674
1890
|
}
|
|
1675
1891
|
return await enqueueTeamsAssistantRequestRecord(parsed.request);
|
|
1676
1892
|
};
|
|
@@ -1690,7 +1906,7 @@ var teamsActivitiesEnqueueHandler = async (payload) => {
|
|
|
1690
1906
|
var teams_activities_enqueue_default = defineLogicFunction({
|
|
1691
1907
|
universalIdentifier: TEAMS_ACTIVITIES_ENQUEUE_UNIVERSAL_IDENTIFIER,
|
|
1692
1908
|
name: "teams-activities-enqueue",
|
|
1693
|
-
description: "Receives a verified Microsoft Teams message activity from the activities route, in the workspace that claimed its tenant.
|
|
1909
|
+
description: "Receives a verified Microsoft Teams message or install activity from the activities route, in the workspace that claimed its tenant. Posts a welcome when the bot is installed, the help text for help, hi and bare mentions, and a notice when chat is turned off. Otherwise, when chat is enabled, records the message as a Teams Assistant Request, once per conversation and activity id, then answers it: matches the sender to a workspace member by email, runs the Teams Assistant agent as that member and posts the answer back to the conversation.",
|
|
1694
1910
|
timeoutSeconds: TEAMS_ASSISTANT_REQUEST_TIMEOUT_SECONDS,
|
|
1695
1911
|
handler: teamsActivitiesEnqueueHandler
|
|
1696
1912
|
});
|