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 @@ -34,13 +34,19 @@ internal sealed class FreeModel(OpenCodeClient client)
}
}

/// <summary>Gets whether a model is free: one of opencode's own, with a price list whose every tier charges nothing for input and output.</summary>
/// <param name="model">The model.</param>
/// <returns>True when the model is free.</returns>
internal static bool IsFree(ModelInfo model) =>
model.ProviderId == "opencode" && model.Cost.Count > 0 && model.Cost.All(static cost => cost.Input <= 0 && cost.Output <= 0);

private async Task<ModelInfo?> ReadAsync(CancellationToken cancellationToken)
{
await client.LanguageModels.WaitForDefaultAsync(cancellationToken: cancellationToken);
try
{
var catalog = await client.LanguageModels.ListModelsAsync(cancellationToken: cancellationToken);
return catalog.Models.FirstOrDefault(static model => model.ProviderId == "opencode" && model.Cost.All(static cost => cost.Input <= 0));
return catalog.Models.FirstOrDefault(IsFree);
}
catch (OpenCodeApiException failure) when (failure.Error is ServiceUnavailableError)
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@

<ItemGroup>
<ProjectReference Include="../../../src/OpenCode.Sdk/OpenCode.Sdk.csproj" />
<InternalsVisibleTo Include="OpenCode.Playground.Hq.Tests" />
</ItemGroup>

</Project>
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
</PropertyGroup>
<ItemGroup>
<ProjectReference Include="../../playground/hq/OpenCode.Playground.Hq/OpenCode.Playground.Hq.csproj" />
<!-- QuickStart has no test project of its own; its free-model filter is tested here. -->
<ProjectReference Include="../../playground/quickstart/OpenCode.Playground.QuickStart/OpenCode.Playground.QuickStart.csproj" />
</ItemGroup>
<ItemGroup>
<PackageReference Include="TUnit" />
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using OpenCode.Playground.QuickStart;
using OpenCode.Sdk.Models;

namespace OpenCode.Playground.Hq.Tests.QuickStart;

/// <summary>
/// QuickStart's free-model filter, the one the root README's streaming example uses: a model of
/// opencode's own provider whose price list is not empty and charges nothing for input and output
/// on every tier.
/// </summary>
public sealed class QuickStartFreeModelTests
{
[Test]
public async Task IsFree_Should_Accept_An_Opencode_Model_That_Charges_Nothing()
{
await Assert.That(FreeModel.IsFree(Model("opencode", Cost(0, 0)))).IsTrue();
}

[Test]
public async Task IsFree_Should_Reject_A_Model_With_No_Listed_Price()
{
await Assert.That(FreeModel.IsFree(Model("opencode"))).IsFalse();
}

[Test]
public async Task IsFree_Should_Reject_A_Model_That_Charges_For_Output()
{
await Assert.That(FreeModel.IsFree(Model("opencode", Cost(0, 15)))).IsFalse();
}

[Test]
public async Task IsFree_Should_Reject_A_Model_With_A_Priced_Tier()
{
await Assert.That(FreeModel.IsFree(Model("opencode", Cost(0, 0), Cost(3, 0)))).IsFalse();
}

[Test]
public async Task IsFree_Should_Reject_Another_Provider()
{
await Assert.That(FreeModel.IsFree(Model("anthropic", Cost(0, 0)))).IsFalse();
}

private static ModelCost Cost(double input, double output) =>
new() { Input = input, Output = output, Cache = new ModelCostCache { Read = 0, Write = 0 } };

private static ModelInfo Model(string providerId, params ModelCost[] cost) => new()
{
Id = "model",
ModelId = "model",
ProviderId = providerId,
Name = "Model",
Capabilities = new ModelCapabilities { Tools = true, Input = ["text"], Output = ["text"] },
Variants = [],
Time = new ModelInfoTime { Released = 0 },
Cost = cost,
Status = ModelInfoStatus.Active,
Enabled = true,
Limit = new ModelInfoLimit { Context = 1, Output = 1 },
};
}
Loading