GeezerVison is showing github.com — fix: improve xai cache hit rate (#35970) · moonug/opencode@ccb6b7c
File treeExpand file treeCollapse file tree Open diff view settings Expand file treeCollapse file tree Open diff view settings Collapse file Copy file name to clipboardExpand all lines: bun.lock +15-5Lines changed: 15 additions & 5 deletions Collapse file Copy file name to clipboardExpand all lines: package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 147 | 147 | | 148 | 148 | | 149 | 149 | | 150 | | - | | 150 | + | 151 | 151 | | 152 | 152 | | 153 | 153 | | |
Collapse file Copy file name to clipboardExpand all lines: packages/core/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 80 | 80 | "@ai-sdk/provider-utils": "4.0.23", | 81 | 81 | "@ai-sdk/togetherai": "2.0.41", | 82 | 82 | "@ai-sdk/vercel": "2.0.39", | 83 | | - "@ai-sdk/xai": "3.0.82", | | 83 | + "@ai-sdk/xai": "3.0.102", | 84 | 84 | "@aws-sdk/credential-providers": "3.1057.0", | 85 | 85 | "@effect/opentelemetry": "catalog:", | 86 | 86 | "@effect/platform-node": "catalog:", | |
Collapse file Copy file name to clipboard +32Lines changed: 32 additions & 0 deletions | Original file line number | Diff line number | Diff line change |
|---|
| | 1 | +import { createXai } from "@ai-sdk/xai" | | 2 | +import { expect, test } from "bun:test" | | 3 | + | | 4 | +test("xAI Responses sends promptCacheKey as prompt_cache_key", async () => { | | 5 | + let body: Record<string, unknown> | undefined | | 6 | + const mockFetch = Object.assign( | | 7 | + async (_input: Parameters<typeof fetch>[0], init?: RequestInit) => { | | 8 | + body = JSON.parse(String(init?.body)) | | 9 | + return Response.json({ | | 10 | + id: "response-1", | | 11 | + created_at: 0, | | 12 | + model: "grok-4", | | 13 | + object: "response", | | 14 | + output: [], | | 15 | + usage: { input_tokens: 1, output_tokens: 0 }, | | 16 | + status: "completed", | | 17 | + }) | | 18 | + }, | | 19 | + { preconnect: fetch.preconnect }, | | 20 | + ) | | 21 | + const model = createXai({ | | 22 | + apiKey: "test", | | 23 | + fetch: mockFetch, | | 24 | + }).responses("grok-4") | | 25 | + | | 26 | + await model.doGenerate({ | | 27 | + prompt: [{ role: "user", content: [{ type: "text", text: "Hello" }] }], | | 28 | + providerOptions: { xai: { promptCacheKey: "session-123" } }, | | 29 | + }) | | 30 | + | | 31 | + expect(body?.prompt_cache_key).toBe("session-123") | | 32 | +}) |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/package.json +1-1Lines changed: 1 addition & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
| 73 | 73 | "@ai-sdk/provider": "3.0.8", | 74 | 74 | "@ai-sdk/togetherai": "2.0.41", | 75 | 75 | "@ai-sdk/vercel": "2.0.39", | 76 | | - "@ai-sdk/xai": "3.0.82", | | 76 | + "@ai-sdk/xai": "3.0.102", | 77 | 77 | "@aws-sdk/credential-providers": "3.1057.0", | 78 | 78 | "@clack/prompts": "1.0.0-alpha.1", | 79 | 79 | "@effect/opentelemetry": "catalog:", | |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/src/provider/transform.ts +6-1Lines changed: 6 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -1127,7 +1127,12 @@ export function options(input: { | 1127 | 1127 | } | 1128 | 1128 | } | 1129 | 1129 |
| 1130 | | - if (input.model.providerID === "openai" || input.providerOptions?.setCacheKey) { | | 1130 | + if ( | | 1131 | + input.providerOptions?.setCacheKey !== false && | | 1132 | + (input.model.providerID === "openai" || | | 1133 | + input.model.api.npm === "@ai-sdk/xai" || | | 1134 | + input.providerOptions?.setCacheKey) | | 1135 | + ) { | 1131 | 1136 | result["promptCacheKey"] = input.sessionID | 1132 | 1137 | } | 1133 | 1138 |
| |
Collapse file Copy file name to clipboardExpand all lines: packages/opencode/test/provider/transform.test.ts +51-1Lines changed: 51 additions & 1 deletion | Original file line number | Diff line number | Diff line change |
|---|
@@ -73,7 +73,7 @@ describe("ProviderTransform.options - setCacheKey", () => { | 73 | 73 | expect(result.promptCacheKey).toBeUndefined() | 74 | 74 | }) | 75 | 75 |
| 76 | | - test("should set promptCacheKey for openai provider regardless of setCacheKey", () => { | | 76 | + test("should set promptCacheKey for openai provider by default", () => { | 77 | 77 | const openaiModel = { | 78 | 78 | ...mockModel, | 79 | 79 | providerID: "openai", | @@ -87,6 +87,56 @@ describe("ProviderTransform.options - setCacheKey", () => { | 87 | 87 | expect(result.promptCacheKey).toBe(sessionID) | 88 | 88 | }) | 89 | 89 |
| | 90 | + test("should not set promptCacheKey for openai when explicitly disabled", () => { | | 91 | + const openaiModel = { | | 92 | + ...mockModel, | | 93 | + providerID: "openai", | | 94 | + api: { | | 95 | + id: "gpt-4", | | 96 | + url: "https://api.openai.com", | | 97 | + npm: "@ai-sdk/openai", | | 98 | + }, | | 99 | + } | | 100 | + const result = ProviderTransform.options({ | | 101 | + model: openaiModel, | | 102 | + sessionID, | | 103 | + providerOptions: { setCacheKey: false }, | | 104 | + }) | | 105 | + expect(result.promptCacheKey).toBeUndefined() | | 106 | + }) | | 107 | + | | 108 | + test("should set promptCacheKey for the xAI SDK by default regardless of provider ID", () => { | | 109 | + const xaiModel = { | | 110 | + ...mockModel, | | 111 | + providerID: "custom-xai", | | 112 | + api: { | | 113 | + id: "grok-4", | | 114 | + url: "https://api.x.ai", | | 115 | + npm: "@ai-sdk/xai", | | 116 | + }, | | 117 | + } | | 118 | + const result = ProviderTransform.options({ model: xaiModel, sessionID, providerOptions: {} }) | | 119 | + expect(result.promptCacheKey).toBe(sessionID) | | 120 | + }) | | 121 | + | | 122 | + test("should not set promptCacheKey for the xAI SDK when explicitly disabled", () => { | | 123 | + const xaiModel = { | | 124 | + ...mockModel, | | 125 | + providerID: "xai", | | 126 | + api: { | | 127 | + id: "grok-4", | | 128 | + url: "https://api.x.ai", | | 129 | + npm: "@ai-sdk/xai", | | 130 | + }, | | 131 | + } | | 132 | + const result = ProviderTransform.options({ | | 133 | + model: xaiModel, | | 134 | + sessionID, | | 135 | + providerOptions: { setCacheKey: false }, | | 136 | + }) | | 137 | + expect(result.promptCacheKey).toBeUndefined() | | 138 | + }) | | 139 | + | 90 | 140 | test("should set store=false for openai provider", () => { | 91 | 141 | const openaiModel = { | 92 | 142 | ...mockModel, | |
|