|
|
import os |
|
|
import subprocess |
|
|
import json |
|
|
import time |
|
|
import re |
|
|
from pathlib import Path |
|
|
|
|
|
DB_PATH = "/data/adaptai/migrate/vast/workspace-vast1-2/webui/webui.db" |
|
|
TOOLS_DIR = Path("/data/adaptai/projects/oui-max/tools") |
|
|
SCRIPTS_DIR = Path("/data/adaptai/projects/oui-max/scripts") |
|
|
USER_ID = "11c8250e-3a8c-4320-83f1-5826fd470522" |
|
|
|
|
|
def parse_frontmatter(text: str) -> dict: |
|
|
|
|
|
|
|
|
m = re.search(r'^\s*"""\s*\n(.*?)\n\s*"""', text, re.DOTALL) |
|
|
fm = {} |
|
|
if not m: |
|
|
return fm |
|
|
for line in m.group(1).splitlines(): |
|
|
if ':' in line: |
|
|
k,v = line.split(':',1) |
|
|
fm[k.strip()] = v.strip() |
|
|
return fm |
|
|
|
|
|
def get_function_specs(tool_path: Path) -> list: |
|
|
|
|
|
cmd = [ |
|
|
"sudo", |
|
|
"/venv/main/bin/python3", |
|
|
str(SCRIPTS_DIR / "get_specs.py"), |
|
|
str(tool_path) |
|
|
] |
|
|
try: |
|
|
result = subprocess.run(cmd, capture_output=True, text=True, check=True, timeout=30) |
|
|
return json.loads(result.stdout) |
|
|
except subprocess.CalledProcessError as e: |
|
|
print(f"Error running get_specs.py for {tool_path.stem}: {e.stderr}") |
|
|
raise |
|
|
except json.JSONDecodeError: |
|
|
print(f"Failed to decode JSON from get_specs.py for {tool_path.stem}: {result.stdout}") |
|
|
raise |
|
|
|
|
|
def generate_sql_for_tool(tool_path: Path, user_id: str, timestamp: int) -> str: |
|
|
tool_content = tool_path.read_text() |
|
|
fm = parse_frontmatter(tool_content) |
|
|
|
|
|
tool_id = fm.get('id') or tool_path.stem |
|
|
name = fm.get('title') or tool_path.stem |
|
|
description = fm.get('description', '') |
|
|
version = fm.get('version', '') |
|
|
|
|
|
specs = get_function_specs(tool_path) |
|
|
specs_json = json.dumps(specs) |
|
|
meta_json = json.dumps({"description": description, "manifest": {"version": version}}) |
|
|
|
|
|
sql_statements = [] |
|
|
|
|
|
|
|
|
|
|
|
tool_insert_sql = f""" |
|
|
REPLACE INTO tool(id,user_id,name,content,specs,meta,created_at,updated_at,valves,access_control) VALUES( |
|
|
'{tool_id}', |
|
|
'{user_id}', |
|
|
'{name.replace("'", "''")}', |
|
|
'{tool_content.replace("'", "''")}', |
|
|
'{specs_json.replace("'", "''")}', |
|
|
'{meta_json.replace("'", "''")}', |
|
|
{timestamp}, |
|
|
{timestamp}, |
|
|
NULL, |
|
|
NULL |
|
|
); |
|
|
""" |
|
|
sql_statements.append(tool_insert_sql) |
|
|
|
|
|
|
|
|
for spec in specs: |
|
|
func_name = spec.get('name') |
|
|
func_id = f"{tool_id}.{func_name}" |
|
|
func_display_name = f"{name}: {func_name}" |
|
|
func_description = spec.get('description', '') |
|
|
func_content_json = json.dumps({"tool": tool_id, "func": func_name}) |
|
|
func_meta_json = json.dumps({"description": func_description, "tool": tool_id}) |
|
|
|
|
|
function_insert_sql = f""" |
|
|
REPLACE INTO function(id,user_id,name,type,content,meta,created_at,updated_at,valves,is_active,is_global) VALUES( |
|
|
'{func_id}', |
|
|
'{user_id}', |
|
|
'{func_display_name.replace("'", "''")}', |
|
|
'action', |
|
|
'{func_content_json.replace("'", "''")}', |
|
|
'{func_meta_json.replace("'", "''")}', |
|
|
{timestamp}, |
|
|
{timestamp}, |
|
|
NULL, |
|
|
1, |
|
|
1 |
|
|
); |
|
|
""" |
|
|
sql_statements.append(function_insert_sql) |
|
|
|
|
|
return "\n".join(sql_statements) |
|
|
|
|
|
def main(): |
|
|
timestamp = int(time.time()) |
|
|
|
|
|
for tool_file in sorted(TOOLS_DIR.glob('*.py')): |
|
|
if tool_file.name.startswith('_'): |
|
|
continue |
|
|
|
|
|
print(f"Seeding tool: {tool_file.stem}") |
|
|
|
|
|
try: |
|
|
sql_content = generate_sql_for_tool(tool_file, USER_ID, timestamp) |
|
|
|
|
|
temp_sql_file = SCRIPTS_DIR / f"temp_{tool_file.stem}.sql" |
|
|
temp_sql_file.write_text(sql_content) |
|
|
|
|
|
subprocess.run( |
|
|
["sqlite3", DB_PATH, "-init", str(temp_sql_file)], |
|
|
check=True, |
|
|
capture_output=True, |
|
|
text=True |
|
|
) |
|
|
print(f"Successfully seeded {tool_file.stem}") |
|
|
|
|
|
except subprocess.CalledProcessError as e: |
|
|
print(f"Error seeding {tool_file.stem}: {e.stderr}") |
|
|
except Exception as e: |
|
|
print(f"Unexpected error seeding {tool_file.stem}: {e}") |
|
|
finally: |
|
|
if temp_sql_file.exists(): |
|
|
temp_sql_file.unlink() |
|
|
|
|
|
if __name__ == '__main__': |
|
|
main() |
|
|
|