Files
MyGoNavi/main.go
Syngnat 5d4989f68f feat(ai): 增加 MCP HTTP 服务与 Docker 配置诊断
- AI 设置页新增 GoNavi MCP HTTP 服务开关与状态展示
- 后端新增 HTTP MCP 子进程生命周期管理和鉴权配置
- 增加 Docker MCP 配置诊断工具与参数提示校验
2026-06-11 18:27:13 +08:00

135 lines
3.4 KiB
Go
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
package main
import (
"context"
"fmt"
"os"
"strings"
aiservice "GoNavi-Wails/internal/ai/service"
"GoNavi-Wails/internal/app"
"GoNavi-Wails/internal/logger"
"GoNavi-Wails/internal/mcpserver"
"github.com/wailsapp/wails/v2"
"github.com/wailsapp/wails/v2/pkg/options"
"github.com/wailsapp/wails/v2/pkg/options/assetserver"
"github.com/wailsapp/wails/v2/pkg/options/mac"
"github.com/wailsapp/wails/v2/pkg/options/windows"
)
func main() {
if runSpecialMode(os.Args[1:]) {
return
}
// Create an instance of the app structure
application := app.NewApp()
aiService := aiservice.NewService()
lowMemoryMode := isLowMemoryMode()
backgroundColour := &options.RGBA{R: 0, G: 0, B: 0, A: 0}
windowsBackdrop := windows.Acrylic
if lowMemoryMode {
backgroundColour = &options.RGBA{R: 255, G: 255, B: 255, A: 255}
windowsBackdrop = windows.None
}
// Create application with options
err := wails.Run(&options.App{
Title: "GoNavi",
Width: 1024,
Height: 768,
MinWidth: 900,
MinHeight: 600,
Frameless: true,
AssetServer: &assetserver.Options{
Assets: assets,
},
BackgroundColour: backgroundColour,
OnStartup: func(ctx context.Context) {
app.InitializeLifecycle(application, ctx)
aiservice.InitializeLifecycle(aiService, ctx)
},
OnShutdown: func(ctx context.Context) {
aiService.Shutdown(ctx)
application.Shutdown(ctx)
},
Bind: []interface{}{
application,
aiService,
},
Windows: &windows.Options{
WebviewIsTransparent: !lowMemoryMode,
WindowIsTranslucent: !lowMemoryMode,
BackdropType: windowsBackdrop,
DisableWindowIcon: false,
DisableFramelessWindowDecorations: false,
WebviewUserDataPath: resolveWindowsWebviewUserDataPath(),
},
Mac: &mac.Options{
WebviewIsTransparent: true,
WindowIsTranslucent: true,
},
})
if err != nil {
logger.Error(err, "应用启动失败")
}
}
func runSpecialMode(args []string) bool {
if !shouldRunMCPServerMode(args) {
return false
}
if err := runMCPServerMode(context.Background(), args[1:]); err != nil {
logger.Error(err, "GoNavi MCP Server 退出")
}
return true
}
func runMCPServerMode(ctx context.Context, args []string) error {
if len(args) == 0 {
return mcpserver.RunAppStdioServer(ctx)
}
mode := strings.ToLower(strings.TrimSpace(args[0]))
switch mode {
case "stdio", "--stdio":
return mcpserver.RunAppStdioServer(ctx)
case "http", "--http", "streamable-http", "--streamable-http":
options, err := mcpserver.ParseHTTPServerOptions(args[1:])
if err != nil {
return err
}
logger.Infof("GoNavi MCP Streamable HTTP Server 启动addr=%s path=%s schemaOnly=%v", options.Addr, options.Path, options.SchemaOnly)
return mcpserver.RunAppStreamableHTTPServer(ctx, options)
case "remote-config", "--remote-config":
return mcpserver.WriteRemoteMCPClientConfig(os.Stdout, args[1:])
default:
return fmt.Errorf("未知 MCP server 模式: %s支持 stdio/http/remote-config", args[0])
}
}
func shouldRunMCPServerMode(args []string) bool {
if len(args) == 0 {
return false
}
switch strings.ToLower(strings.TrimSpace(args[0])) {
case "mcp-server", "--mcp-server":
return true
default:
return false
}
}
func isLowMemoryMode() bool {
switch strings.ToLower(strings.TrimSpace(os.Getenv("GONAVI_LOW_MEMORY_MODE"))) {
case "1", "true", "yes", "on":
return true
default:
return false
}
}