refactor: form-core wiring, JWT sessions, slowapi, packaging, frontend rewrite, test declutter

This commit is contained in:
Jonas Miederer 2026-09-28 18:03:07 +02:00
parent 06ab25fb47
commit 03af41e837
74 changed files with 6089 additions and 5183 deletions

View file

@ -11,10 +11,11 @@ from app (stdlib only), prints no secret values.
from __future__ import annotations
import argparse
import sys
from pathlib import Path
import typer
BACKEND = Path(__file__).resolve().parent.parent
REPO_ROOT = BACKEND.parent
@ -57,6 +58,7 @@ def check(values: dict[str, str]) -> tuple[list[str], list[str]]:
# Shared + service API keys.
secret("CALLBACK_TOKEN", MIN_TOKEN, "callback token")
secret("JWT_SECRET", MIN_TOKEN, "session JWT secret")
secret("LANGFLOW_API_KEY", MIN_API_KEY, "langflow api key")
placeholder("LANGFLOW_API_KEY", ("sk-",))
secret("LFX_API_KEY", MIN_API_KEY, "lfx api key")
@ -87,11 +89,9 @@ def check(values: dict[str, str]) -> tuple[list[str], list[str]]:
return failures, warnings
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--env", default=str(REPO_ROOT / ".env"))
args = parser.parse_args()
path = Path(args.env)
def main(env: str = typer.Option(str(REPO_ROOT / ".env"), "--env")) -> int:
"""Validate a prod .env file (typer CLI; check/parse_env stay importable)."""
path = Path(env)
if not path.is_file():
print(f"no env file at {path}")
return 1
@ -108,4 +108,4 @@ def main() -> int:
if __name__ == "__main__":
sys.exit(main())
sys.exit(typer.run(main))

View file

@ -7,11 +7,11 @@ Usage:
from __future__ import annotations
import argparse
import json
import sys
import httpx
import typer
def post(api: str, path: str, body: dict) -> dict:
@ -26,23 +26,22 @@ def post(api: str, path: str, body: dict) -> dict:
return {"status": 0, "body": str(exc)[:2000]}
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument("--api", default="http://localhost:8000")
parser.add_argument("--langflow-id", required=True)
parser.add_argument("--name", default=None)
parser.add_argument("--author", default="")
parser.add_argument("--no-deploy", action="store_true")
args = parser.parse_args()
def main(
langflow_id: str = typer.Option(..., "--langflow-id"),
api: str = typer.Option("http://localhost:8000", "--api"),
name: str | None = typer.Option(None, "--name"),
author: str = typer.Option("", "--author"),
no_deploy: bool = typer.Option(False, "--no-deploy"),
) -> int:
"""Import a flow from Langflow via the running API (post() stays importable)."""
result = post(
args.api,
api,
"/api/flows/import",
{
"langflow_flow_id": args.langflow_id,
"name": args.name,
"author": args.author,
"deploy": not args.no_deploy,
"langflow_flow_id": langflow_id,
"name": name,
"author": author,
"deploy": not no_deploy,
},
)
print(json.dumps(result, indent=2, ensure_ascii=False))
@ -50,4 +49,4 @@ def main() -> int:
if __name__ == "__main__":
sys.exit(main())
sys.exit(typer.run(main))

View file

