oclaw/runtime/extensions/image-generation-core/api.py
oliver 4a23b715a2 重构仓库目录为统一的 runtime 分层并清理历史 openclaw 残留。
本次迁移将网关/通道/工具/技能/脚本与协议资源集中到新结构,统一路径常量与脚本转发机制,减少顶层噪音并保证运行与测试行为一致。

Made-with: Cursor
2026-04-25 01:24:23 +08:00

40 lines
1.4 KiB
Python

from __future__ import annotations
from typing import Any
def _runtime_get(runtime: Any, key: str):
if isinstance(runtime, dict):
return runtime.get(key)
return getattr(runtime, key, None)
def list_runtime_image_generation_providers(runtime: Any = None) -> list[dict]:
providers = _runtime_get(runtime, "image_generation_providers")
if isinstance(providers, list):
return [p for p in providers if isinstance(p, dict)]
return []
def generate_image(*, prompt: str, provider_id: str | None = None, runtime: Any = None, **kwargs) -> dict:
providers = list_runtime_image_generation_providers(runtime)
if not providers:
return {"ok": False, "error": "no_image_generation_provider_registered"}
chosen = None
if provider_id:
chosen = next((p for p in providers if str(p.get("id")) == provider_id), None)
if chosen is None:
chosen = providers[0]
generator = chosen.get("generate")
if callable(generator):
result = generator(prompt=prompt, **kwargs)
if isinstance(result, dict):
return {"ok": True, "provider": chosen.get("id"), **result}
return {"ok": True, "provider": chosen.get("id"), "result": result}
return {
"ok": True,
"provider": chosen.get("id"),
"prompt": prompt,
"note": "provider_has_no_generate_callable",
}