@memberjunction/server 2.58.0 → 2.60.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.
@@ -114,6 +114,9 @@ export class AgentExecutionStreamMessage {
114
114
 
115
115
  @Field()
116
116
  timestamp: Date;
117
+
118
+ // Not a GraphQL field - used internally for streaming
119
+ agentRun?: any;
117
120
  }
118
121
 
119
122
 
@@ -129,72 +132,29 @@ export class RunAIAgentResolver extends ResolverBase {
129
132
  private sanitizeAgentResult(result: ExecuteAgentResult): any {
130
133
  const sanitized: any = {
131
134
  success: result.success,
132
- returnValue: result.returnValue,
133
- errorMessage: result.errorMessage,
134
- finalStep: result.finalStep,
135
- cancelled: result.cancelled,
136
- cancellationReason: result.cancellationReason
135
+ payload: result.payload,
136
+ errorMessage: result.agentRun?.ErrorMessage,
137
+ finalStep: result.agentRun?.FinalStep,
138
+ cancelled: result.agentRun?.Status === 'Cancelled',
139
+ cancellationReason: result.agentRun?.CancellationReason
137
140
  };
138
141
 
139
- // Safely extract agent run data
140
- if (result.agentRun) {
141
- sanitized.agentRun = {
142
- ID: result.agentRun.ID,
143
- Status: result.agentRun.Status,
144
- StartedAt: result.agentRun.StartedAt,
145
- CompletedAt: result.agentRun.CompletedAt,
146
- Success: result.agentRun.Success,
147
- ErrorMessage: result.agentRun.ErrorMessage,
148
- AgentID: result.agentRun.AgentID,
149
- Result: result.agentRun.Result,
150
- TotalTokensUsed: result.agentRun.TotalTokensUsed,
151
- TotalCost: result.agentRun.TotalCost
152
- };
142
+ // Safely extract agent run data using GetAll() for proper serialization
143
+ if (result.agentRun && typeof result.agentRun.GetAll === 'function') {
144
+ // Use GetAll() to get the full serialized object including extended properties
145
+ sanitized.agentRun = result.agentRun.GetAll();
153
146
  }
154
-
155
- // Safely extract execution tree (hierarchical structure with all step data)
156
- if (result.executionTree && Array.isArray(result.executionTree)) {
157
- sanitized.executionTree = this.sanitizeExecutionTree(result.executionTree);
147
+ else {
148
+ // shouldn't ever get here
149
+ console.error('❌ Unexpected agent run structure:', result.agentRun);
158
150
  }
159
151
 
152
+ // Note: executionTree is no longer part of ExecuteAgentResult
153
+ // Step information is available through agentRun.Steps
154
+
160
155
  return sanitized;
161
156
  }
162
157
 
163
- /**
164
- * Sanitize execution tree for JSON serialization
165
- */
166
- private sanitizeExecutionTree(nodes: any[]): any[] {
167
- return nodes.map(node => ({
168
- step: node.step ? {
169
- ID: node.step.ID,
170
- AgentRunID: node.step.AgentRunID,
171
- StepNumber: node.step.StepNumber,
172
- StepType: node.step.StepType,
173
- StepName: node.step.StepName,
174
- TargetID: node.step.TargetID,
175
- Status: node.step.Status,
176
- StartedAt: node.step.StartedAt,
177
- CompletedAt: node.step.CompletedAt,
178
- Success: node.step.Success,
179
- ErrorMessage: node.step.ErrorMessage,
180
- InputData: node.step.InputData,
181
- OutputData: node.step.OutputData
182
- } : null,
183
- inputData: node.inputData,
184
- outputData: node.outputData,
185
- executionType: node.executionType,
186
- startTime: node.startTime,
187
- endTime: node.endTime,
188
- durationMs: node.durationMs,
189
- nextStepDecision: node.nextStepDecision,
190
- children: node.children && node.children.length > 0
191
- ? this.sanitizeExecutionTree(node.children)
192
- : [],
193
- depth: node.depth,
194
- parentStepId: node.parentStepId,
195
- agentHierarchy: node.agentHierarchy
196
- }));
197
- }
198
158
 
199
159
  /**
200
160
  * Parse and validate JSON input
@@ -234,7 +194,7 @@ export class RunAIAgentResolver extends ResolverBase {
234
194
  /**
235
195
  * Create streaming progress callback
236
196
  */
237
- private createProgressCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunIdRef: { id: string }) {
197
+ private createProgressCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunRef: { current: any }) {
238
198
  return (progress: any) => {
239
199
  // Only publish progress for significant steps (not initialization noise)
240
200
  const significantSteps = ['prompt_execution', 'action_execution', 'subagent_execution', 'decision_processing'];
@@ -243,19 +203,27 @@ export class RunAIAgentResolver extends ResolverBase {
243
203
  return;
244
204
  }
245
205
 
206
+ // Get the agent run from the progress metadata or use the ref
207
+ const agentRun = progress.metadata?.agentRun || agentRunRef.current;
208
+ if (!agentRun) {
209
+ console.error('❌ No agent run available for progress callback');
210
+ return;
211
+ }
212
+
246
213
  console.log('📡 Publishing progress update:', {
247
214
  step: progress.step,
248
215
  percentage: progress.percentage,
249
216
  message: progress.message,
250
217
  sessionId,
251
- agentRunId: agentRunIdRef.id
218
+ agentRunId: agentRun.ID
252
219
  });
253
220
 
254
- // Publish progress updates
221
+ // Publish progress updates with the full serialized agent run
255
222
  const progressMsg: AgentExecutionStreamMessage = {
256
223
  sessionId,
257
- agentRunId: agentRunIdRef.id,
224
+ agentRunId: agentRun.ID,
258
225
  type: 'progress',
226
+ agentRun: agentRun.GetAll(), // Serialize the full agent run
259
227
  progress: {
260
228
  currentStep: progress.step,
261
229
  percentage: progress.percentage,
@@ -297,21 +265,29 @@ export class RunAIAgentResolver extends ResolverBase {
297
265
  /**
298
266
  * Create streaming content callback
299
267
  */
300
- private createStreamingCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunIdRef: { id: string }) {
268
+ private createStreamingCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunRef: { current: any }) {
301
269
  return (chunk: any) => {
270
+ // Use the agent run from the ref
271
+ const agentRun = agentRunRef.current;
272
+ if (!agentRun) {
273
+ console.error('❌ No agent run available for streaming callback');
274
+ return;
275
+ }
276
+
302
277
  console.log('💬 Publishing streaming content:', {
303
278
  content: chunk.content.substring(0, 50) + '...',
304
279
  isComplete: chunk.isComplete,
305
280
  stepType: chunk.stepType,
306
281
  sessionId,
307
- agentRunId: agentRunIdRef.id
282
+ agentRunId: agentRun.ID
308
283
  });
309
284
 
310
- // Publish streaming content
285
+ // Publish streaming content with the full serialized agent run
311
286
  const streamMsg: AgentExecutionStreamMessage = {
312
287
  sessionId,
313
- agentRunId: agentRunIdRef.id,
288
+ agentRunId: agentRun.ID,
314
289
  type: 'streaming',
290
+ agentRun: agentRun.GetAll(), // Include the full serialized agent run
315
291
  streaming: {
316
292
  content: chunk.content,
317
293
  isPartial: !chunk.isComplete,
@@ -361,8 +337,8 @@ export class RunAIAgentResolver extends ResolverBase {
361
337
  // Create AI agent runner
362
338
  const agentRunner = new AgentRunner();
363
339
 
364
- // Track agent run ID for streaming (use ref to update later)
365
- const agentRunIdRef = { id: 'pending' };
340
+ // Track agent run for streaming (use ref to update later)
341
+ const agentRunRef = { current: null as any };
366
342
 
367
343
  console.log(`🚀 Starting agent execution with sessionId: ${sessionId}`);
368
344
 
@@ -371,13 +347,13 @@ export class RunAIAgentResolver extends ResolverBase {
371
347
  agent: agentEntity,
372
348
  conversationMessages: parsedMessages,
373
349
  contextUser: currentUser,
374
- onProgress: this.createProgressCallback(pubSub, sessionId, userPayload, agentRunIdRef),
375
- onStreaming: this.createStreamingCallback(pubSub, sessionId, userPayload, agentRunIdRef)
350
+ onProgress: this.createProgressCallback(pubSub, sessionId, userPayload, agentRunRef),
351
+ onStreaming: this.createStreamingCallback(pubSub, sessionId, userPayload, agentRunRef)
376
352
  });
377
353
 
378
- // Update agent run ID once available
354
+ // Update agent run ref once available
379
355
  if (result.agentRun) {
380
- agentRunIdRef.id = result.agentRun.ID;
356
+ agentRunRef.current = result.agentRun;
381
357
  }
382
358
 
383
359
  const executionTime = Date.now() - startTime;
@@ -393,12 +369,12 @@ export class RunAIAgentResolver extends ResolverBase {
393
369
  if (result.success) {
394
370
  LogStatus(`=== AI AGENT RUN COMPLETED FOR: ${agentEntity.Name} (${executionTime}ms) ===`);
395
371
  } else {
396
- LogError(`AI Agent run failed for ${agentEntity.Name}: ${result.errorMessage}`);
372
+ LogError(`AI Agent run failed for ${agentEntity.Name}: ${result.agentRun?.ErrorMessage}`);
397
373
  }
398
374
 
399
375
  return {
400
376
  success: result.success,
401
- errorMessage: result.errorMessage || undefined,
377
+ errorMessage: result.agentRun?.ErrorMessage || undefined,
402
378
  executionTimeMs: executionTime,
403
379
  payload
404
380
  };
@@ -428,25 +404,18 @@ export class RunAIAgentResolver extends ResolverBase {
428
404
  */
429
405
  private publishFinalEvents(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, result: ExecuteAgentResult) {
430
406
  if (result.agentRun) {
431
- // Get the last step from execution tree
407
+ // Get the last step from agent run
432
408
  let lastStep = 'Completed';
433
- if (result.executionTree && result.executionTree.length > 0) {
434
- // Find the last step by traversing the tree
435
- const findLastStep = (nodes: any[]): any => {
436
- let last = nodes[nodes.length - 1];
437
- if (last.children && last.children.length > 0) {
438
- return findLastStep(last.children);
439
- }
440
- return last;
441
- };
442
- const lastNode = findLastStep(result.executionTree);
443
- lastStep = lastNode.step?.StepName || 'Completed';
409
+ if (result.agentRun?.Steps && result.agentRun.Steps.length > 0) {
410
+ // Get the last step from the Steps array
411
+ const lastStepEntity = result.agentRun.Steps[result.agentRun.Steps.length - 1];
412
+ lastStep = lastStepEntity?.StepName || 'Completed';
444
413
  }
445
414
 
446
415
  // Publish partial result
447
416
  const partialResult: AgentPartialResult = {
448
417
  currentStep: lastStep,
449
- partialOutput: result.returnValue || undefined
418
+ partialOutput: result.payload || undefined
450
419
  };
451
420
 
452
421
  const partialMsg: AgentExecutionStreamMessage = {