tools.write_python_tool module

Create new Python tool files in the tools/ directory.

This tool writes a new .py file into the tools/ directory and handles all downstream registration automatically:

  1. Syntax validation – parses the code with AST before writing.

  2. File creation – writes the .py file (optionally overwriting).

  3. Markdown docs – generates a Markdown doc from the source and indexes it into the stargazer_docs RAG store so it is immediately searchable. Also creates an RST stub for the next full Sphinx rebuild.

  4. Registry reload – hot-reloads the tool registry so the new tool is callable in the same conversation.

  5. Classifier embeddings – regenerates vector embeddings so the tool can be auto-selected by future prompts.

Requires the UNSANDBOXED_EXEC privilege.

What the LLM actually sees at tool-selection time

The OpenAI function-calling API sends exactly three fields per tool:

  • TOOL_NAMEfunction.name

  • TOOL_DESCRIPTIONfunction.descriptionthe ONLY prose the LLM reads when deciding whether to call a tool

  • TOOL_PARAMETERSfunction.parameters

Module docstrings and run() docstrings are NOT sent to the LLM directly. They are auto-extracted into Markdown docs and indexed into the stargazer_docs RAG store, where the LLM can search them later with rag_search.

Documentation best practices

  • TOOL_DESCRIPTION must be self-contained and comprehensive. It is the ONLY text the LLM sees at tool-selection time. Write it so that any future instance of Stargazer can understand the tool’s purpose, constraints, and usage in any context.

  • Function docstrings feed the auto-generated Markdown docs in the stargazer_docs RAG store. Document every public function, parameter, and edge case for deeper reference.

async tools.write_python_tool.run(tool_code, tool_name, description=None, overwrite=False, ctx=None)[source]

Execute this tool and return the result.

Parameters:
  • tool_code (str) – The tool code value.

  • tool_name (str) – The tool name value.

  • description (str) – Human-readable description.

  • overwrite (bool) – The overwrite value.

  • ctx (ToolContext | None) – Tool execution context providing access to bot internals.

Returns:

Result string.

Return type:

str