Skip to content
This repository was archived by the owner on Jul 9, 2025. It is now read-only.
Merged
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
8 changes: 4 additions & 4 deletions Composer/packages/lib/shared/__tests__/copyUtils.test.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import { copyAdaptiveAction } from '../src/copyUtils';

describe('copyAdaptiveAction', () => {
const lgTemplate = [{ Name: 'bfdactivity-1234', Body: '-hello' }];
const lgTemplate = [{ Name: 'bfdactivity-1234', Body: '-hello' }, { Name: 'bfdprompt-1234', Body: '-hi' }];
const externalApi = {
updateDesigner: data => {
data.$designer = { id: '5678' };
Expand Down Expand Up @@ -72,7 +72,7 @@ describe('copyAdaptiveAction', () => {
alwaysPrompt: false,
allowInterruptions: 'true',
outputFormat: 'none',
prompt: '[bfdactivity-1234]',
prompt: '[bfdprompt-1234]',
};

expect(await copyAdaptiveAction(promptText, externalApi)).toEqual({
Expand All @@ -84,7 +84,7 @@ describe('copyAdaptiveAction', () => {
alwaysPrompt: false,
allowInterruptions: 'true',
outputFormat: 'none',
prompt: '[bfdactivity-5678]',
prompt: '[bfdprompt-5678]',
});

expect(await copyAdaptiveAction(promptText, externalApiWithFailure)).toEqual({
Expand All @@ -96,7 +96,7 @@ describe('copyAdaptiveAction', () => {
alwaysPrompt: false,
allowInterruptions: 'true',
outputFormat: 'none',
prompt: '-hello',
prompt: '-hi',
});
});
});
31 changes: 26 additions & 5 deletions Composer/packages/lib/shared/src/copyUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,30 @@ async function walkAdaptiveAction(input: any, visitor: (data: any) => Promise<an
}
}

function isLgActivity(activity: string) {
return activity && (activity.includes('bfdactivity-') || activity.includes('bfdprompt-'));
const TEMPLATE_PATTERN = /^\[bfd(.+)-(\d+)\]$/;
function isLgTemplate(template: string): boolean {
return TEMPLATE_PATTERN.test(template);
}

function parseLgTemplate(template: string) {
const result = TEMPLATE_PATTERN.exec(template);
if (result && result.length === 3) {
return {
templateType: result[1],
templateId: result[2],
};
}
return null;
}

async function copyLgActivity(activity: string, designerId: string, lgApi: any): Promise<string> {
if (!activity) return '';
if (!isLgActivity(activity) || !lgApi) return activity;
if (!lgApi) return activity;

const lgTemplate = parseLgTemplate(activity);
if (!lgTemplate) return activity;

const { templateType } = lgTemplate;
const { getLgTemplates, updateLgTemplate } = lgApi;
if (!getLgTemplates) return activity;

Expand All @@ -60,7 +76,7 @@ async function copyLgActivity(activity: string, designerId: string, lgApi: any):
if (currentLg) {
// Create new lg activity.
const newLgContent = currentLg.Body;
const newLgId = `bfdactivity-${designerId}`;
const newLgId = `bfd${templateType}-${designerId}`;
try {
await updateLgTemplate('common', newLgId, newLgContent);
return `[${newLgId}]`;
Expand All @@ -76,7 +92,12 @@ const overrideLgActivity = async (data, { lgApi }) => {
};

const overrideLgPrompt = async (data, { lgApi }) => {
data.prompt = await copyLgActivity(data.prompt, data.$designer.id, lgApi);
const promptFields = ['prompt', 'unrecognizedPrompt', 'defaultValueResponse', 'invalidPrompt'];
for (const field of promptFields) {
if (isLgTemplate(data[field])) {
data[field] = await copyLgActivity(data[field], data.$designer.id, lgApi);
}
}
};

// TODO: use $type from SDKTypes (after solving circular import issue).
Expand Down