diff --git a/prompt_cache.go b/prompt_cache.go index 23b998e..a76e63c 100644 --- a/prompt_cache.go +++ b/prompt_cache.go @@ -84,21 +84,35 @@ func markBlock(block any) (any, bool) { return cp, true } -// markLastBlock returns a copy of blocks with the last entry marked. +// markLastBlock returns a copy of blocks with the last non-empty entry marked; +// the API rejects a cache marker on an empty text block. func markLastBlock(blocks []any) ([]any, bool) { - if len(blocks) == 0 { + idx := len(blocks) - 1 + for idx >= 0 && isEmptyTextBlock(blocks[idx]) { + idx-- + } + if idx < 0 { return blocks, false } - marked, ok := markBlock(blocks[len(blocks)-1]) + marked, ok := markBlock(blocks[idx]) if !ok { return blocks, false } cp := make([]any, len(blocks)) copy(cp, blocks) - cp[len(cp)-1] = marked + cp[idx] = marked return cp, true } +func isEmptyTextBlock(block any) bool { + m, ok := block.(map[string]any) + if !ok { + return false + } + text, isStr := m["text"].(string) + return m["type"] == "text" && isStr && text == "" +} + // markContent returns content (string or block list) with its last block marked. func markContent(content any) (any, bool) { switch c := content.(type) { diff --git a/prompt_cache_test.go b/prompt_cache_test.go index 83ba933..1b041f8 100644 --- a/prompt_cache_test.go +++ b/prompt_cache_test.go @@ -220,3 +220,23 @@ func TestMessagesPassthroughInjects(t *testing.T) { t.Fatalf("enabled body = %s", on) } } + +func TestInjectCacheControlSkipsEmptyTextBlock(t *testing.T) { + b := map[string]any{ + "model": "m", + "messages": []any{ + map[string]any{"role": "user", "content": []any{ + map[string]any{"type": "text", "text": "real"}, + map[string]any{"type": "text", "text": ""}, + }}, + }, + } + out, _ := injectCacheControl(b) + blocks := out["messages"].([]any)[0].(map[string]any)["content"].([]any) + if _, ok := blocks[0].(map[string]any)["cache_control"]; !ok { + t.Fatalf("non-empty block not marked: %#v", blocks) + } + if _, ok := blocks[1].(map[string]any)["cache_control"]; ok { + t.Fatal("empty block marked") + } +} diff --git a/translate_anthropic.go b/translate_anthropic.go index 24849c8..8138491 100644 --- a/translate_anthropic.go +++ b/translate_anthropic.go @@ -459,10 +459,10 @@ func anthropicRequestFromOpenAI(req map[string]any) (map[string]any, error) { for _, part := range c { pm, _ := part.(map[string]any) block, marked := systemBlockFromPart(pm) - if marked { - systemMarked = true - } if block["text"] != "" { + if marked { + systemMarked = true + } systemBlocks = append(systemBlocks, block) } } diff --git a/translate_anthropic_cache_test.go b/translate_anthropic_cache_test.go index 6ffdc77..bea14dc 100644 --- a/translate_anthropic_cache_test.go +++ b/translate_anthropic_cache_test.go @@ -50,3 +50,39 @@ func TestTranslateSystemNoMarkersUnchanged(t *testing.T) { t.Fatalf("system = %#v", out["system"]) } } + +func systemReq(parts ...any) map[string]any { + return map[string]any{ + "model": "claude-x", + "messages": []any{ + map[string]any{"role": "system", "content": parts}, + map[string]any{"role": "user", "content": "hi"}, + }, + } +} + +func TestTranslateSystemEmptyMarkedPartKeepsString(t *testing.T) { + cc := map[string]any{"type": "ephemeral"} + out, err := anthropicRequestFromOpenAI(systemReq( + map[string]any{"type": "text", "text": "", "cache_control": cc})) + if err != nil { + t.Fatal(err) + } + if out["system"] != "" { + t.Fatalf("system = %#v, want empty string", out["system"]) + } +} + +func TestTranslateSystemEmptyMarkedPartWithOthers(t *testing.T) { + cc := map[string]any{"type": "ephemeral"} + out, err := anthropicRequestFromOpenAI(systemReq( + map[string]any{"type": "text", "text": "A"}, + map[string]any{"type": "text", "text": "", "cache_control": cc}, + map[string]any{"type": "text", "text": "B"})) + if err != nil { + t.Fatal(err) + } + if out["system"] != "A\n\nB" { + t.Fatalf("system = %#v", out["system"]) + } +}