|
| 1 | +package scheduler |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "fmt" |
| 7 | + "reflect" |
| 8 | + "sync" |
| 9 | + |
| 10 | + "github.com/tiny-systems/module/internal/scheduler/runner" |
| 11 | + "github.com/tiny-systems/module/module" |
| 12 | +) |
| 13 | + |
| 14 | +// handleRPC dispatches an MCP tool call directly to a component |
| 15 | +// without touching the flow graph. msg.To carries the bare component |
| 16 | +// name (no flow prefix, no port suffix); msg.Data is the JSON payload |
| 17 | +// the agent supplied. The dispatcher: |
| 18 | +// |
| 19 | +// 1. Looks up the component in the local registry, |
| 20 | +// 2. Confirms it implements AgentTool, |
| 21 | +// 3. Creates a fresh Instance() — no settings, no metadata, no |
| 22 | +// persisted state. The agent's payload is everything, |
| 23 | +// 4. Unmarshals msg.Data into the input port's struct type, |
| 24 | +// 5. Calls Handle() with a capture handler that records the first |
| 25 | +// emit on the configured output port, |
| 26 | +// 6. Returns the captured payload to the JetStream reply path. |
| 27 | +// |
| 28 | +// Stateful components don't opt into AgentTool and so never reach |
| 29 | +// this path. Anything that does opt in must work from a cold instance |
| 30 | +// with only the call payload. |
| 31 | +func (s *Schedule) handleRPC(ctx context.Context, msg *runner.Msg) (any, error) { |
| 32 | + componentName := msg.To |
| 33 | + if componentName == "" { |
| 34 | + return nil, fmt.Errorf("rpc: msg.To is empty (need component name)") |
| 35 | + } |
| 36 | + |
| 37 | + cmp, ok := s.componentsMap.Get(componentName) |
| 38 | + if !ok { |
| 39 | + return nil, fmt.Errorf("rpc: component %q not registered on this module", componentName) |
| 40 | + } |
| 41 | + |
| 42 | + agentTool, ok := cmp.(module.AgentTool) |
| 43 | + if !ok { |
| 44 | + return nil, fmt.Errorf("rpc: component %q does not opt into AgentTool", componentName) |
| 45 | + } |
| 46 | + info := agentTool.AgentTool() |
| 47 | + inputPort := info.InputPort |
| 48 | + if inputPort == "" { |
| 49 | + inputPort = "in" |
| 50 | + } |
| 51 | + outputPort := info.OutputPort |
| 52 | + if outputPort == "" { |
| 53 | + outputPort = "out" |
| 54 | + } |
| 55 | + |
| 56 | + instance := cmp.Instance() |
| 57 | + if instance == nil { |
| 58 | + return nil, fmt.Errorf("rpc: %s.Instance() returned nil", componentName) |
| 59 | + } |
| 60 | + |
| 61 | + inputType, err := portConfigurationType(instance, inputPort) |
| 62 | + if err != nil { |
| 63 | + return nil, fmt.Errorf("rpc: %s: %w", componentName, err) |
| 64 | + } |
| 65 | + input := reflect.New(inputType).Elem() |
| 66 | + if len(msg.Data) > 0 { |
| 67 | + if err := json.Unmarshal(msg.Data, input.Addr().Interface()); err != nil { |
| 68 | + return nil, fmt.Errorf("rpc: unmarshal %s input: %w", componentName, err) |
| 69 | + } |
| 70 | + } |
| 71 | + |
| 72 | + capture := &rpcCapture{port: outputPort} |
| 73 | + result := instance.Handle(ctx, capture.handler, inputPort, input.Interface()) |
| 74 | + if err := result.Err(); err != nil { |
| 75 | + return nil, err |
| 76 | + } |
| 77 | + |
| 78 | + // Prefer the emit on the configured output port; fall back to |
| 79 | + // the Handle Result's value when the component returned its |
| 80 | + // response directly (some authors do this for one-shot tools). |
| 81 | + if capture.captured.Load() { |
| 82 | + return capture.payload, nil |
| 83 | + } |
| 84 | + return result.Value(), nil |
| 85 | +} |
| 86 | + |
| 87 | +// portConfigurationType returns the reflect.Type of the named port's |
| 88 | +// Configuration struct on the component instance, so the dispatcher |
| 89 | +// can build a typed value to JSON-unmarshal the agent's payload into. |
| 90 | +func portConfigurationType(instance module.Component, portName string) (reflect.Type, error) { |
| 91 | + for _, p := range instance.Ports() { |
| 92 | + if p.Name != portName { |
| 93 | + continue |
| 94 | + } |
| 95 | + if p.Configuration == nil { |
| 96 | + return nil, fmt.Errorf("port %q has no Configuration shape", portName) |
| 97 | + } |
| 98 | + return reflect.TypeOf(p.Configuration), nil |
| 99 | + } |
| 100 | + return nil, fmt.Errorf("port %q not found on component", portName) |
| 101 | +} |
| 102 | + |
| 103 | +// rpcCapture wraps a module.Handler that records the first emit on |
| 104 | +// the configured output port. Subsequent emits return Ok(nil) and are |
| 105 | +// otherwise discarded — RPC mode is single-shot by design. |
| 106 | +type rpcCapture struct { |
| 107 | + port string |
| 108 | + |
| 109 | + mu sync.Mutex |
| 110 | + captured atomicBool |
| 111 | + payload any |
| 112 | +} |
| 113 | + |
| 114 | +func (c *rpcCapture) handler(ctx context.Context, port string, data any) module.Result { |
| 115 | + if port != c.port { |
| 116 | + return module.Ok(nil) |
| 117 | + } |
| 118 | + c.mu.Lock() |
| 119 | + if !c.captured.Load() { |
| 120 | + c.payload = data |
| 121 | + c.captured.Store(true) |
| 122 | + } |
| 123 | + c.mu.Unlock() |
| 124 | + return module.Ok(nil) |
| 125 | +} |
| 126 | + |
| 127 | +// atomicBool keeps the tiny capture-state machine self-contained and |
| 128 | +// import-free at the file level. |
| 129 | +type atomicBool struct{ v bool } |
| 130 | + |
| 131 | +func (a *atomicBool) Load() bool { return a.v } |
| 132 | +func (a *atomicBool) Store(b bool) { a.v = b } |
0 commit comments