@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.
Files changed (182) hide show
  1. package/AGENTS.md +434 -0
  2. package/CLAUDE.md +434 -0
  3. package/LICENSE +21 -0
  4. package/README.md +47 -0
  5. package/ai-docs/README.md +44 -0
  6. package/ai-docs/package.json +36 -0
  7. package/ai-docs/src/01_effect/01_basics/01_effect-gen.ts +30 -0
  8. package/ai-docs/src/01_effect/01_basics/02_effect-fn.ts +52 -0
  9. package/ai-docs/src/01_effect/01_basics/10_creating-effects.ts +74 -0
  10. package/ai-docs/src/01_effect/01_basics/index.md +7 -0
  11. package/ai-docs/src/01_effect/02_schema/10_schema-basics.ts +43 -0
  12. package/ai-docs/src/01_effect/02_schema/index.md +7 -0
  13. package/ai-docs/src/01_effect/03_services/01_service.ts +45 -0
  14. package/ai-docs/src/01_effect/03_services/10_reference.ts +10 -0
  15. package/ai-docs/src/01_effect/03_services/20_layer-composition.ts +70 -0
  16. package/ai-docs/src/01_effect/03_services/20_layer-unwrap.ts +66 -0
  17. package/ai-docs/src/01_effect/03_services/index.md +5 -0
  18. package/ai-docs/src/01_effect/04_errors/01_error-handling.ts +30 -0
  19. package/ai-docs/src/01_effect/04_errors/10_catch-tags.ts +24 -0
  20. package/ai-docs/src/01_effect/04_errors/20_reason-errors.ts +64 -0
  21. package/ai-docs/src/01_effect/04_errors/index.md +1 -0
  22. package/ai-docs/src/01_effect/05_resources/10_acquire-release.ts +105 -0
  23. package/ai-docs/src/01_effect/05_resources/20_layer-side-effects.ts +31 -0
  24. package/ai-docs/src/01_effect/05_resources/30_layer-map.ts +86 -0
  25. package/ai-docs/src/01_effect/05_resources/index.md +3 -0
  26. package/ai-docs/src/01_effect/06_running/10_run-main.ts +30 -0
  27. package/ai-docs/src/01_effect/06_running/20_layer-launch.ts +27 -0
  28. package/ai-docs/src/01_effect/06_running/index.md +1 -0
  29. package/ai-docs/src/01_effect/07_pubsub/10_pubsub.ts +56 -0
  30. package/ai-docs/src/01_effect/07_pubsub/index.md +3 -0
  31. package/ai-docs/src/03_stream/10_creating-streams.ts +103 -0
  32. package/ai-docs/src/03_stream/20_consuming-streams.ts +137 -0
  33. package/ai-docs/src/03_stream/30_encoding.ts +163 -0
  34. package/ai-docs/src/03_stream/index.md +4 -0
  35. package/ai-docs/src/04_integration/10_managed-runtime.ts +129 -0
  36. package/ai-docs/src/04_integration/index.md +5 -0
  37. package/ai-docs/src/05_batching/10_request-resolver.ts +89 -0
  38. package/ai-docs/src/05_batching/index.md +3 -0
  39. package/ai-docs/src/06_schedule/10_schedules.ts +111 -0
  40. package/ai-docs/src/06_schedule/index.md +3 -0
  41. package/ai-docs/src/07_datetime/10_creating-and-formatting.ts +30 -0
  42. package/ai-docs/src/07_datetime/20_time-zones.ts +44 -0
  43. package/ai-docs/src/07_datetime/index.md +5 -0
  44. package/ai-docs/src/08_observability/10_logging.ts +66 -0
  45. package/ai-docs/src/08_observability/20_otlp-tracing.ts +95 -0
  46. package/ai-docs/src/08_observability/index.md +7 -0
  47. package/ai-docs/src/09_testing/10_effect-tests.ts +55 -0
  48. package/ai-docs/src/09_testing/20_layer-tests.ts +138 -0
  49. package/ai-docs/src/09_testing/index.md +1 -0
  50. package/ai-docs/src/10_predicate/01_basics.ts +14 -0
  51. package/ai-docs/src/10_predicate/index.md +9 -0
  52. package/ai-docs/src/40_sql/10_basics.ts +175 -0
  53. package/ai-docs/src/40_sql/index.md +6 -0
  54. package/ai-docs/src/50_http-client/10_basics.ts +102 -0
  55. package/ai-docs/src/50_http-client/index.md +3 -0
  56. package/ai-docs/src/51_http-server/10_basics.ts +118 -0
  57. package/ai-docs/src/51_http-server/20_testing.ts +100 -0
  58. package/ai-docs/src/51_http-server/fixtures/api/Api.ts +14 -0
  59. package/ai-docs/src/51_http-server/fixtures/api/Authorization.ts +36 -0
  60. package/ai-docs/src/51_http-server/fixtures/api/System.ts +10 -0
  61. package/ai-docs/src/51_http-server/fixtures/api/Users.ts +107 -0
  62. package/ai-docs/src/51_http-server/fixtures/domain/User.ts +21 -0
  63. package/ai-docs/src/51_http-server/fixtures/domain/UserErrors.ts +22 -0
  64. package/ai-docs/src/51_http-server/fixtures/server/Authorization.ts +37 -0
  65. package/ai-docs/src/51_http-server/fixtures/server/Users/http.ts +80 -0
  66. package/ai-docs/src/51_http-server/fixtures/server/Users.ts +183 -0
  67. package/ai-docs/src/51_http-server/index.md +3 -0
  68. package/ai-docs/src/52_graphql/10_basics.ts +117 -0
  69. package/ai-docs/src/52_graphql/20_testing.ts +45 -0
  70. package/ai-docs/src/52_graphql/30_batching.ts +118 -0
  71. package/ai-docs/src/52_graphql/40_aliased-batching.ts +124 -0
  72. package/ai-docs/src/52_graphql/50_errors-and-retries.ts +74 -0
  73. package/ai-docs/src/52_graphql/index.md +6 -0
  74. package/ai-docs/src/60_child-process/10_working-with-child-processes.ts +117 -0
  75. package/ai-docs/src/60_child-process/index.md +3 -0
  76. package/ai-docs/src/70_cli/10_basics.ts +162 -0
  77. package/ai-docs/src/70_cli/index.md +5 -0
  78. package/ai-docs/src/71_ai/10_language-model.ts +156 -0
  79. package/ai-docs/src/71_ai/20_tools.ts +226 -0
  80. package/ai-docs/src/71_ai/30_chat.ts +158 -0
  81. package/ai-docs/src/71_ai/fixtures/domain/LaunchPlan.ts +9 -0
  82. package/ai-docs/src/71_ai/index.md +5 -0
  83. package/ai-docs/src/80_cluster/10_entities.ts +97 -0
  84. package/ai-docs/src/80_cluster/index.md +4 -0
  85. package/ai-docs/src/index.md +10 -0
  86. package/ai-docs/tsconfig.json +28 -0
  87. package/dist/SlackChat.d.ts +500 -0
  88. package/dist/SlackChat.d.ts.map +1 -0
  89. package/dist/SlackChat.js +512 -0
  90. package/dist/SlackChat.js.map +1 -0
  91. package/dist/SlackWebApi.d.ts +74 -0
  92. package/dist/SlackWebApi.d.ts.map +1 -0
  93. package/dist/SlackWebApi.js +96 -0
  94. package/dist/SlackWebApi.js.map +1 -0
  95. package/dist/index.d.ts +15 -0
  96. package/dist/index.d.ts.map +1 -0
  97. package/dist/index.js +16 -0
  98. package/dist/index.js.map +1 -0
  99. package/dist/internal/actions.d.ts +2 -0
  100. package/dist/internal/actions.d.ts.map +1 -0
  101. package/dist/internal/actions.js +169 -0
  102. package/dist/internal/actions.js.map +1 -0
  103. package/dist/internal/adapter.d.ts +2 -0
  104. package/dist/internal/adapter.d.ts.map +1 -0
  105. package/dist/internal/adapter.js +836 -0
  106. package/dist/internal/adapter.js.map +1 -0
  107. package/dist/internal/address.d.ts +2 -0
  108. package/dist/internal/address.d.ts.map +1 -0
  109. package/dist/internal/address.js +36 -0
  110. package/dist/internal/address.js.map +1 -0
  111. package/dist/internal/emoji.d.ts +2 -0
  112. package/dist/internal/emoji.d.ts.map +1 -0
  113. package/dist/internal/emoji.js +207 -0
  114. package/dist/internal/emoji.js.map +1 -0
  115. package/dist/internal/emojiData.d.ts +54 -0
  116. package/dist/internal/emojiData.d.ts.map +1 -0
  117. package/dist/internal/emojiData.js +1965 -0
  118. package/dist/internal/emojiData.js.map +1 -0
  119. package/dist/internal/errors.d.ts +2 -0
  120. package/dist/internal/errors.d.ts.map +1 -0
  121. package/dist/internal/errors.js +190 -0
  122. package/dist/internal/errors.js.map +1 -0
  123. package/dist/internal/escape.d.ts +14 -0
  124. package/dist/internal/escape.d.ts.map +1 -0
  125. package/dist/internal/escape.js +413 -0
  126. package/dist/internal/escape.js.map +1 -0
  127. package/dist/internal/interactions.d.ts +2 -0
  128. package/dist/internal/interactions.d.ts.map +1 -0
  129. package/dist/internal/interactions.js +389 -0
  130. package/dist/internal/interactions.js.map +1 -0
  131. package/dist/internal/json.d.ts +2 -0
  132. package/dist/internal/json.d.ts.map +1 -0
  133. package/dist/internal/json.js +30 -0
  134. package/dist/internal/json.js.map +1 -0
  135. package/dist/internal/length.d.ts +2 -0
  136. package/dist/internal/length.d.ts.map +1 -0
  137. package/dist/internal/length.js +33 -0
  138. package/dist/internal/length.js.map +1 -0
  139. package/dist/internal/mentions.d.ts +2 -0
  140. package/dist/internal/mentions.d.ts.map +1 -0
  141. package/dist/internal/mentions.js +229 -0
  142. package/dist/internal/mentions.js.map +1 -0
  143. package/dist/internal/normalize.d.ts +2 -0
  144. package/dist/internal/normalize.d.ts.map +1 -0
  145. package/dist/internal/normalize.js +211 -0
  146. package/dist/internal/normalize.js.map +1 -0
  147. package/dist/internal/options.d.ts +2 -0
  148. package/dist/internal/options.d.ts.map +1 -0
  149. package/dist/internal/options.js +6 -0
  150. package/dist/internal/options.js.map +1 -0
  151. package/dist/internal/richText.d.ts +2 -0
  152. package/dist/internal/richText.d.ts.map +1 -0
  153. package/dist/internal/richText.js +346 -0
  154. package/dist/internal/richText.js.map +1 -0
  155. package/dist/internal/text.d.ts +2 -0
  156. package/dist/internal/text.d.ts.map +1 -0
  157. package/dist/internal/text.js +239 -0
  158. package/dist/internal/text.js.map +1 -0
  159. package/dist/internal/webhook.d.ts +2 -0
  160. package/dist/internal/webhook.d.ts.map +1 -0
  161. package/dist/internal/webhook.js +142 -0
  162. package/dist/internal/webhook.js.map +1 -0
  163. package/package.json +62 -1
  164. package/src/SlackChat.ts +640 -0
  165. package/src/SlackWebApi.ts +135 -0
  166. package/src/index.ts +18 -0
  167. package/src/internal/actions.ts +203 -0
  168. package/src/internal/adapter.ts +961 -0
  169. package/src/internal/address.ts +50 -0
  170. package/src/internal/emoji.ts +223 -0
  171. package/src/internal/emojiData.ts +1965 -0
  172. package/src/internal/errors.ts +192 -0
  173. package/src/internal/escape.ts +428 -0
  174. package/src/internal/interactions.ts +391 -0
  175. package/src/internal/json.ts +35 -0
  176. package/src/internal/length.ts +41 -0
  177. package/src/internal/mentions.ts +224 -0
  178. package/src/internal/normalize.ts +221 -0
  179. package/src/internal/options.ts +22 -0
  180. package/src/internal/richText.ts +367 -0
  181. package/src/internal/text.ts +252 -0
  182. package/src/internal/webhook.ts +199 -0
