Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: persona variable state support (CT-2093) #547

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions lib/clients/dataAPI.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import { VoiceflowProgram, VoiceflowVersion } from '@voiceflow/voiceflow-types';
import { BaseModels } from '@voiceflow/base-types';
import { VoiceflowProgram, VoiceflowProject, VoiceflowVersion } from '@voiceflow/voiceflow-types';

import { CreatorDataApi, LocalDataApi } from '@/runtime';
import { Config } from '@/types';
Expand All @@ -17,7 +18,14 @@ class DataAPI {

creatorAPIAuthorization?: string;

creatorDataApi?: (authorization: string) => CreatorDataApi<VoiceflowProgram.Program, VoiceflowVersion.Version>;
creatorDataApi?: (
authorization: string
) => CreatorDataApi<
VoiceflowProgram.Program,
VoiceflowVersion.Version,
VoiceflowProject.Project,
BaseModels.VariableState.Model
>;

constructor(
{ config, mongo }: { config: Config; mongo: MongoDB | null },
Expand Down
1 change: 0 additions & 1 deletion lib/services/interact/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -92,7 +92,6 @@ class Interact extends AbstractManager<{ utils: typeof utils }> {
if (config.selfDelegate) {
return turn.resolve(turn.handle(context));
}

return turn.resolve(this.services.utils.autoDelegate(turn, context));
}
}
Expand Down
4 changes: 4 additions & 0 deletions lib/services/state/cacheDataAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,10 @@ class CacheDataAPI implements DataAPI<VoiceflowProgram.Program, VoiceflowVersion
return this.api.getProgram(programID);
}

async getVariableState(variableStateID: string) {
return this.api.getVariableState(variableStateID);
}

async getVersion(versionID: string) {
if (!(versionID in this.versions)) {
this.versions[versionID] = await this.api.getVersion(versionID);
Expand Down
18 changes: 15 additions & 3 deletions lib/services/state/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,24 @@ class StateManager extends AbstractManager<{ utils: typeof utils }> implements I
throw new Error('context versionID not defined');
}

// cache per interaction (save version call during request/response cycle)
const dataApi = await this.services.dataAPI.get(context.data?.reqHeaders?.authorization);
const api = new CacheDataAPI(dataApi);

if (context.request && BaseRequest.isLaunchRequest(context.request) && context.state) {
context.state.stack = [];
context.state.storage = {};

// Initialize variables to given persona variable state
if (context.request.payload?.persona) {
const personaVariableState = await api.getVariableState(context.request.payload.persona).catch(() => null);

context.state!.variables = {
...context.state!.variables,
...personaVariableState?.variables,
};
}

context.request = null;
}

Expand All @@ -93,9 +108,6 @@ class StateManager extends AbstractManager<{ utils: typeof utils }> implements I
context.state!.stack = [];
}

// cache per interaction (save version call during request/response cycle)
const dataApi = await this.services.dataAPI.get(context.data?.reqHeaders?.authorization);
const api = new CacheDataAPI(dataApi);
const version = await api.getVersion(context.versionID!);
const project = await api.getProject(version.projectID);

Expand Down
18 changes: 9 additions & 9 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,23 +15,23 @@
"@aws-sdk/client-s3": "^3.179.0",
"@opentelemetry/api-metrics": "0.26.0",
"@types/workerpool": "^5.0.1",
"@voiceflow/alexa-types": "2.13.23",
"@voiceflow/alexa-types": "2.13.28",
"@voiceflow/anthropic": "^0.4.3",
"@voiceflow/api-sdk": "3.23.27",
"@voiceflow/api-sdk": "3.24.0",
"@voiceflow/backend-utils": "4.8.5",
"@voiceflow/base-types": "2.84.3",
"@voiceflow/base-types": "2.86.0",
"@voiceflow/body-parser": "^1.21.1",
"@voiceflow/chat-types": "2.13.48",
"@voiceflow/chat-types": "2.13.53",
"@voiceflow/common": "8.2.0",
"@voiceflow/event-ingestion-service": "1.2.3",
"@voiceflow/google-types": "2.19.23",
"@voiceflow/google-types": "2.19.28",
"@voiceflow/logger": "^1.6.1",
"@voiceflow/metrics": "1.4.1",
"@voiceflow/natural-language-commander": "^0.5.1",
"@voiceflow/openai": "^3.2.1",
"@voiceflow/verror": "^1.1.3",
"@voiceflow/voice-types": "2.9.29",
"@voiceflow/voiceflow-types": "3.23.34",
"@voiceflow/voice-types": "2.9.34",
"@voiceflow/voiceflow-types": "3.24.4",
"ajv": "6.12.3",
"axios": "^0.21.1",
"compression": "^1.7.4",
Expand Down Expand Up @@ -94,9 +94,9 @@
"@types/sinon": "^7.5.1",
"@types/supertest": "^2.0.8",
"@types/validator": "^13.7.0",
"@voiceflow/commitlint-config": "2.0.0",
"@voiceflow/commitlint-config": "2.1.0",
"@voiceflow/eslint-config": "6.1.0",
"@voiceflow/git-branch-check": "1.4.0",
"@voiceflow/git-branch-check": "1.4.1",
"@voiceflow/prettier-config": "1.2.1",
"chai": "^4.2.0",
"chai-as-promised": "^7.1.1",
Expand Down
9 changes: 7 additions & 2 deletions runtime/lib/DataAPI/creatorDataAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ import { extractAPIKeyID } from './utils';
class CreatorDataAPI<
P extends BaseModels.Program.Model<any, any> = BaseModels.Program.Model<any, any>,
V extends BaseModels.Version.Model<any> = BaseModels.Version.Model<any>,
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>
> implements DataAPI<P, V, PJ>
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>,
VS extends BaseModels.VariableState.Model = BaseModels.VariableState.Model
> implements DataAPI<P, V, PJ, VS>
{
protected client: Client;

Expand Down Expand Up @@ -52,6 +53,10 @@ class CreatorDataAPI<
return (await this.client.version.get(versionID)) as V;
};

