
// backend/agent/interviewerAgent.ts
import { Agent } from '@livekit/agents';
import { RealtimeModel } from '@livekit/agents-plugin-google';
import { AgentContext, JobContext } from '@livekit/agents';
import { connect } from 'livekit-server-sdk';

// This class runs inside the LiveKit Agents runtime or as a process using @livekit/agents
export default class InterviewerAgent extends Agent {
  // called once when agent boots
  async setup(ctx: AgentContext): Promise<void> {
    console.log('InterviewerAgent setup');
  }

  // entrypoint for a job run — called when we want the agent to start
  async entrypoint(ctx: JobContext): Promise<void> {
    console.log('InterviewerAgent entrypoint starting');

    // Create a Gemini realtime model instance
    const model = new RealtimeModel({
      model: 'gemini-1.0-realtime',   // example model name — use the exact name from plugin docs
      voice: 'puck',                  // choose voice
      instructions: 'You are a helpful interviewer. Ask one question, wait for the candidate to respond, then follow up.',
      modalities: ['AUDIO'],
    });

    // Create a voice session bound to the current room
    // The exact API may depend on @livekit/agents-plugin-google version; this matches the common pattern
    const session = await ctx.voice.createSession({ model });

    // Start session event handling
    session.on('transcript', (t) => {
      console.log('transcript:', t);
    });

    session.on('state', (s) => {
      console.log('session state:', s);
    });

    // Start the session — this will cause Gemini to synthesize audio and publish to the LiveKit room
    await session.start();

    // Keep agent running while the job is active
    await ctx.waitForStop();
  }
}