ReMe/reme/components/as_llm/openai_as_llm.py
jinliyl 54fa3381d7
Some checks failed
CI and Release / Docker / Build and test / amd64 (push) Has been cancelled
CI and Release / Docker / Build and test / arm64 (push) Has been cancelled
Security / CodeQL / Analyze python (push) Has been cancelled
CI / Python quality / Pre-commit (push) Has been cancelled
CI / Python tests / Unit Tests - py3.12 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.13 (push) Has been cancelled
CI / Python tests / Unit Tests - py3.14 (push) Has been cancelled
CI / Python packages / Build and verify distributions (push) Has been cancelled
CI / Python quality / GitHub Actions (push) Has been cancelled
CI / Python tests / Hermes embedded - ubuntu-latest (push) Has been cancelled
CI / Python tests / Hermes embedded - windows-latest (push) Has been cancelled
CI / Python tests / Unit Tests - py3.11 (push) Has been cancelled
CI / ReMe Studio / Studio checks (push) Has been cancelled
Deploy / Documentation / Build documentation (push) Has been cancelled
CI / Windows / CLI smoke - py3.11 (push) Has been cancelled
Security / CodeQL / Analyze javascript-typescript (push) Has been cancelled
CI and Release / Docker / Publish multi-platform tags (push) Has been cancelled
Deploy / Documentation / deploy (push) Has been cancelled
refactor(core): reorganize the ReMe runtime and shared helpers (#769)
* refactor(core): reorganize the ReMe runtime and shared helpers

* test(status): import the base component from its canonical module

* test: remove redundant unit checks

* refactor(core): use canonical runtime imports and shared CLI discovery

Restore evolve behavior to the base branch and update its imports. Use Application as the runtime entry point and encode SSE in the HTTP service.

* fix(cli): honor caller config when service replay is incomplete

Use partial startup overrides for port probes and resolve named configs before requesting the service process working directory.
2026-11-20 22:06:46 +08:00

53 lines
1.8 KiB
Python

"""OpenAIAsLLM provider backend."""
import asyncio
from agentscope.model import ChatModelBase
from agentscope.credential import OpenAICredential
from .base_as_llm import BaseAsLLM
from ...core.registry import R
@R.register("openai")
class OpenAIAsLLM(BaseAsLLM):
"""OpenAI chat model wrapper."""
credential_cls = OpenAICredential
_protect_owned_model_lifecycle = True
@BaseAsLLM.model.setter
def model(self, value: ChatModelBase | None) -> None:
"""Reject replacement while an internally owned client is still live."""
if value is self._model:
return
if self._owns_model:
raise RuntimeError("Close the OpenAI component before replacing its owned model.")
BaseAsLLM.model.fset(self, value)
async def _close(self) -> None:
"""Close an internally owned async client without losing cancellation-safe cleanup."""
if not self._owns_model or self._model is None:
return
cleanup = asyncio.create_task(self._release_model())
cancellation: asyncio.CancelledError | None = None
while not cleanup.done():
try:
await asyncio.shield(cleanup)
except asyncio.CancelledError as exc:
if cancellation is None:
cancellation = exc
cleanup.result()
if cancellation is not None:
raise cancellation
async def _release_model(self) -> None:
"""Release the owned model and clear state even when provider close fails."""
model = self._model
try:
client = getattr(model, "client", None)
if client is not None:
await client.close()
finally:
self._model = None
self._owns_model = False