返回 DeepSeek-Reasonix
opencode_headers.go
根目录 / internal / provider / opencode_headers.go
1 package provider
2
3 import (
4 "context"
5 "crypto/rand"
6 "crypto/sha256"
7 "encoding/hex"
8 "net/http"
9 )
10
11 type cacheSessionContextKey struct{}
12
13 // WithCacheSession scopes transport cache routing to a conversation. The hash
14 // prevents local session paths or user-provided identifiers entering headers.
15 // It deliberately replaces inherited identities when a child Agent starts.
16 func WithCacheSession(ctx context.Context, identity string) context.Context {
17 sum := sha256.Sum256([]byte(identity))
18 return context.WithValue(ctx, cacheSessionContextKey{}, "reasonix-"+hex.EncodeToString(sum[:16]))
19 }
20
21 // NewCacheSessionID provides a stable per-client fallback for standalone calls.
22 func NewCacheSessionID() string { return "reasonix-" + rand.Text() }
23
24 // NewClientIdentityHeaders groups immutable transport identity by client lifetime.
25 func NewClientIdentityHeaders() http.Header {
26 return http.Header{"User-Agent": []string{"Reasonix"}, "X-Opencode-Session": []string{NewCacheSessionID()}}
27 }
28
29 // ApplyOpenCodeGoHeaders implements Go's client identification/cache contract
30 // for all three wire adapters. It never changes model input or other vendors.
31 func ApplyOpenCodeGoHeaders(req *http.Request, _ string, fallback http.Header) {
32 path, ok := officialOpenCodeGoPath(req.URL.String())
33 if !ok {
34 return
35 }
36 switch path {
37 case "/zen/go/v1/chat/completions", "/zen/go/v1/messages", "/zen/go/v1/responses", "/zen/go/v1/models",
38 "/inference/go/openai/v1/chat/completions", "/inference/go/anthropic/v1/messages", "/inference/go/openai/v1/responses", "/inference/go/v1/models":
39 default:
40 return
41 }
42 if req.Header.Get("User-Agent") == "" {
43 req.Header.Set("User-Agent", fallback.Get("User-Agent"))
44 }
45 if req.Header.Get("x-opencode-session") != "" {
46 return
47 }
48 id, _ := req.Context().Value(cacheSessionContextKey{}).(string)
49 if id == "" {
50 id = fallback.Get("x-opencode-session")
51 }
52 if id != "" {
53 req.Header.Set("x-opencode-session", id)
54 }
55 }
56
56 lines GO