This message was deleted.
# ask-ai
s
This message was deleted.
t
Thanks for reaching out, Mohammed. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
Thanks — when you say "live tasks," do you mean the Agents "tasks" / "task groups" used in LiveKit Agents (e.g.,
AgentTask
,
GetEmailTask
,
GetDtmfTask
), or something else? If you mean Agents tasks, what's your goal with them? • Collect user input (email, DTMF)? • Orchestrate multi-step workflows? • Run reusable task logic inside an agent? Tell me which of the above (or describe your use case) and whether you want code examples or high-level steps. I’ll then give the exact steps or sample code to implement it. Sources: Tasks and task groups | LiveKit docs | Workflows | LiveKit docs
You're asking for a task that collects a user's full name, email, and mobile number. Below is a concise custom task you can use with LiveKit Agents to do exactly that, plus a short example of running it. 1) Task definition (Python)
Copy code
from dataclasses import dataclass
from livekit.agents import AgentTask, function_tool

@dataclass
class UserInfoResult:
    name: str
    email: str
    mobile_number: str

class CollectUserInfoTask(AgentTask[UserInfoResult]):
    def __init__(self, chat_ctx=None):
        super().__init__(
            instructions="Collect the user's full name, email address, and mobile phone number.",
            chat_ctx=chat_ctx,
        )
        self._results = {}

    async def on_enter(self) -> None:
        await self.session.generate_reply(
            instructions="Hello! I need to collect your full name. What is your name?"
        )

    @function_tool()
    async def provide_name(self, name: str):
        self._results["name"] = name
        await self.session.generate_reply(
            instructions="Thanks. Please provide your email address."
        )

    @function_tool()
    async def provide_email(self, email: str):
        self._results["email"] = email
        await self.session.generate_reply(
            instructions="Great. Lastly, please provide your mobile phone number."
        )

    @function_tool()
    async def provide_mobile_number(self, mobile_number: str):
        self._results["mobile_number"] = mobile_number
        result = UserInfoResult(
            name=self._results.get("name", ""),
            email=self._results.get("email", ""),
            mobile_number=self._results.get("mobile_number", ""),
        )
        self.complete(result)
2) Run the task inside an Agent
Copy code
from livekit.agents import Agent

class MyAgent(Agent):
    async def on_enter(self) -> None:
        user_info = await CollectUserInfoTask(chat_ctx=self.chat_ctx)
        await self.session.generate_reply(
            instructions=f"Thanks {user_info.name}. We've recorded your email ({user_info.email}) and mobile ({user_info.mobile_number})."
        )
Notes and options • Use the prebuilt
GetEmailTask
if you want email validation; otherwise the
provide_email
tool above accepts a string. • If you want phone validation/formatting, add validation inside
provide_mobile_number
before calling
self.complete(...)
. • I can provide a version that validates email/phone formats or a TypeScript example—which would you prefer? Sources: Tasks and task groups | LiveKit docs | External data and RAG | LiveKit Docs
Thanks — I’ve updated the custom task to include email and phone validation (with simple regex checks) and notes on using stronger validation libraries. 1) Task with validation (Python)
Copy code
from dataclasses import dataclass
import re
from livekit.agents import AgentTask, function_tool

@dataclass
class UserInfoResult:
    name: str
    email: str
    mobile_number: str

EMAIL_RE = re.compile(r"^[^@]+@[^@]+\.[^@]+$")
PHONE_RE = re.compile(r"^\+?\d{7,15}$")  # basic international format: digits, optional leading '+'

class CollectUserInfoTask(AgentTask[UserInfoResult]):
    def __init__(self, chat_ctx=None):
        super().__init__(
            instructions="Collect the user's full name, validated email address, and validated mobile phone number.",
            chat_ctx=chat_ctx,
        )
        self._results = {}

    async def on_enter(self) -> None:
        await self.session.generate_reply(
            instructions="Hello! I need to collect your full name. What is your name?"
        )

    @function_tool()
    async def provide_name(self, name: str):
        name = name.strip()
        if not name:
            await self.session.generate_reply(instructions="Name cannot be empty. Please provide your full name.")
            return
        self._results["name"] = name
        await self.session.generate_reply(instructions="Thanks. Please provide your email address.")

    @function_tool()
    async def provide_email(self, email: str):
        email = email.strip()
        if not EMAIL_RE.match(email):
            await self.session.generate_reply(instructions="That email looks invalid. Please provide a valid email (e.g., user@example.com).")
            return
        self._results["email"] = email
        await self.session.generate_reply(instructions="Great. Lastly, please provide your mobile phone number (include country code, e.g., +123456789).")

    @function_tool()
    async def provide_mobile_number(self, mobile_number: str):
        mobile = mobile_number.strip()
        if not PHONE_RE.match(mobile):
            await self.session.generate_reply(instructions="That phone number looks invalid. Please provide digits only, optionally starting with '+', minimum 7 digits.")
            return
        self._results["mobile_number"] = mobile
        result = UserInfoResult(
            name=self._results.get("name", ""),
            email=self._results.get("email", ""),
            mobile_number=self._results.get("mobile_number", ""),
        )
        self.complete(result)
2) Notes and next steps • The regex validations above are basic. For stronger validation: ◦ Use the
email_validator
library for robust email checks. ◦ Use
phonenumbers
(Google libphonenumber) for parsing/formatting/validation of international phone numbers. • Want me to provide the same task using
phonenumbers
and
email_validator
?