Skip to content

Commit eadccb2

Browse files
committed
feat(executor): add initial cache_helpers.go file
1 parent fed6f3e commit eadccb2

File tree

2 files changed

+56
-1
lines changed

2 files changed

+56
-1
lines changed
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package executor
2+
3+
import "time"
4+
5+
type codexCache struct {
6+
ID string
7+
Expire time.Time
8+
}
9+
10+
var codexCacheMap = map[string]codexCache{}

internal/runtime/executor/codex_executor.go

Lines changed: 46 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,6 +78,26 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
7878
body, _ = sjson.SetBytes(body, "stream", true)
7979
body, _ = sjson.DeleteBytes(body, "previous_response_id")
8080

81+
additionalHeaders := make(map[string]string)
82+
if from == "claude" {
83+
userIDResult := gjson.GetBytes(req.Payload, "metadata.user_id")
84+
if userIDResult.Exists() {
85+
var cache codexCache
86+
var hasKey bool
87+
key := fmt.Sprintf("%s-%s", req.Model, userIDResult.String())
88+
if cache, hasKey = codexCacheMap[key]; !hasKey || cache.Expire.Before(time.Now()) {
89+
cache = codexCache{
90+
ID: uuid.New().String(),
91+
Expire: time.Now().Add(1 * time.Hour),
92+
}
93+
codexCacheMap[key] = cache
94+
}
95+
additionalHeaders["Conversation_id"] = cache.ID
96+
additionalHeaders["Session_id"] = cache.ID
97+
body, _ = sjson.SetBytes(body, "prompt_cache_key", cache.ID)
98+
}
99+
}
100+
81101
url := strings.TrimSuffix(baseURL, "/") + "/responses"
82102
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
83103
if err != nil {
@@ -90,6 +110,9 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
90110
authLabel = auth.Label
91111
authType, authValue = auth.AccountInfo()
92112
}
113+
for k, v := range additionalHeaders {
114+
httpReq.Header.Set(k, v)
115+
}
93116
recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
94117
URL: url,
95118
Method: http.MethodPost,
@@ -101,7 +124,6 @@ func (e *CodexExecutor) Execute(ctx context.Context, auth *cliproxyauth.Auth, re
101124
AuthType: authType,
102125
AuthValue: authValue,
103126
})
104-
105127
httpClient := newProxyAwareHTTPClient(ctx, e.cfg, auth, 0)
106128
resp, err := httpClient.Do(httpReq)
107129
if err != nil {
@@ -183,6 +205,26 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
183205

184206
body, _ = sjson.DeleteBytes(body, "previous_response_id")
185207

208+
additionalHeaders := make(map[string]string)
209+
if from == "claude" {
210+
userIDResult := gjson.GetBytes(req.Payload, "metadata.user_id")
211+
if userIDResult.Exists() {
212+
var cache codexCache
213+
var hasKey bool
214+
key := fmt.Sprintf("%s-%s", req.Model, userIDResult.String())
215+
if cache, hasKey = codexCacheMap[key]; !hasKey || cache.Expire.Before(time.Now()) {
216+
cache = codexCache{
217+
ID: uuid.New().String(),
218+
Expire: time.Now().Add(1 * time.Hour),
219+
}
220+
codexCacheMap[key] = cache
221+
}
222+
additionalHeaders["Conversation_id"] = cache.ID
223+
additionalHeaders["Session_id"] = cache.ID
224+
body, _ = sjson.SetBytes(body, "prompt_cache_key", cache.ID)
225+
}
226+
}
227+
186228
url := strings.TrimSuffix(baseURL, "/") + "/responses"
187229
httpReq, err := http.NewRequestWithContext(ctx, http.MethodPost, url, bytes.NewReader(body))
188230
if err != nil {
@@ -195,6 +237,9 @@ func (e *CodexExecutor) ExecuteStream(ctx context.Context, auth *cliproxyauth.Au
195237
authLabel = auth.Label
196238
authType, authValue = auth.AccountInfo()
197239
}
240+
for k, v := range additionalHeaders {
241+
httpReq.Header.Set(k, v)
242+
}
198243
recordAPIRequest(ctx, e.cfg, upstreamRequestLog{
199244
URL: url,
200245
Method: http.MethodPost,

0 commit comments

Comments
 (0)