Skip to content

Commit 36cc030

Browse files
author
Andrew Omondi
committed
Add plugin generation for openAI
1 parent 18c2e2e commit 36cc030

File tree

2 files changed

+34
-11
lines changed

2 files changed

+34
-11
lines changed

Diff for: src/Kiota.Builder/Plugins/PluginsGenerationService.cs

+21-10
Original file line numberDiff line numberDiff line change
@@ -74,10 +74,17 @@ public async Task GenerateManifestAsync(CancellationToken cancellationToken = de
7474
var apiManifest = new ApiManifestDocument("application"); //TODO add application name
7575
// pass empty config hash so that its not included in this manifest.
7676
apiManifest.ApiDependencies.AddOrReplace(Configuration.ClientClassName, Configuration.ToApiDependency(string.Empty, TreeNode?.GetRequestInfo().ToDictionary(static x => x.Key, static x => x.Value) ?? [], WorkingDirectory));
77+
var publisherName = string.IsNullOrEmpty(OAIDocument.Info?.Contact?.Name)
78+
? DefaultContactName
79+
: OAIDocument.Info.Contact.Name;
80+
var publisherEmail = string.IsNullOrEmpty(OAIDocument.Info?.Contact?.Email)
81+
? DefaultContactEmail
82+
: OAIDocument.Info.Contact.Email;
83+
apiManifest.Publisher = new Publisher(publisherName, publisherEmail);
7784
apiManifest.Write(writer);
7885
break;
7986
case PluginType.OpenAI:
80-
var pluginDocumentV1 = GetV1ManifestDocument();
87+
var pluginDocumentV1 = GetV1ManifestDocument(descriptionRelativePath);
8188
pluginDocumentV1.Write(writer);
8289
break;
8390
default:
@@ -86,24 +93,24 @@ public async Task GenerateManifestAsync(CancellationToken cancellationToken = de
8693
await writer.FlushAsync(cancellationToken).ConfigureAwait(false);
8794
}
8895
}
89-
private PluginManifestDocument GetV1ManifestDocument()
96+
private PluginManifestDocument GetV1ManifestDocument(string openApiDocumentPath)
9097
{
9198
var descriptionForHuman = OAIDocument.Info?.Description.CleanupXMLString() is string d && !string.IsNullOrEmpty(d) ? d : $"Description for {OAIDocument.Info?.Title.CleanupXMLString()}";
9299
var manifestInfo = ExtractInfoFromDocument(OAIDocument.Info);
93100
return new PluginManifestDocument
94101
{
95102
SchemaVersion = "v1",
96103
NameForHuman = OAIDocument.Info?.Title.CleanupXMLString(),
97-
// TODO name for model ???
104+
NameForModel = OAIDocument.Info?.Title.CleanupXMLString(),
98105
DescriptionForHuman = descriptionForHuman,
99106
DescriptionForModel = manifestInfo.DescriptionForModel ?? descriptionForHuman,
100107
Auth = new V1AnonymousAuth(),
101108
Api = new Api()
102109
{
103110
Type = ApiType.openapi,
104-
URL = Configuration.OpenAPIFilePath
111+
URL = openApiDocumentPath
105112
},
106-
ContactEmail = OAIDocument.Info?.Contact?.Email,
113+
ContactEmail = manifestInfo.ContactEmail,
107114
LogoUrl = manifestInfo.LogoUrl,
108115
LegalInfoUrl = manifestInfo.LegalUrl,
109116
};
@@ -118,10 +125,10 @@ private PluginManifestDocument GetManifestDocument(string openApiDocumentPath)
118125
{
119126
SchemaVersion = "v2",
120127
NameForHuman = OAIDocument.Info?.Title.CleanupXMLString(),
121-
// TODO name for model ???
128+
NameForModel = OAIDocument.Info?.Title.CleanupXMLString(),
122129
DescriptionForHuman = descriptionForHuman,
123130
DescriptionForModel = manifestInfo.DescriptionForModel ?? descriptionForHuman,
124-
ContactEmail = OAIDocument.Info?.Contact?.Email,
131+
ContactEmail = manifestInfo.ContactEmail,
125132
Namespace = Configuration.ClientClassName,
126133
LogoUrl = manifestInfo.LogoUrl,
127134
LegalInfoUrl = manifestInfo.LegalUrl,
@@ -150,6 +157,9 @@ private static OpenApiManifestInfo ExtractInfoFromDocument(OpenApiInfo? openApiI
150157
string? legalUrl = null;
151158
string? logoUrl = null;
152159
string? privacyUrl = null;
160+
string contactEmail = string.IsNullOrEmpty(openApiInfo.Contact?.Email)
161+
? DefaultContactEmail
162+
: openApiInfo.Contact.Email;
153163

154164
if (openApiInfo.Extensions.TryGetValue(OpenApiDescriptionForModelExtension.Name, out var descriptionExtension) &&
155165
descriptionExtension is OpenApiDescriptionForModelExtension extension &&
@@ -162,11 +172,12 @@ descriptionExtension is OpenApiDescriptionForModelExtension extension &&
162172
if (openApiInfo.Extensions.TryGetValue(OpenApiPrivacyPolicyUrlExtension.Name, out var privacyExtension) && privacyExtension is OpenApiPrivacyPolicyUrlExtension privacy)
163173
privacyUrl = privacy.Privacy;
164174

165-
return new OpenApiManifestInfo(descriptionForModel, legalUrl, logoUrl, privacyUrl);
175+
return new OpenApiManifestInfo(descriptionForModel, legalUrl, logoUrl, privacyUrl, contactEmail);
166176

167177
}
168-
169-
private sealed record OpenApiManifestInfo(string? DescriptionForModel = null, string? LegalUrl = null, string? LogoUrl = null, string? PrivacyUrl = null);
178+
private const string DefaultContactName = "publisher-name";
179+
private const string DefaultContactEmail = "[email protected]";
180+
private sealed record OpenApiManifestInfo(string? DescriptionForModel = null, string? LegalUrl = null, string? LogoUrl = null, string? PrivacyUrl = null, string ContactEmail = DefaultContactEmail);
170181
private static (OpenApiRuntime[], Function[]) GetRuntimesAndFunctionsFromTree(OpenApiUrlTreeNode currentNode, string openApiDocumentPath)
171182
{
172183
var runtimes = new List<OpenApiRuntime>();

Diff for: tests/Kiota.Builder.Tests/Plugins/PluginsGenerationServiceTests.cs

+13-1
Original file line numberDiff line numberDiff line change
@@ -78,14 +78,26 @@ public async Task GeneratesManifest()
7878

7979
Assert.True(File.Exists(Path.Combine(outputDirectory, ManifestFileName)));
8080
Assert.True(File.Exists(Path.Combine(outputDirectory, "client-apimanifest.json")));
81-
Assert.True(File.Exists(Path.Combine(outputDirectory, "openai-plugins.json")));
81+
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenAIPluginFileName)));
8282
Assert.True(File.Exists(Path.Combine(outputDirectory, OpenApiFileName)));
83+
84+
// Validate the v2 plugin
8385
var manifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, ManifestFileName));
8486
using var jsonDocument = JsonDocument.Parse(manifestContent);
8587
var resultingManifest = PluginManifestDocument.Load(jsonDocument.RootElement);
88+
Assert.NotNull(resultingManifest.Document);
8689
Assert.Equal(OpenApiFileName, resultingManifest.Document.Runtimes.OfType<OpenApiRuntime>().First().Spec.Url);
8790
Assert.Empty(resultingManifest.Problems);
91+
92+
// Validate the v1 plugin
93+
var v1ManifestContent = await File.ReadAllTextAsync(Path.Combine(outputDirectory, OpenAIPluginFileName));
94+
using var v1JsonDocument = JsonDocument.Parse(v1ManifestContent);
95+
var v1Manifest = PluginManifestDocument.Load(v1JsonDocument.RootElement);
96+
Assert.NotNull(resultingManifest.Document);
97+
Assert.Equal(OpenApiFileName, v1Manifest.Document.Api.URL);
98+
Assert.Empty(v1Manifest.Problems);
8899
}
89100
private const string ManifestFileName = "client-microsoft.json";
101+
private const string OpenAIPluginFileName = "openai-plugins.json";
90102
private const string OpenApiFileName = "client-openapi.yml";
91103
}

0 commit comments

Comments
 (0)