Skip to content

Commit

Permalink
perf(spec-parser): adaptive card in type b only keep at most 5 elements
Browse files Browse the repository at this point in the history
  • Loading branch information
SLdragon committed May 10, 2024
1 parent 58b5d2c commit ea8f49f
Show file tree
Hide file tree
Showing 2 changed files with 176 additions and 0 deletions.
2 changes: 2 additions & 0 deletions packages/spec-parser/src/manifestUpdater.ts
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ export class ManifestUpdater {
if (json.schema) {
const [card, jsonPath] =
AdaptiveCardGenerator.generateAdaptiveCard(operationItem);

card.body = card.body?.slice(0, 5);
const responseSemantic = wrapResponseSemantics(card, jsonPath);
funcObj.capabilities = {
response_semantics: responseSemantic,
Expand Down
174 changes: 174 additions & 0 deletions packages/spec-parser/test/manifestUpdater.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -282,6 +282,180 @@ describe("updateManifestWithAiPlugin", () => {
expect(manifest).to.deep.equal(expectedManifest);
expect(apiPlugin).to.deep.equal(expectedPlugins);
});

it("should keep at most 5 properties in response semantics", async () => {
const spec: any = {
openapi: "3.0.2",
info: {
title: "My API",
description: "My API description",
},
servers: [
{
url: "/v3",
},
],
paths: {
"/pets": {
get: {
operationId: "getPets",
summary: "Get all pets",
description: "Returns all pets from the system that the user has access to",
parameters: [
{
name: "limit",
description: "Maximum number of pets to return",
required: true,
schema: {
type: "integer",
},
},
],
responses: {
200: {
content: {
"application/json": {
schema: {
type: "object",
properties: {
name: {
type: "string",
},
description: {
type: "string",
},
imageUrl: {
type: "string",
},
id: {
type: "string",
},
age: {
type: "string",
},
status: {
type: "string",
},
},
},
},
},
},
},
},
},
},
};
const manifestPath = "/path/to/your/manifest.json";
const outputSpecPath = "/path/to/your/spec/outputSpec.yaml";
const pluginFilePath = "/path/to/your/ai-plugin.json";

const originalManifest = {
name: { short: "Original Name", full: "Original Full Name" },
description: { short: "Original Short Description", full: "Original Full Description" },
};
const expectedManifest = {
name: { short: "Original Name", full: "Original Full Name" },
description: { short: "My API", full: "My API description" },
copilotExtensions: {
plugins: [
{
file: "ai-plugin.json",
id: "plugin_1",
},
],
},
};

const expectedPlugins: PluginManifestSchema = {
schema_version: "v2.1",
name_for_human: "Original Name",
namespace: "originalname",
description_for_human: "My API description",
functions: [
{
name: "getPets",
description: "Returns all pets from the system that the user has access to",
capabilities: {
response_semantics: {
data_path: "$",
properties: {
subtitle: "$.description",
title: "$.name",
url: "$.imageUrl",
},
static_template: {
$schema: "http://adaptivecards.io/schemas/adaptive-card.json",
body: [
{
text: "name: ${if(name, name, 'N/A')}",
type: "TextBlock",
wrap: true,
},
{
text: "description: ${if(description, description, 'N/A')}",
type: "TextBlock",
wrap: true,
},
{
$when: "${imageUrl != null}",
type: "Image",
url: "${imageUrl}",
},
{
text: "id: ${if(id, id, 'N/A')}",
type: "TextBlock",
wrap: true,
},
{
text: "age: ${if(age, age, 'N/A')}",
type: "TextBlock",
wrap: true,
},
],
type: "AdaptiveCard",
version: "1.5",
},
},
},
},
],
runtimes: [
{
type: "OpenApi",
auth: {
type: "None",
},
spec: {
url: "spec/outputSpec.yaml",
},
run_for_functions: ["getPets"],
},
],
};
sinon.stub(fs, "readJSON").resolves(originalManifest);
sinon
.stub(fs, "pathExists")
.withArgs(manifestPath)
.resolves(true)
.withArgs(pluginFilePath)
.resolves(false);

const options: ParseOptions = {
allowMethods: ["get", "post"],
allowResponseSemantics: true,
};
const [manifest, apiPlugin] = await ManifestUpdater.updateManifestWithAiPlugin(
manifestPath,
outputSpecPath,
pluginFilePath,
spec,
options
);

expect(manifest).to.deep.equal(expectedManifest);
expect(apiPlugin).to.deep.equal(expectedPlugins);
});
});

describe("auth", () => {
Expand Down

0 comments on commit ea8f49f

Please sign in to comment.