Skip to content

Commit

Permalink
feat: migrate card v2 (CT-749) (#358)
Browse files Browse the repository at this point in the history
This PR does basically this: 
- Adapts CardV2's data structure to allow for cleaner code (voiceflow/creator-app#5878) by moving all the card's data to the type's root and removing the need for redundancy (card.cards, card.card or nasty types like CardV2Card)

- Makes the CardV2's description an union of Slate or string (for Audio vs Chat sake)
- Create all the Trace types required for the card

Co-authored-by: Yauheni <overhawlin@gmail.com>
Co-authored-by: Filipe Merker <filipe.merker@voiceflow.com>
  • Loading branch information
3 people committed Aug 22, 2022
1 parent 842b6b0 commit f84042f
Show file tree
Hide file tree
Showing 15 changed files with 124 additions and 17 deletions.
47 changes: 39 additions & 8 deletions packages/base-types/src/node/cardV2.ts
Original file line number Diff line number Diff line change
@@ -1,29 +1,60 @@
import { GeneralRequestButton } from '@base-types/request';
import { SlateTextValue } from '@base-types/text';
import { Nullable } from '@voiceflow/common';

import { NodeType } from './constants';
import { BaseNoMatchStepData, BaseNoReplyStepData, BaseStep, BaseStepPorts, BuiltInNextPort, BuiltInNoMatchNoReplyPorts, DataID } from './utils';
import {
BaseNode,
BaseNoMatchNodeData,
BaseNoMatchStepData,
BaseNoReplyNodeData,
BaseNoReplyStepData,
BaseStep,
BaseStepPorts,
BaseTraceFrame,
BuiltInNextPort,
BuiltInNoMatchNoReplyPorts,
DataID,
NodeNextID,
TraceType,
} from './utils';

export interface CardV2Button extends DataID {
name: string;
intent?: Nullable<string>;
}

export interface CardV2Card<B = CardV2Button> extends DataID {
imageUrl: string | null;
export interface CardV2Data<Button> {
title: string;
description: SlateTextValue;
buttons: B[];
buttons: Button[];
imageUrl: string | null;
}

export interface StepBuiltInPorts extends BuiltInNextPort, BuiltInNoMatchNoReplyPorts {}

export interface StepPorts extends BaseStepPorts<StepBuiltInPorts, []> {}

export interface StepData extends BaseNoMatchStepData, BaseNoReplyStepData {
card: CardV2Card;
}
export interface StepData<Button = CardV2Button> extends BaseNoMatchStepData, BaseNoReplyStepData, CardV2Data<Button> {}

export interface Step<Data = StepData> extends BaseStep<Data, StepPorts> {
type: NodeType.CARD_V2;
}

type CardDataWithGeneralButton = CardV2Data<GeneralRequestButton>;
export interface Node extends BaseNode, NodeNextID, BaseNoReplyNodeData, BaseNoMatchNodeData, CardDataWithGeneralButton {
type: NodeType.CARD_V2;
isBlocking: boolean;
}

export interface TraceCardV2Description {
slate: SlateTextValue;
text: string;
}
export interface TraceCardV2 extends CardDataWithGeneralButton {
description: TraceCardV2Description;
}

export interface TraceFramePayload extends TraceCardV2 {}
export interface TraceFrame extends BaseTraceFrame<TraceFramePayload> {
type: TraceType.CARD_V2;
}
1 change: 1 addition & 0 deletions packages/base-types/src/node/utils/trace.ts
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ export enum TraceType {
CHOICE = 'choice',
STREAM = 'stream',
VISUAL = 'visual',
CARD_V2 = 'cardV2',
CAROUSEL = 'carousel',
NO_REPLY = 'no-reply',
ENTITY_FILLING = 'entity-filling',
Expand Down
3 changes: 3 additions & 0 deletions packages/base-types/src/trace/index.ts
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import { TraceFrame as CardV2Trace } from '@base-types/node/cardV2';
import { TraceFrame as CarouselTrace } from '@base-types/node/carousel';
import { TraceFrame as ExitTrace } from '@base-types/node/exit';
import { TraceFrame as FlowTrace } from '@base-types/node/flow';
Expand All @@ -10,6 +11,7 @@ import { TraceFrame as VisualTrace } from '@base-types/node/visual';
import { IntentRequest } from '@base-types/request';
import { Log as RuntimeLog } from '@base-types/runtimeLogs';

export { TraceFrame as CardV2Trace } from '@base-types/node/cardV2';
export { TraceFrame as CarouselTrace } from '@base-types/node/carousel';
export { TraceFrame as ExitTrace } from '@base-types/node/exit';
export { TraceFrame as FlowTrace } from '@base-types/node/flow';
Expand Down Expand Up @@ -89,4 +91,5 @@ export type AnyTrace =
| VisualTrace
| NoReplyTrace
| CarouselTrace
| CardV2Trace
| EntityFillingTrace;
5 changes: 3 additions & 2 deletions packages/base-types/src/utils/node.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,13 @@ export const isSpeak = createNodeTypeguard<Node.Speak.Node>(Node.NodeType.SPEAK)
export const isStart = createNodeTypeguard<Node.Start.Node>(Node.NodeType.START);
export const isSetV2 = createNodeTypeguard<Node.SetV2.Node>(Node.NodeType.SET_V2);
export const isVisual = createNodeTypeguard<Node.Visual.Node>(Node.NodeType.VISUAL);
export const isCarousel = createNodeTypeguard<Node.Carousel.Node>(Node.NodeType.CAROUSEL);
export const isStream = createNodeTypeguard<Node.Stream.Node>(Node.NodeType.STREAM);
export const isRandom = createNodeTypeguard<Node.Random.Node>(Node.NodeType.RANDOM);
export const isRandomV2 = createNodeTypeguard<Node.RandomV2.Node>(Node.NodeType.RANDOM_V2);
export const isCardV2 = createNodeTypeguard<Node.CardV2.Node>(Node.NodeType.CARD_V2);
export const isCapture = createNodeTypeguard<Node.Capture.Node>(Node.NodeType.CAPTURE);
export const isGeneral = createNodeTypeguard<Node.General.Node>(Node.NodeType.GENERAL);
export const isCarousel = createNodeTypeguard<Node.Carousel.Node>(Node.NodeType.CAROUSEL);
export const isRandomV2 = createNodeTypeguard<Node.RandomV2.Node>(Node.NodeType.RANDOM_V2);
export const isGoToNode = createNodeTypeguard<Node.GoToNode.Node>(Node.NodeType.GOTO_NODE);
export const isDirective = createNodeTypeguard<Node.Directive.Node>(Node.NodeType.DIRECTIVE);
export const isCaptureV2 = createNodeTypeguard<Node.CaptureV2.Node>(Node.NodeType.CAPTURE_V2);
Expand Down
1 change: 1 addition & 0 deletions packages/base-types/src/utils/step.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ export const isSet = createStepTypeguard<Node.Set.Step>(Node.NodeType.SET);
export const isApi = createStepTypeguard<Node.Api.Step>(Node.NodeType.API);
export const isText = createStepTypeguard<Node.Text.Step>(Node.NodeType.TEXT);
export const isCard = createStepTypeguard<Node.Card.Step>(Node.NodeType.CARD);
export const isCardV2 = createStepTypeguard<Node.CardV2.Step>(Node.NodeType.CARD_V2);
export const isIfV2 = createStepTypeguard<Node.IfV2.Step>(Node.NodeType.IF_V2);
export const isFlow = createStepTypeguard<Node.Flow.Step>(Node.NodeType.FLOW);
export const isGoTo = createStepTypeguard<Node.GoTo.Step>(Node.NodeType.GOTO);
Expand Down
12 changes: 9 additions & 3 deletions packages/chat-types/src/node/cardV2.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,16 @@
import { BaseNode, Nullable } from '@voiceflow/base-types';
import { BaseNode, BaseText, Nullable } from '@voiceflow/base-types';

import { StepNoMatch, StepNoReply } from './utils';
import { NodeNoMatch, NodeNoReply, StepNoMatch, StepNoReply } from './utils';

export interface StepData extends BaseNode.CardV2.StepData {
description: BaseText.SlateTextValue;
noReply?: Nullable<StepNoReply>;
noMatch?: Nullable<StepNoMatch>;
}

export interface Step<Data = StepData> extends BaseNode.CardV2.Step<Data> {}

export interface Node extends BaseNode.CardV2.Node {
description: BaseText.SlateTextValue;
noReply?: Nullable<NodeNoReply>;
noMatch?: Nullable<NodeNoMatch>;
}
17 changes: 17 additions & 0 deletions packages/voice-types/src/node/cardV2.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
import { BaseNode, Nullable } from '@voiceflow/base-types';

import { NodeNoMatch, NodeNoReply, StepNoMatch, StepNoReply } from './utils';

export interface StepData<Voice> extends BaseNode.CardV2.StepData {
description: string;
noReply?: Nullable<StepNoReply<Voice>>;
noMatch?: Nullable<StepNoMatch<Voice>>;
}

export interface Step<Data = StepData<unknown>> extends BaseNode.CardV2.Step<Data> {}

export interface Node extends BaseNode.CardV2.Node {
description: string;
noReply?: Nullable<NodeNoReply>;
noMatch?: Nullable<NodeNoMatch>;
}
1 change: 1 addition & 0 deletions packages/voice-types/src/node/index.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
export * as Buttons from './buttons';
export * as Capture from './capture';
export * as CaptureV2 from './captureV2';
export * as CardV2 from './cardV2';
export * as Interaction from './interaction';
export * as Prompt from './prompt';
export * as Speak from './speak';
Expand Down
7 changes: 7 additions & 0 deletions packages/voiceflow-types/src/node/card/chat.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import { ChatNode } from '@voiceflow/chat-types';

export interface ChatStepData extends ChatNode.CardV2.StepData {}

export interface ChatStep extends ChatNode.CardV2.Step<ChatStepData> {}

export interface ChatNode extends ChatNode.CardV2.Node {}
15 changes: 15 additions & 0 deletions packages/voiceflow-types/src/node/card/index.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
import { BaseNode } from '@voiceflow/base-types';

import { ChatNode, ChatStep, ChatStepData } from './chat';
import { VoiceNode, VoiceStep, VoiceStepData } from './voice';

export * from './chat';
export * from './voice';

export type Step = ChatStep | VoiceStep;

export type StepPorts = BaseNode.CardV2.StepPorts;

export type StepData = ChatStepData | VoiceStepData;

export type Node = ChatNode | VoiceNode;
8 changes: 8 additions & 0 deletions packages/voiceflow-types/src/node/card/voice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VoiceNode } from '@voiceflow/voice-types';
import { Voice } from '@voiceflow-types/constants';

export interface VoiceStepData extends VoiceNode.CardV2.StepData<Voice> {}

export interface VoiceStep extends VoiceNode.CardV2.Step<VoiceStepData> {}

export interface VoiceNode extends VoiceNode.CardV2.Node {}
2 changes: 2 additions & 0 deletions packages/voiceflow-types/src/node/cardV2/chat.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,3 +3,5 @@ import { ChatNode } from '@voiceflow/chat-types';
export interface ChatStepData extends ChatNode.CardV2.StepData {}

export interface ChatStep extends ChatNode.CardV2.Step<ChatStepData> {}

export interface ChatNode extends ChatNode.CardV2.Node {}
10 changes: 7 additions & 3 deletions packages/voiceflow-types/src/node/cardV2/index.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,15 @@
import { BaseNode } from '@voiceflow/base-types';

import { ChatStep, ChatStepData } from './chat';
import { ChatNode, ChatStep, ChatStepData } from './chat';
import { VoiceNode, VoiceStep, VoiceStepData } from './voice';

export * from './chat';
export * from './voice';

export type Step = ChatStep;
export type Step = ChatStep | VoiceStep;

export type StepPorts = BaseNode.CardV2.StepPorts;

export type StepData = ChatStepData;
export type StepData = ChatStepData | VoiceStepData;

export type Node = ChatNode | VoiceNode;
8 changes: 8 additions & 0 deletions packages/voiceflow-types/src/node/cardV2/voice.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
import { VoiceNode } from '@voiceflow/voice-types';
import { Voice } from '@voiceflow-types/constants';

export interface VoiceStepData extends VoiceNode.CardV2.StepData<Voice> {}

export interface VoiceStep extends VoiceNode.CardV2.Step<VoiceStepData> {}

export interface VoiceNode extends VoiceNode.CardV2.Node {}
4 changes: 3 additions & 1 deletion packages/voiceflow-types/src/node/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -28,20 +28,22 @@ export type AnyExtendedStep =
| Buttons.Step
| Carousel.Step
| CardV2.Step;
export type AnyExtendedNode = Speak.Node | Capture.Node | CaptureV2.Node | Interaction.Node | Carousel.Node;
export type AnyExtendedNode = Speak.Node | Capture.Node | CaptureV2.Node | Interaction.Node | Carousel.Node | CardV2.Node;

export type AnyStep =
| BaseNode.AnyCommonStep
| AnyExtendedStep
| BaseNode.Text.Step
| BaseNode.Visual.Step
| BaseNode.Card.Step
| BaseNode.Stream.Step
| BaseNode.Directive.Step;
export type AnyNode =
| BaseNode.AnyCommonNode
| AnyExtendedNode
| BaseNode.Text.Node
| BaseNode.Visual.Node
| BaseNode.Card.Node
| BaseNode.Stream.Node
| BaseNode.Directive.Node;

Expand Down

0 comments on commit f84042f

Please sign in to comment.