Skip to content

Commit d2aaa87

Browse files
authored
core & plugin-flow-builder: allow infer type of user.extra_data #BLT-1592 (#3012)
## Description Allow infer type from BotContext to session.user.extra_data
1 parent 336a7c0 commit d2aaa87

File tree

3 files changed

+43
-29
lines changed

3 files changed

+43
-29
lines changed

packages/botonic-core/src/models/legacy-types.ts

+21-15
Original file line numberDiff line numberDiff line change
@@ -229,7 +229,7 @@ export interface Campaign {
229229
template_name: string
230230
}
231231

232-
export interface SessionUser {
232+
export interface SessionUser<TExtraData = any> {
233233
id: string
234234
// login
235235
username?: string
@@ -238,7 +238,7 @@ export interface SessionUser {
238238
// whatsapp, telegram,...
239239
provider: ProviderType
240240
// The provider's user id
241-
extra_data?: any
241+
extra_data?: TExtraData
242242
imp_id?: string
243243
provider_id?: string
244244
locale: string
@@ -257,7 +257,7 @@ export interface SessionBot {
257257
name?: string
258258
}
259259

260-
export interface Session {
260+
export interface Session<TExtraData = any> {
261261
bot: SessionBot
262262
__retries: number
263263
_access_token: string
@@ -267,7 +267,7 @@ export interface Session {
267267
last_session?: any
268268
organization_id: string
269269
organization: string
270-
user: SessionUser
270+
user: SessionUser<TExtraData>
271271
// after handoff
272272
_botonic_action?: BotonicActionType
273273
_hubtype_case_status?: CaseStatusType
@@ -318,14 +318,16 @@ export interface Route {
318318

319319
export type Routes<R = Route> = R[] | ((_: BotRequest) => R[])
320320

321-
export interface BotRequest {
321+
export interface BotRequest<TExtraData = any> {
322322
input: Input
323323
lastRoutePath: RoutePath
324-
session: Session
324+
session: Session<TExtraData>
325325
}
326326

327-
export interface BotContext<T extends ResolvedPlugins = ResolvedPlugins>
328-
extends BotRequest {
327+
export interface BotContext<
328+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
329+
TExtraData = any,
330+
> extends BotRequest<TExtraData> {
329331
// TODO: remove getString function?
330332
getString: (stringId: string) => string
331333
getUserCountry: () => string
@@ -337,7 +339,7 @@ export interface BotContext<T extends ResolvedPlugins = ResolvedPlugins>
337339
defaultDelay: number
338340
defaultTyping: number
339341
params: Record<string, string>
340-
plugins: T
342+
plugins: TPlugins
341343
}
342344

343345
/** The response of the bot for the triggered actions, which can be
@@ -348,13 +350,17 @@ export interface BotResponse extends BotRequest {
348350
response: any
349351
}
350352

351-
export type PluginPreRequest<T extends ResolvedPlugins = ResolvedPlugins> =
352-
BotContext<T>
353+
export type PluginPreRequest<
354+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
355+
TExtraData = any,
356+
> = BotContext<TPlugins, TExtraData>
353357

354-
export type PluginPostRequest<T extends ResolvedPlugins = ResolvedPlugins> =
355-
BotContext<T> & {
356-
response: string | null
357-
}
358+
export type PluginPostRequest<
359+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
360+
TExtraData = any,
361+
> = BotContext<TPlugins, TExtraData> & {
362+
response: string | null
363+
}
358364

359365
export interface Plugin {
360366
post?(request: PluginPostRequest): void | Promise<void>

packages/botonic-plugin-flow-builder/src/index.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
6666
public jsonVersion: FlowBuilderJSONVersion
6767
public apiUrl: string
6868

69-
constructor(options: BotonicPluginFlowBuilderOptions<ResolvedPlugins>) {
69+
constructor(options: BotonicPluginFlowBuilderOptions<ResolvedPlugins, any>) {
7070
this.apiUrl = options.apiUrl || FLOW_BUILDER_API_URL_PROD
7171
this.jsonVersion = options.jsonVersion || FlowBuilderJSONVersion.LATEST
7272
this.flow = options.flow

packages/botonic-plugin-flow-builder/src/types.ts

+21-13
Original file line numberDiff line numberDiff line change
@@ -13,33 +13,41 @@ export interface InShadowingConfig {
1313
allowKnowledgeBases: boolean
1414
}
1515

16-
export interface BotonicPluginFlowBuilderOptions<T extends ResolvedPlugins> {
16+
export interface BotonicPluginFlowBuilderOptions<
17+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
18+
TExtraData = any,
19+
> {
1720
apiUrl?: string
1821
jsonVersion?: FlowBuilderJSONVersion
1922
flow?: HtFlowBuilderData
2023
customFunctions?: Record<any, any>
2124
getLocale: (session: Session) => string
2225
getAccessToken: () => string
23-
trackEvent?: TrackEventFunction<T>
24-
getKnowledgeBaseResponse?: KnowledgeBaseFunction<T>
26+
trackEvent?: TrackEventFunction<TPlugins, TExtraData>
27+
getKnowledgeBaseResponse?: KnowledgeBaseFunction<TPlugins, TExtraData>
2528
smartIntentsConfig?: { numSmartIntentsToUse: number }
2629
inShadowing?: Partial<InShadowingConfig>
2730
}
2831

29-
export type TrackEventFunction<T extends ResolvedPlugins = ResolvedPlugins> = (
30-
request: BotContext<T>,
32+
export type TrackEventFunction<
33+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
34+
TExtraData = any,
35+
> = (
36+
request: BotContext<TPlugins, TExtraData>,
3137
eventAction: string,
3238
args?: Record<string, any>
3339
) => Promise<void>
3440

35-
export type KnowledgeBaseFunction<T extends ResolvedPlugins = ResolvedPlugins> =
36-
(
37-
request: BotContext<T>,
38-
sources: string[],
39-
instructions: string,
40-
messageId: string,
41-
memoryLength: number
42-
) => Promise<KnowledgeBaseResponse>
41+
export type KnowledgeBaseFunction<
42+
TPlugins extends ResolvedPlugins = ResolvedPlugins,
43+
TExtraData = any,
44+
> = (
45+
request: BotContext<TPlugins, TExtraData>,
46+
sources: string[],
47+
instructions: string,
48+
messageId: string,
49+
memoryLength: number
50+
) => Promise<KnowledgeBaseResponse>
4351

4452
export interface FlowBuilderApiOptions {
4553
url: string

0 commit comments

Comments
 (0)