Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
Prev Previous commit
addressing feedback
  • Loading branch information
andyjmorgan committed Sep 26, 2025
commit 3b5a9158b6d282aa78a0e3a4e09ce6bbbf852c07
4 changes: 2 additions & 2 deletions Anthropic.SDK.Tests/CacheControlTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -495,7 +495,7 @@ public async Task TestCacheControlOfAssistantMessages()
new SystemMessage(content, new CacheControl()
{
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration1Hour,
TTL = CacheDuration.OneHour,
})
};
var parameters = new MessageParameters()
Expand All @@ -517,7 +517,7 @@ public async Task TestCacheControlOfAssistantMessages()
res.Message.Content.First().CacheControl = new CacheControl()
{
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration1Hour,
TTL = CacheDuration.OneHour,
};

messages.Add(res.Message);
Expand Down
2 changes: 1 addition & 1 deletion Anthropic.SDK.Tests/DocumentTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ public async Task TestPDF()
CacheControl = new CacheControl()
{
Type = CacheControlType.ephemeral,
TTL = CacheControl.CacheDuration5Minutes,
TTL = CacheDuration.FiveMinutes,
}
}),
new Message(RoleType.User, "Which model has the highest human preference win rates across each use-case?"),
Expand Down
6 changes: 2 additions & 4 deletions Anthropic.SDK/Messaging/CacheControl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,6 @@ namespace Anthropic.SDK.Messaging;

public class CacheControl
{
public static string CacheDuration5Minutes = "5m";
public static string CacheDuration1Hour = "1h";

[JsonPropertyName("type")]
public CacheControlType Type { get; set; }

Expand All @@ -15,7 +12,8 @@ public class CacheControl
/// Supported values are <see cref="CacheDuration5Minutes"/> or <see cref="CacheDuration1Hour"/>
/// </summary>
[JsonPropertyName("ttl")]
public string TTL { get; set; }
[JsonConverter(typeof(CacheDurationConverter))]
public CacheDuration TTL { get; set; }
}

[JsonConverter(typeof(JsonStringEnumConverter))]
Expand Down
36 changes: 36 additions & 0 deletions Anthropic.SDK/Messaging/CacheDurationConverter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
using System;
using System.Text.Json;
using System.Text.Json.Serialization;

namespace Anthropic.SDK.Messaging;

public enum CacheDuration
{
FiveMinutes,
OneHour
}

public class CacheDurationConverter : JsonConverter<CacheDuration>
{
public override CacheDuration Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options)
{
string? value = reader.GetString();
return value switch
{
"5m" => CacheDuration.FiveMinutes,
"1h" => CacheDuration.OneHour,
_ => throw new JsonException($"Invalid cache duration: {value}")
};
}

public override void Write(Utf8JsonWriter writer, CacheDuration value, JsonSerializerOptions options)
{
string str = value switch
{
CacheDuration.FiveMinutes => "5m",
CacheDuration.OneHour => "1h",
_ => throw new JsonException($"Invalid cache duration enum: {value}")
};
writer.WriteStringValue(str);
}
}
Loading