Skip to content

Commit

Permalink
feat(core): debug supports sub-namespaces
Browse files Browse the repository at this point in the history
  • Loading branch information
slvnperron committed Mar 15, 2019
1 parent 8555d42 commit ce53321
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
15 changes: 14 additions & 1 deletion modules/channel-messenger/src/backend/messenger.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@ import _ from 'lodash'

import { Config } from '../config'

const debug = DEBUG('channel-messenger')
const debugMessages = debug.sub('messages')
const debugHttp = debug.sub('http')
const debugWebhook = debugHttp.sub('webhook')
const debugHttpOut = debugHttp.sub('out')

const outgoingTypes = ['text', 'typing', 'login_prompt', 'carousel']
type MessengerAction = 'typing_on' | 'typing_off' | 'mark_seen'

Expand Down Expand Up @@ -127,7 +133,10 @@ export class MessengerService {
.digest('hex')

if (hash !== expectedHash) {
debugWebhook('invalid signature', req.path)
throw signatureError
} else {
debugWebhook('signed', req.path)
}
}
}
Expand All @@ -147,10 +156,12 @@ export class MessengerService {

const bot = _.find<MountedBot>(this.mountedBots, { pageId })
if (!bot) {
debugMessages('could not find a bot for page id =', pageId)
continue
}

for (const webhookEvent of messages) {
debugMessages('incoming', webhookEvent)
const senderId = webhookEvent.sender.id

await bot.client.sendAction(senderId, 'mark_seen')
Expand Down Expand Up @@ -288,7 +299,7 @@ export class MessengerClient {
},
sender_action: action
}

debugMessages('outgoing action', { senderId, action, body })
await this._callEndpoint('/messages', body)
}

Expand All @@ -300,6 +311,7 @@ export class MessengerClient {
message
}

debugMessages('outgoing text message', { senderId, message, body })
await this._callEndpoint('/messages', body)
}

Expand All @@ -309,6 +321,7 @@ export class MessengerClient {

private async _callEndpoint(endpoint: string, body) {
const config = await this.getConfig()
debugHttpOut(endpoint, body)
await this.http.post(endpoint, body, { params: { access_token: config.accessToken } })
}
}
Expand Down
10 changes: 8 additions & 2 deletions modules/nlu/src/backend/engine.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ import { generateTrainingSequence } from './pipelines/slots/pre-processor'
import Storage from './storage'
import { EntityExtractor, LanguageIdentifier, Model, MODEL_TYPES, SlotExtractor } from './typings'

const debug = DEBUG('nlu')
const debugExtract = debug.sub('extract')
const debugIntents = debugExtract.sub('intents')
const debugEntities = debugExtract.sub('entities')

export default class ScopedEngine {
public readonly storage: Storage
public confidenceTreshold: number = 0.7
Expand Down Expand Up @@ -272,15 +277,15 @@ export default class ScopedEngine {
const patternEntities = extractPatternEntities(text, customEntityDefs.filter(ent => ent.type === 'pattern'))
const listEntities = extractListEntities(text, customEntityDefs.filter(ent => ent.type === 'list'))
const systemEntities = await this.systemEntityExtractor.extract(text, lang)

debugEntities(text, { systemEntities, patternEntities, listEntities })
return [...systemEntities, ...patternEntities, ...listEntities]
}

private async _extractIntents(text: string): Promise<{ intents: sdk.NLU.Intent[]; intent: sdk.NLU.Intent }> {
const intents = await this.intentClassifier.predict(text)
const intent = findMostConfidentIntentMeanStd(intents, this.confidenceTreshold)
intent.matches = createIntentMatcher(intent.name)

debugIntents(text, { intents })
return {
intents,
intent
Expand All @@ -304,6 +309,7 @@ export default class ScopedEngine {
ret = { ...ret, ...(await this._extractIntents(text)) }
ret.entities = await this._extractEntities(text, ret.language)
ret.slots = await this._extractSlots(text, ret.intent, ret.entities)
debugEntities('slots', { text, slots: ret.slots })
ret.errored = false
} catch (error) {
this.logger.attachError(error).error(`Could not extract whole NLU data, ${error}`)
Expand Down
11 changes: 8 additions & 3 deletions src/bp/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,16 @@ const printPlainError = err => {

const available = {}

global.DEBUG = mod => {
available['bp:' + mod] = true
return debug('bp:' + mod)
const DEBUG = (mod, base = 'bp') => {
const namespace = base + ':' + mod
available[namespace] = true
const instance = debug(base).extend(mod)
instance.sub = mod => DEBUG(mod, namespace)
return instance
}

global.DEBUG = DEBUG

export const getDebugScopes = () => {
const status = {}
Object.keys(available).forEach(key => (status[key] = debug.enabled(key)))
Expand Down
7 changes: 6 additions & 1 deletion src/typings/global.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,12 @@ declare type BotpressEnvironementVariables = {
}

interface IDebug {
(module: string): (msg: string) => void
(module: string): IDebugInstance
}

interface IDebugInstance {
(msg: string, extra?: any): void
sub(namespace: string): IDebugInstance
}

declare var DEBUG: IDebug
Expand Down

0 comments on commit ce53321

Please sign in to comment.