Skip to content

Commit

Permalink
Add InputContext
Browse files Browse the repository at this point in the history
  • Loading branch information
adiwajshing committed Mar 13, 2021
1 parent a8f4681 commit d3fe556
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 10 deletions.
23 changes: 16 additions & 7 deletions src/LanguageProcessor.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { randomBytes } from '@adiwajshing/baileys'
import natural from 'natural'
import { chat as cmdLineChat } from './LanguageProcessor.CMDLine'
import { IntentData, LanguageProcessorMetadata } from './types'
import { InputContext, IntentData, LanguageProcessorMetadata } from './types'
import { parseTemplate } from './utils'

export const createLanguageProcessor = (intents: IntentData[], metadata: LanguageProcessorMetadata = {}) => {
Expand Down Expand Up @@ -92,10 +93,10 @@ export const createLanguageProcessor = (intents: IntentData[], metadata: Languag
}
return extractedIntents
}
const computeOutput = async(data: IntentData, entities: string[], user: string) => {
const computeOutput = async(data: IntentData, entities: string[], ctx: InputContext) => {
let answer: string | string[]
if (typeof data.answer === 'function') { // if the intent requires a function to answer
answer = await data.answer(entities, user)
answer = await data.answer(entities, ctx)
} else if(entities.length === 0) {
if (data.answer.includes("{{")) { // if the answer requires an entity to answer but no entities were parsed
throw new Error(
Expand All @@ -110,7 +111,7 @@ export const createLanguageProcessor = (intents: IntentData[], metadata: Languag
const entityObj = data.entities[key]
const value = typeof entityObj === 'object' ? entityObj.value : entityObj

if(typeof value === 'function') return value(entities, user)
if(typeof value === 'function') return value(entities, ctx)
else {
const mustacheParams = { entity: { key, value } }
if(typeof data.answer !== 'string') {
Expand All @@ -128,7 +129,7 @@ export const createLanguageProcessor = (intents: IntentData[], metadata: Languag
* @param user - ID of the user who is requesting the output
* @returns the response
*/
const output = async (input: string, user: string) => {
const output = async (input: string, ctx: InputContext) => {
const compileAnswer = (strings: string[]) => (
strings.length===1 ?
strings[0] :
Expand All @@ -146,7 +147,7 @@ export const createLanguageProcessor = (intents: IntentData[], metadata: Languag
}
// compute the output for each intent & map the errors as text
const tasks = extractedIntents.map(({ intent, entities }) => (
computeOutput(intent, entities, user)
computeOutput(intent, entities, ctx)
))
const outputs = await Promise.allSettled(tasks)
const correctOutputs = outputs.filter(output => output.status === 'fulfilled')
Expand All @@ -164,7 +165,15 @@ export const createLanguageProcessor = (intents: IntentData[], metadata: Languag
throw new Error( compileAnswer(strings) )
}
}
const chat = () => cmdLineChat(ip => output(ip, 'test'))
const chat = () => cmdLineChat(ip => (
output(
ip,
{
userId: 'test',
messageId: randomBytes(8).toString('hex')
}
)
))

if(!metadata.entityRequiredText) {
metadata.entityRequiredText = availableEntities => (
Expand Down
5 changes: 4 additions & 1 deletion src/WAResponder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,10 @@ export const onWAMessage = async(

let response
try {
response = await processor.output(messageText, senderID)
response = await processor.output(
messageText,
{ userId: senderID, messageId: message.key.id }
)
} catch (err) {
// do not respond if its a group
if (senderID.includes("@g.us")) return
Expand Down
9 changes: 7 additions & 2 deletions src/types.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
export type IntentAnswer = string | ((entities: string[], user: string) => Promise<string> | string)
export type InputContext = {
userId: string
messageId: string
}

export type IntentAnswer = string | ((entities: string[], ctx: InputContext) => Promise<string> | string)
export type IntentEntities = {
[_: string]: IntentAnswer | { alternates?: string[], value: IntentAnswer }
}
Expand Down Expand Up @@ -30,7 +35,7 @@ export type LanguageProcessorMetadata = {
entityRequiredText?: (availableEntities: string[]) => string
}
export type LanguageProcessor = {
output: (input: string, user: string) => Promise<string>
output: (input: string, ctx: InputContext) => Promise<string>
}
export type Responser = {
start: () => void | Promise<void>
Expand Down

0 comments on commit d3fe556

Please sign in to comment.