@memberjunction/aiengine
Server-side AI Engine for MemberJunction. Wraps AIEngineBase and adds server-only capabilities including LLM execution, embedding generation, vector-based semantic search for agents and actions, and conversation attachment management. This package is the main orchestration layer for AI operations on the server.
Architecture
Section titled “Architecture”graph TD
AIB["AIEngineBase<br/>Metadata Cache"]
style AIB fill:#2d6a9f,stroke:#1a4971,color:#fff
AIE["AIEngine<br/>Server-Side Singleton"]
style AIE fill:#2d8659,stroke:#1a5c3a,color:#fff
subgraph "Server Capabilities"
LLM["LLM Execution<br/>ChatCompletion, Classify, Summarize"]
style LLM fill:#7c5295,stroke:#563a6b,color:#fff
EMB["Embedding Services<br/>Agent & Action Embeddings"]
style EMB fill:#7c5295,stroke:#563a6b,color:#fff
VS["Vector Search<br/>Semantic Agent/Action/Note Matching"]
style VS fill:#b8762f,stroke:#8a5722,color:#fff
ATT["Attachment Service<br/>Conversation Media Management"]
style ATT fill:#b8762f,stroke:#8a5722,color:#fff
end
AIB --> AIE
AIE --> LLM
AIE --> EMB
AIE --> VS
AIE --> ATT
subgraph "Result Types"
AMR["AgentMatchResult"]
style AMR fill:#7c5295,stroke:#563a6b,color:#fff
ACMR["ActionMatchResult"]
style ACMR fill:#7c5295,stroke:#563a6b,color:#fff
NMR["NoteMatchResult"]
style NMR fill:#7c5295,stroke:#563a6b,color:#fff
EMR["ExampleMatchResult"]
style EMR fill:#7c5295,stroke:#563a6b,color:#fff
end
VS --> AMR
VS --> ACMR
VS --> NMR
VS --> EMR
Installation
Section titled “Installation”npm install @memberjunction/aiengineNote: This package is server-side only. For metadata access on the client, use @memberjunction/ai-engine-base directly.
Key Exports
Section titled “Key Exports”AIEngine (Singleton)
Section titled “AIEngine (Singleton)”The main server-side engine. Uses composition (not inheritance) to delegate metadata operations to AIEngineBase.Instance while adding server-specific features.
import { AIEngine } from '@memberjunction/aiengine';
// Initializeawait AIEngine.Instance.Config(false, contextUser);
// All AIEngineBase properties are delegated:const models = AIEngine.Instance.Models;const agents = AIEngine.Instance.Agents;LLM Execution
Section titled “LLM Execution”// Direct chat completionconst result = await AIEngine.Instance.ChatCompletion({ model: 'gpt-4', messages: [{ role: 'user', content: 'Explain quantum computing' }]});
// Summarize textconst summary = await AIEngine.Instance.SummarizeText({ model: 'gpt-4', text: longDocument});
// Classify textconst classification = await AIEngine.Instance.ClassifyText({ model: 'gpt-4', text: inputText, categories: ['positive', 'negative', 'neutral']});Semantic Search
Section titled “Semantic Search”Find agents, actions, notes, and examples using vector similarity:
// Find agents matching a user queryconst agentMatches: AgentMatchResult[] = await AIEngine.Instance.FindSimilarAgents( 'Help me analyze sales data', 5, // topK contextUser);
// Find relevant actionsconst actionMatches: ActionMatchResult[] = await AIEngine.Instance.FindSimilarActions( 'Send an email notification', 5, contextUser);
// Find relevant notes for an agentconst noteMatches: NoteMatchResult[] = await AIEngine.Instance.FindSimilarNotes( agentId, 'Customer wants a refund', 10, contextUser);
// Find relevant examples for an agentconst exampleMatches: ExampleMatchResult[] = await AIEngine.Instance.FindSimilarExamples( agentId, 'How do I reset my password?', 5, contextUser);Embedding Services
Section titled “Embedding Services”| Class | Purpose |
|---|---|
AgentEmbeddingService | Generates and manages embeddings for AI agents, enabling semantic agent discovery |
ActionEmbeddingService | Generates and manages embeddings for actions, enabling semantic action matching |
Match Result Types
Section titled “Match Result Types”| Type | Fields | Description |
|---|---|---|
AgentMatchResult | agent, score, metadata | Agent found via semantic similarity |
ActionMatchResult | action, score, metadata | Action found via semantic similarity |
NoteMatchResult | note, score, metadata | Agent note found via semantic similarity |
ExampleMatchResult | example, score, metadata | Agent example found via semantic similarity |
Vector Store Invariant Preservation
Section titled “Vector Store Invariant Preservation”AIEngine exposes FindSimilarAgentNotes over the in-process _noteVectorService. Since v5.30.x the vector store is kept strictly in sync with the persisted note state:
- Invariant.
_noteVectorServicecontains an entry for anAIAgentNoteif and only if its persistedStatus='Active'AND itsEmbeddingVectoris non-null. - Write-side enforcement.
MJAIAgentNoteEntityServer.Save()and.Delete()(in@memberjunction/core-entities-server) update the in-process vector store inline with each note write — adding entries when a note becomes Active with a non-null embedding, removing them when Status flips away from Active or when the note is deleted. - What this fixes. Before this change, revoking a note (e.g. during MemoryManagerAgent consolidation, or when a contradiction was resolved) would leave a stale entry in
_noteVectorServiceuntil MJAPI was restarted. Subsequent calls toFindSimilarAgentNoteswould surface revoked notes back to retrieval. The invariant now holds without a restart.
The relevant code paths live in src/AIEngine.ts and packages/MJCoreEntitiesServer/src/custom/MJAIAgentNoteEntityServer.server.ts.
ConversationAttachmentService
Section titled “ConversationAttachmentService”Manages media attachments (images, audio, video, files) in agent conversations:
import { ConversationAttachmentService } from '@memberjunction/aiengine';
const service = new ConversationAttachmentService();
// Process uploaded attachments for a conversationawait service.ProcessAttachments(conversationId, attachments, contextUser);Usage Pattern
Section titled “Usage Pattern”import { AIEngine } from '@memberjunction/aiengine';
// 1. Initialize at server startupawait AIEngine.Instance.Config(false, contextUser);
// 2. Access metadata (delegated to AIEngineBase)const model = AIEngine.Instance.Models.find(m => m.Name === 'GPT-4');const agent = AIEngine.Instance.GetAgentByName('Sales Assistant');
// 3. Use server-side capabilitiesconst similar = await AIEngine.Instance.FindSimilarAgents(userQuery, 5, contextUser);Dependencies
Section titled “Dependencies”@memberjunction/ai-engine-base— Base metadata cache (AIEngineBase)@memberjunction/ai— Core AI abstractions (BaseLLM, BaseEmbeddings)@memberjunction/ai-core-plus— Extended entity classes@memberjunction/ai-vectors-memory— In-memory vector service for semantic search@memberjunction/core— MJ framework core@memberjunction/core-entities— Generated entity classes@memberjunction/actions-base— Action framework integration@memberjunction/storage— File storage integration for attachments