@memberjunction/ai-prompts 2.48.0 → 2.49.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 +369 -24
- package/dist/AIPromptCategoryExtended.d.ts.map +1 -1
- package/dist/AIPromptCategoryExtended.js +2 -2
- package/dist/AIPromptCategoryExtended.js.map +1 -1
- package/dist/AIPromptRunner.d.ts +4 -94
- package/dist/AIPromptRunner.d.ts.map +1 -1
- package/dist/AIPromptRunner.js +175 -143
- package/dist/AIPromptRunner.js.map +1 -1
- package/dist/SystemPlaceholders.d.ts +18 -0
- package/dist/SystemPlaceholders.d.ts.map +1 -0
- package/dist/SystemPlaceholders.js +239 -0
- package/dist/SystemPlaceholders.js.map +1 -0
- package/dist/index.d.ts +2 -0
- package/dist/index.d.ts.map +1 -1
- package/dist/index.js +2 -0
- package/dist/index.js.map +1 -1
- package/dist/types.d.ts +101 -0
- package/dist/types.d.ts.map +1 -0
- package/dist/types.js +14 -0
- package/dist/types.js.map +1 -0
- package/package.json +10 -7
package/README.md
CHANGED
|
@@ -1,26 +1,206 @@
|
|
|
1
1
|
# @memberjunction/ai-prompts
|
|
2
2
|
|
|
3
|
-
|
|
3
|
+
Advanced AI prompt execution engine with hierarchical template composition, intelligent model selection, parallel execution, output validation, and comprehensive execution tracking.
|
|
4
4
|
|
|
5
5
|
[](https://www.npmjs.com/package/@memberjunction/ai-prompts)
|
|
6
6
|
[](https://opensource.org/licenses/ISC)
|
|
7
7
|
|
|
8
|
-
## Features
|
|
9
|
-
|
|
10
|
-
|
|
11
|
-
|
|
12
|
-
|
|
13
|
-
|
|
14
|
-
|
|
15
|
-
|
|
16
|
-
|
|
17
|
-
|
|
18
|
-
|
|
19
|
-
|
|
20
|
-
|
|
21
|
-
|
|
22
|
-
|
|
23
|
-
|
|
8
|
+
## Key Features
|
|
9
|
+
|
|
10
|
+
### 🎯 Dynamic Hierarchical Template Composition
|
|
11
|
+
|
|
12
|
+
#### Why Dynamic Template Composition?
|
|
13
|
+
|
|
14
|
+
While MemberJunction's template system already supports static template composition (where Template A always includes Templates B and C), the AI Prompts system adds **dynamic template composition** - the ability to inject ANY prompt template into ANY other prompt template at runtime.
|
|
15
|
+
|
|
16
|
+
**Static Composition (MJ Templates):** Perfect for fixed relationships like email headers/footers
|
|
17
|
+
```liquid
|
|
18
|
+
<!-- Email template always includes same header -->
|
|
19
|
+
{% include 'email-header' %}
|
|
20
|
+
{{ content }}
|
|
21
|
+
{% include 'email-footer' %}
|
|
22
|
+
```
|
|
23
|
+
|
|
24
|
+
**Dynamic Composition (AI Prompts):** Essential for flexible runtime relationships
|
|
25
|
+
```typescript
|
|
26
|
+
// Inject ANY child prompt into ANY parent prompt at runtime
|
|
27
|
+
const params = new AIPromptParams();
|
|
28
|
+
params.prompt = systemPrompt; // e.g., Agent Type's control flow prompt
|
|
29
|
+
params.childPrompts = [
|
|
30
|
+
new ChildPromptParam(agentPrompt, 'agentInstructions') // Specific agent's prompt
|
|
31
|
+
];
|
|
32
|
+
// System prompt can use {{ agentInstructions }} to embed the agent's specific logic
|
|
33
|
+
```
|
|
34
|
+
|
|
35
|
+
#### The Agent System Use Case
|
|
36
|
+
|
|
37
|
+
This dynamic composition is crucial for AI Agents:
|
|
38
|
+
- **Agent Types** have **System Prompts** that control execution flow and response format
|
|
39
|
+
- **Individual Agents** have their own **specific prompts** with domain logic
|
|
40
|
+
- At runtime, any agent's prompt is dynamically injected into its type's system prompt
|
|
41
|
+
- This creates a complete prompt combining the control wrapper with agent-specific instructions
|
|
42
|
+
|
|
43
|
+
```typescript
|
|
44
|
+
// Agent Type System Prompt (controls flow)
|
|
45
|
+
const systemPrompt = {
|
|
46
|
+
templateText: `You are an AI agent. Follow these instructions:
|
|
47
|
+
|
|
48
|
+
{{ agentInstructions }} <!-- Dynamically injected at runtime -->
|
|
49
|
+
|
|
50
|
+
Respond in JSON format with: { decision: ..., reasoning: ... }`
|
|
51
|
+
};
|
|
52
|
+
|
|
53
|
+
// Individual Agent Prompt (domain logic)
|
|
54
|
+
const dataGatherAgent = {
|
|
55
|
+
templateText: `Your role is to gather data from: {{ dataSources }}`
|
|
56
|
+
};
|
|
57
|
+
|
|
58
|
+
// At runtime, compose them dynamically
|
|
59
|
+
params.childPrompts = [
|
|
60
|
+
new ChildPromptParam(dataGatherAgent, 'agentInstructions')
|
|
61
|
+
];
|
|
62
|
+
```
|
|
63
|
+
|
|
64
|
+
### 🔄 System Placeholders
|
|
65
|
+
Automatically inject common values into all templates without manual data passing. Includes date/time, user context, prompt metadata, and more.
|
|
66
|
+
|
|
67
|
+
```liquid
|
|
68
|
+
Current user: {{ _USER_NAME }}
|
|
69
|
+
Date: {{ _CURRENT_DATE }}
|
|
70
|
+
Expected output: {{ _OUTPUT_EXAMPLE }}
|
|
71
|
+
```
|
|
72
|
+
|
|
73
|
+
## System Placeholders Reference
|
|
74
|
+
|
|
75
|
+
System placeholders are automatically available in all AI prompt templates, providing dynamic values like current date/time, prompt metadata, and user context without requiring manual data passing.
|
|
76
|
+
|
|
77
|
+
### Available System Placeholders
|
|
78
|
+
|
|
79
|
+
#### Date/Time Placeholders
|
|
80
|
+
- `{{ _CURRENT_DATE }}` - Current date in YYYY-MM-DD format
|
|
81
|
+
- `{{ _CURRENT_TIME }}` - Current time in HH:MM AM/PM format with timezone
|
|
82
|
+
- `{{ _CURRENT_DATE_AND_TIME }}` - Full timestamp with date and time
|
|
83
|
+
- `{{ _CURRENT_DAY_OF_WEEK }}` - Current day name (e.g., Monday, Tuesday)
|
|
84
|
+
- `{{ _CURRENT_TIMEZONE }}` - Current timezone identifier
|
|
85
|
+
- `{{ _CURRENT_TIMESTAMP_UTC }}` - Current UTC timestamp in ISO format
|
|
86
|
+
|
|
87
|
+
#### Prompt Metadata Placeholders
|
|
88
|
+
- `{{ _OUTPUT_EXAMPLE }}` - The expected output example from the prompt configuration
|
|
89
|
+
- `{{ _PROMPT_NAME }}` - The name of the current prompt
|
|
90
|
+
- `{{ _PROMPT_DESCRIPTION }}` - The description of the current prompt
|
|
91
|
+
- `{{ _EXPECTED_OUTPUT_TYPE }}` - The expected output type (string, object, number, etc.)
|
|
92
|
+
- `{{ _RESPONSE_FORMAT }}` - The expected response format from the prompt
|
|
93
|
+
|
|
94
|
+
#### User Context Placeholders
|
|
95
|
+
- `{{ _USER_NAME }}` - Current user's full name
|
|
96
|
+
- `{{ _USER_EMAIL }}` - Current user's email address
|
|
97
|
+
- `{{ _USER_ID }}` - Current user's unique identifier
|
|
98
|
+
|
|
99
|
+
#### Environment Placeholders
|
|
100
|
+
- `{{ _ENVIRONMENT }}` - Current environment (development, staging, production)
|
|
101
|
+
- `{{ _API_VERSION }}` - Current API version
|
|
102
|
+
|
|
103
|
+
### System Placeholder Usage Examples
|
|
104
|
+
|
|
105
|
+
#### Example 1: Time-Aware Agent Prompt
|
|
106
|
+
```liquid
|
|
107
|
+
You are an AI assistant helping {{ _USER_NAME }} on {{ _CURRENT_DAY_OF_WEEK }}, {{ _CURRENT_DATE }} at {{ _CURRENT_TIME }}.
|
|
108
|
+
|
|
109
|
+
User's request: {{ userRequest }}
|
|
110
|
+
|
|
111
|
+
Please provide a helpful response considering the current time and day.
|
|
112
|
+
```
|
|
113
|
+
|
|
114
|
+
#### Example 2: Agent Type System Prompt with Metadata
|
|
115
|
+
```liquid
|
|
116
|
+
# Agent Type: Loop Decision Maker
|
|
117
|
+
|
|
118
|
+
Current execution context:
|
|
119
|
+
- Date/Time: {{ _CURRENT_DATE_AND_TIME }}
|
|
120
|
+
- User: {{ _USER_NAME }} ({{ _USER_EMAIL }})
|
|
121
|
+
- Environment: {{ _ENVIRONMENT }}
|
|
122
|
+
|
|
123
|
+
## Expected Output Format
|
|
124
|
+
{{ _OUTPUT_EXAMPLE }}
|
|
125
|
+
|
|
126
|
+
## Agent Specific Instructions
|
|
127
|
+
{{ agentResponse }}
|
|
128
|
+
|
|
129
|
+
Based on the above agent response and the expected output format ({{ _EXPECTED_OUTPUT_TYPE }}), determine the next step.
|
|
130
|
+
```
|
|
131
|
+
|
|
132
|
+
#### Example 3: Debug-Friendly Prompt
|
|
133
|
+
```liquid
|
|
134
|
+
[Debug Info]
|
|
135
|
+
- Prompt: {{ _PROMPT_NAME }}
|
|
136
|
+
- Description: {{ _PROMPT_DESCRIPTION }}
|
|
137
|
+
- Expected Output: {{ _EXPECTED_OUTPUT_TYPE }}
|
|
138
|
+
- User ID: {{ _USER_ID }}
|
|
139
|
+
- Timestamp: {{ _CURRENT_TIMESTAMP_UTC }}
|
|
140
|
+
|
|
141
|
+
[Task]
|
|
142
|
+
{{ taskDescription }}
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
### Adding Custom System Placeholders
|
|
146
|
+
|
|
147
|
+
You can add custom system placeholders programmatically:
|
|
148
|
+
|
|
149
|
+
```typescript
|
|
150
|
+
import { SystemPlaceholderManager } from '@memberjunction/ai-prompts';
|
|
151
|
+
|
|
152
|
+
// Add a custom placeholder
|
|
153
|
+
SystemPlaceholderManager.addPlaceholder({
|
|
154
|
+
name: '_ORGANIZATION_NAME',
|
|
155
|
+
description: 'Current organization name',
|
|
156
|
+
getValue: async (params) => {
|
|
157
|
+
// Custom logic to get organization name
|
|
158
|
+
return params.contextUser?.OrganizationName || 'Default Organization';
|
|
159
|
+
}
|
|
160
|
+
});
|
|
161
|
+
|
|
162
|
+
// Or add directly to the array
|
|
163
|
+
const placeholders = SystemPlaceholderManager.getPlaceholders();
|
|
164
|
+
placeholders.push({
|
|
165
|
+
name: '_CUSTOM_VALUE',
|
|
166
|
+
description: 'My custom value',
|
|
167
|
+
getValue: async (params) => 'custom result'
|
|
168
|
+
});
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
### Data Merge Priority Order
|
|
172
|
+
|
|
173
|
+
When rendering templates, data is merged in this priority order (highest to lowest):
|
|
174
|
+
1. Template-specific data (`templateData` parameter)
|
|
175
|
+
2. Child template renders (for hierarchical template composition)
|
|
176
|
+
3. User-provided data (`data` parameter)
|
|
177
|
+
4. System placeholders (lowest priority)
|
|
178
|
+
|
|
179
|
+
This means users can override system placeholders by providing their own values with the same names.
|
|
180
|
+
|
|
181
|
+
### ⚡ Parallel Processing
|
|
182
|
+
Multi-model execution with intelligent result selection strategies and AI judge ranking for optimal results.
|
|
183
|
+
|
|
184
|
+
### ✅ Output Validation
|
|
185
|
+
JSON schema validation against OutputExample with intelligent retry logic and configurable validation behaviors.
|
|
186
|
+
|
|
187
|
+
### 🚫 Cancellation Support
|
|
188
|
+
AbortSignal integration for graceful execution cancellation with proper cleanup and partial result preservation.
|
|
189
|
+
|
|
190
|
+
### 📈 Progress & Streaming
|
|
191
|
+
Real-time progress callbacks and streaming response support for responsive user interfaces.
|
|
192
|
+
|
|
193
|
+
### 📊 Comprehensive Tracking
|
|
194
|
+
Hierarchical execution logging with the AIPromptRun entity, including token usage, timing, and validation attempts.
|
|
195
|
+
|
|
196
|
+
### 🤖 Agent Integration
|
|
197
|
+
Seamless integration with AI Agents through hierarchical prompts and execution tracking.
|
|
198
|
+
|
|
199
|
+
### 💾 Intelligent Caching
|
|
200
|
+
Vector similarity matching and TTL-based result caching for performance optimization.
|
|
201
|
+
|
|
202
|
+
### 🔧 Template Integration
|
|
203
|
+
Dynamic prompt generation with MemberJunction template system supporting conditionals, loops, and data injection.
|
|
24
204
|
|
|
25
205
|
## Installation
|
|
26
206
|
|
|
@@ -39,6 +219,25 @@ npm install @memberjunction/ai-prompts
|
|
|
39
219
|
|
|
40
220
|
## Core Architecture
|
|
41
221
|
|
|
222
|
+
### Dynamic vs Static Template Composition
|
|
223
|
+
|
|
224
|
+
The AI Prompts system introduces **dynamic template composition** that extends beyond MemberJunction's built-in static template features:
|
|
225
|
+
|
|
226
|
+
#### Static Template Composition (MJ Templates)
|
|
227
|
+
MemberJunction's template system supports embedding templates within templates through `{% include %}` directives. This is perfect for fixed relationships:
|
|
228
|
+
- Email templates with standard headers/footers
|
|
229
|
+
- Report templates with consistent formatting sections
|
|
230
|
+
- Any scenario where Template A always includes Templates B and C
|
|
231
|
+
|
|
232
|
+
#### Dynamic Template Composition (AI Prompts)
|
|
233
|
+
The AI Prompts system adds runtime template composition where relationships are determined dynamically:
|
|
234
|
+
- **Runtime Flexibility**: Inject ANY prompt template into ANY other prompt template
|
|
235
|
+
- **Context-Aware**: Choose which child templates to inject based on runtime conditions
|
|
236
|
+
- **Agent Architecture**: Combine system prompts (control flow) with agent prompts (domain logic)
|
|
237
|
+
- **Modular Design**: Build complex prompts from reusable components selected at runtime
|
|
238
|
+
|
|
239
|
+
**Key Difference**: While MJ Templates handle "Template A always includes B", AI Prompts handle "Template A includes X, where X is determined at runtime"
|
|
240
|
+
|
|
42
241
|
### AIPromptRunner Class
|
|
43
242
|
|
|
44
243
|
The `AIPromptRunner` class is the central component for executing prompts with advanced features:
|
|
@@ -66,9 +265,14 @@ const result = await runner.ExecutePrompt(params);
|
|
|
66
265
|
if (result.success) {
|
|
67
266
|
console.log("Summary:", result.result);
|
|
68
267
|
console.log(`Execution time: ${result.executionTimeMS}ms`);
|
|
69
|
-
console.log(`
|
|
268
|
+
console.log(`Prompt tokens: ${result.promptTokens}`);
|
|
269
|
+
console.log(`Completion tokens: ${result.completionTokens}`);
|
|
270
|
+
console.log(`Total tokens: ${result.tokensUsed}`);
|
|
271
|
+
if (result.cost) {
|
|
272
|
+
console.log(`Cost: ${result.cost} ${result.costCurrency || 'USD'}`);
|
|
273
|
+
}
|
|
70
274
|
} else {
|
|
71
|
-
console.error("Error:", result.
|
|
275
|
+
console.error("Error:", result.errorMessage);
|
|
72
276
|
}
|
|
73
277
|
```
|
|
74
278
|
|
|
@@ -152,7 +356,56 @@ if (result.promptRun?.Messages) {
|
|
|
152
356
|
}
|
|
153
357
|
```
|
|
154
358
|
|
|
155
|
-
### 4.
|
|
359
|
+
### 4. Dynamic Template Composition for AI Agents
|
|
360
|
+
|
|
361
|
+
This example demonstrates the primary use case for dynamic template composition - the AI Agent system:
|
|
362
|
+
|
|
363
|
+
```typescript
|
|
364
|
+
import { AIPromptRunner, ChildPromptParam } from '@memberjunction/ai-prompts';
|
|
365
|
+
|
|
366
|
+
// Agent Type System Prompt - Controls execution flow and response format
|
|
367
|
+
const agentTypeSystemPrompt = {
|
|
368
|
+
Name: "Data Analysis Agent Type System Prompt",
|
|
369
|
+
TemplateID: "system-prompt-template-id",
|
|
370
|
+
// Template contains: "You are an AI agent. {{ agentInstructions }} Respond with JSON..."
|
|
371
|
+
};
|
|
372
|
+
|
|
373
|
+
// Individual Agent Prompt - Contains domain-specific logic
|
|
374
|
+
const specificAgentPrompt = {
|
|
375
|
+
Name: "Customer Churn Analysis Agent",
|
|
376
|
+
TemplateID: "churn-agent-template-id",
|
|
377
|
+
// Template contains: "Analyze customer data for churn risk factors..."
|
|
378
|
+
};
|
|
379
|
+
|
|
380
|
+
// At runtime, dynamically compose the prompts
|
|
381
|
+
const runner = new AIPromptRunner();
|
|
382
|
+
const result = await runner.ExecutePrompt({
|
|
383
|
+
prompt: agentTypeSystemPrompt, // Parent template
|
|
384
|
+
childPrompts: [
|
|
385
|
+
// Dynamically inject the specific agent's instructions
|
|
386
|
+
new ChildPromptParam(specificAgentPrompt, 'agentInstructions')
|
|
387
|
+
],
|
|
388
|
+
data: {
|
|
389
|
+
customerData: analysisData,
|
|
390
|
+
thresholds: { churnRisk: 0.7 }
|
|
391
|
+
},
|
|
392
|
+
contextUser: currentUser
|
|
393
|
+
});
|
|
394
|
+
|
|
395
|
+
// The system executed ONE prompt that combined:
|
|
396
|
+
// 1. System prompt wrapper (control flow)
|
|
397
|
+
// 2. Specific agent instructions (domain logic)
|
|
398
|
+
// 3. Runtime data
|
|
399
|
+
console.log("Agent decision:", result.result);
|
|
400
|
+
```
|
|
401
|
+
|
|
402
|
+
**Why This Matters:**
|
|
403
|
+
- Different agents can use the SAME system prompt template
|
|
404
|
+
- System prompt enforces consistent response format across all agents
|
|
405
|
+
- Agent-specific logic is cleanly separated and reusable
|
|
406
|
+
- Runtime composition allows flexible agent architectures
|
|
407
|
+
|
|
408
|
+
### 5. Complete Example with All New Features
|
|
156
409
|
|
|
157
410
|
```typescript
|
|
158
411
|
import { AIPromptRunner } from '@memberjunction/ai-prompts';
|
|
@@ -608,9 +861,85 @@ TokensUsed int -- Total tokens consumed
|
|
|
608
861
|
TokensPrompt int -- Prompt tokens used
|
|
609
862
|
TokensCompletion int -- Completion tokens generated
|
|
610
863
|
|
|
864
|
+
-- Cost tracking
|
|
865
|
+
Cost decimal(19,8) -- Cost of this specific execution
|
|
866
|
+
CostCurrency nvarchar(10) -- ISO 4217 currency code (USD, EUR, etc.)
|
|
867
|
+
|
|
868
|
+
-- Hierarchical rollup fields (NEW)
|
|
869
|
+
TokensUsedRollup int -- Total tokens including all children
|
|
870
|
+
TokensPromptRollup int -- Total prompt tokens including all children
|
|
871
|
+
TokensCompletionRollup int -- Total completion tokens including all children
|
|
872
|
+
-- Note: TotalCost (existing field) serves as the cost rollup
|
|
873
|
+
|
|
611
874
|
-- Context and configuration
|
|
612
875
|
Messages nvarchar(max) -- JSON with input data and metadata
|
|
613
876
|
ConfigurationID uniqueidentifier -- Environment configuration used
|
|
877
|
+
AgentRunID uniqueidentifier -- Links to parent AIAgentRun if applicable
|
|
878
|
+
```
|
|
879
|
+
|
|
880
|
+
### Hierarchical Token and Cost Tracking
|
|
881
|
+
|
|
882
|
+
The AI Prompts system implements a sophisticated rollup pattern for tracking token usage and costs across hierarchical prompt executions:
|
|
883
|
+
|
|
884
|
+
#### Prompt Execution Rollup Pattern
|
|
885
|
+
|
|
886
|
+
For hierarchical prompt executions (parent prompts with child prompts), each node in the tree contains:
|
|
887
|
+
- **Direct fields** (`TokensPrompt`, `TokensCompletion`, `Cost`): Usage for just that execution
|
|
888
|
+
- **Rollup fields** (`TokensPromptRollup`, `TokensCompletionRollup`, `TotalCost`): Total including all descendants
|
|
889
|
+
|
|
890
|
+
**Example:**
|
|
891
|
+
```
|
|
892
|
+
Parent Prompt (100 prompt, 200 completion tokens, $0.05)
|
|
893
|
+
├── Child A (50 prompt, 100 completion, $0.02)
|
|
894
|
+
└── Child B (75 prompt, 150 completion, $0.03)
|
|
895
|
+
|
|
896
|
+
Database records:
|
|
897
|
+
- Parent: TokensPrompt=100, TokensPromptRollup=225 (100+50+75)
|
|
898
|
+
TokensCompletion=200, TokensCompletionRollup=450 (200+100+150)
|
|
899
|
+
Cost=0.05, TotalCost=0.10 (0.05+0.02+0.03)
|
|
900
|
+
- Child A: TokensPrompt=50, TokensPromptRollup=50 (leaf node)
|
|
901
|
+
Cost=0.02, TotalCost=0.02 (leaf node)
|
|
902
|
+
- Child B: TokensPrompt=75, TokensPromptRollup=75 (leaf node)
|
|
903
|
+
Cost=0.03, TotalCost=0.03 (leaf node)
|
|
904
|
+
```
|
|
905
|
+
|
|
906
|
+
This enables efficient queries like:
|
|
907
|
+
- "What was the total cost of this hierarchical prompt?" → Check root's `TotalCost`
|
|
908
|
+
- "How many tokens did this sub-prompt and its children use?" → Check that node's rollup fields
|
|
909
|
+
- No complex SQL joins or recursive CTEs needed!
|
|
910
|
+
|
|
911
|
+
#### Agent Run Token Tracking
|
|
912
|
+
|
|
913
|
+
The `AIAgentRun` entity tracks aggregate token usage across all prompt executions during an agent's lifecycle:
|
|
914
|
+
|
|
915
|
+
```sql
|
|
916
|
+
-- New fields in AIAgentRun
|
|
917
|
+
TotalTokensUsed int -- Total tokens (existing)
|
|
918
|
+
TotalPromptTokensUsed int -- Breakdown: prompt tokens (NEW)
|
|
919
|
+
TotalCompletionTokensUsed int -- Breakdown: completion tokens (NEW)
|
|
920
|
+
TotalCost decimal -- Total cost (existing)
|
|
921
|
+
|
|
922
|
+
-- Hierarchical agent rollup fields (NEW)
|
|
923
|
+
TotalTokensUsedRollup int -- Including sub-agent runs
|
|
924
|
+
TotalPromptTokensUsedRollup int -- Including sub-agent runs
|
|
925
|
+
TotalCompletionTokensUsedRollup int -- Including sub-agent runs
|
|
926
|
+
TotalCostRollup decimal -- Including sub-agent runs
|
|
927
|
+
```
|
|
928
|
+
|
|
929
|
+
**Agent Hierarchy Example:**
|
|
930
|
+
```
|
|
931
|
+
Parent Agent (A)
|
|
932
|
+
├── Own prompts: 200 prompt, 400 completion tokens
|
|
933
|
+
├── Sub-Agent (B)
|
|
934
|
+
│ └── Own prompts: 100 prompt, 200 completion tokens
|
|
935
|
+
└── Sub-Agent (C)
|
|
936
|
+
└── Own prompts: 150 prompt, 300 completion tokens
|
|
937
|
+
|
|
938
|
+
Rollup values:
|
|
939
|
+
- Agent A: TotalPromptTokensUsedRollup = 450 (200+100+150)
|
|
940
|
+
TotalCompletionTokensUsedRollup = 900 (400+200+300)
|
|
941
|
+
- Agent B: TotalPromptTokensUsedRollup = 100 (leaf agent)
|
|
942
|
+
- Agent C: TotalPromptTokensUsedRollup = 150 (leaf agent)
|
|
614
943
|
```
|
|
615
944
|
|
|
616
945
|
### Querying Hierarchical Log Data
|
|
@@ -1188,19 +1517,35 @@ class AIPromptCategoryEntityExtended extends AIPromptCategoryEntity {
|
|
|
1188
1517
|
### Key Interfaces and Types
|
|
1189
1518
|
|
|
1190
1519
|
```typescript
|
|
1191
|
-
interface AIPromptRunResult {
|
|
1520
|
+
interface AIPromptRunResult<T = unknown> {
|
|
1192
1521
|
success: boolean; // Whether the execution was successful
|
|
1193
1522
|
status?: ExecutionStatus; // Current execution status
|
|
1194
1523
|
cancelled?: boolean; // Whether the execution was cancelled
|
|
1195
1524
|
cancellationReason?: CancellationReason; // Reason for cancellation if applicable
|
|
1196
1525
|
rawResult?: string; // The raw result from the AI model
|
|
1197
|
-
result?:
|
|
1526
|
+
result?: T; // The parsed/validated result based on OutputType
|
|
1198
1527
|
errorMessage?: string; // Error message if execution failed
|
|
1199
1528
|
promptRun?: AIPromptRunEntity; // The AIPromptRun entity that was created for tracking
|
|
1200
1529
|
executionTimeMS?: number; // Total execution time in milliseconds
|
|
1201
|
-
|
|
1530
|
+
|
|
1531
|
+
// Token tracking (follows ModelUsage convention)
|
|
1532
|
+
promptTokens?: number; // Prompt/input tokens for this execution
|
|
1533
|
+
completionTokens?: number; // Completion/output tokens for this execution
|
|
1534
|
+
tokensUsed?: number; // Total tokens (calculated getter)
|
|
1535
|
+
|
|
1536
|
+
// Hierarchical token tracking
|
|
1537
|
+
combinedPromptTokens?: number; // Total prompt tokens including all children
|
|
1538
|
+
combinedCompletionTokens?: number; // Total completion tokens including all children
|
|
1539
|
+
combinedTokensUsed?: number; // Total tokens including all children (calculated)
|
|
1540
|
+
|
|
1541
|
+
// Cost tracking
|
|
1542
|
+
cost?: number; // Cost of this execution
|
|
1543
|
+
costCurrency?: string; // ISO 4217 currency code (USD, EUR, etc.)
|
|
1544
|
+
combinedCost?: number; // Total cost including all children
|
|
1545
|
+
|
|
1202
1546
|
validationResult?: ValidationResult; // Validation result if output validation was performed
|
|
1203
|
-
|
|
1547
|
+
validationAttempts?: ValidationAttempt[]; // Detailed validation attempts
|
|
1548
|
+
additionalResults?: AIPromptRunResult<T>[]; // Additional results from parallel execution, ranked by judge
|
|
1204
1549
|
ranking?: number; // Ranking assigned by judge (1 = best, 2 = second best, etc.)
|
|
1205
1550
|
judgeRationale?: string; // Judge's rationale for this ranking
|
|
1206
1551
|
modelInfo?: ModelInfo; // Model information for this result
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIPromptCategoryExtended.d.ts","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AIPromptCategoryExtended.d.ts","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":"AACA,OAAO,EAAE,sBAAsB,EAAE,cAAc,EAAE,MAAM,+BAA+B,CAAC;AAGvF,qBACa,8BAA+B,SAAQ,sBAAsB;IACxE,OAAO,CAAC,QAAQ,CAAwB;IACxC,IAAW,OAAO,IAAI,cAAc,EAAE,CAErC;CACF"}
|
|
@@ -7,9 +7,9 @@ var __decorate = (this && this.__decorate) || function (decorators, target, key,
|
|
|
7
7
|
};
|
|
8
8
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
9
9
|
exports.AIPromptCategoryEntityExtended = void 0;
|
|
10
|
+
const core_1 = require("@memberjunction/core");
|
|
10
11
|
const core_entities_1 = require("@memberjunction/core-entities");
|
|
11
12
|
const global_1 = require("@memberjunction/global");
|
|
12
|
-
const typeorm_1 = require("typeorm");
|
|
13
13
|
let AIPromptCategoryEntityExtended = class AIPromptCategoryEntityExtended extends core_entities_1.AIPromptCategoryEntity {
|
|
14
14
|
constructor() {
|
|
15
15
|
super(...arguments);
|
|
@@ -21,6 +21,6 @@ let AIPromptCategoryEntityExtended = class AIPromptCategoryEntityExtended extend
|
|
|
21
21
|
};
|
|
22
22
|
exports.AIPromptCategoryEntityExtended = AIPromptCategoryEntityExtended;
|
|
23
23
|
exports.AIPromptCategoryEntityExtended = AIPromptCategoryEntityExtended = __decorate([
|
|
24
|
-
(0, global_1.RegisterClass)(
|
|
24
|
+
(0, global_1.RegisterClass)(core_1.BaseEntity, 'AI Prompt Categories')
|
|
25
25
|
], AIPromptCategoryEntityExtended);
|
|
26
26
|
//# sourceMappingURL=AIPromptCategoryExtended.js.map
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIPromptCategoryExtended.js","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":";;;;;;;;;AAAA,iEAAuF;AACvF,mDAAuD;
|
|
1
|
+
{"version":3,"file":"AIPromptCategoryExtended.js","sourceRoot":"","sources":["../src/AIPromptCategoryExtended.ts"],"names":[],"mappings":";;;;;;;;;AAAA,+CAAkD;AAClD,iEAAuF;AACvF,mDAAuD;AAGhD,IAAM,8BAA8B,GAApC,MAAM,8BAA+B,SAAQ,sCAAsB;IAAnE;;QACG,aAAQ,GAAqB,EAAE,CAAC;IAI1C,CAAC;IAHC,IAAW,OAAO;QAChB,OAAO,IAAI,CAAC,QAAQ,CAAC;IACvB,CAAC;CACF,CAAA;AALY,wEAA8B;yCAA9B,8BAA8B;IAD1C,IAAA,sBAAa,EAAC,iBAAU,EAAE,sBAAsB,CAAC;GACrC,8BAA8B,CAK1C"}
|
package/dist/AIPromptRunner.d.ts
CHANGED
|
@@ -1,94 +1,5 @@
|
|
|
1
|
-
|
|
2
|
-
|
|
3
|
-
import { UserInfo, ValidationResult, ValidationErrorInfo } from '@memberjunction/core';
|
|
4
|
-
import { AIPromptEntity, AIPromptRunEntity } from '@memberjunction/core-entities';
|
|
5
|
-
export type ExecutionProgressCallback = (progress: {
|
|
6
|
-
step: 'template_rendering' | 'model_selection' | 'execution' | 'validation' | 'parallel_coordination' | 'result_selection';
|
|
7
|
-
percentage: number;
|
|
8
|
-
message: string;
|
|
9
|
-
metadata?: Record<string, unknown>;
|
|
10
|
-
}) => void;
|
|
11
|
-
export type ExecutionStreamingCallback = (chunk: {
|
|
12
|
-
content: string;
|
|
13
|
-
isComplete: boolean;
|
|
14
|
-
taskId?: string;
|
|
15
|
-
modelName?: string;
|
|
16
|
-
}) => void;
|
|
17
|
-
export type TemplateMessageRole = 'system' | 'user' | 'none';
|
|
18
|
-
export declare class ChildPromptParam {
|
|
19
|
-
childPrompt: AIPromptParams;
|
|
20
|
-
parentPlaceholder: string;
|
|
21
|
-
constructor(childPrompt: AIPromptParams, parentPlaceholder: string);
|
|
22
|
-
}
|
|
23
|
-
export declare class AIPromptParams {
|
|
24
|
-
prompt: AIPromptEntity;
|
|
25
|
-
data?: Record<string, unknown>;
|
|
26
|
-
modelId?: string;
|
|
27
|
-
vendorId?: string;
|
|
28
|
-
configurationId?: string;
|
|
29
|
-
contextUser?: UserInfo;
|
|
30
|
-
skipValidation?: boolean;
|
|
31
|
-
templateData?: Record<string, unknown>;
|
|
32
|
-
conversationMessages?: ChatMessage[];
|
|
33
|
-
templateMessageRole?: TemplateMessageRole;
|
|
34
|
-
cancellationToken?: AbortSignal;
|
|
35
|
-
onProgress?: ExecutionProgressCallback;
|
|
36
|
-
onStreaming?: ExecutionStreamingCallback;
|
|
37
|
-
agentRunId?: string;
|
|
38
|
-
childPrompts?: ChildPromptParam[];
|
|
39
|
-
}
|
|
40
|
-
export type ExecutionStatus = 'pending' | 'running' | 'completed' | 'failed' | 'cancelled';
|
|
41
|
-
export type CancellationReason = 'user_requested' | 'timeout' | 'error' | 'resource_limit';
|
|
42
|
-
export interface ModelInfo {
|
|
43
|
-
modelId: string;
|
|
44
|
-
modelName: string;
|
|
45
|
-
vendorId?: string;
|
|
46
|
-
vendorName?: string;
|
|
47
|
-
powerRank?: number;
|
|
48
|
-
modelType?: string;
|
|
49
|
-
}
|
|
50
|
-
export interface JudgeMetadata {
|
|
51
|
-
judgePromptId: string;
|
|
52
|
-
judgeExecutionTimeMS: number;
|
|
53
|
-
judgeTokensUsed?: number;
|
|
54
|
-
judgeCancelled?: boolean;
|
|
55
|
-
judgeErrorMessage?: string;
|
|
56
|
-
}
|
|
57
|
-
export interface ValidationAttempt {
|
|
58
|
-
attemptNumber: number;
|
|
59
|
-
success: boolean;
|
|
60
|
-
errorMessage?: string;
|
|
61
|
-
validationErrors?: ValidationErrorInfo[];
|
|
62
|
-
rawOutput: string;
|
|
63
|
-
parsedOutput?: unknown;
|
|
64
|
-
timestamp: Date;
|
|
65
|
-
}
|
|
66
|
-
export declare class AIPromptRunResult<T = unknown> {
|
|
67
|
-
success: boolean;
|
|
68
|
-
status?: ExecutionStatus;
|
|
69
|
-
cancelled?: boolean;
|
|
70
|
-
cancellationReason?: CancellationReason;
|
|
71
|
-
rawResult?: string;
|
|
72
|
-
result?: T;
|
|
73
|
-
chatResult: ChatResult;
|
|
74
|
-
errorMessage?: string;
|
|
75
|
-
promptRun?: AIPromptRunEntity;
|
|
76
|
-
executionTimeMS?: number;
|
|
77
|
-
tokensUsed?: number;
|
|
78
|
-
validationResult?: ValidationResult;
|
|
79
|
-
validationAttempts?: ValidationAttempt[];
|
|
80
|
-
additionalResults?: AIPromptRunResult<T>[];
|
|
81
|
-
ranking?: number;
|
|
82
|
-
judgeRationale?: string;
|
|
83
|
-
modelInfo?: ModelInfo;
|
|
84
|
-
judgeMetadata?: JudgeMetadata;
|
|
85
|
-
wasStreamed?: boolean;
|
|
86
|
-
cacheInfo?: {
|
|
87
|
-
cacheHit: boolean;
|
|
88
|
-
cacheKey?: string;
|
|
89
|
-
cacheSource?: string;
|
|
90
|
-
};
|
|
91
|
-
}
|
|
1
|
+
import { AIPromptParams, AIPromptRunResult } from './types';
|
|
2
|
+
export { TemplateMessageRole, AIPromptParams } from './types';
|
|
92
3
|
export declare class AIPromptRunner {
|
|
93
4
|
private _metadata;
|
|
94
5
|
private _templateEngine;
|
|
@@ -107,8 +18,8 @@ export declare class AIPromptRunner {
|
|
|
107
18
|
private executeSinglePrompt;
|
|
108
19
|
private executePromptInParallel;
|
|
109
20
|
private loadTemplate;
|
|
110
|
-
private
|
|
111
|
-
private
|
|
21
|
+
private renderChildPromptTemplates;
|
|
22
|
+
private renderPromptWithChildTemplates;
|
|
112
23
|
private selectModel;
|
|
113
24
|
private createPromptRun;
|
|
114
25
|
private renderPromptTemplate;
|
|
@@ -122,7 +33,6 @@ export declare class AIPromptRunner {
|
|
|
122
33
|
private generateSchemaForValue;
|
|
123
34
|
private parseAndValidateResultEnhanced;
|
|
124
35
|
private validateAgainstSchema;
|
|
125
|
-
private parseAndValidateResult;
|
|
126
36
|
private updatePromptRun;
|
|
127
37
|
}
|
|
128
38
|
export declare function LoadAIPromptRunner(): void;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
{"version":3,"file":"AIPromptRunner.d.ts","sourceRoot":"","sources":["../src/AIPromptRunner.ts"],"names":[],"mappings":"
|
|
1
|
+
{"version":3,"file":"AIPromptRunner.d.ts","sourceRoot":"","sources":["../src/AIPromptRunner.ts"],"names":[],"mappings":"AAYA,OAAO,EAKH,cAAc,EAMd,iBAAiB,EACpB,MAAM,SAAS,CAAC;AAGjB,OAAO,EAAE,mBAAmB,EAAE,cAAc,EAAE,MAAM,SAAS,CAAC;AAmE9D,qBAAa,cAAc;IACzB,OAAO,CAAC,SAAS,CAAW;IAC5B,OAAO,CAAC,eAAe,CAAuB;IAC9C,OAAO,CAAC,iBAAiB,CAAmB;IAC5C,OAAO,CAAC,oBAAoB,CAA+B;IAC3D,OAAO,CAAC,IAAI,CAAM;IAGlB,OAAO,CAAC,MAAM,CAAC,YAAY,CAA4C;IACvE,OAAO,CAAC,MAAM,CAAC,YAAY,CAAoB;;WAkBjC,gBAAgB,IAAI,IAAI;WAQxB,mBAAmB,IAAI;QAAE,IAAI,EAAE,MAAM,CAAC;QAAC,IAAI,EAAE,MAAM,EAAE,CAAA;KAAE;IAiCxD,aAAa,CAAC,CAAC,GAAG,OAAO,EAAE,MAAM,EAAE,cAAc,GAAG,OAAO,CAAC,iBAAiB,CAAC,CAAC,CAAC,CAAC;YA6IhF,mBAAmB;YA8EnB,uBAAuB;YAkOvB,YAAY;YAoBZ,0BAA0B;YAoI1B,8BAA8B;YA+D9B,WAAW;YA8GX,eAAe;YA8Df,oBAAoB;YAkCpB,YAAY;IAwD1B,OAAO,CAAC,iBAAiB;YAwCX,4BAA4B;YAoH5B,eAAe;YAyBf,oCAAoC;IA2ClD,OAAO,CAAC,gCAAgC;IA0BxC,OAAO,CAAC,yBAAyB;IAuBjC,OAAO,CAAC,sBAAsB;YAiChB,8BAA8B;YAqI9B,qBAAqB;YAuErB,eAAe;CAmF9B;AAED,wBAAgB,kBAAkB,SAEjC"}
|