@@ -0,0 +1,50 @@
1
+ import * as ChatThread from "effect/chat/ChatThread"
2
+ import * as Option from "effect/Option"
3
+
4
+ /** @internal */
5
+ export const platform = "slack"
6
+
7
+ /** @internal */
8
+ export interface Parts {
9
+ readonly team: string
10
+ readonly channel: string
11
+ readonly threadTs?: string | undefined
12
+ }
13
+
14
+ /** @internal */
15
+ export const thread = (parts: Parts): ChatThread.ChatThread =>
16
+ ChatThread.make(
17
+ platform,
18
+ parts.threadTs === undefined
19
+ ? `${parts.team}/${parts.channel}`
20
+ : `${parts.team}/${parts.channel}/${parts.threadTs}`
21
+ )
22
+
23
+ /** @internal */
24
+ export const parts = (address: ChatThread.ChatThread): Option.Option<Parts> => {
25
+ if (address.platform !== platform) return Option.none()
26
+ const segments = address.id.split("/")
27
+ if (segments.length < 2 || segments.length > 3 || segments.some((segment) => segment.length === 0)) {
28
+ return Option.none()
29
+ }
30
+ return Option.some(
31
+ segments.length === 2
32
+ ? { team: segments[0], channel: segments[1] }
33
+ : { team: segments[0], channel: segments[1], threadTs: segments[2] }
34
+ )
35
+ }
36
+
37
+ /** @internal */
38
+ export const isDirectChannel = (channel: string): boolean => channel.startsWith("D")
39
+
40
+ /** Compares two Slack `ts` values in time order. @internal */
41
+ export const compareTs = (a: string, b: string): number => {
42
+ const [aSeconds, aMicros = ""] = a.split(".")
43
+ const [bSeconds, bMicros = ""] = b.split(".")
44
+ const seconds = Number(aSeconds) - Number(bSeconds)
45
+ if (seconds !== 0) return seconds < 0 ? -1 : 1
46
+ const width = Math.max(aMicros.length, bMicros.length)
47
+ const left = aMicros.padEnd(width, "0")
48
+ const right = bMicros.padEnd(width, "0")
49
+ return left < right ? -1 : left > right ? 1 : 0
50
+ }
@@ -0,0 +1,223 @@
1
+ import { emojiData } from "./emojiData.ts"
2
+
3
+ interface Entry {
4
+ readonly unified: string
5
+ /** skin_variations keyed by Slack tone digits: "3" or "23" */
6
+ readonly variations: ReadonlyMap<string, string>
7
+ }
8
+
9
+ interface Table {
10
+ readonly byName: ReadonlyMap<string, Entry>
11
+ /** Emoji string (unified or non_qualified, toned or not) to Slack name without colons */
12
+ readonly bySequence: ReadonlyMap<string, string>
13
+ /** First code point of a multi code point sequence to its UTF-16 lengths, longest first */
14
+ readonly lengthsByFirst: ReadonlyMap<number, ReadonlyArray<number>>
15
+ /** Cost of a lone surrogate, the highest cost of any single code point emoji */
16
+ readonly loneSurrogateCost: number
17
+ }
18
+
19
+ const decode = (sequence: string): string => {
20
+ const codePoints = sequence.split("-")
21
+ let out = ""
22
+ for (let i = 0; i < codePoints.length; i++) out += String.fromCodePoint(parseInt(codePoints[i], 16))
23
+ return out
24
+ }
25
+
26
+ const withoutVariationSelector = (emoji: string): string => emoji.replaceAll("\uFE0F", "")
27
+
28
+ const toneSuffix = (tones: string): string => {
29
+ let out = ""
30
+ for (let i = 0; i < tones.length; i++) out += "::skin-tone-" + tones[i]
31
+ return out
32
+ }
33
+
34
+ // Decoded by hand: optimised String.prototype.codePointAt returned wrong values
35
+ // around lone surrogates on Node 26.8 (V8), which broke blocksCost.
36
+ const codePointAt = (text: string, index: number): number => {
37
+ const high = text.charCodeAt(index)
38
+ if (high < 0xd800 || high > 0xdbff || index + 1 >= text.length) return high
39
+ const low = text.charCodeAt(index + 1)
40
+ return low < 0xdc00 || low > 0xdfff ? high : (high - 0xd800) * 0x400 + (low - 0xdc00) + 0x10000
41
+ }
42
+
43
+ const build = (): Table => {
44
+ const byName = new Map<string, Entry>()
45
+ const bySequence = new Map<string, string>()
46
+ const lengths = new Map<number, Set<number>>()
47
+ let loneSurrogateCost = 0
48
+
49
+ const addSequence = (emoji: string, name: string) => {
50
+ bySequence.set(emoji, name)
51
+ const first = codePointAt(emoji, 0)
52
+ if (emoji.length > (first > 0xffff ? 2 : 1)) {
53
+ let set = lengths.get(first)
54
+ if (set === undefined) lengths.set(first, set = new Set())
55
+ set.add(emoji.length)
56
+ } else {
57
+ loneSurrogateCost = Math.max(loneSurrogateCost, name.length + 2)
58
+ }
59
+ }
60
+
61
+ for (const line of emojiData.split("\n")) {
62
+ const [names, unifiedHex, nonQualified, variationGroups] = line.split("|")
63
+ const shortNames = names.split(",")
64
+ const shortName = shortNames[0]
65
+ const unified = decode(unifiedHex)
66
+ addSequence(unified, shortName)
67
+ if (nonQualified !== "") {
68
+ addSequence(nonQualified === "!" ? withoutVariationSelector(unified) : decode(nonQualified), shortName)
69
+ }
70
+ const variations = new Map<string, string>()
71
+ if (variationGroups !== undefined) {
72
+ for (const group of variationGroups.split(";")) {
73
+ const [keys, template, variationNonQualified] = group.split(":")
74
+ for (const tones of keys.split(",")) {
75
+ let index = 0
76
+ const variation = decode(
77
+ template.replace(/\*/g, () => (0x1f3fb + Number(tones[index++]) - 2).toString(16))
78
+ )
79
+ variations.set(tones, variation)
80
+ const name = shortName + toneSuffix(tones)
81
+ addSequence(variation, name)
82
+ if (variationNonQualified === "!") addSequence(withoutVariationSelector(variation), name)
83
+ }
84
+ }
85
+ }
86
+ const entry: Entry = { unified, variations }
87
+ for (const name of shortNames) byName.set(name, entry)
88
+ }
89
+
90
+ const lengthsByFirst = new Map<number, ReadonlyArray<number>>()
91
+ for (const [first, set] of lengths) lengthsByFirst.set(first, [...set].sort((a, b) => b - a))
92
+
93
+ return { byName, bySequence, lengthsByFirst, loneSurrogateCost }
94
+ }
95
+
96
+ let cached: Table | undefined
97
+ const table = (): Table => cached ??= build()
98
+
99
+ const SKIN_TONE = /^skin-tone-([2-6])$/
100
+
101
+ /**
102
+ * Resolve a Slack emoji name to its fully qualified emoji.
103
+ *
104
+ * Accepts any alias, with or without surrounding colons, followed by at most
105
+ * one `::skin-tone-N` suffix (`+1::skin-tone-3`). The toned emoji is the
106
+ * upstream `skin_variations` entry with that one tone, or, for a multi-person
107
+ * emoji upstream keys only by two tones (`people_holding_hands`), the entry
108
+ * with that tone twice. A tone the emoji does not have, and a name with two
109
+ * tones, yield `undefined`: no recording shows what Slack sends for a
110
+ * two-tone reaction.
111
+ *
112
+ * @internal
113
+ */
114
+ export const fromName = (name: string): string | undefined => {
115
+ const bare = name.length >= 2 && name.startsWith(":") && name.endsWith(":") ? name.slice(1, -1) : name
116
+ const parts = bare.split("::")
117
+ const entry = table().byName.get(parts[0])
118
+ if (entry === undefined) return undefined
119
+ if (parts.length === 1) return entry.unified
120
+ if (parts.length !== 2) return undefined
121
+ const match = SKIN_TONE.exec(parts[1])
122
+ if (match === null) return undefined
123
+ const tone = match[1]
124
+ return entry.variations.get(tone) ?? entry.variations.get(tone + tone)
125
+ }
126
+
127
+ /**
128
+ * Resolve an emoji (fully qualified or not, toned or not) to its primary Slack
129
+ * name without colons, for example `+1` or `+1::skin-tone-3`.
130
+ *
131
+ * @internal
132
+ */
133
+ export const toName = (emoji: string): string | undefined => table().bySequence.get(emoji)
134
+
135
+ /**
136
+ * The longest emoji in the table that starts at `index`, with its name and
137
+ * its length in UTF-16 units, or `undefined`.
138
+ *
139
+ * @internal
140
+ */
141
+ export const longestAt = (
142
+ text: string,
143
+ index: number
144
+ ): { readonly name: string; readonly length: number } | undefined => {
145
+ const { bySequence, lengthsByFirst } = table()
146
+ const codePoint = codePointAt(text, index)
147
+ for (const length of lengthsByFirst.get(codePoint) ?? []) {
148
+ const name = index + length <= text.length ? bySequence.get(text.slice(index, index + length)) : undefined
149
+ if (name !== undefined) return { name, length }
150
+ }
151
+ const width = codePoint > 0xffff ? 2 : 1
152
+ const name = bySequence.get(text.slice(index, index + width))
153
+ return name === undefined ? undefined : { name, length: width }
154
+ }
155
+
156
+ // Code points that are never an emoji on their own but join or modify emoji
157
+ // sequences. Charging them more than one keeps blocksCost subadditive: an
158
+ // emoji sequence split anywhere costs at least as much as the whole sequence.
159
+ const ZERO_WIDTH_JOINER = 0x200d
160
+ const COMBINING_ENCLOSING_KEYCAP = 0x20e3
161
+ const ZERO_WIDTH_JOINER_COST = 10
162
+ const COMBINING_ENCLOSING_KEYCAP_COST = 12
163
+ const REGIONAL_INDICATOR_COST = 6
164
+ // Slack charges a lone hair component 1 (the spec's four override rows), but
165
+ // with this split measure 1 would charge seven hair sequences below their
166
+ // names: 🧑‍🦱 would split as :adult: + joiner + 🦱 for 18 against Slack's 21.
167
+ // 4 is the least that keeps every hair sequence at its name.
168
+ const HAIR_COMPONENT_COST = 4
169
+
170
+ const codePointCost = (codePoint: number, loneSurrogateCost: number): number => {
171
+ if (codePoint === ZERO_WIDTH_JOINER) return ZERO_WIDTH_JOINER_COST
172
+ if (codePoint === COMBINING_ENCLOSING_KEYCAP) return COMBINING_ENCLOSING_KEYCAP_COST
173
+ if (codePoint >= 0x1f1e6 && codePoint <= 0x1f1ff) return REGIONAL_INDICATOR_COST
174
+ if (codePoint >= 0x1f9b0 && codePoint <= 0x1f9b3) return HAIR_COMPONENT_COST
175
+ if (codePoint >= 0xd800 && codePoint <= 0xdfff) return loneSurrogateCost
176
+ return 1
177
+ }
178
+
179
+ /**
180
+ * Measure text the way Slack counts it against the 13,200 character
181
+ * `msg_blocks_too_long` limit: every emoji counts as its `:short_name:` form
182
+ * (`👍🏽` is `:+1::skin-tone-4:`, 17), everything else as one per code point.
183
+ *
184
+ * The result is the cheapest split of the text into emoji and single code
185
+ * points. For every emoji in the table this is exactly its `:short_name:`
186
+ * length, and since the cheapest split of `a + b` is at most the cheapest split
187
+ * of `a` followed by that of `b`, `blocksCost(a + b) <= blocksCost(a) +
188
+ * blocksCost(b)` for all strings. To keep that true when a split lands inside an
189
+ * emoji, code points that only occur inside emoji sequences (zero width
190
+ * joiner, keycap, regional indicators, hair components) cost more than one
191
+ * when they stand alone, and so does a lone surrogate.
192
+ *
193
+ * Runs in O(text length × candidate sequence lengths).
194
+ *
195
+ * @internal
196
+ */
197
+ export const blocksCost = (text: string): number => {
198
+ const { bySequence, lengthsByFirst, loneSurrogateCost } = table()
199
+ const length = text.length
200
+ // costs[i] is the cheapest cost of text.slice(0, i), -1 when i is inside a code point
201
+ const costs = new Int32Array(length + 1).fill(-1)
202
+ costs[0] = 0
203
+ for (let i = 0; i < length; i++) {
204
+ const before = costs[i]
205
+ if (before < 0) continue
206
+ const codePoint = codePointAt(text, i)
207
+ const width = codePoint > 0xffff ? 2 : 1
208
+ const single = bySequence.get(text.slice(i, i + width))
209
+ const singleCost = before + (single === undefined ? codePointCost(codePoint, loneSurrogateCost) : single.length + 2)
210
+ if (costs[i + width] < 0 || singleCost < costs[i + width]) costs[i + width] = singleCost
211
+ const candidates = lengthsByFirst.get(codePoint)
212
+ if (candidates === undefined) continue
213
+ for (const candidate of candidates) {
214
+ const end = i + candidate
215
+ if (end > length) continue
216
+ const name = bySequence.get(text.slice(i, end))
217
+ if (name === undefined) continue
218
+ const cost = before + name.length + 2
219
+ if (costs[end] < 0 || cost < costs[end]) costs[end] = cost
220
+ }
221
+ }
222
+ return costs[length]
223
+ }