H20 on Discord (#general, 10-24): a client offering web_search_20250305
to auto/deepseek-v4.1-flash or deepseek/deepseek-flash got its text
blocks back with no citations, where Anthropic's API gives
web_search_result_location entries pointing at the
web_search_tool_result pages.
magpie's web_search tool asks the model to "Cite the URLs you use", so
the answer names pages by URL. After a KSearch event (or a Search part),
the Anthropic encoder and renderAnthropic now cut the following text
into lines (citer, search_cite.go). A line naming a found page's URL
whole (urlIn: not a longer path, a query or another host; Chinese
punctuation, markdown links and bold end a URL) is its own text block
with citations {type, url, title, encrypted_index, cited_text}; the
lines between run together in one uncited block, and whitespace-only
lines are held for the next, so no block is whitespace alone (Anthropic
refuses one sent back). Streamed, a cited block's citations_delta come
before its text_delta; text is buffered to the line end, only after a
search. The text is the model's, unchanged. A reply with no search is
said exactly as before.
encrypted_index is "", as the results' encrypted_content already is:
it is Anthropic's sealed pointer, which magpie can't make (Claude Code
2.1.310 knows "invalid encrypted_index in text block" and strips these
citations when that comes back). cited_text is Hit.Cited cut to 150
characters: a search API's page text, or the cited_text of an Anthropic
searcher's own citation (searchWith now reads it), else "". No prompt
text sent upstream changed; no Codex prompt touched.
Sibling filed: Responses and Chat replies still carry no url_citation
annotations (#3455). docs/subsystems/gateway-routing.md has a new
"Citations in translated Anthropic replies" section.
Run (at c579609d9 on ed91aa04e, sandbox HOME, pinned caches):
- go test ./internal/gateway -run 'TestSearchCitedAsAnthropics|
TestNoSearchNoCitations|TestCitesOnlyTheWholeURL|
TestWebSearchForAModelThatCannot|TestSearchToldAsAnthropics': ok
- go test ./... -timeout 45m -count=1: exit 0, 70 packages ok,
internal/gateway ok 647s
- gofmt -l internal/gateway: empty; go vet ./internal/gateway,
GOOS=linux go vet -tags nogui ./..., GOOS=windows go vet ./...,
go build ./...: ok
Broken by hand: with anthropic.go put back to main,
TestSearchCitedAsAnthropics/stream and /body both FAIL ("blocks [text
server_tool_use web_search_tool_result text]": one text block, no
citations); restored, both pass. TestNoSearchNoCitations holds a reply
with no search to one uncited block, streamed and whole.
Not tried: a real DeepSeek model through a running magpie (whether it
writes the URLs the tool asks for, and how often); a real Anthropic API
taking such a reply back in history; [n] markers (a search API's
numbered pages) are not matched, since numbers repeat across searches.
134 lines
4.1 KiB
Go
134 lines
4.1 KiB
Go
package gateway
|
|
|
|
import "strings"
|
|
|
|
// After a search magpie ran for a model (searchReply), or one a provider
|
|
// ran and reported (KSearch), the model's answer names the pages it used
|
|
// by their addresses: magpie's web_search tool asks it to cite the URLs.
|
|
// An Anthropic client is told which pages a line cites as Anthropic's own
|
|
// search tells it, a text block whose citations are
|
|
// web_search_result_location entries for the pages of the reply's
|
|
// web_search_tool_result blocks. A line naming a page found is its own
|
|
// text block with those citations; the lines between them run together in
|
|
// one block as before. The text is the model's, unchanged.
|
|
//
|
|
// encrypted_index is Anthropic's sealed pointer into its own result, which
|
|
// magpie can't make: it is "", as the results' encrypted_content is
|
|
// (searchResultBlock). cited_text is what the search said of the page, when
|
|
// it said something (Hit.Cited), else "".
|
|
|
|
// citedTextMost is how much of a page's text a citation quotes, as
|
|
// Anthropic's does (up to 150 characters).
|
|
const citedTextMost = 150
|
|
|
|
// citer cuts a reply's text into lines once a search has found pages, and
|
|
// says which of the pages each line cites. A line that is only whitespace
|
|
// is held for the next, so no block is whitespace alone (Anthropic turns
|
|
// such a block away when the client sends it back).
|
|
type citer struct {
|
|
hits []Hit
|
|
line string // the line being said, not yet ended
|
|
held string // whitespace lines held for the next line
|
|
}
|
|
|
|
// on is whether there are pages a line may cite.
|
|
func (c *citer) on() bool { return len(c.hits) > 0 }
|
|
|
|
// feed takes text and says each line it ends: its text and citations.
|
|
func (c *citer) feed(text string, say func(text string, cites []map[string]any, tail bool)) {
|
|
c.line += text
|
|
for {
|
|
i := strings.IndexByte(c.line, '\n')
|
|
if i < 0 {
|
|
return
|
|
}
|
|
line := c.line[:i+1]
|
|
c.line = c.line[i+1:]
|
|
c.end(line, say)
|
|
}
|
|
}
|
|
|
|
func (c *citer) end(line string, say func(string, []map[string]any, bool)) {
|
|
if strings.TrimSpace(line) == "" {
|
|
c.held += line
|
|
return
|
|
}
|
|
line, c.held = c.held+line, ""
|
|
say(line, citesOf(line, c.hits), false)
|
|
}
|
|
|
|
// flush says what is left: a line not ended, and the whitespace held,
|
|
// which goes on the block said last (tail).
|
|
func (c *citer) flush(say func(string, []map[string]any, bool)) {
|
|
line := c.line
|
|
c.line = ""
|
|
if strings.TrimSpace(line) != "" {
|
|
c.end(line, say)
|
|
return
|
|
}
|
|
if rest := c.held + line; rest != "" {
|
|
c.held = ""
|
|
say(rest, nil, true)
|
|
}
|
|
}
|
|
|
|
// citesOf are the citations of the pages line names, in the order it
|
|
// names them, each once.
|
|
func citesOf(line string, hits []Hit) []map[string]any {
|
|
type at struct {
|
|
i int
|
|
h Hit
|
|
}
|
|
var found []at
|
|
seen := map[string]bool{}
|
|
for _, h := range hits {
|
|
u := strings.TrimSuffix(h.URL, "/")
|
|
if u == "" || seen[u] {
|
|
continue
|
|
}
|
|
if i := urlIn(line, u); i >= 0 {
|
|
seen[u] = true
|
|
found = append(found, at{i, h})
|
|
}
|
|
}
|
|
if len(found) == 0 {
|
|
return nil
|
|
}
|
|
// in the order the line names them
|
|
for i := 1; i < len(found); i++ {
|
|
for j := i; j > 0 && found[j].i < found[j-1].i; j-- {
|
|
found[j], found[j-1] = found[j-1], found[j]
|
|
}
|
|
}
|
|
cites := make([]map[string]any, 0, len(found))
|
|
for _, f := range found {
|
|
cites = append(cites, map[string]any{"type": "web_search_result_location", "url": f.h.URL, "title": f.h.Title,
|
|
"encrypted_index": "", "cited_text": firstRunes(strings.TrimSpace(f.h.Cited), citedTextMost)})
|
|
}
|
|
return cites
|
|
}
|
|
|
|
// urlIn is where line names the address u (its trailing slash cut), the
|
|
// whole of it: what follows, past a slash and the punctuation that ends a
|
|
// sentence or markdown's emphasis, is the end, a space, a bracket, a quote
|
|
// or a character outside ASCII (Chinese punctuation). -1 when it doesn't.
|
|
func urlIn(line, u string) int {
|
|
for from := 0; ; {
|
|
i := strings.Index(line[from:], u)
|
|
if i < 0 {
|
|
return -1
|
|
}
|
|
i += from
|
|
rest := line[i+len(u):]
|
|
if urlEnds(strings.TrimPrefix(rest, "/")) {
|
|
return i
|
|
}
|
|
from = i + 1
|
|
}
|
|
}
|
|
|
|
func urlEnds(rest string) bool {
|
|
rest = strings.TrimLeft(rest, ".,;:!?*_")
|
|
return rest == "" || rest[0] >= 0x80 || strings.IndexByte(" \t\r\n)]}>\"'`|", rest[0]) >= 0
|
|
}
|