@effect/chat-whatsapp 0.0.0-stage → 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 +60 -2
- 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/WhatsAppChat.d.ts +373 -0
- package/dist/WhatsAppChat.d.ts.map +1 -0
- package/dist/WhatsAppChat.js +388 -0
- package/dist/WhatsAppChat.js.map +1 -0
- package/dist/WhatsAppGraphApi.d.ts +90 -0
- package/dist/WhatsAppGraphApi.d.ts.map +1 -0
- package/dist/WhatsAppGraphApi.js +97 -0
- package/dist/WhatsAppGraphApi.js.map +1 -0
- package/dist/WhatsAppUpdate.d.ts +168 -0
- package/dist/WhatsAppUpdate.d.ts.map +1 -0
- package/dist/WhatsAppUpdate.js +103 -0
- package/dist/WhatsAppUpdate.js.map +1 -0
- package/dist/index.d.ts +20 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +21 -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 +224 -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 +330 -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/errors.d.ts +2 -0
- package/dist/internal/errors.d.ts.map +1 -0
- package/dist/internal/errors.js +93 -0
- package/dist/internal/errors.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 +292 -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 +10 -0
- package/dist/internal/options.js.map +1 -0
- package/dist/internal/text.d.ts +11 -0
- package/dist/internal/text.d.ts.map +1 -0
- package/dist/internal/text.js +485 -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 +150 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +58 -3
- package/src/WhatsAppChat.ts +505 -0
- package/src/WhatsAppGraphApi.ts +147 -0
- package/src/WhatsAppUpdate.ts +142 -0
- package/src/index.ts +24 -0
- package/src/internal/actions.ts +220 -0
- package/src/internal/adapter.ts +284 -0
- package/src/internal/address.ts +41 -0
- package/src/internal/errors.ts +102 -0
- package/src/internal/normalize.ts +250 -0
- package/src/internal/options.ts +13 -0
- package/src/internal/text.ts +495 -0
- package/src/internal/webhook.ts +216 -0
|
@@ -0,0 +1,216 @@
|
|
|
1
|
+
import * as ByteSize from "effect/ByteSize"
|
|
2
|
+
import { Unsupported } from "effect/chat/ChatError"
|
|
3
|
+
import type * as ChatEvent from "effect/chat/ChatEvent"
|
|
4
|
+
import type { NotAccepted } from "effect/chat/ChatHandler"
|
|
5
|
+
import * as ChatInteraction from "effect/chat/ChatInteraction"
|
|
6
|
+
import * as Clock from "effect/Clock"
|
|
7
|
+
import * as Mac from "effect/crypto/Mac"
|
|
8
|
+
import * as Duration from "effect/Duration"
|
|
9
|
+
import * as Effect from "effect/Effect"
|
|
10
|
+
import * as Hex from "effect/encoding/Hex"
|
|
11
|
+
import * as Fiber from "effect/Fiber"
|
|
12
|
+
import * as HttpIncomingMessage from "effect/http/HttpIncomingMessage"
|
|
13
|
+
import type * as HttpServerRequest from "effect/http/HttpServerRequest"
|
|
14
|
+
import * as HttpServerResponse from "effect/http/HttpServerResponse"
|
|
15
|
+
import * as Option from "effect/Option"
|
|
16
|
+
import * as Redacted from "effect/Redacted"
|
|
17
|
+
import * as Result from "effect/Result"
|
|
18
|
+
import type { Inbound } from "../WhatsAppChat.ts"
|
|
19
|
+
import type { WhatsAppUpdate } from "../WhatsAppUpdate.ts"
|
|
20
|
+
import { chatError } from "./errors.ts"
|
|
21
|
+
import { groupMessages, isObject } from "./normalize.ts"
|
|
22
|
+
|
|
23
|
+
const encoder = new TextEncoder()
|
|
24
|
+
const decoder = new TextDecoder()
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Checks Meta's `X-Hub-Signature-256`: `sha256=` and the hex HMAC-SHA256
|
|
28
|
+
* of the raw body with the app secret.
|
|
29
|
+
*
|
|
30
|
+
* @internal
|
|
31
|
+
*/
|
|
32
|
+
export const makeVerifier = Effect.fnUntraced(function*(appSecret: Redacted.Redacted) {
|
|
33
|
+
const mac = yield* Mac.Mac
|
|
34
|
+
// An invalid app secret is a configuration defect, not a route error.
|
|
35
|
+
const key = yield* Mac.key(Mac.hmacSha256, encoder.encode(Redacted.value(appSecret))).pipe(Effect.orDie)
|
|
36
|
+
return (headers: { readonly [key: string]: string | undefined }, body: Uint8Array): Effect.Effect<boolean> => {
|
|
37
|
+
const signature = headers["x-hub-signature-256"]
|
|
38
|
+
if (signature === undefined || !/^sha256=(?:[0-9a-fA-F]{2})+$/.test(signature)) return Effect.succeed(false)
|
|
39
|
+
const tag = Hex.decode(signature.slice(7))
|
|
40
|
+
if (Result.isFailure(tag)) return Effect.succeed(false)
|
|
41
|
+
// Crypto failures fail closed, like a mismatched signature.
|
|
42
|
+
return mac.verify(key, tag.success, body).pipe(Effect.catch(() => Effect.succeed(false)))
|
|
43
|
+
}
|
|
44
|
+
})
|
|
45
|
+
|
|
46
|
+
const ok = HttpServerResponse.empty({ status: 200 })
|
|
47
|
+
const status = (code: number) => HttpServerResponse.empty({ status: code })
|
|
48
|
+
|
|
49
|
+
/** What is logged with each 503, and with a dropped delivery. @internal */
|
|
50
|
+
export const logged = {
|
|
51
|
+
notAccepted: "whatsapp webhook: an update was refused, answered 503",
|
|
52
|
+
deadline: "whatsapp webhook: the request passed its deadline, answered 503",
|
|
53
|
+
noHandler: "whatsapp webhook: a click arrived with no ChatInteraction.Handler, dropped",
|
|
54
|
+
group: "whatsapp webhook: group messages are not supported, dropped"
|
|
55
|
+
} as const
|
|
56
|
+
|
|
57
|
+
/**
|
|
58
|
+
* Meta's subscription check: `hub.mode=subscribe` with the verify token is
|
|
59
|
+
* answered with `hub.challenge`, anything else 403.
|
|
60
|
+
*
|
|
61
|
+
* @internal
|
|
62
|
+
*/
|
|
63
|
+
export const handleVerification = (
|
|
64
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
65
|
+
verifyToken: Redacted.Redacted
|
|
66
|
+
): HttpServerResponse.HttpServerResponse => {
|
|
67
|
+
const params = new URL(request.url, "http://localhost").searchParams
|
|
68
|
+
const challenge = params.get("hub.challenge")
|
|
69
|
+
return params.get("hub.mode") === "subscribe" && params.get("hub.verify_token") === Redacted.value(verifyToken) &&
|
|
70
|
+
challenge !== null
|
|
71
|
+
? HttpServerResponse.text(challenge)
|
|
72
|
+
: status(403)
|
|
73
|
+
}
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
export interface ReceiveOptions {
|
|
77
|
+
readonly handle: (event: ChatEvent.ChatEvent) => Effect.Effect<void, NotAccepted>
|
|
78
|
+
readonly onUpdate?: ((update: WhatsAppUpdate) => Effect.Effect<void, NotAccepted>) | undefined
|
|
79
|
+
/** How long the click about to run may take. */
|
|
80
|
+
readonly clickDeadline: Effect.Effect<Duration.Duration>
|
|
81
|
+
}
|
|
82
|
+
|
|
83
|
+
/**
|
|
84
|
+
* Runs one delivery's updates in order. A `NotAccepted` stops the rest.
|
|
85
|
+
* A click never fails: `Ack` and `Late` count as handled, and
|
|
86
|
+
* `OpenModal` is refused with `Unsupported("modals")`.
|
|
87
|
+
*
|
|
88
|
+
* @internal
|
|
89
|
+
*/
|
|
90
|
+
export const receive = (
|
|
91
|
+
items: ReadonlyArray<Inbound>,
|
|
92
|
+
options: ReceiveOptions
|
|
93
|
+
): Effect.Effect<void, NotAccepted> =>
|
|
94
|
+
Effect.forEach(items, (item): Effect.Effect<void, NotAccepted> => {
|
|
95
|
+
switch (item._tag) {
|
|
96
|
+
case "MessageCreated":
|
|
97
|
+
case "ReactionAdded":
|
|
98
|
+
return options.handle(item)
|
|
99
|
+
case "Status":
|
|
100
|
+
case "UserIdChanged":
|
|
101
|
+
case "Alias":
|
|
102
|
+
return options.onUpdate === undefined ? Effect.void : options.onUpdate(item)
|
|
103
|
+
case "ButtonClicked":
|
|
104
|
+
case "SelectChanged":
|
|
105
|
+
return Effect.flatMap(Effect.serviceOption(ChatInteraction.Handler), (handler) => {
|
|
106
|
+
if (Option.isNone(handler)) return Effect.logWarning(logged.noHandler)
|
|
107
|
+
return options.clickDeadline.pipe(
|
|
108
|
+
Effect.flatMap((deadline) => ChatInteraction.handle(item, deadline)),
|
|
109
|
+
Effect.provideService(ChatInteraction.Handler, handler.value),
|
|
110
|
+
Effect.flatMap((reply) =>
|
|
111
|
+
reply._tag === "OpenModal"
|
|
112
|
+
// Awaited only for what is left of the click's share, then left running.
|
|
113
|
+
? Effect.forkDetach(
|
|
114
|
+
handler.value.refused(item, chatError("openModal", new Unsupported({ capability: "modals" })))
|
|
115
|
+
).pipe(
|
|
116
|
+
Effect.flatMap((fiber) =>
|
|
117
|
+
Effect.flatMap(options.clickDeadline, (left) => Effect.timeoutOption(Fiber.await(fiber), left))
|
|
118
|
+
),
|
|
119
|
+
Effect.asVoid
|
|
120
|
+
)
|
|
121
|
+
: Effect.void
|
|
122
|
+
)
|
|
123
|
+
)
|
|
124
|
+
})
|
|
125
|
+
}
|
|
126
|
+
}, { discard: true })
|
|
127
|
+
|
|
128
|
+
/** @internal */
|
|
129
|
+
export interface Options {
|
|
130
|
+
readonly verify: (headers: { readonly [key: string]: string | undefined }, body: Uint8Array) => Effect.Effect<boolean>
|
|
131
|
+
readonly normalize: (envelope: unknown) => ReadonlyArray<Inbound>
|
|
132
|
+
readonly handle: ReceiveOptions["handle"]
|
|
133
|
+
readonly onUpdate?: ReceiveOptions["onUpdate"]
|
|
134
|
+
readonly deadline: Duration.Duration
|
|
135
|
+
readonly maxBodySize: ByteSize.ByteSize
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
/** What a click leaves of the deadline for the rest of the request. */
|
|
139
|
+
const clickMargin = 250
|
|
140
|
+
|
|
141
|
+
/**
|
|
142
|
+
* One webhook POST.
|
|
143
|
+
*
|
|
144
|
+
* - 413 past the body limit, 400 for a body that can't be read, then 401
|
|
145
|
+
* for a bad signature and 400 for a body that is not a JSON object. Another `object` than
|
|
146
|
+
* `whatsapp_business_account` is answered 200 and dropped.
|
|
147
|
+
* - 503 when an update fails `NotAccepted`, or the whole request takes
|
|
148
|
+
* longer than the deadline. Meta then delivers the batch again.
|
|
149
|
+
* - A click gets what is left of the deadline, less a margin, so a slow
|
|
150
|
+
* click handler is detached rather than passing the deadline.
|
|
151
|
+
* - At the deadline the work is interrupted without waiting for its
|
|
152
|
+
* finalizers, so a slow release can't hold the 503 past Meta's cutoff.
|
|
153
|
+
*
|
|
154
|
+
* @internal
|
|
155
|
+
*/
|
|
156
|
+
export const handleRequest = (
|
|
157
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
158
|
+
options: Options
|
|
159
|
+
): Effect.Effect<HttpServerResponse.HttpServerResponse> =>
|
|
160
|
+
// Masked, so a cancellation can't land between forking the worker and
|
|
161
|
+
// arranging its interruption; only the wait itself is interruptible.
|
|
162
|
+
Effect.uninterruptibleMask((restore) =>
|
|
163
|
+
Effect.gen(function*() {
|
|
164
|
+
const fiber = yield* Effect.forkDetach(work(request, options))
|
|
165
|
+
// Interrupted without being awaited, here and at the deadline, so no finalizer delays the answer.
|
|
166
|
+
const stop = Effect.asVoid(Effect.forkDetach(Fiber.interrupt(fiber)))
|
|
167
|
+
const response = yield* Effect.onInterrupt(
|
|
168
|
+
restore(Effect.timeoutOption(Fiber.join(fiber), options.deadline)),
|
|
169
|
+
() => stop
|
|
170
|
+
)
|
|
171
|
+
if (Option.isSome(response)) return response.value
|
|
172
|
+
yield* stop
|
|
173
|
+
yield* Effect.logWarning(logged.deadline)
|
|
174
|
+
return status(503)
|
|
175
|
+
})
|
|
176
|
+
)
|
|
177
|
+
|
|
178
|
+
const work = (
|
|
179
|
+
request: HttpServerRequest.HttpServerRequest,
|
|
180
|
+
options: Options
|
|
181
|
+
): Effect.Effect<HttpServerResponse.HttpServerResponse> =>
|
|
182
|
+
Effect.gen(function*() {
|
|
183
|
+
const until = (yield* Clock.currentTimeMillis) + Duration.toMillis(options.deadline)
|
|
184
|
+
const read = yield* request.arrayBuffer.pipe(
|
|
185
|
+
Effect.provideService(HttpIncomingMessage.MaxBodySize, options.maxBodySize),
|
|
186
|
+
Effect.result
|
|
187
|
+
)
|
|
188
|
+
if (Result.isFailure(read)) {
|
|
189
|
+
const { reason } = read.failure
|
|
190
|
+
return status(reason._tag === "RequestParseError" && reason.reason === "BodyTooLarge" ? 413 : 400)
|
|
191
|
+
}
|
|
192
|
+
// Measured again: the request caches its body, so one read earlier under a larger limit comes back whole.
|
|
193
|
+
if (read.success.byteLength > ByteSize.toNumberUnsafe(options.maxBodySize)) return status(413)
|
|
194
|
+
const body = new Uint8Array(read.success)
|
|
195
|
+
if (!(yield* options.verify(request.headers, body))) return status(401)
|
|
196
|
+
let envelope: unknown
|
|
197
|
+
try {
|
|
198
|
+
envelope = JSON.parse(decoder.decode(body))
|
|
199
|
+
} catch {
|
|
200
|
+
return status(400)
|
|
201
|
+
}
|
|
202
|
+
if (!isObject(envelope)) return status(400)
|
|
203
|
+
if (envelope.object !== "whatsapp_business_account") return ok
|
|
204
|
+
if (groupMessages(envelope) > 0) yield* Effect.logDebug(logged.group)
|
|
205
|
+
return yield* receive(options.normalize(envelope), {
|
|
206
|
+
handle: options.handle,
|
|
207
|
+
onUpdate: options.onUpdate,
|
|
208
|
+
clickDeadline: Effect.map(
|
|
209
|
+
Clock.currentTimeMillis,
|
|
210
|
+
(now) => Duration.millis(Math.max(0, until - now - clickMargin))
|
|
211
|
+
)
|
|
212
|
+
}).pipe(
|
|
213
|
+
Effect.as(ok),
|
|
214
|
+
Effect.catchTag("NotAccepted", () => Effect.as(Effect.logWarning(logged.notAccepted), status(503)))
|
|
215
|
+
)
|
|
216
|
+
})
|