OpenCode 1 (opencode-ai 1.19.5) with its provider's setCacheKey writes the session's key into a Chat body as camelCase promptCacheKey (a real run on mote against a recording server: "promptCacheKey":"ses_…"), a field no Chat API reads; a passthrough upstream got it as written and no cache key, a translated route never read it. The next commit has magpie set setCacheKey for OpenCode, so this comes first. Now a Chat request's promptCacheKey is taken out and sent as prompt_cache_key, for every route (handle, beside thinkingEffort), so req.CacheKey and the relay both see it. A prompt_cache_key the body has already is the one kept. The body is re-encoded without HTML escaping. Run on mote: go test -run 'OpenCode|CacheKey' ./internal/gateway ok; go vet ./internal/gateway on darwin, GOOS=linux and GOOS=windows; go build ./... (darwin, windows). Broken by hand: without the cacheKeyCamel call, TestOpenCodeCamelCacheKey fails (upstream body still "promptCacheKey":"ses_1a2b", no prompt_cache_key). Not tried: OpenCode 1 through a running magpie gateway end to end (the recording server saw OpenCode's body; the test covers magpie's half). Reported-by: mariamatrix Reported-by: mahiro233
49 lines
1.9 KiB
Go
49 lines
1.9 KiB
Go
package gateway
|
|
|
|
import (
|
|
"net/http/httptest"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/tidwall/gjson"
|
|
"github.com/yetone/magpie/internal/provider"
|
|
)
|
|
|
|
// OpenCode 1 (opencode-ai 1.19.5) with its provider's setCacheKey writes
|
|
// the session's key into a Chat body as camelCase promptCacheKey, as a
|
|
// real run recorded it (#5468): the upstream is sent prompt_cache_key,
|
|
// the field every Chat API reads, and no promptCacheKey it doesn't know.
|
|
// A prompt_cache_key the body has already is the one sent.
|
|
func TestOpenCodeCamelCacheKey(t *testing.T) {
|
|
setHome(t, t.TempDir())
|
|
f := &fake{t: t, reply: sse(
|
|
`data: {"id":"c1","choices":[{"delta":{"content":"OK"},"finish_reason":"stop"}],"usage":{"prompt_tokens":5,"completion_tokens":2}}`,
|
|
`data: [DONE]`)}
|
|
setup(t, provider.Chat, f)
|
|
for _, c := range []struct{ keys, want string }{
|
|
{`"promptCacheKey":"ses_1a2b"`, "ses_1a2b"},
|
|
{`"promptCacheKey":"ses_1a2b","prompt_cache_key":"own"`, "own"},
|
|
{`"prompt_cache_key":"own"`, "own"},
|
|
} {
|
|
f.got = nil
|
|
body := `{"model":"fake/m1","stream":true,` + c.keys + `,"messages":[{"role":"user","content":"a <b> & c"}]}`
|
|
rec := httptest.NewRecorder()
|
|
req := httptest.NewRequest("POST", "/v1/chat/completions", strings.NewReader(body))
|
|
req.Header.Set("Authorization", "Bearer "+Token)
|
|
req.Header.Set("User-Agent", "opencode/1.19.5 ai-sdk/provider-utils/3.0.20 runtime/bun/1.3.5")
|
|
New().Handler().ServeHTTP(rec, req)
|
|
if rec.Code != 200 {
|
|
t.Fatalf("%s: %d %s", c.keys, rec.Code, rec.Body.String())
|
|
}
|
|
if got := gjson.GetBytes(f.got, "prompt_cache_key").String(); got != c.want {
|
|
t.Fatalf("%s: upstream prompt_cache_key %q, want %q: %s", c.keys, got, c.want, f.got)
|
|
}
|
|
if gjson.GetBytes(f.got, "promptCacheKey").Exists() {
|
|
t.Fatalf("%s: promptCacheKey went upstream: %s", c.keys, f.got)
|
|
}
|
|
if got := gjson.GetBytes(f.got, "messages.0.content").String(); got != "a <b> & c" {
|
|
t.Fatalf("%s: message %q", c.keys, got)
|
|
}
|
|
}
|
|
}
|