@memberjunction/server 2.59.0 → 2.61.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,26 @@ 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
 
160
152
  return sanitized;
161
153
  }
162
154
 
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
155
 
199
156
  /**
200
157
  * Parse and validate JSON input
@@ -234,7 +191,7 @@ export class RunAIAgentResolver extends ResolverBase {
234
191
  /**
235
192
  * Create streaming progress callback
236
193
  */
237
- private createProgressCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunIdRef: { id: string }) {
194
+ private createProgressCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunRef: { current: any }) {
238
195
  return (progress: any) => {
239
196
  // Only publish progress for significant steps (not initialization noise)
240
197
  const significantSteps = ['prompt_execution', 'action_execution', 'subagent_execution', 'decision_processing'];
@@ -243,19 +200,27 @@ export class RunAIAgentResolver extends ResolverBase {
243
200
  return;
244
201
  }
245
202
 
203
+ // Get the agent run from the progress metadata or use the ref
204
+ const agentRun = progress.metadata?.agentRun || agentRunRef.current;
205
+ if (!agentRun) {
206
+ console.error('❌ No agent run available for progress callback');
207
+ return;
208
+ }
209
+
246
210
  console.log('📡 Publishing progress update:', {
247
211
  step: progress.step,
248
212
  percentage: progress.percentage,
249
213
  message: progress.message,
250
214
  sessionId,
251
- agentRunId: agentRunIdRef.id
215
+ agentRunId: agentRun.ID
252
216
  });
253
217
 
254
- // Publish progress updates
218
+ // Publish progress updates with the full serialized agent run
255
219
  const progressMsg: AgentExecutionStreamMessage = {
256
220
  sessionId,
257
- agentRunId: agentRunIdRef.id,
221
+ agentRunId: agentRun.ID,
258
222
  type: 'progress',
223
+ agentRun: agentRun.GetAll(), // Serialize the full agent run
259
224
  progress: {
260
225
  currentStep: progress.step,
261
226
  percentage: progress.percentage,
@@ -297,21 +262,29 @@ export class RunAIAgentResolver extends ResolverBase {
297
262
  /**
298
263
  * Create streaming content callback
299
264
  */
300
- private createStreamingCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunIdRef: { id: string }) {
265
+ private createStreamingCallback(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, agentRunRef: { current: any }) {
301
266
  return (chunk: any) => {
267
+ // Use the agent run from the ref
268
+ const agentRun = agentRunRef.current;
269
+ if (!agentRun) {
270
+ console.error('❌ No agent run available for streaming callback');
271
+ return;
272
+ }
273
+
302
274
  console.log('💬 Publishing streaming content:', {
303
275
  content: chunk.content.substring(0, 50) + '...',
304
276
  isComplete: chunk.isComplete,
305
277
  stepType: chunk.stepType,
306
278
  sessionId,
307
- agentRunId: agentRunIdRef.id
279
+ agentRunId: agentRun.ID
308
280
  });
309
281
 
310
- // Publish streaming content
282
+ // Publish streaming content with the full serialized agent run
311
283
  const streamMsg: AgentExecutionStreamMessage = {
312
284
  sessionId,
313
- agentRunId: agentRunIdRef.id,
285
+ agentRunId: agentRun.ID,
314
286
  type: 'streaming',
287
+ agentRun: agentRun.GetAll(), // Include the full serialized agent run
315
288
  streaming: {
316
289
  content: chunk.content,
317
290
  isPartial: !chunk.isComplete,
@@ -361,8 +334,8 @@ export class RunAIAgentResolver extends ResolverBase {
361
334
  // Create AI agent runner
362
335
  const agentRunner = new AgentRunner();
363
336
 
364
- // Track agent run ID for streaming (use ref to update later)
365
- const agentRunIdRef = { id: 'pending' };
337
+ // Track agent run for streaming (use ref to update later)
338
+ const agentRunRef = { current: null as any };
366
339
 
367
340
  console.log(`🚀 Starting agent execution with sessionId: ${sessionId}`);
368
341
 
@@ -371,13 +344,13 @@ export class RunAIAgentResolver extends ResolverBase {
371
344
  agent: agentEntity,
372
345
  conversationMessages: parsedMessages,
373
346
  contextUser: currentUser,
374
- onProgress: this.createProgressCallback(pubSub, sessionId, userPayload, agentRunIdRef),
375
- onStreaming: this.createStreamingCallback(pubSub, sessionId, userPayload, agentRunIdRef)
347
+ onProgress: this.createProgressCallback(pubSub, sessionId, userPayload, agentRunRef),
348
+ onStreaming: this.createStreamingCallback(pubSub, sessionId, userPayload, agentRunRef)
376
349
  });
377
350
 
378
- // Update agent run ID once available
351
+ // Update agent run ref once available
379
352
  if (result.agentRun) {
380
- agentRunIdRef.id = result.agentRun.ID;
353
+ agentRunRef.current = result.agentRun;
381
354
  }
382
355
 
383
356
  const executionTime = Date.now() - startTime;
@@ -393,12 +366,12 @@ export class RunAIAgentResolver extends ResolverBase {
393
366
  if (result.success) {
394
367
  LogStatus(`=== AI AGENT RUN COMPLETED FOR: ${agentEntity.Name} (${executionTime}ms) ===`);
395
368
  } else {
396
- LogError(`AI Agent run failed for ${agentEntity.Name}: ${result.errorMessage}`);
369
+ LogError(`AI Agent run failed for ${agentEntity.Name}: ${result.agentRun?.ErrorMessage}`);
397
370
  }
398
371
 
399
372
  return {
400
373
  success: result.success,
401
- errorMessage: result.errorMessage || undefined,
374
+ errorMessage: result.agentRun?.ErrorMessage || undefined,
402
375
  executionTimeMs: executionTime,
403
376
  payload
404
377
  };
@@ -428,25 +401,18 @@ export class RunAIAgentResolver extends ResolverBase {
428
401
  */
429
402
  private publishFinalEvents(pubSub: PubSubEngine, sessionId: string, userPayload: UserPayload, result: ExecuteAgentResult) {
430
403
  if (result.agentRun) {
431
- // Get the last step from execution tree
404
+ // Get the last step from agent run
432
405
  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';
406
+ if (result.agentRun?.Steps && result.agentRun.Steps.length > 0) {
407
+ // Get the last step from the Steps array
408
+ const lastStepEntity = result.agentRun.Steps[result.agentRun.Steps.length - 1];
409
+ lastStep = lastStepEntity?.StepName || 'Completed';
444
410
  }
445
411
 
446
412
  // Publish partial result
447
413
  const partialResult: AgentPartialResult = {
448
414
  currentStep: lastStep,
449
- partialOutput: result.returnValue || undefined
415
+ partialOutput: result.payload || undefined
450
416
  };
451
417
 
452
418
  const partialMsg: AgentExecutionStreamMessage = {