|
| 1 | +import logging |
| 2 | +from typing import Any, Dict, Literal, Optional, Sequence, Set, Union |
| 3 | + |
| 4 | +from slack_sdk.errors import SlackObjectFormationError |
| 5 | +from slack_sdk.models import show_unknown_key_warning |
| 6 | +from slack_sdk.models.basic_objects import JsonObject |
| 7 | + |
| 8 | +LOGGER = logging.getLogger(__name__) |
| 9 | + |
| 10 | + |
| 11 | +class Chunk(JsonObject): |
| 12 | + """ |
| 13 | + Chunk for streaming messages. |
| 14 | +
|
| 15 | + https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming |
| 16 | + """ |
| 17 | + |
| 18 | + attributes = {"type"} |
| 19 | + logger = logging.getLogger(__name__) |
| 20 | + |
| 21 | + def __init__( |
| 22 | + self, |
| 23 | + *, |
| 24 | + type: Optional[str] = None, |
| 25 | + ): |
| 26 | + self.type = type |
| 27 | + |
| 28 | + @classmethod |
| 29 | + def parse(cls, chunk: Union[Dict, "Chunk"]) -> Optional["Chunk"]: |
| 30 | + if chunk is None: |
| 31 | + return None |
| 32 | + elif isinstance(chunk, Chunk): |
| 33 | + return chunk |
| 34 | + else: |
| 35 | + if "type" in chunk: |
| 36 | + type = chunk["type"] |
| 37 | + if type == MarkdownTextChunk.type: |
| 38 | + return MarkdownTextChunk(**chunk) |
| 39 | + elif type == TaskUpdateChunk.type: |
| 40 | + return TaskUpdateChunk(**chunk) |
| 41 | + else: |
| 42 | + cls.logger.warning(f"Unknown chunk detected and skipped ({chunk})") |
| 43 | + return None |
| 44 | + else: |
| 45 | + cls.logger.warning(f"Unknown chunk detected and skipped ({chunk})") |
| 46 | + return None |
| 47 | + |
| 48 | + |
| 49 | +class MarkdownTextChunk(Chunk): |
| 50 | + type = "markdown_text" |
| 51 | + |
| 52 | + @property |
| 53 | + def attributes(self) -> Set[str]: # type: ignore[override] |
| 54 | + return super().attributes.union({"text"}) |
| 55 | + |
| 56 | + def __init__( |
| 57 | + self, |
| 58 | + *, |
| 59 | + text: str, |
| 60 | + **others: Dict, |
| 61 | + ): |
| 62 | + """Used for streaming text content with markdown formatting support. |
| 63 | +
|
| 64 | + https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming |
| 65 | + """ |
| 66 | + super().__init__(type=self.type) |
| 67 | + show_unknown_key_warning(self, others) |
| 68 | + |
| 69 | + self.text = text |
| 70 | + |
| 71 | + |
| 72 | +class URLSource(JsonObject): |
| 73 | + type = "url" |
| 74 | + |
| 75 | + @property |
| 76 | + def attributes(self) -> Set[str]: # type: ignore[override] |
| 77 | + return super().attributes.union( |
| 78 | + { |
| 79 | + "url", |
| 80 | + "text", |
| 81 | + "icon_url", |
| 82 | + } |
| 83 | + ) |
| 84 | + |
| 85 | + def __init__( |
| 86 | + self, |
| 87 | + *, |
| 88 | + url: str, |
| 89 | + text: str, |
| 90 | + icon_url: Optional[str] = None, |
| 91 | + **others: Dict, |
| 92 | + ): |
| 93 | + show_unknown_key_warning(self, others) |
| 94 | + self._url = url |
| 95 | + self._text = text |
| 96 | + self._icon_url = icon_url |
| 97 | + |
| 98 | + def to_dict(self) -> Dict[str, Any]: |
| 99 | + self.validate_json() |
| 100 | + json: Dict[str, Union[str, Dict]] = { |
| 101 | + "type": self.type, |
| 102 | + "url": self._url, |
| 103 | + "text": self._text, |
| 104 | + } |
| 105 | + if self._icon_url: |
| 106 | + json["icon_url"] = self._icon_url |
| 107 | + return json |
| 108 | + |
| 109 | + |
| 110 | +class TaskUpdateChunk(Chunk): |
| 111 | + type = "task_update" |
| 112 | + |
| 113 | + @property |
| 114 | + def attributes(self) -> Set[str]: # type: ignore[override] |
| 115 | + return super().attributes.union( |
| 116 | + { |
| 117 | + "id", |
| 118 | + "title", |
| 119 | + "status", |
| 120 | + "details", |
| 121 | + "output", |
| 122 | + "sources", |
| 123 | + } |
| 124 | + ) |
| 125 | + |
| 126 | + def __init__( |
| 127 | + self, |
| 128 | + *, |
| 129 | + id: str, |
| 130 | + title: str, |
| 131 | + status: Literal["pending", "in_progress", "complete", "error"], |
| 132 | + details: Optional[str] = None, |
| 133 | + output: Optional[str] = None, |
| 134 | + sources: Optional[Sequence[Union[Dict, URLSource]]] = None, |
| 135 | + **others: Dict, |
| 136 | + ): |
| 137 | + """Used for displaying tool execution progress in a timeline-style UI. |
| 138 | +
|
| 139 | + https://docs.slack.dev/messaging/sending-and-scheduling-messages#text-streaming |
| 140 | + """ |
| 141 | + super().__init__(type=self.type) |
| 142 | + show_unknown_key_warning(self, others) |
| 143 | + |
| 144 | + self.id = id |
| 145 | + self.title = title |
| 146 | + self.status = status |
| 147 | + self.details = details |
| 148 | + self.output = output |
| 149 | + if sources is not None: |
| 150 | + self.sources = [] |
| 151 | + for src in sources: |
| 152 | + if isinstance(src, Dict): |
| 153 | + self.sources.append(src) |
| 154 | + elif isinstance(src, URLSource): |
| 155 | + self.sources.append(src.to_dict()) |
| 156 | + else: |
| 157 | + raise SlackObjectFormationError(f"Unsupported type for source in task update chunk: {type(src)}") |
0 commit comments