mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
chore: optimize code structure
This commit is contained in:
@@ -0,0 +1,95 @@
|
||||
package mcp
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"fmt"
|
||||
"strings"
|
||||
"sync"
|
||||
)
|
||||
|
||||
func IsNotification(req JSONRPCRequest) bool {
|
||||
return len(req.ID) == 0 && strings.HasPrefix(req.Method, "notifications/")
|
||||
}
|
||||
|
||||
func JSONRPCErrorResponse(id json.RawMessage, code int, message string) JSONRPCResponse {
|
||||
return JSONRPCResponse{
|
||||
JSONRPC: "2.0",
|
||||
ID: id,
|
||||
Error: &JSONRPCError{Code: code, Message: message},
|
||||
}
|
||||
}
|
||||
|
||||
func BuildPayloads(req JSONRPCRequest, initOnce *sync.Once) ([]string, json.RawMessage, error) {
|
||||
if req.JSONRPC == "" {
|
||||
req.JSONRPC = "2.0"
|
||||
}
|
||||
targetID := req.ID
|
||||
payloads := []string{}
|
||||
shouldInit := req.Method != "initialize" && req.Method != "notifications/initialized"
|
||||
if initOnce != nil {
|
||||
ran := false
|
||||
initOnce.Do(func() {
|
||||
ran = true
|
||||
})
|
||||
if ran {
|
||||
// This is the first call on the session.
|
||||
} else {
|
||||
shouldInit = false
|
||||
}
|
||||
}
|
||||
if shouldInit {
|
||||
initReq := map[string]any{
|
||||
"jsonrpc": "2.0",
|
||||
"id": "init-1",
|
||||
"method": "initialize",
|
||||
"params": map[string]any{
|
||||
"protocolVersion": "2025-06-18",
|
||||
"capabilities": map[string]any{
|
||||
"roots": map[string]any{
|
||||
"listChanged": false,
|
||||
},
|
||||
},
|
||||
"clientInfo": map[string]any{
|
||||
"name": "memoh-http-proxy",
|
||||
"version": "v0",
|
||||
},
|
||||
},
|
||||
}
|
||||
initBytes, err := json.Marshal(initReq)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
payloads = append(payloads, string(initBytes))
|
||||
|
||||
initialized := map[string]any{
|
||||
"jsonrpc": "2.0",
|
||||
"method": "notifications/initialized",
|
||||
}
|
||||
initializedBytes, err := json.Marshal(initialized)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
payloads = append(payloads, string(initializedBytes))
|
||||
}
|
||||
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, nil, err
|
||||
}
|
||||
payloads = append(payloads, string(reqBytes))
|
||||
return payloads, targetID, nil
|
||||
}
|
||||
|
||||
func BuildNotificationPayloads(req JSONRPCRequest) ([]string, error) {
|
||||
if req.JSONRPC == "" {
|
||||
req.JSONRPC = "2.0"
|
||||
}
|
||||
if strings.TrimSpace(req.Method) == "" {
|
||||
return nil, fmt.Errorf("missing method")
|
||||
}
|
||||
reqBytes, err := json.Marshal(req)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
return []string{string(reqBytes)}, nil
|
||||
}
|
||||
+25
-19
@@ -88,15 +88,8 @@ func (m *Manager) EnsureBot(ctx context.Context, botID string) error {
|
||||
return err
|
||||
}
|
||||
|
||||
dataMount := m.cfg.DataMount
|
||||
if dataMount == "" {
|
||||
dataMount = config.DefaultDataMount
|
||||
}
|
||||
|
||||
image := m.cfg.BusyboxImage
|
||||
if image == "" {
|
||||
image = config.DefaultBusyboxImg
|
||||
}
|
||||
dataMount := m.dataMount()
|
||||
image := m.imageRef()
|
||||
|
||||
specOpts := []oci.SpecOpts{
|
||||
oci.WithMounts([]specs.Mount{
|
||||
@@ -235,25 +228,38 @@ func (m *Manager) DataDir(botID string) (string, error) {
|
||||
return "", err
|
||||
}
|
||||
|
||||
root := m.cfg.DataRoot
|
||||
if root == "" {
|
||||
root = config.DefaultDataRoot
|
||||
}
|
||||
return filepath.Join(root, "bots", botID), nil
|
||||
return filepath.Join(m.dataRoot(), "bots", botID), nil
|
||||
}
|
||||
|
||||
func (m *Manager) ensureBotDir(botID string) (string, error) {
|
||||
root := m.cfg.DataRoot
|
||||
if root == "" {
|
||||
root = config.DefaultDataRoot
|
||||
}
|
||||
dir := filepath.Join(root, "bots", botID)
|
||||
dir := filepath.Join(m.dataRoot(), "bots", botID)
|
||||
if err := os.MkdirAll(dir, 0o755); err != nil {
|
||||
return "", err
|
||||
}
|
||||
return dir, nil
|
||||
}
|
||||
|
||||
func (m *Manager) dataRoot() string {
|
||||
if m.cfg.DataRoot == "" {
|
||||
return config.DefaultDataRoot
|
||||
}
|
||||
return m.cfg.DataRoot
|
||||
}
|
||||
|
||||
func (m *Manager) dataMount() string {
|
||||
if m.cfg.DataMount == "" {
|
||||
return config.DefaultDataMount
|
||||
}
|
||||
return m.cfg.DataMount
|
||||
}
|
||||
|
||||
func (m *Manager) imageRef() string {
|
||||
if m.cfg.BusyboxImage == "" {
|
||||
return config.DefaultBusyboxImg
|
||||
}
|
||||
return m.cfg.BusyboxImage
|
||||
}
|
||||
|
||||
func validateBotID(botID string) error {
|
||||
return identity.ValidateUserID(botID)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user