@cuylabs/channel-slack 0.1.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/LICENSE +201 -0
- package/README.md +168 -0
- package/dist/activity-ByrD9Ftr.d.ts +66 -0
- package/dist/assistant.d.ts +58 -0
- package/dist/assistant.js +188 -0
- package/dist/bolt.d.ts +344 -0
- package/dist/bolt.js +705 -0
- package/dist/chunk-BODPT4I6.js +322 -0
- package/dist/chunk-FPCE5V5Y.js +292 -0
- package/dist/chunk-FX2JOVX5.js +405 -0
- package/dist/chunk-JZG4IETE.js +141 -0
- package/dist/chunk-NE57BLLU.js +0 -0
- package/dist/chunk-TWJGVDA2.js +108 -0
- package/dist/core.d.ts +425 -0
- package/dist/core.js +42 -0
- package/dist/diagnostics.d.ts +105 -0
- package/dist/diagnostics.js +8 -0
- package/dist/feedback.d.ts +137 -0
- package/dist/feedback.js +128 -0
- package/dist/history.d.ts +266 -0
- package/dist/history.js +747 -0
- package/dist/index.d.ts +4 -0
- package/dist/index.js +57 -0
- package/dist/logging-Bl3HfcC8.d.ts +8 -0
- package/dist/policy.d.ts +130 -0
- package/dist/policy.js +16 -0
- package/dist/setup.d.ts +165 -0
- package/dist/setup.js +453 -0
- package/dist/shared.d.ts +2 -0
- package/dist/shared.js +43 -0
- package/dist/targets.d.ts +113 -0
- package/dist/targets.js +484 -0
- package/dist/users.d.ts +109 -0
- package/dist/users.js +240 -0
- package/docs/concepts/activity.md +33 -0
- package/docs/concepts/bolt-runtime.md +30 -0
- package/docs/concepts/message-policy.md +49 -0
- package/docs/concepts/setup-requirements.md +44 -0
- package/docs/concepts/supplemental-history.md +55 -0
- package/docs/recipes/app-mention-handler.md +34 -0
- package/docs/recipes/assistant-thread-handler.md +28 -0
- package/docs/recipes/generate-slack-manifest.md +28 -0
- package/docs/recipes/history-visibility.md +36 -0
- package/docs/recipes/socket-mode-app.md +29 -0
- package/docs/reference/channel-slack-boundary.md +50 -0
- package/docs/reference/exports.md +32 -0
- package/package.json +130 -0
package/LICENSE
ADDED
|
@@ -0,0 +1,201 @@
|
|
|
1
|
+
Apache License
|
|
2
|
+
Version 2.0, January 2004
|
|
3
|
+
http://www.apache.org/licenses/
|
|
4
|
+
|
|
5
|
+
TERMS AND CONDITIONS FOR USE, REPRODUCTION, AND DISTRIBUTION
|
|
6
|
+
|
|
7
|
+
1. Definitions.
|
|
8
|
+
|
|
9
|
+
"License" shall mean the terms and conditions for use, reproduction,
|
|
10
|
+
and distribution as defined by Sections 1 through 9 of this document.
|
|
11
|
+
|
|
12
|
+
"Licensor" shall mean the copyright owner or entity authorized by
|
|
13
|
+
the copyright owner that is granting the License.
|
|
14
|
+
|
|
15
|
+
"Legal Entity" shall mean the union of the acting entity and all
|
|
16
|
+
other entities that control, are controlled by, or are under common
|
|
17
|
+
control with that entity. For the purposes of this definition,
|
|
18
|
+
"control" means (i) the power, direct or indirect, to cause the
|
|
19
|
+
direction or management of such entity, whether by contract or
|
|
20
|
+
otherwise, or (ii) ownership of fifty percent (50%) or more of the
|
|
21
|
+
outstanding shares, or (iii) beneficial ownership of such entity.
|
|
22
|
+
|
|
23
|
+
"You" (or "Your") shall mean an individual or Legal Entity
|
|
24
|
+
exercising permissions granted by this License.
|
|
25
|
+
|
|
26
|
+
"Source" form shall mean the preferred form for making modifications,
|
|
27
|
+
including but not limited to software source code, documentation
|
|
28
|
+
source, and configuration files.
|
|
29
|
+
|
|
30
|
+
"Object" form shall mean any form resulting from mechanical
|
|
31
|
+
transformation or translation of a Source form, including but
|
|
32
|
+
not limited to compiled object code, generated documentation,
|
|
33
|
+
and conversions to other media types.
|
|
34
|
+
|
|
35
|
+
"Work" shall mean the work of authorship, whether in Source or
|
|
36
|
+
Object form, made available under the License, as indicated by a
|
|
37
|
+
copyright notice that is included in or attached to the work
|
|
38
|
+
(an example is provided in the Appendix below).
|
|
39
|
+
|
|
40
|
+
"Derivative Works" shall mean any work, whether in Source or Object
|
|
41
|
+
form, that is based on (or derived from) the Work and for which the
|
|
42
|
+
editorial revisions, annotations, elaborations, or other modifications
|
|
43
|
+
represent, as a whole, an original work of authorship. For the purposes
|
|
44
|
+
of this License, Derivative Works shall not include works that remain
|
|
45
|
+
separable from, or merely link (or bind by name) to the interfaces of,
|
|
46
|
+
the Work and Derivative Works thereof.
|
|
47
|
+
|
|
48
|
+
"Contribution" shall mean any work of authorship, including
|
|
49
|
+
the original version of the Work and any modifications or additions
|
|
50
|
+
to that Work or Derivative Works thereof, that is intentionally
|
|
51
|
+
submitted to Licensor for inclusion in the Work by the copyright owner
|
|
52
|
+
or by an individual or Legal Entity authorized to submit on behalf of
|
|
53
|
+
the copyright owner. For the purposes of this definition, "submitted"
|
|
54
|
+
means any form of electronic, verbal, or written communication sent
|
|
55
|
+
to the Licensor or its representatives, including but not limited to
|
|
56
|
+
communication on electronic mailing lists, source code control systems,
|
|
57
|
+
and issue tracking systems that are managed by, or on behalf of, the
|
|
58
|
+
Licensor for the purpose of discussing and improving the Work, but
|
|
59
|
+
excluding communication that is conspicuously marked or otherwise
|
|
60
|
+
designated in writing by the copyright owner as "Not a Contribution."
|
|
61
|
+
|
|
62
|
+
"Contributor" shall mean Licensor and any individual or Legal Entity
|
|
63
|
+
on behalf of whom a Contribution has been received by Licensor and
|
|
64
|
+
subsequently incorporated within the Work.
|
|
65
|
+
|
|
66
|
+
2. Grant of Copyright License. Subject to the terms and conditions of
|
|
67
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
68
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
69
|
+
copyright license to reproduce, prepare Derivative Works of,
|
|
70
|
+
publicly display, publicly perform, sublicense, and distribute the
|
|
71
|
+
Work and such Derivative Works in Source or Object form.
|
|
72
|
+
|
|
73
|
+
3. Grant of Patent License. Subject to the terms and conditions of
|
|
74
|
+
this License, each Contributor hereby grants to You a perpetual,
|
|
75
|
+
worldwide, non-exclusive, no-charge, royalty-free, irrevocable
|
|
76
|
+
(except as stated in this section) patent license to make, have made,
|
|
77
|
+
use, offer to sell, sell, import, and otherwise transfer the Work,
|
|
78
|
+
where such license applies only to those patent claims licensable
|
|
79
|
+
by such Contributor that are necessarily infringed by their
|
|
80
|
+
Contribution(s) alone or by combination of their Contribution(s)
|
|
81
|
+
with the Work to which such Contribution(s) was submitted. If You
|
|
82
|
+
institute patent litigation against any entity (including a
|
|
83
|
+
cross-claim or counterclaim in a lawsuit) alleging that the Work
|
|
84
|
+
or a Contribution incorporated within the Work constitutes direct
|
|
85
|
+
or contributory patent infringement, then any patent licenses
|
|
86
|
+
granted to You under this License for that Work shall terminate
|
|
87
|
+
as of the date such litigation is filed.
|
|
88
|
+
|
|
89
|
+
4. Redistribution. You may reproduce and distribute copies of the
|
|
90
|
+
Work or Derivative Works thereof in any medium, with or without
|
|
91
|
+
modifications, and in Source or Object form, provided that You
|
|
92
|
+
meet the following conditions:
|
|
93
|
+
|
|
94
|
+
(a) You must give any other recipients of the Work or
|
|
95
|
+
Derivative Works a copy of this License; and
|
|
96
|
+
|
|
97
|
+
(b) You must cause any modified files to carry prominent notices
|
|
98
|
+
stating that You changed the files; and
|
|
99
|
+
|
|
100
|
+
(c) You must retain, in the Source form of any Derivative Works
|
|
101
|
+
that You distribute, all copyright, patent, trademark, and
|
|
102
|
+
attribution notices from the Source form of the Work,
|
|
103
|
+
excluding those notices that do not pertain to any part of
|
|
104
|
+
the Derivative Works; and
|
|
105
|
+
|
|
106
|
+
(d) If the Work includes a "NOTICE" text file as part of its
|
|
107
|
+
distribution, then any Derivative Works that You distribute must
|
|
108
|
+
include a readable copy of the attribution notices contained
|
|
109
|
+
within such NOTICE file, excluding those notices that do not
|
|
110
|
+
pertain to any part of the Derivative Works, in at least one
|
|
111
|
+
of the following places: within a NOTICE text file distributed
|
|
112
|
+
as part of the Derivative Works; within the Source form or
|
|
113
|
+
documentation, if provided along with the Derivative Works; or,
|
|
114
|
+
within a display generated by the Derivative Works, if and
|
|
115
|
+
wherever such third-party notices normally appear. The contents
|
|
116
|
+
of the NOTICE file are for informational purposes only and
|
|
117
|
+
do not modify the License. You may add Your own attribution
|
|
118
|
+
notices within Derivative Works that You distribute, alongside
|
|
119
|
+
or as an addendum to the NOTICE text from the Work, provided
|
|
120
|
+
that such additional attribution notices cannot be construed
|
|
121
|
+
as modifying the License.
|
|
122
|
+
|
|
123
|
+
You may add Your own copyright statement to Your modifications and
|
|
124
|
+
may provide additional or different license terms and conditions
|
|
125
|
+
for use, reproduction, or distribution of Your modifications, or
|
|
126
|
+
for any such Derivative Works as a whole, provided Your use,
|
|
127
|
+
reproduction, and distribution of the Work otherwise complies with
|
|
128
|
+
the conditions stated in this License.
|
|
129
|
+
|
|
130
|
+
5. Submission of Contributions. Unless You explicitly state otherwise,
|
|
131
|
+
any Contribution intentionally submitted for inclusion in the Work
|
|
132
|
+
by You to the Licensor shall be under the terms and conditions of
|
|
133
|
+
this License, without any additional terms or conditions.
|
|
134
|
+
Notwithstanding the above, nothing herein shall supersede or modify
|
|
135
|
+
the terms of any separate license agreement you may have executed
|
|
136
|
+
with Licensor regarding such Contributions.
|
|
137
|
+
|
|
138
|
+
6. Trademarks. This License does not grant permission to use the trade
|
|
139
|
+
names, trademarks, service marks, or product names of the Licensor,
|
|
140
|
+
except as required for reasonable and customary use in describing the
|
|
141
|
+
origin of the Work and reproducing the content of the NOTICE file.
|
|
142
|
+
|
|
143
|
+
7. Disclaimer of Warranty. Unless required by applicable law or
|
|
144
|
+
agreed to in writing, Licensor provides the Work (and each
|
|
145
|
+
Contributor provides its Contributions) on an "AS IS" BASIS,
|
|
146
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or
|
|
147
|
+
implied, including, without limitation, any warranties or conditions
|
|
148
|
+
of TITLE, NON-INFRINGEMENT, MERCHANTABILITY, or FITNESS FOR A
|
|
149
|
+
PARTICULAR PURPOSE. You are solely responsible for determining the
|
|
150
|
+
appropriateness of using or redistributing the Work and assume any
|
|
151
|
+
risks associated with Your exercise of permissions under this License.
|
|
152
|
+
|
|
153
|
+
8. Limitation of Liability. In no event and under no legal theory,
|
|
154
|
+
whether in tort (including negligence), contract, or otherwise,
|
|
155
|
+
unless required by applicable law (such as deliberate and grossly
|
|
156
|
+
negligent acts) or agreed to in writing, shall any Contributor be
|
|
157
|
+
liable to You for damages, including any direct, indirect, special,
|
|
158
|
+
incidental, or consequential damages of any character arising as a
|
|
159
|
+
result of this License or out of the use or inability to use the
|
|
160
|
+
Work (including but not limited to damages for loss of goodwill,
|
|
161
|
+
work stoppage, computer failure or malfunction, or any and all
|
|
162
|
+
other commercial damages or losses), even if such Contributor
|
|
163
|
+
has been advised of the possibility of such damages.
|
|
164
|
+
|
|
165
|
+
9. Accepting Warranty or Additional Liability. While redistributing
|
|
166
|
+
the Work or Derivative Works thereof, You may choose to offer,
|
|
167
|
+
and charge a fee for, acceptance of support, warranty, indemnity,
|
|
168
|
+
or other liability obligations and/or rights consistent with this
|
|
169
|
+
License. However, in accepting such obligations, You may act only
|
|
170
|
+
on Your own behalf and on Your sole responsibility, not on behalf
|
|
171
|
+
of any other Contributor, and only if You agree to indemnify,
|
|
172
|
+
defend, and hold each Contributor harmless for any liability
|
|
173
|
+
incurred by, or claims asserted against, such Contributor by reason
|
|
174
|
+
of your accepting any such warranty or additional liability.
|
|
175
|
+
|
|
176
|
+
END OF TERMS AND CONDITIONS
|
|
177
|
+
|
|
178
|
+
APPENDIX: How to apply the Apache License to your work.
|
|
179
|
+
|
|
180
|
+
To apply the Apache License to your work, attach the following
|
|
181
|
+
boilerplate notice, with the fields enclosed by brackets "[]"
|
|
182
|
+
replaced with your own identifying information. (Don't include
|
|
183
|
+
the brackets!) The text should be enclosed in the appropriate
|
|
184
|
+
comment syntax for the file format. We also recommend that a
|
|
185
|
+
file or class name and description of purpose be included on the
|
|
186
|
+
same "printed page" as the copyright notice for easier
|
|
187
|
+
identification within third-party archives.
|
|
188
|
+
|
|
189
|
+
Copyright [yyyy] [name of copyright owner]
|
|
190
|
+
|
|
191
|
+
Licensed under the Apache License, Version 2.0 (the "License");
|
|
192
|
+
you may not use this file except in compliance with the License.
|
|
193
|
+
You may obtain a copy of the License at
|
|
194
|
+
|
|
195
|
+
http://www.apache.org/licenses/LICENSE-2.0
|
|
196
|
+
|
|
197
|
+
Unless required by applicable law or agreed to in writing, software
|
|
198
|
+
distributed under the License is distributed on an "AS IS" BASIS,
|
|
199
|
+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
200
|
+
See the License for the specific language governing permissions and
|
|
201
|
+
limitations under the License.
|
package/README.md
ADDED
|
@@ -0,0 +1,168 @@
|
|
|
1
|
+
# @cuylabs/channel-slack
|
|
2
|
+
|
|
3
|
+
SDK-neutral Slack channel primitives for AI agents.
|
|
4
|
+
|
|
5
|
+
This package contains reusable Slack mechanics: activity parsing, message
|
|
6
|
+
formatting, message admission policy, supplemental history, visibility filters,
|
|
7
|
+
Socket Mode runtime guards, OAuth installation storage, setup inspection, and
|
|
8
|
+
Slack API helpers. It intentionally does not create or run an agent.
|
|
9
|
+
|
|
10
|
+
It is not an agent runtime adapter. For the `@cuylabs/agent-core` binding, use
|
|
11
|
+
`@cuylabs/channel-slack-agent-core`. See
|
|
12
|
+
[Channel Slack Boundary](docs/reference/channel-slack-boundary.md) for the
|
|
13
|
+
package boundary.
|
|
14
|
+
|
|
15
|
+
## Install
|
|
16
|
+
|
|
17
|
+
```bash
|
|
18
|
+
npm install @cuylabs/channel-slack
|
|
19
|
+
```
|
|
20
|
+
|
|
21
|
+
Slack SDK packages are optional peers. Install the peers for the components you
|
|
22
|
+
use:
|
|
23
|
+
|
|
24
|
+
```bash
|
|
25
|
+
npm install @slack/bolt @slack/web-api @slack/types express
|
|
26
|
+
```
|
|
27
|
+
|
|
28
|
+
## Import Map
|
|
29
|
+
|
|
30
|
+
Prefer feature-specific imports so applications only couple to the Slack
|
|
31
|
+
surface they need.
|
|
32
|
+
|
|
33
|
+
| Import | Use for |
|
|
34
|
+
| --- | --- |
|
|
35
|
+
| `@cuylabs/channel-slack/core` | SDK-neutral activity parsing, formatting, sessions, turn context, shared types |
|
|
36
|
+
| `@cuylabs/channel-slack/policy` | Message admission, duplicate suppression, mentioned-thread state |
|
|
37
|
+
| `@cuylabs/channel-slack/history` | Slack history reading, prompt shaping, supplemental-history visibility |
|
|
38
|
+
| `@cuylabs/channel-slack/bolt` | Bolt app factories, auth options, Socket Mode runtime helpers, installation stores |
|
|
39
|
+
| `@cuylabs/channel-slack/setup` | Required scopes/events/settings, generated manifests, setup inspection |
|
|
40
|
+
| `@cuylabs/channel-slack/diagnostics` | Slack token and scope checks |
|
|
41
|
+
| `@cuylabs/channel-slack/users` | User profile lookup and mention enrichment |
|
|
42
|
+
| `@cuylabs/channel-slack/targets` | Human-friendly Slack channel/user target parsing and resolution |
|
|
43
|
+
| `@cuylabs/channel-slack/feedback` | Feedback Block Kit and action helpers |
|
|
44
|
+
|
|
45
|
+
The package root re-exports `core` and `policy` as a lightweight convenience.
|
|
46
|
+
Use feature-specific imports for peer-backed helpers such as `bolt`,
|
|
47
|
+
`diagnostics`, `setup`, and `users`. `@cuylabs/channel-slack/shared` remains as
|
|
48
|
+
an alias of `core`.
|
|
49
|
+
|
|
50
|
+
## Core Flow
|
|
51
|
+
|
|
52
|
+
Most Slack agent adapters follow this flow:
|
|
53
|
+
|
|
54
|
+
1. Parse a Slack event into `SlackActivityInfo`.
|
|
55
|
+
2. Resolve whether the message should start or continue an agent turn.
|
|
56
|
+
3. Optionally load model-visible Slack history for the turn.
|
|
57
|
+
4. Run the product-owned agent.
|
|
58
|
+
5. Format the response back to Slack.
|
|
59
|
+
|
|
60
|
+
```typescript
|
|
61
|
+
import {
|
|
62
|
+
markdownToSlackMrkdwn,
|
|
63
|
+
parseSlackMentionActivity,
|
|
64
|
+
} from "@cuylabs/channel-slack/core";
|
|
65
|
+
import { createSlackMessagePolicyResolver } from "@cuylabs/channel-slack/policy";
|
|
66
|
+
|
|
67
|
+
const activity = parseSlackMentionActivity(slackAppMentionEvent);
|
|
68
|
+
const policy = createSlackMessagePolicyResolver({
|
|
69
|
+
messagePolicy: "mentioned-threads",
|
|
70
|
+
threadReplyPolicy: "original-user",
|
|
71
|
+
});
|
|
72
|
+
|
|
73
|
+
const decision = policy.resolve(activity);
|
|
74
|
+
if (!decision.accepted) return;
|
|
75
|
+
|
|
76
|
+
const responseText = markdownToSlackMrkdwn("**Ready**");
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
## Components
|
|
80
|
+
|
|
81
|
+
### Core
|
|
82
|
+
|
|
83
|
+
`core` is transport-neutral. It parses raw Slack message and app mention
|
|
84
|
+
payloads, extracts model-facing text from Slack text, Block Kit, rich text, and
|
|
85
|
+
attachments, formats Markdown to Slack mrkdwn, derives thread-aware session IDs,
|
|
86
|
+
and carries request context.
|
|
87
|
+
|
|
88
|
+
See [Activity](docs/concepts/activity.md).
|
|
89
|
+
|
|
90
|
+
### Policy
|
|
91
|
+
|
|
92
|
+
`policy` decides which Slack messages an adapter should process. It accepts DMs
|
|
93
|
+
and direct mentions by default, can allow passive channel messages by channel or
|
|
94
|
+
install scope, tracks mentioned threads, and suppresses duplicate Slack events.
|
|
95
|
+
|
|
96
|
+
See [Message Policy](docs/concepts/message-policy.md).
|
|
97
|
+
|
|
98
|
+
### History
|
|
99
|
+
|
|
100
|
+
`history` reads Slack thread/channel history, normalizes messages, formats prompt
|
|
101
|
+
sections, reports expected Slack history access failures, and filters
|
|
102
|
+
supplemental context before it reaches a model.
|
|
103
|
+
|
|
104
|
+
See [Supplemental History](docs/concepts/supplemental-history.md).
|
|
105
|
+
|
|
106
|
+
### Bolt
|
|
107
|
+
|
|
108
|
+
`bolt` contains app factories for HTTP and Socket Mode, direct auth resolution,
|
|
109
|
+
OAuth installation-store types, development installation stores, and Socket Mode
|
|
110
|
+
process/runtime guards. It does not register agent handlers.
|
|
111
|
+
|
|
112
|
+
See [Bolt Runtime](docs/concepts/bolt-runtime.md).
|
|
113
|
+
|
|
114
|
+
### Setup
|
|
115
|
+
|
|
116
|
+
`setup` turns feature selections into Slack scopes, bot events, settings,
|
|
117
|
+
environment variables, and app manifests. It can also inspect a token against the
|
|
118
|
+
requirements.
|
|
119
|
+
|
|
120
|
+
See [Setup Requirements](docs/concepts/setup-requirements.md).
|
|
121
|
+
|
|
122
|
+
## Recipes
|
|
123
|
+
|
|
124
|
+
- [App mention handler](docs/recipes/app-mention-handler.md)
|
|
125
|
+
- [Assistant thread handler](docs/recipes/assistant-thread-handler.md)
|
|
126
|
+
- [Socket Mode app](docs/recipes/socket-mode-app.md)
|
|
127
|
+
- [Generate a Slack manifest](docs/recipes/generate-slack-manifest.md)
|
|
128
|
+
- [History visibility filter](docs/recipes/history-visibility.md)
|
|
129
|
+
|
|
130
|
+
## Source Layout
|
|
131
|
+
|
|
132
|
+
```text
|
|
133
|
+
src/
|
|
134
|
+
core.ts public core entrypoint
|
|
135
|
+
shared/ SDK-neutral types, parsing, formatting, turn helpers
|
|
136
|
+
assistant/ Slack Assistant API helpers
|
|
137
|
+
bolt/ Bolt app/auth/runtime helpers
|
|
138
|
+
diagnostics/ token and scope inspection
|
|
139
|
+
feedback/ feedback blocks and action parsing
|
|
140
|
+
history/
|
|
141
|
+
context/ turn-history loading component
|
|
142
|
+
reader.ts Slack history API reader and prompt formatter
|
|
143
|
+
visibility-policy.ts model-visible history filters
|
|
144
|
+
inclusion-policy.ts direct-message supplemental-history inclusion policy
|
|
145
|
+
policy/
|
|
146
|
+
message/ message admission and state-store component
|
|
147
|
+
setup/ scopes, events, manifests, setup inspection
|
|
148
|
+
targets/ channel/user target parsing and resolution
|
|
149
|
+
users/ profile lookup and mention enrichment
|
|
150
|
+
```
|
|
151
|
+
|
|
152
|
+
## Development
|
|
153
|
+
|
|
154
|
+
```bash
|
|
155
|
+
pnpm --filter @cuylabs/channel-slack typecheck
|
|
156
|
+
pnpm --filter @cuylabs/channel-slack test
|
|
157
|
+
pnpm --filter @cuylabs/channel-slack build
|
|
158
|
+
```
|
|
159
|
+
|
|
160
|
+
Run workspace checks before publishing changes:
|
|
161
|
+
|
|
162
|
+
```bash
|
|
163
|
+
pnpm lint
|
|
164
|
+
pnpm typecheck
|
|
165
|
+
pnpm build
|
|
166
|
+
pnpm test
|
|
167
|
+
pnpm format:check
|
|
168
|
+
```
|
|
@@ -0,0 +1,66 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Slack activity domain types — channel/user/activity shape produced by the
|
|
3
|
+
* package's activity parsers. Pure data; no Slack SDK imports.
|
|
4
|
+
*/
|
|
5
|
+
/**
|
|
6
|
+
* Slack user identity extracted from the incoming event.
|
|
7
|
+
*/
|
|
8
|
+
interface SlackUserIdentity {
|
|
9
|
+
/** Slack user ID (e.g. `U01234ABCDE`) */
|
|
10
|
+
userId: string;
|
|
11
|
+
/** Display name when resolvable (from `users.info`) */
|
|
12
|
+
userName?: string;
|
|
13
|
+
/** Slack channel / DM ID the message arrived in */
|
|
14
|
+
channelId: string;
|
|
15
|
+
/** Slack workspace (team) ID */
|
|
16
|
+
teamId?: string;
|
|
17
|
+
/** Thread timestamp — set when the message is inside a thread */
|
|
18
|
+
threadTs?: string;
|
|
19
|
+
/** Timestamp of the originating message */
|
|
20
|
+
messageTs?: string;
|
|
21
|
+
}
|
|
22
|
+
/** High-level classification of the Slack surface a message arrived on. */
|
|
23
|
+
type SlackChannelType = "dm" | "channel" | "group" | "thread" | "unknown";
|
|
24
|
+
/**
|
|
25
|
+
* Parsed Slack activity metadata — analogous to `TeamsActivityInfo` in the
|
|
26
|
+
* Teams channel package.
|
|
27
|
+
*/
|
|
28
|
+
interface SlackActivityInfo {
|
|
29
|
+
/** Slack channel / DM ID */
|
|
30
|
+
channelId: string;
|
|
31
|
+
/** Classified surface type */
|
|
32
|
+
channelType: SlackChannelType;
|
|
33
|
+
/** Slack user ID of the message author */
|
|
34
|
+
userId: string;
|
|
35
|
+
/** Workspace ID */
|
|
36
|
+
teamId?: string;
|
|
37
|
+
/** Parent thread timestamp (set when the message is a reply) */
|
|
38
|
+
threadTs?: string;
|
|
39
|
+
/** Message's own timestamp */
|
|
40
|
+
messageTs?: string;
|
|
41
|
+
/**
|
|
42
|
+
* User ID of the parent thread message, when Slack includes `parent_user_id`
|
|
43
|
+
* on a threaded reply. Useful for application-level thread continuation
|
|
44
|
+
* policy, such as allowing unmentioned replies only from the thread starter.
|
|
45
|
+
*/
|
|
46
|
+
parentUserId?: string;
|
|
47
|
+
/**
|
|
48
|
+
* Slack AI `action_token` from the inbound message event, when present.
|
|
49
|
+
*
|
|
50
|
+
* Bot-token calls to `assistant.search.context` require this token. Treat it
|
|
51
|
+
* as request-bound sensitive state: do not log, persist, trace, or expose it
|
|
52
|
+
* to the model unless a trusted tool is making the Slack API call directly.
|
|
53
|
+
*/
|
|
54
|
+
actionToken?: string;
|
|
55
|
+
/** Plain text of the message (mentions stripped) */
|
|
56
|
+
text: string;
|
|
57
|
+
/** Whether the message was an @mention of the bot */
|
|
58
|
+
isMention: boolean;
|
|
59
|
+
/**
|
|
60
|
+
* Slack Assistant thread metadata when the activity came from an assistant
|
|
61
|
+
* thread and the transport preserved that payload.
|
|
62
|
+
*/
|
|
63
|
+
assistantThread?: Record<string, unknown>;
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
export type { SlackActivityInfo as S, SlackChannelType as a, SlackUserIdentity as b };
|
|
@@ -0,0 +1,58 @@
|
|
|
1
|
+
import { S as SlackActivityInfo } from './activity-ByrD9Ftr.js';
|
|
2
|
+
|
|
3
|
+
/**
|
|
4
|
+
* Per-thread Assistant context store.
|
|
5
|
+
*
|
|
6
|
+
* Bolt's default store persists context in message metadata, but also keeps a
|
|
7
|
+
* single process-local cached context. That cache is not keyed by thread, so a
|
|
8
|
+
* multi-user app can read the wrong channel-of-origin context after another
|
|
9
|
+
* assistant thread changes it. This store keeps the same metadata persistence
|
|
10
|
+
* model while caching by Slack channel/thread.
|
|
11
|
+
*/
|
|
12
|
+
type ThreadStoreArgs = {
|
|
13
|
+
context?: {
|
|
14
|
+
botUserId?: string;
|
|
15
|
+
};
|
|
16
|
+
client?: {
|
|
17
|
+
conversations?: {
|
|
18
|
+
replies?: (args: Record<string, unknown>) => Promise<{
|
|
19
|
+
messages?: unknown[];
|
|
20
|
+
}>;
|
|
21
|
+
};
|
|
22
|
+
chat?: {
|
|
23
|
+
update?: (args: Record<string, unknown>) => Promise<unknown>;
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
logger?: {
|
|
27
|
+
debug?: (...args: unknown[]) => void;
|
|
28
|
+
};
|
|
29
|
+
payload?: unknown;
|
|
30
|
+
};
|
|
31
|
+
interface SlackAssistantThreadContextStoreLike {
|
|
32
|
+
get: (args: ThreadStoreArgs) => Promise<unknown>;
|
|
33
|
+
save: (args: ThreadStoreArgs) => Promise<void>;
|
|
34
|
+
}
|
|
35
|
+
declare function createSlackAssistantThreadContextStore(): SlackAssistantThreadContextStoreLike;
|
|
36
|
+
|
|
37
|
+
/**
|
|
38
|
+
* Parse the `message` payload delivered to a Bolt assistant `userMessage`
|
|
39
|
+
* handler into the same shape as `parseSlackMessageActivity`.
|
|
40
|
+
*
|
|
41
|
+
* Bolt routes a fairly wide message event to assistant handlers, so we keep
|
|
42
|
+
* the input typed loosely and inspect only the fields we need.
|
|
43
|
+
*/
|
|
44
|
+
|
|
45
|
+
interface ParsedAssistantUserMessage extends SlackActivityInfo {
|
|
46
|
+
/** Slack `channel` identifier, always present after parsing. */
|
|
47
|
+
channel: string;
|
|
48
|
+
/** Slack `thread_ts` identifier — the assistant API requires a thread. */
|
|
49
|
+
threadTs: string;
|
|
50
|
+
}
|
|
51
|
+
/**
|
|
52
|
+
* Returns `undefined` when the event is not a parseable user message — bolt
|
|
53
|
+
* delivers join/leave/edit/delete subtypes through the same handler, and the
|
|
54
|
+
* caller should ignore them.
|
|
55
|
+
*/
|
|
56
|
+
declare function parseSlackMessageActivityFromMessageEvent(message: unknown): ParsedAssistantUserMessage | undefined;
|
|
57
|
+
|
|
58
|
+
export { type ParsedAssistantUserMessage, type SlackAssistantThreadContextStoreLike, createSlackAssistantThreadContextStore, parseSlackMessageActivityFromMessageEvent };
|
|
@@ -0,0 +1,188 @@
|
|
|
1
|
+
import {
|
|
2
|
+
extractSlackActionToken
|
|
3
|
+
} from "./chunk-TWJGVDA2.js";
|
|
4
|
+
import {
|
|
5
|
+
resolveSlackChannelType,
|
|
6
|
+
stripLeadingMentions
|
|
7
|
+
} from "./chunk-FPCE5V5Y.js";
|
|
8
|
+
|
|
9
|
+
// src/assistant/thread-context-store.ts
|
|
10
|
+
var ASSISTANT_THREAD_CONTEXT_EVENT_TYPE = "assistant_thread_context";
|
|
11
|
+
function createSlackAssistantThreadContextStore() {
|
|
12
|
+
const cache = /* @__PURE__ */ new Map();
|
|
13
|
+
return {
|
|
14
|
+
async get(args) {
|
|
15
|
+
args.logger?.debug?.("[channel-slack] thread context store get called");
|
|
16
|
+
const { channelId, threadTs } = extractThreadInfo(args.payload);
|
|
17
|
+
const cacheKey = contextCacheKey(channelId, threadTs);
|
|
18
|
+
const cached = cache.get(cacheKey);
|
|
19
|
+
if (cached) return cached;
|
|
20
|
+
const messages = await readThreadMessages(args, channelId, threadTs);
|
|
21
|
+
const botUserId = args.context?.botUserId;
|
|
22
|
+
const metadataMessage = findContextMessage(messages, botUserId);
|
|
23
|
+
const context = toThreadContext(metadataMessage?.metadata?.event_payload);
|
|
24
|
+
cache.set(cacheKey, context);
|
|
25
|
+
return context;
|
|
26
|
+
},
|
|
27
|
+
async save(args) {
|
|
28
|
+
args.logger?.debug?.("[channel-slack] thread context store save called");
|
|
29
|
+
const { channelId, threadTs, context } = extractThreadInfo(args.payload);
|
|
30
|
+
const cacheKey = contextCacheKey(channelId, threadTs);
|
|
31
|
+
cache.set(cacheKey, context);
|
|
32
|
+
const messages = await readThreadMessages(args, channelId, threadTs);
|
|
33
|
+
const initialMessage = findInitialBotMessage(
|
|
34
|
+
messages,
|
|
35
|
+
args.context?.botUserId
|
|
36
|
+
);
|
|
37
|
+
if (!initialMessage?.ts) return;
|
|
38
|
+
await args.client?.chat?.update?.({
|
|
39
|
+
channel: channelId,
|
|
40
|
+
ts: initialMessage.ts,
|
|
41
|
+
text: initialMessage.text ?? "",
|
|
42
|
+
blocks: Array.isArray(initialMessage.blocks) ? initialMessage.blocks : [],
|
|
43
|
+
metadata: {
|
|
44
|
+
event_type: ASSISTANT_THREAD_CONTEXT_EVENT_TYPE,
|
|
45
|
+
event_payload: context
|
|
46
|
+
}
|
|
47
|
+
});
|
|
48
|
+
}
|
|
49
|
+
};
|
|
50
|
+
}
|
|
51
|
+
async function readThreadMessages(args, channelId, threadTs) {
|
|
52
|
+
const replies = args.client?.conversations?.replies;
|
|
53
|
+
if (typeof replies !== "function") return [];
|
|
54
|
+
const result = await replies({
|
|
55
|
+
channel: channelId,
|
|
56
|
+
ts: threadTs,
|
|
57
|
+
oldest: threadTs,
|
|
58
|
+
include_all_metadata: true,
|
|
59
|
+
limit: 4
|
|
60
|
+
});
|
|
61
|
+
return Array.isArray(result.messages) ? result.messages.filter(isSlackMessageWithMetadata) : [];
|
|
62
|
+
}
|
|
63
|
+
function findContextMessage(messages, botUserId) {
|
|
64
|
+
return messages.find(
|
|
65
|
+
(message) => isBotAuthoredMessage(message, botUserId) && message.metadata?.event_type === ASSISTANT_THREAD_CONTEXT_EVENT_TYPE
|
|
66
|
+
) ?? messages.find(
|
|
67
|
+
(message) => isBotAuthoredMessage(message, botUserId) && message.metadata?.event_payload
|
|
68
|
+
);
|
|
69
|
+
}
|
|
70
|
+
function findInitialBotMessage(messages, botUserId) {
|
|
71
|
+
return messages.find((message) => isBotAuthoredMessage(message, botUserId));
|
|
72
|
+
}
|
|
73
|
+
function isBotAuthoredMessage(message, botUserId) {
|
|
74
|
+
if (message.subtype) return false;
|
|
75
|
+
return botUserId ? message.user === botUserId : Boolean(message.user);
|
|
76
|
+
}
|
|
77
|
+
function extractThreadInfo(payload) {
|
|
78
|
+
const channelId = extractChannelId(payload);
|
|
79
|
+
const threadTs = extractThreadTs(payload);
|
|
80
|
+
if (!channelId || !threadTs) {
|
|
81
|
+
const missing = [];
|
|
82
|
+
if (!channelId) missing.push("channel_id");
|
|
83
|
+
if (!threadTs) missing.push("thread_ts");
|
|
84
|
+
throw new Error(
|
|
85
|
+
`Assistant payload is missing required properties: ${missing.join(", ")}`
|
|
86
|
+
);
|
|
87
|
+
}
|
|
88
|
+
return {
|
|
89
|
+
channelId,
|
|
90
|
+
threadTs,
|
|
91
|
+
context: extractThreadContext(payload)
|
|
92
|
+
};
|
|
93
|
+
}
|
|
94
|
+
function extractThreadContext(payload) {
|
|
95
|
+
if (!isRecord(payload)) return {};
|
|
96
|
+
const assistantThread = payload.assistant_thread;
|
|
97
|
+
if (!isRecord(assistantThread) || !isRecord(assistantThread.context)) {
|
|
98
|
+
return {};
|
|
99
|
+
}
|
|
100
|
+
return toThreadContext(assistantThread.context);
|
|
101
|
+
}
|
|
102
|
+
function extractThreadTs(payload) {
|
|
103
|
+
if (!isRecord(payload)) return void 0;
|
|
104
|
+
if (isNonEmptyString(payload.thread_ts)) return payload.thread_ts;
|
|
105
|
+
if (isRecord(payload.assistant_thread) && isNonEmptyString(payload.assistant_thread.thread_ts)) {
|
|
106
|
+
return payload.assistant_thread.thread_ts;
|
|
107
|
+
}
|
|
108
|
+
if (isRecord(payload.message) && isNonEmptyString(payload.message.thread_ts)) {
|
|
109
|
+
return payload.message.thread_ts;
|
|
110
|
+
}
|
|
111
|
+
if (isRecord(payload.previous_message) && isNonEmptyString(payload.previous_message.thread_ts)) {
|
|
112
|
+
return payload.previous_message.thread_ts;
|
|
113
|
+
}
|
|
114
|
+
return void 0;
|
|
115
|
+
}
|
|
116
|
+
function extractChannelId(payload) {
|
|
117
|
+
if (!isRecord(payload)) return void 0;
|
|
118
|
+
if (isNonEmptyString(payload.channel)) return payload.channel;
|
|
119
|
+
if (isRecord(payload.channel) && isNonEmptyString(payload.channel.id)) {
|
|
120
|
+
return payload.channel.id;
|
|
121
|
+
}
|
|
122
|
+
if (isNonEmptyString(payload.channel_id)) return payload.channel_id;
|
|
123
|
+
if (isRecord(payload.item) && isNonEmptyString(payload.item.channel)) {
|
|
124
|
+
return payload.item.channel;
|
|
125
|
+
}
|
|
126
|
+
if (isRecord(payload.assistant_thread) && isNonEmptyString(payload.assistant_thread.channel_id)) {
|
|
127
|
+
return payload.assistant_thread.channel_id;
|
|
128
|
+
}
|
|
129
|
+
return void 0;
|
|
130
|
+
}
|
|
131
|
+
function toThreadContext(value) {
|
|
132
|
+
if (!isRecord(value)) return {};
|
|
133
|
+
return {
|
|
134
|
+
...isNonEmptyString(value.channel_id) ? { channel_id: value.channel_id } : {},
|
|
135
|
+
...isNonEmptyString(value.team_id) ? { team_id: value.team_id } : {},
|
|
136
|
+
..."enterprise_id" in value ? {
|
|
137
|
+
enterprise_id: typeof value.enterprise_id === "string" ? value.enterprise_id : null
|
|
138
|
+
} : {}
|
|
139
|
+
};
|
|
140
|
+
}
|
|
141
|
+
function isSlackMessageWithMetadata(value) {
|
|
142
|
+
return isRecord(value);
|
|
143
|
+
}
|
|
144
|
+
function contextCacheKey(channelId, threadTs) {
|
|
145
|
+
return `${channelId}:${threadTs}`;
|
|
146
|
+
}
|
|
147
|
+
function isNonEmptyString(value) {
|
|
148
|
+
return typeof value === "string" && value.length > 0;
|
|
149
|
+
}
|
|
150
|
+
function isRecord(value) {
|
|
151
|
+
return typeof value === "object" && value !== null;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
// src/assistant/message-parser.ts
|
|
155
|
+
function parseSlackMessageActivityFromMessageEvent(message) {
|
|
156
|
+
if (!message || typeof message !== "object") return void 0;
|
|
157
|
+
const m = message;
|
|
158
|
+
if (m.subtype && m.subtype.length > 0) return void 0;
|
|
159
|
+
if (typeof m.text !== "string" || m.text.length === 0) return void 0;
|
|
160
|
+
if (typeof m.thread_ts !== "string" || m.thread_ts.length === 0) {
|
|
161
|
+
return void 0;
|
|
162
|
+
}
|
|
163
|
+
const channel = typeof m.channel === "string" ? m.channel : void 0;
|
|
164
|
+
if (!channel) return void 0;
|
|
165
|
+
const threadTs = m.thread_ts;
|
|
166
|
+
const messageTs = m.ts;
|
|
167
|
+
const isThread = threadTs !== messageTs;
|
|
168
|
+
const channelType = resolveSlackChannelType(channel, threadTs, isThread);
|
|
169
|
+
const rawText = m.text;
|
|
170
|
+
const text = channelType === "dm" ? rawText.trim() : stripLeadingMentions(rawText);
|
|
171
|
+
return {
|
|
172
|
+
channel,
|
|
173
|
+
threadTs,
|
|
174
|
+
channelId: channel,
|
|
175
|
+
channelType,
|
|
176
|
+
userId: m.user ?? "unknown",
|
|
177
|
+
teamId: m.team ?? void 0,
|
|
178
|
+
parentUserId: m.parent_user_id,
|
|
179
|
+
actionToken: extractSlackActionToken(m),
|
|
180
|
+
text,
|
|
181
|
+
isMention: false,
|
|
182
|
+
...messageTs ? { messageTs } : {}
|
|
183
|
+
};
|
|
184
|
+
}
|
|
185
|
+
export {
|
|
186
|
+
createSlackAssistantThreadContextStore,
|
|
187
|
+
parseSlackMessageActivityFromMessageEvent
|
|
188
|
+
};
|