mirror of
https://github.com/memohai/Memoh.git
synced 2026-04-27 07:16:19 +09:00
ea719f7ca7
* refactor: memory provider * fix: migrations * feat: divide collection from different built-in memory * feat: add `MEMORY.md` and `PROFILES.md` * use .env for docker compose. fix #142 (#143) * feat(web): add brand icons for search providers (#144) Add custom FontAwesome icon definitions for all 9 search providers: - Yandex: uses existing faYandex from FA free brands - Tavily, Jina, Exa, Bocha, Serper: custom icons from brand SVGs - DuckDuckGo, SearXNG, Sogou: custom icons from Simple Icons Icons are registered with a custom 'fac' prefix and rendered as monochrome (currentColor) via FontAwesome's standard rendering. * fix: resolve multiple UI bugs (#147) * feat: add email service with multi-adapter support (#146) * feat: add email service with multi-adapter support Implement a full-stack email service with global provider management, per-bot bindings with granular read/write permissions, outbox audit storage, and MCP tool integration for direct mailbox access. Backend: - Email providers: CRUD with dynamic config schema (generic SMTP/IMAP, Mailgun) - Generic adapter: go-mail (SMTP) + go-imap/v2 (IMAP IDLE real-time push via UnilateralDataHandler + UID-based tracking + periodic check fallback) - Mailgun adapter: mailgun-go/v5 with dual inbound mode (webhook + poll) - Bot email bindings: per-bot provider binding with independent r/w permissions - Outbox: outbound email audit log with status tracking - Trigger: inbound emails push notification to bot_inbox (from/subject only, LLM reads full content on demand via MCP tools) - MailboxReader interface: on-demand IMAP queries for listing/reading emails - MCP tools: email_accounts, email_send, email_list (paginated mailbox), email_read (by UID) — all with multi-binding and provider_id selection - Webhook: /email/mailgun/webhook/:config_id (JWT-skipped, signature-verified) - DB migration: 0019_add_email (email_providers, bot_email_bindings, email_outbox) Frontend: - Email Providers page: /email-providers with MasterDetailSidebarLayout - Dynamic config form rendered from ordered provider meta schema with i18n keys - Bot detail: Email tab with bindings management + outbox audit table - Sidebar navigation entry - Full i18n support (en + zh) - Auto-generated SDK from Swagger Closes #17 * feat(email): trigger bot conversation immediately on inbound email Instead of only storing an inbox item and waiting for the next chat, the email trigger now proactively invokes the conversation resolver so the bot processes new emails right away — aligned with the schedule/heartbeat trigger pattern. * fix: lint --------- Co-authored-by: Acbox <acbox0328@gmail.com> * chore: update AGENTS.md * feat: files preview * feat(web): improve MCP details page * refactor(skills): import skill with pure markdown string * merge main into refactor/memory * fix: migration * refactor: temp delete qdrant and bm25 index * fix: clean merge code * fix: update memory handler --------- Co-authored-by: Leohearts <leohearts@leohearts.com> Co-authored-by: Menci <mencici@msn.com> Co-authored-by: Quincy <69751197+dqygit@users.noreply.github.com> Co-authored-by: BBQ <35603386+HoneyBBQ@users.noreply.github.com> Co-authored-by: Ran <16112591+chen-ran@users.noreply.github.com>
297 lines
9.2 KiB
Go
297 lines
9.2 KiB
Go
package handlers
|
|
|
|
import (
|
|
"fmt"
|
|
"net/http"
|
|
"strings"
|
|
|
|
"github.com/labstack/echo/v4"
|
|
|
|
fsops "github.com/memohai/memoh/internal/fs"
|
|
)
|
|
|
|
// ---------- request / response types ----------
|
|
|
|
type FSFileInfo = fsops.FileInfo
|
|
type FSListResponse = fsops.ListResult
|
|
type FSReadResponse = fsops.ReadResult
|
|
type FSUploadResponse = fsops.UploadResult
|
|
|
|
// FSWriteRequest is the body for creating / overwriting a file.
|
|
type FSWriteRequest struct {
|
|
Path string `json:"path"`
|
|
Content string `json:"content"`
|
|
}
|
|
|
|
// FSMkdirRequest is the body for creating a directory.
|
|
type FSMkdirRequest struct {
|
|
Path string `json:"path"`
|
|
}
|
|
|
|
// FSDeleteRequest is the body for deleting a file or directory.
|
|
type FSDeleteRequest struct {
|
|
Path string `json:"path"`
|
|
Recursive bool `json:"recursive"`
|
|
}
|
|
|
|
// FSRenameRequest is the body for renaming / moving an entry.
|
|
type FSRenameRequest struct {
|
|
OldPath string `json:"oldPath"`
|
|
NewPath string `json:"newPath"`
|
|
}
|
|
|
|
type fsOpResponse struct {
|
|
OK bool `json:"ok"`
|
|
}
|
|
|
|
// ---------- handlers ----------
|
|
|
|
// FSStat godoc
|
|
// @Summary Get file or directory info
|
|
// @Description Returns metadata about a file or directory at the given container path
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param path query string true "Container path"
|
|
// @Success 200 {object} FSFileInfo
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs [get]
|
|
func (h *ContainerdHandler) FSStat(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
fi, err := h.fsService.Stat(c.Request().Context(), botID, c.QueryParam("path"))
|
|
if err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, fi)
|
|
}
|
|
|
|
// FSList godoc
|
|
// @Summary List directory contents
|
|
// @Description Lists files and directories at the given container path
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param path query string true "Container directory path"
|
|
// @Success 200 {object} FSListResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/list [get]
|
|
func (h *ContainerdHandler) FSList(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.fsService.List(c.Request().Context(), botID, c.QueryParam("path"))
|
|
if err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
// FSRead godoc
|
|
// @Summary Read file content as text
|
|
// @Description Reads the content of a file and returns it as a JSON string
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param path query string true "Container file path"
|
|
// @Success 200 {object} FSReadResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/read [get]
|
|
func (h *ContainerdHandler) FSRead(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.fsService.Read(c.Request().Context(), botID, c.QueryParam("path"))
|
|
if err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
// FSDownload godoc
|
|
// @Summary Download a file as binary stream
|
|
// @Description Downloads a file from the container with appropriate Content-Type
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param path query string true "Container file path"
|
|
// @Produce octet-stream
|
|
// @Success 200 {file} binary
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/download [get]
|
|
func (h *ContainerdHandler) FSDownload(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
resp, err := h.fsService.Download(c.Request().Context(), botID, c.QueryParam("path"))
|
|
if err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
c.Response().Header().Set("Content-Disposition", fmt.Sprintf(`attachment; filename="%s"`, resp.FileName))
|
|
if resp.FromHost {
|
|
return c.File(resp.HostPath)
|
|
}
|
|
return c.Blob(http.StatusOK, resp.ContentType, resp.Data)
|
|
}
|
|
|
|
// FSWrite godoc
|
|
// @Summary Write text content to a file
|
|
// @Description Creates or overwrites a file with the provided text content
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param payload body FSWriteRequest true "Write request"
|
|
// @Success 200 {object} fsOpResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/write [post]
|
|
func (h *ContainerdHandler) FSWrite(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req FSWriteRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if err := h.fsService.Write(botID, req.Path, req.Content); err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, fsOpResponse{OK: true})
|
|
}
|
|
|
|
// FSUpload godoc
|
|
// @Summary Upload a file via multipart form
|
|
// @Description Uploads a binary file to the given container path
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param path formData string true "Destination container path"
|
|
// @Param file formData file true "File to upload"
|
|
// @Accept multipart/form-data
|
|
// @Success 200 {object} FSUploadResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/upload [post]
|
|
func (h *ContainerdHandler) FSUpload(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
destPath := strings.TrimSpace(c.FormValue("path"))
|
|
if destPath == "" {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "path is required")
|
|
}
|
|
file, err := c.FormFile("file")
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, "file is required")
|
|
}
|
|
src, err := file.Open()
|
|
if err != nil {
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|
|
defer src.Close()
|
|
resp, err := h.fsService.Upload(botID, destPath, src)
|
|
if err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, resp)
|
|
}
|
|
|
|
// FSMkdir godoc
|
|
// @Summary Create a directory
|
|
// @Description Creates a directory (and parents) at the given container path
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param payload body FSMkdirRequest true "Mkdir request"
|
|
// @Success 200 {object} fsOpResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/mkdir [post]
|
|
func (h *ContainerdHandler) FSMkdir(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req FSMkdirRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if err := h.fsService.Mkdir(botID, req.Path); err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, fsOpResponse{OK: true})
|
|
}
|
|
|
|
// FSDelete godoc
|
|
// @Summary Delete a file or directory
|
|
// @Description Deletes a file or directory at the given container path
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param payload body FSDeleteRequest true "Delete request"
|
|
// @Success 200 {object} fsOpResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/delete [post]
|
|
func (h *ContainerdHandler) FSDelete(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req FSDeleteRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if err := h.fsService.Delete(botID, req.Path, req.Recursive); err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, fsOpResponse{OK: true})
|
|
}
|
|
|
|
// FSRename godoc
|
|
// @Summary Rename or move a file/directory
|
|
// @Description Renames or moves a file/directory from oldPath to newPath
|
|
// @Tags containerd
|
|
// @Param bot_id path string true "Bot ID"
|
|
// @Param payload body FSRenameRequest true "Rename request"
|
|
// @Success 200 {object} fsOpResponse
|
|
// @Failure 400 {object} ErrorResponse
|
|
// @Failure 403 {object} ErrorResponse
|
|
// @Failure 404 {object} ErrorResponse
|
|
// @Failure 500 {object} ErrorResponse
|
|
// @Router /bots/{bot_id}/container/fs/rename [post]
|
|
func (h *ContainerdHandler) FSRename(c echo.Context) error {
|
|
botID, err := h.requireBotAccess(c)
|
|
if err != nil {
|
|
return err
|
|
}
|
|
var req FSRenameRequest
|
|
if err := c.Bind(&req); err != nil {
|
|
return echo.NewHTTPError(http.StatusBadRequest, err.Error())
|
|
}
|
|
if err := h.fsService.Rename(botID, req.OldPath, req.NewPath); err != nil {
|
|
return h.toFSHTTPError(err)
|
|
}
|
|
return c.JSON(http.StatusOK, fsOpResponse{OK: true})
|
|
}
|
|
|
|
func (h *ContainerdHandler) toFSHTTPError(err error) error {
|
|
if fsErr, ok := fsops.AsError(err); ok {
|
|
return echo.NewHTTPError(fsErr.Code, fsErr.Message)
|
|
}
|
|
return echo.NewHTTPError(http.StatusInternalServerError, err.Error())
|
|
}
|