@salutejs/scenario
Advanced tools
Comparing version 0.1.2 to 0.2.0
@@ -8,2 +8,5 @@ "use strict"; | ||
return { | ||
get appInfo() { | ||
return request.payload.app_info; | ||
}, | ||
get message() { | ||
@@ -10,0 +13,0 @@ return request.payload.message; |
@@ -31,7 +31,29 @@ "use strict"; | ||
}; | ||
var runApp = function (appInfo, parameters) { | ||
message = { | ||
messageId: messageId, | ||
sessionId: sessionId, | ||
uuid: uuid, | ||
messageName: types_1.NLPResponseType.POLICY_RUN_APP, | ||
payload: { | ||
projectName: payload.projectName, | ||
device: payload.device, | ||
server_action: { | ||
app_info: appInfo, | ||
parameters: parameters, | ||
}, | ||
}, | ||
}; | ||
}; | ||
return { | ||
appendBubble: function (bubble) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.items.push({ bubble: { text: bubble, expand_policy: 'auto_expand' } }); | ||
}, | ||
appendCommand: function (command) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.items.push({ command: { type: 'smart_app_data', smart_app_data: __assign({}, command) } }); | ||
@@ -41,25 +63,63 @@ }, | ||
appendItem: function (item) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.items.push(item); | ||
}, | ||
appendError: function (error) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.items.push({ command: { type: 'smart_app_error', smart_app_error: error } }); | ||
}, | ||
appendSuggestions: function (suggestions) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
if (message.payload.suggestions == null) { | ||
message.payload.suggestions = { buttons: [] }; | ||
} | ||
suggestions.forEach(function (suggest) { | ||
message.payload.suggestions.buttons.push({ | ||
title: suggest, | ||
action: { type: 'text', text: suggest, should_send_to_backend: true }, | ||
}); | ||
}); | ||
for (var i = 0; i < suggestions.length; i++) { | ||
var suggest = suggestions[i]; | ||
if (typeof suggest === 'string') { | ||
message.payload.suggestions.buttons.push({ | ||
title: suggest, | ||
action: { type: 'text', text: suggest, should_send_to_backend: true }, | ||
}); | ||
} | ||
else { | ||
message.payload.suggestions.buttons.push(suggest); | ||
} | ||
} | ||
message.payload.suggestions.buttons; | ||
}, | ||
askPayment: function (invoiceId) { | ||
runApp({ systemName: 'payment_app' }, { | ||
invoice_id: invoiceId, | ||
app_info: { | ||
projectId: req.payload.app_info.projectId, | ||
}, | ||
}); | ||
}, | ||
runApp: runApp, | ||
setIntent: function (intent) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.intent = intent; | ||
}, | ||
setPronounceText: function (text) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.pronounceText = text; | ||
}, | ||
setEmotion: function (emotion) { | ||
if (message.messageName !== types_1.NLPResponseType.ANSWER_TO_USER) { | ||
throw new Error('Wrong message type'); | ||
} | ||
message.payload.emotion = { | ||
emotionId: emotion, | ||
}; | ||
}, | ||
get message() { | ||
@@ -66,0 +126,0 @@ return message; |
@@ -1,2 +0,2 @@ | ||
import { IntentsDict, SaluteRequest, SaluteResponse, SaluteSession, Recognizer } from '@salutejs/types'; | ||
import { IntentsDict, SaluteRequest, SaluteResponse, SaluteSession, Recognizer, SaluteRequestVariable, AppState } from '@salutejs/types'; | ||
import { createUserScenario } from './createUserScenario'; | ||
@@ -6,3 +6,3 @@ import { SystemScenario } from './createSystemScenario'; | ||
intents: IntentsDict; | ||
recognizer: Recognizer; | ||
recognizer?: Recognizer; | ||
systemScenario: SystemScenario; | ||
@@ -13,3 +13,7 @@ userScenario: ReturnType<typeof createUserScenario>; | ||
export declare const createScenarioWalker: ({ intents, recognizer, systemScenario, userScenario, slotFillingConfidence, }: ScenarioWalkerOptions) => ({ req, res, session, }: { | ||
req: SaluteRequest; | ||
req: SaluteRequest<SaluteRequestVariable, AppState, { | ||
action_id?: string; | ||
type: string; | ||
payload: unknown; | ||
}>; | ||
res: SaluteResponse; | ||
@@ -16,0 +20,0 @@ session: SaluteSession; |
@@ -47,19 +47,39 @@ "use strict"; | ||
var dispatch, saluteHandlerOpts, variant, currentIntent, connected, missingVars, question, scenarioState, missingVars; | ||
var _b, _c; | ||
return __generator(this, function (_d) { | ||
switch (_d.label) { | ||
var _b, _c, _d; | ||
return __generator(this, function (_e) { | ||
switch (_e.label) { | ||
case 0: | ||
dispatch = function (path) { | ||
var state = userScenario.getByPath(path); | ||
if (state) { | ||
session.path = path; | ||
req.currentState = { | ||
path: session.path, | ||
state: state, | ||
}; | ||
state.handle({ req: req, res: res, session: session.state, history: {} }, dispatch); | ||
} | ||
}; | ||
dispatch = function (path) { return __awaiter(void 0, void 0, void 0, function () { | ||
var state; | ||
return __generator(this, function (_a) { | ||
switch (_a.label) { | ||
case 0: | ||
state = userScenario.getByPath(path); | ||
if (!state) return [3 /*break*/, 2]; | ||
session.path = path; | ||
req.currentState = { | ||
path: session.path, | ||
state: state, | ||
}; | ||
return [4 /*yield*/, state.handle({ req: req, res: res, session: session.state, history: {} }, dispatch)]; | ||
case 1: | ||
_a.sent(); | ||
_a.label = 2; | ||
case 2: return [2 /*return*/]; | ||
} | ||
}); | ||
}); }; | ||
saluteHandlerOpts = { req: req, res: res, session: session.state, history: {} }; | ||
if (req.intent === 'run_app') { | ||
if (((_b = req.serverAction) === null || _b === void 0 ? void 0 : _b.action_id) === 'PAY_DIALOG_FINISHED') { | ||
if (typeof systemScenario.PAY_DIALOG_FINISHED === 'undefined') { | ||
res.appendError({ | ||
code: 404, | ||
description: 'Missing handler for action: "PAY_DIALOG_FINISHED"', | ||
}); | ||
return [2 /*return*/]; | ||
} | ||
systemScenario.PAY_DIALOG_FINISHED(saluteHandlerOpts, dispatch); | ||
return [2 /*return*/]; | ||
} | ||
systemScenario.RUN_APP(saluteHandlerOpts, dispatch); | ||
@@ -82,3 +102,3 @@ return [2 /*return*/]; | ||
} | ||
if (!req.voiceAction) return [3 /*break*/, 2]; | ||
if (!(req.voiceAction && typeof recognizer !== 'undefined')) return [3 /*break*/, 2]; | ||
// INFERENCE LOGIC START | ||
@@ -88,4 +108,4 @@ return [4 /*yield*/, recognizer.inference({ req: req, res: res, session: session })]; | ||
// INFERENCE LOGIC START | ||
_d.sent(); | ||
variant = ((_b = req.inference) === null || _b === void 0 ? void 0 : _b.variants.length) > 0 ? req.inference.variants[0] : null; | ||
_e.sent(); | ||
variant = ((_c = req.inference) === null || _c === void 0 ? void 0 : _c.variants.length) > 0 ? req.inference.variants[0] : null; | ||
if (variant) { | ||
@@ -97,3 +117,3 @@ variant.slots.forEach(function (slot) { | ||
if (session.path.length && session.slotFilling) { | ||
connected = (((_c = req.inference) === null || _c === void 0 ? void 0 : _c.variants) || []).find(function (v) { return v.confidence >= slotFillingConfidence && v.intent.path === session.currentIntent; }); | ||
connected = (((_d = req.inference) === null || _d === void 0 ? void 0 : _d.variants) || []).find(function (v) { return v.confidence >= slotFillingConfidence && v.intent.path === session.currentIntent; }); | ||
currentIntent = connected || variant; | ||
@@ -119,3 +139,3 @@ } | ||
} | ||
_d.label = 2; | ||
_e.label = 2; | ||
case 2: | ||
@@ -143,12 +163,14 @@ scenarioState = userScenario.resolve(session.path, req); | ||
} | ||
if (scenarioState) { | ||
req.currentState = scenarioState; | ||
dispatch(scenarioState.path); | ||
if (!req.currentState.state.children) { | ||
session.path = []; | ||
session.variables = {}; | ||
session.currentIntent = undefined; | ||
} | ||
return [2 /*return*/]; | ||
if (!scenarioState) return [3 /*break*/, 4]; | ||
req.currentState = scenarioState; | ||
return [4 /*yield*/, dispatch(scenarioState.path)]; | ||
case 3: | ||
_e.sent(); | ||
if (!req.currentState.state.children) { | ||
session.path = []; | ||
session.variables = {}; | ||
session.currentIntent = undefined; | ||
} | ||
return [2 /*return*/]; | ||
case 4: | ||
systemScenario.NO_MATCH(saluteHandlerOpts, dispatch); | ||
@@ -155,0 +177,0 @@ return [2 /*return*/]; |
@@ -6,4 +6,5 @@ import { SaluteHandler } from '@salutejs/types'; | ||
NO_MATCH: SaluteHandler; | ||
PAY_DIALOG_FINISHED?: SaluteHandler; | ||
}; | ||
export declare const createSystemScenario: (systemScenarioSchema?: Partial<SystemScenario>) => SystemScenario; | ||
//# sourceMappingURL=createSystemScenario.d.ts.map |
@@ -8,2 +8,3 @@ import { SaluteRequest, ScenarioSchema } from '@salutejs/types'; | ||
}>) => boolean; | ||
schema?: string; | ||
handle: import("@salutejs/types").SaluteHandler<SaluteRequest<Record<string, unknown>, import("@salutejs/types").AppState, { | ||
@@ -10,0 +11,0 @@ payload: unknown; |
@@ -12,2 +12,7 @@ "use strict"; | ||
function createUserScenario(scenarioSchema) { | ||
/** | ||
* Возвращает вложенные обработчики для указанного пути в дереве диалогов | ||
* @param path путь в дереве диалога | ||
* @returns undefined или потомки | ||
*/ | ||
var getByPath = function (path) { | ||
@@ -26,2 +31,7 @@ var obj = scenarioSchema[path[0]]; | ||
}; | ||
/** Возвращает обработчик запроса для указанного пути в дереве диалогов | ||
* @param path путь в дереве диалога, поиск будет выполнен среди вложенных обработчиков | ||
* @param req объект запроса | ||
* @returns Возвращает объект вида { path, state }, где state - обработчик, path - путь из дерева диалогов | ||
*/ | ||
var resolve = function (path, req) { | ||
@@ -28,0 +38,0 @@ var matchedState; |
{ | ||
"name": "@salutejs/scenario", | ||
"version": "0.1.2", | ||
"version": "0.2.0", | ||
"description": "Tiny helpers to make scenario for Salute family", | ||
@@ -33,5 +33,5 @@ "author": "SberDevices Frontend Team <sberdevices.frontend@gmail.com>", | ||
"dependencies": { | ||
"@salutejs/types": "0.1.1" | ||
"@salutejs/types": "0.2.0" | ||
}, | ||
"gitHead": "f618bff07b2d90a7d99064012c3924178c1b29a0" | ||
"gitHead": "457946fb2d87ac9d86f2597eb64f7f0229975dc3" | ||
} |
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
Sorry, the diff of this file is not supported yet
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
New author
Supply chain riskA new npm collaborator published a version of the package for the first time. New collaborators are usually benign additions to a project, but do indicate a change to the security surface area of a package.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
101721
639
1
+ Added@salutejs/types@0.2.0(transitive)
- Removed@salutejs/types@0.1.1(transitive)
Updated@salutejs/types@0.2.0