043aa18f3f
- New api_clients + api_client_scopes tables; tokens scoped per-instance
- Admin UI tab at /admin for token create/rotate/revoke/delete with one-time reveal
- Dual-auth dependency (user session OR Bearer app token) on trigger + runs endpoints
- /api/instances/{id}/trigger pre-creates a run and returns run_id + cached last_result instantly
- New GET /api/runs/{id} for polling
- Generic trigger path for sub-agent instances (weather, calendar, etc.)
- runs.result column for structured JSON alongside markdown output
- agent_catalog.result_schema describes each agent's result shape
- Weather, daily-briefing, project-monitor retrofitted to emit structured results
- log_run: env INSTANCE_ID/RUN_ID only used when target matches, so nested sub-agents don't clobber parent runs
- Wiki docs: API Clients & Token Scoping + Calling Agents From Your Apps
38 lines
1.2 KiB
Python
38 lines
1.2 KiB
Python
from sqlalchemy import create_engine, inspect, text
|
|
from sqlalchemy.orm import sessionmaker, DeclarativeBase
|
|
import os
|
|
|
|
DB_PATH = os.environ.get("DB_PATH", "/app/data/agents.db")
|
|
engine = create_engine(f"sqlite:///{DB_PATH}", connect_args={"check_same_thread": False})
|
|
SessionLocal = sessionmaker(bind=engine)
|
|
|
|
|
|
class Base(DeclarativeBase):
|
|
pass
|
|
|
|
|
|
def get_db():
|
|
db = SessionLocal()
|
|
try:
|
|
yield db
|
|
finally:
|
|
db.close()
|
|
|
|
|
|
def _ensure_column(conn, table: str, column: str, ddl: str):
|
|
"""Add a column to an existing table if it doesn't exist (SQLite idempotent migration)."""
|
|
insp = inspect(conn)
|
|
cols = {c["name"] for c in insp.get_columns(table)}
|
|
if column not in cols:
|
|
conn.execute(text(f"ALTER TABLE {table} ADD COLUMN {column} {ddl}"))
|
|
print(f" migration: added {table}.{column}")
|
|
|
|
|
|
def init_db():
|
|
Base.metadata.create_all(bind=engine)
|
|
with engine.begin() as conn:
|
|
# Additive columns on existing tables (safe to re-run)
|
|
_ensure_column(conn, "runs", "result", "JSON")
|
|
_ensure_column(conn, "runs", "triggered_by", "VARCHAR DEFAULT ''")
|
|
_ensure_column(conn, "agent_catalog", "result_schema", "JSON")
|