diff --git a/docs/subsystems/claude-subscription-bridge.md b/docs/subsystems/claude-subscription-bridge.md index 8bbe71a0f..1d11261e7 100644 --- a/docs/subsystems/claude-subscription-bridge.md +++ b/docs/subsystems/claude-subscription-bridge.md @@ -724,6 +724,13 @@ history to the 1h cache at 2×. are the history's, or the history's first and then the turn's (`currentTurnOpen`, #1751). Told nothing, the model said a text-only turn had an image attached and offered to open the path. + When the images are all the turn's, as a tool's seven photos in a + first turn (no history to open the notes' owner), `imageNotesNote` ends + the turn: the images, tool results' too, are in the message, and the + notes are Claude Code's record of where it keeps them. Told nothing, + the model took the paths for the images and said it could not see them + (a Discord report, v0.1.2862). A turn told to a saved session says the + same (`renderClaudeTurn`). A Claude Code that ends before it answers a control request (`cliControl`: `initialize`, the safety context) is reported with its diff --git a/internal/gateway/claude_prompt_history_test.go b/internal/gateway/claude_prompt_history_test.go index a55a8b680..00cad3edb 100644 --- a/internal/gateway/claude_prompt_history_test.go +++ b/internal/gateway/claude_prompt_history_test.go @@ -123,3 +123,58 @@ func TestClaudePromptSaysWhoseImageNotesAre(t *testing.T) { t.Fatalf("a history with no image:\n%s", now) } } + +// A tool that returns images (seven photos, as sinsoul1647 on Discord, +// v0.1.2862) leaves Claude Code's notes after the message with paths in the +// gateway's temp folder ("[Image: source: …/images/1.jpg]"). With no history +// before the turn there was nothing to say what they are: the model took the +// paths for the images, said it could not see them and quoted them as on +// another computer. The turn says the images are in it, in a run told the +// whole conversation and in a turn told to a saved session. +func TestClaudePromptSaysToolResultImagesAreAttached(t *testing.T) { + img := Part{MediaType: "image/png", Data: "iVBORw0KGgo="} + calls := make([]Part, 7) + results := make([]Part, 7) + for i := range calls { + id := "call_" + string(rune('a'+i)) + calls[i] = Part{Kind: ToolCall, ID: id, Name: "view", Args: json.RawMessage(`{}`)} + results[i] = Part{Kind: ToolResult, CallID: id, Text: "Read image file [image/png]", Images: []Part{img}} + } + msgs := []Message{ + {Role: "user", Parts: []Part{{Kind: Text, Text: "look at the seven pictures"}}}, + {Role: "assistant", Parts: calls}, + {Role: "user", Parts: results}, + } + text := func(blocks []map[string]any) string { + var all strings.Builder + images := 0 + for _, b := range blocks { + if b["type"] == "image" { + images++ + continue + } + s, _ := b["text"].(string) + all.WriteString(s) + } + if images != 7 { + t.Fatalf("%d image blocks, want 7", images) + } + return all.String() + } + tools := []Tool{{Name: "view"}} + blocks, err := renderClaudePrompt(&Request{Messages: msgs, Tools: tools}) + if err != nil { + t.Fatal(err) + } + if got := text(blocks); !strings.Contains(got, imageNotesNote) { + t.Fatalf("the prompt doesn't say the images are attached:\n%s", got) + } + if got := text(renderClaudeTurn(msgs[2:], tools)); !strings.Contains(got, imageNotesNote) { + t.Fatalf("the saved session's turn doesn't say the images are attached:\n%s", got) + } + // no image, no note + blocks, _ = renderClaudePrompt(&Request{Messages: msgs[:1]}) + if got, _ := json.Marshal(blocks); strings.Contains(string(got), "image content") { + t.Fatalf("a turn with no image has the note: %s", got) + } +} diff --git a/internal/gateway/claude_subscription.go b/internal/gateway/claude_subscription.go index 84bdef8a7..6004cebbc 100644 --- a/internal/gateway/claude_subscription.go +++ b/internal/gateway/claude_subscription.go @@ -4093,6 +4093,9 @@ func renderClaudePrompt(req *Request) ([]map[string]any, error) { chunk = len(blocks) - 1 } } + if countImages(req.Messages[split:]) > 0 && (split == 0 || countImages(req.Messages[:split]) == 0) { + text.WriteString(imageNotesNote + "\n\n") + } if slices.ContainsFunc(req.Messages, callsTool) { text.WriteString(pastCallsNote + "\n\n") } @@ -4262,6 +4265,17 @@ func currentTurnOpen(msgs []Message, split int) string { "the images above in their order: the conversation history's first, then this turn's.\n\n" } +// imageNotesNote ends a turn whose images (a tool result's among them) all +// belong to it, where currentTurnOpen has no history to say whose the notes +// are. Claude Code notes each image it is given ("[Image: source: +// …/images/3.jpg]") in a note after the message, with a path in the +// gateway's temp folder. Told nothing of them, the model took the notes for +// the images, said it could not see them and quoted the paths as on +// another computer (a Discord report, v0.1.2862). +const imageNotesNote = "(The images above, those in tool results too, are attached to this message as image content you can see. " + + "Any \"[Image: …]\" notes after it are Claude Code's record of where it keeps them, on its own computer: " + + "a path in one is not something to open, and the image itself is in the message.)" + // countImages is how many images msgs carry, their tool results' among them. func countImages(msgs []Message) int { n := 0 @@ -4493,6 +4507,9 @@ func renderClaudeTurn(msgs []Message, tools []Tool) []map[string]any { } blocks = renderParts(blocks, &text, m.Parts, offered) } + if countImages(msgs) > 0 { + text.WriteString("\n\n" + imageNotesNote) + } if slices.ContainsFunc(msgs, callsTool) { text.WriteString("\n\n" + pastCallsNote) }