feat: add stateful responses API

This commit is contained in:
Xin Wang
2026-09-07 00:15:32 +08:00
parent ab409ba9c0
commit 43f2cc445e
22 changed files with 2461 additions and 5 deletions
+22
View File
@@ -83,6 +83,7 @@ class PromptBrain(BaseBrain):
self._startup_failed = False
self._greeting_pending = False
self._entry_dispatched = False
self._restored_text_state: dict[str, Any] | None = None
async def greeting(self, cfg: AssistantConfig) -> str:
# The built-in opening Message owns the greeting so speech and the
@@ -127,6 +128,12 @@ class PromptBrain(BaseBrain):
self._startup_failed = False
self._greeting_pending = False
self._entry_dispatched = False
if self._restored_text_state is not None:
state = self._restored_text_state
self._preflight_finished = bool(state.get("preflight_finished"))
self._opening_finished = bool(state.get("opening_finished"))
self._opening_started = self._opening_finished
self._entry_dispatched = bool(state.get("entry_dispatched"))
schemas: list[FunctionSchema] = []
registered_names: set[str] = set()
binding_by_id = {binding.tool_id: binding for binding in cfg.tool_bindings}
@@ -171,6 +178,21 @@ class PromptBrain(BaseBrain):
)
runtime.set_tools(schemas)
def restore_text_state(self, snapshot: dict[str, Any]) -> None:
"""Restore Prompt startup progress for a later Responses API turn."""
if snapshot.get("type") != "prompt":
raise ValueError("Responses runtime state does not belong to a prompt")
self._restored_text_state = dict(snapshot)
def text_state_snapshot(self) -> dict[str, Any]:
"""Prevent completed startup actions from running again next turn."""
return {
"type": "prompt",
"preflight_finished": self._preflight_finished,
"opening_finished": self._opening_finished,
"entry_dispatched": self._entry_dispatched,
}
async def setup_realtime(
self,
cfg: AssistantConfig,