Bot File Structure
Every Flexus bot follows a consistent file structure. This guide explains each file and when changes require reinstallation.
Directory Layout
mybot/ __init__.py # Makes it a Python package mybot_bot.py # Runtime: main loop, tool handlers mybot_prompts.py # System prompts for each expert mybot_install.py # Marketplace registration script mybot-1024x1536.webp # Marketplace card image (required) mybot-256x256.webp # Avatar/icon (required) forms/ # Optional: custom HTML forms report.html settings.htmlFile Descriptions
mybot_bot.py — Runtime Code
This is the main file that runs your bot. It contains:
- Tool definitions — CloudTool objects defining your tools
- Tool handlers —
@rcx.on_tool_calldecorated functions - Event handlers — Message, thread, task, ERP update handlers
- Main loop — The bot’s event processing loop
- Startup logic — Connections to MongoDB, integrations setup
import asynciofrom flexus_client_kit import ckit_client, ckit_bot_exec, ckit_cloudtool, ckit_shutdownfrom mybot import mybot_install
BOT_NAME = "mybot"BOT_VERSION = "1.0.0"
# Define toolsMY_TOOL = ckit_cloudtool.CloudTool( strict=True, name="my_tool", description="Does something useful", parameters={ "type": "object", "properties": { "input": {"type": "string", "description": "The input to process"}, }, "required": ["input"], "additionalProperties": False, },)
TOOLS = [MY_TOOL]
async def mybot_main_loop(fclient, rcx): # Get merged setup (defaults + user overrides) setup = ckit_bot_exec.official_setup_mixing_procedure( mybot_install.mybot_setup_schema, rcx.persona.persona_setup )
# Tool handler @rcx.on_tool_call(MY_TOOL.name) async def handle_my_tool(toolcall, args): result = process_input(args["input"]) return f"Processed: {result}"
# Event handlers (optional) @rcx.on_updated_message async def on_message(msg): pass # React to new messages
@rcx.on_updated_task async def on_task(task): pass # React to kanban task changes
# Main loop try: while not ckit_shutdown.shutdown_event.is_set(): await rcx.unpark_collected_events(sleep_if_no_work=10.0) finally: # Cleanup pass
def main(): scenario_fn = ckit_bot_exec.parse_bot_args() fclient = ckit_client.FlexusClient( ckit_client.bot_service_name(BOT_NAME, BOT_VERSION), endpoint="/v1/jailed-bot" )
asyncio.run(ckit_bot_exec.run_bots_in_this_group( fclient, marketable_name=BOT_NAME, marketable_version_str=BOT_VERSION, bot_main_loop=mybot_main_loop, inprocess_tools=TOOLS, scenario_fn=scenario_fn, install_func=mybot_install.install, ))
if __name__ == "__main__": main()Changes require: Bot restart (automatic in dev mode)
mybot_prompts.py — System Prompts
Contains system prompts for each expert. Keeping prompts separate makes them easier to maintain and version.
from flexus_simple_bots import prompts_common
SYSTEM_PROMPT_DEFAULT = """You are MyBot, a helpful assistant that...
## Your Capabilities
- You can process user input using the `my_tool` tool- You can read and write policy documents
## How to Work
1. When the user asks for help, use your tools2. Always explain what you're doing3. If unsure, ask for clarification
""" + prompts_common.PROMPT_POLICY_DOCUMENTS + prompts_common.PROMPT_KANBAN
# Additional expert prompts (if needed)SYSTEM_PROMPT_ANALYST = """You are an analytical expert that..."""Changes require: Reinstallation (python mybot_install.py --ws=xxx)
mybot_install.py — Marketplace Registration
Defines everything needed to register the bot in the marketplace:
import jsonfrom flexus_client_kit import ckit_bot_installfrom flexus_client_kit.ckit_bot_install import FMarketplaceExpertInputfrom flexus_simple_bots import prompts_commonfrom mybot import mybot_bot, mybot_prompts
# Setup schema — defines configurable optionsmybot_setup_schema = { "API_KEY": { "bs_type": "string_long", "bs_default": "", "bs_group": "api", "bs_description": "Your API key for the external service", }, "auto_process": { "bs_type": "bool", "bs_default": True, "bs_group": "behavior", "bs_description": "Automatically process incoming tasks", },}
# Tools as JSON for expertstools_json = json.dumps([t.openai_style_tool() for t in mybot_bot.TOOLS])
def install(ws_id: str): return ckit_bot_install.marketplace_upsert_dev_bot( ws_id=ws_id, marketable_name="mybot", marketable_version="1.0.0", marketable_title1="My Bot", marketable_title2="A helpful bot that does useful things", marketable_occupation="Assistant", marketable_author="Your Name", marketable_description="""My Bot is designed to help you with...
## Features- Feature 1- Feature 2
## Requirements- API key from service X""", marketable_typical_group="Assistants / General", marketable_tags=["helper", "automation"], marketable_setup_schema=json.dumps(mybot_setup_schema), marketable_picture_big_b64=ckit_bot_install.load_image_b64(__file__, "mybot-1024x1536.webp"), marketable_picture_small_b64=ckit_bot_install.load_image_b64(__file__, "mybot-256x256.webp"), marketable_experts=[ ("default", FMarketplaceExpertInput( fexp_system_prompt=mybot_prompts.SYSTEM_PROMPT_DEFAULT, fexp_app_capture_tools=tools_json, )), ], marketable_schedule=[ prompts_common.SCHED_TASK_SORT_10M, prompts_common.SCHED_TODO_5M, ], marketable_forms=ckit_bot_install.load_form_bundles(__file__), # Load forms/ directory )
if __name__ == "__main__": ckit_bot_install.main_install_dev_bot(install)Changes require: Reinstallation
Image Files
Two images are required:
| File | Size | Purpose |
|---|---|---|
mybot-1024x1536.webp | 1024x1536 px | Marketplace detail card |
mybot-256x256.webp | 256x256 px | Avatar in sidebar, chat |
- Format must be WEBP
- Sizes must be exact (1024x1536 and 256x256)
- Big image should be under 300KB
- Avatar should have transparent or white background
forms/ Directory (Optional)
Custom HTML forms for editing policy documents:
forms/ report.html # Form for {"report": {"meta": {...}, ...}} documents settings.html # Form for {"settings": {"meta": {...}, ...}} documentsForm filename must match the document’s top-level key.
See Custom Forms for details.
Change Summary
| File | When Changed | Action Required |
|---|---|---|
_bot.py | Tool handlers, main loop | Restart bot |
_prompts.py | System prompts | Reinstall |
_install.py | Setup schema, experts, schedules | Reinstall |
| Images | Any change | Reinstall |
forms/*.html | Form templates | Reinstall |
Development Workflow
-
Start developing:
Terminal window # Install to marketplace firstpython mybot_install.py --ws=your_workspace_id# Then run the botpython mybot_bot.py -
Make runtime changes (
_bot.py):- Bot auto-restarts in dev mode
- Or manually restart: Ctrl+C and run again
-
Make prompt/config changes (
_prompts.py,_install.py):Terminal window # Reinstall to update marketplace/expertspython mybot_install.py --ws=your_workspace_id -
Test with scenarios:
Terminal window python mybot_bot.py --scenario mybot/mybot__s1.yaml
Next Steps
- Hello World Bot — Create your first working bot
- Tools — Deep dive into tool definitions
- Setup Schema — Configure user options