Skip to content

Commit

Permalink
refactor: apply suggestions, simplify code
Browse files Browse the repository at this point in the history
  • Loading branch information
vanbasten17 committed May 17, 2024
1 parent 80b82c4 commit e9d9d69
Show file tree
Hide file tree
Showing 4 changed files with 16 additions and 22 deletions.
8 changes: 3 additions & 5 deletions packages/botonic-plugin-flow-builder/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,15 +36,14 @@ import {
PayloadParamsBase,
} from './types'
import { getNodeByUserInput } from './user-input'
import { SmartIntentsInferenceParams } from './user-input/smart-intent'
import { SmartIntentsInferenceConfig } from './user-input/smart-intent'
import { resolveGetAccessToken } from './utils'
export default class BotonicPluginFlowBuilder implements Plugin {
public cmsApi: FlowBuilderApi
private flowUrl: string
private flow?: HtFlowBuilderData
private functions: Record<any, any>
private currentRequest: PluginPreRequest

private getAccessToken: (session: Session) => string
public getLocale: (session: Session) => string
public trackEvent?: (
Expand All @@ -55,8 +54,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
public getKnowledgeBaseResponse?: (
request: ActionRequest
) => Promise<KnowledgeBaseResponse>

public smartIntentsConfig: Partial<SmartIntentsInferenceParams>
public smartIntentsConfig: SmartIntentsInferenceConfig

constructor(readonly options: BotonicPluginFlowBuilderOptions) {
const apiUrl = options.apiUrl || FLOW_BUILDER_API_URL_PROD
Expand All @@ -68,7 +66,7 @@ export default class BotonicPluginFlowBuilder implements Plugin {
this.trackEvent = options.trackEvent
this.getKnowledgeBaseResponse = options.getKnowledgeBaseResponse
this.smartIntentsConfig = {
...(options.smartIntentsConfig || {}),
...options?.smartIntentsConfig,
use_latest: jsonVersion === FlowBuilderJSONVersion.LATEST,
}
const customFunctions = options.customFunctions || {}
Expand Down
27 changes: 12 additions & 15 deletions packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,37 +4,34 @@ import axios from 'axios'
import { FlowBuilderApi } from '../api'
import { HtSmartIntentNode } from '../content-fields/hubtype-fields/smart-intent'

export interface SmartIntentsInferenceParams {
export interface SmartIntentsInferenceParams
extends SmartIntentsInferenceConfig {
bot_id: string
text: string
}

export interface SmartIntentsInferenceConfig {
use_latest: boolean
num_smart_intents_to_use?: number
}
export class SmartIntentsApi {
public cmsApi: FlowBuilderApi
public currentRequest: ActionRequest
public smartIntentsConfig: Partial<SmartIntentsInferenceParams>

export class SmartIntentsApi {

Check warning on line 18 in packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts#L18

Added line #L18 was not covered by tests
constructor(
cmsApi: FlowBuilderApi,
request: ActionRequest,
smartIntentsConfig: Partial<SmartIntentsInferenceParams>
) {
this.currentRequest = request
this.cmsApi = cmsApi
this.smartIntentsConfig = smartIntentsConfig
}
public cmsApi: FlowBuilderApi,
public currentRequest: ActionRequest,
public smartIntentsConfig: SmartIntentsInferenceConfig

Check warning on line 22 in packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts#L20-L22

Added lines #L20 - L22 were not covered by tests
) {}

async getNodeByInput(): Promise<HtSmartIntentNode | undefined> {
if (!this.currentRequest.input.data) return undefined
const smartIntentNodes = this.cmsApi.getSmartIntentNodes()
if (!smartIntentNodes.length) return undefined

const params: SmartIntentsInferenceParams = {
const params = {

Check warning on line 30 in packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts

View check run for this annotation

Codecov / codecov/patch

packages/botonic-plugin-flow-builder/src/user-input/smart-intent.ts#L30

Added line #L30 was not covered by tests
bot_id: this.currentRequest.session.bot.id,
text: this.currentRequest.input.data,
...this.smartIntentsConfig,
} as SmartIntentsInferenceParams
}

try {
const response = await this.getInference(params)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,6 @@ export function mockSmartIntent(intentName?: string) {

// Change the implementation of getInference
getInferenceSpy.mockImplementation(async () => {
return intentName ? { data: { intent_name: intentName } } : undefined
return intentName ? { data: { smart_intent_title: intentName } } : undefined
})
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,6 @@ describe('Check the contents returned by the plugin when match a smart intent',
request,
flowBuilderPlugin
)

expect((contents[0] as FlowText).text).toBe(
'Message explaining how to add a bag'
)
Expand Down

0 comments on commit e9d9d69

Please sign in to comment.