Pi 1.2.0 with supportsMidConvoToolAdditions sends a tool it adds
mid-conversation as {"role":"system","tools":[...]} with no content and
keeps top-level tools as they were. parseChat read only top-level tools
and the Chat relay sent the message on as it came, so the tool could be
called on no route, and the tools-only message became an empty part of
the system text ("</cwd>\n\n\n\nUpdated ...").
liftMessageTools (message_tools.go) moves message tools into the
request's tools. handle does it for every Chat body, before serveAgent,
so the Chat relay, schema fixers, redaction, the prompt shown and
routing all see them; parseChat does it too for a body that never came
through handle. Each tool joins once by name and the first definition
wins (top-level, then the earliest message's), as liftAdditionalTools
and a tool_addition in withoutDeferredTools do. A system or developer
message that carried only tools is left out; one with content keeps
its place without tools. Message order is unchanged: the in-place
system message / inline-tools beta part of #2322 is not in this commit.
6312af989 (#2338, inline-only tool_addition joins tools) is not on
origin/main a1f7b8c13, so this builds on neither; the rule matches it.
Tests, from deephbz's literal Pi 1.2.0 request (last comment on #2322):
TestPiAddedToolReachesEveryRoute (Chat relayed to an OpenAI-compatible
upstream, Messages, Responses, Gemini, Codex), TestPiAddedToolOffered-
ToClaudeCode (the Claude subscription bridge's MCP tools.json, a fake
claude; skipped on Windows), TestMessageToolsFirstDefinitionWins.
Run (sandbox HOME, -tags nogui -p 1):
- go test ./internal/gateway -run 'TestPiAddedTool|TestMessageTools|
Chat|Messages|AdditionalTools|Lite|Deferred|ExtraBody|Gemini|
CodexAccessPrograms|Developer|ThinkingEffort|Kimi|DeepSeek|ClaudeRun|
ClaudeSubscription|Strict|Prompt' -count=1: ok (75.8s)
- broken by hand: liftMessageTools a no-op -> every route subtest, the
bridge test and FirstDefinitionWins red (tools [lookup_pantry], empty
system relayed, "\n\n\n\n" in the system text); only handle's lift
removed -> chat relayed red; only parseChat's lift removed -> the
bridge test and FirstDefinitionWins red.
- gofmt -l internal/gateway: clean; go vet -tags nogui ./internal/gateway
for GOOS=darwin, linux, windows: clean.
Not run: other packages, the full suite. Not tried with a real Pi or a
real upstream.
Full suite: SUITE_LINE
99 lines
2.8 KiB
Go
99 lines
2.8 KiB
Go
package gateway
|
|
|
|
import (
|
|
"bytes"
|
|
"encoding/json"
|
|
|
|
"github.com/tidwall/gjson"
|
|
)
|
|
|
|
// Pi with supportsMidConvoToolAdditions on gives a tool it adds during a
|
|
// conversation in a message of its own, not in the request's tools, which
|
|
// it keeps as they were at the start (#2322, Pi 1.2.0):
|
|
//
|
|
// {"role":"system","tools":[{"type":"function","function":{…}}]}
|
|
//
|
|
// No Chat Completions API reads tools there (OpenAI turns a system message
|
|
// with no content away), and parseChat read only the request's own tools,
|
|
// so the model could never call the tool on any route. Such tools join the
|
|
// request's tools as Codex's additional_tools do (liftAdditionalTools).
|
|
|
|
// liftMessageTools is a Chat request with the tools its messages carry
|
|
// moved into its own tools, each once by name: a tool the request already
|
|
// has, or an earlier message gave, keeps that first definition, as a
|
|
// tool_addition's does (withoutDeferredTools). A system or developer
|
|
// message that carried only tools is left out, rather than sent on as an
|
|
// empty system message; one with content keeps its place without its
|
|
// tools. A request with no message tools, or one that doesn't parse, is
|
|
// as it came.
|
|
func liftMessageTools(body []byte) []byte {
|
|
if !bytes.Contains(body, []byte(`"tools"`)) {
|
|
return body
|
|
}
|
|
if len(gjson.GetBytes(body, "messages.#.tools").Array()) == 0 {
|
|
return body
|
|
}
|
|
var v struct {
|
|
Messages []json.RawMessage `json:"messages"`
|
|
Tools []json.RawMessage `json:"tools"`
|
|
}
|
|
if json.Unmarshal(body, &v) != nil {
|
|
return body
|
|
}
|
|
have := map[string]bool{}
|
|
for _, t := range v.Tools {
|
|
have[chatToolKey(gjson.ParseBytes(t))] = true
|
|
}
|
|
tools := v.Tools
|
|
msgs := make([]json.RawMessage, 0, len(v.Messages))
|
|
for _, raw := range v.Messages {
|
|
m := gjson.ParseBytes(raw)
|
|
more := m.Get("tools")
|
|
if !more.Exists() {
|
|
msgs = append(msgs, raw)
|
|
continue
|
|
}
|
|
for _, t := range more.Array() {
|
|
if !t.IsObject() {
|
|
continue
|
|
}
|
|
if k := chatToolKey(t); !have[k] {
|
|
have[k] = true
|
|
tools = append(tools, json.RawMessage(t.Raw))
|
|
}
|
|
}
|
|
role := m.Get("role").String()
|
|
if (role == "system" || role == "developer") && emptyContent(m.Get("content")) {
|
|
continue
|
|
}
|
|
var mm map[string]json.RawMessage
|
|
if json.Unmarshal(raw, &mm) != nil {
|
|
msgs = append(msgs, raw)
|
|
continue
|
|
}
|
|
delete(mm, "tools")
|
|
out, err := marshalPlain(mm)
|
|
if err != nil {
|
|
return body
|
|
}
|
|
msgs = append(msgs, out)
|
|
}
|
|
fields := map[string]any{"messages": msgs}
|
|
if len(tools) > 0 {
|
|
fields["tools"] = tools
|
|
}
|
|
return withFields(body, fields)
|
|
}
|
|
|
|
// chatToolKey is a Chat tool's name, a hosted tool's without one by its
|
|
// type.
|
|
func chatToolKey(t gjson.Result) string {
|
|
if n := t.Get("function.name").String(); n != "" {
|
|
return n
|
|
}
|
|
if n := t.Get("name").String(); n != "" {
|
|
return n
|
|
}
|
|
return "#" + t.Get("type").String()
|
|
}
|