feat: manual full-pass sync Langflow -> files -> DB+LFX with admin guide
This commit is contained in:
parent
acea6ae624
commit
1326cece42
11 changed files with 284 additions and 41 deletions
|
|
@ -2,16 +2,60 @@
|
|||
|
||||
from __future__ import annotations
|
||||
|
||||
from pathlib import Path
|
||||
|
||||
from sqlmodel import Session
|
||||
|
||||
from app.models import FlowDefinition, FlowVersion
|
||||
from app.settings import settings
|
||||
|
||||
from . import export, sync
|
||||
from .langflow_client import LangflowClient
|
||||
from .langflow_client import LangflowClient, LangflowError
|
||||
from .lfx_client import LfxClient
|
||||
|
||||
|
||||
def normalize_exported_flow(exported: dict, langflow_flow_id: str, name: str | None = None) -> dict:
|
||||
"""Reduce a Langflow FlowRead to the canonical fields we persist."""
|
||||
flow_name = name or exported.get("name") or f"langflow-{langflow_flow_id}"
|
||||
return {
|
||||
"id": exported.get("id", langflow_flow_id),
|
||||
"name": flow_name,
|
||||
"description": exported.get("description") or "",
|
||||
"endpoint_name": exported.get("endpoint_name"),
|
||||
"data": exported.get("data", {}),
|
||||
"tags": exported.get("tags", []),
|
||||
"webhook": exported.get("webhook", False),
|
||||
"is_component": exported.get("is_component", False),
|
||||
}
|
||||
|
||||
|
||||
async def pull_all_from_langflow(
|
||||
langflow: LangflowClient, flows_dir: str | Path
|
||||
) -> dict[str, list[str] | dict[str, str]]:
|
||||
"""Export every Langflow flow to its canonical file.
|
||||
|
||||
Returns {"pulled": [...stems], "errors": {langflow_id: reason}}.
|
||||
Per-flow failures are collected, never raised; listing failures raise
|
||||
(caller maps them to 503/502).
|
||||
"""
|
||||
flows_dir = Path(flows_dir)
|
||||
headers = await langflow.list_flows()
|
||||
pulled: list[str] = []
|
||||
errors: dict[str, str] = {}
|
||||
for header in headers:
|
||||
flow_id = str(header.get("id", ""))
|
||||
if not flow_id:
|
||||
continue
|
||||
try:
|
||||
exported = await langflow.export_flow(flow_id)
|
||||
doc = normalize_exported_flow(exported, flow_id)
|
||||
path = export.write_flow_file(flows_dir, doc, name=doc["name"])
|
||||
pulled.append(path.stem)
|
||||
except (LangflowError, OSError, ValueError) as exc:
|
||||
errors[flow_id] = str(exc)
|
||||
return {"pulled": sorted(pulled), "errors": errors}
|
||||
|
||||
|
||||
async def import_from_langflow(
|
||||
session: Session,
|
||||
langflow: LangflowClient,
|
||||
|
|
@ -28,18 +72,8 @@ async def import_from_langflow(
|
|||
truth; sync_document() derives the definition + version from it.
|
||||
"""
|
||||
exported = await langflow.export_flow(langflow_flow_id)
|
||||
flow_name = name or exported.get("name") or f"langflow-{langflow_flow_id}"
|
||||
langflow_json = {
|
||||
"id": exported.get("id", langflow_flow_id),
|
||||
"name": flow_name,
|
||||
"description": exported.get("description") or "",
|
||||
"endpoint_name": exported.get("endpoint_name"),
|
||||
"data": exported.get("data", {}),
|
||||
"tags": exported.get("tags", []),
|
||||
"webhook": exported.get("webhook", False),
|
||||
"is_component": exported.get("is_component", False),
|
||||
}
|
||||
path = export.write_flow_file(settings.flows_dir, langflow_json, name=flow_name)
|
||||
langflow_json = normalize_exported_flow(exported, langflow_flow_id, name=name)
|
||||
path = export.write_flow_file(settings.flows_dir, langflow_json, name=langflow_json["name"])
|
||||
definition, version, _action = await sync.sync_document(
|
||||
session,
|
||||
lfx,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue