import { Injectable } from '@nestjs/common';
import { AgentService } from '../agent/agent.service';
import { InspectionExecutionResult } from '../inspector/inspector.types';
import { InspectionTaskEntity } from '../tasks/inspection-task.entity';
import { InspectionExecutor } from './executor.types';

@Injectable()
export class AgentExecutorService implements InspectionExecutor {
  readonly name = 'agent-s' as const;

  constructor(private readonly agentService: AgentService) {}

  async executeTask(
    task: Pick<
      InspectionTaskEntity,
      | 'id'
      | 'name'
      | 'siteUrl'
      | 'stepsJson'
      | 'preferredExecutor'
      | 'allowAgentFallback'
    >,
    traceId: string,
  ): Promise<InspectionExecutionResult> {
    await this.agentService.run({
      runId: traceId,
      instruction: `打开 ${task.siteUrl}，按任务 ${task.name} 的要求执行浏览器操作。`,
      siteUrl: task.siteUrl,
      checkpoints: ['open-site', 'reach-target-page', 'capture-evidence'],
    });

    return this.agentService.runTask(task, traceId);
  }
}
