hendrik/tools/smart.py
2026-09-27 06:44:25 +07:00

49 lines
2.0 KiB
Python

import os
from scripts import loggorilla
schema_instruction_use = {
"type": "function",
"function": {
"name": "instruction_use",
"description": "Reads a specific instruction file from the 'instruction' directory and returns its content. This is typically used to load specific guidelines or system prompt extensions.",
"parameters": {
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": "The subdirectory within the 'instruction' folder (e.g., 'test', 'costapy', 'costapy/template')."
},
"filename": {
"type": "string",
"description": "The name of the instruction file to read (e.g., 'test.md', 'costapy.md')."
}
},
"required": ["directory", "filename"]
}
}
}
def instruction_use(directory, filename):
loggorilla.fyinf("tools.smart", f"Calling instruction_use with directory='{directory}', filename='{filename}'")
current_file_dir = os.path.dirname(os.path.abspath(__file__))
project_root = os.path.dirname(current_file_dir)
base_dir = os.path.join(project_root, "instruction")
safe_directory = directory.lstrip('/').replace('..', '')
safe_filename = os.path.basename(filename)
file_path = os.path.join(base_dir, safe_directory, safe_filename)
if not os.path.exists(file_path):
res = f"Error: Instruction file not found at {file_path}"
loggorilla.fyinf("tools.smart", f"File not found: {res}")
return res
try:
with open(file_path, 'r', encoding='utf-8') as f:
content = f.read()
loggorilla.fyinf("tools.smart", f"Successfully read file. Content snippet: {content[:100]}...")
return content
except Exception as e:
res = f"Error reading file: {str(e)}"
loggorilla.fyinf("tools.smart", f"Exception occurred: {res}")
return res