Files
new-api/relaykit/relayconvert/internal/oai_chat/citations.go
T
Calcium-Ion 0ed497f066 feat(relay): hosted-tool conversion fidelity, reasoning normalization, and billing usage integrity (#7137)
* feat(relaykit): preserve hosted tools across conversions

- add protocol-neutral hosted-tool DTOs, conversion metadata, and loss policies
- bridge citations, grounding metadata, and hosted-tool stream lifecycles
- document the public conversion behavior and channel policy controls

* refactor(relaykit): normalize reasoning and thinking intent

- centralize provider-neutral reasoning intent, effort, and budget mappings
- parse model suffixes at the host entry boundary while preserving provider-owned tails
- keep adaptive Claude thinking and explicit zero-token compatibility consistent

* fix(billing): preserve authoritative usage across relay hops

- carry native BillingUsage sidecars through direct and streamed protocol bridges
- merge partial and terminal usage monotonically with safe fallback settlement
- retain cache metadata, penultimate usage, and per-call Gemini tool surcharges

* feat(relay): bridge Responses with Claude and Gemini protocols

- add direct request, response, and stream converters across supported relay formats
- expose Claude count_tokens and Chat-to-Responses compatibility endpoints
- carry conversion diagnostics through the host while retaining the curated public goldens

* fix(relay): wire relaykit conversions into host channels

- connect handlers, adaptors, and channel settings to the standalone conversion layer
- keep model mapping, pricing identity, retries, and provider-specific suffix behavior aligned
- ignore local audit artifacts and retain focused public regression coverage
2026-09-01 21:53:35 +08:00

82 lines
2.2 KiB
Go

package oaichat
import (
"encoding/json"
"strings"
"unicode/utf8"
kitutil "github.com/QuantumNous/new-api/relaykit/relayconvert/kitutil"
)
func chatAnnotationsToClaude(raw json.RawMessage, text string) []json.RawMessage {
if len(raw) == 0 {
return nil
}
var annotations []map[string]any
if err := kitutil.Unmarshal(raw, &annotations); err != nil {
return nil
}
citations := make([]json.RawMessage, 0, len(annotations))
for _, annotation := range annotations {
if strings.TrimSpace(kitutil.Interface2String(annotation["type"])) != "url_citation" {
continue
}
citation, ok := annotation["url_citation"].(map[string]any)
if !ok {
citation = annotation
}
url := strings.TrimSpace(kitutil.Interface2String(citation["url"]))
if url == "" {
continue
}
converted := map[string]any{
"type": "web_search_result_location",
"url": url,
"title": strings.TrimSpace(kitutil.Interface2String(citation["title"])),
}
if citedText := kitutil.Interface2String(citation["cited_text"]); citedText != "" {
converted["cited_text"] = citedText
} else if citedText := citedTextFromAnnotation(text, citation); citedText != "" {
converted["cited_text"] = citedText
}
if encryptedIndex := kitutil.Interface2String(citation["encrypted_index"]); encryptedIndex != "" {
converted["encrypted_index"] = encryptedIndex
}
if converted["title"] == "" {
delete(converted, "title")
}
encoded, err := kitutil.Marshal(converted)
if err == nil {
citations = append(citations, encoded)
}
}
return citations
}
func citedTextFromAnnotation(text string, citation map[string]any) string {
start, startOK := annotationIndex(citation["start_index"])
end, endOK := annotationIndex(citation["end_index"])
if !startOK || !endOK || start < 0 || end <= start {
return ""
}
if end > utf8.RuneCountInString(text) {
return ""
}
runes := []rune(text)
return string(runes[start:end])
}
func annotationIndex(value any) (int, bool) {
switch number := value.(type) {
case float64:
return int(number), number >= 0 && number == float64(int(number))
case int:
return number, number >= 0
case json.Number:
parsed, err := number.Int64()
return int(parsed), err == nil && parsed >= 0
default:
return 0, false
}
}