@memberjunction/core-actions 6.3.0-edge.0 → 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 +13 -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/test-runtime-action.action.d.ts.map +1 -1
- package/dist/custom/ai/test-runtime-action.action.js +6 -1
- package/dist/custom/ai/test-runtime-action.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/artifacts/base-governed-artifact.action.d.ts +30 -0
- package/dist/custom/artifacts/base-governed-artifact.action.d.ts.map +1 -0
- package/dist/custom/artifacts/base-governed-artifact.action.js +201 -0
- package/dist/custom/artifacts/base-governed-artifact.action.js.map +1 -0
- package/dist/custom/artifacts/create-governed-artifact.action.d.ts +11 -0
- package/dist/custom/artifacts/create-governed-artifact.action.d.ts.map +1 -0
- package/dist/custom/artifacts/create-governed-artifact.action.js +104 -0
- package/dist/custom/artifacts/create-governed-artifact.action.js.map +1 -0
- package/dist/custom/artifacts/index.d.ts +6 -0
- package/dist/custom/artifacts/index.d.ts.map +1 -0
- package/dist/custom/artifacts/index.js +6 -0
- package/dist/custom/artifacts/index.js.map +1 -0
- package/dist/custom/artifacts/inspect-governed-artifact.action.d.ts +11 -0
- package/dist/custom/artifacts/inspect-governed-artifact.action.d.ts.map +1 -0
- package/dist/custom/artifacts/inspect-governed-artifact.action.js +84 -0
- package/dist/custom/artifacts/inspect-governed-artifact.action.js.map +1 -0
- package/dist/custom/artifacts/list-workspace-artifacts.action.d.ts +11 -0
- package/dist/custom/artifacts/list-workspace-artifacts.action.d.ts.map +1 -0
- package/dist/custom/artifacts/list-workspace-artifacts.action.js +63 -0
- package/dist/custom/artifacts/list-workspace-artifacts.action.js.map +1 -0
- package/dist/custom/artifacts/mutate-governed-artifact.action.d.ts +12 -0
- package/dist/custom/artifacts/mutate-governed-artifact.action.d.ts.map +1 -0
- package/dist/custom/artifacts/mutate-governed-artifact.action.js +110 -0
- package/dist/custom/artifacts/mutate-governed-artifact.action.js.map +1 -0
- package/dist/custom/crud/agent-data-policy-guard.d.ts +9 -1
- package/dist/custom/crud/agent-data-policy-guard.d.ts.map +1 -1
- package/dist/custom/crud/agent-data-policy-guard.js +49 -16
- package/dist/custom/crud/agent-data-policy-guard.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/demo/text-analyzer.action.d.ts +8 -0
- package/dist/custom/demo/text-analyzer.action.d.ts.map +1 -1
- package/dist/custom/demo/text-analyzer.action.js +34 -2
- package/dist/custom/demo/text-analyzer.action.js.map +1 -1
- package/dist/custom/files/excel-reader.action.d.ts +14 -0
- package/dist/custom/files/excel-reader.action.d.ts.map +1 -1
- package/dist/custom/files/excel-reader.action.js +51 -6
- package/dist/custom/files/excel-reader.action.js.map +1 -1
- package/dist/custom/github/github-client.d.ts.map +1 -1
- package/dist/custom/github/github-client.js +72 -11
- package/dist/custom/github/github-client.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/lists/refresh-list-from-source.action.d.ts.map +1 -1
- package/dist/custom/lists/refresh-list-from-source.action.js +3 -2
- package/dist/custom/lists/refresh-list-from-source.action.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/user-management/validate-email-unique.action.d.ts.map +1 -1
- package/dist/custom/user-management/validate-email-unique.action.js +2 -3
- package/dist/custom/user-management/validate-email-unique.action.js.map +1 -1
- package/dist/custom/utilities/refresh-and-snapshot-artifact.action.d.ts +48 -0
- package/dist/custom/utilities/refresh-and-snapshot-artifact.action.d.ts.map +1 -0
- package/dist/custom/utilities/refresh-and-snapshot-artifact.action.js +248 -0
- package/dist/custom/utilities/refresh-and-snapshot-artifact.action.js.map +1 -0
- package/dist/custom/web/rss-feed-parsing.d.ts.map +1 -1
- package/dist/custom/web/rss-feed-parsing.js +80 -6
- package/dist/custom/web/rss-feed-parsing.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/custom/web/web-content-transforms.d.ts +35 -0
- package/dist/custom/web/web-content-transforms.d.ts.map +1 -0
- package/dist/custom/web/web-content-transforms.js +157 -0
- package/dist/custom/web/web-content-transforms.js.map +1 -0
- package/dist/custom/web/web-page-content.action.d.ts +4 -20
- package/dist/custom/web/web-page-content.action.d.ts.map +1 -1
- package/dist/custom/web/web-page-content.action.js +23 -168
- package/dist/custom/web/web-page-content.action.js.map +1 -1
- package/dist/custom/web/worker/ServeWorkerRequests.d.ts +26 -0
- package/dist/custom/web/worker/ServeWorkerRequests.d.ts.map +1 -0
- package/dist/custom/web/worker/ServeWorkerRequests.js +33 -0
- package/dist/custom/web/worker/ServeWorkerRequests.js.map +1 -0
- package/dist/custom/web/worker/WebContentWorker.d.ts +9 -0
- package/dist/custom/web/worker/WebContentWorker.d.ts.map +1 -0
- package/dist/custom/web/worker/WebContentWorker.js +11 -0
- package/dist/custom/web/worker/WebContentWorker.js.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerHandler.d.ts +11 -0
- package/dist/custom/web/worker/WebContentWorkerHandler.d.ts.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerHandler.js +19 -0
- package/dist/custom/web/worker/WebContentWorkerHandler.js.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerHost.d.ts +26 -0
- package/dist/custom/web/worker/WebContentWorkerHost.d.ts.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerHost.js +37 -0
- package/dist/custom/web/worker/WebContentWorkerHost.js.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerProtocol.d.ts +23 -0
- package/dist/custom/web/worker/WebContentWorkerProtocol.d.ts.map +1 -0
- package/dist/custom/web/worker/WebContentWorkerProtocol.js +8 -0
- package/dist/custom/web/worker/WebContentWorkerProtocol.js.map +1 -0
- package/dist/custom/web/worker/WorkerProtocol.d.ts +36 -0
- package/dist/custom/web/worker/WorkerProtocol.d.ts.map +1 -0
- package/dist/custom/web/worker/WorkerProtocol.js +9 -0
- package/dist/custom/web/worker/WorkerProtocol.js.map +1 -0
- package/dist/custom/web/worker/WorkerThreadHost.d.ts +88 -0
- package/dist/custom/web/worker/WorkerThreadHost.d.ts.map +1 -0
- package/dist/custom/web/worker/WorkerThreadHost.js +174 -0
- package/dist/custom/web/worker/WorkerThreadHost.js.map +1 -0
- package/dist/custom/workflow/loop.action.d.ts +8 -0
- package/dist/custom/workflow/loop.action.d.ts.map +1 -1
- package/dist/custom/workflow/loop.action.js +36 -4
- package/dist/custom/workflow/loop.action.js.map +1 -1
- package/dist/index.d.ts +9 -1
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +10 -2
- package/dist/index.js.map +1 -1
- package/dist/tsconfig.tsbuildinfo +1 -0
- package/package.json +45 -45
|
@@ -0,0 +1,752 @@
|
|
|
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, UUIDsEqual, UUIDSet } from '@memberjunction/global';
|
|
9
|
+
import { CompositeKey, LogError, Metadata, RunView } from '@memberjunction/core';
|
|
10
|
+
import { AgentUserPolicyEngine, ApplyDefaultAgentUserPolicyValues, ParseAgentInstantiationConfig, } from '@memberjunction/ai-core-plus';
|
|
11
|
+
import { AIEngine } from '@memberjunction/aiengine';
|
|
12
|
+
/**
|
|
13
|
+
* Creates, updates, lists, promotes to primary, or archives a personal AI Agent instance derived
|
|
14
|
+
* from an instantiable prototype agent (such as `Chief of Staff`, `Research Agent`, or `Query Builder`).
|
|
15
|
+
*
|
|
16
|
+
* Atomically manages:
|
|
17
|
+
* 1. The derived `MJ: AI Agents` row (`BaseAgentID`, `OwnerUserID = contextUser.ID`, `Name`,
|
|
18
|
+
* `Description`, `IconClass`, `LogoURL`, `AdditionalInstructions`, `IsPrimaryInstance`,
|
|
19
|
+
* `InstantiationMode = 'None'`, `Status = 'Active'`).
|
|
20
|
+
* 2. Its optional `MJ: AI Agent Personas` link + `StyleOverrideObject` (`Tone`, `SpeakingStyle`)
|
|
21
|
+
* unifying text chat and Realtime voice personality.
|
|
22
|
+
* 3. Its optional `MJ: AI Agent User Policies` row (`CoordinationMode`, `OnboardingStatus = 'Completed'`).
|
|
23
|
+
* 4. Optional `MJ: Conversations.DefaultAgentID` re-pinning so conversational onboarding
|
|
24
|
+
* seamlessly transitions into the user's freshly personalized instance in the very same thread.
|
|
25
|
+
*/
|
|
26
|
+
let ManageMyAgentInstanceAction = class ManageMyAgentInstanceAction extends BaseAction {
|
|
27
|
+
static { this.AuthorizesCaller = true; }
|
|
28
|
+
async InternalRunAction(params) {
|
|
29
|
+
try {
|
|
30
|
+
const contextUser = params.ContextUser;
|
|
31
|
+
if (!contextUser?.ID) {
|
|
32
|
+
return {
|
|
33
|
+
Success: false,
|
|
34
|
+
ResultCode: 'PERMISSION_DENIED',
|
|
35
|
+
Message: 'Manage My Agent Instance requires a valid ContextUser.',
|
|
36
|
+
Params: params.Params,
|
|
37
|
+
};
|
|
38
|
+
}
|
|
39
|
+
const operation = this.parseOperation(this.getStringParam(params, 'Operation'));
|
|
40
|
+
if (!operation) {
|
|
41
|
+
return {
|
|
42
|
+
Success: false,
|
|
43
|
+
ResultCode: 'INVALID_OPERATION',
|
|
44
|
+
Message: `Unsupported Operation "${this.getStringParam(params, 'Operation') ?? ''}". Expected one of: Create, Update, ListMine, SetPrimary, Archive.`,
|
|
45
|
+
Params: params.Params,
|
|
46
|
+
};
|
|
47
|
+
}
|
|
48
|
+
await AIEngine.Instance.Config(false, contextUser);
|
|
49
|
+
const md = params.Provider ?? Metadata.Provider;
|
|
50
|
+
switch (operation) {
|
|
51
|
+
case 'Create':
|
|
52
|
+
return await this.handleCreate(params, contextUser, md);
|
|
53
|
+
case 'Update':
|
|
54
|
+
return await this.handleUpdate(params, contextUser, md);
|
|
55
|
+
case 'ListMine':
|
|
56
|
+
return this.handleListMine(params, contextUser);
|
|
57
|
+
case 'SetPrimary':
|
|
58
|
+
return await this.handleSetPrimary(params, contextUser, md);
|
|
59
|
+
case 'Archive':
|
|
60
|
+
return await this.handleArchive(params, contextUser, md);
|
|
61
|
+
}
|
|
62
|
+
}
|
|
63
|
+
catch (error) {
|
|
64
|
+
const message = error instanceof Error ? error.message : String(error);
|
|
65
|
+
LogError(`ManageMyAgentInstanceAction error: ${message}`);
|
|
66
|
+
return {
|
|
67
|
+
Success: false,
|
|
68
|
+
ResultCode: 'SAVE_FAILED',
|
|
69
|
+
Message: `Error managing agent instance: ${message}`,
|
|
70
|
+
Params: params.Params,
|
|
71
|
+
};
|
|
72
|
+
}
|
|
73
|
+
}
|
|
74
|
+
async handleCreate(params, contextUser, md) {
|
|
75
|
+
const baseAgent = this.resolveBaseAgentForCreate(params);
|
|
76
|
+
if (!baseAgent) {
|
|
77
|
+
return {
|
|
78
|
+
Success: false,
|
|
79
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
80
|
+
Message: 'Could not resolve the base prototype agent. Provide BaseAgentID or BaseAgentName.',
|
|
81
|
+
Params: params.Params,
|
|
82
|
+
};
|
|
83
|
+
}
|
|
84
|
+
if (!AIEngine.Instance.CanUserRunAgent(baseAgent.ID, contextUser)) {
|
|
85
|
+
return {
|
|
86
|
+
Success: false,
|
|
87
|
+
ResultCode: 'PERMISSION_DENIED',
|
|
88
|
+
Message: `You do not have permission to instantiate "${baseAgent.Name}".`,
|
|
89
|
+
Params: params.Params,
|
|
90
|
+
};
|
|
91
|
+
}
|
|
92
|
+
const isOwner = contextUser.Type === 'Owner';
|
|
93
|
+
if (baseAgent.InstantiationMode === 'None' && !isOwner) {
|
|
94
|
+
return {
|
|
95
|
+
Success: false,
|
|
96
|
+
ResultCode: 'PERMISSION_DENIED',
|
|
97
|
+
Message: `Agent "${baseAgent.Name}" does not allow personal instantiation (InstantiationMode = 'None').`,
|
|
98
|
+
Params: params.Params,
|
|
99
|
+
};
|
|
100
|
+
}
|
|
101
|
+
const rawName = this.getStringParam(params, 'Name')?.trim();
|
|
102
|
+
if (!rawName) {
|
|
103
|
+
return {
|
|
104
|
+
Success: false,
|
|
105
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
106
|
+
Message: 'A non-empty Name is required to create an agent instance.',
|
|
107
|
+
Params: params.Params,
|
|
108
|
+
};
|
|
109
|
+
}
|
|
110
|
+
// Check maxInstancesPerUser hint before DB roundtrip
|
|
111
|
+
const instConfig = ParseAgentInstantiationConfig(baseAgent.TypeConfiguration);
|
|
112
|
+
const maxPerUser = instConfig?.maxInstancesPerUser;
|
|
113
|
+
if (maxPerUser != null && maxPerUser > 0) {
|
|
114
|
+
const rootBase = AIEngine.Instance.GetRootAgent(baseAgent.ID) ?? baseAgent;
|
|
115
|
+
const existingForRoot = AIEngine.Instance.GetUserDerivedAgents(contextUser).filter((a) => {
|
|
116
|
+
if (a.Status !== 'Active' || !UUIDsEqual(a.OwnerUserID, contextUser.ID)) {
|
|
117
|
+
return false;
|
|
118
|
+
}
|
|
119
|
+
const r = AIEngine.Instance.GetRootAgent(a.ID);
|
|
120
|
+
return r != null && UUIDsEqual(r.ID, rootBase.ID);
|
|
121
|
+
});
|
|
122
|
+
if (existingForRoot.length >= maxPerUser) {
|
|
123
|
+
return {
|
|
124
|
+
Success: false,
|
|
125
|
+
ResultCode: 'VALIDATION_ERROR',
|
|
126
|
+
Message: `Maximum of ${maxPerUser} active personal instances for "${baseAgent.Name}" reached. Archive an existing instance or update one instead.`,
|
|
127
|
+
Params: params.Params,
|
|
128
|
+
};
|
|
129
|
+
}
|
|
130
|
+
}
|
|
131
|
+
const effectiveBase = AIEngine.Instance.ResolveEffectiveAgentProperties(baseAgent);
|
|
132
|
+
const description = this.getStringParam(params, 'Description');
|
|
133
|
+
const iconClass = this.getStringParam(params, 'IconClass');
|
|
134
|
+
const logoUrl = this.getStringParam(params, 'LogoURL');
|
|
135
|
+
const additionalInstructions = this.getStringParam(params, 'AdditionalInstructions');
|
|
136
|
+
const isPrimary = this.getBooleanParam(params, 'IsPrimaryInstance');
|
|
137
|
+
const personaId = this.getStringParam(params, 'PersonaID');
|
|
138
|
+
const personaName = this.getStringParam(params, 'PersonaName');
|
|
139
|
+
const tone = this.getStringParam(params, 'Tone');
|
|
140
|
+
const speakingStyle = this.getStringParam(params, 'SpeakingStyle');
|
|
141
|
+
const coordinationMode = this.parseCoordinationMode(this.getStringParam(params, 'CoordinationMode'));
|
|
142
|
+
const conversationId = this.getStringParam(params, 'ConversationID') ?? this.extractContext(params.Context).ConversationID ?? null;
|
|
143
|
+
const agentEntity = await md.GetEntityObject('MJ: AI Agents', contextUser);
|
|
144
|
+
agentEntity.NewRecord();
|
|
145
|
+
agentEntity.BaseAgentID = baseAgent.ID;
|
|
146
|
+
agentEntity.OwnerUserID = contextUser.ID;
|
|
147
|
+
agentEntity.Name = rawName;
|
|
148
|
+
agentEntity.Description = description ?? `Personal ${baseAgent.Name} instance`;
|
|
149
|
+
agentEntity.IconClass = iconClass ?? baseAgent.IconClass ?? null;
|
|
150
|
+
agentEntity.LogoURL = logoUrl ?? null;
|
|
151
|
+
agentEntity.AdditionalInstructions = additionalInstructions ?? null;
|
|
152
|
+
agentEntity.InstantiationMode = 'None';
|
|
153
|
+
agentEntity.Status = 'Active';
|
|
154
|
+
if (effectiveBase.TypeID) {
|
|
155
|
+
agentEntity.TypeID = effectiveBase.TypeID;
|
|
156
|
+
}
|
|
157
|
+
else if (baseAgent.TypeID) {
|
|
158
|
+
agentEntity.TypeID = baseAgent.TypeID;
|
|
159
|
+
}
|
|
160
|
+
if (isPrimary !== null) {
|
|
161
|
+
agentEntity.IsPrimaryInstance = isPrimary;
|
|
162
|
+
}
|
|
163
|
+
const agentSaved = await agentEntity.Save();
|
|
164
|
+
if (!agentSaved) {
|
|
165
|
+
return {
|
|
166
|
+
Success: false,
|
|
167
|
+
ResultCode: 'SAVE_FAILED',
|
|
168
|
+
Message: `Failed to create agent instance "${rawName}": ${agentEntity.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
169
|
+
Params: params.Params,
|
|
170
|
+
};
|
|
171
|
+
}
|
|
172
|
+
// Refresh AIEngine immediately so MJAIAgentPersonaEntityServer and MJAIAgentUserPolicyEntityServer
|
|
173
|
+
// see the newly minted derived child in cache during sync Validate() / ValidateAsync().
|
|
174
|
+
await AIEngine.Instance.Config(true, contextUser);
|
|
175
|
+
// Attach Persona + StyleOverride when requested
|
|
176
|
+
let resolvedPersona = null;
|
|
177
|
+
if (personaId || personaName || tone || speakingStyle) {
|
|
178
|
+
resolvedPersona = this.resolvePersonaEntity(personaId, personaName, baseAgent.ID);
|
|
179
|
+
if (resolvedPersona) {
|
|
180
|
+
const personaResult = await this.upsertDefaultAgentPersona(agentEntity.ID, resolvedPersona.ID, tone, speakingStyle, contextUser, md);
|
|
181
|
+
if (!personaResult.success) {
|
|
182
|
+
return {
|
|
183
|
+
Success: false,
|
|
184
|
+
ResultCode: 'SAVE_FAILED',
|
|
185
|
+
Message: personaResult.errorMessage,
|
|
186
|
+
Params: params.Params,
|
|
187
|
+
};
|
|
188
|
+
}
|
|
189
|
+
}
|
|
190
|
+
}
|
|
191
|
+
// Initialize AIAgentUserPolicy when CoordinationMode is passed or base is Chief of Staff
|
|
192
|
+
const rootBase = AIEngine.Instance.GetRootAgent(baseAgent.ID) ?? baseAgent;
|
|
193
|
+
const isChiefOfStaff = rootBase.Name.trim().toLowerCase() === 'chief of staff';
|
|
194
|
+
if (coordinationMode || isChiefOfStaff) {
|
|
195
|
+
const policyResult = await this.upsertUserPolicyForInstance(agentEntity.ID, agentEntity.Name, coordinationMode, resolvedPersona?.ID ?? null, contextUser, md);
|
|
196
|
+
if (!policyResult.success) {
|
|
197
|
+
return {
|
|
198
|
+
Success: false,
|
|
199
|
+
ResultCode: 'SAVE_FAILED',
|
|
200
|
+
Message: policyResult.errorMessage,
|
|
201
|
+
Params: params.Params,
|
|
202
|
+
};
|
|
203
|
+
}
|
|
204
|
+
}
|
|
205
|
+
// Re-pin the active onboarding conversation so the very next turn runs as the new instance
|
|
206
|
+
let conversationPinned = false;
|
|
207
|
+
if (conversationId) {
|
|
208
|
+
conversationPinned = await this.repinConversationToInstance(conversationId, agentEntity.ID, agentEntity.Name, contextUser, md);
|
|
209
|
+
}
|
|
210
|
+
// Final cache refresh so UI & subsequent turns immediately see the complete instance + persona
|
|
211
|
+
await AIEngine.Instance.Config(true, contextUser);
|
|
212
|
+
AgentUserPolicyEngine.Instance.ClearCache(agentEntity.ID, contextUser.ID);
|
|
213
|
+
const refreshed = AIEngine.Instance.GetAgentByID(agentEntity.ID);
|
|
214
|
+
const summary = this.buildInstanceSummary(refreshed ?? agentEntity, conversationPinned);
|
|
215
|
+
this.setOutputParam(params, 'InstanceAgentID', agentEntity.ID);
|
|
216
|
+
this.setOutputParam(params, 'InstancesJSON', JSON.stringify(summary));
|
|
217
|
+
return {
|
|
218
|
+
Success: true,
|
|
219
|
+
ResultCode: 'SUCCESS',
|
|
220
|
+
Message: `Created personal ${baseAgent.Name} instance "${agentEntity.Name}" (ID: ${agentEntity.ID})${conversationPinned ? ' and switched this conversation to it' : ''}.`,
|
|
221
|
+
Params: params.Params,
|
|
222
|
+
};
|
|
223
|
+
}
|
|
224
|
+
async handleUpdate(params, contextUser, md) {
|
|
225
|
+
const target = this.resolveOwnedDerivedInstance(params, contextUser);
|
|
226
|
+
if ('errorResult' in target) {
|
|
227
|
+
return target.errorResult;
|
|
228
|
+
}
|
|
229
|
+
const existingAgent = target.agent;
|
|
230
|
+
const entity = await md.GetEntityObject('MJ: AI Agents', contextUser);
|
|
231
|
+
const loaded = await entity.InnerLoad(CompositeKey.FromID(existingAgent.ID));
|
|
232
|
+
if (!loaded) {
|
|
233
|
+
return {
|
|
234
|
+
Success: false,
|
|
235
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
236
|
+
Message: `Failed to load agent instance "${existingAgent.ID}" for update.`,
|
|
237
|
+
Params: params.Params,
|
|
238
|
+
};
|
|
239
|
+
}
|
|
240
|
+
const name = this.getStringParam(params, 'Name');
|
|
241
|
+
const description = this.getStringParam(params, 'Description');
|
|
242
|
+
const iconClass = this.getStringParam(params, 'IconClass');
|
|
243
|
+
const logoUrl = this.getStringParam(params, 'LogoURL');
|
|
244
|
+
const additionalInstructions = this.getStringParam(params, 'AdditionalInstructions');
|
|
245
|
+
const isPrimary = this.getBooleanParam(params, 'IsPrimaryInstance');
|
|
246
|
+
const personaId = this.getStringParam(params, 'PersonaID');
|
|
247
|
+
const personaName = this.getStringParam(params, 'PersonaName');
|
|
248
|
+
const tone = this.getStringParam(params, 'Tone');
|
|
249
|
+
const speakingStyle = this.getStringParam(params, 'SpeakingStyle');
|
|
250
|
+
const coordinationMode = this.parseCoordinationMode(this.getStringParam(params, 'CoordinationMode'));
|
|
251
|
+
let agentDirty = false;
|
|
252
|
+
if (name !== null && name.trim().length > 0) {
|
|
253
|
+
entity.Name = name.trim();
|
|
254
|
+
agentDirty = true;
|
|
255
|
+
}
|
|
256
|
+
if (description !== null) {
|
|
257
|
+
entity.Description = description;
|
|
258
|
+
agentDirty = true;
|
|
259
|
+
}
|
|
260
|
+
if (iconClass !== null) {
|
|
261
|
+
entity.IconClass = iconClass;
|
|
262
|
+
agentDirty = true;
|
|
263
|
+
}
|
|
264
|
+
if (logoUrl !== null) {
|
|
265
|
+
entity.LogoURL = logoUrl;
|
|
266
|
+
agentDirty = true;
|
|
267
|
+
}
|
|
268
|
+
if (additionalInstructions !== null) {
|
|
269
|
+
entity.AdditionalInstructions = additionalInstructions;
|
|
270
|
+
agentDirty = true;
|
|
271
|
+
}
|
|
272
|
+
if (isPrimary !== null) {
|
|
273
|
+
entity.IsPrimaryInstance = isPrimary;
|
|
274
|
+
agentDirty = true;
|
|
275
|
+
}
|
|
276
|
+
if (agentDirty) {
|
|
277
|
+
const saved = await entity.Save();
|
|
278
|
+
if (!saved) {
|
|
279
|
+
return {
|
|
280
|
+
Success: false,
|
|
281
|
+
ResultCode: 'SAVE_FAILED',
|
|
282
|
+
Message: `Failed to update agent instance "${entity.Name}": ${entity.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
283
|
+
Params: params.Params,
|
|
284
|
+
};
|
|
285
|
+
}
|
|
286
|
+
}
|
|
287
|
+
let resolvedPersona = null;
|
|
288
|
+
if (personaId || personaName || tone !== null || speakingStyle !== null) {
|
|
289
|
+
resolvedPersona = this.resolvePersonaEntity(personaId, personaName, entity.ID);
|
|
290
|
+
if (resolvedPersona) {
|
|
291
|
+
const personaResult = await this.upsertDefaultAgentPersona(entity.ID, resolvedPersona.ID, tone, speakingStyle, contextUser, md);
|
|
292
|
+
if (!personaResult.success) {
|
|
293
|
+
return {
|
|
294
|
+
Success: false,
|
|
295
|
+
ResultCode: 'SAVE_FAILED',
|
|
296
|
+
Message: personaResult.errorMessage,
|
|
297
|
+
Params: params.Params,
|
|
298
|
+
};
|
|
299
|
+
}
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
if (coordinationMode || name !== null || resolvedPersona) {
|
|
303
|
+
const existingPolicy = await AgentUserPolicyEngine.Instance.ResolvePolicyForAgentAndUser(entity.ID, contextUser.ID, contextUser, { forceRefresh: true, provider: md });
|
|
304
|
+
if (existingPolicy.status === 'found' && existingPolicy.policy) {
|
|
305
|
+
const policyResult = await this.upsertUserPolicyForInstance(entity.ID, entity.Name, coordinationMode, resolvedPersona?.ID ?? null, contextUser, md);
|
|
306
|
+
if (!policyResult.success) {
|
|
307
|
+
return {
|
|
308
|
+
Success: false,
|
|
309
|
+
ResultCode: 'SAVE_FAILED',
|
|
310
|
+
Message: policyResult.errorMessage,
|
|
311
|
+
Params: params.Params,
|
|
312
|
+
};
|
|
313
|
+
}
|
|
314
|
+
}
|
|
315
|
+
else if (coordinationMode) {
|
|
316
|
+
const policyResult = await this.upsertUserPolicyForInstance(entity.ID, entity.Name, coordinationMode, resolvedPersona?.ID ?? null, contextUser, md);
|
|
317
|
+
if (!policyResult.success) {
|
|
318
|
+
return {
|
|
319
|
+
Success: false,
|
|
320
|
+
ResultCode: 'SAVE_FAILED',
|
|
321
|
+
Message: policyResult.errorMessage,
|
|
322
|
+
Params: params.Params,
|
|
323
|
+
};
|
|
324
|
+
}
|
|
325
|
+
}
|
|
326
|
+
}
|
|
327
|
+
await AIEngine.Instance.Config(true, contextUser);
|
|
328
|
+
AgentUserPolicyEngine.Instance.ClearCache(entity.ID, contextUser.ID);
|
|
329
|
+
const refreshed = AIEngine.Instance.GetAgentByID(entity.ID);
|
|
330
|
+
const summary = this.buildInstanceSummary(refreshed ?? entity, false);
|
|
331
|
+
this.setOutputParam(params, 'InstanceAgentID', entity.ID);
|
|
332
|
+
this.setOutputParam(params, 'InstancesJSON', JSON.stringify(summary));
|
|
333
|
+
return {
|
|
334
|
+
Success: true,
|
|
335
|
+
ResultCode: 'SUCCESS',
|
|
336
|
+
Message: `Updated agent instance "${entity.Name}" (ID: ${entity.ID}).`,
|
|
337
|
+
Params: params.Params,
|
|
338
|
+
};
|
|
339
|
+
}
|
|
340
|
+
handleListMine(params, contextUser) {
|
|
341
|
+
const baseIdFilter = this.getStringParam(params, 'BaseAgentID');
|
|
342
|
+
const baseNameFilter = this.getStringParam(params, 'BaseAgentName')?.trim().toLowerCase() ?? null;
|
|
343
|
+
let instances = AIEngine.Instance.GetUserDerivedAgents(contextUser);
|
|
344
|
+
if (baseIdFilter) {
|
|
345
|
+
instances = instances.filter((agent) => {
|
|
346
|
+
const chain = AIEngine.Instance.GetAgentInheritanceChain(agent.ID);
|
|
347
|
+
const ancestorIds = new UUIDSet(chain.slice(0, -1).map((ancestor) => ancestor.ID));
|
|
348
|
+
return ancestorIds.Has(baseIdFilter);
|
|
349
|
+
});
|
|
350
|
+
}
|
|
351
|
+
else if (baseNameFilter) {
|
|
352
|
+
instances = instances.filter((agent) => {
|
|
353
|
+
const chain = AIEngine.Instance.GetAgentInheritanceChain(agent.ID);
|
|
354
|
+
return chain.slice(0, -1).some((ancestor) => ancestor.Name.trim().toLowerCase() === baseNameFilter);
|
|
355
|
+
});
|
|
356
|
+
}
|
|
357
|
+
const summaries = instances.map((agent) => this.buildInstanceSummary(agent, false));
|
|
358
|
+
this.setOutputParam(params, 'InstancesJSON', JSON.stringify(summaries));
|
|
359
|
+
return {
|
|
360
|
+
Success: true,
|
|
361
|
+
ResultCode: 'SUCCESS',
|
|
362
|
+
Message: `Found ${summaries.length} personal or shared agent instance(s).`,
|
|
363
|
+
Params: params.Params,
|
|
364
|
+
};
|
|
365
|
+
}
|
|
366
|
+
async handleSetPrimary(params, contextUser, md) {
|
|
367
|
+
const target = this.resolveOwnedDerivedInstance(params, contextUser);
|
|
368
|
+
if ('errorResult' in target) {
|
|
369
|
+
return target.errorResult;
|
|
370
|
+
}
|
|
371
|
+
const existingAgent = target.agent;
|
|
372
|
+
const entity = await md.GetEntityObject('MJ: AI Agents', contextUser);
|
|
373
|
+
const loaded = await entity.InnerLoad(CompositeKey.FromID(existingAgent.ID));
|
|
374
|
+
if (!loaded) {
|
|
375
|
+
return {
|
|
376
|
+
Success: false,
|
|
377
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
378
|
+
Message: `Failed to load agent instance "${existingAgent.ID}".`,
|
|
379
|
+
Params: params.Params,
|
|
380
|
+
};
|
|
381
|
+
}
|
|
382
|
+
entity.IsPrimaryInstance = true;
|
|
383
|
+
const saved = await entity.Save();
|
|
384
|
+
if (!saved) {
|
|
385
|
+
return {
|
|
386
|
+
Success: false,
|
|
387
|
+
ResultCode: 'SAVE_FAILED',
|
|
388
|
+
Message: `Failed to promote "${entity.Name}" to primary: ${entity.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
389
|
+
Params: params.Params,
|
|
390
|
+
};
|
|
391
|
+
}
|
|
392
|
+
await AIEngine.Instance.Config(true, contextUser);
|
|
393
|
+
const refreshed = AIEngine.Instance.GetAgentByID(entity.ID);
|
|
394
|
+
const summary = this.buildInstanceSummary(refreshed ?? entity, false);
|
|
395
|
+
this.setOutputParam(params, 'InstanceAgentID', entity.ID);
|
|
396
|
+
this.setOutputParam(params, 'InstancesJSON', JSON.stringify(summary));
|
|
397
|
+
return {
|
|
398
|
+
Success: true,
|
|
399
|
+
ResultCode: 'SUCCESS',
|
|
400
|
+
Message: `"${entity.Name}" is now your primary instance.`,
|
|
401
|
+
Params: params.Params,
|
|
402
|
+
};
|
|
403
|
+
}
|
|
404
|
+
async handleArchive(params, contextUser, md) {
|
|
405
|
+
const target = this.resolveOwnedDerivedInstance(params, contextUser);
|
|
406
|
+
if ('errorResult' in target) {
|
|
407
|
+
return target.errorResult;
|
|
408
|
+
}
|
|
409
|
+
const existingAgent = target.agent;
|
|
410
|
+
const entity = await md.GetEntityObject('MJ: AI Agents', contextUser);
|
|
411
|
+
const loaded = await entity.InnerLoad(CompositeKey.FromID(existingAgent.ID));
|
|
412
|
+
if (!loaded) {
|
|
413
|
+
return {
|
|
414
|
+
Success: false,
|
|
415
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
416
|
+
Message: `Failed to load agent instance "${existingAgent.ID}".`,
|
|
417
|
+
Params: params.Params,
|
|
418
|
+
};
|
|
419
|
+
}
|
|
420
|
+
entity.Status = 'Disabled';
|
|
421
|
+
entity.IsPrimaryInstance = false;
|
|
422
|
+
const saved = await entity.Save();
|
|
423
|
+
if (!saved) {
|
|
424
|
+
return {
|
|
425
|
+
Success: false,
|
|
426
|
+
ResultCode: 'SAVE_FAILED',
|
|
427
|
+
Message: `Failed to archive "${entity.Name}": ${entity.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
428
|
+
Params: params.Params,
|
|
429
|
+
};
|
|
430
|
+
}
|
|
431
|
+
await AIEngine.Instance.Config(true, contextUser);
|
|
432
|
+
const summary = this.buildInstanceSummary(entity, false);
|
|
433
|
+
this.setOutputParam(params, 'InstanceAgentID', entity.ID);
|
|
434
|
+
this.setOutputParam(params, 'InstancesJSON', JSON.stringify(summary));
|
|
435
|
+
return {
|
|
436
|
+
Success: true,
|
|
437
|
+
ResultCode: 'SUCCESS',
|
|
438
|
+
Message: `Archived agent instance "${entity.Name}" (ID: ${entity.ID}).`,
|
|
439
|
+
Params: params.Params,
|
|
440
|
+
};
|
|
441
|
+
}
|
|
442
|
+
resolveBaseAgentForCreate(params) {
|
|
443
|
+
const baseAgentId = this.getStringParam(params, 'BaseAgentID');
|
|
444
|
+
if (baseAgentId) {
|
|
445
|
+
return AIEngine.Instance.GetAgentByID(baseAgentId);
|
|
446
|
+
}
|
|
447
|
+
const baseAgentName = this.getStringParam(params, 'BaseAgentName');
|
|
448
|
+
if (baseAgentName) {
|
|
449
|
+
const norm = baseAgentName.trim().toLowerCase();
|
|
450
|
+
// Prefer root/template agents matching BaseAgentName
|
|
451
|
+
const rootMatch = AIEngine.Instance.Agents.find((a) => !a.BaseAgentID && a.Status === 'Active' && a.Name.trim().toLowerCase() === norm);
|
|
452
|
+
if (rootMatch) {
|
|
453
|
+
return rootMatch;
|
|
454
|
+
}
|
|
455
|
+
return AIEngine.Instance.Agents.find((a) => a.Status === 'Active' && a.Name.trim().toLowerCase() === norm);
|
|
456
|
+
}
|
|
457
|
+
const contextAgentId = this.extractContext(params.Context).AgentID ?? AgentUserPolicyEngine.Instance.GetActiveExecutingAgentID();
|
|
458
|
+
if (contextAgentId) {
|
|
459
|
+
const executing = AIEngine.Instance.GetAgentByID(contextAgentId);
|
|
460
|
+
if (executing) {
|
|
461
|
+
if (executing.InstantiationMode !== 'None') {
|
|
462
|
+
return executing;
|
|
463
|
+
}
|
|
464
|
+
if (executing.BaseAgentID) {
|
|
465
|
+
const root = AIEngine.Instance.GetRootAgent(executing.ID);
|
|
466
|
+
if (root && root.InstantiationMode !== 'None') {
|
|
467
|
+
return root;
|
|
468
|
+
}
|
|
469
|
+
}
|
|
470
|
+
}
|
|
471
|
+
}
|
|
472
|
+
return undefined;
|
|
473
|
+
}
|
|
474
|
+
resolveOwnedDerivedInstance(params, contextUser) {
|
|
475
|
+
const explicitId = this.getStringParam(params, 'InstanceAgentID');
|
|
476
|
+
const contextAgentId = this.extractContext(params.Context).AgentID ?? AgentUserPolicyEngine.Instance.GetActiveExecutingAgentID();
|
|
477
|
+
const nameHint = this.getStringParam(params, 'Name')?.trim().toLowerCase() ?? null;
|
|
478
|
+
let candidate;
|
|
479
|
+
if (explicitId) {
|
|
480
|
+
candidate = AIEngine.Instance.GetAgentByID(explicitId);
|
|
481
|
+
}
|
|
482
|
+
else if (contextAgentId) {
|
|
483
|
+
const ctxAgent = AIEngine.Instance.GetAgentByID(contextAgentId);
|
|
484
|
+
if (ctxAgent?.BaseAgentID) {
|
|
485
|
+
candidate = ctxAgent;
|
|
486
|
+
}
|
|
487
|
+
}
|
|
488
|
+
if (!candidate && !explicitId && nameHint) {
|
|
489
|
+
candidate = AIEngine.Instance.Agents.find((a) => Boolean(a.BaseAgentID) &&
|
|
490
|
+
UUIDsEqual(a.OwnerUserID, contextUser.ID) &&
|
|
491
|
+
a.Name.trim().toLowerCase() === nameHint);
|
|
492
|
+
}
|
|
493
|
+
if (!candidate) {
|
|
494
|
+
return {
|
|
495
|
+
errorResult: {
|
|
496
|
+
Success: false,
|
|
497
|
+
ResultCode: 'AGENT_NOT_FOUND',
|
|
498
|
+
Message: 'Could not find the target personal agent instance. Provide InstanceAgentID.',
|
|
499
|
+
Params: params.Params,
|
|
500
|
+
},
|
|
501
|
+
};
|
|
502
|
+
}
|
|
503
|
+
if (!candidate.BaseAgentID) {
|
|
504
|
+
return {
|
|
505
|
+
errorResult: {
|
|
506
|
+
Success: false,
|
|
507
|
+
ResultCode: 'PERMISSION_DENIED',
|
|
508
|
+
Message: `"${candidate.Name}" is a root/template agent, not a personal derived instance.`,
|
|
509
|
+
Params: params.Params,
|
|
510
|
+
},
|
|
511
|
+
};
|
|
512
|
+
}
|
|
513
|
+
if (!UUIDsEqual(candidate.OwnerUserID, contextUser.ID) && contextUser.Type !== 'Owner') {
|
|
514
|
+
return {
|
|
515
|
+
errorResult: {
|
|
516
|
+
Success: false,
|
|
517
|
+
ResultCode: 'PERMISSION_DENIED',
|
|
518
|
+
Message: `You can only modify your own derived agent instances; "${candidate.Name}" belongs to another user.`,
|
|
519
|
+
Params: params.Params,
|
|
520
|
+
},
|
|
521
|
+
};
|
|
522
|
+
}
|
|
523
|
+
return { agent: candidate };
|
|
524
|
+
}
|
|
525
|
+
resolvePersonaEntity(personaId, personaName, fallbackAgentId) {
|
|
526
|
+
const personas = AIEngine.Instance.Personas ?? [];
|
|
527
|
+
if (personaId) {
|
|
528
|
+
const match = personas.find((p) => UUIDsEqual(p.ID, personaId));
|
|
529
|
+
if (match) {
|
|
530
|
+
return match;
|
|
531
|
+
}
|
|
532
|
+
}
|
|
533
|
+
if (personaName) {
|
|
534
|
+
const norm = personaName.trim().toLowerCase();
|
|
535
|
+
const match = personas.find((p) => p.Name.trim().toLowerCase() === norm && p.IsActive !== false);
|
|
536
|
+
if (match) {
|
|
537
|
+
return match;
|
|
538
|
+
}
|
|
539
|
+
}
|
|
540
|
+
const inherited = AIEngine.Instance.ResolveAgentPersona(fallbackAgentId);
|
|
541
|
+
if (inherited?.Persona) {
|
|
542
|
+
return inherited.Persona;
|
|
543
|
+
}
|
|
544
|
+
const sage = personas.find((p) => p.IsActive !== false && p.Name.trim().toLowerCase() === 'sage');
|
|
545
|
+
return sage ?? personas.find((p) => p.IsActive !== false) ?? null;
|
|
546
|
+
}
|
|
547
|
+
async upsertDefaultAgentPersona(agentId, personaId, tone, speakingStyle, contextUser, md) {
|
|
548
|
+
const rv = RunView.FromMetadataProvider(md);
|
|
549
|
+
const existingRes = await rv.RunView({
|
|
550
|
+
EntityName: 'MJ: AI Agent Personas',
|
|
551
|
+
ExtraFilter: `AgentID='${agentId}' AND IsDefault=1`,
|
|
552
|
+
MaxRows: 1,
|
|
553
|
+
ResultType: 'entity_object',
|
|
554
|
+
}, contextUser);
|
|
555
|
+
let link;
|
|
556
|
+
if (existingRes.Success && existingRes.Results.length > 0) {
|
|
557
|
+
link = existingRes.Results[0];
|
|
558
|
+
}
|
|
559
|
+
else {
|
|
560
|
+
link = await md.GetEntityObject('MJ: AI Agent Personas', contextUser);
|
|
561
|
+
link.NewRecord();
|
|
562
|
+
link.AgentID = agentId;
|
|
563
|
+
link.IsDefault = true;
|
|
564
|
+
link.IsAllowed = true;
|
|
565
|
+
link.Sequence = 10;
|
|
566
|
+
}
|
|
567
|
+
link.PersonaID = personaId;
|
|
568
|
+
const currentStyle = link.StyleOverrideObject
|
|
569
|
+
? { ...link.StyleOverrideObject }
|
|
570
|
+
: {};
|
|
571
|
+
if (tone !== null) {
|
|
572
|
+
currentStyle.Tone = tone;
|
|
573
|
+
}
|
|
574
|
+
if (speakingStyle !== null) {
|
|
575
|
+
currentStyle.SpeakingStyle = speakingStyle;
|
|
576
|
+
}
|
|
577
|
+
if (currentStyle.Tone || currentStyle.SpeakingStyle || currentStyle.Visual) {
|
|
578
|
+
link.StyleOverrideObject = currentStyle;
|
|
579
|
+
}
|
|
580
|
+
const saved = await link.Save();
|
|
581
|
+
if (!saved) {
|
|
582
|
+
return {
|
|
583
|
+
success: false,
|
|
584
|
+
errorMessage: `Failed to save agent persona configuration: ${link.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
585
|
+
};
|
|
586
|
+
}
|
|
587
|
+
return { success: true, errorMessage: '' };
|
|
588
|
+
}
|
|
589
|
+
async upsertUserPolicyForInstance(agentId, displayName, coordinationMode, preferredPersonaId, contextUser, md) {
|
|
590
|
+
const lookup = await AgentUserPolicyEngine.Instance.ResolvePolicyForAgentAndUser(agentId, contextUser.ID, contextUser, { forceRefresh: true, provider: md });
|
|
591
|
+
const policy = await md.GetEntityObject('MJ: AI Agent User Policies', contextUser);
|
|
592
|
+
if (lookup.status === 'found' && lookup.policy) {
|
|
593
|
+
const loaded = await policy.InnerLoad(CompositeKey.FromID(lookup.policy.ID));
|
|
594
|
+
if (!loaded) {
|
|
595
|
+
return { success: false, errorMessage: `Failed to load user policy for agent "${agentId}".` };
|
|
596
|
+
}
|
|
597
|
+
}
|
|
598
|
+
else {
|
|
599
|
+
policy.NewRecord();
|
|
600
|
+
policy.AgentID = agentId;
|
|
601
|
+
policy.UserID = contextUser.ID;
|
|
602
|
+
ApplyDefaultAgentUserPolicyValues(policy);
|
|
603
|
+
policy.OnboardingStatus = 'Completed';
|
|
604
|
+
policy.OnboardedAt = new Date();
|
|
605
|
+
}
|
|
606
|
+
policy.DisplayNameOverride = displayName;
|
|
607
|
+
if (preferredPersonaId) {
|
|
608
|
+
policy.PreferredPersonaID = preferredPersonaId;
|
|
609
|
+
}
|
|
610
|
+
if (coordinationMode) {
|
|
611
|
+
policy.CoordinationMode = coordinationMode;
|
|
612
|
+
}
|
|
613
|
+
const saved = await policy.Save();
|
|
614
|
+
if (!saved) {
|
|
615
|
+
return {
|
|
616
|
+
success: false,
|
|
617
|
+
errorMessage: `Failed to save agent user policy: ${policy.LatestResult?.CompleteMessage ?? 'Unknown error'}`,
|
|
618
|
+
};
|
|
619
|
+
}
|
|
620
|
+
AgentUserPolicyEngine.Instance.SetCachedPolicy(policy);
|
|
621
|
+
return { success: true, errorMessage: '' };
|
|
622
|
+
}
|
|
623
|
+
async repinConversationToInstance(conversationId, newAgentId, newAgentName, contextUser, md) {
|
|
624
|
+
try {
|
|
625
|
+
const conv = await md.GetEntityObject('MJ: Conversations', contextUser);
|
|
626
|
+
const loaded = await conv.InnerLoad(CompositeKey.FromID(conversationId));
|
|
627
|
+
if (!loaded) {
|
|
628
|
+
return false;
|
|
629
|
+
}
|
|
630
|
+
if (conv.UserID && !UUIDsEqual(conv.UserID, contextUser.ID) && contextUser.Type !== 'Owner') {
|
|
631
|
+
return false;
|
|
632
|
+
}
|
|
633
|
+
conv.DefaultAgentID = newAgentId;
|
|
634
|
+
const currentName = (conv.Name ?? '').trim().toLowerCase();
|
|
635
|
+
if (!currentName || currentName === 'new chat' || currentName === 'chief of staff') {
|
|
636
|
+
conv.Name = `Chat with ${newAgentName}`;
|
|
637
|
+
}
|
|
638
|
+
return await conv.Save();
|
|
639
|
+
}
|
|
640
|
+
catch (error) {
|
|
641
|
+
LogError(`ManageMyAgentInstanceAction: unable to re-pin conversation "${conversationId}": ${error instanceof Error ? error.message : String(error)}`);
|
|
642
|
+
return false;
|
|
643
|
+
}
|
|
644
|
+
}
|
|
645
|
+
buildInstanceSummary(agent, conversationPinned) {
|
|
646
|
+
const baseAgent = agent.BaseAgentID ? AIEngine.Instance.GetAgentByID(agent.BaseAgentID) : undefined;
|
|
647
|
+
const effectivePersona = AIEngine.Instance.ResolveAgentPersona(agent.ID);
|
|
648
|
+
return {
|
|
649
|
+
ID: agent.ID,
|
|
650
|
+
Name: agent.Name,
|
|
651
|
+
Description: agent.Description ?? null,
|
|
652
|
+
BaseAgentID: agent.BaseAgentID ?? null,
|
|
653
|
+
BaseAgentName: baseAgent?.Name ?? null,
|
|
654
|
+
IconClass: agent.IconClass ?? null,
|
|
655
|
+
LogoURL: agent.LogoURL ?? null,
|
|
656
|
+
IsPrimaryInstance: Boolean(agent.IsPrimaryInstance),
|
|
657
|
+
AdditionalInstructions: agent.AdditionalInstructions ?? null,
|
|
658
|
+
Status: agent.Status,
|
|
659
|
+
PersonaID: effectivePersona?.Persona?.ID ?? null,
|
|
660
|
+
PersonaName: effectivePersona?.Persona?.Name ?? null,
|
|
661
|
+
Tone: effectivePersona?.Tone ?? null,
|
|
662
|
+
SpeakingStyle: effectivePersona?.SpeakingStyle ?? null,
|
|
663
|
+
ConversationPinned: conversationPinned,
|
|
664
|
+
};
|
|
665
|
+
}
|
|
666
|
+
parseOperation(raw) {
|
|
667
|
+
if (!raw) {
|
|
668
|
+
return null;
|
|
669
|
+
}
|
|
670
|
+
const normalized = raw.trim().toLowerCase();
|
|
671
|
+
switch (normalized) {
|
|
672
|
+
case 'create':
|
|
673
|
+
return 'Create';
|
|
674
|
+
case 'update':
|
|
675
|
+
return 'Update';
|
|
676
|
+
case 'listmine':
|
|
677
|
+
case 'list_mine':
|
|
678
|
+
case 'list':
|
|
679
|
+
return 'ListMine';
|
|
680
|
+
case 'setprimary':
|
|
681
|
+
case 'set_primary':
|
|
682
|
+
return 'SetPrimary';
|
|
683
|
+
case 'archive':
|
|
684
|
+
case 'disable':
|
|
685
|
+
return 'Archive';
|
|
686
|
+
default:
|
|
687
|
+
return null;
|
|
688
|
+
}
|
|
689
|
+
}
|
|
690
|
+
parseCoordinationMode(raw) {
|
|
691
|
+
if (!raw) {
|
|
692
|
+
return null;
|
|
693
|
+
}
|
|
694
|
+
const normalized = raw.trim().toLowerCase();
|
|
695
|
+
if (normalized === 'observer')
|
|
696
|
+
return 'Observer';
|
|
697
|
+
if (normalized === 'coordinator')
|
|
698
|
+
return 'Coordinator';
|
|
699
|
+
if (normalized === 'autonomous')
|
|
700
|
+
return 'Autonomous';
|
|
701
|
+
return null;
|
|
702
|
+
}
|
|
703
|
+
extractContext(context) {
|
|
704
|
+
if (!context || typeof context !== 'object') {
|
|
705
|
+
return {};
|
|
706
|
+
}
|
|
707
|
+
const rec = context;
|
|
708
|
+
return {
|
|
709
|
+
AgentID: typeof rec.AgentID === 'string' && rec.AgentID.trim() ? rec.AgentID.trim() : undefined,
|
|
710
|
+
AgentName: typeof rec.AgentName === 'string' && rec.AgentName.trim() ? rec.AgentName.trim() : undefined,
|
|
711
|
+
ConversationID: typeof rec.ConversationID === 'string' && rec.ConversationID.trim()
|
|
712
|
+
? rec.ConversationID.trim()
|
|
713
|
+
: undefined,
|
|
714
|
+
};
|
|
715
|
+
}
|
|
716
|
+
getStringParam(params, name) {
|
|
717
|
+
const found = params.Params.find((p) => p.Name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
718
|
+
if (found?.Value == null) {
|
|
719
|
+
return null;
|
|
720
|
+
}
|
|
721
|
+
const s = String(found.Value);
|
|
722
|
+
return s.length > 0 ? s : null;
|
|
723
|
+
}
|
|
724
|
+
getBooleanParam(params, name) {
|
|
725
|
+
const found = params.Params.find((p) => p.Name.trim().toLowerCase() === name.trim().toLowerCase());
|
|
726
|
+
if (found?.Value == null) {
|
|
727
|
+
return null;
|
|
728
|
+
}
|
|
729
|
+
if (typeof found.Value === 'boolean') {
|
|
730
|
+
return found.Value;
|
|
731
|
+
}
|
|
732
|
+
const str = String(found.Value).trim().toLowerCase();
|
|
733
|
+
if (str === 'true' || str === '1' || str === 'yes')
|
|
734
|
+
return true;
|
|
735
|
+
if (str === 'false' || str === '0' || str === 'no')
|
|
736
|
+
return false;
|
|
737
|
+
return null;
|
|
738
|
+
}
|
|
739
|
+
setOutputParam(params, name, value) {
|
|
740
|
+
const existing = params.Params.find((p) => p.Name.trim().toLowerCase() === name.trim().toLowerCase() && (p.Type === 'Output' || p.Type === 'Both'));
|
|
741
|
+
if (existing) {
|
|
742
|
+
existing.Value = value;
|
|
743
|
+
return;
|
|
744
|
+
}
|
|
745
|
+
params.Params.push({ Name: name, Type: 'Output', Value: value });
|
|
746
|
+
}
|
|
747
|
+
};
|
|
748
|
+
ManageMyAgentInstanceAction = __decorate([
|
|
749
|
+
RegisterClass(BaseAction, 'Manage My Agent Instance')
|
|
750
|
+
], ManageMyAgentInstanceAction);
|
|
751
|
+
export { ManageMyAgentInstanceAction };
|
|
752
|
+
//# sourceMappingURL=manage-my-agent-instance.action.js.map
|