@memberjunction/ai-engine-base 3.4.0 → 4.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/README.md +216 -0
- package/dist/AIAgentPermissionHelper.d.ts +45 -0
- package/dist/AIAgentPermissionHelper.d.ts.map +1 -1
- package/dist/AIAgentPermissionHelper.js +73 -20
- package/dist/AIAgentPermissionHelper.js.map +1 -1
- package/dist/AICredentialBindingEntityExtended.d.ts +33 -1
- package/dist/AICredentialBindingEntityExtended.d.ts.map +1 -1
- package/dist/AICredentialBindingEntityExtended.js +58 -22
- package/dist/AICredentialBindingEntityExtended.js.map +1 -1
- package/dist/BaseAIEngine.d.ts +310 -1
- package/dist/BaseAIEngine.d.ts.map +1 -1
- package/dist/BaseAIEngine.js +378 -41
- package/dist/BaseAIEngine.js.map +1 -1
- package/dist/PriceUnitTypes.d.ts +32 -1
- package/dist/PriceUnitTypes.d.ts.map +1 -1
- package/dist/PriceUnitTypes.js +40 -21
- package/dist/PriceUnitTypes.js.map +1 -1
- package/dist/index.d.ts +4 -5
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +4 -28
- package/dist/index.js.map +1 -1
- package/package.json +13 -12
package/README.md
ADDED
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
# @memberjunction/ai-engine-base
|
|
2
|
+
|
|
3
|
+
Base AI Engine package for MemberJunction. Provides a comprehensive metadata cache for all AI-related entities -- models, vendors, prompts, agents, configurations, modalities, permissions, and more. This package extends `BaseEngine` to load and cache AI metadata in a single batch, making it available on both server and client without duplicating data loading logic.
|
|
4
|
+
|
|
5
|
+
## Architecture
|
|
6
|
+
|
|
7
|
+
```mermaid
|
|
8
|
+
graph TD
|
|
9
|
+
subgraph Dependencies
|
|
10
|
+
CORE["@memberjunction/core<br/>BaseEngine"]
|
|
11
|
+
style CORE fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
12
|
+
|
|
13
|
+
CE["@memberjunction/core-entities"]
|
|
14
|
+
style CE fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
15
|
+
|
|
16
|
+
ACP["@memberjunction/ai-core-plus<br/>Extended Entities"]
|
|
17
|
+
style ACP fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
18
|
+
end
|
|
19
|
+
|
|
20
|
+
AIB["AIEngineBase<br/>Singleton Metadata Cache"]
|
|
21
|
+
style AIB fill:#2d8659,stroke:#1a5c3a,color:#fff
|
|
22
|
+
|
|
23
|
+
subgraph "Cached Metadata"
|
|
24
|
+
M["Models & Model Types"]
|
|
25
|
+
style M fill:#7c5295,stroke:#563a6b,color:#fff
|
|
26
|
+
|
|
27
|
+
V["Vendors & Model Vendors"]
|
|
28
|
+
style V fill:#7c5295,stroke:#563a6b,color:#fff
|
|
29
|
+
|
|
30
|
+
P["Prompts, Categories & Types"]
|
|
31
|
+
style P fill:#7c5295,stroke:#563a6b,color:#fff
|
|
32
|
+
|
|
33
|
+
A["Agents, Types & Relationships"]
|
|
34
|
+
style A fill:#7c5295,stroke:#563a6b,color:#fff
|
|
35
|
+
|
|
36
|
+
CFG["Configurations & Params"]
|
|
37
|
+
style CFG fill:#b8762f,stroke:#8a5722,color:#fff
|
|
38
|
+
|
|
39
|
+
MOD["Modalities & Limits"]
|
|
40
|
+
style MOD fill:#b8762f,stroke:#8a5722,color:#fff
|
|
41
|
+
|
|
42
|
+
PERM["Permissions & Credentials"]
|
|
43
|
+
style PERM fill:#b8762f,stroke:#8a5722,color:#fff
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
CORE --> AIB
|
|
47
|
+
CE --> AIB
|
|
48
|
+
ACP --> AIB
|
|
49
|
+
AIB --> M
|
|
50
|
+
AIB --> V
|
|
51
|
+
AIB --> P
|
|
52
|
+
AIB --> A
|
|
53
|
+
AIB --> CFG
|
|
54
|
+
AIB --> MOD
|
|
55
|
+
AIB --> PERM
|
|
56
|
+
|
|
57
|
+
SRV["AIEngine (Server)"]
|
|
58
|
+
style SRV fill:#2d6a9f,stroke:#1a4971,color:#fff
|
|
59
|
+
|
|
60
|
+
AIB --> SRV
|
|
61
|
+
```
|
|
62
|
+
|
|
63
|
+
## Installation
|
|
64
|
+
|
|
65
|
+
```bash
|
|
66
|
+
npm install @memberjunction/ai-engine-base
|
|
67
|
+
```
|
|
68
|
+
|
|
69
|
+
## Key Exports
|
|
70
|
+
|
|
71
|
+
### AIEngineBase (Singleton)
|
|
72
|
+
|
|
73
|
+
The core class that loads and caches all AI metadata. Access via `AIEngineBase.Instance`.
|
|
74
|
+
|
|
75
|
+
```typescript
|
|
76
|
+
import { AIEngineBase } from '@memberjunction/ai-engine-base';
|
|
77
|
+
|
|
78
|
+
// Initialize (typically at app startup)
|
|
79
|
+
await AIEngineBase.Instance.Config(false, contextUser);
|
|
80
|
+
|
|
81
|
+
// Access cached metadata
|
|
82
|
+
const models = AIEngineBase.Instance.Models;
|
|
83
|
+
const agents = AIEngineBase.Instance.Agents;
|
|
84
|
+
const prompts = AIEngineBase.Instance.Prompts;
|
|
85
|
+
```
|
|
86
|
+
|
|
87
|
+
#### Cached Entity Collections
|
|
88
|
+
|
|
89
|
+
| Property | Entity | Description |
|
|
90
|
+
|---|---|---|
|
|
91
|
+
| `Models` | AI Models | All registered AI models with extended properties |
|
|
92
|
+
| `ModelTypes` | AI Model Types | Model type categories (LLM, Embeddings, etc.) |
|
|
93
|
+
| `Vendors` | MJ: AI Vendors | AI service providers |
|
|
94
|
+
| `ModelVendors` | MJ: AI Model Vendors | Model-vendor associations |
|
|
95
|
+
| `Prompts` | AI Prompts | All prompt definitions with category associations |
|
|
96
|
+
| `PromptModels` | MJ: AI Prompt Models | Prompt-model associations |
|
|
97
|
+
| `PromptTypes` | AI Prompt Types | Prompt type categories |
|
|
98
|
+
| `PromptCategories` | AI Prompt Categories | Hierarchical prompt organization |
|
|
99
|
+
| `Agents` | AI Agents | All agent definitions with actions and notes |
|
|
100
|
+
| `AgentTypes` | MJ: AI Agent Types | Agent type definitions |
|
|
101
|
+
| `AgentActions` | AI Agent Actions | Actions associated with agents |
|
|
102
|
+
| `AgentPrompts` | MJ: AI Agent Prompts | Prompts associated with agents |
|
|
103
|
+
| `AgentSteps` | MJ: AI Agent Steps | Flow agent step definitions |
|
|
104
|
+
| `AgentStepPaths` | MJ: AI Agent Step Paths | Transitions between flow steps |
|
|
105
|
+
| `AgentRelationships` | MJ: AI Agent Relationships | Sub-agent relationships |
|
|
106
|
+
| `AgentPermissions` | MJ: AI Agent Permissions | User/role permission grants |
|
|
107
|
+
| `AgentConfigurations` | MJ: AI Agent Configurations | Semantic presets (Fast, High Quality) |
|
|
108
|
+
| `Configurations` | MJ: AI Configurations | Global AI configurations with inheritance |
|
|
109
|
+
| `ConfigurationParams` | MJ: AI Configuration Params | Key-value parameters for configurations |
|
|
110
|
+
| `ModelCosts` | MJ: AI Model Costs | Cost tracking per model/vendor |
|
|
111
|
+
| `ModelPriceTypes` | MJ: AI Model Price Types | Price type definitions |
|
|
112
|
+
| `ModelPriceUnitTypes` | MJ: AI Model Price Unit Types | Unit type definitions |
|
|
113
|
+
| `CredentialBindings` | MJ: AI Credential Bindings | Credential bindings for vendors/models |
|
|
114
|
+
| `Modalities` | MJ: AI Modalities | Input/output modality definitions |
|
|
115
|
+
| `AgentModalities` | MJ: AI Agent Modalities | Agent modality support |
|
|
116
|
+
| `ModelModalities` | MJ: AI Model Modalities | Model modality support |
|
|
117
|
+
| `VectorDatabases` | Vector Databases | Vector database configurations |
|
|
118
|
+
| `ArtifactTypes` | MJ: Artifact Types | Artifact type definitions |
|
|
119
|
+
|
|
120
|
+
#### Convenience Methods
|
|
121
|
+
|
|
122
|
+
```typescript
|
|
123
|
+
// Get the highest-power model of a given type
|
|
124
|
+
const bestLLM = await AIEngineBase.Instance.GetHighestPowerLLM('OpenAI', contextUser);
|
|
125
|
+
const bestEmbedding = await AIEngineBase.Instance.GetHighestPowerModel('OpenAI', 'Embeddings', contextUser);
|
|
126
|
+
|
|
127
|
+
// Get agent by name
|
|
128
|
+
const agent = AIEngineBase.Instance.GetAgentByName('Customer Support Agent');
|
|
129
|
+
|
|
130
|
+
// Get sub-agents (children + relationships)
|
|
131
|
+
const subAgents = AIEngineBase.Instance.GetSubAgents(agentId, 'Active');
|
|
132
|
+
|
|
133
|
+
// Configuration presets
|
|
134
|
+
const presets = AIEngineBase.Instance.GetAgentConfigurationPresets(agentId);
|
|
135
|
+
const defaultPreset = AIEngineBase.Instance.GetDefaultAgentConfigurationPreset(agentId);
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
#### Configuration Inheritance
|
|
139
|
+
|
|
140
|
+
Configurations support parent-child inheritance chains:
|
|
141
|
+
|
|
142
|
+
```typescript
|
|
143
|
+
// Get the full inheritance chain (child -> parent -> grandparent)
|
|
144
|
+
const chain = AIEngineBase.Instance.GetConfigurationChain(configId);
|
|
145
|
+
|
|
146
|
+
// Get parameters with inheritance (child overrides parent)
|
|
147
|
+
const params = AIEngineBase.Instance.GetConfigurationParamsWithInheritance(configId);
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
#### Model Cost Tracking
|
|
151
|
+
|
|
152
|
+
```typescript
|
|
153
|
+
// Get active cost for a model/vendor combination
|
|
154
|
+
const cost = AIEngineBase.Instance.GetActiveModelCost(modelId, vendorId, 'Realtime');
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
#### Credential Bindings
|
|
158
|
+
|
|
159
|
+
```typescript
|
|
160
|
+
// Get credential bindings for a vendor
|
|
161
|
+
const bindings = AIEngineBase.Instance.GetCredentialBindingsForTarget('Vendor', vendorId);
|
|
162
|
+
|
|
163
|
+
// Check if bindings exist
|
|
164
|
+
const hasBindings = AIEngineBase.Instance.HasCredentialBindings('ModelVendor', modelVendorId);
|
|
165
|
+
```
|
|
166
|
+
|
|
167
|
+
### Modality System
|
|
168
|
+
|
|
169
|
+
The modality system tracks which input/output types (Text, Image, Audio, Video, File) agents and models support.
|
|
170
|
+
|
|
171
|
+
```typescript
|
|
172
|
+
// Check if an agent supports image input
|
|
173
|
+
const supportsImages = AIEngineBase.Instance.AgentSupportsModality(agentId, 'Image', 'Input');
|
|
174
|
+
|
|
175
|
+
// Check if agent accepts any attachments
|
|
176
|
+
const supportsAttachments = AIEngineBase.Instance.AgentSupportsAttachments(agentId);
|
|
177
|
+
|
|
178
|
+
// Get aggregated attachment limits for UI configuration
|
|
179
|
+
const limits = AIEngineBase.Instance.GetAgentAttachmentLimits(agentId, modelId);
|
|
180
|
+
// limits.enabled, limits.maxAttachments, limits.maxAttachmentSizeBytes, limits.acceptedFileTypes
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
### Agent Permission Helper
|
|
184
|
+
|
|
185
|
+
```typescript
|
|
186
|
+
// Check individual permissions
|
|
187
|
+
const canView = await AIEngineBase.Instance.CanUserViewAgent(agentId, user);
|
|
188
|
+
const canRun = await AIEngineBase.Instance.CanUserRunAgent(agentId, user);
|
|
189
|
+
|
|
190
|
+
// Get all permissions at once
|
|
191
|
+
const perms = await AIEngineBase.Instance.GetUserAgentPermissions(agentId, user);
|
|
192
|
+
// perms.canView, perms.canRun, perms.canEdit, perms.canDelete, perms.isOwner
|
|
193
|
+
|
|
194
|
+
// Get all accessible agents for a user
|
|
195
|
+
const agents = await AIEngineBase.Instance.GetAccessibleAgents(user, 'run');
|
|
196
|
+
```
|
|
197
|
+
|
|
198
|
+
### Additional Exports
|
|
199
|
+
|
|
200
|
+
| Export | Purpose |
|
|
201
|
+
|---|---|
|
|
202
|
+
| `ModalityLimits` | Resolved limits for a specific modality (size, count, dimension, formats) |
|
|
203
|
+
| `AgentAttachmentLimits` | Aggregated attachment limits for UI components |
|
|
204
|
+
| `PriceUnitTypes` | Price unit type utilities |
|
|
205
|
+
| `AIAgentPermissionHelper` | Static helper for agent permission checks |
|
|
206
|
+
| `EffectiveAgentPermissions` | Complete permission set for a user/agent combination |
|
|
207
|
+
| `AICredentialBindingEntityExtended` | Extended credential binding entity |
|
|
208
|
+
|
|
209
|
+
## Dependencies
|
|
210
|
+
|
|
211
|
+
- `@memberjunction/core` -- BaseEngine, Metadata, RunView
|
|
212
|
+
- `@memberjunction/core-entities` -- Generated entity classes
|
|
213
|
+
- `@memberjunction/ai` -- Core AI abstractions
|
|
214
|
+
- `@memberjunction/ai-core-plus` -- Extended entity classes
|
|
215
|
+
- `@memberjunction/global` -- Class factory
|
|
216
|
+
- `@memberjunction/templates-base-types` -- Template engine integration
|
|
@@ -1,5 +1,8 @@
|
|
|
1
1
|
import { UserInfo } from "@memberjunction/core";
|
|
2
2
|
import { AIAgentEntity } from "@memberjunction/core-entities";
|
|
3
|
+
/**
|
|
4
|
+
* Effective permissions for a user on a specific AI agent
|
|
5
|
+
*/
|
|
3
6
|
export interface EffectiveAgentPermissions {
|
|
4
7
|
canView: boolean;
|
|
5
8
|
canRun: boolean;
|
|
@@ -7,11 +10,53 @@ export interface EffectiveAgentPermissions {
|
|
|
7
10
|
canDelete: boolean;
|
|
8
11
|
isOwner: boolean;
|
|
9
12
|
}
|
|
13
|
+
/**
|
|
14
|
+
* Helper class for managing AI Agent permissions.
|
|
15
|
+
* Provides methods to check user permissions, get accessible agents, and uses AIEngineBase cached metadata.
|
|
16
|
+
*/
|
|
10
17
|
export declare class AIAgentPermissionHelper {
|
|
18
|
+
/**
|
|
19
|
+
* Checks if a user has a specific permission for an agent.
|
|
20
|
+
* @param agentId - The ID of the agent to check
|
|
21
|
+
* @param user - The user to check permissions for
|
|
22
|
+
* @param permission - The permission level to check ('view', 'run', 'edit', or 'delete')
|
|
23
|
+
* @returns True if the user has the specified permission
|
|
24
|
+
*/
|
|
11
25
|
static HasPermission(agentId: string, user: UserInfo, permission: 'view' | 'run' | 'edit' | 'delete'): Promise<boolean>;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the aggregate permissions for a user on a specific agent.
|
|
28
|
+
* Implements hierarchical permission logic:
|
|
29
|
+
* - Delete implies Edit, Run, and View
|
|
30
|
+
* - Edit implies Run and View
|
|
31
|
+
* - Run implies View
|
|
32
|
+
*
|
|
33
|
+
* Default behavior when no permission records exist:
|
|
34
|
+
* - Anyone can View and Run (open by default)
|
|
35
|
+
* - Only owner can Edit and Delete
|
|
36
|
+
*
|
|
37
|
+
* Checks ownership first, then combines all matching user and role permissions.
|
|
38
|
+
* @param agentId - The ID of the agent
|
|
39
|
+
* @param user - The user to check permissions for
|
|
40
|
+
* @returns Object containing all effective permissions and ownership status
|
|
41
|
+
*/
|
|
12
42
|
static GetEffectivePermissions(agentId: string, user: UserInfo): Promise<EffectiveAgentPermissions>;
|
|
43
|
+
/**
|
|
44
|
+
* Gets all agents that a user has access to with a specific permission level.
|
|
45
|
+
* Uses cached metadata from AIEngineBase instead of querying database.
|
|
46
|
+
* @param user - The user to check permissions for
|
|
47
|
+
* @param permission - The minimum permission level required ('view', 'run', 'edit', or 'delete')
|
|
48
|
+
* @returns Array of agents the user can access with the specified permission
|
|
49
|
+
*/
|
|
13
50
|
static GetAccessibleAgents(user: UserInfo, permission: 'view' | 'run' | 'edit' | 'delete'): Promise<AIAgentEntity[]>;
|
|
51
|
+
/**
|
|
52
|
+
* Clears the AIEngineBase cache. Useful when permissions have been modified.
|
|
53
|
+
* This forces a reload of all cached metadata including permissions.
|
|
54
|
+
*/
|
|
14
55
|
static ClearCache(): void;
|
|
56
|
+
/**
|
|
57
|
+
* Refreshes the AIEngineBase cache to pick up permission changes.
|
|
58
|
+
* @param user - The user context for server-side operations
|
|
59
|
+
*/
|
|
15
60
|
static RefreshCache(user: UserInfo): Promise<void>;
|
|
16
61
|
}
|
|
17
62
|
//# sourceMappingURL=AIAgentPermissionHelper.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIAgentPermissionHelper.d.ts","sourceRoot":"","sources":["../src/AIAgentPermissionHelper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"AIAgentPermissionHelper.d.ts","sourceRoot":"","sources":["../src/AIAgentPermissionHelper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAY,QAAQ,EAAE,MAAM,sBAAsB,CAAC;AAC1D,OAAO,EAAE,aAAa,EAAE,MAAM,+BAA+B,CAAC;AAG9D;;GAEG;AACH,MAAM,WAAW,yBAAyB;IACtC,OAAO,EAAE,OAAO,CAAC;IACjB,MAAM,EAAE,OAAO,CAAC;IAChB,OAAO,EAAE,OAAO,CAAC;IACjB,SAAS,EAAE,OAAO,CAAC;IACnB,OAAO,EAAE,OAAO,CAAC;CACpB;AAED;;;GAGG;AACH,qBAAa,uBAAuB;IAChC;;;;;;OAMG;WACiB,aAAa,CAC7B,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,QAAQ,EACd,UAAU,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,GAC/C,OAAO,CAAC,OAAO,CAAC;IAiBnB;;;;;;;;;;;;;;;OAeG;WACiB,uBAAuB,CACvC,OAAO,EAAE,MAAM,EACf,IAAI,EAAE,QAAQ,GACf,OAAO,CAAC,yBAAyB,CAAC;IAiFrC;;;;;;OAMG;WACiB,mBAAmB,CACnC,IAAI,EAAE,QAAQ,EACd,UAAU,EAAE,MAAM,GAAG,KAAK,GAAG,MAAM,GAAG,QAAQ,GAC/C,OAAO,CAAC,aAAa,EAAE,CAAC;IAsB3B;;;OAGG;WACW,UAAU,IAAI,IAAI;IAOhC;;;OAGG;WACiB,YAAY,CAAC,IAAI,EAAE,QAAQ,GAAG,OAAO,CAAC,IAAI,CAAC;CAOlE"}
|
|
@@ -1,9 +1,17 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
|
|
4
|
-
|
|
5
|
-
|
|
6
|
-
|
|
1
|
+
import { LogError } from "@memberjunction/core";
|
|
2
|
+
import { AIEngineBase } from "./BaseAIEngine.js";
|
|
3
|
+
/**
|
|
4
|
+
* Helper class for managing AI Agent permissions.
|
|
5
|
+
* Provides methods to check user permissions, get accessible agents, and uses AIEngineBase cached metadata.
|
|
6
|
+
*/
|
|
7
|
+
export class AIAgentPermissionHelper {
|
|
8
|
+
/**
|
|
9
|
+
* Checks if a user has a specific permission for an agent.
|
|
10
|
+
* @param agentId - The ID of the agent to check
|
|
11
|
+
* @param user - The user to check permissions for
|
|
12
|
+
* @param permission - The permission level to check ('view', 'run', 'edit', or 'delete')
|
|
13
|
+
* @returns True if the user has the specified permission
|
|
14
|
+
*/
|
|
7
15
|
static async HasPermission(agentId, user, permission) {
|
|
8
16
|
const effective = await this.GetEffectivePermissions(agentId, user);
|
|
9
17
|
switch (permission) {
|
|
@@ -19,13 +27,32 @@ class AIAgentPermissionHelper {
|
|
|
19
27
|
return false;
|
|
20
28
|
}
|
|
21
29
|
}
|
|
30
|
+
/**
|
|
31
|
+
* Gets the aggregate permissions for a user on a specific agent.
|
|
32
|
+
* Implements hierarchical permission logic:
|
|
33
|
+
* - Delete implies Edit, Run, and View
|
|
34
|
+
* - Edit implies Run and View
|
|
35
|
+
* - Run implies View
|
|
36
|
+
*
|
|
37
|
+
* Default behavior when no permission records exist:
|
|
38
|
+
* - Anyone can View and Run (open by default)
|
|
39
|
+
* - Only owner can Edit and Delete
|
|
40
|
+
*
|
|
41
|
+
* Checks ownership first, then combines all matching user and role permissions.
|
|
42
|
+
* @param agentId - The ID of the agent
|
|
43
|
+
* @param user - The user to check permissions for
|
|
44
|
+
* @returns Object containing all effective permissions and ownership status
|
|
45
|
+
*/
|
|
22
46
|
static async GetEffectivePermissions(agentId, user) {
|
|
23
47
|
try {
|
|
24
|
-
|
|
25
|
-
|
|
48
|
+
// Ensure AIEngineBase is loaded with cached metadata
|
|
49
|
+
await AIEngineBase.Instance.Config(false, user);
|
|
50
|
+
// Find agent from cached metadata
|
|
51
|
+
const agent = AIEngineBase.Instance.Agents.find(a => a.ID === agentId);
|
|
26
52
|
if (!agent) {
|
|
27
53
|
throw new Error(`Agent ${agentId} not found in cached metadata`);
|
|
28
54
|
}
|
|
55
|
+
// Check if user is owner - owners have all permissions
|
|
29
56
|
const isOwner = agent.OwnerUserID === user.ID;
|
|
30
57
|
if (isOwner) {
|
|
31
58
|
return {
|
|
@@ -36,22 +63,30 @@ class AIAgentPermissionHelper {
|
|
|
36
63
|
isOwner: true
|
|
37
64
|
};
|
|
38
65
|
}
|
|
39
|
-
|
|
66
|
+
// Get permissions from cached metadata
|
|
67
|
+
const agentPermissions = AIEngineBase.Instance.AgentPermissions.filter(p => p.AgentID === agentId);
|
|
68
|
+
// DEFAULT BEHAVIOR: If no permission records exist, grant View and Run to everyone
|
|
69
|
+
// This minimizes administrative overhead while protecting Edit/Delete through ownership
|
|
40
70
|
if (agentPermissions.length === 0) {
|
|
41
71
|
return {
|
|
42
72
|
canView: true,
|
|
43
73
|
canRun: true,
|
|
44
|
-
canEdit: false,
|
|
45
|
-
canDelete: false,
|
|
74
|
+
canEdit: false, // Only owner can edit
|
|
75
|
+
canDelete: false, // Only owner can delete
|
|
46
76
|
isOwner: false
|
|
47
77
|
};
|
|
48
78
|
}
|
|
79
|
+
// Get user's role IDs
|
|
49
80
|
const userRoleIds = user.UserRoles?.map(ur => ur.RoleID) || [];
|
|
81
|
+
// Find all matching permissions (direct user permissions or role permissions)
|
|
50
82
|
const matchingPermissions = agentPermissions.filter(p => p.UserID === user.ID || (p.RoleID && userRoleIds.includes(p.RoleID)));
|
|
83
|
+
// Aggregate base permissions - any matching permission grants access (OR logic)
|
|
51
84
|
const hasDelete = matchingPermissions.some(p => p.CanDelete === true);
|
|
52
85
|
const hasEdit = matchingPermissions.some(p => p.CanEdit === true);
|
|
53
86
|
const hasRun = matchingPermissions.some(p => p.CanRun === true);
|
|
54
87
|
const hasView = matchingPermissions.some(p => p.CanView === true);
|
|
88
|
+
// Apply hierarchical logic:
|
|
89
|
+
// Delete → Edit → Run → View
|
|
55
90
|
const canDelete = hasDelete;
|
|
56
91
|
const canEdit = hasDelete || hasEdit;
|
|
57
92
|
const canRun = hasDelete || hasEdit || hasRun;
|
|
@@ -65,7 +100,8 @@ class AIAgentPermissionHelper {
|
|
|
65
100
|
};
|
|
66
101
|
}
|
|
67
102
|
catch (error) {
|
|
68
|
-
|
|
103
|
+
LogError(error, 'Error getting effective permissions for agent');
|
|
104
|
+
// Fail closed - deny all permissions on error
|
|
69
105
|
return {
|
|
70
106
|
canView: false,
|
|
71
107
|
canRun: false,
|
|
@@ -75,11 +111,20 @@ class AIAgentPermissionHelper {
|
|
|
75
111
|
};
|
|
76
112
|
}
|
|
77
113
|
}
|
|
114
|
+
/**
|
|
115
|
+
* Gets all agents that a user has access to with a specific permission level.
|
|
116
|
+
* Uses cached metadata from AIEngineBase instead of querying database.
|
|
117
|
+
* @param user - The user to check permissions for
|
|
118
|
+
* @param permission - The minimum permission level required ('view', 'run', 'edit', or 'delete')
|
|
119
|
+
* @returns Array of agents the user can access with the specified permission
|
|
120
|
+
*/
|
|
78
121
|
static async GetAccessibleAgents(user, permission) {
|
|
79
122
|
try {
|
|
80
|
-
|
|
123
|
+
// Ensure AIEngineBase is loaded with cached metadata
|
|
124
|
+
await AIEngineBase.Instance.Config(false, user);
|
|
125
|
+
// Filter agents from cached metadata based on permissions
|
|
81
126
|
const accessibleAgents = [];
|
|
82
|
-
for (const agent of
|
|
127
|
+
for (const agent of AIEngineBase.Instance.Agents) {
|
|
83
128
|
const hasAccess = await this.HasPermission(agent.ID, user, permission);
|
|
84
129
|
if (hasAccess) {
|
|
85
130
|
accessibleAgents.push(agent);
|
|
@@ -88,23 +133,31 @@ class AIAgentPermissionHelper {
|
|
|
88
133
|
return accessibleAgents;
|
|
89
134
|
}
|
|
90
135
|
catch (error) {
|
|
91
|
-
|
|
136
|
+
LogError(error, 'Error getting accessible agents');
|
|
92
137
|
return [];
|
|
93
138
|
}
|
|
94
139
|
}
|
|
140
|
+
/**
|
|
141
|
+
* Clears the AIEngineBase cache. Useful when permissions have been modified.
|
|
142
|
+
* This forces a reload of all cached metadata including permissions.
|
|
143
|
+
*/
|
|
95
144
|
static ClearCache() {
|
|
96
|
-
|
|
97
|
-
|
|
145
|
+
// Force reload by calling Config with forceRefresh=true
|
|
146
|
+
AIEngineBase.Instance.Config(true).catch(error => {
|
|
147
|
+
LogError(error, 'Error clearing AIEngineBase cache');
|
|
98
148
|
});
|
|
99
149
|
}
|
|
150
|
+
/**
|
|
151
|
+
* Refreshes the AIEngineBase cache to pick up permission changes.
|
|
152
|
+
* @param user - The user context for server-side operations
|
|
153
|
+
*/
|
|
100
154
|
static async RefreshCache(user) {
|
|
101
155
|
try {
|
|
102
|
-
await
|
|
156
|
+
await AIEngineBase.Instance.Config(true, user);
|
|
103
157
|
}
|
|
104
158
|
catch (error) {
|
|
105
|
-
|
|
159
|
+
LogError(error, 'Error refreshing AIEngineBase cache');
|
|
106
160
|
}
|
|
107
161
|
}
|
|
108
162
|
}
|
|
109
|
-
exports.AIAgentPermissionHelper = AIAgentPermissionHelper;
|
|
110
163
|
//# sourceMappingURL=AIAgentPermissionHelper.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIAgentPermissionHelper.js","sourceRoot":"","sources":["../src/AIAgentPermissionHelper.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AIAgentPermissionHelper.js","sourceRoot":"","sources":["../src/AIAgentPermissionHelper.ts"],"names":[],"mappings":"AAAA,OAAO,EAAE,QAAQ,EAAY,MAAM,sBAAsB,CAAC;AAE1D,OAAO,EAAE,YAAY,EAAE,MAAM,gBAAgB,CAAC;AAa9C;;;GAGG;AACH,MAAM,OAAO,uBAAuB;IAChC;;;;;;OAMG;IACI,MAAM,CAAC,KAAK,CAAC,aAAa,CAC7B,OAAe,EACf,IAAc,EACd,UAA8C;QAE9C,MAAM,SAAS,GAAG,MAAM,IAAI,CAAC,uBAAuB,CAAC,OAAO,EAAE,IAAI,CAAC,CAAC;QAEpE,QAAQ,UAAU,EAAE,CAAC;YACjB,KAAK,MAAM;gBACP,OAAO,SAAS,C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I,CAAC,CAAC,KAAK,CAAC,KAAK,CAAC,EAAE;YAC7C,QAAQ,CAAC,KAAK,EAAE,mCAAmC,CAAC,CAAC;QACzD,CAAC,CAAC,CAAC;IACP,CAAC;IAED;;;OAGG;IACI,MAAM,CAAC,KAAK,CAAC,YAAY,CAAC,IAAc;QAC3C,IAAI,CAAC;YACD,MAAM,YAAY,CAAC,QAAQ,CAAC,MAAM,CAAC,IAAI,EAAE,IAAI,CAAC,CAAC;QACnD,CAAC;QAAC,OAAO,KAAK,EAAE,CAAC;YACb,QAAQ,CAAC,KAAK,EAAE,qCAAqC,CAAC,CAAC;QAC3D,CAAC;IACL,CAAC;CACJ"}
|
|
@@ -1,13 +1,45 @@
|
|
|
1
1
|
import { ValidationResult } from '@memberjunction/core';
|
|
2
2
|
import { AICredentialBindingEntity } from '@memberjunction/core-entities';
|
|
3
|
+
/**
|
|
4
|
+
* Extended AICredentialBindingEntity class that validates credential types match vendor expectations.
|
|
5
|
+
* When a credential is bound, this validates that the credential's CredentialTypeID matches the
|
|
6
|
+
* CredentialTypeID expected by the vendor (as declared on the AIVendor record).
|
|
7
|
+
*/
|
|
3
8
|
export declare class AICredentialBindingEntityExtended extends AICredentialBindingEntity {
|
|
9
|
+
/**
|
|
10
|
+
* Async validation that checks credential type compatibility.
|
|
11
|
+
* Uses AIEngineBase.Instance for cached AI entity data and RunView for credentials.
|
|
12
|
+
*/
|
|
4
13
|
ValidateAsync(): Promise<ValidationResult>;
|
|
14
|
+
/**
|
|
15
|
+
* Turn on async validation
|
|
16
|
+
*/
|
|
5
17
|
get DefaultSkipAsyncValidation(): boolean;
|
|
18
|
+
/**
|
|
19
|
+
* Validates that the bound credential's type matches the vendor's expected credential type.
|
|
20
|
+
* The vendor is determined based on the binding type:
|
|
21
|
+
* - Vendor binding: directly references the vendor
|
|
22
|
+
* - ModelVendor binding: gets vendor from the ModelVendor record
|
|
23
|
+
* - PromptModel binding: gets vendor from the PromptModel -> ModelVendor -> Vendor chain
|
|
24
|
+
*/
|
|
6
25
|
private validateCredentialTypeMatch;
|
|
26
|
+
/**
|
|
27
|
+
* Gets the expected CredentialTypeID from the vendor based on the binding type.
|
|
28
|
+
* Uses AIEngineBase cached data for lookups.
|
|
29
|
+
*/
|
|
7
30
|
private getExpectedCredentialTypeId;
|
|
31
|
+
/**
|
|
32
|
+
* Gets the expected credential type name for error messages.
|
|
33
|
+
*/
|
|
8
34
|
private getExpectedCredentialTypeName;
|
|
35
|
+
/**
|
|
36
|
+
* Gets the vendor name for error messages based on the binding type.
|
|
37
|
+
*/
|
|
9
38
|
private getVendorNameForError;
|
|
39
|
+
/**
|
|
40
|
+
* Loads the credential using RunView.
|
|
41
|
+
* Credentials are not cached in AIEngineBase, so we need to query them.
|
|
42
|
+
*/
|
|
10
43
|
private loadCredential;
|
|
11
44
|
}
|
|
12
|
-
export declare function LoadAICredentialBindingEntityExtended(): void;
|
|
13
45
|
//# sourceMappingURL=AICredentialBindingEntityExtended.d.ts.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AICredentialBindingEntityExtended.d.ts","sourceRoot":"","sources":["../src/AICredentialBindingEntityExtended.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,gBAAgB,EAA4C,MAAM,sBAAsB,CAAC;AACvH,OAAO,EAAE,yBAAyB,EAAoB,MAAM,+BAA+B,CAAC;
|
|
1
|
+
{"version":3,"file":"AICredentialBindingEntityExtended.d.ts","sourceRoot":"","sources":["../src/AICredentialBindingEntityExtended.ts"],"names":[],"mappings":"AACA,OAAO,EAAuB,gBAAgB,EAA4C,MAAM,sBAAsB,CAAC;AACvH,OAAO,EAAE,yBAAyB,EAAoB,MAAM,+BAA+B,CAAC;AAG5F;;;;GAIG;AACH,qBACa,iCAAkC,SAAQ,yBAAyB;IAE5E;;;OAGG;IACmB,aAAa,IAAI,OAAO,CAAC,gBAAgB,CAAC;IAYhE;;OAEG;IACH,IAAa,0BAA0B,IAAI,OAAO,CAEjD;IAED;;;;;;OAMG;YACW,2BAA2B;IAmCzC;;;OAGG;IACH,OAAO,CAAC,2BAA2B;IAgCnC;;OAEG;IACH,OAAO,CAAC,6BAA6B;IA6BrC;;OAEG;IACH,OAAO,CAAC,qBAAqB;IA6B7B;;;OAGG;YACW,cAAc;CAkB/B"}
|