Skip to content

Commit

Permalink
Support hosted triggers
Browse files Browse the repository at this point in the history
  • Loading branch information
peterszerzo committed Jun 24, 2024
1 parent 7746d3c commit c8a1162
Showing 1 changed file with 42 additions and 8 deletions.
50 changes: 42 additions & 8 deletions packages/journey-manager/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -187,9 +187,10 @@ export interface RunProps {
*/
ui?: UiConfig;
/**
* The triggers dictionary, downloaded from the Dialog Studio desktop app
* The triggers dictionary, downloaded from the Dialog Studio desktop app.
* If triggers are not provided, they will be fetched from the CDN.
*/
triggers: Triggers;
triggers?: Triggers;
/**
* Digression detection callback
*/
Expand All @@ -209,14 +210,47 @@ function filterMap<X, Y>(
}, []);
}

const resolveTriggers = async (
config: Config,
triggers?: Triggers,
): Promise<Triggers> => {
if (triggers != null) {
return triggers;
}
const baseUrl =
config.dev ?? false
? "https://triggers.dev.nlx.ai"
: "https://triggers.nlx.ai";
const triggersFromCdnRequest = await fetch(
`${baseUrl}/${config.workspaceId}/${config.journeyId}.json`,
);
const triggersFromCdn = await triggersFromCdnRequest.json();
return triggersFromCdn;
};

const waitUntilDomContentLoaded = (): Promise<unknown> => {
return new Promise((resolve) => {
window.addEventListener("DOMContentLoaded", (event) => {
resolve(event);
});
});
};

/**
* Run the multimodal journey
* @param props - The run configuration object
* @returns an object containing a teardown function and the multimodal client.
* @returns an promise of an object containing a teardown function and the multimodal client.
*/
export const run = (props: RunProps): RunOutput => {
export const run = async (props: RunProps): Promise<RunOutput> => {
const client = create(props.config);

const triggers: Triggers = await resolveTriggers(
props.config,
props.triggers,
);

await waitUntilDomContentLoaded();

const triggeredSteps = getTriggeredSteps(props.config.conversationId);

/**
Expand All @@ -226,13 +260,13 @@ export const run = (props: RunProps): RunOutput => {
*/

const urlConditions: UrlCondition[] = filterMap(
Object.values(props.triggers),
Object.values(triggers),
(trigger) => trigger.urlCondition,
);

// If there are any steps for which there is no URL condition while also not being used for escalation or end,
// the package assumes that a digression cannot be reliably detected.
const isDigressionDetectable = Object.entries(props.triggers).every(
const isDigressionDetectable = Object.entries(triggers).every(
([stepId, trigger]) => {
return (
// every step has a URL condition
Expand Down Expand Up @@ -265,7 +299,7 @@ export const run = (props: RunProps): RunOutput => {
});
};

const loadSteps: LoadStep[] = Object.entries(props.triggers).reduce(
const loadSteps: LoadStep[] = Object.entries(triggers).reduce(
(prev: LoadStep[], [stepId, trigger]: [StepId, Trigger]) => {
if (trigger.event === "pageLoad") {
return [
Expand All @@ -285,7 +319,7 @@ export const run = (props: RunProps): RunOutput => {
sendStep(stepId, once ?? false);
});

const clickSteps: ClickStep[] = Object.entries(props.triggers).reduce(
const clickSteps: ClickStep[] = Object.entries(triggers).reduce(
(prev: ClickStep[], [stepId, trigger]: [StepId, Trigger]) => {
if (trigger.event === "click" && trigger.query != null) {
const newEntry: ClickStep = {
Expand Down

0 comments on commit c8a1162

Please sign in to comment.