Skip to content

Commit 024979f

Browse files
authored
feat(bedrock): Add token caching for any amazon-bedrock provider (anomalyco#18959)
1 parent bc608fb commit 024979f

2 files changed

Lines changed: 41 additions & 1 deletion

File tree

packages/opencode/src/provider/transform.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,10 @@ export namespace ProviderTransform {
194194
}
195195

196196
for (const msg of unique([...system, ...final])) {
197-
const useMessageLevelOptions = model.providerID === "anthropic" || model.providerID.includes("bedrock")
197+
const useMessageLevelOptions =
198+
model.providerID === "anthropic" ||
199+
model.providerID.includes("bedrock") ||
200+
model.api.npm === "@ai-sdk/amazon-bedrock"
198201
const shouldUseContentOptions = !useMessageLevelOptions && Array.isArray(msg.content) && msg.content.length > 0
199202

200203
if (shouldUseContentOptions) {

packages/opencode/test/provider/transform.test.ts

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1629,6 +1629,43 @@ describe("ProviderTransform.message - claude w/bedrock custom inference profile"
16291629
})
16301630
})
16311631

1632+
describe("ProviderTransform.message - bedrock caching with non-bedrock providerID", () => {
1633+
test("applies cache options at message level when npm package is amazon-bedrock", () => {
1634+
const model = {
1635+
id: "aws/us.anthropic.claude-opus-4-6-v1",
1636+
providerID: "aws",
1637+
api: {
1638+
id: "us.anthropic.claude-opus-4-6-v1",
1639+
url: "https://bedrock-runtime.us-east-1.amazonaws.com",
1640+
npm: "@ai-sdk/amazon-bedrock",
1641+
},
1642+
name: "Claude Opus 4.6",
1643+
capabilities: {},
1644+
options: {},
1645+
headers: {},
1646+
} as any
1647+
1648+
const msgs = [
1649+
{
1650+
role: "system",
1651+
content: [{ type: "text", text: "You are a helpful assistant" }],
1652+
},
1653+
{
1654+
role: "user",
1655+
content: [{ type: "text", text: "Hello" }],
1656+
},
1657+
] as any[]
1658+
1659+
const result = ProviderTransform.message(msgs, model, {}) as any[]
1660+
1661+
// Cache should be at the message level and not the content-part level
1662+
expect(result[0].providerOptions?.bedrock).toEqual({
1663+
cachePoint: { type: "default" },
1664+
})
1665+
expect(result[0].content[0].providerOptions?.bedrock).toBeUndefined()
1666+
})
1667+
})
1668+
16321669
describe("ProviderTransform.message - cache control on gateway", () => {
16331670
const createModel = (overrides: Partial<any> = {}) =>
16341671
({

0 commit comments

Comments
 (0)