From ec665380a194d47d301cd55c22bd63267685033b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Deniz=20=C4=B0rgin?= Date: Mon, 23 Nov 2026 18:46:39 +0300 Subject: [PATCH] fix(quickstart): a free model charges nothing for input and output on every tier QuickStart accepted a model of opencode's own provider when only its input price was zero, and accepted one with no listed price at all. It now uses the root README's filter: opencode's provider, a price list that is not empty, and zero input and output prices on every tier. The retry loop is unchanged. QuickStart has no test project, so the filter is tested from the playground test project, which now references QuickStart. --- .../FreeModel.cs | 8 ++- .../OpenCode.Playground.QuickStart.csproj | 1 + .../OpenCode.Playground.Hq.Tests.csproj | 2 + .../QuickStart/QuickStartFreeModelTests.cs | 60 +++++++++++++++++++ 4 files changed, 70 insertions(+), 1 deletion(-) create mode 100644 tests/OpenCode.Playground.Hq.Tests/QuickStart/QuickStartFreeModelTests.cs 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 }, + }; +}