39 lines
1.3 KiB
Python
39 lines
1.3 KiB
Python
# Copyright (c) 2025 Patrick Motsch
|
|
# All rights reserved.
|
|
|
|
"""Shared helpers for AI workflow actions."""
|
|
|
|
import json
|
|
from typing import Any
|
|
|
|
|
|
def serialize_context(val: Any) -> str:
|
|
"""Convert any context value to a readable string for use in AI prompts.
|
|
|
|
- None / empty string → ""
|
|
- str → as-is
|
|
- dict / list → pretty-printed JSON
|
|
- anything else → str()
|
|
"""
|
|
if val is None or val == "" or val == []:
|
|
return ""
|
|
if isinstance(val, str):
|
|
return val.strip()
|
|
try:
|
|
return json.dumps(val, ensure_ascii=False, indent=2)
|
|
except Exception:
|
|
return str(val)
|
|
|
|
|
|
def applyCommonAiParams(parameters: dict, request) -> None:
|
|
"""Apply common AI parameters (requireNeutralization, allowedModels) from node to request."""
|
|
requireNeutralization = parameters.get("requireNeutralization")
|
|
if requireNeutralization is not None:
|
|
request.requireNeutralization = bool(requireNeutralization)
|
|
|
|
allowedModels = parameters.get("allowedModels")
|
|
if allowedModels and isinstance(allowedModels, list):
|
|
if not request.options:
|
|
from modules.datamodels.datamodelAi import AiCallOptions
|
|
request.options = AiCallOptions()
|
|
request.options.allowedModels = allowedModels
|