@strands-agents/cli 0.1.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/LICENSE +175 -0
- package/NOTICE +1 -0
- package/README.md +576 -0
- package/bin/strands.js +256 -0
- package/dist/src/cli/arguments.js +198 -0
- package/dist/src/cli/arguments.js.map +1 -0
- package/dist/src/cli/invocation.js +124 -0
- package/dist/src/cli/invocation.js.map +1 -0
- package/dist/src/cli/run.js +252 -0
- package/dist/src/cli/run.js.map +1 -0
- package/dist/src/console.js +235 -0
- package/dist/src/console.js.map +1 -0
- package/dist/src/logging.js +103 -0
- package/dist/src/logging.js.map +1 -0
- package/dist/src/main.js +7 -0
- package/dist/src/main.js.map +1 -0
- package/dist/src/tui/acp/projection.js +276 -0
- package/dist/src/tui/acp/projection.js.map +1 -0
- package/dist/src/tui/acp/server.js +263 -0
- package/dist/src/tui/acp/server.js.map +1 -0
- package/dist/src/tui/agent-configuration.js +335 -0
- package/dist/src/tui/agent-configuration.js.map +1 -0
- package/dist/src/tui/agent-setup.js +80 -0
- package/dist/src/tui/agent-setup.js.map +1 -0
- package/dist/src/tui/background/activity.js +273 -0
- package/dist/src/tui/background/activity.js.map +1 -0
- package/dist/src/tui/background/inbox.js +120 -0
- package/dist/src/tui/background/inbox.js.map +1 -0
- package/dist/src/tui/background/tasks.js +98 -0
- package/dist/src/tui/background/tasks.js.map +1 -0
- package/dist/src/tui/builtin-tools.js +65 -0
- package/dist/src/tui/builtin-tools.js.map +1 -0
- package/dist/src/tui/chat/commands.js +98 -0
- package/dist/src/tui/chat/commands.js.map +1 -0
- package/dist/src/tui/chat/controller-helpers.js +62 -0
- package/dist/src/tui/chat/controller-helpers.js.map +1 -0
- package/dist/src/tui/chat/controller.js +1913 -0
- package/dist/src/tui/chat/controller.js.map +1 -0
- package/dist/src/tui/chat/help.js +94 -0
- package/dist/src/tui/chat/help.js.map +1 -0
- package/dist/src/tui/chat/panels.js +418 -0
- package/dist/src/tui/chat/panels.js.map +1 -0
- package/dist/src/tui/chat/projector.js +443 -0
- package/dist/src/tui/chat/projector.js.map +1 -0
- package/dist/src/tui/chat/sdk-projector.js +194 -0
- package/dist/src/tui/chat/sdk-projector.js.map +1 -0
- package/dist/src/tui/chat/shell.js +263 -0
- package/dist/src/tui/chat/shell.js.map +1 -0
- package/dist/src/tui/chat/types.js +2 -0
- package/dist/src/tui/chat/types.js.map +1 -0
- package/dist/src/tui/config.js +497 -0
- package/dist/src/tui/config.js.map +1 -0
- package/dist/src/tui/configuration-instructions.js +14 -0
- package/dist/src/tui/configuration-instructions.js.map +1 -0
- package/dist/src/tui/mcp/config.js +370 -0
- package/dist/src/tui/mcp/config.js.map +1 -0
- package/dist/src/tui/mcp.js +98 -0
- package/dist/src/tui/mcp.js.map +1 -0
- package/dist/src/tui/memory-options.js +10 -0
- package/dist/src/tui/memory-options.js.map +1 -0
- package/dist/src/tui/messaging.js +193 -0
- package/dist/src/tui/messaging.js.map +1 -0
- package/dist/src/tui/model/context.js +13 -0
- package/dist/src/tui/model/context.js.map +1 -0
- package/dist/src/tui/model/display.js +59 -0
- package/dist/src/tui/model/display.js.map +1 -0
- package/dist/src/tui/model/runtime.js +365 -0
- package/dist/src/tui/model/runtime.js.map +1 -0
- package/dist/src/tui/model/selection.js +160 -0
- package/dist/src/tui/model/selection.js.map +1 -0
- package/dist/src/tui/package-version.js +47 -0
- package/dist/src/tui/package-version.js.map +1 -0
- package/dist/src/tui/permissions/file-change-preview.js +182 -0
- package/dist/src/tui/permissions/file-change-preview.js.map +1 -0
- package/dist/src/tui/permissions/policy.js +223 -0
- package/dist/src/tui/permissions/policy.js.map +1 -0
- package/dist/src/tui/permissions/settings.js +21 -0
- package/dist/src/tui/permissions/settings.js.map +1 -0
- package/dist/src/tui/project/acp.js +93 -0
- package/dist/src/tui/project/acp.js.map +1 -0
- package/dist/src/tui/project/archive.js +239 -0
- package/dist/src/tui/project/archive.js.map +1 -0
- package/dist/src/tui/project/configuration-secrets.js +107 -0
- package/dist/src/tui/project/configuration-secrets.js.map +1 -0
- package/dist/src/tui/project/configuration.js +201 -0
- package/dist/src/tui/project/configuration.js.map +1 -0
- package/dist/src/tui/project/export.js +560 -0
- package/dist/src/tui/project/export.js.map +1 -0
- package/dist/src/tui/project/import.js +75 -0
- package/dist/src/tui/project/import.js.map +1 -0
- package/dist/src/tui/project/module-loader.js +157 -0
- package/dist/src/tui/project/module-loader.js.map +1 -0
- package/dist/src/tui/project/packaging.js +53 -0
- package/dist/src/tui/project/packaging.js.map +1 -0
- package/dist/src/tui/project/portable-dependencies.js +34 -0
- package/dist/src/tui/project/portable-dependencies.js.map +1 -0
- package/dist/src/tui/project/portable-mcp.js +118 -0
- package/dist/src/tui/project/portable-mcp.js.map +1 -0
- package/dist/src/tui/project/python.js +414 -0
- package/dist/src/tui/project/python.js.map +1 -0
- package/dist/src/tui/project/run.js +118 -0
- package/dist/src/tui/project/run.js.map +1 -0
- package/dist/src/tui/project/source.js +307 -0
- package/dist/src/tui/project/source.js.map +1 -0
- package/dist/src/tui/project/typescript.js +135 -0
- package/dist/src/tui/project/typescript.js.map +1 -0
- package/dist/src/tui/project/worker.py +731 -0
- package/dist/src/tui/provider/aws-client.js +67 -0
- package/dist/src/tui/provider/aws-client.js.map +1 -0
- package/dist/src/tui/provider/aws-config.js +66 -0
- package/dist/src/tui/provider/aws-config.js.map +1 -0
- package/dist/src/tui/provider/bedrock-catalog.js +87 -0
- package/dist/src/tui/provider/bedrock-catalog.js.map +1 -0
- package/dist/src/tui/provider/discovery.js +323 -0
- package/dist/src/tui/provider/discovery.js.map +1 -0
- package/dist/src/tui/provider/environment.js +138 -0
- package/dist/src/tui/provider/environment.js.map +1 -0
- package/dist/src/tui/provider/packages.js +42 -0
- package/dist/src/tui/provider/packages.js.map +1 -0
- package/dist/src/tui/run.js +257 -0
- package/dist/src/tui/run.js.map +1 -0
- package/dist/src/tui/runtime.js +520 -0
- package/dist/src/tui/runtime.js.map +1 -0
- package/dist/src/tui/session/agent-definition.js +100 -0
- package/dist/src/tui/session/agent-definition.js.map +1 -0
- package/dist/src/tui/session/catalog.js +159 -0
- package/dist/src/tui/session/catalog.js.map +1 -0
- package/dist/src/tui/session/context.js +118 -0
- package/dist/src/tui/session/context.js.map +1 -0
- package/dist/src/tui/session/conversation-helpers.js +46 -0
- package/dist/src/tui/session/conversation-helpers.js.map +1 -0
- package/dist/src/tui/session/conversations.js +456 -0
- package/dist/src/tui/session/conversations.js.map +1 -0
- package/dist/src/tui/session/desktop-profile.js +80 -0
- package/dist/src/tui/session/desktop-profile.js.map +1 -0
- package/dist/src/tui/session/options.js +44 -0
- package/dist/src/tui/session/options.js.map +1 -0
- package/dist/src/tui/session/sessions.js +251 -0
- package/dist/src/tui/session/sessions.js.map +1 -0
- package/dist/src/tui/session/voice.js +353 -0
- package/dist/src/tui/session/voice.js.map +1 -0
- package/dist/src/tui/settings.js +362 -0
- package/dist/src/tui/settings.js.map +1 -0
- package/dist/src/tui/setup/questions.js +277 -0
- package/dist/src/tui/setup/questions.js.map +1 -0
- package/dist/src/tui/skills.js +135 -0
- package/dist/src/tui/skills.js.map +1 -0
- package/dist/src/tui/steering.js +67 -0
- package/dist/src/tui/steering.js.map +1 -0
- package/dist/src/tui/strands-backend.js +368 -0
- package/dist/src/tui/strands-backend.js.map +1 -0
- package/dist/src/tui/stream-presentation.js +138 -0
- package/dist/src/tui/stream-presentation.js.map +1 -0
- package/dist/src/tui/telemetry.js +46 -0
- package/dist/src/tui/telemetry.js.map +1 -0
- package/dist/src/tui/terminal/composer.js +237 -0
- package/dist/src/tui/terminal/composer.js.map +1 -0
- package/dist/src/tui/terminal/directory-picker.js +76 -0
- package/dist/src/tui/terminal/directory-picker.js.map +1 -0
- package/dist/src/tui/terminal/ink.js +94 -0
- package/dist/src/tui/terminal/ink.js.map +1 -0
- package/dist/src/tui/terminal/mouse-input.js +83 -0
- package/dist/src/tui/terminal/mouse-input.js.map +1 -0
- package/dist/src/tui/terminal/open-url.js +21 -0
- package/dist/src/tui/terminal/open-url.js.map +1 -0
- package/dist/src/tui/terminal/process-tree.js +132 -0
- package/dist/src/tui/terminal/process-tree.js.map +1 -0
- package/dist/src/tui/terminal/sanitize.js +71 -0
- package/dist/src/tui/terminal/sanitize.js.map +1 -0
- package/dist/src/tui/terminal/shell-output.js +43 -0
- package/dist/src/tui/terminal/shell-output.js.map +1 -0
- package/dist/src/tui/terminal/terminal.js +39 -0
- package/dist/src/tui/terminal/terminal.js.map +1 -0
- package/dist/src/tui/view/agents-panel.js +21 -0
- package/dist/src/tui/view/agents-panel.js.map +1 -0
- package/dist/src/tui/view/app.js +1068 -0
- package/dist/src/tui/view/app.js.map +1 -0
- package/dist/src/tui/view/chat-root.js +88 -0
- package/dist/src/tui/view/chat-root.js.map +1 -0
- package/dist/src/tui/view/chat-view.js +185 -0
- package/dist/src/tui/view/chat-view.js.map +1 -0
- package/dist/src/tui/view/command-palette.js +18 -0
- package/dist/src/tui/view/command-palette.js.map +1 -0
- package/dist/src/tui/view/custom-theme-editor.js +301 -0
- package/dist/src/tui/view/custom-theme-editor.js.map +1 -0
- package/dist/src/tui/view/export-panel.js +14 -0
- package/dist/src/tui/view/export-panel.js.map +1 -0
- package/dist/src/tui/view/fade-in.js +106 -0
- package/dist/src/tui/view/fade-in.js.map +1 -0
- package/dist/src/tui/view/frog-canvas.js +375 -0
- package/dist/src/tui/view/frog-canvas.js.map +1 -0
- package/dist/src/tui/view/frog-drawing.js +415 -0
- package/dist/src/tui/view/frog-drawing.js.map +1 -0
- package/dist/src/tui/view/frog-easter-egg.js +53 -0
- package/dist/src/tui/view/frog-easter-egg.js.map +1 -0
- package/dist/src/tui/view/frog-intro-renderer.js +634 -0
- package/dist/src/tui/view/frog-intro-renderer.js.map +1 -0
- package/dist/src/tui/view/help-footer.js +36 -0
- package/dist/src/tui/view/help-footer.js.map +1 -0
- package/dist/src/tui/view/interaction.js +349 -0
- package/dist/src/tui/view/interaction.js.map +1 -0
- package/dist/src/tui/view/intro.js +35 -0
- package/dist/src/tui/view/intro.js.map +1 -0
- package/dist/src/tui/view/markdown.js +99 -0
- package/dist/src/tui/view/markdown.js.map +1 -0
- package/dist/src/tui/view/media.js +70 -0
- package/dist/src/tui/view/media.js.map +1 -0
- package/dist/src/tui/view/model-panel.js +65 -0
- package/dist/src/tui/view/model-panel.js.map +1 -0
- package/dist/src/tui/view/panel-components.js +17 -0
- package/dist/src/tui/view/panel-components.js.map +1 -0
- package/dist/src/tui/view/panels.js +129 -0
- package/dist/src/tui/view/panels.js.map +1 -0
- package/dist/src/tui/view/presentation.js +382 -0
- package/dist/src/tui/view/presentation.js.map +1 -0
- package/dist/src/tui/view/prompt-editor.js +74 -0
- package/dist/src/tui/view/prompt-editor.js.map +1 -0
- package/dist/src/tui/view/provider-list.js +14 -0
- package/dist/src/tui/view/provider-list.js.map +1 -0
- package/dist/src/tui/view/sessions-panel.js +15 -0
- package/dist/src/tui/view/sessions-panel.js.map +1 -0
- package/dist/src/tui/view/settings-panel.js +146 -0
- package/dist/src/tui/view/settings-panel.js.map +1 -0
- package/dist/src/tui/view/setup-question.js +90 -0
- package/dist/src/tui/view/setup-question.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/brand.js +18 -0
- package/dist/src/tui/view/setup-wizard/brand.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/index.js +1557 -0
- package/dist/src/tui/view/setup-wizard/index.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/opening-menu.js +88 -0
- package/dist/src/tui/view/setup-wizard/opening-menu.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/path-completion.js +53 -0
- package/dist/src/tui/view/setup-wizard/path-completion.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/profile-fields.js +29 -0
- package/dist/src/tui/view/setup-wizard/profile-fields.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/progress.js +42 -0
- package/dist/src/tui/view/setup-wizard/progress.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/providers.js +344 -0
- package/dist/src/tui/view/setup-wizard/providers.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/settings-panel.js +74 -0
- package/dist/src/tui/view/setup-wizard/settings-panel.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/steps.js +544 -0
- package/dist/src/tui/view/setup-wizard/steps.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/types.js +2 -0
- package/dist/src/tui/view/setup-wizard/types.js.map +1 -0
- package/dist/src/tui/view/setup-wizard/use-provider-discovery.js +145 -0
- package/dist/src/tui/view/setup-wizard/use-provider-discovery.js.map +1 -0
- package/dist/src/tui/view/startup-view.js +42 -0
- package/dist/src/tui/view/startup-view.js.map +1 -0
- package/dist/src/tui/view/text-input.js +33 -0
- package/dist/src/tui/view/text-input.js.map +1 -0
- package/dist/src/tui/view/theme-detection.js +45 -0
- package/dist/src/tui/view/theme-detection.js.map +1 -0
- package/dist/src/tui/view/theme.js +127 -0
- package/dist/src/tui/view/theme.js.map +1 -0
- package/dist/src/tui/view/transcript-layout.js +60 -0
- package/dist/src/tui/view/transcript-layout.js.map +1 -0
- package/dist/src/tui/view/transcript-turn.js +135 -0
- package/dist/src/tui/view/transcript-turn.js.map +1 -0
- package/dist/src/tui/view/transcript.js +138 -0
- package/dist/src/tui/view/transcript.js.map +1 -0
- package/dist/src/tui/view/use-party-frame.js +15 -0
- package/dist/src/tui/view/use-party-frame.js.map +1 -0
- package/dist/src/tui/view/use-spinner.js +14 -0
- package/dist/src/tui/view/use-spinner.js.map +1 -0
- package/dist/src/tui/voice/session.js +515 -0
- package/dist/src/tui/voice/session.js.map +1 -0
- package/dist/src/tui/workspace/sandbox.js +156 -0
- package/dist/src/tui/workspace/sandbox.js.map +1 -0
- package/dist/src/tui/workspace/trust.js +147 -0
- package/dist/src/tui/workspace/trust.js.map +1 -0
- package/dist/src/usage.js +187 -0
- package/dist/src/usage.js.map +1 -0
- package/package.json +97 -0
- package/src/tui/voice/sidecar.py +606 -0
|
@@ -0,0 +1,606 @@
|
|
|
1
|
+
# /// script
|
|
2
|
+
# requires-python = ">=3.12,<3.14"
|
|
3
|
+
# dependencies = [
|
|
4
|
+
# "strands-agents[bidi,bidi-aec,bidi-pyaudio] @ git+https://github.com/strands-agents/harness-sdk.git@647128383ea9ba4598d3cf47e376642091d339d9#subdirectory=strands-py",
|
|
5
|
+
# ]
|
|
6
|
+
# ///
|
|
7
|
+
|
|
8
|
+
"""Nova Sonic audio sidecar for the Strands CLI voice runtime."""
|
|
9
|
+
|
|
10
|
+
from __future__ import annotations
|
|
11
|
+
|
|
12
|
+
import argparse
|
|
13
|
+
import asyncio
|
|
14
|
+
import base64
|
|
15
|
+
import json
|
|
16
|
+
import logging
|
|
17
|
+
import math
|
|
18
|
+
import os
|
|
19
|
+
import signal
|
|
20
|
+
import sys
|
|
21
|
+
import time
|
|
22
|
+
from array import array
|
|
23
|
+
from pathlib import Path
|
|
24
|
+
from typing import Any
|
|
25
|
+
|
|
26
|
+
import boto3
|
|
27
|
+
import pyaudio # pyright: ignore[reportMissingModuleSource]
|
|
28
|
+
from strands import tool
|
|
29
|
+
from strands.experimental.bidi import (
|
|
30
|
+
AudioProcessorConfig,
|
|
31
|
+
BidiAgent,
|
|
32
|
+
BidiAudioInputEvent,
|
|
33
|
+
BidiAudioIO,
|
|
34
|
+
BidiAudioStreamEvent,
|
|
35
|
+
BidiConnectionCloseEvent,
|
|
36
|
+
BidiConnectionRestartEvent,
|
|
37
|
+
BidiConnectionStartEvent,
|
|
38
|
+
BidiErrorEvent,
|
|
39
|
+
BidiInterruptionEvent,
|
|
40
|
+
BidiResponseCompleteEvent,
|
|
41
|
+
BidiResponseStartEvent,
|
|
42
|
+
BidiTextInputEvent,
|
|
43
|
+
BidiTranscriptStreamEvent,
|
|
44
|
+
BidiUsageEvent,
|
|
45
|
+
)
|
|
46
|
+
from strands.experimental.bidi.models import BidiNovaSonicModel
|
|
47
|
+
from strands.experimental.bidi.types.events import BidiOutputEvent
|
|
48
|
+
from strands.experimental.bidi.types.io import BidiInput, BidiOutput
|
|
49
|
+
from strands.experimental.hooks.events import BidiAfterToolCallEvent, BidiBeforeToolCallEvent
|
|
50
|
+
from strands.hooks import HookProvider
|
|
51
|
+
|
|
52
|
+
DEFAULT_MODEL = "amazon.nova-2-sonic-v1:0"
|
|
53
|
+
DEFAULT_ENDPOINTING_SENSITIVITY = "LOW"
|
|
54
|
+
DEFAULT_SYSTEM_PROMPT = """
|
|
55
|
+
You are Strands harness, a concise coding assistant in a live voice conversation.
|
|
56
|
+
Keep spoken answers short unless the user asks for detail.
|
|
57
|
+
Use current_working_directory whenever the user asks where you are working.
|
|
58
|
+
The user may speak over you or send typed text during the conversation.
|
|
59
|
+
""".strip()
|
|
60
|
+
SIDECAR_SYSTEM_PROMPT = """
|
|
61
|
+
You are a speech transcription transport for a separate coding agent.
|
|
62
|
+
Listen carefully to the user and keep each response to one short acknowledgement.
|
|
63
|
+
Do not answer questions, use tools, or attempt coding work.
|
|
64
|
+
""".strip()
|
|
65
|
+
SPEAK_PROMPT = """
|
|
66
|
+
Read the coding assistant response below aloud naturally.
|
|
67
|
+
Preserve its meaning and do not add commentary.
|
|
68
|
+
Skip markdown punctuation, URLs, and code syntax that would be awkward to speak.
|
|
69
|
+
|
|
70
|
+
<coding_assistant_response>
|
|
71
|
+
{text}
|
|
72
|
+
</coding_assistant_response>
|
|
73
|
+
""".strip()
|
|
74
|
+
INPUT_LEVEL_FLOOR_DB = -60.0
|
|
75
|
+
INPUT_LEVEL_INTERVAL_SECONDS = 0.08
|
|
76
|
+
|
|
77
|
+
|
|
78
|
+
def emit(event_type: str, **payload: Any) -> None:
|
|
79
|
+
print(json.dumps({"type": event_type, **payload}, default=str, separators=(",", ":")), flush=True)
|
|
80
|
+
|
|
81
|
+
|
|
82
|
+
@tool
|
|
83
|
+
def current_working_directory() -> str:
|
|
84
|
+
"""Return the absolute working directory for this voice session."""
|
|
85
|
+
return str(Path.cwd())
|
|
86
|
+
|
|
87
|
+
|
|
88
|
+
class TypedTextInput(BidiInput):
|
|
89
|
+
"""Read optional text input while microphone streaming remains active."""
|
|
90
|
+
|
|
91
|
+
def __init__(self) -> None:
|
|
92
|
+
self._lines: asyncio.Queue[str] = asyncio.Queue()
|
|
93
|
+
self._loop: asyncio.AbstractEventLoop | None = None
|
|
94
|
+
self._file_descriptor = sys.stdin.fileno()
|
|
95
|
+
|
|
96
|
+
async def start(self, agent: BidiAgent) -> None:
|
|
97
|
+
del agent
|
|
98
|
+
self._loop = asyncio.get_running_loop()
|
|
99
|
+
self._loop.add_reader(self._file_descriptor, self._read_line)
|
|
100
|
+
print("text> ", end="", file=sys.stderr, flush=True)
|
|
101
|
+
|
|
102
|
+
async def stop(self) -> None:
|
|
103
|
+
if self._loop is not None:
|
|
104
|
+
self._loop.remove_reader(self._file_descriptor)
|
|
105
|
+
self._loop = None
|
|
106
|
+
|
|
107
|
+
async def __call__(self) -> BidiTextInputEvent:
|
|
108
|
+
text = (await self._lines.get()).strip()
|
|
109
|
+
if not text:
|
|
110
|
+
return await self()
|
|
111
|
+
print("text> ", end="", file=sys.stderr, flush=True)
|
|
112
|
+
return BidiTextInputEvent(text=text)
|
|
113
|
+
|
|
114
|
+
def _read_line(self) -> None:
|
|
115
|
+
line = sys.stdin.readline()
|
|
116
|
+
if line:
|
|
117
|
+
self._lines.put_nowait(line)
|
|
118
|
+
|
|
119
|
+
|
|
120
|
+
class MutableAudioInput(BidiInput):
|
|
121
|
+
"""Replace captured audio with silence while the sidecar is muted."""
|
|
122
|
+
|
|
123
|
+
def __init__(self, input_: BidiInput, report_levels: bool) -> None:
|
|
124
|
+
self._input = input_
|
|
125
|
+
self._muted = False
|
|
126
|
+
self._report_levels = report_levels
|
|
127
|
+
self._smoothed_level = 0.0
|
|
128
|
+
self._last_level_emit = 0.0
|
|
129
|
+
|
|
130
|
+
async def start(self, agent: BidiAgent) -> None:
|
|
131
|
+
await self._input.start(agent)
|
|
132
|
+
|
|
133
|
+
async def stop(self) -> None:
|
|
134
|
+
await self._input.stop()
|
|
135
|
+
|
|
136
|
+
async def __call__(self) -> BidiAudioInputEvent:
|
|
137
|
+
event = await self._input()
|
|
138
|
+
if not isinstance(event, BidiAudioInputEvent):
|
|
139
|
+
raise TypeError(f"Expected audio input, received {type(event).__name__}.")
|
|
140
|
+
audio = base64.b64decode(event.audio)
|
|
141
|
+
self._report_input_level(audio)
|
|
142
|
+
if not self._muted:
|
|
143
|
+
return event
|
|
144
|
+
return BidiAudioInputEvent(
|
|
145
|
+
audio=base64.b64encode(bytes(len(audio))).decode("utf-8"),
|
|
146
|
+
format=event.format,
|
|
147
|
+
sample_rate=event.sample_rate,
|
|
148
|
+
channels=event.channels,
|
|
149
|
+
)
|
|
150
|
+
|
|
151
|
+
def set_muted(self, muted: bool) -> None:
|
|
152
|
+
self._muted = muted
|
|
153
|
+
if muted:
|
|
154
|
+
self._smoothed_level = 0.0
|
|
155
|
+
self._emit_input_level(0.0, INPUT_LEVEL_FLOOR_DB)
|
|
156
|
+
|
|
157
|
+
def _report_input_level(self, audio: bytes) -> None:
|
|
158
|
+
if not self._report_levels or self._muted:
|
|
159
|
+
return
|
|
160
|
+
now = time.monotonic()
|
|
161
|
+
if now - self._last_level_emit < INPUT_LEVEL_INTERVAL_SECONDS:
|
|
162
|
+
return
|
|
163
|
+
self._last_level_emit = now
|
|
164
|
+
if len(audio) < 2:
|
|
165
|
+
self._smoothed_level = 0.0
|
|
166
|
+
self._emit_input_level(0.0, INPUT_LEVEL_FLOOR_DB)
|
|
167
|
+
return
|
|
168
|
+
|
|
169
|
+
samples = array("h")
|
|
170
|
+
samples.frombytes(audio[: len(audio) - (len(audio) % 2)])
|
|
171
|
+
if sys.byteorder != "little":
|
|
172
|
+
samples.byteswap()
|
|
173
|
+
|
|
174
|
+
rms = math.sqrt(sum(sample * sample for sample in samples) / len(samples)) / 32768.0
|
|
175
|
+
db = max(INPUT_LEVEL_FLOOR_DB, 20.0 * math.log10(max(rms, 1e-6)))
|
|
176
|
+
level = (db - INPUT_LEVEL_FLOOR_DB) / -INPUT_LEVEL_FLOOR_DB
|
|
177
|
+
smoothing = 0.45 if level > self._smoothed_level else 0.2
|
|
178
|
+
self._smoothed_level += (level - self._smoothed_level) * smoothing
|
|
179
|
+
smoothed_db = INPUT_LEVEL_FLOOR_DB + self._smoothed_level * -INPUT_LEVEL_FLOOR_DB
|
|
180
|
+
self._emit_input_level(self._smoothed_level, smoothed_db)
|
|
181
|
+
|
|
182
|
+
def _emit_input_level(self, level: float, db: float) -> None:
|
|
183
|
+
if self._report_levels:
|
|
184
|
+
emit("input_level", level=round(level, 3), db=round(db, 1))
|
|
185
|
+
|
|
186
|
+
|
|
187
|
+
class SidecarControl:
|
|
188
|
+
"""Read newline-delimited control messages from the Node parent process."""
|
|
189
|
+
|
|
190
|
+
def __init__(self) -> None:
|
|
191
|
+
self._commands: asyncio.Queue[dict[str, Any]] = asyncio.Queue()
|
|
192
|
+
self._loop: asyncio.AbstractEventLoop | None = None
|
|
193
|
+
self._file_descriptor = sys.stdin.fileno()
|
|
194
|
+
|
|
195
|
+
async def start(self) -> None:
|
|
196
|
+
self._loop = asyncio.get_running_loop()
|
|
197
|
+
self._loop.add_reader(self._file_descriptor, self._read_line)
|
|
198
|
+
|
|
199
|
+
async def stop(self) -> None:
|
|
200
|
+
if self._loop is not None:
|
|
201
|
+
self._loop.remove_reader(self._file_descriptor)
|
|
202
|
+
self._loop = None
|
|
203
|
+
|
|
204
|
+
async def next(self) -> dict[str, Any]:
|
|
205
|
+
return await self._commands.get()
|
|
206
|
+
|
|
207
|
+
def _read_line(self) -> None:
|
|
208
|
+
line = sys.stdin.readline()
|
|
209
|
+
if not line:
|
|
210
|
+
self._commands.put_nowait({"type": "stop", "reason": "stdin_closed"})
|
|
211
|
+
return
|
|
212
|
+
try:
|
|
213
|
+
command = json.loads(line)
|
|
214
|
+
except json.JSONDecodeError as error:
|
|
215
|
+
emit("control_error", message=str(error))
|
|
216
|
+
return
|
|
217
|
+
if isinstance(command, dict):
|
|
218
|
+
self._commands.put_nowait(command)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
class JsonEventOutput(BidiOutput):
|
|
222
|
+
"""Project useful Bidi events to JSON without dumping audio payloads."""
|
|
223
|
+
|
|
224
|
+
def __init__(self) -> None:
|
|
225
|
+
self._audio_started = False
|
|
226
|
+
self._last_usage_total = -100
|
|
227
|
+
|
|
228
|
+
async def __call__(self, event: BidiOutputEvent) -> None:
|
|
229
|
+
if isinstance(event, BidiConnectionStartEvent):
|
|
230
|
+
emit("connection_start", connection_id=event.connection_id, model=event.model)
|
|
231
|
+
elif isinstance(event, BidiConnectionRestartEvent):
|
|
232
|
+
emit("connection_restart", error=str(event.timeout_error))
|
|
233
|
+
elif isinstance(event, BidiResponseStartEvent):
|
|
234
|
+
self._audio_started = False
|
|
235
|
+
emit("response_start", response_id=event.response_id)
|
|
236
|
+
elif isinstance(event, BidiAudioStreamEvent):
|
|
237
|
+
if not self._audio_started:
|
|
238
|
+
self._audio_started = True
|
|
239
|
+
emit(
|
|
240
|
+
"audio_output_start",
|
|
241
|
+
format=event.format,
|
|
242
|
+
sample_rate=event.sample_rate,
|
|
243
|
+
channels=event.channels,
|
|
244
|
+
)
|
|
245
|
+
elif isinstance(event, BidiTranscriptStreamEvent):
|
|
246
|
+
emit(
|
|
247
|
+
"transcript",
|
|
248
|
+
role=event.role,
|
|
249
|
+
text=event.text,
|
|
250
|
+
is_final=event.is_final,
|
|
251
|
+
current_transcript=event.current_transcript,
|
|
252
|
+
)
|
|
253
|
+
elif isinstance(event, BidiInterruptionEvent):
|
|
254
|
+
self._audio_started = False
|
|
255
|
+
emit("interruption", reason=event.reason)
|
|
256
|
+
elif isinstance(event, BidiResponseCompleteEvent):
|
|
257
|
+
self._audio_started = False
|
|
258
|
+
emit("response_complete", response_id=event.response_id, stop_reason=event.stop_reason)
|
|
259
|
+
elif isinstance(event, BidiUsageEvent):
|
|
260
|
+
if event.total_tokens - self._last_usage_total >= 100:
|
|
261
|
+
self._last_usage_total = event.total_tokens
|
|
262
|
+
emit(
|
|
263
|
+
"usage",
|
|
264
|
+
input_tokens=event.input_tokens,
|
|
265
|
+
output_tokens=event.output_tokens,
|
|
266
|
+
total_tokens=event.total_tokens,
|
|
267
|
+
)
|
|
268
|
+
elif isinstance(event, BidiConnectionCloseEvent):
|
|
269
|
+
emit("connection_close", connection_id=event.connection_id, reason=event.reason)
|
|
270
|
+
elif isinstance(event, BidiErrorEvent):
|
|
271
|
+
emit("error", code=event.code, message=event.message, details=event.details)
|
|
272
|
+
|
|
273
|
+
|
|
274
|
+
class ControlledAudioOutput(BidiOutput):
|
|
275
|
+
"""Play only responses explicitly requested by the Node parent process."""
|
|
276
|
+
|
|
277
|
+
def __init__(self, output: BidiOutput) -> None:
|
|
278
|
+
self._output = output
|
|
279
|
+
self._response_id: str | None = None
|
|
280
|
+
self._play_current = False
|
|
281
|
+
self._audio_started = False
|
|
282
|
+
self._armed = False
|
|
283
|
+
self._idle = asyncio.Event()
|
|
284
|
+
self._idle.set()
|
|
285
|
+
|
|
286
|
+
async def start(self, agent: BidiAgent) -> None:
|
|
287
|
+
await self._output.start(agent)
|
|
288
|
+
|
|
289
|
+
async def stop(self) -> None:
|
|
290
|
+
await self._output.stop()
|
|
291
|
+
|
|
292
|
+
async def __call__(self, event: BidiOutputEvent) -> None:
|
|
293
|
+
if isinstance(event, BidiResponseStartEvent):
|
|
294
|
+
if not self._idle.is_set() and event.response_id == self._response_id:
|
|
295
|
+
return
|
|
296
|
+
self._response_id = event.response_id
|
|
297
|
+
self._idle.clear()
|
|
298
|
+
self._play_current = self._armed
|
|
299
|
+
self._armed = False
|
|
300
|
+
self._audio_started = False
|
|
301
|
+
return
|
|
302
|
+
if isinstance(event, BidiAudioStreamEvent):
|
|
303
|
+
if not self._play_current:
|
|
304
|
+
return
|
|
305
|
+
if not self._audio_started:
|
|
306
|
+
self._audio_started = True
|
|
307
|
+
emit("speech_output_start")
|
|
308
|
+
await self._output(event)
|
|
309
|
+
return
|
|
310
|
+
if isinstance(event, BidiTranscriptStreamEvent):
|
|
311
|
+
if event.role == "assistant" and event.is_final and not self._idle.is_set():
|
|
312
|
+
if self._play_current:
|
|
313
|
+
emit("speech_output_complete", response_id=self._response_id)
|
|
314
|
+
self._finish_response()
|
|
315
|
+
return
|
|
316
|
+
if isinstance(event, BidiInterruptionEvent):
|
|
317
|
+
await self._output(event)
|
|
318
|
+
if self._play_current:
|
|
319
|
+
emit("speech_output_interrupted", reason=event.reason)
|
|
320
|
+
self._finish_response()
|
|
321
|
+
return
|
|
322
|
+
if isinstance(event, BidiResponseCompleteEvent):
|
|
323
|
+
if self._play_current:
|
|
324
|
+
emit("speech_output_complete", response_id=event.response_id)
|
|
325
|
+
self._finish_response()
|
|
326
|
+
|
|
327
|
+
async def wait_until_idle(self) -> None:
|
|
328
|
+
await self._idle.wait()
|
|
329
|
+
|
|
330
|
+
def arm_next_response(self) -> None:
|
|
331
|
+
self._armed = True
|
|
332
|
+
|
|
333
|
+
async def stop_speaking(self) -> None:
|
|
334
|
+
was_speaking = self._play_current or self._armed
|
|
335
|
+
await self._output(BidiInterruptionEvent(reason="user_speech"))
|
|
336
|
+
self._play_current = False
|
|
337
|
+
self._audio_started = False
|
|
338
|
+
self._armed = False
|
|
339
|
+
if was_speaking:
|
|
340
|
+
emit("speech_output_stopped")
|
|
341
|
+
|
|
342
|
+
def _finish_response(self) -> None:
|
|
343
|
+
self._response_id = None
|
|
344
|
+
self._play_current = False
|
|
345
|
+
self._audio_started = False
|
|
346
|
+
self._armed = False
|
|
347
|
+
self._idle.set()
|
|
348
|
+
|
|
349
|
+
|
|
350
|
+
class ToolEventHooks(HookProvider):
|
|
351
|
+
"""Emit definitive tool lifecycle events from hooks."""
|
|
352
|
+
|
|
353
|
+
def register_hooks(self, registry: Any, **_: Any) -> None:
|
|
354
|
+
registry.add_callback(BidiBeforeToolCallEvent, self._before_tool)
|
|
355
|
+
registry.add_callback(BidiAfterToolCallEvent, self._after_tool)
|
|
356
|
+
|
|
357
|
+
def _before_tool(self, event: BidiBeforeToolCallEvent) -> None:
|
|
358
|
+
emit(
|
|
359
|
+
"tool_start",
|
|
360
|
+
tool_use_id=event.tool_use.get("toolUseId"),
|
|
361
|
+
name=event.tool_use.get("name"),
|
|
362
|
+
input=event.tool_use.get("input", {}),
|
|
363
|
+
)
|
|
364
|
+
|
|
365
|
+
def _after_tool(self, event: BidiAfterToolCallEvent) -> None:
|
|
366
|
+
emit(
|
|
367
|
+
"tool_finish",
|
|
368
|
+
tool_use_id=event.tool_use.get("toolUseId"),
|
|
369
|
+
name=event.tool_use.get("name"),
|
|
370
|
+
result=event.result,
|
|
371
|
+
)
|
|
372
|
+
|
|
373
|
+
|
|
374
|
+
def parse_args() -> argparse.Namespace:
|
|
375
|
+
parser = argparse.ArgumentParser(description="Exercise Strands Bidi with Nova Sonic and local audio.")
|
|
376
|
+
parser.add_argument("--model", default=DEFAULT_MODEL)
|
|
377
|
+
parser.add_argument("--region", help="AWS region; defaults to the active AWS configuration.")
|
|
378
|
+
parser.add_argument("--voice", default="tiffany")
|
|
379
|
+
parser.add_argument(
|
|
380
|
+
"--endpointing-sensitivity",
|
|
381
|
+
choices=["HIGH", "MEDIUM", "LOW"],
|
|
382
|
+
default=DEFAULT_ENDPOINTING_SENSITIVITY,
|
|
383
|
+
)
|
|
384
|
+
parser.add_argument("--input-device", type=int)
|
|
385
|
+
parser.add_argument("--output-device", type=int)
|
|
386
|
+
parser.add_argument("--list-devices", action="store_true")
|
|
387
|
+
parser.add_argument("--sidecar", action="store_true", help="Run as the Strands CLI transcription sidecar.")
|
|
388
|
+
parser.add_argument(
|
|
389
|
+
"--check", action="store_true", help="Validate credentials and audio devices without connecting."
|
|
390
|
+
)
|
|
391
|
+
return parser.parse_args()
|
|
392
|
+
|
|
393
|
+
|
|
394
|
+
def audio_devices() -> list[dict[str, Any]]:
|
|
395
|
+
audio = pyaudio.PyAudio()
|
|
396
|
+
try:
|
|
397
|
+
devices = []
|
|
398
|
+
for index in range(audio.get_device_count()):
|
|
399
|
+
info = audio.get_device_info_by_index(index)
|
|
400
|
+
devices.append(
|
|
401
|
+
{
|
|
402
|
+
"index": index,
|
|
403
|
+
"name": info.get("name"),
|
|
404
|
+
"input_channels": int(info.get("maxInputChannels", 0)),
|
|
405
|
+
"output_channels": int(info.get("maxOutputChannels", 0)),
|
|
406
|
+
"default_sample_rate": int(info.get("defaultSampleRate", 0)),
|
|
407
|
+
}
|
|
408
|
+
)
|
|
409
|
+
return devices
|
|
410
|
+
finally:
|
|
411
|
+
audio.terminate()
|
|
412
|
+
|
|
413
|
+
|
|
414
|
+
def preflight(args: argparse.Namespace) -> tuple[str, list[dict[str, Any]]]:
|
|
415
|
+
credentials = boto3.Session().get_credentials()
|
|
416
|
+
if credentials is None:
|
|
417
|
+
raise RuntimeError("AWS credentials were not found.")
|
|
418
|
+
|
|
419
|
+
region: str = (
|
|
420
|
+
args.region
|
|
421
|
+
or boto3.Session().region_name
|
|
422
|
+
or os.getenv("AWS_REGION")
|
|
423
|
+
or os.getenv("AWS_DEFAULT_REGION")
|
|
424
|
+
or "us-east-1"
|
|
425
|
+
)
|
|
426
|
+
devices = audio_devices()
|
|
427
|
+
if not any(device["input_channels"] > 0 for device in devices):
|
|
428
|
+
raise RuntimeError("No microphone input device was found.")
|
|
429
|
+
if not args.sidecar and not any(device["output_channels"] > 0 for device in devices):
|
|
430
|
+
raise RuntimeError("No speaker output device was found.")
|
|
431
|
+
|
|
432
|
+
emit(
|
|
433
|
+
"preflight",
|
|
434
|
+
ok=True,
|
|
435
|
+
model=args.model,
|
|
436
|
+
region=region,
|
|
437
|
+
cwd=str(Path.cwd()),
|
|
438
|
+
input_device=args.input_device,
|
|
439
|
+
output_device=args.output_device,
|
|
440
|
+
device_count=len(devices),
|
|
441
|
+
)
|
|
442
|
+
return region, devices
|
|
443
|
+
|
|
444
|
+
|
|
445
|
+
async def run_voice(args: argparse.Namespace, region: str) -> None:
|
|
446
|
+
model = BidiNovaSonicModel(
|
|
447
|
+
model_id=args.model,
|
|
448
|
+
provider_config={
|
|
449
|
+
"audio": {"voice": args.voice},
|
|
450
|
+
"turn_detection": {"endpointingSensitivity": args.endpointing_sensitivity},
|
|
451
|
+
},
|
|
452
|
+
client_config={"region": region},
|
|
453
|
+
)
|
|
454
|
+
audio = BidiAudioIO(
|
|
455
|
+
processor=AudioProcessorConfig(),
|
|
456
|
+
input_device_index=args.input_device,
|
|
457
|
+
output_device_index=args.output_device,
|
|
458
|
+
)
|
|
459
|
+
agent = BidiAgent(
|
|
460
|
+
model=model,
|
|
461
|
+
tools=[] if args.sidecar else [current_working_directory],
|
|
462
|
+
system_prompt=SIDECAR_SYSTEM_PROMPT if args.sidecar else DEFAULT_SYSTEM_PROMPT,
|
|
463
|
+
hooks=[] if args.sidecar else [ToolEventHooks()],
|
|
464
|
+
name="Strands Voice",
|
|
465
|
+
)
|
|
466
|
+
audio_input = MutableAudioInput(audio.input(), report_levels=args.sidecar)
|
|
467
|
+
text_input = TypedTextInput()
|
|
468
|
+
audio_output = audio.output()
|
|
469
|
+
controlled_audio_output = ControlledAudioOutput(audio_output)
|
|
470
|
+
json_output = JsonEventOutput()
|
|
471
|
+
sidecar_control = SidecarControl() if args.sidecar else None
|
|
472
|
+
inputs = [audio_input] if args.sidecar else [audio_input, text_input]
|
|
473
|
+
outputs = [controlled_audio_output if args.sidecar else audio_output, json_output]
|
|
474
|
+
stop_requested = asyncio.Event()
|
|
475
|
+
loop = asyncio.get_running_loop()
|
|
476
|
+
|
|
477
|
+
def request_stop(reason: str) -> None:
|
|
478
|
+
if stop_requested.is_set():
|
|
479
|
+
return
|
|
480
|
+
emit("session_stop_requested", reason=reason)
|
|
481
|
+
stop_requested.set()
|
|
482
|
+
|
|
483
|
+
for signal_number in (signal.SIGINT, signal.SIGTERM):
|
|
484
|
+
loop.add_signal_handler(signal_number, request_stop, signal.Signals(signal_number).name.lower())
|
|
485
|
+
|
|
486
|
+
async def run_input(input_: BidiInput) -> None:
|
|
487
|
+
while True:
|
|
488
|
+
await agent.send(await input_())
|
|
489
|
+
|
|
490
|
+
async def run_output() -> None:
|
|
491
|
+
async for event in agent.receive():
|
|
492
|
+
await asyncio.gather(*(output(event) for output in outputs))
|
|
493
|
+
request_stop("connection_closed")
|
|
494
|
+
|
|
495
|
+
async def run_controls() -> None:
|
|
496
|
+
assert sidecar_control is not None
|
|
497
|
+
while True:
|
|
498
|
+
command = await sidecar_control.next()
|
|
499
|
+
command_type = command.get("type")
|
|
500
|
+
if command_type == "set_muted":
|
|
501
|
+
muted = bool(command.get("muted"))
|
|
502
|
+
audio_input.set_muted(muted)
|
|
503
|
+
emit("mute_changed", muted=muted)
|
|
504
|
+
elif command_type == "speak":
|
|
505
|
+
text = str(command.get("text", "")).strip()
|
|
506
|
+
if not text:
|
|
507
|
+
emit("control_error", message="The speak command requires non-empty text.")
|
|
508
|
+
continue
|
|
509
|
+
await controlled_audio_output.wait_until_idle()
|
|
510
|
+
controlled_audio_output.arm_next_response()
|
|
511
|
+
await agent.send(BidiTextInputEvent(text=SPEAK_PROMPT.format(text=text)))
|
|
512
|
+
emit("speech_output_queued")
|
|
513
|
+
elif command_type == "stop_speaking":
|
|
514
|
+
await controlled_audio_output.stop_speaking()
|
|
515
|
+
elif command_type == "stop":
|
|
516
|
+
request_stop(str(command.get("reason", "parent_request")))
|
|
517
|
+
return
|
|
518
|
+
else:
|
|
519
|
+
emit("control_error", message=f"Unknown control message: {command_type}")
|
|
520
|
+
|
|
521
|
+
emit(
|
|
522
|
+
"session_start",
|
|
523
|
+
model=args.model,
|
|
524
|
+
region=region,
|
|
525
|
+
voice=args.voice,
|
|
526
|
+
endpointing_sensitivity=args.endpointing_sensitivity,
|
|
527
|
+
)
|
|
528
|
+
if not args.sidecar:
|
|
529
|
+
print(
|
|
530
|
+
"Voice session starting. Speak normally, type at the text> prompt, interrupt while the assistant speaks, "
|
|
531
|
+
"and press Ctrl+C to stop.",
|
|
532
|
+
file=sys.stderr,
|
|
533
|
+
flush=True,
|
|
534
|
+
)
|
|
535
|
+
workers: list[asyncio.Task[None]] = []
|
|
536
|
+
stop_waiter: asyncio.Task[bool] | None = None
|
|
537
|
+
started = False
|
|
538
|
+
try:
|
|
539
|
+
await agent.start()
|
|
540
|
+
started = True
|
|
541
|
+
for channel in [*inputs, *outputs]:
|
|
542
|
+
await channel.start(agent)
|
|
543
|
+
if sidecar_control is not None:
|
|
544
|
+
await sidecar_control.start()
|
|
545
|
+
|
|
546
|
+
workers = [
|
|
547
|
+
*(asyncio.create_task(run_input(input_)) for input_ in inputs),
|
|
548
|
+
asyncio.create_task(run_output()),
|
|
549
|
+
*([asyncio.create_task(run_controls())] if sidecar_control is not None else []),
|
|
550
|
+
]
|
|
551
|
+
stop_waiter = asyncio.create_task(stop_requested.wait())
|
|
552
|
+
done, _ = await asyncio.wait([stop_waiter, *workers], return_when=asyncio.FIRST_COMPLETED)
|
|
553
|
+
for task in done:
|
|
554
|
+
if task is stop_waiter or task.cancelled():
|
|
555
|
+
continue
|
|
556
|
+
task.result()
|
|
557
|
+
finally:
|
|
558
|
+
if stop_waiter is not None:
|
|
559
|
+
stop_waiter.cancel()
|
|
560
|
+
for worker in workers:
|
|
561
|
+
worker.cancel()
|
|
562
|
+
await asyncio.gather(*workers, return_exceptions=True)
|
|
563
|
+
if stop_waiter is not None:
|
|
564
|
+
await asyncio.gather(stop_waiter, return_exceptions=True)
|
|
565
|
+
for channel in [*inputs, *outputs, *([sidecar_control] if sidecar_control is not None else [])]:
|
|
566
|
+
try:
|
|
567
|
+
await channel.stop()
|
|
568
|
+
except Exception as error:
|
|
569
|
+
emit("cleanup_error", component=type(channel).__name__, message=str(error))
|
|
570
|
+
if started:
|
|
571
|
+
# The experimental Nova client can hang while writing protocol end events after cancellation.
|
|
572
|
+
# Clearing the connection ID skips those writes; BidiAgent.stop still closes the stream and loop.
|
|
573
|
+
if hasattr(agent.model, "_connection_id"):
|
|
574
|
+
agent.model._connection_id = None # pyright: ignore[reportAttributeAccessIssue]
|
|
575
|
+
try:
|
|
576
|
+
await asyncio.wait_for(agent.stop(), timeout=10)
|
|
577
|
+
except TimeoutError:
|
|
578
|
+
emit("cleanup_error", component="BidiAgent", message="Timed out while stopping the agent loop.")
|
|
579
|
+
except Exception as error:
|
|
580
|
+
emit("cleanup_error", component="BidiAgent", message=str(error))
|
|
581
|
+
emit("session_stopped")
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def main() -> int:
|
|
585
|
+
logging.basicConfig(level=logging.WARNING, stream=sys.stderr)
|
|
586
|
+
args = parse_args()
|
|
587
|
+
|
|
588
|
+
try:
|
|
589
|
+
region, devices = preflight(args)
|
|
590
|
+
if args.list_devices:
|
|
591
|
+
for device in devices:
|
|
592
|
+
emit("audio_device", **device)
|
|
593
|
+
if args.check or args.list_devices:
|
|
594
|
+
return 0
|
|
595
|
+
asyncio.run(run_voice(args, region))
|
|
596
|
+
return 0
|
|
597
|
+
except KeyboardInterrupt:
|
|
598
|
+
emit("session_stop", reason="keyboard_interrupt")
|
|
599
|
+
return 0
|
|
600
|
+
except Exception as error:
|
|
601
|
+
emit("fatal_error", code=type(error).__name__, message=str(error))
|
|
602
|
+
return 1
|
|
603
|
+
|
|
604
|
+
|
|
605
|
+
if __name__ == "__main__":
|
|
606
|
+
raise SystemExit(main())
|