168 lines
4.0 KiB
Go
168 lines
4.0 KiB
Go
//go:build windows
|
|
|
|
package cli
|
|
|
|
import (
|
|
"bufio"
|
|
"context"
|
|
"encoding/json"
|
|
"errors"
|
|
"fmt"
|
|
"io"
|
|
"net"
|
|
"os"
|
|
"os/signal"
|
|
"strings"
|
|
"syscall"
|
|
"time"
|
|
|
|
"github.com/Nwflower/reims-go/internal/control"
|
|
)
|
|
|
|
func pluginConnect(args []string, dataRoot 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 := spawnWindowWithDataRoot([]string{"--background"}, dataRoot); 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)
|
|
}
|
|
}
|
|
return runPluginConnect(ctx, conn, os.Stdin, os.Stdout, os.Stderr, pluginID, channel)
|
|
}
|
|
|
|
func runPluginConnect(ctx context.Context, conn net.Conn, input io.Reader, output, errorOutput io.Writer, pluginID, channel string) error {
|
|
finished := make(chan struct{})
|
|
defer close(finished)
|
|
defer conn.Close()
|
|
go func() {
|
|
select {
|
|
case <-ctx.Done():
|
|
_ = conn.Close()
|
|
case <-finished:
|
|
}
|
|
}()
|
|
|
|
encoder := json.NewEncoder(conn)
|
|
stdinDone := make(chan error, 1)
|
|
go func() {
|
|
scanner := bufio.NewScanner(input)
|
|
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")
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
var rpcRequest struct {
|
|
ID json.RawMessage `json:"id"`
|
|
}
|
|
_ = json.Unmarshal(message, &rpcRequest)
|
|
if err := encoder.Encode(control.Request{PluginID: pluginID, Channel: channel, Message: message, RequestID: rpcRequest.ID}); err != nil {
|
|
stdinDone <- err
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
}
|
|
if err := scanner.Err(); err != nil {
|
|
stdinDone <- err
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
if err := encoder.Encode(control.Request{CloseInput: true}); err != nil {
|
|
stdinDone <- err
|
|
_ = conn.Close()
|
|
return
|
|
}
|
|
stdinDone <- nil
|
|
}()
|
|
|
|
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 != "" {
|
|
if len(response.RequestID) == 0 {
|
|
if _, err := fmt.Fprintln(errorOutput, "plugin connect: "+response.Error); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
frame, err := json.Marshal(map[string]any{
|
|
"jsonrpc": "2.0",
|
|
"id": response.RequestID,
|
|
"error": map[string]any{
|
|
"code": -32000,
|
|
"message": response.Error,
|
|
},
|
|
})
|
|
if err != nil {
|
|
return err
|
|
}
|
|
if _, err := output.Write(append(frame, '\n')); err != nil {
|
|
return err
|
|
}
|
|
continue
|
|
}
|
|
if len(response.Message) > 0 && string(response.Message) != "null" {
|
|
if _, err := output.Write(response.Message); err != nil {
|
|
return err
|
|
}
|
|
if _, err := output.Write([]byte{'\n'}); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
}
|
|
if err := responseScanner.Err(); err != nil {
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
return err
|
|
}
|
|
select {
|
|
case stdinErr := <-stdinDone:
|
|
if stdinErr != nil && ctx.Err() == nil {
|
|
return stdinErr
|
|
}
|
|
default:
|
|
}
|
|
if ctx.Err() != nil {
|
|
return nil
|
|
}
|
|
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:
|
|
}
|
|
}
|
|
}
|