@microsoft/m365-spec-parser 0.2.6-alpha.e49436b07.0 → 0.2.6-alpha.e5f43e40e.0

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -47,6 +47,7 @@ var ErrorType;
47
47
  ErrorType["UrlPathNotExist"] = "url-path-not-exist";
48
48
  ErrorType["Cancelled"] = "cancelled";
49
49
  ErrorType["Unknown"] = "unknown";
50
+ ErrorType["AddAuthFailed"] = "add-auth-failed";
50
51
  })(ErrorType || (ErrorType = {}));
51
52
  /**
52
53
  * An enum that represents the types of warnings that can occur during validation.
@@ -77,7 +78,12 @@ var ProjectType;
77
78
  ProjectType[ProjectType["Copilot"] = 0] = "Copilot";
78
79
  ProjectType[ProjectType["SME"] = 1] = "SME";
79
80
  ProjectType[ProjectType["TeamsAi"] = 2] = "TeamsAi";
80
- })(ProjectType || (ProjectType = {}));
81
+ })(ProjectType || (ProjectType = {}));
82
+ var AdaptiveCardUpdateStrategy;
83
+ (function (AdaptiveCardUpdateStrategy) {
84
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["CreateNew"] = 0] = "CreateNew";
85
+ AdaptiveCardUpdateStrategy[AdaptiveCardUpdateStrategy["KeepExisting"] = 1] = "KeepExisting";
86
+ })(AdaptiveCardUpdateStrategy || (AdaptiveCardUpdateStrategy = {}));
81
87
 
82
88
  // Copyright (c) Microsoft Corporation.
83
89
  class ConstantString {
@@ -103,8 +109,8 @@ ConstantString.AuthTypeIsNotSupported = "Unsupported authorization type in API '
103
109
  ConstantString.UnsupportedSchema = "Unsupported schema in %s %s: %s";
104
110
  ConstantString.FuncDescriptionTooLong = "The description of the function '%s' is too long. The current length is %s characters, while the maximum allowed length is %s characters.";
105
111
  ConstantString.GenerateJsonDataFailed = "Failed to generate JSON data for api: %s due to %s.";
106
- ConstantString.WrappedCardVersion = "devPreview";
107
- ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/vDevPreview/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
112
+ ConstantString.WrappedCardVersion = "1.0";
113
+ ConstantString.WrappedCardSchema = "https://developer.microsoft.com/json-schemas/teams/v1.19/MicrosoftTeams.ResponseRenderingTemplate.schema.json";
108
114
  ConstantString.WrappedCardResponseLayout = "list";
109
115
  ConstantString.GetMethod = "get";
110
116
  ConstantString.PostMethod = "post";
@@ -578,6 +584,35 @@ class Utils {
578
584
  const serverUrl = operationServer || methodServer || rootServer;
579
585
  return serverUrl;
580
586
  }
587
+ static getAuthSchemaObject(authType, authParameters) {
588
+ switch (authType) {
589
+ case "oauth":
590
+ case "microsoft-entra":
591
+ return {
592
+ type: "oauth2",
593
+ flows: {
594
+ authorizationCode: {
595
+ authorizationUrl: authParameters.authorizationUrl,
596
+ tokenUrl: authParameters.tokenUrl,
597
+ refreshUrl: authParameters.refreshUrl,
598
+ scopes: authParameters.scopes,
599
+ },
600
+ },
601
+ };
602
+ case "api-key":
603
+ return {
604
+ type: "apiKey",
605
+ in: authParameters.in,
606
+ name: authParameters.name,
607
+ };
608
+ case "bearer-token":
609
+ default:
610
+ return {
611
+ type: "http",
612
+ scheme: "bearer",
613
+ };
614
+ }
615
+ }
581
616
  }
582
617
 
583
618
  // Copyright (c) Microsoft Corporation.
@@ -1722,8 +1757,8 @@ class ManifestUpdater {
1722
1757
  }
1723
1758
  const appName = this.removeEnvs(manifest.name.short);
1724
1759
  const specRelativePath = ManifestUpdater.getRelativePath(manifestPath, outputSpecPath);
1725
- const [apiPlugin, warnings] = await ManifestUpdater.generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authMap, options, existingPluginManifestInfo);
1726
- return [manifest, apiPlugin, warnings];
1760
+ const [apiPlugin, warnings, jsonDataSet] = await ManifestUpdater.generatePluginManifestSchema(spec, specRelativePath, apiPluginFilePath, appName, authMap, options, existingPluginManifestInfo);
1761
+ return [manifest, apiPlugin, warnings, jsonDataSet];
1727
1762
  }
1728
1763
  static updateManifestDescription(manifest, spec) {
1729
1764
  var _a, _b;
@@ -1749,6 +1784,7 @@ class ManifestUpdater {
1749
1784
  const warnings = [];
1750
1785
  const functions = [];
1751
1786
  const functionNamesMap = {};
1787
+ const jsonDataSet = {};
1752
1788
  const conversationStarters = [];
1753
1789
  const paths = spec.paths;
1754
1790
  for (const pathUrl in paths) {
@@ -1780,11 +1816,17 @@ class ManifestUpdater {
1780
1816
  try {
1781
1817
  const { json } = Utils.getResponseJson(operationItem);
1782
1818
  if (json.schema) {
1783
- const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, false, 5);
1819
+ const [card, jsonPath, jsonData] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, false, 5);
1784
1820
  const responseSemantic = wrapResponseSemantics(card, jsonPath);
1785
1821
  funcObj.capabilities = {
1786
1822
  response_semantics: responseSemantic,
1787
1823
  };
1824
+ if (jsonPath === "$") {
1825
+ jsonDataSet[safeFunctionName] = jsonData;
1826
+ }
1827
+ else {
1828
+ jsonDataSet[safeFunctionName] = jsonData[jsonPath];
1829
+ }
1788
1830
  }
1789
1831
  }
1790
1832
  catch (err) {
@@ -1956,7 +1998,7 @@ class ManifestUpdater {
1956
1998
  .map((text) => ({ text }));
1957
1999
  }
1958
2000
  }
1959
- return [apiPlugin, warnings];
2001
+ return [apiPlugin, warnings, jsonDataSet];
1960
2002
  }
1961
2003
  static async updateManifest(manifestPath, outputSpecPath, spec, options, adaptiveCardFolder, authInfo) {
1962
2004
  try {
@@ -2191,6 +2233,39 @@ class SpecParser {
2191
2233
  async listSupportedAPIInfo() {
2192
2234
  throw new Error("Method not implemented.");
2193
2235
  }
2236
+ async addAuthScheme(authName, authType, authParameters, signal) {
2237
+ try {
2238
+ await this.loadSpec();
2239
+ const spec = this.spec;
2240
+ if (signal === null || signal === void 0 ? void 0 : signal.aborted) {
2241
+ throw new SpecParserError(ConstantString.CancelledMessage, ErrorType.Cancelled);
2242
+ }
2243
+ if (!spec.components) {
2244
+ spec.components = {};
2245
+ }
2246
+ if (!spec.components.securitySchemes) {
2247
+ spec.components.securitySchemes = {};
2248
+ }
2249
+ spec.components.securitySchemes[authName] = Utils.getAuthSchemaObject(authType, authParameters);
2250
+ const paths = spec.paths;
2251
+ for (const path in paths) {
2252
+ const methods = paths[path];
2253
+ for (const method in methods) {
2254
+ const operationId = methods[method].operationId;
2255
+ if (authParameters.apis.includes(operationId)) {
2256
+ methods[method].security = [{ [authName]: [] }];
2257
+ }
2258
+ }
2259
+ }
2260
+ await this.saveFilterSpec(this.pathOrSpec, this.spec);
2261
+ }
2262
+ catch (err) {
2263
+ if (err instanceof SpecParserError) {
2264
+ throw err;
2265
+ }
2266
+ throw new SpecParserError(err.toString(), ErrorType.AddAuthFailed);
2267
+ }
2268
+ }
2194
2269
  /**
2195
2270
  * Lists all the OpenAPI operations in the specification file.
2196
2271
  * @returns A string array that represents the HTTP method and path of each operation, such as ['GET /pets/{petId}', 'GET /user/{userId}']
@@ -2302,7 +2377,7 @@ class SpecParser {
2302
2377
  * @param outputSpecPath File path of the new OpenAPI specification file to generate. If not specified or empty, no spec file will be generated.
2303
2378
  * @param pluginFilePath File path of the api plugin file to generate.
2304
2379
  */
