Skip to content
Permalink

Comparing changes

Choose two branches to see what’s changed or to start a new pull request. If you need to, you can also or learn more about diff comparisons.

Open a pull request

Create a new pull request by comparing changes across two branches. If you need to, you can also . Learn more about diff comparisons here.
base repository: voiceflow/libs
Failed to load repositories. Confirm that selected base ref is valid, then try again.
Loading
base: @voiceflow/common@8.3.2
Choose a base ref
...
head repository: voiceflow/libs
Failed to load repositories. Confirm that selected head ref is valid, then try again.
Loading
compare: @voiceflow/common@8.4.0
Choose a head ref
  • 6 commits
  • 19 files changed
  • 3 contributors

Commits on Dec 12, 2024

  1. fix: separate event imports in command.ts (RUN-457) (#566)

    <!-- You can erase any parts of this template not applicable to your Pull Request. -->
    
    **Fixes or implements RUN-457**
    
    Fix is for [this error](https://app.circleci.com/pipelines/github/voiceflow/creator-api/24229/workflows/65cee781-6df7-405e-bc77-511468ebb869/jobs/52200) when upgrading libs packages.
    Sanasar Janjughazyan committed Dec 12, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    lsf37 Gerwin Klein
    Copy the full SHA
    ea40ea3 View commit details
  2. chore(release): publish

     - @voiceflow/alexa-types@2.15.40
     - @voiceflow/api-sdk@3.28.38
     - @voiceflow/base-types@2.127.2
     - @voiceflow/chat-types@2.14.38
     - @voiceflow/voice-types@2.10.38
     - @voiceflow/voiceflow-types@3.32.25
    vf-service-account committed Dec 12, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    lsf37 Gerwin Klein
    Copy the full SHA
    030dd9a View commit details

Commits on Dec 13, 2024

  1. feat: update slate variable element (DLC-517) (#567)

    <!-- You can erase any parts of this template not applicable to your Pull Request. -->
    
    **Fixes or implements VF-XXX**
    
    ### Brief description. What is this change?
    
    <!-- Build up some context for your teammates on the changes made here and potential tradeoffs made and/or highlight any topics for discussion -->
    pmvrmc committed Dec 13, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    lsf37 Gerwin Klein
    Copy the full SHA
    fad3dd7 View commit details
  2. chore(release): publish

     - @voiceflow/alexa-types@2.15.41
     - @voiceflow/api-sdk@3.28.39
     - @voiceflow/base-types@2.128.0
     - @voiceflow/chat-types@2.14.39
     - @voiceflow/voice-types@2.10.39
     - @voiceflow/voiceflow-types@3.32.26
    vf-service-account committed Dec 13, 2024

    Verified

    This commit was signed with the committer’s verified signature. The key has expired.
    lsf37 Gerwin Klein
    Copy the full SHA
    c5387f7 View commit details

Commits on Dec 16, 2024

  1. feat: update replace variable (dlc-504) (#568)

    <!-- You can erase any parts of this template not applicable to your Pull Request. -->
    
    **Fixes or implements VF-XXX**
    
    ### Brief description. What is this change?
    
    <!-- Build up some context for your teammates on the changes made here and potential tradeoffs made and/or highlight any topics for discussion -->
    pmvrmc committed Dec 16, 2024
    Copy the full SHA
    b3f163d View commit details
  2. chore(release): publish

     - @voiceflow/alexa-types@2.15.42
     - @voiceflow/api-sdk@3.28.40
     - @voiceflow/base-types@2.128.1
     - @voiceflow/chat-types@2.14.40
     - @voiceflow/common@8.4.0
     - @voiceflow/voice-types@2.10.40
     - @voiceflow/voiceflow-types@3.32.27
    vf-service-account committed Dec 16, 2024
    Copy the full SHA
    60ae823 View commit details
12 changes: 12 additions & 0 deletions packages/alexa-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.15.42](https://github.com/voiceflow/libs/compare/@voiceflow/alexa-types@2.15.41...@voiceflow/alexa-types@2.15.42) (2024-12-16)

**Note:** Version bump only for package @voiceflow/alexa-types

## [2.15.41](https://github.com/voiceflow/libs/compare/@voiceflow/alexa-types@2.15.40...@voiceflow/alexa-types@2.15.41) (2024-12-13)

**Note:** Version bump only for package @voiceflow/alexa-types

## [2.15.40](https://github.com/voiceflow/libs/compare/@voiceflow/alexa-types@2.15.39...@voiceflow/alexa-types@2.15.40) (2024-12-12)

**Note:** Version bump only for package @voiceflow/alexa-types

## [2.15.39](https://github.com/voiceflow/libs/compare/@voiceflow/alexa-types@2.15.38...@voiceflow/alexa-types@2.15.39) (2024-12-12)

**Note:** Version bump only for package @voiceflow/alexa-types
2 changes: 1 addition & 1 deletion packages/alexa-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voiceflow/alexa-types",
"version": "2.15.39",
"version": "2.15.42",
"description": "Alexa service types",
"keywords": [
"voiceflow",
12 changes: 12 additions & 0 deletions packages/api-sdk/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [3.28.40](https://github.com/voiceflow/libs/compare/@voiceflow/api-sdk@3.28.39...@voiceflow/api-sdk@3.28.40) (2024-12-16)

**Note:** Version bump only for package @voiceflow/api-sdk

## [3.28.39](https://github.com/voiceflow/libs/compare/@voiceflow/api-sdk@3.28.38...@voiceflow/api-sdk@3.28.39) (2024-12-13)

**Note:** Version bump only for package @voiceflow/api-sdk

## [3.28.38](https://github.com/voiceflow/libs/compare/@voiceflow/api-sdk@3.28.37...@voiceflow/api-sdk@3.28.38) (2024-12-12)

**Note:** Version bump only for package @voiceflow/api-sdk

## [3.28.37](https://github.com/voiceflow/libs/compare/@voiceflow/api-sdk@3.28.36...@voiceflow/api-sdk@3.28.37) (2024-12-12)

**Note:** Version bump only for package @voiceflow/api-sdk
2 changes: 1 addition & 1 deletion packages/api-sdk/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voiceflow/api-sdk",
"version": "3.28.37",
"version": "3.28.40",
"description": "wrapper for creator-api",
"keywords": [
"voiceflow"
16 changes: 16 additions & 0 deletions packages/base-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,22 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.128.1](https://github.com/voiceflow/libs/compare/@voiceflow/base-types@2.128.0...@voiceflow/base-types@2.128.1) (2024-12-16)

**Note:** Version bump only for package @voiceflow/base-types

# [2.128.0](https://github.com/voiceflow/libs/compare/@voiceflow/base-types@2.127.2...@voiceflow/base-types@2.128.0) (2024-12-13)

### Features

* update slate variable element (DLC-517) ([#567](https://github.com/voiceflow/libs/issues/567)) ([fad3dd7](https://github.com/voiceflow/libs/commit/fad3dd74065cc01098ccfe7dcd0351b376823f6d))

## [2.127.2](https://github.com/voiceflow/libs/compare/@voiceflow/base-types@2.127.1...@voiceflow/base-types@2.127.2) (2024-12-12)

### Bug Fixes

* separate event imports in command.ts (RUN-457) ([#566](https://github.com/voiceflow/libs/issues/566)) ([ea40ea3](https://github.com/voiceflow/libs/commit/ea40ea3cfdcea8404071cade2847cc4dd112a1d0))

## [2.127.1](https://github.com/voiceflow/libs/compare/@voiceflow/base-types@2.127.0...@voiceflow/base-types@2.127.1) (2024-12-12)

**Note:** Version bump only for package @voiceflow/base-types
2 changes: 1 addition & 1 deletion packages/base-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voiceflow/base-types",
"version": "2.127.1",
"version": "2.128.1",
"description": "Voiceflow base project types",
"keywords": [
"voiceflow",
3 changes: 2 additions & 1 deletion packages/base-types/src/node/utils/command.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
import type { Nullable } from '@voiceflow/common';

import type { BaseCommand } from './base';
import { type BaseEvent, isBaseEvent } from './event';
import type { BaseEvent } from './event';
import { isBaseEvent } from './event';

// BUILT IN COMMANDS
export enum CommandType {
1 change: 1 addition & 0 deletions packages/base-types/src/text/index.ts
Original file line number Diff line number Diff line change
@@ -50,6 +50,7 @@ export interface LinkElement extends Element {
export interface VariableElement extends Element {
type: ElementType.VARIABLE;
id: string;
path?: string;
name: string;
isSlot?: boolean;
isSecret?: boolean;
12 changes: 12 additions & 0 deletions packages/chat-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.14.40](https://github.com/voiceflow/libs/compare/@voiceflow/chat-types@2.14.39...@voiceflow/chat-types@2.14.40) (2024-12-16)

**Note:** Version bump only for package @voiceflow/chat-types

## [2.14.39](https://github.com/voiceflow/libs/compare/@voiceflow/chat-types@2.14.38...@voiceflow/chat-types@2.14.39) (2024-12-13)

**Note:** Version bump only for package @voiceflow/chat-types

## [2.14.38](https://github.com/voiceflow/libs/compare/@voiceflow/chat-types@2.14.37...@voiceflow/chat-types@2.14.38) (2024-12-12)

**Note:** Version bump only for package @voiceflow/chat-types

## [2.14.37](https://github.com/voiceflow/libs/compare/@voiceflow/chat-types@2.14.36...@voiceflow/chat-types@2.14.37) (2024-12-12)

**Note:** Version bump only for package @voiceflow/chat-types
2 changes: 1 addition & 1 deletion packages/chat-types/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voiceflow/chat-types",
"version": "2.14.37",
"version": "2.14.40",
"description": "Voiceflow chat project types",
"keywords": [
"voiceflow",
6 changes: 6 additions & 0 deletions packages/common/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,12 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

# [8.4.0](https://github.com/voiceflow/libs/compare/@voiceflow/common@8.3.2...@voiceflow/common@8.4.0) (2024-12-16)

### Features

* update replace variable (dlc-504) ([#568](https://github.com/voiceflow/libs/issues/568)) ([b3f163d](https://github.com/voiceflow/libs/commit/b3f163d19f311cf411c16678b53d113562c39583))

## [8.3.2](https://github.com/voiceflow/libs/compare/@voiceflow/common@8.3.1...@voiceflow/common@8.3.2) (2024-12-12)

### Bug Fixes
2 changes: 1 addition & 1 deletion packages/common/package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "@voiceflow/common",
"version": "8.3.2",
"version": "8.4.0",
"description": "Junk drawer of utility functions",
"keywords": [
"voiceflow"
4 changes: 3 additions & 1 deletion packages/common/src/constants/regexp.ts
Original file line number Diff line number Diff line change
@@ -6,7 +6,9 @@ export const SLOT_ANNOTATION_SIMPLE_REGEX = /{([^ .[\]{}]+?)}/g;

export const IS_VARIABLE_REGEXP = /^{.*}$/;

export const READABLE_VARIABLE_REGEXP = /{(\w{1,64})}/g;
export const READABLE_VARIABLE_REGEXP = /{(\w{1,64}((\[\d+])*(\.\w{1,64})*)*)}/g;

export const VARIABLE_ONLY_REGEXP = new RegExp(`^${READABLE_VARIABLE_REGEXP.source}$`);

export const VALID_CHARACTER = 'a-zA-Z';

128 changes: 128 additions & 0 deletions packages/common/src/utils/variables.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,128 @@
import { describe, expect, it } from 'vitest';

import { replaceVariables, splitVariableName } from './variables';

describe('Utils | variables', () => {
describe('splitVariableNameName()', () => {
it('works if no path', () => {
const { id, path } = splitVariableName('name');
expect(id).toBe('name');
expect(path).toBe('');
});

it('works with dot notation', () => {
const { id, path } = splitVariableName('name.first');
expect(id).toBe('name');
expect(path).toBe('first');
});

it('works with dot notation (and bracket)', () => {
const { id, path } = splitVariableName('name.first[0].last');
expect(id).toBe('name');
expect(path).toBe('first[0].last');
});

it('works with bracket notation', () => {
const { id, path } = splitVariableName('name[0]');
expect(id).toBe('name');
expect(path).toBe('[0]');
});

it('works with bracket notation (and dot)', () => {
const { id, path } = splitVariableName('name[0].first.last');
expect(id).toBe('name');
expect(path).toBe('[0].first.last');
});
});

describe('replaceVariables()', () => {
it('do with ... ?', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}....aasdasd...something}!`, {});
expect(replacedVariable).toBe('Hello, {name....aasdasd...something}!');
});
it('defaults to match if it cant map', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.something}!`, {});
expect(replacedVariable).toBe('Hello, {name.something}!');
});

it('replaces a variable', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}}!`, { [variable]: 'world' });
expect(replacedVariable).toBe('Hello, world!');
});

it('replaces an object variable with a path', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.name}!`, { [variable]: { name: 'world' } });
expect(replacedVariable).toBe('Hello, world!');
});

it('replaces an object variable with a complex path', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.users[0].friends[0].name}!`, {
[variable]: { users: [{ friends: [{ name: 'world' }] }] },
});
expect(replacedVariable).toBe('Hello, world!');
});

it('replaces an object variable with a complex path (bracket access)', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}[0].friends[0].name}!`, {
[variable]: [{ friends: [{ name: 'world' }] }],
});
expect(replacedVariable).toBe('Hello, world!');
});

it('replaces an object-like-string variable with a path', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.name}!`, {
[variable]: '{ "name": invalid-json }',
});
expect(replacedVariable).toBe('Hello, 0!');
});

it('replaces an object-like-string variable with a complex path', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.users[0].friends[0].name}!`, {
[variable]: '{ "users": [{ "friends": [{ "name": "world" }] }] }',
});
expect(replacedVariable).toBe('Hello, world!');
});

it('replaces an object-like-string variable with a complex path (bracket access)', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}[0].friends[0].name}!`, {
[variable]: '[{ "friends": [{ "name": "world" }] }]',
});
expect(replacedVariable).toBe('Hello, world!');
});

it('works with modifier', () => {
const variable = 'name';
const replacedVariable = replaceVariables(
`{${variable}.name}`,
{ [variable]: { name: 'world' } },
JSON.stringify
);
expect(replacedVariable).toBe('"world"');
});

it('returns toString() of path match, even in cases of object', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.name}!`, {
[variable]: { name: { first: 'john', last: 'doe' } },
});
expect(replacedVariable).toBe('Hello, [object Object]!');
});

it('returns toString() of path match, even in cases of arrays', () => {
const variable = 'name';
const replacedVariable = replaceVariables(`Hello, {${variable}.name}!`, {
[variable]: { name: ['mr', 'john', 'doe'] },
});
expect(replacedVariable).toBe('Hello, mr,john,doe!');
});
});
});
66 changes: 60 additions & 6 deletions packages/common/src/utils/variables.ts
Original file line number Diff line number Diff line change
@@ -1,28 +1,82 @@
import { READABLE_VARIABLE_REGEXP } from '@common/constants';
import { READABLE_VARIABLE_REGEXP, VARIABLE_ONLY_REGEXP } from '@common/constants';
import _get from 'lodash/get.js';

export const variableReplacer = (
match: string,
inner: string,
variables: Record<string, unknown>,
modifier?: (variable: unknown) => unknown
): unknown => {
if (inner in variables) {
return typeof modifier === 'function' ? modifier(variables[inner]) : variables[inner];
const { id, path } = splitVariableName(inner);
if (!(id in variables)) {
return match;
}

return match;
if (!path) {
return typeof modifier === 'function' ? modifier(variables[id]) : variables[id];
}

try {
const variable = typeof variables[id] === 'string' ? JSON.parse(variables[id] as string) : variables[id];
return typeof modifier === 'function' ? modifier(_get(variable, path, 0)) : _get(variable, path, 0);
} catch (err: any) {
if (err?.message.includes('is not valid JSON')) {
return 0;
}
throw err;
}
};

export const splitVariableName = (inner: string) => {
const firstDotIndex = inner.indexOf('.');
const firstSquareBracketIndex = inner.indexOf('[');

if (firstDotIndex === -1 && firstSquareBracketIndex === -1) {
return { id: inner, path: '' };
}

if (firstDotIndex !== -1 && firstSquareBracketIndex === -1) {
return {
id: inner.slice(0, firstDotIndex),
path: inner.slice(firstDotIndex + 1), // skip the dot
};
}
if (firstDotIndex === -1 && firstSquareBracketIndex !== -1) {
return {
id: inner.slice(0, firstSquareBracketIndex),
path: inner.slice(firstSquareBracketIndex),
};
}

if (firstDotIndex < firstSquareBracketIndex) {
return {
id: inner.slice(0, firstDotIndex),
path: inner.slice(firstDotIndex + 1), // skip the dot
};
}

return {
id: inner.slice(0, firstSquareBracketIndex),
path: inner.slice(firstSquareBracketIndex),
};
};

export const replaceVariables = (
phrase: string | undefined | null,
variables: Record<string, unknown>,
modifier: ((variable: unknown) => unknown) | undefined = undefined,
{ trim = true }: { trim?: boolean } = {}
): string => {
{ trim = true, keepTypeIfOnlyVariable = false }: { trim?: boolean; keepTypeIfOnlyVariable?: boolean } = {}
): string | unknown => {
if (!phrase || (trim && !phrase.trim())) {
return '';
}

if (keepTypeIfOnlyVariable && phrase.match(VARIABLE_ONLY_REGEXP)) {
// remove the curly braces {} from phrase to get the inner
const inner = phrase.slice(1, -1);
return variableReplacer(phrase, inner, variables, modifier);
}

return phrase.replace(READABLE_VARIABLE_REGEXP, (match, inner) =>
String(variableReplacer(match, inner, variables, modifier))
);
12 changes: 12 additions & 0 deletions packages/voice-types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -3,6 +3,18 @@
All notable changes to this project will be documented in this file.
See [Conventional Commits](https://conventionalcommits.org) for commit guidelines.

## [2.10.40](https://github.com/voiceflow/libs/compare/@voiceflow/voice-types@2.10.39...@voiceflow/voice-types@2.10.40) (2024-12-16)

**Note:** Version bump only for package @voiceflow/voice-types

## [2.10.39](https://github.com/voiceflow/libs/compare/@voiceflow/voice-types@2.10.38...@voiceflow/voice-types@2.10.39) (2024-12-13)

**Note:** Version bump only for package @voiceflow/voice-types

## [2.10.38](https://github.com/voiceflow/libs/compare/@voiceflow/voice-types@2.10.37...@voiceflow/voice-types@2.10.38) (2024-12-12)

**Note:** Version bump only for package @voiceflow/voice-types

## [2.10.37](https://github.com/voiceflow/libs/compare/@voiceflow/voice-types@2.10.36...@voiceflow/voice-types@2.10.37) (2024-12-12)

**Note:** Version bump only for package @voiceflow/voice-types
Loading