Skip to content

Commit 98a38fa

Browse files
authored
Fix typos and code style (#377)
* Extension methods: use the same namespace of the class being extended * Fix typos * Removed unused vars and params * Use correct cancellation token
1 parent 0d9727a commit 98a38fa

File tree

9 files changed

+65
-46
lines changed

9 files changed

+65
-46
lines changed

dotnet/src/SemanticKernel.Skills/Skills.OpenAPI/Extensions/KernelChatGptPluginExtensions.cs

+15-14
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,8 @@
1414
using Microsoft.SemanticKernel.Skills.OpenAPI.Authentication;
1515
using Microsoft.SemanticKernel.Skills.OpenAPI.Skills;
1616

17-
namespace Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
17+
// ReSharper disable once CheckNamespace
18+
namespace Microsoft.SemanticKernel;
1819

1920
/// <summary>
2021
/// Class for extensions methods for IKernel interface.
@@ -32,7 +33,12 @@ public static class KernelChatGptPluginExtensions
3233
/// <param name="cancellationToken">The cancellation token.</param>
3334
/// <returns>A list of all the semantic functions representing the skill.</returns>
3435
public static async Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSkillFromUrlAsync(
35-
this IKernel kernel, string skillName, Uri url, HttpClient? httpClient = null, AuthenticateRequestAsyncCallback? authCallback = null, CancellationToken cancellationToken = default)
36+
this IKernel kernel,
37+
string skillName,
38+
Uri url,
39+
HttpClient? httpClient = null,
40+
AuthenticateRequestAsyncCallback? authCallback = null,
41+
CancellationToken cancellationToken = default)
3642
{
3743
Verify.ValidSkillName(skillName);
3844

@@ -47,7 +53,7 @@ public static async Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSk
4753
// log: null);
4854

4955
//using HttpClient client = new HttpClient(retryHandler, false);
50-
using HttpClient client = new HttpClient();
56+
using HttpClient client = new();
5157

5258
response = await client.GetAsync(url, cancellationToken).ConfigureAwait(false);
5359
}
@@ -83,7 +89,8 @@ public static async Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSk
8389
/// <returns>A list of all the semantic functions representing the skill.</returns>
8490
public static async Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSkillFromResourceAsync(
8591
this IKernel kernel,
86-
string skillName, HttpClient? httpClient = null,
92+
string skillName,
93+
HttpClient? httpClient = null,
8794
AuthenticateRequestAsyncCallback? authCallback = null,
8895
CancellationToken cancellationToken = default)
8996
{
@@ -135,12 +142,10 @@ public async static Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSk
135142
var chatGptPluginPath = Path.Join(skillDir, CHATGPT_PLUGIN_FILE);
136143
if (!File.Exists(chatGptPluginPath))
137144
{
138-
throw new FileNotFoundException($"No ChatGPT plugin for the specified path - {chatGptPluginPath} is found.");
145+
throw new FileNotFoundException($"No ChatGPT plugin for the specified path - {chatGptPluginPath} is found");
139146
}
140147

141-
kernel.Log.LogTrace("Registering Rest functions from {0} ChatGPT Plugin.", chatGptPluginPath);
142-
143-
var skill = new Dictionary<string, ISKFunction>();
148+
kernel.Log.LogTrace("Registering Rest functions from {0} ChatGPT Plugin", chatGptPluginPath);
144149

145150
using var stream = File.OpenRead(chatGptPluginPath);
146151

@@ -153,15 +158,13 @@ public async static Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSk
153158
/// <param name="kernel">Semantic Kernel instance.</param>
154159
/// <param name="skillName">Name of the skill to register.</param>
155160
/// <param name="filePath">File path to the ChatGPT plugin definition.</param>
156-
/// <param name="httpClient">Optional HttpClient to use for the request.</param>
157161
/// <param name="authCallback">Optional callback for adding auth data to the API requests.</param>
158162
/// <param name="cancellationToken">The cancellation token.</param>
159163
/// <returns>A list of all the semantic functions representing the skill.</returns>
160164
public async static Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSkillSkillFromFileAsync(
161165
this IKernel kernel,
162166
string skillName,
163167
string filePath,
164-
HttpClient? httpClient = null,
165168
AuthenticateRequestAsyncCallback? authCallback = null,
166169
CancellationToken cancellationToken = default)
167170
{
@@ -172,8 +175,6 @@ public async static Task<IDictionary<string, ISKFunction>> ImportChatGptPluginSk
172175

173176
kernel.Log.LogTrace("Registering Rest functions from {0} ChatGPT Plugin.", filePath);
174177

175-
var skill = new Dictionary<string, ISKFunction>();
176-
177178
using var stream = File.OpenRead(filePath);
178179

179180
return await kernel.RegisterOpenApiSkillAsync(stream, skillName, authCallback, cancellationToken);
@@ -186,13 +187,13 @@ private static string ParseOpenApiUrl(string gptPluginJson)
186187
string? apiType = gptPlugin?["api"]?["type"]?.ToString();
187188
if (string.IsNullOrWhiteSpace(apiType) || apiType != "openapi")
188189
{
189-
throw new InvalidOperationException($"Invalid ChatGPT plugin document. Supported api types are: openapi.");
190+
throw new InvalidOperationException($"Invalid ChatGPT plugin document. Supported api types are: openapi");
190191
}
191192

192193
string? openApiUrl = gptPlugin?["api"]?["url"]?.ToString();
193194
if (string.IsNullOrWhiteSpace(openApiUrl))
194195
{
195-
throw new InvalidOperationException($"Invalid ChatGPT plugin document. OpenAPI url is missing.");
196+
throw new InvalidOperationException($"Invalid ChatGPT plugin document. OpenAPI url is missing");
196197
}
197198

198199
return openApiUrl;

dotnet/src/SemanticKernel.Skills/Skills.OpenAPI/Extensions/KernelOpenApiExtensions.cs

+40-17
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,8 @@
1818
using Microsoft.SemanticKernel.Skills.OpenAPI.Rest;
1919
using Microsoft.SemanticKernel.Skills.OpenAPI.Skills;
2020

21-
namespace Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
21+
// ReSharper disable once CheckNamespace
22+
namespace Microsoft.SemanticKernel;
2223

2324
/// <summary>
2425
/// Class for extensions methods for <see cref="IKernel"/> interface.
@@ -92,7 +93,11 @@ public static async Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFro
9293
/// <param name="authCallback">Optional callback for adding auth data to the API requests.</param>
9394
/// <param name="cancellationToken">The cancellation token.</param>
9495
/// <returns>A list of all the semantic functions representing the skill.</returns>
95-
public static Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFromResourceAsync(this IKernel kernel, string skillName, AuthenticateRequestAsyncCallback? authCallback = null, CancellationToken cancellationToken = default)
96+
public static Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFromResourceAsync(
97+
this IKernel kernel,
98+
string skillName,
99+
AuthenticateRequestAsyncCallback? authCallback = null,
100+
CancellationToken cancellationToken = default)
96101
{
97102
Verify.ValidSkillName(skillName);
98103

@@ -157,14 +162,19 @@ public static async Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFro
157162
/// <param name="authCallback">Optional callback for adding auth data to the API requests.</param>
158163
/// <param name="cancellationToken">The cancellation token.</param>
159164
/// <returns>A list of all the semantic functions representing the skill.</returns>
160-
public static async Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFromFileAsync(this IKernel kernel, string skillName, string filePath, AuthenticateRequestAsyncCallback? authCallback = null, CancellationToken cancellationToken = default)
165+
public static async Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFromFileAsync(
166+
this IKernel kernel,
167+
string skillName,
168+
string filePath,
169+
AuthenticateRequestAsyncCallback? authCallback = null,
170+
CancellationToken cancellationToken = default)
161171
{
162172
if (!File.Exists(filePath))
163173
{
164174
throw new FileNotFoundException($"No OpenApi document for the specified path - {filePath} is found.");
165175
}
166176

167-
kernel.Log.LogTrace("Registering Rest functions from {0} OpenApi document.", filePath);
177+
kernel.Log.LogTrace("Registering Rest functions from {0} OpenApi document", filePath);
168178

169179
// TODO: never used, why?
170180
var skill = new Dictionary<string, ISKFunction>();
@@ -183,7 +193,12 @@ public static async Task<IDictionary<string, ISKFunction>> ImportOpenApiSkillFro
183193
/// <param name="authCallback">Optional callback for adding auth data to the API requests.</param>
184194
/// <param name="cancellationToken">The cancellation token.</param>
185195
/// <returns>A list of all the semantic functions representing the skill.</returns>
186-
public static async Task<IDictionary<string, ISKFunction>> RegisterOpenApiSkillAsync(this IKernel kernel, Stream documentStream, string skillName, AuthenticateRequestAsyncCallback? authCallback = null, CancellationToken cancellationToken = default)
196+
public static async Task<IDictionary<string, ISKFunction>> RegisterOpenApiSkillAsync(
197+
this IKernel kernel,
198+
Stream documentStream,
199+
string skillName,
200+
AuthenticateRequestAsyncCallback? authCallback = null,
201+
CancellationToken cancellationToken = default)
187202
{
188203
Verify.NotNull(kernel, nameof(kernel));
189204
Verify.ValidSkillName(skillName);
@@ -199,15 +214,15 @@ public static async Task<IDictionary<string, ISKFunction>> RegisterOpenApiSkillA
199214
{
200215
try
201216
{
202-
kernel.Log.LogTrace("Registering Rest function {0}.{1}.", skillName, operation.Id);
217+
kernel.Log.LogTrace("Registering Rest function {0}.{1}", skillName, operation.Id);
203218
var function = kernel.RegisterRestApiFunction(skillName, operation, authCallback, cancellationToken);
204219
skill[function.Name] = function;
205220
}
206221
catch (Exception ex) when (!ex.IsCriticalException())
207222
{
208223
//Logging the exception and keep registering other Rest functions
209-
kernel.Log.LogWarning(ex, "Something went wrong while rendering the Rest function. Function: {0}.{1}. Error: {2}", skillName, operation.Id,
210-
ex.Message);
224+
kernel.Log.LogWarning(ex, "Something went wrong while rendering the Rest function. Function: {0}.{1}. Error: {2}",
225+
skillName, operation.Id, ex.Message);
211226
}
212227
}
213228

@@ -225,7 +240,12 @@ public static async Task<IDictionary<string, ISKFunction>> RegisterOpenApiSkillA
225240
/// <param name="authCallback">Optional callback for adding auth data to the API requests.</param>
226241
/// <param name="cancellationToken">The cancellation token.</param>
227242
/// <returns>An instance of <see cref="SKFunction"/> class.</returns>
228-
private static ISKFunction RegisterRestApiFunction(this IKernel kernel, string skillName, RestApiOperation operation, AuthenticateRequestAsyncCallback? authCallback = null, CancellationToken cancellationToken = default)
243+
private static ISKFunction RegisterRestApiFunction(
244+
this IKernel kernel,
245+
string skillName,
246+
RestApiOperation operation,
247+
AuthenticateRequestAsyncCallback? authCallback = null,
248+
CancellationToken cancellationToken = default)
229249
{
230250
var restOperationParameters = operation.GetParameters();
231251

@@ -260,7 +280,7 @@ async Task<SKContext> ExecuteAsync(SKContext context)
260280
}
261281
}
262282

263-
var result = await runner.RunAsync(operation, arguments, context.CancellationToken).ConfigureAwait(false);
283+
var result = await runner.RunAsync(operation, arguments, cancellationToken).ConfigureAwait(false);
264284
if (result != null)
265285
{
266286
context.Variables.Update(result.ToString());
@@ -276,18 +296,21 @@ async Task<SKContext> ExecuteAsync(SKContext context)
276296
return context;
277297
}
278298

279-
// TODO: to be fixed later
280-
#pragma warning disable CA2000 // Dispose objects before losing scope.
281-
var function = new SKFunction(
282-
delegateType: SKFunction.DelegateTypes.ContextSwitchInSKContextOutTaskSKContext,
283-
delegateFunction: ExecuteAsync,
284-
parameters: restOperationParameters.Select(p => new ParameterView()
299+
var parameters = restOperationParameters
300+
.Select(p => new ParameterView
285301
{
286302
Name = p.AlternativeName ?? p.Name,
287303
Description = p.Name,
288304
DefaultValue = p.DefaultValue ?? string.Empty
289305
})
290-
.ToList(), //functionConfig.PromptTemplate.GetParameters(),
306+
.ToList();
307+
308+
// TODO: to be fixed later
309+
#pragma warning disable CA2000 // Dispose objects before losing scope.
310+
var function = new SKFunction(
311+
delegateType: SKFunction.DelegateTypes.ContextSwitchInSKContextOutTaskSKContext,
312+
delegateFunction: ExecuteAsync,
313+
parameters: parameters,
291314
description: operation.Description,
292315
skillName: skillName,
293316
functionName: operation.Id,

dotnet/src/SemanticKernel.Skills/Skills.OpenAPI/Extensions/RestApiOperationExtensions.cs renamed to dotnet/src/SemanticKernel.Skills/Skills.OpenAPI/Extensions/RestApiOperationOpenAPIExtensions.cs

+3-3
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,14 @@
33
using System.Collections.Generic;
44
using System.Linq;
55
using System.Text.RegularExpressions;
6-
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
76

8-
namespace Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
7+
// ReSharper disable once CheckNamespace
8+
namespace Microsoft.SemanticKernel.Skills.OpenAPI.Model;
99

1010
/// <summary>
1111
/// Class for extensions methods for the <see cref="RestApiOperation"/> class.
1212
/// </summary>
13-
internal static class RestApiOperationExtensions
13+
internal static class RestApiOperationOpenAPIExtensions
1414
{
1515
/// <summary>
1616
/// Returns list of REST API operation parameters.

dotnet/src/SemanticKernel.Skills/Skills.OpenAPI/OpenApi/OpenApiDocumentParser.cs

+7-7
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ internal class OpenApiDocumentParser : IOpenApiDocumentParser
2828
/// <inheritdoc/>
2929
public async Task<IList<RestApiOperation>> ParseAsync(Stream stream, CancellationToken cancellationToken = default)
3030
{
31-
var jsonObject = await this.DowngradeDocumentVersionToSuportedOneAsync(stream, cancellationToken).ConfigureAwait(false);
31+
var jsonObject = await this.DowngradeDocumentVersionToSupportedOneAsync(stream, cancellationToken).ConfigureAwait(false);
3232

3333
using var memoryStream = new MemoryStream(Encoding.UTF8.GetBytes(jsonObject.ToJson()));
3434

@@ -54,7 +54,7 @@ public async Task<IList<RestApiOperation>> ParseAsync(Stream stream, Cancellatio
5454
/// <param name="stream">The original OpenAPI document stream.</param>
5555
/// <param name="cancellationToken">The cancellation token.</param>
5656
/// <returns>OpenAPI document with downgraded document version.</returns>
57-
private async Task<JsonObject> DowngradeDocumentVersionToSuportedOneAsync(Stream stream, CancellationToken cancellationToken)
57+
private async Task<JsonObject> DowngradeDocumentVersionToSupportedOneAsync(Stream stream, CancellationToken cancellationToken)
5858
{
5959
var jsonObject = await ConvertContentToJsonAsync(stream, cancellationToken);
6060
if (jsonObject == null)
@@ -63,7 +63,7 @@ private async Task<JsonObject> DowngradeDocumentVersionToSuportedOneAsync(Stream
6363
throw new OpenApiDocumentParsingException($"Parsing of OpenAPI document failed.");
6464
}
6565

66-
if (!jsonObject.TryGetPropertyValue(OpenApiVersionPropetyName, out var propertyNode))
66+
if (!jsonObject.TryGetPropertyValue(OpenApiVersionPropertyName, out var propertyNode))
6767
{
6868
//The document is either malformed or has 2.x version that specifies document version in the 'swagger' property rather than in the 'openapi' one.
6969
return jsonObject;
@@ -83,18 +83,18 @@ private async Task<JsonObject> DowngradeDocumentVersionToSuportedOneAsync(Stream
8383

8484
if (version > s_latestSupportedVersion)
8585
{
86-
jsonObject[OpenApiVersionPropetyName] = s_latestSupportedVersion.ToString();
86+
jsonObject[OpenApiVersionPropertyName] = s_latestSupportedVersion.ToString();
8787
}
8888

8989
return jsonObject;
9090
}
9191

9292
/// <summary>
9393
/// Converts YAML content to JSON content.
94-
/// The method uses SharpYaml library that comes as a not-direct dependency of Microsoft.AopenAPI.NET library.
94+
/// The method uses SharpYaml library that comes as a not-direct dependency of Microsoft.OpenAPI.NET library.
9595
/// Should be replaced later when there's more convenient way to convert YAML content to JSON one.
9696
/// </summary>
97-
/// <param name="stream">The JAML/JSON content stream.</param>
97+
/// <param name="stream">The YAML/JSON content stream.</param>
9898
/// <param name="cancellationToken">Cancellation token.</param>
9999
/// <returns>JSON content stream.</returns>
100100
private static async Task<JsonObject?> ConvertContentToJsonAsync(Stream stream, CancellationToken cancellationToken = default)
@@ -371,7 +371,7 @@ private static IList<RestApiOperationPayloadProperty> GetPayloadProperties(strin
371371
/// <summary>
372372
/// Name of property that contains OpenAPI document version.
373373
/// </summary>
374-
private const string OpenApiVersionPropetyName = "openapi";
374+
private const string OpenApiVersionPropertyName = "openapi";
375375

376376
#endregion
377377
}

dotnet/src/SemanticKernel.Skills/Skills.UnitTests/OpenAPI/OpenApiDocumentParserV20Tests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using System.Net.Http;
88
using System.Threading.Tasks;
9-
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
109
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
1110
using Microsoft.SemanticKernel.Skills.OpenAPI.OpenApi;
1211
using SemanticKernel.Skills.UnitTests.OpenAPI.TestSkills;

dotnet/src/SemanticKernel.Skills/Skills.UnitTests/OpenAPI/OpenApiDocumentParserV30Tests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using System.Net.Http;
88
using System.Threading.Tasks;
9-
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
109
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
1110
using Microsoft.SemanticKernel.Skills.OpenAPI.OpenApi;
1211
using SemanticKernel.Skills.UnitTests.OpenAPI.TestSkills;

dotnet/src/SemanticKernel.Skills/Skills.UnitTests/OpenAPI/OpenApiDocumentParserV31Tests.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Linq;
77
using System.Net.Http;
88
using System.Threading.Tasks;
9-
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
109
using Microsoft.SemanticKernel.Skills.OpenAPI.Model;
1110
using Microsoft.SemanticKernel.Skills.OpenAPI.OpenApi;
1211
using SemanticKernel.Skills.UnitTests.OpenAPI.TestSkills;

samples/dotnet/kernel-syntax-examples/Example21_ChatGPTPlugins.cs

-1
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
using System.Threading.Tasks;
55
using Microsoft.SemanticKernel;
66
using Microsoft.SemanticKernel.Orchestration;
7-
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
87
using RepoUtils;
98

109
// ReSharper disable once InconsistentNaming

samples/dotnet/kernel-syntax-examples/Example22_OpenApiSkill.cs

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@
66
using System.Threading.Tasks;
77
using Microsoft.SemanticKernel;
88
using Microsoft.SemanticKernel.Orchestration;
9-
using Microsoft.SemanticKernel.Skills.OpenAPI.Extensions;
109
using Microsoft.SemanticKernel.Skills.OpenAPI.Skills;
1110
using RepoUtils;
1211

0 commit comments

Comments
 (0)