mirror of
https://github.com/Wei-Shaw/sub2api.git
synced 2026-12-05 08:05:12 +00:00
fix(apicompat): tolerate non-string action.url and skip unfinished web_search calls
WebSearchAction.URL was a strict string, so a web_search_call whose action.url was not a string failed decoding of the whole Responses event and the Messages stream lost its terminal event. It is now raw like Sources and read as a string only for open_page. The non-streaming converter also filled results for web_search_call items that had not completed, while the streaming one only handles completed items. Both now emit blocks for completed calls only.
This commit is contained in:
1 parent
aa5a020bd1
commit
30152d1b16
4 files changed
+36
-10
No files matched your search
@@ -55,6 +55,9 @@ func ResponsesToAnthropic(resp *ResponsesResponse, model string) *AnthropicRespo
|
||||
Input: sanitizeAnthropicToolUseInput(item.Name, item.Arguments),
|
||||
})
|
||||
case "web_search_call":
|
||||
if item.Status != "completed" {
|
||||
continue
|
||||
}
|
||||
toolUseID := "srvtoolu_" + item.ID
|
||||
query := ""
|
||||
if item.Action != nil {
|
||||
|
||||
@@ -27,9 +27,9 @@ type webSearchResult struct {
|
||||
}
|
||||
|
||||
// webSearchCallResults is the web_search_tool_result content for one call: a
|
||||
// search's action.sources, or the single page a completed open_page opened.
|
||||
// Sources are decoded entry by entry, so one malformed entry cannot fail the
|
||||
// terminal event that carries the call.
|
||||
// search's action.sources, or the single page an open_page opened. Callers
|
||||
// pass completed calls only. Sources and the page URL are decoded leniently, so
|
||||
// a malformed value cannot fail the terminal event that carries the call.
|
||||
func webSearchCallResults(item *ResponsesOutput) json.RawMessage {
|
||||
results := []webSearchResult{}
|
||||
if action := item.Action; action != nil {
|
||||
@@ -42,8 +42,11 @@ func webSearchCallResults(item *ResponsesOutput) json.RawMessage {
|
||||
results = append(results, newWebSearchResult(rawURL))
|
||||
}
|
||||
}
|
||||
} else if action.Type == "open_page" && item.Status == "completed" && action.URL != "" {
|
||||
results = append(results, newWebSearchResult(action.URL))
|
||||
} else if action.Type == "open_page" {
|
||||
var pageURL string
|
||||
if json.Unmarshal(action.URL, &pageURL) == nil && pageURL != "" {
|
||||
results = append(results, newWebSearchResult(pageURL))
|
||||
}
|
||||
}
|
||||
}
|
||||
out, _ := json.Marshal(results)
|
||||
|
||||
@@ -100,11 +100,31 @@ func TestWebSearchOpenPageReturnsSinglePage(t *testing.T) {
|
||||
|
||||
func TestWebSearchOpenPageRequiresCompletedURL(t *testing.T) {
|
||||
for name, item := range map[string]string{
|
||||
"in progress": `{"type":"web_search_call","id":"ws_1","status":"in_progress","action":{"type":"open_page","url":"https://docs.example/page"}}`,
|
||||
"empty url": `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":""}}`,
|
||||
"empty url": `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":""}}`,
|
||||
} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
require.JSONEq(t, `[]`, nonStreamingWebSearchResults(t, item)["srvtoolu_ws_1"])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSearchNonStringActionURLKeepsEvent(t *testing.T) {
|
||||
for name, url := range map[string]string{"number": `123`, "object": `{"a":1}`, "array": `["a"]`} {
|
||||
t.Run(name, func(t *testing.T) {
|
||||
raw := `{"type":"web_search_call","id":"ws_1","status":"completed","action":{"type":"open_page","url":` + url + `}}`
|
||||
got := nonStreamingWebSearchResults(t, raw)
|
||||
require.JSONEq(t, `[]`, got["srvtoolu_ws_1"])
|
||||
require.JSONEq(t, `[]`, streamingWebSearchResults(t, raw)["srvtoolu_ws_1"])
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestWebSearchCallsThatDidNotCompleteProduceNoBlocks(t *testing.T) {
|
||||
for _, status := range []string{"failed", "in_progress"} {
|
||||
t.Run(status, func(t *testing.T) {
|
||||
raw := `{"type":"web_search_call","id":"ws_1","status":"` + status + `","action":{"type":"search","query":"q","sources":[{"url":"https://a.example/1"}]}}`
|
||||
require.Empty(t, nonStreamingWebSearchResults(t, raw))
|
||||
require.Empty(t, streamingWebSearchResults(t, raw))
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -492,9 +492,9 @@ func (o *ResponsesOutput) UnmarshalJSON(data []byte) error {
|
||||
type WebSearchAction struct {
|
||||
Type string `json:"type,omitempty"` // "search"
|
||||
Query string `json:"query,omitempty"` // primary search query
|
||||
URL string `json:"url,omitempty"` // open_page / find_in_page target
|
||||
// Sources stays raw: a strictly typed entry of an unexpected shape would
|
||||
// fail decoding the whole terminal event that carries this call.
|
||||
// URL and Sources stay raw: a strictly typed value of an unexpected shape
|
||||
// would fail decoding the whole terminal event that carries this call.
|
||||
URL json.RawMessage `json:"url,omitempty"` // open_page / find_in_page target
|
||||
Sources json.RawMessage `json:"sources,omitempty"`
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user