Sessions Module¶
voice_agent.sessions
¶
Session management for Claude Code SDK.
ChatStoredState
dataclass
¶
Stored state for all sessions in a chat.
Attributes:
| Name | Type | Description |
|---|---|---|
active_session |
str
|
Name of the currently active session. |
sessions |
dict[str, StoredSession]
|
Mapping of session names to stored sessions. |
Source code in src/voice_agent/sessions/storage.py
49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
from_dict(chat_id, data)
classmethod
¶
Create from dictionary.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID (needed for session creation). |
required |
data
|
dict[str, Any]
|
Raw dictionary data. |
required |
Source code in src/voice_agent/sessions/storage.py
68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 | |
to_dict()
¶
Convert to dictionary for JSON serialization.
Source code in src/voice_agent/sessions/storage.py
61 62 63 64 65 66 | |
ImageAttachment
dataclass
¶
An image attachment for sending to Claude.
Attributes:
| Name | Type | Description |
|---|---|---|
data |
str
|
Base64-encoded image data. |
media_type |
str
|
MIME type of the image (e.g. "image/jpeg"). |
Source code in src/voice_agent/sessions/image.py
6 7 8 9 10 11 12 13 14 15 16 | |
PermissionHandler
¶
Handles permission requests for Claude SDK tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
pending |
PendingPermission | None
|
Current pending permission, if any. |
timeout |
Seconds to wait for user approval. |
|
notify_callback |
Async callback to notify user of permission request. |
|
sticky_approvals |
list[StickyApproval]
|
List of sticky approval rules for auto-approving. |
Source code in src/voice_agent/sessions/permissions.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
__init__(timeout=300, notify_callback=None)
¶
Initialize the permission handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
Seconds to wait for user approval. |
300
|
notify_callback
|
Callable[[str, dict[str, Any]], Coroutine[Any, Any, None]] | None
|
Async function to notify user of permission request. |
None
|
Source code in src/voice_agent/sessions/permissions.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
approve()
¶
Approve the pending permission.
Returns:
| Type | Description |
|---|---|
bool
|
True if there was a pending permission to approve. |
Source code in src/voice_agent/sessions/permissions.py
276 277 278 279 280 281 282 283 284 285 286 | |
clear_sticky_approvals()
¶
Clear all sticky approvals.
Returns:
| Type | Description |
|---|---|
int
|
Number of approvals cleared. |
Source code in src/voice_agent/sessions/permissions.py
341 342 343 344 345 346 347 348 349 | |
deny(message=None)
¶
Deny the pending permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str | None
|
Optional message explaining denial. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if there was a pending permission to deny. |
Source code in src/voice_agent/sessions/permissions.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
get_pending_description()
¶
Get a human-readable description of the pending permission.
Returns:
| Type | Description |
|---|---|
str | None
|
Description string or None if no pending permission. |
Source code in src/voice_agent/sessions/permissions.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
get_sticky_approvals()
¶
Get all active sticky approvals.
Returns:
| Type | Description |
|---|---|
list[StickyApproval]
|
List of sticky approval rules. |
Source code in src/voice_agent/sessions/permissions.py
333 334 335 336 337 338 339 | |
has_pending()
¶
Check if there's a pending permission request.
Returns:
| Type | Description |
|---|---|
bool
|
True if a permission is pending. |
Source code in src/voice_agent/sessions/permissions.py
187 188 189 190 191 192 193 194 195 | |
remove_sticky_approval(index)
¶
Remove a sticky approval by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the approval to remove (0-based). |
required |
Returns:
| Type | Description |
|---|---|
StickyApproval | None
|
The removed StickyApproval or None if index invalid. |
Source code in src/voice_agent/sessions/permissions.py
351 352 353 354 355 356 357 358 359 360 361 362 | |
request_permission(tool_name, input_data)
async
¶
Request permission for a tool call.
Auto-approves safe tools and sticky approvals, queues others for user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
input_data
|
dict[str, Any]
|
Input parameters for the tool. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (approved, deny_message). |
Source code in src/voice_agent/sessions/permissions.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
sticky_approve()
¶
Approve pending permission and create sticky rule for similar calls.
Creates a sticky approval rule based on the current pending permission, then approves it.
Returns:
| Type | Description |
|---|---|
StickyApproval | None
|
The created StickyApproval or None if no pending permission. |
Source code in src/voice_agent/sessions/permissions.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
PermissionState
¶
Bases: Enum
State of a pending permission request.
Source code in src/voice_agent/sessions/permissions.py
13 14 15 16 17 18 19 | |
Session
dataclass
¶
A Claude Code session.
Attributes:
| Name | Type | Description |
|---|---|---|
chat_id |
int
|
Telegram chat ID this session belongs to. |
name |
str
|
Session name within the chat. |
cwd |
str
|
Working directory for the session. |
created_at |
datetime
|
When the session was created. |
message_count |
int
|
Number of messages exchanged. |
permission_handler |
PermissionHandler
|
Handler for permission requests. |
sdk_client |
ClaudeSDKClient | None
|
Persistent ClaudeSDKClient instance. |
claude_session_id |
str | None
|
Claude CLI session ID for resume. |
Source code in src/voice_agent/sessions/manager.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
get_status()
¶
Get a human-readable status of this session.
Returns:
| Type | Description |
|---|---|
str
|
Status string. |
Source code in src/voice_agent/sessions/manager.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
SessionInfo
dataclass
¶
Summary info for a session, used in listings.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Session name. |
message_count |
int
|
Number of messages exchanged. |
cwd |
str
|
Working directory. |
is_active |
bool
|
Whether this is the active session. |
Source code in src/voice_agent/sessions/manager.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
SessionManager
¶
Manages Claude Code sessions per chat with multi-session support.
Attributes:
| Name | Type | Description |
|---|---|---|
sessions |
dict[int, dict[str, Session]]
|
Mapping of chat_id to dict of session_name to Session. |
active_sessions |
dict[int, str]
|
Mapping of chat_id to active session name. |
default_cwd |
Default working directory for new sessions. |
|
permission_timeout |
Timeout for permission requests. |
|
storage |
Optional persistent storage for sessions. |
Source code in src/voice_agent/sessions/manager.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
__init__(default_cwd='/code', permission_timeout=300, storage=None)
¶
Initialize the session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_cwd
|
str
|
Default working directory for new sessions. |
'/code'
|
permission_timeout
|
int
|
Timeout in seconds for permission requests. |
300
|
storage
|
SessionStorage | None
|
Optional storage for session persistence. |
None
|
Source code in src/voice_agent/sessions/manager.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
close_session(chat_id, name)
¶
Close a specific session synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to close. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was closed, False if not found. |
Source code in src/voice_agent/sessions/manager.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
close_session_async(chat_id, name)
async
¶
Close a specific session asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to close. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was closed, False if not found. |
Source code in src/voice_agent/sessions/manager.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
create_new(chat_id, cwd=None, name=None)
¶
Create a new session synchronously (closes client in background).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory. |
None
|
name
|
str | None
|
Session name (uses "main" if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The new session. |
Source code in src/voice_agent/sessions/manager.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
create_new_async(chat_id, cwd=None, name=None)
async
¶
Create a new session, replacing any existing one with same name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory. |
None
|
name
|
str | None
|
Session name (uses "main" if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The new session. |
Source code in src/voice_agent/sessions/manager.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
delete_session(chat_id)
¶
Delete the active session synchronously (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was deleted, False if not found. |
Source code in src/voice_agent/sessions/manager.py
752 753 754 755 756 757 758 759 760 761 762 | |
delete_session_async(chat_id)
async
¶
Delete the active session asynchronously (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was deleted, False if not found. |
Source code in src/voice_agent/sessions/manager.py
740 741 742 743 744 745 746 747 748 749 750 | |
generate_session_name(chat_id)
¶
Generate a unique session name for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A unique session name like "session-2", "session-3", etc. |
Source code in src/voice_agent/sessions/manager.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
get(chat_id, name=None)
¶
Get session for a chat if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str | None
|
Session name (uses active session if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session | None
|
Session or None. |
Source code in src/voice_agent/sessions/manager.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
get_active_session_name(chat_id)
¶
Get the name of the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Active session name or None if no sessions. |
Source code in src/voice_agent/sessions/manager.py
363 364 365 366 367 368 369 370 371 372 373 374 | |
get_or_create(chat_id, cwd=None, name=None)
¶
Get existing active session or create new one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory (uses default if not specified). |
None
|
name
|
str | None
|
Session name (uses active session if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The session for this chat. |
Source code in src/voice_agent/sessions/manager.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_status(chat_id)
¶
Get status of the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Status string or None if no session. |
Source code in src/voice_agent/sessions/manager.py
649 650 651 652 653 654 655 656 657 658 659 660 661 | |
has_resumable_session(chat_id)
¶
Check if a chat has a resumable Claude session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if there's a session with a Claude session ID. |
Source code in src/voice_agent/sessions/manager.py
776 777 778 779 780 781 782 783 784 785 786 | |
list_sessions(chat_id)
¶
List all sessions for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
list[SessionInfo]
|
List of SessionInfo objects. |
Source code in src/voice_agent/sessions/manager.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
rename_session(chat_id, old_name, new_name)
¶
Rename a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
old_name
|
str
|
Current session name. |
required |
new_name
|
str
|
New session name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if renamed, False if not found or name already exists. |
Source code in src/voice_agent/sessions/manager.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
send_prompt(chat_id, prompt, resume=True, images=None)
async
¶
Send a prompt to the active session and stream responses.
Uses ClaudeSDKClient for persistent sessions - no token reload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
prompt
|
str
|
The prompt to send. |
required |
resume
|
bool
|
Whether to resume existing session if available. |
True
|
images
|
list[ImageAttachment] | None
|
Optional image attachments for multimodal prompts. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[str]
|
Response chunks from Claude. |
Source code in src/voice_agent/sessions/manager.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
set_claude_session_id(chat_id, session_id)
¶
Set the Claude session ID for resume capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
session_id
|
str | None
|
Claude CLI session ID or None to clear. |
required |
Source code in src/voice_agent/sessions/manager.py
764 765 766 767 768 769 770 771 772 773 774 | |
set_cwd(chat_id, cwd)
¶
Set the working directory for the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str
|
New working directory. |
required |
Returns:
| Type | Description |
|---|---|
Session
|
The updated session. |
Source code in src/voice_agent/sessions/manager.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
set_notify_callback(chat_id, callback)
¶
Set the notification callback for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
callback
|
Any
|
Async function to call for notifications. |
required |
Source code in src/voice_agent/sessions/manager.py
177 178 179 180 181 182 183 184 185 186 187 188 | |
switch_session(chat_id, name)
¶
Switch to a different session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to switch to. |
required |
Returns:
| Type | Description |
|---|---|
Session | None
|
The switched-to session, or None if not found. |
Source code in src/voice_agent/sessions/manager.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
SessionStorage
¶
Persistent storage for session data.
Stores sessions in a JSON file with multi-session support per chat.
Attributes:
| Name | Type | Description |
|---|---|---|
path |
Path to the JSON storage file. |
Source code in src/voice_agent/sessions/storage.py
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 | |
__init__(path='sessions.json')
¶
Initialize storage.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
path
|
str | Path
|
Path to the JSON storage file. |
'sessions.json'
|
Source code in src/voice_agent/sessions/storage.py
96 97 98 99 100 101 102 103 104 | |
delete(chat_id)
¶
Delete all sessions for a chat (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Source code in src/voice_agent/sessions/storage.py
351 352 353 354 355 356 357 | |
delete_chat(chat_id)
¶
Delete all sessions for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if deleted, False if not found. |
Source code in src/voice_agent/sessions/storage.py
293 294 295 296 297 298 299 300 301 302 303 304 305 306 | |
delete_session(chat_id, name)
¶
Delete a specific session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to delete. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if deleted, False if not found. |
Source code in src/voice_agent/sessions/storage.py
239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 | |
get(chat_id)
¶
Get active session for a chat (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
StoredSession | None
|
Active StoredSession or None if not found. |
Source code in src/voice_agent/sessions/storage.py
332 333 334 335 336 337 338 339 340 341 | |
get_active_session(chat_id)
¶
Get the active session for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
StoredSession | None
|
Active StoredSession or None if not found. |
Source code in src/voice_agent/sessions/storage.py
193 194 195 196 197 198 199 200 201 202 203 204 205 | |
get_chat_state(chat_id)
¶
Get stored state for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
ChatStoredState | None
|
ChatStoredState or None if not found. |
Source code in src/voice_agent/sessions/storage.py
167 168 169 170 171 172 173 174 175 176 | |
get_session(chat_id, name)
¶
Get a specific session for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name. |
required |
Returns:
| Type | Description |
|---|---|
StoredSession | None
|
StoredSession or None if not found. |
Source code in src/voice_agent/sessions/storage.py
178 179 180 181 182 183 184 185 186 187 188 189 190 191 | |
list_all()
¶
List all active sessions across all chats (legacy compatibility).
Returns:
| Type | Description |
|---|---|
list[StoredSession]
|
List of active stored sessions. |
Source code in src/voice_agent/sessions/storage.py
359 360 361 362 363 364 365 366 367 368 369 370 | |
list_all_chats()
¶
List all chat IDs with sessions.
Returns:
| Type | Description |
|---|---|
list[int]
|
List of chat IDs. |
Source code in src/voice_agent/sessions/storage.py
322 323 324 325 326 327 328 | |
list_sessions(chat_id)
¶
List all sessions for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
list[StoredSession]
|
List of stored sessions. |
Source code in src/voice_agent/sessions/storage.py
308 309 310 311 312 313 314 315 316 317 318 319 320 | |
rename_session(chat_id, old_name, new_name)
¶
Rename a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
old_name
|
str
|
Current session name. |
required |
new_name
|
str
|
New session name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if renamed, False if not found or name exists. |
Source code in src/voice_agent/sessions/storage.py
265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 | |
save(session)
¶
Save a session (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
StoredSession
|
Session to save. |
required |
Source code in src/voice_agent/sessions/storage.py
343 344 345 346 347 348 349 | |
save_session(session)
¶
Save a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
session
|
StoredSession
|
Session to save. |
required |
Source code in src/voice_agent/sessions/storage.py
207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
set_active_session(chat_id, name)
¶
Set the active session for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to make active. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if successful, False if session not found. |
Source code in src/voice_agent/sessions/storage.py
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 | |
StickyApproval
dataclass
¶
A sticky approval rule that auto-approves matching tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_name |
str
|
Name of the tool to auto-approve. |
pattern |
str | None
|
Optional regex pattern to match against field value. |
field_name |
str | None
|
Field to match pattern against ('command', 'file_path', etc). |
Source code in src/voice_agent/sessions/permissions.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
describe()
¶
Get a human-readable description of this approval.
Returns:
| Type | Description |
|---|---|
str
|
Description string. |
Source code in src/voice_agent/sessions/permissions.py
72 73 74 75 76 77 78 79 80 | |
matches(tool_name, input_data)
¶
Check if this sticky approval matches a tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool being called. |
required |
input_data
|
dict[str, Any]
|
Input parameters for the tool. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tool call matches this sticky approval. |
Source code in src/voice_agent/sessions/permissions.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
StoredSession
dataclass
¶
Serializable session data for persistence.
Attributes:
| Name | Type | Description |
|---|---|---|
chat_id |
int
|
Telegram chat ID. |
name |
str
|
Session name within the chat. |
cwd |
str
|
Working directory. |
created_at |
str
|
ISO format timestamp. |
message_count |
int
|
Number of messages exchanged. |
claude_session_id |
str | None
|
Claude CLI session ID for resume. |
Source code in src/voice_agent/sessions/storage.py
12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 | |
from_dict(data)
classmethod
¶
Create from dictionary.
Source code in src/voice_agent/sessions/storage.py
36 37 38 39 40 41 42 43 44 45 46 | |
to_dict()
¶
Convert to dictionary for JSON serialization.
Source code in src/voice_agent/sessions/storage.py
32 33 34 | |
voice_agent.sessions.manager
¶
Session management for Claude Code SDK.
Manages Claude SDK client instances and their lifecycle.
Session
dataclass
¶
A Claude Code session.
Attributes:
| Name | Type | Description |
|---|---|---|
chat_id |
int
|
Telegram chat ID this session belongs to. |
name |
str
|
Session name within the chat. |
cwd |
str
|
Working directory for the session. |
created_at |
datetime
|
When the session was created. |
message_count |
int
|
Number of messages exchanged. |
permission_handler |
PermissionHandler
|
Handler for permission requests. |
sdk_client |
ClaudeSDKClient | None
|
Persistent ClaudeSDKClient instance. |
claude_session_id |
str | None
|
Claude CLI session ID for resume. |
Source code in src/voice_agent/sessions/manager.py
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
get_status()
¶
Get a human-readable status of this session.
Returns:
| Type | Description |
|---|---|
str
|
Status string. |
Source code in src/voice_agent/sessions/manager.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 | |
SessionInfo
dataclass
¶
Summary info for a session, used in listings.
Attributes:
| Name | Type | Description |
|---|---|---|
name |
str
|
Session name. |
message_count |
int
|
Number of messages exchanged. |
cwd |
str
|
Working directory. |
is_active |
bool
|
Whether this is the active session. |
Source code in src/voice_agent/sessions/manager.py
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 | |
SessionManager
¶
Manages Claude Code sessions per chat with multi-session support.
Attributes:
| Name | Type | Description |
|---|---|---|
sessions |
dict[int, dict[str, Session]]
|
Mapping of chat_id to dict of session_name to Session. |
active_sessions |
dict[int, str]
|
Mapping of chat_id to active session name. |
default_cwd |
Default working directory for new sessions. |
|
permission_timeout |
Timeout for permission requests. |
|
storage |
Optional persistent storage for sessions. |
Source code in src/voice_agent/sessions/manager.py
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 | |
__init__(default_cwd='/code', permission_timeout=300, storage=None)
¶
Initialize the session manager.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
default_cwd
|
str
|
Default working directory for new sessions. |
'/code'
|
permission_timeout
|
int
|
Timeout in seconds for permission requests. |
300
|
storage
|
SessionStorage | None
|
Optional storage for session persistence. |
None
|
Source code in src/voice_agent/sessions/manager.py
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 | |
close_session(chat_id, name)
¶
Close a specific session synchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to close. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was closed, False if not found. |
Source code in src/voice_agent/sessions/manager.py
700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 | |
close_session_async(chat_id, name)
async
¶
Close a specific session asynchronously.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to close. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was closed, False if not found. |
Source code in src/voice_agent/sessions/manager.py
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 | |
create_new(chat_id, cwd=None, name=None)
¶
Create a new session synchronously (closes client in background).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory. |
None
|
name
|
str | None
|
Session name (uses "main" if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The new session. |
Source code in src/voice_agent/sessions/manager.py
282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 | |
create_new_async(chat_id, cwd=None, name=None)
async
¶
Create a new session, replacing any existing one with same name.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory. |
None
|
name
|
str | None
|
Session name (uses "main" if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The new session. |
Source code in src/voice_agent/sessions/manager.py
241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 | |
delete_session(chat_id)
¶
Delete the active session synchronously (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was deleted, False if not found. |
Source code in src/voice_agent/sessions/manager.py
752 753 754 755 756 757 758 759 760 761 762 | |
delete_session_async(chat_id)
async
¶
Delete the active session asynchronously (legacy compatibility).
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if session was deleted, False if not found. |
Source code in src/voice_agent/sessions/manager.py
740 741 742 743 744 745 746 747 748 749 750 | |
generate_session_name(chat_id)
¶
Generate a unique session name for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str
|
A unique session name like "session-2", "session-3", etc. |
Source code in src/voice_agent/sessions/manager.py
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 | |
get(chat_id, name=None)
¶
Get session for a chat if it exists.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str | None
|
Session name (uses active session if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session | None
|
Session or None. |
Source code in src/voice_agent/sessions/manager.py
324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 | |
get_active_session_name(chat_id)
¶
Get the name of the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Active session name or None if no sessions. |
Source code in src/voice_agent/sessions/manager.py
363 364 365 366 367 368 369 370 371 372 373 374 | |
get_or_create(chat_id, cwd=None, name=None)
¶
Get existing active session or create new one.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str | None
|
Working directory (uses default if not specified). |
None
|
name
|
str | None
|
Session name (uses active session if not specified). |
None
|
Returns:
| Type | Description |
|---|---|
Session
|
The session for this chat. |
Source code in src/voice_agent/sessions/manager.py
201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 | |
get_status(chat_id)
¶
Get status of the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
str | None
|
Status string or None if no session. |
Source code in src/voice_agent/sessions/manager.py
649 650 651 652 653 654 655 656 657 658 659 660 661 | |
has_resumable_session(chat_id)
¶
Check if a chat has a resumable Claude session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if there's a session with a Claude session ID. |
Source code in src/voice_agent/sessions/manager.py
776 777 778 779 780 781 782 783 784 785 786 | |
list_sessions(chat_id)
¶
List all sessions for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
Returns:
| Type | Description |
|---|---|
list[SessionInfo]
|
List of SessionInfo objects. |
Source code in src/voice_agent/sessions/manager.py
340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 | |
rename_session(chat_id, old_name, new_name)
¶
Rename a session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
old_name
|
str
|
Current session name. |
required |
new_name
|
str
|
New session name. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if renamed, False if not found or name already exists. |
Source code in src/voice_agent/sessions/manager.py
415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 | |
send_prompt(chat_id, prompt, resume=True, images=None)
async
¶
Send a prompt to the active session and stream responses.
Uses ClaudeSDKClient for persistent sessions - no token reload.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
prompt
|
str
|
The prompt to send. |
required |
resume
|
bool
|
Whether to resume existing session if available. |
True
|
images
|
list[ImageAttachment] | None
|
Optional image attachments for multimodal prompts. |
None
|
Yields:
| Type | Description |
|---|---|
AsyncIterator[str]
|
Response chunks from Claude. |
Source code in src/voice_agent/sessions/manager.py
578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 | |
set_claude_session_id(chat_id, session_id)
¶
Set the Claude session ID for resume capability.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
session_id
|
str | None
|
Claude CLI session ID or None to clear. |
required |
Source code in src/voice_agent/sessions/manager.py
764 765 766 767 768 769 770 771 772 773 774 | |
set_cwd(chat_id, cwd)
¶
Set the working directory for the active session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
cwd
|
str
|
New working directory. |
required |
Returns:
| Type | Description |
|---|---|
Session
|
The updated session. |
Source code in src/voice_agent/sessions/manager.py
447 448 449 450 451 452 453 454 455 456 457 458 459 460 | |
set_notify_callback(chat_id, callback)
¶
Set the notification callback for a chat.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
callback
|
Any
|
Async function to call for notifications. |
required |
Source code in src/voice_agent/sessions/manager.py
177 178 179 180 181 182 183 184 185 186 187 188 | |
switch_session(chat_id, name)
¶
Switch to a different session.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
chat_id
|
int
|
Telegram chat ID. |
required |
name
|
str
|
Session name to switch to. |
required |
Returns:
| Type | Description |
|---|---|
Session | None
|
The switched-to session, or None if not found. |
Source code in src/voice_agent/sessions/manager.py
376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 | |
voice_agent.sessions.permissions
¶
Permission handling for Claude SDK tool calls.
Manages the canUseTool callback flow for requesting user approval.
PendingPermission
dataclass
¶
A permission request waiting for user approval.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_name |
str
|
Name of the tool requesting permission. |
input_data |
dict[str, Any]
|
Input parameters for the tool. |
event |
Event
|
Event to signal when user responds. |
state |
PermissionState
|
Current state of the permission. |
deny_message |
str | None
|
Optional message explaining denial. |
Source code in src/voice_agent/sessions/permissions.py
83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 | |
PermissionHandler
¶
Handles permission requests for Claude SDK tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
pending |
PendingPermission | None
|
Current pending permission, if any. |
timeout |
Seconds to wait for user approval. |
|
notify_callback |
Async callback to notify user of permission request. |
|
sticky_approvals |
list[StickyApproval]
|
List of sticky approval rules for auto-approving. |
Source code in src/voice_agent/sessions/permissions.py
160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 | |
__init__(timeout=300, notify_callback=None)
¶
Initialize the permission handler.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
timeout
|
int
|
Seconds to wait for user approval. |
300
|
notify_callback
|
Callable[[str, dict[str, Any]], Coroutine[Any, Any, None]] | None
|
Async function to notify user of permission request. |
None
|
Source code in src/voice_agent/sessions/permissions.py
170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 | |
approve()
¶
Approve the pending permission.
Returns:
| Type | Description |
|---|---|
bool
|
True if there was a pending permission to approve. |
Source code in src/voice_agent/sessions/permissions.py
276 277 278 279 280 281 282 283 284 285 286 | |
clear_sticky_approvals()
¶
Clear all sticky approvals.
Returns:
| Type | Description |
|---|---|
int
|
Number of approvals cleared. |
Source code in src/voice_agent/sessions/permissions.py
341 342 343 344 345 346 347 348 349 | |
deny(message=None)
¶
Deny the pending permission.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
message
|
str | None
|
Optional message explaining denial. |
None
|
Returns:
| Type | Description |
|---|---|
bool
|
True if there was a pending permission to deny. |
Source code in src/voice_agent/sessions/permissions.py
288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 | |
get_pending_description()
¶
Get a human-readable description of the pending permission.
Returns:
| Type | Description |
|---|---|
str | None
|
Description string or None if no pending permission. |
Source code in src/voice_agent/sessions/permissions.py
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 | |
get_sticky_approvals()
¶
Get all active sticky approvals.
Returns:
| Type | Description |
|---|---|
list[StickyApproval]
|
List of sticky approval rules. |
Source code in src/voice_agent/sessions/permissions.py
333 334 335 336 337 338 339 | |
has_pending()
¶
Check if there's a pending permission request.
Returns:
| Type | Description |
|---|---|
bool
|
True if a permission is pending. |
Source code in src/voice_agent/sessions/permissions.py
187 188 189 190 191 192 193 194 195 | |
remove_sticky_approval(index)
¶
Remove a sticky approval by index.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
index
|
int
|
Index of the approval to remove (0-based). |
required |
Returns:
| Type | Description |
|---|---|
StickyApproval | None
|
The removed StickyApproval or None if index invalid. |
Source code in src/voice_agent/sessions/permissions.py
351 352 353 354 355 356 357 358 359 360 361 362 | |
request_permission(tool_name, input_data)
async
¶
Request permission for a tool call.
Auto-approves safe tools and sticky approvals, queues others for user.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
input_data
|
dict[str, Any]
|
Input parameters for the tool. |
required |
Returns:
| Type | Description |
|---|---|
tuple[bool, str | None]
|
Tuple of (approved, deny_message). |
Source code in src/voice_agent/sessions/permissions.py
235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 | |
sticky_approve()
¶
Approve pending permission and create sticky rule for similar calls.
Creates a sticky approval rule based on the current pending permission, then approves it.
Returns:
| Type | Description |
|---|---|
StickyApproval | None
|
The created StickyApproval or None if no pending permission. |
Source code in src/voice_agent/sessions/permissions.py
304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 | |
PermissionState
¶
Bases: Enum
State of a pending permission request.
Source code in src/voice_agent/sessions/permissions.py
13 14 15 16 17 18 19 | |
StickyApproval
dataclass
¶
A sticky approval rule that auto-approves matching tool calls.
Attributes:
| Name | Type | Description |
|---|---|---|
tool_name |
str
|
Name of the tool to auto-approve. |
pattern |
str | None
|
Optional regex pattern to match against field value. |
field_name |
str | None
|
Field to match pattern against ('command', 'file_path', etc). |
Source code in src/voice_agent/sessions/permissions.py
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 | |
describe()
¶
Get a human-readable description of this approval.
Returns:
| Type | Description |
|---|---|
str
|
Description string. |
Source code in src/voice_agent/sessions/permissions.py
72 73 74 75 76 77 78 79 80 | |
matches(tool_name, input_data)
¶
Check if this sticky approval matches a tool call.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool being called. |
required |
input_data
|
dict[str, Any]
|
Input parameters for the tool. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tool call matches this sticky approval. |
Source code in src/voice_agent/sessions/permissions.py
45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 | |
is_safe_bash_command(command)
¶
Check if a bash command is safe to auto-approve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
command
|
str
|
The bash command string. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the command is safe (read-only). |
Source code in src/voice_agent/sessions/permissions.py
124 125 126 127 128 129 130 131 132 133 134 135 136 137 | |
is_safe_tool_call(tool_name, input_data)
¶
Check if a tool call is safe to auto-approve.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
tool_name
|
str
|
Name of the tool. |
required |
input_data
|
dict[str, Any]
|
Input parameters for the tool. |
required |
Returns:
| Type | Description |
|---|---|
bool
|
True if the tool call is safe. |
Source code in src/voice_agent/sessions/permissions.py
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 | |