Skip to content

Commit

Permalink
perf(spec-parser): add confirmation for type b plugin (#11416)
Browse files Browse the repository at this point in the history
* perf(spec-parser): add confirmation for type b plugin

* perf: update test case

---------

Co-authored-by: rentu <rentu@microsoft.com>
  • Loading branch information
SLdragon and SLdragon authored Apr 19, 2024
1 parent 1f45ae7 commit 16b87f3
Show file tree
Hide file tree
Showing 5 changed files with 395 additions and 1 deletion.
5 changes: 5 additions & 0 deletions packages/spec-parser/src/interfaces.ts
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,11 @@ export interface ParseOptions {
*/
allowResponseSemantics?: boolean;

/**
* If true, the paser will allow confirmation in plugin file. Only take effect in Copilot project
*/
allowConfirmation?: boolean;

/**
* The type of project that the parser is being used for.
* Project can be SME/Copilot/TeamsAi
Expand Down
29 changes: 28 additions & 1 deletion packages/spec-parser/src/manifestUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ export class ManifestUpdater {
for (const method in operations) {
if (options.allowMethods!.includes(method)) {
const operationItem = (operations as any)[method] as OpenAPIV3.OperationObject;
const confirmationBodies: string[] = [];
if (operationItem) {
const operationId = operationItem.operationId!;
const description = operationItem.description ?? "";
Expand All @@ -181,6 +182,8 @@ export class ManifestUpdater {
pathUrl
);

confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(param.name));

if (param.required) {
parameters.required.push(param.name);
}
Expand All @@ -207,6 +210,8 @@ export class ManifestUpdater {
method,
pathUrl
);

confirmationBodies.push(ManifestUpdater.getConfirmationBodyItem(property));
}
} else {
throw new SpecParserError(
Expand All @@ -224,9 +229,12 @@ export class ManifestUpdater {
const funcObj: FunctionObject = {
name: operationId,
description: description,
parameters: parameters,
};

if (paramObject || requestBody) {
funcObj.parameters = parameters;
}

if (options.allowResponseSemantics) {
const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem);
const responseSemantic = wrapResponseSemantics(card, jsonPath);
Expand All @@ -235,6 +243,21 @@ export class ManifestUpdater {
};
}

if (options.allowConfirmation && method !== ConstantString.GetMethod) {
if (!funcObj.capabilities) {
funcObj.capabilities = {};
}

funcObj.capabilities.confirmation = {
type: "AdaptiveCard",
title: operationItem.summary ?? description,
};

if (confirmationBodies.length > 0) {
funcObj.capabilities.confirmation.body = confirmationBodies.join("\n");
}
}

functions.push(funcObj);
functionNames.push(operationId);
if (description) {
Expand Down Expand Up @@ -471,4 +494,8 @@ export class ManifestUpdater {
static removeAllSpecialCharacters(str: string): string {
return str.toLowerCase().replace(/[^a-z0-9]/g, "");
}

static getConfirmationBodyItem(paramName: string): string {
return `* **${Utils.updateFirstLetter(paramName)}**: {{function.parameters.${paramName}}}`;
}
}
1 change: 1 addition & 0 deletions packages/spec-parser/src/specParser.browser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ export class SpecParser {
allowMethods: ["get", "post"],
allowConversationStarters: false,
allowResponseSemantics: false,
allowConfirmation: false,
projectType: ProjectType.SME,
};

Expand Down
1 change: 1 addition & 0 deletions packages/spec-parser/src/specParser.ts
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ export class SpecParser {
allowMethods: ["get", "post"],
allowConversationStarters: false,
allowResponseSemantics: false,
allowConfirmation: false,
projectType: ProjectType.SME,
};

Expand Down
Loading

0 comments on commit 16b87f3

Please sign in to comment.