132 lines
3.3 KiB
Go
132 lines
3.3 KiB
Go
//go:build windows
|
|
|
|
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/control"
|
|
)
|
|
|
|
func pluginConnect(args []string) error {
|
|
if len(args) != 2 || strings.TrimSpace(args[0]) == "" || strings.TrimSpace(args[1]) == "" {
|
|
return errors.New("usage: reims-go plugin connect <plugin-id> <channel>")
|
|
}
|
|
pluginID, channel := strings.TrimSpace(args[0]), strings.TrimSpace(args[1])
|
|
ctx, cancel := signal.NotifyContext(context.Background(), os.Interrupt, syscall.SIGTERM)
|
|
defer cancel()
|
|
conn, err := control.Dial(ctx)
|
|
if err != nil {
|
|
if startErr := spawnWindow([]string{"--background"}); startErr != nil {
|
|
return fmt.Errorf("connect to Reims-Go desktop: %w", err)
|
|
}
|
|
conn, err = waitForPluginControl(ctx)
|
|
if err != nil {
|
|
return fmt.Errorf("connect to Reims-Go desktop: %w", err)
|
|
}
|
|
}
|
|
defer conn.Close()
|
|
go func() {
|
|
<-ctx.Done()
|
|
_ = conn.Close()
|
|
}()
|
|
|
|
encoder := json.NewEncoder(conn)
|
|
stdinDone := make(chan error, 1)
|
|
go func() {
|
|
defer conn.Close()
|
|
scanner := bufio.NewScanner(os.Stdin)
|
|
scanner.Buffer(make([]byte, 64*1024), control.MaxFrameBytes)
|
|
for scanner.Scan() {
|
|
message := append(json.RawMessage(nil), scanner.Bytes()...)
|
|
if !json.Valid(message) {
|
|
stdinDone <- errors.New("MCP input line is not valid JSON")
|
|
return
|
|
}
|
|
if err := encoder.Encode(control.Request{PluginID: pluginID, Channel: channel, Message: message}); err != nil {
|
|
stdinDone <- err
|
|
return
|
|
}
|
|
}
|
|
stdinDone <- scanner.Err()
|
|
_ = conn.Close()
|
|
}()
|
|
|
|
responseScanner := bufio.NewScanner(conn)
|
|
responseScanner.Buffer(make([]byte, 64*1024), control.MaxFrameBytes)
|
|
for responseScanner.Scan() {
|
|
var response control.Response
|
|
if err := json.Unmarshal(responseScanner.Bytes(), &response); err != nil {
|
|
return fmt.Errorf("decode host control response: %w", err)
|
|
}
|
|
if response.Error != "" {
|
|
return errors.New(response.Error)
|
|
}
|
|
if len(response.Message) > 0 && string(response.Message) != "null" {
|
|
if _, err := os.Stdout.Write(response.Message); err != nil {
|
|
return err
|
|
}
|
|
if _, err := os.Stdout.Write([]byte{'\n'}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if err := responseScanner.Err(); err != nil {
|
|
// Closing stdin closes the named pipe from the writer goroutine. The
|
|
// response reader can observe that close just before stdinDone is
|
|
// scheduled, so give the writer a short chance to report a clean EOF.
|
|
stdinTimer := time.NewTimer(250 * time.Millisecond)
|
|
defer stdinTimer.Stop()
|
|
select {
|
|
case stdinErr := <-stdinDone:
|
|
if stdinErr != nil && !errors.Is(stdinErr, context.Canceled) {
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
return stdinErr
|
|
}
|
|
return nil
|
|
case <-stdinTimer.C:
|
|
case <-ctx.Done():
|
|
return nil
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func waitForPluginControl(ctx context.Context) (net.Conn, error) {
|
|
deadline := time.NewTimer(15 * time.Second)
|
|
defer deadline.Stop()
|
|
ticker := time.NewTicker(150 * time.Millisecond)
|
|
defer ticker.Stop()
|
|
var lastErr error
|
|
for {
|
|
conn, err := control.Dial(ctx)
|
|
if err == nil {
|
|
return conn, nil
|
|
}
|
|
lastErr = err
|
|
select {
|
|
case <-ctx.Done():
|
|
return nil, ctx.Err()
|
|
case <-deadline.C:
|
|
return nil, lastErr
|
|
case <-ticker.C:
|
|
}
|
|
}
|
|
}
|