2305
- async generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, existingPluginFilePath, signal) {
2380
+ async generateForCopilot(manifestPath, filter, outputSpecPath, pluginFilePath, existingPluginFilePath, signal, adaptiveCardUpdateStrategy) {
2306
2381
  const result = {
2307
2382
  allSuccess: true,
2308
2383
  warnings: [],
@@ -2347,7 +2422,8 @@ class SpecParser {
2347
2422
  }
2348
2423
  : undefined;
2349
2424
  const authMap = Utils.getAuthMap(newSpec);
2350
- const [updatedManifest, apiPlugin, warnings] = await ManifestUpdater.updateManifestWithAiPlugin(manifestPath, outputSpecPath, pluginFilePath, newSpec, this.options, authMap, existingPluginManifestInfo);
2425
+ const [updatedManifest, apiPlugin, warnings, jsonDataSet] = await ManifestUpdater.updateManifestWithAiPlugin(manifestPath, outputSpecPath, pluginFilePath, newSpec, this.options, authMap, existingPluginManifestInfo);
2426
+ await this.separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet, adaptiveCardUpdateStrategy);
2351
2427
  result.warnings.push(...warnings);
2352
2428
  await fs.outputJSON(manifestPath, updatedManifest, { spaces: 4 });
2353
2429
  await fs.outputJSON(pluginFilePath, apiPlugin, { spaces: 4 });
@@ -2436,6 +2512,7 @@ class SpecParser {
2436
2512
  const newSpecs = await this.getFilteredSpecs(filter, signal);
2437
2513
  const newSpec = newSpecs[1];
2438
2514
  const apiPlugin = (await fs.readJSON(pluginFilePath));
2515
+ const jsonDataSet = {};
2439
2516
  const paths = newSpec.paths;
2440
2517
  for (const pathUrl in paths) {
2441
2518
  const pathItem = paths[pathUrl];
@@ -2453,7 +2530,13 @@ class SpecParser {
2453
2530
  }
2454
2531
  const { json } = Utils.getResponseJson(operationItem);
2455
2532
  if (json.schema) {
2456
- const [card, jsonPath] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, false, 5);
2533
+ const [card, jsonPath, jsonData] = AdaptiveCardGenerator.generateAdaptiveCard(operationItem, false, 5);
2534
+ if (jsonPath === "$") {
2535
+ jsonDataSet[safeFunctionName] = jsonData;
2536
+ }
2537
+ else {
2538
+ jsonDataSet[safeFunctionName] = jsonData[jsonPath];
2539
+ }
2457
2540
  const responseSemantic = wrapResponseSemantics(card, jsonPath);
2458
2541
  apiPlugin.functions.find((func) => func.name === safeFunctionName).capabilities = {
2459
2542
  response_semantics: responseSemantic,
@@ -2468,8 +2551,44 @@ class SpecParser {
2468
2551
  }
2469
2552
  }
2470
2553
  }
2554
+ await this.separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet);
2471
2555
  await fs.outputJSON(pluginFilePath, apiPlugin, { spaces: 4 });
2472
2556
  }
2557
+ async separateAdaptiveCards(apiPlugin, pluginFilePath, jsonDataSet = {}, adaptiveCardUpdateStrategy) {
2558
+ var _a, _b;
2559
+ const functions = apiPlugin.functions;
2560
+ if (!adaptiveCardUpdateStrategy) {
2561
+ adaptiveCardUpdateStrategy = AdaptiveCardUpdateStrategy.CreateNew;
2562
+ }
2563
+ if (functions) {
2564
+ const adaptiveCardFolder = path.join(path.dirname(pluginFilePath), "adaptiveCards");
2565
+ for (const func of functions) {
2566
+ if ((_a = func.capabilities) === null || _a === void 0 ? void 0 : _a.response_semantics) {
2567
+ const responseSemantic = func.capabilities.response_semantics;
2568
+ const card = responseSemantic.static_template;
2569
+ if (card && Object.keys(card).length !== 0) {
2570
+ let cardName = func.name;
2571
+ if (adaptiveCardUpdateStrategy === AdaptiveCardUpdateStrategy.CreateNew) {
2572
+ cardName = this.findUniqueCardName(func.name, adaptiveCardFolder);
2573
+ }
2574
+ else {
2575
+ if (adaptiveCardUpdateStrategy === AdaptiveCardUpdateStrategy.KeepExisting &&
2576
+ fs.existsSync(path.join(adaptiveCardFolder, `${cardName}.json`))) {
2577
+ responseSemantic.static_template = { file: `adaptiveCards/${cardName}.json` };
2578
+ continue;
2579
+ }
2580
+ }
2581
+ const cardPath = path.join(adaptiveCardFolder, `${cardName}.json`);
2582
+ const dataPath = path.join(adaptiveCardFolder, `${cardName}.data.json`);
2583
+ responseSemantic.static_template = { file: `adaptiveCards/${cardName}.json` };
2584
+ await fs.outputJSON(cardPath, card, { spaces: 4 });
2585
+ const data = (_b = jsonDataSet[cardName]) !== null && _b !== void 0 ? _b : {};
2586
+ await fs.outputJSON(dataPath, data, { spaces: 4 });
2587
+ }
2588
+ }
2589
+ }
2590
+ }
2591
+ }
2473
2592
  async loadSpec() {
2474
2593
  if (!this.spec) {
2475
2594
  const spec = (await this.parser.parse(this.pathOrSpec));
@@ -2512,7 +2631,19 @@ class SpecParser {
2512
2631
  const specResolved = Utils.resolveEnv(specString);
2513
2632
  return JSON.parse(specResolved);
2514
2633
  }
2634
+ findUniqueCardName(defaultName, cardFolder) {
2635
+ let cardName = defaultName;
2636
+ let counter = 1;
2637
+ while (true) {
2638
+ if (!fs.existsSync(path.join(cardFolder, cardName + ".json"))) {
2639
+ break;
2640
+ }
2641
+ cardName = `${defaultName}${counter}`;
2642
+ counter++;
2643
+ }
2644
+ return cardName;
2645
+ }
2515
2646
  }
2516
2647
 
2517
- export { AdaptiveCardGenerator, ConstantString, ErrorType, ProjectType, SpecParser, SpecParserError, Utils, ValidationStatus, WarningType };
2648
+ export { AdaptiveCardGenerator, AdaptiveCardUpdateStrategy, ConstantString, ErrorType, ProjectType, SpecParser, SpecParserError, Utils, ValidationStatus, WarningType };
2518
2649
  //# sourceMappingURL=index.esm2017.mjs.map