semantic-kernel
semantic-kernel copied to clipboard
Functionality to register RestApiOperationRunner in kernel config
Motivation and Context
This PR introduces a few changes:
- An instance of the RestApiOperationRunner needs to be registered through kernel config so that it can be reused by any skill that uses RestApiOperation functionality.
- RestApiOperationRunner doesn't create HttpClient instance anymore and requires a hosting app to provide the instance instead. SK SDK can't make assumptions about the way hosting apps create/manage HttpClient objects/instances - whether they use HttpClientFactory to create an instance of HttpClient instance per request, or an HttpClient instance is created using the new operator and stored as singleton, or maybe an instance per host?
These changes add more flexibility to SK SDK to support different scenarios a hosting app may require:
- Working with OpenAPI skills that are run against same host. In this case, the same set of Http handlers can be used for all the skills and only one HttpClient instance will be created.
- Working with OpenAPI skills that are run against different hosts. This case may require a set of Http handlers unique to each host and more than one insnance of HttpClient.
Description
This PR introduces the following changes:
- Two overloads of a new SetRestApiOperationRunner method are added to the KernalConfiguration class allowing a hosting app to supply either an instance of HttpClient or a callback that will be called when the RestApiOperationRunner sends Http requests. The callback can be used by the hosting app to route the Http requests to a dedicated HttpClient instance based on the host, header, url string parameter, etc...
- The RegisterRestApiFunction is changed to access the instance of the RestApiOperationRunner class from the Kernal config rather than creating the instance itself.
Contribution Checklist
- [x] The code builds clean without any errors or warnings
- [x] The PR follows SK Contribution Guidelines (https://github.com/microsoft/semantic-kernel/blob/main/CONTRIBUTING.md)
- [x] The code follows the .NET coding conventions (https://learn.microsoft.com/dotnet/csharp/fundamentals/coding-style/coding-conventions) verified with
dotnet format - [x] All unit tests pass, and I have added new tests where possible
- [x] I didn't break anyone :smile: