matrix0487 on Discord:
- The Requests card's line "↑ 13 GB · ↓ 13 MB · 图片文件省下 44…" was cut
with an ellipsis. It was one nowrap span.
- A row's details say "图片文件省下 10 MB" and should say "10 MB(3 张)".
The card line is now made of whole parts (usageParts, the pattern the
token line has since #2378). A card too narrow for all three puts the
saving on a line of its own. A part that is wider than the card by
itself (German at 420 wide) wraps inside itself; it is never cut.
The count:
- withDeepSeekFiles counts the image blocks it puts a file id in place
of, on each try, and upBytes keeps the most of any one try (a retry
sends the same images again, as the saved bytes are counted).
- It goes into usage.Record as `image_files` (omitempty) beside
image_files_saved.
- packedRow keeps Tries and ImageFiles in int16 each (clamped to
MaxInt16), so the row stays 152 bytes. request_snapshot carries the
new field, and heat_disk hashes Tries as before.
- The detail row says "{size} ({n} image|images)": zh "10 MB(3 张)",
zh-TW 張, ja 枚, de "Bild/Bilder", en "image/images".
- A row from before the count was kept has no image_files and shows
the size alone, with no parentheses. The card's period total gets no
count.
Run on mote, at this tree:
- gofmt -l (clean), go vet ./..., GOOS=linux go vet -tags nogui ./...,
GOOS=windows go vet ./..., go build ./...: ok.
- go test -run 'TestRowImageFilesKept|TestRowBytesKept|TestRowsOfOneSession|
TestDeepSeek|Snapshot|Heat' ./internal/usage ./internal/gateway: ok.
- TestRowImageFilesKept: the count survives pack/unpack beside Tries,
100000 clamps to 32767, an old row reads 0, the JSON has
"image_files":3 and leaves out 0, and packedRow is 152 bytes.
- TestDeepSeekImageFilesCountedInRow: two images in one message and
one again in a later message make 3 in the stored row, twice, and a
request with no image makes 0.
- GUI, BROWSER=chromium and webkit: usage-ledger-bytes runs every
language at 1180 and 420. The 3-image and 1-image rows read in each
language's unit, singular in en and de, and the old row stays bare.
The reporter's period (13 GB, 13 MB, 44 MB) at 1180, 760 and 420 has
every part whole and inside its card.
- The 79 usage-*/led* GUI files (concurrency 2): 641 of 659 pass in
chromium and 637 of 654 in webkit. Every red is filed and red on
origin/main d2c29abe7 too (run alone there in chromium, the same 17
subtests): usage-agents-hide (#2276, #3414), usage-fold (#3415, #2163;
in webkit also a timeout in "several accounts"), usage-ledger's
return-to-live (#4078, #5048) and usage-overview-fold (#3947).
- Broken by hand, each red:
- The count left out of packedRow: TestRowImageFilesKept.
- The count not kept in withDeepSeekFiles:
TestDeepSeekImageFilesCountedInRow ("0 images by file, want 3").
- The detail's count dropped: usage-ledger-bytes in every language
("expected '10 MB(3 张)', actual '10 MB'").
- The in-part wrap dropped: "de big 420" ("cut: 0,0,0,11").
Not tried: a real DeepSeek account (the count rides the same path as the
saved bytes, which were tried for #5407), and Linux WebKitGTK.
Reported-by: matrix0487
75 lines
2.4 KiB
Go
75 lines
2.4 KiB
Go
package gateway
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"net/http"
|
|
"sync/atomic"
|
|
)
|
|
|
|
// upBytes is what a request's tries sent up to the vendor, kept in the
|
|
// request's context as a tool call's mcpUse is: forwardOnce adds a try's
|
|
// bytes where it already has them (sent), and the request's ledger row
|
|
// reads the sum, so the row says what a session's upstream traffic was
|
|
// and a hand-on's re-send stands beside Tries (#4456).
|
|
type upBytes struct {
|
|
wire int // the bytes that went on the wire, tries summed
|
|
plain int // the last try's body before compression
|
|
// down is the bytes of the vendor's replies read, tries summed: added
|
|
// as a body is read, which can be on another goroutine than the
|
|
// request's (a stream's pump), so atomic
|
|
down atomic.Int64
|
|
// saved is what DeepSeek's image files kept out of the tries' bodies,
|
|
// less what uploading them cost this request (withDeepSeekFiles)
|
|
saved int
|
|
// images is how many inline images went by file instead, the most of
|
|
// any one try: a retry sends the same ones again
|
|
images int
|
|
}
|
|
|
|
type upBytesKey struct{}
|
|
|
|
func upBytesOf(ctx context.Context) *upBytes {
|
|
u, _ := ctx.Value(upBytesKey{}).(*upBytes)
|
|
return u
|
|
}
|
|
|
|
// sentUp is that a try went to the vendor: wire the bytes it sent
|
|
// (len(body), or what CodexCompressed left on req), plain the body before
|
|
// that. Read and written on the request's own goroutine — the ledger row
|
|
// is assembled after the last try came back — so no lock.
|
|
func sentUp(ctx context.Context, wire, plain int) {
|
|
if u := upBytesOf(ctx); u != nil {
|
|
u.wire += wire
|
|
u.plain = plain
|
|
}
|
|
}
|
|
|
|
// gotDown counts res's body for the request's ledger row as it is read
|
|
// (upBytes.down), and gives res back. A body is counted as Go's client
|
|
// hands it over: after the transport undid a gzip it asked for, so a
|
|
// gzipped reply counts more than its wire bytes. A 101's body is the
|
|
// connection itself and is left as it is.
|
|
func gotDown(ctx context.Context, res *http.Response) *http.Response {
|
|
u := upBytesOf(ctx)
|
|
if u == nil || res == nil || res.Body == nil || res.Body == http.NoBody || res.StatusCode == http.StatusSwitchingProtocols {
|
|
return res
|
|
}
|
|
res.Body = &downCount{ReadCloser: res.Body, u: u}
|
|
return res
|
|
}
|
|
|
|
type downCount struct {
|
|
io.ReadCloser
|
|
u *upBytes
|
|
}
|
|
|
|
func (d *downCount) Read(b []byte) (int, error) {
|
|
n, err := d.ReadCloser.Read(b)
|
|
d.u.down.Add(int64(n))
|
|
return n, err
|
|
}
|
|
|
|
// downOf is the reply bytes counted so far (upBytes.down).
|
|
func (u *upBytes) downOf() int { return int(u.down.Load()) }
|