-
Notifications
You must be signed in to change notification settings - Fork 21
[FEAT] Add stateful responses layer with history rehydration and DB persistence #21
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
16 commits
Select commit
Hold shift + click to select a range
9f043b5
add databse and stateful responses flow
maralbahari c46549c
update rehydration with conversation table
maralbahari 42fd05b
add unit tests
maralbahari 73da713
make conversation api presist along with responses
maralbahari 4b06d40
clean code
maralbahari 7eddfa5
update tests
maralbahari 1b182f7
update packages depencencies
maralbahari adafa76
fix unit tests and add cassettes with recoder
maralbahari cf067bf
Merge remote-tracking branch 'origin/main' into responses-flow
maralbahari 41aa568
get dialect from db url and add validators
maralbahari dd462b2
fix db url validator
maralbahari 1120a5f
redesign conversation db and resolve Critical: Lost-update race in pu…
maralbahari 373ea72
session_factory_engine unconditional replacement
maralbahari eeafb8b
fix exception catch and missing event in sse
maralbahari d0c6b74
add todo comment
maralbahari 155e8b1
add TODO for proxy router
maralbahari File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -16,3 +16,10 @@ build/ | |
| .vscode/ | ||
| *.swp | ||
| *.swo | ||
|
|
||
| # DB | ||
| *.db | ||
| *.db-shm | ||
| *db-wal | ||
|
|
||
| *.log | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,309 @@ | ||
| from __future__ import annotations | ||
|
|
||
| from collections.abc import Iterable | ||
| from dataclasses import dataclass | ||
| from time import time | ||
|
|
||
| from agentic_api.core.normalized_events import ( | ||
| FunctionCallArgumentsDelta, | ||
| FunctionCallDone, | ||
| FunctionCallStarted, | ||
| MessageDelta, | ||
| MessageDone, | ||
| MessageStarted, | ||
| NormalizedEvent, | ||
| ReasoningDelta, | ||
| ReasoningDone, | ||
| ReasoningStarted, | ||
| UsageFinal, | ||
| ) | ||
| from agentic_api.types.responses import ( | ||
| ContentPartEvent, | ||
| ErrorEvent, | ||
| FunctionToolCall, | ||
| IncompleteDetails, | ||
| InputTokenDetails, | ||
| OutputItemEvent, | ||
| OutputMessage, | ||
| OutputTextContent, | ||
| OutputTokenDetails, | ||
| ResponseEvent, | ||
| ResponseUsage, | ||
| ResponsesResponse, | ||
| StreamEvent, | ||
| TextDeltaEvent, | ||
| ) | ||
| from agentic_api.utils.common import uuid7_str | ||
|
|
||
|
|
||
| @dataclass(slots=True) | ||
| class _ItemState: | ||
| item_id: str | ||
| output_index: int | ||
| kind: str | ||
| text: str = "" | ||
| reasoning: str = "" | ||
| function_name: str | None = None | ||
| function_call_id: str | None = None | ||
| function_args_json: str = "" | ||
|
|
||
|
|
||
| class ResponseComposer: | ||
| """Compose a Responses SSE/JSON contract from NormalizedEvents. | ||
|
|
||
| Handles: text messages, reasoning, and user-defined function tool calls. | ||
| """ | ||
|
|
||
| def __init__( | ||
| self, *, response: ResponsesResponse, include: set[str] | None = None | ||
| ) -> None: | ||
| self._response = response | ||
| self._started = False | ||
| self._sequence_number = 0 | ||
| self._next_output_index = 0 | ||
| self._items: dict[str, _ItemState] = {} | ||
| self._output_items: list[OutputMessage | FunctionToolCall] = [] | ||
|
|
||
| @property | ||
| def response(self) -> ResponsesResponse: | ||
| return self._response | ||
|
|
||
| def feed(self, event: NormalizedEvent) -> Iterable[StreamEvent]: | ||
| if not self._started: | ||
| raise RuntimeError("ResponseComposer.start() must be called before feed().") | ||
|
|
||
| if isinstance(event, MessageStarted): | ||
| yield from self._start_message(event) | ||
| elif isinstance(event, MessageDelta): | ||
| yield from self._message_delta(event) | ||
| elif isinstance(event, MessageDone): | ||
| yield from self._message_done(event) | ||
| elif isinstance(event, ReasoningStarted): | ||
| pass # Reasoning items are not emitted as output events in this implementation | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. what does this means? not emitted as output events
Collaborator
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. yes for now. This PR doesnt focus on that. in another PR the reasoning events would be added. |
||
| elif isinstance(event, ReasoningDelta): | ||
| pass | ||
| elif isinstance(event, ReasoningDone): | ||
| pass | ||
| elif isinstance(event, FunctionCallStarted): | ||
| yield from self._start_function_call(event) | ||
| elif isinstance(event, FunctionCallArgumentsDelta): | ||
| yield from self._function_args_delta(event) | ||
| elif isinstance(event, FunctionCallDone): | ||
| yield from self._function_done(event) | ||
| elif isinstance(event, UsageFinal): | ||
| yield from self._complete_response(event) | ||
|
|
||
| def start(self) -> Iterable[StreamEvent]: | ||
| if self._started: | ||
| return [] | ||
| self._started = True | ||
| self._response.status = "in_progress" | ||
| return [ | ||
| ResponseEvent( | ||
| type="response.created", | ||
| sequence_number=self.alloc_sequence_number(), | ||
| response=self._response, | ||
| ), | ||
| ResponseEvent( | ||
| type="response.in_progress", | ||
| sequence_number=self.alloc_sequence_number(), | ||
| response=self._response, | ||
| ), | ||
| ] | ||
|
|
||
| def alloc_sequence_number(self) -> int: | ||
| return self._incr_seq() | ||
|
|
||
| def _incr_seq(self) -> int: | ||
| current = self._sequence_number | ||
| self._sequence_number += 1 | ||
| return current | ||
|
|
||
| def _alloc_output_index(self) -> int: | ||
| current = self._next_output_index | ||
| self._next_output_index += 1 | ||
| return current | ||
|
|
||
| def _start_message(self, event: MessageStarted) -> Iterable[StreamEvent]: | ||
| item_id = uuid7_str("msg_") | ||
| out_index = self._alloc_output_index() | ||
| state = _ItemState(item_id=item_id, output_index=out_index, kind="message") | ||
| self._items[event.item_key] = state | ||
|
|
||
| yield OutputItemEvent( | ||
| type="response.output_item.added", | ||
| sequence_number=self._incr_seq(), | ||
| output_index=out_index, | ||
| item=OutputMessage(content=[], id=item_id, status="in_progress"), | ||
| ) | ||
| yield ContentPartEvent( | ||
| type="response.content_part.added", | ||
| item_id=item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=out_index, | ||
| content_index=0, | ||
| part=OutputTextContent(text=""), | ||
| ) | ||
|
|
||
| def _message_delta(self, event: MessageDelta) -> Iterable[StreamEvent]: | ||
| state = self._items[event.item_key] | ||
| state.text += event.delta | ||
| yield TextDeltaEvent( | ||
| type="response.output_text.delta", | ||
| item_id=state.item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| content_index=0, | ||
| delta=event.delta, | ||
| logprobs=[], | ||
| ) | ||
|
|
||
| def _message_done(self, event: MessageDone) -> Iterable[StreamEvent]: | ||
| state = self._items[event.item_key] | ||
| state.text = event.text | ||
| yield TextDeltaEvent( | ||
| type="response.output_text.done", | ||
| item_id=state.item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| content_index=0, | ||
| text=event.text, | ||
| logprobs=[], | ||
| ) | ||
| yield ContentPartEvent( | ||
| type="response.content_part.done", | ||
| item_id=state.item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| content_index=0, | ||
| part=OutputTextContent(text=event.text), | ||
| ) | ||
| item = OutputMessage( | ||
| content=[OutputTextContent(text=event.text)], | ||
| id=state.item_id, | ||
| status="completed", | ||
| ) | ||
| yield OutputItemEvent( | ||
| type="response.output_item.done", | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| item=item, | ||
| ) | ||
| self._output_items.append(item) | ||
|
|
||
| def _start_function_call(self, event: FunctionCallStarted) -> Iterable[StreamEvent]: | ||
| item_id = uuid7_str("fc_") | ||
| out_index = self._alloc_output_index() | ||
| state = _ItemState( | ||
| item_id=item_id, | ||
| output_index=out_index, | ||
| kind="function_call", | ||
| function_name=event.name, | ||
| function_call_id=event.call_id, | ||
| function_args_json=event.initial_arguments_json, | ||
| ) | ||
| self._items[event.item_key] = state | ||
|
|
||
| yield OutputItemEvent( | ||
| type="response.output_item.added", | ||
| sequence_number=self._incr_seq(), | ||
| output_index=out_index, | ||
| item=FunctionToolCall( | ||
| arguments=event.initial_arguments_json, | ||
| call_id=event.call_id, | ||
| name=event.name, | ||
| id=item_id, | ||
| status="in_progress", | ||
| ), | ||
| ) | ||
|
|
||
| def _function_args_delta( | ||
| self, event: FunctionCallArgumentsDelta | ||
| ) -> Iterable[StreamEvent]: | ||
| state = self._items[event.item_key] | ||
| state.function_args_json += event.delta | ||
| yield TextDeltaEvent( | ||
| type="response.function_call_arguments.delta", | ||
| item_id=state.item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| delta=event.delta, | ||
| ) | ||
|
|
||
| def _function_done(self, event: FunctionCallDone) -> Iterable[StreamEvent]: | ||
| state = self._items[event.item_key] | ||
| state.function_args_json = event.arguments_json | ||
| yield TextDeltaEvent( | ||
| type="response.function_call_arguments.done", | ||
| item_id=state.item_id, | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| arguments=event.arguments_json, | ||
| ) | ||
| item = FunctionToolCall( | ||
| arguments=event.arguments_json, | ||
| call_id=state.function_call_id or "", | ||
| name=state.function_name or "", | ||
| id=state.item_id, | ||
| status="completed", | ||
| ) | ||
| yield OutputItemEvent( | ||
| type="response.output_item.done", | ||
| sequence_number=self._incr_seq(), | ||
| output_index=state.output_index, | ||
| item=item, | ||
| ) | ||
| self._output_items.append(item) | ||
|
|
||
| def _complete_response(self, event: UsageFinal) -> Iterable[StreamEvent]: | ||
| self._response.usage = ResponseUsage( | ||
| input_tokens=event.input_tokens, | ||
| input_tokens_details=InputTokenDetails( | ||
| cached_tokens=event.cache_read_tokens | ||
| ), | ||
| output_tokens=event.output_tokens, | ||
| output_tokens_details=OutputTokenDetails( | ||
| reasoning_tokens=event.reasoning_tokens | ||
| ), | ||
| total_tokens=event.total_tokens, | ||
| ) | ||
| self._response.output = list(self._output_items) | ||
| if event.incomplete_reason is not None: | ||
| self._response.status = "incomplete" | ||
| self._response.incomplete_details = IncompleteDetails( | ||
| reason=event.incomplete_reason | ||
| ) | ||
| yield ResponseEvent( | ||
| type="response.incomplete", | ||
| sequence_number=self._incr_seq(), | ||
| response=self._response, | ||
| ) | ||
| return | ||
|
|
||
| self._response.status = "completed" | ||
| self._response.incomplete_details = None | ||
| self._response.created_at = int(time()) | ||
| yield ResponseEvent( | ||
| type="response.completed", | ||
| sequence_number=self._incr_seq(), | ||
| response=self._response, | ||
| ) | ||
|
|
||
| def make_error_events( | ||
| self, *, code: str, message: str, param: str | None = None | ||
| ) -> Iterable[StreamEvent]: | ||
| """Emit an error event + response.failed lifecycle event.""" | ||
| self._response.error = {"code": code, "message": message} | ||
| self._response.status = "failed" | ||
| yield ErrorEvent( | ||
| type="response.error", | ||
| sequence_number=self.alloc_sequence_number(), | ||
| code=code, | ||
| message=message, | ||
| param=param, | ||
| ) | ||
| yield ResponseEvent( | ||
| type="response.failed", | ||
| sequence_number=self.alloc_sequence_number(), | ||
| response=self._response, | ||
| ) | ||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
High:
db_dialecttype annotation is misleadingdb_dialectis typed asLiteral["sqlite", "postgresql"]but thevalidate_db_urlvalidator only checksis_async, not the dialect name. A URL likemysql+aiomysql://...passes validation but violates the return type.Suggestion — validate the dialect name explicitly in
validate_db_url:This also addresses the open review thread from @noobHappylife —
db_dialectcan then be fully derived fromdb_urlwith confidence.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@franciscojavierarceo I have included the validation in db_url pydantic field validator so that user only enters
sqliteorpostgresqlin the first place. since db_dialect is a computed field based on db_url then we are sure that we only return intended dialect.