@effect/chat-teams 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 +115 -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/TeamsChat.d.ts +318 -0
- package/dist/TeamsChat.d.ts.map +1 -0
- package/dist/TeamsChat.js +316 -0
- package/dist/TeamsChat.js.map +1 -0
- package/dist/index.d.ts +10 -0
- package/dist/index.d.ts.map +1 -0
- package/dist/index.js +11 -0
- package/dist/index.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 +300 -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 +52 -0
- package/dist/internal/address.js.map +1 -0
- package/dist/internal/auth.d.ts +2 -0
- package/dist/internal/auth.d.ts.map +1 -0
- package/dist/internal/auth.js +142 -0
- package/dist/internal/auth.js.map +1 -0
- package/dist/internal/connector.d.ts +2 -0
- package/dist/internal/connector.d.ts.map +1 -0
- package/dist/internal/connector.js +173 -0
- package/dist/internal/connector.js.map +1 -0
- package/dist/internal/download.d.ts +5 -0
- package/dist/internal/download.d.ts.map +1 -0
- package/dist/internal/download.js +183 -0
- package/dist/internal/download.js.map +1 -0
- package/dist/internal/envelope.d.ts +2 -0
- package/dist/internal/envelope.d.ts.map +1 -0
- package/dist/internal/envelope.js +337 -0
- package/dist/internal/envelope.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 +113 -0
- package/dist/internal/errors.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 +288 -0
- package/dist/internal/interactions.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 +147 -0
- package/dist/internal/length.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 +224 -0
- package/dist/internal/normalize.js.map +1 -0
- package/dist/internal/parse.d.ts +2 -0
- package/dist/internal/parse.d.ts.map +1 -0
- package/dist/internal/parse.js +33 -0
- package/dist/internal/parse.js.map +1 -0
- package/dist/internal/render.d.ts +2 -0
- package/dist/internal/render.d.ts.map +1 -0
- package/dist/internal/render.js +316 -0
- package/dist/internal/render.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 +88 -0
- package/dist/internal/webhook.js.map +1 -0
- package/package.json +58 -3
- package/src/TeamsChat.ts +470 -0
- package/src/index.ts +12 -0
- package/src/internal/adapter.ts +351 -0
- package/src/internal/address.ts +67 -0
- package/src/internal/auth.ts +192 -0
- package/src/internal/connector.ts +253 -0
- package/src/internal/download.ts +277 -0
- package/src/internal/envelope.ts +427 -0
- package/src/internal/errors.ts +94 -0
- package/src/internal/interactions.ts +294 -0
- package/src/internal/length.ts +170 -0
- package/src/internal/normalize.ts +232 -0
- package/src/internal/parse.ts +41 -0
- package/src/internal/render.ts +318 -0
- package/src/internal/webhook.ts +117 -0
|
@@ -0,0 +1,427 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* The envelope a button or dialog carries in its `effect` data key: a compact
|
|
3
|
+
* HS256 JWT bound to the bot, the tenant and the conversation. A dialog token
|
|
4
|
+
* also binds the user it opened for and holds the modal's definition, so a
|
|
5
|
+
* submit is checked against what was signed.
|
|
6
|
+
*/
|
|
7
|
+
import type { NonEmptyReadonlyArray } from "effect/Array"
|
|
8
|
+
import type { ChatErrorReason } from "effect/chat/ChatError"
|
|
9
|
+
import type * as ChatUi from "effect/chat/ChatUi"
|
|
10
|
+
import * as Clock from "effect/Clock"
|
|
11
|
+
import * as Digest from "effect/crypto/Digest"
|
|
12
|
+
import * as Mac from "effect/crypto/Mac"
|
|
13
|
+
import * as Duration from "effect/Duration"
|
|
14
|
+
import * as Effect from "effect/Effect"
|
|
15
|
+
import * as Base64Url from "effect/encoding/Base64Url"
|
|
16
|
+
import * as Jwk from "effect/jose/Jwk"
|
|
17
|
+
import * as Jwt from "effect/jose/Jwt"
|
|
18
|
+
import * as Option from "effect/Option"
|
|
19
|
+
import * as Predicate from "effect/Predicate"
|
|
20
|
+
import * as Redacted from "effect/Redacted"
|
|
21
|
+
import * as Schema from "effect/Schema"
|
|
22
|
+
import { baseConversation, maxIdLength } from "./address.ts"
|
|
23
|
+
import { invalidRequest, tooLarge } from "./errors.ts"
|
|
24
|
+
|
|
25
|
+
/** The data key that holds the token. A modal field may not use it. @internal */
|
|
26
|
+
export const reservedKey = "effect"
|
|
27
|
+
|
|
28
|
+
/**
|
|
29
|
+
* Bounds on what a token holds, in UTF-16 code units. `definition` bounds the
|
|
30
|
+
* UTF-8 bytes of a modal's JSON without its metadata.
|
|
31
|
+
*
|
|
32
|
+
* @internal
|
|
33
|
+
*/
|
|
34
|
+
export const limits = {
|
|
35
|
+
id: maxIdLength,
|
|
36
|
+
kid: 16,
|
|
37
|
+
buttonValue: 2_000,
|
|
38
|
+
metadata: 2_800,
|
|
39
|
+
text: 4_000,
|
|
40
|
+
fields: 100,
|
|
41
|
+
options: 100,
|
|
42
|
+
definition: 16_000
|
|
43
|
+
} as const
|
|
44
|
+
|
|
45
|
+
/** The default max length of a text input. @internal */
|
|
46
|
+
export const defaultMaxLength = limits.text
|
|
47
|
+
|
|
48
|
+
/** How long a dialog token is good for when the caller gives no ttl. @internal */
|
|
49
|
+
export const defaultDialogTtl = Duration.hours(1)
|
|
50
|
+
|
|
51
|
+
const typ = { button: "teams-button", dialog: "teams-dialog" } as const
|
|
52
|
+
|
|
53
|
+
// Schemas
|
|
54
|
+
|
|
55
|
+
const bounded = (max: number) => Schema.String.check(Schema.isMaxLength(max))
|
|
56
|
+
const Id = bounded(limits.id)
|
|
57
|
+
const Text = bounded(limits.text)
|
|
58
|
+
const Hash = Schema.String.check(Schema.isBetweenLength(43, 43))
|
|
59
|
+
const Length = Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: limits.text }))
|
|
60
|
+
|
|
61
|
+
const SelectOption = Schema.Struct({ label: Text, value: Text })
|
|
62
|
+
const Options = Schema.Array(SelectOption).check(Schema.isMaxLength(limits.options))
|
|
63
|
+
|
|
64
|
+
const Definition = Schema.TaggedStruct("Modal", {
|
|
65
|
+
id: Id,
|
|
66
|
+
title: Text,
|
|
67
|
+
submitLabel: Schema.optionalKey(Text),
|
|
68
|
+
elements: Schema.Array(Schema.Union([
|
|
69
|
+
Schema.TaggedStruct("TextInput", {
|
|
70
|
+
id: Id,
|
|
71
|
+
label: Text,
|
|
72
|
+
multiline: Schema.optionalKey(Schema.Boolean),
|
|
73
|
+
placeholder: Schema.optionalKey(Text),
|
|
74
|
+
initial: Schema.optionalKey(Text),
|
|
75
|
+
optional: Schema.optionalKey(Schema.Boolean),
|
|
76
|
+
minLength: Schema.optionalKey(Length),
|
|
77
|
+
maxLength: Schema.optionalKey(Length)
|
|
78
|
+
}),
|
|
79
|
+
Schema.TaggedStruct("SelectInput", {
|
|
80
|
+
id: Id,
|
|
81
|
+
label: Text,
|
|
82
|
+
placeholder: Schema.optionalKey(Text),
|
|
83
|
+
options: Options,
|
|
84
|
+
initial: Schema.optionalKey(Text),
|
|
85
|
+
optional: Schema.optionalKey(Schema.Boolean)
|
|
86
|
+
}),
|
|
87
|
+
Schema.TaggedStruct("Text", { text: Text })
|
|
88
|
+
])).check(Schema.isMaxLength(limits.fields))
|
|
89
|
+
})
|
|
90
|
+
|
|
91
|
+
const ButtonClaims = Schema.Struct({
|
|
92
|
+
aud: Id,
|
|
93
|
+
tid: Id,
|
|
94
|
+
cnv: Hash,
|
|
95
|
+
id: Id,
|
|
96
|
+
val: Schema.optionalKey(bounded(limits.buttonValue))
|
|
97
|
+
})
|
|
98
|
+
|
|
99
|
+
const DialogClaims = Schema.Struct({
|
|
100
|
+
aud: Id,
|
|
101
|
+
tid: Id,
|
|
102
|
+
cnv: Hash,
|
|
103
|
+
cid: Id,
|
|
104
|
+
sub: Id,
|
|
105
|
+
exp: Schema.Int.check(Schema.isBetween({ minimum: 0, maximum: Number.MAX_SAFE_INTEGER })),
|
|
106
|
+
mid: Schema.optionalKey(Id),
|
|
107
|
+
def: Definition,
|
|
108
|
+
md: Schema.optionalKey(bounded(limits.metadata))
|
|
109
|
+
})
|
|
110
|
+
|
|
111
|
+
// Lengths
|
|
112
|
+
|
|
113
|
+
const encoder = new TextEncoder()
|
|
114
|
+
const utf8Length = (text: string): number => encoder.encode(text).length
|
|
115
|
+
const base64Length = (bytes: number): number => Math.ceil(bytes * 4 / 3)
|
|
116
|
+
/** A string of `n` code units is at most 6 bytes each in JSON, plus quotes. */
|
|
117
|
+
const jsonString = (n: number): number => 6 * n + 2
|
|
118
|
+
/** An HS256 signature is 32 bytes. */
|
|
119
|
+
const signatureLength = base64Length(32)
|
|
120
|
+
/** The conversation hash: base64url of a SHA-256 digest. */
|
|
121
|
+
const hashLength = base64Length(32)
|
|
122
|
+
|
|
123
|
+
const headerJson = (kid: string, type: string): string => JSON.stringify({ alg: "HS256", kid, typ: type })
|
|
124
|
+
|
|
125
|
+
/**
|
|
126
|
+
* The longest token `signButton` or `signDialog` can produce. A longer value
|
|
127
|
+
* is refused before it is parsed.
|
|
128
|
+
*
|
|
129
|
+
* @internal
|
|
130
|
+
*/
|
|
131
|
+
export const maxTokenLength: number = (() => {
|
|
132
|
+
const header = utf8Length(headerJson("", typ.dialog)) + limits.kid
|
|
133
|
+
const button = utf8Length(JSON.stringify({ aud: "", tid: "", cnv: "", id: "", val: "" })) - 2 * 5 +
|
|
134
|
+
3 * jsonString(limits.id) + jsonString(hashLength) + jsonString(limits.buttonValue)
|
|
135
|
+
const dialog =
|
|
136
|
+
utf8Length(JSON.stringify({ aud: "", tid: "", cnv: "", cid: "", sub: "", exp: 0, mid: "", def: 0, md: "" })) -
|
|
137
|
+
2 * 7 - 2 + 5 * jsonString(limits.id) + jsonString(hashLength) + String(Number.MAX_SAFE_INTEGER).length +
|
|
138
|
+
limits.definition + jsonString(limits.metadata)
|
|
139
|
+
return base64Length(header) + 1 + base64Length(Math.max(button, dialog)) + 1 + signatureLength
|
|
140
|
+
})()
|
|
141
|
+
|
|
142
|
+
/** @internal */
|
|
143
|
+
export interface ButtonContext {
|
|
144
|
+
readonly appId: string
|
|
145
|
+
readonly tenantId: string
|
|
146
|
+
/** The kid of the signing key. */
|
|
147
|
+
readonly kid: string
|
|
148
|
+
}
|
|
149
|
+
|
|
150
|
+
const buttonClaims = (
|
|
151
|
+
context: Omit<ButtonContext, "kid">,
|
|
152
|
+
cnv: string,
|
|
153
|
+
button: { readonly id: string; readonly value?: string | undefined }
|
|
154
|
+
): typeof ButtonClaims.Type => ({
|
|
155
|
+
aud: context.appId,
|
|
156
|
+
tid: context.tenantId,
|
|
157
|
+
cnv,
|
|
158
|
+
id: button.id,
|
|
159
|
+
...(button.value === undefined ? {} : { val: button.value })
|
|
160
|
+
})
|
|
161
|
+
|
|
162
|
+
/**
|
|
163
|
+
* The exact length of the token `signButton` produces for `button`, without
|
|
164
|
+
* signing it. The header and claims are deterministic JSON and the hash and
|
|
165
|
+
* signature have fixed lengths.
|
|
166
|
+
*
|
|
167
|
+
* @internal
|
|
168
|
+
*/
|
|
169
|
+
export const buttonTokenLength = (
|
|
170
|
+
context: ButtonContext,
|
|
171
|
+
button: { readonly id: string; readonly value?: string | undefined }
|
|
172
|
+
): number =>
|
|
173
|
+
base64Length(utf8Length(headerJson(context.kid, typ.button))) + 1 +
|
|
174
|
+
base64Length(utf8Length(JSON.stringify(buttonClaims(context, "x".repeat(hashLength), button)))) + 1 +
|
|
175
|
+
signatureLength
|
|
176
|
+
|
|
177
|
+
// Checks
|
|
178
|
+
|
|
179
|
+
/** Whether a field of `modal` uses the reserved `effect` id. @internal */
|
|
180
|
+
export const hasReservedField = (modal: ChatUi.Modal): boolean =>
|
|
181
|
+
modal.elements.some((element) => element._tag !== "Text" && element.id === reservedKey)
|
|
182
|
+
|
|
183
|
+
const definitionOf = (modal: ChatUi.Modal): typeof Definition.Type => {
|
|
184
|
+
const { metadata: _, ...definition } = modal
|
|
185
|
+
return definition
|
|
186
|
+
}
|
|
187
|
+
|
|
188
|
+
const isDefinition = Schema.is(Definition)
|
|
189
|
+
|
|
190
|
+
/**
|
|
191
|
+
* Why `modal` cannot go in a dialog token, if it can't: a field with the
|
|
192
|
+
* reserved id, or more than the limits allow.
|
|
193
|
+
*
|
|
194
|
+
* @internal
|
|
195
|
+
*/
|
|
196
|
+
export const checkModal = (modal: ChatUi.Modal): ChatErrorReason | undefined => {
|
|
197
|
+
if (hasReservedField(modal)) return invalidRequest("reserved_field_id")
|
|
198
|
+
if (modal.metadata !== undefined && modal.metadata.length > limits.metadata) {
|
|
199
|
+
return tooLarge(undefined, "modal_metadata_too_long")
|
|
200
|
+
}
|
|
201
|
+
const definition = definitionOf(modal)
|
|
202
|
+
if (!isDefinition(definition) || utf8Length(JSON.stringify(definition)) > limits.definition) {
|
|
203
|
+
return tooLarge(undefined, "modal_too_large")
|
|
204
|
+
}
|
|
205
|
+
return undefined
|
|
206
|
+
}
|
|
207
|
+
|
|
208
|
+
/** Why `button` cannot go in a button token, if it can't. @internal */
|
|
209
|
+
export const checkButton = (
|
|
210
|
+
button: { readonly id: string; readonly value?: string | undefined }
|
|
211
|
+
): ChatErrorReason | undefined =>
|
|
212
|
+
button.id.length > limits.id
|
|
213
|
+
? tooLarge(undefined, "action_id_too_long")
|
|
214
|
+
: button.value !== undefined && button.value.length > limits.buttonValue
|
|
215
|
+
? tooLarge(undefined, "action_data_too_long")
|
|
216
|
+
: undefined
|
|
217
|
+
|
|
218
|
+
/**
|
|
219
|
+
* The values of a dialog submit, checked against the signed definition, or
|
|
220
|
+
* nothing when they don't match it.
|
|
221
|
+
*
|
|
222
|
+
* - Keys are the ids of the inputs. The reserved `effect` key is skipped.
|
|
223
|
+
* - Every value is a string. An optional input left empty has no key.
|
|
224
|
+
* - A text input is held to its `minLength` and `maxLength` (default 4,000),
|
|
225
|
+
* and a select to its options.
|
|
226
|
+
*
|
|
227
|
+
* @internal
|
|
228
|
+
*/
|
|
229
|
+
export const validateSubmission = (
|
|
230
|
+
definition: ChatUi.Modal,
|
|
231
|
+
values: unknown
|
|
232
|
+
): Option.Option<Record<string, string>> => {
|
|
233
|
+
if (!Predicate.isObject(values)) return Option.none()
|
|
234
|
+
const input = values
|
|
235
|
+
const fields = new Map<string, ChatUi.TextInput | ChatUi.SelectInput>()
|
|
236
|
+
for (const element of definition.elements) {
|
|
237
|
+
if (element._tag !== "Text") fields.set(element.id, element)
|
|
238
|
+
}
|
|
239
|
+
for (const key of Object.keys(input)) {
|
|
240
|
+
if (key !== reservedKey && !fields.has(key)) return Option.none()
|
|
241
|
+
}
|
|
242
|
+
const out: Record<string, string> = {}
|
|
243
|
+
for (const [id, field] of fields) {
|
|
244
|
+
const value = Object.hasOwn(input, id) ? input[id] : undefined
|
|
245
|
+
if (value !== undefined && typeof value !== "string") return Option.none()
|
|
246
|
+
if (value === undefined || value === "") {
|
|
247
|
+
if (field.optional === true) continue
|
|
248
|
+
return Option.none()
|
|
249
|
+
}
|
|
250
|
+
if (field._tag === "TextInput") {
|
|
251
|
+
if (value.length < (field.minLength ?? 0) || value.length > (field.maxLength ?? defaultMaxLength)) {
|
|
252
|
+
return Option.none()
|
|
253
|
+
}
|
|
254
|
+
} else if (!field.options.some((option) => option.value === value)) {
|
|
255
|
+
return Option.none()
|
|
256
|
+
}
|
|
257
|
+
Object.defineProperty(out, id, { value, enumerable: true, writable: true, configurable: true })
|
|
258
|
+
}
|
|
259
|
+
return Option.some(out)
|
|
260
|
+
}
|
|
261
|
+
|
|
262
|
+
// Envelope
|
|
263
|
+
|
|
264
|
+
/** @internal */
|
|
265
|
+
export interface Button {
|
|
266
|
+
readonly id: string
|
|
267
|
+
readonly value?: string | undefined
|
|
268
|
+
}
|
|
269
|
+
|
|
270
|
+
/** @internal */
|
|
271
|
+
export interface Dialog {
|
|
272
|
+
/**
|
|
273
|
+
* The conversation id the click came from, as Teams sent it. A submit
|
|
274
|
+
* must come from the same one.
|
|
275
|
+
*/
|
|
276
|
+
readonly conversationId: string
|
|
277
|
+
/** The message the click came from. Absent when a command opened it. */
|
|
278
|
+
readonly messageId?: string | undefined
|
|
279
|
+
readonly modal: ChatUi.Modal
|
|
280
|
+
}
|
|
281
|
+
|
|
282
|
+
/** @internal */
|
|
283
|
+
export interface Envelope {
|
|
284
|
+
/** Fails with the reason `button` is over the limits. */
|
|
285
|
+
readonly signButton: (conversationId: string, button: Button) => Effect.Effect<string, ChatErrorReason>
|
|
286
|
+
/** The button the token was signed for, if it was signed for this conversation. */
|
|
287
|
+
readonly verifyButton: (
|
|
288
|
+
token: unknown,
|
|
289
|
+
activity: { readonly conversationId: string }
|
|
290
|
+
) => Effect.Effect<Option.Option<Button>>
|
|
291
|
+
/** Fails with the reason the modal or an id is refused. */
|
|
292
|
+
readonly signDialog: (
|
|
293
|
+
dialog: Dialog & { readonly userId: string; readonly ttl?: Duration.Input | undefined }
|
|
294
|
+
) => Effect.Effect<string, ChatErrorReason>
|
|
295
|
+
/**
|
|
296
|
+
* The dialog the token was signed for, if it is unexpired, for this user,
|
|
297
|
+
* and from exactly this conversation id.
|
|
298
|
+
*/
|
|
299
|
+
readonly verifyDialog: (
|
|
300
|
+
token: unknown,
|
|
301
|
+
activity: { readonly conversationId: string; readonly userId: string }
|
|
302
|
+
) => Effect.Effect<Option.Option<Dialog>>
|
|
303
|
+
/** `buttonTokenLength` with this envelope's app, tenant and signing key. */
|
|
304
|
+
readonly tokenLength: (button: Button) => number
|
|
305
|
+
}
|
|
306
|
+
|
|
307
|
+
const validKid = /^[A-Za-z0-9_-]{1,16}$/
|
|
308
|
+
|
|
309
|
+
/**
|
|
310
|
+
* Builds the envelope. The first key signs, and a token is verified with the
|
|
311
|
+
* key its `kid` names. A key without a valid `kid`, a repeated `kid` or
|
|
312
|
+
* a key under 32 bytes is a defect.
|
|
313
|
+
*
|
|
314
|
+
* @internal
|
|
315
|
+
*/
|
|
316
|
+
export const make = Effect.fnUntraced(function*(options: {
|
|
317
|
+
readonly appId: string
|
|
318
|
+
readonly tenantId: string
|
|
319
|
+
readonly keys: NonEmptyReadonlyArray<Redacted.Redacted<Jwk.SecretJwk>>
|
|
320
|
+
}): Effect.fn.Return<Envelope, never, Mac.Mac | Digest.Digest> {
|
|
321
|
+
const keys = new Map<string, Jwk.SecretKey<"HS256">>()
|
|
322
|
+
for (const input of options.keys) {
|
|
323
|
+
const kid = Redacted.value(input).kid
|
|
324
|
+
if (kid === undefined || !validKid.test(kid)) {
|
|
325
|
+
return yield* Effect.die(new Error("Each Teams interaction key needs a kid of 1 to 16 [A-Za-z0-9_-]"))
|
|
326
|
+
}
|
|
327
|
+
if (keys.has(kid)) return yield* Effect.die(new Error(`The Teams interaction key kid "${kid}" is repeated`))
|
|
328
|
+
keys.set(kid, yield* Effect.orDie(Jwk.importSecret(input, "HS256")))
|
|
329
|
+
}
|
|
330
|
+
const signing = keys.get(Redacted.value(options.keys[0]).kid!)!
|
|
331
|
+
const context: ButtonContext = { appId: options.appId, tenantId: options.tenantId, kid: signing.kid! }
|
|
332
|
+
const mac = yield* Mac.Mac
|
|
333
|
+
const digest = yield* Digest.Digest
|
|
334
|
+
|
|
335
|
+
const hash = (conversationId: string): Effect.Effect<string> =>
|
|
336
|
+
Effect.map(
|
|
337
|
+
digest.digest(Digest.sha256, encoder.encode(baseConversation(conversationId))),
|
|
338
|
+
(bytes) => Base64Url.encode(bytes)
|
|
339
|
+
)
|
|
340
|
+
|
|
341
|
+
const sign = <S extends Schema.Constraint>(schema: S, claims: S["Type"], type: string) =>
|
|
342
|
+
Jwt.sign(schema, claims, signing, { typ: type }).pipe(
|
|
343
|
+
Effect.map(Redacted.value),
|
|
344
|
+
Effect.provideService(Mac.Mac, mac),
|
|
345
|
+
Effect.orDie
|
|
346
|
+
)
|
|
347
|
+
|
|
348
|
+
const verify = <A>(
|
|
349
|
+
schema: Schema.ConstraintDecoder<A>,
|
|
350
|
+
token: unknown,
|
|
351
|
+
type: string,
|
|
352
|
+
conversationId: string,
|
|
353
|
+
policy: Jwt.VerifyOptions
|
|
354
|
+
): Effect.Effect<Option.Option<A>> => {
|
|
355
|
+
if (typeof token !== "string" || token.length > maxTokenLength) return Effect.succeedNone
|
|
356
|
+
return Effect.gen(function*() {
|
|
357
|
+
const header = (yield* Jwt.decodeUnverified(token)).protectedHeader
|
|
358
|
+
const key = header.kid === undefined ? undefined : keys.get(header.kid)
|
|
359
|
+
if (key === undefined) return Option.none<A>()
|
|
360
|
+
const verified = yield* Jwt.verify(schema, token, key, { ...policy, audience: options.appId })
|
|
361
|
+
if (verified.protectedHeader.typ !== type) return Option.none<A>()
|
|
362
|
+
const claims = verified.claims as A & { readonly tid: string; readonly cnv: string }
|
|
363
|
+
if (claims.tid !== options.tenantId || claims.cnv !== (yield* hash(conversationId))) return Option.none<A>()
|
|
364
|
+
return Option.some(verified.claims)
|
|
365
|
+
}).pipe(
|
|
366
|
+
Effect.provideService(Mac.Mac, mac),
|
|
367
|
+
Effect.catch(() => Effect.succeedNone)
|
|
368
|
+
)
|
|
369
|
+
}
|
|
370
|
+
|
|
371
|
+
return {
|
|
372
|
+
signButton: (conversationId, button) => {
|
|
373
|
+
const refusal = checkButton(button)
|
|
374
|
+
if (refusal !== undefined) return Effect.fail(refusal)
|
|
375
|
+
return Effect.flatMap(
|
|
376
|
+
hash(conversationId),
|
|
377
|
+
(cnv) => sign(ButtonClaims, buttonClaims(context, cnv, button), typ.button)
|
|
378
|
+
)
|
|
379
|
+
},
|
|
380
|
+
verifyButton: (token, activity) =>
|
|
381
|
+
Effect.map(
|
|
382
|
+
verify(ButtonClaims, token, typ.button, activity.conversationId, {}),
|
|
383
|
+
Option.map((claims) => ({ id: claims.id, ...(claims.val === undefined ? {} : { value: claims.val }) }))
|
|
384
|
+
),
|
|
385
|
+
signDialog: (dialog) => {
|
|
386
|
+
const refusal = checkModal(dialog.modal) ??
|
|
387
|
+
(dialog.userId.length > limits.id || (dialog.messageId?.length ?? 0) > limits.id ||
|
|
388
|
+
dialog.conversationId.length > limits.id
|
|
389
|
+
? invalidRequest("id_too_long")
|
|
390
|
+
: undefined)
|
|
391
|
+
if (refusal !== undefined) return Effect.fail(refusal)
|
|
392
|
+
return Effect.gen(function*() {
|
|
393
|
+
const now = yield* Clock.currentTimeMillis
|
|
394
|
+
const exp = Math.floor((now + Duration.toMillis(dialog.ttl ?? defaultDialogTtl)) / 1000)
|
|
395
|
+
const { metadata } = dialog.modal
|
|
396
|
+
return yield* sign(DialogClaims, {
|
|
397
|
+
aud: options.appId,
|
|
398
|
+
tid: options.tenantId,
|
|
399
|
+
cnv: yield* hash(dialog.conversationId),
|
|
400
|
+
cid: dialog.conversationId,
|
|
401
|
+
sub: dialog.userId,
|
|
402
|
+
exp,
|
|
403
|
+
...(dialog.messageId === undefined ? {} : { mid: dialog.messageId }),
|
|
404
|
+
def: definitionOf(dialog.modal),
|
|
405
|
+
...(metadata === undefined ? {} : { md: metadata })
|
|
406
|
+
}, typ.dialog)
|
|
407
|
+
})
|
|
408
|
+
},
|
|
409
|
+
verifyDialog: (token, activity) =>
|
|
410
|
+
Effect.map(
|
|
411
|
+
verify(DialogClaims, token, typ.dialog, activity.conversationId, {
|
|
412
|
+
subject: activity.userId,
|
|
413
|
+
requiredClaims: ["exp", "sub"]
|
|
414
|
+
}),
|
|
415
|
+
(verified) =>
|
|
416
|
+
verified.pipe(
|
|
417
|
+
Option.filter((claims) => claims.cid === activity.conversationId),
|
|
418
|
+
Option.map((claims): Dialog => ({
|
|
419
|
+
conversationId: claims.cid,
|
|
420
|
+
...(claims.mid === undefined ? {} : { messageId: claims.mid }),
|
|
421
|
+
modal: { ...claims.def, ...(claims.md === undefined ? {} : { metadata: claims.md }) }
|
|
422
|
+
}))
|
|
423
|
+
)
|
|
424
|
+
),
|
|
425
|
+
tokenLength: (button) => buttonTokenLength(context, button)
|
|
426
|
+
}
|
|
427
|
+
})
|
|
@@ -0,0 +1,94 @@
|
|
|
1
|
+
import {
|
|
2
|
+
type Capability,
|
|
3
|
+
ChatError,
|
|
4
|
+
type ChatErrorReason,
|
|
5
|
+
Forbidden,
|
|
6
|
+
InvalidRequest,
|
|
7
|
+
InvalidResponse,
|
|
8
|
+
NotFound,
|
|
9
|
+
PlatformUnavailable,
|
|
10
|
+
RateLimited,
|
|
11
|
+
TooLarge,
|
|
12
|
+
Transport,
|
|
13
|
+
Unauthenticated,
|
|
14
|
+
Unknown,
|
|
15
|
+
Unsupported
|
|
16
|
+
} from "effect/chat/ChatError"
|
|
17
|
+
import * as Duration from "effect/Duration"
|
|
18
|
+
import { platform } from "./address.ts"
|
|
19
|
+
|
|
20
|
+
/**
|
|
21
|
+
* The reason for an HTTP status from the Connector. `detail` is
|
|
22
|
+
* `http_{status}`, or the Connector's error `code` when it sent one, which
|
|
23
|
+
* holds no message text or ids.
|
|
24
|
+
*
|
|
25
|
+
* @internal
|
|
26
|
+
*/
|
|
27
|
+
export const reasonForStatus = (
|
|
28
|
+
status: number,
|
|
29
|
+
options: {
|
|
30
|
+
readonly write: boolean
|
|
31
|
+
readonly code?: string | undefined
|
|
32
|
+
readonly retryAfter?: Duration.Duration | undefined
|
|
33
|
+
readonly cause?: unknown
|
|
34
|
+
}
|
|
35
|
+
): ChatErrorReason => {
|
|
36
|
+
const detail = options.code !== undefined && /^[A-Za-z0-9_.-]{1,64}$/.test(options.code)
|
|
37
|
+
? options.code
|
|
38
|
+
: `http_${status}`
|
|
39
|
+
const cause = options.cause === undefined ? {} : { cause: options.cause }
|
|
40
|
+
if (status === 429) {
|
|
41
|
+
return new RateLimited({
|
|
42
|
+
detail,
|
|
43
|
+
...cause,
|
|
44
|
+
...(options.retryAfter === undefined ? {} : { retryAfter: options.retryAfter })
|
|
45
|
+
})
|
|
46
|
+
}
|
|
47
|
+
if (status >= 500) return new PlatformUnavailable({ retry: options.write ? "uncertain" : "retry", detail, ...cause })
|
|
48
|
+
if (status === 401) return new Unauthenticated({ detail, ...cause })
|
|
49
|
+
if (status === 403) return new Forbidden({ detail, ...cause })
|
|
50
|
+
if (status === 404) return new NotFound({ detail, ...cause })
|
|
51
|
+
if (status === 413) return new TooLarge({ detail, ...cause })
|
|
52
|
+
return new InvalidRequest({ detail, ...cause })
|
|
53
|
+
}
|
|
54
|
+
|
|
55
|
+
/** A request that failed before an answer came. @internal */
|
|
56
|
+
export const transport = (write: boolean, detail: string, cause?: unknown): ChatErrorReason =>
|
|
57
|
+
new Transport({ retry: write ? "uncertain" : "retry", detail, ...(cause === undefined ? {} : { cause }) })
|
|
58
|
+
|
|
59
|
+
/** An answer the adapter cannot read. A write may have landed. @internal */
|
|
60
|
+
export const invalidResponse = (write: boolean, detail: string, cause?: unknown): ChatErrorReason => {
|
|
61
|
+
const fields = { detail, ...(cause === undefined ? {} : { cause }) }
|
|
62
|
+
return write ? new Unknown(fields) : new InvalidResponse(fields)
|
|
63
|
+
}
|
|
64
|
+
|
|
65
|
+
/** @internal */
|
|
66
|
+
export const parseRetryAfter = (value: string | undefined): Duration.Duration | undefined => {
|
|
67
|
+
if (value === undefined) return undefined
|
|
68
|
+
const seconds = Number(value)
|
|
69
|
+
return Number.isFinite(seconds) && seconds >= 0 ? Duration.seconds(seconds) : undefined
|
|
70
|
+
}
|
|
71
|
+
|
|
72
|
+
/** @internal */
|
|
73
|
+
export const chatError = (op: string, reason: ChatErrorReason): ChatError => new ChatError({ platform, op, reason })
|
|
74
|
+
|
|
75
|
+
/** @internal */
|
|
76
|
+
export const withOp = (op: string) => (error: ChatError): ChatError =>
|
|
77
|
+
error.op === op ? error : new ChatError({ platform: error.platform, op, reason: error.reason })
|
|
78
|
+
|
|
79
|
+
/** @internal */
|
|
80
|
+
export const invalidRequest = (detail: string): ChatErrorReason => new InvalidRequest({ detail })
|
|
81
|
+
|
|
82
|
+
/** @internal */
|
|
83
|
+
export const notFound = (detail: string): ChatErrorReason => new NotFound({ detail })
|
|
84
|
+
|
|
85
|
+
/** @internal */
|
|
86
|
+
export const unsupported = (capability: Capability, detail?: string): ChatErrorReason =>
|
|
87
|
+
new Unsupported({ capability, ...(detail === undefined ? {} : { detail }) })
|
|
88
|
+
|
|
89
|
+
/** @internal */
|
|
90
|
+
export const tooLarge = (subject: "text" | "files" | undefined, detail: string): ChatErrorReason =>
|
|
91
|
+
new TooLarge({ ...(subject === undefined ? {} : { subject }), detail })
|
|
92
|
+
|
|
93
|
+
/** A URL outside the adapter's allowlists. Its host is the only part kept. @internal */
|
|
94
|
+
export const untrustedUrl = (): ChatErrorReason => new Forbidden({ detail: "untrusted_url" })
|