Thanks to visit codestin.com
Credit goes to github.com

Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,6 @@

var chat = builder.AddGitHubModel("chat", "openai/gpt-4o-mini");

// To set the GitHub Models API key define the value for the following parameter in User Secrets.
// Alternatively, you can set the environment variable GITHUB_TOKEN and comment the line below.
chat.WithApiKey(builder.AddParameter("github-api-key", secret: true));

builder.AddProject<Projects.GitHubModelsEndToEnd_WebStory>("webstory")
.WithExternalHttpEndpoints()
.WithReference(chat).WaitFor(chat);
Expand Down
7 changes: 4 additions & 3 deletions src/Aspire.Hosting.GitHub.Models/GitHubModelResource.cs
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@ public class GitHubModelResource : Resource, IResourceWithConnectionString, IRes
/// <param name="name">The name of the resource.</param>
/// <param name="model">The model name.</param>
/// <param name="organization">The organization.</param>
public GitHubModelResource(string name, string model, ParameterResource? organization) : base(name)
/// <param name="key">The key parameter.</param>
public GitHubModelResource(string name, string model, ParameterResource? organization, ParameterResource key) : base(name)
{
Model = model;
Organization = organization;
Key = key;
}

/// <summary>
Expand All @@ -39,10 +41,9 @@ public GitHubModelResource(string name, string model, ParameterResource? organiz
/// Gets or sets the API key (PAT or GitHub App minted token) for accessing GitHub Models.
/// </summary>
/// <remarks>
/// If not set, the value will be retrieved from the environment variable GITHUB_TOKEN.
/// The token must have the <code>models: read</code> permission if using a fine-grained PAT or GitHub App minted token.
/// </remarks>
public ParameterResource Key { get; set; } = new ParameterResource("github-api-key", p => Environment.GetEnvironmentVariable("GITHUB_TOKEN") ?? string.Empty, secret: true);
public ParameterResource Key { get; internal set; }

/// <summary>
/// Gets the connection string expression for the GitHub Models resource.
Expand Down
19 changes: 18 additions & 1 deletion src/Aspire.Hosting.GitHub.Models/GitHubModelsExtensions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,15 @@ public static IResourceBuilder<GitHubModelResource> AddGitHubModel(this IDistrib
ArgumentException.ThrowIfNullOrEmpty(name);
ArgumentException.ThrowIfNullOrEmpty(model);

var resource = new GitHubModelResource(name, model, organization?.Resource);
var defaultApiKeyParameter = builder.AddParameter($"{name}-gh-apikey", () =>
builder.Configuration[$"Parameters:{name}-gh-apikey"] ??
Environment.GetEnvironmentVariable("GITHUB_TOKEN") ??
throw new MissingParameterValueException($"GitHub API key parameter '{name}-gh-apikey' is missing and GITHUB_TOKEN environment variable is not set."),
secret: true);

var resource = new GitHubModelResource(name, model, organization?.Resource, defaultApiKeyParameter.Resource);

defaultApiKeyParameter.WithParentRelationship(resource);

return builder.AddResource(resource)
.WithInitialState(new()
Expand Down Expand Up @@ -66,11 +74,20 @@ await evt.Eventing.PublishAsync(new ConnectionStringAvailableEvent(r, evt.Servic
/// <param name="builder">The resource builder.</param>
/// <param name="apiKey">The API key parameter.</param>
/// <returns>The resource builder.</returns>
/// <exception cref="ArgumentException">Thrown when the provided parameter is not marked as secret.</exception>
public static IResourceBuilder<GitHubModelResource> WithApiKey(this IResourceBuilder<GitHubModelResource> builder, IResourceBuilder<ParameterResource> apiKey)
{
ArgumentNullException.ThrowIfNull(builder);
ArgumentNullException.ThrowIfNull(apiKey);

if (!apiKey.Resource.Secret)
{
throw new ArgumentException("The API key parameter must be marked as secret. Use AddParameter with secret: true when creating the parameter.", nameof(apiKey));
}

// Remove the existing API key parameter
builder.ApplicationBuilder.Resources.Remove(builder.Resource.Key);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should we only remove it if it was the one we added? Say you called WithApiKey twice.

Copy link
Member

@davidfowl davidfowl Jul 15, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ah like switching. Good point!

var p0 = builder.AddParameter("p0", secret: true);
var p1 = builder.AddParameter("p1", secret: true);

var model = builder.AddGithubModel("ghm").WithApiKey(p0).WithApiKey(p1);

This will remove p0 from the model. Yes we should fix this and only ever remove the built in parameter.


builder.Resource.Key = apiKey.Resource;

return builder;
Expand Down
31 changes: 17 additions & 14 deletions src/Aspire.Hosting.GitHub.Models/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,7 @@ Then, in the _AppHost.cs_ file of `AppHost`, add a GitHub Model resource and con
```csharp
var builder = DistributedApplication.CreateBuilder(args);

var apiKey = builder.AddParameter("github-api-key", secret: true);

var chat = builder.AddGitHubModel("chat", "openai/gpt-4o-mini")
.WithApiKey(apiKey);
var chat = builder.AddGitHubModel("chat", "openai/gpt-4o-mini");

var myService = builder.AddProject<Projects.MyService>()
.WithReference(chat);
Expand All @@ -49,30 +46,36 @@ The GitHub Model resource can be configured with the following options:

### API Key

The API key can be configured using a parameter:

```csharp
var apiKey = builder.AddParameter("github-api-key", secret: true);
var chat = builder.AddGitHubModel("chat", "openai/gpt-4o-mini")
.WithApiKey(apiKey);
```
The API key can be set as a configuration value using the default name `{resource_name}-gh-apikey` or the `GITHUB_TOKEN` environment variable.

Then in user secrets:

```json
{
"Parameters":
{
"github-api-key": "YOUR_GITHUB_TOKEN_HERE"
"chat-gh-apikey": "YOUR_GITHUB_TOKEN_HERE"
}
}
```

Or directly as a string (not recommended for production):
Furthermore, the API key can be configured using a custom parameter:

```csharp
var apiKey = builder.AddParameter("my-api-key", secret: true);
var chat = builder.AddGitHubModel("chat", "openai/gpt-4o-mini")
.WithApiKey("your-api-key-here");
.WithApiKey(apiKey);
```

Then in user secrets:

```json
{
"Parameters":
{
"my-api-key": "YOUR_GITHUB_TOKEN_HERE"
}
}
```

## Available Models
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,32 @@ public class GitHubModelsExtensionTests
public void AddGitHubModelAddsResourceWithCorrectName()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Assert.Equal("github", github.Resource.Name);
Assert.Equal("openai/gpt-4o-mini", github.Resource.Model);
}

[Fact]
public void AddGitHubModelCreatesDefaultApiKeyParameter()
{
using var builder = TestDistributedApplicationBuilder.Create();

var github = builder.AddGitHubModel("mymodel", "openai/gpt-4o-mini");

// Verify that the API key parameter exists and follows the naming pattern
Assert.NotNull(github.Resource.Key);
Assert.Equal("mymodel-gh-apikey", github.Resource.Key.Name);
Assert.True(github.Resource.Key.Secret);
}

[Fact]
public void AddGitHubModelUsesCorrectEndpoint()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Expand All @@ -34,6 +49,7 @@ public void AddGitHubModelUsesCorrectEndpoint()
public void ConnectionStringExpressionIsCorrectlyFormatted()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Expand Down Expand Up @@ -72,14 +88,15 @@ public void DefaultKeyParameterIsCreated()
var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Assert.NotNull(github.Resource.Key);
Assert.Equal("github-api-key", github.Resource.Key.Name);
Assert.Equal("github-gh-apikey", github.Resource.Key.Name);
Assert.True(github.Resource.Key.Secret);
}

[Fact]
public void AddGitHubModelWithoutOrganization()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Expand All @@ -93,6 +110,7 @@ public void AddGitHubModelWithOrganization()

var orgParameter = builder.AddParameter("github-org");
builder.Configuration["Parameters:github-org"] = "myorg";
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini", orgParameter);

Expand All @@ -108,6 +126,7 @@ public void ConnectionStringExpressionWithOrganization()

var orgParameter = builder.AddParameter("github-org");
builder.Configuration["Parameters:github-org"] = "myorg";
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini", orgParameter);

Expand All @@ -127,6 +146,7 @@ public async Task ConnectionStringExpressionWithOrganizationResolvesCorrectly()

var orgParameter = builder.AddParameter("github-org");
builder.Configuration["Parameters:github-org"] = "myorg";
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini", orgParameter);

Expand All @@ -141,6 +161,7 @@ public async Task ConnectionStringExpressionWithOrganizationResolvesCorrectly()
public void ConnectionStringExpressionWithoutOrganization()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");

Expand All @@ -160,21 +181,31 @@ public void GitHubModelResourceConstructorSetsOrganization()
var orgParameter = builder.AddParameter("github-org");
builder.Configuration["Parameters:github-org"] = "myorg";

var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", orgParameter.Resource);
var apiKeyParameter = builder.AddParameter("github-api-key", secret: true);
builder.Configuration["Parameters:github-api-key"] = "test-key";

var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", orgParameter.Resource, apiKeyParameter.Resource);

Assert.Equal("test", resource.Name);
Assert.Equal("openai/gpt-4o-mini", resource.Model);
Assert.Equal(orgParameter.Resource, resource.Organization);
Assert.Equal(apiKeyParameter.Resource, resource.Key);
}

[Fact]
public void GitHubModelResourceConstructorWithNullOrganization()
{
var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", null);
using var builder = TestDistributedApplicationBuilder.Create();

var apiKeyParameter = builder.AddParameter("github-api-key", secret: true);
builder.Configuration["Parameters:github-api-key"] = "test-key";

var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", null, apiKeyParameter.Resource);

Assert.Equal("test", resource.Name);
Assert.Equal("openai/gpt-4o-mini", resource.Model);
Assert.Null(resource.Organization);
Assert.Equal(apiKeyParameter.Resource, resource.Key);
}

[Fact]
Expand All @@ -185,17 +216,49 @@ public void GitHubModelResourceOrganizationCanBeChanged()
var orgParameter = builder.AddParameter("github-org");
builder.Configuration["Parameters:github-org"] = "myorg";

var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", null);
var apiKeyParameter = builder.AddParameter("github-api-key", secret: true);
builder.Configuration["Parameters:github-api-key"] = "test-key";

var resource = new GitHubModelResource("test", "openai/gpt-4o-mini", null, apiKeyParameter.Resource);
Assert.Null(resource.Organization);

resource.Organization = orgParameter.Resource;
Assert.Equal(orgParameter.Resource, resource.Organization);
}

[Fact]
public void WithApiKeyThrowsIfParameterIsNotSecret()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");
var apiKey = builder.AddParameter("non-secret-key"); // Not marked as secret

var exception = Assert.Throws<ArgumentException>(() => github.WithApiKey(apiKey));
Assert.Contains("The API key parameter must be marked as secret", exception.Message);
}

[Fact]
public void WithApiKeySucceedsIfParameterIsSecret()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini");
var apiKey = builder.AddParameter("secret-key", secret: true);

// This should not throw
var result = github.WithApiKey(apiKey);
Assert.NotNull(result);
Assert.Equal(apiKey.Resource, github.Resource.Key);
}

[Fact]
public void WithHealthCheckAddsHealthCheckAnnotation()
{
using var builder = TestDistributedApplicationBuilder.Create();
builder.Configuration["Parameters:github-gh-apikey"] = "test-api-key";

var github = builder.AddGitHubModel("github", "openai/gpt-4o-mini").WithHealthCheck();

Expand Down
Loading