@memberjunction/core-actions 6.3.0-edge.1 → 6.3.0-edge.2
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 +2 -0
- package/dist/custom/ai/agent-channel.action.d.ts +29 -0
- package/dist/custom/ai/agent-channel.action.d.ts.map +1 -0
- package/dist/custom/ai/agent-channel.action.js +843 -0
- package/dist/custom/ai/agent-channel.action.js.map +1 -0
- package/dist/custom/ai/chief-of-staff-approval-helper.d.ts +16 -0
- package/dist/custom/ai/chief-of-staff-approval-helper.d.ts.map +1 -0
- package/dist/custom/ai/chief-of-staff-approval-helper.js +63 -0
- package/dist/custom/ai/chief-of-staff-approval-helper.js.map +1 -0
- package/dist/custom/ai/chief-of-staff-core-reads.action.d.ts +64 -0
- package/dist/custom/ai/chief-of-staff-core-reads.action.d.ts.map +1 -0
- package/dist/custom/ai/chief-of-staff-core-reads.action.js +1097 -0
- package/dist/custom/ai/chief-of-staff-core-reads.action.js.map +1 -0
- package/dist/custom/ai/chief-of-staff-rhythms-suggestions.action.d.ts +28 -0
- package/dist/custom/ai/chief-of-staff-rhythms-suggestions.action.d.ts.map +1 -0
- package/dist/custom/ai/chief-of-staff-rhythms-suggestions.action.js +817 -0
- package/dist/custom/ai/chief-of-staff-rhythms-suggestions.action.js.map +1 -0
- package/dist/custom/ai/configure-agent-user-policy.action.d.ts +1 -0
- package/dist/custom/ai/configure-agent-user-policy.action.d.ts.map +1 -1
- package/dist/custom/ai/configure-agent-user-policy.action.js +5 -1
- package/dist/custom/ai/configure-agent-user-policy.action.js.map +1 -1
- package/dist/custom/ai/manage-my-agent-instance.action.d.ts +39 -0
- package/dist/custom/ai/manage-my-agent-instance.action.d.ts.map +1 -0
- package/dist/custom/ai/manage-my-agent-instance.action.js +752 -0
- package/dist/custom/ai/manage-my-agent-instance.action.js.map +1 -0
- package/dist/custom/ai/prepare-outbound-call-brief.action.js +1 -1
- package/dist/custom/ai/prepare-outbound-call-brief.action.js.map +1 -1
- package/dist/custom/ai/schedule-next-agent-wake.action.d.ts +2 -0
- package/dist/custom/ai/schedule-next-agent-wake.action.d.ts.map +1 -1
- package/dist/custom/ai/schedule-next-agent-wake.action.js +105 -2
- package/dist/custom/ai/schedule-next-agent-wake.action.js.map +1 -1
- package/dist/custom/ai/send-inter-agent-request.action.d.ts +1 -0
- package/dist/custom/ai/send-inter-agent-request.action.d.ts.map +1 -1
- package/dist/custom/ai/send-inter-agent-request.action.js +72 -18
- package/dist/custom/ai/send-inter-agent-request.action.js.map +1 -1
- package/dist/custom/ai/trigger-agent-wake.action.d.ts +1 -0
- package/dist/custom/ai/trigger-agent-wake.action.d.ts.map +1 -1
- package/dist/custom/ai/trigger-agent-wake.action.js +3 -0
- package/dist/custom/ai/trigger-agent-wake.action.js.map +1 -1
- package/dist/custom/ai/user-scoped-communication.action.d.ts +20 -0
- package/dist/custom/ai/user-scoped-communication.action.d.ts.map +1 -1
- package/dist/custom/ai/user-scoped-communication.action.js +140 -23
- package/dist/custom/ai/user-scoped-communication.action.js.map +1 -1
- package/dist/custom/data/adhoc-sql-gate.d.ts +40 -0
- package/dist/custom/data/adhoc-sql-gate.d.ts.map +1 -0
- package/dist/custom/data/adhoc-sql-gate.js +102 -0
- package/dist/custom/data/adhoc-sql-gate.js.map +1 -0
- package/dist/custom/data/data-mapper.action.d.ts +8 -0
- package/dist/custom/data/data-mapper.action.d.ts.map +1 -1
- package/dist/custom/data/data-mapper.action.js +21 -3
- package/dist/custom/data/data-mapper.action.js.map +1 -1
- package/dist/custom/data/find-graph-path.action.d.ts +37 -0
- package/dist/custom/data/find-graph-path.action.d.ts.map +1 -0
- package/dist/custom/data/find-graph-path.action.js +114 -0
- package/dist/custom/data/find-graph-path.action.js.map +1 -0
- package/dist/custom/data/get-graph-context.action.d.ts +22 -12
- package/dist/custom/data/get-graph-context.action.d.ts.map +1 -1
- package/dist/custom/data/get-graph-context.action.js +102 -239
- package/dist/custom/data/get-graph-context.action.js.map +1 -1
- package/dist/custom/data/graph-action-params.d.ts +51 -0
- package/dist/custom/data/graph-action-params.d.ts.map +1 -0
- package/dist/custom/data/graph-action-params.js +164 -0
- package/dist/custom/data/graph-action-params.js.map +1 -0
- package/dist/custom/data/mapping-template-guard.d.ts +18 -0
- package/dist/custom/data/mapping-template-guard.d.ts.map +1 -0
- package/dist/custom/data/mapping-template-guard.js +106 -0
- package/dist/custom/data/mapping-template-guard.js.map +1 -0
- package/dist/custom/data/propose-canon-domains.action.d.ts +26 -0
- package/dist/custom/data/propose-canon-domains.action.d.ts.map +1 -0
- package/dist/custom/data/propose-canon-domains.action.js +105 -0
- package/dist/custom/data/propose-canon-domains.action.js.map +1 -0
- package/dist/custom/data/run-adhoc-query.action.d.ts +4 -21
- package/dist/custom/data/run-adhoc-query.action.d.ts.map +1 -1
- package/dist/custom/data/run-adhoc-query.action.js +15 -105
- package/dist/custom/data/run-adhoc-query.action.js.map +1 -1
- package/dist/custom/integration/http-request.action.d.ts.map +1 -1
- package/dist/custom/integration/http-request.action.js +5 -1
- package/dist/custom/integration/http-request.action.js.map +1 -1
- package/dist/custom/lists/create-list-from-query.action.d.ts +39 -0
- package/dist/custom/lists/create-list-from-query.action.d.ts.map +1 -0
- package/dist/custom/lists/create-list-from-query.action.js +179 -0
- package/dist/custom/lists/create-list-from-query.action.js.map +1 -0
- package/dist/custom/lists/index.d.ts +2 -0
- package/dist/custom/lists/index.d.ts.map +1 -1
- package/dist/custom/lists/index.js +2 -0
- package/dist/custom/lists/index.js.map +1 -1
- package/dist/custom/search/scoped-search.action.d.ts +12 -1
- package/dist/custom/search/scoped-search.action.d.ts.map +1 -1
- package/dist/custom/search/scoped-search.action.js +44 -6
- package/dist/custom/search/scoped-search.action.js.map +1 -1
- package/dist/custom/search/search-query.util.d.ts +24 -0
- package/dist/custom/search/search-query.util.d.ts.map +1 -0
- package/dist/custom/search/search-query.util.js +211 -0
- package/dist/custom/search/search-query.util.js.map +1 -0
- package/dist/custom/search/search.action.js +2 -2
- package/dist/custom/search/search.action.js.map +1 -1
- package/dist/custom/web/url-link-validator.action.d.ts.map +1 -1
- package/dist/custom/web/url-link-validator.action.js +3 -1
- package/dist/custom/web/url-link-validator.action.js.map +1 -1
- package/dist/index.d.ts +7 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +7 -0
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -1
- package/package.json +45 -45
|
@@ -0,0 +1,817 @@
|
|
|
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 { EscapeSQLString, RegisterClass, SafeJSONParse, UUIDsEqual, UUIDSet } from '@memberjunction/global';
|
|
9
|
+
import { LogError, Metadata, RunView } from '@memberjunction/core';
|
|
10
|
+
import { AgentUserPolicyEngine, TypedActionPolicyGuard } from '@memberjunction/ai-core-plus';
|
|
11
|
+
import { CheckActivePolicyForAgentAndUser, ResolveAgentConfigurationIDInput, ResolveDefaultTierForWorkstreamKey, ResolveRoutineSeededAgentConfigurationID, } from '@memberjunction/ai-agents';
|
|
12
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
13
|
+
import { VerifyActiveRhythmTemplate, VerifyApprovedAgentRequest, } from './chief-of-staff-approval-helper.js';
|
|
14
|
+
const DEFAULT_AGENT_NAME = 'Chief of Staff';
|
|
15
|
+
function readStringParam(params, name) {
|
|
16
|
+
const found = params.Params.find((p) => p.Name.trim().toLowerCase() === name.toLowerCase());
|
|
17
|
+
if (found?.Value == null) {
|
|
18
|
+
return undefined;
|
|
19
|
+
}
|
|
20
|
+
const str = String(found.Value).trim();
|
|
21
|
+
return str.length > 0 ? str : undefined;
|
|
22
|
+
}
|
|
23
|
+
function readBooleanParam(params, name, fallback) {
|
|
24
|
+
const found = params.Params.find((p) => p.Name.trim().toLowerCase() === name.toLowerCase());
|
|
25
|
+
if (found?.Value == null) {
|
|
26
|
+
return fallback;
|
|
27
|
+
}
|
|
28
|
+
if (typeof found.Value === 'boolean') {
|
|
29
|
+
return found.Value;
|
|
30
|
+
}
|
|
31
|
+
const normalized = String(found.Value).trim().toLowerCase();
|
|
32
|
+
if (normalized === 'true' || normalized === '1' || normalized === 'yes') {
|
|
33
|
+
return true;
|
|
34
|
+
}
|
|
35
|
+
if (normalized === 'false' || normalized === '0' || normalized === 'no') {
|
|
36
|
+
return false;
|
|
37
|
+
}
|
|
38
|
+
return fallback;
|
|
39
|
+
}
|
|
40
|
+
function extractContextAgentId(context) {
|
|
41
|
+
if (!context || typeof context !== 'object') {
|
|
42
|
+
return undefined;
|
|
43
|
+
}
|
|
44
|
+
const val = context.AgentID;
|
|
45
|
+
return typeof val === 'string' && val.trim().length > 0 ? val.trim() : undefined;
|
|
46
|
+
}
|
|
47
|
+
function resolveAgentInheritanceIds(agent) {
|
|
48
|
+
if (agent.BaseAgentID && typeof AIEngine.Instance.GetAgentInheritanceChain === 'function') {
|
|
49
|
+
const chain = AIEngine.Instance.GetAgentInheritanceChain(agent.ID);
|
|
50
|
+
if (chain.length > 0) {
|
|
51
|
+
return chain.map((a) => a.ID);
|
|
52
|
+
}
|
|
53
|
+
}
|
|
54
|
+
return [agent.ID];
|
|
55
|
+
}
|
|
56
|
+
function buildAgentIdInClause(agentIds) {
|
|
57
|
+
if (agentIds.length === 1) {
|
|
58
|
+
return `AgentID='${EscapeSQLString(agentIds[0])}'`;
|
|
59
|
+
}
|
|
60
|
+
return `AgentID IN (${agentIds.map((id) => `'${EscapeSQLString(id)}'`).join(',')})`;
|
|
61
|
+
}
|
|
62
|
+
function resolveTargetAgent(params) {
|
|
63
|
+
const explicitId = readStringParam(params, 'AgentID') ?? extractContextAgentId(params.Context);
|
|
64
|
+
if (explicitId) {
|
|
65
|
+
return AIEngine.Instance.Agents.find((a) => UUIDsEqual(a.ID, explicitId)) ?? null;
|
|
66
|
+
}
|
|
67
|
+
const explicitName = readStringParam(params, 'AgentName') ?? DEFAULT_AGENT_NAME;
|
|
68
|
+
const found = AIEngine.Instance.Agents.find((a) => a.Name.trim().toLowerCase() === explicitName.toLowerCase()) ??
|
|
69
|
+
null;
|
|
70
|
+
if (found &&
|
|
71
|
+
!found.BaseAgentID &&
|
|
72
|
+
params.ContextUser?.ID &&
|
|
73
|
+
typeof AIEngine.Instance.ResolvePrimaryUserAgentInstance === 'function') {
|
|
74
|
+
return AIEngine.Instance.ResolvePrimaryUserAgentInstance(found.ID, params.ContextUser.ID) ?? found;
|
|
75
|
+
}
|
|
76
|
+
return found;
|
|
77
|
+
}
|
|
78
|
+
function setOutputParam(params, name, value) {
|
|
79
|
+
const existing = params.Params.find((p) => p.Name.trim().toLowerCase() === name.toLowerCase() && p.Type === 'Output');
|
|
80
|
+
if (existing) {
|
|
81
|
+
existing.Value = value;
|
|
82
|
+
return params.Params;
|
|
83
|
+
}
|
|
84
|
+
return [...params.Params, { Name: name, Type: 'Output', Value: value }];
|
|
85
|
+
}
|
|
86
|
+
/**
|
|
87
|
+
* Lists offered `MJ: AI Agent Rhythm Templates` available for the agent, annotated with
|
|
88
|
+
* whether the calling user already has an active routine or a prior suggestion response
|
|
89
|
+
* (`Offered`, `Accepted`, `Declined`) for each template's `WorkstreamKey`.
|
|
90
|
+
*/
|
|
91
|
+
let ListAvailableRhythmsAction = class ListAvailableRhythmsAction extends BaseAction {
|
|
92
|
+
static { this.AuthorizesCaller = true; }
|
|
93
|
+
async InternalRunAction(params) {
|
|
94
|
+
try {
|
|
95
|
+
const contextUser = params.ContextUser;
|
|
96
|
+
if (!contextUser?.ID) {
|
|
97
|
+
return {
|
|
98
|
+
Success: false,
|
|
99
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
100
|
+
Message: 'List Available Rhythms requires a valid ContextUser.',
|
|
101
|
+
Params: params.Params,
|
|
102
|
+
};
|
|
103
|
+
}
|
|
104
|
+
const guard = await TypedActionPolicyGuard.CheckEntityAccess({
|
|
105
|
+
entityName: 'MJ: AI Agent Rhythm Templates',
|
|
106
|
+
operation: 'Read',
|
|
107
|
+
contextUser,
|
|
108
|
+
actionContext: params.Context,
|
|
109
|
+
provider: params.Provider,
|
|
110
|
+
});
|
|
111
|
+
if (!guard.Allowed) {
|
|
112
|
+
return {
|
|
113
|
+
Success: false,
|
|
114
|
+
ResultCode: guard.ResultCode,
|
|
115
|
+
Message: guard.Message,
|
|
116
|
+
Params: params.Params,
|
|
117
|
+
};
|
|
118
|
+
}
|
|
119
|
+
await AIEngine.Instance.Config(false, contextUser);
|
|
120
|
+
const agent = resolveTargetAgent(params);
|
|
121
|
+
if (!agent) {
|
|
122
|
+
return {
|
|
123
|
+
Success: false,
|
|
124
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
125
|
+
Message: 'Could not resolve target AI Agent.',
|
|
126
|
+
Params: params.Params,
|
|
127
|
+
};
|
|
128
|
+
}
|
|
129
|
+
const offerAtFilter = readStringParam(params, 'OfferAt')?.toLowerCase();
|
|
130
|
+
const includeEnabled = readBooleanParam(params, 'IncludeEnabled', true);
|
|
131
|
+
const includeDeclined = readBooleanParam(params, 'IncludeDeclined', false);
|
|
132
|
+
const md = params.Provider ?? Metadata.Provider;
|
|
133
|
+
const rv = RunView.FromMetadataProvider(md);
|
|
134
|
+
const safeAgentId = EscapeSQLString(agent.ID);
|
|
135
|
+
const safeUserId = EscapeSQLString(contextUser.ID);
|
|
136
|
+
const chainIds = resolveAgentInheritanceIds(agent);
|
|
137
|
+
const chainFilter = buildAgentIdInClause(chainIds);
|
|
138
|
+
const [templatesRes, routinesRes, suggestionsRes] = await Promise.all([
|
|
139
|
+
rv.RunView({
|
|
140
|
+
EntityName: 'MJ: AI Agent Rhythm Templates',
|
|
141
|
+
ExtraFilter: `${chainFilter} AND Status='Active'`,
|
|
142
|
+
OrderBy: 'Name ASC',
|
|
143
|
+
ResultType: 'entity_object',
|
|
144
|
+
}, contextUser),
|
|
145
|
+
rv.RunView({
|
|
146
|
+
EntityName: 'MJ: User Routines',
|
|
147
|
+
ExtraFilter: `UserID='${safeUserId}' AND TargetID='${safeAgentId}'`,
|
|
148
|
+
ResultType: 'entity_object',
|
|
149
|
+
}, contextUser),
|
|
150
|
+
rv.RunView({
|
|
151
|
+
EntityName: 'MJ: AI Agent Suggestions',
|
|
152
|
+
ExtraFilter: `UserID='${safeUserId}' AND AgentID='${safeAgentId}' AND Kind='Rhythm'`,
|
|
153
|
+
OrderBy: 'OfferedAt DESC',
|
|
154
|
+
ResultType: 'entity_object',
|
|
155
|
+
}, contextUser),
|
|
156
|
+
]);
|
|
157
|
+
if (!templatesRes.Success) {
|
|
158
|
+
return {
|
|
159
|
+
Success: false,
|
|
160
|
+
ResultCode: 'QUERY_FAILED',
|
|
161
|
+
Message: templatesRes.ErrorMessage || 'Failed to load rhythm templates.',
|
|
162
|
+
Params: params.Params,
|
|
163
|
+
};
|
|
164
|
+
}
|
|
165
|
+
const chainIdSet = new UUIDSet(chainIds);
|
|
166
|
+
const grantedSkillIds = new Set(AIEngine.Instance.AgentSkills.filter((s) => chainIdSet.Has(s.AgentID)).map((s) => s.SkillID.toLowerCase()));
|
|
167
|
+
// Deduplicate templates by WorkstreamKey, preferring closer descendant over ancestor templates
|
|
168
|
+
const chainDepthRank = new Map();
|
|
169
|
+
chainIds.forEach((cid, idx) => chainDepthRank.set(cid.toLowerCase(), idx));
|
|
170
|
+
const dedupedByWorkstream = new Map();
|
|
171
|
+
for (const tpl of templatesRes.Results) {
|
|
172
|
+
const wsKey = tpl.WorkstreamKey.trim().toLowerCase();
|
|
173
|
+
const existing = dedupedByWorkstream.get(wsKey);
|
|
174
|
+
if (!existing) {
|
|
175
|
+
dedupedByWorkstream.set(wsKey, tpl);
|
|
176
|
+
}
|
|
177
|
+
else {
|
|
178
|
+
const existingRank = chainDepthRank.get(existing.AgentID.toLowerCase()) ?? -1;
|
|
179
|
+
const candidateRank = chainDepthRank.get(tpl.AgentID.toLowerCase()) ?? -1;
|
|
180
|
+
if (candidateRank > existingRank) {
|
|
181
|
+
dedupedByWorkstream.set(wsKey, tpl);
|
|
182
|
+
}
|
|
183
|
+
}
|
|
184
|
+
}
|
|
185
|
+
const effectiveTemplates = Array.from(dedupedByWorkstream.values());
|
|
186
|
+
const routineByWorkstream = new Map();
|
|
187
|
+
for (const r of routinesRes.Success ? routinesRes.Results : []) {
|
|
188
|
+
const key = r.WakeConfigurationObject?.workstreamKey?.trim().toLowerCase();
|
|
189
|
+
if (key) {
|
|
190
|
+
routineByWorkstream.set(key, r);
|
|
191
|
+
}
|
|
192
|
+
}
|
|
193
|
+
const suggestionByKey = new Map();
|
|
194
|
+
for (const s of suggestionsRes.Success ? suggestionsRes.Results : []) {
|
|
195
|
+
const key = s.TargetKey.trim().toLowerCase();
|
|
196
|
+
if (!suggestionByKey.has(key)) {
|
|
197
|
+
suggestionByKey.set(key, s);
|
|
198
|
+
}
|
|
199
|
+
}
|
|
200
|
+
const filteredTemplates = effectiveTemplates.filter((t) => {
|
|
201
|
+
// Only offer skill-contributed rhythms whose skill is granted to the agent
|
|
202
|
+
if (t.SkillID && !grantedSkillIds.has(t.SkillID.toLowerCase())) {
|
|
203
|
+
return false;
|
|
204
|
+
}
|
|
205
|
+
if (offerAtFilter && offerAtFilter !== 'all') {
|
|
206
|
+
const tplOffer = t.OfferAt.toLowerCase();
|
|
207
|
+
if (tplOffer !== 'both' && tplOffer !== offerAtFilter) {
|
|
208
|
+
return false;
|
|
209
|
+
}
|
|
210
|
+
}
|
|
211
|
+
const key = t.WorkstreamKey.trim().toLowerCase();
|
|
212
|
+
const routine = routineByWorkstream.get(key);
|
|
213
|
+
const isEnabled = routine?.Status === 'Active';
|
|
214
|
+
if (!includeEnabled && isEnabled) {
|
|
215
|
+
return false;
|
|
216
|
+
}
|
|
217
|
+
const suggestion = suggestionByKey.get(key);
|
|
218
|
+
if (!includeDeclined && suggestion?.Status === 'Declined') {
|
|
219
|
+
return false;
|
|
220
|
+
}
|
|
221
|
+
return true;
|
|
222
|
+
});
|
|
223
|
+
// Lazily resolve catalog fallback configuration IDs only when any filtered template
|
|
224
|
+
// has a null DefaultAgentConfigurationID in DB.
|
|
225
|
+
const fallbackConfigIdByTierName = new Map();
|
|
226
|
+
const needsCatalogLookup = filteredTemplates.some((t) => !t.DefaultAgentConfigurationID && Boolean(ResolveDefaultTierForWorkstreamKey(t.WorkstreamKey)));
|
|
227
|
+
if (needsCatalogLookup) {
|
|
228
|
+
const cfgRes = await rv.RunView({
|
|
229
|
+
EntityName: 'MJ: AI Configurations',
|
|
230
|
+
ExtraFilter: "Name IN ('Fast', 'Standard', 'High Power')",
|
|
231
|
+
Fields: ['ID', 'Name', 'Status'],
|
|
232
|
+
OrderBy: 'Status ASC, Name ASC',
|
|
233
|
+
MaxRows: 20,
|
|
234
|
+
ResultType: 'simple',
|
|
235
|
+
}, contextUser);
|
|
236
|
+
if (cfgRes.Success) {
|
|
237
|
+
for (const row of cfgRes.Results) {
|
|
238
|
+
const normalizedName = row.Name.trim().toLowerCase();
|
|
239
|
+
if (!fallbackConfigIdByTierName.has(normalizedName) || row.Status === 'Active') {
|
|
240
|
+
fallbackConfigIdByTierName.set(normalizedName, row.ID);
|
|
241
|
+
}
|
|
242
|
+
}
|
|
243
|
+
}
|
|
244
|
+
}
|
|
245
|
+
const rows = filteredTemplates.map((t) => {
|
|
246
|
+
const key = t.WorkstreamKey.trim().toLowerCase();
|
|
247
|
+
const routine = routineByWorkstream.get(key);
|
|
248
|
+
const suggestion = suggestionByKey.get(key);
|
|
249
|
+
const tierSpec = ResolveDefaultTierForWorkstreamKey(t.WorkstreamKey);
|
|
250
|
+
const catalogFallbackId = tierSpec
|
|
251
|
+
? (fallbackConfigIdByTierName.get(tierSpec.DefaultConfigurationName.toLowerCase()) ?? null)
|
|
252
|
+
: null;
|
|
253
|
+
return {
|
|
254
|
+
RhythmTemplateID: t.ID,
|
|
255
|
+
Name: t.Name,
|
|
256
|
+
WorkstreamKey: t.WorkstreamKey,
|
|
257
|
+
Description: t.Description,
|
|
258
|
+
DefaultCronExpression: t.DefaultCronExpression,
|
|
259
|
+
WakeReason: t.WakeReason,
|
|
260
|
+
OfferAt: t.OfferAt,
|
|
261
|
+
SkillID: t.SkillID,
|
|
262
|
+
SkillName: t.Skill,
|
|
263
|
+
DefaultAgentConfigurationID: t.DefaultAgentConfigurationID ?? catalogFallbackId,
|
|
264
|
+
DefaultAgentConfiguration: t.DefaultAgentConfiguration ?? tierSpec?.DefaultConfigurationName ?? null,
|
|
265
|
+
DefaultModelTier: tierSpec?.Tier ?? null,
|
|
266
|
+
EnabledForUser: routine?.Status === 'Active',
|
|
267
|
+
UserRoutineID: routine?.ID ?? null,
|
|
268
|
+
UserRoutineStatus: routine?.Status ?? null,
|
|
269
|
+
AgentConfigurationID: routine?.AgentConfigurationID ?? null,
|
|
270
|
+
PriorSuggestionStatus: suggestion?.Status ?? null,
|
|
271
|
+
};
|
|
272
|
+
});
|
|
273
|
+
const masked = TypedActionPolicyGuard.MaskRows(rows, guard.DeniedFieldPatterns);
|
|
274
|
+
const payload = { Count: masked.length, Rhythms: masked };
|
|
275
|
+
return {
|
|
276
|
+
Success: true,
|
|
277
|
+
ResultCode: 'SUCCESS',
|
|
278
|
+
Message: JSON.stringify(payload),
|
|
279
|
+
Params: setOutputParam(params, 'RhythmsJSON', JSON.stringify(payload)),
|
|
280
|
+
};
|
|
281
|
+
}
|
|
282
|
+
catch (error) {
|
|
283
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
284
|
+
LogError(`[ListAvailableRhythmsAction] ${msg}`);
|
|
285
|
+
return { Success: false, ResultCode: 'ERROR', Message: msg, Params: params.Params };
|
|
286
|
+
}
|
|
287
|
+
}
|
|
288
|
+
};
|
|
289
|
+
ListAvailableRhythmsAction = __decorate([
|
|
290
|
+
RegisterClass(BaseAction, 'List Available Rhythms')
|
|
291
|
+
], ListAvailableRhythmsAction);
|
|
292
|
+
export { ListAvailableRhythmsAction };
|
|
293
|
+
/**
|
|
294
|
+
* Materializes an active `MJ: AI Agent Rhythm Templates` entry into a recurring
|
|
295
|
+
* `MJ: User Routines` row for `ContextUser` after explicit user opt-in.
|
|
296
|
+
*/
|
|
297
|
+
let EnableAgentRhythmAction = class EnableAgentRhythmAction extends BaseAction {
|
|
298
|
+
static { this.AuthorizesCaller = true; }
|
|
299
|
+
async InternalRunAction(params) {
|
|
300
|
+
try {
|
|
301
|
+
const contextUser = params.ContextUser;
|
|
302
|
+
if (!contextUser?.ID) {
|
|
303
|
+
return {
|
|
304
|
+
Success: false,
|
|
305
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
306
|
+
Message: 'Enable Agent Rhythm requires a valid ContextUser.',
|
|
307
|
+
Params: params.Params,
|
|
308
|
+
};
|
|
309
|
+
}
|
|
310
|
+
const rhythmTemplateId = readStringParam(params, 'RhythmTemplateID');
|
|
311
|
+
const workstreamKey = readStringParam(params, 'WorkstreamKey');
|
|
312
|
+
if (!rhythmTemplateId && !workstreamKey) {
|
|
313
|
+
return {
|
|
314
|
+
Success: false,
|
|
315
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
316
|
+
Message: 'Provide either RhythmTemplateID or WorkstreamKey to enable a rhythm.',
|
|
317
|
+
Params: params.Params,
|
|
318
|
+
};
|
|
319
|
+
}
|
|
320
|
+
const routineGuard = await TypedActionPolicyGuard.CheckEntityAccess({
|
|
321
|
+
entityName: 'MJ: User Routines',
|
|
322
|
+
operation: 'Create',
|
|
323
|
+
contextUser,
|
|
324
|
+
actionContext: params.Context,
|
|
325
|
+
provider: params.Provider,
|
|
326
|
+
});
|
|
327
|
+
if (!routineGuard.Allowed) {
|
|
328
|
+
return {
|
|
329
|
+
Success: false,
|
|
330
|
+
ResultCode: routineGuard.ResultCode,
|
|
331
|
+
Message: routineGuard.Message,
|
|
332
|
+
Params: params.Params,
|
|
333
|
+
};
|
|
334
|
+
}
|
|
335
|
+
await AIEngine.Instance.Config(false, contextUser);
|
|
336
|
+
const agent = resolveTargetAgent(params);
|
|
337
|
+
if (!agent) {
|
|
338
|
+
return {
|
|
339
|
+
Success: false,
|
|
340
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
341
|
+
Message: 'Could not resolve target AI Agent.',
|
|
342
|
+
Params: params.Params,
|
|
343
|
+
};
|
|
344
|
+
}
|
|
345
|
+
const md = params.Provider ?? Metadata.Provider;
|
|
346
|
+
const callingAgentId = extractContextAgentId(params.Context) ||
|
|
347
|
+
AgentUserPolicyEngine.Instance.GetActiveExecutingAgentID();
|
|
348
|
+
if (callingAgentId) {
|
|
349
|
+
const confirmedApprovalRequestId = readStringParam(params, 'ConfirmedApprovalRequestID');
|
|
350
|
+
const approved = confirmedApprovalRequestId
|
|
351
|
+
? await VerifyApprovedAgentRequest(confirmedApprovalRequestId, contextUser.ID, agent.ID, contextUser, md)
|
|
352
|
+
: false;
|
|
353
|
+
if (!approved) {
|
|
354
|
+
return {
|
|
355
|
+
Success: false,
|
|
356
|
+
ResultCode: 'APPROVAL_REQUIRED',
|
|
357
|
+
Message: 'Enabling a recurring agent rhythm during agent execution requires a server-verified approved MJ: AI Agent Requests record (ConfirmedApprovalRequestID). Self-attested ConfirmedByUser is not sufficient.',
|
|
358
|
+
Params: params.Params,
|
|
359
|
+
};
|
|
360
|
+
}
|
|
361
|
+
}
|
|
362
|
+
const rv = RunView.FromMetadataProvider(md);
|
|
363
|
+
const chainIds = resolveAgentInheritanceIds(agent);
|
|
364
|
+
const chainFilter = buildAgentIdInClause(chainIds);
|
|
365
|
+
let template = null;
|
|
366
|
+
if (rhythmTemplateId) {
|
|
367
|
+
template = await VerifyActiveRhythmTemplate(rhythmTemplateId, chainIds, contextUser, md);
|
|
368
|
+
}
|
|
369
|
+
else {
|
|
370
|
+
const tplFilter = `${chainFilter} AND LOWER(WorkstreamKey)='${EscapeSQLString(workstreamKey.toLowerCase())}' AND Status='Active'`;
|
|
371
|
+
const tplRes = await rv.RunView({
|
|
372
|
+
EntityName: 'MJ: AI Agent Rhythm Templates',
|
|
373
|
+
ExtraFilter: tplFilter,
|
|
374
|
+
MaxRows: 10,
|
|
375
|
+
ResultType: 'entity_object',
|
|
376
|
+
}, contextUser);
|
|
377
|
+
if (tplRes.Success && tplRes.Results.length > 0) {
|
|
378
|
+
// Prefer the closest descendant in the inheritance chain (highest index in root->leaf order)
|
|
379
|
+
const chainIdSet = new UUIDSet(chainIds);
|
|
380
|
+
const depthRank = new Map();
|
|
381
|
+
chainIds.forEach((cid, idx) => depthRank.set(cid.toLowerCase(), idx));
|
|
382
|
+
const matching = tplRes.Results.filter((c) => c.Status === 'Active' && chainIdSet.Has(c.AgentID));
|
|
383
|
+
matching.sort((a, b) => (depthRank.get(b.AgentID.toLowerCase()) ?? -1) -
|
|
384
|
+
(depthRank.get(a.AgentID.toLowerCase()) ?? -1));
|
|
385
|
+
template = matching[0] ?? null;
|
|
386
|
+
}
|
|
387
|
+
}
|
|
388
|
+
if (!template) {
|
|
389
|
+
return {
|
|
390
|
+
Success: false,
|
|
391
|
+
ResultCode: 'NOT_FOUND',
|
|
392
|
+
Message: `No active rhythm template found for agent "${agent.Name}" matching ${rhythmTemplateId ? `ID "${rhythmTemplateId}"` : `WorkstreamKey "${workstreamKey}"`}.`,
|
|
393
|
+
Params: params.Params,
|
|
394
|
+
};
|
|
395
|
+
}
|
|
396
|
+
const policyEval = await CheckActivePolicyForAgentAndUser(agent.ID, contextUser.ID, contextUser, { provider: md, preferCache: true });
|
|
397
|
+
if (!policyEval.Allowed) {
|
|
398
|
+
return {
|
|
399
|
+
Success: false,
|
|
400
|
+
ResultCode: 'POLICY_DENIED',
|
|
401
|
+
Message: policyEval.Reason,
|
|
402
|
+
Params: params.Params,
|
|
403
|
+
};
|
|
404
|
+
}
|
|
405
|
+
const cronExpression = readStringParam(params, 'CronExpression') ?? template.DefaultCronExpression;
|
|
406
|
+
const timezone = readStringParam(params, 'Timezone') ?? 'UTC';
|
|
407
|
+
const parsedGate = template.DefaultWakeGate
|
|
408
|
+
? SafeJSONParse(template.DefaultWakeGate) ?? {}
|
|
409
|
+
: {};
|
|
410
|
+
const hasExplicitConfigParam = params.Params.some((p) => p.Name.trim().toLowerCase() === 'agentconfigurationid');
|
|
411
|
+
const rawAgentConfigID = hasExplicitConfigParam
|
|
412
|
+
? (readStringParam(params, 'AgentConfigurationID') ?? null)
|
|
413
|
+
: undefined;
|
|
414
|
+
const rawModelTier = readStringParam(params, 'ModelTier');
|
|
415
|
+
const configResolution = await ResolveAgentConfigurationIDInput({
|
|
416
|
+
agentConfigurationID: rawAgentConfigID,
|
|
417
|
+
modelTier: rawModelTier,
|
|
418
|
+
contextUser,
|
|
419
|
+
metadataProvider: md,
|
|
420
|
+
});
|
|
421
|
+
if (configResolution.ErrorMessage) {
|
|
422
|
+
return {
|
|
423
|
+
Success: false,
|
|
424
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
425
|
+
Message: configResolution.ErrorMessage,
|
|
426
|
+
Params: params.Params,
|
|
427
|
+
};
|
|
428
|
+
}
|
|
429
|
+
// Upsert user's routine for (UserID, AgentID, WorkstreamKey) — always targeting the concrete agent instance
|
|
430
|
+
const existingRes = await rv.RunView({
|
|
431
|
+
EntityName: 'MJ: User Routines',
|
|
432
|
+
ExtraFilter: `UserID='${EscapeSQLString(contextUser.ID)}' AND TargetID='${EscapeSQLString(agent.ID)}' AND RoutineType='AgentWake'`,
|
|
433
|
+
MaxRows: 50,
|
|
434
|
+
ResultType: 'entity_object',
|
|
435
|
+
}, contextUser);
|
|
436
|
+
let routine = (existingRes.Success ? existingRes.Results : []).find((r) => (r.WakeConfigurationObject?.workstreamKey ?? '').toLowerCase() ===
|
|
437
|
+
template.WorkstreamKey.toLowerCase()) ?? null;
|
|
438
|
+
const isNewRoutine = !routine;
|
|
439
|
+
if (!routine) {
|
|
440
|
+
routine = await md.GetEntityObject('MJ: User Routines', contextUser);
|
|
441
|
+
routine.NewRecord();
|
|
442
|
+
routine.UserID = contextUser.ID;
|
|
443
|
+
routine.RoutineType = 'AgentWake';
|
|
444
|
+
routine.TargetType = 'Agent';
|
|
445
|
+
routine.TargetID = agent.ID;
|
|
446
|
+
}
|
|
447
|
+
if (isNewRoutine) {
|
|
448
|
+
routine.AgentConfigurationID = await ResolveRoutineSeededAgentConfigurationID({
|
|
449
|
+
explicitAgentConfigurationID: configResolution.Provided
|
|
450
|
+
? configResolution.ConfigurationID
|
|
451
|
+
: undefined,
|
|
452
|
+
templateDefaultAgentConfigurationID: template.DefaultAgentConfigurationID ?? null,
|
|
453
|
+
workstreamKey: template.WorkstreamKey,
|
|
454
|
+
contextUser,
|
|
455
|
+
metadataProvider: md,
|
|
456
|
+
});
|
|
457
|
+
}
|
|
458
|
+
else if (configResolution.Provided) {
|
|
459
|
+
routine.AgentConfigurationID = configResolution.ConfigurationID;
|
|
460
|
+
}
|
|
461
|
+
const wakeConfig = {
|
|
462
|
+
...parsedGate,
|
|
463
|
+
workstreamKey: template.WorkstreamKey,
|
|
464
|
+
wakeReason: template.WakeReason,
|
|
465
|
+
resumeState: {
|
|
466
|
+
...(parsedGate.resumeState ?? {}),
|
|
467
|
+
rhythmTemplateID: template.ID,
|
|
468
|
+
workstreamKey: template.WorkstreamKey,
|
|
469
|
+
},
|
|
470
|
+
};
|
|
471
|
+
routine.Name = `${agent.Name} — ${template.Name}`;
|
|
472
|
+
routine.Description = template.Description ?? template.WakeReason;
|
|
473
|
+
routine.ScheduleMode = 'Recurring';
|
|
474
|
+
routine.CronExpression = cronExpression;
|
|
475
|
+
routine.Timezone = timezone;
|
|
476
|
+
routine.Status = 'Active';
|
|
477
|
+
routine.InitialMessage = `[AgentWake:${template.WorkstreamKey}] ${template.WakeReason}`;
|
|
478
|
+
routine.WakeConfigurationObject = wakeConfig;
|
|
479
|
+
if (template.SkillID && !routine.RequestedSkillIDs) {
|
|
480
|
+
routine.RequestedSkillIDs = JSON.stringify([template.SkillID]);
|
|
481
|
+
}
|
|
482
|
+
const saved = await routine.Save();
|
|
483
|
+
if (!saved) {
|
|
484
|
+
return {
|
|
485
|
+
Success: false,
|
|
486
|
+
ResultCode: 'SAVE_FAILED',
|
|
487
|
+
Message: routine.LatestResult?.CompleteMessage || 'Failed to save enabled rhythm routine.',
|
|
488
|
+
Params: params.Params,
|
|
489
|
+
};
|
|
490
|
+
}
|
|
491
|
+
// Mark any matching 'Offered' suggestion as 'Accepted' (best effort)
|
|
492
|
+
const sugRes = await rv.RunView({
|
|
493
|
+
EntityName: 'MJ: AI Agent Suggestions',
|
|
494
|
+
ExtraFilter: `UserID='${EscapeSQLString(contextUser.ID)}' AND AgentID='${EscapeSQLString(agent.ID)}' AND LOWER(TargetKey)='${EscapeSQLString(template.WorkstreamKey.toLowerCase())}' AND Status='Offered'`,
|
|
495
|
+
MaxRows: 5,
|
|
496
|
+
ResultType: 'entity_object',
|
|
497
|
+
}, contextUser);
|
|
498
|
+
if (sugRes.Success) {
|
|
499
|
+
const now = new Date();
|
|
500
|
+
for (const sug of sugRes.Results) {
|
|
501
|
+
sug.Status = 'Accepted';
|
|
502
|
+
sug.RespondedAt = now;
|
|
503
|
+
await sug.Save();
|
|
504
|
+
}
|
|
505
|
+
}
|
|
506
|
+
const payload = {
|
|
507
|
+
RoutineID: routine.ID,
|
|
508
|
+
RhythmTemplateID: template.ID,
|
|
509
|
+
Name: routine.Name,
|
|
510
|
+
WorkstreamKey: template.WorkstreamKey,
|
|
511
|
+
CronExpression: routine.CronExpression,
|
|
512
|
+
Timezone: routine.Timezone,
|
|
513
|
+
Status: routine.Status,
|
|
514
|
+
AgentConfigurationID: routine.AgentConfigurationID ?? null,
|
|
515
|
+
};
|
|
516
|
+
return {
|
|
517
|
+
Success: true,
|
|
518
|
+
ResultCode: 'SUCCESS',
|
|
519
|
+
Message: JSON.stringify(payload),
|
|
520
|
+
Params: setOutputParam(params, 'RoutineID', routine.ID),
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
catch (error) {
|
|
524
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
525
|
+
LogError(`[EnableAgentRhythmAction] ${msg}`);
|
|
526
|
+
return { Success: false, ResultCode: 'ERROR', Message: msg, Params: params.Params };
|
|
527
|
+
}
|
|
528
|
+
}
|
|
529
|
+
};
|
|
530
|
+
EnableAgentRhythmAction = __decorate([
|
|
531
|
+
RegisterClass(BaseAction, 'Enable Agent Rhythm')
|
|
532
|
+
], EnableAgentRhythmAction);
|
|
533
|
+
export { EnableAgentRhythmAction };
|
|
534
|
+
/**
|
|
535
|
+
* Lists, records, or updates proactive capability/rhythm suggestions in `MJ: AI Agent Suggestions`
|
|
536
|
+
* so declined suggestions are respected and never repeatedly offered.
|
|
537
|
+
*/
|
|
538
|
+
let ManageAgentSuggestionsAction = class ManageAgentSuggestionsAction extends BaseAction {
|
|
539
|
+
static { this.AuthorizesCaller = true; }
|
|
540
|
+
async InternalRunAction(params) {
|
|
541
|
+
try {
|
|
542
|
+
const contextUser = params.ContextUser;
|
|
543
|
+
if (!contextUser?.ID) {
|
|
544
|
+
return {
|
|
545
|
+
Success: false,
|
|
546
|
+
ResultCode: 'MISSING_USER_CONTEXT',
|
|
547
|
+
Message: 'Manage Agent Suggestions requires a valid ContextUser.',
|
|
548
|
+
Params: params.Params,
|
|
549
|
+
};
|
|
550
|
+
}
|
|
551
|
+
const operation = (readStringParam(params, 'Operation') ?? 'List').toLowerCase();
|
|
552
|
+
await AIEngine.Instance.Config(false, contextUser);
|
|
553
|
+
const agent = resolveTargetAgent(params);
|
|
554
|
+
if (!agent) {
|
|
555
|
+
return {
|
|
556
|
+
Success: false,
|
|
557
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
558
|
+
Message: 'Could not resolve target AI Agent.',
|
|
559
|
+
Params: params.Params,
|
|
560
|
+
};
|
|
561
|
+
}
|
|
562
|
+
const md = params.Provider ?? Metadata.Provider;
|
|
563
|
+
const rv = RunView.FromMetadataProvider(md);
|
|
564
|
+
if (operation === 'list') {
|
|
565
|
+
const guard = await TypedActionPolicyGuard.CheckEntityAccess({
|
|
566
|
+
entityName: 'MJ: AI Agent Suggestions',
|
|
567
|
+
operation: 'Read',
|
|
568
|
+
contextUser,
|
|
569
|
+
actionContext: params.Context,
|
|
570
|
+
provider: md,
|
|
571
|
+
});
|
|
572
|
+
if (!guard.Allowed) {
|
|
573
|
+
return {
|
|
574
|
+
Success: false,
|
|
575
|
+
ResultCode: guard.ResultCode,
|
|
576
|
+
Message: guard.Message,
|
|
577
|
+
Params: params.Params,
|
|
578
|
+
};
|
|
579
|
+
}
|
|
580
|
+
const status = readStringParam(params, 'Status');
|
|
581
|
+
const kind = readStringParam(params, 'Kind');
|
|
582
|
+
const clauses = [
|
|
583
|
+
`UserID='${EscapeSQLString(contextUser.ID)}'`,
|
|
584
|
+
`AgentID='${EscapeSQLString(agent.ID)}'`,
|
|
585
|
+
];
|
|
586
|
+
if (status && status.toLowerCase() !== 'all') {
|
|
587
|
+
clauses.push(`Status='${EscapeSQLString(status)}'`);
|
|
588
|
+
}
|
|
589
|
+
if (kind && kind.toLowerCase() !== 'all') {
|
|
590
|
+
clauses.push(`Kind='${EscapeSQLString(kind)}'`);
|
|
591
|
+
}
|
|
592
|
+
const res = await rv.RunView({
|
|
593
|
+
EntityName: 'MJ: AI Agent Suggestions',
|
|
594
|
+
ExtraFilter: clauses.join(' AND '),
|
|
595
|
+
OrderBy: 'OfferedAt DESC',
|
|
596
|
+
MaxRows: 50,
|
|
597
|
+
ResultType: 'entity_object',
|
|
598
|
+
}, contextUser);
|
|
599
|
+
if (!res.Success) {
|
|
600
|
+
return {
|
|
601
|
+
Success: false,
|
|
602
|
+
ResultCode: 'QUERY_FAILED',
|
|
603
|
+
Message: res.ErrorMessage || 'Failed to list agent suggestions.',
|
|
604
|
+
Params: params.Params,
|
|
605
|
+
};
|
|
606
|
+
}
|
|
607
|
+
const rows = TypedActionPolicyGuard.MaskRows(res.Results.map((s) => ({
|
|
608
|
+
SuggestionID: s.ID,
|
|
609
|
+
Kind: s.Kind,
|
|
610
|
+
TargetKey: s.TargetKey,
|
|
611
|
+
Summary: s.Summary,
|
|
612
|
+
Status: s.Status,
|
|
613
|
+
OfferedAt: s.OfferedAt?.toISOString() ?? null,
|
|
614
|
+
RespondedAt: s.RespondedAt?.toISOString() ?? null,
|
|
615
|
+
})), guard.DeniedFieldPatterns);
|
|
616
|
+
const payload = { Count: rows.length, Suggestions: rows };
|
|
617
|
+
return {
|
|
618
|
+
Success: true,
|
|
619
|
+
ResultCode: 'SUCCESS',
|
|
620
|
+
Message: JSON.stringify(payload),
|
|
621
|
+
Params: setOutputParam(params, 'SuggestionsJSON', JSON.stringify(payload)),
|
|
622
|
+
};
|
|
623
|
+
}
|
|
624
|
+
if (operation === 'recordoffer') {
|
|
625
|
+
const targetKey = readStringParam(params, 'TargetKey');
|
|
626
|
+
const summary = readStringParam(params, 'Summary');
|
|
627
|
+
const rawKind = readStringParam(params, 'Kind') ?? 'Rhythm';
|
|
628
|
+
const validKinds = [
|
|
629
|
+
'Rhythm',
|
|
630
|
+
'Skill',
|
|
631
|
+
'Behavior',
|
|
632
|
+
'Integration',
|
|
633
|
+
];
|
|
634
|
+
const kind = validKinds.find((k) => k.toLowerCase() === rawKind.toLowerCase());
|
|
635
|
+
if (!targetKey || !summary || !kind) {
|
|
636
|
+
return {
|
|
637
|
+
Success: false,
|
|
638
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
639
|
+
Message: 'RecordOffer requires TargetKey, Summary, and Kind (Rhythm, Skill, Behavior, or Integration).',
|
|
640
|
+
Params: params.Params,
|
|
641
|
+
};
|
|
642
|
+
}
|
|
643
|
+
const guard = await TypedActionPolicyGuard.CheckEntityAccess({
|
|
644
|
+
entityName: 'MJ: AI Agent Suggestions',
|
|
645
|
+
operation: 'Create',
|
|
646
|
+
contextUser,
|
|
647
|
+
actionContext: params.Context,
|
|
648
|
+
provider: md,
|
|
649
|
+
});
|
|
650
|
+
if (!guard.Allowed) {
|
|
651
|
+
return {
|
|
652
|
+
Success: false,
|
|
653
|
+
ResultCode: guard.ResultCode,
|
|
654
|
+
Message: guard.Message,
|
|
655
|
+
Params: params.Params,
|
|
656
|
+
};
|
|
657
|
+
}
|
|
658
|
+
const priorRes = await rv.RunView({
|
|
659
|
+
EntityName: 'MJ: AI Agent Suggestions',
|
|
660
|
+
ExtraFilter: `UserID='${EscapeSQLString(contextUser.ID)}' AND AgentID='${EscapeSQLString(agent.ID)}' AND LOWER(TargetKey)='${EscapeSQLString(targetKey.toLowerCase())}'`,
|
|
661
|
+
OrderBy: 'OfferedAt DESC',
|
|
662
|
+
MaxRows: 5,
|
|
663
|
+
ResultType: 'entity_object',
|
|
664
|
+
}, contextUser);
|
|
665
|
+
const priors = priorRes.Success ? priorRes.Results : [];
|
|
666
|
+
const declined = priors.find((p) => p.Status === 'Declined');
|
|
667
|
+
if (declined) {
|
|
668
|
+
return {
|
|
669
|
+
Success: false,
|
|
670
|
+
ResultCode: 'ALREADY_DECLINED',
|
|
671
|
+
Message: `Suggestion "${targetKey}" was previously declined by the user on ${declined.RespondedAt?.toISOString() ?? declined.OfferedAt?.toISOString()}; do not re-offer unless the user explicitly asks.`,
|
|
672
|
+
Params: params.Params,
|
|
673
|
+
};
|
|
674
|
+
}
|
|
675
|
+
const existingOffered = priors.find((p) => p.Status === 'Offered');
|
|
676
|
+
if (existingOffered) {
|
|
677
|
+
const payload = {
|
|
678
|
+
SuggestionID: existingOffered.ID,
|
|
679
|
+
Status: existingOffered.Status,
|
|
680
|
+
AlreadyOffered: true,
|
|
681
|
+
};
|
|
682
|
+
return {
|
|
683
|
+
Success: true,
|
|
684
|
+
ResultCode: 'SUCCESS',
|
|
685
|
+
Message: JSON.stringify(payload),
|
|
686
|
+
Params: setOutputParam(params, 'SuggestionID', existingOffered.ID),
|
|
687
|
+
};
|
|
688
|
+
}
|
|
689
|
+
const suggestion = await md.GetEntityObject('MJ: AI Agent Suggestions', contextUser);
|
|
690
|
+
suggestion.NewRecord();
|
|
691
|
+
suggestion.AgentID = agent.ID;
|
|
692
|
+
suggestion.UserID = contextUser.ID;
|
|
693
|
+
suggestion.Kind = kind;
|
|
694
|
+
suggestion.TargetKey = targetKey;
|
|
695
|
+
suggestion.Summary = summary;
|
|
696
|
+
suggestion.Status = 'Offered';
|
|
697
|
+
suggestion.OfferedAt = new Date();
|
|
698
|
+
const saved = await suggestion.Save();
|
|
699
|
+
if (!saved) {
|
|
700
|
+
return {
|
|
701
|
+
Success: false,
|
|
702
|
+
ResultCode: 'SAVE_FAILED',
|
|
703
|
+
Message: suggestion.LatestResult?.CompleteMessage || 'Failed to record agent suggestion.',
|
|
704
|
+
Params: params.Params,
|
|
705
|
+
};
|
|
706
|
+
}
|
|
707
|
+
const payload = {
|
|
708
|
+
SuggestionID: suggestion.ID,
|
|
709
|
+
Kind: suggestion.Kind,
|
|
710
|
+
TargetKey: suggestion.TargetKey,
|
|
711
|
+
Status: suggestion.Status,
|
|
712
|
+
};
|
|
713
|
+
return {
|
|
714
|
+
Success: true,
|
|
715
|
+
ResultCode: 'SUCCESS',
|
|
716
|
+
Message: JSON.stringify(payload),
|
|
717
|
+
Params: setOutputParam(params, 'SuggestionID', suggestion.ID),
|
|
718
|
+
};
|
|
719
|
+
}
|
|
720
|
+
if (operation === 'respond') {
|
|
721
|
+
const suggestionId = readStringParam(params, 'SuggestionID');
|
|
722
|
+
const targetKey = readStringParam(params, 'TargetKey');
|
|
723
|
+
const rawStatus = readStringParam(params, 'Status');
|
|
724
|
+
const validResponses = [
|
|
725
|
+
'Accepted',
|
|
726
|
+
'Declined',
|
|
727
|
+
'Expired',
|
|
728
|
+
];
|
|
729
|
+
const nextStatus = rawStatus
|
|
730
|
+
? validResponses.find((s) => s.toLowerCase() === rawStatus.toLowerCase())
|
|
731
|
+
: undefined;
|
|
732
|
+
if ((!suggestionId && !targetKey) || !nextStatus) {
|
|
733
|
+
return {
|
|
734
|
+
Success: false,
|
|
735
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
736
|
+
Message: 'Respond requires SuggestionID or TargetKey and Status (Accepted, Declined, or Expired).',
|
|
737
|
+
Params: params.Params,
|
|
738
|
+
};
|
|
739
|
+
}
|
|
740
|
+
const guard = await TypedActionPolicyGuard.CheckEntityAccess({
|
|
741
|
+
entityName: 'MJ: AI Agent Suggestions',
|
|
742
|
+
operation: 'Update',
|
|
743
|
+
writeFields: ['Status', 'RespondedAt'],
|
|
744
|
+
contextUser,
|
|
745
|
+
actionContext: params.Context,
|
|
746
|
+
provider: md,
|
|
747
|
+
});
|
|
748
|
+
if (!guard.Allowed) {
|
|
749
|
+
return {
|
|
750
|
+
Success: false,
|
|
751
|
+
ResultCode: guard.ResultCode,
|
|
752
|
+
Message: guard.Message,
|
|
753
|
+
Params: params.Params,
|
|
754
|
+
};
|
|
755
|
+
}
|
|
756
|
+
const filter = suggestionId
|
|
757
|
+
? `ID='${EscapeSQLString(suggestionId)}' AND UserID='${EscapeSQLString(contextUser.ID)}'`
|
|
758
|
+
: `UserID='${EscapeSQLString(contextUser.ID)}' AND AgentID='${EscapeSQLString(agent.ID)}' AND LOWER(TargetKey)='${EscapeSQLString(targetKey.toLowerCase())}'`;
|
|
759
|
+
const foundRes = await rv.RunView({
|
|
760
|
+
EntityName: 'MJ: AI Agent Suggestions',
|
|
761
|
+
ExtraFilter: filter,
|
|
762
|
+
OrderBy: 'OfferedAt DESC',
|
|
763
|
+
MaxRows: 1,
|
|
764
|
+
ResultType: 'entity_object',
|
|
765
|
+
}, contextUser);
|
|
766
|
+
const suggestion = foundRes.Success ? foundRes.Results[0] ?? null : null;
|
|
767
|
+
if (!suggestion) {
|
|
768
|
+
return {
|
|
769
|
+
Success: false,
|
|
770
|
+
ResultCode: 'NOT_FOUND',
|
|
771
|
+
Message: 'Matching agent suggestion was not found.',
|
|
772
|
+
Params: params.Params,
|
|
773
|
+
};
|
|
774
|
+
}
|
|
775
|
+
suggestion.Status = nextStatus;
|
|
776
|
+
suggestion.RespondedAt = new Date();
|
|
777
|
+
const saved = await suggestion.Save();
|
|
778
|
+
if (!saved) {
|
|
779
|
+
return {
|
|
780
|
+
Success: false,
|
|
781
|
+
ResultCode: 'SAVE_FAILED',
|
|
782
|
+
Message: suggestion.LatestResult?.CompleteMessage || 'Failed to update agent suggestion.',
|
|
783
|
+
Params: params.Params,
|
|
784
|
+
};
|
|
785
|
+
}
|
|
786
|
+
const payload = {
|
|
787
|
+
SuggestionID: suggestion.ID,
|
|
788
|
+
TargetKey: suggestion.TargetKey,
|
|
789
|
+
Status: suggestion.Status,
|
|
790
|
+
RespondedAt: suggestion.RespondedAt?.toISOString() ?? null,
|
|
791
|
+
};
|
|
792
|
+
return {
|
|
793
|
+
Success: true,
|
|
794
|
+
ResultCode: 'SUCCESS',
|
|
795
|
+
Message: JSON.stringify(payload),
|
|
796
|
+
Params: setOutputParam(params, 'SuggestionID', suggestion.ID),
|
|
797
|
+
};
|
|
798
|
+
}
|
|
799
|
+
return {
|
|
800
|
+
Success: false,
|
|
801
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
802
|
+
Message: `Unsupported Operation "${operation}". Expected List, RecordOffer, or Respond.`,
|
|
803
|
+
Params: params.Params,
|
|
804
|
+
};
|
|
805
|
+
}
|
|
806
|
+
catch (error) {
|
|
807
|
+
const msg = error instanceof Error ? error.message : String(error);
|
|
808
|
+
LogError(`[ManageAgentSuggestionsAction] ${msg}`);
|
|
809
|
+
return { Success: false, ResultCode: 'ERROR', Message: msg, Params: params.Params };
|
|
810
|
+
}
|
|
811
|
+
}
|
|
812
|
+
};
|
|
813
|
+
ManageAgentSuggestionsAction = __decorate([
|
|
814
|
+
RegisterClass(BaseAction, 'Manage Agent Suggestions')
|
|
815
|
+
], ManageAgentSuggestionsAction);
|
|
816
|
+
export { ManageAgentSuggestionsAction };
|
|
817
|
+
//# sourceMappingURL=chief-of-staff-rhythms-suggestions.action.js.map
|