@memberjunction/core-actions 6.2.0-edge.3 → 6.2.0-edge.5
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/README.md +3 -1
- package/dist/custom/ai/chief-of-staff-calendar-email.action.d.ts +109 -0
- package/dist/custom/ai/chief-of-staff-calendar-email.action.d.ts.map +1 -0
- package/dist/custom/ai/chief-of-staff-calendar-email.action.js +1252 -0
- package/dist/custom/ai/chief-of-staff-calendar-email.action.js.map +1 -0
- package/dist/custom/ai/chief-of-staff-inter-agent-request.action.d.ts +42 -0
- package/dist/custom/ai/chief-of-staff-inter-agent-request.action.d.ts.map +1 -0
- package/dist/custom/ai/chief-of-staff-inter-agent-request.action.js +203 -0
- package/dist/custom/ai/chief-of-staff-inter-agent-request.action.js.map +1 -0
- package/dist/custom/ai/configure-agent-user-policy.action.d.ts +26 -0
- package/dist/custom/ai/configure-agent-user-policy.action.d.ts.map +1 -0
- package/dist/custom/ai/configure-agent-user-policy.action.js +270 -0
- package/dist/custom/ai/configure-agent-user-policy.action.js.map +1 -0
- package/dist/custom/ai/execute-agent.action.d.ts +42 -0
- package/dist/custom/ai/execute-agent.action.d.ts.map +1 -1
- package/dist/custom/ai/execute-agent.action.js +106 -46
- package/dist/custom/ai/execute-agent.action.js.map +1 -1
- package/dist/custom/ai/prepare-chief-of-staff-outbound-call.action.d.ts +37 -0
- package/dist/custom/ai/prepare-chief-of-staff-outbound-call.action.d.ts.map +1 -0
- package/dist/custom/ai/prepare-chief-of-staff-outbound-call.action.js +214 -0
- package/dist/custom/ai/prepare-chief-of-staff-outbound-call.action.js.map +1 -0
- package/dist/custom/ai/schedule-next-agent-wake.action.d.ts +26 -0
- package/dist/custom/ai/schedule-next-agent-wake.action.d.ts.map +1 -0
- package/dist/custom/ai/schedule-next-agent-wake.action.js +278 -0
- package/dist/custom/ai/schedule-next-agent-wake.action.js.map +1 -0
- package/dist/custom/data/get-entity-details.action.js +2 -2
- package/dist/custom/data/get-entity-details.action.js.map +1 -1
- package/dist/custom/data/run-adhoc-query.action.d.ts +29 -30
- package/dist/custom/data/run-adhoc-query.action.d.ts.map +1 -1
- package/dist/custom/data/run-adhoc-query.action.js +137 -102
- package/dist/custom/data/run-adhoc-query.action.js.map +1 -1
- package/dist/custom/data/run-record-process.action.d.ts +2 -0
- package/dist/custom/data/run-record-process.action.d.ts.map +1 -1
- package/dist/custom/data/run-record-process.action.js +10 -4
- package/dist/custom/data/run-record-process.action.js.map +1 -1
- package/dist/custom/demo/calculate-expression.action.d.ts +2 -6
- package/dist/custom/demo/calculate-expression.action.d.ts.map +1 -1
- package/dist/custom/demo/calculate-expression.action.js +5 -113
- package/dist/custom/demo/calculate-expression.action.js.map +1 -1
- package/dist/custom/files/base-file-storage.action.d.ts +53 -5
- package/dist/custom/files/base-file-storage.action.d.ts.map +1 -1
- package/dist/custom/files/base-file-storage.action.js +72 -14
- package/dist/custom/files/base-file-storage.action.js.map +1 -1
- package/dist/custom/files/copy-object.action.d.ts +3 -0
- package/dist/custom/files/copy-object.action.d.ts.map +1 -1
- package/dist/custom/files/copy-object.action.js +9 -1
- package/dist/custom/files/copy-object.action.js.map +1 -1
- package/dist/custom/files/create-directory.action.d.ts +3 -0
- package/dist/custom/files/create-directory.action.d.ts.map +1 -1
- package/dist/custom/files/create-directory.action.js +4 -0
- package/dist/custom/files/create-directory.action.js.map +1 -1
- package/dist/custom/files/create-document.action.d.ts.map +1 -1
- package/dist/custom/files/create-document.action.js +12 -0
- package/dist/custom/files/create-document.action.js.map +1 -1
- package/dist/custom/files/delete-directory.action.d.ts +3 -0
- package/dist/custom/files/delete-directory.action.d.ts.map +1 -1
- package/dist/custom/files/delete-directory.action.js +4 -0
- package/dist/custom/files/delete-directory.action.js.map +1 -1
- package/dist/custom/files/delete-object.action.d.ts +3 -0
- package/dist/custom/files/delete-object.action.d.ts.map +1 -1
- package/dist/custom/files/delete-object.action.js +9 -1
- package/dist/custom/files/delete-object.action.js.map +1 -1
- package/dist/custom/files/get-download-url.action.d.ts.map +1 -1
- package/dist/custom/files/get-download-url.action.js +5 -1
- package/dist/custom/files/get-download-url.action.js.map +1 -1
- package/dist/custom/files/get-file-content.action.d.ts.map +1 -1
- package/dist/custom/files/get-file-content.action.js +5 -1
- package/dist/custom/files/get-file-content.action.js.map +1 -1
- package/dist/custom/files/get-metadata.action.d.ts.map +1 -1
- package/dist/custom/files/get-metadata.action.js +5 -1
- package/dist/custom/files/get-metadata.action.js.map +1 -1
- package/dist/custom/files/get-object.action.d.ts.map +1 -1
- package/dist/custom/files/get-object.action.js +5 -1
- package/dist/custom/files/get-object.action.js.map +1 -1
- package/dist/custom/files/get-upload-url.action.d.ts +3 -0
- package/dist/custom/files/get-upload-url.action.d.ts.map +1 -1
- package/dist/custom/files/get-upload-url.action.js +9 -1
- package/dist/custom/files/get-upload-url.action.js.map +1 -1
- package/dist/custom/files/list-objects.action.d.ts.map +1 -1
- package/dist/custom/files/list-objects.action.js +7 -3
- package/dist/custom/files/list-objects.action.js.map +1 -1
- package/dist/custom/files/list-storage-providers.action.d.ts +5 -4
- package/dist/custom/files/list-storage-providers.action.d.ts.map +1 -1
- package/dist/custom/files/list-storage-providers.action.js +15 -11
- package/dist/custom/files/list-storage-providers.action.js.map +1 -1
- package/dist/custom/files/move-object.action.d.ts +3 -0
- package/dist/custom/files/move-object.action.d.ts.map +1 -1
- package/dist/custom/files/move-object.action.js +9 -1
- package/dist/custom/files/move-object.action.js.map +1 -1
- package/dist/custom/files/search-storage-files.action.d.ts.map +1 -1
- package/dist/custom/files/search-storage-files.action.js +10 -6
- package/dist/custom/files/search-storage-files.action.js.map +1 -1
- package/dist/custom/interactive-forms/activate-form-contribution-version.action.d.ts +2 -0
- package/dist/custom/interactive-forms/activate-form-contribution-version.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/activate-form-contribution-version.action.js +2 -0
- package/dist/custom/interactive-forms/activate-form-contribution-version.action.js.map +1 -1
- package/dist/custom/interactive-forms/activate-interactive-form-version.action.d.ts +2 -0
- package/dist/custom/interactive-forms/activate-interactive-form-version.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/activate-interactive-form-version.action.js +2 -0
- package/dist/custom/interactive-forms/activate-interactive-form-version.action.js.map +1 -1
- package/dist/custom/interactive-forms/create-form-contribution.action.d.ts +2 -0
- package/dist/custom/interactive-forms/create-form-contribution.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/create-form-contribution.action.js +2 -0
- package/dist/custom/interactive-forms/create-form-contribution.action.js.map +1 -1
- package/dist/custom/interactive-forms/create-interactive-form.action.d.ts +2 -0
- package/dist/custom/interactive-forms/create-interactive-form.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/create-interactive-form.action.js +2 -0
- package/dist/custom/interactive-forms/create-interactive-form.action.js.map +1 -1
- package/dist/custom/interactive-forms/get-active-form-for-entity.action.d.ts +2 -0
- package/dist/custom/interactive-forms/get-active-form-for-entity.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/get-active-form-for-entity.action.js +2 -0
- package/dist/custom/interactive-forms/get-active-form-for-entity.action.js.map +1 -1
- package/dist/custom/interactive-forms/get-default-form-scaffold.action.d.ts +2 -0
- package/dist/custom/interactive-forms/get-default-form-scaffold.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/get-default-form-scaffold.action.js +2 -0
- package/dist/custom/interactive-forms/get-default-form-scaffold.action.js.map +1 -1
- package/dist/custom/interactive-forms/get-entity-schema-for-form.action.d.ts +2 -0
- package/dist/custom/interactive-forms/get-entity-schema-for-form.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/get-entity-schema-for-form.action.js +2 -0
- package/dist/custom/interactive-forms/get-entity-schema-for-form.action.js.map +1 -1
- package/dist/custom/interactive-forms/get-form-composition-for-entity.action.d.ts +2 -0
- package/dist/custom/interactive-forms/get-form-composition-for-entity.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/get-form-composition-for-entity.action.js +2 -0
- package/dist/custom/interactive-forms/get-form-composition-for-entity.action.js.map +1 -1
- package/dist/custom/interactive-forms/get-form-contributions-for-entity.action.d.ts +2 -0
- package/dist/custom/interactive-forms/get-form-contributions-for-entity.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/get-form-contributions-for-entity.action.js +2 -0
- package/dist/custom/interactive-forms/get-form-contributions-for-entity.action.js.map +1 -1
- package/dist/custom/interactive-forms/modify-form-contribution.action.d.ts +2 -0
- package/dist/custom/interactive-forms/modify-form-contribution.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/modify-form-contribution.action.js +2 -0
- package/dist/custom/interactive-forms/modify-form-contribution.action.js.map +1 -1
- package/dist/custom/interactive-forms/modify-interactive-form.action.d.ts +2 -0
- package/dist/custom/interactive-forms/modify-interactive-form.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/modify-interactive-form.action.js +2 -0
- package/dist/custom/interactive-forms/modify-interactive-form.action.js.map +1 -1
- package/dist/custom/interactive-forms/revert-interactive-form.action.d.ts +2 -0
- package/dist/custom/interactive-forms/revert-interactive-form.action.d.ts.map +1 -1
- package/dist/custom/interactive-forms/revert-interactive-form.action.js +2 -0
- package/dist/custom/interactive-forms/revert-interactive-form.action.js.map +1 -1
- package/dist/custom/search/scoped-search.action.d.ts +112 -22
- package/dist/custom/search/scoped-search.action.d.ts.map +1 -1
- package/dist/custom/search/scoped-search.action.js +351 -95
- package/dist/custom/search/scoped-search.action.js.map +1 -1
- package/dist/custom/search/search.action.d.ts +30 -0
- package/dist/custom/search/search.action.d.ts.map +1 -1
- package/dist/custom/search/search.action.js +77 -32
- package/dist/custom/search/search.action.js.map +1 -1
- package/dist/custom/security/password-strength.action.d.ts.map +1 -1
- package/dist/custom/security/password-strength.action.js +3 -4
- package/dist/custom/security/password-strength.action.js.map +1 -1
- package/dist/custom/utilities/artifact-builder-service.d.ts +3 -3
- package/dist/custom/utilities/artifact-builder-service.d.ts.map +1 -1
- package/dist/custom/utilities/artifact-builder-service.js +9 -9
- package/dist/custom/utilities/artifact-builder-service.js.map +1 -1
- package/dist/custom/utilities/base-file-handler.d.ts +14 -6
- package/dist/custom/utilities/base-file-handler.d.ts.map +1 -1
- package/dist/custom/utilities/base-file-handler.js +36 -28
- package/dist/custom/utilities/base-file-handler.js.map +1 -1
- package/dist/custom/utilities/safe-math-evaluator.d.ts +27 -0
- package/dist/custom/utilities/safe-math-evaluator.d.ts.map +1 -0
- package/dist/custom/utilities/safe-math-evaluator.js +104 -0
- package/dist/custom/utilities/safe-math-evaluator.js.map +1 -0
- package/dist/custom/visualization/create-mermaid-diagram.action.d.ts +18 -3
- package/dist/custom/visualization/create-mermaid-diagram.action.d.ts.map +1 -1
- package/dist/custom/visualization/create-mermaid-diagram.action.js +71 -33
- package/dist/custom/visualization/create-mermaid-diagram.action.js.map +1 -1
- package/dist/custom/visualization/shared/mermaid-renderer.d.ts +65 -0
- package/dist/custom/visualization/shared/mermaid-renderer.d.ts.map +1 -0
- package/dist/custom/visualization/shared/mermaid-renderer.js +310 -0
- package/dist/custom/visualization/shared/mermaid-renderer.js.map +1 -0
- package/dist/custom/workflow/conditional.action.d.ts +1 -18
- package/dist/custom/workflow/conditional.action.d.ts.map +1 -1
- package/dist/custom/workflow/conditional.action.js +17 -83
- package/dist/custom/workflow/conditional.action.js.map +1 -1
- package/dist/custom/workflow/loop.action.d.ts.map +1 -1
- package/dist/custom/workflow/loop.action.js +5 -0
- package/dist/custom/workflow/loop.action.js.map +1 -1
- package/dist/custom/workflow/parallel-execute.action.d.ts.map +1 -1
- package/dist/custom/workflow/parallel-execute.action.js +8 -2
- package/dist/custom/workflow/parallel-execute.action.js.map +1 -1
- package/dist/custom/workflow/retry.action.d.ts.map +1 -1
- package/dist/custom/workflow/retry.action.js +5 -0
- package/dist/custom/workflow/retry.action.js.map +1 -1
- package/dist/custom/workflow/workflow-run-scope.d.ts +15 -0
- package/dist/custom/workflow/workflow-run-scope.d.ts.map +1 -0
- package/dist/custom/workflow/workflow-run-scope.js +24 -0
- package/dist/custom/workflow/workflow-run-scope.js.map +1 -0
- package/dist/index.d.ts +6 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +6 -0
- package/dist/index.js.map +1 -1
- package/package.json +44 -42
|
@@ -0,0 +1,1252 @@
|
|
|
1
|
+
var __decorate = (this && this.__decorate) || function (decorators, target, key, desc) {
|
|
2
|
+
var c = arguments.length, r = c < 3 ? target : desc === null ? desc = Object.getOwnPropertyDescriptor(target, key) : desc, d;
|
|
3
|
+
if (typeof Reflect === "object" && typeof Reflect.decorate === "function") r = Reflect.decorate(decorators, target, key, desc);
|
|
4
|
+
else for (var i = decorators.length - 1; i >= 0; i--) if (d = decorators[i]) r = (c < 3 ? d(r) : c > 3 ? d(target, key, r) : d(target, key)) || r;
|
|
5
|
+
return c > 3 && r && Object.defineProperty(target, key, r), r;
|
|
6
|
+
};
|
|
7
|
+
import { BaseAction } from '@memberjunction/actions';
|
|
8
|
+
import { RegisterClass, SafeJSONParse, UUIDsEqual } from '@memberjunction/global';
|
|
9
|
+
import { LogError, Metadata, RunView } from '@memberjunction/core';
|
|
10
|
+
import { AgentUserPolicyEngine } from '@memberjunction/ai-core-plus';
|
|
11
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
12
|
+
import { CommunicationEngine } from '@memberjunction/communication-engine';
|
|
13
|
+
/**
|
|
14
|
+
* Shared security gate and helper methods for Chief of Staff user-scoped Email, Calendar,
|
|
15
|
+
* and External Scheduling actions.
|
|
16
|
+
*
|
|
17
|
+
* Security Invariants:
|
|
18
|
+
* 1. User-scoped actions NEVER accept an arbitrary mailbox/email address from the LLM to act as.
|
|
19
|
+
* Personal mailbox operations are always hard-pinned to `params.ContextUser.Email`.
|
|
20
|
+
* 2. Before accessing a personal mailbox via an enterprise provider (`Microsoft Graph` or `Gmail`),
|
|
21
|
+
* `EnterpriseMailboxSecurityGate` verifies:
|
|
22
|
+
* - `contextUser.Email` domain matches `provider.AllowedEmailDomains` (when configured)
|
|
23
|
+
* - `provider.EnterpriseScopeMode` allows personal mailboxes (`SecurityGroupScoped` or `TenantWide`),
|
|
24
|
+
* or falls back to the user's dedicated `MJ: AI Bridge Agent Identities` utility inbox when
|
|
25
|
+
* `EnterpriseScopeMode === 'UtilityMailboxesOnly'`
|
|
26
|
+
* - `provider.VerifyMailboxAccess` preflight succeeds for the requested channel (`Mail` or `Calendar`).
|
|
27
|
+
*/
|
|
28
|
+
export class BaseUserScopedCommunicationAction extends BaseAction {
|
|
29
|
+
getParamValue(params, name) {
|
|
30
|
+
const found = params.Params.find((p) => p.Name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
31
|
+
return found?.Value;
|
|
32
|
+
}
|
|
33
|
+
getStringParam(params, name) {
|
|
34
|
+
const val = this.getParamValue(params, name);
|
|
35
|
+
if (val == null)
|
|
36
|
+
return null;
|
|
37
|
+
const str = String(val).trim();
|
|
38
|
+
return str.length > 0 ? str : null;
|
|
39
|
+
}
|
|
40
|
+
getBooleanParam(params, name) {
|
|
41
|
+
const val = this.getParamValue(params, name);
|
|
42
|
+
if (val == null || val === '')
|
|
43
|
+
return null;
|
|
44
|
+
if (typeof val === 'boolean')
|
|
45
|
+
return val;
|
|
46
|
+
if (typeof val === 'string') {
|
|
47
|
+
const lower = val.trim().toLowerCase();
|
|
48
|
+
if (lower === 'true' || lower === '1' || lower === 'yes')
|
|
49
|
+
return true;
|
|
50
|
+
if (lower === 'false' || lower === '0' || lower === 'no')
|
|
51
|
+
return false;
|
|
52
|
+
}
|
|
53
|
+
return null;
|
|
54
|
+
}
|
|
55
|
+
getNumericParam(params, name) {
|
|
56
|
+
const val = this.getParamValue(params, name);
|
|
57
|
+
if (val == null || val === '')
|
|
58
|
+
return null;
|
|
59
|
+
const num = Number(val);
|
|
60
|
+
return Number.isFinite(num) ? num : null;
|
|
61
|
+
}
|
|
62
|
+
parseStringArrayParam(params, name) {
|
|
63
|
+
const val = this.getParamValue(params, name);
|
|
64
|
+
if (Array.isArray(val)) {
|
|
65
|
+
return val.map((v) => String(v).trim()).filter((v) => v.length > 0);
|
|
66
|
+
}
|
|
67
|
+
if (typeof val === 'string' && val.trim().length > 0) {
|
|
68
|
+
const parsed = SafeJSONParse(val);
|
|
69
|
+
if (Array.isArray(parsed)) {
|
|
70
|
+
return parsed.map((v) => String(v).trim()).filter((v) => v.length > 0);
|
|
71
|
+
}
|
|
72
|
+
return val
|
|
73
|
+
.split(',')
|
|
74
|
+
.map((s) => s.trim())
|
|
75
|
+
.filter((s) => s.length > 0);
|
|
76
|
+
}
|
|
77
|
+
return [];
|
|
78
|
+
}
|
|
79
|
+
setOutputParam(params, name, value) {
|
|
80
|
+
const existing = params.Params.find((p) => p.Name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
81
|
+
if (existing) {
|
|
82
|
+
existing.Value = value;
|
|
83
|
+
}
|
|
84
|
+
else {
|
|
85
|
+
params.Params.push({
|
|
86
|
+
Name: name,
|
|
87
|
+
Value: value,
|
|
88
|
+
Type: 'Output',
|
|
89
|
+
});
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
async resolveAgentAndUserPolicy(params, contextUser) {
|
|
93
|
+
await AIEngine.Instance.Config(false, contextUser);
|
|
94
|
+
const rawAgentId = this.getStringParam(params, 'AgentID');
|
|
95
|
+
const rawAgentName = this.getStringParam(params, 'AgentName') ?? 'Chief of Staff';
|
|
96
|
+
const agent = rawAgentId
|
|
97
|
+
? AIEngine.Instance.Agents.find((a) => UUIDsEqual(a.ID, rawAgentId))
|
|
98
|
+
: AIEngine.Instance.Agents.find((a) => a.Name.trim().toLowerCase() === rawAgentName.trim().toLowerCase());
|
|
99
|
+
if (!agent) {
|
|
100
|
+
return { agentId: null, policy: null };
|
|
101
|
+
}
|
|
102
|
+
const policy = await AgentUserPolicyEngine.Instance.GetPolicyForAgentAndUser(agent.ID, contextUser.ID, contextUser);
|
|
103
|
+
return { agentId: agent.ID, policy };
|
|
104
|
+
}
|
|
105
|
+
async findUserUtilityMailbox(contextUser, agentId) {
|
|
106
|
+
try {
|
|
107
|
+
const rv = new RunView();
|
|
108
|
+
const safeUserId = contextUser.ID.replace(/'/g, "''");
|
|
109
|
+
const filterParts = [
|
|
110
|
+
`UserID='${safeUserId}'`,
|
|
111
|
+
`IdentityType='Email'`,
|
|
112
|
+
`IsActive=1`,
|
|
113
|
+
];
|
|
114
|
+
if (agentId) {
|
|
115
|
+
const safeAgentId = agentId.replace(/'/g, "''");
|
|
116
|
+
filterParts.push(`AgentID='${safeAgentId}'`);
|
|
117
|
+
}
|
|
118
|
+
const res = await rv.RunView({
|
|
119
|
+
EntityName: 'MJ: AI Bridge Agent Identities',
|
|
120
|
+
ExtraFilter: filterParts.join(' AND '),
|
|
121
|
+
MaxRows: 1,
|
|
122
|
+
ResultType: 'entity_object',
|
|
123
|
+
}, contextUser);
|
|
124
|
+
if (res.Success && res.Results.length > 0) {
|
|
125
|
+
return res.Results[0];
|
|
126
|
+
}
|
|
127
|
+
}
|
|
128
|
+
catch {
|
|
129
|
+
// Non-fatal in environments without bridge rows
|
|
130
|
+
}
|
|
131
|
+
return null;
|
|
132
|
+
}
|
|
133
|
+
static IsEmailDomainAllowed(email, allowedDomainsRaw) {
|
|
134
|
+
if (!allowedDomainsRaw || allowedDomainsRaw.trim().length === 0) {
|
|
135
|
+
return true;
|
|
136
|
+
}
|
|
137
|
+
const atIdx = email.lastIndexOf('@');
|
|
138
|
+
if (atIdx < 0) {
|
|
139
|
+
return false;
|
|
140
|
+
}
|
|
141
|
+
const userDomain = email.slice(atIdx + 1).trim().toLowerCase();
|
|
142
|
+
const allowedDomains = allowedDomainsRaw
|
|
143
|
+
.split(',')
|
|
144
|
+
.map((d) => d.trim().toLowerCase().replace(/^@/, ''))
|
|
145
|
+
.filter((d) => d.length > 0);
|
|
146
|
+
if (allowedDomains.length === 0) {
|
|
147
|
+
return true;
|
|
148
|
+
}
|
|
149
|
+
return allowedDomains.includes(userDomain);
|
|
150
|
+
}
|
|
151
|
+
async evaluateEnterpriseMailboxGate(providerEntity, providerInstance, contextUser, channel, preferUtilityMailbox, agentId) {
|
|
152
|
+
const scopeMode = providerEntity.EnterpriseScopeMode ?? 'UtilityMailboxesOnly';
|
|
153
|
+
const userEmail = contextUser.Email?.trim().toLowerCase() ?? '';
|
|
154
|
+
const utilityIdentity = await this.findUserUtilityMailbox(contextUser, agentId);
|
|
155
|
+
const utilityAddress = utilityIdentity?.IdentityValue?.trim().toLowerCase() ?? null;
|
|
156
|
+
if ((preferUtilityMailbox || scopeMode === 'UtilityMailboxesOnly') &&
|
|
157
|
+
channel === 'Mail' &&
|
|
158
|
+
utilityAddress) {
|
|
159
|
+
return {
|
|
160
|
+
Allowed: true,
|
|
161
|
+
TargetMailbox: utilityAddress,
|
|
162
|
+
UsingUtilityMailbox: true,
|
|
163
|
+
ScopeMode: scopeMode,
|
|
164
|
+
ReasonCode: 'ALLOWED_UTILITY_MAILBOX',
|
|
165
|
+
Message: `Using dedicated agent utility mailbox '${utilityAddress}' under EnterpriseScopeMode='${scopeMode}'.`,
|
|
166
|
+
CanReadMail: true,
|
|
167
|
+
CanReadCalendar: false,
|
|
168
|
+
};
|
|
169
|
+
}
|
|
170
|
+
if (!userEmail) {
|
|
171
|
+
return {
|
|
172
|
+
Allowed: false,
|
|
173
|
+
TargetMailbox: '',
|
|
174
|
+
UsingUtilityMailbox: false,
|
|
175
|
+
ScopeMode: scopeMode,
|
|
176
|
+
ReasonCode: 'MISSING_USER_EMAIL',
|
|
177
|
+
Message: 'Authenticated user does not have an email address on their MJ User profile.',
|
|
178
|
+
CanReadMail: false,
|
|
179
|
+
CanReadCalendar: false,
|
|
180
|
+
};
|
|
181
|
+
}
|
|
182
|
+
if (!BaseUserScopedCommunicationAction.IsEmailDomainAllowed(userEmail, providerEntity.AllowedEmailDomains)) {
|
|
183
|
+
return {
|
|
184
|
+
Allowed: false,
|
|
185
|
+
TargetMailbox: userEmail,
|
|
186
|
+
UsingUtilityMailbox: false,
|
|
187
|
+
ScopeMode: scopeMode,
|
|
188
|
+
ReasonCode: 'DOMAIN_MISMATCH',
|
|
189
|
+
Message: `User email '${userEmail}' does not match provider '${providerEntity.Name}' AllowedEmailDomains (${providerEntity.AllowedEmailDomains}). Personal mailbox access is blocked.`,
|
|
190
|
+
CanReadMail: false,
|
|
191
|
+
CanReadCalendar: false,
|
|
192
|
+
};
|
|
193
|
+
}
|
|
194
|
+
if (scopeMode === 'UtilityMailboxesOnly') {
|
|
195
|
+
return {
|
|
196
|
+
Allowed: false,
|
|
197
|
+
TargetMailbox: userEmail,
|
|
198
|
+
UsingUtilityMailbox: false,
|
|
199
|
+
ScopeMode: scopeMode,
|
|
200
|
+
ReasonCode: 'UTILITY_MAILBOXES_ONLY',
|
|
201
|
+
Message: `Provider '${providerEntity.Name}' is configured with EnterpriseScopeMode='UtilityMailboxesOnly'. Personal user mailbox/calendar access (${userEmail}) is disabled by enterprise policy.`,
|
|
202
|
+
CanReadMail: false,
|
|
203
|
+
CanReadCalendar: false,
|
|
204
|
+
};
|
|
205
|
+
}
|
|
206
|
+
if (!providerInstance.supportsOperation('VerifyMailboxAccess')) {
|
|
207
|
+
return {
|
|
208
|
+
Allowed: true,
|
|
209
|
+
TargetMailbox: userEmail,
|
|
210
|
+
UsingUtilityMailbox: false,
|
|
211
|
+
ScopeMode: scopeMode,
|
|
212
|
+
ReasonCode: 'ALLOWED_PERSONAL_MAILBOX',
|
|
213
|
+
Message: `Personal mailbox '${userEmail}' permitted under EnterpriseScopeMode='${scopeMode}'.`,
|
|
214
|
+
CanReadMail: true,
|
|
215
|
+
CanReadCalendar: Boolean(providerEntity.SupportsCalendar),
|
|
216
|
+
};
|
|
217
|
+
}
|
|
218
|
+
const preflight = await providerInstance.VerifyMailboxAccess({
|
|
219
|
+
Identifier: userEmail,
|
|
220
|
+
CheckMailRead: channel === 'Mail',
|
|
221
|
+
CheckCalendarRead: channel === 'Calendar',
|
|
222
|
+
});
|
|
223
|
+
const channelAllowed = channel === 'Mail' ? preflight.CanReadMail : preflight.CanReadCalendar;
|
|
224
|
+
if (!channelAllowed) {
|
|
225
|
+
return {
|
|
226
|
+
Allowed: false,
|
|
227
|
+
TargetMailbox: userEmail,
|
|
228
|
+
UsingUtilityMailbox: false,
|
|
229
|
+
ScopeMode: scopeMode,
|
|
230
|
+
ReasonCode: 'MAILBOX_ACCESS_DENIED_BY_TENANT_POLICY',
|
|
231
|
+
Message: `Enterprise credential for '${providerEntity.Name}' cannot access ${channel.toLowerCase()} for '${userEmail}' (${preflight.ErrorMessage ?? 'Access denied by Exchange Application Access Policy or Workspace scope'}).`,
|
|
232
|
+
CanReadMail: preflight.CanReadMail,
|
|
233
|
+
CanReadCalendar: preflight.CanReadCalendar,
|
|
234
|
+
};
|
|
235
|
+
}
|
|
236
|
+
return {
|
|
237
|
+
Allowed: true,
|
|
238
|
+
TargetMailbox: userEmail,
|
|
239
|
+
UsingUtilityMailbox: false,
|
|
240
|
+
ScopeMode: scopeMode,
|
|
241
|
+
ReasonCode: 'ALLOWED_PERSONAL_MAILBOX',
|
|
242
|
+
Message: `Verified enterprise access to '${userEmail}' (${channel}) via '${providerEntity.Name}'.`,
|
|
243
|
+
CanReadMail: preflight.CanReadMail,
|
|
244
|
+
CanReadCalendar: preflight.CanReadCalendar,
|
|
245
|
+
};
|
|
246
|
+
}
|
|
247
|
+
async resolveActiveProvider(params, contextUser, requireCalendar) {
|
|
248
|
+
await CommunicationEngine.Instance.Config(false, contextUser);
|
|
249
|
+
const requestedName = this.getStringParam(params, 'ProviderName');
|
|
250
|
+
const activeProviders = CommunicationEngine.Instance.Providers.filter((p) => p.Status === 'Active' && (!requireCalendar || p.SupportsCalendar));
|
|
251
|
+
const chosenEntity = requestedName
|
|
252
|
+
? activeProviders.find((p) => p.Name.trim().toLowerCase() === requestedName.trim().toLowerCase())
|
|
253
|
+
: activeProviders.find((p) => p.Name === 'Microsoft Graph' || p.Name === 'Gmail') ?? activeProviders[0];
|
|
254
|
+
if (!chosenEntity) {
|
|
255
|
+
return null;
|
|
256
|
+
}
|
|
257
|
+
try {
|
|
258
|
+
const instance = CommunicationEngine.Instance.GetProvider(chosenEntity.Name);
|
|
259
|
+
return { providerEntity: chosenEntity, providerInstance: instance };
|
|
260
|
+
}
|
|
261
|
+
catch {
|
|
262
|
+
return null;
|
|
263
|
+
}
|
|
264
|
+
}
|
|
265
|
+
async createApprovalRequest(contextUser, agentId, summary, payloadDetails, provider) {
|
|
266
|
+
try {
|
|
267
|
+
const md = provider ?? new Metadata();
|
|
268
|
+
const req = await md.GetEntityObject('MJ: AI Agent Requests', contextUser);
|
|
269
|
+
req.RequestForUserID = contextUser.ID;
|
|
270
|
+
if (agentId) {
|
|
271
|
+
req.AgentID = agentId;
|
|
272
|
+
}
|
|
273
|
+
req.RequestedAt = new Date();
|
|
274
|
+
req.Status = 'Requested';
|
|
275
|
+
req.Priority = 50;
|
|
276
|
+
req.Request = summary;
|
|
277
|
+
req.Comments = JSON.stringify(payloadDetails, null, 2);
|
|
278
|
+
const saved = await req.Save();
|
|
279
|
+
return saved ? req.ID : null;
|
|
280
|
+
}
|
|
281
|
+
catch {
|
|
282
|
+
return null;
|
|
283
|
+
}
|
|
284
|
+
}
|
|
285
|
+
}
|
|
286
|
+
/**
|
|
287
|
+
* Preflight action that inspects the enterprise communication posture, active providers,
|
|
288
|
+
* the calling user's `MJ: AI Agent User Policies` channel permissions, utility mailboxes,
|
|
289
|
+
* and external scheduling connectors (HubSpot Meetings API v3 / Calendly API v2).
|
|
290
|
+
*/
|
|
291
|
+
let VerifyMyCommunicationCapabilitiesAction = class VerifyMyCommunicationCapabilitiesAction extends BaseUserScopedCommunicationAction {
|
|
292
|
+
async InternalRunAction(params) {
|
|
293
|
+
const contextUser = params.ContextUser;
|
|
294
|
+
if (!contextUser?.ID) {
|
|
295
|
+
return {
|
|
296
|
+
Success: false,
|
|
297
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
298
|
+
Message: 'Verify My Communication Capabilities requires a valid ContextUser.',
|
|
299
|
+
Params: params.Params,
|
|
300
|
+
};
|
|
301
|
+
}
|
|
302
|
+
try {
|
|
303
|
+
await CommunicationEngine.Instance.Config(false, contextUser);
|
|
304
|
+
const { agentId, policy } = await this.resolveAgentAndUserPolicy(params, contextUser);
|
|
305
|
+
const utilityIdentity = await this.findUserUtilityMailbox(contextUser, agentId);
|
|
306
|
+
const providerReports = [];
|
|
307
|
+
const activeProviders = CommunicationEngine.Instance.Providers.filter((p) => p.Status === 'Active');
|
|
308
|
+
for (const prov of activeProviders) {
|
|
309
|
+
let instance = null;
|
|
310
|
+
try {
|
|
311
|
+
instance = CommunicationEngine.Instance.GetProvider(prov.Name);
|
|
312
|
+
}
|
|
313
|
+
catch {
|
|
314
|
+
instance = null;
|
|
315
|
+
}
|
|
316
|
+
if (!instance)
|
|
317
|
+
continue;
|
|
318
|
+
const mailGate = await this.evaluateEnterpriseMailboxGate(prov, instance, contextUser, 'Mail', policy?.EmailChannelMode === 'SendAsAgent', agentId);
|
|
319
|
+
const calendarGate = prov.SupportsCalendar
|
|
320
|
+
? await this.evaluateEnterpriseMailboxGate(prov, instance, contextUser, 'Calendar', false, agentId)
|
|
321
|
+
: null;
|
|
322
|
+
providerReports.push({
|
|
323
|
+
ProviderName: prov.Name,
|
|
324
|
+
EnterpriseScopeMode: prov.EnterpriseScopeMode,
|
|
325
|
+
AllowedEmailDomains: prov.AllowedEmailDomains,
|
|
326
|
+
SupportsCalendar: prov.SupportsCalendar,
|
|
327
|
+
SupportsFreeBusy: prov.SupportsFreeBusy,
|
|
328
|
+
MailGate: mailGate,
|
|
329
|
+
CalendarGate: calendarGate,
|
|
330
|
+
});
|
|
331
|
+
}
|
|
332
|
+
const hubspotConfigured = Boolean(this.getStringParam(params, 'HubSpotAccessToken') ?? process.env['HUBSPOT_ACCESS_TOKEN']);
|
|
333
|
+
const calendlyConfigured = Boolean(this.getStringParam(params, 'CalendlyAccessToken') ?? process.env['CALENDLY_ACCESS_TOKEN']);
|
|
334
|
+
const report = {
|
|
335
|
+
User: {
|
|
336
|
+
ID: contextUser.ID,
|
|
337
|
+
Name: contextUser.Name,
|
|
338
|
+
Email: contextUser.Email,
|
|
339
|
+
},
|
|
340
|
+
UserPolicy: {
|
|
341
|
+
EmailChannelMode: policy?.EmailChannelMode ?? 'DraftOnly',
|
|
342
|
+
CalendarChannelMode: policy?.CalendarChannelMode ?? 'ProposeWithApproval',
|
|
343
|
+
PhoneChannelMode: policy?.PhoneChannelMode ?? 'OutboundWithApproval',
|
|
344
|
+
MessagingChannelMode: policy?.MessagingChannelMode ?? 'Enabled',
|
|
345
|
+
},
|
|
346
|
+
AgentUtilityMailbox: utilityIdentity
|
|
347
|
+
? {
|
|
348
|
+
ID: utilityIdentity.ID,
|
|
349
|
+
Address: utilityIdentity.IdentityValue,
|
|
350
|
+
DisplayName: utilityIdentity.DisplayName,
|
|
351
|
+
}
|
|
352
|
+
: null,
|
|
353
|
+
EnterpriseProviders: providerReports,
|
|
354
|
+
ExternalSchedulingConnectors: {
|
|
355
|
+
HubSpotMeetingsAvailable: hubspotConfigured,
|
|
356
|
+
CalendlyAvailable: calendlyConfigured,
|
|
357
|
+
},
|
|
358
|
+
};
|
|
359
|
+
this.setOutputParam(params, 'CapabilitiesReport', report);
|
|
360
|
+
return {
|
|
361
|
+
Success: true,
|
|
362
|
+
ResultCode: 'SUCCESS',
|
|
363
|
+
Message: `Verified communication capabilities for ${contextUser.Email}: ${providerReports.length} active provider(s), EmailChannelMode=${report.UserPolicy.EmailChannelMode}, CalendarChannelMode=${report.UserPolicy.CalendarChannelMode}.`,
|
|
364
|
+
Params: params.Params,
|
|
365
|
+
};
|
|
366
|
+
}
|
|
367
|
+
catch (error) {
|
|
368
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
369
|
+
LogError(`VerifyMyCommunicationCapabilitiesAction error: ${msg}`);
|
|
370
|
+
return {
|
|
371
|
+
Success: false,
|
|
372
|
+
ResultCode: 'UNEXPECTED_ERROR',
|
|
373
|
+
Message: `Error verifying communication capabilities: ${msg}`,
|
|
374
|
+
Params: params.Params,
|
|
375
|
+
};
|
|
376
|
+
}
|
|
377
|
+
}
|
|
378
|
+
};
|
|
379
|
+
VerifyMyCommunicationCapabilitiesAction = __decorate([
|
|
380
|
+
RegisterClass(BaseAction, '__VerifyMyCommunicationCapabilities'),
|
|
381
|
+
RegisterClass(BaseAction, 'Verify My Communication Capabilities')
|
|
382
|
+
], VerifyMyCommunicationCapabilitiesAction);
|
|
383
|
+
export { VerifyMyCommunicationCapabilitiesAction };
|
|
384
|
+
/**
|
|
385
|
+
* User-scoped action to read the calling user's calendar events, query free/busy schedules,
|
|
386
|
+
* find optimal meeting times across attendees, or check external scheduling availability
|
|
387
|
+
* in HubSpot Meetings API v3 and Calendly API v2.
|
|
388
|
+
*/
|
|
389
|
+
let GetMyCalendarAndAvailabilityAction = class GetMyCalendarAndAvailabilityAction extends BaseUserScopedCommunicationAction {
|
|
390
|
+
async InternalRunAction(params) {
|
|
391
|
+
const contextUser = params.ContextUser;
|
|
392
|
+
if (!contextUser?.ID) {
|
|
393
|
+
return {
|
|
394
|
+
Success: false,
|
|
395
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
396
|
+
Message: 'Get My Calendar Events and Availability requires a valid ContextUser.',
|
|
397
|
+
Params: params.Params,
|
|
398
|
+
};
|
|
399
|
+
}
|
|
400
|
+
try {
|
|
401
|
+
const { agentId, policy } = await this.resolveAgentAndUserPolicy(params, contextUser);
|
|
402
|
+
const calPolicy = AgentUserPolicyEngine.Instance.EvaluateCalendarOperation(policy, false);
|
|
403
|
+
if (calPolicy.verdict === 'Deny') {
|
|
404
|
+
return {
|
|
405
|
+
Success: false,
|
|
406
|
+
ResultCode: 'POLICY_DENIED',
|
|
407
|
+
Message: calPolicy.reason,
|
|
408
|
+
Params: params.Params,
|
|
409
|
+
};
|
|
410
|
+
}
|
|
411
|
+
const operation = this.getStringParam(params, 'Operation') ?? 'GetEvents';
|
|
412
|
+
const startIso = this.getStringParam(params, 'StartDateTime');
|
|
413
|
+
const endIso = this.getStringParam(params, 'EndDateTime');
|
|
414
|
+
const startTime = startIso ? new Date(startIso) : new Date();
|
|
415
|
+
const endTime = endIso ? new Date(endIso) : new Date(startTime.getTime() + 7 * 24 * 60 * 60 * 1000);
|
|
416
|
+
if (operation === 'CheckExternalSchedulingAvailability') {
|
|
417
|
+
return await this.runExternalSchedulingCheck(params, contextUser, startTime, endTime);
|
|
418
|
+
}
|
|
419
|
+
const resolved = await this.resolveActiveProvider(params, contextUser, true);
|
|
420
|
+
if (!resolved) {
|
|
421
|
+
return {
|
|
422
|
+
Success: false,
|
|
423
|
+
ResultCode: 'NO_CALENDAR_PROVIDER',
|
|
424
|
+
Message: 'No active communication provider with SupportsCalendar=true is configured.',
|
|
425
|
+
Params: params.Params,
|
|
426
|
+
};
|
|
427
|
+
}
|
|
428
|
+
const gate = await this.evaluateEnterpriseMailboxGate(resolved.providerEntity, resolved.providerInstance, contextUser, 'Calendar', false, agentId);
|
|
429
|
+
if (!gate.Allowed) {
|
|
430
|
+
return {
|
|
431
|
+
Success: false,
|
|
432
|
+
ResultCode: gate.ReasonCode,
|
|
433
|
+
Message: gate.Message,
|
|
434
|
+
Params: params.Params,
|
|
435
|
+
};
|
|
436
|
+
}
|
|
437
|
+
if (operation === 'GetEvents') {
|
|
438
|
+
const getEventsParams = {
|
|
439
|
+
Identifier: gate.TargetMailbox,
|
|
440
|
+
NumEvents: this.getNumericParam(params, 'MaxEvents') ?? 25,
|
|
441
|
+
StartDateTime: startTime,
|
|
442
|
+
EndDateTime: endTime,
|
|
443
|
+
IncludeCancelled: this.getBooleanParam(params, 'IncludeCancelled') ?? false,
|
|
444
|
+
};
|
|
445
|
+
const res = await resolved.providerInstance.GetEvents(getEventsParams);
|
|
446
|
+
if (!res.Success) {
|
|
447
|
+
return {
|
|
448
|
+
Success: false,
|
|
449
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
450
|
+
Message: res.ErrorMessage ?? 'Failed to retrieve calendar events.',
|
|
451
|
+
Params: params.Params,
|
|
452
|
+
};
|
|
453
|
+
}
|
|
454
|
+
this.setOutputParam(params, 'Events', res.Events);
|
|
455
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
456
|
+
return {
|
|
457
|
+
Success: true,
|
|
458
|
+
ResultCode: 'SUCCESS',
|
|
459
|
+
Message: `Retrieved ${res.Events.length} calendar event(s) for ${gate.TargetMailbox}.`,
|
|
460
|
+
Params: params.Params,
|
|
461
|
+
};
|
|
462
|
+
}
|
|
463
|
+
if (operation === 'GetFreeBusy') {
|
|
464
|
+
const extraSchedules = this.parseStringArrayParam(params, 'AttendeeEmails');
|
|
465
|
+
const schedules = Array.from(new Set([gate.TargetMailbox, ...extraSchedules]));
|
|
466
|
+
const fbParams = {
|
|
467
|
+
Identifier: gate.TargetMailbox,
|
|
468
|
+
Schedules: schedules,
|
|
469
|
+
StartDateTime: startTime,
|
|
470
|
+
EndDateTime: endTime,
|
|
471
|
+
AvailabilityViewIntervalMinutes: this.getNumericParam(params, 'IntervalMinutes') ?? 30,
|
|
472
|
+
};
|
|
473
|
+
const res = await resolved.providerInstance.GetFreeBusy(fbParams);
|
|
474
|
+
if (!res.Success) {
|
|
475
|
+
return {
|
|
476
|
+
Success: false,
|
|
477
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
478
|
+
Message: res.ErrorMessage ?? 'Failed to retrieve free/busy schedules.',
|
|
479
|
+
Params: params.Params,
|
|
480
|
+
};
|
|
481
|
+
}
|
|
482
|
+
this.setOutputParam(params, 'Schedules', res.Schedules);
|
|
483
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
484
|
+
return {
|
|
485
|
+
Success: true,
|
|
486
|
+
ResultCode: 'SUCCESS',
|
|
487
|
+
Message: `Retrieved free/busy availability for ${res.Schedules.length} schedule(s).`,
|
|
488
|
+
Params: params.Params,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
// FindMeetingTimes
|
|
492
|
+
const attendeeEmails = this.parseStringArrayParam(params, 'AttendeeEmails');
|
|
493
|
+
const findParams = {
|
|
494
|
+
Identifier: gate.TargetMailbox,
|
|
495
|
+
Attendees: attendeeEmails.map((email) => ({ Email: email, Required: true })),
|
|
496
|
+
StartDateTime: startTime,
|
|
497
|
+
EndDateTime: endTime,
|
|
498
|
+
DurationMinutes: this.getNumericParam(params, 'DurationMinutes') ?? 30,
|
|
499
|
+
MaxCandidates: this.getNumericParam(params, 'MaxCandidates') ?? 5,
|
|
500
|
+
};
|
|
501
|
+
const res = await resolved.providerInstance.FindMeetingTimes(findParams);
|
|
502
|
+
if (!res.Success) {
|
|
503
|
+
return {
|
|
504
|
+
Success: false,
|
|
505
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
506
|
+
Message: res.ErrorMessage ?? 'Failed to find meeting times.',
|
|
507
|
+
Params: params.Params,
|
|
508
|
+
};
|
|
509
|
+
}
|
|
510
|
+
this.setOutputParam(params, 'Suggestions', res.Suggestions);
|
|
511
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
512
|
+
return {
|
|
513
|
+
Success: true,
|
|
514
|
+
ResultCode: 'SUCCESS',
|
|
515
|
+
Message: `Found ${res.Suggestions.length} candidate meeting slot(s) for ${gate.TargetMailbox}.`,
|
|
516
|
+
Params: params.Params,
|
|
517
|
+
};
|
|
518
|
+
}
|
|
519
|
+
catch (error) {
|
|
520
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
521
|
+
LogError(`GetMyCalendarAndAvailabilityAction error: ${msg}`);
|
|
522
|
+
return {
|
|
523
|
+
Success: false,
|
|
524
|
+
ResultCode: 'UNEXPECTED_ERROR',
|
|
525
|
+
Message: `Error querying calendar/availability: ${msg}`,
|
|
526
|
+
Params: params.Params,
|
|
527
|
+
};
|
|
528
|
+
}
|
|
529
|
+
}
|
|
530
|
+
/**
|
|
531
|
+
* Queries HubSpot Meetings API v3 and/or Calendly API v2 for the calling user's
|
|
532
|
+
* external scheduling links and open availability slots.
|
|
533
|
+
*/
|
|
534
|
+
async runExternalSchedulingCheck(params, contextUser, startTime, endTime) {
|
|
535
|
+
const externalSystem = (this.getStringParam(params, 'ExternalSchedulingSystem') ?? 'All').toLowerCase();
|
|
536
|
+
const userEmail = contextUser.Email?.trim().toLowerCase() ?? '';
|
|
537
|
+
const timezone = this.getStringParam(params, 'TimeZone') ?? 'UTC';
|
|
538
|
+
const slots = [];
|
|
539
|
+
const meetingLinks = [];
|
|
540
|
+
const errors = [];
|
|
541
|
+
// 1. HubSpot Meetings API v3 (`/scheduler/v3/meetings/meeting-links`)
|
|
542
|
+
if (externalSystem === 'all' || externalSystem === 'hubspot') {
|
|
543
|
+
const hubspotToken = this.getStringParam(params, 'HubSpotAccessToken') ?? process.env['HUBSPOT_ACCESS_TOKEN'];
|
|
544
|
+
if (hubspotToken) {
|
|
545
|
+
try {
|
|
546
|
+
const linksResp = await fetch('https://api.hubapi.com/scheduler/v3/meetings/meeting-links', {
|
|
547
|
+
method: 'GET',
|
|
548
|
+
headers: {
|
|
549
|
+
Authorization: `Bearer ${hubspotToken}`,
|
|
550
|
+
Accept: 'application/json',
|
|
551
|
+
},
|
|
552
|
+
});
|
|
553
|
+
if (linksResp.ok) {
|
|
554
|
+
const linksData = (await linksResp.json());
|
|
555
|
+
const allLinks = linksData.results ?? [];
|
|
556
|
+
const requestedSlug = this.getStringParam(params, 'MeetingSlug');
|
|
557
|
+
const targetLinks = requestedSlug
|
|
558
|
+
? allLinks.filter((l) => l.slug === requestedSlug)
|
|
559
|
+
: allLinks;
|
|
560
|
+
for (const linkItem of targetLinks) {
|
|
561
|
+
if (linkItem.link) {
|
|
562
|
+
meetingLinks.push({
|
|
563
|
+
Source: 'HubSpot',
|
|
564
|
+
Name: linkItem.name ?? linkItem.slug ?? 'HubSpot Meeting',
|
|
565
|
+
Url: linkItem.link,
|
|
566
|
+
Slug: linkItem.slug,
|
|
567
|
+
});
|
|
568
|
+
}
|
|
569
|
+
if (linkItem.slug) {
|
|
570
|
+
const availUrl = `https://api.hubapi.com/scheduler/v3/meetings/meeting-links/book/availability-page/${encodeURIComponent(linkItem.slug)}?timezone=${encodeURIComponent(timezone)}`;
|
|
571
|
+
const availResp = await fetch(availUrl, {
|
|
572
|
+
method: 'GET',
|
|
573
|
+
headers: {
|
|
574
|
+
Authorization: `Bearer ${hubspotToken}`,
|
|
575
|
+
Accept: 'application/json',
|
|
576
|
+
},
|
|
577
|
+
});
|
|
578
|
+
if (availResp.ok) {
|
|
579
|
+
const availData = (await availResp.json());
|
|
580
|
+
const byDuration = availData.linkAvailability?.linkAvailabilityByDuration ?? {};
|
|
581
|
+
for (const [durationKey, bucket] of Object.entries(byDuration)) {
|
|
582
|
+
const durMinutes = Math.round(Number(durationKey) / 60000) || 30;
|
|
583
|
+
for (const slot of bucket.availabilities ?? []) {
|
|
584
|
+
if (slot.startMillisUtc) {
|
|
585
|
+
slots.push({
|
|
586
|
+
Source: 'HubSpot',
|
|
587
|
+
StartTimeISO: new Date(slot.startMillisUtc).toISOString(),
|
|
588
|
+
EndTimeISO: slot.endMillisUtc
|
|
589
|
+
? new Date(slot.endMillisUtc).toISOString()
|
|
590
|
+
: undefined,
|
|
591
|
+
DurationMinutes: durMinutes,
|
|
592
|
+
BookingUrl: linkItem.link,
|
|
593
|
+
EventTypeName: linkItem.name,
|
|
594
|
+
});
|
|
595
|
+
}
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
}
|
|
599
|
+
}
|
|
600
|
+
}
|
|
601
|
+
}
|
|
602
|
+
else {
|
|
603
|
+
errors.push(`HubSpot Meetings API returned HTTP ${linksResp.status}`);
|
|
604
|
+
}
|
|
605
|
+
}
|
|
606
|
+
catch (hsErr) {
|
|
607
|
+
errors.push(`HubSpot Meetings API error: ${hsErr instanceof Error ? hsErr.message : String(hsErr)}`);
|
|
608
|
+
}
|
|
609
|
+
}
|
|
610
|
+
}
|
|
611
|
+
// 2. Calendly API v2 (`/users/me`, `/event_types`, `/event_type_available_times`)
|
|
612
|
+
if (externalSystem === 'all' || externalSystem === 'calendly') {
|
|
613
|
+
const calendlyToken = this.getStringParam(params, 'CalendlyAccessToken') ?? process.env['CALENDLY_ACCESS_TOKEN'];
|
|
614
|
+
if (calendlyToken) {
|
|
615
|
+
try {
|
|
616
|
+
const meResp = await fetch('https://api.calendly.com/users/me', {
|
|
617
|
+
method: 'GET',
|
|
618
|
+
headers: {
|
|
619
|
+
Authorization: `Bearer ${calendlyToken}`,
|
|
620
|
+
Accept: 'application/json',
|
|
621
|
+
},
|
|
622
|
+
});
|
|
623
|
+
if (meResp.ok) {
|
|
624
|
+
const meData = (await meResp.json());
|
|
625
|
+
const calUserUri = meData.resource?.uri;
|
|
626
|
+
const calEmail = meData.resource?.email?.trim().toLowerCase();
|
|
627
|
+
if (calEmail && userEmail && calEmail !== userEmail) {
|
|
628
|
+
errors.push(`Calendly token belongs to '${calEmail}', which does not match authenticated user '${userEmail}'. Blocked by identity lock.`);
|
|
629
|
+
}
|
|
630
|
+
else if (calUserUri) {
|
|
631
|
+
const etResp = await fetch(`https://api.calendly.com/event_types?user=${encodeURIComponent(calUserUri)}&active=true`, {
|
|
632
|
+
method: 'GET',
|
|
633
|
+
headers: {
|
|
634
|
+
Authorization: `Bearer ${calendlyToken}`,
|
|
635
|
+
Accept: 'application/json',
|
|
636
|
+
},
|
|
637
|
+
});
|
|
638
|
+
if (etResp.ok) {
|
|
639
|
+
const etData = (await etResp.json());
|
|
640
|
+
for (const et of etData.collection ?? []) {
|
|
641
|
+
if (et.scheduling_url) {
|
|
642
|
+
meetingLinks.push({
|
|
643
|
+
Source: 'Calendly',
|
|
644
|
+
Name: et.name ?? 'Calendly Event',
|
|
645
|
+
Url: et.scheduling_url,
|
|
646
|
+
});
|
|
647
|
+
}
|
|
648
|
+
if (et.uri) {
|
|
649
|
+
const availUrl = `https://api.calendly.com/event_type_available_times?event_type=${encodeURIComponent(et.uri)}&start_time=${encodeURIComponent(startTime.toISOString())}&end_time=${encodeURIComponent(endTime.toISOString())}`;
|
|
650
|
+
const availResp = await fetch(availUrl, {
|
|
651
|
+
method: 'GET',
|
|
652
|
+
headers: {
|
|
653
|
+
Authorization: `Bearer ${calendlyToken}`,
|
|
654
|
+
Accept: 'application/json',
|
|
655
|
+
},
|
|
656
|
+
});
|
|
657
|
+
if (availResp.ok) {
|
|
658
|
+
const availData = (await availResp.json());
|
|
659
|
+
for (const item of availData.collection ?? []) {
|
|
660
|
+
if (item.start_time) {
|
|
661
|
+
slots.push({
|
|
662
|
+
Source: 'Calendly',
|
|
663
|
+
StartTimeISO: item.start_time,
|
|
664
|
+
DurationMinutes: et.duration,
|
|
665
|
+
BookingUrl: item.scheduling_url ?? et.scheduling_url,
|
|
666
|
+
EventTypeName: et.name,
|
|
667
|
+
});
|
|
668
|
+
}
|
|
669
|
+
}
|
|
670
|
+
}
|
|
671
|
+
}
|
|
672
|
+
}
|
|
673
|
+
}
|
|
674
|
+
}
|
|
675
|
+
}
|
|
676
|
+
else {
|
|
677
|
+
errors.push(`Calendly API returned HTTP ${meResp.status}`);
|
|
678
|
+
}
|
|
679
|
+
}
|
|
680
|
+
catch (calErr) {
|
|
681
|
+
errors.push(`Calendly API error: ${calErr instanceof Error ? calErr.message : String(calErr)}`);
|
|
682
|
+
}
|
|
683
|
+
}
|
|
684
|
+
}
|
|
685
|
+
this.setOutputParam(params, 'ExternalAvailabilitySlots', slots);
|
|
686
|
+
this.setOutputParam(params, 'MeetingLinks', meetingLinks);
|
|
687
|
+
if (errors.length > 0) {
|
|
688
|
+
this.setOutputParam(params, 'Warnings', errors);
|
|
689
|
+
}
|
|
690
|
+
return {
|
|
691
|
+
Success: errors.length === 0 || slots.length > 0 || meetingLinks.length > 0,
|
|
692
|
+
ResultCode: errors.length > 0 && slots.length === 0 ? 'EXTERNAL_SCHEDULING_ERROR' : 'SUCCESS',
|
|
693
|
+
Message: slots.length > 0 || meetingLinks.length > 0
|
|
694
|
+
? `Found ${meetingLinks.length} scheduling link(s) and ${slots.length} open slot(s) across external scheduling systems.`
|
|
695
|
+
: errors.join('; ') || 'No external scheduling credentials (HubSpot/Calendly) configured.',
|
|
696
|
+
Params: params.Params,
|
|
697
|
+
};
|
|
698
|
+
}
|
|
699
|
+
};
|
|
700
|
+
GetMyCalendarAndAvailabilityAction = __decorate([
|
|
701
|
+
RegisterClass(BaseAction, '__GetMyCalendarAndAvailability'),
|
|
702
|
+
RegisterClass(BaseAction, 'Get My Calendar Events and Availability')
|
|
703
|
+
], GetMyCalendarAndAvailabilityAction);
|
|
704
|
+
export { GetMyCalendarAndAvailabilityAction };
|
|
705
|
+
/**
|
|
706
|
+
* User-scoped action to create, update, respond to (accept/tentativelyAccept/decline), or cancel
|
|
707
|
+
* a calendar event on the calling user's calendar, or generate a single-use Calendly scheduling link.
|
|
708
|
+
*
|
|
709
|
+
* Enforces `CalendarChannelMode` (`Disabled` | `ReadOnly` | `ProposeWithApproval` | `Autonomous`)
|
|
710
|
+
* and `EnterpriseMailboxSecurityGate`.
|
|
711
|
+
*/
|
|
712
|
+
let ManageMyCalendarEventAction = class ManageMyCalendarEventAction extends BaseUserScopedCommunicationAction {
|
|
713
|
+
async InternalRunAction(params) {
|
|
714
|
+
const contextUser = params.ContextUser;
|
|
715
|
+
if (!contextUser?.ID) {
|
|
716
|
+
return {
|
|
717
|
+
Success: false,
|
|
718
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
719
|
+
Message: 'Manage My Calendar Event requires a valid ContextUser.',
|
|
720
|
+
Params: params.Params,
|
|
721
|
+
};
|
|
722
|
+
}
|
|
723
|
+
try {
|
|
724
|
+
const { agentId, policy } = await this.resolveAgentAndUserPolicy(params, contextUser);
|
|
725
|
+
const calEval = AgentUserPolicyEngine.Instance.EvaluateCalendarOperation(policy, true);
|
|
726
|
+
if (calEval.verdict === 'Deny') {
|
|
727
|
+
return {
|
|
728
|
+
Success: false,
|
|
729
|
+
ResultCode: 'POLICY_DENIED',
|
|
730
|
+
Message: calEval.reason,
|
|
731
|
+
Params: params.Params,
|
|
732
|
+
};
|
|
733
|
+
}
|
|
734
|
+
const operation = this.getStringParam(params, 'Operation') ?? 'Create';
|
|
735
|
+
if (operation === 'CreateCalendlySingleUseLink') {
|
|
736
|
+
return await this.createCalendlySingleUseLink(params);
|
|
737
|
+
}
|
|
738
|
+
const resolved = await this.resolveActiveProvider(params, contextUser, true);
|
|
739
|
+
if (!resolved) {
|
|
740
|
+
return {
|
|
741
|
+
Success: false,
|
|
742
|
+
ResultCode: 'NO_CALENDAR_PROVIDER',
|
|
743
|
+
Message: 'No active communication provider with SupportsCalendar=true is configured.',
|
|
744
|
+
Params: params.Params,
|
|
745
|
+
};
|
|
746
|
+
}
|
|
747
|
+
const gate = await this.evaluateEnterpriseMailboxGate(resolved.providerEntity, resolved.providerInstance, contextUser, 'Calendar', false, agentId);
|
|
748
|
+
if (!gate.Allowed) {
|
|
749
|
+
return {
|
|
750
|
+
Success: false,
|
|
751
|
+
ResultCode: gate.ReasonCode,
|
|
752
|
+
Message: gate.Message,
|
|
753
|
+
Params: params.Params,
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
const confirmedApprovalId = this.getStringParam(params, 'ConfirmedApprovalRequestID');
|
|
757
|
+
const explicitDryRun = this.getBooleanParam(params, 'DryRun') ?? false;
|
|
758
|
+
const requiresApproval = calEval.verdict === 'RequireApproval' && !confirmedApprovalId;
|
|
759
|
+
const isDryRun = explicitDryRun || requiresApproval;
|
|
760
|
+
if (operation === 'Create') {
|
|
761
|
+
const subject = this.getStringParam(params, 'Subject') ?? 'Meeting';
|
|
762
|
+
const startIso = this.getStringParam(params, 'StartTime');
|
|
763
|
+
const endIso = this.getStringParam(params, 'EndTime');
|
|
764
|
+
if (!startIso || !endIso) {
|
|
765
|
+
return {
|
|
766
|
+
Success: false,
|
|
767
|
+
ResultCode: 'MISSING_PARAMETERS',
|
|
768
|
+
Message: 'Create calendar event requires StartTime and EndTime (ISO-8601).',
|
|
769
|
+
Params: params.Params,
|
|
770
|
+
};
|
|
771
|
+
}
|
|
772
|
+
const attendeeEmails = this.parseStringArrayParam(params, 'AttendeeEmails');
|
|
773
|
+
const createParams = {
|
|
774
|
+
Identifier: gate.TargetMailbox,
|
|
775
|
+
Subject: subject,
|
|
776
|
+
Body: this.getStringParam(params, 'Body') ?? undefined,
|
|
777
|
+
IsHTML: this.getBooleanParam(params, 'IsHTML') ?? false,
|
|
778
|
+
StartTime: new Date(startIso),
|
|
779
|
+
EndTime: new Date(endIso),
|
|
780
|
+
TimeZone: this.getStringParam(params, 'TimeZone') ?? 'UTC',
|
|
781
|
+
Location: this.getStringParam(params, 'Location') ?? undefined,
|
|
782
|
+
Attendees: attendeeEmails.map((email) => ({ Email: email, Required: true })),
|
|
783
|
+
IsOnlineMeeting: this.getBooleanParam(params, 'IsOnlineMeeting') ?? true,
|
|
784
|
+
DryRun: isDryRun,
|
|
785
|
+
};
|
|
786
|
+
const res = await resolved.providerInstance.CreateCalendarEvent(createParams);
|
|
787
|
+
if (!res.Success) {
|
|
788
|
+
return {
|
|
789
|
+
Success: false,
|
|
790
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
791
|
+
Message: res.ErrorMessage ?? 'Failed to create calendar event.',
|
|
792
|
+
Params: params.Params,
|
|
793
|
+
};
|
|
794
|
+
}
|
|
795
|
+
if (requiresApproval) {
|
|
796
|
+
const reqId = await this.createApprovalRequest(contextUser, agentId, `Approve calendar invite: "${subject}" (${startIso} - ${endIso})`, {
|
|
797
|
+
Operation: 'CreateCalendarEvent',
|
|
798
|
+
TargetMailbox: gate.TargetMailbox,
|
|
799
|
+
EventPreview: res.Event,
|
|
800
|
+
AttendeeEmails: attendeeEmails,
|
|
801
|
+
});
|
|
802
|
+
this.setOutputParam(params, 'ApprovalRequestID', reqId);
|
|
803
|
+
this.setOutputParam(params, 'EventPreview', res.Event);
|
|
804
|
+
return {
|
|
805
|
+
Success: true,
|
|
806
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
807
|
+
Message: `CalendarChannelMode is 'ProposeWithApproval'. Created dry-run preview for '${subject}' and queued approval request${reqId ? ` (${reqId})` : ''}.`,
|
|
808
|
+
Params: params.Params,
|
|
809
|
+
};
|
|
810
|
+
}
|
|
811
|
+
this.setOutputParam(params, 'Event', res.Event);
|
|
812
|
+
this.setOutputParam(params, 'OnlineMeetingUrl', res.OnlineMeetingUrl);
|
|
813
|
+
return {
|
|
814
|
+
Success: true,
|
|
815
|
+
ResultCode: isDryRun ? 'DRY_RUN_SUCCESS' : 'SUCCESS',
|
|
816
|
+
Message: `Created calendar event '${subject}' on ${gate.TargetMailbox}.`,
|
|
817
|
+
Params: params.Params,
|
|
818
|
+
};
|
|
819
|
+
}
|
|
820
|
+
const eventId = this.getStringParam(params, 'EventID');
|
|
821
|
+
if (!eventId) {
|
|
822
|
+
return {
|
|
823
|
+
Success: false,
|
|
824
|
+
ResultCode: 'MISSING_PARAMETERS',
|
|
825
|
+
Message: `Calendar operation '${operation}' requires EventID.`,
|
|
826
|
+
Params: params.Params,
|
|
827
|
+
};
|
|
828
|
+
}
|
|
829
|
+
if (operation === 'Update') {
|
|
830
|
+
const startIso = this.getStringParam(params, 'StartTime');
|
|
831
|
+
const endIso = this.getStringParam(params, 'EndTime');
|
|
832
|
+
const attendeeEmails = this.parseStringArrayParam(params, 'AttendeeEmails');
|
|
833
|
+
const updateParams = {
|
|
834
|
+
Identifier: gate.TargetMailbox,
|
|
835
|
+
EventID: eventId,
|
|
836
|
+
Subject: this.getStringParam(params, 'Subject') ?? undefined,
|
|
837
|
+
Body: this.getStringParam(params, 'Body') ?? undefined,
|
|
838
|
+
StartTime: startIso ? new Date(startIso) : undefined,
|
|
839
|
+
EndTime: endIso ? new Date(endIso) : undefined,
|
|
840
|
+
TimeZone: this.getStringParam(params, 'TimeZone') ?? undefined,
|
|
841
|
+
Location: this.getStringParam(params, 'Location') ?? undefined,
|
|
842
|
+
Attendees: attendeeEmails.length > 0
|
|
843
|
+
? attendeeEmails.map((email) => ({ Email: email, Required: true }))
|
|
844
|
+
: undefined,
|
|
845
|
+
DryRun: isDryRun,
|
|
846
|
+
};
|
|
847
|
+
const res = await resolved.providerInstance.UpdateCalendarEvent(updateParams);
|
|
848
|
+
if (!res.Success) {
|
|
849
|
+
return {
|
|
850
|
+
Success: false,
|
|
851
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
852
|
+
Message: res.ErrorMessage ?? 'Failed to update calendar event.',
|
|
853
|
+
Params: params.Params,
|
|
854
|
+
};
|
|
855
|
+
}
|
|
856
|
+
if (requiresApproval) {
|
|
857
|
+
const reqId = await this.createApprovalRequest(contextUser, agentId, `Approve calendar update for event ${eventId}`, { Operation: 'UpdateCalendarEvent', EventID: eventId, Updates: updateParams });
|
|
858
|
+
this.setOutputParam(params, 'ApprovalRequestID', reqId);
|
|
859
|
+
return {
|
|
860
|
+
Success: true,
|
|
861
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
862
|
+
Message: `Queued approval request${reqId ? ` (${reqId})` : ''} to update calendar event '${eventId}'.`,
|
|
863
|
+
Params: params.Params,
|
|
864
|
+
};
|
|
865
|
+
}
|
|
866
|
+
this.setOutputParam(params, 'Event', res.Event);
|
|
867
|
+
return {
|
|
868
|
+
Success: true,
|
|
869
|
+
ResultCode: 'SUCCESS',
|
|
870
|
+
Message: `Updated calendar event '${eventId}' on ${gate.TargetMailbox}.`,
|
|
871
|
+
Params: params.Params,
|
|
872
|
+
};
|
|
873
|
+
}
|
|
874
|
+
if (operation === 'Respond') {
|
|
875
|
+
const responseType = this.getStringParam(params, 'Response') ??
|
|
876
|
+
'Accept';
|
|
877
|
+
const rsvpParams = {
|
|
878
|
+
Identifier: gate.TargetMailbox,
|
|
879
|
+
EventID: eventId,
|
|
880
|
+
Response: responseType,
|
|
881
|
+
Comment: this.getStringParam(params, 'Comment') ?? undefined,
|
|
882
|
+
SendResponse: this.getBooleanParam(params, 'SendResponse') ?? true,
|
|
883
|
+
DryRun: isDryRun,
|
|
884
|
+
};
|
|
885
|
+
const res = await resolved.providerInstance.RespondToCalendarEvent(rsvpParams);
|
|
886
|
+
if (!res.Success) {
|
|
887
|
+
return {
|
|
888
|
+
Success: false,
|
|
889
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
890
|
+
Message: res.ErrorMessage ?? 'Failed to respond to calendar event.',
|
|
891
|
+
Params: params.Params,
|
|
892
|
+
};
|
|
893
|
+
}
|
|
894
|
+
if (requiresApproval) {
|
|
895
|
+
const reqId = await this.createApprovalRequest(contextUser, agentId, `Approve RSVP '${responseType}' for event ${eventId}`, { Operation: 'RespondToCalendarEvent', EventID: eventId, Response: responseType });
|
|
896
|
+
this.setOutputParam(params, 'ApprovalRequestID', reqId);
|
|
897
|
+
return {
|
|
898
|
+
Success: true,
|
|
899
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
900
|
+
Message: `Queued approval request${reqId ? ` (${reqId})` : ''} to RSVP '${responseType}' to event '${eventId}'.`,
|
|
901
|
+
Params: params.Params,
|
|
902
|
+
};
|
|
903
|
+
}
|
|
904
|
+
return {
|
|
905
|
+
Success: true,
|
|
906
|
+
ResultCode: 'SUCCESS',
|
|
907
|
+
Message: `Responded '${responseType}' to calendar event '${eventId}'.`,
|
|
908
|
+
Params: params.Params,
|
|
909
|
+
};
|
|
910
|
+
}
|
|
911
|
+
// Cancel
|
|
912
|
+
const cancelRes = await resolved.providerInstance.CancelCalendarEvent({
|
|
913
|
+
Identifier: gate.TargetMailbox,
|
|
914
|
+
EventID: eventId,
|
|
915
|
+
Comment: this.getStringParam(params, 'Comment') ?? undefined,
|
|
916
|
+
DryRun: isDryRun,
|
|
917
|
+
});
|
|
918
|
+
if (!cancelRes.Success) {
|
|
919
|
+
return {
|
|
920
|
+
Success: false,
|
|
921
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
922
|
+
Message: cancelRes.ErrorMessage ?? 'Failed to cancel calendar event.',
|
|
923
|
+
Params: params.Params,
|
|
924
|
+
};
|
|
925
|
+
}
|
|
926
|
+
if (requiresApproval) {
|
|
927
|
+
const reqId = await this.createApprovalRequest(contextUser, agentId, `Approve cancellation of calendar event ${eventId}`, { Operation: 'CancelCalendarEvent', EventID: eventId });
|
|
928
|
+
this.setOutputParam(params, 'ApprovalRequestID', reqId);
|
|
929
|
+
return {
|
|
930
|
+
Success: true,
|
|
931
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
932
|
+
Message: `Queued approval request${reqId ? ` (${reqId})` : ''} to cancel event '${eventId}'.`,
|
|
933
|
+
Params: params.Params,
|
|
934
|
+
};
|
|
935
|
+
}
|
|
936
|
+
return {
|
|
937
|
+
Success: true,
|
|
938
|
+
ResultCode: 'SUCCESS',
|
|
939
|
+
Message: `Cancelled calendar event '${eventId}' on ${gate.TargetMailbox}.`,
|
|
940
|
+
Params: params.Params,
|
|
941
|
+
};
|
|
942
|
+
}
|
|
943
|
+
catch (error) {
|
|
944
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
945
|
+
LogError(`ManageMyCalendarEventAction error: ${msg}`);
|
|
946
|
+
return {
|
|
947
|
+
Success: false,
|
|
948
|
+
ResultCode: 'UNEXPECTED_ERROR',
|
|
949
|
+
Message: `Error managing calendar event: ${msg}`,
|
|
950
|
+
Params: params.Params,
|
|
951
|
+
};
|
|
952
|
+
}
|
|
953
|
+
}
|
|
954
|
+
async createCalendlySingleUseLink(params) {
|
|
955
|
+
const calendlyToken = this.getStringParam(params, 'CalendlyAccessToken') ?? process.env['CALENDLY_ACCESS_TOKEN'];
|
|
956
|
+
const eventTypeUri = this.getStringParam(params, 'CalendlyEventTypeUri');
|
|
957
|
+
if (!calendlyToken || !eventTypeUri) {
|
|
958
|
+
return {
|
|
959
|
+
Success: false,
|
|
960
|
+
ResultCode: 'MISSING_PARAMETERS',
|
|
961
|
+
Message: 'CreateCalendlySingleUseLink requires CalendlyEventTypeUri and a configured Calendly access token.',
|
|
962
|
+
Params: params.Params,
|
|
963
|
+
};
|
|
964
|
+
}
|
|
965
|
+
const resp = await fetch('https://api.calendly.com/scheduling_links', {
|
|
966
|
+
method: 'POST',
|
|
967
|
+
headers: {
|
|
968
|
+
Authorization: `Bearer ${calendlyToken}`,
|
|
969
|
+
'Content-Type': 'application/json',
|
|
970
|
+
Accept: 'application/json',
|
|
971
|
+
},
|
|
972
|
+
body: JSON.stringify({
|
|
973
|
+
max_event_count: 1,
|
|
974
|
+
owner: eventTypeUri,
|
|
975
|
+
owner_type: 'EventType',
|
|
976
|
+
}),
|
|
977
|
+
});
|
|
978
|
+
if (!resp.ok) {
|
|
979
|
+
return {
|
|
980
|
+
Success: false,
|
|
981
|
+
ResultCode: 'CALENDLY_ERROR',
|
|
982
|
+
Message: `Calendly scheduling_links returned HTTP ${resp.status}`,
|
|
983
|
+
Params: params.Params,
|
|
984
|
+
};
|
|
985
|
+
}
|
|
986
|
+
const data = (await resp.json());
|
|
987
|
+
const bookingUrl = data.resource?.booking_url ?? null;
|
|
988
|
+
this.setOutputParam(params, 'BookingUrl', bookingUrl);
|
|
989
|
+
return {
|
|
990
|
+
Success: true,
|
|
991
|
+
ResultCode: 'SUCCESS',
|
|
992
|
+
Message: `Generated single-use Calendly booking link: ${bookingUrl}`,
|
|
993
|
+
Params: params.Params,
|
|
994
|
+
};
|
|
995
|
+
}
|
|
996
|
+
};
|
|
997
|
+
ManageMyCalendarEventAction = __decorate([
|
|
998
|
+
RegisterClass(BaseAction, '__ManageMyCalendarEvent'),
|
|
999
|
+
RegisterClass(BaseAction, 'Manage My Calendar Event')
|
|
1000
|
+
], ManageMyCalendarEventAction);
|
|
1001
|
+
export { ManageMyCalendarEventAction };
|
|
1002
|
+
/**
|
|
1003
|
+
* User-scoped action to read messages, search threads, or create/reply/forward drafts
|
|
1004
|
+
* in the calling user's personal mailbox or dedicated agent utility mailbox.
|
|
1005
|
+
*
|
|
1006
|
+
* Security Invariant:
|
|
1007
|
+
* - Never accepts an arbitrary `From` or `Mailbox` parameter from the LLM.
|
|
1008
|
+
* - Enforces `EmailChannelMode` and `EnterpriseMailboxSecurityGate`.
|
|
1009
|
+
*/
|
|
1010
|
+
let ManageMyEmailInboxAction = class ManageMyEmailInboxAction extends BaseUserScopedCommunicationAction {
|
|
1011
|
+
async InternalRunAction(params) {
|
|
1012
|
+
const contextUser = params.ContextUser;
|
|
1013
|
+
if (!contextUser?.ID) {
|
|
1014
|
+
return {
|
|
1015
|
+
Success: false,
|
|
1016
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
1017
|
+
Message: 'Manage My Email Inbox and Drafts requires a valid ContextUser.',
|
|
1018
|
+
Params: params.Params,
|
|
1019
|
+
};
|
|
1020
|
+
}
|
|
1021
|
+
try {
|
|
1022
|
+
const { agentId, policy } = await this.resolveAgentAndUserPolicy(params, contextUser);
|
|
1023
|
+
const operation = this.getStringParam(params, 'Operation') ?? 'ListMessages';
|
|
1024
|
+
const opCategory = operation === 'ListMessages' || operation === 'SearchMessages' || operation === 'GetSingleMessage'
|
|
1025
|
+
? 'Read'
|
|
1026
|
+
: operation === 'CreateDraft'
|
|
1027
|
+
? 'Draft'
|
|
1028
|
+
: 'Send';
|
|
1029
|
+
const emailEval = AgentUserPolicyEngine.Instance.EvaluateEmailOperation(policy, opCategory);
|
|
1030
|
+
if (emailEval.verdict === 'Deny') {
|
|
1031
|
+
return {
|
|
1032
|
+
Success: false,
|
|
1033
|
+
ResultCode: 'POLICY_DENIED',
|
|
1034
|
+
Message: emailEval.reason,
|
|
1035
|
+
Params: params.Params,
|
|
1036
|
+
};
|
|
1037
|
+
}
|
|
1038
|
+
const resolved = await this.resolveActiveProvider(params, contextUser, false);
|
|
1039
|
+
if (!resolved) {
|
|
1040
|
+
return {
|
|
1041
|
+
Success: false,
|
|
1042
|
+
ResultCode: 'NO_EMAIL_PROVIDER',
|
|
1043
|
+
Message: 'No active communication provider is configured.',
|
|
1044
|
+
Params: params.Params,
|
|
1045
|
+
};
|
|
1046
|
+
}
|
|
1047
|
+
const preferUtility = policy?.EmailChannelMode === 'SendAsAgent';
|
|
1048
|
+
const gate = await this.evaluateEnterpriseMailboxGate(resolved.providerEntity, resolved.providerInstance, contextUser, 'Mail', preferUtility, agentId);
|
|
1049
|
+
if (!gate.Allowed) {
|
|
1050
|
+
return {
|
|
1051
|
+
Success: false,
|
|
1052
|
+
ResultCode: gate.ReasonCode,
|
|
1053
|
+
Message: gate.Message,
|
|
1054
|
+
Params: params.Params,
|
|
1055
|
+
};
|
|
1056
|
+
}
|
|
1057
|
+
if (operation === 'ListMessages') {
|
|
1058
|
+
const res = await resolved.providerInstance.GetMessages({
|
|
1059
|
+
Identifier: gate.TargetMailbox,
|
|
1060
|
+
NumMessages: this.getNumericParam(params, 'MaxMessages') ?? 20,
|
|
1061
|
+
UnreadOnly: this.getBooleanParam(params, 'UnreadOnly') ?? false,
|
|
1062
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1063
|
+
});
|
|
1064
|
+
if (!res.Success) {
|
|
1065
|
+
return {
|
|
1066
|
+
Success: false,
|
|
1067
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1068
|
+
Message: res.ErrorMessage ?? 'Failed to list messages.',
|
|
1069
|
+
Params: params.Params,
|
|
1070
|
+
};
|
|
1071
|
+
}
|
|
1072
|
+
this.setOutputParam(params, 'Messages', res.Messages);
|
|
1073
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
1074
|
+
return {
|
|
1075
|
+
Success: true,
|
|
1076
|
+
ResultCode: 'SUCCESS',
|
|
1077
|
+
Message: `Retrieved ${res.Messages.length} message(s) for ${gate.TargetMailbox}.`,
|
|
1078
|
+
Params: params.Params,
|
|
1079
|
+
};
|
|
1080
|
+
}
|
|
1081
|
+
if (operation === 'SearchMessages') {
|
|
1082
|
+
const query = this.getStringParam(params, 'Query') ?? '';
|
|
1083
|
+
const res = await resolved.providerInstance.SearchMessages({
|
|
1084
|
+
Query: query,
|
|
1085
|
+
MaxResults: this.getNumericParam(params, 'MaxMessages') ?? 20,
|
|
1086
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1087
|
+
});
|
|
1088
|
+
if (!res.Success) {
|
|
1089
|
+
return {
|
|
1090
|
+
Success: false,
|
|
1091
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1092
|
+
Message: res.ErrorMessage ?? 'Failed to search messages.',
|
|
1093
|
+
Params: params.Params,
|
|
1094
|
+
};
|
|
1095
|
+
}
|
|
1096
|
+
this.setOutputParam(params, 'Messages', res.Messages ?? []);
|
|
1097
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
1098
|
+
return {
|
|
1099
|
+
Success: true,
|
|
1100
|
+
ResultCode: 'SUCCESS',
|
|
1101
|
+
Message: `Found ${res.Messages?.length ?? 0} message(s) matching '${query}' in ${gate.TargetMailbox}.`,
|
|
1102
|
+
Params: params.Params,
|
|
1103
|
+
};
|
|
1104
|
+
}
|
|
1105
|
+
if (operation === 'GetSingleMessage') {
|
|
1106
|
+
const messageId = this.getStringParam(params, 'MessageID');
|
|
1107
|
+
if (!messageId) {
|
|
1108
|
+
return {
|
|
1109
|
+
Success: false,
|
|
1110
|
+
ResultCode: 'MISSING_PARAMETERS',
|
|
1111
|
+
Message: 'GetSingleMessage requires MessageID.',
|
|
1112
|
+
Params: params.Params,
|
|
1113
|
+
};
|
|
1114
|
+
}
|
|
1115
|
+
const res = await resolved.providerInstance.GetSingleMessage({
|
|
1116
|
+
MessageID: messageId,
|
|
1117
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1118
|
+
});
|
|
1119
|
+
if (!res.Success) {
|
|
1120
|
+
return {
|
|
1121
|
+
Success: false,
|
|
1122
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1123
|
+
Message: res.ErrorMessage ?? 'Failed to get message.',
|
|
1124
|
+
Params: params.Params,
|
|
1125
|
+
};
|
|
1126
|
+
}
|
|
1127
|
+
this.setOutputParam(params, 'Message', res.Message);
|
|
1128
|
+
return {
|
|
1129
|
+
Success: true,
|
|
1130
|
+
ResultCode: 'SUCCESS',
|
|
1131
|
+
Message: `Retrieved message '${messageId}' from ${gate.TargetMailbox}.`,
|
|
1132
|
+
Params: params.Params,
|
|
1133
|
+
};
|
|
1134
|
+
}
|
|
1135
|
+
// CreateDraft / Reply / Forward
|
|
1136
|
+
const to = this.getStringParam(params, 'To') ?? '';
|
|
1137
|
+
const subject = this.getStringParam(params, 'Subject') ?? '';
|
|
1138
|
+
const body = this.getStringParam(params, 'Body') ?? '';
|
|
1139
|
+
const cc = this.parseStringArrayParam(params, 'CCRecipients');
|
|
1140
|
+
const draftMessage = {
|
|
1141
|
+
From: gate.TargetMailbox,
|
|
1142
|
+
To: to,
|
|
1143
|
+
Subject: subject,
|
|
1144
|
+
ProcessedSubject: subject,
|
|
1145
|
+
Body: body,
|
|
1146
|
+
ProcessedBody: body,
|
|
1147
|
+
HTMLBody: this.getBooleanParam(params, 'IsHTML') ? body : '',
|
|
1148
|
+
ProcessedHTMLBody: this.getBooleanParam(params, 'IsHTML') ? body : '',
|
|
1149
|
+
CCRecipients: cc,
|
|
1150
|
+
BCCRecipients: [],
|
|
1151
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1152
|
+
};
|
|
1153
|
+
if (operation === 'CreateDraft') {
|
|
1154
|
+
const draftRes = await resolved.providerInstance.CreateDraft({
|
|
1155
|
+
Message: draftMessage,
|
|
1156
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1157
|
+
});
|
|
1158
|
+
if (!draftRes.Success) {
|
|
1159
|
+
return {
|
|
1160
|
+
Success: false,
|
|
1161
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1162
|
+
Message: draftRes.ErrorMessage ?? 'Failed to create draft.',
|
|
1163
|
+
Params: params.Params,
|
|
1164
|
+
};
|
|
1165
|
+
}
|
|
1166
|
+
this.setOutputParam(params, 'DraftID', draftRes.DraftID);
|
|
1167
|
+
this.setOutputParam(params, 'TargetMailbox', gate.TargetMailbox);
|
|
1168
|
+
return {
|
|
1169
|
+
Success: true,
|
|
1170
|
+
ResultCode: 'SUCCESS',
|
|
1171
|
+
Message: `Created email draft (${draftRes.DraftID ?? 'saved'}) in ${gate.TargetMailbox}.`,
|
|
1172
|
+
Params: params.Params,
|
|
1173
|
+
};
|
|
1174
|
+
}
|
|
1175
|
+
if (emailEval.verdict === 'RequireApproval' && !this.getStringParam(params, 'ConfirmedApprovalRequestID')) {
|
|
1176
|
+
const reqId = await this.createApprovalRequest(contextUser, agentId, `Approve outbound email (${operation}) from ${gate.TargetMailbox} to ${to}`, {
|
|
1177
|
+
Operation: operation,
|
|
1178
|
+
From: gate.TargetMailbox,
|
|
1179
|
+
To: to,
|
|
1180
|
+
Subject: subject,
|
|
1181
|
+
Body: body,
|
|
1182
|
+
});
|
|
1183
|
+
this.setOutputParam(params, 'ApprovalRequestID', reqId);
|
|
1184
|
+
return {
|
|
1185
|
+
Success: true,
|
|
1186
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
1187
|
+
Message: `Queued approval request${reqId ? ` (${reqId})` : ''} for ${operation} from ${gate.TargetMailbox}.`,
|
|
1188
|
+
Params: params.Params,
|
|
1189
|
+
};
|
|
1190
|
+
}
|
|
1191
|
+
const messageId = this.getStringParam(params, 'MessageID') ?? '';
|
|
1192
|
+
if (operation === 'Reply') {
|
|
1193
|
+
const replyRes = await resolved.providerInstance.ReplyToMessage({
|
|
1194
|
+
MessageID: messageId,
|
|
1195
|
+
Message: draftMessage,
|
|
1196
|
+
ContextData: { Email: gate.TargetMailbox },
|
|
1197
|
+
});
|
|
1198
|
+
if (!replyRes.Success) {
|
|
1199
|
+
return {
|
|
1200
|
+
Success: false,
|
|
1201
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1202
|
+
Message: replyRes.ErrorMessage ?? 'Failed to reply to message.',
|
|
1203
|
+
Params: params.Params,
|
|
1204
|
+
};
|
|
1205
|
+
}
|
|
1206
|
+
return {
|
|
1207
|
+
Success: true,
|
|
1208
|
+
ResultCode: 'SUCCESS',
|
|
1209
|
+
Message: `Replied to message '${messageId}' from ${gate.TargetMailbox}.`,
|
|
1210
|
+
Params: params.Params,
|
|
1211
|
+
};
|
|
1212
|
+
}
|
|
1213
|
+
// Forward
|
|
1214
|
+
const fwdRes = await resolved.providerInstance.ForwardMessage({
|
|
1215
|
+
MessageID: messageId,
|
|
1216
|
+
Message: body,
|
|
1217
|
+
ToRecipients: [to].filter(Boolean),
|
|
1218
|
+
CCRecipients: cc,
|
|
1219
|
+
});
|
|
1220
|
+
if (!fwdRes.Success) {
|
|
1221
|
+
return {
|
|
1222
|
+
Success: false,
|
|
1223
|
+
ResultCode: 'PROVIDER_ERROR',
|
|
1224
|
+
Message: fwdRes.ErrorMessage ?? 'Failed to forward message.',
|
|
1225
|
+
Params: params.Params,
|
|
1226
|
+
};
|
|
1227
|
+
}
|
|
1228
|
+
return {
|
|
1229
|
+
Success: true,
|
|
1230
|
+
ResultCode: 'SUCCESS',
|
|
1231
|
+
Message: `Forwarded message '${messageId}' to ${to} from ${gate.TargetMailbox}.`,
|
|
1232
|
+
Params: params.Params,
|
|
1233
|
+
};
|
|
1234
|
+
}
|
|
1235
|
+
catch (error) {
|
|
1236
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
1237
|
+
LogError(`ManageMyEmailInboxAction error: ${msg}`);
|
|
1238
|
+
return {
|
|
1239
|
+
Success: false,
|
|
1240
|
+
ResultCode: 'UNEXPECTED_ERROR',
|
|
1241
|
+
Message: `Error managing email inbox: ${msg}`,
|
|
1242
|
+
Params: params.Params,
|
|
1243
|
+
};
|
|
1244
|
+
}
|
|
1245
|
+
}
|
|
1246
|
+
};
|
|
1247
|
+
ManageMyEmailInboxAction = __decorate([
|
|
1248
|
+
RegisterClass(BaseAction, '__ManageMyEmailInbox'),
|
|
1249
|
+
RegisterClass(BaseAction, 'Manage My Email Inbox and Drafts')
|
|
1250
|
+
], ManageMyEmailInboxAction);
|
|
1251
|
+
export { ManageMyEmailInboxAction };
|
|
1252
|
+
//# sourceMappingURL=chief-of-staff-calendar-email.action.js.map
|