This is the code of version 0.x. How can it be mod...
# ask-ai
b
This is the code of version 0.x. How can it be modified to 1.x
Copy code
@session.on("response_done")
def on_response_done(response: openai.realtime.RealtimeResponse):
    variant: Literal["warning", "destructive"]
    description: str | None = None
    title: str

    print("on_response_done: %s", response.status)
    print("on_response_done: %s", response.status_details)

    if response.status == "incomplete":
        if response.status_details and response.status_details.get("reason"):
            reason = response.status_details["reason"]
            if reason == "max_output_tokens":
                variant = "warning"
                title = "Max output tokens reached"
                description = "Response may be incomplete"
            elif reason == "content_filter":
                variant = "warning"
                title = "Content filter applied"
                description = "Response may be incomplete"
            else:
                variant = "warning"
                title = "Response incomplete"
        else:
            variant = "warning"
            title = "Response incomplete"

    elif response.status == "failed":
        if response.status_details and response.status_details.get("error"):
            error_code = response.status_details["error"]["code"]
            if error_code == "server_error":
                variant = "destructive"
                title = "Server error"
            elif error_code == "rate_limit_exceeded":
                variant = "destructive"
                title = "Rate limit exceeded"
            else:
                variant = "destructive"
                title = "Response failed"
        else:
            variant = "destructive"
            title = "Response failed"

    else:
        return

    asyncio.create_task(show_toast(title, description, variant))
t
Thanks for reaching out, zhuyu. I'm an AI assistant and I'm here to help. Give me a moment while I research this topic.
I'm here to help you smoothly migrate your code from LiveKit version 0.x to 1.x. Here are some important steps to guide you through the process: • Start by replacing the older classes like
VoicePipelineAgent
or
MultimodalAgent
with the new
AgentSession
class, which now acts as a unified agent orchestrator. • Update your pipeline behavior by incorporating the new
llm_node
and `tts_node`—these will help you modify chat context and customize the TTS pipeline more effectively. • Switch to using the
@function_tool
decorator for defining functions accessible to the LLM, as it replaces the previous
@llm.ai_callable
decorator. • Take a fresh look at how you manage chat context, since the API has been redesigned to offer greater flexibility and reliability. • Adjust your event handling for state changes, keeping in mind that the APIs have been revamped for better performance. • Don’t forget to check the migration guide for a helpful list of removed features along with their alternatives. If you'd like detailed instructions and examples, the full migration guide and the code examples are fantastic resources. And of course, if you have any questions or need more support, just let me know—I'm here to help! Sources: Agents 0.x migration guide | LiveKit Docs | How to Get Help from LiveKit | Agents v0.x migration guide - Node.js | LiveKit Docs
b
@session.on("response_done") has no corresponding event in 1.x. How should it be handled
t
It looks like
@session.on("response_done")
isn’t directly supported in LiveKit 1.x. Could you share a bit more about what you’re hoping to achieve with this event? For instance, are you trying to manage response statuses, handle errors, or something else? Understanding this will really help me guide you on the best way to implement it in 1.x. I’m here to help!
b
@session.on("response_done") def on_response_done(response: openai.realtime.RealtimeResponse): variant: Literal["warning", "destructive"] description: str | None = None title: str print("on_response_done: %s", response.status) print("on_response_done: %s", response.status_details) if response.status == "incomplete": if response.status_details and response.status_details.get("reason"): reason = response.status_details["reason"] if reason == "max_output_tokens": variant = "warning" title = "Max output tokens reached" description = "Response may be incomplete" elif reason == "content_filter": variant = "warning" title = "Content filter applied" description = "Response may be incomplete" else: variant = "warning" title = "Response incomplete" else: variant = "warning" title = "Response incomplete" elif response.status == "failed": if response.status_details and response.status_details.get("error"): error_code = response.status_details["error"]["code"] if error_code == "server_error": variant = "destructive" title = "Server error" elif error_code == "rate_limit_exceeded": variant = "destructive" title = "Rate limit exceeded" else: variant = "destructive" title = "Response failed" else: variant = "destructive" title = "Response failed" else: return asyncio.create_task(show_toast(title, description, variant)) Implement the same function in 1.x
t
In LiveKit 1.x, the
@session.on("response_done")
event isn't directly supported, but don't worry—we can still handle response statuses and details using the updated event handling methods. Here's a friendly guide on how you can achieve similar functionality: • When
response.status == "incomplete"
, take a look at
response.status_details
for reasons like
max_output_tokens
or
content_filter
, and handle them as needed. • If
response.status == "failed"
, check
response.status_details["error"]
for error codes such as
server_error
or
rate_limit_exceeded
, and respond accordingly. • For other statuses like
cancelled
, it’s helpful to log the event ID and response status to assist with debugging. You can implement this by using the updated APIs to listen for the relevant events and process the response details. If you'd like, I’m here to help with more guidance or even some code examples—just let me know! Sources: livekit-plugins/livekit-plugins-openai/livekit/plugins/openai/realtime/real... | plugins/google/src/beta/realtime/realtime_api.ts
b
Provide code examples
t
Here is an example of how you can handle response statuses and details in LiveKit 1.x, replicating the functionality of `@session.on("response_done")`:
Copy code
async def handle_response_done(response):
    if response.status == "incomplete":
        reason = response.status_details.get("reason") if response.status_details else None
        if reason == "max_output_tokens":
            title = "Max output tokens reached"
            description = "Response may be incomplete"
        elif reason == "content_filter":
            title = "Content filter applied"
            description = "Response may be incomplete"
        else:
            title = "Response incomplete"
            description = None
        variant = "warning"

    elif response.status == "failed":
        error_code = response.status_details.get("error", {}).get("code") if response.status_details else None
        if error_code == "server_error":
            title = "Server error"
        elif error_code == "rate_limit_exceeded":
            title = "Rate limit exceeded"
        else:
            title = "Response failed"
        variant = "destructive"

    else:
        # Assume "completed", no action needed
        return

    # Trigger a toast notification or similar UI feedback
    await show_toast(title, description, variant)
Feel free to replace
show_toast
with your own way of displaying notifications or managing the response outcome. If you have any questions or need more help, I’m here for you—just let me know! Sources: agent/main.py | livekit-plugins/livekit-plugins-inworld/livekit/plugins/inworld/tts.py | linux/flutter/include/flutter/method_result.h | Sources/LiveKit/Protos/livekit_rtc.pb.swift | livekit-protocol/src/livekit.rs
b
handle_response_done, It cannot be triggered automatically
t
I don't have the answer you're looking for. You could also try asking your question: • in one of the other Slack channels or • to https://deepwiki.com/livekit/livekit_composite which is trained on all LiveKit source code If you find the answer, please post it here to help others!