Skip to content

Commit 368fbeb

Browse files
author
wangyue.demon
committed
auth(veauth): implement speech token retrieval with credential fallback
1 parent c5a99cf commit 368fbeb

File tree

5 files changed

+28
-12
lines changed

5 files changed

+28
-12
lines changed

config.yaml.full

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,11 @@ tool:
4646
web_scraper:
4747
endpoint:
4848
api_key: # `token`
49+
# [optional] https://console.volcengine.com/speech/new/experience/tts
50+
lark:
51+
app_id: # `app_id`
52+
api_key: # `app_secret`
53+
speaker: # `speaker`
4954
# [optional] https://open.larkoffice.com/app
5055
lark:
5156
endpoint: # `app_id`

tests/tools/builtin_tools/test_tts.py

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,9 @@ def setUp(self):
3737
self.patcher_env = patch.dict(
3838
"os.environ",
3939
{
40-
"TOOL_TTS_APP_ID": "test_app_id",
41-
"TOOL_TTS_API_KEY": "test_api_key",
42-
"TOOL_TTS_SPEAKER": "test_speaker",
40+
"TOOL_VESPEECH_APP_ID": "test_app_id",
41+
"TOOL_VESPEECH_API_KEY": "test_api_key",
42+
"TOOL_VESPEECH_SPEAKER": "test_speaker",
4343
},
4444
)
4545
self.patcher_env.start()
@@ -64,8 +64,7 @@ def test_tts_success(self, mock_session):
6464

6565
# Assertions
6666
self.assertIsInstance(result, dict)
67-
self.assertIn("text", result)
68-
self.assertIn("audio_path", result)
67+
self.assertIn("saved_audio_path", result)
6968
mock_session.return_value.post.assert_called_once()
7069
mock_response.close.assert_called_once()
7170

veadk/auth/veauth/speech_veauth.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,9 +36,9 @@ def get_speech_token(region: str = "cn-beijing") -> str:
3636
session_token = cred.session_token
3737

3838
res = ve_request(
39-
request_body={"ProjectName": "default", "OnlyAvailable": True, "Filter": {}},
39+
request_body={"ProjectName": "default", "OnlyAvailable": True},
4040
header={"X-Security-Token": session_token},
41-
action="ListApiKeys",
41+
action="ListAPIKeys",
4242
ak=access_key,
4343
sk=secret_key,
4444
service="speech_saas_prod",

veadk/configs/tool_configs.py

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@
2020

2121
from veadk.auth.veauth.prompt_pilot_veauth import PromptPilotVeAuth
2222
from veadk.auth.veauth.vesearch_veauth import VesearchVeAuth
23+
from veadk.auth.veauth.speech_veauth import get_speech_token
2324

2425

2526
class PromptPilotConfig(BaseModel):
@@ -38,5 +39,16 @@ def api_key(self) -> str:
3839
return os.getenv("TOOL_VESEARCH_API_KEY") or VesearchVeAuth().token
3940

4041

42+
class VeSpeechConfig(BaseSettings):
43+
model_config = SettingsConfigDict(env_prefix="TOOL_VESPEECH_")
44+
45+
endpoint: int | str = ""
46+
47+
@cached_property
48+
def api_key(self) -> str:
49+
return os.getenv("TOOL_VESPEECH_API_KEY") or get_speech_token()
50+
51+
4152
class BuiltinToolConfigs(BaseModel):
4253
vesearch: VeSearchConfig = Field(default_factory=VeSearchConfig)
54+
vespeech: VeSpeechConfig = Field(default_factory=VeSpeechConfig)

veadk/tools/builtin_tools/tts.py

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
import tempfile
2424
from typing import Dict, Any
2525
from google.adk.tools import ToolContext
26-
from veadk.config import getenv
26+
from veadk.config import getenv, settings
2727
from veadk.utils.logger import get_logger
2828
from veadk.utils.audio_manager import AudioDeviceManager, AudioConfig
2929

@@ -60,16 +60,16 @@ def text_to_speech(text: str, tool_context: ToolContext) -> Dict[str, Any]:
6060
url = "https://openspeech.bytedance.com/api/v3/tts/unidirectional"
6161
audio_save_path = ""
6262

63-
app_id = getenv("TOOL_TTS_APP_ID")
64-
api_key = getenv("TOOL_TTS_API_KEY")
63+
app_id = getenv("TOOL_VESPEECH_APP_ID")
6564
speaker = getenv(
66-
"TOOL_TTS_SPEAKER", "zh_female_vv_uranus_bigtts"
65+
"TOOL_VESPEECH_SPEAKER", "zh_female_vv_uranus_bigtts"
6766
) # e.g. zh_female_vv_mars_bigtts
67+
api_key = settings.tool.vespeech.api_key
6868
if not all([app_id, api_key, speaker]):
6969
return {
7070
"error": (
7171
"Tool text_to_speech execution failed. Missing required env vars: "
72-
"TOOL_TTS_APP_ID, TOOL_TTS_API_KEY, TOOL_TTS_SPEAKER"
72+
"TOOL_VESPEECH_APP_ID, TOOL_VESPEECH_API_KEY, TOOL_VESPEECH_SPEAKER"
7373
)
7474
}
7575

0 commit comments

Comments
 (0)