@ -29,6 +29,74 @@ FLOW_DIR = REPO_ROOT / "backend" / "flows"
HANDLE_QUOTE = "\u0153" # ReactFlow handle-id quoting used by Langflow exports
FORM_CORE_PATH = REPO_ROOT / "backend" / "components" / "kirche" / "_form_core.py"
INLINE_MARKER = "# <-- _form_core inlined by scripts/make_flow_fixtures.py:kirche_node()"
def _inline_core() -> str:
"""Body of _form_core.py minus docstring/future/std-imports (already present)."""
text = FORM_CORE_PATH.read_text(encoding="utf-8")
lines = text.splitlines()
body: list[str] = []
skip_docstring = True
in_docstring = False
for line in lines:
stripped = line.strip()
if skip_docstring:
if stripped.startswith('"""') and not in_docstring:
if stripped.count('"""') >= 2:
skip_docstring = False
else:
in_docstring = True
continue
if in_docstring:
if '"""' in stripped:
in_docstring = False
skip_docstring = False
continue
if stripped in (
"from __future__ import annotations",
"import json",
"import re",
"",
):
continue
skip_docstring = False
if stripped in ("from __future__ import annotations", "import json", "import re"):
continue
body.append(line)
return "\n".join(body).strip() + "\n"
def _compose_component_code(source: str) -> str:
"""Replace the sibling import + marker with inlined core (LFX self-contained)."""
core = _inline_core()
out: list[str] = []
skipping_import = False
for line in source.splitlines():
stripped = line.strip()
if not skipping_import and stripped.startswith(
("from kirche._form_core import", "from ._form_core import")
):
out.append(core.rstrip())
# Multi-line parenthesized import: skip continuation lines
# up to and including the closing paren.
if "(" in stripped and ")" not in stripped:
skipping_import = True
continue
if skipping_import:
if ")" in stripped:
skipping_import = False
continue
if "_form_core inlined by" in stripped:
continue
out.append(line)
composed = "\n".join(out)
assert "from kirche" not in composed, "sibling import survived inlining"
assert "from ._" not in composed, "relative import survived inlining"
return composed
# --------------------------------------------------------------------------
# node builders
@ -118,7 +186,7 @@ def kirche_node(
comp_name = getattr(component_cls, "name", None) or component_cls.__name__
inst = component_cls()
if code_override is not None:
inst._code = code_override
inst._code = _compose_component_code(code_override)
frontend_node = ComponentFrontendNode.from_inputs(**inst.get_template_config(inst))
inst._map_parameters_on_frontend_node(frontend_node)
node = frontend_node.to_dict(keep_name=False)
@ -126,6 +194,10 @@ def kirche_node(
inst._map_parameters_on_template(node["template"])
if not getattr(inst, "_code", None):
inst.set_class_code()
# Always compose: default path may already carry thin source with the
# sibling import (Component base sets _code on init); override path is
# pre-composed but re-composing is idempotent (no import lines left).
inst._code = _compose_component_code(inst._code)
node["template"]["code"] = {
"advanced": True,
"api_editable": False,

View file

@ -37,6 +37,9 @@ LFX_PORT = _free_port()
API_PORT = _free_port()
LFX_KEY = "lfx-759GsfJKjAeOBZ_44-cll9WdHp_yfYn_tIg7eHG38Rs"
# Stateless admin session JWT, minted in main() after planting the user.
_SMOKE_JWT = ""
def wait_healthy(url: str, timeout: int = 180) -> None:
deadline = time.time() + timeout
@ -54,9 +57,9 @@ def api(method: str, path: str, body: dict | None = None) -> tuple[int, dict | s
import httpx
with httpx.Client(base_url=f"http://127.0.0.1:{API_PORT}", timeout=120) as c:
# Management + runs are login-gated; the smoke admin session is
# planted directly in the throwaway DB (see main()).
c.cookies.set("kirche_session", "smoke-session", domain="127.0.0.1")
# Management + runs are login-gated; the smoke admin JWT is minted
# in main() (stateless sessions, see app/auth/service.py).
c.cookies.set("kirche_session", _SMOKE_JWT, domain="127.0.0.1")
r = c.request(method, path, json=body)
try:
return r.status_code, r.json()
@ -90,27 +93,31 @@ def main() -> None:
app_settings.database_url = db_url
alembic_command.upgrade(cfg, "head")
# Plant an admin session: management + runs require login, and there is
# no SMTP in smoke to deliver a magic link.
from datetime import UTC, datetime, timedelta
# Plant an admin user + mint its session JWT: management + runs
# require login, and there is no SMTP in smoke to deliver a magic link.
# Sessions are stateless (see app/auth/service.py) — the cookie value
# below is the JWT sent as `kirche_session`.
global _SMOKE_JWT
import os
from sqlmodel import Session, create_engine
from app.models import User, UserSession
from app.auth import service as auth_service
from app.models import User
from app.settings import settings as app_settings
if not app_settings.jwt_secret:
app_settings.jwt_secret = os.environ.get(
"JWT_SECRET", "smoke-jwt-secret-32-chars-minimum!!"
)
engine = create_engine(db_url)
with Session(engine) as s:
user = User(name="smoke", email="smoke@example.com", role="admin")
s.add(user)
s.flush()
s.add(
UserSession(
id="smoke-session",
user_id=user.id,
expires_at=datetime.now(UTC) + timedelta(hours=1),
)
)
s.commit()
s.refresh(user)
_SMOKE_JWT = auth_service.mint_session_token(user)
print("smoke admin JWT minted")
engine.dispose()
env = {

View file

@ -9,12 +9,12 @@ Exit codes: 0 ok, 1 sync errors, 2 flows dir missing.
from __future__ import annotations
import argparse
import asyncio
import json
import sys
from pathlib import Path
import typer
from sqlmodel import Session
from app.db import get_engine, reset_engine
@ -80,18 +80,17 @@ async def main_async(args) -> int:
return 0
def main() -> int:
parser = argparse.ArgumentParser(description=__doc__)
parser.add_argument(
"--pull", action="store_true", help="export all Langflow flows to backend/flows first"
)
parser.add_argument("--sync", action="store_true", help="reconcile backend/flows into DB + LFX")
parser.add_argument("--dir", default=None, help="flows directory (default: settings.flows_dir)")
args = parser.parse_args()
if not args.pull and not args.sync:
args.sync = True
def main(
pull: bool = typer.Option(False, "--pull", help="export all Langflow flows first"),
sync: bool = typer.Option(False, "--sync", help="reconcile backend/flows into DB + LFX"),
dir: str | None = typer.Option(None, "--dir", help="flows directory"),
) -> int:
"""Reconcile flows (typer CLI; run_sync/run_full_pass stay importable)."""
if not pull and not sync:
sync = True
args = type("Args", (), {"pull": pull, "sync": sync, "dir": dir})()
return asyncio.run(main_async(args))
if __name__ == "__main__":
sys.exit(main())
sys.exit(typer.run(main))