All files / src/llm-orchestration/hooks follow-up-post-execution.hook.ts

90% Statements 18/20
66.66% Branches 2/3
100% Functions 3/3
88.88% Lines 16/18

Press n or j to go to the next uncovered block, b, p or k for the previous block.

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 527x       7x     7x 11x     11x 11x               11x 1x 1x     10x           10x 3x       3x               3x 3x     7x      
import { forwardRef, Inject, Injectable, Logger } from '@nestjs/common';
import { PostExecutionHook } from './post-execution-hook.interface';
import { PlanExecutionContext } from '../llm-orchestration.interfaces';
import { SessionInput } from '../../core-entities';
import { SessionInputsService } from '../../session-inputs/session-inputs.service';
 
@Injectable()
export class FollowUpPostExecutionHook implements PostExecutionHook {
  private readonly logger = new Logger(FollowUpPostExecutionHook.name);
 
  constructor(
    @Inject(forwardRef(() => SessionInputsService))
    private readonly sessionInputsService: SessionInputsService,
  ) {}
 
  async run(
    sessionInput: SessionInput,
    context: PlanExecutionContext,
  ): Promise<void> {
    // If a higher-priority hook has already initiated a new turn, do nothing.
    if (context.flags.should_halt_hooks) {
      this.logger.log('Follow-up hook skipped because hook chain was halted.');
      return;
    }
 
    Iif (context.flags.is_final) {
      this.logger.log('Plan marked as final. No follow-up will be initiated.');
      return;
    }
 
    // Check if there are tool results that need to be sent back to LLM
    if (context.toolResults.length > 0) {
      this.logger.log(
        `Triggering follow-up with ${context.toolResults.length} tool result(s)`,
      );
 
      await this.sessionInputsService.create(sessionInput.session_id, {
        user_prompt: '', // Empty - tool results are in history via role: 'tool' messages
        execution_strategy: sessionInput.execution_strategy,
        skip_persistence: true, // Don't create SessionInput record
        ad_hoc_context_definition: undefined,
        context_template_id: undefined,
      });
 
      context.flags.follow_up_initiated = true;
      return;
    }
 
    this.logger.log('No tool results - no follow-up needed.');
  }
}