@voiceflow/sdk-runtime 1.2.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +38 -0
- package/build/cjs/main.d.ts +4 -0
- package/build/cjs/main.d.ts.map +1 -0
- package/build/cjs/main.js +19 -0
- package/build/cjs/package.json +1 -0
- package/build/cjs/runtime/index.d.ts +3 -0
- package/build/cjs/runtime/index.d.ts.map +1 -0
- package/build/cjs/runtime/index.js +18 -0
- package/build/cjs/runtime/runtime.interface.d.ts +119 -0
- package/build/cjs/runtime/runtime.interface.d.ts.map +1 -0
- package/build/cjs/runtime/runtime.interface.js +50 -0
- package/build/cjs/runtime/runtime.service.d.ts +9 -0
- package/build/cjs/runtime/runtime.service.d.ts.map +1 -0
- package/build/cjs/runtime/runtime.service.js +48 -0
- package/build/cjs/sdk/index.d.ts +3 -0
- package/build/cjs/sdk/index.d.ts.map +1 -0
- package/build/cjs/sdk/index.js +18 -0
- package/build/cjs/sdk/sdk.interface.d.ts +4 -0
- package/build/cjs/sdk/sdk.interface.d.ts.map +1 -0
- package/build/cjs/sdk/sdk.interface.js +2 -0
- package/build/cjs/sdk/sdk.service.d.ts +16 -0
- package/build/cjs/sdk/sdk.service.d.ts.map +1 -0
- package/build/cjs/sdk/sdk.service.js +27 -0
- package/build/cjs/trace/components/card.component.d.ts +3 -0
- package/build/cjs/trace/components/card.component.d.ts.map +1 -0
- package/build/cjs/trace/components/card.component.js +5 -0
- package/build/cjs/trace/components/choice.component.d.ts +3 -0
- package/build/cjs/trace/components/choice.component.d.ts.map +1 -0
- package/build/cjs/trace/components/choice.component.js +5 -0
- package/build/cjs/trace/components/end.component.d.ts +3 -0
- package/build/cjs/trace/components/end.component.d.ts.map +1 -0
- package/build/cjs/trace/components/end.component.js +5 -0
- package/build/cjs/trace/components/index.d.ts +9 -0
- package/build/cjs/trace/components/index.d.ts.map +1 -0
- package/build/cjs/trace/components/index.js +24 -0
- package/build/cjs/trace/components/payload.component.d.ts +3 -0
- package/build/cjs/trace/components/payload.component.d.ts.map +1 -0
- package/build/cjs/trace/components/payload.component.js +5 -0
- package/build/cjs/trace/components/speak.component.d.ts +3 -0
- package/build/cjs/trace/components/speak.component.d.ts.map +1 -0
- package/build/cjs/trace/components/speak.component.js +5 -0
- package/build/cjs/trace/components/stream.component.d.ts +3 -0
- package/build/cjs/trace/components/stream.component.d.ts.map +1 -0
- package/build/cjs/trace/components/stream.component.js +5 -0
- package/build/cjs/trace/components/text.component.d.ts +3 -0
- package/build/cjs/trace/components/text.component.d.ts.map +1 -0
- package/build/cjs/trace/components/text.component.js +5 -0
- package/build/cjs/trace/components/visual.component.d.ts +3 -0
- package/build/cjs/trace/components/visual.component.d.ts.map +1 -0
- package/build/cjs/trace/components/visual.component.js +5 -0
- package/build/cjs/trace/index.d.ts +5 -0
- package/build/cjs/trace/index.d.ts.map +1 -0
- package/build/cjs/trace/index.js +20 -0
- package/build/cjs/trace/trace.component.d.ts +8 -0
- package/build/cjs/trace/trace.component.d.ts.map +1 -0
- package/build/cjs/trace/trace.component.js +13 -0
- package/build/cjs/trace/trace.interface.d.ts +16 -0
- package/build/cjs/trace/trace.interface.d.ts.map +1 -0
- package/build/cjs/trace/trace.interface.js +2 -0
- package/build/cjs/trace/trace.service.d.ts +10 -0
- package/build/cjs/trace/trace.service.d.ts.map +1 -0
- package/build/cjs/trace/trace.service.js +33 -0
- package/build/esm/main.d.ts +4 -0
- package/build/esm/main.d.ts.map +1 -0
- package/build/esm/main.js +3 -0
- package/build/esm/package.json +1 -0
- package/build/esm/runtime/index.d.ts +3 -0
- package/build/esm/runtime/index.d.ts.map +1 -0
- package/build/esm/runtime/index.js +2 -0
- package/build/esm/runtime/runtime.interface.d.ts +119 -0
- package/build/esm/runtime/runtime.interface.d.ts.map +1 -0
- package/build/esm/runtime/runtime.interface.js +33 -0
- package/build/esm/runtime/runtime.service.d.ts +9 -0
- package/build/esm/runtime/runtime.service.d.ts.map +1 -0
- package/build/esm/runtime/runtime.service.js +41 -0
- package/build/esm/sdk/index.d.ts +3 -0
- package/build/esm/sdk/index.d.ts.map +1 -0
- package/build/esm/sdk/index.js +2 -0
- package/build/esm/sdk/sdk.interface.d.ts +4 -0
- package/build/esm/sdk/sdk.interface.d.ts.map +1 -0
- package/build/esm/sdk/sdk.interface.js +1 -0
- package/build/esm/sdk/sdk.service.d.ts +16 -0
- package/build/esm/sdk/sdk.service.d.ts.map +1 -0
- package/build/esm/sdk/sdk.service.js +23 -0
- package/build/esm/trace/components/card.component.d.ts +3 -0
- package/build/esm/trace/components/card.component.d.ts.map +1 -0
- package/build/esm/trace/components/card.component.js +2 -0
- package/build/esm/trace/components/choice.component.d.ts +3 -0
- package/build/esm/trace/components/choice.component.d.ts.map +1 -0
- package/build/esm/trace/components/choice.component.js +2 -0
- package/build/esm/trace/components/end.component.d.ts +3 -0
- package/build/esm/trace/components/end.component.d.ts.map +1 -0
- package/build/esm/trace/components/end.component.js +2 -0
- package/build/esm/trace/components/index.d.ts +9 -0
- package/build/esm/trace/components/index.d.ts.map +1 -0
- package/build/esm/trace/components/index.js +8 -0
- package/build/esm/trace/components/payload.component.d.ts +3 -0
- package/build/esm/trace/components/payload.component.d.ts.map +1 -0
- package/build/esm/trace/components/payload.component.js +2 -0
- package/build/esm/trace/components/speak.component.d.ts +3 -0
- package/build/esm/trace/components/speak.component.d.ts.map +1 -0
- package/build/esm/trace/components/speak.component.js +2 -0
- package/build/esm/trace/components/stream.component.d.ts +3 -0
- package/build/esm/trace/components/stream.component.d.ts.map +1 -0
- package/build/esm/trace/components/stream.component.js +2 -0
- package/build/esm/trace/components/text.component.d.ts +3 -0
- package/build/esm/trace/components/text.component.d.ts.map +1 -0
- package/build/esm/trace/components/text.component.js +2 -0
- package/build/esm/trace/components/visual.component.d.ts +3 -0
- package/build/esm/trace/components/visual.component.d.ts.map +1 -0
- package/build/esm/trace/components/visual.component.js +2 -0
- package/build/esm/trace/index.d.ts +5 -0
- package/build/esm/trace/index.d.ts.map +1 -0
- package/build/esm/trace/index.js +4 -0
- package/build/esm/trace/trace.component.d.ts +8 -0
- package/build/esm/trace/trace.component.d.ts.map +1 -0
- package/build/esm/trace/trace.component.js +9 -0
- package/build/esm/trace/trace.interface.d.ts +16 -0
- package/build/esm/trace/trace.interface.d.ts.map +1 -0
- package/build/esm/trace/trace.interface.js +1 -0
- package/build/esm/trace/trace.service.d.ts +10 -0
- package/build/esm/trace/trace.service.d.ts.map +1 -0
- package/build/esm/trace/trace.service.js +29 -0
- package/package.json +45 -0
- package/sonar-project.properties +6 -0
- package/src/main.ts +3 -0
- package/src/runtime/index.ts +2 -0
- package/src/runtime/runtime.interface.ts +177 -0
- package/src/runtime/runtime.service.ts +54 -0
- package/src/sdk/index.ts +2 -0
- package/src/sdk/sdk.interface.ts +4 -0
- package/src/sdk/sdk.service.ts +35 -0
- package/src/trace/components/card.component.ts +5 -0
- package/src/trace/components/choice.component.ts +5 -0
- package/src/trace/components/end.component.ts +5 -0
- package/src/trace/components/index.ts +8 -0
- package/src/trace/components/payload.component.ts +5 -0
- package/src/trace/components/speak.component.ts +5 -0
- package/src/trace/components/stream.component.ts +5 -0
- package/src/trace/components/text.component.ts +5 -0
- package/src/trace/components/visual.component.ts +5 -0
- package/src/trace/index.ts +4 -0
- package/src/trace/trace.component.ts +23 -0
- package/src/trace/trace.interface.ts +23 -0
- package/src/trace/trace.service.ts +42 -0
- package/tsconfig.build.cjs.json +9 -0
- package/tsconfig.build.esm.json +12 -0
- package/tsconfig.build.json +5 -0
- package/tsconfig.json +11 -0
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
import type { RuntimeInteractRequest, RuntimeState, RuntimeTrace } from '../runtime/runtime.interface.js';
|
|
2
|
+
export interface TraceOptions<T> {
|
|
3
|
+
traces?: TraceDeclaration<T, any>[];
|
|
4
|
+
}
|
|
5
|
+
export interface TraceDeclaration<Context, Trace extends RuntimeTrace> {
|
|
6
|
+
canHandle: TraceCanHandle<Trace>;
|
|
7
|
+
handle: TraceAccumulator<Context, Trace>;
|
|
8
|
+
}
|
|
9
|
+
export declare type TraceCanHandle<Trace extends RuntimeTrace> = (trace: Trace) => boolean;
|
|
10
|
+
export interface TraceHandlerMeta<Context> {
|
|
11
|
+
request: RuntimeInteractRequest;
|
|
12
|
+
state: RuntimeState;
|
|
13
|
+
context: Context;
|
|
14
|
+
}
|
|
15
|
+
export declare type TraceAccumulator<Context, Trace extends RuntimeTrace> = (meta: TraceHandlerMeta<Context>, trace: Trace) => Context;
|
|
16
|
+
//# sourceMappingURL=trace.interface.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.interface.d.ts","sourceRoot":"","sources":["../../../src/trace/trace.interface.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,YAAY,EAAE,YAAY,EAAE,MAAM,6BAA6B,CAAC;AAEtG,MAAM,WAAW,YAAY,CAAC,CAAC;IAC7B,MAAM,CAAC,EAAE,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,CAAC;CACrC;AAED,MAAM,WAAW,gBAAgB,CAAC,OAAO,EAAE,KAAK,SAAS,YAAY;IACnE,SAAS,EAAE,cAAc,CAAC,KAAK,CAAC,CAAC;IACjC,MAAM,EAAE,gBAAgB,CAAC,OAAO,EAAE,KAAK,CAAC,CAAC;CAC1C;AAED,oBAAY,cAAc,CAAC,KAAK,SAAS,YAAY,IAAI,CAAC,KAAK,EAAE,KAAK,KAAK,OAAO,CAAC;AAEnF,MAAM,WAAW,gBAAgB,CAAC,OAAO;IACvC,OAAO,EAAE,sBAAsB,CAAC;IAChC,KAAK,EAAE,YAAY,CAAC;IACpB,OAAO,EAAE,OAAO,CAAC;CAClB;AAED,oBAAY,gBAAgB,CAAC,OAAO,EAAE,KAAK,SAAS,YAAY,IAAI,CAClE,IAAI,EAAE,gBAAgB,CAAC,OAAO,CAAC,EAC/B,KAAK,EAAE,KAAK,KACT,OAAO,CAAC"}
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,10 @@
|
|
|
1
|
+
import type { RuntimeInteractRequest, RuntimeInteractResponse } from '../runtime/runtime.interface.js';
|
|
2
|
+
import type { TraceDeclaration, TraceOptions } from './trace.interface.js';
|
|
3
|
+
export declare class TraceService<T = unknown> {
|
|
4
|
+
private readonly traces;
|
|
5
|
+
constructor(options?: TraceOptions<T>);
|
|
6
|
+
registerTrace(step: TraceDeclaration<T, any>): this;
|
|
7
|
+
registerTraces(steps: TraceDeclaration<T, any>[]): this;
|
|
8
|
+
processTrace(context: T, request: RuntimeInteractRequest, response: RuntimeInteractResponse): Promise<T>;
|
|
9
|
+
}
|
|
10
|
+
//# sourceMappingURL=trace.service.d.ts.map
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
{"version":3,"file":"trace.service.d.ts","sourceRoot":"","sources":["../../../src/trace/trace.service.ts"],"names":[],"mappings":"AAAA,OAAO,KAAK,EAAE,sBAAsB,EAAE,uBAAuB,EAAE,MAAM,gCAAgC,CAAC;AAEtG,OAAO,KAAK,EAAE,gBAAgB,EAAoB,YAAY,EAAE,MAAM,mBAAmB,CAAC;AAE1F,qBAAa,YAAY,CAAC,CAAC,GAAG,OAAO;IACnC,OAAO,CAAC,QAAQ,CAAC,MAAM,CAAkC;gBAEtC,OAAO,GAAE,YAAY,CAAC,CAAC,CAAM;IAIzC,aAAa,CAAC,IAAI,EAAE,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC,GAAG,IAAI;IAKnD,cAAc,CAAC,KAAK,EAAE,gBAAgB,CAAC,CAAC,EAAE,GAAG,CAAC,EAAE,GAAG,IAAI;IAKjD,YAAY,CACvB,OAAO,EAAE,CAAC,EACV,OAAO,EAAE,sBAAsB,EAC/B,QAAQ,EAAE,uBAAuB,GAChC,OAAO,CAAC,CAAC,CAAC;CAgBd"}
|
|
@@ -0,0 +1,29 @@
|
|
|
1
|
+
export class TraceService {
|
|
2
|
+
constructor(options = {}) {
|
|
3
|
+
this.traces = [];
|
|
4
|
+
this.registerTraces(options.traces ?? []);
|
|
5
|
+
}
|
|
6
|
+
registerTrace(step) {
|
|
7
|
+
this.traces.push(step);
|
|
8
|
+
return this;
|
|
9
|
+
}
|
|
10
|
+
registerTraces(steps) {
|
|
11
|
+
steps.forEach((step) => this.registerTrace(step));
|
|
12
|
+
return this;
|
|
13
|
+
}
|
|
14
|
+
async processTrace(context, request, response) {
|
|
15
|
+
const meta = {
|
|
16
|
+
request,
|
|
17
|
+
state: response.state,
|
|
18
|
+
context,
|
|
19
|
+
};
|
|
20
|
+
for (const trace of response.trace) {
|
|
21
|
+
const step = this.traces.find((step) => step.canHandle(trace));
|
|
22
|
+
if (step) {
|
|
23
|
+
// eslint-disable-next-line no-await-in-loop
|
|
24
|
+
meta.context = await step.handle(meta, trace);
|
|
25
|
+
}
|
|
26
|
+
}
|
|
27
|
+
return meta.context;
|
|
28
|
+
}
|
|
29
|
+
}
|
package/package.json
ADDED
|
@@ -0,0 +1,45 @@
|
|
|
1
|
+
{
|
|
2
|
+
"name": "@voiceflow/sdk-runtime",
|
|
3
|
+
"version": "1.2.1",
|
|
4
|
+
"exports": {
|
|
5
|
+
".": {
|
|
6
|
+
"import": "./build/esm/main.js",
|
|
7
|
+
"default": "./build/cjs/main.js"
|
|
8
|
+
}
|
|
9
|
+
},
|
|
10
|
+
"main": "build/cjs/main.js",
|
|
11
|
+
"types": "build/esm/main.d.ts",
|
|
12
|
+
"scripts": {
|
|
13
|
+
"build": "yarn g:run-p build:cjs build:esm",
|
|
14
|
+
"prebuild:cjs": "yarn g:rimraf build/cjs",
|
|
15
|
+
"build:cjs": "yarn prebuild:cjs && yarn g:tsc -p tsconfig.build.cjs.json && yarn g:tsc-alias -p tsconfig.build.cjs.json && yarn postbuild:cjs",
|
|
16
|
+
"postbuild:cjs": "touch build/cjs/package.json && echo '{ \"type\": \"commonjs\" }' > build/cjs/package.json",
|
|
17
|
+
"prebuild:esm": "yarn g:rimraf build/esm",
|
|
18
|
+
"build:esm": "yarn prebuild:esm && yarn g:tsc -p tsconfig.build.esm.json && yarn g:tsc-alias -p tsconfig.build.esm.json && yarn postbuild:esm",
|
|
19
|
+
"postbuild:esm": "touch build/esm/package.json && echo '{ \"type\": \"module\" }' > build/esm/package.json",
|
|
20
|
+
"lint": "yarn g:run-p lint:eslint \"lint:prettier --check\"",
|
|
21
|
+
"lint:eslint": "yarn g:eslint \"src/**/*.{js,ts}\"",
|
|
22
|
+
"lint:fix": "yarn g:run-p \"lint:eslint --fix\" \"lint:prettier --write\"",
|
|
23
|
+
"lint:prettier": "yarn g:prettier . \"!**/*.{js,ts}\"",
|
|
24
|
+
"lint:report": "yarn g:eslint-output --quiet './src/**/*.{js,ts}'",
|
|
25
|
+
"test:dependencies": "depcheck",
|
|
26
|
+
"test:unit": "yarn g:jest --passWithNoTests"
|
|
27
|
+
},
|
|
28
|
+
"prettier": "@voiceflow-meta/prettier-config",
|
|
29
|
+
"eslintConfig": {
|
|
30
|
+
"extends": "@voiceflow-meta/eslint-config"
|
|
31
|
+
},
|
|
32
|
+
"dependencies": {
|
|
33
|
+
"http-errors": "^2.0.0"
|
|
34
|
+
},
|
|
35
|
+
"devDependencies": {
|
|
36
|
+
"@types/http-errors": "^1.8.2",
|
|
37
|
+
"@voiceflow-meta/eslint-config": "1.1.2",
|
|
38
|
+
"@voiceflow-meta/prettier-config": "1.1.2",
|
|
39
|
+
"@voiceflow-meta/typescript-config": "1.1.2"
|
|
40
|
+
},
|
|
41
|
+
"publishConfig": {
|
|
42
|
+
"access": "public"
|
|
43
|
+
},
|
|
44
|
+
"gitHead": "49214dc837eab6ba59490a425d25d818114be2e6"
|
|
45
|
+
}
|
package/src/main.ts
ADDED
|
@@ -0,0 +1,177 @@
|
|
|
1
|
+
// Super broad type so that differing fetch types (ex. builtin, node-fetch, etc.) don't conflict with it
|
|
2
|
+
type FetchFn = (...parameters: any[]) => Promise<any>;
|
|
3
|
+
|
|
4
|
+
export interface RuntimeOptions {
|
|
5
|
+
url: string;
|
|
6
|
+
authorization: string;
|
|
7
|
+
|
|
8
|
+
/**
|
|
9
|
+
* A ponyfill to use for `fetch()`.
|
|
10
|
+
* Defaults to the global `fetch()`.
|
|
11
|
+
*/
|
|
12
|
+
fetchPonyfill?: FetchFn;
|
|
13
|
+
}
|
|
14
|
+
|
|
15
|
+
export interface RuntimeInteractRequest {
|
|
16
|
+
action: RuntimeAction;
|
|
17
|
+
sessionID: string;
|
|
18
|
+
versionID?: string;
|
|
19
|
+
config?: any;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
export interface RuntimeInteractResponse {
|
|
23
|
+
state: RuntimeState;
|
|
24
|
+
request: RuntimeAction;
|
|
25
|
+
trace: RuntimeTrace[];
|
|
26
|
+
}
|
|
27
|
+
|
|
28
|
+
export const RuntimeActionTypeSet = new Set(['launch', 'intent', 'text', '...etc'] as const);
|
|
29
|
+
|
|
30
|
+
export type RuntimeActionType = typeof RuntimeActionTypeSet extends Set<infer K> ? K : never;
|
|
31
|
+
|
|
32
|
+
export const isRuntimeActionType = (value: unknown): value is RuntimeActionType =>
|
|
33
|
+
typeof value === 'string' && RuntimeActionTypeSet.has(value as any);
|
|
34
|
+
|
|
35
|
+
export interface RuntimeAction<T extends RuntimeActionType = RuntimeActionType, P = unknown> {
|
|
36
|
+
type: T;
|
|
37
|
+
payload: P;
|
|
38
|
+
}
|
|
39
|
+
|
|
40
|
+
export const isRuntimeAction = (value: unknown): value is RuntimeAction =>
|
|
41
|
+
typeof value === 'object' &&
|
|
42
|
+
Object.prototype.hasOwnProperty.call(value, 'type') &&
|
|
43
|
+
isRuntimeActionType((value as any).type);
|
|
44
|
+
|
|
45
|
+
export interface IntentActionPayload {
|
|
46
|
+
query: string;
|
|
47
|
+
intent: { name: string };
|
|
48
|
+
entities: { name: string; value: string }[];
|
|
49
|
+
confidence?: number;
|
|
50
|
+
}
|
|
51
|
+
|
|
52
|
+
export type IntentAction = RuntimeAction<'intent', IntentActionPayload>;
|
|
53
|
+
|
|
54
|
+
export const isIntentAction = (value: unknown): value is IntentAction =>
|
|
55
|
+
isRuntimeAction(value) && value.type === 'intent';
|
|
56
|
+
|
|
57
|
+
export const createIntentAction = (payload: IntentActionPayload): IntentAction => ({
|
|
58
|
+
type: 'intent',
|
|
59
|
+
payload,
|
|
60
|
+
});
|
|
61
|
+
|
|
62
|
+
export interface RuntimeState {
|
|
63
|
+
storage: Record<string, any>;
|
|
64
|
+
variables: Record<string, any>;
|
|
65
|
+
}
|
|
66
|
+
|
|
67
|
+
export const RuntimeTraceTypeSet = new Set([
|
|
68
|
+
'speak',
|
|
69
|
+
'text',
|
|
70
|
+
'choice',
|
|
71
|
+
'card',
|
|
72
|
+
'visual',
|
|
73
|
+
'audio',
|
|
74
|
+
'payload',
|
|
75
|
+
'stream',
|
|
76
|
+
'end',
|
|
77
|
+
] as const);
|
|
78
|
+
|
|
79
|
+
export type RuntimeTraceType = typeof RuntimeTraceTypeSet extends Set<infer K> ? K : never;
|
|
80
|
+
|
|
81
|
+
export const isRuntimeTraceType = (value: unknown): value is RuntimeTraceType =>
|
|
82
|
+
typeof value === 'string' && RuntimeTraceTypeSet.has(value as any);
|
|
83
|
+
|
|
84
|
+
export interface RuntimeTrace<T extends RuntimeTraceType = RuntimeTraceType, P = unknown> {
|
|
85
|
+
type: T;
|
|
86
|
+
payload: P;
|
|
87
|
+
}
|
|
88
|
+
|
|
89
|
+
export const isRuntimeTrace = (value: unknown): value is RuntimeTrace =>
|
|
90
|
+
typeof value === 'object' &&
|
|
91
|
+
Object.prototype.hasOwnProperty.call(value, 'type') &&
|
|
92
|
+
isRuntimeTraceType((value as any).type);
|
|
93
|
+
|
|
94
|
+
export interface CardTracePayload {
|
|
95
|
+
card: {
|
|
96
|
+
type: string;
|
|
97
|
+
title: string;
|
|
98
|
+
text: string;
|
|
99
|
+
image?: {
|
|
100
|
+
largeImageUrl: string;
|
|
101
|
+
alt: string;
|
|
102
|
+
};
|
|
103
|
+
};
|
|
104
|
+
}
|
|
105
|
+
|
|
106
|
+
export type CardTrace = RuntimeTrace<'card', CardTracePayload>;
|
|
107
|
+
|
|
108
|
+
export const isCardTrace = (value: unknown): value is CardTrace => isRuntimeTrace(value) && value.type === 'card';
|
|
109
|
+
|
|
110
|
+
export type ChoiceTrace = RuntimeTrace<'choice', ChoiceTracePayload>;
|
|
111
|
+
|
|
112
|
+
export interface ChoiceTracePayload {
|
|
113
|
+
buttons: { name: string }[];
|
|
114
|
+
}
|
|
115
|
+
|
|
116
|
+
export const isChoiceTrace = (value: unknown): value is ChoiceTrace => isRuntimeTrace(value) && value.type === 'choice';
|
|
117
|
+
|
|
118
|
+
export interface PayloadTracePayload {
|
|
119
|
+
data: any;
|
|
120
|
+
}
|
|
121
|
+
|
|
122
|
+
export type EndTrace = RuntimeTrace<'end'>;
|
|
123
|
+
|
|
124
|
+
export const isEndTrace = (value: unknown): value is EndTrace => isRuntimeTrace(value) && value.type === 'end';
|
|
125
|
+
|
|
126
|
+
export type PayloadTrace = RuntimeTrace<'payload', PayloadTracePayload>;
|
|
127
|
+
|
|
128
|
+
export const isPayloadTrace = (value: unknown): value is PayloadTrace =>
|
|
129
|
+
isRuntimeTrace(value) && value.type === 'payload';
|
|
130
|
+
|
|
131
|
+
export interface SpeakTracePayload {
|
|
132
|
+
voice?: string;
|
|
133
|
+
src?: string;
|
|
134
|
+
message: string;
|
|
135
|
+
type: 'speak' | 'audio';
|
|
136
|
+
}
|
|
137
|
+
|
|
138
|
+
export type SpeakTrace = RuntimeTrace<'speak', SpeakTracePayload>;
|
|
139
|
+
|
|
140
|
+
export const isSpeakTrace = (value: unknown): value is SpeakTrace => isRuntimeTrace(value) && value.type === 'speak';
|
|
141
|
+
|
|
142
|
+
export interface TextTracePayload {
|
|
143
|
+
message: string;
|
|
144
|
+
}
|
|
145
|
+
|
|
146
|
+
export type TextTrace = RuntimeTrace<'text', TextTracePayload>;
|
|
147
|
+
|
|
148
|
+
export const isTextTrace = (value: unknown): value is TextTrace => isRuntimeTrace(value) && value.type === 'text';
|
|
149
|
+
|
|
150
|
+
export interface VisualTracePayload {
|
|
151
|
+
image: string;
|
|
152
|
+
}
|
|
153
|
+
|
|
154
|
+
export type VisualTrace = RuntimeTrace<'visual', VisualTracePayload>;
|
|
155
|
+
|
|
156
|
+
export const isVisualTrace = (value: unknown): value is VisualTrace => isRuntimeTrace(value) && value.type === 'visual';
|
|
157
|
+
|
|
158
|
+
export interface StreamTracePayload {
|
|
159
|
+
src: string;
|
|
160
|
+
loop: boolean;
|
|
161
|
+
description?: string;
|
|
162
|
+
title?: string;
|
|
163
|
+
iconImage?: string;
|
|
164
|
+
backgroundImage?: string;
|
|
165
|
+
action: string;
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
export type StreamTrace = RuntimeTrace<'stream', StreamTracePayload>;
|
|
169
|
+
|
|
170
|
+
export const isStreamTrace = (value: unknown): value is StreamTrace => isRuntimeTrace(value) && value.type === 'stream';
|
|
171
|
+
|
|
172
|
+
export interface RuntimeHttpRequest {
|
|
173
|
+
method?: string;
|
|
174
|
+
params?: URLSearchParams;
|
|
175
|
+
body?: any;
|
|
176
|
+
headers?: HeadersInit;
|
|
177
|
+
}
|
|
@@ -0,0 +1,54 @@
|
|
|
1
|
+
import createHTTPError from 'http-errors';
|
|
2
|
+
|
|
3
|
+
import type {
|
|
4
|
+
RuntimeHttpRequest,
|
|
5
|
+
RuntimeInteractRequest,
|
|
6
|
+
RuntimeInteractResponse,
|
|
7
|
+
RuntimeOptions,
|
|
8
|
+
} from '@/runtime/runtime.interface.js';
|
|
9
|
+
|
|
10
|
+
export class RuntimeService {
|
|
11
|
+
private readonly fetch: typeof globalThis.fetch;
|
|
12
|
+
|
|
13
|
+
public constructor(private readonly options: RuntimeOptions) {
|
|
14
|
+
this.fetch = options.fetchPonyfill ?? globalThis.fetch?.bind(globalThis);
|
|
15
|
+
if (!this.fetch) {
|
|
16
|
+
throw new TypeError('fetch implementation was not provided and a global fetch was not available');
|
|
17
|
+
}
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
private async send<T>(path: string, args: RuntimeHttpRequest = {}): Promise<T> {
|
|
21
|
+
const url = new URL(path, this.options.url);
|
|
22
|
+
if (args.params) url.search = args.params.toString();
|
|
23
|
+
|
|
24
|
+
const result = await this.fetch(url, {
|
|
25
|
+
method: args.method ?? 'GET',
|
|
26
|
+
body: args.body ? JSON.stringify(args.body) : undefined,
|
|
27
|
+
headers: {
|
|
28
|
+
'content-type': 'application/json',
|
|
29
|
+
...(args.headers ?? {}),
|
|
30
|
+
},
|
|
31
|
+
});
|
|
32
|
+
|
|
33
|
+
const json = (await result.json().catch(() => null)) as any;
|
|
34
|
+
if (!result.ok) {
|
|
35
|
+
throw createHTTPError(result.status, result.statusText, json);
|
|
36
|
+
}
|
|
37
|
+
|
|
38
|
+
return json;
|
|
39
|
+
}
|
|
40
|
+
|
|
41
|
+
public async interact(request: RuntimeInteractRequest): Promise<RuntimeInteractResponse> {
|
|
42
|
+
return this.send<RuntimeInteractResponse>(`state/${request.versionID}/user/${request.sessionID}/interact`, {
|
|
43
|
+
method: 'POST',
|
|
44
|
+
body: {
|
|
45
|
+
action: request.action,
|
|
46
|
+
config: request.config,
|
|
47
|
+
},
|
|
48
|
+
headers: {
|
|
49
|
+
authorization: this.options.authorization,
|
|
50
|
+
},
|
|
51
|
+
params: new URLSearchParams({ verbose: 'true' }),
|
|
52
|
+
});
|
|
53
|
+
}
|
|
54
|
+
}
|
package/src/sdk/index.ts
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
import type { IntentActionPayload, RuntimeInteractRequest } from '@/runtime/runtime.interface.js';
|
|
2
|
+
import { createIntentAction } from '@/runtime/runtime.interface.js';
|
|
3
|
+
import { RuntimeService } from '@/runtime/runtime.service.js';
|
|
4
|
+
import type { TraceDeclaration } from '@/trace/trace.interface.js';
|
|
5
|
+
import { TraceService } from '@/trace/trace.service.js';
|
|
6
|
+
|
|
7
|
+
import type { VoiceflowRuntimeOptions } from './sdk.interface.js';
|
|
8
|
+
|
|
9
|
+
export class VoiceflowRuntime<T> {
|
|
10
|
+
private readonly runtime: RuntimeService;
|
|
11
|
+
|
|
12
|
+
private readonly trace: TraceService<T>;
|
|
13
|
+
|
|
14
|
+
public constructor(options: VoiceflowRuntimeOptions<T>) {
|
|
15
|
+
this.runtime = new RuntimeService(options);
|
|
16
|
+
this.trace = new TraceService(options);
|
|
17
|
+
}
|
|
18
|
+
|
|
19
|
+
public registerStep(step: TraceDeclaration<T, any>) {
|
|
20
|
+
this.trace.registerTrace(step);
|
|
21
|
+
return this;
|
|
22
|
+
}
|
|
23
|
+
|
|
24
|
+
public async interact(context: T, request: RuntimeInteractRequest): Promise<T> {
|
|
25
|
+
const response = await this.runtime.interact(request);
|
|
26
|
+
return this.trace.processTrace(context, request, response);
|
|
27
|
+
}
|
|
28
|
+
|
|
29
|
+
public intent(request: { context: T; sessionID: string; payload: IntentActionPayload }): Promise<T> {
|
|
30
|
+
return this.interact(request.context, {
|
|
31
|
+
action: createIntentAction(request.payload),
|
|
32
|
+
sessionID: request.sessionID,
|
|
33
|
+
});
|
|
34
|
+
}
|
|
35
|
+
}
|
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
export * from './card.component.js';
|
|
2
|
+
export * from './choice.component.js';
|
|
3
|
+
export * from './end.component.js';
|
|
4
|
+
export * from './payload.component.js';
|
|
5
|
+
export * from './speak.component.js';
|
|
6
|
+
export * from './stream.component.js';
|
|
7
|
+
export * from './text.component.js';
|
|
8
|
+
export * from './visual.component.js';
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RuntimeTrace } from '@/runtime/runtime.interface.js';
|
|
2
|
+
|
|
3
|
+
import type { TraceAccumulator, TraceDeclaration } from './trace.interface.js';
|
|
4
|
+
|
|
5
|
+
export interface TraceComponentOverload<Trace extends RuntimeTrace> {
|
|
6
|
+
<Context>(declaration: TraceDeclaration<Context, Trace>): Required<TraceDeclaration<Context, Trace>>;
|
|
7
|
+
<Context>(handle: TraceAccumulator<Context, Trace>): Required<TraceDeclaration<Context, Trace>>;
|
|
8
|
+
}
|
|
9
|
+
|
|
10
|
+
export const createTraceComponent =
|
|
11
|
+
<Trace extends RuntimeTrace>(type: Trace['type']): TraceComponentOverload<Trace> =>
|
|
12
|
+
<Context>(
|
|
13
|
+
handle: TraceDeclaration<Context, Trace> | TraceAccumulator<Context, Trace>
|
|
14
|
+
): TraceDeclaration<Context, Trace> => {
|
|
15
|
+
if (typeof handle === 'function') {
|
|
16
|
+
return {
|
|
17
|
+
canHandle: (trace) => trace.type === type,
|
|
18
|
+
handle,
|
|
19
|
+
};
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
return handle;
|
|
23
|
+
};
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
import type { RuntimeInteractRequest, RuntimeState, RuntimeTrace } from '@/runtime/runtime.interface';
|
|
2
|
+
|
|
3
|
+
export interface TraceOptions<T> {
|
|
4
|
+
traces?: TraceDeclaration<T, any>[];
|
|
5
|
+
}
|
|
6
|
+
|
|
7
|
+
export interface TraceDeclaration<Context, Trace extends RuntimeTrace> {
|
|
8
|
+
canHandle: TraceCanHandle<Trace>;
|
|
9
|
+
handle: TraceAccumulator<Context, Trace>;
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
export type TraceCanHandle<Trace extends RuntimeTrace> = (trace: Trace) => boolean;
|
|
13
|
+
|
|
14
|
+
export interface TraceHandlerMeta<Context> {
|
|
15
|
+
request: RuntimeInteractRequest;
|
|
16
|
+
state: RuntimeState;
|
|
17
|
+
context: Context;
|
|
18
|
+
}
|
|
19
|
+
|
|
20
|
+
export type TraceAccumulator<Context, Trace extends RuntimeTrace> = (
|
|
21
|
+
meta: TraceHandlerMeta<Context>,
|
|
22
|
+
trace: Trace
|
|
23
|
+
) => Context;
|
|
@@ -0,0 +1,42 @@
|
|
|
1
|
+
import type { RuntimeInteractRequest, RuntimeInteractResponse } from '@/runtime/runtime.interface.js';
|
|
2
|
+
|
|
3
|
+
import type { TraceDeclaration, TraceHandlerMeta, TraceOptions } from './trace.interface';
|
|
4
|
+
|
|
5
|
+
export class TraceService<T = unknown> {
|
|
6
|
+
private readonly traces: TraceDeclaration<T, any>[] = [];
|
|
7
|
+
|
|
8
|
+
public constructor(options: TraceOptions<T> = {}) {
|
|
9
|
+
this.registerTraces(options.traces ?? []);
|
|
10
|
+
}
|
|
11
|
+
|
|
12
|
+
public registerTrace(step: TraceDeclaration<T, any>): this {
|
|
13
|
+
this.traces.push(step);
|
|
14
|
+
return this;
|
|
15
|
+
}
|
|
16
|
+
|
|
17
|
+
public registerTraces(steps: TraceDeclaration<T, any>[]): this {
|
|
18
|
+
steps.forEach((step) => this.registerTrace(step));
|
|
19
|
+
return this;
|
|
20
|
+
}
|
|
21
|
+
|
|
22
|
+
public async processTrace(
|
|
23
|
+
context: T,
|
|
24
|
+
request: RuntimeInteractRequest,
|
|
25
|
+
response: RuntimeInteractResponse
|
|
26
|
+
): Promise<T> {
|
|
27
|
+
const meta: TraceHandlerMeta<T> = {
|
|
28
|
+
request,
|
|
29
|
+
state: response.state,
|
|
30
|
+
context,
|
|
31
|
+
};
|
|
32
|
+
|
|
33
|
+
for (const trace of response.trace) {
|
|
34
|
+
const step = this.traces.find((step) => step.canHandle(trace));
|
|
35
|
+
if (step) {
|
|
36
|
+
// eslint-disable-next-line no-await-in-loop
|
|
37
|
+
meta.context = await step.handle(meta, trace);
|
|
38
|
+
}
|
|
39
|
+
}
|
|
40
|
+
return meta.context;
|
|
41
|
+
}
|
|
42
|
+
}
|
package/tsconfig.json
ADDED
|
@@ -0,0 +1,11 @@
|
|
|
1
|
+
{
|
|
2
|
+
"$schema": "https://json.schemastore.org/tsconfig",
|
|
3
|
+
"extends": "@voiceflow-meta/typescript-config/base.json",
|
|
4
|
+
"compilerOptions": {
|
|
5
|
+
"baseUrl": "./",
|
|
6
|
+
"outDir": "./build",
|
|
7
|
+
"stripInternal": true
|
|
8
|
+
},
|
|
9
|
+
"include": ["src/**/*", "test"],
|
|
10
|
+
"exclude": ["node_modules", "build", "src/**/*.test-d.ts"]
|
|
11
|
+
}
|