From 681d29ec9fa7cda8ed8946c22aecdfb3e33baf6f Mon Sep 17 00:00:00 2001 From: zhangqianze Date: Mon, 9 Sep 2024 13:39:23 +0000 Subject: [PATCH] feat: switch to native env --- agents/property.json | 152 ++++++++++++++++----------------- server/internal/http_server.go | 40 ++++----- 2 files changed, 91 insertions(+), 101 deletions(-) diff --git a/agents/property.json b/agents/property.json index 714af5df..3c89e1fc 100644 --- a/agents/property.json +++ b/agents/property.json @@ -12,7 +12,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -23,8 +23,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -41,12 +41,12 @@ "name": "openai_chatgpt", "property": { "base_url": "", - "api_key": "$OPENAI_API_KEY", + "api_key": "${env:OPENAI_API_KEY}", "frequency_penalty": 0.9, "model": "gpt-4o-mini", "max_tokens": 512, "prompt": "", - "proxy_url": "$OPENAI_PROXY_URL", + "proxy_url": "${env:OPENAI_PROXY_URL}", "greeting": "ASTRA agent connected. How can i help you today?", "max_memory_length": 10 } @@ -57,8 +57,8 @@ "addon": "azure_tts", "name": "azure_tts", "property": { - "azure_subscription_key": "$AZURE_TTS_KEY", - "azure_subscription_region": "$AZURE_TTS_REGION", + "azure_subscription_key": "${env:AZURE_TTS_KEY}", + "azure_subscription_region": "${env:AZURE_TTS_REGION}", "azure_synthesis_voice_name": "en-US-JaneNeural" } }, @@ -211,7 +211,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -222,8 +222,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -240,12 +240,12 @@ "name": "openai_chatgpt", "property": { "base_url": "", - "api_key": "$OPENAI_API_KEY", + "api_key": "${env:OPENAI_API_KEY}", "frequency_penalty": 0.9, "model": "gpt-4o-mini", "max_tokens": 512, "prompt": "", - "proxy_url": "$OPENAI_PROXY_URL", + "proxy_url": "${env:OPENAI_PROXY_URL}", "greeting": "ASTRA agent connected. How can i help you today?", "max_memory_length": 10 } @@ -256,7 +256,7 @@ "addon": "elevenlabs_tts", "name": "elevenlabs_tts", "property": { - "api_key": "$ELEVENLABS_TTS_KEY", + "api_key": "${env:ELEVENLABS_TTS_KEY}", "model_id": "eleven_multilingual_v2", "optimize_streaming_latency": 0, "request_timeout_seconds": 30, @@ -416,7 +416,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -427,8 +427,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -439,8 +439,8 @@ "name": "bedrock_llm", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", "max_tokens": 512, "prompt": "", @@ -454,8 +454,8 @@ "addon": "azure_tts", "name": "azure_tts", "property": { - "azure_subscription_key": "$AZURE_TTS_KEY", - "azure_subscription_region": "$AZURE_TTS_REGION", + "azure_subscription_key": "${env:AZURE_TTS_KEY}", + "azure_subscription_region": "${env:AZURE_TTS_REGION}", "azure_synthesis_voice_name": "en-US-JaneNeural" } }, @@ -580,7 +580,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -591,8 +591,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -603,12 +603,12 @@ "name": "openai_chatgpt", "property": { "base_url": "", - "api_key": "$OPENAI_API_KEY", + "api_key": "${env:OPENAI_API_KEY}", "frequency_penalty": 0.9, "model": "gpt-4o-mini", "max_tokens": 512, "prompt": "", - "proxy_url": "$OPENAI_PROXY_URL", + "proxy_url": "${env:OPENAI_PROXY_URL}", "greeting": "ASTRA agent connected. How can i help you today?", "max_memory_length": 10 } @@ -619,7 +619,7 @@ "addon": "cosy_tts", "name": "cosy_tts", "property": { - "api_key": "$QWEN_API_KEY", + "api_key": "${env:QWEN_API_KEY}", "model": "cosyvoice-v1", "voice": "longxiaochun", "sample_rate": 16000 @@ -780,7 +780,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -791,8 +791,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -802,7 +802,7 @@ "addon": "qwen_llm_python", "name": "qwen_llm", "property": { - "api_key": "$QWEN_API_KEY", + "api_key": "${env:QWEN_API_KEY}", "model": "qwen-max", "max_tokens": 512, "prompt": "", @@ -815,7 +815,7 @@ "addon": "cosy_tts", "name": "cosy_tts", "property": { - "api_key": "$QWEN_API_KEY", + "api_key": "${env:QWEN_API_KEY}", "model": "cosyvoice-v1", "voice": "longxiaochun", "sample_rate": 16000 @@ -983,7 +983,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -994,8 +994,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -1006,8 +1006,8 @@ "name": "bedrock_llm", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", "max_tokens": 512, "prompt": "", @@ -1022,8 +1022,8 @@ "name": "polly_tts", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "engine": "generative", "voice": "Ruth", "sample_rate": "16000", @@ -1151,7 +1151,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -1162,8 +1162,8 @@ "enable_agora_asr": false, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -1174,8 +1174,8 @@ "name": "transcribe_asr", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "sample_rate": "16000", "lang_code": "en-US" } @@ -1187,8 +1187,8 @@ "name": "bedrock_llm", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "model": "anthropic.claude-3-5-sonnet-20240620-v1:0", "max_tokens": 512, "prompt": "", @@ -1203,8 +1203,8 @@ "name": "polly_tts", "property": { "region": "us-east-1", - "access_key": "$AWS_ACCESS_KEY_ID", - "secret_key": "$AWS_SECRET_ACCESS_KEY", + "access_key": "${env:AWS_ACCESS_KEY_ID}", + "secret_key": "${env:AWS_SECRET_ACCESS_KEY}", "engine": "generative", "voice": "Ruth", "sample_rate": "16000", @@ -1381,7 +1381,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -1393,8 +1393,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf", "subscribe_video_pix_fmt": 4 } @@ -1412,12 +1412,12 @@ "name": "openai_chatgpt", "property": { "base_url": "", - "api_key": "$OPENAI_API_KEY", + "api_key": "${env:OPENAI_API_KEY}", "frequency_penalty": 0.9, "model": "gpt-4o", "max_tokens": 512, "prompt": "", - "proxy_url": "$OPENAI_PROXY_URL", + "proxy_url": "${env:OPENAI_PROXY_URL}", "greeting": "ASTRA agent connected. How can i help you today?", "checking_vision_text_items": "[\"Let me take a look...\",\"Let me check your camera...\",\"Please wait for a second...\"]", "max_memory_length": 10, @@ -1430,8 +1430,8 @@ "addon": "azure_tts", "name": "azure_tts", "property": { - "azure_subscription_key": "$AZURE_TTS_KEY", - "azure_subscription_region": "$AZURE_TTS_REGION", + "azure_subscription_key": "${env:AZURE_TTS_KEY}", + "azure_subscription_region": "${env:AZURE_TTS_REGION}", "azure_synthesis_voice_name": "en-US-JaneNeural" } }, @@ -1595,7 +1595,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -1606,8 +1606,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -1623,7 +1623,7 @@ "addon": "gemini_llm_python", "name": "gemini_llm", "property": { - "api_key": "$GEMINI_API_KEY", + "api_key": "${env:GEMINI_API_KEY}", "greeting": "ASTRA agent connected. How can i help you today?", "max_memory_length": 10, "max_output_tokens": 512, @@ -1640,8 +1640,8 @@ "addon": "azure_tts", "name": "azure_tts", "property": { - "azure_subscription_key": "$AZURE_TTS_KEY", - "azure_subscription_region": "$AZURE_TTS_REGION", + "azure_subscription_key": "${env:AZURE_TTS_KEY}", + "azure_subscription_region": "${env:AZURE_TTS_REGION}", "azure_synthesis_voice_name": "en-US-JaneNeural" } }, @@ -1794,7 +1794,7 @@ "addon": "agora_rtc", "name": "agora_rtc", "property": { - "app_id": "$AGORA_APP_ID", + "app_id": "${env:AGORA_APP_ID}", "token": "", "channel": "astra_agents_test", "stream_id": 1234, @@ -1805,8 +1805,8 @@ "enable_agora_asr": true, "agora_asr_vendor_name": "microsoft", "agora_asr_language": "en-US", - "agora_asr_vendor_key": "$AZURE_STT_KEY", - "agora_asr_vendor_region": "$AZURE_STT_REGION", + "agora_asr_vendor_key": "${env:AZURE_STT_KEY}", + "agora_asr_vendor_region": "${env:AZURE_STT_REGION}", "agora_asr_session_control_file_path": "session_control.conf" } }, @@ -1816,7 +1816,7 @@ "addon": "qwen_llm_python", "name": "qwen_llm", "property": { - "api_key": "$QWEN_API_KEY", + "api_key": "${env:QWEN_API_KEY}", "model": "qwen-max", "max_tokens": 512, "prompt": "", @@ -1829,7 +1829,7 @@ "addon": "cosy_tts", "name": "cosy_tts", "property": { - "api_key": "$QWEN_API_KEY", + "api_key": "${env:QWEN_API_KEY}", "model": "cosyvoice-v1", "voice": "longxiaochun", "sample_rate": 16000 @@ -1841,8 +1841,8 @@ "addon": "azure_tts", "name": "azure_tts", "property": { - "azure_subscription_key": "$AZURE_TTS_KEY", - "azure_subscription_region": "$AZURE_TTS_REGION", + "azure_subscription_key": "${env:AZURE_TTS_KEY}", + "azure_subscription_region": "${env:AZURE_TTS_REGION}", "azure_synthesis_voice_name": "en-US-JaneNeural" } }, @@ -1874,7 +1874,7 @@ "addon": "aliyun_text_embedding", "name": "aliyun_text_embedding", "property": { - "api_key": "$ALIYUN_TEXT_EMBEDDING_API_KEY", + "api_key": "${env:ALIYUN_TEXT_EMBEDDING_API_KEY}", "model": "text-embedding-v3" } }, @@ -1884,14 +1884,14 @@ "addon": "aliyun_analyticdb_vector_storage", "name": "aliyun_analyticdb_vector_storage", "property": { - "alibaba_cloud_access_key_id": "$ALIBABA_CLOUD_ACCESS_KEY_ID", - "alibaba_cloud_access_key_secret": "$ALIBABA_CLOUD_ACCESS_KEY_SECRET", - "adbpg_instance_id": "$ALIYUN_ANALYTICDB_INSTANCE_ID", - "adbpg_instance_region": "$ALIYUN_ANALYTICDB_INSTANCE_REGION", - "adbpg_account": "$ALIYUN_ANALYTICDB_ACCOUNT", - "adbpg_account_password": "$ALIYUN_ANALYTICDB_ACCOUNT_PASSWORD", - "adbpg_namespace": "$ALIYUN_ANALYTICDB_NAMESPACE", - "adbpg_namespace_password": "$ALIYUN_ANALYTICDB_NAMESPACE_PASSWORD" + "alibaba_cloud_access_key_id": "${env:ALIBABA_CLOUD_ACCESS_KEY_ID}", + "alibaba_cloud_access_key_secret": "${env:ALIBABA_CLOUD_ACCESS_KEY_SECRET}", + "adbpg_instance_id": "${env:ALIYUN_ANALYTICDB_INSTANCE_ID}", + "adbpg_instance_region": "${env:ALIYUN_ANALYTICDB_INSTANCE_REGION}", + "adbpg_account": "${env:ALIYUN_ANALYTICDB_ACCOUNT}", + "adbpg_account_password": "${env:ALIYUN_ANALYTICDB_ACCOUNT_PASSWORD}", + "adbpg_namespace": "${env:ALIYUN_ANALYTICDB_NAMESPACE}", + "adbpg_namespace_password": "${env:ALIYUN_ANALYTICDB_NAMESPACE_PASSWORD}" } }, { diff --git a/server/internal/http_server.go b/server/internal/http_server.go index f3fea8b2..d7c41915 100644 --- a/server/internal/http_server.go +++ b/server/internal/http_server.go @@ -15,7 +15,6 @@ import ( "net/http" "os" "path/filepath" - "regexp" "strings" "time" @@ -381,23 +380,6 @@ func (s *HttpServer) output(c *gin.Context, code *Code, data any, httpStatus ... c.JSON(httpStatus[0], gin.H{"code": code.code, "msg": code.msg, "data": data}) } -func replaceEnvVarsInJSON(jsonData string) string { - // Regex to find all occurrences of $VAR_NAME - re := regexp.MustCompile(`"\$(\w+)"`) - - // Function to replace the match with the environment variable value - result := re.ReplaceAllStringFunc(jsonData, func(match string) string { - // Extract the variable name (removing the leading $ and surrounding quotes) - envVar := strings.Trim(match, "\"$") - // Get the environment variable value - value := os.Getenv(envVar) - // Replace with the value (keeping it quoted) - return fmt.Sprintf("\"%s\"", value) - }) - - return result -} - func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, logFile string, err error) { content, err := os.ReadFile(PropertyJsonFile) if err != nil { @@ -430,10 +412,10 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo graphs := gjson.Get(propertyJson, "_ten.predefined_graphs").Array() // Create a new array for graphs that match the name - var newGraphs []string + var newGraphs []gjson.Result for _, graph := range graphs { if graph.Get("name").String() == graphName { - newGraphs = append(newGraphs, graph.Raw) + newGraphs = append(newGraphs, graph) } } @@ -443,20 +425,28 @@ func (s *HttpServer) processProperty(req *StartReq) (propertyJsonFile string, lo return } + // Set the array of graphs directly using sjson.Set + graphData := make([]interface{}, len(newGraphs)) + for i, graph := range newGraphs { + graphData[i] = graph.Value() // Convert gjson.Result to interface{} + } + // Replace the predefined_graphs array with the filtered array - propertyJson, _ = sjson.SetRaw(propertyJson, "_ten.predefined_graphs", fmt.Sprintf("[%s]", strings.Join(newGraphs, ","))) + propertyJson, _ = sjson.Set(propertyJson, "_ten.predefined_graphs", graphData) // Automatically start on launch propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.auto_start`, graph), true) - // Set environment variable values to property.json - propertyJson = replaceEnvVarsInJSON(propertyJson) - // Set additional properties to property.json for extensionName, props := range req.Properties { if extKey := extensionName; extKey != "" { for prop, val := range props { - propertyJson, _ = sjson.Set(propertyJson, fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, extKey, prop), val) + // Construct the path + path := fmt.Sprintf(`%s.nodes.#(name=="%s").property.%s`, graph, extKey, prop) + propertyJson, err = sjson.Set(propertyJson, path, val) + if err != nil { + slog.Error("handlerStart set property failed", "err", err, "graph", graphName, "extensionName", extensionName, "prop", prop, "val", val, "requestId", req.RequestId, logTag) + } } } }