@withgateway/sdk 0.1.20 → 0.2.0
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.
- package/README.md +141 -2
- package/dist/cli.js +8683 -1343
- package/dist/cli.js.map +1 -1
- package/dist/cli.mjs +8683 -1344
- package/dist/cli.mjs.map +1 -1
- package/dist/index.d.mts +1 -1
- package/dist/index.d.ts +1 -1
- package/dist/index.js +979 -46
- package/dist/index.js.map +1 -1
- package/dist/index.mjs +943 -38
- package/dist/index.mjs.map +1 -1
- package/dist/tracing/index.js +9 -2
- package/dist/tracing/index.js.map +1 -1
- package/dist/tracing/index.mjs +9 -2
- package/dist/tracing/index.mjs.map +1 -1
- package/dist/worlds/index.d.mts +738 -28
- package/dist/worlds/index.d.ts +738 -28
- package/dist/worlds/index.js +940 -42
- package/dist/worlds/index.js.map +1 -1
- package/dist/worlds/index.mjs +934 -36
- package/dist/worlds/index.mjs.map +1 -1
- package/package.json +9 -2
- package/runtime/gatewaysdk/__init__.py +0 -0
- package/runtime/gatewaysdk/environment/__init__.py +108 -0
- package/runtime/gatewaysdk/environment/_bundle.py +281 -0
- package/runtime/gatewaysdk/environment/_harbor.py +276 -0
- package/runtime/gatewaysdk/environment/_materialize.py +97 -0
- package/runtime/gatewaysdk/environment/_tar.py +33 -0
- package/runtime/gatewaysdk/environment/_world.py +346 -0
- package/runtime/gatewaysdk/environment/core.py +527 -0
- package/runtime/gatewaysdk/environment/errors.py +58 -0
- package/runtime/gatewaysdk/environment/runtime.py +150 -0
- package/runtime/gatewaysdk/environment/schema/__init__.py +35 -0
- package/runtime/gatewaysdk/environment/schema/__main__.py +225 -0
- package/runtime/gatewaysdk/environment/schema/_slack_fidelity.py +116 -0
- package/runtime/gatewaysdk/environment/schema/_slack_scenario.py +183 -0
- package/runtime/gatewaysdk/environment/schema/api.py +1628 -0
- package/runtime/gatewaysdk/environment/schema/batch.py +755 -0
- package/runtime/gatewaysdk/environment/schema/compiler.py +615 -0
- package/runtime/gatewaysdk/environment/schema/conform.py +428 -0
- package/runtime/gatewaysdk/environment/schema/connector.py +777 -0
- package/runtime/gatewaysdk/environment/schema/connectors/apple-business-manager/handlers.py +17 -0
- package/runtime/gatewaysdk/environment/schema/connectors/apple-business-manager/parity.json +28 -0
- package/runtime/gatewaysdk/environment/schema/connectors/apple-business-manager/provenance.json +233 -0
- package/runtime/gatewaysdk/environment/schema/connectors/apple-business-manager/scope.toml +112 -0
- package/runtime/gatewaysdk/environment/schema/connectors/apple-business-manager/world.json +1380 -0
- package/runtime/gatewaysdk/environment/schema/connectors/base.json +28 -0
- package/runtime/gatewaysdk/environment/schema/connectors/custom/handlers.py +18 -0
- package/runtime/gatewaysdk/environment/schema/connectors/custom/world.json +123 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/handlers.py +81 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/parity.json +37 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/provenance.json +134 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/scope.toml +158 -0
- package/runtime/gatewaysdk/environment/schema/connectors/github/world.json +5869 -0
- package/runtime/gatewaysdk/environment/schema/connectors/google-calendar/handlers.py +23 -0
- package/runtime/gatewaysdk/environment/schema/connectors/google-calendar/world.json +209 -0
- package/runtime/gatewaysdk/environment/schema/connectors/google-drive/handlers.py +18 -0
- package/runtime/gatewaysdk/environment/schema/connectors/google-drive/world.json +286 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/handlers.py +34 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/parity.json +49 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/provenance.json +181 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/scope.toml +128 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jamf/world.json +2218 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jira/handlers.py +28 -0
- package/runtime/gatewaysdk/environment/schema/connectors/jira/world.json +389 -0
- package/runtime/gatewaysdk/environment/schema/connectors/kandji/handlers.py +38 -0
- package/runtime/gatewaysdk/environment/schema/connectors/kandji/parity.json +29 -0
- package/runtime/gatewaysdk/environment/schema/connectors/kandji/provenance.json +152 -0
- package/runtime/gatewaysdk/environment/schema/connectors/kandji/scope.toml +120 -0
- package/runtime/gatewaysdk/environment/schema/connectors/kandji/world.json +765 -0
- package/runtime/gatewaysdk/environment/schema/connectors/linear/handlers.py +20 -0
- package/runtime/gatewaysdk/environment/schema/connectors/linear/world.json +363 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/fidelity.json +45 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/handlers.py +1 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/provenance.json +851 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/scope.md +231 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/teams-types.json +1710 -0
- package/runtime/gatewaysdk/environment/schema/connectors/microsoft-teams/world.json +858 -0
- package/runtime/gatewaysdk/environment/schema/connectors/netsuite/handlers.py +32 -0
- package/runtime/gatewaysdk/environment/schema/connectors/netsuite/parity.json +50 -0
- package/runtime/gatewaysdk/environment/schema/connectors/netsuite/provenance.json +137 -0
- package/runtime/gatewaysdk/environment/schema/connectors/netsuite/scope.toml +78 -0
- package/runtime/gatewaysdk/environment/schema/connectors/netsuite/world.json +16243 -0
- package/runtime/gatewaysdk/environment/schema/connectors/salesforce/handlers.py +806 -0
- package/runtime/gatewaysdk/environment/schema/connectors/salesforce/parity.json +46 -0
- package/runtime/gatewaysdk/environment/schema/connectors/salesforce/provenance.json +143 -0
- package/runtime/gatewaysdk/environment/schema/connectors/salesforce/scope.toml +174 -0
- package/runtime/gatewaysdk/environment/schema/connectors/salesforce/world.json +4695 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/MODEL.md +236 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/capabilities.json +1039 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/handlers.py +762 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/parity.json +65 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/provenance.json +294 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/scope.toml +44 -0
- package/runtime/gatewaysdk/environment/schema/connectors/slack/world.json +4993 -0
- package/runtime/gatewaysdk/environment/schema/connectors/workday/handlers.py +75 -0
- package/runtime/gatewaysdk/environment/schema/connectors/workday/parity.json +36 -0
- package/runtime/gatewaysdk/environment/schema/connectors/workday/provenance.json +179 -0
- package/runtime/gatewaysdk/environment/schema/connectors/workday/scope.toml +180 -0
- package/runtime/gatewaysdk/environment/schema/connectors/workday/world.json +806 -0
- package/runtime/gatewaysdk/environment/schema/data.py +593 -0
- package/runtime/gatewaysdk/environment/schema/extract.py +263 -0
- package/runtime/gatewaysdk/environment/schema/host.py +2407 -0
- package/runtime/gatewaysdk/environment/schema/host_surfaces.py +275 -0
- package/runtime/gatewaysdk/environment/schema/platform.py +1272 -0
- package/runtime/gatewaysdk/environment/schema/scaffold.py +250 -0
- package/runtime/gatewaysdk/environment/schema/skill.py +118 -0
- package/runtime/gatewaysdk/environment/schema/skills/agents/connector-template-author.md +202 -0
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/CONNECTORS.md +331 -0
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/CONVENTIONS.md +100 -0
- package/runtime/gatewaysdk/environment/schema/skills/connector-schema-authoring/SKILL.md +313 -0
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/ROWS.md +223 -0
- package/runtime/gatewaysdk/environment/schema/skills/world-data-ingestion/SKILL.md +293 -0
- package/runtime/gatewaysdk/environment/schema/skills/worlds-getting-started/SKILL.md +390 -0
- package/runtime/gatewaysdk/environment/schema/slack.py +757 -0
- package/runtime/gatewaysdk/environment/schema/snapshot.py +479 -0
- package/runtime/gatewaysdk/environment/schema/store.py +904 -0
- package/runtime/gatewaysdk/environment/schema/tasks.py +525 -0
- package/runtime/gatewaysdk/environment/schema/tools_world.py +322 -0
- package/runtime/gatewaysdk/environment/schema/validation.py +577 -0
- package/runtime/gatewaysdk/environment/schema/worker.py +78 -0
- package/runtime/gatewaysdk/environment/schema/workers.py +500 -0
- package/runtime/gatewaysdk/environment/schema/world_tests.py +336 -0
- package/skills/agents/connector-template-author.md +202 -0
- package/skills/connector-schema-authoring/CONNECTORS.md +331 -0
- package/skills/connector-schema-authoring/CONVENTIONS.md +100 -0
- package/skills/connector-schema-authoring/SKILL.md +313 -0
- package/skills/world-data-ingestion/ROWS.md +223 -0
- package/skills/world-data-ingestion/SKILL.md +293 -0
- package/skills/worlds-getting-started/SKILL.md +390 -0
|
@@ -0,0 +1,762 @@
|
|
|
1
|
+
"""Slack collaboration operations over one canonical, validated schema store."""
|
|
2
|
+
|
|
3
|
+
import base64
|
|
4
|
+
import functools
|
|
5
|
+
import hashlib
|
|
6
|
+
import json
|
|
7
|
+
from fractions import Fraction
|
|
8
|
+
from pathlib import Path
|
|
9
|
+
|
|
10
|
+
from gateway_env import store, tool
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
@tool(name="list_channels")
|
|
14
|
+
def list_channels(workspace_id):
|
|
15
|
+
if Path(__file__).with_name("slack-principal.json").is_file():
|
|
16
|
+
if workspace_id != _principal()[0]["workspace_id"]:
|
|
17
|
+
raise ValueError("channel_not_found")
|
|
18
|
+
return [
|
|
19
|
+
row
|
|
20
|
+
for row in store.list("slack_channels")
|
|
21
|
+
if row["workspace_id"] == workspace_id
|
|
22
|
+
and (not (row.get("is_private") or row.get("is_im") or row.get("is_mpim")) or _member(row["id"]))
|
|
23
|
+
]
|
|
24
|
+
return [channel for channel in store.list("slack_channels") if channel["workspace_id"] == workspace_id]
|
|
25
|
+
|
|
26
|
+
|
|
27
|
+
def _timestamp_key(message):
|
|
28
|
+
"""Compare decimal strings exactly, including unequal fractional widths."""
|
|
29
|
+
timestamp = message["ts"]
|
|
30
|
+
seconds, separator, fraction = timestamp.partition(".")
|
|
31
|
+
if not separator or not seconds or not fraction or any(c not in "0123456789" for c in seconds + fraction):
|
|
32
|
+
raise ValueError("list_messages requires decimal Slack timestamp strings")
|
|
33
|
+
seconds = seconds.lstrip("0") or "0"
|
|
34
|
+
return len(seconds), seconds, fraction.rstrip("0"), timestamp
|
|
35
|
+
|
|
36
|
+
|
|
37
|
+
@tool(name="list_messages")
|
|
38
|
+
def list_messages(workspace_id, channel_id):
|
|
39
|
+
if Path(__file__).with_name("slack-principal.json").is_file():
|
|
40
|
+
if workspace_id != _principal()[0]["workspace_id"]:
|
|
41
|
+
raise ValueError("channel_not_found")
|
|
42
|
+
_channel(channel_id)
|
|
43
|
+
messages = [
|
|
44
|
+
message
|
|
45
|
+
for message in store.list("slack_messages")
|
|
46
|
+
if message["workspace_id"] == workspace_id and message["channel_id"] == channel_id
|
|
47
|
+
]
|
|
48
|
+
return sorted(messages, key=_timestamp_key)
|
|
49
|
+
|
|
50
|
+
|
|
51
|
+
@tool(name="post_message")
|
|
52
|
+
def post_message(message):
|
|
53
|
+
if Path(__file__).with_name("slack-principal.json").is_file():
|
|
54
|
+
principal, _ = _principal()
|
|
55
|
+
if message["workspace_id"] != principal["workspace_id"] or message.get("user") != principal["user_id"]:
|
|
56
|
+
raise ValueError("no_permission")
|
|
57
|
+
_channel(message["channel_id"], write=True, membership=True)
|
|
58
|
+
if (
|
|
59
|
+
not message.get("user")
|
|
60
|
+
or not message.get("text")
|
|
61
|
+
or any(field in message for field in ("bot_id", "app_id", "subtype"))
|
|
62
|
+
):
|
|
63
|
+
raise ValueError("post_message supports top-level human text messages only")
|
|
64
|
+
member = {
|
|
65
|
+
"workspace_id": message["workspace_id"],
|
|
66
|
+
"channel_id": message["channel_id"],
|
|
67
|
+
"user_id": message["user"],
|
|
68
|
+
}
|
|
69
|
+
if store.get("slack_memberships", member) is None:
|
|
70
|
+
raise ValueError("not_in_channel: the author is not a member of this channel")
|
|
71
|
+
author = store.get("slack_users", {"workspace_id": message["workspace_id"], "id": message["user"]})
|
|
72
|
+
if author.get("deleted") or author.get("is_bot") or author.get("is_app_user"):
|
|
73
|
+
raise ValueError("post_message supports top-level human text messages only")
|
|
74
|
+
return store.insert("slack_messages", message, source={"record": "tool.post_message"})
|
|
75
|
+
|
|
76
|
+
|
|
77
|
+
# Slack-shaped executable subset. Every adapter delegates to these canonical
|
|
78
|
+
# operations; principal identity is immutable bundle configuration, never input.
|
|
79
|
+
class _SlackError(Exception):
|
|
80
|
+
pass
|
|
81
|
+
|
|
82
|
+
|
|
83
|
+
def _api(fn):
|
|
84
|
+
@functools.wraps(fn)
|
|
85
|
+
def invoke(*args, **kwargs):
|
|
86
|
+
try:
|
|
87
|
+
return fn(*args, **kwargs)
|
|
88
|
+
except _SlackError as error:
|
|
89
|
+
return {"ok": False, "error": str(error)}
|
|
90
|
+
|
|
91
|
+
return invoke
|
|
92
|
+
|
|
93
|
+
|
|
94
|
+
def _principal():
|
|
95
|
+
path = Path(__file__).with_name("slack-principal.json")
|
|
96
|
+
if not path.is_file():
|
|
97
|
+
raise _SlackError("not_authed")
|
|
98
|
+
principal = json.loads(path.read_text())
|
|
99
|
+
actor = store.get("slack_users", {"workspace_id": principal["workspace_id"], "id": principal["user_id"]})
|
|
100
|
+
if actor is None or actor.get("deleted") or actor.get("suspended"):
|
|
101
|
+
raise _SlackError("account_inactive")
|
|
102
|
+
return principal, actor
|
|
103
|
+
|
|
104
|
+
|
|
105
|
+
def _rows(entity, **where):
|
|
106
|
+
principal, _ = _principal()
|
|
107
|
+
return [
|
|
108
|
+
row
|
|
109
|
+
for row in store.list(entity)
|
|
110
|
+
if row.get("workspace_id") == principal["workspace_id"] and all(row.get(k) == v for k, v in where.items())
|
|
111
|
+
]
|
|
112
|
+
|
|
113
|
+
|
|
114
|
+
class _ReadRows:
|
|
115
|
+
"""Lazy lookup maps for one read response, never shared with mutations."""
|
|
116
|
+
|
|
117
|
+
def __init__(self):
|
|
118
|
+
self.workspace = _principal()[0]["workspace_id"]
|
|
119
|
+
self.entities = {}
|
|
120
|
+
self.indexes = {}
|
|
121
|
+
|
|
122
|
+
def __call__(self, entity, **where):
|
|
123
|
+
if entity not in self.entities:
|
|
124
|
+
self.entities[entity] = [row for row in store.list(entity) if row.get("workspace_id") == self.workspace]
|
|
125
|
+
values = self.entities[entity]
|
|
126
|
+
if not where:
|
|
127
|
+
return values
|
|
128
|
+
fields = tuple(sorted(where))
|
|
129
|
+
key = entity, fields
|
|
130
|
+
if key not in self.indexes:
|
|
131
|
+
groups = {}
|
|
132
|
+
for row in values:
|
|
133
|
+
groups.setdefault(tuple(row.get(field) for field in fields), []).append(row)
|
|
134
|
+
self.indexes[key] = groups
|
|
135
|
+
return self.indexes[key].get(tuple(where[field] for field in fields), [])
|
|
136
|
+
|
|
137
|
+
|
|
138
|
+
def _key(**fields):
|
|
139
|
+
principal, _ = _principal()
|
|
140
|
+
return {"workspace_id": principal["workspace_id"], **fields}
|
|
141
|
+
|
|
142
|
+
|
|
143
|
+
def _source(method):
|
|
144
|
+
principal, _ = _principal()
|
|
145
|
+
return {"record": "tool.slack." + method + ":" + principal["user_id"]}
|
|
146
|
+
|
|
147
|
+
|
|
148
|
+
def _member(channel):
|
|
149
|
+
principal, _ = _principal()
|
|
150
|
+
return store.get("slack_memberships", _key(channel_id=channel, user_id=principal["user_id"])) is not None
|
|
151
|
+
|
|
152
|
+
|
|
153
|
+
def _channel(channel, *, write=False, membership=False):
|
|
154
|
+
principal, actor = _principal()
|
|
155
|
+
row = store.get("slack_channels", _key(id=channel))
|
|
156
|
+
if row is None or ((row.get("is_private") or row.get("is_im") or row.get("is_mpim")) and not _member(channel)):
|
|
157
|
+
raise _SlackError("channel_not_found")
|
|
158
|
+
if (membership or actor.get("is_bot")) and not _member(channel):
|
|
159
|
+
raise _SlackError("not_in_channel")
|
|
160
|
+
if write and row.get("is_archived"):
|
|
161
|
+
raise _SlackError("is_archived")
|
|
162
|
+
state = store.get("slack_conversation_states", _key(channel_id=channel, user_id=principal["user_id"]))
|
|
163
|
+
if write and (row.get("is_frozen") or (state and state.get("is_read_only"))):
|
|
164
|
+
raise _SlackError("restricted_action")
|
|
165
|
+
return row
|
|
166
|
+
|
|
167
|
+
|
|
168
|
+
def _public(row):
|
|
169
|
+
return {k: v for k, v in row.items() if k not in ("workspace_id", "channel_id")}
|
|
170
|
+
|
|
171
|
+
|
|
172
|
+
def _conversation(row):
|
|
173
|
+
result = _public(row)
|
|
174
|
+
result["is_member"] = _member(row["id"])
|
|
175
|
+
principal, _ = _principal()
|
|
176
|
+
state = store.get("slack_conversation_states", _key(channel_id=row["id"], user_id=principal["user_id"]))
|
|
177
|
+
if state:
|
|
178
|
+
result.update(
|
|
179
|
+
{
|
|
180
|
+
k: v
|
|
181
|
+
for k, v in state.items()
|
|
182
|
+
if k not in ("workspace_id", "channel_id", "user_id", "counterpart_user_id")
|
|
183
|
+
}
|
|
184
|
+
)
|
|
185
|
+
if "counterpart_user_id" in state:
|
|
186
|
+
result["user"] = state["counterpart_user_id"]
|
|
187
|
+
result["is_member"] = _member(row["id"])
|
|
188
|
+
return result
|
|
189
|
+
|
|
190
|
+
|
|
191
|
+
def _page(items, field, scope, cursor="", limit=100, *, render=None):
|
|
192
|
+
token = hashlib.sha256(json.dumps(scope, sort_keys=True).encode()).hexdigest()[:20]
|
|
193
|
+
offset = 0
|
|
194
|
+
if cursor:
|
|
195
|
+
try:
|
|
196
|
+
parsed = json.loads(base64.urlsafe_b64decode(cursor.encode()).decode())
|
|
197
|
+
if parsed["scope"] != token or type(parsed["offset"]) is not int or parsed["offset"] < 0:
|
|
198
|
+
raise ValueError()
|
|
199
|
+
offset = parsed["offset"]
|
|
200
|
+
except (ValueError, KeyError, TypeError, UnicodeError):
|
|
201
|
+
raise _SlackError("invalid_cursor") from None
|
|
202
|
+
end = offset + limit
|
|
203
|
+
next_cursor = (
|
|
204
|
+
base64.urlsafe_b64encode(json.dumps({"scope": token, "offset": end}, separators=(",", ":")).encode()).decode()
|
|
205
|
+
if end < len(items)
|
|
206
|
+
else ""
|
|
207
|
+
)
|
|
208
|
+
return {
|
|
209
|
+
"ok": True,
|
|
210
|
+
field: [render(item) for item in items[offset:end]] if render else items[offset:end],
|
|
211
|
+
"response_metadata": {"next_cursor": next_cursor},
|
|
212
|
+
"has_more": bool(next_cursor),
|
|
213
|
+
}
|
|
214
|
+
|
|
215
|
+
|
|
216
|
+
def _number(ts, error="invalid_ts_latest"):
|
|
217
|
+
try:
|
|
218
|
+
if not isinstance(ts, str) or not ts or not all(c in "0123456789" for c in ts):
|
|
219
|
+
_timestamp_key({"ts": ts})
|
|
220
|
+
return Fraction(ts)
|
|
221
|
+
except (ValueError, TypeError, AttributeError, ZeroDivisionError):
|
|
222
|
+
raise _SlackError(error) from None
|
|
223
|
+
|
|
224
|
+
|
|
225
|
+
def _message(channel, ts, *, write=False):
|
|
226
|
+
_channel(channel, write=write, membership=write)
|
|
227
|
+
_number(ts, "bad_timestamp")
|
|
228
|
+
row = store.get("slack_messages", _key(channel_id=channel, ts=ts))
|
|
229
|
+
if row is None:
|
|
230
|
+
raise _SlackError("message_not_found")
|
|
231
|
+
return row
|
|
232
|
+
|
|
233
|
+
|
|
234
|
+
def _render_message(row, rows=None):
|
|
235
|
+
rows = rows or _rows
|
|
236
|
+
result = _public(row)
|
|
237
|
+
channel, ts = row["channel_id"], row["ts"]
|
|
238
|
+
thread = store.get("slack_message_threads", _key(channel_id=channel, reply_ts=ts))
|
|
239
|
+
children = rows("slack_message_threads", channel_id=channel, parent_ts=ts)
|
|
240
|
+
if thread or children:
|
|
241
|
+
result["thread_ts"] = thread["parent_ts"] if thread else ts
|
|
242
|
+
reactions = rows("slack_reaction_summaries", channel_id=channel, message_ts=ts)
|
|
243
|
+
if reactions:
|
|
244
|
+
result["reactions"] = [
|
|
245
|
+
{
|
|
246
|
+
"name": r["name"],
|
|
247
|
+
"count": r["count"],
|
|
248
|
+
"users": [
|
|
249
|
+
u["user_id"]
|
|
250
|
+
for u in rows("slack_reaction_users", channel_id=channel, message_ts=ts, name=r["name"])
|
|
251
|
+
],
|
|
252
|
+
}
|
|
253
|
+
for r in reactions
|
|
254
|
+
]
|
|
255
|
+
files = []
|
|
256
|
+
for association in rows("slack_message_files", channel_id=channel, message_ts=ts):
|
|
257
|
+
value = store.get("slack_files", _key(id=association["file_id"]))
|
|
258
|
+
files.append((association.get("position", 0), _public(value)))
|
|
259
|
+
for association in rows("slack_unavailable_message_files", channel_id=channel, message_ts=ts):
|
|
260
|
+
files.append((association.get("position", 0), {"id": association["file_id"], **association["reference"]}))
|
|
261
|
+
if files:
|
|
262
|
+
result["files"] = [value for _, value in sorted(files, key=lambda pair: (pair[0], pair[1]["id"]))]
|
|
263
|
+
return result
|
|
264
|
+
|
|
265
|
+
|
|
266
|
+
def _next_ts():
|
|
267
|
+
# A deterministic logical clock, explicitly not vendor/server wall time.
|
|
268
|
+
seconds = (
|
|
269
|
+
max(
|
|
270
|
+
(int(_number(row["ts"]).numerator // _number(row["ts"]).denominator) for row in _rows("slack_messages")),
|
|
271
|
+
default=1700000000,
|
|
272
|
+
)
|
|
273
|
+
+ 1
|
|
274
|
+
)
|
|
275
|
+
return str(seconds) + ".000001"
|
|
276
|
+
|
|
277
|
+
|
|
278
|
+
def _user(row, rows=None):
|
|
279
|
+
rows = rows or _rows
|
|
280
|
+
result = _public(row)
|
|
281
|
+
if isinstance(result.get("profile"), dict):
|
|
282
|
+
profile = dict(result["profile"])
|
|
283
|
+
marker = profile.pop("fields_state", None)
|
|
284
|
+
fields = rows("slack_user_profile_fields", user_id=row["id"])
|
|
285
|
+
if marker == "null":
|
|
286
|
+
profile["fields"] = None
|
|
287
|
+
elif marker == "present" or fields:
|
|
288
|
+
profile["fields"] = {
|
|
289
|
+
f["field_id"]: {k: v for k, v in f.items() if k not in ("workspace_id", "user_id", "field_id")}
|
|
290
|
+
for f in fields
|
|
291
|
+
}
|
|
292
|
+
result["profile"] = profile
|
|
293
|
+
return result
|
|
294
|
+
|
|
295
|
+
|
|
296
|
+
@tool(name="users.list")
|
|
297
|
+
@_api
|
|
298
|
+
def users_list(cursor="", limit=100):
|
|
299
|
+
rows = _ReadRows()
|
|
300
|
+
return _page(
|
|
301
|
+
rows("slack_users"),
|
|
302
|
+
"members",
|
|
303
|
+
["users.list", _principal()[0]],
|
|
304
|
+
cursor,
|
|
305
|
+
limit,
|
|
306
|
+
render=lambda row: _user(row, rows),
|
|
307
|
+
)
|
|
308
|
+
|
|
309
|
+
|
|
310
|
+
@tool(name="users.info")
|
|
311
|
+
@_api
|
|
312
|
+
def users_info(user):
|
|
313
|
+
value = store.get("slack_users", _key(id=user))
|
|
314
|
+
if value is None:
|
|
315
|
+
raise _SlackError("user_not_found")
|
|
316
|
+
return {"ok": True, "user": _user(value)}
|
|
317
|
+
|
|
318
|
+
|
|
319
|
+
@tool(name="conversations.list")
|
|
320
|
+
@_api
|
|
321
|
+
def conversations_list(cursor="", limit=100, types="public_channel", exclude_archived=False):
|
|
322
|
+
allowed = types.split(",")
|
|
323
|
+
if any(kind not in ("public_channel", "private_channel", "im", "mpim") for kind in allowed):
|
|
324
|
+
raise _SlackError("invalid_types")
|
|
325
|
+
values = []
|
|
326
|
+
for row in _rows("slack_channels"):
|
|
327
|
+
kind = (
|
|
328
|
+
"im"
|
|
329
|
+
if row.get("is_im")
|
|
330
|
+
else "mpim" if row.get("is_mpim") else "private_channel" if row.get("is_private") else "public_channel"
|
|
331
|
+
)
|
|
332
|
+
if (
|
|
333
|
+
kind not in allowed
|
|
334
|
+
or (exclude_archived and row.get("is_archived"))
|
|
335
|
+
or (kind != "public_channel" and not _member(row["id"]))
|
|
336
|
+
):
|
|
337
|
+
continue
|
|
338
|
+
values.append(row)
|
|
339
|
+
return _page(
|
|
340
|
+
values,
|
|
341
|
+
"channels",
|
|
342
|
+
["conversations.list", _principal()[0], types, exclude_archived],
|
|
343
|
+
cursor,
|
|
344
|
+
limit,
|
|
345
|
+
render=_conversation,
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
|
|
349
|
+
@tool(name="conversations.info")
|
|
350
|
+
@_api
|
|
351
|
+
def conversations_info(channel):
|
|
352
|
+
return {"ok": True, "channel": _conversation(_channel(channel))}
|
|
353
|
+
|
|
354
|
+
|
|
355
|
+
@tool(name="conversations.members")
|
|
356
|
+
@_api
|
|
357
|
+
def conversations_members(channel, cursor="", limit=100):
|
|
358
|
+
_channel(channel)
|
|
359
|
+
return _page(
|
|
360
|
+
[row["user_id"] for row in _rows("slack_memberships", channel_id=channel)],
|
|
361
|
+
"members",
|
|
362
|
+
["conversations.members", _principal()[0], channel],
|
|
363
|
+
cursor,
|
|
364
|
+
limit,
|
|
365
|
+
)
|
|
366
|
+
|
|
367
|
+
|
|
368
|
+
def _history(channel, *, parent=None, cursor="", limit=100, oldest=None, latest=None, inclusive=False):
|
|
369
|
+
_channel(channel)
|
|
370
|
+
if parent is not None:
|
|
371
|
+
selected = _message(channel, parent)
|
|
372
|
+
association = store.get("slack_message_threads", _key(channel_id=channel, reply_ts=parent))
|
|
373
|
+
if association:
|
|
374
|
+
parent = association["parent_ts"]
|
|
375
|
+
if selected.get("subtype") in ("channel_join", "channel_leave"):
|
|
376
|
+
raise _SlackError("thread_not_found")
|
|
377
|
+
lower = _number(oldest, "invalid_ts_oldest") if oldest is not None else None
|
|
378
|
+
upper = _number(latest) if latest is not None else None
|
|
379
|
+
rows = _ReadRows()
|
|
380
|
+
threads = {row["reply_ts"]: row["parent_ts"] for row in rows("slack_message_threads", channel_id=channel)}
|
|
381
|
+
selected = []
|
|
382
|
+
for row in rows("slack_messages", channel_id=channel):
|
|
383
|
+
ts = row["ts"]
|
|
384
|
+
if parent is None and ts in threads and row.get("subtype") != "thread_broadcast":
|
|
385
|
+
continue
|
|
386
|
+
if parent is not None and ts != parent and threads.get(ts) != parent:
|
|
387
|
+
continue
|
|
388
|
+
numeric = _number(ts)
|
|
389
|
+
if lower is not None and (numeric < lower or (numeric == lower and not inclusive)):
|
|
390
|
+
continue
|
|
391
|
+
if upper is not None and (numeric > upper or (numeric == upper and not inclusive)):
|
|
392
|
+
continue
|
|
393
|
+
selected.append(row)
|
|
394
|
+
selected.sort(key=_timestamp_key, reverse=parent is None)
|
|
395
|
+
return _page(
|
|
396
|
+
selected,
|
|
397
|
+
"messages",
|
|
398
|
+
["history", _principal()[0], channel, parent, oldest, latest, inclusive],
|
|
399
|
+
cursor,
|
|
400
|
+
limit,
|
|
401
|
+
render=lambda row: _render_message(row, rows),
|
|
402
|
+
)
|
|
403
|
+
|
|
404
|
+
|
|
405
|
+
@tool(name="conversations.history")
|
|
406
|
+
@_api
|
|
407
|
+
def conversations_history(channel, cursor="", limit=100, oldest=None, latest=None, inclusive=False):
|
|
408
|
+
return _history(channel, cursor=cursor, limit=limit, oldest=oldest, latest=latest, inclusive=inclusive)
|
|
409
|
+
|
|
410
|
+
|
|
411
|
+
@tool(name="conversations.replies")
|
|
412
|
+
@_api
|
|
413
|
+
def conversations_replies(channel, ts, cursor="", limit=100, oldest=None, latest=None, inclusive=False):
|
|
414
|
+
return _history(channel, parent=ts, cursor=cursor, limit=limit, oldest=oldest, latest=latest, inclusive=inclusive)
|
|
415
|
+
|
|
416
|
+
|
|
417
|
+
@tool(name="conversations.join")
|
|
418
|
+
@_api
|
|
419
|
+
def conversations_join(channel):
|
|
420
|
+
row = _channel(channel, write=True)
|
|
421
|
+
if row.get("is_private") or row.get("is_im") or row.get("is_mpim"):
|
|
422
|
+
raise _SlackError("method_not_supported_for_channel_type")
|
|
423
|
+
principal, _ = _principal()
|
|
424
|
+
if not _member(channel):
|
|
425
|
+
store.insert(
|
|
426
|
+
"slack_memberships",
|
|
427
|
+
_key(channel_id=channel, user_id=principal["user_id"]),
|
|
428
|
+
source=_source("conversations.join"),
|
|
429
|
+
)
|
|
430
|
+
if "num_members" in row:
|
|
431
|
+
store.update(
|
|
432
|
+
"slack_channels",
|
|
433
|
+
_key(id=channel),
|
|
434
|
+
{"num_members": row["num_members"] + 1},
|
|
435
|
+
source=_source("conversations.join"),
|
|
436
|
+
)
|
|
437
|
+
return {"ok": True, "channel": _conversation(store.get("slack_channels", _key(id=channel)))}
|
|
438
|
+
|
|
439
|
+
|
|
440
|
+
@tool(name="conversations.leave")
|
|
441
|
+
@_api
|
|
442
|
+
def conversations_leave(channel):
|
|
443
|
+
row = _channel(channel, write=True, membership=True)
|
|
444
|
+
if row.get("is_general") or row.get("is_im"):
|
|
445
|
+
raise _SlackError("cant_leave_general" if row.get("is_general") else "method_not_supported_for_channel_type")
|
|
446
|
+
principal, _ = _principal()
|
|
447
|
+
store.delete(
|
|
448
|
+
"slack_memberships",
|
|
449
|
+
_key(channel_id=channel, user_id=principal["user_id"]),
|
|
450
|
+
source=_source("conversations.leave"),
|
|
451
|
+
)
|
|
452
|
+
if "num_members" in row:
|
|
453
|
+
store.update(
|
|
454
|
+
"slack_channels",
|
|
455
|
+
_key(id=channel),
|
|
456
|
+
{"num_members": max(0, row["num_members"] - 1)},
|
|
457
|
+
source=_source("conversations.leave"),
|
|
458
|
+
)
|
|
459
|
+
return {"ok": True}
|
|
460
|
+
|
|
461
|
+
|
|
462
|
+
@tool(name="chat.postMessage")
|
|
463
|
+
@_api
|
|
464
|
+
def chat_post_message(
|
|
465
|
+
channel, text=None, blocks=None, attachments=None, thread_ts=None, reply_broadcast=False, client_msg_id=None
|
|
466
|
+
):
|
|
467
|
+
_channel(channel, write=True, membership=True)
|
|
468
|
+
if not text and not blocks and not attachments:
|
|
469
|
+
raise _SlackError("no_text")
|
|
470
|
+
if reply_broadcast and thread_ts is None:
|
|
471
|
+
raise _SlackError("invalid_arguments")
|
|
472
|
+
principal, _ = _principal()
|
|
473
|
+
parent = None
|
|
474
|
+
if thread_ts:
|
|
475
|
+
parent = _message(channel, thread_ts)
|
|
476
|
+
if store.get("slack_message_threads", _key(channel_id=channel, reply_ts=thread_ts)) or parent.get(
|
|
477
|
+
"subtype"
|
|
478
|
+
) in ("channel_join", "channel_leave"):
|
|
479
|
+
raise _SlackError("thread_not_found")
|
|
480
|
+
if client_msg_id:
|
|
481
|
+
prior = [
|
|
482
|
+
m
|
|
483
|
+
for m in _rows("slack_messages", channel_id=channel, user=principal["user_id"])
|
|
484
|
+
if m.get("client_msg_id") == client_msg_id
|
|
485
|
+
]
|
|
486
|
+
if prior:
|
|
487
|
+
return {"ok": True, "channel": channel, "ts": prior[0]["ts"], "message": _render_message(prior[0])}
|
|
488
|
+
value = _key(channel_id=channel, ts=_next_ts(), type="message", user=principal["user_id"])
|
|
489
|
+
for name, content in (
|
|
490
|
+
("text", text),
|
|
491
|
+
("blocks", blocks),
|
|
492
|
+
("attachments", attachments),
|
|
493
|
+
("client_msg_id", client_msg_id),
|
|
494
|
+
):
|
|
495
|
+
if content is not None:
|
|
496
|
+
value[name] = content
|
|
497
|
+
if reply_broadcast:
|
|
498
|
+
value["subtype"] = "thread_broadcast"
|
|
499
|
+
store.insert("slack_messages", value, source=_source("chat.postMessage"))
|
|
500
|
+
if parent:
|
|
501
|
+
store.insert(
|
|
502
|
+
"slack_message_threads",
|
|
503
|
+
_key(channel_id=channel, reply_ts=value["ts"], parent_ts=thread_ts),
|
|
504
|
+
source=_source("chat.postMessage"),
|
|
505
|
+
)
|
|
506
|
+
store.update(
|
|
507
|
+
"slack_messages",
|
|
508
|
+
_key(channel_id=channel, ts=thread_ts),
|
|
509
|
+
{"reply_count": parent.get("reply_count", 0) + 1, "latest_reply": value["ts"]},
|
|
510
|
+
source=_source("chat.postMessage"),
|
|
511
|
+
)
|
|
512
|
+
return {"ok": True, "channel": channel, "ts": value["ts"], "message": _render_message(value)}
|
|
513
|
+
|
|
514
|
+
|
|
515
|
+
@tool(name="chat.update")
|
|
516
|
+
@_api
|
|
517
|
+
def chat_update(channel, ts, text=None, blocks=None, attachments=None):
|
|
518
|
+
row = _message(channel, ts, write=True)
|
|
519
|
+
principal, _ = _principal()
|
|
520
|
+
if row.get("user") != principal["user_id"] or row.get("subtype") not in (None, "me_message", "thread_broadcast"):
|
|
521
|
+
raise _SlackError("cant_update_message")
|
|
522
|
+
patch = {
|
|
523
|
+
name: value
|
|
524
|
+
for name, value in (("text", text), ("blocks", blocks), ("attachments", attachments))
|
|
525
|
+
if value is not None
|
|
526
|
+
}
|
|
527
|
+
if not patch:
|
|
528
|
+
raise _SlackError("no_text")
|
|
529
|
+
if text is not None and blocks is None:
|
|
530
|
+
patch["blocks"] = []
|
|
531
|
+
patch["edited"] = {"user": principal["user_id"], "ts": _next_ts()}
|
|
532
|
+
updated = store.update("slack_messages", _key(channel_id=channel, ts=ts), patch, source=_source("chat.update"))
|
|
533
|
+
return {
|
|
534
|
+
"ok": True,
|
|
535
|
+
"channel": channel,
|
|
536
|
+
"ts": ts,
|
|
537
|
+
"text": updated.get("text", ""),
|
|
538
|
+
"message": _render_message(updated),
|
|
539
|
+
}
|
|
540
|
+
|
|
541
|
+
|
|
542
|
+
@tool(name="chat.delete")
|
|
543
|
+
@_api
|
|
544
|
+
def chat_delete(channel, ts):
|
|
545
|
+
row = _message(channel, ts, write=True)
|
|
546
|
+
principal, _ = _principal()
|
|
547
|
+
if row.get("user") != principal["user_id"]:
|
|
548
|
+
raise _SlackError("cant_delete_message")
|
|
549
|
+
if (
|
|
550
|
+
_rows("slack_message_threads", channel_id=channel, parent_ts=ts)
|
|
551
|
+
or _rows("slack_scheduled_message_threads", channel_id=channel, parent_ts=ts)
|
|
552
|
+
or _rows("slack_ephemeral_message_threads", channel_id=channel, parent_ts=ts)
|
|
553
|
+
):
|
|
554
|
+
raise _SlackError("unsupported_thread_root_delete")
|
|
555
|
+
association = store.get("slack_message_threads", _key(channel_id=channel, reply_ts=ts))
|
|
556
|
+
if association:
|
|
557
|
+
store.delete("slack_message_threads", _key(channel_id=channel, reply_ts=ts), source=_source("chat.delete"))
|
|
558
|
+
parent = _message(channel, association["parent_ts"])
|
|
559
|
+
store.update(
|
|
560
|
+
"slack_messages",
|
|
561
|
+
_key(channel_id=channel, ts=parent["ts"]),
|
|
562
|
+
{"reply_count": max(0, parent.get("reply_count", 0) - 1)},
|
|
563
|
+
source=_source("chat.delete"),
|
|
564
|
+
)
|
|
565
|
+
for entity, extra in (
|
|
566
|
+
("slack_reaction_users", ("name", "user_id")),
|
|
567
|
+
("slack_reaction_summaries", ("name",)),
|
|
568
|
+
("slack_message_user_states", ("user_id",)),
|
|
569
|
+
("slack_file_shares", ("file_id",)),
|
|
570
|
+
("slack_message_files", ("file_id",)),
|
|
571
|
+
("slack_unavailable_message_files", ("file_id",)),
|
|
572
|
+
("slack_pins", ()),
|
|
573
|
+
):
|
|
574
|
+
for dependent in _rows(entity, channel_id=channel, message_ts=ts):
|
|
575
|
+
store.delete(
|
|
576
|
+
entity,
|
|
577
|
+
_key(channel_id=channel, message_ts=ts, **{k: dependent[k] for k in extra}),
|
|
578
|
+
source=_source("chat.delete"),
|
|
579
|
+
)
|
|
580
|
+
store.delete("slack_messages", _key(channel_id=channel, ts=ts), source=_source("chat.delete"))
|
|
581
|
+
return {"ok": True, "channel": channel, "ts": ts}
|
|
582
|
+
|
|
583
|
+
|
|
584
|
+
def _reaction(channel, timestamp, name, remove=False):
|
|
585
|
+
_message(channel, timestamp, write=True)
|
|
586
|
+
if not name or any(c.isspace() for c in name):
|
|
587
|
+
raise _SlackError("invalid_name")
|
|
588
|
+
principal, _ = _principal()
|
|
589
|
+
key = _key(channel_id=channel, message_ts=timestamp, name=name)
|
|
590
|
+
actor_key = {**key, "user_id": principal["user_id"]}
|
|
591
|
+
actor = store.get("slack_reaction_users", actor_key)
|
|
592
|
+
summary = store.get("slack_reaction_summaries", key)
|
|
593
|
+
method = "reactions.remove" if remove else "reactions.add"
|
|
594
|
+
if remove:
|
|
595
|
+
if actor is None:
|
|
596
|
+
raise _SlackError("no_reaction")
|
|
597
|
+
store.delete("slack_reaction_users", actor_key, source=_source(method))
|
|
598
|
+
count = max(0, summary["count"] - 1)
|
|
599
|
+
if count == 0 and not _rows("slack_reaction_users", channel_id=channel, message_ts=timestamp, name=name):
|
|
600
|
+
store.delete("slack_reaction_summaries", key, source=_source(method))
|
|
601
|
+
else:
|
|
602
|
+
store.update("slack_reaction_summaries", key, {"count": count}, source=_source(method))
|
|
603
|
+
else:
|
|
604
|
+
if actor:
|
|
605
|
+
raise _SlackError("already_reacted")
|
|
606
|
+
if summary:
|
|
607
|
+
store.update("slack_reaction_summaries", key, {"count": summary["count"] + 1}, source=_source(method))
|
|
608
|
+
else:
|
|
609
|
+
store.insert("slack_reaction_summaries", {**key, "count": 1}, source=_source(method))
|
|
610
|
+
store.insert("slack_reaction_users", actor_key, source=_source(method))
|
|
611
|
+
return {"ok": True}
|
|
612
|
+
|
|
613
|
+
|
|
614
|
+
@tool(name="reactions.add")
|
|
615
|
+
@_api
|
|
616
|
+
def reactions_add(channel, timestamp, name):
|
|
617
|
+
return _reaction(channel, timestamp, name)
|
|
618
|
+
|
|
619
|
+
|
|
620
|
+
@tool(name="reactions.remove")
|
|
621
|
+
@_api
|
|
622
|
+
def reactions_remove(channel, timestamp, name):
|
|
623
|
+
return _reaction(channel, timestamp, name, remove=True)
|
|
624
|
+
|
|
625
|
+
|
|
626
|
+
@tool(name="reactions.get")
|
|
627
|
+
@_api
|
|
628
|
+
def reactions_get(channel, timestamp):
|
|
629
|
+
return {"ok": True, "type": "message", "channel": channel, "message": _render_message(_message(channel, timestamp))}
|
|
630
|
+
|
|
631
|
+
|
|
632
|
+
def _file(file_id, *, row=None, rows=None, visible_channels=None):
|
|
633
|
+
rows = rows or _rows
|
|
634
|
+
row = store.get("slack_files", _key(id=file_id)) if row is None else row
|
|
635
|
+
principal, _ = _principal()
|
|
636
|
+
if row is None:
|
|
637
|
+
raise _SlackError("file_not_found")
|
|
638
|
+
visible = row.get("user") == principal["user_id"] or row.get("is_public")
|
|
639
|
+
channels = visible_channels if visible_channels is not None else {}
|
|
640
|
+
for share in rows("slack_message_files", file_id=file_id):
|
|
641
|
+
channel = share["channel_id"]
|
|
642
|
+
if channel not in channels:
|
|
643
|
+
try:
|
|
644
|
+
_channel(channel)
|
|
645
|
+
channels[channel] = True
|
|
646
|
+
except _SlackError:
|
|
647
|
+
channels[channel] = False
|
|
648
|
+
visible = visible or channels[channel]
|
|
649
|
+
if not visible:
|
|
650
|
+
raise _SlackError("file_not_found")
|
|
651
|
+
return row
|
|
652
|
+
|
|
653
|
+
|
|
654
|
+
@tool(name="files.info")
|
|
655
|
+
@_api
|
|
656
|
+
def files_info(file):
|
|
657
|
+
return {"ok": True, "file": _public(_file(file))}
|
|
658
|
+
|
|
659
|
+
|
|
660
|
+
@tool(name="files.list")
|
|
661
|
+
@_api
|
|
662
|
+
def files_list(channel=None, user=None, page=1, count=100):
|
|
663
|
+
if channel:
|
|
664
|
+
_channel(channel)
|
|
665
|
+
files = []
|
|
666
|
+
rows = _ReadRows()
|
|
667
|
+
visible_channels = {}
|
|
668
|
+
for row in rows("slack_files"):
|
|
669
|
+
if user and row.get("user") != user:
|
|
670
|
+
continue
|
|
671
|
+
if channel and not rows("slack_message_files", channel_id=channel, file_id=row["id"]):
|
|
672
|
+
continue
|
|
673
|
+
try:
|
|
674
|
+
files.append(_file(row["id"], row=row, rows=rows, visible_channels=visible_channels))
|
|
675
|
+
except _SlackError:
|
|
676
|
+
continue
|
|
677
|
+
return {
|
|
678
|
+
"ok": True,
|
|
679
|
+
"files": [_public(row) for row in files[(page - 1) * count : page * count]],
|
|
680
|
+
"paging": {"count": count, "total": len(files), "page": page, "pages": (len(files) + count - 1) // count},
|
|
681
|
+
}
|
|
682
|
+
|
|
683
|
+
|
|
684
|
+
@tool(name="pins.list")
|
|
685
|
+
@_api
|
|
686
|
+
def pins_list(channel):
|
|
687
|
+
_channel(channel)
|
|
688
|
+
rows = _ReadRows()
|
|
689
|
+
return {
|
|
690
|
+
"ok": True,
|
|
691
|
+
"items": [
|
|
692
|
+
{
|
|
693
|
+
"type": "message",
|
|
694
|
+
"channel": channel,
|
|
695
|
+
"message": _render_message(_message(channel, r["message_ts"]), rows),
|
|
696
|
+
**{k: r[k] for k in ("created", "created_by") if k in r},
|
|
697
|
+
}
|
|
698
|
+
for r in rows("slack_pins", channel_id=channel)
|
|
699
|
+
],
|
|
700
|
+
}
|
|
701
|
+
|
|
702
|
+
|
|
703
|
+
@tool(name="pins.add")
|
|
704
|
+
@_api
|
|
705
|
+
def pins_add(channel, timestamp):
|
|
706
|
+
_message(channel, timestamp, write=True)
|
|
707
|
+
key = _key(channel_id=channel, message_ts=timestamp)
|
|
708
|
+
if store.get("slack_pins", key):
|
|
709
|
+
raise _SlackError("already_pinned")
|
|
710
|
+
store.insert("slack_pins", {**key, "created_by": _principal()[0]["user_id"]}, source=_source("pins.add"))
|
|
711
|
+
return {"ok": True}
|
|
712
|
+
|
|
713
|
+
|
|
714
|
+
@tool(name="pins.remove")
|
|
715
|
+
@_api
|
|
716
|
+
def pins_remove(channel, timestamp):
|
|
717
|
+
_message(channel, timestamp, write=True)
|
|
718
|
+
key = _key(channel_id=channel, message_ts=timestamp)
|
|
719
|
+
if store.get("slack_pins", key) is None:
|
|
720
|
+
raise _SlackError("no_pin")
|
|
721
|
+
store.delete("slack_pins", key, source=_source("pins.remove"))
|
|
722
|
+
return {"ok": True}
|
|
723
|
+
|
|
724
|
+
|
|
725
|
+
@tool(name="bookmarks.list")
|
|
726
|
+
@_api
|
|
727
|
+
def bookmarks_list(channel_id):
|
|
728
|
+
_channel(channel_id)
|
|
729
|
+
return {"ok": True, "bookmarks": [_public(r) for r in _rows("slack_bookmarks", channel_id=channel_id)]}
|
|
730
|
+
|
|
731
|
+
|
|
732
|
+
@tool(name="usergroups.list")
|
|
733
|
+
@_api
|
|
734
|
+
def usergroups_list(include_users=False, include_disabled=False):
|
|
735
|
+
values = []
|
|
736
|
+
rows = _ReadRows()
|
|
737
|
+
for row in rows("slack_usergroups"):
|
|
738
|
+
if row.get("date_delete") and not include_disabled:
|
|
739
|
+
continue
|
|
740
|
+
value = _public(row)
|
|
741
|
+
value.pop("users_state", None)
|
|
742
|
+
if include_users:
|
|
743
|
+
value["users"] = [r["user_id"] for r in rows("slack_usergroup_memberships", usergroup_id=row["id"])]
|
|
744
|
+
values.append(value)
|
|
745
|
+
return {"ok": True, "usergroups": values}
|
|
746
|
+
|
|
747
|
+
|
|
748
|
+
@tool(name="usergroups.users.list")
|
|
749
|
+
@_api
|
|
750
|
+
def usergroups_users_list(usergroup):
|
|
751
|
+
if store.get("slack_usergroups", _key(id=usergroup)) is None:
|
|
752
|
+
raise _SlackError("usergroup_not_found")
|
|
753
|
+
return {"ok": True, "users": [r["user_id"] for r in _rows("slack_usergroup_memberships", usergroup_id=usergroup)]}
|
|
754
|
+
|
|
755
|
+
|
|
756
|
+
@tool(name="users.getPresence")
|
|
757
|
+
@_api
|
|
758
|
+
def users_get_presence(user):
|
|
759
|
+
row = store.get("slack_presence", _key(user_id=user))
|
|
760
|
+
if row is None:
|
|
761
|
+
raise _SlackError("presence_not_observed")
|
|
762
|
+
return {"ok": True, **{k: v for k, v in _public(row).items() if k != "user_id"}}
|