]> git.giacomolli.uk - onechat/blobdiff - responses.go
Leave the turn state out of the usage records
[onechat] / responses.go
index 9d6b75f6c05ce2e55335e77b44877b69ea56df5f..cdc1558195e159cd111f0e4968f8e9f5d9aea941 100644 (file)
@@ -27,6 +27,10 @@ const (
        // backend, in canonical form.
        quotaPrefix = "X-Codex-"
 
+       // turnStateHeader is an x-codex-* header that holds an opaque state of
+       // the backend, not quota.
+       turnStateHeader = "X-Codex-Turn-State"
+
        // errorBytes is the most bytes of an error response that go into the
        // error.
        errorBytes = 4096
@@ -57,7 +61,6 @@ type Client struct {
        cacheKey string
        usage    *usageLog
        http     *http.Client
-       clock    func() time.Time
 }
 
 // Request is one model call. Input holds the input items in order, such as
@@ -179,8 +182,7 @@ func newClient(url string, auth authorizer, cacheKey, usagePath string) *Client
                usage: &usageLog{
                        path: usagePath,
                },
-               http:  &http.Client{},
-               clock: time.Now,
+               http: &http.Client{},
        }
 }
 
@@ -216,7 +218,7 @@ func (c *Client) Create(ctx context.Context, request Request) (Response, error)
        }
 
        record := usageRecord{
-               Date:  c.clock().Format(time.RFC3339Nano),
+               Date:  time.Now().Format(time.RFC3339Nano),
                Model: request.Model,
        }
 
@@ -264,7 +266,8 @@ type usageLog struct {
 }
 
 // usageRecord is one line of usage.jsonl. Status is the HTTP status, or 0 when
-// no response came. Quota holds the x-codex-* headers of the response.
+// no response came. Quota holds the x-codex-* headers of the response, without
+// the turn state.
 type usageRecord struct {
        Date         string            `json:"date"`
        Model        string            `json:"model"`
@@ -333,11 +336,12 @@ func userMessage(texts ...string) any {
        }
 }
 
-// quotaHeaders returns the x-codex-* headers, with lowercase names.
+// quotaHeaders returns the x-codex-* headers, with lowercase names, without the
+// turn state.
 func quotaHeaders(header http.Header) map[string]string {
        var quota map[string]string
        for name, values := range header {
-               if !strings.HasPrefix(name, quotaPrefix) || len(values) == 0 {
+               if !strings.HasPrefix(name, quotaPrefix) || name == turnStateHeader || len(values) == 0 {
                        continue
                }