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
This commit is contained in:
Calcium-Ion
2026-09-01 21:53:35 +08:00
committed by GitHub
parent b7017c251b
commit 0ed497f066
149 changed files with 14729 additions and 3044 deletions
+25 -10
View File
@@ -81,7 +81,7 @@ type GeneralOpenAIRequest struct {
ExtraBody json.RawMessage `json:"extra_body,omitempty"`
//xai
SearchParameters json.RawMessage `json:"search_parameters,omitempty"`
// claude
// OpenAI Chat web search.
WebSearchOptions *WebSearchOptions `json:"web_search_options,omitempty"`
// OpenRouter Params
Usage json.RawMessage `json:"usage,omitempty"`
@@ -108,6 +108,9 @@ type GeneralOpenAIRequest struct {
ReasoningSplit json.RawMessage `json:"reasoning_split,omitempty"`
// vLLM
ThinkingTokenBudget json.RawMessage `json:"thinking_token_budget,omitempty"`
// Internal conversion state; never serialized to an upstream protocol.
ReasoningConversion *ReasoningConversionState `json:"-"`
}
func (r GeneralOpenAIRequest) MarshalJSON() ([]byte, error) {
@@ -266,6 +269,7 @@ type FunctionRequest struct {
Name string `json:"name"`
Parameters any `json:"parameters,omitempty"`
Arguments string `json:"arguments,omitempty"`
Strict *bool `json:"strict,omitempty"`
}
type StreamOptions struct {
@@ -311,7 +315,10 @@ type Message struct {
Reasoning *string `json:"reasoning,omitempty"`
ToolCalls json.RawMessage `json:"tool_calls,omitempty"`
ToolCallId string `json:"tool_call_id,omitempty"`
parsedContent []MediaContent
// Annotations is an official Chat response field. Keeping it on the shared
// message type also preserves annotations when clients replay assistant output.
Annotations json.RawMessage `json:"annotations,omitempty"`
parsedContent []MediaContent
//parsedStringContent *string
}
@@ -485,14 +492,14 @@ func (m *Message) ParseToolCalls() []ToolCallRequest {
return nil
}
var toolCalls []ToolCallRequest
if err := json.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
if err := kitutil.Unmarshal(m.ToolCalls, &toolCalls); err == nil {
return toolCalls
}
return toolCalls
}
func (m *Message) SetToolCalls(toolCalls any) {
toolCallsJson, _ := json.Marshal(toolCalls)
toolCallsJson, _ := kitutil.Marshal(toolCalls)
m.ToolCalls = toolCallsJson
}
@@ -562,6 +569,11 @@ func (m *Message) ParseContent() []MediaContent {
return contentList
}
if content, ok := m.Content.([]MediaContent); ok {
m.parsedContent = content
return content
}
// 尝试解析为数组
//var arrayContent []map[string]interface{}
@@ -682,7 +694,7 @@ func (m *Message) ParseContent() []MediaContent {
}
var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil {
if err := kitutil.Unmarshal(m.Content, &stringContent); err == nil {
m.parsedStringContent = &stringContent
return stringContent
}
@@ -707,14 +719,14 @@ func (m *Message) SetNullContent() {
}
func (m *Message) SetStringContent(content string) {
jsonContent, _ := json.Marshal(content)
jsonContent, _ := kitutil.Marshal(content)
m.Content = jsonContent
m.parsedStringContent = &content
m.parsedContent = nil
}
func (m *Message) SetMediaContent(content []MediaContent) {
jsonContent, _ := json.Marshal(content)
jsonContent, _ := kitutil.Marshal(content)
m.Content = jsonContent
m.parsedContent = nil
m.parsedStringContent = nil
@@ -725,7 +737,7 @@ func (m *Message) IsStringContent() bool {
return true
}
var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil {
if err := kitutil.Unmarshal(m.Content, &stringContent); err == nil {
m.parsedStringContent = &stringContent
return true
}
@@ -741,7 +753,7 @@ func (m *Message) ParseContent() []MediaContent {
// 先尝试解析为字符串
var stringContent string
if err := json.Unmarshal(m.Content, &stringContent); err == nil {
if err := kitutil.Unmarshal(m.Content, &stringContent); err == nil {
contentList = []MediaContent{{
Type: ContentTypeText,
Text: stringContent,
@@ -752,7 +764,7 @@ func (m *Message) ParseContent() []MediaContent {
// 尝试解析为数组
var arrayContent []map[string]interface{}
if err := json.Unmarshal(m.Content, &arrayContent); err == nil {
if err := kitutil.Unmarshal(m.Content, &arrayContent); err == nil {
for _, contentItem := range arrayContent {
contentType, ok := contentItem["type"].(string)
if !ok {
@@ -907,6 +919,9 @@ type OpenAIResponsesRequest struct {
ThinkingBudget json.RawMessage `json:"thinking_budget,omitempty"`
// perplexity
Preset json.RawMessage `json:"preset,omitempty"`
// Internal conversion state; never serialized to an upstream protocol.
ReasoningConversion *ReasoningConversionState `json:"-"`
}
func (r OpenAIResponsesRequest) MarshalJSON() ([]byte, error) {