-
Notifications
You must be signed in to change notification settings - Fork 850
feat: accept chunks as arguments to chat.{start,append,stop}Stream methods #1806
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
Draft
zimeg
wants to merge
4
commits into
feat-ai-apps-thinking-steps
Choose a base branch
from
zimeg-feat-ai-apps-chunks
base: feat-ai-apps-thinking-steps
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
c3bdb2d
feat: accept chunks as arguments to chat.{start,append,stop}Stream me…
zimeg 8c56e22
fix: remove unsupported and unused identifiers for full support
zimeg 783ada5
style: remove mypy extra ignore comment for overriden attributes
zimeg 1fb7355
test: confirm chunks parse as expected json values
zimeg 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 |
|---|---|---|
| @@ -0,0 +1,155 @@ | ||
| import logging | ||
| from typing import Any, Dict, Optional, Sequence, Set, Union | ||
|
|
||
| from slack_sdk.errors import SlackObjectFormationError | ||
| from slack_sdk.models import show_unknown_key_warning | ||
| from slack_sdk.models.basic_objects import JsonObject | ||
|
|
||
|
|
||
| class Chunk(JsonObject): | ||
| """ | ||
| Chunk for streaming messages. | ||
|
|
||
| https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming | ||
| """ | ||
|
|
||
| attributes = {"type"} | ||
| logger = logging.getLogger(__name__) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| type: Optional[str] = None, | ||
| ): | ||
| self.type = type | ||
|
|
||
| @classmethod | ||
| def parse(cls, chunk: Union[Dict, "Chunk"]) -> Optional["Chunk"]: | ||
| if chunk is None: | ||
| return None | ||
| elif isinstance(chunk, Chunk): | ||
| return chunk | ||
| else: | ||
| if "type" in chunk: | ||
| type = chunk["type"] | ||
| if type == MarkdownTextChunk.type: | ||
| return MarkdownTextChunk(**chunk) | ||
| elif type == TaskUpdateChunk.type: | ||
| return TaskUpdateChunk(**chunk) | ||
| else: | ||
| cls.logger.warning(f"Unknown chunk detected and skipped ({chunk})") | ||
| return None | ||
| else: | ||
| cls.logger.warning(f"Unknown chunk detected and skipped ({chunk})") | ||
| return None | ||
|
|
||
|
|
||
| class MarkdownTextChunk(Chunk): | ||
| type = "markdown_text" | ||
|
|
||
| @property | ||
| def attributes(self) -> Set[str]: # type: ignore[override] | ||
| return super().attributes.union({"text"}) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| text: str, | ||
| **others: Dict, | ||
| ): | ||
| """Used for streaming text content with markdown formatting support. | ||
|
|
||
| https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming | ||
| """ | ||
| super().__init__(type=self.type) | ||
| show_unknown_key_warning(self, others) | ||
|
|
||
| self.text = text | ||
|
|
||
|
|
||
| class URLSource(JsonObject): | ||
| type = "url" | ||
|
|
||
| @property | ||
| def attributes(self) -> Set[str]: | ||
| return super().attributes.union( | ||
| { | ||
| "url", | ||
| "text", | ||
| "icon_url", | ||
| } | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| url: str, | ||
| text: str, | ||
| icon_url: Optional[str] = None, | ||
| **others: Dict, | ||
| ): | ||
| show_unknown_key_warning(self, others) | ||
| self._url = url | ||
| self._text = text | ||
| self._icon_url = icon_url | ||
|
|
||
| def to_dict(self) -> Dict[str, Any]: | ||
| self.validate_json() | ||
| json: Dict[str, Union[str, Dict]] = { | ||
| "type": self.type, | ||
| "url": self._url, | ||
| "text": self._text, | ||
| } | ||
| if self._icon_url: | ||
| json["icon_url"] = self._icon_url | ||
| return json | ||
|
|
||
|
|
||
| class TaskUpdateChunk(Chunk): | ||
| type = "task_update" | ||
|
|
||
| @property | ||
| def attributes(self) -> Set[str]: # type: ignore[override] | ||
| return super().attributes.union( | ||
| { | ||
| "id", | ||
| "title", | ||
| "status", | ||
| "details", | ||
| "output", | ||
| "sources", | ||
| } | ||
| ) | ||
|
|
||
| def __init__( | ||
| self, | ||
| *, | ||
| id: str, | ||
| title: str, | ||
| status: str, # "pending", "in_progress", "complete", "error" | ||
| details: Optional[str] = None, | ||
| output: Optional[str] = None, | ||
| sources: Optional[Sequence[Union[Dict, URLSource]]] = None, | ||
| **others: Dict, | ||
| ): | ||
| """Used for displaying tool execution progress in a timeline-style UI. | ||
|
|
||
| https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming | ||
| """ | ||
| super().__init__(type=self.type) | ||
| show_unknown_key_warning(self, others) | ||
|
|
||
| self.id = id | ||
| self.title = title | ||
| self.status = status | ||
| self.details = details | ||
| self.output = output | ||
| if sources is not None: | ||
| self.sources = [] | ||
| for src in sources: | ||
| if isinstance(src, Dict): | ||
| self.sources.append(src) | ||
| elif isinstance(src, URLSource): | ||
| self.sources.append(src.to_dict()) | ||
| else: | ||
| raise SlackObjectFormationError(f"Unsupported type for source in task update chunk: {type(src)}") | ||
|
Comment on lines
+147
to
+155
Member
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. 🪓 note: This is simplified alongside the changes of #1819 as well! |
||
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
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.
📣 note: In 6073ffe of #1819 this is moved into the block elements class because it can be used in standalone messages - not just chunks.
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.
🧮 note: It's also changed from "URL" to "Url" to match similar elements!