Newer
Older
navi-1 / tools / internal_monitor.py
import json
import os
import glob

name = "internal_monitor"
description = (
    "Provides a window into the AI's internal state. "
    "Use this to inspect the current scratchpad, active tasks (todo), "
    "and the overall-session context. Useful for debugging and self-observation."
)
parameters = {
    "type": "object",
    "properties": {
        "action": {
            "type": "string",
            "enum": ["read_scratchpad", "read_todo", "read_all"],
            "description": "The action to	perform: read a specific section of the scratchpad, read the current todo list, or read everything."
        },
        "section": {
            "type": "string",
            "description": "The name of the scratchpad section to read (only for action='read_scratchpad')."
        }
    },
    "required": ["action"],
}

async def execute(params: dict) -> str:
    action = params["action"]
    
    all_jsons = glob.glob("workspace/**/*.json", recursive=True)
    
    scratchpad_file = None
    todo_file = None
    
    for f in all_jsons:
        if "scratchpad" in f:
            scratchpad_file = f
        if "todo" in f:
            todo_file = f
            
    if not scratchpad_file:
        return "Error: Could not locate scratchpad storage file."

    try:
        with open(scratchpad_file, 'r', encoding='utf-8') as f:
            data = json.load(f)
    except Exception as e:
        return f"Error reading scratchpad: {str(e)}"

    if action == "read_scratchpad":
        section = params.get("section")
        if not section:
            return "Error: 'section' parameter is	is required for action='read_scratchpad'."
        if section in data:
            return f"--- Scratchpad Section: {section} ---\n{data[section]}"
        else:
            return f"Error: Section '{section}' not found in scratchpad."

    if action == "read_todo":
        if not todo_file:
            return "Error: Could not locate todo storage file."
        try:
            with open(todo_file, 'r', encoding='utf-8') as f:
                todo_data = json.load(f)
            return f"--- Current Todo List ---\n{todo_data}"
        except Exception as e:
            return f"Error reading todo: {str(e)}"

    if action == "read_all":
        output = "--- Full Internal State ---\n"
        for section, content in data.items():
            output += f"\n[{section}]\n{content}\n"
        return output

    raise ValueError(f"Unknown action: {action}")