feat: Implement OpenRouter API client for managing headers and retrieving model pricing.
This commit is contained in:
19
OpenRouter/OpenRouterHeaders.cs
Normal file
19
OpenRouter/OpenRouterHeaders.cs
Normal file
@@ -0,0 +1,19 @@
|
|||||||
|
using System.Net.Http;
|
||||||
|
|
||||||
|
namespace AnchorCli.OpenRouter;
|
||||||
|
|
||||||
|
/// <summary>
|
||||||
|
/// Provides extension methods for adding OpenRouter-specific HTTP headers.
|
||||||
|
/// </summary>
|
||||||
|
public static class OpenRouterHeaders
|
||||||
|
{
|
||||||
|
/// <summary>
|
||||||
|
/// Applies the required OpenRouter headers to the specified HttpClient.
|
||||||
|
/// </summary>
|
||||||
|
public static void ApplyTo(HttpClient httpClient)
|
||||||
|
{
|
||||||
|
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("HTTP-Referer", "https://git.technopunk.space/tomi/AnchorCli");
|
||||||
|
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-OpenRouter-Title", "Anchor CLI");
|
||||||
|
httpClient.DefaultRequestHeaders.TryAddWithoutValidation("X-OpenRouter-Categories", "cli-agent");
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -14,6 +14,11 @@ internal sealed class PricingProvider
|
|||||||
private static readonly HttpClient Http = new();
|
private static readonly HttpClient Http = new();
|
||||||
private Dictionary<string, ModelInfo>? _models;
|
private Dictionary<string, ModelInfo>? _models;
|
||||||
|
|
||||||
|
static PricingProvider()
|
||||||
|
{
|
||||||
|
OpenRouterHeaders.ApplyTo(Http);
|
||||||
|
}
|
||||||
|
|
||||||
/// <summary>
|
/// <summary>
|
||||||
/// Fetches the full model list from OpenRouter (cached after first call).
|
/// Fetches the full model list from OpenRouter (cached after first call).
|
||||||
/// </summary>
|
/// </summary>
|
||||||
|
|||||||
@@ -88,9 +88,13 @@ AnsiConsole.Write(infoTable);
|
|||||||
AnsiConsole.WriteLine();
|
AnsiConsole.WriteLine();
|
||||||
|
|
||||||
// ── Build the chat client with tool-calling support ─────────────────────
|
// ── Build the chat client with tool-calling support ─────────────────────
|
||||||
|
var httpClient = new HttpClient();
|
||||||
|
OpenRouterHeaders.ApplyTo(httpClient);
|
||||||
|
|
||||||
var openAiClient = new OpenAIClient(new ApiKeyCredential(apiKey), new OpenAIClientOptions
|
var openAiClient = new OpenAIClient(new ApiKeyCredential(apiKey), new OpenAIClientOptions
|
||||||
{
|
{
|
||||||
Endpoint = new Uri(endpoint)
|
Endpoint = new Uri(endpoint),
|
||||||
|
Transport = new System.ClientModel.Primitives.HttpClientPipelineTransport(httpClient)
|
||||||
});
|
});
|
||||||
|
|
||||||
IChatClient innerClient = openAiClient.GetChatClient(model).AsIChatClient();
|
IChatClient innerClient = openAiClient.GetChatClient(model).AsIChatClient();
|
||||||
|
|||||||
Reference in New Issue
Block a user