First step toward `molecule connect <id>` — the out-of-box external- runtime workspace connector specified in RFC #10. What's in this PR (foundational, ~300 LOC of code + matching tests): - `internal/backends.Backend` — the seam every concrete handler implements: HandleA2A(ctx, Request) → Response, Close(). Two methods, no inheritance, no surprise side effects. Concurrency-safe by contract (poll dispatch may parallelise). - Request/Response/Part/Config types — lossless JSON-RPC mirror so backends can re-issue downstream without re-parsing. - Compile-time registry — `Register("name", factory)` from each backend's init(); `Build(name, cfg)` selects at runtime. Panics on duplicate registration so drift fails loudly at startup, not on first message. - `mock` backend — single-template echo for CI smoke + tests + demos. `--backend-opt reply="<template>"` with `%s` for inbound text. - `molecule connect <workspace-id>` cobra command — flag surface, validation, --dry-run for smoke. Loops (heartbeat, activity poll, dispatch) land in M1.2 in internal/connect/. Coverage: - Registry: duplicate-name panic, empty-name panic, nil-factory panic, Build unknown-name error includes registered list. - Mock: default template, custom template, text-part concatenation, Final=true on terminal response. - Connect: --backend-opt KEY=VALUE parser (incl. value with =), flag validation (missing token, bad mode, bad opt, unknown backend), --dry-run happy path. All tests pass under -race. Out of scope (subsequent M1 PRs): - M1.2: heartbeat + activity poll loops in internal/connect/ - M1.3: claude-code backend (wraps molecule-mcp-claude-channel) - M1.4: GoReleaser tag-triggered release.yml workflow RFC: https://github.com/Molecule-AI/molecule-cli/issues/10 Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
145 lines
5.3 KiB
Go
145 lines
5.3 KiB
Go
package cmd
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
|
|
"github.com/Molecule-AI/molecule-cli/internal/backends"
|
|
_ "github.com/Molecule-AI/molecule-cli/internal/backends/mock" // register backend
|
|
"github.com/spf13/cobra"
|
|
)
|
|
|
|
// ---------------------------------------------------------------------------
|
|
// molecule connect — bridge an external-runtime workspace to a local backend.
|
|
//
|
|
// The full M1+ design lives in the RFC at
|
|
// https://github.com/Molecule-AI/molecule-cli/issues/10. This file owns the
|
|
// command surface; the wiring (heartbeat, activity poll, dispatch) lands in
|
|
// internal/connect/ in subsequent PRs.
|
|
// ---------------------------------------------------------------------------
|
|
|
|
var connectFlags struct {
|
|
backend string
|
|
backendOpts []string // KEY=VALUE pairs, repeatable
|
|
token string
|
|
mode string // "poll" (default) | "push"
|
|
intervalMs int // poll cadence, milliseconds
|
|
sinceSecs int // initial activity-cursor lookback
|
|
dryRun bool // build backend + print summary, do not start loops
|
|
}
|
|
|
|
var connectCmd = &cobra.Command{
|
|
Use: "connect <workspace-id>",
|
|
Short: "Bridge an external workspace to a local backend (Claude Code, exec, mock, ...)",
|
|
Long: `connect attaches the calling process to an external-runtime workspace.
|
|
|
|
Inbound A2A messages routed to the workspace are dispatched to the
|
|
selected --backend. The default backend is "claude-code" (bridges into
|
|
a running Claude Code session via the channel plugin); use "mock" for
|
|
CI smoke and "exec" for arbitrary shell handlers.
|
|
|
|
Authentication: the per-workspace token from the create response is
|
|
read from --token or the MOLECULE_WORKSPACE_TOKEN env var. The
|
|
platform URL is read from --api-url or MOLECULE_API_URL.
|
|
|
|
Mode: poll (default) is the no-public-URL path — the CLI long-polls
|
|
the platform for inbound activity. push requires the local box to be
|
|
reachable from the platform (public HTTPS); use only when running on
|
|
a server with an inbound URL.
|
|
|
|
Examples:
|
|
|
|
# Default: bridge into a running Claude Code session
|
|
molecule connect ws_01HF2K... --token $WS_TOKEN
|
|
|
|
# CI smoke / demo — replies are deterministic
|
|
molecule connect ws_01HF2K... --backend mock \
|
|
--backend-opt reply="echo: %s"
|
|
|
|
# Arbitrary shell handler
|
|
molecule connect ws_01HF2K... --backend exec \
|
|
--backend-opt cmd="python myhandler.py"
|
|
|
|
See full design: https://github.com/Molecule-AI/molecule-cli/issues/10`,
|
|
Args: cobra.ExactArgs(1),
|
|
RunE: runConnect,
|
|
}
|
|
|
|
func init() {
|
|
connectCmd.Flags().StringVar(&connectFlags.backend, "backend", "claude-code",
|
|
fmt.Sprintf("Backend that handles inbound A2A messages (registered: %s)",
|
|
strings.Join(backends.Names(), ", ")))
|
|
connectCmd.Flags().StringArrayVar(&connectFlags.backendOpts, "backend-opt", nil,
|
|
"Backend-specific option, KEY=VALUE (repeatable)")
|
|
connectCmd.Flags().StringVar(&connectFlags.token, "token",
|
|
envOr("MOLECULE_WORKSPACE_TOKEN", ""),
|
|
"Workspace auth token (env: MOLECULE_WORKSPACE_TOKEN)")
|
|
connectCmd.Flags().StringVar(&connectFlags.mode, "mode", "poll",
|
|
"Delivery mode: poll (no public URL needed) | push")
|
|
connectCmd.Flags().IntVar(&connectFlags.intervalMs, "interval-ms", 1000,
|
|
"Poll-mode interval between activity fetches, in milliseconds")
|
|
connectCmd.Flags().IntVar(&connectFlags.sinceSecs, "since-secs", 30,
|
|
"Poll-mode initial cursor lookback, in seconds")
|
|
connectCmd.Flags().BoolVar(&connectFlags.dryRun, "dry-run", false,
|
|
"Build the backend and print the connection summary, but do not start loops")
|
|
}
|
|
|
|
func runConnect(_ *cobra.Command, args []string) error {
|
|
workspaceID := strings.TrimSpace(args[0])
|
|
if workspaceID == "" {
|
|
return &exitError{code: 2, msg: "connect: workspace-id is required"}
|
|
}
|
|
if connectFlags.token == "" {
|
|
return &exitError{code: 2, msg: "connect: --token (or MOLECULE_WORKSPACE_TOKEN) is required"}
|
|
}
|
|
if connectFlags.mode != "poll" && connectFlags.mode != "push" {
|
|
return &exitError{code: 2, msg: "connect: --mode must be poll or push"}
|
|
}
|
|
|
|
cfg, err := parseBackendOpts(connectFlags.backendOpts)
|
|
if err != nil {
|
|
return &exitError{code: 2, msg: err.Error()}
|
|
}
|
|
|
|
backend, err := backends.Build(connectFlags.backend, cfg)
|
|
if err != nil {
|
|
return &exitError{code: 2, msg: err.Error()}
|
|
}
|
|
|
|
fmt.Fprintf(os.Stderr, "molecule connect: workspace=%s backend=%s mode=%s api=%s\n",
|
|
workspaceID, connectFlags.backend, connectFlags.mode, apiURL)
|
|
|
|
if connectFlags.dryRun {
|
|
fmt.Fprintln(os.Stderr, "molecule connect: --dry-run; backend built ok, not starting loops")
|
|
return backend.Close()
|
|
}
|
|
|
|
// Loops (heartbeat + activity poll + dispatch) land in internal/connect
|
|
// in PR M1.2. For M1.1 we wire signal handling so the command exits
|
|
// cleanly when invoked in --dry-run by tests, and so future loops
|
|
// inherit context cancellation.
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer cancel()
|
|
|
|
<-ctx.Done()
|
|
fmt.Fprintln(os.Stderr, "molecule connect: shutting down")
|
|
return backend.Close()
|
|
}
|
|
|
|
// parseBackendOpts converts repeated KEY=VALUE flags into a Config map.
|
|
func parseBackendOpts(opts []string) (backends.Config, error) {
|
|
cfg := backends.Config{}
|
|
for _, opt := range opts {
|
|
k, v, ok := strings.Cut(opt, "=")
|
|
if !ok || k == "" {
|
|
return nil, fmt.Errorf("--backend-opt: %q is not KEY=VALUE", opt)
|
|
}
|
|
cfg[k] = v
|
|
}
|
|
return cfg, nil
|
|
}
|