@effect/chat-slack 0.0.0 → 4.1.5
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/AGENTS.md +434 -0
- package/CLAUDE.md +434 -0
- package/LICENSE +21 -0
- package/README.md +47 -0
- package/ai-docs/README.md +44 -0
- package/ai-docs/package.json +36 -0
- package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
- package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
- package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
- package/ai-docs/src/01_effect/01_basics/index.md +7 -0
- package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
- package/ai-docs/src/01_effect/02_schema/index.md +7 -0
- package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
- package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
- package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
- package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
- package/ai-docs/src/01_effect/03_services/index.md +5 -0
- package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
- package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
- package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
- package/ai-docs/src/01_effect/04_errors/index.md +1 -0
- package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
- package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
- package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
- package/ai-docs/src/01_effect/05_resources/index.md +3 -0
- package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
- package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
- package/ai-docs/src/01_effect/06_running/index.md +1 -0
- package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
- package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
- package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
- package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
- package/ai-docs/src/03_stream/30_encoding.ts +163 -0
- package/ai-docs/src/03_stream/index.md +4 -0
- package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
- package/ai-docs/src/04_integration/index.md +5 -0
- package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
- package/ai-docs/src/05_batching/index.md +3 -0
- package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
- package/ai-docs/src/06_schedule/index.md +3 -0
- package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
- package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
- package/ai-docs/src/07_datetime/index.md +5 -0
- package/ai-docs/src/08_observability/10_logging.ts +66 -0
- package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
- package/ai-docs/src/08_observability/index.md +7 -0
- package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
- package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
- package/ai-docs/src/09_testing/index.md +1 -0
- package/ai-docs/src/10_predicate/01_basics.ts +14 -0
- package/ai-docs/src/10_predicate/index.md +9 -0
- package/ai-docs/src/40_sql/10_basics.ts +175 -0
- package/ai-docs/src/40_sql/index.md +6 -0
- package/ai-docs/src/50_http-client/10_basics.ts +102 -0
- package/ai-docs/src/50_http-client/index.md +3 -0
- package/ai-docs/src/51_http-server/10_basics.ts +118 -0
- package/ai-docs/src/51_http-server/20_testing.ts +100 -0
- package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
- package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
- package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
- package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
- package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
- package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
- package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
- package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
- package/ai-docs/src/51_http-server/index.md +3 -0
- package/ai-docs/src/52_graphql/10_basics.ts +117 -0
- package/ai-docs/src/52_graphql/20_testing.ts +45 -0
- package/ai-docs/src/52_graphql/30_batching.ts +118 -0
- package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
- package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
- package/ai-docs/src/52_graphql/index.md +6 -0
- package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
- package/ai-docs/src/60_child-process/index.md +3 -0
- package/ai-docs/src/70_cli/10_basics.ts +162 -0
- package/ai-docs/src/70_cli/index.md +5 -0
- package/ai-docs/src/71_ai/10_language-model.ts +156 -0
- package/ai-docs/src/71_ai/20_tools.ts +226 -0
- package/ai-docs/src/71_ai/30_chat.ts +158 -0
- package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
- package/ai-docs/src/71_ai/index.md +5 -0
- package/ai-docs/src/80_cluster/10_entities.ts +97 -0
- package/ai-docs/src/80_cluster/index.md +4 -0
- package/ai-docs/src/index.md +10 -0
- package/ai-docs/tsconfig.json +28 -0
- package/dist/SlackChat.d.ts +500 -0
- package/dist/SlackChat.d.ts.map +1 -0
- package/dist/SlackChat.js +512 -0
- package/dist/SlackChat.js.map +1 -0
- package/dist/SlackWebApi.d.ts +74 -0
- package/dist/SlackWebApi.d.ts.map +1 -0
- package/dist/SlackWebApi.js +96 -0
- package/dist/SlackWebApi.js.map +1 -0
- package/dist/index.d.ts +15 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +16 -0
- package/dist/index.js.map +1 -0
- package/dist/internal/actions.d.ts +2 -0
- package/dist/internal/actions.d.ts.map +1 -0
- package/dist/internal/actions.js +169 -0
- package/dist/internal/actions.js.map +1 -0
- package/dist/internal/adapter.d.ts +2 -0
- package/dist/internal/adapter.d.ts.map +1 -0
- package/dist/internal/adapter.js +836 -0
- package/dist/internal/adapter.js.map +1 -0
- package/dist/internal/address.d.ts +2 -0
- package/dist/internal/address.d.ts.map +1 -0
- package/dist/internal/address.js +36 -0
- package/dist/internal/address.js.map +1 -0
- package/dist/internal/emoji.d.ts +2 -0
- package/dist/internal/emoji.d.ts.map +1 -0
- package/dist/internal/emoji.js +207 -0
- package/dist/internal/emoji.js.map +1 -0
- package/dist/internal/emojiData.d.ts +54 -0
- package/dist/internal/emojiData.d.ts.map +1 -0
- package/dist/internal/emojiData.js +1965 -0
- package/dist/internal/emojiData.js.map +1 -0
- package/dist/internal/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +190 -0
- package/dist/internal/errors.js.map +1 -0
- package/dist/internal/escape.d.ts +14 -0
- package/dist/internal/escape.d.ts.map +1 -0
- package/dist/internal/escape.js +413 -0
- package/dist/internal/escape.js.map +1 -0
- package/dist/internal/interactions.d.ts +2 -0
- package/dist/internal/interactions.d.ts.map +1 -0
- package/dist/internal/interactions.js +389 -0
- package/dist/internal/interactions.js.map +1 -0
- package/dist/internal/json.d.ts +2 -0
- package/dist/internal/json.d.ts.map +1 -0
- package/dist/internal/json.js +30 -0
- package/dist/internal/json.js.map +1 -0
- package/dist/internal/length.d.ts +2 -0
- package/dist/internal/length.d.ts.map +1 -0
- package/dist/internal/length.js +33 -0
- package/dist/internal/length.js.map +1 -0
- package/dist/internal/mentions.d.ts +2 -0
- package/dist/internal/mentions.d.ts.map +1 -0
- package/dist/internal/mentions.js +229 -0
- package/dist/internal/mentions.js.map +1 -0
- package/dist/internal/normalize.d.ts +2 -0
- package/dist/internal/normalize.d.ts.map +1 -0
- package/dist/internal/normalize.js +211 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/options.d.ts +2 -0
- package/dist/internal/options.d.ts.map +1 -0
- package/dist/internal/options.js +6 -0
- package/dist/internal/options.js.map +1 -0
- package/dist/internal/richText.d.ts +2 -0
- package/dist/internal/richText.d.ts.map +1 -0
- package/dist/internal/richText.js +346 -0
- package/dist/internal/richText.js.map +1 -0
- package/dist/internal/text.d.ts +2 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +239 -0
- package/dist/internal/text.js.map +1 -0
- package/dist/internal/webhook.d.ts +2 -0
- package/dist/internal/webhook.d.ts.map +1 -0
- package/dist/internal/webhook.js +142 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +62 -1
- package/src/SlackChat.ts +640 -0
- package/src/SlackWebApi.ts +135 -0
- package/src/index.ts +18 -0
- package/src/internal/actions.ts +203 -0
- package/src/internal/adapter.ts +961 -0
- package/src/internal/address.ts +50 -0
- package/src/internal/emoji.ts +223 -0
- package/src/internal/emojiData.ts +1965 -0
- package/src/internal/errors.ts +192 -0
- package/src/internal/escape.ts +428 -0
- package/src/internal/interactions.ts +391 -0
- package/src/internal/json.ts +35 -0
- package/src/internal/length.ts +41 -0
- package/src/internal/mentions.ts +224 -0
- package/src/internal/normalize.ts +221 -0
- package/src/internal/options.ts +22 -0
- package/src/internal/richText.ts +367 -0
- package/src/internal/text.ts +252 -0
- package/src/internal/webhook.ts +199 -0
|
@@ -0,0 +1,221 @@
|
|
|
1
|
+
import type * as ChatEvent from "effect/chat/ChatEvent"
|
|
2
|
+
import type * as ChatMessage from "effect/chat/ChatMessage"
|
|
3
|
+
import type * as ChatThread from "effect/chat/ChatThread"
|
|
4
|
+
import * as DateTime from "effect/DateTime"
|
|
5
|
+
import type * as Schema from "effect/Schema"
|
|
6
|
+
import { isDirectChannel, thread } from "./address.ts"
|
|
7
|
+
import * as Emoji from "./emoji.ts"
|
|
8
|
+
import { isObject, type JsonObject, object, string } from "./json.ts"
|
|
9
|
+
import * as Mentions from "./mentions.ts"
|
|
10
|
+
import * as RichText from "./richText.ts"
|
|
11
|
+
|
|
12
|
+
/**
|
|
13
|
+
* The message subtypes that count as a message, for inbound events and for
|
|
14
|
+
* every read path alike. Anything else is rejected, so a subtype Slack adds
|
|
15
|
+
* later stays out until someone decides about it.
|
|
16
|
+
*
|
|
17
|
+
* @internal
|
|
18
|
+
*/
|
|
19
|
+
export const messageSubtypes: ReadonlySet<string> = new Set([
|
|
20
|
+
"file_share",
|
|
21
|
+
"thread_broadcast",
|
|
22
|
+
"bot_message",
|
|
23
|
+
"me_message"
|
|
24
|
+
])
|
|
25
|
+
|
|
26
|
+
const tsPattern = /^\d+\.\d+$/
|
|
27
|
+
|
|
28
|
+
/** When a Slack `ts` was sent. @internal */
|
|
29
|
+
export const timestamp = (ts: string): DateTime.Utc => DateTime.makeUnsafe(Math.floor(Number(ts) * 1000))
|
|
30
|
+
|
|
31
|
+
/** @internal */
|
|
32
|
+
export interface MessageContext {
|
|
33
|
+
readonly team: string
|
|
34
|
+
readonly channel: string
|
|
35
|
+
readonly botUserId: string
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
const authorName = (message: JsonObject): string | undefined => {
|
|
39
|
+
const profile = object(message, "user_profile")
|
|
40
|
+
return string(profile, "display_name") ?? string(profile, "real_name") ?? string(profile, "name") ??
|
|
41
|
+
string(object(message, "bot_profile"), "name") ?? string(message, "username")
|
|
42
|
+
}
|
|
43
|
+
|
|
44
|
+
const attachments = (message: JsonObject): Array<ChatMessage.Attachment> => {
|
|
45
|
+
const files = message.files
|
|
46
|
+
if (!Array.isArray(files)) return []
|
|
47
|
+
const out: Array<ChatMessage.Attachment> = []
|
|
48
|
+
for (const file of files) {
|
|
49
|
+
if (!isObject(file)) continue
|
|
50
|
+
const id = string(file, "id")
|
|
51
|
+
if (id === undefined) continue
|
|
52
|
+
const name = string(file, "name")
|
|
53
|
+
const contentType = string(file, "mimetype")
|
|
54
|
+
const size = file.size
|
|
55
|
+
out.push({
|
|
56
|
+
id,
|
|
57
|
+
...(name === undefined ? {} : { name }),
|
|
58
|
+
...(contentType === undefined ? {} : { contentType }),
|
|
59
|
+
...(typeof size === "number" && Number.isSafeInteger(size) && size >= 0 ? { size } : {})
|
|
60
|
+
})
|
|
61
|
+
}
|
|
62
|
+
return out
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/**
|
|
66
|
+
* A Slack message payload as a `ChatMessage`, from an event or any read
|
|
67
|
+
* path. `undefined` when it is not a message: a subtype off the list, or no
|
|
68
|
+
* author.
|
|
69
|
+
*
|
|
70
|
+
* - The address is the channel when `thread_ts` is missing or equal to
|
|
71
|
+
* `ts`, and the thread otherwise. It never depends on later replies.
|
|
72
|
+
* - A `bot_message` has `bot_id` and no `user`: `author.id` is the
|
|
73
|
+
* `bot_id`, with `isBot` true.
|
|
74
|
+
* - `text` is Slack's field as stored, except for a message the bot itself
|
|
75
|
+
* wrote that has a `rich_text` block. Its `text` is rendered from the
|
|
76
|
+
* blocks as Markdown, and its mentions are the mention elements outside
|
|
77
|
+
* code, at the offsets the renderer wrote. The adapter's own `text` is
|
|
78
|
+
* often cut, and the blocks hold the whole message. Slack's field stays on
|
|
79
|
+
* `raw`.
|
|
80
|
+
*
|
|
81
|
+
* @internal
|
|
82
|
+
*/
|
|
83
|
+
export const message = (raw: unknown, context: MessageContext): ChatMessage.ChatMessage | undefined => {
|
|
84
|
+
if (!isObject(raw)) return undefined
|
|
85
|
+
const subtype = string(raw, "subtype")
|
|
86
|
+
if (subtype !== undefined && !messageSubtypes.has(subtype)) return undefined
|
|
87
|
+
const ts = string(raw, "ts")
|
|
88
|
+
if (ts === undefined || !tsPattern.test(ts)) return undefined
|
|
89
|
+
const user = string(raw, "user")
|
|
90
|
+
const botId = string(raw, "bot_id")
|
|
91
|
+
const authorId = user ?? botId
|
|
92
|
+
if (authorId === undefined) return undefined
|
|
93
|
+
const threadTs = string(raw, "thread_ts")
|
|
94
|
+
const isSelf = user !== undefined && user === context.botUserId
|
|
95
|
+
const rendered = isSelf && Array.isArray(raw.blocks) && RichText.hasRichText(raw.blocks)
|
|
96
|
+
? RichText.render(raw.blocks, context.botUserId)
|
|
97
|
+
: undefined
|
|
98
|
+
const text = rendered?.text ?? (typeof raw.text === "string" ? raw.text : "")
|
|
99
|
+
const name = authorName(raw)
|
|
100
|
+
return {
|
|
101
|
+
id: ts,
|
|
102
|
+
thread: thread({
|
|
103
|
+
team: context.team,
|
|
104
|
+
channel: context.channel,
|
|
105
|
+
threadTs: threadTs === undefined || threadTs === ts ? undefined : threadTs
|
|
106
|
+
}),
|
|
107
|
+
author: {
|
|
108
|
+
id: authorId,
|
|
109
|
+
...(name === undefined ? {} : { name }),
|
|
110
|
+
isBot: botId !== undefined || subtype === "bot_message" || user === context.botUserId,
|
|
111
|
+
isSelf
|
|
112
|
+
},
|
|
113
|
+
text,
|
|
114
|
+
mentions: rendered === undefined ? Mentions.extract(text, raw.blocks, context.botUserId) : [...rendered.mentions],
|
|
115
|
+
attachments: attachments(raw),
|
|
116
|
+
timestamp: timestamp(ts),
|
|
117
|
+
raw
|
|
118
|
+
}
|
|
119
|
+
}
|
|
120
|
+
|
|
121
|
+
const kindOf = (address: ChatThread.ChatThread, channel: string, channelType: string | undefined) => {
|
|
122
|
+
if (address.id.split("/").length === 3) return "thread" as const
|
|
123
|
+
return channelType === "im" || channelType === "mpim" || isDirectChannel(channel)
|
|
124
|
+
? "direct" as const
|
|
125
|
+
: "channel" as const
|
|
126
|
+
}
|
|
127
|
+
|
|
128
|
+
const forWorkspace = (envelope: JsonObject, teamId: string): boolean => {
|
|
129
|
+
if (envelope.team_id === teamId) return true
|
|
130
|
+
const authorizations = envelope.authorizations
|
|
131
|
+
return Array.isArray(authorizations) &&
|
|
132
|
+
authorizations.some((authorization) => isObject(authorization) && authorization.team_id === teamId)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
/** The envelope as `raw`, without the deprecated verification `token`. */
|
|
136
|
+
const envelopeRaw = (envelope: JsonObject): Schema.Json => {
|
|
137
|
+
if (!("token" in envelope)) return envelope
|
|
138
|
+
const { token: _, ...rest } = envelope
|
|
139
|
+
return rest
|
|
140
|
+
}
|
|
141
|
+
|
|
142
|
+
/** An emoji name as Slack writes it, as Unicode, or `custom:{name}`. @internal */
|
|
143
|
+
export const emoji = (name: string): string => Emoji.fromName(name) ?? `custom:${name}`
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* The events in an `event_callback` envelope.
|
|
147
|
+
*
|
|
148
|
+
* @internal
|
|
149
|
+
*/
|
|
150
|
+
export const normalize = (
|
|
151
|
+
envelope: unknown,
|
|
152
|
+
context: { readonly teamId: string; readonly botUserId: string }
|
|
153
|
+
): ReadonlyArray<ChatEvent.ChatEvent> => {
|
|
154
|
+
if (!isObject(envelope) || envelope.type !== "event_callback") return []
|
|
155
|
+
if (!forWorkspace(envelope, context.teamId)) return []
|
|
156
|
+
const event = envelope.event
|
|
157
|
+
if (!isObject(event)) return []
|
|
158
|
+
const raw = envelopeRaw(envelope)
|
|
159
|
+
const team = context.teamId
|
|
160
|
+
switch (event.type) {
|
|
161
|
+
case "message":
|
|
162
|
+
case "app_mention": {
|
|
163
|
+
if (event.type === "app_mention" && event.edited !== undefined) return []
|
|
164
|
+
const channel = string(event, "channel")
|
|
165
|
+
if (channel === undefined) return []
|
|
166
|
+
const normalized = message(event, { team, channel, botUserId: context.botUserId })
|
|
167
|
+
if (normalized === undefined) return []
|
|
168
|
+
return [{
|
|
169
|
+
_tag: "MessageCreated",
|
|
170
|
+
thread: normalized.thread,
|
|
171
|
+
kind: kindOf(normalized.thread, channel, string(event, "channel_type")),
|
|
172
|
+
message: normalized,
|
|
173
|
+
raw
|
|
174
|
+
}]
|
|
175
|
+
}
|
|
176
|
+
case "reaction_added":
|
|
177
|
+
case "reaction_removed": {
|
|
178
|
+
const item = event.item
|
|
179
|
+
if (!isObject(item) || item.type !== "message") return []
|
|
180
|
+
const channel = string(item, "channel")
|
|
181
|
+
const messageId = string(item, "ts")
|
|
182
|
+
const userId = string(event, "user")
|
|
183
|
+
const name = string(event, "reaction")
|
|
184
|
+
if (channel === undefined || messageId === undefined || userId === undefined || name === undefined) return []
|
|
185
|
+
const messageAuthorId = string(event, "item_user")
|
|
186
|
+
const eventTs = string(event, "event_ts")
|
|
187
|
+
return [{
|
|
188
|
+
_tag: event.type === "reaction_added" ? "ReactionAdded" : "ReactionRemoved",
|
|
189
|
+
thread: thread({ team, channel }),
|
|
190
|
+
messageId,
|
|
191
|
+
...(messageAuthorId === undefined ? {} : { messageAuthorId }),
|
|
192
|
+
userId,
|
|
193
|
+
emoji: emoji(name),
|
|
194
|
+
...(eventTs === undefined || !tsPattern.test(eventTs) ? {} : { timestamp: timestamp(eventTs) }),
|
|
195
|
+
raw
|
|
196
|
+
}]
|
|
197
|
+
}
|
|
198
|
+
case "agent_session_stopped": {
|
|
199
|
+
const channel = string(event, "channel")
|
|
200
|
+
const threadTs = string(event, "thread_ts")
|
|
201
|
+
const userId = string(event, "user")
|
|
202
|
+
const id = string(event, "event_ts")
|
|
203
|
+
if (channel === undefined || threadTs === undefined || userId === undefined || id === undefined) return []
|
|
204
|
+
const streams = event.streaming_message_ts
|
|
205
|
+
return [{
|
|
206
|
+
_tag: "StopRequested",
|
|
207
|
+
thread: thread({ team, channel, threadTs }),
|
|
208
|
+
id,
|
|
209
|
+
userId,
|
|
210
|
+
messageIds: Array.isArray(streams)
|
|
211
|
+
? streams.filter((ts): ts is string => typeof ts === "string" && ts.length > 0)
|
|
212
|
+
: typeof streams === "string" && streams.length > 0
|
|
213
|
+
? [streams]
|
|
214
|
+
: [],
|
|
215
|
+
raw
|
|
216
|
+
}]
|
|
217
|
+
}
|
|
218
|
+
default:
|
|
219
|
+
return []
|
|
220
|
+
}
|
|
221
|
+
}
|
|
@@ -0,0 +1,22 @@
|
|
|
1
|
+
import * as Context from "effect/Context"
|
|
2
|
+
|
|
3
|
+
/** @internal */
|
|
4
|
+
export type SessionStatus = "active" | "processing" | "suspended" | "closed"
|
|
5
|
+
|
|
6
|
+
/** @internal */
|
|
7
|
+
export interface PostOptions {
|
|
8
|
+
readonly notify?: {
|
|
9
|
+
readonly users?: ReadonlyArray<string> | undefined
|
|
10
|
+
readonly groups?: ReadonlyArray<string> | undefined
|
|
11
|
+
readonly broadcast?: boolean | undefined
|
|
12
|
+
} | undefined
|
|
13
|
+
readonly unfurl?: boolean | undefined
|
|
14
|
+
readonly nativeStream?: boolean | undefined
|
|
15
|
+
readonly recipientTeamId?: string | undefined
|
|
16
|
+
readonly sessionStatusAtEnd?: SessionStatus | undefined
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
/** @internal */
|
|
20
|
+
export const CurrentPostOptions = Context.Reference<PostOptions>("@effect/chat-slack/CurrentPostOptions", {
|
|
21
|
+
defaultValue: () => ({})
|
|
22
|
+
})
|
|
@@ -0,0 +1,367 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Renders a message's `rich_text` blocks as Markdown, for the `text` of the
|
|
3
|
+
* bot's own messages. Slack stores the adapter's `markdown` block as
|
|
4
|
+
* `rich_text`, and the stored `text` is the adapter's `text`, which may be
|
|
5
|
+
* cut. Rendering the blocks gives the whole message back, with its newlines.
|
|
6
|
+
*
|
|
7
|
+
* The renderer is total: an element it does not know gives its `text`, or
|
|
8
|
+
* nothing.
|
|
9
|
+
*/
|
|
10
|
+
import type { Mention } from "effect/chat/ChatMessage"
|
|
11
|
+
import { isObject } from "effect/Predicate"
|
|
12
|
+
import * as Emoji from "./emoji.ts"
|
|
13
|
+
|
|
14
|
+
type JsonObject = { readonly [key: string]: unknown }
|
|
15
|
+
|
|
16
|
+
const stringOf = (object: JsonObject, key: string): string | undefined => {
|
|
17
|
+
const value = object[key]
|
|
18
|
+
return typeof value === "string" ? value : undefined
|
|
19
|
+
}
|
|
20
|
+
|
|
21
|
+
const elementsOf = (object: JsonObject): ReadonlyArray<unknown> => Array.isArray(object.elements) ? object.elements : []
|
|
22
|
+
|
|
23
|
+
/** @internal */
|
|
24
|
+
export const hasRichText = (blocks: unknown): boolean =>
|
|
25
|
+
Array.isArray(blocks) && blocks.some((block) => isObject(block) && block.type === "rich_text")
|
|
26
|
+
|
|
27
|
+
interface Style {
|
|
28
|
+
readonly bold: boolean
|
|
29
|
+
readonly italic: boolean
|
|
30
|
+
readonly strike: boolean
|
|
31
|
+
readonly code: boolean
|
|
32
|
+
}
|
|
33
|
+
|
|
34
|
+
const plain: Style = { bold: false, italic: false, strike: false, code: false }
|
|
35
|
+
|
|
36
|
+
const styleOf = (element: JsonObject): Style => {
|
|
37
|
+
const style = element.style
|
|
38
|
+
if (!isObject(style)) return plain
|
|
39
|
+
return {
|
|
40
|
+
bold: style.bold === true,
|
|
41
|
+
italic: style.italic === true,
|
|
42
|
+
strike: style.strike === true,
|
|
43
|
+
code: style.code === true
|
|
44
|
+
}
|
|
45
|
+
}
|
|
46
|
+
|
|
47
|
+
const sameStyle = (a: Style, b: Style) =>
|
|
48
|
+
a.bold === b.bold && a.italic === b.italic && a.strike === b.strike && a.code === b.code
|
|
49
|
+
|
|
50
|
+
const opening = (style: Style): string =>
|
|
51
|
+
(style.bold ? "**" : "") + (style.italic ? "*" : "") + (style.strike ? "~~" : "")
|
|
52
|
+
|
|
53
|
+
const closing = (style: Style): string =>
|
|
54
|
+
(style.strike ? "~~" : "") + (style.italic ? "*" : "") + (style.bold ? "**" : "")
|
|
55
|
+
|
|
56
|
+
const longestRun = (text: string, char: string): number => {
|
|
57
|
+
let longest = 0
|
|
58
|
+
let run = 0
|
|
59
|
+
for (const c of text) {
|
|
60
|
+
run = c === char ? run + 1 : 0
|
|
61
|
+
if (run > longest) longest = run
|
|
62
|
+
}
|
|
63
|
+
return longest
|
|
64
|
+
}
|
|
65
|
+
|
|
66
|
+
/** One element's content: plain text, or a mention token. */
|
|
67
|
+
type Piece =
|
|
68
|
+
| { readonly _tag: "text"; readonly text: string; readonly markdown?: true }
|
|
69
|
+
| { readonly _tag: "mention"; readonly token: string; readonly kind: Mention["kind"]; readonly id: string }
|
|
70
|
+
|
|
71
|
+
const emojiText = (element: JsonObject): string => {
|
|
72
|
+
const unicode = stringOf(element, "unicode")
|
|
73
|
+
if (unicode !== undefined && /^[0-9a-f]+(-[0-9a-f]+)*$/i.test(unicode)) {
|
|
74
|
+
const points = unicode.split("-").map((hex) => parseInt(hex, 16))
|
|
75
|
+
if (points.every((point) => point <= 0x10ffff)) return String.fromCodePoint(...points)
|
|
76
|
+
}
|
|
77
|
+
const name = stringOf(element, "name")
|
|
78
|
+
if (name === undefined) return ""
|
|
79
|
+
const tone = element.skin_tone
|
|
80
|
+
const full = typeof tone === "number" ? `${name}::skin-tone-${tone}` : name
|
|
81
|
+
return Emoji.fromName(full) ?? Emoji.fromName(name) ?? `:${full}:`
|
|
82
|
+
}
|
|
83
|
+
|
|
84
|
+
/** CommonMark accepts unescaped parentheses in a destination only when they nest in pairs. */
|
|
85
|
+
const linkDestination = (url: string): string => {
|
|
86
|
+
let depth = 0
|
|
87
|
+
for (const char of url) {
|
|
88
|
+
if (char === "(") depth++
|
|
89
|
+
if (char === ")" && --depth < 0) break
|
|
90
|
+
}
|
|
91
|
+
return depth === 0 ? url : url.replace(/[()\\]/g, "\\$&")
|
|
92
|
+
}
|
|
93
|
+
|
|
94
|
+
const pieceOf = (element: JsonObject, inCode: boolean): Piece => {
|
|
95
|
+
switch (element.type) {
|
|
96
|
+
case "text":
|
|
97
|
+
return { _tag: "text", text: stringOf(element, "text") ?? "" }
|
|
98
|
+
case "link": {
|
|
99
|
+
const url = stringOf(element, "url") ?? ""
|
|
100
|
+
const text = stringOf(element, "text")
|
|
101
|
+
// Slack stores a bare URL as a link whose text is the URL without its scheme.
|
|
102
|
+
const bare = text === undefined || text === "" || text === url ||
|
|
103
|
+
text === url.replace(/^[a-z][a-z0-9+.-]*:\/\//i, "")
|
|
104
|
+
return {
|
|
105
|
+
_tag: "text",
|
|
106
|
+
text: bare ? url : `[${escapeInline(text)}](${inCode ? url : linkDestination(url)})`,
|
|
107
|
+
markdown: true
|
|
108
|
+
}
|
|
109
|
+
}
|
|
110
|
+
case "user": {
|
|
111
|
+
const id = stringOf(element, "user_id")
|
|
112
|
+
if (id !== undefined) return { _tag: "mention", token: `<@${id}>`, kind: "user", id }
|
|
113
|
+
break
|
|
114
|
+
}
|
|
115
|
+
case "usergroup": {
|
|
116
|
+
const id = stringOf(element, "usergroup_id")
|
|
117
|
+
if (id !== undefined) return { _tag: "mention", token: `<!subteam^${id}>`, kind: "role", id }
|
|
118
|
+
break
|
|
119
|
+
}
|
|
120
|
+
case "channel": {
|
|
121
|
+
const id = stringOf(element, "channel_id")
|
|
122
|
+
if (id !== undefined) return { _tag: "mention", token: `<#${id}>`, kind: "channel", id }
|
|
123
|
+
break
|
|
124
|
+
}
|
|
125
|
+
case "broadcast": {
|
|
126
|
+
const id = stringOf(element, "range")
|
|
127
|
+
if (id !== undefined) return { _tag: "mention", token: `<!${id}>`, kind: "broadcast", id }
|
|
128
|
+
break
|
|
129
|
+
}
|
|
130
|
+
case "emoji":
|
|
131
|
+
return { _tag: "text", text: emojiText(element) }
|
|
132
|
+
case "date":
|
|
133
|
+
return { _tag: "text", text: stringOf(element, "fallback") ?? "" }
|
|
134
|
+
case "color":
|
|
135
|
+
return { _tag: "text", text: stringOf(element, "value") ?? "" }
|
|
136
|
+
}
|
|
137
|
+
return { _tag: "text", text: stringOf(element, "text") ?? "" }
|
|
138
|
+
}
|
|
139
|
+
|
|
140
|
+
const pieceText = (piece: Piece): string => piece._tag === "text" ? piece.text : piece.token
|
|
141
|
+
|
|
142
|
+
const wordChar = /[\p{L}\p{N}]/u
|
|
143
|
+
const asciiPunctuation = /[!-/:-@[-`{-~]/
|
|
144
|
+
|
|
145
|
+
/**
|
|
146
|
+
* Backslash-escapes what Markdown would read as inline syntax, so a text
|
|
147
|
+
* element's `*literal*` reads back literal. An underscore between two
|
|
148
|
+
* letters or digits opens nothing and is left alone, as is a backslash
|
|
149
|
+
* before anything but punctuation or a line end. `<` and `&` are left
|
|
150
|
+
* alone: the adapter sends them as literal text either way.
|
|
151
|
+
*/
|
|
152
|
+
const escapeInline = (text: string): string =>
|
|
153
|
+
text.replace(/[*_`~[\]\\]/g, (char, at: number) => {
|
|
154
|
+
const before = text[at - 1] ?? ""
|
|
155
|
+
const after = text[at + 1] ?? ""
|
|
156
|
+
if (char === "_" && wordChar.test(before) && wordChar.test(after)) return char
|
|
157
|
+
if (char === "\\" && after !== "" && after !== "\n" && !asciiPunctuation.test(after)) return char
|
|
158
|
+
return "\\" + char
|
|
159
|
+
})
|
|
160
|
+
|
|
161
|
+
/** A line start that would open a heading, list or quote, or underline the line before it. */
|
|
162
|
+
const blockStart = /^( {0,3})(?:([#]{1,6}|[-+])(?=[ \t]|$)|(>)|(\d{1,9})([.)])(?=[ \t]|$)|([-=])(?=[-= \t]*$))/
|
|
163
|
+
|
|
164
|
+
const escapeLineStart = (line: string): string =>
|
|
165
|
+
line.replace(
|
|
166
|
+
blockStart,
|
|
167
|
+
(_, indent: string, marker?: string, quote?: string, digits?: string, delimiter?: string, rule?: string) =>
|
|
168
|
+
marker !== undefined
|
|
169
|
+
? indent + "\\" + marker
|
|
170
|
+
: quote !== undefined
|
|
171
|
+
? indent + "\\>"
|
|
172
|
+
: digits !== undefined
|
|
173
|
+
? indent + digits + "\\" + delimiter
|
|
174
|
+
: indent + "\\" + rule
|
|
175
|
+
)
|
|
176
|
+
|
|
177
|
+
/** A text element's content as Markdown that reads back as the same text. */
|
|
178
|
+
const literal = (text: string, atLineStart: boolean): string =>
|
|
179
|
+
escapeInline(text)
|
|
180
|
+
.split("\n")
|
|
181
|
+
.map((line, index) => index > 0 || atLineStart ? escapeLineStart(line) : line)
|
|
182
|
+
.join("\n")
|
|
183
|
+
|
|
184
|
+
/**
|
|
185
|
+
* Markdown that renders `text` as written, as the start of a message.
|
|
186
|
+
*
|
|
187
|
+
* @internal
|
|
188
|
+
*/
|
|
189
|
+
export const escapeMarkdown = (text: string): string => literal(text, true)
|
|
190
|
+
|
|
191
|
+
interface Writer {
|
|
192
|
+
out: string
|
|
193
|
+
readonly mentions: Array<Mention>
|
|
194
|
+
readonly botUserId: string
|
|
195
|
+
}
|
|
196
|
+
|
|
197
|
+
const write = (writer: Writer, text: string): void => {
|
|
198
|
+
writer.out += text
|
|
199
|
+
}
|
|
200
|
+
|
|
201
|
+
/** Writes a piece outside code: a link as built, other text escaped, a mention as its token. */
|
|
202
|
+
const writePiece = (writer: Writer, piece: Piece, text = pieceText(piece)): void => {
|
|
203
|
+
if (piece._tag === "mention") return mention(writer, piece)
|
|
204
|
+
if (piece.markdown === true) return write(writer, text)
|
|
205
|
+
write(writer, literal(text, writer.out === "" || writer.out.endsWith("\n")))
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
const mention = (writer: Writer, piece: Extract<Piece, { _tag: "mention" }>): void => {
|
|
209
|
+
const start = writer.out.length
|
|
210
|
+
writer.out += piece.token
|
|
211
|
+
writer.mentions.push({
|
|
212
|
+
kind: piece.kind,
|
|
213
|
+
id: piece.id,
|
|
214
|
+
start,
|
|
215
|
+
end: writer.out.length,
|
|
216
|
+
isSelf: piece.kind === "user" && piece.id === writer.botUserId
|
|
217
|
+
})
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
/** Starts a new line unless the output is empty or already ends one. */
|
|
221
|
+
const newline = (writer: Writer): void => {
|
|
222
|
+
if (writer.out !== "" && !writer.out.endsWith("\n")) writer.out += "\n"
|
|
223
|
+
}
|
|
224
|
+
|
|
225
|
+
/**
|
|
226
|
+
* Inline elements, grouped into runs of one style so markers wrap a run and
|
|
227
|
+
* not each element. Whitespace at either end of a styled run goes outside
|
|
228
|
+
* its markers, where Markdown needs it. Code is a span, and a token in it is
|
|
229
|
+
* a literal.
|
|
230
|
+
*/
|
|
231
|
+
const inline = (writer: Writer, elements: ReadonlyArray<unknown>): void => {
|
|
232
|
+
const items = elements.filter(isObject).map((element) => {
|
|
233
|
+
const style = styleOf(element)
|
|
234
|
+
return { piece: pieceOf(element, style.code), style }
|
|
235
|
+
})
|
|
236
|
+
let i = 0
|
|
237
|
+
while (i < items.length) {
|
|
238
|
+
const style = items[i].style
|
|
239
|
+
let j = i + 1
|
|
240
|
+
while (j < items.length && sameStyle(items[j].style, style)) j++
|
|
241
|
+
const run = items.slice(i, j).map((item) => item.piece)
|
|
242
|
+
i = j
|
|
243
|
+
if (style.code) {
|
|
244
|
+
const content = run.map(pieceText).join("")
|
|
245
|
+
const fence = "`".repeat(longestRun(content, "`") + 1)
|
|
246
|
+
// A backtick at either end would join the fence, and Markdown strips
|
|
247
|
+
// one space from each end of a span that starts and ends with one.
|
|
248
|
+
const pad = content.startsWith("`") || content.endsWith("`") ||
|
|
249
|
+
(content.startsWith(" ") && content.endsWith(" ") && content.trim() !== "")
|
|
250
|
+
? " "
|
|
251
|
+
: ""
|
|
252
|
+
write(writer, opening(style) + fence + pad + content + pad + fence + closing(style))
|
|
253
|
+
continue
|
|
254
|
+
}
|
|
255
|
+
const open = opening(style)
|
|
256
|
+
if (open === "") {
|
|
257
|
+
for (const piece of run) writePiece(writer, piece)
|
|
258
|
+
continue
|
|
259
|
+
}
|
|
260
|
+
const first = run[0]
|
|
261
|
+
const last = run[run.length - 1]
|
|
262
|
+
const lead = first._tag === "text" ? /^\s*/.exec(first.text)![0] : ""
|
|
263
|
+
const trail = last._tag === "text" ? /\s*$/.exec(last.text)![0] : ""
|
|
264
|
+
if (run.length === 1 && first._tag === "text" && lead.length === first.text.length) {
|
|
265
|
+
writePiece(writer, first)
|
|
266
|
+
continue
|
|
267
|
+
}
|
|
268
|
+
write(writer, lead + open)
|
|
269
|
+
run.forEach((piece, k) => {
|
|
270
|
+
let text = pieceText(piece)
|
|
271
|
+
if (k === 0) text = text.slice(lead.length)
|
|
272
|
+
if (k === run.length - 1) text = text.slice(0, text.length - trail.length)
|
|
273
|
+
writePiece(writer, piece, text)
|
|
274
|
+
})
|
|
275
|
+
write(writer, closing(style) + trail)
|
|
276
|
+
}
|
|
277
|
+
}
|
|
278
|
+
|
|
279
|
+
/** Prefixes each line written since `from`, and moves the mentions in it to match. */
|
|
280
|
+
const prefixLines = (writer: Writer, from: number, first: string, rest: string): void => {
|
|
281
|
+
const body = writer.out.slice(from)
|
|
282
|
+
const lines = body.split("\n")
|
|
283
|
+
let rewritten = ""
|
|
284
|
+
const shifts: Array<readonly [number, number]> = []
|
|
285
|
+
let position = from
|
|
286
|
+
lines.forEach((line, index) => {
|
|
287
|
+
const prefix = index === 0 ? first : rest
|
|
288
|
+
shifts.push([position, prefix.length])
|
|
289
|
+
rewritten += (index === 0 ? "" : "\n") + prefix + line
|
|
290
|
+
position += line.length + 1
|
|
291
|
+
})
|
|
292
|
+
writer.out = writer.out.slice(0, from) + rewritten
|
|
293
|
+
// Adjust mention offsets for inserted prefixes.
|
|
294
|
+
for (let m = 0; m < writer.mentions.length; m++) {
|
|
295
|
+
const mention = writer.mentions[m]
|
|
296
|
+
if (mention.start < from) continue
|
|
297
|
+
let shift = 0
|
|
298
|
+
for (const [at, length] of shifts) if (at <= mention.start) shift += length
|
|
299
|
+
writer.mentions[m] = { ...mention, start: mention.start + shift, end: mention.end + shift }
|
|
300
|
+
}
|
|
301
|
+
}
|
|
302
|
+
|
|
303
|
+
const block = (writer: Writer, element: unknown): void => {
|
|
304
|
+
if (!isObject(element)) return
|
|
305
|
+
switch (element.type) {
|
|
306
|
+
case "rich_text_section":
|
|
307
|
+
return inline(writer, elementsOf(element))
|
|
308
|
+
case "rich_text_preformatted": {
|
|
309
|
+
// A fence holds no mention: every token is literal.
|
|
310
|
+
const content = elementsOf(element).filter(isObject).map((child) => pieceText(pieceOf(child, true))).join("")
|
|
311
|
+
const fence = "`".repeat(Math.max(3, longestRun(content, "`") + 1))
|
|
312
|
+
write(writer, fence + "\n" + content + (content.endsWith("\n") ? "" : "\n") + fence)
|
|
313
|
+
return
|
|
314
|
+
}
|
|
315
|
+
case "rich_text_quote": {
|
|
316
|
+
const from = writer.out.length
|
|
317
|
+
inline(writer, elementsOf(element))
|
|
318
|
+
return prefixLines(writer, from, "> ", "> ")
|
|
319
|
+
}
|
|
320
|
+
case "rich_text_list": {
|
|
321
|
+
const indent = " ".repeat(typeof element.indent === "number" ? Math.max(0, element.indent) : 0)
|
|
322
|
+
const ordered = element.style === "ordered"
|
|
323
|
+
const offset = typeof element.offset === "number" ? element.offset : 0
|
|
324
|
+
elementsOf(element).forEach((item, index) => {
|
|
325
|
+
if (index > 0) write(writer, "\n")
|
|
326
|
+
const marker = ordered ? `${offset + index + 1}. ` : "- "
|
|
327
|
+
const from = writer.out.length
|
|
328
|
+
block(writer, item)
|
|
329
|
+
prefixLines(writer, from, indent + marker, indent + " ".repeat(marker.length))
|
|
330
|
+
})
|
|
331
|
+
return
|
|
332
|
+
}
|
|
333
|
+
}
|
|
334
|
+
const text = stringOf(element, "text")
|
|
335
|
+
if (text !== undefined) write(writer, text)
|
|
336
|
+
}
|
|
337
|
+
|
|
338
|
+
/**
|
|
339
|
+
* The Markdown of a message's blocks, with the mentions at the offsets the
|
|
340
|
+
* renderer wrote. A mention is a `user`, `usergroup`, `channel` or
|
|
341
|
+
* `broadcast` element outside code. Blocks other than `rich_text` give
|
|
342
|
+
* their `text` when it is a string, and nothing otherwise.
|
|
343
|
+
*
|
|
344
|
+
* @internal
|
|
345
|
+
*/
|
|
346
|
+
export const render = (blocks: ReadonlyArray<unknown>, botUserId: string): {
|
|
347
|
+
readonly text: string
|
|
348
|
+
readonly mentions: ReadonlyArray<Mention>
|
|
349
|
+
} => {
|
|
350
|
+
const writer: Writer = { out: "", mentions: [], botUserId }
|
|
351
|
+
for (const item of blocks) {
|
|
352
|
+
if (!isObject(item)) continue
|
|
353
|
+
if (item.type === "rich_text") {
|
|
354
|
+
for (const element of elementsOf(item)) {
|
|
355
|
+
newline(writer)
|
|
356
|
+
block(writer, element)
|
|
357
|
+
}
|
|
358
|
+
} else {
|
|
359
|
+
const text = stringOf(item, "text")
|
|
360
|
+
if (text !== undefined && text !== "") {
|
|
361
|
+
newline(writer)
|
|
362
|
+
write(writer, text)
|
|
363
|
+
}
|
|
364
|
+
}
|
|
365
|
+
}
|
|
366
|
+
return { text: writer.out, mentions: writer.mentions }
|
|
367
|
+
}
|