Skip to content

Commit 83a1400

Browse files
niiish32x越鸿
andauthored
fix: inject correct skill path into agent context for sandbox and loc… (#145)
Co-authored-by: 越鸿 <nishenghao.nsh@oceanbase.com>
1 parent 47ba9af commit 83a1400

2 files changed

Lines changed: 70 additions & 5 deletions

File tree

packages/derisk-core/src/derisk/agent/expand/react_master_agent/react_master_agent.py

Lines changed: 45 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@
1414
from typing import Any, Dict, List, Optional, Tuple, Callable, Awaitable
1515

1616
from derisk._private.pydantic import Field, PrivateAttr
17+
from derisk.configs.model_config import DATA_DIR
18+
import os
1719
from derisk.agent import (
1820
ActionOutput,
1921
Agent,
@@ -1402,6 +1404,21 @@ async def var_available_knowledges(instance):
14021404
async def var_skills(instance):
14031405
logger.info("注入技能资源")
14041406

1407+
# Sandbox mode: skill_dir comes from the sandbox client
1408+
# (e.g. /mnt/derisk/skills set in [sandbox].skill_dir of the toml).
1409+
# Local mode: default to DATA_DIR/skill (pilot/data/skill).
1410+
sandbox_skill_dir: Optional[str] = None
1411+
if instance and getattr(instance, "sandbox_manager", None):
1412+
sb_client = getattr(instance.sandbox_manager, "client", None)
1413+
if sb_client:
1414+
sandbox_skill_dir = getattr(sb_client, "skill_dir", None)
1415+
1416+
local_skill_dir = os.path.join(DATA_DIR, "skill")
1417+
logger.info(
1418+
f"var_skills: sandbox_skill_dir={sandbox_skill_dir!r}, "
1419+
f"local_skill_dir={local_skill_dir!r}"
1420+
)
1421+
14051422
prompts = ""
14061423
for k, v in self.resource_map.items():
14071424
if isinstance(v[0], AgentSkillResource):
@@ -1414,11 +1431,34 @@ async def var_skills(instance):
14141431
skill_meta = skill_item.skill_meta(mode)
14151432
if not skill_meta:
14161433
continue
1417-
skill_path = (
1418-
skill_item._skill.parent_folder
1419-
if hasattr(skill_item, "_skill") and skill_item._skill
1420-
else skill_meta.path
1421-
)
1434+
1435+
# skill_code is the UUID (DeriskSkillResource) or dir name.
1436+
skill_code = getattr(
1437+
skill_item, "_skill_code", None
1438+
) or getattr(skill_item, "skill_code", None)
1439+
if not skill_code and skill_meta.path:
1440+
skill_code = os.path.basename(skill_meta.path)
1441+
1442+
# Use sandbox path only when the skill directory actually
1443+
# exists inside the sandbox; otherwise fall back to local.
1444+
if os.path.isdir(os.path.join(sandbox_skill_dir, skill_code)):
1445+
skill_path = os.path.join(sandbox_skill_dir,skill_code)
1446+
else:
1447+
skill_path = os.path.join(local_skill_dir,skill_item._skill_path)
1448+
1449+
1450+
# if skill_code and sandbox_skill_dir:
1451+
# sandbox_path = os.path.join(sandbox_skill_dir, skill_code)
1452+
# skill_path = (
1453+
# sandbox_path
1454+
# if os.path.exists(sandbox_path)
1455+
# else os.path.join(local_skill_dir, skill_code)
1456+
# )
1457+
# elif skill_code:
1458+
# skill_path = os.path.join(local_skill_dir, skill_code)
1459+
# else:
1460+
# skill_path = skill_meta.path
1461+
14221462
prompts += (
14231463
f"- <skill>"
14241464
f"<name>{skill_meta.name}</name>"

packages/derisk-serve/src/derisk_serve/skill/service/service.py

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,16 @@ def create(self, request: SkillRequest) -> SkillResponse:
8484

8585
request.skill_code = str(uuid.uuid4())
8686

87+
# Set default path to skill_dir/skill_code if not provided
88+
if not request.path:
89+
project_skill_dir = self._serve_config.get_project_skill_dir()
90+
request.path = os.path.join(project_skill_dir, request.skill_code)
91+
92+
logger.info(
93+
f"Skill '{request.name}' path is empty, "
94+
f"using default path '{request.path}'"
95+
)
96+
8797
existing_skill = self.dao.get_one({"skill_code": request.skill_code})
8898
if existing_skill:
8999
logger.info(f"Skill {request.skill_code} already exists, updating instead")
@@ -109,6 +119,21 @@ def update(self, request: SkillRequest) -> SkillResponse:
109119
request_dict.pop("gmt_created", None)
110120
request_dict.pop("gmt_modified", None)
111121

122+
# Set default path if not provided
123+
if not request_dict.get("path"):
124+
existing = self.dao.get_one({"skill_code": request.skill_code})
125+
if existing and existing.path:
126+
request_dict["path"] = existing.path
127+
else:
128+
project_skill_dir = self._serve_config.get_project_skill_dir()
129+
request_dict["path"] = os.path.join(
130+
project_skill_dir, request.skill_code
131+
)
132+
logger.info(
133+
f"Skill '{request.skill_code}' update path is empty, "
134+
f"using default path '{request_dict['path']}'"
135+
)
136+
112137
return self.dao.update(query_request, request_dict)
113138

114139
def get(self, request: SkillRequest) -> Optional[SkillResponse]:

0 commit comments

Comments
 (0)