140 lines
4.0 KiB
Go
140 lines
4.0 KiB
Go
package server
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"reflect"
|
|
"slices"
|
|
"strings"
|
|
"time"
|
|
|
|
"git.a-wan.net/alan/pipelines/internal/chattools"
|
|
"git.a-wan.net/alan/pipelines/internal/protocol"
|
|
)
|
|
|
|
// openCodeCodeTool is OpenCode V2's code-mode tool: the model calls MCP tools
|
|
// from a script (`tools.pipelines.show_file({...})`) instead of as tool parts
|
|
// of their own, so the stream shows one running "execute" part.
|
|
const openCodeCodeTool = "execute"
|
|
|
|
// awaitToolCallSession attributes a call on a shared harness endpoint (one
|
|
// OpenCode service serves every chat, and its MCP requests carry no session)
|
|
// to the chat whose retained stream shows this call running: a tool part with
|
|
// the tool's harness name and exactly these arguments, or a running code-mode
|
|
// script that calls the tool. Several scripts are told apart by the argument
|
|
// values they contain. The harness reports the part as running before or while
|
|
// it calls the tool, so this waits up to wait for it. It returns "" when no
|
|
// chat matches, and ambiguous when several distinct chats still do: a call is
|
|
// never guessed into a chat.
|
|
func (h *chatHub) awaitToolCallSession(ctx context.Context, tool string, arguments json.RawMessage, wait time.Duration) (string, bool) {
|
|
var want map[string]any
|
|
if json.Unmarshal(arguments, &want) != nil {
|
|
return "", false
|
|
}
|
|
deadline := time.Now().Add(wait)
|
|
for {
|
|
sessions := h.runningToolCallSessions(tool, want)
|
|
switch {
|
|
case len(sessions) == 1:
|
|
return sessions[0], false
|
|
case len(sessions) > 1:
|
|
return "", true
|
|
}
|
|
if time.Now().After(deadline) {
|
|
return "", false
|
|
}
|
|
select {
|
|
case <-ctx.Done():
|
|
return "", false
|
|
case <-time.After(50 * time.Millisecond):
|
|
}
|
|
}
|
|
}
|
|
|
|
func (h *chatHub) runningToolCallSessions(tool string, want map[string]any) []string {
|
|
names := chattools.HarnessToolNames(tool)
|
|
h.mu.Lock()
|
|
defer h.mu.Unlock()
|
|
var exact, scripts, narrowed []string
|
|
for sessionID, turn := range h.turns {
|
|
match := turnToolCallMatch(turn, names, tool, want)
|
|
if match == toolCallExact {
|
|
exact = append(exact, sessionID)
|
|
}
|
|
if match >= toolCallScript {
|
|
scripts = append(scripts, sessionID)
|
|
}
|
|
if match >= toolCallScriptWithArguments {
|
|
narrowed = append(narrowed, sessionID)
|
|
}
|
|
}
|
|
for _, candidates := range [][]string{exact, scripts, narrowed} {
|
|
if len(candidates) == 1 {
|
|
return candidates
|
|
}
|
|
}
|
|
slices.Sort(scripts)
|
|
return scripts
|
|
}
|
|
|
|
type toolCallMatch int
|
|
|
|
const (
|
|
toolCallNone toolCallMatch = iota
|
|
toolCallScript
|
|
toolCallScriptWithArguments
|
|
toolCallExact
|
|
)
|
|
|
|
// turnToolCallMatch is the strongest evidence one chat's running tool parts
|
|
// give that it is making this call.
|
|
func turnToolCallMatch(turn *chatTurn, names []string, tool string, want map[string]any) toolCallMatch {
|
|
best := toolCallNone
|
|
for _, message := range turn.messages {
|
|
for _, part := range message.parts {
|
|
p := part.part
|
|
if p.Type != "tool" || p.State == nil || (p.State.Status != protocol.ChatToolRunning && p.State.Status != "pending") {
|
|
continue
|
|
}
|
|
if slices.Contains(names, p.Name) {
|
|
var got map[string]any
|
|
if json.Unmarshal(p.State.Input, &got) == nil && reflect.DeepEqual(got, want) {
|
|
return toolCallExact
|
|
}
|
|
continue
|
|
}
|
|
if p.Name != openCodeCodeTool {
|
|
continue
|
|
}
|
|
var script struct {
|
|
Code string `json:"code"`
|
|
}
|
|
if json.Unmarshal(p.State.Input, &script) != nil || !strings.Contains(script.Code, chattools.ServerName+"."+tool+"(") {
|
|
continue
|
|
}
|
|
match := toolCallScript
|
|
if scriptHasArguments(script.Code, want) {
|
|
match = toolCallScriptWithArguments
|
|
}
|
|
best = max(best, match)
|
|
}
|
|
}
|
|
return best
|
|
}
|
|
|
|
// scriptHasArguments reports whether every string argument appears in code,
|
|
// literally or JSON-escaped.
|
|
func scriptHasArguments(code string, want map[string]any) bool {
|
|
for _, value := range want {
|
|
text, ok := value.(string)
|
|
if !ok || text == "" {
|
|
continue
|
|
}
|
|
escaped, _ := json.Marshal(text)
|
|
if !strings.Contains(code, text) && !strings.Contains(code, strings.Trim(string(escaped), `"`)) {
|
|
return false
|
|
}
|
|
}
|
|
return true
|
|
}
|