diff --git a/playground/quickstart/OpenCode.Playground.QuickStart/FreeModel.cs b/playground/quickstart/OpenCode.Playground.QuickStart/FreeModel.cs index 81d2065f..a4fe2d57 100644 --- a/playground/quickstart/OpenCode.Playground.QuickStart/FreeModel.cs +++ b/playground/quickstart/OpenCode.Playground.QuickStart/FreeModel.cs @@ -34,13 +34,19 @@ internal sealed class FreeModel(OpenCodeClient client) } } + /// Gets whether a model is free: one of opencode's own, with a price list whose every tier charges nothing for input and output. + /// The model. + /// True when the model is free. + 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 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) { diff --git a/playground/quickstart/OpenCode.Playground.QuickStart/OpenCode.Playground.QuickStart.csproj b/playground/quickstart/OpenCode.Playground.QuickStart/OpenCode.Playground.QuickStart.csproj index e7467484..7939b995 100644 --- a/playground/quickstart/OpenCode.Playground.QuickStart/OpenCode.Playground.QuickStart.csproj +++ b/playground/quickstart/OpenCode.Playground.QuickStart/OpenCode.Playground.QuickStart.csproj @@ -8,6 +8,7 @@ + diff --git a/tests/OpenCode.Playground.Hq.Tests/OpenCode.Playground.Hq.Tests.csproj b/tests/OpenCode.Playground.Hq.Tests/OpenCode.Playground.Hq.Tests.csproj index 52177efe..a8bed3a0 100644 --- a/tests/OpenCode.Playground.Hq.Tests/OpenCode.Playground.Hq.Tests.csproj +++ b/tests/OpenCode.Playground.Hq.Tests/OpenCode.Playground.Hq.Tests.csproj @@ -5,6 +5,8 @@ + + diff --git a/tests/OpenCode.Playground.Hq.Tests/QuickStart/QuickStartFreeModelTests.cs b/tests/OpenCode.Playground.Hq.Tests/QuickStart/QuickStartFreeModelTests.cs new file mode 100644 index 00000000..6c1b637c --- /dev/null +++ b/tests/OpenCode.Playground.Hq.Tests/QuickStart/QuickStartFreeModelTests.cs @@ -0,0 +1,60 @@ +using OpenCode.Playground.QuickStart; +using OpenCode.Sdk.Models; + +namespace OpenCode.Playground.Hq.Tests.QuickStart; + +/// +/// 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. +/// +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 }, + }; +}