slackwright 0.1.0__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- slackwright/__init__.py +10 -0
- slackwright/blocks.py +224 -0
- slackwright/canvas.py +151 -0
- slackwright/forms.py +999 -0
- slackwright/inflight.py +93 -0
- slackwright/ingest.py +394 -0
- slackwright/progress.py +90 -0
- slackwright/py.typed +0 -0
- slackwright/render.py +384 -0
- slackwright/stores.py +150 -0
- slackwright/toolkit.py +575 -0
- slackwright-0.1.0.dist-info/METADATA +105 -0
- slackwright-0.1.0.dist-info/RECORD +15 -0
- slackwright-0.1.0.dist-info/WHEEL +4 -0
- slackwright-0.1.0.dist-info/licenses/LICENSE +25 -0
slackwright/__init__.py
ADDED
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
"""Building blocks for AI agents that live in Slack."""
|
|
2
|
+
|
|
3
|
+
import importlib.metadata
|
|
4
|
+
|
|
5
|
+
from slackwright.render import Stream, post_markdown
|
|
6
|
+
from slackwright.toolkit import Toolkit, ToolResult
|
|
7
|
+
|
|
8
|
+
version = importlib.metadata.version('slackwright')
|
|
9
|
+
|
|
10
|
+
__all__ = ['Stream', 'ToolResult', 'Toolkit', 'post_markdown', 'version']
|
slackwright/blocks.py
ADDED
|
@@ -0,0 +1,224 @@
|
|
|
1
|
+
"""Typed specs for Slack blocks.
|
|
2
|
+
|
|
3
|
+
An agent writes a list of :data:`BlockSpec` values. slackwright checks
|
|
4
|
+
Slack's limits and writes the Block Kit JSON, so a bad spec fails with
|
|
5
|
+
a clear error before anything is posted.
|
|
6
|
+
|
|
7
|
+
"""
|
|
8
|
+
|
|
9
|
+
from __future__ import annotations
|
|
10
|
+
|
|
11
|
+
import typing
|
|
12
|
+
|
|
13
|
+
import pydantic
|
|
14
|
+
|
|
15
|
+
MESSAGE_BLOCK_LIMIT = 50
|
|
16
|
+
TABLE_ROW_LIMIT = 100
|
|
17
|
+
TABLE_CELL_LIMIT = 20
|
|
18
|
+
TABLE_TEXT_LIMIT = 10_000
|
|
19
|
+
MARKDOWN_TEXT_LIMIT = 12_000
|
|
20
|
+
TEXT_OBJECT_LIMIT = 3000
|
|
21
|
+
"""Characters in one ``plain_text`` or ``mrkdwn`` text object."""
|
|
22
|
+
|
|
23
|
+
|
|
24
|
+
def plain_text(text: str) -> dict[str, typing.Any]:
|
|
25
|
+
"""Return a Block Kit ``plain_text`` object."""
|
|
26
|
+
return {'type': 'plain_text', 'text': text, 'emoji': True}
|
|
27
|
+
|
|
28
|
+
|
|
29
|
+
class _Spec(pydantic.BaseModel):
|
|
30
|
+
model_config = pydantic.ConfigDict(extra='forbid')
|
|
31
|
+
|
|
32
|
+
|
|
33
|
+
class MarkdownSpec(_Spec):
|
|
34
|
+
"""Standard Markdown, rendered by Slack."""
|
|
35
|
+
|
|
36
|
+
type: typing.Literal['markdown']
|
|
37
|
+
text: str = pydantic.Field(
|
|
38
|
+
min_length=1,
|
|
39
|
+
max_length=MARKDOWN_TEXT_LIMIT,
|
|
40
|
+
description=(
|
|
41
|
+
'Standard Markdown: headings, bold, italic, links, lists, '
|
|
42
|
+
'code blocks, tables, quotes.'
|
|
43
|
+
),
|
|
44
|
+
)
|
|
45
|
+
|
|
46
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
47
|
+
return {'type': 'markdown', 'text': self.text}
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
class HeaderSpec(_Spec):
|
|
51
|
+
"""A large bold header line."""
|
|
52
|
+
|
|
53
|
+
type: typing.Literal['header']
|
|
54
|
+
text: str = pydantic.Field(
|
|
55
|
+
min_length=1, max_length=150, description='Plain text, no Markdown.'
|
|
56
|
+
)
|
|
57
|
+
|
|
58
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
59
|
+
return {
|
|
60
|
+
'type': 'header',
|
|
61
|
+
'text': plain_text(self.text),
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
|
|
65
|
+
class DividerSpec(_Spec):
|
|
66
|
+
"""A horizontal line."""
|
|
67
|
+
|
|
68
|
+
type: typing.Literal['divider']
|
|
69
|
+
|
|
70
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
71
|
+
return {'type': 'divider'}
|
|
72
|
+
|
|
73
|
+
|
|
74
|
+
class ContextSpec(_Spec):
|
|
75
|
+
"""Small grey text, for notes and metadata."""
|
|
76
|
+
|
|
77
|
+
type: typing.Literal['context']
|
|
78
|
+
elements: list[
|
|
79
|
+
typing.Annotated[
|
|
80
|
+
str, pydantic.Field(min_length=1, max_length=TEXT_OBJECT_LIMIT)
|
|
81
|
+
]
|
|
82
|
+
] = pydantic.Field(
|
|
83
|
+
min_length=1,
|
|
84
|
+
max_length=10,
|
|
85
|
+
description=(
|
|
86
|
+
'One to ten short texts in Slack mrkdwn: *bold*, _italic_, '
|
|
87
|
+
'<https://example.com|label>. Not standard Markdown.'
|
|
88
|
+
),
|
|
89
|
+
)
|
|
90
|
+
|
|
91
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
92
|
+
return {
|
|
93
|
+
'type': 'context',
|
|
94
|
+
'elements': [
|
|
95
|
+
{'type': 'mrkdwn', 'text': text} for text in self.elements
|
|
96
|
+
],
|
|
97
|
+
}
|
|
98
|
+
|
|
99
|
+
|
|
100
|
+
class ImageSpec(_Spec):
|
|
101
|
+
"""An image from a public URL."""
|
|
102
|
+
|
|
103
|
+
type: typing.Literal['image']
|
|
104
|
+
image_url: pydantic.HttpUrl = pydantic.Field(
|
|
105
|
+
description='A public https URL of a PNG, JPEG, or GIF image.'
|
|
106
|
+
)
|
|
107
|
+
alt_text: str = pydantic.Field(
|
|
108
|
+
min_length=1,
|
|
109
|
+
max_length=2000,
|
|
110
|
+
description='A description of the image for screen readers.',
|
|
111
|
+
)
|
|
112
|
+
title: str | None = pydantic.Field(
|
|
113
|
+
default=None, max_length=2000, description='Optional caption.'
|
|
114
|
+
)
|
|
115
|
+
|
|
116
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
117
|
+
block: dict[str, typing.Any] = {
|
|
118
|
+
'type': 'image',
|
|
119
|
+
'image_url': str(self.image_url),
|
|
120
|
+
'alt_text': self.alt_text,
|
|
121
|
+
}
|
|
122
|
+
if self.title:
|
|
123
|
+
block['title'] = {'type': 'plain_text', 'text': self.title}
|
|
124
|
+
return block
|
|
125
|
+
|
|
126
|
+
|
|
127
|
+
class TableSpec(_Spec):
|
|
128
|
+
"""A table. The first row is the header row."""
|
|
129
|
+
|
|
130
|
+
type: typing.Literal['table']
|
|
131
|
+
rows: list[list[str]] = pydantic.Field(
|
|
132
|
+
min_length=1,
|
|
133
|
+
max_length=TABLE_ROW_LIMIT,
|
|
134
|
+
description=(
|
|
135
|
+
'Rows of plain-text cells. The first row is the header. All '
|
|
136
|
+
'rows have the same number of cells (max '
|
|
137
|
+
f'{TABLE_CELL_LIMIT}). Max {TABLE_ROW_LIMIT} rows and '
|
|
138
|
+
f'{TABLE_TEXT_LIMIT:,} characters in all cells.'
|
|
139
|
+
),
|
|
140
|
+
)
|
|
141
|
+
align: list[typing.Literal['left', 'center', 'right'] | None] | None = (
|
|
142
|
+
pydantic.Field(
|
|
143
|
+
default=None,
|
|
144
|
+
description='Optional alignment for each column, left to right.',
|
|
145
|
+
)
|
|
146
|
+
)
|
|
147
|
+
|
|
148
|
+
@pydantic.model_validator(mode='after')
|
|
149
|
+
def _check_shape(self) -> TableSpec:
|
|
150
|
+
width = len(self.rows[0])
|
|
151
|
+
if not 1 <= width <= TABLE_CELL_LIMIT:
|
|
152
|
+
raise ValueError(
|
|
153
|
+
f'each row must have 1 to {TABLE_CELL_LIMIT} cells'
|
|
154
|
+
)
|
|
155
|
+
if any(len(row) != width for row in self.rows):
|
|
156
|
+
raise ValueError('all rows must have the same number of cells')
|
|
157
|
+
if self.align is not None and len(self.align) > width:
|
|
158
|
+
raise ValueError('align has more items than the table columns')
|
|
159
|
+
if self.text_length() > TABLE_TEXT_LIMIT:
|
|
160
|
+
raise ValueError(
|
|
161
|
+
f'the table has more than {TABLE_TEXT_LIMIT} characters'
|
|
162
|
+
)
|
|
163
|
+
return self
|
|
164
|
+
|
|
165
|
+
def text_length(self) -> int:
|
|
166
|
+
return sum(len(cell) for row in self.rows for cell in row)
|
|
167
|
+
|
|
168
|
+
def to_block(self) -> dict[str, typing.Any]:
|
|
169
|
+
block: dict[str, typing.Any] = {
|
|
170
|
+
'type': 'table',
|
|
171
|
+
'rows': [
|
|
172
|
+
[{'type': 'raw_text', 'text': cell} for cell in row]
|
|
173
|
+
for row in self.rows
|
|
174
|
+
],
|
|
175
|
+
}
|
|
176
|
+
if self.align:
|
|
177
|
+
block['column_settings'] = [
|
|
178
|
+
{'align': align} if align else None for align in self.align
|
|
179
|
+
]
|
|
180
|
+
return block
|
|
181
|
+
|
|
182
|
+
|
|
183
|
+
BlockSpec = typing.Annotated[
|
|
184
|
+
MarkdownSpec
|
|
185
|
+
| HeaderSpec
|
|
186
|
+
| DividerSpec
|
|
187
|
+
| ContextSpec
|
|
188
|
+
| ImageSpec
|
|
189
|
+
| TableSpec,
|
|
190
|
+
pydantic.Field(discriminator='type'),
|
|
191
|
+
]
|
|
192
|
+
|
|
193
|
+
|
|
194
|
+
def check_message(specs: list[BlockSpec]) -> None:
|
|
195
|
+
"""Check the limits for all blocks in one message.
|
|
196
|
+
|
|
197
|
+
Raises :class:`ValueError` with a message for the agent.
|
|
198
|
+
|
|
199
|
+
"""
|
|
200
|
+
if not 1 <= len(specs) <= MESSAGE_BLOCK_LIMIT:
|
|
201
|
+
raise ValueError(
|
|
202
|
+
f'a message must have 1 to {MESSAGE_BLOCK_LIMIT} blocks'
|
|
203
|
+
)
|
|
204
|
+
markdown = sum(
|
|
205
|
+
len(spec.text) for spec in specs if isinstance(spec, MarkdownSpec)
|
|
206
|
+
)
|
|
207
|
+
if markdown > MARKDOWN_TEXT_LIMIT:
|
|
208
|
+
raise ValueError(
|
|
209
|
+
f'markdown blocks have more than {MARKDOWN_TEXT_LIMIT} '
|
|
210
|
+
'characters in total'
|
|
211
|
+
)
|
|
212
|
+
tables = sum(
|
|
213
|
+
spec.text_length() for spec in specs if isinstance(spec, TableSpec)
|
|
214
|
+
)
|
|
215
|
+
if tables > TABLE_TEXT_LIMIT:
|
|
216
|
+
raise ValueError(
|
|
217
|
+
f'tables have more than {TABLE_TEXT_LIMIT} characters in total'
|
|
218
|
+
)
|
|
219
|
+
|
|
220
|
+
|
|
221
|
+
def to_blocks(specs: list[BlockSpec]) -> list[dict[str, typing.Any]]:
|
|
222
|
+
"""Check ``specs`` and return the Block Kit JSON."""
|
|
223
|
+
check_message(specs)
|
|
224
|
+
return [spec.to_block() for spec in specs]
|
slackwright/canvas.py
ADDED
|
@@ -0,0 +1,151 @@
|
|
|
1
|
+
"""Create, edit, and read Slack canvases.
|
|
2
|
+
|
|
3
|
+
Canvas content is standard Markdown. The bot needs the
|
|
4
|
+
``canvases:read``, ``canvases:write``, and ``files:read`` scopes.
|
|
5
|
+
|
|
6
|
+
"""
|
|
7
|
+
|
|
8
|
+
from __future__ import annotations
|
|
9
|
+
|
|
10
|
+
import typing
|
|
11
|
+
|
|
12
|
+
from slack_sdk import errors
|
|
13
|
+
|
|
14
|
+
Operation = typing.Literal[
|
|
15
|
+
'insert_at_start',
|
|
16
|
+
'insert_at_end',
|
|
17
|
+
'insert_after',
|
|
18
|
+
'insert_before',
|
|
19
|
+
'replace',
|
|
20
|
+
'delete',
|
|
21
|
+
'rename',
|
|
22
|
+
]
|
|
23
|
+
|
|
24
|
+
SECTION_OPERATIONS = frozenset(
|
|
25
|
+
{'insert_after', 'insert_before', 'replace', 'delete'}
|
|
26
|
+
)
|
|
27
|
+
"""Operations that need exactly one section. ``replace`` without a
|
|
28
|
+
section would replace the whole canvas, so slackwright never allows
|
|
29
|
+
that."""
|
|
30
|
+
|
|
31
|
+
|
|
32
|
+
def check(
|
|
33
|
+
operation: Operation,
|
|
34
|
+
markdown: str | None,
|
|
35
|
+
section: str | None,
|
|
36
|
+
*,
|
|
37
|
+
section_name: str = 'section_id',
|
|
38
|
+
) -> None:
|
|
39
|
+
"""Raise :class:`ValueError` if a change misses a required value.
|
|
40
|
+
|
|
41
|
+
``section_name`` is the argument name to use in the message.
|
|
42
|
+
|
|
43
|
+
"""
|
|
44
|
+
if operation in SECTION_OPERATIONS and not section:
|
|
45
|
+
raise ValueError(f'{operation} needs {section_name}')
|
|
46
|
+
if operation != 'delete' and not markdown:
|
|
47
|
+
raise ValueError(f'{operation} needs markdown')
|
|
48
|
+
|
|
49
|
+
|
|
50
|
+
async def create(
|
|
51
|
+
slack: typing.Any,
|
|
52
|
+
channel_id: str,
|
|
53
|
+
title: str,
|
|
54
|
+
markdown: str,
|
|
55
|
+
*,
|
|
56
|
+
channel_canvas: bool = False,
|
|
57
|
+
) -> str:
|
|
58
|
+
"""Create a canvas in ``channel_id``. Return its ID.
|
|
59
|
+
|
|
60
|
+
By default the canvas is a new canvas tab in the channel. With
|
|
61
|
+
``channel_canvas``, it is the channel's own canvas. A channel has
|
|
62
|
+
only one, so this raises :class:`ValueError` if it has one.
|
|
63
|
+
|
|
64
|
+
"""
|
|
65
|
+
content = {'type': 'markdown', 'markdown': markdown}
|
|
66
|
+
try:
|
|
67
|
+
if channel_canvas:
|
|
68
|
+
response = await slack.conversations_canvases_create(
|
|
69
|
+
channel_id=channel_id, document_content=content, title=title
|
|
70
|
+
)
|
|
71
|
+
else:
|
|
72
|
+
response = await slack.canvases_create(
|
|
73
|
+
title=title, document_content=content, channel_id=channel_id
|
|
74
|
+
)
|
|
75
|
+
except errors.SlackApiError as error:
|
|
76
|
+
if error.response.get('error') == 'channel_canvas_already_exists':
|
|
77
|
+
raise ValueError(
|
|
78
|
+
'this channel already has a channel canvas; create a '
|
|
79
|
+
'canvas tab or edit that canvas'
|
|
80
|
+
) from error
|
|
81
|
+
raise
|
|
82
|
+
return str(response['canvas_id'])
|
|
83
|
+
|
|
84
|
+
|
|
85
|
+
async def find_section(
|
|
86
|
+
slack: typing.Any, canvas_id: str, contains: str
|
|
87
|
+
) -> str:
|
|
88
|
+
"""Return the ID of the one section that contains ``contains``.
|
|
89
|
+
|
|
90
|
+
Raises :class:`ValueError` unless exactly one section matches.
|
|
91
|
+
|
|
92
|
+
"""
|
|
93
|
+
response = await slack.canvases_sections_lookup(
|
|
94
|
+
canvas_id=canvas_id, criteria={'contains_text': contains}
|
|
95
|
+
)
|
|
96
|
+
sections = response.get('sections') or []
|
|
97
|
+
if len(sections) != 1:
|
|
98
|
+
raise ValueError(
|
|
99
|
+
f'{len(sections)} sections contain {contains!r}; it must '
|
|
100
|
+
'match exactly one section'
|
|
101
|
+
)
|
|
102
|
+
return str(sections[0]['id'])
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
async def edit(
|
|
106
|
+
slack: typing.Any,
|
|
107
|
+
canvas_id: str,
|
|
108
|
+
operation: Operation,
|
|
109
|
+
markdown: str | None = None,
|
|
110
|
+
*,
|
|
111
|
+
section_id: str | None = None,
|
|
112
|
+
) -> None:
|
|
113
|
+
"""Make one change to a canvas.
|
|
114
|
+
|
|
115
|
+
``markdown`` is the new content, or the new title for ``rename``.
|
|
116
|
+
Operations in :data:`SECTION_OPERATIONS` need ``section_id``; get
|
|
117
|
+
it with :func:`find_section`.
|
|
118
|
+
|
|
119
|
+
"""
|
|
120
|
+
check(operation, markdown, section_id)
|
|
121
|
+
change: dict[str, typing.Any] = {'operation': operation}
|
|
122
|
+
if section_id:
|
|
123
|
+
change['section_id'] = section_id
|
|
124
|
+
if markdown and operation != 'delete':
|
|
125
|
+
key = 'title_content' if operation == 'rename' else 'document_content'
|
|
126
|
+
change[key] = {'type': 'markdown', 'markdown': markdown}
|
|
127
|
+
await slack.canvases_edit(canvas_id=canvas_id, changes=[change])
|
|
128
|
+
|
|
129
|
+
|
|
130
|
+
async def read(slack: typing.Any, canvas_id: str) -> str:
|
|
131
|
+
"""Return the content of a canvas as Markdown."""
|
|
132
|
+
response = await slack.canvases_getContent(
|
|
133
|
+
canvas_id=canvas_id, content_type='markdown'
|
|
134
|
+
)
|
|
135
|
+
return str(response.get('content') or '')
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
async def shared_with(slack: typing.Any, canvas_id: str) -> set[str]:
|
|
139
|
+
"""Return the IDs of the conversations that a canvas is shared in."""
|
|
140
|
+
response = await slack.files_info(file=canvas_id)
|
|
141
|
+
file = response.get('file') or {}
|
|
142
|
+
shares = file.get('shares') or {}
|
|
143
|
+
linked = file.get('linked_channel_id')
|
|
144
|
+
return {
|
|
145
|
+
*([linked] if linked else []),
|
|
146
|
+
*(file.get('channels') or []),
|
|
147
|
+
*(file.get('groups') or []),
|
|
148
|
+
*(file.get('ims') or []),
|
|
149
|
+
*(shares.get('public') or {}),
|
|
150
|
+
*(shares.get('private') or {}),
|
|
151
|
+
}
|