mirror of
https://github.com/jasonxu114514/opencode2api.git
synced 2026-12-05 08:02:53 +00:00
fix: filter phantom non-stream tool blocks
This commit is contained in:
1 parent
278b0bb8e2
commit
82e88f2b7f
1 file changed
+29
-7
@@ -3,6 +3,7 @@ package protocol
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"opencode2api/internal/identity"
|
||||
@@ -143,13 +144,17 @@ func encodeBridgeResponse(protocol Protocol, response bridgeResponse) map[string
|
||||
if response.ID == "" {
|
||||
response.ID = identity.RandomID("resp", 12)
|
||||
}
|
||||
if isToolStop(response.Stop) && len(response.Tools) == 0 {
|
||||
// The turn ended as a tool call upstream, but no usable tool block
|
||||
// was ever assembled (missing name or arguments). A tool_use stop
|
||||
// reason with zero tool_use blocks makes strict clients end the
|
||||
// turn running nothing and reporting no error. Demote to a plain
|
||||
// stop so the client treats it as text end-of-turn and continues.
|
||||
response.Stop = "stop"
|
||||
if isToolStop(response.Stop) {
|
||||
response.Tools = usableToolBlocks(response.Tools)
|
||||
if len(response.Tools) == 0 {
|
||||
// The turn ended as a tool call upstream, but no usable tool block
|
||||
// was ever assembled (for example, the name was missing). A
|
||||
// tool_use stop reason with no usable tool blocks makes strict
|
||||
// clients end the turn running nothing and reporting no error.
|
||||
// Demote to a plain stop so the client treats it as text
|
||||
// end-of-turn and continues.
|
||||
response.Stop = "stop"
|
||||
}
|
||||
}
|
||||
switch protocol {
|
||||
case Chat:
|
||||
@@ -394,6 +399,23 @@ func isToolStop(stop string) bool {
|
||||
}
|
||||
}
|
||||
|
||||
// usableToolBlocks drops phantom tool blocks that cannot be executed by a
|
||||
// downstream client. Empty arguments are valid, so the tool name is the
|
||||
// minimum required signal here; the streaming emitter uses the same rule.
|
||||
func usableToolBlocks(tools []bridgeBlock) []bridgeBlock {
|
||||
if len(tools) == 0 {
|
||||
return nil
|
||||
}
|
||||
usable := tools[:0]
|
||||
for _, tool := range tools {
|
||||
if strings.TrimSpace(tool.Name) == "" {
|
||||
continue
|
||||
}
|
||||
usable = append(usable, tool)
|
||||
}
|
||||
return usable
|
||||
}
|
||||
|
||||
func canonicalResponsesIncomplete(reason string) string {
|
||||
switch reason {
|
||||
case "content_filter":
|
||||
|
||||
Reference in new issue
Block a user