Skip to content

Commit

Permalink
Merge pull request #67 from Cognigy/develop
Browse files Browse the repository at this point in the history
fix(Bug fixes): Release with bug fixing
  • Loading branch information
XavierJordaMurria authored Dec 2, 2022
2 parents bb5ba11 + 3ab60b4 commit fc0de14
Show file tree
Hide file tree
Showing 5 changed files with 41 additions and 26 deletions.
4 changes: 2 additions & 2 deletions src/lib/endpoints.ts
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ export const cloneEndpoints = async (availableProgress: number): Promise<void> =
await removeCreateDir(endpointDir);

// An increment counter for the progress bar
const progressIncrement = Math.round(availableProgress / 10);
const progressIncrement = Math.round(availableProgress / 3);
addToProgressBar(progressIncrement);

// query Cognigy.AI for all Flows in this agent
Expand All @@ -29,7 +29,7 @@ export const cloneEndpoints = async (availableProgress: number): Promise<void> =
});
addToProgressBar(progressIncrement);

const incrementPerEndpoint = 70 / endpoints.items.length;
const incrementPerEndpoint = (progressIncrement/3) / endpoints.items.length;

// create a sub-folder, chart.json and config.json for each Flow
for (let endpoint of endpoints.items) {
Expand Down
50 changes: 34 additions & 16 deletions src/lib/flows.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import { indexAll } from '../utils/indexAll';
// Interfaces
import { ILocaleIndexItem_2_0 } from '@cognigy/rest-api-client/build/shared/interfaces/restAPI/resources/locales/v2.0';
import { IIntent } from '@cognigy/rest-api-client/build/shared/interfaces/resources/intent/IIntent';
import { IIndexFlowsRestReturnValue_2_0 } from '@cognigy/rest-api-client';

/**
* Clones Cognigy Flows to disk
Expand All @@ -39,10 +40,13 @@ export const cloneFlows = async (availableProgress: number): Promise<void> => {
const progressPerFlow = availableProgress / flows.items.length;

// create a sub-folder, chart.json and config.json for each Flow
const flowsPromiseArr = []
for (let flow of flows.items) {
await pullFlow(flow.name, progressPerFlow);
flowsPromiseArr.push(pullFlow(flow.name, progressPerFlow));
}

await Promise.all(flowsPromiseArr);

return Promise.resolve();
};

Expand All @@ -52,17 +56,21 @@ export const cloneFlows = async (availableProgress: number): Promise<void> => {
* @param flowName The name of the Flow to pull
* @param availableProgress How much of the progress bar can be filled by this process
*/
export const pullFlow = async (flowName: string, availableProgress: number): Promise<void> => {
export const pullFlow = async (flowName: string, availableProgress: number, _flows?: Pick<IIndexFlowsRestReturnValue_2_0, 'items' | 'total'>): Promise<void> => {
// The base directory for Flows
const flowsDir = CONFIG.agentDir + "/flows";
const flowDir = flowsDir + "/" + flowName;

await removeCreateDir(flowDir);

let flows: Pick<IIndexFlowsRestReturnValue_2_0, 'items' | 'total'> = _flows

// query Cognigy.AI for all Flows in this agent
const flows = await indexAll(CognigyClient.indexFlows)({
"projectId": CONFIG.agent
});
if (!flows) {
flows = await indexAll(CognigyClient.indexFlows)({
"projectId": CONFIG.agent
});
}

// check if flow with given name exists
const flow = flows.items.find((flow) => {
Expand Down Expand Up @@ -93,7 +101,10 @@ export const pullFlow = async (flowName: string, availableProgress: number): Pro
});

// half of the available progress bar space is allocated to Nodes, the other half to intents
const progressPerNode = progressPerLocale / 2 / chart.nodes.length;
const nodesProgressBar = progressPerLocale / 2
const intentsProgressBar = progressPerLocale / 2;

const progressPerNode = nodesProgressBar / chart.nodes.length;

// iterate through all Nodes for this chart and add the information into the chart
for (let node of chart.nodes) {
Expand All @@ -110,11 +121,18 @@ export const pullFlow = async (flowName: string, availableProgress: number): Pro

fs.writeFileSync(localeDir + "/chart.json", JSON.stringify(chart, undefined, 4));

// console.log(`Fetching intents: ${JSON.stringify({
// flowId: flow._id,
// localeId: locale._id,
// format: 'json'
// })}`);

const flowIntents = await CognigyClient.exportIntents({
flowId: flow._id,
localeId: locale._id,
format: 'json'
});
addToProgressBar(intentsProgressBar);

fs.writeFileSync(localeDir + "/intents.json", JSON.stringify(flowIntents, undefined, 4));
}
Expand Down Expand Up @@ -472,17 +490,17 @@ export const translateFlow = async (flowName: string, options: ITranslateFlowOpt
try {
if (localeReference === targetLocale._id) {
if (['say', 'question', 'optionalQuestion'].indexOf(type) > -1) {
const flowNode = await translateFlowNode(node, toLanguage, translator, apiKey);
// update node in Cognigy.AI
await CognigyClient.updateChartNode({
nodeId: flowNode._id,
config: flowNode.config,
localeId: targetLocale._id,
resourceId: flowConfig._id,
resourceType: 'flow'
})
const flowNode = await translateFlowNode(node, toLanguage, translator, apiKey);
// update node in Cognigy.AI
await CognigyClient.updateChartNode({
nodeId: flowNode._id,
config: flowNode.config,
localeId: targetLocale._id,
resourceId: flowConfig._id,
resourceType: 'flow'
})
}
}
}
} catch (err) {
console.error(err)
// if a localization throws an error, we skip
Expand Down
4 changes: 2 additions & 2 deletions src/lib/lexicons.ts
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ export const pullLexicon = async (lexiconName: string, availableProgress: number
const lexiconDir = CONFIG.agentDir + "/lexicons/" + lexiconName;

// An increment counter for the progress bar
const progressIncrement = Math.round(availableProgress / 10);
const progressIncrement = availableProgress / 3;

// query Cognigy.AI for all Lexicons in this agent
const lexicons = await indexAll(CognigyClient.indexLexicons)({
Expand Down Expand Up @@ -109,7 +109,7 @@ export const pullLexicon = async (lexiconName: string, availableProgress: number

// write files to disk
fs.writeFileSync(lexiconDir + "/keyphrases.csv", lexiconFile);
addToProgressBar(70);
addToProgressBar(progressIncrement);

return Promise.resolve();
};
Expand Down
2 changes: 1 addition & 1 deletion src/utils/RestAdapter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ export class RestAdapter implements IHttpAdapter {
} catch (err) {

if (this.options.retries && retries < this.options.retries) {
console.log(`Retrying API request. Error was: ${err.message}`);
console.log(`Retrying API request. Error was: ${err.message}\n, Request: ${JSON.stringify(httpRequest, null, 2)}`);

return this.request(httpRequest, client, retries + 1);
} else {
Expand Down
7 changes: 2 additions & 5 deletions src/utils/indexAll.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,8 @@ export const indexAll = <Query, Entity>(indexFn: TIndexFn<Query, Entity>) => {

const firstResponse = await indexFn({
...query,
skip: 0
skip: 0,
});

const result: TResponseWithoutPagination = {
items: [...firstResponse.items],
total: firstResponse.total
Expand All @@ -29,12 +28,10 @@ export const indexAll = <Query, Entity>(indexFn: TIndexFn<Query, Entity>) => {
const newPage = await (indexFn({
...query,
limit: MAX_LIMIT,
skip: MAX_LIMIT * page
skip: result.items.length
}));

result.items = result.items.concat(newPage.items);
}

return result;
}

Expand Down

0 comments on commit fc0de14

Please sign in to comment.