Taken over from the join's 473f9323, 5f40d230, 159a679b; the events are the seat's pull-updated, pull-merged, pull-closed and forge-delete. Builds: design 47 G §252, §310, §312, §317 Claude-Session: https://claude.ai/code/session_01JG73KEq4zoDfM9WtYpcQgZ
95 lines
3.7 KiB
Go
95 lines
3.7 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"strings"
|
|
"time"
|
|
|
|
"github.com/nats-io/nats.go"
|
|
|
|
"mesh-delivery-forge-bridge/internal/bridge"
|
|
)
|
|
|
|
// callTimeout bounds a call to a tool (mesh-sdk go/process/call.go CallTimeout).
|
|
const callTimeout = 30 * time.Second
|
|
|
|
// caller calls a module's tool by its manifest key (`<module>.<tool>`) with a JSON body and returns the tool's result.
|
|
type caller func(ctx context.Context, key string, body any) (json.RawMessage, error)
|
|
|
|
// callerOn calls tools and seat verbs as the bridge's own principal. It copies mesh-sdk go/process/call.go (Call, callSubjects,
|
|
// request; hand-written here until the SDK's helper is used, issue 1242): the request is the tool's arguments as
|
|
// JSON, published on the caller-named subject `mesh.mod.<module>.call.<tool>.<user, dots as ~>`, or on the subject that
|
|
// names no caller, `mesh.mod.<module>.tool.<tool>`, when nothing answers the first; the reply is
|
|
// {"result", "error", "node"} and a non-empty error is the call's failure (mesh-tools node-tools/internal/bus/bus.go
|
|
// answerOf). The grant to publish on these subjects comes from the manifest's invokes alone (mesh-control
|
|
// internal/broker/nats.go invokedSubjects).
|
|
func callerOn(nc *nats.Conn, user string) caller {
|
|
token := strings.ReplaceAll(user, ".", "~")
|
|
request := func(ctx context.Context, subject string, data []byte) (json.RawMessage, error) {
|
|
ctx, cancel := context.WithTimeout(ctx, callTimeout)
|
|
defer cancel()
|
|
msg, err := nc.RequestWithContext(ctx, subject, data)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
var r struct {
|
|
Result json.RawMessage `json:"result"`
|
|
Error string `json:"error"`
|
|
Node string `json:"node"`
|
|
}
|
|
if err := json.Unmarshal(msg.Data, &r); err != nil {
|
|
return nil, fmt.Errorf("%s answered something that is not a tool's answer: %w", subject, err)
|
|
}
|
|
if r.Error != "" {
|
|
return nil, errors.New(r.Error)
|
|
}
|
|
return r.Result, nil
|
|
}
|
|
return func(ctx context.Context, key string, body any) (json.RawMessage, error) {
|
|
called, fallback, err := callSubjects(key, token)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
data, err := json.Marshal(body)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
result, err := request(ctx, called, data)
|
|
if errors.Is(err, nats.ErrNoResponders) {
|
|
result, err = request(ctx, fallback, data)
|
|
}
|
|
return result, err
|
|
}
|
|
}
|
|
|
|
// callSubjects is the caller-named subject a key is called on and its plain fallback: a seat's verb
|
|
// (`seat:<seat>.<verb>`) on mesh.seat.<seat>.call.<verb>.<token>, else a module's tool (`<module>.<tool>`) on
|
|
// mesh.mod.<module>.call.<tool>.<token> (mesh-sdk go/process/call.go callSubjectsOn).
|
|
func callSubjects(key, token string) (called, fallback string, err error) {
|
|
if rest, isSeat := strings.CutPrefix(key, "seat:"); isSeat {
|
|
seat, verb, ok := strings.Cut(rest, ".")
|
|
if !ok || seat == "" || verb == "" {
|
|
return "", "", fmt.Errorf("%q is not seat:<seat>.<verb>", key)
|
|
}
|
|
return "mesh.seat." + seat + ".call." + verb + "." + token, "mesh.seat." + seat + ".tool." + verb, nil
|
|
}
|
|
module, tool, ok := strings.Cut(key, ".")
|
|
if !ok || module == "" || tool == "" {
|
|
return "", "", fmt.Errorf("%q is not <module>.<tool>", key)
|
|
}
|
|
return "mesh.mod." + module + ".call." + tool + "." + token, "mesh.mod." + module + ".tool." + tool, nil
|
|
}
|
|
|
|
// treesVia asks the forge for a commit's trees through call.
|
|
func treesVia(ctx context.Context, call caller) bridge.Trees {
|
|
return bridge.TreesVia(ctx, bridge.Caller(call))
|
|
}
|
|
|
|
// filesVia asks the forge for a file through call. A file the forge says is not there is bridge.ErrNotFound.
|
|
func filesVia(ctx context.Context, call caller) bridge.Files {
|
|
return bridge.FilesVia(ctx, bridge.Caller(call))
|
|
}
|