This commit is contained in:
2026-02-15 15:55:49 +08:00
commit 23decb8687
32 changed files with 3822 additions and 0 deletions

88
backend/README.md Normal file
View File

@@ -0,0 +1,88 @@
# Monaco Go Completion Backend
Gin + gopls(JSON-RPC/LSP over stdio) 实现的 Go 代码补全后端。
## 运行
```bash
go mod tidy
go run ./cmd/server
```
默认监听 `http://localhost:8080`
## 环境变量
- `PORT`HTTP 端口,默认 `8080`
- `GOPLS_PATH``gopls` 可执行文件路径,默认 `gopls`
- `WORKSPACE_DIR`gopls 工作目录,默认当前目录
- `CORS_ALLOW_ORIGIN`CORS 允许来源,默认 `*`
## API
### 健康检查
- `GET /health`
### Go 补全
- `POST /api/v1/completions/go`
- `Content-Type: application/json`
### Go 补全WebSocket
- `GET /ws/completions/go`
- 客户端发送:
```json
{
"id": "1",
"uri": "file:///main.go",
"text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n fmt.Pri\n}",
"line": 5,
"character": 11
}
```
- 服务端返回:
```json
{
"id": "1",
"items": [{ "label": "Println" }],
"isIncomplete": false
}
```
请求体:
```json
{
"uri": "file:///C:/Users/meowr/Desktop/bishe/monica_editor_with_code_completion/backend/playground.go",
"text": "package main\n\nimport \"fmt\"\n\nfunc main() {\n fmt.Pri\n}",
"line": 5,
"character": 11
}
```
说明:
- `uri` 建议使用绝对 `file://` URI`WORKSPACE_DIR` 在同一工作区最稳定)。
- 也支持 `file:///main.go` 这类相对根路径 URI后端会自动映射到 `WORKSPACE_DIR` 下。
- `line` / `character` 为 0-based。
- 每次请求都要传前端当前全文 `text`
响应体:
```json
{
"items": [
{
"label": "Println",
"kind": 3,
"detail": "func(a ...any) (n int, err error)",
"documentation": "Println formats using the default formats..."
}
],
"isIncomplete": false
}
```