i want to build a end call functionality from my ...
# ask-ai
b
i want to build a end call functionality from my NEXTJS frontend and Livekit Server
t
Thanks for reaching out, Mayank. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Hey there! If you're looking to add an end call (leave room) feature using LiveKit with a Next.js frontend, here’s a friendly step-by-step guide to help you out: Basic Implementation: • Start by using the
useRoomContext
hook from
@livekit/components-react
to grab the room instance and disconnect it when needed. • Pair that with Next.js's
useRouter
to smoothly navigate away after disconnecting.
Copy code
import { useRoomContext } from "@livekit/components-react";
import { useRouter } from "next/navigation";
import { Button } from "@/components/ui/button";
import { X } from "react-feather";

export default function LeaveButton() {
  const room = useRoomContext();
  const router = useRouter();

  const onClose = async () => {
    await room.disconnect();
    router.push("/");
  };

  return (
    
  );
}
Advanced Implementation: • You might want to use a control bar pattern with some state management while disconnecting. • For instance, manage a loading state (
isDisconnecting
) to disable the button during the process, so users get clear feedback:
Copy code
const onLeave = async () => {
  setIsDisconnecting(true);
  await handleDisconnect();
  setIsDisconnecting(false);
  onDisconnect?.();
};
Best Practices: • Always make sure to disconnect the room instance when leaving — it helps free up resources nicely. • Use loading states during disconnection to keep the user experience smooth and clear. • Don’t forget to add cleanup logic both in your disconnect handler and component cleanup effects. • Also, consider using the
LiveKitRoom
component’s
onDisconnected
prop for automatic cleanup of your UI state — it’s a handy tip! If you want me to walk you through anything else or need some more examples, just give me a shout! Sources: components/livekit/agent-control-bar/agent-control-bar.tsx | complex-agents/ivr-agent/ivr-agent-frontend/src/App.tsx | examples/nextjs/pages/simple.tsx | client/web/src/components/controls/leave-button.tsx | Next.js 12 quickstart | LiveKit Docs
b
what is the difference between @function tool and llm.ai.callable
t
The
@function_tool
decorator is what we use in LiveKit Agents 1.0, while
@llm.ai_callable
was the go-to in Agents 0.x. Here’s a quick rundown of the main differences: • Streamlined Process: With
@function_tool
, you can define tools right inside an
Agent
class, and they’re automatically ready for the LLM to use. Before, with
@llm.ai_callable
, you had to set up functions separately in a
llm.FunctionContext
class and then pass them into the agent constructor. • Argument Handling:
@function_tool
smartly figures out argument types from the function signature and docstring, so you don’t have to fuss with explicit type annotations. On the other hand,
@llm.ai_callable
needed a bit more manual work using annotations like
Annotated
. • RunContext: When using
@function_tool
, your functions get a
RunContext
object, which gives you easy access to the current agent state — making state management a breeze. • Boilerplate Reduction: Thanks to
@function_tool
, there’s no need for a separate context class anymore, which cuts down on boilerplate and keeps your function definitions neat and tidy. If you want me to walk you through any part of this or need some examples, just let me know—I’m happy to help! Sources: Agents 0.x migration guide | LiveKit Docs | Function calling with Voice Agents | LiveKit Docs | Tool definition and use | LiveKit Docs