fix: move draft and template context handling out of create_workflow_run

create_workflow_run now only creates
the run with the definition_id and initial_context provided by the caller. Test call paths explicitly resolve draft definitions
and merge template context variables before creating the run, while production/runtime paths bind the published definition
without adding template defaults.
This commit is contained in:
Sabiha Khan 2026-07-18 19:00:32 +05:30
parent f69ab294af
commit c2541d088b
18 changed files with 552 additions and 75 deletions

View file

@ -25,6 +25,7 @@ from api.services.workflow.text_chat_session_service import (
normalize_text_chat_session_data,
rewind_text_chat_session_state,
)
from api.services.workflow.run_creation import prepare_workflow_run_inputs
router = APIRouter(prefix="/workflow", tags=["workflow-text-chat"])
@ -164,14 +165,26 @@ async def create_text_chat_session(
) -> WorkflowRunTextSessionResponse:
session_name = request.name or f"WR-TEXT-{uuid4().hex[:6].upper()}"
try:
workflow = await db_client.get_workflow(
workflow_id, organization_id=user.selected_organization_id
)
if not workflow:
raise ValueError(f"Workflow with ID {workflow_id} not found")
run_inputs = await prepare_workflow_run_inputs(
db_client,
workflow,
initial_context=request.initial_context,
use_draft=True,
include_template_context=True,
)
workflow_run = await db_client.create_workflow_run(
name=session_name,
workflow_id=workflow_id,
mode=WorkflowRunMode.TEXTCHAT.value,
user_id=user.id,
initial_context=request.initial_context,
use_draft=True,
initial_context=run_inputs.initial_context,
organization_id=user.selected_organization_id,
definition_id=run_inputs.definition_id,
)
except ValueError as e:
raise HTTPException(status_code=404, detail=str(e))