mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-25 07:00:48 +09:00
9ceabf68c4
Replace the host bind-mount + containerd exec approach with a per-bot
in-container gRPC server (ContainerService, port 9090). All file I/O,
exec, and MCP stdio sessions now go through gRPC instead of running
shell commands or reading host-mounted directories.
Architecture changes:
- cmd/mcp: rewritten as a gRPC server (ContainerService) with full
file and exec API (ReadFile, WriteFile, ListDir, ReadRaw, WriteRaw,
Exec, Stat, Mkdir, Rename, DeleteFile)
- internal/mcp/mcpcontainer: protobuf definitions and generated stubs
- internal/mcp/mcpclient: gRPC client wrapper with connection pool
(Pool) and Provider interface for dependency injection
- mcp.Manager: add per-bot IP cache, gRPC connection pool, and
SetContainerIP/MCPClient methods; remove DataDir/Exec helpers
- containerd.Service: remove ExecTask/ExecTaskStreaming; network setup
now returns NetworkResult{IP} for pool routing
- internal/fs/service.go: deleted (replaced by mcpclient)
- handlers/fs.go: deleted; MCP stdio session logic moved to mcp_stdio.go
- container provider Executor: all tools (read/write/list/edit/exec)
now call gRPC client instead of running shell via exec
- storefs, containerfs, media, skills, memory: all I/O ported to
mcpclient.Provider
Database:
- migration 0022: drop host_path column from containers table
One-time data migration:
- migrateBindMountData: on first Start() after upgrade, copies old
bind-mount data into the container via gRPC, then renames src dir
to prevent re-migration; runs in background goroutine
Bug fixes:
- mcp_stdio: callRaw now returns full JSON-RPC envelope
{"jsonrpc","id","result"|"error"} matching protocol spec;
explicit "initialize" call now advances session init state to
prevent duplicate handshake on next non-initialize call
- mcpclient Pool: properly evict stale gRPC connection after snapshot
replace (container process recreated); use SetContainerIP instead
of direct map write so IP changes always evict pool entry
- migrateBindMountData: walkErr on directories now counted as failure
so partially-walked trees don't get incorrectly marked as migrated
- cmd/mcp/Dockerfile: removed dead file (docker/Dockerfile.mcp is the
canonical production build)
Tests:
- provider_test.go: restored with bufconn in-process gRPC mock
(fakeContainerService + staticProvider), 14 cases covering all 5
tools plus edge cases
- mcp_session_test.go: new, covers JSON-RPC envelope, init state
machine, pending cleanup on cancel/close, readLoop cancel
- storefs/service_test.go: restored (pure function roundtrip tests)
182 lines
4.7 KiB
Go
182 lines
4.7 KiB
Go
// Code generated by sqlc. DO NOT EDIT.
|
|
// versions:
|
|
// sqlc v1.30.0
|
|
// source: containers.sql
|
|
|
|
package sqlc
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/jackc/pgx/v5/pgtype"
|
|
)
|
|
|
|
const deleteContainerByBotID = `-- name: DeleteContainerByBotID :exec
|
|
DELETE FROM containers WHERE bot_id = $1
|
|
`
|
|
|
|
func (q *Queries) DeleteContainerByBotID(ctx context.Context, botID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, deleteContainerByBotID, botID)
|
|
return err
|
|
}
|
|
|
|
const getContainerByBotID = `-- name: GetContainerByBotID :one
|
|
SELECT id, bot_id, container_id, container_name, image, status, namespace, auto_start, container_path, created_at, updated_at, last_started_at, last_stopped_at FROM containers WHERE bot_id = $1 ORDER BY updated_at DESC LIMIT 1
|
|
`
|
|
|
|
func (q *Queries) GetContainerByBotID(ctx context.Context, botID pgtype.UUID) (Container, error) {
|
|
row := q.db.QueryRow(ctx, getContainerByBotID, botID)
|
|
var i Container
|
|
err := row.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.ContainerID,
|
|
&i.ContainerName,
|
|
&i.Image,
|
|
&i.Status,
|
|
&i.Namespace,
|
|
&i.AutoStart,
|
|
&i.ContainerPath,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.LastStartedAt,
|
|
&i.LastStoppedAt,
|
|
)
|
|
return i, err
|
|
}
|
|
|
|
const listAutoStartContainers = `-- name: ListAutoStartContainers :many
|
|
SELECT id, bot_id, container_id, container_name, image, status, namespace, auto_start, container_path, created_at, updated_at, last_started_at, last_stopped_at FROM containers WHERE auto_start = true ORDER BY updated_at DESC
|
|
`
|
|
|
|
func (q *Queries) ListAutoStartContainers(ctx context.Context) ([]Container, error) {
|
|
rows, err := q.db.Query(ctx, listAutoStartContainers)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
defer rows.Close()
|
|
var items []Container
|
|
for rows.Next() {
|
|
var i Container
|
|
if err := rows.Scan(
|
|
&i.ID,
|
|
&i.BotID,
|
|
&i.ContainerID,
|
|
&i.ContainerName,
|
|
&i.Image,
|
|
&i.Status,
|
|
&i.Namespace,
|
|
&i.AutoStart,
|
|
&i.ContainerPath,
|
|
&i.CreatedAt,
|
|
&i.UpdatedAt,
|
|
&i.LastStartedAt,
|
|
&i.LastStoppedAt,
|
|
); err != nil {
|
|
return nil, err
|
|
}
|
|
items = append(items, i)
|
|
}
|
|
if err := rows.Err(); err != nil {
|
|
return nil, err
|
|
}
|
|
return items, nil
|
|
}
|
|
|
|
const updateContainerStarted = `-- name: UpdateContainerStarted :exec
|
|
UPDATE containers
|
|
SET status = 'running', last_started_at = now(), updated_at = now()
|
|
WHERE bot_id = $1
|
|
`
|
|
|
|
func (q *Queries) UpdateContainerStarted(ctx context.Context, botID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, updateContainerStarted, botID)
|
|
return err
|
|
}
|
|
|
|
const updateContainerStatus = `-- name: UpdateContainerStatus :exec
|
|
UPDATE containers
|
|
SET status = $1, updated_at = now()
|
|
WHERE bot_id = $2
|
|
`
|
|
|
|
type UpdateContainerStatusParams struct {
|
|
Status string `json:"status"`
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
}
|
|
|
|
func (q *Queries) UpdateContainerStatus(ctx context.Context, arg UpdateContainerStatusParams) error {
|
|
_, err := q.db.Exec(ctx, updateContainerStatus, arg.Status, arg.BotID)
|
|
return err
|
|
}
|
|
|
|
const updateContainerStopped = `-- name: UpdateContainerStopped :exec
|
|
UPDATE containers
|
|
SET status = 'stopped', last_stopped_at = now(), updated_at = now()
|
|
WHERE bot_id = $1
|
|
`
|
|
|
|
func (q *Queries) UpdateContainerStopped(ctx context.Context, botID pgtype.UUID) error {
|
|
_, err := q.db.Exec(ctx, updateContainerStopped, botID)
|
|
return err
|
|
}
|
|
|
|
const upsertContainer = `-- name: UpsertContainer :exec
|
|
INSERT INTO containers (
|
|
bot_id, container_id, container_name, image, status, namespace, auto_start,
|
|
container_path, last_started_at, last_stopped_at
|
|
)
|
|
VALUES (
|
|
$1,
|
|
$2,
|
|
$3,
|
|
$4,
|
|
$5,
|
|
$6,
|
|
$7,
|
|
$8,
|
|
$9,
|
|
$10
|
|
)
|
|
ON CONFLICT (container_id) DO UPDATE SET
|
|
bot_id = EXCLUDED.bot_id,
|
|
container_name = EXCLUDED.container_name,
|
|
image = EXCLUDED.image,
|
|
status = EXCLUDED.status,
|
|
namespace = EXCLUDED.namespace,
|
|
auto_start = EXCLUDED.auto_start,
|
|
container_path = EXCLUDED.container_path,
|
|
last_started_at = EXCLUDED.last_started_at,
|
|
last_stopped_at = EXCLUDED.last_stopped_at,
|
|
updated_at = now()
|
|
`
|
|
|
|
type UpsertContainerParams struct {
|
|
BotID pgtype.UUID `json:"bot_id"`
|
|
ContainerID string `json:"container_id"`
|
|
ContainerName string `json:"container_name"`
|
|
Image string `json:"image"`
|
|
Status string `json:"status"`
|
|
Namespace string `json:"namespace"`
|
|
AutoStart bool `json:"auto_start"`
|
|
ContainerPath string `json:"container_path"`
|
|
LastStartedAt pgtype.Timestamptz `json:"last_started_at"`
|
|
LastStoppedAt pgtype.Timestamptz `json:"last_stopped_at"`
|
|
}
|
|
|
|
func (q *Queries) UpsertContainer(ctx context.Context, arg UpsertContainerParams) error {
|
|
_, err := q.db.Exec(ctx, upsertContainer,
|
|
arg.BotID,
|
|
arg.ContainerID,
|
|
arg.ContainerName,
|
|
arg.Image,
|
|
arg.Status,
|
|
arg.Namespace,
|
|
arg.AutoStart,
|
|
arg.ContainerPath,
|
|
arg.LastStartedAt,
|
|
arg.LastStoppedAt,
|
|
)
|
|
return err
|
|
}
|