add multimodal memory proxy and API logging

This commit is contained in:
2026-06-12 11:04:53 +08:00
parent 8afb460883
commit a29009dc07
12 changed files with 2229 additions and 33 deletions

View File

@ -1,6 +1,6 @@
# Memory Gateway 2
# Memory Gateway
Memory Gateway 2 是一个轻量级 FastAPI 服务,用于在 EverOS 现有
Memory Gateway 是一个轻量级 FastAPI 服务,用于在 EverOS 现有
`/api/v1/memory/add``/api/v1/memory/flush``/api/v1/memory/search`
能力之上构建用户资源记忆层。
@ -23,7 +23,7 @@ Memory Gateway 2 是一个轻量级 FastAPI 服务,用于在 EverOS 现有
## 目录结构
```text
/home/tom/memory-gateway2
/home/tom/memory-gateway
├── core/ # Gateway 核心代码
│ ├── api.py # FastAPI 路由
│ ├── config.py # 环境变量配置
@ -42,7 +42,7 @@ Memory Gateway 2 是一个轻量级 FastAPI 服务,用于在 EverOS 现有
复制示例配置:
```bash
cd /home/tom/memory-gateway2
cd /home/tom/memory-gateway
cp .env.example .env
```
@ -50,7 +50,7 @@ cp .env.example .env
| 变量 | 默认值 | 说明 |
|---|---|---|
| `EVEROS_BASE_URL` | `http://127.0.0.1:8000` | EverOS API 服务地址 |
| `EVEROS_BASE_URL` | `http://127.0.0.1:1995` | EverOS API 服务地址EverOS 可监听 `0.0.0.0:1995`,本机客户端通常连接 `127.0.0.1:1995` |
| `MEMORY_GATEWAY_DB_PATH` | `./data/memory_gateway.sqlite3` | Gateway 自己的 SQLite 数据库路径 |
| `MEMORY_GATEWAY_STORAGE_DIR` | `./data/storage` | 用户上传原始文件保存路径 |
| `MEMORY_GATEWAY_RESOURCE_SEARCH_BATCH_SIZE` | `50` | resources scope 搜索时每批 session_id 数量 |
@ -69,7 +69,7 @@ cp .env.example .env
## 安装依赖
```bash
cd /home/tom/memory-gateway2
cd /home/tom/memory-gateway
uv pip install -e .
```
@ -78,7 +78,7 @@ uv pip install -e .
使用 Python 启动:
```bash
cd /home/tom/memory-gateway2
cd /home/tom/memory-gateway
python main.py
```
@ -113,6 +113,16 @@ resource:{user_id}:{resource_id}
- `memory_tombstones`:用户删除的 memory id 或 session_id。
- `memory_overrides`:用户手动修正后的 memory 文本。
## API 日志
Gateway 会通过 `memory_gateway.api` logger 为每个 API 请求输出一条 JSON 日志,字段包括:
- `request_time`:请求进入 Gateway 的 UTC 时间。
- `duration_ms`:接口处理耗时。
- `method``path``url``client`:请求方法、地址和客户端地址。
- `input`query 参数和请求体。`user_key`、token、password、secret、API key 等敏感字段会记录为 `[REDACTED]`multipart 上传只记录 content type 和大小,不记录文件内容。
- `output`HTTP 状态码和响应体;敏感字段同样会遮蔽。
## API 使用说明
`POST /users` 外,所有业务 API 都需要携带 `user_id``user_key`。认证失败返回 `401`
@ -141,7 +151,7 @@ EverOS 正常时响应示例:
},
"everos": {
"status": "ok",
"base_url": "http://127.0.0.1:8000",
"base_url": "http://127.0.0.1:1995",
"data": {
"status": "ok"
}
@ -159,7 +169,7 @@ EverOS 不可访问时仍返回 HTTP 200但 `status` 会变成 `degraded`
},
"everos": {
"status": "unavailable",
"base_url": "http://127.0.0.1:8000",
"base_url": "http://127.0.0.1:1995",
"error": "Connection refused"
}
}
@ -225,9 +235,10 @@ Content-Type: multipart/form-data
4. 生成 `session_id = resource:{user_id}:{resource_id}`
5. 写入 `user_resources`,状态为 `ingesting`
6. 根据 MIME 类型映射 EverOS content type。
7. 调用 EverOS `/api/v1/memory/add`
8. 调用 EverOS `/api/v1/memory/flush`
9. 成功后状态改为 `extracted`,失败后状态改为 `failed`
7. 构造 EverOS content item文本类上传以内联 `text` 发送,非文本上传以内联 `base64` 发送,不要求 EverOS 访问 Gateway 本地 `file://` 路径
8. 调用 EverOS `/api/v1/memory/add`
9. 调用 EverOS `/api/v1/memory/flush`
10. 成功后状态改为 `extracted`,失败后状态改为 `failed`
上传策略:
@ -585,6 +596,25 @@ Gateway 内部通过 `core/everos_client.py` 调用 EverOS
## 运行测试
```bash
cd /home/tom/memory-gateway2
python -B -m pytest -q -p no:cacheprovider
cd /home/tom/memory-gateway
.venv/bin/python -B -m pytest -q -p no:cacheprovider
```
默认测试不会访问真实 EverOS。若要对已部署的 EverOS 做 health 集成验证,先确认 EverOS 正在监听 `0.0.0.0:1995`,然后从 Gateway 所在机器用客户端可访问地址访问:
```bash
cd /home/tom/memory-gateway
RUN_EVEROS_INTEGRATION=1 \
EVEROS_BASE_URL=http://10.6.80.123:1995 \
.venv/bin/python -B -m pytest -q tests/test_everos_integration.py -p no:cacheprovider
```
真实 add/flush 上传会写入 EverOS且可能受上游解析、LLM、embedding 服务耗时影响。需要验证完整摄入链路时再打开第二层开关:
```bash
cd /home/tom/memory-gateway
RUN_EVEROS_INTEGRATION=1 \
RUN_EVEROS_INGEST_INTEGRATION=1 \
EVEROS_BASE_URL=http://10.6.80.123:1995 \
.venv/bin/python -B -m pytest -q tests/test_everos_integration.py -p no:cacheprovider
```