@whitewall/blip-sdk 0.0.190 → 0.0.192
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/dist/cjs/client.js.map +1 -1
- package/dist/cjs/index.js +1 -0
- package/dist/cjs/index.js.map +1 -1
- package/dist/cjs/types/flow.js +13 -8
- package/dist/cjs/types/flow.js.map +1 -1
- package/dist/cjs/utils/builder.js +40 -21
- package/dist/cjs/utils/builder.js.map +1 -1
- package/dist/cjs/utils/minifier.js +460 -0
- package/dist/cjs/utils/minifier.js.map +1 -0
- package/dist/cjs/utils/whatsapp.js +7 -0
- package/dist/cjs/utils/whatsapp.js.map +1 -1
- package/dist/esm/client.js.map +1 -1
- package/dist/esm/index.js +1 -0
- package/dist/esm/index.js.map +1 -1
- package/dist/esm/types/flow.js +11 -6
- package/dist/esm/types/flow.js.map +1 -1
- package/dist/esm/utils/builder.js +38 -19
- package/dist/esm/utils/builder.js.map +1 -1
- package/dist/esm/utils/minifier.js +455 -0
- package/dist/esm/utils/minifier.js.map +1 -0
- package/dist/esm/utils/whatsapp.js +6 -0
- package/dist/esm/utils/whatsapp.js.map +1 -1
- package/dist/types/client.d.ts +2 -1
- package/dist/types/client.d.ts.map +1 -1
- package/dist/types/index.d.ts +1 -0
- package/dist/types/index.d.ts.map +1 -1
- package/dist/types/namespaces/account.d.ts +20 -20
- package/dist/types/types/flow.d.ts +253 -96
- package/dist/types/types/flow.d.ts.map +1 -1
- package/dist/types/utils/builder.d.ts +28 -16
- package/dist/types/utils/builder.d.ts.map +1 -1
- package/dist/types/utils/minifier.d.ts +2565 -0
- package/dist/types/utils/minifier.d.ts.map +1 -0
- package/dist/types/utils/whatsapp.d.ts +2 -0
- package/dist/types/utils/whatsapp.d.ts.map +1 -1
- package/package.json +1 -1
- package/src/client.ts +2 -1
- package/src/index.ts +1 -0
- package/src/types/flow.ts +12 -6
- package/src/utils/builder.ts +62 -41
- package/src/utils/minifier.ts +554 -0
- package/src/utils/whatsapp.ts +7 -0
|
@@ -0,0 +1,554 @@
|
|
|
1
|
+
/**
|
|
2
|
+
* Round-trippable projection of a builder flow.
|
|
3
|
+
*
|
|
4
|
+
* Unlike the digest in `./builder.ts`, this keeps the `$`-prefixed keys and maps
|
|
5
|
+
* 1:1 onto the builder document: `minifyFlow` strips only the metadata the
|
|
6
|
+
* builder regenerates on its own, and `expandMinifiedFlow` rebuilds it, so a
|
|
7
|
+
* minified flow can be edited by hand and pushed back.
|
|
8
|
+
*/
|
|
9
|
+
import { z } from 'zod'
|
|
10
|
+
import {
|
|
11
|
+
actionTypeSchema,
|
|
12
|
+
baseActionSchema,
|
|
13
|
+
type CONTENT_TYPES,
|
|
14
|
+
conditionOutputSchema,
|
|
15
|
+
defaultOutputSchema,
|
|
16
|
+
type Flow,
|
|
17
|
+
type InputState,
|
|
18
|
+
inputStateSchema,
|
|
19
|
+
type MIME_TYPES,
|
|
20
|
+
ROOT_STATE_ID,
|
|
21
|
+
type State,
|
|
22
|
+
stateSchema,
|
|
23
|
+
} from '../types/flow.ts'
|
|
24
|
+
import { isChatStateSendMessage } from './builder.ts'
|
|
25
|
+
|
|
26
|
+
/**
|
|
27
|
+
* Everything `minifyFlow` removes, grouped by where it lives. Together these
|
|
28
|
+
* describe the same subset as `minifiedStateSchema`, so the two must be kept in
|
|
29
|
+
* sync. The lists deliberately differ from each other: a card only exists on a
|
|
30
|
+
* content action, and only a content action carries a generated `settings.id`.
|
|
31
|
+
*/
|
|
32
|
+
const STATE_NOISE = [
|
|
33
|
+
'$invalidContentActions',
|
|
34
|
+
'$invalidOutputs',
|
|
35
|
+
'$invalidCustomActions',
|
|
36
|
+
'$invalid',
|
|
37
|
+
'id',
|
|
38
|
+
'root',
|
|
39
|
+
'$tags',
|
|
40
|
+
'$inputSuggestions',
|
|
41
|
+
'isAiGenerated',
|
|
42
|
+
'deskStateVersion',
|
|
43
|
+
] as const
|
|
44
|
+
|
|
45
|
+
const CUSTOM_ACTION_LISTS = [
|
|
46
|
+
'$enteringCustomActions',
|
|
47
|
+
'$leavingCustomActions',
|
|
48
|
+
'$afterStateChangedActions',
|
|
49
|
+
'$localCustomActions',
|
|
50
|
+
] as const
|
|
51
|
+
const CUSTOM_ACTION_NOISE = ['$id', '$title', '$invalid', '$typeOfContent'] as const
|
|
52
|
+
|
|
53
|
+
const CONTENT_ACTION_ITEM_NOISE = ['$invalid'] as const
|
|
54
|
+
const CONTENT_ACTION_NOISE = ['$id', '$typeOfContent', '$cardContent', '$inputSchema'] as const
|
|
55
|
+
const CONTENT_ACTION_SETTINGS_NOISE = ['id'] as const
|
|
56
|
+
const CONTENT_INPUT_NOISE = ['$cardContent', '$invalid'] as const
|
|
57
|
+
|
|
58
|
+
const CONDITION_OUTPUT_NOISE = [
|
|
59
|
+
'$id',
|
|
60
|
+
'$invalid',
|
|
61
|
+
'$isBuilderDefaultOutput',
|
|
62
|
+
'$connId',
|
|
63
|
+
'$isDeskOutput',
|
|
64
|
+
'$isDeskCustomOutput',
|
|
65
|
+
'$isDeskDefaultOutput',
|
|
66
|
+
'$contentId',
|
|
67
|
+
] as const
|
|
68
|
+
|
|
69
|
+
const DEFAULT_OUTPUT_NOISE = ['$invalid'] as const
|
|
70
|
+
|
|
71
|
+
/**
|
|
72
|
+
* Pairs an incoming item with the one the builder already has, so its generated
|
|
73
|
+
* ids survive a push instead of being replaced on every write.
|
|
74
|
+
*
|
|
75
|
+
* A match is consumed, because two items in the same state can be identical and
|
|
76
|
+
* must not end up sharing an id.
|
|
77
|
+
*/
|
|
78
|
+
function createStaleMatcher<T>(candidates: ReadonlyArray<T> | undefined, key: (item: T) => string) {
|
|
79
|
+
const byKey = new Map<string, Array<T>>()
|
|
80
|
+
|
|
81
|
+
for (const candidate of candidates ?? []) {
|
|
82
|
+
const candidateKey = key(candidate)
|
|
83
|
+
const bucket = byKey.get(candidateKey)
|
|
84
|
+
|
|
85
|
+
if (bucket) {
|
|
86
|
+
bucket.push(candidate)
|
|
87
|
+
} else {
|
|
88
|
+
byKey.set(candidateKey, [candidate])
|
|
89
|
+
}
|
|
90
|
+
}
|
|
91
|
+
|
|
92
|
+
return (item: T): T | undefined => byKey.get(key(item))?.shift()
|
|
93
|
+
}
|
|
94
|
+
|
|
95
|
+
// The generated `settings.id` is left out of the key since a minified action
|
|
96
|
+
// never carries one.
|
|
97
|
+
const actionKey = ({ type, settings, conditions }: { type: string; settings: unknown; conditions?: unknown }) => {
|
|
98
|
+
const { id: _id, ...rest } = (settings ?? {}) as Record<string, unknown>
|
|
99
|
+
return JSON.stringify([type, rest, conditions])
|
|
100
|
+
}
|
|
101
|
+
|
|
102
|
+
const conditionOutputKey = ({ stateId, conditions }: { stateId: string; conditions?: unknown }) =>
|
|
103
|
+
JSON.stringify([stateId, conditions])
|
|
104
|
+
|
|
105
|
+
// Only the part of the input a minified flow carries, so an unchanged input keeps
|
|
106
|
+
// the card the builder drew for it.
|
|
107
|
+
const inputKey = ({ $cardContent: _card, $invalid: _invalid, ...rest }: InputState) => JSON.stringify(rest)
|
|
108
|
+
|
|
109
|
+
function stripKeys(target: unknown, keys: ReadonlyArray<string>): void {
|
|
110
|
+
if (!target || typeof target !== 'object') {
|
|
111
|
+
return
|
|
112
|
+
}
|
|
113
|
+
|
|
114
|
+
for (const key of keys) {
|
|
115
|
+
delete (target as Record<string, unknown>)[key]
|
|
116
|
+
}
|
|
117
|
+
}
|
|
118
|
+
|
|
119
|
+
/**
|
|
120
|
+
* Angular tags every item of an `ng-repeat`ed array with a `$$hashKey`, so the
|
|
121
|
+
* document the builder saves carries them on its condition outputs, on the
|
|
122
|
+
* conditions inside them and on its content actions. Nothing outside the editor
|
|
123
|
+
* reads them and the editor puts them back, but `minifiedStateSchema` is strict —
|
|
124
|
+
* so left in place they would report a freshly pulled flow as invalid.
|
|
125
|
+
*
|
|
126
|
+
* They are stripped wherever they appear rather than per location, because which
|
|
127
|
+
* arrays the editor happened to repeat over is not something to keep in sync here.
|
|
128
|
+
*/
|
|
129
|
+
function stripAngularKeys(target: unknown): void {
|
|
130
|
+
if (Array.isArray(target)) {
|
|
131
|
+
for (const item of target) {
|
|
132
|
+
stripAngularKeys(item)
|
|
133
|
+
}
|
|
134
|
+
|
|
135
|
+
return
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
if (target && typeof target === 'object') {
|
|
139
|
+
delete (target as Record<string, unknown>).$$hashKey
|
|
140
|
+
|
|
141
|
+
for (const value of Object.values(target)) {
|
|
142
|
+
stripAngularKeys(value)
|
|
143
|
+
}
|
|
144
|
+
}
|
|
145
|
+
}
|
|
146
|
+
|
|
147
|
+
const minifiedActionSchema = z.intersection(baseActionSchema.partial({ $id: true }), actionTypeSchema).check((ctx) => {
|
|
148
|
+
// expandMinifiedFlow derives $typeOfContent from the settings mime type, so
|
|
149
|
+
// a SendMessage without one cannot be pushed. Rejecting it here keeps
|
|
150
|
+
// validation and push in agreement instead of failing halfway through.
|
|
151
|
+
const action = ctx.value
|
|
152
|
+
if (action.type === 'SendMessage' && !action.settings.type) {
|
|
153
|
+
ctx.issues.push({
|
|
154
|
+
code: 'custom',
|
|
155
|
+
input: action,
|
|
156
|
+
path: ['settings', 'type'],
|
|
157
|
+
message: 'A SendMessage action requires a settings.type',
|
|
158
|
+
})
|
|
159
|
+
}
|
|
160
|
+
|
|
161
|
+
// The runtime deserializes the raw content as this media type and refuses to
|
|
162
|
+
// run the action without one, so a hand-written raw message that omits it
|
|
163
|
+
// would be accepted here and then fail in the conversation.
|
|
164
|
+
if (action.type === 'SendRawMessage' && !action.settings.type) {
|
|
165
|
+
ctx.issues.push({
|
|
166
|
+
code: 'custom',
|
|
167
|
+
input: action,
|
|
168
|
+
path: ['settings', 'type'],
|
|
169
|
+
message: 'A SendRawMessage action requires a settings.type',
|
|
170
|
+
})
|
|
171
|
+
}
|
|
172
|
+
})
|
|
173
|
+
|
|
174
|
+
const minifiedInputSchema = inputStateSchema.partial({ $cardContent: true, $invalid: true }).strict()
|
|
175
|
+
|
|
176
|
+
const minifiedContentActionItemSchema = z
|
|
177
|
+
.object({
|
|
178
|
+
action: minifiedActionSchema.optional(),
|
|
179
|
+
input: minifiedInputSchema.optional(),
|
|
180
|
+
})
|
|
181
|
+
.strict()
|
|
182
|
+
|
|
183
|
+
/**
|
|
184
|
+
* `stateSchema` minus the metadata `minifyFlow` strips. It is strict on purpose:
|
|
185
|
+
* minified flows are edited by hand, so an unknown key is almost always a typo
|
|
186
|
+
* that would otherwise be silently dropped on push.
|
|
187
|
+
*/
|
|
188
|
+
export const minifiedStateSchema = stateSchema
|
|
189
|
+
.extend({
|
|
190
|
+
$enteringCustomActions: z.array(minifiedActionSchema),
|
|
191
|
+
$leavingCustomActions: z.array(minifiedActionSchema),
|
|
192
|
+
$localCustomActions: z.array(minifiedActionSchema).optional(),
|
|
193
|
+
$afterStateChangedActions: z.array(minifiedActionSchema).optional(),
|
|
194
|
+
$contentActions: z.array(minifiedContentActionItemSchema),
|
|
195
|
+
$conditionOutputs: z.array(conditionOutputSchema.strict()),
|
|
196
|
+
$defaultOutput: defaultOutputSchema.strict(),
|
|
197
|
+
})
|
|
198
|
+
.partial({
|
|
199
|
+
$tags: true,
|
|
200
|
+
$inputSuggestions: true,
|
|
201
|
+
$invalidContentActions: true,
|
|
202
|
+
$invalidOutputs: true,
|
|
203
|
+
$invalidCustomActions: true,
|
|
204
|
+
})
|
|
205
|
+
.strict()
|
|
206
|
+
|
|
207
|
+
/**
|
|
208
|
+
* A push replaces the whole flow, so a dangling output and a title collision are
|
|
209
|
+
* validation errors here instead of a broken canvas later.
|
|
210
|
+
*/
|
|
211
|
+
export const minifiedFlowSchema = z.record(z.string(), minifiedStateSchema).check((ctx) => {
|
|
212
|
+
const titles = new Map<string, string>()
|
|
213
|
+
|
|
214
|
+
const checkOutput = (output: { stateId: string; typeOfStateId?: string }, path: Array<string | number>) => {
|
|
215
|
+
// A variable output is only resolved at runtime.
|
|
216
|
+
if (output.typeOfStateId === 'variable' || output.stateId in ctx.value) {
|
|
217
|
+
return
|
|
218
|
+
}
|
|
219
|
+
|
|
220
|
+
ctx.issues.push({
|
|
221
|
+
code: 'custom',
|
|
222
|
+
input: output.stateId,
|
|
223
|
+
path: [...path, 'stateId'],
|
|
224
|
+
message: `Output points at '${output.stateId}', which is not a state in this flow`,
|
|
225
|
+
})
|
|
226
|
+
}
|
|
227
|
+
|
|
228
|
+
for (const [stateId, state] of Object.entries(ctx.value)) {
|
|
229
|
+
// The builder addresses a state by its title, so a duplicate hides one.
|
|
230
|
+
const title = state.$title.toLowerCase()
|
|
231
|
+
const duplicate = titles.get(title)
|
|
232
|
+
|
|
233
|
+
if (duplicate) {
|
|
234
|
+
ctx.issues.push({
|
|
235
|
+
code: 'custom',
|
|
236
|
+
input: state.$title,
|
|
237
|
+
path: [stateId, '$title'],
|
|
238
|
+
message: `Duplicate state title '${state.$title}', already used by '${duplicate}'`,
|
|
239
|
+
})
|
|
240
|
+
} else {
|
|
241
|
+
titles.set(title, stateId)
|
|
242
|
+
}
|
|
243
|
+
|
|
244
|
+
checkOutput(state.$defaultOutput, [stateId, '$defaultOutput'])
|
|
245
|
+
|
|
246
|
+
for (const [index, output] of state.$conditionOutputs.entries()) {
|
|
247
|
+
checkOutput(output, [stateId, '$conditionOutputs', index])
|
|
248
|
+
}
|
|
249
|
+
}
|
|
250
|
+
})
|
|
251
|
+
|
|
252
|
+
export type MinifiedState = z.infer<typeof minifiedStateSchema>
|
|
253
|
+
export type MinifiedFlow = z.infer<typeof minifiedFlowSchema>
|
|
254
|
+
|
|
255
|
+
/**
|
|
256
|
+
* Strips everything the builder can regenerate, so what is left is the part of
|
|
257
|
+
* the flow worth reading and editing.
|
|
258
|
+
*
|
|
259
|
+
* The result is round-trippable through `expandMinifiedFlow` with one
|
|
260
|
+
* exception: typing indicators are discarded rather than preserved, so a custom
|
|
261
|
+
* typing interval is replaced by the default on the way back. See
|
|
262
|
+
* `createChatStateContentAction`.
|
|
263
|
+
*/
|
|
264
|
+
export function minifyFlow(flow: Flow): MinifiedFlow {
|
|
265
|
+
const minified: MinifiedFlow = {}
|
|
266
|
+
|
|
267
|
+
for (const [stateId, state] of Object.entries(flow)) {
|
|
268
|
+
const minifiedState = structuredClone(state)
|
|
269
|
+
|
|
270
|
+
stripKeys(minifiedState, STATE_NOISE)
|
|
271
|
+
stripAngularKeys(minifiedState)
|
|
272
|
+
stripKeys(minifiedState.$defaultOutput, DEFAULT_OUTPUT_NOISE)
|
|
273
|
+
|
|
274
|
+
// Expansion writes `false` here for a state that has no plugin, so only a
|
|
275
|
+
// real configuration is worth carrying.
|
|
276
|
+
if (!minifiedState.$whiteWallIntegration) {
|
|
277
|
+
stripKeys(minifiedState, ['$whiteWallIntegration'])
|
|
278
|
+
}
|
|
279
|
+
|
|
280
|
+
for (const output of minifiedState.$conditionOutputs ?? []) {
|
|
281
|
+
stripKeys(output, CONDITION_OUTPUT_NOISE)
|
|
282
|
+
}
|
|
283
|
+
|
|
284
|
+
for (const list of CUSTOM_ACTION_LISTS) {
|
|
285
|
+
for (const action of minifiedState[list] ?? []) {
|
|
286
|
+
stripKeys(action, CUSTOM_ACTION_NOISE)
|
|
287
|
+
}
|
|
288
|
+
}
|
|
289
|
+
|
|
290
|
+
// The builder re-inserts a typing indicator before every message, so
|
|
291
|
+
// carrying them around would just add noise to the minified flow.
|
|
292
|
+
minifiedState.$contentActions = minifiedState.$contentActions
|
|
293
|
+
?.filter((item) => !isChatStateSendMessage(item))
|
|
294
|
+
.map((item) => {
|
|
295
|
+
stripKeys(item, CONTENT_ACTION_ITEM_NOISE)
|
|
296
|
+
stripKeys(item.input, CONTENT_INPUT_NOISE)
|
|
297
|
+
stripKeys(item.action, CONTENT_ACTION_NOISE)
|
|
298
|
+
stripKeys(item.action?.settings, CONTENT_ACTION_SETTINGS_NOISE)
|
|
299
|
+
return item
|
|
300
|
+
})
|
|
301
|
+
|
|
302
|
+
minified[stateId] = minifiedState as unknown as MinifiedState
|
|
303
|
+
}
|
|
304
|
+
|
|
305
|
+
return minified
|
|
306
|
+
}
|
|
307
|
+
|
|
308
|
+
export function expandMinifiedFlow(minifiedFlow: MinifiedFlow, staleFlow?: Flow): Flow {
|
|
309
|
+
const flow: Flow = {}
|
|
310
|
+
|
|
311
|
+
for (const [stateId, minifiedState] of Object.entries(minifiedFlow)) {
|
|
312
|
+
try {
|
|
313
|
+
const staleState = staleFlow?.[stateId]
|
|
314
|
+
const state = structuredClone(minifiedState) as unknown as State
|
|
315
|
+
|
|
316
|
+
state.$invalidContentActions = false
|
|
317
|
+
state.$invalidOutputs = false
|
|
318
|
+
state.$invalidCustomActions = false
|
|
319
|
+
state.$invalid = false
|
|
320
|
+
state.id = stateId
|
|
321
|
+
// When the builder already knows the state, its answer wins; a state
|
|
322
|
+
// being pushed for the first time falls back to the id convention, so
|
|
323
|
+
// a flow pushed without a stale one still gets an entry point.
|
|
324
|
+
state.root = staleState ? staleState.root : stateId === ROOT_STATE_ID || undefined
|
|
325
|
+
state.$tags = staleState?.$tags ?? []
|
|
326
|
+
state.$inputSuggestions = staleState?.$inputSuggestions ?? []
|
|
327
|
+
state.isAiGenerated = staleState?.isAiGenerated ?? false
|
|
328
|
+
// A plugin's configuration is authored, not generated, so the minified
|
|
329
|
+
// flow is the source of truth for it. Falling back to the stale state
|
|
330
|
+
// keeps a plugin configured before this was carried through.
|
|
331
|
+
state.$whiteWallIntegration =
|
|
332
|
+
minifiedState.$whiteWallIntegration ?? staleState?.$whiteWallIntegration ?? false
|
|
333
|
+
state.deskStateVersion = staleState?.deskStateVersion ?? undefined
|
|
334
|
+
state.$defaultOutput.$invalid = false
|
|
335
|
+
|
|
336
|
+
// Reuse the ids the builder already knows so the canvas keeps its
|
|
337
|
+
// connections instead of redrawing them on every push.
|
|
338
|
+
const matchStaleConditionOutput = createStaleMatcher(staleState?.$conditionOutputs, conditionOutputKey)
|
|
339
|
+
|
|
340
|
+
for (const conditionOutput of state.$conditionOutputs ?? []) {
|
|
341
|
+
const staleConditionOutput = matchStaleConditionOutput(conditionOutput)
|
|
342
|
+
|
|
343
|
+
conditionOutput.$id = staleConditionOutput?.$id ?? crypto.randomUUID()
|
|
344
|
+
conditionOutput.$invalid = false
|
|
345
|
+
conditionOutput.$connId = staleConditionOutput?.$connId ?? undefined
|
|
346
|
+
conditionOutput.$isDeskOutput = staleConditionOutput?.$isDeskOutput ?? undefined
|
|
347
|
+
conditionOutput.$isDeskCustomOutput = staleConditionOutput?.$isDeskCustomOutput ?? undefined
|
|
348
|
+
conditionOutput.$isDeskDefaultOutput = staleConditionOutput?.$isDeskDefaultOutput ?? undefined
|
|
349
|
+
conditionOutput.$contentId = staleConditionOutput?.$contentId ?? undefined
|
|
350
|
+
}
|
|
351
|
+
|
|
352
|
+
for (const actionType of CUSTOM_ACTION_LISTS) {
|
|
353
|
+
const matchStaleAction = createStaleMatcher(staleState?.[actionType], actionKey)
|
|
354
|
+
|
|
355
|
+
for (const action of state[actionType] ?? []) {
|
|
356
|
+
const staleAction = matchStaleAction(action)
|
|
357
|
+
|
|
358
|
+
action.$id = staleAction?.$id ?? crypto.randomUUID()
|
|
359
|
+
action.$invalid = false
|
|
360
|
+
action.$title = staleAction?.$title ?? createActionTitle(action)
|
|
361
|
+
action.$typeOfContent = staleAction?.$typeOfContent ?? undefined
|
|
362
|
+
}
|
|
363
|
+
}
|
|
364
|
+
|
|
365
|
+
const matchStaleContentAction = createStaleMatcher(
|
|
366
|
+
staleState?.$contentActions?.flatMap((candidate) => candidate.action ?? []),
|
|
367
|
+
actionKey,
|
|
368
|
+
)
|
|
369
|
+
const matchStaleInput = createStaleMatcher(
|
|
370
|
+
staleState?.$contentActions?.flatMap((candidate) => candidate.input ?? []),
|
|
371
|
+
inputKey,
|
|
372
|
+
)
|
|
373
|
+
|
|
374
|
+
for (const contentAction of state.$contentActions ?? []) {
|
|
375
|
+
contentAction.$invalid = false
|
|
376
|
+
|
|
377
|
+
if (contentAction.input) {
|
|
378
|
+
const staleInput = matchStaleInput(contentAction.input)
|
|
379
|
+
|
|
380
|
+
contentAction.input.$invalid = false
|
|
381
|
+
contentAction.input.$cardContent = staleInput?.$cardContent ?? {
|
|
382
|
+
document: {
|
|
383
|
+
id: crypto.randomUUID(),
|
|
384
|
+
type: 'text/plain',
|
|
385
|
+
textContent: 'Entrada do usuário',
|
|
386
|
+
content: 'Entrada do usuário',
|
|
387
|
+
},
|
|
388
|
+
editable: false,
|
|
389
|
+
deletable: true,
|
|
390
|
+
position: 'right',
|
|
391
|
+
editing: false,
|
|
392
|
+
}
|
|
393
|
+
}
|
|
394
|
+
|
|
395
|
+
if (contentAction.action) {
|
|
396
|
+
const staleAction = matchStaleContentAction(contentAction.action)
|
|
397
|
+
const staleSettings = staleAction?.settings as { id?: string } | undefined
|
|
398
|
+
const settings = contentAction.action.settings as { id?: string; type?: string; content?: unknown }
|
|
399
|
+
|
|
400
|
+
contentAction.action.$id = staleAction?.$id ?? crypto.randomUUID()
|
|
401
|
+
settings.id = staleSettings?.id ?? crypto.randomUUID()
|
|
402
|
+
// The label the builder wrote is at least as precise as a derived
|
|
403
|
+
// one, but a GET can answer with an empty one.
|
|
404
|
+
contentAction.action.$typeOfContent =
|
|
405
|
+
staleAction?.$typeOfContent || getTypeOfContent(contentAction.action)
|
|
406
|
+
contentAction.action.$cardContent = {
|
|
407
|
+
deletable: true,
|
|
408
|
+
editable: true,
|
|
409
|
+
position: 'left',
|
|
410
|
+
document: {
|
|
411
|
+
id: staleAction?.$cardContent?.document.id ?? crypto.randomUUID(),
|
|
412
|
+
type: settings.type as (typeof MIME_TYPES)[number],
|
|
413
|
+
content: settings.content as string,
|
|
414
|
+
},
|
|
415
|
+
}
|
|
416
|
+
}
|
|
417
|
+
}
|
|
418
|
+
|
|
419
|
+
// Walk backwards so the chat states can be spliced in without shifting
|
|
420
|
+
// the indexes still to be visited.
|
|
421
|
+
for (let index = (state.$contentActions ?? []).length - 1; index >= 0; index--) {
|
|
422
|
+
if (state.$contentActions[index].action) {
|
|
423
|
+
state.$contentActions.splice(index, 0, createChatStateContentAction())
|
|
424
|
+
}
|
|
425
|
+
}
|
|
426
|
+
|
|
427
|
+
flow[stateId] = state
|
|
428
|
+
} catch (err) {
|
|
429
|
+
if (err instanceof Error) {
|
|
430
|
+
throw new Error(`[State ${stateId}] ${err.message}`, { cause: err })
|
|
431
|
+
}
|
|
432
|
+
|
|
433
|
+
throw err
|
|
434
|
+
}
|
|
435
|
+
}
|
|
436
|
+
|
|
437
|
+
// Drops the keys explicitly set to undefined above.
|
|
438
|
+
return JSON.parse(JSON.stringify(flow))
|
|
439
|
+
}
|
|
440
|
+
|
|
441
|
+
/**
|
|
442
|
+
* Typing indicators are dropped by `minifyFlow` and rebuilt here from scratch,
|
|
443
|
+
* one before every message, rather than being carried through the minified flow.
|
|
444
|
+
*
|
|
445
|
+
* This is deliberate: they are pure presentation, they outnumber the messages
|
|
446
|
+
* they precede, and keeping them would double the size of the file a person or
|
|
447
|
+
* an LLM has to read and edit. The trade-off is that a typing interval tuned by
|
|
448
|
+
* hand in the builder does not survive a round trip and comes back as the
|
|
449
|
+
* default below.
|
|
450
|
+
*/
|
|
451
|
+
function createChatStateContentAction(): State['$contentActions'][number] {
|
|
452
|
+
const content = { state: 'composing', interval: 1000 }
|
|
453
|
+
|
|
454
|
+
return {
|
|
455
|
+
action: {
|
|
456
|
+
$id: crypto.randomUUID(),
|
|
457
|
+
$typeOfContent: 'chat-state',
|
|
458
|
+
type: 'SendMessage',
|
|
459
|
+
settings: {
|
|
460
|
+
id: crypto.randomUUID(),
|
|
461
|
+
type: 'application/vnd.lime.chatstate+json',
|
|
462
|
+
content,
|
|
463
|
+
},
|
|
464
|
+
$cardContent: {
|
|
465
|
+
document: {
|
|
466
|
+
id: crypto.randomUUID(),
|
|
467
|
+
type: 'application/vnd.lime.chatstate+json',
|
|
468
|
+
content,
|
|
469
|
+
},
|
|
470
|
+
editable: true,
|
|
471
|
+
deletable: true,
|
|
472
|
+
position: 'left',
|
|
473
|
+
},
|
|
474
|
+
},
|
|
475
|
+
$invalid: false,
|
|
476
|
+
}
|
|
477
|
+
}
|
|
478
|
+
|
|
479
|
+
/** Derives the label the builder would have given an action it has never seen. */
|
|
480
|
+
function getTypeOfContent(
|
|
481
|
+
action: NonNullable<State['$contentActions'][number]['action']>,
|
|
482
|
+
): (typeof CONTENT_TYPES)[number] {
|
|
483
|
+
if (action.type === 'SendRawMessage') {
|
|
484
|
+
return 'raw-content'
|
|
485
|
+
}
|
|
486
|
+
|
|
487
|
+
const settings = action.settings as {
|
|
488
|
+
type?: (typeof MIME_TYPES)[number]
|
|
489
|
+
content?: { scope?: string; type?: string }
|
|
490
|
+
}
|
|
491
|
+
|
|
492
|
+
switch (settings.type) {
|
|
493
|
+
case 'text/plain':
|
|
494
|
+
return 'text'
|
|
495
|
+
case 'application/vnd.lime.media-link+json':
|
|
496
|
+
return getTypeOfMedia(settings.content?.type ?? '')
|
|
497
|
+
case 'application/vnd.lime.select+json':
|
|
498
|
+
return settings.content?.scope === 'immediate' ? 'select-immediate' : 'select'
|
|
499
|
+
case 'application/vnd.lime.chatstate+json':
|
|
500
|
+
return 'chat-state'
|
|
501
|
+
case 'application/vnd.lime.web-link+json':
|
|
502
|
+
return 'web-link'
|
|
503
|
+
default:
|
|
504
|
+
throw new Error(`Content action does not have a valid action property: ${JSON.stringify(action)}`)
|
|
505
|
+
}
|
|
506
|
+
}
|
|
507
|
+
|
|
508
|
+
function getTypeOfMedia(mimeType: string): (typeof CONTENT_TYPES)[number] {
|
|
509
|
+
if (mimeType.startsWith('audio/')) {
|
|
510
|
+
return 'media-audio'
|
|
511
|
+
}
|
|
512
|
+
|
|
513
|
+
if (mimeType.startsWith('video/')) {
|
|
514
|
+
return 'media-video'
|
|
515
|
+
}
|
|
516
|
+
|
|
517
|
+
return mimeType.startsWith('image/') ? 'media' : 'media-document'
|
|
518
|
+
}
|
|
519
|
+
|
|
520
|
+
function createActionTitle(action: State['$enteringCustomActions'][number]): string | undefined {
|
|
521
|
+
const sanitize = (value: string | number) => value.toString().replace(/[^a-zA-Z0-9.@]/g, '')
|
|
522
|
+
|
|
523
|
+
switch (action.type) {
|
|
524
|
+
case 'TrackEvent':
|
|
525
|
+
return `Track ${sanitize(action.settings.category)}`
|
|
526
|
+
|
|
527
|
+
case 'ExecuteScript':
|
|
528
|
+
case 'ExecuteScriptV2':
|
|
529
|
+
return `Process ${action.settings.outputVariable}`
|
|
530
|
+
|
|
531
|
+
case 'Redirect':
|
|
532
|
+
return `Redirect to ${sanitize(action.settings.address)}`
|
|
533
|
+
|
|
534
|
+
case 'MergeContact': {
|
|
535
|
+
const items = Object.keys(action.settings).filter((key) => key !== 'extras')
|
|
536
|
+
if (Object.keys(action.settings.extras ?? {}).length > 0) {
|
|
537
|
+
items.push('extras')
|
|
538
|
+
}
|
|
539
|
+
return `Set ${items.join(', ')}`
|
|
540
|
+
}
|
|
541
|
+
|
|
542
|
+
case 'ProcessHttp':
|
|
543
|
+
return `[${action.settings.method}] Process ${action.settings.responseBodyVariable ?? ''}`
|
|
544
|
+
|
|
545
|
+
case 'SetVariable':
|
|
546
|
+
return `Set ${action.settings.variable} to ${sanitize(action.settings.value ?? '')}`
|
|
547
|
+
|
|
548
|
+
case 'ProcessCommand':
|
|
549
|
+
return `[${action.settings.method.toUpperCase()}] Process ${action.settings.variable}`
|
|
550
|
+
|
|
551
|
+
default:
|
|
552
|
+
return undefined
|
|
553
|
+
}
|
|
554
|
+
}
|
package/src/utils/whatsapp.ts
CHANGED
|
@@ -1,5 +1,12 @@
|
|
|
1
|
+
import { Node, type PossiblyNode } from '../types/node.ts'
|
|
1
2
|
import type { MessageTemplate, MessageTemplatePreview, MessageTemplateVariable } from '../types/whatsapp.ts'
|
|
2
3
|
|
|
4
|
+
// A contact's identity is a GUID (BSUID) when they haven't shared their phone number with
|
|
5
|
+
// the business, and a phone number when they have — a phone-shaped check tells them apart.
|
|
6
|
+
export function isWhatsAppGuidIdentity(identity: PossiblyNode): boolean {
|
|
7
|
+
return !/^\d+$/.test(Node.from(identity).name)
|
|
8
|
+
}
|
|
9
|
+
|
|
3
10
|
type TemplateHeaderFormat = NonNullable<MessageTemplatePreview['headerFormat']>
|
|
4
11
|
|
|
5
12
|
type TemplateHeaderComponent = {
|