Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #25 from botpress/rl_engine
feat(core): Add dialog engine
- Loading branch information
Showing
21 changed files
with
922 additions
and
47 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"lerna": "2.11.0", | ||
"packages": ["modules/*", "packages/*"], | ||
"packages": ["packages/*"], | ||
"version": "0.0.1", | ||
"npmClient": "yarn" | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
18 changes: 18 additions & 0 deletions
18
packages/core/src/database/tables/bot-specific/dialog_sessions.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
import { Table } from '../../interfaces' | ||
|
||
export default class DialogSessionTable extends Table { | ||
name: string = 'dialog_sessions' | ||
|
||
async bootstrap() { | ||
await this.knex | ||
.createTableIfNotExists(this.name, table => { | ||
table.increments('id') | ||
table.text('state') | ||
table.text('context') | ||
table.timestamp('active_on') | ||
table.text('created_on') | ||
table.text('modified_on') | ||
}) | ||
.then() | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,10 +1,11 @@ | ||
import { ExtendedKnex, Table } from '../interfaces' | ||
|
||
import DialogSessionTable from './bot-specific/dialog_sessions' | ||
import BotsTable from './server-wide/bots' | ||
import MetadataTable from './server-wide/metadata' | ||
import MigrationsTable from './server-wide/migrations' | ||
import ModulesTable from './server-wide/modules' | ||
|
||
const tables: (typeof Table)[] = [BotsTable, MigrationsTable, MetadataTable, ModulesTable] | ||
const tables: (typeof Table)[] = [BotsTable, DialogSessionTable, MigrationsTable, MetadataTable, ModulesTable] | ||
|
||
export default <(new (knex: ExtendedKnex) => Table)[]>tables |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,109 @@ | ||
import { inject, injectable } from 'inversify' | ||
|
||
import { ExtendedKnex } from '../database/interfaces' | ||
import { TYPES } from '../misc/types' | ||
|
||
export type DialogSession = { | ||
id: string | ||
state: string | ||
context: any | ||
created_on: string | ||
modified_on: string | ||
active_on: string | ||
} | ||
|
||
export interface SessionRepository { | ||
get(id: string): Promise<DialogSession> | ||
upsert(session: DialogSession) | ||
delete(id: string) | ||
update(session: DialogSession) | ||
} | ||
|
||
@injectable() | ||
export class KnexSessionRepository implements SessionRepository { | ||
private readonly tableName = 'dialog_sessions' | ||
|
||
constructor(@inject(TYPES.Database) private knex: ExtendedKnex) {} | ||
|
||
async get(id: string): Promise<DialogSession> { | ||
const session = <DialogSession>await this.knex(this.tableName) | ||
.where({ id }) | ||
.limit(1) | ||
.get(0) | ||
.then() | ||
return session ? this.jsonParse(session) : this.createSession(id) | ||
} | ||
|
||
async upsert(session: DialogSession) { | ||
const params = { | ||
tableName: this.tableName, | ||
id: session.id, | ||
state: JSON.stringify(session.state), | ||
context: JSON.stringify(session.context), | ||
now: this.knex.date.now() | ||
} | ||
|
||
let sql: string | ||
if (this.knex.isLite) { | ||
sql = ` | ||
INSERT OR REPLACE INTO :tableName (id, state, context, active_on, created_on, modified_on) | ||
VALUES (:id, :state, :context, :now, :now, :now) | ||
` | ||
} else { | ||
sql = ` | ||
INSERT INTO :tableName (id, state, context, active_on, created_on, modified_on) | ||
VALUES (:id, :state, :context, :now, :now, :now) | ||
ON CONFLICT (id) DO UPDATE | ||
SET state = :state, context = :context, active_on = :now, modified_on = :now | ||
` | ||
} | ||
|
||
return this.knex.raw(sql, params) | ||
} | ||
|
||
async update(session: DialogSession) { | ||
session.modified_on = this.knex.date.now().toString() | ||
|
||
return await this.knex(this.tableName) | ||
.update(session) | ||
.where('id', session.id) | ||
.then() | ||
} | ||
|
||
async delete(id: string) { | ||
await this.knex(this.tableName) | ||
.where({ id }) | ||
.del() | ||
.then() | ||
} | ||
|
||
private async createSession(id: string): Promise<DialogSession> { | ||
const params = { | ||
tableName: this.tableName, | ||
id, | ||
state: '', | ||
context: '', | ||
now: this.knex.date.now() | ||
} | ||
|
||
const sql = ` | ||
INSERT INTO :tableName: (id, state, active_on, created_on) | ||
VALUES (:id, :state, :now, :now) | ||
ON CONFLICT (id) DO UPDATE | ||
SET created_on = :now, active_on = :now, modified_on = :now, state = :state, context = :context` | ||
|
||
await this.knex.raw(sql, params) | ||
const session = <DialogSession>await this.knex(this.tableName) | ||
.select('*') | ||
.where({ id }) | ||
.then() | ||
|
||
return this.jsonParse(session) | ||
} | ||
|
||
private jsonParse(session: DialogSession) { | ||
session.context = JSON.parse(session.context) | ||
session.state = JSON.parse(session.state) | ||
return session | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.