123 行
4.6 KiB
Python
123 行
4.6 KiB
Python
"""Prompt expansion service for generation requests."""
|
|
|
|
from __future__ import annotations
|
|
|
|
from typing import Any
|
|
|
|
from backend.dto.profile_dto import CharacterProfile
|
|
from backend.dto.workflow_dto import WorkflowNode
|
|
from backend.services.node_injection_template_service import (
|
|
build_node_injection_contract,
|
|
injection_payload_from_generation_request,
|
|
)
|
|
from core.prompt_pipeline import merge_prompt_sections
|
|
|
|
|
|
class PromptExpansionService:
|
|
def expand_payload(self, payload: dict[str, Any]) -> dict[str, Any]:
|
|
sections = payload.get("sections")
|
|
if not isinstance(sections, list):
|
|
sections = [payload]
|
|
result = merge_prompt_sections(*[section for section in sections if isinstance(section, dict)])
|
|
return {
|
|
**result.to_dict(),
|
|
"source": "payload",
|
|
}
|
|
|
|
def expand_for_node(
|
|
self,
|
|
profile: CharacterProfile,
|
|
node: WorkflowNode,
|
|
payload: dict[str, Any],
|
|
) -> dict[str, Any]:
|
|
dependency_context = payload.get("dependency_context") if isinstance(payload.get("dependency_context"), dict) else {}
|
|
prompt_plan = dependency_context.get("prompt_plan") if isinstance(dependency_context.get("prompt_plan"), dict) else {}
|
|
prompt_plan_generation = (
|
|
prompt_plan.get("generation_params") if isinstance(prompt_plan.get("generation_params"), dict) else {}
|
|
)
|
|
variables = {
|
|
"profile_name": profile.name,
|
|
"node_title": node.title,
|
|
**(profile.metadata.get("prompt_variables") if isinstance(profile.metadata.get("prompt_variables"), dict) else {}),
|
|
**(payload.get("variables") if isinstance(payload.get("variables"), dict) else {}),
|
|
}
|
|
result = merge_prompt_sections(
|
|
{
|
|
"parts": [profile.base_prompt, profile.appearance_prompt, profile.style_prompt],
|
|
"negative_parts": [profile.negative_prompt],
|
|
"variables": variables,
|
|
},
|
|
{
|
|
"parts": [node.prompt],
|
|
"negative_parts": [node.negative_prompt],
|
|
"variables": variables,
|
|
},
|
|
{
|
|
"parts": _as_list(prompt_plan_generation.get("prompt_patch")),
|
|
"negative_parts": _as_list(prompt_plan_generation.get("negative_patch")),
|
|
"variables": variables,
|
|
},
|
|
{
|
|
"parts": _payload_parts(payload),
|
|
"negative_parts": _payload_negative_parts(payload),
|
|
"variables": variables,
|
|
},
|
|
)
|
|
injection_payload = injection_payload_from_generation_request(payload)
|
|
dependency_injection = (
|
|
dependency_context.get("injection_template")
|
|
if isinstance(dependency_context.get("injection_template"), dict)
|
|
else {}
|
|
)
|
|
material_bindings = (
|
|
injection_payload["material_bindings"]
|
|
or dependency_context.get("material_bindings")
|
|
or dependency_injection.get("material_bindings")
|
|
or []
|
|
)
|
|
node_injection = build_node_injection_contract(
|
|
node,
|
|
requested_kind=injection_payload["requested_kind"],
|
|
user_instruction=injection_payload["user_instruction"],
|
|
dependency_context=dependency_context,
|
|
material_bindings=material_bindings,
|
|
prompt_plan=prompt_plan or dependency_injection.get("prompt_plan") or {},
|
|
stage_inputs=injection_payload["stage_inputs"],
|
|
selected_draft_image_ids=injection_payload["selected_draft_image_ids"],
|
|
ui_context=injection_payload["ui_context"],
|
|
)
|
|
return {
|
|
**result.to_dict(),
|
|
"source": "profile_node",
|
|
"profile_id": profile.profile_id,
|
|
"node_id": node.node_id,
|
|
"node_injection": node_injection,
|
|
}
|
|
|
|
|
|
def _payload_parts(payload: dict[str, Any]) -> list[Any]:
|
|
parts: list[Any] = []
|
|
parts.extend(_as_list(payload.get("prompt_parts")))
|
|
for key in ("prompt_patch", "extra_prompt", "style_prompt"):
|
|
value = payload.get(key)
|
|
if value:
|
|
parts.append(value)
|
|
return parts
|
|
|
|
|
|
def _payload_negative_parts(payload: dict[str, Any]) -> list[Any]:
|
|
parts: list[Any] = []
|
|
parts.extend(_as_list(payload.get("negative_parts")))
|
|
for key in ("negative_prompt", "negative_prompt_patch"):
|
|
value = payload.get(key)
|
|
if value:
|
|
parts.append(value)
|
|
return parts
|
|
|
|
|
|
def _as_list(value: Any) -> list[Any]:
|
|
if value is None:
|
|
return []
|
|
if isinstance(value, list):
|
|
return value
|
|
return [value]
|