hendrik/tools/smart.py

51 lines
2.2 KiB
Python

import os
# NOTE: loggorilla dimatikan di tool ini. print()-nya bisa bikin error saat
# proses keluar (SCI), jadi semua pemanggilan di bawah hanya di-comment, jangan dihapus.
# 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