|
15 | 15 | // ***************************************************************************** |
16 | 16 |
|
17 | 17 | import { expect } from 'chai'; |
18 | | -import { AnthropicModel, DEFAULT_MAX_TOKENS } from './anthropic-language-model'; |
| 18 | +import { AnthropicModel, DEFAULT_MAX_TOKENS, addCacheControlToLastMessage } from './anthropic-language-model'; |
19 | 19 |
|
20 | 20 | describe('AnthropicModel', () => { |
21 | 21 |
|
@@ -75,4 +75,93 @@ describe('AnthropicModel', () => { |
75 | 75 | expect(model.maxRetries).to.equal(5); |
76 | 76 | }); |
77 | 77 | }); |
| 78 | + |
| 79 | + describe('addCacheControlToLastMessage', () => { |
| 80 | + it('should preserve all content blocks when adding cache control to parallel tool calls', () => { |
| 81 | + const messages = [ |
| 82 | + { |
| 83 | + role: 'user' as const, |
| 84 | + content: [ |
| 85 | + { type: 'tool_result' as const, tool_use_id: 'tool1', content: 'result1' }, |
| 86 | + { type: 'tool_result' as const, tool_use_id: 'tool2', content: 'result2' }, |
| 87 | + { type: 'tool_result' as const, tool_use_id: 'tool3', content: 'result3' } |
| 88 | + ] |
| 89 | + } |
| 90 | + ]; |
| 91 | + |
| 92 | + const result = addCacheControlToLastMessage(messages); |
| 93 | + |
| 94 | + expect(result).to.have.lengthOf(1); |
| 95 | + expect(result[0].content).to.be.an('array').with.lengthOf(3); |
| 96 | + expect(result[0].content[0]).to.deep.equal({ type: 'tool_result', tool_use_id: 'tool1', content: 'result1' }); |
| 97 | + expect(result[0].content[1]).to.deep.equal({ type: 'tool_result', tool_use_id: 'tool2', content: 'result2' }); |
| 98 | + expect(result[0].content[2]).to.deep.equal({ |
| 99 | + type: 'tool_result', |
| 100 | + tool_use_id: 'tool3', |
| 101 | + content: 'result3', |
| 102 | + cache_control: { type: 'ephemeral' } |
| 103 | + }); |
| 104 | + }); |
| 105 | + |
| 106 | + it('should add cache control to last non-thinking block in mixed content', () => { |
| 107 | + const messages = [ |
| 108 | + { |
| 109 | + role: 'assistant' as const, |
| 110 | + content: [ |
| 111 | + { type: 'text' as const, text: 'Some text' }, |
| 112 | + { type: 'tool_use' as const, id: 'tool1', name: 'getTool', input: {} }, |
| 113 | + { type: 'thinking' as const, thinking: 'thinking content', signature: 'signature' } |
| 114 | + ] |
| 115 | + } |
| 116 | + ]; |
| 117 | + |
| 118 | + const result = addCacheControlToLastMessage(messages); |
| 119 | + |
| 120 | + expect(result).to.have.lengthOf(1); |
| 121 | + expect(result[0].content).to.be.an('array').with.lengthOf(3); |
| 122 | + expect(result[0].content[0]).to.deep.equal({ type: 'text', text: 'Some text' }); |
| 123 | + expect(result[0].content[1]).to.deep.equal({ |
| 124 | + type: 'tool_use', |
| 125 | + id: 'tool1', |
| 126 | + name: 'getTool', |
| 127 | + input: {}, |
| 128 | + cache_control: { type: 'ephemeral' } |
| 129 | + }); |
| 130 | + expect(result[0].content[2]).to.deep.equal({ type: 'thinking', thinking: 'thinking content', signature: 'signature' }); |
| 131 | + }); |
| 132 | + |
| 133 | + it('should handle string content by converting to content block', () => { |
| 134 | + const messages = [ |
| 135 | + { |
| 136 | + role: 'user' as const, |
| 137 | + content: 'Simple text message' |
| 138 | + } |
| 139 | + ]; |
| 140 | + |
| 141 | + const result = addCacheControlToLastMessage(messages); |
| 142 | + |
| 143 | + expect(result).to.have.lengthOf(1); |
| 144 | + expect(result[0].content).to.be.an('array').with.lengthOf(1); |
| 145 | + expect(result[0].content[0]).to.deep.equal({ |
| 146 | + type: 'text', |
| 147 | + text: 'Simple text message', |
| 148 | + cache_control: { type: 'ephemeral' } |
| 149 | + }); |
| 150 | + }); |
| 151 | + |
| 152 | + it('should not modify original messages', () => { |
| 153 | + const originalMessages = [ |
| 154 | + { |
| 155 | + role: 'user' as const, |
| 156 | + content: [ |
| 157 | + { type: 'tool_result' as const, tool_use_id: 'tool1', content: 'result1' } |
| 158 | + ] |
| 159 | + } |
| 160 | + ]; |
| 161 | + |
| 162 | + addCacheControlToLastMessage(originalMessages); |
| 163 | + |
| 164 | + expect(originalMessages[0].content[0]).to.not.have.property('cache_control'); |
| 165 | + }); |
| 166 | + }); |
78 | 167 | }); |
0 commit comments