|
| 1 | +package tools |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "errors" |
| 6 | + "fmt" |
| 7 | + "strings" |
| 8 | + "time" |
| 9 | + |
| 10 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/spec" |
| 11 | + "github.com/LAA-Software-Engineering/agentic-control-plane/internal/tools/native" |
| 12 | +) |
| 13 | + |
| 14 | +// Registry resolves workflow uses strings against declared tools and dispatches by transport (MVP: native, mock). |
| 15 | +type Registry struct { |
| 16 | + graph *spec.ProjectGraph |
| 17 | + native *native.Registry |
| 18 | + // Mock is optional; when set, ToolSpec type "mock" delegates here. Otherwise a canned JSON body is returned. |
| 19 | + Mock ToolExecutor |
| 20 | +} |
| 21 | + |
| 22 | +// NewRegistry builds a registry from the merged project graph. |
| 23 | +func NewRegistry(g *spec.ProjectGraph) *Registry { |
| 24 | + return &Registry{ |
| 25 | + graph: g, |
| 26 | + native: native.NewRegistry(), |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +// ParseUses splits tool.github.pull_request.get into tool name "github" and operation "pull_request.get". |
| 31 | +func ParseUses(uses string) (toolName string, operation string, err error) { |
| 32 | + uses = strings.TrimSpace(uses) |
| 33 | + const prefix = "tool." |
| 34 | + if !strings.HasPrefix(uses, prefix) { |
| 35 | + return "", "", fmt.Errorf("tools: uses %q must start with %q", uses, prefix) |
| 36 | + } |
| 37 | + rest := strings.TrimPrefix(uses, prefix) |
| 38 | + i := strings.IndexByte(rest, '.') |
| 39 | + if i <= 0 || i >= len(rest)-1 { |
| 40 | + return "", "", fmt.Errorf("tools: uses %q must be tool.<name>.<operation>", uses) |
| 41 | + } |
| 42 | + toolName = rest[:i] |
| 43 | + operation = rest[i+1:] |
| 44 | + if strings.TrimSpace(toolName) == "" || strings.TrimSpace(operation) == "" { |
| 45 | + return "", "", fmt.Errorf("tools: uses %q must be tool.<name>.<operation>", uses) |
| 46 | + } |
| 47 | + return toolName, operation, nil |
| 48 | +} |
| 49 | + |
| 50 | +// Call implements [ToolExecutor] by resolving Uses against the project graph. |
| 51 | +func (r *Registry) Call(ctx context.Context, req ToolCallRequest) (ToolCallResponse, error) { |
| 52 | + if r == nil { |
| 53 | + return ToolCallResponse{}, fmt.Errorf("tools: nil registry") |
| 54 | + } |
| 55 | + start := time.Now() |
| 56 | + toolName, operation, err := ParseUses(req.Uses) |
| 57 | + if err != nil { |
| 58 | + return ToolCallResponse{}, err |
| 59 | + } |
| 60 | + if r.graph == nil || r.graph.Tools == nil { |
| 61 | + return ToolCallResponse{}, fmt.Errorf("tools: unknown tool %q", toolName) |
| 62 | + } |
| 63 | + tr, ok := r.graph.Tools[toolName] |
| 64 | + if !ok || tr == nil { |
| 65 | + return ToolCallResponse{}, fmt.Errorf("tools: unknown tool %q", toolName) |
| 66 | + } |
| 67 | + typ := strings.ToLower(strings.TrimSpace(tr.Spec.Type)) |
| 68 | + switch typ { |
| 69 | + case "native": |
| 70 | + if r.native == nil { |
| 71 | + r.native = native.NewRegistry() |
| 72 | + } |
| 73 | + out, meta, err := r.native.Dispatch(ctx, operation, req.With) |
| 74 | + if err != nil { |
| 75 | + if errors.Is(err, native.ErrUnknownOperation) { |
| 76 | + return ToolCallResponse{}, &UnknownOperationError{Tool: toolName, Operation: operation} |
| 77 | + } |
| 78 | + return ToolCallResponse{}, err |
| 79 | + } |
| 80 | + return normalizeResponse(out, ToolCallMeta{DurationMs: meta.DurationMs, CostUSD: meta.CostUSD}, start), nil |
| 81 | + case "mock": |
| 82 | + if r.Mock != nil { |
| 83 | + return r.Mock.Call(ctx, req) |
| 84 | + } |
| 85 | + return normalizeResponse( |
| 86 | + map[string]any{"message": "mock", "uses": req.Uses}, |
| 87 | + ToolCallMeta{DurationMs: 1, CostUSD: 0}, |
| 88 | + start, |
| 89 | + ), nil |
| 90 | + default: |
| 91 | + return ToolCallResponse{}, fmt.Errorf("tools: tool %q type %q not supported by MVP registry (native|mock only)", toolName, tr.Spec.Type) |
| 92 | + } |
| 93 | +} |
0 commit comments