public getVariableState = async (variableStateID: string) => {
return (await this.client.variableState.get(variableStateID)) as VS;
};

public getProject = async (ref: string) => {
// reference could be either projectID or apiKey
const apiKeyID = extractAPIKeyID(CreatorDataAPI.isolateAPIKey(ref));
Expand Down
10 changes: 8 additions & 2 deletions runtime/lib/DataAPI/localDataAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,18 @@ import { DataAPI } from './types';
class LocalDataAPI<
P extends BaseModels.Program.Model<any, any> = BaseModels.Program.Model<any, any>,
V extends BaseModels.Version.Model<any> = BaseModels.Version.Model<any>,
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>
> implements DataAPI<P, V, PJ>
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>,
VS extends BaseModels.VariableState.Model = BaseModels.VariableState.Model
> implements DataAPI<P, V, PJ, VS>
{
private version: V;

private project: PJ;

private programs: Record<string, P>;

private variableState: VS;

constructor({ projectSource }: { projectSource: string }, { fs, path }: { fs: typeof FS; path: typeof Path }) {
if (!projectSource) throw new Error('project source undefined');

Expand All @@ -24,13 +27,16 @@ class LocalDataAPI<
this.version = content.version;
this.project = content.project;
this.programs = content.programs;
this.variableState = content.variableState;
}

public getVersion = async () => this.version;

public getProgram = async (programID: string) => this.programs[programID];

public getProject = async () => this.project;

public getVariableState = async () => this.variableState;
}

export default LocalDataAPI;
15 changes: 14 additions & 1 deletion runtime/lib/DataAPI/mongoDataAPI.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ interface Client {
class MongoDataAPI<
P extends BaseModels.Program.Model<any, any>,
V extends BaseModels.Version.Model<any>,
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>,
VS extends BaseModels.VariableState.Model = BaseModels.VariableState.Model
> implements DataAPI<P, V, PJ>
{
protected client: Client;
Expand All @@ -32,6 +33,8 @@ class MongoDataAPI<

protected projectsCollection = 'projects';

protected variableStatesCollection = 'variable-states';

constructor(client: Client) {
this.client = client;
}
Expand Down Expand Up @@ -60,6 +63,16 @@ class MongoDataAPI<
return shallowObjectIdToString(version);
};

public getVariableState = async (variableStateID: string): Promise<VS> => {
const persona = await this.client.db
.collection(this.variableStatesCollection)
.findOne<(VS & { _id: ObjectId; projectID: ObjectId }) | null>({ _id: new ObjectId(variableStateID) });

if (!persona) throw new Error(`Persona not found: ${variableStateID}`);

return shallowObjectIdToString(persona);
};

public getProject = async (projectID: string) => {
const project = await this.client.db
.collection(this.projectsCollection)
Expand Down
5 changes: 4 additions & 1 deletion runtime/lib/DataAPI/types.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,14 @@ export interface Display {
export interface DataAPI<
P extends BaseModels.Program.Model<any, any> = BaseModels.Program.Model<BaseModels.BaseNode, BaseModels.BaseCommand>,
V extends BaseModels.Version.Model<any> = BaseModels.Version.Model<BaseModels.Version.PlatformData>,
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>
PJ extends BaseModels.Project.Model<any, any> = BaseModels.Project.Model<AnyRecord, AnyRecord>,
VS = BaseModels.VariableState.Model
> {
getProgram(programID: string): Promise<P>;

getVersion(versionID: string): Promise<V>;

getProject(projectID: string): Promise<PJ>;

getVariableState(variableStateID: string): Promise<VS>;
}
Loading
Loading