Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/just-cameron/comind. Comind, the cognitive layer for the web. comind.stream/docs/getting-started
Something went wrong. Try again.
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638#!/usr/bin/env python3# Simple fasthtml app to monitor comind records
import osimport sysimport loggingfrom typing import Dict, List, Optional, Anyimport datetimeimport refrom atproto_client import Client as AtProtoClientfrom atproto_client import Session, SessionEventfrom dotenv import load_dotenvfrom fasthtml.common import *import json
from rich import print
# Configure logginglogging.basicConfig( level=logging.INFO, format='%(asctime)s - %(name)s - %(levelname)s - %(message)s')logger = logging.getLogger("monitor")
# Try to import comind formatter, or use local implementationtry: from comind.format import format as comind_formatexcept ImportError: # Fallback implementation if comind package is not available def comind_format(template: str, context: Dict[str, Any], safe: bool = True, default: str = "") -> str: """ Simple implementation of comind.format for use without the full package. """ if not template or not isinstance(template, str): return str(template)
result = template for key, value in context.items(): placeholder = "{" + key + "}" if placeholder in result: result = result.replace(placeholder, str(value))
# Handle any remaining placeholders with default value if safe: result = re.sub(r'\{[^{}]*\}', default, result)
return result
# Load environment variablesload_dotenv(override=True)
# Define collections to monitorCOLLECTIONS = [ "me.comind.sphere.core", "me.comind.thought", "me.comind.emotion", "me.comind.concept", "me.comind.meld.request", "me.comind.meld.response"]
# Create FastHTML appapp, rt = fast_app(debug=True) # Enable debug mode to see detailed errors
# Session reuse functionsdef get_session(username: str) -> Optional[str]: try: with open(f'session_{username}.txt', encoding='UTF-8') as f: return f.read() except FileNotFoundError: logger.debug(f"No existing session found for {username}") return None
def save_session(username: str, session_string: str) -> None: with open(f'session_{username}.txt', 'w', encoding='UTF-8') as f: f.write(session_string) logger.debug(f"Session saved for {username}")
def on_session_change(username: str, event: SessionEvent, session: Session) -> None: logger.info(f'Session changed: {event} {repr(session)}') if event in (SessionEvent.CREATE, SessionEvent.REFRESH): logger.info(f'Saving changed session for {username}') save_session(username, session.export())
def init_client(username: str, password: str) -> AtProtoClient: pds_uri = os.getenv("COMIND_PDS_URI") if pds_uri is None: logger.warning("No PDS URI provided. Falling back to bsky.social.") pds_uri = "https://bsky.social"
logger.info(f"Using PDS URI: {pds_uri}")
client = AtProtoClient(pds_uri) client.on_session_change(lambda event, session: on_session_change(username, event, session))
session_string = get_session(username) if session_string: logger.info(f'Reusing existing session for {username}') client.login(session_string=session_string) else: logger.info(f'Creating new session for {username}') client.login(username, password)
return client
def default_login() -> AtProtoClient: username = os.getenv("COMIND_BSKY_USERNAME") password = os.getenv("COMIND_BSKY_PASSWORD")
if username is None: logger.error("No username provided. Please set COMIND_BSKY_USERNAME env variable.") raise ValueError("No username provided")
if password is None: logger.error("No password provided. Please set COMIND_BSKY_PASSWORD env variable.") raise ValueError("No password provided")
return init_client(username, password)
def format_datetime(dt_str: str) -> str: """Format datetime string to more readable format""" dt = datetime.datetime.fromisoformat(dt_str.replace("Z", "+00:00")) return dt.strftime("%Y-%m-%d %H:%M:%S")
def get_recent_records(client: AtProtoClient, collection: str, limit: int = 10) -> List[Dict]: """Get recent records from a collection""" try: response = client.com.atproto.repo.list_records({ 'collection': collection, 'repo': client.me.did, 'limit': limit }) return response.records except Exception as e: logger.error(f"Error listing records in collection {collection}: {str(e)}") return []
def render_record_card(record, collection): """Render a record as a card with improved display and interactivity""" record_data = record.model_dump()['value'] uri_parts = record.uri.split('/') rkey = uri_parts[-1]
# Extract record type-specific information title = "" body = "" record_type = collection.split(".")[-1] # Extract type from collection name
# Build context for formatting context = { "record_type": record_type, "collection": collection, "rkey": rkey, "uri": record.uri, }
# Add common fields to context for key, value in record_data.items(): if isinstance(value, (str, int, float, bool)): context[key] = value
# Type-specific formatting context if collection == "me.comind.sphere.core": title_template = "{title}" body_template = "{text}" context.update({ "title": record_data.get("title", "Untitled Sphere"), "text": record_data.get("text", "") }) elif "generated" in record_data and isinstance(record_data["generated"], dict): generated = record_data["generated"] context.update(generated)
# Add generated fields to context for key, value in generated.items(): if isinstance(value, (str, int, float, bool)): context[f"generated_{key}"] = value
if collection == "me.comind.thought": title_template = "Thought: {thoughtType}" body_template = "{text}" context.update({ "thoughtType": generated.get("thoughtType", "Unknown"), "text": generated.get("text", "") }) elif collection == "me.comind.emotion": title_template = "Emotion: {emotionType}" body_template = "{text}" context.update({ "emotionType": generated.get("emotionType", "Unknown"), "text": generated.get("text", "") }) elif collection == "me.comind.concept": title_template = "{text}" body_template = "{text}" context.update({ "text": generated.get("text", "") }) elif collection == "me.comind.meld.request": title_template = "Meld Request: {requestType}" body_template = "{prompt}" context.update({ "requestType": generated.get("requestType", "Unknown"), "prompt": generated.get("prompt", "") }) elif collection == "me.comind.meld.response": title_template = "Meld Response" body_template = "{content}" context.update({ "content": generated.get("content", "") }) else: title_template = "Record: {record_type}" body_template = ""
# Format title and body using comind_format title = comind_format(title_template, context, safe=True, default="Unknown") body = comind_format(body_template, context, safe=True, default="")
# Format created date if available created_at = record_data.get("createdAt", "") created_formatted = format_datetime(created_at) if created_at else ""
# Generate a unique ID for this content content_id = f"content-{record_type}-{rkey}"
# Truncate long content with expand/collapse functionality content_div = Div( P(body, id=content_id, cls="truncated" if len(body) > 300 else ""), Button( "Show more", cls="expand-button", onclick=f""" const content = document.getElementById('{content_id}'); const btn = this; if (content.classList.contains('truncated')) {{ content.classList.remove('truncated'); btn.textContent = 'Show less'; }} else {{ content.classList.add('truncated'); btn.textContent = 'Show more'; }} """, style="display: " + ("block" if len(body) > 300 else "none") ) ) if body else Div()
# Define icon based on record type icon_map = { "sphere": "🌐", "thought": "💭", "emotion": "😊", "concept": "💡", "request": "❓", "response": "💬" } icon = icon_map.get(record_type.split(".")[-1], "📄")
return Div( H3(f"{icon} {title}"), content_div, Div( Span(f"Created: {created_formatted}"), Span(f"ID: {rkey}"), cls="card-meta" ), cls="card", hx_get=f"/record/{record_type}/{rkey}", hx_target="this", hx_swap="outerHTML", hx_trigger="dblclick" )
@rt("/")def get(): """Main page showing all recent records""" try: client = default_login()
collection_sections = []
for collection in COLLECTIONS: records = get_recent_records(client, collection, limit=5)
# Skip empty collections if not records: continue
# Create section for this collection collection_name = collection.split(".")[-1].capitalize() record_cards = [render_record_card(record, collection) for record in records]
collection_section = Div( H2(f"{collection_name} Records", cls="collection-title"), *record_cards, cls="collection-section", style="margin-bottom: 2rem;" )
collection_sections.append(collection_section)
# If no records found if not collection_sections: collection_sections = [P("No records found. Please check your credentials and collection names.", cls="no-records")]
# Refresh button with improved styling and auto-refresh option refresh_controls = Div( Button( Span("↻ Refresh", cls="refresh-text"), cls="refresh-button", hx_get="/", hx_swap="outerHTML", hx_target="#content" ), Div( Input(type="checkbox", id="auto-refresh", name="auto-refresh"), Label("Auto-refresh (30s)", for_="auto-refresh"), style="margin-left: 1rem; display: inline-flex; align-items: center;" ), Script(""" document.getElementById('auto-refresh').addEventListener('change', function() { if (this.checked) { window.autoRefreshInterval = setInterval(function() { document.querySelector('.refresh-button').click(); }, 30000); } else { clearInterval(window.autoRefreshInterval); } }); """), style="display: flex; align-items: center;" )
return Title("Comind Record Monitor"), Div( Style(""" :root { --primary-color: #4a6baf; --bg-color: #f5f7fa; --card-bg: #ffffff; --text-color: #333; --border-color: #e0e0e0; --highlight: #617ec2; --muted-text: #666; } body { font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif; background-color: var(--bg-color); color: var(--text-color); line-height: 1.6; padding: 0; margin: 0; } .container { max-width: 1200px; margin: 0 auto; padding: 2rem; } .header { display: flex; justify-content: space-between; align-items: center; margin-bottom: 2rem; border-bottom: 1px solid var(--border-color); padding-bottom: 1rem; } h1 { color: var(--primary-color); margin: 0; } .collection-title { color: var(--primary-color); border-bottom: 2px solid var(--border-color); padding-bottom: 0.5rem; margin-top: 2rem; } .card { background: var(--card-bg); border-radius: 8px; box-shadow: 0 2px 10px rgba(0,0,0,0.05); margin-bottom: 1.5rem; padding: 1.5rem; transition: transform 0.2s, box-shadow 0.2s; border-left: 4px solid var(--primary-color); } .card:hover { transform: translateY(-2px); box-shadow: 0 4px 15px rgba(0,0,0,0.1); } .card h3 { color: var(--primary-color); margin-top: 0; } .card-meta { color: var(--muted-text); font-size: 0.85rem; margin-top: 1rem; display: flex; justify-content: space-between; } .refresh-button { background-color: var(--primary-color); color: white; border: none; padding: 8px 16px; border-radius: 4px; cursor: pointer; font-weight: bold; transition: background-color 0.2s; } .refresh-button:hover { background-color: var(--highlight); } .nav-tabs { display: flex; gap: 0.5rem; margin-bottom: 2rem; flex-wrap: wrap; } .nav-tab { padding: 0.5rem 1rem; background-color: #e0e0e0; border-radius: 4px; text-decoration: none; color: var(--text-color); font-weight: 500; transition: background-color 0.2s; } .nav-tab:hover, .nav-tab.active { background-color: var(--primary-color); color: white; } .expand-button { background: none; border: none; color: var(--primary-color); cursor: pointer; font-size: 0.9rem; padding: 0; margin-top: 0.5rem; text-decoration: underline; } .truncated { max-height: 100px; overflow: hidden; position: relative; } .truncated::after { content: ''; position: absolute; bottom: 0; left: 0; width: 100%; height: 30px; background: linear-gradient(transparent, var(--card-bg)); } @media (max-width: 768px) { .container { padding: 1rem; } .header { flex-direction: column; align-items: flex-start; gap: 1rem; } .nav-tabs { width: 100%; overflow-x: auto; } } """), Div( H1("Comind Record Monitor"), refresh_controls, cls="header" ), Div( *[A(collection.split(".")[-1].capitalize(), href=f"/{collection.split('.')[-1]}", cls="nav-tab") for collection in COLLECTIONS], cls="nav-tabs" ), Div(*collection_sections, id="content"), cls="container" ) except Exception as e: raise e return Title("Error"), Div( H1("Error"), P(f"An error occurred: {str(e)}"), cls="container" )
@rt("/{collection}")def get_collection(collection: str): """View records for a specific collection""" try: client = default_login() records = get_recent_records(client, f"me.comind.{collection}", limit=10) print(records)
record_cards = [render_record_card(record, f"me.comind.{collection}") for record in records]
if not record_cards: record_cards = [P("No records found in this collection.")]
return Title(f"{collection.capitalize()} Records"), Div( H1(f"{collection.capitalize()} Records"), A("Back to All Records", href="/", cls="button"), Div(*record_cards, cls="records"), cls="container" ) except Exception as e: return Title("Error"), Div( H1("Error"), P(f"An error occurred: {str(e)}"), A("Back to All Records", href="/", cls="button"), cls="container" )
@rt("/record/{collection_type}/{rkey}")def get_record_detail(collection_type: str, rkey: str): """Display detailed information about a record""" try: client = default_login() collection = f"me.comind.{collection_type}"
# Get the specific record response = client.com.atproto.repo.get_record({ 'collection': collection, 'repo': client.me.did, 'rkey': rkey })
if not response or not hasattr(response, 'value'): return Div( H3("Record Not Found"), P(f"Could not find record with ID {rkey}"), Button("Close", onclick="this.parentElement.outerHTML = originalCardHTML;"), cls="card error-card" )
record_data = response.value
# Build context for formatting context = { "record_type": collection_type, "collection": collection, "rkey": rkey, }
# Add common fields to context for key, value in record_data.items(): if isinstance(value, (str, int, float, bool)): context[key] = value
# Type-specific formatting title_template = "Record Details" body_template = ""
if collection == "me.comind.sphere.core": title_template = "{title}" body_template = "{text}" context.update({ "title": record_data.get("title", "Untitled Sphere"), "text": record_data.get("text", "") }) elif "generated" in record_data and isinstance(record_data["generated"], dict): generated = record_data["generated"]
# Add generated fields to context for key, value in generated.items(): if isinstance(value, (str, int, float, bool)): context[f"generated_{key}"] = value context[key] = value
if collection == "me.comind.thought": title_template = "Thought: {thoughtType}" body_template = "{text}" elif collection == "me.comind.emotion": title_template = "Emotion: {emotionType}" body_template = "{text}" elif collection == "me.comind.concept": title_template = "{text}" body_template = "{text}" elif collection == "me.comind.meld.request": title_template = "Meld Request: {requestType}" body_template = "{prompt}" elif collection == "me.comind.meld.response": title_template = "Meld Response" body_template = "{content}"
# Format title and body using comind_format title = comind_format(title_template, context, safe=True, default="Unknown") body = comind_format(body_template, context, safe=True, default="")
# Format created date if available created_at = record_data.get("createdAt", "") created_formatted = format_datetime(created_at) if created_at else ""
# Store the full record JSON record_json = json.dumps(record_data, indent=2)
return Div( Script(f"const originalCardHTML = this.outerHTML;"), H3(f"📄 {title}"), P(body) if body else Div(), H4("Record Details:"), Pre(record_json, style="background: #f0f0f0; padding: 1rem; border-radius: 4px; overflow: auto; max-height: 300px;"), Div( Span(f"Created: {created_formatted}"), Span(f"ID: {rkey}"), cls="card-meta" ), Button("Close", cls="refresh-button", style="margin-top: 1rem;", onclick="this.closest('.card').outerHTML = originalCardHTML;"), cls="card detail-card", style="max-width: 100%; overflow: hidden;" )
except Exception as e: return Div( H3("Error"), P(f"An error occurred: {str(e)}"), Button("Close", cls="refresh-button", style="margin-top: 1rem;", onclick="this.closest('.card').outerHTML = originalCardHTML;"), cls="card error-card" )
# Start the app when run directlyif __name__ == "__main__": serve(port=8972)