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>
52 lines
1.3 KiB
Go
52 lines
1.3 KiB
Go
// Package mock implements a deterministic Backend for tests, demos,
|
|
// and CI smoke checks.
|
|
//
|
|
// Config keys:
|
|
// - reply: response template. `%s` is replaced with the concatenated
|
|
// inbound text parts. Default: "echo: %s".
|
|
//
|
|
// Registers itself as "mock". Activate via:
|
|
//
|
|
// molecule connect <id> --backend mock --backend-opt reply="pong"
|
|
package mock
|
|
|
|
import (
|
|
"context"
|
|
"strings"
|
|
|
|
"github.com/Molecule-AI/molecule-cli/internal/backends"
|
|
)
|
|
|
|
func init() {
|
|
backends.Register("mock", New)
|
|
}
|
|
|
|
// New builds a mock backend from cfg.
|
|
func New(cfg backends.Config) (backends.Backend, error) {
|
|
return &Backend{
|
|
template: cfg.Get("reply", "echo: %s"),
|
|
}, nil
|
|
}
|
|
|
|
// Backend is the mock implementation. Pure function: no state, no I/O,
|
|
// safe for concurrent use without locks.
|
|
type Backend struct {
|
|
template string
|
|
}
|
|
|
|
// HandleA2A renders the reply template against the request's text
|
|
// parts and returns it as a single-part terminal response.
|
|
func (b *Backend) HandleA2A(_ context.Context, req backends.Request) (backends.Response, error) {
|
|
var sb strings.Builder
|
|
for _, p := range req.Parts {
|
|
if p.Type == "text" {
|
|
sb.WriteString(p.Text)
|
|
}
|
|
}
|
|
reply := strings.ReplaceAll(b.template, "%s", sb.String())
|
|
return backends.TextResponse(reply), nil
|
|
}
|
|
|
|
// Close is a no-op — nothing to release.
|
|
func (b *Backend) Close() error { return nil }
|