mirror of
https://github.com/CJackHwang/ds2api.git
synced 2026-05-04 00:15:28 +08:00
Compare commits
13 Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
4b73315df0 | ||
|
|
a086e0cfa1 | ||
|
|
f3bc022a36 | ||
|
|
b7cb7ef0c1 | ||
|
|
267420a46a | ||
|
|
3c66ab958a | ||
|
|
cf2f79b6f4 | ||
|
|
ab6e817c8e | ||
|
|
9ae4630a3b | ||
|
|
d1b8537cfb | ||
|
|
d32b4481da | ||
|
|
52a04ac575 | ||
|
|
0d3d535c08 |
@@ -10,7 +10,9 @@ __pycache__
|
|||||||
.Python
|
.Python
|
||||||
build/
|
build/
|
||||||
develop-eggs/
|
develop-eggs/
|
||||||
dist/
|
dist/*
|
||||||
|
!dist/docker-input/
|
||||||
|
!dist/docker-input/*.tar.gz
|
||||||
downloads/
|
downloads/
|
||||||
eggs/
|
eggs/
|
||||||
.eggs/
|
.eggs/
|
||||||
|
|||||||
90
.github/workflows/release-artifacts.yml
vendored
90
.github/workflows/release-artifacts.yml
vendored
@@ -4,6 +4,12 @@ on:
|
|||||||
release:
|
release:
|
||||||
types:
|
types:
|
||||||
- published
|
- published
|
||||||
|
workflow_dispatch:
|
||||||
|
inputs:
|
||||||
|
release_tag:
|
||||||
|
description: "Release tag to build/publish (e.g. v2.1.6)"
|
||||||
|
required: true
|
||||||
|
type: string
|
||||||
|
|
||||||
permissions:
|
permissions:
|
||||||
contents: write
|
contents: write
|
||||||
@@ -13,8 +19,7 @@ jobs:
|
|||||||
build-and-upload:
|
build-and-upload:
|
||||||
runs-on: ubuntu-latest
|
runs-on: ubuntu-latest
|
||||||
env:
|
env:
|
||||||
DOCKERHUB_USERNAME: ${{ secrets.DOCKERHUB_USERNAME }}
|
RELEASE_TAG: ${{ github.event.release.tag_name || github.event.inputs.release_tag }}
|
||||||
DOCKERHUB_TOKEN: ${{ secrets.DOCKERHUB_TOKEN }}
|
|
||||||
steps:
|
steps:
|
||||||
- name: Checkout
|
- name: Checkout
|
||||||
uses: actions/checkout@v4
|
uses: actions/checkout@v4
|
||||||
@@ -45,7 +50,7 @@ jobs:
|
|||||||
- name: Build Multi-Platform Archives
|
- name: Build Multi-Platform Archives
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
TAG="${{ github.event.release.tag_name }}"
|
TAG="${RELEASE_TAG}"
|
||||||
mkdir -p dist
|
mkdir -p dist
|
||||||
|
|
||||||
targets=(
|
targets=(
|
||||||
@@ -82,25 +87,44 @@ jobs:
|
|||||||
rm -rf "${STAGE}"
|
rm -rf "${STAGE}"
|
||||||
done
|
done
|
||||||
|
|
||||||
|
- name: Prepare Docker release inputs
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
TAG="${RELEASE_TAG}"
|
||||||
|
mkdir -p dist/docker-input
|
||||||
|
cp "dist/ds2api_${TAG}_linux_amd64.tar.gz" "dist/docker-input/linux_amd64.tar.gz"
|
||||||
|
cp "dist/ds2api_${TAG}_linux_arm64.tar.gz" "dist/docker-input/linux_arm64.tar.gz"
|
||||||
|
|
||||||
- name: Set up QEMU
|
- name: Set up QEMU
|
||||||
uses: docker/setup-qemu-action@v3
|
uses: docker/setup-qemu-action@v3
|
||||||
|
|
||||||
- name: Set up Docker Buildx
|
- name: Set up Docker Buildx
|
||||||
uses: docker/setup-buildx-action@v3
|
uses: docker/setup-buildx-action@v3
|
||||||
|
|
||||||
- name: Log in to GHCR
|
- name: Wait for GHCR endpoint
|
||||||
uses: docker/login-action@v3
|
run: |
|
||||||
with:
|
set -euo pipefail
|
||||||
registry: ghcr.io
|
for i in {1..6}; do
|
||||||
username: ${{ github.actor }}
|
code="$(curl -sS -o /dev/null -w '%{http_code}' --max-time 15 https://ghcr.io/v2/ || true)"
|
||||||
password: ${{ secrets.GITHUB_TOKEN }}
|
if [ "${code}" = "200" ] || [ "${code}" = "401" ] || [ "${code}" = "405" ]; then
|
||||||
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep "$((i * 10))"
|
||||||
|
done
|
||||||
|
echo "GHCR endpoint is unreachable after multiple retries (last status: ${code:-unknown})." >&2
|
||||||
|
exit 1
|
||||||
|
|
||||||
- name: Log in to Docker Hub
|
- name: Log in to GHCR (with retry)
|
||||||
if: "${{ env.DOCKERHUB_USERNAME != '' }}"
|
run: |
|
||||||
uses: docker/login-action@v3
|
set -euo pipefail
|
||||||
with:
|
for i in {1..6}; do
|
||||||
username: ${{ env.DOCKERHUB_USERNAME }}
|
if echo "${{ secrets.GITHUB_TOKEN }}" | docker login ghcr.io -u "${{ github.actor }}" --password-stdin; then
|
||||||
password: ${{ env.DOCKERHUB_TOKEN }}
|
exit 0
|
||||||
|
fi
|
||||||
|
sleep "$((i * 10))"
|
||||||
|
done
|
||||||
|
echo "Failed to login to GHCR after multiple retries." >&2
|
||||||
|
exit 1
|
||||||
|
|
||||||
- name: Extract Docker metadata
|
- name: Extract Docker metadata
|
||||||
id: meta_release
|
id: meta_release
|
||||||
@@ -108,16 +132,19 @@ jobs:
|
|||||||
with:
|
with:
|
||||||
images: |
|
images: |
|
||||||
ghcr.io/${{ github.repository }}
|
ghcr.io/${{ github.repository }}
|
||||||
${{ env.DOCKERHUB_USERNAME || 'cjackhwang' }}/ds2api
|
|
||||||
tags: |
|
tags: |
|
||||||
type=raw,value=${{ github.event.release.tag_name }}
|
type=raw,value=${{ env.RELEASE_TAG }}
|
||||||
type=raw,value=latest
|
type=raw,value=latest
|
||||||
|
|
||||||
- name: Build and Push Docker Image
|
- name: Build and Push Docker Image
|
||||||
uses: docker/build-push-action@v6
|
uses: docker/build-push-action@v6
|
||||||
|
env:
|
||||||
|
DOCKER_BUILD_RECORD_UPLOAD: "false"
|
||||||
|
DOCKER_BUILD_SUMMARY: "false"
|
||||||
with:
|
with:
|
||||||
context: .
|
context: .
|
||||||
file: ./Dockerfile
|
file: ./Dockerfile
|
||||||
|
target: runtime-from-dist
|
||||||
push: true
|
push: true
|
||||||
platforms: linux/amd64,linux/arm64
|
platforms: linux/amd64,linux/arm64
|
||||||
tags: ${{ steps.meta_release.outputs.tags }}
|
tags: ${{ steps.meta_release.outputs.tags }}
|
||||||
@@ -126,15 +153,17 @@ jobs:
|
|||||||
- name: Export Docker image archives for release assets
|
- name: Export Docker image archives for release assets
|
||||||
run: |
|
run: |
|
||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
TAG="${{ github.event.release.tag_name }}"
|
TAG="${RELEASE_TAG}"
|
||||||
|
|
||||||
docker buildx build \
|
docker buildx build \
|
||||||
--platform linux/amd64 \
|
--platform linux/amd64 \
|
||||||
|
--target runtime-from-dist \
|
||||||
--output type=docker,dest="dist/ds2api_${TAG}_docker_linux_amd64.tar" \
|
--output type=docker,dest="dist/ds2api_${TAG}_docker_linux_amd64.tar" \
|
||||||
.
|
.
|
||||||
|
|
||||||
docker buildx build \
|
docker buildx build \
|
||||||
--platform linux/arm64 \
|
--platform linux/arm64 \
|
||||||
|
--target runtime-from-dist \
|
||||||
--output type=docker,dest="dist/ds2api_${TAG}_docker_linux_arm64.tar" \
|
--output type=docker,dest="dist/ds2api_${TAG}_docker_linux_arm64.tar" \
|
||||||
.
|
.
|
||||||
|
|
||||||
@@ -146,10 +175,29 @@ jobs:
|
|||||||
set -euo pipefail
|
set -euo pipefail
|
||||||
(cd dist && sha256sum *.tar.gz *.zip > sha256sums.txt)
|
(cd dist && sha256sum *.tar.gz *.zip > sha256sums.txt)
|
||||||
|
|
||||||
|
- name: Validate release tag
|
||||||
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
TAG="${RELEASE_TAG}"
|
||||||
|
if [ -z "${TAG}" ]; then
|
||||||
|
echo "release tag is empty; set release_tag when using workflow_dispatch." >&2
|
||||||
|
exit 1
|
||||||
|
fi
|
||||||
|
|
||||||
- name: Upload Release Assets
|
- name: Upload Release Assets
|
||||||
uses: softprops/action-gh-release@v2
|
env:
|
||||||
with:
|
GH_TOKEN: ${{ github.token }}
|
||||||
files: |
|
run: |
|
||||||
|
set -euo pipefail
|
||||||
|
TAG="${RELEASE_TAG}"
|
||||||
|
FILES=(
|
||||||
dist/*.tar.gz
|
dist/*.tar.gz
|
||||||
dist/*.zip
|
dist/*.zip
|
||||||
dist/sha256sums.txt
|
dist/sha256sums.txt
|
||||||
|
)
|
||||||
|
|
||||||
|
if gh release view "${TAG}" >/dev/null 2>&1; then
|
||||||
|
gh release upload "${TAG}" "${FILES[@]}" --clobber
|
||||||
|
else
|
||||||
|
gh release create "${TAG}" "${FILES[@]}" --title "${TAG}" --notes ""
|
||||||
|
fi
|
||||||
|
|||||||
22
DEPLOY.en.md
22
DEPLOY.en.md
@@ -135,11 +135,12 @@ docker-compose up -d --build
|
|||||||
|
|
||||||
### 2.3 Docker Architecture
|
### 2.3 Docker Architecture
|
||||||
|
|
||||||
The `Dockerfile` uses a three-stage build:
|
The `Dockerfile` now provides two image paths:
|
||||||
|
|
||||||
1. **WebUI build stage**: `node:20` image, runs `npm ci && npm run build`
|
1. **Default local/dev path (`runtime-from-source`)**: a three-stage build (WebUI build + Go build + runtime).
|
||||||
2. **Go build stage**: `golang:1.24` image, compiles the binary
|
2. **Release path (`runtime-from-dist`)**: CI first creates `dist/ds2api_<tag>_linux_<arch>.tar.gz`, then Docker directly reuses the binary and `static/admin` assets from those release archives, without running `npm build`/`go build` again.
|
||||||
3. **Runtime stage**: `debian:bookworm-slim` minimal image
|
|
||||||
|
The release path keeps Docker images aligned with release archives and reduces duplicate build work.
|
||||||
|
|
||||||
Container entry command: `/usr/local/bin/ds2api`, default exposed port: `5001`.
|
Container entry command: `/usr/local/bin/ds2api`, default exposed port: `5001`.
|
||||||
|
|
||||||
@@ -160,7 +161,7 @@ Docker Compose includes a built-in health check:
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
test: ["CMD", "/usr/local/bin/busybox", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 3
|
retries: 3
|
||||||
@@ -341,6 +342,7 @@ Built-in GitHub Actions workflow: `.github/workflows/release-artifacts.yml`
|
|||||||
|
|
||||||
- **Trigger**: only on Release `published` (no build on normal push)
|
- **Trigger**: only on Release `published` (no build on normal push)
|
||||||
- **Outputs**: multi-platform binary archives + `sha256sums.txt`
|
- **Outputs**: multi-platform binary archives + `sha256sums.txt`
|
||||||
|
- **Container publishing**: GHCR only (`ghcr.io/cjackhwang/ds2api`)
|
||||||
|
|
||||||
| Platform | Architecture | Format |
|
| Platform | Architecture | Format |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
@@ -378,6 +380,16 @@ cp config.example.json config.json
|
|||||||
2. Wait for the `Release Artifacts` workflow to complete
|
2. Wait for the `Release Artifacts` workflow to complete
|
||||||
3. Download the matching archive from Release Assets
|
3. Download the matching archive from Release Assets
|
||||||
|
|
||||||
|
### Pull from GHCR (Optional)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# latest
|
||||||
|
docker pull ghcr.io/cjackhwang/ds2api:latest
|
||||||
|
|
||||||
|
# specific version (example)
|
||||||
|
docker pull ghcr.io/cjackhwang/ds2api:v2.1.2
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 5. Reverse Proxy (Nginx)
|
## 5. Reverse Proxy (Nginx)
|
||||||
|
|||||||
22
DEPLOY.md
22
DEPLOY.md
@@ -135,11 +135,12 @@ docker-compose up -d --build
|
|||||||
|
|
||||||
### 2.3 Docker 架构说明
|
### 2.3 Docker 架构说明
|
||||||
|
|
||||||
`Dockerfile` 使用三阶段构建:
|
`Dockerfile` 提供两条构建路径:
|
||||||
|
|
||||||
1. **WebUI 构建阶段**:`node:20` 镜像,执行 `npm ci && npm run build`
|
1. **本地/开发默认路径(`runtime-from-source`)**:三阶段构建(WebUI 构建 + Go 构建 + 运行阶段)。
|
||||||
2. **Go 构建阶段**:`golang:1.24` 镜像,编译二进制文件
|
2. **Release 路径(`runtime-from-dist`)**:CI 先生成 `dist/ds2api_<tag>_linux_<arch>.tar.gz`,再由 Docker 直接复用该发布包内的二进制和 `static/admin` 产物组装运行镜像,不再重复执行 `npm build`/`go build`。
|
||||||
3. **运行阶段**:`debian:bookworm-slim` 精简镜像
|
|
||||||
|
Release 路径可确保 Docker 镜像与 release 压缩包使用同一套产物,减少重复构建带来的差异。
|
||||||
|
|
||||||
容器内启动命令:`/usr/local/bin/ds2api`,默认暴露端口 `5001`。
|
容器内启动命令:`/usr/local/bin/ds2api`,默认暴露端口 `5001`。
|
||||||
|
|
||||||
@@ -160,7 +161,7 @@ Docker Compose 已配置内置健康检查:
|
|||||||
|
|
||||||
```yaml
|
```yaml
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
test: ["CMD", "/usr/local/bin/busybox", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 3
|
retries: 3
|
||||||
@@ -341,6 +342,7 @@ No Output Directory named "public" found after the Build completed.
|
|||||||
|
|
||||||
- **触发条件**:仅在 Release `published` 时触发(普通 push 不会构建)
|
- **触发条件**:仅在 Release `published` 时触发(普通 push 不会构建)
|
||||||
- **构建产物**:多平台二进制压缩包 + `sha256sums.txt`
|
- **构建产物**:多平台二进制压缩包 + `sha256sums.txt`
|
||||||
|
- **容器镜像发布**:仅发布到 GHCR(`ghcr.io/cjackhwang/ds2api`)
|
||||||
|
|
||||||
| 平台 | 架构 | 文件格式 |
|
| 平台 | 架构 | 文件格式 |
|
||||||
| --- | --- | --- |
|
| --- | --- | --- |
|
||||||
@@ -378,6 +380,16 @@ cp config.example.json config.json
|
|||||||
2. 等待 Actions 工作流 `Release Artifacts` 完成
|
2. 等待 Actions 工作流 `Release Artifacts` 完成
|
||||||
3. 在 Release 的 Assets 下载对应平台压缩包
|
3. 在 Release 的 Assets 下载对应平台压缩包
|
||||||
|
|
||||||
|
### 拉取 GHCR 镜像(可选)
|
||||||
|
|
||||||
|
```bash
|
||||||
|
# latest
|
||||||
|
docker pull ghcr.io/cjackhwang/ds2api:latest
|
||||||
|
|
||||||
|
# 指定版本(示例)
|
||||||
|
docker pull ghcr.io/cjackhwang/ds2api:v2.1.2
|
||||||
|
```
|
||||||
|
|
||||||
---
|
---
|
||||||
|
|
||||||
## 五、反向代理(Nginx)
|
## 五、反向代理(Nginx)
|
||||||
|
|||||||
40
Dockerfile
40
Dockerfile
@@ -15,12 +15,44 @@ RUN go mod download
|
|||||||
COPY . .
|
COPY . .
|
||||||
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/ds2api ./cmd/ds2api
|
RUN CGO_ENABLED=0 GOOS=${TARGETOS} GOARCH=${TARGETARCH} go build -o /out/ds2api ./cmd/ds2api
|
||||||
|
|
||||||
FROM debian:bookworm-slim
|
FROM busybox:1.36.1-musl AS busybox-tools
|
||||||
|
|
||||||
|
FROM debian:bookworm-slim AS runtime-base
|
||||||
WORKDIR /app
|
WORKDIR /app
|
||||||
RUN apt-get update && apt-get install -y --no-install-recommends ca-certificates wget && rm -rf /var/lib/apt/lists/*
|
COPY --from=go-builder /etc/ssl/certs/ca-certificates.crt /etc/ssl/certs/ca-certificates.crt
|
||||||
|
COPY --from=busybox-tools /bin/busybox /usr/local/bin/busybox
|
||||||
|
EXPOSE 5001
|
||||||
|
CMD ["/usr/local/bin/ds2api"]
|
||||||
|
|
||||||
|
FROM runtime-base AS runtime-from-source
|
||||||
COPY --from=go-builder /out/ds2api /usr/local/bin/ds2api
|
COPY --from=go-builder /out/ds2api /usr/local/bin/ds2api
|
||||||
COPY --from=go-builder /app/sha3_wasm_bg.7b9ca65ddd.wasm /app/sha3_wasm_bg.7b9ca65ddd.wasm
|
COPY --from=go-builder /app/sha3_wasm_bg.7b9ca65ddd.wasm /app/sha3_wasm_bg.7b9ca65ddd.wasm
|
||||||
COPY --from=go-builder /app/config.example.json /app/config.example.json
|
COPY --from=go-builder /app/config.example.json /app/config.example.json
|
||||||
COPY --from=webui-builder /app/static/admin /app/static/admin
|
COPY --from=webui-builder /app/static/admin /app/static/admin
|
||||||
EXPOSE 5001
|
|
||||||
CMD ["/usr/local/bin/ds2api"]
|
FROM busybox-tools AS dist-extract
|
||||||
|
ARG TARGETARCH
|
||||||
|
COPY dist/docker-input/linux_amd64.tar.gz /tmp/ds2api_linux_amd64.tar.gz
|
||||||
|
COPY dist/docker-input/linux_arm64.tar.gz /tmp/ds2api_linux_arm64.tar.gz
|
||||||
|
RUN set -eux; \
|
||||||
|
case "${TARGETARCH}" in \
|
||||||
|
amd64) ARCHIVE="/tmp/ds2api_linux_amd64.tar.gz" ;; \
|
||||||
|
arm64) ARCHIVE="/tmp/ds2api_linux_arm64.tar.gz" ;; \
|
||||||
|
*) echo "unsupported TARGETARCH: ${TARGETARCH}" >&2; exit 1 ;; \
|
||||||
|
esac; \
|
||||||
|
tar -xzf "${ARCHIVE}" -C /tmp; \
|
||||||
|
PKG_DIR="$(find /tmp -maxdepth 1 -type d -name "ds2api_*_linux_${TARGETARCH}" | head -n1)"; \
|
||||||
|
test -n "${PKG_DIR}"; \
|
||||||
|
mkdir -p /out/static; \
|
||||||
|
cp "${PKG_DIR}/ds2api" /out/ds2api; \
|
||||||
|
cp "${PKG_DIR}/sha3_wasm_bg.7b9ca65ddd.wasm" /out/sha3_wasm_bg.7b9ca65ddd.wasm; \
|
||||||
|
cp "${PKG_DIR}/config.example.json" /out/config.example.json; \
|
||||||
|
cp -R "${PKG_DIR}/static/admin" /out/static/admin
|
||||||
|
|
||||||
|
FROM runtime-base AS runtime-from-dist
|
||||||
|
COPY --from=dist-extract /out/ds2api /usr/local/bin/ds2api
|
||||||
|
COPY --from=dist-extract /out/sha3_wasm_bg.7b9ca65ddd.wasm /app/sha3_wasm_bg.7b9ca65ddd.wasm
|
||||||
|
COPY --from=dist-extract /out/config.example.json /app/config.example.json
|
||||||
|
COPY --from=dist-extract /out/static/admin /app/static/admin
|
||||||
|
|
||||||
|
FROM runtime-from-source AS final
|
||||||
|
|||||||
@@ -462,6 +462,7 @@ npm ci --prefix webui && npm run build --prefix webui
|
|||||||
|
|
||||||
- **触发条件**:仅在 GitHub Release `published` 时触发(普通 push 不会触发)
|
- **触发条件**:仅在 GitHub Release `published` 时触发(普通 push 不会触发)
|
||||||
- **构建产物**:多平台二进制包(`linux/amd64`、`linux/arm64`、`darwin/amd64`、`darwin/arm64`、`windows/amd64`)+ `sha256sums.txt`
|
- **构建产物**:多平台二进制包(`linux/amd64`、`linux/arm64`、`darwin/amd64`、`darwin/arm64`、`windows/amd64`)+ `sha256sums.txt`
|
||||||
|
- **容器镜像发布**:仅推送到 GHCR(`ghcr.io/cjackhwang/ds2api`)
|
||||||
- **每个压缩包包含**:`ds2api` 可执行文件、`static/admin`、WASM 文件、配置示例、README、LICENSE
|
- **每个压缩包包含**:`ds2api` 可执行文件、`static/admin`、WASM 文件、配置示例、README、LICENSE
|
||||||
|
|
||||||
## 免责声明
|
## 免责声明
|
||||||
|
|||||||
@@ -462,6 +462,7 @@ Workflow: `.github/workflows/release-artifacts.yml`
|
|||||||
|
|
||||||
- **Trigger**: only on GitHub Release `published` (normal pushes do not trigger builds)
|
- **Trigger**: only on GitHub Release `published` (normal pushes do not trigger builds)
|
||||||
- **Outputs**: multi-platform archives (`linux/amd64`, `linux/arm64`, `darwin/amd64`, `darwin/arm64`, `windows/amd64`) + `sha256sums.txt`
|
- **Outputs**: multi-platform archives (`linux/amd64`, `linux/arm64`, `darwin/amd64`, `darwin/arm64`, `windows/amd64`) + `sha256sums.txt`
|
||||||
|
- **Container publishing**: GHCR only (`ghcr.io/cjackhwang/ds2api`)
|
||||||
- **Each archive includes**: `ds2api` executable, `static/admin`, WASM file, config template, README, LICENSE
|
- **Each archive includes**: `ds2api` executable, `static/admin`, WASM file, config template, README, LICENSE
|
||||||
|
|
||||||
## Disclaimer
|
## Disclaimer
|
||||||
|
|||||||
@@ -11,7 +11,7 @@ services:
|
|||||||
- HOST=0.0.0.0
|
- HOST=0.0.0.0
|
||||||
restart: unless-stopped
|
restart: unless-stopped
|
||||||
healthcheck:
|
healthcheck:
|
||||||
test: ["CMD", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
test: ["CMD", "/usr/local/bin/busybox", "wget", "-qO-", "http://localhost:${PORT:-5001}/healthz"]
|
||||||
interval: 30s
|
interval: 30s
|
||||||
timeout: 10s
|
timeout: 10s
|
||||||
retries: 3
|
retries: 3
|
||||||
|
|||||||
@@ -375,7 +375,7 @@ func TestHandleStreamReasonerToolCallInterceptsWithoutRawContentLeak(t *testing.
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestHandleStreamUnknownToolNotIntercepted(t *testing.T) {
|
func TestHandleStreamUnknownToolDoesNotLeakRawPayload(t *testing.T) {
|
||||||
h := &Handler{}
|
h := &Handler{}
|
||||||
resp := makeSSEHTTPResponse(
|
resp := makeSSEHTTPResponse(
|
||||||
`data: {"p":"response/content","v":"{\"tool_calls\":[{\"name\":\"not_in_schema\",\"input\":{\"q\":\"go\"}}]}"}`,
|
`data: {"p":"response/content","v":"{\"tool_calls\":[{\"name\":\"not_in_schema\",\"input\":{\"q\":\"go\"}}]}"}`,
|
||||||
@@ -393,8 +393,34 @@ func TestHandleStreamUnknownToolNotIntercepted(t *testing.T) {
|
|||||||
if streamHasToolCallsDelta(frames) {
|
if streamHasToolCallsDelta(frames) {
|
||||||
t.Fatalf("did not expect tool_calls delta for unknown schema name, body=%s", rec.Body.String())
|
t.Fatalf("did not expect tool_calls delta for unknown schema name, body=%s", rec.Body.String())
|
||||||
}
|
}
|
||||||
if !streamHasRawToolJSONContent(frames) {
|
if streamHasRawToolJSONContent(frames) {
|
||||||
t.Fatalf("expected raw tool_calls json to remain in content for unknown schema name: %s", rec.Body.String())
|
t.Fatalf("did not expect raw tool_calls json leak for unknown schema name: %s", rec.Body.String())
|
||||||
|
}
|
||||||
|
if streamFinishReason(frames) != "stop" {
|
||||||
|
t.Fatalf("expected finish_reason=stop, body=%s", rec.Body.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestHandleStreamUnknownToolNoArgsDoesNotLeakRawPayload(t *testing.T) {
|
||||||
|
h := &Handler{}
|
||||||
|
resp := makeSSEHTTPResponse(
|
||||||
|
`data: {"p":"response/content","v":"{\"tool_calls\":[{\"name\":\"not_in_schema\"}]}"}`,
|
||||||
|
`data: [DONE]`,
|
||||||
|
)
|
||||||
|
rec := httptest.NewRecorder()
|
||||||
|
req := httptest.NewRequest(http.MethodPost, "/v1/chat/completions", nil)
|
||||||
|
|
||||||
|
h.handleStream(rec, req, resp, "cid5b", "deepseek-chat", "prompt", false, false, []string{"search"})
|
||||||
|
|
||||||
|
frames, done := parseSSEDataFrames(t, rec.Body.String())
|
||||||
|
if !done {
|
||||||
|
t.Fatalf("expected [DONE], body=%s", rec.Body.String())
|
||||||
|
}
|
||||||
|
if streamHasToolCallsDelta(frames) {
|
||||||
|
t.Fatalf("did not expect tool_calls delta for unknown schema name (no args), body=%s", rec.Body.String())
|
||||||
|
}
|
||||||
|
if streamHasRawToolJSONContent(frames) {
|
||||||
|
t.Fatalf("did not expect raw tool_calls json leak for unknown schema name (no args): %s", rec.Body.String())
|
||||||
}
|
}
|
||||||
if streamFinishReason(frames) != "stop" {
|
if streamFinishReason(frames) != "stop" {
|
||||||
t.Fatalf("expected finish_reason=stop, body=%s", rec.Body.String())
|
t.Fatalf("expected finish_reason=stop, body=%s", rec.Body.String())
|
||||||
|
|||||||
@@ -200,9 +200,14 @@ func consumeToolCapture(state *toolStreamSieveState, toolNames []string) (prefix
|
|||||||
if insideCodeFence(state.recentTextTail + prefixPart) {
|
if insideCodeFence(state.recentTextTail + prefixPart) {
|
||||||
return captured, nil, "", true
|
return captured, nil, "", true
|
||||||
}
|
}
|
||||||
parsed := util.ParseStandaloneToolCalls(obj, toolNames)
|
parsed := util.ParseStandaloneToolCallsDetailed(obj, toolNames)
|
||||||
if len(parsed) == 0 {
|
if len(parsed.Calls) == 0 {
|
||||||
|
if parsed.SawToolCallSyntax && parsed.RejectedByPolicy {
|
||||||
|
// Parsed as tool-call payload but rejected by schema/policy:
|
||||||
|
// consume it to avoid leaking raw tool_calls JSON to user content.
|
||||||
|
return prefixPart, nil, suffixPart, true
|
||||||
|
}
|
||||||
return captured, nil, "", true
|
return captured, nil, "", true
|
||||||
}
|
}
|
||||||
return prefixPart, parsed, suffixPart, true
|
return prefixPart, parsed.Calls, suffixPart, true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -205,8 +205,17 @@ function consumeToolCapture(state, toolNames) {
|
|||||||
suffix: '',
|
suffix: '',
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
const rawParsed = parseStandaloneToolCalls(captured.slice(start, obj.end), []);
|
||||||
const parsed = parseStandaloneToolCalls(captured.slice(start, obj.end), toolNames);
|
const parsed = parseStandaloneToolCalls(captured.slice(start, obj.end), toolNames);
|
||||||
if (parsed.length === 0) {
|
if (parsed.length === 0) {
|
||||||
|
if (rawParsed.length > 0 && Array.isArray(toolNames) && toolNames.length > 0) {
|
||||||
|
return {
|
||||||
|
ready: true,
|
||||||
|
prefix: prefixPart,
|
||||||
|
calls: [],
|
||||||
|
suffix: suffixPart,
|
||||||
|
};
|
||||||
|
}
|
||||||
if (state.toolNameSent) {
|
if (state.toolNameSent) {
|
||||||
return {
|
return {
|
||||||
ready: true,
|
ready: true,
|
||||||
|
|||||||
@@ -152,6 +152,20 @@ test('sieve keeps plain text intact in tool mode when no tool call appears', ()
|
|||||||
assert.equal(leakedText, '你好,这是普通文本回复。请继续。');
|
assert.equal(leakedText, '你好,这是普通文本回复。请继续。');
|
||||||
});
|
});
|
||||||
|
|
||||||
|
test('sieve intercepts rejected unknown tool payload (no args) without raw leak', () => {
|
||||||
|
const events = runSieve(
|
||||||
|
['{"tool_calls":[{"name":"not_in_schema"}]}', '后置正文G。'],
|
||||||
|
['read_file'],
|
||||||
|
);
|
||||||
|
const leakedText = collectText(events);
|
||||||
|
const hasToolCall = events.some((evt) => evt.type === 'tool_calls' && Array.isArray(evt.calls) && evt.calls.length > 0);
|
||||||
|
const hasToolDelta = events.some((evt) => evt.type === 'tool_call_deltas' && Array.isArray(evt.deltas) && evt.deltas.length > 0);
|
||||||
|
assert.equal(hasToolCall, false);
|
||||||
|
assert.equal(hasToolDelta, false);
|
||||||
|
assert.equal(leakedText.toLowerCase().includes('tool_calls'), false);
|
||||||
|
assert.equal(leakedText.includes('后置正文G。'), true);
|
||||||
|
});
|
||||||
|
|
||||||
test('sieve emits incremental tool_call_deltas for split arguments payload', () => {
|
test('sieve emits incremental tool_call_deltas for split arguments payload', () => {
|
||||||
const state = createToolSieveState();
|
const state = createToolSieveState();
|
||||||
const first = processToolSieveChunk(
|
const first = processToolSieveChunk(
|
||||||
|
|||||||
Reference in New Issue
Block a user