hendrik/tools/smart.py
2026-10-09 09:33:26 +07:00

93 lines
3.9 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
from tools import instrbase
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
# ─── instruction_char_use (private / per-character variant) ────────────────────
schema_instruction_char_use = {
"type": "function",
"function": {
"name": "instruction_char_use",
"description": (
"Reads a specific instruction file from the PRIVATE 'instruction' directory of the "
"active character (agent/characters/<character>/instruction) and returns its content. "
"This is the character-scoped variant of instruction_use: use it to load snippets, "
"guidelines or system prompt extensions that belong only to the current character, "
"instead of the shared/global 'instruction' directory."
),
"parameters": {
"type": "object",
"properties": {
"directory": {
"type": "string",
"description": (
"The subdirectory within the character's own 'instruction' folder "
"(e.g., 'snippets', 'scenes', 'world'). Pass an empty string to read "
"directly from the root of that folder."
)
},
"filename": {
"type": "string",
"description": "The name of the instruction file to read (e.g., 'opening.md', 'rules.md')."
}
},
"required": ["directory", "filename"]
}
}
}
def instruction_char_use(directory, filename):
# loggorilla dimatikan di tool ini (lihat catatan di atas), logger dikirim None.
return instrbase.read_instruction_file("tools.smart", None, directory, filename)