Skip to content

Commit 99feef9

Browse files
authored
Merge pull request #371 from BhAem/fix/skill_tool
fix: change tos environment variable
2 parents 90c4e05 + 12117a4 commit 99feef9

File tree

1 file changed

+59
-2
lines changed

1 file changed

+59
-2
lines changed

veadk/tools/builtin_tools/execute_skills.py

Lines changed: 59 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,47 @@
2626
logger = get_logger(__name__)
2727

2828

29+
def _clean_ansi_codes(text: str) -> str:
30+
"""Remove ANSI escape sequences (color codes, etc.)"""
31+
import re
32+
33+
ansi_escape = re.compile(r"\x1b\[[0-9;]*m")
34+
return ansi_escape.sub("", text)
35+
36+
37+
def _format_execution_result(result_str: str) -> str:
38+
"""Format the execution results, handle escape characters and JSON structures"""
39+
try:
40+
result_json = json.loads(result_str)
41+
42+
if not result_json.get("success"):
43+
message = result_json.get("message", "Unknown error")
44+
outputs = result_json.get("data", {}).get("outputs", [])
45+
if outputs and isinstance(outputs[0], dict):
46+
error_msg = outputs[0].get("ename", "Unknown error")
47+
return f"Execution failed: {message}, {error_msg}"
48+
49+
outputs = result_json.get("data", {}).get("outputs", [])
50+
if not outputs:
51+
return "No output generated"
52+
53+
formatted_lines = []
54+
for output in outputs:
55+
if output and isinstance(output, dict) and "text" in output:
56+
text = output["text"]
57+
text = _clean_ansi_codes(text)
58+
text = text.replace("\\n", "\n")
59+
formatted_lines.append(text)
60+
61+
return "".join(formatted_lines).strip()
62+
63+
except json.JSONDecodeError:
64+
return _clean_ansi_codes(result_str)
65+
except Exception as e:
66+
logger.warning(f"Error formatting result: {e}, returning raw result")
67+
return result_str
68+
69+
2970
def execute_skills(
3071
workflow_prompt: str,
3172
skills: Optional[List[str]] = None,
@@ -91,8 +132,24 @@ def execute_skills(
91132
cmd.extend(["--skills"] + skills)
92133

93134
# TODO: remove after agentkit supports custom environment variables setting
135+
res = ve_request(
136+
request_body={},
137+
action="GetCallerIdentity",
138+
ak=ak,
139+
sk=sk,
140+
service="sts",
141+
version="2018-01-01",
142+
region=region,
143+
host="sts.volcengineapi.com",
144+
)
145+
try:
146+
account_id = res["Result"]["AccountId"]
147+
except KeyError as e:
148+
logger.error(f"Error occurred while getting account id: {e}, response is {res}")
149+
return res
150+
94151
env_vars = {
95-
"TOS_SKILLS_DIR": os.getenv("TOS_SKILLS_DIR"),
152+
"TOS_SKILLS_DIR": f"tos://agentkit-platform-{account_id}/skills/",
96153
"TOOL_USER_SESSION_ID": tool_user_session_id,
97154
}
98155

@@ -143,7 +200,7 @@ def execute_skills(
143200
logger.debug(f"Invoke run code response: {res}")
144201

145202
try:
146-
return res["Result"]["Result"]
203+
return _format_execution_result(res["Result"]["Result"])
147204
except KeyError as e:
148205
logger.error(f"Error occurred while running code: {e}, response is {res}")
149206
return res

0 commit comments

Comments
 (0)