@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,135 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The minimal Slack Web API client the adapter is built on. It is public, so
|
|
3
|
+
* a consumer can call a method the adapter has no op for, with the same error
|
|
4
|
+
* mapping and the same rule for writes.
|
|
5
|
+
*
|
|
6
|
+
* @stability unstable
|
|
7
|
+
* @since 4.1.5
|
|
8
|
+
*/
|
|
9
|
+
import { retrySafe } from "effect/chat/ChatAdapter"
|
|
10
|
+
import type { ChatError } from "effect/chat/ChatError"
|
|
11
|
+
import * as Context from "effect/Context"
|
|
12
|
+
import * as Effect from "effect/Effect"
|
|
13
|
+
import * as HttpClient from "effect/http/HttpClient"
|
|
14
|
+
import * as HttpClientRequest from "effect/http/HttpClientRequest"
|
|
15
|
+
import * as Layer from "effect/Layer"
|
|
16
|
+
import * as Predicate from "effect/Predicate"
|
|
17
|
+
import * as Redacted from "effect/Redacted"
|
|
18
|
+
import type * as Schema from "effect/Schema"
|
|
19
|
+
import * as Errors from "./internal/errors.ts"
|
|
20
|
+
|
|
21
|
+
/**
|
|
22
|
+
* Options for one Web API call.
|
|
23
|
+
*
|
|
24
|
+
* @stability unstable
|
|
25
|
+
* @category models
|
|
26
|
+
* @since 4.1.5
|
|
27
|
+
*/
|
|
28
|
+
export interface CallOptions {
|
|
29
|
+
/**
|
|
30
|
+
* The call changes something. It is never retried after the request left,
|
|
31
|
+
* and a failure that may have landed is `uncertain`.
|
|
32
|
+
*/
|
|
33
|
+
readonly write?: boolean | undefined
|
|
34
|
+
/** Retry `retry` failures, as `ChatAdapter.retrySafe` does. Default true. */
|
|
35
|
+
readonly retry?: boolean | undefined
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
/**
|
|
39
|
+
* The minimal Web API client, with the bot token.
|
|
40
|
+
*
|
|
41
|
+
* **Details**
|
|
42
|
+
*
|
|
43
|
+
* - `call` posts form parameters to `https://slack.com/api/{method}`. A
|
|
44
|
+
* string goes as it is, any other value as JSON, and `undefined` is left
|
|
45
|
+
* out. It succeeds with the answer once Slack says `ok`.
|
|
46
|
+
* - An `ok: false` answer fails with the reason for Slack's error code, which
|
|
47
|
+
* becomes the reason's `detail`. `ratelimited` and an HTTP 429 give
|
|
48
|
+
* `RateLimited` with the `Retry-After` wait.
|
|
49
|
+
* - A write never repeats once the request left: a transport failure, a
|
|
50
|
+
* server error or an unreadable answer is `uncertain`. A read retries
|
|
51
|
+
* `retry` failures unless `retry: false`.
|
|
52
|
+
* - The error's `op` is the method name.
|
|
53
|
+
*
|
|
54
|
+
* @stability unstable
|
|
55
|
+
* @category services
|
|
56
|
+
* @since 4.1.5
|
|
57
|
+
*/
|
|
58
|
+
export class SlackWebApi extends Context.Service<SlackWebApi, {
|
|
59
|
+
readonly call: (
|
|
60
|
+
method: string,
|
|
61
|
+
body: Readonly<Record<string, unknown>>,
|
|
62
|
+
options?: CallOptions | undefined
|
|
63
|
+
) => Effect.Effect<Schema.Json, ChatError>
|
|
64
|
+
}>()("@effect/chat-slack/SlackWebApi") {}
|
|
65
|
+
|
|
66
|
+
const formBody = (body: Readonly<Record<string, unknown>>): Record<string, string> => {
|
|
67
|
+
const out: Record<string, string> = {}
|
|
68
|
+
for (const [key, value] of Object.entries(body)) {
|
|
69
|
+
if (value === undefined) continue
|
|
70
|
+
out[key] = typeof value === "string" ? value : JSON.stringify(value)
|
|
71
|
+
}
|
|
72
|
+
return out
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/**
|
|
76
|
+
* Builds the client from an `HttpClient` and a bot token.
|
|
77
|
+
*
|
|
78
|
+
* @stability unstable
|
|
79
|
+
* @category constructors
|
|
80
|
+
* @since 4.1.5
|
|
81
|
+
*/
|
|
82
|
+
export const make = Effect.fnUntraced(function*(options: {
|
|
83
|
+
readonly botToken: Redacted.Redacted
|
|
84
|
+
readonly baseUrl?: string | undefined
|
|
85
|
+
}) {
|
|
86
|
+
const http = yield* HttpClient.HttpClient
|
|
87
|
+
const baseUrl = options.baseUrl ?? "https://slack.com/api"
|
|
88
|
+
|
|
89
|
+
const once = (method: string, body: Readonly<Record<string, unknown>>, write: boolean) => {
|
|
90
|
+
const fail = (reason: Parameters<typeof Errors.chatError>[1]) => Effect.fail(Errors.chatError(method, reason))
|
|
91
|
+
const request = HttpClientRequest.post(`${baseUrl}/${method}`).pipe(
|
|
92
|
+
HttpClientRequest.bearerToken(Redacted.value(options.botToken)),
|
|
93
|
+
HttpClientRequest.bodyUrlParams(formBody(body))
|
|
94
|
+
)
|
|
95
|
+
return http.execute(request).pipe(
|
|
96
|
+
Effect.catch((error) => fail(Errors.transport(write, error.reason._tag, error))),
|
|
97
|
+
Effect.flatMap((response) => {
|
|
98
|
+
const retryAfter = Errors.parseRetryAfter(response.headers["retry-after"])
|
|
99
|
+
if (response.status < 200 || response.status >= 300) {
|
|
100
|
+
return fail(Errors.reasonForStatus(response.status, { write, retryAfter }))
|
|
101
|
+
}
|
|
102
|
+
return response.json.pipe(
|
|
103
|
+
Effect.catch((error) => fail(Errors.invalidResponse(write, "invalid_json", error))),
|
|
104
|
+
Effect.flatMap((json): Effect.Effect<Schema.Json, ChatError> => {
|
|
105
|
+
if (!Predicate.isObject(json) || typeof json.ok !== "boolean") {
|
|
106
|
+
return fail(Errors.invalidResponse(write, "invalid_response"))
|
|
107
|
+
}
|
|
108
|
+
if (json.ok) return Effect.succeed(json)
|
|
109
|
+
const code = typeof json.error === "string" ? json.error : "unknown_error"
|
|
110
|
+
return fail(Errors.reasonForCode(code, { write, retryAfter }))
|
|
111
|
+
})
|
|
112
|
+
)
|
|
113
|
+
})
|
|
114
|
+
)
|
|
115
|
+
}
|
|
116
|
+
|
|
117
|
+
return SlackWebApi.of({
|
|
118
|
+
call: (method, body, callOptions) => {
|
|
119
|
+
const call = once(method, body, callOptions?.write === true)
|
|
120
|
+
return callOptions?.retry === false ? call : retrySafe(call)
|
|
121
|
+
}
|
|
122
|
+
})
|
|
123
|
+
})
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The client as a layer.
|
|
127
|
+
*
|
|
128
|
+
* @stability unstable
|
|
129
|
+
* @category layers
|
|
130
|
+
* @since 4.1.5
|
|
131
|
+
*/
|
|
132
|
+
export const layer = (options: {
|
|
133
|
+
readonly botToken: Redacted.Redacted
|
|
134
|
+
readonly baseUrl?: string | undefined
|
|
135
|
+
}): Layer.Layer<SlackWebApi, never, HttpClient.HttpClient> => Layer.effect(SlackWebApi, make(options))
|
package/src/index.ts
ADDED
|
@@ -0,0 +1,18 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* @stability unstable
|
|
3
|
+
* @since 4.1.5
|
|
4
|
+
*/
|
|
5
|
+
|
|
6
|
+
// @barrel: Auto-generated exports. Do not edit manually.
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* @stability unstable
|
|
10
|
+
* @since 4.1.5
|
|
11
|
+
*/
|
|
12
|
+
export * as SlackChat from "./SlackChat.ts"
|
|
13
|
+
|
|
14
|
+
/**
|
|
15
|
+
* @stability unstable
|
|
16
|
+
* @since 4.1.5
|
|
17
|
+
*/
|
|
18
|
+
export * as SlackWebApi from "./SlackWebApi.ts"
|
|
@@ -0,0 +1,203 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* A message's elements as Slack's `actions` block, and what that block
|
|
3
|
+
* costs against the 13,200 cap.
|
|
4
|
+
*/
|
|
5
|
+
import type { Action } from "effect/chat/ChatUi"
|
|
6
|
+
import * as Emoji from "./emoji.ts"
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* The `block_id` of the `actions` block the adapter builds, and so the
|
|
10
|
+
* mark of a click on one of its own elements. Five ASCII characters, the
|
|
11
|
+
* length of the id Slack generates, which `cost` charges.
|
|
12
|
+
*
|
|
13
|
+
* @internal
|
|
14
|
+
*/
|
|
15
|
+
export const blockId = "chat1"
|
|
16
|
+
|
|
17
|
+
/**
|
|
18
|
+
* The one character the adapter reserves: a button or select id may not hold
|
|
19
|
+
* it, a link button's `action_id` starts with it, and it separates the
|
|
20
|
+
* parts of a modal's `private_metadata`.
|
|
21
|
+
*
|
|
22
|
+
* @internal
|
|
23
|
+
*/
|
|
24
|
+
export const join = "\u241F"
|
|
25
|
+
|
|
26
|
+
interface PlainText {
|
|
27
|
+
readonly type: "plain_text"
|
|
28
|
+
readonly text: string
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
interface SelectOption {
|
|
32
|
+
readonly text: PlainText
|
|
33
|
+
readonly value: string
|
|
34
|
+
}
|
|
35
|
+
|
|
36
|
+
/** @internal */
|
|
37
|
+
export type Element =
|
|
38
|
+
| {
|
|
39
|
+
readonly type: "button"
|
|
40
|
+
readonly action_id: string
|
|
41
|
+
readonly text: PlainText
|
|
42
|
+
readonly value?: string
|
|
43
|
+
readonly url?: string
|
|
44
|
+
readonly style?: "primary" | "danger"
|
|
45
|
+
}
|
|
46
|
+
| {
|
|
47
|
+
readonly type: "static_select"
|
|
48
|
+
readonly action_id: string
|
|
49
|
+
readonly placeholder?: PlainText
|
|
50
|
+
readonly options: ReadonlyArray<SelectOption>
|
|
51
|
+
readonly initial_option?: SelectOption
|
|
52
|
+
}
|
|
53
|
+
|
|
54
|
+
/** @internal */
|
|
55
|
+
export const plainText = (text: string): PlainText => ({ type: "plain_text", text })
|
|
56
|
+
|
|
57
|
+
/** @internal */
|
|
58
|
+
export const option = (label: string, value: string): SelectOption => ({ text: plainText(label), value })
|
|
59
|
+
|
|
60
|
+
/**
|
|
61
|
+
* The elements for `actions`, in order. A button's id is its `action_id`
|
|
62
|
+
* and its value its `value`. A link button is a button with a `url`, and an
|
|
63
|
+
* `action_id` of the join and its index, which no id of the consumer's can
|
|
64
|
+
* be. A select's options carry their values, and `selected` is its
|
|
65
|
+
* `initial_option`.
|
|
66
|
+
*
|
|
67
|
+
* @internal
|
|
68
|
+
*/
|
|
69
|
+
export const elements = (actions: ReadonlyArray<Action>): Array<Element> =>
|
|
70
|
+
actions.map((action, index): Element => {
|
|
71
|
+
switch (action._tag) {
|
|
72
|
+
case "Button":
|
|
73
|
+
return {
|
|
74
|
+
type: "button",
|
|
75
|
+
action_id: action.id,
|
|
76
|
+
text: plainText(action.label),
|
|
77
|
+
...(action.value === undefined ? {} : { value: action.value }),
|
|
78
|
+
...(action.style === "primary" || action.style === "danger" ? { style: action.style } : {})
|
|
79
|
+
}
|
|
80
|
+
case "LinkButton":
|
|
81
|
+
return { type: "button", action_id: join + index, text: plainText(action.label), url: action.url }
|
|
82
|
+
case "Select": {
|
|
83
|
+
const selected = action.options.find((option) => option.value === action.selected)
|
|
84
|
+
return {
|
|
85
|
+
type: "static_select",
|
|
86
|
+
action_id: action.id,
|
|
87
|
+
...(action.placeholder === undefined ? {} : { placeholder: plainText(action.placeholder) }),
|
|
88
|
+
options: action.options.map((entry) => option(entry.label, entry.value)),
|
|
89
|
+
...(selected === undefined ? {} : { initial_option: option(selected.label, selected.value) })
|
|
90
|
+
}
|
|
91
|
+
}
|
|
92
|
+
}
|
|
93
|
+
})
|
|
94
|
+
|
|
95
|
+
/**
|
|
96
|
+
* The `actions` block for a message's elements.
|
|
97
|
+
*
|
|
98
|
+
* @internal
|
|
99
|
+
*/
|
|
100
|
+
export const block = (actions: ReadonlyArray<Action>) => ({
|
|
101
|
+
type: "actions" as const,
|
|
102
|
+
block_id: blockId,
|
|
103
|
+
elements: elements(actions)
|
|
104
|
+
})
|
|
105
|
+
|
|
106
|
+
/**
|
|
107
|
+
* The `text` of a message with elements and no Markdown, before escaping:
|
|
108
|
+
* the button labels and the select placeholders, joined by a space.
|
|
109
|
+
*
|
|
110
|
+
* @internal
|
|
111
|
+
*/
|
|
112
|
+
export const labels = (actions: ReadonlyArray<Action>): string =>
|
|
113
|
+
actions.flatMap((action) =>
|
|
114
|
+
action._tag === "Select" ? action.placeholder === undefined ? [] : [action.placeholder] : [action.label]
|
|
115
|
+
).join(" ")
|
|
116
|
+
|
|
117
|
+
// Cost
|
|
118
|
+
|
|
119
|
+
/** The fixed costs of "What `actionsCost` charges". */
|
|
120
|
+
const costs = {
|
|
121
|
+
block: 56,
|
|
122
|
+
select: 52,
|
|
123
|
+
placeholder: 59,
|
|
124
|
+
option: 64,
|
|
125
|
+
initialOption: 18,
|
|
126
|
+
button: 95,
|
|
127
|
+
url: 9,
|
|
128
|
+
noValue: 11,
|
|
129
|
+
style: 18,
|
|
130
|
+
separator: 1
|
|
131
|
+
} as const
|
|
132
|
+
|
|
133
|
+
/** One UTF-16 unit as Slack's server writes it in JSON. */
|
|
134
|
+
const unitCost = (code: number): number =>
|
|
135
|
+
code < 0x20 || code > 0x7e ? 6 : code === 0x2f || code === 0x22 || code === 0x5c ? 2 : 1
|
|
136
|
+
|
|
137
|
+
/**
|
|
138
|
+
* A string as Slack's server writes it in JSON: 6 for any unit that is not
|
|
139
|
+
* printable ASCII, 2 for `/`, `"` and `\\`, 1 otherwise. An id, a value,
|
|
140
|
+
* a url and an option label cost this.
|
|
141
|
+
*
|
|
142
|
+
* @internal
|
|
143
|
+
*/
|
|
144
|
+
export const stringCost = (text: string): number => {
|
|
145
|
+
let cost = 0
|
|
146
|
+
for (let i = 0; i < text.length; i++) cost += unitCost(text.charCodeAt(i))
|
|
147
|
+
return cost
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
/**
|
|
151
|
+
* A button label or a placeholder: `<`, `>` and `&` are stored as
|
|
152
|
+
* entities, and an emoji costs the greater of its `:name:` and its escaped
|
|
153
|
+
* form.
|
|
154
|
+
*
|
|
155
|
+
* @internal
|
|
156
|
+
*/
|
|
157
|
+
export const labelCost = (text: string): number => {
|
|
158
|
+
let cost = 0
|
|
159
|
+
for (let i = 0; i < text.length;) {
|
|
160
|
+
const emoji = Emoji.longestAt(text, i)
|
|
161
|
+
if (emoji !== undefined) {
|
|
162
|
+
cost += Math.max(emoji.name.length + 2, 6 * emoji.length)
|
|
163
|
+
i += emoji.length
|
|
164
|
+
continue
|
|
165
|
+
}
|
|
166
|
+
const code = text.charCodeAt(i++)
|
|
167
|
+
cost += code === 0x3c || code === 0x3e ? 4 : code === 0x26 ? 5 : unitCost(code)
|
|
168
|
+
}
|
|
169
|
+
return cost
|
|
170
|
+
}
|
|
171
|
+
|
|
172
|
+
const optionCost = (option: SelectOption): number =>
|
|
173
|
+
costs.option + stringCost(option.text.text) + stringCost(option.value)
|
|
174
|
+
|
|
175
|
+
const elementCost = (element: Element): number => {
|
|
176
|
+
if (element.type === "button") {
|
|
177
|
+
let cost = costs.button + labelCost(element.text.text) + stringCost(element.action_id) +
|
|
178
|
+
stringCost(element.value ?? "")
|
|
179
|
+
if (element.url !== undefined) {
|
|
180
|
+
cost += costs.url + stringCost(element.url) - (element.value === undefined ? costs.noValue : 0)
|
|
181
|
+
}
|
|
182
|
+
return element.style === undefined ? cost : cost + costs.style
|
|
183
|
+
}
|
|
184
|
+
let cost = costs.select + stringCost(element.action_id)
|
|
185
|
+
if (element.placeholder !== undefined) cost += costs.placeholder + labelCost(element.placeholder.text)
|
|
186
|
+
for (const option of element.options) cost += optionCost(option)
|
|
187
|
+
cost += Math.max(0, element.options.length - 1) * costs.separator
|
|
188
|
+
if (element.initial_option !== undefined) cost += costs.initialOption + optionCost(element.initial_option)
|
|
189
|
+
return cost
|
|
190
|
+
}
|
|
191
|
+
|
|
192
|
+
/**
|
|
193
|
+
* An upper bound on the `actions` block's JSON as Slack's server writes it,
|
|
194
|
+
* which the 13,200 cap charges. 0 for no elements.
|
|
195
|
+
*
|
|
196
|
+
* @internal
|
|
197
|
+
*/
|
|
198
|
+
export const cost = (elements: ReadonlyArray<Element>): number => {
|
|
199
|
+
if (elements.length === 0) return 0
|
|
200
|
+
let total = costs.block + (elements.length - 1) * costs.separator
|
|
201
|
+
for (const element of elements) total += elementCost(element)
|
|
202
|
+
return total
|
|
203
|
+
}
|