forked from microsoft/BotFramework-Composer
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Added trigger grouping methods to indexers (microsoft#4413)
* Added trigger grouping methods to indexers * Fix bug and build issue
- Loading branch information
1 parent
6957c24
commit 7648b1e
Showing
4 changed files
with
118 additions
and
1 deletion.
There are no files selected for viewing
35 changes: 35 additions & 0 deletions
35
Composer/packages/lib/indexers/src/extractSchemaProperties.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,35 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { DialogInfo, JsonSchemaFile } from '@bfc/shared'; | ||
|
||
import { getBaseName } from './utils/help'; | ||
|
||
export const extractSchemaProperties = (dialog: DialogInfo, jsonSchemaFiles: JsonSchemaFile[]): string[] => { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const schemaRef: any = dialog.content?.schema; | ||
|
||
if (schemaRef) { | ||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
let schema: any = undefined; | ||
|
||
if (schemaRef.$public || schemaRef.properties) { | ||
schema = schemaRef; | ||
} else { | ||
schema = jsonSchemaFiles.find( | ||
(file) => file.id === getBaseName(schemaRef as string) || file.id === dialog.content.schema | ||
)?.content; | ||
} | ||
|
||
if (schema) { | ||
if (schema?.$public && Array.isArray(schema.$public)) { | ||
return schema.$public; | ||
} | ||
if (schema?.properties) { | ||
return Object.keys(schema.properties) ?? []; | ||
} | ||
} | ||
} | ||
|
||
return []; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
// Copyright (c) Microsoft Corporation. | ||
// Licensed under the MIT License. | ||
|
||
import { DialogInfo, ITrigger } from '@bfc/shared'; | ||
import { ExpressionParser } from 'adaptive-expressions'; | ||
import uniq from 'lodash/uniq'; | ||
|
||
// eslint-disable-next-line @typescript-eslint/no-explicit-any | ||
const getPropertyReferences = (content: any) => { | ||
const foundProperties: string[] = []; | ||
|
||
if (content) { | ||
// has $designer: { "propertyGroups": ["<name1>", "<name2", ... ] | ||
if (content.$designer?.propertyGroups && Array.isArray(content.$designer?.propertyGroups === 'array')) { | ||
foundProperties.push(...content.$designer.propertyGroups); | ||
} | ||
|
||
// has "property": "<name>" | ||
if (content.property && typeof content.property === 'string') { | ||
foundProperties.push(content.property); | ||
} | ||
|
||
// has condition : "<expresssion referencing properties>" | ||
if (content.condition) { | ||
const expressionParser = new ExpressionParser(); | ||
const expression = expressionParser.parse(content.condition); | ||
const references = expression.references().map((r) => (r.startsWith('$') ? r.substring(1) : r)); | ||
foundProperties.push(...references); | ||
} | ||
} | ||
|
||
return uniq(foundProperties); | ||
}; | ||
|
||
const getTriggerPropertyReferences = (trigger: ITrigger) => { | ||
const content = trigger.content; | ||
|
||
// inspect trigger | ||
const foundProperties: string[] = getPropertyReferences(trigger.content); | ||
|
||
// inspect actions | ||
if (content.actions && Array.isArray(content.actions)) { | ||
for (let i = 0; i < content.actions.length; i++) { | ||
foundProperties.push(...getPropertyReferences(content.actions[i])); | ||
} | ||
} | ||
|
||
return uniq(foundProperties); | ||
}; | ||
|
||
export const groupTriggersByPropertyReference = ( | ||
dialog: DialogInfo, | ||
options?: { allowMultiParent?: boolean; validProperties?: string[] } | ||
): Record<string, ITrigger[]> => { | ||
const result = {} as Record<string, ITrigger[]>; | ||
|
||
const validProperties = options?.validProperties; | ||
const isValidProperty = validProperties | ||
? (x: string | undefined) => x && validProperties.findIndex((p) => x === p) !== -1 | ||
: () => true; | ||
|
||
const addResult = (property: string, trigger: ITrigger) => { | ||
result[property] ? result[property].push(trigger) : (result[property] = [trigger]); | ||
}; | ||
|
||
if (dialog?.triggers) { | ||
dialog.triggers.forEach((t) => { | ||
const properties = getTriggerPropertyReferences(t).filter(isValidProperty); | ||
if (properties.length > 1 && options?.allowMultiParent) { | ||
properties.forEach((p) => { | ||
addResult(p, t); | ||
}); | ||
} else if (properties.length === 1) { | ||
addResult(properties[0], t); | ||
} | ||
}); | ||
} | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters