-
Notifications
You must be signed in to change notification settings - Fork 2.6k
improve EndCallTool #4563
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
longcw
wants to merge
10
commits into
main
Choose a base branch
from
longc/improve-endcall-tool
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+77
−50
Open
improve EndCallTool #4563
Changes from all commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
22a521a
improve EndCallTool
longcw 38a19e4
update docstring
longcw 0c6b434
remove duplicated server
longcw 6f2ac3e
add end_instructions
longcw 0f6433f
fix type
longcw 9b1e72b
remove callback
longcw 6915258
fix ruff
longcw 64c66e7
update default
longcw 4fd8b99
Apply suggestions from code review
longcw 395614d
rename to on_end
longcw File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,28 +1,83 @@ | ||
| from collections.abc import Awaitable | ||
| from typing import Callable | ||
|
|
||
| from ...job import get_job_context | ||
| from ...llm import Tool, Toolset, function_tool | ||
| from ...log import logger | ||
| from ...voice.events import RunContext | ||
| from ...voice.events import CloseEvent, RunContext | ||
|
|
||
| END_CALL_DESCRIPTION = """ | ||
| Ends the current call and disconnects immediately. | ||
|
|
||
| Call when: | ||
| - The user clearly indicates they are done (e.g., “that’s all, bye”). | ||
| - The agent determines the conversation is complete and should end. | ||
|
|
||
| Do not call when: | ||
| - The user asks to pause, hold, or transfer. | ||
| - Intent is unclear. | ||
|
|
||
| This is the final action the agent can take. | ||
| Once called, no further interaction is possible with the user. | ||
| Don't generate any other text or response when the tool is called. | ||
| """ | ||
|
|
||
|
|
||
| class EndCallTool(Toolset): | ||
| @function_tool(name="end_call") | ||
| async def _end_call(self, ctx: RunContext) -> None: | ||
| def __init__( | ||
| self, | ||
| *, | ||
| extra_description: str = "", | ||
| delete_room: bool = True, | ||
| on_end: str | Callable[[RunContext], Awaitable[None]] | None = "say goodbye to the user", | ||
| ): | ||
| """ | ||
| This tool allows the agent to end the call and disconnect from the room. | ||
|
|
||
| Args: | ||
| extra_description: Additional description to add to the end call tool. | ||
| delete_room: Whether to delete the room when the user ends the call. deleting the room disconnects all remote users, including SIP callers. | ||
| on_end: If a string is provided, it will be used as the instructions of | ||
| `session.generate_reply` when the user ends the call. If a callback, it will be called | ||
| when the user ends the call. | ||
| """ | ||
| Ends the current call and disconnects immediately. | ||
| super().__init__() | ||
| self._delete_room = delete_room | ||
| self._extra_description = extra_description | ||
| self._on_end = on_end | ||
|
|
||
| self._end_call_tool = function_tool( | ||
| self._end_call, | ||
| name="end_call", | ||
| description=f"{END_CALL_DESCRIPTION}\n{extra_description}", | ||
| ) | ||
|
|
||
| Call when: | ||
| - The user clearly indicates they are done (e.g., “that’s all, bye”). | ||
| - The agent determines the conversation is complete and should end. | ||
| async def _end_call(self, ctx: RunContext) -> None: | ||
| try: | ||
| logger.debug("end_call tool called") | ||
| ctx.session.once("close", self._on_session_close) | ||
| if isinstance(self._on_end, str): | ||
| await ctx.session.generate_reply(instructions=self._on_end, tool_choice="none") | ||
| elif callable(self._on_end): | ||
| await self._on_end(ctx) | ||
| finally: | ||
| # close the AgentSession | ||
| ctx.session.shutdown() | ||
|
|
||
| Do not call when: | ||
| - The user asks to pause, hold, or transfer. | ||
| - Intent is unclear. | ||
| def _on_session_close(self, ev: CloseEvent) -> None: | ||
| job_ctx = get_job_context() | ||
|
|
||
| This is the final action the agent can take. | ||
| Once called, no further interaction is possible with the user. | ||
| """ | ||
| logger.debug("end_call tool called") | ||
| ctx.session.shutdown() | ||
| if self._delete_room: | ||
|
|
||
| async def _on_shutdown() -> None: | ||
| logger.info("deleting the room because the user ended the call") | ||
| await job_ctx.delete_room() | ||
|
|
||
| job_ctx.add_shutdown_callback(_on_shutdown) | ||
|
|
||
| # shutdown the job process | ||
| job_ctx.shutdown(reason=ev.reason.value) | ||
|
|
||
| @property | ||
| def tools(self) -> list[Tool]: | ||
| return [self._end_call] | ||
| return [self._end_call_tool] |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.