Hana's pre-step hook replaces each image before the last assistant message
with "[图片已省略:历史图片保留为文件引用,避免重复发送原始 base64]" in the
tool result or user message that held it. Over pi-ai's openai-completions
the user message of the results' images ("Attached image(s) from tool
result:" and the images) is gone and each result's text has the line added
to its end. The step after one that read an image missed its run both
ways: tool results failed follows() (the hash of what the run heard), and a
new turn failed turnKey and looseTurn.rewrote (message count changed). A
new claude -p was told the whole conversation: 10 of 67 requests, 79% of
the session's cache writes.
lostMedia (claude_lost_media.go) tells this rewrite by its shape, not by
Hana's string: comparing the conversation a run had (each part kept as the
hash and length of its words, heardRuns) with the request's, a role's run
of messages at a time, it holds when at least one image or file was taken
out and nothing else changed: each image/file is still there, gone, or has
text in its place; a tool result may have text added to its end only when
it carried images or the images after the results went; the caption
before those images goes with them only when a result before it grew.
follows() checks it when the hash misses (toldRuns), and looseTurn.rewrote
when the #912 cut rule doesn't match; the looseTurn key drops the message
count, which the per-turn comparison covers. messageWords is now built from
messageParts, the same words, so the hash and the comparison can't drift.
The shape over the string: any client that strips media the same way is
covered, and a compaction or edit (#459) changes words lostMedia never
accepts. Not covered: a tool result of an image alone, whose pi-ai text
"(see attached image)" is replaced rather than added to.
claude_lost_media_test.go builds its fixtures from pi-ai's literal Chat
Completions bodies (read from the installed HanaAgent server seed, 1.0.0-beta: pi-ai
openai-completions.js and Hana's media-strip hook): a table of 18 shapes
for lostMedia, an end-to-end new turn through serveClaudeSubscription with
the script Claude Code (a tool's image, a user's image), and tool results
after the image step through serveSubscription (goes on; a changed user
message or a changed result still starts anew). The two end-to-end tests
fail with lostMedia unhooked from follows/rewrote; each guard in
lostFromRun was broken by hand and turned a table case red.
docs/subsystems/claude-subscription-bridge.md has the rule.
Run: gofmt -l; go vet -tags nogui ./internal/gateway/ (darwin, GOOS=linux,
GOOS=windows); go test -tags nogui ./internal/gateway/ -count=1 under a
temp HOME (ok); -race -count=5 on the lost-media, rewritten, #459, letGo,
given-up and fork tests (ok). Not tried against a real Hana or a sandbox
magpie with a real claude: the harness's script stands in for Claude Code.
202 lines
6.3 KiB
Go
202 lines
6.3 KiB
Go
package gateway
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"fmt"
|
|
"strings"
|
|
)
|
|
|
|
// A client may send its conversation back with the images and files of
|
|
// earlier messages taken out (#1382): Hana's pre-step hook replaces each
|
|
// image before the last reply with a line of text, in the tool result or
|
|
// user message that held it, so pi-ai's Chat Completions body no longer
|
|
// has the user message of the tool results' images ("Attached image(s)
|
|
// from tool result:" and the images) and each result's text has the line
|
|
// added to its end. The run that had the conversation was given the
|
|
// images, and its Claude Code still holds them: told the conversation
|
|
// anew, it would write all of it to the prompt cache again, at each step
|
|
// after one that read an image. lostMedia tells this rewrite from any
|
|
// other by its shape, not the client's words for it.
|
|
|
|
// wordPart is a part of a message as hashMessages reads it: its kind and
|
|
// its words, and for a tool result how many images it carried beside them.
|
|
type wordPart struct {
|
|
kind Kind
|
|
words string
|
|
media int
|
|
}
|
|
|
|
// messageParts is each message's parts as hashMessages reads them, those
|
|
// with no words left out. A call is told by its place among the
|
|
// conversation's calls, and a result by the call it answers.
|
|
func messageParts(msgs []Message) [][]wordPart {
|
|
out := make([][]wordPart, len(msgs))
|
|
n, calls := 0, map[string]int{} // a call's id → its place
|
|
for i, m := range msgs {
|
|
for _, p := range m.Parts {
|
|
var s string
|
|
media := 0
|
|
switch p.Kind {
|
|
case Text:
|
|
s = p.Text
|
|
case ToolCall:
|
|
n++
|
|
calls[p.ID] = n
|
|
s = fmt.Sprintf("\x01call %s #%d", p.Name, n)
|
|
case ToolResult:
|
|
k := "?"
|
|
if n, ok := calls[p.CallID]; ok {
|
|
k = fmt.Sprint(n)
|
|
}
|
|
s = fmt.Sprintf("\x01result #%s %s", k, p.Text)
|
|
media = len(p.Images)
|
|
case File:
|
|
s = fmt.Sprintf("\x01file %s %d %s", p.MediaType, len(p.Data), p.URL)
|
|
case Image:
|
|
s = fmt.Sprintf("\x01image %d %s", len(p.Data), p.URL)
|
|
default:
|
|
continue
|
|
}
|
|
if words := strings.Join(strings.Fields(s), " "); words != "" {
|
|
out[i] = append(out[i], wordPart{p.Kind, words, media})
|
|
}
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
// heardPart is a wordPart as a run keeps it: the hash and length of its
|
|
// words. words is set only on the conversation compared with what a run
|
|
// kept, which is a request's own.
|
|
type heardPart struct {
|
|
kind Kind
|
|
n int
|
|
sum [sha256.Size]byte
|
|
media int
|
|
words string
|
|
}
|
|
|
|
// heardRun is a role's messages in a row, as hashMessages runs them
|
|
// together: how a reply is split into messages is left out.
|
|
type heardRun struct {
|
|
role string
|
|
parts []heardPart
|
|
}
|
|
|
|
// heardRuns is msgs as lostMedia compares them; sealed keeps no words.
|
|
func heardRuns(msgs []Message, sealed bool) []heardRun {
|
|
var out []heardRun
|
|
for i, parts := range messageParts(msgs) {
|
|
if len(parts) == 0 {
|
|
continue
|
|
}
|
|
if len(out) == 0 || out[len(out)-1].role != msgs[i].Role {
|
|
out = append(out, heardRun{role: msgs[i].Role})
|
|
}
|
|
r := &out[len(out)-1]
|
|
for _, p := range parts {
|
|
h := heardPart{kind: p.kind, n: len(p.words), sum: sha256.Sum256([]byte(p.words)), media: p.media}
|
|
if !sealed {
|
|
h.words = p.words
|
|
}
|
|
r.parts = append(r.parts, h)
|
|
}
|
|
}
|
|
return out
|
|
}
|
|
|
|
func isMedia(k Kind) bool { return k == Image || k == File }
|
|
|
|
// lostMedia says now, a request's conversation (heardRuns unsealed), is
|
|
// was, the one a run had (sealed), with images or files taken out, and
|
|
// that alone, as far as was goes: now may go on after it. Each image or
|
|
// file of was is in now, or gone, or text is in its place; a tool result
|
|
// may have text added to its end when it carried images or the user
|
|
// message of images after it went; that message, a text and images right
|
|
// after the results, may go whole when a result before it had text added.
|
|
// Nothing else changed, and something was taken out. A conversation told
|
|
// to another run, compacted or edited, is never this.
|
|
func lostMedia(was, now []heardRun) bool {
|
|
if len(now) < len(was) {
|
|
return false
|
|
}
|
|
lost := 0
|
|
for i, w := range was {
|
|
if now[i].role != w.role {
|
|
return false
|
|
}
|
|
n, ok := lostFromRun(w.parts, now[i].parts)
|
|
if !ok {
|
|
return false
|
|
}
|
|
lost += n
|
|
}
|
|
return lost > 0
|
|
}
|
|
|
|
// lostFromRun is lostMedia for a role's run of messages: how many images
|
|
// or files were taken out of was to make now, and whether that is all
|
|
// that changed.
|
|
func lostFromRun(was, now []heardPart) (int, bool) {
|
|
lost, j := 0, 0
|
|
grew := false // a result of the results just before had text added
|
|
for i, w := range was {
|
|
switch {
|
|
case j < len(now) && same(w, now[j]):
|
|
if w.kind != ToolResult {
|
|
grew = false
|
|
}
|
|
j++
|
|
case isMedia(w.kind):
|
|
// gone, or the text in its place: up to the part of was's
|
|
// that is no image or file next
|
|
lost++
|
|
next := i + 1
|
|
for next < len(was) && isMedia(was[next].kind) {
|
|
next++
|
|
}
|
|
for j < len(now) && now[j].kind == Text && (next == len(was) || !same(was[next], now[j])) {
|
|
j++
|
|
}
|
|
case w.kind == ToolResult && j < len(now) && now[j].kind == ToolResult && w.media > now[j].media && grown(w, now[j].words):
|
|
// its images, text in their place
|
|
lost += w.media - now[j].media
|
|
grew = true
|
|
j++
|
|
case w.kind == ToolResult && j < len(now) && now[j].kind == ToolResult && w.media == now[j].media && mediaAfter(was, i) && grown(w, now[j].words):
|
|
// the images sent after the results, text in their place
|
|
grew = true
|
|
j++
|
|
case w.kind == Text && grew && i > 0 && was[i-1].kind == ToolResult && i+1 < len(was) && isMedia(was[i+1].kind):
|
|
// the line before the results' images, gone with them
|
|
default:
|
|
return 0, false
|
|
}
|
|
}
|
|
return lost, j == len(now)
|
|
}
|
|
|
|
// mediaAfter says the results was[i] is among are followed by images or
|
|
// files: the user message a Chat Completions client sends a tool's images
|
|
// in, a line of text before them.
|
|
func mediaAfter(was []heardPart, i int) bool {
|
|
k := i + 1
|
|
for k < len(was) && was[k].kind == ToolResult {
|
|
k++
|
|
}
|
|
if k < len(was) && was[k].kind == Text {
|
|
k++
|
|
}
|
|
return k < len(was) && isMedia(was[k].kind)
|
|
}
|
|
|
|
// same says now's part is w.
|
|
func same(w, now heardPart) bool {
|
|
return w.kind == now.kind && w.n == len(now.words) && w.sum == sha256.Sum256([]byte(now.words))
|
|
}
|
|
|
|
// grown says words are w's with text added to their end.
|
|
func grown(w heardPart, words string) bool {
|
|
return len(words) > w.n && w.sum == sha256.Sum256([]byte(words[:w.n]))
|
|
}
|