magnificent-nail-24287
11/15/2025, 2:21 PMmagnificent-nail-24287
11/15/2025, 2:22 PMimport { fileURLToPath } from "node:url";
import {
AutoSubscribe,
cli,
defineAgent,
type JobContext,
ServerOptions,
} from "@livekit/agents";
import type { Participant } from "@livekit/rtc-node";
import { config } from "dotenv";
config({ path: ".env.local" });
export default defineAgent({
entry: async (ctx: JobContext) => {
console.log("[lifecycle] agent entry started");
await ctx.connect(undefined, AutoSubscribe.AUDIO_ONLY);
console.log(`[lifecycle] connected to room ${ctx.room.name}`);
console.log("[lifecycle] waiting for participant...");
const participant = await ctx.waitForParticipant();
console.log(`[lifecycle] participant joined: ${participant.identity}`);
let language = participant.attributes["user.language"];
const onAttrsChanged = (_: Record<string, string>, p: Participant) => {
if (p === participant) {
language = p.attributes["user.language"];
console.log(
`[lifecycle] participant ${p.identity} changed language to ${language}`,
);
}
};
let done = false;
const cleanupAndShutdown = async (reason: string) => {
if (done) return;
done = true;
console.log(`[lifecycle] cleanup started: ${reason}`);
ctx.room.off("participantAttributesChanged", onAttrsChanged);
ctx.room.off("participantDisconnected", onParticipantDisconnected);
ctx.room.off("disconnected", onRoomDisconnected);
console.log("[lifecycle] event handlers removed");
ctx.shutdown(reason);
console.log("[lifecycle] agent shutdown complete");
};
const onParticipantDisconnected = (p: Participant) => {
console.log(`[lifecycle] participant disconnected: ${p.identity}`);
if (p === participant || ctx.room.remoteParticipants.size === 0) {
void cleanupAndShutdown("participant_disconnected");
}
};
const onRoomDisconnected = () => {
console.log("[lifecycle] room disconnected event");
void cleanupAndShutdown("room_disconnected");
};
console.log("[lifecycle] registering event listeners");
ctx.room.on("participantAttributesChanged", onAttrsChanged);
ctx.room.on("participantDisconnected", onParticipantDisconnected);
ctx.room.on("disconnected", onRoomDisconnected);
// ctx.addShutdownCallback(async () => {});
},
});
cli.runApp(
new ServerOptions({
agent: fileURLToPath(import.meta.url),
}),
);
</agent>
<logs>
[dotenv@17.2.3] injecting env (7) from .env.local -- tip: 🔐 encrypt with Dotenvx: https://dotenvx.com
[06:05:40.477] [32mINFO[39m (6012): [36mstarting worker[39m
version: "0.1.0"
[06:05:40.492] [32mINFO[39m (6012): [36mServer is listening on port 61667[39m
[06:05:40.642] [32mINFO[39m (6012): [36mregistered worker[39m
version: "0.1.0"
id: "AW_YWX448N7jgPJ"
server_info: {
"edition": "Cloud",
"version": "1.9.3",
"protocol": 16,
"region": "US West B",
"nodeId": "NC_OPHOENIX1B_U2YJFQg76627",
"debugInfo": "",
"agentProtocol": 0
}
[dotenv@17.2.3] injecting env (0) from .env.local -- tip: 🔑 add access controls to secrets: https://dotenvx.com/ops
[lifecycle] agent entry started
[06:05:56.000] [32mINFO[39m (6012): [36mreceived job request[39m
version: "0.1.0"
job: {
...
}
resuming: false
agentName: ""
[lifecycle] connected to room playground-dghu-k6U8
[lifecycle] waiting for participant...
[lifecycle] participant joined: identity-LHyA
[lifecycle] registering event listeners
[06:05:57.074] [34mDEBUG[39m (lk-rtc/6332): [36mConnect callback received[39m
[lifecycle] participant disconnected: identity-LHyA
[lifecycle] cleanup started: participant_disconnected
[lifecycle] event handlers removed
[06:06:58.919] [33mWARN[39m (6012): [36mjob is unresponsive[39m
</logs>