-
Notifications
You must be signed in to change notification settings - Fork 1
feat: support sign-in into toolsets via client channel #100
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
Merged
Merged
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
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
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,47 @@ | ||
| from typing import AsyncIterator, Iterator, List | ||
|
|
||
| from aidial_client._log import logger | ||
|
|
||
| _UNCOMMITTED_BUFFER_WARNING = ( | ||
| "Uncommitted data chunks in SSE stream " | ||
| "(stream ended without a terminating blank line); discarding." | ||
| ) | ||
|
|
||
|
|
||
| def _strip_field(line: str, prefix: str) -> str: | ||
| """Strip a single leading U+0020 SPACE after the field colon, per the SSE spec.""" | ||
| value = line[len(prefix) :] | ||
| return value[1:] if value.startswith(" ") else value | ||
|
|
||
|
|
||
| def iter_data_events(lines: Iterator[str]) -> Iterator[str]: | ||
| """Yield the payload of each complete ``data:`` event from an SSE line stream. | ||
|
|
||
| An event is complete when a blank line follows the ``data:`` line(s). Per | ||
| the SSE dispatch rule, a buffer that has not been terminated by a blank | ||
| line is discarded (we do NOT flush partial events at end of stream). | ||
| Comment lines (``:``) and other field names are ignored. | ||
| """ | ||
| buffer: List[str] = [] | ||
| for line in lines: | ||
| if line == "": | ||
| if buffer: | ||
| yield "\n".join(buffer) | ||
| buffer = [] | ||
| elif line.startswith("data:"): | ||
| buffer.append(_strip_field(line, "data:")) | ||
|
adubovik marked this conversation as resolved.
|
||
| if buffer: | ||
| logger.warning(_UNCOMMITTED_BUFFER_WARNING) | ||
|
|
||
|
|
||
| async def aiter_data_events(lines: AsyncIterator[str]) -> AsyncIterator[str]: | ||
| buffer: List[str] = [] | ||
| async for line in lines: | ||
| if line == "": | ||
| if buffer: | ||
| yield "\n".join(buffer) | ||
| buffer = [] | ||
| elif line.startswith("data:"): | ||
| buffer.append(_strip_field(line, "data:")) | ||
| if buffer: | ||
| logger.warning(_UNCOMMITTED_BUFFER_WARNING) | ||
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,75 @@ | ||
| from typing import Any, Dict, List, Literal, Optional, Union | ||
|
|
||
| from aidial_client._compatibility.pydantic_v1 import ( | ||
| BaseModel, | ||
| Extra, | ||
| Field, | ||
| root_validator, | ||
| ) | ||
|
|
||
|
|
||
| class JsonRpcError(BaseModel): | ||
| code: int | ||
| message: str | ||
| data: Optional[Any] = None | ||
|
|
||
| class Config: | ||
| extra = Extra.allow | ||
|
|
||
|
|
||
| class JsonRpcRequest(BaseModel): | ||
| jsonrpc: Literal["2.0"] = "2.0" | ||
| method: str | ||
| params: Optional[Union[List[Any], Dict[str, Any]]] = None | ||
| id: Optional[Union[int, str]] = None | ||
|
|
||
| class Config: | ||
| smart_union = True | ||
|
|
||
|
|
||
| class JsonRpcResponse(BaseModel): | ||
| jsonrpc: Literal["2.0"] | ||
| result: Optional[Any] = None | ||
| error: Optional[JsonRpcError] = None | ||
| id: Optional[Union[int, str]] = Field(...) | ||
|
|
||
| class Config: | ||
| smart_union = True | ||
| extra = Extra.allow | ||
|
|
||
| @root_validator(pre=True) | ||
| def _validate_result_xor_error(cls, values): | ||
| """Per JSON-RPC 2.0 (https://www.jsonrpc.org/specification#response_object), | ||
| either ``result`` or ``error`` MUST be included (presence-wise — ``null`` | ||
| is a valid result value), and both MUST NOT be included. | ||
| """ | ||
| if not isinstance(values, dict): | ||
| return values | ||
| has_result = "result" in values | ||
| has_error = "error" in values | ||
| if has_result and has_error: | ||
| raise ValueError( | ||
| "JSON-RPC response must not contain both 'result' and 'error'" | ||
| ) | ||
| if not has_result and not has_error: | ||
| raise ValueError( | ||
| "JSON-RPC response must contain either 'result' or 'error'" | ||
| ) | ||
| return values | ||
|
|
||
|
|
||
| class JsonRpcResponses(BaseModel): | ||
| """Pydantic root model that accepts a single JSON-RPC response object or | ||
| a batch array, normalizing both to a list via the ``responses`` property. | ||
| """ | ||
|
|
||
| __root__: Union[JsonRpcResponse, List[JsonRpcResponse]] | ||
|
|
||
| class Config: | ||
| smart_union = True | ||
|
|
||
| @property | ||
| def responses(self) -> List[JsonRpcResponse]: | ||
| if isinstance(self.__root__, list): | ||
| return self.__root__ | ||
| return [self.__root__] |
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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.