forked from volcengine/OpenViking
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathskill_processor.py
More file actions
226 lines (193 loc) · 7.43 KB
/
skill_processor.py
File metadata and controls
226 lines (193 loc) · 7.43 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
# Copyright (c) 2026 Beijing Volcano Engine Technology Co., Ltd.
# SPDX-License-Identifier: Apache-2.0
"""
Skill Processor for OpenViking.
Handles skill parsing, LLM generation, and storage operations.
"""
import tempfile
import zipfile
from pathlib import Path
from typing import Any, Dict, List, Optional
from openviking.core.context import Context, ContextType, Vectorize
from openviking.core.mcp_converter import is_mcp_format, mcp_to_skill
from openviking.core.skill_loader import SkillLoader
from openviking.server.identity import RequestContext
from openviking.storage import VikingDBManager
from openviking.storage.queuefs.embedding_msg_converter import EmbeddingMsgConverter
from openviking.storage.viking_fs import VikingFS
from openviking_cli.utils import get_logger
from openviking_cli.utils.config import get_openviking_config
logger = get_logger(__name__)
class SkillProcessor:
"""
Handles skill processing and storage.
Workflow:
1. Parse skill data (directory, file, string, or dict)
2. Generate L1 overview using VLM
3. Write skill content to VikingFS
4. Write auxiliary files
5. Index to vector store
"""
def __init__(self, vikingdb: VikingDBManager):
"""Initialize skill processor."""
self.vikingdb = vikingdb
async def process_skill(
self,
data: Any,
viking_fs: VikingFS,
ctx: RequestContext,
) -> Dict[str, Any]:
"""
Process and store a skill.
Args:
data: Skill data (directory path, file path, string, or dict)
viking_fs: VikingFS instance for storage
user: Username for context
Returns:
Processing result with status and metadata
"""
config = get_openviking_config()
skill_dict, auxiliary_files, base_path = self._parse_skill(data)
context = Context(
uri=f"viking://agent/skills/{skill_dict['name']}",
parent_uri="viking://agent/skills",
is_leaf=False,
abstract=skill_dict.get("description", ""),
context_type=ContextType.SKILL.value,
user=ctx.user,
account_id=ctx.account_id,
owner_space=ctx.user.agent_space_name(),
meta={
"name": skill_dict["name"],
"description": skill_dict.get("description", ""),
"allowed_tools": skill_dict.get("allowed_tools", []),
"tags": skill_dict.get("tags", []),
"source_path": skill_dict.get("source_path", ""),
},
)
context.set_vectorize(Vectorize(text=context.abstract))
overview = await self._generate_overview(skill_dict, config)
skill_dir_uri = f"viking://agent/skills/{context.meta['name']}"
await self._write_skill_content(
viking_fs=viking_fs,
skill_dict=skill_dict,
skill_dir_uri=skill_dir_uri,
overview=overview,
ctx=ctx,
)
await self._write_auxiliary_files(
viking_fs=viking_fs,
auxiliary_files=auxiliary_files,
base_path=base_path,
skill_dir_uri=skill_dir_uri,
ctx=ctx,
)
await self._index_skill(
context=context,
skill_dir_uri=skill_dir_uri,
)
return {
"status": "success",
"uri": skill_dir_uri,
"name": skill_dict["name"],
"auxiliary_files": len(auxiliary_files),
}
def _parse_skill(self, data: Any) -> tuple[Dict[str, Any], List[Path], Optional[Path]]:
"""Parse skill data from various formats."""
auxiliary_files = []
base_path = None
if isinstance(data, str):
path_obj = Path(data)
if path_obj.exists():
if zipfile.is_zipfile(path_obj):
temp_dir = Path(tempfile.mkdtemp())
with zipfile.ZipFile(path_obj, "r") as zipf:
zipf.extractall(temp_dir)
data = temp_dir
else:
data = path_obj
if isinstance(data, Path):
if data.is_dir():
# Directory containing SKILL.md
skill_file = data / "SKILL.md"
if not skill_file.exists():
raise ValueError(f"SKILL.md not found in {data}")
skill_dict = SkillLoader.load(str(skill_file))
base_path = data
for item in data.rglob("*"):
if item.is_file() and item.name != "SKILL.md":
auxiliary_files.append(item)
else:
# Single SKILL.md file
skill_dict = SkillLoader.load(str(data))
elif isinstance(data, str):
# Raw SKILL.md content
skill_dict = SkillLoader.parse(data)
elif isinstance(data, dict):
if is_mcp_format(data):
skill_dict = mcp_to_skill(data)
else:
skill_dict = data
else:
raise ValueError(f"Unsupported data type: {type(data)}")
return skill_dict, auxiliary_files, base_path
async def _generate_overview(self, skill_dict: Dict[str, Any], config) -> str:
"""Generate L1 overview using VLM."""
from openviking.prompts import render_prompt
prompt = render_prompt(
"skill.overview_generation",
{
"skill_name": skill_dict["name"],
"skill_description": skill_dict.get("description", ""),
"skill_content": skill_dict.get("content", ""),
},
)
return await config.vlm.get_completion_async(prompt)
async def _write_skill_content(
self,
viking_fs: VikingFS,
skill_dict: Dict[str, Any],
skill_dir_uri: str,
overview: str,
ctx: RequestContext,
):
"""Write main skill content to VikingFS."""
await viking_fs.write_context(
uri=skill_dir_uri,
content=skill_dict.get("content", ""),
abstract=skill_dict.get("description", ""),
overview=overview,
content_filename="SKILL.md",
is_leaf=False,
ctx=ctx,
)
async def _write_auxiliary_files(
self,
viking_fs: VikingFS,
auxiliary_files: List[Path],
base_path: Optional[Path],
skill_dir_uri: str,
ctx: RequestContext,
):
"""Write auxiliary files to VikingFS."""
for aux_file in auxiliary_files:
if base_path:
rel_path = aux_file.relative_to(base_path)
aux_uri = f"{skill_dir_uri}/{rel_path}"
else:
aux_uri = f"{skill_dir_uri}/{aux_file.name}"
file_bytes = aux_file.read_bytes()
try:
file_bytes.decode("utf-8")
is_text = True
except UnicodeDecodeError:
is_text = False
if is_text:
await viking_fs.write_file(aux_uri, file_bytes.decode("utf-8"), ctx=ctx)
else:
await viking_fs.write_file_bytes(aux_uri, file_bytes, ctx=ctx)
async def _index_skill(self, context: Context, skill_dir_uri: str):
"""Write skill to vector store via async queue."""
context.uri = skill_dir_uri
embedding_msg = EmbeddingMsgConverter.from_context(context)
await self.vikingdb.enqueue_embedding_msg(embedding_msg)